xprt.c 34 KB

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