xprt.c 31 KB

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