xprt.c 36 KB

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