blob: 3fde9e9faddd049d9de9092fd3fadb7378ff9e69 [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>
Mike Snitzer8d47e652018-03-05 14:10:11 -050025#include <scsi/scsi_device.h>
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070026#include <scsi/scsi_dh.h>
Arun Sharma600634972011-07-26 16:09:06 -070027#include <linux/atomic.h>
Mike Snitzer78ce23b2016-01-31 17:38:28 -050028#include <linux/blk-mq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Alasdair G Kergon72d94862006-06-26 00:27:35 -070030#define DM_MSG_PREFIX "multipath"
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000031#define DM_PG_INIT_DELAY_MSECS 2000
32#define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* Path properties */
35struct pgpath {
36 struct list_head list;
37
38 struct priority_group *pg; /* Owning PG */
39 unsigned fail_count; /* Cumulative failure count */
40
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080041 struct dm_path path;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000042 struct delayed_work activate_path;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -050043
44 bool is_active:1; /* Path status */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045};
46
47#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
48
49/*
50 * Paths are grouped into Priority Groups and numbered from 1 upwards.
51 * Each has a path selector which controls which path gets used.
52 */
53struct priority_group {
54 struct list_head list;
55
56 struct multipath *m; /* Owning multipath instance */
57 struct path_selector ps;
58
59 unsigned pg_num; /* Reference number */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 unsigned nr_pgpaths; /* Number of paths in PG */
61 struct list_head pgpaths;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -050062
63 bool bypassed:1; /* Temporarily bypass this PG? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66/* Multipath context */
67struct multipath {
Mike Snitzer848b8ae2017-12-10 15:37:21 -050068 unsigned long flags; /* Multipath state flags */
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000069
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010070 spinlock_t lock;
Mike Snitzer848b8ae2017-12-10 15:37:21 -050071 enum dm_queue_mode queue_mode;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct pgpath *current_pgpath;
74 struct priority_group *current_pg;
75 struct priority_group *next_pg; /* Switch to this PG if set */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Mike Snitzer848b8ae2017-12-10 15:37:21 -050077 atomic_t nr_valid_paths; /* Total number of usable paths */
78 unsigned nr_priority_groups;
79 struct list_head priority_groups;
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010080
Mike Snitzer848b8ae2017-12-10 15:37:21 -050081 const char *hw_handler_name;
82 char *hw_handler_params;
83 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
Dave Wysochanskic9e45582007-10-19 22:47:53 +010084 unsigned pg_init_retries; /* Number of times to retry pg_init */
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000085 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
Mike Snitzer91e968a2016-03-17 17:10:15 -040086 atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */
87 atomic_t pg_init_count; /* Number of times pg_init called */
88
Mike Anderson6380f262009-12-10 23:52:21 +000089 struct mutex work_mutex;
Mike Snitzer20800cb2016-03-17 17:13:10 -040090 struct work_struct trigger_event;
Mike Snitzer848b8ae2017-12-10 15:37:21 -050091 struct dm_target *ti;
Mike Snitzer76e33fe2016-05-19 16:15:14 -040092
93 struct work_struct process_queued_bios;
94 struct bio_list queued_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
97/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -040098 * Context information attached to each io we process.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100100struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct pgpath *pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100102 size_t nr_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105typedef int (*action_fn) (struct pgpath *pgpath);
106
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700107static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000108static void trigger_event(struct work_struct *work);
Bart Van Assche89bfce72017-04-27 10:11:14 -0700109static void activate_or_offline_path(struct pgpath *pgpath);
110static void activate_path_work(struct work_struct *work);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400111static void process_queued_bios(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Mike Snitzer518257b2016-03-17 16:32:10 -0400113/*-----------------------------------------------
114 * Multipath state flags.
115 *-----------------------------------------------*/
116
117#define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */
118#define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */
119#define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */
120#define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */
121#define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */
122#define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */
123#define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125/*-----------------------------------------------
126 * Allocation routines
127 *-----------------------------------------------*/
128
129static struct pgpath *alloc_pgpath(void)
130{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700131 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500133 if (!pgpath)
134 return NULL;
135
136 pgpath->is_active = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 return pgpath;
139}
140
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100141static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 kfree(pgpath);
144}
145
146static struct priority_group *alloc_priority_group(void)
147{
148 struct priority_group *pg;
149
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700150 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700152 if (pg)
153 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 return pg;
156}
157
158static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
159{
160 struct pgpath *pgpath, *tmp;
161
162 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
163 list_del(&pgpath->list);
164 dm_put_device(ti, pgpath->path.dev);
165 free_pgpath(pgpath);
166 }
167}
168
169static void free_priority_group(struct priority_group *pg,
170 struct dm_target *ti)
171{
172 struct path_selector *ps = &pg->ps;
173
174 if (ps->type) {
175 ps->type->destroy(ps);
176 dm_put_path_selector(ps->type);
177 }
178
179 free_pgpaths(&pg->pgpaths, ti);
180 kfree(pg);
181}
182
Mike Snitzere83068a2016-05-24 21:16:51 -0400183static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct multipath *m;
186
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700187 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 INIT_LIST_HEAD(&m->priority_groups);
190 spin_lock_init(&m->lock);
Mike Snitzer91e968a2016-03-17 17:10:15 -0400191 atomic_set(&m->nr_valid_paths, 0);
David Howellsc4028952006-11-22 14:57:56 +0000192 INIT_WORK(&m->trigger_event, trigger_event);
Mike Anderson6380f262009-12-10 23:52:21 +0000193 mutex_init(&m->work_mutex);
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500194
Mike Snitzere83068a2016-05-24 21:16:51 -0400195 m->queue_mode = DM_TYPE_NONE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400196
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700197 m->ti = ti;
198 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200
201 return m;
202}
203
Mike Snitzere83068a2016-05-24 21:16:51 -0400204static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m)
205{
206 if (m->queue_mode == DM_TYPE_NONE) {
207 /*
208 * Default to request-based.
209 */
210 if (dm_use_blk_mq(dm_table_get_md(ti->table)))
211 m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
212 else
213 m->queue_mode = DM_TYPE_REQUEST_BASED;
Mike Snitzercd025382017-12-05 16:02:21 -0500214
Mike Snitzer8d47e652018-03-05 14:10:11 -0500215 } else if (m->queue_mode == DM_TYPE_BIO_BASED) {
Mike Snitzere83068a2016-05-24 21:16:51 -0400216 INIT_WORK(&m->process_queued_bios, process_queued_bios);
Mike Snitzer8d47e652018-03-05 14:10:11 -0500217 /*
218 * bio-based doesn't support any direct scsi_dh management;
219 * it just discovers if a scsi_dh is attached.
220 */
221 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
Mike Snitzere83068a2016-05-24 21:16:51 -0400222 }
223
224 dm_table_set_type(ti->table, m->queue_mode);
225
226 return 0;
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static void free_multipath(struct multipath *m)
230{
231 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
234 list_del(&pg->list);
235 free_priority_group(pg, m->ti);
236 }
237
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700238 kfree(m->hw_handler_name);
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700239 kfree(m->hw_handler_params);
Mike Snitzerd5ffebd2018-01-05 21:17:20 -0500240 mutex_destroy(&m->work_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 kfree(m);
242}
243
Mike Snitzer2eff1922016-02-03 09:13:14 -0500244static struct dm_mpath_io *get_mpio(union map_info *info)
245{
246 return info->ptr;
247}
248
Mike Snitzerbf661be2016-05-24 15:48:08 -0400249static size_t multipath_per_bio_data_size(void)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400250{
Mike Snitzerbf661be2016-05-24 15:48:08 -0400251 return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400252}
253
Mike Snitzerbf661be2016-05-24 15:48:08 -0400254static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
255{
256 return dm_per_bio_data(bio, multipath_per_bio_data_size());
257}
258
Mike Snitzerd07a2412017-12-11 16:08:54 -0500259static struct dm_bio_details *get_bio_details_from_mpio(struct dm_mpath_io *mpio)
Mike Snitzerbf661be2016-05-24 15:48:08 -0400260{
261 /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */
Mike Snitzerbf661be2016-05-24 15:48:08 -0400262 void *bio_details = mpio + 1;
Mike Snitzerbf661be2016-05-24 15:48:08 -0400263 return bio_details;
264}
265
Mike Snitzer63f6e6f2017-12-05 14:10:33 -0500266static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400267{
268 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
Mike Snitzerd07a2412017-12-11 16:08:54 -0500269 struct dm_bio_details *bio_details = get_bio_details_from_mpio(mpio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400270
Mike Snitzerd0442f82017-12-11 15:58:41 -0500271 mpio->nr_bytes = bio->bi_iter.bi_size;
272 mpio->pgpath = NULL;
273 *mpio_p = mpio;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400274
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400275 dm_bio_record(bio_details, bio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/*-----------------------------------------------
279 * Path selection
280 *-----------------------------------------------*/
281
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100282static int __pg_init_all_paths(struct multipath *m)
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000283{
284 struct pgpath *pgpath;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000285 unsigned long pg_init_delay = 0;
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000286
Bart Van Asscheb1946792017-04-27 10:11:22 -0700287 lockdep_assert_held(&m->lock);
288
Mike Snitzer91e968a2016-03-17 17:10:15 -0400289 if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100290 return 0;
Hannes Reinecke17f4ff42014-02-28 15:33:42 +0100291
Mike Snitzer91e968a2016-03-17 17:10:15 -0400292 atomic_inc(&m->pg_init_count);
Mike Snitzer518257b2016-03-17 16:32:10 -0400293 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100294
295 /* Check here to reset pg_init_required */
296 if (!m->current_pg)
297 return 0;
298
Mike Snitzer518257b2016-03-17 16:32:10 -0400299 if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000300 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
301 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000302 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
303 /* Skip failed paths */
304 if (!pgpath->is_active)
305 continue;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000306 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
307 pg_init_delay))
Mike Snitzer91e968a2016-03-17 17:10:15 -0400308 atomic_inc(&m->pg_init_in_progress);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000309 }
Mike Snitzer91e968a2016-03-17 17:10:15 -0400310 return atomic_read(&m->pg_init_in_progress);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000311}
312
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700313static int pg_init_all_paths(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700315 int ret;
Mike Snitzer2da16102016-03-17 18:38:17 -0400316 unsigned long flags;
317
318 spin_lock_irqsave(&m->lock, flags);
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700319 ret = __pg_init_all_paths(m);
Mike Snitzer2da16102016-03-17 18:38:17 -0400320 spin_unlock_irqrestore(&m->lock, flags);
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700321
322 return ret;
Mike Snitzer2da16102016-03-17 18:38:17 -0400323}
324
325static void __switch_pg(struct multipath *m, struct priority_group *pg)
326{
327 m->current_pg = pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700330 if (m->hw_handler_name) {
Mike Snitzer518257b2016-03-17 16:32:10 -0400331 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
332 set_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 } else {
Mike Snitzer8d47e652018-03-05 14:10:11 -0500334 /* FIXME: not needed if no scsi_dh is attached */
Mike Snitzer518257b2016-03-17 16:32:10 -0400335 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
336 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100338
Mike Snitzer91e968a2016-03-17 17:10:15 -0400339 atomic_set(&m->pg_init_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Mike Snitzer2da16102016-03-17 18:38:17 -0400342static struct pgpath *choose_path_in_pg(struct multipath *m,
343 struct priority_group *pg,
344 size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Mike Snitzer2da16102016-03-17 18:38:17 -0400346 unsigned long flags;
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800347 struct dm_path *path;
Mike Snitzer2da16102016-03-17 18:38:17 -0400348 struct pgpath *pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Mike Snitzer90a43232016-02-17 21:29:17 -0500350 path = pg->ps.type->select_path(&pg->ps, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (!path)
Mike Snitzer2da16102016-03-17 18:38:17 -0400352 return ERR_PTR(-ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Mike Snitzer2da16102016-03-17 18:38:17 -0400354 pgpath = path_to_pgpath(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Will Deacon506458e2017-10-24 11:22:48 +0100356 if (unlikely(READ_ONCE(m->current_pg) != pg)) {
Mike Snitzer2da16102016-03-17 18:38:17 -0400357 /* Only update current_pgpath if pg changed */
358 spin_lock_irqsave(&m->lock, flags);
359 m->current_pgpath = pgpath;
360 __switch_pg(m, pg);
361 spin_unlock_irqrestore(&m->lock, flags);
362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Mike Snitzer2da16102016-03-17 18:38:17 -0400364 return pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Mike Snitzer2da16102016-03-17 18:38:17 -0400367static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Mike Snitzer2da16102016-03-17 18:38:17 -0400369 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 struct priority_group *pg;
Mike Snitzer2da16102016-03-17 18:38:17 -0400371 struct pgpath *pgpath;
Mike Snitzerd19a55c2017-01-06 15:33:14 -0500372 unsigned bypassed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Mike Snitzer91e968a2016-03-17 17:10:15 -0400374 if (!atomic_read(&m->nr_valid_paths)) {
Mike Snitzer8d47e652018-03-05 14:10:11 -0500375 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 goto failed;
Benjamin Marzinski1f271972014-08-13 13:53:42 -0500377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* Were we instructed to switch PG? */
Will Deacon506458e2017-10-24 11:22:48 +0100380 if (READ_ONCE(m->next_pg)) {
Mike Snitzer2da16102016-03-17 18:38:17 -0400381 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 pg = m->next_pg;
Mike Snitzer2da16102016-03-17 18:38:17 -0400383 if (!pg) {
384 spin_unlock_irqrestore(&m->lock, flags);
385 goto check_current_pg;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 m->next_pg = NULL;
Mike Snitzer2da16102016-03-17 18:38:17 -0400388 spin_unlock_irqrestore(&m->lock, flags);
389 pgpath = choose_path_in_pg(m, pg, nr_bytes);
390 if (!IS_ERR_OR_NULL(pgpath))
391 return pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
394 /* Don't change PG until it has no remaining paths */
Mike Snitzer2da16102016-03-17 18:38:17 -0400395check_current_pg:
Will Deacon506458e2017-10-24 11:22:48 +0100396 pg = READ_ONCE(m->current_pg);
Mike Snitzer2da16102016-03-17 18:38:17 -0400397 if (pg) {
398 pgpath = choose_path_in_pg(m, pg, nr_bytes);
399 if (!IS_ERR_OR_NULL(pgpath))
400 return pgpath;
401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /*
404 * Loop through priority groups until we find a valid path.
405 * First time we skip PGs marked 'bypassed'.
Mike Christief220fd42012-06-03 00:29:45 +0100406 * Second time we only try the ones we skipped, but set
407 * pg_init_delay_retry so we do not hammer controllers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 */
409 do {
410 list_for_each_entry(pg, &m->priority_groups, list) {
Mike Snitzerd19a55c2017-01-06 15:33:14 -0500411 if (pg->bypassed == !!bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 continue;
Mike Snitzer2da16102016-03-17 18:38:17 -0400413 pgpath = choose_path_in_pg(m, pg, nr_bytes);
414 if (!IS_ERR_OR_NULL(pgpath)) {
Mike Christief220fd42012-06-03 00:29:45 +0100415 if (!bypassed)
Mike Snitzer518257b2016-03-17 16:32:10 -0400416 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
Mike Snitzer2da16102016-03-17 18:38:17 -0400417 return pgpath;
Mike Christief220fd42012-06-03 00:29:45 +0100418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420 } while (bypassed--);
421
422failed:
Mike Snitzer2da16102016-03-17 18:38:17 -0400423 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 m->current_pgpath = NULL;
425 m->current_pg = NULL;
Mike Snitzer2da16102016-03-17 18:38:17 -0400426 spin_unlock_irqrestore(&m->lock, flags);
427
428 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800431/*
Bart Van Assche86331f32017-04-27 10:11:26 -0700432 * dm_report_EIO() is a macro instead of a function to make pr_debug()
433 * report the function name and line number of the function from which
434 * it has been invoked.
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800435 */
Bart Van Assche86331f32017-04-27 10:11:26 -0700436#define dm_report_EIO(m) \
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200437do { \
Bart Van Assche86331f32017-04-27 10:11:26 -0700438 struct mapped_device *md = dm_table_get_md((m)->ti->table); \
439 \
440 pr_debug("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d\n", \
441 dm_device_name(md), \
442 test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags), \
443 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags), \
444 dm_noflush_suspending((m)->ti)); \
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200445} while (0)
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800446
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100447/*
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500448 * Check whether bios must be queued in the device-mapper core rather
449 * than here in the target.
450 *
451 * If MPATHF_QUEUE_IF_NO_PATH and MPATHF_SAVED_QUEUE_IF_NO_PATH hold
452 * the same value then we are not between multipath_presuspend()
453 * and multipath_resume() calls and we have no need to check
454 * for the DMF_NOFLUSH_SUSPENDING flag.
455 */
456static bool __must_push_back(struct multipath *m, unsigned long flags)
457{
458 return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) !=
459 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &flags)) &&
460 dm_noflush_suspending(m->ti));
461}
462
463/*
464 * Following functions use READ_ONCE to get atomic access to
465 * all m->flags to avoid taking spinlock
466 */
467static bool must_push_back_rq(struct multipath *m)
468{
469 unsigned long flags = READ_ONCE(m->flags);
470 return test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) || __must_push_back(m, flags);
471}
472
473static bool must_push_back_bio(struct multipath *m)
474{
475 unsigned long flags = READ_ONCE(m->flags);
476 return __must_push_back(m, flags);
477}
478
479/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400480 * Map cloned requests (request-based multipath)
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100481 */
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100482static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
483 union map_info *map_context,
484 struct request **__clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Mike Snitzer7943bd62016-02-02 21:53:15 -0500486 struct multipath *m = ti->private;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100487 size_t nr_bytes = blk_rq_bytes(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100489 struct block_device *bdev;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100490 struct dm_mpath_io *mpio = get_mpio(map_context);
Bart Van Assche7083abb2017-04-27 10:11:15 -0700491 struct request_queue *q;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100492 struct request *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /* Do we need to select a new pgpath? */
Will Deacon506458e2017-10-24 11:22:48 +0100495 pgpath = READ_ONCE(m->current_pgpath);
Mike Snitzer2da16102016-03-17 18:38:17 -0400496 if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
497 pgpath = choose_pgpath(m, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100499 if (!pgpath) {
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500500 if (must_push_back_rq(m))
Mike Snitzerb88efd42016-09-09 19:26:19 -0400501 return DM_MAPIO_DELAY_REQUEUE;
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200502 dm_report_EIO(m); /* Failed */
Christoph Hellwigf98e0eb2017-05-15 17:28:38 +0200503 return DM_MAPIO_KILL;
Mike Snitzer518257b2016-03-17 16:32:10 -0400504 } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
505 test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
Ming Lei459b5402018-01-11 14:01:55 +0800506 pg_init_all_paths(m);
507 return DM_MAPIO_DELAY_REQUEUE;
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100508 }
Mike Snitzer6afbc012014-07-08 11:55:09 -0400509
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100510 mpio->pgpath = pgpath;
511 mpio->nr_bytes = nr_bytes;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600512
513 bdev = pgpath->path.dev->bdev;
Bart Van Assche7083abb2017-04-27 10:11:15 -0700514 q = bdev_get_queue(bdev);
515 clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE, GFP_ATOMIC);
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100516 if (IS_ERR(clone)) {
517 /* EBUSY, ENODEV or EWOULDBLOCK: requeue */
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500518 if (blk_queue_dying(q)) {
Bart Van Assche7083abb2017-04-27 10:11:15 -0700519 atomic_inc(&m->pg_init_in_progress);
520 activate_or_offline_path(pgpath);
Ming Lei050af082018-01-11 14:01:56 +0800521 return DM_MAPIO_DELAY_REQUEUE;
Bart Van Assche7083abb2017-04-27 10:11:15 -0700522 }
Ming Lei050af082018-01-11 14:01:56 +0800523
524 /*
525 * blk-mq's SCHED_RESTART can cover this requeue, so we
526 * needn't deal with it by DELAY_REQUEUE. More importantly,
527 * we have to return DM_MAPIO_REQUEUE so that blk-mq can
528 * get the queue busy feedback (via BLK_STS_RESOURCE),
529 * otherwise I/O merging can suffer.
530 */
531 if (q->mq_ops)
532 return DM_MAPIO_REQUEUE;
533 else
534 return DM_MAPIO_DELAY_REQUEUE;
Mike Snitzere5863d92014-12-17 21:08:12 -0500535 }
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100536 clone->bio = clone->biotail = NULL;
537 clone->rq_disk = bdev->bd_disk;
538 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
539 *__clone = clone;
Mike Snitzere5863d92014-12-17 21:08:12 -0500540
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100541 if (pgpath->pg->ps.type->start_io)
542 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
543 &pgpath->path,
544 nr_bytes);
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600545 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Mike Snitzere5863d92014-12-17 21:08:12 -0500548static void multipath_release_clone(struct request *clone)
549{
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100550 blk_put_request(clone);
Mike Snitzere5863d92014-12-17 21:08:12 -0500551}
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400554 * Map cloned bios (bio-based multipath)
555 */
Mike Snitzer0001ec52017-12-11 11:02:29 -0500556
557static struct pgpath *__map_bio(struct multipath *m, struct bio *bio)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400558{
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400559 struct pgpath *pgpath;
560 unsigned long flags;
561 bool queue_io;
562
563 /* Do we need to select a new pgpath? */
Will Deacon506458e2017-10-24 11:22:48 +0100564 pgpath = READ_ONCE(m->current_pgpath);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400565 queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
566 if (!pgpath || !queue_io)
Mike Snitzer0001ec52017-12-11 11:02:29 -0500567 pgpath = choose_pgpath(m, bio->bi_iter.bi_size);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400568
569 if ((pgpath && queue_io) ||
570 (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
571 /* Queue for the daemon to resubmit */
572 spin_lock_irqsave(&m->lock, flags);
573 bio_list_add(&m->queued_bios, bio);
574 spin_unlock_irqrestore(&m->lock, flags);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500575
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400576 /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
577 if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
578 pg_init_all_paths(m);
579 else if (!queue_io)
580 queue_work(kmultipathd, &m->process_queued_bios);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500581
Mike Snitzer0001ec52017-12-11 11:02:29 -0500582 return ERR_PTR(-EAGAIN);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400583 }
584
Mike Snitzer0001ec52017-12-11 11:02:29 -0500585 return pgpath;
586}
587
Mike Snitzer8d47e652018-03-05 14:10:11 -0500588static struct pgpath *__map_bio_fast(struct multipath *m, struct bio *bio)
Mike Snitzer0001ec52017-12-11 11:02:29 -0500589{
590 struct pgpath *pgpath;
591 unsigned long flags;
592
593 /* Do we need to select a new pgpath? */
594 /*
595 * FIXME: currently only switching path if no path (due to failure, etc)
596 * - which negates the point of using a path selector
597 */
598 pgpath = READ_ONCE(m->current_pgpath);
599 if (!pgpath)
600 pgpath = choose_pgpath(m, bio->bi_iter.bi_size);
601
602 if (!pgpath) {
603 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
604 /* Queue for the daemon to resubmit */
605 spin_lock_irqsave(&m->lock, flags);
606 bio_list_add(&m->queued_bios, bio);
607 spin_unlock_irqrestore(&m->lock, flags);
608 queue_work(kmultipathd, &m->process_queued_bios);
609
610 return ERR_PTR(-EAGAIN);
611 }
612 return NULL;
613 }
614
615 return pgpath;
616}
617
618static int __multipath_map_bio(struct multipath *m, struct bio *bio,
619 struct dm_mpath_io *mpio)
620{
621 struct pgpath *pgpath;
622
Mike Snitzer8d47e652018-03-05 14:10:11 -0500623 if (!m->hw_handler_name)
624 pgpath = __map_bio_fast(m, bio);
Mike Snitzer0001ec52017-12-11 11:02:29 -0500625 else
626 pgpath = __map_bio(m, bio);
627
628 if (IS_ERR(pgpath))
629 return DM_MAPIO_SUBMITTED;
630
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400631 if (!pgpath) {
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500632 if (must_push_back_bio(m))
Bart Van Asscheca5beb72017-04-27 10:11:24 -0700633 return DM_MAPIO_REQUEUE;
Christoph Hellwig18a482f2017-05-15 17:28:37 +0200634 dm_report_EIO(m);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200635 return DM_MAPIO_KILL;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400636 }
637
638 mpio->pgpath = pgpath;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400639
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200640 bio->bi_status = 0;
Christoph Hellwig74d46992017-08-23 19:10:32 +0200641 bio_set_dev(bio, pgpath->path.dev->bdev);
Jens Axboe1eff9d32016-08-05 15:35:16 -0600642 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400643
644 if (pgpath->pg->ps.type->start_io)
645 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
646 &pgpath->path,
Mike Snitzerd0442f82017-12-11 15:58:41 -0500647 mpio->nr_bytes);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400648 return DM_MAPIO_REMAPPED;
649}
650
651static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
652{
653 struct multipath *m = ti->private;
Mike Snitzerbf661be2016-05-24 15:48:08 -0400654 struct dm_mpath_io *mpio = NULL;
655
Mike Snitzer63f6e6f2017-12-05 14:10:33 -0500656 multipath_init_per_bio_data(bio, &mpio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400657 return __multipath_map_bio(m, bio, mpio);
658}
659
Mike Snitzer7e48c762016-09-14 10:47:03 -0400660static void process_queued_io_list(struct multipath *m)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400661{
Mike Snitzer7e48c762016-09-14 10:47:03 -0400662 if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED)
663 dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
Mike Snitzer8d47e652018-03-05 14:10:11 -0500664 else if (m->queue_mode == DM_TYPE_BIO_BASED)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400665 queue_work(kmultipathd, &m->process_queued_bios);
666}
667
668static void process_queued_bios(struct work_struct *work)
669{
670 int r;
671 unsigned long flags;
672 struct bio *bio;
673 struct bio_list bios;
674 struct blk_plug plug;
675 struct multipath *m =
676 container_of(work, struct multipath, process_queued_bios);
677
678 bio_list_init(&bios);
679
680 spin_lock_irqsave(&m->lock, flags);
681
682 if (bio_list_empty(&m->queued_bios)) {
683 spin_unlock_irqrestore(&m->lock, flags);
684 return;
685 }
686
687 bio_list_merge(&bios, &m->queued_bios);
688 bio_list_init(&m->queued_bios);
689
690 spin_unlock_irqrestore(&m->lock, flags);
691
692 blk_start_plug(&plug);
693 while ((bio = bio_list_pop(&bios))) {
Mike Snitzer1836df02017-12-06 16:08:14 -0500694 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
695 dm_bio_restore(get_bio_details_from_mpio(mpio), bio);
696 r = __multipath_map_bio(m, bio, mpio);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200697 switch (r) {
698 case DM_MAPIO_KILL:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200699 bio->bi_status = BLK_STS_IOERR;
700 bio_endio(bio);
Dan Carpenter047385b2017-06-14 08:22:55 -0600701 break;
Christoph Hellwig846785e2017-06-03 09:38:02 +0200702 case DM_MAPIO_REQUEUE:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200703 bio->bi_status = BLK_STS_DM_REQUEUE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400704 bio_endio(bio);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200705 break;
706 case DM_MAPIO_REMAPPED:
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400707 generic_make_request(bio);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200708 break;
Bart Van Assche9157c8d2017-08-09 11:32:14 -0700709 case 0:
710 break;
711 default:
712 WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r);
Christoph Hellwig846785e2017-06-03 09:38:02 +0200713 }
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400714 }
715 blk_finish_plug(&plug);
716}
717
718/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 * If we run out of usable paths, should we queue I/O or error it?
720 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -0500721static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
722 bool save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
724 unsigned long flags;
725
726 spin_lock_irqsave(&m->lock, flags);
Lukas Wunner5307e2a2017-10-12 12:40:10 +0200727 assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags,
728 (save_old_value && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) ||
729 (!save_old_value && queue_if_no_path));
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -0500730 assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 spin_unlock_irqrestore(&m->lock, flags);
732
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400733 if (!queue_if_no_path) {
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200734 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -0400735 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400736 }
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return 0;
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741/*
742 * An event is triggered whenever a path is taken out of use.
743 * Includes path failure and PG bypass.
744 */
David Howellsc4028952006-11-22 14:57:56 +0000745static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
David Howellsc4028952006-11-22 14:57:56 +0000747 struct multipath *m =
748 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 dm_table_event(m->ti->table);
751}
752
753/*-----------------------------------------------------------------
754 * Constructor/argument parsing:
755 * <#multipath feature args> [<arg>]*
756 * <#hw_handler args> [hw_handler [<arg>]*]
757 * <#priority groups>
758 * <initial priority group>
759 * [<selector> <#selector args> [<arg>]*
760 * <#paths> <#per-path selector args>
761 * [<path> [<arg>]* ]+ ]+
762 *---------------------------------------------------------------*/
Mike Snitzer498f0102011-08-02 12:32:04 +0100763static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 struct dm_target *ti)
765{
766 int r;
767 struct path_selector_type *pst;
768 unsigned ps_argc;
769
Eric Biggers5916a222017-06-22 11:32:45 -0700770 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700771 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 };
773
Mike Snitzer498f0102011-08-02 12:32:04 +0100774 pst = dm_get_path_selector(dm_shift_arg(as));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700776 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return -EINVAL;
778 }
779
Mike Snitzer498f0102011-08-02 12:32:04 +0100780 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100781 if (r) {
782 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 r = pst->create(&pg->ps, ps_argc, as->argv);
787 if (r) {
788 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700789 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return r;
791 }
792
793 pg->ps.type = pst;
Mike Snitzer498f0102011-08-02 12:32:04 +0100794 dm_consume_args(as, ps_argc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 return 0;
797}
798
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500799static int setup_scsi_dh(struct block_device *bdev, struct multipath *m, char **error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500801 struct request_queue *q = bdev_get_queue(bdev);
Mike Snitzera58a9352012-07-27 15:08:04 +0100802 const char *attached_handler_name;
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500803 int r;
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100804
Mike Snitzer518257b2016-03-17 16:32:10 -0400805 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200806retain:
Mike Snitzera58a9352012-07-27 15:08:04 +0100807 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
808 if (attached_handler_name) {
809 /*
tang.junhui54cd6402016-11-24 15:11:48 +0800810 * Clear any hw_handler_params associated with a
811 * handler that isn't already attached.
812 */
813 if (m->hw_handler_name && strcmp(attached_handler_name, m->hw_handler_name)) {
814 kfree(m->hw_handler_params);
815 m->hw_handler_params = NULL;
816 }
817
818 /*
Mike Snitzera58a9352012-07-27 15:08:04 +0100819 * Reset hw_handler_name to match the attached handler
Mike Snitzera58a9352012-07-27 15:08:04 +0100820 *
821 * NB. This modifies the table line to show the actual
822 * handler instead of the original table passed in.
823 */
824 kfree(m->hw_handler_name);
825 m->hw_handler_name = attached_handler_name;
Mike Snitzer8d47e652018-03-05 14:10:11 -0500826
827 /*
828 * Init fields that are only used when a scsi_dh is attached
829 */
830 if (!test_and_set_bit(MPATHF_QUEUE_IO, &m->flags)) {
831 atomic_set(&m->pg_init_in_progress, 0);
832 atomic_set(&m->pg_init_count, 0);
833 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
834 init_waitqueue_head(&m->pg_init_wait);
835 }
Mike Snitzera58a9352012-07-27 15:08:04 +0100836 }
837 }
838
839 if (m->hw_handler_name) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100840 r = scsi_dh_attach(q, m->hw_handler_name);
841 if (r == -EBUSY) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200842 char b[BDEVNAME_SIZE];
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100843
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200844 printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500845 bdevname(bdev, b));
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200846 goto retain;
847 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700848 if (r < 0) {
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500849 *error = "error attaching hardware handler";
850 return r;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700851 }
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700852
853 if (m->hw_handler_params) {
854 r = scsi_dh_set_params(q, m->hw_handler_params);
855 if (r < 0) {
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500856 *error = "unable to set hardware handler parameters";
857 return r;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700858 }
859 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700860 }
861
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500862 return 0;
863}
864
865static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
866 struct dm_target *ti)
867{
868 int r;
869 struct pgpath *p;
870 struct multipath *m = ti->private;
Mike Snitzer8d47e652018-03-05 14:10:11 -0500871 struct scsi_device *sdev;
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500872
873 /* we need at least a path arg */
874 if (as->argc < 1) {
875 ti->error = "no device given";
876 return ERR_PTR(-EINVAL);
877 }
878
879 p = alloc_pgpath();
880 if (!p)
881 return ERR_PTR(-ENOMEM);
882
883 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
884 &p->path.dev);
885 if (r) {
886 ti->error = "error getting device";
887 goto bad;
888 }
889
Mike Snitzer8d47e652018-03-05 14:10:11 -0500890 sdev = scsi_device_from_queue(bdev_get_queue(p->path.dev->bdev));
891 if (sdev) {
892 put_device(&sdev->sdev_gendev);
Mike Snitzer848b8ae2017-12-10 15:37:21 -0500893 INIT_DELAYED_WORK(&p->activate_path, activate_path_work);
894 r = setup_scsi_dh(p->path.dev->bdev, m, &ti->error);
895 if (r) {
896 dm_put_device(ti, p->path.dev);
897 goto bad;
898 }
899 }
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
902 if (r) {
903 dm_put_device(ti, p->path.dev);
904 goto bad;
905 }
906
907 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 bad:
909 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100910 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Mike Snitzer498f0102011-08-02 12:32:04 +0100913static struct priority_group *parse_priority_group(struct dm_arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700914 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Eric Biggers5916a222017-06-22 11:32:45 -0700916 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700917 {1, 1024, "invalid number of paths"},
918 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 };
920
921 int r;
Mike Snitzer498f0102011-08-02 12:32:04 +0100922 unsigned i, nr_selector_args, nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700924 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 if (as->argc < 2) {
927 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100928 ti->error = "not enough priority group arguments";
929 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931
932 pg = alloc_priority_group();
933 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700934 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100935 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937 pg->m = m;
938
939 r = parse_path_selector(as, pg, ti);
940 if (r)
941 goto bad;
942
943 /*
944 * read the paths
945 */
Mike Snitzer498f0102011-08-02 12:32:04 +0100946 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (r)
948 goto bad;
949
Mike Snitzer498f0102011-08-02 12:32:04 +0100950 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (r)
952 goto bad;
953
Mike Snitzer498f0102011-08-02 12:32:04 +0100954 nr_args = 1 + nr_selector_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 for (i = 0; i < pg->nr_pgpaths; i++) {
956 struct pgpath *pgpath;
Mike Snitzer498f0102011-08-02 12:32:04 +0100957 struct dm_arg_set path_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Mike Snitzer498f0102011-08-02 12:32:04 +0100959 if (as->argc < nr_args) {
Mikulas Patocka148acff2008-07-21 12:00:30 +0100960 ti->error = "not enough path parameters";
Alasdair G Kergon6bbf79a2010-08-12 04:13:49 +0100961 r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Mike Snitzer498f0102011-08-02 12:32:04 +0100965 path_args.argc = nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 path_args.argv = as->argv;
967
968 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100969 if (IS_ERR(pgpath)) {
970 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 pgpath->pg = pg;
975 list_add_tail(&pgpath->list, &pg->pgpaths);
Mike Snitzer498f0102011-08-02 12:32:04 +0100976 dm_consume_args(as, nr_args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978
979 return pg;
980
981 bad:
982 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100983 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Mike Snitzer498f0102011-08-02 12:32:04 +0100986static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 unsigned hw_argc;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700989 int ret;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700990 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Eric Biggers5916a222017-06-22 11:32:45 -0700992 static const struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700993 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 };
995
Mike Snitzer498f0102011-08-02 12:32:04 +0100996 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return -EINVAL;
998
999 if (!hw_argc)
1000 return 0;
1001
Mike Snitzer8d47e652018-03-05 14:10:11 -05001002 if (m->queue_mode == DM_TYPE_BIO_BASED) {
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001003 dm_consume_args(as, hw_argc);
1004 DMERR("bio-based multipath doesn't allow hardware handler args");
1005 return 0;
1006 }
1007
Mike Snitzer498f0102011-08-02 12:32:04 +01001008 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
tang.junhuif97dc422016-10-28 17:04:46 +08001009 if (!m->hw_handler_name)
1010 return -EINVAL;
Chandra Seetharaman14e98c52008-11-13 23:39:06 +00001011
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -07001012 if (hw_argc > 1) {
1013 char *p;
1014 int i, j, len = 4;
1015
1016 for (i = 0; i <= hw_argc - 2; i++)
1017 len += strlen(as->argv[i]) + 1;
1018 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
1019 if (!p) {
1020 ti->error = "memory allocation failed";
1021 ret = -ENOMEM;
1022 goto fail;
1023 }
1024 j = sprintf(p, "%d", hw_argc - 1);
1025 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
1026 j = sprintf(p, "%s", as->argv[i]);
1027 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001028 dm_consume_args(as, hw_argc - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 return 0;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -07001031fail:
1032 kfree(m->hw_handler_name);
1033 m->hw_handler_name = NULL;
1034 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
Mike Snitzer498f0102011-08-02 12:32:04 +01001037static int parse_features(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 int r;
1040 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001041 struct dm_target *ti = m->ti;
Mike Snitzer498f0102011-08-02 12:32:04 +01001042 const char *arg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Eric Biggers5916a222017-06-22 11:32:45 -07001044 static const struct dm_arg _args[] = {
Mike Snitzere83068a2016-05-24 21:16:51 -04001045 {0, 8, "invalid number of feature args"},
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001046 {1, 50, "pg_init_retries must be between 1 and 50"},
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001047 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 };
1049
Mike Snitzer498f0102011-08-02 12:32:04 +01001050 r = dm_read_arg_group(_args, as, &argc, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (r)
1052 return -EINVAL;
1053
1054 if (!argc)
1055 return 0;
1056
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001057 do {
Mike Snitzer498f0102011-08-02 12:32:04 +01001058 arg_name = dm_shift_arg(as);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001059 argc--;
1060
Mike Snitzer498f0102011-08-02 12:32:04 +01001061 if (!strcasecmp(arg_name, "queue_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001062 r = queue_if_no_path(m, true, false);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001063 continue;
1064 }
1065
Mike Snitzera58a9352012-07-27 15:08:04 +01001066 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
Mike Snitzer518257b2016-03-17 16:32:10 -04001067 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
Mike Snitzera58a9352012-07-27 15:08:04 +01001068 continue;
1069 }
1070
Mike Snitzer498f0102011-08-02 12:32:04 +01001071 if (!strcasecmp(arg_name, "pg_init_retries") &&
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001072 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001073 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001074 argc--;
1075 continue;
1076 }
1077
Mike Snitzer498f0102011-08-02 12:32:04 +01001078 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001079 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001080 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001081 argc--;
1082 continue;
1083 }
1084
Mike Snitzere83068a2016-05-24 21:16:51 -04001085 if (!strcasecmp(arg_name, "queue_mode") &&
1086 (argc >= 1)) {
1087 const char *queue_mode_name = dm_shift_arg(as);
1088
1089 if (!strcasecmp(queue_mode_name, "bio"))
1090 m->queue_mode = DM_TYPE_BIO_BASED;
1091 else if (!strcasecmp(queue_mode_name, "rq"))
1092 m->queue_mode = DM_TYPE_REQUEST_BASED;
1093 else if (!strcasecmp(queue_mode_name, "mq"))
1094 m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
1095 else {
1096 ti->error = "Unknown 'queue_mode' requested";
1097 r = -EINVAL;
1098 }
1099 argc--;
1100 continue;
1101 }
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001104 r = -EINVAL;
1105 } while (argc && !r);
1106
1107 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Mike Snitzere83068a2016-05-24 21:16:51 -04001110static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Mike Snitzer498f0102011-08-02 12:32:04 +01001112 /* target arguments */
Eric Biggers5916a222017-06-22 11:32:45 -07001113 static const struct dm_arg _args[] = {
Mike Snitzera490a072011-03-24 13:54:33 +00001114 {0, 1024, "invalid number of priority groups"},
1115 {0, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 };
1117
1118 int r;
1119 struct multipath *m;
Mike Snitzer498f0102011-08-02 12:32:04 +01001120 struct dm_arg_set as;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 unsigned pg_count = 0;
1122 unsigned next_pg_num;
1123
1124 as.argc = argc;
1125 as.argv = argv;
1126
Mike Snitzere83068a2016-05-24 21:16:51 -04001127 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001129 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return -EINVAL;
1131 }
1132
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001133 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (r)
1135 goto bad;
1136
Mike Snitzere83068a2016-05-24 21:16:51 -04001137 r = alloc_multipath_stage2(ti, m);
1138 if (r)
1139 goto bad;
1140
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001141 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (r)
1143 goto bad;
1144
Mike Snitzer498f0102011-08-02 12:32:04 +01001145 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (r)
1147 goto bad;
1148
Mike Snitzer498f0102011-08-02 12:32:04 +01001149 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (r)
1151 goto bad;
1152
Mike Snitzera490a072011-03-24 13:54:33 +00001153 if ((!m->nr_priority_groups && next_pg_num) ||
1154 (m->nr_priority_groups && !next_pg_num)) {
1155 ti->error = "invalid initial priority group";
1156 r = -EINVAL;
1157 goto bad;
1158 }
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 /* parse the priority groups */
1161 while (as.argc) {
1162 struct priority_group *pg;
Mike Snitzer91e968a2016-03-17 17:10:15 -04001163 unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001165 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +01001166 if (IS_ERR(pg)) {
1167 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 goto bad;
1169 }
1170
Mike Snitzer91e968a2016-03-17 17:10:15 -04001171 nr_valid_paths += pg->nr_pgpaths;
1172 atomic_set(&m->nr_valid_paths, nr_valid_paths);
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 list_add_tail(&pg->list, &m->priority_groups);
1175 pg_count++;
1176 pg->pg_num = pg_count;
1177 if (!--next_pg_num)
1178 m->next_pg = pg;
1179 }
1180
1181 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001182 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 r = -EINVAL;
1184 goto bad;
1185 }
1186
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001187 ti->num_flush_bios = 1;
1188 ti->num_discard_bios = 1;
Mike Snitzer042bcef2013-05-10 14:37:16 +01001189 ti->num_write_same_bios = 1;
Christoph Hellwigac62d622017-04-05 19:21:05 +02001190 ti->num_write_zeroes_bios = 1;
Mike Snitzer8d47e652018-03-05 14:10:11 -05001191 if (m->queue_mode == DM_TYPE_BIO_BASED)
Mike Snitzerbf661be2016-05-24 15:48:08 -04001192 ti->per_io_data_size = multipath_per_bio_data_size();
Christoph Hellwigeb8db832017-01-22 18:32:46 +01001193 else
Mike Snitzer8637a6b2016-01-31 12:08:36 -05001194 ti->per_io_data_size = sizeof(struct dm_mpath_io);
Mikulas Patocka86279212009-06-22 10:12:24 +01001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return 0;
1197
1198 bad:
1199 free_multipath(m);
1200 return r;
1201}
1202
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001203static void multipath_wait_for_pg_init_completion(struct multipath *m)
1204{
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001205 DEFINE_WAIT(wait);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001206
1207 while (1) {
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001208 prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001209
Mike Snitzer91e968a2016-03-17 17:10:15 -04001210 if (!atomic_read(&m->pg_init_in_progress))
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001211 break;
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001212
1213 io_schedule();
1214 }
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001215 finish_wait(&m->pg_init_wait, &wait);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001216}
1217
1218static void flush_multipath_work(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Mike Snitzer848b8ae2017-12-10 15:37:21 -05001220 if (m->hw_handler_name) {
1221 set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1222 smp_mb__after_atomic();
Shiva Krishna Merla954a73d2013-10-30 03:26:38 +00001223
Mike Snitzer848b8ae2017-12-10 15:37:21 -05001224 flush_workqueue(kmpath_handlerd);
1225 multipath_wait_for_pg_init_completion(m);
1226
1227 clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1228 smp_mb__after_atomic();
1229 }
1230
Alasdair G Kergona044d012005-07-12 15:53:02 -07001231 flush_workqueue(kmultipathd);
Tejun Heo43829732012-08-20 14:51:24 -07001232 flush_work(&m->trigger_event);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001233}
1234
1235static void multipath_dtr(struct dm_target *ti)
1236{
1237 struct multipath *m = ti->private;
1238
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001239 flush_multipath_work(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 free_multipath(m);
1241}
1242
1243/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 * Take a path out of use.
1245 */
1246static int fail_path(struct pgpath *pgpath)
1247{
1248 unsigned long flags;
1249 struct multipath *m = pgpath->pg->m;
1250
1251 spin_lock_irqsave(&m->lock, flags);
1252
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001253 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 goto out;
1255
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001256 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001259 pgpath->is_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 pgpath->fail_count++;
1261
Mike Snitzer91e968a2016-03-17 17:10:15 -04001262 atomic_dec(&m->nr_valid_paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 if (pgpath == m->current_pgpath)
1265 m->current_pgpath = NULL;
1266
Mike Andersonb15546f2007-10-19 22:48:02 +01001267 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
Mike Snitzer91e968a2016-03-17 17:10:15 -04001268 pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
Mike Andersonb15546f2007-10-19 22:48:02 +01001269
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001270 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272out:
1273 spin_unlock_irqrestore(&m->lock, flags);
1274
1275 return 0;
1276}
1277
1278/*
1279 * Reinstate a previously-failed path
1280 */
1281static int reinstate_path(struct pgpath *pgpath)
1282{
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001283 int r = 0, run_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 unsigned long flags;
1285 struct multipath *m = pgpath->pg->m;
Mike Snitzer91e968a2016-03-17 17:10:15 -04001286 unsigned nr_valid_paths;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 spin_lock_irqsave(&m->lock, flags);
1289
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001290 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 goto out;
1292
Mike Snitzerec31f3f2016-02-20 12:49:43 -05001293 DMWARN("Reinstating path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1296 if (r)
1297 goto out;
1298
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001299 pgpath->is_active = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Mike Snitzer91e968a2016-03-17 17:10:15 -04001301 nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
1302 if (nr_valid_paths == 1) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001303 m->current_pgpath = NULL;
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001304 run_queue = 1;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001305 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001306 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
Mike Snitzer91e968a2016-03-17 17:10:15 -04001307 atomic_inc(&m->pg_init_in_progress);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Mike Andersonb15546f2007-10-19 22:48:02 +01001310 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
Mike Snitzer91e968a2016-03-17 17:10:15 -04001311 pgpath->path.dev->name, nr_valid_paths);
Mike Andersonb15546f2007-10-19 22:48:02 +01001312
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001313 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315out:
1316 spin_unlock_irqrestore(&m->lock, flags);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001317 if (run_queue) {
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001318 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -04001319 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 return r;
1323}
1324
1325/*
1326 * Fail or reinstate all paths that match the provided struct dm_dev.
1327 */
1328static int action_dev(struct multipath *m, struct dm_dev *dev,
1329 action_fn action)
1330{
Mike Snitzer19040c02011-03-24 13:54:31 +00001331 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 struct pgpath *pgpath;
1333 struct priority_group *pg;
1334
1335 list_for_each_entry(pg, &m->priority_groups, list) {
1336 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1337 if (pgpath->path.dev == dev)
1338 r = action(pgpath);
1339 }
1340 }
1341
1342 return r;
1343}
1344
1345/*
1346 * Temporarily try to avoid having to use the specified PG
1347 */
1348static void bypass_pg(struct multipath *m, struct priority_group *pg,
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001349 bool bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
1351 unsigned long flags;
1352
1353 spin_lock_irqsave(&m->lock, flags);
1354
1355 pg->bypassed = bypassed;
1356 m->current_pgpath = NULL;
1357 m->current_pg = NULL;
1358
1359 spin_unlock_irqrestore(&m->lock, flags);
1360
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001361 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
1364/*
1365 * Switch to using the specified PG from the next I/O that gets mapped
1366 */
1367static int switch_pg_num(struct multipath *m, const char *pgstr)
1368{
1369 struct priority_group *pg;
1370 unsigned pgnum;
1371 unsigned long flags;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001372 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001374 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
tang.junhuicc5bd922016-11-04 12:37:09 +08001375 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 DMWARN("invalid PG number supplied to switch_pg_num");
1377 return -EINVAL;
1378 }
1379
1380 spin_lock_irqsave(&m->lock, flags);
1381 list_for_each_entry(pg, &m->priority_groups, list) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001382 pg->bypassed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (--pgnum)
1384 continue;
1385
1386 m->current_pgpath = NULL;
1387 m->current_pg = NULL;
1388 m->next_pg = pg;
1389 }
1390 spin_unlock_irqrestore(&m->lock, flags);
1391
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001392 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return 0;
1394}
1395
1396/*
1397 * Set/clear bypassed status of a PG.
1398 * PGs are numbered upwards from 1 in the order they were declared.
1399 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001400static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
1402 struct priority_group *pg;
1403 unsigned pgnum;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001404 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001406 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
tang.junhuicc5bd922016-11-04 12:37:09 +08001407 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 DMWARN("invalid PG number supplied to bypass_pg");
1409 return -EINVAL;
1410 }
1411
1412 list_for_each_entry(pg, &m->priority_groups, list) {
1413 if (!--pgnum)
1414 break;
1415 }
1416
1417 bypass_pg(m, pg, bypassed);
1418 return 0;
1419}
1420
1421/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001422 * Should we retry pg_init immediately?
1423 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001424static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001425{
1426 unsigned long flags;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001427 bool limit_reached = false;
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001428
1429 spin_lock_irqsave(&m->lock, flags);
1430
Mike Snitzer91e968a2016-03-17 17:10:15 -04001431 if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
1432 !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
Mike Snitzer518257b2016-03-17 16:32:10 -04001433 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001434 else
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001435 limit_reached = true;
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001436
1437 spin_unlock_irqrestore(&m->lock, flags);
1438
1439 return limit_reached;
1440}
1441
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001442static void pg_init_done(void *data, int errors)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001443{
Moger, Babu83c0d5d2010-03-06 02:29:45 +00001444 struct pgpath *pgpath = data;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001445 struct priority_group *pg = pgpath->pg;
1446 struct multipath *m = pg->m;
1447 unsigned long flags;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001448 bool delay_retry = false;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001449
1450 /* device or driver problems */
1451 switch (errors) {
1452 case SCSI_DH_OK:
1453 break;
1454 case SCSI_DH_NOSYS:
1455 if (!m->hw_handler_name) {
1456 errors = 0;
1457 break;
1458 }
Moger, Babuf7b934c2010-03-06 02:29:49 +00001459 DMERR("Could not failover the device: Handler scsi_dh_%s "
1460 "Error %d.", m->hw_handler_name, errors);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001461 /*
1462 * Fail path for now, so we do not ping pong
1463 */
1464 fail_path(pgpath);
1465 break;
1466 case SCSI_DH_DEV_TEMP_BUSY:
1467 /*
1468 * Probably doing something like FW upgrade on the
1469 * controller so try the other pg.
1470 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001471 bypass_pg(m, pg, true);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001472 break;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001473 case SCSI_DH_RETRY:
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001474 /* Wait before retrying. */
1475 delay_retry = 1;
Bart Van Assche7b06e092017-08-09 11:32:13 -07001476 /* fall through */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001477 case SCSI_DH_IMM_RETRY:
1478 case SCSI_DH_RES_TEMP_UNAVAIL:
1479 if (pg_init_limit_reached(m, pgpath))
1480 fail_path(pgpath);
1481 errors = 0;
1482 break;
Mike Snitzerec31f3f2016-02-20 12:49:43 -05001483 case SCSI_DH_DEV_OFFLINED:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001484 default:
1485 /*
1486 * We probably do not want to fail the path for a device
1487 * error, but this is what the old dm did. In future
1488 * patches we can do more advanced handling.
1489 */
1490 fail_path(pgpath);
1491 }
1492
1493 spin_lock_irqsave(&m->lock, flags);
1494 if (errors) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001495 if (pgpath == m->current_pgpath) {
1496 DMERR("Could not failover device. Error %d.", errors);
1497 m->current_pgpath = NULL;
1498 m->current_pg = NULL;
1499 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001500 } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001501 pg->bypassed = false;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001502
Mike Snitzer91e968a2016-03-17 17:10:15 -04001503 if (atomic_dec_return(&m->pg_init_in_progress) > 0)
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001504 /* Activations of other paths are still on going */
1505 goto out;
1506
Mike Snitzer518257b2016-03-17 16:32:10 -04001507 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1508 if (delay_retry)
1509 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1510 else
1511 clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1512
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001513 if (__pg_init_all_paths(m))
1514 goto out;
1515 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001516 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001517
Mike Snitzer7e48c762016-09-14 10:47:03 -04001518 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001519
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001520 /*
1521 * Wake up any thread waiting to suspend.
1522 */
1523 wake_up(&m->pg_init_wait);
1524
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001525out:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001526 spin_unlock_irqrestore(&m->lock, flags);
1527}
1528
Bart Van Assche89bfce72017-04-27 10:11:14 -07001529static void activate_or_offline_path(struct pgpath *pgpath)
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001530{
Mike Snitzerf10e06b2016-09-01 12:06:37 -04001531 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001532
Mike Snitzerf10e06b2016-09-01 12:06:37 -04001533 if (pgpath->is_active && !blk_queue_dying(q))
1534 scsi_dh_activate(q, pg_init_done, pgpath);
Hannes Reinecke3a017502014-02-28 15:33:49 +01001535 else
1536 pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001537}
1538
Bart Van Assche89bfce72017-04-27 10:11:14 -07001539static void activate_path_work(struct work_struct *work)
1540{
1541 struct pgpath *pgpath =
1542 container_of(work, struct pgpath, activate_path.work);
1543
1544 activate_or_offline_path(pgpath);
1545}
1546
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001547static int multipath_end_io(struct dm_target *ti, struct request *clone,
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001548 blk_status_t error, union map_info *map_context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001550 struct dm_mpath_io *mpio = get_mpio(map_context);
1551 struct pgpath *pgpath = mpio->pgpath;
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001552 int r = DM_ENDIO_DONE;
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001553
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001554 /*
1555 * We don't queue any clone request inside the multipath target
1556 * during end I/O handling, since those clone requests don't have
1557 * bio clones. If we queue them inside the multipath target,
1558 * we need to make bio clones, that requires memory allocation.
Mike Snitzer4cc96132016-05-12 16:28:10 -04001559 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001560 * don't have bio clones.)
1561 * Instead of queueing the clone request here, we queue the original
1562 * request into dm core, which will remake a clone request and
1563 * clone bios for it and resubmit it later.
1564 */
Keith Buscha1275672018-01-09 12:04:18 -07001565 if (error && blk_path_error(error)) {
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001566 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Mike Snitzerac514ff2018-01-12 19:53:40 -05001568 if (error == BLK_STS_RESOURCE)
1569 r = DM_ENDIO_DELAY_REQUEUE;
1570 else
1571 r = DM_ENDIO_REQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001573 if (pgpath)
1574 fail_path(pgpath);
Mike Snitzer959eb4e2010-08-12 04:14:32 +01001575
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001576 if (atomic_read(&m->nr_valid_paths) == 0 &&
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -05001577 !must_push_back_rq(m)) {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001578 if (error == BLK_STS_IOERR)
Christoph Hellwig18a482f2017-05-15 17:28:37 +02001579 dm_report_EIO(m);
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001580 /* complete with the original error */
1581 r = DM_ENDIO_DONE;
1582 }
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (pgpath) {
Christoph Hellwigb79f10e2017-04-26 09:40:36 +02001586 struct path_selector *ps = &pgpath->pg->ps;
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (ps->type->end_io)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001589 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Christoph Hellwig7ed85782017-04-26 09:40:37 +02001592 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001595static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone,
Mike Snitzer848b8ae2017-12-10 15:37:21 -05001596 blk_status_t *error)
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001597{
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001598 struct multipath *m = ti->private;
1599 struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
1600 struct pgpath *pgpath = mpio->pgpath;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001601 unsigned long flags;
Christoph Hellwig1be56902017-06-03 09:38:03 +02001602 int r = DM_ENDIO_DONE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001603
Keith Buscha1275672018-01-09 12:04:18 -07001604 if (!*error || !blk_path_error(*error))
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001605 goto done;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001606
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001607 if (pgpath)
1608 fail_path(pgpath);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001609
Bart Van Asscheca5beb72017-04-27 10:11:24 -07001610 if (atomic_read(&m->nr_valid_paths) == 0 &&
Christoph Hellwig18a482f2017-05-15 17:28:37 +02001611 !test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
Mike Snitzerc1fd0ab2017-12-07 22:42:27 -05001612 if (must_push_back_bio(m)) {
1613 r = DM_ENDIO_REQUEUE;
1614 } else {
1615 dm_report_EIO(m);
1616 *error = BLK_STS_IOERR;
1617 }
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001618 goto done;
Christoph Hellwig18a482f2017-05-15 17:28:37 +02001619 }
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001620
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001621 spin_lock_irqsave(&m->lock, flags);
1622 bio_list_add(&m->queued_bios, clone);
1623 spin_unlock_irqrestore(&m->lock, flags);
1624 if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
1625 queue_work(kmultipathd, &m->process_queued_bios);
1626
Christoph Hellwig1be56902017-06-03 09:38:03 +02001627 r = DM_ENDIO_INCOMPLETE;
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001628done:
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001629 if (pgpath) {
Christoph Hellwig14ef1e42017-06-03 09:38:01 +02001630 struct path_selector *ps = &pgpath->pg->ps;
1631
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001632 if (ps->type->end_io)
1633 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1634 }
1635
Christoph Hellwig1be56902017-06-03 09:38:03 +02001636 return r;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001637}
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639/*
1640 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001641 * the last path fails we must error any remaining I/O.
1642 * Note that if the freeze_bdev fails while suspending, the
1643 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 */
1645static void multipath_presuspend(struct dm_target *ti)
1646{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001647 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001649 queue_if_no_path(m, false, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001652static void multipath_postsuspend(struct dm_target *ti)
1653{
Mike Anderson6380f262009-12-10 23:52:21 +00001654 struct multipath *m = ti->private;
1655
1656 mutex_lock(&m->work_mutex);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001657 flush_multipath_work(m);
Mike Anderson6380f262009-12-10 23:52:21 +00001658 mutex_unlock(&m->work_mutex);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001659}
1660
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001661/*
1662 * Restore the queue_if_no_path setting.
1663 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664static void multipath_resume(struct dm_target *ti)
1665{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001666 struct multipath *m = ti->private;
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001667 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001669 spin_lock_irqsave(&m->lock, flags);
Lukas Wunner5307e2a2017-10-12 12:40:10 +02001670 assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags,
1671 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags));
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001672 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
1674
1675/*
1676 * Info output has the following format:
1677 * num_multipath_feature_args [multipath_feature_args]*
1678 * num_handler_status_args [handler_status_args]*
1679 * num_groups init_group_number
1680 * [A|D|E num_ps_status_args [ps_status_args]*
1681 * num_paths num_selector_args
1682 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1683 *
1684 * Table output has the following format (identical to the constructor string):
1685 * num_feature_args [features_args]*
1686 * num_handler_args hw_handler [hw_handler_args]*
1687 * num_groups init_group_number
1688 * [priority selector-name num_ps_args [ps_args]*
1689 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1690 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001691static void multipath_status(struct dm_target *ti, status_type_t type,
1692 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
1694 int sz = 0;
1695 unsigned long flags;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001696 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 struct priority_group *pg;
1698 struct pgpath *p;
1699 unsigned pg_num;
1700 char state;
1701
1702 spin_lock_irqsave(&m->lock, flags);
1703
1704 /* Features */
1705 if (type == STATUSTYPE_INFO)
Mike Snitzer91e968a2016-03-17 17:10:15 -04001706 DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
1707 atomic_read(&m->pg_init_count));
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001708 else {
Mike Snitzer518257b2016-03-17 16:32:10 -04001709 DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001710 (m->pg_init_retries > 0) * 2 +
Mike Snitzera58a9352012-07-27 15:08:04 +01001711 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
Mike Snitzere83068a2016-05-24 21:16:51 -04001712 test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
1713 (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
1714
Mike Snitzer518257b2016-03-17 16:32:10 -04001715 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001716 DMEMIT("queue_if_no_path ");
1717 if (m->pg_init_retries)
1718 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001719 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1720 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
Mike Snitzer518257b2016-03-17 16:32:10 -04001721 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
Mike Snitzera58a9352012-07-27 15:08:04 +01001722 DMEMIT("retain_attached_hw_handler ");
Mike Snitzere83068a2016-05-24 21:16:51 -04001723 if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
1724 switch(m->queue_mode) {
1725 case DM_TYPE_BIO_BASED:
1726 DMEMIT("queue_mode bio ");
1727 break;
1728 case DM_TYPE_MQ_REQUEST_BASED:
1729 DMEMIT("queue_mode mq ");
1730 break;
Bart Van Assche7e0d5742017-04-27 10:11:23 -07001731 default:
1732 WARN_ON_ONCE(true);
1733 break;
Mike Snitzere83068a2016-05-24 21:16:51 -04001734 }
1735 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001738 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 DMEMIT("0 ");
1740 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001741 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 DMEMIT("%u ", m->nr_priority_groups);
1744
1745 if (m->next_pg)
1746 pg_num = m->next_pg->pg_num;
1747 else if (m->current_pg)
1748 pg_num = m->current_pg->pg_num;
1749 else
Mike Snitzera490a072011-03-24 13:54:33 +00001750 pg_num = (m->nr_priority_groups ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 DMEMIT("%u ", pg_num);
1753
1754 switch (type) {
1755 case STATUSTYPE_INFO:
1756 list_for_each_entry(pg, &m->priority_groups, list) {
1757 if (pg->bypassed)
1758 state = 'D'; /* Disabled */
1759 else if (pg == m->current_pg)
1760 state = 'A'; /* Currently Active */
1761 else
1762 state = 'E'; /* Enabled */
1763
1764 DMEMIT("%c ", state);
1765
1766 if (pg->ps.type->status)
1767 sz += pg->ps.type->status(&pg->ps, NULL, type,
1768 result + sz,
1769 maxlen - sz);
1770 else
1771 DMEMIT("0 ");
1772
1773 DMEMIT("%u %u ", pg->nr_pgpaths,
1774 pg->ps.type->info_args);
1775
1776 list_for_each_entry(p, &pg->pgpaths, list) {
1777 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001778 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 p->fail_count);
1780 if (pg->ps.type->status)
1781 sz += pg->ps.type->status(&pg->ps,
1782 &p->path, type, result + sz,
1783 maxlen - sz);
1784 }
1785 }
1786 break;
1787
1788 case STATUSTYPE_TABLE:
1789 list_for_each_entry(pg, &m->priority_groups, list) {
1790 DMEMIT("%s ", pg->ps.type->name);
1791
1792 if (pg->ps.type->status)
1793 sz += pg->ps.type->status(&pg->ps, NULL, type,
1794 result + sz,
1795 maxlen - sz);
1796 else
1797 DMEMIT("0 ");
1798
1799 DMEMIT("%u %u ", pg->nr_pgpaths,
1800 pg->ps.type->table_args);
1801
1802 list_for_each_entry(p, &pg->pgpaths, list) {
1803 DMEMIT("%s ", p->path.dev->name);
1804 if (pg->ps.type->status)
1805 sz += pg->ps.type->status(&pg->ps,
1806 &p->path, type, result + sz,
1807 maxlen - sz);
1808 }
1809 }
1810 break;
1811 }
1812
1813 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
1816static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1817{
Mike Anderson6380f262009-12-10 23:52:21 +00001818 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 struct dm_dev *dev;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001820 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 action_fn action;
1822
Mike Anderson6380f262009-12-10 23:52:21 +00001823 mutex_lock(&m->work_mutex);
1824
Kiyoshi Uedac2f3d242009-12-10 23:52:27 +00001825 if (dm_suspended(ti)) {
1826 r = -EBUSY;
1827 goto out;
1828 }
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (argc == 1) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001831 if (!strcasecmp(argv[0], "queue_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001832 r = queue_if_no_path(m, true, false);
Mike Anderson6380f262009-12-10 23:52:21 +00001833 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001834 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001835 r = queue_if_no_path(m, false, false);
Mike Anderson6380f262009-12-10 23:52:21 +00001836 goto out;
1837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839
Mike Anderson6380f262009-12-10 23:52:21 +00001840 if (argc != 2) {
Jose Castilloa356e422014-01-29 17:52:45 +01001841 DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
Mike Anderson6380f262009-12-10 23:52:21 +00001842 goto out;
1843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Mike Snitzer498f0102011-08-02 12:32:04 +01001845 if (!strcasecmp(argv[0], "disable_group")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001846 r = bypass_pg_num(m, argv[1], true);
Mike Anderson6380f262009-12-10 23:52:21 +00001847 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001848 } else if (!strcasecmp(argv[0], "enable_group")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001849 r = bypass_pg_num(m, argv[1], false);
Mike Anderson6380f262009-12-10 23:52:21 +00001850 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001851 } else if (!strcasecmp(argv[0], "switch_group")) {
Mike Anderson6380f262009-12-10 23:52:21 +00001852 r = switch_pg_num(m, argv[1]);
1853 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001854 } else if (!strcasecmp(argv[0], "reinstate_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 action = reinstate_path;
Mike Snitzer498f0102011-08-02 12:32:04 +01001856 else if (!strcasecmp(argv[0], "fail_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 action = fail_path;
Mike Anderson6380f262009-12-10 23:52:21 +00001858 else {
Jose Castilloa356e422014-01-29 17:52:45 +01001859 DMWARN("Unrecognised multipath message received: %s", argv[0]);
Mike Anderson6380f262009-12-10 23:52:21 +00001860 goto out;
1861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00001863 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001865 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 argv[1]);
Mike Anderson6380f262009-12-10 23:52:21 +00001867 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869
1870 r = action_dev(m, dev, action);
1871
1872 dm_put_device(ti, dev);
1873
Mike Anderson6380f262009-12-10 23:52:21 +00001874out:
1875 mutex_unlock(&m->work_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001879static int multipath_prepare_ioctl(struct dm_target *ti,
1880 struct block_device **bdev, fmode_t *mode)
Milan Broz9af4aa32006-10-03 01:15:20 -07001881{
Mikulas Patocka35991652012-06-03 00:29:58 +01001882 struct multipath *m = ti->private;
Mike Snitzer2da16102016-03-17 18:38:17 -04001883 struct pgpath *current_pgpath;
Mikulas Patocka35991652012-06-03 00:29:58 +01001884 int r;
1885
Will Deacon506458e2017-10-24 11:22:48 +01001886 current_pgpath = READ_ONCE(m->current_pgpath);
Mike Snitzer2da16102016-03-17 18:38:17 -04001887 if (!current_pgpath)
1888 current_pgpath = choose_pgpath(m, 0);
Milan Broz9af4aa32006-10-03 01:15:20 -07001889
Mike Snitzer2da16102016-03-17 18:38:17 -04001890 if (current_pgpath) {
Mike Snitzer518257b2016-03-17 16:32:10 -04001891 if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
Mike Snitzer2da16102016-03-17 18:38:17 -04001892 *bdev = current_pgpath->path.dev->bdev;
1893 *mode = current_pgpath->path.dev->mode;
Junichi Nomura43e43c92015-11-17 09:36:56 +00001894 r = 0;
1895 } else {
1896 /* pg_init has not started or completed */
1897 r = -ENOTCONN;
1898 }
1899 } else {
1900 /* No path is available */
Mike Snitzer518257b2016-03-17 16:32:10 -04001901 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Junichi Nomura43e43c92015-11-17 09:36:56 +00001902 r = -ENOTCONN;
1903 else
1904 r = -EIO;
Milan Broze90dae12006-10-03 01:15:22 -07001905 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001906
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +00001907 if (r == -ENOTCONN) {
Will Deacon506458e2017-10-24 11:22:48 +01001908 if (!READ_ONCE(m->current_pg)) {
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001909 /* Path status changed, redo selection */
Mike Snitzer2da16102016-03-17 18:38:17 -04001910 (void) choose_pgpath(m, 0);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001911 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001912 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
Mike Snitzer2da16102016-03-17 18:38:17 -04001913 pg_init_all_paths(m);
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001914 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -04001915 process_queued_io_list(m);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001916 }
Mikulas Patocka35991652012-06-03 00:29:58 +01001917
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001918 /*
1919 * Only pass ioctls through if the device sizes match exactly.
1920 */
1921 if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1922 return 1;
1923 return r;
Milan Broz9af4aa32006-10-03 01:15:20 -07001924}
1925
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001926static int multipath_iterate_devices(struct dm_target *ti,
1927 iterate_devices_callout_fn fn, void *data)
1928{
1929 struct multipath *m = ti->private;
1930 struct priority_group *pg;
1931 struct pgpath *p;
1932 int ret = 0;
1933
1934 list_for_each_entry(pg, &m->priority_groups, list) {
1935 list_for_each_entry(p, &pg->pgpaths, list) {
Mike Snitzer5dea2712009-07-23 20:30:42 +01001936 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001937 if (ret)
1938 goto out;
1939 }
1940 }
1941
1942out:
1943 return ret;
1944}
1945
Mike Snitzer9f54cec2016-02-11 21:42:28 -05001946static int pgpath_busy(struct pgpath *pgpath)
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001947{
1948 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1949
Mike Snitzer52b09912015-02-23 16:36:41 -05001950 return blk_lld_busy(q);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001951}
1952
1953/*
1954 * We return "busy", only when we can map I/Os but underlying devices
1955 * are busy (so even if we map I/Os now, the I/Os will wait on
1956 * the underlying queue).
1957 * In other words, if we want to kill I/Os or queue them inside us
1958 * due to map unavailability, we don't return "busy". Otherwise,
1959 * dm core won't give us the I/Os and we can't do what we want.
1960 */
1961static int multipath_busy(struct dm_target *ti)
1962{
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001963 bool busy = false, has_active = false;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001964 struct multipath *m = ti->private;
Mike Snitzer2da16102016-03-17 18:38:17 -04001965 struct priority_group *pg, *next_pg;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001966 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001967
Mike Snitzerb88efd42016-09-09 19:26:19 -04001968 /* pg_init in progress */
1969 if (atomic_read(&m->pg_init_in_progress))
Mike Snitzer2da16102016-03-17 18:38:17 -04001970 return true;
1971
Mike Snitzerb88efd42016-09-09 19:26:19 -04001972 /* no paths available, for blk-mq: rely on IO mapping to delay requeue */
1973 if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1974 return (m->queue_mode != DM_TYPE_MQ_REQUEST_BASED);
1975
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001976 /* Guess which priority_group will be used at next mapping time */
Will Deacon506458e2017-10-24 11:22:48 +01001977 pg = READ_ONCE(m->current_pg);
1978 next_pg = READ_ONCE(m->next_pg);
1979 if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg))
Mike Snitzer2da16102016-03-17 18:38:17 -04001980 pg = next_pg;
1981
1982 if (!pg) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001983 /*
1984 * We don't know which pg will be used at next mapping time.
Mike Snitzer2da16102016-03-17 18:38:17 -04001985 * We don't call choose_pgpath() here to avoid to trigger
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001986 * pg_init just by busy checking.
1987 * So we don't know whether underlying devices we will be using
1988 * at next mapping time are busy or not. Just try mapping.
1989 */
Mike Snitzer2da16102016-03-17 18:38:17 -04001990 return busy;
1991 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001992
1993 /*
1994 * If there is one non-busy active path at least, the path selector
1995 * will be able to select it. So we consider such a pg as not busy.
1996 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001997 busy = true;
Mike Snitzer2da16102016-03-17 18:38:17 -04001998 list_for_each_entry(pgpath, &pg->pgpaths, list) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001999 if (pgpath->is_active) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002000 has_active = true;
Mike Snitzer9f54cec2016-02-11 21:42:28 -05002001 if (!pgpath_busy(pgpath)) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002002 busy = false;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002003 break;
2004 }
2005 }
Mike Snitzer2da16102016-03-17 18:38:17 -04002006 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002007
Mike Snitzer2da16102016-03-17 18:38:17 -04002008 if (!has_active) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002009 /*
2010 * No active path in this pg, so this pg won't be used and
2011 * the current_pg will be changed at next mapping time.
2012 * We need to try mapping to determine it.
2013 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002014 busy = false;
Mike Snitzer2da16102016-03-17 18:38:17 -04002015 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002016
2017 return busy;
2018}
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020/*-----------------------------------------------------------------
2021 * Module setup
2022 *---------------------------------------------------------------*/
2023static struct target_type multipath_target = {
2024 .name = "multipath",
Mike Snitzere83068a2016-05-24 21:16:51 -04002025 .version = {1, 12, 0},
Mike Snitzer16f12262016-01-31 17:22:27 -05002026 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 .module = THIS_MODULE,
2028 .ctr = multipath_ctr,
2029 .dtr = multipath_dtr,
Mike Snitzere5863d92014-12-17 21:08:12 -05002030 .clone_and_map_rq = multipath_clone_and_map,
2031 .release_clone_rq = multipath_release_clone,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002032 .rq_end_io = multipath_end_io,
Mike Snitzer76e33fe2016-05-19 16:15:14 -04002033 .map = multipath_map_bio,
2034 .end_io = multipath_end_io_bio,
2035 .presuspend = multipath_presuspend,
2036 .postsuspend = multipath_postsuspend,
2037 .resume = multipath_resume,
2038 .status = multipath_status,
2039 .message = multipath_message,
2040 .prepare_ioctl = multipath_prepare_ioctl,
2041 .iterate_devices = multipath_iterate_devices,
2042 .busy = multipath_busy,
2043};
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045static int __init dm_multipath_init(void)
2046{
2047 int r;
2048
Tejun Heo4d4d66a2011-01-13 19:59:57 +00002049 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002050 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01002051 DMERR("failed to create workqueue kmpathd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002052 r = -ENOMEM;
2053 goto bad_alloc_kmultipathd;
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002054 }
2055
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002056 /*
2057 * A separate workqueue is used to handle the device handlers
2058 * to avoid overloading existing workqueue. Overloading the
2059 * old workqueue would also create a bottleneck in the
2060 * path of the storage hardware device activation.
2061 */
Tejun Heo4d4d66a2011-01-13 19:59:57 +00002062 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
2063 WQ_MEM_RECLAIM);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002064 if (!kmpath_handlerd) {
2065 DMERR("failed to create workqueue kmpath_handlerd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002066 r = -ENOMEM;
2067 goto bad_alloc_kmpath_handlerd;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002068 }
2069
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002070 r = dm_register_target(&multipath_target);
2071 if (r < 0) {
2072 DMERR("request-based register failed %d", r);
2073 r = -EINVAL;
2074 goto bad_register_target;
2075 }
2076
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002077 return 0;
2078
monty_pavel@sina.com7e6358d2017-11-25 01:43:50 +08002079bad_register_target:
2080 destroy_workqueue(kmpath_handlerd);
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002081bad_alloc_kmpath_handlerd:
2082 destroy_workqueue(kmultipathd);
2083bad_alloc_kmultipathd:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return r;
2085}
2086
2087static void __exit dm_multipath_exit(void)
2088{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002089 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002090 destroy_workqueue(kmultipathd);
2091
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002092 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095module_init(dm_multipath_init);
2096module_exit(dm_multipath_exit);
2097
2098MODULE_DESCRIPTION(DM_NAME " multipath target");
2099MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
2100MODULE_LICENSE("GPL");