13bd94003SHeinz Mauelshagen // SPDX-License-Identifier: GPL-2.0-only
2298fb372SMike Snitzer /*
3298fb372SMike Snitzer * Copyright (C) 2003 Sistina Software.
4298fb372SMike Snitzer * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
5298fb372SMike Snitzer *
6298fb372SMike Snitzer * Module Author: Heinz Mauelshagen
7298fb372SMike Snitzer *
8298fb372SMike Snitzer * This file is released under the GPL.
9298fb372SMike Snitzer *
10298fb372SMike Snitzer * Round-robin path selector.
11298fb372SMike Snitzer */
12298fb372SMike Snitzer
13298fb372SMike Snitzer #include <linux/device-mapper.h>
14298fb372SMike Snitzer
15298fb372SMike Snitzer #include "dm-path-selector.h"
16298fb372SMike Snitzer
17298fb372SMike Snitzer #include <linux/slab.h>
18298fb372SMike Snitzer #include <linux/module.h>
19298fb372SMike Snitzer
20298fb372SMike Snitzer #define DM_MSG_PREFIX "multipath round-robin"
21298fb372SMike Snitzer #define RR_MIN_IO 1
22298fb372SMike Snitzer #define RR_VERSION "1.2.0"
23298fb372SMike Snitzer
24*a4a82ce3SHeinz Mauelshagen /*
25*a4a82ce3SHeinz Mauelshagen *---------------------------------------------------------------
26298fb372SMike Snitzer * Path-handling code, paths are held in lists
27*a4a82ce3SHeinz Mauelshagen *---------------------------------------------------------------
28*a4a82ce3SHeinz Mauelshagen */
29298fb372SMike Snitzer struct path_info {
30298fb372SMike Snitzer struct list_head list;
31298fb372SMike Snitzer struct dm_path *path;
3286a3238cSHeinz Mauelshagen unsigned int repeat_count;
33298fb372SMike Snitzer };
34298fb372SMike Snitzer
free_paths(struct list_head * paths)35298fb372SMike Snitzer static void free_paths(struct list_head *paths)
36298fb372SMike Snitzer {
37298fb372SMike Snitzer struct path_info *pi, *next;
38298fb372SMike Snitzer
39298fb372SMike Snitzer list_for_each_entry_safe(pi, next, paths, list) {
40298fb372SMike Snitzer list_del(&pi->list);
41298fb372SMike Snitzer kfree(pi);
42298fb372SMike Snitzer }
43298fb372SMike Snitzer }
44298fb372SMike Snitzer
45*a4a82ce3SHeinz Mauelshagen /*
46*a4a82ce3SHeinz Mauelshagen *---------------------------------------------------------------
47298fb372SMike Snitzer * Round-robin selector
48*a4a82ce3SHeinz Mauelshagen *---------------------------------------------------------------
49*a4a82ce3SHeinz Mauelshagen */
50298fb372SMike Snitzer struct selector {
51298fb372SMike Snitzer struct list_head valid_paths;
52298fb372SMike Snitzer struct list_head invalid_paths;
53298fb372SMike Snitzer spinlock_t lock;
54298fb372SMike Snitzer };
55298fb372SMike Snitzer
alloc_selector(void)56298fb372SMike Snitzer static struct selector *alloc_selector(void)
57298fb372SMike Snitzer {
58298fb372SMike Snitzer struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
59298fb372SMike Snitzer
60298fb372SMike Snitzer if (s) {
61298fb372SMike Snitzer INIT_LIST_HEAD(&s->valid_paths);
62298fb372SMike Snitzer INIT_LIST_HEAD(&s->invalid_paths);
63298fb372SMike Snitzer spin_lock_init(&s->lock);
64298fb372SMike Snitzer }
65298fb372SMike Snitzer
66298fb372SMike Snitzer return s;
67298fb372SMike Snitzer }
68298fb372SMike Snitzer
rr_create(struct path_selector * ps,unsigned int argc,char ** argv)6986a3238cSHeinz Mauelshagen static int rr_create(struct path_selector *ps, unsigned int argc, char **argv)
70298fb372SMike Snitzer {
71298fb372SMike Snitzer struct selector *s;
72298fb372SMike Snitzer
73298fb372SMike Snitzer s = alloc_selector();
74298fb372SMike Snitzer if (!s)
75298fb372SMike Snitzer return -ENOMEM;
76298fb372SMike Snitzer
77298fb372SMike Snitzer ps->context = s;
78298fb372SMike Snitzer return 0;
79298fb372SMike Snitzer }
80298fb372SMike Snitzer
rr_destroy(struct path_selector * ps)81298fb372SMike Snitzer static void rr_destroy(struct path_selector *ps)
82298fb372SMike Snitzer {
83298fb372SMike Snitzer struct selector *s = ps->context;
84298fb372SMike Snitzer
85298fb372SMike Snitzer free_paths(&s->valid_paths);
86298fb372SMike Snitzer free_paths(&s->invalid_paths);
87298fb372SMike Snitzer kfree(s);
88298fb372SMike Snitzer ps->context = NULL;
89298fb372SMike Snitzer }
90298fb372SMike Snitzer
rr_status(struct path_selector * ps,struct dm_path * path,status_type_t type,char * result,unsigned int maxlen)91298fb372SMike Snitzer static int rr_status(struct path_selector *ps, struct dm_path *path,
92298fb372SMike Snitzer status_type_t type, char *result, unsigned int maxlen)
93298fb372SMike Snitzer {
94298fb372SMike Snitzer struct path_info *pi;
95298fb372SMike Snitzer int sz = 0;
96298fb372SMike Snitzer
97298fb372SMike Snitzer if (!path)
98298fb372SMike Snitzer DMEMIT("0 ");
99298fb372SMike Snitzer else {
100298fb372SMike Snitzer switch (type) {
101298fb372SMike Snitzer case STATUSTYPE_INFO:
102298fb372SMike Snitzer break;
103298fb372SMike Snitzer case STATUSTYPE_TABLE:
104298fb372SMike Snitzer pi = path->pscontext;
105298fb372SMike Snitzer DMEMIT("%u ", pi->repeat_count);
106298fb372SMike Snitzer break;
1078ec45662STushar Sugandhi
1088ec45662STushar Sugandhi case STATUSTYPE_IMA:
1098ec45662STushar Sugandhi *result = '\0';
1108ec45662STushar Sugandhi break;
111298fb372SMike Snitzer }
112298fb372SMike Snitzer }
113298fb372SMike Snitzer
114298fb372SMike Snitzer return sz;
115298fb372SMike Snitzer }
116298fb372SMike Snitzer
117298fb372SMike Snitzer /*
118298fb372SMike Snitzer * Called during initialisation to register each path with an
119298fb372SMike Snitzer * optional repeat_count.
120298fb372SMike Snitzer */
rr_add_path(struct path_selector * ps,struct dm_path * path,int argc,char ** argv,char ** error)121298fb372SMike Snitzer static int rr_add_path(struct path_selector *ps, struct dm_path *path,
122298fb372SMike Snitzer int argc, char **argv, char **error)
123298fb372SMike Snitzer {
124298fb372SMike Snitzer struct selector *s = ps->context;
125298fb372SMike Snitzer struct path_info *pi;
12686a3238cSHeinz Mauelshagen unsigned int repeat_count = RR_MIN_IO;
127298fb372SMike Snitzer char dummy;
128298fb372SMike Snitzer unsigned long flags;
129298fb372SMike Snitzer
130298fb372SMike Snitzer if (argc > 1) {
131298fb372SMike Snitzer *error = "round-robin ps: incorrect number of arguments";
132298fb372SMike Snitzer return -EINVAL;
133298fb372SMike Snitzer }
134298fb372SMike Snitzer
135298fb372SMike Snitzer /* First path argument is number of I/Os before switching path */
136298fb372SMike Snitzer if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
137298fb372SMike Snitzer *error = "round-robin ps: invalid repeat count";
138298fb372SMike Snitzer return -EINVAL;
139298fb372SMike Snitzer }
140298fb372SMike Snitzer
141298fb372SMike Snitzer if (repeat_count > 1) {
142298fb372SMike Snitzer DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
143298fb372SMike Snitzer repeat_count = 1;
144298fb372SMike Snitzer }
145298fb372SMike Snitzer
146298fb372SMike Snitzer /* allocate the path */
147298fb372SMike Snitzer pi = kmalloc(sizeof(*pi), GFP_KERNEL);
148298fb372SMike Snitzer if (!pi) {
149298fb372SMike Snitzer *error = "round-robin ps: Error allocating path context";
150298fb372SMike Snitzer return -ENOMEM;
151298fb372SMike Snitzer }
152298fb372SMike Snitzer
153298fb372SMike Snitzer pi->path = path;
154298fb372SMike Snitzer pi->repeat_count = repeat_count;
155298fb372SMike Snitzer
156298fb372SMike Snitzer path->pscontext = pi;
157298fb372SMike Snitzer
158298fb372SMike Snitzer spin_lock_irqsave(&s->lock, flags);
159298fb372SMike Snitzer list_add_tail(&pi->list, &s->valid_paths);
160298fb372SMike Snitzer spin_unlock_irqrestore(&s->lock, flags);
161298fb372SMike Snitzer
162298fb372SMike Snitzer return 0;
163298fb372SMike Snitzer }
164298fb372SMike Snitzer
rr_fail_path(struct path_selector * ps,struct dm_path * p)165298fb372SMike Snitzer static void rr_fail_path(struct path_selector *ps, struct dm_path *p)
166298fb372SMike Snitzer {
167298fb372SMike Snitzer unsigned long flags;
168298fb372SMike Snitzer struct selector *s = ps->context;
169298fb372SMike Snitzer struct path_info *pi = p->pscontext;
170298fb372SMike Snitzer
171298fb372SMike Snitzer spin_lock_irqsave(&s->lock, flags);
172298fb372SMike Snitzer list_move(&pi->list, &s->invalid_paths);
173298fb372SMike Snitzer spin_unlock_irqrestore(&s->lock, flags);
174298fb372SMike Snitzer }
175298fb372SMike Snitzer
rr_reinstate_path(struct path_selector * ps,struct dm_path * p)176298fb372SMike Snitzer static int rr_reinstate_path(struct path_selector *ps, struct dm_path *p)
177298fb372SMike Snitzer {
178298fb372SMike Snitzer unsigned long flags;
179298fb372SMike Snitzer struct selector *s = ps->context;
180298fb372SMike Snitzer struct path_info *pi = p->pscontext;
181298fb372SMike Snitzer
182298fb372SMike Snitzer spin_lock_irqsave(&s->lock, flags);
183298fb372SMike Snitzer list_move(&pi->list, &s->valid_paths);
184298fb372SMike Snitzer spin_unlock_irqrestore(&s->lock, flags);
185298fb372SMike Snitzer
186298fb372SMike Snitzer return 0;
187298fb372SMike Snitzer }
188298fb372SMike Snitzer
rr_select_path(struct path_selector * ps,size_t nr_bytes)189298fb372SMike Snitzer static struct dm_path *rr_select_path(struct path_selector *ps, size_t nr_bytes)
190298fb372SMike Snitzer {
191298fb372SMike Snitzer unsigned long flags;
192298fb372SMike Snitzer struct selector *s = ps->context;
193298fb372SMike Snitzer struct path_info *pi = NULL;
194298fb372SMike Snitzer
195298fb372SMike Snitzer spin_lock_irqsave(&s->lock, flags);
196298fb372SMike Snitzer if (!list_empty(&s->valid_paths)) {
197298fb372SMike Snitzer pi = list_entry(s->valid_paths.next, struct path_info, list);
198298fb372SMike Snitzer list_move_tail(&pi->list, &s->valid_paths);
199298fb372SMike Snitzer }
200298fb372SMike Snitzer spin_unlock_irqrestore(&s->lock, flags);
201298fb372SMike Snitzer
202298fb372SMike Snitzer return pi ? pi->path : NULL;
203298fb372SMike Snitzer }
204298fb372SMike Snitzer
205298fb372SMike Snitzer static struct path_selector_type rr_ps = {
206298fb372SMike Snitzer .name = "round-robin",
207298fb372SMike Snitzer .module = THIS_MODULE,
208298fb372SMike Snitzer .table_args = 1,
209298fb372SMike Snitzer .info_args = 0,
210298fb372SMike Snitzer .create = rr_create,
211298fb372SMike Snitzer .destroy = rr_destroy,
212298fb372SMike Snitzer .status = rr_status,
213298fb372SMike Snitzer .add_path = rr_add_path,
214298fb372SMike Snitzer .fail_path = rr_fail_path,
215298fb372SMike Snitzer .reinstate_path = rr_reinstate_path,
216298fb372SMike Snitzer .select_path = rr_select_path,
217298fb372SMike Snitzer };
218298fb372SMike Snitzer
dm_rr_init(void)219298fb372SMike Snitzer static int __init dm_rr_init(void)
220298fb372SMike Snitzer {
221298fb372SMike Snitzer int r = dm_register_path_selector(&rr_ps);
222298fb372SMike Snitzer
223298fb372SMike Snitzer if (r < 0)
224298fb372SMike Snitzer DMERR("register failed %d", r);
225298fb372SMike Snitzer
226298fb372SMike Snitzer DMINFO("version " RR_VERSION " loaded");
227298fb372SMike Snitzer
228298fb372SMike Snitzer return r;
229298fb372SMike Snitzer }
230298fb372SMike Snitzer
dm_rr_exit(void)231298fb372SMike Snitzer static void __exit dm_rr_exit(void)
232298fb372SMike Snitzer {
233298fb372SMike Snitzer int r = dm_unregister_path_selector(&rr_ps);
234298fb372SMike Snitzer
235298fb372SMike Snitzer if (r < 0)
236298fb372SMike Snitzer DMERR("unregister failed %d", r);
237298fb372SMike Snitzer }
238298fb372SMike Snitzer
239298fb372SMike Snitzer module_init(dm_rr_init);
240298fb372SMike Snitzer module_exit(dm_rr_exit);
241298fb372SMike Snitzer
242298fb372SMike Snitzer MODULE_DESCRIPTION(DM_NAME " round-robin multipath path selector");
243298fb372SMike Snitzer MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
244298fb372SMike Snitzer MODULE_LICENSE("GPL");
245