xprt.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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_xprt *xprt = task->tk_xprt;
  179. struct rpc_rqst *req = task->tk_rqstp;
  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 (xprt_connected(xprt))
  638. xprt_release_write(xprt, task);
  639. else {
  640. if (task->tk_rqstp)
  641. task->tk_rqstp->rq_bytes_sent = 0;
  642. task->tk_timeout = xprt->connect_timeout;
  643. rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
  644. xprt->stat.connect_start = jiffies;
  645. xprt->ops->connect(task);
  646. }
  647. return;
  648. }
  649. static void xprt_connect_status(struct rpc_task *task)
  650. {
  651. struct rpc_xprt *xprt = task->tk_xprt;
  652. if (task->tk_status == 0) {
  653. xprt->stat.connect_count++;
  654. xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
  655. dprintk("RPC: %5u xprt_connect_status: connection established\n",
  656. task->tk_pid);
  657. return;
  658. }
  659. switch (task->tk_status) {
  660. case -EAGAIN:
  661. dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
  662. break;
  663. case -ETIMEDOUT:
  664. dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
  665. "out\n", task->tk_pid);
  666. break;
  667. default:
  668. dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
  669. "server %s\n", task->tk_pid, -task->tk_status,
  670. task->tk_client->cl_server);
  671. xprt_release_write(xprt, task);
  672. task->tk_status = -EIO;
  673. }
  674. }
  675. /**
  676. * xprt_lookup_rqst - find an RPC request corresponding to an XID
  677. * @xprt: transport on which the original request was transmitted
  678. * @xid: RPC XID of incoming reply
  679. *
  680. */
  681. struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
  682. {
  683. struct list_head *pos;
  684. list_for_each(pos, &xprt->recv) {
  685. struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list);
  686. if (entry->rq_xid == xid)
  687. return entry;
  688. }
  689. dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
  690. ntohl(xid));
  691. xprt->stat.bad_xids++;
  692. return NULL;
  693. }
  694. EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
  695. /**
  696. * xprt_update_rtt - update an RPC client's RTT state after receiving a reply
  697. * @task: RPC request that recently completed
  698. *
  699. */
  700. void xprt_update_rtt(struct rpc_task *task)
  701. {
  702. struct rpc_rqst *req = task->tk_rqstp;
  703. struct rpc_rtt *rtt = task->tk_client->cl_rtt;
  704. unsigned timer = task->tk_msg.rpc_proc->p_timer;
  705. if (timer) {
  706. if (req->rq_ntrans == 1)
  707. rpc_update_rtt(rtt, timer,
  708. (long)jiffies - req->rq_xtime);
  709. rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
  710. }
  711. }
  712. EXPORT_SYMBOL_GPL(xprt_update_rtt);
  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. task->tk_rtt = (long)jiffies - req->rq_xtime;
  728. list_del_init(&req->rq_list);
  729. req->rq_private_buf.len = copied;
  730. /* Ensure all writes are done before we update req->rq_received */
  731. smp_wmb();
  732. req->rq_received = copied;
  733. rpc_wake_up_queued_task(&xprt->pending, task);
  734. }
  735. EXPORT_SYMBOL_GPL(xprt_complete_rqst);
  736. static void xprt_timer(struct rpc_task *task)
  737. {
  738. struct rpc_rqst *req = task->tk_rqstp;
  739. struct rpc_xprt *xprt = req->rq_xprt;
  740. if (task->tk_status != -ETIMEDOUT)
  741. return;
  742. dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
  743. spin_lock_bh(&xprt->transport_lock);
  744. if (!req->rq_received) {
  745. if (xprt->ops->timer)
  746. xprt->ops->timer(task);
  747. } else
  748. task->tk_status = 0;
  749. spin_unlock_bh(&xprt->transport_lock);
  750. }
  751. /**
  752. * xprt_prepare_transmit - reserve the transport before sending a request
  753. * @task: RPC task about to send a request
  754. *
  755. */
  756. int xprt_prepare_transmit(struct rpc_task *task)
  757. {
  758. struct rpc_rqst *req = task->tk_rqstp;
  759. struct rpc_xprt *xprt = req->rq_xprt;
  760. int err = 0;
  761. dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
  762. spin_lock_bh(&xprt->transport_lock);
  763. if (req->rq_received && !req->rq_bytes_sent) {
  764. err = req->rq_received;
  765. goto out_unlock;
  766. }
  767. if (!xprt->ops->reserve_xprt(task))
  768. err = -EAGAIN;
  769. out_unlock:
  770. spin_unlock_bh(&xprt->transport_lock);
  771. return err;
  772. }
  773. void xprt_end_transmit(struct rpc_task *task)
  774. {
  775. xprt_release_write(task->tk_xprt, task);
  776. }
  777. /**
  778. * xprt_transmit - send an RPC request on a transport
  779. * @task: controlling RPC task
  780. *
  781. * We have to copy the iovec because sendmsg fiddles with its contents.
  782. */
  783. void xprt_transmit(struct rpc_task *task)
  784. {
  785. struct rpc_rqst *req = task->tk_rqstp;
  786. struct rpc_xprt *xprt = req->rq_xprt;
  787. int status;
  788. dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
  789. if (!req->rq_received) {
  790. if (list_empty(&req->rq_list) && rpc_reply_expected(task)) {
  791. /*
  792. * Add to the list only if we're expecting a reply
  793. */
  794. spin_lock_bh(&xprt->transport_lock);
  795. /* Update the softirq receive buffer */
  796. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  797. sizeof(req->rq_private_buf));
  798. /* Add request to the receive list */
  799. list_add_tail(&req->rq_list, &xprt->recv);
  800. spin_unlock_bh(&xprt->transport_lock);
  801. xprt_reset_majortimeo(req);
  802. /* Turn off autodisconnect */
  803. del_singleshot_timer_sync(&xprt->timer);
  804. }
  805. } else if (!req->rq_bytes_sent)
  806. return;
  807. req->rq_connect_cookie = xprt->connect_cookie;
  808. req->rq_xtime = jiffies;
  809. status = xprt->ops->send_request(task);
  810. if (status != 0) {
  811. task->tk_status = status;
  812. return;
  813. }
  814. dprintk("RPC: %5u xmit complete\n", task->tk_pid);
  815. spin_lock_bh(&xprt->transport_lock);
  816. xprt->ops->set_retrans_timeout(task);
  817. xprt->stat.sends++;
  818. xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
  819. xprt->stat.bklog_u += xprt->backlog.qlen;
  820. /* Don't race with disconnect */
  821. if (!xprt_connected(xprt))
  822. task->tk_status = -ENOTCONN;
  823. else if (!req->rq_received && rpc_reply_expected(task)) {
  824. /*
  825. * Sleep on the pending queue since
  826. * we're expecting a reply.
  827. */
  828. rpc_sleep_on(&xprt->pending, task, xprt_timer);
  829. }
  830. spin_unlock_bh(&xprt->transport_lock);
  831. }
  832. static inline void do_xprt_reserve(struct rpc_task *task)
  833. {
  834. struct rpc_xprt *xprt = task->tk_xprt;
  835. task->tk_status = 0;
  836. if (task->tk_rqstp)
  837. return;
  838. if (!list_empty(&xprt->free)) {
  839. struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
  840. list_del_init(&req->rq_list);
  841. task->tk_rqstp = req;
  842. xprt_request_init(task, xprt);
  843. return;
  844. }
  845. dprintk("RPC: waiting for request slot\n");
  846. task->tk_status = -EAGAIN;
  847. task->tk_timeout = 0;
  848. rpc_sleep_on(&xprt->backlog, task, NULL);
  849. }
  850. /**
  851. * xprt_reserve - allocate an RPC request slot
  852. * @task: RPC task requesting a slot allocation
  853. *
  854. * If no more slots are available, place the task on the transport's
  855. * backlog queue.
  856. */
  857. void xprt_reserve(struct rpc_task *task)
  858. {
  859. struct rpc_xprt *xprt = task->tk_xprt;
  860. task->tk_status = -EIO;
  861. spin_lock(&xprt->reserve_lock);
  862. do_xprt_reserve(task);
  863. spin_unlock(&xprt->reserve_lock);
  864. }
  865. static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
  866. {
  867. return xprt->xid++;
  868. }
  869. static inline void xprt_init_xid(struct rpc_xprt *xprt)
  870. {
  871. xprt->xid = net_random();
  872. }
  873. static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
  874. {
  875. struct rpc_rqst *req = task->tk_rqstp;
  876. req->rq_timeout = task->tk_client->cl_timeout->to_initval;
  877. req->rq_task = task;
  878. req->rq_xprt = xprt;
  879. req->rq_buffer = NULL;
  880. req->rq_xid = xprt_alloc_xid(xprt);
  881. req->rq_release_snd_buf = NULL;
  882. xprt_reset_majortimeo(req);
  883. dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
  884. req, ntohl(req->rq_xid));
  885. }
  886. /**
  887. * xprt_release - release an RPC request slot
  888. * @task: task which is finished with the slot
  889. *
  890. */
  891. void xprt_release(struct rpc_task *task)
  892. {
  893. struct rpc_xprt *xprt;
  894. struct rpc_rqst *req;
  895. int is_bc_request;
  896. if (!(req = task->tk_rqstp))
  897. return;
  898. /* Preallocated backchannel request? */
  899. is_bc_request = bc_prealloc(req);
  900. xprt = req->rq_xprt;
  901. rpc_count_iostats(task);
  902. spin_lock_bh(&xprt->transport_lock);
  903. xprt->ops->release_xprt(xprt, task);
  904. if (xprt->ops->release_request)
  905. xprt->ops->release_request(task);
  906. if (!list_empty(&req->rq_list))
  907. list_del(&req->rq_list);
  908. xprt->last_used = jiffies;
  909. if (list_empty(&xprt->recv))
  910. mod_timer(&xprt->timer,
  911. xprt->last_used + xprt->idle_timeout);
  912. spin_unlock_bh(&xprt->transport_lock);
  913. if (!bc_prealloc(req))
  914. xprt->ops->buf_free(req->rq_buffer);
  915. task->tk_rqstp = NULL;
  916. if (req->rq_release_snd_buf)
  917. req->rq_release_snd_buf(req);
  918. /*
  919. * Early exit if this is a backchannel preallocated request.
  920. * There is no need to have it added to the RPC slot list.
  921. */
  922. if (is_bc_request)
  923. return;
  924. memset(req, 0, sizeof(*req)); /* mark unused */
  925. dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
  926. spin_lock(&xprt->reserve_lock);
  927. list_add(&req->rq_list, &xprt->free);
  928. rpc_wake_up_next(&xprt->backlog);
  929. spin_unlock(&xprt->reserve_lock);
  930. }
  931. /**
  932. * xprt_create_transport - create an RPC transport
  933. * @args: rpc transport creation arguments
  934. *
  935. */
  936. struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
  937. {
  938. struct rpc_xprt *xprt;
  939. struct rpc_rqst *req;
  940. struct xprt_class *t;
  941. spin_lock(&xprt_list_lock);
  942. list_for_each_entry(t, &xprt_list, list) {
  943. if (t->ident == args->ident) {
  944. spin_unlock(&xprt_list_lock);
  945. goto found;
  946. }
  947. }
  948. spin_unlock(&xprt_list_lock);
  949. printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident);
  950. return ERR_PTR(-EIO);
  951. found:
  952. xprt = t->setup(args);
  953. if (IS_ERR(xprt)) {
  954. dprintk("RPC: xprt_create_transport: failed, %ld\n",
  955. -PTR_ERR(xprt));
  956. return xprt;
  957. }
  958. kref_init(&xprt->kref);
  959. spin_lock_init(&xprt->transport_lock);
  960. spin_lock_init(&xprt->reserve_lock);
  961. INIT_LIST_HEAD(&xprt->free);
  962. INIT_LIST_HEAD(&xprt->recv);
  963. #if defined(CONFIG_NFS_V4_1)
  964. spin_lock_init(&xprt->bc_pa_lock);
  965. INIT_LIST_HEAD(&xprt->bc_pa_list);
  966. #endif /* CONFIG_NFS_V4_1 */
  967. INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
  968. setup_timer(&xprt->timer, xprt_init_autodisconnect,
  969. (unsigned long)xprt);
  970. xprt->last_used = jiffies;
  971. xprt->cwnd = RPC_INITCWND;
  972. xprt->bind_index = 0;
  973. rpc_init_wait_queue(&xprt->binding, "xprt_binding");
  974. rpc_init_wait_queue(&xprt->pending, "xprt_pending");
  975. rpc_init_wait_queue(&xprt->sending, "xprt_sending");
  976. rpc_init_wait_queue(&xprt->resend, "xprt_resend");
  977. rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
  978. /* initialize free list */
  979. for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
  980. list_add(&req->rq_list, &xprt->free);
  981. xprt_init_xid(xprt);
  982. dprintk("RPC: created transport %p with %u slots\n", xprt,
  983. xprt->max_reqs);
  984. return xprt;
  985. }
  986. /**
  987. * xprt_destroy - destroy an RPC transport, killing off all requests.
  988. * @kref: kref for the transport to destroy
  989. *
  990. */
  991. static void xprt_destroy(struct kref *kref)
  992. {
  993. struct rpc_xprt *xprt = container_of(kref, struct rpc_xprt, kref);
  994. dprintk("RPC: destroying transport %p\n", xprt);
  995. xprt->shutdown = 1;
  996. del_timer_sync(&xprt->timer);
  997. rpc_destroy_wait_queue(&xprt->binding);
  998. rpc_destroy_wait_queue(&xprt->pending);
  999. rpc_destroy_wait_queue(&xprt->sending);
  1000. rpc_destroy_wait_queue(&xprt->resend);
  1001. rpc_destroy_wait_queue(&xprt->backlog);
  1002. /*
  1003. * Tear down transport state and free the rpc_xprt
  1004. */
  1005. xprt->ops->destroy(xprt);
  1006. }
  1007. /**
  1008. * xprt_put - release a reference to an RPC transport.
  1009. * @xprt: pointer to the transport
  1010. *
  1011. */
  1012. void xprt_put(struct rpc_xprt *xprt)
  1013. {
  1014. kref_put(&xprt->kref, xprt_destroy);
  1015. }
  1016. /**
  1017. * xprt_get - return a reference to an RPC transport.
  1018. * @xprt: pointer to the transport
  1019. *
  1020. */
  1021. struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
  1022. {
  1023. kref_get(&xprt->kref);
  1024. return xprt;
  1025. }