xprt.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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_put_cong(struct rpc_xprt *, struct rpc_rqst *);
  63. static void xprt_destroy(struct rpc_xprt *xprt);
  64. static DEFINE_SPINLOCK(xprt_list_lock);
  65. static LIST_HEAD(xprt_list);
  66. /*
  67. * The transport code maintains an estimate on the maximum number of out-
  68. * standing RPC requests, using a smoothed version of the congestion
  69. * avoidance implemented in 44BSD. This is basically the Van Jacobson
  70. * congestion algorithm: If a retransmit occurs, the congestion window is
  71. * halved; otherwise, it is incremented by 1/cwnd when
  72. *
  73. * - a reply is received and
  74. * - a full number of requests are outstanding and
  75. * - the congestion window hasn't been updated recently.
  76. */
  77. #define RPC_CWNDSHIFT (8U)
  78. #define RPC_CWNDSCALE (1U << RPC_CWNDSHIFT)
  79. #define RPC_INITCWND RPC_CWNDSCALE
  80. #define RPC_MAXCWND(xprt) ((xprt)->max_reqs << RPC_CWNDSHIFT)
  81. #define RPCXPRT_CONGESTED(xprt) ((xprt)->cong >= (xprt)->cwnd)
  82. /**
  83. * xprt_register_transport - register a transport implementation
  84. * @transport: transport to register
  85. *
  86. * If a transport implementation is loaded as a kernel module, it can
  87. * call this interface to make itself known to the RPC client.
  88. *
  89. * Returns:
  90. * 0: transport successfully registered
  91. * -EEXIST: transport already registered
  92. * -EINVAL: transport module being unloaded
  93. */
  94. int xprt_register_transport(struct xprt_class *transport)
  95. {
  96. struct xprt_class *t;
  97. int result;
  98. result = -EEXIST;
  99. spin_lock(&xprt_list_lock);
  100. list_for_each_entry(t, &xprt_list, list) {
  101. /* don't register the same transport class twice */
  102. if (t->ident == transport->ident)
  103. goto out;
  104. }
  105. list_add_tail(&transport->list, &xprt_list);
  106. printk(KERN_INFO "RPC: Registered %s transport module.\n",
  107. transport->name);
  108. result = 0;
  109. out:
  110. spin_unlock(&xprt_list_lock);
  111. return result;
  112. }
  113. EXPORT_SYMBOL_GPL(xprt_register_transport);
  114. /**
  115. * xprt_unregister_transport - unregister a transport implementation
  116. * @transport: transport to unregister
  117. *
  118. * Returns:
  119. * 0: transport successfully unregistered
  120. * -ENOENT: transport never registered
  121. */
  122. int xprt_unregister_transport(struct xprt_class *transport)
  123. {
  124. struct xprt_class *t;
  125. int result;
  126. result = 0;
  127. spin_lock(&xprt_list_lock);
  128. list_for_each_entry(t, &xprt_list, list) {
  129. if (t == transport) {
  130. printk(KERN_INFO
  131. "RPC: Unregistered %s transport module.\n",
  132. transport->name);
  133. list_del_init(&transport->list);
  134. goto out;
  135. }
  136. }
  137. result = -ENOENT;
  138. out:
  139. spin_unlock(&xprt_list_lock);
  140. return result;
  141. }
  142. EXPORT_SYMBOL_GPL(xprt_unregister_transport);
  143. /**
  144. * xprt_load_transport - load a transport implementation
  145. * @transport_name: transport to load
  146. *
  147. * Returns:
  148. * 0: transport successfully loaded
  149. * -ENOENT: transport module not available
  150. */
  151. int xprt_load_transport(const char *transport_name)
  152. {
  153. struct xprt_class *t;
  154. int result;
  155. result = 0;
  156. spin_lock(&xprt_list_lock);
  157. list_for_each_entry(t, &xprt_list, list) {
  158. if (strcmp(t->name, transport_name) == 0) {
  159. spin_unlock(&xprt_list_lock);
  160. goto out;
  161. }
  162. }
  163. spin_unlock(&xprt_list_lock);
  164. result = request_module("xprt%s", transport_name);
  165. out:
  166. return result;
  167. }
  168. EXPORT_SYMBOL_GPL(xprt_load_transport);
  169. /**
  170. * xprt_reserve_xprt - serialize write access to transports
  171. * @task: task that is requesting access to the transport
  172. * @xprt: pointer to the target transport
  173. *
  174. * This prevents mixing the payload of separate requests, and prevents
  175. * transport connects from colliding with writes. No congestion control
  176. * is provided.
  177. */
  178. int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
  179. {
  180. struct rpc_rqst *req = task->tk_rqstp;
  181. int priority;
  182. if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
  183. if (task == xprt->snd_task)
  184. return 1;
  185. goto out_sleep;
  186. }
  187. xprt->snd_task = task;
  188. if (req != NULL) {
  189. req->rq_bytes_sent = 0;
  190. req->rq_ntrans++;
  191. }
  192. return 1;
  193. out_sleep:
  194. dprintk("RPC: %5u failed to lock transport %p\n",
  195. task->tk_pid, xprt);
  196. task->tk_timeout = 0;
  197. task->tk_status = -EAGAIN;
  198. if (req == NULL)
  199. priority = RPC_PRIORITY_LOW;
  200. else if (!req->rq_ntrans)
  201. priority = RPC_PRIORITY_NORMAL;
  202. else
  203. priority = RPC_PRIORITY_HIGH;
  204. rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
  205. return 0;
  206. }
  207. EXPORT_SYMBOL_GPL(xprt_reserve_xprt);
  208. static void xprt_clear_locked(struct rpc_xprt *xprt)
  209. {
  210. xprt->snd_task = NULL;
  211. if (!test_bit(XPRT_CLOSE_WAIT, &xprt->state) || xprt->shutdown) {
  212. smp_mb__before_clear_bit();
  213. clear_bit(XPRT_LOCKED, &xprt->state);
  214. smp_mb__after_clear_bit();
  215. } else
  216. queue_work(rpciod_workqueue, &xprt->task_cleanup);
  217. }
  218. /*
  219. * xprt_reserve_xprt_cong - serialize write access to transports
  220. * @task: task that is requesting access to the transport
  221. *
  222. * Same as xprt_reserve_xprt, but Van Jacobson congestion control is
  223. * integrated into the decision of whether a request is allowed to be
  224. * woken up and given access to the transport.
  225. */
  226. int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
  227. {
  228. struct rpc_rqst *req = task->tk_rqstp;
  229. int priority;
  230. if (test_and_set_bit(XPRT_LOCKED, &xprt->state)) {
  231. if (task == xprt->snd_task)
  232. return 1;
  233. goto out_sleep;
  234. }
  235. if (req == NULL) {
  236. xprt->snd_task = task;
  237. return 1;
  238. }
  239. if (__xprt_get_cong(xprt, task)) {
  240. xprt->snd_task = task;
  241. req->rq_bytes_sent = 0;
  242. req->rq_ntrans++;
  243. return 1;
  244. }
  245. xprt_clear_locked(xprt);
  246. out_sleep:
  247. if (req)
  248. __xprt_put_cong(xprt, req);
  249. dprintk("RPC: %5u failed to lock transport %p\n", task->tk_pid, xprt);
  250. task->tk_timeout = 0;
  251. task->tk_status = -EAGAIN;
  252. if (req == NULL)
  253. priority = RPC_PRIORITY_LOW;
  254. else if (!req->rq_ntrans)
  255. priority = RPC_PRIORITY_NORMAL;
  256. else
  257. priority = RPC_PRIORITY_HIGH;
  258. rpc_sleep_on_priority(&xprt->sending, task, NULL, priority);
  259. return 0;
  260. }
  261. EXPORT_SYMBOL_GPL(xprt_reserve_xprt_cong);
  262. static inline int xprt_lock_write(struct rpc_xprt *xprt, struct rpc_task *task)
  263. {
  264. int retval;
  265. spin_lock_bh(&xprt->transport_lock);
  266. retval = xprt->ops->reserve_xprt(xprt, task);
  267. spin_unlock_bh(&xprt->transport_lock);
  268. return retval;
  269. }
  270. static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
  271. {
  272. struct rpc_xprt *xprt = data;
  273. struct rpc_rqst *req;
  274. req = task->tk_rqstp;
  275. xprt->snd_task = task;
  276. if (req) {
  277. req->rq_bytes_sent = 0;
  278. req->rq_ntrans++;
  279. }
  280. return true;
  281. }
  282. static void __xprt_lock_write_next(struct rpc_xprt *xprt)
  283. {
  284. if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
  285. return;
  286. if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_func, xprt))
  287. return;
  288. xprt_clear_locked(xprt);
  289. }
  290. static bool __xprt_lock_write_cong_func(struct rpc_task *task, void *data)
  291. {
  292. struct rpc_xprt *xprt = data;
  293. struct rpc_rqst *req;
  294. req = task->tk_rqstp;
  295. if (req == NULL) {
  296. xprt->snd_task = task;
  297. return true;
  298. }
  299. if (__xprt_get_cong(xprt, task)) {
  300. xprt->snd_task = task;
  301. req->rq_bytes_sent = 0;
  302. req->rq_ntrans++;
  303. return true;
  304. }
  305. return false;
  306. }
  307. static void __xprt_lock_write_next_cong(struct rpc_xprt *xprt)
  308. {
  309. if (test_and_set_bit(XPRT_LOCKED, &xprt->state))
  310. return;
  311. if (RPCXPRT_CONGESTED(xprt))
  312. goto out_unlock;
  313. if (rpc_wake_up_first(&xprt->sending, __xprt_lock_write_cong_func, xprt))
  314. return;
  315. out_unlock:
  316. xprt_clear_locked(xprt);
  317. }
  318. /**
  319. * xprt_release_xprt - allow other requests to use a transport
  320. * @xprt: transport with other tasks potentially waiting
  321. * @task: task that is releasing access to the transport
  322. *
  323. * Note that "task" can be NULL. No congestion control is provided.
  324. */
  325. void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
  326. {
  327. if (xprt->snd_task == task) {
  328. xprt_clear_locked(xprt);
  329. __xprt_lock_write_next(xprt);
  330. }
  331. }
  332. EXPORT_SYMBOL_GPL(xprt_release_xprt);
  333. /**
  334. * xprt_release_xprt_cong - allow other requests to use a transport
  335. * @xprt: transport with other tasks potentially waiting
  336. * @task: task that is releasing access to the transport
  337. *
  338. * Note that "task" can be NULL. Another task is awoken to use the
  339. * transport if the transport's congestion window allows it.
  340. */
  341. void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
  342. {
  343. if (xprt->snd_task == task) {
  344. xprt_clear_locked(xprt);
  345. __xprt_lock_write_next_cong(xprt);
  346. }
  347. }
  348. EXPORT_SYMBOL_GPL(xprt_release_xprt_cong);
  349. static inline void xprt_release_write(struct rpc_xprt *xprt, struct rpc_task *task)
  350. {
  351. spin_lock_bh(&xprt->transport_lock);
  352. xprt->ops->release_xprt(xprt, task);
  353. spin_unlock_bh(&xprt->transport_lock);
  354. }
  355. /*
  356. * Van Jacobson congestion avoidance. Check if the congestion window
  357. * overflowed. Put the task to sleep if this is the case.
  358. */
  359. static int
  360. __xprt_get_cong(struct rpc_xprt *xprt, struct rpc_task *task)
  361. {
  362. struct rpc_rqst *req = task->tk_rqstp;
  363. if (req->rq_cong)
  364. return 1;
  365. dprintk("RPC: %5u xprt_cwnd_limited cong = %lu cwnd = %lu\n",
  366. task->tk_pid, xprt->cong, xprt->cwnd);
  367. if (RPCXPRT_CONGESTED(xprt))
  368. return 0;
  369. req->rq_cong = 1;
  370. xprt->cong += RPC_CWNDSCALE;
  371. return 1;
  372. }
  373. /*
  374. * Adjust the congestion window, and wake up the next task
  375. * that has been sleeping due to congestion
  376. */
  377. static void
  378. __xprt_put_cong(struct rpc_xprt *xprt, struct rpc_rqst *req)
  379. {
  380. if (!req->rq_cong)
  381. return;
  382. req->rq_cong = 0;
  383. xprt->cong -= RPC_CWNDSCALE;
  384. __xprt_lock_write_next_cong(xprt);
  385. }
  386. /**
  387. * xprt_release_rqst_cong - housekeeping when request is complete
  388. * @task: RPC request that recently completed
  389. *
  390. * Useful for transports that require congestion control.
  391. */
  392. void xprt_release_rqst_cong(struct rpc_task *task)
  393. {
  394. __xprt_put_cong(task->tk_xprt, task->tk_rqstp);
  395. }
  396. EXPORT_SYMBOL_GPL(xprt_release_rqst_cong);
  397. /**
  398. * xprt_adjust_cwnd - adjust transport congestion window
  399. * @task: recently completed RPC request used to adjust window
  400. * @result: result code of completed RPC request
  401. *
  402. * We use a time-smoothed congestion estimator to avoid heavy oscillation.
  403. */
  404. void xprt_adjust_cwnd(struct rpc_task *task, int result)
  405. {
  406. struct rpc_rqst *req = task->tk_rqstp;
  407. struct rpc_xprt *xprt = task->tk_xprt;
  408. unsigned long cwnd = xprt->cwnd;
  409. if (result >= 0 && cwnd <= xprt->cong) {
  410. /* The (cwnd >> 1) term makes sure
  411. * the result gets rounded properly. */
  412. cwnd += (RPC_CWNDSCALE * RPC_CWNDSCALE + (cwnd >> 1)) / cwnd;
  413. if (cwnd > RPC_MAXCWND(xprt))
  414. cwnd = RPC_MAXCWND(xprt);
  415. __xprt_lock_write_next_cong(xprt);
  416. } else if (result == -ETIMEDOUT) {
  417. cwnd >>= 1;
  418. if (cwnd < RPC_CWNDSCALE)
  419. cwnd = RPC_CWNDSCALE;
  420. }
  421. dprintk("RPC: cong %ld, cwnd was %ld, now %ld\n",
  422. xprt->cong, xprt->cwnd, cwnd);
  423. xprt->cwnd = cwnd;
  424. __xprt_put_cong(xprt, req);
  425. }
  426. EXPORT_SYMBOL_GPL(xprt_adjust_cwnd);
  427. /**
  428. * xprt_wake_pending_tasks - wake all tasks on a transport's pending queue
  429. * @xprt: transport with waiting tasks
  430. * @status: result code to plant in each task before waking it
  431. *
  432. */
  433. void xprt_wake_pending_tasks(struct rpc_xprt *xprt, int status)
  434. {
  435. if (status < 0)
  436. rpc_wake_up_status(&xprt->pending, status);
  437. else
  438. rpc_wake_up(&xprt->pending);
  439. }
  440. EXPORT_SYMBOL_GPL(xprt_wake_pending_tasks);
  441. /**
  442. * xprt_wait_for_buffer_space - wait for transport output buffer to clear
  443. * @task: task to be put to sleep
  444. * @action: function pointer to be executed after wait
  445. *
  446. * Note that we only set the timer for the case of RPC_IS_SOFT(), since
  447. * we don't in general want to force a socket disconnection due to
  448. * an incomplete RPC call transmission.
  449. */
  450. void xprt_wait_for_buffer_space(struct rpc_task *task, rpc_action action)
  451. {
  452. struct rpc_rqst *req = task->tk_rqstp;
  453. struct rpc_xprt *xprt = req->rq_xprt;
  454. task->tk_timeout = RPC_IS_SOFT(task) ? req->rq_timeout : 0;
  455. rpc_sleep_on(&xprt->pending, task, action);
  456. }
  457. EXPORT_SYMBOL_GPL(xprt_wait_for_buffer_space);
  458. /**
  459. * xprt_write_space - wake the task waiting for transport output buffer space
  460. * @xprt: transport with waiting tasks
  461. *
  462. * Can be called in a soft IRQ context, so xprt_write_space never sleeps.
  463. */
  464. void xprt_write_space(struct rpc_xprt *xprt)
  465. {
  466. if (unlikely(xprt->shutdown))
  467. return;
  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) || xprt->shutdown)
  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_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(task);
  666. }
  667. }
  668. static void xprt_connect_status(struct rpc_task *task)
  669. {
  670. struct rpc_xprt *xprt = task->tk_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 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(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. int 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. int err = 0;
  779. dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
  780. spin_lock_bh(&xprt->transport_lock);
  781. if (req->rq_reply_bytes_recvd && !req->rq_bytes_sent) {
  782. err = req->rq_reply_bytes_recvd;
  783. goto out_unlock;
  784. }
  785. if (!xprt->ops->reserve_xprt(xprt, task))
  786. err = -EAGAIN;
  787. out_unlock:
  788. spin_unlock_bh(&xprt->transport_lock);
  789. return err;
  790. }
  791. void xprt_end_transmit(struct rpc_task *task)
  792. {
  793. xprt_release_write(task->tk_rqstp->rq_xprt, task);
  794. }
  795. /**
  796. * xprt_transmit - send an RPC request on a transport
  797. * @task: controlling RPC task
  798. *
  799. * We have to copy the iovec because sendmsg fiddles with its contents.
  800. */
  801. void xprt_transmit(struct rpc_task *task)
  802. {
  803. struct rpc_rqst *req = task->tk_rqstp;
  804. struct rpc_xprt *xprt = req->rq_xprt;
  805. int status, numreqs;
  806. dprintk("RPC: %5u xprt_transmit(%u)\n", task->tk_pid, req->rq_slen);
  807. if (!req->rq_reply_bytes_recvd) {
  808. if (list_empty(&req->rq_list) && rpc_reply_expected(task)) {
  809. /*
  810. * Add to the list only if we're expecting a reply
  811. */
  812. spin_lock_bh(&xprt->transport_lock);
  813. /* Update the softirq receive buffer */
  814. memcpy(&req->rq_private_buf, &req->rq_rcv_buf,
  815. sizeof(req->rq_private_buf));
  816. /* Add request to the receive list */
  817. list_add_tail(&req->rq_list, &xprt->recv);
  818. spin_unlock_bh(&xprt->transport_lock);
  819. xprt_reset_majortimeo(req);
  820. /* Turn off autodisconnect */
  821. del_singleshot_timer_sync(&xprt->timer);
  822. }
  823. } else if (!req->rq_bytes_sent)
  824. return;
  825. req->rq_connect_cookie = xprt->connect_cookie;
  826. req->rq_xtime = ktime_get();
  827. status = xprt->ops->send_request(task);
  828. if (status != 0) {
  829. task->tk_status = status;
  830. return;
  831. }
  832. dprintk("RPC: %5u xmit complete\n", task->tk_pid);
  833. task->tk_flags |= RPC_TASK_SENT;
  834. spin_lock_bh(&xprt->transport_lock);
  835. xprt->ops->set_retrans_timeout(task);
  836. numreqs = atomic_read(&xprt->num_reqs);
  837. if (numreqs > xprt->stat.max_slots)
  838. xprt->stat.max_slots = numreqs;
  839. xprt->stat.sends++;
  840. xprt->stat.req_u += xprt->stat.sends - xprt->stat.recvs;
  841. xprt->stat.bklog_u += xprt->backlog.qlen;
  842. xprt->stat.sending_u += xprt->sending.qlen;
  843. xprt->stat.pending_u += xprt->pending.qlen;
  844. /* Don't race with disconnect */
  845. if (!xprt_connected(xprt))
  846. task->tk_status = -ENOTCONN;
  847. else if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task)) {
  848. /*
  849. * Sleep on the pending queue since
  850. * we're expecting a reply.
  851. */
  852. rpc_sleep_on(&xprt->pending, task, xprt_timer);
  853. }
  854. spin_unlock_bh(&xprt->transport_lock);
  855. }
  856. static struct rpc_rqst *xprt_dynamic_alloc_slot(struct rpc_xprt *xprt, gfp_t gfp_flags)
  857. {
  858. struct rpc_rqst *req = ERR_PTR(-EAGAIN);
  859. if (!atomic_add_unless(&xprt->num_reqs, 1, xprt->max_reqs))
  860. goto out;
  861. req = kzalloc(sizeof(struct rpc_rqst), gfp_flags);
  862. if (req != NULL)
  863. goto out;
  864. atomic_dec(&xprt->num_reqs);
  865. req = ERR_PTR(-ENOMEM);
  866. out:
  867. return req;
  868. }
  869. static bool xprt_dynamic_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
  870. {
  871. if (atomic_add_unless(&xprt->num_reqs, -1, xprt->min_reqs)) {
  872. kfree(req);
  873. return true;
  874. }
  875. return false;
  876. }
  877. void xprt_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
  878. {
  879. struct rpc_rqst *req;
  880. spin_lock(&xprt->reserve_lock);
  881. if (!list_empty(&xprt->free)) {
  882. req = list_entry(xprt->free.next, struct rpc_rqst, rq_list);
  883. list_del(&req->rq_list);
  884. goto out_init_req;
  885. }
  886. req = xprt_dynamic_alloc_slot(xprt, GFP_NOWAIT);
  887. if (!IS_ERR(req))
  888. goto out_init_req;
  889. switch (PTR_ERR(req)) {
  890. case -ENOMEM:
  891. dprintk("RPC: dynamic allocation of request slot "
  892. "failed! Retrying\n");
  893. task->tk_status = -ENOMEM;
  894. break;
  895. case -EAGAIN:
  896. rpc_sleep_on(&xprt->backlog, task, NULL);
  897. dprintk("RPC: waiting for request slot\n");
  898. default:
  899. task->tk_status = -EAGAIN;
  900. }
  901. spin_unlock(&xprt->reserve_lock);
  902. return;
  903. out_init_req:
  904. task->tk_status = 0;
  905. task->tk_rqstp = req;
  906. xprt_request_init(task, xprt);
  907. spin_unlock(&xprt->reserve_lock);
  908. }
  909. EXPORT_SYMBOL_GPL(xprt_alloc_slot);
  910. void xprt_lock_and_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
  911. {
  912. /* Note: grabbing the xprt_lock_write() ensures that we throttle
  913. * new slot allocation if the transport is congested (i.e. when
  914. * reconnecting a stream transport or when out of socket write
  915. * buffer space).
  916. */
  917. if (xprt_lock_write(xprt, task)) {
  918. xprt_alloc_slot(xprt, task);
  919. xprt_release_write(xprt, task);
  920. }
  921. }
  922. EXPORT_SYMBOL_GPL(xprt_lock_and_alloc_slot);
  923. static void xprt_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *req)
  924. {
  925. spin_lock(&xprt->reserve_lock);
  926. if (!xprt_dynamic_free_slot(xprt, req)) {
  927. memset(req, 0, sizeof(*req)); /* mark unused */
  928. list_add(&req->rq_list, &xprt->free);
  929. }
  930. rpc_wake_up_next(&xprt->backlog);
  931. spin_unlock(&xprt->reserve_lock);
  932. }
  933. static void xprt_free_all_slots(struct rpc_xprt *xprt)
  934. {
  935. struct rpc_rqst *req;
  936. while (!list_empty(&xprt->free)) {
  937. req = list_first_entry(&xprt->free, struct rpc_rqst, rq_list);
  938. list_del(&req->rq_list);
  939. kfree(req);
  940. }
  941. }
  942. struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
  943. unsigned int num_prealloc,
  944. unsigned int max_alloc)
  945. {
  946. struct rpc_xprt *xprt;
  947. struct rpc_rqst *req;
  948. int i;
  949. xprt = kzalloc(size, GFP_KERNEL);
  950. if (xprt == NULL)
  951. goto out;
  952. xprt_init(xprt, net);
  953. for (i = 0; i < num_prealloc; i++) {
  954. req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
  955. if (!req)
  956. break;
  957. list_add(&req->rq_list, &xprt->free);
  958. }
  959. if (i < num_prealloc)
  960. goto out_free;
  961. if (max_alloc > num_prealloc)
  962. xprt->max_reqs = max_alloc;
  963. else
  964. xprt->max_reqs = num_prealloc;
  965. xprt->min_reqs = num_prealloc;
  966. atomic_set(&xprt->num_reqs, num_prealloc);
  967. return xprt;
  968. out_free:
  969. xprt_free(xprt);
  970. out:
  971. return NULL;
  972. }
  973. EXPORT_SYMBOL_GPL(xprt_alloc);
  974. void xprt_free(struct rpc_xprt *xprt)
  975. {
  976. put_net(xprt->xprt_net);
  977. xprt_free_all_slots(xprt);
  978. kfree(xprt);
  979. }
  980. EXPORT_SYMBOL_GPL(xprt_free);
  981. /**
  982. * xprt_reserve - allocate an RPC request slot
  983. * @task: RPC task requesting a slot allocation
  984. *
  985. * If no more slots are available, place the task on the transport's
  986. * backlog queue.
  987. */
  988. void xprt_reserve(struct rpc_task *task)
  989. {
  990. struct rpc_xprt *xprt = task->tk_xprt;
  991. task->tk_status = 0;
  992. if (task->tk_rqstp != NULL)
  993. return;
  994. task->tk_timeout = 0;
  995. task->tk_status = -EAGAIN;
  996. xprt->ops->alloc_slot(xprt, task);
  997. }
  998. static inline __be32 xprt_alloc_xid(struct rpc_xprt *xprt)
  999. {
  1000. return (__force __be32)xprt->xid++;
  1001. }
  1002. static inline void xprt_init_xid(struct rpc_xprt *xprt)
  1003. {
  1004. xprt->xid = net_random();
  1005. }
  1006. static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
  1007. {
  1008. struct rpc_rqst *req = task->tk_rqstp;
  1009. INIT_LIST_HEAD(&req->rq_list);
  1010. req->rq_timeout = task->tk_client->cl_timeout->to_initval;
  1011. req->rq_task = task;
  1012. req->rq_xprt = xprt;
  1013. req->rq_buffer = NULL;
  1014. req->rq_xid = xprt_alloc_xid(xprt);
  1015. req->rq_release_snd_buf = NULL;
  1016. xprt_reset_majortimeo(req);
  1017. dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
  1018. req, ntohl(req->rq_xid));
  1019. }
  1020. /**
  1021. * xprt_release - release an RPC request slot
  1022. * @task: task which is finished with the slot
  1023. *
  1024. */
  1025. void xprt_release(struct rpc_task *task)
  1026. {
  1027. struct rpc_xprt *xprt;
  1028. struct rpc_rqst *req = task->tk_rqstp;
  1029. if (req == NULL) {
  1030. if (task->tk_client) {
  1031. rcu_read_lock();
  1032. xprt = rcu_dereference(task->tk_client->cl_xprt);
  1033. if (xprt->snd_task == task)
  1034. xprt_release_write(xprt, task);
  1035. rcu_read_unlock();
  1036. }
  1037. return;
  1038. }
  1039. xprt = req->rq_xprt;
  1040. if (task->tk_ops->rpc_count_stats != NULL)
  1041. task->tk_ops->rpc_count_stats(task, task->tk_calldata);
  1042. else if (task->tk_client)
  1043. rpc_count_iostats(task, task->tk_client->cl_metrics);
  1044. spin_lock_bh(&xprt->transport_lock);
  1045. xprt->ops->release_xprt(xprt, task);
  1046. if (xprt->ops->release_request)
  1047. xprt->ops->release_request(task);
  1048. if (!list_empty(&req->rq_list))
  1049. list_del(&req->rq_list);
  1050. xprt->last_used = jiffies;
  1051. if (list_empty(&xprt->recv) && xprt_has_timer(xprt))
  1052. mod_timer(&xprt->timer,
  1053. xprt->last_used + xprt->idle_timeout);
  1054. spin_unlock_bh(&xprt->transport_lock);
  1055. if (req->rq_buffer)
  1056. xprt->ops->buf_free(req->rq_buffer);
  1057. if (req->rq_cred != NULL)
  1058. put_rpccred(req->rq_cred);
  1059. task->tk_rqstp = NULL;
  1060. if (req->rq_release_snd_buf)
  1061. req->rq_release_snd_buf(req);
  1062. dprintk("RPC: %5u release request %p\n", task->tk_pid, req);
  1063. if (likely(!bc_prealloc(req)))
  1064. xprt_free_slot(xprt, req);
  1065. else
  1066. xprt_free_bc_request(req);
  1067. }
  1068. static void xprt_init(struct rpc_xprt *xprt, struct net *net)
  1069. {
  1070. atomic_set(&xprt->count, 1);
  1071. spin_lock_init(&xprt->transport_lock);
  1072. spin_lock_init(&xprt->reserve_lock);
  1073. INIT_LIST_HEAD(&xprt->free);
  1074. INIT_LIST_HEAD(&xprt->recv);
  1075. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  1076. spin_lock_init(&xprt->bc_pa_lock);
  1077. INIT_LIST_HEAD(&xprt->bc_pa_list);
  1078. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  1079. xprt->last_used = jiffies;
  1080. xprt->cwnd = RPC_INITCWND;
  1081. xprt->bind_index = 0;
  1082. rpc_init_wait_queue(&xprt->binding, "xprt_binding");
  1083. rpc_init_wait_queue(&xprt->pending, "xprt_pending");
  1084. rpc_init_priority_wait_queue(&xprt->sending, "xprt_sending");
  1085. rpc_init_priority_wait_queue(&xprt->backlog, "xprt_backlog");
  1086. xprt_init_xid(xprt);
  1087. xprt->xprt_net = get_net(net);
  1088. }
  1089. /**
  1090. * xprt_create_transport - create an RPC transport
  1091. * @args: rpc transport creation arguments
  1092. *
  1093. */
  1094. struct rpc_xprt *xprt_create_transport(struct xprt_create *args)
  1095. {
  1096. struct rpc_xprt *xprt;
  1097. struct xprt_class *t;
  1098. spin_lock(&xprt_list_lock);
  1099. list_for_each_entry(t, &xprt_list, list) {
  1100. if (t->ident == args->ident) {
  1101. spin_unlock(&xprt_list_lock);
  1102. goto found;
  1103. }
  1104. }
  1105. spin_unlock(&xprt_list_lock);
  1106. printk(KERN_ERR "RPC: transport (%d) not supported\n", args->ident);
  1107. return ERR_PTR(-EIO);
  1108. found:
  1109. xprt = t->setup(args);
  1110. if (IS_ERR(xprt)) {
  1111. dprintk("RPC: xprt_create_transport: failed, %ld\n",
  1112. -PTR_ERR(xprt));
  1113. goto out;
  1114. }
  1115. INIT_WORK(&xprt->task_cleanup, xprt_autoclose);
  1116. if (xprt_has_timer(xprt))
  1117. setup_timer(&xprt->timer, xprt_init_autodisconnect,
  1118. (unsigned long)xprt);
  1119. else
  1120. init_timer(&xprt->timer);
  1121. if (strlen(args->servername) > RPC_MAXNETNAMELEN) {
  1122. xprt_destroy(xprt);
  1123. return ERR_PTR(-EINVAL);
  1124. }
  1125. xprt->servername = kstrdup(args->servername, GFP_KERNEL);
  1126. if (xprt->servername == NULL) {
  1127. xprt_destroy(xprt);
  1128. return ERR_PTR(-ENOMEM);
  1129. }
  1130. dprintk("RPC: created transport %p with %u slots\n", xprt,
  1131. xprt->max_reqs);
  1132. out:
  1133. return xprt;
  1134. }
  1135. /**
  1136. * xprt_destroy - destroy an RPC transport, killing off all requests.
  1137. * @xprt: transport to destroy
  1138. *
  1139. */
  1140. static void xprt_destroy(struct rpc_xprt *xprt)
  1141. {
  1142. dprintk("RPC: destroying transport %p\n", xprt);
  1143. xprt->shutdown = 1;
  1144. del_timer_sync(&xprt->timer);
  1145. rpc_destroy_wait_queue(&xprt->binding);
  1146. rpc_destroy_wait_queue(&xprt->pending);
  1147. rpc_destroy_wait_queue(&xprt->sending);
  1148. rpc_destroy_wait_queue(&xprt->backlog);
  1149. cancel_work_sync(&xprt->task_cleanup);
  1150. kfree(xprt->servername);
  1151. /*
  1152. * Tear down transport state and free the rpc_xprt
  1153. */
  1154. xprt->ops->destroy(xprt);
  1155. }
  1156. /**
  1157. * xprt_put - release a reference to an RPC transport.
  1158. * @xprt: pointer to the transport
  1159. *
  1160. */
  1161. void xprt_put(struct rpc_xprt *xprt)
  1162. {
  1163. if (atomic_dec_and_test(&xprt->count))
  1164. xprt_destroy(xprt);
  1165. }
  1166. /**
  1167. * xprt_get - return a reference to an RPC transport.
  1168. * @xprt: pointer to the transport
  1169. *
  1170. */
  1171. struct rpc_xprt *xprt_get(struct rpc_xprt *xprt)
  1172. {
  1173. if (atomic_inc_not_zero(&xprt->count))
  1174. return xprt;
  1175. return NULL;
  1176. }