2 * Copyright (C) 2003 Sistina Software.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5 * Module Author: Heinz Mauelshagen
7 * This file is released under the GPL.
9 * Round-robin path selector.
12 #include <linux/device-mapper.h>
14 #include "dm-path-selector.h"
16 #include <linux/slab.h>
17 #include <linux/module.h>
19 #define DM_MSG_PREFIX "multipath round-robin"
21 #define RR_VERSION "1.2.0"
23 /*-----------------------------------------------------------------
24 * Path-handling code, paths are held in lists
25 *---------------------------------------------------------------*/
27 struct list_head list;
29 unsigned repeat_count;
32 static void free_paths(struct list_head *paths)
34 struct path_info *pi, *next;
36 list_for_each_entry_safe(pi, next, paths, list) {
42 /*-----------------------------------------------------------------
43 * Round-robin selector
44 *---------------------------------------------------------------*/
47 struct list_head valid_paths;
48 struct list_head invalid_paths;
52 static struct selector *alloc_selector(void)
54 struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
57 INIT_LIST_HEAD(&s->valid_paths);
58 INIT_LIST_HEAD(&s->invalid_paths);
59 spin_lock_init(&s->lock);
65 static int rr_create(struct path_selector *ps, unsigned argc, char **argv)
77 static void rr_destroy(struct path_selector *ps)
79 struct selector *s = ps->context;
81 free_paths(&s->valid_paths);
82 free_paths(&s->invalid_paths);
87 static int rr_status(struct path_selector *ps, struct dm_path *path,
88 status_type_t type, char *result, unsigned int maxlen)
99 case STATUSTYPE_TABLE:
100 pi = path->pscontext;
101 DMEMIT("%u ", pi->repeat_count);
114 * Called during initialisation to register each path with an
115 * optional repeat_count.
117 static int rr_add_path(struct path_selector *ps, struct dm_path *path,
118 int argc, char **argv, char **error)
120 struct selector *s = ps->context;
121 struct path_info *pi;
122 unsigned repeat_count = RR_MIN_IO;
127 *error = "round-robin ps: incorrect number of arguments";
131 /* First path argument is number of I/Os before switching path */
132 if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
133 *error = "round-robin ps: invalid repeat count";
137 if (repeat_count > 1) {
138 DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
142 /* allocate the path */
143 pi = kmalloc(sizeof(*pi), GFP_KERNEL);
145 *error = "round-robin ps: Error allocating path context";
150 pi->repeat_count = repeat_count;
152 path->pscontext = pi;
154 spin_lock_irqsave(&s->lock, flags);
155 list_add_tail(&pi->list, &s->valid_paths);
156 spin_unlock_irqrestore(&s->lock, flags);
161 static void rr_fail_path(struct path_selector *ps, struct dm_path *p)
164 struct selector *s = ps->context;
165 struct path_info *pi = p->pscontext;
167 spin_lock_irqsave(&s->lock, flags);
168 list_move(&pi->list, &s->invalid_paths);
169 spin_unlock_irqrestore(&s->lock, flags);
172 static int rr_reinstate_path(struct path_selector *ps, struct dm_path *p)
175 struct selector *s = ps->context;
176 struct path_info *pi = p->pscontext;
178 spin_lock_irqsave(&s->lock, flags);
179 list_move(&pi->list, &s->valid_paths);
180 spin_unlock_irqrestore(&s->lock, flags);
185 static struct dm_path *rr_select_path(struct path_selector *ps, size_t nr_bytes)
188 struct selector *s = ps->context;
189 struct path_info *pi = NULL;
191 spin_lock_irqsave(&s->lock, flags);
192 if (!list_empty(&s->valid_paths)) {
193 pi = list_entry(s->valid_paths.next, struct path_info, list);
194 list_move_tail(&pi->list, &s->valid_paths);
196 spin_unlock_irqrestore(&s->lock, flags);
198 return pi ? pi->path : NULL;
201 static struct path_selector_type rr_ps = {
202 .name = "round-robin",
203 .module = THIS_MODULE,
207 .destroy = rr_destroy,
209 .add_path = rr_add_path,
210 .fail_path = rr_fail_path,
211 .reinstate_path = rr_reinstate_path,
212 .select_path = rr_select_path,
215 static int __init dm_rr_init(void)
217 int r = dm_register_path_selector(&rr_ps);
220 DMERR("register failed %d", r);
222 DMINFO("version " RR_VERSION " loaded");
227 static void __exit dm_rr_exit(void)
229 int r = dm_unregister_path_selector(&rr_ps);
232 DMERR("unregister failed %d", r);
235 module_init(dm_rr_init);
236 module_exit(dm_rr_exit);
238 MODULE_DESCRIPTION(DM_NAME " round-robin multipath path selector");
240 MODULE_LICENSE("GPL");