xprtmultipath.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. if (xps == NULL)
  244. return NULL;
  245. return xprt_switch_set_next_cursor(&xps->xps_xprt_list,
  246. &xpi->xpi_cursor,
  247. find_next);
  248. }
  249. static
  250. struct rpc_xprt *xprt_switch_find_next_entry_roundrobin(struct list_head *head,
  251. const struct rpc_xprt *cur)
  252. {
  253. struct rpc_xprt *ret;
  254. ret = xprt_switch_find_next_entry(head, cur);
  255. if (ret != NULL)
  256. return ret;
  257. return xprt_switch_find_first_entry(head);
  258. }
  259. static
  260. struct rpc_xprt *xprt_iter_next_entry_roundrobin(struct rpc_xprt_iter *xpi)
  261. {
  262. return xprt_iter_next_entry_multiple(xpi,
  263. xprt_switch_find_next_entry_roundrobin);
  264. }
  265. static
  266. struct rpc_xprt *xprt_iter_next_entry_all(struct rpc_xprt_iter *xpi)
  267. {
  268. return xprt_iter_next_entry_multiple(xpi, xprt_switch_find_next_entry);
  269. }
  270. /*
  271. * xprt_iter_rewind - Resets the xprt iterator
  272. * @xpi: pointer to rpc_xprt_iter
  273. *
  274. * Resets xpi to ensure that it points to the first entry in the list
  275. * of transports.
  276. */
  277. static
  278. void xprt_iter_rewind(struct rpc_xprt_iter *xpi)
  279. {
  280. rcu_read_lock();
  281. xprt_iter_ops(xpi)->xpi_rewind(xpi);
  282. rcu_read_unlock();
  283. }
  284. static void __xprt_iter_init(struct rpc_xprt_iter *xpi,
  285. struct rpc_xprt_switch *xps,
  286. const struct rpc_xprt_iter_ops *ops)
  287. {
  288. rcu_assign_pointer(xpi->xpi_xpswitch, xprt_switch_get(xps));
  289. xpi->xpi_cursor = NULL;
  290. xpi->xpi_ops = ops;
  291. }
  292. /**
  293. * xprt_iter_init - Initialise an xprt iterator
  294. * @xpi: pointer to rpc_xprt_iter
  295. * @xps: pointer to rpc_xprt_switch
  296. *
  297. * Initialises the iterator to use the default iterator ops
  298. * as set in xps. This function is mainly intended for internal
  299. * use in the rpc_client.
  300. */
  301. void xprt_iter_init(struct rpc_xprt_iter *xpi,
  302. struct rpc_xprt_switch *xps)
  303. {
  304. __xprt_iter_init(xpi, xps, NULL);
  305. }
  306. /**
  307. * xprt_iter_init_listall - Initialise an xprt iterator
  308. * @xpi: pointer to rpc_xprt_iter
  309. * @xps: pointer to rpc_xprt_switch
  310. *
  311. * Initialises the iterator to iterate once through the entire list
  312. * of entries in xps.
  313. */
  314. void xprt_iter_init_listall(struct rpc_xprt_iter *xpi,
  315. struct rpc_xprt_switch *xps)
  316. {
  317. __xprt_iter_init(xpi, xps, &rpc_xprt_iter_listall);
  318. }
  319. /**
  320. * xprt_iter_xchg_switch - Atomically swap out the rpc_xprt_switch
  321. * @xpi: pointer to rpc_xprt_iter
  322. * @xps: pointer to a new rpc_xprt_switch or NULL
  323. *
  324. * Swaps out the existing xpi->xpi_xpswitch with a new value.
  325. */
  326. struct rpc_xprt_switch *xprt_iter_xchg_switch(struct rpc_xprt_iter *xpi,
  327. struct rpc_xprt_switch *newswitch)
  328. {
  329. struct rpc_xprt_switch __rcu *oldswitch;
  330. /* Atomically swap out the old xpswitch */
  331. oldswitch = xchg(&xpi->xpi_xpswitch, RCU_INITIALIZER(newswitch));
  332. if (newswitch != NULL)
  333. xprt_iter_rewind(xpi);
  334. return rcu_dereference_protected(oldswitch, true);
  335. }
  336. /**
  337. * xprt_iter_destroy - Destroys the xprt iterator
  338. * @xpi pointer to rpc_xprt_iter
  339. */
  340. void xprt_iter_destroy(struct rpc_xprt_iter *xpi)
  341. {
  342. xprt_switch_put(xprt_iter_xchg_switch(xpi, NULL));
  343. }
  344. /**
  345. * xprt_iter_xprt - Returns the rpc_xprt pointed to by the cursor
  346. * @xpi: pointer to rpc_xprt_iter
  347. *
  348. * Returns a pointer to the struct rpc_xprt that is currently
  349. * pointed to by the cursor.
  350. * Caller must be holding rcu_read_lock().
  351. */
  352. struct rpc_xprt *xprt_iter_xprt(struct rpc_xprt_iter *xpi)
  353. {
  354. WARN_ON_ONCE(!rcu_read_lock_held());
  355. return xprt_iter_ops(xpi)->xpi_xprt(xpi);
  356. }
  357. static
  358. struct rpc_xprt *xprt_iter_get_helper(struct rpc_xprt_iter *xpi,
  359. struct rpc_xprt *(*fn)(struct rpc_xprt_iter *))
  360. {
  361. struct rpc_xprt *ret;
  362. do {
  363. ret = fn(xpi);
  364. if (ret == NULL)
  365. break;
  366. ret = xprt_get(ret);
  367. } while (ret == NULL);
  368. return ret;
  369. }
  370. /**
  371. * xprt_iter_get_xprt - Returns the rpc_xprt pointed to by the cursor
  372. * @xpi: pointer to rpc_xprt_iter
  373. *
  374. * Returns a reference to the struct rpc_xprt that is currently
  375. * pointed to by the cursor.
  376. */
  377. struct rpc_xprt *xprt_iter_get_xprt(struct rpc_xprt_iter *xpi)
  378. {
  379. struct rpc_xprt *xprt;
  380. rcu_read_lock();
  381. xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_xprt);
  382. rcu_read_unlock();
  383. return xprt;
  384. }
  385. /**
  386. * xprt_iter_get_next - Returns the next rpc_xprt following the cursor
  387. * @xpi: pointer to rpc_xprt_iter
  388. *
  389. * Returns a reference to the struct rpc_xprt that immediately follows the
  390. * entry pointed to by the cursor.
  391. */
  392. struct rpc_xprt *xprt_iter_get_next(struct rpc_xprt_iter *xpi)
  393. {
  394. struct rpc_xprt *xprt;
  395. rcu_read_lock();
  396. xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_next);
  397. rcu_read_unlock();
  398. return xprt;
  399. }
  400. /* Policy for always returning the first entry in the rpc_xprt_switch */
  401. static
  402. const struct rpc_xprt_iter_ops rpc_xprt_iter_singular = {
  403. .xpi_rewind = xprt_iter_no_rewind,
  404. .xpi_xprt = xprt_iter_first_entry,
  405. .xpi_next = xprt_iter_first_entry,
  406. };
  407. /* Policy for round-robin iteration of entries in the rpc_xprt_switch */
  408. static
  409. const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin = {
  410. .xpi_rewind = xprt_iter_default_rewind,
  411. .xpi_xprt = xprt_iter_current_entry,
  412. .xpi_next = xprt_iter_next_entry_roundrobin,
  413. };
  414. /* Policy for once-through iteration of entries in the rpc_xprt_switch */
  415. static
  416. const struct rpc_xprt_iter_ops rpc_xprt_iter_listall = {
  417. .xpi_rewind = xprt_iter_default_rewind,
  418. .xpi_xprt = xprt_iter_current_entry,
  419. .xpi_next = xprt_iter_next_entry_all,
  420. };