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