blob: a3acbfb638be4904e8db440ed58843a659767449 [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 {
67 struct list_head list;
68 struct dm_target *ti;
69
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070070 const char *hw_handler_name;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -070071 char *hw_handler_params;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000072
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010073 spinlock_t lock;
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 unsigned nr_priority_groups;
76 struct list_head priority_groups;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000077
78 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 struct pgpath *current_pgpath;
81 struct priority_group *current_pg;
82 struct priority_group *next_pg; /* Switch to this PG if set */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Mike Snitzer518257b2016-03-17 16:32:10 -040084 unsigned long flags; /* Multipath state flags */
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010085
Dave Wysochanskic9e45582007-10-19 22:47:53 +010086 unsigned pg_init_retries; /* Number of times to retry pg_init */
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000087 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Mike Snitzer91e968a2016-03-17 17:10:15 -040089 atomic_t nr_valid_paths; /* Total number of usable paths */
90 atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */
91 atomic_t pg_init_count; /* Number of times pg_init called */
92
Bart Van Assche7e0d5742017-04-27 10:11:23 -070093 enum dm_queue_mode queue_mode;
Mike Snitzere83068a2016-05-24 21:16:51 -040094
Mike Anderson6380f262009-12-10 23:52:21 +000095 struct mutex work_mutex;
Mike Snitzer20800cb2016-03-17 17:13:10 -040096 struct work_struct trigger_event;
Mike Snitzer76e33fe2016-05-19 16:15:14 -040097
98 struct work_struct process_queued_bios;
99 struct bio_list queued_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
102/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400103 * Context information attached to each io we process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100105struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct pgpath *pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100107 size_t nr_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110typedef int (*action_fn) (struct pgpath *pgpath);
111
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700112static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000113static void trigger_event(struct work_struct *work);
Bart Van Assche89bfce72017-04-27 10:11:14 -0700114static void activate_or_offline_path(struct pgpath *pgpath);
115static void activate_path_work(struct work_struct *work);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400116static void process_queued_bios(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Mike Snitzer518257b2016-03-17 16:32:10 -0400118/*-----------------------------------------------
119 * Multipath state flags.
120 *-----------------------------------------------*/
121
122#define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */
123#define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */
124#define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */
125#define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */
126#define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */
127#define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */
128#define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/*-----------------------------------------------
131 * Allocation routines
132 *-----------------------------------------------*/
133
134static struct pgpath *alloc_pgpath(void)
135{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700136 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Mike Anderson224cb3e2008-08-29 09:36:09 +0200138 if (pgpath) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -0500139 pgpath->is_active = true;
Bart Van Assche89bfce72017-04-27 10:11:14 -0700140 INIT_DELAYED_WORK(&pgpath->activate_path, activate_path_work);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 return pgpath;
144}
145
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100146static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 kfree(pgpath);
149}
150
151static struct priority_group *alloc_priority_group(void)
152{
153 struct priority_group *pg;
154
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700155 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700157 if (pg)
158 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 return pg;
161}
162
163static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
164{
165 struct pgpath *pgpath, *tmp;
166
167 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
168 list_del(&pgpath->list);
169 dm_put_device(ti, pgpath->path.dev);
170 free_pgpath(pgpath);
171 }
172}
173
174static void free_priority_group(struct priority_group *pg,
175 struct dm_target *ti)
176{
177 struct path_selector *ps = &pg->ps;
178
179 if (ps->type) {
180 ps->type->destroy(ps);
181 dm_put_path_selector(ps->type);
182 }
183
184 free_pgpaths(&pg->pgpaths, ti);
185 kfree(pg);
186}
187
Mike Snitzere83068a2016-05-24 21:16:51 -0400188static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct multipath *m;
191
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700192 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 INIT_LIST_HEAD(&m->priority_groups);
195 spin_lock_init(&m->lock);
Mike Snitzer518257b2016-03-17 16:32:10 -0400196 set_bit(MPATHF_QUEUE_IO, &m->flags);
Mike Snitzer91e968a2016-03-17 17:10:15 -0400197 atomic_set(&m->nr_valid_paths, 0);
198 atomic_set(&m->pg_init_in_progress, 0);
199 atomic_set(&m->pg_init_count, 0);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000200 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
David Howellsc4028952006-11-22 14:57:56 +0000201 INIT_WORK(&m->trigger_event, trigger_event);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000202 init_waitqueue_head(&m->pg_init_wait);
Mike Anderson6380f262009-12-10 23:52:21 +0000203 mutex_init(&m->work_mutex);
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500204
Mike Snitzere83068a2016-05-24 21:16:51 -0400205 m->queue_mode = DM_TYPE_NONE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400206
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700207 m->ti = ti;
208 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 return m;
212}
213
Mike Snitzere83068a2016-05-24 21:16:51 -0400214static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m)
215{
216 if (m->queue_mode == DM_TYPE_NONE) {
217 /*
218 * Default to request-based.
219 */
220 if (dm_use_blk_mq(dm_table_get_md(ti->table)))
221 m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
222 else
223 m->queue_mode = DM_TYPE_REQUEST_BASED;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100224 } else if (m->queue_mode == DM_TYPE_BIO_BASED) {
Mike Snitzere83068a2016-05-24 21:16:51 -0400225 INIT_WORK(&m->process_queued_bios, process_queued_bios);
226 /*
227 * bio-based doesn't support any direct scsi_dh management;
228 * it just discovers if a scsi_dh is attached.
229 */
230 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
231 }
232
233 dm_table_set_type(ti->table, m->queue_mode);
234
235 return 0;
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static void free_multipath(struct multipath *m)
239{
240 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
243 list_del(&pg->list);
244 free_priority_group(pg, m->ti);
245 }
246
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700247 kfree(m->hw_handler_name);
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700248 kfree(m->hw_handler_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 kfree(m);
250}
251
Mike Snitzer2eff1922016-02-03 09:13:14 -0500252static struct dm_mpath_io *get_mpio(union map_info *info)
253{
254 return info->ptr;
255}
256
Mike Snitzerbf661be2016-05-24 15:48:08 -0400257static size_t multipath_per_bio_data_size(void)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400258{
Mike Snitzerbf661be2016-05-24 15:48:08 -0400259 return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400260}
261
Mike Snitzerbf661be2016-05-24 15:48:08 -0400262static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
263{
264 return dm_per_bio_data(bio, multipath_per_bio_data_size());
265}
266
267static struct dm_bio_details *get_bio_details_from_bio(struct bio *bio)
268{
269 /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */
270 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
271 void *bio_details = mpio + 1;
272
273 return bio_details;
274}
275
Mike Snitzer63f6e6f2017-12-05 14:10:33 -0500276static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400277{
278 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
Mike Snitzerbf661be2016-05-24 15:48:08 -0400279 struct dm_bio_details *bio_details = get_bio_details_from_bio(bio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400280
Mike Snitzerd0442f82017-12-11 15:58:41 -0500281 mpio->nr_bytes = bio->bi_iter.bi_size;
282 mpio->pgpath = NULL;
283 *mpio_p = mpio;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400284
Mike Snitzerd0442f82017-12-11 15:58:41 -0500285 dm_bio_record(bio_details, bio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/*-----------------------------------------------
289 * Path selection
290 *-----------------------------------------------*/
291
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100292static int __pg_init_all_paths(struct multipath *m)
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000293{
294 struct pgpath *pgpath;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000295 unsigned long pg_init_delay = 0;
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000296
Bart Van Asscheb1946792017-04-27 10:11:22 -0700297 lockdep_assert_held(&m->lock);
298
Mike Snitzer91e968a2016-03-17 17:10:15 -0400299 if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100300 return 0;
Hannes Reinecke17f4ff42014-02-28 15:33:42 +0100301
Mike Snitzer91e968a2016-03-17 17:10:15 -0400302 atomic_inc(&m->pg_init_count);
Mike Snitzer518257b2016-03-17 16:32:10 -0400303 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100304
305 /* Check here to reset pg_init_required */
306 if (!m->current_pg)
307 return 0;
308
Mike Snitzer518257b2016-03-17 16:32:10 -0400309 if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000310 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
311 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000312 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
313 /* Skip failed paths */
314 if (!pgpath->is_active)
315 continue;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000316 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
317 pg_init_delay))
Mike Snitzer91e968a2016-03-17 17:10:15 -0400318 atomic_inc(&m->pg_init_in_progress);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000319 }
Mike Snitzer91e968a2016-03-17 17:10:15 -0400320 return atomic_read(&m->pg_init_in_progress);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000321}
322
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700323static int pg_init_all_paths(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700325 int ret;
Mike Snitzer2da16102016-03-17 18:38:17 -0400326 unsigned long flags;
327
328 spin_lock_irqsave(&m->lock, flags);
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700329 ret = __pg_init_all_paths(m);
Mike Snitzer2da16102016-03-17 18:38:17 -0400330 spin_unlock_irqrestore(&m->lock, flags);
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700331
332 return ret;
Mike Snitzer2da16102016-03-17 18:38:17 -0400333}
334
335static void __switch_pg(struct multipath *m, struct priority_group *pg)
336{
337 m->current_pg = pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700340 if (m->hw_handler_name) {
Mike Snitzer518257b2016-03-17 16:32:10 -0400341 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
342 set_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 } else {
Mike Snitzer518257b2016-03-17 16:32:10 -0400344 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
345 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100347
Mike Snitzer91e968a2016-03-17 17:10:15 -0400348 atomic_set(&m->pg_init_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Mike Snitzer2da16102016-03-17 18:38:17 -0400351static struct pgpath *choose_path_in_pg(struct multipath *m,
352 struct priority_group *pg,
353 size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Mike Snitzer2da16102016-03-17 18:38:17 -0400355 unsigned long flags;
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800356 struct dm_path *path;
Mike Snitzer2da16102016-03-17 18:38:17 -0400357 struct pgpath *pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Mike Snitzer90a43232016-02-17 21:29:17 -0500359 path = pg->ps.type->select_path(&pg->ps, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (!path)
Mike Snitzer2da16102016-03-17 18:38:17 -0400361 return ERR_PTR(-ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Mike Snitzer2da16102016-03-17 18:38:17 -0400363 pgpath = path_to_pgpath(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Will Deacon506458e2017-10-24 11:22:48 +0100365 if (unlikely(READ_ONCE(m->current_pg) != pg)) {
Mike Snitzer2da16102016-03-17 18:38:17 -0400366 /* Only update current_pgpath if pg changed */
367 spin_lock_irqsave(&m->lock, flags);
368 m->current_pgpath = pgpath;
369 __switch_pg(m, pg);
370 spin_unlock_irqrestore(&m->lock, flags);
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Mike Snitzer2da16102016-03-17 18:38:17 -0400373 return pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Mike Snitzer2da16102016-03-17 18:38:17 -0400376static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Mike Snitzer2da16102016-03-17 18:38:17 -0400378 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct priority_group *pg;
Mike Snitzer2da16102016-03-17 18:38:17 -0400380 struct pgpath *pgpath;
Mike Snitzerd19a55c2017-01-06 15:33:14 -0500381 unsigned bypassed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Mike Snitzer91e968a2016-03-17 17:10:15 -0400383 if (!atomic_read(&m->nr_valid_paths)) {
Mike Snitzer518257b2016-03-17 16:32:10 -0400384 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 goto failed;
Benjamin Marzinski1f271972014-08-13 13:53:42 -0500386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /* Were we instructed to switch PG? */
Will Deacon506458e2017-10-24 11:22:48 +0100389 if (READ_ONCE(m->next_pg)) {
Mike Snitzer2da16102016-03-17 18:38:17 -0400390 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 pg = m->next_pg;
Mike Snitzer2da16102016-03-17 18:38:17 -0400392 if (!pg) {
393 spin_unlock_irqrestore(&m->lock, flags);
394 goto check_current_pg;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 m->next_pg = NULL;
Mike Snitzer2da16102016-03-17 18:38:17 -0400397 spin_unlock_irqrestore(&m->lock, flags);
398 pgpath = choose_path_in_pg(m, pg, nr_bytes);
399 if (!IS_ERR_OR_NULL(pgpath))
400 return pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
403 /* Don't change PG until it has no remaining paths */
Mike Snitzer2da16102016-03-17 18:38:17 -0400404check_current_pg:
Will Deacon506458e2017-10-24 11:22:48 +0100405 pg = READ_ONCE(m->current_pg);
Mike Snitzer2da16102016-03-17 18:38:17 -0400406 if (pg) {
407 pgpath = choose_path_in_pg(m, pg, nr_bytes);
408 if (!IS_ERR_OR_NULL(pgpath))
409 return pgpath;
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /*
413 * Loop through priority groups until we find a valid path.
414 * First time we skip PGs marked 'bypassed'.
Mike Christief220fd42012-06-03 00:29:45 +0100415 * Second time we only try the ones we skipped, but set
416 * pg_init_delay_retry so we do not hammer controllers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
418 do {
419 list_for_each_entry(pg, &m->priority_groups, list) {
Mike Snitzerd19a55c2017-01-06 15:33:14 -0500420 if (pg->bypassed == !!bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 continue;
Mike Snitzer2da16102016-03-17 18:38:17 -0400422 pgpath = choose_path_in_pg(m, pg, nr_bytes);
423 if (!IS_ERR_OR_NULL(pgpath)) {
Mike Christief220fd42012-06-03 00:29:45 +0100424 if (!bypassed)
Mike Snitzer518257b2016-03-17 16:32:10 -0400425 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
Mike Snitzer2da16102016-03-17 18:38:17 -0400426 return pgpath;
Mike Christief220fd42012-06-03 00:29:45 +0100427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 } while (bypassed--);
430
431failed:
Mike Snitzer2da16102016-03-17 18:38:17 -0400432 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 m->current_pgpath = NULL;
434 m->current_pg = NULL;
Mike Snitzer2da16102016-03-17 18:38:17 -0400435 spin_unlock_irqrestore(&m->lock, flags);
436
437 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800440/*
Bart Van Assche86331f32017-04-27 10:11:26 -0700441 * dm_report_EIO() is a macro instead of a function to make pr_debug()
442 * report the function name and line number of the function from which
443 * it has been invoked.
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800444 */
Bart Van Assche86331f32017-04-27 10:11:26 -0700445#define dm_report_EIO(m) \
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200446do { \
Bart Van Assche86331f32017-04-27 10:11:26 -0700447 struct mapped_device *md = dm_table_get_md((m)->ti->table); \
448 \
449 pr_debug("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d\n", \
450 dm_device_name(md), \
451 test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags), \
452 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags), \
453 dm_noflush_suspending((m)->ti)); \
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200454} while (0)
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800455
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100456/*
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500457 * Check whether bios must be queued in the device-mapper core rather
458 * than here in the target.
459 *
460 * If MPATHF_QUEUE_IF_NO_PATH and MPATHF_SAVED_QUEUE_IF_NO_PATH hold
461 * the same value then we are not between multipath_presuspend()
462 * and multipath_resume() calls and we have no need to check
463 * for the DMF_NOFLUSH_SUSPENDING flag.
464 */
465static bool __must_push_back(struct multipath *m, unsigned long flags)
466{
467 return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) !=
468 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &flags)) &&
469 dm_noflush_suspending(m->ti));
470}
471
472/*
473 * Following functions use READ_ONCE to get atomic access to
474 * all m->flags to avoid taking spinlock
475 */
476static bool must_push_back_rq(struct multipath *m)
477{
478 unsigned long flags = READ_ONCE(m->flags);
479 return test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) || __must_push_back(m, flags);
480}
481
482static bool must_push_back_bio(struct multipath *m)
483{
484 unsigned long flags = READ_ONCE(m->flags);
485 return __must_push_back(m, flags);
486}
487
488/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400489 * Map cloned requests (request-based multipath)
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100490 */
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100491static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
492 union map_info *map_context,
493 struct request **__clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Mike Snitzer7943bd62016-02-02 21:53:15 -0500495 struct multipath *m = ti->private;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100496 size_t nr_bytes = blk_rq_bytes(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100498 struct block_device *bdev;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100499 struct dm_mpath_io *mpio = get_mpio(map_context);
Bart Van Assche7083abb2017-04-27 10:11:15 -0700500 struct request_queue *q;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100501 struct request *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /* Do we need to select a new pgpath? */
Will Deacon506458e2017-10-24 11:22:48 +0100504 pgpath = READ_ONCE(m->current_pgpath);
Mike Snitzer2da16102016-03-17 18:38:17 -0400505 if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
506 pgpath = choose_pgpath(m, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100508 if (!pgpath) {
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500509 if (must_push_back_rq(m))
Mike Snitzerb88efd42016-09-09 19:26:19 -0400510 return DM_MAPIO_DELAY_REQUEUE;
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200511 dm_report_EIO(m); /* Failed */
Christoph Hellwigf98e0eb2017-05-15 17:28:38 +0200512 return DM_MAPIO_KILL;
Mike Snitzer518257b2016-03-17 16:32:10 -0400513 } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
514 test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700515 if (pg_init_all_paths(m))
516 return DM_MAPIO_DELAY_REQUEUE;
Bart Van Assche06eb0612017-04-07 16:50:44 -0700517 return DM_MAPIO_REQUEUE;
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100518 }
Mike Snitzer6afbc012014-07-08 11:55:09 -0400519
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100520 mpio->pgpath = pgpath;
521 mpio->nr_bytes = nr_bytes;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600522
523 bdev = pgpath->path.dev->bdev;
Bart Van Assche7083abb2017-04-27 10:11:15 -0700524 q = bdev_get_queue(bdev);
525 clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE, GFP_ATOMIC);
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100526 if (IS_ERR(clone)) {
527 /* EBUSY, ENODEV or EWOULDBLOCK: requeue */
Bart Van Assche7083abb2017-04-27 10:11:15 -0700528 bool queue_dying = blk_queue_dying(q);
Bart Van Assche7083abb2017-04-27 10:11:15 -0700529 if (queue_dying) {
530 atomic_inc(&m->pg_init_in_progress);
531 activate_or_offline_path(pgpath);
Bart Van Assche7083abb2017-04-27 10:11:15 -0700532 }
Bart Van Assche06eb0612017-04-07 16:50:44 -0700533 return DM_MAPIO_DELAY_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
Mike Snitzere5863d92014-12-17 21:08:12 -0500547static void multipath_release_clone(struct request *clone)
548{
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100549 blk_put_request(clone);
Mike Snitzere5863d92014-12-17 21:08:12 -0500550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400553 * Map cloned bios (bio-based multipath)
554 */
555static int __multipath_map_bio(struct multipath *m, struct bio *bio, struct dm_mpath_io *mpio)
556{
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400557 struct pgpath *pgpath;
558 unsigned long flags;
559 bool queue_io;
560
561 /* Do we need to select a new pgpath? */
Will Deacon506458e2017-10-24 11:22:48 +0100562 pgpath = READ_ONCE(m->current_pgpath);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400563 queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
564 if (!pgpath || !queue_io)
Mike Snitzerd0442f82017-12-11 15:58:41 -0500565 pgpath = choose_pgpath(m, mpio->nr_bytes);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400566
567 if ((pgpath && queue_io) ||
568 (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
569 /* Queue for the daemon to resubmit */
570 spin_lock_irqsave(&m->lock, flags);
571 bio_list_add(&m->queued_bios, bio);
572 spin_unlock_irqrestore(&m->lock, flags);
573 /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
574 if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
575 pg_init_all_paths(m);
576 else if (!queue_io)
577 queue_work(kmultipathd, &m->process_queued_bios);
578 return DM_MAPIO_SUBMITTED;
579 }
580
581 if (!pgpath) {
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500582 if (must_push_back_bio(m))
Bart Van Asscheca5beb72017-04-27 10:11:24 -0700583 return DM_MAPIO_REQUEUE;
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200584 dm_report_EIO(m);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200585 return DM_MAPIO_KILL;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400586 }
587
588 mpio->pgpath = pgpath;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400589
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200590 bio->bi_status = 0;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200591 bio_set_dev(bio, pgpath->path.dev->bdev);
Jens Axboe1eff9d32016-08-05 15:35:16 -0600592 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400593
594 if (pgpath->pg->ps.type->start_io)
595 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
596 &pgpath->path,
Mike Snitzerd0442f82017-12-11 15:58:41 -0500597 mpio->nr_bytes);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400598 return DM_MAPIO_REMAPPED;
599}
600
601static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
602{
603 struct multipath *m = ti->private;
Mike Snitzerbf661be2016-05-24 15:48:08 -0400604 struct dm_mpath_io *mpio = NULL;
605
Mike Snitzer63f6e6f2017-12-05 14:10:33 -0500606 multipath_init_per_bio_data(bio, &mpio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400607 return __multipath_map_bio(m, bio, mpio);
608}
609
Mike Snitzer7e48c762016-09-14 10:47:03 -0400610static void process_queued_io_list(struct multipath *m)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400611{
Mike Snitzer7e48c762016-09-14 10:47:03 -0400612 if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED)
613 dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
614 else if (m->queue_mode == DM_TYPE_BIO_BASED)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400615 queue_work(kmultipathd, &m->process_queued_bios);
616}
617
618static void process_queued_bios(struct work_struct *work)
619{
620 int r;
621 unsigned long flags;
622 struct bio *bio;
623 struct bio_list bios;
624 struct blk_plug plug;
625 struct multipath *m =
626 container_of(work, struct multipath, process_queued_bios);
627
628 bio_list_init(&bios);
629
630 spin_lock_irqsave(&m->lock, flags);
631
632 if (bio_list_empty(&m->queued_bios)) {
633 spin_unlock_irqrestore(&m->lock, flags);
634 return;
635 }
636
637 bio_list_merge(&bios, &m->queued_bios);
638 bio_list_init(&m->queued_bios);
639
640 spin_unlock_irqrestore(&m->lock, flags);
641
642 blk_start_plug(&plug);
643 while ((bio = bio_list_pop(&bios))) {
644 r = __multipath_map_bio(m, bio, get_mpio_from_bio(bio));
Christoph Hellwig846785e2017-06-03 09:38:02 +0200645 switch (r) {
646 case DM_MAPIO_KILL:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200647 bio->bi_status = BLK_STS_IOERR;
648 bio_endio(bio);
Dan Carpenter047385b2017-06-14 08:22:55 -0600649 break;
Christoph Hellwig846785e2017-06-03 09:38:02 +0200650 case DM_MAPIO_REQUEUE:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200651 bio->bi_status = BLK_STS_DM_REQUEUE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400652 bio_endio(bio);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200653 break;
654 case DM_MAPIO_REMAPPED:
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400655 generic_make_request(bio);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200656 break;
Bart Van Assche9157c8d2017-08-09 11:32:14 -0700657 case 0:
658 break;
659 default:
660 WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200661 }
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400662 }
663 blk_finish_plug(&plug);
664}
665
666/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 * If we run out of usable paths, should we queue I/O or error it?
668 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -0500669static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
670 bool save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 unsigned long flags;
673
674 spin_lock_irqsave(&m->lock, flags);
Lukas Wunner5307e2a2017-10-12 12:40:10 +0200675 assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags,
676 (save_old_value && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) ||
677 (!save_old_value && queue_if_no_path));
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500678 assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 spin_unlock_irqrestore(&m->lock, flags);
680
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400681 if (!queue_if_no_path) {
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200682 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -0400683 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400684 }
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return 0;
687}
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689/*
690 * An event is triggered whenever a path is taken out of use.
691 * Includes path failure and PG bypass.
692 */
David Howellsc4028952006-11-22 14:57:56 +0000693static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
David Howellsc4028952006-11-22 14:57:56 +0000695 struct multipath *m =
696 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 dm_table_event(m->ti->table);
699}
700
701/*-----------------------------------------------------------------
702 * Constructor/argument parsing:
703 * <#multipath feature args> [<arg>]*
704 * <#hw_handler args> [hw_handler [<arg>]*]
705 * <#priority groups>
706 * <initial priority group>
707 * [<selector> <#selector args> [<arg>]*
708 * <#paths> <#per-path selector args>
709 * [<path> [<arg>]* ]+ ]+
710 *---------------------------------------------------------------*/
Mike Snitzer498f0102011-08-02 12:32:04 +0100711static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct dm_target *ti)
713{
714 int r;
715 struct path_selector_type *pst;
716 unsigned ps_argc;
717
Eric Biggers5916a222017-06-22 11:32:45 -0700718 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700719 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 };
721
Mike Snitzer498f0102011-08-02 12:32:04 +0100722 pst = dm_get_path_selector(dm_shift_arg(as));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700724 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return -EINVAL;
726 }
727
Mike Snitzer498f0102011-08-02 12:32:04 +0100728 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100729 if (r) {
730 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 r = pst->create(&pg->ps, ps_argc, as->argv);
735 if (r) {
736 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700737 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return r;
739 }
740
741 pg->ps.type = pst;
Mike Snitzer498f0102011-08-02 12:32:04 +0100742 dm_consume_args(as, ps_argc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 return 0;
745}
746
Mike Snitzer498f0102011-08-02 12:32:04 +0100747static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 struct dm_target *ti)
749{
750 int r;
751 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700752 struct multipath *m = ti->private;
Mike Snitzera58a9352012-07-27 15:08:04 +0100753 struct request_queue *q = NULL;
754 const char *attached_handler_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* we need at least a path arg */
757 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700758 ti->error = "no device given";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100759 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761
762 p = alloc_pgpath();
763 if (!p)
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100764 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Mike Snitzer498f0102011-08-02 12:32:04 +0100766 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +0000767 &p->path.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700769 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 goto bad;
771 }
772
Mike Snitzer518257b2016-03-17 16:32:10 -0400773 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) || m->hw_handler_name)
Mike Snitzera58a9352012-07-27 15:08:04 +0100774 q = bdev_get_queue(p->path.dev->bdev);
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100775
Mike Snitzer518257b2016-03-17 16:32:10 -0400776 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200777retain:
Mike Snitzera58a9352012-07-27 15:08:04 +0100778 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
779 if (attached_handler_name) {
780 /*
tang.junhui54cd6402016-11-24 15:11:48 +0800781 * Clear any hw_handler_params associated with a
782 * handler that isn't already attached.
783 */
784 if (m->hw_handler_name && strcmp(attached_handler_name, m->hw_handler_name)) {
785 kfree(m->hw_handler_params);
786 m->hw_handler_params = NULL;
787 }
788
789 /*
Mike Snitzera58a9352012-07-27 15:08:04 +0100790 * Reset hw_handler_name to match the attached handler
Mike Snitzera58a9352012-07-27 15:08:04 +0100791 *
792 * NB. This modifies the table line to show the actual
793 * handler instead of the original table passed in.
794 */
795 kfree(m->hw_handler_name);
796 m->hw_handler_name = attached_handler_name;
Mike Snitzera58a9352012-07-27 15:08:04 +0100797 }
798 }
799
800 if (m->hw_handler_name) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100801 r = scsi_dh_attach(q, m->hw_handler_name);
802 if (r == -EBUSY) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200803 char b[BDEVNAME_SIZE];
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100804
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200805 printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
806 bdevname(p->path.dev->bdev, b));
807 goto retain;
808 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700809 if (r < 0) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100810 ti->error = "error attaching hardware handler";
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700811 dm_put_device(ti, p->path.dev);
812 goto bad;
813 }
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700814
815 if (m->hw_handler_params) {
816 r = scsi_dh_set_params(q, m->hw_handler_params);
817 if (r < 0) {
818 ti->error = "unable to set hardware "
819 "handler parameters";
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700820 dm_put_device(ti, p->path.dev);
821 goto bad;
822 }
823 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700824 }
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
827 if (r) {
828 dm_put_device(ti, p->path.dev);
829 goto bad;
830 }
831
832 return p;
833
834 bad:
835 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100836 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Mike Snitzer498f0102011-08-02 12:32:04 +0100839static struct priority_group *parse_priority_group(struct dm_arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700840 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Eric Biggers5916a222017-06-22 11:32:45 -0700842 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700843 {1, 1024, "invalid number of paths"},
844 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 };
846
847 int r;
Mike Snitzer498f0102011-08-02 12:32:04 +0100848 unsigned i, nr_selector_args, nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700850 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (as->argc < 2) {
853 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100854 ti->error = "not enough priority group arguments";
855 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
858 pg = alloc_priority_group();
859 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700860 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100861 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863 pg->m = m;
864
865 r = parse_path_selector(as, pg, ti);
866 if (r)
867 goto bad;
868
869 /*
870 * read the paths
871 */
Mike Snitzer498f0102011-08-02 12:32:04 +0100872 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (r)
874 goto bad;
875
Mike Snitzer498f0102011-08-02 12:32:04 +0100876 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (r)
878 goto bad;
879
Mike Snitzer498f0102011-08-02 12:32:04 +0100880 nr_args = 1 + nr_selector_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 for (i = 0; i < pg->nr_pgpaths; i++) {
882 struct pgpath *pgpath;
Mike Snitzer498f0102011-08-02 12:32:04 +0100883 struct dm_arg_set path_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Mike Snitzer498f0102011-08-02 12:32:04 +0100885 if (as->argc < nr_args) {
Mikulas Patocka148acff2008-07-21 12:00:30 +0100886 ti->error = "not enough path parameters";
Alasdair G Kergon6bbf79a12010-08-12 04:13:49 +0100887 r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Mike Snitzer498f0102011-08-02 12:32:04 +0100891 path_args.argc = nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 path_args.argv = as->argv;
893
894 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100895 if (IS_ERR(pgpath)) {
896 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 pgpath->pg = pg;
901 list_add_tail(&pgpath->list, &pg->pgpaths);
Mike Snitzer498f0102011-08-02 12:32:04 +0100902 dm_consume_args(as, nr_args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
905 return pg;
906
907 bad:
908 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100909 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Mike Snitzer498f0102011-08-02 12:32:04 +0100912static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 unsigned hw_argc;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700915 int ret;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700916 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Eric Biggers5916a222017-06-22 11:32:45 -0700918 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700919 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 };
921
Mike Snitzer498f0102011-08-02 12:32:04 +0100922 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return -EINVAL;
924
925 if (!hw_argc)
926 return 0;
927
Mike Snitzere83068a2016-05-24 21:16:51 -0400928 if (m->queue_mode == DM_TYPE_BIO_BASED) {
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400929 dm_consume_args(as, hw_argc);
930 DMERR("bio-based multipath doesn't allow hardware handler args");
931 return 0;
932 }
933
Mike Snitzer498f0102011-08-02 12:32:04 +0100934 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
tang.junhuif97dc422016-10-28 17:04:46 +0800935 if (!m->hw_handler_name)
936 return -EINVAL;
Chandra Seetharaman14e98c52008-11-13 23:39:06 +0000937
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700938 if (hw_argc > 1) {
939 char *p;
940 int i, j, len = 4;
941
942 for (i = 0; i <= hw_argc - 2; i++)
943 len += strlen(as->argv[i]) + 1;
944 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
945 if (!p) {
946 ti->error = "memory allocation failed";
947 ret = -ENOMEM;
948 goto fail;
949 }
950 j = sprintf(p, "%d", hw_argc - 1);
951 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
952 j = sprintf(p, "%s", as->argv[i]);
953 }
Mike Snitzer498f0102011-08-02 12:32:04 +0100954 dm_consume_args(as, hw_argc - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 return 0;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700957fail:
958 kfree(m->hw_handler_name);
959 m->hw_handler_name = NULL;
960 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Mike Snitzer498f0102011-08-02 12:32:04 +0100963static int parse_features(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 int r;
966 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700967 struct dm_target *ti = m->ti;
Mike Snitzer498f0102011-08-02 12:32:04 +0100968 const char *arg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Eric Biggers5916a222017-06-22 11:32:45 -0700970 static const struct dm_arg _args[] = {
Mike Snitzere83068a2016-05-24 21:16:51 -0400971 {0, 8, "invalid number of feature args"},
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100972 {1, 50, "pg_init_retries must be between 1 and 50"},
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000973 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 };
975
Mike Snitzer498f0102011-08-02 12:32:04 +0100976 r = dm_read_arg_group(_args, as, &argc, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (r)
978 return -EINVAL;
979
980 if (!argc)
981 return 0;
982
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100983 do {
Mike Snitzer498f0102011-08-02 12:32:04 +0100984 arg_name = dm_shift_arg(as);
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100985 argc--;
986
Mike Snitzer498f0102011-08-02 12:32:04 +0100987 if (!strcasecmp(arg_name, "queue_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -0500988 r = queue_if_no_path(m, true, false);
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100989 continue;
990 }
991
Mike Snitzera58a9352012-07-27 15:08:04 +0100992 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
Mike Snitzer518257b2016-03-17 16:32:10 -0400993 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
Mike Snitzera58a9352012-07-27 15:08:04 +0100994 continue;
995 }
996
Mike Snitzer498f0102011-08-02 12:32:04 +0100997 if (!strcasecmp(arg_name, "pg_init_retries") &&
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100998 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +0100999 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001000 argc--;
1001 continue;
1002 }
1003
Mike Snitzer498f0102011-08-02 12:32:04 +01001004 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001005 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001006 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001007 argc--;
1008 continue;
1009 }
1010
Mike Snitzere83068a2016-05-24 21:16:51 -04001011 if (!strcasecmp(arg_name, "queue_mode") &&
1012 (argc >= 1)) {
1013 const char *queue_mode_name = dm_shift_arg(as);
1014
1015 if (!strcasecmp(queue_mode_name, "bio"))
1016 m->queue_mode = DM_TYPE_BIO_BASED;
1017 else if (!strcasecmp(queue_mode_name, "rq"))
1018 m->queue_mode = DM_TYPE_REQUEST_BASED;
1019 else if (!strcasecmp(queue_mode_name, "mq"))
1020 m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
1021 else {
1022 ti->error = "Unknown 'queue_mode' requested";
1023 r = -EINVAL;
1024 }
1025 argc--;
1026 continue;
1027 }
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001030 r = -EINVAL;
1031 } while (argc && !r);
1032
1033 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Mike Snitzere83068a2016-05-24 21:16:51 -04001036static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Mike Snitzer498f0102011-08-02 12:32:04 +01001038 /* target arguments */
Eric Biggers5916a222017-06-22 11:32:45 -07001039 static const struct dm_arg _args[] = {
Mike Snitzera490a072011-03-24 13:54:33 +00001040 {0, 1024, "invalid number of priority groups"},
1041 {0, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 };
1043
1044 int r;
1045 struct multipath *m;
Mike Snitzer498f0102011-08-02 12:32:04 +01001046 struct dm_arg_set as;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 unsigned pg_count = 0;
1048 unsigned next_pg_num;
1049
1050 as.argc = argc;
1051 as.argv = argv;
1052
Mike Snitzere83068a2016-05-24 21:16:51 -04001053 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001055 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return -EINVAL;
1057 }
1058
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001059 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (r)
1061 goto bad;
1062
Mike Snitzere83068a2016-05-24 21:16:51 -04001063 r = alloc_multipath_stage2(ti, m);
1064 if (r)
1065 goto bad;
1066
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001067 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (r)
1069 goto bad;
1070
Mike Snitzer498f0102011-08-02 12:32:04 +01001071 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (r)
1073 goto bad;
1074
Mike Snitzer498f0102011-08-02 12:32:04 +01001075 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (r)
1077 goto bad;
1078
Mike Snitzera490a072011-03-24 13:54:33 +00001079 if ((!m->nr_priority_groups && next_pg_num) ||
1080 (m->nr_priority_groups && !next_pg_num)) {
1081 ti->error = "invalid initial priority group";
1082 r = -EINVAL;
1083 goto bad;
1084 }
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /* parse the priority groups */
1087 while (as.argc) {
1088 struct priority_group *pg;
Mike Snitzer91e968a2016-03-17 17:10:15 -04001089 unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001091 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +01001092 if (IS_ERR(pg)) {
1093 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 goto bad;
1095 }
1096
Mike Snitzer91e968a2016-03-17 17:10:15 -04001097 nr_valid_paths += pg->nr_pgpaths;
1098 atomic_set(&m->nr_valid_paths, nr_valid_paths);
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 list_add_tail(&pg->list, &m->priority_groups);
1101 pg_count++;
1102 pg->pg_num = pg_count;
1103 if (!--next_pg_num)
1104 m->next_pg = pg;
1105 }
1106
1107 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001108 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 r = -EINVAL;
1110 goto bad;
1111 }
1112
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001113 ti->num_flush_bios = 1;
1114 ti->num_discard_bios = 1;
Mike Snitzer042bcef2013-05-10 14:37:16 +01001115 ti->num_write_same_bios = 1;
Christoph Hellwigac62d622017-04-05 19:21:05 +02001116 ti->num_write_zeroes_bios = 1;
Mike Snitzere83068a2016-05-24 21:16:51 -04001117 if (m->queue_mode == DM_TYPE_BIO_BASED)
Mike Snitzerbf661be2016-05-24 15:48:08 -04001118 ti->per_io_data_size = multipath_per_bio_data_size();
Christoph Hellwigeb8db832017-01-22 18:32:46 +01001119 else
Mike Snitzer8637a6b2016-01-31 12:08:36 -05001120 ti->per_io_data_size = sizeof(struct dm_mpath_io);
Mikulas Patocka86279212009-06-22 10:12:24 +01001121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return 0;
1123
1124 bad:
1125 free_multipath(m);
1126 return r;
1127}
1128
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001129static void multipath_wait_for_pg_init_completion(struct multipath *m)
1130{
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001131 DEFINE_WAIT(wait);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001132
1133 while (1) {
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001134 prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001135
Mike Snitzer91e968a2016-03-17 17:10:15 -04001136 if (!atomic_read(&m->pg_init_in_progress))
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001137 break;
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001138
1139 io_schedule();
1140 }
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001141 finish_wait(&m->pg_init_wait, &wait);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001142}
1143
1144static void flush_multipath_work(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Mike Snitzer518257b2016-03-17 16:32:10 -04001146 set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1147 smp_mb__after_atomic();
Shiva Krishna Merla954a73d2013-10-30 03:26:38 +00001148
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001149 flush_workqueue(kmpath_handlerd);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001150 multipath_wait_for_pg_init_completion(m);
Alasdair G Kergona044d012005-07-12 15:53:02 -07001151 flush_workqueue(kmultipathd);
Tejun Heo43829732012-08-20 14:51:24 -07001152 flush_work(&m->trigger_event);
Shiva Krishna Merla954a73d2013-10-30 03:26:38 +00001153
Mike Snitzer518257b2016-03-17 16:32:10 -04001154 clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1155 smp_mb__after_atomic();
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001156}
1157
1158static void multipath_dtr(struct dm_target *ti)
1159{
1160 struct multipath *m = ti->private;
1161
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001162 flush_multipath_work(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 free_multipath(m);
1164}
1165
1166/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 * Take a path out of use.
1168 */
1169static int fail_path(struct pgpath *pgpath)
1170{
1171 unsigned long flags;
1172 struct multipath *m = pgpath->pg->m;
1173
1174 spin_lock_irqsave(&m->lock, flags);
1175
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001176 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 goto out;
1178
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001179 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001182 pgpath->is_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 pgpath->fail_count++;
1184
Mike Snitzer91e968a2016-03-17 17:10:15 -04001185 atomic_dec(&m->nr_valid_paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 if (pgpath == m->current_pgpath)
1188 m->current_pgpath = NULL;
1189
Mike Andersonb15546f2007-10-19 22:48:02 +01001190 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
Mike Snitzer91e968a2016-03-17 17:10:15 -04001191 pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
Mike Andersonb15546f2007-10-19 22:48:02 +01001192
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001193 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195out:
1196 spin_unlock_irqrestore(&m->lock, flags);
1197
1198 return 0;
1199}
1200
1201/*
1202 * Reinstate a previously-failed path
1203 */
1204static int reinstate_path(struct pgpath *pgpath)
1205{
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001206 int r = 0, run_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 unsigned long flags;
1208 struct multipath *m = pgpath->pg->m;
Mike Snitzer91e968a2016-03-17 17:10:15 -04001209 unsigned nr_valid_paths;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 spin_lock_irqsave(&m->lock, flags);
1212
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001213 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 goto out;
1215
Mike Snitzerec31f3f2016-02-20 12:49:43 -05001216 DMWARN("Reinstating path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1219 if (r)
1220 goto out;
1221
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001222 pgpath->is_active = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Mike Snitzer91e968a2016-03-17 17:10:15 -04001224 nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
1225 if (nr_valid_paths == 1) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001226 m->current_pgpath = NULL;
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001227 run_queue = 1;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001228 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001229 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
Mike Snitzer91e968a2016-03-17 17:10:15 -04001230 atomic_inc(&m->pg_init_in_progress);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Mike Andersonb15546f2007-10-19 22:48:02 +01001233 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
Mike Snitzer91e968a2016-03-17 17:10:15 -04001234 pgpath->path.dev->name, nr_valid_paths);
Mike Andersonb15546f2007-10-19 22:48:02 +01001235
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001236 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238out:
1239 spin_unlock_irqrestore(&m->lock, flags);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001240 if (run_queue) {
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001241 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -04001242 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 return r;
1246}
1247
1248/*
1249 * Fail or reinstate all paths that match the provided struct dm_dev.
1250 */
1251static int action_dev(struct multipath *m, struct dm_dev *dev,
1252 action_fn action)
1253{
Mike Snitzer19040c02011-03-24 13:54:31 +00001254 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 struct pgpath *pgpath;
1256 struct priority_group *pg;
1257
1258 list_for_each_entry(pg, &m->priority_groups, list) {
1259 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1260 if (pgpath->path.dev == dev)
1261 r = action(pgpath);
1262 }
1263 }
1264
1265 return r;
1266}
1267
1268/*
1269 * Temporarily try to avoid having to use the specified PG
1270 */
1271static void bypass_pg(struct multipath *m, struct priority_group *pg,
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001272 bool bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 unsigned long flags;
1275
1276 spin_lock_irqsave(&m->lock, flags);
1277
1278 pg->bypassed = bypassed;
1279 m->current_pgpath = NULL;
1280 m->current_pg = NULL;
1281
1282 spin_unlock_irqrestore(&m->lock, flags);
1283
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001284 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
1287/*
1288 * Switch to using the specified PG from the next I/O that gets mapped
1289 */
1290static int switch_pg_num(struct multipath *m, const char *pgstr)
1291{
1292 struct priority_group *pg;
1293 unsigned pgnum;
1294 unsigned long flags;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001295 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001297 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
tang.junhuicc5bd922016-11-04 12:37:09 +08001298 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 DMWARN("invalid PG number supplied to switch_pg_num");
1300 return -EINVAL;
1301 }
1302
1303 spin_lock_irqsave(&m->lock, flags);
1304 list_for_each_entry(pg, &m->priority_groups, list) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001305 pg->bypassed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (--pgnum)
1307 continue;
1308
1309 m->current_pgpath = NULL;
1310 m->current_pg = NULL;
1311 m->next_pg = pg;
1312 }
1313 spin_unlock_irqrestore(&m->lock, flags);
1314
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001315 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return 0;
1317}
1318
1319/*
1320 * Set/clear bypassed status of a PG.
1321 * PGs are numbered upwards from 1 in the order they were declared.
1322 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001323static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 struct priority_group *pg;
1326 unsigned pgnum;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001327 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001329 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
tang.junhuicc5bd922016-11-04 12:37:09 +08001330 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 DMWARN("invalid PG number supplied to bypass_pg");
1332 return -EINVAL;
1333 }
1334
1335 list_for_each_entry(pg, &m->priority_groups, list) {
1336 if (!--pgnum)
1337 break;
1338 }
1339
1340 bypass_pg(m, pg, bypassed);
1341 return 0;
1342}
1343
1344/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001345 * Should we retry pg_init immediately?
1346 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001347static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001348{
1349 unsigned long flags;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001350 bool limit_reached = false;
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001351
1352 spin_lock_irqsave(&m->lock, flags);
1353
Mike Snitzer91e968a2016-03-17 17:10:15 -04001354 if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
1355 !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
Mike Snitzer518257b2016-03-17 16:32:10 -04001356 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001357 else
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001358 limit_reached = true;
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001359
1360 spin_unlock_irqrestore(&m->lock, flags);
1361
1362 return limit_reached;
1363}
1364
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001365static void pg_init_done(void *data, int errors)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001366{
Moger, Babu83c0d5d2010-03-06 02:29:45 +00001367 struct pgpath *pgpath = data;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001368 struct priority_group *pg = pgpath->pg;
1369 struct multipath *m = pg->m;
1370 unsigned long flags;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001371 bool delay_retry = false;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001372
1373 /* device or driver problems */
1374 switch (errors) {
1375 case SCSI_DH_OK:
1376 break;
1377 case SCSI_DH_NOSYS:
1378 if (!m->hw_handler_name) {
1379 errors = 0;
1380 break;
1381 }
Moger, Babuf7b934c2010-03-06 02:29:49 +00001382 DMERR("Could not failover the device: Handler scsi_dh_%s "
1383 "Error %d.", m->hw_handler_name, errors);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001384 /*
1385 * Fail path for now, so we do not ping pong
1386 */
1387 fail_path(pgpath);
1388 break;
1389 case SCSI_DH_DEV_TEMP_BUSY:
1390 /*
1391 * Probably doing something like FW upgrade on the
1392 * controller so try the other pg.
1393 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001394 bypass_pg(m, pg, true);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001395 break;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001396 case SCSI_DH_RETRY:
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001397 /* Wait before retrying. */
1398 delay_retry = 1;
Bart Van Assche7b06e092017-08-09 11:32:13 -07001399 /* fall through */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001400 case SCSI_DH_IMM_RETRY:
1401 case SCSI_DH_RES_TEMP_UNAVAIL:
1402 if (pg_init_limit_reached(m, pgpath))
1403 fail_path(pgpath);
1404 errors = 0;
1405 break;
Mike Snitzerec31f3f2016-02-20 12:49:43 -05001406 case SCSI_DH_DEV_OFFLINED:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001407 default:
1408 /*
1409 * We probably do not want to fail the path for a device
1410 * error, but this is what the old dm did. In future
1411 * patches we can do more advanced handling.
1412 */
1413 fail_path(pgpath);
1414 }
1415
1416 spin_lock_irqsave(&m->lock, flags);
1417 if (errors) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001418 if (pgpath == m->current_pgpath) {
1419 DMERR("Could not failover device. Error %d.", errors);
1420 m->current_pgpath = NULL;
1421 m->current_pg = NULL;
1422 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001423 } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001424 pg->bypassed = false;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001425
Mike Snitzer91e968a2016-03-17 17:10:15 -04001426 if (atomic_dec_return(&m->pg_init_in_progress) > 0)
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001427 /* Activations of other paths are still on going */
1428 goto out;
1429
Mike Snitzer518257b2016-03-17 16:32:10 -04001430 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1431 if (delay_retry)
1432 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1433 else
1434 clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1435
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001436 if (__pg_init_all_paths(m))
1437 goto out;
1438 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001439 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001440
Mike Snitzer7e48c762016-09-14 10:47:03 -04001441 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001442
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001443 /*
1444 * Wake up any thread waiting to suspend.
1445 */
1446 wake_up(&m->pg_init_wait);
1447
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001448out:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001449 spin_unlock_irqrestore(&m->lock, flags);
1450}
1451
Bart Van Assche89bfce72017-04-27 10:11:14 -07001452static void activate_or_offline_path(struct pgpath *pgpath)
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001453{
Mike Snitzerf10e06b2016-09-01 12:06:37 -04001454 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001455
Mike Snitzerf10e06b2016-09-01 12:06:37 -04001456 if (pgpath->is_active && !blk_queue_dying(q))
1457 scsi_dh_activate(q, pg_init_done, pgpath);
Hannes Reinecke3a017502014-02-28 15:33:49 +01001458 else
1459 pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001460}
1461
Bart Van Assche89bfce72017-04-27 10:11:14 -07001462static void activate_path_work(struct work_struct *work)
1463{
1464 struct pgpath *pgpath =
1465 container_of(work, struct pgpath, activate_path.work);
1466
1467 activate_or_offline_path(pgpath);
1468}
1469
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001470static int noretry_error(blk_status_t error)
Hannes Reinecke7e782af2013-07-01 15:16:26 +02001471{
1472 switch (error) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001473 case BLK_STS_NOTSUPP:
1474 case BLK_STS_NOSPC:
1475 case BLK_STS_TARGET:
1476 case BLK_STS_NEXUS:
1477 case BLK_STS_MEDIUM:
Hannes Reinecke7e782af2013-07-01 15:16:26 +02001478 return 1;
1479 }
1480
1481 /* Anything else could be a path failure, so should be retried */
1482 return 0;
1483}
1484
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001485static int multipath_end_io(struct dm_target *ti, struct request *clone,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001486 blk_status_t error, union map_info *map_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001488 struct dm_mpath_io *mpio = get_mpio(map_context);
1489 struct pgpath *pgpath = mpio->pgpath;
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001490 int r = DM_ENDIO_DONE;
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001491
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001492 /*
1493 * We don't queue any clone request inside the multipath target
1494 * during end I/O handling, since those clone requests don't have
1495 * bio clones. If we queue them inside the multipath target,
1496 * we need to make bio clones, that requires memory allocation.
Mike Snitzer4cc96132016-05-12 16:28:10 -04001497 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001498 * don't have bio clones.)
1499 * Instead of queueing the clone request here, we queue the original
1500 * request into dm core, which will remake a clone request and
1501 * clone bios for it and resubmit it later.
1502 */
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001503 if (error && !noretry_error(error)) {
1504 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001506 r = DM_ENDIO_REQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001508 if (pgpath)
1509 fail_path(pgpath);
Mike Snitzer959eb4e2010-08-12 04:14:32 +01001510
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001511 if (atomic_read(&m->nr_valid_paths) == 0 &&
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -05001512 !must_push_back_rq(m)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001513 if (error == BLK_STS_IOERR)
Christoph Hellwig18a482f2017-05-15 17:28:37 +02001514 dm_report_EIO(m);
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001515 /* complete with the original error */
1516 r = DM_ENDIO_DONE;
1517 }
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (pgpath) {
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001521 struct path_selector *ps = &pgpath->pg->ps;
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (ps->type->end_io)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001524 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001527 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001530static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone,
1531 blk_status_t *error)
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001532{
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001533 struct multipath *m = ti->private;
1534 struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
1535 struct pgpath *pgpath = mpio->pgpath;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001536 unsigned long flags;
Christoph Hellwig1be56902017-06-03 09:38:03 +02001537 int r = DM_ENDIO_DONE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001538
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001539 if (!*error || noretry_error(*error))
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001540 goto done;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001541
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001542 if (pgpath)
1543 fail_path(pgpath);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001544
Bart Van Asscheca5beb72017-04-27 10:11:24 -07001545 if (atomic_read(&m->nr_valid_paths) == 0 &&
Christoph Hellwig18a482f2017-05-15 17:28:37 +02001546 !test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -05001547 if (must_push_back_bio(m)) {
1548 r = DM_ENDIO_REQUEUE;
1549 } else {
1550 dm_report_EIO(m);
1551 *error = BLK_STS_IOERR;
1552 }
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001553 goto done;
Christoph Hellwig18a482f2017-05-15 17:28:37 +02001554 }
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001555
1556 /* Queue for the daemon to resubmit */
Mike Snitzerbf661be2016-05-24 15:48:08 -04001557 dm_bio_restore(get_bio_details_from_bio(clone), clone);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001558
1559 spin_lock_irqsave(&m->lock, flags);
1560 bio_list_add(&m->queued_bios, clone);
1561 spin_unlock_irqrestore(&m->lock, flags);
1562 if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
1563 queue_work(kmultipathd, &m->process_queued_bios);
1564
Christoph Hellwig1be56902017-06-03 09:38:03 +02001565 r = DM_ENDIO_INCOMPLETE;
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001566done:
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001567 if (pgpath) {
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001568 struct path_selector *ps = &pgpath->pg->ps;
1569
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001570 if (ps->type->end_io)
1571 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1572 }
1573
Christoph Hellwig1be56902017-06-03 09:38:03 +02001574 return r;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001575}
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577/*
1578 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001579 * the last path fails we must error any remaining I/O.
1580 * Note that if the freeze_bdev fails while suspending, the
1581 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 */
1583static void multipath_presuspend(struct dm_target *ti)
1584{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001585 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001587 queue_if_no_path(m, false, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001590static void multipath_postsuspend(struct dm_target *ti)
1591{
Mike Anderson6380f262009-12-10 23:52:21 +00001592 struct multipath *m = ti->private;
1593
1594 mutex_lock(&m->work_mutex);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001595 flush_multipath_work(m);
Mike Anderson6380f262009-12-10 23:52:21 +00001596 mutex_unlock(&m->work_mutex);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001597}
1598
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001599/*
1600 * Restore the queue_if_no_path setting.
1601 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602static void multipath_resume(struct dm_target *ti)
1603{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001604 struct multipath *m = ti->private;
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001605 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001607 spin_lock_irqsave(&m->lock, flags);
Lukas Wunner5307e2a2017-10-12 12:40:10 +02001608 assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags,
1609 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags));
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001610 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
1613/*
1614 * Info output has the following format:
1615 * num_multipath_feature_args [multipath_feature_args]*
1616 * num_handler_status_args [handler_status_args]*
1617 * num_groups init_group_number
1618 * [A|D|E num_ps_status_args [ps_status_args]*
1619 * num_paths num_selector_args
1620 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1621 *
1622 * Table output has the following format (identical to the constructor string):
1623 * num_feature_args [features_args]*
1624 * num_handler_args hw_handler [hw_handler_args]*
1625 * num_groups init_group_number
1626 * [priority selector-name num_ps_args [ps_args]*
1627 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1628 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001629static void multipath_status(struct dm_target *ti, status_type_t type,
1630 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632 int sz = 0;
1633 unsigned long flags;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001634 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 struct priority_group *pg;
1636 struct pgpath *p;
1637 unsigned pg_num;
1638 char state;
1639
1640 spin_lock_irqsave(&m->lock, flags);
1641
1642 /* Features */
1643 if (type == STATUSTYPE_INFO)
Mike Snitzer91e968a2016-03-17 17:10:15 -04001644 DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
1645 atomic_read(&m->pg_init_count));
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001646 else {
Mike Snitzer518257b2016-03-17 16:32:10 -04001647 DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001648 (m->pg_init_retries > 0) * 2 +
Mike Snitzera58a9352012-07-27 15:08:04 +01001649 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
Mike Snitzere83068a2016-05-24 21:16:51 -04001650 test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
1651 (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
1652
Mike Snitzer518257b2016-03-17 16:32:10 -04001653 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001654 DMEMIT("queue_if_no_path ");
1655 if (m->pg_init_retries)
1656 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001657 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1658 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
Mike Snitzer518257b2016-03-17 16:32:10 -04001659 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
Mike Snitzera58a9352012-07-27 15:08:04 +01001660 DMEMIT("retain_attached_hw_handler ");
Mike Snitzere83068a2016-05-24 21:16:51 -04001661 if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
1662 switch(m->queue_mode) {
1663 case DM_TYPE_BIO_BASED:
1664 DMEMIT("queue_mode bio ");
1665 break;
1666 case DM_TYPE_MQ_REQUEST_BASED:
1667 DMEMIT("queue_mode mq ");
1668 break;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07001669 default:
1670 WARN_ON_ONCE(true);
1671 break;
Mike Snitzere83068a2016-05-24 21:16:51 -04001672 }
1673 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001676 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 DMEMIT("0 ");
1678 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001679 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 DMEMIT("%u ", m->nr_priority_groups);
1682
1683 if (m->next_pg)
1684 pg_num = m->next_pg->pg_num;
1685 else if (m->current_pg)
1686 pg_num = m->current_pg->pg_num;
1687 else
Mike Snitzera490a072011-03-24 13:54:33 +00001688 pg_num = (m->nr_priority_groups ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 DMEMIT("%u ", pg_num);
1691
1692 switch (type) {
1693 case STATUSTYPE_INFO:
1694 list_for_each_entry(pg, &m->priority_groups, list) {
1695 if (pg->bypassed)
1696 state = 'D'; /* Disabled */
1697 else if (pg == m->current_pg)
1698 state = 'A'; /* Currently Active */
1699 else
1700 state = 'E'; /* Enabled */
1701
1702 DMEMIT("%c ", state);
1703
1704 if (pg->ps.type->status)
1705 sz += pg->ps.type->status(&pg->ps, NULL, type,
1706 result + sz,
1707 maxlen - sz);
1708 else
1709 DMEMIT("0 ");
1710
1711 DMEMIT("%u %u ", pg->nr_pgpaths,
1712 pg->ps.type->info_args);
1713
1714 list_for_each_entry(p, &pg->pgpaths, list) {
1715 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001716 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 p->fail_count);
1718 if (pg->ps.type->status)
1719 sz += pg->ps.type->status(&pg->ps,
1720 &p->path, type, result + sz,
1721 maxlen - sz);
1722 }
1723 }
1724 break;
1725
1726 case STATUSTYPE_TABLE:
1727 list_for_each_entry(pg, &m->priority_groups, list) {
1728 DMEMIT("%s ", pg->ps.type->name);
1729
1730 if (pg->ps.type->status)
1731 sz += pg->ps.type->status(&pg->ps, NULL, type,
1732 result + sz,
1733 maxlen - sz);
1734 else
1735 DMEMIT("0 ");
1736
1737 DMEMIT("%u %u ", pg->nr_pgpaths,
1738 pg->ps.type->table_args);
1739
1740 list_for_each_entry(p, &pg->pgpaths, list) {
1741 DMEMIT("%s ", p->path.dev->name);
1742 if (pg->ps.type->status)
1743 sz += pg->ps.type->status(&pg->ps,
1744 &p->path, type, result + sz,
1745 maxlen - sz);
1746 }
1747 }
1748 break;
1749 }
1750
1751 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
1754static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1755{
Mike Anderson6380f262009-12-10 23:52:21 +00001756 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 struct dm_dev *dev;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001758 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 action_fn action;
1760
Mike Anderson6380f262009-12-10 23:52:21 +00001761 mutex_lock(&m->work_mutex);
1762
Kiyoshi Uedac2f3d242009-12-10 23:52:27 +00001763 if (dm_suspended(ti)) {
1764 r = -EBUSY;
1765 goto out;
1766 }
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 if (argc == 1) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001769 if (!strcasecmp(argv[0], "queue_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001770 r = queue_if_no_path(m, true, false);
Mike Anderson6380f262009-12-10 23:52:21 +00001771 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001772 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001773 r = queue_if_no_path(m, false, false);
Mike Anderson6380f262009-12-10 23:52:21 +00001774 goto out;
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777
Mike Anderson6380f262009-12-10 23:52:21 +00001778 if (argc != 2) {
Jose Castilloa356e422014-01-29 17:52:45 +01001779 DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
Mike Anderson6380f262009-12-10 23:52:21 +00001780 goto out;
1781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Mike Snitzer498f0102011-08-02 12:32:04 +01001783 if (!strcasecmp(argv[0], "disable_group")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001784 r = bypass_pg_num(m, argv[1], true);
Mike Anderson6380f262009-12-10 23:52:21 +00001785 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001786 } else if (!strcasecmp(argv[0], "enable_group")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001787 r = bypass_pg_num(m, argv[1], false);
Mike Anderson6380f262009-12-10 23:52:21 +00001788 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001789 } else if (!strcasecmp(argv[0], "switch_group")) {
Mike Anderson6380f262009-12-10 23:52:21 +00001790 r = switch_pg_num(m, argv[1]);
1791 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001792 } else if (!strcasecmp(argv[0], "reinstate_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 action = reinstate_path;
Mike Snitzer498f0102011-08-02 12:32:04 +01001794 else if (!strcasecmp(argv[0], "fail_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 action = fail_path;
Mike Anderson6380f262009-12-10 23:52:21 +00001796 else {
Jose Castilloa356e422014-01-29 17:52:45 +01001797 DMWARN("Unrecognised multipath message received: %s", argv[0]);
Mike Anderson6380f262009-12-10 23:52:21 +00001798 goto out;
1799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00001801 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001803 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 argv[1]);
Mike Anderson6380f262009-12-10 23:52:21 +00001805 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
1807
1808 r = action_dev(m, dev, action);
1809
1810 dm_put_device(ti, dev);
1811
Mike Anderson6380f262009-12-10 23:52:21 +00001812out:
1813 mutex_unlock(&m->work_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001817static int multipath_prepare_ioctl(struct dm_target *ti,
1818 struct block_device **bdev, fmode_t *mode)
Milan Broz9af4aa32006-10-03 01:15:20 -07001819{
Mikulas Patocka35991652012-06-03 00:29:58 +01001820 struct multipath *m = ti->private;
Mike Snitzer2da16102016-03-17 18:38:17 -04001821 struct pgpath *current_pgpath;
Mikulas Patocka35991652012-06-03 00:29:58 +01001822 int r;
1823
Will Deacon506458e2017-10-24 11:22:48 +01001824 current_pgpath = READ_ONCE(m->current_pgpath);
Mike Snitzer2da16102016-03-17 18:38:17 -04001825 if (!current_pgpath)
1826 current_pgpath = choose_pgpath(m, 0);
Milan Broz9af4aa32006-10-03 01:15:20 -07001827
Mike Snitzer2da16102016-03-17 18:38:17 -04001828 if (current_pgpath) {
Mike Snitzer518257b2016-03-17 16:32:10 -04001829 if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
Mike Snitzer2da16102016-03-17 18:38:17 -04001830 *bdev = current_pgpath->path.dev->bdev;
1831 *mode = current_pgpath->path.dev->mode;
Junichi Nomura43e43c92015-11-17 09:36:56 +00001832 r = 0;
1833 } else {
1834 /* pg_init has not started or completed */
1835 r = -ENOTCONN;
1836 }
1837 } else {
1838 /* No path is available */
Mike Snitzer518257b2016-03-17 16:32:10 -04001839 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Junichi Nomura43e43c92015-11-17 09:36:56 +00001840 r = -ENOTCONN;
1841 else
1842 r = -EIO;
Milan Broze90dae12006-10-03 01:15:22 -07001843 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001844
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +00001845 if (r == -ENOTCONN) {
Will Deacon506458e2017-10-24 11:22:48 +01001846 if (!READ_ONCE(m->current_pg)) {
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001847 /* Path status changed, redo selection */
Mike Snitzer2da16102016-03-17 18:38:17 -04001848 (void) choose_pgpath(m, 0);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001849 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001850 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
Mike Snitzer2da16102016-03-17 18:38:17 -04001851 pg_init_all_paths(m);
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001852 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -04001853 process_queued_io_list(m);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001854 }
Mikulas Patocka35991652012-06-03 00:29:58 +01001855
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001856 /*
1857 * Only pass ioctls through if the device sizes match exactly.
1858 */
1859 if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1860 return 1;
1861 return r;
Milan Broz9af4aa32006-10-03 01:15:20 -07001862}
1863
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001864static int multipath_iterate_devices(struct dm_target *ti,
1865 iterate_devices_callout_fn fn, void *data)
1866{
1867 struct multipath *m = ti->private;
1868 struct priority_group *pg;
1869 struct pgpath *p;
1870 int ret = 0;
1871
1872 list_for_each_entry(pg, &m->priority_groups, list) {
1873 list_for_each_entry(p, &pg->pgpaths, list) {
Mike Snitzer5dea2712009-07-23 20:30:42 +01001874 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001875 if (ret)
1876 goto out;
1877 }
1878 }
1879
1880out:
1881 return ret;
1882}
1883
Mike Snitzer9f54cec2016-02-11 21:42:28 -05001884static int pgpath_busy(struct pgpath *pgpath)
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001885{
1886 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1887
Mike Snitzer52b09912015-02-23 16:36:41 -05001888 return blk_lld_busy(q);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001889}
1890
1891/*
1892 * We return "busy", only when we can map I/Os but underlying devices
1893 * are busy (so even if we map I/Os now, the I/Os will wait on
1894 * the underlying queue).
1895 * In other words, if we want to kill I/Os or queue them inside us
1896 * due to map unavailability, we don't return "busy". Otherwise,
1897 * dm core won't give us the I/Os and we can't do what we want.
1898 */
1899static int multipath_busy(struct dm_target *ti)
1900{
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001901 bool busy = false, has_active = false;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001902 struct multipath *m = ti->private;
Mike Snitzer2da16102016-03-17 18:38:17 -04001903 struct priority_group *pg, *next_pg;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001904 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001905
Mike Snitzerb88efd42016-09-09 19:26:19 -04001906 /* pg_init in progress */
1907 if (atomic_read(&m->pg_init_in_progress))
Mike Snitzer2da16102016-03-17 18:38:17 -04001908 return true;
1909
Mike Snitzerb88efd42016-09-09 19:26:19 -04001910 /* no paths available, for blk-mq: rely on IO mapping to delay requeue */
1911 if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1912 return (m->queue_mode != DM_TYPE_MQ_REQUEST_BASED);
1913
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001914 /* Guess which priority_group will be used at next mapping time */
Will Deacon506458e2017-10-24 11:22:48 +01001915 pg = READ_ONCE(m->current_pg);
1916 next_pg = READ_ONCE(m->next_pg);
1917 if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg))
Mike Snitzer2da16102016-03-17 18:38:17 -04001918 pg = next_pg;
1919
1920 if (!pg) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001921 /*
1922 * We don't know which pg will be used at next mapping time.
Mike Snitzer2da16102016-03-17 18:38:17 -04001923 * We don't call choose_pgpath() here to avoid to trigger
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001924 * pg_init just by busy checking.
1925 * So we don't know whether underlying devices we will be using
1926 * at next mapping time are busy or not. Just try mapping.
1927 */
Mike Snitzer2da16102016-03-17 18:38:17 -04001928 return busy;
1929 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001930
1931 /*
1932 * If there is one non-busy active path at least, the path selector
1933 * will be able to select it. So we consider such a pg as not busy.
1934 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001935 busy = true;
Mike Snitzer2da16102016-03-17 18:38:17 -04001936 list_for_each_entry(pgpath, &pg->pgpaths, list) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001937 if (pgpath->is_active) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001938 has_active = true;
Mike Snitzer9f54cec2016-02-11 21:42:28 -05001939 if (!pgpath_busy(pgpath)) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001940 busy = false;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001941 break;
1942 }
1943 }
Mike Snitzer2da16102016-03-17 18:38:17 -04001944 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001945
Mike Snitzer2da16102016-03-17 18:38:17 -04001946 if (!has_active) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001947 /*
1948 * No active path in this pg, so this pg won't be used and
1949 * the current_pg will be changed at next mapping time.
1950 * We need to try mapping to determine it.
1951 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001952 busy = false;
Mike Snitzer2da16102016-03-17 18:38:17 -04001953 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001954
1955 return busy;
1956}
1957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958/*-----------------------------------------------------------------
1959 * Module setup
1960 *---------------------------------------------------------------*/
1961static struct target_type multipath_target = {
1962 .name = "multipath",
Mike Snitzere83068a2016-05-24 21:16:51 -04001963 .version = {1, 12, 0},
Mike Snitzer16f12262016-01-31 17:22:27 -05001964 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 .module = THIS_MODULE,
1966 .ctr = multipath_ctr,
1967 .dtr = multipath_dtr,
Mike Snitzere5863d92014-12-17 21:08:12 -05001968 .clone_and_map_rq = multipath_clone_and_map,
1969 .release_clone_rq = multipath_release_clone,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001970 .rq_end_io = multipath_end_io,
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001971 .map = multipath_map_bio,
1972 .end_io = multipath_end_io_bio,
1973 .presuspend = multipath_presuspend,
1974 .postsuspend = multipath_postsuspend,
1975 .resume = multipath_resume,
1976 .status = multipath_status,
1977 .message = multipath_message,
1978 .prepare_ioctl = multipath_prepare_ioctl,
1979 .iterate_devices = multipath_iterate_devices,
1980 .busy = multipath_busy,
1981};
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983static int __init dm_multipath_init(void)
1984{
1985 int r;
1986
Tejun Heo4d4d66a2011-01-13 19:59:57 +00001987 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001988 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001989 DMERR("failed to create workqueue kmpathd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01001990 r = -ENOMEM;
1991 goto bad_alloc_kmultipathd;
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001992 }
1993
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001994 /*
1995 * A separate workqueue is used to handle the device handlers
1996 * to avoid overloading existing workqueue. Overloading the
1997 * old workqueue would also create a bottleneck in the
1998 * path of the storage hardware device activation.
1999 */
Tejun Heo4d4d66a2011-01-13 19:59:57 +00002000 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
2001 WQ_MEM_RECLAIM);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002002 if (!kmpath_handlerd) {
2003 DMERR("failed to create workqueue kmpath_handlerd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002004 r = -ENOMEM;
2005 goto bad_alloc_kmpath_handlerd;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002006 }
2007
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002008 r = dm_register_target(&multipath_target);
2009 if (r < 0) {
2010 DMERR("request-based register failed %d", r);
2011 r = -EINVAL;
2012 goto bad_register_target;
2013 }
2014
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002015 return 0;
2016
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002017bad_register_target:
2018 destroy_workqueue(kmpath_handlerd);
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002019bad_alloc_kmpath_handlerd:
2020 destroy_workqueue(kmultipathd);
2021bad_alloc_kmultipathd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return r;
2023}
2024
2025static void __exit dm_multipath_exit(void)
2026{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002027 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002028 destroy_workqueue(kmultipathd);
2029
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002030 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033module_init(dm_multipath_init);
2034module_exit(dm_multipath_exit);
2035
2036MODULE_DESCRIPTION(DM_NAME " multipath target");
2037MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
2038MODULE_LICENSE("GPL");