xprt.c 33 KB

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