xref: /linux/fs/nfs/client.c (revision d993831fa7ffeb89e994f046f93eeb09ec91df08)
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>
15e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
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>
260896a725S\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h>
272cf7ff7aS\"Talpey, Thomas\ #include <linux/sunrpc/xprtrdma.h>
2824c8dbbbSDavid Howells #include <linux/nfs_fs.h>
2924c8dbbbSDavid Howells #include <linux/nfs_mount.h>
3024c8dbbbSDavid Howells #include <linux/nfs4_mount.h>
3124c8dbbbSDavid Howells #include <linux/lockd/bind.h>
3224c8dbbbSDavid Howells #include <linux/seq_file.h>
3324c8dbbbSDavid Howells #include <linux/mount.h>
3424c8dbbbSDavid Howells #include <linux/nfs_idmap.h>
3524c8dbbbSDavid Howells #include <linux/vfs.h>
3624c8dbbbSDavid Howells #include <linux/inet.h>
373b0d3f93STrond Myklebust #include <linux/in6.h>
383b0d3f93STrond Myklebust #include <net/ipv6.h>
3924c8dbbbSDavid Howells #include <linux/nfs_xdr.h>
400b5b7ae0SAndy Adamson #include <linux/sunrpc/bc_xprt.h>
4124c8dbbbSDavid Howells 
4224c8dbbbSDavid Howells #include <asm/system.h>
4324c8dbbbSDavid Howells 
4424c8dbbbSDavid Howells #include "nfs4_fs.h"
4524c8dbbbSDavid Howells #include "callback.h"
4624c8dbbbSDavid Howells #include "delegation.h"
4724c8dbbbSDavid Howells #include "iostat.h"
4824c8dbbbSDavid Howells #include "internal.h"
4914727281SDavid Howells #include "fscache.h"
5024c8dbbbSDavid Howells 
5124c8dbbbSDavid Howells #define NFSDBG_FACILITY		NFSDBG_CLIENT
5224c8dbbbSDavid Howells 
5324c8dbbbSDavid Howells static DEFINE_SPINLOCK(nfs_client_lock);
5424c8dbbbSDavid Howells static LIST_HEAD(nfs_client_list);
5554ceac45SDavid Howells static LIST_HEAD(nfs_volume_list);
5624c8dbbbSDavid Howells static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
5724c8dbbbSDavid Howells 
5824c8dbbbSDavid Howells /*
595006a76cSDavid Howells  * RPC cruft for NFS
605006a76cSDavid Howells  */
615006a76cSDavid Howells static struct rpc_version *nfs_version[5] = {
625006a76cSDavid Howells 	[2]			= &nfs_version2,
635006a76cSDavid Howells #ifdef CONFIG_NFS_V3
645006a76cSDavid Howells 	[3]			= &nfs_version3,
655006a76cSDavid Howells #endif
665006a76cSDavid Howells #ifdef CONFIG_NFS_V4
675006a76cSDavid Howells 	[4]			= &nfs_version4,
685006a76cSDavid Howells #endif
695006a76cSDavid Howells };
705006a76cSDavid Howells 
715006a76cSDavid Howells struct rpc_program nfs_program = {
725006a76cSDavid Howells 	.name			= "nfs",
735006a76cSDavid Howells 	.number			= NFS_PROGRAM,
745006a76cSDavid Howells 	.nrvers			= ARRAY_SIZE(nfs_version),
755006a76cSDavid Howells 	.version		= nfs_version,
765006a76cSDavid Howells 	.stats			= &nfs_rpcstat,
775006a76cSDavid Howells 	.pipe_dir_name		= "/nfs",
785006a76cSDavid Howells };
795006a76cSDavid Howells 
805006a76cSDavid Howells struct rpc_stat nfs_rpcstat = {
815006a76cSDavid Howells 	.program		= &nfs_program
825006a76cSDavid Howells };
835006a76cSDavid Howells 
845006a76cSDavid Howells 
855006a76cSDavid Howells #ifdef CONFIG_NFS_V3_ACL
865006a76cSDavid Howells static struct rpc_stat		nfsacl_rpcstat = { &nfsacl_program };
875006a76cSDavid Howells static struct rpc_version *	nfsacl_version[] = {
885006a76cSDavid Howells 	[3]			= &nfsacl_version3,
895006a76cSDavid Howells };
905006a76cSDavid Howells 
915006a76cSDavid Howells struct rpc_program		nfsacl_program = {
925006a76cSDavid Howells 	.name			= "nfsacl",
935006a76cSDavid Howells 	.number			= NFS_ACL_PROGRAM,
945006a76cSDavid Howells 	.nrvers			= ARRAY_SIZE(nfsacl_version),
955006a76cSDavid Howells 	.version		= nfsacl_version,
965006a76cSDavid Howells 	.stats			= &nfsacl_rpcstat,
975006a76cSDavid Howells };
985006a76cSDavid Howells #endif  /* CONFIG_NFS_V3_ACL */
995006a76cSDavid Howells 
1003a498026STrond Myklebust struct nfs_client_initdata {
1013a498026STrond Myklebust 	const char *hostname;
102d7422c47SChuck Lever 	const struct sockaddr *addr;
1036e4cffd7SChuck Lever 	size_t addrlen;
10440c55319STrond Myklebust 	const struct nfs_rpc_ops *rpc_ops;
10559dca3b2STrond Myklebust 	int proto;
1065aae4a9aSBenny Halevy 	u32 minorversion;
1073a498026STrond Myklebust };
1083a498026STrond Myklebust 
1095006a76cSDavid Howells /*
11024c8dbbbSDavid Howells  * Allocate a shared client record
11124c8dbbbSDavid Howells  *
11224c8dbbbSDavid Howells  * Since these are allocated/deallocated very rarely, we don't
11324c8dbbbSDavid Howells  * bother putting them in a slab cache...
11424c8dbbbSDavid Howells  */
1153a498026STrond Myklebust static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
11624c8dbbbSDavid Howells {
11724c8dbbbSDavid Howells 	struct nfs_client *clp;
1187c67db3aSTrond Myklebust 	struct rpc_cred *cred;
119a21bdd9bSChuck Lever 	int err = -ENOMEM;
12024c8dbbbSDavid Howells 
12124c8dbbbSDavid Howells 	if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
12224c8dbbbSDavid Howells 		goto error_0;
12324c8dbbbSDavid Howells 
12440c55319STrond Myklebust 	clp->rpc_ops = cl_init->rpc_ops;
12540c55319STrond Myklebust 
12624c8dbbbSDavid Howells 	atomic_set(&clp->cl_count, 1);
12724c8dbbbSDavid Howells 	clp->cl_cons_state = NFS_CS_INITING;
12824c8dbbbSDavid Howells 
1296e4cffd7SChuck Lever 	memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
1306e4cffd7SChuck Lever 	clp->cl_addrlen = cl_init->addrlen;
13124c8dbbbSDavid Howells 
1323a498026STrond Myklebust 	if (cl_init->hostname) {
133a21bdd9bSChuck Lever 		err = -ENOMEM;
1343a498026STrond Myklebust 		clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
13524c8dbbbSDavid Howells 		if (!clp->cl_hostname)
13671468513SBenny Halevy 			goto error_cleanup;
13724c8dbbbSDavid Howells 	}
13824c8dbbbSDavid Howells 
13924c8dbbbSDavid Howells 	INIT_LIST_HEAD(&clp->cl_superblocks);
14024c8dbbbSDavid Howells 	clp->cl_rpcclient = ERR_PTR(-EINVAL);
14124c8dbbbSDavid Howells 
14259dca3b2STrond Myklebust 	clp->cl_proto = cl_init->proto;
14359dca3b2STrond Myklebust 
14424c8dbbbSDavid Howells #ifdef CONFIG_NFS_V4
14524c8dbbbSDavid Howells 	INIT_LIST_HEAD(&clp->cl_delegations);
14624c8dbbbSDavid Howells 	spin_lock_init(&clp->cl_lock);
14765f27f38SDavid Howells 	INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
14824c8dbbbSDavid Howells 	rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
14924c8dbbbSDavid Howells 	clp->cl_boot_time = CURRENT_TIME;
15024c8dbbbSDavid Howells 	clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
1515aae4a9aSBenny Halevy 	clp->cl_minorversion = cl_init->minorversion;
15224c8dbbbSDavid Howells #endif
1537c67db3aSTrond Myklebust 	cred = rpc_lookup_machine_cred();
1547c67db3aSTrond Myklebust 	if (!IS_ERR(cred))
1557c67db3aSTrond Myklebust 		clp->cl_machine_cred = cred;
15624c8dbbbSDavid Howells 
15714727281SDavid Howells 	nfs_fscache_get_client_cookie(clp);
15814727281SDavid Howells 
15924c8dbbbSDavid Howells 	return clp;
16024c8dbbbSDavid Howells 
16171468513SBenny Halevy error_cleanup:
16224c8dbbbSDavid Howells 	kfree(clp);
16324c8dbbbSDavid Howells error_0:
164a21bdd9bSChuck Lever 	return ERR_PTR(err);
16524c8dbbbSDavid Howells }
16624c8dbbbSDavid Howells 
1675dd3177aSTrond Myklebust static void nfs4_shutdown_client(struct nfs_client *clp)
16824c8dbbbSDavid Howells {
16924c8dbbbSDavid Howells #ifdef CONFIG_NFS_V4
1705dd3177aSTrond Myklebust 	if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
1715dd3177aSTrond Myklebust 		nfs4_kill_renewd(clp);
1729f958ab8STrond Myklebust 	BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
1735dd3177aSTrond Myklebust 	if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
17424c8dbbbSDavid Howells 		nfs_idmap_delete(clp);
175f6a1cc89STrond Myklebust 
176f6a1cc89STrond Myklebust 	rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
17724c8dbbbSDavid Howells #endif
1785dd3177aSTrond Myklebust }
1795dd3177aSTrond Myklebust 
1805dd3177aSTrond Myklebust /*
1819bdaa86dSBenny Halevy  * Destroy the NFS4 callback service
1829bdaa86dSBenny Halevy  */
1839bdaa86dSBenny Halevy static void nfs4_destroy_callback(struct nfs_client *clp)
1849bdaa86dSBenny Halevy {
1859bdaa86dSBenny Halevy #ifdef CONFIG_NFS_V4
1869bdaa86dSBenny Halevy 	if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
187e82dc22dSAndy Adamson 		nfs_callback_down(clp->cl_minorversion);
1889bdaa86dSBenny Halevy #endif /* CONFIG_NFS_V4 */
1899bdaa86dSBenny Halevy }
1909bdaa86dSBenny Halevy 
1919bdaa86dSBenny Halevy /*
192557134a3SAndy Adamson  * Clears/puts all minor version specific parts from an nfs_client struct
193557134a3SAndy Adamson  * reverting it to minorversion 0.
194557134a3SAndy Adamson  */
195557134a3SAndy Adamson static void nfs4_clear_client_minor_version(struct nfs_client *clp)
196557134a3SAndy Adamson {
197557134a3SAndy Adamson #ifdef CONFIG_NFS_V4_1
198557134a3SAndy Adamson 	if (nfs4_has_session(clp)) {
199557134a3SAndy Adamson 		nfs4_destroy_session(clp->cl_session);
200557134a3SAndy Adamson 		clp->cl_session = NULL;
201557134a3SAndy Adamson 	}
202cccef3b9SAndy Adamson 
203cccef3b9SAndy Adamson 	clp->cl_call_sync = _nfs4_call_sync;
204557134a3SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
20571468513SBenny Halevy 
20671468513SBenny Halevy 	nfs4_destroy_callback(clp);
207557134a3SAndy Adamson }
208557134a3SAndy Adamson 
209557134a3SAndy Adamson /*
2105dd3177aSTrond Myklebust  * Destroy a shared client record
2115dd3177aSTrond Myklebust  */
2125dd3177aSTrond Myklebust static void nfs_free_client(struct nfs_client *clp)
2135dd3177aSTrond Myklebust {
21440c55319STrond Myklebust 	dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
2155dd3177aSTrond Myklebust 
216557134a3SAndy Adamson 	nfs4_clear_client_minor_version(clp);
2175dd3177aSTrond Myklebust 	nfs4_shutdown_client(clp);
21824c8dbbbSDavid Howells 
21914727281SDavid Howells 	nfs_fscache_release_client_cookie(clp);
22014727281SDavid Howells 
22124c8dbbbSDavid Howells 	/* -EIO all pending I/O */
22224c8dbbbSDavid Howells 	if (!IS_ERR(clp->cl_rpcclient))
22324c8dbbbSDavid Howells 		rpc_shutdown_client(clp->cl_rpcclient);
22424c8dbbbSDavid Howells 
2257c67db3aSTrond Myklebust 	if (clp->cl_machine_cred != NULL)
2267c67db3aSTrond Myklebust 		put_rpccred(clp->cl_machine_cred);
2277c67db3aSTrond Myklebust 
22824c8dbbbSDavid Howells 	kfree(clp->cl_hostname);
22924c8dbbbSDavid Howells 	kfree(clp);
23024c8dbbbSDavid Howells 
23124c8dbbbSDavid Howells 	dprintk("<-- nfs_free_client()\n");
23224c8dbbbSDavid Howells }
23324c8dbbbSDavid Howells 
23424c8dbbbSDavid Howells /*
23524c8dbbbSDavid Howells  * Release a reference to a shared client record
23624c8dbbbSDavid Howells  */
23724c8dbbbSDavid Howells void nfs_put_client(struct nfs_client *clp)
23824c8dbbbSDavid Howells {
23927ba8512SDavid Howells 	if (!clp)
24027ba8512SDavid Howells 		return;
24127ba8512SDavid Howells 
24224c8dbbbSDavid Howells 	dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
24324c8dbbbSDavid Howells 
24424c8dbbbSDavid Howells 	if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
24524c8dbbbSDavid Howells 		list_del(&clp->cl_share_link);
24624c8dbbbSDavid Howells 		spin_unlock(&nfs_client_lock);
24724c8dbbbSDavid Howells 
24824c8dbbbSDavid Howells 		BUG_ON(!list_empty(&clp->cl_superblocks));
24924c8dbbbSDavid Howells 
25024c8dbbbSDavid Howells 		nfs_free_client(clp);
25124c8dbbbSDavid Howells 	}
25224c8dbbbSDavid Howells }
25324c8dbbbSDavid Howells 
2549082a5ccSTrond Myklebust #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
2559f4c899cSTrond Myklebust /*
2569f4c899cSTrond Myklebust  * Test if two ip6 socket addresses refer to the same socket by
2579f4c899cSTrond Myklebust  * comparing relevant fields. The padding bytes specifically, are not
2589f4c899cSTrond Myklebust  * compared. sin6_flowinfo is not compared because it only affects QoS
2599f4c899cSTrond Myklebust  * and sin6_scope_id is only compared if the address is "link local"
2609f4c899cSTrond Myklebust  * because "link local" addresses need only be unique to a specific
2619f4c899cSTrond Myklebust  * link. Conversely, ordinary unicast addresses might have different
2629f4c899cSTrond Myklebust  * sin6_scope_id.
2639f4c899cSTrond Myklebust  *
2649f4c899cSTrond Myklebust  * The caller should ensure both socket addresses are AF_INET6.
2659f4c899cSTrond Myklebust  */
2663c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
2679f4c899cSTrond Myklebust 				      const struct sockaddr *sa2)
2689f4c899cSTrond Myklebust {
2693c8c45dfSChuck Lever 	const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
2703c8c45dfSChuck Lever 	const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
2719f4c899cSTrond Myklebust 
2723c8c45dfSChuck Lever 	if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
2733c8c45dfSChuck Lever 	    sin1->sin6_scope_id != sin2->sin6_scope_id)
2749f4c899cSTrond Myklebust 		return 0;
2753b0d3f93STrond Myklebust 
2763c8c45dfSChuck Lever 	return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
2773b0d3f93STrond Myklebust }
2783c8c45dfSChuck Lever #else	/* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
2793c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
2809f4c899cSTrond Myklebust 				      const struct sockaddr *sa2)
2819f4c899cSTrond Myklebust {
2829f4c899cSTrond Myklebust 	return 0;
2839f4c899cSTrond Myklebust }
2849082a5ccSTrond Myklebust #endif
2853b0d3f93STrond Myklebust 
28624c8dbbbSDavid Howells /*
287d7371c41SIan Dall  * Test if two ip4 socket addresses refer to the same socket, by
288d7371c41SIan Dall  * comparing relevant fields. The padding bytes specifically, are
289d7371c41SIan Dall  * not compared.
290d7371c41SIan Dall  *
291d7371c41SIan Dall  * The caller should ensure both socket addresses are AF_INET.
292d7371c41SIan Dall  */
2933c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
2943c8c45dfSChuck Lever 				      const struct sockaddr *sa2)
2953c8c45dfSChuck Lever {
2963c8c45dfSChuck Lever 	const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
2973c8c45dfSChuck Lever 	const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
2983c8c45dfSChuck Lever 
2993c8c45dfSChuck Lever 	return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
3003c8c45dfSChuck Lever }
3013c8c45dfSChuck Lever 
3023c8c45dfSChuck Lever static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
3033c8c45dfSChuck Lever 				const struct sockaddr *sa2)
3043c8c45dfSChuck Lever {
3053c8c45dfSChuck Lever 	const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
3063c8c45dfSChuck Lever 	const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
3073c8c45dfSChuck Lever 
3083c8c45dfSChuck Lever 	return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
3093c8c45dfSChuck Lever 		(sin1->sin6_port == sin2->sin6_port);
3103c8c45dfSChuck Lever }
3113c8c45dfSChuck Lever 
3129f4c899cSTrond Myklebust static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
3139f4c899cSTrond Myklebust 				const struct sockaddr *sa2)
314d7371c41SIan Dall {
3153c8c45dfSChuck Lever 	const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
3163c8c45dfSChuck Lever 	const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
3179f4c899cSTrond Myklebust 
3183c8c45dfSChuck Lever 	return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
3193c8c45dfSChuck Lever 		(sin1->sin_port == sin2->sin_port);
320d7371c41SIan Dall }
321d7371c41SIan Dall 
322d7371c41SIan Dall /*
323d7371c41SIan Dall  * Test if two socket addresses represent the same actual socket,
3243c8c45dfSChuck Lever  * by comparing (only) relevant fields, excluding the port number.
3253c8c45dfSChuck Lever  */
3263c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
3273c8c45dfSChuck Lever 				     const struct sockaddr *sa2)
3283c8c45dfSChuck Lever {
3293c8c45dfSChuck Lever 	if (sa1->sa_family != sa2->sa_family)
3303c8c45dfSChuck Lever 		return 0;
3313c8c45dfSChuck Lever 
3323c8c45dfSChuck Lever 	switch (sa1->sa_family) {
3333c8c45dfSChuck Lever 	case AF_INET:
3343c8c45dfSChuck Lever 		return nfs_sockaddr_match_ipaddr4(sa1, sa2);
3353c8c45dfSChuck Lever 	case AF_INET6:
3363c8c45dfSChuck Lever 		return nfs_sockaddr_match_ipaddr6(sa1, sa2);
3373c8c45dfSChuck Lever 	}
3383c8c45dfSChuck Lever 	return 0;
3393c8c45dfSChuck Lever }
3403c8c45dfSChuck Lever 
3413c8c45dfSChuck Lever /*
3423c8c45dfSChuck Lever  * Test if two socket addresses represent the same actual socket,
3433c8c45dfSChuck Lever  * by comparing (only) relevant fields, including the port number.
344d7371c41SIan Dall  */
345d7371c41SIan Dall static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
346d7371c41SIan Dall 			    const struct sockaddr *sa2)
347d7371c41SIan Dall {
348d7371c41SIan Dall 	if (sa1->sa_family != sa2->sa_family)
349d7371c41SIan Dall 		return 0;
350d7371c41SIan Dall 
351d7371c41SIan Dall 	switch (sa1->sa_family) {
352d7371c41SIan Dall 	case AF_INET:
3539f4c899cSTrond Myklebust 		return nfs_sockaddr_cmp_ip4(sa1, sa2);
354d7371c41SIan Dall 	case AF_INET6:
3559f4c899cSTrond Myklebust 		return nfs_sockaddr_cmp_ip6(sa1, sa2);
356d7371c41SIan Dall 	}
357d7371c41SIan Dall 	return 0;
358d7371c41SIan Dall }
359d7371c41SIan Dall 
360d7371c41SIan Dall /*
361c81468a1STrond Myklebust  * Find a client by IP address and protocol version
362c81468a1STrond Myklebust  * - returns NULL if no such client
36324c8dbbbSDavid Howells  */
364ff052645SChuck Lever struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
365c81468a1STrond Myklebust {
366c81468a1STrond Myklebust 	struct nfs_client *clp;
367c81468a1STrond Myklebust 
368c81468a1STrond Myklebust 	spin_lock(&nfs_client_lock);
369c81468a1STrond Myklebust 	list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
3703b0d3f93STrond Myklebust 		struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
3713b0d3f93STrond Myklebust 
372c81468a1STrond Myklebust 		/* Don't match clients that failed to initialise properly */
37376db6d95SAndy Adamson 		if (!(clp->cl_cons_state == NFS_CS_READY ||
37476db6d95SAndy Adamson 		      clp->cl_cons_state == NFS_CS_SESSION_INITING))
375c81468a1STrond Myklebust 			continue;
376c81468a1STrond Myklebust 
377c81468a1STrond Myklebust 		/* Different NFS versions cannot share the same nfs_client */
37840c55319STrond Myklebust 		if (clp->rpc_ops->version != nfsversion)
379c81468a1STrond Myklebust 			continue;
380c81468a1STrond Myklebust 
381c81468a1STrond Myklebust 		/* Match only the IP address, not the port number */
3823b0d3f93STrond Myklebust 		if (!nfs_sockaddr_match_ipaddr(addr, clap))
383c81468a1STrond Myklebust 			continue;
384c81468a1STrond Myklebust 
385c81468a1STrond Myklebust 		atomic_inc(&clp->cl_count);
386c81468a1STrond Myklebust 		spin_unlock(&nfs_client_lock);
387c81468a1STrond Myklebust 		return clp;
388c81468a1STrond Myklebust 	}
389c81468a1STrond Myklebust 	spin_unlock(&nfs_client_lock);
390c81468a1STrond Myklebust 	return NULL;
391c81468a1STrond Myklebust }
392c81468a1STrond Myklebust 
393c81468a1STrond Myklebust /*
3943fbd67adSTrond Myklebust  * Find a client by IP address and protocol version
3953fbd67adSTrond Myklebust  * - returns NULL if no such client
3963fbd67adSTrond Myklebust  */
3973fbd67adSTrond Myklebust struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
3983fbd67adSTrond Myklebust {
3993fbd67adSTrond Myklebust 	struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
4003fbd67adSTrond Myklebust 	u32 nfsvers = clp->rpc_ops->version;
4013fbd67adSTrond Myklebust 
4023fbd67adSTrond Myklebust 	spin_lock(&nfs_client_lock);
4033fbd67adSTrond Myklebust 	list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
4043fbd67adSTrond Myklebust 		struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
4053fbd67adSTrond Myklebust 
4063fbd67adSTrond Myklebust 		/* Don't match clients that failed to initialise properly */
4073fbd67adSTrond Myklebust 		if (clp->cl_cons_state != NFS_CS_READY)
4083fbd67adSTrond Myklebust 			continue;
4093fbd67adSTrond Myklebust 
4103fbd67adSTrond Myklebust 		/* Different NFS versions cannot share the same nfs_client */
4113fbd67adSTrond Myklebust 		if (clp->rpc_ops->version != nfsvers)
4123fbd67adSTrond Myklebust 			continue;
4133fbd67adSTrond Myklebust 
4143fbd67adSTrond Myklebust 		/* Match only the IP address, not the port number */
4153fbd67adSTrond Myklebust 		if (!nfs_sockaddr_match_ipaddr(sap, clap))
4163fbd67adSTrond Myklebust 			continue;
4173fbd67adSTrond Myklebust 
4183fbd67adSTrond Myklebust 		atomic_inc(&clp->cl_count);
4193fbd67adSTrond Myklebust 		spin_unlock(&nfs_client_lock);
4203fbd67adSTrond Myklebust 		return clp;
4213fbd67adSTrond Myklebust 	}
4223fbd67adSTrond Myklebust 	spin_unlock(&nfs_client_lock);
4233fbd67adSTrond Myklebust 	return NULL;
4243fbd67adSTrond Myklebust }
4253fbd67adSTrond Myklebust 
4263fbd67adSTrond Myklebust /*
427c81468a1STrond Myklebust  * Find an nfs_client on the list that matches the initialisation data
428c81468a1STrond Myklebust  * that is supplied.
429c81468a1STrond Myklebust  */
430c81468a1STrond Myklebust static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
43124c8dbbbSDavid Howells {
43224c8dbbbSDavid Howells 	struct nfs_client *clp;
433d7371c41SIan Dall 	const struct sockaddr *sap = data->addr;
43424c8dbbbSDavid Howells 
43524c8dbbbSDavid Howells 	list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
436d7371c41SIan Dall 	        const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
43713bbc06aSTrond Myklebust 		/* Don't match clients that failed to initialise properly */
43813bbc06aSTrond Myklebust 		if (clp->cl_cons_state < 0)
43913bbc06aSTrond Myklebust 			continue;
44013bbc06aSTrond Myklebust 
44124c8dbbbSDavid Howells 		/* Different NFS versions cannot share the same nfs_client */
44240c55319STrond Myklebust 		if (clp->rpc_ops != data->rpc_ops)
44324c8dbbbSDavid Howells 			continue;
44424c8dbbbSDavid Howells 
44559dca3b2STrond Myklebust 		if (clp->cl_proto != data->proto)
44659dca3b2STrond Myklebust 			continue;
4475aae4a9aSBenny Halevy 		/* Match nfsv4 minorversion */
4485aae4a9aSBenny Halevy 		if (clp->cl_minorversion != data->minorversion)
4495aae4a9aSBenny Halevy 			continue;
450c81468a1STrond Myklebust 		/* Match the full socket address */
451d7371c41SIan Dall 		if (!nfs_sockaddr_cmp(sap, clap))
45224c8dbbbSDavid Howells 			continue;
45324c8dbbbSDavid Howells 
45424c8dbbbSDavid Howells 		atomic_inc(&clp->cl_count);
45524c8dbbbSDavid Howells 		return clp;
45624c8dbbbSDavid Howells 	}
457c81468a1STrond Myklebust 	return NULL;
45824c8dbbbSDavid Howells }
45924c8dbbbSDavid Howells 
46024c8dbbbSDavid Howells /*
46124c8dbbbSDavid Howells  * Look up a client by IP address and protocol version
46224c8dbbbSDavid Howells  * - creates a new record if one doesn't yet exist
46324c8dbbbSDavid Howells  */
4643a498026STrond Myklebust static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
46524c8dbbbSDavid Howells {
46624c8dbbbSDavid Howells 	struct nfs_client *clp, *new = NULL;
46724c8dbbbSDavid Howells 	int error;
46824c8dbbbSDavid Howells 
469d7422c47SChuck Lever 	dprintk("--> nfs_get_client(%s,v%u)\n",
470d7422c47SChuck Lever 		cl_init->hostname ?: "", cl_init->rpc_ops->version);
47124c8dbbbSDavid Howells 
47224c8dbbbSDavid Howells 	/* see if the client already exists */
47324c8dbbbSDavid Howells 	do {
47424c8dbbbSDavid Howells 		spin_lock(&nfs_client_lock);
47524c8dbbbSDavid Howells 
476c81468a1STrond Myklebust 		clp = nfs_match_client(cl_init);
47724c8dbbbSDavid Howells 		if (clp)
47824c8dbbbSDavid Howells 			goto found_client;
47924c8dbbbSDavid Howells 		if (new)
48024c8dbbbSDavid Howells 			goto install_client;
48124c8dbbbSDavid Howells 
48224c8dbbbSDavid Howells 		spin_unlock(&nfs_client_lock);
48324c8dbbbSDavid Howells 
4843a498026STrond Myklebust 		new = nfs_alloc_client(cl_init);
485a21bdd9bSChuck Lever 	} while (!IS_ERR(new));
48624c8dbbbSDavid Howells 
487a21bdd9bSChuck Lever 	dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
488a21bdd9bSChuck Lever 	return new;
48924c8dbbbSDavid Howells 
49024c8dbbbSDavid Howells 	/* install a new client and return with it unready */
49124c8dbbbSDavid Howells install_client:
49224c8dbbbSDavid Howells 	clp = new;
49324c8dbbbSDavid Howells 	list_add(&clp->cl_share_link, &nfs_client_list);
49424c8dbbbSDavid Howells 	spin_unlock(&nfs_client_lock);
49524c8dbbbSDavid Howells 	dprintk("--> nfs_get_client() = %p [new]\n", clp);
49624c8dbbbSDavid Howells 	return clp;
49724c8dbbbSDavid Howells 
49824c8dbbbSDavid Howells 	/* found an existing client
49924c8dbbbSDavid Howells 	 * - make sure it's ready before returning
50024c8dbbbSDavid Howells 	 */
50124c8dbbbSDavid Howells found_client:
50224c8dbbbSDavid Howells 	spin_unlock(&nfs_client_lock);
50324c8dbbbSDavid Howells 
50424c8dbbbSDavid Howells 	if (new)
50524c8dbbbSDavid Howells 		nfs_free_client(new);
50624c8dbbbSDavid Howells 
507150030b7SMatthew Wilcox 	error = wait_event_killable(nfs_client_active_wq,
50876db6d95SAndy Adamson 				clp->cl_cons_state < NFS_CS_INITING);
5090bae89ecSTrond Myklebust 	if (error < 0) {
51024c8dbbbSDavid Howells 		nfs_put_client(clp);
51124c8dbbbSDavid Howells 		return ERR_PTR(-ERESTARTSYS);
51224c8dbbbSDavid Howells 	}
51324c8dbbbSDavid Howells 
51424c8dbbbSDavid Howells 	if (clp->cl_cons_state < NFS_CS_READY) {
51524c8dbbbSDavid Howells 		error = clp->cl_cons_state;
51624c8dbbbSDavid Howells 		nfs_put_client(clp);
51724c8dbbbSDavid Howells 		return ERR_PTR(error);
51824c8dbbbSDavid Howells 	}
51924c8dbbbSDavid Howells 
52054ceac45SDavid Howells 	BUG_ON(clp->cl_cons_state != NFS_CS_READY);
52154ceac45SDavid Howells 
52224c8dbbbSDavid Howells 	dprintk("--> nfs_get_client() = %p [share]\n", clp);
52324c8dbbbSDavid Howells 	return clp;
52424c8dbbbSDavid Howells }
52524c8dbbbSDavid Howells 
52624c8dbbbSDavid Howells /*
52724c8dbbbSDavid Howells  * Mark a server as ready or failed
52824c8dbbbSDavid Howells  */
52976db6d95SAndy Adamson void nfs_mark_client_ready(struct nfs_client *clp, int state)
53024c8dbbbSDavid Howells {
53124c8dbbbSDavid Howells 	clp->cl_cons_state = state;
53224c8dbbbSDavid Howells 	wake_up_all(&nfs_client_active_wq);
53324c8dbbbSDavid Howells }
5345006a76cSDavid Howells 
5355006a76cSDavid Howells /*
536008f55d0SBenny Halevy  * With sessions, the client is not marked ready until after a
537008f55d0SBenny Halevy  * successful EXCHANGE_ID and CREATE_SESSION.
538008f55d0SBenny Halevy  *
539008f55d0SBenny Halevy  * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
540008f55d0SBenny Halevy  * other versions of NFS can be tried.
541008f55d0SBenny Halevy  */
542008f55d0SBenny Halevy int nfs4_check_client_ready(struct nfs_client *clp)
543008f55d0SBenny Halevy {
544008f55d0SBenny Halevy 	if (!nfs4_has_session(clp))
545008f55d0SBenny Halevy 		return 0;
546008f55d0SBenny Halevy 	if (clp->cl_cons_state < NFS_CS_READY)
547008f55d0SBenny Halevy 		return -EPROTONOSUPPORT;
548008f55d0SBenny Halevy 	return 0;
549008f55d0SBenny Halevy }
550008f55d0SBenny Halevy 
551008f55d0SBenny Halevy /*
5525006a76cSDavid Howells  * Initialise the timeout values for a connection
5535006a76cSDavid Howells  */
5545006a76cSDavid Howells static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
5555006a76cSDavid Howells 				    unsigned int timeo, unsigned int retrans)
5565006a76cSDavid Howells {
5575006a76cSDavid Howells 	to->to_initval = timeo * HZ / 10;
5585006a76cSDavid Howells 	to->to_retries = retrans;
5595006a76cSDavid Howells 
5605006a76cSDavid Howells 	switch (proto) {
5610896a725S\"Talpey, Thomas\ 	case XPRT_TRANSPORT_TCP:
5622cf7ff7aS\"Talpey, Thomas\ 	case XPRT_TRANSPORT_RDMA:
563259875efSTrond Myklebust 		if (to->to_retries == 0)
564259875efSTrond Myklebust 			to->to_retries = NFS_DEF_TCP_RETRANS;
5657a3e3e18STrond Myklebust 		if (to->to_initval == 0)
566259875efSTrond Myklebust 			to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
5675006a76cSDavid Howells 		if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
5685006a76cSDavid Howells 			to->to_initval = NFS_MAX_TCP_TIMEOUT;
5695006a76cSDavid Howells 		to->to_increment = to->to_initval;
5705006a76cSDavid Howells 		to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
5717a3e3e18STrond Myklebust 		if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
5727a3e3e18STrond Myklebust 			to->to_maxval = NFS_MAX_TCP_TIMEOUT;
5737a3e3e18STrond Myklebust 		if (to->to_maxval < to->to_initval)
5747a3e3e18STrond Myklebust 			to->to_maxval = to->to_initval;
5755006a76cSDavid Howells 		to->to_exponential = 0;
5765006a76cSDavid Howells 		break;
5770896a725S\"Talpey, Thomas\ 	case XPRT_TRANSPORT_UDP:
578259875efSTrond Myklebust 		if (to->to_retries == 0)
579259875efSTrond Myklebust 			to->to_retries = NFS_DEF_UDP_RETRANS;
5805006a76cSDavid Howells 		if (!to->to_initval)
581259875efSTrond Myklebust 			to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
5825006a76cSDavid Howells 		if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
5835006a76cSDavid Howells 			to->to_initval = NFS_MAX_UDP_TIMEOUT;
5845006a76cSDavid Howells 		to->to_maxval = NFS_MAX_UDP_TIMEOUT;
5855006a76cSDavid Howells 		to->to_exponential = 1;
5865006a76cSDavid Howells 		break;
587259875efSTrond Myklebust 	default:
588259875efSTrond Myklebust 		BUG();
5895006a76cSDavid Howells 	}
5905006a76cSDavid Howells }
5915006a76cSDavid Howells 
5925006a76cSDavid Howells /*
5935006a76cSDavid Howells  * Create an RPC client handle
5945006a76cSDavid Howells  */
59559dca3b2STrond Myklebust static int nfs_create_rpc_client(struct nfs_client *clp,
59633170233STrond Myklebust 				 const struct rpc_timeout *timeparms,
59743d78ef2SChuck Lever 				 rpc_authflavor_t flavor,
5984a01b8a4SChuck Lever 				 int discrtry, int noresvport)
5995006a76cSDavid Howells {
6005006a76cSDavid Howells 	struct rpc_clnt		*clnt = NULL;
60141877d20SChuck Lever 	struct rpc_create_args args = {
60259dca3b2STrond Myklebust 		.protocol	= clp->cl_proto,
60341877d20SChuck Lever 		.address	= (struct sockaddr *)&clp->cl_addr,
6046e4cffd7SChuck Lever 		.addrsize	= clp->cl_addrlen,
60533170233STrond Myklebust 		.timeout	= timeparms,
60641877d20SChuck Lever 		.servername	= clp->cl_hostname,
60741877d20SChuck Lever 		.program	= &nfs_program,
60841877d20SChuck Lever 		.version	= clp->rpc_ops->version,
60941877d20SChuck Lever 		.authflavor	= flavor,
61041877d20SChuck Lever 	};
6115006a76cSDavid Howells 
6124a01b8a4SChuck Lever 	if (discrtry)
6134a01b8a4SChuck Lever 		args.flags |= RPC_CLNT_CREATE_DISCRTRY;
6144a01b8a4SChuck Lever 	if (noresvport)
6154a01b8a4SChuck Lever 		args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
6164a01b8a4SChuck Lever 
6175006a76cSDavid Howells 	if (!IS_ERR(clp->cl_rpcclient))
6185006a76cSDavid Howells 		return 0;
6195006a76cSDavid Howells 
62041877d20SChuck Lever 	clnt = rpc_create(&args);
6215006a76cSDavid Howells 	if (IS_ERR(clnt)) {
6225006a76cSDavid Howells 		dprintk("%s: cannot create RPC client. Error = %ld\n",
6233110ff80SHarvey Harrison 				__func__, PTR_ERR(clnt));
6245006a76cSDavid Howells 		return PTR_ERR(clnt);
6255006a76cSDavid Howells 	}
6265006a76cSDavid Howells 
6275006a76cSDavid Howells 	clp->cl_rpcclient = clnt;
6285006a76cSDavid Howells 	return 0;
6295006a76cSDavid Howells }
63054ceac45SDavid Howells 
63154ceac45SDavid Howells /*
63254ceac45SDavid Howells  * Version 2 or 3 client destruction
63354ceac45SDavid Howells  */
63454ceac45SDavid Howells static void nfs_destroy_server(struct nfs_server *server)
63554ceac45SDavid Howells {
63654ceac45SDavid Howells 	if (!(server->flags & NFS_MOUNT_NONLM))
6379289e7f9SChuck Lever 		nlmclnt_done(server->nlm_host);
63854ceac45SDavid Howells }
63954ceac45SDavid Howells 
64054ceac45SDavid Howells /*
64154ceac45SDavid Howells  * Version 2 or 3 lockd setup
64254ceac45SDavid Howells  */
64354ceac45SDavid Howells static int nfs_start_lockd(struct nfs_server *server)
64454ceac45SDavid Howells {
6459289e7f9SChuck Lever 	struct nlm_host *host;
6469289e7f9SChuck Lever 	struct nfs_client *clp = server->nfs_client;
647883bb163SChuck Lever 	struct nlmclnt_initdata nlm_init = {
648883bb163SChuck Lever 		.hostname	= clp->cl_hostname,
649883bb163SChuck Lever 		.address	= (struct sockaddr *)&clp->cl_addr,
650883bb163SChuck Lever 		.addrlen	= clp->cl_addrlen,
651883bb163SChuck Lever 		.protocol	= server->flags & NFS_MOUNT_TCP ?
652883bb163SChuck Lever 						IPPROTO_TCP : IPPROTO_UDP,
653883bb163SChuck Lever 		.nfs_version	= clp->rpc_ops->version,
6540cb2659bSChuck Lever 		.noresvport	= server->flags & NFS_MOUNT_NORESVPORT ?
6550cb2659bSChuck Lever 					1 : 0,
656883bb163SChuck Lever 	};
65754ceac45SDavid Howells 
658883bb163SChuck Lever 	if (nlm_init.nfs_version > 3)
6599289e7f9SChuck Lever 		return 0;
66054ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_NONLM)
6619289e7f9SChuck Lever 		return 0;
6629289e7f9SChuck Lever 
663883bb163SChuck Lever 	host = nlmclnt_init(&nlm_init);
6649289e7f9SChuck Lever 	if (IS_ERR(host))
6659289e7f9SChuck Lever 		return PTR_ERR(host);
6669289e7f9SChuck Lever 
6679289e7f9SChuck Lever 	server->nlm_host = host;
66854ceac45SDavid Howells 	server->destroy = nfs_destroy_server;
6699289e7f9SChuck Lever 	return 0;
67054ceac45SDavid Howells }
67154ceac45SDavid Howells 
67254ceac45SDavid Howells /*
67354ceac45SDavid Howells  * Initialise an NFSv3 ACL client connection
67454ceac45SDavid Howells  */
67554ceac45SDavid Howells #ifdef CONFIG_NFS_V3_ACL
67654ceac45SDavid Howells static void nfs_init_server_aclclient(struct nfs_server *server)
67754ceac45SDavid Howells {
67840c55319STrond Myklebust 	if (server->nfs_client->rpc_ops->version != 3)
67954ceac45SDavid Howells 		goto out_noacl;
68054ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_NOACL)
68154ceac45SDavid Howells 		goto out_noacl;
68254ceac45SDavid Howells 
68354ceac45SDavid Howells 	server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
68454ceac45SDavid Howells 	if (IS_ERR(server->client_acl))
68554ceac45SDavid Howells 		goto out_noacl;
68654ceac45SDavid Howells 
68754ceac45SDavid Howells 	/* No errors! Assume that Sun nfsacls are supported */
68854ceac45SDavid Howells 	server->caps |= NFS_CAP_ACLS;
68954ceac45SDavid Howells 	return;
69054ceac45SDavid Howells 
69154ceac45SDavid Howells out_noacl:
69254ceac45SDavid Howells 	server->caps &= ~NFS_CAP_ACLS;
69354ceac45SDavid Howells }
69454ceac45SDavid Howells #else
69554ceac45SDavid Howells static inline void nfs_init_server_aclclient(struct nfs_server *server)
69654ceac45SDavid Howells {
69754ceac45SDavid Howells 	server->flags &= ~NFS_MOUNT_NOACL;
69854ceac45SDavid Howells 	server->caps &= ~NFS_CAP_ACLS;
69954ceac45SDavid Howells }
70054ceac45SDavid Howells #endif
70154ceac45SDavid Howells 
70254ceac45SDavid Howells /*
70354ceac45SDavid Howells  * Create a general RPC client
70454ceac45SDavid Howells  */
70533170233STrond Myklebust static int nfs_init_server_rpcclient(struct nfs_server *server,
70633170233STrond Myklebust 		const struct rpc_timeout *timeo,
70733170233STrond Myklebust 		rpc_authflavor_t pseudoflavour)
70854ceac45SDavid Howells {
70954ceac45SDavid Howells 	struct nfs_client *clp = server->nfs_client;
71054ceac45SDavid Howells 
71154ceac45SDavid Howells 	server->client = rpc_clone_client(clp->cl_rpcclient);
71254ceac45SDavid Howells 	if (IS_ERR(server->client)) {
7133110ff80SHarvey Harrison 		dprintk("%s: couldn't create rpc_client!\n", __func__);
71454ceac45SDavid Howells 		return PTR_ERR(server->client);
71554ceac45SDavid Howells 	}
71654ceac45SDavid Howells 
71733170233STrond Myklebust 	memcpy(&server->client->cl_timeout_default,
71833170233STrond Myklebust 			timeo,
71933170233STrond Myklebust 			sizeof(server->client->cl_timeout_default));
72033170233STrond Myklebust 	server->client->cl_timeout = &server->client->cl_timeout_default;
72133170233STrond Myklebust 
72254ceac45SDavid Howells 	if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
72354ceac45SDavid Howells 		struct rpc_auth *auth;
72454ceac45SDavid Howells 
72554ceac45SDavid Howells 		auth = rpcauth_create(pseudoflavour, server->client);
72654ceac45SDavid Howells 		if (IS_ERR(auth)) {
7273110ff80SHarvey Harrison 			dprintk("%s: couldn't create credcache!\n", __func__);
72854ceac45SDavid Howells 			return PTR_ERR(auth);
72954ceac45SDavid Howells 		}
73054ceac45SDavid Howells 	}
73154ceac45SDavid Howells 	server->client->cl_softrtry = 0;
73254ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_SOFT)
73354ceac45SDavid Howells 		server->client->cl_softrtry = 1;
73454ceac45SDavid Howells 
73554ceac45SDavid Howells 	return 0;
73654ceac45SDavid Howells }
73754ceac45SDavid Howells 
73854ceac45SDavid Howells /*
73954ceac45SDavid Howells  * Initialise an NFS2 or NFS3 client
74054ceac45SDavid Howells  */
7412283f8d6S\"Talpey, Thomas\ static int nfs_init_client(struct nfs_client *clp,
74233170233STrond Myklebust 			   const struct rpc_timeout *timeparms,
7432283f8d6S\"Talpey, Thomas\ 			   const struct nfs_parsed_mount_data *data)
74454ceac45SDavid Howells {
74554ceac45SDavid Howells 	int error;
74654ceac45SDavid Howells 
74754ceac45SDavid Howells 	if (clp->cl_cons_state == NFS_CS_READY) {
74854ceac45SDavid Howells 		/* the client is already initialised */
74954ceac45SDavid Howells 		dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
75054ceac45SDavid Howells 		return 0;
75154ceac45SDavid Howells 	}
75254ceac45SDavid Howells 
75354ceac45SDavid Howells 	/*
75454ceac45SDavid Howells 	 * Create a client RPC handle for doing FSSTAT with UNIX auth only
75554ceac45SDavid Howells 	 * - RFC 2623, sec 2.3.2
75654ceac45SDavid Howells 	 */
757d740351bSChuck Lever 	error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
758d740351bSChuck Lever 				      0, data->flags & NFS_MOUNT_NORESVPORT);
75954ceac45SDavid Howells 	if (error < 0)
76054ceac45SDavid Howells 		goto error;
76154ceac45SDavid Howells 	nfs_mark_client_ready(clp, NFS_CS_READY);
76254ceac45SDavid Howells 	return 0;
76354ceac45SDavid Howells 
76454ceac45SDavid Howells error:
76554ceac45SDavid Howells 	nfs_mark_client_ready(clp, error);
76654ceac45SDavid Howells 	dprintk("<-- nfs_init_client() = xerror %d\n", error);
76754ceac45SDavid Howells 	return error;
76854ceac45SDavid Howells }
76954ceac45SDavid Howells 
77054ceac45SDavid Howells /*
77154ceac45SDavid Howells  * Create a version 2 or 3 client
77254ceac45SDavid Howells  */
7732283f8d6S\"Talpey, Thomas\ static int nfs_init_server(struct nfs_server *server,
7742283f8d6S\"Talpey, Thomas\ 			   const struct nfs_parsed_mount_data *data)
77554ceac45SDavid Howells {
7763a498026STrond Myklebust 	struct nfs_client_initdata cl_init = {
7773a498026STrond Myklebust 		.hostname = data->nfs_server.hostname,
778d7422c47SChuck Lever 		.addr = (const struct sockaddr *)&data->nfs_server.address,
7794c568017SChuck Lever 		.addrlen = data->nfs_server.addrlen,
78040c55319STrond Myklebust 		.rpc_ops = &nfs_v2_clientops,
78159dca3b2STrond Myklebust 		.proto = data->nfs_server.protocol,
7823a498026STrond Myklebust 	};
78333170233STrond Myklebust 	struct rpc_timeout timeparms;
78454ceac45SDavid Howells 	struct nfs_client *clp;
7853a498026STrond Myklebust 	int error;
78654ceac45SDavid Howells 
78754ceac45SDavid Howells 	dprintk("--> nfs_init_server()\n");
78854ceac45SDavid Howells 
78954ceac45SDavid Howells #ifdef CONFIG_NFS_V3
79054ceac45SDavid Howells 	if (data->flags & NFS_MOUNT_VER3)
79140c55319STrond Myklebust 		cl_init.rpc_ops = &nfs_v3_clientops;
79254ceac45SDavid Howells #endif
79354ceac45SDavid Howells 
79454ceac45SDavid Howells 	/* Allocate or find a client reference we can use */
7953a498026STrond Myklebust 	clp = nfs_get_client(&cl_init);
79654ceac45SDavid Howells 	if (IS_ERR(clp)) {
79754ceac45SDavid Howells 		dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
79854ceac45SDavid Howells 		return PTR_ERR(clp);
79954ceac45SDavid Howells 	}
80054ceac45SDavid Howells 
80133170233STrond Myklebust 	nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
80233170233STrond Myklebust 			data->timeo, data->retrans);
80333170233STrond Myklebust 	error = nfs_init_client(clp, &timeparms, data);
80454ceac45SDavid Howells 	if (error < 0)
80554ceac45SDavid Howells 		goto error;
80654ceac45SDavid Howells 
80754ceac45SDavid Howells 	server->nfs_client = clp;
80854ceac45SDavid Howells 
80954ceac45SDavid Howells 	/* Initialise the client representation from the mount data */
810ff3525a5STrond Myklebust 	server->flags = data->flags;
811b797cac7SDavid Howells 	server->options = data->options;
81254ceac45SDavid Howells 
81354ceac45SDavid Howells 	if (data->rsize)
81454ceac45SDavid Howells 		server->rsize = nfs_block_size(data->rsize, NULL);
81554ceac45SDavid Howells 	if (data->wsize)
81654ceac45SDavid Howells 		server->wsize = nfs_block_size(data->wsize, NULL);
81754ceac45SDavid Howells 
81854ceac45SDavid Howells 	server->acregmin = data->acregmin * HZ;
81954ceac45SDavid Howells 	server->acregmax = data->acregmax * HZ;
82054ceac45SDavid Howells 	server->acdirmin = data->acdirmin * HZ;
82154ceac45SDavid Howells 	server->acdirmax = data->acdirmax * HZ;
82254ceac45SDavid Howells 
82354ceac45SDavid Howells 	/* Start lockd here, before we might error out */
82454ceac45SDavid Howells 	error = nfs_start_lockd(server);
82554ceac45SDavid Howells 	if (error < 0)
82654ceac45SDavid Howells 		goto error;
82754ceac45SDavid Howells 
828f22d6d79SChuck Lever 	server->port = data->nfs_server.port;
829f22d6d79SChuck Lever 
83033170233STrond Myklebust 	error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
83154ceac45SDavid Howells 	if (error < 0)
83254ceac45SDavid Howells 		goto error;
83354ceac45SDavid Howells 
8343f8400d1SChuck Lever 	/* Preserve the values of mount_server-related mount options */
8353f8400d1SChuck Lever 	if (data->mount_server.addrlen) {
8363f8400d1SChuck Lever 		memcpy(&server->mountd_address, &data->mount_server.address,
8373f8400d1SChuck Lever 			data->mount_server.addrlen);
8383f8400d1SChuck Lever 		server->mountd_addrlen = data->mount_server.addrlen;
8393f8400d1SChuck Lever 	}
8403f8400d1SChuck Lever 	server->mountd_version = data->mount_server.version;
8413f8400d1SChuck Lever 	server->mountd_port = data->mount_server.port;
8423f8400d1SChuck Lever 	server->mountd_protocol = data->mount_server.protocol;
8433f8400d1SChuck Lever 
84454ceac45SDavid Howells 	server->namelen  = data->namlen;
84554ceac45SDavid Howells 	/* Create a client RPC handle for the NFSv3 ACL management interface */
84654ceac45SDavid Howells 	nfs_init_server_aclclient(server);
84754ceac45SDavid Howells 	dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
84854ceac45SDavid Howells 	return 0;
84954ceac45SDavid Howells 
85054ceac45SDavid Howells error:
85154ceac45SDavid Howells 	server->nfs_client = NULL;
85254ceac45SDavid Howells 	nfs_put_client(clp);
85354ceac45SDavid Howells 	dprintk("<-- nfs_init_server() = xerror %d\n", error);
85454ceac45SDavid Howells 	return error;
85554ceac45SDavid Howells }
85654ceac45SDavid Howells 
85754ceac45SDavid Howells /*
85854ceac45SDavid Howells  * Load up the server record from information gained in an fsinfo record
85954ceac45SDavid Howells  */
86054ceac45SDavid Howells static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
86154ceac45SDavid Howells {
86254ceac45SDavid Howells 	unsigned long max_rpc_payload;
86354ceac45SDavid Howells 
86454ceac45SDavid Howells 	/* Work out a lot of parameters */
86554ceac45SDavid Howells 	if (server->rsize == 0)
86654ceac45SDavid Howells 		server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
86754ceac45SDavid Howells 	if (server->wsize == 0)
86854ceac45SDavid Howells 		server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
86954ceac45SDavid Howells 
87054ceac45SDavid Howells 	if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
87154ceac45SDavid Howells 		server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
87254ceac45SDavid Howells 	if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
87354ceac45SDavid Howells 		server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
87454ceac45SDavid Howells 
87554ceac45SDavid Howells 	max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
87654ceac45SDavid Howells 	if (server->rsize > max_rpc_payload)
87754ceac45SDavid Howells 		server->rsize = max_rpc_payload;
87854ceac45SDavid Howells 	if (server->rsize > NFS_MAX_FILE_IO_SIZE)
87954ceac45SDavid Howells 		server->rsize = NFS_MAX_FILE_IO_SIZE;
88054ceac45SDavid Howells 	server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
881e0bf68ddSPeter Zijlstra 
882*d993831fSJens Axboe 	server->backing_dev_info.name = "nfs";
88354ceac45SDavid Howells 	server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
88454ceac45SDavid Howells 
88554ceac45SDavid Howells 	if (server->wsize > max_rpc_payload)
88654ceac45SDavid Howells 		server->wsize = max_rpc_payload;
88754ceac45SDavid Howells 	if (server->wsize > NFS_MAX_FILE_IO_SIZE)
88854ceac45SDavid Howells 		server->wsize = NFS_MAX_FILE_IO_SIZE;
88954ceac45SDavid Howells 	server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
89054ceac45SDavid Howells 	server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
89154ceac45SDavid Howells 
89254ceac45SDavid Howells 	server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
89354ceac45SDavid Howells 	if (server->dtsize > PAGE_CACHE_SIZE)
89454ceac45SDavid Howells 		server->dtsize = PAGE_CACHE_SIZE;
89554ceac45SDavid Howells 	if (server->dtsize > server->rsize)
89654ceac45SDavid Howells 		server->dtsize = server->rsize;
89754ceac45SDavid Howells 
89854ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_NOAC) {
89954ceac45SDavid Howells 		server->acregmin = server->acregmax = 0;
90054ceac45SDavid Howells 		server->acdirmin = server->acdirmax = 0;
90154ceac45SDavid Howells 	}
90254ceac45SDavid Howells 
90354ceac45SDavid Howells 	server->maxfilesize = fsinfo->maxfilesize;
90454ceac45SDavid Howells 
90554ceac45SDavid Howells 	/* We're airborne Set socket buffersize */
90654ceac45SDavid Howells 	rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
90754ceac45SDavid Howells }
90854ceac45SDavid Howells 
90954ceac45SDavid Howells /*
91054ceac45SDavid Howells  * Probe filesystem information, including the FSID on v2/v3
91154ceac45SDavid Howells  */
91254ceac45SDavid Howells static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
91354ceac45SDavid Howells {
91454ceac45SDavid Howells 	struct nfs_fsinfo fsinfo;
91554ceac45SDavid Howells 	struct nfs_client *clp = server->nfs_client;
91654ceac45SDavid Howells 	int error;
91754ceac45SDavid Howells 
91854ceac45SDavid Howells 	dprintk("--> nfs_probe_fsinfo()\n");
91954ceac45SDavid Howells 
92054ceac45SDavid Howells 	if (clp->rpc_ops->set_capabilities != NULL) {
92154ceac45SDavid Howells 		error = clp->rpc_ops->set_capabilities(server, mntfh);
92254ceac45SDavid Howells 		if (error < 0)
92354ceac45SDavid Howells 			goto out_error;
92454ceac45SDavid Howells 	}
92554ceac45SDavid Howells 
92654ceac45SDavid Howells 	fsinfo.fattr = fattr;
92754ceac45SDavid Howells 	nfs_fattr_init(fattr);
92854ceac45SDavid Howells 	error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
92954ceac45SDavid Howells 	if (error < 0)
93054ceac45SDavid Howells 		goto out_error;
93154ceac45SDavid Howells 
93254ceac45SDavid Howells 	nfs_server_set_fsinfo(server, &fsinfo);
933e0bf68ddSPeter Zijlstra 	error = bdi_init(&server->backing_dev_info);
934e0bf68ddSPeter Zijlstra 	if (error)
935e0bf68ddSPeter Zijlstra 		goto out_error;
936e0bf68ddSPeter Zijlstra 
93754ceac45SDavid Howells 
93854ceac45SDavid Howells 	/* Get some general file system info */
93954ceac45SDavid Howells 	if (server->namelen == 0) {
94054ceac45SDavid Howells 		struct nfs_pathconf pathinfo;
94154ceac45SDavid Howells 
94254ceac45SDavid Howells 		pathinfo.fattr = fattr;
94354ceac45SDavid Howells 		nfs_fattr_init(fattr);
94454ceac45SDavid Howells 
94554ceac45SDavid Howells 		if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
94654ceac45SDavid Howells 			server->namelen = pathinfo.max_namelen;
94754ceac45SDavid Howells 	}
94854ceac45SDavid Howells 
94954ceac45SDavid Howells 	dprintk("<-- nfs_probe_fsinfo() = 0\n");
95054ceac45SDavid Howells 	return 0;
95154ceac45SDavid Howells 
95254ceac45SDavid Howells out_error:
95354ceac45SDavid Howells 	dprintk("nfs_probe_fsinfo: error = %d\n", -error);
95454ceac45SDavid Howells 	return error;
95554ceac45SDavid Howells }
95654ceac45SDavid Howells 
95754ceac45SDavid Howells /*
95854ceac45SDavid Howells  * Copy useful information when duplicating a server record
95954ceac45SDavid Howells  */
96054ceac45SDavid Howells static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
96154ceac45SDavid Howells {
96254ceac45SDavid Howells 	target->flags = source->flags;
96354ceac45SDavid Howells 	target->acregmin = source->acregmin;
96454ceac45SDavid Howells 	target->acregmax = source->acregmax;
96554ceac45SDavid Howells 	target->acdirmin = source->acdirmin;
96654ceac45SDavid Howells 	target->acdirmax = source->acdirmax;
96754ceac45SDavid Howells 	target->caps = source->caps;
96854ceac45SDavid Howells }
96954ceac45SDavid Howells 
97054ceac45SDavid Howells /*
97154ceac45SDavid Howells  * Allocate and initialise a server record
97254ceac45SDavid Howells  */
97354ceac45SDavid Howells static struct nfs_server *nfs_alloc_server(void)
97454ceac45SDavid Howells {
97554ceac45SDavid Howells 	struct nfs_server *server;
97654ceac45SDavid Howells 
97754ceac45SDavid Howells 	server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
97854ceac45SDavid Howells 	if (!server)
97954ceac45SDavid Howells 		return NULL;
98054ceac45SDavid Howells 
98154ceac45SDavid Howells 	server->client = server->client_acl = ERR_PTR(-EINVAL);
98254ceac45SDavid Howells 
98354ceac45SDavid Howells 	/* Zero out the NFS state stuff */
98454ceac45SDavid Howells 	INIT_LIST_HEAD(&server->client_link);
98554ceac45SDavid Howells 	INIT_LIST_HEAD(&server->master_link);
98654ceac45SDavid Howells 
987ef818a28SSteve Dickson 	atomic_set(&server->active, 0);
988ef818a28SSteve Dickson 
98954ceac45SDavid Howells 	server->io_stats = nfs_alloc_iostats();
99054ceac45SDavid Howells 	if (!server->io_stats) {
99154ceac45SDavid Howells 		kfree(server);
99254ceac45SDavid Howells 		return NULL;
99354ceac45SDavid Howells 	}
99454ceac45SDavid Howells 
99554ceac45SDavid Howells 	return server;
99654ceac45SDavid Howells }
99754ceac45SDavid Howells 
99854ceac45SDavid Howells /*
99954ceac45SDavid Howells  * Free up a server record
100054ceac45SDavid Howells  */
100154ceac45SDavid Howells void nfs_free_server(struct nfs_server *server)
100254ceac45SDavid Howells {
100354ceac45SDavid Howells 	dprintk("--> nfs_free_server()\n");
100454ceac45SDavid Howells 
100554ceac45SDavid Howells 	spin_lock(&nfs_client_lock);
100654ceac45SDavid Howells 	list_del(&server->client_link);
100754ceac45SDavid Howells 	list_del(&server->master_link);
100854ceac45SDavid Howells 	spin_unlock(&nfs_client_lock);
100954ceac45SDavid Howells 
101054ceac45SDavid Howells 	if (server->destroy != NULL)
101154ceac45SDavid Howells 		server->destroy(server);
10125cef338bSTrond Myklebust 
10135cef338bSTrond Myklebust 	if (!IS_ERR(server->client_acl))
10145cef338bSTrond Myklebust 		rpc_shutdown_client(server->client_acl);
101554ceac45SDavid Howells 	if (!IS_ERR(server->client))
101654ceac45SDavid Howells 		rpc_shutdown_client(server->client);
101754ceac45SDavid Howells 
101854ceac45SDavid Howells 	nfs_put_client(server->nfs_client);
101954ceac45SDavid Howells 
102054ceac45SDavid Howells 	nfs_free_iostats(server->io_stats);
1021e0bf68ddSPeter Zijlstra 	bdi_destroy(&server->backing_dev_info);
102254ceac45SDavid Howells 	kfree(server);
102354ceac45SDavid Howells 	nfs_release_automount_timer();
102454ceac45SDavid Howells 	dprintk("<-- nfs_free_server()\n");
102554ceac45SDavid Howells }
102654ceac45SDavid Howells 
102754ceac45SDavid Howells /*
102854ceac45SDavid Howells  * Create a version 2 or 3 volume record
102954ceac45SDavid Howells  * - keyed on server and FSID
103054ceac45SDavid Howells  */
10312283f8d6S\"Talpey, Thomas\ struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
103254ceac45SDavid Howells 				     struct nfs_fh *mntfh)
103354ceac45SDavid Howells {
103454ceac45SDavid Howells 	struct nfs_server *server;
103554ceac45SDavid Howells 	struct nfs_fattr fattr;
103654ceac45SDavid Howells 	int error;
103754ceac45SDavid Howells 
103854ceac45SDavid Howells 	server = nfs_alloc_server();
103954ceac45SDavid Howells 	if (!server)
104054ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
104154ceac45SDavid Howells 
104254ceac45SDavid Howells 	/* Get a client representation */
104354ceac45SDavid Howells 	error = nfs_init_server(server, data);
104454ceac45SDavid Howells 	if (error < 0)
104554ceac45SDavid Howells 		goto error;
104654ceac45SDavid Howells 
104754ceac45SDavid Howells 	BUG_ON(!server->nfs_client);
104854ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops);
104954ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
105054ceac45SDavid Howells 
105154ceac45SDavid Howells 	/* Probe the root fh to retrieve its FSID */
105254ceac45SDavid Howells 	error = nfs_probe_fsinfo(server, mntfh, &fattr);
105354ceac45SDavid Howells 	if (error < 0)
105454ceac45SDavid Howells 		goto error;
105554af3bb5STrond Myklebust 	if (server->nfs_client->rpc_ops->version == 3) {
105654af3bb5STrond Myklebust 		if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
105754af3bb5STrond Myklebust 			server->namelen = NFS3_MAXNAMLEN;
105854af3bb5STrond Myklebust 		if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
105954af3bb5STrond Myklebust 			server->caps |= NFS_CAP_READDIRPLUS;
106054af3bb5STrond Myklebust 	} else {
106154af3bb5STrond Myklebust 		if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
106254af3bb5STrond Myklebust 			server->namelen = NFS2_MAXNAMLEN;
106354af3bb5STrond Myklebust 	}
106454af3bb5STrond Myklebust 
106554ceac45SDavid Howells 	if (!(fattr.valid & NFS_ATTR_FATTR)) {
106654ceac45SDavid Howells 		error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
106754ceac45SDavid Howells 		if (error < 0) {
106854ceac45SDavid Howells 			dprintk("nfs_create_server: getattr error = %d\n", -error);
106954ceac45SDavid Howells 			goto error;
107054ceac45SDavid Howells 		}
107154ceac45SDavid Howells 	}
107254ceac45SDavid Howells 	memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
107354ceac45SDavid Howells 
10746daabf1bSDavid Howells 	dprintk("Server FSID: %llx:%llx\n",
10756daabf1bSDavid Howells 		(unsigned long long) server->fsid.major,
10766daabf1bSDavid Howells 		(unsigned long long) server->fsid.minor);
107754ceac45SDavid Howells 
107854ceac45SDavid Howells 	BUG_ON(!server->nfs_client);
107954ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops);
108054ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
108154ceac45SDavid Howells 
108254ceac45SDavid Howells 	spin_lock(&nfs_client_lock);
108354ceac45SDavid Howells 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
108454ceac45SDavid Howells 	list_add_tail(&server->master_link, &nfs_volume_list);
108554ceac45SDavid Howells 	spin_unlock(&nfs_client_lock);
108654ceac45SDavid Howells 
108754ceac45SDavid Howells 	server->mount_time = jiffies;
108854ceac45SDavid Howells 	return server;
108954ceac45SDavid Howells 
109054ceac45SDavid Howells error:
109154ceac45SDavid Howells 	nfs_free_server(server);
109254ceac45SDavid Howells 	return ERR_PTR(error);
109354ceac45SDavid Howells }
109454ceac45SDavid Howells 
109554ceac45SDavid Howells #ifdef CONFIG_NFS_V4
109654ceac45SDavid Howells /*
10979bdaa86dSBenny Halevy  * Initialize the NFS4 callback service
10989bdaa86dSBenny Halevy  */
10999bdaa86dSBenny Halevy static int nfs4_init_callback(struct nfs_client *clp)
11009bdaa86dSBenny Halevy {
11019bdaa86dSBenny Halevy 	int error;
11029bdaa86dSBenny Halevy 
11039bdaa86dSBenny Halevy 	if (clp->rpc_ops->version == 4) {
11040b5b7ae0SAndy Adamson 		if (nfs4_has_session(clp)) {
11050b5b7ae0SAndy Adamson 			error = xprt_setup_backchannel(
11060b5b7ae0SAndy Adamson 						clp->cl_rpcclient->cl_xprt,
11070b5b7ae0SAndy Adamson 						NFS41_BC_MIN_CALLBACKS);
11080b5b7ae0SAndy Adamson 			if (error < 0)
11090b5b7ae0SAndy Adamson 				return error;
11100b5b7ae0SAndy Adamson 		}
11110b5b7ae0SAndy Adamson 
111271468513SBenny Halevy 		error = nfs_callback_up(clp->cl_minorversion,
111371468513SBenny Halevy 					clp->cl_rpcclient->cl_xprt);
11149bdaa86dSBenny Halevy 		if (error < 0) {
11159bdaa86dSBenny Halevy 			dprintk("%s: failed to start callback. Error = %d\n",
11169bdaa86dSBenny Halevy 				__func__, error);
11179bdaa86dSBenny Halevy 			return error;
11189bdaa86dSBenny Halevy 		}
11199bdaa86dSBenny Halevy 		__set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
11209bdaa86dSBenny Halevy 	}
11219bdaa86dSBenny Halevy 	return 0;
11229bdaa86dSBenny Halevy }
11239bdaa86dSBenny Halevy 
11249bdaa86dSBenny Halevy /*
1125557134a3SAndy Adamson  * Initialize the minor version specific parts of an NFS4 client record
1126557134a3SAndy Adamson  */
1127557134a3SAndy Adamson static int nfs4_init_client_minor_version(struct nfs_client *clp)
1128557134a3SAndy Adamson {
1129cccef3b9SAndy Adamson 	clp->cl_call_sync = _nfs4_call_sync;
1130cccef3b9SAndy Adamson 
1131557134a3SAndy Adamson #if defined(CONFIG_NFS_V4_1)
1132557134a3SAndy Adamson 	if (clp->cl_minorversion) {
1133557134a3SAndy Adamson 		struct nfs4_session *session = NULL;
1134557134a3SAndy Adamson 		/*
1135557134a3SAndy Adamson 		 * Create the session and mark it expired.
1136557134a3SAndy Adamson 		 * When a SEQUENCE operation encounters the expired session
1137557134a3SAndy Adamson 		 * it will do session recovery to initialize it.
1138557134a3SAndy Adamson 		 */
1139557134a3SAndy Adamson 		session = nfs4_alloc_session(clp);
1140557134a3SAndy Adamson 		if (!session)
1141557134a3SAndy Adamson 			return -ENOMEM;
1142557134a3SAndy Adamson 
1143557134a3SAndy Adamson 		clp->cl_session = session;
1144cccef3b9SAndy Adamson 		clp->cl_call_sync = _nfs4_call_sync_session;
1145557134a3SAndy Adamson 	}
1146557134a3SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1147557134a3SAndy Adamson 
114871468513SBenny Halevy 	return nfs4_init_callback(clp);
1149557134a3SAndy Adamson }
1150557134a3SAndy Adamson 
1151557134a3SAndy Adamson /*
115254ceac45SDavid Howells  * Initialise an NFS4 client record
115354ceac45SDavid Howells  */
115454ceac45SDavid Howells static int nfs4_init_client(struct nfs_client *clp,
115533170233STrond Myklebust 		const struct rpc_timeout *timeparms,
11567d9ac06fSJ. Bruce Fields 		const char *ip_addr,
1157d740351bSChuck Lever 		rpc_authflavor_t authflavour,
1158d740351bSChuck Lever 		int flags)
115954ceac45SDavid Howells {
116054ceac45SDavid Howells 	int error;
116154ceac45SDavid Howells 
116254ceac45SDavid Howells 	if (clp->cl_cons_state == NFS_CS_READY) {
116354ceac45SDavid Howells 		/* the client is initialised already */
116454ceac45SDavid Howells 		dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
116554ceac45SDavid Howells 		return 0;
116654ceac45SDavid Howells 	}
116754ceac45SDavid Howells 
116854ceac45SDavid Howells 	/* Check NFS protocol revision and initialize RPC op vector */
116954ceac45SDavid Howells 	clp->rpc_ops = &nfs_v4_clientops;
117054ceac45SDavid Howells 
117159dca3b2STrond Myklebust 	error = nfs_create_rpc_client(clp, timeparms, authflavour,
1172d740351bSChuck Lever 				      1, flags & NFS_MOUNT_NORESVPORT);
117354ceac45SDavid Howells 	if (error < 0)
117454ceac45SDavid Howells 		goto error;
11757d9ac06fSJ. Bruce Fields 	memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
117654ceac45SDavid Howells 
117754ceac45SDavid Howells 	error = nfs_idmap_new(clp);
117854ceac45SDavid Howells 	if (error < 0) {
117954ceac45SDavid Howells 		dprintk("%s: failed to create idmapper. Error = %d\n",
11803110ff80SHarvey Harrison 			__func__, error);
118154ceac45SDavid Howells 		goto error;
118254ceac45SDavid Howells 	}
11839c5bf38dSTrond Myklebust 	__set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
118454ceac45SDavid Howells 
1185557134a3SAndy Adamson 	error = nfs4_init_client_minor_version(clp);
1186557134a3SAndy Adamson 	if (error < 0)
1187557134a3SAndy Adamson 		goto error;
1188557134a3SAndy Adamson 
118976db6d95SAndy Adamson 	if (!nfs4_has_session(clp))
119054ceac45SDavid Howells 		nfs_mark_client_ready(clp, NFS_CS_READY);
119154ceac45SDavid Howells 	return 0;
119254ceac45SDavid Howells 
119354ceac45SDavid Howells error:
119454ceac45SDavid Howells 	nfs_mark_client_ready(clp, error);
119554ceac45SDavid Howells 	dprintk("<-- nfs4_init_client() = xerror %d\n", error);
119654ceac45SDavid Howells 	return error;
119754ceac45SDavid Howells }
119854ceac45SDavid Howells 
119954ceac45SDavid Howells /*
120054ceac45SDavid Howells  * Set up an NFS4 client
120154ceac45SDavid Howells  */
120254ceac45SDavid Howells static int nfs4_set_client(struct nfs_server *server,
1203dcecae0fSChuck Lever 		const char *hostname,
1204dcecae0fSChuck Lever 		const struct sockaddr *addr,
1205dcecae0fSChuck Lever 		const size_t addrlen,
12067d9ac06fSJ. Bruce Fields 		const char *ip_addr,
120754ceac45SDavid Howells 		rpc_authflavor_t authflavour,
120894a417f3SBenny Halevy 		int proto, const struct rpc_timeout *timeparms,
120994a417f3SBenny Halevy 		u32 minorversion)
121054ceac45SDavid Howells {
12113a498026STrond Myklebust 	struct nfs_client_initdata cl_init = {
12123a498026STrond Myklebust 		.hostname = hostname,
1213dcecae0fSChuck Lever 		.addr = addr,
1214dcecae0fSChuck Lever 		.addrlen = addrlen,
121540c55319STrond Myklebust 		.rpc_ops = &nfs_v4_clientops,
121659dca3b2STrond Myklebust 		.proto = proto,
12175aae4a9aSBenny Halevy 		.minorversion = minorversion,
12183a498026STrond Myklebust 	};
121954ceac45SDavid Howells 	struct nfs_client *clp;
122054ceac45SDavid Howells 	int error;
122154ceac45SDavid Howells 
122254ceac45SDavid Howells 	dprintk("--> nfs4_set_client()\n");
122354ceac45SDavid Howells 
122454ceac45SDavid Howells 	/* Allocate or find a client reference we can use */
12253a498026STrond Myklebust 	clp = nfs_get_client(&cl_init);
122654ceac45SDavid Howells 	if (IS_ERR(clp)) {
122754ceac45SDavid Howells 		error = PTR_ERR(clp);
122854ceac45SDavid Howells 		goto error;
122954ceac45SDavid Howells 	}
1230d740351bSChuck Lever 	error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1231d740351bSChuck Lever 					server->flags);
123254ceac45SDavid Howells 	if (error < 0)
123354ceac45SDavid Howells 		goto error_put;
123454ceac45SDavid Howells 
123554ceac45SDavid Howells 	server->nfs_client = clp;
123654ceac45SDavid Howells 	dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
123754ceac45SDavid Howells 	return 0;
123854ceac45SDavid Howells 
123954ceac45SDavid Howells error_put:
124054ceac45SDavid Howells 	nfs_put_client(clp);
124154ceac45SDavid Howells error:
124254ceac45SDavid Howells 	dprintk("<-- nfs4_set_client() = xerror %d\n", error);
124354ceac45SDavid Howells 	return error;
124454ceac45SDavid Howells }
124554ceac45SDavid Howells 
1246557134a3SAndy Adamson 
1247557134a3SAndy Adamson /*
124896b09e02SAndy Adamson  * Session has been established, and the client marked ready.
124996b09e02SAndy Adamson  * Set the mount rsize and wsize with negotiated fore channel
125096b09e02SAndy Adamson  * attributes which will be bound checked in nfs_server_set_fsinfo.
125196b09e02SAndy Adamson  */
125296b09e02SAndy Adamson static void nfs4_session_set_rwsize(struct nfs_server *server)
125396b09e02SAndy Adamson {
125496b09e02SAndy Adamson #ifdef CONFIG_NFS_V4_1
125596b09e02SAndy Adamson 	if (!nfs4_has_session(server->nfs_client))
125696b09e02SAndy Adamson 		return;
125796b09e02SAndy Adamson 	server->rsize = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
125896b09e02SAndy Adamson 	server->wsize = server->nfs_client->cl_session->fc_attrs.max_rqst_sz;
125996b09e02SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
126096b09e02SAndy Adamson }
126196b09e02SAndy Adamson 
126296b09e02SAndy Adamson /*
126354ceac45SDavid Howells  * Create a version 4 volume record
126454ceac45SDavid Howells  */
126554ceac45SDavid Howells static int nfs4_init_server(struct nfs_server *server,
126691ea40b9S\"Talpey, Thomas\ 		const struct nfs_parsed_mount_data *data)
126754ceac45SDavid Howells {
126833170233STrond Myklebust 	struct rpc_timeout timeparms;
126954ceac45SDavid Howells 	int error;
127054ceac45SDavid Howells 
127154ceac45SDavid Howells 	dprintk("--> nfs4_init_server()\n");
127254ceac45SDavid Howells 
127333170233STrond Myklebust 	nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
127433170233STrond Myklebust 			data->timeo, data->retrans);
127533170233STrond Myklebust 
1276542fcc33SChuck Lever 	/* Initialise the client representation from the mount data */
1277542fcc33SChuck Lever 	server->flags = data->flags;
1278542fcc33SChuck Lever 	server->caps |= NFS_CAP_ATOMIC_OPEN;
1279b797cac7SDavid Howells 	server->options = data->options;
1280542fcc33SChuck Lever 
128133170233STrond Myklebust 	/* Get a client record */
128233170233STrond Myklebust 	error = nfs4_set_client(server,
128333170233STrond Myklebust 			data->nfs_server.hostname,
128433170233STrond Myklebust 			(const struct sockaddr *)&data->nfs_server.address,
128533170233STrond Myklebust 			data->nfs_server.addrlen,
128633170233STrond Myklebust 			data->client_address,
128733170233STrond Myklebust 			data->auth_flavors[0],
128833170233STrond Myklebust 			data->nfs_server.protocol,
128994a417f3SBenny Halevy 			&timeparms,
129094a417f3SBenny Halevy 			data->minorversion);
129133170233STrond Myklebust 	if (error < 0)
129233170233STrond Myklebust 		goto error;
129333170233STrond Myklebust 
129454ceac45SDavid Howells 	if (data->rsize)
129554ceac45SDavid Howells 		server->rsize = nfs_block_size(data->rsize, NULL);
129654ceac45SDavid Howells 	if (data->wsize)
129754ceac45SDavid Howells 		server->wsize = nfs_block_size(data->wsize, NULL);
129854ceac45SDavid Howells 
129954ceac45SDavid Howells 	server->acregmin = data->acregmin * HZ;
130054ceac45SDavid Howells 	server->acregmax = data->acregmax * HZ;
130154ceac45SDavid Howells 	server->acdirmin = data->acdirmin * HZ;
130254ceac45SDavid Howells 	server->acdirmax = data->acdirmax * HZ;
130354ceac45SDavid Howells 
1304f22d6d79SChuck Lever 	server->port = data->nfs_server.port;
1305f22d6d79SChuck Lever 
130633170233STrond Myklebust 	error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
130754ceac45SDavid Howells 
130833170233STrond Myklebust error:
130954ceac45SDavid Howells 	/* Done */
131054ceac45SDavid Howells 	dprintk("<-- nfs4_init_server() = %d\n", error);
131154ceac45SDavid Howells 	return error;
131254ceac45SDavid Howells }
131354ceac45SDavid Howells 
131454ceac45SDavid Howells /*
131554ceac45SDavid Howells  * Create a version 4 volume record
131654ceac45SDavid Howells  * - keyed on server and FSID
131754ceac45SDavid Howells  */
131891ea40b9S\"Talpey, Thomas\ struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
131954ceac45SDavid Howells 				      struct nfs_fh *mntfh)
132054ceac45SDavid Howells {
132154ceac45SDavid Howells 	struct nfs_fattr fattr;
132254ceac45SDavid Howells 	struct nfs_server *server;
132354ceac45SDavid Howells 	int error;
132454ceac45SDavid Howells 
132554ceac45SDavid Howells 	dprintk("--> nfs4_create_server()\n");
132654ceac45SDavid Howells 
132754ceac45SDavid Howells 	server = nfs_alloc_server();
132854ceac45SDavid Howells 	if (!server)
132954ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
133054ceac45SDavid Howells 
133154ceac45SDavid Howells 	/* set up the general RPC client */
133291ea40b9S\"Talpey, Thomas\ 	error = nfs4_init_server(server, data);
133354ceac45SDavid Howells 	if (error < 0)
133454ceac45SDavid Howells 		goto error;
133554ceac45SDavid Howells 
133654ceac45SDavid Howells 	BUG_ON(!server->nfs_client);
133754ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops);
133854ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
133954ceac45SDavid Howells 
1340fccba804STrond Myklebust 	error = nfs4_init_session(server);
1341fccba804STrond Myklebust 	if (error < 0)
1342fccba804STrond Myklebust 		goto error;
1343557134a3SAndy Adamson 
134454ceac45SDavid Howells 	/* Probe the root fh to retrieve its FSID */
134591ea40b9S\"Talpey, Thomas\ 	error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
134654ceac45SDavid Howells 	if (error < 0)
134754ceac45SDavid Howells 		goto error;
134854ceac45SDavid Howells 
13496daabf1bSDavid Howells 	dprintk("Server FSID: %llx:%llx\n",
13506daabf1bSDavid Howells 		(unsigned long long) server->fsid.major,
13516daabf1bSDavid Howells 		(unsigned long long) server->fsid.minor);
135254ceac45SDavid Howells 	dprintk("Mount FH: %d\n", mntfh->size);
135354ceac45SDavid Howells 
135496b09e02SAndy Adamson 	nfs4_session_set_rwsize(server);
135596b09e02SAndy Adamson 
135654ceac45SDavid Howells 	error = nfs_probe_fsinfo(server, mntfh, &fattr);
135754ceac45SDavid Howells 	if (error < 0)
135854ceac45SDavid Howells 		goto error;
135954ceac45SDavid Howells 
136054af3bb5STrond Myklebust 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
136154af3bb5STrond Myklebust 		server->namelen = NFS4_MAXNAMLEN;
136254af3bb5STrond Myklebust 
136354ceac45SDavid Howells 	BUG_ON(!server->nfs_client);
136454ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops);
136554ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
136654ceac45SDavid Howells 
136754ceac45SDavid Howells 	spin_lock(&nfs_client_lock);
136854ceac45SDavid Howells 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
136954ceac45SDavid Howells 	list_add_tail(&server->master_link, &nfs_volume_list);
137054ceac45SDavid Howells 	spin_unlock(&nfs_client_lock);
137154ceac45SDavid Howells 
137254ceac45SDavid Howells 	server->mount_time = jiffies;
137354ceac45SDavid Howells 	dprintk("<-- nfs4_create_server() = %p\n", server);
137454ceac45SDavid Howells 	return server;
137554ceac45SDavid Howells 
137654ceac45SDavid Howells error:
137754ceac45SDavid Howells 	nfs_free_server(server);
137854ceac45SDavid Howells 	dprintk("<-- nfs4_create_server() = error %d\n", error);
137954ceac45SDavid Howells 	return ERR_PTR(error);
138054ceac45SDavid Howells }
138154ceac45SDavid Howells 
138254ceac45SDavid Howells /*
138354ceac45SDavid Howells  * Create an NFS4 referral server record
138454ceac45SDavid Howells  */
138554ceac45SDavid Howells struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1386f2d0d85eSTrond Myklebust 					       struct nfs_fh *mntfh)
138754ceac45SDavid Howells {
138854ceac45SDavid Howells 	struct nfs_client *parent_client;
138954ceac45SDavid Howells 	struct nfs_server *server, *parent_server;
139054ceac45SDavid Howells 	struct nfs_fattr fattr;
139154ceac45SDavid Howells 	int error;
139254ceac45SDavid Howells 
139354ceac45SDavid Howells 	dprintk("--> nfs4_create_referral_server()\n");
139454ceac45SDavid Howells 
139554ceac45SDavid Howells 	server = nfs_alloc_server();
139654ceac45SDavid Howells 	if (!server)
139754ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
139854ceac45SDavid Howells 
139954ceac45SDavid Howells 	parent_server = NFS_SB(data->sb);
140054ceac45SDavid Howells 	parent_client = parent_server->nfs_client;
140154ceac45SDavid Howells 
1402542fcc33SChuck Lever 	/* Initialise the client representation from the parent server */
1403542fcc33SChuck Lever 	nfs_server_copy_userdata(server, parent_server);
1404542fcc33SChuck Lever 	server->caps |= NFS_CAP_ATOMIC_OPEN;
1405542fcc33SChuck Lever 
140654ceac45SDavid Howells 	/* Get a client representation.
140754ceac45SDavid Howells 	 * Note: NFSv4 always uses TCP, */
1408dcecae0fSChuck Lever 	error = nfs4_set_client(server, data->hostname,
14096677d095SChuck Lever 				data->addr,
14106677d095SChuck Lever 				data->addrlen,
14117d9ac06fSJ. Bruce Fields 				parent_client->cl_ipaddr,
141254ceac45SDavid Howells 				data->authflavor,
141354ceac45SDavid Howells 				parent_server->client->cl_xprt->prot,
141494a417f3SBenny Halevy 				parent_server->client->cl_timeout,
141594a417f3SBenny Halevy 				parent_client->cl_minorversion);
1416297de4f6Sandros@citi.umich.edu 	if (error < 0)
1417297de4f6Sandros@citi.umich.edu 		goto error;
141854ceac45SDavid Howells 
141933170233STrond Myklebust 	error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
142054ceac45SDavid Howells 	if (error < 0)
142154ceac45SDavid Howells 		goto error;
142254ceac45SDavid Howells 
142354ceac45SDavid Howells 	BUG_ON(!server->nfs_client);
142454ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops);
142554ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
142654ceac45SDavid Howells 
1427f2d0d85eSTrond Myklebust 	/* Probe the root fh to retrieve its FSID and filehandle */
1428f2d0d85eSTrond Myklebust 	error = nfs4_path_walk(server, mntfh, data->mnt_path);
1429f2d0d85eSTrond Myklebust 	if (error < 0)
1430f2d0d85eSTrond Myklebust 		goto error;
1431f2d0d85eSTrond Myklebust 
143254ceac45SDavid Howells 	/* probe the filesystem info for this server filesystem */
1433f2d0d85eSTrond Myklebust 	error = nfs_probe_fsinfo(server, mntfh, &fattr);
143454ceac45SDavid Howells 	if (error < 0)
143554ceac45SDavid Howells 		goto error;
143654ceac45SDavid Howells 
143754af3bb5STrond Myklebust 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
143854af3bb5STrond Myklebust 		server->namelen = NFS4_MAXNAMLEN;
143954af3bb5STrond Myklebust 
144054ceac45SDavid Howells 	dprintk("Referral FSID: %llx:%llx\n",
14416daabf1bSDavid Howells 		(unsigned long long) server->fsid.major,
14426daabf1bSDavid Howells 		(unsigned long long) server->fsid.minor);
144354ceac45SDavid Howells 
144454ceac45SDavid Howells 	spin_lock(&nfs_client_lock);
144554ceac45SDavid Howells 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
144654ceac45SDavid Howells 	list_add_tail(&server->master_link, &nfs_volume_list);
144754ceac45SDavid Howells 	spin_unlock(&nfs_client_lock);
144854ceac45SDavid Howells 
144954ceac45SDavid Howells 	server->mount_time = jiffies;
145054ceac45SDavid Howells 
145154ceac45SDavid Howells 	dprintk("<-- nfs_create_referral_server() = %p\n", server);
145254ceac45SDavid Howells 	return server;
145354ceac45SDavid Howells 
145454ceac45SDavid Howells error:
145554ceac45SDavid Howells 	nfs_free_server(server);
145654ceac45SDavid Howells 	dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
145754ceac45SDavid Howells 	return ERR_PTR(error);
145854ceac45SDavid Howells }
145954ceac45SDavid Howells 
146054ceac45SDavid Howells #endif /* CONFIG_NFS_V4 */
146154ceac45SDavid Howells 
146254ceac45SDavid Howells /*
146354ceac45SDavid Howells  * Clone an NFS2, NFS3 or NFS4 server record
146454ceac45SDavid Howells  */
146554ceac45SDavid Howells struct nfs_server *nfs_clone_server(struct nfs_server *source,
146654ceac45SDavid Howells 				    struct nfs_fh *fh,
146754ceac45SDavid Howells 				    struct nfs_fattr *fattr)
146854ceac45SDavid Howells {
146954ceac45SDavid Howells 	struct nfs_server *server;
147054ceac45SDavid Howells 	struct nfs_fattr fattr_fsinfo;
147154ceac45SDavid Howells 	int error;
147254ceac45SDavid Howells 
147354ceac45SDavid Howells 	dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
14746daabf1bSDavid Howells 		(unsigned long long) fattr->fsid.major,
14756daabf1bSDavid Howells 		(unsigned long long) fattr->fsid.minor);
147654ceac45SDavid Howells 
147754ceac45SDavid Howells 	server = nfs_alloc_server();
147854ceac45SDavid Howells 	if (!server)
147954ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
148054ceac45SDavid Howells 
148154ceac45SDavid Howells 	/* Copy data from the source */
148254ceac45SDavid Howells 	server->nfs_client = source->nfs_client;
148354ceac45SDavid Howells 	atomic_inc(&server->nfs_client->cl_count);
148454ceac45SDavid Howells 	nfs_server_copy_userdata(server, source);
148554ceac45SDavid Howells 
148654ceac45SDavid Howells 	server->fsid = fattr->fsid;
148754ceac45SDavid Howells 
148833170233STrond Myklebust 	error = nfs_init_server_rpcclient(server,
148933170233STrond Myklebust 			source->client->cl_timeout,
149033170233STrond Myklebust 			source->client->cl_auth->au_flavor);
149154ceac45SDavid Howells 	if (error < 0)
149254ceac45SDavid Howells 		goto out_free_server;
149354ceac45SDavid Howells 	if (!IS_ERR(source->client_acl))
149454ceac45SDavid Howells 		nfs_init_server_aclclient(server);
149554ceac45SDavid Howells 
149654ceac45SDavid Howells 	/* probe the filesystem info for this server filesystem */
149754ceac45SDavid Howells 	error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
149854ceac45SDavid Howells 	if (error < 0)
149954ceac45SDavid Howells 		goto out_free_server;
150054ceac45SDavid Howells 
150154af3bb5STrond Myklebust 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
150254af3bb5STrond Myklebust 		server->namelen = NFS4_MAXNAMLEN;
150354af3bb5STrond Myklebust 
150454ceac45SDavid Howells 	dprintk("Cloned FSID: %llx:%llx\n",
15056daabf1bSDavid Howells 		(unsigned long long) server->fsid.major,
15066daabf1bSDavid Howells 		(unsigned long long) server->fsid.minor);
150754ceac45SDavid Howells 
150854ceac45SDavid Howells 	error = nfs_start_lockd(server);
150954ceac45SDavid Howells 	if (error < 0)
151054ceac45SDavid Howells 		goto out_free_server;
151154ceac45SDavid Howells 
151254ceac45SDavid Howells 	spin_lock(&nfs_client_lock);
151354ceac45SDavid Howells 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
151454ceac45SDavid Howells 	list_add_tail(&server->master_link, &nfs_volume_list);
151554ceac45SDavid Howells 	spin_unlock(&nfs_client_lock);
151654ceac45SDavid Howells 
151754ceac45SDavid Howells 	server->mount_time = jiffies;
151854ceac45SDavid Howells 
151954ceac45SDavid Howells 	dprintk("<-- nfs_clone_server() = %p\n", server);
152054ceac45SDavid Howells 	return server;
152154ceac45SDavid Howells 
152254ceac45SDavid Howells out_free_server:
152354ceac45SDavid Howells 	nfs_free_server(server);
152454ceac45SDavid Howells 	dprintk("<-- nfs_clone_server() = error %d\n", error);
152554ceac45SDavid Howells 	return ERR_PTR(error);
152654ceac45SDavid Howells }
15276aaca566SDavid Howells 
15286aaca566SDavid Howells #ifdef CONFIG_PROC_FS
15296aaca566SDavid Howells static struct proc_dir_entry *proc_fs_nfs;
15306aaca566SDavid Howells 
15316aaca566SDavid Howells static int nfs_server_list_open(struct inode *inode, struct file *file);
15326aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
15336aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
15346aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v);
15356aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v);
15366aaca566SDavid Howells 
15376aaca566SDavid Howells static struct seq_operations nfs_server_list_ops = {
15386aaca566SDavid Howells 	.start	= nfs_server_list_start,
15396aaca566SDavid Howells 	.next	= nfs_server_list_next,
15406aaca566SDavid Howells 	.stop	= nfs_server_list_stop,
15416aaca566SDavid Howells 	.show	= nfs_server_list_show,
15426aaca566SDavid Howells };
15436aaca566SDavid Howells 
154400977a59SArjan van de Ven static const struct file_operations nfs_server_list_fops = {
15456aaca566SDavid Howells 	.open		= nfs_server_list_open,
15466aaca566SDavid Howells 	.read		= seq_read,
15476aaca566SDavid Howells 	.llseek		= seq_lseek,
15486aaca566SDavid Howells 	.release	= seq_release,
154934b37235SDenis V. Lunev 	.owner		= THIS_MODULE,
15506aaca566SDavid Howells };
15516aaca566SDavid Howells 
15526aaca566SDavid Howells static int nfs_volume_list_open(struct inode *inode, struct file *file);
15536aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
15546aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
15556aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v);
15566aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v);
15576aaca566SDavid Howells 
15586aaca566SDavid Howells static struct seq_operations nfs_volume_list_ops = {
15596aaca566SDavid Howells 	.start	= nfs_volume_list_start,
15606aaca566SDavid Howells 	.next	= nfs_volume_list_next,
15616aaca566SDavid Howells 	.stop	= nfs_volume_list_stop,
15626aaca566SDavid Howells 	.show	= nfs_volume_list_show,
15636aaca566SDavid Howells };
15646aaca566SDavid Howells 
156500977a59SArjan van de Ven static const struct file_operations nfs_volume_list_fops = {
15666aaca566SDavid Howells 	.open		= nfs_volume_list_open,
15676aaca566SDavid Howells 	.read		= seq_read,
15686aaca566SDavid Howells 	.llseek		= seq_lseek,
15696aaca566SDavid Howells 	.release	= seq_release,
157034b37235SDenis V. Lunev 	.owner		= THIS_MODULE,
15716aaca566SDavid Howells };
15726aaca566SDavid Howells 
15736aaca566SDavid Howells /*
15746aaca566SDavid Howells  * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
15756aaca566SDavid Howells  * we're dealing
15766aaca566SDavid Howells  */
15776aaca566SDavid Howells static int nfs_server_list_open(struct inode *inode, struct file *file)
15786aaca566SDavid Howells {
15796aaca566SDavid Howells 	struct seq_file *m;
15806aaca566SDavid Howells 	int ret;
15816aaca566SDavid Howells 
15826aaca566SDavid Howells 	ret = seq_open(file, &nfs_server_list_ops);
15836aaca566SDavid Howells 	if (ret < 0)
15846aaca566SDavid Howells 		return ret;
15856aaca566SDavid Howells 
15866aaca566SDavid Howells 	m = file->private_data;
15876aaca566SDavid Howells 	m->private = PDE(inode)->data;
15886aaca566SDavid Howells 
15896aaca566SDavid Howells 	return 0;
15906aaca566SDavid Howells }
15916aaca566SDavid Howells 
15926aaca566SDavid Howells /*
15936aaca566SDavid Howells  * set up the iterator to start reading from the server list and return the first item
15946aaca566SDavid Howells  */
15956aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
15966aaca566SDavid Howells {
15976aaca566SDavid Howells 	/* lock the list against modification */
15986aaca566SDavid Howells 	spin_lock(&nfs_client_lock);
1599259902eaSPavel Emelianov 	return seq_list_start_head(&nfs_client_list, *_pos);
16006aaca566SDavid Howells }
16016aaca566SDavid Howells 
16026aaca566SDavid Howells /*
16036aaca566SDavid Howells  * move to next server
16046aaca566SDavid Howells  */
16056aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
16066aaca566SDavid Howells {
1607259902eaSPavel Emelianov 	return seq_list_next(v, &nfs_client_list, pos);
16086aaca566SDavid Howells }
16096aaca566SDavid Howells 
16106aaca566SDavid Howells /*
16116aaca566SDavid Howells  * clean up after reading from the transports list
16126aaca566SDavid Howells  */
16136aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v)
16146aaca566SDavid Howells {
16156aaca566SDavid Howells 	spin_unlock(&nfs_client_lock);
16166aaca566SDavid Howells }
16176aaca566SDavid Howells 
16186aaca566SDavid Howells /*
16196aaca566SDavid Howells  * display a header line followed by a load of call lines
16206aaca566SDavid Howells  */
16216aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v)
16226aaca566SDavid Howells {
16236aaca566SDavid Howells 	struct nfs_client *clp;
16246aaca566SDavid Howells 
16256aaca566SDavid Howells 	/* display header on line 1 */
1626259902eaSPavel Emelianov 	if (v == &nfs_client_list) {
16276aaca566SDavid Howells 		seq_puts(m, "NV SERVER   PORT USE HOSTNAME\n");
16286aaca566SDavid Howells 		return 0;
16296aaca566SDavid Howells 	}
16306aaca566SDavid Howells 
16316aaca566SDavid Howells 	/* display one transport per line on subsequent lines */
16326aaca566SDavid Howells 	clp = list_entry(v, struct nfs_client, cl_share_link);
16336aaca566SDavid Howells 
16345d8515caSChuck Lever 	seq_printf(m, "v%u %s %s %3d %s\n",
163540c55319STrond Myklebust 		   clp->rpc_ops->version,
16365d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
16375d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
16386aaca566SDavid Howells 		   atomic_read(&clp->cl_count),
16396aaca566SDavid Howells 		   clp->cl_hostname);
16406aaca566SDavid Howells 
16416aaca566SDavid Howells 	return 0;
16426aaca566SDavid Howells }
16436aaca566SDavid Howells 
16446aaca566SDavid Howells /*
16456aaca566SDavid Howells  * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
16466aaca566SDavid Howells  */
16476aaca566SDavid Howells static int nfs_volume_list_open(struct inode *inode, struct file *file)
16486aaca566SDavid Howells {
16496aaca566SDavid Howells 	struct seq_file *m;
16506aaca566SDavid Howells 	int ret;
16516aaca566SDavid Howells 
16526aaca566SDavid Howells 	ret = seq_open(file, &nfs_volume_list_ops);
16536aaca566SDavid Howells 	if (ret < 0)
16546aaca566SDavid Howells 		return ret;
16556aaca566SDavid Howells 
16566aaca566SDavid Howells 	m = file->private_data;
16576aaca566SDavid Howells 	m->private = PDE(inode)->data;
16586aaca566SDavid Howells 
16596aaca566SDavid Howells 	return 0;
16606aaca566SDavid Howells }
16616aaca566SDavid Howells 
16626aaca566SDavid Howells /*
16636aaca566SDavid Howells  * set up the iterator to start reading from the volume list and return the first item
16646aaca566SDavid Howells  */
16656aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
16666aaca566SDavid Howells {
16676aaca566SDavid Howells 	/* lock the list against modification */
16686aaca566SDavid Howells 	spin_lock(&nfs_client_lock);
1669259902eaSPavel Emelianov 	return seq_list_start_head(&nfs_volume_list, *_pos);
16706aaca566SDavid Howells }
16716aaca566SDavid Howells 
16726aaca566SDavid Howells /*
16736aaca566SDavid Howells  * move to next volume
16746aaca566SDavid Howells  */
16756aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
16766aaca566SDavid Howells {
1677259902eaSPavel Emelianov 	return seq_list_next(v, &nfs_volume_list, pos);
16786aaca566SDavid Howells }
16796aaca566SDavid Howells 
16806aaca566SDavid Howells /*
16816aaca566SDavid Howells  * clean up after reading from the transports list
16826aaca566SDavid Howells  */
16836aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v)
16846aaca566SDavid Howells {
16856aaca566SDavid Howells 	spin_unlock(&nfs_client_lock);
16866aaca566SDavid Howells }
16876aaca566SDavid Howells 
16886aaca566SDavid Howells /*
16896aaca566SDavid Howells  * display a header line followed by a load of call lines
16906aaca566SDavid Howells  */
16916aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v)
16926aaca566SDavid Howells {
16936aaca566SDavid Howells 	struct nfs_server *server;
16946aaca566SDavid Howells 	struct nfs_client *clp;
16956aaca566SDavid Howells 	char dev[8], fsid[17];
16966aaca566SDavid Howells 
16976aaca566SDavid Howells 	/* display header on line 1 */
1698259902eaSPavel Emelianov 	if (v == &nfs_volume_list) {
16995d1acff1SDavid Howells 		seq_puts(m, "NV SERVER   PORT DEV     FSID              FSC\n");
17006aaca566SDavid Howells 		return 0;
17016aaca566SDavid Howells 	}
17026aaca566SDavid Howells 	/* display one transport per line on subsequent lines */
17036aaca566SDavid Howells 	server = list_entry(v, struct nfs_server, master_link);
17046aaca566SDavid Howells 	clp = server->nfs_client;
17056aaca566SDavid Howells 
17066aaca566SDavid Howells 	snprintf(dev, 8, "%u:%u",
17076aaca566SDavid Howells 		 MAJOR(server->s_dev), MINOR(server->s_dev));
17086aaca566SDavid Howells 
17096aaca566SDavid Howells 	snprintf(fsid, 17, "%llx:%llx",
17106daabf1bSDavid Howells 		 (unsigned long long) server->fsid.major,
17116daabf1bSDavid Howells 		 (unsigned long long) server->fsid.minor);
17126aaca566SDavid Howells 
17135d1acff1SDavid Howells 	seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
171440c55319STrond Myklebust 		   clp->rpc_ops->version,
17155d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
17165d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
17176aaca566SDavid Howells 		   dev,
17185d1acff1SDavid Howells 		   fsid,
17195d1acff1SDavid Howells 		   nfs_server_fscache_state(server));
17206aaca566SDavid Howells 
17216aaca566SDavid Howells 	return 0;
17226aaca566SDavid Howells }
17236aaca566SDavid Howells 
17246aaca566SDavid Howells /*
17256aaca566SDavid Howells  * initialise the /proc/fs/nfsfs/ directory
17266aaca566SDavid Howells  */
17276aaca566SDavid Howells int __init nfs_fs_proc_init(void)
17286aaca566SDavid Howells {
17296aaca566SDavid Howells 	struct proc_dir_entry *p;
17306aaca566SDavid Howells 
173136a5aeb8SAlexey Dobriyan 	proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
17326aaca566SDavid Howells 	if (!proc_fs_nfs)
17336aaca566SDavid Howells 		goto error_0;
17346aaca566SDavid Howells 
17356aaca566SDavid Howells 	/* a file of servers with which we're dealing */
173634b37235SDenis V. Lunev 	p = proc_create("servers", S_IFREG|S_IRUGO,
173734b37235SDenis V. Lunev 			proc_fs_nfs, &nfs_server_list_fops);
17386aaca566SDavid Howells 	if (!p)
17396aaca566SDavid Howells 		goto error_1;
17406aaca566SDavid Howells 
17416aaca566SDavid Howells 	/* a file of volumes that we have mounted */
174234b37235SDenis V. Lunev 	p = proc_create("volumes", S_IFREG|S_IRUGO,
174334b37235SDenis V. Lunev 			proc_fs_nfs, &nfs_volume_list_fops);
17446aaca566SDavid Howells 	if (!p)
17456aaca566SDavid Howells 		goto error_2;
17466aaca566SDavid Howells 	return 0;
17476aaca566SDavid Howells 
17486aaca566SDavid Howells error_2:
17496aaca566SDavid Howells 	remove_proc_entry("servers", proc_fs_nfs);
17506aaca566SDavid Howells error_1:
175136a5aeb8SAlexey Dobriyan 	remove_proc_entry("fs/nfsfs", NULL);
17526aaca566SDavid Howells error_0:
17536aaca566SDavid Howells 	return -ENOMEM;
17546aaca566SDavid Howells }
17556aaca566SDavid Howells 
17566aaca566SDavid Howells /*
17576aaca566SDavid Howells  * clean up the /proc/fs/nfsfs/ directory
17586aaca566SDavid Howells  */
17596aaca566SDavid Howells void nfs_fs_proc_exit(void)
17606aaca566SDavid Howells {
17616aaca566SDavid Howells 	remove_proc_entry("volumes", proc_fs_nfs);
17626aaca566SDavid Howells 	remove_proc_entry("servers", proc_fs_nfs);
176336a5aeb8SAlexey Dobriyan 	remove_proc_entry("fs/nfsfs", NULL);
17646aaca566SDavid Howells }
17656aaca566SDavid Howells 
17666aaca566SDavid Howells #endif /* CONFIG_PROC_FS */
1767