xprt.c 37 KB

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