xprt.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. req->rq_release_snd_buf = NULL;
  755. dprintk("RPC: %4d reserved req %p xid %08x\n", task->tk_pid,
  756. req, ntohl(req->rq_xid));
  757. }
  758. /**
  759. * xprt_release - release an RPC request slot
  760. * @task: task which is finished with the slot
  761. *
  762. */
  763. void xprt_release(struct rpc_task *task)
  764. {
  765. struct rpc_xprt *xprt = task->tk_xprt;
  766. struct rpc_rqst *req;
  767. if (!(req = task->tk_rqstp))
  768. return;
  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) && !xprt->shutdown)
  777. mod_timer(&xprt->timer,
  778. xprt->last_used + xprt->idle_timeout);
  779. spin_unlock_bh(&xprt->transport_lock);
  780. task->tk_rqstp = NULL;
  781. if (req->rq_release_snd_buf)
  782. req->rq_release_snd_buf(req);
  783. memset(req, 0, sizeof(*req)); /* mark unused */
  784. dprintk("RPC: %4d release request %p\n", task->tk_pid, req);
  785. spin_lock(&xprt->reserve_lock);
  786. list_add(&req->rq_list, &xprt->free);
  787. rpc_wake_up_next(&xprt->backlog);
  788. spin_unlock(&xprt->reserve_lock);
  789. }
  790. /**
  791. * xprt_set_timeout - set constant RPC timeout
  792. * @to: RPC timeout parameters to set up
  793. * @retr: number of retries
  794. * @incr: amount of increase after each retry
  795. *
  796. */
  797. void xprt_set_timeout(struct rpc_timeout *to, unsigned int retr, unsigned long incr)
  798. {
  799. to->to_initval =
  800. to->to_increment = incr;
  801. to->to_maxval = to->to_initval + (incr * retr);
  802. to->to_retries = retr;
  803. to->to_exponential = 0;
  804. }
  805. static struct rpc_xprt *xprt_setup(int proto, struct sockaddr_in *ap, struct rpc_timeout *to)
  806. {
  807. int result;
  808. struct rpc_xprt *xprt;
  809. struct rpc_rqst *req;
  810. if ((xprt = kmalloc(sizeof(struct rpc_xprt), GFP_KERNEL)) == NULL)
  811. return ERR_PTR(-ENOMEM);
  812. memset(xprt, 0, sizeof(*xprt)); /* Nnnngh! */
  813. xprt->addr = *ap;
  814. switch (proto) {
  815. case IPPROTO_UDP:
  816. result = xs_setup_udp(xprt, to);
  817. break;
  818. case IPPROTO_TCP:
  819. result = xs_setup_tcp(xprt, to);
  820. break;
  821. default:
  822. printk(KERN_ERR "RPC: unrecognized transport protocol: %d\n",
  823. proto);
  824. result = -EIO;
  825. break;
  826. }
  827. if (result) {
  828. kfree(xprt);
  829. return ERR_PTR(result);
  830. }
  831. spin_lock_init(&xprt->transport_lock);
  832. spin_lock_init(&xprt->reserve_lock);
  833. INIT_LIST_HEAD(&xprt->free);
  834. INIT_LIST_HEAD(&xprt->recv);
  835. INIT_WORK(&xprt->task_cleanup, xprt_autoclose, xprt);
  836. init_timer(&xprt->timer);
  837. xprt->timer.function = xprt_init_autodisconnect;
  838. xprt->timer.data = (unsigned long) xprt;
  839. xprt->last_used = jiffies;
  840. xprt->cwnd = RPC_INITCWND;
  841. rpc_init_wait_queue(&xprt->pending, "xprt_pending");
  842. rpc_init_wait_queue(&xprt->sending, "xprt_sending");
  843. rpc_init_wait_queue(&xprt->resend, "xprt_resend");
  844. rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
  845. /* initialize free list */
  846. for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
  847. list_add(&req->rq_list, &xprt->free);
  848. xprt_init_xid(xprt);
  849. dprintk("RPC: created transport %p with %u slots\n", xprt,
  850. xprt->max_reqs);
  851. return xprt;
  852. }
  853. /**
  854. * xprt_create_proto - create an RPC client transport
  855. * @proto: requested transport protocol
  856. * @sap: remote peer's address
  857. * @to: timeout parameters for new transport
  858. *
  859. */
  860. struct rpc_xprt *xprt_create_proto(int proto, struct sockaddr_in *sap, struct rpc_timeout *to)
  861. {
  862. struct rpc_xprt *xprt;
  863. xprt = xprt_setup(proto, sap, to);
  864. if (IS_ERR(xprt))
  865. dprintk("RPC: xprt_create_proto failed\n");
  866. else
  867. dprintk("RPC: xprt_create_proto created xprt %p\n", xprt);
  868. return xprt;
  869. }
  870. static void xprt_shutdown(struct rpc_xprt *xprt)
  871. {
  872. xprt->shutdown = 1;
  873. rpc_wake_up(&xprt->sending);
  874. rpc_wake_up(&xprt->resend);
  875. xprt_wake_pending_tasks(xprt, -EIO);
  876. rpc_wake_up(&xprt->backlog);
  877. del_timer_sync(&xprt->timer);
  878. }
  879. /**
  880. * xprt_destroy - destroy an RPC transport, killing off all requests.
  881. * @xprt: transport to destroy
  882. *
  883. */
  884. int xprt_destroy(struct rpc_xprt *xprt)
  885. {
  886. dprintk("RPC: destroying transport %p\n", xprt);
  887. xprt_shutdown(xprt);
  888. xprt->ops->destroy(xprt);
  889. kfree(xprt);
  890. return 0;
  891. }