blob: be9aa7f30b61aedc3b3ab41ae48dbe4ffd4838c2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Mikulas Patocka586e80e2008-10-21 17:44:59 +010012#include <linux/device-mapper.h>
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "dm-path-selector.h"
15
16#include <linux/slab.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040017#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Alasdair G Kergon72d94862006-06-26 00:27:35 -070019#define DM_MSG_PREFIX "multipath round-robin"
Mike Snitzer21136f82016-02-10 11:58:45 -050020#define RR_MIN_IO 1
21#define RR_VERSION "1.1.0"
Alasdair G Kergon72d94862006-06-26 00:27:35 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/*-----------------------------------------------------------------
24 * Path-handling code, paths are held in lists
25 *---------------------------------------------------------------*/
26struct path_info {
27 struct list_head list;
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080028 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 unsigned repeat_count;
30};
31
32static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070046struct selector {
47 struct list_head valid_paths;
48 struct list_head invalid_paths;
Mike Snitzer9659f812016-02-15 14:25:00 -050049 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
52static struct selector *alloc_selector(void)
53{
54 struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL);
55
56 if (s) {
57 INIT_LIST_HEAD(&s->valid_paths);
58 INIT_LIST_HEAD(&s->invalid_paths);
Mike Snitzer9659f812016-02-15 14:25:00 -050059 spin_lock_init(&s->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
61
62 return s;
63}
64
65static int rr_create(struct path_selector *ps, unsigned argc, char **argv)
66{
67 struct selector *s;
68
69 s = alloc_selector();
70 if (!s)
71 return -ENOMEM;
72
73 ps->context = s;
74 return 0;
75}
76
77static void rr_destroy(struct path_selector *ps)
78{
Mike Snitzer9659f812016-02-15 14:25:00 -050079 struct selector *s = ps->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 free_paths(&s->valid_paths);
82 free_paths(&s->invalid_paths);
83 kfree(s);
84 ps->context = NULL;
85}
86
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080087static int rr_status(struct path_selector *ps, struct dm_path *path,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 status_type_t type, char *result, unsigned int maxlen)
89{
90 struct path_info *pi;
91 int sz = 0;
92
93 if (!path)
94 DMEMIT("0 ");
95 else {
96 switch(type) {
97 case STATUSTYPE_INFO:
98 break;
99 case STATUSTYPE_TABLE:
100 pi = path->pscontext;
101 DMEMIT("%u ", pi->repeat_count);
102 break;
103 }
104 }
105
106 return sz;
107}
108
109/*
110 * Called during initialisation to register each path with an
111 * optional repeat_count.
112 */
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800113static int rr_add_path(struct path_selector *ps, struct dm_path *path,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 int argc, char **argv, char **error)
115{
Mike Snitzer9659f812016-02-15 14:25:00 -0500116 struct selector *s = ps->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 struct path_info *pi;
118 unsigned repeat_count = RR_MIN_IO;
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100119 char dummy;
Mike Snitzer9659f812016-02-15 14:25:00 -0500120 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if (argc > 1) {
123 *error = "round-robin ps: incorrect number of arguments";
124 return -EINVAL;
125 }
126
127 /* First path argument is number of I/Os before switching path */
Mikulas Patocka31998ef2012-03-28 18:41:26 +0100128 if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 *error = "round-robin ps: invalid repeat count";
130 return -EINVAL;
131 }
132
Mike Snitzer21136f82016-02-10 11:58:45 -0500133 if (repeat_count > 1) {
134 DMWARN_LIMIT("repeat_count > 1 is deprecated, using 1 instead");
135 repeat_count = 1;
136 }
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /* allocate the path */
139 pi = kmalloc(sizeof(*pi), GFP_KERNEL);
140 if (!pi) {
141 *error = "round-robin ps: Error allocating path context";
142 return -ENOMEM;
143 }
144
145 pi->path = path;
146 pi->repeat_count = repeat_count;
147
148 path->pscontext = pi;
149
Mike Snitzer9659f812016-02-15 14:25:00 -0500150 spin_lock_irqsave(&s->lock, flags);
Jonathan E Brassow5d55fdf92006-11-08 17:44:43 -0800151 list_add_tail(&pi->list, &s->valid_paths);
Mike Snitzer9659f812016-02-15 14:25:00 -0500152 spin_unlock_irqrestore(&s->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 return 0;
155}
156
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800157static void rr_fail_path(struct path_selector *ps, struct dm_path *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Mike Snitzer9659f812016-02-15 14:25:00 -0500159 unsigned long flags;
160 struct selector *s = ps->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 struct path_info *pi = p->pscontext;
162
Mike Snitzer9659f812016-02-15 14:25:00 -0500163 spin_lock_irqsave(&s->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 list_move(&pi->list, &s->invalid_paths);
Mike Snitzer9659f812016-02-15 14:25:00 -0500165 spin_unlock_irqrestore(&s->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800168static int rr_reinstate_path(struct path_selector *ps, struct dm_path *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Mike Snitzer9659f812016-02-15 14:25:00 -0500170 unsigned long flags;
171 struct selector *s = ps->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct path_info *pi = p->pscontext;
173
Mike Snitzer9659f812016-02-15 14:25:00 -0500174 spin_lock_irqsave(&s->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 list_move(&pi->list, &s->valid_paths);
Mike Snitzer9659f812016-02-15 14:25:00 -0500176 spin_unlock_irqrestore(&s->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 return 0;
179}
180
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800181static struct dm_path *rr_select_path(struct path_selector *ps,
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100182 unsigned *repeat_count, size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Mike Snitzer9659f812016-02-15 14:25:00 -0500184 unsigned long flags;
185 struct selector *s = ps->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct path_info *pi = NULL;
187
Mike Snitzer9659f812016-02-15 14:25:00 -0500188 spin_lock_irqsave(&s->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (!list_empty(&s->valid_paths)) {
190 pi = list_entry(s->valid_paths.next, struct path_info, list);
191 list_move_tail(&pi->list, &s->valid_paths);
192 *repeat_count = pi->repeat_count;
193 }
Mike Snitzer9659f812016-02-15 14:25:00 -0500194 spin_unlock_irqrestore(&s->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 return pi ? pi->path : NULL;
197}
198
199static struct path_selector_type rr_ps = {
200 .name = "round-robin",
201 .module = THIS_MODULE,
202 .table_args = 1,
203 .info_args = 0,
204 .create = rr_create,
205 .destroy = rr_destroy,
206 .status = rr_status,
207 .add_path = rr_add_path,
208 .fail_path = rr_fail_path,
209 .reinstate_path = rr_reinstate_path,
210 .select_path = rr_select_path,
211};
212
213static int __init dm_rr_init(void)
214{
215 int r = dm_register_path_selector(&rr_ps);
216
217 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700218 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Mike Snitzer21136f82016-02-10 11:58:45 -0500220 DMINFO("version " RR_VERSION " loaded");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 return r;
223}
224
225static void __exit dm_rr_exit(void)
226{
227 int r = dm_unregister_path_selector(&rr_ps);
228
229 if (r < 0)
Alasdair G Kergon0cd33122007-07-12 17:27:01 +0100230 DMERR("unregister failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233module_init(dm_rr_init);
234module_exit(dm_rr_exit);
235
236MODULE_DESCRIPTION(DM_NAME " round-robin multipath path selector");
237MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
238MODULE_LICENSE("GPL");