blob: dbcc1e41cd57dd5a669466a6907a70f78a16014f [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>
23#include <linux/workqueue.h>
Mikulas Patocka35991652012-06-03 00:29:58 +010024#include <linux/delay.h>
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070025#include <scsi/scsi_dh.h>
Arun Sharma600634972011-07-26 16:09:06 -070026#include <linux/atomic.h>
Mike Snitzer78ce23b2016-01-31 17:38:28 -050027#include <linux/blk-mq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Alasdair G Kergon72d94862006-06-26 00:27:35 -070029#define DM_MSG_PREFIX "multipath"
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000030#define DM_PG_INIT_DELAY_MSECS 2000
31#define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/* Path properties */
34struct pgpath {
35 struct list_head list;
36
37 struct priority_group *pg; /* Owning PG */
38 unsigned fail_count; /* Cumulative failure count */
39
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080040 struct dm_path path;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000041 struct delayed_work activate_path;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -050042
43 bool is_active:1; /* Path status */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
46#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
47
48/*
49 * Paths are grouped into Priority Groups and numbered from 1 upwards.
50 * Each has a path selector which controls which path gets used.
51 */
52struct priority_group {
53 struct list_head list;
54
55 struct multipath *m; /* Owning multipath instance */
56 struct path_selector ps;
57
58 unsigned pg_num; /* Reference number */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned nr_pgpaths; /* Number of paths in PG */
60 struct list_head pgpaths;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -050061
62 bool bypassed:1; /* Temporarily bypass this PG? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65/* Multipath context */
66struct multipath {
Mike Snitzer848b8ae2017-12-10 15:37:21 -050067 unsigned long flags; /* Multipath state flags */
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000068
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010069 spinlock_t lock;
Mike Snitzer848b8ae2017-12-10 15:37:21 -050070 enum dm_queue_mode queue_mode;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 struct pgpath *current_pgpath;
73 struct priority_group *current_pg;
74 struct priority_group *next_pg; /* Switch to this PG if set */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Mike Snitzer848b8ae2017-12-10 15:37:21 -050076 atomic_t nr_valid_paths; /* Total number of usable paths */
77 unsigned nr_priority_groups;
78 struct list_head priority_groups;
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010079
Mike Snitzer848b8ae2017-12-10 15:37:21 -050080 const char *hw_handler_name;
81 char *hw_handler_params;
82 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
Dave Wysochanskic9e45582007-10-19 22:47:53 +010083 unsigned pg_init_retries; /* Number of times to retry pg_init */
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000084 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
Mike Snitzer91e968a2016-03-17 17:10:15 -040085 atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */
86 atomic_t pg_init_count; /* Number of times pg_init called */
87
Mike Anderson6380f262009-12-10 23:52:21 +000088 struct mutex work_mutex;
Mike Snitzer20800cb2016-03-17 17:13:10 -040089 struct work_struct trigger_event;
Mike Snitzer848b8ae2017-12-10 15:37:21 -050090 struct dm_target *ti;
Mike Snitzer76e33fe2016-05-19 16:15:14 -040091
92 struct work_struct process_queued_bios;
93 struct bio_list queued_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094};
95
96/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -040097 * Context information attached to each io we process.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010099struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 struct pgpath *pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100101 size_t nr_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
104typedef int (*action_fn) (struct pgpath *pgpath);
105
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700106static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000107static void trigger_event(struct work_struct *work);
Bart Van Assche89bfce72017-04-27 10:11:14 -0700108static void activate_or_offline_path(struct pgpath *pgpath);
109static void activate_path_work(struct work_struct *work);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400110static void process_queued_bios(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Mike Snitzer518257b2016-03-17 16:32:10 -0400112/*-----------------------------------------------
113 * Multipath state flags.
114 *-----------------------------------------------*/
115
116#define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */
117#define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */
118#define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */
119#define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */
120#define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */
121#define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */
122#define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124/*-----------------------------------------------
125 * Allocation routines
126 *-----------------------------------------------*/
127
128static struct pgpath *alloc_pgpath(void)
129{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700130 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500132 if (!pgpath)
133 return NULL;
134
135 pgpath->is_active = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 return pgpath;
138}
139
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100140static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 kfree(pgpath);
143}
144
145static struct priority_group *alloc_priority_group(void)
146{
147 struct priority_group *pg;
148
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700149 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700151 if (pg)
152 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 return pg;
155}
156
157static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
158{
159 struct pgpath *pgpath, *tmp;
160
161 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
162 list_del(&pgpath->list);
163 dm_put_device(ti, pgpath->path.dev);
164 free_pgpath(pgpath);
165 }
166}
167
168static void free_priority_group(struct priority_group *pg,
169 struct dm_target *ti)
170{
171 struct path_selector *ps = &pg->ps;
172
173 if (ps->type) {
174 ps->type->destroy(ps);
175 dm_put_path_selector(ps->type);
176 }
177
178 free_pgpaths(&pg->pgpaths, ti);
179 kfree(pg);
180}
181
Mike Snitzere83068a2016-05-24 21:16:51 -0400182static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct multipath *m;
185
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700186 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 INIT_LIST_HEAD(&m->priority_groups);
189 spin_lock_init(&m->lock);
Mike Snitzer91e968a2016-03-17 17:10:15 -0400190 atomic_set(&m->nr_valid_paths, 0);
David Howellsc4028952006-11-22 14:57:56 +0000191 INIT_WORK(&m->trigger_event, trigger_event);
Mike Anderson6380f262009-12-10 23:52:21 +0000192 mutex_init(&m->work_mutex);
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500193
Mike Snitzere83068a2016-05-24 21:16:51 -0400194 m->queue_mode = DM_TYPE_NONE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400195
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700196 m->ti = ti;
197 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
200 return m;
201}
202
Mike Snitzere83068a2016-05-24 21:16:51 -0400203static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m)
204{
205 if (m->queue_mode == DM_TYPE_NONE) {
Mike Snitzer953923c2018-10-11 11:06:29 -0400206 m->queue_mode = DM_TYPE_REQUEST_BASED;
Mike Snitzer8d47e652018-03-05 14:10:11 -0500207 } else if (m->queue_mode == DM_TYPE_BIO_BASED) {
Mike Snitzere83068a2016-05-24 21:16:51 -0400208 INIT_WORK(&m->process_queued_bios, process_queued_bios);
Mike Snitzer8d47e652018-03-05 14:10:11 -0500209 /*
210 * bio-based doesn't support any direct scsi_dh management;
211 * it just discovers if a scsi_dh is attached.
212 */
213 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
Mike Snitzere83068a2016-05-24 21:16:51 -0400214 }
215
216 dm_table_set_type(ti->table, m->queue_mode);
217
Mike Snitzerc3736672018-03-12 20:30:43 -0400218 /*
219 * Init fields that are only used when a scsi_dh is attached
220 * - must do this unconditionally (really doesn't hurt non-SCSI uses)
221 */
222 set_bit(MPATHF_QUEUE_IO, &m->flags);
223 atomic_set(&m->pg_init_in_progress, 0);
224 atomic_set(&m->pg_init_count, 0);
225 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
226 init_waitqueue_head(&m->pg_init_wait);
227
Mike Snitzere83068a2016-05-24 21:16:51 -0400228 return 0;
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static void free_multipath(struct multipath *m)
232{
233 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
236 list_del(&pg->list);
237 free_priority_group(pg, m->ti);
238 }
239
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700240 kfree(m->hw_handler_name);
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700241 kfree(m->hw_handler_params);
Mike Snitzerd5ffebd2018-01-05 21:17:20 -0500242 mutex_destroy(&m->work_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 kfree(m);
244}
245
Mike Snitzer2eff1922016-02-03 09:13:14 -0500246static struct dm_mpath_io *get_mpio(union map_info *info)
247{
248 return info->ptr;
249}
250
Mike Snitzerbf661be2016-05-24 15:48:08 -0400251static size_t multipath_per_bio_data_size(void)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400252{
Mike Snitzerbf661be2016-05-24 15:48:08 -0400253 return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400254}
255
Mike Snitzerbf661be2016-05-24 15:48:08 -0400256static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
257{
258 return dm_per_bio_data(bio, multipath_per_bio_data_size());
259}
260
Mike Snitzerd07a2412017-12-11 16:08:54 -0500261static struct dm_bio_details *get_bio_details_from_mpio(struct dm_mpath_io *mpio)
Mike Snitzerbf661be2016-05-24 15:48:08 -0400262{
263 /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */
Mike Snitzerbf661be2016-05-24 15:48:08 -0400264 void *bio_details = mpio + 1;
Mike Snitzerbf661be2016-05-24 15:48:08 -0400265 return bio_details;
266}
267
Mike Snitzer63f6e6f2017-12-05 14:10:33 -0500268static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400269{
270 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
Mike Snitzerd07a2412017-12-11 16:08:54 -0500271 struct dm_bio_details *bio_details = get_bio_details_from_mpio(mpio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400272
Mike Snitzerd0442f82017-12-11 15:58:41 -0500273 mpio->nr_bytes = bio->bi_iter.bi_size;
274 mpio->pgpath = NULL;
275 *mpio_p = mpio;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400276
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400277 dm_bio_record(bio_details, bio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/*-----------------------------------------------
281 * Path selection
282 *-----------------------------------------------*/
283
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100284static int __pg_init_all_paths(struct multipath *m)
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000285{
286 struct pgpath *pgpath;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000287 unsigned long pg_init_delay = 0;
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000288
Bart Van Asscheb1946792017-04-27 10:11:22 -0700289 lockdep_assert_held(&m->lock);
290
Mike Snitzer91e968a2016-03-17 17:10:15 -0400291 if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100292 return 0;
Hannes Reinecke17f4ff42014-02-28 15:33:42 +0100293
Mike Snitzer91e968a2016-03-17 17:10:15 -0400294 atomic_inc(&m->pg_init_count);
Mike Snitzer518257b2016-03-17 16:32:10 -0400295 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100296
297 /* Check here to reset pg_init_required */
298 if (!m->current_pg)
299 return 0;
300
Mike Snitzer518257b2016-03-17 16:32:10 -0400301 if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000302 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
303 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000304 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
305 /* Skip failed paths */
306 if (!pgpath->is_active)
307 continue;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000308 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
309 pg_init_delay))
Mike Snitzer91e968a2016-03-17 17:10:15 -0400310 atomic_inc(&m->pg_init_in_progress);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000311 }
Mike Snitzer91e968a2016-03-17 17:10:15 -0400312 return atomic_read(&m->pg_init_in_progress);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000313}
314
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700315static int pg_init_all_paths(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700317 int ret;
Mike Snitzer2da16102016-03-17 18:38:17 -0400318 unsigned long flags;
319
320 spin_lock_irqsave(&m->lock, flags);
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700321 ret = __pg_init_all_paths(m);
Mike Snitzer2da16102016-03-17 18:38:17 -0400322 spin_unlock_irqrestore(&m->lock, flags);
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700323
324 return ret;
Mike Snitzer2da16102016-03-17 18:38:17 -0400325}
326
327static void __switch_pg(struct multipath *m, struct priority_group *pg)
328{
329 m->current_pg = pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700332 if (m->hw_handler_name) {
Mike Snitzer518257b2016-03-17 16:32:10 -0400333 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
334 set_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 } else {
Mike Snitzer518257b2016-03-17 16:32:10 -0400336 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
337 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100339
Mike Snitzer91e968a2016-03-17 17:10:15 -0400340 atomic_set(&m->pg_init_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Mike Snitzer2da16102016-03-17 18:38:17 -0400343static struct pgpath *choose_path_in_pg(struct multipath *m,
344 struct priority_group *pg,
345 size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Mike Snitzer2da16102016-03-17 18:38:17 -0400347 unsigned long flags;
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800348 struct dm_path *path;
Mike Snitzer2da16102016-03-17 18:38:17 -0400349 struct pgpath *pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Mike Snitzer90a43232016-02-17 21:29:17 -0500351 path = pg->ps.type->select_path(&pg->ps, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (!path)
Mike Snitzer2da16102016-03-17 18:38:17 -0400353 return ERR_PTR(-ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Mike Snitzer2da16102016-03-17 18:38:17 -0400355 pgpath = path_to_pgpath(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Will Deacon506458e2017-10-24 11:22:48 +0100357 if (unlikely(READ_ONCE(m->current_pg) != pg)) {
Mike Snitzer2da16102016-03-17 18:38:17 -0400358 /* Only update current_pgpath if pg changed */
359 spin_lock_irqsave(&m->lock, flags);
360 m->current_pgpath = pgpath;
361 __switch_pg(m, pg);
362 spin_unlock_irqrestore(&m->lock, flags);
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Mike Snitzer2da16102016-03-17 18:38:17 -0400365 return pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Mike Snitzer2da16102016-03-17 18:38:17 -0400368static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Mike Snitzer2da16102016-03-17 18:38:17 -0400370 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct priority_group *pg;
Mike Snitzer2da16102016-03-17 18:38:17 -0400372 struct pgpath *pgpath;
Mike Snitzerd19a55c2017-01-06 15:33:14 -0500373 unsigned bypassed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Mike Snitzer91e968a2016-03-17 17:10:15 -0400375 if (!atomic_read(&m->nr_valid_paths)) {
Mike Snitzer8d47e652018-03-05 14:10:11 -0500376 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 goto failed;
Benjamin Marzinski1f271972014-08-13 13:53:42 -0500378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 /* Were we instructed to switch PG? */
Will Deacon506458e2017-10-24 11:22:48 +0100381 if (READ_ONCE(m->next_pg)) {
Mike Snitzer2da16102016-03-17 18:38:17 -0400382 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 pg = m->next_pg;
Mike Snitzer2da16102016-03-17 18:38:17 -0400384 if (!pg) {
385 spin_unlock_irqrestore(&m->lock, flags);
386 goto check_current_pg;
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 m->next_pg = NULL;
Mike Snitzer2da16102016-03-17 18:38:17 -0400389 spin_unlock_irqrestore(&m->lock, flags);
390 pgpath = choose_path_in_pg(m, pg, nr_bytes);
391 if (!IS_ERR_OR_NULL(pgpath))
392 return pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
395 /* Don't change PG until it has no remaining paths */
Mike Snitzer2da16102016-03-17 18:38:17 -0400396check_current_pg:
Will Deacon506458e2017-10-24 11:22:48 +0100397 pg = READ_ONCE(m->current_pg);
Mike Snitzer2da16102016-03-17 18:38:17 -0400398 if (pg) {
399 pgpath = choose_path_in_pg(m, pg, nr_bytes);
400 if (!IS_ERR_OR_NULL(pgpath))
401 return pgpath;
402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /*
405 * Loop through priority groups until we find a valid path.
406 * First time we skip PGs marked 'bypassed'.
Mike Christief220fd42012-06-03 00:29:45 +0100407 * Second time we only try the ones we skipped, but set
408 * pg_init_delay_retry so we do not hammer controllers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
410 do {
411 list_for_each_entry(pg, &m->priority_groups, list) {
Mike Snitzerd19a55c2017-01-06 15:33:14 -0500412 if (pg->bypassed == !!bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 continue;
Mike Snitzer2da16102016-03-17 18:38:17 -0400414 pgpath = choose_path_in_pg(m, pg, nr_bytes);
415 if (!IS_ERR_OR_NULL(pgpath)) {
Mike Christief220fd42012-06-03 00:29:45 +0100416 if (!bypassed)
Mike Snitzer518257b2016-03-17 16:32:10 -0400417 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
Mike Snitzer2da16102016-03-17 18:38:17 -0400418 return pgpath;
Mike Christief220fd42012-06-03 00:29:45 +0100419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421 } while (bypassed--);
422
423failed:
Mike Snitzer2da16102016-03-17 18:38:17 -0400424 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 m->current_pgpath = NULL;
426 m->current_pg = NULL;
Mike Snitzer2da16102016-03-17 18:38:17 -0400427 spin_unlock_irqrestore(&m->lock, flags);
428
429 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800432/*
Bart Van Assche86331f32017-04-27 10:11:26 -0700433 * dm_report_EIO() is a macro instead of a function to make pr_debug()
434 * report the function name and line number of the function from which
435 * it has been invoked.
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800436 */
Bart Van Assche86331f32017-04-27 10:11:26 -0700437#define dm_report_EIO(m) \
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200438do { \
Bart Van Assche86331f32017-04-27 10:11:26 -0700439 struct mapped_device *md = dm_table_get_md((m)->ti->table); \
440 \
441 pr_debug("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d\n", \
442 dm_device_name(md), \
443 test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags), \
444 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags), \
445 dm_noflush_suspending((m)->ti)); \
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200446} while (0)
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800447
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100448/*
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500449 * Check whether bios must be queued in the device-mapper core rather
450 * than here in the target.
451 *
452 * If MPATHF_QUEUE_IF_NO_PATH and MPATHF_SAVED_QUEUE_IF_NO_PATH hold
453 * the same value then we are not between multipath_presuspend()
454 * and multipath_resume() calls and we have no need to check
455 * for the DMF_NOFLUSH_SUSPENDING flag.
456 */
457static bool __must_push_back(struct multipath *m, unsigned long flags)
458{
459 return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) !=
460 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &flags)) &&
461 dm_noflush_suspending(m->ti));
462}
463
464/*
465 * Following functions use READ_ONCE to get atomic access to
466 * all m->flags to avoid taking spinlock
467 */
468static bool must_push_back_rq(struct multipath *m)
469{
470 unsigned long flags = READ_ONCE(m->flags);
471 return test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) || __must_push_back(m, flags);
472}
473
474static bool must_push_back_bio(struct multipath *m)
475{
476 unsigned long flags = READ_ONCE(m->flags);
477 return __must_push_back(m, flags);
478}
479
480/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400481 * Map cloned requests (request-based multipath)
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100482 */
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100483static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
484 union map_info *map_context,
485 struct request **__clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Mike Snitzer7943bd62016-02-02 21:53:15 -0500487 struct multipath *m = ti->private;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100488 size_t nr_bytes = blk_rq_bytes(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100490 struct block_device *bdev;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100491 struct dm_mpath_io *mpio = get_mpio(map_context);
Bart Van Assche7083abb2017-04-27 10:11:15 -0700492 struct request_queue *q;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100493 struct request *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /* Do we need to select a new pgpath? */
Will Deacon506458e2017-10-24 11:22:48 +0100496 pgpath = READ_ONCE(m->current_pgpath);
Mike Snitzer2da16102016-03-17 18:38:17 -0400497 if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
498 pgpath = choose_pgpath(m, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100500 if (!pgpath) {
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500501 if (must_push_back_rq(m))
Mike Snitzerb88efd42016-09-09 19:26:19 -0400502 return DM_MAPIO_DELAY_REQUEUE;
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200503 dm_report_EIO(m); /* Failed */
Christoph Hellwigf98e0eb2017-05-15 17:28:38 +0200504 return DM_MAPIO_KILL;
Mike Snitzer518257b2016-03-17 16:32:10 -0400505 } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
506 test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
Ming Lei459b5402018-01-11 14:01:55 +0800507 pg_init_all_paths(m);
508 return DM_MAPIO_DELAY_REQUEUE;
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100509 }
Mike Snitzer6afbc012014-07-08 11:55:09 -0400510
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100511 mpio->pgpath = pgpath;
512 mpio->nr_bytes = nr_bytes;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600513
514 bdev = pgpath->path.dev->bdev;
Bart Van Assche7083abb2017-04-27 10:11:15 -0700515 q = bdev_get_queue(bdev);
Christoph Hellwigff005a02018-05-09 09:54:05 +0200516 clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE,
517 BLK_MQ_REQ_NOWAIT);
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100518 if (IS_ERR(clone)) {
519 /* EBUSY, ENODEV or EWOULDBLOCK: requeue */
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500520 if (blk_queue_dying(q)) {
Bart Van Assche7083abb2017-04-27 10:11:15 -0700521 atomic_inc(&m->pg_init_in_progress);
522 activate_or_offline_path(pgpath);
Ming Lei050af082018-01-11 14:01:56 +0800523 return DM_MAPIO_DELAY_REQUEUE;
Bart Van Assche7083abb2017-04-27 10:11:15 -0700524 }
Ming Lei050af082018-01-11 14:01:56 +0800525
526 /*
527 * blk-mq's SCHED_RESTART can cover this requeue, so we
528 * needn't deal with it by DELAY_REQUEUE. More importantly,
529 * we have to return DM_MAPIO_REQUEUE so that blk-mq can
530 * get the queue busy feedback (via BLK_STS_RESOURCE),
531 * otherwise I/O merging can suffer.
532 */
Jens Axboe6a23e052018-10-10 20:49:26 -0600533 return DM_MAPIO_REQUEUE;
Mike Snitzere5863d92014-12-17 21:08:12 -0500534 }
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100535 clone->bio = clone->biotail = NULL;
536 clone->rq_disk = bdev->bd_disk;
537 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
538 *__clone = clone;
Mike Snitzere5863d92014-12-17 21:08:12 -0500539
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100540 if (pgpath->pg->ps.type->start_io)
541 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
542 &pgpath->path,
543 nr_bytes);
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600544 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Yufen Yu5de719e2019-04-24 23:19:05 +0800547static void multipath_release_clone(struct request *clone,
548 union map_info *map_context)
Mike Snitzere5863d92014-12-17 21:08:12 -0500549{
Yufen Yu5de719e2019-04-24 23:19:05 +0800550 if (unlikely(map_context)) {
551 /*
552 * non-NULL map_context means caller is still map
553 * method; must undo multipath_clone_and_map()
554 */
555 struct dm_mpath_io *mpio = get_mpio(map_context);
556 struct pgpath *pgpath = mpio->pgpath;
557
558 if (pgpath && pgpath->pg->ps.type->end_io)
559 pgpath->pg->ps.type->end_io(&pgpath->pg->ps,
560 &pgpath->path,
561 mpio->nr_bytes);
562 }
563
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100564 blk_put_request(clone);
Mike Snitzere5863d92014-12-17 21:08:12 -0500565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400568 * Map cloned bios (bio-based multipath)
569 */
Mike Snitzer0001ec52017-12-11 11:02:29 -0500570
571static struct pgpath *__map_bio(struct multipath *m, struct bio *bio)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400572{
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400573 struct pgpath *pgpath;
574 unsigned long flags;
575 bool queue_io;
576
577 /* Do we need to select a new pgpath? */
Will Deacon506458e2017-10-24 11:22:48 +0100578 pgpath = READ_ONCE(m->current_pgpath);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400579 queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
580 if (!pgpath || !queue_io)
Mike Snitzer0001ec52017-12-11 11:02:29 -0500581 pgpath = choose_pgpath(m, bio->bi_iter.bi_size);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400582
583 if ((pgpath && queue_io) ||
584 (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
585 /* Queue for the daemon to resubmit */
586 spin_lock_irqsave(&m->lock, flags);
587 bio_list_add(&m->queued_bios, bio);
588 spin_unlock_irqrestore(&m->lock, flags);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500589
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400590 /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
591 if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
592 pg_init_all_paths(m);
593 else if (!queue_io)
594 queue_work(kmultipathd, &m->process_queued_bios);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500595
Mike Snitzer0001ec52017-12-11 11:02:29 -0500596 return ERR_PTR(-EAGAIN);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400597 }
598
Mike Snitzer0001ec52017-12-11 11:02:29 -0500599 return pgpath;
600}
601
Mike Snitzer8d47e652018-03-05 14:10:11 -0500602static struct pgpath *__map_bio_fast(struct multipath *m, struct bio *bio)
Mike Snitzer0001ec52017-12-11 11:02:29 -0500603{
604 struct pgpath *pgpath;
605 unsigned long flags;
606
607 /* Do we need to select a new pgpath? */
608 /*
609 * FIXME: currently only switching path if no path (due to failure, etc)
610 * - which negates the point of using a path selector
611 */
612 pgpath = READ_ONCE(m->current_pgpath);
613 if (!pgpath)
614 pgpath = choose_pgpath(m, bio->bi_iter.bi_size);
615
616 if (!pgpath) {
617 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
618 /* Queue for the daemon to resubmit */
619 spin_lock_irqsave(&m->lock, flags);
620 bio_list_add(&m->queued_bios, bio);
621 spin_unlock_irqrestore(&m->lock, flags);
622 queue_work(kmultipathd, &m->process_queued_bios);
623
624 return ERR_PTR(-EAGAIN);
625 }
626 return NULL;
627 }
628
629 return pgpath;
630}
631
632static int __multipath_map_bio(struct multipath *m, struct bio *bio,
633 struct dm_mpath_io *mpio)
634{
635 struct pgpath *pgpath;
636
Mike Snitzer8d47e652018-03-05 14:10:11 -0500637 if (!m->hw_handler_name)
638 pgpath = __map_bio_fast(m, bio);
Mike Snitzer0001ec52017-12-11 11:02:29 -0500639 else
640 pgpath = __map_bio(m, bio);
641
642 if (IS_ERR(pgpath))
643 return DM_MAPIO_SUBMITTED;
644
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400645 if (!pgpath) {
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500646 if (must_push_back_bio(m))
Bart Van Asscheca5beb72017-04-27 10:11:24 -0700647 return DM_MAPIO_REQUEUE;
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200648 dm_report_EIO(m);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200649 return DM_MAPIO_KILL;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400650 }
651
652 mpio->pgpath = pgpath;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400653
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200654 bio->bi_status = 0;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200655 bio_set_dev(bio, pgpath->path.dev->bdev);
Jens Axboe1eff9d32016-08-05 15:35:16 -0600656 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400657
658 if (pgpath->pg->ps.type->start_io)
659 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
660 &pgpath->path,
Mike Snitzerd0442f82017-12-11 15:58:41 -0500661 mpio->nr_bytes);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400662 return DM_MAPIO_REMAPPED;
663}
664
665static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
666{
667 struct multipath *m = ti->private;
Mike Snitzerbf661be2016-05-24 15:48:08 -0400668 struct dm_mpath_io *mpio = NULL;
669
Mike Snitzer63f6e6f2017-12-05 14:10:33 -0500670 multipath_init_per_bio_data(bio, &mpio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400671 return __multipath_map_bio(m, bio, mpio);
672}
673
Mike Snitzer7e48c762016-09-14 10:47:03 -0400674static void process_queued_io_list(struct multipath *m)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400675{
Mike Snitzer953923c2018-10-11 11:06:29 -0400676 if (m->queue_mode == DM_TYPE_REQUEST_BASED)
Mike Snitzer7e48c762016-09-14 10:47:03 -0400677 dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
Mike Snitzer8d47e652018-03-05 14:10:11 -0500678 else if (m->queue_mode == DM_TYPE_BIO_BASED)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400679 queue_work(kmultipathd, &m->process_queued_bios);
680}
681
682static void process_queued_bios(struct work_struct *work)
683{
684 int r;
685 unsigned long flags;
686 struct bio *bio;
687 struct bio_list bios;
688 struct blk_plug plug;
689 struct multipath *m =
690 container_of(work, struct multipath, process_queued_bios);
691
692 bio_list_init(&bios);
693
694 spin_lock_irqsave(&m->lock, flags);
695
696 if (bio_list_empty(&m->queued_bios)) {
697 spin_unlock_irqrestore(&m->lock, flags);
698 return;
699 }
700
701 bio_list_merge(&bios, &m->queued_bios);
702 bio_list_init(&m->queued_bios);
703
704 spin_unlock_irqrestore(&m->lock, flags);
705
706 blk_start_plug(&plug);
707 while ((bio = bio_list_pop(&bios))) {
Mike Snitzer1836df02017-12-06 16:08:14 -0500708 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
709 dm_bio_restore(get_bio_details_from_mpio(mpio), bio);
710 r = __multipath_map_bio(m, bio, mpio);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200711 switch (r) {
712 case DM_MAPIO_KILL:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200713 bio->bi_status = BLK_STS_IOERR;
714 bio_endio(bio);
Dan Carpenter047385b2017-06-14 08:22:55 -0600715 break;
Christoph Hellwig846785e2017-06-03 09:38:02 +0200716 case DM_MAPIO_REQUEUE:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200717 bio->bi_status = BLK_STS_DM_REQUEUE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400718 bio_endio(bio);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200719 break;
720 case DM_MAPIO_REMAPPED:
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400721 generic_make_request(bio);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200722 break;
Wang Sheng-Hui8192a0c2018-03-10 10:18:55 +0800723 case DM_MAPIO_SUBMITTED:
Bart Van Assche9157c8d2017-08-09 11:32:14 -0700724 break;
725 default:
726 WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200727 }
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400728 }
729 blk_finish_plug(&plug);
730}
731
732/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * If we run out of usable paths, should we queue I/O or error it?
734 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -0500735static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
736 bool save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 unsigned long flags;
739
740 spin_lock_irqsave(&m->lock, flags);
Lukas Wunner5307e2a2017-10-12 12:40:10 +0200741 assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags,
742 (save_old_value && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) ||
743 (!save_old_value && queue_if_no_path));
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500744 assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 spin_unlock_irqrestore(&m->lock, flags);
746
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400747 if (!queue_if_no_path) {
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200748 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -0400749 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400750 }
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return 0;
753}
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/*
756 * An event is triggered whenever a path is taken out of use.
757 * Includes path failure and PG bypass.
758 */
David Howellsc4028952006-11-22 14:57:56 +0000759static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
David Howellsc4028952006-11-22 14:57:56 +0000761 struct multipath *m =
762 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 dm_table_event(m->ti->table);
765}
766
767/*-----------------------------------------------------------------
768 * Constructor/argument parsing:
769 * <#multipath feature args> [<arg>]*
770 * <#hw_handler args> [hw_handler [<arg>]*]
771 * <#priority groups>
772 * <initial priority group>
773 * [<selector> <#selector args> [<arg>]*
774 * <#paths> <#per-path selector args>
775 * [<path> [<arg>]* ]+ ]+
776 *---------------------------------------------------------------*/
Mike Snitzer498f0102011-08-02 12:32:04 +0100777static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct dm_target *ti)
779{
780 int r;
781 struct path_selector_type *pst;
782 unsigned ps_argc;
783
Eric Biggers5916a222017-06-22 11:32:45 -0700784 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700785 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 };
787
Mike Snitzer498f0102011-08-02 12:32:04 +0100788 pst = dm_get_path_selector(dm_shift_arg(as));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700790 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return -EINVAL;
792 }
793
Mike Snitzer498f0102011-08-02 12:32:04 +0100794 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100795 if (r) {
796 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 r = pst->create(&pg->ps, ps_argc, as->argv);
801 if (r) {
802 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700803 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return r;
805 }
806
807 pg->ps.type = pst;
Mike Snitzer498f0102011-08-02 12:32:04 +0100808 dm_consume_args(as, ps_argc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 return 0;
811}
812
Mike Snitzere8f74a02018-03-12 19:49:25 -0400813static int setup_scsi_dh(struct block_device *bdev, struct multipath *m,
Mike Snitzerb5922112018-09-17 11:38:47 -0400814 const char **attached_handler_name, char **error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500816 struct request_queue *q = bdev_get_queue(bdev);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500817 int r;
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100818
Mike Snitzer518257b2016-03-17 16:32:10 -0400819 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200820retain:
Mike Snitzerb5922112018-09-17 11:38:47 -0400821 if (*attached_handler_name) {
Mike Snitzera58a9352012-07-27 15:08:04 +0100822 /*
tang.junhui54cd6402016-11-24 15:11:48 +0800823 * Clear any hw_handler_params associated with a
824 * handler that isn't already attached.
825 */
Mike Snitzerb5922112018-09-17 11:38:47 -0400826 if (m->hw_handler_name && strcmp(*attached_handler_name, m->hw_handler_name)) {
tang.junhui54cd6402016-11-24 15:11:48 +0800827 kfree(m->hw_handler_params);
828 m->hw_handler_params = NULL;
829 }
830
831 /*
Mike Snitzera58a9352012-07-27 15:08:04 +0100832 * Reset hw_handler_name to match the attached handler
Mike Snitzera58a9352012-07-27 15:08:04 +0100833 *
834 * NB. This modifies the table line to show the actual
835 * handler instead of the original table passed in.
836 */
837 kfree(m->hw_handler_name);
Mike Snitzerb5922112018-09-17 11:38:47 -0400838 m->hw_handler_name = *attached_handler_name;
839 *attached_handler_name = NULL;
Mike Snitzera58a9352012-07-27 15:08:04 +0100840 }
841 }
842
843 if (m->hw_handler_name) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100844 r = scsi_dh_attach(q, m->hw_handler_name);
845 if (r == -EBUSY) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200846 char b[BDEVNAME_SIZE];
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100847
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200848 printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500849 bdevname(bdev, b));
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200850 goto retain;
851 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700852 if (r < 0) {
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500853 *error = "error attaching hardware handler";
854 return r;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700855 }
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700856
857 if (m->hw_handler_params) {
858 r = scsi_dh_set_params(q, m->hw_handler_params);
859 if (r < 0) {
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500860 *error = "unable to set hardware handler parameters";
861 return r;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700862 }
863 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700864 }
865
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500866 return 0;
867}
868
869static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
870 struct dm_target *ti)
871{
872 int r;
873 struct pgpath *p;
874 struct multipath *m = ti->private;
Mike Snitzere8f74a02018-03-12 19:49:25 -0400875 struct request_queue *q;
Mike Snitzerb5922112018-09-17 11:38:47 -0400876 const char *attached_handler_name = NULL;
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500877
878 /* we need at least a path arg */
879 if (as->argc < 1) {
880 ti->error = "no device given";
881 return ERR_PTR(-EINVAL);
882 }
883
884 p = alloc_pgpath();
885 if (!p)
886 return ERR_PTR(-ENOMEM);
887
888 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
889 &p->path.dev);
890 if (r) {
891 ti->error = "error getting device";
892 goto bad;
893 }
894
Mike Snitzere8f74a02018-03-12 19:49:25 -0400895 q = bdev_get_queue(p->path.dev->bdev);
896 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
Mike Snitzere457edf2018-03-29 11:50:10 -0400897 if (attached_handler_name || m->hw_handler_name) {
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500898 INIT_DELAYED_WORK(&p->activate_path, activate_path_work);
Mike Snitzerb5922112018-09-17 11:38:47 -0400899 r = setup_scsi_dh(p->path.dev->bdev, m, &attached_handler_name, &ti->error);
Martin Wilck940bc472019-04-29 11:48:15 +0200900 kfree(attached_handler_name);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500901 if (r) {
902 dm_put_device(ti, p->path.dev);
903 goto bad;
904 }
905 }
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
908 if (r) {
909 dm_put_device(ti, p->path.dev);
910 goto bad;
911 }
912
913 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 bad:
915 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100916 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Mike Snitzer498f0102011-08-02 12:32:04 +0100919static struct priority_group *parse_priority_group(struct dm_arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700920 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Eric Biggers5916a222017-06-22 11:32:45 -0700922 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700923 {1, 1024, "invalid number of paths"},
924 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 };
926
927 int r;
Mike Snitzer498f0102011-08-02 12:32:04 +0100928 unsigned i, nr_selector_args, nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700930 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 if (as->argc < 2) {
933 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100934 ti->error = "not enough priority group arguments";
935 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
938 pg = alloc_priority_group();
939 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700940 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100941 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943 pg->m = m;
944
945 r = parse_path_selector(as, pg, ti);
946 if (r)
947 goto bad;
948
949 /*
950 * read the paths
951 */
Mike Snitzer498f0102011-08-02 12:32:04 +0100952 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (r)
954 goto bad;
955
Mike Snitzer498f0102011-08-02 12:32:04 +0100956 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (r)
958 goto bad;
959
Mike Snitzer498f0102011-08-02 12:32:04 +0100960 nr_args = 1 + nr_selector_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 for (i = 0; i < pg->nr_pgpaths; i++) {
962 struct pgpath *pgpath;
Mike Snitzer498f0102011-08-02 12:32:04 +0100963 struct dm_arg_set path_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Mike Snitzer498f0102011-08-02 12:32:04 +0100965 if (as->argc < nr_args) {
Mikulas Patocka148acff2008-07-21 12:00:30 +0100966 ti->error = "not enough path parameters";
Alasdair G Kergon6bbf79a12010-08-12 04:13:49 +0100967 r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Mike Snitzer498f0102011-08-02 12:32:04 +0100971 path_args.argc = nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 path_args.argv = as->argv;
973
974 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100975 if (IS_ERR(pgpath)) {
976 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 pgpath->pg = pg;
981 list_add_tail(&pgpath->list, &pg->pgpaths);
Mike Snitzer498f0102011-08-02 12:32:04 +0100982 dm_consume_args(as, nr_args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984
985 return pg;
986
987 bad:
988 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100989 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Mike Snitzer498f0102011-08-02 12:32:04 +0100992static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 unsigned hw_argc;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700995 int ret;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700996 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Eric Biggers5916a222017-06-22 11:32:45 -0700998 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700999 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 };
1001
Mike Snitzer498f0102011-08-02 12:32:04 +01001002 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return -EINVAL;
1004
1005 if (!hw_argc)
1006 return 0;
1007
Mike Snitzer8d47e652018-03-05 14:10:11 -05001008 if (m->queue_mode == DM_TYPE_BIO_BASED) {
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001009 dm_consume_args(as, hw_argc);
1010 DMERR("bio-based multipath doesn't allow hardware handler args");
1011 return 0;
1012 }
1013
Mike Snitzer498f0102011-08-02 12:32:04 +01001014 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
tang.junhuif97dc422016-10-28 17:04:46 +08001015 if (!m->hw_handler_name)
1016 return -EINVAL;
Chandra Seetharaman14e98c52008-11-13 23:39:06 +00001017
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -07001018 if (hw_argc > 1) {
1019 char *p;
1020 int i, j, len = 4;
1021
1022 for (i = 0; i <= hw_argc - 2; i++)
1023 len += strlen(as->argv[i]) + 1;
1024 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
1025 if (!p) {
1026 ti->error = "memory allocation failed";
1027 ret = -ENOMEM;
1028 goto fail;
1029 }
1030 j = sprintf(p, "%d", hw_argc - 1);
1031 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
1032 j = sprintf(p, "%s", as->argv[i]);
1033 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001034 dm_consume_args(as, hw_argc - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 return 0;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -07001037fail:
1038 kfree(m->hw_handler_name);
1039 m->hw_handler_name = NULL;
1040 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Mike Snitzer498f0102011-08-02 12:32:04 +01001043static int parse_features(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 int r;
1046 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001047 struct dm_target *ti = m->ti;
Mike Snitzer498f0102011-08-02 12:32:04 +01001048 const char *arg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Eric Biggers5916a222017-06-22 11:32:45 -07001050 static const struct dm_arg _args[] = {
Mike Snitzere83068a2016-05-24 21:16:51 -04001051 {0, 8, "invalid number of feature args"},
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001052 {1, 50, "pg_init_retries must be between 1 and 50"},
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001053 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 };
1055
Mike Snitzer498f0102011-08-02 12:32:04 +01001056 r = dm_read_arg_group(_args, as, &argc, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (r)
1058 return -EINVAL;
1059
1060 if (!argc)
1061 return 0;
1062
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001063 do {
Mike Snitzer498f0102011-08-02 12:32:04 +01001064 arg_name = dm_shift_arg(as);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001065 argc--;
1066
Mike Snitzer498f0102011-08-02 12:32:04 +01001067 if (!strcasecmp(arg_name, "queue_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001068 r = queue_if_no_path(m, true, false);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001069 continue;
1070 }
1071
Mike Snitzera58a9352012-07-27 15:08:04 +01001072 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
Mike Snitzer518257b2016-03-17 16:32:10 -04001073 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
Mike Snitzera58a9352012-07-27 15:08:04 +01001074 continue;
1075 }
1076
Mike Snitzer498f0102011-08-02 12:32:04 +01001077 if (!strcasecmp(arg_name, "pg_init_retries") &&
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001078 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001079 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001080 argc--;
1081 continue;
1082 }
1083
Mike Snitzer498f0102011-08-02 12:32:04 +01001084 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001085 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001086 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001087 argc--;
1088 continue;
1089 }
1090
Mike Snitzere83068a2016-05-24 21:16:51 -04001091 if (!strcasecmp(arg_name, "queue_mode") &&
1092 (argc >= 1)) {
1093 const char *queue_mode_name = dm_shift_arg(as);
1094
1095 if (!strcasecmp(queue_mode_name, "bio"))
1096 m->queue_mode = DM_TYPE_BIO_BASED;
Mike Snitzer953923c2018-10-11 11:06:29 -04001097 else if (!strcasecmp(queue_mode_name, "rq") ||
1098 !strcasecmp(queue_mode_name, "mq"))
Mike Snitzere83068a2016-05-24 21:16:51 -04001099 m->queue_mode = DM_TYPE_REQUEST_BASED;
Mike Snitzere83068a2016-05-24 21:16:51 -04001100 else {
1101 ti->error = "Unknown 'queue_mode' requested";
1102 r = -EINVAL;
1103 }
1104 argc--;
1105 continue;
1106 }
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001109 r = -EINVAL;
1110 } while (argc && !r);
1111
1112 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Mike Snitzere83068a2016-05-24 21:16:51 -04001115static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Mike Snitzer498f0102011-08-02 12:32:04 +01001117 /* target arguments */
Eric Biggers5916a222017-06-22 11:32:45 -07001118 static const struct dm_arg _args[] = {
Mike Snitzera490a072011-03-24 13:54:33 +00001119 {0, 1024, "invalid number of priority groups"},
1120 {0, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 };
1122
1123 int r;
1124 struct multipath *m;
Mike Snitzer498f0102011-08-02 12:32:04 +01001125 struct dm_arg_set as;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 unsigned pg_count = 0;
1127 unsigned next_pg_num;
1128
1129 as.argc = argc;
1130 as.argv = argv;
1131
Mike Snitzere83068a2016-05-24 21:16:51 -04001132 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001134 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return -EINVAL;
1136 }
1137
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001138 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (r)
1140 goto bad;
1141
Mike Snitzere83068a2016-05-24 21:16:51 -04001142 r = alloc_multipath_stage2(ti, m);
1143 if (r)
1144 goto bad;
1145
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001146 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (r)
1148 goto bad;
1149
Mike Snitzer498f0102011-08-02 12:32:04 +01001150 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (r)
1152 goto bad;
1153
Mike Snitzer498f0102011-08-02 12:32:04 +01001154 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (r)
1156 goto bad;
1157
Mike Snitzera490a072011-03-24 13:54:33 +00001158 if ((!m->nr_priority_groups && next_pg_num) ||
1159 (m->nr_priority_groups && !next_pg_num)) {
1160 ti->error = "invalid initial priority group";
1161 r = -EINVAL;
1162 goto bad;
1163 }
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* parse the priority groups */
1166 while (as.argc) {
1167 struct priority_group *pg;
Mike Snitzer91e968a2016-03-17 17:10:15 -04001168 unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001170 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +01001171 if (IS_ERR(pg)) {
1172 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 goto bad;
1174 }
1175
Mike Snitzer91e968a2016-03-17 17:10:15 -04001176 nr_valid_paths += pg->nr_pgpaths;
1177 atomic_set(&m->nr_valid_paths, nr_valid_paths);
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 list_add_tail(&pg->list, &m->priority_groups);
1180 pg_count++;
1181 pg->pg_num = pg_count;
1182 if (!--next_pg_num)
1183 m->next_pg = pg;
1184 }
1185
1186 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001187 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 r = -EINVAL;
1189 goto bad;
1190 }
1191
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001192 ti->num_flush_bios = 1;
1193 ti->num_discard_bios = 1;
Mike Snitzer042bcef2013-05-10 14:37:16 +01001194 ti->num_write_same_bios = 1;
Christoph Hellwigac62d622017-04-05 19:21:05 +02001195 ti->num_write_zeroes_bios = 1;
Mike Snitzer8d47e652018-03-05 14:10:11 -05001196 if (m->queue_mode == DM_TYPE_BIO_BASED)
Mike Snitzerbf661be2016-05-24 15:48:08 -04001197 ti->per_io_data_size = multipath_per_bio_data_size();
Christoph Hellwigeb8db832017-01-22 18:32:46 +01001198 else
Mike Snitzer8637a6b2016-01-31 12:08:36 -05001199 ti->per_io_data_size = sizeof(struct dm_mpath_io);
Mikulas Patocka86279212009-06-22 10:12:24 +01001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return 0;
1202
1203 bad:
1204 free_multipath(m);
1205 return r;
1206}
1207
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001208static void multipath_wait_for_pg_init_completion(struct multipath *m)
1209{
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001210 DEFINE_WAIT(wait);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001211
1212 while (1) {
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001213 prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001214
Mike Snitzer91e968a2016-03-17 17:10:15 -04001215 if (!atomic_read(&m->pg_init_in_progress))
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001216 break;
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001217
1218 io_schedule();
1219 }
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001220 finish_wait(&m->pg_init_wait, &wait);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001221}
1222
1223static void flush_multipath_work(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Mike Snitzer848b8ae2017-12-10 15:37:21 -05001225 if (m->hw_handler_name) {
1226 set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1227 smp_mb__after_atomic();
Shiva Krishna Merla954a73d2013-10-30 03:26:38 +00001228
wuzhouhui935fcc52018-03-22 22:22:38 +08001229 if (atomic_read(&m->pg_init_in_progress))
1230 flush_workqueue(kmpath_handlerd);
Mike Snitzer848b8ae2017-12-10 15:37:21 -05001231 multipath_wait_for_pg_init_completion(m);
1232
1233 clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1234 smp_mb__after_atomic();
1235 }
1236
wuzhouhui935fcc52018-03-22 22:22:38 +08001237 if (m->queue_mode == DM_TYPE_BIO_BASED)
1238 flush_work(&m->process_queued_bios);
Tejun Heo43829732012-08-20 14:51:24 -07001239 flush_work(&m->trigger_event);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001240}
1241
1242static void multipath_dtr(struct dm_target *ti)
1243{
1244 struct multipath *m = ti->private;
1245
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001246 flush_multipath_work(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 free_multipath(m);
1248}
1249
1250/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 * Take a path out of use.
1252 */
1253static int fail_path(struct pgpath *pgpath)
1254{
1255 unsigned long flags;
1256 struct multipath *m = pgpath->pg->m;
1257
1258 spin_lock_irqsave(&m->lock, flags);
1259
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001260 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 goto out;
1262
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001263 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001266 pgpath->is_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 pgpath->fail_count++;
1268
Mike Snitzer91e968a2016-03-17 17:10:15 -04001269 atomic_dec(&m->nr_valid_paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 if (pgpath == m->current_pgpath)
1272 m->current_pgpath = NULL;
1273
Mike Andersonb15546f2007-10-19 22:48:02 +01001274 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
Mike Snitzer91e968a2016-03-17 17:10:15 -04001275 pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
Mike Andersonb15546f2007-10-19 22:48:02 +01001276
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001277 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279out:
1280 spin_unlock_irqrestore(&m->lock, flags);
1281
1282 return 0;
1283}
1284
1285/*
1286 * Reinstate a previously-failed path
1287 */
1288static int reinstate_path(struct pgpath *pgpath)
1289{
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001290 int r = 0, run_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 unsigned long flags;
1292 struct multipath *m = pgpath->pg->m;
Mike Snitzer91e968a2016-03-17 17:10:15 -04001293 unsigned nr_valid_paths;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 spin_lock_irqsave(&m->lock, flags);
1296
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001297 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 goto out;
1299
Mike Snitzerec31f3f2016-02-20 12:49:43 -05001300 DMWARN("Reinstating path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1303 if (r)
1304 goto out;
1305
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001306 pgpath->is_active = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Mike Snitzer91e968a2016-03-17 17:10:15 -04001308 nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
1309 if (nr_valid_paths == 1) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001310 m->current_pgpath = NULL;
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001311 run_queue = 1;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001312 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001313 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
Mike Snitzer91e968a2016-03-17 17:10:15 -04001314 atomic_inc(&m->pg_init_in_progress);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Mike Andersonb15546f2007-10-19 22:48:02 +01001317 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
Mike Snitzer91e968a2016-03-17 17:10:15 -04001318 pgpath->path.dev->name, nr_valid_paths);
Mike Andersonb15546f2007-10-19 22:48:02 +01001319
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001320 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322out:
1323 spin_unlock_irqrestore(&m->lock, flags);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001324 if (run_queue) {
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001325 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -04001326 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 return r;
1330}
1331
1332/*
1333 * Fail or reinstate all paths that match the provided struct dm_dev.
1334 */
1335static int action_dev(struct multipath *m, struct dm_dev *dev,
1336 action_fn action)
1337{
Mike Snitzer19040c02011-03-24 13:54:31 +00001338 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct pgpath *pgpath;
1340 struct priority_group *pg;
1341
1342 list_for_each_entry(pg, &m->priority_groups, list) {
1343 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1344 if (pgpath->path.dev == dev)
1345 r = action(pgpath);
1346 }
1347 }
1348
1349 return r;
1350}
1351
1352/*
1353 * Temporarily try to avoid having to use the specified PG
1354 */
1355static void bypass_pg(struct multipath *m, struct priority_group *pg,
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001356 bool bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 unsigned long flags;
1359
1360 spin_lock_irqsave(&m->lock, flags);
1361
1362 pg->bypassed = bypassed;
1363 m->current_pgpath = NULL;
1364 m->current_pg = NULL;
1365
1366 spin_unlock_irqrestore(&m->lock, flags);
1367
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001368 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
1371/*
1372 * Switch to using the specified PG from the next I/O that gets mapped
1373 */
1374static int switch_pg_num(struct multipath *m, const char *pgstr)
1375{
1376 struct priority_group *pg;
1377 unsigned pgnum;
1378 unsigned long flags;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001379 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001381 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
tang.junhuicc5bd922016-11-04 12:37:09 +08001382 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 DMWARN("invalid PG number supplied to switch_pg_num");
1384 return -EINVAL;
1385 }
1386
1387 spin_lock_irqsave(&m->lock, flags);
1388 list_for_each_entry(pg, &m->priority_groups, list) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001389 pg->bypassed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (--pgnum)
1391 continue;
1392
1393 m->current_pgpath = NULL;
1394 m->current_pg = NULL;
1395 m->next_pg = pg;
1396 }
1397 spin_unlock_irqrestore(&m->lock, flags);
1398
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001399 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return 0;
1401}
1402
1403/*
1404 * Set/clear bypassed status of a PG.
1405 * PGs are numbered upwards from 1 in the order they were declared.
1406 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001407static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 struct priority_group *pg;
1410 unsigned pgnum;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001411 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001413 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
tang.junhuicc5bd922016-11-04 12:37:09 +08001414 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 DMWARN("invalid PG number supplied to bypass_pg");
1416 return -EINVAL;
1417 }
1418
1419 list_for_each_entry(pg, &m->priority_groups, list) {
1420 if (!--pgnum)
1421 break;
1422 }
1423
1424 bypass_pg(m, pg, bypassed);
1425 return 0;
1426}
1427
1428/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001429 * Should we retry pg_init immediately?
1430 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001431static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001432{
1433 unsigned long flags;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001434 bool limit_reached = false;
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001435
1436 spin_lock_irqsave(&m->lock, flags);
1437
Mike Snitzer91e968a2016-03-17 17:10:15 -04001438 if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
1439 !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
Mike Snitzer518257b2016-03-17 16:32:10 -04001440 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001441 else
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001442 limit_reached = true;
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001443
1444 spin_unlock_irqrestore(&m->lock, flags);
1445
1446 return limit_reached;
1447}
1448
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001449static void pg_init_done(void *data, int errors)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001450{
Moger, Babu83c0d5d2010-03-06 02:29:45 +00001451 struct pgpath *pgpath = data;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001452 struct priority_group *pg = pgpath->pg;
1453 struct multipath *m = pg->m;
1454 unsigned long flags;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001455 bool delay_retry = false;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001456
1457 /* device or driver problems */
1458 switch (errors) {
1459 case SCSI_DH_OK:
1460 break;
1461 case SCSI_DH_NOSYS:
1462 if (!m->hw_handler_name) {
1463 errors = 0;
1464 break;
1465 }
Moger, Babuf7b934c2010-03-06 02:29:49 +00001466 DMERR("Could not failover the device: Handler scsi_dh_%s "
1467 "Error %d.", m->hw_handler_name, errors);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001468 /*
1469 * Fail path for now, so we do not ping pong
1470 */
1471 fail_path(pgpath);
1472 break;
1473 case SCSI_DH_DEV_TEMP_BUSY:
1474 /*
1475 * Probably doing something like FW upgrade on the
1476 * controller so try the other pg.
1477 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001478 bypass_pg(m, pg, true);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001479 break;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001480 case SCSI_DH_RETRY:
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001481 /* Wait before retrying. */
1482 delay_retry = 1;
Bart Van Assche7b06e092017-08-09 11:32:13 -07001483 /* fall through */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001484 case SCSI_DH_IMM_RETRY:
1485 case SCSI_DH_RES_TEMP_UNAVAIL:
1486 if (pg_init_limit_reached(m, pgpath))
1487 fail_path(pgpath);
1488 errors = 0;
1489 break;
Mike Snitzerec31f3f2016-02-20 12:49:43 -05001490 case SCSI_DH_DEV_OFFLINED:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001491 default:
1492 /*
1493 * We probably do not want to fail the path for a device
1494 * error, but this is what the old dm did. In future
1495 * patches we can do more advanced handling.
1496 */
1497 fail_path(pgpath);
1498 }
1499
1500 spin_lock_irqsave(&m->lock, flags);
1501 if (errors) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001502 if (pgpath == m->current_pgpath) {
1503 DMERR("Could not failover device. Error %d.", errors);
1504 m->current_pgpath = NULL;
1505 m->current_pg = NULL;
1506 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001507 } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001508 pg->bypassed = false;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001509
Mike Snitzer91e968a2016-03-17 17:10:15 -04001510 if (atomic_dec_return(&m->pg_init_in_progress) > 0)
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001511 /* Activations of other paths are still on going */
1512 goto out;
1513
Mike Snitzer518257b2016-03-17 16:32:10 -04001514 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1515 if (delay_retry)
1516 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1517 else
1518 clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1519
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001520 if (__pg_init_all_paths(m))
1521 goto out;
1522 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001523 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001524
Mike Snitzer7e48c762016-09-14 10:47:03 -04001525 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001526
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001527 /*
1528 * Wake up any thread waiting to suspend.
1529 */
1530 wake_up(&m->pg_init_wait);
1531
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001532out:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001533 spin_unlock_irqrestore(&m->lock, flags);
1534}
1535
Bart Van Assche89bfce72017-04-27 10:11:14 -07001536static void activate_or_offline_path(struct pgpath *pgpath)
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001537{
Mike Snitzerf10e06b2016-09-01 12:06:37 -04001538 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001539
Mike Snitzerf10e06b2016-09-01 12:06:37 -04001540 if (pgpath->is_active && !blk_queue_dying(q))
1541 scsi_dh_activate(q, pg_init_done, pgpath);
Hannes Reinecke3a017502014-02-28 15:33:49 +01001542 else
1543 pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001544}
1545
Bart Van Assche89bfce72017-04-27 10:11:14 -07001546static void activate_path_work(struct work_struct *work)
1547{
1548 struct pgpath *pgpath =
1549 container_of(work, struct pgpath, activate_path.work);
1550
1551 activate_or_offline_path(pgpath);
1552}
1553
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001554static int multipath_end_io(struct dm_target *ti, struct request *clone,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001555 blk_status_t error, union map_info *map_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001557 struct dm_mpath_io *mpio = get_mpio(map_context);
1558 struct pgpath *pgpath = mpio->pgpath;
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001559 int r = DM_ENDIO_DONE;
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001560
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001561 /*
1562 * We don't queue any clone request inside the multipath target
1563 * during end I/O handling, since those clone requests don't have
1564 * bio clones. If we queue them inside the multipath target,
1565 * we need to make bio clones, that requires memory allocation.
Mike Snitzer4cc96132016-05-12 16:28:10 -04001566 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001567 * don't have bio clones.)
1568 * Instead of queueing the clone request here, we queue the original
1569 * request into dm core, which will remake a clone request and
1570 * clone bios for it and resubmit it later.
1571 */
Keith Buscha1275672018-01-09 12:04:18 -07001572 if (error && blk_path_error(error)) {
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001573 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Mike Snitzerac514ff2018-01-12 19:53:40 -05001575 if (error == BLK_STS_RESOURCE)
1576 r = DM_ENDIO_DELAY_REQUEUE;
1577 else
1578 r = DM_ENDIO_REQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001580 if (pgpath)
1581 fail_path(pgpath);
Mike Snitzer959eb4e2010-08-12 04:14:32 +01001582
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001583 if (atomic_read(&m->nr_valid_paths) == 0 &&
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -05001584 !must_push_back_rq(m)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001585 if (error == BLK_STS_IOERR)
Christoph Hellwig18a482f2017-05-15 17:28:37 +02001586 dm_report_EIO(m);
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001587 /* complete with the original error */
1588 r = DM_ENDIO_DONE;
1589 }
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (pgpath) {
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001593 struct path_selector *ps = &pgpath->pg->ps;
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (ps->type->end_io)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001596 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001599 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001602static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone,
Mike Snitzer848b8ae2017-12-10 15:37:21 -05001603 blk_status_t *error)
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001604{
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001605 struct multipath *m = ti->private;
1606 struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
1607 struct pgpath *pgpath = mpio->pgpath;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001608 unsigned long flags;
Christoph Hellwig1be56902017-06-03 09:38:03 +02001609 int r = DM_ENDIO_DONE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001610
Keith Buscha1275672018-01-09 12:04:18 -07001611 if (!*error || !blk_path_error(*error))
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001612 goto done;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001613
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001614 if (pgpath)
1615 fail_path(pgpath);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001616
Bart Van Asscheca5beb72017-04-27 10:11:24 -07001617 if (atomic_read(&m->nr_valid_paths) == 0 &&
Christoph Hellwig18a482f2017-05-15 17:28:37 +02001618 !test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -05001619 if (must_push_back_bio(m)) {
1620 r = DM_ENDIO_REQUEUE;
1621 } else {
1622 dm_report_EIO(m);
1623 *error = BLK_STS_IOERR;
1624 }
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001625 goto done;
Christoph Hellwig18a482f2017-05-15 17:28:37 +02001626 }
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001627
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001628 spin_lock_irqsave(&m->lock, flags);
1629 bio_list_add(&m->queued_bios, clone);
1630 spin_unlock_irqrestore(&m->lock, flags);
1631 if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
1632 queue_work(kmultipathd, &m->process_queued_bios);
1633
Christoph Hellwig1be56902017-06-03 09:38:03 +02001634 r = DM_ENDIO_INCOMPLETE;
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001635done:
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001636 if (pgpath) {
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001637 struct path_selector *ps = &pgpath->pg->ps;
1638
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001639 if (ps->type->end_io)
1640 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1641 }
1642
Christoph Hellwig1be56902017-06-03 09:38:03 +02001643 return r;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001644}
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646/*
1647 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001648 * the last path fails we must error any remaining I/O.
1649 * Note that if the freeze_bdev fails while suspending, the
1650 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 */
1652static void multipath_presuspend(struct dm_target *ti)
1653{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001654 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001656 queue_if_no_path(m, false, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001659static void multipath_postsuspend(struct dm_target *ti)
1660{
Mike Anderson6380f262009-12-10 23:52:21 +00001661 struct multipath *m = ti->private;
1662
1663 mutex_lock(&m->work_mutex);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001664 flush_multipath_work(m);
Mike Anderson6380f262009-12-10 23:52:21 +00001665 mutex_unlock(&m->work_mutex);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001666}
1667
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001668/*
1669 * Restore the queue_if_no_path setting.
1670 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671static void multipath_resume(struct dm_target *ti)
1672{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001673 struct multipath *m = ti->private;
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001674 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001676 spin_lock_irqsave(&m->lock, flags);
Lukas Wunner5307e2a2017-10-12 12:40:10 +02001677 assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags,
1678 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags));
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001679 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
1682/*
1683 * Info output has the following format:
1684 * num_multipath_feature_args [multipath_feature_args]*
1685 * num_handler_status_args [handler_status_args]*
1686 * num_groups init_group_number
1687 * [A|D|E num_ps_status_args [ps_status_args]*
1688 * num_paths num_selector_args
1689 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1690 *
1691 * Table output has the following format (identical to the constructor string):
1692 * num_feature_args [features_args]*
1693 * num_handler_args hw_handler [hw_handler_args]*
1694 * num_groups init_group_number
1695 * [priority selector-name num_ps_args [ps_args]*
1696 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1697 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001698static void multipath_status(struct dm_target *ti, status_type_t type,
1699 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 int sz = 0;
1702 unsigned long flags;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001703 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 struct priority_group *pg;
1705 struct pgpath *p;
1706 unsigned pg_num;
1707 char state;
1708
1709 spin_lock_irqsave(&m->lock, flags);
1710
1711 /* Features */
1712 if (type == STATUSTYPE_INFO)
Mike Snitzer91e968a2016-03-17 17:10:15 -04001713 DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
1714 atomic_read(&m->pg_init_count));
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001715 else {
Mike Snitzer518257b2016-03-17 16:32:10 -04001716 DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001717 (m->pg_init_retries > 0) * 2 +
Mike Snitzera58a9352012-07-27 15:08:04 +01001718 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
Mike Snitzere83068a2016-05-24 21:16:51 -04001719 test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
1720 (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
1721
Mike Snitzer518257b2016-03-17 16:32:10 -04001722 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001723 DMEMIT("queue_if_no_path ");
1724 if (m->pg_init_retries)
1725 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001726 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1727 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
Mike Snitzer518257b2016-03-17 16:32:10 -04001728 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
Mike Snitzera58a9352012-07-27 15:08:04 +01001729 DMEMIT("retain_attached_hw_handler ");
Mike Snitzere83068a2016-05-24 21:16:51 -04001730 if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
1731 switch(m->queue_mode) {
1732 case DM_TYPE_BIO_BASED:
1733 DMEMIT("queue_mode bio ");
1734 break;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07001735 default:
1736 WARN_ON_ONCE(true);
1737 break;
Mike Snitzere83068a2016-05-24 21:16:51 -04001738 }
1739 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001742 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 DMEMIT("0 ");
1744 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001745 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 DMEMIT("%u ", m->nr_priority_groups);
1748
1749 if (m->next_pg)
1750 pg_num = m->next_pg->pg_num;
1751 else if (m->current_pg)
1752 pg_num = m->current_pg->pg_num;
1753 else
Mike Snitzera490a072011-03-24 13:54:33 +00001754 pg_num = (m->nr_priority_groups ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 DMEMIT("%u ", pg_num);
1757
1758 switch (type) {
1759 case STATUSTYPE_INFO:
1760 list_for_each_entry(pg, &m->priority_groups, list) {
1761 if (pg->bypassed)
1762 state = 'D'; /* Disabled */
1763 else if (pg == m->current_pg)
1764 state = 'A'; /* Currently Active */
1765 else
1766 state = 'E'; /* Enabled */
1767
1768 DMEMIT("%c ", state);
1769
1770 if (pg->ps.type->status)
1771 sz += pg->ps.type->status(&pg->ps, NULL, type,
1772 result + sz,
1773 maxlen - sz);
1774 else
1775 DMEMIT("0 ");
1776
1777 DMEMIT("%u %u ", pg->nr_pgpaths,
1778 pg->ps.type->info_args);
1779
1780 list_for_each_entry(p, &pg->pgpaths, list) {
1781 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001782 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 p->fail_count);
1784 if (pg->ps.type->status)
1785 sz += pg->ps.type->status(&pg->ps,
1786 &p->path, type, result + sz,
1787 maxlen - sz);
1788 }
1789 }
1790 break;
1791
1792 case STATUSTYPE_TABLE:
1793 list_for_each_entry(pg, &m->priority_groups, list) {
1794 DMEMIT("%s ", pg->ps.type->name);
1795
1796 if (pg->ps.type->status)
1797 sz += pg->ps.type->status(&pg->ps, NULL, type,
1798 result + sz,
1799 maxlen - sz);
1800 else
1801 DMEMIT("0 ");
1802
1803 DMEMIT("%u %u ", pg->nr_pgpaths,
1804 pg->ps.type->table_args);
1805
1806 list_for_each_entry(p, &pg->pgpaths, list) {
1807 DMEMIT("%s ", p->path.dev->name);
1808 if (pg->ps.type->status)
1809 sz += pg->ps.type->status(&pg->ps,
1810 &p->path, type, result + sz,
1811 maxlen - sz);
1812 }
1813 }
1814 break;
1815 }
1816
1817 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
Mike Snitzer1eb5fa82018-02-28 15:59:59 -05001820static int multipath_message(struct dm_target *ti, unsigned argc, char **argv,
1821 char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Mike Anderson6380f262009-12-10 23:52:21 +00001823 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 struct dm_dev *dev;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001825 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 action_fn action;
1827
Mike Anderson6380f262009-12-10 23:52:21 +00001828 mutex_lock(&m->work_mutex);
1829
Kiyoshi Uedac2f3d242009-12-10 23:52:27 +00001830 if (dm_suspended(ti)) {
1831 r = -EBUSY;
1832 goto out;
1833 }
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (argc == 1) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001836 if (!strcasecmp(argv[0], "queue_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001837 r = queue_if_no_path(m, true, false);
Mike Anderson6380f262009-12-10 23:52:21 +00001838 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001839 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001840 r = queue_if_no_path(m, false, false);
Mike Anderson6380f262009-12-10 23:52:21 +00001841 goto out;
1842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
1844
Mike Anderson6380f262009-12-10 23:52:21 +00001845 if (argc != 2) {
Jose Castilloa356e422014-01-29 17:52:45 +01001846 DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
Mike Anderson6380f262009-12-10 23:52:21 +00001847 goto out;
1848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Mike Snitzer498f0102011-08-02 12:32:04 +01001850 if (!strcasecmp(argv[0], "disable_group")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001851 r = bypass_pg_num(m, argv[1], true);
Mike Anderson6380f262009-12-10 23:52:21 +00001852 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001853 } else if (!strcasecmp(argv[0], "enable_group")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001854 r = bypass_pg_num(m, argv[1], false);
Mike Anderson6380f262009-12-10 23:52:21 +00001855 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001856 } else if (!strcasecmp(argv[0], "switch_group")) {
Mike Anderson6380f262009-12-10 23:52:21 +00001857 r = switch_pg_num(m, argv[1]);
1858 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001859 } else if (!strcasecmp(argv[0], "reinstate_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 action = reinstate_path;
Mike Snitzer498f0102011-08-02 12:32:04 +01001861 else if (!strcasecmp(argv[0], "fail_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 action = fail_path;
Mike Anderson6380f262009-12-10 23:52:21 +00001863 else {
Jose Castilloa356e422014-01-29 17:52:45 +01001864 DMWARN("Unrecognised multipath message received: %s", argv[0]);
Mike Anderson6380f262009-12-10 23:52:21 +00001865 goto out;
1866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00001868 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001870 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 argv[1]);
Mike Anderson6380f262009-12-10 23:52:21 +00001872 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874
1875 r = action_dev(m, dev, action);
1876
1877 dm_put_device(ti, dev);
1878
Mike Anderson6380f262009-12-10 23:52:21 +00001879out:
1880 mutex_unlock(&m->work_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001884static int multipath_prepare_ioctl(struct dm_target *ti,
Mike Snitzer5bd5e8d2018-04-03 16:54:10 -04001885 struct block_device **bdev)
Milan Broz9af4aa32006-10-03 01:15:20 -07001886{
Mikulas Patocka35991652012-06-03 00:29:58 +01001887 struct multipath *m = ti->private;
Mike Snitzer2da16102016-03-17 18:38:17 -04001888 struct pgpath *current_pgpath;
Mikulas Patocka35991652012-06-03 00:29:58 +01001889 int r;
1890
Will Deacon506458e2017-10-24 11:22:48 +01001891 current_pgpath = READ_ONCE(m->current_pgpath);
Mike Snitzer2da16102016-03-17 18:38:17 -04001892 if (!current_pgpath)
1893 current_pgpath = choose_pgpath(m, 0);
Milan Broz9af4aa32006-10-03 01:15:20 -07001894
Mike Snitzer2da16102016-03-17 18:38:17 -04001895 if (current_pgpath) {
Mike Snitzer518257b2016-03-17 16:32:10 -04001896 if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
Mike Snitzer2da16102016-03-17 18:38:17 -04001897 *bdev = current_pgpath->path.dev->bdev;
Junichi Nomura43e43c92015-11-17 09:36:56 +00001898 r = 0;
1899 } else {
1900 /* pg_init has not started or completed */
1901 r = -ENOTCONN;
1902 }
1903 } else {
1904 /* No path is available */
Mike Snitzer518257b2016-03-17 16:32:10 -04001905 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Junichi Nomura43e43c92015-11-17 09:36:56 +00001906 r = -ENOTCONN;
1907 else
1908 r = -EIO;
Milan Broze90dae12006-10-03 01:15:22 -07001909 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001910
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +00001911 if (r == -ENOTCONN) {
Will Deacon506458e2017-10-24 11:22:48 +01001912 if (!READ_ONCE(m->current_pg)) {
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001913 /* Path status changed, redo selection */
Mike Snitzer2da16102016-03-17 18:38:17 -04001914 (void) choose_pgpath(m, 0);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001915 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001916 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
Mike Snitzer2da16102016-03-17 18:38:17 -04001917 pg_init_all_paths(m);
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001918 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -04001919 process_queued_io_list(m);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001920 }
Mikulas Patocka35991652012-06-03 00:29:58 +01001921
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001922 /*
1923 * Only pass ioctls through if the device sizes match exactly.
1924 */
1925 if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1926 return 1;
1927 return r;
Milan Broz9af4aa32006-10-03 01:15:20 -07001928}
1929
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001930static int multipath_iterate_devices(struct dm_target *ti,
1931 iterate_devices_callout_fn fn, void *data)
1932{
1933 struct multipath *m = ti->private;
1934 struct priority_group *pg;
1935 struct pgpath *p;
1936 int ret = 0;
1937
1938 list_for_each_entry(pg, &m->priority_groups, list) {
1939 list_for_each_entry(p, &pg->pgpaths, list) {
Mike Snitzer5dea2712009-07-23 20:30:42 +01001940 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001941 if (ret)
1942 goto out;
1943 }
1944 }
1945
1946out:
1947 return ret;
1948}
1949
Mike Snitzer9f54cec2016-02-11 21:42:28 -05001950static int pgpath_busy(struct pgpath *pgpath)
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001951{
1952 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1953
Mike Snitzer52b09912015-02-23 16:36:41 -05001954 return blk_lld_busy(q);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001955}
1956
1957/*
1958 * We return "busy", only when we can map I/Os but underlying devices
1959 * are busy (so even if we map I/Os now, the I/Os will wait on
1960 * the underlying queue).
1961 * In other words, if we want to kill I/Os or queue them inside us
1962 * due to map unavailability, we don't return "busy". Otherwise,
1963 * dm core won't give us the I/Os and we can't do what we want.
1964 */
1965static int multipath_busy(struct dm_target *ti)
1966{
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001967 bool busy = false, has_active = false;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001968 struct multipath *m = ti->private;
Mike Snitzer2da16102016-03-17 18:38:17 -04001969 struct priority_group *pg, *next_pg;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001970 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001971
Mike Snitzerb88efd42016-09-09 19:26:19 -04001972 /* pg_init in progress */
1973 if (atomic_read(&m->pg_init_in_progress))
Mike Snitzer2da16102016-03-17 18:38:17 -04001974 return true;
1975
Mike Snitzerb88efd42016-09-09 19:26:19 -04001976 /* no paths available, for blk-mq: rely on IO mapping to delay requeue */
1977 if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Mike Snitzer953923c2018-10-11 11:06:29 -04001978 return (m->queue_mode != DM_TYPE_REQUEST_BASED);
Mike Snitzerb88efd42016-09-09 19:26:19 -04001979
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001980 /* Guess which priority_group will be used at next mapping time */
Will Deacon506458e2017-10-24 11:22:48 +01001981 pg = READ_ONCE(m->current_pg);
1982 next_pg = READ_ONCE(m->next_pg);
1983 if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg))
Mike Snitzer2da16102016-03-17 18:38:17 -04001984 pg = next_pg;
1985
1986 if (!pg) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001987 /*
1988 * We don't know which pg will be used at next mapping time.
Mike Snitzer2da16102016-03-17 18:38:17 -04001989 * We don't call choose_pgpath() here to avoid to trigger
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001990 * pg_init just by busy checking.
1991 * So we don't know whether underlying devices we will be using
1992 * at next mapping time are busy or not. Just try mapping.
1993 */
Mike Snitzer2da16102016-03-17 18:38:17 -04001994 return busy;
1995 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001996
1997 /*
1998 * If there is one non-busy active path at least, the path selector
1999 * will be able to select it. So we consider such a pg as not busy.
2000 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002001 busy = true;
Mike Snitzer2da16102016-03-17 18:38:17 -04002002 list_for_each_entry(pgpath, &pg->pgpaths, list) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002003 if (pgpath->is_active) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002004 has_active = true;
Mike Snitzer9f54cec2016-02-11 21:42:28 -05002005 if (!pgpath_busy(pgpath)) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002006 busy = false;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002007 break;
2008 }
2009 }
Mike Snitzer2da16102016-03-17 18:38:17 -04002010 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002011
Mike Snitzer2da16102016-03-17 18:38:17 -04002012 if (!has_active) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002013 /*
2014 * No active path in this pg, so this pg won't be used and
2015 * the current_pg will be changed at next mapping time.
2016 * We need to try mapping to determine it.
2017 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002018 busy = false;
Mike Snitzer2da16102016-03-17 18:38:17 -04002019 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002020
2021 return busy;
2022}
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024/*-----------------------------------------------------------------
2025 * Module setup
2026 *---------------------------------------------------------------*/
2027static struct target_type multipath_target = {
2028 .name = "multipath",
Steffen Maier8c5c1472018-03-14 15:33:06 +01002029 .version = {1, 13, 0},
2030 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE |
2031 DM_TARGET_PASSES_INTEGRITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 .module = THIS_MODULE,
2033 .ctr = multipath_ctr,
2034 .dtr = multipath_dtr,
Mike Snitzere5863d92014-12-17 21:08:12 -05002035 .clone_and_map_rq = multipath_clone_and_map,
2036 .release_clone_rq = multipath_release_clone,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002037 .rq_end_io = multipath_end_io,
Mike Snitzer76e33fe2016-05-19 16:15:14 -04002038 .map = multipath_map_bio,
2039 .end_io = multipath_end_io_bio,
2040 .presuspend = multipath_presuspend,
2041 .postsuspend = multipath_postsuspend,
2042 .resume = multipath_resume,
2043 .status = multipath_status,
2044 .message = multipath_message,
2045 .prepare_ioctl = multipath_prepare_ioctl,
2046 .iterate_devices = multipath_iterate_devices,
2047 .busy = multipath_busy,
2048};
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050static int __init dm_multipath_init(void)
2051{
2052 int r;
2053
Tejun Heo4d4d66a2011-01-13 19:59:57 +00002054 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002055 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01002056 DMERR("failed to create workqueue kmpathd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002057 r = -ENOMEM;
2058 goto bad_alloc_kmultipathd;
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002059 }
2060
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002061 /*
2062 * A separate workqueue is used to handle the device handlers
2063 * to avoid overloading existing workqueue. Overloading the
2064 * old workqueue would also create a bottleneck in the
2065 * path of the storage hardware device activation.
2066 */
Tejun Heo4d4d66a2011-01-13 19:59:57 +00002067 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
2068 WQ_MEM_RECLAIM);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002069 if (!kmpath_handlerd) {
2070 DMERR("failed to create workqueue kmpath_handlerd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002071 r = -ENOMEM;
2072 goto bad_alloc_kmpath_handlerd;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002073 }
2074
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002075 r = dm_register_target(&multipath_target);
2076 if (r < 0) {
2077 DMERR("request-based register failed %d", r);
2078 r = -EINVAL;
2079 goto bad_register_target;
2080 }
2081
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002082 return 0;
2083
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002084bad_register_target:
2085 destroy_workqueue(kmpath_handlerd);
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002086bad_alloc_kmpath_handlerd:
2087 destroy_workqueue(kmultipathd);
2088bad_alloc_kmultipathd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return r;
2090}
2091
2092static void __exit dm_multipath_exit(void)
2093{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002094 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002095 destroy_workqueue(kmultipathd);
2096
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002097 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100module_init(dm_multipath_init);
2101module_exit(dm_multipath_exit);
2102
2103MODULE_DESCRIPTION(DM_NAME " multipath target");
2104MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
2105MODULE_LICENSE("GPL");