xprt.c 37 KB

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