Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2003 Sistina Software Limited. |
| 3 | * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. |
| 4 | * |
| 5 | * This file is released under the GPL. |
| 6 | */ |
| 7 | |
| 8 | #include "dm.h" |
| 9 | #include "dm-path-selector.h" |
| 10 | #include "dm-hw-handler.h" |
| 11 | #include "dm-bio-list.h" |
| 12 | #include "dm-bio-record.h" |
| 13 | |
| 14 | #include <linux/ctype.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/mempool.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/pagemap.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/time.h> |
| 21 | #include <linux/workqueue.h> |
| 22 | #include <asm/atomic.h> |
| 23 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 24 | #define DM_MSG_PREFIX "multipath" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #define MESG_STR(x) x, sizeof(x) |
| 26 | |
| 27 | /* Path properties */ |
| 28 | struct pgpath { |
| 29 | struct list_head list; |
| 30 | |
| 31 | struct priority_group *pg; /* Owning PG */ |
| 32 | unsigned fail_count; /* Cumulative failure count */ |
| 33 | |
| 34 | struct path path; |
| 35 | }; |
| 36 | |
| 37 | #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path) |
| 38 | |
| 39 | /* |
| 40 | * Paths are grouped into Priority Groups and numbered from 1 upwards. |
| 41 | * Each has a path selector which controls which path gets used. |
| 42 | */ |
| 43 | struct priority_group { |
| 44 | struct list_head list; |
| 45 | |
| 46 | struct multipath *m; /* Owning multipath instance */ |
| 47 | struct path_selector ps; |
| 48 | |
| 49 | unsigned pg_num; /* Reference number */ |
| 50 | unsigned bypassed; /* Temporarily bypass this PG? */ |
| 51 | |
| 52 | unsigned nr_pgpaths; /* Number of paths in PG */ |
| 53 | struct list_head pgpaths; |
| 54 | }; |
| 55 | |
| 56 | /* Multipath context */ |
| 57 | struct multipath { |
| 58 | struct list_head list; |
| 59 | struct dm_target *ti; |
| 60 | |
| 61 | spinlock_t lock; |
| 62 | |
| 63 | struct hw_handler hw_handler; |
| 64 | unsigned nr_priority_groups; |
| 65 | struct list_head priority_groups; |
| 66 | unsigned pg_init_required; /* pg_init needs calling? */ |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 67 | unsigned pg_init_in_progress; /* Only one pg_init allowed at once */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | unsigned nr_valid_paths; /* Total number of usable paths */ |
| 70 | struct pgpath *current_pgpath; |
| 71 | struct priority_group *current_pg; |
| 72 | struct priority_group *next_pg; /* Switch to this PG if set */ |
| 73 | unsigned repeat_count; /* I/Os left before calling PS again */ |
| 74 | |
| 75 | unsigned queue_io; /* Must we queue all I/O? */ |
| 76 | unsigned queue_if_no_path; /* Queue I/O if last path fails? */ |
Alasdair G Kergon | 436d410 | 2005-07-12 15:53:03 -0700 | [diff] [blame] | 77 | unsigned saved_queue_if_no_path;/* Saved state during suspension */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | struct work_struct process_queued_ios; |
| 80 | struct bio_list queued_ios; |
| 81 | unsigned queue_size; |
| 82 | |
| 83 | struct work_struct trigger_event; |
| 84 | |
| 85 | /* |
| 86 | * We must use a mempool of mpath_io structs so that we |
| 87 | * can resubmit bios on error. |
| 88 | */ |
| 89 | mempool_t *mpio_pool; |
| 90 | }; |
| 91 | |
| 92 | /* |
| 93 | * Context information attached to each bio we process. |
| 94 | */ |
| 95 | struct mpath_io { |
| 96 | struct pgpath *pgpath; |
| 97 | struct dm_bio_details details; |
| 98 | }; |
| 99 | |
| 100 | typedef int (*action_fn) (struct pgpath *pgpath); |
| 101 | |
| 102 | #define MIN_IOS 256 /* Mempool size */ |
| 103 | |
| 104 | static kmem_cache_t *_mpio_cache; |
| 105 | |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 106 | struct workqueue_struct *kmultipathd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | static void process_queued_ios(void *data); |
| 108 | static void trigger_event(void *data); |
| 109 | |
| 110 | |
| 111 | /*----------------------------------------------- |
| 112 | * Allocation routines |
| 113 | *-----------------------------------------------*/ |
| 114 | |
| 115 | static struct pgpath *alloc_pgpath(void) |
| 116 | { |
| 117 | struct pgpath *pgpath = kmalloc(sizeof(*pgpath), GFP_KERNEL); |
| 118 | |
| 119 | if (pgpath) { |
| 120 | memset(pgpath, 0, sizeof(*pgpath)); |
| 121 | pgpath->path.is_active = 1; |
| 122 | } |
| 123 | |
| 124 | return pgpath; |
| 125 | } |
| 126 | |
| 127 | static inline void free_pgpath(struct pgpath *pgpath) |
| 128 | { |
| 129 | kfree(pgpath); |
| 130 | } |
| 131 | |
| 132 | static struct priority_group *alloc_priority_group(void) |
| 133 | { |
| 134 | struct priority_group *pg; |
| 135 | |
| 136 | pg = kmalloc(sizeof(*pg), GFP_KERNEL); |
| 137 | if (!pg) |
| 138 | return NULL; |
| 139 | |
| 140 | memset(pg, 0, sizeof(*pg)); |
| 141 | INIT_LIST_HEAD(&pg->pgpaths); |
| 142 | |
| 143 | return pg; |
| 144 | } |
| 145 | |
| 146 | static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti) |
| 147 | { |
| 148 | struct pgpath *pgpath, *tmp; |
| 149 | |
| 150 | list_for_each_entry_safe(pgpath, tmp, pgpaths, list) { |
| 151 | list_del(&pgpath->list); |
| 152 | dm_put_device(ti, pgpath->path.dev); |
| 153 | free_pgpath(pgpath); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | static void free_priority_group(struct priority_group *pg, |
| 158 | struct dm_target *ti) |
| 159 | { |
| 160 | struct path_selector *ps = &pg->ps; |
| 161 | |
| 162 | if (ps->type) { |
| 163 | ps->type->destroy(ps); |
| 164 | dm_put_path_selector(ps->type); |
| 165 | } |
| 166 | |
| 167 | free_pgpaths(&pg->pgpaths, ti); |
| 168 | kfree(pg); |
| 169 | } |
| 170 | |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame^] | 171 | static struct multipath *alloc_multipath(struct dm_target *ti) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | struct multipath *m; |
| 174 | |
| 175 | m = kmalloc(sizeof(*m), GFP_KERNEL); |
| 176 | if (m) { |
| 177 | memset(m, 0, sizeof(*m)); |
| 178 | INIT_LIST_HEAD(&m->priority_groups); |
| 179 | spin_lock_init(&m->lock); |
| 180 | m->queue_io = 1; |
| 181 | INIT_WORK(&m->process_queued_ios, process_queued_ios, m); |
| 182 | INIT_WORK(&m->trigger_event, trigger_event, m); |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 183 | m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | if (!m->mpio_pool) { |
| 185 | kfree(m); |
| 186 | return NULL; |
| 187 | } |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame^] | 188 | m->ti = ti; |
| 189 | ti->private = m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return m; |
| 193 | } |
| 194 | |
| 195 | static void free_multipath(struct multipath *m) |
| 196 | { |
| 197 | struct priority_group *pg, *tmp; |
| 198 | struct hw_handler *hwh = &m->hw_handler; |
| 199 | |
| 200 | list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) { |
| 201 | list_del(&pg->list); |
| 202 | free_priority_group(pg, m->ti); |
| 203 | } |
| 204 | |
| 205 | if (hwh->type) { |
| 206 | hwh->type->destroy(hwh); |
| 207 | dm_put_hw_handler(hwh->type); |
| 208 | } |
| 209 | |
| 210 | mempool_destroy(m->mpio_pool); |
| 211 | kfree(m); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /*----------------------------------------------- |
| 216 | * Path selection |
| 217 | *-----------------------------------------------*/ |
| 218 | |
| 219 | static void __switch_pg(struct multipath *m, struct pgpath *pgpath) |
| 220 | { |
| 221 | struct hw_handler *hwh = &m->hw_handler; |
| 222 | |
| 223 | m->current_pg = pgpath->pg; |
| 224 | |
| 225 | /* Must we initialise the PG first, and queue I/O till it's ready? */ |
| 226 | if (hwh->type && hwh->type->pg_init) { |
| 227 | m->pg_init_required = 1; |
| 228 | m->queue_io = 1; |
| 229 | } else { |
| 230 | m->pg_init_required = 0; |
| 231 | m->queue_io = 0; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg) |
| 236 | { |
| 237 | struct path *path; |
| 238 | |
| 239 | path = pg->ps.type->select_path(&pg->ps, &m->repeat_count); |
| 240 | if (!path) |
| 241 | return -ENXIO; |
| 242 | |
| 243 | m->current_pgpath = path_to_pgpath(path); |
| 244 | |
| 245 | if (m->current_pg != pg) |
| 246 | __switch_pg(m, m->current_pgpath); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static void __choose_pgpath(struct multipath *m) |
| 252 | { |
| 253 | struct priority_group *pg; |
| 254 | unsigned bypassed = 1; |
| 255 | |
| 256 | if (!m->nr_valid_paths) |
| 257 | goto failed; |
| 258 | |
| 259 | /* Were we instructed to switch PG? */ |
| 260 | if (m->next_pg) { |
| 261 | pg = m->next_pg; |
| 262 | m->next_pg = NULL; |
| 263 | if (!__choose_path_in_pg(m, pg)) |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | /* Don't change PG until it has no remaining paths */ |
| 268 | if (m->current_pg && !__choose_path_in_pg(m, m->current_pg)) |
| 269 | return; |
| 270 | |
| 271 | /* |
| 272 | * Loop through priority groups until we find a valid path. |
| 273 | * First time we skip PGs marked 'bypassed'. |
| 274 | * Second time we only try the ones we skipped. |
| 275 | */ |
| 276 | do { |
| 277 | list_for_each_entry(pg, &m->priority_groups, list) { |
| 278 | if (pg->bypassed == bypassed) |
| 279 | continue; |
| 280 | if (!__choose_path_in_pg(m, pg)) |
| 281 | return; |
| 282 | } |
| 283 | } while (bypassed--); |
| 284 | |
| 285 | failed: |
| 286 | m->current_pgpath = NULL; |
| 287 | m->current_pg = NULL; |
| 288 | } |
| 289 | |
| 290 | static int map_io(struct multipath *m, struct bio *bio, struct mpath_io *mpio, |
| 291 | unsigned was_queued) |
| 292 | { |
| 293 | int r = 1; |
| 294 | unsigned long flags; |
| 295 | struct pgpath *pgpath; |
| 296 | |
| 297 | spin_lock_irqsave(&m->lock, flags); |
| 298 | |
| 299 | /* Do we need to select a new pgpath? */ |
| 300 | if (!m->current_pgpath || |
| 301 | (!m->queue_io && (m->repeat_count && --m->repeat_count == 0))) |
| 302 | __choose_pgpath(m); |
| 303 | |
| 304 | pgpath = m->current_pgpath; |
| 305 | |
| 306 | if (was_queued) |
| 307 | m->queue_size--; |
| 308 | |
| 309 | if ((pgpath && m->queue_io) || |
Alasdair G Kergon | 436d410 | 2005-07-12 15:53:03 -0700 | [diff] [blame] | 310 | (!pgpath && m->queue_if_no_path)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | /* Queue for the daemon to resubmit */ |
| 312 | bio_list_add(&m->queued_ios, bio); |
| 313 | m->queue_size++; |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 314 | if ((m->pg_init_required && !m->pg_init_in_progress) || |
| 315 | !m->queue_io) |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 316 | queue_work(kmultipathd, &m->process_queued_ios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | pgpath = NULL; |
| 318 | r = 0; |
| 319 | } else if (!pgpath) |
| 320 | r = -EIO; /* Failed */ |
| 321 | else |
| 322 | bio->bi_bdev = pgpath->path.dev->bdev; |
| 323 | |
| 324 | mpio->pgpath = pgpath; |
| 325 | |
| 326 | spin_unlock_irqrestore(&m->lock, flags); |
| 327 | |
| 328 | return r; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * If we run out of usable paths, should we queue I/O or error it? |
| 333 | */ |
Alasdair G Kergon | 485ef69 | 2005-09-27 21:45:45 -0700 | [diff] [blame] | 334 | static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path, |
| 335 | unsigned save_old_value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | { |
| 337 | unsigned long flags; |
| 338 | |
| 339 | spin_lock_irqsave(&m->lock, flags); |
| 340 | |
Alasdair G Kergon | 485ef69 | 2005-09-27 21:45:45 -0700 | [diff] [blame] | 341 | if (save_old_value) |
| 342 | m->saved_queue_if_no_path = m->queue_if_no_path; |
| 343 | else |
| 344 | m->saved_queue_if_no_path = queue_if_no_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | m->queue_if_no_path = queue_if_no_path; |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 346 | if (!m->queue_if_no_path && m->queue_size) |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 347 | queue_work(kmultipathd, &m->process_queued_ios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
| 349 | spin_unlock_irqrestore(&m->lock, flags); |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | /*----------------------------------------------------------------- |
| 355 | * The multipath daemon is responsible for resubmitting queued ios. |
| 356 | *---------------------------------------------------------------*/ |
| 357 | |
| 358 | static void dispatch_queued_ios(struct multipath *m) |
| 359 | { |
| 360 | int r; |
| 361 | unsigned long flags; |
| 362 | struct bio *bio = NULL, *next; |
| 363 | struct mpath_io *mpio; |
| 364 | union map_info *info; |
| 365 | |
| 366 | spin_lock_irqsave(&m->lock, flags); |
| 367 | bio = bio_list_get(&m->queued_ios); |
| 368 | spin_unlock_irqrestore(&m->lock, flags); |
| 369 | |
| 370 | while (bio) { |
| 371 | next = bio->bi_next; |
| 372 | bio->bi_next = NULL; |
| 373 | |
| 374 | info = dm_get_mapinfo(bio); |
| 375 | mpio = info->ptr; |
| 376 | |
| 377 | r = map_io(m, bio, mpio, 1); |
| 378 | if (r < 0) |
| 379 | bio_endio(bio, bio->bi_size, r); |
| 380 | else if (r == 1) |
| 381 | generic_make_request(bio); |
| 382 | |
| 383 | bio = next; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | static void process_queued_ios(void *data) |
| 388 | { |
| 389 | struct multipath *m = (struct multipath *) data; |
| 390 | struct hw_handler *hwh = &m->hw_handler; |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 391 | struct pgpath *pgpath = NULL; |
| 392 | unsigned init_required = 0, must_queue = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | unsigned long flags; |
| 394 | |
| 395 | spin_lock_irqsave(&m->lock, flags); |
| 396 | |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 397 | if (!m->queue_size) |
| 398 | goto out; |
| 399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | if (!m->current_pgpath) |
| 401 | __choose_pgpath(m); |
| 402 | |
| 403 | pgpath = m->current_pgpath; |
| 404 | |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 405 | if ((pgpath && !m->queue_io) || |
| 406 | (!pgpath && !m->queue_if_no_path)) |
| 407 | must_queue = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 409 | if (m->pg_init_required && !m->pg_init_in_progress) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | m->pg_init_required = 0; |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 411 | m->pg_init_in_progress = 1; |
| 412 | init_required = 1; |
| 413 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 415 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | spin_unlock_irqrestore(&m->lock, flags); |
| 417 | |
| 418 | if (init_required) |
| 419 | hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path); |
| 420 | |
| 421 | if (!must_queue) |
| 422 | dispatch_queued_ios(m); |
| 423 | } |
| 424 | |
| 425 | /* |
| 426 | * An event is triggered whenever a path is taken out of use. |
| 427 | * Includes path failure and PG bypass. |
| 428 | */ |
| 429 | static void trigger_event(void *data) |
| 430 | { |
| 431 | struct multipath *m = (struct multipath *) data; |
| 432 | |
| 433 | dm_table_event(m->ti->table); |
| 434 | } |
| 435 | |
| 436 | /*----------------------------------------------------------------- |
| 437 | * Constructor/argument parsing: |
| 438 | * <#multipath feature args> [<arg>]* |
| 439 | * <#hw_handler args> [hw_handler [<arg>]*] |
| 440 | * <#priority groups> |
| 441 | * <initial priority group> |
| 442 | * [<selector> <#selector args> [<arg>]* |
| 443 | * <#paths> <#per-path selector args> |
| 444 | * [<path> [<arg>]* ]+ ]+ |
| 445 | *---------------------------------------------------------------*/ |
| 446 | struct param { |
| 447 | unsigned min; |
| 448 | unsigned max; |
| 449 | char *error; |
| 450 | }; |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | static int read_param(struct param *param, char *str, unsigned *v, char **error) |
| 453 | { |
| 454 | if (!str || |
| 455 | (sscanf(str, "%u", v) != 1) || |
| 456 | (*v < param->min) || |
| 457 | (*v > param->max)) { |
| 458 | *error = param->error; |
| 459 | return -EINVAL; |
| 460 | } |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | struct arg_set { |
| 466 | unsigned argc; |
| 467 | char **argv; |
| 468 | }; |
| 469 | |
| 470 | static char *shift(struct arg_set *as) |
| 471 | { |
| 472 | char *r; |
| 473 | |
| 474 | if (as->argc) { |
| 475 | as->argc--; |
| 476 | r = *as->argv; |
| 477 | as->argv++; |
| 478 | return r; |
| 479 | } |
| 480 | |
| 481 | return NULL; |
| 482 | } |
| 483 | |
| 484 | static void consume(struct arg_set *as, unsigned n) |
| 485 | { |
| 486 | BUG_ON (as->argc < n); |
| 487 | as->argc -= n; |
| 488 | as->argv += n; |
| 489 | } |
| 490 | |
| 491 | static int parse_path_selector(struct arg_set *as, struct priority_group *pg, |
| 492 | struct dm_target *ti) |
| 493 | { |
| 494 | int r; |
| 495 | struct path_selector_type *pst; |
| 496 | unsigned ps_argc; |
| 497 | |
| 498 | static struct param _params[] = { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 499 | {0, 1024, "invalid number of path selector args"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | }; |
| 501 | |
| 502 | pst = dm_get_path_selector(shift(as)); |
| 503 | if (!pst) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 504 | ti->error = "unknown path selector type"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | return -EINVAL; |
| 506 | } |
| 507 | |
| 508 | r = read_param(_params, shift(as), &ps_argc, &ti->error); |
| 509 | if (r) |
| 510 | return -EINVAL; |
| 511 | |
| 512 | r = pst->create(&pg->ps, ps_argc, as->argv); |
| 513 | if (r) { |
| 514 | dm_put_path_selector(pst); |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 515 | ti->error = "path selector constructor failed"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | return r; |
| 517 | } |
| 518 | |
| 519 | pg->ps.type = pst; |
| 520 | consume(as, ps_argc); |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps, |
| 526 | struct dm_target *ti) |
| 527 | { |
| 528 | int r; |
| 529 | struct pgpath *p; |
| 530 | |
| 531 | /* we need at least a path arg */ |
| 532 | if (as->argc < 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 533 | ti->error = "no device given"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | p = alloc_pgpath(); |
| 538 | if (!p) |
| 539 | return NULL; |
| 540 | |
| 541 | r = dm_get_device(ti, shift(as), ti->begin, ti->len, |
| 542 | dm_table_get_mode(ti->table), &p->path.dev); |
| 543 | if (r) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 544 | ti->error = "error getting device"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | goto bad; |
| 546 | } |
| 547 | |
| 548 | r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error); |
| 549 | if (r) { |
| 550 | dm_put_device(ti, p->path.dev); |
| 551 | goto bad; |
| 552 | } |
| 553 | |
| 554 | return p; |
| 555 | |
| 556 | bad: |
| 557 | free_pgpath(p); |
| 558 | return NULL; |
| 559 | } |
| 560 | |
| 561 | static struct priority_group *parse_priority_group(struct arg_set *as, |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame^] | 562 | struct multipath *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { |
| 564 | static struct param _params[] = { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 565 | {1, 1024, "invalid number of paths"}, |
| 566 | {0, 1024, "invalid number of selector args"} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | }; |
| 568 | |
| 569 | int r; |
| 570 | unsigned i, nr_selector_args, nr_params; |
| 571 | struct priority_group *pg; |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame^] | 572 | struct dm_target *ti = m->ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
| 574 | if (as->argc < 2) { |
| 575 | as->argc = 0; |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 576 | ti->error = "not enough priority group aruments"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | return NULL; |
| 578 | } |
| 579 | |
| 580 | pg = alloc_priority_group(); |
| 581 | if (!pg) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 582 | ti->error = "couldn't allocate priority group"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | return NULL; |
| 584 | } |
| 585 | pg->m = m; |
| 586 | |
| 587 | r = parse_path_selector(as, pg, ti); |
| 588 | if (r) |
| 589 | goto bad; |
| 590 | |
| 591 | /* |
| 592 | * read the paths |
| 593 | */ |
| 594 | r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error); |
| 595 | if (r) |
| 596 | goto bad; |
| 597 | |
| 598 | r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error); |
| 599 | if (r) |
| 600 | goto bad; |
| 601 | |
| 602 | nr_params = 1 + nr_selector_args; |
| 603 | for (i = 0; i < pg->nr_pgpaths; i++) { |
| 604 | struct pgpath *pgpath; |
| 605 | struct arg_set path_args; |
| 606 | |
| 607 | if (as->argc < nr_params) |
| 608 | goto bad; |
| 609 | |
| 610 | path_args.argc = nr_params; |
| 611 | path_args.argv = as->argv; |
| 612 | |
| 613 | pgpath = parse_path(&path_args, &pg->ps, ti); |
| 614 | if (!pgpath) |
| 615 | goto bad; |
| 616 | |
| 617 | pgpath->pg = pg; |
| 618 | list_add_tail(&pgpath->list, &pg->pgpaths); |
| 619 | consume(as, nr_params); |
| 620 | } |
| 621 | |
| 622 | return pg; |
| 623 | |
| 624 | bad: |
| 625 | free_priority_group(pg, ti); |
| 626 | return NULL; |
| 627 | } |
| 628 | |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame^] | 629 | static int parse_hw_handler(struct arg_set *as, struct multipath *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | { |
| 631 | int r; |
| 632 | struct hw_handler_type *hwht; |
| 633 | unsigned hw_argc; |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame^] | 634 | struct dm_target *ti = m->ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
| 636 | static struct param _params[] = { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 637 | {0, 1024, "invalid number of hardware handler args"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | }; |
| 639 | |
| 640 | r = read_param(_params, shift(as), &hw_argc, &ti->error); |
| 641 | if (r) |
| 642 | return -EINVAL; |
| 643 | |
| 644 | if (!hw_argc) |
| 645 | return 0; |
| 646 | |
| 647 | hwht = dm_get_hw_handler(shift(as)); |
| 648 | if (!hwht) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 649 | ti->error = "unknown hardware handler type"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | return -EINVAL; |
| 651 | } |
| 652 | |
| 653 | r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv); |
| 654 | if (r) { |
| 655 | dm_put_hw_handler(hwht); |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 656 | ti->error = "hardware handler constructor failed"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | return r; |
| 658 | } |
| 659 | |
| 660 | m->hw_handler.type = hwht; |
| 661 | consume(as, hw_argc - 1); |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame^] | 666 | static int parse_features(struct arg_set *as, struct multipath *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
| 668 | int r; |
| 669 | unsigned argc; |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame^] | 670 | struct dm_target *ti = m->ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
| 672 | static struct param _params[] = { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 673 | {0, 1, "invalid number of feature args"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | }; |
| 675 | |
| 676 | r = read_param(_params, shift(as), &argc, &ti->error); |
| 677 | if (r) |
| 678 | return -EINVAL; |
| 679 | |
| 680 | if (!argc) |
| 681 | return 0; |
| 682 | |
| 683 | if (!strnicmp(shift(as), MESG_STR("queue_if_no_path"))) |
Alasdair G Kergon | 485ef69 | 2005-09-27 21:45:45 -0700 | [diff] [blame] | 684 | return queue_if_no_path(m, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | else { |
| 686 | ti->error = "Unrecognised multipath feature request"; |
| 687 | return -EINVAL; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | static int multipath_ctr(struct dm_target *ti, unsigned int argc, |
| 692 | char **argv) |
| 693 | { |
| 694 | /* target parameters */ |
| 695 | static struct param _params[] = { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 696 | {1, 1024, "invalid number of priority groups"}, |
| 697 | {1, 1024, "invalid initial priority group number"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | }; |
| 699 | |
| 700 | int r; |
| 701 | struct multipath *m; |
| 702 | struct arg_set as; |
| 703 | unsigned pg_count = 0; |
| 704 | unsigned next_pg_num; |
| 705 | |
| 706 | as.argc = argc; |
| 707 | as.argv = argv; |
| 708 | |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame^] | 709 | m = alloc_multipath(ti); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | if (!m) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 711 | ti->error = "can't allocate multipath"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | return -EINVAL; |
| 713 | } |
| 714 | |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame^] | 715 | r = parse_features(&as, m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | if (r) |
| 717 | goto bad; |
| 718 | |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame^] | 719 | r = parse_hw_handler(&as, m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | if (r) |
| 721 | goto bad; |
| 722 | |
| 723 | r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error); |
| 724 | if (r) |
| 725 | goto bad; |
| 726 | |
| 727 | r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error); |
| 728 | if (r) |
| 729 | goto bad; |
| 730 | |
| 731 | /* parse the priority groups */ |
| 732 | while (as.argc) { |
| 733 | struct priority_group *pg; |
| 734 | |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame^] | 735 | pg = parse_priority_group(&as, m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | if (!pg) { |
| 737 | r = -EINVAL; |
| 738 | goto bad; |
| 739 | } |
| 740 | |
| 741 | m->nr_valid_paths += pg->nr_pgpaths; |
| 742 | list_add_tail(&pg->list, &m->priority_groups); |
| 743 | pg_count++; |
| 744 | pg->pg_num = pg_count; |
| 745 | if (!--next_pg_num) |
| 746 | m->next_pg = pg; |
| 747 | } |
| 748 | |
| 749 | if (pg_count != m->nr_priority_groups) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 750 | ti->error = "priority group count mismatch"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | r = -EINVAL; |
| 752 | goto bad; |
| 753 | } |
| 754 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | return 0; |
| 756 | |
| 757 | bad: |
| 758 | free_multipath(m); |
| 759 | return r; |
| 760 | } |
| 761 | |
| 762 | static void multipath_dtr(struct dm_target *ti) |
| 763 | { |
| 764 | struct multipath *m = (struct multipath *) ti->private; |
Alasdair G Kergon | a044d01 | 2005-07-12 15:53:02 -0700 | [diff] [blame] | 765 | |
| 766 | flush_workqueue(kmultipathd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | free_multipath(m); |
| 768 | } |
| 769 | |
| 770 | /* |
| 771 | * Map bios, recording original fields for later in case we have to resubmit |
| 772 | */ |
| 773 | static int multipath_map(struct dm_target *ti, struct bio *bio, |
| 774 | union map_info *map_context) |
| 775 | { |
| 776 | int r; |
| 777 | struct mpath_io *mpio; |
| 778 | struct multipath *m = (struct multipath *) ti->private; |
| 779 | |
Alasdair G Kergon | f6a80ea | 2005-07-12 15:53:01 -0700 | [diff] [blame] | 780 | if (bio_barrier(bio)) |
| 781 | return -EOPNOTSUPP; |
| 782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | mpio = mempool_alloc(m->mpio_pool, GFP_NOIO); |
| 784 | dm_bio_record(&mpio->details, bio); |
| 785 | |
| 786 | map_context->ptr = mpio; |
| 787 | bio->bi_rw |= (1 << BIO_RW_FAILFAST); |
| 788 | r = map_io(m, bio, mpio, 0); |
| 789 | if (r < 0) |
| 790 | mempool_free(mpio, m->mpio_pool); |
| 791 | |
| 792 | return r; |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | * Take a path out of use. |
| 797 | */ |
| 798 | static int fail_path(struct pgpath *pgpath) |
| 799 | { |
| 800 | unsigned long flags; |
| 801 | struct multipath *m = pgpath->pg->m; |
| 802 | |
| 803 | spin_lock_irqsave(&m->lock, flags); |
| 804 | |
| 805 | if (!pgpath->path.is_active) |
| 806 | goto out; |
| 807 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 808 | DMWARN("Failing path %s.", pgpath->path.dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
| 810 | pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path); |
| 811 | pgpath->path.is_active = 0; |
| 812 | pgpath->fail_count++; |
| 813 | |
| 814 | m->nr_valid_paths--; |
| 815 | |
| 816 | if (pgpath == m->current_pgpath) |
| 817 | m->current_pgpath = NULL; |
| 818 | |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 819 | queue_work(kmultipathd, &m->trigger_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
| 821 | out: |
| 822 | spin_unlock_irqrestore(&m->lock, flags); |
| 823 | |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * Reinstate a previously-failed path |
| 829 | */ |
| 830 | static int reinstate_path(struct pgpath *pgpath) |
| 831 | { |
| 832 | int r = 0; |
| 833 | unsigned long flags; |
| 834 | struct multipath *m = pgpath->pg->m; |
| 835 | |
| 836 | spin_lock_irqsave(&m->lock, flags); |
| 837 | |
| 838 | if (pgpath->path.is_active) |
| 839 | goto out; |
| 840 | |
| 841 | if (!pgpath->pg->ps.type) { |
| 842 | DMWARN("Reinstate path not supported by path selector %s", |
| 843 | pgpath->pg->ps.type->name); |
| 844 | r = -EINVAL; |
| 845 | goto out; |
| 846 | } |
| 847 | |
| 848 | r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path); |
| 849 | if (r) |
| 850 | goto out; |
| 851 | |
| 852 | pgpath->path.is_active = 1; |
| 853 | |
| 854 | m->current_pgpath = NULL; |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 855 | if (!m->nr_valid_paths++ && m->queue_size) |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 856 | queue_work(kmultipathd, &m->process_queued_ios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 858 | queue_work(kmultipathd, &m->trigger_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | |
| 860 | out: |
| 861 | spin_unlock_irqrestore(&m->lock, flags); |
| 862 | |
| 863 | return r; |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | * Fail or reinstate all paths that match the provided struct dm_dev. |
| 868 | */ |
| 869 | static int action_dev(struct multipath *m, struct dm_dev *dev, |
| 870 | action_fn action) |
| 871 | { |
| 872 | int r = 0; |
| 873 | struct pgpath *pgpath; |
| 874 | struct priority_group *pg; |
| 875 | |
| 876 | list_for_each_entry(pg, &m->priority_groups, list) { |
| 877 | list_for_each_entry(pgpath, &pg->pgpaths, list) { |
| 878 | if (pgpath->path.dev == dev) |
| 879 | r = action(pgpath); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | return r; |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * Temporarily try to avoid having to use the specified PG |
| 888 | */ |
| 889 | static void bypass_pg(struct multipath *m, struct priority_group *pg, |
| 890 | int bypassed) |
| 891 | { |
| 892 | unsigned long flags; |
| 893 | |
| 894 | spin_lock_irqsave(&m->lock, flags); |
| 895 | |
| 896 | pg->bypassed = bypassed; |
| 897 | m->current_pgpath = NULL; |
| 898 | m->current_pg = NULL; |
| 899 | |
| 900 | spin_unlock_irqrestore(&m->lock, flags); |
| 901 | |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 902 | queue_work(kmultipathd, &m->trigger_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | /* |
| 906 | * Switch to using the specified PG from the next I/O that gets mapped |
| 907 | */ |
| 908 | static int switch_pg_num(struct multipath *m, const char *pgstr) |
| 909 | { |
| 910 | struct priority_group *pg; |
| 911 | unsigned pgnum; |
| 912 | unsigned long flags; |
| 913 | |
| 914 | if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum || |
| 915 | (pgnum > m->nr_priority_groups)) { |
| 916 | DMWARN("invalid PG number supplied to switch_pg_num"); |
| 917 | return -EINVAL; |
| 918 | } |
| 919 | |
| 920 | spin_lock_irqsave(&m->lock, flags); |
| 921 | list_for_each_entry(pg, &m->priority_groups, list) { |
| 922 | pg->bypassed = 0; |
| 923 | if (--pgnum) |
| 924 | continue; |
| 925 | |
| 926 | m->current_pgpath = NULL; |
| 927 | m->current_pg = NULL; |
| 928 | m->next_pg = pg; |
| 929 | } |
| 930 | spin_unlock_irqrestore(&m->lock, flags); |
| 931 | |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 932 | queue_work(kmultipathd, &m->trigger_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | * Set/clear bypassed status of a PG. |
| 938 | * PGs are numbered upwards from 1 in the order they were declared. |
| 939 | */ |
| 940 | static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed) |
| 941 | { |
| 942 | struct priority_group *pg; |
| 943 | unsigned pgnum; |
| 944 | |
| 945 | if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum || |
| 946 | (pgnum > m->nr_priority_groups)) { |
| 947 | DMWARN("invalid PG number supplied to bypass_pg"); |
| 948 | return -EINVAL; |
| 949 | } |
| 950 | |
| 951 | list_for_each_entry(pg, &m->priority_groups, list) { |
| 952 | if (!--pgnum) |
| 953 | break; |
| 954 | } |
| 955 | |
| 956 | bypass_pg(m, pg, bypassed); |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * pg_init must call this when it has completed its initialisation |
| 962 | */ |
| 963 | void dm_pg_init_complete(struct path *path, unsigned err_flags) |
| 964 | { |
| 965 | struct pgpath *pgpath = path_to_pgpath(path); |
| 966 | struct priority_group *pg = pgpath->pg; |
| 967 | struct multipath *m = pg->m; |
| 968 | unsigned long flags; |
| 969 | |
| 970 | /* We insist on failing the path if the PG is already bypassed. */ |
| 971 | if (err_flags && pg->bypassed) |
| 972 | err_flags |= MP_FAIL_PATH; |
| 973 | |
| 974 | if (err_flags & MP_FAIL_PATH) |
| 975 | fail_path(pgpath); |
| 976 | |
| 977 | if (err_flags & MP_BYPASS_PG) |
| 978 | bypass_pg(m, pg, 1); |
| 979 | |
| 980 | spin_lock_irqsave(&m->lock, flags); |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 981 | if (err_flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | m->current_pgpath = NULL; |
| 983 | m->current_pg = NULL; |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 984 | } else if (!m->pg_init_required) |
| 985 | m->queue_io = 0; |
| 986 | |
| 987 | m->pg_init_in_progress = 0; |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 988 | queue_work(kmultipathd, &m->process_queued_ios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | spin_unlock_irqrestore(&m->lock, flags); |
| 990 | } |
| 991 | |
| 992 | /* |
| 993 | * end_io handling |
| 994 | */ |
| 995 | static int do_end_io(struct multipath *m, struct bio *bio, |
| 996 | int error, struct mpath_io *mpio) |
| 997 | { |
| 998 | struct hw_handler *hwh = &m->hw_handler; |
| 999 | unsigned err_flags = MP_FAIL_PATH; /* Default behavior */ |
Stefan Bader | 640eb3b | 2005-11-21 21:32:35 -0800 | [diff] [blame] | 1000 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
| 1002 | if (!error) |
| 1003 | return 0; /* I/O complete */ |
| 1004 | |
Lars Marowsky-Bree | 4f58802 | 2005-06-08 15:50:31 -0700 | [diff] [blame] | 1005 | if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio)) |
| 1006 | return error; |
| 1007 | |
Alasdair G Kergon | f6a80ea | 2005-07-12 15:53:01 -0700 | [diff] [blame] | 1008 | if (error == -EOPNOTSUPP) |
| 1009 | return error; |
| 1010 | |
Stefan Bader | 640eb3b | 2005-11-21 21:32:35 -0800 | [diff] [blame] | 1011 | spin_lock_irqsave(&m->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | if (!m->nr_valid_paths) { |
Alasdair G Kergon | 436d410 | 2005-07-12 15:53:03 -0700 | [diff] [blame] | 1013 | if (!m->queue_if_no_path) { |
Stefan Bader | 640eb3b | 2005-11-21 21:32:35 -0800 | [diff] [blame] | 1014 | spin_unlock_irqrestore(&m->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | return -EIO; |
| 1016 | } else { |
Stefan Bader | 640eb3b | 2005-11-21 21:32:35 -0800 | [diff] [blame] | 1017 | spin_unlock_irqrestore(&m->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | goto requeue; |
| 1019 | } |
| 1020 | } |
Stefan Bader | 640eb3b | 2005-11-21 21:32:35 -0800 | [diff] [blame] | 1021 | spin_unlock_irqrestore(&m->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | |
| 1023 | if (hwh->type && hwh->type->error) |
| 1024 | err_flags = hwh->type->error(hwh, bio); |
| 1025 | |
| 1026 | if (mpio->pgpath) { |
| 1027 | if (err_flags & MP_FAIL_PATH) |
| 1028 | fail_path(mpio->pgpath); |
| 1029 | |
| 1030 | if (err_flags & MP_BYPASS_PG) |
| 1031 | bypass_pg(m, mpio->pgpath->pg, 1); |
| 1032 | } |
| 1033 | |
| 1034 | if (err_flags & MP_ERROR_IO) |
| 1035 | return -EIO; |
| 1036 | |
| 1037 | requeue: |
| 1038 | dm_bio_restore(&mpio->details, bio); |
| 1039 | |
| 1040 | /* queue for the daemon to resubmit or fail */ |
Stefan Bader | 640eb3b | 2005-11-21 21:32:35 -0800 | [diff] [blame] | 1041 | spin_lock_irqsave(&m->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | bio_list_add(&m->queued_ios, bio); |
| 1043 | m->queue_size++; |
| 1044 | if (!m->queue_io) |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 1045 | queue_work(kmultipathd, &m->process_queued_ios); |
Stefan Bader | 640eb3b | 2005-11-21 21:32:35 -0800 | [diff] [blame] | 1046 | spin_unlock_irqrestore(&m->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | |
| 1048 | return 1; /* io not complete */ |
| 1049 | } |
| 1050 | |
| 1051 | static int multipath_end_io(struct dm_target *ti, struct bio *bio, |
| 1052 | int error, union map_info *map_context) |
| 1053 | { |
| 1054 | struct multipath *m = (struct multipath *) ti->private; |
| 1055 | struct mpath_io *mpio = (struct mpath_io *) map_context->ptr; |
| 1056 | struct pgpath *pgpath = mpio->pgpath; |
| 1057 | struct path_selector *ps; |
| 1058 | int r; |
| 1059 | |
| 1060 | r = do_end_io(m, bio, error, mpio); |
| 1061 | if (pgpath) { |
| 1062 | ps = &pgpath->pg->ps; |
| 1063 | if (ps->type->end_io) |
| 1064 | ps->type->end_io(ps, &pgpath->path); |
| 1065 | } |
| 1066 | if (r <= 0) |
| 1067 | mempool_free(mpio, m->mpio_pool); |
| 1068 | |
| 1069 | return r; |
| 1070 | } |
| 1071 | |
| 1072 | /* |
| 1073 | * Suspend can't complete until all the I/O is processed so if |
Alasdair G Kergon | 436d410 | 2005-07-12 15:53:03 -0700 | [diff] [blame] | 1074 | * the last path fails we must error any remaining I/O. |
| 1075 | * Note that if the freeze_bdev fails while suspending, the |
| 1076 | * queue_if_no_path state is lost - userspace should reset it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | */ |
| 1078 | static void multipath_presuspend(struct dm_target *ti) |
| 1079 | { |
| 1080 | struct multipath *m = (struct multipath *) ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
Alasdair G Kergon | 485ef69 | 2005-09-27 21:45:45 -0700 | [diff] [blame] | 1082 | queue_if_no_path(m, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Alasdair G Kergon | 436d410 | 2005-07-12 15:53:03 -0700 | [diff] [blame] | 1085 | /* |
| 1086 | * Restore the queue_if_no_path setting. |
| 1087 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | static void multipath_resume(struct dm_target *ti) |
| 1089 | { |
| 1090 | struct multipath *m = (struct multipath *) ti->private; |
| 1091 | unsigned long flags; |
| 1092 | |
| 1093 | spin_lock_irqsave(&m->lock, flags); |
Alasdair G Kergon | 436d410 | 2005-07-12 15:53:03 -0700 | [diff] [blame] | 1094 | m->queue_if_no_path = m->saved_queue_if_no_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | spin_unlock_irqrestore(&m->lock, flags); |
| 1096 | } |
| 1097 | |
| 1098 | /* |
| 1099 | * Info output has the following format: |
| 1100 | * num_multipath_feature_args [multipath_feature_args]* |
| 1101 | * num_handler_status_args [handler_status_args]* |
| 1102 | * num_groups init_group_number |
| 1103 | * [A|D|E num_ps_status_args [ps_status_args]* |
| 1104 | * num_paths num_selector_args |
| 1105 | * [path_dev A|F fail_count [selector_args]* ]+ ]+ |
| 1106 | * |
| 1107 | * Table output has the following format (identical to the constructor string): |
| 1108 | * num_feature_args [features_args]* |
| 1109 | * num_handler_args hw_handler [hw_handler_args]* |
| 1110 | * num_groups init_group_number |
| 1111 | * [priority selector-name num_ps_args [ps_args]* |
| 1112 | * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+ |
| 1113 | */ |
| 1114 | static int multipath_status(struct dm_target *ti, status_type_t type, |
| 1115 | char *result, unsigned int maxlen) |
| 1116 | { |
| 1117 | int sz = 0; |
| 1118 | unsigned long flags; |
| 1119 | struct multipath *m = (struct multipath *) ti->private; |
| 1120 | struct hw_handler *hwh = &m->hw_handler; |
| 1121 | struct priority_group *pg; |
| 1122 | struct pgpath *p; |
| 1123 | unsigned pg_num; |
| 1124 | char state; |
| 1125 | |
| 1126 | spin_lock_irqsave(&m->lock, flags); |
| 1127 | |
| 1128 | /* Features */ |
| 1129 | if (type == STATUSTYPE_INFO) |
| 1130 | DMEMIT("1 %u ", m->queue_size); |
| 1131 | else if (m->queue_if_no_path) |
| 1132 | DMEMIT("1 queue_if_no_path "); |
| 1133 | else |
| 1134 | DMEMIT("0 "); |
| 1135 | |
| 1136 | if (hwh->type && hwh->type->status) |
| 1137 | sz += hwh->type->status(hwh, type, result + sz, maxlen - sz); |
| 1138 | else if (!hwh->type || type == STATUSTYPE_INFO) |
| 1139 | DMEMIT("0 "); |
| 1140 | else |
| 1141 | DMEMIT("1 %s ", hwh->type->name); |
| 1142 | |
| 1143 | DMEMIT("%u ", m->nr_priority_groups); |
| 1144 | |
| 1145 | if (m->next_pg) |
| 1146 | pg_num = m->next_pg->pg_num; |
| 1147 | else if (m->current_pg) |
| 1148 | pg_num = m->current_pg->pg_num; |
| 1149 | else |
| 1150 | pg_num = 1; |
| 1151 | |
| 1152 | DMEMIT("%u ", pg_num); |
| 1153 | |
| 1154 | switch (type) { |
| 1155 | case STATUSTYPE_INFO: |
| 1156 | list_for_each_entry(pg, &m->priority_groups, list) { |
| 1157 | if (pg->bypassed) |
| 1158 | state = 'D'; /* Disabled */ |
| 1159 | else if (pg == m->current_pg) |
| 1160 | state = 'A'; /* Currently Active */ |
| 1161 | else |
| 1162 | state = 'E'; /* Enabled */ |
| 1163 | |
| 1164 | DMEMIT("%c ", state); |
| 1165 | |
| 1166 | if (pg->ps.type->status) |
| 1167 | sz += pg->ps.type->status(&pg->ps, NULL, type, |
| 1168 | result + sz, |
| 1169 | maxlen - sz); |
| 1170 | else |
| 1171 | DMEMIT("0 "); |
| 1172 | |
| 1173 | DMEMIT("%u %u ", pg->nr_pgpaths, |
| 1174 | pg->ps.type->info_args); |
| 1175 | |
| 1176 | list_for_each_entry(p, &pg->pgpaths, list) { |
| 1177 | DMEMIT("%s %s %u ", p->path.dev->name, |
| 1178 | p->path.is_active ? "A" : "F", |
| 1179 | p->fail_count); |
| 1180 | if (pg->ps.type->status) |
| 1181 | sz += pg->ps.type->status(&pg->ps, |
| 1182 | &p->path, type, result + sz, |
| 1183 | maxlen - sz); |
| 1184 | } |
| 1185 | } |
| 1186 | break; |
| 1187 | |
| 1188 | case STATUSTYPE_TABLE: |
| 1189 | list_for_each_entry(pg, &m->priority_groups, list) { |
| 1190 | DMEMIT("%s ", pg->ps.type->name); |
| 1191 | |
| 1192 | if (pg->ps.type->status) |
| 1193 | sz += pg->ps.type->status(&pg->ps, NULL, type, |
| 1194 | result + sz, |
| 1195 | maxlen - sz); |
| 1196 | else |
| 1197 | DMEMIT("0 "); |
| 1198 | |
| 1199 | DMEMIT("%u %u ", pg->nr_pgpaths, |
| 1200 | pg->ps.type->table_args); |
| 1201 | |
| 1202 | list_for_each_entry(p, &pg->pgpaths, list) { |
| 1203 | DMEMIT("%s ", p->path.dev->name); |
| 1204 | if (pg->ps.type->status) |
| 1205 | sz += pg->ps.type->status(&pg->ps, |
| 1206 | &p->path, type, result + sz, |
| 1207 | maxlen - sz); |
| 1208 | } |
| 1209 | } |
| 1210 | break; |
| 1211 | } |
| 1212 | |
| 1213 | spin_unlock_irqrestore(&m->lock, flags); |
| 1214 | |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
| 1218 | static int multipath_message(struct dm_target *ti, unsigned argc, char **argv) |
| 1219 | { |
| 1220 | int r; |
| 1221 | struct dm_dev *dev; |
| 1222 | struct multipath *m = (struct multipath *) ti->private; |
| 1223 | action_fn action; |
| 1224 | |
| 1225 | if (argc == 1) { |
| 1226 | if (!strnicmp(argv[0], MESG_STR("queue_if_no_path"))) |
Alasdair G Kergon | 485ef69 | 2005-09-27 21:45:45 -0700 | [diff] [blame] | 1227 | return queue_if_no_path(m, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path"))) |
Alasdair G Kergon | 485ef69 | 2005-09-27 21:45:45 -0700 | [diff] [blame] | 1229 | return queue_if_no_path(m, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | if (argc != 2) |
| 1233 | goto error; |
| 1234 | |
| 1235 | if (!strnicmp(argv[0], MESG_STR("disable_group"))) |
| 1236 | return bypass_pg_num(m, argv[1], 1); |
| 1237 | else if (!strnicmp(argv[0], MESG_STR("enable_group"))) |
| 1238 | return bypass_pg_num(m, argv[1], 0); |
| 1239 | else if (!strnicmp(argv[0], MESG_STR("switch_group"))) |
| 1240 | return switch_pg_num(m, argv[1]); |
| 1241 | else if (!strnicmp(argv[0], MESG_STR("reinstate_path"))) |
| 1242 | action = reinstate_path; |
| 1243 | else if (!strnicmp(argv[0], MESG_STR("fail_path"))) |
| 1244 | action = fail_path; |
| 1245 | else |
| 1246 | goto error; |
| 1247 | |
| 1248 | r = dm_get_device(ti, argv[1], ti->begin, ti->len, |
| 1249 | dm_table_get_mode(ti->table), &dev); |
| 1250 | if (r) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1251 | DMWARN("message: error getting device %s", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | argv[1]); |
| 1253 | return -EINVAL; |
| 1254 | } |
| 1255 | |
| 1256 | r = action_dev(m, dev, action); |
| 1257 | |
| 1258 | dm_put_device(ti, dev); |
| 1259 | |
| 1260 | return r; |
| 1261 | |
| 1262 | error: |
| 1263 | DMWARN("Unrecognised multipath message received."); |
| 1264 | return -EINVAL; |
| 1265 | } |
| 1266 | |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1267 | static int multipath_ioctl(struct dm_target *ti, struct inode *inode, |
| 1268 | struct file *filp, unsigned int cmd, |
| 1269 | unsigned long arg) |
| 1270 | { |
| 1271 | struct multipath *m = (struct multipath *) ti->private; |
| 1272 | struct block_device *bdev = NULL; |
| 1273 | unsigned long flags; |
Milan Broz | e90dae1 | 2006-10-03 01:15:22 -0700 | [diff] [blame] | 1274 | struct file fake_file = {}; |
| 1275 | struct dentry fake_dentry = {}; |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1276 | int r = 0; |
| 1277 | |
Milan Broz | e90dae1 | 2006-10-03 01:15:22 -0700 | [diff] [blame] | 1278 | fake_file.f_dentry = &fake_dentry; |
| 1279 | |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1280 | spin_lock_irqsave(&m->lock, flags); |
| 1281 | |
| 1282 | if (!m->current_pgpath) |
| 1283 | __choose_pgpath(m); |
| 1284 | |
Milan Broz | e90dae1 | 2006-10-03 01:15:22 -0700 | [diff] [blame] | 1285 | if (m->current_pgpath) { |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1286 | bdev = m->current_pgpath->path.dev->bdev; |
Milan Broz | e90dae1 | 2006-10-03 01:15:22 -0700 | [diff] [blame] | 1287 | fake_dentry.d_inode = bdev->bd_inode; |
| 1288 | fake_file.f_mode = m->current_pgpath->path.dev->mode; |
| 1289 | } |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1290 | |
| 1291 | if (m->queue_io) |
| 1292 | r = -EAGAIN; |
| 1293 | else if (!bdev) |
| 1294 | r = -EIO; |
| 1295 | |
| 1296 | spin_unlock_irqrestore(&m->lock, flags); |
| 1297 | |
Milan Broz | e90dae1 | 2006-10-03 01:15:22 -0700 | [diff] [blame] | 1298 | return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file, |
| 1299 | bdev->bd_disk, cmd, arg); |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | /*----------------------------------------------------------------- |
| 1303 | * Module setup |
| 1304 | *---------------------------------------------------------------*/ |
| 1305 | static struct target_type multipath_target = { |
| 1306 | .name = "multipath", |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1307 | .version = {1, 0, 5}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | .module = THIS_MODULE, |
| 1309 | .ctr = multipath_ctr, |
| 1310 | .dtr = multipath_dtr, |
| 1311 | .map = multipath_map, |
| 1312 | .end_io = multipath_end_io, |
| 1313 | .presuspend = multipath_presuspend, |
| 1314 | .resume = multipath_resume, |
| 1315 | .status = multipath_status, |
| 1316 | .message = multipath_message, |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1317 | .ioctl = multipath_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | }; |
| 1319 | |
| 1320 | static int __init dm_multipath_init(void) |
| 1321 | { |
| 1322 | int r; |
| 1323 | |
| 1324 | /* allocate a slab for the dm_ios */ |
| 1325 | _mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io), |
| 1326 | 0, 0, NULL, NULL); |
| 1327 | if (!_mpio_cache) |
| 1328 | return -ENOMEM; |
| 1329 | |
| 1330 | r = dm_register_target(&multipath_target); |
| 1331 | if (r < 0) { |
| 1332 | DMERR("%s: register failed %d", multipath_target.name, r); |
| 1333 | kmem_cache_destroy(_mpio_cache); |
| 1334 | return -EINVAL; |
| 1335 | } |
| 1336 | |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 1337 | kmultipathd = create_workqueue("kmpathd"); |
| 1338 | if (!kmultipathd) { |
| 1339 | DMERR("%s: failed to create workqueue kmpathd", |
| 1340 | multipath_target.name); |
| 1341 | dm_unregister_target(&multipath_target); |
| 1342 | kmem_cache_destroy(_mpio_cache); |
| 1343 | return -ENOMEM; |
| 1344 | } |
| 1345 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1346 | DMINFO("version %u.%u.%u loaded", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | multipath_target.version[0], multipath_target.version[1], |
| 1348 | multipath_target.version[2]); |
| 1349 | |
| 1350 | return r; |
| 1351 | } |
| 1352 | |
| 1353 | static void __exit dm_multipath_exit(void) |
| 1354 | { |
| 1355 | int r; |
| 1356 | |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 1357 | destroy_workqueue(kmultipathd); |
| 1358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | r = dm_unregister_target(&multipath_target); |
| 1360 | if (r < 0) |
| 1361 | DMERR("%s: target unregister failed %d", |
| 1362 | multipath_target.name, r); |
| 1363 | kmem_cache_destroy(_mpio_cache); |
| 1364 | } |
| 1365 | |
| 1366 | EXPORT_SYMBOL_GPL(dm_pg_init_complete); |
| 1367 | |
| 1368 | module_init(dm_multipath_init); |
| 1369 | module_exit(dm_multipath_exit); |
| 1370 | |
| 1371 | MODULE_DESCRIPTION(DM_NAME " multipath target"); |
| 1372 | MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>"); |
| 1373 | MODULE_LICENSE("GPL"); |