xprt.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  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, it installs a timer that
  16. * is run after the packet's timeout has expired.
  17. * - When a packet arrives, the data_ready handler walks the list of
  18. * pending requests for that transport. If a matching XID is found, the
  19. * caller is woken up, and the timer removed.
  20. * - When no reply arrives within the timeout interval, the timer is
  21. * fired by the kernel and runs xprt_timer(). It either adjusts the
  22. * timeout values (minor timeout) or wakes up the caller with a status
  23. * of -ETIMEDOUT.
  24. * - When the caller receives a notification from RPC that a reply arrived,
  25. * it should release the RPC slot, and process the reply.
  26. * If the call timed out, it may choose to retry the operation by
  27. * adjusting the initial timeout value, and simply calling rpc_call
  28. * again.
  29. *
  30. * Support for async RPC is done through a set of RPC-specific scheduling
  31. * primitives that `transparently' work for processes as well as async
  32. * tasks that rely on callbacks.
  33. *
  34. * Copyright (C) 1995-1997, Olaf Kirch <okir@monad.swb.de>
  35. *
  36. * Transport switch API copyright (C) 2005, Chuck Lever <cel@netapp.com>
  37. */
  38. #include <linux/module.h>
  39. #include <linux/types.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/workqueue.h>
  42. #include <linux/net.h>
  43. #include <linux/sunrpc/clnt.h>
  44. #include <linux/sunrpc/metrics.h>
  45. /*
  46. * Local variables
  47. */
  48. #ifdef RPC_DEBUG
  49. # define RPCDBG_FACILITY RPCDBG_XPRT
  50. #endif
  51. /*
  52. * Local functions
  53. */
  54. static void xprt_request_init(struct rpc_task *, struct rpc_xprt *);
  55. static inline void do_xprt_reserve(struct rpc_task *);
  56. static void xprt_connect_status(struct rpc_task *task);
  57. static int __xprt_get_cong(struct rpc_xprt *, struct rpc_task *);
  58. static DEFINE_SPINLOCK(xprt_list_lock);
  59. static LIST_HEAD(xprt_list);
  60. /*
  61. * The transport code maintains an estimate on the maximum number of out-
  62. * standing RPC requests, using a smoothed version of the congestion
  63. * avoidance implemented in 44BSD. This is basically the Van Jacobson
  64. * congestion algorithm: If a retransmit occurs, the congestion window is
  65. * halved; otherwise, it is incremented by 1/cwnd when
  66. *
  67. * - a reply is received and
  68. * - a full number of requests are outstanding and
  69. * - the congestion window hasn't been updated recently.
  70. */
  71. #define RPC_CWNDSHIFT (8U)
  72. #define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT)
  73. #define RPC_INITCWND RPC_CWNDSCALE
  74. #define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT)
  75. #define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
  76. /**
  77. * xprt_register_transport - register a transport implementation
  78. * @transport: transport to register
  79. *
  80. * If a transport implementation is loaded as a kernel module, it can
  81. * call this interface to make itself known to the RPC client.
  82. *
  83. * Returns:
  84. * 0: transport successfully registered
  85. * -EEXIST: transport already registered
  86. * -EINVAL: transport module being unloaded
  87. */
  88. int xprt_register_transport(struct xprt_class *transport)
  89. {
  90. struct xprt_class *t;
  91. int result;
  92. result = -EEXIST;
  93. spin_lock(&xprt_list_lock);
  94. list_for_each_entry(t, &xprt_list, list) {
  95. /* don't register the same transport class twice */
  96. if (t->ident == transport->ident)
  97. goto out;
  98. }
  99. list_add_tail(&transport->list, &xprt_list);
  100. printk(KERN_INFO "RPC: Registered %s transport module.\n",
  101. transport->name);
  102. result = 0;
  103. out:
  104. spin_unlock(&xprt_list_lock);
  105. return result;
  106. }
  107. EXPORT_SYMBOL_GPL(xprt_register_transport);
  108. /**
  109. * xprt_unregister_transport - unregister a transport implementation
  110. * @transport: transport to unregister
  111. *
  112. * Returns:
  113. * 0: transport successfully unregistered
  114. * -ENOENT: transport never registered
  115. */
  116. int xprt_unregister_transport(struct xprt_class *transport)
  117. {
  118. struct xprt_class *t;
  119. int result;
  120. result = 0;
  121. spin_lock(&xprt_list_lock);
  122. list_for_each_entry(t, &xprt_list, list) {
  123. if (t == transport) {
  124. printk(KERN_INFO
  125. "RPC: Unregistered %s transport module.\n",
  126. transport->name);
  127. list_del_init(&transport->list);
  128. goto out;
  129. }
  130. }
  131. result = -ENOENT;
  132. out:
  133. spin_unlock(&xprt_list_lock);
  134. return result;
  135. }
  136. EXPORT_SYMBOL_GPL(xprt_unregister_transport);
  137. /**
  138. * xprt_load_transport - load a transport implementation
  139. * @transport_name: transport to load
  140. *
  141. * Returns:
  142. * 0: transport successfully loaded
  143. * -ENOENT: transport module not available
  144. */
  145. int xprt_load_transport(const char *transport_name)
  146. {
  147. struct xprt_class *t;
  148. char module_name[sizeof t->name + 5];
  149. int result;
  150. result = 0;
  151. spin_lock(&xprt_list_lock);
  152. list_for_each_entry(t, &xprt_list, list) {
  153. if (strcmp(t->name, transport_name) == 0) {
  154. spin_unlock(&xprt_list_lock);
  155. goto out;
  156. }
  157. }
  158. spin_unlock(&xprt_list_lock);
  159. strcpy(module_name, "xprt");
  160. strncat(module_name, transport_name, sizeof t->name);
  161. result = request_module(module_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_xprt *xprt = task->tk_xprt;
  177. struct rpc_rqst *req = task->tk_rqstp;
  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 (xprt_connected(xprt))
  636. xprt_release_write(xprt, task);
  637. else {
  638. if (task->tk_rqstp)
  639. task->tk_rqstp->rq_bytes_sent = 0;
  640. task->tk_timeout = xprt->connect_timeout;
  641. rpc_sleep_on(&xprt->pending, task, xprt_connect_status);
  642. xprt->stat.connect_start = jiffies;
  643. xprt->ops->connect(task);
  644. }
  645. return;
  646. }
  647. static void xprt_connect_status(struct rpc_task *task)
  648. {
  649. struct rpc_xprt *xprt = task->tk_xprt;
  650. if (task->tk_status == 0) {
  651. xprt->stat.connect_count++;
  652. xprt->stat.connect_time += (long)jiffies - xprt->stat.connect_start;
  653. dprintk("RPC: %5u xprt_connect_status: connection established\n",
  654. task->tk_pid);
  655. return;
  656. }
  657. switch (task->tk_status) {
  658. case -EAGAIN:
  659. dprintk("RPC: %5u xprt_connect_status: retrying\n", task->tk_pid);
  660. break;
  661. case -ETIMEDOUT:
  662. dprintk("RPC: %5u xprt_connect_status: connect attempt timed "
  663. "out\n", task->tk_pid);
  664. break;
  665. default:
  666. dprintk("RPC: %5u xprt_connect_status: error %d connecting to "
  667. "server %s\n", task->tk_pid, -task->tk_status,
  668. task->tk_client->cl_server);
  669. xprt_release_write(xprt, task);
  670. task->tk_status = -EIO;
  671. }
  672. }
  673. /**
  674. * xprt_lookup_rqst - find an RPC request corresponding to an XID
  675. * @xprt: transport on which the original request was transmitted
  676. * @xid: RPC XID of incoming reply
  677. *
  678. */
  679. struct rpc_rqst *xprt_lookup_rqst(struct rpc_xprt *xprt, __be32 xid)
  680. {
  681. struct list_head *pos;
  682. list_for_each(pos, &xprt->recv) {
  683. struct rpc_rqst *entry = list_entry(pos, struct rpc_rqst, rq_list);
  684. if (entry->rq_xid == xid)
  685. return entry;
  686. }
  687. dprintk("RPC: xprt_lookup_rqst did not find xid %08x\n",
  688. ntohl(xid));
  689. xprt->stat.bad_xids++;
  690. return NULL;
  691. }
  692. EXPORT_SYMBOL_GPL(xprt_lookup_rqst);
  693. /**
  694. * xprt_update_rtt - update an RPC client's RTT state after receiving a reply
  695. * @task: RPC request that recently completed
  696. *
  697. */
  698. 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. if (timer) {
  704. if (req->rq_ntrans == 1)
  705. rpc_update_rtt(rtt, timer,
  706. (long)jiffies - req->rq_xtime);
  707. rpc_set_timeo(rtt, timer, req->rq_ntrans - 1);
  708. }
  709. }
  710. EXPORT_SYMBOL_GPL(xprt_update_rtt);
  711. /**
  712. * xprt_complete_rqst - called when reply processing is complete
  713. * @task: RPC request that recently completed
  714. * @copied: actual number of bytes received from the transport
  715. *
  716. * Caller holds transport lock.
  717. */
  718. void xprt_complete_rqst(struct rpc_task *task, int copied)
  719. {
  720. struct rpc_rqst *req = task->tk_rqstp;
  721. struct rpc_xprt *xprt = req->rq_xprt;
  722. dprintk("RPC: %5u xid %08x complete (%d bytes received)\n",
  723. task->tk_pid, ntohl(req->rq_xid), copied);
  724. xprt->stat.recvs++;
  725. task->tk_rtt = (long)jiffies - req->rq_xtime;
  726. list_del_init(&req->rq_list);
  727. req->rq_private_buf.len = copied;
  728. /* Ensure all writes are done before we update req->rq_received */
  729. smp_wmb();
  730. req->rq_received = copied;
  731. rpc_wake_up_queued_task(&xprt->pending, task);
  732. }
  733. EXPORT_SYMBOL_GPL(xprt_complete_rqst);
  734. static void xprt_timer(struct rpc_task *task)
  735. {
  736. struct rpc_rqst *req = task->tk_rqstp;
  737. struct rpc_xprt *xprt = req->rq_xprt;
  738. if (task->tk_status != -ETIMEDOUT)
  739. return;
  740. dprintk("RPC: %5u xprt_timer\n", task->tk_pid);
  741. spin_lock_bh(&xprt->transport_lock);
  742. if (!req->rq_received) {
  743. if (xprt->ops->timer)
  744. xprt->ops->timer(task);
  745. } else
  746. task->tk_status = 0;
  747. spin_unlock_bh(&xprt->transport_lock);
  748. }
  749. /**
  750. * xprt_prepare_transmit - reserve the transport before sending a request
  751. * @task: RPC task about to send a request
  752. *
  753. */
  754. int xprt_prepare_transmit(struct rpc_task *task)
  755. {
  756. struct rpc_rqst *req = task->tk_rqstp;
  757. struct rpc_xprt *xprt = req->rq_xprt;
  758. int err = 0;
  759. dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
  760. spin_lock_bh(&xprt->transport_lock);
  761. if (req->rq_received && !req->rq_bytes_sent) {
  762. err = req->rq_received;
  763. goto out_unlock;
  764. }
  765. if (!xprt->ops->reserve_xprt(task))
  766. err = -EAGAIN;
  767. out_unlock:
  768. spin_unlock_bh(&xprt->transport_lock);
  769. return err;
  770. }
  771. void xprt_end_transmit(struct rpc_task *task)
  772. {
  773. xprt_release_write(task->tk_xprt, task);
  774. }
  775. /**
  776. * xprt_transmit - send an RPC request on a transport
  777. * @task: controlling RPC task
  778. *
  779. * We have to copy the iovec because sendmsg fiddles with its contents.
  780. */
  781. void xprt_transmit(struct rpc_task *task)
  782. {
  783. struct rpc_rqst *req = task->tk_rqstp;
  784. struct rpc_xprt *xprt = req->rq_xprt;
  785. int status;
  786. dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
  787. if (!req->rq_received) {
  788. if (list_empty(&req->rq_list)) {
  789. spin_lock_bh(&xprt->transport_lock);
  790. /* Update the softirq receive buffer */
  791. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  792. sizeof(req->rq_private_buf));
  793. /* Add request to the receive list */
  794. list_add_tail(&req->rq_list, &xprt->recv);
  795. spin_unlock_bh(&xprt->transport_lock);
  796. xprt_reset_majortimeo(req);
  797. /* Turn off autodisconnect */
  798. del_singleshot_timer_sync(&xprt->timer);
  799. }
  800. } else if (!req->rq_bytes_sent)
  801. return;
  802. req->rq_connect_cookie = xprt->connect_cookie;
  803. req->rq_xtime = jiffies;
  804. status = xprt->ops->send_request(task);
  805. if (status != 0) {
  806. task->tk_status = status;
  807. return;
  808. }
  809. dprintk("RPC: %5u xmit complete\n", task->tk_pid);
  810. spin_lock_bh(&xprt->transport_lock);
  811. xprt->ops->set_retrans_timeout(task);
  812. xprt->stat.sends++;
  813. xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
  814. xprt->stat.bklog_u += xprt->backlog.qlen;
  815. /* Don't race with disconnect */
  816. if (!xprt_connected(xprt))
  817. task->tk_status = -ENOTCONN;
  818. else if (!req->rq_received)
  819. rpc_sleep_on(&xprt->pending, task, xprt_timer);
  820. spin_unlock_bh(&xprt->transport_lock);
  821. }
  822. static inline void do_xprt_reserve(struct rpc_task *task)
  823. {
  824. struct rpc_xprt *xprt = task->tk_xprt;
  825. task->tk_status = 0;
  826. if (task->tk_rqstp)
  827. return;
  828. if (!list_empty(&xprt->free)) {
  829. struct rpc_rqst *req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
  830. list_del_init(&req->rq_list);
  831. task->tk_rqstp = req;
  832. xprt_request_init(task, xprt);
  833. return;
  834. }
  835. dprintk("RPC: waiting for request slot\n");
  836. task->tk_status = -EAGAIN;
  837. task->tk_timeout = 0;
  838. rpc_sleep_on(&xprt->backlog, task, NULL);
  839. }
  840. /**
  841. * xprt_reserve - allocate an RPC request slot
  842. * @task: RPC task requesting a slot allocation
  843. *
  844. * If no more slots are available, place the task on the transport's
  845. * backlog queue.
  846. */
  847. void xprt_reserve(struct rpc_task *task)
  848. {
  849. struct rpc_xprt *xprt = task->tk_xprt;
  850. task->tk_status = -EIO;
  851. spin_lock(&xprt->reserve_lock);
  852. do_xprt_reserve(task);
  853. spin_unlock(&xprt->reserve_lock);
  854. }
  855. static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
  856. {
  857. return xprt->xid++;
  858. }
  859. static inline void xprt_init_xid(struct rpc_xprt *xprt)
  860. {
  861. xprt->xid = net_random();
  862. }
  863. static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
  864. {
  865. struct rpc_rqst *req = task->tk_rqstp;
  866. req->rq_timeout = task->tk_client->cl_timeout->to_initval;
  867. req->rq_task = task;
  868. req->rq_xprt = xprt;
  869. req->rq_buffer = NULL;
  870. req->rq_xid = xprt_alloc_xid(xprt);
  871. req->rq_release_snd_buf = NULL;
  872. xprt_reset_majortimeo(req);
  873. dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
  874. req, ntohl(req->rq_xid));
  875. }
  876. /**
  877. * xprt_release - release an RPC request slot
  878. * @task: task which is finished with the slot
  879. *
  880. */
  881. void xprt_release(struct rpc_task *task)
  882. {
  883. struct rpc_xprt *xprt = task->tk_xprt;
  884. struct rpc_rqst *req;
  885. if (!(req = task->tk_rqstp))
  886. return;
  887. rpc_count_iostats(task);
  888. spin_lock_bh(&xprt->transport_lock);
  889. xprt->ops->release_xprt(xprt, task);
  890. if (xprt->ops->release_request)
  891. xprt->ops->release_request(task);
  892. if (!list_empty(&req->rq_list))
  893. list_del(&req->rq_list);
  894. xprt->last_used = jiffies;
  895. if (list_empty(&xprt->recv))
  896. mod_timer(&xprt->timer,
  897. xprt->last_used + xprt->idle_timeout);
  898. spin_unlock_bh(&xprt->transport_lock);
  899. xprt->ops->buf_free(req->rq_buffer);
  900. task->tk_rqstp = NULL;
  901. if (req->rq_release_snd_buf)
  902. req->rq_release_snd_buf(req);
  903. memset(req, 0, sizeof(*req)); /* mark unused */
  904. dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
  905. spin_lock(&xprt->reserve_lock);
  906. list_add(&req->rq_list, &xprt->free);
  907. rpc_wake_up_next(&xprt->backlog);
  908. spin_unlock(&xprt->reserve_lock);
  909. }
  910. /**
  911. * xprt_create_transport - create an RPC transport
  912. * @args: rpc transport creation arguments
  913. *
  914. */
  915. struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
  916. {
  917. struct rpc_xprt *xprt;
  918. struct rpc_rqst *req;
  919. struct xprt_class *t;
  920. spin_lock(&xprt_list_lock);
  921. list_for_each_entry(t, &xprt_list, list) {
  922. if (t->ident == args->ident) {
  923. spin_unlock(&xprt_list_lock);
  924. goto found;
  925. }
  926. }
  927. spin_unlock(&xprt_list_lock);
  928. printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident);
  929. return ERR_PTR(-EIO);
  930. found:
  931. xprt = t->setup(args);
  932. if (IS_ERR(xprt)) {
  933. dprintk("RPC: xprt_create_transport: failed, %ld\n",
  934. -PTR_ERR(xprt));
  935. return xprt;
  936. }
  937. kref_init(&xprt->kref);
  938. spin_lock_init(&xprt->transport_lock);
  939. spin_lock_init(&xprt->reserve_lock);
  940. INIT_LIST_HEAD(&xprt->free);
  941. INIT_LIST_HEAD(&xprt->recv);
  942. INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
  943. setup_timer(&xprt->timer, xprt_init_autodisconnect,
  944. (unsigned long)xprt);
  945. xprt->last_used = jiffies;
  946. xprt->cwnd = RPC_INITCWND;
  947. xprt->bind_index = 0;
  948. rpc_init_wait_queue(&xprt->binding, "xprt_binding");
  949. rpc_init_wait_queue(&xprt->pending, "xprt_pending");
  950. rpc_init_wait_queue(&xprt->sending, "xprt_sending");
  951. rpc_init_wait_queue(&xprt->resend, "xprt_resend");
  952. rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
  953. /* initialize free list */
  954. for (req = &xprt->slot[xprt->max_reqs-1]; req >= &xprt->slot[0]; req--)
  955. list_add(&req->rq_list, &xprt->free);
  956. xprt_init_xid(xprt);
  957. dprintk("RPC: created transport %p with %u slots\n", xprt,
  958. xprt->max_reqs);
  959. return xprt;
  960. }
  961. /**
  962. * xprt_destroy - destroy an RPC transport, killing off all requests.
  963. * @kref: kref for the transport to destroy
  964. *
  965. */
  966. static void xprt_destroy(struct kref *kref)
  967. {
  968. struct rpc_xprt *xprt = container_of(kref, struct rpc_xprt, kref);
  969. dprintk("RPC: destroying transport %p\n", xprt);
  970. xprt->shutdown = 1;
  971. del_timer_sync(&xprt->timer);
  972. rpc_destroy_wait_queue(&xprt->binding);
  973. rpc_destroy_wait_queue(&xprt->pending);
  974. rpc_destroy_wait_queue(&xprt->sending);
  975. rpc_destroy_wait_queue(&xprt->resend);
  976. rpc_destroy_wait_queue(&xprt->backlog);
  977. /*
  978. * Tear down transport state and free the rpc_xprt
  979. */
  980. xprt->ops->destroy(xprt);
  981. }
  982. /**
  983. * xprt_put - release a reference to an RPC transport.
  984. * @xprt: pointer to the transport
  985. *
  986. */
  987. void xprt_put(struct rpc_xprt *xprt)
  988. {
  989. kref_put(&xprt->kref, xprt_destroy);
  990. }
  991. /**
  992. * xprt_get - return a reference to an RPC transport.
  993. * @xprt: pointer to the transport
  994. *
  995. */
  996. struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
  997. {
  998. kref_get(&xprt->kref);
  999. return xprt;
  1000. }