blob: 78cff42d987ee5274346aefff184075366a2a777 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Patocka586e80e2008-10-21 17:44:59 +01008#include <linux/device-mapper.h>
9
Mike Snitzer4cc96132016-05-12 16:28:10 -040010#include "dm-rq.h"
Mike Snitzer76e33fe2016-05-19 16:15:14 -040011#include "dm-bio-record.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "dm-path-selector.h"
Mike Andersonb15546f2007-10-19 22:48:02 +010013#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Mike Snitzere5863d92014-12-17 21:08:12 -050015#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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>
Anatol Pomazaube240ff2020-01-13 17:41:27 -050023#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/workqueue.h>
Mikulas Patocka35991652012-06-03 00:29:58 +010025#include <linux/delay.h>
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070026#include <scsi/scsi_dh.h>
Arun Sharma600634972011-07-26 16:09:06 -070027#include <linux/atomic.h>
Mike Snitzer78ce23b2016-01-31 17:38:28 -050028#include <linux/blk-mq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Alasdair G Kergon72d94862006-06-26 00:27:35 -070030#define DM_MSG_PREFIX "multipath"
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000031#define DM_PG_INIT_DELAY_MSECS 2000
32#define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
Anatol Pomazaube240ff2020-01-13 17:41:27 -050033#define QUEUE_IF_NO_PATH_TIMEOUT_DEFAULT 0
34
35static unsigned long queue_if_no_path_timeout_secs = QUEUE_IF_NO_PATH_TIMEOUT_DEFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/* Path properties */
38struct pgpath {
39 struct list_head list;
40
41 struct priority_group *pg; /* Owning PG */
42 unsigned fail_count; /* Cumulative failure count */
43
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080044 struct dm_path path;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000045 struct delayed_work activate_path;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -050046
47 bool is_active:1; /* Path status */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
50#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
51
52/*
53 * Paths are grouped into Priority Groups and numbered from 1 upwards.
54 * Each has a path selector which controls which path gets used.
55 */
56struct priority_group {
57 struct list_head list;
58
59 struct multipath *m; /* Owning multipath instance */
60 struct path_selector ps;
61
62 unsigned pg_num; /* Reference number */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 unsigned nr_pgpaths; /* Number of paths in PG */
64 struct list_head pgpaths;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -050065
66 bool bypassed:1; /* Temporarily bypass this PG? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
68
69/* Multipath context */
70struct multipath {
Mike Snitzer848b8ae2017-12-10 15:37:21 -050071 unsigned long flags; /* Multipath state flags */
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000072
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010073 spinlock_t lock;
Mike Snitzer848b8ae2017-12-10 15:37:21 -050074 enum dm_queue_mode queue_mode;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 struct pgpath *current_pgpath;
77 struct priority_group *current_pg;
78 struct priority_group *next_pg; /* Switch to this PG if set */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Mike Snitzer848b8ae2017-12-10 15:37:21 -050080 atomic_t nr_valid_paths; /* Total number of usable paths */
81 unsigned nr_priority_groups;
82 struct list_head priority_groups;
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010083
Mike Snitzer848b8ae2017-12-10 15:37:21 -050084 const char *hw_handler_name;
85 char *hw_handler_params;
86 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
Dave Wysochanskic9e45582007-10-19 22:47:53 +010087 unsigned pg_init_retries; /* Number of times to retry pg_init */
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000088 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
Mike Snitzer91e968a2016-03-17 17:10:15 -040089 atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */
90 atomic_t pg_init_count; /* Number of times pg_init called */
91
Mike Anderson6380f262009-12-10 23:52:21 +000092 struct mutex work_mutex;
Mike Snitzer20800cb2016-03-17 17:13:10 -040093 struct work_struct trigger_event;
Mike Snitzer848b8ae2017-12-10 15:37:21 -050094 struct dm_target *ti;
Mike Snitzer76e33fe2016-05-19 16:15:14 -040095
96 struct work_struct process_queued_bios;
97 struct bio_list queued_bios;
Anatol Pomazaube240ff2020-01-13 17:41:27 -050098
99 struct timer_list nopath_timer; /* Timeout for queue_if_no_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
102/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400103 * Context information attached to each io we process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100105struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct pgpath *pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100107 size_t nr_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110typedef int (*action_fn) (struct pgpath *pgpath);
111
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700112static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000113static void trigger_event(struct work_struct *work);
Bart Van Assche89bfce72017-04-27 10:11:14 -0700114static void activate_or_offline_path(struct pgpath *pgpath);
115static void activate_path_work(struct work_struct *work);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400116static void process_queued_bios(struct work_struct *work);
Anatol Pomazaube240ff2020-01-13 17:41:27 -0500117static void queue_if_no_path_timeout_work(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Mike Snitzer518257b2016-03-17 16:32:10 -0400119/*-----------------------------------------------
120 * Multipath state flags.
121 *-----------------------------------------------*/
122
123#define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */
124#define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */
125#define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */
126#define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */
127#define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */
128#define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */
129#define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/*-----------------------------------------------
132 * Allocation routines
133 *-----------------------------------------------*/
134
135static struct pgpath *alloc_pgpath(void)
136{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700137 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500139 if (!pgpath)
140 return NULL;
141
142 pgpath->is_active = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 return pgpath;
145}
146
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100147static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 kfree(pgpath);
150}
151
152static struct priority_group *alloc_priority_group(void)
153{
154 struct priority_group *pg;
155
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700156 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700158 if (pg)
159 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 return pg;
162}
163
164static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
165{
166 struct pgpath *pgpath, *tmp;
167
168 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
169 list_del(&pgpath->list);
170 dm_put_device(ti, pgpath->path.dev);
171 free_pgpath(pgpath);
172 }
173}
174
175static void free_priority_group(struct priority_group *pg,
176 struct dm_target *ti)
177{
178 struct path_selector *ps = &pg->ps;
179
180 if (ps->type) {
181 ps->type->destroy(ps);
182 dm_put_path_selector(ps->type);
183 }
184
185 free_pgpaths(&pg->pgpaths, ti);
186 kfree(pg);
187}
188
Mike Snitzere83068a2016-05-24 21:16:51 -0400189static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct multipath *m;
192
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700193 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 INIT_LIST_HEAD(&m->priority_groups);
196 spin_lock_init(&m->lock);
Mike Snitzer91e968a2016-03-17 17:10:15 -0400197 atomic_set(&m->nr_valid_paths, 0);
David Howellsc4028952006-11-22 14:57:56 +0000198 INIT_WORK(&m->trigger_event, trigger_event);
Mike Anderson6380f262009-12-10 23:52:21 +0000199 mutex_init(&m->work_mutex);
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500200
Mike Snitzere83068a2016-05-24 21:16:51 -0400201 m->queue_mode = DM_TYPE_NONE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400202
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700203 m->ti = ti;
204 ti->private = m;
Anatol Pomazaube240ff2020-01-13 17:41:27 -0500205
206 timer_setup(&m->nopath_timer, queue_if_no_path_timeout_work, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209 return m;
210}
211
Mike Snitzere83068a2016-05-24 21:16:51 -0400212static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m)
213{
214 if (m->queue_mode == DM_TYPE_NONE) {
Mike Snitzer953923c2018-10-11 11:06:29 -0400215 m->queue_mode = DM_TYPE_REQUEST_BASED;
Mike Snitzer8d47e652018-03-05 14:10:11 -0500216 } else if (m->queue_mode == DM_TYPE_BIO_BASED) {
Mike Snitzere83068a2016-05-24 21:16:51 -0400217 INIT_WORK(&m->process_queued_bios, process_queued_bios);
Mike Snitzer8d47e652018-03-05 14:10:11 -0500218 /*
219 * bio-based doesn't support any direct scsi_dh management;
220 * it just discovers if a scsi_dh is attached.
221 */
222 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
Mike Snitzere83068a2016-05-24 21:16:51 -0400223 }
224
225 dm_table_set_type(ti->table, m->queue_mode);
226
Mike Snitzerc3736672018-03-12 20:30:43 -0400227 /*
228 * Init fields that are only used when a scsi_dh is attached
229 * - must do this unconditionally (really doesn't hurt non-SCSI uses)
230 */
231 set_bit(MPATHF_QUEUE_IO, &m->flags);
232 atomic_set(&m->pg_init_in_progress, 0);
233 atomic_set(&m->pg_init_count, 0);
234 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
235 init_waitqueue_head(&m->pg_init_wait);
236
Mike Snitzere83068a2016-05-24 21:16:51 -0400237 return 0;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static void free_multipath(struct multipath *m)
241{
242 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
245 list_del(&pg->list);
246 free_priority_group(pg, m->ti);
247 }
248
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700249 kfree(m->hw_handler_name);
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700250 kfree(m->hw_handler_params);
Mike Snitzerd5ffebd2018-01-05 21:17:20 -0500251 mutex_destroy(&m->work_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 kfree(m);
253}
254
Mike Snitzer2eff1922016-02-03 09:13:14 -0500255static struct dm_mpath_io *get_mpio(union map_info *info)
256{
257 return info->ptr;
258}
259
Mike Snitzerbf661be2016-05-24 15:48:08 -0400260static size_t multipath_per_bio_data_size(void)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400261{
Mike Snitzerbf661be2016-05-24 15:48:08 -0400262 return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400263}
264
Mike Snitzerbf661be2016-05-24 15:48:08 -0400265static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
266{
267 return dm_per_bio_data(bio, multipath_per_bio_data_size());
268}
269
Mike Snitzerd07a2412017-12-11 16:08:54 -0500270static struct dm_bio_details *get_bio_details_from_mpio(struct dm_mpath_io *mpio)
Mike Snitzerbf661be2016-05-24 15:48:08 -0400271{
272 /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */
Mike Snitzerbf661be2016-05-24 15:48:08 -0400273 void *bio_details = mpio + 1;
Mike Snitzerbf661be2016-05-24 15:48:08 -0400274 return bio_details;
275}
276
Mike Snitzer63f6e6f2017-12-05 14:10:33 -0500277static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400278{
279 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
Mike Snitzerd07a2412017-12-11 16:08:54 -0500280 struct dm_bio_details *bio_details = get_bio_details_from_mpio(mpio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400281
Mike Snitzerd0442f82017-12-11 15:58:41 -0500282 mpio->nr_bytes = bio->bi_iter.bi_size;
283 mpio->pgpath = NULL;
284 *mpio_p = mpio;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400285
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400286 dm_bio_record(bio_details, bio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400287}
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/*-----------------------------------------------
290 * Path selection
291 *-----------------------------------------------*/
292
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100293static int __pg_init_all_paths(struct multipath *m)
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000294{
295 struct pgpath *pgpath;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000296 unsigned long pg_init_delay = 0;
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000297
Bart Van Asscheb1946792017-04-27 10:11:22 -0700298 lockdep_assert_held(&m->lock);
299
Mike Snitzer91e968a2016-03-17 17:10:15 -0400300 if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100301 return 0;
Hannes Reinecke17f4ff42014-02-28 15:33:42 +0100302
Mike Snitzer91e968a2016-03-17 17:10:15 -0400303 atomic_inc(&m->pg_init_count);
Mike Snitzer518257b2016-03-17 16:32:10 -0400304 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100305
306 /* Check here to reset pg_init_required */
307 if (!m->current_pg)
308 return 0;
309
Mike Snitzer518257b2016-03-17 16:32:10 -0400310 if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000311 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
312 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000313 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
314 /* Skip failed paths */
315 if (!pgpath->is_active)
316 continue;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000317 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
318 pg_init_delay))
Mike Snitzer91e968a2016-03-17 17:10:15 -0400319 atomic_inc(&m->pg_init_in_progress);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000320 }
Mike Snitzer91e968a2016-03-17 17:10:15 -0400321 return atomic_read(&m->pg_init_in_progress);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000322}
323
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700324static int pg_init_all_paths(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700326 int ret;
Mike Snitzer2da16102016-03-17 18:38:17 -0400327 unsigned long flags;
328
329 spin_lock_irqsave(&m->lock, flags);
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700330 ret = __pg_init_all_paths(m);
Mike Snitzer2da16102016-03-17 18:38:17 -0400331 spin_unlock_irqrestore(&m->lock, flags);
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700332
333 return ret;
Mike Snitzer2da16102016-03-17 18:38:17 -0400334}
335
336static void __switch_pg(struct multipath *m, struct priority_group *pg)
337{
338 m->current_pg = pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700341 if (m->hw_handler_name) {
Mike Snitzer518257b2016-03-17 16:32:10 -0400342 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
343 set_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 } else {
Mike Snitzer518257b2016-03-17 16:32:10 -0400345 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
346 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100348
Mike Snitzer91e968a2016-03-17 17:10:15 -0400349 atomic_set(&m->pg_init_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Mike Snitzer2da16102016-03-17 18:38:17 -0400352static struct pgpath *choose_path_in_pg(struct multipath *m,
353 struct priority_group *pg,
354 size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Mike Snitzer2da16102016-03-17 18:38:17 -0400356 unsigned long flags;
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800357 struct dm_path *path;
Mike Snitzer2da16102016-03-17 18:38:17 -0400358 struct pgpath *pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Mike Snitzer90a43232016-02-17 21:29:17 -0500360 path = pg->ps.type->select_path(&pg->ps, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (!path)
Mike Snitzer2da16102016-03-17 18:38:17 -0400362 return ERR_PTR(-ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Mike Snitzer2da16102016-03-17 18:38:17 -0400364 pgpath = path_to_pgpath(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Will Deacon506458e2017-10-24 11:22:48 +0100366 if (unlikely(READ_ONCE(m->current_pg) != pg)) {
Mike Snitzer2da16102016-03-17 18:38:17 -0400367 /* Only update current_pgpath if pg changed */
368 spin_lock_irqsave(&m->lock, flags);
369 m->current_pgpath = pgpath;
370 __switch_pg(m, pg);
371 spin_unlock_irqrestore(&m->lock, flags);
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Mike Snitzer2da16102016-03-17 18:38:17 -0400374 return pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Mike Snitzer2da16102016-03-17 18:38:17 -0400377static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Mike Snitzer2da16102016-03-17 18:38:17 -0400379 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 struct priority_group *pg;
Mike Snitzer2da16102016-03-17 18:38:17 -0400381 struct pgpath *pgpath;
Mike Snitzerd19a55c2017-01-06 15:33:14 -0500382 unsigned bypassed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Mike Snitzer91e968a2016-03-17 17:10:15 -0400384 if (!atomic_read(&m->nr_valid_paths)) {
Mike Snitzer8d47e652018-03-05 14:10:11 -0500385 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 goto failed;
Benjamin Marzinski1f271972014-08-13 13:53:42 -0500387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* Were we instructed to switch PG? */
Will Deacon506458e2017-10-24 11:22:48 +0100390 if (READ_ONCE(m->next_pg)) {
Mike Snitzer2da16102016-03-17 18:38:17 -0400391 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 pg = m->next_pg;
Mike Snitzer2da16102016-03-17 18:38:17 -0400393 if (!pg) {
394 spin_unlock_irqrestore(&m->lock, flags);
395 goto check_current_pg;
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 m->next_pg = NULL;
Mike Snitzer2da16102016-03-17 18:38:17 -0400398 spin_unlock_irqrestore(&m->lock, flags);
399 pgpath = choose_path_in_pg(m, pg, nr_bytes);
400 if (!IS_ERR_OR_NULL(pgpath))
401 return pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
404 /* Don't change PG until it has no remaining paths */
Mike Snitzer2da16102016-03-17 18:38:17 -0400405check_current_pg:
Will Deacon506458e2017-10-24 11:22:48 +0100406 pg = READ_ONCE(m->current_pg);
Mike Snitzer2da16102016-03-17 18:38:17 -0400407 if (pg) {
408 pgpath = choose_path_in_pg(m, pg, nr_bytes);
409 if (!IS_ERR_OR_NULL(pgpath))
410 return pgpath;
411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 /*
414 * Loop through priority groups until we find a valid path.
415 * First time we skip PGs marked 'bypassed'.
Mike Christief220fd42012-06-03 00:29:45 +0100416 * Second time we only try the ones we skipped, but set
417 * pg_init_delay_retry so we do not hammer controllers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
419 do {
420 list_for_each_entry(pg, &m->priority_groups, list) {
Mike Snitzerd19a55c2017-01-06 15:33:14 -0500421 if (pg->bypassed == !!bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 continue;
Mike Snitzer2da16102016-03-17 18:38:17 -0400423 pgpath = choose_path_in_pg(m, pg, nr_bytes);
424 if (!IS_ERR_OR_NULL(pgpath)) {
Mike Christief220fd42012-06-03 00:29:45 +0100425 if (!bypassed)
Mike Snitzer518257b2016-03-17 16:32:10 -0400426 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
Mike Snitzer2da16102016-03-17 18:38:17 -0400427 return pgpath;
Mike Christief220fd42012-06-03 00:29:45 +0100428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 } while (bypassed--);
431
432failed:
Mike Snitzer2da16102016-03-17 18:38:17 -0400433 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 m->current_pgpath = NULL;
435 m->current_pg = NULL;
Mike Snitzer2da16102016-03-17 18:38:17 -0400436 spin_unlock_irqrestore(&m->lock, flags);
437
438 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800441/*
Mike Snitzerac75b09f2020-05-14 12:55:39 -0400442 * dm_report_EIO() is a macro instead of a function to make pr_debug_ratelimited()
Bart Van Assche86331f32017-04-27 10:11:26 -0700443 * report the function name and line number of the function from which
444 * it has been invoked.
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800445 */
Bart Van Assche86331f32017-04-27 10:11:26 -0700446#define dm_report_EIO(m) \
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200447do { \
Bart Van Assche86331f32017-04-27 10:11:26 -0700448 struct mapped_device *md = dm_table_get_md((m)->ti->table); \
449 \
Mike Snitzerac75b09f2020-05-14 12:55:39 -0400450 DMDEBUG_LIMIT("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d", \
451 dm_device_name(md), \
452 test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags), \
453 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags), \
454 dm_noflush_suspending((m)->ti)); \
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200455} while (0)
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800456
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100457/*
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500458 * Check whether bios must be queued in the device-mapper core rather
459 * than here in the target.
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500460 */
Mike Snitzera862e4e2020-05-26 16:06:56 -0400461static bool __must_push_back(struct multipath *m)
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500462{
Mike Snitzera862e4e2020-05-26 16:06:56 -0400463 return dm_noflush_suspending(m->ti);
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500464}
465
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500466static bool must_push_back_rq(struct multipath *m)
467{
Mike Snitzera862e4e2020-05-26 16:06:56 -0400468 return test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) || __must_push_back(m);
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500469}
470
471/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400472 * Map cloned requests (request-based multipath)
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100473 */
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100474static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
475 union map_info *map_context,
476 struct request **__clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Mike Snitzer7943bd62016-02-02 21:53:15 -0500478 struct multipath *m = ti->private;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100479 size_t nr_bytes = blk_rq_bytes(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100481 struct block_device *bdev;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100482 struct dm_mpath_io *mpio = get_mpio(map_context);
Bart Van Assche7083abb2017-04-27 10:11:15 -0700483 struct request_queue *q;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100484 struct request *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /* Do we need to select a new pgpath? */
Will Deacon506458e2017-10-24 11:22:48 +0100487 pgpath = READ_ONCE(m->current_pgpath);
Mike Snitzer2da16102016-03-17 18:38:17 -0400488 if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
489 pgpath = choose_pgpath(m, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100491 if (!pgpath) {
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500492 if (must_push_back_rq(m))
Mike Snitzerb88efd42016-09-09 19:26:19 -0400493 return DM_MAPIO_DELAY_REQUEUE;
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200494 dm_report_EIO(m); /* Failed */
Christoph Hellwigf98e0eb2017-05-15 17:28:38 +0200495 return DM_MAPIO_KILL;
Mike Snitzer518257b2016-03-17 16:32:10 -0400496 } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
497 test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
Ming Lei459b5402018-01-11 14:01:55 +0800498 pg_init_all_paths(m);
499 return DM_MAPIO_DELAY_REQUEUE;
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100500 }
Mike Snitzer6afbc012014-07-08 11:55:09 -0400501
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100502 mpio->pgpath = pgpath;
503 mpio->nr_bytes = nr_bytes;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600504
505 bdev = pgpath->path.dev->bdev;
Bart Van Assche7083abb2017-04-27 10:11:15 -0700506 q = bdev_get_queue(bdev);
Christoph Hellwigff005a02018-05-09 09:54:05 +0200507 clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE,
508 BLK_MQ_REQ_NOWAIT);
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100509 if (IS_ERR(clone)) {
510 /* EBUSY, ENODEV or EWOULDBLOCK: requeue */
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500511 if (blk_queue_dying(q)) {
Bart Van Assche7083abb2017-04-27 10:11:15 -0700512 atomic_inc(&m->pg_init_in_progress);
513 activate_or_offline_path(pgpath);
Ming Lei050af082018-01-11 14:01:56 +0800514 return DM_MAPIO_DELAY_REQUEUE;
Bart Van Assche7083abb2017-04-27 10:11:15 -0700515 }
Ming Lei050af082018-01-11 14:01:56 +0800516
517 /*
518 * blk-mq's SCHED_RESTART can cover this requeue, so we
519 * needn't deal with it by DELAY_REQUEUE. More importantly,
520 * we have to return DM_MAPIO_REQUEUE so that blk-mq can
521 * get the queue busy feedback (via BLK_STS_RESOURCE),
522 * otherwise I/O merging can suffer.
523 */
Jens Axboe6a23e052018-10-10 20:49:26 -0600524 return DM_MAPIO_REQUEUE;
Mike Snitzere5863d92014-12-17 21:08:12 -0500525 }
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100526 clone->bio = clone->biotail = NULL;
527 clone->rq_disk = bdev->bd_disk;
528 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
529 *__clone = clone;
Mike Snitzere5863d92014-12-17 21:08:12 -0500530
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100531 if (pgpath->pg->ps.type->start_io)
532 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
533 &pgpath->path,
534 nr_bytes);
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600535 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Yufen Yu5de719e2019-04-24 23:19:05 +0800538static void multipath_release_clone(struct request *clone,
539 union map_info *map_context)
Mike Snitzere5863d92014-12-17 21:08:12 -0500540{
Yufen Yu5de719e2019-04-24 23:19:05 +0800541 if (unlikely(map_context)) {
542 /*
543 * non-NULL map_context means caller is still map
544 * method; must undo multipath_clone_and_map()
545 */
546 struct dm_mpath_io *mpio = get_mpio(map_context);
547 struct pgpath *pgpath = mpio->pgpath;
548
549 if (pgpath && pgpath->pg->ps.type->end_io)
550 pgpath->pg->ps.type->end_io(&pgpath->pg->ps,
551 &pgpath->path,
Gabriel Krisman Bertazi087615bf2020-04-30 16:48:29 -0400552 mpio->nr_bytes,
553 clone->io_start_time_ns);
Yufen Yu5de719e2019-04-24 23:19:05 +0800554 }
555
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100556 blk_put_request(clone);
Mike Snitzere5863d92014-12-17 21:08:12 -0500557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400560 * Map cloned bios (bio-based multipath)
561 */
Mike Snitzer0001ec52017-12-11 11:02:29 -0500562
563static struct pgpath *__map_bio(struct multipath *m, struct bio *bio)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400564{
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400565 struct pgpath *pgpath;
566 unsigned long flags;
567 bool queue_io;
568
569 /* Do we need to select a new pgpath? */
Will Deacon506458e2017-10-24 11:22:48 +0100570 pgpath = READ_ONCE(m->current_pgpath);
Gabriel Krisman Bertazi5686dee2020-04-27 20:39:11 -0400571 if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
Mike Snitzer0001ec52017-12-11 11:02:29 -0500572 pgpath = choose_pgpath(m, bio->bi_iter.bi_size);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400573
Gabriel Krisman Bertazi5686dee2020-04-27 20:39:11 -0400574 /* MPATHF_QUEUE_IO might have been cleared by choose_pgpath. */
575 queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
576
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400577 if ((pgpath && queue_io) ||
578 (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
579 /* Queue for the daemon to resubmit */
580 spin_lock_irqsave(&m->lock, flags);
581 bio_list_add(&m->queued_bios, bio);
582 spin_unlock_irqrestore(&m->lock, flags);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500583
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400584 /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
585 if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
586 pg_init_all_paths(m);
587 else if (!queue_io)
588 queue_work(kmultipathd, &m->process_queued_bios);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500589
Mike Snitzer0001ec52017-12-11 11:02:29 -0500590 return ERR_PTR(-EAGAIN);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400591 }
592
Mike Snitzer0001ec52017-12-11 11:02:29 -0500593 return pgpath;
594}
595
Mike Snitzer0001ec52017-12-11 11:02:29 -0500596static int __multipath_map_bio(struct multipath *m, struct bio *bio,
597 struct dm_mpath_io *mpio)
598{
Mike Snitzerdbaf9712019-11-26 10:08:29 -0500599 struct pgpath *pgpath = __map_bio(m, bio);
Mike Snitzer0001ec52017-12-11 11:02:29 -0500600
601 if (IS_ERR(pgpath))
602 return DM_MAPIO_SUBMITTED;
603
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400604 if (!pgpath) {
Mike Snitzera862e4e2020-05-26 16:06:56 -0400605 if (__must_push_back(m))
Bart Van Asscheca5beb72017-04-27 10:11:24 -0700606 return DM_MAPIO_REQUEUE;
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200607 dm_report_EIO(m);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200608 return DM_MAPIO_KILL;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400609 }
610
611 mpio->pgpath = pgpath;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400612
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200613 bio->bi_status = 0;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200614 bio_set_dev(bio, pgpath->path.dev->bdev);
Jens Axboe1eff9d32016-08-05 15:35:16 -0600615 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400616
617 if (pgpath->pg->ps.type->start_io)
618 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
619 &pgpath->path,
Mike Snitzerd0442f82017-12-11 15:58:41 -0500620 mpio->nr_bytes);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400621 return DM_MAPIO_REMAPPED;
622}
623
624static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
625{
626 struct multipath *m = ti->private;
Mike Snitzerbf661be2016-05-24 15:48:08 -0400627 struct dm_mpath_io *mpio = NULL;
628
Mike Snitzer63f6e6f2017-12-05 14:10:33 -0500629 multipath_init_per_bio_data(bio, &mpio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400630 return __multipath_map_bio(m, bio, mpio);
631}
632
Mike Snitzer7e48c762016-09-14 10:47:03 -0400633static void process_queued_io_list(struct multipath *m)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400634{
Mike Snitzer953923c2018-10-11 11:06:29 -0400635 if (m->queue_mode == DM_TYPE_REQUEST_BASED)
Mike Snitzer7e48c762016-09-14 10:47:03 -0400636 dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
Mike Snitzer8d47e652018-03-05 14:10:11 -0500637 else if (m->queue_mode == DM_TYPE_BIO_BASED)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400638 queue_work(kmultipathd, &m->process_queued_bios);
639}
640
641static void process_queued_bios(struct work_struct *work)
642{
643 int r;
644 unsigned long flags;
645 struct bio *bio;
646 struct bio_list bios;
647 struct blk_plug plug;
648 struct multipath *m =
649 container_of(work, struct multipath, process_queued_bios);
650
651 bio_list_init(&bios);
652
653 spin_lock_irqsave(&m->lock, flags);
654
655 if (bio_list_empty(&m->queued_bios)) {
656 spin_unlock_irqrestore(&m->lock, flags);
657 return;
658 }
659
660 bio_list_merge(&bios, &m->queued_bios);
661 bio_list_init(&m->queued_bios);
662
663 spin_unlock_irqrestore(&m->lock, flags);
664
665 blk_start_plug(&plug);
666 while ((bio = bio_list_pop(&bios))) {
Mike Snitzer1836df02017-12-06 16:08:14 -0500667 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
668 dm_bio_restore(get_bio_details_from_mpio(mpio), bio);
669 r = __multipath_map_bio(m, bio, mpio);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200670 switch (r) {
671 case DM_MAPIO_KILL:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200672 bio->bi_status = BLK_STS_IOERR;
673 bio_endio(bio);
Dan Carpenter047385b2017-06-14 08:22:55 -0600674 break;
Christoph Hellwig846785e2017-06-03 09:38:02 +0200675 case DM_MAPIO_REQUEUE:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200676 bio->bi_status = BLK_STS_DM_REQUEUE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400677 bio_endio(bio);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200678 break;
679 case DM_MAPIO_REMAPPED:
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400680 generic_make_request(bio);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200681 break;
Wang Sheng-Hui8192a0c2018-03-10 10:18:55 +0800682 case DM_MAPIO_SUBMITTED:
Bart Van Assche9157c8d2017-08-09 11:32:14 -0700683 break;
684 default:
685 WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200686 }
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400687 }
688 blk_finish_plug(&plug);
689}
690
691/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * If we run out of usable paths, should we queue I/O or error it?
693 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -0500694static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
Mike Snitzer4c3f4832020-05-29 15:59:13 -0400695 bool save_old_value, const char *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
697 unsigned long flags;
Mike Snitzer553ec942020-05-27 16:32:51 -0400698 bool queue_if_no_path_bit, saved_queue_if_no_path_bit;
Mike Snitzer4c3f4832020-05-29 15:59:13 -0400699 const char *dm_dev_name = dm_device_name(dm_table_get_md(m->ti->table));
700
701 DMDEBUG("%s: %s caller=%s queue_if_no_path=%d save_old_value=%d",
702 dm_dev_name, __func__, caller, queue_if_no_path, save_old_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 spin_lock_irqsave(&m->lock, flags);
Mike Snitzer553ec942020-05-27 16:32:51 -0400705
706 queue_if_no_path_bit = test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
707 saved_queue_if_no_path_bit = test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
708
709 if (save_old_value) {
710 if (unlikely(!queue_if_no_path_bit && saved_queue_if_no_path_bit)) {
711 DMERR("%s: QIFNP disabled but saved as enabled, saving again loses state, not saving!",
Mike Snitzer4c3f4832020-05-29 15:59:13 -0400712 dm_dev_name);
Mike Snitzer553ec942020-05-27 16:32:51 -0400713 } else
714 assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path_bit);
715 } else if (!queue_if_no_path && saved_queue_if_no_path_bit) {
716 /* due to "fail_if_no_path" message, need to honor it. */
717 clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
718 }
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500719 assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path);
Mike Snitzer553ec942020-05-27 16:32:51 -0400720
Mike Snitzer4c3f4832020-05-29 15:59:13 -0400721 DMDEBUG("%s: after %s changes; QIFNP = %d; SQIFNP = %d; DNFS = %d",
722 dm_dev_name, __func__,
723 test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags),
724 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags),
725 dm_noflush_suspending(m->ti));
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 spin_unlock_irqrestore(&m->lock, flags);
728
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400729 if (!queue_if_no_path) {
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200730 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -0400731 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400732 }
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return 0;
735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/*
Anatol Pomazaube240ff2020-01-13 17:41:27 -0500738 * If the queue_if_no_path timeout fires, turn off queue_if_no_path and
739 * process any queued I/O.
740 */
741static void queue_if_no_path_timeout_work(struct timer_list *t)
742{
743 struct multipath *m = from_timer(m, t, nopath_timer);
744 struct mapped_device *md = dm_table_get_md(m->ti->table);
745
746 DMWARN("queue_if_no_path timeout on %s, failing queued IO", dm_device_name(md));
Mike Snitzer4c3f4832020-05-29 15:59:13 -0400747 queue_if_no_path(m, false, false, __func__);
Anatol Pomazaube240ff2020-01-13 17:41:27 -0500748}
749
750/*
751 * Enable the queue_if_no_path timeout if necessary.
752 * Called with m->lock held.
753 */
754static void enable_nopath_timeout(struct multipath *m)
755{
756 unsigned long queue_if_no_path_timeout =
757 READ_ONCE(queue_if_no_path_timeout_secs) * HZ;
758
759 lockdep_assert_held(&m->lock);
760
761 if (queue_if_no_path_timeout > 0 &&
762 atomic_read(&m->nr_valid_paths) == 0 &&
763 test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
764 mod_timer(&m->nopath_timer,
765 jiffies + queue_if_no_path_timeout);
766 }
767}
768
769static void disable_nopath_timeout(struct multipath *m)
770{
771 del_timer_sync(&m->nopath_timer);
772}
773
774/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * An event is triggered whenever a path is taken out of use.
776 * Includes path failure and PG bypass.
777 */
David Howellsc4028952006-11-22 14:57:56 +0000778static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
David Howellsc4028952006-11-22 14:57:56 +0000780 struct multipath *m =
781 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 dm_table_event(m->ti->table);
784}
785
786/*-----------------------------------------------------------------
787 * Constructor/argument parsing:
788 * <#multipath feature args> [<arg>]*
789 * <#hw_handler args> [hw_handler [<arg>]*]
790 * <#priority groups>
791 * <initial priority group>
792 * [<selector> <#selector args> [<arg>]*
793 * <#paths> <#per-path selector args>
794 * [<path> [<arg>]* ]+ ]+
795 *---------------------------------------------------------------*/
Mike Snitzer498f0102011-08-02 12:32:04 +0100796static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 struct dm_target *ti)
798{
799 int r;
800 struct path_selector_type *pst;
801 unsigned ps_argc;
802
Eric Biggers5916a222017-06-22 11:32:45 -0700803 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700804 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 };
806
Mike Snitzer498f0102011-08-02 12:32:04 +0100807 pst = dm_get_path_selector(dm_shift_arg(as));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700809 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -EINVAL;
811 }
812
Mike Snitzer498f0102011-08-02 12:32:04 +0100813 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100814 if (r) {
815 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 r = pst->create(&pg->ps, ps_argc, as->argv);
820 if (r) {
821 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700822 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return r;
824 }
825
826 pg->ps.type = pst;
Mike Snitzer498f0102011-08-02 12:32:04 +0100827 dm_consume_args(as, ps_argc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 return 0;
830}
831
Mike Snitzere8f74a02018-03-12 19:49:25 -0400832static int setup_scsi_dh(struct block_device *bdev, struct multipath *m,
Mike Snitzerb5922112018-09-17 11:38:47 -0400833 const char **attached_handler_name, char **error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500835 struct request_queue *q = bdev_get_queue(bdev);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500836 int r;
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100837
Mike Snitzer518257b2016-03-17 16:32:10 -0400838 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200839retain:
Mike Snitzerb5922112018-09-17 11:38:47 -0400840 if (*attached_handler_name) {
Mike Snitzera58a9352012-07-27 15:08:04 +0100841 /*
tang.junhui54cd6402016-11-24 15:11:48 +0800842 * Clear any hw_handler_params associated with a
843 * handler that isn't already attached.
844 */
Mike Snitzerb5922112018-09-17 11:38:47 -0400845 if (m->hw_handler_name && strcmp(*attached_handler_name, m->hw_handler_name)) {
tang.junhui54cd6402016-11-24 15:11:48 +0800846 kfree(m->hw_handler_params);
847 m->hw_handler_params = NULL;
848 }
849
850 /*
Mike Snitzera58a9352012-07-27 15:08:04 +0100851 * Reset hw_handler_name to match the attached handler
Mike Snitzera58a9352012-07-27 15:08:04 +0100852 *
853 * NB. This modifies the table line to show the actual
854 * handler instead of the original table passed in.
855 */
856 kfree(m->hw_handler_name);
Mike Snitzerb5922112018-09-17 11:38:47 -0400857 m->hw_handler_name = *attached_handler_name;
858 *attached_handler_name = NULL;
Mike Snitzera58a9352012-07-27 15:08:04 +0100859 }
860 }
861
862 if (m->hw_handler_name) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100863 r = scsi_dh_attach(q, m->hw_handler_name);
864 if (r == -EBUSY) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200865 char b[BDEVNAME_SIZE];
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100866
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200867 printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500868 bdevname(bdev, b));
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200869 goto retain;
870 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700871 if (r < 0) {
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500872 *error = "error attaching hardware handler";
873 return r;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700874 }
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700875
876 if (m->hw_handler_params) {
877 r = scsi_dh_set_params(q, m->hw_handler_params);
878 if (r < 0) {
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500879 *error = "unable to set hardware handler parameters";
880 return r;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700881 }
882 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700883 }
884
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500885 return 0;
886}
887
888static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
889 struct dm_target *ti)
890{
891 int r;
892 struct pgpath *p;
893 struct multipath *m = ti->private;
Mike Snitzere8f74a02018-03-12 19:49:25 -0400894 struct request_queue *q;
Mike Snitzerb5922112018-09-17 11:38:47 -0400895 const char *attached_handler_name = NULL;
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500896
897 /* we need at least a path arg */
898 if (as->argc < 1) {
899 ti->error = "no device given";
900 return ERR_PTR(-EINVAL);
901 }
902
903 p = alloc_pgpath();
904 if (!p)
905 return ERR_PTR(-ENOMEM);
906
907 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
908 &p->path.dev);
909 if (r) {
910 ti->error = "error getting device";
911 goto bad;
912 }
913
Mike Snitzere8f74a02018-03-12 19:49:25 -0400914 q = bdev_get_queue(p->path.dev->bdev);
915 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
Mike Snitzere457edf2018-03-29 11:50:10 -0400916 if (attached_handler_name || m->hw_handler_name) {
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500917 INIT_DELAYED_WORK(&p->activate_path, activate_path_work);
Mike Snitzerb5922112018-09-17 11:38:47 -0400918 r = setup_scsi_dh(p->path.dev->bdev, m, &attached_handler_name, &ti->error);
Martin Wilck940bc472019-04-29 11:48:15 +0200919 kfree(attached_handler_name);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500920 if (r) {
921 dm_put_device(ti, p->path.dev);
922 goto bad;
923 }
924 }
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
927 if (r) {
928 dm_put_device(ti, p->path.dev);
929 goto bad;
930 }
931
932 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 bad:
934 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100935 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
Mike Snitzer498f0102011-08-02 12:32:04 +0100938static struct priority_group *parse_priority_group(struct dm_arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700939 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Eric Biggers5916a222017-06-22 11:32:45 -0700941 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700942 {1, 1024, "invalid number of paths"},
943 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 };
945
946 int r;
Mike Snitzer498f0102011-08-02 12:32:04 +0100947 unsigned i, nr_selector_args, nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700949 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 if (as->argc < 2) {
952 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100953 ti->error = "not enough priority group arguments";
954 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956
957 pg = alloc_priority_group();
958 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700959 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100960 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962 pg->m = m;
963
964 r = parse_path_selector(as, pg, ti);
965 if (r)
966 goto bad;
967
968 /*
969 * read the paths
970 */
Mike Snitzer498f0102011-08-02 12:32:04 +0100971 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (r)
973 goto bad;
974
Mike Snitzer498f0102011-08-02 12:32:04 +0100975 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (r)
977 goto bad;
978
Mike Snitzer498f0102011-08-02 12:32:04 +0100979 nr_args = 1 + nr_selector_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 for (i = 0; i < pg->nr_pgpaths; i++) {
981 struct pgpath *pgpath;
Mike Snitzer498f0102011-08-02 12:32:04 +0100982 struct dm_arg_set path_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Mike Snitzer498f0102011-08-02 12:32:04 +0100984 if (as->argc < nr_args) {
Mikulas Patocka148acff2008-07-21 12:00:30 +0100985 ti->error = "not enough path parameters";
Alasdair G Kergon6bbf79a12010-08-12 04:13:49 +0100986 r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Mike Snitzer498f0102011-08-02 12:32:04 +0100990 path_args.argc = nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 path_args.argv = as->argv;
992
993 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100994 if (IS_ERR(pgpath)) {
995 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 pgpath->pg = pg;
1000 list_add_tail(&pgpath->list, &pg->pgpaths);
Mike Snitzer498f0102011-08-02 12:32:04 +01001001 dm_consume_args(as, nr_args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003
1004 return pg;
1005
1006 bad:
1007 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +01001008 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Mike Snitzer498f0102011-08-02 12:32:04 +01001011static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 unsigned hw_argc;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -07001014 int ret;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001015 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Eric Biggers5916a222017-06-22 11:32:45 -07001017 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001018 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 };
1020
Mike Snitzer498f0102011-08-02 12:32:04 +01001021 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return -EINVAL;
1023
1024 if (!hw_argc)
1025 return 0;
1026
Mike Snitzer8d47e652018-03-05 14:10:11 -05001027 if (m->queue_mode == DM_TYPE_BIO_BASED) {
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001028 dm_consume_args(as, hw_argc);
1029 DMERR("bio-based multipath doesn't allow hardware handler args");
1030 return 0;
1031 }
1032
Mike Snitzer498f0102011-08-02 12:32:04 +01001033 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
tang.junhuif97dc422016-10-28 17:04:46 +08001034 if (!m->hw_handler_name)
1035 return -EINVAL;
Chandra Seetharaman14e98c52008-11-13 23:39:06 +00001036
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -07001037 if (hw_argc > 1) {
1038 char *p;
1039 int i, j, len = 4;
1040
1041 for (i = 0; i <= hw_argc - 2; i++)
1042 len += strlen(as->argv[i]) + 1;
1043 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
1044 if (!p) {
1045 ti->error = "memory allocation failed";
1046 ret = -ENOMEM;
1047 goto fail;
1048 }
1049 j = sprintf(p, "%d", hw_argc - 1);
1050 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
1051 j = sprintf(p, "%s", as->argv[i]);
1052 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001053 dm_consume_args(as, hw_argc - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 return 0;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -07001056fail:
1057 kfree(m->hw_handler_name);
1058 m->hw_handler_name = NULL;
1059 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
Mike Snitzer498f0102011-08-02 12:32:04 +01001062static int parse_features(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 int r;
1065 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001066 struct dm_target *ti = m->ti;
Mike Snitzer498f0102011-08-02 12:32:04 +01001067 const char *arg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Eric Biggers5916a222017-06-22 11:32:45 -07001069 static const struct dm_arg _args[] = {
Mike Snitzere83068a2016-05-24 21:16:51 -04001070 {0, 8, "invalid number of feature args"},
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001071 {1, 50, "pg_init_retries must be between 1 and 50"},
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001072 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 };
1074
Mike Snitzer498f0102011-08-02 12:32:04 +01001075 r = dm_read_arg_group(_args, as, &argc, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (r)
1077 return -EINVAL;
1078
1079 if (!argc)
1080 return 0;
1081
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001082 do {
Mike Snitzer498f0102011-08-02 12:32:04 +01001083 arg_name = dm_shift_arg(as);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001084 argc--;
1085
Mike Snitzer498f0102011-08-02 12:32:04 +01001086 if (!strcasecmp(arg_name, "queue_if_no_path")) {
Mike Snitzer4c3f4832020-05-29 15:59:13 -04001087 r = queue_if_no_path(m, true, false, __func__);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001088 continue;
1089 }
1090
Mike Snitzera58a9352012-07-27 15:08:04 +01001091 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
Mike Snitzer518257b2016-03-17 16:32:10 -04001092 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
Mike Snitzera58a9352012-07-27 15:08:04 +01001093 continue;
1094 }
1095
Mike Snitzer498f0102011-08-02 12:32:04 +01001096 if (!strcasecmp(arg_name, "pg_init_retries") &&
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001097 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001098 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001099 argc--;
1100 continue;
1101 }
1102
Mike Snitzer498f0102011-08-02 12:32:04 +01001103 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001104 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001105 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001106 argc--;
1107 continue;
1108 }
1109
Mike Snitzere83068a2016-05-24 21:16:51 -04001110 if (!strcasecmp(arg_name, "queue_mode") &&
1111 (argc >= 1)) {
1112 const char *queue_mode_name = dm_shift_arg(as);
1113
1114 if (!strcasecmp(queue_mode_name, "bio"))
1115 m->queue_mode = DM_TYPE_BIO_BASED;
Mike Snitzer953923c2018-10-11 11:06:29 -04001116 else if (!strcasecmp(queue_mode_name, "rq") ||
1117 !strcasecmp(queue_mode_name, "mq"))
Mike Snitzere83068a2016-05-24 21:16:51 -04001118 m->queue_mode = DM_TYPE_REQUEST_BASED;
Mike Snitzere83068a2016-05-24 21:16:51 -04001119 else {
1120 ti->error = "Unknown 'queue_mode' requested";
1121 r = -EINVAL;
1122 }
1123 argc--;
1124 continue;
1125 }
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001128 r = -EINVAL;
1129 } while (argc && !r);
1130
1131 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
Mike Snitzere83068a2016-05-24 21:16:51 -04001134static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Mike Snitzer498f0102011-08-02 12:32:04 +01001136 /* target arguments */
Eric Biggers5916a222017-06-22 11:32:45 -07001137 static const struct dm_arg _args[] = {
Mike Snitzera490a072011-03-24 13:54:33 +00001138 {0, 1024, "invalid number of priority groups"},
1139 {0, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 };
1141
1142 int r;
1143 struct multipath *m;
Mike Snitzer498f0102011-08-02 12:32:04 +01001144 struct dm_arg_set as;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 unsigned pg_count = 0;
1146 unsigned next_pg_num;
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001147 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 as.argc = argc;
1150 as.argv = argv;
1151
Mike Snitzere83068a2016-05-24 21:16:51 -04001152 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001154 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return -EINVAL;
1156 }
1157
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001158 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (r)
1160 goto bad;
1161
Mike Snitzere83068a2016-05-24 21:16:51 -04001162 r = alloc_multipath_stage2(ti, m);
1163 if (r)
1164 goto bad;
1165
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001166 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (r)
1168 goto bad;
1169
Mike Snitzer498f0102011-08-02 12:32:04 +01001170 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (r)
1172 goto bad;
1173
Mike Snitzer498f0102011-08-02 12:32:04 +01001174 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (r)
1176 goto bad;
1177
Mike Snitzera490a072011-03-24 13:54:33 +00001178 if ((!m->nr_priority_groups && next_pg_num) ||
1179 (m->nr_priority_groups && !next_pg_num)) {
1180 ti->error = "invalid initial priority group";
1181 r = -EINVAL;
1182 goto bad;
1183 }
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 /* parse the priority groups */
1186 while (as.argc) {
1187 struct priority_group *pg;
Mike Snitzer91e968a2016-03-17 17:10:15 -04001188 unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001190 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +01001191 if (IS_ERR(pg)) {
1192 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 goto bad;
1194 }
1195
Mike Snitzer91e968a2016-03-17 17:10:15 -04001196 nr_valid_paths += pg->nr_pgpaths;
1197 atomic_set(&m->nr_valid_paths, nr_valid_paths);
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 list_add_tail(&pg->list, &m->priority_groups);
1200 pg_count++;
1201 pg->pg_num = pg_count;
1202 if (!--next_pg_num)
1203 m->next_pg = pg;
1204 }
1205
1206 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001207 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 r = -EINVAL;
1209 goto bad;
1210 }
1211
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001212 spin_lock_irqsave(&m->lock, flags);
1213 enable_nopath_timeout(m);
1214 spin_unlock_irqrestore(&m->lock, flags);
1215
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001216 ti->num_flush_bios = 1;
1217 ti->num_discard_bios = 1;
Mike Snitzer042bcef2013-05-10 14:37:16 +01001218 ti->num_write_same_bios = 1;
Christoph Hellwigac62d622017-04-05 19:21:05 +02001219 ti->num_write_zeroes_bios = 1;
Mike Snitzer8d47e652018-03-05 14:10:11 -05001220 if (m->queue_mode == DM_TYPE_BIO_BASED)
Mike Snitzerbf661be2016-05-24 15:48:08 -04001221 ti->per_io_data_size = multipath_per_bio_data_size();
Christoph Hellwigeb8db832017-01-22 18:32:46 +01001222 else
Mike Snitzer8637a6b2016-01-31 12:08:36 -05001223 ti->per_io_data_size = sizeof(struct dm_mpath_io);
Mikulas Patocka86279212009-06-22 10:12:24 +01001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return 0;
1226
1227 bad:
1228 free_multipath(m);
1229 return r;
1230}
1231
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001232static void multipath_wait_for_pg_init_completion(struct multipath *m)
1233{
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001234 DEFINE_WAIT(wait);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001235
1236 while (1) {
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001237 prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001238
Mike Snitzer91e968a2016-03-17 17:10:15 -04001239 if (!atomic_read(&m->pg_init_in_progress))
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001240 break;
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001241
1242 io_schedule();
1243 }
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001244 finish_wait(&m->pg_init_wait, &wait);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001245}
1246
1247static void flush_multipath_work(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
Mike Snitzer848b8ae2017-12-10 15:37:21 -05001249 if (m->hw_handler_name) {
1250 set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1251 smp_mb__after_atomic();
Shiva Krishna Merla954a73d2013-10-30 03:26:38 +00001252
wuzhouhui935fcc52018-03-22 22:22:38 +08001253 if (atomic_read(&m->pg_init_in_progress))
1254 flush_workqueue(kmpath_handlerd);
Mike Snitzer848b8ae2017-12-10 15:37:21 -05001255 multipath_wait_for_pg_init_completion(m);
1256
1257 clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1258 smp_mb__after_atomic();
1259 }
1260
wuzhouhui935fcc52018-03-22 22:22:38 +08001261 if (m->queue_mode == DM_TYPE_BIO_BASED)
1262 flush_work(&m->process_queued_bios);
Tejun Heo43829732012-08-20 14:51:24 -07001263 flush_work(&m->trigger_event);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001264}
1265
1266static void multipath_dtr(struct dm_target *ti)
1267{
1268 struct multipath *m = ti->private;
1269
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001270 disable_nopath_timeout(m);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001271 flush_multipath_work(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 free_multipath(m);
1273}
1274
1275/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 * Take a path out of use.
1277 */
1278static int fail_path(struct pgpath *pgpath)
1279{
1280 unsigned long flags;
1281 struct multipath *m = pgpath->pg->m;
1282
1283 spin_lock_irqsave(&m->lock, flags);
1284
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001285 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 goto out;
1287
Mike Snitzer04867372020-06-04 10:44:34 -04001288 DMWARN("%s: Failing path %s.",
1289 dm_device_name(dm_table_get_md(m->ti->table)),
1290 pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001293 pgpath->is_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 pgpath->fail_count++;
1295
Mike Snitzer91e968a2016-03-17 17:10:15 -04001296 atomic_dec(&m->nr_valid_paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 if (pgpath == m->current_pgpath)
1299 m->current_pgpath = NULL;
1300
Mike Andersonb15546f2007-10-19 22:48:02 +01001301 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
Mike Snitzer91e968a2016-03-17 17:10:15 -04001302 pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
Mike Andersonb15546f2007-10-19 22:48:02 +01001303
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001304 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001306 enable_nopath_timeout(m);
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308out:
1309 spin_unlock_irqrestore(&m->lock, flags);
1310
1311 return 0;
1312}
1313
1314/*
1315 * Reinstate a previously-failed path
1316 */
1317static int reinstate_path(struct pgpath *pgpath)
1318{
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001319 int r = 0, run_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 unsigned long flags;
1321 struct multipath *m = pgpath->pg->m;
Mike Snitzer91e968a2016-03-17 17:10:15 -04001322 unsigned nr_valid_paths;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 spin_lock_irqsave(&m->lock, flags);
1325
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001326 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 goto out;
1328
Mike Snitzer04867372020-06-04 10:44:34 -04001329 DMWARN("%s: Reinstating path %s.",
1330 dm_device_name(dm_table_get_md(m->ti->table)),
1331 pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1334 if (r)
1335 goto out;
1336
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001337 pgpath->is_active = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Mike Snitzer91e968a2016-03-17 17:10:15 -04001339 nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
1340 if (nr_valid_paths == 1) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001341 m->current_pgpath = NULL;
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001342 run_queue = 1;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001343 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001344 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
Mike Snitzer91e968a2016-03-17 17:10:15 -04001345 atomic_inc(&m->pg_init_in_progress);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Mike Andersonb15546f2007-10-19 22:48:02 +01001348 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
Mike Snitzer91e968a2016-03-17 17:10:15 -04001349 pgpath->path.dev->name, nr_valid_paths);
Mike Andersonb15546f2007-10-19 22:48:02 +01001350
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001351 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353out:
1354 spin_unlock_irqrestore(&m->lock, flags);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001355 if (run_queue) {
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001356 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -04001357 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001360 if (pgpath->is_active)
1361 disable_nopath_timeout(m);
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return r;
1364}
1365
1366/*
1367 * Fail or reinstate all paths that match the provided struct dm_dev.
1368 */
1369static int action_dev(struct multipath *m, struct dm_dev *dev,
1370 action_fn action)
1371{
Mike Snitzer19040c02011-03-24 13:54:31 +00001372 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 struct pgpath *pgpath;
1374 struct priority_group *pg;
1375
1376 list_for_each_entry(pg, &m->priority_groups, list) {
1377 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1378 if (pgpath->path.dev == dev)
1379 r = action(pgpath);
1380 }
1381 }
1382
1383 return r;
1384}
1385
1386/*
1387 * Temporarily try to avoid having to use the specified PG
1388 */
1389static void bypass_pg(struct multipath *m, struct priority_group *pg,
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001390 bool bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 unsigned long flags;
1393
1394 spin_lock_irqsave(&m->lock, flags);
1395
1396 pg->bypassed = bypassed;
1397 m->current_pgpath = NULL;
1398 m->current_pg = NULL;
1399
1400 spin_unlock_irqrestore(&m->lock, flags);
1401
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001402 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
1405/*
1406 * Switch to using the specified PG from the next I/O that gets mapped
1407 */
1408static int switch_pg_num(struct multipath *m, const char *pgstr)
1409{
1410 struct priority_group *pg;
1411 unsigned pgnum;
1412 unsigned long flags;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001413 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001415 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
tang.junhuicc5bd922016-11-04 12:37:09 +08001416 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 DMWARN("invalid PG number supplied to switch_pg_num");
1418 return -EINVAL;
1419 }
1420
1421 spin_lock_irqsave(&m->lock, flags);
1422 list_for_each_entry(pg, &m->priority_groups, list) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001423 pg->bypassed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (--pgnum)
1425 continue;
1426
1427 m->current_pgpath = NULL;
1428 m->current_pg = NULL;
1429 m->next_pg = pg;
1430 }
1431 spin_unlock_irqrestore(&m->lock, flags);
1432
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001433 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return 0;
1435}
1436
1437/*
1438 * Set/clear bypassed status of a PG.
1439 * PGs are numbered upwards from 1 in the order they were declared.
1440 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001441static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 struct priority_group *pg;
1444 unsigned pgnum;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001445 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001447 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
tang.junhuicc5bd922016-11-04 12:37:09 +08001448 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 DMWARN("invalid PG number supplied to bypass_pg");
1450 return -EINVAL;
1451 }
1452
1453 list_for_each_entry(pg, &m->priority_groups, list) {
1454 if (!--pgnum)
1455 break;
1456 }
1457
1458 bypass_pg(m, pg, bypassed);
1459 return 0;
1460}
1461
1462/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001463 * Should we retry pg_init immediately?
1464 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001465static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001466{
1467 unsigned long flags;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001468 bool limit_reached = false;
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001469
1470 spin_lock_irqsave(&m->lock, flags);
1471
Mike Snitzer91e968a2016-03-17 17:10:15 -04001472 if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
1473 !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
Mike Snitzer518257b2016-03-17 16:32:10 -04001474 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001475 else
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001476 limit_reached = true;
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001477
1478 spin_unlock_irqrestore(&m->lock, flags);
1479
1480 return limit_reached;
1481}
1482
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001483static void pg_init_done(void *data, int errors)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001484{
Moger, Babu83c0d5d2010-03-06 02:29:45 +00001485 struct pgpath *pgpath = data;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001486 struct priority_group *pg = pgpath->pg;
1487 struct multipath *m = pg->m;
1488 unsigned long flags;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001489 bool delay_retry = false;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001490
1491 /* device or driver problems */
1492 switch (errors) {
1493 case SCSI_DH_OK:
1494 break;
1495 case SCSI_DH_NOSYS:
1496 if (!m->hw_handler_name) {
1497 errors = 0;
1498 break;
1499 }
Moger, Babuf7b934c2010-03-06 02:29:49 +00001500 DMERR("Could not failover the device: Handler scsi_dh_%s "
1501 "Error %d.", m->hw_handler_name, errors);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001502 /*
1503 * Fail path for now, so we do not ping pong
1504 */
1505 fail_path(pgpath);
1506 break;
1507 case SCSI_DH_DEV_TEMP_BUSY:
1508 /*
1509 * Probably doing something like FW upgrade on the
1510 * controller so try the other pg.
1511 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001512 bypass_pg(m, pg, true);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001513 break;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001514 case SCSI_DH_RETRY:
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001515 /* Wait before retrying. */
zhengbin4ecc5082019-12-24 14:38:00 +08001516 delay_retry = true;
Bart Van Assche7b06e092017-08-09 11:32:13 -07001517 /* fall through */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001518 case SCSI_DH_IMM_RETRY:
1519 case SCSI_DH_RES_TEMP_UNAVAIL:
1520 if (pg_init_limit_reached(m, pgpath))
1521 fail_path(pgpath);
1522 errors = 0;
1523 break;
Mike Snitzerec31f3f2016-02-20 12:49:43 -05001524 case SCSI_DH_DEV_OFFLINED:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001525 default:
1526 /*
1527 * We probably do not want to fail the path for a device
1528 * error, but this is what the old dm did. In future
1529 * patches we can do more advanced handling.
1530 */
1531 fail_path(pgpath);
1532 }
1533
1534 spin_lock_irqsave(&m->lock, flags);
1535 if (errors) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001536 if (pgpath == m->current_pgpath) {
1537 DMERR("Could not failover device. Error %d.", errors);
1538 m->current_pgpath = NULL;
1539 m->current_pg = NULL;
1540 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001541 } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001542 pg->bypassed = false;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001543
Mike Snitzer91e968a2016-03-17 17:10:15 -04001544 if (atomic_dec_return(&m->pg_init_in_progress) > 0)
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001545 /* Activations of other paths are still on going */
1546 goto out;
1547
Mike Snitzer518257b2016-03-17 16:32:10 -04001548 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1549 if (delay_retry)
1550 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1551 else
1552 clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1553
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001554 if (__pg_init_all_paths(m))
1555 goto out;
1556 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001557 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001558
Mike Snitzer7e48c762016-09-14 10:47:03 -04001559 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001560
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001561 /*
1562 * Wake up any thread waiting to suspend.
1563 */
1564 wake_up(&m->pg_init_wait);
1565
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001566out:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001567 spin_unlock_irqrestore(&m->lock, flags);
1568}
1569
Bart Van Assche89bfce72017-04-27 10:11:14 -07001570static void activate_or_offline_path(struct pgpath *pgpath)
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001571{
Mike Snitzerf10e06b2016-09-01 12:06:37 -04001572 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001573
Mike Snitzerf10e06b2016-09-01 12:06:37 -04001574 if (pgpath->is_active && !blk_queue_dying(q))
1575 scsi_dh_activate(q, pg_init_done, pgpath);
Hannes Reinecke3a017502014-02-28 15:33:49 +01001576 else
1577 pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001578}
1579
Bart Van Assche89bfce72017-04-27 10:11:14 -07001580static void activate_path_work(struct work_struct *work)
1581{
1582 struct pgpath *pgpath =
1583 container_of(work, struct pgpath, activate_path.work);
1584
1585 activate_or_offline_path(pgpath);
1586}
1587
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001588static int multipath_end_io(struct dm_target *ti, struct request *clone,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001589 blk_status_t error, union map_info *map_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001591 struct dm_mpath_io *mpio = get_mpio(map_context);
1592 struct pgpath *pgpath = mpio->pgpath;
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001593 int r = DM_ENDIO_DONE;
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001594
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001595 /*
1596 * We don't queue any clone request inside the multipath target
1597 * during end I/O handling, since those clone requests don't have
1598 * bio clones. If we queue them inside the multipath target,
1599 * we need to make bio clones, that requires memory allocation.
Mike Snitzer4cc96132016-05-12 16:28:10 -04001600 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001601 * don't have bio clones.)
1602 * Instead of queueing the clone request here, we queue the original
1603 * request into dm core, which will remake a clone request and
1604 * clone bios for it and resubmit it later.
1605 */
Keith Buscha1275672018-01-09 12:04:18 -07001606 if (error && blk_path_error(error)) {
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001607 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Mike Snitzerac514ff2018-01-12 19:53:40 -05001609 if (error == BLK_STS_RESOURCE)
1610 r = DM_ENDIO_DELAY_REQUEUE;
1611 else
1612 r = DM_ENDIO_REQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001614 if (pgpath)
1615 fail_path(pgpath);
Mike Snitzer959eb4e2010-08-12 04:14:32 +01001616
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001617 if (atomic_read(&m->nr_valid_paths) == 0 &&
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -05001618 !must_push_back_rq(m)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001619 if (error == BLK_STS_IOERR)
Christoph Hellwig18a482f2017-05-15 17:28:37 +02001620 dm_report_EIO(m);
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001621 /* complete with the original error */
1622 r = DM_ENDIO_DONE;
1623 }
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (pgpath) {
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001627 struct path_selector *ps = &pgpath->pg->ps;
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (ps->type->end_io)
Gabriel Krisman Bertazi087615bf2020-04-30 16:48:29 -04001630 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes,
1631 clone->io_start_time_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001634 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001637static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone,
Mike Snitzer848b8ae2017-12-10 15:37:21 -05001638 blk_status_t *error)
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001639{
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001640 struct multipath *m = ti->private;
1641 struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
1642 struct pgpath *pgpath = mpio->pgpath;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001643 unsigned long flags;
Christoph Hellwig1be56902017-06-03 09:38:03 +02001644 int r = DM_ENDIO_DONE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001645
Keith Buscha1275672018-01-09 12:04:18 -07001646 if (!*error || !blk_path_error(*error))
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001647 goto done;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001648
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001649 if (pgpath)
1650 fail_path(pgpath);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001651
Bart Van Asscheca5beb72017-04-27 10:11:24 -07001652 if (atomic_read(&m->nr_valid_paths) == 0 &&
Christoph Hellwig18a482f2017-05-15 17:28:37 +02001653 !test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
Mike Snitzera862e4e2020-05-26 16:06:56 -04001654 if (__must_push_back(m)) {
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -05001655 r = DM_ENDIO_REQUEUE;
1656 } else {
1657 dm_report_EIO(m);
1658 *error = BLK_STS_IOERR;
1659 }
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001660 goto done;
Christoph Hellwig18a482f2017-05-15 17:28:37 +02001661 }
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001662
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001663 spin_lock_irqsave(&m->lock, flags);
1664 bio_list_add(&m->queued_bios, clone);
1665 spin_unlock_irqrestore(&m->lock, flags);
1666 if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
1667 queue_work(kmultipathd, &m->process_queued_bios);
1668
Christoph Hellwig1be56902017-06-03 09:38:03 +02001669 r = DM_ENDIO_INCOMPLETE;
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001670done:
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001671 if (pgpath) {
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001672 struct path_selector *ps = &pgpath->pg->ps;
1673
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001674 if (ps->type->end_io)
Gabriel Krisman Bertazi087615bf2020-04-30 16:48:29 -04001675 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes,
1676 dm_start_time_ns_from_clone(clone));
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001677 }
1678
Christoph Hellwig1be56902017-06-03 09:38:03 +02001679 return r;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001680}
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682/*
Mike Snitzer553ec942020-05-27 16:32:51 -04001683 * Suspend with flush can't complete until all the I/O is processed
1684 * so if the last path fails we must error any remaining I/O.
1685 * - Note that if the freeze_bdev fails while suspending, the
1686 * queue_if_no_path state is lost - userspace should reset it.
1687 * Otherwise, during noflush suspend, queue_if_no_path will not change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 */
1689static void multipath_presuspend(struct dm_target *ti)
1690{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001691 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Mike Snitzer553ec942020-05-27 16:32:51 -04001693 /* FIXME: bio-based shouldn't need to always disable queue_if_no_path */
1694 if (m->queue_mode == DM_TYPE_BIO_BASED || !dm_noflush_suspending(m->ti))
Mike Snitzer4c3f4832020-05-29 15:59:13 -04001695 queue_if_no_path(m, false, true, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001698static void multipath_postsuspend(struct dm_target *ti)
1699{
Mike Anderson6380f262009-12-10 23:52:21 +00001700 struct multipath *m = ti->private;
1701
1702 mutex_lock(&m->work_mutex);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001703 flush_multipath_work(m);
Mike Anderson6380f262009-12-10 23:52:21 +00001704 mutex_unlock(&m->work_mutex);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001705}
1706
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001707/*
1708 * Restore the queue_if_no_path setting.
1709 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710static void multipath_resume(struct dm_target *ti)
1711{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001712 struct multipath *m = ti->private;
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001713 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001715 spin_lock_irqsave(&m->lock, flags);
Mike Snitzer553ec942020-05-27 16:32:51 -04001716 if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) {
1717 set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
1718 clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
1719 }
Mike Snitzer4c3f4832020-05-29 15:59:13 -04001720
1721 DMDEBUG("%s: %s finished; QIFNP = %d; SQIFNP = %d",
1722 dm_device_name(dm_table_get_md(m->ti->table)), __func__,
1723 test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags),
1724 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags));
1725
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001726 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729/*
1730 * Info output has the following format:
1731 * num_multipath_feature_args [multipath_feature_args]*
1732 * num_handler_status_args [handler_status_args]*
1733 * num_groups init_group_number
1734 * [A|D|E num_ps_status_args [ps_status_args]*
1735 * num_paths num_selector_args
1736 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1737 *
1738 * Table output has the following format (identical to the constructor string):
1739 * num_feature_args [features_args]*
1740 * num_handler_args hw_handler [hw_handler_args]*
1741 * num_groups init_group_number
1742 * [priority selector-name num_ps_args [ps_args]*
1743 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1744 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001745static void multipath_status(struct dm_target *ti, status_type_t type,
1746 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747{
1748 int sz = 0;
1749 unsigned long flags;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001750 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 struct priority_group *pg;
1752 struct pgpath *p;
1753 unsigned pg_num;
1754 char state;
1755
1756 spin_lock_irqsave(&m->lock, flags);
1757
1758 /* Features */
1759 if (type == STATUSTYPE_INFO)
Mike Snitzer91e968a2016-03-17 17:10:15 -04001760 DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
1761 atomic_read(&m->pg_init_count));
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001762 else {
Mike Snitzer518257b2016-03-17 16:32:10 -04001763 DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001764 (m->pg_init_retries > 0) * 2 +
Mike Snitzera58a9352012-07-27 15:08:04 +01001765 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
Mike Snitzere83068a2016-05-24 21:16:51 -04001766 test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
1767 (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
1768
Mike Snitzer518257b2016-03-17 16:32:10 -04001769 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001770 DMEMIT("queue_if_no_path ");
1771 if (m->pg_init_retries)
1772 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001773 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1774 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
Mike Snitzer518257b2016-03-17 16:32:10 -04001775 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
Mike Snitzera58a9352012-07-27 15:08:04 +01001776 DMEMIT("retain_attached_hw_handler ");
Mike Snitzere83068a2016-05-24 21:16:51 -04001777 if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
1778 switch(m->queue_mode) {
1779 case DM_TYPE_BIO_BASED:
1780 DMEMIT("queue_mode bio ");
1781 break;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07001782 default:
1783 WARN_ON_ONCE(true);
1784 break;
Mike Snitzere83068a2016-05-24 21:16:51 -04001785 }
1786 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001789 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 DMEMIT("0 ");
1791 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001792 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 DMEMIT("%u ", m->nr_priority_groups);
1795
1796 if (m->next_pg)
1797 pg_num = m->next_pg->pg_num;
1798 else if (m->current_pg)
1799 pg_num = m->current_pg->pg_num;
1800 else
Mike Snitzera490a072011-03-24 13:54:33 +00001801 pg_num = (m->nr_priority_groups ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 DMEMIT("%u ", pg_num);
1804
1805 switch (type) {
1806 case STATUSTYPE_INFO:
1807 list_for_each_entry(pg, &m->priority_groups, list) {
1808 if (pg->bypassed)
1809 state = 'D'; /* Disabled */
1810 else if (pg == m->current_pg)
1811 state = 'A'; /* Currently Active */
1812 else
1813 state = 'E'; /* Enabled */
1814
1815 DMEMIT("%c ", state);
1816
1817 if (pg->ps.type->status)
1818 sz += pg->ps.type->status(&pg->ps, NULL, type,
1819 result + sz,
1820 maxlen - sz);
1821 else
1822 DMEMIT("0 ");
1823
1824 DMEMIT("%u %u ", pg->nr_pgpaths,
1825 pg->ps.type->info_args);
1826
1827 list_for_each_entry(p, &pg->pgpaths, list) {
1828 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001829 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 p->fail_count);
1831 if (pg->ps.type->status)
1832 sz += pg->ps.type->status(&pg->ps,
1833 &p->path, type, result + sz,
1834 maxlen - sz);
1835 }
1836 }
1837 break;
1838
1839 case STATUSTYPE_TABLE:
1840 list_for_each_entry(pg, &m->priority_groups, list) {
1841 DMEMIT("%s ", pg->ps.type->name);
1842
1843 if (pg->ps.type->status)
1844 sz += pg->ps.type->status(&pg->ps, NULL, type,
1845 result + sz,
1846 maxlen - sz);
1847 else
1848 DMEMIT("0 ");
1849
1850 DMEMIT("%u %u ", pg->nr_pgpaths,
1851 pg->ps.type->table_args);
1852
1853 list_for_each_entry(p, &pg->pgpaths, list) {
1854 DMEMIT("%s ", p->path.dev->name);
1855 if (pg->ps.type->status)
1856 sz += pg->ps.type->status(&pg->ps,
1857 &p->path, type, result + sz,
1858 maxlen - sz);
1859 }
1860 }
1861 break;
1862 }
1863
1864 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Mike Snitzer1eb5fa82018-02-28 15:59:59 -05001867static int multipath_message(struct dm_target *ti, unsigned argc, char **argv,
1868 char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Mike Anderson6380f262009-12-10 23:52:21 +00001870 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 struct dm_dev *dev;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001872 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 action_fn action;
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001874 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Mike Anderson6380f262009-12-10 23:52:21 +00001876 mutex_lock(&m->work_mutex);
1877
Kiyoshi Uedac2f3d242009-12-10 23:52:27 +00001878 if (dm_suspended(ti)) {
1879 r = -EBUSY;
1880 goto out;
1881 }
1882
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (argc == 1) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001884 if (!strcasecmp(argv[0], "queue_if_no_path")) {
Mike Snitzer4c3f4832020-05-29 15:59:13 -04001885 r = queue_if_no_path(m, true, false, __func__);
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001886 spin_lock_irqsave(&m->lock, flags);
1887 enable_nopath_timeout(m);
1888 spin_unlock_irqrestore(&m->lock, flags);
Mike Anderson6380f262009-12-10 23:52:21 +00001889 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001890 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
Mike Snitzer4c3f4832020-05-29 15:59:13 -04001891 r = queue_if_no_path(m, false, false, __func__);
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001892 disable_nopath_timeout(m);
Mike Anderson6380f262009-12-10 23:52:21 +00001893 goto out;
1894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896
Mike Anderson6380f262009-12-10 23:52:21 +00001897 if (argc != 2) {
Jose Castilloa356e422014-01-29 17:52:45 +01001898 DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
Mike Anderson6380f262009-12-10 23:52:21 +00001899 goto out;
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Mike Snitzer498f0102011-08-02 12:32:04 +01001902 if (!strcasecmp(argv[0], "disable_group")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001903 r = bypass_pg_num(m, argv[1], true);
Mike Anderson6380f262009-12-10 23:52:21 +00001904 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001905 } else if (!strcasecmp(argv[0], "enable_group")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001906 r = bypass_pg_num(m, argv[1], false);
Mike Anderson6380f262009-12-10 23:52:21 +00001907 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001908 } else if (!strcasecmp(argv[0], "switch_group")) {
Mike Anderson6380f262009-12-10 23:52:21 +00001909 r = switch_pg_num(m, argv[1]);
1910 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001911 } else if (!strcasecmp(argv[0], "reinstate_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 action = reinstate_path;
Mike Snitzer498f0102011-08-02 12:32:04 +01001913 else if (!strcasecmp(argv[0], "fail_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 action = fail_path;
Mike Anderson6380f262009-12-10 23:52:21 +00001915 else {
Jose Castilloa356e422014-01-29 17:52:45 +01001916 DMWARN("Unrecognised multipath message received: %s", argv[0]);
Mike Anderson6380f262009-12-10 23:52:21 +00001917 goto out;
1918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00001920 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001922 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 argv[1]);
Mike Anderson6380f262009-12-10 23:52:21 +00001924 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926
1927 r = action_dev(m, dev, action);
1928
1929 dm_put_device(ti, dev);
1930
Mike Anderson6380f262009-12-10 23:52:21 +00001931out:
1932 mutex_unlock(&m->work_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934}
1935
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001936static int multipath_prepare_ioctl(struct dm_target *ti,
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -04001937 struct block_device **bdev)
Milan Broz9af4aa32006-10-03 01:15:20 -07001938{
Mikulas Patocka35991652012-06-03 00:29:58 +01001939 struct multipath *m = ti->private;
Mike Snitzer2da16102016-03-17 18:38:17 -04001940 struct pgpath *current_pgpath;
Mikulas Patocka35991652012-06-03 00:29:58 +01001941 int r;
1942
Will Deacon506458e2017-10-24 11:22:48 +01001943 current_pgpath = READ_ONCE(m->current_pgpath);
Martin Wilck2361ae52020-04-20 22:29:09 +02001944 if (!current_pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
Mike Snitzer2da16102016-03-17 18:38:17 -04001945 current_pgpath = choose_pgpath(m, 0);
Milan Broz9af4aa32006-10-03 01:15:20 -07001946
Mike Snitzer2da16102016-03-17 18:38:17 -04001947 if (current_pgpath) {
Mike Snitzer518257b2016-03-17 16:32:10 -04001948 if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
Mike Snitzer2da16102016-03-17 18:38:17 -04001949 *bdev = current_pgpath->path.dev->bdev;
Junichi Nomura43e43c92015-11-17 09:36:56 +00001950 r = 0;
1951 } else {
1952 /* pg_init has not started or completed */
1953 r = -ENOTCONN;
1954 }
1955 } else {
1956 /* No path is available */
Mike Snitzer518257b2016-03-17 16:32:10 -04001957 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Junichi Nomura43e43c92015-11-17 09:36:56 +00001958 r = -ENOTCONN;
1959 else
1960 r = -EIO;
Milan Broze90dae12006-10-03 01:15:22 -07001961 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001962
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +00001963 if (r == -ENOTCONN) {
Will Deacon506458e2017-10-24 11:22:48 +01001964 if (!READ_ONCE(m->current_pg)) {
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001965 /* Path status changed, redo selection */
Mike Snitzer2da16102016-03-17 18:38:17 -04001966 (void) choose_pgpath(m, 0);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001967 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001968 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
Mike Snitzer2da16102016-03-17 18:38:17 -04001969 pg_init_all_paths(m);
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001970 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -04001971 process_queued_io_list(m);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001972 }
Mikulas Patocka35991652012-06-03 00:29:58 +01001973
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001974 /*
1975 * Only pass ioctls through if the device sizes match exactly.
1976 */
1977 if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1978 return 1;
1979 return r;
Milan Broz9af4aa32006-10-03 01:15:20 -07001980}
1981
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001982static int multipath_iterate_devices(struct dm_target *ti,
1983 iterate_devices_callout_fn fn, void *data)
1984{
1985 struct multipath *m = ti->private;
1986 struct priority_group *pg;
1987 struct pgpath *p;
1988 int ret = 0;
1989
1990 list_for_each_entry(pg, &m->priority_groups, list) {
1991 list_for_each_entry(p, &pg->pgpaths, list) {
Mike Snitzer5dea2712009-07-23 20:30:42 +01001992 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001993 if (ret)
1994 goto out;
1995 }
1996 }
1997
1998out:
1999 return ret;
2000}
2001
Mike Snitzer9f54cec2016-02-11 21:42:28 -05002002static int pgpath_busy(struct pgpath *pgpath)
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002003{
2004 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
2005
Mike Snitzer52b09912015-02-23 16:36:41 -05002006 return blk_lld_busy(q);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002007}
2008
2009/*
2010 * We return "busy", only when we can map I/Os but underlying devices
2011 * are busy (so even if we map I/Os now, the I/Os will wait on
2012 * the underlying queue).
2013 * In other words, if we want to kill I/Os or queue them inside us
2014 * due to map unavailability, we don't return "busy". Otherwise,
2015 * dm core won't give us the I/Os and we can't do what we want.
2016 */
2017static int multipath_busy(struct dm_target *ti)
2018{
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002019 bool busy = false, has_active = false;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002020 struct multipath *m = ti->private;
Mike Snitzer2da16102016-03-17 18:38:17 -04002021 struct priority_group *pg, *next_pg;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002022 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002023
Mike Snitzerb88efd42016-09-09 19:26:19 -04002024 /* pg_init in progress */
2025 if (atomic_read(&m->pg_init_in_progress))
Mike Snitzer2da16102016-03-17 18:38:17 -04002026 return true;
2027
Mike Snitzerb88efd42016-09-09 19:26:19 -04002028 /* no paths available, for blk-mq: rely on IO mapping to delay requeue */
2029 if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Mike Snitzer953923c2018-10-11 11:06:29 -04002030 return (m->queue_mode != DM_TYPE_REQUEST_BASED);
Mike Snitzerb88efd42016-09-09 19:26:19 -04002031
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002032 /* Guess which priority_group will be used at next mapping time */
Will Deacon506458e2017-10-24 11:22:48 +01002033 pg = READ_ONCE(m->current_pg);
2034 next_pg = READ_ONCE(m->next_pg);
2035 if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg))
Mike Snitzer2da16102016-03-17 18:38:17 -04002036 pg = next_pg;
2037
2038 if (!pg) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002039 /*
2040 * We don't know which pg will be used at next mapping time.
Mike Snitzer2da16102016-03-17 18:38:17 -04002041 * We don't call choose_pgpath() here to avoid to trigger
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002042 * pg_init just by busy checking.
2043 * So we don't know whether underlying devices we will be using
2044 * at next mapping time are busy or not. Just try mapping.
2045 */
Mike Snitzer2da16102016-03-17 18:38:17 -04002046 return busy;
2047 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002048
2049 /*
2050 * If there is one non-busy active path at least, the path selector
2051 * will be able to select it. So we consider such a pg as not busy.
2052 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002053 busy = true;
Mike Snitzer2da16102016-03-17 18:38:17 -04002054 list_for_each_entry(pgpath, &pg->pgpaths, list) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002055 if (pgpath->is_active) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002056 has_active = true;
Mike Snitzer9f54cec2016-02-11 21:42:28 -05002057 if (!pgpath_busy(pgpath)) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002058 busy = false;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002059 break;
2060 }
2061 }
Mike Snitzer2da16102016-03-17 18:38:17 -04002062 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002063
Mike Snitzer2da16102016-03-17 18:38:17 -04002064 if (!has_active) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002065 /*
2066 * No active path in this pg, so this pg won't be used and
2067 * the current_pg will be changed at next mapping time.
2068 * We need to try mapping to determine it.
2069 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002070 busy = false;
Mike Snitzer2da16102016-03-17 18:38:17 -04002071 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002072
2073 return busy;
2074}
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076/*-----------------------------------------------------------------
2077 * Module setup
2078 *---------------------------------------------------------------*/
2079static struct target_type multipath_target = {
2080 .name = "multipath",
Mike Snitzer636be422020-02-27 14:25:31 -05002081 .version = {1, 14, 0},
Steffen Maier8c5c1472018-03-14 15:33:06 +01002082 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE |
2083 DM_TARGET_PASSES_INTEGRITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 .module = THIS_MODULE,
2085 .ctr = multipath_ctr,
2086 .dtr = multipath_dtr,
Mike Snitzere5863d92014-12-17 21:08:12 -05002087 .clone_and_map_rq = multipath_clone_and_map,
2088 .release_clone_rq = multipath_release_clone,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002089 .rq_end_io = multipath_end_io,
Mike Snitzer76e33fe2016-05-19 16:15:14 -04002090 .map = multipath_map_bio,
2091 .end_io = multipath_end_io_bio,
2092 .presuspend = multipath_presuspend,
2093 .postsuspend = multipath_postsuspend,
2094 .resume = multipath_resume,
2095 .status = multipath_status,
2096 .message = multipath_message,
2097 .prepare_ioctl = multipath_prepare_ioctl,
2098 .iterate_devices = multipath_iterate_devices,
2099 .busy = multipath_busy,
2100};
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102static int __init dm_multipath_init(void)
2103{
2104 int r;
2105
Tejun Heo4d4d66a2011-01-13 19:59:57 +00002106 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002107 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01002108 DMERR("failed to create workqueue kmpathd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002109 r = -ENOMEM;
2110 goto bad_alloc_kmultipathd;
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002111 }
2112
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002113 /*
2114 * A separate workqueue is used to handle the device handlers
2115 * to avoid overloading existing workqueue. Overloading the
2116 * old workqueue would also create a bottleneck in the
2117 * path of the storage hardware device activation.
2118 */
Tejun Heo4d4d66a2011-01-13 19:59:57 +00002119 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
2120 WQ_MEM_RECLAIM);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002121 if (!kmpath_handlerd) {
2122 DMERR("failed to create workqueue kmpath_handlerd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002123 r = -ENOMEM;
2124 goto bad_alloc_kmpath_handlerd;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002125 }
2126
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002127 r = dm_register_target(&multipath_target);
2128 if (r < 0) {
2129 DMERR("request-based register failed %d", r);
2130 r = -EINVAL;
2131 goto bad_register_target;
2132 }
2133
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002134 return 0;
2135
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002136bad_register_target:
2137 destroy_workqueue(kmpath_handlerd);
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002138bad_alloc_kmpath_handlerd:
2139 destroy_workqueue(kmultipathd);
2140bad_alloc_kmultipathd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return r;
2142}
2143
2144static void __exit dm_multipath_exit(void)
2145{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002146 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002147 destroy_workqueue(kmultipathd);
2148
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002149 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152module_init(dm_multipath_init);
2153module_exit(dm_multipath_exit);
2154
Anatol Pomazaube240ff2020-01-13 17:41:27 -05002155module_param_named(queue_if_no_path_timeout_secs,
2156 queue_if_no_path_timeout_secs, ulong, S_IRUGO | S_IWUSR);
2157MODULE_PARM_DESC(queue_if_no_path_timeout_secs, "No available paths queue IO timeout in seconds");
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159MODULE_DESCRIPTION(DM_NAME " multipath target");
2160MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
2161MODULE_LICENSE("GPL");