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 | |
Mikulas Patocka | 586e80e | 2008-10-21 17:44:59 +0100 | [diff] [blame] | 8 | #include <linux/device-mapper.h> |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include "dm-path-selector.h" |
Mike Anderson | b15546f | 2007-10-19 22:48:02 +0100 | [diff] [blame] | 11 | #include "dm-uevent.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 13 | #include <linux/ctype.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/mempool.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/pagemap.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/time.h> |
| 20 | #include <linux/workqueue.h> |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 21 | #include <scsi/scsi_dh.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Kiyoshi Ueda | 6680073 | 2008-10-10 13:36:58 +0100 | [diff] [blame] | 32 | unsigned is_active; /* Path status */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | unsigned fail_count; /* Cumulative failure count */ |
| 34 | |
Josef "Jeff" Sipek | c922d5f | 2006-12-08 02:36:33 -0800 | [diff] [blame] | 35 | struct dm_path path; |
Mike Anderson | 224cb3e | 2008-08-29 09:36:09 +0200 | [diff] [blame] | 36 | struct work_struct deactivate_path; |
Chandra Seetharaman | e54f77d | 2009-06-22 10:12:12 +0100 | [diff] [blame] | 37 | struct work_struct activate_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path) |
| 41 | |
| 42 | /* |
| 43 | * Paths are grouped into Priority Groups and numbered from 1 upwards. |
| 44 | * Each has a path selector which controls which path gets used. |
| 45 | */ |
| 46 | struct priority_group { |
| 47 | struct list_head list; |
| 48 | |
| 49 | struct multipath *m; /* Owning multipath instance */ |
| 50 | struct path_selector ps; |
| 51 | |
| 52 | unsigned pg_num; /* Reference number */ |
| 53 | unsigned bypassed; /* Temporarily bypass this PG? */ |
| 54 | |
| 55 | unsigned nr_pgpaths; /* Number of paths in PG */ |
| 56 | struct list_head pgpaths; |
| 57 | }; |
| 58 | |
| 59 | /* Multipath context */ |
| 60 | struct multipath { |
| 61 | struct list_head list; |
| 62 | struct dm_target *ti; |
| 63 | |
| 64 | spinlock_t lock; |
| 65 | |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 66 | const char *hw_handler_name; |
Chandra Seetharaman | 2bfd2e1 | 2009-08-03 12:42:45 -0700 | [diff] [blame] | 67 | char *hw_handler_params; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | unsigned nr_priority_groups; |
| 69 | struct list_head priority_groups; |
| 70 | unsigned pg_init_required; /* pg_init needs calling? */ |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 71 | unsigned pg_init_in_progress; /* Only one pg_init allowed at once */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | unsigned nr_valid_paths; /* Total number of usable paths */ |
| 74 | struct pgpath *current_pgpath; |
| 75 | struct priority_group *current_pg; |
| 76 | struct priority_group *next_pg; /* Switch to this PG if set */ |
| 77 | unsigned repeat_count; /* I/Os left before calling PS again */ |
| 78 | |
| 79 | unsigned queue_io; /* Must we queue all I/O? */ |
| 80 | 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] | 81 | unsigned saved_queue_if_no_path;/* Saved state during suspension */ |
Dave Wysochanski | c9e4558 | 2007-10-19 22:47:53 +0100 | [diff] [blame] | 82 | unsigned pg_init_retries; /* Number of times to retry pg_init */ |
| 83 | unsigned pg_init_count; /* Number of times pg_init called */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | struct work_struct process_queued_ios; |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 86 | struct list_head queued_ios; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | unsigned queue_size; |
| 88 | |
| 89 | struct work_struct trigger_event; |
| 90 | |
| 91 | /* |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 92 | * We must use a mempool of dm_mpath_io structs so that we |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * can resubmit bios on error. |
| 94 | */ |
| 95 | mempool_t *mpio_pool; |
Mike Anderson | 6380f26 | 2009-12-10 23:52:21 +0000 | [diff] [blame] | 96 | |
| 97 | struct mutex work_mutex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | /* |
| 101 | * Context information attached to each bio we process. |
| 102 | */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 103 | struct dm_mpath_io { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | struct pgpath *pgpath; |
Kiyoshi Ueda | 02ab823 | 2009-06-22 10:12:27 +0100 | [diff] [blame] | 105 | size_t nr_bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | typedef int (*action_fn) (struct pgpath *pgpath); |
| 109 | |
| 110 | #define MIN_IOS 256 /* Mempool size */ |
| 111 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 112 | static struct kmem_cache *_mpio_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Chandra Seetharaman | bab7cfc | 2008-05-01 14:50:22 -0700 | [diff] [blame] | 114 | static struct workqueue_struct *kmultipathd, *kmpath_handlerd; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 115 | static void process_queued_ios(struct work_struct *work); |
| 116 | static void trigger_event(struct work_struct *work); |
Chandra Seetharaman | bab7cfc | 2008-05-01 14:50:22 -0700 | [diff] [blame] | 117 | static void activate_path(struct work_struct *work); |
Mike Anderson | 224cb3e | 2008-08-29 09:36:09 +0200 | [diff] [blame] | 118 | static void deactivate_path(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | |
| 121 | /*----------------------------------------------- |
| 122 | * Allocation routines |
| 123 | *-----------------------------------------------*/ |
| 124 | |
| 125 | static struct pgpath *alloc_pgpath(void) |
| 126 | { |
Micha³ Miros³aw | e69fae5 | 2006-10-03 01:15:34 -0700 | [diff] [blame] | 127 | struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Mike Anderson | 224cb3e | 2008-08-29 09:36:09 +0200 | [diff] [blame] | 129 | if (pgpath) { |
Kiyoshi Ueda | 6680073 | 2008-10-10 13:36:58 +0100 | [diff] [blame] | 130 | pgpath->is_active = 1; |
Mike Anderson | 224cb3e | 2008-08-29 09:36:09 +0200 | [diff] [blame] | 131 | INIT_WORK(&pgpath->deactivate_path, deactivate_path); |
Chandra Seetharaman | e54f77d | 2009-06-22 10:12:12 +0100 | [diff] [blame] | 132 | INIT_WORK(&pgpath->activate_path, activate_path); |
Mike Anderson | 224cb3e | 2008-08-29 09:36:09 +0200 | [diff] [blame] | 133 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | return pgpath; |
| 136 | } |
| 137 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 138 | static void free_pgpath(struct pgpath *pgpath) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
| 140 | kfree(pgpath); |
| 141 | } |
| 142 | |
Mike Anderson | 224cb3e | 2008-08-29 09:36:09 +0200 | [diff] [blame] | 143 | static void deactivate_path(struct work_struct *work) |
| 144 | { |
| 145 | struct pgpath *pgpath = |
| 146 | container_of(work, struct pgpath, deactivate_path); |
| 147 | |
| 148 | blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue); |
| 149 | } |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | static struct priority_group *alloc_priority_group(void) |
| 152 | { |
| 153 | struct priority_group *pg; |
| 154 | |
Micha³ Miros³aw | e69fae5 | 2006-10-03 01:15:34 -0700 | [diff] [blame] | 155 | pg = kzalloc(sizeof(*pg), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Micha³ Miros³aw | e69fae5 | 2006-10-03 01:15:34 -0700 | [diff] [blame] | 157 | if (pg) |
| 158 | INIT_LIST_HEAD(&pg->pgpaths); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | return pg; |
| 161 | } |
| 162 | |
| 163 | static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti) |
| 164 | { |
| 165 | struct pgpath *pgpath, *tmp; |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 166 | struct multipath *m = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | list_for_each_entry_safe(pgpath, tmp, pgpaths, list) { |
| 169 | list_del(&pgpath->list); |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 170 | if (m->hw_handler_name) |
| 171 | scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | dm_put_device(ti, pgpath->path.dev); |
| 173 | free_pgpath(pgpath); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static void free_priority_group(struct priority_group *pg, |
| 178 | struct dm_target *ti) |
| 179 | { |
| 180 | struct path_selector *ps = &pg->ps; |
| 181 | |
| 182 | if (ps->type) { |
| 183 | ps->type->destroy(ps); |
| 184 | dm_put_path_selector(ps->type); |
| 185 | } |
| 186 | |
| 187 | free_pgpaths(&pg->pgpaths, ti); |
| 188 | kfree(pg); |
| 189 | } |
| 190 | |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame] | 191 | static struct multipath *alloc_multipath(struct dm_target *ti) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | struct multipath *m; |
| 194 | |
Micha³ Miros³aw | e69fae5 | 2006-10-03 01:15:34 -0700 | [diff] [blame] | 195 | m = kzalloc(sizeof(*m), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | if (m) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | INIT_LIST_HEAD(&m->priority_groups); |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 198 | INIT_LIST_HEAD(&m->queued_ios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | spin_lock_init(&m->lock); |
| 200 | m->queue_io = 1; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 201 | INIT_WORK(&m->process_queued_ios, process_queued_ios); |
| 202 | INIT_WORK(&m->trigger_event, trigger_event); |
Mike Anderson | 6380f26 | 2009-12-10 23:52:21 +0000 | [diff] [blame] | 203 | mutex_init(&m->work_mutex); |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 204 | m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | if (!m->mpio_pool) { |
| 206 | kfree(m); |
| 207 | return NULL; |
| 208 | } |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame] | 209 | m->ti = ti; |
| 210 | ti->private = m; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | return m; |
| 214 | } |
| 215 | |
| 216 | static void free_multipath(struct multipath *m) |
| 217 | { |
| 218 | struct priority_group *pg, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) { |
| 221 | list_del(&pg->list); |
| 222 | free_priority_group(pg, m->ti); |
| 223 | } |
| 224 | |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 225 | kfree(m->hw_handler_name); |
Chandra Seetharaman | 2bfd2e1 | 2009-08-03 12:42:45 -0700 | [diff] [blame] | 226 | kfree(m->hw_handler_params); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | mempool_destroy(m->mpio_pool); |
| 228 | kfree(m); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | /*----------------------------------------------- |
| 233 | * Path selection |
| 234 | *-----------------------------------------------*/ |
| 235 | |
| 236 | static void __switch_pg(struct multipath *m, struct pgpath *pgpath) |
| 237 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | m->current_pg = pgpath->pg; |
| 239 | |
| 240 | /* Must we initialise the PG first, and queue I/O till it's ready? */ |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 241 | if (m->hw_handler_name) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | m->pg_init_required = 1; |
| 243 | m->queue_io = 1; |
| 244 | } else { |
| 245 | m->pg_init_required = 0; |
| 246 | m->queue_io = 0; |
| 247 | } |
Dave Wysochanski | c9e4558 | 2007-10-19 22:47:53 +0100 | [diff] [blame] | 248 | |
| 249 | m->pg_init_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Kiyoshi Ueda | 02ab823 | 2009-06-22 10:12:27 +0100 | [diff] [blame] | 252 | static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg, |
| 253 | size_t nr_bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
Josef "Jeff" Sipek | c922d5f | 2006-12-08 02:36:33 -0800 | [diff] [blame] | 255 | struct dm_path *path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Kiyoshi Ueda | 02ab823 | 2009-06-22 10:12:27 +0100 | [diff] [blame] | 257 | path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if (!path) |
| 259 | return -ENXIO; |
| 260 | |
| 261 | m->current_pgpath = path_to_pgpath(path); |
| 262 | |
| 263 | if (m->current_pg != pg) |
| 264 | __switch_pg(m, m->current_pgpath); |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
Kiyoshi Ueda | 02ab823 | 2009-06-22 10:12:27 +0100 | [diff] [blame] | 269 | static void __choose_pgpath(struct multipath *m, size_t nr_bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
| 271 | struct priority_group *pg; |
| 272 | unsigned bypassed = 1; |
| 273 | |
| 274 | if (!m->nr_valid_paths) |
| 275 | goto failed; |
| 276 | |
| 277 | /* Were we instructed to switch PG? */ |
| 278 | if (m->next_pg) { |
| 279 | pg = m->next_pg; |
| 280 | m->next_pg = NULL; |
Kiyoshi Ueda | 02ab823 | 2009-06-22 10:12:27 +0100 | [diff] [blame] | 281 | if (!__choose_path_in_pg(m, pg, nr_bytes)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | return; |
| 283 | } |
| 284 | |
| 285 | /* Don't change PG until it has no remaining paths */ |
Kiyoshi Ueda | 02ab823 | 2009-06-22 10:12:27 +0100 | [diff] [blame] | 286 | if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | return; |
| 288 | |
| 289 | /* |
| 290 | * Loop through priority groups until we find a valid path. |
| 291 | * First time we skip PGs marked 'bypassed'. |
| 292 | * Second time we only try the ones we skipped. |
| 293 | */ |
| 294 | do { |
| 295 | list_for_each_entry(pg, &m->priority_groups, list) { |
| 296 | if (pg->bypassed == bypassed) |
| 297 | continue; |
Kiyoshi Ueda | 02ab823 | 2009-06-22 10:12:27 +0100 | [diff] [blame] | 298 | if (!__choose_path_in_pg(m, pg, nr_bytes)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | return; |
| 300 | } |
| 301 | } while (bypassed--); |
| 302 | |
| 303 | failed: |
| 304 | m->current_pgpath = NULL; |
| 305 | m->current_pg = NULL; |
| 306 | } |
| 307 | |
Kiyoshi Ueda | 45e1572 | 2006-12-08 02:41:10 -0800 | [diff] [blame] | 308 | /* |
| 309 | * Check whether bios must be queued in the device-mapper core rather |
| 310 | * than here in the target. |
| 311 | * |
| 312 | * m->lock must be held on entry. |
| 313 | * |
| 314 | * If m->queue_if_no_path and m->saved_queue_if_no_path hold the |
| 315 | * same value then we are not between multipath_presuspend() |
| 316 | * and multipath_resume() calls and we have no need to check |
| 317 | * for the DMF_NOFLUSH_SUSPENDING flag. |
| 318 | */ |
| 319 | static int __must_push_back(struct multipath *m) |
| 320 | { |
| 321 | return (m->queue_if_no_path != m->saved_queue_if_no_path && |
| 322 | dm_noflush_suspending(m->ti)); |
| 323 | } |
| 324 | |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 325 | static int map_io(struct multipath *m, struct request *clone, |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 326 | struct dm_mpath_io *mpio, unsigned was_queued) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 328 | int r = DM_MAPIO_REMAPPED; |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 329 | size_t nr_bytes = blk_rq_bytes(clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | unsigned long flags; |
| 331 | struct pgpath *pgpath; |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 332 | struct block_device *bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | spin_lock_irqsave(&m->lock, flags); |
| 335 | |
| 336 | /* Do we need to select a new pgpath? */ |
| 337 | if (!m->current_pgpath || |
| 338 | (!m->queue_io && (m->repeat_count && --m->repeat_count == 0))) |
Kiyoshi Ueda | 02ab823 | 2009-06-22 10:12:27 +0100 | [diff] [blame] | 339 | __choose_pgpath(m, nr_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
| 341 | pgpath = m->current_pgpath; |
| 342 | |
| 343 | if (was_queued) |
| 344 | m->queue_size--; |
| 345 | |
| 346 | if ((pgpath && m->queue_io) || |
Alasdair G Kergon | 436d410 | 2005-07-12 15:53:03 -0700 | [diff] [blame] | 347 | (!pgpath && m->queue_if_no_path)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | /* Queue for the daemon to resubmit */ |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 349 | list_add_tail(&clone->queuelist, &m->queued_ios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | m->queue_size++; |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 351 | if ((m->pg_init_required && !m->pg_init_in_progress) || |
| 352 | !m->queue_io) |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 353 | queue_work(kmultipathd, &m->process_queued_ios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | pgpath = NULL; |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 355 | r = DM_MAPIO_SUBMITTED; |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 356 | } else if (pgpath) { |
| 357 | bdev = pgpath->path.dev->bdev; |
| 358 | clone->q = bdev_get_queue(bdev); |
| 359 | clone->rq_disk = bdev->bd_disk; |
| 360 | } else if (__must_push_back(m)) |
Kiyoshi Ueda | 45e1572 | 2006-12-08 02:41:10 -0800 | [diff] [blame] | 361 | r = DM_MAPIO_REQUEUE; |
| 362 | else |
| 363 | r = -EIO; /* Failed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
| 365 | mpio->pgpath = pgpath; |
Kiyoshi Ueda | 02ab823 | 2009-06-22 10:12:27 +0100 | [diff] [blame] | 366 | mpio->nr_bytes = nr_bytes; |
| 367 | |
| 368 | if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io) |
| 369 | pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path, |
| 370 | nr_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
| 372 | spin_unlock_irqrestore(&m->lock, flags); |
| 373 | |
| 374 | return r; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * If we run out of usable paths, should we queue I/O or error it? |
| 379 | */ |
Alasdair G Kergon | 485ef69 | 2005-09-27 21:45:45 -0700 | [diff] [blame] | 380 | static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path, |
| 381 | unsigned save_old_value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | { |
| 383 | unsigned long flags; |
| 384 | |
| 385 | spin_lock_irqsave(&m->lock, flags); |
| 386 | |
Alasdair G Kergon | 485ef69 | 2005-09-27 21:45:45 -0700 | [diff] [blame] | 387 | if (save_old_value) |
| 388 | m->saved_queue_if_no_path = m->queue_if_no_path; |
| 389 | else |
| 390 | m->saved_queue_if_no_path = queue_if_no_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | m->queue_if_no_path = queue_if_no_path; |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 392 | if (!m->queue_if_no_path && m->queue_size) |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 393 | queue_work(kmultipathd, &m->process_queued_ios); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
| 395 | spin_unlock_irqrestore(&m->lock, flags); |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | /*----------------------------------------------------------------- |
| 401 | * The multipath daemon is responsible for resubmitting queued ios. |
| 402 | *---------------------------------------------------------------*/ |
| 403 | |
| 404 | static void dispatch_queued_ios(struct multipath *m) |
| 405 | { |
| 406 | int r; |
| 407 | unsigned long flags; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 408 | struct dm_mpath_io *mpio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | union map_info *info; |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 410 | struct request *clone, *n; |
| 411 | LIST_HEAD(cl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | spin_lock_irqsave(&m->lock, flags); |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 414 | list_splice_init(&m->queued_ios, &cl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | spin_unlock_irqrestore(&m->lock, flags); |
| 416 | |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 417 | list_for_each_entry_safe(clone, n, &cl, queuelist) { |
| 418 | list_del_init(&clone->queuelist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 420 | info = dm_get_rq_mapinfo(clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | mpio = info->ptr; |
| 422 | |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 423 | r = map_io(m, clone, mpio, 1); |
| 424 | if (r < 0) { |
| 425 | mempool_free(mpio, m->mpio_pool); |
| 426 | dm_kill_unmapped_request(clone, r); |
| 427 | } else if (r == DM_MAPIO_REMAPPED) |
| 428 | dm_dispatch_request(clone); |
| 429 | else if (r == DM_MAPIO_REQUEUE) { |
| 430 | mempool_free(mpio, m->mpio_pool); |
| 431 | dm_requeue_unmapped_request(clone); |
| 432 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 436 | static void process_queued_ios(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 438 | struct multipath *m = |
| 439 | container_of(work, struct multipath, process_queued_ios); |
Chandra Seetharaman | e54f77d | 2009-06-22 10:12:12 +0100 | [diff] [blame] | 440 | struct pgpath *pgpath = NULL, *tmp; |
| 441 | unsigned must_queue = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | unsigned long flags; |
| 443 | |
| 444 | spin_lock_irqsave(&m->lock, flags); |
| 445 | |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 446 | if (!m->queue_size) |
| 447 | goto out; |
| 448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | if (!m->current_pgpath) |
Kiyoshi Ueda | 02ab823 | 2009-06-22 10:12:27 +0100 | [diff] [blame] | 450 | __choose_pgpath(m, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | pgpath = m->current_pgpath; |
| 453 | |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 454 | if ((pgpath && !m->queue_io) || |
| 455 | (!pgpath && !m->queue_if_no_path)) |
| 456 | must_queue = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
Chandra Seetharaman | b81aa1c | 2008-11-13 23:39:00 +0000 | [diff] [blame] | 458 | if (m->pg_init_required && !m->pg_init_in_progress && pgpath) { |
Dave Wysochanski | c9e4558 | 2007-10-19 22:47:53 +0100 | [diff] [blame] | 459 | m->pg_init_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | m->pg_init_required = 0; |
Chandra Seetharaman | e54f77d | 2009-06-22 10:12:12 +0100 | [diff] [blame] | 461 | list_for_each_entry(tmp, &pgpath->pg->pgpaths, list) { |
Moger, Babu | f7b934c | 2010-03-06 02:29:49 +0000 | [diff] [blame] | 462 | /* Skip failed paths */ |
| 463 | if (!tmp->is_active) |
| 464 | continue; |
Chandra Seetharaman | e54f77d | 2009-06-22 10:12:12 +0100 | [diff] [blame] | 465 | if (queue_work(kmpath_handlerd, &tmp->activate_path)) |
| 466 | m->pg_init_in_progress++; |
| 467 | } |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 468 | } |
Alasdair G Kergon | c3cd4f6 | 2005-07-12 15:53:04 -0700 | [diff] [blame] | 469 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | spin_unlock_irqrestore(&m->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | if (!must_queue) |
| 472 | dispatch_queued_ios(m); |
| 473 | } |
| 474 | |
| 475 | /* |
| 476 | * An event is triggered whenever a path is taken out of use. |
| 477 | * Includes path failure and PG bypass. |
| 478 | */ |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 479 | static void trigger_event(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 481 | struct multipath *m = |
| 482 | container_of(work, struct multipath, trigger_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | dm_table_event(m->ti->table); |
| 485 | } |
| 486 | |
| 487 | /*----------------------------------------------------------------- |
| 488 | * Constructor/argument parsing: |
| 489 | * <#multipath feature args> [<arg>]* |
| 490 | * <#hw_handler args> [hw_handler [<arg>]*] |
| 491 | * <#priority groups> |
| 492 | * <initial priority group> |
| 493 | * [<selector> <#selector args> [<arg>]* |
| 494 | * <#paths> <#per-path selector args> |
| 495 | * [<path> [<arg>]* ]+ ]+ |
| 496 | *---------------------------------------------------------------*/ |
| 497 | struct param { |
| 498 | unsigned min; |
| 499 | unsigned max; |
| 500 | char *error; |
| 501 | }; |
| 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | static int read_param(struct param *param, char *str, unsigned *v, char **error) |
| 504 | { |
| 505 | if (!str || |
| 506 | (sscanf(str, "%u", v) != 1) || |
| 507 | (*v < param->min) || |
| 508 | (*v > param->max)) { |
| 509 | *error = param->error; |
| 510 | return -EINVAL; |
| 511 | } |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | struct arg_set { |
| 517 | unsigned argc; |
| 518 | char **argv; |
| 519 | }; |
| 520 | |
| 521 | static char *shift(struct arg_set *as) |
| 522 | { |
| 523 | char *r; |
| 524 | |
| 525 | if (as->argc) { |
| 526 | as->argc--; |
| 527 | r = *as->argv; |
| 528 | as->argv++; |
| 529 | return r; |
| 530 | } |
| 531 | |
| 532 | return NULL; |
| 533 | } |
| 534 | |
| 535 | static void consume(struct arg_set *as, unsigned n) |
| 536 | { |
| 537 | BUG_ON (as->argc < n); |
| 538 | as->argc -= n; |
| 539 | as->argv += n; |
| 540 | } |
| 541 | |
| 542 | static int parse_path_selector(struct arg_set *as, struct priority_group *pg, |
| 543 | struct dm_target *ti) |
| 544 | { |
| 545 | int r; |
| 546 | struct path_selector_type *pst; |
| 547 | unsigned ps_argc; |
| 548 | |
| 549 | static struct param _params[] = { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 550 | {0, 1024, "invalid number of path selector args"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | }; |
| 552 | |
| 553 | pst = dm_get_path_selector(shift(as)); |
| 554 | if (!pst) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 555 | ti->error = "unknown path selector type"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | return -EINVAL; |
| 557 | } |
| 558 | |
| 559 | r = read_param(_params, shift(as), &ps_argc, &ti->error); |
Mikulas Patocka | 371b2e3 | 2008-07-21 12:00:24 +0100 | [diff] [blame] | 560 | if (r) { |
| 561 | dm_put_path_selector(pst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | return -EINVAL; |
Mikulas Patocka | 371b2e3 | 2008-07-21 12:00:24 +0100 | [diff] [blame] | 563 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
Mikulas Patocka | 0e0497c | 2009-06-22 10:08:02 +0100 | [diff] [blame] | 565 | if (ps_argc > as->argc) { |
| 566 | dm_put_path_selector(pst); |
| 567 | ti->error = "not enough arguments for path selector"; |
| 568 | return -EINVAL; |
| 569 | } |
| 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | r = pst->create(&pg->ps, ps_argc, as->argv); |
| 572 | if (r) { |
| 573 | dm_put_path_selector(pst); |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 574 | ti->error = "path selector constructor failed"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | return r; |
| 576 | } |
| 577 | |
| 578 | pg->ps.type = pst; |
| 579 | consume(as, ps_argc); |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps, |
| 585 | struct dm_target *ti) |
| 586 | { |
| 587 | int r; |
| 588 | struct pgpath *p; |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 589 | struct multipath *m = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
| 591 | /* we need at least a path arg */ |
| 592 | if (as->argc < 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 593 | ti->error = "no device given"; |
Benjamin Marzinski | 01460f3 | 2008-10-10 13:36:57 +0100 | [diff] [blame] | 594 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | p = alloc_pgpath(); |
| 598 | if (!p) |
Benjamin Marzinski | 01460f3 | 2008-10-10 13:36:57 +0100 | [diff] [blame] | 599 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
| 601 | r = dm_get_device(ti, shift(as), ti->begin, ti->len, |
| 602 | dm_table_get_mode(ti->table), &p->path.dev); |
| 603 | if (r) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 604 | ti->error = "error getting device"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | goto bad; |
| 606 | } |
| 607 | |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 608 | if (m->hw_handler_name) { |
Hannes Reinecke | a0cf7ea | 2009-06-22 10:12:11 +0100 | [diff] [blame] | 609 | struct request_queue *q = bdev_get_queue(p->path.dev->bdev); |
| 610 | |
| 611 | r = scsi_dh_attach(q, m->hw_handler_name); |
| 612 | if (r == -EBUSY) { |
| 613 | /* |
| 614 | * Already attached to different hw_handler, |
| 615 | * try to reattach with correct one. |
| 616 | */ |
| 617 | scsi_dh_detach(q); |
| 618 | r = scsi_dh_attach(q, m->hw_handler_name); |
| 619 | } |
| 620 | |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 621 | if (r < 0) { |
Hannes Reinecke | a0cf7ea | 2009-06-22 10:12:11 +0100 | [diff] [blame] | 622 | ti->error = "error attaching hardware handler"; |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 623 | dm_put_device(ti, p->path.dev); |
| 624 | goto bad; |
| 625 | } |
Chandra Seetharaman | 2bfd2e1 | 2009-08-03 12:42:45 -0700 | [diff] [blame] | 626 | |
| 627 | if (m->hw_handler_params) { |
| 628 | r = scsi_dh_set_params(q, m->hw_handler_params); |
| 629 | if (r < 0) { |
| 630 | ti->error = "unable to set hardware " |
| 631 | "handler parameters"; |
| 632 | scsi_dh_detach(q); |
| 633 | dm_put_device(ti, p->path.dev); |
| 634 | goto bad; |
| 635 | } |
| 636 | } |
Hannes Reinecke | ae11b1b | 2008-07-17 17:49:02 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error); |
| 640 | if (r) { |
| 641 | dm_put_device(ti, p->path.dev); |
| 642 | goto bad; |
| 643 | } |
| 644 | |
| 645 | return p; |
| 646 | |
| 647 | bad: |
| 648 | free_pgpath(p); |
Benjamin Marzinski | 01460f3 | 2008-10-10 13:36:57 +0100 | [diff] [blame] | 649 | return ERR_PTR(r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static struct priority_group *parse_priority_group(struct arg_set *as, |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame] | 653 | struct multipath *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | { |
| 655 | static struct param _params[] = { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 656 | {1, 1024, "invalid number of paths"}, |
| 657 | {0, 1024, "invalid number of selector args"} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | }; |
| 659 | |
| 660 | int r; |
| 661 | unsigned i, nr_selector_args, nr_params; |
| 662 | struct priority_group *pg; |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame] | 663 | struct dm_target *ti = m->ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
| 665 | if (as->argc < 2) { |
| 666 | as->argc = 0; |
Benjamin Marzinski | 01460f3 | 2008-10-10 13:36:57 +0100 | [diff] [blame] | 667 | ti->error = "not enough priority group arguments"; |
| 668 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | pg = alloc_priority_group(); |
| 672 | if (!pg) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 673 | ti->error = "couldn't allocate priority group"; |
Benjamin Marzinski | 01460f3 | 2008-10-10 13:36:57 +0100 | [diff] [blame] | 674 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } |
| 676 | pg->m = m; |
| 677 | |
| 678 | r = parse_path_selector(as, pg, ti); |
| 679 | if (r) |
| 680 | goto bad; |
| 681 | |
| 682 | /* |
| 683 | * read the paths |
| 684 | */ |
| 685 | r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error); |
| 686 | if (r) |
| 687 | goto bad; |
| 688 | |
| 689 | r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error); |
| 690 | if (r) |
| 691 | goto bad; |
| 692 | |
| 693 | nr_params = 1 + nr_selector_args; |
| 694 | for (i = 0; i < pg->nr_pgpaths; i++) { |
| 695 | struct pgpath *pgpath; |
| 696 | struct arg_set path_args; |
| 697 | |
Mikulas Patocka | 148acff | 2008-07-21 12:00:30 +0100 | [diff] [blame] | 698 | if (as->argc < nr_params) { |
| 699 | ti->error = "not enough path parameters"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | goto bad; |
Mikulas Patocka | 148acff | 2008-07-21 12:00:30 +0100 | [diff] [blame] | 701 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | |
| 703 | path_args.argc = nr_params; |
| 704 | path_args.argv = as->argv; |
| 705 | |
| 706 | pgpath = parse_path(&path_args, &pg->ps, ti); |
Benjamin Marzinski | 01460f3 | 2008-10-10 13:36:57 +0100 | [diff] [blame] | 707 | if (IS_ERR(pgpath)) { |
| 708 | r = PTR_ERR(pgpath); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | goto bad; |
Benjamin Marzinski | 01460f3 | 2008-10-10 13:36:57 +0100 | [diff] [blame] | 710 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
| 712 | pgpath->pg = pg; |
| 713 | list_add_tail(&pgpath->list, &pg->pgpaths); |
| 714 | consume(as, nr_params); |
| 715 | } |
| 716 | |
| 717 | return pg; |
| 718 | |
| 719 | bad: |
| 720 | free_priority_group(pg, ti); |
Benjamin Marzinski | 01460f3 | 2008-10-10 13:36:57 +0100 | [diff] [blame] | 721 | return ERR_PTR(r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame] | 724 | static int parse_hw_handler(struct arg_set *as, struct multipath *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | unsigned hw_argc; |
Chandra Seetharaman | 2bfd2e1 | 2009-08-03 12:42:45 -0700 | [diff] [blame] | 727 | int ret; |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame] | 728 | struct dm_target *ti = m->ti; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | |
| 730 | static struct param _params[] = { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 731 | {0, 1024, "invalid number of hardware handler args"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | }; |
| 733 | |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 734 | if (read_param(_params, shift(as), &hw_argc, &ti->error)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | return -EINVAL; |
| 736 | |
| 737 | if (!hw_argc) |
| 738 | return 0; |
| 739 | |
Mikulas Patocka | e094f4f | 2009-06-22 10:12:10 +0100 | [diff] [blame] | 740 | if (hw_argc > as->argc) { |
| 741 | ti->error = "not enough arguments for hardware handler"; |
| 742 | return -EINVAL; |
| 743 | } |
| 744 | |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 745 | m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL); |
| 746 | request_module("scsi_dh_%s", m->hw_handler_name); |
| 747 | if (scsi_dh_handler_exist(m->hw_handler_name) == 0) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 748 | ti->error = "unknown hardware handler type"; |
Chandra Seetharaman | 2bfd2e1 | 2009-08-03 12:42:45 -0700 | [diff] [blame] | 749 | ret = -EINVAL; |
| 750 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | } |
Chandra Seetharaman | 14e98c5 | 2008-11-13 23:39:06 +0000 | [diff] [blame] | 752 | |
Chandra Seetharaman | 2bfd2e1 | 2009-08-03 12:42:45 -0700 | [diff] [blame] | 753 | if (hw_argc > 1) { |
| 754 | char *p; |
| 755 | int i, j, len = 4; |
| 756 | |
| 757 | for (i = 0; i <= hw_argc - 2; i++) |
| 758 | len += strlen(as->argv[i]) + 1; |
| 759 | p = m->hw_handler_params = kzalloc(len, GFP_KERNEL); |
| 760 | if (!p) { |
| 761 | ti->error = "memory allocation failed"; |
| 762 | ret = -ENOMEM; |
| 763 | goto fail; |
| 764 | } |
| 765 | j = sprintf(p, "%d", hw_argc - 1); |
| 766 | for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1) |
| 767 | j = sprintf(p, "%s", as->argv[i]); |
| 768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | consume(as, hw_argc - 1); |
| 770 | |
| 771 | return 0; |
Chandra Seetharaman | 2bfd2e1 | 2009-08-03 12:42:45 -0700 | [diff] [blame] | 772 | fail: |
| 773 | kfree(m->hw_handler_name); |
| 774 | m->hw_handler_name = NULL; |
| 775 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame] | 778 | static int parse_features(struct arg_set *as, struct multipath *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | { |
| 780 | int r; |
| 781 | unsigned argc; |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame] | 782 | struct dm_target *ti = m->ti; |
Dave Wysochanski | c9e4558 | 2007-10-19 22:47:53 +0100 | [diff] [blame] | 783 | const char *param_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | |
| 785 | static struct param _params[] = { |
Dave Wysochanski | c9e4558 | 2007-10-19 22:47:53 +0100 | [diff] [blame] | 786 | {0, 3, "invalid number of feature args"}, |
| 787 | {1, 50, "pg_init_retries must be between 1 and 50"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | }; |
| 789 | |
| 790 | r = read_param(_params, shift(as), &argc, &ti->error); |
| 791 | if (r) |
| 792 | return -EINVAL; |
| 793 | |
| 794 | if (!argc) |
| 795 | return 0; |
| 796 | |
Dave Wysochanski | c9e4558 | 2007-10-19 22:47:53 +0100 | [diff] [blame] | 797 | do { |
| 798 | param_name = shift(as); |
| 799 | argc--; |
| 800 | |
| 801 | if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) { |
| 802 | r = queue_if_no_path(m, 1, 0); |
| 803 | continue; |
| 804 | } |
| 805 | |
| 806 | if (!strnicmp(param_name, MESG_STR("pg_init_retries")) && |
| 807 | (argc >= 1)) { |
| 808 | r = read_param(_params + 1, shift(as), |
| 809 | &m->pg_init_retries, &ti->error); |
| 810 | argc--; |
| 811 | continue; |
| 812 | } |
| 813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | ti->error = "Unrecognised multipath feature request"; |
Dave Wysochanski | c9e4558 | 2007-10-19 22:47:53 +0100 | [diff] [blame] | 815 | r = -EINVAL; |
| 816 | } while (argc && !r); |
| 817 | |
| 818 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | static int multipath_ctr(struct dm_target *ti, unsigned int argc, |
| 822 | char **argv) |
| 823 | { |
| 824 | /* target parameters */ |
| 825 | static struct param _params[] = { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 826 | {1, 1024, "invalid number of priority groups"}, |
| 827 | {1, 1024, "invalid initial priority group number"}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | }; |
| 829 | |
| 830 | int r; |
| 831 | struct multipath *m; |
| 832 | struct arg_set as; |
| 833 | unsigned pg_count = 0; |
| 834 | unsigned next_pg_num; |
| 835 | |
| 836 | as.argc = argc; |
| 837 | as.argv = argv; |
| 838 | |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame] | 839 | m = alloc_multipath(ti); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | if (!m) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 841 | ti->error = "can't allocate multipath"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | return -EINVAL; |
| 843 | } |
| 844 | |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame] | 845 | r = parse_features(&as, m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | if (r) |
| 847 | goto bad; |
| 848 | |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame] | 849 | r = parse_hw_handler(&as, m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | if (r) |
| 851 | goto bad; |
| 852 | |
| 853 | r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error); |
| 854 | if (r) |
| 855 | goto bad; |
| 856 | |
| 857 | r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error); |
| 858 | if (r) |
| 859 | goto bad; |
| 860 | |
| 861 | /* parse the priority groups */ |
| 862 | while (as.argc) { |
| 863 | struct priority_group *pg; |
| 864 | |
Micha³ Miros³aw | 28f16c2 | 2006-10-03 01:15:33 -0700 | [diff] [blame] | 865 | pg = parse_priority_group(&as, m); |
Benjamin Marzinski | 01460f3 | 2008-10-10 13:36:57 +0100 | [diff] [blame] | 866 | if (IS_ERR(pg)) { |
| 867 | r = PTR_ERR(pg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | goto bad; |
| 869 | } |
| 870 | |
| 871 | m->nr_valid_paths += pg->nr_pgpaths; |
| 872 | list_add_tail(&pg->list, &m->priority_groups); |
| 873 | pg_count++; |
| 874 | pg->pg_num = pg_count; |
| 875 | if (!--next_pg_num) |
| 876 | m->next_pg = pg; |
| 877 | } |
| 878 | |
| 879 | if (pg_count != m->nr_priority_groups) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 880 | ti->error = "priority group count mismatch"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | r = -EINVAL; |
| 882 | goto bad; |
| 883 | } |
| 884 | |
Mikulas Patocka | 8627921 | 2009-06-22 10:12:24 +0100 | [diff] [blame] | 885 | ti->num_flush_requests = 1; |
| 886 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | return 0; |
| 888 | |
| 889 | bad: |
| 890 | free_multipath(m); |
| 891 | return r; |
| 892 | } |
| 893 | |
Kiyoshi Ueda | 6df400a | 2009-12-10 23:52:19 +0000 | [diff] [blame] | 894 | static void flush_multipath_work(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | { |
Chandra Seetharaman | bab7cfc | 2008-05-01 14:50:22 -0700 | [diff] [blame] | 896 | flush_workqueue(kmpath_handlerd); |
Alasdair G Kergon | a044d01 | 2005-07-12 15:53:02 -0700 | [diff] [blame] | 897 | flush_workqueue(kmultipathd); |
Mikulas Patocka | 53b351f | 2009-06-22 10:12:13 +0100 | [diff] [blame] | 898 | flush_scheduled_work(); |
Kiyoshi Ueda | 6df400a | 2009-12-10 23:52:19 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | static void multipath_dtr(struct dm_target *ti) |
| 902 | { |
| 903 | struct multipath *m = ti->private; |
| 904 | |
| 905 | flush_multipath_work(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | free_multipath(m); |
| 907 | } |
| 908 | |
| 909 | /* |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 910 | * Map cloned requests |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | */ |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 912 | static int multipath_map(struct dm_target *ti, struct request *clone, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | union map_info *map_context) |
| 914 | { |
| 915 | int r; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 916 | struct dm_mpath_io *mpio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | struct multipath *m = (struct multipath *) ti->private; |
| 918 | |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 919 | mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC); |
| 920 | if (!mpio) |
| 921 | /* ENOMEM, requeue */ |
| 922 | return DM_MAPIO_REQUEUE; |
| 923 | memset(mpio, 0, sizeof(*mpio)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | |
| 925 | map_context->ptr = mpio; |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 926 | clone->cmd_flags |= REQ_FAILFAST_TRANSPORT; |
| 927 | r = map_io(m, clone, mpio, 0); |
Kiyoshi Ueda | 45e1572 | 2006-12-08 02:41:10 -0800 | [diff] [blame] | 928 | if (r < 0 || r == DM_MAPIO_REQUEUE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | mempool_free(mpio, m->mpio_pool); |
| 930 | |
| 931 | return r; |
| 932 | } |
| 933 | |
| 934 | /* |
| 935 | * Take a path out of use. |
| 936 | */ |
| 937 | static int fail_path(struct pgpath *pgpath) |
| 938 | { |
| 939 | unsigned long flags; |
| 940 | struct multipath *m = pgpath->pg->m; |
| 941 | |
| 942 | spin_lock_irqsave(&m->lock, flags); |
| 943 | |
Kiyoshi Ueda | 6680073 | 2008-10-10 13:36:58 +0100 | [diff] [blame] | 944 | if (!pgpath->is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | goto out; |
| 946 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 947 | DMWARN("Failing path %s.", pgpath->path.dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
| 949 | pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path); |
Kiyoshi Ueda | 6680073 | 2008-10-10 13:36:58 +0100 | [diff] [blame] | 950 | pgpath->is_active = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | pgpath->fail_count++; |
| 952 | |
| 953 | m->nr_valid_paths--; |
| 954 | |
| 955 | if (pgpath == m->current_pgpath) |
| 956 | m->current_pgpath = NULL; |
| 957 | |
Mike Anderson | b15546f | 2007-10-19 22:48:02 +0100 | [diff] [blame] | 958 | dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti, |
| 959 | pgpath->path.dev->name, m->nr_valid_paths); |
| 960 | |
Alasdair G Kergon | fe9cf30 | 2009-01-06 03:05:13 +0000 | [diff] [blame] | 961 | schedule_work(&m->trigger_event); |
Mike Anderson | 224cb3e | 2008-08-29 09:36:09 +0200 | [diff] [blame] | 962 | queue_work(kmultipathd, &pgpath->deactivate_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | |
| 964 | out: |
| 965 | spin_unlock_irqrestore(&m->lock, flags); |
| 966 | |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * Reinstate a previously-failed path |
| 972 | */ |
| 973 | static int reinstate_path(struct pgpath *pgpath) |
| 974 | { |
| 975 | int r = 0; |
| 976 | unsigned long flags; |
| 977 | struct multipath *m = pgpath->pg->m; |
| 978 | |
| 979 | spin_lock_irqsave(&m->lock, flags); |
| 980 | |
Kiyoshi Ueda | 6680073 | 2008-10-10 13:36:58 +0100 | [diff] [blame] | 981 | if (pgpath->is_active) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | goto out; |
| 983 | |
Alasdair G Kergon | def052d | 2008-07-21 12:00:31 +0100 | [diff] [blame] | 984 | if (!pgpath->pg->ps.type->reinstate_path) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | DMWARN("Reinstate path not supported by path selector %s", |
| 986 | pgpath->pg->ps.type->name); |
| 987 | r = -EINVAL; |
| 988 | goto out; |
| 989 | } |
| 990 | |
| 991 | r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path); |
| 992 | if (r) |
| 993 | goto out; |
| 994 | |
Kiyoshi Ueda | 6680073 | 2008-10-10 13:36:58 +0100 | [diff] [blame] | 995 | pgpath->is_active = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
Chandra Seetharaman | e54f77d | 2009-06-22 10:12:12 +0100 | [diff] [blame] | 997 | if (!m->nr_valid_paths++ && m->queue_size) { |
| 998 | m->current_pgpath = NULL; |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 999 | queue_work(kmultipathd, &m->process_queued_ios); |
Chandra Seetharaman | e54f77d | 2009-06-22 10:12:12 +0100 | [diff] [blame] | 1000 | } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) { |
| 1001 | if (queue_work(kmpath_handlerd, &pgpath->activate_path)) |
| 1002 | m->pg_init_in_progress++; |
| 1003 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
Mike Anderson | b15546f | 2007-10-19 22:48:02 +0100 | [diff] [blame] | 1005 | dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti, |
| 1006 | pgpath->path.dev->name, m->nr_valid_paths); |
| 1007 | |
Alasdair G Kergon | fe9cf30 | 2009-01-06 03:05:13 +0000 | [diff] [blame] | 1008 | schedule_work(&m->trigger_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | |
| 1010 | out: |
| 1011 | spin_unlock_irqrestore(&m->lock, flags); |
| 1012 | |
| 1013 | return r; |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * Fail or reinstate all paths that match the provided struct dm_dev. |
| 1018 | */ |
| 1019 | static int action_dev(struct multipath *m, struct dm_dev *dev, |
| 1020 | action_fn action) |
| 1021 | { |
| 1022 | int r = 0; |
| 1023 | struct pgpath *pgpath; |
| 1024 | struct priority_group *pg; |
| 1025 | |
| 1026 | list_for_each_entry(pg, &m->priority_groups, list) { |
| 1027 | list_for_each_entry(pgpath, &pg->pgpaths, list) { |
| 1028 | if (pgpath->path.dev == dev) |
| 1029 | r = action(pgpath); |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | return r; |
| 1034 | } |
| 1035 | |
| 1036 | /* |
| 1037 | * Temporarily try to avoid having to use the specified PG |
| 1038 | */ |
| 1039 | static void bypass_pg(struct multipath *m, struct priority_group *pg, |
| 1040 | int bypassed) |
| 1041 | { |
| 1042 | unsigned long flags; |
| 1043 | |
| 1044 | spin_lock_irqsave(&m->lock, flags); |
| 1045 | |
| 1046 | pg->bypassed = bypassed; |
| 1047 | m->current_pgpath = NULL; |
| 1048 | m->current_pg = NULL; |
| 1049 | |
| 1050 | spin_unlock_irqrestore(&m->lock, flags); |
| 1051 | |
Alasdair G Kergon | fe9cf30 | 2009-01-06 03:05:13 +0000 | [diff] [blame] | 1052 | schedule_work(&m->trigger_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | /* |
| 1056 | * Switch to using the specified PG from the next I/O that gets mapped |
| 1057 | */ |
| 1058 | static int switch_pg_num(struct multipath *m, const char *pgstr) |
| 1059 | { |
| 1060 | struct priority_group *pg; |
| 1061 | unsigned pgnum; |
| 1062 | unsigned long flags; |
| 1063 | |
| 1064 | if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum || |
| 1065 | (pgnum > m->nr_priority_groups)) { |
| 1066 | DMWARN("invalid PG number supplied to switch_pg_num"); |
| 1067 | return -EINVAL; |
| 1068 | } |
| 1069 | |
| 1070 | spin_lock_irqsave(&m->lock, flags); |
| 1071 | list_for_each_entry(pg, &m->priority_groups, list) { |
| 1072 | pg->bypassed = 0; |
| 1073 | if (--pgnum) |
| 1074 | continue; |
| 1075 | |
| 1076 | m->current_pgpath = NULL; |
| 1077 | m->current_pg = NULL; |
| 1078 | m->next_pg = pg; |
| 1079 | } |
| 1080 | spin_unlock_irqrestore(&m->lock, flags); |
| 1081 | |
Alasdair G Kergon | fe9cf30 | 2009-01-06 03:05:13 +0000 | [diff] [blame] | 1082 | schedule_work(&m->trigger_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | /* |
| 1087 | * Set/clear bypassed status of a PG. |
| 1088 | * PGs are numbered upwards from 1 in the order they were declared. |
| 1089 | */ |
| 1090 | static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed) |
| 1091 | { |
| 1092 | struct priority_group *pg; |
| 1093 | unsigned pgnum; |
| 1094 | |
| 1095 | if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum || |
| 1096 | (pgnum > m->nr_priority_groups)) { |
| 1097 | DMWARN("invalid PG number supplied to bypass_pg"); |
| 1098 | return -EINVAL; |
| 1099 | } |
| 1100 | |
| 1101 | list_for_each_entry(pg, &m->priority_groups, list) { |
| 1102 | if (!--pgnum) |
| 1103 | break; |
| 1104 | } |
| 1105 | |
| 1106 | bypass_pg(m, pg, bypassed); |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | /* |
Dave Wysochanski | c9e4558 | 2007-10-19 22:47:53 +0100 | [diff] [blame] | 1111 | * Should we retry pg_init immediately? |
| 1112 | */ |
| 1113 | static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath) |
| 1114 | { |
| 1115 | unsigned long flags; |
| 1116 | int limit_reached = 0; |
| 1117 | |
| 1118 | spin_lock_irqsave(&m->lock, flags); |
| 1119 | |
| 1120 | if (m->pg_init_count <= m->pg_init_retries) |
| 1121 | m->pg_init_required = 1; |
| 1122 | else |
| 1123 | limit_reached = 1; |
| 1124 | |
| 1125 | spin_unlock_irqrestore(&m->lock, flags); |
| 1126 | |
| 1127 | return limit_reached; |
| 1128 | } |
| 1129 | |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 1130 | static void pg_init_done(void *data, int errors) |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 1131 | { |
Moger, Babu | 83c0d5d | 2010-03-06 02:29:45 +0000 | [diff] [blame] | 1132 | struct pgpath *pgpath = data; |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 1133 | struct priority_group *pg = pgpath->pg; |
| 1134 | struct multipath *m = pg->m; |
| 1135 | unsigned long flags; |
| 1136 | |
| 1137 | /* device or driver problems */ |
| 1138 | switch (errors) { |
| 1139 | case SCSI_DH_OK: |
| 1140 | break; |
| 1141 | case SCSI_DH_NOSYS: |
| 1142 | if (!m->hw_handler_name) { |
| 1143 | errors = 0; |
| 1144 | break; |
| 1145 | } |
Moger, Babu | f7b934c | 2010-03-06 02:29:49 +0000 | [diff] [blame] | 1146 | DMERR("Could not failover the device: Handler scsi_dh_%s " |
| 1147 | "Error %d.", m->hw_handler_name, errors); |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 1148 | /* |
| 1149 | * Fail path for now, so we do not ping pong |
| 1150 | */ |
| 1151 | fail_path(pgpath); |
| 1152 | break; |
| 1153 | case SCSI_DH_DEV_TEMP_BUSY: |
| 1154 | /* |
| 1155 | * Probably doing something like FW upgrade on the |
| 1156 | * controller so try the other pg. |
| 1157 | */ |
| 1158 | bypass_pg(m, pg, 1); |
| 1159 | break; |
| 1160 | /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */ |
| 1161 | case SCSI_DH_RETRY: |
| 1162 | case SCSI_DH_IMM_RETRY: |
| 1163 | case SCSI_DH_RES_TEMP_UNAVAIL: |
| 1164 | if (pg_init_limit_reached(m, pgpath)) |
| 1165 | fail_path(pgpath); |
| 1166 | errors = 0; |
| 1167 | break; |
| 1168 | default: |
| 1169 | /* |
| 1170 | * We probably do not want to fail the path for a device |
| 1171 | * error, but this is what the old dm did. In future |
| 1172 | * patches we can do more advanced handling. |
| 1173 | */ |
| 1174 | fail_path(pgpath); |
| 1175 | } |
| 1176 | |
| 1177 | spin_lock_irqsave(&m->lock, flags); |
| 1178 | if (errors) { |
Chandra Seetharaman | e54f77d | 2009-06-22 10:12:12 +0100 | [diff] [blame] | 1179 | if (pgpath == m->current_pgpath) { |
| 1180 | DMERR("Could not failover device. Error %d.", errors); |
| 1181 | m->current_pgpath = NULL; |
| 1182 | m->current_pg = NULL; |
| 1183 | } |
Kiyoshi Ueda | d0259bf | 2010-03-06 02:30:02 +0000 | [diff] [blame^] | 1184 | } else if (!m->pg_init_required) |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 1185 | pg->bypassed = 0; |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 1186 | |
Kiyoshi Ueda | d0259bf | 2010-03-06 02:30:02 +0000 | [diff] [blame^] | 1187 | if (--m->pg_init_in_progress) |
| 1188 | /* Activations of other paths are still on going */ |
| 1189 | goto out; |
| 1190 | |
| 1191 | if (!m->pg_init_required) |
| 1192 | m->queue_io = 0; |
| 1193 | |
| 1194 | queue_work(kmultipathd, &m->process_queued_ios); |
| 1195 | |
| 1196 | out: |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 1197 | spin_unlock_irqrestore(&m->lock, flags); |
| 1198 | } |
| 1199 | |
Chandra Seetharaman | bab7cfc | 2008-05-01 14:50:22 -0700 | [diff] [blame] | 1200 | static void activate_path(struct work_struct *work) |
| 1201 | { |
Chandra Seetharaman | e54f77d | 2009-06-22 10:12:12 +0100 | [diff] [blame] | 1202 | struct pgpath *pgpath = |
| 1203 | container_of(work, struct pgpath, activate_path); |
Chandra Seetharaman | bab7cfc | 2008-05-01 14:50:22 -0700 | [diff] [blame] | 1204 | |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 1205 | scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev), |
Moger, Babu | 83c0d5d | 2010-03-06 02:29:45 +0000 | [diff] [blame] | 1206 | pg_init_done, pgpath); |
Chandra Seetharaman | bab7cfc | 2008-05-01 14:50:22 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | /* |
| 1210 | * end_io handling |
| 1211 | */ |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1212 | static int do_end_io(struct multipath *m, struct request *clone, |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1213 | int error, struct dm_mpath_io *mpio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | { |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1215 | /* |
| 1216 | * We don't queue any clone request inside the multipath target |
| 1217 | * during end I/O handling, since those clone requests don't have |
| 1218 | * bio clones. If we queue them inside the multipath target, |
| 1219 | * we need to make bio clones, that requires memory allocation. |
| 1220 | * (See drivers/md/dm.c:end_clone_bio() about why the clone requests |
| 1221 | * don't have bio clones.) |
| 1222 | * Instead of queueing the clone request here, we queue the original |
| 1223 | * request into dm core, which will remake a clone request and |
| 1224 | * clone bios for it and resubmit it later. |
| 1225 | */ |
| 1226 | int r = DM_ENDIO_REQUEUE; |
Stefan Bader | 640eb3b | 2005-11-21 21:32:35 -0800 | [diff] [blame] | 1227 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1229 | if (!error && !clone->errors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | return 0; /* I/O complete */ |
| 1231 | |
Alasdair G Kergon | f6a80ea | 2005-07-12 15:53:01 -0700 | [diff] [blame] | 1232 | if (error == -EOPNOTSUPP) |
| 1233 | return error; |
| 1234 | |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 1235 | if (mpio->pgpath) |
| 1236 | fail_path(mpio->pgpath); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | |
Stefan Bader | 640eb3b | 2005-11-21 21:32:35 -0800 | [diff] [blame] | 1238 | spin_lock_irqsave(&m->lock, flags); |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1239 | if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m)) |
| 1240 | r = -EIO; |
Stefan Bader | 640eb3b | 2005-11-21 21:32:35 -0800 | [diff] [blame] | 1241 | spin_unlock_irqrestore(&m->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1243 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1246 | static int multipath_end_io(struct dm_target *ti, struct request *clone, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | int error, union map_info *map_context) |
| 1248 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1249 | struct multipath *m = ti->private; |
| 1250 | struct dm_mpath_io *mpio = map_context->ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | struct pgpath *pgpath = mpio->pgpath; |
| 1252 | struct path_selector *ps; |
| 1253 | int r; |
| 1254 | |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1255 | r = do_end_io(m, clone, error, mpio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | if (pgpath) { |
| 1257 | ps = &pgpath->pg->ps; |
| 1258 | if (ps->type->end_io) |
Kiyoshi Ueda | 02ab823 | 2009-06-22 10:12:27 +0100 | [diff] [blame] | 1259 | ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | } |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1261 | mempool_free(mpio, m->mpio_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
| 1263 | return r; |
| 1264 | } |
| 1265 | |
| 1266 | /* |
| 1267 | * 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] | 1268 | * the last path fails we must error any remaining I/O. |
| 1269 | * Note that if the freeze_bdev fails while suspending, the |
| 1270 | * queue_if_no_path state is lost - userspace should reset it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | */ |
| 1272 | static void multipath_presuspend(struct dm_target *ti) |
| 1273 | { |
| 1274 | struct multipath *m = (struct multipath *) ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | |
Alasdair G Kergon | 485ef69 | 2005-09-27 21:45:45 -0700 | [diff] [blame] | 1276 | queue_if_no_path(m, 0, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
Kiyoshi Ueda | 6df400a | 2009-12-10 23:52:19 +0000 | [diff] [blame] | 1279 | static void multipath_postsuspend(struct dm_target *ti) |
| 1280 | { |
Mike Anderson | 6380f26 | 2009-12-10 23:52:21 +0000 | [diff] [blame] | 1281 | struct multipath *m = ti->private; |
| 1282 | |
| 1283 | mutex_lock(&m->work_mutex); |
Kiyoshi Ueda | 6df400a | 2009-12-10 23:52:19 +0000 | [diff] [blame] | 1284 | flush_multipath_work(); |
Mike Anderson | 6380f26 | 2009-12-10 23:52:21 +0000 | [diff] [blame] | 1285 | mutex_unlock(&m->work_mutex); |
Kiyoshi Ueda | 6df400a | 2009-12-10 23:52:19 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Alasdair G Kergon | 436d410 | 2005-07-12 15:53:03 -0700 | [diff] [blame] | 1288 | /* |
| 1289 | * Restore the queue_if_no_path setting. |
| 1290 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | static void multipath_resume(struct dm_target *ti) |
| 1292 | { |
| 1293 | struct multipath *m = (struct multipath *) ti->private; |
| 1294 | unsigned long flags; |
| 1295 | |
| 1296 | spin_lock_irqsave(&m->lock, flags); |
Alasdair G Kergon | 436d410 | 2005-07-12 15:53:03 -0700 | [diff] [blame] | 1297 | m->queue_if_no_path = m->saved_queue_if_no_path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | spin_unlock_irqrestore(&m->lock, flags); |
| 1299 | } |
| 1300 | |
| 1301 | /* |
| 1302 | * Info output has the following format: |
| 1303 | * num_multipath_feature_args [multipath_feature_args]* |
| 1304 | * num_handler_status_args [handler_status_args]* |
| 1305 | * num_groups init_group_number |
| 1306 | * [A|D|E num_ps_status_args [ps_status_args]* |
| 1307 | * num_paths num_selector_args |
| 1308 | * [path_dev A|F fail_count [selector_args]* ]+ ]+ |
| 1309 | * |
| 1310 | * Table output has the following format (identical to the constructor string): |
| 1311 | * num_feature_args [features_args]* |
| 1312 | * num_handler_args hw_handler [hw_handler_args]* |
| 1313 | * num_groups init_group_number |
| 1314 | * [priority selector-name num_ps_args [ps_args]* |
| 1315 | * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+ |
| 1316 | */ |
| 1317 | static int multipath_status(struct dm_target *ti, status_type_t type, |
| 1318 | char *result, unsigned int maxlen) |
| 1319 | { |
| 1320 | int sz = 0; |
| 1321 | unsigned long flags; |
| 1322 | struct multipath *m = (struct multipath *) ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | struct priority_group *pg; |
| 1324 | struct pgpath *p; |
| 1325 | unsigned pg_num; |
| 1326 | char state; |
| 1327 | |
| 1328 | spin_lock_irqsave(&m->lock, flags); |
| 1329 | |
| 1330 | /* Features */ |
| 1331 | if (type == STATUSTYPE_INFO) |
Dave Wysochanski | c9e4558 | 2007-10-19 22:47:53 +0100 | [diff] [blame] | 1332 | DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count); |
| 1333 | else { |
| 1334 | DMEMIT("%u ", m->queue_if_no_path + |
| 1335 | (m->pg_init_retries > 0) * 2); |
| 1336 | if (m->queue_if_no_path) |
| 1337 | DMEMIT("queue_if_no_path "); |
| 1338 | if (m->pg_init_retries) |
| 1339 | DMEMIT("pg_init_retries %u ", m->pg_init_retries); |
| 1340 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 1342 | if (!m->hw_handler_name || type == STATUSTYPE_INFO) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | DMEMIT("0 "); |
| 1344 | else |
Chandra Seetharaman | cfae5c9 | 2008-05-01 14:50:11 -0700 | [diff] [blame] | 1345 | DMEMIT("1 %s ", m->hw_handler_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | |
| 1347 | DMEMIT("%u ", m->nr_priority_groups); |
| 1348 | |
| 1349 | if (m->next_pg) |
| 1350 | pg_num = m->next_pg->pg_num; |
| 1351 | else if (m->current_pg) |
| 1352 | pg_num = m->current_pg->pg_num; |
| 1353 | else |
| 1354 | pg_num = 1; |
| 1355 | |
| 1356 | DMEMIT("%u ", pg_num); |
| 1357 | |
| 1358 | switch (type) { |
| 1359 | case STATUSTYPE_INFO: |
| 1360 | list_for_each_entry(pg, &m->priority_groups, list) { |
| 1361 | if (pg->bypassed) |
| 1362 | state = 'D'; /* Disabled */ |
| 1363 | else if (pg == m->current_pg) |
| 1364 | state = 'A'; /* Currently Active */ |
| 1365 | else |
| 1366 | state = 'E'; /* Enabled */ |
| 1367 | |
| 1368 | DMEMIT("%c ", state); |
| 1369 | |
| 1370 | if (pg->ps.type->status) |
| 1371 | sz += pg->ps.type->status(&pg->ps, NULL, type, |
| 1372 | result + sz, |
| 1373 | maxlen - sz); |
| 1374 | else |
| 1375 | DMEMIT("0 "); |
| 1376 | |
| 1377 | DMEMIT("%u %u ", pg->nr_pgpaths, |
| 1378 | pg->ps.type->info_args); |
| 1379 | |
| 1380 | list_for_each_entry(p, &pg->pgpaths, list) { |
| 1381 | DMEMIT("%s %s %u ", p->path.dev->name, |
Kiyoshi Ueda | 6680073 | 2008-10-10 13:36:58 +0100 | [diff] [blame] | 1382 | p->is_active ? "A" : "F", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | p->fail_count); |
| 1384 | if (pg->ps.type->status) |
| 1385 | sz += pg->ps.type->status(&pg->ps, |
| 1386 | &p->path, type, result + sz, |
| 1387 | maxlen - sz); |
| 1388 | } |
| 1389 | } |
| 1390 | break; |
| 1391 | |
| 1392 | case STATUSTYPE_TABLE: |
| 1393 | list_for_each_entry(pg, &m->priority_groups, list) { |
| 1394 | DMEMIT("%s ", pg->ps.type->name); |
| 1395 | |
| 1396 | if (pg->ps.type->status) |
| 1397 | sz += pg->ps.type->status(&pg->ps, NULL, type, |
| 1398 | result + sz, |
| 1399 | maxlen - sz); |
| 1400 | else |
| 1401 | DMEMIT("0 "); |
| 1402 | |
| 1403 | DMEMIT("%u %u ", pg->nr_pgpaths, |
| 1404 | pg->ps.type->table_args); |
| 1405 | |
| 1406 | list_for_each_entry(p, &pg->pgpaths, list) { |
| 1407 | DMEMIT("%s ", p->path.dev->name); |
| 1408 | if (pg->ps.type->status) |
| 1409 | sz += pg->ps.type->status(&pg->ps, |
| 1410 | &p->path, type, result + sz, |
| 1411 | maxlen - sz); |
| 1412 | } |
| 1413 | } |
| 1414 | break; |
| 1415 | } |
| 1416 | |
| 1417 | spin_unlock_irqrestore(&m->lock, flags); |
| 1418 | |
| 1419 | return 0; |
| 1420 | } |
| 1421 | |
| 1422 | static int multipath_message(struct dm_target *ti, unsigned argc, char **argv) |
| 1423 | { |
Mike Anderson | 6380f26 | 2009-12-10 23:52:21 +0000 | [diff] [blame] | 1424 | int r = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | struct dm_dev *dev; |
| 1426 | struct multipath *m = (struct multipath *) ti->private; |
| 1427 | action_fn action; |
| 1428 | |
Mike Anderson | 6380f26 | 2009-12-10 23:52:21 +0000 | [diff] [blame] | 1429 | mutex_lock(&m->work_mutex); |
| 1430 | |
Kiyoshi Ueda | c2f3d24 | 2009-12-10 23:52:27 +0000 | [diff] [blame] | 1431 | if (dm_suspended(ti)) { |
| 1432 | r = -EBUSY; |
| 1433 | goto out; |
| 1434 | } |
| 1435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | if (argc == 1) { |
Mike Anderson | 6380f26 | 2009-12-10 23:52:21 +0000 | [diff] [blame] | 1437 | if (!strnicmp(argv[0], MESG_STR("queue_if_no_path"))) { |
| 1438 | r = queue_if_no_path(m, 1, 0); |
| 1439 | goto out; |
| 1440 | } else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path"))) { |
| 1441 | r = queue_if_no_path(m, 0, 0); |
| 1442 | goto out; |
| 1443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Mike Anderson | 6380f26 | 2009-12-10 23:52:21 +0000 | [diff] [blame] | 1446 | if (argc != 2) { |
| 1447 | DMWARN("Unrecognised multipath message received."); |
| 1448 | goto out; |
| 1449 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | |
Mike Anderson | 6380f26 | 2009-12-10 23:52:21 +0000 | [diff] [blame] | 1451 | if (!strnicmp(argv[0], MESG_STR("disable_group"))) { |
| 1452 | r = bypass_pg_num(m, argv[1], 1); |
| 1453 | goto out; |
| 1454 | } else if (!strnicmp(argv[0], MESG_STR("enable_group"))) { |
| 1455 | r = bypass_pg_num(m, argv[1], 0); |
| 1456 | goto out; |
| 1457 | } else if (!strnicmp(argv[0], MESG_STR("switch_group"))) { |
| 1458 | r = switch_pg_num(m, argv[1]); |
| 1459 | goto out; |
| 1460 | } else if (!strnicmp(argv[0], MESG_STR("reinstate_path"))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | action = reinstate_path; |
| 1462 | else if (!strnicmp(argv[0], MESG_STR("fail_path"))) |
| 1463 | action = fail_path; |
Mike Anderson | 6380f26 | 2009-12-10 23:52:21 +0000 | [diff] [blame] | 1464 | else { |
| 1465 | DMWARN("Unrecognised multipath message received."); |
| 1466 | goto out; |
| 1467 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | |
| 1469 | r = dm_get_device(ti, argv[1], ti->begin, ti->len, |
| 1470 | dm_table_get_mode(ti->table), &dev); |
| 1471 | if (r) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1472 | DMWARN("message: error getting device %s", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | argv[1]); |
Mike Anderson | 6380f26 | 2009-12-10 23:52:21 +0000 | [diff] [blame] | 1474 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | r = action_dev(m, dev, action); |
| 1478 | |
| 1479 | dm_put_device(ti, dev); |
| 1480 | |
Mike Anderson | 6380f26 | 2009-12-10 23:52:21 +0000 | [diff] [blame] | 1481 | out: |
| 1482 | mutex_unlock(&m->work_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
Al Viro | 647b3d0 | 2007-08-28 22:15:59 -0400 | [diff] [blame] | 1486 | static int multipath_ioctl(struct dm_target *ti, unsigned int cmd, |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1487 | unsigned long arg) |
| 1488 | { |
| 1489 | struct multipath *m = (struct multipath *) ti->private; |
| 1490 | struct block_device *bdev = NULL; |
Al Viro | 633a08b | 2007-08-29 20:34:12 -0400 | [diff] [blame] | 1491 | fmode_t mode = 0; |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1492 | unsigned long flags; |
| 1493 | int r = 0; |
| 1494 | |
| 1495 | spin_lock_irqsave(&m->lock, flags); |
| 1496 | |
| 1497 | if (!m->current_pgpath) |
Kiyoshi Ueda | 02ab823 | 2009-06-22 10:12:27 +0100 | [diff] [blame] | 1498 | __choose_pgpath(m, 0); |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1499 | |
Milan Broz | e90dae1 | 2006-10-03 01:15:22 -0700 | [diff] [blame] | 1500 | if (m->current_pgpath) { |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1501 | bdev = m->current_pgpath->path.dev->bdev; |
Al Viro | 633a08b | 2007-08-29 20:34:12 -0400 | [diff] [blame] | 1502 | mode = m->current_pgpath->path.dev->mode; |
Milan Broz | e90dae1 | 2006-10-03 01:15:22 -0700 | [diff] [blame] | 1503 | } |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1504 | |
| 1505 | if (m->queue_io) |
| 1506 | r = -EAGAIN; |
| 1507 | else if (!bdev) |
| 1508 | r = -EIO; |
| 1509 | |
| 1510 | spin_unlock_irqrestore(&m->lock, flags); |
| 1511 | |
Al Viro | 633a08b | 2007-08-29 20:34:12 -0400 | [diff] [blame] | 1512 | return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg); |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1515 | static int multipath_iterate_devices(struct dm_target *ti, |
| 1516 | iterate_devices_callout_fn fn, void *data) |
| 1517 | { |
| 1518 | struct multipath *m = ti->private; |
| 1519 | struct priority_group *pg; |
| 1520 | struct pgpath *p; |
| 1521 | int ret = 0; |
| 1522 | |
| 1523 | list_for_each_entry(pg, &m->priority_groups, list) { |
| 1524 | list_for_each_entry(p, &pg->pgpaths, list) { |
Mike Snitzer | 5dea271 | 2009-07-23 20:30:42 +0100 | [diff] [blame] | 1525 | ret = fn(ti, p->path.dev, ti->begin, ti->len, data); |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1526 | if (ret) |
| 1527 | goto out; |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | out: |
| 1532 | return ret; |
| 1533 | } |
| 1534 | |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1535 | static int __pgpath_busy(struct pgpath *pgpath) |
| 1536 | { |
| 1537 | struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); |
| 1538 | |
| 1539 | return dm_underlying_device_busy(q); |
| 1540 | } |
| 1541 | |
| 1542 | /* |
| 1543 | * We return "busy", only when we can map I/Os but underlying devices |
| 1544 | * are busy (so even if we map I/Os now, the I/Os will wait on |
| 1545 | * the underlying queue). |
| 1546 | * In other words, if we want to kill I/Os or queue them inside us |
| 1547 | * due to map unavailability, we don't return "busy". Otherwise, |
| 1548 | * dm core won't give us the I/Os and we can't do what we want. |
| 1549 | */ |
| 1550 | static int multipath_busy(struct dm_target *ti) |
| 1551 | { |
| 1552 | int busy = 0, has_active = 0; |
| 1553 | struct multipath *m = ti->private; |
| 1554 | struct priority_group *pg; |
| 1555 | struct pgpath *pgpath; |
| 1556 | unsigned long flags; |
| 1557 | |
| 1558 | spin_lock_irqsave(&m->lock, flags); |
| 1559 | |
| 1560 | /* Guess which priority_group will be used at next mapping time */ |
| 1561 | if (unlikely(!m->current_pgpath && m->next_pg)) |
| 1562 | pg = m->next_pg; |
| 1563 | else if (likely(m->current_pg)) |
| 1564 | pg = m->current_pg; |
| 1565 | else |
| 1566 | /* |
| 1567 | * We don't know which pg will be used at next mapping time. |
| 1568 | * We don't call __choose_pgpath() here to avoid to trigger |
| 1569 | * pg_init just by busy checking. |
| 1570 | * So we don't know whether underlying devices we will be using |
| 1571 | * at next mapping time are busy or not. Just try mapping. |
| 1572 | */ |
| 1573 | goto out; |
| 1574 | |
| 1575 | /* |
| 1576 | * If there is one non-busy active path at least, the path selector |
| 1577 | * will be able to select it. So we consider such a pg as not busy. |
| 1578 | */ |
| 1579 | busy = 1; |
| 1580 | list_for_each_entry(pgpath, &pg->pgpaths, list) |
| 1581 | if (pgpath->is_active) { |
| 1582 | has_active = 1; |
| 1583 | |
| 1584 | if (!__pgpath_busy(pgpath)) { |
| 1585 | busy = 0; |
| 1586 | break; |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | if (!has_active) |
| 1591 | /* |
| 1592 | * No active path in this pg, so this pg won't be used and |
| 1593 | * the current_pg will be changed at next mapping time. |
| 1594 | * We need to try mapping to determine it. |
| 1595 | */ |
| 1596 | busy = 0; |
| 1597 | |
| 1598 | out: |
| 1599 | spin_unlock_irqrestore(&m->lock, flags); |
| 1600 | |
| 1601 | return busy; |
| 1602 | } |
| 1603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | /*----------------------------------------------------------------- |
| 1605 | * Module setup |
| 1606 | *---------------------------------------------------------------*/ |
| 1607 | static struct target_type multipath_target = { |
| 1608 | .name = "multipath", |
Kiyoshi Ueda | 6df400a | 2009-12-10 23:52:19 +0000 | [diff] [blame] | 1609 | .version = {1, 1, 1}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | .module = THIS_MODULE, |
| 1611 | .ctr = multipath_ctr, |
| 1612 | .dtr = multipath_dtr, |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1613 | .map_rq = multipath_map, |
| 1614 | .rq_end_io = multipath_end_io, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | .presuspend = multipath_presuspend, |
Kiyoshi Ueda | 6df400a | 2009-12-10 23:52:19 +0000 | [diff] [blame] | 1616 | .postsuspend = multipath_postsuspend, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | .resume = multipath_resume, |
| 1618 | .status = multipath_status, |
| 1619 | .message = multipath_message, |
Milan Broz | 9af4aa3 | 2006-10-03 01:15:20 -0700 | [diff] [blame] | 1620 | .ioctl = multipath_ioctl, |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1621 | .iterate_devices = multipath_iterate_devices, |
Kiyoshi Ueda | f40c67f | 2009-06-22 10:12:37 +0100 | [diff] [blame] | 1622 | .busy = multipath_busy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | }; |
| 1624 | |
| 1625 | static int __init dm_multipath_init(void) |
| 1626 | { |
| 1627 | int r; |
| 1628 | |
| 1629 | /* allocate a slab for the dm_ios */ |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1630 | _mpio_cache = KMEM_CACHE(dm_mpath_io, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | if (!_mpio_cache) |
| 1632 | return -ENOMEM; |
| 1633 | |
| 1634 | r = dm_register_target(&multipath_target); |
| 1635 | if (r < 0) { |
Alasdair G Kergon | 0cd3312 | 2007-07-12 17:27:01 +0100 | [diff] [blame] | 1636 | DMERR("register failed %d", r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | kmem_cache_destroy(_mpio_cache); |
| 1638 | return -EINVAL; |
| 1639 | } |
| 1640 | |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 1641 | kmultipathd = create_workqueue("kmpathd"); |
| 1642 | if (!kmultipathd) { |
Alasdair G Kergon | 0cd3312 | 2007-07-12 17:27:01 +0100 | [diff] [blame] | 1643 | DMERR("failed to create workqueue kmpathd"); |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 1644 | dm_unregister_target(&multipath_target); |
| 1645 | kmem_cache_destroy(_mpio_cache); |
| 1646 | return -ENOMEM; |
| 1647 | } |
| 1648 | |
Chandra Seetharaman | bab7cfc | 2008-05-01 14:50:22 -0700 | [diff] [blame] | 1649 | /* |
| 1650 | * A separate workqueue is used to handle the device handlers |
| 1651 | * to avoid overloading existing workqueue. Overloading the |
| 1652 | * old workqueue would also create a bottleneck in the |
| 1653 | * path of the storage hardware device activation. |
| 1654 | */ |
| 1655 | kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd"); |
| 1656 | if (!kmpath_handlerd) { |
| 1657 | DMERR("failed to create workqueue kmpath_handlerd"); |
| 1658 | destroy_workqueue(kmultipathd); |
| 1659 | dm_unregister_target(&multipath_target); |
| 1660 | kmem_cache_destroy(_mpio_cache); |
| 1661 | return -ENOMEM; |
| 1662 | } |
| 1663 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1664 | DMINFO("version %u.%u.%u loaded", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | multipath_target.version[0], multipath_target.version[1], |
| 1666 | multipath_target.version[2]); |
| 1667 | |
| 1668 | return r; |
| 1669 | } |
| 1670 | |
| 1671 | static void __exit dm_multipath_exit(void) |
| 1672 | { |
Chandra Seetharaman | bab7cfc | 2008-05-01 14:50:22 -0700 | [diff] [blame] | 1673 | destroy_workqueue(kmpath_handlerd); |
Alasdair G Kergon | c557308 | 2005-05-05 16:16:07 -0700 | [diff] [blame] | 1674 | destroy_workqueue(kmultipathd); |
| 1675 | |
Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 1676 | dm_unregister_target(&multipath_target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | kmem_cache_destroy(_mpio_cache); |
| 1678 | } |
| 1679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | module_init(dm_multipath_init); |
| 1681 | module_exit(dm_multipath_exit); |
| 1682 | |
| 1683 | MODULE_DESCRIPTION(DM_NAME " multipath target"); |
| 1684 | MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>"); |
| 1685 | MODULE_LICENSE("GPL"); |