1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004-2005 IBM Corp. All Rights Reserved.
4 * Copyright (C) 2006-2009 NEC Corporation.
8 * Module Author: Stefan Bader, IBM
9 * Modified by: Kiyoshi Ueda, NEC
11 * This file is released under the GPL.
13 * queue-length path selector - choose a path with the least number of
18 #include "dm-path-selector.h"
20 #include <linux/slab.h>
21 #include <linux/ctype.h>
22 #include <linux/errno.h>
23 #include <linux/module.h>
24 #include <linux/atomic.h>
26 #define DM_MSG_PREFIX "multipath queue-length"
28 #define QL_VERSION "0.2.0"
31 struct list_head valid_paths;
32 struct list_head failed_paths;
37 struct list_head list;
39 unsigned int repeat_count;
40 atomic_t qlen; /* the number of in-flight I/Os */
43 static struct selector *alloc_selector(void)
45 struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
48 INIT_LIST_HEAD(&s->valid_paths);
49 INIT_LIST_HEAD(&s->failed_paths);
50 spin_lock_init(&s->lock);
56 static int ql_create(struct path_selector *ps, unsigned int argc, char **argv)
58 struct selector *s = alloc_selector();
67 static void ql_free_paths(struct list_head *paths)
69 struct path_info *pi, *next;
71 list_for_each_entry_safe(pi, next, paths, list) {
77 static void ql_destroy(struct path_selector *ps)
79 struct selector *s = ps->context;
81 ql_free_paths(&s->valid_paths);
82 ql_free_paths(&s->failed_paths);
87 static int ql_status(struct path_selector *ps, struct dm_path *path,
88 status_type_t type, char *result, unsigned int maxlen)
93 /* When called with NULL path, return selector status/args. */
100 case STATUSTYPE_INFO:
101 DMEMIT("%d ", atomic_read(&pi->qlen));
103 case STATUSTYPE_TABLE:
104 DMEMIT("%u ", pi->repeat_count);
115 static int ql_add_path(struct path_selector *ps, struct dm_path *path,
116 int argc, char **argv, char **error)
118 struct selector *s = ps->context;
119 struct path_info *pi;
120 unsigned int repeat_count = QL_MIN_IO;
125 * Arguments: [<repeat_count>]
126 * <repeat_count>: The number of I/Os before switching path.
127 * If not given, default (QL_MIN_IO) is used.
130 *error = "queue-length ps: incorrect number of arguments";
134 if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
135 *error = "queue-length ps: invalid repeat count";
139 if (repeat_count > 1) {
140 DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
144 /* Allocate the path information structure */
145 pi = kmalloc(sizeof(*pi), GFP_KERNEL);
147 *error = "queue-length ps: Error allocating path information";
152 pi->repeat_count = repeat_count;
153 atomic_set(&pi->qlen, 0);
155 path->pscontext = pi;
157 spin_lock_irqsave(&s->lock, flags);
158 list_add_tail(&pi->list, &s->valid_paths);
159 spin_unlock_irqrestore(&s->lock, flags);
164 static void ql_fail_path(struct path_selector *ps, struct dm_path *path)
166 struct selector *s = ps->context;
167 struct path_info *pi = path->pscontext;
170 spin_lock_irqsave(&s->lock, flags);
171 list_move(&pi->list, &s->failed_paths);
172 spin_unlock_irqrestore(&s->lock, flags);
175 static int ql_reinstate_path(struct path_selector *ps, struct dm_path *path)
177 struct selector *s = ps->context;
178 struct path_info *pi = path->pscontext;
181 spin_lock_irqsave(&s->lock, flags);
182 list_move_tail(&pi->list, &s->valid_paths);
183 spin_unlock_irqrestore(&s->lock, flags);
189 * Select a path having the minimum number of in-flight I/Os
191 static struct dm_path *ql_select_path(struct path_selector *ps, size_t nr_bytes)
193 struct selector *s = ps->context;
194 struct path_info *pi = NULL, *best = NULL;
195 struct dm_path *ret = NULL;
198 spin_lock_irqsave(&s->lock, flags);
199 if (list_empty(&s->valid_paths))
202 list_for_each_entry(pi, &s->valid_paths, list) {
204 (atomic_read(&pi->qlen) < atomic_read(&best->qlen)))
207 if (!atomic_read(&best->qlen))
214 /* Move most recently used to least preferred to evenly balance. */
215 list_move_tail(&best->list, &s->valid_paths);
219 spin_unlock_irqrestore(&s->lock, flags);
223 static int ql_start_io(struct path_selector *ps, struct dm_path *path,
226 struct path_info *pi = path->pscontext;
228 atomic_inc(&pi->qlen);
233 static int ql_end_io(struct path_selector *ps, struct dm_path *path,
234 size_t nr_bytes, u64 start_time)
236 struct path_info *pi = path->pscontext;
238 atomic_dec(&pi->qlen);
243 static struct path_selector_type ql_ps = {
244 .name = "queue-length",
245 .module = THIS_MODULE,
249 .destroy = ql_destroy,
251 .add_path = ql_add_path,
252 .fail_path = ql_fail_path,
253 .reinstate_path = ql_reinstate_path,
254 .select_path = ql_select_path,
255 .start_io = ql_start_io,
259 static int __init dm_ql_init(void)
261 int r = dm_register_path_selector(&ql_ps);
264 DMERR("register failed %d", r);
266 DMINFO("version " QL_VERSION " loaded");
271 static void __exit dm_ql_exit(void)
273 int r = dm_unregister_path_selector(&ql_ps);
276 DMERR("unregister failed %d", r);
279 module_init(dm_ql_init);
280 module_exit(dm_ql_exit);
282 MODULE_AUTHOR("Stefan Bader <Stefan.Bader at de.ibm.com>");
284 "(C) Copyright IBM Corp. 2004,2005 All Rights Reserved.\n"
285 DM_NAME " path selector to balance the number of in-flight I/Os"
287 MODULE_LICENSE("GPL");