dm-ps-queue-length.c 6.0 KB

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