18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * linux/net/sunrpc/svc_xprt.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author: Tom Tucker <tom@opengridcomputing.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/sched.h>
98c2ecf20Sopenharmony_ci#include <linux/errno.h>
108c2ecf20Sopenharmony_ci#include <linux/freezer.h>
118c2ecf20Sopenharmony_ci#include <linux/kthread.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <net/sock.h>
148c2ecf20Sopenharmony_ci#include <linux/sunrpc/addr.h>
158c2ecf20Sopenharmony_ci#include <linux/sunrpc/stats.h>
168c2ecf20Sopenharmony_ci#include <linux/sunrpc/svc_xprt.h>
178c2ecf20Sopenharmony_ci#include <linux/sunrpc/svcsock.h>
188c2ecf20Sopenharmony_ci#include <linux/sunrpc/xprt.h>
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
218c2ecf20Sopenharmony_ci#include <trace/events/sunrpc.h>
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define RPCDBG_FACILITY	RPCDBG_SVCXPRT
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cistatic unsigned int svc_rpc_per_connection_limit __read_mostly;
268c2ecf20Sopenharmony_cimodule_param(svc_rpc_per_connection_limit, uint, 0644);
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt);
308c2ecf20Sopenharmony_cistatic int svc_deferred_recv(struct svc_rqst *rqstp);
318c2ecf20Sopenharmony_cistatic struct cache_deferred_req *svc_defer(struct cache_req *req);
328c2ecf20Sopenharmony_cistatic void svc_age_temp_xprts(struct timer_list *t);
338c2ecf20Sopenharmony_cistatic void svc_delete_xprt(struct svc_xprt *xprt);
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* apparently the "standard" is that clients close
368c2ecf20Sopenharmony_ci * idle connections after 5 minutes, servers after
378c2ecf20Sopenharmony_ci * 6 minutes
388c2ecf20Sopenharmony_ci *   http://nfsv4bat.org/Documents/ConnectAThon/1996/nfstcp.pdf
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_cistatic int svc_conn_age_period = 6*60;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* List of registered transport classes */
438c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(svc_xprt_class_lock);
448c2ecf20Sopenharmony_cistatic LIST_HEAD(svc_xprt_class_list);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* SMP locking strategy:
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci *	svc_pool->sp_lock protects most of the fields of that pool.
498c2ecf20Sopenharmony_ci *	svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
508c2ecf20Sopenharmony_ci *	when both need to be taken (rare), svc_serv->sv_lock is first.
518c2ecf20Sopenharmony_ci *	The "service mutex" protects svc_serv->sv_nrthread.
528c2ecf20Sopenharmony_ci *	svc_sock->sk_lock protects the svc_sock->sk_deferred list
538c2ecf20Sopenharmony_ci *             and the ->sk_info_authunix cache.
548c2ecf20Sopenharmony_ci *
558c2ecf20Sopenharmony_ci *	The XPT_BUSY bit in xprt->xpt_flags prevents a transport being
568c2ecf20Sopenharmony_ci *	enqueued multiply. During normal transport processing this bit
578c2ecf20Sopenharmony_ci *	is set by svc_xprt_enqueue and cleared by svc_xprt_received.
588c2ecf20Sopenharmony_ci *	Providers should not manipulate this bit directly.
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci *	Some flags can be set to certain values at any time
618c2ecf20Sopenharmony_ci *	providing that certain rules are followed:
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci *	XPT_CONN, XPT_DATA:
648c2ecf20Sopenharmony_ci *		- Can be set or cleared at any time.
658c2ecf20Sopenharmony_ci *		- After a set, svc_xprt_enqueue must be called to enqueue
668c2ecf20Sopenharmony_ci *		  the transport for processing.
678c2ecf20Sopenharmony_ci *		- After a clear, the transport must be read/accepted.
688c2ecf20Sopenharmony_ci *		  If this succeeds, it must be set again.
698c2ecf20Sopenharmony_ci *	XPT_CLOSE:
708c2ecf20Sopenharmony_ci *		- Can set at any time. It is never cleared.
718c2ecf20Sopenharmony_ci *      XPT_DEAD:
728c2ecf20Sopenharmony_ci *		- Can only be set while XPT_BUSY is held which ensures
738c2ecf20Sopenharmony_ci *		  that no other thread will be using the transport or will
748c2ecf20Sopenharmony_ci *		  try to set XPT_DEAD.
758c2ecf20Sopenharmony_ci */
768c2ecf20Sopenharmony_ciint svc_reg_xprt_class(struct svc_xprt_class *xcl)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	struct svc_xprt_class *cl;
798c2ecf20Sopenharmony_ci	int res = -EEXIST;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&xcl->xcl_list);
848c2ecf20Sopenharmony_ci	spin_lock(&svc_xprt_class_lock);
858c2ecf20Sopenharmony_ci	/* Make sure there isn't already a class with the same name */
868c2ecf20Sopenharmony_ci	list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) {
878c2ecf20Sopenharmony_ci		if (strcmp(xcl->xcl_name, cl->xcl_name) == 0)
888c2ecf20Sopenharmony_ci			goto out;
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci	list_add_tail(&xcl->xcl_list, &svc_xprt_class_list);
918c2ecf20Sopenharmony_ci	res = 0;
928c2ecf20Sopenharmony_ciout:
938c2ecf20Sopenharmony_ci	spin_unlock(&svc_xprt_class_lock);
948c2ecf20Sopenharmony_ci	return res;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_reg_xprt_class);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_civoid svc_unreg_xprt_class(struct svc_xprt_class *xcl)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	dprintk("svc: Removing svc transport class '%s'\n", xcl->xcl_name);
1018c2ecf20Sopenharmony_ci	spin_lock(&svc_xprt_class_lock);
1028c2ecf20Sopenharmony_ci	list_del_init(&xcl->xcl_list);
1038c2ecf20Sopenharmony_ci	spin_unlock(&svc_xprt_class_lock);
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_unreg_xprt_class);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/**
1088c2ecf20Sopenharmony_ci * svc_print_xprts - Format the transport list for printing
1098c2ecf20Sopenharmony_ci * @buf: target buffer for formatted address
1108c2ecf20Sopenharmony_ci * @maxlen: length of target buffer
1118c2ecf20Sopenharmony_ci *
1128c2ecf20Sopenharmony_ci * Fills in @buf with a string containing a list of transport names, each name
1138c2ecf20Sopenharmony_ci * terminated with '\n'. If the buffer is too small, some entries may be
1148c2ecf20Sopenharmony_ci * missing, but it is guaranteed that all lines in the output buffer are
1158c2ecf20Sopenharmony_ci * complete.
1168c2ecf20Sopenharmony_ci *
1178c2ecf20Sopenharmony_ci * Returns positive length of the filled-in string.
1188c2ecf20Sopenharmony_ci */
1198c2ecf20Sopenharmony_ciint svc_print_xprts(char *buf, int maxlen)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct svc_xprt_class *xcl;
1228c2ecf20Sopenharmony_ci	char tmpstr[80];
1238c2ecf20Sopenharmony_ci	int len = 0;
1248c2ecf20Sopenharmony_ci	buf[0] = '\0';
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	spin_lock(&svc_xprt_class_lock);
1278c2ecf20Sopenharmony_ci	list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
1288c2ecf20Sopenharmony_ci		int slen;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci		slen = snprintf(tmpstr, sizeof(tmpstr), "%s %d\n",
1318c2ecf20Sopenharmony_ci				xcl->xcl_name, xcl->xcl_max_payload);
1328c2ecf20Sopenharmony_ci		if (slen >= sizeof(tmpstr) || len + slen >= maxlen)
1338c2ecf20Sopenharmony_ci			break;
1348c2ecf20Sopenharmony_ci		len += slen;
1358c2ecf20Sopenharmony_ci		strcat(buf, tmpstr);
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci	spin_unlock(&svc_xprt_class_lock);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	return len;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic void svc_xprt_free(struct kref *kref)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	struct svc_xprt *xprt =
1458c2ecf20Sopenharmony_ci		container_of(kref, struct svc_xprt, xpt_ref);
1468c2ecf20Sopenharmony_ci	struct module *owner = xprt->xpt_class->xcl_owner;
1478c2ecf20Sopenharmony_ci	if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags))
1488c2ecf20Sopenharmony_ci		svcauth_unix_info_release(xprt);
1498c2ecf20Sopenharmony_ci	put_cred(xprt->xpt_cred);
1508c2ecf20Sopenharmony_ci	put_net(xprt->xpt_net);
1518c2ecf20Sopenharmony_ci	/* See comment on corresponding get in xs_setup_bc_tcp(): */
1528c2ecf20Sopenharmony_ci	if (xprt->xpt_bc_xprt)
1538c2ecf20Sopenharmony_ci		xprt_put(xprt->xpt_bc_xprt);
1548c2ecf20Sopenharmony_ci	if (xprt->xpt_bc_xps)
1558c2ecf20Sopenharmony_ci		xprt_switch_put(xprt->xpt_bc_xps);
1568c2ecf20Sopenharmony_ci	trace_svc_xprt_free(xprt);
1578c2ecf20Sopenharmony_ci	xprt->xpt_ops->xpo_free(xprt);
1588c2ecf20Sopenharmony_ci	module_put(owner);
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_civoid svc_xprt_put(struct svc_xprt *xprt)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	kref_put(&xprt->xpt_ref, svc_xprt_free);
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_xprt_put);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci/*
1688c2ecf20Sopenharmony_ci * Called by transport drivers to initialize the transport independent
1698c2ecf20Sopenharmony_ci * portion of the transport instance.
1708c2ecf20Sopenharmony_ci */
1718c2ecf20Sopenharmony_civoid svc_xprt_init(struct net *net, struct svc_xprt_class *xcl,
1728c2ecf20Sopenharmony_ci		   struct svc_xprt *xprt, struct svc_serv *serv)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	memset(xprt, 0, sizeof(*xprt));
1758c2ecf20Sopenharmony_ci	xprt->xpt_class = xcl;
1768c2ecf20Sopenharmony_ci	xprt->xpt_ops = xcl->xcl_ops;
1778c2ecf20Sopenharmony_ci	kref_init(&xprt->xpt_ref);
1788c2ecf20Sopenharmony_ci	xprt->xpt_server = serv;
1798c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&xprt->xpt_list);
1808c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&xprt->xpt_ready);
1818c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&xprt->xpt_deferred);
1828c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&xprt->xpt_users);
1838c2ecf20Sopenharmony_ci	mutex_init(&xprt->xpt_mutex);
1848c2ecf20Sopenharmony_ci	spin_lock_init(&xprt->xpt_lock);
1858c2ecf20Sopenharmony_ci	set_bit(XPT_BUSY, &xprt->xpt_flags);
1868c2ecf20Sopenharmony_ci	xprt->xpt_net = get_net(net);
1878c2ecf20Sopenharmony_ci	strcpy(xprt->xpt_remotebuf, "uninitialized");
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_xprt_init);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic struct svc_xprt *__svc_xpo_create(struct svc_xprt_class *xcl,
1928c2ecf20Sopenharmony_ci					 struct svc_serv *serv,
1938c2ecf20Sopenharmony_ci					 struct net *net,
1948c2ecf20Sopenharmony_ci					 const int family,
1958c2ecf20Sopenharmony_ci					 const unsigned short port,
1968c2ecf20Sopenharmony_ci					 int flags)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	struct sockaddr_in sin = {
1998c2ecf20Sopenharmony_ci		.sin_family		= AF_INET,
2008c2ecf20Sopenharmony_ci		.sin_addr.s_addr	= htonl(INADDR_ANY),
2018c2ecf20Sopenharmony_ci		.sin_port		= htons(port),
2028c2ecf20Sopenharmony_ci	};
2038c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
2048c2ecf20Sopenharmony_ci	struct sockaddr_in6 sin6 = {
2058c2ecf20Sopenharmony_ci		.sin6_family		= AF_INET6,
2068c2ecf20Sopenharmony_ci		.sin6_addr		= IN6ADDR_ANY_INIT,
2078c2ecf20Sopenharmony_ci		.sin6_port		= htons(port),
2088c2ecf20Sopenharmony_ci	};
2098c2ecf20Sopenharmony_ci#endif
2108c2ecf20Sopenharmony_ci	struct svc_xprt *xprt;
2118c2ecf20Sopenharmony_ci	struct sockaddr *sap;
2128c2ecf20Sopenharmony_ci	size_t len;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	switch (family) {
2158c2ecf20Sopenharmony_ci	case PF_INET:
2168c2ecf20Sopenharmony_ci		sap = (struct sockaddr *)&sin;
2178c2ecf20Sopenharmony_ci		len = sizeof(sin);
2188c2ecf20Sopenharmony_ci		break;
2198c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
2208c2ecf20Sopenharmony_ci	case PF_INET6:
2218c2ecf20Sopenharmony_ci		sap = (struct sockaddr *)&sin6;
2228c2ecf20Sopenharmony_ci		len = sizeof(sin6);
2238c2ecf20Sopenharmony_ci		break;
2248c2ecf20Sopenharmony_ci#endif
2258c2ecf20Sopenharmony_ci	default:
2268c2ecf20Sopenharmony_ci		return ERR_PTR(-EAFNOSUPPORT);
2278c2ecf20Sopenharmony_ci	}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	xprt = xcl->xcl_ops->xpo_create(serv, net, sap, len, flags);
2308c2ecf20Sopenharmony_ci	if (IS_ERR(xprt))
2318c2ecf20Sopenharmony_ci		trace_svc_xprt_create_err(serv->sv_program->pg_name,
2328c2ecf20Sopenharmony_ci					  xcl->xcl_name, sap, xprt);
2338c2ecf20Sopenharmony_ci	return xprt;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci/*
2378c2ecf20Sopenharmony_ci * svc_xprt_received conditionally queues the transport for processing
2388c2ecf20Sopenharmony_ci * by another thread. The caller must hold the XPT_BUSY bit and must
2398c2ecf20Sopenharmony_ci * not thereafter touch transport data.
2408c2ecf20Sopenharmony_ci *
2418c2ecf20Sopenharmony_ci * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
2428c2ecf20Sopenharmony_ci * insufficient) data.
2438c2ecf20Sopenharmony_ci */
2448c2ecf20Sopenharmony_cistatic void svc_xprt_received(struct svc_xprt *xprt)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	if (!test_bit(XPT_BUSY, &xprt->xpt_flags)) {
2478c2ecf20Sopenharmony_ci		WARN_ONCE(1, "xprt=0x%p already busy!", xprt);
2488c2ecf20Sopenharmony_ci		return;
2498c2ecf20Sopenharmony_ci	}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	/* As soon as we clear busy, the xprt could be closed and
2528c2ecf20Sopenharmony_ci	 * 'put', so we need a reference to call svc_enqueue_xprt with:
2538c2ecf20Sopenharmony_ci	 */
2548c2ecf20Sopenharmony_ci	svc_xprt_get(xprt);
2558c2ecf20Sopenharmony_ci	smp_mb__before_atomic();
2568c2ecf20Sopenharmony_ci	clear_bit(XPT_BUSY, &xprt->xpt_flags);
2578c2ecf20Sopenharmony_ci	xprt->xpt_server->sv_ops->svo_enqueue_xprt(xprt);
2588c2ecf20Sopenharmony_ci	svc_xprt_put(xprt);
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_civoid svc_add_new_perm_xprt(struct svc_serv *serv, struct svc_xprt *new)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	clear_bit(XPT_TEMP, &new->xpt_flags);
2648c2ecf20Sopenharmony_ci	spin_lock_bh(&serv->sv_lock);
2658c2ecf20Sopenharmony_ci	list_add(&new->xpt_list, &serv->sv_permsocks);
2668c2ecf20Sopenharmony_ci	spin_unlock_bh(&serv->sv_lock);
2678c2ecf20Sopenharmony_ci	svc_xprt_received(new);
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic int _svc_create_xprt(struct svc_serv *serv, const char *xprt_name,
2718c2ecf20Sopenharmony_ci			    struct net *net, const int family,
2728c2ecf20Sopenharmony_ci			    const unsigned short port, int flags,
2738c2ecf20Sopenharmony_ci			    const struct cred *cred)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	struct svc_xprt_class *xcl;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	spin_lock(&svc_xprt_class_lock);
2788c2ecf20Sopenharmony_ci	list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
2798c2ecf20Sopenharmony_ci		struct svc_xprt *newxprt;
2808c2ecf20Sopenharmony_ci		unsigned short newport;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci		if (strcmp(xprt_name, xcl->xcl_name))
2838c2ecf20Sopenharmony_ci			continue;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci		if (!try_module_get(xcl->xcl_owner))
2868c2ecf20Sopenharmony_ci			goto err;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci		spin_unlock(&svc_xprt_class_lock);
2898c2ecf20Sopenharmony_ci		newxprt = __svc_xpo_create(xcl, serv, net, family, port, flags);
2908c2ecf20Sopenharmony_ci		if (IS_ERR(newxprt)) {
2918c2ecf20Sopenharmony_ci			module_put(xcl->xcl_owner);
2928c2ecf20Sopenharmony_ci			return PTR_ERR(newxprt);
2938c2ecf20Sopenharmony_ci		}
2948c2ecf20Sopenharmony_ci		newxprt->xpt_cred = get_cred(cred);
2958c2ecf20Sopenharmony_ci		svc_add_new_perm_xprt(serv, newxprt);
2968c2ecf20Sopenharmony_ci		newport = svc_xprt_local_port(newxprt);
2978c2ecf20Sopenharmony_ci		return newport;
2988c2ecf20Sopenharmony_ci	}
2998c2ecf20Sopenharmony_ci err:
3008c2ecf20Sopenharmony_ci	spin_unlock(&svc_xprt_class_lock);
3018c2ecf20Sopenharmony_ci	/* This errno is exposed to user space.  Provide a reasonable
3028c2ecf20Sopenharmony_ci	 * perror msg for a bad transport. */
3038c2ecf20Sopenharmony_ci	return -EPROTONOSUPPORT;
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ciint svc_create_xprt(struct svc_serv *serv, const char *xprt_name,
3078c2ecf20Sopenharmony_ci		    struct net *net, const int family,
3088c2ecf20Sopenharmony_ci		    const unsigned short port, int flags,
3098c2ecf20Sopenharmony_ci		    const struct cred *cred)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	int err;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	err = _svc_create_xprt(serv, xprt_name, net, family, port, flags, cred);
3148c2ecf20Sopenharmony_ci	if (err == -EPROTONOSUPPORT) {
3158c2ecf20Sopenharmony_ci		request_module("svc%s", xprt_name);
3168c2ecf20Sopenharmony_ci		err = _svc_create_xprt(serv, xprt_name, net, family, port, flags, cred);
3178c2ecf20Sopenharmony_ci	}
3188c2ecf20Sopenharmony_ci	return err;
3198c2ecf20Sopenharmony_ci}
3208c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_create_xprt);
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci/*
3238c2ecf20Sopenharmony_ci * Copy the local and remote xprt addresses to the rqstp structure
3248c2ecf20Sopenharmony_ci */
3258c2ecf20Sopenharmony_civoid svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt)
3268c2ecf20Sopenharmony_ci{
3278c2ecf20Sopenharmony_ci	memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen);
3288c2ecf20Sopenharmony_ci	rqstp->rq_addrlen = xprt->xpt_remotelen;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	/*
3318c2ecf20Sopenharmony_ci	 * Destination address in request is needed for binding the
3328c2ecf20Sopenharmony_ci	 * source address in RPC replies/callbacks later.
3338c2ecf20Sopenharmony_ci	 */
3348c2ecf20Sopenharmony_ci	memcpy(&rqstp->rq_daddr, &xprt->xpt_local, xprt->xpt_locallen);
3358c2ecf20Sopenharmony_ci	rqstp->rq_daddrlen = xprt->xpt_locallen;
3368c2ecf20Sopenharmony_ci}
3378c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_xprt_copy_addrs);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci/**
3408c2ecf20Sopenharmony_ci * svc_print_addr - Format rq_addr field for printing
3418c2ecf20Sopenharmony_ci * @rqstp: svc_rqst struct containing address to print
3428c2ecf20Sopenharmony_ci * @buf: target buffer for formatted address
3438c2ecf20Sopenharmony_ci * @len: length of target buffer
3448c2ecf20Sopenharmony_ci *
3458c2ecf20Sopenharmony_ci */
3468c2ecf20Sopenharmony_cichar *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	return __svc_print_addr(svc_addr(rqstp), buf, len);
3498c2ecf20Sopenharmony_ci}
3508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_print_addr);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_cistatic bool svc_xprt_slots_in_range(struct svc_xprt *xprt)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	unsigned int limit = svc_rpc_per_connection_limit;
3558c2ecf20Sopenharmony_ci	int nrqsts = atomic_read(&xprt->xpt_nr_rqsts);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	return limit == 0 || (nrqsts >= 0 && nrqsts < limit);
3588c2ecf20Sopenharmony_ci}
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_cistatic bool svc_xprt_reserve_slot(struct svc_rqst *rqstp, struct svc_xprt *xprt)
3618c2ecf20Sopenharmony_ci{
3628c2ecf20Sopenharmony_ci	if (!test_bit(RQ_DATA, &rqstp->rq_flags)) {
3638c2ecf20Sopenharmony_ci		if (!svc_xprt_slots_in_range(xprt))
3648c2ecf20Sopenharmony_ci			return false;
3658c2ecf20Sopenharmony_ci		atomic_inc(&xprt->xpt_nr_rqsts);
3668c2ecf20Sopenharmony_ci		set_bit(RQ_DATA, &rqstp->rq_flags);
3678c2ecf20Sopenharmony_ci	}
3688c2ecf20Sopenharmony_ci	return true;
3698c2ecf20Sopenharmony_ci}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic void svc_xprt_release_slot(struct svc_rqst *rqstp)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	struct svc_xprt	*xprt = rqstp->rq_xprt;
3748c2ecf20Sopenharmony_ci	if (test_and_clear_bit(RQ_DATA, &rqstp->rq_flags)) {
3758c2ecf20Sopenharmony_ci		atomic_dec(&xprt->xpt_nr_rqsts);
3768c2ecf20Sopenharmony_ci		smp_wmb(); /* See smp_rmb() in svc_xprt_ready() */
3778c2ecf20Sopenharmony_ci		svc_xprt_enqueue(xprt);
3788c2ecf20Sopenharmony_ci	}
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_cistatic bool svc_xprt_ready(struct svc_xprt *xprt)
3828c2ecf20Sopenharmony_ci{
3838c2ecf20Sopenharmony_ci	unsigned long xpt_flags;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	/*
3868c2ecf20Sopenharmony_ci	 * If another cpu has recently updated xpt_flags,
3878c2ecf20Sopenharmony_ci	 * sk_sock->flags, xpt_reserved, or xpt_nr_rqsts, we need to
3888c2ecf20Sopenharmony_ci	 * know about it; otherwise it's possible that both that cpu and
3898c2ecf20Sopenharmony_ci	 * this one could call svc_xprt_enqueue() without either
3908c2ecf20Sopenharmony_ci	 * svc_xprt_enqueue() recognizing that the conditions below
3918c2ecf20Sopenharmony_ci	 * are satisfied, and we could stall indefinitely:
3928c2ecf20Sopenharmony_ci	 */
3938c2ecf20Sopenharmony_ci	smp_rmb();
3948c2ecf20Sopenharmony_ci	xpt_flags = READ_ONCE(xprt->xpt_flags);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	if (xpt_flags & (BIT(XPT_CONN) | BIT(XPT_CLOSE)))
3978c2ecf20Sopenharmony_ci		return true;
3988c2ecf20Sopenharmony_ci	if (xpt_flags & (BIT(XPT_DATA) | BIT(XPT_DEFERRED))) {
3998c2ecf20Sopenharmony_ci		if (xprt->xpt_ops->xpo_has_wspace(xprt) &&
4008c2ecf20Sopenharmony_ci		    svc_xprt_slots_in_range(xprt))
4018c2ecf20Sopenharmony_ci			return true;
4028c2ecf20Sopenharmony_ci		trace_svc_xprt_no_write_space(xprt);
4038c2ecf20Sopenharmony_ci		return false;
4048c2ecf20Sopenharmony_ci	}
4058c2ecf20Sopenharmony_ci	return false;
4068c2ecf20Sopenharmony_ci}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_civoid svc_xprt_do_enqueue(struct svc_xprt *xprt)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	struct svc_pool *pool;
4118c2ecf20Sopenharmony_ci	struct svc_rqst	*rqstp = NULL;
4128c2ecf20Sopenharmony_ci	int cpu;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	if (!svc_xprt_ready(xprt))
4158c2ecf20Sopenharmony_ci		return;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	/* Mark transport as busy. It will remain in this state until
4188c2ecf20Sopenharmony_ci	 * the provider calls svc_xprt_received. We update XPT_BUSY
4198c2ecf20Sopenharmony_ci	 * atomically because it also guards against trying to enqueue
4208c2ecf20Sopenharmony_ci	 * the transport twice.
4218c2ecf20Sopenharmony_ci	 */
4228c2ecf20Sopenharmony_ci	if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
4238c2ecf20Sopenharmony_ci		return;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	cpu = get_cpu();
4268c2ecf20Sopenharmony_ci	pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	atomic_long_inc(&pool->sp_stats.packets);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	spin_lock_bh(&pool->sp_lock);
4318c2ecf20Sopenharmony_ci	list_add_tail(&xprt->xpt_ready, &pool->sp_sockets);
4328c2ecf20Sopenharmony_ci	pool->sp_stats.sockets_queued++;
4338c2ecf20Sopenharmony_ci	spin_unlock_bh(&pool->sp_lock);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	/* find a thread for this xprt */
4368c2ecf20Sopenharmony_ci	rcu_read_lock();
4378c2ecf20Sopenharmony_ci	list_for_each_entry_rcu(rqstp, &pool->sp_all_threads, rq_all) {
4388c2ecf20Sopenharmony_ci		if (test_and_set_bit(RQ_BUSY, &rqstp->rq_flags))
4398c2ecf20Sopenharmony_ci			continue;
4408c2ecf20Sopenharmony_ci		atomic_long_inc(&pool->sp_stats.threads_woken);
4418c2ecf20Sopenharmony_ci		rqstp->rq_qtime = ktime_get();
4428c2ecf20Sopenharmony_ci		wake_up_process(rqstp->rq_task);
4438c2ecf20Sopenharmony_ci		goto out_unlock;
4448c2ecf20Sopenharmony_ci	}
4458c2ecf20Sopenharmony_ci	set_bit(SP_CONGESTED, &pool->sp_flags);
4468c2ecf20Sopenharmony_ci	rqstp = NULL;
4478c2ecf20Sopenharmony_ciout_unlock:
4488c2ecf20Sopenharmony_ci	rcu_read_unlock();
4498c2ecf20Sopenharmony_ci	put_cpu();
4508c2ecf20Sopenharmony_ci	trace_svc_xprt_do_enqueue(xprt, rqstp);
4518c2ecf20Sopenharmony_ci}
4528c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_xprt_do_enqueue);
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci/*
4558c2ecf20Sopenharmony_ci * Queue up a transport with data pending. If there are idle nfsd
4568c2ecf20Sopenharmony_ci * processes, wake 'em up.
4578c2ecf20Sopenharmony_ci *
4588c2ecf20Sopenharmony_ci */
4598c2ecf20Sopenharmony_civoid svc_xprt_enqueue(struct svc_xprt *xprt)
4608c2ecf20Sopenharmony_ci{
4618c2ecf20Sopenharmony_ci	if (test_bit(XPT_BUSY, &xprt->xpt_flags))
4628c2ecf20Sopenharmony_ci		return;
4638c2ecf20Sopenharmony_ci	xprt->xpt_server->sv_ops->svo_enqueue_xprt(xprt);
4648c2ecf20Sopenharmony_ci}
4658c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_xprt_enqueue);
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci/*
4688c2ecf20Sopenharmony_ci * Dequeue the first transport, if there is one.
4698c2ecf20Sopenharmony_ci */
4708c2ecf20Sopenharmony_cistatic struct svc_xprt *svc_xprt_dequeue(struct svc_pool *pool)
4718c2ecf20Sopenharmony_ci{
4728c2ecf20Sopenharmony_ci	struct svc_xprt	*xprt = NULL;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	if (list_empty(&pool->sp_sockets))
4758c2ecf20Sopenharmony_ci		goto out;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	spin_lock_bh(&pool->sp_lock);
4788c2ecf20Sopenharmony_ci	if (likely(!list_empty(&pool->sp_sockets))) {
4798c2ecf20Sopenharmony_ci		xprt = list_first_entry(&pool->sp_sockets,
4808c2ecf20Sopenharmony_ci					struct svc_xprt, xpt_ready);
4818c2ecf20Sopenharmony_ci		list_del_init(&xprt->xpt_ready);
4828c2ecf20Sopenharmony_ci		svc_xprt_get(xprt);
4838c2ecf20Sopenharmony_ci	}
4848c2ecf20Sopenharmony_ci	spin_unlock_bh(&pool->sp_lock);
4858c2ecf20Sopenharmony_ciout:
4868c2ecf20Sopenharmony_ci	return xprt;
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci/**
4908c2ecf20Sopenharmony_ci * svc_reserve - change the space reserved for the reply to a request.
4918c2ecf20Sopenharmony_ci * @rqstp:  The request in question
4928c2ecf20Sopenharmony_ci * @space: new max space to reserve
4938c2ecf20Sopenharmony_ci *
4948c2ecf20Sopenharmony_ci * Each request reserves some space on the output queue of the transport
4958c2ecf20Sopenharmony_ci * to make sure the reply fits.  This function reduces that reserved
4968c2ecf20Sopenharmony_ci * space to be the amount of space used already, plus @space.
4978c2ecf20Sopenharmony_ci *
4988c2ecf20Sopenharmony_ci */
4998c2ecf20Sopenharmony_civoid svc_reserve(struct svc_rqst *rqstp, int space)
5008c2ecf20Sopenharmony_ci{
5018c2ecf20Sopenharmony_ci	struct svc_xprt *xprt = rqstp->rq_xprt;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	space += rqstp->rq_res.head[0].iov_len;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	if (xprt && space < rqstp->rq_reserved) {
5068c2ecf20Sopenharmony_ci		atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved);
5078c2ecf20Sopenharmony_ci		rqstp->rq_reserved = space;
5088c2ecf20Sopenharmony_ci		smp_wmb(); /* See smp_rmb() in svc_xprt_ready() */
5098c2ecf20Sopenharmony_ci		svc_xprt_enqueue(xprt);
5108c2ecf20Sopenharmony_ci	}
5118c2ecf20Sopenharmony_ci}
5128c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_reserve);
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_cistatic void svc_xprt_release(struct svc_rqst *rqstp)
5158c2ecf20Sopenharmony_ci{
5168c2ecf20Sopenharmony_ci	struct svc_xprt	*xprt = rqstp->rq_xprt;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	xprt->xpt_ops->xpo_release_rqst(rqstp);
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	kfree(rqstp->rq_deferred);
5218c2ecf20Sopenharmony_ci	rqstp->rq_deferred = NULL;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	svc_free_res_pages(rqstp);
5248c2ecf20Sopenharmony_ci	rqstp->rq_res.page_len = 0;
5258c2ecf20Sopenharmony_ci	rqstp->rq_res.page_base = 0;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	/* Reset response buffer and release
5288c2ecf20Sopenharmony_ci	 * the reservation.
5298c2ecf20Sopenharmony_ci	 * But first, check that enough space was reserved
5308c2ecf20Sopenharmony_ci	 * for the reply, otherwise we have a bug!
5318c2ecf20Sopenharmony_ci	 */
5328c2ecf20Sopenharmony_ci	if ((rqstp->rq_res.len) >  rqstp->rq_reserved)
5338c2ecf20Sopenharmony_ci		printk(KERN_ERR "RPC request reserved %d but used %d\n",
5348c2ecf20Sopenharmony_ci		       rqstp->rq_reserved,
5358c2ecf20Sopenharmony_ci		       rqstp->rq_res.len);
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	rqstp->rq_res.head[0].iov_len = 0;
5388c2ecf20Sopenharmony_ci	svc_reserve(rqstp, 0);
5398c2ecf20Sopenharmony_ci	svc_xprt_release_slot(rqstp);
5408c2ecf20Sopenharmony_ci	rqstp->rq_xprt = NULL;
5418c2ecf20Sopenharmony_ci	svc_xprt_put(xprt);
5428c2ecf20Sopenharmony_ci}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci/*
5458c2ecf20Sopenharmony_ci * Some svc_serv's will have occasional work to do, even when a xprt is not
5468c2ecf20Sopenharmony_ci * waiting to be serviced. This function is there to "kick" a task in one of
5478c2ecf20Sopenharmony_ci * those services so that it can wake up and do that work. Note that we only
5488c2ecf20Sopenharmony_ci * bother with pool 0 as we don't need to wake up more than one thread for
5498c2ecf20Sopenharmony_ci * this purpose.
5508c2ecf20Sopenharmony_ci */
5518c2ecf20Sopenharmony_civoid svc_wake_up(struct svc_serv *serv)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	struct svc_rqst	*rqstp;
5548c2ecf20Sopenharmony_ci	struct svc_pool *pool;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	pool = &serv->sv_pools[0];
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	rcu_read_lock();
5598c2ecf20Sopenharmony_ci	list_for_each_entry_rcu(rqstp, &pool->sp_all_threads, rq_all) {
5608c2ecf20Sopenharmony_ci		/* skip any that aren't queued */
5618c2ecf20Sopenharmony_ci		if (test_bit(RQ_BUSY, &rqstp->rq_flags))
5628c2ecf20Sopenharmony_ci			continue;
5638c2ecf20Sopenharmony_ci		rcu_read_unlock();
5648c2ecf20Sopenharmony_ci		wake_up_process(rqstp->rq_task);
5658c2ecf20Sopenharmony_ci		trace_svc_wake_up(rqstp->rq_task->pid);
5668c2ecf20Sopenharmony_ci		return;
5678c2ecf20Sopenharmony_ci	}
5688c2ecf20Sopenharmony_ci	rcu_read_unlock();
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	/* No free entries available */
5718c2ecf20Sopenharmony_ci	set_bit(SP_TASK_PENDING, &pool->sp_flags);
5728c2ecf20Sopenharmony_ci	smp_wmb();
5738c2ecf20Sopenharmony_ci	trace_svc_wake_up(0);
5748c2ecf20Sopenharmony_ci}
5758c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_wake_up);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ciint svc_port_is_privileged(struct sockaddr *sin)
5788c2ecf20Sopenharmony_ci{
5798c2ecf20Sopenharmony_ci	switch (sin->sa_family) {
5808c2ecf20Sopenharmony_ci	case AF_INET:
5818c2ecf20Sopenharmony_ci		return ntohs(((struct sockaddr_in *)sin)->sin_port)
5828c2ecf20Sopenharmony_ci			< PROT_SOCK;
5838c2ecf20Sopenharmony_ci	case AF_INET6:
5848c2ecf20Sopenharmony_ci		return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
5858c2ecf20Sopenharmony_ci			< PROT_SOCK;
5868c2ecf20Sopenharmony_ci	default:
5878c2ecf20Sopenharmony_ci		return 0;
5888c2ecf20Sopenharmony_ci	}
5898c2ecf20Sopenharmony_ci}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci/*
5928c2ecf20Sopenharmony_ci * Make sure that we don't have too many active connections. If we have,
5938c2ecf20Sopenharmony_ci * something must be dropped. It's not clear what will happen if we allow
5948c2ecf20Sopenharmony_ci * "too many" connections, but when dealing with network-facing software,
5958c2ecf20Sopenharmony_ci * we have to code defensively. Here we do that by imposing hard limits.
5968c2ecf20Sopenharmony_ci *
5978c2ecf20Sopenharmony_ci * There's no point in trying to do random drop here for DoS
5988c2ecf20Sopenharmony_ci * prevention. The NFS clients does 1 reconnect in 15 seconds. An
5998c2ecf20Sopenharmony_ci * attacker can easily beat that.
6008c2ecf20Sopenharmony_ci *
6018c2ecf20Sopenharmony_ci * The only somewhat efficient mechanism would be if drop old
6028c2ecf20Sopenharmony_ci * connections from the same IP first. But right now we don't even
6038c2ecf20Sopenharmony_ci * record the client IP in svc_sock.
6048c2ecf20Sopenharmony_ci *
6058c2ecf20Sopenharmony_ci * single-threaded services that expect a lot of clients will probably
6068c2ecf20Sopenharmony_ci * need to set sv_maxconn to override the default value which is based
6078c2ecf20Sopenharmony_ci * on the number of threads
6088c2ecf20Sopenharmony_ci */
6098c2ecf20Sopenharmony_cistatic void svc_check_conn_limits(struct svc_serv *serv)
6108c2ecf20Sopenharmony_ci{
6118c2ecf20Sopenharmony_ci	unsigned int limit = serv->sv_maxconn ? serv->sv_maxconn :
6128c2ecf20Sopenharmony_ci				(serv->sv_nrthreads+3) * 20;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	if (serv->sv_tmpcnt > limit) {
6158c2ecf20Sopenharmony_ci		struct svc_xprt *xprt = NULL;
6168c2ecf20Sopenharmony_ci		spin_lock_bh(&serv->sv_lock);
6178c2ecf20Sopenharmony_ci		if (!list_empty(&serv->sv_tempsocks)) {
6188c2ecf20Sopenharmony_ci			/* Try to help the admin */
6198c2ecf20Sopenharmony_ci			net_notice_ratelimited("%s: too many open connections, consider increasing the %s\n",
6208c2ecf20Sopenharmony_ci					       serv->sv_name, serv->sv_maxconn ?
6218c2ecf20Sopenharmony_ci					       "max number of connections" :
6228c2ecf20Sopenharmony_ci					       "number of threads");
6238c2ecf20Sopenharmony_ci			/*
6248c2ecf20Sopenharmony_ci			 * Always select the oldest connection. It's not fair,
6258c2ecf20Sopenharmony_ci			 * but so is life
6268c2ecf20Sopenharmony_ci			 */
6278c2ecf20Sopenharmony_ci			xprt = list_entry(serv->sv_tempsocks.prev,
6288c2ecf20Sopenharmony_ci					  struct svc_xprt,
6298c2ecf20Sopenharmony_ci					  xpt_list);
6308c2ecf20Sopenharmony_ci			set_bit(XPT_CLOSE, &xprt->xpt_flags);
6318c2ecf20Sopenharmony_ci			svc_xprt_get(xprt);
6328c2ecf20Sopenharmony_ci		}
6338c2ecf20Sopenharmony_ci		spin_unlock_bh(&serv->sv_lock);
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci		if (xprt) {
6368c2ecf20Sopenharmony_ci			svc_xprt_enqueue(xprt);
6378c2ecf20Sopenharmony_ci			svc_xprt_put(xprt);
6388c2ecf20Sopenharmony_ci		}
6398c2ecf20Sopenharmony_ci	}
6408c2ecf20Sopenharmony_ci}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_cistatic int svc_alloc_arg(struct svc_rqst *rqstp)
6438c2ecf20Sopenharmony_ci{
6448c2ecf20Sopenharmony_ci	struct svc_serv *serv = rqstp->rq_server;
6458c2ecf20Sopenharmony_ci	struct xdr_buf *arg;
6468c2ecf20Sopenharmony_ci	int pages;
6478c2ecf20Sopenharmony_ci	int i;
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	/* now allocate needed pages.  If we get a failure, sleep briefly */
6508c2ecf20Sopenharmony_ci	pages = (serv->sv_max_mesg + 2 * PAGE_SIZE) >> PAGE_SHIFT;
6518c2ecf20Sopenharmony_ci	if (pages > RPCSVC_MAXPAGES) {
6528c2ecf20Sopenharmony_ci		pr_warn_once("svc: warning: pages=%u > RPCSVC_MAXPAGES=%lu\n",
6538c2ecf20Sopenharmony_ci			     pages, RPCSVC_MAXPAGES);
6548c2ecf20Sopenharmony_ci		/* use as many pages as possible */
6558c2ecf20Sopenharmony_ci		pages = RPCSVC_MAXPAGES;
6568c2ecf20Sopenharmony_ci	}
6578c2ecf20Sopenharmony_ci	for (i = 0; i < pages ; i++)
6588c2ecf20Sopenharmony_ci		while (rqstp->rq_pages[i] == NULL) {
6598c2ecf20Sopenharmony_ci			struct page *p = alloc_page(GFP_KERNEL);
6608c2ecf20Sopenharmony_ci			if (!p) {
6618c2ecf20Sopenharmony_ci				set_current_state(TASK_INTERRUPTIBLE);
6628c2ecf20Sopenharmony_ci				if (signalled() || kthread_should_stop()) {
6638c2ecf20Sopenharmony_ci					set_current_state(TASK_RUNNING);
6648c2ecf20Sopenharmony_ci					return -EINTR;
6658c2ecf20Sopenharmony_ci				}
6668c2ecf20Sopenharmony_ci				schedule_timeout(msecs_to_jiffies(500));
6678c2ecf20Sopenharmony_ci			}
6688c2ecf20Sopenharmony_ci			rqstp->rq_pages[i] = p;
6698c2ecf20Sopenharmony_ci		}
6708c2ecf20Sopenharmony_ci	rqstp->rq_page_end = &rqstp->rq_pages[i];
6718c2ecf20Sopenharmony_ci	rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	/* Make arg->head point to first page and arg->pages point to rest */
6748c2ecf20Sopenharmony_ci	arg = &rqstp->rq_arg;
6758c2ecf20Sopenharmony_ci	arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
6768c2ecf20Sopenharmony_ci	arg->head[0].iov_len = PAGE_SIZE;
6778c2ecf20Sopenharmony_ci	arg->pages = rqstp->rq_pages + 1;
6788c2ecf20Sopenharmony_ci	arg->page_base = 0;
6798c2ecf20Sopenharmony_ci	/* save at least one page for response */
6808c2ecf20Sopenharmony_ci	arg->page_len = (pages-2)*PAGE_SIZE;
6818c2ecf20Sopenharmony_ci	arg->len = (pages-1)*PAGE_SIZE;
6828c2ecf20Sopenharmony_ci	arg->tail[0].iov_len = 0;
6838c2ecf20Sopenharmony_ci	return 0;
6848c2ecf20Sopenharmony_ci}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_cistatic bool
6878c2ecf20Sopenharmony_cirqst_should_sleep(struct svc_rqst *rqstp)
6888c2ecf20Sopenharmony_ci{
6898c2ecf20Sopenharmony_ci	struct svc_pool		*pool = rqstp->rq_pool;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	/* did someone call svc_wake_up? */
6928c2ecf20Sopenharmony_ci	if (test_and_clear_bit(SP_TASK_PENDING, &pool->sp_flags))
6938c2ecf20Sopenharmony_ci		return false;
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci	/* was a socket queued? */
6968c2ecf20Sopenharmony_ci	if (!list_empty(&pool->sp_sockets))
6978c2ecf20Sopenharmony_ci		return false;
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	/* are we shutting down? */
7008c2ecf20Sopenharmony_ci	if (signalled() || kthread_should_stop())
7018c2ecf20Sopenharmony_ci		return false;
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	/* are we freezing? */
7048c2ecf20Sopenharmony_ci	if (freezing(current))
7058c2ecf20Sopenharmony_ci		return false;
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	return true;
7088c2ecf20Sopenharmony_ci}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_cistatic struct svc_xprt *svc_get_next_xprt(struct svc_rqst *rqstp, long timeout)
7118c2ecf20Sopenharmony_ci{
7128c2ecf20Sopenharmony_ci	struct svc_pool		*pool = rqstp->rq_pool;
7138c2ecf20Sopenharmony_ci	long			time_left = 0;
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	/* rq_xprt should be clear on entry */
7168c2ecf20Sopenharmony_ci	WARN_ON_ONCE(rqstp->rq_xprt);
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	rqstp->rq_xprt = svc_xprt_dequeue(pool);
7198c2ecf20Sopenharmony_ci	if (rqstp->rq_xprt)
7208c2ecf20Sopenharmony_ci		goto out_found;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	/*
7238c2ecf20Sopenharmony_ci	 * We have to be able to interrupt this wait
7248c2ecf20Sopenharmony_ci	 * to bring down the daemons ...
7258c2ecf20Sopenharmony_ci	 */
7268c2ecf20Sopenharmony_ci	set_current_state(TASK_INTERRUPTIBLE);
7278c2ecf20Sopenharmony_ci	smp_mb__before_atomic();
7288c2ecf20Sopenharmony_ci	clear_bit(SP_CONGESTED, &pool->sp_flags);
7298c2ecf20Sopenharmony_ci	clear_bit(RQ_BUSY, &rqstp->rq_flags);
7308c2ecf20Sopenharmony_ci	smp_mb__after_atomic();
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	if (likely(rqst_should_sleep(rqstp)))
7338c2ecf20Sopenharmony_ci		time_left = schedule_timeout(timeout);
7348c2ecf20Sopenharmony_ci	else
7358c2ecf20Sopenharmony_ci		__set_current_state(TASK_RUNNING);
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	try_to_freeze();
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	set_bit(RQ_BUSY, &rqstp->rq_flags);
7408c2ecf20Sopenharmony_ci	smp_mb__after_atomic();
7418c2ecf20Sopenharmony_ci	rqstp->rq_xprt = svc_xprt_dequeue(pool);
7428c2ecf20Sopenharmony_ci	if (rqstp->rq_xprt)
7438c2ecf20Sopenharmony_ci		goto out_found;
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci	if (!time_left)
7468c2ecf20Sopenharmony_ci		atomic_long_inc(&pool->sp_stats.threads_timedout);
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ci	if (signalled() || kthread_should_stop())
7498c2ecf20Sopenharmony_ci		return ERR_PTR(-EINTR);
7508c2ecf20Sopenharmony_ci	return ERR_PTR(-EAGAIN);
7518c2ecf20Sopenharmony_ciout_found:
7528c2ecf20Sopenharmony_ci	/* Normally we will wait up to 5 seconds for any required
7538c2ecf20Sopenharmony_ci	 * cache information to be provided.
7548c2ecf20Sopenharmony_ci	 */
7558c2ecf20Sopenharmony_ci	if (!test_bit(SP_CONGESTED, &pool->sp_flags))
7568c2ecf20Sopenharmony_ci		rqstp->rq_chandle.thread_wait = 5*HZ;
7578c2ecf20Sopenharmony_ci	else
7588c2ecf20Sopenharmony_ci		rqstp->rq_chandle.thread_wait = 1*HZ;
7598c2ecf20Sopenharmony_ci	trace_svc_xprt_dequeue(rqstp);
7608c2ecf20Sopenharmony_ci	return rqstp->rq_xprt;
7618c2ecf20Sopenharmony_ci}
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_cistatic void svc_add_new_temp_xprt(struct svc_serv *serv, struct svc_xprt *newxpt)
7648c2ecf20Sopenharmony_ci{
7658c2ecf20Sopenharmony_ci	spin_lock_bh(&serv->sv_lock);
7668c2ecf20Sopenharmony_ci	set_bit(XPT_TEMP, &newxpt->xpt_flags);
7678c2ecf20Sopenharmony_ci	list_add(&newxpt->xpt_list, &serv->sv_tempsocks);
7688c2ecf20Sopenharmony_ci	serv->sv_tmpcnt++;
7698c2ecf20Sopenharmony_ci	if (serv->sv_temptimer.function == NULL) {
7708c2ecf20Sopenharmony_ci		/* setup timer to age temp transports */
7718c2ecf20Sopenharmony_ci		serv->sv_temptimer.function = svc_age_temp_xprts;
7728c2ecf20Sopenharmony_ci		mod_timer(&serv->sv_temptimer,
7738c2ecf20Sopenharmony_ci			  jiffies + svc_conn_age_period * HZ);
7748c2ecf20Sopenharmony_ci	}
7758c2ecf20Sopenharmony_ci	spin_unlock_bh(&serv->sv_lock);
7768c2ecf20Sopenharmony_ci	svc_xprt_received(newxpt);
7778c2ecf20Sopenharmony_ci}
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_cistatic int svc_handle_xprt(struct svc_rqst *rqstp, struct svc_xprt *xprt)
7808c2ecf20Sopenharmony_ci{
7818c2ecf20Sopenharmony_ci	struct svc_serv *serv = rqstp->rq_server;
7828c2ecf20Sopenharmony_ci	int len = 0;
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
7858c2ecf20Sopenharmony_ci		if (test_and_clear_bit(XPT_KILL_TEMP, &xprt->xpt_flags))
7868c2ecf20Sopenharmony_ci			xprt->xpt_ops->xpo_kill_temp_xprt(xprt);
7878c2ecf20Sopenharmony_ci		svc_delete_xprt(xprt);
7888c2ecf20Sopenharmony_ci		/* Leave XPT_BUSY set on the dead xprt: */
7898c2ecf20Sopenharmony_ci		goto out;
7908c2ecf20Sopenharmony_ci	}
7918c2ecf20Sopenharmony_ci	if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
7928c2ecf20Sopenharmony_ci		struct svc_xprt *newxpt;
7938c2ecf20Sopenharmony_ci		/*
7948c2ecf20Sopenharmony_ci		 * We know this module_get will succeed because the
7958c2ecf20Sopenharmony_ci		 * listener holds a reference too
7968c2ecf20Sopenharmony_ci		 */
7978c2ecf20Sopenharmony_ci		__module_get(xprt->xpt_class->xcl_owner);
7988c2ecf20Sopenharmony_ci		svc_check_conn_limits(xprt->xpt_server);
7998c2ecf20Sopenharmony_ci		newxpt = xprt->xpt_ops->xpo_accept(xprt);
8008c2ecf20Sopenharmony_ci		if (newxpt) {
8018c2ecf20Sopenharmony_ci			newxpt->xpt_cred = get_cred(xprt->xpt_cred);
8028c2ecf20Sopenharmony_ci			svc_add_new_temp_xprt(serv, newxpt);
8038c2ecf20Sopenharmony_ci			trace_svc_xprt_accept(newxpt, serv->sv_name);
8048c2ecf20Sopenharmony_ci		} else
8058c2ecf20Sopenharmony_ci			module_put(xprt->xpt_class->xcl_owner);
8068c2ecf20Sopenharmony_ci	} else if (svc_xprt_reserve_slot(rqstp, xprt)) {
8078c2ecf20Sopenharmony_ci		/* XPT_DATA|XPT_DEFERRED case: */
8088c2ecf20Sopenharmony_ci		dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
8098c2ecf20Sopenharmony_ci			rqstp, rqstp->rq_pool->sp_id, xprt,
8108c2ecf20Sopenharmony_ci			kref_read(&xprt->xpt_ref));
8118c2ecf20Sopenharmony_ci		rqstp->rq_deferred = svc_deferred_dequeue(xprt);
8128c2ecf20Sopenharmony_ci		if (rqstp->rq_deferred)
8138c2ecf20Sopenharmony_ci			len = svc_deferred_recv(rqstp);
8148c2ecf20Sopenharmony_ci		else
8158c2ecf20Sopenharmony_ci			len = xprt->xpt_ops->xpo_recvfrom(rqstp);
8168c2ecf20Sopenharmony_ci		if (len > 0)
8178c2ecf20Sopenharmony_ci			trace_svc_xdr_recvfrom(rqstp, &rqstp->rq_arg);
8188c2ecf20Sopenharmony_ci		rqstp->rq_stime = ktime_get();
8198c2ecf20Sopenharmony_ci		rqstp->rq_reserved = serv->sv_max_mesg;
8208c2ecf20Sopenharmony_ci		atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
8218c2ecf20Sopenharmony_ci	}
8228c2ecf20Sopenharmony_ci	/* clear XPT_BUSY: */
8238c2ecf20Sopenharmony_ci	svc_xprt_received(xprt);
8248c2ecf20Sopenharmony_ciout:
8258c2ecf20Sopenharmony_ci	trace_svc_handle_xprt(xprt, len);
8268c2ecf20Sopenharmony_ci	return len;
8278c2ecf20Sopenharmony_ci}
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci/*
8308c2ecf20Sopenharmony_ci * Receive the next request on any transport.  This code is carefully
8318c2ecf20Sopenharmony_ci * organised not to touch any cachelines in the shared svc_serv
8328c2ecf20Sopenharmony_ci * structure, only cachelines in the local svc_pool.
8338c2ecf20Sopenharmony_ci */
8348c2ecf20Sopenharmony_ciint svc_recv(struct svc_rqst *rqstp, long timeout)
8358c2ecf20Sopenharmony_ci{
8368c2ecf20Sopenharmony_ci	struct svc_xprt		*xprt = NULL;
8378c2ecf20Sopenharmony_ci	struct svc_serv		*serv = rqstp->rq_server;
8388c2ecf20Sopenharmony_ci	int			len, err;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	err = svc_alloc_arg(rqstp);
8418c2ecf20Sopenharmony_ci	if (err)
8428c2ecf20Sopenharmony_ci		goto out;
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	try_to_freeze();
8458c2ecf20Sopenharmony_ci	cond_resched();
8468c2ecf20Sopenharmony_ci	err = -EINTR;
8478c2ecf20Sopenharmony_ci	if (signalled() || kthread_should_stop())
8488c2ecf20Sopenharmony_ci		goto out;
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	xprt = svc_get_next_xprt(rqstp, timeout);
8518c2ecf20Sopenharmony_ci	if (IS_ERR(xprt)) {
8528c2ecf20Sopenharmony_ci		err = PTR_ERR(xprt);
8538c2ecf20Sopenharmony_ci		goto out;
8548c2ecf20Sopenharmony_ci	}
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	len = svc_handle_xprt(rqstp, xprt);
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	/* No data, incomplete (TCP) read, or accept() */
8598c2ecf20Sopenharmony_ci	err = -EAGAIN;
8608c2ecf20Sopenharmony_ci	if (len <= 0)
8618c2ecf20Sopenharmony_ci		goto out_release;
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	clear_bit(XPT_OLD, &xprt->xpt_flags);
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	xprt->xpt_ops->xpo_secure_port(rqstp);
8668c2ecf20Sopenharmony_ci	rqstp->rq_chandle.defer = svc_defer;
8678c2ecf20Sopenharmony_ci	rqstp->rq_xid = svc_getu32(&rqstp->rq_arg.head[0]);
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	if (serv->sv_stats)
8708c2ecf20Sopenharmony_ci		serv->sv_stats->netcnt++;
8718c2ecf20Sopenharmony_ci	trace_svc_recv(rqstp, len);
8728c2ecf20Sopenharmony_ci	return len;
8738c2ecf20Sopenharmony_ciout_release:
8748c2ecf20Sopenharmony_ci	rqstp->rq_res.len = 0;
8758c2ecf20Sopenharmony_ci	svc_xprt_release(rqstp);
8768c2ecf20Sopenharmony_ciout:
8778c2ecf20Sopenharmony_ci	return err;
8788c2ecf20Sopenharmony_ci}
8798c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_recv);
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci/*
8828c2ecf20Sopenharmony_ci * Drop request
8838c2ecf20Sopenharmony_ci */
8848c2ecf20Sopenharmony_civoid svc_drop(struct svc_rqst *rqstp)
8858c2ecf20Sopenharmony_ci{
8868c2ecf20Sopenharmony_ci	trace_svc_drop(rqstp);
8878c2ecf20Sopenharmony_ci	svc_xprt_release(rqstp);
8888c2ecf20Sopenharmony_ci}
8898c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_drop);
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci/*
8928c2ecf20Sopenharmony_ci * Return reply to client.
8938c2ecf20Sopenharmony_ci */
8948c2ecf20Sopenharmony_ciint svc_send(struct svc_rqst *rqstp)
8958c2ecf20Sopenharmony_ci{
8968c2ecf20Sopenharmony_ci	struct svc_xprt	*xprt;
8978c2ecf20Sopenharmony_ci	int		len = -EFAULT;
8988c2ecf20Sopenharmony_ci	struct xdr_buf	*xb;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	xprt = rqstp->rq_xprt;
9018c2ecf20Sopenharmony_ci	if (!xprt)
9028c2ecf20Sopenharmony_ci		goto out;
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	/* calculate over-all length */
9058c2ecf20Sopenharmony_ci	xb = &rqstp->rq_res;
9068c2ecf20Sopenharmony_ci	xb->len = xb->head[0].iov_len +
9078c2ecf20Sopenharmony_ci		xb->page_len +
9088c2ecf20Sopenharmony_ci		xb->tail[0].iov_len;
9098c2ecf20Sopenharmony_ci	trace_svc_xdr_sendto(rqstp, xb);
9108c2ecf20Sopenharmony_ci	trace_svc_stats_latency(rqstp);
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	len = xprt->xpt_ops->xpo_sendto(rqstp);
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	trace_svc_send(rqstp, len);
9158c2ecf20Sopenharmony_ci	svc_xprt_release(rqstp);
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci	if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
9188c2ecf20Sopenharmony_ci		len = 0;
9198c2ecf20Sopenharmony_ciout:
9208c2ecf20Sopenharmony_ci	return len;
9218c2ecf20Sopenharmony_ci}
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci/*
9248c2ecf20Sopenharmony_ci * Timer function to close old temporary transports, using
9258c2ecf20Sopenharmony_ci * a mark-and-sweep algorithm.
9268c2ecf20Sopenharmony_ci */
9278c2ecf20Sopenharmony_cistatic void svc_age_temp_xprts(struct timer_list *t)
9288c2ecf20Sopenharmony_ci{
9298c2ecf20Sopenharmony_ci	struct svc_serv *serv = from_timer(serv, t, sv_temptimer);
9308c2ecf20Sopenharmony_ci	struct svc_xprt *xprt;
9318c2ecf20Sopenharmony_ci	struct list_head *le, *next;
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci	dprintk("svc_age_temp_xprts\n");
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_ci	if (!spin_trylock_bh(&serv->sv_lock)) {
9368c2ecf20Sopenharmony_ci		/* busy, try again 1 sec later */
9378c2ecf20Sopenharmony_ci		dprintk("svc_age_temp_xprts: busy\n");
9388c2ecf20Sopenharmony_ci		mod_timer(&serv->sv_temptimer, jiffies + HZ);
9398c2ecf20Sopenharmony_ci		return;
9408c2ecf20Sopenharmony_ci	}
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	list_for_each_safe(le, next, &serv->sv_tempsocks) {
9438c2ecf20Sopenharmony_ci		xprt = list_entry(le, struct svc_xprt, xpt_list);
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci		/* First time through, just mark it OLD. Second time
9468c2ecf20Sopenharmony_ci		 * through, close it. */
9478c2ecf20Sopenharmony_ci		if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags))
9488c2ecf20Sopenharmony_ci			continue;
9498c2ecf20Sopenharmony_ci		if (kref_read(&xprt->xpt_ref) > 1 ||
9508c2ecf20Sopenharmony_ci		    test_bit(XPT_BUSY, &xprt->xpt_flags))
9518c2ecf20Sopenharmony_ci			continue;
9528c2ecf20Sopenharmony_ci		list_del_init(le);
9538c2ecf20Sopenharmony_ci		set_bit(XPT_CLOSE, &xprt->xpt_flags);
9548c2ecf20Sopenharmony_ci		dprintk("queuing xprt %p for closing\n", xprt);
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci		/* a thread will dequeue and close it soon */
9578c2ecf20Sopenharmony_ci		svc_xprt_enqueue(xprt);
9588c2ecf20Sopenharmony_ci	}
9598c2ecf20Sopenharmony_ci	spin_unlock_bh(&serv->sv_lock);
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci	mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
9628c2ecf20Sopenharmony_ci}
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci/* Close temporary transports whose xpt_local matches server_addr immediately
9658c2ecf20Sopenharmony_ci * instead of waiting for them to be picked up by the timer.
9668c2ecf20Sopenharmony_ci *
9678c2ecf20Sopenharmony_ci * This is meant to be called from a notifier_block that runs when an ip
9688c2ecf20Sopenharmony_ci * address is deleted.
9698c2ecf20Sopenharmony_ci */
9708c2ecf20Sopenharmony_civoid svc_age_temp_xprts_now(struct svc_serv *serv, struct sockaddr *server_addr)
9718c2ecf20Sopenharmony_ci{
9728c2ecf20Sopenharmony_ci	struct svc_xprt *xprt;
9738c2ecf20Sopenharmony_ci	struct list_head *le, *next;
9748c2ecf20Sopenharmony_ci	LIST_HEAD(to_be_closed);
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci	spin_lock_bh(&serv->sv_lock);
9778c2ecf20Sopenharmony_ci	list_for_each_safe(le, next, &serv->sv_tempsocks) {
9788c2ecf20Sopenharmony_ci		xprt = list_entry(le, struct svc_xprt, xpt_list);
9798c2ecf20Sopenharmony_ci		if (rpc_cmp_addr(server_addr, (struct sockaddr *)
9808c2ecf20Sopenharmony_ci				&xprt->xpt_local)) {
9818c2ecf20Sopenharmony_ci			dprintk("svc_age_temp_xprts_now: found %p\n", xprt);
9828c2ecf20Sopenharmony_ci			list_move(le, &to_be_closed);
9838c2ecf20Sopenharmony_ci		}
9848c2ecf20Sopenharmony_ci	}
9858c2ecf20Sopenharmony_ci	spin_unlock_bh(&serv->sv_lock);
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	while (!list_empty(&to_be_closed)) {
9888c2ecf20Sopenharmony_ci		le = to_be_closed.next;
9898c2ecf20Sopenharmony_ci		list_del_init(le);
9908c2ecf20Sopenharmony_ci		xprt = list_entry(le, struct svc_xprt, xpt_list);
9918c2ecf20Sopenharmony_ci		set_bit(XPT_CLOSE, &xprt->xpt_flags);
9928c2ecf20Sopenharmony_ci		set_bit(XPT_KILL_TEMP, &xprt->xpt_flags);
9938c2ecf20Sopenharmony_ci		dprintk("svc_age_temp_xprts_now: queuing xprt %p for closing\n",
9948c2ecf20Sopenharmony_ci				xprt);
9958c2ecf20Sopenharmony_ci		svc_xprt_enqueue(xprt);
9968c2ecf20Sopenharmony_ci	}
9978c2ecf20Sopenharmony_ci}
9988c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_age_temp_xprts_now);
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_cistatic void call_xpt_users(struct svc_xprt *xprt)
10018c2ecf20Sopenharmony_ci{
10028c2ecf20Sopenharmony_ci	struct svc_xpt_user *u;
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	spin_lock(&xprt->xpt_lock);
10058c2ecf20Sopenharmony_ci	while (!list_empty(&xprt->xpt_users)) {
10068c2ecf20Sopenharmony_ci		u = list_first_entry(&xprt->xpt_users, struct svc_xpt_user, list);
10078c2ecf20Sopenharmony_ci		list_del_init(&u->list);
10088c2ecf20Sopenharmony_ci		u->callback(u);
10098c2ecf20Sopenharmony_ci	}
10108c2ecf20Sopenharmony_ci	spin_unlock(&xprt->xpt_lock);
10118c2ecf20Sopenharmony_ci}
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci/*
10148c2ecf20Sopenharmony_ci * Remove a dead transport
10158c2ecf20Sopenharmony_ci */
10168c2ecf20Sopenharmony_cistatic void svc_delete_xprt(struct svc_xprt *xprt)
10178c2ecf20Sopenharmony_ci{
10188c2ecf20Sopenharmony_ci	struct svc_serv	*serv = xprt->xpt_server;
10198c2ecf20Sopenharmony_ci	struct svc_deferred_req *dr;
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	if (test_and_set_bit(XPT_DEAD, &xprt->xpt_flags))
10228c2ecf20Sopenharmony_ci		return;
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	trace_svc_xprt_detach(xprt);
10258c2ecf20Sopenharmony_ci	xprt->xpt_ops->xpo_detach(xprt);
10268c2ecf20Sopenharmony_ci	if (xprt->xpt_bc_xprt)
10278c2ecf20Sopenharmony_ci		xprt->xpt_bc_xprt->ops->close(xprt->xpt_bc_xprt);
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	spin_lock_bh(&serv->sv_lock);
10308c2ecf20Sopenharmony_ci	list_del_init(&xprt->xpt_list);
10318c2ecf20Sopenharmony_ci	WARN_ON_ONCE(!list_empty(&xprt->xpt_ready));
10328c2ecf20Sopenharmony_ci	if (test_bit(XPT_TEMP, &xprt->xpt_flags))
10338c2ecf20Sopenharmony_ci		serv->sv_tmpcnt--;
10348c2ecf20Sopenharmony_ci	spin_unlock_bh(&serv->sv_lock);
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci	while ((dr = svc_deferred_dequeue(xprt)) != NULL)
10378c2ecf20Sopenharmony_ci		kfree(dr);
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	call_xpt_users(xprt);
10408c2ecf20Sopenharmony_ci	svc_xprt_put(xprt);
10418c2ecf20Sopenharmony_ci}
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_civoid svc_close_xprt(struct svc_xprt *xprt)
10448c2ecf20Sopenharmony_ci{
10458c2ecf20Sopenharmony_ci	trace_svc_xprt_close(xprt);
10468c2ecf20Sopenharmony_ci	set_bit(XPT_CLOSE, &xprt->xpt_flags);
10478c2ecf20Sopenharmony_ci	if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
10488c2ecf20Sopenharmony_ci		/* someone else will have to effect the close */
10498c2ecf20Sopenharmony_ci		return;
10508c2ecf20Sopenharmony_ci	/*
10518c2ecf20Sopenharmony_ci	 * We expect svc_close_xprt() to work even when no threads are
10528c2ecf20Sopenharmony_ci	 * running (e.g., while configuring the server before starting
10538c2ecf20Sopenharmony_ci	 * any threads), so if the transport isn't busy, we delete
10548c2ecf20Sopenharmony_ci	 * it ourself:
10558c2ecf20Sopenharmony_ci	 */
10568c2ecf20Sopenharmony_ci	svc_delete_xprt(xprt);
10578c2ecf20Sopenharmony_ci}
10588c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_close_xprt);
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_cistatic int svc_close_list(struct svc_serv *serv, struct list_head *xprt_list, struct net *net)
10618c2ecf20Sopenharmony_ci{
10628c2ecf20Sopenharmony_ci	struct svc_xprt *xprt;
10638c2ecf20Sopenharmony_ci	int ret = 0;
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci	spin_lock_bh(&serv->sv_lock);
10668c2ecf20Sopenharmony_ci	list_for_each_entry(xprt, xprt_list, xpt_list) {
10678c2ecf20Sopenharmony_ci		if (xprt->xpt_net != net)
10688c2ecf20Sopenharmony_ci			continue;
10698c2ecf20Sopenharmony_ci		ret++;
10708c2ecf20Sopenharmony_ci		set_bit(XPT_CLOSE, &xprt->xpt_flags);
10718c2ecf20Sopenharmony_ci		svc_xprt_enqueue(xprt);
10728c2ecf20Sopenharmony_ci	}
10738c2ecf20Sopenharmony_ci	spin_unlock_bh(&serv->sv_lock);
10748c2ecf20Sopenharmony_ci	return ret;
10758c2ecf20Sopenharmony_ci}
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_cistatic struct svc_xprt *svc_dequeue_net(struct svc_serv *serv, struct net *net)
10788c2ecf20Sopenharmony_ci{
10798c2ecf20Sopenharmony_ci	struct svc_pool *pool;
10808c2ecf20Sopenharmony_ci	struct svc_xprt *xprt;
10818c2ecf20Sopenharmony_ci	struct svc_xprt *tmp;
10828c2ecf20Sopenharmony_ci	int i;
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	for (i = 0; i < serv->sv_nrpools; i++) {
10858c2ecf20Sopenharmony_ci		pool = &serv->sv_pools[i];
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci		spin_lock_bh(&pool->sp_lock);
10888c2ecf20Sopenharmony_ci		list_for_each_entry_safe(xprt, tmp, &pool->sp_sockets, xpt_ready) {
10898c2ecf20Sopenharmony_ci			if (xprt->xpt_net != net)
10908c2ecf20Sopenharmony_ci				continue;
10918c2ecf20Sopenharmony_ci			list_del_init(&xprt->xpt_ready);
10928c2ecf20Sopenharmony_ci			spin_unlock_bh(&pool->sp_lock);
10938c2ecf20Sopenharmony_ci			return xprt;
10948c2ecf20Sopenharmony_ci		}
10958c2ecf20Sopenharmony_ci		spin_unlock_bh(&pool->sp_lock);
10968c2ecf20Sopenharmony_ci	}
10978c2ecf20Sopenharmony_ci	return NULL;
10988c2ecf20Sopenharmony_ci}
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_cistatic void svc_clean_up_xprts(struct svc_serv *serv, struct net *net)
11018c2ecf20Sopenharmony_ci{
11028c2ecf20Sopenharmony_ci	struct svc_xprt *xprt;
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	while ((xprt = svc_dequeue_net(serv, net))) {
11058c2ecf20Sopenharmony_ci		set_bit(XPT_CLOSE, &xprt->xpt_flags);
11068c2ecf20Sopenharmony_ci		svc_delete_xprt(xprt);
11078c2ecf20Sopenharmony_ci	}
11088c2ecf20Sopenharmony_ci}
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci/*
11118c2ecf20Sopenharmony_ci * Server threads may still be running (especially in the case where the
11128c2ecf20Sopenharmony_ci * service is still running in other network namespaces).
11138c2ecf20Sopenharmony_ci *
11148c2ecf20Sopenharmony_ci * So we shut down sockets the same way we would on a running server, by
11158c2ecf20Sopenharmony_ci * setting XPT_CLOSE, enqueuing, and letting a thread pick it up to do
11168c2ecf20Sopenharmony_ci * the close.  In the case there are no such other threads,
11178c2ecf20Sopenharmony_ci * threads running, svc_clean_up_xprts() does a simple version of a
11188c2ecf20Sopenharmony_ci * server's main event loop, and in the case where there are other
11198c2ecf20Sopenharmony_ci * threads, we may need to wait a little while and then check again to
11208c2ecf20Sopenharmony_ci * see if they're done.
11218c2ecf20Sopenharmony_ci */
11228c2ecf20Sopenharmony_civoid svc_close_net(struct svc_serv *serv, struct net *net)
11238c2ecf20Sopenharmony_ci{
11248c2ecf20Sopenharmony_ci	int delay = 0;
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci	while (svc_close_list(serv, &serv->sv_permsocks, net) +
11278c2ecf20Sopenharmony_ci	       svc_close_list(serv, &serv->sv_tempsocks, net)) {
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci		svc_clean_up_xprts(serv, net);
11308c2ecf20Sopenharmony_ci		msleep(delay++);
11318c2ecf20Sopenharmony_ci	}
11328c2ecf20Sopenharmony_ci}
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci/*
11358c2ecf20Sopenharmony_ci * Handle defer and revisit of requests
11368c2ecf20Sopenharmony_ci */
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_cistatic void svc_revisit(struct cache_deferred_req *dreq, int too_many)
11398c2ecf20Sopenharmony_ci{
11408c2ecf20Sopenharmony_ci	struct svc_deferred_req *dr =
11418c2ecf20Sopenharmony_ci		container_of(dreq, struct svc_deferred_req, handle);
11428c2ecf20Sopenharmony_ci	struct svc_xprt *xprt = dr->xprt;
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_ci	spin_lock(&xprt->xpt_lock);
11458c2ecf20Sopenharmony_ci	set_bit(XPT_DEFERRED, &xprt->xpt_flags);
11468c2ecf20Sopenharmony_ci	if (too_many || test_bit(XPT_DEAD, &xprt->xpt_flags)) {
11478c2ecf20Sopenharmony_ci		spin_unlock(&xprt->xpt_lock);
11488c2ecf20Sopenharmony_ci		trace_svc_defer_drop(dr);
11498c2ecf20Sopenharmony_ci		svc_xprt_put(xprt);
11508c2ecf20Sopenharmony_ci		kfree(dr);
11518c2ecf20Sopenharmony_ci		return;
11528c2ecf20Sopenharmony_ci	}
11538c2ecf20Sopenharmony_ci	dr->xprt = NULL;
11548c2ecf20Sopenharmony_ci	list_add(&dr->handle.recent, &xprt->xpt_deferred);
11558c2ecf20Sopenharmony_ci	spin_unlock(&xprt->xpt_lock);
11568c2ecf20Sopenharmony_ci	trace_svc_defer_queue(dr);
11578c2ecf20Sopenharmony_ci	svc_xprt_enqueue(xprt);
11588c2ecf20Sopenharmony_ci	svc_xprt_put(xprt);
11598c2ecf20Sopenharmony_ci}
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci/*
11628c2ecf20Sopenharmony_ci * Save the request off for later processing. The request buffer looks
11638c2ecf20Sopenharmony_ci * like this:
11648c2ecf20Sopenharmony_ci *
11658c2ecf20Sopenharmony_ci * <xprt-header><rpc-header><rpc-pagelist><rpc-tail>
11668c2ecf20Sopenharmony_ci *
11678c2ecf20Sopenharmony_ci * This code can only handle requests that consist of an xprt-header
11688c2ecf20Sopenharmony_ci * and rpc-header.
11698c2ecf20Sopenharmony_ci */
11708c2ecf20Sopenharmony_cistatic struct cache_deferred_req *svc_defer(struct cache_req *req)
11718c2ecf20Sopenharmony_ci{
11728c2ecf20Sopenharmony_ci	struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
11738c2ecf20Sopenharmony_ci	struct svc_deferred_req *dr;
11748c2ecf20Sopenharmony_ci
11758c2ecf20Sopenharmony_ci	if (rqstp->rq_arg.page_len || !test_bit(RQ_USEDEFERRAL, &rqstp->rq_flags))
11768c2ecf20Sopenharmony_ci		return NULL; /* if more than a page, give up FIXME */
11778c2ecf20Sopenharmony_ci	if (rqstp->rq_deferred) {
11788c2ecf20Sopenharmony_ci		dr = rqstp->rq_deferred;
11798c2ecf20Sopenharmony_ci		rqstp->rq_deferred = NULL;
11808c2ecf20Sopenharmony_ci	} else {
11818c2ecf20Sopenharmony_ci		size_t skip;
11828c2ecf20Sopenharmony_ci		size_t size;
11838c2ecf20Sopenharmony_ci		/* FIXME maybe discard if size too large */
11848c2ecf20Sopenharmony_ci		size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len;
11858c2ecf20Sopenharmony_ci		dr = kmalloc(size, GFP_KERNEL);
11868c2ecf20Sopenharmony_ci		if (dr == NULL)
11878c2ecf20Sopenharmony_ci			return NULL;
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci		dr->handle.owner = rqstp->rq_server;
11908c2ecf20Sopenharmony_ci		dr->prot = rqstp->rq_prot;
11918c2ecf20Sopenharmony_ci		memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
11928c2ecf20Sopenharmony_ci		dr->addrlen = rqstp->rq_addrlen;
11938c2ecf20Sopenharmony_ci		dr->daddr = rqstp->rq_daddr;
11948c2ecf20Sopenharmony_ci		dr->argslen = rqstp->rq_arg.len >> 2;
11958c2ecf20Sopenharmony_ci		dr->xprt_hlen = rqstp->rq_xprt_hlen;
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci		/* back up head to the start of the buffer and copy */
11988c2ecf20Sopenharmony_ci		skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
11998c2ecf20Sopenharmony_ci		memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip,
12008c2ecf20Sopenharmony_ci		       dr->argslen << 2);
12018c2ecf20Sopenharmony_ci	}
12028c2ecf20Sopenharmony_ci	trace_svc_defer(rqstp);
12038c2ecf20Sopenharmony_ci	svc_xprt_get(rqstp->rq_xprt);
12048c2ecf20Sopenharmony_ci	dr->xprt = rqstp->rq_xprt;
12058c2ecf20Sopenharmony_ci	set_bit(RQ_DROPME, &rqstp->rq_flags);
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci	dr->handle.revisit = svc_revisit;
12088c2ecf20Sopenharmony_ci	return &dr->handle;
12098c2ecf20Sopenharmony_ci}
12108c2ecf20Sopenharmony_ci
12118c2ecf20Sopenharmony_ci/*
12128c2ecf20Sopenharmony_ci * recv data from a deferred request into an active one
12138c2ecf20Sopenharmony_ci */
12148c2ecf20Sopenharmony_cistatic noinline int svc_deferred_recv(struct svc_rqst *rqstp)
12158c2ecf20Sopenharmony_ci{
12168c2ecf20Sopenharmony_ci	struct svc_deferred_req *dr = rqstp->rq_deferred;
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	trace_svc_defer_recv(dr);
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	/* setup iov_base past transport header */
12218c2ecf20Sopenharmony_ci	rqstp->rq_arg.head[0].iov_base = dr->args + (dr->xprt_hlen>>2);
12228c2ecf20Sopenharmony_ci	/* The iov_len does not include the transport header bytes */
12238c2ecf20Sopenharmony_ci	rqstp->rq_arg.head[0].iov_len = (dr->argslen<<2) - dr->xprt_hlen;
12248c2ecf20Sopenharmony_ci	rqstp->rq_arg.page_len = 0;
12258c2ecf20Sopenharmony_ci	/* The rq_arg.len includes the transport header bytes */
12268c2ecf20Sopenharmony_ci	rqstp->rq_arg.len     = dr->argslen<<2;
12278c2ecf20Sopenharmony_ci	rqstp->rq_prot        = dr->prot;
12288c2ecf20Sopenharmony_ci	memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
12298c2ecf20Sopenharmony_ci	rqstp->rq_addrlen     = dr->addrlen;
12308c2ecf20Sopenharmony_ci	/* Save off transport header len in case we get deferred again */
12318c2ecf20Sopenharmony_ci	rqstp->rq_xprt_hlen   = dr->xprt_hlen;
12328c2ecf20Sopenharmony_ci	rqstp->rq_daddr       = dr->daddr;
12338c2ecf20Sopenharmony_ci	rqstp->rq_respages    = rqstp->rq_pages;
12348c2ecf20Sopenharmony_ci	return (dr->argslen<<2) - dr->xprt_hlen;
12358c2ecf20Sopenharmony_ci}
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_cistatic struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
12398c2ecf20Sopenharmony_ci{
12408c2ecf20Sopenharmony_ci	struct svc_deferred_req *dr = NULL;
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
12438c2ecf20Sopenharmony_ci		return NULL;
12448c2ecf20Sopenharmony_ci	spin_lock(&xprt->xpt_lock);
12458c2ecf20Sopenharmony_ci	if (!list_empty(&xprt->xpt_deferred)) {
12468c2ecf20Sopenharmony_ci		dr = list_entry(xprt->xpt_deferred.next,
12478c2ecf20Sopenharmony_ci				struct svc_deferred_req,
12488c2ecf20Sopenharmony_ci				handle.recent);
12498c2ecf20Sopenharmony_ci		list_del_init(&dr->handle.recent);
12508c2ecf20Sopenharmony_ci	} else
12518c2ecf20Sopenharmony_ci		clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
12528c2ecf20Sopenharmony_ci	spin_unlock(&xprt->xpt_lock);
12538c2ecf20Sopenharmony_ci	return dr;
12548c2ecf20Sopenharmony_ci}
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci/**
12578c2ecf20Sopenharmony_ci * svc_find_xprt - find an RPC transport instance
12588c2ecf20Sopenharmony_ci * @serv: pointer to svc_serv to search
12598c2ecf20Sopenharmony_ci * @xcl_name: C string containing transport's class name
12608c2ecf20Sopenharmony_ci * @net: owner net pointer
12618c2ecf20Sopenharmony_ci * @af: Address family of transport's local address
12628c2ecf20Sopenharmony_ci * @port: transport's IP port number
12638c2ecf20Sopenharmony_ci *
12648c2ecf20Sopenharmony_ci * Return the transport instance pointer for the endpoint accepting
12658c2ecf20Sopenharmony_ci * connections/peer traffic from the specified transport class,
12668c2ecf20Sopenharmony_ci * address family and port.
12678c2ecf20Sopenharmony_ci *
12688c2ecf20Sopenharmony_ci * Specifying 0 for the address family or port is effectively a
12698c2ecf20Sopenharmony_ci * wild-card, and will result in matching the first transport in the
12708c2ecf20Sopenharmony_ci * service's list that has a matching class name.
12718c2ecf20Sopenharmony_ci */
12728c2ecf20Sopenharmony_cistruct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name,
12738c2ecf20Sopenharmony_ci			       struct net *net, const sa_family_t af,
12748c2ecf20Sopenharmony_ci			       const unsigned short port)
12758c2ecf20Sopenharmony_ci{
12768c2ecf20Sopenharmony_ci	struct svc_xprt *xprt;
12778c2ecf20Sopenharmony_ci	struct svc_xprt *found = NULL;
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci	/* Sanity check the args */
12808c2ecf20Sopenharmony_ci	if (serv == NULL || xcl_name == NULL)
12818c2ecf20Sopenharmony_ci		return found;
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_ci	spin_lock_bh(&serv->sv_lock);
12848c2ecf20Sopenharmony_ci	list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
12858c2ecf20Sopenharmony_ci		if (xprt->xpt_net != net)
12868c2ecf20Sopenharmony_ci			continue;
12878c2ecf20Sopenharmony_ci		if (strcmp(xprt->xpt_class->xcl_name, xcl_name))
12888c2ecf20Sopenharmony_ci			continue;
12898c2ecf20Sopenharmony_ci		if (af != AF_UNSPEC && af != xprt->xpt_local.ss_family)
12908c2ecf20Sopenharmony_ci			continue;
12918c2ecf20Sopenharmony_ci		if (port != 0 && port != svc_xprt_local_port(xprt))
12928c2ecf20Sopenharmony_ci			continue;
12938c2ecf20Sopenharmony_ci		found = xprt;
12948c2ecf20Sopenharmony_ci		svc_xprt_get(xprt);
12958c2ecf20Sopenharmony_ci		break;
12968c2ecf20Sopenharmony_ci	}
12978c2ecf20Sopenharmony_ci	spin_unlock_bh(&serv->sv_lock);
12988c2ecf20Sopenharmony_ci	return found;
12998c2ecf20Sopenharmony_ci}
13008c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_find_xprt);
13018c2ecf20Sopenharmony_ci
13028c2ecf20Sopenharmony_cistatic int svc_one_xprt_name(const struct svc_xprt *xprt,
13038c2ecf20Sopenharmony_ci			     char *pos, int remaining)
13048c2ecf20Sopenharmony_ci{
13058c2ecf20Sopenharmony_ci	int len;
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci	len = snprintf(pos, remaining, "%s %u\n",
13088c2ecf20Sopenharmony_ci			xprt->xpt_class->xcl_name,
13098c2ecf20Sopenharmony_ci			svc_xprt_local_port(xprt));
13108c2ecf20Sopenharmony_ci	if (len >= remaining)
13118c2ecf20Sopenharmony_ci		return -ENAMETOOLONG;
13128c2ecf20Sopenharmony_ci	return len;
13138c2ecf20Sopenharmony_ci}
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_ci/**
13168c2ecf20Sopenharmony_ci * svc_xprt_names - format a buffer with a list of transport names
13178c2ecf20Sopenharmony_ci * @serv: pointer to an RPC service
13188c2ecf20Sopenharmony_ci * @buf: pointer to a buffer to be filled in
13198c2ecf20Sopenharmony_ci * @buflen: length of buffer to be filled in
13208c2ecf20Sopenharmony_ci *
13218c2ecf20Sopenharmony_ci * Fills in @buf with a string containing a list of transport names,
13228c2ecf20Sopenharmony_ci * each name terminated with '\n'.
13238c2ecf20Sopenharmony_ci *
13248c2ecf20Sopenharmony_ci * Returns positive length of the filled-in string on success; otherwise
13258c2ecf20Sopenharmony_ci * a negative errno value is returned if an error occurs.
13268c2ecf20Sopenharmony_ci */
13278c2ecf20Sopenharmony_ciint svc_xprt_names(struct svc_serv *serv, char *buf, const int buflen)
13288c2ecf20Sopenharmony_ci{
13298c2ecf20Sopenharmony_ci	struct svc_xprt *xprt;
13308c2ecf20Sopenharmony_ci	int len, totlen;
13318c2ecf20Sopenharmony_ci	char *pos;
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_ci	/* Sanity check args */
13348c2ecf20Sopenharmony_ci	if (!serv)
13358c2ecf20Sopenharmony_ci		return 0;
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ci	spin_lock_bh(&serv->sv_lock);
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_ci	pos = buf;
13408c2ecf20Sopenharmony_ci	totlen = 0;
13418c2ecf20Sopenharmony_ci	list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
13428c2ecf20Sopenharmony_ci		len = svc_one_xprt_name(xprt, pos, buflen - totlen);
13438c2ecf20Sopenharmony_ci		if (len < 0) {
13448c2ecf20Sopenharmony_ci			*buf = '\0';
13458c2ecf20Sopenharmony_ci			totlen = len;
13468c2ecf20Sopenharmony_ci		}
13478c2ecf20Sopenharmony_ci		if (len <= 0)
13488c2ecf20Sopenharmony_ci			break;
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ci		pos += len;
13518c2ecf20Sopenharmony_ci		totlen += len;
13528c2ecf20Sopenharmony_ci	}
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_ci	spin_unlock_bh(&serv->sv_lock);
13558c2ecf20Sopenharmony_ci	return totlen;
13568c2ecf20Sopenharmony_ci}
13578c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(svc_xprt_names);
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci
13608c2ecf20Sopenharmony_ci/*----------------------------------------------------------------------------*/
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_cistatic void *svc_pool_stats_start(struct seq_file *m, loff_t *pos)
13638c2ecf20Sopenharmony_ci{
13648c2ecf20Sopenharmony_ci	unsigned int pidx = (unsigned int)*pos;
13658c2ecf20Sopenharmony_ci	struct svc_serv *serv = m->private;
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_ci	dprintk("svc_pool_stats_start, *pidx=%u\n", pidx);
13688c2ecf20Sopenharmony_ci
13698c2ecf20Sopenharmony_ci	if (!pidx)
13708c2ecf20Sopenharmony_ci		return SEQ_START_TOKEN;
13718c2ecf20Sopenharmony_ci	return (pidx > serv->sv_nrpools ? NULL : &serv->sv_pools[pidx-1]);
13728c2ecf20Sopenharmony_ci}
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_cistatic void *svc_pool_stats_next(struct seq_file *m, void *p, loff_t *pos)
13758c2ecf20Sopenharmony_ci{
13768c2ecf20Sopenharmony_ci	struct svc_pool *pool = p;
13778c2ecf20Sopenharmony_ci	struct svc_serv *serv = m->private;
13788c2ecf20Sopenharmony_ci
13798c2ecf20Sopenharmony_ci	dprintk("svc_pool_stats_next, *pos=%llu\n", *pos);
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci	if (p == SEQ_START_TOKEN) {
13828c2ecf20Sopenharmony_ci		pool = &serv->sv_pools[0];
13838c2ecf20Sopenharmony_ci	} else {
13848c2ecf20Sopenharmony_ci		unsigned int pidx = (pool - &serv->sv_pools[0]);
13858c2ecf20Sopenharmony_ci		if (pidx < serv->sv_nrpools-1)
13868c2ecf20Sopenharmony_ci			pool = &serv->sv_pools[pidx+1];
13878c2ecf20Sopenharmony_ci		else
13888c2ecf20Sopenharmony_ci			pool = NULL;
13898c2ecf20Sopenharmony_ci	}
13908c2ecf20Sopenharmony_ci	++*pos;
13918c2ecf20Sopenharmony_ci	return pool;
13928c2ecf20Sopenharmony_ci}
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_cistatic void svc_pool_stats_stop(struct seq_file *m, void *p)
13958c2ecf20Sopenharmony_ci{
13968c2ecf20Sopenharmony_ci}
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_cistatic int svc_pool_stats_show(struct seq_file *m, void *p)
13998c2ecf20Sopenharmony_ci{
14008c2ecf20Sopenharmony_ci	struct svc_pool *pool = p;
14018c2ecf20Sopenharmony_ci
14028c2ecf20Sopenharmony_ci	if (p == SEQ_START_TOKEN) {
14038c2ecf20Sopenharmony_ci		seq_puts(m, "# pool packets-arrived sockets-enqueued threads-woken threads-timedout\n");
14048c2ecf20Sopenharmony_ci		return 0;
14058c2ecf20Sopenharmony_ci	}
14068c2ecf20Sopenharmony_ci
14078c2ecf20Sopenharmony_ci	seq_printf(m, "%u %lu %lu %lu %lu\n",
14088c2ecf20Sopenharmony_ci		pool->sp_id,
14098c2ecf20Sopenharmony_ci		(unsigned long)atomic_long_read(&pool->sp_stats.packets),
14108c2ecf20Sopenharmony_ci		pool->sp_stats.sockets_queued,
14118c2ecf20Sopenharmony_ci		(unsigned long)atomic_long_read(&pool->sp_stats.threads_woken),
14128c2ecf20Sopenharmony_ci		(unsigned long)atomic_long_read(&pool->sp_stats.threads_timedout));
14138c2ecf20Sopenharmony_ci
14148c2ecf20Sopenharmony_ci	return 0;
14158c2ecf20Sopenharmony_ci}
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_cistatic const struct seq_operations svc_pool_stats_seq_ops = {
14188c2ecf20Sopenharmony_ci	.start	= svc_pool_stats_start,
14198c2ecf20Sopenharmony_ci	.next	= svc_pool_stats_next,
14208c2ecf20Sopenharmony_ci	.stop	= svc_pool_stats_stop,
14218c2ecf20Sopenharmony_ci	.show	= svc_pool_stats_show,
14228c2ecf20Sopenharmony_ci};
14238c2ecf20Sopenharmony_ci
14248c2ecf20Sopenharmony_ciint svc_pool_stats_open(struct svc_serv *serv, struct file *file)
14258c2ecf20Sopenharmony_ci{
14268c2ecf20Sopenharmony_ci	int err;
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_ci	err = seq_open(file, &svc_pool_stats_seq_ops);
14298c2ecf20Sopenharmony_ci	if (!err)
14308c2ecf20Sopenharmony_ci		((struct seq_file *) file->private_data)->private = serv;
14318c2ecf20Sopenharmony_ci	return err;
14328c2ecf20Sopenharmony_ci}
14338c2ecf20Sopenharmony_ciEXPORT_SYMBOL(svc_pool_stats_open);
14348c2ecf20Sopenharmony_ci
14358c2ecf20Sopenharmony_ci/*----------------------------------------------------------------------------*/
1436