xref: /openbmc/linux/drivers/md/dm-mpath.c (revision 436d41087d047b61f8ab0604dc74fff3240a8933)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2003 Sistina Software Limited.
31da177e4SLinus Torvalds  * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * This file is released under the GPL.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include "dm.h"
91da177e4SLinus Torvalds #include "dm-path-selector.h"
101da177e4SLinus Torvalds #include "dm-hw-handler.h"
111da177e4SLinus Torvalds #include "dm-bio-list.h"
121da177e4SLinus Torvalds #include "dm-bio-record.h"
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/ctype.h>
151da177e4SLinus Torvalds #include <linux/init.h>
161da177e4SLinus Torvalds #include <linux/mempool.h>
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/pagemap.h>
191da177e4SLinus Torvalds #include <linux/slab.h>
201da177e4SLinus Torvalds #include <linux/time.h>
211da177e4SLinus Torvalds #include <linux/workqueue.h>
221da177e4SLinus Torvalds #include <asm/atomic.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #define MESG_STR(x) x, sizeof(x)
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds /* Path properties */
271da177e4SLinus Torvalds struct pgpath {
281da177e4SLinus Torvalds 	struct list_head list;
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds 	struct priority_group *pg;	/* Owning PG */
311da177e4SLinus Torvalds 	unsigned fail_count;		/* Cumulative failure count */
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds 	struct path path;
341da177e4SLinus Torvalds };
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /*
391da177e4SLinus Torvalds  * Paths are grouped into Priority Groups and numbered from 1 upwards.
401da177e4SLinus Torvalds  * Each has a path selector which controls which path gets used.
411da177e4SLinus Torvalds  */
421da177e4SLinus Torvalds struct priority_group {
431da177e4SLinus Torvalds 	struct list_head list;
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds 	struct multipath *m;		/* Owning multipath instance */
461da177e4SLinus Torvalds 	struct path_selector ps;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds 	unsigned pg_num;		/* Reference number */
491da177e4SLinus Torvalds 	unsigned bypassed;		/* Temporarily bypass this PG? */
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds 	unsigned nr_pgpaths;		/* Number of paths in PG */
521da177e4SLinus Torvalds 	struct list_head pgpaths;
531da177e4SLinus Torvalds };
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /* Multipath context */
561da177e4SLinus Torvalds struct multipath {
571da177e4SLinus Torvalds 	struct list_head list;
581da177e4SLinus Torvalds 	struct dm_target *ti;
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	spinlock_t lock;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	struct hw_handler hw_handler;
631da177e4SLinus Torvalds 	unsigned nr_priority_groups;
641da177e4SLinus Torvalds 	struct list_head priority_groups;
651da177e4SLinus Torvalds 	unsigned pg_init_required;	/* pg_init needs calling? */
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds 	unsigned nr_valid_paths;	/* Total number of usable paths */
681da177e4SLinus Torvalds 	struct pgpath *current_pgpath;
691da177e4SLinus Torvalds 	struct priority_group *current_pg;
701da177e4SLinus Torvalds 	struct priority_group *next_pg;	/* Switch to this PG if set */
711da177e4SLinus Torvalds 	unsigned repeat_count;		/* I/Os left before calling PS again */
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	unsigned queue_io;		/* Must we queue all I/O? */
741da177e4SLinus Torvalds 	unsigned queue_if_no_path;	/* Queue I/O if last path fails? */
75*436d4108SAlasdair G Kergon 	unsigned saved_queue_if_no_path;/* Saved state during suspension */
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	struct work_struct process_queued_ios;
781da177e4SLinus Torvalds 	struct bio_list queued_ios;
791da177e4SLinus Torvalds 	unsigned queue_size;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	struct work_struct trigger_event;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	/*
841da177e4SLinus Torvalds 	 * We must use a mempool of mpath_io structs so that we
851da177e4SLinus Torvalds 	 * can resubmit bios on error.
861da177e4SLinus Torvalds 	 */
871da177e4SLinus Torvalds 	mempool_t *mpio_pool;
881da177e4SLinus Torvalds };
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /*
911da177e4SLinus Torvalds  * Context information attached to each bio we process.
921da177e4SLinus Torvalds  */
931da177e4SLinus Torvalds struct mpath_io {
941da177e4SLinus Torvalds 	struct pgpath *pgpath;
951da177e4SLinus Torvalds 	struct dm_bio_details details;
961da177e4SLinus Torvalds };
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath);
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #define MIN_IOS 256	/* Mempool size */
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds static kmem_cache_t *_mpio_cache;
1031da177e4SLinus Torvalds 
104c557308eSAlasdair G Kergon struct workqueue_struct *kmultipathd;
1051da177e4SLinus Torvalds static void process_queued_ios(void *data);
1061da177e4SLinus Torvalds static void trigger_event(void *data);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds /*-----------------------------------------------
1101da177e4SLinus Torvalds  * Allocation routines
1111da177e4SLinus Torvalds  *-----------------------------------------------*/
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds 	struct pgpath *pgpath = kmalloc(sizeof(*pgpath), GFP_KERNEL);
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	if (pgpath) {
1181da177e4SLinus Torvalds 		memset(pgpath, 0, sizeof(*pgpath));
1191da177e4SLinus Torvalds 		pgpath->path.is_active = 1;
1201da177e4SLinus Torvalds 	}
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	return pgpath;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds static inline void free_pgpath(struct pgpath *pgpath)
1261da177e4SLinus Torvalds {
1271da177e4SLinus Torvalds 	kfree(pgpath);
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void)
1311da177e4SLinus Torvalds {
1321da177e4SLinus Torvalds 	struct priority_group *pg;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	pg = kmalloc(sizeof(*pg), GFP_KERNEL);
1351da177e4SLinus Torvalds 	if (!pg)
1361da177e4SLinus Torvalds 		return NULL;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	memset(pg, 0, sizeof(*pg));
1391da177e4SLinus Torvalds 	INIT_LIST_HEAD(&pg->pgpaths);
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	return pg;
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	struct pgpath *pgpath, *tmp;
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
1491da177e4SLinus Torvalds 		list_del(&pgpath->list);
1501da177e4SLinus Torvalds 		dm_put_device(ti, pgpath->path.dev);
1511da177e4SLinus Torvalds 		free_pgpath(pgpath);
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg,
1561da177e4SLinus Torvalds 				struct dm_target *ti)
1571da177e4SLinus Torvalds {
1581da177e4SLinus Torvalds 	struct path_selector *ps = &pg->ps;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	if (ps->type) {
1611da177e4SLinus Torvalds 		ps->type->destroy(ps);
1621da177e4SLinus Torvalds 		dm_put_path_selector(ps->type);
1631da177e4SLinus Torvalds 	}
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	free_pgpaths(&pg->pgpaths, ti);
1661da177e4SLinus Torvalds 	kfree(pg);
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds static struct multipath *alloc_multipath(void)
1701da177e4SLinus Torvalds {
1711da177e4SLinus Torvalds 	struct multipath *m;
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	m = kmalloc(sizeof(*m), GFP_KERNEL);
1741da177e4SLinus Torvalds 	if (m) {
1751da177e4SLinus Torvalds 		memset(m, 0, sizeof(*m));
1761da177e4SLinus Torvalds 		INIT_LIST_HEAD(&m->priority_groups);
1771da177e4SLinus Torvalds 		spin_lock_init(&m->lock);
1781da177e4SLinus Torvalds 		m->queue_io = 1;
1791da177e4SLinus Torvalds 		INIT_WORK(&m->process_queued_ios, process_queued_ios, m);
1801da177e4SLinus Torvalds 		INIT_WORK(&m->trigger_event, trigger_event, m);
1811da177e4SLinus Torvalds 		m->mpio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
1821da177e4SLinus Torvalds 					      mempool_free_slab, _mpio_cache);
1831da177e4SLinus Torvalds 		if (!m->mpio_pool) {
1841da177e4SLinus Torvalds 			kfree(m);
1851da177e4SLinus Torvalds 			return NULL;
1861da177e4SLinus Torvalds 		}
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	return m;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds static void free_multipath(struct multipath *m)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	struct priority_group *pg, *tmp;
1951da177e4SLinus Torvalds 	struct hw_handler *hwh = &m->hw_handler;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
1981da177e4SLinus Torvalds 		list_del(&pg->list);
1991da177e4SLinus Torvalds 		free_priority_group(pg, m->ti);
2001da177e4SLinus Torvalds 	}
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	if (hwh->type) {
2031da177e4SLinus Torvalds 		hwh->type->destroy(hwh);
2041da177e4SLinus Torvalds 		dm_put_hw_handler(hwh->type);
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	mempool_destroy(m->mpio_pool);
2081da177e4SLinus Torvalds 	kfree(m);
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds /*-----------------------------------------------
2131da177e4SLinus Torvalds  * Path selection
2141da177e4SLinus Torvalds  *-----------------------------------------------*/
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
2171da177e4SLinus Torvalds {
2181da177e4SLinus Torvalds 	struct hw_handler *hwh = &m->hw_handler;
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	m->current_pg = pgpath->pg;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	/* Must we initialise the PG first, and queue I/O till it's ready? */
2231da177e4SLinus Torvalds 	if (hwh->type && hwh->type->pg_init) {
2241da177e4SLinus Torvalds 		m->pg_init_required = 1;
2251da177e4SLinus Torvalds 		m->queue_io = 1;
2261da177e4SLinus Torvalds 	} else {
2271da177e4SLinus Torvalds 		m->pg_init_required = 0;
2281da177e4SLinus Torvalds 		m->queue_io = 0;
2291da177e4SLinus Torvalds 	}
2301da177e4SLinus Torvalds }
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
2331da177e4SLinus Torvalds {
2341da177e4SLinus Torvalds 	struct path *path;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
2371da177e4SLinus Torvalds 	if (!path)
2381da177e4SLinus Torvalds 		return -ENXIO;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	m->current_pgpath = path_to_pgpath(path);
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	if (m->current_pg != pg)
2431da177e4SLinus Torvalds 		__switch_pg(m, m->current_pgpath);
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	return 0;
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds static void __choose_pgpath(struct multipath *m)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds 	struct priority_group *pg;
2511da177e4SLinus Torvalds 	unsigned bypassed = 1;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	if (!m->nr_valid_paths)
2541da177e4SLinus Torvalds 		goto failed;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	/* Were we instructed to switch PG? */
2571da177e4SLinus Torvalds 	if (m->next_pg) {
2581da177e4SLinus Torvalds 		pg = m->next_pg;
2591da177e4SLinus Torvalds 		m->next_pg = NULL;
2601da177e4SLinus Torvalds 		if (!__choose_path_in_pg(m, pg))
2611da177e4SLinus Torvalds 			return;
2621da177e4SLinus Torvalds 	}
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	/* Don't change PG until it has no remaining paths */
2651da177e4SLinus Torvalds 	if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
2661da177e4SLinus Torvalds 		return;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	/*
2691da177e4SLinus Torvalds 	 * Loop through priority groups until we find a valid path.
2701da177e4SLinus Torvalds 	 * First time we skip PGs marked 'bypassed'.
2711da177e4SLinus Torvalds 	 * Second time we only try the ones we skipped.
2721da177e4SLinus Torvalds 	 */
2731da177e4SLinus Torvalds 	do {
2741da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
2751da177e4SLinus Torvalds 			if (pg->bypassed == bypassed)
2761da177e4SLinus Torvalds 				continue;
2771da177e4SLinus Torvalds 			if (!__choose_path_in_pg(m, pg))
2781da177e4SLinus Torvalds 				return;
2791da177e4SLinus Torvalds 		}
2801da177e4SLinus Torvalds 	} while (bypassed--);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds failed:
2831da177e4SLinus Torvalds 	m->current_pgpath = NULL;
2841da177e4SLinus Torvalds 	m->current_pg = NULL;
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds static int map_io(struct multipath *m, struct bio *bio, struct mpath_io *mpio,
2881da177e4SLinus Torvalds 		  unsigned was_queued)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds 	int r = 1;
2911da177e4SLinus Torvalds 	unsigned long flags;
2921da177e4SLinus Torvalds 	struct pgpath *pgpath;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	/* Do we need to select a new pgpath? */
2971da177e4SLinus Torvalds 	if (!m->current_pgpath ||
2981da177e4SLinus Torvalds 	    (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
2991da177e4SLinus Torvalds 		__choose_pgpath(m);
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	pgpath = m->current_pgpath;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	if (was_queued)
3041da177e4SLinus Torvalds 		m->queue_size--;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	if ((pgpath && m->queue_io) ||
307*436d4108SAlasdair G Kergon 	    (!pgpath && m->queue_if_no_path)) {
3081da177e4SLinus Torvalds 		/* Queue for the daemon to resubmit */
3091da177e4SLinus Torvalds 		bio_list_add(&m->queued_ios, bio);
3101da177e4SLinus Torvalds 		m->queue_size++;
3111da177e4SLinus Torvalds 		if (m->pg_init_required || !m->queue_io)
312c557308eSAlasdair G Kergon 			queue_work(kmultipathd, &m->process_queued_ios);
3131da177e4SLinus Torvalds 		pgpath = NULL;
3141da177e4SLinus Torvalds 		r = 0;
3151da177e4SLinus Torvalds 	} else if (!pgpath)
3161da177e4SLinus Torvalds 		r = -EIO;		/* Failed */
3171da177e4SLinus Torvalds 	else
3181da177e4SLinus Torvalds 		bio->bi_bdev = pgpath->path.dev->bdev;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	mpio->pgpath = pgpath;
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	return r;
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds /*
3281da177e4SLinus Torvalds  * If we run out of usable paths, should we queue I/O or error it?
3291da177e4SLinus Torvalds  */
3301da177e4SLinus Torvalds static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	unsigned long flags;
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
3351da177e4SLinus Torvalds 
336*436d4108SAlasdair G Kergon 	m->saved_queue_if_no_path = m->queue_if_no_path;
3371da177e4SLinus Torvalds 	m->queue_if_no_path = queue_if_no_path;
3381da177e4SLinus Torvalds 	if (!m->queue_if_no_path)
339c557308eSAlasdair G Kergon 		queue_work(kmultipathd, &m->process_queued_ios);
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	return 0;
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds /*-----------------------------------------------------------------
3471da177e4SLinus Torvalds  * The multipath daemon is responsible for resubmitting queued ios.
3481da177e4SLinus Torvalds  *---------------------------------------------------------------*/
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds static void dispatch_queued_ios(struct multipath *m)
3511da177e4SLinus Torvalds {
3521da177e4SLinus Torvalds 	int r;
3531da177e4SLinus Torvalds 	unsigned long flags;
3541da177e4SLinus Torvalds 	struct bio *bio = NULL, *next;
3551da177e4SLinus Torvalds 	struct mpath_io *mpio;
3561da177e4SLinus Torvalds 	union map_info *info;
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
3591da177e4SLinus Torvalds 	bio = bio_list_get(&m->queued_ios);
3601da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	while (bio) {
3631da177e4SLinus Torvalds 		next = bio->bi_next;
3641da177e4SLinus Torvalds 		bio->bi_next = NULL;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 		info = dm_get_mapinfo(bio);
3671da177e4SLinus Torvalds 		mpio = info->ptr;
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 		r = map_io(m, bio, mpio, 1);
3701da177e4SLinus Torvalds 		if (r < 0)
3711da177e4SLinus Torvalds 			bio_endio(bio, bio->bi_size, r);
3721da177e4SLinus Torvalds 		else if (r == 1)
3731da177e4SLinus Torvalds 			generic_make_request(bio);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 		bio = next;
3761da177e4SLinus Torvalds 	}
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds static void process_queued_ios(void *data)
3801da177e4SLinus Torvalds {
3811da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) data;
3821da177e4SLinus Torvalds 	struct hw_handler *hwh = &m->hw_handler;
3831da177e4SLinus Torvalds 	struct pgpath *pgpath;
3841da177e4SLinus Torvalds 	unsigned init_required, must_queue = 0;
3851da177e4SLinus Torvalds 	unsigned long flags;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	if (!m->current_pgpath)
3901da177e4SLinus Torvalds 		__choose_pgpath(m);
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 	pgpath = m->current_pgpath;
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	if ((pgpath && m->queue_io) ||
395*436d4108SAlasdair G Kergon 	    (!pgpath && m->queue_if_no_path))
3961da177e4SLinus Torvalds 		must_queue = 1;
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	init_required = m->pg_init_required;
3991da177e4SLinus Torvalds 	if (init_required)
4001da177e4SLinus Torvalds 		m->pg_init_required = 0;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	if (init_required)
4051da177e4SLinus Torvalds 		hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	if (!must_queue)
4081da177e4SLinus Torvalds 		dispatch_queued_ios(m);
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds /*
4121da177e4SLinus Torvalds  * An event is triggered whenever a path is taken out of use.
4131da177e4SLinus Torvalds  * Includes path failure and PG bypass.
4141da177e4SLinus Torvalds  */
4151da177e4SLinus Torvalds static void trigger_event(void *data)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) data;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	dm_table_event(m->ti->table);
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds /*-----------------------------------------------------------------
4231da177e4SLinus Torvalds  * Constructor/argument parsing:
4241da177e4SLinus Torvalds  * <#multipath feature args> [<arg>]*
4251da177e4SLinus Torvalds  * <#hw_handler args> [hw_handler [<arg>]*]
4261da177e4SLinus Torvalds  * <#priority groups>
4271da177e4SLinus Torvalds  * <initial priority group>
4281da177e4SLinus Torvalds  *     [<selector> <#selector args> [<arg>]*
4291da177e4SLinus Torvalds  *      <#paths> <#per-path selector args>
4301da177e4SLinus Torvalds  *         [<path> [<arg>]* ]+ ]+
4311da177e4SLinus Torvalds  *---------------------------------------------------------------*/
4321da177e4SLinus Torvalds struct param {
4331da177e4SLinus Torvalds 	unsigned min;
4341da177e4SLinus Torvalds 	unsigned max;
4351da177e4SLinus Torvalds 	char *error;
4361da177e4SLinus Torvalds };
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds #define ESTR(s) ("dm-multipath: " s)
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds static int read_param(struct param *param, char *str, unsigned *v, char **error)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds 	if (!str ||
4431da177e4SLinus Torvalds 	    (sscanf(str, "%u", v) != 1) ||
4441da177e4SLinus Torvalds 	    (*v < param->min) ||
4451da177e4SLinus Torvalds 	    (*v > param->max)) {
4461da177e4SLinus Torvalds 		*error = param->error;
4471da177e4SLinus Torvalds 		return -EINVAL;
4481da177e4SLinus Torvalds 	}
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	return 0;
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds struct arg_set {
4541da177e4SLinus Torvalds 	unsigned argc;
4551da177e4SLinus Torvalds 	char **argv;
4561da177e4SLinus Torvalds };
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds static char *shift(struct arg_set *as)
4591da177e4SLinus Torvalds {
4601da177e4SLinus Torvalds 	char *r;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	if (as->argc) {
4631da177e4SLinus Torvalds 		as->argc--;
4641da177e4SLinus Torvalds 		r = *as->argv;
4651da177e4SLinus Torvalds 		as->argv++;
4661da177e4SLinus Torvalds 		return r;
4671da177e4SLinus Torvalds 	}
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	return NULL;
4701da177e4SLinus Torvalds }
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds static void consume(struct arg_set *as, unsigned n)
4731da177e4SLinus Torvalds {
4741da177e4SLinus Torvalds 	BUG_ON (as->argc < n);
4751da177e4SLinus Torvalds 	as->argc -= n;
4761da177e4SLinus Torvalds 	as->argv += n;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
4801da177e4SLinus Torvalds 			       struct dm_target *ti)
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds 	int r;
4831da177e4SLinus Torvalds 	struct path_selector_type *pst;
4841da177e4SLinus Torvalds 	unsigned ps_argc;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	static struct param _params[] = {
4871da177e4SLinus Torvalds 		{0, 1024, ESTR("invalid number of path selector args")},
4881da177e4SLinus Torvalds 	};
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	pst = dm_get_path_selector(shift(as));
4911da177e4SLinus Torvalds 	if (!pst) {
4921da177e4SLinus Torvalds 		ti->error = ESTR("unknown path selector type");
4931da177e4SLinus Torvalds 		return -EINVAL;
4941da177e4SLinus Torvalds 	}
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	r = read_param(_params, shift(as), &ps_argc, &ti->error);
4971da177e4SLinus Torvalds 	if (r)
4981da177e4SLinus Torvalds 		return -EINVAL;
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds 	r = pst->create(&pg->ps, ps_argc, as->argv);
5011da177e4SLinus Torvalds 	if (r) {
5021da177e4SLinus Torvalds 		dm_put_path_selector(pst);
5031da177e4SLinus Torvalds 		ti->error = ESTR("path selector constructor failed");
5041da177e4SLinus Torvalds 		return r;
5051da177e4SLinus Torvalds 	}
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	pg->ps.type = pst;
5081da177e4SLinus Torvalds 	consume(as, ps_argc);
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	return 0;
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
5141da177e4SLinus Torvalds 			       struct dm_target *ti)
5151da177e4SLinus Torvalds {
5161da177e4SLinus Torvalds 	int r;
5171da177e4SLinus Torvalds 	struct pgpath *p;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	/* we need at least a path arg */
5201da177e4SLinus Torvalds 	if (as->argc < 1) {
5211da177e4SLinus Torvalds 		ti->error = ESTR("no device given");
5221da177e4SLinus Torvalds 		return NULL;
5231da177e4SLinus Torvalds 	}
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	p = alloc_pgpath();
5261da177e4SLinus Torvalds 	if (!p)
5271da177e4SLinus Torvalds 		return NULL;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	r = dm_get_device(ti, shift(as), ti->begin, ti->len,
5301da177e4SLinus Torvalds 			  dm_table_get_mode(ti->table), &p->path.dev);
5311da177e4SLinus Torvalds 	if (r) {
5321da177e4SLinus Torvalds 		ti->error = ESTR("error getting device");
5331da177e4SLinus Torvalds 		goto bad;
5341da177e4SLinus Torvalds 	}
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
5371da177e4SLinus Torvalds 	if (r) {
5381da177e4SLinus Torvalds 		dm_put_device(ti, p->path.dev);
5391da177e4SLinus Torvalds 		goto bad;
5401da177e4SLinus Torvalds 	}
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	return p;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds  bad:
5451da177e4SLinus Torvalds 	free_pgpath(p);
5461da177e4SLinus Torvalds 	return NULL;
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds static struct priority_group *parse_priority_group(struct arg_set *as,
5501da177e4SLinus Torvalds 						   struct multipath *m,
5511da177e4SLinus Torvalds 						   struct dm_target *ti)
5521da177e4SLinus Torvalds {
5531da177e4SLinus Torvalds 	static struct param _params[] = {
5541da177e4SLinus Torvalds 		{1, 1024, ESTR("invalid number of paths")},
5551da177e4SLinus Torvalds 		{0, 1024, ESTR("invalid number of selector args")}
5561da177e4SLinus Torvalds 	};
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	int r;
5591da177e4SLinus Torvalds 	unsigned i, nr_selector_args, nr_params;
5601da177e4SLinus Torvalds 	struct priority_group *pg;
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	if (as->argc < 2) {
5631da177e4SLinus Torvalds 		as->argc = 0;
5641da177e4SLinus Torvalds 		ti->error = ESTR("not enough priority group aruments");
5651da177e4SLinus Torvalds 		return NULL;
5661da177e4SLinus Torvalds 	}
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	pg = alloc_priority_group();
5691da177e4SLinus Torvalds 	if (!pg) {
5701da177e4SLinus Torvalds 		ti->error = ESTR("couldn't allocate priority group");
5711da177e4SLinus Torvalds 		return NULL;
5721da177e4SLinus Torvalds 	}
5731da177e4SLinus Torvalds 	pg->m = m;
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	r = parse_path_selector(as, pg, ti);
5761da177e4SLinus Torvalds 	if (r)
5771da177e4SLinus Torvalds 		goto bad;
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	/*
5801da177e4SLinus Torvalds 	 * read the paths
5811da177e4SLinus Torvalds 	 */
5821da177e4SLinus Torvalds 	r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
5831da177e4SLinus Torvalds 	if (r)
5841da177e4SLinus Torvalds 		goto bad;
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
5871da177e4SLinus Torvalds 	if (r)
5881da177e4SLinus Torvalds 		goto bad;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	nr_params = 1 + nr_selector_args;
5911da177e4SLinus Torvalds 	for (i = 0; i < pg->nr_pgpaths; i++) {
5921da177e4SLinus Torvalds 		struct pgpath *pgpath;
5931da177e4SLinus Torvalds 		struct arg_set path_args;
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 		if (as->argc < nr_params)
5961da177e4SLinus Torvalds 			goto bad;
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 		path_args.argc = nr_params;
5991da177e4SLinus Torvalds 		path_args.argv = as->argv;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 		pgpath = parse_path(&path_args, &pg->ps, ti);
6021da177e4SLinus Torvalds 		if (!pgpath)
6031da177e4SLinus Torvalds 			goto bad;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 		pgpath->pg = pg;
6061da177e4SLinus Torvalds 		list_add_tail(&pgpath->list, &pg->pgpaths);
6071da177e4SLinus Torvalds 		consume(as, nr_params);
6081da177e4SLinus Torvalds 	}
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	return pg;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds  bad:
6131da177e4SLinus Torvalds 	free_priority_group(pg, ti);
6141da177e4SLinus Torvalds 	return NULL;
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds static int parse_hw_handler(struct arg_set *as, struct multipath *m,
6181da177e4SLinus Torvalds 			    struct dm_target *ti)
6191da177e4SLinus Torvalds {
6201da177e4SLinus Torvalds 	int r;
6211da177e4SLinus Torvalds 	struct hw_handler_type *hwht;
6221da177e4SLinus Torvalds 	unsigned hw_argc;
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	static struct param _params[] = {
6251da177e4SLinus Torvalds 		{0, 1024, ESTR("invalid number of hardware handler args")},
6261da177e4SLinus Torvalds 	};
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	r = read_param(_params, shift(as), &hw_argc, &ti->error);
6291da177e4SLinus Torvalds 	if (r)
6301da177e4SLinus Torvalds 		return -EINVAL;
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	if (!hw_argc)
6331da177e4SLinus Torvalds 		return 0;
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	hwht = dm_get_hw_handler(shift(as));
6361da177e4SLinus Torvalds 	if (!hwht) {
6371da177e4SLinus Torvalds 		ti->error = ESTR("unknown hardware handler type");
6381da177e4SLinus Torvalds 		return -EINVAL;
6391da177e4SLinus Torvalds 	}
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
6421da177e4SLinus Torvalds 	if (r) {
6431da177e4SLinus Torvalds 		dm_put_hw_handler(hwht);
6441da177e4SLinus Torvalds 		ti->error = ESTR("hardware handler constructor failed");
6451da177e4SLinus Torvalds 		return r;
6461da177e4SLinus Torvalds 	}
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	m->hw_handler.type = hwht;
6491da177e4SLinus Torvalds 	consume(as, hw_argc - 1);
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	return 0;
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds static int parse_features(struct arg_set *as, struct multipath *m,
6551da177e4SLinus Torvalds 			  struct dm_target *ti)
6561da177e4SLinus Torvalds {
6571da177e4SLinus Torvalds 	int r;
6581da177e4SLinus Torvalds 	unsigned argc;
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	static struct param _params[] = {
6611da177e4SLinus Torvalds 		{0, 1, ESTR("invalid number of feature args")},
6621da177e4SLinus Torvalds 	};
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	r = read_param(_params, shift(as), &argc, &ti->error);
6651da177e4SLinus Torvalds 	if (r)
6661da177e4SLinus Torvalds 		return -EINVAL;
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 	if (!argc)
6691da177e4SLinus Torvalds 		return 0;
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 	if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
6721da177e4SLinus Torvalds 		return queue_if_no_path(m, 1);
6731da177e4SLinus Torvalds 	else {
6741da177e4SLinus Torvalds 		ti->error = "Unrecognised multipath feature request";
6751da177e4SLinus Torvalds 		return -EINVAL;
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds }
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds static int multipath_ctr(struct dm_target *ti, unsigned int argc,
6801da177e4SLinus Torvalds 			 char **argv)
6811da177e4SLinus Torvalds {
6821da177e4SLinus Torvalds 	/* target parameters */
6831da177e4SLinus Torvalds 	static struct param _params[] = {
6841da177e4SLinus Torvalds 		{1, 1024, ESTR("invalid number of priority groups")},
6851da177e4SLinus Torvalds 		{1, 1024, ESTR("invalid initial priority group number")},
6861da177e4SLinus Torvalds 	};
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	int r;
6891da177e4SLinus Torvalds 	struct multipath *m;
6901da177e4SLinus Torvalds 	struct arg_set as;
6911da177e4SLinus Torvalds 	unsigned pg_count = 0;
6921da177e4SLinus Torvalds 	unsigned next_pg_num;
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	as.argc = argc;
6951da177e4SLinus Torvalds 	as.argv = argv;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	m = alloc_multipath();
6981da177e4SLinus Torvalds 	if (!m) {
6991da177e4SLinus Torvalds 		ti->error = ESTR("can't allocate multipath");
7001da177e4SLinus Torvalds 		return -EINVAL;
7011da177e4SLinus Torvalds 	}
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	r = parse_features(&as, m, ti);
7041da177e4SLinus Torvalds 	if (r)
7051da177e4SLinus Torvalds 		goto bad;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	r = parse_hw_handler(&as, m, ti);
7081da177e4SLinus Torvalds 	if (r)
7091da177e4SLinus Torvalds 		goto bad;
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
7121da177e4SLinus Torvalds 	if (r)
7131da177e4SLinus Torvalds 		goto bad;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
7161da177e4SLinus Torvalds 	if (r)
7171da177e4SLinus Torvalds 		goto bad;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds 	/* parse the priority groups */
7201da177e4SLinus Torvalds 	while (as.argc) {
7211da177e4SLinus Torvalds 		struct priority_group *pg;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 		pg = parse_priority_group(&as, m, ti);
7241da177e4SLinus Torvalds 		if (!pg) {
7251da177e4SLinus Torvalds 			r = -EINVAL;
7261da177e4SLinus Torvalds 			goto bad;
7271da177e4SLinus Torvalds 		}
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 		m->nr_valid_paths += pg->nr_pgpaths;
7301da177e4SLinus Torvalds 		list_add_tail(&pg->list, &m->priority_groups);
7311da177e4SLinus Torvalds 		pg_count++;
7321da177e4SLinus Torvalds 		pg->pg_num = pg_count;
7331da177e4SLinus Torvalds 		if (!--next_pg_num)
7341da177e4SLinus Torvalds 			m->next_pg = pg;
7351da177e4SLinus Torvalds 	}
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds 	if (pg_count != m->nr_priority_groups) {
7381da177e4SLinus Torvalds 		ti->error = ESTR("priority group count mismatch");
7391da177e4SLinus Torvalds 		r = -EINVAL;
7401da177e4SLinus Torvalds 		goto bad;
7411da177e4SLinus Torvalds 	}
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 	ti->private = m;
7441da177e4SLinus Torvalds 	m->ti = ti;
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	return 0;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds  bad:
7491da177e4SLinus Torvalds 	free_multipath(m);
7501da177e4SLinus Torvalds 	return r;
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds static void multipath_dtr(struct dm_target *ti)
7541da177e4SLinus Torvalds {
7551da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
756a044d016SAlasdair G Kergon 
757a044d016SAlasdair G Kergon 	flush_workqueue(kmultipathd);
7581da177e4SLinus Torvalds 	free_multipath(m);
7591da177e4SLinus Torvalds }
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds /*
7621da177e4SLinus Torvalds  * Map bios, recording original fields for later in case we have to resubmit
7631da177e4SLinus Torvalds  */
7641da177e4SLinus Torvalds static int multipath_map(struct dm_target *ti, struct bio *bio,
7651da177e4SLinus Torvalds 			 union map_info *map_context)
7661da177e4SLinus Torvalds {
7671da177e4SLinus Torvalds 	int r;
7681da177e4SLinus Torvalds 	struct mpath_io *mpio;
7691da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
7701da177e4SLinus Torvalds 
771f6a80ea8SAlasdair G Kergon 	if (bio_barrier(bio))
772f6a80ea8SAlasdair G Kergon 		return -EOPNOTSUPP;
773f6a80ea8SAlasdair G Kergon 
7741da177e4SLinus Torvalds 	mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
7751da177e4SLinus Torvalds 	dm_bio_record(&mpio->details, bio);
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	map_context->ptr = mpio;
7781da177e4SLinus Torvalds 	bio->bi_rw |= (1 << BIO_RW_FAILFAST);
7791da177e4SLinus Torvalds 	r = map_io(m, bio, mpio, 0);
7801da177e4SLinus Torvalds 	if (r < 0)
7811da177e4SLinus Torvalds 		mempool_free(mpio, m->mpio_pool);
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	return r;
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds /*
7871da177e4SLinus Torvalds  * Take a path out of use.
7881da177e4SLinus Torvalds  */
7891da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath)
7901da177e4SLinus Torvalds {
7911da177e4SLinus Torvalds 	unsigned long flags;
7921da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	if (!pgpath->path.is_active)
7971da177e4SLinus Torvalds 		goto out;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	DMWARN("dm-multipath: Failing path %s.", pgpath->path.dev->name);
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
8021da177e4SLinus Torvalds 	pgpath->path.is_active = 0;
8031da177e4SLinus Torvalds 	pgpath->fail_count++;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	m->nr_valid_paths--;
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	if (pgpath == m->current_pgpath)
8081da177e4SLinus Torvalds 		m->current_pgpath = NULL;
8091da177e4SLinus Torvalds 
810c557308eSAlasdair G Kergon 	queue_work(kmultipathd, &m->trigger_event);
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds out:
8131da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	return 0;
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds /*
8191da177e4SLinus Torvalds  * Reinstate a previously-failed path
8201da177e4SLinus Torvalds  */
8211da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	int r = 0;
8241da177e4SLinus Torvalds 	unsigned long flags;
8251da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	if (pgpath->path.is_active)
8301da177e4SLinus Torvalds 		goto out;
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	if (!pgpath->pg->ps.type) {
8331da177e4SLinus Torvalds 		DMWARN("Reinstate path not supported by path selector %s",
8341da177e4SLinus Torvalds 		       pgpath->pg->ps.type->name);
8351da177e4SLinus Torvalds 		r = -EINVAL;
8361da177e4SLinus Torvalds 		goto out;
8371da177e4SLinus Torvalds 	}
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 	r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
8401da177e4SLinus Torvalds 	if (r)
8411da177e4SLinus Torvalds 		goto out;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	pgpath->path.is_active = 1;
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	m->current_pgpath = NULL;
8461da177e4SLinus Torvalds 	if (!m->nr_valid_paths++)
847c557308eSAlasdair G Kergon 		queue_work(kmultipathd, &m->process_queued_ios);
8481da177e4SLinus Torvalds 
849c557308eSAlasdair G Kergon 	queue_work(kmultipathd, &m->trigger_event);
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds out:
8521da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	return r;
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds /*
8581da177e4SLinus Torvalds  * Fail or reinstate all paths that match the provided struct dm_dev.
8591da177e4SLinus Torvalds  */
8601da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev,
8611da177e4SLinus Torvalds 		      action_fn action)
8621da177e4SLinus Torvalds {
8631da177e4SLinus Torvalds 	int r = 0;
8641da177e4SLinus Torvalds 	struct pgpath *pgpath;
8651da177e4SLinus Torvalds 	struct priority_group *pg;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
8681da177e4SLinus Torvalds 		list_for_each_entry(pgpath, &pg->pgpaths, list) {
8691da177e4SLinus Torvalds 			if (pgpath->path.dev == dev)
8701da177e4SLinus Torvalds 				r = action(pgpath);
8711da177e4SLinus Torvalds 		}
8721da177e4SLinus Torvalds 	}
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	return r;
8751da177e4SLinus Torvalds }
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds /*
8781da177e4SLinus Torvalds  * Temporarily try to avoid having to use the specified PG
8791da177e4SLinus Torvalds  */
8801da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg,
8811da177e4SLinus Torvalds 		      int bypassed)
8821da177e4SLinus Torvalds {
8831da177e4SLinus Torvalds 	unsigned long flags;
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	pg->bypassed = bypassed;
8881da177e4SLinus Torvalds 	m->current_pgpath = NULL;
8891da177e4SLinus Torvalds 	m->current_pg = NULL;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
8921da177e4SLinus Torvalds 
893c557308eSAlasdair G Kergon 	queue_work(kmultipathd, &m->trigger_event);
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds /*
8971da177e4SLinus Torvalds  * Switch to using the specified PG from the next I/O that gets mapped
8981da177e4SLinus Torvalds  */
8991da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr)
9001da177e4SLinus Torvalds {
9011da177e4SLinus Torvalds 	struct priority_group *pg;
9021da177e4SLinus Torvalds 	unsigned pgnum;
9031da177e4SLinus Torvalds 	unsigned long flags;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
9061da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
9071da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to switch_pg_num");
9081da177e4SLinus Torvalds 		return -EINVAL;
9091da177e4SLinus Torvalds 	}
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9121da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
9131da177e4SLinus Torvalds 		pg->bypassed = 0;
9141da177e4SLinus Torvalds 		if (--pgnum)
9151da177e4SLinus Torvalds 			continue;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 		m->current_pgpath = NULL;
9181da177e4SLinus Torvalds 		m->current_pg = NULL;
9191da177e4SLinus Torvalds 		m->next_pg = pg;
9201da177e4SLinus Torvalds 	}
9211da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
9221da177e4SLinus Torvalds 
923c557308eSAlasdair G Kergon 	queue_work(kmultipathd, &m->trigger_event);
9241da177e4SLinus Torvalds 	return 0;
9251da177e4SLinus Torvalds }
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds /*
9281da177e4SLinus Torvalds  * Set/clear bypassed status of a PG.
9291da177e4SLinus Torvalds  * PGs are numbered upwards from 1 in the order they were declared.
9301da177e4SLinus Torvalds  */
9311da177e4SLinus Torvalds static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
9321da177e4SLinus Torvalds {
9331da177e4SLinus Torvalds 	struct priority_group *pg;
9341da177e4SLinus Torvalds 	unsigned pgnum;
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
9371da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
9381da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to bypass_pg");
9391da177e4SLinus Torvalds 		return -EINVAL;
9401da177e4SLinus Torvalds 	}
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
9431da177e4SLinus Torvalds 		if (!--pgnum)
9441da177e4SLinus Torvalds 			break;
9451da177e4SLinus Torvalds 	}
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	bypass_pg(m, pg, bypassed);
9481da177e4SLinus Torvalds 	return 0;
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds /*
9521da177e4SLinus Torvalds  * pg_init must call this when it has completed its initialisation
9531da177e4SLinus Torvalds  */
9541da177e4SLinus Torvalds void dm_pg_init_complete(struct path *path, unsigned err_flags)
9551da177e4SLinus Torvalds {
9561da177e4SLinus Torvalds 	struct pgpath *pgpath = path_to_pgpath(path);
9571da177e4SLinus Torvalds 	struct priority_group *pg = pgpath->pg;
9581da177e4SLinus Torvalds 	struct multipath *m = pg->m;
9591da177e4SLinus Torvalds 	unsigned long flags;
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	/* We insist on failing the path if the PG is already bypassed. */
9621da177e4SLinus Torvalds 	if (err_flags && pg->bypassed)
9631da177e4SLinus Torvalds 		err_flags |= MP_FAIL_PATH;
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	if (err_flags & MP_FAIL_PATH)
9661da177e4SLinus Torvalds 		fail_path(pgpath);
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	if (err_flags & MP_BYPASS_PG)
9691da177e4SLinus Torvalds 		bypass_pg(m, pg, 1);
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9721da177e4SLinus Torvalds 	if (!err_flags)
9731da177e4SLinus Torvalds 		m->queue_io = 0;
9741da177e4SLinus Torvalds 	else {
9751da177e4SLinus Torvalds 		m->current_pgpath = NULL;
9761da177e4SLinus Torvalds 		m->current_pg = NULL;
9771da177e4SLinus Torvalds 	}
978c557308eSAlasdair G Kergon 	queue_work(kmultipathd, &m->process_queued_ios);
9791da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds /*
9831da177e4SLinus Torvalds  * end_io handling
9841da177e4SLinus Torvalds  */
9851da177e4SLinus Torvalds static int do_end_io(struct multipath *m, struct bio *bio,
9861da177e4SLinus Torvalds 		     int error, struct mpath_io *mpio)
9871da177e4SLinus Torvalds {
9881da177e4SLinus Torvalds 	struct hw_handler *hwh = &m->hw_handler;
9891da177e4SLinus Torvalds 	unsigned err_flags = MP_FAIL_PATH;	/* Default behavior */
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	if (!error)
9921da177e4SLinus Torvalds 		return 0;	/* I/O complete */
9931da177e4SLinus Torvalds 
9944f58802fSLars Marowsky-Bree 	if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
9954f58802fSLars Marowsky-Bree 		return error;
9964f58802fSLars Marowsky-Bree 
997f6a80ea8SAlasdair G Kergon 	if (error == -EOPNOTSUPP)
998f6a80ea8SAlasdair G Kergon 		return error;
999f6a80ea8SAlasdair G Kergon 
10001da177e4SLinus Torvalds 	spin_lock(&m->lock);
10011da177e4SLinus Torvalds 	if (!m->nr_valid_paths) {
1002*436d4108SAlasdair G Kergon 		if (!m->queue_if_no_path) {
10031da177e4SLinus Torvalds 			spin_unlock(&m->lock);
10041da177e4SLinus Torvalds 			return -EIO;
10051da177e4SLinus Torvalds 		} else {
10061da177e4SLinus Torvalds 			spin_unlock(&m->lock);
10071da177e4SLinus Torvalds 			goto requeue;
10081da177e4SLinus Torvalds 		}
10091da177e4SLinus Torvalds 	}
10101da177e4SLinus Torvalds 	spin_unlock(&m->lock);
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	if (hwh->type && hwh->type->error)
10131da177e4SLinus Torvalds 		err_flags = hwh->type->error(hwh, bio);
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 	if (mpio->pgpath) {
10161da177e4SLinus Torvalds 		if (err_flags & MP_FAIL_PATH)
10171da177e4SLinus Torvalds 			fail_path(mpio->pgpath);
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 		if (err_flags & MP_BYPASS_PG)
10201da177e4SLinus Torvalds 			bypass_pg(m, mpio->pgpath->pg, 1);
10211da177e4SLinus Torvalds 	}
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	if (err_flags & MP_ERROR_IO)
10241da177e4SLinus Torvalds 		return -EIO;
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds       requeue:
10271da177e4SLinus Torvalds 	dm_bio_restore(&mpio->details, bio);
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds 	/* queue for the daemon to resubmit or fail */
10301da177e4SLinus Torvalds 	spin_lock(&m->lock);
10311da177e4SLinus Torvalds 	bio_list_add(&m->queued_ios, bio);
10321da177e4SLinus Torvalds 	m->queue_size++;
10331da177e4SLinus Torvalds 	if (!m->queue_io)
1034c557308eSAlasdair G Kergon 		queue_work(kmultipathd, &m->process_queued_ios);
10351da177e4SLinus Torvalds 	spin_unlock(&m->lock);
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 	return 1;	/* io not complete */
10381da177e4SLinus Torvalds }
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds static int multipath_end_io(struct dm_target *ti, struct bio *bio,
10411da177e4SLinus Torvalds 			    int error, union map_info *map_context)
10421da177e4SLinus Torvalds {
10431da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
10441da177e4SLinus Torvalds 	struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
10451da177e4SLinus Torvalds 	struct pgpath *pgpath = mpio->pgpath;
10461da177e4SLinus Torvalds 	struct path_selector *ps;
10471da177e4SLinus Torvalds 	int r;
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 	r  = do_end_io(m, bio, error, mpio);
10501da177e4SLinus Torvalds 	if (pgpath) {
10511da177e4SLinus Torvalds 		ps = &pgpath->pg->ps;
10521da177e4SLinus Torvalds 		if (ps->type->end_io)
10531da177e4SLinus Torvalds 			ps->type->end_io(ps, &pgpath->path);
10541da177e4SLinus Torvalds 	}
10551da177e4SLinus Torvalds 	if (r <= 0)
10561da177e4SLinus Torvalds 		mempool_free(mpio, m->mpio_pool);
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	return r;
10591da177e4SLinus Torvalds }
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds /*
10621da177e4SLinus Torvalds  * Suspend can't complete until all the I/O is processed so if
1063*436d4108SAlasdair G Kergon  * the last path fails we must error any remaining I/O.
1064*436d4108SAlasdair G Kergon  * Note that if the freeze_bdev fails while suspending, the
1065*436d4108SAlasdair G Kergon  * queue_if_no_path state is lost - userspace should reset it.
10661da177e4SLinus Torvalds  */
10671da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti)
10681da177e4SLinus Torvalds {
10691da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
10701da177e4SLinus Torvalds 
1071*436d4108SAlasdair G Kergon 	queue_if_no_path(m, 0);
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
1074*436d4108SAlasdair G Kergon /*
1075*436d4108SAlasdair G Kergon  * Restore the queue_if_no_path setting.
1076*436d4108SAlasdair G Kergon  */
10771da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti)
10781da177e4SLinus Torvalds {
10791da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
10801da177e4SLinus Torvalds 	unsigned long flags;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
1083*436d4108SAlasdair G Kergon 	m->queue_if_no_path = m->saved_queue_if_no_path;
10841da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds /*
10881da177e4SLinus Torvalds  * Info output has the following format:
10891da177e4SLinus Torvalds  * num_multipath_feature_args [multipath_feature_args]*
10901da177e4SLinus Torvalds  * num_handler_status_args [handler_status_args]*
10911da177e4SLinus Torvalds  * num_groups init_group_number
10921da177e4SLinus Torvalds  *            [A|D|E num_ps_status_args [ps_status_args]*
10931da177e4SLinus Torvalds  *             num_paths num_selector_args
10941da177e4SLinus Torvalds  *             [path_dev A|F fail_count [selector_args]* ]+ ]+
10951da177e4SLinus Torvalds  *
10961da177e4SLinus Torvalds  * Table output has the following format (identical to the constructor string):
10971da177e4SLinus Torvalds  * num_feature_args [features_args]*
10981da177e4SLinus Torvalds  * num_handler_args hw_handler [hw_handler_args]*
10991da177e4SLinus Torvalds  * num_groups init_group_number
11001da177e4SLinus Torvalds  *     [priority selector-name num_ps_args [ps_args]*
11011da177e4SLinus Torvalds  *      num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
11021da177e4SLinus Torvalds  */
11031da177e4SLinus Torvalds static int multipath_status(struct dm_target *ti, status_type_t type,
11041da177e4SLinus Torvalds 			    char *result, unsigned int maxlen)
11051da177e4SLinus Torvalds {
11061da177e4SLinus Torvalds 	int sz = 0;
11071da177e4SLinus Torvalds 	unsigned long flags;
11081da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
11091da177e4SLinus Torvalds 	struct hw_handler *hwh = &m->hw_handler;
11101da177e4SLinus Torvalds 	struct priority_group *pg;
11111da177e4SLinus Torvalds 	struct pgpath *p;
11121da177e4SLinus Torvalds 	unsigned pg_num;
11131da177e4SLinus Torvalds 	char state;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	/* Features */
11181da177e4SLinus Torvalds 	if (type == STATUSTYPE_INFO)
11191da177e4SLinus Torvalds 		DMEMIT("1 %u ", m->queue_size);
11201da177e4SLinus Torvalds 	else if (m->queue_if_no_path)
11211da177e4SLinus Torvalds 		DMEMIT("1 queue_if_no_path ");
11221da177e4SLinus Torvalds 	else
11231da177e4SLinus Torvalds 		DMEMIT("0 ");
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	if (hwh->type && hwh->type->status)
11261da177e4SLinus Torvalds 		sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
11271da177e4SLinus Torvalds 	else if (!hwh->type || type == STATUSTYPE_INFO)
11281da177e4SLinus Torvalds 		DMEMIT("0 ");
11291da177e4SLinus Torvalds 	else
11301da177e4SLinus Torvalds 		DMEMIT("1 %s ", hwh->type->name);
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	DMEMIT("%u ", m->nr_priority_groups);
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	if (m->next_pg)
11351da177e4SLinus Torvalds 		pg_num = m->next_pg->pg_num;
11361da177e4SLinus Torvalds 	else if (m->current_pg)
11371da177e4SLinus Torvalds 		pg_num = m->current_pg->pg_num;
11381da177e4SLinus Torvalds 	else
11391da177e4SLinus Torvalds 			pg_num = 1;
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	DMEMIT("%u ", pg_num);
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds 	switch (type) {
11441da177e4SLinus Torvalds 	case STATUSTYPE_INFO:
11451da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
11461da177e4SLinus Torvalds 			if (pg->bypassed)
11471da177e4SLinus Torvalds 				state = 'D';	/* Disabled */
11481da177e4SLinus Torvalds 			else if (pg == m->current_pg)
11491da177e4SLinus Torvalds 				state = 'A';	/* Currently Active */
11501da177e4SLinus Torvalds 			else
11511da177e4SLinus Torvalds 				state = 'E';	/* Enabled */
11521da177e4SLinus Torvalds 
11531da177e4SLinus Torvalds 			DMEMIT("%c ", state);
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 			if (pg->ps.type->status)
11561da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
11571da177e4SLinus Torvalds 							  result + sz,
11581da177e4SLinus Torvalds 							  maxlen - sz);
11591da177e4SLinus Torvalds 			else
11601da177e4SLinus Torvalds 				DMEMIT("0 ");
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
11631da177e4SLinus Torvalds 			       pg->ps.type->info_args);
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
11661da177e4SLinus Torvalds 				DMEMIT("%s %s %u ", p->path.dev->name,
11671da177e4SLinus Torvalds 				       p->path.is_active ? "A" : "F",
11681da177e4SLinus Torvalds 				       p->fail_count);
11691da177e4SLinus Torvalds 				if (pg->ps.type->status)
11701da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
11711da177e4SLinus Torvalds 					      &p->path, type, result + sz,
11721da177e4SLinus Torvalds 					      maxlen - sz);
11731da177e4SLinus Torvalds 			}
11741da177e4SLinus Torvalds 		}
11751da177e4SLinus Torvalds 		break;
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	case STATUSTYPE_TABLE:
11781da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
11791da177e4SLinus Torvalds 			DMEMIT("%s ", pg->ps.type->name);
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 			if (pg->ps.type->status)
11821da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
11831da177e4SLinus Torvalds 							  result + sz,
11841da177e4SLinus Torvalds 							  maxlen - sz);
11851da177e4SLinus Torvalds 			else
11861da177e4SLinus Torvalds 				DMEMIT("0 ");
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
11891da177e4SLinus Torvalds 			       pg->ps.type->table_args);
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
11921da177e4SLinus Torvalds 				DMEMIT("%s ", p->path.dev->name);
11931da177e4SLinus Torvalds 				if (pg->ps.type->status)
11941da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
11951da177e4SLinus Torvalds 					      &p->path, type, result + sz,
11961da177e4SLinus Torvalds 					      maxlen - sz);
11971da177e4SLinus Torvalds 			}
11981da177e4SLinus Torvalds 		}
11991da177e4SLinus Torvalds 		break;
12001da177e4SLinus Torvalds 	}
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 	return 0;
12051da177e4SLinus Torvalds }
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
12081da177e4SLinus Torvalds {
12091da177e4SLinus Torvalds 	int r;
12101da177e4SLinus Torvalds 	struct dm_dev *dev;
12111da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
12121da177e4SLinus Torvalds 	action_fn action;
12131da177e4SLinus Torvalds 
12141da177e4SLinus Torvalds 	if (argc == 1) {
12151da177e4SLinus Torvalds 		if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
12161da177e4SLinus Torvalds 			return queue_if_no_path(m, 1);
12171da177e4SLinus Torvalds 		else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
12181da177e4SLinus Torvalds 			return queue_if_no_path(m, 0);
12191da177e4SLinus Torvalds 	}
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	if (argc != 2)
12221da177e4SLinus Torvalds 		goto error;
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	if (!strnicmp(argv[0], MESG_STR("disable_group")))
12251da177e4SLinus Torvalds 		return bypass_pg_num(m, argv[1], 1);
12261da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("enable_group")))
12271da177e4SLinus Torvalds 		return bypass_pg_num(m, argv[1], 0);
12281da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("switch_group")))
12291da177e4SLinus Torvalds 		return switch_pg_num(m, argv[1]);
12301da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
12311da177e4SLinus Torvalds 		action = reinstate_path;
12321da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("fail_path")))
12331da177e4SLinus Torvalds 		action = fail_path;
12341da177e4SLinus Torvalds 	else
12351da177e4SLinus Torvalds 		goto error;
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	r = dm_get_device(ti, argv[1], ti->begin, ti->len,
12381da177e4SLinus Torvalds 			  dm_table_get_mode(ti->table), &dev);
12391da177e4SLinus Torvalds 	if (r) {
12401da177e4SLinus Torvalds 		DMWARN("dm-multipath message: error getting device %s",
12411da177e4SLinus Torvalds 		       argv[1]);
12421da177e4SLinus Torvalds 		return -EINVAL;
12431da177e4SLinus Torvalds 	}
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	r = action_dev(m, dev, action);
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	dm_put_device(ti, dev);
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	return r;
12501da177e4SLinus Torvalds 
12511da177e4SLinus Torvalds error:
12521da177e4SLinus Torvalds 	DMWARN("Unrecognised multipath message received.");
12531da177e4SLinus Torvalds 	return -EINVAL;
12541da177e4SLinus Torvalds }
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds /*-----------------------------------------------------------------
12571da177e4SLinus Torvalds  * Module setup
12581da177e4SLinus Torvalds  *---------------------------------------------------------------*/
12591da177e4SLinus Torvalds static struct target_type multipath_target = {
12601da177e4SLinus Torvalds 	.name = "multipath",
12611da177e4SLinus Torvalds 	.version = {1, 0, 4},
12621da177e4SLinus Torvalds 	.module = THIS_MODULE,
12631da177e4SLinus Torvalds 	.ctr = multipath_ctr,
12641da177e4SLinus Torvalds 	.dtr = multipath_dtr,
12651da177e4SLinus Torvalds 	.map = multipath_map,
12661da177e4SLinus Torvalds 	.end_io = multipath_end_io,
12671da177e4SLinus Torvalds 	.presuspend = multipath_presuspend,
12681da177e4SLinus Torvalds 	.resume = multipath_resume,
12691da177e4SLinus Torvalds 	.status = multipath_status,
12701da177e4SLinus Torvalds 	.message = multipath_message,
12711da177e4SLinus Torvalds };
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds static int __init dm_multipath_init(void)
12741da177e4SLinus Torvalds {
12751da177e4SLinus Torvalds 	int r;
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
12781da177e4SLinus Torvalds 	_mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
12791da177e4SLinus Torvalds 					0, 0, NULL, NULL);
12801da177e4SLinus Torvalds 	if (!_mpio_cache)
12811da177e4SLinus Torvalds 		return -ENOMEM;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	r = dm_register_target(&multipath_target);
12841da177e4SLinus Torvalds 	if (r < 0) {
12851da177e4SLinus Torvalds 		DMERR("%s: register failed %d", multipath_target.name, r);
12861da177e4SLinus Torvalds 		kmem_cache_destroy(_mpio_cache);
12871da177e4SLinus Torvalds 		return -EINVAL;
12881da177e4SLinus Torvalds 	}
12891da177e4SLinus Torvalds 
1290c557308eSAlasdair G Kergon 	kmultipathd = create_workqueue("kmpathd");
1291c557308eSAlasdair G Kergon 	if (!kmultipathd) {
1292c557308eSAlasdair G Kergon 		DMERR("%s: failed to create workqueue kmpathd",
1293c557308eSAlasdair G Kergon 				multipath_target.name);
1294c557308eSAlasdair G Kergon 		dm_unregister_target(&multipath_target);
1295c557308eSAlasdair G Kergon 		kmem_cache_destroy(_mpio_cache);
1296c557308eSAlasdair G Kergon 		return -ENOMEM;
1297c557308eSAlasdair G Kergon 	}
1298c557308eSAlasdair G Kergon 
12991da177e4SLinus Torvalds 	DMINFO("dm-multipath version %u.%u.%u loaded",
13001da177e4SLinus Torvalds 	       multipath_target.version[0], multipath_target.version[1],
13011da177e4SLinus Torvalds 	       multipath_target.version[2]);
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds 	return r;
13041da177e4SLinus Torvalds }
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds static void __exit dm_multipath_exit(void)
13071da177e4SLinus Torvalds {
13081da177e4SLinus Torvalds 	int r;
13091da177e4SLinus Torvalds 
1310c557308eSAlasdair G Kergon 	destroy_workqueue(kmultipathd);
1311c557308eSAlasdair G Kergon 
13121da177e4SLinus Torvalds 	r = dm_unregister_target(&multipath_target);
13131da177e4SLinus Torvalds 	if (r < 0)
13141da177e4SLinus Torvalds 		DMERR("%s: target unregister failed %d",
13151da177e4SLinus Torvalds 		      multipath_target.name, r);
13161da177e4SLinus Torvalds 	kmem_cache_destroy(_mpio_cache);
13171da177e4SLinus Torvalds }
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(dm_pg_init_complete);
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds module_init(dm_multipath_init);
13221da177e4SLinus Torvalds module_exit(dm_multipath_exit);
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target");
13251da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
13261da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1327