dm-round-robin.c 6.0 KB

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