xprt.c 26 KB

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