xprt.c 31 KB

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