xprt.c 31 KB

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