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