drivers/ide/ide_platform.c: use resource_size()
[linux-2.6/linux-2.6-openrd.git] / drivers / md / dm-queue-length.c
blobf92b6cea9d9c8791997dc5e5c5d6003df578158e
1 /*
2 * Copyright (C) 2004-2005 IBM Corp. All Rights Reserved.
3 * Copyright (C) 2006-2009 NEC Corporation.
5 * dm-queue-length.c
7 * Module Author: Stefan Bader, IBM
8 * Modified by: Kiyoshi Ueda, NEC
10 * This file is released under the GPL.
12 * queue-length path selector - choose a path with the least number of
13 * in-flight I/Os.
16 #include "dm.h"
17 #include "dm-path-selector.h"
19 #include <linux/slab.h>
20 #include <linux/ctype.h>
21 #include <linux/errno.h>
22 #include <linux/module.h>
23 #include <asm/atomic.h>
25 #define DM_MSG_PREFIX "multipath queue-length"
26 #define QL_MIN_IO 128
27 #define QL_VERSION "0.1.0"
29 struct selector {
30 struct list_head valid_paths;
31 struct list_head failed_paths;
34 struct path_info {
35 struct list_head list;
36 struct dm_path *path;
37 unsigned repeat_count;
38 atomic_t qlen; /* the number of in-flight I/Os */
41 static struct selector *alloc_selector(void)
43 struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
45 if (s) {
46 INIT_LIST_HEAD(&s->valid_paths);
47 INIT_LIST_HEAD(&s->failed_paths);
50 return s;
53 static int ql_create(struct path_selector *ps, unsigned argc, char **argv)
55 struct selector *s = alloc_selector();
57 if (!s)
58 return -ENOMEM;
60 ps->context = s;
61 return 0;
64 static void ql_free_paths(struct list_head *paths)
66 struct path_info *pi, *next;
68 list_for_each_entry_safe(pi, next, paths, list) {
69 list_del(&pi->list);
70 kfree(pi);
74 static void ql_destroy(struct path_selector *ps)
76 struct selector *s = ps->context;
78 ql_free_paths(&s->valid_paths);
79 ql_free_paths(&s->failed_paths);
80 kfree(s);
81 ps->context = NULL;
84 static int ql_status(struct path_selector *ps, struct dm_path *path,
85 status_type_t type, char *result, unsigned maxlen)
87 unsigned sz = 0;
88 struct path_info *pi;
90 /* When called with NULL path, return selector status/args. */
91 if (!path)
92 DMEMIT("0 ");
93 else {
94 pi = path->pscontext;
96 switch (type) {
97 case STATUSTYPE_INFO:
98 DMEMIT("%d ", atomic_read(&pi->qlen));
99 break;
100 case STATUSTYPE_TABLE:
101 DMEMIT("%u ", pi->repeat_count);
102 break;
106 return sz;
109 static int ql_add_path(struct path_selector *ps, struct dm_path *path,
110 int argc, char **argv, char **error)
112 struct selector *s = ps->context;
113 struct path_info *pi;
114 unsigned repeat_count = QL_MIN_IO;
117 * Arguments: [<repeat_count>]
118 * <repeat_count>: The number of I/Os before switching path.
119 * If not given, default (QL_MIN_IO) is used.
121 if (argc > 1) {
122 *error = "queue-length ps: incorrect number of arguments";
123 return -EINVAL;
126 if ((argc == 1) && (sscanf(argv[0], "%u", &repeat_count) != 1)) {
127 *error = "queue-length ps: invalid repeat count";
128 return -EINVAL;
131 /* Allocate the path information structure */
132 pi = kmalloc(sizeof(*pi), GFP_KERNEL);
133 if (!pi) {
134 *error = "queue-length ps: Error allocating path information";
135 return -ENOMEM;
138 pi->path = path;
139 pi->repeat_count = repeat_count;
140 atomic_set(&pi->qlen, 0);
142 path->pscontext = pi;
144 list_add_tail(&pi->list, &s->valid_paths);
146 return 0;
149 static void ql_fail_path(struct path_selector *ps, struct dm_path *path)
151 struct selector *s = ps->context;
152 struct path_info *pi = path->pscontext;
154 list_move(&pi->list, &s->failed_paths);
157 static int ql_reinstate_path(struct path_selector *ps, struct dm_path *path)
159 struct selector *s = ps->context;
160 struct path_info *pi = path->pscontext;
162 list_move_tail(&pi->list, &s->valid_paths);
164 return 0;
168 * Select a path having the minimum number of in-flight I/Os
170 static struct dm_path *ql_select_path(struct path_selector *ps,
171 unsigned *repeat_count, size_t nr_bytes)
173 struct selector *s = ps->context;
174 struct path_info *pi = NULL, *best = NULL;
176 if (list_empty(&s->valid_paths))
177 return NULL;
179 /* Change preferred (first in list) path to evenly balance. */
180 list_move_tail(s->valid_paths.next, &s->valid_paths);
182 list_for_each_entry(pi, &s->valid_paths, list) {
183 if (!best ||
184 (atomic_read(&pi->qlen) < atomic_read(&best->qlen)))
185 best = pi;
187 if (!atomic_read(&best->qlen))
188 break;
191 if (!best)
192 return NULL;
194 *repeat_count = best->repeat_count;
196 return best->path;
199 static int ql_start_io(struct path_selector *ps, struct dm_path *path,
200 size_t nr_bytes)
202 struct path_info *pi = path->pscontext;
204 atomic_inc(&pi->qlen);
206 return 0;
209 static int ql_end_io(struct path_selector *ps, struct dm_path *path,
210 size_t nr_bytes)
212 struct path_info *pi = path->pscontext;
214 atomic_dec(&pi->qlen);
216 return 0;
219 static struct path_selector_type ql_ps = {
220 .name = "queue-length",
221 .module = THIS_MODULE,
222 .table_args = 1,
223 .info_args = 1,
224 .create = ql_create,
225 .destroy = ql_destroy,
226 .status = ql_status,
227 .add_path = ql_add_path,
228 .fail_path = ql_fail_path,
229 .reinstate_path = ql_reinstate_path,
230 .select_path = ql_select_path,
231 .start_io = ql_start_io,
232 .end_io = ql_end_io,
235 static int __init dm_ql_init(void)
237 int r = dm_register_path_selector(&ql_ps);
239 if (r < 0)
240 DMERR("register failed %d", r);
242 DMINFO("version " QL_VERSION " loaded");
244 return r;
247 static void __exit dm_ql_exit(void)
249 int r = dm_unregister_path_selector(&ql_ps);
251 if (r < 0)
252 DMERR("unregister failed %d", r);
255 module_init(dm_ql_init);
256 module_exit(dm_ql_exit);
258 MODULE_AUTHOR("Stefan Bader <Stefan.Bader at de.ibm.com>");
259 MODULE_DESCRIPTION(
260 "(C) Copyright IBM Corp. 2004,2005 All Rights Reserved.\n"
261 DM_NAME " path selector to balance the number of in-flight I/Os"
263 MODULE_LICENSE("GPL");