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