dm-round-robin.c 4.5 KB

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