xprt.c revision 6b6ca86b77b62b798cf9ca2599036420abce7796
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, it installs a timer that 16 * is run after the packet's timeout has expired. 17 * - When a packet arrives, the data_ready handler walks the list of 18 * pending requests for that transport. If a matching XID is found, the 19 * caller is woken up, and the timer removed. 20 * - When no reply arrives within the timeout interval, the timer is 21 * fired by the kernel and runs xprt_timer(). It either adjusts the 22 * timeout values (minor timeout) or wakes up the caller with a status 23 * of -ETIMEDOUT. 24 * - When the caller receives a notification from RPC that a reply arrived, 25 * it should release the RPC slot, and process the reply. 26 * If the call timed out, it may choose to retry the operation by 27 * adjusting the initial timeout value, and simply calling rpc_call 28 * again. 29 * 30 * Support for async RPC is done through a set of RPC-specific scheduling 31 * primitives that `transparently' work for processes as well as async 32 * tasks that rely on callbacks. 33 * 34 * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de> 35 * 36 * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com> 37 */ 38 39#include <linux/module.h> 40 41#include <linux/types.h> 42#include <linux/interrupt.h> 43#include <linux/workqueue.h> 44#include <linux/net.h> 45 46#include <linux/sunrpc/clnt.h> 47#include <linux/sunrpc/metrics.h> 48 49/* 50 * Local variables 51 */ 52 53#ifdef RPC_DEBUG 54# define RPCDBG_FACILITY RPCDBG_XPRT 55#endif 56 57/* 58 * Local functions 59 */ 60static void xprt_request_init(struct rpc_task *, struct rpc_xprt *); 61static inline void do_xprt_reserve(struct rpc_task *); 62static void xprt_connect_status(struct rpc_task *task); 63static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *); 64 65/* 66 * The transport code maintains an estimate on the maximum number of out- 67 * standing RPC requests, using a smoothed version of the congestion 68 * avoidance implemented in 44BSD. This is basically the Van Jacobson 69 * congestion algorithm: If a retransmit occurs, the congestion window is 70 * halved; otherwise, it is incremented by 1/cwnd when 71 * 72 * - a reply is received and 73 * - a full number of requests are outstanding and 74 * - the congestion window hasn't been updated recently. 75 */ 76#define RPC_CWNDSHIFT (8U) 77#define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT) 78#define RPC_INITCWND RPC_CWNDSCALE 79#define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT) 80 81#define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd) 82 83/** 84 * xprt_reserve_xprt - serialize write access to transports 85 * @task: task that is requesting access to the transport 86 * 87 * This prevents mixing the payload of separate requests, and prevents 88 * transport connects from colliding with writes. No congestion control 89 * is provided. 90 */ 91int xprt_reserve_xprt(struct rpc_task *task) 92{ 93 struct rpc_xprt *xprt = task->tk_xprt; 94 struct rpc_rqst *req = task->tk_rqstp; 95 96 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 97 if (task == xprt->snd_task) 98 return 1; 99 if (task == NULL) 100 return 0; 101 goto out_sleep; 102 } 103 xprt->snd_task = task; 104 if (req) { 105 req->rq_bytes_sent = 0; 106 req->rq_ntrans++; 107 } 108 return 1; 109 110out_sleep: 111 dprintk("RPC: %4d failed to lock transport %p\n", 112 task->tk_pid, xprt); 113 task->tk_timeout = 0; 114 task->tk_status = -EAGAIN; 115 if (req && req->rq_ntrans) 116 rpc_sleep_on(&xprt->resend, task, NULL, NULL); 117 else 118 rpc_sleep_on(&xprt->sending, task, NULL, NULL); 119 return 0; 120} 121 122static void xprt_clear_locked(struct rpc_xprt *xprt) 123{ 124 xprt->snd_task = NULL; 125 if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) { 126 smp_mb__before_clear_bit(); 127 clear_bit(XPRT_LOCKED, &xprt->state); 128 smp_mb__after_clear_bit(); 129 } else 130 schedule_work(&xprt->task_cleanup); 131} 132 133/* 134 * xprt_reserve_xprt_cong - serialize write access to transports 135 * @task: task that is requesting access to the transport 136 * 137 * Same as xprt_reserve_xprt, but Van Jacobson congestion control is 138 * integrated into the decision of whether a request is allowed to be 139 * woken up and given access to the transport. 140 */ 141int xprt_reserve_xprt_cong(struct rpc_task *task) 142{ 143 struct rpc_xprt *xprt = task->tk_xprt; 144 struct rpc_rqst *req = task->tk_rqstp; 145 146 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) { 147 if (task == xprt->snd_task) 148 return 1; 149 goto out_sleep; 150 } 151 if (__xprt_get_cong(xprt, task)) { 152 xprt->snd_task = task; 153 if (req) { 154 req->rq_bytes_sent = 0; 155 req->rq_ntrans++; 156 } 157 return 1; 158 } 159 xprt_clear_locked(xprt); 160out_sleep: 161 dprintk("RPC: %4d failed to lock transport %p\n", task->tk_pid, xprt); 162 task->tk_timeout = 0; 163 task->tk_status = -EAGAIN; 164 if (req && req->rq_ntrans) 165 rpc_sleep_on(&xprt->resend, task, NULL, NULL); 166 else 167 rpc_sleep_on(&xprt->sending, task, NULL, NULL); 168 return 0; 169} 170 171static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task) 172{ 173 int retval; 174 175 spin_lock_bh(&xprt->transport_lock); 176 retval = xprt->ops->reserve_xprt(task); 177 spin_unlock_bh(&xprt->transport_lock); 178 return retval; 179} 180 181static void __xprt_lock_write_next(struct rpc_xprt *xprt) 182{ 183 struct rpc_task *task; 184 struct rpc_rqst *req; 185 186 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 187 return; 188 189 task = rpc_wake_up_next(&xprt->resend); 190 if (!task) { 191 task = rpc_wake_up_next(&xprt->sending); 192 if (!task) 193 goto out_unlock; 194 } 195 196 req = task->tk_rqstp; 197 xprt->snd_task = task; 198 if (req) { 199 req->rq_bytes_sent = 0; 200 req->rq_ntrans++; 201 } 202 return; 203 204out_unlock: 205 xprt_clear_locked(xprt); 206} 207 208static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt) 209{ 210 struct rpc_task *task; 211 212 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 213 return; 214 if (RPCXPRT_CONGESTED(xprt)) 215 goto out_unlock; 216 task = rpc_wake_up_next(&xprt->resend); 217 if (!task) { 218 task = rpc_wake_up_next(&xprt->sending); 219 if (!task) 220 goto out_unlock; 221 } 222 if (__xprt_get_cong(xprt, task)) { 223 struct rpc_rqst *req = task->tk_rqstp; 224 xprt->snd_task = task; 225 if (req) { 226 req->rq_bytes_sent = 0; 227 req->rq_ntrans++; 228 } 229 return; 230 } 231out_unlock: 232 xprt_clear_locked(xprt); 233} 234 235/** 236 * xprt_release_xprt - allow other requests to use a transport 237 * @xprt: transport with other tasks potentially waiting 238 * @task: task that is releasing access to the transport 239 * 240 * Note that "task" can be NULL. No congestion control is provided. 241 */ 242void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 243{ 244 if (xprt->snd_task == task) { 245 xprt_clear_locked(xprt); 246 __xprt_lock_write_next(xprt); 247 } 248} 249 250/** 251 * xprt_release_xprt_cong - allow other requests to use a transport 252 * @xprt: transport with other tasks potentially waiting 253 * @task: task that is releasing access to the transport 254 * 255 * Note that "task" can be NULL. Another task is awoken to use the 256 * transport if the transport's congestion window allows it. 257 */ 258void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 259{ 260 if (xprt->snd_task == task) { 261 xprt_clear_locked(xprt); 262 __xprt_lock_write_next_cong(xprt); 263 } 264} 265 266static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task) 267{ 268 spin_lock_bh(&xprt->transport_lock); 269 xprt->ops->release_xprt(xprt, task); 270 spin_unlock_bh(&xprt->transport_lock); 271} 272 273/* 274 * Van Jacobson congestion avoidance. Check if the congestion window 275 * overflowed. Put the task to sleep if this is the case. 276 */ 277static int 278__xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task) 279{ 280 struct rpc_rqst *req = task->tk_rqstp; 281 282 if (req->rq_cong) 283 return 1; 284 dprintk("RPC: %4d xprt_cwnd_limited cong = %ld cwnd = %ld\n", 285 task->tk_pid, xprt->cong, xprt->cwnd); 286 if (RPCXPRT_CONGESTED(xprt)) 287 return 0; 288 req->rq_cong = 1; 289 xprt->cong += RPC_CWNDSCALE; 290 return 1; 291} 292 293/* 294 * Adjust the congestion window, and wake up the next task 295 * that has been sleeping due to congestion 296 */ 297static void 298__xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req) 299{ 300 if (!req->rq_cong) 301 return; 302 req->rq_cong = 0; 303 xprt->cong -= RPC_CWNDSCALE; 304 __xprt_lock_write_next_cong(xprt); 305} 306 307/** 308 * xprt_release_rqst_cong - housekeeping when request is complete 309 * @task: RPC request that recently completed 310 * 311 * Useful for transports that require congestion control. 312 */ 313void xprt_release_rqst_cong(struct rpc_task *task) 314{ 315 __xprt_put_cong(task->tk_xprt, task->tk_rqstp); 316} 317 318/** 319 * xprt_adjust_cwnd - adjust transport congestion window 320 * @task: recently completed RPC request used to adjust window 321 * @result: result code of completed RPC request 322 * 323 * We use a time-smoothed congestion estimator to avoid heavy oscillation. 324 */ 325void xprt_adjust_cwnd(struct rpc_task *task, int result) 326{ 327 struct rpc_rqst *req = task->tk_rqstp; 328 struct rpc_xprt *xprt = task->tk_xprt; 329 unsigned long cwnd = xprt->cwnd; 330 331 if (result >= 0 && cwnd <= xprt->cong) { 332 /* The (cwnd >> 1) term makes sure 333 * the result gets rounded properly. */ 334 cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd; 335 if (cwnd > RPC_MAXCWND(xprt)) 336 cwnd = RPC_MAXCWND(xprt); 337 __xprt_lock_write_next_cong(xprt); 338 } else if (result == -ETIMEDOUT) { 339 cwnd >>= 1; 340 if (cwnd < RPC_CWNDSCALE) 341 cwnd = RPC_CWNDSCALE; 342 } 343 dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n", 344 xprt->cong, xprt->cwnd, cwnd); 345 xprt->cwnd = cwnd; 346 __xprt_put_cong(xprt, req); 347} 348 349/** 350 * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue 351 * @xprt: transport with waiting tasks 352 * @status: result code to plant in each task before waking it 353 * 354 */ 355void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status) 356{ 357 if (status < 0) 358 rpc_wake_up_status(&xprt->pending, status); 359 else 360 rpc_wake_up(&xprt->pending); 361} 362 363/** 364 * xprt_wait_for_buffer_space - wait for transport output buffer to clear 365 * @task: task to be put to sleep 366 * 367 */ 368void xprt_wait_for_buffer_space(struct rpc_task *task) 369{ 370 struct rpc_rqst *req = task->tk_rqstp; 371 struct rpc_xprt *xprt = req->rq_xprt; 372 373 task->tk_timeout = req->rq_timeout; 374 rpc_sleep_on(&xprt->pending, task, NULL, NULL); 375} 376 377/** 378 * xprt_write_space - wake the task waiting for transport output buffer space 379 * @xprt: transport with waiting tasks 380 * 381 * Can be called in a soft IRQ context, so xprt_write_space never sleeps. 382 */ 383void xprt_write_space(struct rpc_xprt *xprt) 384{ 385 if (unlikely(xprt->shutdown)) 386 return; 387 388 spin_lock_bh(&xprt->transport_lock); 389 if (xprt->snd_task) { 390 dprintk("RPC: write space: waking waiting task on xprt %p\n", 391 xprt); 392 rpc_wake_up_task(xprt->snd_task); 393 } 394 spin_unlock_bh(&xprt->transport_lock); 395} 396 397/** 398 * xprt_set_retrans_timeout_def - set a request's retransmit timeout 399 * @task: task whose timeout is to be set 400 * 401 * Set a request's retransmit timeout based on the transport's 402 * default timeout parameters. Used by transports that don't adjust 403 * the retransmit timeout based on round-trip time estimation. 404 */ 405void xprt_set_retrans_timeout_def(struct rpc_task *task) 406{ 407 task->tk_timeout = task->tk_rqstp->rq_timeout; 408} 409 410/* 411 * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout 412 * @task: task whose timeout is to be set 413 * 414 * Set a request's retransmit timeout using the RTT estimator. 415 */ 416void xprt_set_retrans_timeout_rtt(struct rpc_task *task) 417{ 418 int timer = task->tk_msg.rpc_proc->p_timer; 419 struct rpc_rtt *rtt = task->tk_client->cl_rtt; 420 struct rpc_rqst *req = task->tk_rqstp; 421 unsigned long max_timeout = req->rq_xprt->timeout.to_maxval; 422 423 task->tk_timeout = rpc_calc_rto(rtt, timer); 424 task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries; 425 if (task->tk_timeout > max_timeout || task->tk_timeout == 0) 426 task->tk_timeout = max_timeout; 427} 428 429static void xprt_reset_majortimeo(struct rpc_rqst *req) 430{ 431 struct rpc_timeout *to = &req->rq_xprt->timeout; 432 433 req->rq_majortimeo = req->rq_timeout; 434 if (to->to_exponential) 435 req->rq_majortimeo <<= to->to_retries; 436 else 437 req->rq_majortimeo += to->to_increment * to->to_retries; 438 if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0) 439 req->rq_majortimeo = to->to_maxval; 440 req->rq_majortimeo += jiffies; 441} 442 443/** 444 * xprt_adjust_timeout - adjust timeout values for next retransmit 445 * @req: RPC request containing parameters to use for the adjustment 446 * 447 */ 448int xprt_adjust_timeout(struct rpc_rqst *req) 449{ 450 struct rpc_xprt *xprt = req->rq_xprt; 451 struct rpc_timeout *to = &xprt->timeout; 452 int status = 0; 453 454 if (time_before(jiffies, req->rq_majortimeo)) { 455 if (to->to_exponential) 456 req->rq_timeout <<= 1; 457 else 458 req->rq_timeout += to->to_increment; 459 if (to->to_maxval && req->rq_timeout >= to->to_maxval) 460 req->rq_timeout = to->to_maxval; 461 req->rq_retries++; 462 pprintk("RPC: %lu retrans\n", jiffies); 463 } else { 464 req->rq_timeout = to->to_initval; 465 req->rq_retries = 0; 466 xprt_reset_majortimeo(req); 467 /* Reset the RTT counters == "slow start" */ 468 spin_lock_bh(&xprt->transport_lock); 469 rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval); 470 spin_unlock_bh(&xprt->transport_lock); 471 pprintk("RPC: %lu timeout\n", jiffies); 472 status = -ETIMEDOUT; 473 } 474 475 if (req->rq_timeout == 0) { 476 printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n"); 477 req->rq_timeout = 5 * HZ; 478 } 479 return status; 480} 481 482static void xprt_autoclose(void *args) 483{ 484 struct rpc_xprt *xprt = (struct rpc_xprt *)args; 485 486 xprt_disconnect(xprt); 487 xprt->ops->close(xprt); 488 xprt_release_write(xprt, NULL); 489} 490 491/** 492 * xprt_disconnect - mark a transport as disconnected 493 * @xprt: transport to flag for disconnect 494 * 495 */ 496void xprt_disconnect(struct rpc_xprt *xprt) 497{ 498 dprintk("RPC: disconnected transport %p\n", xprt); 499 spin_lock_bh(&xprt->transport_lock); 500 xprt_clear_connected(xprt); 501 xprt_wake_pending_tasks(xprt, -ENOTCONN); 502 spin_unlock_bh(&xprt->transport_lock); 503} 504 505static void 506xprt_init_autodisconnect(unsigned long data) 507{ 508 struct rpc_xprt *xprt = (struct rpc_xprt *)data; 509 510 spin_lock(&xprt->transport_lock); 511 if (!list_empty(&xprt->recv) || xprt->shutdown) 512 goto out_abort; 513 if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) 514 goto out_abort; 515 spin_unlock(&xprt->transport_lock); 516 if (xprt_connecting(xprt)) 517 xprt_release_write(xprt, NULL); 518 else 519 schedule_work(&xprt->task_cleanup); 520 return; 521out_abort: 522 spin_unlock(&xprt->transport_lock); 523} 524 525/** 526 * xprt_connect - schedule a transport connect operation 527 * @task: RPC task that is requesting the connect 528 * 529 */ 530void xprt_connect(struct rpc_task *task) 531{ 532 struct rpc_xprt *xprt = task->tk_xprt; 533 534 dprintk("RPC: %4d xprt_connect xprt %p %s connected\n", task->tk_pid, 535 xprt, (xprt_connected(xprt) ? "is" : "is not")); 536 537 if (!xprt_bound(xprt)) { 538 task->tk_status = -EIO; 539 return; 540 } 541 if (!xprt_lock_write(xprt, task)) 542 return; 543 if (xprt_connected(xprt)) 544 xprt_release_write(xprt, task); 545 else { 546 if (task->tk_rqstp) 547 task->tk_rqstp->rq_bytes_sent = 0; 548 549 task->tk_timeout = xprt->connect_timeout; 550 rpc_sleep_on(&xprt->pending, task, xprt_connect_status, NULL); 551 xprt->stat.connect_start = jiffies; 552 xprt->ops->connect(task); 553 } 554 return; 555} 556 557static void xprt_connect_status(struct rpc_task *task) 558{ 559 struct rpc_xprt *xprt = task->tk_xprt; 560 561 if (task->tk_status >= 0) { 562 xprt->stat.connect_count++; 563 xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start; 564 dprintk("RPC: %4d xprt_connect_status: connection established\n", 565 task->tk_pid); 566 return; 567 } 568 569 switch (task->tk_status) { 570 case -ECONNREFUSED: 571 case -ECONNRESET: 572 dprintk("RPC: %4d xprt_connect_status: server %s refused connection\n", 573 task->tk_pid, task->tk_client->cl_server); 574 break; 575 case -ENOTCONN: 576 dprintk("RPC: %4d xprt_connect_status: connection broken\n", 577 task->tk_pid); 578 break; 579 case -ETIMEDOUT: 580 dprintk("RPC: %4d xprt_connect_status: connect attempt timed out\n", 581 task->tk_pid); 582 break; 583 default: 584 dprintk("RPC: %4d xprt_connect_status: error %d connecting to server %s\n", 585 task->tk_pid, -task->tk_status, task->tk_client->cl_server); 586 xprt_release_write(xprt, task); 587 task->tk_status = -EIO; 588 } 589} 590 591/** 592 * xprt_lookup_rqst - find an RPC request corresponding to an XID 593 * @xprt: transport on which the original request was transmitted 594 * @xid: RPC XID of incoming reply 595 * 596 */ 597struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, u32 xid) 598{ 599 struct list_head *pos; 600 601 list_for_each(pos, &xprt->recv) { 602 struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list); 603 if (entry->rq_xid == xid) 604 return entry; 605 } 606 xprt->stat.bad_xids++; 607 return NULL; 608} 609 610/** 611 * xprt_update_rtt - update an RPC client's RTT state after receiving a reply 612 * @task: RPC request that recently completed 613 * 614 */ 615void xprt_update_rtt(struct rpc_task *task) 616{ 617 struct rpc_rqst *req = task->tk_rqstp; 618 struct rpc_rtt *rtt = task->tk_client->cl_rtt; 619 unsigned timer = task->tk_msg.rpc_proc->p_timer; 620 621 if (timer) { 622 if (req->rq_ntrans == 1) 623 rpc_update_rtt(rtt, timer, 624 (long)jiffies - req->rq_xtime); 625 rpc_set_timeo(rtt, timer, req->rq_ntrans - 1); 626 } 627} 628 629/** 630 * xprt_complete_rqst - called when reply processing is complete 631 * @task: RPC request that recently completed 632 * @copied: actual number of bytes received from the transport 633 * 634 * Caller holds transport lock. 635 */ 636void xprt_complete_rqst(struct rpc_task *task, int copied) 637{ 638 struct rpc_rqst *req = task->tk_rqstp; 639 640 dprintk("RPC: %5u xid %08x complete (%d bytes received)\n", 641 task->tk_pid, ntohl(req->rq_xid), copied); 642 643 task->tk_xprt->stat.recvs++; 644 task->tk_rtt = (long)jiffies - req->rq_xtime; 645 646 list_del_init(&req->rq_list); 647 /* Ensure all writes are done before we update req->rq_received */ 648 smp_wmb(); 649 req->rq_received = req->rq_private_buf.len = copied; 650 rpc_wake_up_task(task); 651} 652 653static void xprt_timer(struct rpc_task *task) 654{ 655 struct rpc_rqst *req = task->tk_rqstp; 656 struct rpc_xprt *xprt = req->rq_xprt; 657 658 dprintk("RPC: %4d xprt_timer\n", task->tk_pid); 659 660 spin_lock(&xprt->transport_lock); 661 if (!req->rq_received) { 662 if (xprt->ops->timer) 663 xprt->ops->timer(task); 664 task->tk_status = -ETIMEDOUT; 665 } 666 task->tk_timeout = 0; 667 rpc_wake_up_task(task); 668 spin_unlock(&xprt->transport_lock); 669} 670 671/** 672 * xprt_prepare_transmit - reserve the transport before sending a request 673 * @task: RPC task about to send a request 674 * 675 */ 676int xprt_prepare_transmit(struct rpc_task *task) 677{ 678 struct rpc_rqst *req = task->tk_rqstp; 679 struct rpc_xprt *xprt = req->rq_xprt; 680 int err = 0; 681 682 dprintk("RPC: %4d xprt_prepare_transmit\n", task->tk_pid); 683 684 spin_lock_bh(&xprt->transport_lock); 685 if (req->rq_received && !req->rq_bytes_sent) { 686 err = req->rq_received; 687 goto out_unlock; 688 } 689 if (!xprt->ops->reserve_xprt(task)) { 690 err = -EAGAIN; 691 goto out_unlock; 692 } 693 694 if (!xprt_connected(xprt)) { 695 err = -ENOTCONN; 696 goto out_unlock; 697 } 698out_unlock: 699 spin_unlock_bh(&xprt->transport_lock); 700 return err; 701} 702 703void xprt_end_transmit(struct rpc_task *task) 704{ 705 xprt_release_write(task->tk_xprt, task); 706} 707 708/** 709 * xprt_transmit - send an RPC request on a transport 710 * @task: controlling RPC task 711 * 712 * We have to copy the iovec because sendmsg fiddles with its contents. 713 */ 714void xprt_transmit(struct rpc_task *task) 715{ 716 struct rpc_rqst *req = task->tk_rqstp; 717 struct rpc_xprt *xprt = req->rq_xprt; 718 int status; 719 720 dprintk("RPC: %4d xprt_transmit(%u)\n", task->tk_pid, req->rq_slen); 721 722 if (!req->rq_received) { 723 if (list_empty(&req->rq_list)) { 724 spin_lock_bh(&xprt->transport_lock); 725 /* Update the softirq receive buffer */ 726 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, 727 sizeof(req->rq_private_buf)); 728 /* Add request to the receive list */ 729 list_add_tail(&req->rq_list, &xprt->recv); 730 spin_unlock_bh(&xprt->transport_lock); 731 xprt_reset_majortimeo(req); 732 /* Turn off autodisconnect */ 733 del_singleshot_timer_sync(&xprt->timer); 734 } 735 } else if (!req->rq_bytes_sent) 736 return; 737 738 status = xprt->ops->send_request(task); 739 if (status == 0) { 740 dprintk("RPC: %4d xmit complete\n", task->tk_pid); 741 spin_lock_bh(&xprt->transport_lock); 742 743 xprt->ops->set_retrans_timeout(task); 744 745 xprt->stat.sends++; 746 xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs; 747 xprt->stat.bklog_u += xprt->backlog.qlen; 748 749 /* Don't race with disconnect */ 750 if (!xprt_connected(xprt)) 751 task->tk_status = -ENOTCONN; 752 else if (!req->rq_received) 753 rpc_sleep_on(&xprt->pending, task, NULL, xprt_timer); 754 spin_unlock_bh(&xprt->transport_lock); 755 return; 756 } 757 758 /* Note: at this point, task->tk_sleeping has not yet been set, 759 * hence there is no danger of the waking up task being put on 760 * schedq, and being picked up by a parallel run of rpciod(). 761 */ 762 task->tk_status = status; 763 if (status == -ECONNREFUSED) 764 rpc_sleep_on(&xprt->sending, task, NULL, NULL); 765} 766 767static inline void do_xprt_reserve(struct rpc_task *task) 768{ 769 struct rpc_xprt *xprt = task->tk_xprt; 770 771 task->tk_status = 0; 772 if (task->tk_rqstp) 773 return; 774 if (!list_empty(&xprt->free)) { 775 struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list); 776 list_del_init(&req->rq_list); 777 task->tk_rqstp = req; 778 xprt_request_init(task, xprt); 779 return; 780 } 781 dprintk("RPC: waiting for request slot\n"); 782 task->tk_status = -EAGAIN; 783 task->tk_timeout = 0; 784 rpc_sleep_on(&xprt->backlog, task, NULL, NULL); 785} 786 787/** 788 * xprt_reserve - allocate an RPC request slot 789 * @task: RPC task requesting a slot allocation 790 * 791 * If no more slots are available, place the task on the transport's 792 * backlog queue. 793 */ 794void xprt_reserve(struct rpc_task *task) 795{ 796 struct rpc_xprt *xprt = task->tk_xprt; 797 798 task->tk_status = -EIO; 799 spin_lock(&xprt->reserve_lock); 800 do_xprt_reserve(task); 801 spin_unlock(&xprt->reserve_lock); 802} 803 804static inline u32 xprt_alloc_xid(struct rpc_xprt *xprt) 805{ 806 return xprt->xid++; 807} 808 809static inline void xprt_init_xid(struct rpc_xprt *xprt) 810{ 811 xprt->xid = net_random(); 812} 813 814static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt) 815{ 816 struct rpc_rqst *req = task->tk_rqstp; 817 818 req->rq_timeout = xprt->timeout.to_initval; 819 req->rq_task = task; 820 req->rq_xprt = xprt; 821 req->rq_buffer = NULL; 822 req->rq_bufsize = 0; 823 req->rq_xid = xprt_alloc_xid(xprt); 824 req->rq_release_snd_buf = NULL; 825 xprt_reset_majortimeo(req); 826 dprintk("RPC: %4d reserved req %p xid %08x\n", task->tk_pid, 827 req, ntohl(req->rq_xid)); 828} 829 830/** 831 * xprt_release - release an RPC request slot 832 * @task: task which is finished with the slot 833 * 834 */ 835void xprt_release(struct rpc_task *task) 836{ 837 struct rpc_xprt *xprt = task->tk_xprt; 838 struct rpc_rqst *req; 839 840 if (!(req = task->tk_rqstp)) 841 return; 842 rpc_count_iostats(task); 843 spin_lock_bh(&xprt->transport_lock); 844 xprt->ops->release_xprt(xprt, task); 845 if (xprt->ops->release_request) 846 xprt->ops->release_request(task); 847 if (!list_empty(&req->rq_list)) 848 list_del(&req->rq_list); 849 xprt->last_used = jiffies; 850 if (list_empty(&xprt->recv)) 851 mod_timer(&xprt->timer, 852 xprt->last_used + xprt->idle_timeout); 853 spin_unlock_bh(&xprt->transport_lock); 854 xprt->ops->buf_free(task); 855 task->tk_rqstp = NULL; 856 if (req->rq_release_snd_buf) 857 req->rq_release_snd_buf(req); 858 memset(req, 0, sizeof(*req)); /* mark unused */ 859 860 dprintk("RPC: %4d release request %p\n", task->tk_pid, req); 861 862 spin_lock(&xprt->reserve_lock); 863 list_add(&req->rq_list, &xprt->free); 864 rpc_wake_up_next(&xprt->backlog); 865 spin_unlock(&xprt->reserve_lock); 866} 867 868/** 869 * xprt_set_timeout - set constant RPC timeout 870 * @to: RPC timeout parameters to set up 871 * @retr: number of retries 872 * @incr: amount of increase after each retry 873 * 874 */ 875void xprt_set_timeout(struct rpc_timeout *to, unsigned int retr, unsigned long incr) 876{ 877 to->to_initval = 878 to->to_increment = incr; 879 to->to_maxval = to->to_initval + (incr * retr); 880 to->to_retries = retr; 881 to->to_exponential = 0; 882} 883 884/** 885 * xprt_create_transport - create an RPC transport 886 * @proto: requested transport protocol 887 * @ap: remote peer address 888 * @size: length of address 889 * @to: timeout parameters 890 * 891 */ 892struct rpc_xprt *xprt_create_transport(int proto, struct sockaddr *ap, size_t size, struct rpc_timeout *to) 893{ 894 int result; 895 struct rpc_xprt *xprt; 896 struct rpc_rqst *req; 897 898 if ((xprt = kzalloc(sizeof(struct rpc_xprt), GFP_KERNEL)) == NULL) { 899 dprintk("RPC: xprt_create_transport: no memory\n"); 900 return ERR_PTR(-ENOMEM); 901 } 902 if (size <= sizeof(xprt->addr)) { 903 memcpy(&xprt->addr, ap, size); 904 xprt->addrlen = size; 905 } else { 906 kfree(xprt); 907 dprintk("RPC: xprt_create_transport: address too large\n"); 908 return ERR_PTR(-EBADF); 909 } 910 911 switch (proto) { 912 case IPPROTO_UDP: 913 result = xs_setup_udp(xprt, to); 914 break; 915 case IPPROTO_TCP: 916 result = xs_setup_tcp(xprt, to); 917 break; 918 default: 919 printk(KERN_ERR "RPC: unrecognized transport protocol: %d\n", 920 proto); 921 return ERR_PTR(-EIO); 922 } 923 if (result) { 924 kfree(xprt); 925 dprintk("RPC: xprt_create_transport: failed, %d\n", result); 926 return ERR_PTR(result); 927 } 928 929 kref_init(&xprt->kref); 930 spin_lock_init(&xprt->transport_lock); 931 spin_lock_init(&xprt->reserve_lock); 932 933 INIT_LIST_HEAD(&xprt->free); 934 INIT_LIST_HEAD(&xprt->recv); 935 INIT_WORK(&xprt->task_cleanup, xprt_autoclose, xprt); 936 init_timer(&xprt->timer); 937 xprt->timer.function = xprt_init_autodisconnect; 938 xprt->timer.data = (unsigned long) xprt; 939 xprt->last_used = jiffies; 940 xprt->cwnd = RPC_INITCWND; 941 942 rpc_init_wait_queue(&xprt->binding, "xprt_binding"); 943 rpc_init_wait_queue(&xprt->pending, "xprt_pending"); 944 rpc_init_wait_queue(&xprt->sending, "xprt_sending"); 945 rpc_init_wait_queue(&xprt->resend, "xprt_resend"); 946 rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog"); 947 948 /* initialize free list */ 949 for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--) 950 list_add(&req->rq_list, &xprt->free); 951 952 xprt_init_xid(xprt); 953 954 dprintk("RPC: created transport %p with %u slots\n", xprt, 955 xprt->max_reqs); 956 957 return xprt; 958} 959 960/** 961 * xprt_destroy - destroy an RPC transport, killing off all requests. 962 * @kref: kref for the transport to destroy 963 * 964 */ 965static void xprt_destroy(struct kref *kref) 966{ 967 struct rpc_xprt *xprt = container_of(kref, struct rpc_xprt, kref); 968 969 dprintk("RPC: destroying transport %p\n", xprt); 970 xprt->shutdown = 1; 971 del_timer_sync(&xprt->timer); 972 xprt->ops->destroy(xprt); 973 kfree(xprt); 974} 975 976/** 977 * xprt_put - release a reference to an RPC transport. 978 * @xprt: pointer to the transport 979 * 980 */ 981void xprt_put(struct rpc_xprt *xprt) 982{ 983 kref_put(&xprt->kref, xprt_destroy); 984} 985 986/** 987 * xprt_get - return a reference to an RPC transport. 988 * @xprt: pointer to the transport 989 * 990 */ 991struct rpc_xprt *xprt_get(struct rpc_xprt *xprt) 992{ 993 kref_get(&xprt->kref); 994 return xprt; 995} 996