| 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" | 
|  | 20 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | /*----------------------------------------------------------------- | 
|  | 22 | * Path-handling code, paths are held in lists | 
|  | 23 | *---------------------------------------------------------------*/ | 
|  | 24 | struct path_info { | 
|  | 25 | struct list_head list; | 
| Josef "Jeff" Sipek | c922d5f | 2006-12-08 02:36:33 -0800 | [diff] [blame] | 26 | struct dm_path *path; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | unsigned repeat_count; | 
|  | 28 | }; | 
|  | 29 |  | 
|  | 30 | static void free_paths(struct list_head *paths) | 
|  | 31 | { | 
|  | 32 | struct path_info *pi, *next; | 
|  | 33 |  | 
|  | 34 | list_for_each_entry_safe(pi, next, paths, list) { | 
|  | 35 | list_del(&pi->list); | 
|  | 36 | kfree(pi); | 
|  | 37 | } | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | /*----------------------------------------------------------------- | 
|  | 41 | * Round-robin selector | 
|  | 42 | *---------------------------------------------------------------*/ | 
|  | 43 |  | 
|  | 44 | #define RR_MIN_IO		1000 | 
|  | 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 |  | 
| Josef "Jeff" Sipek | c922d5f | 2006-12-08 02:36:33 -0800 | [diff] [blame] | 85 | static int rr_status(struct path_selector *ps, struct dm_path *path, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ | 
| Josef "Jeff" Sipek | c922d5f | 2006-12-08 02:36:33 -0800 | [diff] [blame] | 111 | 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] | 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 |  | 
|  | 118 | if (argc > 1) { | 
|  | 119 | *error = "round-robin ps: incorrect number of arguments"; | 
|  | 120 | return -EINVAL; | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | /* First path argument is number of I/Os before switching path */ | 
|  | 124 | if ((argc == 1) && (sscanf(argv[0], "%u", &repeat_count) != 1)) { | 
|  | 125 | *error = "round-robin ps: invalid repeat count"; | 
|  | 126 | return -EINVAL; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | /* allocate the path */ | 
|  | 130 | pi = kmalloc(sizeof(*pi), GFP_KERNEL); | 
|  | 131 | if (!pi) { | 
|  | 132 | *error = "round-robin ps: Error allocating path context"; | 
|  | 133 | return -ENOMEM; | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | pi->path = path; | 
|  | 137 | pi->repeat_count = repeat_count; | 
|  | 138 |  | 
|  | 139 | path->pscontext = pi; | 
|  | 140 |  | 
| Jonathan E Brassow | 5d55fdf | 2006-11-08 17:44:43 -0800 | [diff] [blame] | 141 | list_add_tail(&pi->list, &s->valid_paths); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 |  | 
|  | 143 | return 0; | 
|  | 144 | } | 
|  | 145 |  | 
| Josef "Jeff" Sipek | c922d5f | 2006-12-08 02:36:33 -0800 | [diff] [blame] | 146 | 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] | 147 | { | 
|  | 148 | struct selector *s = (struct selector *) ps->context; | 
|  | 149 | struct path_info *pi = p->pscontext; | 
|  | 150 |  | 
|  | 151 | list_move(&pi->list, &s->invalid_paths); | 
|  | 152 | } | 
|  | 153 |  | 
| Josef "Jeff" Sipek | c922d5f | 2006-12-08 02:36:33 -0800 | [diff] [blame] | 154 | 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] | 155 | { | 
|  | 156 | struct selector *s = (struct selector *) ps->context; | 
|  | 157 | struct path_info *pi = p->pscontext; | 
|  | 158 |  | 
|  | 159 | list_move(&pi->list, &s->valid_paths); | 
|  | 160 |  | 
|  | 161 | return 0; | 
|  | 162 | } | 
|  | 163 |  | 
| Josef "Jeff" Sipek | c922d5f | 2006-12-08 02:36:33 -0800 | [diff] [blame] | 164 | static struct dm_path *rr_select_path(struct path_selector *ps, | 
| Kiyoshi Ueda | 02ab823 | 2009-06-22 10:12:27 +0100 | [diff] [blame] | 165 | unsigned *repeat_count, size_t nr_bytes) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { | 
|  | 167 | struct selector *s = (struct selector *) ps->context; | 
|  | 168 | struct path_info *pi = NULL; | 
|  | 169 |  | 
|  | 170 | if (!list_empty(&s->valid_paths)) { | 
|  | 171 | pi = list_entry(s->valid_paths.next, struct path_info, list); | 
|  | 172 | list_move_tail(&pi->list, &s->valid_paths); | 
|  | 173 | *repeat_count = pi->repeat_count; | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | return pi ? pi->path : NULL; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | static struct path_selector_type rr_ps = { | 
|  | 180 | .name = "round-robin", | 
|  | 181 | .module = THIS_MODULE, | 
|  | 182 | .table_args = 1, | 
|  | 183 | .info_args = 0, | 
|  | 184 | .create = rr_create, | 
|  | 185 | .destroy = rr_destroy, | 
|  | 186 | .status = rr_status, | 
|  | 187 | .add_path = rr_add_path, | 
|  | 188 | .fail_path = rr_fail_path, | 
|  | 189 | .reinstate_path = rr_reinstate_path, | 
|  | 190 | .select_path = rr_select_path, | 
|  | 191 | }; | 
|  | 192 |  | 
|  | 193 | static int __init dm_rr_init(void) | 
|  | 194 | { | 
|  | 195 | int r = dm_register_path_selector(&rr_ps); | 
|  | 196 |  | 
|  | 197 | if (r < 0) | 
| Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 198 | DMERR("register failed %d", r); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 |  | 
| Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 200 | DMINFO("version 1.0.0 loaded"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 |  | 
|  | 202 | return r; | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | static void __exit dm_rr_exit(void) | 
|  | 206 | { | 
|  | 207 | int r = dm_unregister_path_selector(&rr_ps); | 
|  | 208 |  | 
|  | 209 | if (r < 0) | 
| Alasdair G Kergon | 0cd3312 | 2007-07-12 17:27:01 +0100 | [diff] [blame] | 210 | DMERR("unregister failed %d", r); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } | 
|  | 212 |  | 
|  | 213 | module_init(dm_rr_init); | 
|  | 214 | module_exit(dm_rr_exit); | 
|  | 215 |  | 
|  | 216 | MODULE_DESCRIPTION(DM_NAME " round-robin multipath path selector"); | 
|  | 217 | MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>"); | 
|  | 218 | MODULE_LICENSE("GPL"); |