xprt.c 29 KB

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