xprt.c 38 KB

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