Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Mikulas Patocka | 586e80e | 2008-10-21 17:44:59 +0100 | [diff] [blame] | 12 | #include <linux/device-mapper.h> |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "dm-path-selector.h" |
| 15 | |
| 16 | #include <linux/slab.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 17 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 19 | #define DM_MSG_PREFIX "multipath round-robin" |
Mike Snitzer | b0b477c | 2016-02-17 15:04:05 -0500 | [diff] [blame] | 20 | #define RR_MIN_IO 1000 |
Mike Snitzer | 21136f8 | 2016-02-10 11:58:45 -0500 | [diff] [blame] | 21 | #define RR_VERSION "1.1.0" |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | /*----------------------------------------------------------------- |
| 24 | * Path-handling code, paths are held in lists |
| 25 | *---------------------------------------------------------------*/ |
| 26 | struct path_info { |
| 27 | struct list_head list; |
Josef "Jeff" Sipek | c922d5f | 2006-12-08 02:36:33 -0800 | [diff] [blame] | 28 | struct dm_path *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | struct selector { |
| 47 | struct list_head valid_paths; |
| 48 | struct list_head invalid_paths; |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 49 | spinlock_t lock; |
Mike Snitzer | b0b477c | 2016-02-17 15:04:05 -0500 | [diff] [blame] | 50 | struct dm_path * __percpu *current_path; |
| 51 | struct percpu_counter repeat_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Mike Snitzer | b0b477c | 2016-02-17 15:04:05 -0500 | [diff] [blame] | 54 | static void set_percpu_current_path(struct selector *s, struct dm_path *path) |
| 55 | { |
| 56 | int cpu; |
| 57 | |
| 58 | for_each_possible_cpu(cpu) |
| 59 | *per_cpu_ptr(s->current_path, cpu) = path; |
| 60 | } |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static struct selector *alloc_selector(void) |
| 63 | { |
| 64 | struct selector *s = kmalloc(sizeof(*s), GFP_KERNEL); |
| 65 | |
Mike Snitzer | b0b477c | 2016-02-17 15:04:05 -0500 | [diff] [blame] | 66 | if (!s) |
| 67 | return NULL; |
| 68 | |
| 69 | INIT_LIST_HEAD(&s->valid_paths); |
| 70 | INIT_LIST_HEAD(&s->invalid_paths); |
| 71 | spin_lock_init(&s->lock); |
| 72 | |
| 73 | s->current_path = alloc_percpu(struct dm_path *); |
| 74 | if (!s->current_path) |
| 75 | goto out_current_path; |
| 76 | set_percpu_current_path(s, NULL); |
| 77 | |
| 78 | if (percpu_counter_init(&s->repeat_count, 0, GFP_KERNEL)) |
| 79 | goto out_repeat_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | return s; |
Mike Snitzer | b0b477c | 2016-02-17 15:04:05 -0500 | [diff] [blame] | 82 | |
| 83 | out_repeat_count: |
| 84 | free_percpu(s->current_path); |
| 85 | out_current_path: |
| 86 | kfree(s); |
| 87 | return NULL;; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static int rr_create(struct path_selector *ps, unsigned argc, char **argv) |
| 91 | { |
| 92 | struct selector *s; |
| 93 | |
| 94 | s = alloc_selector(); |
| 95 | if (!s) |
| 96 | return -ENOMEM; |
| 97 | |
| 98 | ps->context = s; |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static void rr_destroy(struct path_selector *ps) |
| 103 | { |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 104 | struct selector *s = ps->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | free_paths(&s->valid_paths); |
| 107 | free_paths(&s->invalid_paths); |
Mike Snitzer | b0b477c | 2016-02-17 15:04:05 -0500 | [diff] [blame] | 108 | free_percpu(s->current_path); |
| 109 | percpu_counter_destroy(&s->repeat_count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | kfree(s); |
| 111 | ps->context = NULL; |
| 112 | } |
| 113 | |
Josef "Jeff" Sipek | c922d5f | 2006-12-08 02:36:33 -0800 | [diff] [blame] | 114 | static int rr_status(struct path_selector *ps, struct dm_path *path, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | status_type_t type, char *result, unsigned int maxlen) |
| 116 | { |
| 117 | struct path_info *pi; |
| 118 | int sz = 0; |
| 119 | |
| 120 | if (!path) |
| 121 | DMEMIT("0 "); |
| 122 | else { |
| 123 | switch(type) { |
| 124 | case STATUSTYPE_INFO: |
| 125 | break; |
| 126 | case STATUSTYPE_TABLE: |
| 127 | pi = path->pscontext; |
| 128 | DMEMIT("%u ", pi->repeat_count); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return sz; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Called during initialisation to register each path with an |
| 138 | * optional repeat_count. |
| 139 | */ |
Josef "Jeff" Sipek | c922d5f | 2006-12-08 02:36:33 -0800 | [diff] [blame] | 140 | static int rr_add_path(struct path_selector *ps, struct dm_path *path, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | int argc, char **argv, char **error) |
| 142 | { |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 143 | struct selector *s = ps->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | struct path_info *pi; |
| 145 | unsigned repeat_count = RR_MIN_IO; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 146 | char dummy; |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 147 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | if (argc > 1) { |
| 150 | *error = "round-robin ps: incorrect number of arguments"; |
| 151 | return -EINVAL; |
| 152 | } |
| 153 | |
| 154 | /* First path argument is number of I/Os before switching path */ |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 155 | if ((argc == 1) && (sscanf(argv[0], "%u%c", &repeat_count, &dummy) != 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | *error = "round-robin ps: invalid repeat count"; |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | |
| 160 | /* allocate the path */ |
| 161 | pi = kmalloc(sizeof(*pi), GFP_KERNEL); |
| 162 | if (!pi) { |
| 163 | *error = "round-robin ps: Error allocating path context"; |
| 164 | return -ENOMEM; |
| 165 | } |
| 166 | |
| 167 | pi->path = path; |
| 168 | pi->repeat_count = repeat_count; |
| 169 | |
| 170 | path->pscontext = pi; |
| 171 | |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 172 | spin_lock_irqsave(&s->lock, flags); |
Jonathan E Brassow | 5d55fdf | 2006-11-08 17:44:43 -0800 | [diff] [blame] | 173 | list_add_tail(&pi->list, &s->valid_paths); |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 174 | spin_unlock_irqrestore(&s->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Josef "Jeff" Sipek | c922d5f | 2006-12-08 02:36:33 -0800 | [diff] [blame] | 179 | static void rr_fail_path(struct path_selector *ps, struct dm_path *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 181 | unsigned long flags; |
| 182 | struct selector *s = ps->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | struct path_info *pi = p->pscontext; |
| 184 | |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 185 | spin_lock_irqsave(&s->lock, flags); |
Mike Snitzer | b0b477c | 2016-02-17 15:04:05 -0500 | [diff] [blame] | 186 | if (p == *this_cpu_ptr(s->current_path)) |
| 187 | set_percpu_current_path(s, NULL); |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | list_move(&pi->list, &s->invalid_paths); |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 190 | spin_unlock_irqrestore(&s->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Josef "Jeff" Sipek | c922d5f | 2006-12-08 02:36:33 -0800 | [diff] [blame] | 193 | static int rr_reinstate_path(struct path_selector *ps, struct dm_path *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 195 | unsigned long flags; |
| 196 | struct selector *s = ps->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | struct path_info *pi = p->pscontext; |
| 198 | |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 199 | spin_lock_irqsave(&s->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | list_move(&pi->list, &s->valid_paths); |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 201 | spin_unlock_irqrestore(&s->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
Mike Snitzer | 90a4323 | 2016-02-17 21:29:17 -0500 | [diff] [blame] | 206 | static struct dm_path *rr_select_path(struct path_selector *ps, size_t nr_bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 208 | unsigned long flags; |
| 209 | struct selector *s = ps->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | struct path_info *pi = NULL; |
Mike Snitzer | b0b477c | 2016-02-17 15:04:05 -0500 | [diff] [blame] | 211 | struct dm_path *current_path = NULL; |
| 212 | |
| 213 | current_path = *this_cpu_ptr(s->current_path); |
| 214 | if (current_path) { |
| 215 | percpu_counter_dec(&s->repeat_count); |
| 216 | if (percpu_counter_read_positive(&s->repeat_count) > 0) |
| 217 | return current_path; |
| 218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 220 | spin_lock_irqsave(&s->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | if (!list_empty(&s->valid_paths)) { |
| 222 | pi = list_entry(s->valid_paths.next, struct path_info, list); |
| 223 | list_move_tail(&pi->list, &s->valid_paths); |
Mike Snitzer | b0b477c | 2016-02-17 15:04:05 -0500 | [diff] [blame] | 224 | percpu_counter_set(&s->repeat_count, pi->repeat_count); |
| 225 | set_percpu_current_path(s, pi->path); |
| 226 | current_path = pi->path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
Mike Snitzer | 9659f81 | 2016-02-15 14:25:00 -0500 | [diff] [blame] | 228 | spin_unlock_irqrestore(&s->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Mike Snitzer | b0b477c | 2016-02-17 15:04:05 -0500 | [diff] [blame] | 230 | return current_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static struct path_selector_type rr_ps = { |
| 234 | .name = "round-robin", |
| 235 | .module = THIS_MODULE, |
| 236 | .table_args = 1, |
| 237 | .info_args = 0, |
| 238 | .create = rr_create, |
| 239 | .destroy = rr_destroy, |
| 240 | .status = rr_status, |
| 241 | .add_path = rr_add_path, |
| 242 | .fail_path = rr_fail_path, |
| 243 | .reinstate_path = rr_reinstate_path, |
| 244 | .select_path = rr_select_path, |
| 245 | }; |
| 246 | |
| 247 | static int __init dm_rr_init(void) |
| 248 | { |
| 249 | int r = dm_register_path_selector(&rr_ps); |
| 250 | |
| 251 | if (r < 0) |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 252 | DMERR("register failed %d", r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Mike Snitzer | 21136f8 | 2016-02-10 11:58:45 -0500 | [diff] [blame] | 254 | DMINFO("version " RR_VERSION " loaded"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | return r; |
| 257 | } |
| 258 | |
| 259 | static void __exit dm_rr_exit(void) |
| 260 | { |
| 261 | int r = dm_unregister_path_selector(&rr_ps); |
| 262 | |
| 263 | if (r < 0) |
Alasdair G Kergon | 0cd3312 | 2007-07-12 17:27:01 +0100 | [diff] [blame] | 264 | DMERR("unregister failed %d", r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | module_init(dm_rr_init); |
| 268 | module_exit(dm_rr_exit); |
| 269 | |
| 270 | MODULE_DESCRIPTION(DM_NAME " round-robin multipath path selector"); |
| 271 | MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>"); |
| 272 | MODULE_LICENSE("GPL"); |