65ffdc3403b6ce963eb89b6cf3fe09aeb88d734f
[cascardo/linux.git] / drivers / md / dm-round-robin.c
1 /*
2  * Copyright (C) 2003 Sistina Software.
3  * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4  *
5  * Module Author: Heinz Mauelshagen
6  *
7  * This file is released under the GPL.
8  *
9  * Round-robin path selector.
10  */
11
12 #include <linux/device-mapper.h>
13
14 #include "dm-path-selector.h"
15
16 #include <linux/slab.h>
17 #include <linux/module.h>
18
19 #define DM_MSG_PREFIX "multipath round-robin"
20 #define RR_MIN_IO     1
21 #define RR_VERSION    "1.1.0"
22
23 /*-----------------------------------------------------------------
24  * Path-handling code, paths are held in lists
25  *---------------------------------------------------------------*/
26 struct path_info {
27         struct list_head list;
28         struct dm_path *path;
29         unsigned repeat_count;
30 };
31
32 static void free_paths(struct list_head *paths)
33 {
34         struct path_info *pi, *next;
35
36         list_for_each_entry_safe(pi, next, paths, list) {
37                 list_del(&pi->list);
38                 kfree(pi);
39         }
40 }
41
42 /*-----------------------------------------------------------------
43  * Round-robin selector
44  *---------------------------------------------------------------*/
45
46 struct selector {
47         struct list_head valid_paths;
48         struct list_head invalid_paths;
49 };
50
51 static struct selector *alloc_selector(void)
52 {
53         struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
54
55         if (s) {
56                 INIT_LIST_HEAD(&s->valid_paths);
57                 INIT_LIST_HEAD(&s->invalid_paths);
58         }
59
60         return s;
61 }
62
63 static int rr_create(struct path_selector *ps, unsigned argc, char **argv)
64 {
65         struct selector *s;
66
67         s = alloc_selector();
68         if (!s)
69                 return -ENOMEM;
70
71         ps->context = s;
72         return 0;
73 }
74
75 static void rr_destroy(struct path_selector *ps)
76 {
77         struct selector *s = (struct selector *) ps->context;
78
79         free_paths(&s->valid_paths);
80         free_paths(&s->invalid_paths);
81         kfree(s);
82         ps->context = NULL;
83 }
84
85 static int rr_status(struct path_selector *ps, struct dm_path *path,
86                      status_type_t type, char *result, unsigned int maxlen)
87 {
88         struct path_info *pi;
89         int sz = 0;
90
91         if (!path)
92                 DMEMIT("0 ");
93         else {
94                 switch(type) {
95                 case STATUSTYPE_INFO:
96                         break;
97                 case STATUSTYPE_TABLE:
98                         pi = path->pscontext;
99                         DMEMIT("%u ", pi->repeat_count);
100                         break;
101                 }
102         }
103
104         return sz;
105 }
106
107 /*
108  * Called during initialisation to register each path with an
109  * optional repeat_count.
110  */
111 static int rr_add_path(struct path_selector *ps, struct dm_path *path,
112                        int argc, char **argv, char **error)
113 {
114         struct selector *s = (struct selector *) ps->context;
115         struct path_info *pi;
116         unsigned repeat_count = RR_MIN_IO;
117         char dummy;
118
119         if (argc > 1) {
120                 *error = "round-robin ps: incorrect number of arguments";
121                 return -EINVAL;
122         }
123
124         /* First path argument is number of I/Os before switching path */
125         if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
126                 *error = "round-robin ps: invalid repeat count";
127                 return -EINVAL;
128         }
129
130         if (repeat_count > 1) {
131                 DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
132                 repeat_count = 1;
133         }
134
135         /* allocate the path */
136         pi = kmalloc(sizeof(*pi), GFP_KERNEL);
137         if (!pi) {
138                 *error = "round-robin ps: Error allocating path context";
139                 return -ENOMEM;
140         }
141
142         pi->path = path;
143         pi->repeat_count = repeat_count;
144
145         path->pscontext = pi;
146
147         list_add_tail(&pi->list, &s->valid_paths);
148
149         return 0;
150 }
151
152 static void rr_fail_path(struct path_selector *ps, struct dm_path *p)
153 {
154         struct selector *s = (struct selector *) ps->context;
155         struct path_info *pi = p->pscontext;
156
157         list_move(&pi->list, &s->invalid_paths);
158 }
159
160 static int rr_reinstate_path(struct path_selector *ps, struct dm_path *p)
161 {
162         struct selector *s = (struct selector *) ps->context;
163         struct path_info *pi = p->pscontext;
164
165         list_move(&pi->list, &s->valid_paths);
166
167         return 0;
168 }
169
170 static struct dm_path *rr_select_path(struct path_selector *ps,
171                                       unsigned *repeat_count, size_t nr_bytes)
172 {
173         struct selector *s = (struct selector *) ps->context;
174         struct path_info *pi = NULL;
175
176         if (!list_empty(&s->valid_paths)) {
177                 pi = list_entry(s->valid_paths.next, struct path_info, list);
178                 list_move_tail(&pi->list, &s->valid_paths);
179                 *repeat_count = pi->repeat_count;
180         }
181
182         return pi ? pi->path : NULL;
183 }
184
185 static struct path_selector_type rr_ps = {
186         .name = "round-robin",
187         .module = THIS_MODULE,
188         .table_args = 1,
189         .info_args = 0,
190         .create = rr_create,
191         .destroy = rr_destroy,
192         .status = rr_status,
193         .add_path = rr_add_path,
194         .fail_path = rr_fail_path,
195         .reinstate_path = rr_reinstate_path,
196         .select_path = rr_select_path,
197 };
198
199 static int __init dm_rr_init(void)
200 {
201         int r = dm_register_path_selector(&rr_ps);
202
203         if (r < 0)
204                 DMERR("register failed %d", r);
205
206         DMINFO("version " RR_VERSION " loaded");
207
208         return r;
209 }
210
211 static void __exit dm_rr_exit(void)
212 {
213         int r = dm_unregister_path_selector(&rr_ps);
214
215         if (r < 0)
216                 DMERR("unregister failed %d", r);
217 }
218
219 module_init(dm_rr_init);
220 module_exit(dm_rr_exit);
221
222 MODULE_DESCRIPTION(DM_NAME " round-robin multipath path selector");
223 MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
224 MODULE_LICENSE("GPL");