xref: /linux/fs/nfs/client.c (revision 28cd1b3f262dba56b5e335ba668e342d530f6129)
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>
426b13168bSStanislav Kinsbursky #include <linux/nsproxy.h>
436b13168bSStanislav Kinsbursky #include <linux/pid_namespace.h>
4424c8dbbbSDavid Howells 
4524c8dbbbSDavid Howells #include <asm/system.h>
4624c8dbbbSDavid Howells 
4724c8dbbbSDavid Howells #include "nfs4_fs.h"
4824c8dbbbSDavid Howells #include "callback.h"
4924c8dbbbSDavid Howells #include "delegation.h"
5024c8dbbbSDavid Howells #include "iostat.h"
5124c8dbbbSDavid Howells #include "internal.h"
5214727281SDavid Howells #include "fscache.h"
5385e174baSRicardo Labiaga #include "pnfs.h"
546b13168bSStanislav Kinsbursky #include "netns.h"
5524c8dbbbSDavid Howells 
5624c8dbbbSDavid Howells #define NFSDBG_FACILITY		NFSDBG_CLIENT
5724c8dbbbSDavid Howells 
58eee17325SStanislav Kinsbursky DEFINE_SPINLOCK(nfs_client_lock);
5924c8dbbbSDavid Howells static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
60f4eecd5dSAndy Adamson #ifdef CONFIG_NFS_V4
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;
69*28cd1b3fSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
70f4eecd5dSAndy Adamson 
71f4eecd5dSAndy Adamson 	if (clp->rpc_ops->version != 4 || minorversion != 0)
72f4eecd5dSAndy Adamson 		return ret;
73f4eecd5dSAndy Adamson retry:
74*28cd1b3fSStanislav Kinsbursky 	if (!idr_pre_get(&nn->cb_ident_idr, GFP_KERNEL))
75f4eecd5dSAndy Adamson 		return -ENOMEM;
76f4eecd5dSAndy Adamson 	spin_lock(&nfs_client_lock);
77*28cd1b3fSStanislav Kinsbursky 	ret = idr_get_new(&nn->cb_ident_idr, clp, &clp->cl_cb_ident);
78f4eecd5dSAndy Adamson 	spin_unlock(&nfs_client_lock);
79f4eecd5dSAndy Adamson 	if (ret == -EAGAIN)
80f4eecd5dSAndy Adamson 		goto retry;
81f4eecd5dSAndy Adamson 	return ret;
82f4eecd5dSAndy Adamson }
83f4eecd5dSAndy Adamson #endif /* CONFIG_NFS_V4 */
8424c8dbbbSDavid Howells 
8524c8dbbbSDavid Howells /*
86b064eca2STrond Myklebust  * Turn off NFSv4 uid/gid mapping when using AUTH_SYS
87b064eca2STrond Myklebust  */
8890ab5ee9SRusty Russell static bool nfs4_disable_idmapping = true;
89b064eca2STrond Myklebust 
90b064eca2STrond Myklebust /*
915006a76cSDavid Howells  * RPC cruft for NFS
925006a76cSDavid Howells  */
93a613fa16STrond Myklebust static const struct rpc_version *nfs_version[5] = {
945006a76cSDavid Howells 	[2]			= &nfs_version2,
955006a76cSDavid Howells #ifdef CONFIG_NFS_V3
965006a76cSDavid Howells 	[3]			= &nfs_version3,
975006a76cSDavid Howells #endif
985006a76cSDavid Howells #ifdef CONFIG_NFS_V4
995006a76cSDavid Howells 	[4]			= &nfs_version4,
1005006a76cSDavid Howells #endif
1015006a76cSDavid Howells };
1025006a76cSDavid Howells 
103a613fa16STrond Myklebust const struct rpc_program nfs_program = {
1045006a76cSDavid Howells 	.name			= "nfs",
1055006a76cSDavid Howells 	.number			= NFS_PROGRAM,
1065006a76cSDavid Howells 	.nrvers			= ARRAY_SIZE(nfs_version),
1075006a76cSDavid Howells 	.version		= nfs_version,
1085006a76cSDavid Howells 	.stats			= &nfs_rpcstat,
109fe0a9b74SJim Rees 	.pipe_dir_name		= NFS_PIPE_DIRNAME,
1105006a76cSDavid Howells };
1115006a76cSDavid Howells 
1125006a76cSDavid Howells struct rpc_stat nfs_rpcstat = {
1135006a76cSDavid Howells 	.program		= &nfs_program
1145006a76cSDavid Howells };
1155006a76cSDavid Howells 
1165006a76cSDavid Howells 
1175006a76cSDavid Howells #ifdef CONFIG_NFS_V3_ACL
1185006a76cSDavid Howells static struct rpc_stat		nfsacl_rpcstat = { &nfsacl_program };
119a613fa16STrond Myklebust static const struct rpc_version *nfsacl_version[] = {
1205006a76cSDavid Howells 	[3]			= &nfsacl_version3,
1215006a76cSDavid Howells };
1225006a76cSDavid Howells 
123a613fa16STrond Myklebust const struct rpc_program nfsacl_program = {
1245006a76cSDavid Howells 	.name			= "nfsacl",
1255006a76cSDavid Howells 	.number			= NFS_ACL_PROGRAM,
1265006a76cSDavid Howells 	.nrvers			= ARRAY_SIZE(nfsacl_version),
1275006a76cSDavid Howells 	.version		= nfsacl_version,
1285006a76cSDavid Howells 	.stats			= &nfsacl_rpcstat,
1295006a76cSDavid Howells };
1305006a76cSDavid Howells #endif  /* CONFIG_NFS_V3_ACL */
1315006a76cSDavid Howells 
1323a498026STrond Myklebust struct nfs_client_initdata {
1333a498026STrond Myklebust 	const char *hostname;
134d7422c47SChuck Lever 	const struct sockaddr *addr;
1356e4cffd7SChuck Lever 	size_t addrlen;
13640c55319STrond Myklebust 	const struct nfs_rpc_ops *rpc_ops;
13759dca3b2STrond Myklebust 	int proto;
1385aae4a9aSBenny Halevy 	u32 minorversion;
139e50a7a1aSStanislav Kinsbursky 	struct net *net;
1403a498026STrond Myklebust };
1413a498026STrond Myklebust 
1425006a76cSDavid Howells /*
14324c8dbbbSDavid Howells  * Allocate a shared client record
14424c8dbbbSDavid Howells  *
14524c8dbbbSDavid Howells  * Since these are allocated/deallocated very rarely, we don't
14624c8dbbbSDavid Howells  * bother putting them in a slab cache...
14724c8dbbbSDavid Howells  */
1483a498026STrond Myklebust static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
14924c8dbbbSDavid Howells {
15024c8dbbbSDavid Howells 	struct nfs_client *clp;
1517c67db3aSTrond Myklebust 	struct rpc_cred *cred;
152a21bdd9bSChuck Lever 	int err = -ENOMEM;
15324c8dbbbSDavid Howells 
15424c8dbbbSDavid Howells 	if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
15524c8dbbbSDavid Howells 		goto error_0;
15624c8dbbbSDavid Howells 
15740c55319STrond Myklebust 	clp->rpc_ops = cl_init->rpc_ops;
15840c55319STrond Myklebust 
15924c8dbbbSDavid Howells 	atomic_set(&clp->cl_count, 1);
16024c8dbbbSDavid Howells 	clp->cl_cons_state = NFS_CS_INITING;
16124c8dbbbSDavid Howells 
1626e4cffd7SChuck Lever 	memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
1636e4cffd7SChuck Lever 	clp->cl_addrlen = cl_init->addrlen;
16424c8dbbbSDavid Howells 
1653a498026STrond Myklebust 	if (cl_init->hostname) {
166a21bdd9bSChuck Lever 		err = -ENOMEM;
1673a498026STrond Myklebust 		clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
16824c8dbbbSDavid Howells 		if (!clp->cl_hostname)
16971468513SBenny Halevy 			goto error_cleanup;
17024c8dbbbSDavid Howells 	}
17124c8dbbbSDavid Howells 
17224c8dbbbSDavid Howells 	INIT_LIST_HEAD(&clp->cl_superblocks);
17324c8dbbbSDavid Howells 	clp->cl_rpcclient = ERR_PTR(-EINVAL);
17424c8dbbbSDavid Howells 
17559dca3b2STrond Myklebust 	clp->cl_proto = cl_init->proto;
176*28cd1b3fSStanislav Kinsbursky 	clp->net = cl_init->net;
17759dca3b2STrond Myklebust 
17824c8dbbbSDavid Howells #ifdef CONFIG_NFS_V4
179f4eecd5dSAndy Adamson 	err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
180f4eecd5dSAndy Adamson 	if (err)
181f4eecd5dSAndy Adamson 		goto error_cleanup;
182f4eecd5dSAndy Adamson 
18324c8dbbbSDavid Howells 	spin_lock_init(&clp->cl_lock);
18465f27f38SDavid Howells 	INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
18524c8dbbbSDavid Howells 	rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
18624c8dbbbSDavid Howells 	clp->cl_boot_time = CURRENT_TIME;
18724c8dbbbSDavid Howells 	clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
1885aae4a9aSBenny Halevy 	clp->cl_minorversion = cl_init->minorversion;
18997dc1359STrond Myklebust 	clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
19024c8dbbbSDavid Howells #endif
19168c97153STrond Myklebust 	cred = rpc_lookup_machine_cred("*");
1927c67db3aSTrond Myklebust 	if (!IS_ERR(cred))
1937c67db3aSTrond Myklebust 		clp->cl_machine_cred = cred;
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 */
239*28cd1b3fSStanislav Kinsbursky void nfs_cleanup_cb_ident_idr(struct net *net)
240f4eecd5dSAndy Adamson {
241*28cd1b3fSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(net, nfs_net_id);
242*28cd1b3fSStanislav Kinsbursky 
243*28cd1b3fSStanislav Kinsbursky 	idr_destroy(&nn->cb_ident_idr);
244f4eecd5dSAndy Adamson }
245f4eecd5dSAndy Adamson 
246f4eecd5dSAndy Adamson /* nfs_client_lock held */
247f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
248f4eecd5dSAndy Adamson {
249*28cd1b3fSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
250*28cd1b3fSStanislav Kinsbursky 
251f4eecd5dSAndy Adamson 	if (clp->cl_cb_ident)
252*28cd1b3fSStanislav Kinsbursky 		idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
253f4eecd5dSAndy Adamson }
254f4eecd5dSAndy Adamson 
255f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server)
256f7e8917aSFred Isaman {
257f7e8917aSFred Isaman 	rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
258f7e8917aSFred Isaman }
259f7e8917aSFred Isaman 
2600aaaf5c4SChuck Lever static void nfs4_destroy_server(struct nfs_server *server)
2610aaaf5c4SChuck Lever {
2620aaaf5c4SChuck Lever 	nfs4_purge_state_owners(server);
2630aaaf5c4SChuck Lever }
2640aaaf5c4SChuck Lever 
265888ef2e3SAlexandros Batsakis #else
266888ef2e3SAlexandros Batsakis static void nfs4_shutdown_client(struct nfs_client *clp)
267888ef2e3SAlexandros Batsakis {
268888ef2e3SAlexandros Batsakis }
269f4eecd5dSAndy Adamson 
270*28cd1b3fSStanislav Kinsbursky void nfs_cleanup_cb_ident_idr(struct net *net)
271f4eecd5dSAndy Adamson {
272f4eecd5dSAndy Adamson }
273f4eecd5dSAndy Adamson 
274f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
275f4eecd5dSAndy Adamson {
276f4eecd5dSAndy Adamson }
277f7e8917aSFred Isaman 
278f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server)
279f7e8917aSFred Isaman {
280f7e8917aSFred Isaman }
281f7e8917aSFred Isaman 
282888ef2e3SAlexandros Batsakis #endif /* CONFIG_NFS_V4 */
283888ef2e3SAlexandros Batsakis 
284888ef2e3SAlexandros Batsakis /*
2855dd3177aSTrond Myklebust  * Destroy a shared client record
2865dd3177aSTrond Myklebust  */
2875dd3177aSTrond Myklebust static void nfs_free_client(struct nfs_client *clp)
2885dd3177aSTrond Myklebust {
28940c55319STrond Myklebust 	dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
2905dd3177aSTrond Myklebust 
2915dd3177aSTrond Myklebust 	nfs4_shutdown_client(clp);
29224c8dbbbSDavid Howells 
29314727281SDavid Howells 	nfs_fscache_release_client_cookie(clp);
29414727281SDavid Howells 
29524c8dbbbSDavid Howells 	/* -EIO all pending I/O */
29624c8dbbbSDavid Howells 	if (!IS_ERR(clp->cl_rpcclient))
29724c8dbbbSDavid Howells 		rpc_shutdown_client(clp->cl_rpcclient);
29824c8dbbbSDavid Howells 
2997c67db3aSTrond Myklebust 	if (clp->cl_machine_cred != NULL)
3007c67db3aSTrond Myklebust 		put_rpccred(clp->cl_machine_cred);
3017c67db3aSTrond Myklebust 
3021775bc34SBenny Halevy 	nfs4_deviceid_purge_client(clp);
3031775bc34SBenny Halevy 
30424c8dbbbSDavid Howells 	kfree(clp->cl_hostname);
30578fe0f41SWeston Andros Adamson 	kfree(clp->server_scope);
30624c8dbbbSDavid Howells 	kfree(clp);
30724c8dbbbSDavid Howells 
30824c8dbbbSDavid Howells 	dprintk("<-- nfs_free_client()\n");
30924c8dbbbSDavid Howells }
31024c8dbbbSDavid Howells 
31124c8dbbbSDavid Howells /*
31224c8dbbbSDavid Howells  * Release a reference to a shared client record
31324c8dbbbSDavid Howells  */
31424c8dbbbSDavid Howells void nfs_put_client(struct nfs_client *clp)
31524c8dbbbSDavid Howells {
31627ba8512SDavid Howells 	if (!clp)
31727ba8512SDavid Howells 		return;
31827ba8512SDavid Howells 
31924c8dbbbSDavid Howells 	dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
32024c8dbbbSDavid Howells 
32124c8dbbbSDavid Howells 	if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
32224c8dbbbSDavid Howells 		list_del(&clp->cl_share_link);
323f4eecd5dSAndy Adamson 		nfs_cb_idr_remove_locked(clp);
32424c8dbbbSDavid Howells 		spin_unlock(&nfs_client_lock);
32524c8dbbbSDavid Howells 
32624c8dbbbSDavid Howells 		BUG_ON(!list_empty(&clp->cl_superblocks));
32724c8dbbbSDavid Howells 
32824c8dbbbSDavid Howells 		nfs_free_client(clp);
32924c8dbbbSDavid Howells 	}
33024c8dbbbSDavid Howells }
33116b374caSAndy Adamson EXPORT_SYMBOL_GPL(nfs_put_client);
33224c8dbbbSDavid Howells 
3339082a5ccSTrond Myklebust #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
3349f4c899cSTrond Myklebust /*
3359f4c899cSTrond Myklebust  * Test if two ip6 socket addresses refer to the same socket by
3369f4c899cSTrond Myklebust  * comparing relevant fields. The padding bytes specifically, are not
3379f4c899cSTrond Myklebust  * compared. sin6_flowinfo is not compared because it only affects QoS
3389f4c899cSTrond Myklebust  * and sin6_scope_id is only compared if the address is "link local"
3399f4c899cSTrond Myklebust  * because "link local" addresses need only be unique to a specific
3409f4c899cSTrond Myklebust  * link. Conversely, ordinary unicast addresses might have different
3419f4c899cSTrond Myklebust  * sin6_scope_id.
3429f4c899cSTrond Myklebust  *
3439f4c899cSTrond Myklebust  * The caller should ensure both socket addresses are AF_INET6.
3449f4c899cSTrond Myklebust  */
3453c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
3469f4c899cSTrond Myklebust 				      const struct sockaddr *sa2)
3479f4c899cSTrond Myklebust {
3483c8c45dfSChuck Lever 	const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
3493c8c45dfSChuck Lever 	const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
3509f4c899cSTrond Myklebust 
351b9dd3abbSMi Jinlong 	if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
3529f4c899cSTrond Myklebust 		return 0;
353b9dd3abbSMi Jinlong 	else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
354b9dd3abbSMi Jinlong 		return sin1->sin6_scope_id == sin2->sin6_scope_id;
3553b0d3f93STrond Myklebust 
356b9dd3abbSMi Jinlong 	return 1;
3573b0d3f93STrond Myklebust }
3583c8c45dfSChuck Lever #else	/* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
3593c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
3609f4c899cSTrond Myklebust 				      const struct sockaddr *sa2)
3619f4c899cSTrond Myklebust {
3629f4c899cSTrond Myklebust 	return 0;
3639f4c899cSTrond Myklebust }
3649082a5ccSTrond Myklebust #endif
3653b0d3f93STrond Myklebust 
36624c8dbbbSDavid Howells /*
367d7371c41SIan Dall  * Test if two ip4 socket addresses refer to the same socket, by
368d7371c41SIan Dall  * comparing relevant fields. The padding bytes specifically, are
369d7371c41SIan Dall  * not compared.
370d7371c41SIan Dall  *
371d7371c41SIan Dall  * The caller should ensure both socket addresses are AF_INET.
372d7371c41SIan Dall  */
3733c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
3743c8c45dfSChuck Lever 				      const struct sockaddr *sa2)
3753c8c45dfSChuck Lever {
3763c8c45dfSChuck Lever 	const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
3773c8c45dfSChuck Lever 	const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
3783c8c45dfSChuck Lever 
3793c8c45dfSChuck Lever 	return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
3803c8c45dfSChuck Lever }
3813c8c45dfSChuck Lever 
3823c8c45dfSChuck Lever static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
3833c8c45dfSChuck Lever 				const struct sockaddr *sa2)
3843c8c45dfSChuck Lever {
3853c8c45dfSChuck Lever 	const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
3863c8c45dfSChuck Lever 	const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
3873c8c45dfSChuck Lever 
3883c8c45dfSChuck Lever 	return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
3893c8c45dfSChuck Lever 		(sin1->sin6_port == sin2->sin6_port);
3903c8c45dfSChuck Lever }
3913c8c45dfSChuck Lever 
3929f4c899cSTrond Myklebust static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
3939f4c899cSTrond Myklebust 				const struct sockaddr *sa2)
394d7371c41SIan Dall {
3953c8c45dfSChuck Lever 	const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
3963c8c45dfSChuck Lever 	const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
3979f4c899cSTrond Myklebust 
3983c8c45dfSChuck Lever 	return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
3993c8c45dfSChuck Lever 		(sin1->sin_port == sin2->sin_port);
400d7371c41SIan Dall }
401d7371c41SIan Dall 
402d7371c41SIan Dall /*
403d7371c41SIan Dall  * Test if two socket addresses represent the same actual socket,
4043c8c45dfSChuck Lever  * by comparing (only) relevant fields, excluding the port number.
4053c8c45dfSChuck Lever  */
4063c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
4073c8c45dfSChuck Lever 				     const struct sockaddr *sa2)
4083c8c45dfSChuck Lever {
4093c8c45dfSChuck Lever 	if (sa1->sa_family != sa2->sa_family)
4103c8c45dfSChuck Lever 		return 0;
4113c8c45dfSChuck Lever 
4123c8c45dfSChuck Lever 	switch (sa1->sa_family) {
4133c8c45dfSChuck Lever 	case AF_INET:
4143c8c45dfSChuck Lever 		return nfs_sockaddr_match_ipaddr4(sa1, sa2);
4153c8c45dfSChuck Lever 	case AF_INET6:
4163c8c45dfSChuck Lever 		return nfs_sockaddr_match_ipaddr6(sa1, sa2);
4173c8c45dfSChuck Lever 	}
4183c8c45dfSChuck Lever 	return 0;
4193c8c45dfSChuck Lever }
4203c8c45dfSChuck Lever 
4213c8c45dfSChuck Lever /*
4223c8c45dfSChuck Lever  * Test if two socket addresses represent the same actual socket,
4233c8c45dfSChuck Lever  * by comparing (only) relevant fields, including the port number.
424d7371c41SIan Dall  */
425d7371c41SIan Dall static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
426d7371c41SIan Dall 			    const struct sockaddr *sa2)
427d7371c41SIan Dall {
428d7371c41SIan Dall 	if (sa1->sa_family != sa2->sa_family)
429d7371c41SIan Dall 		return 0;
430d7371c41SIan Dall 
431d7371c41SIan Dall 	switch (sa1->sa_family) {
432d7371c41SIan Dall 	case AF_INET:
4339f4c899cSTrond Myklebust 		return nfs_sockaddr_cmp_ip4(sa1, sa2);
434d7371c41SIan Dall 	case AF_INET6:
4359f4c899cSTrond Myklebust 		return nfs_sockaddr_cmp_ip6(sa1, sa2);
436d7371c41SIan Dall 	}
437d7371c41SIan Dall 	return 0;
438d7371c41SIan Dall }
439d7371c41SIan Dall 
440c36fca52SAndy Adamson /* Common match routine for v4.0 and v4.1 callback services */
441c36fca52SAndy Adamson bool
442c36fca52SAndy Adamson nfs4_cb_match_client(const struct sockaddr *addr, struct nfs_client *clp,
443c36fca52SAndy Adamson 		     u32 minorversion)
444c81468a1STrond Myklebust {
4453b0d3f93STrond Myklebust 	struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
4463b0d3f93STrond Myklebust 
447c36fca52SAndy Adamson 	/* Don't match clients that failed to initialise */
44876db6d95SAndy Adamson 	if (!(clp->cl_cons_state == NFS_CS_READY ||
44976db6d95SAndy Adamson 	    clp->cl_cons_state == NFS_CS_SESSION_INITING))
450c36fca52SAndy Adamson 		return false;
451c81468a1STrond Myklebust 
452c36fca52SAndy Adamson 	/* Match the version and minorversion */
453c36fca52SAndy Adamson 	if (clp->rpc_ops->version != 4 ||
454c36fca52SAndy Adamson 	    clp->cl_minorversion != minorversion)
455c36fca52SAndy Adamson 		return false;
456c81468a1STrond Myklebust 
457c81468a1STrond Myklebust 	/* Match only the IP address, not the port number */
4583b0d3f93STrond Myklebust 	if (!nfs_sockaddr_match_ipaddr(addr, clap))
459c36fca52SAndy Adamson 		return false;
460c81468a1STrond Myklebust 
461c36fca52SAndy Adamson 	return true;
4623fbd67adSTrond Myklebust }
4633fbd67adSTrond Myklebust 
4643fbd67adSTrond Myklebust /*
465c81468a1STrond Myklebust  * Find an nfs_client on the list that matches the initialisation data
466c81468a1STrond Myklebust  * that is supplied.
467c81468a1STrond Myklebust  */
468c81468a1STrond Myklebust static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
46924c8dbbbSDavid Howells {
47024c8dbbbSDavid Howells 	struct nfs_client *clp;
471d7371c41SIan Dall 	const struct sockaddr *sap = data->addr;
4726b13168bSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(data->net, nfs_net_id);
47324c8dbbbSDavid Howells 
4746b13168bSStanislav Kinsbursky 	list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
475d7371c41SIan Dall 	        const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
47613bbc06aSTrond Myklebust 		/* Don't match clients that failed to initialise properly */
47713bbc06aSTrond Myklebust 		if (clp->cl_cons_state < 0)
47813bbc06aSTrond Myklebust 			continue;
47913bbc06aSTrond Myklebust 
48024c8dbbbSDavid Howells 		/* Different NFS versions cannot share the same nfs_client */
48140c55319STrond Myklebust 		if (clp->rpc_ops != data->rpc_ops)
48224c8dbbbSDavid Howells 			continue;
48324c8dbbbSDavid Howells 
48459dca3b2STrond Myklebust 		if (clp->cl_proto != data->proto)
48559dca3b2STrond Myklebust 			continue;
4865aae4a9aSBenny Halevy 		/* Match nfsv4 minorversion */
4875aae4a9aSBenny Halevy 		if (clp->cl_minorversion != data->minorversion)
4885aae4a9aSBenny Halevy 			continue;
489c81468a1STrond Myklebust 		/* Match the full socket address */
490d7371c41SIan Dall 		if (!nfs_sockaddr_cmp(sap, clap))
49124c8dbbbSDavid Howells 			continue;
49224c8dbbbSDavid Howells 
49324c8dbbbSDavid Howells 		atomic_inc(&clp->cl_count);
49424c8dbbbSDavid Howells 		return clp;
49524c8dbbbSDavid Howells 	}
496c81468a1STrond Myklebust 	return NULL;
49724c8dbbbSDavid Howells }
49824c8dbbbSDavid Howells 
49924c8dbbbSDavid Howells /*
50024c8dbbbSDavid Howells  * Look up a client by IP address and protocol version
50124c8dbbbSDavid Howells  * - creates a new record if one doesn't yet exist
50224c8dbbbSDavid Howells  */
50345a52a02SAndy Adamson static struct nfs_client *
50445a52a02SAndy Adamson nfs_get_client(const struct nfs_client_initdata *cl_init,
50545a52a02SAndy Adamson 	       const struct rpc_timeout *timeparms,
50645a52a02SAndy Adamson 	       const char *ip_addr,
50745a52a02SAndy Adamson 	       rpc_authflavor_t authflavour,
50845a52a02SAndy Adamson 	       int noresvport)
50924c8dbbbSDavid Howells {
51024c8dbbbSDavid Howells 	struct nfs_client *clp, *new = NULL;
51124c8dbbbSDavid Howells 	int error;
5126b13168bSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
51324c8dbbbSDavid Howells 
514d7422c47SChuck Lever 	dprintk("--> nfs_get_client(%s,v%u)\n",
515d7422c47SChuck Lever 		cl_init->hostname ?: "", cl_init->rpc_ops->version);
51624c8dbbbSDavid Howells 
51724c8dbbbSDavid Howells 	/* see if the client already exists */
51824c8dbbbSDavid Howells 	do {
51924c8dbbbSDavid Howells 		spin_lock(&nfs_client_lock);
52024c8dbbbSDavid Howells 
521c81468a1STrond Myklebust 		clp = nfs_match_client(cl_init);
52224c8dbbbSDavid Howells 		if (clp)
52324c8dbbbSDavid Howells 			goto found_client;
52424c8dbbbSDavid Howells 		if (new)
52524c8dbbbSDavid Howells 			goto install_client;
52624c8dbbbSDavid Howells 
52724c8dbbbSDavid Howells 		spin_unlock(&nfs_client_lock);
52824c8dbbbSDavid Howells 
5293a498026STrond Myklebust 		new = nfs_alloc_client(cl_init);
530a21bdd9bSChuck Lever 	} while (!IS_ERR(new));
53124c8dbbbSDavid Howells 
532a21bdd9bSChuck Lever 	dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
533a21bdd9bSChuck Lever 	return new;
53424c8dbbbSDavid Howells 
53524c8dbbbSDavid Howells 	/* install a new client and return with it unready */
53624c8dbbbSDavid Howells install_client:
53724c8dbbbSDavid Howells 	clp = new;
5386b13168bSStanislav Kinsbursky 	list_add(&clp->cl_share_link, &nn->nfs_client_list);
53924c8dbbbSDavid Howells 	spin_unlock(&nfs_client_lock);
54045a52a02SAndy Adamson 
54145a52a02SAndy Adamson 	error = cl_init->rpc_ops->init_client(clp, timeparms, ip_addr,
54245a52a02SAndy Adamson 					      authflavour, noresvport);
54345a52a02SAndy Adamson 	if (error < 0) {
54445a52a02SAndy Adamson 		nfs_put_client(clp);
54545a52a02SAndy Adamson 		return ERR_PTR(error);
54645a52a02SAndy Adamson 	}
54724c8dbbbSDavid Howells 	dprintk("--> nfs_get_client() = %p [new]\n", clp);
54824c8dbbbSDavid Howells 	return clp;
54924c8dbbbSDavid Howells 
55024c8dbbbSDavid Howells 	/* found an existing client
55124c8dbbbSDavid Howells 	 * - make sure it's ready before returning
55224c8dbbbSDavid Howells 	 */
55324c8dbbbSDavid Howells found_client:
55424c8dbbbSDavid Howells 	spin_unlock(&nfs_client_lock);
55524c8dbbbSDavid Howells 
55624c8dbbbSDavid Howells 	if (new)
55724c8dbbbSDavid Howells 		nfs_free_client(new);
55824c8dbbbSDavid Howells 
559150030b7SMatthew Wilcox 	error = wait_event_killable(nfs_client_active_wq,
56076db6d95SAndy Adamson 				clp->cl_cons_state < NFS_CS_INITING);
5610bae89ecSTrond Myklebust 	if (error < 0) {
56224c8dbbbSDavid Howells 		nfs_put_client(clp);
56324c8dbbbSDavid Howells 		return ERR_PTR(-ERESTARTSYS);
56424c8dbbbSDavid Howells 	}
56524c8dbbbSDavid Howells 
56624c8dbbbSDavid Howells 	if (clp->cl_cons_state < NFS_CS_READY) {
56724c8dbbbSDavid Howells 		error = clp->cl_cons_state;
56824c8dbbbSDavid Howells 		nfs_put_client(clp);
56924c8dbbbSDavid Howells 		return ERR_PTR(error);
57024c8dbbbSDavid Howells 	}
57124c8dbbbSDavid Howells 
57254ceac45SDavid Howells 	BUG_ON(clp->cl_cons_state != NFS_CS_READY);
57354ceac45SDavid Howells 
57424c8dbbbSDavid Howells 	dprintk("--> nfs_get_client() = %p [share]\n", clp);
57524c8dbbbSDavid Howells 	return clp;
57624c8dbbbSDavid Howells }
57724c8dbbbSDavid Howells 
57824c8dbbbSDavid Howells /*
57924c8dbbbSDavid Howells  * Mark a server as ready or failed
58024c8dbbbSDavid Howells  */
58176db6d95SAndy Adamson void nfs_mark_client_ready(struct nfs_client *clp, int state)
58224c8dbbbSDavid Howells {
58324c8dbbbSDavid Howells 	clp->cl_cons_state = state;
58424c8dbbbSDavid Howells 	wake_up_all(&nfs_client_active_wq);
58524c8dbbbSDavid Howells }
5865006a76cSDavid Howells 
5875006a76cSDavid Howells /*
588008f55d0SBenny Halevy  * With sessions, the client is not marked ready until after a
589008f55d0SBenny Halevy  * successful EXCHANGE_ID and CREATE_SESSION.
590008f55d0SBenny Halevy  *
591008f55d0SBenny Halevy  * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
592008f55d0SBenny Halevy  * other versions of NFS can be tried.
593008f55d0SBenny Halevy  */
594008f55d0SBenny Halevy int nfs4_check_client_ready(struct nfs_client *clp)
595008f55d0SBenny Halevy {
596008f55d0SBenny Halevy 	if (!nfs4_has_session(clp))
597008f55d0SBenny Halevy 		return 0;
598008f55d0SBenny Halevy 	if (clp->cl_cons_state < NFS_CS_READY)
599008f55d0SBenny Halevy 		return -EPROTONOSUPPORT;
600008f55d0SBenny Halevy 	return 0;
601008f55d0SBenny Halevy }
602008f55d0SBenny Halevy 
603008f55d0SBenny Halevy /*
6045006a76cSDavid Howells  * Initialise the timeout values for a connection
6055006a76cSDavid Howells  */
6065006a76cSDavid Howells static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
6075006a76cSDavid Howells 				    unsigned int timeo, unsigned int retrans)
6085006a76cSDavid Howells {
6095006a76cSDavid Howells 	to->to_initval = timeo * HZ / 10;
6105006a76cSDavid Howells 	to->to_retries = retrans;
6115006a76cSDavid Howells 
6125006a76cSDavid Howells 	switch (proto) {
6130896a725S\"Talpey, Thomas\ 	case XPRT_TRANSPORT_TCP:
6142cf7ff7aS\"Talpey, Thomas\ 	case XPRT_TRANSPORT_RDMA:
615259875efSTrond Myklebust 		if (to->to_retries == 0)
616259875efSTrond Myklebust 			to->to_retries = NFS_DEF_TCP_RETRANS;
6177a3e3e18STrond Myklebust 		if (to->to_initval == 0)
618259875efSTrond Myklebust 			to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
6195006a76cSDavid Howells 		if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
6205006a76cSDavid Howells 			to->to_initval = NFS_MAX_TCP_TIMEOUT;
6215006a76cSDavid Howells 		to->to_increment = to->to_initval;
6225006a76cSDavid Howells 		to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
6237a3e3e18STrond Myklebust 		if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
6247a3e3e18STrond Myklebust 			to->to_maxval = NFS_MAX_TCP_TIMEOUT;
6257a3e3e18STrond Myklebust 		if (to->to_maxval < to->to_initval)
6267a3e3e18STrond Myklebust 			to->to_maxval = to->to_initval;
6275006a76cSDavid Howells 		to->to_exponential = 0;
6285006a76cSDavid Howells 		break;
6290896a725S\"Talpey, Thomas\ 	case XPRT_TRANSPORT_UDP:
630259875efSTrond Myklebust 		if (to->to_retries == 0)
631259875efSTrond Myklebust 			to->to_retries = NFS_DEF_UDP_RETRANS;
6325006a76cSDavid Howells 		if (!to->to_initval)
633259875efSTrond Myklebust 			to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
6345006a76cSDavid Howells 		if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
6355006a76cSDavid Howells 			to->to_initval = NFS_MAX_UDP_TIMEOUT;
6365006a76cSDavid Howells 		to->to_maxval = NFS_MAX_UDP_TIMEOUT;
6375006a76cSDavid Howells 		to->to_exponential = 1;
6385006a76cSDavid Howells 		break;
639259875efSTrond Myklebust 	default:
640259875efSTrond Myklebust 		BUG();
6415006a76cSDavid Howells 	}
6425006a76cSDavid Howells }
6435006a76cSDavid Howells 
6445006a76cSDavid Howells /*
6455006a76cSDavid Howells  * Create an RPC client handle
6465006a76cSDavid Howells  */
64759dca3b2STrond Myklebust static int nfs_create_rpc_client(struct nfs_client *clp,
64833170233STrond Myklebust 				 const struct rpc_timeout *timeparms,
64943d78ef2SChuck Lever 				 rpc_authflavor_t flavor,
6504a01b8a4SChuck Lever 				 int discrtry, int noresvport)
6515006a76cSDavid Howells {
6525006a76cSDavid Howells 	struct rpc_clnt		*clnt = NULL;
65341877d20SChuck Lever 	struct rpc_create_args args = {
6546d59b8d5SStanislav Kinsbursky 		.net		= clp->net,
65559dca3b2STrond Myklebust 		.protocol	= clp->cl_proto,
65641877d20SChuck Lever 		.address	= (struct sockaddr *)&clp->cl_addr,
6576e4cffd7SChuck Lever 		.addrsize	= clp->cl_addrlen,
65833170233STrond Myklebust 		.timeout	= timeparms,
65941877d20SChuck Lever 		.servername	= clp->cl_hostname,
66041877d20SChuck Lever 		.program	= &nfs_program,
66141877d20SChuck Lever 		.version	= clp->rpc_ops->version,
66241877d20SChuck Lever 		.authflavor	= flavor,
66341877d20SChuck Lever 	};
6645006a76cSDavid Howells 
6654a01b8a4SChuck Lever 	if (discrtry)
6664a01b8a4SChuck Lever 		args.flags |= RPC_CLNT_CREATE_DISCRTRY;
6674a01b8a4SChuck Lever 	if (noresvport)
6684a01b8a4SChuck Lever 		args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
6694a01b8a4SChuck Lever 
6705006a76cSDavid Howells 	if (!IS_ERR(clp->cl_rpcclient))
6715006a76cSDavid Howells 		return 0;
6725006a76cSDavid Howells 
67341877d20SChuck Lever 	clnt = rpc_create(&args);
6745006a76cSDavid Howells 	if (IS_ERR(clnt)) {
6755006a76cSDavid Howells 		dprintk("%s: cannot create RPC client. Error = %ld\n",
6763110ff80SHarvey Harrison 				__func__, PTR_ERR(clnt));
6775006a76cSDavid Howells 		return PTR_ERR(clnt);
6785006a76cSDavid Howells 	}
6795006a76cSDavid Howells 
6805006a76cSDavid Howells 	clp->cl_rpcclient = clnt;
6815006a76cSDavid Howells 	return 0;
6825006a76cSDavid Howells }
68354ceac45SDavid Howells 
68454ceac45SDavid Howells /*
68554ceac45SDavid Howells  * Version 2 or 3 client destruction
68654ceac45SDavid Howells  */
68754ceac45SDavid Howells static void nfs_destroy_server(struct nfs_server *server)
68854ceac45SDavid Howells {
6895eebde23SSuresh Jayaraman 	if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
6905eebde23SSuresh Jayaraman 			!(server->flags & NFS_MOUNT_LOCAL_FCNTL))
6919289e7f9SChuck Lever 		nlmclnt_done(server->nlm_host);
69254ceac45SDavid Howells }
69354ceac45SDavid Howells 
69454ceac45SDavid Howells /*
69554ceac45SDavid Howells  * Version 2 or 3 lockd setup
69654ceac45SDavid Howells  */
69754ceac45SDavid Howells static int nfs_start_lockd(struct nfs_server *server)
69854ceac45SDavid Howells {
6999289e7f9SChuck Lever 	struct nlm_host *host;
7009289e7f9SChuck Lever 	struct nfs_client *clp = server->nfs_client;
701883bb163SChuck Lever 	struct nlmclnt_initdata nlm_init = {
702883bb163SChuck Lever 		.hostname	= clp->cl_hostname,
703883bb163SChuck Lever 		.address	= (struct sockaddr *)&clp->cl_addr,
704883bb163SChuck Lever 		.addrlen	= clp->cl_addrlen,
705883bb163SChuck Lever 		.nfs_version	= clp->rpc_ops->version,
7060cb2659bSChuck Lever 		.noresvport	= server->flags & NFS_MOUNT_NORESVPORT ?
7070cb2659bSChuck Lever 					1 : 0,
708883bb163SChuck Lever 	};
70954ceac45SDavid Howells 
710883bb163SChuck Lever 	if (nlm_init.nfs_version > 3)
7119289e7f9SChuck Lever 		return 0;
7125eebde23SSuresh Jayaraman 	if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
7135eebde23SSuresh Jayaraman 			(server->flags & NFS_MOUNT_LOCAL_FCNTL))
7149289e7f9SChuck Lever 		return 0;
7159289e7f9SChuck Lever 
7168a6e5debSTrond Myklebust 	switch (clp->cl_proto) {
7178a6e5debSTrond Myklebust 		default:
7188a6e5debSTrond Myklebust 			nlm_init.protocol = IPPROTO_TCP;
7198a6e5debSTrond Myklebust 			break;
7208a6e5debSTrond Myklebust 		case XPRT_TRANSPORT_UDP:
7218a6e5debSTrond Myklebust 			nlm_init.protocol = IPPROTO_UDP;
7228a6e5debSTrond Myklebust 	}
7238a6e5debSTrond Myklebust 
724883bb163SChuck Lever 	host = nlmclnt_init(&nlm_init);
7259289e7f9SChuck Lever 	if (IS_ERR(host))
7269289e7f9SChuck Lever 		return PTR_ERR(host);
7279289e7f9SChuck Lever 
7289289e7f9SChuck Lever 	server->nlm_host = host;
72954ceac45SDavid Howells 	server->destroy = nfs_destroy_server;
7309289e7f9SChuck Lever 	return 0;
73154ceac45SDavid Howells }
73254ceac45SDavid Howells 
73354ceac45SDavid Howells /*
73454ceac45SDavid Howells  * Initialise an NFSv3 ACL client connection
73554ceac45SDavid Howells  */
73654ceac45SDavid Howells #ifdef CONFIG_NFS_V3_ACL
73754ceac45SDavid Howells static void nfs_init_server_aclclient(struct nfs_server *server)
73854ceac45SDavid Howells {
73940c55319STrond Myklebust 	if (server->nfs_client->rpc_ops->version != 3)
74054ceac45SDavid Howells 		goto out_noacl;
74154ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_NOACL)
74254ceac45SDavid Howells 		goto out_noacl;
74354ceac45SDavid Howells 
74454ceac45SDavid Howells 	server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
74554ceac45SDavid Howells 	if (IS_ERR(server->client_acl))
74654ceac45SDavid Howells 		goto out_noacl;
74754ceac45SDavid Howells 
74854ceac45SDavid Howells 	/* No errors! Assume that Sun nfsacls are supported */
74954ceac45SDavid Howells 	server->caps |= NFS_CAP_ACLS;
75054ceac45SDavid Howells 	return;
75154ceac45SDavid Howells 
75254ceac45SDavid Howells out_noacl:
75354ceac45SDavid Howells 	server->caps &= ~NFS_CAP_ACLS;
75454ceac45SDavid Howells }
75554ceac45SDavid Howells #else
75654ceac45SDavid Howells static inline void nfs_init_server_aclclient(struct nfs_server *server)
75754ceac45SDavid Howells {
75854ceac45SDavid Howells 	server->flags &= ~NFS_MOUNT_NOACL;
75954ceac45SDavid Howells 	server->caps &= ~NFS_CAP_ACLS;
76054ceac45SDavid Howells }
76154ceac45SDavid Howells #endif
76254ceac45SDavid Howells 
76354ceac45SDavid Howells /*
76454ceac45SDavid Howells  * Create a general RPC client
76554ceac45SDavid Howells  */
76633170233STrond Myklebust static int nfs_init_server_rpcclient(struct nfs_server *server,
76733170233STrond Myklebust 		const struct rpc_timeout *timeo,
76833170233STrond Myklebust 		rpc_authflavor_t pseudoflavour)
76954ceac45SDavid Howells {
77054ceac45SDavid Howells 	struct nfs_client *clp = server->nfs_client;
77154ceac45SDavid Howells 
77254ceac45SDavid Howells 	server->client = rpc_clone_client(clp->cl_rpcclient);
77354ceac45SDavid Howells 	if (IS_ERR(server->client)) {
7743110ff80SHarvey Harrison 		dprintk("%s: couldn't create rpc_client!\n", __func__);
77554ceac45SDavid Howells 		return PTR_ERR(server->client);
77654ceac45SDavid Howells 	}
77754ceac45SDavid Howells 
77833170233STrond Myklebust 	memcpy(&server->client->cl_timeout_default,
77933170233STrond Myklebust 			timeo,
78033170233STrond Myklebust 			sizeof(server->client->cl_timeout_default));
78133170233STrond Myklebust 	server->client->cl_timeout = &server->client->cl_timeout_default;
78233170233STrond Myklebust 
78354ceac45SDavid Howells 	if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
78454ceac45SDavid Howells 		struct rpc_auth *auth;
78554ceac45SDavid Howells 
78654ceac45SDavid Howells 		auth = rpcauth_create(pseudoflavour, server->client);
78754ceac45SDavid Howells 		if (IS_ERR(auth)) {
7883110ff80SHarvey Harrison 			dprintk("%s: couldn't create credcache!\n", __func__);
78954ceac45SDavid Howells 			return PTR_ERR(auth);
79054ceac45SDavid Howells 		}
79154ceac45SDavid Howells 	}
79254ceac45SDavid Howells 	server->client->cl_softrtry = 0;
79354ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_SOFT)
79454ceac45SDavid Howells 		server->client->cl_softrtry = 1;
79554ceac45SDavid Howells 
79654ceac45SDavid Howells 	return 0;
79754ceac45SDavid Howells }
79854ceac45SDavid Howells 
79954ceac45SDavid Howells /*
80054ceac45SDavid Howells  * Initialise an NFS2 or NFS3 client
80154ceac45SDavid Howells  */
80245a52a02SAndy Adamson int nfs_init_client(struct nfs_client *clp, const struct rpc_timeout *timeparms,
80345a52a02SAndy Adamson 		    const char *ip_addr, rpc_authflavor_t authflavour,
80445a52a02SAndy Adamson 		    int noresvport)
80554ceac45SDavid Howells {
80654ceac45SDavid Howells 	int error;
80754ceac45SDavid Howells 
80854ceac45SDavid Howells 	if (clp->cl_cons_state == NFS_CS_READY) {
80954ceac45SDavid Howells 		/* the client is already initialised */
81054ceac45SDavid Howells 		dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
81154ceac45SDavid Howells 		return 0;
81254ceac45SDavid Howells 	}
81354ceac45SDavid Howells 
81454ceac45SDavid Howells 	/*
81554ceac45SDavid Howells 	 * Create a client RPC handle for doing FSSTAT with UNIX auth only
81654ceac45SDavid Howells 	 * - RFC 2623, sec 2.3.2
81754ceac45SDavid Howells 	 */
818d740351bSChuck Lever 	error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
81945a52a02SAndy Adamson 				      0, noresvport);
82054ceac45SDavid Howells 	if (error < 0)
82154ceac45SDavid Howells 		goto error;
82254ceac45SDavid Howells 	nfs_mark_client_ready(clp, NFS_CS_READY);
82354ceac45SDavid Howells 	return 0;
82454ceac45SDavid Howells 
82554ceac45SDavid Howells error:
82654ceac45SDavid Howells 	nfs_mark_client_ready(clp, error);
82754ceac45SDavid Howells 	dprintk("<-- nfs_init_client() = xerror %d\n", error);
82854ceac45SDavid Howells 	return error;
82954ceac45SDavid Howells }
83054ceac45SDavid Howells 
83154ceac45SDavid Howells /*
83254ceac45SDavid Howells  * Create a version 2 or 3 client
83354ceac45SDavid Howells  */
8342283f8d6S\"Talpey, Thomas\ static int nfs_init_server(struct nfs_server *server,
8352283f8d6S\"Talpey, Thomas\ 			   const struct nfs_parsed_mount_data *data)
83654ceac45SDavid Howells {
8373a498026STrond Myklebust 	struct nfs_client_initdata cl_init = {
8383a498026STrond Myklebust 		.hostname = data->nfs_server.hostname,
839d7422c47SChuck Lever 		.addr = (const struct sockaddr *)&data->nfs_server.address,
8404c568017SChuck Lever 		.addrlen = data->nfs_server.addrlen,
84140c55319STrond Myklebust 		.rpc_ops = &nfs_v2_clientops,
84259dca3b2STrond Myklebust 		.proto = data->nfs_server.protocol,
843e50a7a1aSStanislav Kinsbursky 		.net = data->net,
8443a498026STrond Myklebust 	};
84533170233STrond Myklebust 	struct rpc_timeout timeparms;
84654ceac45SDavid Howells 	struct nfs_client *clp;
8473a498026STrond Myklebust 	int error;
84854ceac45SDavid Howells 
84954ceac45SDavid Howells 	dprintk("--> nfs_init_server()\n");
85054ceac45SDavid Howells 
85154ceac45SDavid Howells #ifdef CONFIG_NFS_V3
8528a6e5debSTrond Myklebust 	if (data->version == 3)
85340c55319STrond Myklebust 		cl_init.rpc_ops = &nfs_v3_clientops;
85454ceac45SDavid Howells #endif
85554ceac45SDavid Howells 
85645a52a02SAndy Adamson 	nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
85745a52a02SAndy Adamson 			data->timeo, data->retrans);
85845a52a02SAndy Adamson 
85954ceac45SDavid Howells 	/* Allocate or find a client reference we can use */
86045a52a02SAndy Adamson 	clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX,
86145a52a02SAndy Adamson 			     data->flags & NFS_MOUNT_NORESVPORT);
86254ceac45SDavid Howells 	if (IS_ERR(clp)) {
86354ceac45SDavid Howells 		dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
86454ceac45SDavid Howells 		return PTR_ERR(clp);
86554ceac45SDavid Howells 	}
86654ceac45SDavid Howells 
86754ceac45SDavid Howells 	server->nfs_client = clp;
86854ceac45SDavid Howells 
86954ceac45SDavid Howells 	/* Initialise the client representation from the mount data */
870ff3525a5STrond Myklebust 	server->flags = data->flags;
871b797cac7SDavid Howells 	server->options = data->options;
87262ab460cSTrond Myklebust 	server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
87362ab460cSTrond Myklebust 		NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
87462ab460cSTrond Myklebust 		NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
87554ceac45SDavid Howells 
87654ceac45SDavid Howells 	if (data->rsize)
87754ceac45SDavid Howells 		server->rsize = nfs_block_size(data->rsize, NULL);
87854ceac45SDavid Howells 	if (data->wsize)
87954ceac45SDavid Howells 		server->wsize = nfs_block_size(data->wsize, NULL);
88054ceac45SDavid Howells 
88154ceac45SDavid Howells 	server->acregmin = data->acregmin * HZ;
88254ceac45SDavid Howells 	server->acregmax = data->acregmax * HZ;
88354ceac45SDavid Howells 	server->acdirmin = data->acdirmin * HZ;
88454ceac45SDavid Howells 	server->acdirmax = data->acdirmax * HZ;
88554ceac45SDavid Howells 
88654ceac45SDavid Howells 	/* Start lockd here, before we might error out */
88754ceac45SDavid Howells 	error = nfs_start_lockd(server);
88854ceac45SDavid Howells 	if (error < 0)
88954ceac45SDavid Howells 		goto error;
89054ceac45SDavid Howells 
891f22d6d79SChuck Lever 	server->port = data->nfs_server.port;
892f22d6d79SChuck Lever 
89333170233STrond Myklebust 	error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
89454ceac45SDavid Howells 	if (error < 0)
89554ceac45SDavid Howells 		goto error;
89654ceac45SDavid Howells 
8973f8400d1SChuck Lever 	/* Preserve the values of mount_server-related mount options */
8983f8400d1SChuck Lever 	if (data->mount_server.addrlen) {
8993f8400d1SChuck Lever 		memcpy(&server->mountd_address, &data->mount_server.address,
9003f8400d1SChuck Lever 			data->mount_server.addrlen);
9013f8400d1SChuck Lever 		server->mountd_addrlen = data->mount_server.addrlen;
9023f8400d1SChuck Lever 	}
9033f8400d1SChuck Lever 	server->mountd_version = data->mount_server.version;
9043f8400d1SChuck Lever 	server->mountd_port = data->mount_server.port;
9053f8400d1SChuck Lever 	server->mountd_protocol = data->mount_server.protocol;
9063f8400d1SChuck Lever 
90754ceac45SDavid Howells 	server->namelen  = data->namlen;
90854ceac45SDavid Howells 	/* Create a client RPC handle for the NFSv3 ACL management interface */
90954ceac45SDavid Howells 	nfs_init_server_aclclient(server);
91054ceac45SDavid Howells 	dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
91154ceac45SDavid Howells 	return 0;
91254ceac45SDavid Howells 
91354ceac45SDavid Howells error:
91454ceac45SDavid Howells 	server->nfs_client = NULL;
91554ceac45SDavid Howells 	nfs_put_client(clp);
91654ceac45SDavid Howells 	dprintk("<-- nfs_init_server() = xerror %d\n", error);
91754ceac45SDavid Howells 	return error;
91854ceac45SDavid Howells }
91954ceac45SDavid Howells 
92054ceac45SDavid Howells /*
92154ceac45SDavid Howells  * Load up the server record from information gained in an fsinfo record
92254ceac45SDavid Howells  */
923738fd0f3SBenny Halevy static void nfs_server_set_fsinfo(struct nfs_server *server,
924738fd0f3SBenny Halevy 				  struct nfs_fh *mntfh,
925738fd0f3SBenny Halevy 				  struct nfs_fsinfo *fsinfo)
92654ceac45SDavid Howells {
92754ceac45SDavid Howells 	unsigned long max_rpc_payload;
92854ceac45SDavid Howells 
92954ceac45SDavid Howells 	/* Work out a lot of parameters */
93054ceac45SDavid Howells 	if (server->rsize == 0)
93154ceac45SDavid Howells 		server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
93254ceac45SDavid Howells 	if (server->wsize == 0)
93354ceac45SDavid Howells 		server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
93454ceac45SDavid Howells 
93554ceac45SDavid Howells 	if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
93654ceac45SDavid Howells 		server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
93754ceac45SDavid Howells 	if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
93854ceac45SDavid Howells 		server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
93954ceac45SDavid Howells 
94054ceac45SDavid Howells 	max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
94154ceac45SDavid Howells 	if (server->rsize > max_rpc_payload)
94254ceac45SDavid Howells 		server->rsize = max_rpc_payload;
94354ceac45SDavid Howells 	if (server->rsize > NFS_MAX_FILE_IO_SIZE)
94454ceac45SDavid Howells 		server->rsize = NFS_MAX_FILE_IO_SIZE;
94554ceac45SDavid Howells 	server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
946e0bf68ddSPeter Zijlstra 
947d993831fSJens Axboe 	server->backing_dev_info.name = "nfs";
94854ceac45SDavid Howells 	server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
94954ceac45SDavid Howells 
95054ceac45SDavid Howells 	if (server->wsize > max_rpc_payload)
95154ceac45SDavid Howells 		server->wsize = max_rpc_payload;
95254ceac45SDavid Howells 	if (server->wsize > NFS_MAX_FILE_IO_SIZE)
95354ceac45SDavid Howells 		server->wsize = NFS_MAX_FILE_IO_SIZE;
95454ceac45SDavid Howells 	server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
955dae100c2SFred Isaman 	server->pnfs_blksize = fsinfo->blksize;
956738fd0f3SBenny Halevy 	set_pnfs_layoutdriver(server, mntfh, fsinfo->layouttype);
95785e174baSRicardo Labiaga 
95854ceac45SDavid Howells 	server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
95954ceac45SDavid Howells 
96054ceac45SDavid Howells 	server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
96156e4ebf8SBryan Schumaker 	if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
96256e4ebf8SBryan Schumaker 		server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
96354ceac45SDavid Howells 	if (server->dtsize > server->rsize)
96454ceac45SDavid Howells 		server->dtsize = server->rsize;
96554ceac45SDavid Howells 
96654ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_NOAC) {
96754ceac45SDavid Howells 		server->acregmin = server->acregmax = 0;
96854ceac45SDavid Howells 		server->acdirmin = server->acdirmax = 0;
96954ceac45SDavid Howells 	}
97054ceac45SDavid Howells 
97154ceac45SDavid Howells 	server->maxfilesize = fsinfo->maxfilesize;
97254ceac45SDavid Howells 
9736b96724eSRicardo Labiaga 	server->time_delta = fsinfo->time_delta;
9746b96724eSRicardo Labiaga 
97554ceac45SDavid Howells 	/* We're airborne Set socket buffersize */
97654ceac45SDavid Howells 	rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
97754ceac45SDavid Howells }
97854ceac45SDavid Howells 
97954ceac45SDavid Howells /*
98054ceac45SDavid Howells  * Probe filesystem information, including the FSID on v2/v3
98154ceac45SDavid Howells  */
98254ceac45SDavid Howells static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
98354ceac45SDavid Howells {
98454ceac45SDavid Howells 	struct nfs_fsinfo fsinfo;
98554ceac45SDavid Howells 	struct nfs_client *clp = server->nfs_client;
98654ceac45SDavid Howells 	int error;
98754ceac45SDavid Howells 
98854ceac45SDavid Howells 	dprintk("--> nfs_probe_fsinfo()\n");
98954ceac45SDavid Howells 
99054ceac45SDavid Howells 	if (clp->rpc_ops->set_capabilities != NULL) {
99154ceac45SDavid Howells 		error = clp->rpc_ops->set_capabilities(server, mntfh);
99254ceac45SDavid Howells 		if (error < 0)
99354ceac45SDavid Howells 			goto out_error;
99454ceac45SDavid Howells 	}
99554ceac45SDavid Howells 
99654ceac45SDavid Howells 	fsinfo.fattr = fattr;
99785e174baSRicardo Labiaga 	fsinfo.layouttype = 0;
99854ceac45SDavid Howells 	error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
99954ceac45SDavid Howells 	if (error < 0)
100054ceac45SDavid Howells 		goto out_error;
100154ceac45SDavid Howells 
1002738fd0f3SBenny Halevy 	nfs_server_set_fsinfo(server, mntfh, &fsinfo);
100354ceac45SDavid Howells 
100454ceac45SDavid Howells 	/* Get some general file system info */
100554ceac45SDavid Howells 	if (server->namelen == 0) {
100654ceac45SDavid Howells 		struct nfs_pathconf pathinfo;
100754ceac45SDavid Howells 
100854ceac45SDavid Howells 		pathinfo.fattr = fattr;
100954ceac45SDavid Howells 		nfs_fattr_init(fattr);
101054ceac45SDavid Howells 
101154ceac45SDavid Howells 		if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
101254ceac45SDavid Howells 			server->namelen = pathinfo.max_namelen;
101354ceac45SDavid Howells 	}
101454ceac45SDavid Howells 
101554ceac45SDavid Howells 	dprintk("<-- nfs_probe_fsinfo() = 0\n");
101654ceac45SDavid Howells 	return 0;
101754ceac45SDavid Howells 
101854ceac45SDavid Howells out_error:
101954ceac45SDavid Howells 	dprintk("nfs_probe_fsinfo: error = %d\n", -error);
102054ceac45SDavid Howells 	return error;
102154ceac45SDavid Howells }
102254ceac45SDavid Howells 
102354ceac45SDavid Howells /*
102454ceac45SDavid Howells  * Copy useful information when duplicating a server record
102554ceac45SDavid Howells  */
102654ceac45SDavid Howells static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
102754ceac45SDavid Howells {
102854ceac45SDavid Howells 	target->flags = source->flags;
1029356e76b8SChuck Lever 	target->rsize = source->rsize;
1030356e76b8SChuck Lever 	target->wsize = source->wsize;
103154ceac45SDavid Howells 	target->acregmin = source->acregmin;
103254ceac45SDavid Howells 	target->acregmax = source->acregmax;
103354ceac45SDavid Howells 	target->acdirmin = source->acdirmin;
103454ceac45SDavid Howells 	target->acdirmax = source->acdirmax;
103554ceac45SDavid Howells 	target->caps = source->caps;
10362df54806SDavid Howells 	target->options = source->options;
103754ceac45SDavid Howells }
103854ceac45SDavid Howells 
1039fca5238eSChuck Lever static void nfs_server_insert_lists(struct nfs_server *server)
1040fca5238eSChuck Lever {
1041fca5238eSChuck Lever 	struct nfs_client *clp = server->nfs_client;
1042c25d32b2SStanislav Kinsbursky 	struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
1043fca5238eSChuck Lever 
1044fca5238eSChuck Lever 	spin_lock(&nfs_client_lock);
1045fca5238eSChuck Lever 	list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
1046c25d32b2SStanislav Kinsbursky 	list_add_tail(&server->master_link, &nn->nfs_volume_list);
1047d3b4c9d7SAndy Adamson 	clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
1048fca5238eSChuck Lever 	spin_unlock(&nfs_client_lock);
1049fca5238eSChuck Lever 
1050fca5238eSChuck Lever }
1051fca5238eSChuck Lever 
1052fca5238eSChuck Lever static void nfs_server_remove_lists(struct nfs_server *server)
1053fca5238eSChuck Lever {
1054d3b4c9d7SAndy Adamson 	struct nfs_client *clp = server->nfs_client;
1055d3b4c9d7SAndy Adamson 
1056fca5238eSChuck Lever 	spin_lock(&nfs_client_lock);
1057fca5238eSChuck Lever 	list_del_rcu(&server->client_link);
1058d3b4c9d7SAndy Adamson 	if (clp && list_empty(&clp->cl_superblocks))
1059d3b4c9d7SAndy Adamson 		set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
1060fca5238eSChuck Lever 	list_del(&server->master_link);
1061fca5238eSChuck Lever 	spin_unlock(&nfs_client_lock);
1062fca5238eSChuck Lever 
1063fca5238eSChuck Lever 	synchronize_rcu();
1064fca5238eSChuck Lever }
1065fca5238eSChuck Lever 
106654ceac45SDavid Howells /*
106754ceac45SDavid Howells  * Allocate and initialise a server record
106854ceac45SDavid Howells  */
106954ceac45SDavid Howells static struct nfs_server *nfs_alloc_server(void)
107054ceac45SDavid Howells {
107154ceac45SDavid Howells 	struct nfs_server *server;
107254ceac45SDavid Howells 
107354ceac45SDavid Howells 	server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
107454ceac45SDavid Howells 	if (!server)
107554ceac45SDavid Howells 		return NULL;
107654ceac45SDavid Howells 
107754ceac45SDavid Howells 	server->client = server->client_acl = ERR_PTR(-EINVAL);
107854ceac45SDavid Howells 
107954ceac45SDavid Howells 	/* Zero out the NFS state stuff */
108054ceac45SDavid Howells 	INIT_LIST_HEAD(&server->client_link);
108154ceac45SDavid Howells 	INIT_LIST_HEAD(&server->master_link);
1082d3978bb3SChuck Lever 	INIT_LIST_HEAD(&server->delegations);
10836382a441SWeston Andros Adamson 	INIT_LIST_HEAD(&server->layouts);
10840aaaf5c4SChuck Lever 	INIT_LIST_HEAD(&server->state_owners_lru);
108554ceac45SDavid Howells 
1086ef818a28SSteve Dickson 	atomic_set(&server->active, 0);
1087ef818a28SSteve Dickson 
108854ceac45SDavid Howells 	server->io_stats = nfs_alloc_iostats();
108954ceac45SDavid Howells 	if (!server->io_stats) {
109054ceac45SDavid Howells 		kfree(server);
109154ceac45SDavid Howells 		return NULL;
109254ceac45SDavid Howells 	}
109354ceac45SDavid Howells 
109448d07649SJens Axboe 	if (bdi_init(&server->backing_dev_info)) {
109548d07649SJens Axboe 		nfs_free_iostats(server->io_stats);
109648d07649SJens Axboe 		kfree(server);
109748d07649SJens Axboe 		return NULL;
109848d07649SJens Axboe 	}
109948d07649SJens Axboe 
11009157c31dSTrond Myklebust 	ida_init(&server->openowner_id);
1101d2d7ce28STrond Myklebust 	ida_init(&server->lockowner_id);
1102f7e8917aSFred Isaman 	pnfs_init_server(server);
1103f7e8917aSFred Isaman 
110454ceac45SDavid Howells 	return server;
110554ceac45SDavid Howells }
110654ceac45SDavid Howells 
110754ceac45SDavid Howells /*
110854ceac45SDavid Howells  * Free up a server record
110954ceac45SDavid Howells  */
111054ceac45SDavid Howells void nfs_free_server(struct nfs_server *server)
111154ceac45SDavid Howells {
111254ceac45SDavid Howells 	dprintk("--> nfs_free_server()\n");
111354ceac45SDavid Howells 
1114fca5238eSChuck Lever 	nfs_server_remove_lists(server);
111585e174baSRicardo Labiaga 	unset_pnfs_layoutdriver(server);
111654ceac45SDavid Howells 
111754ceac45SDavid Howells 	if (server->destroy != NULL)
111854ceac45SDavid Howells 		server->destroy(server);
11195cef338bSTrond Myklebust 
11205cef338bSTrond Myklebust 	if (!IS_ERR(server->client_acl))
11215cef338bSTrond Myklebust 		rpc_shutdown_client(server->client_acl);
112254ceac45SDavid Howells 	if (!IS_ERR(server->client))
112354ceac45SDavid Howells 		rpc_shutdown_client(server->client);
112454ceac45SDavid Howells 
112554ceac45SDavid Howells 	nfs_put_client(server->nfs_client);
112654ceac45SDavid Howells 
1127d2d7ce28STrond Myklebust 	ida_destroy(&server->lockowner_id);
11289157c31dSTrond Myklebust 	ida_destroy(&server->openowner_id);
112954ceac45SDavid Howells 	nfs_free_iostats(server->io_stats);
1130e0bf68ddSPeter Zijlstra 	bdi_destroy(&server->backing_dev_info);
113154ceac45SDavid Howells 	kfree(server);
113254ceac45SDavid Howells 	nfs_release_automount_timer();
113354ceac45SDavid Howells 	dprintk("<-- nfs_free_server()\n");
113454ceac45SDavid Howells }
113554ceac45SDavid Howells 
113654ceac45SDavid Howells /*
113754ceac45SDavid Howells  * Create a version 2 or 3 volume record
113854ceac45SDavid Howells  * - keyed on server and FSID
113954ceac45SDavid Howells  */
11402283f8d6S\"Talpey, Thomas\ struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
114154ceac45SDavid Howells 				     struct nfs_fh *mntfh)
114254ceac45SDavid Howells {
114354ceac45SDavid Howells 	struct nfs_server *server;
1144fbca779aSTrond Myklebust 	struct nfs_fattr *fattr;
114554ceac45SDavid Howells 	int error;
114654ceac45SDavid Howells 
114754ceac45SDavid Howells 	server = nfs_alloc_server();
114854ceac45SDavid Howells 	if (!server)
114954ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
115054ceac45SDavid Howells 
1151fbca779aSTrond Myklebust 	error = -ENOMEM;
1152fbca779aSTrond Myklebust 	fattr = nfs_alloc_fattr();
1153fbca779aSTrond Myklebust 	if (fattr == NULL)
1154fbca779aSTrond Myklebust 		goto error;
1155fbca779aSTrond Myklebust 
115654ceac45SDavid Howells 	/* Get a client representation */
115754ceac45SDavid Howells 	error = nfs_init_server(server, data);
115854ceac45SDavid Howells 	if (error < 0)
115954ceac45SDavid Howells 		goto error;
116054ceac45SDavid Howells 
116154ceac45SDavid Howells 	BUG_ON(!server->nfs_client);
116254ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops);
116354ceac45SDavid Howells 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
116454ceac45SDavid Howells 
116554ceac45SDavid Howells 	/* Probe the root fh to retrieve its FSID */
1166fbca779aSTrond Myklebust 	error = nfs_probe_fsinfo(server, mntfh, fattr);
116754ceac45SDavid Howells 	if (error < 0)
116854ceac45SDavid Howells 		goto error;
116954af3bb5STrond Myklebust 	if (server->nfs_client->rpc_ops->version == 3) {
117054af3bb5STrond Myklebust 		if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
117154af3bb5STrond Myklebust 			server->namelen = NFS3_MAXNAMLEN;
117254af3bb5STrond Myklebust 		if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
117354af3bb5STrond Myklebust 			server->caps |= NFS_CAP_READDIRPLUS;
117454af3bb5STrond Myklebust 	} else {
117554af3bb5STrond Myklebust 		if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
117654af3bb5STrond Myklebust 			server->namelen = NFS2_MAXNAMLEN;
117754af3bb5STrond Myklebust 	}
117854af3bb5STrond Myklebust 
1179fbca779aSTrond Myklebust 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
1180fbca779aSTrond Myklebust 		error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
118154ceac45SDavid Howells 		if (error < 0) {
118254ceac45SDavid Howells 			dprintk("nfs_create_server: getattr error = %d\n", -error);
118354ceac45SDavid Howells 			goto error;
118454ceac45SDavid Howells 		}
118554ceac45SDavid Howells 	}
1186fbca779aSTrond Myklebust 	memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
118754ceac45SDavid Howells 
11886daabf1bSDavid Howells 	dprintk("Server FSID: %llx:%llx\n",
11896daabf1bSDavid Howells 		(unsigned long long) server->fsid.major,
11906daabf1bSDavid Howells 		(unsigned long long) server->fsid.minor);
119154ceac45SDavid Howells 
1192fca5238eSChuck Lever 	nfs_server_insert_lists(server);
119354ceac45SDavid Howells 	server->mount_time = jiffies;
1194fbca779aSTrond Myklebust 	nfs_free_fattr(fattr);
119554ceac45SDavid Howells 	return server;
119654ceac45SDavid Howells 
119754ceac45SDavid Howells error:
1198fbca779aSTrond Myklebust 	nfs_free_fattr(fattr);
119954ceac45SDavid Howells 	nfs_free_server(server);
120054ceac45SDavid Howells 	return ERR_PTR(error);
120154ceac45SDavid Howells }
120254ceac45SDavid Howells 
120354ceac45SDavid Howells #ifdef CONFIG_NFS_V4
120454ceac45SDavid Howells /*
1205c36fca52SAndy Adamson  * NFSv4.0 callback thread helper
1206c36fca52SAndy Adamson  *
1207c36fca52SAndy Adamson  * Find a client by callback identifier
1208c36fca52SAndy Adamson  */
1209c36fca52SAndy Adamson struct nfs_client *
1210*28cd1b3fSStanislav Kinsbursky nfs4_find_client_ident(struct net *net, int cb_ident)
1211c36fca52SAndy Adamson {
1212c36fca52SAndy Adamson 	struct nfs_client *clp;
1213*28cd1b3fSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(net, nfs_net_id);
1214c36fca52SAndy Adamson 
1215c36fca52SAndy Adamson 	spin_lock(&nfs_client_lock);
1216*28cd1b3fSStanislav Kinsbursky 	clp = idr_find(&nn->cb_ident_idr, cb_ident);
1217c36fca52SAndy Adamson 	if (clp)
1218c36fca52SAndy Adamson 		atomic_inc(&clp->cl_count);
1219c36fca52SAndy Adamson 	spin_unlock(&nfs_client_lock);
1220c36fca52SAndy Adamson 	return clp;
1221c36fca52SAndy Adamson }
1222c36fca52SAndy Adamson 
1223c36fca52SAndy Adamson #if defined(CONFIG_NFS_V4_1)
1224c36fca52SAndy Adamson /*
1225c36fca52SAndy Adamson  * NFSv4.1 callback thread helper
1226c36fca52SAndy Adamson  * For CB_COMPOUND calls, find a client by IP address, protocol version,
1227c36fca52SAndy Adamson  * minorversion, and sessionID
1228c36fca52SAndy Adamson  *
1229c36fca52SAndy Adamson  * Returns NULL if no such client
1230c36fca52SAndy Adamson  */
1231c36fca52SAndy Adamson struct nfs_client *
1232c36fca52SAndy Adamson nfs4_find_client_sessionid(const struct sockaddr *addr,
1233778be232SAndy Adamson 			   struct nfs4_sessionid *sid)
1234c36fca52SAndy Adamson {
1235c36fca52SAndy Adamson 	struct nfs_client *clp;
12366b13168bSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(&init_net, nfs_net_id);
1237c36fca52SAndy Adamson 
1238c36fca52SAndy Adamson 	spin_lock(&nfs_client_lock);
12396b13168bSStanislav Kinsbursky 	list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
1240c36fca52SAndy Adamson 		if (nfs4_cb_match_client(addr, clp, 1) == false)
1241c36fca52SAndy Adamson 			continue;
1242c36fca52SAndy Adamson 
1243c36fca52SAndy Adamson 		if (!nfs4_has_session(clp))
1244c36fca52SAndy Adamson 			continue;
1245c36fca52SAndy Adamson 
1246778be232SAndy Adamson 		/* Match sessionid*/
1247778be232SAndy Adamson 		if (memcmp(clp->cl_session->sess_id.data,
1248778be232SAndy Adamson 		    sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
1249c36fca52SAndy Adamson 			continue;
1250c36fca52SAndy Adamson 
1251c36fca52SAndy Adamson 		atomic_inc(&clp->cl_count);
1252c36fca52SAndy Adamson 		spin_unlock(&nfs_client_lock);
1253c36fca52SAndy Adamson 		return clp;
1254c36fca52SAndy Adamson 	}
1255c36fca52SAndy Adamson 	spin_unlock(&nfs_client_lock);
1256c36fca52SAndy Adamson 	return NULL;
1257c36fca52SAndy Adamson }
1258c36fca52SAndy Adamson 
1259c36fca52SAndy Adamson #else /* CONFIG_NFS_V4_1 */
1260c36fca52SAndy Adamson 
1261c36fca52SAndy Adamson struct nfs_client *
1262c36fca52SAndy Adamson nfs4_find_client_sessionid(const struct sockaddr *addr,
1263778be232SAndy Adamson 			   struct nfs4_sessionid *sid)
1264c36fca52SAndy Adamson {
1265c36fca52SAndy Adamson 	return NULL;
1266c36fca52SAndy Adamson }
1267c36fca52SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1268c36fca52SAndy Adamson 
1269c36fca52SAndy Adamson /*
12709bdaa86dSBenny Halevy  * Initialize the NFS4 callback service
12719bdaa86dSBenny Halevy  */
12729bdaa86dSBenny Halevy static int nfs4_init_callback(struct nfs_client *clp)
12739bdaa86dSBenny Halevy {
12749bdaa86dSBenny Halevy 	int error;
12759bdaa86dSBenny Halevy 
12769bdaa86dSBenny Halevy 	if (clp->rpc_ops->version == 4) {
12770b5b7ae0SAndy Adamson 		if (nfs4_has_session(clp)) {
12780b5b7ae0SAndy Adamson 			error = xprt_setup_backchannel(
12790b5b7ae0SAndy Adamson 						clp->cl_rpcclient->cl_xprt,
12800b5b7ae0SAndy Adamson 						NFS41_BC_MIN_CALLBACKS);
12810b5b7ae0SAndy Adamson 			if (error < 0)
12820b5b7ae0SAndy Adamson 				return error;
12830b5b7ae0SAndy Adamson 		}
12840b5b7ae0SAndy Adamson 
128597dc1359STrond Myklebust 		error = nfs_callback_up(clp->cl_mvops->minor_version,
128671468513SBenny Halevy 					clp->cl_rpcclient->cl_xprt);
12879bdaa86dSBenny Halevy 		if (error < 0) {
12889bdaa86dSBenny Halevy 			dprintk("%s: failed to start callback. Error = %d\n",
12899bdaa86dSBenny Halevy 				__func__, error);
12909bdaa86dSBenny Halevy 			return error;
12919bdaa86dSBenny Halevy 		}
12929bdaa86dSBenny Halevy 		__set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
12939bdaa86dSBenny Halevy 	}
12949bdaa86dSBenny Halevy 	return 0;
12959bdaa86dSBenny Halevy }
12969bdaa86dSBenny Halevy 
12979bdaa86dSBenny Halevy /*
1298557134a3SAndy Adamson  * Initialize the minor version specific parts of an NFS4 client record
1299557134a3SAndy Adamson  */
1300557134a3SAndy Adamson static int nfs4_init_client_minor_version(struct nfs_client *clp)
1301557134a3SAndy Adamson {
1302557134a3SAndy Adamson #if defined(CONFIG_NFS_V4_1)
130397dc1359STrond Myklebust 	if (clp->cl_mvops->minor_version) {
1304557134a3SAndy Adamson 		struct nfs4_session *session = NULL;
1305557134a3SAndy Adamson 		/*
1306557134a3SAndy Adamson 		 * Create the session and mark it expired.
1307557134a3SAndy Adamson 		 * When a SEQUENCE operation encounters the expired session
1308557134a3SAndy Adamson 		 * it will do session recovery to initialize it.
1309557134a3SAndy Adamson 		 */
1310557134a3SAndy Adamson 		session = nfs4_alloc_session(clp);
1311557134a3SAndy Adamson 		if (!session)
1312557134a3SAndy Adamson 			return -ENOMEM;
1313557134a3SAndy Adamson 
1314557134a3SAndy Adamson 		clp->cl_session = session;
1315fe74ba3aSTrond Myklebust 		/*
1316fe74ba3aSTrond Myklebust 		 * The create session reply races with the server back
1317fe74ba3aSTrond Myklebust 		 * channel probe. Mark the client NFS_CS_SESSION_INITING
1318fe74ba3aSTrond Myklebust 		 * so that the client back channel can find the
1319fe74ba3aSTrond Myklebust 		 * nfs_client struct
1320fe74ba3aSTrond Myklebust 		 */
1321fe74ba3aSTrond Myklebust 		clp->cl_cons_state = NFS_CS_SESSION_INITING;
1322557134a3SAndy Adamson 	}
1323557134a3SAndy Adamson #endif /* CONFIG_NFS_V4_1 */
1324557134a3SAndy Adamson 
132571468513SBenny Halevy 	return nfs4_init_callback(clp);
1326557134a3SAndy Adamson }
1327557134a3SAndy Adamson 
1328557134a3SAndy Adamson /*
132954ceac45SDavid Howells  * Initialise an NFS4 client record
133054ceac45SDavid Howells  */
133145a52a02SAndy Adamson int nfs4_init_client(struct nfs_client *clp,
133233170233STrond Myklebust 		     const struct rpc_timeout *timeparms,
13337d9ac06fSJ. Bruce Fields 		     const char *ip_addr,
1334d740351bSChuck Lever 		     rpc_authflavor_t authflavour,
133545a52a02SAndy Adamson 		     int noresvport)
133654ceac45SDavid Howells {
133754ceac45SDavid Howells 	int error;
133854ceac45SDavid Howells 
133954ceac45SDavid Howells 	if (clp->cl_cons_state == NFS_CS_READY) {
134054ceac45SDavid Howells 		/* the client is initialised already */
134154ceac45SDavid Howells 		dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
134254ceac45SDavid Howells 		return 0;
134354ceac45SDavid Howells 	}
134454ceac45SDavid Howells 
134554ceac45SDavid Howells 	/* Check NFS protocol revision and initialize RPC op vector */
134654ceac45SDavid Howells 	clp->rpc_ops = &nfs_v4_clientops;
134754ceac45SDavid Howells 
134859dca3b2STrond Myklebust 	error = nfs_create_rpc_client(clp, timeparms, authflavour,
134945a52a02SAndy Adamson 				      1, noresvport);
135054ceac45SDavid Howells 	if (error < 0)
135154ceac45SDavid Howells 		goto error;
1352f4373bf9SBen Hutchings 	strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
135354ceac45SDavid Howells 
135454ceac45SDavid Howells 	error = nfs_idmap_new(clp);
135554ceac45SDavid Howells 	if (error < 0) {
135654ceac45SDavid Howells 		dprintk("%s: failed to create idmapper. Error = %d\n",
13573110ff80SHarvey Harrison 			__func__, error);
135854ceac45SDavid Howells 		goto error;
135954ceac45SDavid Howells 	}
13609c5bf38dSTrond Myklebust 	__set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
136154ceac45SDavid Howells 
1362557134a3SAndy Adamson 	error = nfs4_init_client_minor_version(clp);
1363557134a3SAndy Adamson 	if (error < 0)
1364557134a3SAndy Adamson 		goto error;
1365557134a3SAndy Adamson 
136676db6d95SAndy Adamson 	if (!nfs4_has_session(clp))
136754ceac45SDavid Howells 		nfs_mark_client_ready(clp, NFS_CS_READY);
136854ceac45SDavid Howells 	return 0;
136954ceac45SDavid Howells 
137054ceac45SDavid Howells error:
137154ceac45SDavid Howells 	nfs_mark_client_ready(clp, error);
137254ceac45SDavid Howells 	dprintk("<-- nfs4_init_client() = xerror %d\n", error);
137354ceac45SDavid Howells 	return error;
137454ceac45SDavid Howells }
137554ceac45SDavid Howells 
137654ceac45SDavid Howells /*
137754ceac45SDavid Howells  * Set up an NFS4 client
137854ceac45SDavid Howells  */
137954ceac45SDavid Howells static int nfs4_set_client(struct nfs_server *server,
1380dcecae0fSChuck Lever 		const char *hostname,
1381dcecae0fSChuck Lever 		const struct sockaddr *addr,
1382dcecae0fSChuck Lever 		const size_t addrlen,
13837d9ac06fSJ. Bruce Fields 		const char *ip_addr,
138454ceac45SDavid Howells 		rpc_authflavor_t authflavour,
138594a417f3SBenny Halevy 		int proto, const struct rpc_timeout *timeparms,
1386e50a7a1aSStanislav Kinsbursky 		u32 minorversion, struct net *net)
138754ceac45SDavid Howells {
13883a498026STrond Myklebust 	struct nfs_client_initdata cl_init = {
13893a498026STrond Myklebust 		.hostname = hostname,
1390dcecae0fSChuck Lever 		.addr = addr,
1391dcecae0fSChuck Lever 		.addrlen = addrlen,
139240c55319STrond Myklebust 		.rpc_ops = &nfs_v4_clientops,
139359dca3b2STrond Myklebust 		.proto = proto,
13945aae4a9aSBenny Halevy 		.minorversion = minorversion,
1395e50a7a1aSStanislav Kinsbursky 		.net = net,
13963a498026STrond Myklebust 	};
139754ceac45SDavid Howells 	struct nfs_client *clp;
139854ceac45SDavid Howells 	int error;
139954ceac45SDavid Howells 
140054ceac45SDavid Howells 	dprintk("--> nfs4_set_client()\n");
140154ceac45SDavid Howells 
140254ceac45SDavid Howells 	/* Allocate or find a client reference we can use */
140345a52a02SAndy Adamson 	clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour,
140445a52a02SAndy Adamson 			     server->flags & NFS_MOUNT_NORESVPORT);
140554ceac45SDavid Howells 	if (IS_ERR(clp)) {
140654ceac45SDavid Howells 		error = PTR_ERR(clp);
140754ceac45SDavid Howells 		goto error;
140854ceac45SDavid Howells 	}
140954ceac45SDavid Howells 
1410d6fb79d4SAndy Adamson 	/*
1411d6fb79d4SAndy Adamson 	 * Query for the lease time on clientid setup or renewal
1412d6fb79d4SAndy Adamson 	 *
1413d6fb79d4SAndy Adamson 	 * Note that this will be set on nfs_clients that were created
1414d6fb79d4SAndy Adamson 	 * only for the DS role and did not set this bit, but now will
1415d6fb79d4SAndy Adamson 	 * serve a dual role.
1416d6fb79d4SAndy Adamson 	 */
1417d6fb79d4SAndy Adamson 	set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
1418d6fb79d4SAndy Adamson 
141954ceac45SDavid Howells 	server->nfs_client = clp;
142054ceac45SDavid Howells 	dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
142154ceac45SDavid Howells 	return 0;
142254ceac45SDavid Howells error:
142354ceac45SDavid Howells 	dprintk("<-- nfs4_set_client() = xerror %d\n", error);
142454ceac45SDavid Howells 	return error;
142554ceac45SDavid Howells }
142654ceac45SDavid Howells 
1427d83217c1SAndy Adamson /*
1428d83217c1SAndy Adamson  * Set up a pNFS Data Server client.
1429d83217c1SAndy Adamson  *
1430d83217c1SAndy Adamson  * Return any existing nfs_client that matches server address,port,version
1431d83217c1SAndy Adamson  * and minorversion.
1432d83217c1SAndy Adamson  *
1433d83217c1SAndy Adamson  * For a new nfs_client, use a soft mount (default), a low retrans and a
1434d83217c1SAndy Adamson  * low timeout interval so that if a connection is lost, we retry through
1435d83217c1SAndy Adamson  * the MDS.
1436d83217c1SAndy Adamson  */
1437d83217c1SAndy Adamson struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
1438d83217c1SAndy Adamson 		const struct sockaddr *ds_addr,
1439d83217c1SAndy Adamson 		int ds_addrlen, int ds_proto)
1440d83217c1SAndy Adamson {
1441d83217c1SAndy Adamson 	struct nfs_client_initdata cl_init = {
1442d83217c1SAndy Adamson 		.addr = ds_addr,
1443d83217c1SAndy Adamson 		.addrlen = ds_addrlen,
1444d83217c1SAndy Adamson 		.rpc_ops = &nfs_v4_clientops,
1445d83217c1SAndy Adamson 		.proto = ds_proto,
1446d83217c1SAndy Adamson 		.minorversion = mds_clp->cl_minorversion,
1447e50a7a1aSStanislav Kinsbursky 		.net = mds_clp->net,
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 }
146894b134acSTrond Myklebust EXPORT_SYMBOL_GPL(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;
15390aaaf5c4SChuck Lever 	server->destroy = nfs4_destroy_server;
154044950b67STrond Myklebust out:
154144950b67STrond Myklebust 	nfs_free_fattr(fattr);
154244950b67STrond Myklebust 	return error;
154344950b67STrond Myklebust }
154444950b67STrond Myklebust 
154596b09e02SAndy Adamson /*
154654ceac45SDavid Howells  * Create a version 4 volume record
154754ceac45SDavid Howells  */
154854ceac45SDavid Howells static int nfs4_init_server(struct nfs_server *server,
154991ea40b9S\"Talpey, Thomas\ 		const struct nfs_parsed_mount_data *data)
155054ceac45SDavid Howells {
155133170233STrond Myklebust 	struct rpc_timeout timeparms;
155254ceac45SDavid Howells 	int error;
155354ceac45SDavid Howells 
155454ceac45SDavid Howells 	dprintk("--> nfs4_init_server()\n");
155554ceac45SDavid Howells 
155633170233STrond Myklebust 	nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
155733170233STrond Myklebust 			data->timeo, data->retrans);
155833170233STrond Myklebust 
1559542fcc33SChuck Lever 	/* Initialise the client representation from the mount data */
1560542fcc33SChuck Lever 	server->flags = data->flags;
156182f2e547SBryan Schumaker 	server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
156282f2e547SBryan Schumaker 	if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
156382f2e547SBryan Schumaker 			server->caps |= NFS_CAP_READDIRPLUS;
1564b797cac7SDavid Howells 	server->options = data->options;
1565542fcc33SChuck Lever 
156633170233STrond Myklebust 	/* Get a client record */
156733170233STrond Myklebust 	error = nfs4_set_client(server,
156833170233STrond Myklebust 			data->nfs_server.hostname,
156933170233STrond Myklebust 			(const struct sockaddr *)&data->nfs_server.address,
157033170233STrond Myklebust 			data->nfs_server.addrlen,
157133170233STrond Myklebust 			data->client_address,
157233170233STrond Myklebust 			data->auth_flavors[0],
157333170233STrond Myklebust 			data->nfs_server.protocol,
157494a417f3SBenny Halevy 			&timeparms,
1575e50a7a1aSStanislav Kinsbursky 			data->minorversion,
1576e50a7a1aSStanislav Kinsbursky 			data->net);
157733170233STrond Myklebust 	if (error < 0)
157833170233STrond Myklebust 		goto error;
157933170233STrond Myklebust 
1580b064eca2STrond Myklebust 	/*
1581b064eca2STrond Myklebust 	 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1582b064eca2STrond Myklebust 	 * authentication.
1583b064eca2STrond Myklebust 	 */
1584b064eca2STrond Myklebust 	if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
1585b064eca2STrond Myklebust 		server->caps |= NFS_CAP_UIDGID_NOMAP;
1586b064eca2STrond Myklebust 
158754ceac45SDavid Howells 	if (data->rsize)
158854ceac45SDavid Howells 		server->rsize = nfs_block_size(data->rsize, NULL);
158954ceac45SDavid Howells 	if (data->wsize)
159054ceac45SDavid Howells 		server->wsize = nfs_block_size(data->wsize, NULL);
159154ceac45SDavid Howells 
159254ceac45SDavid Howells 	server->acregmin = data->acregmin * HZ;
159354ceac45SDavid Howells 	server->acregmax = data->acregmax * HZ;
159454ceac45SDavid Howells 	server->acdirmin = data->acdirmin * HZ;
159554ceac45SDavid Howells 	server->acdirmax = data->acdirmax * HZ;
159654ceac45SDavid Howells 
1597f22d6d79SChuck Lever 	server->port = data->nfs_server.port;
1598f22d6d79SChuck Lever 
159933170233STrond Myklebust 	error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
160054ceac45SDavid Howells 
160133170233STrond Myklebust error:
160254ceac45SDavid Howells 	/* Done */
160354ceac45SDavid Howells 	dprintk("<-- nfs4_init_server() = %d\n", error);
160454ceac45SDavid Howells 	return error;
160554ceac45SDavid Howells }
160654ceac45SDavid Howells 
160754ceac45SDavid Howells /*
160854ceac45SDavid Howells  * Create a version 4 volume record
160954ceac45SDavid Howells  * - keyed on server and FSID
161054ceac45SDavid Howells  */
161191ea40b9S\"Talpey, Thomas\ struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
161254ceac45SDavid Howells 				      struct nfs_fh *mntfh)
161354ceac45SDavid Howells {
161454ceac45SDavid Howells 	struct nfs_server *server;
161554ceac45SDavid Howells 	int error;
161654ceac45SDavid Howells 
161754ceac45SDavid Howells 	dprintk("--> nfs4_create_server()\n");
161854ceac45SDavid Howells 
161954ceac45SDavid Howells 	server = nfs_alloc_server();
162054ceac45SDavid Howells 	if (!server)
162154ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
162254ceac45SDavid Howells 
162354ceac45SDavid Howells 	/* set up the general RPC client */
162491ea40b9S\"Talpey, Thomas\ 	error = nfs4_init_server(server, data);
162554ceac45SDavid Howells 	if (error < 0)
162654ceac45SDavid Howells 		goto error;
162754ceac45SDavid Howells 
162844950b67STrond Myklebust 	error = nfs4_server_common_setup(server, mntfh);
1629fccba804STrond Myklebust 	if (error < 0)
1630fccba804STrond Myklebust 		goto error;
1631557134a3SAndy Adamson 
163254ceac45SDavid Howells 	dprintk("<-- nfs4_create_server() = %p\n", server);
163354ceac45SDavid Howells 	return server;
163454ceac45SDavid Howells 
163554ceac45SDavid Howells error:
163654ceac45SDavid Howells 	nfs_free_server(server);
163754ceac45SDavid Howells 	dprintk("<-- nfs4_create_server() = error %d\n", error);
163854ceac45SDavid Howells 	return ERR_PTR(error);
163954ceac45SDavid Howells }
164054ceac45SDavid Howells 
164154ceac45SDavid Howells /*
164254ceac45SDavid Howells  * Create an NFS4 referral server record
164354ceac45SDavid Howells  */
164454ceac45SDavid Howells struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1645f2d0d85eSTrond Myklebust 					       struct nfs_fh *mntfh)
164654ceac45SDavid Howells {
164754ceac45SDavid Howells 	struct nfs_client *parent_client;
164854ceac45SDavid Howells 	struct nfs_server *server, *parent_server;
164954ceac45SDavid Howells 	int error;
165054ceac45SDavid Howells 
165154ceac45SDavid Howells 	dprintk("--> nfs4_create_referral_server()\n");
165254ceac45SDavid Howells 
165354ceac45SDavid Howells 	server = nfs_alloc_server();
165454ceac45SDavid Howells 	if (!server)
165554ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
165654ceac45SDavid Howells 
165754ceac45SDavid Howells 	parent_server = NFS_SB(data->sb);
165854ceac45SDavid Howells 	parent_client = parent_server->nfs_client;
165954ceac45SDavid Howells 
1660542fcc33SChuck Lever 	/* Initialise the client representation from the parent server */
1661542fcc33SChuck Lever 	nfs_server_copy_userdata(server, parent_server);
166262ab460cSTrond Myklebust 	server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
1663542fcc33SChuck Lever 
166454ceac45SDavid Howells 	/* Get a client representation.
166554ceac45SDavid Howells 	 * Note: NFSv4 always uses TCP, */
1666dcecae0fSChuck Lever 	error = nfs4_set_client(server, data->hostname,
16676677d095SChuck Lever 				data->addr,
16686677d095SChuck Lever 				data->addrlen,
16697d9ac06fSJ. Bruce Fields 				parent_client->cl_ipaddr,
167054ceac45SDavid Howells 				data->authflavor,
167154ceac45SDavid Howells 				parent_server->client->cl_xprt->prot,
167294a417f3SBenny Halevy 				parent_server->client->cl_timeout,
1673e50a7a1aSStanislav Kinsbursky 				parent_client->cl_mvops->minor_version,
1674e50a7a1aSStanislav Kinsbursky 				parent_client->net);
1675297de4f6Sandros@citi.umich.edu 	if (error < 0)
1676297de4f6Sandros@citi.umich.edu 		goto error;
167754ceac45SDavid Howells 
167833170233STrond Myklebust 	error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
167954ceac45SDavid Howells 	if (error < 0)
168054ceac45SDavid Howells 		goto error;
168154ceac45SDavid Howells 
168244950b67STrond Myklebust 	error = nfs4_server_common_setup(server, mntfh);
1683f2d0d85eSTrond Myklebust 	if (error < 0)
1684f2d0d85eSTrond Myklebust 		goto error;
1685f2d0d85eSTrond Myklebust 
168654ceac45SDavid Howells 	dprintk("<-- nfs_create_referral_server() = %p\n", server);
168754ceac45SDavid Howells 	return server;
168854ceac45SDavid Howells 
168954ceac45SDavid Howells error:
169054ceac45SDavid Howells 	nfs_free_server(server);
169154ceac45SDavid Howells 	dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
169254ceac45SDavid Howells 	return ERR_PTR(error);
169354ceac45SDavid Howells }
169454ceac45SDavid Howells 
169554ceac45SDavid Howells #endif /* CONFIG_NFS_V4 */
169654ceac45SDavid Howells 
169754ceac45SDavid Howells /*
169854ceac45SDavid Howells  * Clone an NFS2, NFS3 or NFS4 server record
169954ceac45SDavid Howells  */
170054ceac45SDavid Howells struct nfs_server *nfs_clone_server(struct nfs_server *source,
170154ceac45SDavid Howells 				    struct nfs_fh *fh,
170254ceac45SDavid Howells 				    struct nfs_fattr *fattr)
170354ceac45SDavid Howells {
170454ceac45SDavid Howells 	struct nfs_server *server;
1705fbca779aSTrond Myklebust 	struct nfs_fattr *fattr_fsinfo;
170654ceac45SDavid Howells 	int error;
170754ceac45SDavid Howells 
170854ceac45SDavid Howells 	dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
17096daabf1bSDavid Howells 		(unsigned long long) fattr->fsid.major,
17106daabf1bSDavid Howells 		(unsigned long long) fattr->fsid.minor);
171154ceac45SDavid Howells 
171254ceac45SDavid Howells 	server = nfs_alloc_server();
171354ceac45SDavid Howells 	if (!server)
171454ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
171554ceac45SDavid Howells 
1716fbca779aSTrond Myklebust 	error = -ENOMEM;
1717fbca779aSTrond Myklebust 	fattr_fsinfo = nfs_alloc_fattr();
1718fbca779aSTrond Myklebust 	if (fattr_fsinfo == NULL)
1719fbca779aSTrond Myklebust 		goto out_free_server;
1720fbca779aSTrond Myklebust 
172154ceac45SDavid Howells 	/* Copy data from the source */
172254ceac45SDavid Howells 	server->nfs_client = source->nfs_client;
17230aaaf5c4SChuck Lever 	server->destroy = source->destroy;
172454ceac45SDavid Howells 	atomic_inc(&server->nfs_client->cl_count);
172554ceac45SDavid Howells 	nfs_server_copy_userdata(server, source);
172654ceac45SDavid Howells 
172754ceac45SDavid Howells 	server->fsid = fattr->fsid;
172854ceac45SDavid Howells 
172933170233STrond Myklebust 	error = nfs_init_server_rpcclient(server,
173033170233STrond Myklebust 			source->client->cl_timeout,
173133170233STrond Myklebust 			source->client->cl_auth->au_flavor);
173254ceac45SDavid Howells 	if (error < 0)
173354ceac45SDavid Howells 		goto out_free_server;
173454ceac45SDavid Howells 	if (!IS_ERR(source->client_acl))
173554ceac45SDavid Howells 		nfs_init_server_aclclient(server);
173654ceac45SDavid Howells 
173754ceac45SDavid Howells 	/* probe the filesystem info for this server filesystem */
1738fbca779aSTrond Myklebust 	error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
173954ceac45SDavid Howells 	if (error < 0)
174054ceac45SDavid Howells 		goto out_free_server;
174154ceac45SDavid Howells 
174254af3bb5STrond Myklebust 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
174354af3bb5STrond Myklebust 		server->namelen = NFS4_MAXNAMLEN;
174454af3bb5STrond Myklebust 
174554ceac45SDavid Howells 	dprintk("Cloned FSID: %llx:%llx\n",
17466daabf1bSDavid Howells 		(unsigned long long) server->fsid.major,
17476daabf1bSDavid Howells 		(unsigned long long) server->fsid.minor);
174854ceac45SDavid Howells 
174954ceac45SDavid Howells 	error = nfs_start_lockd(server);
175054ceac45SDavid Howells 	if (error < 0)
175154ceac45SDavid Howells 		goto out_free_server;
175254ceac45SDavid Howells 
1753fca5238eSChuck Lever 	nfs_server_insert_lists(server);
175454ceac45SDavid Howells 	server->mount_time = jiffies;
175554ceac45SDavid Howells 
1756fbca779aSTrond Myklebust 	nfs_free_fattr(fattr_fsinfo);
175754ceac45SDavid Howells 	dprintk("<-- nfs_clone_server() = %p\n", server);
175854ceac45SDavid Howells 	return server;
175954ceac45SDavid Howells 
176054ceac45SDavid Howells out_free_server:
1761fbca779aSTrond Myklebust 	nfs_free_fattr(fattr_fsinfo);
176254ceac45SDavid Howells 	nfs_free_server(server);
176354ceac45SDavid Howells 	dprintk("<-- nfs_clone_server() = error %d\n", error);
176454ceac45SDavid Howells 	return ERR_PTR(error);
176554ceac45SDavid Howells }
17666aaca566SDavid Howells 
17676b13168bSStanislav Kinsbursky void nfs_clients_init(struct net *net)
17686b13168bSStanislav Kinsbursky {
17696b13168bSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(net, nfs_net_id);
17706b13168bSStanislav Kinsbursky 
17716b13168bSStanislav Kinsbursky 	INIT_LIST_HEAD(&nn->nfs_client_list);
1772c25d32b2SStanislav Kinsbursky 	INIT_LIST_HEAD(&nn->nfs_volume_list);
1773*28cd1b3fSStanislav Kinsbursky #ifdef CONFIG_NFS_V4
1774*28cd1b3fSStanislav Kinsbursky 	idr_init(&nn->cb_ident_idr);
1775*28cd1b3fSStanislav Kinsbursky #endif
17766b13168bSStanislav Kinsbursky }
17776b13168bSStanislav Kinsbursky 
17786aaca566SDavid Howells #ifdef CONFIG_PROC_FS
17796aaca566SDavid Howells static struct proc_dir_entry *proc_fs_nfs;
17806aaca566SDavid Howells 
17816aaca566SDavid Howells static int nfs_server_list_open(struct inode *inode, struct file *file);
17826aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
17836aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
17846aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v);
17856aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v);
17866aaca566SDavid Howells 
178788e9d34cSJames Morris static const struct seq_operations nfs_server_list_ops = {
17886aaca566SDavid Howells 	.start	= nfs_server_list_start,
17896aaca566SDavid Howells 	.next	= nfs_server_list_next,
17906aaca566SDavid Howells 	.stop	= nfs_server_list_stop,
17916aaca566SDavid Howells 	.show	= nfs_server_list_show,
17926aaca566SDavid Howells };
17936aaca566SDavid Howells 
179400977a59SArjan van de Ven static const struct file_operations nfs_server_list_fops = {
17956aaca566SDavid Howells 	.open		= nfs_server_list_open,
17966aaca566SDavid Howells 	.read		= seq_read,
17976aaca566SDavid Howells 	.llseek		= seq_lseek,
17986aaca566SDavid Howells 	.release	= seq_release,
179934b37235SDenis V. Lunev 	.owner		= THIS_MODULE,
18006aaca566SDavid Howells };
18016aaca566SDavid Howells 
18026aaca566SDavid Howells static int nfs_volume_list_open(struct inode *inode, struct file *file);
18036aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
18046aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
18056aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v);
18066aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v);
18076aaca566SDavid Howells 
180888e9d34cSJames Morris static const struct seq_operations nfs_volume_list_ops = {
18096aaca566SDavid Howells 	.start	= nfs_volume_list_start,
18106aaca566SDavid Howells 	.next	= nfs_volume_list_next,
18116aaca566SDavid Howells 	.stop	= nfs_volume_list_stop,
18126aaca566SDavid Howells 	.show	= nfs_volume_list_show,
18136aaca566SDavid Howells };
18146aaca566SDavid Howells 
181500977a59SArjan van de Ven static const struct file_operations nfs_volume_list_fops = {
18166aaca566SDavid Howells 	.open		= nfs_volume_list_open,
18176aaca566SDavid Howells 	.read		= seq_read,
18186aaca566SDavid Howells 	.llseek		= seq_lseek,
18196aaca566SDavid Howells 	.release	= seq_release,
182034b37235SDenis V. Lunev 	.owner		= THIS_MODULE,
18216aaca566SDavid Howells };
18226aaca566SDavid Howells 
18236aaca566SDavid Howells /*
18246aaca566SDavid Howells  * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
18256aaca566SDavid Howells  * we're dealing
18266aaca566SDavid Howells  */
18276aaca566SDavid Howells static int nfs_server_list_open(struct inode *inode, struct file *file)
18286aaca566SDavid Howells {
18296aaca566SDavid Howells 	struct seq_file *m;
18306aaca566SDavid Howells 	int ret;
18316b13168bSStanislav Kinsbursky 	struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
18326b13168bSStanislav Kinsbursky 	struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
18336aaca566SDavid Howells 
18346aaca566SDavid Howells 	ret = seq_open(file, &nfs_server_list_ops);
18356aaca566SDavid Howells 	if (ret < 0)
18366aaca566SDavid Howells 		return ret;
18376aaca566SDavid Howells 
18386aaca566SDavid Howells 	m = file->private_data;
18396b13168bSStanislav Kinsbursky 	m->private = net;
18406aaca566SDavid Howells 
18416aaca566SDavid Howells 	return 0;
18426aaca566SDavid Howells }
18436aaca566SDavid Howells 
18446aaca566SDavid Howells /*
18456aaca566SDavid Howells  * set up the iterator to start reading from the server list and return the first item
18466aaca566SDavid Howells  */
18476aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
18486aaca566SDavid Howells {
18496b13168bSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(m->private, nfs_net_id);
18506b13168bSStanislav Kinsbursky 
18516aaca566SDavid Howells 	/* lock the list against modification */
18526aaca566SDavid Howells 	spin_lock(&nfs_client_lock);
18536b13168bSStanislav Kinsbursky 	return seq_list_start_head(&nn->nfs_client_list, *_pos);
18546aaca566SDavid Howells }
18556aaca566SDavid Howells 
18566aaca566SDavid Howells /*
18576aaca566SDavid Howells  * move to next server
18586aaca566SDavid Howells  */
18596aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
18606aaca566SDavid Howells {
18616b13168bSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(p->private, nfs_net_id);
18626b13168bSStanislav Kinsbursky 
18636b13168bSStanislav Kinsbursky 	return seq_list_next(v, &nn->nfs_client_list, pos);
18646aaca566SDavid Howells }
18656aaca566SDavid Howells 
18666aaca566SDavid Howells /*
18676aaca566SDavid Howells  * clean up after reading from the transports list
18686aaca566SDavid Howells  */
18696aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v)
18706aaca566SDavid Howells {
18716aaca566SDavid Howells 	spin_unlock(&nfs_client_lock);
18726aaca566SDavid Howells }
18736aaca566SDavid Howells 
18746aaca566SDavid Howells /*
18756aaca566SDavid Howells  * display a header line followed by a load of call lines
18766aaca566SDavid Howells  */
18776aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v)
18786aaca566SDavid Howells {
18796aaca566SDavid Howells 	struct nfs_client *clp;
18806b13168bSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(m->private, nfs_net_id);
18816aaca566SDavid Howells 
18826aaca566SDavid Howells 	/* display header on line 1 */
18836b13168bSStanislav Kinsbursky 	if (v == &nn->nfs_client_list) {
18846aaca566SDavid Howells 		seq_puts(m, "NV SERVER   PORT USE HOSTNAME\n");
18856aaca566SDavid Howells 		return 0;
18866aaca566SDavid Howells 	}
18876aaca566SDavid Howells 
18886aaca566SDavid Howells 	/* display one transport per line on subsequent lines */
18896aaca566SDavid Howells 	clp = list_entry(v, struct nfs_client, cl_share_link);
18906aaca566SDavid Howells 
1891940aab49SMalahal Naineni 	/* Check if the client is initialized */
1892940aab49SMalahal Naineni 	if (clp->cl_cons_state != NFS_CS_READY)
1893940aab49SMalahal Naineni 		return 0;
1894940aab49SMalahal Naineni 
18955d8515caSChuck Lever 	seq_printf(m, "v%u %s %s %3d %s\n",
189640c55319STrond Myklebust 		   clp->rpc_ops->version,
18975d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
18985d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
18996aaca566SDavid Howells 		   atomic_read(&clp->cl_count),
19006aaca566SDavid Howells 		   clp->cl_hostname);
19016aaca566SDavid Howells 
19026aaca566SDavid Howells 	return 0;
19036aaca566SDavid Howells }
19046aaca566SDavid Howells 
19056aaca566SDavid Howells /*
19066aaca566SDavid Howells  * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
19076aaca566SDavid Howells  */
19086aaca566SDavid Howells static int nfs_volume_list_open(struct inode *inode, struct file *file)
19096aaca566SDavid Howells {
19106aaca566SDavid Howells 	struct seq_file *m;
19116aaca566SDavid Howells 	int ret;
1912c25d32b2SStanislav Kinsbursky 	struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1913c25d32b2SStanislav Kinsbursky 	struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
19146aaca566SDavid Howells 
19156aaca566SDavid Howells 	ret = seq_open(file, &nfs_volume_list_ops);
19166aaca566SDavid Howells 	if (ret < 0)
19176aaca566SDavid Howells 		return ret;
19186aaca566SDavid Howells 
19196aaca566SDavid Howells 	m = file->private_data;
1920c25d32b2SStanislav Kinsbursky 	m->private = net;
19216aaca566SDavid Howells 
19226aaca566SDavid Howells 	return 0;
19236aaca566SDavid Howells }
19246aaca566SDavid Howells 
19256aaca566SDavid Howells /*
19266aaca566SDavid Howells  * set up the iterator to start reading from the volume list and return the first item
19276aaca566SDavid Howells  */
19286aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
19296aaca566SDavid Howells {
1930c25d32b2SStanislav Kinsbursky 	struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1931c25d32b2SStanislav Kinsbursky 
19326aaca566SDavid Howells 	/* lock the list against modification */
19336aaca566SDavid Howells 	spin_lock(&nfs_client_lock);
1934c25d32b2SStanislav Kinsbursky 	return seq_list_start_head(&nn->nfs_volume_list, *_pos);
19356aaca566SDavid Howells }
19366aaca566SDavid Howells 
19376aaca566SDavid Howells /*
19386aaca566SDavid Howells  * move to next volume
19396aaca566SDavid Howells  */
19406aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
19416aaca566SDavid Howells {
1942c25d32b2SStanislav Kinsbursky 	struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1943c25d32b2SStanislav Kinsbursky 
1944c25d32b2SStanislav Kinsbursky 	return seq_list_next(v, &nn->nfs_volume_list, pos);
19456aaca566SDavid Howells }
19466aaca566SDavid Howells 
19476aaca566SDavid Howells /*
19486aaca566SDavid Howells  * clean up after reading from the transports list
19496aaca566SDavid Howells  */
19506aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v)
19516aaca566SDavid Howells {
19526aaca566SDavid Howells 	spin_unlock(&nfs_client_lock);
19536aaca566SDavid Howells }
19546aaca566SDavid Howells 
19556aaca566SDavid Howells /*
19566aaca566SDavid Howells  * display a header line followed by a load of call lines
19576aaca566SDavid Howells  */
19586aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v)
19596aaca566SDavid Howells {
19606aaca566SDavid Howells 	struct nfs_server *server;
19616aaca566SDavid Howells 	struct nfs_client *clp;
19626aaca566SDavid Howells 	char dev[8], fsid[17];
1963c25d32b2SStanislav Kinsbursky 	struct nfs_net *nn = net_generic(m->private, nfs_net_id);
19646aaca566SDavid Howells 
19656aaca566SDavid Howells 	/* display header on line 1 */
1966c25d32b2SStanislav Kinsbursky 	if (v == &nn->nfs_volume_list) {
19675d1acff1SDavid Howells 		seq_puts(m, "NV SERVER   PORT DEV     FSID              FSC\n");
19686aaca566SDavid Howells 		return 0;
19696aaca566SDavid Howells 	}
19706aaca566SDavid Howells 	/* display one transport per line on subsequent lines */
19716aaca566SDavid Howells 	server = list_entry(v, struct nfs_server, master_link);
19726aaca566SDavid Howells 	clp = server->nfs_client;
19736aaca566SDavid Howells 
19746aaca566SDavid Howells 	snprintf(dev, 8, "%u:%u",
19756aaca566SDavid Howells 		 MAJOR(server->s_dev), MINOR(server->s_dev));
19766aaca566SDavid Howells 
19776aaca566SDavid Howells 	snprintf(fsid, 17, "%llx:%llx",
19786daabf1bSDavid Howells 		 (unsigned long long) server->fsid.major,
19796daabf1bSDavid Howells 		 (unsigned long long) server->fsid.minor);
19806aaca566SDavid Howells 
19815d1acff1SDavid Howells 	seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
198240c55319STrond Myklebust 		   clp->rpc_ops->version,
19835d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
19845d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
19856aaca566SDavid Howells 		   dev,
19865d1acff1SDavid Howells 		   fsid,
19875d1acff1SDavid Howells 		   nfs_server_fscache_state(server));
19886aaca566SDavid Howells 
19896aaca566SDavid Howells 	return 0;
19906aaca566SDavid Howells }
19916aaca566SDavid Howells 
19926aaca566SDavid Howells /*
19936aaca566SDavid Howells  * initialise the /proc/fs/nfsfs/ directory
19946aaca566SDavid Howells  */
19956aaca566SDavid Howells int __init nfs_fs_proc_init(void)
19966aaca566SDavid Howells {
19976aaca566SDavid Howells 	struct proc_dir_entry *p;
19986aaca566SDavid Howells 
199936a5aeb8SAlexey Dobriyan 	proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
20006aaca566SDavid Howells 	if (!proc_fs_nfs)
20016aaca566SDavid Howells 		goto error_0;
20026aaca566SDavid Howells 
20036aaca566SDavid Howells 	/* a file of servers with which we're dealing */
200434b37235SDenis V. Lunev 	p = proc_create("servers", S_IFREG|S_IRUGO,
200534b37235SDenis V. Lunev 			proc_fs_nfs, &nfs_server_list_fops);
20066aaca566SDavid Howells 	if (!p)
20076aaca566SDavid Howells 		goto error_1;
20086aaca566SDavid Howells 
20096aaca566SDavid Howells 	/* a file of volumes that we have mounted */
201034b37235SDenis V. Lunev 	p = proc_create("volumes", S_IFREG|S_IRUGO,
201134b37235SDenis V. Lunev 			proc_fs_nfs, &nfs_volume_list_fops);
20126aaca566SDavid Howells 	if (!p)
20136aaca566SDavid Howells 		goto error_2;
20146aaca566SDavid Howells 	return 0;
20156aaca566SDavid Howells 
20166aaca566SDavid Howells error_2:
20176aaca566SDavid Howells 	remove_proc_entry("servers", proc_fs_nfs);
20186aaca566SDavid Howells error_1:
201936a5aeb8SAlexey Dobriyan 	remove_proc_entry("fs/nfsfs", NULL);
20206aaca566SDavid Howells error_0:
20216aaca566SDavid Howells 	return -ENOMEM;
20226aaca566SDavid Howells }
20236aaca566SDavid Howells 
20246aaca566SDavid Howells /*
20256aaca566SDavid Howells  * clean up the /proc/fs/nfsfs/ directory
20266aaca566SDavid Howells  */
20276aaca566SDavid Howells void nfs_fs_proc_exit(void)
20286aaca566SDavid Howells {
20296aaca566SDavid Howells 	remove_proc_entry("volumes", proc_fs_nfs);
20306aaca566SDavid Howells 	remove_proc_entry("servers", proc_fs_nfs);
203136a5aeb8SAlexey Dobriyan 	remove_proc_entry("fs/nfsfs", NULL);
20326aaca566SDavid Howells }
20336aaca566SDavid Howells 
20346aaca566SDavid Howells #endif /* CONFIG_PROC_FS */
2035b064eca2STrond Myklebust 
2036b064eca2STrond Myklebust module_param(nfs4_disable_idmapping, bool, 0644);
2037b064eca2STrond Myklebust MODULE_PARM_DESC(nfs4_disable_idmapping,
2038b064eca2STrond Myklebust 		"Turn off NFSv4 idmapping when using 'sec=sys'");
2039