xref: /linux/fs/nfs/client.c (revision ce62b114bbad9346641d16853c528ba01513e1b0)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
224c8dbbbSDavid Howells /* client.c: NFS client sharing and management code
324c8dbbbSDavid Howells  *
424c8dbbbSDavid Howells  * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
524c8dbbbSDavid Howells  * Written by David Howells (dhowells@redhat.com)
624c8dbbbSDavid Howells  */
724c8dbbbSDavid Howells 
824c8dbbbSDavid Howells 
924c8dbbbSDavid Howells #include <linux/module.h>
1024c8dbbbSDavid Howells #include <linux/init.h>
11e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
1224c8dbbbSDavid Howells #include <linux/time.h>
1324c8dbbbSDavid Howells #include <linux/kernel.h>
1424c8dbbbSDavid Howells #include <linux/mm.h>
1524c8dbbbSDavid Howells #include <linux/string.h>
1624c8dbbbSDavid Howells #include <linux/stat.h>
1724c8dbbbSDavid Howells #include <linux/errno.h>
1824c8dbbbSDavid Howells #include <linux/unistd.h>
19d8efa4e6SAnna Schumaker #include <linux/sunrpc/addr.h>
2024c8dbbbSDavid Howells #include <linux/sunrpc/clnt.h>
2124c8dbbbSDavid Howells #include <linux/sunrpc/stats.h>
2224c8dbbbSDavid Howells #include <linux/sunrpc/metrics.h>
230896a725S\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h>
242cf7ff7aS\"Talpey, Thomas\ #include <linux/sunrpc/xprtrdma.h>
2524c8dbbbSDavid Howells #include <linux/nfs_fs.h>
2624c8dbbbSDavid Howells #include <linux/nfs_mount.h>
2724c8dbbbSDavid Howells #include <linux/nfs4_mount.h>
2824c8dbbbSDavid Howells #include <linux/lockd/bind.h>
2924c8dbbbSDavid Howells #include <linux/seq_file.h>
3024c8dbbbSDavid Howells #include <linux/mount.h>
3124c8dbbbSDavid Howells #include <linux/vfs.h>
3224c8dbbbSDavid Howells #include <linux/inet.h>
333b0d3f93STrond Myklebust #include <linux/in6.h>
345a0e3ad6STejun Heo #include <linux/slab.h>
3540401530SAl Viro #include <linux/idr.h>
363b0d3f93STrond Myklebust #include <net/ipv6.h>
3724c8dbbbSDavid Howells #include <linux/nfs_xdr.h>
380b5b7ae0SAndy Adamson #include <linux/sunrpc/bc_xprt.h>
396b13168bSStanislav Kinsbursky #include <linux/nsproxy.h>
406b13168bSStanislav Kinsbursky #include <linux/pid_namespace.h>
4124c8dbbbSDavid Howells 
4224c8dbbbSDavid Howells 
4324c8dbbbSDavid Howells #include "nfs4_fs.h"
4424c8dbbbSDavid Howells #include "callback.h"
4524c8dbbbSDavid Howells #include "delegation.h"
4624c8dbbbSDavid Howells #include "iostat.h"
4724c8dbbbSDavid Howells #include "internal.h"
4814727281SDavid Howells #include "fscache.h"
4985e174baSRicardo Labiaga #include "pnfs.h"
50ab7017a3SBryan Schumaker #include "nfs.h"
516b13168bSStanislav Kinsbursky #include "netns.h"
52bf11fbdbSTrond Myklebust #include "sysfs.h"
5304a5da69SFrank van der Linden #include "nfs42.h"
5424c8dbbbSDavid Howells 
5524c8dbbbSDavid Howells #define NFSDBG_FACILITY		NFSDBG_CLIENT
5624c8dbbbSDavid Howells 
5724c8dbbbSDavid Howells static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
58ab7017a3SBryan Schumaker static DEFINE_SPINLOCK(nfs_version_lock);
59ab7017a3SBryan Schumaker static DEFINE_MUTEX(nfs_version_mutex);
60ab7017a3SBryan Schumaker static LIST_HEAD(nfs_versions);
6124c8dbbbSDavid Howells 
6224c8dbbbSDavid Howells /*
635006a76cSDavid Howells  * RPC cruft for NFS
645006a76cSDavid Howells  */
65a613fa16STrond Myklebust static const struct rpc_version *nfs_version[5] = {
66ab7017a3SBryan Schumaker 	[2] = NULL,
67ab7017a3SBryan Schumaker 	[3] = NULL,
68ab7017a3SBryan Schumaker 	[4] = NULL,
695006a76cSDavid Howells };
705006a76cSDavid Howells 
71a613fa16STrond Myklebust const struct rpc_program nfs_program = {
725006a76cSDavid Howells 	.name			= "nfs",
735006a76cSDavid Howells 	.number			= NFS_PROGRAM,
745006a76cSDavid Howells 	.nrvers			= ARRAY_SIZE(nfs_version),
755006a76cSDavid Howells 	.version		= nfs_version,
765006a76cSDavid Howells 	.stats			= &nfs_rpcstat,
77fe0a9b74SJim Rees 	.pipe_dir_name		= NFS_PIPE_DIRNAME,
785006a76cSDavid Howells };
795006a76cSDavid Howells 
805006a76cSDavid Howells struct rpc_stat nfs_rpcstat = {
815006a76cSDavid Howells 	.program		= &nfs_program
825006a76cSDavid Howells };
835006a76cSDavid Howells 
84ab7017a3SBryan Schumaker static struct nfs_subversion *find_nfs_version(unsigned int version)
85ab7017a3SBryan Schumaker {
86ab7017a3SBryan Schumaker 	struct nfs_subversion *nfs;
87ab7017a3SBryan Schumaker 	spin_lock(&nfs_version_lock);
88ab7017a3SBryan Schumaker 
89ab7017a3SBryan Schumaker 	list_for_each_entry(nfs, &nfs_versions, list) {
90ab7017a3SBryan Schumaker 		if (nfs->rpc_ops->version == version) {
91ab7017a3SBryan Schumaker 			spin_unlock(&nfs_version_lock);
92ab7017a3SBryan Schumaker 			return nfs;
93ab7017a3SBryan Schumaker 		}
94ee34e136SYanchuan Nian 	}
95ab7017a3SBryan Schumaker 
96ab7017a3SBryan Schumaker 	spin_unlock(&nfs_version_lock);
97ee34e136SYanchuan Nian 	return ERR_PTR(-EPROTONOSUPPORT);
98ab7017a3SBryan Schumaker }
99ab7017a3SBryan Schumaker 
100ab7017a3SBryan Schumaker struct nfs_subversion *get_nfs_version(unsigned int version)
101ab7017a3SBryan Schumaker {
102ab7017a3SBryan Schumaker 	struct nfs_subversion *nfs = find_nfs_version(version);
103ab7017a3SBryan Schumaker 
104ab7017a3SBryan Schumaker 	if (IS_ERR(nfs)) {
105ab7017a3SBryan Schumaker 		mutex_lock(&nfs_version_mutex);
1061ae811eeSbjschuma@gmail.com 		request_module("nfsv%d", version);
107ab7017a3SBryan Schumaker 		nfs = find_nfs_version(version);
108ab7017a3SBryan Schumaker 		mutex_unlock(&nfs_version_mutex);
109ab7017a3SBryan Schumaker 	}
110ab7017a3SBryan Schumaker 
1111f70ef96SAlexey Khoroshilov 	if (!IS_ERR(nfs) && !try_module_get(nfs->owner))
1121f70ef96SAlexey Khoroshilov 		return ERR_PTR(-EAGAIN);
113ab7017a3SBryan Schumaker 	return nfs;
114ab7017a3SBryan Schumaker }
115ab7017a3SBryan Schumaker 
116ab7017a3SBryan Schumaker void put_nfs_version(struct nfs_subversion *nfs)
117ab7017a3SBryan Schumaker {
118ab7017a3SBryan Schumaker 	module_put(nfs->owner);
119ab7017a3SBryan Schumaker }
120ab7017a3SBryan Schumaker 
121ab7017a3SBryan Schumaker void register_nfs_version(struct nfs_subversion *nfs)
122ab7017a3SBryan Schumaker {
123ab7017a3SBryan Schumaker 	spin_lock(&nfs_version_lock);
124ab7017a3SBryan Schumaker 
125ab7017a3SBryan Schumaker 	list_add(&nfs->list, &nfs_versions);
126ab7017a3SBryan Schumaker 	nfs_version[nfs->rpc_ops->version] = nfs->rpc_vers;
127ab7017a3SBryan Schumaker 
128ab7017a3SBryan Schumaker 	spin_unlock(&nfs_version_lock);
129ab7017a3SBryan Schumaker }
130ab7017a3SBryan Schumaker EXPORT_SYMBOL_GPL(register_nfs_version);
131ab7017a3SBryan Schumaker 
132ab7017a3SBryan Schumaker void unregister_nfs_version(struct nfs_subversion *nfs)
133ab7017a3SBryan Schumaker {
134ab7017a3SBryan Schumaker 	spin_lock(&nfs_version_lock);
135ab7017a3SBryan Schumaker 
136ab7017a3SBryan Schumaker 	nfs_version[nfs->rpc_ops->version] = NULL;
137ab7017a3SBryan Schumaker 	list_del(&nfs->list);
138ab7017a3SBryan Schumaker 
139ab7017a3SBryan Schumaker 	spin_unlock(&nfs_version_lock);
140ab7017a3SBryan Schumaker }
141ab7017a3SBryan Schumaker EXPORT_SYMBOL_GPL(unregister_nfs_version);
142ab7017a3SBryan Schumaker 
143ab7017a3SBryan Schumaker /*
14424c8dbbbSDavid Howells  * Allocate a shared client record
14524c8dbbbSDavid Howells  *
14624c8dbbbSDavid Howells  * Since these are allocated/deallocated very rarely, we don't
14724c8dbbbSDavid Howells  * bother putting them in a slab cache...
14824c8dbbbSDavid Howells  */
1496663ee7fSBryan Schumaker struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
15024c8dbbbSDavid Howells {
15124c8dbbbSDavid Howells 	struct nfs_client *clp;
152a21bdd9bSChuck Lever 	int err = -ENOMEM;
15324c8dbbbSDavid Howells 
15424c8dbbbSDavid Howells 	if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
15524c8dbbbSDavid Howells 		goto error_0;
15624c8dbbbSDavid Howells 
15755dee1bcSScott Mayhew 	clp->cl_minorversion = cl_init->minorversion;
158ab7017a3SBryan Schumaker 	clp->cl_nfs_mod = cl_init->nfs_mod;
1591f70ef96SAlexey Khoroshilov 	if (!try_module_get(clp->cl_nfs_mod->owner))
1601f70ef96SAlexey Khoroshilov 		goto error_dealloc;
161ab7017a3SBryan Schumaker 
162ab7017a3SBryan Schumaker 	clp->rpc_ops = clp->cl_nfs_mod->rpc_ops;
16340c55319STrond Myklebust 
164212bf41dSElena Reshetova 	refcount_set(&clp->cl_count, 1);
16524c8dbbbSDavid Howells 	clp->cl_cons_state = NFS_CS_INITING;
16624c8dbbbSDavid Howells 
1676e4cffd7SChuck Lever 	memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
1686e4cffd7SChuck Lever 	clp->cl_addrlen = cl_init->addrlen;
16924c8dbbbSDavid Howells 
1703a498026STrond Myklebust 	if (cl_init->hostname) {
171a21bdd9bSChuck Lever 		err = -ENOMEM;
1723a498026STrond Myklebust 		clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
17324c8dbbbSDavid Howells 		if (!clp->cl_hostname)
17471468513SBenny Halevy 			goto error_cleanup;
17524c8dbbbSDavid Howells 	}
17624c8dbbbSDavid Howells 
17724c8dbbbSDavid Howells 	INIT_LIST_HEAD(&clp->cl_superblocks);
17824c8dbbbSDavid Howells 	clp->cl_rpcclient = ERR_PTR(-EINVAL);
17924c8dbbbSDavid Howells 
18059dca3b2STrond Myklebust 	clp->cl_proto = cl_init->proto;
1816619079dSTrond Myklebust 	clp->cl_nconnect = cl_init->nconnect;
18273ea666cSChuck Lever 	clp->cl_net = get_net(cl_init->net);
18359dca3b2STrond Myklebust 
1845e16923bSNeilBrown 	clp->cl_principal = "*";
18514727281SDavid Howells 	nfs_fscache_get_client_cookie(clp);
18614727281SDavid Howells 
18724c8dbbbSDavid Howells 	return clp;
18824c8dbbbSDavid Howells 
18971468513SBenny Halevy error_cleanup:
190ab7017a3SBryan Schumaker 	put_nfs_version(clp->cl_nfs_mod);
1911f70ef96SAlexey Khoroshilov error_dealloc:
19224c8dbbbSDavid Howells 	kfree(clp);
19324c8dbbbSDavid Howells error_0:
194a21bdd9bSChuck Lever 	return ERR_PTR(err);
19524c8dbbbSDavid Howells }
196ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_alloc_client);
19724c8dbbbSDavid Howells 
19889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
19910b7a70cSTrond Myklebust static void nfs_cleanup_cb_ident_idr(struct net *net)
200f4eecd5dSAndy Adamson {
20128cd1b3fSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(net, nfs_net_id);
20228cd1b3fSStanislav Kinsbursky 
20328cd1b3fSStanislav Kinsbursky 	idr_destroy(&nn->cb_ident_idr);
204f4eecd5dSAndy Adamson }
205f4eecd5dSAndy Adamson 
206f4eecd5dSAndy Adamson /* nfs_client_lock held */
207f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
208f4eecd5dSAndy Adamson {
20973ea666cSChuck Lever 	struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
21028cd1b3fSStanislav Kinsbursky 
211f4eecd5dSAndy Adamson 	if (clp->cl_cb_ident)
21228cd1b3fSStanislav Kinsbursky 		idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
213f4eecd5dSAndy Adamson }
214f4eecd5dSAndy Adamson 
215f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server)
216f7e8917aSFred Isaman {
217f7e8917aSFred Isaman 	rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
218f7e8917aSFred Isaman }
219f7e8917aSFred Isaman 
220888ef2e3SAlexandros Batsakis #else
22110b7a70cSTrond Myklebust static void nfs_cleanup_cb_ident_idr(struct net *net)
222f4eecd5dSAndy Adamson {
223f4eecd5dSAndy Adamson }
224f4eecd5dSAndy Adamson 
225f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
226f4eecd5dSAndy Adamson {
227f4eecd5dSAndy Adamson }
228f7e8917aSFred Isaman 
229f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server)
230f7e8917aSFred Isaman {
231f7e8917aSFred Isaman }
232f7e8917aSFred Isaman 
233888ef2e3SAlexandros Batsakis #endif /* CONFIG_NFS_V4 */
234888ef2e3SAlexandros Batsakis 
235888ef2e3SAlexandros Batsakis /*
2365dd3177aSTrond Myklebust  * Destroy a shared client record
2375dd3177aSTrond Myklebust  */
238cdb7ecedSBryan Schumaker void nfs_free_client(struct nfs_client *clp)
2395dd3177aSTrond Myklebust {
24014727281SDavid Howells 	nfs_fscache_release_client_cookie(clp);
24114727281SDavid Howells 
24224c8dbbbSDavid Howells 	/* -EIO all pending I/O */
24324c8dbbbSDavid Howells 	if (!IS_ERR(clp->cl_rpcclient))
24424c8dbbbSDavid Howells 		rpc_shutdown_client(clp->cl_rpcclient);
24524c8dbbbSDavid Howells 
24673ea666cSChuck Lever 	put_net(clp->cl_net);
247ab7017a3SBryan Schumaker 	put_nfs_version(clp->cl_nfs_mod);
24824c8dbbbSDavid Howells 	kfree(clp->cl_hostname);
249f11b2a1cSJeff Layton 	kfree(clp->cl_acceptor);
25024c8dbbbSDavid Howells 	kfree(clp);
25124c8dbbbSDavid Howells }
252ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_free_client);
25324c8dbbbSDavid Howells 
25424c8dbbbSDavid Howells /*
25524c8dbbbSDavid Howells  * Release a reference to a shared client record
25624c8dbbbSDavid Howells  */
25724c8dbbbSDavid Howells void nfs_put_client(struct nfs_client *clp)
25824c8dbbbSDavid Howells {
259dc030858SStanislav Kinsbursky 	struct nfs_net *nn;
260dc030858SStanislav Kinsbursky 
26127ba8512SDavid Howells 	if (!clp)
26227ba8512SDavid Howells 		return;
26327ba8512SDavid Howells 
26473ea666cSChuck Lever 	nn = net_generic(clp->cl_net, nfs_net_id);
26524c8dbbbSDavid Howells 
266212bf41dSElena Reshetova 	if (refcount_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
26724c8dbbbSDavid Howells 		list_del(&clp->cl_share_link);
268f4eecd5dSAndy Adamson 		nfs_cb_idr_remove_locked(clp);
269dc030858SStanislav Kinsbursky 		spin_unlock(&nn->nfs_client_lock);
27024c8dbbbSDavid Howells 
2711fea73a8STrond Myklebust 		WARN_ON_ONCE(!list_empty(&clp->cl_superblocks));
27224c8dbbbSDavid Howells 
273cdb7ecedSBryan Schumaker 		clp->rpc_ops->free_client(clp);
27424c8dbbbSDavid Howells 	}
27524c8dbbbSDavid Howells }
27616b374caSAndy Adamson EXPORT_SYMBOL_GPL(nfs_put_client);
27724c8dbbbSDavid Howells 
2783fbd67adSTrond Myklebust /*
279c81468a1STrond Myklebust  * Find an nfs_client on the list that matches the initialisation data
280c81468a1STrond Myklebust  * that is supplied.
281c81468a1STrond Myklebust  */
282c81468a1STrond Myklebust static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
28324c8dbbbSDavid Howells {
28424c8dbbbSDavid Howells 	struct nfs_client *clp;
285d7371c41SIan Dall 	const struct sockaddr *sap = data->addr;
2866b13168bSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(data->net, nfs_net_id);
287950a578cSRoberto Bergantinos Corpas 	int error;
28824c8dbbbSDavid Howells 
289c156618eSScott Mayhew again:
2906b13168bSStanislav Kinsbursky 	list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
291d7371c41SIan Dall 	        const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
29213bbc06aSTrond Myklebust 		/* Don't match clients that failed to initialise properly */
29313bbc06aSTrond Myklebust 		if (clp->cl_cons_state < 0)
29413bbc06aSTrond Myklebust 			continue;
29513bbc06aSTrond Myklebust 
296c156618eSScott Mayhew 		/* If a client is still initializing then we need to wait */
297c156618eSScott Mayhew 		if (clp->cl_cons_state > NFS_CS_READY) {
298c156618eSScott Mayhew 			refcount_inc(&clp->cl_count);
299c156618eSScott Mayhew 			spin_unlock(&nn->nfs_client_lock);
300950a578cSRoberto Bergantinos Corpas 			error = nfs_wait_client_init_complete(clp);
301c156618eSScott Mayhew 			nfs_put_client(clp);
302c260121aSBenjamin Coddington 			spin_lock(&nn->nfs_client_lock);
303950a578cSRoberto Bergantinos Corpas 			if (error < 0)
304950a578cSRoberto Bergantinos Corpas 				return ERR_PTR(error);
305c156618eSScott Mayhew 			goto again;
306c156618eSScott Mayhew 		}
307c156618eSScott Mayhew 
30824c8dbbbSDavid Howells 		/* Different NFS versions cannot share the same nfs_client */
309ab7017a3SBryan Schumaker 		if (clp->rpc_ops != data->nfs_mod->rpc_ops)
31024c8dbbbSDavid Howells 			continue;
31124c8dbbbSDavid Howells 
31259dca3b2STrond Myklebust 		if (clp->cl_proto != data->proto)
31359dca3b2STrond Myklebust 			continue;
3145aae4a9aSBenny Halevy 		/* Match nfsv4 minorversion */
3155aae4a9aSBenny Halevy 		if (clp->cl_minorversion != data->minorversion)
3165aae4a9aSBenny Halevy 			continue;
31752f98f1aSTrond Myklebust 
31852f98f1aSTrond Myklebust 		/* Match request for a dedicated DS */
31952f98f1aSTrond Myklebust 		if (test_bit(NFS_CS_DS, &data->init_flags) !=
32052f98f1aSTrond Myklebust 		    test_bit(NFS_CS_DS, &clp->cl_flags))
32152f98f1aSTrond Myklebust 			continue;
32252f98f1aSTrond Myklebust 
323c81468a1STrond Myklebust 		/* Match the full socket address */
324d8efa4e6SAnna Schumaker 		if (!rpc_cmp_addr_port(sap, clap))
32504ea1b3eSAndy Adamson 			/* Match all xprt_switch full socket addresses */
3268ef32955SPetr Vandrovec 			if (IS_ERR(clp->cl_rpcclient) ||
3278ef32955SPetr Vandrovec                             !rpc_clnt_xprt_switch_has_addr(clp->cl_rpcclient,
32804ea1b3eSAndy Adamson 							   sap))
32924c8dbbbSDavid Howells 				continue;
33024c8dbbbSDavid Howells 
331212bf41dSElena Reshetova 		refcount_inc(&clp->cl_count);
33224c8dbbbSDavid Howells 		return clp;
33324c8dbbbSDavid Howells 	}
334c81468a1STrond Myklebust 	return NULL;
33524c8dbbbSDavid Howells }
33624c8dbbbSDavid Howells 
337a33e4b03SWeston Andros Adamson /*
338a33e4b03SWeston Andros Adamson  * Return true if @clp is done initializing, false if still working on it.
339a33e4b03SWeston Andros Adamson  *
340a33e4b03SWeston Andros Adamson  * Use nfs_client_init_status to check if it was successful.
341a33e4b03SWeston Andros Adamson  */
342a33e4b03SWeston Andros Adamson bool nfs_client_init_is_complete(const struct nfs_client *clp)
3434697bd5eSTrond Myklebust {
34448d66b97STrond Myklebust 	return clp->cl_cons_state <= NFS_CS_READY;
3454697bd5eSTrond Myklebust }
346a33e4b03SWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_client_init_is_complete);
347a33e4b03SWeston Andros Adamson 
348a33e4b03SWeston Andros Adamson /*
349a33e4b03SWeston Andros Adamson  * Return 0 if @clp was successfully initialized, -errno otherwise.
350a33e4b03SWeston Andros Adamson  *
351a33e4b03SWeston Andros Adamson  * This must be called *after* nfs_client_init_is_complete() returns true,
352a33e4b03SWeston Andros Adamson  * otherwise it will pop WARN_ON_ONCE and return -EINVAL
353a33e4b03SWeston Andros Adamson  */
354a33e4b03SWeston Andros Adamson int nfs_client_init_status(const struct nfs_client *clp)
355a33e4b03SWeston Andros Adamson {
356a33e4b03SWeston Andros Adamson 	/* called without checking nfs_client_init_is_complete */
357a33e4b03SWeston Andros Adamson 	if (clp->cl_cons_state > NFS_CS_READY) {
358a33e4b03SWeston Andros Adamson 		WARN_ON_ONCE(1);
359a33e4b03SWeston Andros Adamson 		return -EINVAL;
360a33e4b03SWeston Andros Adamson 	}
361a33e4b03SWeston Andros Adamson 	return clp->cl_cons_state;
362a33e4b03SWeston Andros Adamson }
363a33e4b03SWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_client_init_status);
3644697bd5eSTrond Myklebust 
3654697bd5eSTrond Myklebust int nfs_wait_client_init_complete(const struct nfs_client *clp)
3664697bd5eSTrond Myklebust {
3674697bd5eSTrond Myklebust 	return wait_event_killable(nfs_client_active_wq,
3684697bd5eSTrond Myklebust 			nfs_client_init_is_complete(clp));
3694697bd5eSTrond Myklebust }
37089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wait_client_init_complete);
3714697bd5eSTrond Myklebust 
37224c8dbbbSDavid Howells /*
373f411703aSChuck Lever  * Found an existing client.  Make sure it's ready before returning.
374f411703aSChuck Lever  */
375f411703aSChuck Lever static struct nfs_client *
376f411703aSChuck Lever nfs_found_client(const struct nfs_client_initdata *cl_init,
377f411703aSChuck Lever 		 struct nfs_client *clp)
378f411703aSChuck Lever {
379f411703aSChuck Lever 	int error;
380f411703aSChuck Lever 
3814697bd5eSTrond Myklebust 	error = nfs_wait_client_init_complete(clp);
382f411703aSChuck Lever 	if (error < 0) {
383f411703aSChuck Lever 		nfs_put_client(clp);
384f411703aSChuck Lever 		return ERR_PTR(-ERESTARTSYS);
385f411703aSChuck Lever 	}
386f411703aSChuck Lever 
387f411703aSChuck Lever 	if (clp->cl_cons_state < NFS_CS_READY) {
388f411703aSChuck Lever 		error = clp->cl_cons_state;
389f411703aSChuck Lever 		nfs_put_client(clp);
390f411703aSChuck Lever 		return ERR_PTR(error);
391f411703aSChuck Lever 	}
392f411703aSChuck Lever 
39354ac471cSTrond Myklebust 	smp_rmb();
394f411703aSChuck Lever 	return clp;
395f411703aSChuck Lever }
396f411703aSChuck Lever 
397f411703aSChuck Lever /*
39824c8dbbbSDavid Howells  * Look up a client by IP address and protocol version
39924c8dbbbSDavid Howells  * - creates a new record if one doesn't yet exist
40024c8dbbbSDavid Howells  */
4017d38de3fSAnna Schumaker struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
40224c8dbbbSDavid Howells {
40324c8dbbbSDavid Howells 	struct nfs_client *clp, *new = NULL;
4046b13168bSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
405ab7017a3SBryan Schumaker 	const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops;
40624c8dbbbSDavid Howells 
40731434f49SPeng Tao 	if (cl_init->hostname == NULL) {
40831434f49SPeng Tao 		WARN_ON(1);
40931434f49SPeng Tao 		return NULL;
41031434f49SPeng Tao 	}
41131434f49SPeng Tao 
41224c8dbbbSDavid Howells 	/* see if the client already exists */
41324c8dbbbSDavid Howells 	do {
414dc030858SStanislav Kinsbursky 		spin_lock(&nn->nfs_client_lock);
41524c8dbbbSDavid Howells 
416c81468a1STrond Myklebust 		clp = nfs_match_client(cl_init);
417f411703aSChuck Lever 		if (clp) {
418f411703aSChuck Lever 			spin_unlock(&nn->nfs_client_lock);
419f411703aSChuck Lever 			if (new)
420cdb7ecedSBryan Schumaker 				new->rpc_ops->free_client(new);
4219f7761cfSBenjamin Coddington 			if (IS_ERR(clp))
4229f7761cfSBenjamin Coddington 				return clp;
423f411703aSChuck Lever 			return nfs_found_client(cl_init, clp);
424f411703aSChuck Lever 		}
4258cab4c39SChuck Lever 		if (new) {
42605f4c350SChuck Lever 			list_add_tail(&new->cl_share_link,
42705f4c350SChuck Lever 					&nn->nfs_client_list);
4288cab4c39SChuck Lever 			spin_unlock(&nn->nfs_client_lock);
4294bf590e0SChuck Lever 			new->cl_flags = cl_init->init_flags;
4305c6e5b60STrond Myklebust 			return rpc_ops->init_client(new, cl_init);
4318cab4c39SChuck Lever 		}
43224c8dbbbSDavid Howells 
433dc030858SStanislav Kinsbursky 		spin_unlock(&nn->nfs_client_lock);
43424c8dbbbSDavid Howells 
435ab7017a3SBryan Schumaker 		new = rpc_ops->alloc_client(cl_init);
436a21bdd9bSChuck Lever 	} while (!IS_ERR(new));
43724c8dbbbSDavid Howells 
438a21bdd9bSChuck Lever 	return new;
43924c8dbbbSDavid Howells }
44089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_get_client);
44124c8dbbbSDavid Howells 
44224c8dbbbSDavid Howells /*
44324c8dbbbSDavid Howells  * Mark a server as ready or failed
44424c8dbbbSDavid Howells  */
44576db6d95SAndy Adamson void nfs_mark_client_ready(struct nfs_client *clp, int state)
44624c8dbbbSDavid Howells {
44754ac471cSTrond Myklebust 	smp_wmb();
44824c8dbbbSDavid Howells 	clp->cl_cons_state = state;
44924c8dbbbSDavid Howells 	wake_up_all(&nfs_client_active_wq);
45024c8dbbbSDavid Howells }
45189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mark_client_ready);
4525006a76cSDavid Howells 
4535006a76cSDavid Howells /*
4545006a76cSDavid Howells  * Initialise the timeout values for a connection
4555006a76cSDavid Howells  */
456fcf10398SBryan Schumaker void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
457a956bedaSTrond Myklebust 				    int timeo, int retrans)
4585006a76cSDavid Howells {
4595006a76cSDavid Howells 	to->to_initval = timeo * HZ / 10;
4605006a76cSDavid Howells 	to->to_retries = retrans;
4615006a76cSDavid Howells 
4625006a76cSDavid Howells 	switch (proto) {
4630896a725S\"Talpey, Thomas\ 	case XPRT_TRANSPORT_TCP:
4642cf7ff7aS\"Talpey, Thomas\ 	case XPRT_TRANSPORT_RDMA:
465a956bedaSTrond Myklebust 		if (retrans == NFS_UNSPEC_RETRANS)
466259875efSTrond Myklebust 			to->to_retries = NFS_DEF_TCP_RETRANS;
4675a698243STrond Myklebust 		if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0)
468259875efSTrond Myklebust 			to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
4695006a76cSDavid Howells 		if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
4705006a76cSDavid Howells 			to->to_initval = NFS_MAX_TCP_TIMEOUT;
4715006a76cSDavid Howells 		to->to_increment = to->to_initval;
4725006a76cSDavid Howells 		to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
4737a3e3e18STrond Myklebust 		if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
4747a3e3e18STrond Myklebust 			to->to_maxval = NFS_MAX_TCP_TIMEOUT;
4757a3e3e18STrond Myklebust 		if (to->to_maxval < to->to_initval)
4767a3e3e18STrond Myklebust 			to->to_maxval = to->to_initval;
4775006a76cSDavid Howells 		to->to_exponential = 0;
4785006a76cSDavid Howells 		break;
4790896a725S\"Talpey, Thomas\ 	case XPRT_TRANSPORT_UDP:
480a956bedaSTrond Myklebust 		if (retrans == NFS_UNSPEC_RETRANS)
481259875efSTrond Myklebust 			to->to_retries = NFS_DEF_UDP_RETRANS;
482a956bedaSTrond Myklebust 		if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0)
483259875efSTrond Myklebust 			to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
4845006a76cSDavid Howells 		if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
4855006a76cSDavid Howells 			to->to_initval = NFS_MAX_UDP_TIMEOUT;
4865006a76cSDavid Howells 		to->to_maxval = NFS_MAX_UDP_TIMEOUT;
4875006a76cSDavid Howells 		to->to_exponential = 1;
4885006a76cSDavid Howells 		break;
489259875efSTrond Myklebust 	default:
490259875efSTrond Myklebust 		BUG();
4915006a76cSDavid Howells 	}
4925006a76cSDavid Howells }
49389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_timeout_values);
4945006a76cSDavid Howells 
4955006a76cSDavid Howells /*
4965006a76cSDavid Howells  * Create an RPC client handle
4975006a76cSDavid Howells  */
498428360d7SBryan Schumaker int nfs_create_rpc_client(struct nfs_client *clp,
4995c6e5b60STrond Myklebust 			  const struct nfs_client_initdata *cl_init,
5004bf590e0SChuck Lever 			  rpc_authflavor_t flavor)
5015006a76cSDavid Howells {
5025006a76cSDavid Howells 	struct rpc_clnt		*clnt = NULL;
50341877d20SChuck Lever 	struct rpc_create_args args = {
50473ea666cSChuck Lever 		.net		= clp->cl_net,
50559dca3b2STrond Myklebust 		.protocol	= clp->cl_proto,
5066619079dSTrond Myklebust 		.nconnect	= clp->cl_nconnect,
50741877d20SChuck Lever 		.address	= (struct sockaddr *)&clp->cl_addr,
5086e4cffd7SChuck Lever 		.addrsize	= clp->cl_addrlen,
5095c6e5b60STrond Myklebust 		.timeout	= cl_init->timeparms,
51041877d20SChuck Lever 		.servername	= clp->cl_hostname,
5115c6e5b60STrond Myklebust 		.nodename	= cl_init->nodename,
51241877d20SChuck Lever 		.program	= &nfs_program,
51341877d20SChuck Lever 		.version	= clp->rpc_ops->version,
51441877d20SChuck Lever 		.authflavor	= flavor,
5151a58e8a0STrond Myklebust 		.cred		= cl_init->cred,
51641877d20SChuck Lever 	};
5175006a76cSDavid Howells 
5184bf590e0SChuck Lever 	if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags))
5194a01b8a4SChuck Lever 		args.flags |= RPC_CLNT_CREATE_DISCRTRY;
52099875249STrond Myklebust 	if (test_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags))
52199875249STrond Myklebust 		args.flags |= RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT;
5224bf590e0SChuck Lever 	if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags))
5234a01b8a4SChuck Lever 		args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
52498f98cf5STrond Myklebust 	if (test_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags))
52598f98cf5STrond Myklebust 		args.flags |= RPC_CLNT_CREATE_INFINITE_SLOTS;
5264b1b69ceSTrond Myklebust 	if (test_bit(NFS_CS_NOPING, &clp->cl_flags))
5274b1b69ceSTrond Myklebust 		args.flags |= RPC_CLNT_CREATE_NOPING;
528e6237b6fSTrond Myklebust 	if (test_bit(NFS_CS_REUSEPORT, &clp->cl_flags))
529e6237b6fSTrond Myklebust 		args.flags |= RPC_CLNT_CREATE_REUSEPORT;
5304a01b8a4SChuck Lever 
5315006a76cSDavid Howells 	if (!IS_ERR(clp->cl_rpcclient))
5325006a76cSDavid Howells 		return 0;
5335006a76cSDavid Howells 
53441877d20SChuck Lever 	clnt = rpc_create(&args);
5355006a76cSDavid Howells 	if (IS_ERR(clnt)) {
5365006a76cSDavid Howells 		dprintk("%s: cannot create RPC client. Error = %ld\n",
5373110ff80SHarvey Harrison 				__func__, PTR_ERR(clnt));
5385006a76cSDavid Howells 		return PTR_ERR(clnt);
5395006a76cSDavid Howells 	}
5405006a76cSDavid Howells 
5415e16923bSNeilBrown 	clnt->cl_principal = clp->cl_principal;
5425006a76cSDavid Howells 	clp->cl_rpcclient = clnt;
5435006a76cSDavid Howells 	return 0;
5445006a76cSDavid Howells }
54589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create_rpc_client);
54654ceac45SDavid Howells 
54754ceac45SDavid Howells /*
54854ceac45SDavid Howells  * Version 2 or 3 client destruction
54954ceac45SDavid Howells  */
55054ceac45SDavid Howells static void nfs_destroy_server(struct nfs_server *server)
55154ceac45SDavid Howells {
552f259613aSNeilBrown 	if (server->nlm_host)
5539289e7f9SChuck Lever 		nlmclnt_done(server->nlm_host);
55454ceac45SDavid Howells }
55554ceac45SDavid Howells 
55654ceac45SDavid Howells /*
55754ceac45SDavid Howells  * Version 2 or 3 lockd setup
55854ceac45SDavid Howells  */
55954ceac45SDavid Howells static int nfs_start_lockd(struct nfs_server *server)
56054ceac45SDavid Howells {
5619289e7f9SChuck Lever 	struct nlm_host *host;
5629289e7f9SChuck Lever 	struct nfs_client *clp = server->nfs_client;
563883bb163SChuck Lever 	struct nlmclnt_initdata nlm_init = {
564883bb163SChuck Lever 		.hostname	= clp->cl_hostname,
565883bb163SChuck Lever 		.address	= (struct sockaddr *)&clp->cl_addr,
566883bb163SChuck Lever 		.addrlen	= clp->cl_addrlen,
567883bb163SChuck Lever 		.nfs_version	= clp->rpc_ops->version,
5680cb2659bSChuck Lever 		.noresvport	= server->flags & NFS_MOUNT_NORESVPORT ?
5690cb2659bSChuck Lever 					1 : 0,
57073ea666cSChuck Lever 		.net		= clp->cl_net,
571b1ece737SBenjamin Coddington 		.nlmclnt_ops 	= clp->cl_nfs_mod->rpc_ops->nlmclnt_ops,
572d18a9d3fSSargun Dhillon 		.cred		= server->cred,
573883bb163SChuck Lever 	};
57454ceac45SDavid Howells 
575883bb163SChuck Lever 	if (nlm_init.nfs_version > 3)
5769289e7f9SChuck Lever 		return 0;
5775eebde23SSuresh Jayaraman 	if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
5785eebde23SSuresh Jayaraman 			(server->flags & NFS_MOUNT_LOCAL_FCNTL))
5799289e7f9SChuck Lever 		return 0;
5809289e7f9SChuck Lever 
5818a6e5debSTrond Myklebust 	switch (clp->cl_proto) {
5828a6e5debSTrond Myklebust 		default:
5838a6e5debSTrond Myklebust 			nlm_init.protocol = IPPROTO_TCP;
5848a6e5debSTrond Myklebust 			break;
585b24ee6c6SOlga Kornievskaia #ifndef CONFIG_NFS_DISABLE_UDP_SUPPORT
5868a6e5debSTrond Myklebust 		case XPRT_TRANSPORT_UDP:
5878a6e5debSTrond Myklebust 			nlm_init.protocol = IPPROTO_UDP;
588b24ee6c6SOlga Kornievskaia #endif
5898a6e5debSTrond Myklebust 	}
5908a6e5debSTrond Myklebust 
591883bb163SChuck Lever 	host = nlmclnt_init(&nlm_init);
5929289e7f9SChuck Lever 	if (IS_ERR(host))
5939289e7f9SChuck Lever 		return PTR_ERR(host);
5949289e7f9SChuck Lever 
5959289e7f9SChuck Lever 	server->nlm_host = host;
59654ceac45SDavid Howells 	server->destroy = nfs_destroy_server;
5979289e7f9SChuck Lever 	return 0;
59854ceac45SDavid Howells }
59954ceac45SDavid Howells 
60054ceac45SDavid Howells /*
60154ceac45SDavid Howells  * Create a general RPC client
60254ceac45SDavid Howells  */
603fcf10398SBryan Schumaker int nfs_init_server_rpcclient(struct nfs_server *server,
60433170233STrond Myklebust 		const struct rpc_timeout *timeo,
60533170233STrond Myklebust 		rpc_authflavor_t pseudoflavour)
60654ceac45SDavid Howells {
60754ceac45SDavid Howells 	struct nfs_client *clp = server->nfs_client;
60854ceac45SDavid Howells 
609ba9b584cSChuck Lever 	server->client = rpc_clone_client_set_auth(clp->cl_rpcclient,
610ba9b584cSChuck Lever 							pseudoflavour);
61154ceac45SDavid Howells 	if (IS_ERR(server->client)) {
6123110ff80SHarvey Harrison 		dprintk("%s: couldn't create rpc_client!\n", __func__);
61354ceac45SDavid Howells 		return PTR_ERR(server->client);
61454ceac45SDavid Howells 	}
61554ceac45SDavid Howells 
61633170233STrond Myklebust 	memcpy(&server->client->cl_timeout_default,
61733170233STrond Myklebust 			timeo,
61833170233STrond Myklebust 			sizeof(server->client->cl_timeout_default));
61933170233STrond Myklebust 	server->client->cl_timeout = &server->client->cl_timeout_default;
62054ceac45SDavid Howells 	server->client->cl_softrtry = 0;
62191a575e1STrond Myklebust 	if (server->flags & NFS_MOUNT_SOFTERR)
62291a575e1STrond Myklebust 		server->client->cl_softerr = 1;
62354ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_SOFT)
62454ceac45SDavid Howells 		server->client->cl_softrtry = 1;
62554ceac45SDavid Howells 
62654ceac45SDavid Howells 	return 0;
62754ceac45SDavid Howells }
62889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_server_rpcclient);
62954ceac45SDavid Howells 
6308cab4c39SChuck Lever /**
6318cab4c39SChuck Lever  * nfs_init_client - Initialise an NFS2 or NFS3 client
6328cab4c39SChuck Lever  *
6338cab4c39SChuck Lever  * @clp: nfs_client to initialise
6345c6e5b60STrond Myklebust  * @cl_init: Initialisation parameters
6358cab4c39SChuck Lever  *
6368cab4c39SChuck Lever  * Returns pointer to an NFS client, or an ERR_PTR value.
63754ceac45SDavid Howells  */
6388cab4c39SChuck Lever struct nfs_client *nfs_init_client(struct nfs_client *clp,
6395c6e5b60STrond Myklebust 				   const struct nfs_client_initdata *cl_init)
64054ceac45SDavid Howells {
64154ceac45SDavid Howells 	int error;
64254ceac45SDavid Howells 
64354ceac45SDavid Howells 	/* the client is already initialised */
6442844b6aeSAnna Schumaker 	if (clp->cl_cons_state == NFS_CS_READY)
6458cab4c39SChuck Lever 		return clp;
64654ceac45SDavid Howells 
64754ceac45SDavid Howells 	/*
64854ceac45SDavid Howells 	 * Create a client RPC handle for doing FSSTAT with UNIX auth only
64954ceac45SDavid Howells 	 * - RFC 2623, sec 2.3.2
65054ceac45SDavid Howells 	 */
6515c6e5b60STrond Myklebust 	error = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_UNIX);
6522844b6aeSAnna Schumaker 	nfs_mark_client_ready(clp, error == 0 ? NFS_CS_READY : error);
6532844b6aeSAnna Schumaker 	if (error < 0) {
6548cab4c39SChuck Lever 		nfs_put_client(clp);
6552844b6aeSAnna Schumaker 		clp = ERR_PTR(error);
6562844b6aeSAnna Schumaker 	}
6572844b6aeSAnna Schumaker 	return clp;
65854ceac45SDavid Howells }
659ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_client);
66054ceac45SDavid Howells 
66154ceac45SDavid Howells /*
66254ceac45SDavid Howells  * Create a version 2 or 3 client
66354ceac45SDavid Howells  */
6642283f8d6S\"Talpey, Thomas\ static int nfs_init_server(struct nfs_server *server,
66562a55d08SScott Mayhew 			   const struct fs_context *fc)
66654ceac45SDavid Howells {
66762a55d08SScott Mayhew 	const struct nfs_fs_context *ctx = nfs_fc2context(fc);
6685c6e5b60STrond Myklebust 	struct rpc_timeout timeparms;
6693a498026STrond Myklebust 	struct nfs_client_initdata cl_init = {
67038465f5dSScott Mayhew 		.hostname = ctx->nfs_server.hostname,
67138465f5dSScott Mayhew 		.addr = (const struct sockaddr *)&ctx->nfs_server.address,
67238465f5dSScott Mayhew 		.addrlen = ctx->nfs_server.addrlen,
67362a55d08SScott Mayhew 		.nfs_mod = ctx->nfs_mod,
67438465f5dSScott Mayhew 		.proto = ctx->nfs_server.protocol,
67562a55d08SScott Mayhew 		.net = fc->net_ns,
6765c6e5b60STrond Myklebust 		.timeparms = &timeparms,
6771a58e8a0STrond Myklebust 		.cred = server->cred,
67838465f5dSScott Mayhew 		.nconnect = ctx->nfs_server.nconnect,
679e6237b6fSTrond Myklebust 		.init_flags = (1UL << NFS_CS_REUSEPORT),
6803a498026STrond Myklebust 	};
68154ceac45SDavid Howells 	struct nfs_client *clp;
6823a498026STrond Myklebust 	int error;
68354ceac45SDavid Howells 
68438465f5dSScott Mayhew 	nfs_init_timeout_values(&timeparms, ctx->nfs_server.protocol,
68538465f5dSScott Mayhew 				ctx->timeo, ctx->retrans);
68638465f5dSScott Mayhew 	if (ctx->flags & NFS_MOUNT_NORESVPORT)
6874bf590e0SChuck Lever 		set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
68845a52a02SAndy Adamson 
68954ceac45SDavid Howells 	/* Allocate or find a client reference we can use */
6907d38de3fSAnna Schumaker 	clp = nfs_get_client(&cl_init);
6914cbb9768SAnna Schumaker 	if (IS_ERR(clp))
69254ceac45SDavid Howells 		return PTR_ERR(clp);
69354ceac45SDavid Howells 
69454ceac45SDavid Howells 	server->nfs_client = clp;
69554ceac45SDavid Howells 
69654ceac45SDavid Howells 	/* Initialise the client representation from the mount data */
69738465f5dSScott Mayhew 	server->flags = ctx->flags;
69838465f5dSScott Mayhew 	server->options = ctx->options;
699*ce62b114STrond Myklebust 	server->caps |= NFS_CAP_HARDLINKS | NFS_CAP_SYMLINKS;
700*ce62b114STrond Myklebust 
701*ce62b114STrond Myklebust 	switch (clp->rpc_ops->version) {
702*ce62b114STrond Myklebust 	case 2:
703*ce62b114STrond Myklebust 		server->fattr_valid = NFS_ATTR_FATTR_V2;
704*ce62b114STrond Myklebust 		break;
705*ce62b114STrond Myklebust 	case 3:
706*ce62b114STrond Myklebust 		server->fattr_valid = NFS_ATTR_FATTR_V3;
707*ce62b114STrond Myklebust 		break;
708*ce62b114STrond Myklebust 	default:
709*ce62b114STrond Myklebust 		server->fattr_valid = NFS_ATTR_FATTR_V4;
710*ce62b114STrond Myklebust 	}
71154ceac45SDavid Howells 
71238465f5dSScott Mayhew 	if (ctx->rsize)
71338465f5dSScott Mayhew 		server->rsize = nfs_block_size(ctx->rsize, NULL);
71438465f5dSScott Mayhew 	if (ctx->wsize)
71538465f5dSScott Mayhew 		server->wsize = nfs_block_size(ctx->wsize, NULL);
71654ceac45SDavid Howells 
71738465f5dSScott Mayhew 	server->acregmin = ctx->acregmin * HZ;
71838465f5dSScott Mayhew 	server->acregmax = ctx->acregmax * HZ;
71938465f5dSScott Mayhew 	server->acdirmin = ctx->acdirmin * HZ;
72038465f5dSScott Mayhew 	server->acdirmax = ctx->acdirmax * HZ;
72154ceac45SDavid Howells 
72254ceac45SDavid Howells 	/* Start lockd here, before we might error out */
72354ceac45SDavid Howells 	error = nfs_start_lockd(server);
72454ceac45SDavid Howells 	if (error < 0)
72554ceac45SDavid Howells 		goto error;
72654ceac45SDavid Howells 
72738465f5dSScott Mayhew 	server->port = ctx->nfs_server.port;
72838465f5dSScott Mayhew 	server->auth_info = ctx->auth_info;
729f22d6d79SChuck Lever 
730a3f73c27SWeston Andros Adamson 	error = nfs_init_server_rpcclient(server, &timeparms,
73138465f5dSScott Mayhew 					  ctx->selected_flavor);
73254ceac45SDavid Howells 	if (error < 0)
73354ceac45SDavid Howells 		goto error;
73454ceac45SDavid Howells 
7353f8400d1SChuck Lever 	/* Preserve the values of mount_server-related mount options */
73638465f5dSScott Mayhew 	if (ctx->mount_server.addrlen) {
73738465f5dSScott Mayhew 		memcpy(&server->mountd_address, &ctx->mount_server.address,
73838465f5dSScott Mayhew 			ctx->mount_server.addrlen);
73938465f5dSScott Mayhew 		server->mountd_addrlen = ctx->mount_server.addrlen;
7403f8400d1SChuck Lever 	}
74138465f5dSScott Mayhew 	server->mountd_version = ctx->mount_server.version;
74238465f5dSScott Mayhew 	server->mountd_port = ctx->mount_server.port;
74338465f5dSScott Mayhew 	server->mountd_protocol = ctx->mount_server.protocol;
7443f8400d1SChuck Lever 
74538465f5dSScott Mayhew 	server->namelen  = ctx->namlen;
74654ceac45SDavid Howells 	return 0;
74754ceac45SDavid Howells 
74854ceac45SDavid Howells error:
74954ceac45SDavid Howells 	server->nfs_client = NULL;
75054ceac45SDavid Howells 	nfs_put_client(clp);
75154ceac45SDavid Howells 	return error;
75254ceac45SDavid Howells }
75354ceac45SDavid Howells 
75454ceac45SDavid Howells /*
75554ceac45SDavid Howells  * Load up the server record from information gained in an fsinfo record
75654ceac45SDavid Howells  */
757738fd0f3SBenny Halevy static void nfs_server_set_fsinfo(struct nfs_server *server,
758738fd0f3SBenny Halevy 				  struct nfs_fsinfo *fsinfo)
75954ceac45SDavid Howells {
76004a5da69SFrank van der Linden 	unsigned long max_rpc_payload, raw_max_rpc_payload;
76154ceac45SDavid Howells 
76254ceac45SDavid Howells 	/* Work out a lot of parameters */
76354ceac45SDavid Howells 	if (server->rsize == 0)
76454ceac45SDavid Howells 		server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
76554ceac45SDavid Howells 	if (server->wsize == 0)
76654ceac45SDavid Howells 		server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
76754ceac45SDavid Howells 
76854ceac45SDavid Howells 	if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
76954ceac45SDavid Howells 		server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
77054ceac45SDavid Howells 	if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
77154ceac45SDavid Howells 		server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
77254ceac45SDavid Howells 
77304a5da69SFrank van der Linden 	raw_max_rpc_payload = rpc_max_payload(server->client);
77404a5da69SFrank van der Linden 	max_rpc_payload = nfs_block_size(raw_max_rpc_payload, NULL);
77504a5da69SFrank van der Linden 
77654ceac45SDavid Howells 	if (server->rsize > max_rpc_payload)
77754ceac45SDavid Howells 		server->rsize = max_rpc_payload;
77854ceac45SDavid Howells 	if (server->rsize > NFS_MAX_FILE_IO_SIZE)
77954ceac45SDavid Howells 		server->rsize = NFS_MAX_FILE_IO_SIZE;
78009cbfeafSKirill A. Shutemov 	server->rpages = (server->rsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
781e0bf68ddSPeter Zijlstra 
78254ceac45SDavid Howells 	if (server->wsize > max_rpc_payload)
78354ceac45SDavid Howells 		server->wsize = max_rpc_payload;
78454ceac45SDavid Howells 	if (server->wsize > NFS_MAX_FILE_IO_SIZE)
78554ceac45SDavid Howells 		server->wsize = NFS_MAX_FILE_IO_SIZE;
78609cbfeafSKirill A. Shutemov 	server->wpages = (server->wsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
78785e174baSRicardo Labiaga 
78854ceac45SDavid Howells 	server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
78954ceac45SDavid Howells 
79054ceac45SDavid Howells 	server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
7911a34c8c9STrond Myklebust 	if (server->dtsize > NFS_MAX_FILE_IO_SIZE)
7921a34c8c9STrond Myklebust 		server->dtsize = NFS_MAX_FILE_IO_SIZE;
79354ceac45SDavid Howells 	if (server->dtsize > server->rsize)
79454ceac45SDavid Howells 		server->dtsize = server->rsize;
79554ceac45SDavid Howells 
79654ceac45SDavid Howells 	if (server->flags & NFS_MOUNT_NOAC) {
79754ceac45SDavid Howells 		server->acregmin = server->acregmax = 0;
79854ceac45SDavid Howells 		server->acdirmin = server->acdirmax = 0;
79954ceac45SDavid Howells 	}
80054ceac45SDavid Howells 
80154ceac45SDavid Howells 	server->maxfilesize = fsinfo->maxfilesize;
80254ceac45SDavid Howells 
8036b96724eSRicardo Labiaga 	server->time_delta = fsinfo->time_delta;
8047f08a335STrond Myklebust 	server->change_attr_type = fsinfo->change_attr_type;
8056b96724eSRicardo Labiaga 
8062a92ee92SPeng Tao 	server->clone_blksize = fsinfo->clone_blksize;
80754ceac45SDavid Howells 	/* We're airborne Set socket buffersize */
80854ceac45SDavid Howells 	rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
80904a5da69SFrank van der Linden 
81004a5da69SFrank van der Linden #ifdef CONFIG_NFS_V4_2
81104a5da69SFrank van der Linden 	/*
81204a5da69SFrank van der Linden 	 * Defaults until limited by the session parameters.
81304a5da69SFrank van der Linden 	 */
81404a5da69SFrank van der Linden 	server->gxasize = min_t(unsigned int, raw_max_rpc_payload,
81504a5da69SFrank van der Linden 				XATTR_SIZE_MAX);
81604a5da69SFrank van der Linden 	server->sxasize = min_t(unsigned int, raw_max_rpc_payload,
81704a5da69SFrank van der Linden 				XATTR_SIZE_MAX);
81804a5da69SFrank van der Linden 	server->lxasize = min_t(unsigned int, raw_max_rpc_payload,
81904a5da69SFrank van der Linden 				nfs42_listxattr_xdrsize(XATTR_LIST_MAX));
820b78ef845SFrank van der Linden 
821b78ef845SFrank van der Linden 	if (fsinfo->xattr_support)
822b78ef845SFrank van der Linden 		server->caps |= NFS_CAP_XATTR;
82304a5da69SFrank van der Linden #endif
82454ceac45SDavid Howells }
82554ceac45SDavid Howells 
82654ceac45SDavid Howells /*
82754ceac45SDavid Howells  * Probe filesystem information, including the FSID on v2/v3
82854ceac45SDavid Howells  */
829fcf10398SBryan Schumaker int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
83054ceac45SDavid Howells {
83154ceac45SDavid Howells 	struct nfs_fsinfo fsinfo;
83254ceac45SDavid Howells 	struct nfs_client *clp = server->nfs_client;
83354ceac45SDavid Howells 	int error;
83454ceac45SDavid Howells 
83554ceac45SDavid Howells 	if (clp->rpc_ops->set_capabilities != NULL) {
83654ceac45SDavid Howells 		error = clp->rpc_ops->set_capabilities(server, mntfh);
83754ceac45SDavid Howells 		if (error < 0)
8384cbb9768SAnna Schumaker 			return error;
83954ceac45SDavid Howells 	}
84054ceac45SDavid Howells 
84154ceac45SDavid Howells 	fsinfo.fattr = fattr;
842ca440c38SJeff Layton 	fsinfo.nlayouttypes = 0;
8433132e49eSJeff Layton 	memset(fsinfo.layouttype, 0, sizeof(fsinfo.layouttype));
84454ceac45SDavid Howells 	error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
84554ceac45SDavid Howells 	if (error < 0)
8464cbb9768SAnna Schumaker 		return error;
84754ceac45SDavid Howells 
84871f81e51SKinglong Mee 	nfs_server_set_fsinfo(server, &fsinfo);
84954ceac45SDavid Howells 
85054ceac45SDavid Howells 	/* Get some general file system info */
85154ceac45SDavid Howells 	if (server->namelen == 0) {
85254ceac45SDavid Howells 		struct nfs_pathconf pathinfo;
85354ceac45SDavid Howells 
85454ceac45SDavid Howells 		pathinfo.fattr = fattr;
85554ceac45SDavid Howells 		nfs_fattr_init(fattr);
85654ceac45SDavid Howells 
85754ceac45SDavid Howells 		if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
85854ceac45SDavid Howells 			server->namelen = pathinfo.max_namelen;
85954ceac45SDavid Howells 	}
86054ceac45SDavid Howells 
86154ceac45SDavid Howells 	return 0;
86254ceac45SDavid Howells }
86389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_probe_fsinfo);
86454ceac45SDavid Howells 
86554ceac45SDavid Howells /*
86654ceac45SDavid Howells  * Copy useful information when duplicating a server record
86754ceac45SDavid Howells  */
868fcf10398SBryan Schumaker void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
86954ceac45SDavid Howells {
87054ceac45SDavid Howells 	target->flags = source->flags;
871356e76b8SChuck Lever 	target->rsize = source->rsize;
872356e76b8SChuck Lever 	target->wsize = source->wsize;
87354ceac45SDavid Howells 	target->acregmin = source->acregmin;
87454ceac45SDavid Howells 	target->acregmax = source->acregmax;
87554ceac45SDavid Howells 	target->acdirmin = source->acdirmin;
87654ceac45SDavid Howells 	target->acdirmax = source->acdirmax;
87754ceac45SDavid Howells 	target->caps = source->caps;
8782df54806SDavid Howells 	target->options = source->options;
8790f5f49b8SWeston Andros Adamson 	target->auth_info = source->auth_info;
88089a6814dSSteve Dickson 	target->port = source->port;
88154ceac45SDavid Howells }
88289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_server_copy_userdata);
88354ceac45SDavid Howells 
884fcf10398SBryan Schumaker void nfs_server_insert_lists(struct nfs_server *server)
885fca5238eSChuck Lever {
886fca5238eSChuck Lever 	struct nfs_client *clp = server->nfs_client;
88773ea666cSChuck Lever 	struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
888fca5238eSChuck Lever 
889dc030858SStanislav Kinsbursky 	spin_lock(&nn->nfs_client_lock);
890fca5238eSChuck Lever 	list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
891c25d32b2SStanislav Kinsbursky 	list_add_tail(&server->master_link, &nn->nfs_volume_list);
892d3b4c9d7SAndy Adamson 	clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
893dc030858SStanislav Kinsbursky 	spin_unlock(&nn->nfs_client_lock);
894fca5238eSChuck Lever 
895fca5238eSChuck Lever }
89689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_server_insert_lists);
897fca5238eSChuck Lever 
89832e62b7cSChuck Lever void nfs_server_remove_lists(struct nfs_server *server)
899fca5238eSChuck Lever {
900d3b4c9d7SAndy Adamson 	struct nfs_client *clp = server->nfs_client;
9014c03ae4aSTrond Myklebust 	struct nfs_net *nn;
902d3b4c9d7SAndy Adamson 
9034c03ae4aSTrond Myklebust 	if (clp == NULL)
9044c03ae4aSTrond Myklebust 		return;
90573ea666cSChuck Lever 	nn = net_generic(clp->cl_net, nfs_net_id);
906dc030858SStanislav Kinsbursky 	spin_lock(&nn->nfs_client_lock);
907fca5238eSChuck Lever 	list_del_rcu(&server->client_link);
9084c03ae4aSTrond Myklebust 	if (list_empty(&clp->cl_superblocks))
909d3b4c9d7SAndy Adamson 		set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
910fca5238eSChuck Lever 	list_del(&server->master_link);
911dc030858SStanislav Kinsbursky 	spin_unlock(&nn->nfs_client_lock);
912fca5238eSChuck Lever 
913fca5238eSChuck Lever 	synchronize_rcu();
914fca5238eSChuck Lever }
91532e62b7cSChuck Lever EXPORT_SYMBOL_GPL(nfs_server_remove_lists);
916fca5238eSChuck Lever 
91754ceac45SDavid Howells /*
91854ceac45SDavid Howells  * Allocate and initialise a server record
91954ceac45SDavid Howells  */
920fcf10398SBryan Schumaker struct nfs_server *nfs_alloc_server(void)
92154ceac45SDavid Howells {
92254ceac45SDavid Howells 	struct nfs_server *server;
92354ceac45SDavid Howells 
92454ceac45SDavid Howells 	server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
92554ceac45SDavid Howells 	if (!server)
92654ceac45SDavid Howells 		return NULL;
92754ceac45SDavid Howells 
92854ceac45SDavid Howells 	server->client = server->client_acl = ERR_PTR(-EINVAL);
92954ceac45SDavid Howells 
93054ceac45SDavid Howells 	/* Zero out the NFS state stuff */
93154ceac45SDavid Howells 	INIT_LIST_HEAD(&server->client_link);
93254ceac45SDavid Howells 	INIT_LIST_HEAD(&server->master_link);
933d3978bb3SChuck Lever 	INIT_LIST_HEAD(&server->delegations);
9346382a441SWeston Andros Adamson 	INIT_LIST_HEAD(&server->layouts);
9350aaaf5c4SChuck Lever 	INIT_LIST_HEAD(&server->state_owners_lru);
93662164f31SOlga Kornievskaia 	INIT_LIST_HEAD(&server->ss_copies);
93754ceac45SDavid Howells 
938ef818a28SSteve Dickson 	atomic_set(&server->active, 0);
939ef818a28SSteve Dickson 
94054ceac45SDavid Howells 	server->io_stats = nfs_alloc_iostats();
94154ceac45SDavid Howells 	if (!server->io_stats) {
94254ceac45SDavid Howells 		kfree(server);
94354ceac45SDavid Howells 		return NULL;
94454ceac45SDavid Howells 	}
94554ceac45SDavid Howells 
9467f08a335STrond Myklebust 	server->change_attr_type = NFS4_CHANGE_TYPE_IS_UNDEFINED;
9477f08a335STrond Myklebust 
9489157c31dSTrond Myklebust 	ida_init(&server->openowner_id);
949d2d7ce28STrond Myklebust 	ida_init(&server->lockowner_id);
950f7e8917aSFred Isaman 	pnfs_init_server(server);
95168ebf8feSBenjamin Coddington 	rpc_init_wait_queue(&server->uoc_rpcwaitq, "NFS UOC");
952f7e8917aSFred Isaman 
95354ceac45SDavid Howells 	return server;
95454ceac45SDavid Howells }
95589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_alloc_server);
95654ceac45SDavid Howells 
95754ceac45SDavid Howells /*
95854ceac45SDavid Howells  * Free up a server record
95954ceac45SDavid Howells  */
96054ceac45SDavid Howells void nfs_free_server(struct nfs_server *server)
96154ceac45SDavid Howells {
962fca5238eSChuck Lever 	nfs_server_remove_lists(server);
96354ceac45SDavid Howells 
96454ceac45SDavid Howells 	if (server->destroy != NULL)
96554ceac45SDavid Howells 		server->destroy(server);
9665cef338bSTrond Myklebust 
9675cef338bSTrond Myklebust 	if (!IS_ERR(server->client_acl))
9685cef338bSTrond Myklebust 		rpc_shutdown_client(server->client_acl);
96954ceac45SDavid Howells 	if (!IS_ERR(server->client))
97054ceac45SDavid Howells 		rpc_shutdown_client(server->client);
97154ceac45SDavid Howells 
97254ceac45SDavid Howells 	nfs_put_client(server->nfs_client);
97354ceac45SDavid Howells 
974d2d7ce28STrond Myklebust 	ida_destroy(&server->lockowner_id);
9759157c31dSTrond Myklebust 	ida_destroy(&server->openowner_id);
97654ceac45SDavid Howells 	nfs_free_iostats(server->io_stats);
9771a58e8a0STrond Myklebust 	put_cred(server->cred);
97854ceac45SDavid Howells 	kfree(server);
97954ceac45SDavid Howells 	nfs_release_automount_timer();
98054ceac45SDavid Howells }
98189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_free_server);
98254ceac45SDavid Howells 
98354ceac45SDavid Howells /*
98454ceac45SDavid Howells  * Create a version 2 or 3 volume record
98554ceac45SDavid Howells  * - keyed on server and FSID
98654ceac45SDavid Howells  */
98762a55d08SScott Mayhew struct nfs_server *nfs_create_server(struct fs_context *fc)
98854ceac45SDavid Howells {
98962a55d08SScott Mayhew 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
99054ceac45SDavid Howells 	struct nfs_server *server;
991fbca779aSTrond Myklebust 	struct nfs_fattr *fattr;
99254ceac45SDavid Howells 	int error;
99354ceac45SDavid Howells 
99454ceac45SDavid Howells 	server = nfs_alloc_server();
99554ceac45SDavid Howells 	if (!server)
99654ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
99754ceac45SDavid Howells 
998d18a9d3fSSargun Dhillon 	server->cred = get_cred(fc->cred);
9991a58e8a0STrond Myklebust 
1000fbca779aSTrond Myklebust 	error = -ENOMEM;
1001fbca779aSTrond Myklebust 	fattr = nfs_alloc_fattr();
1002fbca779aSTrond Myklebust 	if (fattr == NULL)
1003fbca779aSTrond Myklebust 		goto error;
1004fbca779aSTrond Myklebust 
100554ceac45SDavid Howells 	/* Get a client representation */
100662a55d08SScott Mayhew 	error = nfs_init_server(server, fc);
100754ceac45SDavid Howells 	if (error < 0)
100854ceac45SDavid Howells 		goto error;
100954ceac45SDavid Howells 
101054ceac45SDavid Howells 	/* Probe the root fh to retrieve its FSID */
101162a55d08SScott Mayhew 	error = nfs_probe_fsinfo(server, ctx->mntfh, fattr);
101254ceac45SDavid Howells 	if (error < 0)
101354ceac45SDavid Howells 		goto error;
101454af3bb5STrond Myklebust 	if (server->nfs_client->rpc_ops->version == 3) {
101554af3bb5STrond Myklebust 		if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
101654af3bb5STrond Myklebust 			server->namelen = NFS3_MAXNAMLEN;
101762a55d08SScott Mayhew 		if (!(ctx->flags & NFS_MOUNT_NORDIRPLUS))
101854af3bb5STrond Myklebust 			server->caps |= NFS_CAP_READDIRPLUS;
101954af3bb5STrond Myklebust 	} else {
102054af3bb5STrond Myklebust 		if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
102154af3bb5STrond Myklebust 			server->namelen = NFS2_MAXNAMLEN;
102254af3bb5STrond Myklebust 	}
102354af3bb5STrond Myklebust 
1024fbca779aSTrond Myklebust 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
102562a55d08SScott Mayhew 		error = ctx->nfs_mod->rpc_ops->getattr(server, ctx->mntfh,
1026a841b54dSTrond Myklebust 						       fattr, NULL, NULL);
102754ceac45SDavid Howells 		if (error < 0) {
102854ceac45SDavid Howells 			dprintk("nfs_create_server: getattr error = %d\n", -error);
102954ceac45SDavid Howells 			goto error;
103054ceac45SDavid Howells 		}
103154ceac45SDavid Howells 	}
1032fbca779aSTrond Myklebust 	memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
103354ceac45SDavid Howells 
10346daabf1bSDavid Howells 	dprintk("Server FSID: %llx:%llx\n",
10356daabf1bSDavid Howells 		(unsigned long long) server->fsid.major,
10366daabf1bSDavid Howells 		(unsigned long long) server->fsid.minor);
103754ceac45SDavid Howells 
1038fca5238eSChuck Lever 	nfs_server_insert_lists(server);
103954ceac45SDavid Howells 	server->mount_time = jiffies;
1040fbca779aSTrond Myklebust 	nfs_free_fattr(fattr);
104154ceac45SDavid Howells 	return server;
104254ceac45SDavid Howells 
104354ceac45SDavid Howells error:
1044fbca779aSTrond Myklebust 	nfs_free_fattr(fattr);
104554ceac45SDavid Howells 	nfs_free_server(server);
104654ceac45SDavid Howells 	return ERR_PTR(error);
104754ceac45SDavid Howells }
1048ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create_server);
104954ceac45SDavid Howells 
105054ceac45SDavid Howells /*
105154ceac45SDavid Howells  * Clone an NFS2, NFS3 or NFS4 server record
105254ceac45SDavid Howells  */
105354ceac45SDavid Howells struct nfs_server *nfs_clone_server(struct nfs_server *source,
105454ceac45SDavid Howells 				    struct nfs_fh *fh,
10557e6eb683SBryan Schumaker 				    struct nfs_fattr *fattr,
10567e6eb683SBryan Schumaker 				    rpc_authflavor_t flavor)
105754ceac45SDavid Howells {
105854ceac45SDavid Howells 	struct nfs_server *server;
1059fbca779aSTrond Myklebust 	struct nfs_fattr *fattr_fsinfo;
106054ceac45SDavid Howells 	int error;
106154ceac45SDavid Howells 
106254ceac45SDavid Howells 	server = nfs_alloc_server();
106354ceac45SDavid Howells 	if (!server)
106454ceac45SDavid Howells 		return ERR_PTR(-ENOMEM);
106554ceac45SDavid Howells 
10661a58e8a0STrond Myklebust 	server->cred = get_cred(source->cred);
10671a58e8a0STrond Myklebust 
1068fbca779aSTrond Myklebust 	error = -ENOMEM;
1069fbca779aSTrond Myklebust 	fattr_fsinfo = nfs_alloc_fattr();
1070fbca779aSTrond Myklebust 	if (fattr_fsinfo == NULL)
1071fbca779aSTrond Myklebust 		goto out_free_server;
1072fbca779aSTrond Myklebust 
107354ceac45SDavid Howells 	/* Copy data from the source */
107454ceac45SDavid Howells 	server->nfs_client = source->nfs_client;
10750aaaf5c4SChuck Lever 	server->destroy = source->destroy;
1076212bf41dSElena Reshetova 	refcount_inc(&server->nfs_client->cl_count);
107754ceac45SDavid Howells 	nfs_server_copy_userdata(server, source);
107854ceac45SDavid Howells 
107954ceac45SDavid Howells 	server->fsid = fattr->fsid;
108054ceac45SDavid Howells 
108133170233STrond Myklebust 	error = nfs_init_server_rpcclient(server,
108233170233STrond Myklebust 			source->client->cl_timeout,
10837e6eb683SBryan Schumaker 			flavor);
108454ceac45SDavid Howells 	if (error < 0)
108554ceac45SDavid Howells 		goto out_free_server;
108654ceac45SDavid Howells 
108754ceac45SDavid Howells 	/* probe the filesystem info for this server filesystem */
1088fbca779aSTrond Myklebust 	error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
108954ceac45SDavid Howells 	if (error < 0)
109054ceac45SDavid Howells 		goto out_free_server;
109154ceac45SDavid Howells 
109254af3bb5STrond Myklebust 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
109354af3bb5STrond Myklebust 		server->namelen = NFS4_MAXNAMLEN;
109454af3bb5STrond Myklebust 
109554ceac45SDavid Howells 	error = nfs_start_lockd(server);
109654ceac45SDavid Howells 	if (error < 0)
109754ceac45SDavid Howells 		goto out_free_server;
109854ceac45SDavid Howells 
1099fca5238eSChuck Lever 	nfs_server_insert_lists(server);
110054ceac45SDavid Howells 	server->mount_time = jiffies;
110154ceac45SDavid Howells 
1102fbca779aSTrond Myklebust 	nfs_free_fattr(fattr_fsinfo);
110354ceac45SDavid Howells 	return server;
110454ceac45SDavid Howells 
110554ceac45SDavid Howells out_free_server:
1106fbca779aSTrond Myklebust 	nfs_free_fattr(fattr_fsinfo);
110754ceac45SDavid Howells 	nfs_free_server(server);
110854ceac45SDavid Howells 	return ERR_PTR(error);
110954ceac45SDavid Howells }
1110ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_clone_server);
11116aaca566SDavid Howells 
11126b13168bSStanislav Kinsbursky void nfs_clients_init(struct net *net)
11136b13168bSStanislav Kinsbursky {
11146b13168bSStanislav Kinsbursky 	struct nfs_net *nn = net_generic(net, nfs_net_id);
11156b13168bSStanislav Kinsbursky 
11166b13168bSStanislav Kinsbursky 	INIT_LIST_HEAD(&nn->nfs_client_list);
1117c25d32b2SStanislav Kinsbursky 	INIT_LIST_HEAD(&nn->nfs_volume_list);
111889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4)
111928cd1b3fSStanislav Kinsbursky 	idr_init(&nn->cb_ident_idr);
112028cd1b3fSStanislav Kinsbursky #endif
11214c03ae4aSTrond Myklebust 	spin_lock_init(&nn->nfs_client_lock);
11222f86e091SDeepa Dinamani 	nn->boot_time = ktime_get_real();
1123bf11fbdbSTrond Myklebust 
1124bf11fbdbSTrond Myklebust 	nfs_netns_sysfs_setup(nn, net);
11256b13168bSStanislav Kinsbursky }
11266b13168bSStanislav Kinsbursky 
112710b7a70cSTrond Myklebust void nfs_clients_exit(struct net *net)
112810b7a70cSTrond Myklebust {
112910b7a70cSTrond Myklebust 	struct nfs_net *nn = net_generic(net, nfs_net_id);
113010b7a70cSTrond Myklebust 
1131bf11fbdbSTrond Myklebust 	nfs_netns_sysfs_destroy(nn);
113210b7a70cSTrond Myklebust 	nfs_cleanup_cb_ident_idr(net);
113310b7a70cSTrond Myklebust 	WARN_ON_ONCE(!list_empty(&nn->nfs_client_list));
113410b7a70cSTrond Myklebust 	WARN_ON_ONCE(!list_empty(&nn->nfs_volume_list));
113510b7a70cSTrond Myklebust }
113610b7a70cSTrond Myklebust 
11376aaca566SDavid Howells #ifdef CONFIG_PROC_FS
11386aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
11396aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
11406aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v);
11416aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v);
11426aaca566SDavid Howells 
114388e9d34cSJames Morris static const struct seq_operations nfs_server_list_ops = {
11446aaca566SDavid Howells 	.start	= nfs_server_list_start,
11456aaca566SDavid Howells 	.next	= nfs_server_list_next,
11466aaca566SDavid Howells 	.stop	= nfs_server_list_stop,
11476aaca566SDavid Howells 	.show	= nfs_server_list_show,
11486aaca566SDavid Howells };
11496aaca566SDavid Howells 
11506aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
11516aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
11526aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v);
11536aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v);
11546aaca566SDavid Howells 
115588e9d34cSJames Morris static const struct seq_operations nfs_volume_list_ops = {
11566aaca566SDavid Howells 	.start	= nfs_volume_list_start,
11576aaca566SDavid Howells 	.next	= nfs_volume_list_next,
11586aaca566SDavid Howells 	.stop	= nfs_volume_list_stop,
11596aaca566SDavid Howells 	.show	= nfs_volume_list_show,
11606aaca566SDavid Howells };
11616aaca566SDavid Howells 
11626aaca566SDavid Howells /*
11636aaca566SDavid Howells  * set up the iterator to start reading from the server list and return the first item
11646aaca566SDavid Howells  */
11656aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
11668d11620eSJeff Layton 				__acquires(&nn->nfs_client_lock)
11676aaca566SDavid Howells {
116865b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
11696b13168bSStanislav Kinsbursky 
11706aaca566SDavid Howells 	/* lock the list against modification */
1171dc030858SStanislav Kinsbursky 	spin_lock(&nn->nfs_client_lock);
11726b13168bSStanislav Kinsbursky 	return seq_list_start_head(&nn->nfs_client_list, *_pos);
11736aaca566SDavid Howells }
11746aaca566SDavid Howells 
11756aaca566SDavid Howells /*
11766aaca566SDavid Howells  * move to next server
11776aaca566SDavid Howells  */
11786aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
11796aaca566SDavid Howells {
118065b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
11816b13168bSStanislav Kinsbursky 
11826b13168bSStanislav Kinsbursky 	return seq_list_next(v, &nn->nfs_client_list, pos);
11836aaca566SDavid Howells }
11846aaca566SDavid Howells 
11856aaca566SDavid Howells /*
11866aaca566SDavid Howells  * clean up after reading from the transports list
11876aaca566SDavid Howells  */
11886aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v)
11898d11620eSJeff Layton 				__releases(&nn->nfs_client_lock)
11906aaca566SDavid Howells {
119165b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1192dc030858SStanislav Kinsbursky 
1193dc030858SStanislav Kinsbursky 	spin_unlock(&nn->nfs_client_lock);
11946aaca566SDavid Howells }
11956aaca566SDavid Howells 
11966aaca566SDavid Howells /*
11976aaca566SDavid Howells  * display a header line followed by a load of call lines
11986aaca566SDavid Howells  */
11996aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v)
12006aaca566SDavid Howells {
12016aaca566SDavid Howells 	struct nfs_client *clp;
120265b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
12036aaca566SDavid Howells 
12046aaca566SDavid Howells 	/* display header on line 1 */
12056b13168bSStanislav Kinsbursky 	if (v == &nn->nfs_client_list) {
12066aaca566SDavid Howells 		seq_puts(m, "NV SERVER   PORT USE HOSTNAME\n");
12076aaca566SDavid Howells 		return 0;
12086aaca566SDavid Howells 	}
12096aaca566SDavid Howells 
12106aaca566SDavid Howells 	/* display one transport per line on subsequent lines */
12116aaca566SDavid Howells 	clp = list_entry(v, struct nfs_client, cl_share_link);
12126aaca566SDavid Howells 
1213940aab49SMalahal Naineni 	/* Check if the client is initialized */
1214940aab49SMalahal Naineni 	if (clp->cl_cons_state != NFS_CS_READY)
1215940aab49SMalahal Naineni 		return 0;
1216940aab49SMalahal Naineni 
12172446ab60STrond Myklebust 	rcu_read_lock();
12185d8515caSChuck Lever 	seq_printf(m, "v%u %s %s %3d %s\n",
121940c55319STrond Myklebust 		   clp->rpc_ops->version,
12205d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
12215d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1222212bf41dSElena Reshetova 		   refcount_read(&clp->cl_count),
12236aaca566SDavid Howells 		   clp->cl_hostname);
12242446ab60STrond Myklebust 	rcu_read_unlock();
12256aaca566SDavid Howells 
12266aaca566SDavid Howells 	return 0;
12276aaca566SDavid Howells }
12286aaca566SDavid Howells 
12296aaca566SDavid Howells /*
12306aaca566SDavid Howells  * set up the iterator to start reading from the volume list and return the first item
12316aaca566SDavid Howells  */
12326aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
12338d11620eSJeff Layton 				__acquires(&nn->nfs_client_lock)
12346aaca566SDavid Howells {
123565b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1236c25d32b2SStanislav Kinsbursky 
12376aaca566SDavid Howells 	/* lock the list against modification */
1238dc030858SStanislav Kinsbursky 	spin_lock(&nn->nfs_client_lock);
1239c25d32b2SStanislav Kinsbursky 	return seq_list_start_head(&nn->nfs_volume_list, *_pos);
12406aaca566SDavid Howells }
12416aaca566SDavid Howells 
12426aaca566SDavid Howells /*
12436aaca566SDavid Howells  * move to next volume
12446aaca566SDavid Howells  */
12456aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
12466aaca566SDavid Howells {
124765b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1248c25d32b2SStanislav Kinsbursky 
1249c25d32b2SStanislav Kinsbursky 	return seq_list_next(v, &nn->nfs_volume_list, pos);
12506aaca566SDavid Howells }
12516aaca566SDavid Howells 
12526aaca566SDavid Howells /*
12536aaca566SDavid Howells  * clean up after reading from the transports list
12546aaca566SDavid Howells  */
12556aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v)
12568d11620eSJeff Layton 				__releases(&nn->nfs_client_lock)
12576aaca566SDavid Howells {
125865b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1259dc030858SStanislav Kinsbursky 
1260dc030858SStanislav Kinsbursky 	spin_unlock(&nn->nfs_client_lock);
12616aaca566SDavid Howells }
12626aaca566SDavid Howells 
12636aaca566SDavid Howells /*
12646aaca566SDavid Howells  * display a header line followed by a load of call lines
12656aaca566SDavid Howells  */
12666aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v)
12676aaca566SDavid Howells {
12686aaca566SDavid Howells 	struct nfs_server *server;
12696aaca566SDavid Howells 	struct nfs_client *clp;
1270df05a49fSKinglong Mee 	char dev[13];	// 8 for 2^24, 1 for ':', 3 for 2^8, 1 for '\0'
1271df05a49fSKinglong Mee 	char fsid[34];	// 2 * 16 for %llx, 1 for ':', 1 for '\0'
127265b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
12736aaca566SDavid Howells 
12746aaca566SDavid Howells 	/* display header on line 1 */
1275c25d32b2SStanislav Kinsbursky 	if (v == &nn->nfs_volume_list) {
1276df05a49fSKinglong Mee 		seq_puts(m, "NV SERVER   PORT DEV          FSID"
1277df05a49fSKinglong Mee 			    "                              FSC\n");
12786aaca566SDavid Howells 		return 0;
12796aaca566SDavid Howells 	}
12806aaca566SDavid Howells 	/* display one transport per line on subsequent lines */
12816aaca566SDavid Howells 	server = list_entry(v, struct nfs_server, master_link);
12826aaca566SDavid Howells 	clp = server->nfs_client;
12836aaca566SDavid Howells 
1284df05a49fSKinglong Mee 	snprintf(dev, sizeof(dev), "%u:%u",
12856aaca566SDavid Howells 		 MAJOR(server->s_dev), MINOR(server->s_dev));
12866aaca566SDavid Howells 
1287df05a49fSKinglong Mee 	snprintf(fsid, sizeof(fsid), "%llx:%llx",
12886daabf1bSDavid Howells 		 (unsigned long long) server->fsid.major,
12896daabf1bSDavid Howells 		 (unsigned long long) server->fsid.minor);
12906aaca566SDavid Howells 
12912446ab60STrond Myklebust 	rcu_read_lock();
1292df05a49fSKinglong Mee 	seq_printf(m, "v%u %s %s %-12s %-33s %s\n",
129340c55319STrond Myklebust 		   clp->rpc_ops->version,
12945d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
12955d8515caSChuck Lever 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
12966aaca566SDavid Howells 		   dev,
12975d1acff1SDavid Howells 		   fsid,
12985d1acff1SDavid Howells 		   nfs_server_fscache_state(server));
12992446ab60STrond Myklebust 	rcu_read_unlock();
13006aaca566SDavid Howells 
13016aaca566SDavid Howells 	return 0;
13026aaca566SDavid Howells }
13036aaca566SDavid Howells 
130465b38851SEric W. Biederman int nfs_fs_proc_net_init(struct net *net)
130565b38851SEric W. Biederman {
130665b38851SEric W. Biederman 	struct nfs_net *nn = net_generic(net, nfs_net_id);
130765b38851SEric W. Biederman 	struct proc_dir_entry *p;
130865b38851SEric W. Biederman 
130965b38851SEric W. Biederman 	nn->proc_nfsfs = proc_net_mkdir(net, "nfsfs", net->proc_net);
131065b38851SEric W. Biederman 	if (!nn->proc_nfsfs)
131165b38851SEric W. Biederman 		goto error_0;
131265b38851SEric W. Biederman 
131365b38851SEric W. Biederman 	/* a file of servers with which we're dealing */
1314c3506372SChristoph Hellwig 	p = proc_create_net("servers", S_IFREG|S_IRUGO, nn->proc_nfsfs,
1315c3506372SChristoph Hellwig 			&nfs_server_list_ops, sizeof(struct seq_net_private));
131665b38851SEric W. Biederman 	if (!p)
131765b38851SEric W. Biederman 		goto error_1;
131865b38851SEric W. Biederman 
131965b38851SEric W. Biederman 	/* a file of volumes that we have mounted */
1320c3506372SChristoph Hellwig 	p = proc_create_net("volumes", S_IFREG|S_IRUGO, nn->proc_nfsfs,
1321c3506372SChristoph Hellwig 			&nfs_volume_list_ops, sizeof(struct seq_net_private));
132265b38851SEric W. Biederman 	if (!p)
132321e81002SCong Wang 		goto error_1;
132465b38851SEric W. Biederman 	return 0;
132565b38851SEric W. Biederman 
132665b38851SEric W. Biederman error_1:
132721e81002SCong Wang 	remove_proc_subtree("nfsfs", net->proc_net);
132865b38851SEric W. Biederman error_0:
132965b38851SEric W. Biederman 	return -ENOMEM;
133065b38851SEric W. Biederman }
133165b38851SEric W. Biederman 
133265b38851SEric W. Biederman void nfs_fs_proc_net_exit(struct net *net)
133365b38851SEric W. Biederman {
133421e81002SCong Wang 	remove_proc_subtree("nfsfs", net->proc_net);
133565b38851SEric W. Biederman }
133665b38851SEric W. Biederman 
13376aaca566SDavid Howells /*
13386aaca566SDavid Howells  * initialise the /proc/fs/nfsfs/ directory
13396aaca566SDavid Howells  */
13406aaca566SDavid Howells int __init nfs_fs_proc_init(void)
13416aaca566SDavid Howells {
13426a062a36SKinglong Mee 	if (!proc_mkdir("fs/nfsfs", NULL))
13436aaca566SDavid Howells 		goto error_0;
13446aaca566SDavid Howells 
13456aaca566SDavid Howells 	/* a file of servers with which we're dealing */
13466a062a36SKinglong Mee 	if (!proc_symlink("fs/nfsfs/servers", NULL, "../../net/nfsfs/servers"))
13476aaca566SDavid Howells 		goto error_1;
13486aaca566SDavid Howells 
13496aaca566SDavid Howells 	/* a file of volumes that we have mounted */
13506a062a36SKinglong Mee 	if (!proc_symlink("fs/nfsfs/volumes", NULL, "../../net/nfsfs/volumes"))
13516a062a36SKinglong Mee 		goto error_1;
13526aaca566SDavid Howells 
13536a062a36SKinglong Mee 	return 0;
13546aaca566SDavid Howells error_1:
13556a062a36SKinglong Mee 	remove_proc_subtree("fs/nfsfs", NULL);
13566aaca566SDavid Howells error_0:
13576aaca566SDavid Howells 	return -ENOMEM;
13586aaca566SDavid Howells }
13596aaca566SDavid Howells 
13606aaca566SDavid Howells /*
13616aaca566SDavid Howells  * clean up the /proc/fs/nfsfs/ directory
13626aaca566SDavid Howells  */
13636aaca566SDavid Howells void nfs_fs_proc_exit(void)
13646aaca566SDavid Howells {
13656a062a36SKinglong Mee 	remove_proc_subtree("fs/nfsfs", NULL);
13666aaca566SDavid Howells }
13676aaca566SDavid Howells 
13686aaca566SDavid Howells #endif /* CONFIG_PROC_FS */
1369