xprt.c 31 KB

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