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