xprt.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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_init(struct rpc_xprt *xprt, struct net *net);
  59. static void xprt_request_init(struct rpc_task *, struct rpc_xprt *);
  60. static void xprt_connect_status(struct rpc_task *task);
  61. static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *);
  62. static void xprt_destroy(struct rpc_xprt *xprt);
  63. static DEFINE_SPINLOCK(xprt_list_lock);
  64. static LIST_HEAD(xprt_list);
  65. /*
  66. * The transport code maintains an estimate on the maximum number of out-
  67. * standing RPC requests, using a smoothed version of the congestion
  68. * avoidance implemented in 44BSD. This is basically the Van Jacobson
  69. * congestion algorithm: If a retransmit occurs, the congestion window is
  70. * halved; otherwise, it is incremented by 1/cwnd when
  71. *
  72. * - a reply is received and
  73. * - a full number of requests are outstanding and
  74. * - the congestion window hasn't been updated recently.
  75. */
  76. #define RPC_CWNDSHIFT (8U)
  77. #define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT)
  78. #define RPC_INITCWND RPC_CWNDSCALE
  79. #define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT)
  80. #define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
  81. /**
  82. * xprt_register_transport - register a transport implementation
  83. * @transport: transport to register
  84. *
  85. * If a transport implementation is loaded as a kernel module, it can
  86. * call this interface to make itself known to the RPC client.
  87. *
  88. * Returns:
  89. * 0: transport successfully registered
  90. * -EEXIST: transport already registered
  91. * -EINVAL: transport module being unloaded
  92. */
  93. int xprt_register_transport(struct xprt_class *transport)
  94. {
  95. struct xprt_class *t;
  96. int result;
  97. result = -EEXIST;
  98. spin_lock(&xprt_list_lock);
  99. list_for_each_entry(t, &xprt_list, list) {
  100. /* don't register the same transport class twice */
  101. if (t->ident == transport->ident)
  102. goto out;
  103. }
  104. list_add_tail(&transport->list, &xprt_list);
  105. printk(KERN_INFO "RPC: Registered %s transport module.\n",
  106. transport->name);
  107. result = 0;
  108. out:
  109. spin_unlock(&xprt_list_lock);
  110. return result;
  111. }
  112. EXPORT_SYMBOL_GPL(xprt_register_transport);
  113. /**
  114. * xprt_unregister_transport - unregister a transport implementation
  115. * @transport: transport to unregister
  116. *
  117. * Returns:
  118. * 0: transport successfully unregistered
  119. * -ENOENT: transport never registered
  120. */
  121. int xprt_unregister_transport(struct xprt_class *transport)
  122. {
  123. struct xprt_class *t;
  124. int result;
  125. result = 0;
  126. spin_lock(&xprt_list_lock);
  127. list_for_each_entry(t, &xprt_list, list) {
  128. if (t == transport) {
  129. printk(KERN_INFO
  130. "RPC: Unregistered %s transport module.\n",
  131. transport->name);
  132. list_del_init(&transport->list);
  133. goto out;
  134. }
  135. }
  136. result = -ENOENT;
  137. out:
  138. spin_unlock(&xprt_list_lock);
  139. return result;
  140. }
  141. EXPORT_SYMBOL_GPL(xprt_unregister_transport);
  142. /**
  143. * xprt_load_transport - load a transport implementation
  144. * @transport_name: transport to load
  145. *
  146. * Returns:
  147. * 0: transport successfully loaded
  148. * -ENOENT: transport module not available
  149. */
  150. int xprt_load_transport(const char *transport_name)
  151. {
  152. struct xprt_class *t;
  153. int result;
  154. result = 0;
  155. spin_lock(&xprt_list_lock);
  156. list_for_each_entry(t, &xprt_list, list) {
  157. if (strcmp(t->name, transport_name) == 0) {
  158. spin_unlock(&xprt_list_lock);
  159. goto out;
  160. }
  161. }
  162. spin_unlock(&xprt_list_lock);
  163. result = request_module("xprt%s", transport_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. * @xprt: pointer to the target 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_xprt *xprt, struct rpc_task *task)
  178. {
  179. struct rpc_rqst *req = task->tk_rqstp;
  180. int priority;
  181. if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
  182. if (task == xprt->snd_task)
  183. return 1;
  184. goto out_sleep;
  185. }
  186. xprt->snd_task = task;
  187. if (req != NULL)
  188. req->rq_ntrans++;
  189. return 1;
  190. out_sleep:
  191. dprintk("RPC: %5u failed to lock transport %p\n",
  192. task->tk_pid, xprt);
  193. task->tk_timeout = 0;
  194. task->tk_status = -EAGAIN;
  195. if (req == NULL)
  196. priority = RPC_PRIORITY_LOW;
  197. else if (!req->rq_ntrans)
  198. priority = RPC_PRIORITY_NORMAL;
  199. else
  200. priority = RPC_PRIORITY_HIGH;
  201. rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
  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)) {
  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_xprt *xprt, struct rpc_task *task)
  224. {
  225. struct rpc_rqst *req = task->tk_rqstp;
  226. int priority;
  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 (req == NULL) {
  233. xprt->snd_task = task;
  234. return 1;
  235. }
  236. if (__xprt_get_cong(xprt, task)) {
  237. xprt->snd_task = task;
  238. req->rq_ntrans++;
  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 == NULL)
  247. priority = RPC_PRIORITY_LOW;
  248. else if (!req->rq_ntrans)
  249. priority = RPC_PRIORITY_NORMAL;
  250. else
  251. priority = RPC_PRIORITY_HIGH;
  252. rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
  253. return 0;
  254. }
  255. EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
  256. static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
  257. {
  258. int retval;
  259. spin_lock_bh(&xprt->transport_lock);
  260. retval = xprt->ops->reserve_xprt(xprt, task);
  261. spin_unlock_bh(&xprt->transport_lock);
  262. return retval;
  263. }
  264. static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
  265. {
  266. struct rpc_xprt *xprt = data;
  267. struct rpc_rqst *req;
  268. req = task->tk_rqstp;
  269. xprt->snd_task = task;
  270. if (req)
  271. req->rq_ntrans++;
  272. return true;
  273. }
  274. static void __xprt_lock_write_next(struct rpc_xprt *xprt)
  275. {
  276. if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
  277. return;
  278. if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_func, xprt))
  279. return;
  280. xprt_clear_locked(xprt);
  281. }
  282. static bool __xprt_lock_write_cong_func(struct rpc_task *task, void *data)
  283. {
  284. struct rpc_xprt *xprt = data;
  285. struct rpc_rqst *req;
  286. req = task->tk_rqstp;
  287. if (req == NULL) {
  288. xprt->snd_task = task;
  289. return true;
  290. }
  291. if (__xprt_get_cong(xprt, task)) {
  292. xprt->snd_task = task;
  293. req->rq_ntrans++;
  294. return true;
  295. }
  296. return false;
  297. }
  298. static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
  299. {
  300. if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
  301. return;
  302. if (RPCXPRT_CONGESTED(xprt))
  303. goto out_unlock;
  304. if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_cong_func, xprt))
  305. return;
  306. out_unlock:
  307. xprt_clear_locked(xprt);
  308. }
  309. /**
  310. * xprt_release_xprt - allow other requests to use a transport
  311. * @xprt: transport with other tasks potentially waiting
  312. * @task: task that is releasing access to the transport
  313. *
  314. * Note that "task" can be NULL. No congestion control is provided.
  315. */
  316. void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
  317. {
  318. if (xprt->snd_task == task) {
  319. if (task != NULL) {
  320. struct rpc_rqst *req = task->tk_rqstp;
  321. if (req != NULL)
  322. req->rq_bytes_sent = 0;
  323. }
  324. xprt_clear_locked(xprt);
  325. __xprt_lock_write_next(xprt);
  326. }
  327. }
  328. EXPORT_SYMBOL_GPL(xprt_release_xprt);
  329. /**
  330. * xprt_release_xprt_cong - allow other requests to use a transport
  331. * @xprt: transport with other tasks potentially waiting
  332. * @task: task that is releasing access to the transport
  333. *
  334. * Note that "task" can be NULL. Another task is awoken to use the
  335. * transport if the transport's congestion window allows it.
  336. */
  337. void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
  338. {
  339. if (xprt->snd_task == task) {
  340. if (task != NULL) {
  341. struct rpc_rqst *req = task->tk_rqstp;
  342. if (req != NULL)
  343. req->rq_bytes_sent = 0;
  344. }
  345. xprt_clear_locked(xprt);
  346. __xprt_lock_write_next_cong(xprt);
  347. }
  348. }
  349. EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
  350. static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
  351. {
  352. spin_lock_bh(&xprt->transport_lock);
  353. xprt->ops->release_xprt(xprt, task);
  354. spin_unlock_bh(&xprt->transport_lock);
  355. }
  356. /*
  357. * Van Jacobson congestion avoidance. Check if the congestion window
  358. * overflowed. Put the task to sleep if this is the case.
  359. */
  360. static int
  361. __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
  362. {
  363. struct rpc_rqst *req = task->tk_rqstp;
  364. if (req->rq_cong)
  365. return 1;
  366. dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
  367. task->tk_pid, xprt->cong, xprt->cwnd);
  368. if (RPCXPRT_CONGESTED(xprt))
  369. return 0;
  370. req->rq_cong = 1;
  371. xprt->cong += RPC_CWNDSCALE;
  372. return 1;
  373. }
  374. /*
  375. * Adjust the congestion window, and wake up the next task
  376. * that has been sleeping due to congestion
  377. */
  378. static void
  379. __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
  380. {
  381. if (!req->rq_cong)
  382. return;
  383. req->rq_cong = 0;
  384. xprt->cong -= RPC_CWNDSCALE;
  385. __xprt_lock_write_next_cong(xprt);
  386. }
  387. /**
  388. * xprt_release_rqst_cong - housekeeping when request is complete
  389. * @task: RPC request that recently completed
  390. *
  391. * Useful for transports that require congestion control.
  392. */
  393. void xprt_release_rqst_cong(struct rpc_task *task)
  394. {
  395. struct rpc_rqst *req = task->tk_rqstp;
  396. __xprt_put_cong(req->rq_xprt, req);
  397. }
  398. EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
  399. /**
  400. * xprt_adjust_cwnd - adjust transport congestion window
  401. * @xprt: pointer to xprt
  402. * @task: recently completed RPC request used to adjust window
  403. * @result: result code of completed RPC request
  404. *
  405. * We use a time-smoothed congestion estimator to avoid heavy oscillation.
  406. */
  407. void xprt_adjust_cwnd(struct rpc_xprt *xprt, struct rpc_task *task, int result)
  408. {
  409. struct rpc_rqst *req = task->tk_rqstp;
  410. unsigned long cwnd = xprt->cwnd;
  411. if (result >= 0 && cwnd <= xprt->cong) {
  412. /* The (cwnd >> 1) term makes sure
  413. * the result gets rounded properly. */
  414. cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
  415. if (cwnd > RPC_MAXCWND(xprt))
  416. cwnd = RPC_MAXCWND(xprt);
  417. __xprt_lock_write_next_cong(xprt);
  418. } else if (result == -ETIMEDOUT) {
  419. cwnd >>= 1;
  420. if (cwnd < RPC_CWNDSCALE)
  421. cwnd = RPC_CWNDSCALE;
  422. }
  423. dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
  424. xprt->cong, xprt->cwnd, cwnd);
  425. xprt->cwnd = cwnd;
  426. __xprt_put_cong(xprt, req);
  427. }
  428. EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
  429. /**
  430. * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
  431. * @xprt: transport with waiting tasks
  432. * @status: result code to plant in each task before waking it
  433. *
  434. */
  435. void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
  436. {
  437. if (status < 0)
  438. rpc_wake_up_status(&xprt->pending, status);
  439. else
  440. rpc_wake_up(&xprt->pending);
  441. }
  442. EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
  443. /**
  444. * xprt_wait_for_buffer_space - wait for transport output buffer to clear
  445. * @task: task to be put to sleep
  446. * @action: function pointer to be executed after wait
  447. *
  448. * Note that we only set the timer for the case of RPC_IS_SOFT(), since
  449. * we don't in general want to force a socket disconnection due to
  450. * an incomplete RPC call transmission.
  451. */
  452. void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
  453. {
  454. struct rpc_rqst *req = task->tk_rqstp;
  455. struct rpc_xprt *xprt = req->rq_xprt;
  456. task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
  457. rpc_sleep_on(&xprt->pending, task, action);
  458. }
  459. EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
  460. /**
  461. * xprt_write_space - wake the task waiting for transport output buffer space
  462. * @xprt: transport with waiting tasks
  463. *
  464. * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
  465. */
  466. void xprt_write_space(struct rpc_xprt *xprt)
  467. {
  468. spin_lock_bh(&xprt->transport_lock);
  469. if (xprt->snd_task) {
  470. dprintk("RPC: write space: waking waiting task on "
  471. "xprt %p\n", xprt);
  472. rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task);
  473. }
  474. spin_unlock_bh(&xprt->transport_lock);
  475. }
  476. EXPORT_SYMBOL_GPL(xprt_write_space);
  477. /**
  478. * xprt_set_retrans_timeout_def - set a request's retransmit timeout
  479. * @task: task whose timeout is to be set
  480. *
  481. * Set a request's retransmit timeout based on the transport's
  482. * default timeout parameters. Used by transports that don't adjust
  483. * the retransmit timeout based on round-trip time estimation.
  484. */
  485. void xprt_set_retrans_timeout_def(struct rpc_task *task)
  486. {
  487. task->tk_timeout = task->tk_rqstp->rq_timeout;
  488. }
  489. EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
  490. /**
  491. * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
  492. * @task: task whose timeout is to be set
  493. *
  494. * Set a request's retransmit timeout using the RTT estimator.
  495. */
  496. void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
  497. {
  498. int timer = task->tk_msg.rpc_proc->p_timer;
  499. struct rpc_clnt *clnt = task->tk_client;
  500. struct rpc_rtt *rtt = clnt->cl_rtt;
  501. struct rpc_rqst *req = task->tk_rqstp;
  502. unsigned long max_timeout = clnt->cl_timeout->to_maxval;
  503. task->tk_timeout = rpc_calc_rto(rtt, timer);
  504. task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
  505. if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
  506. task->tk_timeout = max_timeout;
  507. }
  508. EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
  509. static void xprt_reset_majortimeo(struct rpc_rqst *req)
  510. {
  511. const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
  512. req->rq_majortimeo = req->rq_timeout;
  513. if (to->to_exponential)
  514. req->rq_majortimeo <<= to->to_retries;
  515. else
  516. req->rq_majortimeo += to->to_increment * to->to_retries;
  517. if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
  518. req->rq_majortimeo = to->to_maxval;
  519. req->rq_majortimeo += jiffies;
  520. }
  521. /**
  522. * xprt_adjust_timeout - adjust timeout values for next retransmit
  523. * @req: RPC request containing parameters to use for the adjustment
  524. *
  525. */
  526. int xprt_adjust_timeout(struct rpc_rqst *req)
  527. {
  528. struct rpc_xprt *xprt = req->rq_xprt;
  529. const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
  530. int status = 0;
  531. if (time_before(jiffies, req->rq_majortimeo)) {
  532. if (to->to_exponential)
  533. req->rq_timeout <<= 1;
  534. else
  535. req->rq_timeout += to->to_increment;
  536. if (to->to_maxval && req->rq_timeout >= to->to_maxval)
  537. req->rq_timeout = to->to_maxval;
  538. req->rq_retries++;
  539. } else {
  540. req->rq_timeout = to->to_initval;
  541. req->rq_retries = 0;
  542. xprt_reset_majortimeo(req);
  543. /* Reset the RTT counters == "slow start" */
  544. spin_lock_bh(&xprt->transport_lock);
  545. rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
  546. spin_unlock_bh(&xprt->transport_lock);
  547. status = -ETIMEDOUT;
  548. }
  549. if (req->rq_timeout == 0) {
  550. printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
  551. req->rq_timeout = 5 * HZ;
  552. }
  553. return status;
  554. }
  555. static void xprt_autoclose(struct work_struct *work)
  556. {
  557. struct rpc_xprt *xprt =
  558. container_of(work, struct rpc_xprt, task_cleanup);
  559. xprt->ops->close(xprt);
  560. clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
  561. xprt_release_write(xprt, NULL);
  562. }
  563. /**
  564. * xprt_disconnect_done - mark a transport as disconnected
  565. * @xprt: transport to flag for disconnect
  566. *
  567. */
  568. void xprt_disconnect_done(struct rpc_xprt *xprt)
  569. {
  570. dprintk("RPC: disconnected transport %p\n", xprt);
  571. spin_lock_bh(&xprt->transport_lock);
  572. xprt_clear_connected(xprt);
  573. xprt_wake_pending_tasks(xprt, -EAGAIN);
  574. spin_unlock_bh(&xprt->transport_lock);
  575. }
  576. EXPORT_SYMBOL_GPL(xprt_disconnect_done);
  577. /**
  578. * xprt_force_disconnect - force a transport to disconnect
  579. * @xprt: transport to disconnect
  580. *
  581. */
  582. void xprt_force_disconnect(struct rpc_xprt *xprt)
  583. {
  584. /* Don't race with the test_bit() in xprt_clear_locked() */
  585. spin_lock_bh(&xprt->transport_lock);
  586. set_bit(XPRT_CLOSE_WAIT, &xprt->state);
  587. /* Try to schedule an autoclose RPC call */
  588. if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
  589. queue_work(rpciod_workqueue, &xprt->task_cleanup);
  590. xprt_wake_pending_tasks(xprt, -EAGAIN);
  591. spin_unlock_bh(&xprt->transport_lock);
  592. }
  593. /**
  594. * xprt_conditional_disconnect - force a transport to disconnect
  595. * @xprt: transport to disconnect
  596. * @cookie: 'connection cookie'
  597. *
  598. * This attempts to break the connection if and only if 'cookie' matches
  599. * the current transport 'connection cookie'. It ensures that we don't
  600. * try to break the connection more than once when we need to retransmit
  601. * a batch of RPC requests.
  602. *
  603. */
  604. void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
  605. {
  606. /* Don't race with the test_bit() in xprt_clear_locked() */
  607. spin_lock_bh(&xprt->transport_lock);
  608. if (cookie != xprt->connect_cookie)
  609. goto out;
  610. if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt))
  611. goto out;
  612. set_bit(XPRT_CLOSE_WAIT, &xprt->state);
  613. /* Try to schedule an autoclose RPC call */
  614. if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
  615. queue_work(rpciod_workqueue, &xprt->task_cleanup);
  616. xprt_wake_pending_tasks(xprt, -EAGAIN);
  617. out:
  618. spin_unlock_bh(&xprt->transport_lock);
  619. }
  620. static void
  621. xprt_init_autodisconnect(unsigned long data)
  622. {
  623. struct rpc_xprt *xprt = (struct rpc_xprt *)data;
  624. spin_lock(&xprt->transport_lock);
  625. if (!list_empty(&xprt->recv))
  626. goto out_abort;
  627. if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
  628. goto out_abort;
  629. spin_unlock(&xprt->transport_lock);
  630. set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
  631. queue_work(rpciod_workqueue, &xprt->task_cleanup);
  632. return;
  633. out_abort:
  634. spin_unlock(&xprt->transport_lock);
  635. }
  636. /**
  637. * xprt_connect - schedule a transport connect operation
  638. * @task: RPC task that is requesting the connect
  639. *
  640. */
  641. void xprt_connect(struct rpc_task *task)
  642. {
  643. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  644. dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
  645. xprt, (xprt_connected(xprt) ? "is" : "is not"));
  646. if (!xprt_bound(xprt)) {
  647. task->tk_status = -EAGAIN;
  648. return;
  649. }
  650. if (!xprt_lock_write(xprt, task))
  651. return;
  652. if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
  653. xprt->ops->close(xprt);
  654. if (xprt_connected(xprt))
  655. xprt_release_write(xprt, task);
  656. else {
  657. task->tk_rqstp->rq_bytes_sent = 0;
  658. task->tk_timeout = task->tk_rqstp->rq_timeout;
  659. rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
  660. if (test_bit(XPRT_CLOSING, &xprt->state))
  661. return;
  662. if (xprt_test_and_set_connecting(xprt))
  663. return;
  664. xprt->stat.connect_start = jiffies;
  665. xprt->ops->connect(xprt, task);
  666. }
  667. }
  668. static void xprt_connect_status(struct rpc_task *task)
  669. {
  670. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  671. if (task->tk_status == 0) {
  672. xprt->stat.connect_count++;
  673. xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
  674. dprintk("RPC: %5u xprt_connect_status: connection established\n",
  675. task->tk_pid);
  676. return;
  677. }
  678. switch (task->tk_status) {
  679. case -ECONNREFUSED:
  680. case -ECONNRESET:
  681. case -ECONNABORTED:
  682. case -ENETUNREACH:
  683. case -EHOSTUNREACH:
  684. case -EAGAIN:
  685. dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
  686. break;
  687. case -ETIMEDOUT:
  688. dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
  689. "out\n", task->tk_pid);
  690. break;
  691. default:
  692. dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
  693. "server %s\n", task->tk_pid, -task->tk_status,
  694. xprt->servername);
  695. xprt_release_write(xprt, task);
  696. task->tk_status = -EIO;
  697. }
  698. }
  699. /**
  700. * xprt_lookup_rqst - find an RPC request corresponding to an XID
  701. * @xprt: transport on which the original request was transmitted
  702. * @xid: RPC XID of incoming reply
  703. *
  704. */
  705. struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
  706. {
  707. struct rpc_rqst *entry;
  708. list_for_each_entry(entry, &xprt->recv, rq_list)
  709. if (entry->rq_xid == xid)
  710. return entry;
  711. dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
  712. ntohl(xid));
  713. xprt->stat.bad_xids++;
  714. return NULL;
  715. }
  716. EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
  717. static void xprt_update_rtt(struct rpc_task *task)
  718. {
  719. struct rpc_rqst *req = task->tk_rqstp;
  720. struct rpc_rtt *rtt = task->tk_client->cl_rtt;
  721. unsigned int timer = task->tk_msg.rpc_proc->p_timer;
  722. long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
  723. if (timer) {
  724. if (req->rq_ntrans == 1)
  725. rpc_update_rtt(rtt, timer, m);
  726. rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
  727. }
  728. }
  729. /**
  730. * xprt_complete_rqst - called when reply processing is complete
  731. * @task: RPC request that recently completed
  732. * @copied: actual number of bytes received from the transport
  733. *
  734. * Caller holds transport lock.
  735. */
  736. void xprt_complete_rqst(struct rpc_task *task, int copied)
  737. {
  738. struct rpc_rqst *req = task->tk_rqstp;
  739. struct rpc_xprt *xprt = req->rq_xprt;
  740. dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
  741. task->tk_pid, ntohl(req->rq_xid), copied);
  742. xprt->stat.recvs++;
  743. req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime);
  744. if (xprt->ops->timer != NULL)
  745. xprt_update_rtt(task);
  746. list_del_init(&req->rq_list);
  747. req->rq_private_buf.len = copied;
  748. /* Ensure all writes are done before we update */
  749. /* req->rq_reply_bytes_recvd */
  750. smp_wmb();
  751. req->rq_reply_bytes_recvd = copied;
  752. rpc_wake_up_queued_task(&xprt->pending, task);
  753. }
  754. EXPORT_SYMBOL_GPL(xprt_complete_rqst);
  755. static void xprt_timer(struct rpc_task *task)
  756. {
  757. struct rpc_rqst *req = task->tk_rqstp;
  758. struct rpc_xprt *xprt = req->rq_xprt;
  759. if (task->tk_status != -ETIMEDOUT)
  760. return;
  761. dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
  762. spin_lock_bh(&xprt->transport_lock);
  763. if (!req->rq_reply_bytes_recvd) {
  764. if (xprt->ops->timer)
  765. xprt->ops->timer(xprt, task);
  766. } else
  767. task->tk_status = 0;
  768. spin_unlock_bh(&xprt->transport_lock);
  769. }
  770. static inline int xprt_has_timer(struct rpc_xprt *xprt)
  771. {
  772. return xprt->idle_timeout != 0;
  773. }
  774. /**
  775. * xprt_prepare_transmit - reserve the transport before sending a request
  776. * @task: RPC task about to send a request
  777. *
  778. */
  779. bool xprt_prepare_transmit(struct rpc_task *task)
  780. {
  781. struct rpc_rqst *req = task->tk_rqstp;
  782. struct rpc_xprt *xprt = req->rq_xprt;
  783. bool ret = false;
  784. dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
  785. spin_lock_bh(&xprt->transport_lock);
  786. if (!req->rq_bytes_sent) {
  787. if (req->rq_reply_bytes_recvd) {
  788. task->tk_status = req->rq_reply_bytes_recvd;
  789. goto out_unlock;
  790. }
  791. if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
  792. && xprt_connected(xprt)
  793. && req->rq_connect_cookie == xprt->connect_cookie) {
  794. xprt->ops->set_retrans_timeout(task);
  795. rpc_sleep_on(&xprt->pending, task, xprt_timer);
  796. goto out_unlock;
  797. }
  798. }
  799. if (!xprt->ops->reserve_xprt(xprt, task)) {
  800. task->tk_status = -EAGAIN;
  801. goto out_unlock;
  802. }
  803. ret = true;
  804. out_unlock:
  805. spin_unlock_bh(&xprt->transport_lock);
  806. return ret;
  807. }
  808. void xprt_end_transmit(struct rpc_task *task)
  809. {
  810. xprt_release_write(task->tk_rqstp->rq_xprt, task);
  811. }
  812. /**
  813. * xprt_transmit - send an RPC request on a transport
  814. * @task: controlling RPC task
  815. *
  816. * We have to copy the iovec because sendmsg fiddles with its contents.
  817. */
  818. void xprt_transmit(struct rpc_task *task)
  819. {
  820. struct rpc_rqst *req = task->tk_rqstp;
  821. struct rpc_xprt *xprt = req->rq_xprt;
  822. int status, numreqs;
  823. dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
  824. if (!req->rq_reply_bytes_recvd) {
  825. if (list_empty(&req->rq_list) && rpc_reply_expected(task)) {
  826. /*
  827. * Add to the list only if we're expecting a reply
  828. */
  829. spin_lock_bh(&xprt->transport_lock);
  830. /* Update the softirq receive buffer */
  831. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  832. sizeof(req->rq_private_buf));
  833. /* Add request to the receive list */
  834. list_add_tail(&req->rq_list, &xprt->recv);
  835. spin_unlock_bh(&xprt->transport_lock);
  836. xprt_reset_majortimeo(req);
  837. /* Turn off autodisconnect */
  838. del_singleshot_timer_sync(&xprt->timer);
  839. }
  840. } else if (!req->rq_bytes_sent)
  841. return;
  842. req->rq_xtime = ktime_get();
  843. status = xprt->ops->send_request(task);
  844. if (status != 0) {
  845. task->tk_status = status;
  846. return;
  847. }
  848. dprintk("RPC: %5u xmit complete\n", task->tk_pid);
  849. task->tk_flags |= RPC_TASK_SENT;
  850. spin_lock_bh(&xprt->transport_lock);
  851. xprt->ops->set_retrans_timeout(task);
  852. numreqs = atomic_read(&xprt->num_reqs);
  853. if (numreqs > xprt->stat.max_slots)
  854. xprt->stat.max_slots = numreqs;
  855. xprt->stat.sends++;
  856. xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
  857. xprt->stat.bklog_u += xprt->backlog.qlen;
  858. xprt->stat.sending_u += xprt->sending.qlen;
  859. xprt->stat.pending_u += xprt->pending.qlen;
  860. /* Don't race with disconnect */
  861. if (!xprt_connected(xprt))
  862. task->tk_status = -ENOTCONN;
  863. else {
  864. /*
  865. * Sleep on the pending queue since
  866. * we're expecting a reply.
  867. */
  868. if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task))
  869. rpc_sleep_on(&xprt->pending, task, xprt_timer);
  870. req->rq_connect_cookie = xprt->connect_cookie;
  871. }
  872. spin_unlock_bh(&xprt->transport_lock);
  873. }
  874. static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
  875. {
  876. set_bit(XPRT_CONGESTED, &xprt->state);
  877. rpc_sleep_on(&xprt->backlog, task, NULL);
  878. }
  879. static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
  880. {
  881. if (rpc_wake_up_next(&xprt->backlog) == NULL)
  882. clear_bit(XPRT_CONGESTED, &xprt->state);
  883. }
  884. static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
  885. {
  886. bool ret = false;
  887. if (!test_bit(XPRT_CONGESTED, &xprt->state))
  888. goto out;
  889. spin_lock(&xprt->reserve_lock);
  890. if (test_bit(XPRT_CONGESTED, &xprt->state)) {
  891. rpc_sleep_on(&xprt->backlog, task, NULL);
  892. ret = true;
  893. }
  894. spin_unlock(&xprt->reserve_lock);
  895. out:
  896. return ret;
  897. }
  898. static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt, gfp_t gfp_flags)
  899. {
  900. struct rpc_rqst *req = ERR_PTR(-EAGAIN);
  901. if (!atomic_add_unless(&xprt->num_reqs, 1, xprt->max_reqs))
  902. goto out;
  903. req = kzalloc(sizeof(struct rpc_rqst), gfp_flags);
  904. if (req != NULL)
  905. goto out;
  906. atomic_dec(&xprt->num_reqs);
  907. req = ERR_PTR(-ENOMEM);
  908. out:
  909. return req;
  910. }
  911. static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
  912. {
  913. if (atomic_add_unless(&xprt->num_reqs, -1, xprt->min_reqs)) {
  914. kfree(req);
  915. return true;
  916. }
  917. return false;
  918. }
  919. void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
  920. {
  921. struct rpc_rqst *req;
  922. spin_lock(&xprt->reserve_lock);
  923. if (!list_empty(&xprt->free)) {
  924. req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
  925. list_del(&req->rq_list);
  926. goto out_init_req;
  927. }
  928. req = xprt_dynamic_alloc_slot(xprt, GFP_NOWAIT|__GFP_NOWARN);
  929. if (!IS_ERR(req))
  930. goto out_init_req;
  931. switch (PTR_ERR(req)) {
  932. case -ENOMEM:
  933. dprintk("RPC: dynamic allocation of request slot "
  934. "failed! Retrying\n");
  935. task->tk_status = -ENOMEM;
  936. break;
  937. case -EAGAIN:
  938. xprt_add_backlog(xprt, task);
  939. dprintk("RPC: waiting for request slot\n");
  940. default:
  941. task->tk_status = -EAGAIN;
  942. }
  943. spin_unlock(&xprt->reserve_lock);
  944. return;
  945. out_init_req:
  946. task->tk_status = 0;
  947. task->tk_rqstp = req;
  948. xprt_request_init(task, xprt);
  949. spin_unlock(&xprt->reserve_lock);
  950. }
  951. EXPORT_SYMBOL_GPL(xprt_alloc_slot);
  952. void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
  953. {
  954. /* Note: grabbing the xprt_lock_write() ensures that we throttle
  955. * new slot allocation if the transport is congested (i.e. when
  956. * reconnecting a stream transport or when out of socket write
  957. * buffer space).
  958. */
  959. if (xprt_lock_write(xprt, task)) {
  960. xprt_alloc_slot(xprt, task);
  961. xprt_release_write(xprt, task);
  962. }
  963. }
  964. EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot);
  965. static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
  966. {
  967. spin_lock(&xprt->reserve_lock);
  968. if (!xprt_dynamic_free_slot(xprt, req)) {
  969. memset(req, 0, sizeof(*req)); /* mark unused */
  970. list_add(&req->rq_list, &xprt->free);
  971. }
  972. xprt_wake_up_backlog(xprt);
  973. spin_unlock(&xprt->reserve_lock);
  974. }
  975. static void xprt_free_all_slots(struct rpc_xprt *xprt)
  976. {
  977. struct rpc_rqst *req;
  978. while (!list_empty(&xprt->free)) {
  979. req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
  980. list_del(&req->rq_list);
  981. kfree(req);
  982. }
  983. }
  984. struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
  985. unsigned int num_prealloc,
  986. unsigned int max_alloc)
  987. {
  988. struct rpc_xprt *xprt;
  989. struct rpc_rqst *req;
  990. int i;
  991. xprt = kzalloc(size, GFP_KERNEL);
  992. if (xprt == NULL)
  993. goto out;
  994. xprt_init(xprt, net);
  995. for (i = 0; i < num_prealloc; i++) {
  996. req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
  997. if (!req)
  998. goto out_free;
  999. list_add(&req->rq_list, &xprt->free);
  1000. }
  1001. if (max_alloc > num_prealloc)
  1002. xprt->max_reqs = max_alloc;
  1003. else
  1004. xprt->max_reqs = num_prealloc;
  1005. xprt->min_reqs = num_prealloc;
  1006. atomic_set(&xprt->num_reqs, num_prealloc);
  1007. return xprt;
  1008. out_free:
  1009. xprt_free(xprt);
  1010. out:
  1011. return NULL;
  1012. }
  1013. EXPORT_SYMBOL_GPL(xprt_alloc);
  1014. void xprt_free(struct rpc_xprt *xprt)
  1015. {
  1016. put_net(xprt->xprt_net);
  1017. xprt_free_all_slots(xprt);
  1018. kfree(xprt);
  1019. }
  1020. EXPORT_SYMBOL_GPL(xprt_free);
  1021. /**
  1022. * xprt_reserve - allocate an RPC request slot
  1023. * @task: RPC task requesting a slot allocation
  1024. *
  1025. * If the transport is marked as being congested, or if no more
  1026. * slots are available, place the task on the transport's
  1027. * backlog queue.
  1028. */
  1029. void xprt_reserve(struct rpc_task *task)
  1030. {
  1031. struct rpc_xprt *xprt;
  1032. task->tk_status = 0;
  1033. if (task->tk_rqstp != NULL)
  1034. return;
  1035. task->tk_timeout = 0;
  1036. task->tk_status = -EAGAIN;
  1037. rcu_read_lock();
  1038. xprt = rcu_dereference(task->tk_client->cl_xprt);
  1039. if (!xprt_throttle_congested(xprt, task))
  1040. xprt->ops->alloc_slot(xprt, task);
  1041. rcu_read_unlock();
  1042. }
  1043. /**
  1044. * xprt_retry_reserve - allocate an RPC request slot
  1045. * @task: RPC task requesting a slot allocation
  1046. *
  1047. * If no more slots are available, place the task on the transport's
  1048. * backlog queue.
  1049. * Note that the only difference with xprt_reserve is that we now
  1050. * ignore the value of the XPRT_CONGESTED flag.
  1051. */
  1052. void xprt_retry_reserve(struct rpc_task *task)
  1053. {
  1054. struct rpc_xprt *xprt;
  1055. task->tk_status = 0;
  1056. if (task->tk_rqstp != NULL)
  1057. return;
  1058. task->tk_timeout = 0;
  1059. task->tk_status = -EAGAIN;
  1060. rcu_read_lock();
  1061. xprt = rcu_dereference(task->tk_client->cl_xprt);
  1062. xprt->ops->alloc_slot(xprt, task);
  1063. rcu_read_unlock();
  1064. }
  1065. static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
  1066. {
  1067. return (__force __be32)xprt->xid++;
  1068. }
  1069. static inline void xprt_init_xid(struct rpc_xprt *xprt)
  1070. {
  1071. xprt->xid = prandom_u32();
  1072. }
  1073. static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
  1074. {
  1075. struct rpc_rqst *req = task->tk_rqstp;
  1076. INIT_LIST_HEAD(&req->rq_list);
  1077. req->rq_timeout = task->tk_client->cl_timeout->to_initval;
  1078. req->rq_task = task;
  1079. req->rq_xprt = xprt;
  1080. req->rq_buffer = NULL;
  1081. req->rq_xid = xprt_alloc_xid(xprt);
  1082. req->rq_connect_cookie = xprt->connect_cookie - 1;
  1083. req->rq_bytes_sent = 0;
  1084. req->rq_snd_buf.len = 0;
  1085. req->rq_snd_buf.buflen = 0;
  1086. req->rq_rcv_buf.len = 0;
  1087. req->rq_rcv_buf.buflen = 0;
  1088. req->rq_release_snd_buf = NULL;
  1089. xprt_reset_majortimeo(req);
  1090. dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
  1091. req, ntohl(req->rq_xid));
  1092. }
  1093. /**
  1094. * xprt_release - release an RPC request slot
  1095. * @task: task which is finished with the slot
  1096. *
  1097. */
  1098. void xprt_release(struct rpc_task *task)
  1099. {
  1100. struct rpc_xprt *xprt;
  1101. struct rpc_rqst *req = task->tk_rqstp;
  1102. if (req == NULL) {
  1103. if (task->tk_client) {
  1104. rcu_read_lock();
  1105. xprt = rcu_dereference(task->tk_client->cl_xprt);
  1106. if (xprt->snd_task == task)
  1107. xprt_release_write(xprt, task);
  1108. rcu_read_unlock();
  1109. }
  1110. return;
  1111. }
  1112. xprt = req->rq_xprt;
  1113. if (task->tk_ops->rpc_count_stats != NULL)
  1114. task->tk_ops->rpc_count_stats(task, task->tk_calldata);
  1115. else if (task->tk_client)
  1116. rpc_count_iostats(task, task->tk_client->cl_metrics);
  1117. spin_lock_bh(&xprt->transport_lock);
  1118. xprt->ops->release_xprt(xprt, task);
  1119. if (xprt->ops->release_request)
  1120. xprt->ops->release_request(task);
  1121. if (!list_empty(&req->rq_list))
  1122. list_del(&req->rq_list);
  1123. xprt->last_used = jiffies;
  1124. if (list_empty(&xprt->recv) && xprt_has_timer(xprt))
  1125. mod_timer(&xprt->timer,
  1126. xprt->last_used + xprt->idle_timeout);
  1127. spin_unlock_bh(&xprt->transport_lock);
  1128. if (req->rq_buffer)
  1129. xprt->ops->buf_free(req->rq_buffer);
  1130. if (req->rq_cred != NULL)
  1131. put_rpccred(req->rq_cred);
  1132. task->tk_rqstp = NULL;
  1133. if (req->rq_release_snd_buf)
  1134. req->rq_release_snd_buf(req);
  1135. dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
  1136. if (likely(!bc_prealloc(req)))
  1137. xprt_free_slot(xprt, req);
  1138. else
  1139. xprt_free_bc_request(req);
  1140. }
  1141. static void xprt_init(struct rpc_xprt *xprt, struct net *net)
  1142. {
  1143. atomic_set(&xprt->count, 1);
  1144. spin_lock_init(&xprt->transport_lock);
  1145. spin_lock_init(&xprt->reserve_lock);
  1146. INIT_LIST_HEAD(&xprt->free);
  1147. INIT_LIST_HEAD(&xprt->recv);
  1148. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1149. spin_lock_init(&xprt->bc_pa_lock);
  1150. INIT_LIST_HEAD(&xprt->bc_pa_list);
  1151. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1152. xprt->last_used = jiffies;
  1153. xprt->cwnd = RPC_INITCWND;
  1154. xprt->bind_index = 0;
  1155. rpc_init_wait_queue(&xprt->binding, "xprt_binding");
  1156. rpc_init_wait_queue(&xprt->pending, "xprt_pending");
  1157. rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending");
  1158. rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
  1159. xprt_init_xid(xprt);
  1160. xprt->xprt_net = get_net(net);
  1161. }
  1162. /**
  1163. * xprt_create_transport - create an RPC transport
  1164. * @args: rpc transport creation arguments
  1165. *
  1166. */
  1167. struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
  1168. {
  1169. struct rpc_xprt *xprt;
  1170. struct xprt_class *t;
  1171. spin_lock(&xprt_list_lock);
  1172. list_for_each_entry(t, &xprt_list, list) {
  1173. if (t->ident == args->ident) {
  1174. spin_unlock(&xprt_list_lock);
  1175. goto found;
  1176. }
  1177. }
  1178. spin_unlock(&xprt_list_lock);
  1179. printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident);
  1180. return ERR_PTR(-EIO);
  1181. found:
  1182. xprt = t->setup(args);
  1183. if (IS_ERR(xprt)) {
  1184. dprintk("RPC: xprt_create_transport: failed, %ld\n",
  1185. -PTR_ERR(xprt));
  1186. goto out;
  1187. }
  1188. if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
  1189. xprt->idle_timeout = 0;
  1190. INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
  1191. if (xprt_has_timer(xprt))
  1192. setup_timer(&xprt->timer, xprt_init_autodisconnect,
  1193. (unsigned long)xprt);
  1194. else
  1195. init_timer(&xprt->timer);
  1196. if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
  1197. xprt_destroy(xprt);
  1198. return ERR_PTR(-EINVAL);
  1199. }
  1200. xprt->servername = kstrdup(args->servername, GFP_KERNEL);
  1201. if (xprt->servername == NULL) {
  1202. xprt_destroy(xprt);
  1203. return ERR_PTR(-ENOMEM);
  1204. }
  1205. dprintk("RPC: created transport %p with %u slots\n", xprt,
  1206. xprt->max_reqs);
  1207. out:
  1208. return xprt;
  1209. }
  1210. /**
  1211. * xprt_destroy - destroy an RPC transport, killing off all requests.
  1212. * @xprt: transport to destroy
  1213. *
  1214. */
  1215. static void xprt_destroy(struct rpc_xprt *xprt)
  1216. {
  1217. dprintk("RPC: destroying transport %p\n", xprt);
  1218. del_timer_sync(&xprt->timer);
  1219. rpc_destroy_wait_queue(&xprt->binding);
  1220. rpc_destroy_wait_queue(&xprt->pending);
  1221. rpc_destroy_wait_queue(&xprt->sending);
  1222. rpc_destroy_wait_queue(&xprt->backlog);
  1223. cancel_work_sync(&xprt->task_cleanup);
  1224. kfree(xprt->servername);
  1225. /*
  1226. * Tear down transport state and free the rpc_xprt
  1227. */
  1228. xprt->ops->destroy(xprt);
  1229. }
  1230. /**
  1231. * xprt_put - release a reference to an RPC transport.
  1232. * @xprt: pointer to the transport
  1233. *
  1234. */
  1235. void xprt_put(struct rpc_xprt *xprt)
  1236. {
  1237. if (atomic_dec_and_test(&xprt->count))
  1238. xprt_destroy(xprt);
  1239. }
  1240. /**
  1241. * xprt_get - return a reference to an RPC transport.
  1242. * @xprt: pointer to the transport
  1243. *
  1244. */
  1245. struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
  1246. {
  1247. if (atomic_inc_not_zero(&xprt->count))
  1248. return xprt;
  1249. return NULL;
  1250. }