xprt.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  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. /**
  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_on_wq(xprtiod_workqueue,
  472. &xprt->pending, xprt->snd_task);
  473. }
  474. spin_unlock_bh(&xprt->transport_lock);
  475. }
  476. EXPORT_SYMBOL_GPL(xprt_write_space);
  477. /**
  478. * xprt_set_retrans_timeout_def - set a request's retransmit timeout
  479. * @task: task whose timeout is to be set
  480. *
  481. * Set a request's retransmit timeout based on the transport's
  482. * default timeout parameters. Used by transports that don't adjust
  483. * the retransmit timeout based on round-trip time estimation.
  484. */
  485. void xprt_set_retrans_timeout_def(struct rpc_task *task)
  486. {
  487. task->tk_timeout = task->tk_rqstp->rq_timeout;
  488. }
  489. EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
  490. /**
  491. * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
  492. * @task: task whose timeout is to be set
  493. *
  494. * Set a request's retransmit timeout using the RTT estimator.
  495. */
  496. void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
  497. {
  498. int timer = task->tk_msg.rpc_proc->p_timer;
  499. struct rpc_clnt *clnt = task->tk_client;
  500. struct rpc_rtt *rtt = clnt->cl_rtt;
  501. struct rpc_rqst *req = task->tk_rqstp;
  502. unsigned long max_timeout = clnt->cl_timeout->to_maxval;
  503. task->tk_timeout = rpc_calc_rto(rtt, timer);
  504. task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
  505. if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
  506. task->tk_timeout = max_timeout;
  507. }
  508. EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
  509. static void xprt_reset_majortimeo(struct rpc_rqst *req)
  510. {
  511. const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
  512. req->rq_majortimeo = req->rq_timeout;
  513. if (to->to_exponential)
  514. req->rq_majortimeo <<= to->to_retries;
  515. else
  516. req->rq_majortimeo += to->to_increment * to->to_retries;
  517. if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
  518. req->rq_majortimeo = to->to_maxval;
  519. req->rq_majortimeo += jiffies;
  520. }
  521. /**
  522. * xprt_adjust_timeout - adjust timeout values for next retransmit
  523. * @req: RPC request containing parameters to use for the adjustment
  524. *
  525. */
  526. int xprt_adjust_timeout(struct rpc_rqst *req)
  527. {
  528. struct rpc_xprt *xprt = req->rq_xprt;
  529. const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
  530. int status = 0;
  531. if (time_before(jiffies, req->rq_majortimeo)) {
  532. if (to->to_exponential)
  533. req->rq_timeout <<= 1;
  534. else
  535. req->rq_timeout += to->to_increment;
  536. if (to->to_maxval && req->rq_timeout >= to->to_maxval)
  537. req->rq_timeout = to->to_maxval;
  538. req->rq_retries++;
  539. } else {
  540. req->rq_timeout = to->to_initval;
  541. req->rq_retries = 0;
  542. xprt_reset_majortimeo(req);
  543. /* Reset the RTT counters == "slow start" */
  544. spin_lock_bh(&xprt->transport_lock);
  545. rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
  546. spin_unlock_bh(&xprt->transport_lock);
  547. status = -ETIMEDOUT;
  548. }
  549. if (req->rq_timeout == 0) {
  550. printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
  551. req->rq_timeout = 5 * HZ;
  552. }
  553. return status;
  554. }
  555. static void xprt_autoclose(struct work_struct *work)
  556. {
  557. struct rpc_xprt *xprt =
  558. container_of(work, struct rpc_xprt, task_cleanup);
  559. clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
  560. xprt->ops->close(xprt);
  561. xprt_release_write(xprt, NULL);
  562. wake_up_bit(&xprt->state, XPRT_LOCKED);
  563. }
  564. /**
  565. * xprt_disconnect_done - mark a transport as disconnected
  566. * @xprt: transport to flag for disconnect
  567. *
  568. */
  569. void xprt_disconnect_done(struct rpc_xprt *xprt)
  570. {
  571. dprintk("RPC: disconnected transport %p\n", xprt);
  572. spin_lock_bh(&xprt->transport_lock);
  573. xprt_clear_connected(xprt);
  574. xprt_wake_pending_tasks(xprt, -EAGAIN);
  575. spin_unlock_bh(&xprt->transport_lock);
  576. }
  577. EXPORT_SYMBOL_GPL(xprt_disconnect_done);
  578. /**
  579. * xprt_force_disconnect - force a transport to disconnect
  580. * @xprt: transport to disconnect
  581. *
  582. */
  583. void xprt_force_disconnect(struct rpc_xprt *xprt)
  584. {
  585. /* Don't race with the test_bit() in xprt_clear_locked() */
  586. spin_lock_bh(&xprt->transport_lock);
  587. set_bit(XPRT_CLOSE_WAIT, &xprt->state);
  588. /* Try to schedule an autoclose RPC call */
  589. if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
  590. queue_work(xprtiod_workqueue, &xprt->task_cleanup);
  591. xprt_wake_pending_tasks(xprt, -EAGAIN);
  592. spin_unlock_bh(&xprt->transport_lock);
  593. }
  594. EXPORT_SYMBOL_GPL(xprt_force_disconnect);
  595. /**
  596. * xprt_conditional_disconnect - force a transport to disconnect
  597. * @xprt: transport to disconnect
  598. * @cookie: 'connection cookie'
  599. *
  600. * This attempts to break the connection if and only if 'cookie' matches
  601. * the current transport 'connection cookie'. It ensures that we don't
  602. * try to break the connection more than once when we need to retransmit
  603. * a batch of RPC requests.
  604. *
  605. */
  606. void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
  607. {
  608. /* Don't race with the test_bit() in xprt_clear_locked() */
  609. spin_lock_bh(&xprt->transport_lock);
  610. if (cookie != xprt->connect_cookie)
  611. goto out;
  612. if (test_bit(XPRT_CLOSING, &xprt->state))
  613. goto out;
  614. set_bit(XPRT_CLOSE_WAIT, &xprt->state);
  615. /* Try to schedule an autoclose RPC call */
  616. if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
  617. queue_work(xprtiod_workqueue, &xprt->task_cleanup);
  618. xprt_wake_pending_tasks(xprt, -EAGAIN);
  619. out:
  620. spin_unlock_bh(&xprt->transport_lock);
  621. }
  622. static bool
  623. xprt_has_timer(const struct rpc_xprt *xprt)
  624. {
  625. return xprt->idle_timeout != 0;
  626. }
  627. static void
  628. xprt_schedule_autodisconnect(struct rpc_xprt *xprt)
  629. __must_hold(&xprt->transport_lock)
  630. {
  631. if (list_empty(&xprt->recv) && xprt_has_timer(xprt))
  632. mod_timer(&xprt->timer, xprt->last_used + xprt->idle_timeout);
  633. }
  634. static void
  635. xprt_init_autodisconnect(struct timer_list *t)
  636. {
  637. struct rpc_xprt *xprt = from_timer(xprt, t, timer);
  638. spin_lock(&xprt->transport_lock);
  639. if (!list_empty(&xprt->recv))
  640. goto out_abort;
  641. /* Reset xprt->last_used to avoid connect/autodisconnect cycling */
  642. xprt->last_used = jiffies;
  643. if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
  644. goto out_abort;
  645. spin_unlock(&xprt->transport_lock);
  646. queue_work(xprtiod_workqueue, &xprt->task_cleanup);
  647. return;
  648. out_abort:
  649. spin_unlock(&xprt->transport_lock);
  650. }
  651. bool xprt_lock_connect(struct rpc_xprt *xprt,
  652. struct rpc_task *task,
  653. void *cookie)
  654. {
  655. bool ret = false;
  656. spin_lock_bh(&xprt->transport_lock);
  657. if (!test_bit(XPRT_LOCKED, &xprt->state))
  658. goto out;
  659. if (xprt->snd_task != task)
  660. goto out;
  661. xprt_task_clear_bytes_sent(task);
  662. xprt->snd_task = cookie;
  663. ret = true;
  664. out:
  665. spin_unlock_bh(&xprt->transport_lock);
  666. return ret;
  667. }
  668. void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
  669. {
  670. spin_lock_bh(&xprt->transport_lock);
  671. if (xprt->snd_task != cookie)
  672. goto out;
  673. if (!test_bit(XPRT_LOCKED, &xprt->state))
  674. goto out;
  675. xprt->snd_task =NULL;
  676. xprt->ops->release_xprt(xprt, NULL);
  677. xprt_schedule_autodisconnect(xprt);
  678. out:
  679. spin_unlock_bh(&xprt->transport_lock);
  680. wake_up_bit(&xprt->state, XPRT_LOCKED);
  681. }
  682. /**
  683. * xprt_connect - schedule a transport connect operation
  684. * @task: RPC task that is requesting the connect
  685. *
  686. */
  687. void xprt_connect(struct rpc_task *task)
  688. {
  689. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  690. dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
  691. xprt, (xprt_connected(xprt) ? "is" : "is not"));
  692. if (!xprt_bound(xprt)) {
  693. task->tk_status = -EAGAIN;
  694. return;
  695. }
  696. if (!xprt_lock_write(xprt, task))
  697. return;
  698. if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
  699. xprt->ops->close(xprt);
  700. if (!xprt_connected(xprt)) {
  701. task->tk_rqstp->rq_bytes_sent = 0;
  702. task->tk_timeout = task->tk_rqstp->rq_timeout;
  703. task->tk_rqstp->rq_connect_cookie = xprt->connect_cookie;
  704. rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
  705. if (test_bit(XPRT_CLOSING, &xprt->state))
  706. return;
  707. if (xprt_test_and_set_connecting(xprt))
  708. return;
  709. /* Race breaker */
  710. if (!xprt_connected(xprt)) {
  711. xprt->stat.connect_start = jiffies;
  712. xprt->ops->connect(xprt, task);
  713. } else {
  714. xprt_clear_connecting(xprt);
  715. task->tk_status = 0;
  716. rpc_wake_up_queued_task(&xprt->pending, task);
  717. }
  718. }
  719. xprt_release_write(xprt, task);
  720. }
  721. static void xprt_connect_status(struct rpc_task *task)
  722. {
  723. switch (task->tk_status) {
  724. case 0:
  725. dprintk("RPC: %5u xprt_connect_status: connection established\n",
  726. task->tk_pid);
  727. break;
  728. case -ECONNREFUSED:
  729. case -ECONNRESET:
  730. case -ECONNABORTED:
  731. case -ENETUNREACH:
  732. case -EHOSTUNREACH:
  733. case -EPIPE:
  734. case -EAGAIN:
  735. dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
  736. break;
  737. case -ETIMEDOUT:
  738. dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
  739. "out\n", task->tk_pid);
  740. break;
  741. default:
  742. dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
  743. "server %s\n", task->tk_pid, -task->tk_status,
  744. task->tk_rqstp->rq_xprt->servername);
  745. task->tk_status = -EIO;
  746. }
  747. }
  748. /**
  749. * xprt_lookup_rqst - find an RPC request corresponding to an XID
  750. * @xprt: transport on which the original request was transmitted
  751. * @xid: RPC XID of incoming reply
  752. *
  753. * Caller holds xprt->recv_lock.
  754. */
  755. struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
  756. {
  757. struct rpc_rqst *entry;
  758. list_for_each_entry(entry, &xprt->recv, rq_list)
  759. if (entry->rq_xid == xid) {
  760. trace_xprt_lookup_rqst(xprt, xid, 0);
  761. entry->rq_rtt = ktime_sub(ktime_get(), entry->rq_xtime);
  762. return entry;
  763. }
  764. dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
  765. ntohl(xid));
  766. trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
  767. xprt->stat.bad_xids++;
  768. return NULL;
  769. }
  770. EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
  771. /**
  772. * xprt_pin_rqst - Pin a request on the transport receive list
  773. * @req: Request to pin
  774. *
  775. * Caller must ensure this is atomic with the call to xprt_lookup_rqst()
  776. * so should be holding the xprt transport lock.
  777. */
  778. void xprt_pin_rqst(struct rpc_rqst *req)
  779. {
  780. set_bit(RPC_TASK_MSG_RECV, &req->rq_task->tk_runstate);
  781. }
  782. EXPORT_SYMBOL_GPL(xprt_pin_rqst);
  783. /**
  784. * xprt_unpin_rqst - Unpin a request on the transport receive list
  785. * @req: Request to pin
  786. *
  787. * Caller should be holding the xprt transport lock.
  788. */
  789. void xprt_unpin_rqst(struct rpc_rqst *req)
  790. {
  791. struct rpc_task *task = req->rq_task;
  792. clear_bit(RPC_TASK_MSG_RECV, &task->tk_runstate);
  793. if (test_bit(RPC_TASK_MSG_RECV_WAIT, &task->tk_runstate))
  794. wake_up_bit(&task->tk_runstate, RPC_TASK_MSG_RECV);
  795. }
  796. EXPORT_SYMBOL_GPL(xprt_unpin_rqst);
  797. static void xprt_wait_on_pinned_rqst(struct rpc_rqst *req)
  798. __must_hold(&req->rq_xprt->recv_lock)
  799. {
  800. struct rpc_task *task = req->rq_task;
  801. if (task && test_bit(RPC_TASK_MSG_RECV, &task->tk_runstate)) {
  802. spin_unlock(&req->rq_xprt->recv_lock);
  803. set_bit(RPC_TASK_MSG_RECV_WAIT, &task->tk_runstate);
  804. wait_on_bit(&task->tk_runstate, RPC_TASK_MSG_RECV,
  805. TASK_UNINTERRUPTIBLE);
  806. clear_bit(RPC_TASK_MSG_RECV_WAIT, &task->tk_runstate);
  807. spin_lock(&req->rq_xprt->recv_lock);
  808. }
  809. }
  810. /**
  811. * xprt_update_rtt - Update RPC RTT statistics
  812. * @task: RPC request that recently completed
  813. *
  814. * Caller holds xprt->recv_lock.
  815. */
  816. void xprt_update_rtt(struct rpc_task *task)
  817. {
  818. struct rpc_rqst *req = task->tk_rqstp;
  819. struct rpc_rtt *rtt = task->tk_client->cl_rtt;
  820. unsigned int timer = task->tk_msg.rpc_proc->p_timer;
  821. long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
  822. if (timer) {
  823. if (req->rq_ntrans == 1)
  824. rpc_update_rtt(rtt, timer, m);
  825. rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
  826. }
  827. }
  828. EXPORT_SYMBOL_GPL(xprt_update_rtt);
  829. /**
  830. * xprt_complete_rqst - called when reply processing is complete
  831. * @task: RPC request that recently completed
  832. * @copied: actual number of bytes received from the transport
  833. *
  834. * Caller holds xprt->recv_lock.
  835. */
  836. void xprt_complete_rqst(struct rpc_task *task, int copied)
  837. {
  838. struct rpc_rqst *req = task->tk_rqstp;
  839. struct rpc_xprt *xprt = req->rq_xprt;
  840. dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
  841. task->tk_pid, ntohl(req->rq_xid), copied);
  842. trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
  843. xprt->stat.recvs++;
  844. list_del_init(&req->rq_list);
  845. req->rq_private_buf.len = copied;
  846. /* Ensure all writes are done before we update */
  847. /* req->rq_reply_bytes_recvd */
  848. smp_wmb();
  849. req->rq_reply_bytes_recvd = copied;
  850. rpc_wake_up_queued_task(&xprt->pending, task);
  851. }
  852. EXPORT_SYMBOL_GPL(xprt_complete_rqst);
  853. static void xprt_timer(struct rpc_task *task)
  854. {
  855. struct rpc_rqst *req = task->tk_rqstp;
  856. struct rpc_xprt *xprt = req->rq_xprt;
  857. if (task->tk_status != -ETIMEDOUT)
  858. return;
  859. trace_xprt_timer(xprt, req->rq_xid, task->tk_status);
  860. if (!req->rq_reply_bytes_recvd) {
  861. if (xprt->ops->timer)
  862. xprt->ops->timer(xprt, task);
  863. } else
  864. task->tk_status = 0;
  865. }
  866. /**
  867. * xprt_prepare_transmit - reserve the transport before sending a request
  868. * @task: RPC task about to send a request
  869. *
  870. */
  871. bool xprt_prepare_transmit(struct rpc_task *task)
  872. {
  873. struct rpc_rqst *req = task->tk_rqstp;
  874. struct rpc_xprt *xprt = req->rq_xprt;
  875. bool ret = false;
  876. dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
  877. spin_lock_bh(&xprt->transport_lock);
  878. if (!req->rq_bytes_sent) {
  879. if (req->rq_reply_bytes_recvd) {
  880. task->tk_status = req->rq_reply_bytes_recvd;
  881. goto out_unlock;
  882. }
  883. if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
  884. && xprt_connected(xprt)
  885. && req->rq_connect_cookie == xprt->connect_cookie) {
  886. xprt->ops->set_retrans_timeout(task);
  887. rpc_sleep_on(&xprt->pending, task, xprt_timer);
  888. goto out_unlock;
  889. }
  890. }
  891. if (!xprt->ops->reserve_xprt(xprt, task)) {
  892. task->tk_status = -EAGAIN;
  893. goto out_unlock;
  894. }
  895. ret = true;
  896. out_unlock:
  897. spin_unlock_bh(&xprt->transport_lock);
  898. return ret;
  899. }
  900. void xprt_end_transmit(struct rpc_task *task)
  901. {
  902. xprt_release_write(task->tk_rqstp->rq_xprt, task);
  903. }
  904. /**
  905. * xprt_transmit - send an RPC request on a transport
  906. * @task: controlling RPC task
  907. *
  908. * We have to copy the iovec because sendmsg fiddles with its contents.
  909. */
  910. void xprt_transmit(struct rpc_task *task)
  911. {
  912. struct rpc_rqst *req = task->tk_rqstp;
  913. struct rpc_xprt *xprt = req->rq_xprt;
  914. unsigned int connect_cookie;
  915. int status;
  916. dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
  917. if (!req->rq_reply_bytes_recvd) {
  918. if (list_empty(&req->rq_list) && rpc_reply_expected(task)) {
  919. /*
  920. * Add to the list only if we're expecting a reply
  921. */
  922. /* Update the softirq receive buffer */
  923. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  924. sizeof(req->rq_private_buf));
  925. /* Add request to the receive list */
  926. spin_lock(&xprt->recv_lock);
  927. list_add_tail(&req->rq_list, &xprt->recv);
  928. spin_unlock(&xprt->recv_lock);
  929. xprt_reset_majortimeo(req);
  930. /* Turn off autodisconnect */
  931. del_singleshot_timer_sync(&xprt->timer);
  932. }
  933. } else if (!req->rq_bytes_sent)
  934. return;
  935. connect_cookie = xprt->connect_cookie;
  936. status = xprt->ops->send_request(task);
  937. trace_xprt_transmit(xprt, req->rq_xid, status);
  938. if (status != 0) {
  939. task->tk_status = status;
  940. return;
  941. }
  942. xprt_inject_disconnect(xprt);
  943. dprintk("RPC: %5u xmit complete\n", task->tk_pid);
  944. task->tk_flags |= RPC_TASK_SENT;
  945. spin_lock_bh(&xprt->transport_lock);
  946. xprt->ops->set_retrans_timeout(task);
  947. xprt->stat.sends++;
  948. xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
  949. xprt->stat.bklog_u += xprt->backlog.qlen;
  950. xprt->stat.sending_u += xprt->sending.qlen;
  951. xprt->stat.pending_u += xprt->pending.qlen;
  952. spin_unlock_bh(&xprt->transport_lock);
  953. req->rq_connect_cookie = connect_cookie;
  954. if (rpc_reply_expected(task) && !READ_ONCE(req->rq_reply_bytes_recvd)) {
  955. /*
  956. * Sleep on the pending queue if we're expecting a reply.
  957. * The spinlock ensures atomicity between the test of
  958. * req->rq_reply_bytes_recvd, and the call to rpc_sleep_on().
  959. */
  960. spin_lock(&xprt->recv_lock);
  961. if (!req->rq_reply_bytes_recvd) {
  962. rpc_sleep_on(&xprt->pending, task, xprt_timer);
  963. /*
  964. * Send an extra queue wakeup call if the
  965. * connection was dropped in case the call to
  966. * rpc_sleep_on() raced.
  967. */
  968. if (!xprt_connected(xprt))
  969. xprt_wake_pending_tasks(xprt, -ENOTCONN);
  970. }
  971. spin_unlock(&xprt->recv_lock);
  972. }
  973. }
  974. static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
  975. {
  976. set_bit(XPRT_CONGESTED, &xprt->state);
  977. rpc_sleep_on(&xprt->backlog, task, NULL);
  978. }
  979. static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
  980. {
  981. if (rpc_wake_up_next(&xprt->backlog) == NULL)
  982. clear_bit(XPRT_CONGESTED, &xprt->state);
  983. }
  984. static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
  985. {
  986. bool ret = false;
  987. if (!test_bit(XPRT_CONGESTED, &xprt->state))
  988. goto out;
  989. spin_lock(&xprt->reserve_lock);
  990. if (test_bit(XPRT_CONGESTED, &xprt->state)) {
  991. rpc_sleep_on(&xprt->backlog, task, NULL);
  992. ret = true;
  993. }
  994. spin_unlock(&xprt->reserve_lock);
  995. out:
  996. return ret;
  997. }
  998. static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt)
  999. {
  1000. struct rpc_rqst *req = ERR_PTR(-EAGAIN);
  1001. if (xprt->num_reqs >= xprt->max_reqs)
  1002. goto out;
  1003. ++xprt->num_reqs;
  1004. spin_unlock(&xprt->reserve_lock);
  1005. req = kzalloc(sizeof(struct rpc_rqst), GFP_NOFS);
  1006. spin_lock(&xprt->reserve_lock);
  1007. if (req != NULL)
  1008. goto out;
  1009. --xprt->num_reqs;
  1010. req = ERR_PTR(-ENOMEM);
  1011. out:
  1012. return req;
  1013. }
  1014. static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
  1015. {
  1016. if (xprt->num_reqs > xprt->min_reqs) {
  1017. --xprt->num_reqs;
  1018. kfree(req);
  1019. return true;
  1020. }
  1021. return false;
  1022. }
  1023. void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
  1024. {
  1025. struct rpc_rqst *req;
  1026. spin_lock(&xprt->reserve_lock);
  1027. if (!list_empty(&xprt->free)) {
  1028. req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
  1029. list_del(&req->rq_list);
  1030. goto out_init_req;
  1031. }
  1032. req = xprt_dynamic_alloc_slot(xprt);
  1033. if (!IS_ERR(req))
  1034. goto out_init_req;
  1035. switch (PTR_ERR(req)) {
  1036. case -ENOMEM:
  1037. dprintk("RPC: dynamic allocation of request slot "
  1038. "failed! Retrying\n");
  1039. task->tk_status = -ENOMEM;
  1040. break;
  1041. case -EAGAIN:
  1042. xprt_add_backlog(xprt, task);
  1043. dprintk("RPC: waiting for request slot\n");
  1044. /* fall through */
  1045. default:
  1046. task->tk_status = -EAGAIN;
  1047. }
  1048. spin_unlock(&xprt->reserve_lock);
  1049. return;
  1050. out_init_req:
  1051. xprt->stat.max_slots = max_t(unsigned int, xprt->stat.max_slots,
  1052. xprt->num_reqs);
  1053. spin_unlock(&xprt->reserve_lock);
  1054. task->tk_status = 0;
  1055. task->tk_rqstp = req;
  1056. }
  1057. EXPORT_SYMBOL_GPL(xprt_alloc_slot);
  1058. void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
  1059. {
  1060. /* Note: grabbing the xprt_lock_write() ensures that we throttle
  1061. * new slot allocation if the transport is congested (i.e. when
  1062. * reconnecting a stream transport or when out of socket write
  1063. * buffer space).
  1064. */
  1065. if (xprt_lock_write(xprt, task)) {
  1066. xprt_alloc_slot(xprt, task);
  1067. xprt_release_write(xprt, task);
  1068. }
  1069. }
  1070. EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot);
  1071. void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
  1072. {
  1073. spin_lock(&xprt->reserve_lock);
  1074. if (!xprt_dynamic_free_slot(xprt, req)) {
  1075. memset(req, 0, sizeof(*req)); /* mark unused */
  1076. list_add(&req->rq_list, &xprt->free);
  1077. }
  1078. xprt_wake_up_backlog(xprt);
  1079. spin_unlock(&xprt->reserve_lock);
  1080. }
  1081. EXPORT_SYMBOL_GPL(xprt_free_slot);
  1082. static void xprt_free_all_slots(struct rpc_xprt *xprt)
  1083. {
  1084. struct rpc_rqst *req;
  1085. while (!list_empty(&xprt->free)) {
  1086. req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
  1087. list_del(&req->rq_list);
  1088. kfree(req);
  1089. }
  1090. }
  1091. struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
  1092. unsigned int num_prealloc,
  1093. unsigned int max_alloc)
  1094. {
  1095. struct rpc_xprt *xprt;
  1096. struct rpc_rqst *req;
  1097. int i;
  1098. xprt = kzalloc(size, GFP_KERNEL);
  1099. if (xprt == NULL)
  1100. goto out;
  1101. xprt_init(xprt, net);
  1102. for (i = 0; i < num_prealloc; i++) {
  1103. req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
  1104. if (!req)
  1105. goto out_free;
  1106. list_add(&req->rq_list, &xprt->free);
  1107. }
  1108. if (max_alloc > num_prealloc)
  1109. xprt->max_reqs = max_alloc;
  1110. else
  1111. xprt->max_reqs = num_prealloc;
  1112. xprt->min_reqs = num_prealloc;
  1113. xprt->num_reqs = num_prealloc;
  1114. return xprt;
  1115. out_free:
  1116. xprt_free(xprt);
  1117. out:
  1118. return NULL;
  1119. }
  1120. EXPORT_SYMBOL_GPL(xprt_alloc);
  1121. void xprt_free(struct rpc_xprt *xprt)
  1122. {
  1123. put_net(xprt->xprt_net);
  1124. xprt_free_all_slots(xprt);
  1125. kfree_rcu(xprt, rcu);
  1126. }
  1127. EXPORT_SYMBOL_GPL(xprt_free);
  1128. static __be32
  1129. xprt_alloc_xid(struct rpc_xprt *xprt)
  1130. {
  1131. __be32 xid;
  1132. spin_lock(&xprt->reserve_lock);
  1133. xid = (__force __be32)xprt->xid++;
  1134. spin_unlock(&xprt->reserve_lock);
  1135. return xid;
  1136. }
  1137. static void
  1138. xprt_init_xid(struct rpc_xprt *xprt)
  1139. {
  1140. xprt->xid = prandom_u32();
  1141. }
  1142. static void
  1143. xprt_request_init(struct rpc_task *task)
  1144. {
  1145. struct rpc_xprt *xprt = task->tk_xprt;
  1146. struct rpc_rqst *req = task->tk_rqstp;
  1147. INIT_LIST_HEAD(&req->rq_list);
  1148. req->rq_timeout = task->tk_client->cl_timeout->to_initval;
  1149. req->rq_task = task;
  1150. req->rq_xprt = xprt;
  1151. req->rq_buffer = NULL;
  1152. req->rq_xid = xprt_alloc_xid(xprt);
  1153. req->rq_connect_cookie = xprt->connect_cookie - 1;
  1154. req->rq_bytes_sent = 0;
  1155. req->rq_snd_buf.len = 0;
  1156. req->rq_snd_buf.buflen = 0;
  1157. req->rq_rcv_buf.len = 0;
  1158. req->rq_rcv_buf.buflen = 0;
  1159. req->rq_release_snd_buf = NULL;
  1160. xprt_reset_majortimeo(req);
  1161. dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
  1162. req, ntohl(req->rq_xid));
  1163. }
  1164. static void
  1165. xprt_do_reserve(struct rpc_xprt *xprt, struct rpc_task *task)
  1166. {
  1167. xprt->ops->alloc_slot(xprt, task);
  1168. if (task->tk_rqstp != NULL)
  1169. xprt_request_init(task);
  1170. }
  1171. /**
  1172. * xprt_reserve - allocate an RPC request slot
  1173. * @task: RPC task requesting a slot allocation
  1174. *
  1175. * If the transport is marked as being congested, or if no more
  1176. * slots are available, place the task on the transport's
  1177. * backlog queue.
  1178. */
  1179. void xprt_reserve(struct rpc_task *task)
  1180. {
  1181. struct rpc_xprt *xprt = task->tk_xprt;
  1182. task->tk_status = 0;
  1183. if (task->tk_rqstp != NULL)
  1184. return;
  1185. task->tk_timeout = 0;
  1186. task->tk_status = -EAGAIN;
  1187. if (!xprt_throttle_congested(xprt, task))
  1188. xprt_do_reserve(xprt, task);
  1189. }
  1190. /**
  1191. * xprt_retry_reserve - allocate an RPC request slot
  1192. * @task: RPC task requesting a slot allocation
  1193. *
  1194. * If no more slots are available, place the task on the transport's
  1195. * backlog queue.
  1196. * Note that the only difference with xprt_reserve is that we now
  1197. * ignore the value of the XPRT_CONGESTED flag.
  1198. */
  1199. void xprt_retry_reserve(struct rpc_task *task)
  1200. {
  1201. struct rpc_xprt *xprt = task->tk_xprt;
  1202. task->tk_status = 0;
  1203. if (task->tk_rqstp != NULL)
  1204. return;
  1205. task->tk_timeout = 0;
  1206. task->tk_status = -EAGAIN;
  1207. xprt_do_reserve(xprt, task);
  1208. }
  1209. /**
  1210. * xprt_release - release an RPC request slot
  1211. * @task: task which is finished with the slot
  1212. *
  1213. */
  1214. void xprt_release(struct rpc_task *task)
  1215. {
  1216. struct rpc_xprt *xprt;
  1217. struct rpc_rqst *req = task->tk_rqstp;
  1218. if (req == NULL) {
  1219. if (task->tk_client) {
  1220. xprt = task->tk_xprt;
  1221. if (xprt->snd_task == task)
  1222. xprt_release_write(xprt, task);
  1223. }
  1224. return;
  1225. }
  1226. xprt = req->rq_xprt;
  1227. if (task->tk_ops->rpc_count_stats != NULL)
  1228. task->tk_ops->rpc_count_stats(task, task->tk_calldata);
  1229. else if (task->tk_client)
  1230. rpc_count_iostats(task, task->tk_client->cl_metrics);
  1231. spin_lock(&xprt->recv_lock);
  1232. if (!list_empty(&req->rq_list)) {
  1233. list_del_init(&req->rq_list);
  1234. xprt_wait_on_pinned_rqst(req);
  1235. }
  1236. spin_unlock(&xprt->recv_lock);
  1237. spin_lock_bh(&xprt->transport_lock);
  1238. xprt->ops->release_xprt(xprt, task);
  1239. if (xprt->ops->release_request)
  1240. xprt->ops->release_request(task);
  1241. xprt->last_used = jiffies;
  1242. xprt_schedule_autodisconnect(xprt);
  1243. spin_unlock_bh(&xprt->transport_lock);
  1244. if (req->rq_buffer)
  1245. xprt->ops->buf_free(task);
  1246. xprt_inject_disconnect(xprt);
  1247. if (req->rq_cred != NULL)
  1248. put_rpccred(req->rq_cred);
  1249. task->tk_rqstp = NULL;
  1250. if (req->rq_release_snd_buf)
  1251. req->rq_release_snd_buf(req);
  1252. dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
  1253. if (likely(!bc_prealloc(req)))
  1254. xprt->ops->free_slot(xprt, req);
  1255. else
  1256. xprt_free_bc_request(req);
  1257. }
  1258. static void xprt_init(struct rpc_xprt *xprt, struct net *net)
  1259. {
  1260. kref_init(&xprt->kref);
  1261. spin_lock_init(&xprt->transport_lock);
  1262. spin_lock_init(&xprt->reserve_lock);
  1263. spin_lock_init(&xprt->recv_lock);
  1264. INIT_LIST_HEAD(&xprt->free);
  1265. INIT_LIST_HEAD(&xprt->recv);
  1266. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1267. spin_lock_init(&xprt->bc_pa_lock);
  1268. INIT_LIST_HEAD(&xprt->bc_pa_list);
  1269. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1270. INIT_LIST_HEAD(&xprt->xprt_switch);
  1271. xprt->last_used = jiffies;
  1272. xprt->cwnd = RPC_INITCWND;
  1273. xprt->bind_index = 0;
  1274. rpc_init_wait_queue(&xprt->binding, "xprt_binding");
  1275. rpc_init_wait_queue(&xprt->pending, "xprt_pending");
  1276. rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending");
  1277. rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
  1278. xprt_init_xid(xprt);
  1279. xprt->xprt_net = get_net(net);
  1280. }
  1281. /**
  1282. * xprt_create_transport - create an RPC transport
  1283. * @args: rpc transport creation arguments
  1284. *
  1285. */
  1286. struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
  1287. {
  1288. struct rpc_xprt *xprt;
  1289. struct xprt_class *t;
  1290. spin_lock(&xprt_list_lock);
  1291. list_for_each_entry(t, &xprt_list, list) {
  1292. if (t->ident == args->ident) {
  1293. spin_unlock(&xprt_list_lock);
  1294. goto found;
  1295. }
  1296. }
  1297. spin_unlock(&xprt_list_lock);
  1298. dprintk("RPC: transport (%d) not supported\n", args->ident);
  1299. return ERR_PTR(-EIO);
  1300. found:
  1301. xprt = t->setup(args);
  1302. if (IS_ERR(xprt)) {
  1303. dprintk("RPC: xprt_create_transport: failed, %ld\n",
  1304. -PTR_ERR(xprt));
  1305. goto out;
  1306. }
  1307. if (args->flags & XPRT_CREATE_NO_IDLE_TIMEOUT)
  1308. xprt->idle_timeout = 0;
  1309. INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
  1310. if (xprt_has_timer(xprt))
  1311. timer_setup(&xprt->timer, xprt_init_autodisconnect, 0);
  1312. else
  1313. timer_setup(&xprt->timer, NULL, 0);
  1314. if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
  1315. xprt_destroy(xprt);
  1316. return ERR_PTR(-EINVAL);
  1317. }
  1318. xprt->servername = kstrdup(args->servername, GFP_KERNEL);
  1319. if (xprt->servername == NULL) {
  1320. xprt_destroy(xprt);
  1321. return ERR_PTR(-ENOMEM);
  1322. }
  1323. rpc_xprt_debugfs_register(xprt);
  1324. dprintk("RPC: created transport %p with %u slots\n", xprt,
  1325. xprt->max_reqs);
  1326. out:
  1327. return xprt;
  1328. }
  1329. static void xprt_destroy_cb(struct work_struct *work)
  1330. {
  1331. struct rpc_xprt *xprt =
  1332. container_of(work, struct rpc_xprt, task_cleanup);
  1333. rpc_xprt_debugfs_unregister(xprt);
  1334. rpc_destroy_wait_queue(&xprt->binding);
  1335. rpc_destroy_wait_queue(&xprt->pending);
  1336. rpc_destroy_wait_queue(&xprt->sending);
  1337. rpc_destroy_wait_queue(&xprt->backlog);
  1338. kfree(xprt->servername);
  1339. /*
  1340. * Tear down transport state and free the rpc_xprt
  1341. */
  1342. xprt->ops->destroy(xprt);
  1343. }
  1344. /**
  1345. * xprt_destroy - destroy an RPC transport, killing off all requests.
  1346. * @xprt: transport to destroy
  1347. *
  1348. */
  1349. static void xprt_destroy(struct rpc_xprt *xprt)
  1350. {
  1351. dprintk("RPC: destroying transport %p\n", xprt);
  1352. /*
  1353. * Exclude transport connect/disconnect handlers and autoclose
  1354. */
  1355. wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_UNINTERRUPTIBLE);
  1356. del_timer_sync(&xprt->timer);
  1357. /*
  1358. * Destroy sockets etc from the system workqueue so they can
  1359. * safely flush receive work running on rpciod.
  1360. */
  1361. INIT_WORK(&xprt->task_cleanup, xprt_destroy_cb);
  1362. schedule_work(&xprt->task_cleanup);
  1363. }
  1364. static void xprt_destroy_kref(struct kref *kref)
  1365. {
  1366. xprt_destroy(container_of(kref, struct rpc_xprt, kref));
  1367. }
  1368. /**
  1369. * xprt_get - return a reference to an RPC transport.
  1370. * @xprt: pointer to the transport
  1371. *
  1372. */
  1373. struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
  1374. {
  1375. if (xprt != NULL && kref_get_unless_zero(&xprt->kref))
  1376. return xprt;
  1377. return NULL;
  1378. }
  1379. EXPORT_SYMBOL_GPL(xprt_get);
  1380. /**
  1381. * xprt_put - release a reference to an RPC transport.
  1382. * @xprt: pointer to the transport
  1383. *
  1384. */
  1385. void xprt_put(struct rpc_xprt *xprt)
  1386. {
  1387. if (xprt != NULL)
  1388. kref_put(&xprt->kref, xprt_destroy_kref);
  1389. }
  1390. EXPORT_SYMBOL_GPL(xprt_put);