xprt.c 36 KB

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