xprt.c 36 KB

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