xref: /linux/fs/nfs/client.c (revision 0bae89ec8b1519dae67036637942f5b5bbaa9424)
124c8dbbbSDavid Howells /* client.c: NFS client sharing and management code
224c8dbbbSDavid Howells  *
324c8dbbbSDavid Howells  * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
424c8dbbbSDavid Howells  * Written by David Howells (dhowells@redhat.com)
524c8dbbbSDavid Howells  *
624c8dbbbSDavid Howells  * This program is free software; you can redistribute it and/or
724c8dbbbSDavid Howells  * modify it under the terms of the GNU General Public License
824c8dbbbSDavid Howells  * as published by the Free Software Foundation; either version
924c8dbbbSDavid Howells  * 2 of the License, or (at your option) any later version.
1024c8dbbbSDavid Howells  */
1124c8dbbbSDavid Howells 
1224c8dbbbSDavid Howells 
1324c8dbbbSDavid Howells #include <linux/module.h>
1424c8dbbbSDavid Howells #include <linux/init.h>
1524c8dbbbSDavid Howells 
1624c8dbbbSDavid Howells #include <linux/time.h>
1724c8dbbbSDavid Howells #include <linux/kernel.h>
1824c8dbbbSDavid Howells #include <linux/mm.h>
1924c8dbbbSDavid Howells #include <linux/string.h>
2024c8dbbbSDavid Howells #include <linux/stat.h>
2124c8dbbbSDavid Howells #include <linux/errno.h>
2224c8dbbbSDavid Howells #include <linux/unistd.h>
2324c8dbbbSDavid Howells #include <linux/sunrpc/clnt.h>
2424c8dbbbSDavid Howells #include <linux/sunrpc/stats.h>
2524c8dbbbSDavid Howells #include <linux/sunrpc/metrics.h>
2624c8dbbbSDavid Howells #include <linux/nfs_fs.h>
2724c8dbbbSDavid Howells #include <linux/nfs_mount.h>
2824c8dbbbSDavid Howells #include <linux/nfs4_mount.h>
2924c8dbbbSDavid Howells #include <linux/lockd/bind.h>
3024c8dbbbSDavid Howells #include <linux/smp_lock.h>
3124c8dbbbSDavid Howells #include <linux/seq_file.h>
3224c8dbbbSDavid Howells #include <linux/mount.h>
3324c8dbbbSDavid Howells #include <linux/nfs_idmap.h>
3424c8dbbbSDavid Howells #include <linux/vfs.h>
3524c8dbbbSDavid Howells #include <linux/inet.h>
3624c8dbbbSDavid Howells #include <linux/nfs_xdr.h>
3724c8dbbbSDavid Howells 
3824c8dbbbSDavid Howells #include <asm/system.h>
3924c8dbbbSDavid Howells 
4024c8dbbbSDavid Howells #include "nfs4_fs.h"
4124c8dbbbSDavid Howells #include "callback.h"
4224c8dbbbSDavid Howells #include "delegation.h"
4324c8dbbbSDavid Howells #include "iostat.h"
4424c8dbbbSDavid Howells #include "internal.h"
4524c8dbbbSDavid Howells 
4624c8dbbbSDavid Howells #define NFSDBG_FACILITY		NFSDBG_CLIENT
4724c8dbbbSDavid Howells 
4824c8dbbbSDavid Howells static DEFINE_SPINLOCK(nfs_client_lock);
4924c8dbbbSDavid Howells static LIST_HEAD(nfs_client_list);
5054ceac45SDavid Howells static LIST_HEAD(nfs_volume_list);
5124c8dbbbSDavid Howells static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
5224c8dbbbSDavid Howells 
5324c8dbbbSDavid Howells /*
545006a76cSDavid Howells  * RPC cruft for NFS
555006a76cSDavid Howells  */
565006a76cSDavid Howells static struct rpc_version *nfs_version[5] = {
575006a76cSDavid Howells 	[2]			= &nfs_version2,
585006a76cSDavid Howells #ifdef CONFIG_NFS_V3
595006a76cSDavid Howells 	[3]			= &nfs_version3,
605006a76cSDavid Howells #endif
615006a76cSDavid Howells #ifdef CONFIG_NFS_V4
625006a76cSDavid Howells 	[4]			= &nfs_version4,
635006a76cSDavid Howells #endif
645006a76cSDavid Howells };
655006a76cSDavid Howells 
665006a76cSDavid Howells struct rpc_program nfs_program = {
675006a76cSDavid Howells 	.name			= "nfs",
685006a76cSDavid Howells 	.number			= NFS_PROGRAM,
695006a76cSDavid Howells 	.nrvers			= ARRAY_SIZE(nfs_version),
705006a76cSDavid Howells 	.version		= nfs_version,
715006a76cSDavid Howells 	.stats			= &nfs_rpcstat,
725006a76cSDavid Howells 	.pipe_dir_name		= "/nfs",
735006a76cSDavid Howells };
745006a76cSDavid Howells 
755006a76cSDavid Howells struct rpc_stat nfs_rpcstat = {
765006a76cSDavid Howells 	.program		= &nfs_program
775006a76cSDavid Howells };
785006a76cSDavid Howells 
795006a76cSDavid Howells 
805006a76cSDavid Howells #ifdef CONFIG_NFS_V3_ACL
815006a76cSDavid Howells static struct rpc_stat		nfsacl_rpcstat = { &nfsacl_program };
825006a76cSDavid Howells static struct rpc_version *	nfsacl_version[] = {
835006a76cSDavid Howells 	[3]			= &nfsacl_version3,
845006a76cSDavid Howells };
855006a76cSDavid Howells 
865006a76cSDavid Howells struct rpc_program		nfsacl_program = {
875006a76cSDavid Howells 	.name			= "nfsacl",
885006a76cSDavid Howells 	.number			= NFS_ACL_PROGRAM,
895006a76cSDavid Howells 	.nrvers			= ARRAY_SIZE(nfsacl_version),
905006a76cSDavid Howells 	.version		= nfsacl_version,
915006a76cSDavid Howells 	.stats			= &nfsacl_rpcstat,
925006a76cSDavid Howells };
935006a76cSDavid Howells #endif  /* CONFIG_NFS_V3_ACL */
945006a76cSDavid Howells 
955006a76cSDavid Howells /*
9624c8dbbbSDavid Howells  * Allocate a shared client record
9724c8dbbbSDavid Howells  *
9824c8dbbbSDavid Howells  * Since these are allocated/deallocated very rarely, we don't
9924c8dbbbSDavid Howells  * bother putting them in a slab cache...
10024c8dbbbSDavid Howells  */
10124c8dbbbSDavid Howells static struct nfs_client *nfs_alloc_client(const char *hostname,
10224c8dbbbSDavid Howells 					   const struct sockaddr_in *addr,
10324c8dbbbSDavid Howells 					   int nfsversion)
10424c8dbbbSDavid Howells {
10524c8dbbbSDavid Howells 	struct nfs_client *clp;
10624c8dbbbSDavid Howells 	int error;
10724c8dbbbSDavid Howells 
10824c8dbbbSDavid Howells 	if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
10924c8dbbbSDavid Howells 		goto error_0;
11024c8dbbbSDavid Howells 
11124c8dbbbSDavid Howells 	error = rpciod_up();
11224c8dbbbSDavid Howells 	if (error < 0) {
11324c8dbbbSDavid Howells 		dprintk("%s: couldn't start rpciod! Error = %d\n",
11424c8dbbbSDavid Howells 				__FUNCTION__, error);
11524c8dbbbSDavid Howells 		goto error_1;
11624c8dbbbSDavid Howells 	}
1179c5bf38dSTrond Myklebust 	__set_bit(NFS_CS_RPCIOD, &clp->cl_res_state);
11824c8dbbbSDavid Howells 
11924c8dbbbSDavid Howells 	if (nfsversion == 4) {
12024c8dbbbSDavid Howells 		if (nfs_callback_up() < 0)
12124c8dbbbSDavid Howells 			goto error_2;
12224c8dbbbSDavid Howells 		__set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
12324c8dbbbSDavid Howells 	}
12424c8dbbbSDavid Howells 
12524c8dbbbSDavid Howells 	atomic_set(&clp->cl_count, 1);
12624c8dbbbSDavid Howells 	clp->cl_cons_state = NFS_CS_INITING;
12724c8dbbbSDavid Howells 
12824c8dbbbSDavid Howells 	clp->cl_nfsversion = nfsversion;
12924c8dbbbSDavid Howells 	memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
13024c8dbbbSDavid Howells 
13124c8dbbbSDavid Howells 	if (hostname) {
13224c8dbbbSDavid Howells 		clp->cl_hostname = kstrdup(hostname, GFP_KERNEL);
13324c8dbbbSDavid Howells 		if (!clp->cl_hostname)
13424c8dbbbSDavid Howells 			goto error_3;
13524c8dbbbSDavid Howells 	}
13624c8dbbbSDavid Howells 
13724c8dbbbSDavid Howells 	INIT_LIST_HEAD(&clp->cl_superblocks);
13824c8dbbbSDavid Howells 	clp->cl_rpcclient = ERR_PTR(-EINVAL);
13924c8dbbbSDavid Howells 
14024c8dbbbSDavid Howells #ifdef CONFIG_NFS_V4
14124c8dbbbSDavid Howells 	init_rwsem(&clp->cl_sem);
14224c8dbbbSDavid Howells 	INIT_LIST_HEAD(&clp->cl_delegations);
14324c8dbbbSDavid Howells 	INIT_LIST_HEAD(&clp->cl_state_owners);
14424c8dbbbSDavid Howells 	INIT_LIST_HEAD(&clp->cl_unused);
14524c8dbbbSDavid Howells 	spin_lock_init(&clp->cl_lock);
14624c8dbbbSDavid Howells 	INIT_WORK(&clp->cl_renewd, nfs4_renew_state, clp);
14724c8dbbbSDavid Howells 	rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
14824c8dbbbSDavid Howells 	clp->cl_boot_time = CURRENT_TIME;
14924c8dbbbSDavid Howells 	clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
15024c8dbbbSDavid Howells #endif
15124c8dbbbSDavid Howells 
15224c8dbbbSDavid Howells 	return clp;
15324c8dbbbSDavid Howells 
15424c8dbbbSDavid Howells error_3:
1559c5bf38dSTrond Myklebust 	if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
15624c8dbbbSDavid Howells 		nfs_callback_down();
15724c8dbbbSDavid Howells error_2:
15824c8dbbbSDavid Howells 	rpciod_down();
15924c8dbbbSDavid Howells 	__clear_bit(NFS_CS_RPCIOD, &clp->cl_res_state);
16024c8dbbbSDavid Howells error_1:
16124c8dbbbSDavid Howells 	kfree(clp);
16224c8dbbbSDavid Howells error_0:
16324c8dbbbSDavid Howells 	return NULL;
16424c8dbbbSDavid Howells }
16524c8dbbbSDavid Howells 
1665dd3177aSTrond Myklebust static void nfs4_shutdown_client(struct nfs_client *clp)
16724c8dbbbSDavid Howells {
16824c8dbbbSDavid Howells #ifdef CONFIG_NFS_V4
1695dd3177aSTrond Myklebust 	if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
1705dd3177aSTrond Myklebust 		nfs4_kill_renewd(clp);
17124c8dbbbSDavid Howells 	while (!list_empty(&clp->cl_unused)) {
17224c8dbbbSDavid Howells 		struct nfs4_state_owner *sp;
17324c8dbbbSDavid Howells 
17424c8dbbbSDavid Howells 		sp = list_entry(clp->cl_unused.next,
17524c8dbbbSDavid Howells 				struct nfs4_state_owner,
17624c8dbbbSDavid Howells 				so_list);
17724c8dbbbSDavid Howells 		list_del(&sp->so_list);
17824c8dbbbSDavid Howells 		kfree(sp);
17924c8dbbbSDavid Howells 	}
18024c8dbbbSDavid Howells 	BUG_ON(!list_empty(&clp->cl_state_owners));
1815dd3177aSTrond Myklebust 	if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
18224c8dbbbSDavid Howells 		nfs_idmap_delete(clp);
18324c8dbbbSDavid Howells #endif
1845dd3177aSTrond Myklebust }
1855dd3177aSTrond Myklebust 
1865dd3177aSTrond Myklebust /*
1875dd3177aSTrond Myklebust  * Destroy a shared client record
1885dd3177aSTrond Myklebust  */
1895dd3177aSTrond Myklebust static void nfs_free_client(struct nfs_client *clp)
1905dd3177aSTrond Myklebust {
1915dd3177aSTrond Myklebust 	dprintk("--> nfs_free_client(%d)\n", clp->cl_nfsversion);
1925dd3177aSTrond Myklebust 
1935dd3177aSTrond Myklebust 	nfs4_shutdown_client(clp);
19424c8dbbbSDavid Howells 
19524c8dbbbSDavid Howells 	/* -EIO all pending I/O */
19624c8dbbbSDavid Howells 	if (!IS_ERR(clp->cl_rpcclient))
19724c8dbbbSDavid Howells 		rpc_shutdown_client(clp->cl_rpcclient);
19824c8dbbbSDavid Howells 
19924c8dbbbSDavid Howells 	if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
20024c8dbbbSDavid Howells 		nfs_callback_down();
20124c8dbbbSDavid Howells 
20224c8dbbbSDavid Howells 	if (__test_and_clear_bit(NFS_CS_RPCIOD, &clp->cl_res_state))
20324c8dbbbSDavid Howells 		rpciod_down();
20424c8dbbbSDavid Howells 
20524c8dbbbSDavid Howells 	kfree(clp->cl_hostname);
20624c8dbbbSDavid Howells 	kfree(clp);
20724c8dbbbSDavid Howells 
20824c8dbbbSDavid Howells 	dprintk("<-- nfs_free_client()\n");
20924c8dbbbSDavid Howells }
21024c8dbbbSDavid Howells 
21124c8dbbbSDavid Howells /*
21224c8dbbbSDavid Howells  * Release a reference to a shared client record
21324c8dbbbSDavid Howells  */
21424c8dbbbSDavid Howells void nfs_put_client(struct nfs_client *clp)
21524c8dbbbSDavid Howells {
21627ba8512SDavid Howells 	if (!clp)
21727ba8512SDavid Howells 		return;
21827ba8512SDavid Howells 
21924c8dbbbSDavid Howells 	dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
22024c8dbbbSDavid Howells 
22124c8dbbbSDavid Howells 	if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
22224c8dbbbSDavid Howells 		list_del(&clp->cl_share_link);
22324c8dbbbSDavid Howells 		spin_unlock(&nfs_client_lock);
22424c8dbbbSDavid Howells 
22524c8dbbbSDavid Howells 		BUG_ON(!list_empty(&clp->cl_superblocks));
22624c8dbbbSDavid Howells 
22724c8dbbbSDavid Howells 		nfs_free_client(clp);
22824c8dbbbSDavid Howells 	}
22924c8dbbbSDavid Howells }
23024c8dbbbSDavid Howells 
23124c8dbbbSDavid Howells /*
23224c8dbbbSDavid Howells  * Find a client by address
23324c8dbbbSDavid Howells  * - caller must hold nfs_client_lock
23424c8dbbbSDavid Howells  */
23524c8dbbbSDavid Howells static struct nfs_client *__nfs_find_client(const struct sockaddr_in *addr, int nfsversion)
23624c8dbbbSDavid Howells {
23724c8dbbbSDavid Howells 	struct nfs_client *clp;
23824c8dbbbSDavid Howells 
23924c8dbbbSDavid Howells 	list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
24024c8dbbbSDavid Howells 		/* Different NFS versions cannot share the same nfs_client */
24124c8dbbbSDavid Howells 		if (clp->cl_nfsversion != nfsversion)
24224c8dbbbSDavid Howells 			continue;
24324c8dbbbSDavid Howells 
24424c8dbbbSDavid Howells 		if (memcmp(&clp->cl_addr.sin_addr, &addr->sin_addr,
24524c8dbbbSDavid Howells 			   sizeof(clp->cl_addr.sin_addr)) != 0)
24624c8dbbbSDavid Howells 			continue;
24724c8dbbbSDavid Howells 
24824c8dbbbSDavid Howells 		if (clp->cl_addr.sin_port == addr->sin_port)
24924c8dbbbSDavid Howells 			goto found;
25024c8dbbbSDavid Howells 	}
25124c8dbbbSDavid Howells 
25224c8dbbbSDavid Howells 	return NULL;
25324c8dbbbSDavid Howells 
25424c8dbbbSDavid Howells found:
25524c8dbbbSDavid Howells 	atomic_inc(&clp->cl_count);
25624c8dbbbSDavid Howells 	return clp;
25724c8dbbbSDavid Howells }
25824c8dbbbSDavid Howells 
25924c8dbbbSDavid Howells /*
26024c8dbbbSDavid Howells  * Find a client by IP address and protocol version
26124c8dbbbSDavid Howells  * - returns NULL if no such client
26224c8dbbbSDavid Howells  */
26324c8dbbbSDavid Howells struct nfs_client *nfs_find_client(const struct sockaddr_in *addr, int nfsversion)
26424c8dbbbSDavid Howells {
26524c8dbbbSDavid Howells 	struct nfs_client *clp;
26624c8dbbbSDavid Howells 
26724c8dbbbSDavid Howells 	spin_lock(&nfs_client_lock);
26824c8dbbbSDavid Howells 	clp = __nfs_find_client(addr, nfsversion);
26924c8dbbbSDavid Howells 	spin_unlock(&nfs_client_lock);
27024c8dbbbSDavid Howells 
2712dec5146SJ. Bruce Fields 	BUG_ON(clp && clp->cl_cons_state == 0);
27224c8dbbbSDavid Howells 
27324c8dbbbSDavid Howells 	return clp;
27424c8dbbbSDavid Howells }
27524c8dbbbSDavid Howells 
27624c8dbbbSDavid Howells /*
27724c8dbbbSDavid Howells  * Look up a client by IP address and protocol version
27824c8dbbbSDavid Howells  * - creates a new record if one doesn't yet exist
27924c8dbbbSDavid Howells  */
28054ceac45SDavid Howells static struct nfs_client *nfs_get_client(const char *hostname,
28124c8dbbbSDavid Howells 					 const struct sockaddr_in *addr,
28224c8dbbbSDavid Howells 					 int nfsversion)
28324c8dbbbSDavid Howells {
28424c8dbbbSDavid Howells 	struct nfs_client *clp, *new = NULL;
28524c8dbbbSDavid Howells 	int error;
28624c8dbbbSDavid Howells 
28724c8dbbbSDavid Howells 	dprintk("--> nfs_get_client(%s,"NIPQUAD_FMT":%d,%d)\n",
28824c8dbbbSDavid Howells 		hostname ?: "", NIPQUAD(addr->sin_addr),
28924c8dbbbSDavid Howells 		addr->sin_port, nfsversion);
29024c8dbbbSDavid Howells 
29124c8dbbbSDavid Howells 	/* see if the client already exists */
29224c8dbbbSDavid Howells 	do {
29324c8dbbbSDavid Howells 		spin_lock(&nfs_client_lock);
29424c8dbbbSDavid Howells 
29524c8dbbbSDavid Howells 		clp = __nfs_find_client(addr, nfsversion);
29624c8dbbbSDavid Howells 		if (clp)
29724c8dbbbSDavid Howells 			goto found_client;
29824c8dbbbSDavid Howells 		if (new)
29924c8dbbbSDavid Howells 			goto install_client;
30024c8dbbbSDavid Howells 
30124c8dbbbSDavid Howells 		spin_unlock(&nfs_client_lock);
30224c8dbbbSDavid Howells 
30324c8dbbbSDavid Howells 		new = nfs_alloc_client(hostname, addr, nfsversion);
30424c8dbbbSDavid Howells 	} while (new);
30524c8dbbbSDavid Howells 
30624c8dbbbSDavid Howells 	return ERR_PTR(-ENOMEM);
30724c8dbbbSDavid Howells 
30824c8dbbbSDavid Howells 	/* install a new client and return with it unready */
30924c8dbbbSDavid Howells install_client:
31024c8dbbbSDavid Howells 	clp = new;
31124c8dbbbSDavid Howells 	list_add(&clp->cl_share_link, &nfs_client_list);
31224c8dbbbSDavid Howells 	spin_unlock(&nfs_client_lock);
31324c8dbbbSDavid Howells 	dprintk("--> nfs_get_client() = %p [new]\n", clp);
31424c8dbbbSDavid Howells 	return clp;
31524c8dbbbSDavid Howells 
31624c8dbbbSDavid Howells 	/* found an existing client
31724c8dbbbSDavid Howells 	 * - make sure it's ready before returning
31824c8dbbbSDavid Howells 	 */
31924c8dbbbSDavid Howells found_client:
32024c8dbbbSDavid Howells 	spin_unlock(&nfs_client_lock);
32124c8dbbbSDavid Howells 
32224c8dbbbSDavid Howells 	if (new)
32324c8dbbbSDavid Howells 		nfs_free_client(new);
32424c8dbbbSDavid Howells 
325*0bae89ecSTrond Myklebust 	error = wait_event_interruptible(&nfs_client_active_wq,
326*0bae89ecSTrond Myklebust 				clp->cl_cons_state != NFS_CS_INITING);
327*0bae89ecSTrond Myklebust 	if (error < 0) {
32824c8dbbbSDavid Howells 		nfs_put_client(clp);
32924c8dbbbSDavid Howells 		return ERR_PTR(-ERESTARTSYS);
33024c8dbbbSDavid Howells 	}
33124c8dbbbSDavid Howells 
33224c8dbbbSDavid Howells 	if (clp->cl_cons_state < NFS_CS_READY) {
33324c8dbbbSDavid Howells 		error = clp->cl_cons_state;
33424c8dbbbSDavid Howells 		nfs_put_client(clp);
33524c8dbbbSDavid Howells 		return ERR_PTR(error);
33624c8dbbbSDavid Howells 	}
33724c8dbbbSDavid Howells 
33854ceac45SDavid Howells 	BUG_ON(clp->cl_cons_state != NFS_CS_READY);
33954ceac45SDavid Howells 
34024c8dbbbSDavid Howells 	dprintk("--> nfs_get_client() = %p [share]\n", clp);
34124c8dbbbSDavid Howells 	return clp;
34224c8dbbbSDavid Howells }
34324c8dbbbSDavid Howells 
34424c8dbbbSDavid Howells /*
34524c8dbbbSDavid Howells  * Mark a server as ready or failed
34624c8dbbbSDavid Howells  */
34754ceac45SDavid Howells static void nfs_mark_client_ready(struct nfs_client *clp, int state)
34824c8dbbbSDavid Howells {
34924c8dbbbSDavid Howells 	clp->cl_cons_state = state;
35024c8dbbbSDavid Howells 	wake_up_all(&nfs_client_active_wq);
35124c8dbbbSDavid Howells }
3525006a76cSDavid Howells 
3535006a76cSDavid Howells /*
3545006a76cSDavid Howells  * Initialise the timeout values for a connection
3555006a76cSDavid Howells  */
3565006a76cSDavid Howells static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
3575006a76cSDavid Howells 				    unsigned int timeo, unsigned int retrans)
3585006a76cSDavid Howells {
3595006a76cSDavid Howells 	to->to_initval = timeo * HZ / 10;
3605006a76cSDavid Howells 	to->to_retries = retrans;
3615006a76cSDavid Howells 	if (!to->to_retries)
3625006a76cSDavid Howells 		to->to_retries = 2;
3635006a76cSDavid Howells 
3645006a76cSDavid Howells 	switch (proto) {
3655006a76cSDavid Howells 	case IPPROTO_TCP:
3665006a76cSDavid Howells 		if (!to->to_initval)
3675006a76cSDavid Howells 			to->to_initval = 60 * HZ;
3685006a76cSDavid Howells 		if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
3695006a76cSDavid Howells 			to->to_initval = NFS_MAX_TCP_TIMEOUT;
3705006a76cSDavid Howells 		to->to_increment = to->to_initval;
3715006a76cSDavid Howells 		to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
3725006a76cSDavid Howells 		to->to_exponential = 0;
3735006a76cSDavid Howells 		break;
3745006a76cSDavid Howells 	case IPPROTO_UDP:
3755006a76cSDavid Howells 	default:
3765006a76cSDavid Howells 		if (!to->to_initval)
3775006a76cSDavid Howells 			to->to_initval = 11 * HZ / 10;
3785006a76cSDavid Howells 		if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
3795006a76cSDavid Howells 			to->to_initval = NFS_MAX_UDP_TIMEOUT;
3805006a76cSDavid Howells 		to->to_maxval = NFS_MAX_UDP_TIMEOUT;
3815006a76cSDavid Howells 		to->to_exponential = 1;
3825006a76cSDavid Howells 		break;
3835006a76cSDavid Howells 	}
3845006a76cSDavid Howells }
3855006a76cSDavid Howells 
3865006a76cSDavid Howells /*
3875006a76cSDavid Howells  * Create an RPC client handle
3885006a76cSDavid Howells  */
38954ceac45SDavid Howells static int nfs_create_rpc_client(struct nfs_client *clp, int proto,
3905006a76cSDavid Howells 						unsigned int timeo,
3915006a76cSDavid Howells 						unsigned int retrans,
3925006a76cSDavid Howells 						rpc_authflavor_t flavor)
3935006a76cSDavid Howells {
3945006a76cSDavid Howells 	struct rpc_timeout	timeparms;
3955006a76cSDavid Howells 	struct rpc_clnt		*clnt = NULL;
39641877d20SChuck Lever 	struct rpc_create_args args = {
39741877d20SChuck Lever 		.protocol	= proto,
39841877d20SChuck Lever 		.address	= (struct sockaddr *)&clp->cl_addr,
39941877d20SChuck Lever 		.addrsize	= sizeof(clp->cl_addr),
40041877d20SChuck Lever 		.timeout	= &timeparms,
40141877d20SChuck Lever 		.servername	= clp->cl_hostname,
40241877d20SChuck Lever 		.program	= &nfs_program,
40341877d20SChuck Lever 		.version	= clp->rpc_ops->version,
40441877d20SChuck Lever 		.authflavor	= flavor,
40541877d20SChuck Lever 	};
4065006a76cSDavid Howells 
4075006a76cSDavid Howells 	if (!IS_ERR(clp->cl_rpcclient))
4085006a76cSDavid Howells 		return 0;
4095006a76cSDavid Howells 
4105006a76cSDavid Howells 	nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
4115006a76cSDavid Howells 	clp->retrans_timeo = timeparms.to_initval;
4125006a76cSDavid Howells 	clp->retrans_count = timeparms.to_retries;
4135006a76cSDavid Howells 
41441877d20SChuck Lever 	clnt = rpc_create(&args);
4155006a76cSDavid Howells 	if (IS_ERR(clnt)) {
4165006a76cSDavid Howells 		dprintk("%s: cannot create RPC client. Error = %ld\n",
4175006a76cSDavid Howells 				__FUNCTION__, PTR_ERR(clnt));
4185006a76cSDavid Howells 		return PTR_ERR(clnt);
4195006a76cSDavid Howells 	}
4205006a76cSDavid Howells 
4215006a76cSDavid Howells 	clp->cl_rpcclient = clnt;
4225006a76cSDavid Howells 	return 0;
4235006a76cSDavid Howells }
42454ceac45SDavid Howells 
42554ceac45SDavid Howells /*
42654ceac45SDavid Howells  * Version 2 or 3 client destruction
42754ceac45SDavid Howells  */
42854ceac45SDavid Howells static void nfs_destroy_server(struct nfs_server *server)
42954ceac45SDavid Howells {
43054ceac45SDavid Howells 	if (!IS_ERR(server->client_acl))
43154ceac45SDavid Howells 		rpc_shutdown_client(server->client_acl);
43254ceac45SDavid Howells 
43354ceac45SDavid Howells 	if (!(server->flags & NFS_MOUNT_NONLM))
43454ceac45SDavid Howells 		lockd_down();	/* release rpc.lockd */
43554ceac45SDavid Howells }
43654ceac45SDavid Howells 
43754ceac45SDavid Howells /*
43854ceac45SDavid Howells  * Version 2 or 3 lockd setup
43954ceac45SDavid Howells  */
44054ceac45SDavid Howells static int nfs_start_lockd(struct nfs_server *server)
44154ceac45SDavid Howells {
44254ceac45SDavid Howells 	int error = 0;
44354ceac45SDavid Howells 
44454ceac45SDavid Howells 	if (server->nfs_client->cl_nfsversion > 3)
44554ceac45SDavid Howells 		goto out;
44654ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_NONLM)
44754ceac45SDavid Howells 		goto out;
44824e36663SNeilBrown 	error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
44924e36663SNeilBrown 			IPPROTO_TCP : IPPROTO_UDP);
45054ceac45SDavid Howells 	if (error < 0)
45154ceac45SDavid Howells 		server->flags |= NFS_MOUNT_NONLM;
45254ceac45SDavid Howells 	else
45354ceac45SDavid Howells 		server->destroy = nfs_destroy_server;
45454ceac45SDavid Howells out:
45554ceac45SDavid Howells 	return error;
45654ceac45SDavid Howells }
45754ceac45SDavid Howells 
45854ceac45SDavid Howells /*
45954ceac45SDavid Howells  * Initialise an NFSv3 ACL client connection
46054ceac45SDavid Howells  */
46154ceac45SDavid Howells #ifdef CONFIG_NFS_V3_ACL
46254ceac45SDavid Howells static void nfs_init_server_aclclient(struct nfs_server *server)
46354ceac45SDavid Howells {
46454ceac45SDavid Howells 	if (server->nfs_client->cl_nfsversion != 3)
46554ceac45SDavid Howells 		goto out_noacl;
46654ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_NOACL)
46754ceac45SDavid Howells 		goto out_noacl;
46854ceac45SDavid Howells 
46954ceac45SDavid Howells 	server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
47054ceac45SDavid Howells 	if (IS_ERR(server->client_acl))
47154ceac45SDavid Howells 		goto out_noacl;
47254ceac45SDavid Howells 
47354ceac45SDavid Howells 	/* No errors! Assume that Sun nfsacls are supported */
47454ceac45SDavid Howells 	server->caps |= NFS_CAP_ACLS;
47554ceac45SDavid Howells 	return;
47654ceac45SDavid Howells 
47754ceac45SDavid Howells out_noacl:
47854ceac45SDavid Howells 	server->caps &= ~NFS_CAP_ACLS;
47954ceac45SDavid Howells }
48054ceac45SDavid Howells #else
48154ceac45SDavid Howells static inline void nfs_init_server_aclclient(struct nfs_server *server)
48254ceac45SDavid Howells {
48354ceac45SDavid Howells 	server->flags &= ~NFS_MOUNT_NOACL;
48454ceac45SDavid Howells 	server->caps &= ~NFS_CAP_ACLS;
48554ceac45SDavid Howells }
48654ceac45SDavid Howells #endif
48754ceac45SDavid Howells 
48854ceac45SDavid Howells /*
48954ceac45SDavid Howells  * Create a general RPC client
49054ceac45SDavid Howells  */
49154ceac45SDavid Howells static int nfs_init_server_rpcclient(struct nfs_server *server, rpc_authflavor_t pseudoflavour)
49254ceac45SDavid Howells {
49354ceac45SDavid Howells 	struct nfs_client *clp = server->nfs_client;
49454ceac45SDavid Howells 
49554ceac45SDavid Howells 	server->client = rpc_clone_client(clp->cl_rpcclient);
49654ceac45SDavid Howells 	if (IS_ERR(server->client)) {
49754ceac45SDavid Howells 		dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
49854ceac45SDavid Howells 		return PTR_ERR(server->client);
49954ceac45SDavid Howells 	}
50054ceac45SDavid Howells 
50154ceac45SDavid Howells 	if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
50254ceac45SDavid Howells 		struct rpc_auth *auth;
50354ceac45SDavid Howells 
50454ceac45SDavid Howells 		auth = rpcauth_create(pseudoflavour, server->client);
50554ceac45SDavid Howells 		if (IS_ERR(auth)) {
50654ceac45SDavid Howells 			dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
50754ceac45SDavid Howells 			return PTR_ERR(auth);
50854ceac45SDavid Howells 		}
50954ceac45SDavid Howells 	}
51054ceac45SDavid Howells 	server->client->cl_softrtry = 0;
51154ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_SOFT)
51254ceac45SDavid Howells 		server->client->cl_softrtry = 1;
51354ceac45SDavid Howells 
51454ceac45SDavid Howells 	server->client->cl_intr = 0;
51554ceac45SDavid Howells 	if (server->flags & NFS4_MOUNT_INTR)
51654ceac45SDavid Howells 		server->client->cl_intr = 1;
51754ceac45SDavid Howells 
51854ceac45SDavid Howells 	return 0;
51954ceac45SDavid Howells }
52054ceac45SDavid Howells 
52154ceac45SDavid Howells /*
52254ceac45SDavid Howells  * Initialise an NFS2 or NFS3 client
52354ceac45SDavid Howells  */
52454ceac45SDavid Howells static int nfs_init_client(struct nfs_client *clp, const struct nfs_mount_data *data)
52554ceac45SDavid Howells {
52654ceac45SDavid Howells 	int proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
52754ceac45SDavid Howells 	int error;
52854ceac45SDavid Howells 
52954ceac45SDavid Howells 	if (clp->cl_cons_state == NFS_CS_READY) {
53054ceac45SDavid Howells 		/* the client is already initialised */
53154ceac45SDavid Howells 		dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
53254ceac45SDavid Howells 		return 0;
53354ceac45SDavid Howells 	}
53454ceac45SDavid Howells 
53554ceac45SDavid Howells 	/* Check NFS protocol revision and initialize RPC op vector */
53654ceac45SDavid Howells 	clp->rpc_ops = &nfs_v2_clientops;
53754ceac45SDavid Howells #ifdef CONFIG_NFS_V3
53854ceac45SDavid Howells 	if (clp->cl_nfsversion == 3)
53954ceac45SDavid Howells 		clp->rpc_ops = &nfs_v3_clientops;
54054ceac45SDavid Howells #endif
54154ceac45SDavid Howells 	/*
54254ceac45SDavid Howells 	 * Create a client RPC handle for doing FSSTAT with UNIX auth only
54354ceac45SDavid Howells 	 * - RFC 2623, sec 2.3.2
54454ceac45SDavid Howells 	 */
54554ceac45SDavid Howells 	error = nfs_create_rpc_client(clp, proto, data->timeo, data->retrans,
54654ceac45SDavid Howells 			RPC_AUTH_UNIX);
54754ceac45SDavid Howells 	if (error < 0)
54854ceac45SDavid Howells 		goto error;
54954ceac45SDavid Howells 	nfs_mark_client_ready(clp, NFS_CS_READY);
55054ceac45SDavid Howells 	return 0;
55154ceac45SDavid Howells 
55254ceac45SDavid Howells error:
55354ceac45SDavid Howells 	nfs_mark_client_ready(clp, error);
55454ceac45SDavid Howells 	dprintk("<-- nfs_init_client() = xerror %d\n", error);
55554ceac45SDavid Howells 	return error;
55654ceac45SDavid Howells }
55754ceac45SDavid Howells 
55854ceac45SDavid Howells /*
55954ceac45SDavid Howells  * Create a version 2 or 3 client
56054ceac45SDavid Howells  */
56154ceac45SDavid Howells static int nfs_init_server(struct nfs_server *server, const struct nfs_mount_data *data)
56254ceac45SDavid Howells {
56354ceac45SDavid Howells 	struct nfs_client *clp;
56454ceac45SDavid Howells 	int error, nfsvers = 2;
56554ceac45SDavid Howells 
56654ceac45SDavid Howells 	dprintk("--> nfs_init_server()\n");
56754ceac45SDavid Howells 
56854ceac45SDavid Howells #ifdef CONFIG_NFS_V3
56954ceac45SDavid Howells 	if (data->flags & NFS_MOUNT_VER3)
57054ceac45SDavid Howells 		nfsvers = 3;
57154ceac45SDavid Howells #endif
57254ceac45SDavid Howells 
57354ceac45SDavid Howells 	/* Allocate or find a client reference we can use */
57454ceac45SDavid Howells 	clp = nfs_get_client(data->hostname, &data->addr, nfsvers);
57554ceac45SDavid Howells 	if (IS_ERR(clp)) {
57654ceac45SDavid Howells 		dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
57754ceac45SDavid Howells 		return PTR_ERR(clp);
57854ceac45SDavid Howells 	}
57954ceac45SDavid Howells 
58054ceac45SDavid Howells 	error = nfs_init_client(clp, data);
58154ceac45SDavid Howells 	if (error < 0)
58254ceac45SDavid Howells 		goto error;
58354ceac45SDavid Howells 
58454ceac45SDavid Howells 	server->nfs_client = clp;
58554ceac45SDavid Howells 
58654ceac45SDavid Howells 	/* Initialise the client representation from the mount data */
58754ceac45SDavid Howells 	server->flags = data->flags & NFS_MOUNT_FLAGMASK;
58854ceac45SDavid Howells 
58954ceac45SDavid Howells 	if (data->rsize)
59054ceac45SDavid Howells 		server->rsize = nfs_block_size(data->rsize, NULL);
59154ceac45SDavid Howells 	if (data->wsize)
59254ceac45SDavid Howells 		server->wsize = nfs_block_size(data->wsize, NULL);
59354ceac45SDavid Howells 
59454ceac45SDavid Howells 	server->acregmin = data->acregmin * HZ;
59554ceac45SDavid Howells 	server->acregmax = data->acregmax * HZ;
59654ceac45SDavid Howells 	server->acdirmin = data->acdirmin * HZ;
59754ceac45SDavid Howells 	server->acdirmax = data->acdirmax * HZ;
59854ceac45SDavid Howells 
59954ceac45SDavid Howells 	/* Start lockd here, before we might error out */
60054ceac45SDavid Howells 	error = nfs_start_lockd(server);
60154ceac45SDavid Howells 	if (error < 0)
60254ceac45SDavid Howells 		goto error;
60354ceac45SDavid Howells 
60454ceac45SDavid Howells 	error = nfs_init_server_rpcclient(server, data->pseudoflavor);
60554ceac45SDavid Howells 	if (error < 0)
60654ceac45SDavid Howells 		goto error;
60754ceac45SDavid Howells 
60854ceac45SDavid Howells 	server->namelen  = data->namlen;
60954ceac45SDavid Howells 	/* Create a client RPC handle for the NFSv3 ACL management interface */
61054ceac45SDavid Howells 	nfs_init_server_aclclient(server);
61154ceac45SDavid Howells 	if (clp->cl_nfsversion == 3) {
61254ceac45SDavid Howells 		if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
61354ceac45SDavid Howells 			server->namelen = NFS3_MAXNAMLEN;
61454ceac45SDavid Howells 		server->caps |= NFS_CAP_READDIRPLUS;
61554ceac45SDavid Howells 	} else {
61654ceac45SDavid Howells 		if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
61754ceac45SDavid Howells 			server->namelen = NFS2_MAXNAMLEN;
61854ceac45SDavid Howells 	}
61954ceac45SDavid Howells 
62054ceac45SDavid Howells 	dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
62154ceac45SDavid Howells 	return 0;
62254ceac45SDavid Howells 
62354ceac45SDavid Howells error:
62454ceac45SDavid Howells 	server->nfs_client = NULL;
62554ceac45SDavid Howells 	nfs_put_client(clp);
62654ceac45SDavid Howells 	dprintk("<-- nfs_init_server() = xerror %d\n", error);
62754ceac45SDavid Howells 	return error;
62854ceac45SDavid Howells }
62954ceac45SDavid Howells 
63054ceac45SDavid Howells /*
63154ceac45SDavid Howells  * Load up the server record from information gained in an fsinfo record
63254ceac45SDavid Howells  */
63354ceac45SDavid Howells static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
63454ceac45SDavid Howells {
63554ceac45SDavid Howells 	unsigned long max_rpc_payload;
63654ceac45SDavid Howells 
63754ceac45SDavid Howells 	/* Work out a lot of parameters */
63854ceac45SDavid Howells 	if (server->rsize == 0)
63954ceac45SDavid Howells 		server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
64054ceac45SDavid Howells 	if (server->wsize == 0)
64154ceac45SDavid Howells 		server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
64254ceac45SDavid Howells 
64354ceac45SDavid Howells 	if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
64454ceac45SDavid Howells 		server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
64554ceac45SDavid Howells 	if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
64654ceac45SDavid Howells 		server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
64754ceac45SDavid Howells 
64854ceac45SDavid Howells 	max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
64954ceac45SDavid Howells 	if (server->rsize > max_rpc_payload)
65054ceac45SDavid Howells 		server->rsize = max_rpc_payload;
65154ceac45SDavid Howells 	if (server->rsize > NFS_MAX_FILE_IO_SIZE)
65254ceac45SDavid Howells 		server->rsize = NFS_MAX_FILE_IO_SIZE;
65354ceac45SDavid Howells 	server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
65454ceac45SDavid Howells 	server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
65554ceac45SDavid Howells 
65654ceac45SDavid Howells 	if (server->wsize > max_rpc_payload)
65754ceac45SDavid Howells 		server->wsize = max_rpc_payload;
65854ceac45SDavid Howells 	if (server->wsize > NFS_MAX_FILE_IO_SIZE)
65954ceac45SDavid Howells 		server->wsize = NFS_MAX_FILE_IO_SIZE;
66054ceac45SDavid Howells 	server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
66154ceac45SDavid Howells 	server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
66254ceac45SDavid Howells 
66354ceac45SDavid Howells 	server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
66454ceac45SDavid Howells 	if (server->dtsize > PAGE_CACHE_SIZE)
66554ceac45SDavid Howells 		server->dtsize = PAGE_CACHE_SIZE;
66654ceac45SDavid Howells 	if (server->dtsize > server->rsize)
66754ceac45SDavid Howells 		server->dtsize = server->rsize;
66854ceac45SDavid Howells 
66954ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_NOAC) {
67054ceac45SDavid Howells 		server->acregmin = server->acregmax = 0;
67154ceac45SDavid Howells 		server->acdirmin = server->acdirmax = 0;
67254ceac45SDavid Howells 	}
67354ceac45SDavid Howells 
67454ceac45SDavid Howells 	server->maxfilesize = fsinfo->maxfilesize;
67554ceac45SDavid Howells 
67654ceac45SDavid Howells 	/* We're airborne Set socket buffersize */
67754ceac45SDavid Howells 	rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
67854ceac45SDavid Howells }
67954ceac45SDavid Howells 
68054ceac45SDavid Howells /*
68154ceac45SDavid Howells  * Probe filesystem information, including the FSID on v2/v3
68254ceac45SDavid Howells  */
68354ceac45SDavid Howells static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
68454ceac45SDavid Howells {
68554ceac45SDavid Howells 	struct nfs_fsinfo fsinfo;
68654ceac45SDavid Howells 	struct nfs_client *clp = server->nfs_client;
68754ceac45SDavid Howells 	int error;
68854ceac45SDavid Howells 
68954ceac45SDavid Howells 	dprintk("--> nfs_probe_fsinfo()\n");
69054ceac45SDavid Howells 
69154ceac45SDavid Howells 	if (clp->rpc_ops->set_capabilities != NULL) {
69254ceac45SDavid Howells 		error = clp->rpc_ops->set_capabilities(server, mntfh);
69354ceac45SDavid Howells 		if (error < 0)
69454ceac45SDavid Howells 			goto out_error;
69554ceac45SDavid Howells 	}
69654ceac45SDavid Howells 
69754ceac45SDavid Howells 	fsinfo.fattr = fattr;
69854ceac45SDavid Howells 	nfs_fattr_init(fattr);
69954ceac45SDavid Howells 	error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
70054ceac45SDavid Howells 	if (error < 0)
70154ceac45SDavid Howells 		goto out_error;
70254ceac45SDavid Howells 
70354ceac45SDavid Howells 	nfs_server_set_fsinfo(server, &fsinfo);
70454ceac45SDavid Howells 
70554ceac45SDavid Howells 	/* Get some general file system info */
70654ceac45SDavid Howells 	if (server->namelen == 0) {
70754ceac45SDavid Howells 		struct nfs_pathconf pathinfo;
70854ceac45SDavid Howells 
70954ceac45SDavid Howells 		pathinfo.fattr = fattr;
71054ceac45SDavid Howells 		nfs_fattr_init(fattr);
71154ceac45SDavid Howells 
71254ceac45SDavid Howells 		if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
71354ceac45SDavid Howells 			server->namelen = pathinfo.max_namelen;
71454ceac45SDavid Howells 	}
71554ceac45SDavid Howells 
71654ceac45SDavid Howells 	dprintk("<-- nfs_probe_fsinfo() = 0\n");
71754ceac45SDavid Howells 	return 0;
71854ceac45SDavid Howells 
71954ceac45SDavid Howells out_error:
72054ceac45SDavid Howells 	dprintk("nfs_probe_fsinfo: error = %d\n", -error);
72154ceac45SDavid Howells 	return error;
72254ceac45SDavid Howells }
72354ceac45SDavid Howells 
72454ceac45SDavid Howells /*
72554ceac45SDavid Howells  * Copy useful information when duplicating a server record
72654ceac45SDavid Howells  */
72754ceac45SDavid Howells static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
72854ceac45SDavid Howells {
72954ceac45SDavid Howells 	target->flags = source->flags;
73054ceac45SDavid Howells 	target->acregmin = source->acregmin;
73154ceac45SDavid Howells 	target->acregmax = source->acregmax;
73254ceac45SDavid Howells 	target->acdirmin = source->acdirmin;
73354ceac45SDavid Howells 	target->acdirmax = source->acdirmax;
73454ceac45SDavid Howells 	target->caps = source->caps;
73554ceac45SDavid Howells }
73654ceac45SDavid Howells 
73754ceac45SDavid Howells /*
73854ceac45SDavid Howells  * Allocate and initialise a server record
73954ceac45SDavid Howells  */
74054ceac45SDavid Howells static struct nfs_server *nfs_alloc_server(void)
74154ceac45SDavid Howells {
74254ceac45SDavid Howells 	struct nfs_server *server;
74354ceac45SDavid Howells 
74454ceac45SDavid Howells 	server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
74554ceac45SDavid Howells 	if (!server)
74654ceac45SDavid Howells 		return NULL;
74754ceac45SDavid Howells 
74854ceac45SDavid Howells 	server->client = server->client_acl = ERR_PTR(-EINVAL);
74954ceac45SDavid Howells 
75054ceac45SDavid Howells 	/* Zero out the NFS state stuff */
75154ceac45SDavid Howells 	INIT_LIST_HEAD(&server->client_link);
75254ceac45SDavid Howells 	INIT_LIST_HEAD(&server->master_link);
75354ceac45SDavid Howells 
75454ceac45SDavid Howells 	server->io_stats = nfs_alloc_iostats();
75554ceac45SDavid Howells 	if (!server->io_stats) {
75654ceac45SDavid Howells 		kfree(server);
75754ceac45SDavid Howells 		return NULL;
75854ceac45SDavid Howells 	}
75954ceac45SDavid Howells 
76054ceac45SDavid Howells 	return server;
76154ceac45SDavid Howells }
76254ceac45SDavid Howells 
76354ceac45SDavid Howells /*
76454ceac45SDavid Howells  * Free up a server record
76554ceac45SDavid Howells  */
76654ceac45SDavid Howells void nfs_free_server(struct nfs_server *server)
76754ceac45SDavid Howells {
76854ceac45SDavid Howells 	dprintk("--> nfs_free_server()\n");
76954ceac45SDavid Howells 
77054ceac45SDavid Howells 	spin_lock(&nfs_client_lock);
77154ceac45SDavid Howells 	list_del(&server->client_link);
77254ceac45SDavid Howells 	list_del(&server->master_link);
77354ceac45SDavid Howells 	spin_unlock(&nfs_client_lock);
77454ceac45SDavid Howells 
77554ceac45SDavid Howells 	if (server->destroy != NULL)
77654ceac45SDavid Howells 		server->destroy(server);
77754ceac45SDavid Howells 	if (!IS_ERR(server->client))
77854ceac45SDavid Howells 		rpc_shutdown_client(server->client);
77954ceac45SDavid Howells 
78054ceac45SDavid Howells 	nfs_put_client(server->nfs_client);
78154ceac45SDavid Howells 
78254ceac45SDavid Howells 	nfs_free_iostats(server->io_stats);
78354ceac45SDavid Howells 	kfree(server);
78454ceac45SDavid Howells 	nfs_release_automount_timer();
78554ceac45SDavid Howells 	dprintk("<-- nfs_free_server()\n");
78654ceac45SDavid Howells }
78754ceac45SDavid Howells 
78854ceac45SDavid Howells /*
78954ceac45SDavid Howells  * Create a version 2 or 3 volume record
79054ceac45SDavid Howells  * - keyed on server and FSID
79154ceac45SDavid Howells  */
79254ceac45SDavid Howells struct nfs_server *nfs_create_server(const struct nfs_mount_data *data,
79354ceac45SDavid Howells 				     struct nfs_fh *mntfh)
79454ceac45SDavid Howells {
79554ceac45SDavid Howells 	struct nfs_server *server;
79654ceac45SDavid Howells 	struct nfs_fattr fattr;
79754ceac45SDavid Howells 	int error;
79854ceac45SDavid Howells 
79954ceac45SDavid Howells 	server = nfs_alloc_server();
80054ceac45SDavid Howells 	if (!server)
80154ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
80254ceac45SDavid Howells 
80354ceac45SDavid Howells 	/* Get a client representation */
80454ceac45SDavid Howells 	error = nfs_init_server(server, data);
80554ceac45SDavid Howells 	if (error < 0)
80654ceac45SDavid Howells 		goto error;
80754ceac45SDavid Howells 
80854ceac45SDavid Howells 	BUG_ON(!server->nfs_client);
80954ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops);
81054ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
81154ceac45SDavid Howells 
81254ceac45SDavid Howells 	/* Probe the root fh to retrieve its FSID */
81354ceac45SDavid Howells 	error = nfs_probe_fsinfo(server, mntfh, &fattr);
81454ceac45SDavid Howells 	if (error < 0)
81554ceac45SDavid Howells 		goto error;
81654ceac45SDavid Howells 	if (!(fattr.valid & NFS_ATTR_FATTR)) {
81754ceac45SDavid Howells 		error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
81854ceac45SDavid Howells 		if (error < 0) {
81954ceac45SDavid Howells 			dprintk("nfs_create_server: getattr error = %d\n", -error);
82054ceac45SDavid Howells 			goto error;
82154ceac45SDavid Howells 		}
82254ceac45SDavid Howells 	}
82354ceac45SDavid Howells 	memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
82454ceac45SDavid Howells 
8256daabf1bSDavid Howells 	dprintk("Server FSID: %llx:%llx\n",
8266daabf1bSDavid Howells 		(unsigned long long) server->fsid.major,
8276daabf1bSDavid Howells 		(unsigned long long) server->fsid.minor);
82854ceac45SDavid Howells 
82954ceac45SDavid Howells 	BUG_ON(!server->nfs_client);
83054ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops);
83154ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
83254ceac45SDavid Howells 
83354ceac45SDavid Howells 	spin_lock(&nfs_client_lock);
83454ceac45SDavid Howells 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
83554ceac45SDavid Howells 	list_add_tail(&server->master_link, &nfs_volume_list);
83654ceac45SDavid Howells 	spin_unlock(&nfs_client_lock);
83754ceac45SDavid Howells 
83854ceac45SDavid Howells 	server->mount_time = jiffies;
83954ceac45SDavid Howells 	return server;
84054ceac45SDavid Howells 
84154ceac45SDavid Howells error:
84254ceac45SDavid Howells 	nfs_free_server(server);
84354ceac45SDavid Howells 	return ERR_PTR(error);
84454ceac45SDavid Howells }
84554ceac45SDavid Howells 
84654ceac45SDavid Howells #ifdef CONFIG_NFS_V4
84754ceac45SDavid Howells /*
84854ceac45SDavid Howells  * Initialise an NFS4 client record
84954ceac45SDavid Howells  */
85054ceac45SDavid Howells static int nfs4_init_client(struct nfs_client *clp,
85154ceac45SDavid Howells 		int proto, int timeo, int retrans,
85254ceac45SDavid Howells 		rpc_authflavor_t authflavour)
85354ceac45SDavid Howells {
85454ceac45SDavid Howells 	int error;
85554ceac45SDavid Howells 
85654ceac45SDavid Howells 	if (clp->cl_cons_state == NFS_CS_READY) {
85754ceac45SDavid Howells 		/* the client is initialised already */
85854ceac45SDavid Howells 		dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
85954ceac45SDavid Howells 		return 0;
86054ceac45SDavid Howells 	}
86154ceac45SDavid Howells 
86254ceac45SDavid Howells 	/* Check NFS protocol revision and initialize RPC op vector */
86354ceac45SDavid Howells 	clp->rpc_ops = &nfs_v4_clientops;
86454ceac45SDavid Howells 
86554ceac45SDavid Howells 	error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour);
86654ceac45SDavid Howells 	if (error < 0)
86754ceac45SDavid Howells 		goto error;
86854ceac45SDavid Howells 
86954ceac45SDavid Howells 	error = nfs_idmap_new(clp);
87054ceac45SDavid Howells 	if (error < 0) {
87154ceac45SDavid Howells 		dprintk("%s: failed to create idmapper. Error = %d\n",
87254ceac45SDavid Howells 			__FUNCTION__, error);
87354ceac45SDavid Howells 		goto error;
87454ceac45SDavid Howells 	}
8759c5bf38dSTrond Myklebust 	__set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
87654ceac45SDavid Howells 
87754ceac45SDavid Howells 	nfs_mark_client_ready(clp, NFS_CS_READY);
87854ceac45SDavid Howells 	return 0;
87954ceac45SDavid Howells 
88054ceac45SDavid Howells error:
88154ceac45SDavid Howells 	nfs_mark_client_ready(clp, error);
88254ceac45SDavid Howells 	dprintk("<-- nfs4_init_client() = xerror %d\n", error);
88354ceac45SDavid Howells 	return error;
88454ceac45SDavid Howells }
88554ceac45SDavid Howells 
88654ceac45SDavid Howells /*
88754ceac45SDavid Howells  * Set up an NFS4 client
88854ceac45SDavid Howells  */
88954ceac45SDavid Howells static int nfs4_set_client(struct nfs_server *server,
89054ceac45SDavid Howells 		const char *hostname, const struct sockaddr_in *addr,
89154ceac45SDavid Howells 		rpc_authflavor_t authflavour,
89254ceac45SDavid Howells 		int proto, int timeo, int retrans)
89354ceac45SDavid Howells {
89454ceac45SDavid Howells 	struct nfs_client *clp;
89554ceac45SDavid Howells 	int error;
89654ceac45SDavid Howells 
89754ceac45SDavid Howells 	dprintk("--> nfs4_set_client()\n");
89854ceac45SDavid Howells 
89954ceac45SDavid Howells 	/* Allocate or find a client reference we can use */
90054ceac45SDavid Howells 	clp = nfs_get_client(hostname, addr, 4);
90154ceac45SDavid Howells 	if (IS_ERR(clp)) {
90254ceac45SDavid Howells 		error = PTR_ERR(clp);
90354ceac45SDavid Howells 		goto error;
90454ceac45SDavid Howells 	}
90554ceac45SDavid Howells 	error = nfs4_init_client(clp, proto, timeo, retrans, authflavour);
90654ceac45SDavid Howells 	if (error < 0)
90754ceac45SDavid Howells 		goto error_put;
90854ceac45SDavid Howells 
90954ceac45SDavid Howells 	server->nfs_client = clp;
91054ceac45SDavid Howells 	dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
91154ceac45SDavid Howells 	return 0;
91254ceac45SDavid Howells 
91354ceac45SDavid Howells error_put:
91454ceac45SDavid Howells 	nfs_put_client(clp);
91554ceac45SDavid Howells error:
91654ceac45SDavid Howells 	dprintk("<-- nfs4_set_client() = xerror %d\n", error);
91754ceac45SDavid Howells 	return error;
91854ceac45SDavid Howells }
91954ceac45SDavid Howells 
92054ceac45SDavid Howells /*
92154ceac45SDavid Howells  * Create a version 4 volume record
92254ceac45SDavid Howells  */
92354ceac45SDavid Howells static int nfs4_init_server(struct nfs_server *server,
92454ceac45SDavid Howells 		const struct nfs4_mount_data *data, rpc_authflavor_t authflavour)
92554ceac45SDavid Howells {
92654ceac45SDavid Howells 	int error;
92754ceac45SDavid Howells 
92854ceac45SDavid Howells 	dprintk("--> nfs4_init_server()\n");
92954ceac45SDavid Howells 
93054ceac45SDavid Howells 	/* Initialise the client representation from the mount data */
93154ceac45SDavid Howells 	server->flags = data->flags & NFS_MOUNT_FLAGMASK;
93254ceac45SDavid Howells 	server->caps |= NFS_CAP_ATOMIC_OPEN;
93354ceac45SDavid Howells 
93454ceac45SDavid Howells 	if (data->rsize)
93554ceac45SDavid Howells 		server->rsize = nfs_block_size(data->rsize, NULL);
93654ceac45SDavid Howells 	if (data->wsize)
93754ceac45SDavid Howells 		server->wsize = nfs_block_size(data->wsize, NULL);
93854ceac45SDavid Howells 
93954ceac45SDavid Howells 	server->acregmin = data->acregmin * HZ;
94054ceac45SDavid Howells 	server->acregmax = data->acregmax * HZ;
94154ceac45SDavid Howells 	server->acdirmin = data->acdirmin * HZ;
94254ceac45SDavid Howells 	server->acdirmax = data->acdirmax * HZ;
94354ceac45SDavid Howells 
94454ceac45SDavid Howells 	error = nfs_init_server_rpcclient(server, authflavour);
94554ceac45SDavid Howells 
94654ceac45SDavid Howells 	/* Done */
94754ceac45SDavid Howells 	dprintk("<-- nfs4_init_server() = %d\n", error);
94854ceac45SDavid Howells 	return error;
94954ceac45SDavid Howells }
95054ceac45SDavid Howells 
95154ceac45SDavid Howells /*
95254ceac45SDavid Howells  * Create a version 4 volume record
95354ceac45SDavid Howells  * - keyed on server and FSID
95454ceac45SDavid Howells  */
95554ceac45SDavid Howells struct nfs_server *nfs4_create_server(const struct nfs4_mount_data *data,
95654ceac45SDavid Howells 				      const char *hostname,
95754ceac45SDavid Howells 				      const struct sockaddr_in *addr,
95854ceac45SDavid Howells 				      const char *mntpath,
95954ceac45SDavid Howells 				      const char *ip_addr,
96054ceac45SDavid Howells 				      rpc_authflavor_t authflavour,
96154ceac45SDavid Howells 				      struct nfs_fh *mntfh)
96254ceac45SDavid Howells {
96354ceac45SDavid Howells 	struct nfs_fattr fattr;
96454ceac45SDavid Howells 	struct nfs_server *server;
96554ceac45SDavid Howells 	int error;
96654ceac45SDavid Howells 
96754ceac45SDavid Howells 	dprintk("--> nfs4_create_server()\n");
96854ceac45SDavid Howells 
96954ceac45SDavid Howells 	server = nfs_alloc_server();
97054ceac45SDavid Howells 	if (!server)
97154ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
97254ceac45SDavid Howells 
97354ceac45SDavid Howells 	/* Get a client record */
97454ceac45SDavid Howells 	error = nfs4_set_client(server, hostname, addr, authflavour,
97554ceac45SDavid Howells 			data->proto, data->timeo, data->retrans);
97654ceac45SDavid Howells 	if (error < 0)
97754ceac45SDavid Howells 		goto error;
97854ceac45SDavid Howells 
97954ceac45SDavid Howells 	/* set up the general RPC client */
98054ceac45SDavid Howells 	error = nfs4_init_server(server, data, authflavour);
98154ceac45SDavid Howells 	if (error < 0)
98254ceac45SDavid Howells 		goto error;
98354ceac45SDavid Howells 
98454ceac45SDavid Howells 	BUG_ON(!server->nfs_client);
98554ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops);
98654ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
98754ceac45SDavid Howells 
98854ceac45SDavid Howells 	/* Probe the root fh to retrieve its FSID */
98954ceac45SDavid Howells 	error = nfs4_path_walk(server, mntfh, mntpath);
99054ceac45SDavid Howells 	if (error < 0)
99154ceac45SDavid Howells 		goto error;
99254ceac45SDavid Howells 
9936daabf1bSDavid Howells 	dprintk("Server FSID: %llx:%llx\n",
9946daabf1bSDavid Howells 		(unsigned long long) server->fsid.major,
9956daabf1bSDavid Howells 		(unsigned long long) server->fsid.minor);
99654ceac45SDavid Howells 	dprintk("Mount FH: %d\n", mntfh->size);
99754ceac45SDavid Howells 
99854ceac45SDavid Howells 	error = nfs_probe_fsinfo(server, mntfh, &fattr);
99954ceac45SDavid Howells 	if (error < 0)
100054ceac45SDavid Howells 		goto error;
100154ceac45SDavid Howells 
100254ceac45SDavid Howells 	BUG_ON(!server->nfs_client);
100354ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops);
100454ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
100554ceac45SDavid Howells 
100654ceac45SDavid Howells 	spin_lock(&nfs_client_lock);
100754ceac45SDavid Howells 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
100854ceac45SDavid Howells 	list_add_tail(&server->master_link, &nfs_volume_list);
100954ceac45SDavid Howells 	spin_unlock(&nfs_client_lock);
101054ceac45SDavid Howells 
101154ceac45SDavid Howells 	server->mount_time = jiffies;
101254ceac45SDavid Howells 	dprintk("<-- nfs4_create_server() = %p\n", server);
101354ceac45SDavid Howells 	return server;
101454ceac45SDavid Howells 
101554ceac45SDavid Howells error:
101654ceac45SDavid Howells 	nfs_free_server(server);
101754ceac45SDavid Howells 	dprintk("<-- nfs4_create_server() = error %d\n", error);
101854ceac45SDavid Howells 	return ERR_PTR(error);
101954ceac45SDavid Howells }
102054ceac45SDavid Howells 
102154ceac45SDavid Howells /*
102254ceac45SDavid Howells  * Create an NFS4 referral server record
102354ceac45SDavid Howells  */
102454ceac45SDavid Howells struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
102554ceac45SDavid Howells 					       struct nfs_fh *fh)
102654ceac45SDavid Howells {
102754ceac45SDavid Howells 	struct nfs_client *parent_client;
102854ceac45SDavid Howells 	struct nfs_server *server, *parent_server;
102954ceac45SDavid Howells 	struct nfs_fattr fattr;
103054ceac45SDavid Howells 	int error;
103154ceac45SDavid Howells 
103254ceac45SDavid Howells 	dprintk("--> nfs4_create_referral_server()\n");
103354ceac45SDavid Howells 
103454ceac45SDavid Howells 	server = nfs_alloc_server();
103554ceac45SDavid Howells 	if (!server)
103654ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
103754ceac45SDavid Howells 
103854ceac45SDavid Howells 	parent_server = NFS_SB(data->sb);
103954ceac45SDavid Howells 	parent_client = parent_server->nfs_client;
104054ceac45SDavid Howells 
104154ceac45SDavid Howells 	/* Get a client representation.
104254ceac45SDavid Howells 	 * Note: NFSv4 always uses TCP, */
104354ceac45SDavid Howells 	error = nfs4_set_client(server, data->hostname, data->addr,
104454ceac45SDavid Howells 			data->authflavor,
104554ceac45SDavid Howells 			parent_server->client->cl_xprt->prot,
104654ceac45SDavid Howells 			parent_client->retrans_timeo,
104754ceac45SDavid Howells 			parent_client->retrans_count);
1048297de4f6Sandros@citi.umich.edu 	if (error < 0)
1049297de4f6Sandros@citi.umich.edu 		goto error;
105054ceac45SDavid Howells 
105154ceac45SDavid Howells 	/* Initialise the client representation from the parent server */
105254ceac45SDavid Howells 	nfs_server_copy_userdata(server, parent_server);
105354ceac45SDavid Howells 	server->caps |= NFS_CAP_ATOMIC_OPEN;
105454ceac45SDavid Howells 
105554ceac45SDavid Howells 	error = nfs_init_server_rpcclient(server, data->authflavor);
105654ceac45SDavid Howells 	if (error < 0)
105754ceac45SDavid Howells 		goto error;
105854ceac45SDavid Howells 
105954ceac45SDavid Howells 	BUG_ON(!server->nfs_client);
106054ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops);
106154ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
106254ceac45SDavid Howells 
106354ceac45SDavid Howells 	/* probe the filesystem info for this server filesystem */
106454ceac45SDavid Howells 	error = nfs_probe_fsinfo(server, fh, &fattr);
106554ceac45SDavid Howells 	if (error < 0)
106654ceac45SDavid Howells 		goto error;
106754ceac45SDavid Howells 
106854ceac45SDavid Howells 	dprintk("Referral FSID: %llx:%llx\n",
10696daabf1bSDavid Howells 		(unsigned long long) server->fsid.major,
10706daabf1bSDavid Howells 		(unsigned long long) server->fsid.minor);
107154ceac45SDavid Howells 
107254ceac45SDavid Howells 	spin_lock(&nfs_client_lock);
107354ceac45SDavid Howells 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
107454ceac45SDavid Howells 	list_add_tail(&server->master_link, &nfs_volume_list);
107554ceac45SDavid Howells 	spin_unlock(&nfs_client_lock);
107654ceac45SDavid Howells 
107754ceac45SDavid Howells 	server->mount_time = jiffies;
107854ceac45SDavid Howells 
107954ceac45SDavid Howells 	dprintk("<-- nfs_create_referral_server() = %p\n", server);
108054ceac45SDavid Howells 	return server;
108154ceac45SDavid Howells 
108254ceac45SDavid Howells error:
108354ceac45SDavid Howells 	nfs_free_server(server);
108454ceac45SDavid Howells 	dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
108554ceac45SDavid Howells 	return ERR_PTR(error);
108654ceac45SDavid Howells }
108754ceac45SDavid Howells 
108854ceac45SDavid Howells #endif /* CONFIG_NFS_V4 */
108954ceac45SDavid Howells 
109054ceac45SDavid Howells /*
109154ceac45SDavid Howells  * Clone an NFS2, NFS3 or NFS4 server record
109254ceac45SDavid Howells  */
109354ceac45SDavid Howells struct nfs_server *nfs_clone_server(struct nfs_server *source,
109454ceac45SDavid Howells 				    struct nfs_fh *fh,
109554ceac45SDavid Howells 				    struct nfs_fattr *fattr)
109654ceac45SDavid Howells {
109754ceac45SDavid Howells 	struct nfs_server *server;
109854ceac45SDavid Howells 	struct nfs_fattr fattr_fsinfo;
109954ceac45SDavid Howells 	int error;
110054ceac45SDavid Howells 
110154ceac45SDavid Howells 	dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
11026daabf1bSDavid Howells 		(unsigned long long) fattr->fsid.major,
11036daabf1bSDavid Howells 		(unsigned long long) fattr->fsid.minor);
110454ceac45SDavid Howells 
110554ceac45SDavid Howells 	server = nfs_alloc_server();
110654ceac45SDavid Howells 	if (!server)
110754ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
110854ceac45SDavid Howells 
110954ceac45SDavid Howells 	/* Copy data from the source */
111054ceac45SDavid Howells 	server->nfs_client = source->nfs_client;
111154ceac45SDavid Howells 	atomic_inc(&server->nfs_client->cl_count);
111254ceac45SDavid Howells 	nfs_server_copy_userdata(server, source);
111354ceac45SDavid Howells 
111454ceac45SDavid Howells 	server->fsid = fattr->fsid;
111554ceac45SDavid Howells 
111654ceac45SDavid Howells 	error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
111754ceac45SDavid Howells 	if (error < 0)
111854ceac45SDavid Howells 		goto out_free_server;
111954ceac45SDavid Howells 	if (!IS_ERR(source->client_acl))
112054ceac45SDavid Howells 		nfs_init_server_aclclient(server);
112154ceac45SDavid Howells 
112254ceac45SDavid Howells 	/* probe the filesystem info for this server filesystem */
112354ceac45SDavid Howells 	error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
112454ceac45SDavid Howells 	if (error < 0)
112554ceac45SDavid Howells 		goto out_free_server;
112654ceac45SDavid Howells 
112754ceac45SDavid Howells 	dprintk("Cloned FSID: %llx:%llx\n",
11286daabf1bSDavid Howells 		(unsigned long long) server->fsid.major,
11296daabf1bSDavid Howells 		(unsigned long long) server->fsid.minor);
113054ceac45SDavid Howells 
113154ceac45SDavid Howells 	error = nfs_start_lockd(server);
113254ceac45SDavid Howells 	if (error < 0)
113354ceac45SDavid Howells 		goto out_free_server;
113454ceac45SDavid Howells 
113554ceac45SDavid Howells 	spin_lock(&nfs_client_lock);
113654ceac45SDavid Howells 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
113754ceac45SDavid Howells 	list_add_tail(&server->master_link, &nfs_volume_list);
113854ceac45SDavid Howells 	spin_unlock(&nfs_client_lock);
113954ceac45SDavid Howells 
114054ceac45SDavid Howells 	server->mount_time = jiffies;
114154ceac45SDavid Howells 
114254ceac45SDavid Howells 	dprintk("<-- nfs_clone_server() = %p\n", server);
114354ceac45SDavid Howells 	return server;
114454ceac45SDavid Howells 
114554ceac45SDavid Howells out_free_server:
114654ceac45SDavid Howells 	nfs_free_server(server);
114754ceac45SDavid Howells 	dprintk("<-- nfs_clone_server() = error %d\n", error);
114854ceac45SDavid Howells 	return ERR_PTR(error);
114954ceac45SDavid Howells }
11506aaca566SDavid Howells 
11516aaca566SDavid Howells #ifdef CONFIG_PROC_FS
11526aaca566SDavid Howells static struct proc_dir_entry *proc_fs_nfs;
11536aaca566SDavid Howells 
11546aaca566SDavid Howells static int nfs_server_list_open(struct inode *inode, struct file *file);
11556aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
11566aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
11576aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v);
11586aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v);
11596aaca566SDavid Howells 
11606aaca566SDavid Howells static struct seq_operations nfs_server_list_ops = {
11616aaca566SDavid Howells 	.start	= nfs_server_list_start,
11626aaca566SDavid Howells 	.next	= nfs_server_list_next,
11636aaca566SDavid Howells 	.stop	= nfs_server_list_stop,
11646aaca566SDavid Howells 	.show	= nfs_server_list_show,
11656aaca566SDavid Howells };
11666aaca566SDavid Howells 
11676aaca566SDavid Howells static struct file_operations nfs_server_list_fops = {
11686aaca566SDavid Howells 	.open		= nfs_server_list_open,
11696aaca566SDavid Howells 	.read		= seq_read,
11706aaca566SDavid Howells 	.llseek		= seq_lseek,
11716aaca566SDavid Howells 	.release	= seq_release,
11726aaca566SDavid Howells };
11736aaca566SDavid Howells 
11746aaca566SDavid Howells static int nfs_volume_list_open(struct inode *inode, struct file *file);
11756aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
11766aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
11776aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v);
11786aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v);
11796aaca566SDavid Howells 
11806aaca566SDavid Howells static struct seq_operations nfs_volume_list_ops = {
11816aaca566SDavid Howells 	.start	= nfs_volume_list_start,
11826aaca566SDavid Howells 	.next	= nfs_volume_list_next,
11836aaca566SDavid Howells 	.stop	= nfs_volume_list_stop,
11846aaca566SDavid Howells 	.show	= nfs_volume_list_show,
11856aaca566SDavid Howells };
11866aaca566SDavid Howells 
11876aaca566SDavid Howells static struct file_operations nfs_volume_list_fops = {
11886aaca566SDavid Howells 	.open		= nfs_volume_list_open,
11896aaca566SDavid Howells 	.read		= seq_read,
11906aaca566SDavid Howells 	.llseek		= seq_lseek,
11916aaca566SDavid Howells 	.release	= seq_release,
11926aaca566SDavid Howells };
11936aaca566SDavid Howells 
11946aaca566SDavid Howells /*
11956aaca566SDavid Howells  * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
11966aaca566SDavid Howells  * we're dealing
11976aaca566SDavid Howells  */
11986aaca566SDavid Howells static int nfs_server_list_open(struct inode *inode, struct file *file)
11996aaca566SDavid Howells {
12006aaca566SDavid Howells 	struct seq_file *m;
12016aaca566SDavid Howells 	int ret;
12026aaca566SDavid Howells 
12036aaca566SDavid Howells 	ret = seq_open(file, &nfs_server_list_ops);
12046aaca566SDavid Howells 	if (ret < 0)
12056aaca566SDavid Howells 		return ret;
12066aaca566SDavid Howells 
12076aaca566SDavid Howells 	m = file->private_data;
12086aaca566SDavid Howells 	m->private = PDE(inode)->data;
12096aaca566SDavid Howells 
12106aaca566SDavid Howells 	return 0;
12116aaca566SDavid Howells }
12126aaca566SDavid Howells 
12136aaca566SDavid Howells /*
12146aaca566SDavid Howells  * set up the iterator to start reading from the server list and return the first item
12156aaca566SDavid Howells  */
12166aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
12176aaca566SDavid Howells {
12186aaca566SDavid Howells 	struct list_head *_p;
12196aaca566SDavid Howells 	loff_t pos = *_pos;
12206aaca566SDavid Howells 
12216aaca566SDavid Howells 	/* lock the list against modification */
12226aaca566SDavid Howells 	spin_lock(&nfs_client_lock);
12236aaca566SDavid Howells 
12246aaca566SDavid Howells 	/* allow for the header line */
12256aaca566SDavid Howells 	if (!pos)
12266aaca566SDavid Howells 		return SEQ_START_TOKEN;
12276aaca566SDavid Howells 	pos--;
12286aaca566SDavid Howells 
12296aaca566SDavid Howells 	/* find the n'th element in the list */
12306aaca566SDavid Howells 	list_for_each(_p, &nfs_client_list)
12316aaca566SDavid Howells 		if (!pos--)
12326aaca566SDavid Howells 			break;
12336aaca566SDavid Howells 
12346aaca566SDavid Howells 	return _p != &nfs_client_list ? _p : NULL;
12356aaca566SDavid Howells }
12366aaca566SDavid Howells 
12376aaca566SDavid Howells /*
12386aaca566SDavid Howells  * move to next server
12396aaca566SDavid Howells  */
12406aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
12416aaca566SDavid Howells {
12426aaca566SDavid Howells 	struct list_head *_p;
12436aaca566SDavid Howells 
12446aaca566SDavid Howells 	(*pos)++;
12456aaca566SDavid Howells 
12466aaca566SDavid Howells 	_p = v;
12476aaca566SDavid Howells 	_p = (v == SEQ_START_TOKEN) ? nfs_client_list.next : _p->next;
12486aaca566SDavid Howells 
12496aaca566SDavid Howells 	return _p != &nfs_client_list ? _p : NULL;
12506aaca566SDavid Howells }
12516aaca566SDavid Howells 
12526aaca566SDavid Howells /*
12536aaca566SDavid Howells  * clean up after reading from the transports list
12546aaca566SDavid Howells  */
12556aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v)
12566aaca566SDavid Howells {
12576aaca566SDavid Howells 	spin_unlock(&nfs_client_lock);
12586aaca566SDavid Howells }
12596aaca566SDavid Howells 
12606aaca566SDavid Howells /*
12616aaca566SDavid Howells  * display a header line followed by a load of call lines
12626aaca566SDavid Howells  */
12636aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v)
12646aaca566SDavid Howells {
12656aaca566SDavid Howells 	struct nfs_client *clp;
12666aaca566SDavid Howells 
12676aaca566SDavid Howells 	/* display header on line 1 */
12686aaca566SDavid Howells 	if (v == SEQ_START_TOKEN) {
12696aaca566SDavid Howells 		seq_puts(m, "NV SERVER   PORT USE HOSTNAME\n");
12706aaca566SDavid Howells 		return 0;
12716aaca566SDavid Howells 	}
12726aaca566SDavid Howells 
12736aaca566SDavid Howells 	/* display one transport per line on subsequent lines */
12746aaca566SDavid Howells 	clp = list_entry(v, struct nfs_client, cl_share_link);
12756aaca566SDavid Howells 
12766aaca566SDavid Howells 	seq_printf(m, "v%d %02x%02x%02x%02x %4hx %3d %s\n",
12776aaca566SDavid Howells 		   clp->cl_nfsversion,
12786aaca566SDavid Howells 		   NIPQUAD(clp->cl_addr.sin_addr),
12796aaca566SDavid Howells 		   ntohs(clp->cl_addr.sin_port),
12806aaca566SDavid Howells 		   atomic_read(&clp->cl_count),
12816aaca566SDavid Howells 		   clp->cl_hostname);
12826aaca566SDavid Howells 
12836aaca566SDavid Howells 	return 0;
12846aaca566SDavid Howells }
12856aaca566SDavid Howells 
12866aaca566SDavid Howells /*
12876aaca566SDavid Howells  * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
12886aaca566SDavid Howells  */
12896aaca566SDavid Howells static int nfs_volume_list_open(struct inode *inode, struct file *file)
12906aaca566SDavid Howells {
12916aaca566SDavid Howells 	struct seq_file *m;
12926aaca566SDavid Howells 	int ret;
12936aaca566SDavid Howells 
12946aaca566SDavid Howells 	ret = seq_open(file, &nfs_volume_list_ops);
12956aaca566SDavid Howells 	if (ret < 0)
12966aaca566SDavid Howells 		return ret;
12976aaca566SDavid Howells 
12986aaca566SDavid Howells 	m = file->private_data;
12996aaca566SDavid Howells 	m->private = PDE(inode)->data;
13006aaca566SDavid Howells 
13016aaca566SDavid Howells 	return 0;
13026aaca566SDavid Howells }
13036aaca566SDavid Howells 
13046aaca566SDavid Howells /*
13056aaca566SDavid Howells  * set up the iterator to start reading from the volume list and return the first item
13066aaca566SDavid Howells  */
13076aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
13086aaca566SDavid Howells {
13096aaca566SDavid Howells 	struct list_head *_p;
13106aaca566SDavid Howells 	loff_t pos = *_pos;
13116aaca566SDavid Howells 
13126aaca566SDavid Howells 	/* lock the list against modification */
13136aaca566SDavid Howells 	spin_lock(&nfs_client_lock);
13146aaca566SDavid Howells 
13156aaca566SDavid Howells 	/* allow for the header line */
13166aaca566SDavid Howells 	if (!pos)
13176aaca566SDavid Howells 		return SEQ_START_TOKEN;
13186aaca566SDavid Howells 	pos--;
13196aaca566SDavid Howells 
13206aaca566SDavid Howells 	/* find the n'th element in the list */
13216aaca566SDavid Howells 	list_for_each(_p, &nfs_volume_list)
13226aaca566SDavid Howells 		if (!pos--)
13236aaca566SDavid Howells 			break;
13246aaca566SDavid Howells 
13256aaca566SDavid Howells 	return _p != &nfs_volume_list ? _p : NULL;
13266aaca566SDavid Howells }
13276aaca566SDavid Howells 
13286aaca566SDavid Howells /*
13296aaca566SDavid Howells  * move to next volume
13306aaca566SDavid Howells  */
13316aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
13326aaca566SDavid Howells {
13336aaca566SDavid Howells 	struct list_head *_p;
13346aaca566SDavid Howells 
13356aaca566SDavid Howells 	(*pos)++;
13366aaca566SDavid Howells 
13376aaca566SDavid Howells 	_p = v;
13386aaca566SDavid Howells 	_p = (v == SEQ_START_TOKEN) ? nfs_volume_list.next : _p->next;
13396aaca566SDavid Howells 
13406aaca566SDavid Howells 	return _p != &nfs_volume_list ? _p : NULL;
13416aaca566SDavid Howells }
13426aaca566SDavid Howells 
13436aaca566SDavid Howells /*
13446aaca566SDavid Howells  * clean up after reading from the transports list
13456aaca566SDavid Howells  */
13466aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v)
13476aaca566SDavid Howells {
13486aaca566SDavid Howells 	spin_unlock(&nfs_client_lock);
13496aaca566SDavid Howells }
13506aaca566SDavid Howells 
13516aaca566SDavid Howells /*
13526aaca566SDavid Howells  * display a header line followed by a load of call lines
13536aaca566SDavid Howells  */
13546aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v)
13556aaca566SDavid Howells {
13566aaca566SDavid Howells 	struct nfs_server *server;
13576aaca566SDavid Howells 	struct nfs_client *clp;
13586aaca566SDavid Howells 	char dev[8], fsid[17];
13596aaca566SDavid Howells 
13606aaca566SDavid Howells 	/* display header on line 1 */
13616aaca566SDavid Howells 	if (v == SEQ_START_TOKEN) {
13626aaca566SDavid Howells 		seq_puts(m, "NV SERVER   PORT DEV     FSID\n");
13636aaca566SDavid Howells 		return 0;
13646aaca566SDavid Howells 	}
13656aaca566SDavid Howells 	/* display one transport per line on subsequent lines */
13666aaca566SDavid Howells 	server = list_entry(v, struct nfs_server, master_link);
13676aaca566SDavid Howells 	clp = server->nfs_client;
13686aaca566SDavid Howells 
13696aaca566SDavid Howells 	snprintf(dev, 8, "%u:%u",
13706aaca566SDavid Howells 		 MAJOR(server->s_dev), MINOR(server->s_dev));
13716aaca566SDavid Howells 
13726aaca566SDavid Howells 	snprintf(fsid, 17, "%llx:%llx",
13736daabf1bSDavid Howells 		 (unsigned long long) server->fsid.major,
13746daabf1bSDavid Howells 		 (unsigned long long) server->fsid.minor);
13756aaca566SDavid Howells 
13766aaca566SDavid Howells 	seq_printf(m, "v%d %02x%02x%02x%02x %4hx %-7s %-17s\n",
13776aaca566SDavid Howells 		   clp->cl_nfsversion,
13786aaca566SDavid Howells 		   NIPQUAD(clp->cl_addr.sin_addr),
13796aaca566SDavid Howells 		   ntohs(clp->cl_addr.sin_port),
13806aaca566SDavid Howells 		   dev,
13816aaca566SDavid Howells 		   fsid);
13826aaca566SDavid Howells 
13836aaca566SDavid Howells 	return 0;
13846aaca566SDavid Howells }
13856aaca566SDavid Howells 
13866aaca566SDavid Howells /*
13876aaca566SDavid Howells  * initialise the /proc/fs/nfsfs/ directory
13886aaca566SDavid Howells  */
13896aaca566SDavid Howells int __init nfs_fs_proc_init(void)
13906aaca566SDavid Howells {
13916aaca566SDavid Howells 	struct proc_dir_entry *p;
13926aaca566SDavid Howells 
13936aaca566SDavid Howells 	proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
13946aaca566SDavid Howells 	if (!proc_fs_nfs)
13956aaca566SDavid Howells 		goto error_0;
13966aaca566SDavid Howells 
13976aaca566SDavid Howells 	proc_fs_nfs->owner = THIS_MODULE;
13986aaca566SDavid Howells 
13996aaca566SDavid Howells 	/* a file of servers with which we're dealing */
14006aaca566SDavid Howells 	p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
14016aaca566SDavid Howells 	if (!p)
14026aaca566SDavid Howells 		goto error_1;
14036aaca566SDavid Howells 
14046aaca566SDavid Howells 	p->proc_fops = &nfs_server_list_fops;
14056aaca566SDavid Howells 	p->owner = THIS_MODULE;
14066aaca566SDavid Howells 
14076aaca566SDavid Howells 	/* a file of volumes that we have mounted */
14086aaca566SDavid Howells 	p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
14096aaca566SDavid Howells 	if (!p)
14106aaca566SDavid Howells 		goto error_2;
14116aaca566SDavid Howells 
14126aaca566SDavid Howells 	p->proc_fops = &nfs_volume_list_fops;
14136aaca566SDavid Howells 	p->owner = THIS_MODULE;
14146aaca566SDavid Howells 	return 0;
14156aaca566SDavid Howells 
14166aaca566SDavid Howells error_2:
14176aaca566SDavid Howells 	remove_proc_entry("servers", proc_fs_nfs);
14186aaca566SDavid Howells error_1:
14196aaca566SDavid Howells 	remove_proc_entry("nfsfs", proc_root_fs);
14206aaca566SDavid Howells error_0:
14216aaca566SDavid Howells 	return -ENOMEM;
14226aaca566SDavid Howells }
14236aaca566SDavid Howells 
14246aaca566SDavid Howells /*
14256aaca566SDavid Howells  * clean up the /proc/fs/nfsfs/ directory
14266aaca566SDavid Howells  */
14276aaca566SDavid Howells void nfs_fs_proc_exit(void)
14286aaca566SDavid Howells {
14296aaca566SDavid Howells 	remove_proc_entry("volumes", proc_fs_nfs);
14306aaca566SDavid Howells 	remove_proc_entry("servers", proc_fs_nfs);
14316aaca566SDavid Howells 	remove_proc_entry("nfsfs", proc_root_fs);
14326aaca566SDavid Howells }
14336aaca566SDavid Howells 
14346aaca566SDavid Howells #endif /* CONFIG_PROC_FS */
1435