blob: 4b48183c6b5c48feb35c5a9fb19dab4c2ee2d56b [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{
Mike Snitzer69cea0d2020-06-10 15:02:37 -0400338 lockdep_assert_held(&m->lock);
339
Mike Snitzer2da16102016-03-17 18:38:17 -0400340 m->current_pg = pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700343 if (m->hw_handler_name) {
Mike Snitzer518257b2016-03-17 16:32:10 -0400344 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
345 set_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 } else {
Mike Snitzer518257b2016-03-17 16:32:10 -0400347 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
348 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100350
Mike Snitzer91e968a2016-03-17 17:10:15 -0400351 atomic_set(&m->pg_init_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Mike Snitzer2da16102016-03-17 18:38:17 -0400354static struct pgpath *choose_path_in_pg(struct multipath *m,
355 struct priority_group *pg,
356 size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Mike Snitzer2da16102016-03-17 18:38:17 -0400358 unsigned long flags;
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800359 struct dm_path *path;
Mike Snitzer2da16102016-03-17 18:38:17 -0400360 struct pgpath *pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Mike Snitzer90a43232016-02-17 21:29:17 -0500362 path = pg->ps.type->select_path(&pg->ps, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (!path)
Mike Snitzer2da16102016-03-17 18:38:17 -0400364 return ERR_PTR(-ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Mike Snitzer2da16102016-03-17 18:38:17 -0400366 pgpath = path_to_pgpath(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Will Deacon506458e2017-10-24 11:22:48 +0100368 if (unlikely(READ_ONCE(m->current_pg) != pg)) {
Mike Snitzer2da16102016-03-17 18:38:17 -0400369 /* Only update current_pgpath if pg changed */
370 spin_lock_irqsave(&m->lock, flags);
371 m->current_pgpath = pgpath;
372 __switch_pg(m, pg);
373 spin_unlock_irqrestore(&m->lock, flags);
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Mike Snitzer2da16102016-03-17 18:38:17 -0400376 return pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Mike Snitzer2da16102016-03-17 18:38:17 -0400379static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Mike Snitzer2da16102016-03-17 18:38:17 -0400381 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct priority_group *pg;
Mike Snitzer2da16102016-03-17 18:38:17 -0400383 struct pgpath *pgpath;
Mike Snitzerd19a55c2017-01-06 15:33:14 -0500384 unsigned bypassed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Mike Snitzer91e968a2016-03-17 17:10:15 -0400386 if (!atomic_read(&m->nr_valid_paths)) {
Mike Snitzer69cea0d2020-06-10 15:02:37 -0400387 spin_lock_irqsave(&m->lock, flags);
Mike Snitzer8d47e652018-03-05 14:10:11 -0500388 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Mike Snitzer69cea0d2020-06-10 15:02:37 -0400389 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 goto failed;
Benjamin Marzinski1f271972014-08-13 13:53:42 -0500391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 /* Were we instructed to switch PG? */
Will Deacon506458e2017-10-24 11:22:48 +0100394 if (READ_ONCE(m->next_pg)) {
Mike Snitzer2da16102016-03-17 18:38:17 -0400395 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 pg = m->next_pg;
Mike Snitzer2da16102016-03-17 18:38:17 -0400397 if (!pg) {
398 spin_unlock_irqrestore(&m->lock, flags);
399 goto check_current_pg;
400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 m->next_pg = NULL;
Mike Snitzer2da16102016-03-17 18:38:17 -0400402 spin_unlock_irqrestore(&m->lock, flags);
403 pgpath = choose_path_in_pg(m, pg, nr_bytes);
404 if (!IS_ERR_OR_NULL(pgpath))
405 return pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
408 /* Don't change PG until it has no remaining paths */
Mike Snitzer2da16102016-03-17 18:38:17 -0400409check_current_pg:
Will Deacon506458e2017-10-24 11:22:48 +0100410 pg = READ_ONCE(m->current_pg);
Mike Snitzer2da16102016-03-17 18:38:17 -0400411 if (pg) {
412 pgpath = choose_path_in_pg(m, pg, nr_bytes);
413 if (!IS_ERR_OR_NULL(pgpath))
414 return pgpath;
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 /*
418 * Loop through priority groups until we find a valid path.
419 * First time we skip PGs marked 'bypassed'.
Mike Christief220fd42012-06-03 00:29:45 +0100420 * Second time we only try the ones we skipped, but set
421 * pg_init_delay_retry so we do not hammer controllers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 */
423 do {
424 list_for_each_entry(pg, &m->priority_groups, list) {
Mike Snitzerd19a55c2017-01-06 15:33:14 -0500425 if (pg->bypassed == !!bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 continue;
Mike Snitzer2da16102016-03-17 18:38:17 -0400427 pgpath = choose_path_in_pg(m, pg, nr_bytes);
428 if (!IS_ERR_OR_NULL(pgpath)) {
Mike Snitzer69cea0d2020-06-10 15:02:37 -0400429 if (!bypassed) {
430 spin_lock_irqsave(&m->lock, flags);
Mike Snitzer518257b2016-03-17 16:32:10 -0400431 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
Mike Snitzer69cea0d2020-06-10 15:02:37 -0400432 spin_unlock_irqrestore(&m->lock, flags);
433 }
Mike Snitzer2da16102016-03-17 18:38:17 -0400434 return pgpath;
Mike Christief220fd42012-06-03 00:29:45 +0100435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437 } while (bypassed--);
438
439failed:
Mike Snitzer2da16102016-03-17 18:38:17 -0400440 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 m->current_pgpath = NULL;
442 m->current_pg = NULL;
Mike Snitzer2da16102016-03-17 18:38:17 -0400443 spin_unlock_irqrestore(&m->lock, flags);
444
445 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800448/*
Mike Snitzerac75b092020-05-14 12:55:39 -0400449 * dm_report_EIO() is a macro instead of a function to make pr_debug_ratelimited()
Bart Van Assche86331f32017-04-27 10:11:26 -0700450 * report the function name and line number of the function from which
451 * it has been invoked.
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800452 */
Bart Van Assche86331f32017-04-27 10:11:26 -0700453#define dm_report_EIO(m) \
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200454do { \
Bart Van Assche86331f32017-04-27 10:11:26 -0700455 struct mapped_device *md = dm_table_get_md((m)->ti->table); \
456 \
Mike Snitzerac75b092020-05-14 12:55:39 -0400457 DMDEBUG_LIMIT("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d", \
458 dm_device_name(md), \
459 test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags), \
460 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags), \
461 dm_noflush_suspending((m)->ti)); \
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200462} while (0)
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800463
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100464/*
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500465 * Check whether bios must be queued in the device-mapper core rather
466 * than here in the target.
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500467 */
Mike Snitzera862e4e2020-05-26 16:06:56 -0400468static bool __must_push_back(struct multipath *m)
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500469{
Mike Snitzera862e4e2020-05-26 16:06:56 -0400470 return dm_noflush_suspending(m->ti);
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500471}
472
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500473static bool must_push_back_rq(struct multipath *m)
474{
Mike Snitzer73265f32020-06-10 16:51:32 -0400475 unsigned long flags;
476 bool ret;
477
478 spin_lock_irqsave(&m->lock, flags);
479 ret = (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) || __must_push_back(m));
480 spin_unlock_irqrestore(&m->lock, flags);
481
482 return ret;
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500483}
484
485/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400486 * Map cloned requests (request-based multipath)
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100487 */
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100488static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
489 union map_info *map_context,
490 struct request **__clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Mike Snitzer7943bd62016-02-02 21:53:15 -0500492 struct multipath *m = ti->private;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100493 size_t nr_bytes = blk_rq_bytes(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100495 struct block_device *bdev;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100496 struct dm_mpath_io *mpio = get_mpio(map_context);
Bart Van Assche7083abb2017-04-27 10:11:15 -0700497 struct request_queue *q;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100498 struct request *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* Do we need to select a new pgpath? */
Will Deacon506458e2017-10-24 11:22:48 +0100501 pgpath = READ_ONCE(m->current_pgpath);
Mike Snitzer2da16102016-03-17 18:38:17 -0400502 if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
503 pgpath = choose_pgpath(m, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100505 if (!pgpath) {
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500506 if (must_push_back_rq(m))
Mike Snitzerb88efd42016-09-09 19:26:19 -0400507 return DM_MAPIO_DELAY_REQUEUE;
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200508 dm_report_EIO(m); /* Failed */
Christoph Hellwigf98e0eb2017-05-15 17:28:38 +0200509 return DM_MAPIO_KILL;
Mike Snitzer518257b2016-03-17 16:32:10 -0400510 } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
511 test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
Ming Lei459b5402018-01-11 14:01:55 +0800512 pg_init_all_paths(m);
513 return DM_MAPIO_DELAY_REQUEUE;
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100514 }
Mike Snitzer6afbc012014-07-08 11:55:09 -0400515
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100516 mpio->pgpath = pgpath;
517 mpio->nr_bytes = nr_bytes;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600518
519 bdev = pgpath->path.dev->bdev;
Bart Van Assche7083abb2017-04-27 10:11:15 -0700520 q = bdev_get_queue(bdev);
Christoph Hellwigff005a02018-05-09 09:54:05 +0200521 clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE,
522 BLK_MQ_REQ_NOWAIT);
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100523 if (IS_ERR(clone)) {
524 /* EBUSY, ENODEV or EWOULDBLOCK: requeue */
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500525 if (blk_queue_dying(q)) {
Bart Van Assche7083abb2017-04-27 10:11:15 -0700526 atomic_inc(&m->pg_init_in_progress);
527 activate_or_offline_path(pgpath);
Ming Lei050af082018-01-11 14:01:56 +0800528 return DM_MAPIO_DELAY_REQUEUE;
Bart Van Assche7083abb2017-04-27 10:11:15 -0700529 }
Ming Lei050af082018-01-11 14:01:56 +0800530
531 /*
532 * blk-mq's SCHED_RESTART can cover this requeue, so we
533 * needn't deal with it by DELAY_REQUEUE. More importantly,
534 * we have to return DM_MAPIO_REQUEUE so that blk-mq can
535 * get the queue busy feedback (via BLK_STS_RESOURCE),
536 * otherwise I/O merging can suffer.
537 */
Jens Axboe6a23e052018-10-10 20:49:26 -0600538 return DM_MAPIO_REQUEUE;
Mike Snitzere5863d92014-12-17 21:08:12 -0500539 }
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100540 clone->bio = clone->biotail = NULL;
541 clone->rq_disk = bdev->bd_disk;
542 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
543 *__clone = clone;
Mike Snitzere5863d92014-12-17 21:08:12 -0500544
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100545 if (pgpath->pg->ps.type->start_io)
546 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
547 &pgpath->path,
548 nr_bytes);
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600549 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Yufen Yu5de719e2019-04-24 23:19:05 +0800552static void multipath_release_clone(struct request *clone,
553 union map_info *map_context)
Mike Snitzere5863d92014-12-17 21:08:12 -0500554{
Yufen Yu5de719e2019-04-24 23:19:05 +0800555 if (unlikely(map_context)) {
556 /*
557 * non-NULL map_context means caller is still map
558 * method; must undo multipath_clone_and_map()
559 */
560 struct dm_mpath_io *mpio = get_mpio(map_context);
561 struct pgpath *pgpath = mpio->pgpath;
562
563 if (pgpath && pgpath->pg->ps.type->end_io)
564 pgpath->pg->ps.type->end_io(&pgpath->pg->ps,
565 &pgpath->path,
Gabriel Krisman Bertazi087615bf2020-04-30 16:48:29 -0400566 mpio->nr_bytes,
567 clone->io_start_time_ns);
Yufen Yu5de719e2019-04-24 23:19:05 +0800568 }
569
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100570 blk_put_request(clone);
Mike Snitzere5863d92014-12-17 21:08:12 -0500571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400574 * Map cloned bios (bio-based multipath)
575 */
Mike Snitzer0001ec52017-12-11 11:02:29 -0500576
Mike Snitzerf45f1182020-06-10 20:03:19 -0400577static void multipath_queue_bio(struct multipath *m, struct bio *bio)
578{
579 unsigned long flags;
580
581 /* Queue for the daemon to resubmit */
582 spin_lock_irqsave(&m->lock, flags);
583 bio_list_add(&m->queued_bios, bio);
584 if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
585 queue_work(kmultipathd, &m->process_queued_bios);
586 spin_unlock_irqrestore(&m->lock, flags);
587}
588
Mike Snitzer0001ec52017-12-11 11:02:29 -0500589static struct pgpath *__map_bio(struct multipath *m, struct bio *bio)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400590{
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400591 struct pgpath *pgpath;
592 unsigned long flags;
593 bool queue_io;
594
595 /* Do we need to select a new pgpath? */
Will Deacon506458e2017-10-24 11:22:48 +0100596 pgpath = READ_ONCE(m->current_pgpath);
Gabriel Krisman Bertazi5686dee2020-04-27 20:39:11 -0400597 if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
Mike Snitzer0001ec52017-12-11 11:02:29 -0500598 pgpath = choose_pgpath(m, bio->bi_iter.bi_size);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400599
Gabriel Krisman Bertazi5686dee2020-04-27 20:39:11 -0400600 /* MPATHF_QUEUE_IO might have been cleared by choose_pgpath. */
601 queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
602
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400603 if ((pgpath && queue_io) ||
604 (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
Mike Snitzerf45f1182020-06-10 20:03:19 -0400605 multipath_queue_bio(m, bio);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500606
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400607 /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
608 if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
609 pg_init_all_paths(m);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500610
Mike Snitzer0001ec52017-12-11 11:02:29 -0500611 return ERR_PTR(-EAGAIN);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400612 }
613
Mike Snitzer0001ec52017-12-11 11:02:29 -0500614 return pgpath;
615}
616
Mike Snitzer0001ec52017-12-11 11:02:29 -0500617static int __multipath_map_bio(struct multipath *m, struct bio *bio,
618 struct dm_mpath_io *mpio)
619{
Mike Snitzerdbaf9712019-11-26 10:08:29 -0500620 struct pgpath *pgpath = __map_bio(m, bio);
Mike Snitzer0001ec52017-12-11 11:02:29 -0500621
622 if (IS_ERR(pgpath))
623 return DM_MAPIO_SUBMITTED;
624
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400625 if (!pgpath) {
Mike Snitzera862e4e2020-05-26 16:06:56 -0400626 if (__must_push_back(m))
Bart Van Asscheca5beb72017-04-27 10:11:24 -0700627 return DM_MAPIO_REQUEUE;
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200628 dm_report_EIO(m);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200629 return DM_MAPIO_KILL;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400630 }
631
632 mpio->pgpath = pgpath;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400633
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200634 bio->bi_status = 0;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200635 bio_set_dev(bio, pgpath->path.dev->bdev);
Jens Axboe1eff9d32016-08-05 15:35:16 -0600636 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400637
638 if (pgpath->pg->ps.type->start_io)
639 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
640 &pgpath->path,
Mike Snitzerd0442f82017-12-11 15:58:41 -0500641 mpio->nr_bytes);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400642 return DM_MAPIO_REMAPPED;
643}
644
645static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
646{
647 struct multipath *m = ti->private;
Mike Snitzerbf661be2016-05-24 15:48:08 -0400648 struct dm_mpath_io *mpio = NULL;
649
Mike Snitzer63f6e6f2017-12-05 14:10:33 -0500650 multipath_init_per_bio_data(bio, &mpio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400651 return __multipath_map_bio(m, bio, mpio);
652}
653
Mike Snitzer7e48c762016-09-14 10:47:03 -0400654static void process_queued_io_list(struct multipath *m)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400655{
Mike Snitzer953923c2018-10-11 11:06:29 -0400656 if (m->queue_mode == DM_TYPE_REQUEST_BASED)
Mike Snitzer7e48c762016-09-14 10:47:03 -0400657 dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
Mike Snitzer8d47e652018-03-05 14:10:11 -0500658 else if (m->queue_mode == DM_TYPE_BIO_BASED)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400659 queue_work(kmultipathd, &m->process_queued_bios);
660}
661
662static void process_queued_bios(struct work_struct *work)
663{
664 int r;
665 unsigned long flags;
666 struct bio *bio;
667 struct bio_list bios;
668 struct blk_plug plug;
669 struct multipath *m =
670 container_of(work, struct multipath, process_queued_bios);
671
672 bio_list_init(&bios);
673
674 spin_lock_irqsave(&m->lock, flags);
675
676 if (bio_list_empty(&m->queued_bios)) {
677 spin_unlock_irqrestore(&m->lock, flags);
678 return;
679 }
680
681 bio_list_merge(&bios, &m->queued_bios);
682 bio_list_init(&m->queued_bios);
683
684 spin_unlock_irqrestore(&m->lock, flags);
685
686 blk_start_plug(&plug);
687 while ((bio = bio_list_pop(&bios))) {
Mike Snitzer1836df02017-12-06 16:08:14 -0500688 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
689 dm_bio_restore(get_bio_details_from_mpio(mpio), bio);
690 r = __multipath_map_bio(m, bio, mpio);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200691 switch (r) {
692 case DM_MAPIO_KILL:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200693 bio->bi_status = BLK_STS_IOERR;
694 bio_endio(bio);
Dan Carpenter047385b2017-06-14 08:22:55 -0600695 break;
Christoph Hellwig846785e2017-06-03 09:38:02 +0200696 case DM_MAPIO_REQUEUE:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200697 bio->bi_status = BLK_STS_DM_REQUEUE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400698 bio_endio(bio);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200699 break;
700 case DM_MAPIO_REMAPPED:
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400701 generic_make_request(bio);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200702 break;
Wang Sheng-Hui8192a0c2018-03-10 10:18:55 +0800703 case DM_MAPIO_SUBMITTED:
Bart Van Assche9157c8d2017-08-09 11:32:14 -0700704 break;
705 default:
706 WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200707 }
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400708 }
709 blk_finish_plug(&plug);
710}
711
712/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * If we run out of usable paths, should we queue I/O or error it?
714 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -0500715static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
Mike Snitzer4c3f4832020-05-29 15:59:13 -0400716 bool save_old_value, const char *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 unsigned long flags;
Mike Snitzer553ec942020-05-27 16:32:51 -0400719 bool queue_if_no_path_bit, saved_queue_if_no_path_bit;
Mike Snitzer4c3f4832020-05-29 15:59:13 -0400720 const char *dm_dev_name = dm_device_name(dm_table_get_md(m->ti->table));
721
722 DMDEBUG("%s: %s caller=%s queue_if_no_path=%d save_old_value=%d",
723 dm_dev_name, __func__, caller, queue_if_no_path, save_old_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 spin_lock_irqsave(&m->lock, flags);
Mike Snitzer553ec942020-05-27 16:32:51 -0400726
727 queue_if_no_path_bit = test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
728 saved_queue_if_no_path_bit = test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
729
730 if (save_old_value) {
731 if (unlikely(!queue_if_no_path_bit && saved_queue_if_no_path_bit)) {
732 DMERR("%s: QIFNP disabled but saved as enabled, saving again loses state, not saving!",
Mike Snitzer4c3f4832020-05-29 15:59:13 -0400733 dm_dev_name);
Mike Snitzer553ec942020-05-27 16:32:51 -0400734 } else
735 assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path_bit);
736 } else if (!queue_if_no_path && saved_queue_if_no_path_bit) {
737 /* due to "fail_if_no_path" message, need to honor it. */
738 clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
739 }
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500740 assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path);
Mike Snitzer553ec942020-05-27 16:32:51 -0400741
Mike Snitzer4c3f4832020-05-29 15:59:13 -0400742 DMDEBUG("%s: after %s changes; QIFNP = %d; SQIFNP = %d; DNFS = %d",
743 dm_dev_name, __func__,
744 test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags),
745 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags),
746 dm_noflush_suspending(m->ti));
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 spin_unlock_irqrestore(&m->lock, flags);
749
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400750 if (!queue_if_no_path) {
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200751 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -0400752 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400753 }
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return 0;
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
Anatol Pomazaube240ff2020-01-13 17:41:27 -0500759 * If the queue_if_no_path timeout fires, turn off queue_if_no_path and
760 * process any queued I/O.
761 */
762static void queue_if_no_path_timeout_work(struct timer_list *t)
763{
764 struct multipath *m = from_timer(m, t, nopath_timer);
765 struct mapped_device *md = dm_table_get_md(m->ti->table);
766
767 DMWARN("queue_if_no_path timeout on %s, failing queued IO", dm_device_name(md));
Mike Snitzer4c3f4832020-05-29 15:59:13 -0400768 queue_if_no_path(m, false, false, __func__);
Anatol Pomazaube240ff2020-01-13 17:41:27 -0500769}
770
771/*
772 * Enable the queue_if_no_path timeout if necessary.
773 * Called with m->lock held.
774 */
775static void enable_nopath_timeout(struct multipath *m)
776{
777 unsigned long queue_if_no_path_timeout =
778 READ_ONCE(queue_if_no_path_timeout_secs) * HZ;
779
780 lockdep_assert_held(&m->lock);
781
782 if (queue_if_no_path_timeout > 0 &&
783 atomic_read(&m->nr_valid_paths) == 0 &&
784 test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
785 mod_timer(&m->nopath_timer,
786 jiffies + queue_if_no_path_timeout);
787 }
788}
789
790static void disable_nopath_timeout(struct multipath *m)
791{
792 del_timer_sync(&m->nopath_timer);
793}
794
795/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 * An event is triggered whenever a path is taken out of use.
797 * Includes path failure and PG bypass.
798 */
David Howellsc4028952006-11-22 14:57:56 +0000799static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
David Howellsc4028952006-11-22 14:57:56 +0000801 struct multipath *m =
802 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 dm_table_event(m->ti->table);
805}
806
807/*-----------------------------------------------------------------
808 * Constructor/argument parsing:
809 * <#multipath feature args> [<arg>]*
810 * <#hw_handler args> [hw_handler [<arg>]*]
811 * <#priority groups>
812 * <initial priority group>
813 * [<selector> <#selector args> [<arg>]*
814 * <#paths> <#per-path selector args>
815 * [<path> [<arg>]* ]+ ]+
816 *---------------------------------------------------------------*/
Mike Snitzer498f0102011-08-02 12:32:04 +0100817static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct dm_target *ti)
819{
820 int r;
821 struct path_selector_type *pst;
822 unsigned ps_argc;
823
Eric Biggers5916a222017-06-22 11:32:45 -0700824 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700825 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 };
827
Mike Snitzer498f0102011-08-02 12:32:04 +0100828 pst = dm_get_path_selector(dm_shift_arg(as));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700830 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return -EINVAL;
832 }
833
Mike Snitzer498f0102011-08-02 12:32:04 +0100834 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100835 if (r) {
836 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 r = pst->create(&pg->ps, ps_argc, as->argv);
841 if (r) {
842 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700843 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return r;
845 }
846
847 pg->ps.type = pst;
Mike Snitzer498f0102011-08-02 12:32:04 +0100848 dm_consume_args(as, ps_argc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 return 0;
851}
852
Mike Snitzere8f74a02018-03-12 19:49:25 -0400853static int setup_scsi_dh(struct block_device *bdev, struct multipath *m,
Mike Snitzerb5922112018-09-17 11:38:47 -0400854 const char **attached_handler_name, char **error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500856 struct request_queue *q = bdev_get_queue(bdev);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500857 int r;
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100858
Mike Snitzer518257b2016-03-17 16:32:10 -0400859 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200860retain:
Mike Snitzerb5922112018-09-17 11:38:47 -0400861 if (*attached_handler_name) {
Mike Snitzera58a9352012-07-27 15:08:04 +0100862 /*
tang.junhui54cd6402016-11-24 15:11:48 +0800863 * Clear any hw_handler_params associated with a
864 * handler that isn't already attached.
865 */
Mike Snitzerb5922112018-09-17 11:38:47 -0400866 if (m->hw_handler_name && strcmp(*attached_handler_name, m->hw_handler_name)) {
tang.junhui54cd6402016-11-24 15:11:48 +0800867 kfree(m->hw_handler_params);
868 m->hw_handler_params = NULL;
869 }
870
871 /*
Mike Snitzera58a9352012-07-27 15:08:04 +0100872 * Reset hw_handler_name to match the attached handler
Mike Snitzera58a9352012-07-27 15:08:04 +0100873 *
874 * NB. This modifies the table line to show the actual
875 * handler instead of the original table passed in.
876 */
877 kfree(m->hw_handler_name);
Mike Snitzerb5922112018-09-17 11:38:47 -0400878 m->hw_handler_name = *attached_handler_name;
879 *attached_handler_name = NULL;
Mike Snitzera58a9352012-07-27 15:08:04 +0100880 }
881 }
882
883 if (m->hw_handler_name) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100884 r = scsi_dh_attach(q, m->hw_handler_name);
885 if (r == -EBUSY) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200886 char b[BDEVNAME_SIZE];
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100887
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200888 printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500889 bdevname(bdev, b));
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200890 goto retain;
891 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700892 if (r < 0) {
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500893 *error = "error attaching hardware handler";
894 return r;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700895 }
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700896
897 if (m->hw_handler_params) {
898 r = scsi_dh_set_params(q, m->hw_handler_params);
899 if (r < 0) {
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500900 *error = "unable to set hardware handler parameters";
901 return r;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700902 }
903 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700904 }
905
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500906 return 0;
907}
908
909static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
910 struct dm_target *ti)
911{
912 int r;
913 struct pgpath *p;
914 struct multipath *m = ti->private;
Mike Snitzere8f74a02018-03-12 19:49:25 -0400915 struct request_queue *q;
Mike Snitzerb5922112018-09-17 11:38:47 -0400916 const char *attached_handler_name = NULL;
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500917
918 /* we need at least a path arg */
919 if (as->argc < 1) {
920 ti->error = "no device given";
921 return ERR_PTR(-EINVAL);
922 }
923
924 p = alloc_pgpath();
925 if (!p)
926 return ERR_PTR(-ENOMEM);
927
928 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
929 &p->path.dev);
930 if (r) {
931 ti->error = "error getting device";
932 goto bad;
933 }
934
Mike Snitzere8f74a02018-03-12 19:49:25 -0400935 q = bdev_get_queue(p->path.dev->bdev);
936 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
Mike Snitzere457edf2018-03-29 11:50:10 -0400937 if (attached_handler_name || m->hw_handler_name) {
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500938 INIT_DELAYED_WORK(&p->activate_path, activate_path_work);
Mike Snitzerb5922112018-09-17 11:38:47 -0400939 r = setup_scsi_dh(p->path.dev->bdev, m, &attached_handler_name, &ti->error);
Martin Wilck940bc472019-04-29 11:48:15 +0200940 kfree(attached_handler_name);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500941 if (r) {
942 dm_put_device(ti, p->path.dev);
943 goto bad;
944 }
945 }
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
948 if (r) {
949 dm_put_device(ti, p->path.dev);
950 goto bad;
951 }
952
953 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 bad:
955 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100956 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Mike Snitzer498f0102011-08-02 12:32:04 +0100959static struct priority_group *parse_priority_group(struct dm_arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700960 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Eric Biggers5916a222017-06-22 11:32:45 -0700962 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700963 {1, 1024, "invalid number of paths"},
964 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 };
966
967 int r;
Mike Snitzer498f0102011-08-02 12:32:04 +0100968 unsigned i, nr_selector_args, nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700970 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 if (as->argc < 2) {
973 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100974 ti->error = "not enough priority group arguments";
975 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977
978 pg = alloc_priority_group();
979 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700980 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100981 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983 pg->m = m;
984
985 r = parse_path_selector(as, pg, ti);
986 if (r)
987 goto bad;
988
989 /*
990 * read the paths
991 */
Mike Snitzer498f0102011-08-02 12:32:04 +0100992 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (r)
994 goto bad;
995
Mike Snitzer498f0102011-08-02 12:32:04 +0100996 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (r)
998 goto bad;
999
Mike Snitzer498f0102011-08-02 12:32:04 +01001000 nr_args = 1 + nr_selector_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 for (i = 0; i < pg->nr_pgpaths; i++) {
1002 struct pgpath *pgpath;
Mike Snitzer498f0102011-08-02 12:32:04 +01001003 struct dm_arg_set path_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Mike Snitzer498f0102011-08-02 12:32:04 +01001005 if (as->argc < nr_args) {
Mikulas Patocka148acff2008-07-21 12:00:30 +01001006 ti->error = "not enough path parameters";
Alasdair G Kergon6bbf79a2010-08-12 04:13:49 +01001007 r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +01001009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Mike Snitzer498f0102011-08-02 12:32:04 +01001011 path_args.argc = nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 path_args.argv = as->argv;
1013
1014 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +01001015 if (IS_ERR(pgpath)) {
1016 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +01001018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 pgpath->pg = pg;
1021 list_add_tail(&pgpath->list, &pg->pgpaths);
Mike Snitzer498f0102011-08-02 12:32:04 +01001022 dm_consume_args(as, nr_args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024
1025 return pg;
1026
1027 bad:
1028 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +01001029 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
Mike Snitzer498f0102011-08-02 12:32:04 +01001032static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 unsigned hw_argc;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -07001035 int ret;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001036 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Eric Biggers5916a222017-06-22 11:32:45 -07001038 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001039 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 };
1041
Mike Snitzer498f0102011-08-02 12:32:04 +01001042 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return -EINVAL;
1044
1045 if (!hw_argc)
1046 return 0;
1047
Mike Snitzer8d47e652018-03-05 14:10:11 -05001048 if (m->queue_mode == DM_TYPE_BIO_BASED) {
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001049 dm_consume_args(as, hw_argc);
1050 DMERR("bio-based multipath doesn't allow hardware handler args");
1051 return 0;
1052 }
1053
Mike Snitzer498f0102011-08-02 12:32:04 +01001054 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
tang.junhuif97dc422016-10-28 17:04:46 +08001055 if (!m->hw_handler_name)
1056 return -EINVAL;
Chandra Seetharaman14e98c52008-11-13 23:39:06 +00001057
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -07001058 if (hw_argc > 1) {
1059 char *p;
1060 int i, j, len = 4;
1061
1062 for (i = 0; i <= hw_argc - 2; i++)
1063 len += strlen(as->argv[i]) + 1;
1064 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
1065 if (!p) {
1066 ti->error = "memory allocation failed";
1067 ret = -ENOMEM;
1068 goto fail;
1069 }
1070 j = sprintf(p, "%d", hw_argc - 1);
1071 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
1072 j = sprintf(p, "%s", as->argv[i]);
1073 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001074 dm_consume_args(as, hw_argc - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 return 0;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -07001077fail:
1078 kfree(m->hw_handler_name);
1079 m->hw_handler_name = NULL;
1080 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
Mike Snitzer498f0102011-08-02 12:32:04 +01001083static int parse_features(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 int r;
1086 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001087 struct dm_target *ti = m->ti;
Mike Snitzer498f0102011-08-02 12:32:04 +01001088 const char *arg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Eric Biggers5916a222017-06-22 11:32:45 -07001090 static const struct dm_arg _args[] = {
Mike Snitzere83068a2016-05-24 21:16:51 -04001091 {0, 8, "invalid number of feature args"},
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001092 {1, 50, "pg_init_retries must be between 1 and 50"},
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001093 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 };
1095
Mike Snitzer498f0102011-08-02 12:32:04 +01001096 r = dm_read_arg_group(_args, as, &argc, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (r)
1098 return -EINVAL;
1099
1100 if (!argc)
1101 return 0;
1102
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001103 do {
Mike Snitzer498f0102011-08-02 12:32:04 +01001104 arg_name = dm_shift_arg(as);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001105 argc--;
1106
Mike Snitzer498f0102011-08-02 12:32:04 +01001107 if (!strcasecmp(arg_name, "queue_if_no_path")) {
Mike Snitzer4c3f4832020-05-29 15:59:13 -04001108 r = queue_if_no_path(m, true, false, __func__);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001109 continue;
1110 }
1111
Mike Snitzera58a9352012-07-27 15:08:04 +01001112 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
Mike Snitzer518257b2016-03-17 16:32:10 -04001113 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
Mike Snitzera58a9352012-07-27 15:08:04 +01001114 continue;
1115 }
1116
Mike Snitzer498f0102011-08-02 12:32:04 +01001117 if (!strcasecmp(arg_name, "pg_init_retries") &&
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001118 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001119 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001120 argc--;
1121 continue;
1122 }
1123
Mike Snitzer498f0102011-08-02 12:32:04 +01001124 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001125 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001126 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001127 argc--;
1128 continue;
1129 }
1130
Mike Snitzere83068a2016-05-24 21:16:51 -04001131 if (!strcasecmp(arg_name, "queue_mode") &&
1132 (argc >= 1)) {
1133 const char *queue_mode_name = dm_shift_arg(as);
1134
1135 if (!strcasecmp(queue_mode_name, "bio"))
1136 m->queue_mode = DM_TYPE_BIO_BASED;
Mike Snitzer953923c2018-10-11 11:06:29 -04001137 else if (!strcasecmp(queue_mode_name, "rq") ||
1138 !strcasecmp(queue_mode_name, "mq"))
Mike Snitzere83068a2016-05-24 21:16:51 -04001139 m->queue_mode = DM_TYPE_REQUEST_BASED;
Mike Snitzere83068a2016-05-24 21:16:51 -04001140 else {
1141 ti->error = "Unknown 'queue_mode' requested";
1142 r = -EINVAL;
1143 }
1144 argc--;
1145 continue;
1146 }
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001149 r = -EINVAL;
1150 } while (argc && !r);
1151
1152 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Mike Snitzere83068a2016-05-24 21:16:51 -04001155static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Mike Snitzer498f0102011-08-02 12:32:04 +01001157 /* target arguments */
Eric Biggers5916a222017-06-22 11:32:45 -07001158 static const struct dm_arg _args[] = {
Mike Snitzera490a072011-03-24 13:54:33 +00001159 {0, 1024, "invalid number of priority groups"},
1160 {0, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 };
1162
1163 int r;
1164 struct multipath *m;
Mike Snitzer498f0102011-08-02 12:32:04 +01001165 struct dm_arg_set as;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 unsigned pg_count = 0;
1167 unsigned next_pg_num;
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001168 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 as.argc = argc;
1171 as.argv = argv;
1172
Mike Snitzere83068a2016-05-24 21:16:51 -04001173 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001175 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return -EINVAL;
1177 }
1178
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001179 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (r)
1181 goto bad;
1182
Mike Snitzere83068a2016-05-24 21:16:51 -04001183 r = alloc_multipath_stage2(ti, m);
1184 if (r)
1185 goto bad;
1186
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001187 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (r)
1189 goto bad;
1190
Mike Snitzer498f0102011-08-02 12:32:04 +01001191 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (r)
1193 goto bad;
1194
Mike Snitzer498f0102011-08-02 12:32:04 +01001195 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (r)
1197 goto bad;
1198
Mike Snitzera490a072011-03-24 13:54:33 +00001199 if ((!m->nr_priority_groups && next_pg_num) ||
1200 (m->nr_priority_groups && !next_pg_num)) {
1201 ti->error = "invalid initial priority group";
1202 r = -EINVAL;
1203 goto bad;
1204 }
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* parse the priority groups */
1207 while (as.argc) {
1208 struct priority_group *pg;
Mike Snitzer91e968a2016-03-17 17:10:15 -04001209 unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001211 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +01001212 if (IS_ERR(pg)) {
1213 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 goto bad;
1215 }
1216
Mike Snitzer91e968a2016-03-17 17:10:15 -04001217 nr_valid_paths += pg->nr_pgpaths;
1218 atomic_set(&m->nr_valid_paths, nr_valid_paths);
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 list_add_tail(&pg->list, &m->priority_groups);
1221 pg_count++;
1222 pg->pg_num = pg_count;
1223 if (!--next_pg_num)
1224 m->next_pg = pg;
1225 }
1226
1227 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001228 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 r = -EINVAL;
1230 goto bad;
1231 }
1232
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001233 spin_lock_irqsave(&m->lock, flags);
1234 enable_nopath_timeout(m);
1235 spin_unlock_irqrestore(&m->lock, flags);
1236
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001237 ti->num_flush_bios = 1;
1238 ti->num_discard_bios = 1;
Mike Snitzer042bcef2013-05-10 14:37:16 +01001239 ti->num_write_same_bios = 1;
Christoph Hellwigac62d622017-04-05 19:21:05 +02001240 ti->num_write_zeroes_bios = 1;
Mike Snitzer8d47e652018-03-05 14:10:11 -05001241 if (m->queue_mode == DM_TYPE_BIO_BASED)
Mike Snitzerbf661be2016-05-24 15:48:08 -04001242 ti->per_io_data_size = multipath_per_bio_data_size();
Christoph Hellwigeb8db832017-01-22 18:32:46 +01001243 else
Mike Snitzer8637a6b2016-01-31 12:08:36 -05001244 ti->per_io_data_size = sizeof(struct dm_mpath_io);
Mikulas Patocka86279212009-06-22 10:12:24 +01001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return 0;
1247
1248 bad:
1249 free_multipath(m);
1250 return r;
1251}
1252
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001253static void multipath_wait_for_pg_init_completion(struct multipath *m)
1254{
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001255 DEFINE_WAIT(wait);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001256
1257 while (1) {
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001258 prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001259
Mike Snitzer91e968a2016-03-17 17:10:15 -04001260 if (!atomic_read(&m->pg_init_in_progress))
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001261 break;
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001262
1263 io_schedule();
1264 }
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001265 finish_wait(&m->pg_init_wait, &wait);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001266}
1267
1268static void flush_multipath_work(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Mike Snitzer848b8ae2017-12-10 15:37:21 -05001270 if (m->hw_handler_name) {
1271 set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1272 smp_mb__after_atomic();
Shiva Krishna Merla954a73d2013-10-30 03:26:38 +00001273
wuzhouhui935fcc52018-03-22 22:22:38 +08001274 if (atomic_read(&m->pg_init_in_progress))
1275 flush_workqueue(kmpath_handlerd);
Mike Snitzer848b8ae2017-12-10 15:37:21 -05001276 multipath_wait_for_pg_init_completion(m);
1277
1278 clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1279 smp_mb__after_atomic();
1280 }
1281
wuzhouhui935fcc52018-03-22 22:22:38 +08001282 if (m->queue_mode == DM_TYPE_BIO_BASED)
1283 flush_work(&m->process_queued_bios);
Tejun Heo43829732012-08-20 14:51:24 -07001284 flush_work(&m->trigger_event);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001285}
1286
1287static void multipath_dtr(struct dm_target *ti)
1288{
1289 struct multipath *m = ti->private;
1290
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001291 disable_nopath_timeout(m);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001292 flush_multipath_work(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 free_multipath(m);
1294}
1295
1296/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 * Take a path out of use.
1298 */
1299static int fail_path(struct pgpath *pgpath)
1300{
1301 unsigned long flags;
1302 struct multipath *m = pgpath->pg->m;
1303
1304 spin_lock_irqsave(&m->lock, flags);
1305
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001306 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 goto out;
1308
Mike Snitzer04867372020-06-04 10:44:34 -04001309 DMWARN("%s: Failing path %s.",
1310 dm_device_name(dm_table_get_md(m->ti->table)),
1311 pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001314 pgpath->is_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 pgpath->fail_count++;
1316
Mike Snitzer91e968a2016-03-17 17:10:15 -04001317 atomic_dec(&m->nr_valid_paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 if (pgpath == m->current_pgpath)
1320 m->current_pgpath = NULL;
1321
Mike Andersonb15546f2007-10-19 22:48:02 +01001322 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
Mike Snitzer91e968a2016-03-17 17:10:15 -04001323 pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
Mike Andersonb15546f2007-10-19 22:48:02 +01001324
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001325 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001327 enable_nopath_timeout(m);
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329out:
1330 spin_unlock_irqrestore(&m->lock, flags);
1331
1332 return 0;
1333}
1334
1335/*
1336 * Reinstate a previously-failed path
1337 */
1338static int reinstate_path(struct pgpath *pgpath)
1339{
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001340 int r = 0, run_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 unsigned long flags;
1342 struct multipath *m = pgpath->pg->m;
Mike Snitzer91e968a2016-03-17 17:10:15 -04001343 unsigned nr_valid_paths;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 spin_lock_irqsave(&m->lock, flags);
1346
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001347 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 goto out;
1349
Mike Snitzer04867372020-06-04 10:44:34 -04001350 DMWARN("%s: Reinstating path %s.",
1351 dm_device_name(dm_table_get_md(m->ti->table)),
1352 pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1355 if (r)
1356 goto out;
1357
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001358 pgpath->is_active = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Mike Snitzer91e968a2016-03-17 17:10:15 -04001360 nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
1361 if (nr_valid_paths == 1) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001362 m->current_pgpath = NULL;
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001363 run_queue = 1;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001364 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001365 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
Mike Snitzer91e968a2016-03-17 17:10:15 -04001366 atomic_inc(&m->pg_init_in_progress);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Mike Andersonb15546f2007-10-19 22:48:02 +01001369 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
Mike Snitzer91e968a2016-03-17 17:10:15 -04001370 pgpath->path.dev->name, nr_valid_paths);
Mike Andersonb15546f2007-10-19 22:48:02 +01001371
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001372 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374out:
1375 spin_unlock_irqrestore(&m->lock, flags);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001376 if (run_queue) {
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001377 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -04001378 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001381 if (pgpath->is_active)
1382 disable_nopath_timeout(m);
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return r;
1385}
1386
1387/*
1388 * Fail or reinstate all paths that match the provided struct dm_dev.
1389 */
1390static int action_dev(struct multipath *m, struct dm_dev *dev,
1391 action_fn action)
1392{
Mike Snitzer19040c02011-03-24 13:54:31 +00001393 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 struct pgpath *pgpath;
1395 struct priority_group *pg;
1396
1397 list_for_each_entry(pg, &m->priority_groups, list) {
1398 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1399 if (pgpath->path.dev == dev)
1400 r = action(pgpath);
1401 }
1402 }
1403
1404 return r;
1405}
1406
1407/*
1408 * Temporarily try to avoid having to use the specified PG
1409 */
1410static void bypass_pg(struct multipath *m, struct priority_group *pg,
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001411 bool bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 unsigned long flags;
1414
1415 spin_lock_irqsave(&m->lock, flags);
1416
1417 pg->bypassed = bypassed;
1418 m->current_pgpath = NULL;
1419 m->current_pg = NULL;
1420
1421 spin_unlock_irqrestore(&m->lock, flags);
1422
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001423 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
1426/*
1427 * Switch to using the specified PG from the next I/O that gets mapped
1428 */
1429static int switch_pg_num(struct multipath *m, const char *pgstr)
1430{
1431 struct priority_group *pg;
1432 unsigned pgnum;
1433 unsigned long flags;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001434 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001436 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
tang.junhuicc5bd922016-11-04 12:37:09 +08001437 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 DMWARN("invalid PG number supplied to switch_pg_num");
1439 return -EINVAL;
1440 }
1441
1442 spin_lock_irqsave(&m->lock, flags);
1443 list_for_each_entry(pg, &m->priority_groups, list) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001444 pg->bypassed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (--pgnum)
1446 continue;
1447
1448 m->current_pgpath = NULL;
1449 m->current_pg = NULL;
1450 m->next_pg = pg;
1451 }
1452 spin_unlock_irqrestore(&m->lock, flags);
1453
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001454 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return 0;
1456}
1457
1458/*
1459 * Set/clear bypassed status of a PG.
1460 * PGs are numbered upwards from 1 in the order they were declared.
1461 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001462static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 struct priority_group *pg;
1465 unsigned pgnum;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001466 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001468 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
tang.junhuicc5bd922016-11-04 12:37:09 +08001469 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 DMWARN("invalid PG number supplied to bypass_pg");
1471 return -EINVAL;
1472 }
1473
1474 list_for_each_entry(pg, &m->priority_groups, list) {
1475 if (!--pgnum)
1476 break;
1477 }
1478
1479 bypass_pg(m, pg, bypassed);
1480 return 0;
1481}
1482
1483/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001484 * Should we retry pg_init immediately?
1485 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001486static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001487{
1488 unsigned long flags;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001489 bool limit_reached = false;
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001490
1491 spin_lock_irqsave(&m->lock, flags);
1492
Mike Snitzer91e968a2016-03-17 17:10:15 -04001493 if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
1494 !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
Mike Snitzer518257b2016-03-17 16:32:10 -04001495 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001496 else
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001497 limit_reached = true;
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001498
1499 spin_unlock_irqrestore(&m->lock, flags);
1500
1501 return limit_reached;
1502}
1503
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001504static void pg_init_done(void *data, int errors)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001505{
Moger, Babu83c0d5d2010-03-06 02:29:45 +00001506 struct pgpath *pgpath = data;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001507 struct priority_group *pg = pgpath->pg;
1508 struct multipath *m = pg->m;
1509 unsigned long flags;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001510 bool delay_retry = false;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001511
1512 /* device or driver problems */
1513 switch (errors) {
1514 case SCSI_DH_OK:
1515 break;
1516 case SCSI_DH_NOSYS:
1517 if (!m->hw_handler_name) {
1518 errors = 0;
1519 break;
1520 }
Moger, Babuf7b934c2010-03-06 02:29:49 +00001521 DMERR("Could not failover the device: Handler scsi_dh_%s "
1522 "Error %d.", m->hw_handler_name, errors);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001523 /*
1524 * Fail path for now, so we do not ping pong
1525 */
1526 fail_path(pgpath);
1527 break;
1528 case SCSI_DH_DEV_TEMP_BUSY:
1529 /*
1530 * Probably doing something like FW upgrade on the
1531 * controller so try the other pg.
1532 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001533 bypass_pg(m, pg, true);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001534 break;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001535 case SCSI_DH_RETRY:
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001536 /* Wait before retrying. */
zhengbin4ecc5082019-12-24 14:38:00 +08001537 delay_retry = true;
Bart Van Assche7b06e092017-08-09 11:32:13 -07001538 /* fall through */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001539 case SCSI_DH_IMM_RETRY:
1540 case SCSI_DH_RES_TEMP_UNAVAIL:
1541 if (pg_init_limit_reached(m, pgpath))
1542 fail_path(pgpath);
1543 errors = 0;
1544 break;
Mike Snitzerec31f3f2016-02-20 12:49:43 -05001545 case SCSI_DH_DEV_OFFLINED:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001546 default:
1547 /*
1548 * We probably do not want to fail the path for a device
1549 * error, but this is what the old dm did. In future
1550 * patches we can do more advanced handling.
1551 */
1552 fail_path(pgpath);
1553 }
1554
1555 spin_lock_irqsave(&m->lock, flags);
1556 if (errors) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001557 if (pgpath == m->current_pgpath) {
1558 DMERR("Could not failover device. Error %d.", errors);
1559 m->current_pgpath = NULL;
1560 m->current_pg = NULL;
1561 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001562 } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001563 pg->bypassed = false;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001564
Mike Snitzer91e968a2016-03-17 17:10:15 -04001565 if (atomic_dec_return(&m->pg_init_in_progress) > 0)
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001566 /* Activations of other paths are still on going */
1567 goto out;
1568
Mike Snitzer518257b2016-03-17 16:32:10 -04001569 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1570 if (delay_retry)
1571 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1572 else
1573 clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1574
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001575 if (__pg_init_all_paths(m))
1576 goto out;
1577 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001578 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001579
Mike Snitzer7e48c762016-09-14 10:47:03 -04001580 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001581
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001582 /*
1583 * Wake up any thread waiting to suspend.
1584 */
1585 wake_up(&m->pg_init_wait);
1586
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001587out:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001588 spin_unlock_irqrestore(&m->lock, flags);
1589}
1590
Bart Van Assche89bfce72017-04-27 10:11:14 -07001591static void activate_or_offline_path(struct pgpath *pgpath)
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001592{
Mike Snitzerf10e06b2016-09-01 12:06:37 -04001593 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001594
Mike Snitzerf10e06b2016-09-01 12:06:37 -04001595 if (pgpath->is_active && !blk_queue_dying(q))
1596 scsi_dh_activate(q, pg_init_done, pgpath);
Hannes Reinecke3a017502014-02-28 15:33:49 +01001597 else
1598 pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001599}
1600
Bart Van Assche89bfce72017-04-27 10:11:14 -07001601static void activate_path_work(struct work_struct *work)
1602{
1603 struct pgpath *pgpath =
1604 container_of(work, struct pgpath, activate_path.work);
1605
1606 activate_or_offline_path(pgpath);
1607}
1608
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001609static int multipath_end_io(struct dm_target *ti, struct request *clone,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001610 blk_status_t error, union map_info *map_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001612 struct dm_mpath_io *mpio = get_mpio(map_context);
1613 struct pgpath *pgpath = mpio->pgpath;
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001614 int r = DM_ENDIO_DONE;
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001615
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001616 /*
1617 * We don't queue any clone request inside the multipath target
1618 * during end I/O handling, since those clone requests don't have
1619 * bio clones. If we queue them inside the multipath target,
1620 * we need to make bio clones, that requires memory allocation.
Mike Snitzer4cc96132016-05-12 16:28:10 -04001621 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001622 * don't have bio clones.)
1623 * Instead of queueing the clone request here, we queue the original
1624 * request into dm core, which will remake a clone request and
1625 * clone bios for it and resubmit it later.
1626 */
Keith Buscha1275672018-01-09 12:04:18 -07001627 if (error && blk_path_error(error)) {
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001628 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Mike Snitzerac514ff2018-01-12 19:53:40 -05001630 if (error == BLK_STS_RESOURCE)
1631 r = DM_ENDIO_DELAY_REQUEUE;
1632 else
1633 r = DM_ENDIO_REQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001635 if (pgpath)
1636 fail_path(pgpath);
Mike Snitzer959eb4e2010-08-12 04:14:32 +01001637
Mike Snitzer73265f32020-06-10 16:51:32 -04001638 if (!atomic_read(&m->nr_valid_paths) &&
1639 !must_push_back_rq(m)) {
1640 if (error == BLK_STS_IOERR)
1641 dm_report_EIO(m);
1642 /* complete with the original error */
1643 r = DM_ENDIO_DONE;
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001644 }
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (pgpath) {
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001648 struct path_selector *ps = &pgpath->pg->ps;
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (ps->type->end_io)
Gabriel Krisman Bertazi087615bf2020-04-30 16:48:29 -04001651 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes,
1652 clone->io_start_time_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001655 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001658static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone,
Mike Snitzer848b8ae2017-12-10 15:37:21 -05001659 blk_status_t *error)
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001660{
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001661 struct multipath *m = ti->private;
1662 struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
1663 struct pgpath *pgpath = mpio->pgpath;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001664 unsigned long flags;
Christoph Hellwig1be56902017-06-03 09:38:03 +02001665 int r = DM_ENDIO_DONE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001666
Keith Buscha1275672018-01-09 12:04:18 -07001667 if (!*error || !blk_path_error(*error))
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001668 goto done;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001669
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001670 if (pgpath)
1671 fail_path(pgpath);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001672
Mike Snitzera271a892020-06-10 16:07:57 -04001673 if (!atomic_read(&m->nr_valid_paths)) {
1674 spin_lock_irqsave(&m->lock, flags);
1675 if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
1676 if (__must_push_back(m)) {
1677 r = DM_ENDIO_REQUEUE;
1678 } else {
1679 dm_report_EIO(m);
1680 *error = BLK_STS_IOERR;
1681 }
1682 spin_unlock_irqrestore(&m->lock, flags);
1683 goto done;
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -05001684 }
Mike Snitzera271a892020-06-10 16:07:57 -04001685 spin_unlock_irqrestore(&m->lock, flags);
Christoph Hellwig18a482f2017-05-15 17:28:37 +02001686 }
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001687
Mike Snitzerf45f1182020-06-10 20:03:19 -04001688 multipath_queue_bio(m, clone);
Christoph Hellwig1be56902017-06-03 09:38:03 +02001689 r = DM_ENDIO_INCOMPLETE;
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001690done:
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001691 if (pgpath) {
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001692 struct path_selector *ps = &pgpath->pg->ps;
1693
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001694 if (ps->type->end_io)
Gabriel Krisman Bertazi087615bf2020-04-30 16:48:29 -04001695 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes,
1696 dm_start_time_ns_from_clone(clone));
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001697 }
1698
Christoph Hellwig1be56902017-06-03 09:38:03 +02001699 return r;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001700}
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702/*
Mike Snitzer553ec942020-05-27 16:32:51 -04001703 * Suspend with flush can't complete until all the I/O is processed
1704 * so if the last path fails we must error any remaining I/O.
1705 * - Note that if the freeze_bdev fails while suspending, the
1706 * queue_if_no_path state is lost - userspace should reset it.
1707 * Otherwise, during noflush suspend, queue_if_no_path will not change.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 */
1709static void multipath_presuspend(struct dm_target *ti)
1710{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001711 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Mike Snitzer553ec942020-05-27 16:32:51 -04001713 /* FIXME: bio-based shouldn't need to always disable queue_if_no_path */
1714 if (m->queue_mode == DM_TYPE_BIO_BASED || !dm_noflush_suspending(m->ti))
Mike Snitzer4c3f4832020-05-29 15:59:13 -04001715 queue_if_no_path(m, false, true, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001718static void multipath_postsuspend(struct dm_target *ti)
1719{
Mike Anderson6380f262009-12-10 23:52:21 +00001720 struct multipath *m = ti->private;
1721
1722 mutex_lock(&m->work_mutex);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001723 flush_multipath_work(m);
Mike Anderson6380f262009-12-10 23:52:21 +00001724 mutex_unlock(&m->work_mutex);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001725}
1726
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001727/*
1728 * Restore the queue_if_no_path setting.
1729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730static void multipath_resume(struct dm_target *ti)
1731{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001732 struct multipath *m = ti->private;
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001733 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001735 spin_lock_irqsave(&m->lock, flags);
Mike Snitzer553ec942020-05-27 16:32:51 -04001736 if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) {
1737 set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
1738 clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
1739 }
Mike Snitzer4c3f4832020-05-29 15:59:13 -04001740
1741 DMDEBUG("%s: %s finished; QIFNP = %d; SQIFNP = %d",
1742 dm_device_name(dm_table_get_md(m->ti->table)), __func__,
1743 test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags),
1744 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags));
1745
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001746 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
1749/*
1750 * Info output has the following format:
1751 * num_multipath_feature_args [multipath_feature_args]*
1752 * num_handler_status_args [handler_status_args]*
1753 * num_groups init_group_number
1754 * [A|D|E num_ps_status_args [ps_status_args]*
1755 * num_paths num_selector_args
1756 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1757 *
1758 * Table output has the following format (identical to the constructor string):
1759 * num_feature_args [features_args]*
1760 * num_handler_args hw_handler [hw_handler_args]*
1761 * num_groups init_group_number
1762 * [priority selector-name num_ps_args [ps_args]*
1763 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1764 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001765static void multipath_status(struct dm_target *ti, status_type_t type,
1766 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
1768 int sz = 0;
1769 unsigned long flags;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001770 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 struct priority_group *pg;
1772 struct pgpath *p;
1773 unsigned pg_num;
1774 char state;
1775
1776 spin_lock_irqsave(&m->lock, flags);
1777
1778 /* Features */
1779 if (type == STATUSTYPE_INFO)
Mike Snitzer91e968a2016-03-17 17:10:15 -04001780 DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
1781 atomic_read(&m->pg_init_count));
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001782 else {
Mike Snitzer518257b2016-03-17 16:32:10 -04001783 DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001784 (m->pg_init_retries > 0) * 2 +
Mike Snitzera58a9352012-07-27 15:08:04 +01001785 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
Mike Snitzere83068a2016-05-24 21:16:51 -04001786 test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
1787 (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
1788
Mike Snitzer518257b2016-03-17 16:32:10 -04001789 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001790 DMEMIT("queue_if_no_path ");
1791 if (m->pg_init_retries)
1792 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001793 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1794 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
Mike Snitzer518257b2016-03-17 16:32:10 -04001795 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
Mike Snitzera58a9352012-07-27 15:08:04 +01001796 DMEMIT("retain_attached_hw_handler ");
Mike Snitzere83068a2016-05-24 21:16:51 -04001797 if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
1798 switch(m->queue_mode) {
1799 case DM_TYPE_BIO_BASED:
1800 DMEMIT("queue_mode bio ");
1801 break;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07001802 default:
1803 WARN_ON_ONCE(true);
1804 break;
Mike Snitzere83068a2016-05-24 21:16:51 -04001805 }
1806 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001809 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 DMEMIT("0 ");
1811 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001812 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 DMEMIT("%u ", m->nr_priority_groups);
1815
1816 if (m->next_pg)
1817 pg_num = m->next_pg->pg_num;
1818 else if (m->current_pg)
1819 pg_num = m->current_pg->pg_num;
1820 else
Mike Snitzera490a072011-03-24 13:54:33 +00001821 pg_num = (m->nr_priority_groups ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 DMEMIT("%u ", pg_num);
1824
1825 switch (type) {
1826 case STATUSTYPE_INFO:
1827 list_for_each_entry(pg, &m->priority_groups, list) {
1828 if (pg->bypassed)
1829 state = 'D'; /* Disabled */
1830 else if (pg == m->current_pg)
1831 state = 'A'; /* Currently Active */
1832 else
1833 state = 'E'; /* Enabled */
1834
1835 DMEMIT("%c ", state);
1836
1837 if (pg->ps.type->status)
1838 sz += pg->ps.type->status(&pg->ps, NULL, type,
1839 result + sz,
1840 maxlen - sz);
1841 else
1842 DMEMIT("0 ");
1843
1844 DMEMIT("%u %u ", pg->nr_pgpaths,
1845 pg->ps.type->info_args);
1846
1847 list_for_each_entry(p, &pg->pgpaths, list) {
1848 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001849 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 p->fail_count);
1851 if (pg->ps.type->status)
1852 sz += pg->ps.type->status(&pg->ps,
1853 &p->path, type, result + sz,
1854 maxlen - sz);
1855 }
1856 }
1857 break;
1858
1859 case STATUSTYPE_TABLE:
1860 list_for_each_entry(pg, &m->priority_groups, list) {
1861 DMEMIT("%s ", pg->ps.type->name);
1862
1863 if (pg->ps.type->status)
1864 sz += pg->ps.type->status(&pg->ps, NULL, type,
1865 result + sz,
1866 maxlen - sz);
1867 else
1868 DMEMIT("0 ");
1869
1870 DMEMIT("%u %u ", pg->nr_pgpaths,
1871 pg->ps.type->table_args);
1872
1873 list_for_each_entry(p, &pg->pgpaths, list) {
1874 DMEMIT("%s ", p->path.dev->name);
1875 if (pg->ps.type->status)
1876 sz += pg->ps.type->status(&pg->ps,
1877 &p->path, type, result + sz,
1878 maxlen - sz);
1879 }
1880 }
1881 break;
1882 }
1883
1884 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Mike Snitzer1eb5fa82018-02-28 15:59:59 -05001887static int multipath_message(struct dm_target *ti, unsigned argc, char **argv,
1888 char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Mike Anderson6380f262009-12-10 23:52:21 +00001890 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 struct dm_dev *dev;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001892 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 action_fn action;
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001894 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Mike Anderson6380f262009-12-10 23:52:21 +00001896 mutex_lock(&m->work_mutex);
1897
Kiyoshi Uedac2f3d242009-12-10 23:52:27 +00001898 if (dm_suspended(ti)) {
1899 r = -EBUSY;
1900 goto out;
1901 }
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 if (argc == 1) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001904 if (!strcasecmp(argv[0], "queue_if_no_path")) {
Mike Snitzer4c3f4832020-05-29 15:59:13 -04001905 r = queue_if_no_path(m, true, false, __func__);
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001906 spin_lock_irqsave(&m->lock, flags);
1907 enable_nopath_timeout(m);
1908 spin_unlock_irqrestore(&m->lock, flags);
Mike Anderson6380f262009-12-10 23:52:21 +00001909 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001910 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
Mike Snitzer4c3f4832020-05-29 15:59:13 -04001911 r = queue_if_no_path(m, false, false, __func__);
Anatol Pomazaube240ff2020-01-13 17:41:27 -05001912 disable_nopath_timeout(m);
Mike Anderson6380f262009-12-10 23:52:21 +00001913 goto out;
1914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916
Mike Anderson6380f262009-12-10 23:52:21 +00001917 if (argc != 2) {
Jose Castilloa356e422014-01-29 17:52:45 +01001918 DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
Mike Anderson6380f262009-12-10 23:52:21 +00001919 goto out;
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Mike Snitzer498f0102011-08-02 12:32:04 +01001922 if (!strcasecmp(argv[0], "disable_group")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001923 r = bypass_pg_num(m, argv[1], true);
Mike Anderson6380f262009-12-10 23:52:21 +00001924 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001925 } else if (!strcasecmp(argv[0], "enable_group")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001926 r = bypass_pg_num(m, argv[1], false);
Mike Anderson6380f262009-12-10 23:52:21 +00001927 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001928 } else if (!strcasecmp(argv[0], "switch_group")) {
Mike Anderson6380f262009-12-10 23:52:21 +00001929 r = switch_pg_num(m, argv[1]);
1930 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001931 } else if (!strcasecmp(argv[0], "reinstate_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 action = reinstate_path;
Mike Snitzer498f0102011-08-02 12:32:04 +01001933 else if (!strcasecmp(argv[0], "fail_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 action = fail_path;
Mike Anderson6380f262009-12-10 23:52:21 +00001935 else {
Jose Castilloa356e422014-01-29 17:52:45 +01001936 DMWARN("Unrecognised multipath message received: %s", argv[0]);
Mike Anderson6380f262009-12-10 23:52:21 +00001937 goto out;
1938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00001940 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001942 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 argv[1]);
Mike Anderson6380f262009-12-10 23:52:21 +00001944 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946
1947 r = action_dev(m, dev, action);
1948
1949 dm_put_device(ti, dev);
1950
Mike Anderson6380f262009-12-10 23:52:21 +00001951out:
1952 mutex_unlock(&m->work_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001956static int multipath_prepare_ioctl(struct dm_target *ti,
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -04001957 struct block_device **bdev)
Milan Broz9af4aa32006-10-03 01:15:20 -07001958{
Mikulas Patocka35991652012-06-03 00:29:58 +01001959 struct multipath *m = ti->private;
Mike Snitzer2da16102016-03-17 18:38:17 -04001960 struct pgpath *current_pgpath;
Mike Snitzer69cea0d2020-06-10 15:02:37 -04001961 unsigned long flags;
Mikulas Patocka35991652012-06-03 00:29:58 +01001962 int r;
1963
Will Deacon506458e2017-10-24 11:22:48 +01001964 current_pgpath = READ_ONCE(m->current_pgpath);
Martin Wilck2361ae52020-04-20 22:29:09 +02001965 if (!current_pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
Mike Snitzer2da16102016-03-17 18:38:17 -04001966 current_pgpath = choose_pgpath(m, 0);
Milan Broz9af4aa32006-10-03 01:15:20 -07001967
Mike Snitzer2da16102016-03-17 18:38:17 -04001968 if (current_pgpath) {
Mike Snitzer518257b2016-03-17 16:32:10 -04001969 if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
Mike Snitzer2da16102016-03-17 18:38:17 -04001970 *bdev = current_pgpath->path.dev->bdev;
Junichi Nomura43e43c92015-11-17 09:36:56 +00001971 r = 0;
1972 } else {
1973 /* pg_init has not started or completed */
1974 r = -ENOTCONN;
1975 }
1976 } else {
1977 /* No path is available */
Mike Snitzera271a892020-06-10 16:07:57 -04001978 r = -EIO;
1979 spin_lock_irqsave(&m->lock, flags);
Mike Snitzer518257b2016-03-17 16:32:10 -04001980 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Junichi Nomura43e43c92015-11-17 09:36:56 +00001981 r = -ENOTCONN;
Mike Snitzera271a892020-06-10 16:07:57 -04001982 spin_unlock_irqrestore(&m->lock, flags);
Milan Broze90dae12006-10-03 01:15:22 -07001983 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001984
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +00001985 if (r == -ENOTCONN) {
Will Deacon506458e2017-10-24 11:22:48 +01001986 if (!READ_ONCE(m->current_pg)) {
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001987 /* Path status changed, redo selection */
Mike Snitzer2da16102016-03-17 18:38:17 -04001988 (void) choose_pgpath(m, 0);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001989 }
Mike Snitzer69cea0d2020-06-10 15:02:37 -04001990 spin_lock_irqsave(&m->lock, flags);
Mike Snitzer518257b2016-03-17 16:32:10 -04001991 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
Mike Snitzer69cea0d2020-06-10 15:02:37 -04001992 (void) __pg_init_all_paths(m);
1993 spin_unlock_irqrestore(&m->lock, flags);
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001994 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -04001995 process_queued_io_list(m);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001996 }
Mikulas Patocka35991652012-06-03 00:29:58 +01001997
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001998 /*
1999 * Only pass ioctls through if the device sizes match exactly.
2000 */
2001 if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
2002 return 1;
2003 return r;
Milan Broz9af4aa32006-10-03 01:15:20 -07002004}
2005
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002006static int multipath_iterate_devices(struct dm_target *ti,
2007 iterate_devices_callout_fn fn, void *data)
2008{
2009 struct multipath *m = ti->private;
2010 struct priority_group *pg;
2011 struct pgpath *p;
2012 int ret = 0;
2013
2014 list_for_each_entry(pg, &m->priority_groups, list) {
2015 list_for_each_entry(p, &pg->pgpaths, list) {
Mike Snitzer5dea2712009-07-23 20:30:42 +01002016 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002017 if (ret)
2018 goto out;
2019 }
2020 }
2021
2022out:
2023 return ret;
2024}
2025
Mike Snitzer9f54cec2016-02-11 21:42:28 -05002026static int pgpath_busy(struct pgpath *pgpath)
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002027{
2028 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
2029
Mike Snitzer52b09912015-02-23 16:36:41 -05002030 return blk_lld_busy(q);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002031}
2032
2033/*
2034 * We return "busy", only when we can map I/Os but underlying devices
2035 * are busy (so even if we map I/Os now, the I/Os will wait on
2036 * the underlying queue).
2037 * In other words, if we want to kill I/Os or queue them inside us
2038 * due to map unavailability, we don't return "busy". Otherwise,
2039 * dm core won't give us the I/Os and we can't do what we want.
2040 */
2041static int multipath_busy(struct dm_target *ti)
2042{
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002043 bool busy = false, has_active = false;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002044 struct multipath *m = ti->private;
Mike Snitzer2da16102016-03-17 18:38:17 -04002045 struct priority_group *pg, *next_pg;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002046 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002047
Mike Snitzerb88efd42016-09-09 19:26:19 -04002048 /* pg_init in progress */
2049 if (atomic_read(&m->pg_init_in_progress))
Mike Snitzer2da16102016-03-17 18:38:17 -04002050 return true;
2051
Mike Snitzerb88efd42016-09-09 19:26:19 -04002052 /* no paths available, for blk-mq: rely on IO mapping to delay requeue */
Mike Snitzera271a892020-06-10 16:07:57 -04002053 if (!atomic_read(&m->nr_valid_paths)) {
2054 unsigned long flags;
2055 spin_lock_irqsave(&m->lock, flags);
2056 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
2057 spin_unlock_irqrestore(&m->lock, flags);
2058 return (m->queue_mode != DM_TYPE_REQUEST_BASED);
2059 }
2060 spin_unlock_irqrestore(&m->lock, flags);
2061 }
Mike Snitzerb88efd42016-09-09 19:26:19 -04002062
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002063 /* Guess which priority_group will be used at next mapping time */
Will Deacon506458e2017-10-24 11:22:48 +01002064 pg = READ_ONCE(m->current_pg);
2065 next_pg = READ_ONCE(m->next_pg);
2066 if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg))
Mike Snitzer2da16102016-03-17 18:38:17 -04002067 pg = next_pg;
2068
2069 if (!pg) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002070 /*
2071 * We don't know which pg will be used at next mapping time.
Mike Snitzer2da16102016-03-17 18:38:17 -04002072 * We don't call choose_pgpath() here to avoid to trigger
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002073 * pg_init just by busy checking.
2074 * So we don't know whether underlying devices we will be using
2075 * at next mapping time are busy or not. Just try mapping.
2076 */
Mike Snitzer2da16102016-03-17 18:38:17 -04002077 return busy;
2078 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002079
2080 /*
2081 * If there is one non-busy active path at least, the path selector
2082 * will be able to select it. So we consider such a pg as not busy.
2083 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002084 busy = true;
Mike Snitzer2da16102016-03-17 18:38:17 -04002085 list_for_each_entry(pgpath, &pg->pgpaths, list) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002086 if (pgpath->is_active) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002087 has_active = true;
Mike Snitzer9f54cec2016-02-11 21:42:28 -05002088 if (!pgpath_busy(pgpath)) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002089 busy = false;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002090 break;
2091 }
2092 }
Mike Snitzer2da16102016-03-17 18:38:17 -04002093 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002094
Mike Snitzer2da16102016-03-17 18:38:17 -04002095 if (!has_active) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002096 /*
2097 * No active path in this pg, so this pg won't be used and
2098 * the current_pg will be changed at next mapping time.
2099 * We need to try mapping to determine it.
2100 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002101 busy = false;
Mike Snitzer2da16102016-03-17 18:38:17 -04002102 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002103
2104 return busy;
2105}
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107/*-----------------------------------------------------------------
2108 * Module setup
2109 *---------------------------------------------------------------*/
2110static struct target_type multipath_target = {
2111 .name = "multipath",
Mike Snitzer636be422020-02-27 14:25:31 -05002112 .version = {1, 14, 0},
Steffen Maier8c5c1472018-03-14 15:33:06 +01002113 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE |
2114 DM_TARGET_PASSES_INTEGRITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 .module = THIS_MODULE,
2116 .ctr = multipath_ctr,
2117 .dtr = multipath_dtr,
Mike Snitzere5863d92014-12-17 21:08:12 -05002118 .clone_and_map_rq = multipath_clone_and_map,
2119 .release_clone_rq = multipath_release_clone,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002120 .rq_end_io = multipath_end_io,
Mike Snitzer76e33fe2016-05-19 16:15:14 -04002121 .map = multipath_map_bio,
2122 .end_io = multipath_end_io_bio,
2123 .presuspend = multipath_presuspend,
2124 .postsuspend = multipath_postsuspend,
2125 .resume = multipath_resume,
2126 .status = multipath_status,
2127 .message = multipath_message,
2128 .prepare_ioctl = multipath_prepare_ioctl,
2129 .iterate_devices = multipath_iterate_devices,
2130 .busy = multipath_busy,
2131};
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133static int __init dm_multipath_init(void)
2134{
2135 int r;
2136
Tejun Heo4d4d66a2011-01-13 19:59:57 +00002137 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002138 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01002139 DMERR("failed to create workqueue kmpathd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002140 r = -ENOMEM;
2141 goto bad_alloc_kmultipathd;
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002142 }
2143
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002144 /*
2145 * A separate workqueue is used to handle the device handlers
2146 * to avoid overloading existing workqueue. Overloading the
2147 * old workqueue would also create a bottleneck in the
2148 * path of the storage hardware device activation.
2149 */
Tejun Heo4d4d66a2011-01-13 19:59:57 +00002150 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
2151 WQ_MEM_RECLAIM);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002152 if (!kmpath_handlerd) {
2153 DMERR("failed to create workqueue kmpath_handlerd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002154 r = -ENOMEM;
2155 goto bad_alloc_kmpath_handlerd;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002156 }
2157
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002158 r = dm_register_target(&multipath_target);
2159 if (r < 0) {
2160 DMERR("request-based register failed %d", r);
2161 r = -EINVAL;
2162 goto bad_register_target;
2163 }
2164
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002165 return 0;
2166
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002167bad_register_target:
2168 destroy_workqueue(kmpath_handlerd);
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002169bad_alloc_kmpath_handlerd:
2170 destroy_workqueue(kmultipathd);
2171bad_alloc_kmultipathd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return r;
2173}
2174
2175static void __exit dm_multipath_exit(void)
2176{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002177 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002178 destroy_workqueue(kmultipathd);
2179
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002180 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183module_init(dm_multipath_init);
2184module_exit(dm_multipath_exit);
2185
Anatol Pomazaube240ff2020-01-13 17:41:27 -05002186module_param_named(queue_if_no_path_timeout_secs,
2187 queue_if_no_path_timeout_secs, ulong, S_IRUGO | S_IWUSR);
2188MODULE_PARM_DESC(queue_if_no_path_timeout_secs, "No available paths queue IO timeout in seconds");
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190MODULE_DESCRIPTION(DM_NAME " multipath target");
2191MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
2192MODULE_LICENSE("GPL");