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