xprt.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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. /**
  295. * xprt_release_xprt - allow other requests to use a transport
  296. * @xprt: transport with other tasks potentially waiting
  297. * @task: task that is releasing access to the transport
  298. *
  299. * Note that "task" can be NULL. No congestion control is provided.
  300. */
  301. void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
  302. {
  303. if (xprt->snd_task == task) {
  304. if (task != NULL) {
  305. struct rpc_rqst *req = task->tk_rqstp;
  306. if (req != NULL)
  307. req->rq_bytes_sent = 0;
  308. }
  309. xprt_clear_locked(xprt);
  310. __xprt_lock_write_next(xprt);
  311. }
  312. }
  313. EXPORT_SYMBOL_GPL(xprt_release_xprt);
  314. /**
  315. * xprt_release_xprt_cong - allow other requests to use a transport
  316. * @xprt: transport with other tasks potentially waiting
  317. * @task: task that is releasing access to the transport
  318. *
  319. * Note that "task" can be NULL. Another task is awoken to use the
  320. * transport if the transport's congestion window allows it.
  321. */
  322. void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
  323. {
  324. if (xprt->snd_task == task) {
  325. if (task != NULL) {
  326. struct rpc_rqst *req = task->tk_rqstp;
  327. if (req != NULL)
  328. req->rq_bytes_sent = 0;
  329. }
  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->snd_task = cookie;
  639. ret = true;
  640. out:
  641. spin_unlock_bh(&xprt->transport_lock);
  642. return ret;
  643. }
  644. void xprt_unlock_connect(struct rpc_xprt *xprt, void *cookie)
  645. {
  646. spin_lock_bh(&xprt->transport_lock);
  647. if (xprt->snd_task != cookie)
  648. goto out;
  649. if (!test_bit(XPRT_LOCKED, &xprt->state))
  650. goto out;
  651. xprt->snd_task =NULL;
  652. xprt->ops->release_xprt(xprt, NULL);
  653. out:
  654. spin_unlock_bh(&xprt->transport_lock);
  655. }
  656. /**
  657. * xprt_connect - schedule a transport connect operation
  658. * @task: RPC task that is requesting the connect
  659. *
  660. */
  661. void xprt_connect(struct rpc_task *task)
  662. {
  663. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  664. dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
  665. xprt, (xprt_connected(xprt) ? "is" : "is not"));
  666. if (!xprt_bound(xprt)) {
  667. task->tk_status = -EAGAIN;
  668. return;
  669. }
  670. if (!xprt_lock_write(xprt, task))
  671. return;
  672. if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
  673. xprt->ops->close(xprt);
  674. if (!xprt_connected(xprt)) {
  675. task->tk_rqstp->rq_bytes_sent = 0;
  676. task->tk_timeout = task->tk_rqstp->rq_timeout;
  677. rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
  678. if (test_bit(XPRT_CLOSING, &xprt->state))
  679. return;
  680. if (xprt_test_and_set_connecting(xprt))
  681. return;
  682. xprt->stat.connect_start = jiffies;
  683. xprt->ops->connect(xprt, task);
  684. }
  685. xprt_release_write(xprt, task);
  686. }
  687. static void xprt_connect_status(struct rpc_task *task)
  688. {
  689. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  690. if (task->tk_status == 0) {
  691. xprt->stat.connect_count++;
  692. xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
  693. dprintk("RPC: %5u xprt_connect_status: connection established\n",
  694. task->tk_pid);
  695. return;
  696. }
  697. switch (task->tk_status) {
  698. case -ECONNREFUSED:
  699. case -ECONNRESET:
  700. case -ECONNABORTED:
  701. case -ENETUNREACH:
  702. case -EHOSTUNREACH:
  703. case -EPIPE:
  704. case -EAGAIN:
  705. dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
  706. break;
  707. case -ETIMEDOUT:
  708. dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
  709. "out\n", task->tk_pid);
  710. break;
  711. default:
  712. dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
  713. "server %s\n", task->tk_pid, -task->tk_status,
  714. xprt->servername);
  715. task->tk_status = -EIO;
  716. }
  717. }
  718. /**
  719. * xprt_lookup_rqst - find an RPC request corresponding to an XID
  720. * @xprt: transport on which the original request was transmitted
  721. * @xid: RPC XID of incoming reply
  722. *
  723. */
  724. struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
  725. {
  726. struct rpc_rqst *entry;
  727. list_for_each_entry(entry, &xprt->recv, rq_list)
  728. if (entry->rq_xid == xid) {
  729. trace_xprt_lookup_rqst(xprt, xid, 0);
  730. return entry;
  731. }
  732. dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
  733. ntohl(xid));
  734. trace_xprt_lookup_rqst(xprt, xid, -ENOENT);
  735. xprt->stat.bad_xids++;
  736. return NULL;
  737. }
  738. EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
  739. static void xprt_update_rtt(struct rpc_task *task)
  740. {
  741. struct rpc_rqst *req = task->tk_rqstp;
  742. struct rpc_rtt *rtt = task->tk_client->cl_rtt;
  743. unsigned int timer = task->tk_msg.rpc_proc->p_timer;
  744. long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
  745. if (timer) {
  746. if (req->rq_ntrans == 1)
  747. rpc_update_rtt(rtt, timer, m);
  748. rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
  749. }
  750. }
  751. /**
  752. * xprt_complete_rqst - called when reply processing is complete
  753. * @task: RPC request that recently completed
  754. * @copied: actual number of bytes received from the transport
  755. *
  756. * Caller holds transport lock.
  757. */
  758. void xprt_complete_rqst(struct rpc_task *task, int copied)
  759. {
  760. struct rpc_rqst *req = task->tk_rqstp;
  761. struct rpc_xprt *xprt = req->rq_xprt;
  762. dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
  763. task->tk_pid, ntohl(req->rq_xid), copied);
  764. trace_xprt_complete_rqst(xprt, req->rq_xid, copied);
  765. xprt->stat.recvs++;
  766. req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime);
  767. if (xprt->ops->timer != NULL)
  768. xprt_update_rtt(task);
  769. list_del_init(&req->rq_list);
  770. req->rq_private_buf.len = copied;
  771. /* Ensure all writes are done before we update */
  772. /* req->rq_reply_bytes_recvd */
  773. smp_wmb();
  774. req->rq_reply_bytes_recvd = copied;
  775. rpc_wake_up_queued_task(&xprt->pending, task);
  776. }
  777. EXPORT_SYMBOL_GPL(xprt_complete_rqst);
  778. static void xprt_timer(struct rpc_task *task)
  779. {
  780. struct rpc_rqst *req = task->tk_rqstp;
  781. struct rpc_xprt *xprt = req->rq_xprt;
  782. if (task->tk_status != -ETIMEDOUT)
  783. return;
  784. dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
  785. spin_lock_bh(&xprt->transport_lock);
  786. if (!req->rq_reply_bytes_recvd) {
  787. if (xprt->ops->timer)
  788. xprt->ops->timer(xprt, task);
  789. } else
  790. task->tk_status = 0;
  791. spin_unlock_bh(&xprt->transport_lock);
  792. }
  793. static inline int xprt_has_timer(struct rpc_xprt *xprt)
  794. {
  795. return xprt->idle_timeout != 0;
  796. }
  797. /**
  798. * xprt_prepare_transmit - reserve the transport before sending a request
  799. * @task: RPC task about to send a request
  800. *
  801. */
  802. bool xprt_prepare_transmit(struct rpc_task *task)
  803. {
  804. struct rpc_rqst *req = task->tk_rqstp;
  805. struct rpc_xprt *xprt = req->rq_xprt;
  806. bool ret = false;
  807. dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
  808. spin_lock_bh(&xprt->transport_lock);
  809. if (!req->rq_bytes_sent) {
  810. if (req->rq_reply_bytes_recvd) {
  811. task->tk_status = req->rq_reply_bytes_recvd;
  812. goto out_unlock;
  813. }
  814. if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
  815. && xprt_connected(xprt)
  816. && req->rq_connect_cookie == xprt->connect_cookie) {
  817. xprt->ops->set_retrans_timeout(task);
  818. rpc_sleep_on(&xprt->pending, task, xprt_timer);
  819. goto out_unlock;
  820. }
  821. }
  822. if (!xprt->ops->reserve_xprt(xprt, task)) {
  823. task->tk_status = -EAGAIN;
  824. goto out_unlock;
  825. }
  826. ret = true;
  827. out_unlock:
  828. spin_unlock_bh(&xprt->transport_lock);
  829. return ret;
  830. }
  831. void xprt_end_transmit(struct rpc_task *task)
  832. {
  833. xprt_release_write(task->tk_rqstp->rq_xprt, task);
  834. }
  835. /**
  836. * xprt_transmit - send an RPC request on a transport
  837. * @task: controlling RPC task
  838. *
  839. * We have to copy the iovec because sendmsg fiddles with its contents.
  840. */
  841. void xprt_transmit(struct rpc_task *task)
  842. {
  843. struct rpc_rqst *req = task->tk_rqstp;
  844. struct rpc_xprt *xprt = req->rq_xprt;
  845. int status, numreqs;
  846. dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
  847. if (!req->rq_reply_bytes_recvd) {
  848. if (list_empty(&req->rq_list) && rpc_reply_expected(task)) {
  849. /*
  850. * Add to the list only if we're expecting a reply
  851. */
  852. spin_lock_bh(&xprt->transport_lock);
  853. /* Update the softirq receive buffer */
  854. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  855. sizeof(req->rq_private_buf));
  856. /* Add request to the receive list */
  857. list_add_tail(&req->rq_list, &xprt->recv);
  858. spin_unlock_bh(&xprt->transport_lock);
  859. xprt_reset_majortimeo(req);
  860. /* Turn off autodisconnect */
  861. del_singleshot_timer_sync(&xprt->timer);
  862. }
  863. } else if (!req->rq_bytes_sent)
  864. return;
  865. req->rq_xtime = ktime_get();
  866. status = xprt->ops->send_request(task);
  867. trace_xprt_transmit(xprt, req->rq_xid, status);
  868. if (status != 0) {
  869. task->tk_status = status;
  870. return;
  871. }
  872. dprintk("RPC: %5u xmit complete\n", task->tk_pid);
  873. task->tk_flags |= RPC_TASK_SENT;
  874. spin_lock_bh(&xprt->transport_lock);
  875. xprt->ops->set_retrans_timeout(task);
  876. numreqs = atomic_read(&xprt->num_reqs);
  877. if (numreqs > xprt->stat.max_slots)
  878. xprt->stat.max_slots = numreqs;
  879. xprt->stat.sends++;
  880. xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
  881. xprt->stat.bklog_u += xprt->backlog.qlen;
  882. xprt->stat.sending_u += xprt->sending.qlen;
  883. xprt->stat.pending_u += xprt->pending.qlen;
  884. /* Don't race with disconnect */
  885. if (!xprt_connected(xprt))
  886. task->tk_status = -ENOTCONN;
  887. else {
  888. /*
  889. * Sleep on the pending queue since
  890. * we're expecting a reply.
  891. */
  892. if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task))
  893. rpc_sleep_on(&xprt->pending, task, xprt_timer);
  894. req->rq_connect_cookie = xprt->connect_cookie;
  895. }
  896. spin_unlock_bh(&xprt->transport_lock);
  897. }
  898. static void xprt_add_backlog(struct rpc_xprt *xprt, struct rpc_task *task)
  899. {
  900. set_bit(XPRT_CONGESTED, &xprt->state);
  901. rpc_sleep_on(&xprt->backlog, task, NULL);
  902. }
  903. static void xprt_wake_up_backlog(struct rpc_xprt *xprt)
  904. {
  905. if (rpc_wake_up_next(&xprt->backlog) == NULL)
  906. clear_bit(XPRT_CONGESTED, &xprt->state);
  907. }
  908. static bool xprt_throttle_congested(struct rpc_xprt *xprt, struct rpc_task *task)
  909. {
  910. bool ret = false;
  911. if (!test_bit(XPRT_CONGESTED, &xprt->state))
  912. goto out;
  913. spin_lock(&xprt->reserve_lock);
  914. if (test_bit(XPRT_CONGESTED, &xprt->state)) {
  915. rpc_sleep_on(&xprt->backlog, task, NULL);
  916. ret = true;
  917. }
  918. spin_unlock(&xprt->reserve_lock);
  919. out:
  920. return ret;
  921. }
  922. static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt, gfp_t gfp_flags)
  923. {
  924. struct rpc_rqst *req = ERR_PTR(-EAGAIN);
  925. if (!atomic_add_unless(&xprt->num_reqs, 1, xprt->max_reqs))
  926. goto out;
  927. req = kzalloc(sizeof(struct rpc_rqst), gfp_flags);
  928. if (req != NULL)
  929. goto out;
  930. atomic_dec(&xprt->num_reqs);
  931. req = ERR_PTR(-ENOMEM);
  932. out:
  933. return req;
  934. }
  935. static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
  936. {
  937. if (atomic_add_unless(&xprt->num_reqs, -1, xprt->min_reqs)) {
  938. kfree(req);
  939. return true;
  940. }
  941. return false;
  942. }
  943. void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
  944. {
  945. struct rpc_rqst *req;
  946. spin_lock(&xprt->reserve_lock);
  947. if (!list_empty(&xprt->free)) {
  948. req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
  949. list_del(&req->rq_list);
  950. goto out_init_req;
  951. }
  952. req = xprt_dynamic_alloc_slot(xprt, GFP_NOWAIT|__GFP_NOWARN);
  953. if (!IS_ERR(req))
  954. goto out_init_req;
  955. switch (PTR_ERR(req)) {
  956. case -ENOMEM:
  957. dprintk("RPC: dynamic allocation of request slot "
  958. "failed! Retrying\n");
  959. task->tk_status = -ENOMEM;
  960. break;
  961. case -EAGAIN:
  962. xprt_add_backlog(xprt, task);
  963. dprintk("RPC: waiting for request slot\n");
  964. default:
  965. task->tk_status = -EAGAIN;
  966. }
  967. spin_unlock(&xprt->reserve_lock);
  968. return;
  969. out_init_req:
  970. task->tk_status = 0;
  971. task->tk_rqstp = req;
  972. xprt_request_init(task, xprt);
  973. spin_unlock(&xprt->reserve_lock);
  974. }
  975. EXPORT_SYMBOL_GPL(xprt_alloc_slot);
  976. void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
  977. {
  978. /* Note: grabbing the xprt_lock_write() ensures that we throttle
  979. * new slot allocation if the transport is congested (i.e. when
  980. * reconnecting a stream transport or when out of socket write
  981. * buffer space).
  982. */
  983. if (xprt_lock_write(xprt, task)) {
  984. xprt_alloc_slot(xprt, task);
  985. xprt_release_write(xprt, task);
  986. }
  987. }
  988. EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot);
  989. static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
  990. {
  991. spin_lock(&xprt->reserve_lock);
  992. if (!xprt_dynamic_free_slot(xprt, req)) {
  993. memset(req, 0, sizeof(*req)); /* mark unused */
  994. list_add(&req->rq_list, &xprt->free);
  995. }
  996. xprt_wake_up_backlog(xprt);
  997. spin_unlock(&xprt->reserve_lock);
  998. }
  999. static void xprt_free_all_slots(struct rpc_xprt *xprt)
  1000. {
  1001. struct rpc_rqst *req;
  1002. while (!list_empty(&xprt->free)) {
  1003. req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
  1004. list_del(&req->rq_list);
  1005. kfree(req);
  1006. }
  1007. }
  1008. struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
  1009. unsigned int num_prealloc,
  1010. unsigned int max_alloc)
  1011. {
  1012. struct rpc_xprt *xprt;
  1013. struct rpc_rqst *req;
  1014. int i;
  1015. xprt = kzalloc(size, GFP_KERNEL);
  1016. if (xprt == NULL)
  1017. goto out;
  1018. xprt_init(xprt, net);
  1019. for (i = 0; i < num_prealloc; i++) {
  1020. req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
  1021. if (!req)
  1022. goto out_free;
  1023. list_add(&req->rq_list, &xprt->free);
  1024. }
  1025. if (max_alloc > num_prealloc)
  1026. xprt->max_reqs = max_alloc;
  1027. else
  1028. xprt->max_reqs = num_prealloc;
  1029. xprt->min_reqs = num_prealloc;
  1030. atomic_set(&xprt->num_reqs, num_prealloc);
  1031. return xprt;
  1032. out_free:
  1033. xprt_free(xprt);
  1034. out:
  1035. return NULL;
  1036. }
  1037. EXPORT_SYMBOL_GPL(xprt_alloc);
  1038. void xprt_free(struct rpc_xprt *xprt)
  1039. {
  1040. put_net(xprt->xprt_net);
  1041. xprt_free_all_slots(xprt);
  1042. kfree(xprt);
  1043. }
  1044. EXPORT_SYMBOL_GPL(xprt_free);
  1045. /**
  1046. * xprt_reserve - allocate an RPC request slot
  1047. * @task: RPC task requesting a slot allocation
  1048. *
  1049. * If the transport is marked as being congested, or if no more
  1050. * slots are available, place the task on the transport's
  1051. * backlog queue.
  1052. */
  1053. void xprt_reserve(struct rpc_task *task)
  1054. {
  1055. struct rpc_xprt *xprt;
  1056. task->tk_status = 0;
  1057. if (task->tk_rqstp != NULL)
  1058. return;
  1059. task->tk_timeout = 0;
  1060. task->tk_status = -EAGAIN;
  1061. rcu_read_lock();
  1062. xprt = rcu_dereference(task->tk_client->cl_xprt);
  1063. if (!xprt_throttle_congested(xprt, task))
  1064. xprt->ops->alloc_slot(xprt, task);
  1065. rcu_read_unlock();
  1066. }
  1067. /**
  1068. * xprt_retry_reserve - allocate an RPC request slot
  1069. * @task: RPC task requesting a slot allocation
  1070. *
  1071. * If no more slots are available, place the task on the transport's
  1072. * backlog queue.
  1073. * Note that the only difference with xprt_reserve is that we now
  1074. * ignore the value of the XPRT_CONGESTED flag.
  1075. */
  1076. void xprt_retry_reserve(struct rpc_task *task)
  1077. {
  1078. struct rpc_xprt *xprt;
  1079. task->tk_status = 0;
  1080. if (task->tk_rqstp != NULL)
  1081. return;
  1082. task->tk_timeout = 0;
  1083. task->tk_status = -EAGAIN;
  1084. rcu_read_lock();
  1085. xprt = rcu_dereference(task->tk_client->cl_xprt);
  1086. xprt->ops->alloc_slot(xprt, task);
  1087. rcu_read_unlock();
  1088. }
  1089. static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
  1090. {
  1091. return (__force __be32)xprt->xid++;
  1092. }
  1093. static inline void xprt_init_xid(struct rpc_xprt *xprt)
  1094. {
  1095. xprt->xid = prandom_u32();
  1096. }
  1097. static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
  1098. {
  1099. struct rpc_rqst *req = task->tk_rqstp;
  1100. INIT_LIST_HEAD(&req->rq_list);
  1101. req->rq_timeout = task->tk_client->cl_timeout->to_initval;
  1102. req->rq_task = task;
  1103. req->rq_xprt = xprt;
  1104. req->rq_buffer = NULL;
  1105. req->rq_xid = xprt_alloc_xid(xprt);
  1106. req->rq_connect_cookie = xprt->connect_cookie - 1;
  1107. req->rq_bytes_sent = 0;
  1108. req->rq_snd_buf.len = 0;
  1109. req->rq_snd_buf.buflen = 0;
  1110. req->rq_rcv_buf.len = 0;
  1111. req->rq_rcv_buf.buflen = 0;
  1112. req->rq_release_snd_buf = NULL;
  1113. xprt_reset_majortimeo(req);
  1114. dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
  1115. req, ntohl(req->rq_xid));
  1116. }
  1117. /**
  1118. * xprt_release - release an RPC request slot
  1119. * @task: task which is finished with the slot
  1120. *
  1121. */
  1122. void xprt_release(struct rpc_task *task)
  1123. {
  1124. struct rpc_xprt *xprt;
  1125. struct rpc_rqst *req = task->tk_rqstp;
  1126. if (req == NULL) {
  1127. if (task->tk_client) {
  1128. rcu_read_lock();
  1129. xprt = rcu_dereference(task->tk_client->cl_xprt);
  1130. if (xprt->snd_task == task)
  1131. xprt_release_write(xprt, task);
  1132. rcu_read_unlock();
  1133. }
  1134. return;
  1135. }
  1136. xprt = req->rq_xprt;
  1137. if (task->tk_ops->rpc_count_stats != NULL)
  1138. task->tk_ops->rpc_count_stats(task, task->tk_calldata);
  1139. else if (task->tk_client)
  1140. rpc_count_iostats(task, task->tk_client->cl_metrics);
  1141. spin_lock_bh(&xprt->transport_lock);
  1142. xprt->ops->release_xprt(xprt, task);
  1143. if (xprt->ops->release_request)
  1144. xprt->ops->release_request(task);
  1145. if (!list_empty(&req->rq_list))
  1146. list_del(&req->rq_list);
  1147. xprt->last_used = jiffies;
  1148. if (list_empty(&xprt->recv) && xprt_has_timer(xprt))
  1149. mod_timer(&xprt->timer,
  1150. xprt->last_used + xprt->idle_timeout);
  1151. spin_unlock_bh(&xprt->transport_lock);
  1152. if (req->rq_buffer)
  1153. xprt->ops->buf_free(req->rq_buffer);
  1154. if (req->rq_cred != NULL)
  1155. put_rpccred(req->rq_cred);
  1156. task->tk_rqstp = NULL;
  1157. if (req->rq_release_snd_buf)
  1158. req->rq_release_snd_buf(req);
  1159. dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
  1160. if (likely(!bc_prealloc(req)))
  1161. xprt_free_slot(xprt, req);
  1162. else
  1163. xprt_free_bc_request(req);
  1164. }
  1165. static void xprt_init(struct rpc_xprt *xprt, struct net *net)
  1166. {
  1167. atomic_set(&xprt->count, 1);
  1168. spin_lock_init(&xprt->transport_lock);
  1169. spin_lock_init(&xprt->reserve_lock);
  1170. INIT_LIST_HEAD(&xprt->free);
  1171. INIT_LIST_HEAD(&xprt->recv);
  1172. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1173. spin_lock_init(&xprt->bc_pa_lock);
  1174. INIT_LIST_HEAD(&xprt->bc_pa_list);
  1175. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1176. xprt->last_used = jiffies;
  1177. xprt->cwnd = RPC_INITCWND;
  1178. xprt->bind_index = 0;
  1179. rpc_init_wait_queue(&xprt->binding, "xprt_binding");
  1180. rpc_init_wait_queue(&xprt->pending, "xprt_pending");
  1181. rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending");
  1182. rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
  1183. xprt_init_xid(xprt);
  1184. xprt->xprt_net = get_net(net);
  1185. }
  1186. /**
  1187. * xprt_create_transport - create an RPC transport
  1188. * @args: rpc transport creation arguments
  1189. *
  1190. */
  1191. struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
  1192. {
  1193. int err;
  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. err = rpc_xprt_debugfs_register(xprt);
  1231. if (err) {
  1232. xprt_destroy(xprt);
  1233. return ERR_PTR(err);
  1234. }
  1235. dprintk("RPC: created transport %p with %u slots\n", xprt,
  1236. xprt->max_reqs);
  1237. out:
  1238. return xprt;
  1239. }
  1240. /**
  1241. * xprt_destroy - destroy an RPC transport, killing off all requests.
  1242. * @xprt: transport to destroy
  1243. *
  1244. */
  1245. static void xprt_destroy(struct rpc_xprt *xprt)
  1246. {
  1247. dprintk("RPC: destroying transport %p\n", xprt);
  1248. del_timer_sync(&xprt->timer);
  1249. rpc_xprt_debugfs_unregister(xprt);
  1250. rpc_destroy_wait_queue(&xprt->binding);
  1251. rpc_destroy_wait_queue(&xprt->pending);
  1252. rpc_destroy_wait_queue(&xprt->sending);
  1253. rpc_destroy_wait_queue(&xprt->backlog);
  1254. cancel_work_sync(&xprt->task_cleanup);
  1255. kfree(xprt->servername);
  1256. /*
  1257. * Tear down transport state and free the rpc_xprt
  1258. */
  1259. xprt->ops->destroy(xprt);
  1260. }
  1261. /**
  1262. * xprt_put - release a reference to an RPC transport.
  1263. * @xprt: pointer to the transport
  1264. *
  1265. */
  1266. void xprt_put(struct rpc_xprt *xprt)
  1267. {
  1268. if (atomic_dec_and_test(&xprt->count))
  1269. xprt_destroy(xprt);
  1270. }