dm-round-robin.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 1
  17. #define RR_VERSION "1.2.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. };
  42. static struct selector *alloc_selector(void)
  43. {
  44. struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
  45. if (s) {
  46. INIT_LIST_HEAD(&s->valid_paths);
  47. INIT_LIST_HEAD(&s->invalid_paths);
  48. spin_lock_init(&s->lock);
  49. }
  50. return s;
  51. }
  52. static int rr_create(struct path_selector *ps, unsigned argc, char **argv)
  53. {
  54. struct selector *s;
  55. s = alloc_selector();
  56. if (!s)
  57. return -ENOMEM;
  58. ps->context = s;
  59. return 0;
  60. }
  61. static void rr_destroy(struct path_selector *ps)
  62. {
  63. struct selector *s = ps->context;
  64. free_paths(&s->valid_paths);
  65. free_paths(&s->invalid_paths);
  66. kfree(s);
  67. ps->context = NULL;
  68. }
  69. static int rr_status(struct path_selector *ps, struct dm_path *path,
  70. status_type_t type, char *result, unsigned int maxlen)
  71. {
  72. struct path_info *pi;
  73. int sz = 0;
  74. if (!path)
  75. DMEMIT("0 ");
  76. else {
  77. switch(type) {
  78. case STATUSTYPE_INFO:
  79. break;
  80. case STATUSTYPE_TABLE:
  81. pi = path->pscontext;
  82. DMEMIT("%u ", pi->repeat_count);
  83. break;
  84. }
  85. }
  86. return sz;
  87. }
  88. /*
  89. * Called during initialisation to register each path with an
  90. * optional repeat_count.
  91. */
  92. static int rr_add_path(struct path_selector *ps, struct dm_path *path,
  93. int argc, char **argv, char **error)
  94. {
  95. struct selector *s = ps->context;
  96. struct path_info *pi;
  97. unsigned repeat_count = RR_MIN_IO;
  98. char dummy;
  99. unsigned long flags;
  100. if (argc > 1) {
  101. *error = "round-robin ps: incorrect number of arguments";
  102. return -EINVAL;
  103. }
  104. /* First path argument is number of I/Os before switching path */
  105. if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
  106. *error = "round-robin ps: invalid repeat count";
  107. return -EINVAL;
  108. }
  109. if (repeat_count > 1) {
  110. DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
  111. repeat_count = 1;
  112. }
  113. /* allocate the path */
  114. pi = kmalloc(sizeof(*pi), GFP_KERNEL);
  115. if (!pi) {
  116. *error = "round-robin ps: Error allocating path context";
  117. return -ENOMEM;
  118. }
  119. pi->path = path;
  120. pi->repeat_count = repeat_count;
  121. path->pscontext = pi;
  122. spin_lock_irqsave(&s->lock, flags);
  123. list_add_tail(&pi->list, &s->valid_paths);
  124. spin_unlock_irqrestore(&s->lock, flags);
  125. return 0;
  126. }
  127. static void rr_fail_path(struct path_selector *ps, struct dm_path *p)
  128. {
  129. unsigned long flags;
  130. struct selector *s = ps->context;
  131. struct path_info *pi = p->pscontext;
  132. spin_lock_irqsave(&s->lock, flags);
  133. list_move(&pi->list, &s->invalid_paths);
  134. spin_unlock_irqrestore(&s->lock, flags);
  135. }
  136. static int rr_reinstate_path(struct path_selector *ps, struct dm_path *p)
  137. {
  138. unsigned long flags;
  139. struct selector *s = ps->context;
  140. struct path_info *pi = p->pscontext;
  141. spin_lock_irqsave(&s->lock, flags);
  142. list_move(&pi->list, &s->valid_paths);
  143. spin_unlock_irqrestore(&s->lock, flags);
  144. return 0;
  145. }
  146. static struct dm_path *rr_select_path(struct path_selector *ps, size_t nr_bytes)
  147. {
  148. unsigned long flags;
  149. struct selector *s = ps->context;
  150. struct path_info *pi = NULL;
  151. spin_lock_irqsave(&s->lock, flags);
  152. if (!list_empty(&s->valid_paths)) {
  153. pi = list_entry(s->valid_paths.next, struct path_info, list);
  154. list_move_tail(&pi->list, &s->valid_paths);
  155. }
  156. spin_unlock_irqrestore(&s->lock, flags);
  157. return pi ? pi->path : NULL;
  158. }
  159. static struct path_selector_type rr_ps = {
  160. .name = "round-robin",
  161. .module = THIS_MODULE,
  162. .table_args = 1,
  163. .info_args = 0,
  164. .create = rr_create,
  165. .destroy = rr_destroy,
  166. .status = rr_status,
  167. .add_path = rr_add_path,
  168. .fail_path = rr_fail_path,
  169. .reinstate_path = rr_reinstate_path,
  170. .select_path = rr_select_path,
  171. };
  172. static int __init dm_rr_init(void)
  173. {
  174. int r = dm_register_path_selector(&rr_ps);
  175. if (r < 0)
  176. DMERR("register failed %d", r);
  177. DMINFO("version " RR_VERSION " loaded");
  178. return r;
  179. }
  180. static void __exit dm_rr_exit(void)
  181. {
  182. int r = dm_unregister_path_selector(&rr_ps);
  183. if (r < 0)
  184. DMERR("unregister failed %d", r);
  185. }
  186. module_init(dm_rr_init);
  187. module_exit(dm_rr_exit);
  188. MODULE_DESCRIPTION(DM_NAME " round-robin multipath path selector");
  189. MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
  190. MODULE_LICENSE("GPL");