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