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