Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / drivers / md / dm-mpath.c
CommitLineData
1da177e4
LT
1/*
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
586e80e6
MP
8#include <linux/device-mapper.h>
9
4cc96131 10#include "dm-rq.h"
76e33fe4 11#include "dm-bio-record.h"
1da177e4 12#include "dm-path-selector.h"
b15546f9 13#include "dm-uevent.h"
1da177e4 14
e5863d9a 15#include <linux/blkdev.h>
1da177e4
LT
16#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>
35991652 24#include <linux/delay.h>
cfae5c9b 25#include <scsi/scsi_dh.h>
60063497 26#include <linux/atomic.h>
78ce23b5 27#include <linux/blk-mq.h>
1da177e4 28
72d94861 29#define DM_MSG_PREFIX "multipath"
4e2d19e4
CS
30#define DM_PG_INIT_DELAY_MSECS 2000
31#define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
1da177e4
LT
32
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
c922d5f7 40 struct dm_path path;
4e2d19e4 41 struct delayed_work activate_path;
be7d31cc
MS
42
43 bool is_active:1; /* Path status */
1da177e4
LT
44};
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 */
1da177e4
LT
59 unsigned nr_pgpaths; /* Number of paths in PG */
60 struct list_head pgpaths;
be7d31cc
MS
61
62 bool bypassed:1; /* Temporarily bypass this PG? */
1da177e4
LT
63};
64
65/* Multipath context */
66struct multipath {
67 struct list_head list;
68 struct dm_target *ti;
69
cfae5c9b 70 const char *hw_handler_name;
2bfd2e13 71 char *hw_handler_params;
4e2d19e4 72
1fbdd2b3
MS
73 spinlock_t lock;
74
1da177e4
LT
75 unsigned nr_priority_groups;
76 struct list_head priority_groups;
4e2d19e4
CS
77
78 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
79
1da177e4
LT
80 struct pgpath *current_pgpath;
81 struct priority_group *current_pg;
82 struct priority_group *next_pg; /* Switch to this PG if set */
1da177e4 83
518257b1 84 unsigned long flags; /* Multipath state flags */
1fbdd2b3 85
c9e45581 86 unsigned pg_init_retries; /* Number of times to retry pg_init */
4e2d19e4 87 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
1da177e4 88
91e968aa
MS
89 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
7e0d574f 93 enum dm_queue_mode queue_mode;
e83068a5 94
6380f26f 95 struct mutex work_mutex;
20800cb3 96 struct work_struct trigger_event;
76e33fe4
MS
97
98 struct work_struct process_queued_bios;
99 struct bio_list queued_bios;
1da177e4
LT
100};
101
102/*
76e33fe4 103 * Context information attached to each io we process.
1da177e4 104 */
028867ac 105struct dm_mpath_io {
1da177e4 106 struct pgpath *pgpath;
02ab823f 107 size_t nr_bytes;
1da177e4
LT
108};
109
110typedef int (*action_fn) (struct pgpath *pgpath);
111
bab7cfc7 112static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
c4028958 113static void trigger_event(struct work_struct *work);
89bfce76
BVA
114static void activate_or_offline_path(struct pgpath *pgpath);
115static void activate_path_work(struct work_struct *work);
76e33fe4 116static void process_queued_bios(struct work_struct *work);
1da177e4 117
518257b1
MS
118/*-----------------------------------------------
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? */
1da177e4
LT
129
130/*-----------------------------------------------
131 * Allocation routines
132 *-----------------------------------------------*/
133
134static struct pgpath *alloc_pgpath(void)
135{
e69fae56 136 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
1da177e4 137
224cb3e9 138 if (pgpath) {
be7d31cc 139 pgpath->is_active = true;
89bfce76 140 INIT_DELAYED_WORK(&pgpath->activate_path, activate_path_work);
224cb3e9 141 }
1da177e4
LT
142
143 return pgpath;
144}
145
028867ac 146static void free_pgpath(struct pgpath *pgpath)
1da177e4
LT
147{
148 kfree(pgpath);
149}
150
151static struct priority_group *alloc_priority_group(void)
152{
153 struct priority_group *pg;
154
e69fae56 155 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1da177e4 156
e69fae56
MM
157 if (pg)
158 INIT_LIST_HEAD(&pg->pgpaths);
1da177e4
LT
159
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
e83068a5 188static struct multipath *alloc_multipath(struct dm_target *ti)
1da177e4
LT
189{
190 struct multipath *m;
191
e69fae56 192 m = kzalloc(sizeof(*m), GFP_KERNEL);
1da177e4 193 if (m) {
1da177e4
LT
194 INIT_LIST_HEAD(&m->priority_groups);
195 spin_lock_init(&m->lock);
518257b1 196 set_bit(MPATHF_QUEUE_IO, &m->flags);
91e968aa
MS
197 atomic_set(&m->nr_valid_paths, 0);
198 atomic_set(&m->pg_init_in_progress, 0);
199 atomic_set(&m->pg_init_count, 0);
4e2d19e4 200 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
c4028958 201 INIT_WORK(&m->trigger_event, trigger_event);
2bded7bd 202 init_waitqueue_head(&m->pg_init_wait);
6380f26f 203 mutex_init(&m->work_mutex);
8637a6bf 204
e83068a5 205 m->queue_mode = DM_TYPE_NONE;
76e33fe4 206
28f16c20
MM
207 m->ti = ti;
208 ti->private = m;
1da177e4
LT
209 }
210
211 return m;
212}
213
e83068a5
MS
214static 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;
eb8db831 224 } else if (m->queue_mode == DM_TYPE_BIO_BASED) {
e83068a5
MS
225 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
1da177e4
LT
238static void free_multipath(struct multipath *m)
239{
240 struct priority_group *pg, *tmp;
1da177e4
LT
241
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
cfae5c9b 247 kfree(m->hw_handler_name);
2bfd2e13 248 kfree(m->hw_handler_params);
1da177e4
LT
249 kfree(m);
250}
251
2eff1924
MS
252static struct dm_mpath_io *get_mpio(union map_info *info)
253{
254 return info->ptr;
255}
256
bf661be1
MS
257static size_t multipath_per_bio_data_size(void)
258{
259 return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details);
260}
261
76e33fe4
MS
262static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
263{
bf661be1 264 return dm_per_bio_data(bio, multipath_per_bio_data_size());
76e33fe4
MS
265}
266
bf661be1 267static struct dm_bio_details *get_bio_details_from_bio(struct bio *bio)
76e33fe4 268{
bf661be1 269 /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */
76e33fe4 270 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
bf661be1
MS
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)
278{
279 struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
280 struct dm_bio_details *bio_details = get_bio_details_from_bio(bio);
76e33fe4
MS
281
282 memset(mpio, 0, sizeof(*mpio));
bf661be1
MS
283 memset(bio_details, 0, sizeof(*bio_details));
284 dm_bio_record(bio_details, bio);
76e33fe4 285
bf661be1
MS
286 if (mpio_p)
287 *mpio_p = mpio;
288 if (bio_details_p)
289 *bio_details_p = bio_details;
76e33fe4
MS
290}
291
1da177e4
LT
292/*-----------------------------------------------
293 * Path selection
294 *-----------------------------------------------*/
295
3e9f1be1 296static int __pg_init_all_paths(struct multipath *m)
fb612642
KU
297{
298 struct pgpath *pgpath;
4e2d19e4 299 unsigned long pg_init_delay = 0;
fb612642 300
b194679f
BVA
301 lockdep_assert_held(&m->lock);
302
91e968aa 303 if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
3e9f1be1 304 return 0;
17f4ff45 305
91e968aa 306 atomic_inc(&m->pg_init_count);
518257b1 307 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
3e9f1be1
HR
308
309 /* Check here to reset pg_init_required */
310 if (!m->current_pg)
311 return 0;
312
518257b1 313 if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
4e2d19e4
CS
314 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);
fb612642
KU
316 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
317 /* Skip failed paths */
318 if (!pgpath->is_active)
319 continue;
4e2d19e4
CS
320 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
321 pg_init_delay))
91e968aa 322 atomic_inc(&m->pg_init_in_progress);
fb612642 323 }
91e968aa 324 return atomic_read(&m->pg_init_in_progress);
fb612642
KU
325}
326
c1d7ecf7 327static int pg_init_all_paths(struct multipath *m)
1da177e4 328{
c1d7ecf7 329 int ret;
2da1610a
MS
330 unsigned long flags;
331
332 spin_lock_irqsave(&m->lock, flags);
c1d7ecf7 333 ret = __pg_init_all_paths(m);
2da1610a 334 spin_unlock_irqrestore(&m->lock, flags);
c1d7ecf7
BVA
335
336 return ret;
2da1610a
MS
337}
338
339static void __switch_pg(struct multipath *m, struct priority_group *pg)
340{
341 m->current_pg = pg;
1da177e4
LT
342
343 /* Must we initialise the PG first, and queue I/O till it's ready? */
cfae5c9b 344 if (m->hw_handler_name) {
518257b1
MS
345 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
346 set_bit(MPATHF_QUEUE_IO, &m->flags);
1da177e4 347 } else {
518257b1
MS
348 clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
349 clear_bit(MPATHF_QUEUE_IO, &m->flags);
1da177e4 350 }
c9e45581 351
91e968aa 352 atomic_set(&m->pg_init_count, 0);
1da177e4
LT
353}
354
2da1610a
MS
355static struct pgpath *choose_path_in_pg(struct multipath *m,
356 struct priority_group *pg,
357 size_t nr_bytes)
1da177e4 358{
2da1610a 359 unsigned long flags;
c922d5f7 360 struct dm_path *path;
2da1610a 361 struct pgpath *pgpath;
1da177e4 362
90a4323c 363 path = pg->ps.type->select_path(&pg->ps, nr_bytes);
1da177e4 364 if (!path)
2da1610a 365 return ERR_PTR(-ENXIO);
1da177e4 366
2da1610a 367 pgpath = path_to_pgpath(path);
1da177e4 368
2da1610a
MS
369 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 }
1da177e4 376
2da1610a 377 return pgpath;
1da177e4
LT
378}
379
2da1610a 380static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
1da177e4 381{
2da1610a 382 unsigned long flags;
1da177e4 383 struct priority_group *pg;
2da1610a 384 struct pgpath *pgpath;
d19a55cc 385 unsigned bypassed = 1;
1da177e4 386
91e968aa 387 if (!atomic_read(&m->nr_valid_paths)) {
518257b1 388 clear_bit(MPATHF_QUEUE_IO, &m->flags);
1da177e4 389 goto failed;
1f271972 390 }
1da177e4
LT
391
392 /* Were we instructed to switch PG? */
2da1610a
MS
393 if (lockless_dereference(m->next_pg)) {
394 spin_lock_irqsave(&m->lock, flags);
1da177e4 395 pg = m->next_pg;
2da1610a
MS
396 if (!pg) {
397 spin_unlock_irqrestore(&m->lock, flags);
398 goto check_current_pg;
399 }
1da177e4 400 m->next_pg = NULL;
2da1610a
MS
401 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;
1da177e4
LT
405 }
406
407 /* Don't change PG until it has no remaining paths */
2da1610a
MS
408check_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 }
1da177e4
LT
415
416 /*
417 * Loop through priority groups until we find a valid path.
418 * First time we skip PGs marked 'bypassed'.
f220fd4e
MC
419 * Second time we only try the ones we skipped, but set
420 * pg_init_delay_retry so we do not hammer controllers.
1da177e4
LT
421 */
422 do {
423 list_for_each_entry(pg, &m->priority_groups, list) {
d19a55cc 424 if (pg->bypassed == !!bypassed)
1da177e4 425 continue;
2da1610a
MS
426 pgpath = choose_path_in_pg(m, pg, nr_bytes);
427 if (!IS_ERR_OR_NULL(pgpath)) {
f220fd4e 428 if (!bypassed)
518257b1 429 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
2da1610a 430 return pgpath;
f220fd4e 431 }
1da177e4
LT
432 }
433 } while (bypassed--);
434
435failed:
2da1610a 436 spin_lock_irqsave(&m->lock, flags);
1da177e4
LT
437 m->current_pgpath = NULL;
438 m->current_pg = NULL;
2da1610a
MS
439 spin_unlock_irqrestore(&m->lock, flags);
440
441 return NULL;
1da177e4
LT
442}
443
45e15720 444/*
86331f39
BVA
445 * dm_report_EIO() is a macro instead of a function to make pr_debug()
446 * report the function name and line number of the function from which
447 * it has been invoked.
45e15720 448 */
86331f39 449#define dm_report_EIO(m) \
18a482f5 450do { \
86331f39
BVA
451 struct mapped_device *md = dm_table_get_md((m)->ti->table); \
452 \
453 pr_debug("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d\n", \
454 dm_device_name(md), \
455 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)); \
18a482f5 458} while (0)
45e15720 459
36fcffcc 460/*
76e33fe4 461 * Map cloned requests (request-based multipath)
36fcffcc 462 */
eb8db831
CH
463static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
464 union map_info *map_context,
465 struct request **__clone)
1da177e4 466{
7943bd6d 467 struct multipath *m = ti->private;
eb8db831 468 size_t nr_bytes = blk_rq_bytes(rq);
1da177e4 469 struct pgpath *pgpath;
f40c67f0 470 struct block_device *bdev;
eb8db831 471 struct dm_mpath_io *mpio = get_mpio(map_context);
7083abbb 472 struct request_queue *q;
eb8db831 473 struct request *clone;
1da177e4 474
1da177e4 475 /* Do we need to select a new pgpath? */
2da1610a
MS
476 pgpath = lockless_dereference(m->current_pgpath);
477 if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
478 pgpath = choose_pgpath(m, nr_bytes);
1da177e4 479
9bf59a61 480 if (!pgpath) {
ca5beb76 481 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
b88efd43 482 return DM_MAPIO_DELAY_REQUEUE;
18a482f5 483 dm_report_EIO(m); /* Failed */
f98e0eb6 484 return DM_MAPIO_KILL;
518257b1
MS
485 } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
486 test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
c1d7ecf7
BVA
487 if (pg_init_all_paths(m))
488 return DM_MAPIO_DELAY_REQUEUE;
06eb061f 489 return DM_MAPIO_REQUEUE;
9bf59a61 490 }
6afbc01d 491
eb8db831 492 memset(mpio, 0, sizeof(*mpio));
2eb6e1e3
KB
493 mpio->pgpath = pgpath;
494 mpio->nr_bytes = nr_bytes;
495
9bf59a61 496 bdev = pgpath->path.dev->bdev;
7083abbb
BVA
497 q = bdev_get_queue(bdev);
498 clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE, GFP_ATOMIC);
eb8db831
CH
499 if (IS_ERR(clone)) {
500 /* EBUSY, ENODEV or EWOULDBLOCK: requeue */
7083abbb
BVA
501 bool queue_dying = blk_queue_dying(q);
502 DMERR_LIMIT("blk_get_request() returned %ld%s - requeuing",
503 PTR_ERR(clone), queue_dying ? " (path offline)" : "");
504 if (queue_dying) {
505 atomic_inc(&m->pg_init_in_progress);
506 activate_or_offline_path(pgpath);
507 return DM_MAPIO_REQUEUE;
508 }
06eb061f 509 return DM_MAPIO_DELAY_REQUEUE;
e5863d9a 510 }
eb8db831
CH
511 clone->bio = clone->biotail = NULL;
512 clone->rq_disk = bdev->bd_disk;
513 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
514 *__clone = clone;
e5863d9a 515
9bf59a61
MS
516 if (pgpath->pg->ps.type->start_io)
517 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
518 &pgpath->path,
519 nr_bytes);
2eb6e1e3 520 return DM_MAPIO_REMAPPED;
1da177e4
LT
521}
522
e5863d9a
MS
523static void multipath_release_clone(struct request *clone)
524{
eb8db831 525 blk_put_request(clone);
e5863d9a
MS
526}
527
76e33fe4
MS
528/*
529 * Map cloned bios (bio-based multipath)
530 */
531static int __multipath_map_bio(struct multipath *m, struct bio *bio, struct dm_mpath_io *mpio)
532{
533 size_t nr_bytes = bio->bi_iter.bi_size;
534 struct pgpath *pgpath;
535 unsigned long flags;
536 bool queue_io;
537
538 /* Do we need to select a new pgpath? */
539 pgpath = lockless_dereference(m->current_pgpath);
540 queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
541 if (!pgpath || !queue_io)
542 pgpath = choose_pgpath(m, nr_bytes);
543
544 if ((pgpath && queue_io) ||
545 (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
546 /* Queue for the daemon to resubmit */
547 spin_lock_irqsave(&m->lock, flags);
548 bio_list_add(&m->queued_bios, bio);
549 spin_unlock_irqrestore(&m->lock, flags);
550 /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
551 if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
552 pg_init_all_paths(m);
553 else if (!queue_io)
554 queue_work(kmultipathd, &m->process_queued_bios);
555 return DM_MAPIO_SUBMITTED;
556 }
557
558 if (!pgpath) {
ca5beb76
BVA
559 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
560 return DM_MAPIO_REQUEUE;
18a482f5
CH
561 dm_report_EIO(m);
562 return -EIO;
76e33fe4
MS
563 }
564
565 mpio->pgpath = pgpath;
566 mpio->nr_bytes = nr_bytes;
567
568 bio->bi_error = 0;
569 bio->bi_bdev = pgpath->path.dev->bdev;
1eff9d32 570 bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
76e33fe4
MS
571
572 if (pgpath->pg->ps.type->start_io)
573 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
574 &pgpath->path,
575 nr_bytes);
576 return DM_MAPIO_REMAPPED;
577}
578
579static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
580{
581 struct multipath *m = ti->private;
bf661be1
MS
582 struct dm_mpath_io *mpio = NULL;
583
584 multipath_init_per_bio_data(bio, &mpio, NULL);
76e33fe4
MS
585
586 return __multipath_map_bio(m, bio, mpio);
587}
588
7e48c768 589static void process_queued_io_list(struct multipath *m)
76e33fe4 590{
7e48c768
MS
591 if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED)
592 dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
593 else if (m->queue_mode == DM_TYPE_BIO_BASED)
76e33fe4
MS
594 queue_work(kmultipathd, &m->process_queued_bios);
595}
596
597static void process_queued_bios(struct work_struct *work)
598{
599 int r;
600 unsigned long flags;
601 struct bio *bio;
602 struct bio_list bios;
603 struct blk_plug plug;
604 struct multipath *m =
605 container_of(work, struct multipath, process_queued_bios);
606
607 bio_list_init(&bios);
608
609 spin_lock_irqsave(&m->lock, flags);
610
611 if (bio_list_empty(&m->queued_bios)) {
612 spin_unlock_irqrestore(&m->lock, flags);
613 return;
614 }
615
616 bio_list_merge(&bios, &m->queued_bios);
617 bio_list_init(&m->queued_bios);
618
619 spin_unlock_irqrestore(&m->lock, flags);
620
621 blk_start_plug(&plug);
622 while ((bio = bio_list_pop(&bios))) {
623 r = __multipath_map_bio(m, bio, get_mpio_from_bio(bio));
624 if (r < 0 || r == DM_MAPIO_REQUEUE) {
625 bio->bi_error = r;
626 bio_endio(bio);
627 } else if (r == DM_MAPIO_REMAPPED)
628 generic_make_request(bio);
629 }
630 blk_finish_plug(&plug);
631}
632
9a8ac3ae
BVA
633static void assign_bit(bool value, long nr, unsigned long *addr)
634{
635 if (value)
636 set_bit(nr, addr);
637 else
638 clear_bit(nr, addr);
639}
640
1da177e4
LT
641/*
642 * If we run out of usable paths, should we queue I/O or error it?
643 */
be7d31cc
MS
644static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
645 bool save_old_value)
1da177e4
LT
646{
647 unsigned long flags;
648
649 spin_lock_irqsave(&m->lock, flags);
9a8ac3ae
BVA
650 assign_bit((save_old_value && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) ||
651 (!save_old_value && queue_if_no_path),
652 MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
653 assign_bit(queue_if_no_path || dm_noflush_suspending(m->ti),
654 MPATHF_QUEUE_IF_NO_PATH, &m->flags);
1da177e4
LT
655 spin_unlock_irqrestore(&m->lock, flags);
656
76e33fe4 657 if (!queue_if_no_path) {
63d832c3 658 dm_table_run_md_queue_async(m->ti->table);
7e48c768 659 process_queued_io_list(m);
76e33fe4 660 }
63d832c3 661
1da177e4
LT
662 return 0;
663}
664
1da177e4
LT
665/*
666 * An event is triggered whenever a path is taken out of use.
667 * Includes path failure and PG bypass.
668 */
c4028958 669static void trigger_event(struct work_struct *work)
1da177e4 670{
c4028958
DH
671 struct multipath *m =
672 container_of(work, struct multipath, trigger_event);
1da177e4
LT
673
674 dm_table_event(m->ti->table);
675}
676
677/*-----------------------------------------------------------------
678 * Constructor/argument parsing:
679 * <#multipath feature args> [<arg>]*
680 * <#hw_handler args> [hw_handler [<arg>]*]
681 * <#priority groups>
682 * <initial priority group>
683 * [<selector> <#selector args> [<arg>]*
684 * <#paths> <#per-path selector args>
685 * [<path> [<arg>]* ]+ ]+
686 *---------------------------------------------------------------*/
498f0103 687static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
1da177e4
LT
688 struct dm_target *ti)
689{
690 int r;
691 struct path_selector_type *pst;
692 unsigned ps_argc;
693
498f0103 694 static struct dm_arg _args[] = {
72d94861 695 {0, 1024, "invalid number of path selector args"},
1da177e4
LT
696 };
697
498f0103 698 pst = dm_get_path_selector(dm_shift_arg(as));
1da177e4 699 if (!pst) {
72d94861 700 ti->error = "unknown path selector type";
1da177e4
LT
701 return -EINVAL;
702 }
703
498f0103 704 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
371b2e34
MP
705 if (r) {
706 dm_put_path_selector(pst);
1da177e4 707 return -EINVAL;
371b2e34 708 }
1da177e4
LT
709
710 r = pst->create(&pg->ps, ps_argc, as->argv);
711 if (r) {
712 dm_put_path_selector(pst);
72d94861 713 ti->error = "path selector constructor failed";
1da177e4
LT
714 return r;
715 }
716
717 pg->ps.type = pst;
498f0103 718 dm_consume_args(as, ps_argc);
1da177e4
LT
719
720 return 0;
721}
722
498f0103 723static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
1da177e4
LT
724 struct dm_target *ti)
725{
726 int r;
727 struct pgpath *p;
ae11b1b3 728 struct multipath *m = ti->private;
a58a935d
MS
729 struct request_queue *q = NULL;
730 const char *attached_handler_name;
1da177e4
LT
731
732 /* we need at least a path arg */
733 if (as->argc < 1) {
72d94861 734 ti->error = "no device given";
01460f35 735 return ERR_PTR(-EINVAL);
1da177e4
LT
736 }
737
738 p = alloc_pgpath();
739 if (!p)
01460f35 740 return ERR_PTR(-ENOMEM);
1da177e4 741
498f0103 742 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
8215d6ec 743 &p->path.dev);
1da177e4 744 if (r) {
72d94861 745 ti->error = "error getting device";
1da177e4
LT
746 goto bad;
747 }
748
518257b1 749 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) || m->hw_handler_name)
a58a935d
MS
750 q = bdev_get_queue(p->path.dev->bdev);
751
518257b1 752 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
1bab0de0 753retain:
a58a935d
MS
754 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
755 if (attached_handler_name) {
54cd640d 756 /*
757 * Clear any hw_handler_params associated with a
758 * handler that isn't already attached.
759 */
760 if (m->hw_handler_name && strcmp(attached_handler_name, m->hw_handler_name)) {
761 kfree(m->hw_handler_params);
762 m->hw_handler_params = NULL;
763 }
764
a58a935d
MS
765 /*
766 * Reset hw_handler_name to match the attached handler
a58a935d
MS
767 *
768 * NB. This modifies the table line to show the actual
769 * handler instead of the original table passed in.
770 */
771 kfree(m->hw_handler_name);
772 m->hw_handler_name = attached_handler_name;
a58a935d
MS
773 }
774 }
a0cf7ea9 775
a58a935d 776 if (m->hw_handler_name) {
a0cf7ea9
HR
777 r = scsi_dh_attach(q, m->hw_handler_name);
778 if (r == -EBUSY) {
1bab0de0 779 char b[BDEVNAME_SIZE];
a0cf7ea9 780
1bab0de0
CH
781 printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
782 bdevname(p->path.dev->bdev, b));
783 goto retain;
784 }
ae11b1b3 785 if (r < 0) {
a0cf7ea9 786 ti->error = "error attaching hardware handler";
ae11b1b3
HR
787 dm_put_device(ti, p->path.dev);
788 goto bad;
789 }
2bfd2e13
CS
790
791 if (m->hw_handler_params) {
792 r = scsi_dh_set_params(q, m->hw_handler_params);
793 if (r < 0) {
794 ti->error = "unable to set hardware "
795 "handler parameters";
2bfd2e13
CS
796 dm_put_device(ti, p->path.dev);
797 goto bad;
798 }
799 }
ae11b1b3
HR
800 }
801
1da177e4
LT
802 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
803 if (r) {
804 dm_put_device(ti, p->path.dev);
805 goto bad;
806 }
807
808 return p;
809
810 bad:
811 free_pgpath(p);
01460f35 812 return ERR_PTR(r);
1da177e4
LT
813}
814
498f0103 815static struct priority_group *parse_priority_group(struct dm_arg_set *as,
28f16c20 816 struct multipath *m)
1da177e4 817{
498f0103 818 static struct dm_arg _args[] = {
72d94861
AK
819 {1, 1024, "invalid number of paths"},
820 {0, 1024, "invalid number of selector args"}
1da177e4
LT
821 };
822
823 int r;
498f0103 824 unsigned i, nr_selector_args, nr_args;
1da177e4 825 struct priority_group *pg;
28f16c20 826 struct dm_target *ti = m->ti;
1da177e4
LT
827
828 if (as->argc < 2) {
829 as->argc = 0;
01460f35
BM
830 ti->error = "not enough priority group arguments";
831 return ERR_PTR(-EINVAL);
1da177e4
LT
832 }
833
834 pg = alloc_priority_group();
835 if (!pg) {
72d94861 836 ti->error = "couldn't allocate priority group";
01460f35 837 return ERR_PTR(-ENOMEM);
1da177e4
LT
838 }
839 pg->m = m;
840
841 r = parse_path_selector(as, pg, ti);
842 if (r)
843 goto bad;
844
845 /*
846 * read the paths
847 */
498f0103 848 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
1da177e4
LT
849 if (r)
850 goto bad;
851
498f0103 852 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
1da177e4
LT
853 if (r)
854 goto bad;
855
498f0103 856 nr_args = 1 + nr_selector_args;
1da177e4
LT
857 for (i = 0; i < pg->nr_pgpaths; i++) {
858 struct pgpath *pgpath;
498f0103 859 struct dm_arg_set path_args;
1da177e4 860
498f0103 861 if (as->argc < nr_args) {
148acff6 862 ti->error = "not enough path parameters";
6bbf79a1 863 r = -EINVAL;
1da177e4 864 goto bad;
148acff6 865 }
1da177e4 866
498f0103 867 path_args.argc = nr_args;
1da177e4
LT
868 path_args.argv = as->argv;
869
870 pgpath = parse_path(&path_args, &pg->ps, ti);
01460f35
BM
871 if (IS_ERR(pgpath)) {
872 r = PTR_ERR(pgpath);
1da177e4 873 goto bad;
01460f35 874 }
1da177e4
LT
875
876 pgpath->pg = pg;
877 list_add_tail(&pgpath->list, &pg->pgpaths);
498f0103 878 dm_consume_args(as, nr_args);
1da177e4
LT
879 }
880
881 return pg;
882
883 bad:
884 free_priority_group(pg, ti);
01460f35 885 return ERR_PTR(r);
1da177e4
LT
886}
887
498f0103 888static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
1da177e4 889{
1da177e4 890 unsigned hw_argc;
2bfd2e13 891 int ret;
28f16c20 892 struct dm_target *ti = m->ti;
1da177e4 893
498f0103 894 static struct dm_arg _args[] = {
72d94861 895 {0, 1024, "invalid number of hardware handler args"},
1da177e4
LT
896 };
897
498f0103 898 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
1da177e4
LT
899 return -EINVAL;
900
901 if (!hw_argc)
902 return 0;
903
e83068a5 904 if (m->queue_mode == DM_TYPE_BIO_BASED) {
76e33fe4
MS
905 dm_consume_args(as, hw_argc);
906 DMERR("bio-based multipath doesn't allow hardware handler args");
907 return 0;
908 }
909
498f0103 910 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
f97dc421 911 if (!m->hw_handler_name)
912 return -EINVAL;
14e98c5c 913
2bfd2e13
CS
914 if (hw_argc > 1) {
915 char *p;
916 int i, j, len = 4;
917
918 for (i = 0; i <= hw_argc - 2; i++)
919 len += strlen(as->argv[i]) + 1;
920 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
921 if (!p) {
922 ti->error = "memory allocation failed";
923 ret = -ENOMEM;
924 goto fail;
925 }
926 j = sprintf(p, "%d", hw_argc - 1);
927 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
928 j = sprintf(p, "%s", as->argv[i]);
929 }
498f0103 930 dm_consume_args(as, hw_argc - 1);
1da177e4
LT
931
932 return 0;
2bfd2e13
CS
933fail:
934 kfree(m->hw_handler_name);
935 m->hw_handler_name = NULL;
936 return ret;
1da177e4
LT
937}
938
498f0103 939static int parse_features(struct dm_arg_set *as, struct multipath *m)
1da177e4
LT
940{
941 int r;
942 unsigned argc;
28f16c20 943 struct dm_target *ti = m->ti;
498f0103 944 const char *arg_name;
1da177e4 945
498f0103 946 static struct dm_arg _args[] = {
e83068a5 947 {0, 8, "invalid number of feature args"},
c9e45581 948 {1, 50, "pg_init_retries must be between 1 and 50"},
4e2d19e4 949 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
1da177e4
LT
950 };
951
498f0103 952 r = dm_read_arg_group(_args, as, &argc, &ti->error);
1da177e4
LT
953 if (r)
954 return -EINVAL;
955
956 if (!argc)
957 return 0;
958
c9e45581 959 do {
498f0103 960 arg_name = dm_shift_arg(as);
c9e45581
DW
961 argc--;
962
498f0103 963 if (!strcasecmp(arg_name, "queue_if_no_path")) {
be7d31cc 964 r = queue_if_no_path(m, true, false);
c9e45581
DW
965 continue;
966 }
967
a58a935d 968 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
518257b1 969 set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
a58a935d
MS
970 continue;
971 }
972
498f0103 973 if (!strcasecmp(arg_name, "pg_init_retries") &&
c9e45581 974 (argc >= 1)) {
498f0103 975 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
c9e45581
DW
976 argc--;
977 continue;
978 }
979
498f0103 980 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
4e2d19e4 981 (argc >= 1)) {
498f0103 982 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
4e2d19e4
CS
983 argc--;
984 continue;
985 }
986
e83068a5
MS
987 if (!strcasecmp(arg_name, "queue_mode") &&
988 (argc >= 1)) {
989 const char *queue_mode_name = dm_shift_arg(as);
990
991 if (!strcasecmp(queue_mode_name, "bio"))
992 m->queue_mode = DM_TYPE_BIO_BASED;
993 else if (!strcasecmp(queue_mode_name, "rq"))
994 m->queue_mode = DM_TYPE_REQUEST_BASED;
995 else if (!strcasecmp(queue_mode_name, "mq"))
996 m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
997 else {
998 ti->error = "Unknown 'queue_mode' requested";
999 r = -EINVAL;
1000 }
1001 argc--;
1002 continue;
1003 }
1004
1da177e4 1005 ti->error = "Unrecognised multipath feature request";
c9e45581
DW
1006 r = -EINVAL;
1007 } while (argc && !r);
1008
1009 return r;
1da177e4
LT
1010}
1011
e83068a5 1012static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
1da177e4 1013{
498f0103
MS
1014 /* target arguments */
1015 static struct dm_arg _args[] = {
a490a07a
MS
1016 {0, 1024, "invalid number of priority groups"},
1017 {0, 1024, "invalid initial priority group number"},
1da177e4
LT
1018 };
1019
1020 int r;
1021 struct multipath *m;
498f0103 1022 struct dm_arg_set as;
1da177e4
LT
1023 unsigned pg_count = 0;
1024 unsigned next_pg_num;
1025
1026 as.argc = argc;
1027 as.argv = argv;
1028
e83068a5 1029 m = alloc_multipath(ti);
1da177e4 1030 if (!m) {
72d94861 1031 ti->error = "can't allocate multipath";
1da177e4
LT
1032 return -EINVAL;
1033 }
1034
28f16c20 1035 r = parse_features(&as, m);
1da177e4
LT
1036 if (r)
1037 goto bad;
1038
e83068a5
MS
1039 r = alloc_multipath_stage2(ti, m);
1040 if (r)
1041 goto bad;
1042
28f16c20 1043 r = parse_hw_handler(&as, m);
1da177e4
LT
1044 if (r)
1045 goto bad;
1046
498f0103 1047 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
1da177e4
LT
1048 if (r)
1049 goto bad;
1050
498f0103 1051 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
1da177e4
LT
1052 if (r)
1053 goto bad;
1054
a490a07a
MS
1055 if ((!m->nr_priority_groups && next_pg_num) ||
1056 (m->nr_priority_groups && !next_pg_num)) {
1057 ti->error = "invalid initial priority group";
1058 r = -EINVAL;
1059 goto bad;
1060 }
1061
1da177e4
LT
1062 /* parse the priority groups */
1063 while (as.argc) {
1064 struct priority_group *pg;
91e968aa 1065 unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
1da177e4 1066
28f16c20 1067 pg = parse_priority_group(&as, m);
01460f35
BM
1068 if (IS_ERR(pg)) {
1069 r = PTR_ERR(pg);
1da177e4
LT
1070 goto bad;
1071 }
1072
91e968aa
MS
1073 nr_valid_paths += pg->nr_pgpaths;
1074 atomic_set(&m->nr_valid_paths, nr_valid_paths);
1075
1da177e4
LT
1076 list_add_tail(&pg->list, &m->priority_groups);
1077 pg_count++;
1078 pg->pg_num = pg_count;
1079 if (!--next_pg_num)
1080 m->next_pg = pg;
1081 }
1082
1083 if (pg_count != m->nr_priority_groups) {
72d94861 1084 ti->error = "priority group count mismatch";
1da177e4
LT
1085 r = -EINVAL;
1086 goto bad;
1087 }
1088
55a62eef
AK
1089 ti->num_flush_bios = 1;
1090 ti->num_discard_bios = 1;
042bcef8 1091 ti->num_write_same_bios = 1;
ac62d620 1092 ti->num_write_zeroes_bios = 1;
e83068a5 1093 if (m->queue_mode == DM_TYPE_BIO_BASED)
bf661be1 1094 ti->per_io_data_size = multipath_per_bio_data_size();
eb8db831 1095 else
8637a6bf 1096 ti->per_io_data_size = sizeof(struct dm_mpath_io);
8627921f 1097
1da177e4
LT
1098 return 0;
1099
1100 bad:
1101 free_multipath(m);
1102 return r;
1103}
1104
2bded7bd
KU
1105static void multipath_wait_for_pg_init_completion(struct multipath *m)
1106{
9f4c3f87 1107 DEFINE_WAIT(wait);
2bded7bd
KU
1108
1109 while (1) {
9f4c3f87 1110 prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
2bded7bd 1111
91e968aa 1112 if (!atomic_read(&m->pg_init_in_progress))
2bded7bd 1113 break;
2bded7bd
KU
1114
1115 io_schedule();
1116 }
9f4c3f87 1117 finish_wait(&m->pg_init_wait, &wait);
2bded7bd
KU
1118}
1119
1120static void flush_multipath_work(struct multipath *m)
1da177e4 1121{
518257b1
MS
1122 set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1123 smp_mb__after_atomic();
954a73d5 1124
bab7cfc7 1125 flush_workqueue(kmpath_handlerd);
2bded7bd 1126 multipath_wait_for_pg_init_completion(m);
a044d016 1127 flush_workqueue(kmultipathd);
43829731 1128 flush_work(&m->trigger_event);
954a73d5 1129
518257b1
MS
1130 clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
1131 smp_mb__after_atomic();
6df400ab
KU
1132}
1133
1134static void multipath_dtr(struct dm_target *ti)
1135{
1136 struct multipath *m = ti->private;
1137
2bded7bd 1138 flush_multipath_work(m);
1da177e4
LT
1139 free_multipath(m);
1140}
1141
1da177e4
LT
1142/*
1143 * Take a path out of use.
1144 */
1145static int fail_path(struct pgpath *pgpath)
1146{
1147 unsigned long flags;
1148 struct multipath *m = pgpath->pg->m;
1149
1150 spin_lock_irqsave(&m->lock, flags);
1151
6680073d 1152 if (!pgpath->is_active)
1da177e4
LT
1153 goto out;
1154
72d94861 1155 DMWARN("Failing path %s.", pgpath->path.dev->name);
1da177e4
LT
1156
1157 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
be7d31cc 1158 pgpath->is_active = false;
1da177e4
LT
1159 pgpath->fail_count++;
1160
91e968aa 1161 atomic_dec(&m->nr_valid_paths);
1da177e4
LT
1162
1163 if (pgpath == m->current_pgpath)
1164 m->current_pgpath = NULL;
1165
b15546f9 1166 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
91e968aa 1167 pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
b15546f9 1168
fe9cf30e 1169 schedule_work(&m->trigger_event);
1da177e4
LT
1170
1171out:
1172 spin_unlock_irqrestore(&m->lock, flags);
1173
1174 return 0;
1175}
1176
1177/*
1178 * Reinstate a previously-failed path
1179 */
1180static int reinstate_path(struct pgpath *pgpath)
1181{
63d832c3 1182 int r = 0, run_queue = 0;
1da177e4
LT
1183 unsigned long flags;
1184 struct multipath *m = pgpath->pg->m;
91e968aa 1185 unsigned nr_valid_paths;
1da177e4
LT
1186
1187 spin_lock_irqsave(&m->lock, flags);
1188
6680073d 1189 if (pgpath->is_active)
1da177e4
LT
1190 goto out;
1191
ec31f3f7 1192 DMWARN("Reinstating path %s.", pgpath->path.dev->name);
1da177e4
LT
1193
1194 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1195 if (r)
1196 goto out;
1197
be7d31cc 1198 pgpath->is_active = true;
1da177e4 1199
91e968aa
MS
1200 nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
1201 if (nr_valid_paths == 1) {
e54f77dd 1202 m->current_pgpath = NULL;
63d832c3 1203 run_queue = 1;
e54f77dd 1204 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
4e2d19e4 1205 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
91e968aa 1206 atomic_inc(&m->pg_init_in_progress);
e54f77dd 1207 }
1da177e4 1208
b15546f9 1209 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
91e968aa 1210 pgpath->path.dev->name, nr_valid_paths);
b15546f9 1211
fe9cf30e 1212 schedule_work(&m->trigger_event);
1da177e4
LT
1213
1214out:
1215 spin_unlock_irqrestore(&m->lock, flags);
76e33fe4 1216 if (run_queue) {
63d832c3 1217 dm_table_run_md_queue_async(m->ti->table);
7e48c768 1218 process_queued_io_list(m);
76e33fe4 1219 }
1da177e4
LT
1220
1221 return r;
1222}
1223
1224/*
1225 * Fail or reinstate all paths that match the provided struct dm_dev.
1226 */
1227static int action_dev(struct multipath *m, struct dm_dev *dev,
1228 action_fn action)
1229{
19040c0b 1230 int r = -EINVAL;
1da177e4
LT
1231 struct pgpath *pgpath;
1232 struct priority_group *pg;
1233
1234 list_for_each_entry(pg, &m->priority_groups, list) {
1235 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1236 if (pgpath->path.dev == dev)
1237 r = action(pgpath);
1238 }
1239 }
1240
1241 return r;
1242}
1243
1244/*
1245 * Temporarily try to avoid having to use the specified PG
1246 */
1247static void bypass_pg(struct multipath *m, struct priority_group *pg,
be7d31cc 1248 bool bypassed)
1da177e4
LT
1249{
1250 unsigned long flags;
1251
1252 spin_lock_irqsave(&m->lock, flags);
1253
1254 pg->bypassed = bypassed;
1255 m->current_pgpath = NULL;
1256 m->current_pg = NULL;
1257
1258 spin_unlock_irqrestore(&m->lock, flags);
1259
fe9cf30e 1260 schedule_work(&m->trigger_event);
1da177e4
LT
1261}
1262
1263/*
1264 * Switch to using the specified PG from the next I/O that gets mapped
1265 */
1266static int switch_pg_num(struct multipath *m, const char *pgstr)
1267{
1268 struct priority_group *pg;
1269 unsigned pgnum;
1270 unsigned long flags;
31998ef1 1271 char dummy;
1da177e4 1272
31998ef1 1273 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
cc5bd925 1274 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
1da177e4
LT
1275 DMWARN("invalid PG number supplied to switch_pg_num");
1276 return -EINVAL;
1277 }
1278
1279 spin_lock_irqsave(&m->lock, flags);
1280 list_for_each_entry(pg, &m->priority_groups, list) {
be7d31cc 1281 pg->bypassed = false;
1da177e4
LT
1282 if (--pgnum)
1283 continue;
1284
1285 m->current_pgpath = NULL;
1286 m->current_pg = NULL;
1287 m->next_pg = pg;
1288 }
1289 spin_unlock_irqrestore(&m->lock, flags);
1290
fe9cf30e 1291 schedule_work(&m->trigger_event);
1da177e4
LT
1292 return 0;
1293}
1294
1295/*
1296 * Set/clear bypassed status of a PG.
1297 * PGs are numbered upwards from 1 in the order they were declared.
1298 */
be7d31cc 1299static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
1da177e4
LT
1300{
1301 struct priority_group *pg;
1302 unsigned pgnum;
31998ef1 1303 char dummy;
1da177e4 1304
31998ef1 1305 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
cc5bd925 1306 !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
1da177e4
LT
1307 DMWARN("invalid PG number supplied to bypass_pg");
1308 return -EINVAL;
1309 }
1310
1311 list_for_each_entry(pg, &m->priority_groups, list) {
1312 if (!--pgnum)
1313 break;
1314 }
1315
1316 bypass_pg(m, pg, bypassed);
1317 return 0;
1318}
1319
c9e45581
DW
1320/*
1321 * Should we retry pg_init immediately?
1322 */
be7d31cc 1323static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
c9e45581
DW
1324{
1325 unsigned long flags;
be7d31cc 1326 bool limit_reached = false;
c9e45581
DW
1327
1328 spin_lock_irqsave(&m->lock, flags);
1329
91e968aa
MS
1330 if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
1331 !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
518257b1 1332 set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
c9e45581 1333 else
be7d31cc 1334 limit_reached = true;
c9e45581
DW
1335
1336 spin_unlock_irqrestore(&m->lock, flags);
1337
1338 return limit_reached;
1339}
1340
3ae31f6a 1341static void pg_init_done(void *data, int errors)
cfae5c9b 1342{
83c0d5d5 1343 struct pgpath *pgpath = data;
cfae5c9b
CS
1344 struct priority_group *pg = pgpath->pg;
1345 struct multipath *m = pg->m;
1346 unsigned long flags;
be7d31cc 1347 bool delay_retry = false;
cfae5c9b
CS
1348
1349 /* device or driver problems */
1350 switch (errors) {
1351 case SCSI_DH_OK:
1352 break;
1353 case SCSI_DH_NOSYS:
1354 if (!m->hw_handler_name) {
1355 errors = 0;
1356 break;
1357 }
f7b934c8
MB
1358 DMERR("Could not failover the device: Handler scsi_dh_%s "
1359 "Error %d.", m->hw_handler_name, errors);
cfae5c9b
CS
1360 /*
1361 * Fail path for now, so we do not ping pong
1362 */
1363 fail_path(pgpath);
1364 break;
1365 case SCSI_DH_DEV_TEMP_BUSY:
1366 /*
1367 * Probably doing something like FW upgrade on the
1368 * controller so try the other pg.
1369 */
be7d31cc 1370 bypass_pg(m, pg, true);
cfae5c9b 1371 break;
cfae5c9b 1372 case SCSI_DH_RETRY:
4e2d19e4
CS
1373 /* Wait before retrying. */
1374 delay_retry = 1;
cfae5c9b
CS
1375 case SCSI_DH_IMM_RETRY:
1376 case SCSI_DH_RES_TEMP_UNAVAIL:
1377 if (pg_init_limit_reached(m, pgpath))
1378 fail_path(pgpath);
1379 errors = 0;
1380 break;
ec31f3f7 1381 case SCSI_DH_DEV_OFFLINED:
cfae5c9b
CS
1382 default:
1383 /*
1384 * We probably do not want to fail the path for a device
1385 * error, but this is what the old dm did. In future
1386 * patches we can do more advanced handling.
1387 */
1388 fail_path(pgpath);
1389 }
1390
1391 spin_lock_irqsave(&m->lock, flags);
1392 if (errors) {
e54f77dd
CS
1393 if (pgpath == m->current_pgpath) {
1394 DMERR("Could not failover device. Error %d.", errors);
1395 m->current_pgpath = NULL;
1396 m->current_pg = NULL;
1397 }
518257b1 1398 } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
be7d31cc 1399 pg->bypassed = false;
cfae5c9b 1400
91e968aa 1401 if (atomic_dec_return(&m->pg_init_in_progress) > 0)
d0259bf0
KU
1402 /* Activations of other paths are still on going */
1403 goto out;
1404
518257b1
MS
1405 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1406 if (delay_retry)
1407 set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1408 else
1409 clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1410
3e9f1be1
HR
1411 if (__pg_init_all_paths(m))
1412 goto out;
1413 }
518257b1 1414 clear_bit(MPATHF_QUEUE_IO, &m->flags);
d0259bf0 1415
7e48c768 1416 process_queued_io_list(m);
76e33fe4 1417
2bded7bd
KU
1418 /*
1419 * Wake up any thread waiting to suspend.
1420 */
1421 wake_up(&m->pg_init_wait);
1422
d0259bf0 1423out:
cfae5c9b
CS
1424 spin_unlock_irqrestore(&m->lock, flags);
1425}
1426
89bfce76 1427static void activate_or_offline_path(struct pgpath *pgpath)
bab7cfc7 1428{
f10e06b7 1429 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
bab7cfc7 1430
f10e06b7
MS
1431 if (pgpath->is_active && !blk_queue_dying(q))
1432 scsi_dh_activate(q, pg_init_done, pgpath);
3a017509
HR
1433 else
1434 pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
bab7cfc7
CS
1435}
1436
89bfce76
BVA
1437static void activate_path_work(struct work_struct *work)
1438{
1439 struct pgpath *pgpath =
1440 container_of(work, struct pgpath, activate_path.work);
1441
1442 activate_or_offline_path(pgpath);
1443}
1444
7e782af5
HR
1445static int noretry_error(int error)
1446{
1447 switch (error) {
8ff232c1
HR
1448 case -EBADE:
1449 /*
1450 * EBADE signals an reservation conflict.
1451 * We shouldn't fail the path here as we can communicate with
1452 * the target. We should failover to the next path, but in
1453 * doing so we might be causing a ping-pong between paths.
1454 * So just return the reservation conflict error.
1455 */
7e782af5
HR
1456 case -EOPNOTSUPP:
1457 case -EREMOTEIO:
1458 case -EILSEQ:
1459 case -ENODATA:
cc9d3c38 1460 case -ENOSPC:
7e782af5
HR
1461 return 1;
1462 }
1463
1464 /* Anything else could be a path failure, so should be retried */
1465 return 0;
1466}
1467
b79f10ee
CH
1468static int multipath_end_io(struct dm_target *ti, struct request *clone,
1469 int error, union map_info *map_context)
1da177e4 1470{
b79f10ee
CH
1471 struct dm_mpath_io *mpio = get_mpio(map_context);
1472 struct pgpath *pgpath = mpio->pgpath;
7ed8578a 1473 int r = DM_ENDIO_DONE;
b79f10ee 1474
f40c67f0
KU
1475 /*
1476 * We don't queue any clone request inside the multipath target
1477 * during end I/O handling, since those clone requests don't have
1478 * bio clones. If we queue them inside the multipath target,
1479 * we need to make bio clones, that requires memory allocation.
4cc96131 1480 * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
f40c67f0
KU
1481 * don't have bio clones.)
1482 * Instead of queueing the clone request here, we queue the original
1483 * request into dm core, which will remake a clone request and
1484 * clone bios for it and resubmit it later.
1485 */
b79f10ee
CH
1486 if (error && !noretry_error(error)) {
1487 struct multipath *m = ti->private;
1da177e4 1488
7ed8578a 1489 r = DM_ENDIO_REQUEUE;
1da177e4 1490
b79f10ee
CH
1491 if (pgpath)
1492 fail_path(pgpath);
1da177e4 1493
b79f10ee 1494 if (atomic_read(&m->nr_valid_paths) == 0 &&
7ed8578a
CH
1495 !test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
1496 if (error == -EIO)
18a482f5 1497 dm_report_EIO(m);
7ed8578a
CH
1498 /* complete with the original error */
1499 r = DM_ENDIO_DONE;
1500 }
b79f10ee 1501 }
466891f9 1502
1da177e4 1503 if (pgpath) {
b79f10ee
CH
1504 struct path_selector *ps = &pgpath->pg->ps;
1505
1da177e4 1506 if (ps->type->end_io)
02ab823f 1507 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1da177e4 1508 }
1da177e4 1509
7ed8578a 1510 return r;
1da177e4
LT
1511}
1512
76e33fe4
MS
1513static int do_end_io_bio(struct multipath *m, struct bio *clone,
1514 int error, struct dm_mpath_io *mpio)
1515{
1516 unsigned long flags;
1517
1518 if (!error)
1519 return 0; /* I/O complete */
1520
1521 if (noretry_error(error))
1522 return error;
1523
1524 if (mpio->pgpath)
1525 fail_path(mpio->pgpath);
1526
ca5beb76 1527 if (atomic_read(&m->nr_valid_paths) == 0 &&
18a482f5
CH
1528 !test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
1529 dm_report_EIO(m);
1530 return -EIO;
1531 }
76e33fe4
MS
1532
1533 /* Queue for the daemon to resubmit */
bf661be1 1534 dm_bio_restore(get_bio_details_from_bio(clone), clone);
76e33fe4
MS
1535
1536 spin_lock_irqsave(&m->lock, flags);
1537 bio_list_add(&m->queued_bios, clone);
1538 spin_unlock_irqrestore(&m->lock, flags);
1539 if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
1540 queue_work(kmultipathd, &m->process_queued_bios);
1541
1542 return DM_ENDIO_INCOMPLETE;
1543}
1544
1545static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone, int error)
1546{
1547 struct multipath *m = ti->private;
1548 struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
1549 struct pgpath *pgpath;
1550 struct path_selector *ps;
1551 int r;
1552
1553 BUG_ON(!mpio);
1554
1555 r = do_end_io_bio(m, clone, error, mpio);
1556 pgpath = mpio->pgpath;
1557 if (pgpath) {
1558 ps = &pgpath->pg->ps;
1559 if (ps->type->end_io)
1560 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1561 }
1562
1563 return r;
1564}
1565
1da177e4
LT
1566/*
1567 * Suspend can't complete until all the I/O is processed so if
436d4108
AK
1568 * the last path fails we must error any remaining I/O.
1569 * Note that if the freeze_bdev fails while suspending, the
1570 * queue_if_no_path state is lost - userspace should reset it.
1da177e4
LT
1571 */
1572static void multipath_presuspend(struct dm_target *ti)
1573{
7943bd6d 1574 struct multipath *m = ti->private;
1da177e4 1575
be7d31cc 1576 queue_if_no_path(m, false, true);
1da177e4
LT
1577}
1578
6df400ab
KU
1579static void multipath_postsuspend(struct dm_target *ti)
1580{
6380f26f
MA
1581 struct multipath *m = ti->private;
1582
1583 mutex_lock(&m->work_mutex);
2bded7bd 1584 flush_multipath_work(m);
6380f26f 1585 mutex_unlock(&m->work_mutex);
6df400ab
KU
1586}
1587
436d4108
AK
1588/*
1589 * Restore the queue_if_no_path setting.
1590 */
1da177e4
LT
1591static void multipath_resume(struct dm_target *ti)
1592{
7943bd6d 1593 struct multipath *m = ti->private;
1814f2e3 1594 unsigned long flags;
1da177e4 1595
1814f2e3 1596 spin_lock_irqsave(&m->lock, flags);
9a8ac3ae
BVA
1597 assign_bit(test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags),
1598 MPATHF_QUEUE_IF_NO_PATH, &m->flags);
1814f2e3 1599 spin_unlock_irqrestore(&m->lock, flags);
1da177e4
LT
1600}
1601
1602/*
1603 * Info output has the following format:
1604 * num_multipath_feature_args [multipath_feature_args]*
1605 * num_handler_status_args [handler_status_args]*
1606 * num_groups init_group_number
1607 * [A|D|E num_ps_status_args [ps_status_args]*
1608 * num_paths num_selector_args
1609 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1610 *
1611 * Table output has the following format (identical to the constructor string):
1612 * num_feature_args [features_args]*
1613 * num_handler_args hw_handler [hw_handler_args]*
1614 * num_groups init_group_number
1615 * [priority selector-name num_ps_args [ps_args]*
1616 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1617 */
fd7c092e
MP
1618static void multipath_status(struct dm_target *ti, status_type_t type,
1619 unsigned status_flags, char *result, unsigned maxlen)
1da177e4
LT
1620{
1621 int sz = 0;
1622 unsigned long flags;
7943bd6d 1623 struct multipath *m = ti->private;
1da177e4
LT
1624 struct priority_group *pg;
1625 struct pgpath *p;
1626 unsigned pg_num;
1627 char state;
1628
1629 spin_lock_irqsave(&m->lock, flags);
1630
1631 /* Features */
1632 if (type == STATUSTYPE_INFO)
91e968aa
MS
1633 DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
1634 atomic_read(&m->pg_init_count));
c9e45581 1635 else {
518257b1 1636 DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
4e2d19e4 1637 (m->pg_init_retries > 0) * 2 +
a58a935d 1638 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
e83068a5
MS
1639 test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
1640 (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
1641
518257b1 1642 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
c9e45581
DW
1643 DMEMIT("queue_if_no_path ");
1644 if (m->pg_init_retries)
1645 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
4e2d19e4
CS
1646 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1647 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
518257b1 1648 if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
a58a935d 1649 DMEMIT("retain_attached_hw_handler ");
e83068a5
MS
1650 if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
1651 switch(m->queue_mode) {
1652 case DM_TYPE_BIO_BASED:
1653 DMEMIT("queue_mode bio ");
1654 break;
1655 case DM_TYPE_MQ_REQUEST_BASED:
1656 DMEMIT("queue_mode mq ");
1657 break;
7e0d574f
BVA
1658 default:
1659 WARN_ON_ONCE(true);
1660 break;
e83068a5
MS
1661 }
1662 }
c9e45581 1663 }
1da177e4 1664
cfae5c9b 1665 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1da177e4
LT
1666 DMEMIT("0 ");
1667 else
cfae5c9b 1668 DMEMIT("1 %s ", m->hw_handler_name);
1da177e4
LT
1669
1670 DMEMIT("%u ", m->nr_priority_groups);
1671
1672 if (m->next_pg)
1673 pg_num = m->next_pg->pg_num;
1674 else if (m->current_pg)
1675 pg_num = m->current_pg->pg_num;
1676 else
a490a07a 1677 pg_num = (m->nr_priority_groups ? 1 : 0);
1da177e4
LT
1678
1679 DMEMIT("%u ", pg_num);
1680
1681 switch (type) {
1682 case STATUSTYPE_INFO:
1683 list_for_each_entry(pg, &m->priority_groups, list) {
1684 if (pg->bypassed)
1685 state = 'D'; /* Disabled */
1686 else if (pg == m->current_pg)
1687 state = 'A'; /* Currently Active */
1688 else
1689 state = 'E'; /* Enabled */
1690
1691 DMEMIT("%c ", state);
1692
1693 if (pg->ps.type->status)
1694 sz += pg->ps.type->status(&pg->ps, NULL, type,
1695 result + sz,
1696 maxlen - sz);
1697 else
1698 DMEMIT("0 ");
1699
1700 DMEMIT("%u %u ", pg->nr_pgpaths,
1701 pg->ps.type->info_args);
1702
1703 list_for_each_entry(p, &pg->pgpaths, list) {
1704 DMEMIT("%s %s %u ", p->path.dev->name,
6680073d 1705 p->is_active ? "A" : "F",
1da177e4
LT
1706 p->fail_count);
1707 if (pg->ps.type->status)
1708 sz += pg->ps.type->status(&pg->ps,
1709 &p->path, type, result + sz,
1710 maxlen - sz);
1711 }
1712 }
1713 break;
1714
1715 case STATUSTYPE_TABLE:
1716 list_for_each_entry(pg, &m->priority_groups, list) {
1717 DMEMIT("%s ", pg->ps.type->name);
1718
1719 if (pg->ps.type->status)
1720 sz += pg->ps.type->status(&pg->ps, NULL, type,
1721 result + sz,
1722 maxlen - sz);
1723 else
1724 DMEMIT("0 ");
1725
1726 DMEMIT("%u %u ", pg->nr_pgpaths,
1727 pg->ps.type->table_args);
1728
1729 list_for_each_entry(p, &pg->pgpaths, list) {
1730 DMEMIT("%s ", p->path.dev->name);
1731 if (pg->ps.type->status)
1732 sz += pg->ps.type->status(&pg->ps,
1733 &p->path, type, result + sz,
1734 maxlen - sz);
1735 }
1736 }
1737 break;
1738 }
1739
1740 spin_unlock_irqrestore(&m->lock, flags);
1da177e4
LT
1741}
1742
1743static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1744{
6380f26f 1745 int r = -EINVAL;
1da177e4 1746 struct dm_dev *dev;
7943bd6d 1747 struct multipath *m = ti->private;
1da177e4
LT
1748 action_fn action;
1749
6380f26f
MA
1750 mutex_lock(&m->work_mutex);
1751
c2f3d24b
KU
1752 if (dm_suspended(ti)) {
1753 r = -EBUSY;
1754 goto out;
1755 }
1756
1da177e4 1757 if (argc == 1) {
498f0103 1758 if (!strcasecmp(argv[0], "queue_if_no_path")) {
be7d31cc 1759 r = queue_if_no_path(m, true, false);
6380f26f 1760 goto out;
498f0103 1761 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
be7d31cc 1762 r = queue_if_no_path(m, false, false);
6380f26f
MA
1763 goto out;
1764 }
1da177e4
LT
1765 }
1766
6380f26f 1767 if (argc != 2) {
a356e426 1768 DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
6380f26f
MA
1769 goto out;
1770 }
1da177e4 1771
498f0103 1772 if (!strcasecmp(argv[0], "disable_group")) {
be7d31cc 1773 r = bypass_pg_num(m, argv[1], true);
6380f26f 1774 goto out;
498f0103 1775 } else if (!strcasecmp(argv[0], "enable_group")) {
be7d31cc 1776 r = bypass_pg_num(m, argv[1], false);
6380f26f 1777 goto out;
498f0103 1778 } else if (!strcasecmp(argv[0], "switch_group")) {
6380f26f
MA
1779 r = switch_pg_num(m, argv[1]);
1780 goto out;
498f0103 1781 } else if (!strcasecmp(argv[0], "reinstate_path"))
1da177e4 1782 action = reinstate_path;
498f0103 1783 else if (!strcasecmp(argv[0], "fail_path"))
1da177e4 1784 action = fail_path;
6380f26f 1785 else {
a356e426 1786 DMWARN("Unrecognised multipath message received: %s", argv[0]);
6380f26f
MA
1787 goto out;
1788 }
1da177e4 1789
8215d6ec 1790 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
1da177e4 1791 if (r) {
72d94861 1792 DMWARN("message: error getting device %s",
1da177e4 1793 argv[1]);
6380f26f 1794 goto out;
1da177e4
LT
1795 }
1796
1797 r = action_dev(m, dev, action);
1798
1799 dm_put_device(ti, dev);
1800
6380f26f
MA
1801out:
1802 mutex_unlock(&m->work_mutex);
1da177e4 1803 return r;
1da177e4
LT
1804}
1805
e56f81e0
CH
1806static int multipath_prepare_ioctl(struct dm_target *ti,
1807 struct block_device **bdev, fmode_t *mode)
9af4aa30 1808{
35991652 1809 struct multipath *m = ti->private;
2da1610a 1810 struct pgpath *current_pgpath;
35991652
MP
1811 int r;
1812
2da1610a
MS
1813 current_pgpath = lockless_dereference(m->current_pgpath);
1814 if (!current_pgpath)
1815 current_pgpath = choose_pgpath(m, 0);
9af4aa30 1816
2da1610a 1817 if (current_pgpath) {
518257b1 1818 if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
2da1610a
MS
1819 *bdev = current_pgpath->path.dev->bdev;
1820 *mode = current_pgpath->path.dev->mode;
43e43c9e
JN
1821 r = 0;
1822 } else {
1823 /* pg_init has not started or completed */
1824 r = -ENOTCONN;
1825 }
1826 } else {
1827 /* No path is available */
518257b1 1828 if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
43e43c9e
JN
1829 r = -ENOTCONN;
1830 else
1831 r = -EIO;
e90dae1f 1832 }
9af4aa30 1833
5bbbfdf6 1834 if (r == -ENOTCONN) {
2da1610a 1835 if (!lockless_dereference(m->current_pg)) {
3e9f1be1 1836 /* Path status changed, redo selection */
2da1610a 1837 (void) choose_pgpath(m, 0);
3e9f1be1 1838 }
518257b1 1839 if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
2da1610a 1840 pg_init_all_paths(m);
63d832c3 1841 dm_table_run_md_queue_async(m->ti->table);
7e48c768 1842 process_queued_io_list(m);
3e9f1be1 1843 }
35991652 1844
e56f81e0
CH
1845 /*
1846 * Only pass ioctls through if the device sizes match exactly.
1847 */
1848 if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1849 return 1;
1850 return r;
9af4aa30
MB
1851}
1852
af4874e0
MS
1853static int multipath_iterate_devices(struct dm_target *ti,
1854 iterate_devices_callout_fn fn, void *data)
1855{
1856 struct multipath *m = ti->private;
1857 struct priority_group *pg;
1858 struct pgpath *p;
1859 int ret = 0;
1860
1861 list_for_each_entry(pg, &m->priority_groups, list) {
1862 list_for_each_entry(p, &pg->pgpaths, list) {
5dea271b 1863 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
af4874e0
MS
1864 if (ret)
1865 goto out;
1866 }
1867 }
1868
1869out:
1870 return ret;
1871}
1872
9f54cec5 1873static int pgpath_busy(struct pgpath *pgpath)
f40c67f0
KU
1874{
1875 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1876
52b09914 1877 return blk_lld_busy(q);
f40c67f0
KU
1878}
1879
1880/*
1881 * We return "busy", only when we can map I/Os but underlying devices
1882 * are busy (so even if we map I/Os now, the I/Os will wait on
1883 * the underlying queue).
1884 * In other words, if we want to kill I/Os or queue them inside us
1885 * due to map unavailability, we don't return "busy". Otherwise,
1886 * dm core won't give us the I/Os and we can't do what we want.
1887 */
1888static int multipath_busy(struct dm_target *ti)
1889{
be7d31cc 1890 bool busy = false, has_active = false;
f40c67f0 1891 struct multipath *m = ti->private;
2da1610a 1892 struct priority_group *pg, *next_pg;
f40c67f0 1893 struct pgpath *pgpath;
f40c67f0 1894
b88efd43
MS
1895 /* pg_init in progress */
1896 if (atomic_read(&m->pg_init_in_progress))
2da1610a
MS
1897 return true;
1898
b88efd43
MS
1899 /* no paths available, for blk-mq: rely on IO mapping to delay requeue */
1900 if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1901 return (m->queue_mode != DM_TYPE_MQ_REQUEST_BASED);
1902
f40c67f0 1903 /* Guess which priority_group will be used at next mapping time */
2da1610a
MS
1904 pg = lockless_dereference(m->current_pg);
1905 next_pg = lockless_dereference(m->next_pg);
1906 if (unlikely(!lockless_dereference(m->current_pgpath) && next_pg))
1907 pg = next_pg;
1908
1909 if (!pg) {
f40c67f0
KU
1910 /*
1911 * We don't know which pg will be used at next mapping time.
2da1610a 1912 * We don't call choose_pgpath() here to avoid to trigger
f40c67f0
KU
1913 * pg_init just by busy checking.
1914 * So we don't know whether underlying devices we will be using
1915 * at next mapping time are busy or not. Just try mapping.
1916 */
2da1610a
MS
1917 return busy;
1918 }
f40c67f0
KU
1919
1920 /*
1921 * If there is one non-busy active path at least, the path selector
1922 * will be able to select it. So we consider such a pg as not busy.
1923 */
be7d31cc 1924 busy = true;
2da1610a 1925 list_for_each_entry(pgpath, &pg->pgpaths, list) {
f40c67f0 1926 if (pgpath->is_active) {
be7d31cc 1927 has_active = true;
9f54cec5 1928 if (!pgpath_busy(pgpath)) {
be7d31cc 1929 busy = false;
f40c67f0
KU
1930 break;
1931 }
1932 }
2da1610a 1933 }
f40c67f0 1934
2da1610a 1935 if (!has_active) {
f40c67f0
KU
1936 /*
1937 * No active path in this pg, so this pg won't be used and
1938 * the current_pg will be changed at next mapping time.
1939 * We need to try mapping to determine it.
1940 */
be7d31cc 1941 busy = false;
2da1610a 1942 }
f40c67f0
KU
1943
1944 return busy;
1945}
1946
1da177e4
LT
1947/*-----------------------------------------------------------------
1948 * Module setup
1949 *---------------------------------------------------------------*/
1950static struct target_type multipath_target = {
1951 .name = "multipath",
e83068a5 1952 .version = {1, 12, 0},
16f12266 1953 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
1da177e4
LT
1954 .module = THIS_MODULE,
1955 .ctr = multipath_ctr,
1956 .dtr = multipath_dtr,
e5863d9a
MS
1957 .clone_and_map_rq = multipath_clone_and_map,
1958 .release_clone_rq = multipath_release_clone,
f40c67f0 1959 .rq_end_io = multipath_end_io,
76e33fe4
MS
1960 .map = multipath_map_bio,
1961 .end_io = multipath_end_io_bio,
1962 .presuspend = multipath_presuspend,
1963 .postsuspend = multipath_postsuspend,
1964 .resume = multipath_resume,
1965 .status = multipath_status,
1966 .message = multipath_message,
1967 .prepare_ioctl = multipath_prepare_ioctl,
1968 .iterate_devices = multipath_iterate_devices,
1969 .busy = multipath_busy,
1970};
1971
1da177e4
LT
1972static int __init dm_multipath_init(void)
1973{
1974 int r;
1975
1da177e4
LT
1976 r = dm_register_target(&multipath_target);
1977 if (r < 0) {
76e33fe4 1978 DMERR("request-based register failed %d", r);
ff658e9c
JT
1979 r = -EINVAL;
1980 goto bad_register_target;
1da177e4
LT
1981 }
1982
4d4d66ab 1983 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
c557308e 1984 if (!kmultipathd) {
0cd33124 1985 DMERR("failed to create workqueue kmpathd");
ff658e9c
JT
1986 r = -ENOMEM;
1987 goto bad_alloc_kmultipathd;
c557308e
AK
1988 }
1989
bab7cfc7
CS
1990 /*
1991 * A separate workqueue is used to handle the device handlers
1992 * to avoid overloading existing workqueue. Overloading the
1993 * old workqueue would also create a bottleneck in the
1994 * path of the storage hardware device activation.
1995 */
4d4d66ab
TH
1996 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
1997 WQ_MEM_RECLAIM);
bab7cfc7
CS
1998 if (!kmpath_handlerd) {
1999 DMERR("failed to create workqueue kmpath_handlerd");
ff658e9c
JT
2000 r = -ENOMEM;
2001 goto bad_alloc_kmpath_handlerd;
bab7cfc7
CS
2002 }
2003
ff658e9c
JT
2004 return 0;
2005
2006bad_alloc_kmpath_handlerd:
2007 destroy_workqueue(kmultipathd);
2008bad_alloc_kmultipathd:
2009 dm_unregister_target(&multipath_target);
2010bad_register_target:
1da177e4
LT
2011 return r;
2012}
2013
2014static void __exit dm_multipath_exit(void)
2015{
bab7cfc7 2016 destroy_workqueue(kmpath_handlerd);
c557308e
AK
2017 destroy_workqueue(kmultipathd);
2018
10d3bd09 2019 dm_unregister_target(&multipath_target);
1da177e4
LT
2020}
2021
1da177e4
LT
2022module_init(dm_multipath_init);
2023module_exit(dm_multipath_exit);
2024
2025MODULE_DESCRIPTION(DM_NAME " multipath target");
2026MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
2027MODULE_LICENSE("GPL");