xref: /linux/fs/nfs/client.c (revision 62164f317972fcd36590578888f33a1994dda519)
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>
23d8efa4e6SAnna Schumaker #include <linux/sunrpc/addr.h>
2424c8dbbbSDavid Howells #include <linux/sunrpc/clnt.h>
2524c8dbbbSDavid Howells #include <linux/sunrpc/stats.h>
2624c8dbbbSDavid Howells #include <linux/sunrpc/metrics.h>
270896a725S\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h>
282cf7ff7aS\"Talpey, Thomas\ #include <linux/sunrpc/xprtrdma.h>
2924c8dbbbSDavid Howells #include <linux/nfs_fs.h>
3024c8dbbbSDavid Howells #include <linux/nfs_mount.h>
3124c8dbbbSDavid Howells #include <linux/nfs4_mount.h>
3224c8dbbbSDavid Howells #include <linux/lockd/bind.h>
3324c8dbbbSDavid Howells #include <linux/seq_file.h>
3424c8dbbbSDavid Howells #include <linux/mount.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>
3940401530SAl Viro #include <linux/idr.h>
403b0d3f93STrond Myklebust #include <net/ipv6.h>
4124c8dbbbSDavid Howells #include <linux/nfs_xdr.h>
420b5b7ae0SAndy Adamson #include <linux/sunrpc/bc_xprt.h>
436b13168bSStanislav Kinsbursky #include <linux/nsproxy.h>
446b13168bSStanislav Kinsbursky #include <linux/pid_namespace.h>
4524c8dbbbSDavid Howells 
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"
54ab7017a3SBryan Schumaker #include "nfs.h"
556b13168bSStanislav Kinsbursky #include "netns.h"
5624c8dbbbSDavid Howells 
5724c8dbbbSDavid Howells #define NFSDBG_FACILITY		NFSDBG_CLIENT
5824c8dbbbSDavid Howells 
5924c8dbbbSDavid Howells static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
60ab7017a3SBryan Schumaker static DEFINE_SPINLOCK(nfs_version_lock);
61ab7017a3SBryan Schumaker static DEFINE_MUTEX(nfs_version_mutex);
62ab7017a3SBryan Schumaker static LIST_HEAD(nfs_versions);
6324c8dbbbSDavid Howells 
6424c8dbbbSDavid Howells /*
655006a76cSDavid Howells  * RPC cruft for NFS
665006a76cSDavid Howells  */
67a613fa16STrond Myklebust static const struct rpc_version *nfs_version[5] = {
68ab7017a3SBryan Schumaker 	[2] = NULL,
69ab7017a3SBryan Schumaker 	[3] = NULL,
70ab7017a3SBryan Schumaker 	[4] = NULL,
715006a76cSDavid Howells };
725006a76cSDavid Howells 
73a613fa16STrond Myklebust const struct rpc_program nfs_program = {
745006a76cSDavid Howells 	.name			= "nfs",
755006a76cSDavid Howells 	.number			= NFS_PROGRAM,
765006a76cSDavid Howells 	.nrvers			= ARRAY_SIZE(nfs_version),
775006a76cSDavid Howells 	.version		= nfs_version,
785006a76cSDavid Howells 	.stats			= &nfs_rpcstat,
79fe0a9b74SJim Rees 	.pipe_dir_name		= NFS_PIPE_DIRNAME,
805006a76cSDavid Howells };
815006a76cSDavid Howells 
825006a76cSDavid Howells struct rpc_stat nfs_rpcstat = {
835006a76cSDavid Howells 	.program		= &nfs_program
845006a76cSDavid Howells };
855006a76cSDavid Howells 
86ab7017a3SBryan Schumaker static struct nfs_subversion *find_nfs_version(unsigned int version)
87ab7017a3SBryan Schumaker {
88ab7017a3SBryan Schumaker 	struct nfs_subversion *nfs;
89ab7017a3SBryan Schumaker 	spin_lock(&nfs_version_lock);
90ab7017a3SBryan Schumaker 
91ab7017a3SBryan Schumaker 	list_for_each_entry(nfs, &nfs_versions, list) {
92ab7017a3SBryan Schumaker 		if (nfs->rpc_ops->version == version) {
93ab7017a3SBryan Schumaker 			spin_unlock(&nfs_version_lock);
94ab7017a3SBryan Schumaker 			return nfs;
95ab7017a3SBryan Schumaker 		}
96ee34e136SYanchuan Nian 	}
97ab7017a3SBryan Schumaker 
98ab7017a3SBryan Schumaker 	spin_unlock(&nfs_version_lock);
99ee34e136SYanchuan Nian 	return ERR_PTR(-EPROTONOSUPPORT);
100ab7017a3SBryan Schumaker }
101ab7017a3SBryan Schumaker 
102ab7017a3SBryan Schumaker struct nfs_subversion *get_nfs_version(unsigned int version)
103ab7017a3SBryan Schumaker {
104ab7017a3SBryan Schumaker 	struct nfs_subversion *nfs = find_nfs_version(version);
105ab7017a3SBryan Schumaker 
106ab7017a3SBryan Schumaker 	if (IS_ERR(nfs)) {
107ab7017a3SBryan Schumaker 		mutex_lock(&nfs_version_mutex);
1081ae811eeSbjschuma@gmail.com 		request_module("nfsv%d", version);
109ab7017a3SBryan Schumaker 		nfs = find_nfs_version(version);
110ab7017a3SBryan Schumaker 		mutex_unlock(&nfs_version_mutex);
111ab7017a3SBryan Schumaker 	}
112ab7017a3SBryan Schumaker 
1131f70ef96SAlexey Khoroshilov 	if (!IS_ERR(nfs) && !try_module_get(nfs->owner))
1141f70ef96SAlexey Khoroshilov 		return ERR_PTR(-EAGAIN);
115ab7017a3SBryan Schumaker 	return nfs;
116ab7017a3SBryan Schumaker }
117ab7017a3SBryan Schumaker 
118ab7017a3SBryan Schumaker void put_nfs_version(struct nfs_subversion *nfs)
119ab7017a3SBryan Schumaker {
120ab7017a3SBryan Schumaker 	module_put(nfs->owner);
121ab7017a3SBryan Schumaker }
122ab7017a3SBryan Schumaker 
123ab7017a3SBryan Schumaker void register_nfs_version(struct nfs_subversion *nfs)
124ab7017a3SBryan Schumaker {
125ab7017a3SBryan Schumaker 	spin_lock(&nfs_version_lock);
126ab7017a3SBryan Schumaker 
127ab7017a3SBryan Schumaker 	list_add(&nfs->list, &nfs_versions);
128ab7017a3SBryan Schumaker 	nfs_version[nfs->rpc_ops->version] = nfs->rpc_vers;
129ab7017a3SBryan Schumaker 
130ab7017a3SBryan Schumaker 	spin_unlock(&nfs_version_lock);
131ab7017a3SBryan Schumaker }
132ab7017a3SBryan Schumaker EXPORT_SYMBOL_GPL(register_nfs_version);
133ab7017a3SBryan Schumaker 
134ab7017a3SBryan Schumaker void unregister_nfs_version(struct nfs_subversion *nfs)
135ab7017a3SBryan Schumaker {
136ab7017a3SBryan Schumaker 	spin_lock(&nfs_version_lock);
137ab7017a3SBryan Schumaker 
138ab7017a3SBryan Schumaker 	nfs_version[nfs->rpc_ops->version] = NULL;
139ab7017a3SBryan Schumaker 	list_del(&nfs->list);
140ab7017a3SBryan Schumaker 
141ab7017a3SBryan Schumaker 	spin_unlock(&nfs_version_lock);
142ab7017a3SBryan Schumaker }
143ab7017a3SBryan Schumaker EXPORT_SYMBOL_GPL(unregister_nfs_version);
144ab7017a3SBryan Schumaker 
145ab7017a3SBryan Schumaker /*
14624c8dbbbSDavid Howells  * Allocate a shared client record
14724c8dbbbSDavid Howells  *
14824c8dbbbSDavid Howells  * Since these are allocated/deallocated very rarely, we don't
14924c8dbbbSDavid Howells  * bother putting them in a slab cache...
15024c8dbbbSDavid Howells  */
1516663ee7fSBryan Schumaker struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
15224c8dbbbSDavid Howells {
15324c8dbbbSDavid Howells 	struct nfs_client *clp;
1547c67db3aSTrond Myklebust 	struct rpc_cred *cred;
155a21bdd9bSChuck Lever 	int err = -ENOMEM;
15624c8dbbbSDavid Howells 
15724c8dbbbSDavid Howells 	if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
15824c8dbbbSDavid Howells 		goto error_0;
15924c8dbbbSDavid Howells 
160ab7017a3SBryan Schumaker 	clp->cl_nfs_mod = cl_init->nfs_mod;
1611f70ef96SAlexey Khoroshilov 	if (!try_module_get(clp->cl_nfs_mod->owner))
1621f70ef96SAlexey Khoroshilov 		goto error_dealloc;
163ab7017a3SBryan Schumaker 
164ab7017a3SBryan Schumaker 	clp->rpc_ops = clp->cl_nfs_mod->rpc_ops;
16540c55319STrond Myklebust 
166212bf41dSElena Reshetova 	refcount_set(&clp->cl_count, 1);
16724c8dbbbSDavid Howells 	clp->cl_cons_state = NFS_CS_INITING;
16824c8dbbbSDavid Howells 
1696e4cffd7SChuck Lever 	memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
1706e4cffd7SChuck Lever 	clp->cl_addrlen = cl_init->addrlen;
17124c8dbbbSDavid Howells 
1723a498026STrond Myklebust 	if (cl_init->hostname) {
173a21bdd9bSChuck Lever 		err = -ENOMEM;
1743a498026STrond Myklebust 		clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
17524c8dbbbSDavid Howells 		if (!clp->cl_hostname)
17671468513SBenny Halevy 			goto error_cleanup;
17724c8dbbbSDavid Howells 	}
17824c8dbbbSDavid Howells 
17924c8dbbbSDavid Howells 	INIT_LIST_HEAD(&clp->cl_superblocks);
18024c8dbbbSDavid Howells 	clp->cl_rpcclient = ERR_PTR(-EINVAL);
18124c8dbbbSDavid Howells 
18259dca3b2STrond Myklebust 	clp->cl_proto = cl_init->proto;
18373ea666cSChuck Lever 	clp->cl_net = get_net(cl_init->net);
18459dca3b2STrond Myklebust 
18568c97153STrond Myklebust 	cred = rpc_lookup_machine_cred("*");
1867c67db3aSTrond Myklebust 	if (!IS_ERR(cred))
1877c67db3aSTrond Myklebust 		clp->cl_machine_cred = cred;
18814727281SDavid Howells 	nfs_fscache_get_client_cookie(clp);
18914727281SDavid Howells 
19024c8dbbbSDavid Howells 	return clp;
19124c8dbbbSDavid Howells 
19271468513SBenny Halevy error_cleanup:
193ab7017a3SBryan Schumaker 	put_nfs_version(clp->cl_nfs_mod);
1941f70ef96SAlexey Khoroshilov error_dealloc:
19524c8dbbbSDavid Howells 	kfree(clp);
19624c8dbbbSDavid Howells error_0:
197a21bdd9bSChuck Lever 	return ERR_PTR(err);
19824c8dbbbSDavid Howells }
199ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_alloc_client);
20024c8dbbbSDavid Howells 
20189d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
20228cd1b3fSStanislav Kinsbursky void nfs_cleanup_cb_ident_idr(struct net *net)
203f4eecd5dSAndy Adamson {
20428cd1b3fSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(net, nfs_net_id);
20528cd1b3fSStanislav Kinsbursky 
20628cd1b3fSStanislav Kinsbursky 	idr_destroy(&nn->cb_ident_idr);
207f4eecd5dSAndy Adamson }
208f4eecd5dSAndy Adamson 
209f4eecd5dSAndy Adamson /* nfs_client_lock held */
210f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
211f4eecd5dSAndy Adamson {
21273ea666cSChuck Lever 	struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
21328cd1b3fSStanislav Kinsbursky 
214f4eecd5dSAndy Adamson 	if (clp->cl_cb_ident)
21528cd1b3fSStanislav Kinsbursky 		idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
216f4eecd5dSAndy Adamson }
217f4eecd5dSAndy Adamson 
218f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server)
219f7e8917aSFred Isaman {
220f7e8917aSFred Isaman 	rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
221f7e8917aSFred Isaman }
222f7e8917aSFred Isaman 
223888ef2e3SAlexandros Batsakis #else
22428cd1b3fSStanislav Kinsbursky void nfs_cleanup_cb_ident_idr(struct net *net)
225f4eecd5dSAndy Adamson {
226f4eecd5dSAndy Adamson }
227f4eecd5dSAndy Adamson 
228f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
229f4eecd5dSAndy Adamson {
230f4eecd5dSAndy Adamson }
231f7e8917aSFred Isaman 
232f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server)
233f7e8917aSFred Isaman {
234f7e8917aSFred Isaman }
235f7e8917aSFred Isaman 
236888ef2e3SAlexandros Batsakis #endif /* CONFIG_NFS_V4 */
237888ef2e3SAlexandros Batsakis 
238888ef2e3SAlexandros Batsakis /*
2395dd3177aSTrond Myklebust  * Destroy a shared client record
2405dd3177aSTrond Myklebust  */
241cdb7ecedSBryan Schumaker void nfs_free_client(struct nfs_client *clp)
2425dd3177aSTrond Myklebust {
24314727281SDavid Howells 	nfs_fscache_release_client_cookie(clp);
24414727281SDavid Howells 
24524c8dbbbSDavid Howells 	/* -EIO all pending I/O */
24624c8dbbbSDavid Howells 	if (!IS_ERR(clp->cl_rpcclient))
24724c8dbbbSDavid Howells 		rpc_shutdown_client(clp->cl_rpcclient);
24824c8dbbbSDavid Howells 
2497c67db3aSTrond Myklebust 	if (clp->cl_machine_cred != NULL)
2507c67db3aSTrond Myklebust 		put_rpccred(clp->cl_machine_cred);
2517c67db3aSTrond Myklebust 
25273ea666cSChuck Lever 	put_net(clp->cl_net);
253ab7017a3SBryan Schumaker 	put_nfs_version(clp->cl_nfs_mod);
25424c8dbbbSDavid Howells 	kfree(clp->cl_hostname);
255f11b2a1cSJeff Layton 	kfree(clp->cl_acceptor);
25624c8dbbbSDavid Howells 	kfree(clp);
25724c8dbbbSDavid Howells }
258ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_free_client);
25924c8dbbbSDavid Howells 
26024c8dbbbSDavid Howells /*
26124c8dbbbSDavid Howells  * Release a reference to a shared client record
26224c8dbbbSDavid Howells  */
26324c8dbbbSDavid Howells void nfs_put_client(struct nfs_client *clp)
26424c8dbbbSDavid Howells {
265dc030858SStanislav Kinsbursky 	struct nfs_net *nn;
266dc030858SStanislav Kinsbursky 
26727ba8512SDavid Howells 	if (!clp)
26827ba8512SDavid Howells 		return;
26927ba8512SDavid Howells 
27073ea666cSChuck Lever 	nn = net_generic(clp->cl_net, nfs_net_id);
27124c8dbbbSDavid Howells 
272212bf41dSElena Reshetova 	if (refcount_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
27324c8dbbbSDavid Howells 		list_del(&clp->cl_share_link);
274f4eecd5dSAndy Adamson 		nfs_cb_idr_remove_locked(clp);
275dc030858SStanislav Kinsbursky 		spin_unlock(&nn->nfs_client_lock);
27624c8dbbbSDavid Howells 
2771fea73a8STrond Myklebust 		WARN_ON_ONCE(!list_empty(&clp->cl_superblocks));
27824c8dbbbSDavid Howells 
279cdb7ecedSBryan Schumaker 		clp->rpc_ops->free_client(clp);
28024c8dbbbSDavid Howells 	}
28124c8dbbbSDavid Howells }
28216b374caSAndy Adamson EXPORT_SYMBOL_GPL(nfs_put_client);
28324c8dbbbSDavid Howells 
2843fbd67adSTrond Myklebust /*
285c81468a1STrond Myklebust  * Find an nfs_client on the list that matches the initialisation data
286c81468a1STrond Myklebust  * that is supplied.
287c81468a1STrond Myklebust  */
288c81468a1STrond Myklebust static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
28924c8dbbbSDavid Howells {
29024c8dbbbSDavid Howells 	struct nfs_client *clp;
291d7371c41SIan Dall 	const struct sockaddr *sap = data->addr;
2926b13168bSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(data->net, nfs_net_id);
29324c8dbbbSDavid Howells 
294c156618eSScott Mayhew again:
2956b13168bSStanislav Kinsbursky 	list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
296d7371c41SIan Dall 	        const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
29713bbc06aSTrond Myklebust 		/* Don't match clients that failed to initialise properly */
29813bbc06aSTrond Myklebust 		if (clp->cl_cons_state < 0)
29913bbc06aSTrond Myklebust 			continue;
30013bbc06aSTrond Myklebust 
301c156618eSScott Mayhew 		/* If a client is still initializing then we need to wait */
302c156618eSScott Mayhew 		if (clp->cl_cons_state > NFS_CS_READY) {
303c156618eSScott Mayhew 			refcount_inc(&clp->cl_count);
304c156618eSScott Mayhew 			spin_unlock(&nn->nfs_client_lock);
305c156618eSScott Mayhew 			nfs_wait_client_init_complete(clp);
306c156618eSScott Mayhew 			nfs_put_client(clp);
307c156618eSScott Mayhew 			spin_lock(&nn->nfs_client_lock);
308c156618eSScott Mayhew 			goto again;
309c156618eSScott Mayhew 		}
310c156618eSScott Mayhew 
31124c8dbbbSDavid Howells 		/* Different NFS versions cannot share the same nfs_client */
312ab7017a3SBryan Schumaker 		if (clp->rpc_ops != data->nfs_mod->rpc_ops)
31324c8dbbbSDavid Howells 			continue;
31424c8dbbbSDavid Howells 
31559dca3b2STrond Myklebust 		if (clp->cl_proto != data->proto)
31659dca3b2STrond Myklebust 			continue;
3175aae4a9aSBenny Halevy 		/* Match nfsv4 minorversion */
3185aae4a9aSBenny Halevy 		if (clp->cl_minorversion != data->minorversion)
3195aae4a9aSBenny Halevy 			continue;
320c81468a1STrond Myklebust 		/* Match the full socket address */
321d8efa4e6SAnna Schumaker 		if (!rpc_cmp_addr_port(sap, clap))
32204ea1b3eSAndy Adamson 			/* Match all xprt_switch full socket addresses */
3238ef32955SPetr Vandrovec 			if (IS_ERR(clp->cl_rpcclient) ||
3248ef32955SPetr Vandrovec                             !rpc_clnt_xprt_switch_has_addr(clp->cl_rpcclient,
32504ea1b3eSAndy Adamson 							   sap))
32624c8dbbbSDavid Howells 				continue;
32724c8dbbbSDavid Howells 
328212bf41dSElena Reshetova 		refcount_inc(&clp->cl_count);
32924c8dbbbSDavid Howells 		return clp;
33024c8dbbbSDavid Howells 	}
331c81468a1STrond Myklebust 	return NULL;
33224c8dbbbSDavid Howells }
33324c8dbbbSDavid Howells 
334a33e4b03SWeston Andros Adamson /*
335a33e4b03SWeston Andros Adamson  * Return true if @clp is done initializing, false if still working on it.
336a33e4b03SWeston Andros Adamson  *
337a33e4b03SWeston Andros Adamson  * Use nfs_client_init_status to check if it was successful.
338a33e4b03SWeston Andros Adamson  */
339a33e4b03SWeston Andros Adamson bool nfs_client_init_is_complete(const struct nfs_client *clp)
3404697bd5eSTrond Myklebust {
34148d66b97STrond Myklebust 	return clp->cl_cons_state <= NFS_CS_READY;
3424697bd5eSTrond Myklebust }
343a33e4b03SWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_client_init_is_complete);
344a33e4b03SWeston Andros Adamson 
345a33e4b03SWeston Andros Adamson /*
346a33e4b03SWeston Andros Adamson  * Return 0 if @clp was successfully initialized, -errno otherwise.
347a33e4b03SWeston Andros Adamson  *
348a33e4b03SWeston Andros Adamson  * This must be called *after* nfs_client_init_is_complete() returns true,
349a33e4b03SWeston Andros Adamson  * otherwise it will pop WARN_ON_ONCE and return -EINVAL
350a33e4b03SWeston Andros Adamson  */
351a33e4b03SWeston Andros Adamson int nfs_client_init_status(const struct nfs_client *clp)
352a33e4b03SWeston Andros Adamson {
353a33e4b03SWeston Andros Adamson 	/* called without checking nfs_client_init_is_complete */
354a33e4b03SWeston Andros Adamson 	if (clp->cl_cons_state > NFS_CS_READY) {
355a33e4b03SWeston Andros Adamson 		WARN_ON_ONCE(1);
356a33e4b03SWeston Andros Adamson 		return -EINVAL;
357a33e4b03SWeston Andros Adamson 	}
358a33e4b03SWeston Andros Adamson 	return clp->cl_cons_state;
359a33e4b03SWeston Andros Adamson }
360a33e4b03SWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_client_init_status);
3614697bd5eSTrond Myklebust 
3624697bd5eSTrond Myklebust int nfs_wait_client_init_complete(const struct nfs_client *clp)
3634697bd5eSTrond Myklebust {
3644697bd5eSTrond Myklebust 	return wait_event_killable(nfs_client_active_wq,
3654697bd5eSTrond Myklebust 			nfs_client_init_is_complete(clp));
3664697bd5eSTrond Myklebust }
36789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wait_client_init_complete);
3684697bd5eSTrond Myklebust 
36924c8dbbbSDavid Howells /*
370f411703aSChuck Lever  * Found an existing client.  Make sure it's ready before returning.
371f411703aSChuck Lever  */
372f411703aSChuck Lever static struct nfs_client *
373f411703aSChuck Lever nfs_found_client(const struct nfs_client_initdata *cl_init,
374f411703aSChuck Lever 		 struct nfs_client *clp)
375f411703aSChuck Lever {
376f411703aSChuck Lever 	int error;
377f411703aSChuck Lever 
3784697bd5eSTrond Myklebust 	error = nfs_wait_client_init_complete(clp);
379f411703aSChuck Lever 	if (error < 0) {
380f411703aSChuck Lever 		nfs_put_client(clp);
381f411703aSChuck Lever 		return ERR_PTR(-ERESTARTSYS);
382f411703aSChuck Lever 	}
383f411703aSChuck Lever 
384f411703aSChuck Lever 	if (clp->cl_cons_state < NFS_CS_READY) {
385f411703aSChuck Lever 		error = clp->cl_cons_state;
386f411703aSChuck Lever 		nfs_put_client(clp);
387f411703aSChuck Lever 		return ERR_PTR(error);
388f411703aSChuck Lever 	}
389f411703aSChuck Lever 
39054ac471cSTrond Myklebust 	smp_rmb();
391f411703aSChuck Lever 	return clp;
392f411703aSChuck Lever }
393f411703aSChuck Lever 
394f411703aSChuck Lever /*
39524c8dbbbSDavid Howells  * Look up a client by IP address and protocol version
39624c8dbbbSDavid Howells  * - creates a new record if one doesn't yet exist
39724c8dbbbSDavid Howells  */
3987d38de3fSAnna Schumaker struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
39924c8dbbbSDavid Howells {
40024c8dbbbSDavid Howells 	struct nfs_client *clp, *new = NULL;
4016b13168bSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
402ab7017a3SBryan Schumaker 	const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops;
40324c8dbbbSDavid Howells 
40431434f49SPeng Tao 	if (cl_init->hostname == NULL) {
40531434f49SPeng Tao 		WARN_ON(1);
40631434f49SPeng Tao 		return NULL;
40731434f49SPeng Tao 	}
40831434f49SPeng Tao 
40924c8dbbbSDavid Howells 	/* see if the client already exists */
41024c8dbbbSDavid Howells 	do {
411dc030858SStanislav Kinsbursky 		spin_lock(&nn->nfs_client_lock);
41224c8dbbbSDavid Howells 
413c81468a1STrond Myklebust 		clp = nfs_match_client(cl_init);
414f411703aSChuck Lever 		if (clp) {
415f411703aSChuck Lever 			spin_unlock(&nn->nfs_client_lock);
416f411703aSChuck Lever 			if (new)
417cdb7ecedSBryan Schumaker 				new->rpc_ops->free_client(new);
418f411703aSChuck Lever 			return nfs_found_client(cl_init, clp);
419f411703aSChuck Lever 		}
4208cab4c39SChuck Lever 		if (new) {
42105f4c350SChuck Lever 			list_add_tail(&new->cl_share_link,
42205f4c350SChuck Lever 					&nn->nfs_client_list);
4238cab4c39SChuck Lever 			spin_unlock(&nn->nfs_client_lock);
4244bf590e0SChuck Lever 			new->cl_flags = cl_init->init_flags;
4255c6e5b60STrond Myklebust 			return rpc_ops->init_client(new, cl_init);
4268cab4c39SChuck Lever 		}
42724c8dbbbSDavid Howells 
428dc030858SStanislav Kinsbursky 		spin_unlock(&nn->nfs_client_lock);
42924c8dbbbSDavid Howells 
430ab7017a3SBryan Schumaker 		new = rpc_ops->alloc_client(cl_init);
431a21bdd9bSChuck Lever 	} while (!IS_ERR(new));
43224c8dbbbSDavid Howells 
433a21bdd9bSChuck Lever 	return new;
43424c8dbbbSDavid Howells }
43589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_get_client);
43624c8dbbbSDavid Howells 
43724c8dbbbSDavid Howells /*
43824c8dbbbSDavid Howells  * Mark a server as ready or failed
43924c8dbbbSDavid Howells  */
44076db6d95SAndy Adamson void nfs_mark_client_ready(struct nfs_client *clp, int state)
44124c8dbbbSDavid Howells {
44254ac471cSTrond Myklebust 	smp_wmb();
44324c8dbbbSDavid Howells 	clp->cl_cons_state = state;
44424c8dbbbSDavid Howells 	wake_up_all(&nfs_client_active_wq);
44524c8dbbbSDavid Howells }
44689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mark_client_ready);
4475006a76cSDavid Howells 
4485006a76cSDavid Howells /*
4495006a76cSDavid Howells  * Initialise the timeout values for a connection
4505006a76cSDavid Howells  */
451fcf10398SBryan Schumaker void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
452a956bedaSTrond Myklebust 				    int timeo, int retrans)
4535006a76cSDavid Howells {
4545006a76cSDavid Howells 	to->to_initval = timeo * HZ / 10;
4555006a76cSDavid Howells 	to->to_retries = retrans;
4565006a76cSDavid Howells 
4575006a76cSDavid Howells 	switch (proto) {
4580896a725S\"Talpey, Thomas\ 	case XPRT_TRANSPORT_TCP:
4592cf7ff7aS\"Talpey, Thomas\ 	case XPRT_TRANSPORT_RDMA:
460a956bedaSTrond Myklebust 		if (retrans == NFS_UNSPEC_RETRANS)
461259875efSTrond Myklebust 			to->to_retries = NFS_DEF_TCP_RETRANS;
462a956bedaSTrond Myklebust 		if (timeo == NFS_UNSPEC_TIMEO || to->to_retries == 0)
463259875efSTrond Myklebust 			to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
4645006a76cSDavid Howells 		if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
4655006a76cSDavid Howells 			to->to_initval = NFS_MAX_TCP_TIMEOUT;
4665006a76cSDavid Howells 		to->to_increment = to->to_initval;
4675006a76cSDavid Howells 		to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
4687a3e3e18STrond Myklebust 		if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
4697a3e3e18STrond Myklebust 			to->to_maxval = NFS_MAX_TCP_TIMEOUT;
4707a3e3e18STrond Myklebust 		if (to->to_maxval < to->to_initval)
4717a3e3e18STrond Myklebust 			to->to_maxval = to->to_initval;
4725006a76cSDavid Howells 		to->to_exponential = 0;
4735006a76cSDavid Howells 		break;
4740896a725S\"Talpey, Thomas\ 	case XPRT_TRANSPORT_UDP:
475a956bedaSTrond Myklebust 		if (retrans == NFS_UNSPEC_RETRANS)
476259875efSTrond Myklebust 			to->to_retries = NFS_DEF_UDP_RETRANS;
477a956bedaSTrond Myklebust 		if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0)
478259875efSTrond Myklebust 			to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
4795006a76cSDavid Howells 		if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
4805006a76cSDavid Howells 			to->to_initval = NFS_MAX_UDP_TIMEOUT;
4815006a76cSDavid Howells 		to->to_maxval = NFS_MAX_UDP_TIMEOUT;
4825006a76cSDavid Howells 		to->to_exponential = 1;
4835006a76cSDavid Howells 		break;
484259875efSTrond Myklebust 	default:
485259875efSTrond Myklebust 		BUG();
4865006a76cSDavid Howells 	}
4875006a76cSDavid Howells }
48889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_timeout_values);
4895006a76cSDavid Howells 
4905006a76cSDavid Howells /*
4915006a76cSDavid Howells  * Create an RPC client handle
4925006a76cSDavid Howells  */
493428360d7SBryan Schumaker int nfs_create_rpc_client(struct nfs_client *clp,
4945c6e5b60STrond Myklebust 			  const struct nfs_client_initdata *cl_init,
4954bf590e0SChuck Lever 			  rpc_authflavor_t flavor)
4965006a76cSDavid Howells {
4975006a76cSDavid Howells 	struct rpc_clnt		*clnt = NULL;
49841877d20SChuck Lever 	struct rpc_create_args args = {
49973ea666cSChuck Lever 		.net		= clp->cl_net,
50059dca3b2STrond Myklebust 		.protocol	= clp->cl_proto,
50141877d20SChuck Lever 		.address	= (struct sockaddr *)&clp->cl_addr,
5026e4cffd7SChuck Lever 		.addrsize	= clp->cl_addrlen,
5035c6e5b60STrond Myklebust 		.timeout	= cl_init->timeparms,
50441877d20SChuck Lever 		.servername	= clp->cl_hostname,
5055c6e5b60STrond Myklebust 		.nodename	= cl_init->nodename,
50641877d20SChuck Lever 		.program	= &nfs_program,
50741877d20SChuck Lever 		.version	= clp->rpc_ops->version,
50841877d20SChuck Lever 		.authflavor	= flavor,
50941877d20SChuck Lever 	};
5105006a76cSDavid Howells 
5114bf590e0SChuck Lever 	if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags))
5124a01b8a4SChuck Lever 		args.flags |= RPC_CLNT_CREATE_DISCRTRY;
51399875249STrond Myklebust 	if (test_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags))
51499875249STrond Myklebust 		args.flags |= RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT;
5154bf590e0SChuck Lever 	if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags))
5164a01b8a4SChuck Lever 		args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
51798f98cf5STrond Myklebust 	if (test_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags))
51898f98cf5STrond Myklebust 		args.flags |= RPC_CLNT_CREATE_INFINITE_SLOTS;
5194a01b8a4SChuck Lever 
5205006a76cSDavid Howells 	if (!IS_ERR(clp->cl_rpcclient))
5215006a76cSDavid Howells 		return 0;
5225006a76cSDavid Howells 
52341877d20SChuck Lever 	clnt = rpc_create(&args);
5245006a76cSDavid Howells 	if (IS_ERR(clnt)) {
5255006a76cSDavid Howells 		dprintk("%s: cannot create RPC client. Error = %ld\n",
5263110ff80SHarvey Harrison 				__func__, PTR_ERR(clnt));
5275006a76cSDavid Howells 		return PTR_ERR(clnt);
5285006a76cSDavid Howells 	}
5295006a76cSDavid Howells 
5305006a76cSDavid Howells 	clp->cl_rpcclient = clnt;
5315006a76cSDavid Howells 	return 0;
5325006a76cSDavid Howells }
53389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create_rpc_client);
53454ceac45SDavid Howells 
53554ceac45SDavid Howells /*
53654ceac45SDavid Howells  * Version 2 or 3 client destruction
53754ceac45SDavid Howells  */
53854ceac45SDavid Howells static void nfs_destroy_server(struct nfs_server *server)
53954ceac45SDavid Howells {
540f259613aSNeilBrown 	if (server->nlm_host)
5419289e7f9SChuck Lever 		nlmclnt_done(server->nlm_host);
54254ceac45SDavid Howells }
54354ceac45SDavid Howells 
54454ceac45SDavid Howells /*
54554ceac45SDavid Howells  * Version 2 or 3 lockd setup
54654ceac45SDavid Howells  */
54754ceac45SDavid Howells static int nfs_start_lockd(struct nfs_server *server)
54854ceac45SDavid Howells {
5499289e7f9SChuck Lever 	struct nlm_host *host;
5509289e7f9SChuck Lever 	struct nfs_client *clp = server->nfs_client;
551883bb163SChuck Lever 	struct nlmclnt_initdata nlm_init = {
552883bb163SChuck Lever 		.hostname	= clp->cl_hostname,
553883bb163SChuck Lever 		.address	= (struct sockaddr *)&clp->cl_addr,
554883bb163SChuck Lever 		.addrlen	= clp->cl_addrlen,
555883bb163SChuck Lever 		.nfs_version	= clp->rpc_ops->version,
5560cb2659bSChuck Lever 		.noresvport	= server->flags & NFS_MOUNT_NORESVPORT ?
5570cb2659bSChuck Lever 					1 : 0,
55873ea666cSChuck Lever 		.net		= clp->cl_net,
559b1ece737SBenjamin Coddington 		.nlmclnt_ops 	= clp->cl_nfs_mod->rpc_ops->nlmclnt_ops,
560883bb163SChuck Lever 	};
56154ceac45SDavid Howells 
562883bb163SChuck Lever 	if (nlm_init.nfs_version > 3)
5639289e7f9SChuck Lever 		return 0;
5645eebde23SSuresh Jayaraman 	if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
5655eebde23SSuresh Jayaraman 			(server->flags & NFS_MOUNT_LOCAL_FCNTL))
5669289e7f9SChuck Lever 		return 0;
5679289e7f9SChuck Lever 
5688a6e5debSTrond Myklebust 	switch (clp->cl_proto) {
5698a6e5debSTrond Myklebust 		default:
5708a6e5debSTrond Myklebust 			nlm_init.protocol = IPPROTO_TCP;
5718a6e5debSTrond Myklebust 			break;
5728a6e5debSTrond Myklebust 		case XPRT_TRANSPORT_UDP:
5738a6e5debSTrond Myklebust 			nlm_init.protocol = IPPROTO_UDP;
5748a6e5debSTrond Myklebust 	}
5758a6e5debSTrond Myklebust 
576883bb163SChuck Lever 	host = nlmclnt_init(&nlm_init);
5779289e7f9SChuck Lever 	if (IS_ERR(host))
5789289e7f9SChuck Lever 		return PTR_ERR(host);
5799289e7f9SChuck Lever 
5809289e7f9SChuck Lever 	server->nlm_host = host;
58154ceac45SDavid Howells 	server->destroy = nfs_destroy_server;
5829289e7f9SChuck Lever 	return 0;
58354ceac45SDavid Howells }
58454ceac45SDavid Howells 
58554ceac45SDavid Howells /*
58654ceac45SDavid Howells  * Create a general RPC client
58754ceac45SDavid Howells  */
588fcf10398SBryan Schumaker int nfs_init_server_rpcclient(struct nfs_server *server,
58933170233STrond Myklebust 		const struct rpc_timeout *timeo,
59033170233STrond Myklebust 		rpc_authflavor_t pseudoflavour)
59154ceac45SDavid Howells {
59254ceac45SDavid Howells 	struct nfs_client *clp = server->nfs_client;
59354ceac45SDavid Howells 
594ba9b584cSChuck Lever 	server->client = rpc_clone_client_set_auth(clp->cl_rpcclient,
595ba9b584cSChuck Lever 							pseudoflavour);
59654ceac45SDavid Howells 	if (IS_ERR(server->client)) {
5973110ff80SHarvey Harrison 		dprintk("%s: couldn't create rpc_client!\n", __func__);
59854ceac45SDavid Howells 		return PTR_ERR(server->client);
59954ceac45SDavid Howells 	}
60054ceac45SDavid Howells 
60133170233STrond Myklebust 	memcpy(&server->client->cl_timeout_default,
60233170233STrond Myklebust 			timeo,
60333170233STrond Myklebust 			sizeof(server->client->cl_timeout_default));
60433170233STrond Myklebust 	server->client->cl_timeout = &server->client->cl_timeout_default;
60554ceac45SDavid Howells 	server->client->cl_softrtry = 0;
60654ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_SOFT)
60754ceac45SDavid Howells 		server->client->cl_softrtry = 1;
60854ceac45SDavid Howells 
60954ceac45SDavid Howells 	return 0;
61054ceac45SDavid Howells }
61189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_server_rpcclient);
61254ceac45SDavid Howells 
6138cab4c39SChuck Lever /**
6148cab4c39SChuck Lever  * nfs_init_client - Initialise an NFS2 or NFS3 client
6158cab4c39SChuck Lever  *
6168cab4c39SChuck Lever  * @clp: nfs_client to initialise
6175c6e5b60STrond Myklebust  * @cl_init: Initialisation parameters
6188cab4c39SChuck Lever  *
6198cab4c39SChuck Lever  * Returns pointer to an NFS client, or an ERR_PTR value.
62054ceac45SDavid Howells  */
6218cab4c39SChuck Lever struct nfs_client *nfs_init_client(struct nfs_client *clp,
6225c6e5b60STrond Myklebust 				   const struct nfs_client_initdata *cl_init)
62354ceac45SDavid Howells {
62454ceac45SDavid Howells 	int error;
62554ceac45SDavid Howells 
62654ceac45SDavid Howells 	/* the client is already initialised */
6272844b6aeSAnna Schumaker 	if (clp->cl_cons_state == NFS_CS_READY)
6288cab4c39SChuck Lever 		return clp;
62954ceac45SDavid Howells 
63054ceac45SDavid Howells 	/*
63154ceac45SDavid Howells 	 * Create a client RPC handle for doing FSSTAT with UNIX auth only
63254ceac45SDavid Howells 	 * - RFC 2623, sec 2.3.2
63354ceac45SDavid Howells 	 */
6345c6e5b60STrond Myklebust 	error = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_UNIX);
6352844b6aeSAnna Schumaker 	nfs_mark_client_ready(clp, error == 0 ? NFS_CS_READY : error);
6362844b6aeSAnna Schumaker 	if (error < 0) {
6378cab4c39SChuck Lever 		nfs_put_client(clp);
6382844b6aeSAnna Schumaker 		clp = ERR_PTR(error);
6392844b6aeSAnna Schumaker 	}
6402844b6aeSAnna Schumaker 	return clp;
64154ceac45SDavid Howells }
642ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_client);
64354ceac45SDavid Howells 
64454ceac45SDavid Howells /*
64554ceac45SDavid Howells  * Create a version 2 or 3 client
64654ceac45SDavid Howells  */
6472283f8d6S\"Talpey, Thomas\ static int nfs_init_server(struct nfs_server *server,
648ab7017a3SBryan Schumaker 			   const struct nfs_parsed_mount_data *data,
649ab7017a3SBryan Schumaker 			   struct nfs_subversion *nfs_mod)
65054ceac45SDavid Howells {
6515c6e5b60STrond Myklebust 	struct rpc_timeout timeparms;
6523a498026STrond Myklebust 	struct nfs_client_initdata cl_init = {
6533a498026STrond Myklebust 		.hostname = data->nfs_server.hostname,
654d7422c47SChuck Lever 		.addr = (const struct sockaddr *)&data->nfs_server.address,
6554c568017SChuck Lever 		.addrlen = data->nfs_server.addrlen,
656ab7017a3SBryan Schumaker 		.nfs_mod = nfs_mod,
65759dca3b2STrond Myklebust 		.proto = data->nfs_server.protocol,
658e50a7a1aSStanislav Kinsbursky 		.net = data->net,
6595c6e5b60STrond Myklebust 		.timeparms = &timeparms,
6603a498026STrond Myklebust 	};
66154ceac45SDavid Howells 	struct nfs_client *clp;
6623a498026STrond Myklebust 	int error;
66354ceac45SDavid Howells 
66445a52a02SAndy Adamson 	nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
66545a52a02SAndy Adamson 			data->timeo, data->retrans);
6664bf590e0SChuck Lever 	if (data->flags & NFS_MOUNT_NORESVPORT)
6674bf590e0SChuck Lever 		set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
66845a52a02SAndy Adamson 
66954ceac45SDavid Howells 	/* Allocate or find a client reference we can use */
6707d38de3fSAnna Schumaker 	clp = nfs_get_client(&cl_init);
6714cbb9768SAnna Schumaker 	if (IS_ERR(clp))
67254ceac45SDavid Howells 		return PTR_ERR(clp);
67354ceac45SDavid Howells 
67454ceac45SDavid Howells 	server->nfs_client = clp;
67554ceac45SDavid Howells 
67654ceac45SDavid Howells 	/* Initialise the client representation from the mount data */
677ff3525a5STrond Myklebust 	server->flags = data->flags;
678b797cac7SDavid Howells 	server->options = data->options;
67962ab460cSTrond Myklebust 	server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
68062ab460cSTrond Myklebust 		NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
681cd812599STrond Myklebust 		NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
68254ceac45SDavid Howells 
68354ceac45SDavid Howells 	if (data->rsize)
68454ceac45SDavid Howells 		server->rsize = nfs_block_size(data->rsize, NULL);
68554ceac45SDavid Howells 	if (data->wsize)
68654ceac45SDavid Howells 		server->wsize = nfs_block_size(data->wsize, NULL);
68754ceac45SDavid Howells 
68854ceac45SDavid Howells 	server->acregmin = data->acregmin * HZ;
68954ceac45SDavid Howells 	server->acregmax = data->acregmax * HZ;
69054ceac45SDavid Howells 	server->acdirmin = data->acdirmin * HZ;
69154ceac45SDavid Howells 	server->acdirmax = data->acdirmax * HZ;
69254ceac45SDavid Howells 
69354ceac45SDavid Howells 	/* Start lockd here, before we might error out */
69454ceac45SDavid Howells 	error = nfs_start_lockd(server);
69554ceac45SDavid Howells 	if (error < 0)
69654ceac45SDavid Howells 		goto error;
69754ceac45SDavid Howells 
698f22d6d79SChuck Lever 	server->port = data->nfs_server.port;
6990f5f49b8SWeston Andros Adamson 	server->auth_info = data->auth_info;
700f22d6d79SChuck Lever 
701a3f73c27SWeston Andros Adamson 	error = nfs_init_server_rpcclient(server, &timeparms,
702a3f73c27SWeston Andros Adamson 					  data->selected_flavor);
70354ceac45SDavid Howells 	if (error < 0)
70454ceac45SDavid Howells 		goto error;
70554ceac45SDavid Howells 
7063f8400d1SChuck Lever 	/* Preserve the values of mount_server-related mount options */
7073f8400d1SChuck Lever 	if (data->mount_server.addrlen) {
7083f8400d1SChuck Lever 		memcpy(&server->mountd_address, &data->mount_server.address,
7093f8400d1SChuck Lever 			data->mount_server.addrlen);
7103f8400d1SChuck Lever 		server->mountd_addrlen = data->mount_server.addrlen;
7113f8400d1SChuck Lever 	}
7123f8400d1SChuck Lever 	server->mountd_version = data->mount_server.version;
7133f8400d1SChuck Lever 	server->mountd_port = data->mount_server.port;
7143f8400d1SChuck Lever 	server->mountd_protocol = data->mount_server.protocol;
7153f8400d1SChuck Lever 
71654ceac45SDavid Howells 	server->namelen  = data->namlen;
71754ceac45SDavid Howells 	return 0;
71854ceac45SDavid Howells 
71954ceac45SDavid Howells error:
72054ceac45SDavid Howells 	server->nfs_client = NULL;
72154ceac45SDavid Howells 	nfs_put_client(clp);
72254ceac45SDavid Howells 	return error;
72354ceac45SDavid Howells }
72454ceac45SDavid Howells 
72554ceac45SDavid Howells /*
72654ceac45SDavid Howells  * Load up the server record from information gained in an fsinfo record
72754ceac45SDavid Howells  */
728738fd0f3SBenny Halevy static void nfs_server_set_fsinfo(struct nfs_server *server,
729738fd0f3SBenny Halevy 				  struct nfs_fsinfo *fsinfo)
73054ceac45SDavid Howells {
73154ceac45SDavid Howells 	unsigned long max_rpc_payload;
73254ceac45SDavid Howells 
73354ceac45SDavid Howells 	/* Work out a lot of parameters */
73454ceac45SDavid Howells 	if (server->rsize == 0)
73554ceac45SDavid Howells 		server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
73654ceac45SDavid Howells 	if (server->wsize == 0)
73754ceac45SDavid Howells 		server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
73854ceac45SDavid Howells 
73954ceac45SDavid Howells 	if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
74054ceac45SDavid Howells 		server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
74154ceac45SDavid Howells 	if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
74254ceac45SDavid Howells 		server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
74354ceac45SDavid Howells 
74454ceac45SDavid Howells 	max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
74554ceac45SDavid Howells 	if (server->rsize > max_rpc_payload)
74654ceac45SDavid Howells 		server->rsize = max_rpc_payload;
74754ceac45SDavid Howells 	if (server->rsize > NFS_MAX_FILE_IO_SIZE)
74854ceac45SDavid Howells 		server->rsize = NFS_MAX_FILE_IO_SIZE;
74909cbfeafSKirill A. Shutemov 	server->rpages = (server->rsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
750e0bf68ddSPeter Zijlstra 
75154ceac45SDavid Howells 	if (server->wsize > max_rpc_payload)
75254ceac45SDavid Howells 		server->wsize = max_rpc_payload;
75354ceac45SDavid Howells 	if (server->wsize > NFS_MAX_FILE_IO_SIZE)
75454ceac45SDavid Howells 		server->wsize = NFS_MAX_FILE_IO_SIZE;
75509cbfeafSKirill A. Shutemov 	server->wpages = (server->wsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
75685e174baSRicardo Labiaga 
75754ceac45SDavid Howells 	server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
75854ceac45SDavid Howells 
75954ceac45SDavid Howells 	server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
76009cbfeafSKirill A. Shutemov 	if (server->dtsize > PAGE_SIZE * NFS_MAX_READDIR_PAGES)
76109cbfeafSKirill A. Shutemov 		server->dtsize = PAGE_SIZE * NFS_MAX_READDIR_PAGES;
76254ceac45SDavid Howells 	if (server->dtsize > server->rsize)
76354ceac45SDavid Howells 		server->dtsize = server->rsize;
76454ceac45SDavid Howells 
76554ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_NOAC) {
76654ceac45SDavid Howells 		server->acregmin = server->acregmax = 0;
76754ceac45SDavid Howells 		server->acdirmin = server->acdirmax = 0;
76854ceac45SDavid Howells 	}
76954ceac45SDavid Howells 
77054ceac45SDavid Howells 	server->maxfilesize = fsinfo->maxfilesize;
77154ceac45SDavid Howells 
7726b96724eSRicardo Labiaga 	server->time_delta = fsinfo->time_delta;
7736b96724eSRicardo Labiaga 
7742a92ee92SPeng Tao 	server->clone_blksize = fsinfo->clone_blksize;
77554ceac45SDavid Howells 	/* We're airborne Set socket buffersize */
77654ceac45SDavid Howells 	rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
77754ceac45SDavid Howells }
77854ceac45SDavid Howells 
77954ceac45SDavid Howells /*
78054ceac45SDavid Howells  * Probe filesystem information, including the FSID on v2/v3
78154ceac45SDavid Howells  */
782fcf10398SBryan Schumaker int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
78354ceac45SDavid Howells {
78454ceac45SDavid Howells 	struct nfs_fsinfo fsinfo;
78554ceac45SDavid Howells 	struct nfs_client *clp = server->nfs_client;
78654ceac45SDavid Howells 	int error;
78754ceac45SDavid Howells 
78854ceac45SDavid Howells 	if (clp->rpc_ops->set_capabilities != NULL) {
78954ceac45SDavid Howells 		error = clp->rpc_ops->set_capabilities(server, mntfh);
79054ceac45SDavid Howells 		if (error < 0)
7914cbb9768SAnna Schumaker 			return error;
79254ceac45SDavid Howells 	}
79354ceac45SDavid Howells 
79454ceac45SDavid Howells 	fsinfo.fattr = fattr;
795ca440c38SJeff Layton 	fsinfo.nlayouttypes = 0;
7963132e49eSJeff Layton 	memset(fsinfo.layouttype, 0, sizeof(fsinfo.layouttype));
79754ceac45SDavid Howells 	error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
79854ceac45SDavid Howells 	if (error < 0)
7994cbb9768SAnna Schumaker 		return error;
80054ceac45SDavid Howells 
80171f81e51SKinglong Mee 	nfs_server_set_fsinfo(server, &fsinfo);
80254ceac45SDavid Howells 
80354ceac45SDavid Howells 	/* Get some general file system info */
80454ceac45SDavid Howells 	if (server->namelen == 0) {
80554ceac45SDavid Howells 		struct nfs_pathconf pathinfo;
80654ceac45SDavid Howells 
80754ceac45SDavid Howells 		pathinfo.fattr = fattr;
80854ceac45SDavid Howells 		nfs_fattr_init(fattr);
80954ceac45SDavid Howells 
81054ceac45SDavid Howells 		if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
81154ceac45SDavid Howells 			server->namelen = pathinfo.max_namelen;
81254ceac45SDavid Howells 	}
81354ceac45SDavid Howells 
81454ceac45SDavid Howells 	return 0;
81554ceac45SDavid Howells }
81689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_probe_fsinfo);
81754ceac45SDavid Howells 
81854ceac45SDavid Howells /*
81954ceac45SDavid Howells  * Copy useful information when duplicating a server record
82054ceac45SDavid Howells  */
821fcf10398SBryan Schumaker void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
82254ceac45SDavid Howells {
82354ceac45SDavid Howells 	target->flags = source->flags;
824356e76b8SChuck Lever 	target->rsize = source->rsize;
825356e76b8SChuck Lever 	target->wsize = source->wsize;
82654ceac45SDavid Howells 	target->acregmin = source->acregmin;
82754ceac45SDavid Howells 	target->acregmax = source->acregmax;
82854ceac45SDavid Howells 	target->acdirmin = source->acdirmin;
82954ceac45SDavid Howells 	target->acdirmax = source->acdirmax;
83054ceac45SDavid Howells 	target->caps = source->caps;
8312df54806SDavid Howells 	target->options = source->options;
8320f5f49b8SWeston Andros Adamson 	target->auth_info = source->auth_info;
83389a6814dSSteve Dickson 	target->port = source->port;
83454ceac45SDavid Howells }
83589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_server_copy_userdata);
83654ceac45SDavid Howells 
837fcf10398SBryan Schumaker void nfs_server_insert_lists(struct nfs_server *server)
838fca5238eSChuck Lever {
839fca5238eSChuck Lever 	struct nfs_client *clp = server->nfs_client;
84073ea666cSChuck Lever 	struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
841fca5238eSChuck Lever 
842dc030858SStanislav Kinsbursky 	spin_lock(&nn->nfs_client_lock);
843fca5238eSChuck Lever 	list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
844c25d32b2SStanislav Kinsbursky 	list_add_tail(&server->master_link, &nn->nfs_volume_list);
845d3b4c9d7SAndy Adamson 	clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
846dc030858SStanislav Kinsbursky 	spin_unlock(&nn->nfs_client_lock);
847fca5238eSChuck Lever 
848fca5238eSChuck Lever }
84989d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_server_insert_lists);
850fca5238eSChuck Lever 
85132e62b7cSChuck Lever void nfs_server_remove_lists(struct nfs_server *server)
852fca5238eSChuck Lever {
853d3b4c9d7SAndy Adamson 	struct nfs_client *clp = server->nfs_client;
8544c03ae4aSTrond Myklebust 	struct nfs_net *nn;
855d3b4c9d7SAndy Adamson 
8564c03ae4aSTrond Myklebust 	if (clp == NULL)
8574c03ae4aSTrond Myklebust 		return;
85873ea666cSChuck Lever 	nn = net_generic(clp->cl_net, nfs_net_id);
859dc030858SStanislav Kinsbursky 	spin_lock(&nn->nfs_client_lock);
860fca5238eSChuck Lever 	list_del_rcu(&server->client_link);
8614c03ae4aSTrond Myklebust 	if (list_empty(&clp->cl_superblocks))
862d3b4c9d7SAndy Adamson 		set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
863fca5238eSChuck Lever 	list_del(&server->master_link);
864dc030858SStanislav Kinsbursky 	spin_unlock(&nn->nfs_client_lock);
865fca5238eSChuck Lever 
866fca5238eSChuck Lever 	synchronize_rcu();
867fca5238eSChuck Lever }
86832e62b7cSChuck Lever EXPORT_SYMBOL_GPL(nfs_server_remove_lists);
869fca5238eSChuck Lever 
87054ceac45SDavid Howells /*
87154ceac45SDavid Howells  * Allocate and initialise a server record
87254ceac45SDavid Howells  */
873fcf10398SBryan Schumaker struct nfs_server *nfs_alloc_server(void)
87454ceac45SDavid Howells {
87554ceac45SDavid Howells 	struct nfs_server *server;
87654ceac45SDavid Howells 
87754ceac45SDavid Howells 	server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
87854ceac45SDavid Howells 	if (!server)
87954ceac45SDavid Howells 		return NULL;
88054ceac45SDavid Howells 
88154ceac45SDavid Howells 	server->client = server->client_acl = ERR_PTR(-EINVAL);
88254ceac45SDavid Howells 
88354ceac45SDavid Howells 	/* Zero out the NFS state stuff */
88454ceac45SDavid Howells 	INIT_LIST_HEAD(&server->client_link);
88554ceac45SDavid Howells 	INIT_LIST_HEAD(&server->master_link);
886d3978bb3SChuck Lever 	INIT_LIST_HEAD(&server->delegations);
8876382a441SWeston Andros Adamson 	INIT_LIST_HEAD(&server->layouts);
8880aaaf5c4SChuck Lever 	INIT_LIST_HEAD(&server->state_owners_lru);
889*62164f31SOlga Kornievskaia 	INIT_LIST_HEAD(&server->ss_copies);
89054ceac45SDavid Howells 
891ef818a28SSteve Dickson 	atomic_set(&server->active, 0);
892ef818a28SSteve Dickson 
89354ceac45SDavid Howells 	server->io_stats = nfs_alloc_iostats();
89454ceac45SDavid Howells 	if (!server->io_stats) {
89554ceac45SDavid Howells 		kfree(server);
89654ceac45SDavid Howells 		return NULL;
89754ceac45SDavid Howells 	}
89854ceac45SDavid Howells 
8999157c31dSTrond Myklebust 	ida_init(&server->openowner_id);
900d2d7ce28STrond Myklebust 	ida_init(&server->lockowner_id);
901f7e8917aSFred Isaman 	pnfs_init_server(server);
90268ebf8feSBenjamin Coddington 	rpc_init_wait_queue(&server->uoc_rpcwaitq, "NFS UOC");
903f7e8917aSFred Isaman 
90454ceac45SDavid Howells 	return server;
90554ceac45SDavid Howells }
90689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_alloc_server);
90754ceac45SDavid Howells 
90854ceac45SDavid Howells /*
90954ceac45SDavid Howells  * Free up a server record
91054ceac45SDavid Howells  */
91154ceac45SDavid Howells void nfs_free_server(struct nfs_server *server)
91254ceac45SDavid Howells {
913fca5238eSChuck Lever 	nfs_server_remove_lists(server);
91454ceac45SDavid Howells 
91554ceac45SDavid Howells 	if (server->destroy != NULL)
91654ceac45SDavid Howells 		server->destroy(server);
9175cef338bSTrond Myklebust 
9185cef338bSTrond Myklebust 	if (!IS_ERR(server->client_acl))
9195cef338bSTrond Myklebust 		rpc_shutdown_client(server->client_acl);
92054ceac45SDavid Howells 	if (!IS_ERR(server->client))
92154ceac45SDavid Howells 		rpc_shutdown_client(server->client);
92254ceac45SDavid Howells 
92354ceac45SDavid Howells 	nfs_put_client(server->nfs_client);
92454ceac45SDavid Howells 
925d2d7ce28STrond Myklebust 	ida_destroy(&server->lockowner_id);
9269157c31dSTrond Myklebust 	ida_destroy(&server->openowner_id);
92754ceac45SDavid Howells 	nfs_free_iostats(server->io_stats);
92854ceac45SDavid Howells 	kfree(server);
92954ceac45SDavid Howells 	nfs_release_automount_timer();
93054ceac45SDavid Howells }
93189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_free_server);
93254ceac45SDavid Howells 
93354ceac45SDavid Howells /*
93454ceac45SDavid Howells  * Create a version 2 or 3 volume record
93554ceac45SDavid Howells  * - keyed on server and FSID
93654ceac45SDavid Howells  */
9371179acc6SBryan Schumaker struct nfs_server *nfs_create_server(struct nfs_mount_info *mount_info,
938ab7017a3SBryan Schumaker 				     struct nfs_subversion *nfs_mod)
93954ceac45SDavid Howells {
94054ceac45SDavid Howells 	struct nfs_server *server;
941fbca779aSTrond Myklebust 	struct nfs_fattr *fattr;
94254ceac45SDavid Howells 	int error;
94354ceac45SDavid Howells 
94454ceac45SDavid Howells 	server = nfs_alloc_server();
94554ceac45SDavid Howells 	if (!server)
94654ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
94754ceac45SDavid Howells 
948fbca779aSTrond Myklebust 	error = -ENOMEM;
949fbca779aSTrond Myklebust 	fattr = nfs_alloc_fattr();
950fbca779aSTrond Myklebust 	if (fattr == NULL)
951fbca779aSTrond Myklebust 		goto error;
952fbca779aSTrond Myklebust 
95354ceac45SDavid Howells 	/* Get a client representation */
9541179acc6SBryan Schumaker 	error = nfs_init_server(server, mount_info->parsed, nfs_mod);
95554ceac45SDavid Howells 	if (error < 0)
95654ceac45SDavid Howells 		goto error;
95754ceac45SDavid Howells 
95854ceac45SDavid Howells 	/* Probe the root fh to retrieve its FSID */
9591179acc6SBryan Schumaker 	error = nfs_probe_fsinfo(server, mount_info->mntfh, fattr);
96054ceac45SDavid Howells 	if (error < 0)
96154ceac45SDavid Howells 		goto error;
96254af3bb5STrond Myklebust 	if (server->nfs_client->rpc_ops->version == 3) {
96354af3bb5STrond Myklebust 		if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
96454af3bb5STrond Myklebust 			server->namelen = NFS3_MAXNAMLEN;
9651179acc6SBryan Schumaker 		if (!(mount_info->parsed->flags & NFS_MOUNT_NORDIRPLUS))
96654af3bb5STrond Myklebust 			server->caps |= NFS_CAP_READDIRPLUS;
96754af3bb5STrond Myklebust 	} else {
96854af3bb5STrond Myklebust 		if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
96954af3bb5STrond Myklebust 			server->namelen = NFS2_MAXNAMLEN;
97054af3bb5STrond Myklebust 	}
97154af3bb5STrond Myklebust 
972fbca779aSTrond Myklebust 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
973a841b54dSTrond Myklebust 		error = nfs_mod->rpc_ops->getattr(server, mount_info->mntfh,
974a841b54dSTrond Myklebust 				fattr, NULL, NULL);
97554ceac45SDavid Howells 		if (error < 0) {
97654ceac45SDavid Howells 			dprintk("nfs_create_server: getattr error = %d\n", -error);
97754ceac45SDavid Howells 			goto error;
97854ceac45SDavid Howells 		}
97954ceac45SDavid Howells 	}
980fbca779aSTrond Myklebust 	memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
98154ceac45SDavid Howells 
9826daabf1bSDavid Howells 	dprintk("Server FSID: %llx:%llx\n",
9836daabf1bSDavid Howells 		(unsigned long long) server->fsid.major,
9846daabf1bSDavid Howells 		(unsigned long long) server->fsid.minor);
98554ceac45SDavid Howells 
986fca5238eSChuck Lever 	nfs_server_insert_lists(server);
98754ceac45SDavid Howells 	server->mount_time = jiffies;
988fbca779aSTrond Myklebust 	nfs_free_fattr(fattr);
98954ceac45SDavid Howells 	return server;
99054ceac45SDavid Howells 
99154ceac45SDavid Howells error:
992fbca779aSTrond Myklebust 	nfs_free_fattr(fattr);
99354ceac45SDavid Howells 	nfs_free_server(server);
99454ceac45SDavid Howells 	return ERR_PTR(error);
99554ceac45SDavid Howells }
996ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create_server);
99754ceac45SDavid Howells 
99854ceac45SDavid Howells /*
99954ceac45SDavid Howells  * Clone an NFS2, NFS3 or NFS4 server record
100054ceac45SDavid Howells  */
100154ceac45SDavid Howells struct nfs_server *nfs_clone_server(struct nfs_server *source,
100254ceac45SDavid Howells 				    struct nfs_fh *fh,
10037e6eb683SBryan Schumaker 				    struct nfs_fattr *fattr,
10047e6eb683SBryan Schumaker 				    rpc_authflavor_t flavor)
100554ceac45SDavid Howells {
100654ceac45SDavid Howells 	struct nfs_server *server;
1007fbca779aSTrond Myklebust 	struct nfs_fattr *fattr_fsinfo;
100854ceac45SDavid Howells 	int error;
100954ceac45SDavid Howells 
101054ceac45SDavid Howells 	server = nfs_alloc_server();
101154ceac45SDavid Howells 	if (!server)
101254ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
101354ceac45SDavid Howells 
1014fbca779aSTrond Myklebust 	error = -ENOMEM;
1015fbca779aSTrond Myklebust 	fattr_fsinfo = nfs_alloc_fattr();
1016fbca779aSTrond Myklebust 	if (fattr_fsinfo == NULL)
1017fbca779aSTrond Myklebust 		goto out_free_server;
1018fbca779aSTrond Myklebust 
101954ceac45SDavid Howells 	/* Copy data from the source */
102054ceac45SDavid Howells 	server->nfs_client = source->nfs_client;
10210aaaf5c4SChuck Lever 	server->destroy = source->destroy;
1022212bf41dSElena Reshetova 	refcount_inc(&server->nfs_client->cl_count);
102354ceac45SDavid Howells 	nfs_server_copy_userdata(server, source);
102454ceac45SDavid Howells 
102554ceac45SDavid Howells 	server->fsid = fattr->fsid;
102654ceac45SDavid Howells 
102733170233STrond Myklebust 	error = nfs_init_server_rpcclient(server,
102833170233STrond Myklebust 			source->client->cl_timeout,
10297e6eb683SBryan Schumaker 			flavor);
103054ceac45SDavid Howells 	if (error < 0)
103154ceac45SDavid Howells 		goto out_free_server;
103254ceac45SDavid Howells 
103354ceac45SDavid Howells 	/* probe the filesystem info for this server filesystem */
1034fbca779aSTrond Myklebust 	error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
103554ceac45SDavid Howells 	if (error < 0)
103654ceac45SDavid Howells 		goto out_free_server;
103754ceac45SDavid Howells 
103854af3bb5STrond Myklebust 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
103954af3bb5STrond Myklebust 		server->namelen = NFS4_MAXNAMLEN;
104054af3bb5STrond Myklebust 
104154ceac45SDavid Howells 	error = nfs_start_lockd(server);
104254ceac45SDavid Howells 	if (error < 0)
104354ceac45SDavid Howells 		goto out_free_server;
104454ceac45SDavid Howells 
1045fca5238eSChuck Lever 	nfs_server_insert_lists(server);
104654ceac45SDavid Howells 	server->mount_time = jiffies;
104754ceac45SDavid Howells 
1048fbca779aSTrond Myklebust 	nfs_free_fattr(fattr_fsinfo);
104954ceac45SDavid Howells 	return server;
105054ceac45SDavid Howells 
105154ceac45SDavid Howells out_free_server:
1052fbca779aSTrond Myklebust 	nfs_free_fattr(fattr_fsinfo);
105354ceac45SDavid Howells 	nfs_free_server(server);
105454ceac45SDavid Howells 	return ERR_PTR(error);
105554ceac45SDavid Howells }
1056ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_clone_server);
10576aaca566SDavid Howells 
10586b13168bSStanislav Kinsbursky void nfs_clients_init(struct net *net)
10596b13168bSStanislav Kinsbursky {
10606b13168bSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(net, nfs_net_id);
10616b13168bSStanislav Kinsbursky 
10626b13168bSStanislav Kinsbursky 	INIT_LIST_HEAD(&nn->nfs_client_list);
1063c25d32b2SStanislav Kinsbursky 	INIT_LIST_HEAD(&nn->nfs_volume_list);
106489d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
106528cd1b3fSStanislav Kinsbursky 	idr_init(&nn->cb_ident_idr);
106628cd1b3fSStanislav Kinsbursky #endif
10674c03ae4aSTrond Myklebust 	spin_lock_init(&nn->nfs_client_lock);
10682f86e091SDeepa Dinamani 	nn->boot_time = ktime_get_real();
10696b13168bSStanislav Kinsbursky }
10706b13168bSStanislav Kinsbursky 
10716aaca566SDavid Howells #ifdef CONFIG_PROC_FS
10726aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
10736aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
10746aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v);
10756aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v);
10766aaca566SDavid Howells 
107788e9d34cSJames Morris static const struct seq_operations nfs_server_list_ops = {
10786aaca566SDavid Howells 	.start	= nfs_server_list_start,
10796aaca566SDavid Howells 	.next	= nfs_server_list_next,
10806aaca566SDavid Howells 	.stop	= nfs_server_list_stop,
10816aaca566SDavid Howells 	.show	= nfs_server_list_show,
10826aaca566SDavid Howells };
10836aaca566SDavid Howells 
10846aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
10856aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
10866aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v);
10876aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v);
10886aaca566SDavid Howells 
108988e9d34cSJames Morris static const struct seq_operations nfs_volume_list_ops = {
10906aaca566SDavid Howells 	.start	= nfs_volume_list_start,
10916aaca566SDavid Howells 	.next	= nfs_volume_list_next,
10926aaca566SDavid Howells 	.stop	= nfs_volume_list_stop,
10936aaca566SDavid Howells 	.show	= nfs_volume_list_show,
10946aaca566SDavid Howells };
10956aaca566SDavid Howells 
10966aaca566SDavid Howells /*
10976aaca566SDavid Howells  * set up the iterator to start reading from the server list and return the first item
10986aaca566SDavid Howells  */
10996aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
11008d11620eSJeff Layton 				__acquires(&nn->nfs_client_lock)
11016aaca566SDavid Howells {
110265b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
11036b13168bSStanislav Kinsbursky 
11046aaca566SDavid Howells 	/* lock the list against modification */
1105dc030858SStanislav Kinsbursky 	spin_lock(&nn->nfs_client_lock);
11066b13168bSStanislav Kinsbursky 	return seq_list_start_head(&nn->nfs_client_list, *_pos);
11076aaca566SDavid Howells }
11086aaca566SDavid Howells 
11096aaca566SDavid Howells /*
11106aaca566SDavid Howells  * move to next server
11116aaca566SDavid Howells  */
11126aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
11136aaca566SDavid Howells {
111465b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
11156b13168bSStanislav Kinsbursky 
11166b13168bSStanislav Kinsbursky 	return seq_list_next(v, &nn->nfs_client_list, pos);
11176aaca566SDavid Howells }
11186aaca566SDavid Howells 
11196aaca566SDavid Howells /*
11206aaca566SDavid Howells  * clean up after reading from the transports list
11216aaca566SDavid Howells  */
11226aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v)
11238d11620eSJeff Layton 				__releases(&nn->nfs_client_lock)
11246aaca566SDavid Howells {
112565b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1126dc030858SStanislav Kinsbursky 
1127dc030858SStanislav Kinsbursky 	spin_unlock(&nn->nfs_client_lock);
11286aaca566SDavid Howells }
11296aaca566SDavid Howells 
11306aaca566SDavid Howells /*
11316aaca566SDavid Howells  * display a header line followed by a load of call lines
11326aaca566SDavid Howells  */
11336aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v)
11346aaca566SDavid Howells {
11356aaca566SDavid Howells 	struct nfs_client *clp;
113665b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
11376aaca566SDavid Howells 
11386aaca566SDavid Howells 	/* display header on line 1 */
11396b13168bSStanislav Kinsbursky 	if (v == &nn->nfs_client_list) {
11406aaca566SDavid Howells 		seq_puts(m, "NV SERVER   PORT USE HOSTNAME\n");
11416aaca566SDavid Howells 		return 0;
11426aaca566SDavid Howells 	}
11436aaca566SDavid Howells 
11446aaca566SDavid Howells 	/* display one transport per line on subsequent lines */
11456aaca566SDavid Howells 	clp = list_entry(v, struct nfs_client, cl_share_link);
11466aaca566SDavid Howells 
1147940aab49SMalahal Naineni 	/* Check if the client is initialized */
1148940aab49SMalahal Naineni 	if (clp->cl_cons_state != NFS_CS_READY)
1149940aab49SMalahal Naineni 		return 0;
1150940aab49SMalahal Naineni 
11512446ab60STrond Myklebust 	rcu_read_lock();
11525d8515caSChuck Lever 	seq_printf(m, "v%u %s %s %3d %s\n",
115340c55319STrond Myklebust 		   clp->rpc_ops->version,
11545d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
11555d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1156212bf41dSElena Reshetova 		   refcount_read(&clp->cl_count),
11576aaca566SDavid Howells 		   clp->cl_hostname);
11582446ab60STrond Myklebust 	rcu_read_unlock();
11596aaca566SDavid Howells 
11606aaca566SDavid Howells 	return 0;
11616aaca566SDavid Howells }
11626aaca566SDavid Howells 
11636aaca566SDavid Howells /*
11646aaca566SDavid Howells  * set up the iterator to start reading from the volume list and return the first item
11656aaca566SDavid Howells  */
11666aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
11678d11620eSJeff Layton 				__acquires(&nn->nfs_client_lock)
11686aaca566SDavid Howells {
116965b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1170c25d32b2SStanislav Kinsbursky 
11716aaca566SDavid Howells 	/* lock the list against modification */
1172dc030858SStanislav Kinsbursky 	spin_lock(&nn->nfs_client_lock);
1173c25d32b2SStanislav Kinsbursky 	return seq_list_start_head(&nn->nfs_volume_list, *_pos);
11746aaca566SDavid Howells }
11756aaca566SDavid Howells 
11766aaca566SDavid Howells /*
11776aaca566SDavid Howells  * move to next volume
11786aaca566SDavid Howells  */
11796aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
11806aaca566SDavid Howells {
118165b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1182c25d32b2SStanislav Kinsbursky 
1183c25d32b2SStanislav Kinsbursky 	return seq_list_next(v, &nn->nfs_volume_list, pos);
11846aaca566SDavid Howells }
11856aaca566SDavid Howells 
11866aaca566SDavid Howells /*
11876aaca566SDavid Howells  * clean up after reading from the transports list
11886aaca566SDavid Howells  */
11896aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v)
11908d11620eSJeff Layton 				__releases(&nn->nfs_client_lock)
11916aaca566SDavid Howells {
119265b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1193dc030858SStanislav Kinsbursky 
1194dc030858SStanislav Kinsbursky 	spin_unlock(&nn->nfs_client_lock);
11956aaca566SDavid Howells }
11966aaca566SDavid Howells 
11976aaca566SDavid Howells /*
11986aaca566SDavid Howells  * display a header line followed by a load of call lines
11996aaca566SDavid Howells  */
12006aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v)
12016aaca566SDavid Howells {
12026aaca566SDavid Howells 	struct nfs_server *server;
12036aaca566SDavid Howells 	struct nfs_client *clp;
1204df05a49fSKinglong Mee 	char dev[13];	// 8 for 2^24, 1 for ':', 3 for 2^8, 1 for '\0'
1205df05a49fSKinglong Mee 	char fsid[34];	// 2 * 16 for %llx, 1 for ':', 1 for '\0'
120665b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
12076aaca566SDavid Howells 
12086aaca566SDavid Howells 	/* display header on line 1 */
1209c25d32b2SStanislav Kinsbursky 	if (v == &nn->nfs_volume_list) {
1210df05a49fSKinglong Mee 		seq_puts(m, "NV SERVER   PORT DEV          FSID"
1211df05a49fSKinglong Mee 			    "                              FSC\n");
12126aaca566SDavid Howells 		return 0;
12136aaca566SDavid Howells 	}
12146aaca566SDavid Howells 	/* display one transport per line on subsequent lines */
12156aaca566SDavid Howells 	server = list_entry(v, struct nfs_server, master_link);
12166aaca566SDavid Howells 	clp = server->nfs_client;
12176aaca566SDavid Howells 
1218df05a49fSKinglong Mee 	snprintf(dev, sizeof(dev), "%u:%u",
12196aaca566SDavid Howells 		 MAJOR(server->s_dev), MINOR(server->s_dev));
12206aaca566SDavid Howells 
1221df05a49fSKinglong Mee 	snprintf(fsid, sizeof(fsid), "%llx:%llx",
12226daabf1bSDavid Howells 		 (unsigned long long) server->fsid.major,
12236daabf1bSDavid Howells 		 (unsigned long long) server->fsid.minor);
12246aaca566SDavid Howells 
12252446ab60STrond Myklebust 	rcu_read_lock();
1226df05a49fSKinglong Mee 	seq_printf(m, "v%u %s %s %-12s %-33s %s\n",
122740c55319STrond Myklebust 		   clp->rpc_ops->version,
12285d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
12295d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
12306aaca566SDavid Howells 		   dev,
12315d1acff1SDavid Howells 		   fsid,
12325d1acff1SDavid Howells 		   nfs_server_fscache_state(server));
12332446ab60STrond Myklebust 	rcu_read_unlock();
12346aaca566SDavid Howells 
12356aaca566SDavid Howells 	return 0;
12366aaca566SDavid Howells }
12376aaca566SDavid Howells 
123865b38851SEric W. Biederman int nfs_fs_proc_net_init(struct net *net)
123965b38851SEric W. Biederman {
124065b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(net, nfs_net_id);
124165b38851SEric W. Biederman 	struct proc_dir_entry *p;
124265b38851SEric W. Biederman 
124365b38851SEric W. Biederman 	nn->proc_nfsfs = proc_net_mkdir(net, "nfsfs", net->proc_net);
124465b38851SEric W. Biederman 	if (!nn->proc_nfsfs)
124565b38851SEric W. Biederman 		goto error_0;
124665b38851SEric W. Biederman 
124765b38851SEric W. Biederman 	/* a file of servers with which we're dealing */
1248c3506372SChristoph Hellwig 	p = proc_create_net("servers", S_IFREG|S_IRUGO, nn->proc_nfsfs,
1249c3506372SChristoph Hellwig 			&nfs_server_list_ops, sizeof(struct seq_net_private));
125065b38851SEric W. Biederman 	if (!p)
125165b38851SEric W. Biederman 		goto error_1;
125265b38851SEric W. Biederman 
125365b38851SEric W. Biederman 	/* a file of volumes that we have mounted */
1254c3506372SChristoph Hellwig 	p = proc_create_net("volumes", S_IFREG|S_IRUGO, nn->proc_nfsfs,
1255c3506372SChristoph Hellwig 			&nfs_volume_list_ops, sizeof(struct seq_net_private));
125665b38851SEric W. Biederman 	if (!p)
125721e81002SCong Wang 		goto error_1;
125865b38851SEric W. Biederman 	return 0;
125965b38851SEric W. Biederman 
126065b38851SEric W. Biederman error_1:
126121e81002SCong Wang 	remove_proc_subtree("nfsfs", net->proc_net);
126265b38851SEric W. Biederman error_0:
126365b38851SEric W. Biederman 	return -ENOMEM;
126465b38851SEric W. Biederman }
126565b38851SEric W. Biederman 
126665b38851SEric W. Biederman void nfs_fs_proc_net_exit(struct net *net)
126765b38851SEric W. Biederman {
126821e81002SCong Wang 	remove_proc_subtree("nfsfs", net->proc_net);
126965b38851SEric W. Biederman }
127065b38851SEric W. Biederman 
12716aaca566SDavid Howells /*
12726aaca566SDavid Howells  * initialise the /proc/fs/nfsfs/ directory
12736aaca566SDavid Howells  */
12746aaca566SDavid Howells int __init nfs_fs_proc_init(void)
12756aaca566SDavid Howells {
12766a062a36SKinglong Mee 	if (!proc_mkdir("fs/nfsfs", NULL))
12776aaca566SDavid Howells 		goto error_0;
12786aaca566SDavid Howells 
12796aaca566SDavid Howells 	/* a file of servers with which we're dealing */
12806a062a36SKinglong Mee 	if (!proc_symlink("fs/nfsfs/servers", NULL, "../../net/nfsfs/servers"))
12816aaca566SDavid Howells 		goto error_1;
12826aaca566SDavid Howells 
12836aaca566SDavid Howells 	/* a file of volumes that we have mounted */
12846a062a36SKinglong Mee 	if (!proc_symlink("fs/nfsfs/volumes", NULL, "../../net/nfsfs/volumes"))
12856a062a36SKinglong Mee 		goto error_1;
12866aaca566SDavid Howells 
12876a062a36SKinglong Mee 	return 0;
12886aaca566SDavid Howells error_1:
12896a062a36SKinglong Mee 	remove_proc_subtree("fs/nfsfs", NULL);
12906aaca566SDavid Howells error_0:
12916aaca566SDavid Howells 	return -ENOMEM;
12926aaca566SDavid Howells }
12936aaca566SDavid Howells 
12946aaca566SDavid Howells /*
12956aaca566SDavid Howells  * clean up the /proc/fs/nfsfs/ directory
12966aaca566SDavid Howells  */
12976aaca566SDavid Howells void nfs_fs_proc_exit(void)
12986aaca566SDavid Howells {
12996a062a36SKinglong Mee 	remove_proc_subtree("fs/nfsfs", NULL);
13006aaca566SDavid Howells }
13016aaca566SDavid Howells 
13026aaca566SDavid Howells #endif /* CONFIG_PROC_FS */
1303