dm-queue-length.c 5.9 KB

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