xprtmultipath.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Multipath support for RPC
  3. *
  4. * Copyright (c) 2015, 2016, Primary Data, Inc. All rights reserved.
  5. *
  6. * Trond Myklebust <trond.myklebust@primarydata.com>
  7. *
  8. */
  9. #include <linux/types.h>
  10. #include <linux/kref.h>
  11. #include <linux/list.h>
  12. #include <linux/rcupdate.h>
  13. #include <linux/rculist.h>
  14. #include <linux/slab.h>
  15. #include <asm/cmpxchg.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/sunrpc/xprt.h>
  18. #include <linux/sunrpc/xprtmultipath.h>
  19. typedef struct rpc_xprt *(*xprt_switch_find_xprt_t)(struct list_head *head,
  20. const struct rpc_xprt *cur);
  21. static const struct rpc_xprt_iter_ops rpc_xprt_iter_singular;
  22. static const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin;
  23. static const struct rpc_xprt_iter_ops rpc_xprt_iter_listall;
  24. static void xprt_switch_add_xprt_locked(struct rpc_xprt_switch *xps,
  25. struct rpc_xprt *xprt)
  26. {
  27. if (unlikely(xprt_get(xprt) == NULL))
  28. return;
  29. list_add_tail_rcu(&xprt->xprt_switch, &xps->xps_xprt_list);
  30. smp_wmb();
  31. if (xps->xps_nxprts == 0)
  32. xps->xps_net = xprt->xprt_net;
  33. xps->xps_nxprts++;
  34. }
  35. /**
  36. * rpc_xprt_switch_add_xprt - Add a new rpc_xprt to an rpc_xprt_switch
  37. * @xps: pointer to struct rpc_xprt_switch
  38. * @xprt: pointer to struct rpc_xprt
  39. *
  40. * Adds xprt to the end of the list of struct rpc_xprt in xps.
  41. */
  42. void rpc_xprt_switch_add_xprt(struct rpc_xprt_switch *xps,
  43. struct rpc_xprt *xprt)
  44. {
  45. if (xprt == NULL)
  46. return;
  47. spin_lock(&xps->xps_lock);
  48. if (xps->xps_net == xprt->xprt_net || xps->xps_net == NULL)
  49. xprt_switch_add_xprt_locked(xps, xprt);
  50. spin_unlock(&xps->xps_lock);
  51. }
  52. static void xprt_switch_remove_xprt_locked(struct rpc_xprt_switch *xps,
  53. struct rpc_xprt *xprt)
  54. {
  55. if (unlikely(xprt == NULL))
  56. return;
  57. xps->xps_nxprts--;
  58. if (xps->xps_nxprts == 0)
  59. xps->xps_net = NULL;
  60. smp_wmb();
  61. list_del_rcu(&xprt->xprt_switch);
  62. }
  63. /**
  64. * rpc_xprt_switch_remove_xprt - Removes an rpc_xprt from a rpc_xprt_switch
  65. * @xps: pointer to struct rpc_xprt_switch
  66. * @xprt: pointer to struct rpc_xprt
  67. *
  68. * Removes xprt from the list of struct rpc_xprt in xps.
  69. */
  70. void rpc_xprt_switch_remove_xprt(struct rpc_xprt_switch *xps,
  71. struct rpc_xprt *xprt)
  72. {
  73. spin_lock(&xps->xps_lock);
  74. xprt_switch_remove_xprt_locked(xps, xprt);
  75. spin_unlock(&xps->xps_lock);
  76. xprt_put(xprt);
  77. }
  78. /**
  79. * xprt_switch_alloc - Allocate a new struct rpc_xprt_switch
  80. * @xprt: pointer to struct rpc_xprt
  81. * @gfp_flags: allocation flags
  82. *
  83. * On success, returns an initialised struct rpc_xprt_switch, containing
  84. * the entry xprt. Returns NULL on failure.
  85. */
  86. struct rpc_xprt_switch *xprt_switch_alloc(struct rpc_xprt *xprt,
  87. gfp_t gfp_flags)
  88. {
  89. struct rpc_xprt_switch *xps;
  90. xps = kmalloc(sizeof(*xps), gfp_flags);
  91. if (xps != NULL) {
  92. spin_lock_init(&xps->xps_lock);
  93. kref_init(&xps->xps_kref);
  94. xps->xps_nxprts = 0;
  95. INIT_LIST_HEAD(&xps->xps_xprt_list);
  96. xps->xps_iter_ops = &rpc_xprt_iter_singular;
  97. xprt_switch_add_xprt_locked(xps, xprt);
  98. }
  99. return xps;
  100. }
  101. static void xprt_switch_free_entries(struct rpc_xprt_switch *xps)
  102. {
  103. spin_lock(&xps->xps_lock);
  104. while (!list_empty(&xps->xps_xprt_list)) {
  105. struct rpc_xprt *xprt;
  106. xprt = list_first_entry(&xps->xps_xprt_list,
  107. struct rpc_xprt, xprt_switch);
  108. xprt_switch_remove_xprt_locked(xps, xprt);
  109. spin_unlock(&xps->xps_lock);
  110. xprt_put(xprt);
  111. spin_lock(&xps->xps_lock);
  112. }
  113. spin_unlock(&xps->xps_lock);
  114. }
  115. static void xprt_switch_free(struct kref *kref)
  116. {
  117. struct rpc_xprt_switch *xps = container_of(kref,
  118. struct rpc_xprt_switch, xps_kref);
  119. xprt_switch_free_entries(xps);
  120. kfree_rcu(xps, xps_rcu);
  121. }
  122. /**
  123. * xprt_switch_get - Return a reference to a rpc_xprt_switch
  124. * @xps: pointer to struct rpc_xprt_switch
  125. *
  126. * Returns a reference to xps unless the refcount is already zero.
  127. */
  128. struct rpc_xprt_switch *xprt_switch_get(struct rpc_xprt_switch *xps)
  129. {
  130. if (xps != NULL && kref_get_unless_zero(&xps->xps_kref))
  131. return xps;
  132. return NULL;
  133. }
  134. /**
  135. * xprt_switch_put - Release a reference to a rpc_xprt_switch
  136. * @xps: pointer to struct rpc_xprt_switch
  137. *
  138. * Release the reference to xps, and free it once the refcount is zero.
  139. */
  140. void xprt_switch_put(struct rpc_xprt_switch *xps)
  141. {
  142. if (xps != NULL)
  143. kref_put(&xps->xps_kref, xprt_switch_free);
  144. }
  145. /**
  146. * rpc_xprt_switch_set_roundrobin - Set a round-robin policy on rpc_xprt_switch
  147. * @xps: pointer to struct rpc_xprt_switch
  148. *
  149. * Sets a round-robin default policy for iterators acting on xps.
  150. */
  151. void rpc_xprt_switch_set_roundrobin(struct rpc_xprt_switch *xps)
  152. {
  153. if (READ_ONCE(xps->xps_iter_ops) != &rpc_xprt_iter_roundrobin)
  154. WRITE_ONCE(xps->xps_iter_ops, &rpc_xprt_iter_roundrobin);
  155. }
  156. static
  157. const struct rpc_xprt_iter_ops *xprt_iter_ops(const struct rpc_xprt_iter *xpi)
  158. {
  159. if (xpi->xpi_ops != NULL)
  160. return xpi->xpi_ops;
  161. return rcu_dereference(xpi->xpi_xpswitch)->xps_iter_ops;
  162. }
  163. static
  164. void xprt_iter_no_rewind(struct rpc_xprt_iter *xpi)
  165. {
  166. }
  167. static
  168. void xprt_iter_default_rewind(struct rpc_xprt_iter *xpi)
  169. {
  170. WRITE_ONCE(xpi->xpi_cursor, NULL);
  171. }
  172. static
  173. struct rpc_xprt *xprt_switch_find_first_entry(struct list_head *head)
  174. {
  175. return list_first_or_null_rcu(head, struct rpc_xprt, xprt_switch);
  176. }
  177. static
  178. struct rpc_xprt *xprt_iter_first_entry(struct rpc_xprt_iter *xpi)
  179. {
  180. struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
  181. if (xps == NULL)
  182. return NULL;
  183. return xprt_switch_find_first_entry(&xps->xps_xprt_list);
  184. }
  185. static
  186. struct rpc_xprt *xprt_switch_find_current_entry(struct list_head *head,
  187. const struct rpc_xprt *cur)
  188. {
  189. struct rpc_xprt *pos;
  190. list_for_each_entry_rcu(pos, head, xprt_switch) {
  191. if (cur == pos)
  192. return pos;
  193. }
  194. return NULL;
  195. }
  196. static
  197. struct rpc_xprt *xprt_iter_current_entry(struct rpc_xprt_iter *xpi)
  198. {
  199. struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
  200. struct list_head *head;
  201. if (xps == NULL)
  202. return NULL;
  203. head = &xps->xps_xprt_list;
  204. if (xpi->xpi_cursor == NULL || xps->xps_nxprts < 2)
  205. return xprt_switch_find_first_entry(head);
  206. return xprt_switch_find_current_entry(head, xpi->xpi_cursor);
  207. }
  208. static
  209. struct rpc_xprt *xprt_switch_find_next_entry(struct list_head *head,
  210. const struct rpc_xprt *cur)
  211. {
  212. struct rpc_xprt *pos, *prev = NULL;
  213. list_for_each_entry_rcu(pos, head, xprt_switch) {
  214. if (cur == prev)
  215. return pos;
  216. prev = pos;
  217. }
  218. return NULL;
  219. }
  220. static
  221. struct rpc_xprt *xprt_switch_set_next_cursor(struct list_head *head,
  222. struct rpc_xprt **cursor,
  223. xprt_switch_find_xprt_t find_next)
  224. {
  225. struct rpc_xprt *cur, *pos, *old;
  226. cur = READ_ONCE(*cursor);
  227. for (;;) {
  228. old = cur;
  229. pos = find_next(head, old);
  230. if (pos == NULL)
  231. break;
  232. cur = cmpxchg_relaxed(cursor, old, pos);
  233. if (cur == old)
  234. break;
  235. }
  236. return pos;
  237. }
  238. static
  239. struct rpc_xprt *xprt_iter_next_entry_multiple(struct rpc_xprt_iter *xpi,
  240. xprt_switch_find_xprt_t find_next)
  241. {
  242. struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
  243. struct list_head *head;
  244. if (xps == NULL)
  245. return NULL;
  246. head = &xps->xps_xprt_list;
  247. if (xps->xps_nxprts < 2)
  248. return xprt_switch_find_first_entry(head);
  249. return xprt_switch_set_next_cursor(head, &xpi->xpi_cursor, find_next);
  250. }
  251. static
  252. struct rpc_xprt *xprt_switch_find_next_entry_roundrobin(struct list_head *head,
  253. const struct rpc_xprt *cur)
  254. {
  255. struct rpc_xprt *ret;
  256. ret = xprt_switch_find_next_entry(head, cur);
  257. if (ret != NULL)
  258. return ret;
  259. return xprt_switch_find_first_entry(head);
  260. }
  261. static
  262. struct rpc_xprt *xprt_iter_next_entry_roundrobin(struct rpc_xprt_iter *xpi)
  263. {
  264. return xprt_iter_next_entry_multiple(xpi,
  265. xprt_switch_find_next_entry_roundrobin);
  266. }
  267. static
  268. struct rpc_xprt *xprt_iter_next_entry_all(struct rpc_xprt_iter *xpi)
  269. {
  270. return xprt_iter_next_entry_multiple(xpi, xprt_switch_find_next_entry);
  271. }
  272. /*
  273. * xprt_iter_rewind - Resets the xprt iterator
  274. * @xpi: pointer to rpc_xprt_iter
  275. *
  276. * Resets xpi to ensure that it points to the first entry in the list
  277. * of transports.
  278. */
  279. static
  280. void xprt_iter_rewind(struct rpc_xprt_iter *xpi)
  281. {
  282. rcu_read_lock();
  283. xprt_iter_ops(xpi)->xpi_rewind(xpi);
  284. rcu_read_unlock();
  285. }
  286. static void __xprt_iter_init(struct rpc_xprt_iter *xpi,
  287. struct rpc_xprt_switch *xps,
  288. const struct rpc_xprt_iter_ops *ops)
  289. {
  290. rcu_assign_pointer(xpi->xpi_xpswitch, xprt_switch_get(xps));
  291. xpi->xpi_cursor = NULL;
  292. xpi->xpi_ops = ops;
  293. }
  294. /**
  295. * xprt_iter_init - Initialise an xprt iterator
  296. * @xpi: pointer to rpc_xprt_iter
  297. * @xps: pointer to rpc_xprt_switch
  298. *
  299. * Initialises the iterator to use the default iterator ops
  300. * as set in xps. This function is mainly intended for internal
  301. * use in the rpc_client.
  302. */
  303. void xprt_iter_init(struct rpc_xprt_iter *xpi,
  304. struct rpc_xprt_switch *xps)
  305. {
  306. __xprt_iter_init(xpi, xps, NULL);
  307. }
  308. /**
  309. * xprt_iter_init_listall - Initialise an xprt iterator
  310. * @xpi: pointer to rpc_xprt_iter
  311. * @xps: pointer to rpc_xprt_switch
  312. *
  313. * Initialises the iterator to iterate once through the entire list
  314. * of entries in xps.
  315. */
  316. void xprt_iter_init_listall(struct rpc_xprt_iter *xpi,
  317. struct rpc_xprt_switch *xps)
  318. {
  319. __xprt_iter_init(xpi, xps, &rpc_xprt_iter_listall);
  320. }
  321. /**
  322. * xprt_iter_xchg_switch - Atomically swap out the rpc_xprt_switch
  323. * @xpi: pointer to rpc_xprt_iter
  324. * @xps: pointer to a new rpc_xprt_switch or NULL
  325. *
  326. * Swaps out the existing xpi->xpi_xpswitch with a new value.
  327. */
  328. struct rpc_xprt_switch *xprt_iter_xchg_switch(struct rpc_xprt_iter *xpi,
  329. struct rpc_xprt_switch *newswitch)
  330. {
  331. struct rpc_xprt_switch __rcu *oldswitch;
  332. /* Atomically swap out the old xpswitch */
  333. oldswitch = xchg(&xpi->xpi_xpswitch, RCU_INITIALIZER(newswitch));
  334. if (newswitch != NULL)
  335. xprt_iter_rewind(xpi);
  336. return rcu_dereference_protected(oldswitch, true);
  337. }
  338. /**
  339. * xprt_iter_destroy - Destroys the xprt iterator
  340. * @xpi pointer to rpc_xprt_iter
  341. */
  342. void xprt_iter_destroy(struct rpc_xprt_iter *xpi)
  343. {
  344. xprt_switch_put(xprt_iter_xchg_switch(xpi, NULL));
  345. }
  346. /**
  347. * xprt_iter_xprt - Returns the rpc_xprt pointed to by the cursor
  348. * @xpi: pointer to rpc_xprt_iter
  349. *
  350. * Returns a pointer to the struct rpc_xprt that is currently
  351. * pointed to by the cursor.
  352. * Caller must be holding rcu_read_lock().
  353. */
  354. struct rpc_xprt *xprt_iter_xprt(struct rpc_xprt_iter *xpi)
  355. {
  356. WARN_ON_ONCE(!rcu_read_lock_held());
  357. return xprt_iter_ops(xpi)->xpi_xprt(xpi);
  358. }
  359. static
  360. struct rpc_xprt *xprt_iter_get_helper(struct rpc_xprt_iter *xpi,
  361. struct rpc_xprt *(*fn)(struct rpc_xprt_iter *))
  362. {
  363. struct rpc_xprt *ret;
  364. do {
  365. ret = fn(xpi);
  366. if (ret == NULL)
  367. break;
  368. ret = xprt_get(ret);
  369. } while (ret == NULL);
  370. return ret;
  371. }
  372. /**
  373. * xprt_iter_get_xprt - Returns the rpc_xprt pointed to by the cursor
  374. * @xpi: pointer to rpc_xprt_iter
  375. *
  376. * Returns a reference to the struct rpc_xprt that is currently
  377. * pointed to by the cursor.
  378. */
  379. struct rpc_xprt *xprt_iter_get_xprt(struct rpc_xprt_iter *xpi)
  380. {
  381. struct rpc_xprt *xprt;
  382. rcu_read_lock();
  383. xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_xprt);
  384. rcu_read_unlock();
  385. return xprt;
  386. }
  387. /**
  388. * xprt_iter_get_next - Returns the next rpc_xprt following the cursor
  389. * @xpi: pointer to rpc_xprt_iter
  390. *
  391. * Returns a reference to the struct rpc_xprt that immediately follows the
  392. * entry pointed to by the cursor.
  393. */
  394. struct rpc_xprt *xprt_iter_get_next(struct rpc_xprt_iter *xpi)
  395. {
  396. struct rpc_xprt *xprt;
  397. rcu_read_lock();
  398. xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_next);
  399. rcu_read_unlock();
  400. return xprt;
  401. }
  402. /* Policy for always returning the first entry in the rpc_xprt_switch */
  403. static
  404. const struct rpc_xprt_iter_ops rpc_xprt_iter_singular = {
  405. .xpi_rewind = xprt_iter_no_rewind,
  406. .xpi_xprt = xprt_iter_first_entry,
  407. .xpi_next = xprt_iter_first_entry,
  408. };
  409. /* Policy for round-robin iteration of entries in the rpc_xprt_switch */
  410. static
  411. const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin = {
  412. .xpi_rewind = xprt_iter_default_rewind,
  413. .xpi_xprt = xprt_iter_current_entry,
  414. .xpi_next = xprt_iter_next_entry_roundrobin,
  415. };
  416. /* Policy for once-through iteration of entries in the rpc_xprt_switch */
  417. static
  418. const struct rpc_xprt_iter_ops rpc_xprt_iter_listall = {
  419. .xpi_rewind = xprt_iter_default_rewind,
  420. .xpi_xprt = xprt_iter_current_entry,
  421. .xpi_next = xprt_iter_next_entry_all,
  422. };