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.
13 #include "dm-path-selector.h"
15 #include <linux/slab.h>
17 #define DM_MSG_PREFIX "multipath round-robin"
19 /*-----------------------------------------------------------------
20 * Path-handling code, paths are held in lists
21 *---------------------------------------------------------------*/
23 struct list_head list;
25 unsigned repeat_count;
28 static void free_paths(struct list_head *paths)
30 struct path_info *pi, *next;
32 list_for_each_entry_safe(pi, next, paths, list) {
38 /*-----------------------------------------------------------------
39 * Round-robin selector
40 *---------------------------------------------------------------*/
42 #define RR_MIN_IO 1000
45 struct list_head valid_paths;
46 struct list_head invalid_paths;
49 static struct selector *alloc_selector(void)
51 struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
54 INIT_LIST_HEAD(&s->valid_paths);
55 INIT_LIST_HEAD(&s->invalid_paths);
61 static int rr_create(struct path_selector *ps, unsigned argc, char **argv)
73 static void rr_destroy(struct path_selector *ps)
75 struct selector *s = (struct selector *) ps->context;
77 free_paths(&s->valid_paths);
78 free_paths(&s->invalid_paths);
83 static int rr_status(struct path_selector *ps, struct dm_path *path,
84 status_type_t type, char *result, unsigned int maxlen)
95 case STATUSTYPE_TABLE:
97 DMEMIT("%u ", pi->repeat_count);
106 * Called during initialisation to register each path with an
107 * optional repeat_count.
109 static int rr_add_path(struct path_selector *ps, struct dm_path *path,
110 int argc, char **argv, char **error)
112 struct selector *s = (struct selector *) ps->context;
113 struct path_info *pi;
114 unsigned repeat_count = RR_MIN_IO;
117 *error = "round-robin ps: incorrect number of arguments";
121 /* First path argument is number of I/Os before switching path */
122 if ((argc == 1) && (sscanf(argv[0], "%u", &repeat_count) != 1)) {
123 *error = "round-robin ps: invalid repeat count";
127 /* allocate the path */
128 pi = kmalloc(sizeof(*pi), GFP_KERNEL);
130 *error = "round-robin ps: Error allocating path context";
135 pi->repeat_count = repeat_count;
137 path->pscontext = pi;
139 list_add_tail(&pi->list, &s->valid_paths);
144 static void rr_fail_path(struct path_selector *ps, struct dm_path *p)
146 struct selector *s = (struct selector *) ps->context;
147 struct path_info *pi = p->pscontext;
149 list_move(&pi->list, &s->invalid_paths);
152 static int rr_reinstate_path(struct path_selector *ps, struct dm_path *p)
154 struct selector *s = (struct selector *) ps->context;
155 struct path_info *pi = p->pscontext;
157 list_move(&pi->list, &s->valid_paths);
162 static struct dm_path *rr_select_path(struct path_selector *ps,
163 unsigned *repeat_count)
165 struct selector *s = (struct selector *) ps->context;
166 struct path_info *pi = NULL;
168 if (!list_empty(&s->valid_paths)) {
169 pi = list_entry(s->valid_paths.next, struct path_info, list);
170 list_move_tail(&pi->list, &s->valid_paths);
171 *repeat_count = pi->repeat_count;
174 return pi ? pi->path : NULL;
177 static struct path_selector_type rr_ps = {
178 .name = "round-robin",
179 .module = THIS_MODULE,
183 .destroy = rr_destroy,
185 .add_path = rr_add_path,
186 .fail_path = rr_fail_path,
187 .reinstate_path = rr_reinstate_path,
188 .select_path = rr_select_path,
191 static int __init dm_rr_init(void)
193 int r = dm_register_path_selector(&rr_ps);
196 DMERR("register failed %d", r);
198 DMINFO("version 1.0.0 loaded");
203 static void __exit dm_rr_exit(void)
205 int r = dm_unregister_path_selector(&rr_ps);
208 DMERR("round-robin: unregister failed %d", r);
211 module_init(dm_rr_init);
212 module_exit(dm_rr_exit);
214 MODULE_DESCRIPTION(DM_NAME " round-robin multipath path selector");
215 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
216 MODULE_LICENSE("GPL");