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 <asm/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. /*
  96. * Arguments: [<repeat_count>]
  97. * <repeat_count>: The number of I/Os before switching path.
  98. * If not given, default (QL_MIN_IO) is used.
  99. */
  100. if (argc > 1) {
  101. *error = "queue-length ps: incorrect number of arguments";
  102. return -EINVAL;
  103. }
  104. if ((argc == 1) && (sscanf(argv[0], "%u", &repeat_count) != 1)) {
  105. *error = "queue-length ps: invalid repeat count";
  106. return -EINVAL;
  107. }
  108. /* Allocate the path information structure */
  109. pi = kmalloc(sizeof(*pi), GFP_KERNEL);
  110. if (!pi) {
  111. *error = "queue-length ps: Error allocating path information";
  112. return -ENOMEM;
  113. }
  114. pi->path = path;
  115. pi->repeat_count = repeat_count;
  116. atomic_set(&pi->qlen, 0);
  117. path->pscontext = pi;
  118. list_add_tail(&pi->list, &s->valid_paths);
  119. return 0;
  120. }
  121. static void ql_fail_path(struct path_selector *ps, struct dm_path *path)
  122. {
  123. struct selector *s = ps->context;
  124. struct path_info *pi = path->pscontext;
  125. list_move(&pi->list, &s->failed_paths);
  126. }
  127. static int ql_reinstate_path(struct path_selector *ps, struct dm_path *path)
  128. {
  129. struct selector *s = ps->context;
  130. struct path_info *pi = path->pscontext;
  131. list_move_tail(&pi->list, &s->valid_paths);
  132. return 0;
  133. }
  134. /*
  135. * Select a path having the minimum number of in-flight I/Os
  136. */
  137. static struct dm_path *ql_select_path(struct path_selector *ps,
  138. unsigned *repeat_count, size_t nr_bytes)
  139. {
  140. struct selector *s = ps->context;
  141. struct path_info *pi = NULL, *best = NULL;
  142. if (list_empty(&s->valid_paths))
  143. return NULL;
  144. /* Change preferred (first in list) path to evenly balance. */
  145. list_move_tail(s->valid_paths.next, &s->valid_paths);
  146. list_for_each_entry(pi, &s->valid_paths, list) {
  147. if (!best ||
  148. (atomic_read(&pi->qlen) < atomic_read(&best->qlen)))
  149. best = pi;
  150. if (!atomic_read(&best->qlen))
  151. break;
  152. }
  153. if (!best)
  154. return NULL;
  155. *repeat_count = best->repeat_count;
  156. return best->path;
  157. }
  158. static int ql_start_io(struct path_selector *ps, struct dm_path *path,
  159. size_t nr_bytes)
  160. {
  161. struct path_info *pi = path->pscontext;
  162. atomic_inc(&pi->qlen);
  163. return 0;
  164. }
  165. static int ql_end_io(struct path_selector *ps, struct dm_path *path,
  166. size_t nr_bytes)
  167. {
  168. struct path_info *pi = path->pscontext;
  169. atomic_dec(&pi->qlen);
  170. return 0;
  171. }
  172. static struct path_selector_type ql_ps = {
  173. .name = "queue-length",
  174. .module = THIS_MODULE,
  175. .table_args = 1,
  176. .info_args = 1,
  177. .create = ql_create,
  178. .destroy = ql_destroy,
  179. .status = ql_status,
  180. .add_path = ql_add_path,
  181. .fail_path = ql_fail_path,
  182. .reinstate_path = ql_reinstate_path,
  183. .select_path = ql_select_path,
  184. .start_io = ql_start_io,
  185. .end_io = ql_end_io,
  186. };
  187. static int __init dm_ql_init(void)
  188. {
  189. int r = dm_register_path_selector(&ql_ps);
  190. if (r < 0)
  191. DMERR("register failed %d", r);
  192. DMINFO("version " QL_VERSION " loaded");
  193. return r;
  194. }
  195. static void __exit dm_ql_exit(void)
  196. {
  197. int r = dm_unregister_path_selector(&ql_ps);
  198. if (r < 0)
  199. DMERR("unregister failed %d", r);
  200. }
  201. module_init(dm_ql_init);
  202. module_exit(dm_ql_exit);
  203. MODULE_AUTHOR("Stefan Bader <Stefan.Bader at de.ibm.com>");
  204. MODULE_DESCRIPTION(
  205. "(C) Copyright IBM Corp. 2004,2005 All Rights Reserved.\n"
  206. DM_NAME " path selector to balance the number of in-flight I/Os"
  207. );
  208. MODULE_LICENSE("GPL");