xref: /linux/net/sunrpc/xprt.c (revision 9d84719d34cb98a2f584e38679f95216c1a15d0e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/net/sunrpc/xprt.c
4  *
5  *  This is a generic RPC call interface supporting congestion avoidance,
6  *  and asynchronous calls.
7  *
8  *  The interface works like this:
9  *
10  *  -	When a process places a call, it allocates a request slot if
11  *	one is available. Otherwise, it sleeps on the backlog queue
12  *	(xprt_reserve).
13  *  -	Next, the caller puts together the RPC message, stuffs it into
14  *	the request struct, and calls xprt_transmit().
15  *  -	xprt_transmit sends the message and installs the caller on the
16  *	transport's wait list. At the same time, if a reply is expected,
17  *	it installs a timer that is run after the packet's timeout has
18  *	expired.
19  *  -	When a packet arrives, the data_ready handler walks the list of
20  *	pending requests for that transport. If a matching XID is found, the
21  *	caller is woken up, and the timer removed.
22  *  -	When no reply arrives within the timeout interval, the timer is
23  *	fired by the kernel and runs xprt_timer(). It either adjusts the
24  *	timeout values (minor timeout) or wakes up the caller with a status
25  *	of -ETIMEDOUT.
26  *  -	When the caller receives a notification from RPC that a reply arrived,
27  *	it should release the RPC slot, and process the reply.
28  *	If the call timed out, it may choose to retry the operation by
29  *	adjusting the initial timeout value, and simply calling rpc_call
30  *	again.
31  *
32  *  Support for async RPC is done through a set of RPC-specific scheduling
33  *  primitives that `transparently' work for processes as well as async
34  *  tasks that rely on callbacks.
35  *
36  *  Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
37  *
38  *  Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
39  */
40 
41 #include <linux/module.h>
42 
43 #include <linux/types.h>
44 #include <linux/interrupt.h>
45 #include <linux/workqueue.h>
46 #include <linux/net.h>
47 #include <linux/ktime.h>
48 
49 #include <linux/sunrpc/clnt.h>
50 #include <linux/sunrpc/metrics.h>
51 #include <linux/sunrpc/bc_xprt.h>
52 #include <linux/rcupdate.h>
53 #include <linux/sched/mm.h>
54 
55 #include <trace/events/sunrpc.h>
56 
57 #include "sunrpc.h"
58 #include "sysfs.h"
59 #include "fail.h"
60 
61 /*
62  * Local variables
63  */
64 
65 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
66 # define RPCDBG_FACILITY	RPCDBG_XPRT
67 #endif
68 
69 /*
70  * Local functions
71  */
72 static void	xprt_init(struct rpc_xprt *xprt, struct net *net);
73 static __be32	xprt_alloc_xid(struct rpc_xprt *xprt);
74 static void	xprt_destroy(struct rpc_xprt *xprt);
75 static void	xprt_request_init(struct rpc_task *task);
76 static int	xprt_request_prepare(struct rpc_rqst *req, struct xdr_buf *buf);
77 
78 static DEFINE_SPINLOCK(xprt_list_lock);
79 static LIST_HEAD(xprt_list);
80 
81 static unsigned long xprt_request_timeout(const struct rpc_rqst *req)
82 {
83 	unsigned long timeout = jiffies + req->rq_timeout;
84 
85 	if (time_before(timeout, req->rq_majortimeo))
86 		return timeout;
87 	return req->rq_majortimeo;
88 }
89 
90 /**
91  * xprt_register_transport - register a transport implementation
92  * @transport: transport to register
93  *
94  * If a transport implementation is loaded as a kernel module, it can
95  * call this interface to make itself known to the RPC client.
96  *
97  * Returns:
98  * 0:		transport successfully registered
99  * -EEXIST:	transport already registered
100  * -EINVAL:	transport module being unloaded
101  */
102 int xprt_register_transport(struct xprt_class *transport)
103 {
104 	struct xprt_class *t;
105 	int result;
106 
107 	result = -EEXIST;
108 	spin_lock(&xprt_list_lock);
109 	list_for_each_entry(t, &xprt_list, list) {
110 		/* don't register the same transport class twice */
111 		if (t->ident == transport->ident)
112 			goto out;
113 	}
114 
115 	list_add_tail(&transport->list, &xprt_list);
116 	printk(KERN_INFO "RPC: Registered %s transport module.\n",
117 	       transport->name);
118 	result = 0;
119 
120 out:
121 	spin_unlock(&xprt_list_lock);
122 	return result;
123 }
124 EXPORT_SYMBOL_GPL(xprt_register_transport);
125 
126 /**
127  * xprt_unregister_transport - unregister a transport implementation
128  * @transport: transport to unregister
129  *
130  * Returns:
131  * 0:		transport successfully unregistered
132  * -ENOENT:	transport never registered
133  */
134 int xprt_unregister_transport(struct xprt_class *transport)
135 {
136 	struct xprt_class *t;
137 	int result;
138 
139 	result = 0;
140 	spin_lock(&xprt_list_lock);
141 	list_for_each_entry(t, &xprt_list, list) {
142 		if (t == transport) {
143 			printk(KERN_INFO
144 				"RPC: Unregistered %s transport module.\n",
145 				transport->name);
146 			list_del_init(&transport->list);
147 			goto out;
148 		}
149 	}
150 	result = -ENOENT;
151 
152 out:
153 	spin_unlock(&xprt_list_lock);
154 	return result;
155 }
156 EXPORT_SYMBOL_GPL(xprt_unregister_transport);
157 
158 static void
159 xprt_class_release(const struct xprt_class *t)
160 {
161 	module_put(t->owner);
162 }
163 
164 static const struct xprt_class *
165 xprt_class_find_by_ident_locked(int ident)
166 {
167 	const struct xprt_class *t;
168 
169 	list_for_each_entry(t, &xprt_list, list) {
170 		if (t->ident != ident)
171 			continue;
172 		if (!try_module_get(t->owner))
173 			continue;
174 		return t;
175 	}
176 	return NULL;
177 }
178 
179 static const struct xprt_class *
180 xprt_class_find_by_ident(int ident)
181 {
182 	const struct xprt_class *t;
183 
184 	spin_lock(&xprt_list_lock);
185 	t = xprt_class_find_by_ident_locked(ident);
186 	spin_unlock(&xprt_list_lock);
187 	return t;
188 }
189 
190 static const struct xprt_class *
191 xprt_class_find_by_netid_locked(const char *netid)
192 {
193 	const struct xprt_class *t;
194 	unsigned int i;
195 
196 	list_for_each_entry(t, &xprt_list, list) {
197 		for (i = 0; t->netid[i][0] != '\0'; i++) {
198 			if (strcmp(t->netid[i], netid) != 0)
199 				continue;
200 			if (!try_module_get(t->owner))
201 				continue;
202 			return t;
203 		}
204 	}
205 	return NULL;
206 }
207 
208 static const struct xprt_class *
209 xprt_class_find_by_netid(const char *netid)
210 {
211 	const struct xprt_class *t;
212 
213 	spin_lock(&xprt_list_lock);
214 	t = xprt_class_find_by_netid_locked(netid);
215 	if (!t) {
216 		spin_unlock(&xprt_list_lock);
217 		request_module("rpc%s", netid);
218 		spin_lock(&xprt_list_lock);
219 		t = xprt_class_find_by_netid_locked(netid);
220 	}
221 	spin_unlock(&xprt_list_lock);
222 	return t;
223 }
224 
225 /**
226  * xprt_find_transport_ident - convert a netid into a transport identifier
227  * @netid: transport to load
228  *
229  * Returns:
230  * > 0:		transport identifier
231  * -ENOENT:	transport module not available
232  */
233 int xprt_find_transport_ident(const char *netid)
234 {
235 	const struct xprt_class *t;
236 	int ret;
237 
238 	t = xprt_class_find_by_netid(netid);
239 	if (!t)
240 		return -ENOENT;
241 	ret = t->ident;
242 	xprt_class_release(t);
243 	return ret;
244 }
245 EXPORT_SYMBOL_GPL(xprt_find_transport_ident);
246 
247 static void xprt_clear_locked(struct rpc_xprt *xprt)
248 {
249 	xprt->snd_task = NULL;
250 	if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state))
251 		clear_bit_unlock(XPRT_LOCKED, &xprt->state);
252 	else
253 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
254 }
255 
256 /**
257  * xprt_reserve_xprt - serialize write access to transports
258  * @task: task that is requesting access to the transport
259  * @xprt: pointer to the target transport
260  *
261  * This prevents mixing the payload of separate requests, and prevents
262  * transport connects from colliding with writes.  No congestion control
263  * is provided.
264  */
265 int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
266 {
267 	struct rpc_rqst *req = task->tk_rqstp;
268 
269 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
270 		if (task == xprt->snd_task)
271 			goto out_locked;
272 		goto out_sleep;
273 	}
274 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
275 		goto out_unlock;
276 	xprt->snd_task = task;
277 
278 out_locked:
279 	trace_xprt_reserve_xprt(xprt, task);
280 	return 1;
281 
282 out_unlock:
283 	xprt_clear_locked(xprt);
284 out_sleep:
285 	task->tk_status = -EAGAIN;
286 	if (RPC_IS_SOFT(task) || RPC_IS_SOFTCONN(task))
287 		rpc_sleep_on_timeout(&xprt->sending, task, NULL,
288 				xprt_request_timeout(req));
289 	else
290 		rpc_sleep_on(&xprt->sending, task, NULL);
291 	return 0;
292 }
293 EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
294 
295 static bool
296 xprt_need_congestion_window_wait(struct rpc_xprt *xprt)
297 {
298 	return test_bit(XPRT_CWND_WAIT, &xprt->state);
299 }
300 
301 static void
302 xprt_set_congestion_window_wait(struct rpc_xprt *xprt)
303 {
304 	if (!list_empty(&xprt->xmit_queue)) {
305 		/* Peek at head of queue to see if it can make progress */
306 		if (list_first_entry(&xprt->xmit_queue, struct rpc_rqst,
307 					rq_xmit)->rq_cong)
308 			return;
309 	}
310 	set_bit(XPRT_CWND_WAIT, &xprt->state);
311 }
312 
313 static void
314 xprt_test_and_clear_congestion_window_wait(struct rpc_xprt *xprt)
315 {
316 	if (!RPCXPRT_CONGESTED(xprt))
317 		clear_bit(XPRT_CWND_WAIT, &xprt->state);
318 }
319 
320 /*
321  * xprt_reserve_xprt_cong - serialize write access to transports
322  * @task: task that is requesting access to the transport
323  *
324  * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
325  * integrated into the decision of whether a request is allowed to be
326  * woken up and given access to the transport.
327  * Note that the lock is only granted if we know there are free slots.
328  */
329 int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
330 {
331 	struct rpc_rqst *req = task->tk_rqstp;
332 
333 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
334 		if (task == xprt->snd_task)
335 			goto out_locked;
336 		goto out_sleep;
337 	}
338 	if (req == NULL) {
339 		xprt->snd_task = task;
340 		goto out_locked;
341 	}
342 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
343 		goto out_unlock;
344 	if (!xprt_need_congestion_window_wait(xprt)) {
345 		xprt->snd_task = task;
346 		goto out_locked;
347 	}
348 out_unlock:
349 	xprt_clear_locked(xprt);
350 out_sleep:
351 	task->tk_status = -EAGAIN;
352 	if (RPC_IS_SOFT(task) || RPC_IS_SOFTCONN(task))
353 		rpc_sleep_on_timeout(&xprt->sending, task, NULL,
354 				xprt_request_timeout(req));
355 	else
356 		rpc_sleep_on(&xprt->sending, task, NULL);
357 	return 0;
358 out_locked:
359 	trace_xprt_reserve_cong(xprt, task);
360 	return 1;
361 }
362 EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
363 
364 static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
365 {
366 	int retval;
367 
368 	if (test_bit(XPRT_LOCKED, &xprt->state) && xprt->snd_task == task)
369 		return 1;
370 	spin_lock(&xprt->transport_lock);
371 	retval = xprt->ops->reserve_xprt(xprt, task);
372 	spin_unlock(&xprt->transport_lock);
373 	return retval;
374 }
375 
376 static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
377 {
378 	struct rpc_xprt *xprt = data;
379 
380 	xprt->snd_task = task;
381 	return true;
382 }
383 
384 static void __xprt_lock_write_next(struct rpc_xprt *xprt)
385 {
386 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
387 		return;
388 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
389 		goto out_unlock;
390 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
391 				__xprt_lock_write_func, xprt))
392 		return;
393 out_unlock:
394 	xprt_clear_locked(xprt);
395 }
396 
397 static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
398 {
399 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
400 		return;
401 	if (test_bit(XPRT_WRITE_SPACE, &xprt->state))
402 		goto out_unlock;
403 	if (xprt_need_congestion_window_wait(xprt))
404 		goto out_unlock;
405 	if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
406 				__xprt_lock_write_func, xprt))
407 		return;
408 out_unlock:
409 	xprt_clear_locked(xprt);
410 }
411 
412 /**
413  * xprt_release_xprt - allow other requests to use a transport
414  * @xprt: transport with other tasks potentially waiting
415  * @task: task that is releasing access to the transport
416  *
417  * Note that "task" can be NULL.  No congestion control is provided.
418  */
419 void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
420 {
421 	if (xprt->snd_task == task) {
422 		xprt_clear_locked(xprt);
423 		__xprt_lock_write_next(xprt);
424 	}
425 	trace_xprt_release_xprt(xprt, task);
426 }
427 EXPORT_SYMBOL_GPL(xprt_release_xprt);
428 
429 /**
430  * xprt_release_xprt_cong - allow other requests to use a transport
431  * @xprt: transport with other tasks potentially waiting
432  * @task: task that is releasing access to the transport
433  *
434  * Note that "task" can be NULL.  Another task is awoken to use the
435  * transport if the transport's congestion window allows it.
436  */
437 void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
438 {
439 	if (xprt->snd_task == task) {
440 		xprt_clear_locked(xprt);
441 		__xprt_lock_write_next_cong(xprt);
442 	}
443 	trace_xprt_release_cong(xprt, task);
444 }
445 EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
446 
447 void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
448 {
449 	if (xprt->snd_task != task)
450 		return;
451 	spin_lock(&xprt->transport_lock);
452 	xprt->ops->release_xprt(xprt, task);
453 	spin_unlock(&xprt->transport_lock);
454 }
455 
456 /*
457  * Van Jacobson congestion avoidance. Check if the congestion window
458  * overflowed. Put the task to sleep if this is the case.
459  */
460 static int
461 __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
462 {
463 	if (req->rq_cong)
464 		return 1;
465 	trace_xprt_get_cong(xprt, req->rq_task);
466 	if (RPCXPRT_CONGESTED(xprt)) {
467 		xprt_set_congestion_window_wait(xprt);
468 		return 0;
469 	}
470 	req->rq_cong = 1;
471 	xprt->cong += RPC_CWNDSCALE;
472 	return 1;
473 }
474 
475 /*
476  * Adjust the congestion window, and wake up the next task
477  * that has been sleeping due to congestion
478  */
479 static void
480 __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
481 {
482 	if (!req->rq_cong)
483 		return;
484 	req->rq_cong = 0;
485 	xprt->cong -= RPC_CWNDSCALE;
486 	xprt_test_and_clear_congestion_window_wait(xprt);
487 	trace_xprt_put_cong(xprt, req->rq_task);
488 	__xprt_lock_write_next_cong(xprt);
489 }
490 
491 /**
492  * xprt_request_get_cong - Request congestion control credits
493  * @xprt: pointer to transport
494  * @req: pointer to RPC request
495  *
496  * Useful for transports that require congestion control.
497  */
498 bool
499 xprt_request_get_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
500 {
501 	bool ret = false;
502 
503 	if (req->rq_cong)
504 		return true;
505 	spin_lock(&xprt->transport_lock);
506 	ret = __xprt_get_cong(xprt, req) != 0;
507 	spin_unlock(&xprt->transport_lock);
508 	return ret;
509 }
510 EXPORT_SYMBOL_GPL(xprt_request_get_cong);
511 
512 /**
513  * xprt_release_rqst_cong - housekeeping when request is complete
514  * @task: RPC request that recently completed
515  *
516  * Useful for transports that require congestion control.
517  */
518 void xprt_release_rqst_cong(struct rpc_task *task)
519 {
520 	struct rpc_rqst *req = task->tk_rqstp;
521 
522 	__xprt_put_cong(req->rq_xprt, req);
523 }
524 EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
525 
526 static void xprt_clear_congestion_window_wait_locked(struct rpc_xprt *xprt)
527 {
528 	if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state))
529 		__xprt_lock_write_next_cong(xprt);
530 }
531 
532 /*
533  * Clear the congestion window wait flag and wake up the next
534  * entry on xprt->sending
535  */
536 static void
537 xprt_clear_congestion_window_wait(struct rpc_xprt *xprt)
538 {
539 	if (test_and_clear_bit(XPRT_CWND_WAIT, &xprt->state)) {
540 		spin_lock(&xprt->transport_lock);
541 		__xprt_lock_write_next_cong(xprt);
542 		spin_unlock(&xprt->transport_lock);
543 	}
544 }
545 
546 /**
547  * xprt_adjust_cwnd - adjust transport congestion window
548  * @xprt: pointer to xprt
549  * @task: recently completed RPC request used to adjust window
550  * @result: result code of completed RPC request
551  *
552  * The transport code maintains an estimate on the maximum number of out-
553  * standing RPC requests, using a smoothed version of the congestion
554  * avoidance implemented in 44BSD. This is basically the Van Jacobson
555  * congestion algorithm: If a retransmit occurs, the congestion window is
556  * halved; otherwise, it is incremented by 1/cwnd when
557  *
558  *	-	a reply is received and
559  *	-	a full number of requests are outstanding and
560  *	-	the congestion window hasn't been updated recently.
561  */
562 void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
563 {
564 	struct rpc_rqst *req = task->tk_rqstp;
565 	unsigned long cwnd = xprt->cwnd;
566 
567 	if (result >= 0 && cwnd <= xprt->cong) {
568 		/* The (cwnd >> 1) term makes sure
569 		 * the result gets rounded properly. */
570 		cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
571 		if (cwnd > RPC_MAXCWND(xprt))
572 			cwnd = RPC_MAXCWND(xprt);
573 		__xprt_lock_write_next_cong(xprt);
574 	} else if (result == -ETIMEDOUT) {
575 		cwnd >>= 1;
576 		if (cwnd < RPC_CWNDSCALE)
577 			cwnd = RPC_CWNDSCALE;
578 	}
579 	dprintk("RPC:       cong %ld, cwnd was %ld, now %ld\n",
580 			xprt->cong, xprt->cwnd, cwnd);
581 	xprt->cwnd = cwnd;
582 	__xprt_put_cong(xprt, req);
583 }
584 EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
585 
586 /**
587  * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
588  * @xprt: transport with waiting tasks
589  * @status: result code to plant in each task before waking it
590  *
591  */
592 void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
593 {
594 	if (status < 0)
595 		rpc_wake_up_status(&xprt->pending, status);
596 	else
597 		rpc_wake_up(&xprt->pending);
598 }
599 EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
600 
601 /**
602  * xprt_wait_for_buffer_space - wait for transport output buffer to clear
603  * @xprt: transport
604  *
605  * Note that we only set the timer for the case of RPC_IS_SOFT(), since
606  * we don't in general want to force a socket disconnection due to
607  * an incomplete RPC call transmission.
608  */
609 void xprt_wait_for_buffer_space(struct rpc_xprt *xprt)
610 {
611 	set_bit(XPRT_WRITE_SPACE, &xprt->state);
612 }
613 EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
614 
615 static bool
616 xprt_clear_write_space_locked(struct rpc_xprt *xprt)
617 {
618 	if (test_and_clear_bit(XPRT_WRITE_SPACE, &xprt->state)) {
619 		__xprt_lock_write_next(xprt);
620 		dprintk("RPC:       write space: waking waiting task on "
621 				"xprt %p\n", xprt);
622 		return true;
623 	}
624 	return false;
625 }
626 
627 /**
628  * xprt_write_space - wake the task waiting for transport output buffer space
629  * @xprt: transport with waiting tasks
630  *
631  * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
632  */
633 bool xprt_write_space(struct rpc_xprt *xprt)
634 {
635 	bool ret;
636 
637 	if (!test_bit(XPRT_WRITE_SPACE, &xprt->state))
638 		return false;
639 	spin_lock(&xprt->transport_lock);
640 	ret = xprt_clear_write_space_locked(xprt);
641 	spin_unlock(&xprt->transport_lock);
642 	return ret;
643 }
644 EXPORT_SYMBOL_GPL(xprt_write_space);
645 
646 static unsigned long xprt_abs_ktime_to_jiffies(ktime_t abstime)
647 {
648 	s64 delta = ktime_to_ns(ktime_get() - abstime);
649 	return likely(delta >= 0) ?
650 		jiffies - nsecs_to_jiffies(delta) :
651 		jiffies + nsecs_to_jiffies(-delta);
652 }
653 
654 static unsigned long xprt_calc_majortimeo(struct rpc_rqst *req,
655 		const struct rpc_timeout *to)
656 {
657 	unsigned long majortimeo = req->rq_timeout;
658 
659 	if (to->to_exponential)
660 		majortimeo <<= to->to_retries;
661 	else
662 		majortimeo += to->to_increment * to->to_retries;
663 	if (majortimeo > to->to_maxval || majortimeo == 0)
664 		majortimeo = to->to_maxval;
665 	return majortimeo;
666 }
667 
668 static void xprt_reset_majortimeo(struct rpc_rqst *req,
669 		const struct rpc_timeout *to)
670 {
671 	req->rq_majortimeo += xprt_calc_majortimeo(req, to);
672 }
673 
674 static void xprt_reset_minortimeo(struct rpc_rqst *req)
675 {
676 	req->rq_minortimeo += req->rq_timeout;
677 }
678 
679 static void xprt_init_majortimeo(struct rpc_task *task, struct rpc_rqst *req,
680 		const struct rpc_timeout *to)
681 {
682 	unsigned long time_init;
683 	struct rpc_xprt *xprt = req->rq_xprt;
684 
685 	if (likely(xprt && xprt_connected(xprt)))
686 		time_init = jiffies;
687 	else
688 		time_init = xprt_abs_ktime_to_jiffies(task->tk_start);
689 
690 	req->rq_timeout = to->to_initval;
691 	req->rq_majortimeo = time_init + xprt_calc_majortimeo(req, to);
692 	req->rq_minortimeo = time_init + req->rq_timeout;
693 }
694 
695 /**
696  * xprt_adjust_timeout - adjust timeout values for next retransmit
697  * @req: RPC request containing parameters to use for the adjustment
698  *
699  */
700 int xprt_adjust_timeout(struct rpc_rqst *req)
701 {
702 	struct rpc_xprt *xprt = req->rq_xprt;
703 	const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
704 	int status = 0;
705 
706 	if (time_before(jiffies, req->rq_majortimeo)) {
707 		if (time_before(jiffies, req->rq_minortimeo))
708 			return status;
709 		if (to->to_exponential)
710 			req->rq_timeout <<= 1;
711 		else
712 			req->rq_timeout += to->to_increment;
713 		if (to->to_maxval && req->rq_timeout >= to->to_maxval)
714 			req->rq_timeout = to->to_maxval;
715 		req->rq_retries++;
716 	} else {
717 		req->rq_timeout = to->to_initval;
718 		req->rq_retries = 0;
719 		xprt_reset_majortimeo(req, to);
720 		/* Reset the RTT counters == "slow start" */
721 		spin_lock(&xprt->transport_lock);
722 		rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
723 		spin_unlock(&xprt->transport_lock);
724 		status = -ETIMEDOUT;
725 	}
726 	xprt_reset_minortimeo(req);
727 
728 	if (req->rq_timeout == 0) {
729 		printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
730 		req->rq_timeout = 5 * HZ;
731 	}
732 	return status;
733 }
734 
735 static void xprt_autoclose(struct work_struct *work)
736 {
737 	struct rpc_xprt *xprt =
738 		container_of(work, struct rpc_xprt, task_cleanup);
739 	unsigned int pflags = memalloc_nofs_save();
740 
741 	trace_xprt_disconnect_auto(xprt);
742 	xprt->connect_cookie++;
743 	smp_mb__before_atomic();
744 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
745 	xprt->ops->close(xprt);
746 	xprt_release_write(xprt, NULL);
747 	wake_up_bit(&xprt->state, XPRT_LOCKED);
748 	memalloc_nofs_restore(pflags);
749 }
750 
751 /**
752  * xprt_disconnect_done - mark a transport as disconnected
753  * @xprt: transport to flag for disconnect
754  *
755  */
756 void xprt_disconnect_done(struct rpc_xprt *xprt)
757 {
758 	trace_xprt_disconnect_done(xprt);
759 	spin_lock(&xprt->transport_lock);
760 	xprt_clear_connected(xprt);
761 	xprt_clear_write_space_locked(xprt);
762 	xprt_clear_congestion_window_wait_locked(xprt);
763 	xprt_wake_pending_tasks(xprt, -ENOTCONN);
764 	spin_unlock(&xprt->transport_lock);
765 }
766 EXPORT_SYMBOL_GPL(xprt_disconnect_done);
767 
768 /**
769  * xprt_schedule_autoclose_locked - Try to schedule an autoclose RPC call
770  * @xprt: transport to disconnect
771  */
772 static void xprt_schedule_autoclose_locked(struct rpc_xprt *xprt)
773 {
774 	if (test_and_set_bit(XPRT_CLOSE_WAIT, &xprt->state))
775 		return;
776 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
777 		queue_work(xprtiod_workqueue, &xprt->task_cleanup);
778 	else if (xprt->snd_task && !test_bit(XPRT_SND_IS_COOKIE, &xprt->state))
779 		rpc_wake_up_queued_task_set_status(&xprt->pending,
780 						   xprt->snd_task, -ENOTCONN);
781 }
782 
783 /**
784  * xprt_force_disconnect - force a transport to disconnect
785  * @xprt: transport to disconnect
786  *
787  */
788 void xprt_force_disconnect(struct rpc_xprt *xprt)
789 {
790 	trace_xprt_disconnect_force(xprt);
791 
792 	/* Don't race with the test_bit() in xprt_clear_locked() */
793 	spin_lock(&xprt->transport_lock);
794 	xprt_schedule_autoclose_locked(xprt);
795 	spin_unlock(&xprt->transport_lock);
796 }
797 EXPORT_SYMBOL_GPL(xprt_force_disconnect);
798 
799 static unsigned int
800 xprt_connect_cookie(struct rpc_xprt *xprt)
801 {
802 	return READ_ONCE(xprt->connect_cookie);
803 }
804 
805 static bool
806 xprt_request_retransmit_after_disconnect(struct rpc_task *task)
807 {
808 	struct rpc_rqst *req = task->tk_rqstp;
809 	struct rpc_xprt *xprt = req->rq_xprt;
810 
811 	return req->rq_connect_cookie != xprt_connect_cookie(xprt) ||
812 		!xprt_connected(xprt);
813 }
814 
815 static void xprt_conditional_disconnect_locked(struct rpc_xprt *xprt,
816 					       unsigned int cookie)
817 	__must_hold(&xprt->transport_lock)
818 {
819 	if (cookie != xprt->connect_cookie)
820 		return;
821 	if (test_bit(XPRT_CLOSING, &xprt->state))
822 		return;
823 	xprt_schedule_autoclose_locked(xprt);
824 }
825 
826 /**
827  * xprt_conditional_disconnect - force a transport to disconnect
828  * @xprt: transport to disconnect
829  * @cookie: 'connection cookie'
830  *
831  * This attempts to break the connection if and only if 'cookie' matches
832  * the current transport 'connection cookie'. It ensures that we don't
833  * try to break the connection more than once when we need to retransmit
834  * a batch of RPC requests.
835  *
836  */
837 void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
838 {
839 	/* Don't race with the test_bit() in xprt_clear_locked() */
840 	spin_lock(&xprt->transport_lock);
841 	xprt_conditional_disconnect_locked(xprt, cookie);
842 	spin_unlock(&xprt->transport_lock);
843 }
844 
845 static bool
846 xprt_has_timer(const struct rpc_xprt *xprt)
847 {
848 	return xprt->idle_timeout != 0;
849 }
850 
851 static void
852 xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
853 	__must_hold(&xprt->transport_lock)
854 {
855 	xprt->last_used = jiffies;
856 	if (RB_EMPTY_ROOT(&xprt->recv_queue) && xprt_has_timer(xprt))
857 		mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
858 }
859 
860 static void
861 xprt_init_autodisconnect(struct timer_list *t)
862 {
863 	struct rpc_xprt *xprt = timer_container_of(xprt, t, timer);
864 
865 	if (!RB_EMPTY_ROOT(&xprt->recv_queue))
866 		return;
867 	/* Reset xprt->last_used to avoid connect/autodisconnect cycling */
868 	xprt->last_used = jiffies;
869 	if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
870 		return;
871 	queue_work(xprtiod_workqueue, &xprt->task_cleanup);
872 }
873 
874 #if IS_ENABLED(CONFIG_FAIL_SUNRPC)
875 static void xprt_inject_disconnect(struct rpc_xprt *xprt)
876 {
877 	if (!fail_sunrpc.ignore_client_disconnect &&
878 	    should_fail(&fail_sunrpc.attr, 1))
879 		xprt->ops->inject_disconnect(xprt);
880 }
881 #else
882 static inline void xprt_inject_disconnect(struct rpc_xprt *xprt)
883 {
884 }
885 #endif
886 
887 bool xprt_lock_connect(struct rpc_xprt *xprt,
888 		struct rpc_task *task,
889 		void *cookie)
890 {
891 	bool ret = false;
892 
893 	spin_lock(&xprt->transport_lock);
894 	if (!test_bit(XPRT_LOCKED, &xprt->state))
895 		goto out;
896 	if (xprt->snd_task != task)
897 		goto out;
898 	set_bit(XPRT_SND_IS_COOKIE, &xprt->state);
899 	xprt->snd_task = cookie;
900 	ret = true;
901 out:
902 	spin_unlock(&xprt->transport_lock);
903 	return ret;
904 }
905 EXPORT_SYMBOL_GPL(xprt_lock_connect);
906 
907 void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
908 {
909 	spin_lock(&xprt->transport_lock);
910 	if (xprt->snd_task != cookie)
911 		goto out;
912 	if (!test_bit(XPRT_LOCKED, &xprt->state))
913 		goto out;
914 	xprt->snd_task =NULL;
915 	clear_bit(XPRT_SND_IS_COOKIE, &xprt->state);
916 	xprt->ops->release_xprt(xprt, NULL);
917 	xprt_schedule_autodisconnect(xprt);
918 out:
919 	spin_unlock(&xprt->transport_lock);
920 	wake_up_bit(&xprt->state, XPRT_LOCKED);
921 }
922 EXPORT_SYMBOL_GPL(xprt_unlock_connect);
923 
924 /**
925  * xprt_connect - schedule a transport connect operation
926  * @task: RPC task that is requesting the connect
927  *
928  */
929 void xprt_connect(struct rpc_task *task)
930 {
931 	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
932 
933 	trace_xprt_connect(xprt);
934 
935 	if (!xprt_bound(xprt)) {
936 		task->tk_status = -EAGAIN;
937 		return;
938 	}
939 	if (!xprt_lock_write(xprt, task))
940 		return;
941 
942 	if (!xprt_connected(xprt) && !test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
943 		task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
944 		rpc_sleep_on_timeout(&xprt->pending, task, NULL,
945 				xprt_request_timeout(task->tk_rqstp));
946 
947 		if (test_bit(XPRT_CLOSING, &xprt->state))
948 			return;
949 		if (xprt_test_and_set_connecting(xprt))
950 			return;
951 		/* Race breaker */
952 		if (!xprt_connected(xprt)) {
953 			xprt->stat.connect_start = jiffies;
954 			xprt->ops->connect(xprt, task);
955 		} else {
956 			xprt_clear_connecting(xprt);
957 			task->tk_status = 0;
958 			rpc_wake_up_queued_task(&xprt->pending, task);
959 		}
960 	}
961 	xprt_release_write(xprt, task);
962 }
963 
964 /**
965  * xprt_reconnect_delay - compute the wait before scheduling a connect
966  * @xprt: transport instance
967  *
968  */
969 unsigned long xprt_reconnect_delay(const struct rpc_xprt *xprt)
970 {
971 	unsigned long start, now = jiffies;
972 
973 	start = xprt->stat.connect_start + xprt->reestablish_timeout;
974 	if (time_after(start, now))
975 		return start - now;
976 	return 0;
977 }
978 EXPORT_SYMBOL_GPL(xprt_reconnect_delay);
979 
980 /**
981  * xprt_reconnect_backoff - compute the new re-establish timeout
982  * @xprt: transport instance
983  * @init_to: initial reestablish timeout
984  *
985  */
986 void xprt_reconnect_backoff(struct rpc_xprt *xprt, unsigned long init_to)
987 {
988 	xprt->reestablish_timeout <<= 1;
989 	if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
990 		xprt->reestablish_timeout = xprt->max_reconnect_timeout;
991 	if (xprt->reestablish_timeout < init_to)
992 		xprt->reestablish_timeout = init_to;
993 }
994 EXPORT_SYMBOL_GPL(xprt_reconnect_backoff);
995 
996 enum xprt_xid_rb_cmp {
997 	XID_RB_EQUAL,
998 	XID_RB_LEFT,
999 	XID_RB_RIGHT,
1000 };
1001 static enum xprt_xid_rb_cmp
1002 xprt_xid_cmp(__be32 xid1, __be32 xid2)
1003 {
1004 	if (xid1 == xid2)
1005 		return XID_RB_EQUAL;
1006 	if ((__force u32)xid1 < (__force u32)xid2)
1007 		return XID_RB_LEFT;
1008 	return XID_RB_RIGHT;
1009 }
1010 
1011 static struct rpc_rqst *
1012 xprt_request_rb_find(struct rpc_xprt *xprt, __be32 xid)
1013 {
1014 	struct rb_node *n = xprt->recv_queue.rb_node;
1015 	struct rpc_rqst *req;
1016 
1017 	while (n != NULL) {
1018 		req = rb_entry(n, struct rpc_rqst, rq_recv);
1019 		switch (xprt_xid_cmp(xid, req->rq_xid)) {
1020 		case XID_RB_LEFT:
1021 			n = n->rb_left;
1022 			break;
1023 		case XID_RB_RIGHT:
1024 			n = n->rb_right;
1025 			break;
1026 		case XID_RB_EQUAL:
1027 			return req;
1028 		}
1029 	}
1030 	return NULL;
1031 }
1032 
1033 static void
1034 xprt_request_rb_insert(struct rpc_xprt *xprt, struct rpc_rqst *new)
1035 {
1036 	struct rb_node **p = &xprt->recv_queue.rb_node;
1037 	struct rb_node *n = NULL;
1038 	struct rpc_rqst *req;
1039 
1040 	while (*p != NULL) {
1041 		n = *p;
1042 		req = rb_entry(n, struct rpc_rqst, rq_recv);
1043 		switch(xprt_xid_cmp(new->rq_xid, req->rq_xid)) {
1044 		case XID_RB_LEFT:
1045 			p = &n->rb_left;
1046 			break;
1047 		case XID_RB_RIGHT:
1048 			p = &n->rb_right;
1049 			break;
1050 		case XID_RB_EQUAL:
1051 			WARN_ON_ONCE(new != req);
1052 			return;
1053 		}
1054 	}
1055 	rb_link_node(&new->rq_recv, n, p);
1056 	rb_insert_color(&new->rq_recv, &xprt->recv_queue);
1057 }
1058 
1059 static void
1060 xprt_request_rb_remove(struct rpc_xprt *xprt, struct rpc_rqst *req)
1061 {
1062 	rb_erase(&req->rq_recv, &xprt->recv_queue);
1063 }
1064 
1065 /**
1066  * xprt_lookup_rqst - find an RPC request corresponding to an XID
1067  * @xprt: transport on which the original request was transmitted
1068  * @xid: RPC XID of incoming reply
1069  *
1070  * Caller holds xprt->queue_lock.
1071  */
1072 struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
1073 {
1074 	struct rpc_rqst *entry;
1075 
1076 	entry = xprt_request_rb_find(xprt, xid);
1077 	if (entry != NULL) {
1078 		trace_xprt_lookup_rqst(xprt, xid, 0);
1079 		entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
1080 		return entry;
1081 	}
1082 
1083 	dprintk("RPC:       xprt_lookup_rqst did not find xid %08x\n",
1084 			ntohl(xid));
1085 	trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
1086 	xprt->stat.bad_xids++;
1087 	return NULL;
1088 }
1089 EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
1090 
1091 static bool
1092 xprt_is_pinned_rqst(struct rpc_rqst *req)
1093 {
1094 	return atomic_read(&req->rq_pin) != 0;
1095 }
1096 
1097 /**
1098  * xprt_pin_rqst - Pin a request on the transport receive list
1099  * @req: Request to pin
1100  *
1101  * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
1102  * so should be holding xprt->queue_lock.
1103  */
1104 void xprt_pin_rqst(struct rpc_rqst *req)
1105 {
1106 	atomic_inc(&req->rq_pin);
1107 }
1108 EXPORT_SYMBOL_GPL(xprt_pin_rqst);
1109 
1110 /**
1111  * xprt_unpin_rqst - Unpin a request on the transport receive list
1112  * @req: Request to pin
1113  *
1114  * Caller should be holding xprt->queue_lock.
1115  */
1116 void xprt_unpin_rqst(struct rpc_rqst *req)
1117 {
1118 	if (!test_bit(RPC_TASK_MSG_PIN_WAIT, &req->rq_task->tk_runstate)) {
1119 		atomic_dec(&req->rq_pin);
1120 		return;
1121 	}
1122 	if (atomic_dec_and_test(&req->rq_pin))
1123 		wake_up_var(&req->rq_pin);
1124 }
1125 EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
1126 
1127 static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
1128 {
1129 	wait_var_event(&req->rq_pin, !xprt_is_pinned_rqst(req));
1130 }
1131 
1132 static bool
1133 xprt_request_data_received(struct rpc_task *task)
1134 {
1135 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1136 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) != 0;
1137 }
1138 
1139 static bool
1140 xprt_request_need_enqueue_receive(struct rpc_task *task, struct rpc_rqst *req)
1141 {
1142 	return !test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) &&
1143 		READ_ONCE(task->tk_rqstp->rq_reply_bytes_recvd) == 0;
1144 }
1145 
1146 /**
1147  * xprt_request_enqueue_receive - Add an request to the receive queue
1148  * @task: RPC task
1149  *
1150  */
1151 int
1152 xprt_request_enqueue_receive(struct rpc_task *task)
1153 {
1154 	struct rpc_rqst *req = task->tk_rqstp;
1155 	struct rpc_xprt *xprt = req->rq_xprt;
1156 	int ret;
1157 
1158 	if (!xprt_request_need_enqueue_receive(task, req))
1159 		return 0;
1160 
1161 	ret = xprt_request_prepare(task->tk_rqstp, &req->rq_rcv_buf);
1162 	if (ret)
1163 		return ret;
1164 	spin_lock(&xprt->queue_lock);
1165 
1166 	/* Update the softirq receive buffer */
1167 	memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1168 			sizeof(req->rq_private_buf));
1169 
1170 	/* Add request to the receive list */
1171 	xprt_request_rb_insert(xprt, req);
1172 	set_bit(RPC_TASK_NEED_RECV, &task->tk_runstate);
1173 	spin_unlock(&xprt->queue_lock);
1174 
1175 	/* Turn off autodisconnect */
1176 	timer_delete_sync(&xprt->timer);
1177 	return 0;
1178 }
1179 
1180 /**
1181  * xprt_request_dequeue_receive_locked - Remove a request from the receive queue
1182  * @task: RPC task
1183  *
1184  * Caller must hold xprt->queue_lock.
1185  */
1186 static void
1187 xprt_request_dequeue_receive_locked(struct rpc_task *task)
1188 {
1189 	struct rpc_rqst *req = task->tk_rqstp;
1190 
1191 	if (test_and_clear_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1192 		xprt_request_rb_remove(req->rq_xprt, req);
1193 }
1194 
1195 /**
1196  * xprt_update_rtt - Update RPC RTT statistics
1197  * @task: RPC request that recently completed
1198  *
1199  * Caller holds xprt->queue_lock.
1200  */
1201 void xprt_update_rtt(struct rpc_task *task)
1202 {
1203 	struct rpc_rqst *req = task->tk_rqstp;
1204 	struct rpc_rtt *rtt = task->tk_client->cl_rtt;
1205 	unsigned int timer = task->tk_msg.rpc_proc->p_timer;
1206 	long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
1207 
1208 	if (timer) {
1209 		if (req->rq_ntrans == 1)
1210 			rpc_update_rtt(rtt, timer, m);
1211 		rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
1212 	}
1213 }
1214 EXPORT_SYMBOL_GPL(xprt_update_rtt);
1215 
1216 /**
1217  * xprt_complete_rqst - called when reply processing is complete
1218  * @task: RPC request that recently completed
1219  * @copied: actual number of bytes received from the transport
1220  *
1221  * Caller holds xprt->queue_lock.
1222  */
1223 void xprt_complete_rqst(struct rpc_task *task, int copied)
1224 {
1225 	struct rpc_rqst *req = task->tk_rqstp;
1226 	struct rpc_xprt *xprt = req->rq_xprt;
1227 
1228 	xprt->stat.recvs++;
1229 
1230 	xdr_free_bvec(&req->rq_rcv_buf);
1231 	req->rq_private_buf.bvec = NULL;
1232 	req->rq_private_buf.len = copied;
1233 	/* Ensure all writes are done before we update */
1234 	/* req->rq_reply_bytes_recvd */
1235 	smp_wmb();
1236 	req->rq_reply_bytes_recvd = copied;
1237 	xprt_request_dequeue_receive_locked(task);
1238 	rpc_wake_up_queued_task(&xprt->pending, task);
1239 }
1240 EXPORT_SYMBOL_GPL(xprt_complete_rqst);
1241 
1242 static void xprt_timer(struct rpc_task *task)
1243 {
1244 	struct rpc_rqst *req = task->tk_rqstp;
1245 	struct rpc_xprt *xprt = req->rq_xprt;
1246 
1247 	if (task->tk_status != -ETIMEDOUT)
1248 		return;
1249 
1250 	trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
1251 	if (!req->rq_reply_bytes_recvd) {
1252 		if (xprt->ops->timer)
1253 			xprt->ops->timer(xprt, task);
1254 	} else
1255 		task->tk_status = 0;
1256 }
1257 
1258 /**
1259  * xprt_wait_for_reply_request_def - wait for reply
1260  * @task: pointer to rpc_task
1261  *
1262  * Set a request's retransmit timeout based on the transport's
1263  * default timeout parameters.  Used by transports that don't adjust
1264  * the retransmit timeout based on round-trip time estimation,
1265  * and put the task to sleep on the pending queue.
1266  */
1267 void xprt_wait_for_reply_request_def(struct rpc_task *task)
1268 {
1269 	struct rpc_rqst *req = task->tk_rqstp;
1270 
1271 	rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
1272 			xprt_request_timeout(req));
1273 }
1274 EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_def);
1275 
1276 /**
1277  * xprt_wait_for_reply_request_rtt - wait for reply using RTT estimator
1278  * @task: pointer to rpc_task
1279  *
1280  * Set a request's retransmit timeout using the RTT estimator,
1281  * and put the task to sleep on the pending queue.
1282  */
1283 void xprt_wait_for_reply_request_rtt(struct rpc_task *task)
1284 {
1285 	int timer = task->tk_msg.rpc_proc->p_timer;
1286 	struct rpc_clnt *clnt = task->tk_client;
1287 	struct rpc_rtt *rtt = clnt->cl_rtt;
1288 	struct rpc_rqst *req = task->tk_rqstp;
1289 	unsigned long max_timeout = clnt->cl_timeout->to_maxval;
1290 	unsigned long timeout;
1291 
1292 	timeout = rpc_calc_rto(rtt, timer);
1293 	timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
1294 	if (timeout > max_timeout || timeout == 0)
1295 		timeout = max_timeout;
1296 	rpc_sleep_on_timeout(&req->rq_xprt->pending, task, xprt_timer,
1297 			jiffies + timeout);
1298 }
1299 EXPORT_SYMBOL_GPL(xprt_wait_for_reply_request_rtt);
1300 
1301 /**
1302  * xprt_request_wait_receive - wait for the reply to an RPC request
1303  * @task: RPC task about to send a request
1304  *
1305  */
1306 void xprt_request_wait_receive(struct rpc_task *task)
1307 {
1308 	struct rpc_rqst *req = task->tk_rqstp;
1309 	struct rpc_xprt *xprt = req->rq_xprt;
1310 
1311 	if (!test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate))
1312 		return;
1313 	/*
1314 	 * Sleep on the pending queue if we're expecting a reply.
1315 	 * The spinlock ensures atomicity between the test of
1316 	 * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1317 	 */
1318 	spin_lock(&xprt->queue_lock);
1319 	if (test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate)) {
1320 		xprt->ops->wait_for_reply_request(task);
1321 		/*
1322 		 * Send an extra queue wakeup call if the
1323 		 * connection was dropped in case the call to
1324 		 * rpc_sleep_on() raced.
1325 		 */
1326 		if (xprt_request_retransmit_after_disconnect(task))
1327 			rpc_wake_up_queued_task_set_status(&xprt->pending,
1328 					task, -ENOTCONN);
1329 	}
1330 	spin_unlock(&xprt->queue_lock);
1331 }
1332 
1333 static bool
1334 xprt_request_need_enqueue_transmit(struct rpc_task *task, struct rpc_rqst *req)
1335 {
1336 	return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1337 }
1338 
1339 /**
1340  * xprt_request_enqueue_transmit - queue a task for transmission
1341  * @task: pointer to rpc_task
1342  *
1343  * Add a task to the transmission queue.
1344  */
1345 void
1346 xprt_request_enqueue_transmit(struct rpc_task *task)
1347 {
1348 	struct rpc_rqst *pos, *req = task->tk_rqstp;
1349 	struct rpc_xprt *xprt = req->rq_xprt;
1350 	int ret;
1351 
1352 	if (xprt_request_need_enqueue_transmit(task, req)) {
1353 		ret = xprt_request_prepare(task->tk_rqstp, &req->rq_snd_buf);
1354 		if (ret) {
1355 			task->tk_status = ret;
1356 			return;
1357 		}
1358 		req->rq_bytes_sent = 0;
1359 		spin_lock(&xprt->queue_lock);
1360 		/*
1361 		 * Requests that carry congestion control credits are added
1362 		 * to the head of the list to avoid starvation issues.
1363 		 */
1364 		if (req->rq_cong) {
1365 			xprt_clear_congestion_window_wait(xprt);
1366 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1367 				if (pos->rq_cong)
1368 					continue;
1369 				/* Note: req is added _before_ pos */
1370 				list_add_tail(&req->rq_xmit, &pos->rq_xmit);
1371 				INIT_LIST_HEAD(&req->rq_xmit2);
1372 				goto out;
1373 			}
1374 		} else if (req->rq_seqno_count == 0) {
1375 			list_for_each_entry(pos, &xprt->xmit_queue, rq_xmit) {
1376 				if (pos->rq_task->tk_owner != task->tk_owner)
1377 					continue;
1378 				list_add_tail(&req->rq_xmit2, &pos->rq_xmit2);
1379 				INIT_LIST_HEAD(&req->rq_xmit);
1380 				goto out;
1381 			}
1382 		}
1383 		list_add_tail(&req->rq_xmit, &xprt->xmit_queue);
1384 		INIT_LIST_HEAD(&req->rq_xmit2);
1385 out:
1386 		atomic_long_inc(&xprt->xmit_queuelen);
1387 		set_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1388 		spin_unlock(&xprt->queue_lock);
1389 	}
1390 }
1391 
1392 /**
1393  * xprt_request_dequeue_transmit_locked - remove a task from the transmission queue
1394  * @task: pointer to rpc_task
1395  *
1396  * Remove a task from the transmission queue
1397  * Caller must hold xprt->queue_lock
1398  */
1399 static void
1400 xprt_request_dequeue_transmit_locked(struct rpc_task *task)
1401 {
1402 	struct rpc_rqst *req = task->tk_rqstp;
1403 
1404 	if (!test_and_clear_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1405 		return;
1406 	if (!list_empty(&req->rq_xmit)) {
1407 		struct rpc_xprt *xprt = req->rq_xprt;
1408 
1409 		if (list_is_first(&req->rq_xmit, &xprt->xmit_queue) &&
1410 		    xprt->ops->abort_send_request)
1411 			xprt->ops->abort_send_request(req);
1412 
1413 		list_del(&req->rq_xmit);
1414 		if (!list_empty(&req->rq_xmit2)) {
1415 			struct rpc_rqst *next = list_first_entry(&req->rq_xmit2,
1416 					struct rpc_rqst, rq_xmit2);
1417 			list_del(&req->rq_xmit2);
1418 			list_add_tail(&next->rq_xmit, &next->rq_xprt->xmit_queue);
1419 		}
1420 	} else
1421 		list_del(&req->rq_xmit2);
1422 	atomic_long_dec(&req->rq_xprt->xmit_queuelen);
1423 	xdr_free_bvec(&req->rq_snd_buf);
1424 }
1425 
1426 /**
1427  * xprt_request_dequeue_transmit - remove a task from the transmission queue
1428  * @task: pointer to rpc_task
1429  *
1430  * Remove a task from the transmission queue
1431  */
1432 static void
1433 xprt_request_dequeue_transmit(struct rpc_task *task)
1434 {
1435 	struct rpc_rqst *req = task->tk_rqstp;
1436 	struct rpc_xprt *xprt = req->rq_xprt;
1437 
1438 	spin_lock(&xprt->queue_lock);
1439 	xprt_request_dequeue_transmit_locked(task);
1440 	spin_unlock(&xprt->queue_lock);
1441 }
1442 
1443 static bool
1444 xprt_request_matches(const struct rpc_rqst *req,
1445 		     const struct rpc_clnt *clnt,
1446 		     bool (*fnmatch)(const struct rpc_task *, const void *),
1447 		     const void *data)
1448 {
1449 	const struct rpc_task *task = req->rq_task;
1450 
1451 	return task && task->tk_client == clnt && fnmatch(task, data);
1452 }
1453 
1454 static void
1455 xprt_request_mark_disconnect_on_release(struct rpc_xprt *xprt,
1456 					struct rpc_rqst *req)
1457 	__must_hold(&xprt->queue_lock)
1458 {
1459 	spin_lock(&xprt->transport_lock);
1460 	req->rq_disconnect_on_release = true;
1461 	spin_unlock(&xprt->transport_lock);
1462 }
1463 
1464 /**
1465  * xprt_mark_matching_reqs_disconnect_on_release - mark matching in-flight
1466  *                                     requests for connection-generation
1467  *                                     teardown at release
1468  * @xprt: transport whose queues to walk
1469  * @clnt: RPC client whose requests are eligible
1470  * @fnmatch: selector called with the request's owning rpc_task
1471  * @data: caller data forwarded to @fnmatch
1472  *
1473  * @fnmatch must not sleep and must be safe to call while xprt->queue_lock
1474  * is held.
1475  *
1476  * Walks the transmit queue (including per-owner rq_xmit2 chains) and the
1477  * receive tree under xprt->queue_lock and marks each matching rpc_rqst so
1478  * that xprt_release() will conditionally disconnect the transport against
1479  * that request's rq_connect_cookie. Requests remain eligible for marking
1480  * while they are present on a transport queue. The marker is set under
1481  * xprt->transport_lock, nested inside xprt->queue_lock, and xprt_release()
1482  * consumes it with xprt->transport_lock held after removing queue membership.
1483  *
1484  * The @clnt qualification preserves the selection domain of
1485  * rpc_cancel_tasks(). An rpc_xprt may be shared by multiple rpc_clnt
1486  * instances, so requests belonging to other clients must not be marked.
1487  */
1488 void
1489 xprt_mark_matching_reqs_disconnect_on_release(struct rpc_xprt *xprt,
1490 					      const struct rpc_clnt *clnt,
1491 					      bool (*fnmatch)(const struct rpc_task *,
1492 							      const void *),
1493 					      const void *data)
1494 {
1495 	struct rpc_rqst *req, *pos;
1496 	struct rb_node *n;
1497 
1498 	spin_lock(&xprt->queue_lock);
1499 	list_for_each_entry(req, &xprt->xmit_queue, rq_xmit) {
1500 		if (xprt_request_matches(req, clnt, fnmatch, data))
1501 			xprt_request_mark_disconnect_on_release(xprt, req);
1502 		list_for_each_entry(pos, &req->rq_xmit2, rq_xmit2) {
1503 			if (xprt_request_matches(pos, clnt, fnmatch, data))
1504 				xprt_request_mark_disconnect_on_release(xprt,
1505 									pos);
1506 		}
1507 	}
1508 	for (n = rb_first(&xprt->recv_queue); n; n = rb_next(n)) {
1509 		req = rb_entry(n, struct rpc_rqst, rq_recv);
1510 		if (xprt_request_matches(req, clnt, fnmatch, data))
1511 			xprt_request_mark_disconnect_on_release(xprt, req);
1512 	}
1513 	spin_unlock(&xprt->queue_lock);
1514 }
1515 
1516 /**
1517  * xprt_request_dequeue_xprt - remove a task from the transmit+receive queue
1518  * @task: pointer to rpc_task
1519  *
1520  * Remove a task from the transmit and receive queues, and ensure that
1521  * it is not pinned by the receive work item.
1522  */
1523 void
1524 xprt_request_dequeue_xprt(struct rpc_task *task)
1525 {
1526 	struct rpc_rqst	*req = task->tk_rqstp;
1527 	struct rpc_xprt *xprt = req->rq_xprt;
1528 
1529 	if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) ||
1530 	    test_bit(RPC_TASK_NEED_RECV, &task->tk_runstate) ||
1531 	    xprt_is_pinned_rqst(req)) {
1532 		spin_lock(&xprt->queue_lock);
1533 		while (xprt_is_pinned_rqst(req)) {
1534 			set_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1535 			spin_unlock(&xprt->queue_lock);
1536 			xprt_wait_on_pinned_rqst(req);
1537 			spin_lock(&xprt->queue_lock);
1538 			clear_bit(RPC_TASK_MSG_PIN_WAIT, &task->tk_runstate);
1539 		}
1540 		xprt_request_dequeue_transmit_locked(task);
1541 		xprt_request_dequeue_receive_locked(task);
1542 		spin_unlock(&xprt->queue_lock);
1543 		xdr_free_bvec(&req->rq_rcv_buf);
1544 	}
1545 }
1546 
1547 /**
1548  * xprt_request_prepare - prepare an encoded request for transport
1549  * @req: pointer to rpc_rqst
1550  * @buf: pointer to send/rcv xdr_buf
1551  *
1552  * Calls into the transport layer to do whatever is needed to prepare
1553  * the request for transmission or receive.
1554  * Returns error, or zero.
1555  */
1556 static int
1557 xprt_request_prepare(struct rpc_rqst *req, struct xdr_buf *buf)
1558 {
1559 	struct rpc_xprt *xprt = req->rq_xprt;
1560 
1561 	if (xprt->ops->prepare_request)
1562 		return xprt->ops->prepare_request(req, buf);
1563 	return 0;
1564 }
1565 
1566 /**
1567  * xprt_request_need_retransmit - Test if a task needs retransmission
1568  * @task: pointer to rpc_task
1569  *
1570  * Test for whether a connection breakage requires the task to retransmit
1571  */
1572 bool
1573 xprt_request_need_retransmit(struct rpc_task *task)
1574 {
1575 	return xprt_request_retransmit_after_disconnect(task);
1576 }
1577 
1578 /**
1579  * xprt_prepare_transmit - reserve the transport before sending a request
1580  * @task: RPC task about to send a request
1581  *
1582  */
1583 bool xprt_prepare_transmit(struct rpc_task *task)
1584 {
1585 	struct rpc_rqst	*req = task->tk_rqstp;
1586 	struct rpc_xprt	*xprt = req->rq_xprt;
1587 
1588 	if (!xprt_lock_write(xprt, task)) {
1589 		/* Race breaker: someone may have transmitted us */
1590 		if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1591 			rpc_wake_up_queued_task_set_status(&xprt->sending,
1592 					task, 0);
1593 		return false;
1594 
1595 	}
1596 	if (atomic_read(&xprt->swapper))
1597 		/* This will be clear in __rpc_execute */
1598 		current->flags |= PF_MEMALLOC;
1599 	return true;
1600 }
1601 
1602 void xprt_end_transmit(struct rpc_task *task)
1603 {
1604 	struct rpc_xprt	*xprt = task->tk_rqstp->rq_xprt;
1605 
1606 	xprt_inject_disconnect(xprt);
1607 	xprt_release_write(xprt, task);
1608 }
1609 
1610 /**
1611  * xprt_request_transmit - send an RPC request on a transport
1612  * @req: pointer to request to transmit
1613  * @snd_task: RPC task that owns the transport lock
1614  *
1615  * This performs the transmission of a single request.
1616  * Note that if the request is not the same as snd_task, then it
1617  * does need to be pinned.
1618  * Returns '0' on success.
1619  */
1620 static int
1621 xprt_request_transmit(struct rpc_rqst *req, struct rpc_task *snd_task)
1622 {
1623 	struct rpc_xprt *xprt = req->rq_xprt;
1624 	struct rpc_task *task = req->rq_task;
1625 	unsigned int connect_cookie;
1626 	int is_retrans = RPC_WAS_SENT(task);
1627 	int status;
1628 
1629 	if (test_bit(XPRT_CLOSE_WAIT, &xprt->state))
1630 		return -ENOTCONN;
1631 
1632 	if (!req->rq_bytes_sent) {
1633 		if (xprt_request_data_received(task)) {
1634 			status = 0;
1635 			goto out_dequeue;
1636 		}
1637 		/* Verify that our message lies in the RPCSEC_GSS window */
1638 		if (rpcauth_xmit_need_reencode(task)) {
1639 			status = -EBADMSG;
1640 			goto out_dequeue;
1641 		}
1642 		if (RPC_SIGNALLED(task)) {
1643 			status = -ERESTARTSYS;
1644 			goto out_dequeue;
1645 		}
1646 	}
1647 
1648 	/*
1649 	 * Update req->rq_ntrans before transmitting to avoid races with
1650 	 * xprt_update_rtt(), which needs to know that it is recording a
1651 	 * reply to the first transmission.
1652 	 */
1653 	req->rq_ntrans++;
1654 
1655 	trace_rpc_xdr_sendto(task, &req->rq_snd_buf);
1656 	connect_cookie = xprt->connect_cookie;
1657 	status = xprt->ops->send_request(req);
1658 	if (status != 0) {
1659 		req->rq_ntrans--;
1660 		trace_xprt_transmit(req, status);
1661 		return status;
1662 	}
1663 
1664 	if (is_retrans) {
1665 		task->tk_client->cl_stats->rpcretrans++;
1666 		trace_xprt_retransmit(req);
1667 	}
1668 
1669 	xprt_inject_disconnect(xprt);
1670 
1671 	task->tk_flags |= RPC_TASK_SENT;
1672 	spin_lock(&xprt->transport_lock);
1673 
1674 	xprt->stat.sends++;
1675 	xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1676 	xprt->stat.bklog_u += xprt->backlog.qlen;
1677 	xprt->stat.sending_u += xprt->sending.qlen;
1678 	xprt->stat.pending_u += xprt->pending.qlen;
1679 	spin_unlock(&xprt->transport_lock);
1680 
1681 	req->rq_connect_cookie = connect_cookie;
1682 out_dequeue:
1683 	trace_xprt_transmit(req, status);
1684 	xprt_request_dequeue_transmit(task);
1685 	rpc_wake_up_queued_task_set_status(&xprt->sending, task, status);
1686 	return status;
1687 }
1688 
1689 /**
1690  * xprt_transmit - send an RPC request on a transport
1691  * @task: controlling RPC task
1692  *
1693  * Attempts to drain the transmit queue. On exit, either the transport
1694  * signalled an error that needs to be handled before transmission can
1695  * resume, or @task finished transmitting, and detected that it already
1696  * received a reply.
1697  */
1698 void
1699 xprt_transmit(struct rpc_task *task)
1700 {
1701 	struct rpc_rqst *next, *req = task->tk_rqstp;
1702 	struct rpc_xprt	*xprt = req->rq_xprt;
1703 	int status;
1704 
1705 	spin_lock(&xprt->queue_lock);
1706 	for (;;) {
1707 		next = list_first_entry_or_null(&xprt->xmit_queue,
1708 						struct rpc_rqst, rq_xmit);
1709 		if (!next)
1710 			break;
1711 		xprt_pin_rqst(next);
1712 		spin_unlock(&xprt->queue_lock);
1713 		status = xprt_request_transmit(next, task);
1714 		if (status == -EBADMSG && next != req)
1715 			status = 0;
1716 		spin_lock(&xprt->queue_lock);
1717 		xprt_unpin_rqst(next);
1718 		if (status < 0) {
1719 			if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1720 				task->tk_status = status;
1721 			break;
1722 		}
1723 		/* Was @task transmitted, and has it received a reply? */
1724 		if (xprt_request_data_received(task) &&
1725 		    !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
1726 			break;
1727 		cond_resched_lock(&xprt->queue_lock);
1728 	}
1729 	spin_unlock(&xprt->queue_lock);
1730 }
1731 
1732 static void xprt_complete_request_init(struct rpc_task *task)
1733 {
1734 	if (task->tk_rqstp)
1735 		xprt_request_init(task);
1736 }
1737 
1738 void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1739 {
1740 	set_bit(XPRT_CONGESTED, &xprt->state);
1741 	rpc_sleep_on(&xprt->backlog, task, xprt_complete_request_init);
1742 }
1743 EXPORT_SYMBOL_GPL(xprt_add_backlog);
1744 
1745 /**
1746  * xprt_add_backlog_noncongested - queue task on backlog
1747  * @xprt: transport whose backlog queue receives the task
1748  * @task: task to queue
1749  *
1750  * Like xprt_add_backlog, but does not set XPRT_CONGESTED.
1751  * For transports whose free_slot path does not synchronize
1752  * with xprt_throttle_congested via reserve_lock.
1753  */
1754 void xprt_add_backlog_noncongested(struct rpc_xprt *xprt,
1755 				   struct rpc_task *task)
1756 {
1757 	rpc_sleep_on(&xprt->backlog, task, xprt_complete_request_init);
1758 }
1759 EXPORT_SYMBOL_GPL(xprt_add_backlog_noncongested);
1760 
1761 static bool __xprt_set_rq(struct rpc_task *task, void *data)
1762 {
1763 	struct rpc_rqst *req = data;
1764 
1765 	if (task->tk_rqstp == NULL) {
1766 		memset(req, 0, sizeof(*req));	/* mark unused */
1767 		task->tk_rqstp = req;
1768 		return true;
1769 	}
1770 	return false;
1771 }
1772 
1773 bool xprt_wake_up_backlog(struct rpc_xprt *xprt, struct rpc_rqst *req)
1774 {
1775 	if (rpc_wake_up_first(&xprt->backlog, __xprt_set_rq, req) == NULL) {
1776 		clear_bit(XPRT_CONGESTED, &xprt->state);
1777 		return false;
1778 	}
1779 	return true;
1780 }
1781 EXPORT_SYMBOL_GPL(xprt_wake_up_backlog);
1782 
1783 static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1784 {
1785 	bool ret = false;
1786 
1787 	if (!test_bit(XPRT_CONGESTED, &xprt->state))
1788 		goto out;
1789 	spin_lock(&xprt->reserve_lock);
1790 	if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1791 		xprt_add_backlog(xprt, task);
1792 		ret = true;
1793 	}
1794 	spin_unlock(&xprt->reserve_lock);
1795 out:
1796 	return ret;
1797 }
1798 
1799 static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
1800 {
1801 	struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1802 
1803 	if (xprt->num_reqs >= xprt->max_reqs)
1804 		goto out;
1805 	++xprt->num_reqs;
1806 	spin_unlock(&xprt->reserve_lock);
1807 	req = kzalloc_obj(*req, rpc_task_gfp_mask());
1808 	spin_lock(&xprt->reserve_lock);
1809 	if (req != NULL)
1810 		goto out;
1811 	--xprt->num_reqs;
1812 	req = ERR_PTR(-ENOMEM);
1813 out:
1814 	return req;
1815 }
1816 
1817 static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1818 {
1819 	if (xprt->num_reqs > xprt->min_reqs) {
1820 		--xprt->num_reqs;
1821 		kfree(req);
1822 		return true;
1823 	}
1824 	return false;
1825 }
1826 
1827 void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
1828 {
1829 	struct rpc_rqst *req;
1830 
1831 	spin_lock(&xprt->reserve_lock);
1832 	if (!list_empty(&xprt->free)) {
1833 		req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1834 		list_del(&req->rq_list);
1835 		goto out_init_req;
1836 	}
1837 	req = xprt_dynamic_alloc_slot(xprt);
1838 	if (!IS_ERR(req))
1839 		goto out_init_req;
1840 	switch (PTR_ERR(req)) {
1841 	case -ENOMEM:
1842 		dprintk("RPC:       dynamic allocation of request slot "
1843 				"failed! Retrying\n");
1844 		task->tk_status = -ENOMEM;
1845 		break;
1846 	case -EAGAIN:
1847 		xprt_add_backlog(xprt, task);
1848 		dprintk("RPC:       waiting for request slot\n");
1849 		fallthrough;
1850 	default:
1851 		task->tk_status = -EAGAIN;
1852 	}
1853 	spin_unlock(&xprt->reserve_lock);
1854 	return;
1855 out_init_req:
1856 	xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
1857 				     xprt->num_reqs);
1858 	spin_unlock(&xprt->reserve_lock);
1859 
1860 	task->tk_status = 0;
1861 	task->tk_rqstp = req;
1862 }
1863 EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1864 
1865 void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1866 {
1867 	spin_lock(&xprt->reserve_lock);
1868 	if (!xprt_wake_up_backlog(xprt, req) &&
1869 	    !xprt_dynamic_free_slot(xprt, req)) {
1870 		memset(req, 0, sizeof(*req));	/* mark unused */
1871 		list_add(&req->rq_list, &xprt->free);
1872 	}
1873 	spin_unlock(&xprt->reserve_lock);
1874 }
1875 EXPORT_SYMBOL_GPL(xprt_free_slot);
1876 
1877 static void xprt_free_all_slots(struct rpc_xprt *xprt)
1878 {
1879 	struct rpc_rqst *req;
1880 	while (!list_empty(&xprt->free)) {
1881 		req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1882 		list_del(&req->rq_list);
1883 		kfree(req);
1884 	}
1885 }
1886 
1887 static DEFINE_IDA(rpc_xprt_ids);
1888 
1889 void xprt_cleanup_ids(void)
1890 {
1891 	ida_destroy(&rpc_xprt_ids);
1892 }
1893 
1894 static int xprt_alloc_id(struct rpc_xprt *xprt)
1895 {
1896 	int id;
1897 
1898 	id = ida_alloc(&rpc_xprt_ids, GFP_KERNEL);
1899 	if (id < 0)
1900 		return id;
1901 
1902 	xprt->id = id;
1903 	return 0;
1904 }
1905 
1906 static void xprt_free_id(struct rpc_xprt *xprt)
1907 {
1908 	ida_free(&rpc_xprt_ids, xprt->id);
1909 }
1910 
1911 struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1912 		unsigned int num_prealloc,
1913 		unsigned int max_alloc)
1914 {
1915 	struct rpc_xprt *xprt;
1916 	struct rpc_rqst *req;
1917 	int i;
1918 
1919 	xprt = kzalloc(size, GFP_KERNEL);
1920 	if (xprt == NULL)
1921 		goto out;
1922 
1923 	xprt_alloc_id(xprt);
1924 	xprt_init(xprt, net);
1925 
1926 	for (i = 0; i < num_prealloc; i++) {
1927 		req = kzalloc_obj(struct rpc_rqst);
1928 		if (!req)
1929 			goto out_free;
1930 		list_add(&req->rq_list, &xprt->free);
1931 	}
1932 	xprt->max_reqs = max_t(unsigned int, max_alloc, num_prealloc);
1933 	xprt->min_reqs = num_prealloc;
1934 	xprt->num_reqs = num_prealloc;
1935 
1936 	return xprt;
1937 
1938 out_free:
1939 	xprt_free(xprt);
1940 out:
1941 	return NULL;
1942 }
1943 EXPORT_SYMBOL_GPL(xprt_alloc);
1944 
1945 void xprt_free(struct rpc_xprt *xprt)
1946 {
1947 	put_net_track(xprt->xprt_net, &xprt->ns_tracker);
1948 	xprt_free_all_slots(xprt);
1949 	xprt_free_id(xprt);
1950 	rpc_sysfs_xprt_destroy(xprt);
1951 	kfree_rcu(xprt, rcu);
1952 }
1953 EXPORT_SYMBOL_GPL(xprt_free);
1954 
1955 static void
1956 xprt_init_connect_cookie(struct rpc_rqst *req, struct rpc_xprt *xprt)
1957 {
1958 	req->rq_connect_cookie = xprt_connect_cookie(xprt) - 1;
1959 }
1960 
1961 static __be32
1962 xprt_alloc_xid(struct rpc_xprt *xprt)
1963 {
1964 	__be32 xid;
1965 
1966 	spin_lock(&xprt->reserve_lock);
1967 	xid = (__force __be32)xprt->xid++;
1968 	spin_unlock(&xprt->reserve_lock);
1969 	return xid;
1970 }
1971 
1972 static void
1973 xprt_init_xid(struct rpc_xprt *xprt)
1974 {
1975 	xprt->xid = get_random_u32();
1976 }
1977 
1978 static void
1979 xprt_request_init(struct rpc_task *task)
1980 {
1981 	struct rpc_xprt *xprt = task->tk_xprt;
1982 	struct rpc_rqst	*req = task->tk_rqstp;
1983 
1984 	req->rq_task	= task;
1985 	req->rq_xprt    = xprt;
1986 	req->rq_buffer  = NULL;
1987 	req->rq_xid	= xprt_alloc_xid(xprt);
1988 	xprt_init_connect_cookie(req, xprt);
1989 	req->rq_snd_buf.len = 0;
1990 	req->rq_snd_buf.buflen = 0;
1991 	req->rq_rcv_buf.len = 0;
1992 	req->rq_rcv_buf.buflen = 0;
1993 	req->rq_snd_buf.bvec = NULL;
1994 	req->rq_rcv_buf.bvec = NULL;
1995 	req->rq_release_snd_buf = NULL;
1996 	req->rq_seqno_count = 0;
1997 	req->rq_disconnect_on_release = false;
1998 	xprt_init_majortimeo(task, req, task->tk_client->cl_timeout);
1999 
2000 	trace_xprt_reserve(req);
2001 }
2002 
2003 static void
2004 xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
2005 {
2006 	xprt->ops->alloc_slot(xprt, task);
2007 	if (task->tk_rqstp != NULL)
2008 		xprt_request_init(task);
2009 }
2010 
2011 /**
2012  * xprt_reserve - allocate an RPC request slot
2013  * @task: RPC task requesting a slot allocation
2014  *
2015  * If the transport is marked as being congested, or if no more
2016  * slots are available, place the task on the transport's
2017  * backlog queue.
2018  */
2019 void xprt_reserve(struct rpc_task *task)
2020 {
2021 	struct rpc_xprt *xprt = task->tk_xprt;
2022 
2023 	task->tk_status = 0;
2024 	if (task->tk_rqstp != NULL)
2025 		return;
2026 
2027 	task->tk_status = -EAGAIN;
2028 	if (!xprt_throttle_congested(xprt, task))
2029 		xprt_do_reserve(xprt, task);
2030 }
2031 
2032 /**
2033  * xprt_retry_reserve - allocate an RPC request slot
2034  * @task: RPC task requesting a slot allocation
2035  *
2036  * If no more slots are available, place the task on the transport's
2037  * backlog queue.
2038  * Note that the only difference with xprt_reserve is that we now
2039  * ignore the value of the XPRT_CONGESTED flag.
2040  */
2041 void xprt_retry_reserve(struct rpc_task *task)
2042 {
2043 	struct rpc_xprt *xprt = task->tk_xprt;
2044 
2045 	task->tk_status = 0;
2046 	if (task->tk_rqstp != NULL)
2047 		return;
2048 
2049 	task->tk_status = -EAGAIN;
2050 	xprt_do_reserve(xprt, task);
2051 }
2052 
2053 /**
2054  * xprt_release - release an RPC request slot
2055  * @task: task which is finished with the slot
2056  *
2057  */
2058 void xprt_release(struct rpc_task *task)
2059 {
2060 	struct rpc_xprt	*xprt;
2061 	struct rpc_rqst	*req = task->tk_rqstp;
2062 
2063 	if (req == NULL) {
2064 		if (task->tk_client) {
2065 			xprt = task->tk_xprt;
2066 			xprt_release_write(xprt, task);
2067 		}
2068 		return;
2069 	}
2070 
2071 	xprt = req->rq_xprt;
2072 	xprt_request_dequeue_xprt(task);
2073 	spin_lock(&xprt->transport_lock);
2074 	if (unlikely(req->rq_disconnect_on_release)) {
2075 		req->rq_disconnect_on_release = false;
2076 		/*
2077 		 * Dequeue may skip queue_lock. Pair with the fully ordered
2078 		 * test_and_clear_bit(RPC_TASK_NEED_XMIT) in
2079 		 * xprt_request_dequeue_transmit_locked() to order the runstate
2080 		 * check before reading the sender's rq_ntrans and
2081 		 * rq_connect_cookie updates.
2082 		 */
2083 		smp_rmb();
2084 		if (req->rq_ntrans > 0)
2085 			xprt_conditional_disconnect_locked(xprt,
2086 							   req->rq_connect_cookie);
2087 	}
2088 	xprt->ops->release_xprt(xprt, task);
2089 	if (xprt->ops->release_request)
2090 		xprt->ops->release_request(task);
2091 	xprt_schedule_autodisconnect(xprt);
2092 	spin_unlock(&xprt->transport_lock);
2093 	if (req->rq_buffer)
2094 		xprt->ops->buf_free(task);
2095 	if (req->rq_cred != NULL)
2096 		put_rpccred(req->rq_cred);
2097 	if (req->rq_release_snd_buf)
2098 		req->rq_release_snd_buf(req);
2099 
2100 	task->tk_rqstp = NULL;
2101 	if (likely(!bc_prealloc(req)))
2102 		xprt->ops->free_slot(xprt, req);
2103 	else
2104 		xprt_free_bc_request(req);
2105 }
2106 
2107 #ifdef CONFIG_SUNRPC_BACKCHANNEL
2108 void
2109 xprt_init_bc_request(struct rpc_rqst *req, struct rpc_task *task,
2110 		const struct rpc_timeout *to)
2111 {
2112 	struct xdr_buf *xbufp = &req->rq_snd_buf;
2113 
2114 	task->tk_rqstp = req;
2115 	req->rq_task = task;
2116 	xprt_init_connect_cookie(req, req->rq_xprt);
2117 	req->rq_disconnect_on_release = false;
2118 	/*
2119 	 * Set up the xdr_buf length.
2120 	 * This also indicates that the buffer is XDR encoded already.
2121 	 */
2122 	xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
2123 		xbufp->tail[0].iov_len;
2124 	/*
2125 	 * Backchannel Replies are sent with !RPC_TASK_SOFT and
2126 	 * RPC_TASK_NO_RETRANS_TIMEOUT. The major timeout setting
2127 	 * affects only how long each Reply waits to be sent when
2128 	 * a transport connection cannot be established.
2129 	 */
2130 	xprt_init_majortimeo(task, req, to);
2131 }
2132 #endif
2133 
2134 static void xprt_init(struct rpc_xprt *xprt, struct net *net)
2135 {
2136 	kref_init(&xprt->kref);
2137 
2138 	spin_lock_init(&xprt->transport_lock);
2139 	spin_lock_init(&xprt->reserve_lock);
2140 	spin_lock_init(&xprt->queue_lock);
2141 
2142 	INIT_LIST_HEAD(&xprt->free);
2143 	xprt->recv_queue = RB_ROOT;
2144 	INIT_LIST_HEAD(&xprt->xmit_queue);
2145 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2146 	spin_lock_init(&xprt->bc_pa_lock);
2147 	INIT_LIST_HEAD(&xprt->bc_pa_list);
2148 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
2149 	INIT_LIST_HEAD(&xprt->xprt_switch);
2150 
2151 	xprt->last_used = jiffies;
2152 	xprt->cwnd = RPC_INITCWND;
2153 	xprt->bind_index = 0;
2154 
2155 	rpc_init_wait_queue(&xprt->binding, "xprt_binding");
2156 	rpc_init_wait_queue(&xprt->pending, "xprt_pending");
2157 	rpc_init_wait_queue(&xprt->sending, "xprt_sending");
2158 	rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
2159 
2160 	xprt_init_xid(xprt);
2161 
2162 	xprt->xprt_net = get_net_track(net, &xprt->ns_tracker, GFP_KERNEL);
2163 }
2164 
2165 /**
2166  * xprt_create_transport - create an RPC transport
2167  * @args: rpc transport creation arguments
2168  *
2169  */
2170 struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
2171 {
2172 	struct rpc_xprt	*xprt;
2173 	const struct xprt_class *t;
2174 
2175 	t = xprt_class_find_by_ident(args->ident);
2176 	if (!t) {
2177 		dprintk("RPC: transport (%d) not supported\n", args->ident);
2178 		return ERR_PTR(-EIO);
2179 	}
2180 
2181 	xprt = t->setup(args);
2182 	xprt_class_release(t);
2183 
2184 	if (IS_ERR(xprt))
2185 		goto out;
2186 	if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
2187 		xprt->idle_timeout = 0;
2188 	INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
2189 	if (xprt_has_timer(xprt))
2190 		timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
2191 	else
2192 		timer_setup(&xprt->timer, NULL, 0);
2193 
2194 	if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
2195 		xprt_destroy(xprt);
2196 		return ERR_PTR(-EINVAL);
2197 	}
2198 	xprt->servername = kstrdup(args->servername, GFP_KERNEL);
2199 	if (xprt->servername == NULL) {
2200 		xprt_destroy(xprt);
2201 		return ERR_PTR(-ENOMEM);
2202 	}
2203 
2204 	rpc_xprt_debugfs_register(xprt);
2205 
2206 	trace_xprt_create(xprt);
2207 out:
2208 	return xprt;
2209 }
2210 
2211 static void xprt_destroy_cb(struct work_struct *work)
2212 {
2213 	struct rpc_xprt *xprt =
2214 		container_of(work, struct rpc_xprt, task_cleanup);
2215 
2216 	trace_xprt_destroy(xprt);
2217 
2218 	rpc_xprt_debugfs_unregister(xprt);
2219 	rpc_destroy_wait_queue(&xprt->binding);
2220 	rpc_destroy_wait_queue(&xprt->pending);
2221 	rpc_destroy_wait_queue(&xprt->sending);
2222 	rpc_destroy_wait_queue(&xprt->backlog);
2223 	kfree(xprt->servername);
2224 	/*
2225 	 * Destroy any existing back channel
2226 	 */
2227 	xprt_destroy_backchannel(xprt, UINT_MAX);
2228 
2229 	/*
2230 	 * Tear down transport state and free the rpc_xprt
2231 	 */
2232 	xprt->ops->destroy(xprt);
2233 }
2234 
2235 /**
2236  * xprt_destroy - destroy an RPC transport, killing off all requests.
2237  * @xprt: transport to destroy
2238  *
2239  */
2240 static void xprt_destroy(struct rpc_xprt *xprt)
2241 {
2242 	/*
2243 	 * Exclude transport connect/disconnect handlers and autoclose
2244 	 */
2245 	wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
2246 
2247 	/*
2248 	 * xprt_schedule_autodisconnect() can run after XPRT_LOCKED
2249 	 * is cleared.  We use ->transport_lock to ensure the mod_timer()
2250 	 * can only run *before* del_time_sync(), never after.
2251 	 */
2252 	spin_lock(&xprt->transport_lock);
2253 	timer_delete_sync(&xprt->timer);
2254 	spin_unlock(&xprt->transport_lock);
2255 
2256 	/*
2257 	 * Destroy sockets etc from the system workqueue so they can
2258 	 * safely flush receive work running on rpciod.
2259 	 */
2260 	INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
2261 	schedule_work(&xprt->task_cleanup);
2262 }
2263 
2264 static void xprt_destroy_kref(struct kref *kref)
2265 {
2266 	xprt_destroy(container_of(kref, struct rpc_xprt, kref));
2267 }
2268 
2269 /**
2270  * xprt_get - return a reference to an RPC transport.
2271  * @xprt: pointer to the transport
2272  *
2273  */
2274 struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
2275 {
2276 	if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
2277 		return xprt;
2278 	return NULL;
2279 }
2280 EXPORT_SYMBOL_GPL(xprt_get);
2281 
2282 /**
2283  * xprt_put - release a reference to an RPC transport.
2284  * @xprt: pointer to the transport
2285  *
2286  */
2287 void xprt_put(struct rpc_xprt *xprt)
2288 {
2289 	if (xprt != NULL)
2290 		kref_put(&xprt->kref, xprt_destroy_kref);
2291 }
2292 EXPORT_SYMBOL_GPL(xprt_put);
2293 
2294 void xprt_set_offline_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps)
2295 {
2296 	if (!test_and_set_bit(XPRT_OFFLINE, &xprt->state)) {
2297 		spin_lock(&xps->xps_lock);
2298 		xps->xps_nactive--;
2299 		spin_unlock(&xps->xps_lock);
2300 	}
2301 }
2302 
2303 void xprt_set_online_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps)
2304 {
2305 	if (test_and_clear_bit(XPRT_OFFLINE, &xprt->state)) {
2306 		spin_lock(&xps->xps_lock);
2307 		xps->xps_nactive++;
2308 		spin_unlock(&xps->xps_lock);
2309 	}
2310 }
2311 
2312 void xprt_delete_locked(struct rpc_xprt *xprt, struct rpc_xprt_switch *xps)
2313 {
2314 	if (test_and_set_bit(XPRT_REMOVE, &xprt->state))
2315 		return;
2316 
2317 	xprt_force_disconnect(xprt);
2318 	if (!test_bit(XPRT_CONNECTED, &xprt->state))
2319 		return;
2320 
2321 	if (!xprt->sending.qlen && !xprt->pending.qlen &&
2322 	    !xprt->backlog.qlen && !atomic_long_read(&xprt->queuelen))
2323 		rpc_xprt_switch_remove_xprt(xps, xprt, true);
2324 }
2325