xprt.c 33 KB

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