xprt.c 33 KB

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