xprt.c 36 KB

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