blob: 373e5e3eec78d96496a01d2c70a6ab30fb7d9b49 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Sistina Software Limited.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4 *
5 * This file is released under the GPL.
6 */
7
Mikulas Patocka586e80e2008-10-21 17:44:59 +01008#include <linux/device-mapper.h>
9
Mike Snitzer4cc96132016-05-12 16:28:10 -040010#include "dm-rq.h"
Mike Snitzer76e33fe2016-05-19 16:15:14 -040011#include "dm-bio-record.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "dm-path-selector.h"
Mike Andersonb15546f2007-10-19 22:48:02 +010013#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Mike Snitzere5863d92014-12-17 21:08:12 -050015#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/ctype.h>
17#include <linux/init.h>
18#include <linux/mempool.h>
19#include <linux/module.h>
20#include <linux/pagemap.h>
21#include <linux/slab.h>
22#include <linux/time.h>
23#include <linux/workqueue.h>
Mikulas Patocka35991652012-06-03 00:29:58 +010024#include <linux/delay.h>
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070025#include <scsi/scsi_dh.h>
Arun Sharma600634972011-07-26 16:09:06 -070026#include <linux/atomic.h>
Mike Snitzer78ce23b2016-01-31 17:38:28 -050027#include <linux/blk-mq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Alasdair G Kergon72d94862006-06-26 00:27:35 -070029#define DM_MSG_PREFIX "multipath"
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000030#define DM_PG_INIT_DELAY_MSECS 2000
31#define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/* Path properties */
34struct pgpath {
35 struct list_head list;
36
37 struct priority_group *pg; /* Owning PG */
38 unsigned fail_count; /* Cumulative failure count */
39
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080040 struct dm_path path;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000041 struct delayed_work activate_path;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -050042
43 bool is_active:1; /* Path status */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
46#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
47
48/*
49 * Paths are grouped into Priority Groups and numbered from 1 upwards.
50 * Each has a path selector which controls which path gets used.
51 */
52struct priority_group {
53 struct list_head list;
54
55 struct multipath *m; /* Owning multipath instance */
56 struct path_selector ps;
57
58 unsigned pg_num; /* Reference number */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned nr_pgpaths; /* Number of paths in PG */
60 struct list_head pgpaths;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -050061
62 bool bypassed:1; /* Temporarily bypass this PG? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65/* Multipath context */
66struct multipath {
67 struct list_head list;
68 struct dm_target *ti;
69
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070070 const char *hw_handler_name;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -070071 char *hw_handler_params;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000072
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010073 spinlock_t lock;
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 unsigned nr_priority_groups;
76 struct list_head priority_groups;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000077
78 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 struct pgpath *current_pgpath;
81 struct priority_group *current_pg;
82 struct priority_group *next_pg; /* Switch to this PG if set */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Mike Snitzer518257b2016-03-17 16:32:10 -040084 unsigned long flags; /* Multipath state flags */
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010085
Dave Wysochanskic9e45582007-10-19 22:47:53 +010086 unsigned pg_init_retries; /* Number of times to retry pg_init */
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000087 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Mike Snitzer91e968a2016-03-17 17:10:15 -040089 atomic_t nr_valid_paths; /* Total number of usable paths */
90 atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */
91 atomic_t pg_init_count; /* Number of times pg_init called */
92
Mike Snitzere83068a2016-05-24 21:16:51 -040093 unsigned queue_mode;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 /*
Alasdair G Kergon028867a2007-07-12 17:26:32 +010096 * We must use a mempool of dm_mpath_io structs so that we
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 * can resubmit bios on error.
98 */
99 mempool_t *mpio_pool;
Mike Anderson6380f262009-12-10 23:52:21 +0000100
101 struct mutex work_mutex;
Mike Snitzer20800cb2016-03-17 17:13:10 -0400102 struct work_struct trigger_event;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400103
104 struct work_struct process_queued_bios;
105 struct bio_list queued_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
108/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400109 * Context information attached to each io we process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100111struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 struct pgpath *pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100113 size_t nr_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
116typedef int (*action_fn) (struct pgpath *pgpath);
117
Christoph Lametere18b8902006-12-06 20:33:20 -0800118static struct kmem_cache *_mpio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700120static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000121static void trigger_event(struct work_struct *work);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700122static void activate_path(struct work_struct *work);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400123static void process_queued_bios(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Mike Snitzer518257b2016-03-17 16:32:10 -0400125/*-----------------------------------------------
126 * Multipath state flags.
127 *-----------------------------------------------*/
128
129#define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */
130#define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */
131#define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */
132#define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */
133#define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */
134#define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */
135#define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/*-----------------------------------------------
138 * Allocation routines
139 *-----------------------------------------------*/
140
141static struct pgpath *alloc_pgpath(void)
142{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700143 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Mike Anderson224cb3e2008-08-29 09:36:09 +0200145 if (pgpath) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -0500146 pgpath->is_active = true;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000147 INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 return pgpath;
151}
152
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100153static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 kfree(pgpath);
156}
157
158static struct priority_group *alloc_priority_group(void)
159{
160 struct priority_group *pg;
161
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700162 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700164 if (pg)
165 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 return pg;
168}
169
170static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
171{
172 struct pgpath *pgpath, *tmp;
173
174 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
175 list_del(&pgpath->list);
176 dm_put_device(ti, pgpath->path.dev);
177 free_pgpath(pgpath);
178 }
179}
180
181static void free_priority_group(struct priority_group *pg,
182 struct dm_target *ti)
183{
184 struct path_selector *ps = &pg->ps;
185
186 if (ps->type) {
187 ps->type->destroy(ps);
188 dm_put_path_selector(ps->type);
189 }
190
191 free_pgpaths(&pg->pgpaths, ti);
192 kfree(pg);
193}
194
Mike Snitzere83068a2016-05-24 21:16:51 -0400195static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct multipath *m;
198
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700199 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 INIT_LIST_HEAD(&m->priority_groups);
202 spin_lock_init(&m->lock);
Mike Snitzer518257b2016-03-17 16:32:10 -0400203 set_bit(MPATHF_QUEUE_IO, &m->flags);
Mike Snitzer91e968a2016-03-17 17:10:15 -0400204 atomic_set(&m->nr_valid_paths, 0);
205 atomic_set(&m->pg_init_in_progress, 0);
206 atomic_set(&m->pg_init_count, 0);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000207 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
David Howellsc4028952006-11-22 14:57:56 +0000208 INIT_WORK(&m->trigger_event, trigger_event);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000209 init_waitqueue_head(&m->pg_init_wait);
Mike Anderson6380f262009-12-10 23:52:21 +0000210 mutex_init(&m->work_mutex);
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500211
212 m->mpio_pool = NULL;
Mike Snitzere83068a2016-05-24 21:16:51 -0400213 m->queue_mode = DM_TYPE_NONE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400214
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700215 m->ti = ti;
216 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
219 return m;
220}
221
Mike Snitzere83068a2016-05-24 21:16:51 -0400222static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m)
223{
224 if (m->queue_mode == DM_TYPE_NONE) {
225 /*
226 * Default to request-based.
227 */
228 if (dm_use_blk_mq(dm_table_get_md(ti->table)))
229 m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
230 else
231 m->queue_mode = DM_TYPE_REQUEST_BASED;
232 }
233
234 if (m->queue_mode == DM_TYPE_REQUEST_BASED) {
235 unsigned min_ios = dm_get_reserved_rq_based_ios();
236
237 m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache);
238 if (!m->mpio_pool)
239 return -ENOMEM;
240 }
241 else if (m->queue_mode == DM_TYPE_BIO_BASED) {
242 INIT_WORK(&m->process_queued_bios, process_queued_bios);
243 /*
244 * bio-based doesn't support any direct scsi_dh management;
245 * it just discovers if a scsi_dh is attached.
246 */
247 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
248 }
249
250 dm_table_set_type(ti->table, m->queue_mode);
251
252 return 0;
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static void free_multipath(struct multipath *m)
256{
257 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
260 list_del(&pg->list);
261 free_priority_group(pg, m->ti);
262 }
263
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700264 kfree(m->hw_handler_name);
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700265 kfree(m->hw_handler_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 mempool_destroy(m->mpio_pool);
267 kfree(m);
268}
269
Mike Snitzer2eff1922016-02-03 09:13:14 -0500270static struct dm_mpath_io *get_mpio(union map_info *info)
271{
272 return info->ptr;
273}
274
275static struct dm_mpath_io *set_mpio(struct multipath *m, union map_info *info)
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100276{
277 struct dm_mpath_io *mpio;
278
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500279 if (!m->mpio_pool) {
280 /* Use blk-mq pdu memory requested via per_io_data_size */
Mike Snitzer2eff1922016-02-03 09:13:14 -0500281 mpio = get_mpio(info);
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500282 memset(mpio, 0, sizeof(*mpio));
283 return mpio;
284 }
285
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100286 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
287 if (!mpio)
Mike Snitzer2eff1922016-02-03 09:13:14 -0500288 return NULL;
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100289
290 memset(mpio, 0, sizeof(*mpio));
291 info->ptr = mpio;
292
Mike Snitzer2eff1922016-02-03 09:13:14 -0500293 return mpio;
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100294}
295
Mike Snitzer2eff1922016-02-03 09:13:14 -0500296static void clear_request_fn_mpio(struct multipath *m, union map_info *info)
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100297{
Mike Snitzer2eff1922016-02-03 09:13:14 -0500298 /* Only needed for non blk-mq (.request_fn) multipath */
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500299 if (m->mpio_pool) {
300 struct dm_mpath_io *mpio = info->ptr;
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100301
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500302 info->ptr = NULL;
303 mempool_free(mpio, m->mpio_pool);
304 }
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100305}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Mike Snitzerbf661be2016-05-24 15:48:08 -0400307static size_t multipath_per_bio_data_size(void)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400308{
Mike Snitzerbf661be2016-05-24 15:48:08 -0400309 return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400310}
311
Mike Snitzerbf661be2016-05-24 15:48:08 -0400312static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
313{
314 return dm_per_bio_data(bio, multipath_per_bio_data_size());
315}
316
317static struct dm_bio_details *get_bio_details_from_bio(struct bio *bio)
318{
319 /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */
320 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
321 void *bio_details = mpio + 1;
322
323 return bio_details;
324}
325
326static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p,
327 struct dm_bio_details **bio_details_p)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400328{
329 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
Mike Snitzerbf661be2016-05-24 15:48:08 -0400330 struct dm_bio_details *bio_details = get_bio_details_from_bio(bio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400331
332 memset(mpio, 0, sizeof(*mpio));
Mike Snitzerbf661be2016-05-24 15:48:08 -0400333 memset(bio_details, 0, sizeof(*bio_details));
334 dm_bio_record(bio_details, bio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400335
Mike Snitzerbf661be2016-05-24 15:48:08 -0400336 if (mpio_p)
337 *mpio_p = mpio;
338 if (bio_details_p)
339 *bio_details_p = bio_details;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/*-----------------------------------------------
343 * Path selection
344 *-----------------------------------------------*/
345
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100346static int __pg_init_all_paths(struct multipath *m)
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000347{
348 struct pgpath *pgpath;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000349 unsigned long pg_init_delay = 0;
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000350
Mike Snitzer91e968a2016-03-17 17:10:15 -0400351 if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100352 return 0;
Hannes Reinecke17f4ff42014-02-28 15:33:42 +0100353
Mike Snitzer91e968a2016-03-17 17:10:15 -0400354 atomic_inc(&m->pg_init_count);
Mike Snitzer518257b2016-03-17 16:32:10 -0400355 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100356
357 /* Check here to reset pg_init_required */
358 if (!m->current_pg)
359 return 0;
360
Mike Snitzer518257b2016-03-17 16:32:10 -0400361 if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000362 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
363 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000364 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
365 /* Skip failed paths */
366 if (!pgpath->is_active)
367 continue;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000368 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
369 pg_init_delay))
Mike Snitzer91e968a2016-03-17 17:10:15 -0400370 atomic_inc(&m->pg_init_in_progress);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000371 }
Mike Snitzer91e968a2016-03-17 17:10:15 -0400372 return atomic_read(&m->pg_init_in_progress);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000373}
374
Bart Van Assche48135772016-11-15 15:34:09 -0800375static void pg_init_all_paths(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Mike Snitzer2da16102016-03-17 18:38:17 -0400377 unsigned long flags;
378
379 spin_lock_irqsave(&m->lock, flags);
Bart Van Assche48135772016-11-15 15:34:09 -0800380 __pg_init_all_paths(m);
Mike Snitzer2da16102016-03-17 18:38:17 -0400381 spin_unlock_irqrestore(&m->lock, flags);
Mike Snitzer2da16102016-03-17 18:38:17 -0400382}
383
384static void __switch_pg(struct multipath *m, struct priority_group *pg)
385{
386 m->current_pg = pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700389 if (m->hw_handler_name) {
Mike Snitzer518257b2016-03-17 16:32:10 -0400390 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
391 set_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 } else {
Mike Snitzer518257b2016-03-17 16:32:10 -0400393 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
394 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100396
Mike Snitzer91e968a2016-03-17 17:10:15 -0400397 atomic_set(&m->pg_init_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Mike Snitzer2da16102016-03-17 18:38:17 -0400400static struct pgpath *choose_path_in_pg(struct multipath *m,
401 struct priority_group *pg,
402 size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Mike Snitzer2da16102016-03-17 18:38:17 -0400404 unsigned long flags;
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800405 struct dm_path *path;
Mike Snitzer2da16102016-03-17 18:38:17 -0400406 struct pgpath *pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Mike Snitzer90a43232016-02-17 21:29:17 -0500408 path = pg->ps.type->select_path(&pg->ps, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (!path)
Mike Snitzer2da16102016-03-17 18:38:17 -0400410 return ERR_PTR(-ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Mike Snitzer2da16102016-03-17 18:38:17 -0400412 pgpath = path_to_pgpath(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Mike Snitzer2da16102016-03-17 18:38:17 -0400414 if (unlikely(lockless_dereference(m->current_pg) != pg)) {
415 /* Only update current_pgpath if pg changed */
416 spin_lock_irqsave(&m->lock, flags);
417 m->current_pgpath = pgpath;
418 __switch_pg(m, pg);
419 spin_unlock_irqrestore(&m->lock, flags);
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Mike Snitzer2da16102016-03-17 18:38:17 -0400422 return pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Mike Snitzer2da16102016-03-17 18:38:17 -0400425static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Mike Snitzer2da16102016-03-17 18:38:17 -0400427 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct priority_group *pg;
Mike Snitzer2da16102016-03-17 18:38:17 -0400429 struct pgpath *pgpath;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -0500430 bool bypassed = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Mike Snitzer91e968a2016-03-17 17:10:15 -0400432 if (!atomic_read(&m->nr_valid_paths)) {
Mike Snitzer518257b2016-03-17 16:32:10 -0400433 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 goto failed;
Benjamin Marzinski1f271972014-08-13 13:53:42 -0500435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /* Were we instructed to switch PG? */
Mike Snitzer2da16102016-03-17 18:38:17 -0400438 if (lockless_dereference(m->next_pg)) {
439 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 pg = m->next_pg;
Mike Snitzer2da16102016-03-17 18:38:17 -0400441 if (!pg) {
442 spin_unlock_irqrestore(&m->lock, flags);
443 goto check_current_pg;
444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 m->next_pg = NULL;
Mike Snitzer2da16102016-03-17 18:38:17 -0400446 spin_unlock_irqrestore(&m->lock, flags);
447 pgpath = choose_path_in_pg(m, pg, nr_bytes);
448 if (!IS_ERR_OR_NULL(pgpath))
449 return pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
452 /* Don't change PG until it has no remaining paths */
Mike Snitzer2da16102016-03-17 18:38:17 -0400453check_current_pg:
454 pg = lockless_dereference(m->current_pg);
455 if (pg) {
456 pgpath = choose_path_in_pg(m, pg, nr_bytes);
457 if (!IS_ERR_OR_NULL(pgpath))
458 return pgpath;
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /*
462 * Loop through priority groups until we find a valid path.
463 * First time we skip PGs marked 'bypassed'.
Mike Christief220fd42012-06-03 00:29:45 +0100464 * Second time we only try the ones we skipped, but set
465 * pg_init_delay_retry so we do not hammer controllers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 */
467 do {
468 list_for_each_entry(pg, &m->priority_groups, list) {
469 if (pg->bypassed == bypassed)
470 continue;
Mike Snitzer2da16102016-03-17 18:38:17 -0400471 pgpath = choose_path_in_pg(m, pg, nr_bytes);
472 if (!IS_ERR_OR_NULL(pgpath)) {
Mike Christief220fd42012-06-03 00:29:45 +0100473 if (!bypassed)
Mike Snitzer518257b2016-03-17 16:32:10 -0400474 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
Mike Snitzer2da16102016-03-17 18:38:17 -0400475 return pgpath;
Mike Christief220fd42012-06-03 00:29:45 +0100476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 } while (bypassed--);
479
480failed:
Mike Snitzer2da16102016-03-17 18:38:17 -0400481 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 m->current_pgpath = NULL;
483 m->current_pg = NULL;
Mike Snitzer2da16102016-03-17 18:38:17 -0400484 spin_unlock_irqrestore(&m->lock, flags);
485
486 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800489/*
490 * Check whether bios must be queued in the device-mapper core rather
491 * than here in the target.
492 *
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800493 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
494 * same value then we are not between multipath_presuspend()
495 * and multipath_resume() calls and we have no need to check
496 * for the DMF_NOFLUSH_SUSPENDING flag.
497 */
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400498static bool __must_push_back(struct multipath *m)
499{
500 return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) !=
501 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) &&
502 dm_noflush_suspending(m->ti));
503}
504
505static bool must_push_back_rq(struct multipath *m)
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800506{
Mike Snitzer1814f2e2016-07-25 21:08:51 -0400507 bool r;
508 unsigned long flags;
509
510 spin_lock_irqsave(&m->lock, flags);
511 r = (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) ||
512 __must_push_back(m));
513 spin_unlock_irqrestore(&m->lock, flags);
514
515 return r;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400516}
517
518static bool must_push_back_bio(struct multipath *m)
519{
Mike Snitzer1814f2e2016-07-25 21:08:51 -0400520 bool r;
521 unsigned long flags;
522
523 spin_lock_irqsave(&m->lock, flags);
524 r = __must_push_back(m);
525 spin_unlock_irqrestore(&m->lock, flags);
526
527 return r;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800528}
529
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100530/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400531 * Map cloned requests (request-based multipath)
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100532 */
Mike Snitzere5863d92014-12-17 21:08:12 -0500533static int __multipath_map(struct dm_target *ti, struct request *clone,
534 union map_info *map_context,
535 struct request *rq, struct request **__clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Mike Snitzer7943bd62016-02-02 21:53:15 -0500537 struct multipath *m = ti->private;
Hannes Reineckee3bde042014-02-28 15:33:46 +0100538 int r = DM_MAPIO_REQUEUE;
Mike Snitzere5863d92014-12-17 21:08:12 -0500539 size_t nr_bytes = clone ? blk_rq_bytes(clone) : blk_rq_bytes(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100541 struct block_device *bdev;
Hannes Reineckee3bde042014-02-28 15:33:46 +0100542 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* Do we need to select a new pgpath? */
Mike Snitzer2da16102016-03-17 18:38:17 -0400545 pgpath = lockless_dereference(m->current_pgpath);
546 if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
547 pgpath = choose_pgpath(m, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100549 if (!pgpath) {
Mike Snitzerb88efd42016-09-09 19:26:19 -0400550 if (must_push_back_rq(m))
551 return DM_MAPIO_DELAY_REQUEUE;
552 return -EIO; /* Failed */
Mike Snitzer518257b2016-03-17 16:32:10 -0400553 } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
554 test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
Mike Snitzer2da16102016-03-17 18:38:17 -0400555 pg_init_all_paths(m);
556 return r;
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100557 }
Mike Snitzer6afbc012014-07-08 11:55:09 -0400558
Mike Snitzer2eff1922016-02-03 09:13:14 -0500559 mpio = set_mpio(m, map_context);
560 if (!mpio)
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100561 /* ENOMEM, requeue */
Mike Snitzer2da16102016-03-17 18:38:17 -0400562 return r;
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100563
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100564 mpio->pgpath = pgpath;
565 mpio->nr_bytes = nr_bytes;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600566
567 bdev = pgpath->path.dev->bdev;
568
Mike Snitzere5863d92014-12-17 21:08:12 -0500569 if (clone) {
Mike Snitzerc5248f72016-02-20 14:02:49 -0500570 /*
571 * Old request-based interface: allocated clone is passed in.
572 * Used by: .request_fn stacked on .request_fn path(s).
573 */
Mike Snitzere5863d92014-12-17 21:08:12 -0500574 clone->q = bdev_get_queue(bdev);
575 clone->rq_disk = bdev->bd_disk;
576 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
577 } else {
Mike Snitzereca7ee62016-02-20 13:45:38 -0500578 /*
579 * blk-mq request-based interface; used by both:
580 * .request_fn stacked on blk-mq path(s) and
581 * blk-mq stacked on blk-mq path(s).
582 */
Mike Snitzer78ce23b2016-01-31 17:38:28 -0500583 *__clone = blk_mq_alloc_request(bdev_get_queue(bdev),
584 rq_data_dir(rq), BLK_MQ_REQ_NOWAIT);
Mike Snitzer4c6dd532015-05-27 15:23:56 -0400585 if (IS_ERR(*__clone)) {
Mike Snitzere5863d92014-12-17 21:08:12 -0500586 /* ENOMEM, requeue */
Mike Snitzer2eff1922016-02-03 09:13:14 -0500587 clear_request_fn_mpio(m, map_context);
Mike Snitzere5863d92014-12-17 21:08:12 -0500588 return r;
Mike Snitzer4c6dd532015-05-27 15:23:56 -0400589 }
Mike Snitzere5863d92014-12-17 21:08:12 -0500590 (*__clone)->bio = (*__clone)->biotail = NULL;
591 (*__clone)->rq_disk = bdev->bd_disk;
592 (*__clone)->cmd_flags |= REQ_FAILFAST_TRANSPORT;
593 }
594
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100595 if (pgpath->pg->ps.type->start_io)
596 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
597 &pgpath->path,
598 nr_bytes);
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600599 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Mike Snitzere5863d92014-12-17 21:08:12 -0500602static int multipath_map(struct dm_target *ti, struct request *clone,
603 union map_info *map_context)
604{
605 return __multipath_map(ti, clone, map_context, NULL, NULL);
606}
607
608static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
609 union map_info *map_context,
610 struct request **clone)
611{
612 return __multipath_map(ti, NULL, map_context, rq, clone);
613}
614
615static void multipath_release_clone(struct request *clone)
616{
Mike Snitzer78ce23b2016-01-31 17:38:28 -0500617 blk_mq_free_request(clone);
Mike Snitzere5863d92014-12-17 21:08:12 -0500618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400621 * Map cloned bios (bio-based multipath)
622 */
623static int __multipath_map_bio(struct multipath *m, struct bio *bio, struct dm_mpath_io *mpio)
624{
625 size_t nr_bytes = bio->bi_iter.bi_size;
626 struct pgpath *pgpath;
627 unsigned long flags;
628 bool queue_io;
629
630 /* Do we need to select a new pgpath? */
631 pgpath = lockless_dereference(m->current_pgpath);
632 queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
633 if (!pgpath || !queue_io)
634 pgpath = choose_pgpath(m, nr_bytes);
635
636 if ((pgpath && queue_io) ||
637 (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
638 /* Queue for the daemon to resubmit */
639 spin_lock_irqsave(&m->lock, flags);
640 bio_list_add(&m->queued_bios, bio);
641 spin_unlock_irqrestore(&m->lock, flags);
642 /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
643 if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
644 pg_init_all_paths(m);
645 else if (!queue_io)
646 queue_work(kmultipathd, &m->process_queued_bios);
647 return DM_MAPIO_SUBMITTED;
648 }
649
650 if (!pgpath) {
651 if (!must_push_back_bio(m))
652 return -EIO;
653 return DM_MAPIO_REQUEUE;
654 }
655
656 mpio->pgpath = pgpath;
657 mpio->nr_bytes = nr_bytes;
658
659 bio->bi_error = 0;
660 bio->bi_bdev = pgpath->path.dev->bdev;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600661 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400662
663 if (pgpath->pg->ps.type->start_io)
664 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
665 &pgpath->path,
666 nr_bytes);
667 return DM_MAPIO_REMAPPED;
668}
669
670static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
671{
672 struct multipath *m = ti->private;
Mike Snitzerbf661be2016-05-24 15:48:08 -0400673 struct dm_mpath_io *mpio = NULL;
674
675 multipath_init_per_bio_data(bio, &mpio, NULL);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400676
677 return __multipath_map_bio(m, bio, mpio);
678}
679
Mike Snitzer7e48c762016-09-14 10:47:03 -0400680static void process_queued_io_list(struct multipath *m)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400681{
Mike Snitzer7e48c762016-09-14 10:47:03 -0400682 if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED)
683 dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
684 else if (m->queue_mode == DM_TYPE_BIO_BASED)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400685 queue_work(kmultipathd, &m->process_queued_bios);
686}
687
688static void process_queued_bios(struct work_struct *work)
689{
690 int r;
691 unsigned long flags;
692 struct bio *bio;
693 struct bio_list bios;
694 struct blk_plug plug;
695 struct multipath *m =
696 container_of(work, struct multipath, process_queued_bios);
697
698 bio_list_init(&bios);
699
700 spin_lock_irqsave(&m->lock, flags);
701
702 if (bio_list_empty(&m->queued_bios)) {
703 spin_unlock_irqrestore(&m->lock, flags);
704 return;
705 }
706
707 bio_list_merge(&bios, &m->queued_bios);
708 bio_list_init(&m->queued_bios);
709
710 spin_unlock_irqrestore(&m->lock, flags);
711
712 blk_start_plug(&plug);
713 while ((bio = bio_list_pop(&bios))) {
714 r = __multipath_map_bio(m, bio, get_mpio_from_bio(bio));
715 if (r < 0 || r == DM_MAPIO_REQUEUE) {
716 bio->bi_error = r;
717 bio_endio(bio);
718 } else if (r == DM_MAPIO_REMAPPED)
719 generic_make_request(bio);
720 }
721 blk_finish_plug(&plug);
722}
723
724/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * If we run out of usable paths, should we queue I/O or error it?
726 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -0500727static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
728 bool save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 unsigned long flags;
731
732 spin_lock_irqsave(&m->lock, flags);
733
Mike Snitzer518257b2016-03-17 16:32:10 -0400734 if (save_old_value) {
735 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
736 set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
737 else
738 clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
739 } else {
740 if (queue_if_no_path)
741 set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
742 else
743 clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
744 }
745 if (queue_if_no_path)
746 set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700747 else
Mike Snitzer518257b2016-03-17 16:32:10 -0400748 clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 spin_unlock_irqrestore(&m->lock, flags);
751
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400752 if (!queue_if_no_path) {
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200753 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -0400754 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400755 }
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return 0;
758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760/*
761 * An event is triggered whenever a path is taken out of use.
762 * Includes path failure and PG bypass.
763 */
David Howellsc4028952006-11-22 14:57:56 +0000764static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
David Howellsc4028952006-11-22 14:57:56 +0000766 struct multipath *m =
767 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 dm_table_event(m->ti->table);
770}
771
772/*-----------------------------------------------------------------
773 * Constructor/argument parsing:
774 * <#multipath feature args> [<arg>]*
775 * <#hw_handler args> [hw_handler [<arg>]*]
776 * <#priority groups>
777 * <initial priority group>
778 * [<selector> <#selector args> [<arg>]*
779 * <#paths> <#per-path selector args>
780 * [<path> [<arg>]* ]+ ]+
781 *---------------------------------------------------------------*/
Mike Snitzer498f0102011-08-02 12:32:04 +0100782static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 struct dm_target *ti)
784{
785 int r;
786 struct path_selector_type *pst;
787 unsigned ps_argc;
788
Mike Snitzer498f0102011-08-02 12:32:04 +0100789 static struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700790 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 };
792
Mike Snitzer498f0102011-08-02 12:32:04 +0100793 pst = dm_get_path_selector(dm_shift_arg(as));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700795 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return -EINVAL;
797 }
798
Mike Snitzer498f0102011-08-02 12:32:04 +0100799 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100800 if (r) {
801 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 r = pst->create(&pg->ps, ps_argc, as->argv);
806 if (r) {
807 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700808 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return r;
810 }
811
812 pg->ps.type = pst;
Mike Snitzer498f0102011-08-02 12:32:04 +0100813 dm_consume_args(as, ps_argc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 return 0;
816}
817
Mike Snitzer498f0102011-08-02 12:32:04 +0100818static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 struct dm_target *ti)
820{
821 int r;
822 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700823 struct multipath *m = ti->private;
Mike Snitzera58a9352012-07-27 15:08:04 +0100824 struct request_queue *q = NULL;
825 const char *attached_handler_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /* we need at least a path arg */
828 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700829 ti->error = "no device given";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100830 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
833 p = alloc_pgpath();
834 if (!p)
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100835 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Mike Snitzer498f0102011-08-02 12:32:04 +0100837 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +0000838 &p->path.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700840 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto bad;
842 }
843
Mike Snitzer518257b2016-03-17 16:32:10 -0400844 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) || m->hw_handler_name)
Mike Snitzera58a9352012-07-27 15:08:04 +0100845 q = bdev_get_queue(p->path.dev->bdev);
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100846
Mike Snitzer518257b2016-03-17 16:32:10 -0400847 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200848retain:
Mike Snitzera58a9352012-07-27 15:08:04 +0100849 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
850 if (attached_handler_name) {
851 /*
852 * Reset hw_handler_name to match the attached handler
853 * and clear any hw_handler_params associated with the
854 * ignored handler.
855 *
856 * NB. This modifies the table line to show the actual
857 * handler instead of the original table passed in.
858 */
859 kfree(m->hw_handler_name);
860 m->hw_handler_name = attached_handler_name;
861
862 kfree(m->hw_handler_params);
863 m->hw_handler_params = NULL;
864 }
865 }
866
867 if (m->hw_handler_name) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100868 r = scsi_dh_attach(q, m->hw_handler_name);
869 if (r == -EBUSY) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200870 char b[BDEVNAME_SIZE];
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100871
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200872 printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
873 bdevname(p->path.dev->bdev, b));
874 goto retain;
875 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700876 if (r < 0) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100877 ti->error = "error attaching hardware handler";
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700878 dm_put_device(ti, p->path.dev);
879 goto bad;
880 }
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700881
882 if (m->hw_handler_params) {
883 r = scsi_dh_set_params(q, m->hw_handler_params);
884 if (r < 0) {
885 ti->error = "unable to set hardware "
886 "handler parameters";
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700887 dm_put_device(ti, p->path.dev);
888 goto bad;
889 }
890 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700891 }
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
894 if (r) {
895 dm_put_device(ti, p->path.dev);
896 goto bad;
897 }
898
899 return p;
900
901 bad:
902 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100903 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Mike Snitzer498f0102011-08-02 12:32:04 +0100906static struct priority_group *parse_priority_group(struct dm_arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700907 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Mike Snitzer498f0102011-08-02 12:32:04 +0100909 static struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700910 {1, 1024, "invalid number of paths"},
911 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 };
913
914 int r;
Mike Snitzer498f0102011-08-02 12:32:04 +0100915 unsigned i, nr_selector_args, nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700917 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 if (as->argc < 2) {
920 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100921 ti->error = "not enough priority group arguments";
922 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
925 pg = alloc_priority_group();
926 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700927 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100928 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930 pg->m = m;
931
932 r = parse_path_selector(as, pg, ti);
933 if (r)
934 goto bad;
935
936 /*
937 * read the paths
938 */
Mike Snitzer498f0102011-08-02 12:32:04 +0100939 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (r)
941 goto bad;
942
Mike Snitzer498f0102011-08-02 12:32:04 +0100943 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (r)
945 goto bad;
946
Mike Snitzer498f0102011-08-02 12:32:04 +0100947 nr_args = 1 + nr_selector_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 for (i = 0; i < pg->nr_pgpaths; i++) {
949 struct pgpath *pgpath;
Mike Snitzer498f0102011-08-02 12:32:04 +0100950 struct dm_arg_set path_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Mike Snitzer498f0102011-08-02 12:32:04 +0100952 if (as->argc < nr_args) {
Mikulas Patocka148acff2008-07-21 12:00:30 +0100953 ti->error = "not enough path parameters";
Alasdair G Kergon6bbf79a2010-08-12 04:13:49 +0100954 r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Mike Snitzer498f0102011-08-02 12:32:04 +0100958 path_args.argc = nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 path_args.argv = as->argv;
960
961 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100962 if (IS_ERR(pgpath)) {
963 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 pgpath->pg = pg;
968 list_add_tail(&pgpath->list, &pg->pgpaths);
Mike Snitzer498f0102011-08-02 12:32:04 +0100969 dm_consume_args(as, nr_args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971
972 return pg;
973
974 bad:
975 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100976 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Mike Snitzer498f0102011-08-02 12:32:04 +0100979static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 unsigned hw_argc;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700982 int ret;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700983 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Mike Snitzer498f0102011-08-02 12:32:04 +0100985 static struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700986 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 };
988
Mike Snitzer498f0102011-08-02 12:32:04 +0100989 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return -EINVAL;
991
992 if (!hw_argc)
993 return 0;
994
Mike Snitzere83068a2016-05-24 21:16:51 -0400995 if (m->queue_mode == DM_TYPE_BIO_BASED) {
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400996 dm_consume_args(as, hw_argc);
997 DMERR("bio-based multipath doesn't allow hardware handler args");
998 return 0;
999 }
1000
Mike Snitzer498f0102011-08-02 12:32:04 +01001001 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
tang.junhuif97dc422016-10-28 17:04:46 +08001002 if (!m->hw_handler_name)
1003 return -EINVAL;
Chandra Seetharaman14e98c52008-11-13 23:39:06 +00001004
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -07001005 if (hw_argc > 1) {
1006 char *p;
1007 int i, j, len = 4;
1008
1009 for (i = 0; i <= hw_argc - 2; i++)
1010 len += strlen(as->argv[i]) + 1;
1011 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
1012 if (!p) {
1013 ti->error = "memory allocation failed";
1014 ret = -ENOMEM;
1015 goto fail;
1016 }
1017 j = sprintf(p, "%d", hw_argc - 1);
1018 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
1019 j = sprintf(p, "%s", as->argv[i]);
1020 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001021 dm_consume_args(as, hw_argc - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 return 0;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -07001024fail:
1025 kfree(m->hw_handler_name);
1026 m->hw_handler_name = NULL;
1027 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Mike Snitzer498f0102011-08-02 12:32:04 +01001030static int parse_features(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 int r;
1033 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001034 struct dm_target *ti = m->ti;
Mike Snitzer498f0102011-08-02 12:32:04 +01001035 const char *arg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Mike Snitzer498f0102011-08-02 12:32:04 +01001037 static struct dm_arg _args[] = {
Mike Snitzere83068a2016-05-24 21:16:51 -04001038 {0, 8, "invalid number of feature args"},
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001039 {1, 50, "pg_init_retries must be between 1 and 50"},
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001040 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 };
1042
Mike Snitzer498f0102011-08-02 12:32:04 +01001043 r = dm_read_arg_group(_args, as, &argc, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (r)
1045 return -EINVAL;
1046
1047 if (!argc)
1048 return 0;
1049
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001050 do {
Mike Snitzer498f0102011-08-02 12:32:04 +01001051 arg_name = dm_shift_arg(as);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001052 argc--;
1053
Mike Snitzer498f0102011-08-02 12:32:04 +01001054 if (!strcasecmp(arg_name, "queue_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001055 r = queue_if_no_path(m, true, false);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001056 continue;
1057 }
1058
Mike Snitzera58a9352012-07-27 15:08:04 +01001059 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
Mike Snitzer518257b2016-03-17 16:32:10 -04001060 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
Mike Snitzera58a9352012-07-27 15:08:04 +01001061 continue;
1062 }
1063
Mike Snitzer498f0102011-08-02 12:32:04 +01001064 if (!strcasecmp(arg_name, "pg_init_retries") &&
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001065 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001066 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001067 argc--;
1068 continue;
1069 }
1070
Mike Snitzer498f0102011-08-02 12:32:04 +01001071 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001072 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001073 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001074 argc--;
1075 continue;
1076 }
1077
Mike Snitzere83068a2016-05-24 21:16:51 -04001078 if (!strcasecmp(arg_name, "queue_mode") &&
1079 (argc >= 1)) {
1080 const char *queue_mode_name = dm_shift_arg(as);
1081
1082 if (!strcasecmp(queue_mode_name, "bio"))
1083 m->queue_mode = DM_TYPE_BIO_BASED;
1084 else if (!strcasecmp(queue_mode_name, "rq"))
1085 m->queue_mode = DM_TYPE_REQUEST_BASED;
1086 else if (!strcasecmp(queue_mode_name, "mq"))
1087 m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
1088 else {
1089 ti->error = "Unknown 'queue_mode' requested";
1090 r = -EINVAL;
1091 }
1092 argc--;
1093 continue;
1094 }
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001097 r = -EINVAL;
1098 } while (argc && !r);
1099
1100 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
Mike Snitzere83068a2016-05-24 21:16:51 -04001103static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Mike Snitzer498f0102011-08-02 12:32:04 +01001105 /* target arguments */
1106 static struct dm_arg _args[] = {
Mike Snitzera490a072011-03-24 13:54:33 +00001107 {0, 1024, "invalid number of priority groups"},
1108 {0, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 };
1110
1111 int r;
1112 struct multipath *m;
Mike Snitzer498f0102011-08-02 12:32:04 +01001113 struct dm_arg_set as;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 unsigned pg_count = 0;
1115 unsigned next_pg_num;
1116
1117 as.argc = argc;
1118 as.argv = argv;
1119
Mike Snitzere83068a2016-05-24 21:16:51 -04001120 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001122 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return -EINVAL;
1124 }
1125
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001126 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (r)
1128 goto bad;
1129
Mike Snitzere83068a2016-05-24 21:16:51 -04001130 r = alloc_multipath_stage2(ti, m);
1131 if (r)
1132 goto bad;
1133
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001134 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (r)
1136 goto bad;
1137
Mike Snitzer498f0102011-08-02 12:32:04 +01001138 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (r)
1140 goto bad;
1141
Mike Snitzer498f0102011-08-02 12:32:04 +01001142 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (r)
1144 goto bad;
1145
Mike Snitzera490a072011-03-24 13:54:33 +00001146 if ((!m->nr_priority_groups && next_pg_num) ||
1147 (m->nr_priority_groups && !next_pg_num)) {
1148 ti->error = "invalid initial priority group";
1149 r = -EINVAL;
1150 goto bad;
1151 }
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /* parse the priority groups */
1154 while (as.argc) {
1155 struct priority_group *pg;
Mike Snitzer91e968a2016-03-17 17:10:15 -04001156 unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001158 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +01001159 if (IS_ERR(pg)) {
1160 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 goto bad;
1162 }
1163
Mike Snitzer91e968a2016-03-17 17:10:15 -04001164 nr_valid_paths += pg->nr_pgpaths;
1165 atomic_set(&m->nr_valid_paths, nr_valid_paths);
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 list_add_tail(&pg->list, &m->priority_groups);
1168 pg_count++;
1169 pg->pg_num = pg_count;
1170 if (!--next_pg_num)
1171 m->next_pg = pg;
1172 }
1173
1174 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001175 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 r = -EINVAL;
1177 goto bad;
1178 }
1179
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001180 ti->num_flush_bios = 1;
1181 ti->num_discard_bios = 1;
Mike Snitzer042bcef2013-05-10 14:37:16 +01001182 ti->num_write_same_bios = 1;
Mike Snitzere83068a2016-05-24 21:16:51 -04001183 if (m->queue_mode == DM_TYPE_BIO_BASED)
Mike Snitzerbf661be2016-05-24 15:48:08 -04001184 ti->per_io_data_size = multipath_per_bio_data_size();
Mike Snitzere83068a2016-05-24 21:16:51 -04001185 else if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED)
Mike Snitzer8637a6b2016-01-31 12:08:36 -05001186 ti->per_io_data_size = sizeof(struct dm_mpath_io);
Mikulas Patocka86279212009-06-22 10:12:24 +01001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return 0;
1189
1190 bad:
1191 free_multipath(m);
1192 return r;
1193}
1194
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001195static void multipath_wait_for_pg_init_completion(struct multipath *m)
1196{
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001197 DEFINE_WAIT(wait);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001198
1199 while (1) {
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001200 prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001201
Mike Snitzer91e968a2016-03-17 17:10:15 -04001202 if (!atomic_read(&m->pg_init_in_progress))
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001203 break;
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001204
1205 io_schedule();
1206 }
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001207 finish_wait(&m->pg_init_wait, &wait);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001208}
1209
1210static void flush_multipath_work(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Mike Snitzer518257b2016-03-17 16:32:10 -04001212 set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1213 smp_mb__after_atomic();
Shiva Krishna Merla954a73d2013-10-30 03:26:38 +00001214
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001215 flush_workqueue(kmpath_handlerd);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001216 multipath_wait_for_pg_init_completion(m);
Alasdair G Kergona044d012005-07-12 15:53:02 -07001217 flush_workqueue(kmultipathd);
Tejun Heo43829732012-08-20 14:51:24 -07001218 flush_work(&m->trigger_event);
Shiva Krishna Merla954a73d2013-10-30 03:26:38 +00001219
Mike Snitzer518257b2016-03-17 16:32:10 -04001220 clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1221 smp_mb__after_atomic();
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001222}
1223
1224static void multipath_dtr(struct dm_target *ti)
1225{
1226 struct multipath *m = ti->private;
1227
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001228 flush_multipath_work(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 free_multipath(m);
1230}
1231
1232/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 * Take a path out of use.
1234 */
1235static int fail_path(struct pgpath *pgpath)
1236{
1237 unsigned long flags;
1238 struct multipath *m = pgpath->pg->m;
1239
1240 spin_lock_irqsave(&m->lock, flags);
1241
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001242 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 goto out;
1244
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001245 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001248 pgpath->is_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 pgpath->fail_count++;
1250
Mike Snitzer91e968a2016-03-17 17:10:15 -04001251 atomic_dec(&m->nr_valid_paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 if (pgpath == m->current_pgpath)
1254 m->current_pgpath = NULL;
1255
Mike Andersonb15546f2007-10-19 22:48:02 +01001256 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
Mike Snitzer91e968a2016-03-17 17:10:15 -04001257 pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
Mike Andersonb15546f2007-10-19 22:48:02 +01001258
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001259 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261out:
1262 spin_unlock_irqrestore(&m->lock, flags);
1263
1264 return 0;
1265}
1266
1267/*
1268 * Reinstate a previously-failed path
1269 */
1270static int reinstate_path(struct pgpath *pgpath)
1271{
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001272 int r = 0, run_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 unsigned long flags;
1274 struct multipath *m = pgpath->pg->m;
Mike Snitzer91e968a2016-03-17 17:10:15 -04001275 unsigned nr_valid_paths;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 spin_lock_irqsave(&m->lock, flags);
1278
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001279 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 goto out;
1281
Mike Snitzerec31f3f2016-02-20 12:49:43 -05001282 DMWARN("Reinstating path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1285 if (r)
1286 goto out;
1287
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001288 pgpath->is_active = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Mike Snitzer91e968a2016-03-17 17:10:15 -04001290 nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
1291 if (nr_valid_paths == 1) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001292 m->current_pgpath = NULL;
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001293 run_queue = 1;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001294 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001295 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
Mike Snitzer91e968a2016-03-17 17:10:15 -04001296 atomic_inc(&m->pg_init_in_progress);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Mike Andersonb15546f2007-10-19 22:48:02 +01001299 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
Mike Snitzer91e968a2016-03-17 17:10:15 -04001300 pgpath->path.dev->name, nr_valid_paths);
Mike Andersonb15546f2007-10-19 22:48:02 +01001301
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001302 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304out:
1305 spin_unlock_irqrestore(&m->lock, flags);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001306 if (run_queue) {
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001307 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -04001308 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 return r;
1312}
1313
1314/*
1315 * Fail or reinstate all paths that match the provided struct dm_dev.
1316 */
1317static int action_dev(struct multipath *m, struct dm_dev *dev,
1318 action_fn action)
1319{
Mike Snitzer19040c02011-03-24 13:54:31 +00001320 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 struct pgpath *pgpath;
1322 struct priority_group *pg;
1323
1324 list_for_each_entry(pg, &m->priority_groups, list) {
1325 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1326 if (pgpath->path.dev == dev)
1327 r = action(pgpath);
1328 }
1329 }
1330
1331 return r;
1332}
1333
1334/*
1335 * Temporarily try to avoid having to use the specified PG
1336 */
1337static void bypass_pg(struct multipath *m, struct priority_group *pg,
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001338 bool bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 unsigned long flags;
1341
1342 spin_lock_irqsave(&m->lock, flags);
1343
1344 pg->bypassed = bypassed;
1345 m->current_pgpath = NULL;
1346 m->current_pg = NULL;
1347
1348 spin_unlock_irqrestore(&m->lock, flags);
1349
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001350 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
1353/*
1354 * Switch to using the specified PG from the next I/O that gets mapped
1355 */
1356static int switch_pg_num(struct multipath *m, const char *pgstr)
1357{
1358 struct priority_group *pg;
1359 unsigned pgnum;
1360 unsigned long flags;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001361 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001363 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
tang.junhuicc5bd922016-11-04 12:37:09 +08001364 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 DMWARN("invalid PG number supplied to switch_pg_num");
1366 return -EINVAL;
1367 }
1368
1369 spin_lock_irqsave(&m->lock, flags);
1370 list_for_each_entry(pg, &m->priority_groups, list) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001371 pg->bypassed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (--pgnum)
1373 continue;
1374
1375 m->current_pgpath = NULL;
1376 m->current_pg = NULL;
1377 m->next_pg = pg;
1378 }
1379 spin_unlock_irqrestore(&m->lock, flags);
1380
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001381 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return 0;
1383}
1384
1385/*
1386 * Set/clear bypassed status of a PG.
1387 * PGs are numbered upwards from 1 in the order they were declared.
1388 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001389static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 struct priority_group *pg;
1392 unsigned pgnum;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001393 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001395 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
tang.junhuicc5bd922016-11-04 12:37:09 +08001396 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 DMWARN("invalid PG number supplied to bypass_pg");
1398 return -EINVAL;
1399 }
1400
1401 list_for_each_entry(pg, &m->priority_groups, list) {
1402 if (!--pgnum)
1403 break;
1404 }
1405
1406 bypass_pg(m, pg, bypassed);
1407 return 0;
1408}
1409
1410/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001411 * Should we retry pg_init immediately?
1412 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001413static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001414{
1415 unsigned long flags;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001416 bool limit_reached = false;
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001417
1418 spin_lock_irqsave(&m->lock, flags);
1419
Mike Snitzer91e968a2016-03-17 17:10:15 -04001420 if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
1421 !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
Mike Snitzer518257b2016-03-17 16:32:10 -04001422 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001423 else
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001424 limit_reached = true;
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001425
1426 spin_unlock_irqrestore(&m->lock, flags);
1427
1428 return limit_reached;
1429}
1430
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001431static void pg_init_done(void *data, int errors)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001432{
Moger, Babu83c0d5d2010-03-06 02:29:45 +00001433 struct pgpath *pgpath = data;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001434 struct priority_group *pg = pgpath->pg;
1435 struct multipath *m = pg->m;
1436 unsigned long flags;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001437 bool delay_retry = false;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001438
1439 /* device or driver problems */
1440 switch (errors) {
1441 case SCSI_DH_OK:
1442 break;
1443 case SCSI_DH_NOSYS:
1444 if (!m->hw_handler_name) {
1445 errors = 0;
1446 break;
1447 }
Moger, Babuf7b934c2010-03-06 02:29:49 +00001448 DMERR("Could not failover the device: Handler scsi_dh_%s "
1449 "Error %d.", m->hw_handler_name, errors);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001450 /*
1451 * Fail path for now, so we do not ping pong
1452 */
1453 fail_path(pgpath);
1454 break;
1455 case SCSI_DH_DEV_TEMP_BUSY:
1456 /*
1457 * Probably doing something like FW upgrade on the
1458 * controller so try the other pg.
1459 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001460 bypass_pg(m, pg, true);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001461 break;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001462 case SCSI_DH_RETRY:
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001463 /* Wait before retrying. */
1464 delay_retry = 1;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001465 case SCSI_DH_IMM_RETRY:
1466 case SCSI_DH_RES_TEMP_UNAVAIL:
1467 if (pg_init_limit_reached(m, pgpath))
1468 fail_path(pgpath);
1469 errors = 0;
1470 break;
Mike Snitzerec31f3f2016-02-20 12:49:43 -05001471 case SCSI_DH_DEV_OFFLINED:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001472 default:
1473 /*
1474 * We probably do not want to fail the path for a device
1475 * error, but this is what the old dm did. In future
1476 * patches we can do more advanced handling.
1477 */
1478 fail_path(pgpath);
1479 }
1480
1481 spin_lock_irqsave(&m->lock, flags);
1482 if (errors) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001483 if (pgpath == m->current_pgpath) {
1484 DMERR("Could not failover device. Error %d.", errors);
1485 m->current_pgpath = NULL;
1486 m->current_pg = NULL;
1487 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001488 } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001489 pg->bypassed = false;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001490
Mike Snitzer91e968a2016-03-17 17:10:15 -04001491 if (atomic_dec_return(&m->pg_init_in_progress) > 0)
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001492 /* Activations of other paths are still on going */
1493 goto out;
1494
Mike Snitzer518257b2016-03-17 16:32:10 -04001495 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1496 if (delay_retry)
1497 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1498 else
1499 clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1500
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001501 if (__pg_init_all_paths(m))
1502 goto out;
1503 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001504 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001505
Mike Snitzer7e48c762016-09-14 10:47:03 -04001506 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001507
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001508 /*
1509 * Wake up any thread waiting to suspend.
1510 */
1511 wake_up(&m->pg_init_wait);
1512
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001513out:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001514 spin_unlock_irqrestore(&m->lock, flags);
1515}
1516
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001517static void activate_path(struct work_struct *work)
1518{
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001519 struct pgpath *pgpath =
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001520 container_of(work, struct pgpath, activate_path.work);
Mike Snitzerf10e06b2016-09-01 12:06:37 -04001521 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001522
Mike Snitzerf10e06b2016-09-01 12:06:37 -04001523 if (pgpath->is_active && !blk_queue_dying(q))
1524 scsi_dh_activate(q, pg_init_done, pgpath);
Hannes Reinecke3a017502014-02-28 15:33:49 +01001525 else
1526 pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001527}
1528
Hannes Reinecke7e782af2013-07-01 15:16:26 +02001529static int noretry_error(int error)
1530{
1531 switch (error) {
Hannes Reinecke8ff232c2015-07-15 13:23:24 +02001532 case -EBADE:
1533 /*
1534 * EBADE signals an reservation conflict.
1535 * We shouldn't fail the path here as we can communicate with
1536 * the target. We should failover to the next path, but in
1537 * doing so we might be causing a ping-pong between paths.
1538 * So just return the reservation conflict error.
1539 */
Hannes Reinecke7e782af2013-07-01 15:16:26 +02001540 case -EOPNOTSUPP:
1541 case -EREMOTEIO:
1542 case -EILSEQ:
1543 case -ENODATA:
Jun'ichi Nomuracc9d3c32013-09-13 14:54:30 +09001544 case -ENOSPC:
Hannes Reinecke7e782af2013-07-01 15:16:26 +02001545 return 1;
1546 }
1547
1548 /* Anything else could be a path failure, so should be retried */
1549 return 0;
1550}
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552/*
1553 * end_io handling
1554 */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001555static int do_end_io(struct multipath *m, struct request *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001556 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001558 /*
1559 * We don't queue any clone request inside the multipath target
1560 * during end I/O handling, since those clone requests don't have
1561 * bio clones. If we queue them inside the multipath target,
1562 * we need to make bio clones, that requires memory allocation.
Mike Snitzer4cc96132016-05-12 16:28:10 -04001563 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001564 * don't have bio clones.)
1565 * Instead of queueing the clone request here, we queue the original
1566 * request into dm core, which will remake a clone request and
1567 * clone bios for it and resubmit it later.
1568 */
1569 int r = DM_ENDIO_REQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001571 if (!error && !clone->errors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return 0; /* I/O complete */
1573
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001574 if (noretry_error(error))
Mike Snitzer959eb4e2010-08-12 04:14:32 +01001575 return error;
1576
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001577 if (mpio->pgpath)
1578 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Mike Snitzer91e968a2016-03-17 17:10:15 -04001580 if (!atomic_read(&m->nr_valid_paths)) {
Mike Snitzer518257b2016-03-17 16:32:10 -04001581 if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001582 if (!must_push_back_rq(m))
Hannes Reinecke751b2a72011-01-18 10:13:12 +01001583 r = -EIO;
Hannes Reinecke751b2a72011-01-18 10:13:12 +01001584 }
1585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001587 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001590static int multipath_end_io(struct dm_target *ti, struct request *clone,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 int error, union map_info *map_context)
1592{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001593 struct multipath *m = ti->private;
Mike Snitzer2eff1922016-02-03 09:13:14 -05001594 struct dm_mpath_io *mpio = get_mpio(map_context);
Wei Yongjuna71a2612012-10-12 16:59:42 +01001595 struct pgpath *pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 struct path_selector *ps;
1597 int r;
1598
Jun'ichi Nomura466891f2012-03-28 18:41:25 +01001599 BUG_ON(!mpio);
1600
Mike Snitzer2eff1922016-02-03 09:13:14 -05001601 r = do_end_io(m, clone, error, mpio);
Wei Yongjuna71a2612012-10-12 16:59:42 +01001602 pgpath = mpio->pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (pgpath) {
1604 ps = &pgpath->pg->ps;
1605 if (ps->type->end_io)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001606 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
Mike Snitzer2eff1922016-02-03 09:13:14 -05001608 clear_request_fn_mpio(m, map_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 return r;
1611}
1612
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001613static int do_end_io_bio(struct multipath *m, struct bio *clone,
1614 int error, struct dm_mpath_io *mpio)
1615{
1616 unsigned long flags;
1617
1618 if (!error)
1619 return 0; /* I/O complete */
1620
1621 if (noretry_error(error))
1622 return error;
1623
1624 if (mpio->pgpath)
1625 fail_path(mpio->pgpath);
1626
1627 if (!atomic_read(&m->nr_valid_paths)) {
1628 if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
1629 if (!must_push_back_bio(m))
1630 return -EIO;
1631 return DM_ENDIO_REQUEUE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001632 }
1633 }
1634
1635 /* Queue for the daemon to resubmit */
Mike Snitzerbf661be2016-05-24 15:48:08 -04001636 dm_bio_restore(get_bio_details_from_bio(clone), clone);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001637
1638 spin_lock_irqsave(&m->lock, flags);
1639 bio_list_add(&m->queued_bios, clone);
1640 spin_unlock_irqrestore(&m->lock, flags);
1641 if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
1642 queue_work(kmultipathd, &m->process_queued_bios);
1643
1644 return DM_ENDIO_INCOMPLETE;
1645}
1646
1647static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone, int error)
1648{
1649 struct multipath *m = ti->private;
1650 struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
1651 struct pgpath *pgpath;
1652 struct path_selector *ps;
1653 int r;
1654
1655 BUG_ON(!mpio);
1656
1657 r = do_end_io_bio(m, clone, error, mpio);
1658 pgpath = mpio->pgpath;
1659 if (pgpath) {
1660 ps = &pgpath->pg->ps;
1661 if (ps->type->end_io)
1662 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1663 }
1664
1665 return r;
1666}
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668/*
1669 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001670 * the last path fails we must error any remaining I/O.
1671 * Note that if the freeze_bdev fails while suspending, the
1672 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 */
1674static void multipath_presuspend(struct dm_target *ti)
1675{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001676 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001678 queue_if_no_path(m, false, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001681static void multipath_postsuspend(struct dm_target *ti)
1682{
Mike Anderson6380f262009-12-10 23:52:21 +00001683 struct multipath *m = ti->private;
1684
1685 mutex_lock(&m->work_mutex);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001686 flush_multipath_work(m);
Mike Anderson6380f262009-12-10 23:52:21 +00001687 mutex_unlock(&m->work_mutex);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001688}
1689
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001690/*
1691 * Restore the queue_if_no_path setting.
1692 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693static void multipath_resume(struct dm_target *ti)
1694{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001695 struct multipath *m = ti->private;
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001696 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001698 spin_lock_irqsave(&m->lock, flags);
Mike Snitzer518257b2016-03-17 16:32:10 -04001699 if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags))
1700 set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
1701 else
1702 clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001703 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
1706/*
1707 * Info output has the following format:
1708 * num_multipath_feature_args [multipath_feature_args]*
1709 * num_handler_status_args [handler_status_args]*
1710 * num_groups init_group_number
1711 * [A|D|E num_ps_status_args [ps_status_args]*
1712 * num_paths num_selector_args
1713 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1714 *
1715 * Table output has the following format (identical to the constructor string):
1716 * num_feature_args [features_args]*
1717 * num_handler_args hw_handler [hw_handler_args]*
1718 * num_groups init_group_number
1719 * [priority selector-name num_ps_args [ps_args]*
1720 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1721 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001722static void multipath_status(struct dm_target *ti, status_type_t type,
1723 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
1725 int sz = 0;
1726 unsigned long flags;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001727 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 struct priority_group *pg;
1729 struct pgpath *p;
1730 unsigned pg_num;
1731 char state;
1732
1733 spin_lock_irqsave(&m->lock, flags);
1734
1735 /* Features */
1736 if (type == STATUSTYPE_INFO)
Mike Snitzer91e968a2016-03-17 17:10:15 -04001737 DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
1738 atomic_read(&m->pg_init_count));
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001739 else {
Mike Snitzer518257b2016-03-17 16:32:10 -04001740 DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001741 (m->pg_init_retries > 0) * 2 +
Mike Snitzera58a9352012-07-27 15:08:04 +01001742 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
Mike Snitzere83068a2016-05-24 21:16:51 -04001743 test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
1744 (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
1745
Mike Snitzer518257b2016-03-17 16:32:10 -04001746 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001747 DMEMIT("queue_if_no_path ");
1748 if (m->pg_init_retries)
1749 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001750 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1751 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
Mike Snitzer518257b2016-03-17 16:32:10 -04001752 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
Mike Snitzera58a9352012-07-27 15:08:04 +01001753 DMEMIT("retain_attached_hw_handler ");
Mike Snitzere83068a2016-05-24 21:16:51 -04001754 if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
1755 switch(m->queue_mode) {
1756 case DM_TYPE_BIO_BASED:
1757 DMEMIT("queue_mode bio ");
1758 break;
1759 case DM_TYPE_MQ_REQUEST_BASED:
1760 DMEMIT("queue_mode mq ");
1761 break;
1762 }
1763 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001766 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 DMEMIT("0 ");
1768 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001769 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 DMEMIT("%u ", m->nr_priority_groups);
1772
1773 if (m->next_pg)
1774 pg_num = m->next_pg->pg_num;
1775 else if (m->current_pg)
1776 pg_num = m->current_pg->pg_num;
1777 else
Mike Snitzera490a072011-03-24 13:54:33 +00001778 pg_num = (m->nr_priority_groups ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 DMEMIT("%u ", pg_num);
1781
1782 switch (type) {
1783 case STATUSTYPE_INFO:
1784 list_for_each_entry(pg, &m->priority_groups, list) {
1785 if (pg->bypassed)
1786 state = 'D'; /* Disabled */
1787 else if (pg == m->current_pg)
1788 state = 'A'; /* Currently Active */
1789 else
1790 state = 'E'; /* Enabled */
1791
1792 DMEMIT("%c ", state);
1793
1794 if (pg->ps.type->status)
1795 sz += pg->ps.type->status(&pg->ps, NULL, type,
1796 result + sz,
1797 maxlen - sz);
1798 else
1799 DMEMIT("0 ");
1800
1801 DMEMIT("%u %u ", pg->nr_pgpaths,
1802 pg->ps.type->info_args);
1803
1804 list_for_each_entry(p, &pg->pgpaths, list) {
1805 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001806 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 p->fail_count);
1808 if (pg->ps.type->status)
1809 sz += pg->ps.type->status(&pg->ps,
1810 &p->path, type, result + sz,
1811 maxlen - sz);
1812 }
1813 }
1814 break;
1815
1816 case STATUSTYPE_TABLE:
1817 list_for_each_entry(pg, &m->priority_groups, list) {
1818 DMEMIT("%s ", pg->ps.type->name);
1819
1820 if (pg->ps.type->status)
1821 sz += pg->ps.type->status(&pg->ps, NULL, type,
1822 result + sz,
1823 maxlen - sz);
1824 else
1825 DMEMIT("0 ");
1826
1827 DMEMIT("%u %u ", pg->nr_pgpaths,
1828 pg->ps.type->table_args);
1829
1830 list_for_each_entry(p, &pg->pgpaths, list) {
1831 DMEMIT("%s ", p->path.dev->name);
1832 if (pg->ps.type->status)
1833 sz += pg->ps.type->status(&pg->ps,
1834 &p->path, type, result + sz,
1835 maxlen - sz);
1836 }
1837 }
1838 break;
1839 }
1840
1841 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
1844static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1845{
Mike Anderson6380f262009-12-10 23:52:21 +00001846 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 struct dm_dev *dev;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001848 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 action_fn action;
1850
Mike Anderson6380f262009-12-10 23:52:21 +00001851 mutex_lock(&m->work_mutex);
1852
Kiyoshi Uedac2f3d242009-12-10 23:52:27 +00001853 if (dm_suspended(ti)) {
1854 r = -EBUSY;
1855 goto out;
1856 }
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (argc == 1) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001859 if (!strcasecmp(argv[0], "queue_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001860 r = queue_if_no_path(m, true, false);
Mike Anderson6380f262009-12-10 23:52:21 +00001861 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001862 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001863 r = queue_if_no_path(m, false, false);
Mike Anderson6380f262009-12-10 23:52:21 +00001864 goto out;
1865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867
Mike Anderson6380f262009-12-10 23:52:21 +00001868 if (argc != 2) {
Jose Castilloa356e422014-01-29 17:52:45 +01001869 DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
Mike Anderson6380f262009-12-10 23:52:21 +00001870 goto out;
1871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Mike Snitzer498f0102011-08-02 12:32:04 +01001873 if (!strcasecmp(argv[0], "disable_group")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001874 r = bypass_pg_num(m, argv[1], true);
Mike Anderson6380f262009-12-10 23:52:21 +00001875 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001876 } else if (!strcasecmp(argv[0], "enable_group")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001877 r = bypass_pg_num(m, argv[1], false);
Mike Anderson6380f262009-12-10 23:52:21 +00001878 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001879 } else if (!strcasecmp(argv[0], "switch_group")) {
Mike Anderson6380f262009-12-10 23:52:21 +00001880 r = switch_pg_num(m, argv[1]);
1881 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001882 } else if (!strcasecmp(argv[0], "reinstate_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 action = reinstate_path;
Mike Snitzer498f0102011-08-02 12:32:04 +01001884 else if (!strcasecmp(argv[0], "fail_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 action = fail_path;
Mike Anderson6380f262009-12-10 23:52:21 +00001886 else {
Jose Castilloa356e422014-01-29 17:52:45 +01001887 DMWARN("Unrecognised multipath message received: %s", argv[0]);
Mike Anderson6380f262009-12-10 23:52:21 +00001888 goto out;
1889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00001891 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001893 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 argv[1]);
Mike Anderson6380f262009-12-10 23:52:21 +00001895 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
1897
1898 r = action_dev(m, dev, action);
1899
1900 dm_put_device(ti, dev);
1901
Mike Anderson6380f262009-12-10 23:52:21 +00001902out:
1903 mutex_unlock(&m->work_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001907static int multipath_prepare_ioctl(struct dm_target *ti,
1908 struct block_device **bdev, fmode_t *mode)
Milan Broz9af4aa32006-10-03 01:15:20 -07001909{
Mikulas Patocka35991652012-06-03 00:29:58 +01001910 struct multipath *m = ti->private;
Mike Snitzer2da16102016-03-17 18:38:17 -04001911 struct pgpath *current_pgpath;
Mikulas Patocka35991652012-06-03 00:29:58 +01001912 int r;
1913
Mike Snitzer2da16102016-03-17 18:38:17 -04001914 current_pgpath = lockless_dereference(m->current_pgpath);
1915 if (!current_pgpath)
1916 current_pgpath = choose_pgpath(m, 0);
Milan Broz9af4aa32006-10-03 01:15:20 -07001917
Mike Snitzer2da16102016-03-17 18:38:17 -04001918 if (current_pgpath) {
Mike Snitzer518257b2016-03-17 16:32:10 -04001919 if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
Mike Snitzer2da16102016-03-17 18:38:17 -04001920 *bdev = current_pgpath->path.dev->bdev;
1921 *mode = current_pgpath->path.dev->mode;
Junichi Nomura43e43c92015-11-17 09:36:56 +00001922 r = 0;
1923 } else {
1924 /* pg_init has not started or completed */
1925 r = -ENOTCONN;
1926 }
1927 } else {
1928 /* No path is available */
Mike Snitzer518257b2016-03-17 16:32:10 -04001929 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Junichi Nomura43e43c92015-11-17 09:36:56 +00001930 r = -ENOTCONN;
1931 else
1932 r = -EIO;
Milan Broze90dae12006-10-03 01:15:22 -07001933 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001934
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +00001935 if (r == -ENOTCONN) {
Mike Snitzer2da16102016-03-17 18:38:17 -04001936 if (!lockless_dereference(m->current_pg)) {
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001937 /* Path status changed, redo selection */
Mike Snitzer2da16102016-03-17 18:38:17 -04001938 (void) choose_pgpath(m, 0);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001939 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001940 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
Mike Snitzer2da16102016-03-17 18:38:17 -04001941 pg_init_all_paths(m);
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001942 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -04001943 process_queued_io_list(m);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001944 }
Mikulas Patocka35991652012-06-03 00:29:58 +01001945
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001946 /*
1947 * Only pass ioctls through if the device sizes match exactly.
1948 */
1949 if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1950 return 1;
1951 return r;
Milan Broz9af4aa32006-10-03 01:15:20 -07001952}
1953
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001954static int multipath_iterate_devices(struct dm_target *ti,
1955 iterate_devices_callout_fn fn, void *data)
1956{
1957 struct multipath *m = ti->private;
1958 struct priority_group *pg;
1959 struct pgpath *p;
1960 int ret = 0;
1961
1962 list_for_each_entry(pg, &m->priority_groups, list) {
1963 list_for_each_entry(p, &pg->pgpaths, list) {
Mike Snitzer5dea2712009-07-23 20:30:42 +01001964 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001965 if (ret)
1966 goto out;
1967 }
1968 }
1969
1970out:
1971 return ret;
1972}
1973
Mike Snitzer9f54cec2016-02-11 21:42:28 -05001974static int pgpath_busy(struct pgpath *pgpath)
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001975{
1976 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1977
Mike Snitzer52b09912015-02-23 16:36:41 -05001978 return blk_lld_busy(q);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001979}
1980
1981/*
1982 * We return "busy", only when we can map I/Os but underlying devices
1983 * are busy (so even if we map I/Os now, the I/Os will wait on
1984 * the underlying queue).
1985 * In other words, if we want to kill I/Os or queue them inside us
1986 * due to map unavailability, we don't return "busy". Otherwise,
1987 * dm core won't give us the I/Os and we can't do what we want.
1988 */
1989static int multipath_busy(struct dm_target *ti)
1990{
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001991 bool busy = false, has_active = false;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001992 struct multipath *m = ti->private;
Mike Snitzer2da16102016-03-17 18:38:17 -04001993 struct priority_group *pg, *next_pg;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001994 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001995
Mike Snitzerb88efd42016-09-09 19:26:19 -04001996 /* pg_init in progress */
1997 if (atomic_read(&m->pg_init_in_progress))
Mike Snitzer2da16102016-03-17 18:38:17 -04001998 return true;
1999
Mike Snitzerb88efd42016-09-09 19:26:19 -04002000 /* no paths available, for blk-mq: rely on IO mapping to delay requeue */
2001 if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
2002 return (m->queue_mode != DM_TYPE_MQ_REQUEST_BASED);
2003
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002004 /* Guess which priority_group will be used at next mapping time */
Mike Snitzer2da16102016-03-17 18:38:17 -04002005 pg = lockless_dereference(m->current_pg);
2006 next_pg = lockless_dereference(m->next_pg);
2007 if (unlikely(!lockless_dereference(m->current_pgpath) && next_pg))
2008 pg = next_pg;
2009
2010 if (!pg) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002011 /*
2012 * We don't know which pg will be used at next mapping time.
Mike Snitzer2da16102016-03-17 18:38:17 -04002013 * We don't call choose_pgpath() here to avoid to trigger
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002014 * pg_init just by busy checking.
2015 * So we don't know whether underlying devices we will be using
2016 * at next mapping time are busy or not. Just try mapping.
2017 */
Mike Snitzer2da16102016-03-17 18:38:17 -04002018 return busy;
2019 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002020
2021 /*
2022 * If there is one non-busy active path at least, the path selector
2023 * will be able to select it. So we consider such a pg as not busy.
2024 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002025 busy = true;
Mike Snitzer2da16102016-03-17 18:38:17 -04002026 list_for_each_entry(pgpath, &pg->pgpaths, list) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002027 if (pgpath->is_active) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002028 has_active = true;
Mike Snitzer9f54cec2016-02-11 21:42:28 -05002029 if (!pgpath_busy(pgpath)) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002030 busy = false;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002031 break;
2032 }
2033 }
Mike Snitzer2da16102016-03-17 18:38:17 -04002034 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002035
Mike Snitzer2da16102016-03-17 18:38:17 -04002036 if (!has_active) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002037 /*
2038 * No active path in this pg, so this pg won't be used and
2039 * the current_pg will be changed at next mapping time.
2040 * We need to try mapping to determine it.
2041 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05002042 busy = false;
Mike Snitzer2da16102016-03-17 18:38:17 -04002043 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002044
2045 return busy;
2046}
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048/*-----------------------------------------------------------------
2049 * Module setup
2050 *---------------------------------------------------------------*/
2051static struct target_type multipath_target = {
2052 .name = "multipath",
Mike Snitzere83068a2016-05-24 21:16:51 -04002053 .version = {1, 12, 0},
Mike Snitzer16f12262016-01-31 17:22:27 -05002054 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 .module = THIS_MODULE,
2056 .ctr = multipath_ctr,
2057 .dtr = multipath_dtr,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002058 .map_rq = multipath_map,
Mike Snitzere5863d92014-12-17 21:08:12 -05002059 .clone_and_map_rq = multipath_clone_and_map,
2060 .release_clone_rq = multipath_release_clone,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002061 .rq_end_io = multipath_end_io,
Mike Snitzer76e33fe2016-05-19 16:15:14 -04002062 .map = multipath_map_bio,
2063 .end_io = multipath_end_io_bio,
2064 .presuspend = multipath_presuspend,
2065 .postsuspend = multipath_postsuspend,
2066 .resume = multipath_resume,
2067 .status = multipath_status,
2068 .message = multipath_message,
2069 .prepare_ioctl = multipath_prepare_ioctl,
2070 .iterate_devices = multipath_iterate_devices,
2071 .busy = multipath_busy,
2072};
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074static int __init dm_multipath_init(void)
2075{
2076 int r;
2077
Mike Snitzer76e33fe2016-05-19 16:15:14 -04002078 /* allocate a slab for the dm_mpath_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002079 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (!_mpio_cache)
2081 return -ENOMEM;
2082
2083 r = dm_register_target(&multipath_target);
2084 if (r < 0) {
Mike Snitzer76e33fe2016-05-19 16:15:14 -04002085 DMERR("request-based register failed %d", r);
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002086 r = -EINVAL;
2087 goto bad_register_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
2089
Tejun Heo4d4d66a2011-01-13 19:59:57 +00002090 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002091 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01002092 DMERR("failed to create workqueue kmpathd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002093 r = -ENOMEM;
2094 goto bad_alloc_kmultipathd;
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002095 }
2096
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002097 /*
2098 * A separate workqueue is used to handle the device handlers
2099 * to avoid overloading existing workqueue. Overloading the
2100 * old workqueue would also create a bottleneck in the
2101 * path of the storage hardware device activation.
2102 */
Tejun Heo4d4d66a2011-01-13 19:59:57 +00002103 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
2104 WQ_MEM_RECLAIM);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002105 if (!kmpath_handlerd) {
2106 DMERR("failed to create workqueue kmpath_handlerd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002107 r = -ENOMEM;
2108 goto bad_alloc_kmpath_handlerd;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002109 }
2110
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002111 return 0;
2112
2113bad_alloc_kmpath_handlerd:
2114 destroy_workqueue(kmultipathd);
2115bad_alloc_kmultipathd:
2116 dm_unregister_target(&multipath_target);
2117bad_register_target:
2118 kmem_cache_destroy(_mpio_cache);
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 return r;
2121}
2122
2123static void __exit dm_multipath_exit(void)
2124{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002125 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002126 destroy_workqueue(kmultipathd);
2127
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002128 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 kmem_cache_destroy(_mpio_cache);
2130}
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132module_init(dm_multipath_init);
2133module_exit(dm_multipath_exit);
2134
2135MODULE_DESCRIPTION(DM_NAME " multipath target");
2136MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
2137MODULE_LICENSE("GPL");