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