Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2004-2005 IBM Corp. All Rights Reserved.
4 * Copyright (C) 2006-2009 NEC Corporation.
5 *
6 * dm-queue-length.c
7 *
8 * Module Author: Stefan Bader, IBM
9 * Modified by: Kiyoshi Ueda, NEC
10 *
11 * This file is released under the GPL.
12 *
13 * queue-length path selector - choose a path with the least number of
14 * in-flight I/Os.
15 */
16
17#include "dm.h"
18#include "dm-path-selector.h"
19
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>
25
26#define DM_MSG_PREFIX "multipath queue-length"
27#define QL_MIN_IO 1
28#define QL_VERSION "0.2.0"
29
30struct selector {
31 struct list_head valid_paths;
32 struct list_head failed_paths;
33 spinlock_t lock;
34};
35
36struct path_info {
37 struct list_head list;
38 struct dm_path *path;
39 unsigned int repeat_count;
40 atomic_t qlen; /* the number of in-flight I/Os */
41};
42
43static struct selector *alloc_selector(void)
44{
45 struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
46
47 if (s) {
48 INIT_LIST_HEAD(&s->valid_paths);
49 INIT_LIST_HEAD(&s->failed_paths);
50 spin_lock_init(&s->lock);
51 }
52
53 return s;
54}
55
56static int ql_create(struct path_selector *ps, unsigned int argc, char **argv)
57{
58 struct selector *s = alloc_selector();
59
60 if (!s)
61 return -ENOMEM;
62
63 ps->context = s;
64 return 0;
65}
66
67static void ql_free_paths(struct list_head *paths)
68{
69 struct path_info *pi, *next;
70
71 list_for_each_entry_safe(pi, next, paths, list) {
72 list_del(&pi->list);
73 kfree(pi);
74 }
75}
76
77static void ql_destroy(struct path_selector *ps)
78{
79 struct selector *s = ps->context;
80
81 ql_free_paths(&s->valid_paths);
82 ql_free_paths(&s->failed_paths);
83 kfree(s);
84 ps->context = NULL;
85}
86
87static int ql_status(struct path_selector *ps, struct dm_path *path,
88 status_type_t type, char *result, unsigned int maxlen)
89{
90 unsigned int sz = 0;
91 struct path_info *pi;
92
93 /* When called with NULL path, return selector status/args. */
94 if (!path)
95 DMEMIT("0 ");
96 else {
97 pi = path->pscontext;
98
99 switch (type) {
100 case STATUSTYPE_INFO:
101 DMEMIT("%d ", atomic_read(&pi->qlen));
102 break;
103 case STATUSTYPE_TABLE:
104 DMEMIT("%u ", pi->repeat_count);
105 break;
106 case STATUSTYPE_IMA:
107 *result = '\0';
108 break;
109 }
110 }
111
112 return sz;
113}
114
115static int ql_add_path(struct path_selector *ps, struct dm_path *path,
116 int argc, char **argv, char **error)
117{
118 struct selector *s = ps->context;
119 struct path_info *pi;
120 unsigned int repeat_count = QL_MIN_IO;
121 char dummy;
122 unsigned long flags;
123
124 /*
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.
128 */
129 if (argc > 1) {
130 *error = "queue-length ps: incorrect number of arguments";
131 return -EINVAL;
132 }
133
134 if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
135 *error = "queue-length ps: invalid repeat count";
136 return -EINVAL;
137 }
138
139 if (repeat_count > 1) {
140 DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
141 repeat_count = 1;
142 }
143
144 /* Allocate the path information structure */
145 pi = kmalloc(sizeof(*pi), GFP_KERNEL);
146 if (!pi) {
147 *error = "queue-length ps: Error allocating path information";
148 return -ENOMEM;
149 }
150
151 pi->path = path;
152 pi->repeat_count = repeat_count;
153 atomic_set(&pi->qlen, 0);
154
155 path->pscontext = pi;
156
157 spin_lock_irqsave(&s->lock, flags);
158 list_add_tail(&pi->list, &s->valid_paths);
159 spin_unlock_irqrestore(&s->lock, flags);
160
161 return 0;
162}
163
164static void ql_fail_path(struct path_selector *ps, struct dm_path *path)
165{
166 struct selector *s = ps->context;
167 struct path_info *pi = path->pscontext;
168 unsigned long flags;
169
170 spin_lock_irqsave(&s->lock, flags);
171 list_move(&pi->list, &s->failed_paths);
172 spin_unlock_irqrestore(&s->lock, flags);
173}
174
175static int ql_reinstate_path(struct path_selector *ps, struct dm_path *path)
176{
177 struct selector *s = ps->context;
178 struct path_info *pi = path->pscontext;
179 unsigned long flags;
180
181 spin_lock_irqsave(&s->lock, flags);
182 list_move_tail(&pi->list, &s->valid_paths);
183 spin_unlock_irqrestore(&s->lock, flags);
184
185 return 0;
186}
187
188/*
189 * Select a path having the minimum number of in-flight I/Os
190 */
191static struct dm_path *ql_select_path(struct path_selector *ps, size_t nr_bytes)
192{
193 struct selector *s = ps->context;
194 struct path_info *pi = NULL, *best = NULL;
195 struct dm_path *ret = NULL;
196 unsigned long flags;
197
198 spin_lock_irqsave(&s->lock, flags);
199 if (list_empty(&s->valid_paths))
200 goto out;
201
202 list_for_each_entry(pi, &s->valid_paths, list) {
203 if (!best ||
204 (atomic_read(&pi->qlen) < atomic_read(&best->qlen)))
205 best = pi;
206
207 if (!atomic_read(&best->qlen))
208 break;
209 }
210
211 if (!best)
212 goto out;
213
214 /* Move most recently used to least preferred to evenly balance. */
215 list_move_tail(&best->list, &s->valid_paths);
216
217 ret = best->path;
218out:
219 spin_unlock_irqrestore(&s->lock, flags);
220 return ret;
221}
222
223static int ql_start_io(struct path_selector *ps, struct dm_path *path,
224 size_t nr_bytes)
225{
226 struct path_info *pi = path->pscontext;
227
228 atomic_inc(&pi->qlen);
229
230 return 0;
231}
232
233static int ql_end_io(struct path_selector *ps, struct dm_path *path,
234 size_t nr_bytes, u64 start_time)
235{
236 struct path_info *pi = path->pscontext;
237
238 atomic_dec(&pi->qlen);
239
240 return 0;
241}
242
243static struct path_selector_type ql_ps = {
244 .name = "queue-length",
245 .module = THIS_MODULE,
246 .table_args = 1,
247 .info_args = 1,
248 .create = ql_create,
249 .destroy = ql_destroy,
250 .status = ql_status,
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,
256 .end_io = ql_end_io,
257};
258
259static int __init dm_ql_init(void)
260{
261 int r = dm_register_path_selector(&ql_ps);
262
263 if (r < 0)
264 DMERR("register failed %d", r);
265
266 DMINFO("version " QL_VERSION " loaded");
267
268 return r;
269}
270
271static void __exit dm_ql_exit(void)
272{
273 int r = dm_unregister_path_selector(&ql_ps);
274
275 if (r < 0)
276 DMERR("unregister failed %d", r);
277}
278
279module_init(dm_ql_init);
280module_exit(dm_ql_exit);
281
282MODULE_AUTHOR("Stefan Bader <Stefan.Bader at de.ibm.com>");
283MODULE_DESCRIPTION(
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"
286);
287MODULE_LICENSE("GPL");
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
16#include "dm.h"
17#include "dm-path-selector.h"
18
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>
24
25#define DM_MSG_PREFIX "multipath queue-length"
26#define QL_MIN_IO 1
27#define QL_VERSION "0.2.0"
28
29struct selector {
30 struct list_head valid_paths;
31 struct list_head failed_paths;
32 spinlock_t lock;
33};
34
35struct 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 */
40};
41
42static struct selector *alloc_selector(void)
43{
44 struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
45
46 if (s) {
47 INIT_LIST_HEAD(&s->valid_paths);
48 INIT_LIST_HEAD(&s->failed_paths);
49 spin_lock_init(&s->lock);
50 }
51
52 return s;
53}
54
55static int ql_create(struct path_selector *ps, unsigned argc, char **argv)
56{
57 struct selector *s = alloc_selector();
58
59 if (!s)
60 return -ENOMEM;
61
62 ps->context = s;
63 return 0;
64}
65
66static void ql_free_paths(struct list_head *paths)
67{
68 struct path_info *pi, *next;
69
70 list_for_each_entry_safe(pi, next, paths, list) {
71 list_del(&pi->list);
72 kfree(pi);
73 }
74}
75
76static void ql_destroy(struct path_selector *ps)
77{
78 struct selector *s = ps->context;
79
80 ql_free_paths(&s->valid_paths);
81 ql_free_paths(&s->failed_paths);
82 kfree(s);
83 ps->context = NULL;
84}
85
86static int ql_status(struct path_selector *ps, struct dm_path *path,
87 status_type_t type, char *result, unsigned maxlen)
88{
89 unsigned sz = 0;
90 struct path_info *pi;
91
92 /* When called with NULL path, return selector status/args. */
93 if (!path)
94 DMEMIT("0 ");
95 else {
96 pi = path->pscontext;
97
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;
105 }
106 }
107
108 return sz;
109}
110
111static int ql_add_path(struct path_selector *ps, struct dm_path *path,
112 int argc, char **argv, char **error)
113{
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;
119
120 /*
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.
124 */
125 if (argc > 1) {
126 *error = "queue-length ps: incorrect number of arguments";
127 return -EINVAL;
128 }
129
130 if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
131 *error = "queue-length ps: invalid repeat count";
132 return -EINVAL;
133 }
134
135 if (repeat_count > 1) {
136 DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
137 repeat_count = 1;
138 }
139
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;
145 }
146
147 pi->path = path;
148 pi->repeat_count = repeat_count;
149 atomic_set(&pi->qlen, 0);
150
151 path->pscontext = pi;
152
153 spin_lock_irqsave(&s->lock, flags);
154 list_add_tail(&pi->list, &s->valid_paths);
155 spin_unlock_irqrestore(&s->lock, flags);
156
157 return 0;
158}
159
160static void ql_fail_path(struct path_selector *ps, struct dm_path *path)
161{
162 struct selector *s = ps->context;
163 struct path_info *pi = path->pscontext;
164 unsigned long flags;
165
166 spin_lock_irqsave(&s->lock, flags);
167 list_move(&pi->list, &s->failed_paths);
168 spin_unlock_irqrestore(&s->lock, flags);
169}
170
171static int ql_reinstate_path(struct path_selector *ps, struct dm_path *path)
172{
173 struct selector *s = ps->context;
174 struct path_info *pi = path->pscontext;
175 unsigned long flags;
176
177 spin_lock_irqsave(&s->lock, flags);
178 list_move_tail(&pi->list, &s->valid_paths);
179 spin_unlock_irqrestore(&s->lock, flags);
180
181 return 0;
182}
183
184/*
185 * Select a path having the minimum number of in-flight I/Os
186 */
187static struct dm_path *ql_select_path(struct path_selector *ps, size_t nr_bytes)
188{
189 struct selector *s = ps->context;
190 struct path_info *pi = NULL, *best = NULL;
191 struct dm_path *ret = NULL;
192 unsigned long flags;
193
194 spin_lock_irqsave(&s->lock, flags);
195 if (list_empty(&s->valid_paths))
196 goto out;
197
198 list_for_each_entry(pi, &s->valid_paths, list) {
199 if (!best ||
200 (atomic_read(&pi->qlen) < atomic_read(&best->qlen)))
201 best = pi;
202
203 if (!atomic_read(&best->qlen))
204 break;
205 }
206
207 if (!best)
208 goto out;
209
210 /* Move most recently used to least preferred to evenly balance. */
211 list_move_tail(&best->list, &s->valid_paths);
212
213 ret = best->path;
214out:
215 spin_unlock_irqrestore(&s->lock, flags);
216 return ret;
217}
218
219static int ql_start_io(struct path_selector *ps, struct dm_path *path,
220 size_t nr_bytes)
221{
222 struct path_info *pi = path->pscontext;
223
224 atomic_inc(&pi->qlen);
225
226 return 0;
227}
228
229static int ql_end_io(struct path_selector *ps, struct dm_path *path,
230 size_t nr_bytes, u64 start_time)
231{
232 struct path_info *pi = path->pscontext;
233
234 atomic_dec(&pi->qlen);
235
236 return 0;
237}
238
239static 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,
253};
254
255static int __init dm_ql_init(void)
256{
257 int r = dm_register_path_selector(&ql_ps);
258
259 if (r < 0)
260 DMERR("register failed %d", r);
261
262 DMINFO("version " QL_VERSION " loaded");
263
264 return r;
265}
266
267static void __exit dm_ql_exit(void)
268{
269 int r = dm_unregister_path_selector(&ql_ps);
270
271 if (r < 0)
272 DMERR("unregister failed %d", r);
273}
274
275module_init(dm_ql_init);
276module_exit(dm_ql_exit);
277
278MODULE_AUTHOR("Stefan Bader <Stefan.Bader at de.ibm.com>");
279MODULE_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"
282);
283MODULE_LICENSE("GPL");