xprt.c 40 KB

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