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