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