xprt.c 41 KB

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