phylink: convert to fwnode
[linux-2.6/btrfs-unstable.git] / drivers / md / dm-queue-length.c
blob23f1786417946e9ee8375a051544e1024db1f7d1
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 <linux/atomic.h>
25 #define DM_MSG_PREFIX "multipath queue-length"
26 #define QL_MIN_IO 1
27 #define QL_VERSION "0.2.0"
29 struct selector {
30 struct list_head valid_paths;
31 struct list_head failed_paths;
32 spinlock_t lock;
35 struct path_info {
36 struct list_head list;
37 struct dm_path *path;
38 unsigned repeat_count;
39 atomic_t qlen; /* the number of in-flight I/Os */
42 static struct selector *alloc_selector(void)
44 struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
46 if (s) {
47 INIT_LIST_HEAD(&s->valid_paths);
48 INIT_LIST_HEAD(&s->failed_paths);
49 spin_lock_init(&s->lock);
52 return s;
55 static int ql_create(struct path_selector *ps, unsigned argc, char **argv)
57 struct selector *s = alloc_selector();
59 if (!s)
60 return -ENOMEM;
62 ps->context = s;
63 return 0;
66 static void ql_free_paths(struct list_head *paths)
68 struct path_info *pi, *next;
70 list_for_each_entry_safe(pi, next, paths, list) {
71 list_del(&pi->list);
72 kfree(pi);
76 static void ql_destroy(struct path_selector *ps)
78 struct selector *s = ps->context;
80 ql_free_paths(&s->valid_paths);
81 ql_free_paths(&s->failed_paths);
82 kfree(s);
83 ps->context = NULL;
86 static int ql_status(struct path_selector *ps, struct dm_path *path,
87 status_type_t type, char *result, unsigned maxlen)
89 unsigned sz = 0;
90 struct path_info *pi;
92 /* When called with NULL path, return selector status/args. */
93 if (!path)
94 DMEMIT("0 ");
95 else {
96 pi = path->pscontext;
98 switch (type) {
99 case STATUSTYPE_INFO:
100 DMEMIT("%d ", atomic_read(&pi->qlen));
101 break;
102 case STATUSTYPE_TABLE:
103 DMEMIT("%u ", pi->repeat_count);
104 break;
108 return sz;
111 static int ql_add_path(struct path_selector *ps, struct dm_path *path,
112 int argc, char **argv, char **error)
114 struct selector *s = ps->context;
115 struct path_info *pi;
116 unsigned repeat_count = QL_MIN_IO;
117 char dummy;
118 unsigned long flags;
121 * Arguments: [<repeat_count>]
122 * <repeat_count>: The number of I/Os before switching path.
123 * If not given, default (QL_MIN_IO) is used.
125 if (argc > 1) {
126 *error = "queue-length ps: incorrect number of arguments";
127 return -EINVAL;
130 if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
131 *error = "queue-length ps: invalid repeat count";
132 return -EINVAL;
135 if (repeat_count > 1) {
136 DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
137 repeat_count = 1;
140 /* Allocate the path information structure */
141 pi = kmalloc(sizeof(*pi), GFP_KERNEL);
142 if (!pi) {
143 *error = "queue-length ps: Error allocating path information";
144 return -ENOMEM;
147 pi->path = path;
148 pi->repeat_count = repeat_count;
149 atomic_set(&pi->qlen, 0);
151 path->pscontext = pi;
153 spin_lock_irqsave(&s->lock, flags);
154 list_add_tail(&pi->list, &s->valid_paths);
155 spin_unlock_irqrestore(&s->lock, flags);
157 return 0;
160 static void ql_fail_path(struct path_selector *ps, struct dm_path *path)
162 struct selector *s = ps->context;
163 struct path_info *pi = path->pscontext;
164 unsigned long flags;
166 spin_lock_irqsave(&s->lock, flags);
167 list_move(&pi->list, &s->failed_paths);
168 spin_unlock_irqrestore(&s->lock, flags);
171 static int ql_reinstate_path(struct path_selector *ps, struct dm_path *path)
173 struct selector *s = ps->context;
174 struct path_info *pi = path->pscontext;
175 unsigned long flags;
177 spin_lock_irqsave(&s->lock, flags);
178 list_move_tail(&pi->list, &s->valid_paths);
179 spin_unlock_irqrestore(&s->lock, flags);
181 return 0;
185 * Select a path having the minimum number of in-flight I/Os
187 static struct dm_path *ql_select_path(struct path_selector *ps, size_t nr_bytes)
189 struct selector *s = ps->context;
190 struct path_info *pi = NULL, *best = NULL;
191 struct dm_path *ret = NULL;
192 unsigned long flags;
194 spin_lock_irqsave(&s->lock, flags);
195 if (list_empty(&s->valid_paths))
196 goto out;
198 /* Change preferred (first in list) path to evenly balance. */
199 list_move_tail(s->valid_paths.next, &s->valid_paths);
201 list_for_each_entry(pi, &s->valid_paths, list) {
202 if (!best ||
203 (atomic_read(&pi->qlen) < atomic_read(&best->qlen)))
204 best = pi;
206 if (!atomic_read(&best->qlen))
207 break;
210 if (!best)
211 goto out;
213 ret = best->path;
214 out:
215 spin_unlock_irqrestore(&s->lock, flags);
216 return ret;
219 static int ql_start_io(struct path_selector *ps, struct dm_path *path,
220 size_t nr_bytes)
222 struct path_info *pi = path->pscontext;
224 atomic_inc(&pi->qlen);
226 return 0;
229 static int ql_end_io(struct path_selector *ps, struct dm_path *path,
230 size_t nr_bytes)
232 struct path_info *pi = path->pscontext;
234 atomic_dec(&pi->qlen);
236 return 0;
239 static struct path_selector_type ql_ps = {
240 .name = "queue-length",
241 .module = THIS_MODULE,
242 .table_args = 1,
243 .info_args = 1,
244 .create = ql_create,
245 .destroy = ql_destroy,
246 .status = ql_status,
247 .add_path = ql_add_path,
248 .fail_path = ql_fail_path,
249 .reinstate_path = ql_reinstate_path,
250 .select_path = ql_select_path,
251 .start_io = ql_start_io,
252 .end_io = ql_end_io,
255 static int __init dm_ql_init(void)
257 int r = dm_register_path_selector(&ql_ps);
259 if (r < 0)
260 DMERR("register failed %d", r);
262 DMINFO("version " QL_VERSION " loaded");
264 return r;
267 static void __exit dm_ql_exit(void)
269 int r = dm_unregister_path_selector(&ql_ps);
271 if (r < 0)
272 DMERR("unregister failed %d", r);
275 module_init(dm_ql_init);
276 module_exit(dm_ql_exit);
278 MODULE_AUTHOR("Stefan Bader <Stefan.Bader at de.ibm.com>");
279 MODULE_DESCRIPTION(
280 "(C) Copyright IBM Corp. 2004,2005 All Rights Reserved.\n"
281 DM_NAME " path selector to balance the number of in-flight I/Os"
283 MODULE_LICENSE("GPL");