dm-ps-round-robin.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2003 Sistina Software.
  4. * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
  5. *
  6. * Module Author: Heinz Mauelshagen
  7. *
  8. * This file is released under the GPL.
  9. *
  10. * Round-robin path selector.
  11. */
  12. #include <linux/device-mapper.h>
  13. #include "dm-path-selector.h"
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #define DM_MSG_PREFIX "multipath round-robin"
  17. #define RR_MIN_IO 1
  18. #define RR_VERSION "1.2.0"
  19. /*
  20. *---------------------------------------------------------------
  21. * Path-handling code, paths are held in lists
  22. *---------------------------------------------------------------
  23. */
  24. struct path_info {
  25. struct list_head list;
  26. struct dm_path *path;
  27. unsigned int repeat_count;
  28. };
  29. static void free_paths(struct list_head *paths)
  30. {
  31. struct path_info *pi, *next;
  32. list_for_each_entry_safe(pi, next, paths, list) {
  33. list_del(&pi->list);
  34. kfree(pi);
  35. }
  36. }
  37. /*
  38. *---------------------------------------------------------------
  39. * Round-robin selector
  40. *---------------------------------------------------------------
  41. */
  42. struct selector {
  43. struct list_head valid_paths;
  44. struct list_head invalid_paths;
  45. spinlock_t lock;
  46. };
  47. static struct selector *alloc_selector(void)
  48. {
  49. struct selector *s = kmalloc_obj(*s);
  50. if (s) {
  51. INIT_LIST_HEAD(&s->valid_paths);
  52. INIT_LIST_HEAD(&s->invalid_paths);
  53. spin_lock_init(&s->lock);
  54. }
  55. return s;
  56. }
  57. static int rr_create(struct path_selector *ps, unsigned int argc, char **argv)
  58. {
  59. struct selector *s;
  60. s = alloc_selector();
  61. if (!s)
  62. return -ENOMEM;
  63. ps->context = s;
  64. return 0;
  65. }
  66. static void rr_destroy(struct path_selector *ps)
  67. {
  68. struct selector *s = ps->context;
  69. free_paths(&s->valid_paths);
  70. free_paths(&s->invalid_paths);
  71. kfree(s);
  72. ps->context = NULL;
  73. }
  74. static int rr_status(struct path_selector *ps, struct dm_path *path,
  75. status_type_t type, char *result, unsigned int maxlen)
  76. {
  77. struct path_info *pi;
  78. int sz = 0;
  79. if (!path)
  80. DMEMIT("0 ");
  81. else {
  82. switch (type) {
  83. case STATUSTYPE_INFO:
  84. break;
  85. case STATUSTYPE_TABLE:
  86. pi = path->pscontext;
  87. DMEMIT("%u ", pi->repeat_count);
  88. break;
  89. case STATUSTYPE_IMA:
  90. *result = '\0';
  91. break;
  92. }
  93. }
  94. return sz;
  95. }
  96. /*
  97. * Called during initialisation to register each path with an
  98. * optional repeat_count.
  99. */
  100. static int rr_add_path(struct path_selector *ps, struct dm_path *path,
  101. int argc, char **argv, char **error)
  102. {
  103. struct selector *s = ps->context;
  104. struct path_info *pi;
  105. unsigned int repeat_count = RR_MIN_IO;
  106. char dummy;
  107. unsigned long flags;
  108. if (argc > 1) {
  109. *error = "round-robin ps: incorrect number of arguments";
  110. return -EINVAL;
  111. }
  112. /* First path argument is number of I/Os before switching path */
  113. if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
  114. *error = "round-robin ps: invalid repeat count";
  115. return -EINVAL;
  116. }
  117. if (repeat_count > 1) {
  118. DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
  119. repeat_count = 1;
  120. }
  121. /* allocate the path */
  122. pi = kmalloc_obj(*pi);
  123. if (!pi) {
  124. *error = "round-robin ps: Error allocating path context";
  125. return -ENOMEM;
  126. }
  127. pi->path = path;
  128. pi->repeat_count = repeat_count;
  129. path->pscontext = pi;
  130. spin_lock_irqsave(&s->lock, flags);
  131. list_add_tail(&pi->list, &s->valid_paths);
  132. spin_unlock_irqrestore(&s->lock, flags);
  133. return 0;
  134. }
  135. static void rr_fail_path(struct path_selector *ps, struct dm_path *p)
  136. {
  137. unsigned long flags;
  138. struct selector *s = ps->context;
  139. struct path_info *pi = p->pscontext;
  140. spin_lock_irqsave(&s->lock, flags);
  141. list_move(&pi->list, &s->invalid_paths);
  142. spin_unlock_irqrestore(&s->lock, flags);
  143. }
  144. static int rr_reinstate_path(struct path_selector *ps, struct dm_path *p)
  145. {
  146. unsigned long flags;
  147. struct selector *s = ps->context;
  148. struct path_info *pi = p->pscontext;
  149. spin_lock_irqsave(&s->lock, flags);
  150. list_move(&pi->list, &s->valid_paths);
  151. spin_unlock_irqrestore(&s->lock, flags);
  152. return 0;
  153. }
  154. static struct dm_path *rr_select_path(struct path_selector *ps, size_t nr_bytes)
  155. {
  156. unsigned long flags;
  157. struct selector *s = ps->context;
  158. struct path_info *pi = NULL;
  159. spin_lock_irqsave(&s->lock, flags);
  160. if (!list_empty(&s->valid_paths)) {
  161. pi = list_entry(s->valid_paths.next, struct path_info, list);
  162. list_move_tail(&pi->list, &s->valid_paths);
  163. }
  164. spin_unlock_irqrestore(&s->lock, flags);
  165. return pi ? pi->path : NULL;
  166. }
  167. static struct path_selector_type rr_ps = {
  168. .name = "round-robin",
  169. .module = THIS_MODULE,
  170. .table_args = 1,
  171. .info_args = 0,
  172. .create = rr_create,
  173. .destroy = rr_destroy,
  174. .status = rr_status,
  175. .add_path = rr_add_path,
  176. .fail_path = rr_fail_path,
  177. .reinstate_path = rr_reinstate_path,
  178. .select_path = rr_select_path,
  179. };
  180. static int __init dm_rr_init(void)
  181. {
  182. int r = dm_register_path_selector(&rr_ps);
  183. if (r < 0) {
  184. DMERR("register failed %d", r);
  185. return r;
  186. }
  187. DMINFO("version " RR_VERSION " loaded");
  188. return r;
  189. }
  190. static void __exit dm_rr_exit(void)
  191. {
  192. dm_unregister_path_selector(&rr_ps);
  193. }
  194. module_init(dm_rr_init);
  195. module_exit(dm_rr_exit);
  196. MODULE_DESCRIPTION(DM_NAME " round-robin multipath path selector");
  197. MODULE_AUTHOR("Sistina Software <dm-devel@lists.linux.dev>");
  198. MODULE_LICENSE("GPL");