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