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