GNU Linux-libre 4.14.251-gnu1
[releases.git] / net / sunrpc / xprt.c
1 /*
2  *  linux/net/sunrpc/xprt.c
3  *
4  *  This is a generic RPC call interface supporting congestion avoidance,
5  *  and asynchronous calls.
6  *
7  *  The interface works like this:
8  *
9  *  -   When a process places a call, it allocates a request slot if
10  *      one is available. Otherwise, it sleeps on the backlog queue
11  *      (xprt_reserve).
12  *  -   Next, the caller puts together the RPC message, stuffs it into
13  *      the request struct, and calls xprt_transmit().
14  *  -   xprt_transmit sends the message and installs the caller on the
15  *      transport's wait list. At the same time, if a reply is expected,
16  *      it installs a timer that is run after the packet's timeout has
17  *      expired.
18  *  -   When a packet arrives, the data_ready handler walks the list of
19  *      pending requests for that transport. If a matching XID is found, the
20  *      caller is woken up, and the timer removed.
21  *  -   When no reply arrives within the timeout interval, the timer is
22  *      fired by the kernel and runs xprt_timer(). It either adjusts the
23  *      timeout values (minor timeout) or wakes up the caller with a status
24  *      of -ETIMEDOUT.
25  *  -   When the caller receives a notification from RPC that a reply arrived,
26  *      it should release the RPC slot, and process the reply.
27  *      If the call timed out, it may choose to retry the operation by
28  *      adjusting the initial timeout value, and simply calling rpc_call
29  *      again.
30  *
31  *  Support for async RPC is done through a set of RPC-specific scheduling
32  *  primitives that `transparently' work for processes as well as async
33  *  tasks that rely on callbacks.
34  *
35  *  Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
36  *
37  *  Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
38  */
39
40 #include <linux/module.h>
41
42 #include <linux/types.h>
43 #include <linux/interrupt.h>
44 #include <linux/workqueue.h>
45 #include <linux/net.h>
46 #include <linux/ktime.h>
47
48 #include <linux/sunrpc/clnt.h>
49 #include <linux/sunrpc/metrics.h>
50 #include <linux/sunrpc/bc_xprt.h>
51 #include <linux/rcupdate.h>
52
53 #include <trace/events/sunrpc.h>
54
55 #include "sunrpc.h"
56
57 /*
58  * Local variables
59  */
60
61 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
62 # define RPCDBG_FACILITY        RPCDBG_XPRT
63 #endif
64
65 /*
66  * Local functions
67  */
68 static void      xprt_init(struct rpc_xprt *xprt, struct net *net);
69 static void     xprt_request_init(struct rpc_task *, struct rpc_xprt *);
70 static void     xprt_connect_status(struct rpc_task *task);
71 static int      __xprt_get_cong(struct rpc_xprt *, struct rpc_task *);
72 static void     __xprt_put_cong(struct rpc_xprt *, struct rpc_rqst *);
73 static void      xprt_destroy(struct rpc_xprt *xprt);
74
75 static DEFINE_SPINLOCK(xprt_list_lock);
76 static LIST_HEAD(xprt_list);
77
78 /**
79  * xprt_register_transport - register a transport implementation
80  * @transport: transport to register
81  *
82  * If a transport implementation is loaded as a kernel module, it can
83  * call this interface to make itself known to the RPC client.
84  *
85  * Returns:
86  * 0:           transport successfully registered
87  * -EEXIST:     transport already registered
88  * -EINVAL:     transport module being unloaded
89  */
90 int xprt_register_transport(struct xprt_class *transport)
91 {
92         struct xprt_class *t;
93         int result;
94
95         result = -EEXIST;
96         spin_lock(&xprt_list_lock);
97         list_for_each_entry(t, &xprt_list, list) {
98                 /* don't register the same transport class twice */
99                 if (t->ident == transport->ident)
100                         goto out;
101         }
102
103         list_add_tail(&transport->list, &xprt_list);
104         printk(KERN_INFO "RPC: Registered %s transport module.\n",
105                transport->name);
106         result = 0;
107
108 out:
109         spin_unlock(&xprt_list_lock);
110         return result;
111 }
112 EXPORT_SYMBOL_GPL(xprt_register_transport);
113
114 /**
115  * xprt_unregister_transport - unregister a transport implementation
116  * @transport: transport to unregister
117  *
118  * Returns:
119  * 0:           transport successfully unregistered
120  * -ENOENT:     transport never registered
121  */
122 int xprt_unregister_transport(struct xprt_class *transport)
123 {
124         struct xprt_class *t;
125         int result;
126
127         result = 0;
128         spin_lock(&xprt_list_lock);
129         list_for_each_entry(t, &xprt_list, list) {
130                 if (t == transport) {
131                         printk(KERN_INFO
132                                 "RPC: Unregistered %s transport module.\n",
133                                 transport->name);
134                         list_del_init(&transport->list);
135                         goto out;
136                 }
137         }
138         result = -ENOENT;
139
140 out:
141         spin_unlock(&xprt_list_lock);
142         return result;
143 }
144 EXPORT_SYMBOL_GPL(xprt_unregister_transport);
145
146 static void
147 xprt_class_release(const struct xprt_class *t)
148 {
149         module_put(t->owner);
150 }
151
152 static const struct xprt_class *
153 xprt_class_find_by_netid_locked(const char *netid)
154 {
155         const struct xprt_class *t;
156         unsigned int i;
157
158         list_for_each_entry(t, &xprt_list, list) {
159                 for (i = 0; t->netid[i][0] != '\0'; i++) {
160                         if (strcmp(t->netid[i], netid) != 0)
161                                 continue;
162                         if (!try_module_get(t->owner))
163                                 continue;
164                         return t;
165                 }
166         }
167         return NULL;
168 }
169
170 static const struct xprt_class *
171 xprt_class_find_by_netid(const char *netid)
172 {
173         const struct xprt_class *t;
174
175         spin_lock(&xprt_list_lock);
176         t = xprt_class_find_by_netid_locked(netid);
177         if (!t) {
178                 spin_unlock(&xprt_list_lock);
179                 request_module("rpc%s", netid);
180                 spin_lock(&xprt_list_lock);
181                 t = xprt_class_find_by_netid_locked(netid);
182         }
183         spin_unlock(&xprt_list_lock);
184         return t;
185 }
186
187 /**
188  * xprt_load_transport - load a transport implementation
189  * @netid: transport to load
190  *
191  * Returns:
192  * 0:           transport successfully loaded
193  * -ENOENT:     transport module not available
194  */
195 int xprt_load_transport(const char *netid)
196 {
197         const struct xprt_class *t;
198
199         t = xprt_class_find_by_netid(netid);
200         if (!t)
201                 return -ENOENT;
202         xprt_class_release(t);
203         return 0;
204 }
205 EXPORT_SYMBOL_GPL(xprt_load_transport);
206
207 /**
208  * xprt_reserve_xprt - serialize write access to transports
209  * @task: task that is requesting access to the transport
210  * @xprt: pointer to the target transport
211  *
212  * This prevents mixing the payload of separate requests, and prevents
213  * transport connects from colliding with writes.  No congestion control
214  * is provided.
215  */
216 int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
217 {
218         struct rpc_rqst *req = task->tk_rqstp;
219         int priority;
220
221         if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
222                 if (task == xprt->snd_task)
223                         return 1;
224                 goto out_sleep;
225         }
226         xprt->snd_task = task;
227         if (req != NULL)
228                 req->rq_ntrans++;
229
230         return 1;
231
232 out_sleep:
233         dprintk("RPC: %5u failed to lock transport %p\n",
234                         task->tk_pid, xprt);
235         task->tk_timeout = 0;
236         task->tk_status = -EAGAIN;
237         if (req == NULL)
238                 priority = RPC_PRIORITY_LOW;
239         else if (!req->rq_ntrans)
240                 priority = RPC_PRIORITY_NORMAL;
241         else
242                 priority = RPC_PRIORITY_HIGH;
243         rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
244         return 0;
245 }
246 EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
247
248 static void xprt_clear_locked(struct rpc_xprt *xprt)
249 {
250         xprt->snd_task = NULL;
251         if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state)) {
252                 smp_mb__before_atomic();
253                 clear_bit(XPRT_LOCKED, &xprt->state);
254                 smp_mb__after_atomic();
255         } else
256                 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
257 }
258
259 /*
260  * xprt_reserve_xprt_cong - serialize write access to transports
261  * @task: task that is requesting access to the transport
262  *
263  * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
264  * integrated into the decision of whether a request is allowed to be
265  * woken up and given access to the transport.
266  */
267 int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
268 {
269         struct rpc_rqst *req = task->tk_rqstp;
270         int priority;
271
272         if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
273                 if (task == xprt->snd_task)
274                         return 1;
275                 goto out_sleep;
276         }
277         if (req == NULL) {
278                 xprt->snd_task = task;
279                 return 1;
280         }
281         if (__xprt_get_cong(xprt, task)) {
282                 xprt->snd_task = task;
283                 req->rq_ntrans++;
284                 return 1;
285         }
286         xprt_clear_locked(xprt);
287 out_sleep:
288         if (req)
289                 __xprt_put_cong(xprt, req);
290         dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
291         task->tk_timeout = 0;
292         task->tk_status = -EAGAIN;
293         if (req == NULL)
294                 priority = RPC_PRIORITY_LOW;
295         else if (!req->rq_ntrans)
296                 priority = RPC_PRIORITY_NORMAL;
297         else
298                 priority = RPC_PRIORITY_HIGH;
299         rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
300         return 0;
301 }
302 EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
303
304 static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
305 {
306         int retval;
307
308         spin_lock_bh(&xprt->transport_lock);
309         retval = xprt->ops->reserve_xprt(xprt, task);
310         spin_unlock_bh(&xprt->transport_lock);
311         return retval;
312 }
313
314 static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
315 {
316         struct rpc_xprt *xprt = data;
317         struct rpc_rqst *req;
318
319         req = task->tk_rqstp;
320         xprt->snd_task = task;
321         if (req)
322                 req->rq_ntrans++;
323         return true;
324 }
325
326 static void __xprt_lock_write_next(struct rpc_xprt *xprt)
327 {
328         if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
329                 return;
330
331         if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
332                                 __xprt_lock_write_func, xprt))
333                 return;
334         xprt_clear_locked(xprt);
335 }
336
337 static bool __xprt_lock_write_cong_func(struct rpc_task *task, void *data)
338 {
339         struct rpc_xprt *xprt = data;
340         struct rpc_rqst *req;
341
342         req = task->tk_rqstp;
343         if (req == NULL) {
344                 xprt->snd_task = task;
345                 return true;
346         }
347         if (__xprt_get_cong(xprt, task)) {
348                 xprt->snd_task = task;
349                 req->rq_ntrans++;
350                 return true;
351         }
352         return false;
353 }
354
355 static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
356 {
357         if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
358                 return;
359         if (RPCXPRT_CONGESTED(xprt))
360                 goto out_unlock;
361         if (rpc_wake_up_first_on_wq(xprtiod_workqueue, &xprt->sending,
362                                 __xprt_lock_write_cong_func, xprt))
363                 return;
364 out_unlock:
365         xprt_clear_locked(xprt);
366 }
367
368 static void xprt_task_clear_bytes_sent(struct rpc_task *task)
369 {
370         if (task != NULL) {
371                 struct rpc_rqst *req = task->tk_rqstp;
372                 if (req != NULL)
373                         req->rq_bytes_sent = 0;
374         }
375 }
376
377 /**
378  * xprt_release_xprt - allow other requests to use a transport
379  * @xprt: transport with other tasks potentially waiting
380  * @task: task that is releasing access to the transport
381  *
382  * Note that "task" can be NULL.  No congestion control is provided.
383  */
384 void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
385 {
386         if (xprt->snd_task == task) {
387                 xprt_task_clear_bytes_sent(task);
388                 xprt_clear_locked(xprt);
389                 __xprt_lock_write_next(xprt);
390         }
391 }
392 EXPORT_SYMBOL_GPL(xprt_release_xprt);
393
394 /**
395  * xprt_release_xprt_cong - allow other requests to use a transport
396  * @xprt: transport with other tasks potentially waiting
397  * @task: task that is releasing access to the transport
398  *
399  * Note that "task" can be NULL.  Another task is awoken to use the
400  * transport if the transport's congestion window allows it.
401  */
402 void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
403 {
404         if (xprt->snd_task == task) {
405                 xprt_task_clear_bytes_sent(task);
406                 xprt_clear_locked(xprt);
407                 __xprt_lock_write_next_cong(xprt);
408         }
409 }
410 EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
411
412 static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
413 {
414         spin_lock_bh(&xprt->transport_lock);
415         xprt->ops->release_xprt(xprt, task);
416         spin_unlock_bh(&xprt->transport_lock);
417 }
418
419 /*
420  * Van Jacobson congestion avoidance. Check if the congestion window
421  * overflowed. Put the task to sleep if this is the case.
422  */
423 static int
424 __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
425 {
426         struct rpc_rqst *req = task->tk_rqstp;
427
428         if (req->rq_cong)
429                 return 1;
430         dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
431                         task->tk_pid, xprt->cong, xprt->cwnd);
432         if (RPCXPRT_CONGESTED(xprt))
433                 return 0;
434         req->rq_cong = 1;
435         xprt->cong += RPC_CWNDSCALE;
436         return 1;
437 }
438
439 /*
440  * Adjust the congestion window, and wake up the next task
441  * that has been sleeping due to congestion
442  */
443 static void
444 __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
445 {
446         if (!req->rq_cong)
447                 return;
448         req->rq_cong = 0;
449         xprt->cong -= RPC_CWNDSCALE;
450         __xprt_lock_write_next_cong(xprt);
451 }
452
453 /**
454  * xprt_release_rqst_cong - housekeeping when request is complete
455  * @task: RPC request that recently completed
456  *
457  * Useful for transports that require congestion control.
458  */
459 void xprt_release_rqst_cong(struct rpc_task *task)
460 {
461         struct rpc_rqst *req = task->tk_rqstp;
462
463         __xprt_put_cong(req->rq_xprt, req);
464 }
465 EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
466
467 /**
468  * xprt_adjust_cwnd - adjust transport congestion window
469  * @xprt: pointer to xprt
470  * @task: recently completed RPC request used to adjust window
471  * @result: result code of completed RPC request
472  *
473  * The transport code maintains an estimate on the maximum number of out-
474  * standing RPC requests, using a smoothed version of the congestion
475  * avoidance implemented in 44BSD. This is basically the Van Jacobson
476  * congestion algorithm: If a retransmit occurs, the congestion window is
477  * halved; otherwise, it is incremented by 1/cwnd when
478  *
479  *      -       a reply is received and
480  *      -       a full number of requests are outstanding and
481  *      -       the congestion window hasn't been updated recently.
482  */
483 void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
484 {
485         struct rpc_rqst *req = task->tk_rqstp;
486         unsigned long cwnd = xprt->cwnd;
487
488         if (result >= 0 && cwnd <= xprt->cong) {
489                 /* The (cwnd >> 1) term makes sure
490                  * the result gets rounded properly. */
491                 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
492                 if (cwnd > RPC_MAXCWND(xprt))
493                         cwnd = RPC_MAXCWND(xprt);
494                 __xprt_lock_write_next_cong(xprt);
495         } else if (result == -ETIMEDOUT) {
496                 cwnd >>= 1;
497                 if (cwnd < RPC_CWNDSCALE)
498                         cwnd = RPC_CWNDSCALE;
499         }
500         dprintk("RPC:       cong %ld, cwnd was %ld, now %ld\n",
501                         xprt->cong, xprt->cwnd, cwnd);
502         xprt->cwnd = cwnd;
503         __xprt_put_cong(xprt, req);
504 }
505 EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
506
507 /**
508  * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
509  * @xprt: transport with waiting tasks
510  * @status: result code to plant in each task before waking it
511  *
512  */
513 void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
514 {
515         if (status < 0)
516                 rpc_wake_up_status(&xprt->pending, status);
517         else
518                 rpc_wake_up(&xprt->pending);
519 }
520 EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
521
522 /**
523  * xprt_wait_for_buffer_space - wait for transport output buffer to clear
524  * @task: task to be put to sleep
525  * @action: function pointer to be executed after wait
526  *
527  * Note that we only set the timer for the case of RPC_IS_SOFT(), since
528  * we don't in general want to force a socket disconnection due to
529  * an incomplete RPC call transmission.
530  */
531 void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
532 {
533         struct rpc_rqst *req = task->tk_rqstp;
534         struct rpc_xprt *xprt = req->rq_xprt;
535
536         task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
537         rpc_sleep_on(&xprt->pending, task, action);
538 }
539 EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
540
541 /**
542  * xprt_write_space - wake the task waiting for transport output buffer space
543  * @xprt: transport with waiting tasks
544  *
545  * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
546  */
547 void xprt_write_space(struct rpc_xprt *xprt)
548 {
549         spin_lock_bh(&xprt->transport_lock);
550         if (xprt->snd_task) {
551                 dprintk("RPC:       write space: waking waiting task on "
552                                 "xprt %p\n", xprt);
553                 rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task);
554         }
555         spin_unlock_bh(&xprt->transport_lock);
556 }
557 EXPORT_SYMBOL_GPL(xprt_write_space);
558
559 /**
560  * xprt_set_retrans_timeout_def - set a request's retransmit timeout
561  * @task: task whose timeout is to be set
562  *
563  * Set a request's retransmit timeout based on the transport's
564  * default timeout parameters.  Used by transports that don't adjust
565  * the retransmit timeout based on round-trip time estimation.
566  */
567 void xprt_set_retrans_timeout_def(struct rpc_task *task)
568 {
569         task->tk_timeout = task->tk_rqstp->rq_timeout;
570 }
571 EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
572
573 /**
574  * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
575  * @task: task whose timeout is to be set
576  *
577  * Set a request's retransmit timeout using the RTT estimator.
578  */
579 void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
580 {
581         int timer = task->tk_msg.rpc_proc->p_timer;
582         struct rpc_clnt *clnt = task->tk_client;
583         struct rpc_rtt *rtt = clnt->cl_rtt;
584         struct rpc_rqst *req = task->tk_rqstp;
585         unsigned long max_timeout = clnt->cl_timeout->to_maxval;
586
587         task->tk_timeout = rpc_calc_rto(rtt, timer);
588         task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
589         if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
590                 task->tk_timeout = max_timeout;
591 }
592 EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
593
594 static void xprt_reset_majortimeo(struct rpc_rqst *req)
595 {
596         const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
597
598         req->rq_majortimeo = req->rq_timeout;
599         if (to->to_exponential)
600                 req->rq_majortimeo <<= to->to_retries;
601         else
602                 req->rq_majortimeo += to->to_increment * to->to_retries;
603         if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
604                 req->rq_majortimeo = to->to_maxval;
605         req->rq_majortimeo += jiffies;
606 }
607
608 /**
609  * xprt_adjust_timeout - adjust timeout values for next retransmit
610  * @req: RPC request containing parameters to use for the adjustment
611  *
612  */
613 int xprt_adjust_timeout(struct rpc_rqst *req)
614 {
615         struct rpc_xprt *xprt = req->rq_xprt;
616         const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
617         int status = 0;
618
619         if (time_before(jiffies, req->rq_majortimeo)) {
620                 if (to->to_exponential)
621                         req->rq_timeout <<= 1;
622                 else
623                         req->rq_timeout += to->to_increment;
624                 if (to->to_maxval && req->rq_timeout >= to->to_maxval)
625                         req->rq_timeout = to->to_maxval;
626                 req->rq_retries++;
627         } else {
628                 req->rq_timeout = to->to_initval;
629                 req->rq_retries = 0;
630                 xprt_reset_majortimeo(req);
631                 /* Reset the RTT counters == "slow start" */
632                 spin_lock_bh(&xprt->transport_lock);
633                 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
634                 spin_unlock_bh(&xprt->transport_lock);
635                 status = -ETIMEDOUT;
636         }
637
638         if (req->rq_timeout == 0) {
639                 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
640                 req->rq_timeout = 5 * HZ;
641         }
642         return status;
643 }
644
645 static void xprt_autoclose(struct work_struct *work)
646 {
647         struct rpc_xprt *xprt =
648                 container_of(work, struct rpc_xprt, task_cleanup);
649
650         clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
651         xprt->ops->close(xprt);
652         xprt_release_write(xprt, NULL);
653         wake_up_bit(&xprt->state, XPRT_LOCKED);
654 }
655
656 /**
657  * xprt_disconnect_done - mark a transport as disconnected
658  * @xprt: transport to flag for disconnect
659  *
660  */
661 void xprt_disconnect_done(struct rpc_xprt *xprt)
662 {
663         dprintk("RPC:       disconnected transport %p\n", xprt);
664         spin_lock_bh(&xprt->transport_lock);
665         xprt_clear_connected(xprt);
666         xprt_wake_pending_tasks(xprt, -EAGAIN);
667         spin_unlock_bh(&xprt->transport_lock);
668 }
669 EXPORT_SYMBOL_GPL(xprt_disconnect_done);
670
671 /**
672  * xprt_force_disconnect - force a transport to disconnect
673  * @xprt: transport to disconnect
674  *
675  */
676 void xprt_force_disconnect(struct rpc_xprt *xprt)
677 {
678         /* Don't race with the test_bit() in xprt_clear_locked() */
679         spin_lock_bh(&xprt->transport_lock);
680         set_bit(XPRT_CLOSE_WAIT, &xprt->state);
681         /* Try to schedule an autoclose RPC call */
682         if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
683                 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
684         xprt_wake_pending_tasks(xprt, -EAGAIN);
685         spin_unlock_bh(&xprt->transport_lock);
686 }
687 EXPORT_SYMBOL_GPL(xprt_force_disconnect);
688
689 /**
690  * xprt_conditional_disconnect - force a transport to disconnect
691  * @xprt: transport to disconnect
692  * @cookie: 'connection cookie'
693  *
694  * This attempts to break the connection if and only if 'cookie' matches
695  * the current transport 'connection cookie'. It ensures that we don't
696  * try to break the connection more than once when we need to retransmit
697  * a batch of RPC requests.
698  *
699  */
700 void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
701 {
702         /* Don't race with the test_bit() in xprt_clear_locked() */
703         spin_lock_bh(&xprt->transport_lock);
704         if (cookie != xprt->connect_cookie)
705                 goto out;
706         if (test_bit(XPRT_CLOSING, &xprt->state))
707                 goto out;
708         set_bit(XPRT_CLOSE_WAIT, &xprt->state);
709         /* Try to schedule an autoclose RPC call */
710         if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
711                 queue_work(xprtiod_workqueue, &xprt->task_cleanup);
712         xprt_wake_pending_tasks(xprt, -EAGAIN);
713 out:
714         spin_unlock_bh(&xprt->transport_lock);
715 }
716
717 static bool
718 xprt_has_timer(const struct rpc_xprt *xprt)
719 {
720         return xprt->idle_timeout != 0;
721 }
722
723 static void
724 xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
725         __must_hold(&xprt->transport_lock)
726 {
727         if (list_empty(&xprt->recv) && xprt_has_timer(xprt))
728                 mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
729 }
730
731 static void
732 xprt_init_autodisconnect(unsigned long data)
733 {
734         struct rpc_xprt *xprt = (struct rpc_xprt *)data;
735
736         spin_lock(&xprt->transport_lock);
737         if (!list_empty(&xprt->recv))
738                 goto out_abort;
739         /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
740         xprt->last_used = jiffies;
741         if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
742                 goto out_abort;
743         spin_unlock(&xprt->transport_lock);
744         queue_work(xprtiod_workqueue, &xprt->task_cleanup);
745         return;
746 out_abort:
747         spin_unlock(&xprt->transport_lock);
748 }
749
750 bool xprt_lock_connect(struct rpc_xprt *xprt,
751                 struct rpc_task *task,
752                 void *cookie)
753 {
754         bool ret = false;
755
756         spin_lock_bh(&xprt->transport_lock);
757         if (!test_bit(XPRT_LOCKED, &xprt->state))
758                 goto out;
759         if (xprt->snd_task != task)
760                 goto out;
761         xprt_task_clear_bytes_sent(task);
762         xprt->snd_task = cookie;
763         ret = true;
764 out:
765         spin_unlock_bh(&xprt->transport_lock);
766         return ret;
767 }
768
769 void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
770 {
771         spin_lock_bh(&xprt->transport_lock);
772         if (xprt->snd_task != cookie)
773                 goto out;
774         if (!test_bit(XPRT_LOCKED, &xprt->state))
775                 goto out;
776         xprt->snd_task =NULL;
777         xprt->ops->release_xprt(xprt, NULL);
778         xprt_schedule_autodisconnect(xprt);
779 out:
780         spin_unlock_bh(&xprt->transport_lock);
781         wake_up_bit(&xprt->state, XPRT_LOCKED);
782 }
783
784 /**
785  * xprt_connect - schedule a transport connect operation
786  * @task: RPC task that is requesting the connect
787  *
788  */
789 void xprt_connect(struct rpc_task *task)
790 {
791         struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
792
793         dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
794                         xprt, (xprt_connected(xprt) ? "is" : "is not"));
795
796         if (!xprt_bound(xprt)) {
797                 task->tk_status = -EAGAIN;
798                 return;
799         }
800         if (!xprt_lock_write(xprt, task))
801                 return;
802
803         if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
804                 xprt->ops->close(xprt);
805
806         if (!xprt_connected(xprt)) {
807                 task->tk_rqstp->rq_bytes_sent = 0;
808                 task->tk_timeout = task->tk_rqstp->rq_timeout;
809                 task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
810                 rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
811
812                 if (test_bit(XPRT_CLOSING, &xprt->state))
813                         return;
814                 if (xprt_test_and_set_connecting(xprt))
815                         return;
816                 /* Race breaker */
817                 if (!xprt_connected(xprt)) {
818                         xprt->stat.connect_start = jiffies;
819                         xprt->ops->connect(xprt, task);
820                 } else {
821                         xprt_clear_connecting(xprt);
822                         task->tk_status = 0;
823                         rpc_wake_up_queued_task(&xprt->pending, task);
824                 }
825         }
826         xprt_release_write(xprt, task);
827 }
828
829 static void xprt_connect_status(struct rpc_task *task)
830 {
831         switch (task->tk_status) {
832         case 0:
833                 dprintk("RPC: %5u xprt_connect_status: connection established\n",
834                                 task->tk_pid);
835                 break;
836         case -ECONNREFUSED:
837         case -ECONNRESET:
838         case -ECONNABORTED:
839         case -ENETUNREACH:
840         case -EHOSTUNREACH:
841         case -EPIPE:
842         case -EAGAIN:
843                 dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
844                 break;
845         case -ETIMEDOUT:
846                 dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
847                                 "out\n", task->tk_pid);
848                 break;
849         default:
850                 dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
851                                 "server %s\n", task->tk_pid, -task->tk_status,
852                                 task->tk_rqstp->rq_xprt->servername);
853                 task->tk_status = -EIO;
854         }
855 }
856
857 /**
858  * xprt_lookup_rqst - find an RPC request corresponding to an XID
859  * @xprt: transport on which the original request was transmitted
860  * @xid: RPC XID of incoming reply
861  *
862  */
863 struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
864 {
865         struct rpc_rqst *entry;
866
867         list_for_each_entry(entry, &xprt->recv, rq_list)
868                 if (entry->rq_xid == xid) {
869                         trace_xprt_lookup_rqst(xprt, xid, 0);
870                         return entry;
871                 }
872
873         dprintk("RPC:       xprt_lookup_rqst did not find xid %08x\n",
874                         ntohl(xid));
875         trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
876         xprt->stat.bad_xids++;
877         return NULL;
878 }
879 EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
880
881 /**
882  * xprt_pin_rqst - Pin a request on the transport receive list
883  * @req: Request to pin
884  *
885  * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
886  * so should be holding the xprt transport lock.
887  */
888 void xprt_pin_rqst(struct rpc_rqst *req)
889 {
890         set_bit(RPC_TASK_MSG_RECV, &req->rq_task->tk_runstate);
891 }
892 EXPORT_SYMBOL_GPL(xprt_pin_rqst);
893
894 /**
895  * xprt_unpin_rqst - Unpin a request on the transport receive list
896  * @req: Request to pin
897  *
898  * Caller should be holding the xprt transport lock.
899  */
900 void xprt_unpin_rqst(struct rpc_rqst *req)
901 {
902         struct rpc_task *task = req->rq_task;
903
904         clear_bit(RPC_TASK_MSG_RECV, &task->tk_runstate);
905         if (test_bit(RPC_TASK_MSG_RECV_WAIT, &task->tk_runstate))
906                 wake_up_bit(&task->tk_runstate, RPC_TASK_MSG_RECV);
907 }
908 EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
909
910 static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
911 __must_hold(&req->rq_xprt->recv_lock)
912 {
913         struct rpc_task *task = req->rq_task;
914         
915         if (task && test_bit(RPC_TASK_MSG_RECV, &task->tk_runstate)) {
916                 spin_unlock(&req->rq_xprt->recv_lock);
917                 set_bit(RPC_TASK_MSG_RECV_WAIT, &task->tk_runstate);
918                 wait_on_bit(&task->tk_runstate, RPC_TASK_MSG_RECV,
919                                 TASK_UNINTERRUPTIBLE);
920                 clear_bit(RPC_TASK_MSG_RECV_WAIT, &task->tk_runstate);
921                 spin_lock(&req->rq_xprt->recv_lock);
922         }
923 }
924
925 static void xprt_update_rtt(struct rpc_task *task)
926 {
927         struct rpc_rqst *req = task->tk_rqstp;
928         struct rpc_rtt *rtt = task->tk_client->cl_rtt;
929         unsigned int timer = task->tk_msg.rpc_proc->p_timer;
930         long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
931
932         if (timer) {
933                 if (req->rq_ntrans == 1)
934                         rpc_update_rtt(rtt, timer, m);
935                 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
936         }
937 }
938
939 /**
940  * xprt_complete_rqst - called when reply processing is complete
941  * @task: RPC request that recently completed
942  * @copied: actual number of bytes received from the transport
943  *
944  * Caller holds transport lock.
945  */
946 void xprt_complete_rqst(struct rpc_task *task, int copied)
947 {
948         struct rpc_rqst *req = task->tk_rqstp;
949         struct rpc_xprt *xprt = req->rq_xprt;
950
951         dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
952                         task->tk_pid, ntohl(req->rq_xid), copied);
953         trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
954
955         xprt->stat.recvs++;
956         req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime);
957         if (xprt->ops->timer != NULL)
958                 xprt_update_rtt(task);
959
960         list_del_init(&req->rq_list);
961         req->rq_private_buf.len = copied;
962         /* Ensure all writes are done before we update */
963         /* req->rq_reply_bytes_recvd */
964         smp_wmb();
965         req->rq_reply_bytes_recvd = copied;
966         rpc_wake_up_queued_task(&xprt->pending, task);
967 }
968 EXPORT_SYMBOL_GPL(xprt_complete_rqst);
969
970 static void xprt_timer(struct rpc_task *task)
971 {
972         struct rpc_rqst *req = task->tk_rqstp;
973         struct rpc_xprt *xprt = req->rq_xprt;
974
975         if (task->tk_status != -ETIMEDOUT)
976                 return;
977         dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
978
979         if (!req->rq_reply_bytes_recvd) {
980                 if (xprt->ops->timer)
981                         xprt->ops->timer(xprt, task);
982         } else
983                 task->tk_status = 0;
984 }
985
986 /**
987  * xprt_prepare_transmit - reserve the transport before sending a request
988  * @task: RPC task about to send a request
989  *
990  */
991 bool xprt_prepare_transmit(struct rpc_task *task)
992 {
993         struct rpc_rqst *req = task->tk_rqstp;
994         struct rpc_xprt *xprt = req->rq_xprt;
995         bool ret = false;
996
997         dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
998
999         spin_lock_bh(&xprt->transport_lock);
1000         if (!req->rq_bytes_sent) {
1001                 if (req->rq_reply_bytes_recvd) {
1002                         task->tk_status = req->rq_reply_bytes_recvd;
1003                         goto out_unlock;
1004                 }
1005                 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
1006                     && xprt_connected(xprt)
1007                     && req->rq_connect_cookie == xprt->connect_cookie) {
1008                         xprt->ops->set_retrans_timeout(task);
1009                         rpc_sleep_on(&xprt->pending, task, xprt_timer);
1010                         goto out_unlock;
1011                 }
1012         }
1013         if (!xprt->ops->reserve_xprt(xprt, task)) {
1014                 task->tk_status = -EAGAIN;
1015                 goto out_unlock;
1016         }
1017         ret = true;
1018 out_unlock:
1019         spin_unlock_bh(&xprt->transport_lock);
1020         return ret;
1021 }
1022
1023 void xprt_end_transmit(struct rpc_task *task)
1024 {
1025         xprt_release_write(task->tk_rqstp->rq_xprt, task);
1026 }
1027
1028 /**
1029  * xprt_transmit - send an RPC request on a transport
1030  * @task: controlling RPC task
1031  *
1032  * We have to copy the iovec because sendmsg fiddles with its contents.
1033  */
1034 void xprt_transmit(struct rpc_task *task)
1035 {
1036         struct rpc_rqst *req = task->tk_rqstp;
1037         struct rpc_xprt *xprt = req->rq_xprt;
1038         unsigned int connect_cookie;
1039         int status, numreqs;
1040
1041         dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
1042
1043         if (!req->rq_reply_bytes_recvd) {
1044                 if (list_empty(&req->rq_list) && rpc_reply_expected(task)) {
1045                         /*
1046                          * Add to the list only if we're expecting a reply
1047                          */
1048                         /* Update the softirq receive buffer */
1049                         memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
1050                                         sizeof(req->rq_private_buf));
1051                         /* Add request to the receive list */
1052                         spin_lock(&xprt->recv_lock);
1053                         list_add_tail(&req->rq_list, &xprt->recv);
1054                         spin_unlock(&xprt->recv_lock);
1055                         xprt_reset_majortimeo(req);
1056                         /* Turn off autodisconnect */
1057                         del_singleshot_timer_sync(&xprt->timer);
1058                 }
1059         } else if (!req->rq_bytes_sent)
1060                 return;
1061
1062         connect_cookie = xprt->connect_cookie;
1063         req->rq_xtime = ktime_get();
1064         status = xprt->ops->send_request(task);
1065         trace_xprt_transmit(xprt, req->rq_xid, status);
1066         if (status != 0) {
1067                 task->tk_status = status;
1068                 return;
1069         }
1070         xprt_inject_disconnect(xprt);
1071
1072         dprintk("RPC: %5u xmit complete\n", task->tk_pid);
1073         task->tk_flags |= RPC_TASK_SENT;
1074         spin_lock_bh(&xprt->transport_lock);
1075
1076         xprt->ops->set_retrans_timeout(task);
1077
1078         numreqs = atomic_read(&xprt->num_reqs);
1079         if (numreqs > xprt->stat.max_slots)
1080                 xprt->stat.max_slots = numreqs;
1081         xprt->stat.sends++;
1082         xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
1083         xprt->stat.bklog_u += xprt->backlog.qlen;
1084         xprt->stat.sending_u += xprt->sending.qlen;
1085         xprt->stat.pending_u += xprt->pending.qlen;
1086         spin_unlock_bh(&xprt->transport_lock);
1087
1088         req->rq_connect_cookie = connect_cookie;
1089         if (rpc_reply_expected(task) && !READ_ONCE(req->rq_reply_bytes_recvd)) {
1090                 /*
1091                  * Sleep on the pending queue if we're expecting a reply.
1092                  * The spinlock ensures atomicity between the test of
1093                  * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
1094                  */
1095                 spin_lock(&xprt->recv_lock);
1096                 if (!req->rq_reply_bytes_recvd) {
1097                         rpc_sleep_on(&xprt->pending, task, xprt_timer);
1098                         /*
1099                          * Send an extra queue wakeup call if the
1100                          * connection was dropped in case the call to
1101                          * rpc_sleep_on() raced.
1102                          */
1103                         if (!xprt_connected(xprt))
1104                                 xprt_wake_pending_tasks(xprt, -ENOTCONN);
1105                 }
1106                 spin_unlock(&xprt->recv_lock);
1107         }
1108 }
1109
1110 static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
1111 {
1112         set_bit(XPRT_CONGESTED, &xprt->state);
1113         rpc_sleep_on(&xprt->backlog, task, NULL);
1114 }
1115
1116 static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
1117 {
1118         if (rpc_wake_up_next(&xprt->backlog) == NULL)
1119                 clear_bit(XPRT_CONGESTED, &xprt->state);
1120 }
1121
1122 static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
1123 {
1124         bool ret = false;
1125
1126         if (!test_bit(XPRT_CONGESTED, &xprt->state))
1127                 goto out;
1128         spin_lock(&xprt->reserve_lock);
1129         if (test_bit(XPRT_CONGESTED, &xprt->state)) {
1130                 rpc_sleep_on(&xprt->backlog, task, NULL);
1131                 ret = true;
1132         }
1133         spin_unlock(&xprt->reserve_lock);
1134 out:
1135         return ret;
1136 }
1137
1138 static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
1139 {
1140         struct rpc_rqst *req = ERR_PTR(-EAGAIN);
1141
1142         if (!atomic_add_unless(&xprt->num_reqs, 1, xprt->max_reqs))
1143                 goto out;
1144         spin_unlock(&xprt->reserve_lock);
1145         req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
1146         spin_lock(&xprt->reserve_lock);
1147         if (req != NULL)
1148                 goto out;
1149         atomic_dec(&xprt->num_reqs);
1150         req = ERR_PTR(-ENOMEM);
1151 out:
1152         return req;
1153 }
1154
1155 static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1156 {
1157         if (atomic_add_unless(&xprt->num_reqs, -1, xprt->min_reqs)) {
1158                 kfree(req);
1159                 return true;
1160         }
1161         return false;
1162 }
1163
1164 void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
1165 {
1166         struct rpc_rqst *req;
1167
1168         spin_lock(&xprt->reserve_lock);
1169         if (!list_empty(&xprt->free)) {
1170                 req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
1171                 list_del(&req->rq_list);
1172                 goto out_init_req;
1173         }
1174         req = xprt_dynamic_alloc_slot(xprt);
1175         if (!IS_ERR(req))
1176                 goto out_init_req;
1177         switch (PTR_ERR(req)) {
1178         case -ENOMEM:
1179                 dprintk("RPC:       dynamic allocation of request slot "
1180                                 "failed! Retrying\n");
1181                 task->tk_status = -ENOMEM;
1182                 break;
1183         case -EAGAIN:
1184                 xprt_add_backlog(xprt, task);
1185                 dprintk("RPC:       waiting for request slot\n");
1186         default:
1187                 task->tk_status = -EAGAIN;
1188         }
1189         spin_unlock(&xprt->reserve_lock);
1190         return;
1191 out_init_req:
1192         task->tk_status = 0;
1193         task->tk_rqstp = req;
1194         xprt_request_init(task, xprt);
1195         spin_unlock(&xprt->reserve_lock);
1196 }
1197 EXPORT_SYMBOL_GPL(xprt_alloc_slot);
1198
1199 void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
1200 {
1201         /* Note: grabbing the xprt_lock_write() ensures that we throttle
1202          * new slot allocation if the transport is congested (i.e. when
1203          * reconnecting a stream transport or when out of socket write
1204          * buffer space).
1205          */
1206         if (xprt_lock_write(xprt, task)) {
1207                 xprt_alloc_slot(xprt, task);
1208                 xprt_release_write(xprt, task);
1209         }
1210 }
1211 EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot);
1212
1213 static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
1214 {
1215         spin_lock(&xprt->reserve_lock);
1216         if (!xprt_dynamic_free_slot(xprt, req)) {
1217                 memset(req, 0, sizeof(*req));   /* mark unused */
1218                 list_add(&req->rq_list, &xprt->free);
1219         }
1220         xprt_wake_up_backlog(xprt);
1221         spin_unlock(&xprt->reserve_lock);
1222 }
1223
1224 static void xprt_free_all_slots(struct rpc_xprt *xprt)
1225 {
1226         struct rpc_rqst *req;
1227         while (!list_empty(&xprt->free)) {
1228                 req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
1229                 list_del(&req->rq_list);
1230                 kfree(req);
1231         }
1232 }
1233
1234 struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1235                 unsigned int num_prealloc,
1236                 unsigned int max_alloc)
1237 {
1238         struct rpc_xprt *xprt;
1239         struct rpc_rqst *req;
1240         int i;
1241
1242         xprt = kzalloc(size, GFP_KERNEL);
1243         if (xprt == NULL)
1244                 goto out;
1245
1246         xprt_init(xprt, net);
1247
1248         for (i = 0; i < num_prealloc; i++) {
1249                 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1250                 if (!req)
1251                         goto out_free;
1252                 list_add(&req->rq_list, &xprt->free);
1253         }
1254         if (max_alloc > num_prealloc)
1255                 xprt->max_reqs = max_alloc;
1256         else
1257                 xprt->max_reqs = num_prealloc;
1258         xprt->min_reqs = num_prealloc;
1259         atomic_set(&xprt->num_reqs, num_prealloc);
1260
1261         return xprt;
1262
1263 out_free:
1264         xprt_free(xprt);
1265 out:
1266         return NULL;
1267 }
1268 EXPORT_SYMBOL_GPL(xprt_alloc);
1269
1270 void xprt_free(struct rpc_xprt *xprt)
1271 {
1272         put_net(xprt->xprt_net);
1273         xprt_free_all_slots(xprt);
1274         kfree_rcu(xprt, rcu);
1275 }
1276 EXPORT_SYMBOL_GPL(xprt_free);
1277
1278 /**
1279  * xprt_reserve - allocate an RPC request slot
1280  * @task: RPC task requesting a slot allocation
1281  *
1282  * If the transport is marked as being congested, or if no more
1283  * slots are available, place the task on the transport's
1284  * backlog queue.
1285  */
1286 void xprt_reserve(struct rpc_task *task)
1287 {
1288         struct rpc_xprt *xprt = task->tk_xprt;
1289
1290         task->tk_status = 0;
1291         if (task->tk_rqstp != NULL)
1292                 return;
1293
1294         task->tk_timeout = 0;
1295         task->tk_status = -EAGAIN;
1296         if (!xprt_throttle_congested(xprt, task))
1297                 xprt->ops->alloc_slot(xprt, task);
1298 }
1299
1300 /**
1301  * xprt_retry_reserve - allocate an RPC request slot
1302  * @task: RPC task requesting a slot allocation
1303  *
1304  * If no more slots are available, place the task on the transport's
1305  * backlog queue.
1306  * Note that the only difference with xprt_reserve is that we now
1307  * ignore the value of the XPRT_CONGESTED flag.
1308  */
1309 void xprt_retry_reserve(struct rpc_task *task)
1310 {
1311         struct rpc_xprt *xprt = task->tk_xprt;
1312
1313         task->tk_status = 0;
1314         if (task->tk_rqstp != NULL)
1315                 return;
1316
1317         task->tk_timeout = 0;
1318         task->tk_status = -EAGAIN;
1319         xprt->ops->alloc_slot(xprt, task);
1320 }
1321
1322 static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
1323 {
1324         return (__force __be32)xprt->xid++;
1325 }
1326
1327 static inline void xprt_init_xid(struct rpc_xprt *xprt)
1328 {
1329         xprt->xid = prandom_u32();
1330 }
1331
1332 static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
1333 {
1334         struct rpc_rqst *req = task->tk_rqstp;
1335
1336         INIT_LIST_HEAD(&req->rq_list);
1337         req->rq_timeout = task->tk_client->cl_timeout->to_initval;
1338         req->rq_task    = task;
1339         req->rq_xprt    = xprt;
1340         req->rq_buffer  = NULL;
1341         req->rq_xid     = xprt_alloc_xid(xprt);
1342         req->rq_connect_cookie = xprt->connect_cookie - 1;
1343         req->rq_bytes_sent = 0;
1344         req->rq_snd_buf.len = 0;
1345         req->rq_snd_buf.buflen = 0;
1346         req->rq_rcv_buf.len = 0;
1347         req->rq_rcv_buf.buflen = 0;
1348         req->rq_release_snd_buf = NULL;
1349         xprt_reset_majortimeo(req);
1350         dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
1351                         req, ntohl(req->rq_xid));
1352 }
1353
1354 /**
1355  * xprt_release - release an RPC request slot
1356  * @task: task which is finished with the slot
1357  *
1358  */
1359 void xprt_release(struct rpc_task *task)
1360 {
1361         struct rpc_xprt *xprt;
1362         struct rpc_rqst *req = task->tk_rqstp;
1363
1364         if (req == NULL) {
1365                 if (task->tk_client) {
1366                         xprt = task->tk_xprt;
1367                         if (xprt->snd_task == task)
1368                                 xprt_release_write(xprt, task);
1369                 }
1370                 return;
1371         }
1372
1373         xprt = req->rq_xprt;
1374         if (task->tk_ops->rpc_count_stats != NULL)
1375                 task->tk_ops->rpc_count_stats(task, task->tk_calldata);
1376         else if (task->tk_client)
1377                 rpc_count_iostats(task, task->tk_client->cl_metrics);
1378         spin_lock(&xprt->recv_lock);
1379         if (!list_empty(&req->rq_list)) {
1380                 list_del_init(&req->rq_list);
1381                 xprt_wait_on_pinned_rqst(req);
1382         }
1383         spin_unlock(&xprt->recv_lock);
1384         spin_lock_bh(&xprt->transport_lock);
1385         xprt->ops->release_xprt(xprt, task);
1386         if (xprt->ops->release_request)
1387                 xprt->ops->release_request(task);
1388         xprt->last_used = jiffies;
1389         xprt_schedule_autodisconnect(xprt);
1390         spin_unlock_bh(&xprt->transport_lock);
1391         if (req->rq_buffer)
1392                 xprt->ops->buf_free(task);
1393         xprt_inject_disconnect(xprt);
1394         if (req->rq_cred != NULL)
1395                 put_rpccred(req->rq_cred);
1396         task->tk_rqstp = NULL;
1397         if (req->rq_release_snd_buf)
1398                 req->rq_release_snd_buf(req);
1399
1400         dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
1401         if (likely(!bc_prealloc(req)))
1402                 xprt_free_slot(xprt, req);
1403         else
1404                 xprt_free_bc_request(req);
1405 }
1406
1407 static void xprt_init(struct rpc_xprt *xprt, struct net *net)
1408 {
1409         kref_init(&xprt->kref);
1410
1411         spin_lock_init(&xprt->transport_lock);
1412         spin_lock_init(&xprt->reserve_lock);
1413         spin_lock_init(&xprt->recv_lock);
1414
1415         INIT_LIST_HEAD(&xprt->free);
1416         INIT_LIST_HEAD(&xprt->recv);
1417 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1418         spin_lock_init(&xprt->bc_pa_lock);
1419         INIT_LIST_HEAD(&xprt->bc_pa_list);
1420 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1421         INIT_LIST_HEAD(&xprt->xprt_switch);
1422
1423         xprt->last_used = jiffies;
1424         xprt->cwnd = RPC_INITCWND;
1425         xprt->bind_index = 0;
1426
1427         rpc_init_wait_queue(&xprt->binding, "xprt_binding");
1428         rpc_init_wait_queue(&xprt->pending, "xprt_pending");
1429         rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending");
1430         rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
1431
1432         xprt_init_xid(xprt);
1433
1434         xprt->xprt_net = get_net(net);
1435 }
1436
1437 /**
1438  * xprt_create_transport - create an RPC transport
1439  * @args: rpc transport creation arguments
1440  *
1441  */
1442 struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
1443 {
1444         struct rpc_xprt *xprt;
1445         struct xprt_class *t;
1446
1447         spin_lock(&xprt_list_lock);
1448         list_for_each_entry(t, &xprt_list, list) {
1449                 if (t->ident == args->ident) {
1450                         spin_unlock(&xprt_list_lock);
1451                         goto found;
1452                 }
1453         }
1454         spin_unlock(&xprt_list_lock);
1455         dprintk("RPC: transport (%d) not supported\n", args->ident);
1456         return ERR_PTR(-EIO);
1457
1458 found:
1459         xprt = t->setup(args);
1460         if (IS_ERR(xprt)) {
1461                 dprintk("RPC:       xprt_create_transport: failed, %ld\n",
1462                                 -PTR_ERR(xprt));
1463                 goto out;
1464         }
1465         if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
1466                 xprt->idle_timeout = 0;
1467         INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
1468         if (xprt_has_timer(xprt))
1469                 setup_timer(&xprt->timer, xprt_init_autodisconnect,
1470                             (unsigned long)xprt);
1471         else
1472                 init_timer(&xprt->timer);
1473
1474         if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
1475                 xprt_destroy(xprt);
1476                 return ERR_PTR(-EINVAL);
1477         }
1478         xprt->servername = kstrdup(args->servername, GFP_KERNEL);
1479         if (xprt->servername == NULL) {
1480                 xprt_destroy(xprt);
1481                 return ERR_PTR(-ENOMEM);
1482         }
1483
1484         rpc_xprt_debugfs_register(xprt);
1485
1486         dprintk("RPC:       created transport %p with %u slots\n", xprt,
1487                         xprt->max_reqs);
1488 out:
1489         return xprt;
1490 }
1491
1492 static void xprt_destroy_cb(struct work_struct *work)
1493 {
1494         struct rpc_xprt *xprt =
1495                 container_of(work, struct rpc_xprt, task_cleanup);
1496
1497         rpc_xprt_debugfs_unregister(xprt);
1498         rpc_destroy_wait_queue(&xprt->binding);
1499         rpc_destroy_wait_queue(&xprt->pending);
1500         rpc_destroy_wait_queue(&xprt->sending);
1501         rpc_destroy_wait_queue(&xprt->backlog);
1502         kfree(xprt->servername);
1503         /*
1504          * Tear down transport state and free the rpc_xprt
1505          */
1506         xprt->ops->destroy(xprt);
1507 }
1508
1509 /**
1510  * xprt_destroy - destroy an RPC transport, killing off all requests.
1511  * @xprt: transport to destroy
1512  *
1513  */
1514 static void xprt_destroy(struct rpc_xprt *xprt)
1515 {
1516         dprintk("RPC:       destroying transport %p\n", xprt);
1517
1518         /*
1519          * Exclude transport connect/disconnect handlers and autoclose
1520          */
1521         wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
1522
1523         del_timer_sync(&xprt->timer);
1524
1525         /*
1526          * Destroy sockets etc from the system workqueue so they can
1527          * safely flush receive work running on rpciod.
1528          */
1529         INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
1530         schedule_work(&xprt->task_cleanup);
1531 }
1532
1533 static void xprt_destroy_kref(struct kref *kref)
1534 {
1535         xprt_destroy(container_of(kref, struct rpc_xprt, kref));
1536 }
1537
1538 /**
1539  * xprt_get - return a reference to an RPC transport.
1540  * @xprt: pointer to the transport
1541  *
1542  */
1543 struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
1544 {
1545         if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
1546                 return xprt;
1547         return NULL;
1548 }
1549 EXPORT_SYMBOL_GPL(xprt_get);
1550
1551 /**
1552  * xprt_put - release a reference to an RPC transport.
1553  * @xprt: pointer to the transport
1554  *
1555  */
1556 void xprt_put(struct rpc_xprt *xprt)
1557 {
1558         if (xprt != NULL)
1559                 kref_put(&xprt->kref, xprt_destroy_kref);
1560 }
1561 EXPORT_SYMBOL_GPL(xprt_put);