xprt.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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 "sunrpc.h"
  49. /*
  50. * Local variables
  51. */
  52. #ifdef RPC_DEBUG
  53. # define RPCDBG_FACILITY RPCDBG_XPRT
  54. #endif
  55. /*
  56. * Local functions
  57. */
  58. static void xprt_request_init(struct rpc_task *, struct rpc_xprt *);
  59. static void xprt_connect_status(struct rpc_task *task);
  60. static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *);
  61. static DEFINE_SPINLOCK(xprt_list_lock);
  62. static LIST_HEAD(xprt_list);
  63. /*
  64. * The transport code maintains an estimate on the maximum number of out-
  65. * standing RPC requests, using a smoothed version of the congestion
  66. * avoidance implemented in 44BSD. This is basically the Van Jacobson
  67. * congestion algorithm: If a retransmit occurs, the congestion window is
  68. * halved; otherwise, it is incremented by 1/cwnd when
  69. *
  70. * - a reply is received and
  71. * - a full number of requests are outstanding and
  72. * - the congestion window hasn't been updated recently.
  73. */
  74. #define RPC_CWNDSHIFT (8U)
  75. #define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT)
  76. #define RPC_INITCWND RPC_CWNDSCALE
  77. #define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT)
  78. #define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
  79. /**
  80. * xprt_register_transport - register a transport implementation
  81. * @transport: transport to register
  82. *
  83. * If a transport implementation is loaded as a kernel module, it can
  84. * call this interface to make itself known to the RPC client.
  85. *
  86. * Returns:
  87. * 0: transport successfully registered
  88. * -EEXIST: transport already registered
  89. * -EINVAL: transport module being unloaded
  90. */
  91. int xprt_register_transport(struct xprt_class *transport)
  92. {
  93. struct xprt_class *t;
  94. int result;
  95. result = -EEXIST;
  96. spin_lock(&xprt_list_lock);
  97. list_for_each_entry(t, &xprt_list, list) {
  98. /* don't register the same transport class twice */
  99. if (t->ident == transport->ident)
  100. goto out;
  101. }
  102. list_add_tail(&transport->list, &xprt_list);
  103. printk(KERN_INFO "RPC: Registered %s transport module.\n",
  104. transport->name);
  105. result = 0;
  106. out:
  107. spin_unlock(&xprt_list_lock);
  108. return result;
  109. }
  110. EXPORT_SYMBOL_GPL(xprt_register_transport);
  111. /**
  112. * xprt_unregister_transport - unregister a transport implementation
  113. * @transport: transport to unregister
  114. *
  115. * Returns:
  116. * 0: transport successfully unregistered
  117. * -ENOENT: transport never registered
  118. */
  119. int xprt_unregister_transport(struct xprt_class *transport)
  120. {
  121. struct xprt_class *t;
  122. int result;
  123. result = 0;
  124. spin_lock(&xprt_list_lock);
  125. list_for_each_entry(t, &xprt_list, list) {
  126. if (t == transport) {
  127. printk(KERN_INFO
  128. "RPC: Unregistered %s transport module.\n",
  129. transport->name);
  130. list_del_init(&transport->list);
  131. goto out;
  132. }
  133. }
  134. result = -ENOENT;
  135. out:
  136. spin_unlock(&xprt_list_lock);
  137. return result;
  138. }
  139. EXPORT_SYMBOL_GPL(xprt_unregister_transport);
  140. /**
  141. * xprt_load_transport - load a transport implementation
  142. * @transport_name: transport to load
  143. *
  144. * Returns:
  145. * 0: transport successfully loaded
  146. * -ENOENT: transport module not available
  147. */
  148. int xprt_load_transport(const char *transport_name)
  149. {
  150. struct xprt_class *t;
  151. int result;
  152. result = 0;
  153. spin_lock(&xprt_list_lock);
  154. list_for_each_entry(t, &xprt_list, list) {
  155. if (strcmp(t->name, transport_name) == 0) {
  156. spin_unlock(&xprt_list_lock);
  157. goto out;
  158. }
  159. }
  160. spin_unlock(&xprt_list_lock);
  161. result = request_module("xprt%s", transport_name);
  162. out:
  163. return result;
  164. }
  165. EXPORT_SYMBOL_GPL(xprt_load_transport);
  166. /**
  167. * xprt_reserve_xprt - serialize write access to transports
  168. * @task: task that is requesting access to the transport
  169. *
  170. * This prevents mixing the payload of separate requests, and prevents
  171. * transport connects from colliding with writes. No congestion control
  172. * is provided.
  173. */
  174. int xprt_reserve_xprt(struct rpc_task *task)
  175. {
  176. struct rpc_rqst *req = task->tk_rqstp;
  177. struct rpc_xprt *xprt = req->rq_xprt;
  178. if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
  179. if (task == xprt->snd_task)
  180. return 1;
  181. if (task == NULL)
  182. return 0;
  183. goto out_sleep;
  184. }
  185. xprt->snd_task = task;
  186. if (req) {
  187. req->rq_bytes_sent = 0;
  188. req->rq_ntrans++;
  189. }
  190. return 1;
  191. out_sleep:
  192. dprintk("RPC: %5u failed to lock transport %p\n",
  193. task->tk_pid, xprt);
  194. task->tk_timeout = 0;
  195. task->tk_status = -EAGAIN;
  196. if (req && req->rq_ntrans)
  197. rpc_sleep_on(&xprt->resend, task, NULL);
  198. else
  199. rpc_sleep_on(&xprt->sending, task, NULL);
  200. return 0;
  201. }
  202. EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
  203. static void xprt_clear_locked(struct rpc_xprt *xprt)
  204. {
  205. xprt->snd_task = NULL;
  206. if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) {
  207. smp_mb__before_clear_bit();
  208. clear_bit(XPRT_LOCKED, &xprt->state);
  209. smp_mb__after_clear_bit();
  210. } else
  211. queue_work(rpciod_workqueue, &xprt->task_cleanup);
  212. }
  213. /*
  214. * xprt_reserve_xprt_cong - serialize write access to transports
  215. * @task: task that is requesting access to the transport
  216. *
  217. * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
  218. * integrated into the decision of whether a request is allowed to be
  219. * woken up and given access to the transport.
  220. */
  221. int xprt_reserve_xprt_cong(struct rpc_task *task)
  222. {
  223. struct rpc_xprt *xprt = task->tk_xprt;
  224. struct rpc_rqst *req = task->tk_rqstp;
  225. if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
  226. if (task == xprt->snd_task)
  227. return 1;
  228. goto out_sleep;
  229. }
  230. if (__xprt_get_cong(xprt, task)) {
  231. xprt->snd_task = task;
  232. if (req) {
  233. req->rq_bytes_sent = 0;
  234. req->rq_ntrans++;
  235. }
  236. return 1;
  237. }
  238. xprt_clear_locked(xprt);
  239. out_sleep:
  240. dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
  241. task->tk_timeout = 0;
  242. task->tk_status = -EAGAIN;
  243. if (req && req->rq_ntrans)
  244. rpc_sleep_on(&xprt->resend, task, NULL);
  245. else
  246. rpc_sleep_on(&xprt->sending, task, NULL);
  247. return 0;
  248. }
  249. EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
  250. static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
  251. {
  252. int retval;
  253. spin_lock_bh(&xprt->transport_lock);
  254. retval = xprt->ops->reserve_xprt(task);
  255. spin_unlock_bh(&xprt->transport_lock);
  256. return retval;
  257. }
  258. static void __xprt_lock_write_next(struct rpc_xprt *xprt)
  259. {
  260. struct rpc_task *task;
  261. struct rpc_rqst *req;
  262. if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
  263. return;
  264. task = rpc_wake_up_next(&xprt->resend);
  265. if (!task) {
  266. task = rpc_wake_up_next(&xprt->sending);
  267. if (!task)
  268. goto out_unlock;
  269. }
  270. req = task->tk_rqstp;
  271. xprt->snd_task = task;
  272. if (req) {
  273. req->rq_bytes_sent = 0;
  274. req->rq_ntrans++;
  275. }
  276. return;
  277. out_unlock:
  278. xprt_clear_locked(xprt);
  279. }
  280. static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
  281. {
  282. struct rpc_task *task;
  283. if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
  284. return;
  285. if (RPCXPRT_CONGESTED(xprt))
  286. goto out_unlock;
  287. task = rpc_wake_up_next(&xprt->resend);
  288. if (!task) {
  289. task = rpc_wake_up_next(&xprt->sending);
  290. if (!task)
  291. goto out_unlock;
  292. }
  293. if (__xprt_get_cong(xprt, task)) {
  294. struct rpc_rqst *req = task->tk_rqstp;
  295. xprt->snd_task = task;
  296. if (req) {
  297. req->rq_bytes_sent = 0;
  298. req->rq_ntrans++;
  299. }
  300. return;
  301. }
  302. out_unlock:
  303. xprt_clear_locked(xprt);
  304. }
  305. /**
  306. * xprt_release_xprt - allow other requests to use a transport
  307. * @xprt: transport with other tasks potentially waiting
  308. * @task: task that is releasing access to the transport
  309. *
  310. * Note that "task" can be NULL. No congestion control is provided.
  311. */
  312. void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
  313. {
  314. if (xprt->snd_task == task) {
  315. xprt_clear_locked(xprt);
  316. __xprt_lock_write_next(xprt);
  317. }
  318. }
  319. EXPORT_SYMBOL_GPL(xprt_release_xprt);
  320. /**
  321. * xprt_release_xprt_cong - allow other requests to use a transport
  322. * @xprt: transport with other tasks potentially waiting
  323. * @task: task that is releasing access to the transport
  324. *
  325. * Note that "task" can be NULL. Another task is awoken to use the
  326. * transport if the transport's congestion window allows it.
  327. */
  328. void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
  329. {
  330. if (xprt->snd_task == task) {
  331. xprt_clear_locked(xprt);
  332. __xprt_lock_write_next_cong(xprt);
  333. }
  334. }
  335. EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
  336. static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
  337. {
  338. spin_lock_bh(&xprt->transport_lock);
  339. xprt->ops->release_xprt(xprt, task);
  340. spin_unlock_bh(&xprt->transport_lock);
  341. }
  342. /*
  343. * Van Jacobson congestion avoidance. Check if the congestion window
  344. * overflowed. Put the task to sleep if this is the case.
  345. */
  346. static int
  347. __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
  348. {
  349. struct rpc_rqst *req = task->tk_rqstp;
  350. if (req->rq_cong)
  351. return 1;
  352. dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
  353. task->tk_pid, xprt->cong, xprt->cwnd);
  354. if (RPCXPRT_CONGESTED(xprt))
  355. return 0;
  356. req->rq_cong = 1;
  357. xprt->cong += RPC_CWNDSCALE;
  358. return 1;
  359. }
  360. /*
  361. * Adjust the congestion window, and wake up the next task
  362. * that has been sleeping due to congestion
  363. */
  364. static void
  365. __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
  366. {
  367. if (!req->rq_cong)
  368. return;
  369. req->rq_cong = 0;
  370. xprt->cong -= RPC_CWNDSCALE;
  371. __xprt_lock_write_next_cong(xprt);
  372. }
  373. /**
  374. * xprt_release_rqst_cong - housekeeping when request is complete
  375. * @task: RPC request that recently completed
  376. *
  377. * Useful for transports that require congestion control.
  378. */
  379. void xprt_release_rqst_cong(struct rpc_task *task)
  380. {
  381. __xprt_put_cong(task->tk_xprt, task->tk_rqstp);
  382. }
  383. EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
  384. /**
  385. * xprt_adjust_cwnd - adjust transport congestion window
  386. * @task: recently completed RPC request used to adjust window
  387. * @result: result code of completed RPC request
  388. *
  389. * We use a time-smoothed congestion estimator to avoid heavy oscillation.
  390. */
  391. void xprt_adjust_cwnd(struct rpc_task *task, int result)
  392. {
  393. struct rpc_rqst *req = task->tk_rqstp;
  394. struct rpc_xprt *xprt = task->tk_xprt;
  395. unsigned long cwnd = xprt->cwnd;
  396. if (result >= 0 && cwnd <= xprt->cong) {
  397. /* The (cwnd >> 1) term makes sure
  398. * the result gets rounded properly. */
  399. cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
  400. if (cwnd > RPC_MAXCWND(xprt))
  401. cwnd = RPC_MAXCWND(xprt);
  402. __xprt_lock_write_next_cong(xprt);
  403. } else if (result == -ETIMEDOUT) {
  404. cwnd >>= 1;
  405. if (cwnd < RPC_CWNDSCALE)
  406. cwnd = RPC_CWNDSCALE;
  407. }
  408. dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
  409. xprt->cong, xprt->cwnd, cwnd);
  410. xprt->cwnd = cwnd;
  411. __xprt_put_cong(xprt, req);
  412. }
  413. EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
  414. /**
  415. * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
  416. * @xprt: transport with waiting tasks
  417. * @status: result code to plant in each task before waking it
  418. *
  419. */
  420. void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
  421. {
  422. if (status < 0)
  423. rpc_wake_up_status(&xprt->pending, status);
  424. else
  425. rpc_wake_up(&xprt->pending);
  426. }
  427. EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
  428. /**
  429. * xprt_wait_for_buffer_space - wait for transport output buffer to clear
  430. * @task: task to be put to sleep
  431. * @action: function pointer to be executed after wait
  432. */
  433. void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
  434. {
  435. struct rpc_rqst *req = task->tk_rqstp;
  436. struct rpc_xprt *xprt = req->rq_xprt;
  437. task->tk_timeout = req->rq_timeout;
  438. rpc_sleep_on(&xprt->pending, task, action);
  439. }
  440. EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
  441. /**
  442. * xprt_write_space - wake the task waiting for transport output buffer space
  443. * @xprt: transport with waiting tasks
  444. *
  445. * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
  446. */
  447. void xprt_write_space(struct rpc_xprt *xprt)
  448. {
  449. if (unlikely(xprt->shutdown))
  450. return;
  451. spin_lock_bh(&xprt->transport_lock);
  452. if (xprt->snd_task) {
  453. dprintk("RPC: write space: waking waiting task on "
  454. "xprt %p\n", xprt);
  455. rpc_wake_up_queued_task(&xprt->pending, xprt->snd_task);
  456. }
  457. spin_unlock_bh(&xprt->transport_lock);
  458. }
  459. EXPORT_SYMBOL_GPL(xprt_write_space);
  460. /**
  461. * xprt_set_retrans_timeout_def - set a request's retransmit timeout
  462. * @task: task whose timeout is to be set
  463. *
  464. * Set a request's retransmit timeout based on the transport's
  465. * default timeout parameters. Used by transports that don't adjust
  466. * the retransmit timeout based on round-trip time estimation.
  467. */
  468. void xprt_set_retrans_timeout_def(struct rpc_task *task)
  469. {
  470. task->tk_timeout = task->tk_rqstp->rq_timeout;
  471. }
  472. EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_def);
  473. /*
  474. * xprt_set_retrans_timeout_rtt - set a request's retransmit timeout
  475. * @task: task whose timeout is to be set
  476. *
  477. * Set a request's retransmit timeout using the RTT estimator.
  478. */
  479. void xprt_set_retrans_timeout_rtt(struct rpc_task *task)
  480. {
  481. int timer = task->tk_msg.rpc_proc->p_timer;
  482. struct rpc_clnt *clnt = task->tk_client;
  483. struct rpc_rtt *rtt = clnt->cl_rtt;
  484. struct rpc_rqst *req = task->tk_rqstp;
  485. unsigned long max_timeout = clnt->cl_timeout->to_maxval;
  486. task->tk_timeout = rpc_calc_rto(rtt, timer);
  487. task->tk_timeout <<= rpc_ntimeo(rtt, timer) + req->rq_retries;
  488. if (task->tk_timeout > max_timeout || task->tk_timeout == 0)
  489. task->tk_timeout = max_timeout;
  490. }
  491. EXPORT_SYMBOL_GPL(xprt_set_retrans_timeout_rtt);
  492. static void xprt_reset_majortimeo(struct rpc_rqst *req)
  493. {
  494. const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
  495. req->rq_majortimeo = req->rq_timeout;
  496. if (to->to_exponential)
  497. req->rq_majortimeo <<= to->to_retries;
  498. else
  499. req->rq_majortimeo += to->to_increment * to->to_retries;
  500. if (req->rq_majortimeo > to->to_maxval || req->rq_majortimeo == 0)
  501. req->rq_majortimeo = to->to_maxval;
  502. req->rq_majortimeo += jiffies;
  503. }
  504. /**
  505. * xprt_adjust_timeout - adjust timeout values for next retransmit
  506. * @req: RPC request containing parameters to use for the adjustment
  507. *
  508. */
  509. int xprt_adjust_timeout(struct rpc_rqst *req)
  510. {
  511. struct rpc_xprt *xprt = req->rq_xprt;
  512. const struct rpc_timeout *to = req->rq_task->tk_client->cl_timeout;
  513. int status = 0;
  514. if (time_before(jiffies, req->rq_majortimeo)) {
  515. if (to->to_exponential)
  516. req->rq_timeout <<= 1;
  517. else
  518. req->rq_timeout += to->to_increment;
  519. if (to->to_maxval && req->rq_timeout >= to->to_maxval)
  520. req->rq_timeout = to->to_maxval;
  521. req->rq_retries++;
  522. } else {
  523. req->rq_timeout = to->to_initval;
  524. req->rq_retries = 0;
  525. xprt_reset_majortimeo(req);
  526. /* Reset the RTT counters == "slow start" */
  527. spin_lock_bh(&xprt->transport_lock);
  528. rpc_init_rtt(req->rq_task->tk_client->cl_rtt, to->to_initval);
  529. spin_unlock_bh(&xprt->transport_lock);
  530. status = -ETIMEDOUT;
  531. }
  532. if (req->rq_timeout == 0) {
  533. printk(KERN_WARNING "xprt_adjust_timeout: rq_timeout = 0!\n");
  534. req->rq_timeout = 5 * HZ;
  535. }
  536. return status;
  537. }
  538. static void xprt_autoclose(struct work_struct *work)
  539. {
  540. struct rpc_xprt *xprt =
  541. container_of(work, struct rpc_xprt, task_cleanup);
  542. xprt->ops->close(xprt);
  543. clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
  544. xprt_release_write(xprt, NULL);
  545. }
  546. /**
  547. * xprt_disconnect_done - mark a transport as disconnected
  548. * @xprt: transport to flag for disconnect
  549. *
  550. */
  551. void xprt_disconnect_done(struct rpc_xprt *xprt)
  552. {
  553. dprintk("RPC: disconnected transport %p\n", xprt);
  554. spin_lock_bh(&xprt->transport_lock);
  555. xprt_clear_connected(xprt);
  556. xprt_wake_pending_tasks(xprt, -EAGAIN);
  557. spin_unlock_bh(&xprt->transport_lock);
  558. }
  559. EXPORT_SYMBOL_GPL(xprt_disconnect_done);
  560. /**
  561. * xprt_force_disconnect - force a transport to disconnect
  562. * @xprt: transport to disconnect
  563. *
  564. */
  565. void xprt_force_disconnect(struct rpc_xprt *xprt)
  566. {
  567. /* Don't race with the test_bit() in xprt_clear_locked() */
  568. spin_lock_bh(&xprt->transport_lock);
  569. set_bit(XPRT_CLOSE_WAIT, &xprt->state);
  570. /* Try to schedule an autoclose RPC call */
  571. if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
  572. queue_work(rpciod_workqueue, &xprt->task_cleanup);
  573. xprt_wake_pending_tasks(xprt, -EAGAIN);
  574. spin_unlock_bh(&xprt->transport_lock);
  575. }
  576. /**
  577. * xprt_conditional_disconnect - force a transport to disconnect
  578. * @xprt: transport to disconnect
  579. * @cookie: 'connection cookie'
  580. *
  581. * This attempts to break the connection if and only if 'cookie' matches
  582. * the current transport 'connection cookie'. It ensures that we don't
  583. * try to break the connection more than once when we need to retransmit
  584. * a batch of RPC requests.
  585. *
  586. */
  587. void xprt_conditional_disconnect(struct rpc_xprt *xprt, unsigned int cookie)
  588. {
  589. /* Don't race with the test_bit() in xprt_clear_locked() */
  590. spin_lock_bh(&xprt->transport_lock);
  591. if (cookie != xprt->connect_cookie)
  592. goto out;
  593. if (test_bit(XPRT_CLOSING, &xprt->state) || !xprt_connected(xprt))
  594. goto out;
  595. set_bit(XPRT_CLOSE_WAIT, &xprt->state);
  596. /* Try to schedule an autoclose RPC call */
  597. if (test_and_set_bit(XPRT_LOCKED, &xprt->state) == 0)
  598. queue_work(rpciod_workqueue, &xprt->task_cleanup);
  599. xprt_wake_pending_tasks(xprt, -EAGAIN);
  600. out:
  601. spin_unlock_bh(&xprt->transport_lock);
  602. }
  603. static void
  604. xprt_init_autodisconnect(unsigned long data)
  605. {
  606. struct rpc_xprt *xprt = (struct rpc_xprt *)data;
  607. spin_lock(&xprt->transport_lock);
  608. if (!list_empty(&xprt->recv) || xprt->shutdown)
  609. goto out_abort;
  610. if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
  611. goto out_abort;
  612. spin_unlock(&xprt->transport_lock);
  613. set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
  614. queue_work(rpciod_workqueue, &xprt->task_cleanup);
  615. return;
  616. out_abort:
  617. spin_unlock(&xprt->transport_lock);
  618. }
  619. /**
  620. * xprt_connect - schedule a transport connect operation
  621. * @task: RPC task that is requesting the connect
  622. *
  623. */
  624. void xprt_connect(struct rpc_task *task)
  625. {
  626. struct rpc_xprt *xprt = task->tk_xprt;
  627. dprintk("RPC: %5u xprt_connect xprt %p %s connected\n", task->tk_pid,
  628. xprt, (xprt_connected(xprt) ? "is" : "is not"));
  629. if (!xprt_bound(xprt)) {
  630. task->tk_status = -EAGAIN;
  631. return;
  632. }
  633. if (!xprt_lock_write(xprt, task))
  634. return;
  635. if (test_and_clear_bit(XPRT_CLOSE_WAIT, &xprt->state))
  636. xprt->ops->close(xprt);
  637. if (xprt_connected(xprt))
  638. xprt_release_write(xprt, task);
  639. else {
  640. if (task->tk_rqstp)
  641. task->tk_rqstp->rq_bytes_sent = 0;
  642. task->tk_timeout = task->tk_rqstp->rq_timeout;
  643. rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
  644. if (test_bit(XPRT_CLOSING, &xprt->state))
  645. return;
  646. if (xprt_test_and_set_connecting(xprt))
  647. return;
  648. xprt->stat.connect_start = jiffies;
  649. xprt->ops->connect(task);
  650. }
  651. }
  652. static void xprt_connect_status(struct rpc_task *task)
  653. {
  654. struct rpc_xprt *xprt = task->tk_xprt;
  655. if (task->tk_status == 0) {
  656. xprt->stat.connect_count++;
  657. xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
  658. dprintk("RPC: %5u xprt_connect_status: connection established\n",
  659. task->tk_pid);
  660. return;
  661. }
  662. switch (task->tk_status) {
  663. case -EAGAIN:
  664. dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
  665. break;
  666. case -ETIMEDOUT:
  667. dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
  668. "out\n", task->tk_pid);
  669. break;
  670. default:
  671. dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
  672. "server %s\n", task->tk_pid, -task->tk_status,
  673. task->tk_client->cl_server);
  674. xprt_release_write(xprt, task);
  675. task->tk_status = -EIO;
  676. }
  677. }
  678. /**
  679. * xprt_lookup_rqst - find an RPC request corresponding to an XID
  680. * @xprt: transport on which the original request was transmitted
  681. * @xid: RPC XID of incoming reply
  682. *
  683. */
  684. struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
  685. {
  686. struct list_head *pos;
  687. list_for_each(pos, &xprt->recv) {
  688. struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list);
  689. if (entry->rq_xid == xid)
  690. return entry;
  691. }
  692. dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
  693. ntohl(xid));
  694. xprt->stat.bad_xids++;
  695. return NULL;
  696. }
  697. EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
  698. static void xprt_update_rtt(struct rpc_task *task)
  699. {
  700. struct rpc_rqst *req = task->tk_rqstp;
  701. struct rpc_rtt *rtt = task->tk_client->cl_rtt;
  702. unsigned timer = task->tk_msg.rpc_proc->p_timer;
  703. long m = usecs_to_jiffies(ktime_to_us(req->rq_rtt));
  704. if (timer) {
  705. if (req->rq_ntrans == 1)
  706. rpc_update_rtt(rtt, timer, m);
  707. rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
  708. }
  709. }
  710. /**
  711. * xprt_complete_rqst - called when reply processing is complete
  712. * @task: RPC request that recently completed
  713. * @copied: actual number of bytes received from the transport
  714. *
  715. * Caller holds transport lock.
  716. */
  717. void xprt_complete_rqst(struct rpc_task *task, int copied)
  718. {
  719. struct rpc_rqst *req = task->tk_rqstp;
  720. struct rpc_xprt *xprt = req->rq_xprt;
  721. dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
  722. task->tk_pid, ntohl(req->rq_xid), copied);
  723. xprt->stat.recvs++;
  724. req->rq_rtt = ktime_sub(ktime_get(), req->rq_xtime);
  725. if (xprt->ops->timer != NULL)
  726. xprt_update_rtt(task);
  727. list_del_init(&req->rq_list);
  728. req->rq_private_buf.len = copied;
  729. /* Ensure all writes are done before we update */
  730. /* req->rq_reply_bytes_recvd */
  731. smp_wmb();
  732. req->rq_reply_bytes_recvd = copied;
  733. rpc_wake_up_queued_task(&xprt->pending, task);
  734. }
  735. EXPORT_SYMBOL_GPL(xprt_complete_rqst);
  736. static void xprt_timer(struct rpc_task *task)
  737. {
  738. struct rpc_rqst *req = task->tk_rqstp;
  739. struct rpc_xprt *xprt = req->rq_xprt;
  740. if (task->tk_status != -ETIMEDOUT)
  741. return;
  742. dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
  743. spin_lock_bh(&xprt->transport_lock);
  744. if (!req->rq_reply_bytes_recvd) {
  745. if (xprt->ops->timer)
  746. xprt->ops->timer(task);
  747. } else
  748. task->tk_status = 0;
  749. spin_unlock_bh(&xprt->transport_lock);
  750. }
  751. static inline int xprt_has_timer(struct rpc_xprt *xprt)
  752. {
  753. return xprt->idle_timeout != 0;
  754. }
  755. /**
  756. * xprt_prepare_transmit - reserve the transport before sending a request
  757. * @task: RPC task about to send a request
  758. *
  759. */
  760. int xprt_prepare_transmit(struct rpc_task *task)
  761. {
  762. struct rpc_rqst *req = task->tk_rqstp;
  763. struct rpc_xprt *xprt = req->rq_xprt;
  764. int err = 0;
  765. dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
  766. spin_lock_bh(&xprt->transport_lock);
  767. if (req->rq_reply_bytes_recvd && !req->rq_bytes_sent) {
  768. err = req->rq_reply_bytes_recvd;
  769. goto out_unlock;
  770. }
  771. if (!xprt->ops->reserve_xprt(task))
  772. err = -EAGAIN;
  773. out_unlock:
  774. spin_unlock_bh(&xprt->transport_lock);
  775. return err;
  776. }
  777. void xprt_end_transmit(struct rpc_task *task)
  778. {
  779. xprt_release_write(task->tk_rqstp->rq_xprt, task);
  780. }
  781. /**
  782. * xprt_transmit - send an RPC request on a transport
  783. * @task: controlling RPC task
  784. *
  785. * We have to copy the iovec because sendmsg fiddles with its contents.
  786. */
  787. void xprt_transmit(struct rpc_task *task)
  788. {
  789. struct rpc_rqst *req = task->tk_rqstp;
  790. struct rpc_xprt *xprt = req->rq_xprt;
  791. int status;
  792. dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
  793. if (!req->rq_reply_bytes_recvd) {
  794. if (list_empty(&req->rq_list) && rpc_reply_expected(task)) {
  795. /*
  796. * Add to the list only if we're expecting a reply
  797. */
  798. spin_lock_bh(&xprt->transport_lock);
  799. /* Update the softirq receive buffer */
  800. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  801. sizeof(req->rq_private_buf));
  802. /* Add request to the receive list */
  803. list_add_tail(&req->rq_list, &xprt->recv);
  804. spin_unlock_bh(&xprt->transport_lock);
  805. xprt_reset_majortimeo(req);
  806. /* Turn off autodisconnect */
  807. del_singleshot_timer_sync(&xprt->timer);
  808. }
  809. } else if (!req->rq_bytes_sent)
  810. return;
  811. req->rq_connect_cookie = xprt->connect_cookie;
  812. req->rq_xtime = ktime_get();
  813. status = xprt->ops->send_request(task);
  814. if (status != 0) {
  815. task->tk_status = status;
  816. return;
  817. }
  818. dprintk("RPC: %5u xmit complete\n", task->tk_pid);
  819. spin_lock_bh(&xprt->transport_lock);
  820. xprt->ops->set_retrans_timeout(task);
  821. xprt->stat.sends++;
  822. xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
  823. xprt->stat.bklog_u += xprt->backlog.qlen;
  824. /* Don't race with disconnect */
  825. if (!xprt_connected(xprt))
  826. task->tk_status = -ENOTCONN;
  827. else if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task)) {
  828. /*
  829. * Sleep on the pending queue since
  830. * we're expecting a reply.
  831. */
  832. rpc_sleep_on(&xprt->pending, task, xprt_timer);
  833. }
  834. spin_unlock_bh(&xprt->transport_lock);
  835. }
  836. static void xprt_alloc_slot(struct rpc_task *task)
  837. {
  838. struct rpc_xprt *xprt = task->tk_xprt;
  839. task->tk_status = 0;
  840. if (task->tk_rqstp)
  841. return;
  842. if (!list_empty(&xprt->free)) {
  843. struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
  844. list_del_init(&req->rq_list);
  845. task->tk_rqstp = req;
  846. xprt_request_init(task, xprt);
  847. return;
  848. }
  849. dprintk("RPC: waiting for request slot\n");
  850. task->tk_status = -EAGAIN;
  851. task->tk_timeout = 0;
  852. rpc_sleep_on(&xprt->backlog, task, NULL);
  853. }
  854. static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
  855. {
  856. memset(req, 0, sizeof(*req)); /* mark unused */
  857. spin_lock(&xprt->reserve_lock);
  858. list_add(&req->rq_list, &xprt->free);
  859. rpc_wake_up_next(&xprt->backlog);
  860. spin_unlock(&xprt->reserve_lock);
  861. }
  862. struct rpc_xprt *xprt_alloc(struct net *net, int size, int max_req)
  863. {
  864. struct rpc_xprt *xprt;
  865. xprt = kzalloc(size, GFP_KERNEL);
  866. if (xprt == NULL)
  867. goto out;
  868. xprt->max_reqs = max_req;
  869. xprt->slot = kcalloc(max_req, sizeof(struct rpc_rqst), GFP_KERNEL);
  870. if (xprt->slot == NULL)
  871. goto out_free;
  872. xprt->xprt_net = get_net(net);
  873. return xprt;
  874. out_free:
  875. kfree(xprt);
  876. out:
  877. return NULL;
  878. }
  879. EXPORT_SYMBOL_GPL(xprt_alloc);
  880. void xprt_free(struct rpc_xprt *xprt)
  881. {
  882. put_net(xprt->xprt_net);
  883. kfree(xprt->slot);
  884. kfree(xprt);
  885. }
  886. EXPORT_SYMBOL_GPL(xprt_free);
  887. /**
  888. * xprt_reserve - allocate an RPC request slot
  889. * @task: RPC task requesting a slot allocation
  890. *
  891. * If no more slots are available, place the task on the transport's
  892. * backlog queue.
  893. */
  894. void xprt_reserve(struct rpc_task *task)
  895. {
  896. struct rpc_xprt *xprt = task->tk_xprt;
  897. task->tk_status = -EIO;
  898. spin_lock(&xprt->reserve_lock);
  899. xprt_alloc_slot(task);
  900. spin_unlock(&xprt->reserve_lock);
  901. }
  902. static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
  903. {
  904. return (__force __be32)xprt->xid++;
  905. }
  906. static inline void xprt_init_xid(struct rpc_xprt *xprt)
  907. {
  908. xprt->xid = net_random();
  909. }
  910. static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
  911. {
  912. struct rpc_rqst *req = task->tk_rqstp;
  913. req->rq_timeout = task->tk_client->cl_timeout->to_initval;
  914. req->rq_task = task;
  915. req->rq_xprt = xprt;
  916. req->rq_buffer = NULL;
  917. req->rq_xid = xprt_alloc_xid(xprt);
  918. req->rq_release_snd_buf = NULL;
  919. xprt_reset_majortimeo(req);
  920. dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
  921. req, ntohl(req->rq_xid));
  922. }
  923. /**
  924. * xprt_release - release an RPC request slot
  925. * @task: task which is finished with the slot
  926. *
  927. */
  928. void xprt_release(struct rpc_task *task)
  929. {
  930. struct rpc_xprt *xprt;
  931. struct rpc_rqst *req;
  932. if (!(req = task->tk_rqstp))
  933. return;
  934. xprt = req->rq_xprt;
  935. rpc_count_iostats(task);
  936. spin_lock_bh(&xprt->transport_lock);
  937. xprt->ops->release_xprt(xprt, task);
  938. if (xprt->ops->release_request)
  939. xprt->ops->release_request(task);
  940. if (!list_empty(&req->rq_list))
  941. list_del(&req->rq_list);
  942. xprt->last_used = jiffies;
  943. if (list_empty(&xprt->recv) && xprt_has_timer(xprt))
  944. mod_timer(&xprt->timer,
  945. xprt->last_used + xprt->idle_timeout);
  946. spin_unlock_bh(&xprt->transport_lock);
  947. if (req->rq_buffer)
  948. xprt->ops->buf_free(req->rq_buffer);
  949. if (req->rq_cred != NULL)
  950. put_rpccred(req->rq_cred);
  951. task->tk_rqstp = NULL;
  952. if (req->rq_release_snd_buf)
  953. req->rq_release_snd_buf(req);
  954. dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
  955. if (likely(!bc_prealloc(req)))
  956. xprt_free_slot(xprt, req);
  957. else
  958. xprt_free_bc_request(req);
  959. }
  960. /**
  961. * xprt_create_transport - create an RPC transport
  962. * @args: rpc transport creation arguments
  963. *
  964. */
  965. struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
  966. {
  967. struct rpc_xprt *xprt;
  968. struct rpc_rqst *req;
  969. struct xprt_class *t;
  970. spin_lock(&xprt_list_lock);
  971. list_for_each_entry(t, &xprt_list, list) {
  972. if (t->ident == args->ident) {
  973. spin_unlock(&xprt_list_lock);
  974. goto found;
  975. }
  976. }
  977. spin_unlock(&xprt_list_lock);
  978. printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident);
  979. return ERR_PTR(-EIO);
  980. found:
  981. xprt = t->setup(args);
  982. if (IS_ERR(xprt)) {
  983. dprintk("RPC: xprt_create_transport: failed, %ld\n",
  984. -PTR_ERR(xprt));
  985. return xprt;
  986. }
  987. kref_init(&xprt->kref);
  988. spin_lock_init(&xprt->transport_lock);
  989. spin_lock_init(&xprt->reserve_lock);
  990. INIT_LIST_HEAD(&xprt->free);
  991. INIT_LIST_HEAD(&xprt->recv);
  992. #if defined(CONFIG_NFS_V4_1)
  993. spin_lock_init(&xprt->bc_pa_lock);
  994. INIT_LIST_HEAD(&xprt->bc_pa_list);
  995. #endif /* CONFIG_NFS_V4_1 */
  996. INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
  997. if (xprt_has_timer(xprt))
  998. setup_timer(&xprt->timer, xprt_init_autodisconnect,
  999. (unsigned long)xprt);
  1000. else
  1001. init_timer(&xprt->timer);
  1002. xprt->last_used = jiffies;
  1003. xprt->cwnd = RPC_INITCWND;
  1004. xprt->bind_index = 0;
  1005. rpc_init_wait_queue(&xprt->binding, "xprt_binding");
  1006. rpc_init_wait_queue(&xprt->pending, "xprt_pending");
  1007. rpc_init_wait_queue(&xprt->sending, "xprt_sending");
  1008. rpc_init_wait_queue(&xprt->resend, "xprt_resend");
  1009. rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
  1010. /* initialize free list */
  1011. for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
  1012. list_add(&req->rq_list, &xprt->free);
  1013. xprt_init_xid(xprt);
  1014. dprintk("RPC: created transport %p with %u slots\n", xprt,
  1015. xprt->max_reqs);
  1016. return xprt;
  1017. }
  1018. /**
  1019. * xprt_destroy - destroy an RPC transport, killing off all requests.
  1020. * @kref: kref for the transport to destroy
  1021. *
  1022. */
  1023. static void xprt_destroy(struct kref *kref)
  1024. {
  1025. struct rpc_xprt *xprt = container_of(kref, struct rpc_xprt, kref);
  1026. dprintk("RPC: destroying transport %p\n", xprt);
  1027. xprt->shutdown = 1;
  1028. del_timer_sync(&xprt->timer);
  1029. rpc_destroy_wait_queue(&xprt->binding);
  1030. rpc_destroy_wait_queue(&xprt->pending);
  1031. rpc_destroy_wait_queue(&xprt->sending);
  1032. rpc_destroy_wait_queue(&xprt->resend);
  1033. rpc_destroy_wait_queue(&xprt->backlog);
  1034. cancel_work_sync(&xprt->task_cleanup);
  1035. /*
  1036. * Tear down transport state and free the rpc_xprt
  1037. */
  1038. xprt->ops->destroy(xprt);
  1039. }
  1040. /**
  1041. * xprt_put - release a reference to an RPC transport.
  1042. * @xprt: pointer to the transport
  1043. *
  1044. */
  1045. void xprt_put(struct rpc_xprt *xprt)
  1046. {
  1047. kref_put(&xprt->kref, xprt_destroy);
  1048. }
  1049. /**
  1050. * xprt_get - return a reference to an RPC transport.
  1051. * @xprt: pointer to the transport
  1052. *
  1053. */
  1054. struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
  1055. {
  1056. kref_get(&xprt->kref);
  1057. return xprt;
  1058. }