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