xprt.c 34 KB

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