18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  linux/net/sunrpc/xprt.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  This is a generic RPC call interface supporting congestion avoidance,
68c2ecf20Sopenharmony_ci *  and asynchronous calls.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *  The interface works like this:
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *  -	When a process places a call, it allocates a request slot if
118c2ecf20Sopenharmony_ci *	one is available. Otherwise, it sleeps on the backlog queue
128c2ecf20Sopenharmony_ci *	(xprt_reserve).
138c2ecf20Sopenharmony_ci *  -	Next, the caller puts together the RPC message, stuffs it into
148c2ecf20Sopenharmony_ci *	the request struct, and calls xprt_transmit().
158c2ecf20Sopenharmony_ci *  -	xprt_transmit sends the message and installs the caller on the
168c2ecf20Sopenharmony_ci *	transport's wait list. At the same time, if a reply is expected,
178c2ecf20Sopenharmony_ci *	it installs a timer that is run after the packet's timeout has
188c2ecf20Sopenharmony_ci *	expired.
198c2ecf20Sopenharmony_ci *  -	When a packet arrives, the data_ready handler walks the list of
208c2ecf20Sopenharmony_ci *	pending requests for that transport. If a matching XID is found, the
218c2ecf20Sopenharmony_ci *	caller is woken up, and the timer removed.
228c2ecf20Sopenharmony_ci *  -	When no reply arrives within the timeout interval, the timer is
238c2ecf20Sopenharmony_ci *	fired by the kernel and runs xprt_timer(). It either adjusts the
248c2ecf20Sopenharmony_ci *	timeout values (minor timeout) or wakes up the caller with a status
258c2ecf20Sopenharmony_ci *	of -ETIMEDOUT.
268c2ecf20Sopenharmony_ci *  -	When the caller receives a notification from RPC that a reply arrived,
278c2ecf20Sopenharmony_ci *	it should release the RPC slot, and process the reply.
288c2ecf20Sopenharmony_ci *	If the call timed out, it may choose to retry the operation by
298c2ecf20Sopenharmony_ci *	adjusting the initial timeout value, and simply calling rpc_call
308c2ecf20Sopenharmony_ci *	again.
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci *  Support for async RPC is done through a set of RPC-specific scheduling
338c2ecf20Sopenharmony_ci *  primitives that `transparently' work for processes as well as async
348c2ecf20Sopenharmony_ci *  tasks that rely on callbacks.
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci *  Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci *  Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
398c2ecf20Sopenharmony_ci */
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#include <linux/module.h>
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#include <linux/types.h>
448c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
458c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
468c2ecf20Sopenharmony_ci#include <linux/net.h>
478c2ecf20Sopenharmony_ci#include <linux/ktime.h>
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#include <linux/sunrpc/clnt.h>
508c2ecf20Sopenharmony_ci#include <linux/sunrpc/metrics.h>
518c2ecf20Sopenharmony_ci#include <linux/sunrpc/bc_xprt.h>
528c2ecf20Sopenharmony_ci#include <linux/rcupdate.h>
538c2ecf20Sopenharmony_ci#include <linux/sched/mm.h>
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#include <trace/events/sunrpc.h>
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#include "sunrpc.h"
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/*
608c2ecf20Sopenharmony_ci * Local variables
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
648c2ecf20Sopenharmony_ci# define RPCDBG_FACILITY	RPCDBG_XPRT
658c2ecf20Sopenharmony_ci#endif
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/*
688c2ecf20Sopenharmony_ci * Local functions
698c2ecf20Sopenharmony_ci */
708c2ecf20Sopenharmony_cistatic void	 xprt_init(struct rpc_xprt *xprt, struct net *net);
718c2ecf20Sopenharmony_cistatic __be32	xprt_alloc_xid(struct rpc_xprt *xprt);
728c2ecf20Sopenharmony_cistatic void	 xprt_destroy(struct rpc_xprt *xprt);
738c2ecf20Sopenharmony_cistatic void	 xprt_request_init(struct rpc_task *task);
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(xprt_list_lock);
768c2ecf20Sopenharmony_cistatic LIST_HEAD(xprt_list);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic unsigned long xprt_request_timeout(const struct rpc_rqst *req)
798c2ecf20Sopenharmony_ci{
808c2ecf20Sopenharmony_ci	unsigned long timeout = jiffies + req->rq_timeout;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	if (time_before(timeout, req->rq_majortimeo))
838c2ecf20Sopenharmony_ci		return timeout;
848c2ecf20Sopenharmony_ci	return req->rq_majortimeo;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/**
888c2ecf20Sopenharmony_ci * xprt_register_transport - register a transport implementation
898c2ecf20Sopenharmony_ci * @transport: transport to register
908c2ecf20Sopenharmony_ci *
918c2ecf20Sopenharmony_ci * If a transport implementation is loaded as a kernel module, it can
928c2ecf20Sopenharmony_ci * call this interface to make itself known to the RPC client.
938c2ecf20Sopenharmony_ci *
948c2ecf20Sopenharmony_ci * Returns:
958c2ecf20Sopenharmony_ci * 0:		transport successfully registered
968c2ecf20Sopenharmony_ci * -EEXIST:	transport already registered
978c2ecf20Sopenharmony_ci * -EINVAL:	transport module being unloaded
988c2ecf20Sopenharmony_ci */
998c2ecf20Sopenharmony_ciint xprt_register_transport(struct xprt_class *transport)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	struct xprt_class *t;
1028c2ecf20Sopenharmony_ci	int result;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	result = -EEXIST;
1058c2ecf20Sopenharmony_ci	spin_lock(&xprt_list_lock);
1068c2ecf20Sopenharmony_ci	list_for_each_entry(t, &xprt_list, list) {
1078c2ecf20Sopenharmony_ci		/* don't register the same transport class twice */
1088c2ecf20Sopenharmony_ci		if (t->ident == transport->ident)
1098c2ecf20Sopenharmony_ci			goto out;
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	list_add_tail(&transport->list, &xprt_list);
1138c2ecf20Sopenharmony_ci	printk(KERN_INFO "RPC: Registered %s transport module.\n",
1148c2ecf20Sopenharmony_ci	       transport->name);
1158c2ecf20Sopenharmony_ci	result = 0;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ciout:
1188c2ecf20Sopenharmony_ci	spin_unlock(&xprt_list_lock);
1198c2ecf20Sopenharmony_ci	return result;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_register_transport);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/**
1248c2ecf20Sopenharmony_ci * xprt_unregister_transport - unregister a transport implementation
1258c2ecf20Sopenharmony_ci * @transport: transport to unregister
1268c2ecf20Sopenharmony_ci *
1278c2ecf20Sopenharmony_ci * Returns:
1288c2ecf20Sopenharmony_ci * 0:		transport successfully unregistered
1298c2ecf20Sopenharmony_ci * -ENOENT:	transport never registered
1308c2ecf20Sopenharmony_ci */
1318c2ecf20Sopenharmony_ciint xprt_unregister_transport(struct xprt_class *transport)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	struct xprt_class *t;
1348c2ecf20Sopenharmony_ci	int result;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	result = 0;
1378c2ecf20Sopenharmony_ci	spin_lock(&xprt_list_lock);
1388c2ecf20Sopenharmony_ci	list_for_each_entry(t, &xprt_list, list) {
1398c2ecf20Sopenharmony_ci		if (t == transport) {
1408c2ecf20Sopenharmony_ci			printk(KERN_INFO
1418c2ecf20Sopenharmony_ci				"RPC: Unregistered %s transport module.\n",
1428c2ecf20Sopenharmony_ci				transport->name);
1438c2ecf20Sopenharmony_ci			list_del_init(&transport->list);
1448c2ecf20Sopenharmony_ci			goto out;
1458c2ecf20Sopenharmony_ci		}
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci	result = -ENOENT;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ciout:
1508c2ecf20Sopenharmony_ci	spin_unlock(&xprt_list_lock);
1518c2ecf20Sopenharmony_ci	return result;
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_unregister_transport);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic void
1568c2ecf20Sopenharmony_cixprt_class_release(const struct xprt_class *t)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	module_put(t->owner);
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistatic const struct xprt_class *
1628c2ecf20Sopenharmony_cixprt_class_find_by_netid_locked(const char *netid)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	const struct xprt_class *t;
1658c2ecf20Sopenharmony_ci	unsigned int i;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	list_for_each_entry(t, &xprt_list, list) {
1688c2ecf20Sopenharmony_ci		for (i = 0; t->netid[i][0] != '\0'; i++) {
1698c2ecf20Sopenharmony_ci			if (strcmp(t->netid[i], netid) != 0)
1708c2ecf20Sopenharmony_ci				continue;
1718c2ecf20Sopenharmony_ci			if (!try_module_get(t->owner))
1728c2ecf20Sopenharmony_ci				continue;
1738c2ecf20Sopenharmony_ci			return t;
1748c2ecf20Sopenharmony_ci		}
1758c2ecf20Sopenharmony_ci	}
1768c2ecf20Sopenharmony_ci	return NULL;
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic const struct xprt_class *
1808c2ecf20Sopenharmony_cixprt_class_find_by_netid(const char *netid)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	const struct xprt_class *t;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	spin_lock(&xprt_list_lock);
1858c2ecf20Sopenharmony_ci	t = xprt_class_find_by_netid_locked(netid);
1868c2ecf20Sopenharmony_ci	if (!t) {
1878c2ecf20Sopenharmony_ci		spin_unlock(&xprt_list_lock);
1888c2ecf20Sopenharmony_ci		request_module("rpc%s", netid);
1898c2ecf20Sopenharmony_ci		spin_lock(&xprt_list_lock);
1908c2ecf20Sopenharmony_ci		t = xprt_class_find_by_netid_locked(netid);
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci	spin_unlock(&xprt_list_lock);
1938c2ecf20Sopenharmony_ci	return t;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci/**
1978c2ecf20Sopenharmony_ci * xprt_load_transport - load a transport implementation
1988c2ecf20Sopenharmony_ci * @netid: transport to load
1998c2ecf20Sopenharmony_ci *
2008c2ecf20Sopenharmony_ci * Returns:
2018c2ecf20Sopenharmony_ci * 0:		transport successfully loaded
2028c2ecf20Sopenharmony_ci * -ENOENT:	transport module not available
2038c2ecf20Sopenharmony_ci */
2048c2ecf20Sopenharmony_ciint xprt_load_transport(const char *netid)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	const struct xprt_class *t;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	t = xprt_class_find_by_netid(netid);
2098c2ecf20Sopenharmony_ci	if (!t)
2108c2ecf20Sopenharmony_ci		return -ENOENT;
2118c2ecf20Sopenharmony_ci	xprt_class_release(t);
2128c2ecf20Sopenharmony_ci	return 0;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_load_transport);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic void xprt_clear_locked(struct rpc_xprt *xprt)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	xprt->snd_task = NULL;
2198c2ecf20Sopenharmony_ci	if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
2208c2ecf20Sopenharmony_ci		smp_mb__before_atomic();
2218c2ecf20Sopenharmony_ci		clear_bit(XPRT_LOCKED, &xprt->state);
2228c2ecf20Sopenharmony_ci		smp_mb__after_atomic();
2238c2ecf20Sopenharmony_ci	} else
2248c2ecf20Sopenharmony_ci		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci/**
2288c2ecf20Sopenharmony_ci * xprt_reserve_xprt - serialize write access to transports
2298c2ecf20Sopenharmony_ci * @task: task that is requesting access to the transport
2308c2ecf20Sopenharmony_ci * @xprt: pointer to the target transport
2318c2ecf20Sopenharmony_ci *
2328c2ecf20Sopenharmony_ci * This prevents mixing the payload of separate requests, and prevents
2338c2ecf20Sopenharmony_ci * transport connects from colliding with writes.  No congestion control
2348c2ecf20Sopenharmony_ci * is provided.
2358c2ecf20Sopenharmony_ci */
2368c2ecf20Sopenharmony_ciint xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
2418c2ecf20Sopenharmony_ci		if (task == xprt->snd_task)
2428c2ecf20Sopenharmony_ci			goto out_locked;
2438c2ecf20Sopenharmony_ci		goto out_sleep;
2448c2ecf20Sopenharmony_ci	}
2458c2ecf20Sopenharmony_ci	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
2468c2ecf20Sopenharmony_ci		goto out_unlock;
2478c2ecf20Sopenharmony_ci	xprt->snd_task = task;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ciout_locked:
2508c2ecf20Sopenharmony_ci	trace_xprt_reserve_xprt(xprt, task);
2518c2ecf20Sopenharmony_ci	return 1;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ciout_unlock:
2548c2ecf20Sopenharmony_ci	xprt_clear_locked(xprt);
2558c2ecf20Sopenharmony_ciout_sleep:
2568c2ecf20Sopenharmony_ci	task->tk_status = -EAGAIN;
2578c2ecf20Sopenharmony_ci	if  (RPC_IS_SOFT(task))
2588c2ecf20Sopenharmony_ci		rpc_sleep_on_timeout(&xprt->sending, task, NULL,
2598c2ecf20Sopenharmony_ci				xprt_request_timeout(req));
2608c2ecf20Sopenharmony_ci	else
2618c2ecf20Sopenharmony_ci		rpc_sleep_on(&xprt->sending, task, NULL);
2628c2ecf20Sopenharmony_ci	return 0;
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_reserve_xprt);
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic bool
2678c2ecf20Sopenharmony_cixprt_need_congestion_window_wait(struct rpc_xprt *xprt)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	return test_bit(XPRT_CWND_WAIT, &xprt->state);
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic void
2738c2ecf20Sopenharmony_cixprt_set_congestion_window_wait(struct rpc_xprt *xprt)
2748c2ecf20Sopenharmony_ci{
2758c2ecf20Sopenharmony_ci	if (!list_empty(&xprt->xmit_queue)) {
2768c2ecf20Sopenharmony_ci		/* Peek at head of queue to see if it can make progress */
2778c2ecf20Sopenharmony_ci		if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
2788c2ecf20Sopenharmony_ci					rq_xmit)->rq_cong)
2798c2ecf20Sopenharmony_ci			return;
2808c2ecf20Sopenharmony_ci	}
2818c2ecf20Sopenharmony_ci	set_bit(XPRT_CWND_WAIT, &xprt->state);
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic void
2858c2ecf20Sopenharmony_cixprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
2868c2ecf20Sopenharmony_ci{
2878c2ecf20Sopenharmony_ci	if (!RPCXPRT_CONGESTED(xprt))
2888c2ecf20Sopenharmony_ci		clear_bit(XPRT_CWND_WAIT, &xprt->state);
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci/*
2928c2ecf20Sopenharmony_ci * xprt_reserve_xprt_cong - serialize write access to transports
2938c2ecf20Sopenharmony_ci * @task: task that is requesting access to the transport
2948c2ecf20Sopenharmony_ci *
2958c2ecf20Sopenharmony_ci * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
2968c2ecf20Sopenharmony_ci * integrated into the decision of whether a request is allowed to be
2978c2ecf20Sopenharmony_ci * woken up and given access to the transport.
2988c2ecf20Sopenharmony_ci * Note that the lock is only granted if we know there are free slots.
2998c2ecf20Sopenharmony_ci */
3008c2ecf20Sopenharmony_ciint xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
3058c2ecf20Sopenharmony_ci		if (task == xprt->snd_task)
3068c2ecf20Sopenharmony_ci			goto out_locked;
3078c2ecf20Sopenharmony_ci		goto out_sleep;
3088c2ecf20Sopenharmony_ci	}
3098c2ecf20Sopenharmony_ci	if (req == NULL) {
3108c2ecf20Sopenharmony_ci		xprt->snd_task = task;
3118c2ecf20Sopenharmony_ci		goto out_locked;
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ci	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
3148c2ecf20Sopenharmony_ci		goto out_unlock;
3158c2ecf20Sopenharmony_ci	if (!xprt_need_congestion_window_wait(xprt)) {
3168c2ecf20Sopenharmony_ci		xprt->snd_task = task;
3178c2ecf20Sopenharmony_ci		goto out_locked;
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ciout_unlock:
3208c2ecf20Sopenharmony_ci	xprt_clear_locked(xprt);
3218c2ecf20Sopenharmony_ciout_sleep:
3228c2ecf20Sopenharmony_ci	task->tk_status = -EAGAIN;
3238c2ecf20Sopenharmony_ci	if (RPC_IS_SOFT(task))
3248c2ecf20Sopenharmony_ci		rpc_sleep_on_timeout(&xprt->sending, task, NULL,
3258c2ecf20Sopenharmony_ci				xprt_request_timeout(req));
3268c2ecf20Sopenharmony_ci	else
3278c2ecf20Sopenharmony_ci		rpc_sleep_on(&xprt->sending, task, NULL);
3288c2ecf20Sopenharmony_ci	return 0;
3298c2ecf20Sopenharmony_ciout_locked:
3308c2ecf20Sopenharmony_ci	trace_xprt_reserve_cong(xprt, task);
3318c2ecf20Sopenharmony_ci	return 1;
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
3368c2ecf20Sopenharmony_ci{
3378c2ecf20Sopenharmony_ci	int retval;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
3408c2ecf20Sopenharmony_ci		return 1;
3418c2ecf20Sopenharmony_ci	spin_lock(&xprt->transport_lock);
3428c2ecf20Sopenharmony_ci	retval = xprt->ops->reserve_xprt(xprt, task);
3438c2ecf20Sopenharmony_ci	spin_unlock(&xprt->transport_lock);
3448c2ecf20Sopenharmony_ci	return retval;
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic bool __xprt_lock_write_func(struct rpc_task *task, void *data)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = data;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	xprt->snd_task = task;
3528c2ecf20Sopenharmony_ci	return true;
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_cistatic void __xprt_lock_write_next(struct rpc_xprt *xprt)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
3588c2ecf20Sopenharmony_ci		return;
3598c2ecf20Sopenharmony_ci	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
3608c2ecf20Sopenharmony_ci		goto out_unlock;
3618c2ecf20Sopenharmony_ci	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
3628c2ecf20Sopenharmony_ci				__xprt_lock_write_func, xprt))
3638c2ecf20Sopenharmony_ci		return;
3648c2ecf20Sopenharmony_ciout_unlock:
3658c2ecf20Sopenharmony_ci	xprt_clear_locked(xprt);
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_cistatic void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
3718c2ecf20Sopenharmony_ci		return;
3728c2ecf20Sopenharmony_ci	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
3738c2ecf20Sopenharmony_ci		goto out_unlock;
3748c2ecf20Sopenharmony_ci	if (xprt_need_congestion_window_wait(xprt))
3758c2ecf20Sopenharmony_ci		goto out_unlock;
3768c2ecf20Sopenharmony_ci	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
3778c2ecf20Sopenharmony_ci				__xprt_lock_write_func, xprt))
3788c2ecf20Sopenharmony_ci		return;
3798c2ecf20Sopenharmony_ciout_unlock:
3808c2ecf20Sopenharmony_ci	xprt_clear_locked(xprt);
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci/**
3848c2ecf20Sopenharmony_ci * xprt_release_xprt - allow other requests to use a transport
3858c2ecf20Sopenharmony_ci * @xprt: transport with other tasks potentially waiting
3868c2ecf20Sopenharmony_ci * @task: task that is releasing access to the transport
3878c2ecf20Sopenharmony_ci *
3888c2ecf20Sopenharmony_ci * Note that "task" can be NULL.  No congestion control is provided.
3898c2ecf20Sopenharmony_ci */
3908c2ecf20Sopenharmony_civoid xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
3918c2ecf20Sopenharmony_ci{
3928c2ecf20Sopenharmony_ci	if (xprt->snd_task == task) {
3938c2ecf20Sopenharmony_ci		xprt_clear_locked(xprt);
3948c2ecf20Sopenharmony_ci		__xprt_lock_write_next(xprt);
3958c2ecf20Sopenharmony_ci	}
3968c2ecf20Sopenharmony_ci	trace_xprt_release_xprt(xprt, task);
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_release_xprt);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci/**
4018c2ecf20Sopenharmony_ci * xprt_release_xprt_cong - allow other requests to use a transport
4028c2ecf20Sopenharmony_ci * @xprt: transport with other tasks potentially waiting
4038c2ecf20Sopenharmony_ci * @task: task that is releasing access to the transport
4048c2ecf20Sopenharmony_ci *
4058c2ecf20Sopenharmony_ci * Note that "task" can be NULL.  Another task is awoken to use the
4068c2ecf20Sopenharmony_ci * transport if the transport's congestion window allows it.
4078c2ecf20Sopenharmony_ci */
4088c2ecf20Sopenharmony_civoid xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	if (xprt->snd_task == task) {
4118c2ecf20Sopenharmony_ci		xprt_clear_locked(xprt);
4128c2ecf20Sopenharmony_ci		__xprt_lock_write_next_cong(xprt);
4138c2ecf20Sopenharmony_ci	}
4148c2ecf20Sopenharmony_ci	trace_xprt_release_cong(xprt, task);
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_cistatic inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
4198c2ecf20Sopenharmony_ci{
4208c2ecf20Sopenharmony_ci	if (xprt->snd_task != task)
4218c2ecf20Sopenharmony_ci		return;
4228c2ecf20Sopenharmony_ci	spin_lock(&xprt->transport_lock);
4238c2ecf20Sopenharmony_ci	xprt->ops->release_xprt(xprt, task);
4248c2ecf20Sopenharmony_ci	spin_unlock(&xprt->transport_lock);
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci/*
4288c2ecf20Sopenharmony_ci * Van Jacobson congestion avoidance. Check if the congestion window
4298c2ecf20Sopenharmony_ci * overflowed. Put the task to sleep if this is the case.
4308c2ecf20Sopenharmony_ci */
4318c2ecf20Sopenharmony_cistatic int
4328c2ecf20Sopenharmony_ci__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
4338c2ecf20Sopenharmony_ci{
4348c2ecf20Sopenharmony_ci	if (req->rq_cong)
4358c2ecf20Sopenharmony_ci		return 1;
4368c2ecf20Sopenharmony_ci	trace_xprt_get_cong(xprt, req->rq_task);
4378c2ecf20Sopenharmony_ci	if (RPCXPRT_CONGESTED(xprt)) {
4388c2ecf20Sopenharmony_ci		xprt_set_congestion_window_wait(xprt);
4398c2ecf20Sopenharmony_ci		return 0;
4408c2ecf20Sopenharmony_ci	}
4418c2ecf20Sopenharmony_ci	req->rq_cong = 1;
4428c2ecf20Sopenharmony_ci	xprt->cong += RPC_CWNDSCALE;
4438c2ecf20Sopenharmony_ci	return 1;
4448c2ecf20Sopenharmony_ci}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci/*
4478c2ecf20Sopenharmony_ci * Adjust the congestion window, and wake up the next task
4488c2ecf20Sopenharmony_ci * that has been sleeping due to congestion
4498c2ecf20Sopenharmony_ci */
4508c2ecf20Sopenharmony_cistatic void
4518c2ecf20Sopenharmony_ci__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	if (!req->rq_cong)
4548c2ecf20Sopenharmony_ci		return;
4558c2ecf20Sopenharmony_ci	req->rq_cong = 0;
4568c2ecf20Sopenharmony_ci	xprt->cong -= RPC_CWNDSCALE;
4578c2ecf20Sopenharmony_ci	xprt_test_and_clear_congestion_window_wait(xprt);
4588c2ecf20Sopenharmony_ci	trace_xprt_put_cong(xprt, req->rq_task);
4598c2ecf20Sopenharmony_ci	__xprt_lock_write_next_cong(xprt);
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci/**
4638c2ecf20Sopenharmony_ci * xprt_request_get_cong - Request congestion control credits
4648c2ecf20Sopenharmony_ci * @xprt: pointer to transport
4658c2ecf20Sopenharmony_ci * @req: pointer to RPC request
4668c2ecf20Sopenharmony_ci *
4678c2ecf20Sopenharmony_ci * Useful for transports that require congestion control.
4688c2ecf20Sopenharmony_ci */
4698c2ecf20Sopenharmony_cibool
4708c2ecf20Sopenharmony_cixprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
4718c2ecf20Sopenharmony_ci{
4728c2ecf20Sopenharmony_ci	bool ret = false;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	if (req->rq_cong)
4758c2ecf20Sopenharmony_ci		return true;
4768c2ecf20Sopenharmony_ci	spin_lock(&xprt->transport_lock);
4778c2ecf20Sopenharmony_ci	ret = __xprt_get_cong(xprt, req) != 0;
4788c2ecf20Sopenharmony_ci	spin_unlock(&xprt->transport_lock);
4798c2ecf20Sopenharmony_ci	return ret;
4808c2ecf20Sopenharmony_ci}
4818c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_request_get_cong);
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci/**
4848c2ecf20Sopenharmony_ci * xprt_release_rqst_cong - housekeeping when request is complete
4858c2ecf20Sopenharmony_ci * @task: RPC request that recently completed
4868c2ecf20Sopenharmony_ci *
4878c2ecf20Sopenharmony_ci * Useful for transports that require congestion control.
4888c2ecf20Sopenharmony_ci */
4898c2ecf20Sopenharmony_civoid xprt_release_rqst_cong(struct rpc_task *task)
4908c2ecf20Sopenharmony_ci{
4918c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci	__xprt_put_cong(req->rq_xprt, req);
4948c2ecf20Sopenharmony_ci}
4958c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_cistatic void xprt_clear_congestion_window_wait_locked(struct rpc_xprt *xprt)
4988c2ecf20Sopenharmony_ci{
4998c2ecf20Sopenharmony_ci	if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state))
5008c2ecf20Sopenharmony_ci		__xprt_lock_write_next_cong(xprt);
5018c2ecf20Sopenharmony_ci}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci/*
5048c2ecf20Sopenharmony_ci * Clear the congestion window wait flag and wake up the next
5058c2ecf20Sopenharmony_ci * entry on xprt->sending
5068c2ecf20Sopenharmony_ci */
5078c2ecf20Sopenharmony_cistatic void
5088c2ecf20Sopenharmony_cixprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
5098c2ecf20Sopenharmony_ci{
5108c2ecf20Sopenharmony_ci	if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
5118c2ecf20Sopenharmony_ci		spin_lock(&xprt->transport_lock);
5128c2ecf20Sopenharmony_ci		__xprt_lock_write_next_cong(xprt);
5138c2ecf20Sopenharmony_ci		spin_unlock(&xprt->transport_lock);
5148c2ecf20Sopenharmony_ci	}
5158c2ecf20Sopenharmony_ci}
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci/**
5188c2ecf20Sopenharmony_ci * xprt_adjust_cwnd - adjust transport congestion window
5198c2ecf20Sopenharmony_ci * @xprt: pointer to xprt
5208c2ecf20Sopenharmony_ci * @task: recently completed RPC request used to adjust window
5218c2ecf20Sopenharmony_ci * @result: result code of completed RPC request
5228c2ecf20Sopenharmony_ci *
5238c2ecf20Sopenharmony_ci * The transport code maintains an estimate on the maximum number of out-
5248c2ecf20Sopenharmony_ci * standing RPC requests, using a smoothed version of the congestion
5258c2ecf20Sopenharmony_ci * avoidance implemented in 44BSD. This is basically the Van Jacobson
5268c2ecf20Sopenharmony_ci * congestion algorithm: If a retransmit occurs, the congestion window is
5278c2ecf20Sopenharmony_ci * halved; otherwise, it is incremented by 1/cwnd when
5288c2ecf20Sopenharmony_ci *
5298c2ecf20Sopenharmony_ci *	-	a reply is received and
5308c2ecf20Sopenharmony_ci *	-	a full number of requests are outstanding and
5318c2ecf20Sopenharmony_ci *	-	the congestion window hasn't been updated recently.
5328c2ecf20Sopenharmony_ci */
5338c2ecf20Sopenharmony_civoid xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
5348c2ecf20Sopenharmony_ci{
5358c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
5368c2ecf20Sopenharmony_ci	unsigned long cwnd = xprt->cwnd;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	if (result >= 0 && cwnd <= xprt->cong) {
5398c2ecf20Sopenharmony_ci		/* The (cwnd >> 1) term makes sure
5408c2ecf20Sopenharmony_ci		 * the result gets rounded properly. */
5418c2ecf20Sopenharmony_ci		cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
5428c2ecf20Sopenharmony_ci		if (cwnd > RPC_MAXCWND(xprt))
5438c2ecf20Sopenharmony_ci			cwnd = RPC_MAXCWND(xprt);
5448c2ecf20Sopenharmony_ci		__xprt_lock_write_next_cong(xprt);
5458c2ecf20Sopenharmony_ci	} else if (result == -ETIMEDOUT) {
5468c2ecf20Sopenharmony_ci		cwnd >>= 1;
5478c2ecf20Sopenharmony_ci		if (cwnd < RPC_CWNDSCALE)
5488c2ecf20Sopenharmony_ci			cwnd = RPC_CWNDSCALE;
5498c2ecf20Sopenharmony_ci	}
5508c2ecf20Sopenharmony_ci	dprintk("RPC:       cong %ld, cwnd was %ld, now %ld\n",
5518c2ecf20Sopenharmony_ci			xprt->cong, xprt->cwnd, cwnd);
5528c2ecf20Sopenharmony_ci	xprt->cwnd = cwnd;
5538c2ecf20Sopenharmony_ci	__xprt_put_cong(xprt, req);
5548c2ecf20Sopenharmony_ci}
5558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci/**
5588c2ecf20Sopenharmony_ci * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
5598c2ecf20Sopenharmony_ci * @xprt: transport with waiting tasks
5608c2ecf20Sopenharmony_ci * @status: result code to plant in each task before waking it
5618c2ecf20Sopenharmony_ci *
5628c2ecf20Sopenharmony_ci */
5638c2ecf20Sopenharmony_civoid xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
5648c2ecf20Sopenharmony_ci{
5658c2ecf20Sopenharmony_ci	if (status < 0)
5668c2ecf20Sopenharmony_ci		rpc_wake_up_status(&xprt->pending, status);
5678c2ecf20Sopenharmony_ci	else
5688c2ecf20Sopenharmony_ci		rpc_wake_up(&xprt->pending);
5698c2ecf20Sopenharmony_ci}
5708c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci/**
5738c2ecf20Sopenharmony_ci * xprt_wait_for_buffer_space - wait for transport output buffer to clear
5748c2ecf20Sopenharmony_ci * @xprt: transport
5758c2ecf20Sopenharmony_ci *
5768c2ecf20Sopenharmony_ci * Note that we only set the timer for the case of RPC_IS_SOFT(), since
5778c2ecf20Sopenharmony_ci * we don't in general want to force a socket disconnection due to
5788c2ecf20Sopenharmony_ci * an incomplete RPC call transmission.
5798c2ecf20Sopenharmony_ci */
5808c2ecf20Sopenharmony_civoid xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
5818c2ecf20Sopenharmony_ci{
5828c2ecf20Sopenharmony_ci	set_bit(XPRT_WRITE_SPACE, &xprt->state);
5838c2ecf20Sopenharmony_ci}
5848c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_cistatic bool
5878c2ecf20Sopenharmony_cixprt_clear_write_space_locked(struct rpc_xprt *xprt)
5888c2ecf20Sopenharmony_ci{
5898c2ecf20Sopenharmony_ci	if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
5908c2ecf20Sopenharmony_ci		__xprt_lock_write_next(xprt);
5918c2ecf20Sopenharmony_ci		dprintk("RPC:       write space: waking waiting task on "
5928c2ecf20Sopenharmony_ci				"xprt %p\n", xprt);
5938c2ecf20Sopenharmony_ci		return true;
5948c2ecf20Sopenharmony_ci	}
5958c2ecf20Sopenharmony_ci	return false;
5968c2ecf20Sopenharmony_ci}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci/**
5998c2ecf20Sopenharmony_ci * xprt_write_space - wake the task waiting for transport output buffer space
6008c2ecf20Sopenharmony_ci * @xprt: transport with waiting tasks
6018c2ecf20Sopenharmony_ci *
6028c2ecf20Sopenharmony_ci * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
6038c2ecf20Sopenharmony_ci */
6048c2ecf20Sopenharmony_cibool xprt_write_space(struct rpc_xprt *xprt)
6058c2ecf20Sopenharmony_ci{
6068c2ecf20Sopenharmony_ci	bool ret;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
6098c2ecf20Sopenharmony_ci		return false;
6108c2ecf20Sopenharmony_ci	spin_lock(&xprt->transport_lock);
6118c2ecf20Sopenharmony_ci	ret = xprt_clear_write_space_locked(xprt);
6128c2ecf20Sopenharmony_ci	spin_unlock(&xprt->transport_lock);
6138c2ecf20Sopenharmony_ci	return ret;
6148c2ecf20Sopenharmony_ci}
6158c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_write_space);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_cistatic unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime)
6188c2ecf20Sopenharmony_ci{
6198c2ecf20Sopenharmony_ci	s64 delta = ktime_to_ns(ktime_get() - abstime);
6208c2ecf20Sopenharmony_ci	return likely(delta >= 0) ?
6218c2ecf20Sopenharmony_ci		jiffies - nsecs_to_jiffies(delta) :
6228c2ecf20Sopenharmony_ci		jiffies + nsecs_to_jiffies(-delta);
6238c2ecf20Sopenharmony_ci}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_cistatic unsigned long xprt_calc_majortimeo(struct rpc_rqst *req)
6268c2ecf20Sopenharmony_ci{
6278c2ecf20Sopenharmony_ci	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
6288c2ecf20Sopenharmony_ci	unsigned long majortimeo = req->rq_timeout;
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	if (to->to_exponential)
6318c2ecf20Sopenharmony_ci		majortimeo <<= to->to_retries;
6328c2ecf20Sopenharmony_ci	else
6338c2ecf20Sopenharmony_ci		majortimeo += to->to_increment * to->to_retries;
6348c2ecf20Sopenharmony_ci	if (majortimeo > to->to_maxval || majortimeo == 0)
6358c2ecf20Sopenharmony_ci		majortimeo = to->to_maxval;
6368c2ecf20Sopenharmony_ci	return majortimeo;
6378c2ecf20Sopenharmony_ci}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_cistatic void xprt_reset_majortimeo(struct rpc_rqst *req)
6408c2ecf20Sopenharmony_ci{
6418c2ecf20Sopenharmony_ci	req->rq_majortimeo += xprt_calc_majortimeo(req);
6428c2ecf20Sopenharmony_ci}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_cistatic void xprt_reset_minortimeo(struct rpc_rqst *req)
6458c2ecf20Sopenharmony_ci{
6468c2ecf20Sopenharmony_ci	req->rq_minortimeo += req->rq_timeout;
6478c2ecf20Sopenharmony_ci}
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_cistatic void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req)
6508c2ecf20Sopenharmony_ci{
6518c2ecf20Sopenharmony_ci	unsigned long time_init;
6528c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = req->rq_xprt;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	if (likely(xprt && xprt_connected(xprt)))
6558c2ecf20Sopenharmony_ci		time_init = jiffies;
6568c2ecf20Sopenharmony_ci	else
6578c2ecf20Sopenharmony_ci		time_init = xprt_abs_ktime_to_jiffies(task->tk_start);
6588c2ecf20Sopenharmony_ci	req->rq_timeout = task->tk_client->cl_timeout->to_initval;
6598c2ecf20Sopenharmony_ci	req->rq_majortimeo = time_init + xprt_calc_majortimeo(req);
6608c2ecf20Sopenharmony_ci	req->rq_minortimeo = time_init + req->rq_timeout;
6618c2ecf20Sopenharmony_ci}
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ci/**
6648c2ecf20Sopenharmony_ci * xprt_adjust_timeout - adjust timeout values for next retransmit
6658c2ecf20Sopenharmony_ci * @req: RPC request containing parameters to use for the adjustment
6668c2ecf20Sopenharmony_ci *
6678c2ecf20Sopenharmony_ci */
6688c2ecf20Sopenharmony_ciint xprt_adjust_timeout(struct rpc_rqst *req)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = req->rq_xprt;
6718c2ecf20Sopenharmony_ci	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
6728c2ecf20Sopenharmony_ci	int status = 0;
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	if (time_before(jiffies, req->rq_majortimeo)) {
6758c2ecf20Sopenharmony_ci		if (time_before(jiffies, req->rq_minortimeo))
6768c2ecf20Sopenharmony_ci			return status;
6778c2ecf20Sopenharmony_ci		if (to->to_exponential)
6788c2ecf20Sopenharmony_ci			req->rq_timeout <<= 1;
6798c2ecf20Sopenharmony_ci		else
6808c2ecf20Sopenharmony_ci			req->rq_timeout += to->to_increment;
6818c2ecf20Sopenharmony_ci		if (to->to_maxval && req->rq_timeout >= to->to_maxval)
6828c2ecf20Sopenharmony_ci			req->rq_timeout = to->to_maxval;
6838c2ecf20Sopenharmony_ci		req->rq_retries++;
6848c2ecf20Sopenharmony_ci	} else {
6858c2ecf20Sopenharmony_ci		req->rq_timeout = to->to_initval;
6868c2ecf20Sopenharmony_ci		req->rq_retries = 0;
6878c2ecf20Sopenharmony_ci		xprt_reset_majortimeo(req);
6888c2ecf20Sopenharmony_ci		/* Reset the RTT counters == "slow start" */
6898c2ecf20Sopenharmony_ci		spin_lock(&xprt->transport_lock);
6908c2ecf20Sopenharmony_ci		rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
6918c2ecf20Sopenharmony_ci		spin_unlock(&xprt->transport_lock);
6928c2ecf20Sopenharmony_ci		status = -ETIMEDOUT;
6938c2ecf20Sopenharmony_ci	}
6948c2ecf20Sopenharmony_ci	xprt_reset_minortimeo(req);
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	if (req->rq_timeout == 0) {
6978c2ecf20Sopenharmony_ci		printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
6988c2ecf20Sopenharmony_ci		req->rq_timeout = 5 * HZ;
6998c2ecf20Sopenharmony_ci	}
7008c2ecf20Sopenharmony_ci	return status;
7018c2ecf20Sopenharmony_ci}
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_cistatic void xprt_autoclose(struct work_struct *work)
7048c2ecf20Sopenharmony_ci{
7058c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt =
7068c2ecf20Sopenharmony_ci		container_of(work, struct rpc_xprt, task_cleanup);
7078c2ecf20Sopenharmony_ci	unsigned int pflags = memalloc_nofs_save();
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	trace_xprt_disconnect_auto(xprt);
7108c2ecf20Sopenharmony_ci	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
7118c2ecf20Sopenharmony_ci	xprt->ops->close(xprt);
7128c2ecf20Sopenharmony_ci	xprt_release_write(xprt, NULL);
7138c2ecf20Sopenharmony_ci	wake_up_bit(&xprt->state, XPRT_LOCKED);
7148c2ecf20Sopenharmony_ci	memalloc_nofs_restore(pflags);
7158c2ecf20Sopenharmony_ci}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci/**
7188c2ecf20Sopenharmony_ci * xprt_disconnect_done - mark a transport as disconnected
7198c2ecf20Sopenharmony_ci * @xprt: transport to flag for disconnect
7208c2ecf20Sopenharmony_ci *
7218c2ecf20Sopenharmony_ci */
7228c2ecf20Sopenharmony_civoid xprt_disconnect_done(struct rpc_xprt *xprt)
7238c2ecf20Sopenharmony_ci{
7248c2ecf20Sopenharmony_ci	trace_xprt_disconnect_done(xprt);
7258c2ecf20Sopenharmony_ci	spin_lock(&xprt->transport_lock);
7268c2ecf20Sopenharmony_ci	xprt_clear_connected(xprt);
7278c2ecf20Sopenharmony_ci	xprt_clear_write_space_locked(xprt);
7288c2ecf20Sopenharmony_ci	xprt_clear_congestion_window_wait_locked(xprt);
7298c2ecf20Sopenharmony_ci	xprt_wake_pending_tasks(xprt, -ENOTCONN);
7308c2ecf20Sopenharmony_ci	spin_unlock(&xprt->transport_lock);
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_disconnect_done);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci/**
7358c2ecf20Sopenharmony_ci * xprt_schedule_autoclose_locked - Try to schedule an autoclose RPC call
7368c2ecf20Sopenharmony_ci * @xprt: transport to disconnect
7378c2ecf20Sopenharmony_ci */
7388c2ecf20Sopenharmony_cistatic void xprt_schedule_autoclose_locked(struct rpc_xprt *xprt)
7398c2ecf20Sopenharmony_ci{
7408c2ecf20Sopenharmony_ci	if (test_and_set_bit(XPRT_CLOSE_WAIT, &xprt->state))
7418c2ecf20Sopenharmony_ci		return;
7428c2ecf20Sopenharmony_ci	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
7438c2ecf20Sopenharmony_ci		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
7448c2ecf20Sopenharmony_ci	else if (xprt->snd_task && !test_bit(XPRT_SND_IS_COOKIE, &xprt->state))
7458c2ecf20Sopenharmony_ci		rpc_wake_up_queued_task_set_status(&xprt->pending,
7468c2ecf20Sopenharmony_ci						   xprt->snd_task, -ENOTCONN);
7478c2ecf20Sopenharmony_ci}
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci/**
7508c2ecf20Sopenharmony_ci * xprt_force_disconnect - force a transport to disconnect
7518c2ecf20Sopenharmony_ci * @xprt: transport to disconnect
7528c2ecf20Sopenharmony_ci *
7538c2ecf20Sopenharmony_ci */
7548c2ecf20Sopenharmony_civoid xprt_force_disconnect(struct rpc_xprt *xprt)
7558c2ecf20Sopenharmony_ci{
7568c2ecf20Sopenharmony_ci	trace_xprt_disconnect_force(xprt);
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	/* Don't race with the test_bit() in xprt_clear_locked() */
7598c2ecf20Sopenharmony_ci	spin_lock(&xprt->transport_lock);
7608c2ecf20Sopenharmony_ci	xprt_schedule_autoclose_locked(xprt);
7618c2ecf20Sopenharmony_ci	spin_unlock(&xprt->transport_lock);
7628c2ecf20Sopenharmony_ci}
7638c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_force_disconnect);
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_cistatic unsigned int
7668c2ecf20Sopenharmony_cixprt_connect_cookie(struct rpc_xprt *xprt)
7678c2ecf20Sopenharmony_ci{
7688c2ecf20Sopenharmony_ci	return READ_ONCE(xprt->connect_cookie);
7698c2ecf20Sopenharmony_ci}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_cistatic bool
7728c2ecf20Sopenharmony_cixprt_request_retransmit_after_disconnect(struct rpc_task *task)
7738c2ecf20Sopenharmony_ci{
7748c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
7758c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = req->rq_xprt;
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
7788c2ecf20Sopenharmony_ci		!xprt_connected(xprt);
7798c2ecf20Sopenharmony_ci}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci/**
7828c2ecf20Sopenharmony_ci * xprt_conditional_disconnect - force a transport to disconnect
7838c2ecf20Sopenharmony_ci * @xprt: transport to disconnect
7848c2ecf20Sopenharmony_ci * @cookie: 'connection cookie'
7858c2ecf20Sopenharmony_ci *
7868c2ecf20Sopenharmony_ci * This attempts to break the connection if and only if 'cookie' matches
7878c2ecf20Sopenharmony_ci * the current transport 'connection cookie'. It ensures that we don't
7888c2ecf20Sopenharmony_ci * try to break the connection more than once when we need to retransmit
7898c2ecf20Sopenharmony_ci * a batch of RPC requests.
7908c2ecf20Sopenharmony_ci *
7918c2ecf20Sopenharmony_ci */
7928c2ecf20Sopenharmony_civoid xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
7938c2ecf20Sopenharmony_ci{
7948c2ecf20Sopenharmony_ci	/* Don't race with the test_bit() in xprt_clear_locked() */
7958c2ecf20Sopenharmony_ci	spin_lock(&xprt->transport_lock);
7968c2ecf20Sopenharmony_ci	if (cookie != xprt->connect_cookie)
7978c2ecf20Sopenharmony_ci		goto out;
7988c2ecf20Sopenharmony_ci	if (test_bit(XPRT_CLOSING, &xprt->state))
7998c2ecf20Sopenharmony_ci		goto out;
8008c2ecf20Sopenharmony_ci	xprt_schedule_autoclose_locked(xprt);
8018c2ecf20Sopenharmony_ciout:
8028c2ecf20Sopenharmony_ci	spin_unlock(&xprt->transport_lock);
8038c2ecf20Sopenharmony_ci}
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_cistatic bool
8068c2ecf20Sopenharmony_cixprt_has_timer(const struct rpc_xprt *xprt)
8078c2ecf20Sopenharmony_ci{
8088c2ecf20Sopenharmony_ci	return xprt->idle_timeout != 0;
8098c2ecf20Sopenharmony_ci}
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_cistatic void
8128c2ecf20Sopenharmony_cixprt_schedule_autodisconnect(struct rpc_xprt *xprt)
8138c2ecf20Sopenharmony_ci	__must_hold(&xprt->transport_lock)
8148c2ecf20Sopenharmony_ci{
8158c2ecf20Sopenharmony_ci	xprt->last_used = jiffies;
8168c2ecf20Sopenharmony_ci	if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
8178c2ecf20Sopenharmony_ci		mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
8188c2ecf20Sopenharmony_ci}
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_cistatic void
8218c2ecf20Sopenharmony_cixprt_init_autodisconnect(struct timer_list *t)
8228c2ecf20Sopenharmony_ci{
8238c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = from_timer(xprt, t, timer);
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	if (!RB_EMPTY_ROOT(&xprt->recv_queue))
8268c2ecf20Sopenharmony_ci		return;
8278c2ecf20Sopenharmony_ci	/* Reset xprt->last_used to avoid connect/autodisconnect cycling */
8288c2ecf20Sopenharmony_ci	xprt->last_used = jiffies;
8298c2ecf20Sopenharmony_ci	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
8308c2ecf20Sopenharmony_ci		return;
8318c2ecf20Sopenharmony_ci	queue_work(xprtiod_workqueue, &xprt->task_cleanup);
8328c2ecf20Sopenharmony_ci}
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_cibool xprt_lock_connect(struct rpc_xprt *xprt,
8358c2ecf20Sopenharmony_ci		struct rpc_task *task,
8368c2ecf20Sopenharmony_ci		void *cookie)
8378c2ecf20Sopenharmony_ci{
8388c2ecf20Sopenharmony_ci	bool ret = false;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	spin_lock(&xprt->transport_lock);
8418c2ecf20Sopenharmony_ci	if (!test_bit(XPRT_LOCKED, &xprt->state))
8428c2ecf20Sopenharmony_ci		goto out;
8438c2ecf20Sopenharmony_ci	if (xprt->snd_task != task)
8448c2ecf20Sopenharmony_ci		goto out;
8458c2ecf20Sopenharmony_ci	set_bit(XPRT_SND_IS_COOKIE, &xprt->state);
8468c2ecf20Sopenharmony_ci	xprt->snd_task = cookie;
8478c2ecf20Sopenharmony_ci	ret = true;
8488c2ecf20Sopenharmony_ciout:
8498c2ecf20Sopenharmony_ci	spin_unlock(&xprt->transport_lock);
8508c2ecf20Sopenharmony_ci	return ret;
8518c2ecf20Sopenharmony_ci}
8528c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_lock_connect);
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_civoid xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
8558c2ecf20Sopenharmony_ci{
8568c2ecf20Sopenharmony_ci	spin_lock(&xprt->transport_lock);
8578c2ecf20Sopenharmony_ci	if (xprt->snd_task != cookie)
8588c2ecf20Sopenharmony_ci		goto out;
8598c2ecf20Sopenharmony_ci	if (!test_bit(XPRT_LOCKED, &xprt->state))
8608c2ecf20Sopenharmony_ci		goto out;
8618c2ecf20Sopenharmony_ci	xprt->snd_task =NULL;
8628c2ecf20Sopenharmony_ci	clear_bit(XPRT_SND_IS_COOKIE, &xprt->state);
8638c2ecf20Sopenharmony_ci	xprt->ops->release_xprt(xprt, NULL);
8648c2ecf20Sopenharmony_ci	xprt_schedule_autodisconnect(xprt);
8658c2ecf20Sopenharmony_ciout:
8668c2ecf20Sopenharmony_ci	spin_unlock(&xprt->transport_lock);
8678c2ecf20Sopenharmony_ci	wake_up_bit(&xprt->state, XPRT_LOCKED);
8688c2ecf20Sopenharmony_ci}
8698c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_unlock_connect);
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci/**
8728c2ecf20Sopenharmony_ci * xprt_connect - schedule a transport connect operation
8738c2ecf20Sopenharmony_ci * @task: RPC task that is requesting the connect
8748c2ecf20Sopenharmony_ci *
8758c2ecf20Sopenharmony_ci */
8768c2ecf20Sopenharmony_civoid xprt_connect(struct rpc_task *task)
8778c2ecf20Sopenharmony_ci{
8788c2ecf20Sopenharmony_ci	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	trace_xprt_connect(xprt);
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	if (!xprt_bound(xprt)) {
8838c2ecf20Sopenharmony_ci		task->tk_status = -EAGAIN;
8848c2ecf20Sopenharmony_ci		return;
8858c2ecf20Sopenharmony_ci	}
8868c2ecf20Sopenharmony_ci	if (!xprt_lock_write(xprt, task))
8878c2ecf20Sopenharmony_ci		return;
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	if (!xprt_connected(xprt) && !test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
8908c2ecf20Sopenharmony_ci		task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
8918c2ecf20Sopenharmony_ci		rpc_sleep_on_timeout(&xprt->pending, task, NULL,
8928c2ecf20Sopenharmony_ci				xprt_request_timeout(task->tk_rqstp));
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci		if (test_bit(XPRT_CLOSING, &xprt->state))
8958c2ecf20Sopenharmony_ci			return;
8968c2ecf20Sopenharmony_ci		if (xprt_test_and_set_connecting(xprt))
8978c2ecf20Sopenharmony_ci			return;
8988c2ecf20Sopenharmony_ci		/* Race breaker */
8998c2ecf20Sopenharmony_ci		if (!xprt_connected(xprt)) {
9008c2ecf20Sopenharmony_ci			xprt->stat.connect_start = jiffies;
9018c2ecf20Sopenharmony_ci			xprt->ops->connect(xprt, task);
9028c2ecf20Sopenharmony_ci		} else {
9038c2ecf20Sopenharmony_ci			xprt_clear_connecting(xprt);
9048c2ecf20Sopenharmony_ci			task->tk_status = 0;
9058c2ecf20Sopenharmony_ci			rpc_wake_up_queued_task(&xprt->pending, task);
9068c2ecf20Sopenharmony_ci		}
9078c2ecf20Sopenharmony_ci	}
9088c2ecf20Sopenharmony_ci	xprt_release_write(xprt, task);
9098c2ecf20Sopenharmony_ci}
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci/**
9128c2ecf20Sopenharmony_ci * xprt_reconnect_delay - compute the wait before scheduling a connect
9138c2ecf20Sopenharmony_ci * @xprt: transport instance
9148c2ecf20Sopenharmony_ci *
9158c2ecf20Sopenharmony_ci */
9168c2ecf20Sopenharmony_ciunsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt)
9178c2ecf20Sopenharmony_ci{
9188c2ecf20Sopenharmony_ci	unsigned long start, now = jiffies;
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	start = xprt->stat.connect_start + xprt->reestablish_timeout;
9218c2ecf20Sopenharmony_ci	if (time_after(start, now))
9228c2ecf20Sopenharmony_ci		return start - now;
9238c2ecf20Sopenharmony_ci	return 0;
9248c2ecf20Sopenharmony_ci}
9258c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_reconnect_delay);
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci/**
9288c2ecf20Sopenharmony_ci * xprt_reconnect_backoff - compute the new re-establish timeout
9298c2ecf20Sopenharmony_ci * @xprt: transport instance
9308c2ecf20Sopenharmony_ci * @init_to: initial reestablish timeout
9318c2ecf20Sopenharmony_ci *
9328c2ecf20Sopenharmony_ci */
9338c2ecf20Sopenharmony_civoid xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to)
9348c2ecf20Sopenharmony_ci{
9358c2ecf20Sopenharmony_ci	xprt->reestablish_timeout <<= 1;
9368c2ecf20Sopenharmony_ci	if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
9378c2ecf20Sopenharmony_ci		xprt->reestablish_timeout = xprt->max_reconnect_timeout;
9388c2ecf20Sopenharmony_ci	if (xprt->reestablish_timeout < init_to)
9398c2ecf20Sopenharmony_ci		xprt->reestablish_timeout = init_to;
9408c2ecf20Sopenharmony_ci}
9418c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_reconnect_backoff);
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_cienum xprt_xid_rb_cmp {
9448c2ecf20Sopenharmony_ci	XID_RB_EQUAL,
9458c2ecf20Sopenharmony_ci	XID_RB_LEFT,
9468c2ecf20Sopenharmony_ci	XID_RB_RIGHT,
9478c2ecf20Sopenharmony_ci};
9488c2ecf20Sopenharmony_cistatic enum xprt_xid_rb_cmp
9498c2ecf20Sopenharmony_cixprt_xid_cmp(__be32 xid1, __be32 xid2)
9508c2ecf20Sopenharmony_ci{
9518c2ecf20Sopenharmony_ci	if (xid1 == xid2)
9528c2ecf20Sopenharmony_ci		return XID_RB_EQUAL;
9538c2ecf20Sopenharmony_ci	if ((__force u32)xid1 < (__force u32)xid2)
9548c2ecf20Sopenharmony_ci		return XID_RB_LEFT;
9558c2ecf20Sopenharmony_ci	return XID_RB_RIGHT;
9568c2ecf20Sopenharmony_ci}
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_cistatic struct rpc_rqst *
9598c2ecf20Sopenharmony_cixprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
9608c2ecf20Sopenharmony_ci{
9618c2ecf20Sopenharmony_ci	struct rb_node *n = xprt->recv_queue.rb_node;
9628c2ecf20Sopenharmony_ci	struct rpc_rqst *req;
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	while (n != NULL) {
9658c2ecf20Sopenharmony_ci		req = rb_entry(n, struct rpc_rqst, rq_recv);
9668c2ecf20Sopenharmony_ci		switch (xprt_xid_cmp(xid, req->rq_xid)) {
9678c2ecf20Sopenharmony_ci		case XID_RB_LEFT:
9688c2ecf20Sopenharmony_ci			n = n->rb_left;
9698c2ecf20Sopenharmony_ci			break;
9708c2ecf20Sopenharmony_ci		case XID_RB_RIGHT:
9718c2ecf20Sopenharmony_ci			n = n->rb_right;
9728c2ecf20Sopenharmony_ci			break;
9738c2ecf20Sopenharmony_ci		case XID_RB_EQUAL:
9748c2ecf20Sopenharmony_ci			return req;
9758c2ecf20Sopenharmony_ci		}
9768c2ecf20Sopenharmony_ci	}
9778c2ecf20Sopenharmony_ci	return NULL;
9788c2ecf20Sopenharmony_ci}
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_cistatic void
9818c2ecf20Sopenharmony_cixprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	struct rb_node **p = &xprt->recv_queue.rb_node;
9848c2ecf20Sopenharmony_ci	struct rb_node *n = NULL;
9858c2ecf20Sopenharmony_ci	struct rpc_rqst *req;
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	while (*p != NULL) {
9888c2ecf20Sopenharmony_ci		n = *p;
9898c2ecf20Sopenharmony_ci		req = rb_entry(n, struct rpc_rqst, rq_recv);
9908c2ecf20Sopenharmony_ci		switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
9918c2ecf20Sopenharmony_ci		case XID_RB_LEFT:
9928c2ecf20Sopenharmony_ci			p = &n->rb_left;
9938c2ecf20Sopenharmony_ci			break;
9948c2ecf20Sopenharmony_ci		case XID_RB_RIGHT:
9958c2ecf20Sopenharmony_ci			p = &n->rb_right;
9968c2ecf20Sopenharmony_ci			break;
9978c2ecf20Sopenharmony_ci		case XID_RB_EQUAL:
9988c2ecf20Sopenharmony_ci			WARN_ON_ONCE(new != req);
9998c2ecf20Sopenharmony_ci			return;
10008c2ecf20Sopenharmony_ci		}
10018c2ecf20Sopenharmony_ci	}
10028c2ecf20Sopenharmony_ci	rb_link_node(&new->rq_recv, n, p);
10038c2ecf20Sopenharmony_ci	rb_insert_color(&new->rq_recv, &xprt->recv_queue);
10048c2ecf20Sopenharmony_ci}
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_cistatic void
10078c2ecf20Sopenharmony_cixprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
10088c2ecf20Sopenharmony_ci{
10098c2ecf20Sopenharmony_ci	rb_erase(&req->rq_recv, &xprt->recv_queue);
10108c2ecf20Sopenharmony_ci}
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci/**
10138c2ecf20Sopenharmony_ci * xprt_lookup_rqst - find an RPC request corresponding to an XID
10148c2ecf20Sopenharmony_ci * @xprt: transport on which the original request was transmitted
10158c2ecf20Sopenharmony_ci * @xid: RPC XID of incoming reply
10168c2ecf20Sopenharmony_ci *
10178c2ecf20Sopenharmony_ci * Caller holds xprt->queue_lock.
10188c2ecf20Sopenharmony_ci */
10198c2ecf20Sopenharmony_cistruct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
10208c2ecf20Sopenharmony_ci{
10218c2ecf20Sopenharmony_ci	struct rpc_rqst *entry;
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	entry = xprt_request_rb_find(xprt, xid);
10248c2ecf20Sopenharmony_ci	if (entry != NULL) {
10258c2ecf20Sopenharmony_ci		trace_xprt_lookup_rqst(xprt, xid, 0);
10268c2ecf20Sopenharmony_ci		entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
10278c2ecf20Sopenharmony_ci		return entry;
10288c2ecf20Sopenharmony_ci	}
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci	dprintk("RPC:       xprt_lookup_rqst did not find xid %08x\n",
10318c2ecf20Sopenharmony_ci			ntohl(xid));
10328c2ecf20Sopenharmony_ci	trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
10338c2ecf20Sopenharmony_ci	xprt->stat.bad_xids++;
10348c2ecf20Sopenharmony_ci	return NULL;
10358c2ecf20Sopenharmony_ci}
10368c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_lookup_rqst);
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_cistatic bool
10398c2ecf20Sopenharmony_cixprt_is_pinned_rqst(struct rpc_rqst *req)
10408c2ecf20Sopenharmony_ci{
10418c2ecf20Sopenharmony_ci	return atomic_read(&req->rq_pin) != 0;
10428c2ecf20Sopenharmony_ci}
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci/**
10458c2ecf20Sopenharmony_ci * xprt_pin_rqst - Pin a request on the transport receive list
10468c2ecf20Sopenharmony_ci * @req: Request to pin
10478c2ecf20Sopenharmony_ci *
10488c2ecf20Sopenharmony_ci * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
10498c2ecf20Sopenharmony_ci * so should be holding xprt->queue_lock.
10508c2ecf20Sopenharmony_ci */
10518c2ecf20Sopenharmony_civoid xprt_pin_rqst(struct rpc_rqst *req)
10528c2ecf20Sopenharmony_ci{
10538c2ecf20Sopenharmony_ci	atomic_inc(&req->rq_pin);
10548c2ecf20Sopenharmony_ci}
10558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_pin_rqst);
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci/**
10588c2ecf20Sopenharmony_ci * xprt_unpin_rqst - Unpin a request on the transport receive list
10598c2ecf20Sopenharmony_ci * @req: Request to pin
10608c2ecf20Sopenharmony_ci *
10618c2ecf20Sopenharmony_ci * Caller should be holding xprt->queue_lock.
10628c2ecf20Sopenharmony_ci */
10638c2ecf20Sopenharmony_civoid xprt_unpin_rqst(struct rpc_rqst *req)
10648c2ecf20Sopenharmony_ci{
10658c2ecf20Sopenharmony_ci	if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
10668c2ecf20Sopenharmony_ci		atomic_dec(&req->rq_pin);
10678c2ecf20Sopenharmony_ci		return;
10688c2ecf20Sopenharmony_ci	}
10698c2ecf20Sopenharmony_ci	if (atomic_dec_and_test(&req->rq_pin))
10708c2ecf20Sopenharmony_ci		wake_up_var(&req->rq_pin);
10718c2ecf20Sopenharmony_ci}
10728c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_unpin_rqst);
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_cistatic void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
10758c2ecf20Sopenharmony_ci{
10768c2ecf20Sopenharmony_ci	wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
10778c2ecf20Sopenharmony_ci}
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_cistatic bool
10808c2ecf20Sopenharmony_cixprt_request_data_received(struct rpc_task *task)
10818c2ecf20Sopenharmony_ci{
10828c2ecf20Sopenharmony_ci	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
10838c2ecf20Sopenharmony_ci		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
10848c2ecf20Sopenharmony_ci}
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_cistatic bool
10878c2ecf20Sopenharmony_cixprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
10888c2ecf20Sopenharmony_ci{
10898c2ecf20Sopenharmony_ci	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
10908c2ecf20Sopenharmony_ci		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
10918c2ecf20Sopenharmony_ci}
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci/**
10948c2ecf20Sopenharmony_ci * xprt_request_enqueue_receive - Add an request to the receive queue
10958c2ecf20Sopenharmony_ci * @task: RPC task
10968c2ecf20Sopenharmony_ci *
10978c2ecf20Sopenharmony_ci */
10988c2ecf20Sopenharmony_civoid
10998c2ecf20Sopenharmony_cixprt_request_enqueue_receive(struct rpc_task *task)
11008c2ecf20Sopenharmony_ci{
11018c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
11028c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = req->rq_xprt;
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_ci	if (!xprt_request_need_enqueue_receive(task, req))
11058c2ecf20Sopenharmony_ci		return;
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci	xprt_request_prepare(task->tk_rqstp);
11088c2ecf20Sopenharmony_ci	spin_lock(&xprt->queue_lock);
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	/* Update the softirq receive buffer */
11118c2ecf20Sopenharmony_ci	memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
11128c2ecf20Sopenharmony_ci			sizeof(req->rq_private_buf));
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci	/* Add request to the receive list */
11158c2ecf20Sopenharmony_ci	xprt_request_rb_insert(xprt, req);
11168c2ecf20Sopenharmony_ci	set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
11178c2ecf20Sopenharmony_ci	spin_unlock(&xprt->queue_lock);
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci	/* Turn off autodisconnect */
11208c2ecf20Sopenharmony_ci	del_singleshot_timer_sync(&xprt->timer);
11218c2ecf20Sopenharmony_ci}
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci/**
11248c2ecf20Sopenharmony_ci * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
11258c2ecf20Sopenharmony_ci * @task: RPC task
11268c2ecf20Sopenharmony_ci *
11278c2ecf20Sopenharmony_ci * Caller must hold xprt->queue_lock.
11288c2ecf20Sopenharmony_ci */
11298c2ecf20Sopenharmony_cistatic void
11308c2ecf20Sopenharmony_cixprt_request_dequeue_receive_locked(struct rpc_task *task)
11318c2ecf20Sopenharmony_ci{
11328c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci	if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
11358c2ecf20Sopenharmony_ci		xprt_request_rb_remove(req->rq_xprt, req);
11368c2ecf20Sopenharmony_ci}
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci/**
11398c2ecf20Sopenharmony_ci * xprt_update_rtt - Update RPC RTT statistics
11408c2ecf20Sopenharmony_ci * @task: RPC request that recently completed
11418c2ecf20Sopenharmony_ci *
11428c2ecf20Sopenharmony_ci * Caller holds xprt->queue_lock.
11438c2ecf20Sopenharmony_ci */
11448c2ecf20Sopenharmony_civoid xprt_update_rtt(struct rpc_task *task)
11458c2ecf20Sopenharmony_ci{
11468c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
11478c2ecf20Sopenharmony_ci	struct rpc_rtt *rtt = task->tk_client->cl_rtt;
11488c2ecf20Sopenharmony_ci	unsigned int timer = task->tk_msg.rpc_proc->p_timer;
11498c2ecf20Sopenharmony_ci	long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_ci	if (timer) {
11528c2ecf20Sopenharmony_ci		if (req->rq_ntrans == 1)
11538c2ecf20Sopenharmony_ci			rpc_update_rtt(rtt, timer, m);
11548c2ecf20Sopenharmony_ci		rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
11558c2ecf20Sopenharmony_ci	}
11568c2ecf20Sopenharmony_ci}
11578c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_update_rtt);
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci/**
11608c2ecf20Sopenharmony_ci * xprt_complete_rqst - called when reply processing is complete
11618c2ecf20Sopenharmony_ci * @task: RPC request that recently completed
11628c2ecf20Sopenharmony_ci * @copied: actual number of bytes received from the transport
11638c2ecf20Sopenharmony_ci *
11648c2ecf20Sopenharmony_ci * Caller holds xprt->queue_lock.
11658c2ecf20Sopenharmony_ci */
11668c2ecf20Sopenharmony_civoid xprt_complete_rqst(struct rpc_task *task, int copied)
11678c2ecf20Sopenharmony_ci{
11688c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
11698c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = req->rq_xprt;
11708c2ecf20Sopenharmony_ci
11718c2ecf20Sopenharmony_ci	xprt->stat.recvs++;
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci	req->rq_private_buf.len = copied;
11748c2ecf20Sopenharmony_ci	/* Ensure all writes are done before we update */
11758c2ecf20Sopenharmony_ci	/* req->rq_reply_bytes_recvd */
11768c2ecf20Sopenharmony_ci	smp_wmb();
11778c2ecf20Sopenharmony_ci	req->rq_reply_bytes_recvd = copied;
11788c2ecf20Sopenharmony_ci	xprt_request_dequeue_receive_locked(task);
11798c2ecf20Sopenharmony_ci	rpc_wake_up_queued_task(&xprt->pending, task);
11808c2ecf20Sopenharmony_ci}
11818c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_complete_rqst);
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_cistatic void xprt_timer(struct rpc_task *task)
11848c2ecf20Sopenharmony_ci{
11858c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
11868c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = req->rq_xprt;
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	if (task->tk_status != -ETIMEDOUT)
11898c2ecf20Sopenharmony_ci		return;
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci	trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
11928c2ecf20Sopenharmony_ci	if (!req->rq_reply_bytes_recvd) {
11938c2ecf20Sopenharmony_ci		if (xprt->ops->timer)
11948c2ecf20Sopenharmony_ci			xprt->ops->timer(xprt, task);
11958c2ecf20Sopenharmony_ci	} else
11968c2ecf20Sopenharmony_ci		task->tk_status = 0;
11978c2ecf20Sopenharmony_ci}
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_ci/**
12008c2ecf20Sopenharmony_ci * xprt_wait_for_reply_request_def - wait for reply
12018c2ecf20Sopenharmony_ci * @task: pointer to rpc_task
12028c2ecf20Sopenharmony_ci *
12038c2ecf20Sopenharmony_ci * Set a request's retransmit timeout based on the transport's
12048c2ecf20Sopenharmony_ci * default timeout parameters.  Used by transports that don't adjust
12058c2ecf20Sopenharmony_ci * the retransmit timeout based on round-trip time estimation,
12068c2ecf20Sopenharmony_ci * and put the task to sleep on the pending queue.
12078c2ecf20Sopenharmony_ci */
12088c2ecf20Sopenharmony_civoid xprt_wait_for_reply_request_def(struct rpc_task *task)
12098c2ecf20Sopenharmony_ci{
12108c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ci	rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
12138c2ecf20Sopenharmony_ci			xprt_request_timeout(req));
12148c2ecf20Sopenharmony_ci}
12158c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci/**
12188c2ecf20Sopenharmony_ci * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
12198c2ecf20Sopenharmony_ci * @task: pointer to rpc_task
12208c2ecf20Sopenharmony_ci *
12218c2ecf20Sopenharmony_ci * Set a request's retransmit timeout using the RTT estimator,
12228c2ecf20Sopenharmony_ci * and put the task to sleep on the pending queue.
12238c2ecf20Sopenharmony_ci */
12248c2ecf20Sopenharmony_civoid xprt_wait_for_reply_request_rtt(struct rpc_task *task)
12258c2ecf20Sopenharmony_ci{
12268c2ecf20Sopenharmony_ci	int timer = task->tk_msg.rpc_proc->p_timer;
12278c2ecf20Sopenharmony_ci	struct rpc_clnt *clnt = task->tk_client;
12288c2ecf20Sopenharmony_ci	struct rpc_rtt *rtt = clnt->cl_rtt;
12298c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
12308c2ecf20Sopenharmony_ci	unsigned long max_timeout = clnt->cl_timeout->to_maxval;
12318c2ecf20Sopenharmony_ci	unsigned long timeout;
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci	timeout = rpc_calc_rto(rtt, timer);
12348c2ecf20Sopenharmony_ci	timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
12358c2ecf20Sopenharmony_ci	if (timeout > max_timeout || timeout == 0)
12368c2ecf20Sopenharmony_ci		timeout = max_timeout;
12378c2ecf20Sopenharmony_ci	rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
12388c2ecf20Sopenharmony_ci			jiffies + timeout);
12398c2ecf20Sopenharmony_ci}
12408c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci/**
12438c2ecf20Sopenharmony_ci * xprt_request_wait_receive - wait for the reply to an RPC request
12448c2ecf20Sopenharmony_ci * @task: RPC task about to send a request
12458c2ecf20Sopenharmony_ci *
12468c2ecf20Sopenharmony_ci */
12478c2ecf20Sopenharmony_civoid xprt_request_wait_receive(struct rpc_task *task)
12488c2ecf20Sopenharmony_ci{
12498c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
12508c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = req->rq_xprt;
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci	if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
12538c2ecf20Sopenharmony_ci		return;
12548c2ecf20Sopenharmony_ci	/*
12558c2ecf20Sopenharmony_ci	 * Sleep on the pending queue if we're expecting a reply.
12568c2ecf20Sopenharmony_ci	 * The spinlock ensures atomicity between the test of
12578c2ecf20Sopenharmony_ci	 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
12588c2ecf20Sopenharmony_ci	 */
12598c2ecf20Sopenharmony_ci	spin_lock(&xprt->queue_lock);
12608c2ecf20Sopenharmony_ci	if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
12618c2ecf20Sopenharmony_ci		xprt->ops->wait_for_reply_request(task);
12628c2ecf20Sopenharmony_ci		/*
12638c2ecf20Sopenharmony_ci		 * Send an extra queue wakeup call if the
12648c2ecf20Sopenharmony_ci		 * connection was dropped in case the call to
12658c2ecf20Sopenharmony_ci		 * rpc_sleep_on() raced.
12668c2ecf20Sopenharmony_ci		 */
12678c2ecf20Sopenharmony_ci		if (xprt_request_retransmit_after_disconnect(task))
12688c2ecf20Sopenharmony_ci			rpc_wake_up_queued_task_set_status(&xprt->pending,
12698c2ecf20Sopenharmony_ci					task, -ENOTCONN);
12708c2ecf20Sopenharmony_ci	}
12718c2ecf20Sopenharmony_ci	spin_unlock(&xprt->queue_lock);
12728c2ecf20Sopenharmony_ci}
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_cistatic bool
12758c2ecf20Sopenharmony_cixprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
12768c2ecf20Sopenharmony_ci{
12778c2ecf20Sopenharmony_ci	return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
12788c2ecf20Sopenharmony_ci}
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci/**
12818c2ecf20Sopenharmony_ci * xprt_request_enqueue_transmit - queue a task for transmission
12828c2ecf20Sopenharmony_ci * @task: pointer to rpc_task
12838c2ecf20Sopenharmony_ci *
12848c2ecf20Sopenharmony_ci * Add a task to the transmission queue.
12858c2ecf20Sopenharmony_ci */
12868c2ecf20Sopenharmony_civoid
12878c2ecf20Sopenharmony_cixprt_request_enqueue_transmit(struct rpc_task *task)
12888c2ecf20Sopenharmony_ci{
12898c2ecf20Sopenharmony_ci	struct rpc_rqst *pos, *req = task->tk_rqstp;
12908c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = req->rq_xprt;
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	if (xprt_request_need_enqueue_transmit(task, req)) {
12938c2ecf20Sopenharmony_ci		req->rq_bytes_sent = 0;
12948c2ecf20Sopenharmony_ci		spin_lock(&xprt->queue_lock);
12958c2ecf20Sopenharmony_ci		/*
12968c2ecf20Sopenharmony_ci		 * Requests that carry congestion control credits are added
12978c2ecf20Sopenharmony_ci		 * to the head of the list to avoid starvation issues.
12988c2ecf20Sopenharmony_ci		 */
12998c2ecf20Sopenharmony_ci		if (req->rq_cong) {
13008c2ecf20Sopenharmony_ci			xprt_clear_congestion_window_wait(xprt);
13018c2ecf20Sopenharmony_ci			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
13028c2ecf20Sopenharmony_ci				if (pos->rq_cong)
13038c2ecf20Sopenharmony_ci					continue;
13048c2ecf20Sopenharmony_ci				/* Note: req is added _before_ pos */
13058c2ecf20Sopenharmony_ci				list_add_tail(&req->rq_xmit, &pos->rq_xmit);
13068c2ecf20Sopenharmony_ci				INIT_LIST_HEAD(&req->rq_xmit2);
13078c2ecf20Sopenharmony_ci				goto out;
13088c2ecf20Sopenharmony_ci			}
13098c2ecf20Sopenharmony_ci		} else if (!req->rq_seqno) {
13108c2ecf20Sopenharmony_ci			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
13118c2ecf20Sopenharmony_ci				if (pos->rq_task->tk_owner != task->tk_owner)
13128c2ecf20Sopenharmony_ci					continue;
13138c2ecf20Sopenharmony_ci				list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
13148c2ecf20Sopenharmony_ci				INIT_LIST_HEAD(&req->rq_xmit);
13158c2ecf20Sopenharmony_ci				goto out;
13168c2ecf20Sopenharmony_ci			}
13178c2ecf20Sopenharmony_ci		}
13188c2ecf20Sopenharmony_ci		list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
13198c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&req->rq_xmit2);
13208c2ecf20Sopenharmony_ciout:
13218c2ecf20Sopenharmony_ci		set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
13228c2ecf20Sopenharmony_ci		spin_unlock(&xprt->queue_lock);
13238c2ecf20Sopenharmony_ci	}
13248c2ecf20Sopenharmony_ci}
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci/**
13278c2ecf20Sopenharmony_ci * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
13288c2ecf20Sopenharmony_ci * @task: pointer to rpc_task
13298c2ecf20Sopenharmony_ci *
13308c2ecf20Sopenharmony_ci * Remove a task from the transmission queue
13318c2ecf20Sopenharmony_ci * Caller must hold xprt->queue_lock
13328c2ecf20Sopenharmony_ci */
13338c2ecf20Sopenharmony_cistatic void
13348c2ecf20Sopenharmony_cixprt_request_dequeue_transmit_locked(struct rpc_task *task)
13358c2ecf20Sopenharmony_ci{
13368c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
13378c2ecf20Sopenharmony_ci
13388c2ecf20Sopenharmony_ci	if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
13398c2ecf20Sopenharmony_ci		return;
13408c2ecf20Sopenharmony_ci	if (!list_empty(&req->rq_xmit)) {
13418c2ecf20Sopenharmony_ci		list_del(&req->rq_xmit);
13428c2ecf20Sopenharmony_ci		if (!list_empty(&req->rq_xmit2)) {
13438c2ecf20Sopenharmony_ci			struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
13448c2ecf20Sopenharmony_ci					struct rpc_rqst, rq_xmit2);
13458c2ecf20Sopenharmony_ci			list_del(&req->rq_xmit2);
13468c2ecf20Sopenharmony_ci			list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
13478c2ecf20Sopenharmony_ci		}
13488c2ecf20Sopenharmony_ci	} else
13498c2ecf20Sopenharmony_ci		list_del(&req->rq_xmit2);
13508c2ecf20Sopenharmony_ci}
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_ci/**
13538c2ecf20Sopenharmony_ci * xprt_request_dequeue_transmit - remove a task from the transmission queue
13548c2ecf20Sopenharmony_ci * @task: pointer to rpc_task
13558c2ecf20Sopenharmony_ci *
13568c2ecf20Sopenharmony_ci * Remove a task from the transmission queue
13578c2ecf20Sopenharmony_ci */
13588c2ecf20Sopenharmony_cistatic void
13598c2ecf20Sopenharmony_cixprt_request_dequeue_transmit(struct rpc_task *task)
13608c2ecf20Sopenharmony_ci{
13618c2ecf20Sopenharmony_ci	struct rpc_rqst *req = task->tk_rqstp;
13628c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = req->rq_xprt;
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci	spin_lock(&xprt->queue_lock);
13658c2ecf20Sopenharmony_ci	xprt_request_dequeue_transmit_locked(task);
13668c2ecf20Sopenharmony_ci	spin_unlock(&xprt->queue_lock);
13678c2ecf20Sopenharmony_ci}
13688c2ecf20Sopenharmony_ci
13698c2ecf20Sopenharmony_ci/**
13708c2ecf20Sopenharmony_ci * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue
13718c2ecf20Sopenharmony_ci * @task: pointer to rpc_task
13728c2ecf20Sopenharmony_ci *
13738c2ecf20Sopenharmony_ci * Remove a task from the transmit and receive queues, and ensure that
13748c2ecf20Sopenharmony_ci * it is not pinned by the receive work item.
13758c2ecf20Sopenharmony_ci */
13768c2ecf20Sopenharmony_civoid
13778c2ecf20Sopenharmony_cixprt_request_dequeue_xprt(struct rpc_task *task)
13788c2ecf20Sopenharmony_ci{
13798c2ecf20Sopenharmony_ci	struct rpc_rqst	*req = task->tk_rqstp;
13808c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = req->rq_xprt;
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_ci	if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
13838c2ecf20Sopenharmony_ci	    test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
13848c2ecf20Sopenharmony_ci	    xprt_is_pinned_rqst(req)) {
13858c2ecf20Sopenharmony_ci		spin_lock(&xprt->queue_lock);
13868c2ecf20Sopenharmony_ci		xprt_request_dequeue_transmit_locked(task);
13878c2ecf20Sopenharmony_ci		xprt_request_dequeue_receive_locked(task);
13888c2ecf20Sopenharmony_ci		while (xprt_is_pinned_rqst(req)) {
13898c2ecf20Sopenharmony_ci			set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
13908c2ecf20Sopenharmony_ci			spin_unlock(&xprt->queue_lock);
13918c2ecf20Sopenharmony_ci			xprt_wait_on_pinned_rqst(req);
13928c2ecf20Sopenharmony_ci			spin_lock(&xprt->queue_lock);
13938c2ecf20Sopenharmony_ci			clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
13948c2ecf20Sopenharmony_ci		}
13958c2ecf20Sopenharmony_ci		spin_unlock(&xprt->queue_lock);
13968c2ecf20Sopenharmony_ci	}
13978c2ecf20Sopenharmony_ci}
13988c2ecf20Sopenharmony_ci
13998c2ecf20Sopenharmony_ci/**
14008c2ecf20Sopenharmony_ci * xprt_request_prepare - prepare an encoded request for transport
14018c2ecf20Sopenharmony_ci * @req: pointer to rpc_rqst
14028c2ecf20Sopenharmony_ci *
14038c2ecf20Sopenharmony_ci * Calls into the transport layer to do whatever is needed to prepare
14048c2ecf20Sopenharmony_ci * the request for transmission or receive.
14058c2ecf20Sopenharmony_ci */
14068c2ecf20Sopenharmony_civoid
14078c2ecf20Sopenharmony_cixprt_request_prepare(struct rpc_rqst *req)
14088c2ecf20Sopenharmony_ci{
14098c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = req->rq_xprt;
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_ci	if (xprt->ops->prepare_request)
14128c2ecf20Sopenharmony_ci		xprt->ops->prepare_request(req);
14138c2ecf20Sopenharmony_ci}
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_ci/**
14168c2ecf20Sopenharmony_ci * xprt_request_need_retransmit - Test if a task needs retransmission
14178c2ecf20Sopenharmony_ci * @task: pointer to rpc_task
14188c2ecf20Sopenharmony_ci *
14198c2ecf20Sopenharmony_ci * Test for whether a connection breakage requires the task to retransmit
14208c2ecf20Sopenharmony_ci */
14218c2ecf20Sopenharmony_cibool
14228c2ecf20Sopenharmony_cixprt_request_need_retransmit(struct rpc_task *task)
14238c2ecf20Sopenharmony_ci{
14248c2ecf20Sopenharmony_ci	return xprt_request_retransmit_after_disconnect(task);
14258c2ecf20Sopenharmony_ci}
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ci/**
14288c2ecf20Sopenharmony_ci * xprt_prepare_transmit - reserve the transport before sending a request
14298c2ecf20Sopenharmony_ci * @task: RPC task about to send a request
14308c2ecf20Sopenharmony_ci *
14318c2ecf20Sopenharmony_ci */
14328c2ecf20Sopenharmony_cibool xprt_prepare_transmit(struct rpc_task *task)
14338c2ecf20Sopenharmony_ci{
14348c2ecf20Sopenharmony_ci	struct rpc_rqst	*req = task->tk_rqstp;
14358c2ecf20Sopenharmony_ci	struct rpc_xprt	*xprt = req->rq_xprt;
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_ci	if (!xprt_lock_write(xprt, task)) {
14388c2ecf20Sopenharmony_ci		/* Race breaker: someone may have transmitted us */
14398c2ecf20Sopenharmony_ci		if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
14408c2ecf20Sopenharmony_ci			rpc_wake_up_queued_task_set_status(&xprt->sending,
14418c2ecf20Sopenharmony_ci					task, 0);
14428c2ecf20Sopenharmony_ci		return false;
14438c2ecf20Sopenharmony_ci
14448c2ecf20Sopenharmony_ci	}
14458c2ecf20Sopenharmony_ci	return true;
14468c2ecf20Sopenharmony_ci}
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_civoid xprt_end_transmit(struct rpc_task *task)
14498c2ecf20Sopenharmony_ci{
14508c2ecf20Sopenharmony_ci	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
14518c2ecf20Sopenharmony_ci
14528c2ecf20Sopenharmony_ci	xprt_inject_disconnect(xprt);
14538c2ecf20Sopenharmony_ci	xprt_release_write(xprt, task);
14548c2ecf20Sopenharmony_ci}
14558c2ecf20Sopenharmony_ci
14568c2ecf20Sopenharmony_ci/**
14578c2ecf20Sopenharmony_ci * xprt_request_transmit - send an RPC request on a transport
14588c2ecf20Sopenharmony_ci * @req: pointer to request to transmit
14598c2ecf20Sopenharmony_ci * @snd_task: RPC task that owns the transport lock
14608c2ecf20Sopenharmony_ci *
14618c2ecf20Sopenharmony_ci * This performs the transmission of a single request.
14628c2ecf20Sopenharmony_ci * Note that if the request is not the same as snd_task, then it
14638c2ecf20Sopenharmony_ci * does need to be pinned.
14648c2ecf20Sopenharmony_ci * Returns '0' on success.
14658c2ecf20Sopenharmony_ci */
14668c2ecf20Sopenharmony_cistatic int
14678c2ecf20Sopenharmony_cixprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
14688c2ecf20Sopenharmony_ci{
14698c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = req->rq_xprt;
14708c2ecf20Sopenharmony_ci	struct rpc_task *task = req->rq_task;
14718c2ecf20Sopenharmony_ci	unsigned int connect_cookie;
14728c2ecf20Sopenharmony_ci	int is_retrans = RPC_WAS_SENT(task);
14738c2ecf20Sopenharmony_ci	int status;
14748c2ecf20Sopenharmony_ci
14758c2ecf20Sopenharmony_ci	if (!req->rq_bytes_sent) {
14768c2ecf20Sopenharmony_ci		if (xprt_request_data_received(task)) {
14778c2ecf20Sopenharmony_ci			status = 0;
14788c2ecf20Sopenharmony_ci			goto out_dequeue;
14798c2ecf20Sopenharmony_ci		}
14808c2ecf20Sopenharmony_ci		/* Verify that our message lies in the RPCSEC_GSS window */
14818c2ecf20Sopenharmony_ci		if (rpcauth_xmit_need_reencode(task)) {
14828c2ecf20Sopenharmony_ci			status = -EBADMSG;
14838c2ecf20Sopenharmony_ci			goto out_dequeue;
14848c2ecf20Sopenharmony_ci		}
14858c2ecf20Sopenharmony_ci		if (RPC_SIGNALLED(task)) {
14868c2ecf20Sopenharmony_ci			status = -ERESTARTSYS;
14878c2ecf20Sopenharmony_ci			goto out_dequeue;
14888c2ecf20Sopenharmony_ci		}
14898c2ecf20Sopenharmony_ci	}
14908c2ecf20Sopenharmony_ci
14918c2ecf20Sopenharmony_ci	/*
14928c2ecf20Sopenharmony_ci	 * Update req->rq_ntrans before transmitting to avoid races with
14938c2ecf20Sopenharmony_ci	 * xprt_update_rtt(), which needs to know that it is recording a
14948c2ecf20Sopenharmony_ci	 * reply to the first transmission.
14958c2ecf20Sopenharmony_ci	 */
14968c2ecf20Sopenharmony_ci	req->rq_ntrans++;
14978c2ecf20Sopenharmony_ci
14988c2ecf20Sopenharmony_ci	trace_rpc_xdr_sendto(task, &req->rq_snd_buf);
14998c2ecf20Sopenharmony_ci	connect_cookie = xprt->connect_cookie;
15008c2ecf20Sopenharmony_ci	status = xprt->ops->send_request(req);
15018c2ecf20Sopenharmony_ci	if (status != 0) {
15028c2ecf20Sopenharmony_ci		req->rq_ntrans--;
15038c2ecf20Sopenharmony_ci		trace_xprt_transmit(req, status);
15048c2ecf20Sopenharmony_ci		return status;
15058c2ecf20Sopenharmony_ci	}
15068c2ecf20Sopenharmony_ci
15078c2ecf20Sopenharmony_ci	if (is_retrans)
15088c2ecf20Sopenharmony_ci		task->tk_client->cl_stats->rpcretrans++;
15098c2ecf20Sopenharmony_ci
15108c2ecf20Sopenharmony_ci	xprt_inject_disconnect(xprt);
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci	task->tk_flags |= RPC_TASK_SENT;
15138c2ecf20Sopenharmony_ci	spin_lock(&xprt->transport_lock);
15148c2ecf20Sopenharmony_ci
15158c2ecf20Sopenharmony_ci	xprt->stat.sends++;
15168c2ecf20Sopenharmony_ci	xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
15178c2ecf20Sopenharmony_ci	xprt->stat.bklog_u += xprt->backlog.qlen;
15188c2ecf20Sopenharmony_ci	xprt->stat.sending_u += xprt->sending.qlen;
15198c2ecf20Sopenharmony_ci	xprt->stat.pending_u += xprt->pending.qlen;
15208c2ecf20Sopenharmony_ci	spin_unlock(&xprt->transport_lock);
15218c2ecf20Sopenharmony_ci
15228c2ecf20Sopenharmony_ci	req->rq_connect_cookie = connect_cookie;
15238c2ecf20Sopenharmony_ciout_dequeue:
15248c2ecf20Sopenharmony_ci	trace_xprt_transmit(req, status);
15258c2ecf20Sopenharmony_ci	xprt_request_dequeue_transmit(task);
15268c2ecf20Sopenharmony_ci	rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
15278c2ecf20Sopenharmony_ci	return status;
15288c2ecf20Sopenharmony_ci}
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_ci/**
15318c2ecf20Sopenharmony_ci * xprt_transmit - send an RPC request on a transport
15328c2ecf20Sopenharmony_ci * @task: controlling RPC task
15338c2ecf20Sopenharmony_ci *
15348c2ecf20Sopenharmony_ci * Attempts to drain the transmit queue. On exit, either the transport
15358c2ecf20Sopenharmony_ci * signalled an error that needs to be handled before transmission can
15368c2ecf20Sopenharmony_ci * resume, or @task finished transmitting, and detected that it already
15378c2ecf20Sopenharmony_ci * received a reply.
15388c2ecf20Sopenharmony_ci */
15398c2ecf20Sopenharmony_civoid
15408c2ecf20Sopenharmony_cixprt_transmit(struct rpc_task *task)
15418c2ecf20Sopenharmony_ci{
15428c2ecf20Sopenharmony_ci	struct rpc_rqst *next, *req = task->tk_rqstp;
15438c2ecf20Sopenharmony_ci	struct rpc_xprt	*xprt = req->rq_xprt;
15448c2ecf20Sopenharmony_ci	int status;
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ci	spin_lock(&xprt->queue_lock);
15478c2ecf20Sopenharmony_ci	for (;;) {
15488c2ecf20Sopenharmony_ci		next = list_first_entry_or_null(&xprt->xmit_queue,
15498c2ecf20Sopenharmony_ci						struct rpc_rqst, rq_xmit);
15508c2ecf20Sopenharmony_ci		if (!next)
15518c2ecf20Sopenharmony_ci			break;
15528c2ecf20Sopenharmony_ci		xprt_pin_rqst(next);
15538c2ecf20Sopenharmony_ci		spin_unlock(&xprt->queue_lock);
15548c2ecf20Sopenharmony_ci		status = xprt_request_transmit(next, task);
15558c2ecf20Sopenharmony_ci		if (status == -EBADMSG && next != req)
15568c2ecf20Sopenharmony_ci			status = 0;
15578c2ecf20Sopenharmony_ci		spin_lock(&xprt->queue_lock);
15588c2ecf20Sopenharmony_ci		xprt_unpin_rqst(next);
15598c2ecf20Sopenharmony_ci		if (status < 0) {
15608c2ecf20Sopenharmony_ci			if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
15618c2ecf20Sopenharmony_ci				task->tk_status = status;
15628c2ecf20Sopenharmony_ci			break;
15638c2ecf20Sopenharmony_ci		}
15648c2ecf20Sopenharmony_ci		/* Was @task transmitted, and has it received a reply? */
15658c2ecf20Sopenharmony_ci		if (xprt_request_data_received(task) &&
15668c2ecf20Sopenharmony_ci		    !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
15678c2ecf20Sopenharmony_ci			break;
15688c2ecf20Sopenharmony_ci		cond_resched_lock(&xprt->queue_lock);
15698c2ecf20Sopenharmony_ci	}
15708c2ecf20Sopenharmony_ci	spin_unlock(&xprt->queue_lock);
15718c2ecf20Sopenharmony_ci}
15728c2ecf20Sopenharmony_ci
15738c2ecf20Sopenharmony_cistatic void xprt_complete_request_init(struct rpc_task *task)
15748c2ecf20Sopenharmony_ci{
15758c2ecf20Sopenharmony_ci	if (task->tk_rqstp)
15768c2ecf20Sopenharmony_ci		xprt_request_init(task);
15778c2ecf20Sopenharmony_ci}
15788c2ecf20Sopenharmony_ci
15798c2ecf20Sopenharmony_civoid xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
15808c2ecf20Sopenharmony_ci{
15818c2ecf20Sopenharmony_ci	set_bit(XPRT_CONGESTED, &xprt->state);
15828c2ecf20Sopenharmony_ci	rpc_sleep_on(&xprt->backlog, task, xprt_complete_request_init);
15838c2ecf20Sopenharmony_ci}
15848c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_add_backlog);
15858c2ecf20Sopenharmony_ci
15868c2ecf20Sopenharmony_cistatic bool __xprt_set_rq(struct rpc_task *task, void *data)
15878c2ecf20Sopenharmony_ci{
15888c2ecf20Sopenharmony_ci	struct rpc_rqst *req = data;
15898c2ecf20Sopenharmony_ci
15908c2ecf20Sopenharmony_ci	if (task->tk_rqstp == NULL) {
15918c2ecf20Sopenharmony_ci		memset(req, 0, sizeof(*req));	/* mark unused */
15928c2ecf20Sopenharmony_ci		task->tk_rqstp = req;
15938c2ecf20Sopenharmony_ci		return true;
15948c2ecf20Sopenharmony_ci	}
15958c2ecf20Sopenharmony_ci	return false;
15968c2ecf20Sopenharmony_ci}
15978c2ecf20Sopenharmony_ci
15988c2ecf20Sopenharmony_cibool xprt_wake_up_backlog(struct rpc_xprt *xprt, struct rpc_rqst *req)
15998c2ecf20Sopenharmony_ci{
16008c2ecf20Sopenharmony_ci	if (rpc_wake_up_first(&xprt->backlog, __xprt_set_rq, req) == NULL) {
16018c2ecf20Sopenharmony_ci		clear_bit(XPRT_CONGESTED, &xprt->state);
16028c2ecf20Sopenharmony_ci		return false;
16038c2ecf20Sopenharmony_ci	}
16048c2ecf20Sopenharmony_ci	return true;
16058c2ecf20Sopenharmony_ci}
16068c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_wake_up_backlog);
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_cistatic bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
16098c2ecf20Sopenharmony_ci{
16108c2ecf20Sopenharmony_ci	bool ret = false;
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_ci	if (!test_bit(XPRT_CONGESTED, &xprt->state))
16138c2ecf20Sopenharmony_ci		goto out;
16148c2ecf20Sopenharmony_ci	spin_lock(&xprt->reserve_lock);
16158c2ecf20Sopenharmony_ci	if (test_bit(XPRT_CONGESTED, &xprt->state)) {
16168c2ecf20Sopenharmony_ci		xprt_add_backlog(xprt, task);
16178c2ecf20Sopenharmony_ci		ret = true;
16188c2ecf20Sopenharmony_ci	}
16198c2ecf20Sopenharmony_ci	spin_unlock(&xprt->reserve_lock);
16208c2ecf20Sopenharmony_ciout:
16218c2ecf20Sopenharmony_ci	return ret;
16228c2ecf20Sopenharmony_ci}
16238c2ecf20Sopenharmony_ci
16248c2ecf20Sopenharmony_cistatic struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
16258c2ecf20Sopenharmony_ci{
16268c2ecf20Sopenharmony_ci	struct rpc_rqst *req = ERR_PTR(-EAGAIN);
16278c2ecf20Sopenharmony_ci	gfp_t gfp_mask = GFP_KERNEL;
16288c2ecf20Sopenharmony_ci
16298c2ecf20Sopenharmony_ci	if (xprt->num_reqs >= xprt->max_reqs)
16308c2ecf20Sopenharmony_ci		goto out;
16318c2ecf20Sopenharmony_ci	++xprt->num_reqs;
16328c2ecf20Sopenharmony_ci	spin_unlock(&xprt->reserve_lock);
16338c2ecf20Sopenharmony_ci	if (current->flags & PF_WQ_WORKER)
16348c2ecf20Sopenharmony_ci		gfp_mask |= __GFP_NORETRY | __GFP_NOWARN;
16358c2ecf20Sopenharmony_ci	req = kzalloc(sizeof(*req), gfp_mask);
16368c2ecf20Sopenharmony_ci	spin_lock(&xprt->reserve_lock);
16378c2ecf20Sopenharmony_ci	if (req != NULL)
16388c2ecf20Sopenharmony_ci		goto out;
16398c2ecf20Sopenharmony_ci	--xprt->num_reqs;
16408c2ecf20Sopenharmony_ci	req = ERR_PTR(-ENOMEM);
16418c2ecf20Sopenharmony_ciout:
16428c2ecf20Sopenharmony_ci	return req;
16438c2ecf20Sopenharmony_ci}
16448c2ecf20Sopenharmony_ci
16458c2ecf20Sopenharmony_cistatic bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
16468c2ecf20Sopenharmony_ci{
16478c2ecf20Sopenharmony_ci	if (xprt->num_reqs > xprt->min_reqs) {
16488c2ecf20Sopenharmony_ci		--xprt->num_reqs;
16498c2ecf20Sopenharmony_ci		kfree(req);
16508c2ecf20Sopenharmony_ci		return true;
16518c2ecf20Sopenharmony_ci	}
16528c2ecf20Sopenharmony_ci	return false;
16538c2ecf20Sopenharmony_ci}
16548c2ecf20Sopenharmony_ci
16558c2ecf20Sopenharmony_civoid xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
16568c2ecf20Sopenharmony_ci{
16578c2ecf20Sopenharmony_ci	struct rpc_rqst *req;
16588c2ecf20Sopenharmony_ci
16598c2ecf20Sopenharmony_ci	spin_lock(&xprt->reserve_lock);
16608c2ecf20Sopenharmony_ci	if (!list_empty(&xprt->free)) {
16618c2ecf20Sopenharmony_ci		req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
16628c2ecf20Sopenharmony_ci		list_del(&req->rq_list);
16638c2ecf20Sopenharmony_ci		goto out_init_req;
16648c2ecf20Sopenharmony_ci	}
16658c2ecf20Sopenharmony_ci	req = xprt_dynamic_alloc_slot(xprt);
16668c2ecf20Sopenharmony_ci	if (!IS_ERR(req))
16678c2ecf20Sopenharmony_ci		goto out_init_req;
16688c2ecf20Sopenharmony_ci	switch (PTR_ERR(req)) {
16698c2ecf20Sopenharmony_ci	case -ENOMEM:
16708c2ecf20Sopenharmony_ci		dprintk("RPC:       dynamic allocation of request slot "
16718c2ecf20Sopenharmony_ci				"failed! Retrying\n");
16728c2ecf20Sopenharmony_ci		task->tk_status = -ENOMEM;
16738c2ecf20Sopenharmony_ci		break;
16748c2ecf20Sopenharmony_ci	case -EAGAIN:
16758c2ecf20Sopenharmony_ci		xprt_add_backlog(xprt, task);
16768c2ecf20Sopenharmony_ci		dprintk("RPC:       waiting for request slot\n");
16778c2ecf20Sopenharmony_ci		fallthrough;
16788c2ecf20Sopenharmony_ci	default:
16798c2ecf20Sopenharmony_ci		task->tk_status = -EAGAIN;
16808c2ecf20Sopenharmony_ci	}
16818c2ecf20Sopenharmony_ci	spin_unlock(&xprt->reserve_lock);
16828c2ecf20Sopenharmony_ci	return;
16838c2ecf20Sopenharmony_ciout_init_req:
16848c2ecf20Sopenharmony_ci	xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
16858c2ecf20Sopenharmony_ci				     xprt->num_reqs);
16868c2ecf20Sopenharmony_ci	spin_unlock(&xprt->reserve_lock);
16878c2ecf20Sopenharmony_ci
16888c2ecf20Sopenharmony_ci	task->tk_status = 0;
16898c2ecf20Sopenharmony_ci	task->tk_rqstp = req;
16908c2ecf20Sopenharmony_ci}
16918c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_alloc_slot);
16928c2ecf20Sopenharmony_ci
16938c2ecf20Sopenharmony_civoid xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
16948c2ecf20Sopenharmony_ci{
16958c2ecf20Sopenharmony_ci	spin_lock(&xprt->reserve_lock);
16968c2ecf20Sopenharmony_ci	if (!xprt_wake_up_backlog(xprt, req) &&
16978c2ecf20Sopenharmony_ci	    !xprt_dynamic_free_slot(xprt, req)) {
16988c2ecf20Sopenharmony_ci		memset(req, 0, sizeof(*req));	/* mark unused */
16998c2ecf20Sopenharmony_ci		list_add(&req->rq_list, &xprt->free);
17008c2ecf20Sopenharmony_ci	}
17018c2ecf20Sopenharmony_ci	spin_unlock(&xprt->reserve_lock);
17028c2ecf20Sopenharmony_ci}
17038c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_free_slot);
17048c2ecf20Sopenharmony_ci
17058c2ecf20Sopenharmony_cistatic void xprt_free_all_slots(struct rpc_xprt *xprt)
17068c2ecf20Sopenharmony_ci{
17078c2ecf20Sopenharmony_ci	struct rpc_rqst *req;
17088c2ecf20Sopenharmony_ci	while (!list_empty(&xprt->free)) {
17098c2ecf20Sopenharmony_ci		req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
17108c2ecf20Sopenharmony_ci		list_del(&req->rq_list);
17118c2ecf20Sopenharmony_ci		kfree(req);
17128c2ecf20Sopenharmony_ci	}
17138c2ecf20Sopenharmony_ci}
17148c2ecf20Sopenharmony_ci
17158c2ecf20Sopenharmony_cistruct rpc_xprt *xprt_alloc(struct net *net, size_t size,
17168c2ecf20Sopenharmony_ci		unsigned int num_prealloc,
17178c2ecf20Sopenharmony_ci		unsigned int max_alloc)
17188c2ecf20Sopenharmony_ci{
17198c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt;
17208c2ecf20Sopenharmony_ci	struct rpc_rqst *req;
17218c2ecf20Sopenharmony_ci	int i;
17228c2ecf20Sopenharmony_ci
17238c2ecf20Sopenharmony_ci	xprt = kzalloc(size, GFP_KERNEL);
17248c2ecf20Sopenharmony_ci	if (xprt == NULL)
17258c2ecf20Sopenharmony_ci		goto out;
17268c2ecf20Sopenharmony_ci
17278c2ecf20Sopenharmony_ci	xprt_init(xprt, net);
17288c2ecf20Sopenharmony_ci
17298c2ecf20Sopenharmony_ci	for (i = 0; i < num_prealloc; i++) {
17308c2ecf20Sopenharmony_ci		req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
17318c2ecf20Sopenharmony_ci		if (!req)
17328c2ecf20Sopenharmony_ci			goto out_free;
17338c2ecf20Sopenharmony_ci		list_add(&req->rq_list, &xprt->free);
17348c2ecf20Sopenharmony_ci	}
17358c2ecf20Sopenharmony_ci	if (max_alloc > num_prealloc)
17368c2ecf20Sopenharmony_ci		xprt->max_reqs = max_alloc;
17378c2ecf20Sopenharmony_ci	else
17388c2ecf20Sopenharmony_ci		xprt->max_reqs = num_prealloc;
17398c2ecf20Sopenharmony_ci	xprt->min_reqs = num_prealloc;
17408c2ecf20Sopenharmony_ci	xprt->num_reqs = num_prealloc;
17418c2ecf20Sopenharmony_ci
17428c2ecf20Sopenharmony_ci	return xprt;
17438c2ecf20Sopenharmony_ci
17448c2ecf20Sopenharmony_ciout_free:
17458c2ecf20Sopenharmony_ci	xprt_free(xprt);
17468c2ecf20Sopenharmony_ciout:
17478c2ecf20Sopenharmony_ci	return NULL;
17488c2ecf20Sopenharmony_ci}
17498c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_alloc);
17508c2ecf20Sopenharmony_ci
17518c2ecf20Sopenharmony_civoid xprt_free(struct rpc_xprt *xprt)
17528c2ecf20Sopenharmony_ci{
17538c2ecf20Sopenharmony_ci	put_net(xprt->xprt_net);
17548c2ecf20Sopenharmony_ci	xprt_free_all_slots(xprt);
17558c2ecf20Sopenharmony_ci	kfree_rcu(xprt, rcu);
17568c2ecf20Sopenharmony_ci}
17578c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_free);
17588c2ecf20Sopenharmony_ci
17598c2ecf20Sopenharmony_cistatic void
17608c2ecf20Sopenharmony_cixprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
17618c2ecf20Sopenharmony_ci{
17628c2ecf20Sopenharmony_ci	req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
17638c2ecf20Sopenharmony_ci}
17648c2ecf20Sopenharmony_ci
17658c2ecf20Sopenharmony_cistatic __be32
17668c2ecf20Sopenharmony_cixprt_alloc_xid(struct rpc_xprt *xprt)
17678c2ecf20Sopenharmony_ci{
17688c2ecf20Sopenharmony_ci	__be32 xid;
17698c2ecf20Sopenharmony_ci
17708c2ecf20Sopenharmony_ci	spin_lock(&xprt->reserve_lock);
17718c2ecf20Sopenharmony_ci	xid = (__force __be32)xprt->xid++;
17728c2ecf20Sopenharmony_ci	spin_unlock(&xprt->reserve_lock);
17738c2ecf20Sopenharmony_ci	return xid;
17748c2ecf20Sopenharmony_ci}
17758c2ecf20Sopenharmony_ci
17768c2ecf20Sopenharmony_cistatic void
17778c2ecf20Sopenharmony_cixprt_init_xid(struct rpc_xprt *xprt)
17788c2ecf20Sopenharmony_ci{
17798c2ecf20Sopenharmony_ci	xprt->xid = prandom_u32();
17808c2ecf20Sopenharmony_ci}
17818c2ecf20Sopenharmony_ci
17828c2ecf20Sopenharmony_cistatic void
17838c2ecf20Sopenharmony_cixprt_request_init(struct rpc_task *task)
17848c2ecf20Sopenharmony_ci{
17858c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = task->tk_xprt;
17868c2ecf20Sopenharmony_ci	struct rpc_rqst	*req = task->tk_rqstp;
17878c2ecf20Sopenharmony_ci
17888c2ecf20Sopenharmony_ci	req->rq_task	= task;
17898c2ecf20Sopenharmony_ci	req->rq_xprt    = xprt;
17908c2ecf20Sopenharmony_ci	req->rq_buffer  = NULL;
17918c2ecf20Sopenharmony_ci	req->rq_xid	= xprt_alloc_xid(xprt);
17928c2ecf20Sopenharmony_ci	xprt_init_connect_cookie(req, xprt);
17938c2ecf20Sopenharmony_ci	req->rq_snd_buf.len = 0;
17948c2ecf20Sopenharmony_ci	req->rq_snd_buf.buflen = 0;
17958c2ecf20Sopenharmony_ci	req->rq_rcv_buf.len = 0;
17968c2ecf20Sopenharmony_ci	req->rq_rcv_buf.buflen = 0;
17978c2ecf20Sopenharmony_ci	req->rq_snd_buf.bvec = NULL;
17988c2ecf20Sopenharmony_ci	req->rq_rcv_buf.bvec = NULL;
17998c2ecf20Sopenharmony_ci	req->rq_release_snd_buf = NULL;
18008c2ecf20Sopenharmony_ci	xprt_init_majortimeo(task, req);
18018c2ecf20Sopenharmony_ci
18028c2ecf20Sopenharmony_ci	trace_xprt_reserve(req);
18038c2ecf20Sopenharmony_ci}
18048c2ecf20Sopenharmony_ci
18058c2ecf20Sopenharmony_cistatic void
18068c2ecf20Sopenharmony_cixprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
18078c2ecf20Sopenharmony_ci{
18088c2ecf20Sopenharmony_ci	xprt->ops->alloc_slot(xprt, task);
18098c2ecf20Sopenharmony_ci	if (task->tk_rqstp != NULL)
18108c2ecf20Sopenharmony_ci		xprt_request_init(task);
18118c2ecf20Sopenharmony_ci}
18128c2ecf20Sopenharmony_ci
18138c2ecf20Sopenharmony_ci/**
18148c2ecf20Sopenharmony_ci * xprt_reserve - allocate an RPC request slot
18158c2ecf20Sopenharmony_ci * @task: RPC task requesting a slot allocation
18168c2ecf20Sopenharmony_ci *
18178c2ecf20Sopenharmony_ci * If the transport is marked as being congested, or if no more
18188c2ecf20Sopenharmony_ci * slots are available, place the task on the transport's
18198c2ecf20Sopenharmony_ci * backlog queue.
18208c2ecf20Sopenharmony_ci */
18218c2ecf20Sopenharmony_civoid xprt_reserve(struct rpc_task *task)
18228c2ecf20Sopenharmony_ci{
18238c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = task->tk_xprt;
18248c2ecf20Sopenharmony_ci
18258c2ecf20Sopenharmony_ci	task->tk_status = 0;
18268c2ecf20Sopenharmony_ci	if (task->tk_rqstp != NULL)
18278c2ecf20Sopenharmony_ci		return;
18288c2ecf20Sopenharmony_ci
18298c2ecf20Sopenharmony_ci	task->tk_status = -EAGAIN;
18308c2ecf20Sopenharmony_ci	if (!xprt_throttle_congested(xprt, task))
18318c2ecf20Sopenharmony_ci		xprt_do_reserve(xprt, task);
18328c2ecf20Sopenharmony_ci}
18338c2ecf20Sopenharmony_ci
18348c2ecf20Sopenharmony_ci/**
18358c2ecf20Sopenharmony_ci * xprt_retry_reserve - allocate an RPC request slot
18368c2ecf20Sopenharmony_ci * @task: RPC task requesting a slot allocation
18378c2ecf20Sopenharmony_ci *
18388c2ecf20Sopenharmony_ci * If no more slots are available, place the task on the transport's
18398c2ecf20Sopenharmony_ci * backlog queue.
18408c2ecf20Sopenharmony_ci * Note that the only difference with xprt_reserve is that we now
18418c2ecf20Sopenharmony_ci * ignore the value of the XPRT_CONGESTED flag.
18428c2ecf20Sopenharmony_ci */
18438c2ecf20Sopenharmony_civoid xprt_retry_reserve(struct rpc_task *task)
18448c2ecf20Sopenharmony_ci{
18458c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt = task->tk_xprt;
18468c2ecf20Sopenharmony_ci
18478c2ecf20Sopenharmony_ci	task->tk_status = 0;
18488c2ecf20Sopenharmony_ci	if (task->tk_rqstp != NULL)
18498c2ecf20Sopenharmony_ci		return;
18508c2ecf20Sopenharmony_ci
18518c2ecf20Sopenharmony_ci	task->tk_status = -EAGAIN;
18528c2ecf20Sopenharmony_ci	xprt_do_reserve(xprt, task);
18538c2ecf20Sopenharmony_ci}
18548c2ecf20Sopenharmony_ci
18558c2ecf20Sopenharmony_ci/**
18568c2ecf20Sopenharmony_ci * xprt_release - release an RPC request slot
18578c2ecf20Sopenharmony_ci * @task: task which is finished with the slot
18588c2ecf20Sopenharmony_ci *
18598c2ecf20Sopenharmony_ci */
18608c2ecf20Sopenharmony_civoid xprt_release(struct rpc_task *task)
18618c2ecf20Sopenharmony_ci{
18628c2ecf20Sopenharmony_ci	struct rpc_xprt	*xprt;
18638c2ecf20Sopenharmony_ci	struct rpc_rqst	*req = task->tk_rqstp;
18648c2ecf20Sopenharmony_ci
18658c2ecf20Sopenharmony_ci	if (req == NULL) {
18668c2ecf20Sopenharmony_ci		if (task->tk_client) {
18678c2ecf20Sopenharmony_ci			xprt = task->tk_xprt;
18688c2ecf20Sopenharmony_ci			xprt_release_write(xprt, task);
18698c2ecf20Sopenharmony_ci		}
18708c2ecf20Sopenharmony_ci		return;
18718c2ecf20Sopenharmony_ci	}
18728c2ecf20Sopenharmony_ci
18738c2ecf20Sopenharmony_ci	xprt = req->rq_xprt;
18748c2ecf20Sopenharmony_ci	xprt_request_dequeue_xprt(task);
18758c2ecf20Sopenharmony_ci	spin_lock(&xprt->transport_lock);
18768c2ecf20Sopenharmony_ci	xprt->ops->release_xprt(xprt, task);
18778c2ecf20Sopenharmony_ci	if (xprt->ops->release_request)
18788c2ecf20Sopenharmony_ci		xprt->ops->release_request(task);
18798c2ecf20Sopenharmony_ci	xprt_schedule_autodisconnect(xprt);
18808c2ecf20Sopenharmony_ci	spin_unlock(&xprt->transport_lock);
18818c2ecf20Sopenharmony_ci	if (req->rq_buffer)
18828c2ecf20Sopenharmony_ci		xprt->ops->buf_free(task);
18838c2ecf20Sopenharmony_ci	xdr_free_bvec(&req->rq_rcv_buf);
18848c2ecf20Sopenharmony_ci	xdr_free_bvec(&req->rq_snd_buf);
18858c2ecf20Sopenharmony_ci	if (req->rq_cred != NULL)
18868c2ecf20Sopenharmony_ci		put_rpccred(req->rq_cred);
18878c2ecf20Sopenharmony_ci	if (req->rq_release_snd_buf)
18888c2ecf20Sopenharmony_ci		req->rq_release_snd_buf(req);
18898c2ecf20Sopenharmony_ci
18908c2ecf20Sopenharmony_ci	task->tk_rqstp = NULL;
18918c2ecf20Sopenharmony_ci	if (likely(!bc_prealloc(req)))
18928c2ecf20Sopenharmony_ci		xprt->ops->free_slot(xprt, req);
18938c2ecf20Sopenharmony_ci	else
18948c2ecf20Sopenharmony_ci		xprt_free_bc_request(req);
18958c2ecf20Sopenharmony_ci}
18968c2ecf20Sopenharmony_ci
18978c2ecf20Sopenharmony_ci#ifdef CONFIG_SUNRPC_BACKCHANNEL
18988c2ecf20Sopenharmony_civoid
18998c2ecf20Sopenharmony_cixprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task)
19008c2ecf20Sopenharmony_ci{
19018c2ecf20Sopenharmony_ci	struct xdr_buf *xbufp = &req->rq_snd_buf;
19028c2ecf20Sopenharmony_ci
19038c2ecf20Sopenharmony_ci	task->tk_rqstp = req;
19048c2ecf20Sopenharmony_ci	req->rq_task = task;
19058c2ecf20Sopenharmony_ci	xprt_init_connect_cookie(req, req->rq_xprt);
19068c2ecf20Sopenharmony_ci	/*
19078c2ecf20Sopenharmony_ci	 * Set up the xdr_buf length.
19088c2ecf20Sopenharmony_ci	 * This also indicates that the buffer is XDR encoded already.
19098c2ecf20Sopenharmony_ci	 */
19108c2ecf20Sopenharmony_ci	xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
19118c2ecf20Sopenharmony_ci		xbufp->tail[0].iov_len;
19128c2ecf20Sopenharmony_ci}
19138c2ecf20Sopenharmony_ci#endif
19148c2ecf20Sopenharmony_ci
19158c2ecf20Sopenharmony_cistatic void xprt_init(struct rpc_xprt *xprt, struct net *net)
19168c2ecf20Sopenharmony_ci{
19178c2ecf20Sopenharmony_ci	kref_init(&xprt->kref);
19188c2ecf20Sopenharmony_ci
19198c2ecf20Sopenharmony_ci	spin_lock_init(&xprt->transport_lock);
19208c2ecf20Sopenharmony_ci	spin_lock_init(&xprt->reserve_lock);
19218c2ecf20Sopenharmony_ci	spin_lock_init(&xprt->queue_lock);
19228c2ecf20Sopenharmony_ci
19238c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&xprt->free);
19248c2ecf20Sopenharmony_ci	xprt->recv_queue = RB_ROOT;
19258c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&xprt->xmit_queue);
19268c2ecf20Sopenharmony_ci#if defined(CONFIG_SUNRPC_BACKCHANNEL)
19278c2ecf20Sopenharmony_ci	spin_lock_init(&xprt->bc_pa_lock);
19288c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&xprt->bc_pa_list);
19298c2ecf20Sopenharmony_ci#endif /* CONFIG_SUNRPC_BACKCHANNEL */
19308c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&xprt->xprt_switch);
19318c2ecf20Sopenharmony_ci
19328c2ecf20Sopenharmony_ci	xprt->last_used = jiffies;
19338c2ecf20Sopenharmony_ci	xprt->cwnd = RPC_INITCWND;
19348c2ecf20Sopenharmony_ci	xprt->bind_index = 0;
19358c2ecf20Sopenharmony_ci
19368c2ecf20Sopenharmony_ci	rpc_init_wait_queue(&xprt->binding, "xprt_binding");
19378c2ecf20Sopenharmony_ci	rpc_init_wait_queue(&xprt->pending, "xprt_pending");
19388c2ecf20Sopenharmony_ci	rpc_init_wait_queue(&xprt->sending, "xprt_sending");
19398c2ecf20Sopenharmony_ci	rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
19408c2ecf20Sopenharmony_ci
19418c2ecf20Sopenharmony_ci	xprt_init_xid(xprt);
19428c2ecf20Sopenharmony_ci
19438c2ecf20Sopenharmony_ci	xprt->xprt_net = get_net(net);
19448c2ecf20Sopenharmony_ci}
19458c2ecf20Sopenharmony_ci
19468c2ecf20Sopenharmony_ci/**
19478c2ecf20Sopenharmony_ci * xprt_create_transport - create an RPC transport
19488c2ecf20Sopenharmony_ci * @args: rpc transport creation arguments
19498c2ecf20Sopenharmony_ci *
19508c2ecf20Sopenharmony_ci */
19518c2ecf20Sopenharmony_cistruct rpc_xprt *xprt_create_transport(struct xprt_create *args)
19528c2ecf20Sopenharmony_ci{
19538c2ecf20Sopenharmony_ci	struct rpc_xprt	*xprt;
19548c2ecf20Sopenharmony_ci	struct xprt_class *t;
19558c2ecf20Sopenharmony_ci
19568c2ecf20Sopenharmony_ci	spin_lock(&xprt_list_lock);
19578c2ecf20Sopenharmony_ci	list_for_each_entry(t, &xprt_list, list) {
19588c2ecf20Sopenharmony_ci		if (t->ident == args->ident) {
19598c2ecf20Sopenharmony_ci			spin_unlock(&xprt_list_lock);
19608c2ecf20Sopenharmony_ci			goto found;
19618c2ecf20Sopenharmony_ci		}
19628c2ecf20Sopenharmony_ci	}
19638c2ecf20Sopenharmony_ci	spin_unlock(&xprt_list_lock);
19648c2ecf20Sopenharmony_ci	dprintk("RPC: transport (%d) not supported\n", args->ident);
19658c2ecf20Sopenharmony_ci	return ERR_PTR(-EIO);
19668c2ecf20Sopenharmony_ci
19678c2ecf20Sopenharmony_cifound:
19688c2ecf20Sopenharmony_ci	xprt = t->setup(args);
19698c2ecf20Sopenharmony_ci	if (IS_ERR(xprt))
19708c2ecf20Sopenharmony_ci		goto out;
19718c2ecf20Sopenharmony_ci	if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
19728c2ecf20Sopenharmony_ci		xprt->idle_timeout = 0;
19738c2ecf20Sopenharmony_ci	INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
19748c2ecf20Sopenharmony_ci	if (xprt_has_timer(xprt))
19758c2ecf20Sopenharmony_ci		timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
19768c2ecf20Sopenharmony_ci	else
19778c2ecf20Sopenharmony_ci		timer_setup(&xprt->timer, NULL, 0);
19788c2ecf20Sopenharmony_ci
19798c2ecf20Sopenharmony_ci	if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
19808c2ecf20Sopenharmony_ci		xprt_destroy(xprt);
19818c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
19828c2ecf20Sopenharmony_ci	}
19838c2ecf20Sopenharmony_ci	xprt->servername = kstrdup(args->servername, GFP_KERNEL);
19848c2ecf20Sopenharmony_ci	if (xprt->servername == NULL) {
19858c2ecf20Sopenharmony_ci		xprt_destroy(xprt);
19868c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
19878c2ecf20Sopenharmony_ci	}
19888c2ecf20Sopenharmony_ci
19898c2ecf20Sopenharmony_ci	rpc_xprt_debugfs_register(xprt);
19908c2ecf20Sopenharmony_ci
19918c2ecf20Sopenharmony_ci	trace_xprt_create(xprt);
19928c2ecf20Sopenharmony_ciout:
19938c2ecf20Sopenharmony_ci	return xprt;
19948c2ecf20Sopenharmony_ci}
19958c2ecf20Sopenharmony_ci
19968c2ecf20Sopenharmony_cistatic void xprt_destroy_cb(struct work_struct *work)
19978c2ecf20Sopenharmony_ci{
19988c2ecf20Sopenharmony_ci	struct rpc_xprt *xprt =
19998c2ecf20Sopenharmony_ci		container_of(work, struct rpc_xprt, task_cleanup);
20008c2ecf20Sopenharmony_ci
20018c2ecf20Sopenharmony_ci	trace_xprt_destroy(xprt);
20028c2ecf20Sopenharmony_ci
20038c2ecf20Sopenharmony_ci	rpc_xprt_debugfs_unregister(xprt);
20048c2ecf20Sopenharmony_ci	rpc_destroy_wait_queue(&xprt->binding);
20058c2ecf20Sopenharmony_ci	rpc_destroy_wait_queue(&xprt->pending);
20068c2ecf20Sopenharmony_ci	rpc_destroy_wait_queue(&xprt->sending);
20078c2ecf20Sopenharmony_ci	rpc_destroy_wait_queue(&xprt->backlog);
20088c2ecf20Sopenharmony_ci	kfree(xprt->servername);
20098c2ecf20Sopenharmony_ci	/*
20108c2ecf20Sopenharmony_ci	 * Destroy any existing back channel
20118c2ecf20Sopenharmony_ci	 */
20128c2ecf20Sopenharmony_ci	xprt_destroy_backchannel(xprt, UINT_MAX);
20138c2ecf20Sopenharmony_ci
20148c2ecf20Sopenharmony_ci	/*
20158c2ecf20Sopenharmony_ci	 * Tear down transport state and free the rpc_xprt
20168c2ecf20Sopenharmony_ci	 */
20178c2ecf20Sopenharmony_ci	xprt->ops->destroy(xprt);
20188c2ecf20Sopenharmony_ci}
20198c2ecf20Sopenharmony_ci
20208c2ecf20Sopenharmony_ci/**
20218c2ecf20Sopenharmony_ci * xprt_destroy - destroy an RPC transport, killing off all requests.
20228c2ecf20Sopenharmony_ci * @xprt: transport to destroy
20238c2ecf20Sopenharmony_ci *
20248c2ecf20Sopenharmony_ci */
20258c2ecf20Sopenharmony_cistatic void xprt_destroy(struct rpc_xprt *xprt)
20268c2ecf20Sopenharmony_ci{
20278c2ecf20Sopenharmony_ci	/*
20288c2ecf20Sopenharmony_ci	 * Exclude transport connect/disconnect handlers and autoclose
20298c2ecf20Sopenharmony_ci	 */
20308c2ecf20Sopenharmony_ci	wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
20318c2ecf20Sopenharmony_ci
20328c2ecf20Sopenharmony_ci	/*
20338c2ecf20Sopenharmony_ci	 * xprt_schedule_autodisconnect() can run after XPRT_LOCKED
20348c2ecf20Sopenharmony_ci	 * is cleared.  We use ->transport_lock to ensure the mod_timer()
20358c2ecf20Sopenharmony_ci	 * can only run *before* del_time_sync(), never after.
20368c2ecf20Sopenharmony_ci	 */
20378c2ecf20Sopenharmony_ci	spin_lock(&xprt->transport_lock);
20388c2ecf20Sopenharmony_ci	del_timer_sync(&xprt->timer);
20398c2ecf20Sopenharmony_ci	spin_unlock(&xprt->transport_lock);
20408c2ecf20Sopenharmony_ci
20418c2ecf20Sopenharmony_ci	/*
20428c2ecf20Sopenharmony_ci	 * Destroy sockets etc from the system workqueue so they can
20438c2ecf20Sopenharmony_ci	 * safely flush receive work running on rpciod.
20448c2ecf20Sopenharmony_ci	 */
20458c2ecf20Sopenharmony_ci	INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
20468c2ecf20Sopenharmony_ci	schedule_work(&xprt->task_cleanup);
20478c2ecf20Sopenharmony_ci}
20488c2ecf20Sopenharmony_ci
20498c2ecf20Sopenharmony_cistatic void xprt_destroy_kref(struct kref *kref)
20508c2ecf20Sopenharmony_ci{
20518c2ecf20Sopenharmony_ci	xprt_destroy(container_of(kref, struct rpc_xprt, kref));
20528c2ecf20Sopenharmony_ci}
20538c2ecf20Sopenharmony_ci
20548c2ecf20Sopenharmony_ci/**
20558c2ecf20Sopenharmony_ci * xprt_get - return a reference to an RPC transport.
20568c2ecf20Sopenharmony_ci * @xprt: pointer to the transport
20578c2ecf20Sopenharmony_ci *
20588c2ecf20Sopenharmony_ci */
20598c2ecf20Sopenharmony_cistruct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
20608c2ecf20Sopenharmony_ci{
20618c2ecf20Sopenharmony_ci	if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
20628c2ecf20Sopenharmony_ci		return xprt;
20638c2ecf20Sopenharmony_ci	return NULL;
20648c2ecf20Sopenharmony_ci}
20658c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_get);
20668c2ecf20Sopenharmony_ci
20678c2ecf20Sopenharmony_ci/**
20688c2ecf20Sopenharmony_ci * xprt_put - release a reference to an RPC transport.
20698c2ecf20Sopenharmony_ci * @xprt: pointer to the transport
20708c2ecf20Sopenharmony_ci *
20718c2ecf20Sopenharmony_ci */
20728c2ecf20Sopenharmony_civoid xprt_put(struct rpc_xprt *xprt)
20738c2ecf20Sopenharmony_ci{
20748c2ecf20Sopenharmony_ci	if (xprt != NULL)
20758c2ecf20Sopenharmony_ci		kref_put(&xprt->kref, xprt_destroy_kref);
20768c2ecf20Sopenharmony_ci}
20778c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(xprt_put);
2078