xprt.c 31 KB

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