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