xref: /linux/fs/nfs/client.c (revision 78fe0f41d9937ee62817912ac8d627e06243c269)
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>
385a0e3ad6STejun Heo #include <linux/slab.h>
393b0d3f93STrond Myklebust #include <net/ipv6.h>
4024c8dbbbSDavid Howells #include <linux/nfs_xdr.h>
410b5b7ae0SAndy Adamson #include <linux/sunrpc/bc_xprt.h>
4224c8dbbbSDavid Howells 
4324c8dbbbSDavid Howells #include <asm/system.h>
4424c8dbbbSDavid Howells 
4524c8dbbbSDavid Howells #include "nfs4_fs.h"
4624c8dbbbSDavid Howells #include "callback.h"
4724c8dbbbSDavid Howells #include "delegation.h"
4824c8dbbbSDavid Howells #include "iostat.h"
4924c8dbbbSDavid Howells #include "internal.h"
5014727281SDavid Howells #include "fscache.h"
5185e174baSRicardo Labiaga #include "pnfs.h"
5224c8dbbbSDavid Howells 
5324c8dbbbSDavid Howells #define NFSDBG_FACILITY		NFSDBG_CLIENT
5424c8dbbbSDavid Howells 
5524c8dbbbSDavid Howells static DEFINE_SPINLOCK(nfs_client_lock);
5624c8dbbbSDavid Howells static LIST_HEAD(nfs_client_list);
5754ceac45SDavid Howells static LIST_HEAD(nfs_volume_list);
5824c8dbbbSDavid Howells static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
59f4eecd5dSAndy Adamson #ifdef CONFIG_NFS_V4
60f4eecd5dSAndy Adamson static DEFINE_IDR(cb_ident_idr); /* Protected by nfs_client_lock */
61f4eecd5dSAndy Adamson 
62f4eecd5dSAndy Adamson /*
63f4eecd5dSAndy Adamson  * Get a unique NFSv4.0 callback identifier which will be used
64f4eecd5dSAndy Adamson  * by the V4.0 callback service to lookup the nfs_client struct
65f4eecd5dSAndy Adamson  */
66f4eecd5dSAndy Adamson static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion)
67f4eecd5dSAndy Adamson {
68f4eecd5dSAndy Adamson 	int ret = 0;
69f4eecd5dSAndy Adamson 
70f4eecd5dSAndy Adamson 	if (clp->rpc_ops->version != 4 || minorversion != 0)
71f4eecd5dSAndy Adamson 		return ret;
72f4eecd5dSAndy Adamson retry:
73f4eecd5dSAndy Adamson 	if (!idr_pre_get(&cb_ident_idr, GFP_KERNEL))
74f4eecd5dSAndy Adamson 		return -ENOMEM;
75f4eecd5dSAndy Adamson 	spin_lock(&nfs_client_lock);
76f4eecd5dSAndy Adamson 	ret = idr_get_new(&cb_ident_idr, clp, &clp->cl_cb_ident);
77f4eecd5dSAndy Adamson 	spin_unlock(&nfs_client_lock);
78f4eecd5dSAndy Adamson 	if (ret == -EAGAIN)
79f4eecd5dSAndy Adamson 		goto retry;
80f4eecd5dSAndy Adamson 	return ret;
81f4eecd5dSAndy Adamson }
82f4eecd5dSAndy Adamson #endif /* CONFIG_NFS_V4 */
8324c8dbbbSDavid Howells 
8424c8dbbbSDavid Howells /*
85b064eca2STrond Myklebust  * Turn off NFSv4 uid/gid mapping when using AUTH_SYS
86b064eca2STrond Myklebust  */
87b064eca2STrond Myklebust static int nfs4_disable_idmapping = 0;
88b064eca2STrond Myklebust 
89b064eca2STrond Myklebust /*
905006a76cSDavid Howells  * RPC cruft for NFS
915006a76cSDavid Howells  */
925006a76cSDavid Howells static struct rpc_version *nfs_version[5] = {
935006a76cSDavid Howells 	[2]			= &nfs_version2,
945006a76cSDavid Howells #ifdef CONFIG_NFS_V3
955006a76cSDavid Howells 	[3]			= &nfs_version3,
965006a76cSDavid Howells #endif
975006a76cSDavid Howells #ifdef CONFIG_NFS_V4
985006a76cSDavid Howells 	[4]			= &nfs_version4,
995006a76cSDavid Howells #endif
1005006a76cSDavid Howells };
1015006a76cSDavid Howells 
1025006a76cSDavid Howells struct rpc_program nfs_program = {
1035006a76cSDavid Howells 	.name			= "nfs",
1045006a76cSDavid Howells 	.number			= NFS_PROGRAM,
1055006a76cSDavid Howells 	.nrvers			= ARRAY_SIZE(nfs_version),
1065006a76cSDavid Howells 	.version		= nfs_version,
1075006a76cSDavid Howells 	.stats			= &nfs_rpcstat,
1085006a76cSDavid Howells 	.pipe_dir_name		= "/nfs",
1095006a76cSDavid Howells };
1105006a76cSDavid Howells 
1115006a76cSDavid Howells struct rpc_stat nfs_rpcstat = {
1125006a76cSDavid Howells 	.program		= &nfs_program
1135006a76cSDavid Howells };
1145006a76cSDavid Howells 
1155006a76cSDavid Howells 
1165006a76cSDavid Howells #ifdef CONFIG_NFS_V3_ACL
1175006a76cSDavid Howells static struct rpc_stat		nfsacl_rpcstat = { &nfsacl_program };
1185006a76cSDavid Howells static struct rpc_version *	nfsacl_version[] = {
1195006a76cSDavid Howells 	[3]			= &nfsacl_version3,
1205006a76cSDavid Howells };
1215006a76cSDavid Howells 
1225006a76cSDavid Howells struct rpc_program		nfsacl_program = {
1235006a76cSDavid Howells 	.name			= "nfsacl",
1245006a76cSDavid Howells 	.number			= NFS_ACL_PROGRAM,
1255006a76cSDavid Howells 	.nrvers			= ARRAY_SIZE(nfsacl_version),
1265006a76cSDavid Howells 	.version		= nfsacl_version,
1275006a76cSDavid Howells 	.stats			= &nfsacl_rpcstat,
1285006a76cSDavid Howells };
1295006a76cSDavid Howells #endif  /* CONFIG_NFS_V3_ACL */
1305006a76cSDavid Howells 
1313a498026STrond Myklebust struct nfs_client_initdata {
1323a498026STrond Myklebust 	const char *hostname;
133d7422c47SChuck Lever 	const struct sockaddr *addr;
1346e4cffd7SChuck Lever 	size_t addrlen;
13540c55319STrond Myklebust 	const struct nfs_rpc_ops *rpc_ops;
13659dca3b2STrond Myklebust 	int proto;
1375aae4a9aSBenny Halevy 	u32 minorversion;
1383a498026STrond Myklebust };
1393a498026STrond Myklebust 
1405006a76cSDavid Howells /*
14124c8dbbbSDavid Howells  * Allocate a shared client record
14224c8dbbbSDavid Howells  *
14324c8dbbbSDavid Howells  * Since these are allocated/deallocated very rarely, we don't
14424c8dbbbSDavid Howells  * bother putting them in a slab cache...
14524c8dbbbSDavid Howells  */
1463a498026STrond Myklebust static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
14724c8dbbbSDavid Howells {
14824c8dbbbSDavid Howells 	struct nfs_client *clp;
1497c67db3aSTrond Myklebust 	struct rpc_cred *cred;
150a21bdd9bSChuck Lever 	int err = -ENOMEM;
15124c8dbbbSDavid Howells 
15224c8dbbbSDavid Howells 	if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
15324c8dbbbSDavid Howells 		goto error_0;
15424c8dbbbSDavid Howells 
15540c55319STrond Myklebust 	clp->rpc_ops = cl_init->rpc_ops;
15640c55319STrond Myklebust 
15724c8dbbbSDavid Howells 	atomic_set(&clp->cl_count, 1);
15824c8dbbbSDavid Howells 	clp->cl_cons_state = NFS_CS_INITING;
15924c8dbbbSDavid Howells 
1606e4cffd7SChuck Lever 	memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
1616e4cffd7SChuck Lever 	clp->cl_addrlen = cl_init->addrlen;
16224c8dbbbSDavid Howells 
1633a498026STrond Myklebust 	if (cl_init->hostname) {
164a21bdd9bSChuck Lever 		err = -ENOMEM;
1653a498026STrond Myklebust 		clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
16624c8dbbbSDavid Howells 		if (!clp->cl_hostname)
16771468513SBenny Halevy 			goto error_cleanup;
16824c8dbbbSDavid Howells 	}
16924c8dbbbSDavid Howells 
17024c8dbbbSDavid Howells 	INIT_LIST_HEAD(&clp->cl_superblocks);
17124c8dbbbSDavid Howells 	clp->cl_rpcclient = ERR_PTR(-EINVAL);
17224c8dbbbSDavid Howells 
17359dca3b2STrond Myklebust 	clp->cl_proto = cl_init->proto;
17459dca3b2STrond Myklebust 
17524c8dbbbSDavid Howells #ifdef CONFIG_NFS_V4
176f4eecd5dSAndy Adamson 	err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
177f4eecd5dSAndy Adamson 	if (err)
178f4eecd5dSAndy Adamson 		goto error_cleanup;
179f4eecd5dSAndy Adamson 
18024c8dbbbSDavid Howells 	spin_lock_init(&clp->cl_lock);
18165f27f38SDavid Howells 	INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
18224c8dbbbSDavid Howells 	rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
18324c8dbbbSDavid Howells 	clp->cl_boot_time = CURRENT_TIME;
18424c8dbbbSDavid Howells 	clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
1855aae4a9aSBenny Halevy 	clp->cl_minorversion = cl_init->minorversion;
18697dc1359STrond Myklebust 	clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
18724c8dbbbSDavid Howells #endif
1887c67db3aSTrond Myklebust 	cred = rpc_lookup_machine_cred();
1897c67db3aSTrond Myklebust 	if (!IS_ERR(cred))
1907c67db3aSTrond Myklebust 		clp->cl_machine_cred = cred;
191974cec8cSAndy Adamson #if defined(CONFIG_NFS_V4_1)
192974cec8cSAndy Adamson 	INIT_LIST_HEAD(&clp->cl_layouts);
193974cec8cSAndy Adamson #endif
19414727281SDavid Howells 	nfs_fscache_get_client_cookie(clp);
19514727281SDavid Howells 
19624c8dbbbSDavid Howells 	return clp;
19724c8dbbbSDavid Howells 
19871468513SBenny Halevy error_cleanup:
19924c8dbbbSDavid Howells 	kfree(clp);
20024c8dbbbSDavid Howells error_0:
201a21bdd9bSChuck Lever 	return ERR_PTR(err);
20224c8dbbbSDavid Howells }
20324c8dbbbSDavid Howells 
20424c8dbbbSDavid Howells #ifdef CONFIG_NFS_V4
205557134a3SAndy Adamson #ifdef CONFIG_NFS_V4_1
206ea005281SAndy Adamson static void nfs4_shutdown_session(struct nfs_client *clp)
207ea005281SAndy Adamson {
208ea005281SAndy Adamson 	if (nfs4_has_session(clp))
209557134a3SAndy Adamson 		nfs4_destroy_session(clp->cl_session);
210557134a3SAndy Adamson }
211ea005281SAndy Adamson #else /* CONFIG_NFS_V4_1 */
212ea005281SAndy Adamson static void nfs4_shutdown_session(struct nfs_client *clp)
213ea005281SAndy Adamson {
214ea005281SAndy Adamson }
215557134a3SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
216557134a3SAndy Adamson 
217557134a3SAndy Adamson /*
218888ef2e3SAlexandros Batsakis  * Destroy the NFS4 callback service
219888ef2e3SAlexandros Batsakis  */
220888ef2e3SAlexandros Batsakis static void nfs4_destroy_callback(struct nfs_client *clp)
221888ef2e3SAlexandros Batsakis {
222888ef2e3SAlexandros Batsakis 	if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
22397dc1359STrond Myklebust 		nfs_callback_down(clp->cl_mvops->minor_version);
224888ef2e3SAlexandros Batsakis }
225888ef2e3SAlexandros Batsakis 
226888ef2e3SAlexandros Batsakis static void nfs4_shutdown_client(struct nfs_client *clp)
227888ef2e3SAlexandros Batsakis {
228888ef2e3SAlexandros Batsakis 	if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
229888ef2e3SAlexandros Batsakis 		nfs4_kill_renewd(clp);
230ea005281SAndy Adamson 	nfs4_shutdown_session(clp);
231888ef2e3SAlexandros Batsakis 	nfs4_destroy_callback(clp);
232888ef2e3SAlexandros Batsakis 	if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
233888ef2e3SAlexandros Batsakis 		nfs_idmap_delete(clp);
234888ef2e3SAlexandros Batsakis 
235888ef2e3SAlexandros Batsakis 	rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
236888ef2e3SAlexandros Batsakis }
237f4eecd5dSAndy Adamson 
238f4eecd5dSAndy Adamson /* idr_remove_all is not needed as all id's are removed by nfs_put_client */
239f4eecd5dSAndy Adamson void nfs_cleanup_cb_ident_idr(void)
240f4eecd5dSAndy Adamson {
241f4eecd5dSAndy Adamson 	idr_destroy(&cb_ident_idr);
242f4eecd5dSAndy Adamson }
243f4eecd5dSAndy Adamson 
244f4eecd5dSAndy Adamson /* nfs_client_lock held */
245f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
246f4eecd5dSAndy Adamson {
247f4eecd5dSAndy Adamson 	if (clp->cl_cb_ident)
248f4eecd5dSAndy Adamson 		idr_remove(&cb_ident_idr, clp->cl_cb_ident);
249f4eecd5dSAndy Adamson }
250f4eecd5dSAndy Adamson 
251f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server)
252f7e8917aSFred Isaman {
253f7e8917aSFred Isaman 	rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
254f7e8917aSFred Isaman }
255f7e8917aSFred Isaman 
256888ef2e3SAlexandros Batsakis #else
257888ef2e3SAlexandros Batsakis static void nfs4_shutdown_client(struct nfs_client *clp)
258888ef2e3SAlexandros Batsakis {
259888ef2e3SAlexandros Batsakis }
260f4eecd5dSAndy Adamson 
261f4eecd5dSAndy Adamson void nfs_cleanup_cb_ident_idr(void)
262f4eecd5dSAndy Adamson {
263f4eecd5dSAndy Adamson }
264f4eecd5dSAndy Adamson 
265f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
266f4eecd5dSAndy Adamson {
267f4eecd5dSAndy Adamson }
268f7e8917aSFred Isaman 
269f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server)
270f7e8917aSFred Isaman {
271f7e8917aSFred Isaman }
272f7e8917aSFred Isaman 
273888ef2e3SAlexandros Batsakis #endif /* CONFIG_NFS_V4 */
274888ef2e3SAlexandros Batsakis 
275888ef2e3SAlexandros Batsakis /*
2765dd3177aSTrond Myklebust  * Destroy a shared client record
2775dd3177aSTrond Myklebust  */
2785dd3177aSTrond Myklebust static void nfs_free_client(struct nfs_client *clp)
2795dd3177aSTrond Myklebust {
28040c55319STrond Myklebust 	dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
2815dd3177aSTrond Myklebust 
2825dd3177aSTrond Myklebust 	nfs4_shutdown_client(clp);
28324c8dbbbSDavid Howells 
28414727281SDavid Howells 	nfs_fscache_release_client_cookie(clp);
28514727281SDavid Howells 
28624c8dbbbSDavid Howells 	/* -EIO all pending I/O */
28724c8dbbbSDavid Howells 	if (!IS_ERR(clp->cl_rpcclient))
28824c8dbbbSDavid Howells 		rpc_shutdown_client(clp->cl_rpcclient);
28924c8dbbbSDavid Howells 
2907c67db3aSTrond Myklebust 	if (clp->cl_machine_cred != NULL)
2917c67db3aSTrond Myklebust 		put_rpccred(clp->cl_machine_cred);
2927c67db3aSTrond Myklebust 
2931775bc34SBenny Halevy 	nfs4_deviceid_purge_client(clp);
2941775bc34SBenny Halevy 
29524c8dbbbSDavid Howells 	kfree(clp->cl_hostname);
296*78fe0f41SWeston Andros Adamson 	kfree(clp->server_scope);
29724c8dbbbSDavid Howells 	kfree(clp);
29824c8dbbbSDavid Howells 
29924c8dbbbSDavid Howells 	dprintk("<-- nfs_free_client()\n");
30024c8dbbbSDavid Howells }
30124c8dbbbSDavid Howells 
30224c8dbbbSDavid Howells /*
30324c8dbbbSDavid Howells  * Release a reference to a shared client record
30424c8dbbbSDavid Howells  */
30524c8dbbbSDavid Howells void nfs_put_client(struct nfs_client *clp)
30624c8dbbbSDavid Howells {
30727ba8512SDavid Howells 	if (!clp)
30827ba8512SDavid Howells 		return;
30927ba8512SDavid Howells 
31024c8dbbbSDavid Howells 	dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
31124c8dbbbSDavid Howells 
31224c8dbbbSDavid Howells 	if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
31324c8dbbbSDavid Howells 		list_del(&clp->cl_share_link);
314f4eecd5dSAndy Adamson 		nfs_cb_idr_remove_locked(clp);
31524c8dbbbSDavid Howells 		spin_unlock(&nfs_client_lock);
31624c8dbbbSDavid Howells 
31724c8dbbbSDavid Howells 		BUG_ON(!list_empty(&clp->cl_superblocks));
31824c8dbbbSDavid Howells 
31924c8dbbbSDavid Howells 		nfs_free_client(clp);
32024c8dbbbSDavid Howells 	}
32124c8dbbbSDavid Howells }
32216b374caSAndy Adamson EXPORT_SYMBOL_GPL(nfs_put_client);
32324c8dbbbSDavid Howells 
3249082a5ccSTrond Myklebust #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3259f4c899cSTrond Myklebust /*
3269f4c899cSTrond Myklebust  * Test if two ip6 socket addresses refer to the same socket by
3279f4c899cSTrond Myklebust  * comparing relevant fields. The padding bytes specifically, are not
3289f4c899cSTrond Myklebust  * compared. sin6_flowinfo is not compared because it only affects QoS
3299f4c899cSTrond Myklebust  * and sin6_scope_id is only compared if the address is "link local"
3309f4c899cSTrond Myklebust  * because "link local" addresses need only be unique to a specific
3319f4c899cSTrond Myklebust  * link. Conversely, ordinary unicast addresses might have different
3329f4c899cSTrond Myklebust  * sin6_scope_id.
3339f4c899cSTrond Myklebust  *
3349f4c899cSTrond Myklebust  * The caller should ensure both socket addresses are AF_INET6.
3359f4c899cSTrond Myklebust  */
3363c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
3379f4c899cSTrond Myklebust 				      const struct sockaddr *sa2)
3389f4c899cSTrond Myklebust {
3393c8c45dfSChuck Lever 	const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
3403c8c45dfSChuck Lever 	const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
3419f4c899cSTrond Myklebust 
3423c8c45dfSChuck Lever 	if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
3433c8c45dfSChuck Lever 	    sin1->sin6_scope_id != sin2->sin6_scope_id)
3449f4c899cSTrond Myklebust 		return 0;
3453b0d3f93STrond Myklebust 
346b20d37caSTrond Myklebust 	return ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr);
3473b0d3f93STrond Myklebust }
3483c8c45dfSChuck Lever #else	/* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
3493c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
3509f4c899cSTrond Myklebust 				      const struct sockaddr *sa2)
3519f4c899cSTrond Myklebust {
3529f4c899cSTrond Myklebust 	return 0;
3539f4c899cSTrond Myklebust }
3549082a5ccSTrond Myklebust #endif
3553b0d3f93STrond Myklebust 
35624c8dbbbSDavid Howells /*
357d7371c41SIan Dall  * Test if two ip4 socket addresses refer to the same socket, by
358d7371c41SIan Dall  * comparing relevant fields. The padding bytes specifically, are
359d7371c41SIan Dall  * not compared.
360d7371c41SIan Dall  *
361d7371c41SIan Dall  * The caller should ensure both socket addresses are AF_INET.
362d7371c41SIan Dall  */
3633c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
3643c8c45dfSChuck Lever 				      const struct sockaddr *sa2)
3653c8c45dfSChuck Lever {
3663c8c45dfSChuck Lever 	const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
3673c8c45dfSChuck Lever 	const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
3683c8c45dfSChuck Lever 
3693c8c45dfSChuck Lever 	return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
3703c8c45dfSChuck Lever }
3713c8c45dfSChuck Lever 
3723c8c45dfSChuck Lever static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
3733c8c45dfSChuck Lever 				const struct sockaddr *sa2)
3743c8c45dfSChuck Lever {
3753c8c45dfSChuck Lever 	const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
3763c8c45dfSChuck Lever 	const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
3773c8c45dfSChuck Lever 
3783c8c45dfSChuck Lever 	return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
3793c8c45dfSChuck Lever 		(sin1->sin6_port == sin2->sin6_port);
3803c8c45dfSChuck Lever }
3813c8c45dfSChuck Lever 
3829f4c899cSTrond Myklebust static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
3839f4c899cSTrond Myklebust 				const struct sockaddr *sa2)
384d7371c41SIan Dall {
3853c8c45dfSChuck Lever 	const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
3863c8c45dfSChuck Lever 	const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
3879f4c899cSTrond Myklebust 
3883c8c45dfSChuck Lever 	return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
3893c8c45dfSChuck Lever 		(sin1->sin_port == sin2->sin_port);
390d7371c41SIan Dall }
391d7371c41SIan Dall 
392d7371c41SIan Dall /*
393d7371c41SIan Dall  * Test if two socket addresses represent the same actual socket,
3943c8c45dfSChuck Lever  * by comparing (only) relevant fields, excluding the port number.
3953c8c45dfSChuck Lever  */
3963c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
3973c8c45dfSChuck Lever 				     const struct sockaddr *sa2)
3983c8c45dfSChuck Lever {
3993c8c45dfSChuck Lever 	if (sa1->sa_family != sa2->sa_family)
4003c8c45dfSChuck Lever 		return 0;
4013c8c45dfSChuck Lever 
4023c8c45dfSChuck Lever 	switch (sa1->sa_family) {
4033c8c45dfSChuck Lever 	case AF_INET:
4043c8c45dfSChuck Lever 		return nfs_sockaddr_match_ipaddr4(sa1, sa2);
4053c8c45dfSChuck Lever 	case AF_INET6:
4063c8c45dfSChuck Lever 		return nfs_sockaddr_match_ipaddr6(sa1, sa2);
4073c8c45dfSChuck Lever 	}
4083c8c45dfSChuck Lever 	return 0;
4093c8c45dfSChuck Lever }
4103c8c45dfSChuck Lever 
4113c8c45dfSChuck Lever /*
4123c8c45dfSChuck Lever  * Test if two socket addresses represent the same actual socket,
4133c8c45dfSChuck Lever  * by comparing (only) relevant fields, including the port number.
414d7371c41SIan Dall  */
415d7371c41SIan Dall static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
416d7371c41SIan Dall 			    const struct sockaddr *sa2)
417d7371c41SIan Dall {
418d7371c41SIan Dall 	if (sa1->sa_family != sa2->sa_family)
419d7371c41SIan Dall 		return 0;
420d7371c41SIan Dall 
421d7371c41SIan Dall 	switch (sa1->sa_family) {
422d7371c41SIan Dall 	case AF_INET:
4239f4c899cSTrond Myklebust 		return nfs_sockaddr_cmp_ip4(sa1, sa2);
424d7371c41SIan Dall 	case AF_INET6:
4259f4c899cSTrond Myklebust 		return nfs_sockaddr_cmp_ip6(sa1, sa2);
426d7371c41SIan Dall 	}
427d7371c41SIan Dall 	return 0;
428d7371c41SIan Dall }
429d7371c41SIan Dall 
430c36fca52SAndy Adamson /* Common match routine for v4.0 and v4.1 callback services */
431c36fca52SAndy Adamson bool
432c36fca52SAndy Adamson nfs4_cb_match_client(const struct sockaddr *addr, struct nfs_client *clp,
433c36fca52SAndy Adamson 		     u32 minorversion)
434c81468a1STrond Myklebust {
4353b0d3f93STrond Myklebust 	struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
4363b0d3f93STrond Myklebust 
437c36fca52SAndy Adamson 	/* Don't match clients that failed to initialise */
43876db6d95SAndy Adamson 	if (!(clp->cl_cons_state == NFS_CS_READY ||
43976db6d95SAndy Adamson 	    clp->cl_cons_state == NFS_CS_SESSION_INITING))
440c36fca52SAndy Adamson 		return false;
441c81468a1STrond Myklebust 
442c36fca52SAndy Adamson 	/* Match the version and minorversion */
443c36fca52SAndy Adamson 	if (clp->rpc_ops->version != 4 ||
444c36fca52SAndy Adamson 	    clp->cl_minorversion != minorversion)
445c36fca52SAndy Adamson 		return false;
446c81468a1STrond Myklebust 
447c81468a1STrond Myklebust 	/* Match only the IP address, not the port number */
4483b0d3f93STrond Myklebust 	if (!nfs_sockaddr_match_ipaddr(addr, clap))
449c36fca52SAndy Adamson 		return false;
450c81468a1STrond Myklebust 
451c36fca52SAndy Adamson 	return true;
4523fbd67adSTrond Myklebust }
4533fbd67adSTrond Myklebust 
4543fbd67adSTrond Myklebust /*
455c81468a1STrond Myklebust  * Find an nfs_client on the list that matches the initialisation data
456c81468a1STrond Myklebust  * that is supplied.
457c81468a1STrond Myklebust  */
458c81468a1STrond Myklebust static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
45924c8dbbbSDavid Howells {
46024c8dbbbSDavid Howells 	struct nfs_client *clp;
461d7371c41SIan Dall 	const struct sockaddr *sap = data->addr;
46224c8dbbbSDavid Howells 
46324c8dbbbSDavid Howells 	list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
464d7371c41SIan Dall 	        const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
46513bbc06aSTrond Myklebust 		/* Don't match clients that failed to initialise properly */
46613bbc06aSTrond Myklebust 		if (clp->cl_cons_state < 0)
46713bbc06aSTrond Myklebust 			continue;
46813bbc06aSTrond Myklebust 
46924c8dbbbSDavid Howells 		/* Different NFS versions cannot share the same nfs_client */
47040c55319STrond Myklebust 		if (clp->rpc_ops != data->rpc_ops)
47124c8dbbbSDavid Howells 			continue;
47224c8dbbbSDavid Howells 
47359dca3b2STrond Myklebust 		if (clp->cl_proto != data->proto)
47459dca3b2STrond Myklebust 			continue;
4755aae4a9aSBenny Halevy 		/* Match nfsv4 minorversion */
4765aae4a9aSBenny Halevy 		if (clp->cl_minorversion != data->minorversion)
4775aae4a9aSBenny Halevy 			continue;
478c81468a1STrond Myklebust 		/* Match the full socket address */
479d7371c41SIan Dall 		if (!nfs_sockaddr_cmp(sap, clap))
48024c8dbbbSDavid Howells 			continue;
48124c8dbbbSDavid Howells 
48224c8dbbbSDavid Howells 		atomic_inc(&clp->cl_count);
48324c8dbbbSDavid Howells 		return clp;
48424c8dbbbSDavid Howells 	}
485c81468a1STrond Myklebust 	return NULL;
48624c8dbbbSDavid Howells }
48724c8dbbbSDavid Howells 
48824c8dbbbSDavid Howells /*
48924c8dbbbSDavid Howells  * Look up a client by IP address and protocol version
49024c8dbbbSDavid Howells  * - creates a new record if one doesn't yet exist
49124c8dbbbSDavid Howells  */
49245a52a02SAndy Adamson static struct nfs_client *
49345a52a02SAndy Adamson nfs_get_client(const struct nfs_client_initdata *cl_init,
49445a52a02SAndy Adamson 	       const struct rpc_timeout *timeparms,
49545a52a02SAndy Adamson 	       const char *ip_addr,
49645a52a02SAndy Adamson 	       rpc_authflavor_t authflavour,
49745a52a02SAndy Adamson 	       int noresvport)
49824c8dbbbSDavid Howells {
49924c8dbbbSDavid Howells 	struct nfs_client *clp, *new = NULL;
50024c8dbbbSDavid Howells 	int error;
50124c8dbbbSDavid Howells 
502d7422c47SChuck Lever 	dprintk("--> nfs_get_client(%s,v%u)\n",
503d7422c47SChuck Lever 		cl_init->hostname ?: "", cl_init->rpc_ops->version);
50424c8dbbbSDavid Howells 
50524c8dbbbSDavid Howells 	/* see if the client already exists */
50624c8dbbbSDavid Howells 	do {
50724c8dbbbSDavid Howells 		spin_lock(&nfs_client_lock);
50824c8dbbbSDavid Howells 
509c81468a1STrond Myklebust 		clp = nfs_match_client(cl_init);
51024c8dbbbSDavid Howells 		if (clp)
51124c8dbbbSDavid Howells 			goto found_client;
51224c8dbbbSDavid Howells 		if (new)
51324c8dbbbSDavid Howells 			goto install_client;
51424c8dbbbSDavid Howells 
51524c8dbbbSDavid Howells 		spin_unlock(&nfs_client_lock);
51624c8dbbbSDavid Howells 
5173a498026STrond Myklebust 		new = nfs_alloc_client(cl_init);
518a21bdd9bSChuck Lever 	} while (!IS_ERR(new));
51924c8dbbbSDavid Howells 
520a21bdd9bSChuck Lever 	dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
521a21bdd9bSChuck Lever 	return new;
52224c8dbbbSDavid Howells 
52324c8dbbbSDavid Howells 	/* install a new client and return with it unready */
52424c8dbbbSDavid Howells install_client:
52524c8dbbbSDavid Howells 	clp = new;
52624c8dbbbSDavid Howells 	list_add(&clp->cl_share_link, &nfs_client_list);
52724c8dbbbSDavid Howells 	spin_unlock(&nfs_client_lock);
52845a52a02SAndy Adamson 
52945a52a02SAndy Adamson 	error = cl_init->rpc_ops->init_client(clp, timeparms, ip_addr,
53045a52a02SAndy Adamson 					      authflavour, noresvport);
53145a52a02SAndy Adamson 	if (error < 0) {
53245a52a02SAndy Adamson 		nfs_put_client(clp);
53345a52a02SAndy Adamson 		return ERR_PTR(error);
53445a52a02SAndy Adamson 	}
53524c8dbbbSDavid Howells 	dprintk("--> nfs_get_client() = %p [new]\n", clp);
53624c8dbbbSDavid Howells 	return clp;
53724c8dbbbSDavid Howells 
53824c8dbbbSDavid Howells 	/* found an existing client
53924c8dbbbSDavid Howells 	 * - make sure it's ready before returning
54024c8dbbbSDavid Howells 	 */
54124c8dbbbSDavid Howells found_client:
54224c8dbbbSDavid Howells 	spin_unlock(&nfs_client_lock);
54324c8dbbbSDavid Howells 
54424c8dbbbSDavid Howells 	if (new)
54524c8dbbbSDavid Howells 		nfs_free_client(new);
54624c8dbbbSDavid Howells 
547150030b7SMatthew Wilcox 	error = wait_event_killable(nfs_client_active_wq,
54876db6d95SAndy Adamson 				clp->cl_cons_state < NFS_CS_INITING);
5490bae89ecSTrond Myklebust 	if (error < 0) {
55024c8dbbbSDavid Howells 		nfs_put_client(clp);
55124c8dbbbSDavid Howells 		return ERR_PTR(-ERESTARTSYS);
55224c8dbbbSDavid Howells 	}
55324c8dbbbSDavid Howells 
55424c8dbbbSDavid Howells 	if (clp->cl_cons_state < NFS_CS_READY) {
55524c8dbbbSDavid Howells 		error = clp->cl_cons_state;
55624c8dbbbSDavid Howells 		nfs_put_client(clp);
55724c8dbbbSDavid Howells 		return ERR_PTR(error);
55824c8dbbbSDavid Howells 	}
55924c8dbbbSDavid Howells 
56054ceac45SDavid Howells 	BUG_ON(clp->cl_cons_state != NFS_CS_READY);
56154ceac45SDavid Howells 
56224c8dbbbSDavid Howells 	dprintk("--> nfs_get_client() = %p [share]\n", clp);
56324c8dbbbSDavid Howells 	return clp;
56424c8dbbbSDavid Howells }
56524c8dbbbSDavid Howells 
56624c8dbbbSDavid Howells /*
56724c8dbbbSDavid Howells  * Mark a server as ready or failed
56824c8dbbbSDavid Howells  */
56976db6d95SAndy Adamson void nfs_mark_client_ready(struct nfs_client *clp, int state)
57024c8dbbbSDavid Howells {
57124c8dbbbSDavid Howells 	clp->cl_cons_state = state;
57224c8dbbbSDavid Howells 	wake_up_all(&nfs_client_active_wq);
57324c8dbbbSDavid Howells }
5745006a76cSDavid Howells 
5755006a76cSDavid Howells /*
576008f55d0SBenny Halevy  * With sessions, the client is not marked ready until after a
577008f55d0SBenny Halevy  * successful EXCHANGE_ID and CREATE_SESSION.
578008f55d0SBenny Halevy  *
579008f55d0SBenny Halevy  * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
580008f55d0SBenny Halevy  * other versions of NFS can be tried.
581008f55d0SBenny Halevy  */
582008f55d0SBenny Halevy int nfs4_check_client_ready(struct nfs_client *clp)
583008f55d0SBenny Halevy {
584008f55d0SBenny Halevy 	if (!nfs4_has_session(clp))
585008f55d0SBenny Halevy 		return 0;
586008f55d0SBenny Halevy 	if (clp->cl_cons_state < NFS_CS_READY)
587008f55d0SBenny Halevy 		return -EPROTONOSUPPORT;
588008f55d0SBenny Halevy 	return 0;
589008f55d0SBenny Halevy }
590008f55d0SBenny Halevy 
591008f55d0SBenny Halevy /*
5925006a76cSDavid Howells  * Initialise the timeout values for a connection
5935006a76cSDavid Howells  */
5945006a76cSDavid Howells static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
5955006a76cSDavid Howells 				    unsigned int timeo, unsigned int retrans)
5965006a76cSDavid Howells {
5975006a76cSDavid Howells 	to->to_initval = timeo * HZ / 10;
5985006a76cSDavid Howells 	to->to_retries = retrans;
5995006a76cSDavid Howells 
6005006a76cSDavid Howells 	switch (proto) {
6010896a725S\"Talpey, Thomas\ 	case XPRT_TRANSPORT_TCP:
6022cf7ff7aS\"Talpey, Thomas\ 	case XPRT_TRANSPORT_RDMA:
603259875efSTrond Myklebust 		if (to->to_retries == 0)
604259875efSTrond Myklebust 			to->to_retries = NFS_DEF_TCP_RETRANS;
6057a3e3e18STrond Myklebust 		if (to->to_initval == 0)
606259875efSTrond Myklebust 			to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
6075006a76cSDavid Howells 		if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
6085006a76cSDavid Howells 			to->to_initval = NFS_MAX_TCP_TIMEOUT;
6095006a76cSDavid Howells 		to->to_increment = to->to_initval;
6105006a76cSDavid Howells 		to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
6117a3e3e18STrond Myklebust 		if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
6127a3e3e18STrond Myklebust 			to->to_maxval = NFS_MAX_TCP_TIMEOUT;
6137a3e3e18STrond Myklebust 		if (to->to_maxval < to->to_initval)
6147a3e3e18STrond Myklebust 			to->to_maxval = to->to_initval;
6155006a76cSDavid Howells 		to->to_exponential = 0;
6165006a76cSDavid Howells 		break;
6170896a725S\"Talpey, Thomas\ 	case XPRT_TRANSPORT_UDP:
618259875efSTrond Myklebust 		if (to->to_retries == 0)
619259875efSTrond Myklebust 			to->to_retries = NFS_DEF_UDP_RETRANS;
6205006a76cSDavid Howells 		if (!to->to_initval)
621259875efSTrond Myklebust 			to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
6225006a76cSDavid Howells 		if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
6235006a76cSDavid Howells 			to->to_initval = NFS_MAX_UDP_TIMEOUT;
6245006a76cSDavid Howells 		to->to_maxval = NFS_MAX_UDP_TIMEOUT;
6255006a76cSDavid Howells 		to->to_exponential = 1;
6265006a76cSDavid Howells 		break;
627259875efSTrond Myklebust 	default:
628259875efSTrond Myklebust 		BUG();
6295006a76cSDavid Howells 	}
6305006a76cSDavid Howells }
6315006a76cSDavid Howells 
6325006a76cSDavid Howells /*
6335006a76cSDavid Howells  * Create an RPC client handle
6345006a76cSDavid Howells  */
63559dca3b2STrond Myklebust static int nfs_create_rpc_client(struct nfs_client *clp,
63633170233STrond Myklebust 				 const struct rpc_timeout *timeparms,
63743d78ef2SChuck Lever 				 rpc_authflavor_t flavor,
6384a01b8a4SChuck Lever 				 int discrtry, int noresvport)
6395006a76cSDavid Howells {
6405006a76cSDavid Howells 	struct rpc_clnt		*clnt = NULL;
64141877d20SChuck Lever 	struct rpc_create_args args = {
642c653ce3fSPavel Emelyanov 		.net		= &init_net,
64359dca3b2STrond Myklebust 		.protocol	= clp->cl_proto,
64441877d20SChuck Lever 		.address	= (struct sockaddr *)&clp->cl_addr,
6456e4cffd7SChuck Lever 		.addrsize	= clp->cl_addrlen,
64633170233STrond Myklebust 		.timeout	= timeparms,
64741877d20SChuck Lever 		.servername	= clp->cl_hostname,
64841877d20SChuck Lever 		.program	= &nfs_program,
64941877d20SChuck Lever 		.version	= clp->rpc_ops->version,
65041877d20SChuck Lever 		.authflavor	= flavor,
65141877d20SChuck Lever 	};
6525006a76cSDavid Howells 
6534a01b8a4SChuck Lever 	if (discrtry)
6544a01b8a4SChuck Lever 		args.flags |= RPC_CLNT_CREATE_DISCRTRY;
6554a01b8a4SChuck Lever 	if (noresvport)
6564a01b8a4SChuck Lever 		args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
6574a01b8a4SChuck Lever 
6585006a76cSDavid Howells 	if (!IS_ERR(clp->cl_rpcclient))
6595006a76cSDavid Howells 		return 0;
6605006a76cSDavid Howells 
66141877d20SChuck Lever 	clnt = rpc_create(&args);
6625006a76cSDavid Howells 	if (IS_ERR(clnt)) {
6635006a76cSDavid Howells 		dprintk("%s: cannot create RPC client. Error = %ld\n",
6643110ff80SHarvey Harrison 				__func__, PTR_ERR(clnt));
6655006a76cSDavid Howells 		return PTR_ERR(clnt);
6665006a76cSDavid Howells 	}
6675006a76cSDavid Howells 
6685006a76cSDavid Howells 	clp->cl_rpcclient = clnt;
6695006a76cSDavid Howells 	return 0;
6705006a76cSDavid Howells }
67154ceac45SDavid Howells 
67254ceac45SDavid Howells /*
67354ceac45SDavid Howells  * Version 2 or 3 client destruction
67454ceac45SDavid Howells  */
67554ceac45SDavid Howells static void nfs_destroy_server(struct nfs_server *server)
67654ceac45SDavid Howells {
6775eebde23SSuresh Jayaraman 	if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
6785eebde23SSuresh Jayaraman 			!(server->flags & NFS_MOUNT_LOCAL_FCNTL))
6799289e7f9SChuck Lever 		nlmclnt_done(server->nlm_host);
68054ceac45SDavid Howells }
68154ceac45SDavid Howells 
68254ceac45SDavid Howells /*
68354ceac45SDavid Howells  * Version 2 or 3 lockd setup
68454ceac45SDavid Howells  */
68554ceac45SDavid Howells static int nfs_start_lockd(struct nfs_server *server)
68654ceac45SDavid Howells {
6879289e7f9SChuck Lever 	struct nlm_host *host;
6889289e7f9SChuck Lever 	struct nfs_client *clp = server->nfs_client;
689883bb163SChuck Lever 	struct nlmclnt_initdata nlm_init = {
690883bb163SChuck Lever 		.hostname	= clp->cl_hostname,
691883bb163SChuck Lever 		.address	= (struct sockaddr *)&clp->cl_addr,
692883bb163SChuck Lever 		.addrlen	= clp->cl_addrlen,
693883bb163SChuck Lever 		.nfs_version	= clp->rpc_ops->version,
6940cb2659bSChuck Lever 		.noresvport	= server->flags & NFS_MOUNT_NORESVPORT ?
6950cb2659bSChuck Lever 					1 : 0,
696883bb163SChuck Lever 	};
69754ceac45SDavid Howells 
698883bb163SChuck Lever 	if (nlm_init.nfs_version > 3)
6999289e7f9SChuck Lever 		return 0;
7005eebde23SSuresh Jayaraman 	if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
7015eebde23SSuresh Jayaraman 			(server->flags & NFS_MOUNT_LOCAL_FCNTL))
7029289e7f9SChuck Lever 		return 0;
7039289e7f9SChuck Lever 
7048a6e5debSTrond Myklebust 	switch (clp->cl_proto) {
7058a6e5debSTrond Myklebust 		default:
7068a6e5debSTrond Myklebust 			nlm_init.protocol = IPPROTO_TCP;
7078a6e5debSTrond Myklebust 			break;
7088a6e5debSTrond Myklebust 		case XPRT_TRANSPORT_UDP:
7098a6e5debSTrond Myklebust 			nlm_init.protocol = IPPROTO_UDP;
7108a6e5debSTrond Myklebust 	}
7118a6e5debSTrond Myklebust 
712883bb163SChuck Lever 	host = nlmclnt_init(&nlm_init);
7139289e7f9SChuck Lever 	if (IS_ERR(host))
7149289e7f9SChuck Lever 		return PTR_ERR(host);
7159289e7f9SChuck Lever 
7169289e7f9SChuck Lever 	server->nlm_host = host;
71754ceac45SDavid Howells 	server->destroy = nfs_destroy_server;
7189289e7f9SChuck Lever 	return 0;
71954ceac45SDavid Howells }
72054ceac45SDavid Howells 
72154ceac45SDavid Howells /*
72254ceac45SDavid Howells  * Initialise an NFSv3 ACL client connection
72354ceac45SDavid Howells  */
72454ceac45SDavid Howells #ifdef CONFIG_NFS_V3_ACL
72554ceac45SDavid Howells static void nfs_init_server_aclclient(struct nfs_server *server)
72654ceac45SDavid Howells {
72740c55319STrond Myklebust 	if (server->nfs_client->rpc_ops->version != 3)
72854ceac45SDavid Howells 		goto out_noacl;
72954ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_NOACL)
73054ceac45SDavid Howells 		goto out_noacl;
73154ceac45SDavid Howells 
73254ceac45SDavid Howells 	server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
73354ceac45SDavid Howells 	if (IS_ERR(server->client_acl))
73454ceac45SDavid Howells 		goto out_noacl;
73554ceac45SDavid Howells 
73654ceac45SDavid Howells 	/* No errors! Assume that Sun nfsacls are supported */
73754ceac45SDavid Howells 	server->caps |= NFS_CAP_ACLS;
73854ceac45SDavid Howells 	return;
73954ceac45SDavid Howells 
74054ceac45SDavid Howells out_noacl:
74154ceac45SDavid Howells 	server->caps &= ~NFS_CAP_ACLS;
74254ceac45SDavid Howells }
74354ceac45SDavid Howells #else
74454ceac45SDavid Howells static inline void nfs_init_server_aclclient(struct nfs_server *server)
74554ceac45SDavid Howells {
74654ceac45SDavid Howells 	server->flags &= ~NFS_MOUNT_NOACL;
74754ceac45SDavid Howells 	server->caps &= ~NFS_CAP_ACLS;
74854ceac45SDavid Howells }
74954ceac45SDavid Howells #endif
75054ceac45SDavid Howells 
75154ceac45SDavid Howells /*
75254ceac45SDavid Howells  * Create a general RPC client
75354ceac45SDavid Howells  */
75433170233STrond Myklebust static int nfs_init_server_rpcclient(struct nfs_server *server,
75533170233STrond Myklebust 		const struct rpc_timeout *timeo,
75633170233STrond Myklebust 		rpc_authflavor_t pseudoflavour)
75754ceac45SDavid Howells {
75854ceac45SDavid Howells 	struct nfs_client *clp = server->nfs_client;
75954ceac45SDavid Howells 
76054ceac45SDavid Howells 	server->client = rpc_clone_client(clp->cl_rpcclient);
76154ceac45SDavid Howells 	if (IS_ERR(server->client)) {
7623110ff80SHarvey Harrison 		dprintk("%s: couldn't create rpc_client!\n", __func__);
76354ceac45SDavid Howells 		return PTR_ERR(server->client);
76454ceac45SDavid Howells 	}
76554ceac45SDavid Howells 
76633170233STrond Myklebust 	memcpy(&server->client->cl_timeout_default,
76733170233STrond Myklebust 			timeo,
76833170233STrond Myklebust 			sizeof(server->client->cl_timeout_default));
76933170233STrond Myklebust 	server->client->cl_timeout = &server->client->cl_timeout_default;
77033170233STrond Myklebust 
77154ceac45SDavid Howells 	if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
77254ceac45SDavid Howells 		struct rpc_auth *auth;
77354ceac45SDavid Howells 
77454ceac45SDavid Howells 		auth = rpcauth_create(pseudoflavour, server->client);
77554ceac45SDavid Howells 		if (IS_ERR(auth)) {
7763110ff80SHarvey Harrison 			dprintk("%s: couldn't create credcache!\n", __func__);
77754ceac45SDavid Howells 			return PTR_ERR(auth);
77854ceac45SDavid Howells 		}
77954ceac45SDavid Howells 	}
78054ceac45SDavid Howells 	server->client->cl_softrtry = 0;
78154ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_SOFT)
78254ceac45SDavid Howells 		server->client->cl_softrtry = 1;
78354ceac45SDavid Howells 
78454ceac45SDavid Howells 	return 0;
78554ceac45SDavid Howells }
78654ceac45SDavid Howells 
78754ceac45SDavid Howells /*
78854ceac45SDavid Howells  * Initialise an NFS2 or NFS3 client
78954ceac45SDavid Howells  */
79045a52a02SAndy Adamson int nfs_init_client(struct nfs_client *clp, const struct rpc_timeout *timeparms,
79145a52a02SAndy Adamson 		    const char *ip_addr, rpc_authflavor_t authflavour,
79245a52a02SAndy Adamson 		    int noresvport)
79354ceac45SDavid Howells {
79454ceac45SDavid Howells 	int error;
79554ceac45SDavid Howells 
79654ceac45SDavid Howells 	if (clp->cl_cons_state == NFS_CS_READY) {
79754ceac45SDavid Howells 		/* the client is already initialised */
79854ceac45SDavid Howells 		dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
79954ceac45SDavid Howells 		return 0;
80054ceac45SDavid Howells 	}
80154ceac45SDavid Howells 
80254ceac45SDavid Howells 	/*
80354ceac45SDavid Howells 	 * Create a client RPC handle for doing FSSTAT with UNIX auth only
80454ceac45SDavid Howells 	 * - RFC 2623, sec 2.3.2
80554ceac45SDavid Howells 	 */
806d740351bSChuck Lever 	error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
80745a52a02SAndy Adamson 				      0, noresvport);
80854ceac45SDavid Howells 	if (error < 0)
80954ceac45SDavid Howells 		goto error;
81054ceac45SDavid Howells 	nfs_mark_client_ready(clp, NFS_CS_READY);
81154ceac45SDavid Howells 	return 0;
81254ceac45SDavid Howells 
81354ceac45SDavid Howells error:
81454ceac45SDavid Howells 	nfs_mark_client_ready(clp, error);
81554ceac45SDavid Howells 	dprintk("<-- nfs_init_client() = xerror %d\n", error);
81654ceac45SDavid Howells 	return error;
81754ceac45SDavid Howells }
81854ceac45SDavid Howells 
81954ceac45SDavid Howells /*
82054ceac45SDavid Howells  * Create a version 2 or 3 client
82154ceac45SDavid Howells  */
8222283f8d6S\"Talpey, Thomas\ static int nfs_init_server(struct nfs_server *server,
8232283f8d6S\"Talpey, Thomas\ 			   const struct nfs_parsed_mount_data *data)
82454ceac45SDavid Howells {
8253a498026STrond Myklebust 	struct nfs_client_initdata cl_init = {
8263a498026STrond Myklebust 		.hostname = data->nfs_server.hostname,
827d7422c47SChuck Lever 		.addr = (const struct sockaddr *)&data->nfs_server.address,
8284c568017SChuck Lever 		.addrlen = data->nfs_server.addrlen,
82940c55319STrond Myklebust 		.rpc_ops = &nfs_v2_clientops,
83059dca3b2STrond Myklebust 		.proto = data->nfs_server.protocol,
8313a498026STrond Myklebust 	};
83233170233STrond Myklebust 	struct rpc_timeout timeparms;
83354ceac45SDavid Howells 	struct nfs_client *clp;
8343a498026STrond Myklebust 	int error;
83554ceac45SDavid Howells 
83654ceac45SDavid Howells 	dprintk("--> nfs_init_server()\n");
83754ceac45SDavid Howells 
83854ceac45SDavid Howells #ifdef CONFIG_NFS_V3
8398a6e5debSTrond Myklebust 	if (data->version == 3)
84040c55319STrond Myklebust 		cl_init.rpc_ops = &nfs_v3_clientops;
84154ceac45SDavid Howells #endif
84254ceac45SDavid Howells 
84345a52a02SAndy Adamson 	nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
84445a52a02SAndy Adamson 			data->timeo, data->retrans);
84545a52a02SAndy Adamson 
84654ceac45SDavid Howells 	/* Allocate or find a client reference we can use */
84745a52a02SAndy Adamson 	clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX,
84845a52a02SAndy Adamson 			     data->flags & NFS_MOUNT_NORESVPORT);
84954ceac45SDavid Howells 	if (IS_ERR(clp)) {
85054ceac45SDavid Howells 		dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
85154ceac45SDavid Howells 		return PTR_ERR(clp);
85254ceac45SDavid Howells 	}
85354ceac45SDavid Howells 
85454ceac45SDavid Howells 	server->nfs_client = clp;
85554ceac45SDavid Howells 
85654ceac45SDavid Howells 	/* Initialise the client representation from the mount data */
857ff3525a5STrond Myklebust 	server->flags = data->flags;
858b797cac7SDavid Howells 	server->options = data->options;
85962ab460cSTrond Myklebust 	server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
86062ab460cSTrond Myklebust 		NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
86162ab460cSTrond Myklebust 		NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
86254ceac45SDavid Howells 
86354ceac45SDavid Howells 	if (data->rsize)
86454ceac45SDavid Howells 		server->rsize = nfs_block_size(data->rsize, NULL);
86554ceac45SDavid Howells 	if (data->wsize)
86654ceac45SDavid Howells 		server->wsize = nfs_block_size(data->wsize, NULL);
86754ceac45SDavid Howells 
86854ceac45SDavid Howells 	server->acregmin = data->acregmin * HZ;
86954ceac45SDavid Howells 	server->acregmax = data->acregmax * HZ;
87054ceac45SDavid Howells 	server->acdirmin = data->acdirmin * HZ;
87154ceac45SDavid Howells 	server->acdirmax = data->acdirmax * HZ;
87254ceac45SDavid Howells 
87354ceac45SDavid Howells 	/* Start lockd here, before we might error out */
87454ceac45SDavid Howells 	error = nfs_start_lockd(server);
87554ceac45SDavid Howells 	if (error < 0)
87654ceac45SDavid Howells 		goto error;
87754ceac45SDavid Howells 
878f22d6d79SChuck Lever 	server->port = data->nfs_server.port;
879f22d6d79SChuck Lever 
88033170233STrond Myklebust 	error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
88154ceac45SDavid Howells 	if (error < 0)
88254ceac45SDavid Howells 		goto error;
88354ceac45SDavid Howells 
8843f8400d1SChuck Lever 	/* Preserve the values of mount_server-related mount options */
8853f8400d1SChuck Lever 	if (data->mount_server.addrlen) {
8863f8400d1SChuck Lever 		memcpy(&server->mountd_address, &data->mount_server.address,
8873f8400d1SChuck Lever 			data->mount_server.addrlen);
8883f8400d1SChuck Lever 		server->mountd_addrlen = data->mount_server.addrlen;
8893f8400d1SChuck Lever 	}
8903f8400d1SChuck Lever 	server->mountd_version = data->mount_server.version;
8913f8400d1SChuck Lever 	server->mountd_port = data->mount_server.port;
8923f8400d1SChuck Lever 	server->mountd_protocol = data->mount_server.protocol;
8933f8400d1SChuck Lever 
89454ceac45SDavid Howells 	server->namelen  = data->namlen;
89554ceac45SDavid Howells 	/* Create a client RPC handle for the NFSv3 ACL management interface */
89654ceac45SDavid Howells 	nfs_init_server_aclclient(server);
89754ceac45SDavid Howells 	dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
89854ceac45SDavid Howells 	return 0;
89954ceac45SDavid Howells 
90054ceac45SDavid Howells error:
90154ceac45SDavid Howells 	server->nfs_client = NULL;
90254ceac45SDavid Howells 	nfs_put_client(clp);
90354ceac45SDavid Howells 	dprintk("<-- nfs_init_server() = xerror %d\n", error);
90454ceac45SDavid Howells 	return error;
90554ceac45SDavid Howells }
90654ceac45SDavid Howells 
90754ceac45SDavid Howells /*
90854ceac45SDavid Howells  * Load up the server record from information gained in an fsinfo record
90954ceac45SDavid Howells  */
91054ceac45SDavid Howells static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
91154ceac45SDavid Howells {
91254ceac45SDavid Howells 	unsigned long max_rpc_payload;
91354ceac45SDavid Howells 
91454ceac45SDavid Howells 	/* Work out a lot of parameters */
91554ceac45SDavid Howells 	if (server->rsize == 0)
91654ceac45SDavid Howells 		server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
91754ceac45SDavid Howells 	if (server->wsize == 0)
91854ceac45SDavid Howells 		server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
91954ceac45SDavid Howells 
92054ceac45SDavid Howells 	if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
92154ceac45SDavid Howells 		server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
92254ceac45SDavid Howells 	if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
92354ceac45SDavid Howells 		server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
92454ceac45SDavid Howells 
92554ceac45SDavid Howells 	max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
92654ceac45SDavid Howells 	if (server->rsize > max_rpc_payload)
92754ceac45SDavid Howells 		server->rsize = max_rpc_payload;
92854ceac45SDavid Howells 	if (server->rsize > NFS_MAX_FILE_IO_SIZE)
92954ceac45SDavid Howells 		server->rsize = NFS_MAX_FILE_IO_SIZE;
93054ceac45SDavid Howells 	server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
931e0bf68ddSPeter Zijlstra 
932d993831fSJens Axboe 	server->backing_dev_info.name = "nfs";
93354ceac45SDavid Howells 	server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
93454ceac45SDavid Howells 
93554ceac45SDavid Howells 	if (server->wsize > max_rpc_payload)
93654ceac45SDavid Howells 		server->wsize = max_rpc_payload;
93754ceac45SDavid Howells 	if (server->wsize > NFS_MAX_FILE_IO_SIZE)
93854ceac45SDavid Howells 		server->wsize = NFS_MAX_FILE_IO_SIZE;
93954ceac45SDavid Howells 	server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
94085e174baSRicardo Labiaga 	set_pnfs_layoutdriver(server, fsinfo->layouttype);
94185e174baSRicardo Labiaga 
94254ceac45SDavid Howells 	server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
94354ceac45SDavid Howells 
94454ceac45SDavid Howells 	server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
94556e4ebf8SBryan Schumaker 	if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
94656e4ebf8SBryan Schumaker 		server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
94754ceac45SDavid Howells 	if (server->dtsize > server->rsize)
94854ceac45SDavid Howells 		server->dtsize = server->rsize;
94954ceac45SDavid Howells 
95054ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_NOAC) {
95154ceac45SDavid Howells 		server->acregmin = server->acregmax = 0;
95254ceac45SDavid Howells 		server->acdirmin = server->acdirmax = 0;
95354ceac45SDavid Howells 	}
95454ceac45SDavid Howells 
95554ceac45SDavid Howells 	server->maxfilesize = fsinfo->maxfilesize;
95654ceac45SDavid Howells 
9576b96724eSRicardo Labiaga 	server->time_delta = fsinfo->time_delta;
9586b96724eSRicardo Labiaga 
95954ceac45SDavid Howells 	/* We're airborne Set socket buffersize */
96054ceac45SDavid Howells 	rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
96154ceac45SDavid Howells }
96254ceac45SDavid Howells 
96354ceac45SDavid Howells /*
96454ceac45SDavid Howells  * Probe filesystem information, including the FSID on v2/v3
96554ceac45SDavid Howells  */
96654ceac45SDavid Howells static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
96754ceac45SDavid Howells {
96854ceac45SDavid Howells 	struct nfs_fsinfo fsinfo;
96954ceac45SDavid Howells 	struct nfs_client *clp = server->nfs_client;
97054ceac45SDavid Howells 	int error;
97154ceac45SDavid Howells 
97254ceac45SDavid Howells 	dprintk("--> nfs_probe_fsinfo()\n");
97354ceac45SDavid Howells 
97454ceac45SDavid Howells 	if (clp->rpc_ops->set_capabilities != NULL) {
97554ceac45SDavid Howells 		error = clp->rpc_ops->set_capabilities(server, mntfh);
97654ceac45SDavid Howells 		if (error < 0)
97754ceac45SDavid Howells 			goto out_error;
97854ceac45SDavid Howells 	}
97954ceac45SDavid Howells 
98054ceac45SDavid Howells 	fsinfo.fattr = fattr;
98185e174baSRicardo Labiaga 	fsinfo.layouttype = 0;
98254ceac45SDavid Howells 	error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
98354ceac45SDavid Howells 	if (error < 0)
98454ceac45SDavid Howells 		goto out_error;
98554ceac45SDavid Howells 
98654ceac45SDavid Howells 	nfs_server_set_fsinfo(server, &fsinfo);
98754ceac45SDavid Howells 
98854ceac45SDavid Howells 	/* Get some general file system info */
98954ceac45SDavid Howells 	if (server->namelen == 0) {
99054ceac45SDavid Howells 		struct nfs_pathconf pathinfo;
99154ceac45SDavid Howells 
99254ceac45SDavid Howells 		pathinfo.fattr = fattr;
99354ceac45SDavid Howells 		nfs_fattr_init(fattr);
99454ceac45SDavid Howells 
99554ceac45SDavid Howells 		if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
99654ceac45SDavid Howells 			server->namelen = pathinfo.max_namelen;
99754ceac45SDavid Howells 	}
99854ceac45SDavid Howells 
99954ceac45SDavid Howells 	dprintk("<-- nfs_probe_fsinfo() = 0\n");
100054ceac45SDavid Howells 	return 0;
100154ceac45SDavid Howells 
100254ceac45SDavid Howells out_error:
100354ceac45SDavid Howells 	dprintk("nfs_probe_fsinfo: error = %d\n", -error);
100454ceac45SDavid Howells 	return error;
100554ceac45SDavid Howells }
100654ceac45SDavid Howells 
100754ceac45SDavid Howells /*
100854ceac45SDavid Howells  * Copy useful information when duplicating a server record
100954ceac45SDavid Howells  */
101054ceac45SDavid Howells static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
101154ceac45SDavid Howells {
101254ceac45SDavid Howells 	target->flags = source->flags;
1013356e76b8SChuck Lever 	target->rsize = source->rsize;
1014356e76b8SChuck Lever 	target->wsize = source->wsize;
101554ceac45SDavid Howells 	target->acregmin = source->acregmin;
101654ceac45SDavid Howells 	target->acregmax = source->acregmax;
101754ceac45SDavid Howells 	target->acdirmin = source->acdirmin;
101854ceac45SDavid Howells 	target->acdirmax = source->acdirmax;
101954ceac45SDavid Howells 	target->caps = source->caps;
10202df54806SDavid Howells 	target->options = source->options;
102154ceac45SDavid Howells }
102254ceac45SDavid Howells 
1023fca5238eSChuck Lever static void nfs_server_insert_lists(struct nfs_server *server)
1024fca5238eSChuck Lever {
1025fca5238eSChuck Lever 	struct nfs_client *clp = server->nfs_client;
1026fca5238eSChuck Lever 
1027fca5238eSChuck Lever 	spin_lock(&nfs_client_lock);
1028fca5238eSChuck Lever 	list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
1029fca5238eSChuck Lever 	list_add_tail(&server->master_link, &nfs_volume_list);
1030d3b4c9d7SAndy Adamson 	clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
1031fca5238eSChuck Lever 	spin_unlock(&nfs_client_lock);
1032fca5238eSChuck Lever 
1033fca5238eSChuck Lever }
1034fca5238eSChuck Lever 
1035fca5238eSChuck Lever static void nfs_server_remove_lists(struct nfs_server *server)
1036fca5238eSChuck Lever {
1037d3b4c9d7SAndy Adamson 	struct nfs_client *clp = server->nfs_client;
1038d3b4c9d7SAndy Adamson 
1039fca5238eSChuck Lever 	spin_lock(&nfs_client_lock);
1040fca5238eSChuck Lever 	list_del_rcu(&server->client_link);
1041d3b4c9d7SAndy Adamson 	if (clp && list_empty(&clp->cl_superblocks))
1042d3b4c9d7SAndy Adamson 		set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
1043fca5238eSChuck Lever 	list_del(&server->master_link);
1044fca5238eSChuck Lever 	spin_unlock(&nfs_client_lock);
1045fca5238eSChuck Lever 
1046fca5238eSChuck Lever 	synchronize_rcu();
1047fca5238eSChuck Lever }
1048fca5238eSChuck Lever 
104954ceac45SDavid Howells /*
105054ceac45SDavid Howells  * Allocate and initialise a server record
105154ceac45SDavid Howells  */
105254ceac45SDavid Howells static struct nfs_server *nfs_alloc_server(void)
105354ceac45SDavid Howells {
105454ceac45SDavid Howells 	struct nfs_server *server;
105554ceac45SDavid Howells 
105654ceac45SDavid Howells 	server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
105754ceac45SDavid Howells 	if (!server)
105854ceac45SDavid Howells 		return NULL;
105954ceac45SDavid Howells 
106054ceac45SDavid Howells 	server->client = server->client_acl = ERR_PTR(-EINVAL);
106154ceac45SDavid Howells 
106254ceac45SDavid Howells 	/* Zero out the NFS state stuff */
106354ceac45SDavid Howells 	INIT_LIST_HEAD(&server->client_link);
106454ceac45SDavid Howells 	INIT_LIST_HEAD(&server->master_link);
1065d3978bb3SChuck Lever 	INIT_LIST_HEAD(&server->delegations);
106654ceac45SDavid Howells 
1067ef818a28SSteve Dickson 	atomic_set(&server->active, 0);
1068ef818a28SSteve Dickson 
106954ceac45SDavid Howells 	server->io_stats = nfs_alloc_iostats();
107054ceac45SDavid Howells 	if (!server->io_stats) {
107154ceac45SDavid Howells 		kfree(server);
107254ceac45SDavid Howells 		return NULL;
107354ceac45SDavid Howells 	}
107454ceac45SDavid Howells 
107548d07649SJens Axboe 	if (bdi_init(&server->backing_dev_info)) {
107648d07649SJens Axboe 		nfs_free_iostats(server->io_stats);
107748d07649SJens Axboe 		kfree(server);
107848d07649SJens Axboe 		return NULL;
107948d07649SJens Axboe 	}
108048d07649SJens Axboe 
1081f7e8917aSFred Isaman 	pnfs_init_server(server);
1082f7e8917aSFred Isaman 
108354ceac45SDavid Howells 	return server;
108454ceac45SDavid Howells }
108554ceac45SDavid Howells 
108654ceac45SDavid Howells /*
108754ceac45SDavid Howells  * Free up a server record
108854ceac45SDavid Howells  */
108954ceac45SDavid Howells void nfs_free_server(struct nfs_server *server)
109054ceac45SDavid Howells {
109154ceac45SDavid Howells 	dprintk("--> nfs_free_server()\n");
109254ceac45SDavid Howells 
1093fca5238eSChuck Lever 	nfs_server_remove_lists(server);
109485e174baSRicardo Labiaga 	unset_pnfs_layoutdriver(server);
109554ceac45SDavid Howells 
109654ceac45SDavid Howells 	if (server->destroy != NULL)
109754ceac45SDavid Howells 		server->destroy(server);
10985cef338bSTrond Myklebust 
10995cef338bSTrond Myklebust 	if (!IS_ERR(server->client_acl))
11005cef338bSTrond Myklebust 		rpc_shutdown_client(server->client_acl);
110154ceac45SDavid Howells 	if (!IS_ERR(server->client))
110254ceac45SDavid Howells 		rpc_shutdown_client(server->client);
110354ceac45SDavid Howells 
110454ceac45SDavid Howells 	nfs_put_client(server->nfs_client);
110554ceac45SDavid Howells 
110654ceac45SDavid Howells 	nfs_free_iostats(server->io_stats);
1107e0bf68ddSPeter Zijlstra 	bdi_destroy(&server->backing_dev_info);
110854ceac45SDavid Howells 	kfree(server);
110954ceac45SDavid Howells 	nfs_release_automount_timer();
111054ceac45SDavid Howells 	dprintk("<-- nfs_free_server()\n");
111154ceac45SDavid Howells }
111254ceac45SDavid Howells 
111354ceac45SDavid Howells /*
111454ceac45SDavid Howells  * Create a version 2 or 3 volume record
111554ceac45SDavid Howells  * - keyed on server and FSID
111654ceac45SDavid Howells  */
11172283f8d6S\"Talpey, Thomas\ struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
111854ceac45SDavid Howells 				     struct nfs_fh *mntfh)
111954ceac45SDavid Howells {
112054ceac45SDavid Howells 	struct nfs_server *server;
1121fbca779aSTrond Myklebust 	struct nfs_fattr *fattr;
112254ceac45SDavid Howells 	int error;
112354ceac45SDavid Howells 
112454ceac45SDavid Howells 	server = nfs_alloc_server();
112554ceac45SDavid Howells 	if (!server)
112654ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
112754ceac45SDavid Howells 
1128fbca779aSTrond Myklebust 	error = -ENOMEM;
1129fbca779aSTrond Myklebust 	fattr = nfs_alloc_fattr();
1130fbca779aSTrond Myklebust 	if (fattr == NULL)
1131fbca779aSTrond Myklebust 		goto error;
1132fbca779aSTrond Myklebust 
113354ceac45SDavid Howells 	/* Get a client representation */
113454ceac45SDavid Howells 	error = nfs_init_server(server, data);
113554ceac45SDavid Howells 	if (error < 0)
113654ceac45SDavid Howells 		goto error;
113754ceac45SDavid Howells 
113854ceac45SDavid Howells 	BUG_ON(!server->nfs_client);
113954ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops);
114054ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
114154ceac45SDavid Howells 
114254ceac45SDavid Howells 	/* Probe the root fh to retrieve its FSID */
1143fbca779aSTrond Myklebust 	error = nfs_probe_fsinfo(server, mntfh, fattr);
114454ceac45SDavid Howells 	if (error < 0)
114554ceac45SDavid Howells 		goto error;
114654af3bb5STrond Myklebust 	if (server->nfs_client->rpc_ops->version == 3) {
114754af3bb5STrond Myklebust 		if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
114854af3bb5STrond Myklebust 			server->namelen = NFS3_MAXNAMLEN;
114954af3bb5STrond Myklebust 		if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
115054af3bb5STrond Myklebust 			server->caps |= NFS_CAP_READDIRPLUS;
115154af3bb5STrond Myklebust 	} else {
115254af3bb5STrond Myklebust 		if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
115354af3bb5STrond Myklebust 			server->namelen = NFS2_MAXNAMLEN;
115454af3bb5STrond Myklebust 	}
115554af3bb5STrond Myklebust 
1156fbca779aSTrond Myklebust 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
1157fbca779aSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
115854ceac45SDavid Howells 		if (error < 0) {
115954ceac45SDavid Howells 			dprintk("nfs_create_server: getattr error = %d\n", -error);
116054ceac45SDavid Howells 			goto error;
116154ceac45SDavid Howells 		}
116254ceac45SDavid Howells 	}
1163fbca779aSTrond Myklebust 	memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
116454ceac45SDavid Howells 
11656daabf1bSDavid Howells 	dprintk("Server FSID: %llx:%llx\n",
11666daabf1bSDavid Howells 		(unsigned long long) server->fsid.major,
11676daabf1bSDavid Howells 		(unsigned long long) server->fsid.minor);
116854ceac45SDavid Howells 
1169fca5238eSChuck Lever 	nfs_server_insert_lists(server);
117054ceac45SDavid Howells 	server->mount_time = jiffies;
1171fbca779aSTrond Myklebust 	nfs_free_fattr(fattr);
117254ceac45SDavid Howells 	return server;
117354ceac45SDavid Howells 
117454ceac45SDavid Howells error:
1175fbca779aSTrond Myklebust 	nfs_free_fattr(fattr);
117654ceac45SDavid Howells 	nfs_free_server(server);
117754ceac45SDavid Howells 	return ERR_PTR(error);
117854ceac45SDavid Howells }
117954ceac45SDavid Howells 
118054ceac45SDavid Howells #ifdef CONFIG_NFS_V4
118154ceac45SDavid Howells /*
1182c36fca52SAndy Adamson  * NFSv4.0 callback thread helper
1183c36fca52SAndy Adamson  *
1184c36fca52SAndy Adamson  * Find a client by IP address, protocol version, and minorversion
1185c36fca52SAndy Adamson  *
1186c36fca52SAndy Adamson  * Called from the pg_authenticate method. The callback identifier
1187c36fca52SAndy Adamson  * is not used as it has not been decoded.
1188c36fca52SAndy Adamson  *
1189c36fca52SAndy Adamson  * Returns NULL if no such client
1190c36fca52SAndy Adamson  */
1191c36fca52SAndy Adamson struct nfs_client *
1192c36fca52SAndy Adamson nfs4_find_client_no_ident(const struct sockaddr *addr)
1193c36fca52SAndy Adamson {
1194c36fca52SAndy Adamson 	struct nfs_client *clp;
1195c36fca52SAndy Adamson 
1196c36fca52SAndy Adamson 	spin_lock(&nfs_client_lock);
1197c36fca52SAndy Adamson 	list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
1198c36fca52SAndy Adamson 		if (nfs4_cb_match_client(addr, clp, 0) == false)
1199c36fca52SAndy Adamson 			continue;
1200c36fca52SAndy Adamson 		atomic_inc(&clp->cl_count);
1201c36fca52SAndy Adamson 		spin_unlock(&nfs_client_lock);
1202c36fca52SAndy Adamson 		return clp;
1203c36fca52SAndy Adamson 	}
1204c36fca52SAndy Adamson 	spin_unlock(&nfs_client_lock);
1205c36fca52SAndy Adamson 	return NULL;
1206c36fca52SAndy Adamson }
1207c36fca52SAndy Adamson 
1208c36fca52SAndy Adamson /*
1209c36fca52SAndy Adamson  * NFSv4.0 callback thread helper
1210c36fca52SAndy Adamson  *
1211c36fca52SAndy Adamson  * Find a client by callback identifier
1212c36fca52SAndy Adamson  */
1213c36fca52SAndy Adamson struct nfs_client *
1214c36fca52SAndy Adamson nfs4_find_client_ident(int cb_ident)
1215c36fca52SAndy Adamson {
1216c36fca52SAndy Adamson 	struct nfs_client *clp;
1217c36fca52SAndy Adamson 
1218c36fca52SAndy Adamson 	spin_lock(&nfs_client_lock);
1219c36fca52SAndy Adamson 	clp = idr_find(&cb_ident_idr, cb_ident);
1220c36fca52SAndy Adamson 	if (clp)
1221c36fca52SAndy Adamson 		atomic_inc(&clp->cl_count);
1222c36fca52SAndy Adamson 	spin_unlock(&nfs_client_lock);
1223c36fca52SAndy Adamson 	return clp;
1224c36fca52SAndy Adamson }
1225c36fca52SAndy Adamson 
1226c36fca52SAndy Adamson #if defined(CONFIG_NFS_V4_1)
1227c36fca52SAndy Adamson /*
1228c36fca52SAndy Adamson  * NFSv4.1 callback thread helper
1229c36fca52SAndy Adamson  * For CB_COMPOUND calls, find a client by IP address, protocol version,
1230c36fca52SAndy Adamson  * minorversion, and sessionID
1231c36fca52SAndy Adamson  *
1232c36fca52SAndy Adamson  * Returns NULL if no such client
1233c36fca52SAndy Adamson  */
1234c36fca52SAndy Adamson struct nfs_client *
1235c36fca52SAndy Adamson nfs4_find_client_sessionid(const struct sockaddr *addr,
1236778be232SAndy Adamson 			   struct nfs4_sessionid *sid)
1237c36fca52SAndy Adamson {
1238c36fca52SAndy Adamson 	struct nfs_client *clp;
1239c36fca52SAndy Adamson 
1240c36fca52SAndy Adamson 	spin_lock(&nfs_client_lock);
1241c36fca52SAndy Adamson 	list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
1242c36fca52SAndy Adamson 		if (nfs4_cb_match_client(addr, clp, 1) == false)
1243c36fca52SAndy Adamson 			continue;
1244c36fca52SAndy Adamson 
1245c36fca52SAndy Adamson 		if (!nfs4_has_session(clp))
1246c36fca52SAndy Adamson 			continue;
1247c36fca52SAndy Adamson 
1248778be232SAndy Adamson 		/* Match sessionid*/
1249778be232SAndy Adamson 		if (memcmp(clp->cl_session->sess_id.data,
1250778be232SAndy Adamson 		    sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
1251c36fca52SAndy Adamson 			continue;
1252c36fca52SAndy Adamson 
1253c36fca52SAndy Adamson 		atomic_inc(&clp->cl_count);
1254c36fca52SAndy Adamson 		spin_unlock(&nfs_client_lock);
1255c36fca52SAndy Adamson 		return clp;
1256c36fca52SAndy Adamson 	}
1257c36fca52SAndy Adamson 	spin_unlock(&nfs_client_lock);
1258c36fca52SAndy Adamson 	return NULL;
1259c36fca52SAndy Adamson }
1260c36fca52SAndy Adamson 
1261c36fca52SAndy Adamson #else /* CONFIG_NFS_V4_1 */
1262c36fca52SAndy Adamson 
1263c36fca52SAndy Adamson struct nfs_client *
1264c36fca52SAndy Adamson nfs4_find_client_sessionid(const struct sockaddr *addr,
1265778be232SAndy Adamson 			   struct nfs4_sessionid *sid)
1266c36fca52SAndy Adamson {
1267c36fca52SAndy Adamson 	return NULL;
1268c36fca52SAndy Adamson }
1269c36fca52SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1270c36fca52SAndy Adamson 
1271c36fca52SAndy Adamson /*
12729bdaa86dSBenny Halevy  * Initialize the NFS4 callback service
12739bdaa86dSBenny Halevy  */
12749bdaa86dSBenny Halevy static int nfs4_init_callback(struct nfs_client *clp)
12759bdaa86dSBenny Halevy {
12769bdaa86dSBenny Halevy 	int error;
12779bdaa86dSBenny Halevy 
12789bdaa86dSBenny Halevy 	if (clp->rpc_ops->version == 4) {
12790b5b7ae0SAndy Adamson 		if (nfs4_has_session(clp)) {
12800b5b7ae0SAndy Adamson 			error = xprt_setup_backchannel(
12810b5b7ae0SAndy Adamson 						clp->cl_rpcclient->cl_xprt,
12820b5b7ae0SAndy Adamson 						NFS41_BC_MIN_CALLBACKS);
12830b5b7ae0SAndy Adamson 			if (error < 0)
12840b5b7ae0SAndy Adamson 				return error;
12850b5b7ae0SAndy Adamson 		}
12860b5b7ae0SAndy Adamson 
128797dc1359STrond Myklebust 		error = nfs_callback_up(clp->cl_mvops->minor_version,
128871468513SBenny Halevy 					clp->cl_rpcclient->cl_xprt);
12899bdaa86dSBenny Halevy 		if (error < 0) {
12909bdaa86dSBenny Halevy 			dprintk("%s: failed to start callback. Error = %d\n",
12919bdaa86dSBenny Halevy 				__func__, error);
12929bdaa86dSBenny Halevy 			return error;
12939bdaa86dSBenny Halevy 		}
12949bdaa86dSBenny Halevy 		__set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
12959bdaa86dSBenny Halevy 	}
12969bdaa86dSBenny Halevy 	return 0;
12979bdaa86dSBenny Halevy }
12989bdaa86dSBenny Halevy 
12999bdaa86dSBenny Halevy /*
1300557134a3SAndy Adamson  * Initialize the minor version specific parts of an NFS4 client record
1301557134a3SAndy Adamson  */
1302557134a3SAndy Adamson static int nfs4_init_client_minor_version(struct nfs_client *clp)
1303557134a3SAndy Adamson {
1304557134a3SAndy Adamson #if defined(CONFIG_NFS_V4_1)
130597dc1359STrond Myklebust 	if (clp->cl_mvops->minor_version) {
1306557134a3SAndy Adamson 		struct nfs4_session *session = NULL;
1307557134a3SAndy Adamson 		/*
1308557134a3SAndy Adamson 		 * Create the session and mark it expired.
1309557134a3SAndy Adamson 		 * When a SEQUENCE operation encounters the expired session
1310557134a3SAndy Adamson 		 * it will do session recovery to initialize it.
1311557134a3SAndy Adamson 		 */
1312557134a3SAndy Adamson 		session = nfs4_alloc_session(clp);
1313557134a3SAndy Adamson 		if (!session)
1314557134a3SAndy Adamson 			return -ENOMEM;
1315557134a3SAndy Adamson 
1316557134a3SAndy Adamson 		clp->cl_session = session;
1317fe74ba3aSTrond Myklebust 		/*
1318fe74ba3aSTrond Myklebust 		 * The create session reply races with the server back
1319fe74ba3aSTrond Myklebust 		 * channel probe. Mark the client NFS_CS_SESSION_INITING
1320fe74ba3aSTrond Myklebust 		 * so that the client back channel can find the
1321fe74ba3aSTrond Myklebust 		 * nfs_client struct
1322fe74ba3aSTrond Myklebust 		 */
1323fe74ba3aSTrond Myklebust 		clp->cl_cons_state = NFS_CS_SESSION_INITING;
1324557134a3SAndy Adamson 	}
1325557134a3SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1326557134a3SAndy Adamson 
132771468513SBenny Halevy 	return nfs4_init_callback(clp);
1328557134a3SAndy Adamson }
1329557134a3SAndy Adamson 
1330557134a3SAndy Adamson /*
133154ceac45SDavid Howells  * Initialise an NFS4 client record
133254ceac45SDavid Howells  */
133345a52a02SAndy Adamson int nfs4_init_client(struct nfs_client *clp,
133433170233STrond Myklebust 		     const struct rpc_timeout *timeparms,
13357d9ac06fSJ. Bruce Fields 		     const char *ip_addr,
1336d740351bSChuck Lever 		     rpc_authflavor_t authflavour,
133745a52a02SAndy Adamson 		     int noresvport)
133854ceac45SDavid Howells {
133954ceac45SDavid Howells 	int error;
134054ceac45SDavid Howells 
134154ceac45SDavid Howells 	if (clp->cl_cons_state == NFS_CS_READY) {
134254ceac45SDavid Howells 		/* the client is initialised already */
134354ceac45SDavid Howells 		dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
134454ceac45SDavid Howells 		return 0;
134554ceac45SDavid Howells 	}
134654ceac45SDavid Howells 
134754ceac45SDavid Howells 	/* Check NFS protocol revision and initialize RPC op vector */
134854ceac45SDavid Howells 	clp->rpc_ops = &nfs_v4_clientops;
134954ceac45SDavid Howells 
135059dca3b2STrond Myklebust 	error = nfs_create_rpc_client(clp, timeparms, authflavour,
135145a52a02SAndy Adamson 				      1, noresvport);
135254ceac45SDavid Howells 	if (error < 0)
135354ceac45SDavid Howells 		goto error;
1354f4373bf9SBen Hutchings 	strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
135554ceac45SDavid Howells 
135654ceac45SDavid Howells 	error = nfs_idmap_new(clp);
135754ceac45SDavid Howells 	if (error < 0) {
135854ceac45SDavid Howells 		dprintk("%s: failed to create idmapper. Error = %d\n",
13593110ff80SHarvey Harrison 			__func__, error);
136054ceac45SDavid Howells 		goto error;
136154ceac45SDavid Howells 	}
13629c5bf38dSTrond Myklebust 	__set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
136354ceac45SDavid Howells 
1364557134a3SAndy Adamson 	error = nfs4_init_client_minor_version(clp);
1365557134a3SAndy Adamson 	if (error < 0)
1366557134a3SAndy Adamson 		goto error;
1367557134a3SAndy Adamson 
136876db6d95SAndy Adamson 	if (!nfs4_has_session(clp))
136954ceac45SDavid Howells 		nfs_mark_client_ready(clp, NFS_CS_READY);
137054ceac45SDavid Howells 	return 0;
137154ceac45SDavid Howells 
137254ceac45SDavid Howells error:
137354ceac45SDavid Howells 	nfs_mark_client_ready(clp, error);
137454ceac45SDavid Howells 	dprintk("<-- nfs4_init_client() = xerror %d\n", error);
137554ceac45SDavid Howells 	return error;
137654ceac45SDavid Howells }
137754ceac45SDavid Howells 
137854ceac45SDavid Howells /*
137954ceac45SDavid Howells  * Set up an NFS4 client
138054ceac45SDavid Howells  */
138154ceac45SDavid Howells static int nfs4_set_client(struct nfs_server *server,
1382dcecae0fSChuck Lever 		const char *hostname,
1383dcecae0fSChuck Lever 		const struct sockaddr *addr,
1384dcecae0fSChuck Lever 		const size_t addrlen,
13857d9ac06fSJ. Bruce Fields 		const char *ip_addr,
138654ceac45SDavid Howells 		rpc_authflavor_t authflavour,
138794a417f3SBenny Halevy 		int proto, const struct rpc_timeout *timeparms,
138894a417f3SBenny Halevy 		u32 minorversion)
138954ceac45SDavid Howells {
13903a498026STrond Myklebust 	struct nfs_client_initdata cl_init = {
13913a498026STrond Myklebust 		.hostname = hostname,
1392dcecae0fSChuck Lever 		.addr = addr,
1393dcecae0fSChuck Lever 		.addrlen = addrlen,
139440c55319STrond Myklebust 		.rpc_ops = &nfs_v4_clientops,
139559dca3b2STrond Myklebust 		.proto = proto,
13965aae4a9aSBenny Halevy 		.minorversion = minorversion,
13973a498026STrond Myklebust 	};
139854ceac45SDavid Howells 	struct nfs_client *clp;
139954ceac45SDavid Howells 	int error;
140054ceac45SDavid Howells 
140154ceac45SDavid Howells 	dprintk("--> nfs4_set_client()\n");
140254ceac45SDavid Howells 
140354ceac45SDavid Howells 	/* Allocate or find a client reference we can use */
140445a52a02SAndy Adamson 	clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour,
140545a52a02SAndy Adamson 			     server->flags & NFS_MOUNT_NORESVPORT);
140654ceac45SDavid Howells 	if (IS_ERR(clp)) {
140754ceac45SDavid Howells 		error = PTR_ERR(clp);
140854ceac45SDavid Howells 		goto error;
140954ceac45SDavid Howells 	}
141054ceac45SDavid Howells 
1411d6fb79d4SAndy Adamson 	/*
1412d6fb79d4SAndy Adamson 	 * Query for the lease time on clientid setup or renewal
1413d6fb79d4SAndy Adamson 	 *
1414d6fb79d4SAndy Adamson 	 * Note that this will be set on nfs_clients that were created
1415d6fb79d4SAndy Adamson 	 * only for the DS role and did not set this bit, but now will
1416d6fb79d4SAndy Adamson 	 * serve a dual role.
1417d6fb79d4SAndy Adamson 	 */
1418d6fb79d4SAndy Adamson 	set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
1419d6fb79d4SAndy Adamson 
142054ceac45SDavid Howells 	server->nfs_client = clp;
142154ceac45SDavid Howells 	dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
142254ceac45SDavid Howells 	return 0;
142354ceac45SDavid Howells error:
142454ceac45SDavid Howells 	dprintk("<-- nfs4_set_client() = xerror %d\n", error);
142554ceac45SDavid Howells 	return error;
142654ceac45SDavid Howells }
142754ceac45SDavid Howells 
1428d83217c1SAndy Adamson /*
1429d83217c1SAndy Adamson  * Set up a pNFS Data Server client.
1430d83217c1SAndy Adamson  *
1431d83217c1SAndy Adamson  * Return any existing nfs_client that matches server address,port,version
1432d83217c1SAndy Adamson  * and minorversion.
1433d83217c1SAndy Adamson  *
1434d83217c1SAndy Adamson  * For a new nfs_client, use a soft mount (default), a low retrans and a
1435d83217c1SAndy Adamson  * low timeout interval so that if a connection is lost, we retry through
1436d83217c1SAndy Adamson  * the MDS.
1437d83217c1SAndy Adamson  */
1438d83217c1SAndy Adamson struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
1439d83217c1SAndy Adamson 		const struct sockaddr *ds_addr,
1440d83217c1SAndy Adamson 		int ds_addrlen, int ds_proto)
1441d83217c1SAndy Adamson {
1442d83217c1SAndy Adamson 	struct nfs_client_initdata cl_init = {
1443d83217c1SAndy Adamson 		.addr = ds_addr,
1444d83217c1SAndy Adamson 		.addrlen = ds_addrlen,
1445d83217c1SAndy Adamson 		.rpc_ops = &nfs_v4_clientops,
1446d83217c1SAndy Adamson 		.proto = ds_proto,
1447d83217c1SAndy Adamson 		.minorversion = mds_clp->cl_minorversion,
1448d83217c1SAndy Adamson 	};
1449d83217c1SAndy Adamson 	struct rpc_timeout ds_timeout = {
1450d83217c1SAndy Adamson 		.to_initval = 15 * HZ,
1451d83217c1SAndy Adamson 		.to_maxval = 15 * HZ,
1452d83217c1SAndy Adamson 		.to_retries = 1,
1453d83217c1SAndy Adamson 		.to_exponential = 1,
1454d83217c1SAndy Adamson 	};
1455d83217c1SAndy Adamson 	struct nfs_client *clp;
1456d83217c1SAndy Adamson 
1457d83217c1SAndy Adamson 	/*
1458d83217c1SAndy Adamson 	 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
1459d83217c1SAndy Adamson 	 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
1460d83217c1SAndy Adamson 	 * (section 13.1 RFC 5661).
1461d83217c1SAndy Adamson 	 */
1462d83217c1SAndy Adamson 	clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
1463d83217c1SAndy Adamson 			     mds_clp->cl_rpcclient->cl_auth->au_flavor, 0);
1464d83217c1SAndy Adamson 
1465d83217c1SAndy Adamson 	dprintk("<-- %s %p\n", __func__, clp);
1466d83217c1SAndy Adamson 	return clp;
1467d83217c1SAndy Adamson }
1468d83217c1SAndy Adamson EXPORT_SYMBOL(nfs4_set_ds_client);
1469557134a3SAndy Adamson 
1470557134a3SAndy Adamson /*
147196b09e02SAndy Adamson  * Session has been established, and the client marked ready.
147296b09e02SAndy Adamson  * Set the mount rsize and wsize with negotiated fore channel
147396b09e02SAndy Adamson  * attributes which will be bound checked in nfs_server_set_fsinfo.
147496b09e02SAndy Adamson  */
147596b09e02SAndy Adamson static void nfs4_session_set_rwsize(struct nfs_server *server)
147696b09e02SAndy Adamson {
147796b09e02SAndy Adamson #ifdef CONFIG_NFS_V4_1
14782449ea2eSAlexandros Batsakis 	struct nfs4_session *sess;
14792449ea2eSAlexandros Batsakis 	u32 server_resp_sz;
14802449ea2eSAlexandros Batsakis 	u32 server_rqst_sz;
14812449ea2eSAlexandros Batsakis 
148296b09e02SAndy Adamson 	if (!nfs4_has_session(server->nfs_client))
148396b09e02SAndy Adamson 		return;
14842449ea2eSAlexandros Batsakis 	sess = server->nfs_client->cl_session;
14852449ea2eSAlexandros Batsakis 	server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
14862449ea2eSAlexandros Batsakis 	server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
14872449ea2eSAlexandros Batsakis 
14882449ea2eSAlexandros Batsakis 	if (server->rsize > server_resp_sz)
14892449ea2eSAlexandros Batsakis 		server->rsize = server_resp_sz;
14902449ea2eSAlexandros Batsakis 	if (server->wsize > server_rqst_sz)
14912449ea2eSAlexandros Batsakis 		server->wsize = server_rqst_sz;
149296b09e02SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
149396b09e02SAndy Adamson }
149496b09e02SAndy Adamson 
149544950b67STrond Myklebust static int nfs4_server_common_setup(struct nfs_server *server,
149644950b67STrond Myklebust 		struct nfs_fh *mntfh)
149744950b67STrond Myklebust {
149844950b67STrond Myklebust 	struct nfs_fattr *fattr;
149944950b67STrond Myklebust 	int error;
150044950b67STrond Myklebust 
150144950b67STrond Myklebust 	BUG_ON(!server->nfs_client);
150244950b67STrond Myklebust 	BUG_ON(!server->nfs_client->rpc_ops);
150344950b67STrond Myklebust 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
150444950b67STrond Myklebust 
150594de8b27SAndy Adamson 	/* data servers support only a subset of NFSv4.1 */
150694de8b27SAndy Adamson 	if (is_ds_only_client(server->nfs_client))
150794de8b27SAndy Adamson 		return -EPROTONOSUPPORT;
150894de8b27SAndy Adamson 
150944950b67STrond Myklebust 	fattr = nfs_alloc_fattr();
151044950b67STrond Myklebust 	if (fattr == NULL)
151144950b67STrond Myklebust 		return -ENOMEM;
151244950b67STrond Myklebust 
151344950b67STrond Myklebust 	/* We must ensure the session is initialised first */
151444950b67STrond Myklebust 	error = nfs4_init_session(server);
151544950b67STrond Myklebust 	if (error < 0)
151644950b67STrond Myklebust 		goto out;
151744950b67STrond Myklebust 
151844950b67STrond Myklebust 	/* Probe the root fh to retrieve its FSID and filehandle */
151944950b67STrond Myklebust 	error = nfs4_get_rootfh(server, mntfh);
152044950b67STrond Myklebust 	if (error < 0)
152144950b67STrond Myklebust 		goto out;
152244950b67STrond Myklebust 
152344950b67STrond Myklebust 	dprintk("Server FSID: %llx:%llx\n",
152444950b67STrond Myklebust 			(unsigned long long) server->fsid.major,
152544950b67STrond Myklebust 			(unsigned long long) server->fsid.minor);
152644950b67STrond Myklebust 	dprintk("Mount FH: %d\n", mntfh->size);
152744950b67STrond Myklebust 
152844950b67STrond Myklebust 	nfs4_session_set_rwsize(server);
152944950b67STrond Myklebust 
153044950b67STrond Myklebust 	error = nfs_probe_fsinfo(server, mntfh, fattr);
153144950b67STrond Myklebust 	if (error < 0)
153244950b67STrond Myklebust 		goto out;
153344950b67STrond Myklebust 
153444950b67STrond Myklebust 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
153544950b67STrond Myklebust 		server->namelen = NFS4_MAXNAMLEN;
153644950b67STrond Myklebust 
1537fca5238eSChuck Lever 	nfs_server_insert_lists(server);
153844950b67STrond Myklebust 	server->mount_time = jiffies;
153944950b67STrond Myklebust out:
154044950b67STrond Myklebust 	nfs_free_fattr(fattr);
154144950b67STrond Myklebust 	return error;
154244950b67STrond Myklebust }
154344950b67STrond Myklebust 
154496b09e02SAndy Adamson /*
154554ceac45SDavid Howells  * Create a version 4 volume record
154654ceac45SDavid Howells  */
154754ceac45SDavid Howells static int nfs4_init_server(struct nfs_server *server,
154891ea40b9S\"Talpey, Thomas\ 		const struct nfs_parsed_mount_data *data)
154954ceac45SDavid Howells {
155033170233STrond Myklebust 	struct rpc_timeout timeparms;
155154ceac45SDavid Howells 	int error;
155254ceac45SDavid Howells 
155354ceac45SDavid Howells 	dprintk("--> nfs4_init_server()\n");
155454ceac45SDavid Howells 
155533170233STrond Myklebust 	nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
155633170233STrond Myklebust 			data->timeo, data->retrans);
155733170233STrond Myklebust 
1558542fcc33SChuck Lever 	/* Initialise the client representation from the mount data */
1559542fcc33SChuck Lever 	server->flags = data->flags;
156082f2e547SBryan Schumaker 	server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
156182f2e547SBryan Schumaker 	if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
156282f2e547SBryan Schumaker 			server->caps |= NFS_CAP_READDIRPLUS;
1563b797cac7SDavid Howells 	server->options = data->options;
1564542fcc33SChuck Lever 
156533170233STrond Myklebust 	/* Get a client record */
156633170233STrond Myklebust 	error = nfs4_set_client(server,
156733170233STrond Myklebust 			data->nfs_server.hostname,
156833170233STrond Myklebust 			(const struct sockaddr *)&data->nfs_server.address,
156933170233STrond Myklebust 			data->nfs_server.addrlen,
157033170233STrond Myklebust 			data->client_address,
157133170233STrond Myklebust 			data->auth_flavors[0],
157233170233STrond Myklebust 			data->nfs_server.protocol,
157394a417f3SBenny Halevy 			&timeparms,
157494a417f3SBenny Halevy 			data->minorversion);
157533170233STrond Myklebust 	if (error < 0)
157633170233STrond Myklebust 		goto error;
157733170233STrond Myklebust 
1578b064eca2STrond Myklebust 	/*
1579b064eca2STrond Myklebust 	 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1580b064eca2STrond Myklebust 	 * authentication.
1581b064eca2STrond Myklebust 	 */
1582b064eca2STrond Myklebust 	if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
1583b064eca2STrond Myklebust 		server->caps |= NFS_CAP_UIDGID_NOMAP;
1584b064eca2STrond Myklebust 
158554ceac45SDavid Howells 	if (data->rsize)
158654ceac45SDavid Howells 		server->rsize = nfs_block_size(data->rsize, NULL);
158754ceac45SDavid Howells 	if (data->wsize)
158854ceac45SDavid Howells 		server->wsize = nfs_block_size(data->wsize, NULL);
158954ceac45SDavid Howells 
159054ceac45SDavid Howells 	server->acregmin = data->acregmin * HZ;
159154ceac45SDavid Howells 	server->acregmax = data->acregmax * HZ;
159254ceac45SDavid Howells 	server->acdirmin = data->acdirmin * HZ;
159354ceac45SDavid Howells 	server->acdirmax = data->acdirmax * HZ;
159454ceac45SDavid Howells 
1595f22d6d79SChuck Lever 	server->port = data->nfs_server.port;
1596f22d6d79SChuck Lever 
159733170233STrond Myklebust 	error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
159854ceac45SDavid Howells 
159933170233STrond Myklebust error:
160054ceac45SDavid Howells 	/* Done */
160154ceac45SDavid Howells 	dprintk("<-- nfs4_init_server() = %d\n", error);
160254ceac45SDavid Howells 	return error;
160354ceac45SDavid Howells }
160454ceac45SDavid Howells 
160554ceac45SDavid Howells /*
160654ceac45SDavid Howells  * Create a version 4 volume record
160754ceac45SDavid Howells  * - keyed on server and FSID
160854ceac45SDavid Howells  */
160991ea40b9S\"Talpey, Thomas\ struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
161054ceac45SDavid Howells 				      struct nfs_fh *mntfh)
161154ceac45SDavid Howells {
161254ceac45SDavid Howells 	struct nfs_server *server;
161354ceac45SDavid Howells 	int error;
161454ceac45SDavid Howells 
161554ceac45SDavid Howells 	dprintk("--> nfs4_create_server()\n");
161654ceac45SDavid Howells 
161754ceac45SDavid Howells 	server = nfs_alloc_server();
161854ceac45SDavid Howells 	if (!server)
161954ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
162054ceac45SDavid Howells 
162154ceac45SDavid Howells 	/* set up the general RPC client */
162291ea40b9S\"Talpey, Thomas\ 	error = nfs4_init_server(server, data);
162354ceac45SDavid Howells 	if (error < 0)
162454ceac45SDavid Howells 		goto error;
162554ceac45SDavid Howells 
162644950b67STrond Myklebust 	error = nfs4_server_common_setup(server, mntfh);
1627fccba804STrond Myklebust 	if (error < 0)
1628fccba804STrond Myklebust 		goto error;
1629557134a3SAndy Adamson 
163054ceac45SDavid Howells 	dprintk("<-- nfs4_create_server() = %p\n", server);
163154ceac45SDavid Howells 	return server;
163254ceac45SDavid Howells 
163354ceac45SDavid Howells error:
163454ceac45SDavid Howells 	nfs_free_server(server);
163554ceac45SDavid Howells 	dprintk("<-- nfs4_create_server() = error %d\n", error);
163654ceac45SDavid Howells 	return ERR_PTR(error);
163754ceac45SDavid Howells }
163854ceac45SDavid Howells 
163954ceac45SDavid Howells /*
164054ceac45SDavid Howells  * Create an NFS4 referral server record
164154ceac45SDavid Howells  */
164254ceac45SDavid Howells struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1643f2d0d85eSTrond Myklebust 					       struct nfs_fh *mntfh)
164454ceac45SDavid Howells {
164554ceac45SDavid Howells 	struct nfs_client *parent_client;
164654ceac45SDavid Howells 	struct nfs_server *server, *parent_server;
164754ceac45SDavid Howells 	int error;
164854ceac45SDavid Howells 
164954ceac45SDavid Howells 	dprintk("--> nfs4_create_referral_server()\n");
165054ceac45SDavid Howells 
165154ceac45SDavid Howells 	server = nfs_alloc_server();
165254ceac45SDavid Howells 	if (!server)
165354ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
165454ceac45SDavid Howells 
165554ceac45SDavid Howells 	parent_server = NFS_SB(data->sb);
165654ceac45SDavid Howells 	parent_client = parent_server->nfs_client;
165754ceac45SDavid Howells 
1658542fcc33SChuck Lever 	/* Initialise the client representation from the parent server */
1659542fcc33SChuck Lever 	nfs_server_copy_userdata(server, parent_server);
166062ab460cSTrond Myklebust 	server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
1661542fcc33SChuck Lever 
166254ceac45SDavid Howells 	/* Get a client representation.
166354ceac45SDavid Howells 	 * Note: NFSv4 always uses TCP, */
1664dcecae0fSChuck Lever 	error = nfs4_set_client(server, data->hostname,
16656677d095SChuck Lever 				data->addr,
16666677d095SChuck Lever 				data->addrlen,
16677d9ac06fSJ. Bruce Fields 				parent_client->cl_ipaddr,
166854ceac45SDavid Howells 				data->authflavor,
166954ceac45SDavid Howells 				parent_server->client->cl_xprt->prot,
167094a417f3SBenny Halevy 				parent_server->client->cl_timeout,
167197dc1359STrond Myklebust 				parent_client->cl_mvops->minor_version);
1672297de4f6Sandros@citi.umich.edu 	if (error < 0)
1673297de4f6Sandros@citi.umich.edu 		goto error;
167454ceac45SDavid Howells 
167533170233STrond Myklebust 	error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
167654ceac45SDavid Howells 	if (error < 0)
167754ceac45SDavid Howells 		goto error;
167854ceac45SDavid Howells 
167944950b67STrond Myklebust 	error = nfs4_server_common_setup(server, mntfh);
1680f2d0d85eSTrond Myklebust 	if (error < 0)
1681f2d0d85eSTrond Myklebust 		goto error;
1682f2d0d85eSTrond Myklebust 
168354ceac45SDavid Howells 	dprintk("<-- nfs_create_referral_server() = %p\n", server);
168454ceac45SDavid Howells 	return server;
168554ceac45SDavid Howells 
168654ceac45SDavid Howells error:
168754ceac45SDavid Howells 	nfs_free_server(server);
168854ceac45SDavid Howells 	dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
168954ceac45SDavid Howells 	return ERR_PTR(error);
169054ceac45SDavid Howells }
169154ceac45SDavid Howells 
169254ceac45SDavid Howells #endif /* CONFIG_NFS_V4 */
169354ceac45SDavid Howells 
169454ceac45SDavid Howells /*
169554ceac45SDavid Howells  * Clone an NFS2, NFS3 or NFS4 server record
169654ceac45SDavid Howells  */
169754ceac45SDavid Howells struct nfs_server *nfs_clone_server(struct nfs_server *source,
169854ceac45SDavid Howells 				    struct nfs_fh *fh,
169954ceac45SDavid Howells 				    struct nfs_fattr *fattr)
170054ceac45SDavid Howells {
170154ceac45SDavid Howells 	struct nfs_server *server;
1702fbca779aSTrond Myklebust 	struct nfs_fattr *fattr_fsinfo;
170354ceac45SDavid Howells 	int error;
170454ceac45SDavid Howells 
170554ceac45SDavid Howells 	dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
17066daabf1bSDavid Howells 		(unsigned long long) fattr->fsid.major,
17076daabf1bSDavid Howells 		(unsigned long long) fattr->fsid.minor);
170854ceac45SDavid Howells 
170954ceac45SDavid Howells 	server = nfs_alloc_server();
171054ceac45SDavid Howells 	if (!server)
171154ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
171254ceac45SDavid Howells 
1713fbca779aSTrond Myklebust 	error = -ENOMEM;
1714fbca779aSTrond Myklebust 	fattr_fsinfo = nfs_alloc_fattr();
1715fbca779aSTrond Myklebust 	if (fattr_fsinfo == NULL)
1716fbca779aSTrond Myklebust 		goto out_free_server;
1717fbca779aSTrond Myklebust 
171854ceac45SDavid Howells 	/* Copy data from the source */
171954ceac45SDavid Howells 	server->nfs_client = source->nfs_client;
172054ceac45SDavid Howells 	atomic_inc(&server->nfs_client->cl_count);
172154ceac45SDavid Howells 	nfs_server_copy_userdata(server, source);
172254ceac45SDavid Howells 
172354ceac45SDavid Howells 	server->fsid = fattr->fsid;
172454ceac45SDavid Howells 
172533170233STrond Myklebust 	error = nfs_init_server_rpcclient(server,
172633170233STrond Myklebust 			source->client->cl_timeout,
172733170233STrond Myklebust 			source->client->cl_auth->au_flavor);
172854ceac45SDavid Howells 	if (error < 0)
172954ceac45SDavid Howells 		goto out_free_server;
173054ceac45SDavid Howells 	if (!IS_ERR(source->client_acl))
173154ceac45SDavid Howells 		nfs_init_server_aclclient(server);
173254ceac45SDavid Howells 
173354ceac45SDavid Howells 	/* probe the filesystem info for this server filesystem */
1734fbca779aSTrond Myklebust 	error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
173554ceac45SDavid Howells 	if (error < 0)
173654ceac45SDavid Howells 		goto out_free_server;
173754ceac45SDavid Howells 
173854af3bb5STrond Myklebust 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
173954af3bb5STrond Myklebust 		server->namelen = NFS4_MAXNAMLEN;
174054af3bb5STrond Myklebust 
174154ceac45SDavid Howells 	dprintk("Cloned FSID: %llx:%llx\n",
17426daabf1bSDavid Howells 		(unsigned long long) server->fsid.major,
17436daabf1bSDavid Howells 		(unsigned long long) server->fsid.minor);
174454ceac45SDavid Howells 
174554ceac45SDavid Howells 	error = nfs_start_lockd(server);
174654ceac45SDavid Howells 	if (error < 0)
174754ceac45SDavid Howells 		goto out_free_server;
174854ceac45SDavid Howells 
1749fca5238eSChuck Lever 	nfs_server_insert_lists(server);
175054ceac45SDavid Howells 	server->mount_time = jiffies;
175154ceac45SDavid Howells 
1752fbca779aSTrond Myklebust 	nfs_free_fattr(fattr_fsinfo);
175354ceac45SDavid Howells 	dprintk("<-- nfs_clone_server() = %p\n", server);
175454ceac45SDavid Howells 	return server;
175554ceac45SDavid Howells 
175654ceac45SDavid Howells out_free_server:
1757fbca779aSTrond Myklebust 	nfs_free_fattr(fattr_fsinfo);
175854ceac45SDavid Howells 	nfs_free_server(server);
175954ceac45SDavid Howells 	dprintk("<-- nfs_clone_server() = error %d\n", error);
176054ceac45SDavid Howells 	return ERR_PTR(error);
176154ceac45SDavid Howells }
17626aaca566SDavid Howells 
17636aaca566SDavid Howells #ifdef CONFIG_PROC_FS
17646aaca566SDavid Howells static struct proc_dir_entry *proc_fs_nfs;
17656aaca566SDavid Howells 
17666aaca566SDavid Howells static int nfs_server_list_open(struct inode *inode, struct file *file);
17676aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
17686aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
17696aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v);
17706aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v);
17716aaca566SDavid Howells 
177288e9d34cSJames Morris static const struct seq_operations nfs_server_list_ops = {
17736aaca566SDavid Howells 	.start	= nfs_server_list_start,
17746aaca566SDavid Howells 	.next	= nfs_server_list_next,
17756aaca566SDavid Howells 	.stop	= nfs_server_list_stop,
17766aaca566SDavid Howells 	.show	= nfs_server_list_show,
17776aaca566SDavid Howells };
17786aaca566SDavid Howells 
177900977a59SArjan van de Ven static const struct file_operations nfs_server_list_fops = {
17806aaca566SDavid Howells 	.open		= nfs_server_list_open,
17816aaca566SDavid Howells 	.read		= seq_read,
17826aaca566SDavid Howells 	.llseek		= seq_lseek,
17836aaca566SDavid Howells 	.release	= seq_release,
178434b37235SDenis V. Lunev 	.owner		= THIS_MODULE,
17856aaca566SDavid Howells };
17866aaca566SDavid Howells 
17876aaca566SDavid Howells static int nfs_volume_list_open(struct inode *inode, struct file *file);
17886aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
17896aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
17906aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v);
17916aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v);
17926aaca566SDavid Howells 
179388e9d34cSJames Morris static const struct seq_operations nfs_volume_list_ops = {
17946aaca566SDavid Howells 	.start	= nfs_volume_list_start,
17956aaca566SDavid Howells 	.next	= nfs_volume_list_next,
17966aaca566SDavid Howells 	.stop	= nfs_volume_list_stop,
17976aaca566SDavid Howells 	.show	= nfs_volume_list_show,
17986aaca566SDavid Howells };
17996aaca566SDavid Howells 
180000977a59SArjan van de Ven static const struct file_operations nfs_volume_list_fops = {
18016aaca566SDavid Howells 	.open		= nfs_volume_list_open,
18026aaca566SDavid Howells 	.read		= seq_read,
18036aaca566SDavid Howells 	.llseek		= seq_lseek,
18046aaca566SDavid Howells 	.release	= seq_release,
180534b37235SDenis V. Lunev 	.owner		= THIS_MODULE,
18066aaca566SDavid Howells };
18076aaca566SDavid Howells 
18086aaca566SDavid Howells /*
18096aaca566SDavid Howells  * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
18106aaca566SDavid Howells  * we're dealing
18116aaca566SDavid Howells  */
18126aaca566SDavid Howells static int nfs_server_list_open(struct inode *inode, struct file *file)
18136aaca566SDavid Howells {
18146aaca566SDavid Howells 	struct seq_file *m;
18156aaca566SDavid Howells 	int ret;
18166aaca566SDavid Howells 
18176aaca566SDavid Howells 	ret = seq_open(file, &nfs_server_list_ops);
18186aaca566SDavid Howells 	if (ret < 0)
18196aaca566SDavid Howells 		return ret;
18206aaca566SDavid Howells 
18216aaca566SDavid Howells 	m = file->private_data;
18226aaca566SDavid Howells 	m->private = PDE(inode)->data;
18236aaca566SDavid Howells 
18246aaca566SDavid Howells 	return 0;
18256aaca566SDavid Howells }
18266aaca566SDavid Howells 
18276aaca566SDavid Howells /*
18286aaca566SDavid Howells  * set up the iterator to start reading from the server list and return the first item
18296aaca566SDavid Howells  */
18306aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
18316aaca566SDavid Howells {
18326aaca566SDavid Howells 	/* lock the list against modification */
18336aaca566SDavid Howells 	spin_lock(&nfs_client_lock);
1834259902eaSPavel Emelianov 	return seq_list_start_head(&nfs_client_list, *_pos);
18356aaca566SDavid Howells }
18366aaca566SDavid Howells 
18376aaca566SDavid Howells /*
18386aaca566SDavid Howells  * move to next server
18396aaca566SDavid Howells  */
18406aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
18416aaca566SDavid Howells {
1842259902eaSPavel Emelianov 	return seq_list_next(v, &nfs_client_list, pos);
18436aaca566SDavid Howells }
18446aaca566SDavid Howells 
18456aaca566SDavid Howells /*
18466aaca566SDavid Howells  * clean up after reading from the transports list
18476aaca566SDavid Howells  */
18486aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v)
18496aaca566SDavid Howells {
18506aaca566SDavid Howells 	spin_unlock(&nfs_client_lock);
18516aaca566SDavid Howells }
18526aaca566SDavid Howells 
18536aaca566SDavid Howells /*
18546aaca566SDavid Howells  * display a header line followed by a load of call lines
18556aaca566SDavid Howells  */
18566aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v)
18576aaca566SDavid Howells {
18586aaca566SDavid Howells 	struct nfs_client *clp;
18596aaca566SDavid Howells 
18606aaca566SDavid Howells 	/* display header on line 1 */
1861259902eaSPavel Emelianov 	if (v == &nfs_client_list) {
18626aaca566SDavid Howells 		seq_puts(m, "NV SERVER   PORT USE HOSTNAME\n");
18636aaca566SDavid Howells 		return 0;
18646aaca566SDavid Howells 	}
18656aaca566SDavid Howells 
18666aaca566SDavid Howells 	/* display one transport per line on subsequent lines */
18676aaca566SDavid Howells 	clp = list_entry(v, struct nfs_client, cl_share_link);
18686aaca566SDavid Howells 
18695d8515caSChuck Lever 	seq_printf(m, "v%u %s %s %3d %s\n",
187040c55319STrond Myklebust 		   clp->rpc_ops->version,
18715d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
18725d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
18736aaca566SDavid Howells 		   atomic_read(&clp->cl_count),
18746aaca566SDavid Howells 		   clp->cl_hostname);
18756aaca566SDavid Howells 
18766aaca566SDavid Howells 	return 0;
18776aaca566SDavid Howells }
18786aaca566SDavid Howells 
18796aaca566SDavid Howells /*
18806aaca566SDavid Howells  * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
18816aaca566SDavid Howells  */
18826aaca566SDavid Howells static int nfs_volume_list_open(struct inode *inode, struct file *file)
18836aaca566SDavid Howells {
18846aaca566SDavid Howells 	struct seq_file *m;
18856aaca566SDavid Howells 	int ret;
18866aaca566SDavid Howells 
18876aaca566SDavid Howells 	ret = seq_open(file, &nfs_volume_list_ops);
18886aaca566SDavid Howells 	if (ret < 0)
18896aaca566SDavid Howells 		return ret;
18906aaca566SDavid Howells 
18916aaca566SDavid Howells 	m = file->private_data;
18926aaca566SDavid Howells 	m->private = PDE(inode)->data;
18936aaca566SDavid Howells 
18946aaca566SDavid Howells 	return 0;
18956aaca566SDavid Howells }
18966aaca566SDavid Howells 
18976aaca566SDavid Howells /*
18986aaca566SDavid Howells  * set up the iterator to start reading from the volume list and return the first item
18996aaca566SDavid Howells  */
19006aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
19016aaca566SDavid Howells {
19026aaca566SDavid Howells 	/* lock the list against modification */
19036aaca566SDavid Howells 	spin_lock(&nfs_client_lock);
1904259902eaSPavel Emelianov 	return seq_list_start_head(&nfs_volume_list, *_pos);
19056aaca566SDavid Howells }
19066aaca566SDavid Howells 
19076aaca566SDavid Howells /*
19086aaca566SDavid Howells  * move to next volume
19096aaca566SDavid Howells  */
19106aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
19116aaca566SDavid Howells {
1912259902eaSPavel Emelianov 	return seq_list_next(v, &nfs_volume_list, pos);
19136aaca566SDavid Howells }
19146aaca566SDavid Howells 
19156aaca566SDavid Howells /*
19166aaca566SDavid Howells  * clean up after reading from the transports list
19176aaca566SDavid Howells  */
19186aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v)
19196aaca566SDavid Howells {
19206aaca566SDavid Howells 	spin_unlock(&nfs_client_lock);
19216aaca566SDavid Howells }
19226aaca566SDavid Howells 
19236aaca566SDavid Howells /*
19246aaca566SDavid Howells  * display a header line followed by a load of call lines
19256aaca566SDavid Howells  */
19266aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v)
19276aaca566SDavid Howells {
19286aaca566SDavid Howells 	struct nfs_server *server;
19296aaca566SDavid Howells 	struct nfs_client *clp;
19306aaca566SDavid Howells 	char dev[8], fsid[17];
19316aaca566SDavid Howells 
19326aaca566SDavid Howells 	/* display header on line 1 */
1933259902eaSPavel Emelianov 	if (v == &nfs_volume_list) {
19345d1acff1SDavid Howells 		seq_puts(m, "NV SERVER   PORT DEV     FSID              FSC\n");
19356aaca566SDavid Howells 		return 0;
19366aaca566SDavid Howells 	}
19376aaca566SDavid Howells 	/* display one transport per line on subsequent lines */
19386aaca566SDavid Howells 	server = list_entry(v, struct nfs_server, master_link);
19396aaca566SDavid Howells 	clp = server->nfs_client;
19406aaca566SDavid Howells 
19416aaca566SDavid Howells 	snprintf(dev, 8, "%u:%u",
19426aaca566SDavid Howells 		 MAJOR(server->s_dev), MINOR(server->s_dev));
19436aaca566SDavid Howells 
19446aaca566SDavid Howells 	snprintf(fsid, 17, "%llx:%llx",
19456daabf1bSDavid Howells 		 (unsigned long long) server->fsid.major,
19466daabf1bSDavid Howells 		 (unsigned long long) server->fsid.minor);
19476aaca566SDavid Howells 
19485d1acff1SDavid Howells 	seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
194940c55319STrond Myklebust 		   clp->rpc_ops->version,
19505d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
19515d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
19526aaca566SDavid Howells 		   dev,
19535d1acff1SDavid Howells 		   fsid,
19545d1acff1SDavid Howells 		   nfs_server_fscache_state(server));
19556aaca566SDavid Howells 
19566aaca566SDavid Howells 	return 0;
19576aaca566SDavid Howells }
19586aaca566SDavid Howells 
19596aaca566SDavid Howells /*
19606aaca566SDavid Howells  * initialise the /proc/fs/nfsfs/ directory
19616aaca566SDavid Howells  */
19626aaca566SDavid Howells int __init nfs_fs_proc_init(void)
19636aaca566SDavid Howells {
19646aaca566SDavid Howells 	struct proc_dir_entry *p;
19656aaca566SDavid Howells 
196636a5aeb8SAlexey Dobriyan 	proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
19676aaca566SDavid Howells 	if (!proc_fs_nfs)
19686aaca566SDavid Howells 		goto error_0;
19696aaca566SDavid Howells 
19706aaca566SDavid Howells 	/* a file of servers with which we're dealing */
197134b37235SDenis V. Lunev 	p = proc_create("servers", S_IFREG|S_IRUGO,
197234b37235SDenis V. Lunev 			proc_fs_nfs, &nfs_server_list_fops);
19736aaca566SDavid Howells 	if (!p)
19746aaca566SDavid Howells 		goto error_1;
19756aaca566SDavid Howells 
19766aaca566SDavid Howells 	/* a file of volumes that we have mounted */
197734b37235SDenis V. Lunev 	p = proc_create("volumes", S_IFREG|S_IRUGO,
197834b37235SDenis V. Lunev 			proc_fs_nfs, &nfs_volume_list_fops);
19796aaca566SDavid Howells 	if (!p)
19806aaca566SDavid Howells 		goto error_2;
19816aaca566SDavid Howells 	return 0;
19826aaca566SDavid Howells 
19836aaca566SDavid Howells error_2:
19846aaca566SDavid Howells 	remove_proc_entry("servers", proc_fs_nfs);
19856aaca566SDavid Howells error_1:
198636a5aeb8SAlexey Dobriyan 	remove_proc_entry("fs/nfsfs", NULL);
19876aaca566SDavid Howells error_0:
19886aaca566SDavid Howells 	return -ENOMEM;
19896aaca566SDavid Howells }
19906aaca566SDavid Howells 
19916aaca566SDavid Howells /*
19926aaca566SDavid Howells  * clean up the /proc/fs/nfsfs/ directory
19936aaca566SDavid Howells  */
19946aaca566SDavid Howells void nfs_fs_proc_exit(void)
19956aaca566SDavid Howells {
19966aaca566SDavid Howells 	remove_proc_entry("volumes", proc_fs_nfs);
19976aaca566SDavid Howells 	remove_proc_entry("servers", proc_fs_nfs);
199836a5aeb8SAlexey Dobriyan 	remove_proc_entry("fs/nfsfs", NULL);
19996aaca566SDavid Howells }
20006aaca566SDavid Howells 
20016aaca566SDavid Howells #endif /* CONFIG_PROC_FS */
2002b064eca2STrond Myklebust 
2003b064eca2STrond Myklebust module_param(nfs4_disable_idmapping, bool, 0644);
2004b064eca2STrond Myklebust MODULE_PARM_DESC(nfs4_disable_idmapping,
2005b064eca2STrond Myklebust 		"Turn off NFSv4 idmapping when using 'sec=sys'");
2006