1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2003 Sistina Software.
4 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
6 * Module Author: Heinz Mauelshagen
8 * This file is released under the GPL.
10 * Round-robin path selector.
13 #include <linux/device-mapper.h>
15 #include "dm-path-selector.h"
17 #include <linux/slab.h>
18 #include <linux/module.h>
20 #define DM_MSG_PREFIX "multipath round-robin"
22 #define RR_VERSION "1.2.0"
25 *---------------------------------------------------------------
26 * Path-handling code, paths are held in lists
27 *---------------------------------------------------------------
30 struct list_head list;
32 unsigned int repeat_count;
35 static void free_paths(struct list_head *paths)
37 struct path_info *pi, *next;
39 list_for_each_entry_safe(pi, next, paths, list) {
46 *---------------------------------------------------------------
47 * Round-robin selector
48 *---------------------------------------------------------------
51 struct list_head valid_paths;
52 struct list_head invalid_paths;
56 static struct selector *alloc_selector(void)
58 struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
61 INIT_LIST_HEAD(&s->valid_paths);
62 INIT_LIST_HEAD(&s->invalid_paths);
63 spin_lock_init(&s->lock);
69 static int rr_create(struct path_selector *ps, unsigned int argc, char **argv)
81 static void rr_destroy(struct path_selector *ps)
83 struct selector *s = ps->context;
85 free_paths(&s->valid_paths);
86 free_paths(&s->invalid_paths);
91 static int rr_status(struct path_selector *ps, struct dm_path *path,
92 status_type_t type, char *result, unsigned int maxlen)
101 case STATUSTYPE_INFO:
103 case STATUSTYPE_TABLE:
104 pi = path->pscontext;
105 DMEMIT("%u ", pi->repeat_count);
118 * Called during initialisation to register each path with an
119 * optional repeat_count.
121 static int rr_add_path(struct path_selector *ps, struct dm_path *path,
122 int argc, char **argv, char **error)
124 struct selector *s = ps->context;
125 struct path_info *pi;
126 unsigned int repeat_count = RR_MIN_IO;
131 *error = "round-robin ps: incorrect number of arguments";
135 /* First path argument is number of I/Os before switching path */
136 if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
137 *error = "round-robin ps: invalid repeat count";
141 if (repeat_count > 1) {
142 DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
146 /* allocate the path */
147 pi = kmalloc(sizeof(*pi), GFP_KERNEL);
149 *error = "round-robin ps: Error allocating path context";
154 pi->repeat_count = repeat_count;
156 path->pscontext = pi;
158 spin_lock_irqsave(&s->lock, flags);
159 list_add_tail(&pi->list, &s->valid_paths);
160 spin_unlock_irqrestore(&s->lock, flags);
165 static void rr_fail_path(struct path_selector *ps, struct dm_path *p)
168 struct selector *s = ps->context;
169 struct path_info *pi = p->pscontext;
171 spin_lock_irqsave(&s->lock, flags);
172 list_move(&pi->list, &s->invalid_paths);
173 spin_unlock_irqrestore(&s->lock, flags);
176 static int rr_reinstate_path(struct path_selector *ps, struct dm_path *p)
179 struct selector *s = ps->context;
180 struct path_info *pi = p->pscontext;
182 spin_lock_irqsave(&s->lock, flags);
183 list_move(&pi->list, &s->valid_paths);
184 spin_unlock_irqrestore(&s->lock, flags);
189 static struct dm_path *rr_select_path(struct path_selector *ps, size_t nr_bytes)
192 struct selector *s = ps->context;
193 struct path_info *pi = NULL;
195 spin_lock_irqsave(&s->lock, flags);
196 if (!list_empty(&s->valid_paths)) {
197 pi = list_entry(s->valid_paths.next, struct path_info, list);
198 list_move_tail(&pi->list, &s->valid_paths);
200 spin_unlock_irqrestore(&s->lock, flags);
202 return pi ? pi->path : NULL;
205 static struct path_selector_type rr_ps = {
206 .name = "round-robin",
207 .module = THIS_MODULE,
211 .destroy = rr_destroy,
213 .add_path = rr_add_path,
214 .fail_path = rr_fail_path,
215 .reinstate_path = rr_reinstate_path,
216 .select_path = rr_select_path,
219 static int __init dm_rr_init(void)
221 int r = dm_register_path_selector(&rr_ps);
224 DMERR("register failed %d", r);
226 DMINFO("version " RR_VERSION " loaded");
231 static void __exit dm_rr_exit(void)
233 int r = dm_unregister_path_selector(&rr_ps);
236 DMERR("unregister failed %d", r);
239 module_init(dm_rr_init);
240 module_exit(dm_rr_exit);
242 MODULE_DESCRIPTION(DM_NAME " round-robin multipath path selector");
244 MODULE_LICENSE("GPL");