dm-round-robin.c 4.6 KB

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