xprt.c 38 KB

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