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