dm-queue-length.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2004-2005 IBM Corp. All Rights Reserved.
  3. * Copyright (C) 2006-2009 NEC Corporation.
  4. *
  5. * dm-queue-length.c
  6. *
  7. * Module Author: Stefan Bader, IBM
  8. * Modified by: Kiyoshi Ueda, NEC
  9. *
  10. * This file is released under the GPL.
  11. *
  12. * queue-length path selector - choose a path with the least number of
  13. * in-flight I/Os.
  14. */
  15. #include "dm.h"
  16. #include "dm-path-selector.h"
  17. #include <linux/slab.h>
  18. #include <linux/ctype.h>
  19. #include <linux/errno.h>
  20. #include <linux/module.h>
  21. #include <linux/atomic.h>
  22. #define DM_MSG_PREFIX "multipath queue-length"
  23. #define QL_MIN_IO 128
  24. #define QL_VERSION "0.1.0"
  25. struct selector {
  26. struct list_head valid_paths;
  27. struct list_head failed_paths;
  28. };
  29. struct path_info {
  30. struct list_head list;
  31. struct dm_path *path;
  32. unsigned repeat_count;
  33. atomic_t qlen; /* the number of in-flight I/Os */
  34. };
  35. static struct selector *alloc_selector(void)
  36. {
  37. struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
  38. if (s) {
  39. INIT_LIST_HEAD(&s->valid_paths);
  40. INIT_LIST_HEAD(&s->failed_paths);
  41. }
  42. return s;
  43. }
  44. static int ql_create(struct path_selector *ps, unsigned argc, char **argv)
  45. {
  46. struct selector *s = alloc_selector();
  47. if (!s)
  48. return -ENOMEM;
  49. ps->context = s;
  50. return 0;
  51. }
  52. static void ql_free_paths(struct list_head *paths)
  53. {
  54. struct path_info *pi, *next;
  55. list_for_each_entry_safe(pi, next, paths, list) {
  56. list_del(&pi->list);
  57. kfree(pi);
  58. }
  59. }
  60. static void ql_destroy(struct path_selector *ps)
  61. {
  62. struct selector *s = ps->context;
  63. ql_free_paths(&s->valid_paths);
  64. ql_free_paths(&s->failed_paths);
  65. kfree(s);
  66. ps->context = NULL;
  67. }
  68. static int ql_status(struct path_selector *ps, struct dm_path *path,
  69. status_type_t type, char *result, unsigned maxlen)
  70. {
  71. unsigned sz = 0;
  72. struct path_info *pi;
  73. /* When called with NULL path, return selector status/args. */
  74. if (!path)
  75. DMEMIT("0 ");
  76. else {
  77. pi = path->pscontext;
  78. switch (type) {
  79. case STATUSTYPE_INFO:
  80. DMEMIT("%d ", atomic_read(&pi->qlen));
  81. break;
  82. case STATUSTYPE_TABLE:
  83. DMEMIT("%u ", pi->repeat_count);
  84. break;
  85. }
  86. }
  87. return sz;
  88. }
  89. static int ql_add_path(struct path_selector *ps, struct dm_path *path,
  90. int argc, char **argv, char **error)
  91. {
  92. struct selector *s = ps->context;
  93. struct path_info *pi;
  94. unsigned repeat_count = QL_MIN_IO;
  95. char dummy;
  96. /*
  97. * Arguments: [<repeat_count>]
  98. * <repeat_count>: The number of I/Os before switching path.
  99. * If not given, default (QL_MIN_IO) is used.
  100. */
  101. if (argc > 1) {
  102. *error = "queue-length ps: incorrect number of arguments";
  103. return -EINVAL;
  104. }
  105. if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
  106. *error = "queue-length ps: invalid repeat count";
  107. return -EINVAL;
  108. }
  109. /* Allocate the path information structure */
  110. pi = kmalloc(sizeof(*pi), GFP_KERNEL);
  111. if (!pi) {
  112. *error = "queue-length ps: Error allocating path information";
  113. return -ENOMEM;
  114. }
  115. pi->path = path;
  116. pi->repeat_count = repeat_count;
  117. atomic_set(&pi->qlen, 0);
  118. path->pscontext = pi;
  119. list_add_tail(&pi->list, &s->valid_paths);
  120. return 0;
  121. }
  122. static void ql_fail_path(struct path_selector *ps, struct dm_path *path)
  123. {
  124. struct selector *s = ps->context;
  125. struct path_info *pi = path->pscontext;
  126. list_move(&pi->list, &s->failed_paths);
  127. }
  128. static int ql_reinstate_path(struct path_selector *ps, struct dm_path *path)
  129. {
  130. struct selector *s = ps->context;
  131. struct path_info *pi = path->pscontext;
  132. list_move_tail(&pi->list, &s->valid_paths);
  133. return 0;
  134. }
  135. /*
  136. * Select a path having the minimum number of in-flight I/Os
  137. */
  138. static struct dm_path *ql_select_path(struct path_selector *ps,
  139. unsigned *repeat_count, size_t nr_bytes)
  140. {
  141. struct selector *s = ps->context;
  142. struct path_info *pi = NULL, *best = NULL;
  143. if (list_empty(&s->valid_paths))
  144. return NULL;
  145. /* Change preferred (first in list) path to evenly balance. */
  146. list_move_tail(s->valid_paths.next, &s->valid_paths);
  147. list_for_each_entry(pi, &s->valid_paths, list) {
  148. if (!best ||
  149. (atomic_read(&pi->qlen) < atomic_read(&best->qlen)))
  150. best = pi;
  151. if (!atomic_read(&best->qlen))
  152. break;
  153. }
  154. if (!best)
  155. return NULL;
  156. *repeat_count = best->repeat_count;
  157. return best->path;
  158. }
  159. static int ql_start_io(struct path_selector *ps, struct dm_path *path,
  160. size_t nr_bytes)
  161. {
  162. struct path_info *pi = path->pscontext;
  163. atomic_inc(&pi->qlen);
  164. return 0;
  165. }
  166. static int ql_end_io(struct path_selector *ps, struct dm_path *path,
  167. size_t nr_bytes)
  168. {
  169. struct path_info *pi = path->pscontext;
  170. atomic_dec(&pi->qlen);
  171. return 0;
  172. }
  173. static struct path_selector_type ql_ps = {
  174. .name = "queue-length",
  175. .module = THIS_MODULE,
  176. .table_args = 1,
  177. .info_args = 1,
  178. .create = ql_create,
  179. .destroy = ql_destroy,
  180. .status = ql_status,
  181. .add_path = ql_add_path,
  182. .fail_path = ql_fail_path,
  183. .reinstate_path = ql_reinstate_path,
  184. .select_path = ql_select_path,
  185. .start_io = ql_start_io,
  186. .end_io = ql_end_io,
  187. };
  188. static int __init dm_ql_init(void)
  189. {
  190. int r = dm_register_path_selector(&ql_ps);
  191. if (r < 0)
  192. DMERR("register failed %d", r);
  193. DMINFO("version " QL_VERSION " loaded");
  194. return r;
  195. }
  196. static void __exit dm_ql_exit(void)
  197. {
  198. int r = dm_unregister_path_selector(&ql_ps);
  199. if (r < 0)
  200. DMERR("unregister failed %d", r);
  201. }
  202. module_init(dm_ql_init);
  203. module_exit(dm_ql_exit);
  204. MODULE_AUTHOR("Stefan Bader <Stefan.Bader at de.ibm.com>");
  205. MODULE_DESCRIPTION(
  206. "(C) Copyright IBM Corp. 2004,2005 All Rights Reserved.\n"
  207. DM_NAME " path selector to balance the number of in-flight I/Os"
  208. );
  209. MODULE_LICENSE("GPL");