xprt.c 37 KB

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