blob: 730ec0be1afa855a1c1ffc3076b8aa787201ac60 [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
Mike Anderson6380f262009-12-10 23:52:21 +000095 struct mutex work_mutex;
Mike Snitzer20800cb2016-03-17 17:13:10 -040096 struct work_struct trigger_event;
Mike Snitzer76e33fe2016-05-19 16:15:14 -040097
98 struct work_struct process_queued_bios;
99 struct bio_list queued_bios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
102/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400103 * Context information attached to each io we process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100105struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct pgpath *pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100107 size_t nr_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110typedef int (*action_fn) (struct pgpath *pgpath);
111
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700112static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000113static void trigger_event(struct work_struct *work);
Bart Van Assche89bfce72017-04-27 10:11:14 -0700114static void activate_or_offline_path(struct pgpath *pgpath);
115static void activate_path_work(struct work_struct *work);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400116static void process_queued_bios(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Mike Snitzer518257b2016-03-17 16:32:10 -0400118/*-----------------------------------------------
119 * Multipath state flags.
120 *-----------------------------------------------*/
121
122#define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */
123#define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */
124#define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */
125#define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */
126#define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */
127#define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */
128#define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/*-----------------------------------------------
131 * Allocation routines
132 *-----------------------------------------------*/
133
134static struct pgpath *alloc_pgpath(void)
135{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700136 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Mike Anderson224cb3e2008-08-29 09:36:09 +0200138 if (pgpath) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -0500139 pgpath->is_active = true;
Bart Van Assche89bfce72017-04-27 10:11:14 -0700140 INIT_DELAYED_WORK(&pgpath->activate_path, activate_path_work);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 return pgpath;
144}
145
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100146static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 kfree(pgpath);
149}
150
151static struct priority_group *alloc_priority_group(void)
152{
153 struct priority_group *pg;
154
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700155 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700157 if (pg)
158 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 return pg;
161}
162
163static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
164{
165 struct pgpath *pgpath, *tmp;
166
167 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
168 list_del(&pgpath->list);
169 dm_put_device(ti, pgpath->path.dev);
170 free_pgpath(pgpath);
171 }
172}
173
174static void free_priority_group(struct priority_group *pg,
175 struct dm_target *ti)
176{
177 struct path_selector *ps = &pg->ps;
178
179 if (ps->type) {
180 ps->type->destroy(ps);
181 dm_put_path_selector(ps->type);
182 }
183
184 free_pgpaths(&pg->pgpaths, ti);
185 kfree(pg);
186}
187
Mike Snitzere83068a2016-05-24 21:16:51 -0400188static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 struct multipath *m;
191
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700192 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 INIT_LIST_HEAD(&m->priority_groups);
195 spin_lock_init(&m->lock);
Mike Snitzer518257b2016-03-17 16:32:10 -0400196 set_bit(MPATHF_QUEUE_IO, &m->flags);
Mike Snitzer91e968a2016-03-17 17:10:15 -0400197 atomic_set(&m->nr_valid_paths, 0);
198 atomic_set(&m->pg_init_in_progress, 0);
199 atomic_set(&m->pg_init_count, 0);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000200 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
David Howellsc4028952006-11-22 14:57:56 +0000201 INIT_WORK(&m->trigger_event, trigger_event);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000202 init_waitqueue_head(&m->pg_init_wait);
Mike Anderson6380f262009-12-10 23:52:21 +0000203 mutex_init(&m->work_mutex);
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500204
Mike Snitzere83068a2016-05-24 21:16:51 -0400205 m->queue_mode = DM_TYPE_NONE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400206
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700207 m->ti = ti;
208 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 return m;
212}
213
Mike Snitzere83068a2016-05-24 21:16:51 -0400214static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m)
215{
216 if (m->queue_mode == DM_TYPE_NONE) {
217 /*
218 * Default to request-based.
219 */
220 if (dm_use_blk_mq(dm_table_get_md(ti->table)))
221 m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
222 else
223 m->queue_mode = DM_TYPE_REQUEST_BASED;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100224 } else if (m->queue_mode == DM_TYPE_BIO_BASED) {
Mike Snitzere83068a2016-05-24 21:16:51 -0400225 INIT_WORK(&m->process_queued_bios, process_queued_bios);
226 /*
227 * bio-based doesn't support any direct scsi_dh management;
228 * it just discovers if a scsi_dh is attached.
229 */
230 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
231 }
232
233 dm_table_set_type(ti->table, m->queue_mode);
234
235 return 0;
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static void free_multipath(struct multipath *m)
239{
240 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
243 list_del(&pg->list);
244 free_priority_group(pg, m->ti);
245 }
246
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700247 kfree(m->hw_handler_name);
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700248 kfree(m->hw_handler_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 kfree(m);
250}
251
Mike Snitzer2eff1922016-02-03 09:13:14 -0500252static struct dm_mpath_io *get_mpio(union map_info *info)
253{
254 return info->ptr;
255}
256
Mike Snitzerbf661be2016-05-24 15:48:08 -0400257static size_t multipath_per_bio_data_size(void)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400258{
Mike Snitzerbf661be2016-05-24 15:48:08 -0400259 return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400260}
261
Mike Snitzerbf661be2016-05-24 15:48:08 -0400262static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
263{
264 return dm_per_bio_data(bio, multipath_per_bio_data_size());
265}
266
267static struct dm_bio_details *get_bio_details_from_bio(struct bio *bio)
268{
269 /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */
270 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
271 void *bio_details = mpio + 1;
272
273 return bio_details;
274}
275
276static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p,
277 struct dm_bio_details **bio_details_p)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400278{
279 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
Mike Snitzerbf661be2016-05-24 15:48:08 -0400280 struct dm_bio_details *bio_details = get_bio_details_from_bio(bio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400281
282 memset(mpio, 0, sizeof(*mpio));
Mike Snitzerbf661be2016-05-24 15:48:08 -0400283 memset(bio_details, 0, sizeof(*bio_details));
284 dm_bio_record(bio_details, bio);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400285
Mike Snitzerbf661be2016-05-24 15:48:08 -0400286 if (mpio_p)
287 *mpio_p = mpio;
288 if (bio_details_p)
289 *bio_details_p = bio_details;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*-----------------------------------------------
293 * Path selection
294 *-----------------------------------------------*/
295
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100296static int __pg_init_all_paths(struct multipath *m)
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000297{
298 struct pgpath *pgpath;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000299 unsigned long pg_init_delay = 0;
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000300
Bart Van Asscheb1946792017-04-27 10:11:22 -0700301 lockdep_assert_held(&m->lock);
302
Mike Snitzer91e968a2016-03-17 17:10:15 -0400303 if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100304 return 0;
Hannes Reinecke17f4ff42014-02-28 15:33:42 +0100305
Mike Snitzer91e968a2016-03-17 17:10:15 -0400306 atomic_inc(&m->pg_init_count);
Mike Snitzer518257b2016-03-17 16:32:10 -0400307 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100308
309 /* Check here to reset pg_init_required */
310 if (!m->current_pg)
311 return 0;
312
Mike Snitzer518257b2016-03-17 16:32:10 -0400313 if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000314 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
315 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000316 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
317 /* Skip failed paths */
318 if (!pgpath->is_active)
319 continue;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000320 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
321 pg_init_delay))
Mike Snitzer91e968a2016-03-17 17:10:15 -0400322 atomic_inc(&m->pg_init_in_progress);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000323 }
Mike Snitzer91e968a2016-03-17 17:10:15 -0400324 return atomic_read(&m->pg_init_in_progress);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000325}
326
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700327static int pg_init_all_paths(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700329 int ret;
Mike Snitzer2da16102016-03-17 18:38:17 -0400330 unsigned long flags;
331
332 spin_lock_irqsave(&m->lock, flags);
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700333 ret = __pg_init_all_paths(m);
Mike Snitzer2da16102016-03-17 18:38:17 -0400334 spin_unlock_irqrestore(&m->lock, flags);
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700335
336 return ret;
Mike Snitzer2da16102016-03-17 18:38:17 -0400337}
338
339static void __switch_pg(struct multipath *m, struct priority_group *pg)
340{
341 m->current_pg = pg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700344 if (m->hw_handler_name) {
Mike Snitzer518257b2016-03-17 16:32:10 -0400345 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
346 set_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 } else {
Mike Snitzer518257b2016-03-17 16:32:10 -0400348 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
349 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100351
Mike Snitzer91e968a2016-03-17 17:10:15 -0400352 atomic_set(&m->pg_init_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Mike Snitzer2da16102016-03-17 18:38:17 -0400355static struct pgpath *choose_path_in_pg(struct multipath *m,
356 struct priority_group *pg,
357 size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Mike Snitzer2da16102016-03-17 18:38:17 -0400359 unsigned long flags;
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800360 struct dm_path *path;
Mike Snitzer2da16102016-03-17 18:38:17 -0400361 struct pgpath *pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Mike Snitzer90a43232016-02-17 21:29:17 -0500363 path = pg->ps.type->select_path(&pg->ps, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (!path)
Mike Snitzer2da16102016-03-17 18:38:17 -0400365 return ERR_PTR(-ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Mike Snitzer2da16102016-03-17 18:38:17 -0400367 pgpath = path_to_pgpath(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Mike Snitzer2da16102016-03-17 18:38:17 -0400369 if (unlikely(lockless_dereference(m->current_pg) != pg)) {
370 /* Only update current_pgpath if pg changed */
371 spin_lock_irqsave(&m->lock, flags);
372 m->current_pgpath = pgpath;
373 __switch_pg(m, pg);
374 spin_unlock_irqrestore(&m->lock, flags);
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Mike Snitzer2da16102016-03-17 18:38:17 -0400377 return pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Mike Snitzer2da16102016-03-17 18:38:17 -0400380static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Mike Snitzer2da16102016-03-17 18:38:17 -0400382 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct priority_group *pg;
Mike Snitzer2da16102016-03-17 18:38:17 -0400384 struct pgpath *pgpath;
Mike Snitzerd19a55c2017-01-06 15:33:14 -0500385 unsigned bypassed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Mike Snitzer91e968a2016-03-17 17:10:15 -0400387 if (!atomic_read(&m->nr_valid_paths)) {
Mike Snitzer518257b2016-03-17 16:32:10 -0400388 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 goto failed;
Benjamin Marzinski1f271972014-08-13 13:53:42 -0500390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /* Were we instructed to switch PG? */
Mike Snitzer2da16102016-03-17 18:38:17 -0400393 if (lockless_dereference(m->next_pg)) {
394 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 pg = m->next_pg;
Mike Snitzer2da16102016-03-17 18:38:17 -0400396 if (!pg) {
397 spin_unlock_irqrestore(&m->lock, flags);
398 goto check_current_pg;
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 m->next_pg = NULL;
Mike Snitzer2da16102016-03-17 18:38:17 -0400401 spin_unlock_irqrestore(&m->lock, flags);
402 pgpath = choose_path_in_pg(m, pg, nr_bytes);
403 if (!IS_ERR_OR_NULL(pgpath))
404 return pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
406
407 /* Don't change PG until it has no remaining paths */
Mike Snitzer2da16102016-03-17 18:38:17 -0400408check_current_pg:
409 pg = lockless_dereference(m->current_pg);
410 if (pg) {
411 pgpath = choose_path_in_pg(m, pg, nr_bytes);
412 if (!IS_ERR_OR_NULL(pgpath))
413 return pgpath;
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /*
417 * Loop through priority groups until we find a valid path.
418 * First time we skip PGs marked 'bypassed'.
Mike Christief220fd42012-06-03 00:29:45 +0100419 * Second time we only try the ones we skipped, but set
420 * pg_init_delay_retry so we do not hammer controllers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 */
422 do {
423 list_for_each_entry(pg, &m->priority_groups, list) {
Mike Snitzerd19a55c2017-01-06 15:33:14 -0500424 if (pg->bypassed == !!bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 continue;
Mike Snitzer2da16102016-03-17 18:38:17 -0400426 pgpath = choose_path_in_pg(m, pg, nr_bytes);
427 if (!IS_ERR_OR_NULL(pgpath)) {
Mike Christief220fd42012-06-03 00:29:45 +0100428 if (!bypassed)
Mike Snitzer518257b2016-03-17 16:32:10 -0400429 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
Mike Snitzer2da16102016-03-17 18:38:17 -0400430 return pgpath;
Mike Christief220fd42012-06-03 00:29:45 +0100431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433 } while (bypassed--);
434
435failed:
Mike Snitzer2da16102016-03-17 18:38:17 -0400436 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 m->current_pgpath = NULL;
438 m->current_pg = NULL;
Mike Snitzer2da16102016-03-17 18:38:17 -0400439 spin_unlock_irqrestore(&m->lock, flags);
440
441 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800444/*
445 * Check whether bios must be queued in the device-mapper core rather
446 * than here in the target.
447 *
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800448 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
449 * same value then we are not between multipath_presuspend()
450 * and multipath_resume() calls and we have no need to check
451 * for the DMF_NOFLUSH_SUSPENDING flag.
452 */
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400453static bool __must_push_back(struct multipath *m)
454{
455 return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) !=
456 test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) &&
457 dm_noflush_suspending(m->ti));
458}
459
460static bool must_push_back_rq(struct multipath *m)
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800461{
Mike Snitzer1814f2e2016-07-25 21:08:51 -0400462 bool r;
463 unsigned long flags;
464
465 spin_lock_irqsave(&m->lock, flags);
466 r = (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) ||
467 __must_push_back(m));
468 spin_unlock_irqrestore(&m->lock, flags);
469
470 return r;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400471}
472
473static bool must_push_back_bio(struct multipath *m)
474{
Mike Snitzer1814f2e2016-07-25 21:08:51 -0400475 bool r;
476 unsigned long flags;
477
478 spin_lock_irqsave(&m->lock, flags);
479 r = __must_push_back(m);
480 spin_unlock_irqrestore(&m->lock, flags);
481
482 return r;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800483}
484
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100485/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400486 * Map cloned requests (request-based multipath)
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100487 */
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100488static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
489 union map_info *map_context,
490 struct request **__clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Mike Snitzer7943bd62016-02-02 21:53:15 -0500492 struct multipath *m = ti->private;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100493 size_t nr_bytes = blk_rq_bytes(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100495 struct block_device *bdev;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100496 struct dm_mpath_io *mpio = get_mpio(map_context);
Bart Van Assche7083abb2017-04-27 10:11:15 -0700497 struct request_queue *q;
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100498 struct request *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* Do we need to select a new pgpath? */
Mike Snitzer2da16102016-03-17 18:38:17 -0400501 pgpath = lockless_dereference(m->current_pgpath);
502 if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
503 pgpath = choose_pgpath(m, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100505 if (!pgpath) {
Mike Snitzerb88efd42016-09-09 19:26:19 -0400506 if (must_push_back_rq(m))
507 return DM_MAPIO_DELAY_REQUEUE;
508 return -EIO; /* Failed */
Mike Snitzer518257b2016-03-17 16:32:10 -0400509 } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
510 test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
Bart Van Asschec1d7ecf2017-04-27 10:11:16 -0700511 if (pg_init_all_paths(m))
512 return DM_MAPIO_DELAY_REQUEUE;
Bart Van Assche06eb0612017-04-07 16:50:44 -0700513 return DM_MAPIO_REQUEUE;
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100514 }
Mike Snitzer6afbc012014-07-08 11:55:09 -0400515
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100516 memset(mpio, 0, sizeof(*mpio));
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100517 mpio->pgpath = pgpath;
518 mpio->nr_bytes = nr_bytes;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600519
520 bdev = pgpath->path.dev->bdev;
Bart Van Assche7083abb2017-04-27 10:11:15 -0700521 q = bdev_get_queue(bdev);
522 clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE, GFP_ATOMIC);
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100523 if (IS_ERR(clone)) {
524 /* EBUSY, ENODEV or EWOULDBLOCK: requeue */
Bart Van Assche7083abb2017-04-27 10:11:15 -0700525 bool queue_dying = blk_queue_dying(q);
526 DMERR_LIMIT("blk_get_request() returned %ld%s - requeuing",
527 PTR_ERR(clone), queue_dying ? " (path offline)" : "");
528 if (queue_dying) {
529 atomic_inc(&m->pg_init_in_progress);
530 activate_or_offline_path(pgpath);
531 return DM_MAPIO_REQUEUE;
532 }
Bart Van Assche06eb0612017-04-07 16:50:44 -0700533 return DM_MAPIO_DELAY_REQUEUE;
Mike Snitzere5863d92014-12-17 21:08:12 -0500534 }
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100535 clone->bio = clone->biotail = NULL;
536 clone->rq_disk = bdev->bd_disk;
537 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
538 *__clone = clone;
Mike Snitzere5863d92014-12-17 21:08:12 -0500539
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100540 if (pgpath->pg->ps.type->start_io)
541 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
542 &pgpath->path,
543 nr_bytes);
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600544 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Mike Snitzere5863d92014-12-17 21:08:12 -0500547static void multipath_release_clone(struct request *clone)
548{
Christoph Hellwigeb8db832017-01-22 18:32:46 +0100549 blk_put_request(clone);
Mike Snitzere5863d92014-12-17 21:08:12 -0500550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/*
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400553 * Map cloned bios (bio-based multipath)
554 */
555static int __multipath_map_bio(struct multipath *m, struct bio *bio, struct dm_mpath_io *mpio)
556{
557 size_t nr_bytes = bio->bi_iter.bi_size;
558 struct pgpath *pgpath;
559 unsigned long flags;
560 bool queue_io;
561
562 /* Do we need to select a new pgpath? */
563 pgpath = lockless_dereference(m->current_pgpath);
564 queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
565 if (!pgpath || !queue_io)
566 pgpath = choose_pgpath(m, nr_bytes);
567
568 if ((pgpath && queue_io) ||
569 (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
570 /* Queue for the daemon to resubmit */
571 spin_lock_irqsave(&m->lock, flags);
572 bio_list_add(&m->queued_bios, bio);
573 spin_unlock_irqrestore(&m->lock, flags);
574 /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
575 if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
576 pg_init_all_paths(m);
577 else if (!queue_io)
578 queue_work(kmultipathd, &m->process_queued_bios);
579 return DM_MAPIO_SUBMITTED;
580 }
581
582 if (!pgpath) {
583 if (!must_push_back_bio(m))
584 return -EIO;
585 return DM_MAPIO_REQUEUE;
586 }
587
588 mpio->pgpath = pgpath;
589 mpio->nr_bytes = nr_bytes;
590
591 bio->bi_error = 0;
592 bio->bi_bdev = pgpath->path.dev->bdev;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600593 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400594
595 if (pgpath->pg->ps.type->start_io)
596 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
597 &pgpath->path,
598 nr_bytes);
599 return DM_MAPIO_REMAPPED;
600}
601
602static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
603{
604 struct multipath *m = ti->private;
Mike Snitzerbf661be2016-05-24 15:48:08 -0400605 struct dm_mpath_io *mpio = NULL;
606
607 multipath_init_per_bio_data(bio, &mpio, NULL);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400608
609 return __multipath_map_bio(m, bio, mpio);
610}
611
Mike Snitzer7e48c762016-09-14 10:47:03 -0400612static void process_queued_io_list(struct multipath *m)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400613{
Mike Snitzer7e48c762016-09-14 10:47:03 -0400614 if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED)
615 dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
616 else if (m->queue_mode == DM_TYPE_BIO_BASED)
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400617 queue_work(kmultipathd, &m->process_queued_bios);
618}
619
620static void process_queued_bios(struct work_struct *work)
621{
622 int r;
623 unsigned long flags;
624 struct bio *bio;
625 struct bio_list bios;
626 struct blk_plug plug;
627 struct multipath *m =
628 container_of(work, struct multipath, process_queued_bios);
629
630 bio_list_init(&bios);
631
632 spin_lock_irqsave(&m->lock, flags);
633
634 if (bio_list_empty(&m->queued_bios)) {
635 spin_unlock_irqrestore(&m->lock, flags);
636 return;
637 }
638
639 bio_list_merge(&bios, &m->queued_bios);
640 bio_list_init(&m->queued_bios);
641
642 spin_unlock_irqrestore(&m->lock, flags);
643
644 blk_start_plug(&plug);
645 while ((bio = bio_list_pop(&bios))) {
646 r = __multipath_map_bio(m, bio, get_mpio_from_bio(bio));
647 if (r < 0 || r == DM_MAPIO_REQUEUE) {
648 bio->bi_error = r;
649 bio_endio(bio);
650 } else if (r == DM_MAPIO_REMAPPED)
651 generic_make_request(bio);
652 }
653 blk_finish_plug(&plug);
654}
655
656/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * If we run out of usable paths, should we queue I/O or error it?
658 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -0500659static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
660 bool save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 unsigned long flags;
663
664 spin_lock_irqsave(&m->lock, flags);
665
Mike Snitzer518257b2016-03-17 16:32:10 -0400666 if (save_old_value) {
667 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
668 set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
669 else
670 clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
671 } else {
672 if (queue_if_no_path)
673 set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
674 else
675 clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
676 }
677 if (queue_if_no_path)
678 set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700679 else
Mike Snitzer518257b2016-03-17 16:32:10 -0400680 clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 spin_unlock_irqrestore(&m->lock, flags);
683
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400684 if (!queue_if_no_path) {
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200685 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -0400686 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400687 }
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690}
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692/*
693 * An event is triggered whenever a path is taken out of use.
694 * Includes path failure and PG bypass.
695 */
David Howellsc4028952006-11-22 14:57:56 +0000696static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
David Howellsc4028952006-11-22 14:57:56 +0000698 struct multipath *m =
699 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 dm_table_event(m->ti->table);
702}
703
704/*-----------------------------------------------------------------
705 * Constructor/argument parsing:
706 * <#multipath feature args> [<arg>]*
707 * <#hw_handler args> [hw_handler [<arg>]*]
708 * <#priority groups>
709 * <initial priority group>
710 * [<selector> <#selector args> [<arg>]*
711 * <#paths> <#per-path selector args>
712 * [<path> [<arg>]* ]+ ]+
713 *---------------------------------------------------------------*/
Mike Snitzer498f0102011-08-02 12:32:04 +0100714static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct dm_target *ti)
716{
717 int r;
718 struct path_selector_type *pst;
719 unsigned ps_argc;
720
Mike Snitzer498f0102011-08-02 12:32:04 +0100721 static struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700722 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 };
724
Mike Snitzer498f0102011-08-02 12:32:04 +0100725 pst = dm_get_path_selector(dm_shift_arg(as));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700727 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return -EINVAL;
729 }
730
Mike Snitzer498f0102011-08-02 12:32:04 +0100731 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100732 if (r) {
733 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 r = pst->create(&pg->ps, ps_argc, as->argv);
738 if (r) {
739 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700740 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return r;
742 }
743
744 pg->ps.type = pst;
Mike Snitzer498f0102011-08-02 12:32:04 +0100745 dm_consume_args(as, ps_argc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 return 0;
748}
749
Mike Snitzer498f0102011-08-02 12:32:04 +0100750static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 struct dm_target *ti)
752{
753 int r;
754 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700755 struct multipath *m = ti->private;
Mike Snitzera58a9352012-07-27 15:08:04 +0100756 struct request_queue *q = NULL;
757 const char *attached_handler_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /* we need at least a path arg */
760 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700761 ti->error = "no device given";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100762 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764
765 p = alloc_pgpath();
766 if (!p)
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100767 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Mike Snitzer498f0102011-08-02 12:32:04 +0100769 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +0000770 &p->path.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700772 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 goto bad;
774 }
775
Mike Snitzer518257b2016-03-17 16:32:10 -0400776 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) || m->hw_handler_name)
Mike Snitzera58a9352012-07-27 15:08:04 +0100777 q = bdev_get_queue(p->path.dev->bdev);
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100778
Mike Snitzer518257b2016-03-17 16:32:10 -0400779 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200780retain:
Mike Snitzera58a9352012-07-27 15:08:04 +0100781 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
782 if (attached_handler_name) {
783 /*
tang.junhui54cd6402016-11-24 15:11:48 +0800784 * Clear any hw_handler_params associated with a
785 * handler that isn't already attached.
786 */
787 if (m->hw_handler_name && strcmp(attached_handler_name, m->hw_handler_name)) {
788 kfree(m->hw_handler_params);
789 m->hw_handler_params = NULL;
790 }
791
792 /*
Mike Snitzera58a9352012-07-27 15:08:04 +0100793 * Reset hw_handler_name to match the attached handler
Mike Snitzera58a9352012-07-27 15:08:04 +0100794 *
795 * NB. This modifies the table line to show the actual
796 * handler instead of the original table passed in.
797 */
798 kfree(m->hw_handler_name);
799 m->hw_handler_name = attached_handler_name;
Mike Snitzera58a9352012-07-27 15:08:04 +0100800 }
801 }
802
803 if (m->hw_handler_name) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100804 r = scsi_dh_attach(q, m->hw_handler_name);
805 if (r == -EBUSY) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200806 char b[BDEVNAME_SIZE];
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100807
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200808 printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
809 bdevname(p->path.dev->bdev, b));
810 goto retain;
811 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700812 if (r < 0) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100813 ti->error = "error attaching hardware handler";
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700814 dm_put_device(ti, p->path.dev);
815 goto bad;
816 }
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700817
818 if (m->hw_handler_params) {
819 r = scsi_dh_set_params(q, m->hw_handler_params);
820 if (r < 0) {
821 ti->error = "unable to set hardware "
822 "handler parameters";
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700823 dm_put_device(ti, p->path.dev);
824 goto bad;
825 }
826 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700827 }
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
830 if (r) {
831 dm_put_device(ti, p->path.dev);
832 goto bad;
833 }
834
835 return p;
836
837 bad:
838 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100839 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Mike Snitzer498f0102011-08-02 12:32:04 +0100842static struct priority_group *parse_priority_group(struct dm_arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700843 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Mike Snitzer498f0102011-08-02 12:32:04 +0100845 static struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700846 {1, 1024, "invalid number of paths"},
847 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 };
849
850 int r;
Mike Snitzer498f0102011-08-02 12:32:04 +0100851 unsigned i, nr_selector_args, nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700853 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 if (as->argc < 2) {
856 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100857 ti->error = "not enough priority group arguments";
858 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
861 pg = alloc_priority_group();
862 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700863 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100864 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 pg->m = m;
867
868 r = parse_path_selector(as, pg, ti);
869 if (r)
870 goto bad;
871
872 /*
873 * read the paths
874 */
Mike Snitzer498f0102011-08-02 12:32:04 +0100875 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (r)
877 goto bad;
878
Mike Snitzer498f0102011-08-02 12:32:04 +0100879 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (r)
881 goto bad;
882
Mike Snitzer498f0102011-08-02 12:32:04 +0100883 nr_args = 1 + nr_selector_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 for (i = 0; i < pg->nr_pgpaths; i++) {
885 struct pgpath *pgpath;
Mike Snitzer498f0102011-08-02 12:32:04 +0100886 struct dm_arg_set path_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Mike Snitzer498f0102011-08-02 12:32:04 +0100888 if (as->argc < nr_args) {
Mikulas Patocka148acff2008-07-21 12:00:30 +0100889 ti->error = "not enough path parameters";
Alasdair G Kergon6bbf79a2010-08-12 04:13:49 +0100890 r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Mike Snitzer498f0102011-08-02 12:32:04 +0100894 path_args.argc = nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 path_args.argv = as->argv;
896
897 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100898 if (IS_ERR(pgpath)) {
899 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 pgpath->pg = pg;
904 list_add_tail(&pgpath->list, &pg->pgpaths);
Mike Snitzer498f0102011-08-02 12:32:04 +0100905 dm_consume_args(as, nr_args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907
908 return pg;
909
910 bad:
911 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100912 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Mike Snitzer498f0102011-08-02 12:32:04 +0100915static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 unsigned hw_argc;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700918 int ret;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700919 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Mike Snitzer498f0102011-08-02 12:32:04 +0100921 static struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700922 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 };
924
Mike Snitzer498f0102011-08-02 12:32:04 +0100925 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return -EINVAL;
927
928 if (!hw_argc)
929 return 0;
930
Mike Snitzere83068a2016-05-24 21:16:51 -0400931 if (m->queue_mode == DM_TYPE_BIO_BASED) {
Mike Snitzer76e33fe2016-05-19 16:15:14 -0400932 dm_consume_args(as, hw_argc);
933 DMERR("bio-based multipath doesn't allow hardware handler args");
934 return 0;
935 }
936
Mike Snitzer498f0102011-08-02 12:32:04 +0100937 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
tang.junhuif97dc422016-10-28 17:04:46 +0800938 if (!m->hw_handler_name)
939 return -EINVAL;
Chandra Seetharaman14e98c52008-11-13 23:39:06 +0000940
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700941 if (hw_argc > 1) {
942 char *p;
943 int i, j, len = 4;
944
945 for (i = 0; i <= hw_argc - 2; i++)
946 len += strlen(as->argv[i]) + 1;
947 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
948 if (!p) {
949 ti->error = "memory allocation failed";
950 ret = -ENOMEM;
951 goto fail;
952 }
953 j = sprintf(p, "%d", hw_argc - 1);
954 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
955 j = sprintf(p, "%s", as->argv[i]);
956 }
Mike Snitzer498f0102011-08-02 12:32:04 +0100957 dm_consume_args(as, hw_argc - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 return 0;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700960fail:
961 kfree(m->hw_handler_name);
962 m->hw_handler_name = NULL;
963 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Mike Snitzer498f0102011-08-02 12:32:04 +0100966static int parse_features(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 int r;
969 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700970 struct dm_target *ti = m->ti;
Mike Snitzer498f0102011-08-02 12:32:04 +0100971 const char *arg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Mike Snitzer498f0102011-08-02 12:32:04 +0100973 static struct dm_arg _args[] = {
Mike Snitzere83068a2016-05-24 21:16:51 -0400974 {0, 8, "invalid number of feature args"},
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100975 {1, 50, "pg_init_retries must be between 1 and 50"},
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000976 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 };
978
Mike Snitzer498f0102011-08-02 12:32:04 +0100979 r = dm_read_arg_group(_args, as, &argc, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (r)
981 return -EINVAL;
982
983 if (!argc)
984 return 0;
985
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100986 do {
Mike Snitzer498f0102011-08-02 12:32:04 +0100987 arg_name = dm_shift_arg(as);
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100988 argc--;
989
Mike Snitzer498f0102011-08-02 12:32:04 +0100990 if (!strcasecmp(arg_name, "queue_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -0500991 r = queue_if_no_path(m, true, false);
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100992 continue;
993 }
994
Mike Snitzera58a9352012-07-27 15:08:04 +0100995 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
Mike Snitzer518257b2016-03-17 16:32:10 -0400996 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
Mike Snitzera58a9352012-07-27 15:08:04 +0100997 continue;
998 }
999
Mike Snitzer498f0102011-08-02 12:32:04 +01001000 if (!strcasecmp(arg_name, "pg_init_retries") &&
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001001 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001002 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001003 argc--;
1004 continue;
1005 }
1006
Mike Snitzer498f0102011-08-02 12:32:04 +01001007 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001008 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001009 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001010 argc--;
1011 continue;
1012 }
1013
Mike Snitzere83068a2016-05-24 21:16:51 -04001014 if (!strcasecmp(arg_name, "queue_mode") &&
1015 (argc >= 1)) {
1016 const char *queue_mode_name = dm_shift_arg(as);
1017
1018 if (!strcasecmp(queue_mode_name, "bio"))
1019 m->queue_mode = DM_TYPE_BIO_BASED;
1020 else if (!strcasecmp(queue_mode_name, "rq"))
1021 m->queue_mode = DM_TYPE_REQUEST_BASED;
1022 else if (!strcasecmp(queue_mode_name, "mq"))
1023 m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
1024 else {
1025 ti->error = "Unknown 'queue_mode' requested";
1026 r = -EINVAL;
1027 }
1028 argc--;
1029 continue;
1030 }
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001033 r = -EINVAL;
1034 } while (argc && !r);
1035
1036 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Mike Snitzere83068a2016-05-24 21:16:51 -04001039static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
Mike Snitzer498f0102011-08-02 12:32:04 +01001041 /* target arguments */
1042 static struct dm_arg _args[] = {
Mike Snitzera490a072011-03-24 13:54:33 +00001043 {0, 1024, "invalid number of priority groups"},
1044 {0, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 };
1046
1047 int r;
1048 struct multipath *m;
Mike Snitzer498f0102011-08-02 12:32:04 +01001049 struct dm_arg_set as;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 unsigned pg_count = 0;
1051 unsigned next_pg_num;
1052
1053 as.argc = argc;
1054 as.argv = argv;
1055
Mike Snitzere83068a2016-05-24 21:16:51 -04001056 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001058 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return -EINVAL;
1060 }
1061
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001062 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (r)
1064 goto bad;
1065
Mike Snitzere83068a2016-05-24 21:16:51 -04001066 r = alloc_multipath_stage2(ti, m);
1067 if (r)
1068 goto bad;
1069
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001070 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (r)
1072 goto bad;
1073
Mike Snitzer498f0102011-08-02 12:32:04 +01001074 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (r)
1076 goto bad;
1077
Mike Snitzer498f0102011-08-02 12:32:04 +01001078 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (r)
1080 goto bad;
1081
Mike Snitzera490a072011-03-24 13:54:33 +00001082 if ((!m->nr_priority_groups && next_pg_num) ||
1083 (m->nr_priority_groups && !next_pg_num)) {
1084 ti->error = "invalid initial priority group";
1085 r = -EINVAL;
1086 goto bad;
1087 }
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /* parse the priority groups */
1090 while (as.argc) {
1091 struct priority_group *pg;
Mike Snitzer91e968a2016-03-17 17:10:15 -04001092 unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -07001094 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +01001095 if (IS_ERR(pg)) {
1096 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 goto bad;
1098 }
1099
Mike Snitzer91e968a2016-03-17 17:10:15 -04001100 nr_valid_paths += pg->nr_pgpaths;
1101 atomic_set(&m->nr_valid_paths, nr_valid_paths);
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 list_add_tail(&pg->list, &m->priority_groups);
1104 pg_count++;
1105 pg->pg_num = pg_count;
1106 if (!--next_pg_num)
1107 m->next_pg = pg;
1108 }
1109
1110 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001111 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 r = -EINVAL;
1113 goto bad;
1114 }
1115
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001116 ti->num_flush_bios = 1;
1117 ti->num_discard_bios = 1;
Mike Snitzer042bcef2013-05-10 14:37:16 +01001118 ti->num_write_same_bios = 1;
Mike Snitzere83068a2016-05-24 21:16:51 -04001119 if (m->queue_mode == DM_TYPE_BIO_BASED)
Mike Snitzerbf661be2016-05-24 15:48:08 -04001120 ti->per_io_data_size = multipath_per_bio_data_size();
Christoph Hellwigeb8db832017-01-22 18:32:46 +01001121 else
Mike Snitzer8637a6b2016-01-31 12:08:36 -05001122 ti->per_io_data_size = sizeof(struct dm_mpath_io);
Mikulas Patocka86279212009-06-22 10:12:24 +01001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return 0;
1125
1126 bad:
1127 free_multipath(m);
1128 return r;
1129}
1130
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001131static void multipath_wait_for_pg_init_completion(struct multipath *m)
1132{
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001133 DEFINE_WAIT(wait);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001134
1135 while (1) {
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001136 prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001137
Mike Snitzer91e968a2016-03-17 17:10:15 -04001138 if (!atomic_read(&m->pg_init_in_progress))
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001139 break;
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001140
1141 io_schedule();
1142 }
Bart Van Assche9f4c3f82016-08-31 15:16:43 -07001143 finish_wait(&m->pg_init_wait, &wait);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001144}
1145
1146static void flush_multipath_work(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Mike Snitzer518257b2016-03-17 16:32:10 -04001148 set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1149 smp_mb__after_atomic();
Shiva Krishna Merla954a73d2013-10-30 03:26:38 +00001150
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001151 flush_workqueue(kmpath_handlerd);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001152 multipath_wait_for_pg_init_completion(m);
Alasdair G Kergona044d012005-07-12 15:53:02 -07001153 flush_workqueue(kmultipathd);
Tejun Heo43829732012-08-20 14:51:24 -07001154 flush_work(&m->trigger_event);
Shiva Krishna Merla954a73d2013-10-30 03:26:38 +00001155
Mike Snitzer518257b2016-03-17 16:32:10 -04001156 clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1157 smp_mb__after_atomic();
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001158}
1159
1160static void multipath_dtr(struct dm_target *ti)
1161{
1162 struct multipath *m = ti->private;
1163
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001164 flush_multipath_work(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 free_multipath(m);
1166}
1167
1168/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 * Take a path out of use.
1170 */
1171static int fail_path(struct pgpath *pgpath)
1172{
1173 unsigned long flags;
1174 struct multipath *m = pgpath->pg->m;
1175
1176 spin_lock_irqsave(&m->lock, flags);
1177
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001178 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 goto out;
1180
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001181 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001184 pgpath->is_active = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 pgpath->fail_count++;
1186
Mike Snitzer91e968a2016-03-17 17:10:15 -04001187 atomic_dec(&m->nr_valid_paths);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 if (pgpath == m->current_pgpath)
1190 m->current_pgpath = NULL;
1191
Mike Andersonb15546f2007-10-19 22:48:02 +01001192 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
Mike Snitzer91e968a2016-03-17 17:10:15 -04001193 pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
Mike Andersonb15546f2007-10-19 22:48:02 +01001194
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001195 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
1197out:
1198 spin_unlock_irqrestore(&m->lock, flags);
1199
1200 return 0;
1201}
1202
1203/*
1204 * Reinstate a previously-failed path
1205 */
1206static int reinstate_path(struct pgpath *pgpath)
1207{
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001208 int r = 0, run_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 unsigned long flags;
1210 struct multipath *m = pgpath->pg->m;
Mike Snitzer91e968a2016-03-17 17:10:15 -04001211 unsigned nr_valid_paths;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 spin_lock_irqsave(&m->lock, flags);
1214
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001215 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 goto out;
1217
Mike Snitzerec31f3f2016-02-20 12:49:43 -05001218 DMWARN("Reinstating path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1221 if (r)
1222 goto out;
1223
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001224 pgpath->is_active = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Mike Snitzer91e968a2016-03-17 17:10:15 -04001226 nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
1227 if (nr_valid_paths == 1) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001228 m->current_pgpath = NULL;
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001229 run_queue = 1;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001230 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001231 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
Mike Snitzer91e968a2016-03-17 17:10:15 -04001232 atomic_inc(&m->pg_init_in_progress);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Mike Andersonb15546f2007-10-19 22:48:02 +01001235 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
Mike Snitzer91e968a2016-03-17 17:10:15 -04001236 pgpath->path.dev->name, nr_valid_paths);
Mike Andersonb15546f2007-10-19 22:48:02 +01001237
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001238 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240out:
1241 spin_unlock_irqrestore(&m->lock, flags);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001242 if (run_queue) {
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001243 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -04001244 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 return r;
1248}
1249
1250/*
1251 * Fail or reinstate all paths that match the provided struct dm_dev.
1252 */
1253static int action_dev(struct multipath *m, struct dm_dev *dev,
1254 action_fn action)
1255{
Mike Snitzer19040c02011-03-24 13:54:31 +00001256 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 struct pgpath *pgpath;
1258 struct priority_group *pg;
1259
1260 list_for_each_entry(pg, &m->priority_groups, list) {
1261 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1262 if (pgpath->path.dev == dev)
1263 r = action(pgpath);
1264 }
1265 }
1266
1267 return r;
1268}
1269
1270/*
1271 * Temporarily try to avoid having to use the specified PG
1272 */
1273static void bypass_pg(struct multipath *m, struct priority_group *pg,
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001274 bool bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 unsigned long flags;
1277
1278 spin_lock_irqsave(&m->lock, flags);
1279
1280 pg->bypassed = bypassed;
1281 m->current_pgpath = NULL;
1282 m->current_pg = NULL;
1283
1284 spin_unlock_irqrestore(&m->lock, flags);
1285
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001286 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289/*
1290 * Switch to using the specified PG from the next I/O that gets mapped
1291 */
1292static int switch_pg_num(struct multipath *m, const char *pgstr)
1293{
1294 struct priority_group *pg;
1295 unsigned pgnum;
1296 unsigned long flags;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001297 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001299 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
tang.junhuicc5bd922016-11-04 12:37:09 +08001300 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 DMWARN("invalid PG number supplied to switch_pg_num");
1302 return -EINVAL;
1303 }
1304
1305 spin_lock_irqsave(&m->lock, flags);
1306 list_for_each_entry(pg, &m->priority_groups, list) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001307 pg->bypassed = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (--pgnum)
1309 continue;
1310
1311 m->current_pgpath = NULL;
1312 m->current_pg = NULL;
1313 m->next_pg = pg;
1314 }
1315 spin_unlock_irqrestore(&m->lock, flags);
1316
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001317 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return 0;
1319}
1320
1321/*
1322 * Set/clear bypassed status of a PG.
1323 * PGs are numbered upwards from 1 in the order they were declared.
1324 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001325static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 struct priority_group *pg;
1328 unsigned pgnum;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001329 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001331 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
tang.junhuicc5bd922016-11-04 12:37:09 +08001332 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 DMWARN("invalid PG number supplied to bypass_pg");
1334 return -EINVAL;
1335 }
1336
1337 list_for_each_entry(pg, &m->priority_groups, list) {
1338 if (!--pgnum)
1339 break;
1340 }
1341
1342 bypass_pg(m, pg, bypassed);
1343 return 0;
1344}
1345
1346/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001347 * Should we retry pg_init immediately?
1348 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001349static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001350{
1351 unsigned long flags;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001352 bool limit_reached = false;
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001353
1354 spin_lock_irqsave(&m->lock, flags);
1355
Mike Snitzer91e968a2016-03-17 17:10:15 -04001356 if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
1357 !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
Mike Snitzer518257b2016-03-17 16:32:10 -04001358 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001359 else
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001360 limit_reached = true;
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001361
1362 spin_unlock_irqrestore(&m->lock, flags);
1363
1364 return limit_reached;
1365}
1366
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001367static void pg_init_done(void *data, int errors)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001368{
Moger, Babu83c0d5d2010-03-06 02:29:45 +00001369 struct pgpath *pgpath = data;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001370 struct priority_group *pg = pgpath->pg;
1371 struct multipath *m = pg->m;
1372 unsigned long flags;
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001373 bool delay_retry = false;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001374
1375 /* device or driver problems */
1376 switch (errors) {
1377 case SCSI_DH_OK:
1378 break;
1379 case SCSI_DH_NOSYS:
1380 if (!m->hw_handler_name) {
1381 errors = 0;
1382 break;
1383 }
Moger, Babuf7b934c2010-03-06 02:29:49 +00001384 DMERR("Could not failover the device: Handler scsi_dh_%s "
1385 "Error %d.", m->hw_handler_name, errors);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001386 /*
1387 * Fail path for now, so we do not ping pong
1388 */
1389 fail_path(pgpath);
1390 break;
1391 case SCSI_DH_DEV_TEMP_BUSY:
1392 /*
1393 * Probably doing something like FW upgrade on the
1394 * controller so try the other pg.
1395 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001396 bypass_pg(m, pg, true);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001397 break;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001398 case SCSI_DH_RETRY:
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001399 /* Wait before retrying. */
1400 delay_retry = 1;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001401 case SCSI_DH_IMM_RETRY:
1402 case SCSI_DH_RES_TEMP_UNAVAIL:
1403 if (pg_init_limit_reached(m, pgpath))
1404 fail_path(pgpath);
1405 errors = 0;
1406 break;
Mike Snitzerec31f3f2016-02-20 12:49:43 -05001407 case SCSI_DH_DEV_OFFLINED:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001408 default:
1409 /*
1410 * We probably do not want to fail the path for a device
1411 * error, but this is what the old dm did. In future
1412 * patches we can do more advanced handling.
1413 */
1414 fail_path(pgpath);
1415 }
1416
1417 spin_lock_irqsave(&m->lock, flags);
1418 if (errors) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001419 if (pgpath == m->current_pgpath) {
1420 DMERR("Could not failover device. Error %d.", errors);
1421 m->current_pgpath = NULL;
1422 m->current_pg = NULL;
1423 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001424 } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001425 pg->bypassed = false;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001426
Mike Snitzer91e968a2016-03-17 17:10:15 -04001427 if (atomic_dec_return(&m->pg_init_in_progress) > 0)
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001428 /* Activations of other paths are still on going */
1429 goto out;
1430
Mike Snitzer518257b2016-03-17 16:32:10 -04001431 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1432 if (delay_retry)
1433 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1434 else
1435 clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1436
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001437 if (__pg_init_all_paths(m))
1438 goto out;
1439 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001440 clear_bit(MPATHF_QUEUE_IO, &m->flags);
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001441
Mike Snitzer7e48c762016-09-14 10:47:03 -04001442 process_queued_io_list(m);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001443
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001444 /*
1445 * Wake up any thread waiting to suspend.
1446 */
1447 wake_up(&m->pg_init_wait);
1448
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001449out:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001450 spin_unlock_irqrestore(&m->lock, flags);
1451}
1452
Bart Van Assche89bfce72017-04-27 10:11:14 -07001453static void activate_or_offline_path(struct pgpath *pgpath)
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001454{
Mike Snitzerf10e06b2016-09-01 12:06:37 -04001455 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001456
Mike Snitzerf10e06b2016-09-01 12:06:37 -04001457 if (pgpath->is_active && !blk_queue_dying(q))
1458 scsi_dh_activate(q, pg_init_done, pgpath);
Hannes Reinecke3a017502014-02-28 15:33:49 +01001459 else
1460 pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001461}
1462
Bart Van Assche89bfce72017-04-27 10:11:14 -07001463static void activate_path_work(struct work_struct *work)
1464{
1465 struct pgpath *pgpath =
1466 container_of(work, struct pgpath, activate_path.work);
1467
1468 activate_or_offline_path(pgpath);
1469}
1470
Hannes Reinecke7e782af2013-07-01 15:16:26 +02001471static int noretry_error(int error)
1472{
1473 switch (error) {
Hannes Reinecke8ff232c2015-07-15 13:23:24 +02001474 case -EBADE:
1475 /*
1476 * EBADE signals an reservation conflict.
1477 * We shouldn't fail the path here as we can communicate with
1478 * the target. We should failover to the next path, but in
1479 * doing so we might be causing a ping-pong between paths.
1480 * So just return the reservation conflict error.
1481 */
Hannes Reinecke7e782af2013-07-01 15:16:26 +02001482 case -EOPNOTSUPP:
1483 case -EREMOTEIO:
1484 case -EILSEQ:
1485 case -ENODATA:
Jun'ichi Nomuracc9d3c32013-09-13 14:54:30 +09001486 case -ENOSPC:
Hannes Reinecke7e782af2013-07-01 15:16:26 +02001487 return 1;
1488 }
1489
1490 /* Anything else could be a path failure, so should be retried */
1491 return 0;
1492}
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494/*
1495 * end_io handling
1496 */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001497static int do_end_io(struct multipath *m, struct request *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001498 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001500 /*
1501 * We don't queue any clone request inside the multipath target
1502 * during end I/O handling, since those clone requests don't have
1503 * bio clones. If we queue them inside the multipath target,
1504 * we need to make bio clones, that requires memory allocation.
Mike Snitzer4cc96132016-05-12 16:28:10 -04001505 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001506 * don't have bio clones.)
1507 * Instead of queueing the clone request here, we queue the original
1508 * request into dm core, which will remake a clone request and
1509 * clone bios for it and resubmit it later.
1510 */
1511 int r = DM_ENDIO_REQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001513 if (!error && !clone->errors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return 0; /* I/O complete */
1515
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001516 if (noretry_error(error))
Mike Snitzer959eb4e2010-08-12 04:14:32 +01001517 return error;
1518
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001519 if (mpio->pgpath)
1520 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Mike Snitzer91e968a2016-03-17 17:10:15 -04001522 if (!atomic_read(&m->nr_valid_paths)) {
Mike Snitzer518257b2016-03-17 16:32:10 -04001523 if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001524 if (!must_push_back_rq(m))
Hannes Reinecke751b2a72011-01-18 10:13:12 +01001525 r = -EIO;
Hannes Reinecke751b2a72011-01-18 10:13:12 +01001526 }
1527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001529 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001532static int multipath_end_io(struct dm_target *ti, struct request *clone,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 int error, union map_info *map_context)
1534{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001535 struct multipath *m = ti->private;
Mike Snitzer2eff1922016-02-03 09:13:14 -05001536 struct dm_mpath_io *mpio = get_mpio(map_context);
Wei Yongjuna71a2612012-10-12 16:59:42 +01001537 struct pgpath *pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 struct path_selector *ps;
1539 int r;
1540
Jun'ichi Nomura466891f2012-03-28 18:41:25 +01001541 BUG_ON(!mpio);
1542
Mike Snitzer2eff1922016-02-03 09:13:14 -05001543 r = do_end_io(m, clone, error, mpio);
Wei Yongjuna71a2612012-10-12 16:59:42 +01001544 pgpath = mpio->pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (pgpath) {
1546 ps = &pgpath->pg->ps;
1547 if (ps->type->end_io)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001548 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 return r;
1552}
1553
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001554static int do_end_io_bio(struct multipath *m, struct bio *clone,
1555 int error, struct dm_mpath_io *mpio)
1556{
1557 unsigned long flags;
1558
1559 if (!error)
1560 return 0; /* I/O complete */
1561
1562 if (noretry_error(error))
1563 return error;
1564
1565 if (mpio->pgpath)
1566 fail_path(mpio->pgpath);
1567
1568 if (!atomic_read(&m->nr_valid_paths)) {
1569 if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
1570 if (!must_push_back_bio(m))
1571 return -EIO;
1572 return DM_ENDIO_REQUEUE;
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001573 }
1574 }
1575
1576 /* Queue for the daemon to resubmit */
Mike Snitzerbf661be2016-05-24 15:48:08 -04001577 dm_bio_restore(get_bio_details_from_bio(clone), clone);
Mike Snitzer76e33fe2016-05-19 16:15:14 -04001578
1579 spin_lock_irqsave(&m->lock, flags);
1580 bio_list_add(&m->queued_bios, clone);
1581 spin_unlock_irqrestore(&m->lock, flags);
1582 if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
1583 queue_work(kmultipathd, &m->process_queued_bios);
1584
1585 return DM_ENDIO_INCOMPLETE;
1586}
1587
1588static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone, int error)
1589{
1590 struct multipath *m = ti->private;
1591 struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
1592 struct pgpath *pgpath;
1593 struct path_selector *ps;
1594 int r;
1595
1596 BUG_ON(!mpio);
1597
1598 r = do_end_io_bio(m, clone, error, mpio);
1599 pgpath = mpio->pgpath;
1600 if (pgpath) {
1601 ps = &pgpath->pg->ps;
1602 if (ps->type->end_io)
1603 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1604 }
1605
1606 return r;
1607}
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609/*
1610 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001611 * the last path fails we must error any remaining I/O.
1612 * Note that if the freeze_bdev fails while suspending, the
1613 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 */
1615static void multipath_presuspend(struct dm_target *ti)
1616{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001617 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001619 queue_if_no_path(m, false, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001622static void multipath_postsuspend(struct dm_target *ti)
1623{
Mike Anderson6380f262009-12-10 23:52:21 +00001624 struct multipath *m = ti->private;
1625
1626 mutex_lock(&m->work_mutex);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001627 flush_multipath_work(m);
Mike Anderson6380f262009-12-10 23:52:21 +00001628 mutex_unlock(&m->work_mutex);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001629}
1630
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001631/*
1632 * Restore the queue_if_no_path setting.
1633 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634static void multipath_resume(struct dm_target *ti)
1635{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001636 struct multipath *m = ti->private;
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001637 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001639 spin_lock_irqsave(&m->lock, flags);
Mike Snitzer518257b2016-03-17 16:32:10 -04001640 if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags))
1641 set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
1642 else
1643 clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
Mike Snitzer1814f2e2016-07-25 21:08:51 -04001644 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
1647/*
1648 * Info output has the following format:
1649 * num_multipath_feature_args [multipath_feature_args]*
1650 * num_handler_status_args [handler_status_args]*
1651 * num_groups init_group_number
1652 * [A|D|E num_ps_status_args [ps_status_args]*
1653 * num_paths num_selector_args
1654 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1655 *
1656 * Table output has the following format (identical to the constructor string):
1657 * num_feature_args [features_args]*
1658 * num_handler_args hw_handler [hw_handler_args]*
1659 * num_groups init_group_number
1660 * [priority selector-name num_ps_args [ps_args]*
1661 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1662 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001663static void multipath_status(struct dm_target *ti, status_type_t type,
1664 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
1666 int sz = 0;
1667 unsigned long flags;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001668 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 struct priority_group *pg;
1670 struct pgpath *p;
1671 unsigned pg_num;
1672 char state;
1673
1674 spin_lock_irqsave(&m->lock, flags);
1675
1676 /* Features */
1677 if (type == STATUSTYPE_INFO)
Mike Snitzer91e968a2016-03-17 17:10:15 -04001678 DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
1679 atomic_read(&m->pg_init_count));
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001680 else {
Mike Snitzer518257b2016-03-17 16:32:10 -04001681 DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001682 (m->pg_init_retries > 0) * 2 +
Mike Snitzera58a9352012-07-27 15:08:04 +01001683 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
Mike Snitzere83068a2016-05-24 21:16:51 -04001684 test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
1685 (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
1686
Mike Snitzer518257b2016-03-17 16:32:10 -04001687 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001688 DMEMIT("queue_if_no_path ");
1689 if (m->pg_init_retries)
1690 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001691 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1692 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
Mike Snitzer518257b2016-03-17 16:32:10 -04001693 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
Mike Snitzera58a9352012-07-27 15:08:04 +01001694 DMEMIT("retain_attached_hw_handler ");
Mike Snitzere83068a2016-05-24 21:16:51 -04001695 if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
1696 switch(m->queue_mode) {
1697 case DM_TYPE_BIO_BASED:
1698 DMEMIT("queue_mode bio ");
1699 break;
1700 case DM_TYPE_MQ_REQUEST_BASED:
1701 DMEMIT("queue_mode mq ");
1702 break;
1703 }
1704 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001707 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 DMEMIT("0 ");
1709 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001710 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 DMEMIT("%u ", m->nr_priority_groups);
1713
1714 if (m->next_pg)
1715 pg_num = m->next_pg->pg_num;
1716 else if (m->current_pg)
1717 pg_num = m->current_pg->pg_num;
1718 else
Mike Snitzera490a072011-03-24 13:54:33 +00001719 pg_num = (m->nr_priority_groups ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 DMEMIT("%u ", pg_num);
1722
1723 switch (type) {
1724 case STATUSTYPE_INFO:
1725 list_for_each_entry(pg, &m->priority_groups, list) {
1726 if (pg->bypassed)
1727 state = 'D'; /* Disabled */
1728 else if (pg == m->current_pg)
1729 state = 'A'; /* Currently Active */
1730 else
1731 state = 'E'; /* Enabled */
1732
1733 DMEMIT("%c ", state);
1734
1735 if (pg->ps.type->status)
1736 sz += pg->ps.type->status(&pg->ps, NULL, type,
1737 result + sz,
1738 maxlen - sz);
1739 else
1740 DMEMIT("0 ");
1741
1742 DMEMIT("%u %u ", pg->nr_pgpaths,
1743 pg->ps.type->info_args);
1744
1745 list_for_each_entry(p, &pg->pgpaths, list) {
1746 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001747 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 p->fail_count);
1749 if (pg->ps.type->status)
1750 sz += pg->ps.type->status(&pg->ps,
1751 &p->path, type, result + sz,
1752 maxlen - sz);
1753 }
1754 }
1755 break;
1756
1757 case STATUSTYPE_TABLE:
1758 list_for_each_entry(pg, &m->priority_groups, list) {
1759 DMEMIT("%s ", pg->ps.type->name);
1760
1761 if (pg->ps.type->status)
1762 sz += pg->ps.type->status(&pg->ps, NULL, type,
1763 result + sz,
1764 maxlen - sz);
1765 else
1766 DMEMIT("0 ");
1767
1768 DMEMIT("%u %u ", pg->nr_pgpaths,
1769 pg->ps.type->table_args);
1770
1771 list_for_each_entry(p, &pg->pgpaths, list) {
1772 DMEMIT("%s ", p->path.dev->name);
1773 if (pg->ps.type->status)
1774 sz += pg->ps.type->status(&pg->ps,
1775 &p->path, type, result + sz,
1776 maxlen - sz);
1777 }
1778 }
1779 break;
1780 }
1781
1782 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
1785static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1786{
Mike Anderson6380f262009-12-10 23:52:21 +00001787 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 struct dm_dev *dev;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001789 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 action_fn action;
1791
Mike Anderson6380f262009-12-10 23:52:21 +00001792 mutex_lock(&m->work_mutex);
1793
Kiyoshi Uedac2f3d242009-12-10 23:52:27 +00001794 if (dm_suspended(ti)) {
1795 r = -EBUSY;
1796 goto out;
1797 }
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (argc == 1) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001800 if (!strcasecmp(argv[0], "queue_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001801 r = queue_if_no_path(m, true, false);
Mike Anderson6380f262009-12-10 23:52:21 +00001802 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001803 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001804 r = queue_if_no_path(m, false, false);
Mike Anderson6380f262009-12-10 23:52:21 +00001805 goto out;
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808
Mike Anderson6380f262009-12-10 23:52:21 +00001809 if (argc != 2) {
Jose Castilloa356e422014-01-29 17:52:45 +01001810 DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
Mike Anderson6380f262009-12-10 23:52:21 +00001811 goto out;
1812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Mike Snitzer498f0102011-08-02 12:32:04 +01001814 if (!strcasecmp(argv[0], "disable_group")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001815 r = bypass_pg_num(m, argv[1], true);
Mike Anderson6380f262009-12-10 23:52:21 +00001816 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001817 } else if (!strcasecmp(argv[0], "enable_group")) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001818 r = bypass_pg_num(m, argv[1], false);
Mike Anderson6380f262009-12-10 23:52:21 +00001819 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001820 } else if (!strcasecmp(argv[0], "switch_group")) {
Mike Anderson6380f262009-12-10 23:52:21 +00001821 r = switch_pg_num(m, argv[1]);
1822 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001823 } else if (!strcasecmp(argv[0], "reinstate_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 action = reinstate_path;
Mike Snitzer498f0102011-08-02 12:32:04 +01001825 else if (!strcasecmp(argv[0], "fail_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 action = fail_path;
Mike Anderson6380f262009-12-10 23:52:21 +00001827 else {
Jose Castilloa356e422014-01-29 17:52:45 +01001828 DMWARN("Unrecognised multipath message received: %s", argv[0]);
Mike Anderson6380f262009-12-10 23:52:21 +00001829 goto out;
1830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00001832 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001834 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 argv[1]);
Mike Anderson6380f262009-12-10 23:52:21 +00001836 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838
1839 r = action_dev(m, dev, action);
1840
1841 dm_put_device(ti, dev);
1842
Mike Anderson6380f262009-12-10 23:52:21 +00001843out:
1844 mutex_unlock(&m->work_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001848static int multipath_prepare_ioctl(struct dm_target *ti,
1849 struct block_device **bdev, fmode_t *mode)
Milan Broz9af4aa32006-10-03 01:15:20 -07001850{
Mikulas Patocka35991652012-06-03 00:29:58 +01001851 struct multipath *m = ti->private;
Mike Snitzer2da16102016-03-17 18:38:17 -04001852 struct pgpath *current_pgpath;
Mikulas Patocka35991652012-06-03 00:29:58 +01001853 int r;
1854
Mike Snitzer2da16102016-03-17 18:38:17 -04001855 current_pgpath = lockless_dereference(m->current_pgpath);
1856 if (!current_pgpath)
1857 current_pgpath = choose_pgpath(m, 0);
Milan Broz9af4aa32006-10-03 01:15:20 -07001858
Mike Snitzer2da16102016-03-17 18:38:17 -04001859 if (current_pgpath) {
Mike Snitzer518257b2016-03-17 16:32:10 -04001860 if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
Mike Snitzer2da16102016-03-17 18:38:17 -04001861 *bdev = current_pgpath->path.dev->bdev;
1862 *mode = current_pgpath->path.dev->mode;
Junichi Nomura43e43c92015-11-17 09:36:56 +00001863 r = 0;
1864 } else {
1865 /* pg_init has not started or completed */
1866 r = -ENOTCONN;
1867 }
1868 } else {
1869 /* No path is available */
Mike Snitzer518257b2016-03-17 16:32:10 -04001870 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
Junichi Nomura43e43c92015-11-17 09:36:56 +00001871 r = -ENOTCONN;
1872 else
1873 r = -EIO;
Milan Broze90dae12006-10-03 01:15:22 -07001874 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001875
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +00001876 if (r == -ENOTCONN) {
Mike Snitzer2da16102016-03-17 18:38:17 -04001877 if (!lockless_dereference(m->current_pg)) {
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001878 /* Path status changed, redo selection */
Mike Snitzer2da16102016-03-17 18:38:17 -04001879 (void) choose_pgpath(m, 0);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001880 }
Mike Snitzer518257b2016-03-17 16:32:10 -04001881 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
Mike Snitzer2da16102016-03-17 18:38:17 -04001882 pg_init_all_paths(m);
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001883 dm_table_run_md_queue_async(m->ti->table);
Mike Snitzer7e48c762016-09-14 10:47:03 -04001884 process_queued_io_list(m);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001885 }
Mikulas Patocka35991652012-06-03 00:29:58 +01001886
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001887 /*
1888 * Only pass ioctls through if the device sizes match exactly.
1889 */
1890 if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1891 return 1;
1892 return r;
Milan Broz9af4aa32006-10-03 01:15:20 -07001893}
1894
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001895static int multipath_iterate_devices(struct dm_target *ti,
1896 iterate_devices_callout_fn fn, void *data)
1897{
1898 struct multipath *m = ti->private;
1899 struct priority_group *pg;
1900 struct pgpath *p;
1901 int ret = 0;
1902
1903 list_for_each_entry(pg, &m->priority_groups, list) {
1904 list_for_each_entry(p, &pg->pgpaths, list) {
Mike Snitzer5dea2712009-07-23 20:30:42 +01001905 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001906 if (ret)
1907 goto out;
1908 }
1909 }
1910
1911out:
1912 return ret;
1913}
1914
Mike Snitzer9f54cec2016-02-11 21:42:28 -05001915static int pgpath_busy(struct pgpath *pgpath)
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001916{
1917 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1918
Mike Snitzer52b09912015-02-23 16:36:41 -05001919 return blk_lld_busy(q);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001920}
1921
1922/*
1923 * We return "busy", only when we can map I/Os but underlying devices
1924 * are busy (so even if we map I/Os now, the I/Os will wait on
1925 * the underlying queue).
1926 * In other words, if we want to kill I/Os or queue them inside us
1927 * due to map unavailability, we don't return "busy". Otherwise,
1928 * dm core won't give us the I/Os and we can't do what we want.
1929 */
1930static int multipath_busy(struct dm_target *ti)
1931{
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001932 bool busy = false, has_active = false;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001933 struct multipath *m = ti->private;
Mike Snitzer2da16102016-03-17 18:38:17 -04001934 struct priority_group *pg, *next_pg;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001935 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001936
Mike Snitzerb88efd42016-09-09 19:26:19 -04001937 /* pg_init in progress */
1938 if (atomic_read(&m->pg_init_in_progress))
Mike Snitzer2da16102016-03-17 18:38:17 -04001939 return true;
1940
Mike Snitzerb88efd42016-09-09 19:26:19 -04001941 /* no paths available, for blk-mq: rely on IO mapping to delay requeue */
1942 if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1943 return (m->queue_mode != DM_TYPE_MQ_REQUEST_BASED);
1944
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001945 /* Guess which priority_group will be used at next mapping time */
Mike Snitzer2da16102016-03-17 18:38:17 -04001946 pg = lockless_dereference(m->current_pg);
1947 next_pg = lockless_dereference(m->next_pg);
1948 if (unlikely(!lockless_dereference(m->current_pgpath) && next_pg))
1949 pg = next_pg;
1950
1951 if (!pg) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001952 /*
1953 * We don't know which pg will be used at next mapping time.
Mike Snitzer2da16102016-03-17 18:38:17 -04001954 * We don't call choose_pgpath() here to avoid to trigger
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001955 * pg_init just by busy checking.
1956 * So we don't know whether underlying devices we will be using
1957 * at next mapping time are busy or not. Just try mapping.
1958 */
Mike Snitzer2da16102016-03-17 18:38:17 -04001959 return busy;
1960 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001961
1962 /*
1963 * If there is one non-busy active path at least, the path selector
1964 * will be able to select it. So we consider such a pg as not busy.
1965 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001966 busy = true;
Mike Snitzer2da16102016-03-17 18:38:17 -04001967 list_for_each_entry(pgpath, &pg->pgpaths, list) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001968 if (pgpath->is_active) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001969 has_active = true;
Mike Snitzer9f54cec2016-02-11 21:42:28 -05001970 if (!pgpath_busy(pgpath)) {
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001971 busy = false;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001972 break;
1973 }
1974 }
Mike Snitzer2da16102016-03-17 18:38:17 -04001975 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001976
Mike Snitzer2da16102016-03-17 18:38:17 -04001977 if (!has_active) {
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001978 /*
1979 * No active path in this pg, so this pg won't be used and
1980 * the current_pg will be changed at next mapping time.
1981 * We need to try mapping to determine it.
1982 */
Mike Snitzerbe7d31c2016-02-10 13:02:21 -05001983 busy = false;
Mike Snitzer2da16102016-03-17 18:38:17 -04001984 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001985
1986 return busy;
1987}
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989/*-----------------------------------------------------------------
1990 * Module setup
1991 *---------------------------------------------------------------*/
1992static struct target_type multipath_target = {
1993 .name = "multipath",
Mike Snitzere83068a2016-05-24 21:16:51 -04001994 .version = {1, 12, 0},
Mike Snitzer16f12262016-01-31 17:22:27 -05001995 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 .module = THIS_MODULE,
1997 .ctr = multipath_ctr,
1998 .dtr = multipath_dtr,
Mike Snitzere5863d92014-12-17 21:08:12 -05001999 .clone_and_map_rq = multipath_clone_and_map,
2000 .release_clone_rq = multipath_release_clone,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01002001 .rq_end_io = multipath_end_io,
Mike Snitzer76e33fe2016-05-19 16:15:14 -04002002 .map = multipath_map_bio,
2003 .end_io = multipath_end_io_bio,
2004 .presuspend = multipath_presuspend,
2005 .postsuspend = multipath_postsuspend,
2006 .resume = multipath_resume,
2007 .status = multipath_status,
2008 .message = multipath_message,
2009 .prepare_ioctl = multipath_prepare_ioctl,
2010 .iterate_devices = multipath_iterate_devices,
2011 .busy = multipath_busy,
2012};
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014static int __init dm_multipath_init(void)
2015{
2016 int r;
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 r = dm_register_target(&multipath_target);
2019 if (r < 0) {
Mike Snitzer76e33fe2016-05-19 16:15:14 -04002020 DMERR("request-based register failed %d", r);
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002021 r = -EINVAL;
2022 goto bad_register_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
2024
Tejun Heo4d4d66a2011-01-13 19:59:57 +00002025 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002026 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01002027 DMERR("failed to create workqueue kmpathd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002028 r = -ENOMEM;
2029 goto bad_alloc_kmultipathd;
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002030 }
2031
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002032 /*
2033 * A separate workqueue is used to handle the device handlers
2034 * to avoid overloading existing workqueue. Overloading the
2035 * old workqueue would also create a bottleneck in the
2036 * path of the storage hardware device activation.
2037 */
Tejun Heo4d4d66a2011-01-13 19:59:57 +00002038 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
2039 WQ_MEM_RECLAIM);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002040 if (!kmpath_handlerd) {
2041 DMERR("failed to create workqueue kmpath_handlerd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002042 r = -ENOMEM;
2043 goto bad_alloc_kmpath_handlerd;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002044 }
2045
Johannes Thumshirnff658e92015-01-11 12:45:23 +01002046 return 0;
2047
2048bad_alloc_kmpath_handlerd:
2049 destroy_workqueue(kmultipathd);
2050bad_alloc_kmultipathd:
2051 dm_unregister_target(&multipath_target);
2052bad_register_target:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return r;
2054}
2055
2056static void __exit dm_multipath_exit(void)
2057{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07002058 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07002059 destroy_workqueue(kmultipathd);
2060
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002061 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064module_init(dm_multipath_init);
2065module_exit(dm_multipath_exit);
2066
2067MODULE_DESCRIPTION(DM_NAME " multipath target");
2068MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
2069MODULE_LICENSE("GPL");