/kernel/2.6.32_froyo_photon_nightly/net/sunrpc/xprt.c

http://photon-android.googlecode.com/ · C · 1159 lines · 745 code · 126 blank · 288 comment · 122 complexity · e973109a420b658e0498874d9d5e684d MD5 · raw file

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