xref: /openbmc/linux/drivers/md/dm-mpath.c (revision f6a80ea8ed44de0b19c42d41928be37a186a3f41)
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? */
751da177e4SLinus Torvalds 	unsigned suspended;		/* Has dm core suspended our I/O? */
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) ||
3071da177e4SLinus Torvalds 	    (!pgpath && m->queue_if_no_path && !m->suspended)) {
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 
3361da177e4SLinus Torvalds 	m->queue_if_no_path = queue_if_no_path;
3371da177e4SLinus Torvalds 	if (!m->queue_if_no_path)
338c557308eSAlasdair G Kergon 		queue_work(kmultipathd, &m->process_queued_ios);
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	return 0;
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds /*-----------------------------------------------------------------
3461da177e4SLinus Torvalds  * The multipath daemon is responsible for resubmitting queued ios.
3471da177e4SLinus Torvalds  *---------------------------------------------------------------*/
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds static void dispatch_queued_ios(struct multipath *m)
3501da177e4SLinus Torvalds {
3511da177e4SLinus Torvalds 	int r;
3521da177e4SLinus Torvalds 	unsigned long flags;
3531da177e4SLinus Torvalds 	struct bio *bio = NULL, *next;
3541da177e4SLinus Torvalds 	struct mpath_io *mpio;
3551da177e4SLinus Torvalds 	union map_info *info;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
3581da177e4SLinus Torvalds 	bio = bio_list_get(&m->queued_ios);
3591da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	while (bio) {
3621da177e4SLinus Torvalds 		next = bio->bi_next;
3631da177e4SLinus Torvalds 		bio->bi_next = NULL;
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 		info = dm_get_mapinfo(bio);
3661da177e4SLinus Torvalds 		mpio = info->ptr;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 		r = map_io(m, bio, mpio, 1);
3691da177e4SLinus Torvalds 		if (r < 0)
3701da177e4SLinus Torvalds 			bio_endio(bio, bio->bi_size, r);
3711da177e4SLinus Torvalds 		else if (r == 1)
3721da177e4SLinus Torvalds 			generic_make_request(bio);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 		bio = next;
3751da177e4SLinus Torvalds 	}
3761da177e4SLinus Torvalds }
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds static void process_queued_ios(void *data)
3791da177e4SLinus Torvalds {
3801da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) data;
3811da177e4SLinus Torvalds 	struct hw_handler *hwh = &m->hw_handler;
3821da177e4SLinus Torvalds 	struct pgpath *pgpath;
3831da177e4SLinus Torvalds 	unsigned init_required, must_queue = 0;
3841da177e4SLinus Torvalds 	unsigned long flags;
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	if (!m->current_pgpath)
3891da177e4SLinus Torvalds 		__choose_pgpath(m);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	pgpath = m->current_pgpath;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	if ((pgpath && m->queue_io) ||
3941da177e4SLinus Torvalds 	    (!pgpath && m->queue_if_no_path && !m->suspended))
3951da177e4SLinus Torvalds 		must_queue = 1;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	init_required = m->pg_init_required;
3981da177e4SLinus Torvalds 	if (init_required)
3991da177e4SLinus Torvalds 		m->pg_init_required = 0;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	if (init_required)
4041da177e4SLinus Torvalds 		hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	if (!must_queue)
4071da177e4SLinus Torvalds 		dispatch_queued_ios(m);
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds /*
4111da177e4SLinus Torvalds  * An event is triggered whenever a path is taken out of use.
4121da177e4SLinus Torvalds  * Includes path failure and PG bypass.
4131da177e4SLinus Torvalds  */
4141da177e4SLinus Torvalds static void trigger_event(void *data)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) data;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	dm_table_event(m->ti->table);
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds /*-----------------------------------------------------------------
4221da177e4SLinus Torvalds  * Constructor/argument parsing:
4231da177e4SLinus Torvalds  * <#multipath feature args> [<arg>]*
4241da177e4SLinus Torvalds  * <#hw_handler args> [hw_handler [<arg>]*]
4251da177e4SLinus Torvalds  * <#priority groups>
4261da177e4SLinus Torvalds  * <initial priority group>
4271da177e4SLinus Torvalds  *     [<selector> <#selector args> [<arg>]*
4281da177e4SLinus Torvalds  *      <#paths> <#per-path selector args>
4291da177e4SLinus Torvalds  *         [<path> [<arg>]* ]+ ]+
4301da177e4SLinus Torvalds  *---------------------------------------------------------------*/
4311da177e4SLinus Torvalds struct param {
4321da177e4SLinus Torvalds 	unsigned min;
4331da177e4SLinus Torvalds 	unsigned max;
4341da177e4SLinus Torvalds 	char *error;
4351da177e4SLinus Torvalds };
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds #define ESTR(s) ("dm-multipath: " s)
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds static int read_param(struct param *param, char *str, unsigned *v, char **error)
4401da177e4SLinus Torvalds {
4411da177e4SLinus Torvalds 	if (!str ||
4421da177e4SLinus Torvalds 	    (sscanf(str, "%u", v) != 1) ||
4431da177e4SLinus Torvalds 	    (*v < param->min) ||
4441da177e4SLinus Torvalds 	    (*v > param->max)) {
4451da177e4SLinus Torvalds 		*error = param->error;
4461da177e4SLinus Torvalds 		return -EINVAL;
4471da177e4SLinus Torvalds 	}
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	return 0;
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds struct arg_set {
4531da177e4SLinus Torvalds 	unsigned argc;
4541da177e4SLinus Torvalds 	char **argv;
4551da177e4SLinus Torvalds };
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds static char *shift(struct arg_set *as)
4581da177e4SLinus Torvalds {
4591da177e4SLinus Torvalds 	char *r;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	if (as->argc) {
4621da177e4SLinus Torvalds 		as->argc--;
4631da177e4SLinus Torvalds 		r = *as->argv;
4641da177e4SLinus Torvalds 		as->argv++;
4651da177e4SLinus Torvalds 		return r;
4661da177e4SLinus Torvalds 	}
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds 	return NULL;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds static void consume(struct arg_set *as, unsigned n)
4721da177e4SLinus Torvalds {
4731da177e4SLinus Torvalds 	BUG_ON (as->argc < n);
4741da177e4SLinus Torvalds 	as->argc -= n;
4751da177e4SLinus Torvalds 	as->argv += n;
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
4791da177e4SLinus Torvalds 			       struct dm_target *ti)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	int r;
4821da177e4SLinus Torvalds 	struct path_selector_type *pst;
4831da177e4SLinus Torvalds 	unsigned ps_argc;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	static struct param _params[] = {
4861da177e4SLinus Torvalds 		{0, 1024, ESTR("invalid number of path selector args")},
4871da177e4SLinus Torvalds 	};
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	pst = dm_get_path_selector(shift(as));
4901da177e4SLinus Torvalds 	if (!pst) {
4911da177e4SLinus Torvalds 		ti->error = ESTR("unknown path selector type");
4921da177e4SLinus Torvalds 		return -EINVAL;
4931da177e4SLinus Torvalds 	}
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	r = read_param(_params, shift(as), &ps_argc, &ti->error);
4961da177e4SLinus Torvalds 	if (r)
4971da177e4SLinus Torvalds 		return -EINVAL;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	r = pst->create(&pg->ps, ps_argc, as->argv);
5001da177e4SLinus Torvalds 	if (r) {
5011da177e4SLinus Torvalds 		dm_put_path_selector(pst);
5021da177e4SLinus Torvalds 		ti->error = ESTR("path selector constructor failed");
5031da177e4SLinus Torvalds 		return r;
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	pg->ps.type = pst;
5071da177e4SLinus Torvalds 	consume(as, ps_argc);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	return 0;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
5131da177e4SLinus Torvalds 			       struct dm_target *ti)
5141da177e4SLinus Torvalds {
5151da177e4SLinus Torvalds 	int r;
5161da177e4SLinus Torvalds 	struct pgpath *p;
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	/* we need at least a path arg */
5191da177e4SLinus Torvalds 	if (as->argc < 1) {
5201da177e4SLinus Torvalds 		ti->error = ESTR("no device given");
5211da177e4SLinus Torvalds 		return NULL;
5221da177e4SLinus Torvalds 	}
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	p = alloc_pgpath();
5251da177e4SLinus Torvalds 	if (!p)
5261da177e4SLinus Torvalds 		return NULL;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	r = dm_get_device(ti, shift(as), ti->begin, ti->len,
5291da177e4SLinus Torvalds 			  dm_table_get_mode(ti->table), &p->path.dev);
5301da177e4SLinus Torvalds 	if (r) {
5311da177e4SLinus Torvalds 		ti->error = ESTR("error getting device");
5321da177e4SLinus Torvalds 		goto bad;
5331da177e4SLinus Torvalds 	}
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
5361da177e4SLinus Torvalds 	if (r) {
5371da177e4SLinus Torvalds 		dm_put_device(ti, p->path.dev);
5381da177e4SLinus Torvalds 		goto bad;
5391da177e4SLinus Torvalds 	}
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 	return p;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds  bad:
5441da177e4SLinus Torvalds 	free_pgpath(p);
5451da177e4SLinus Torvalds 	return NULL;
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds static struct priority_group *parse_priority_group(struct arg_set *as,
5491da177e4SLinus Torvalds 						   struct multipath *m,
5501da177e4SLinus Torvalds 						   struct dm_target *ti)
5511da177e4SLinus Torvalds {
5521da177e4SLinus Torvalds 	static struct param _params[] = {
5531da177e4SLinus Torvalds 		{1, 1024, ESTR("invalid number of paths")},
5541da177e4SLinus Torvalds 		{0, 1024, ESTR("invalid number of selector args")}
5551da177e4SLinus Torvalds 	};
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	int r;
5581da177e4SLinus Torvalds 	unsigned i, nr_selector_args, nr_params;
5591da177e4SLinus Torvalds 	struct priority_group *pg;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	if (as->argc < 2) {
5621da177e4SLinus Torvalds 		as->argc = 0;
5631da177e4SLinus Torvalds 		ti->error = ESTR("not enough priority group aruments");
5641da177e4SLinus Torvalds 		return NULL;
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	pg = alloc_priority_group();
5681da177e4SLinus Torvalds 	if (!pg) {
5691da177e4SLinus Torvalds 		ti->error = ESTR("couldn't allocate priority group");
5701da177e4SLinus Torvalds 		return NULL;
5711da177e4SLinus Torvalds 	}
5721da177e4SLinus Torvalds 	pg->m = m;
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	r = parse_path_selector(as, pg, ti);
5751da177e4SLinus Torvalds 	if (r)
5761da177e4SLinus Torvalds 		goto bad;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	/*
5791da177e4SLinus Torvalds 	 * read the paths
5801da177e4SLinus Torvalds 	 */
5811da177e4SLinus Torvalds 	r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
5821da177e4SLinus Torvalds 	if (r)
5831da177e4SLinus Torvalds 		goto bad;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
5861da177e4SLinus Torvalds 	if (r)
5871da177e4SLinus Torvalds 		goto bad;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	nr_params = 1 + nr_selector_args;
5901da177e4SLinus Torvalds 	for (i = 0; i < pg->nr_pgpaths; i++) {
5911da177e4SLinus Torvalds 		struct pgpath *pgpath;
5921da177e4SLinus Torvalds 		struct arg_set path_args;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 		if (as->argc < nr_params)
5951da177e4SLinus Torvalds 			goto bad;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 		path_args.argc = nr_params;
5981da177e4SLinus Torvalds 		path_args.argv = as->argv;
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 		pgpath = parse_path(&path_args, &pg->ps, ti);
6011da177e4SLinus Torvalds 		if (!pgpath)
6021da177e4SLinus Torvalds 			goto bad;
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 		pgpath->pg = pg;
6051da177e4SLinus Torvalds 		list_add_tail(&pgpath->list, &pg->pgpaths);
6061da177e4SLinus Torvalds 		consume(as, nr_params);
6071da177e4SLinus Torvalds 	}
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	return pg;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds  bad:
6121da177e4SLinus Torvalds 	free_priority_group(pg, ti);
6131da177e4SLinus Torvalds 	return NULL;
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds static int parse_hw_handler(struct arg_set *as, struct multipath *m,
6171da177e4SLinus Torvalds 			    struct dm_target *ti)
6181da177e4SLinus Torvalds {
6191da177e4SLinus Torvalds 	int r;
6201da177e4SLinus Torvalds 	struct hw_handler_type *hwht;
6211da177e4SLinus Torvalds 	unsigned hw_argc;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	static struct param _params[] = {
6241da177e4SLinus Torvalds 		{0, 1024, ESTR("invalid number of hardware handler args")},
6251da177e4SLinus Torvalds 	};
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	r = read_param(_params, shift(as), &hw_argc, &ti->error);
6281da177e4SLinus Torvalds 	if (r)
6291da177e4SLinus Torvalds 		return -EINVAL;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	if (!hw_argc)
6321da177e4SLinus Torvalds 		return 0;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	hwht = dm_get_hw_handler(shift(as));
6351da177e4SLinus Torvalds 	if (!hwht) {
6361da177e4SLinus Torvalds 		ti->error = ESTR("unknown hardware handler type");
6371da177e4SLinus Torvalds 		return -EINVAL;
6381da177e4SLinus Torvalds 	}
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
6411da177e4SLinus Torvalds 	if (r) {
6421da177e4SLinus Torvalds 		dm_put_hw_handler(hwht);
6431da177e4SLinus Torvalds 		ti->error = ESTR("hardware handler constructor failed");
6441da177e4SLinus Torvalds 		return r;
6451da177e4SLinus Torvalds 	}
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds 	m->hw_handler.type = hwht;
6481da177e4SLinus Torvalds 	consume(as, hw_argc - 1);
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	return 0;
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds static int parse_features(struct arg_set *as, struct multipath *m,
6541da177e4SLinus Torvalds 			  struct dm_target *ti)
6551da177e4SLinus Torvalds {
6561da177e4SLinus Torvalds 	int r;
6571da177e4SLinus Torvalds 	unsigned argc;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	static struct param _params[] = {
6601da177e4SLinus Torvalds 		{0, 1, ESTR("invalid number of feature args")},
6611da177e4SLinus Torvalds 	};
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	r = read_param(_params, shift(as), &argc, &ti->error);
6641da177e4SLinus Torvalds 	if (r)
6651da177e4SLinus Torvalds 		return -EINVAL;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	if (!argc)
6681da177e4SLinus Torvalds 		return 0;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
6711da177e4SLinus Torvalds 		return queue_if_no_path(m, 1);
6721da177e4SLinus Torvalds 	else {
6731da177e4SLinus Torvalds 		ti->error = "Unrecognised multipath feature request";
6741da177e4SLinus Torvalds 		return -EINVAL;
6751da177e4SLinus Torvalds 	}
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds static int multipath_ctr(struct dm_target *ti, unsigned int argc,
6791da177e4SLinus Torvalds 			 char **argv)
6801da177e4SLinus Torvalds {
6811da177e4SLinus Torvalds 	/* target parameters */
6821da177e4SLinus Torvalds 	static struct param _params[] = {
6831da177e4SLinus Torvalds 		{1, 1024, ESTR("invalid number of priority groups")},
6841da177e4SLinus Torvalds 		{1, 1024, ESTR("invalid initial priority group number")},
6851da177e4SLinus Torvalds 	};
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	int r;
6881da177e4SLinus Torvalds 	struct multipath *m;
6891da177e4SLinus Torvalds 	struct arg_set as;
6901da177e4SLinus Torvalds 	unsigned pg_count = 0;
6911da177e4SLinus Torvalds 	unsigned next_pg_num;
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	as.argc = argc;
6941da177e4SLinus Torvalds 	as.argv = argv;
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	m = alloc_multipath();
6971da177e4SLinus Torvalds 	if (!m) {
6981da177e4SLinus Torvalds 		ti->error = ESTR("can't allocate multipath");
6991da177e4SLinus Torvalds 		return -EINVAL;
7001da177e4SLinus Torvalds 	}
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds 	r = parse_features(&as, m, ti);
7031da177e4SLinus Torvalds 	if (r)
7041da177e4SLinus Torvalds 		goto bad;
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds 	r = parse_hw_handler(&as, m, ti);
7071da177e4SLinus Torvalds 	if (r)
7081da177e4SLinus Torvalds 		goto bad;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
7111da177e4SLinus Torvalds 	if (r)
7121da177e4SLinus Torvalds 		goto bad;
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
7151da177e4SLinus Torvalds 	if (r)
7161da177e4SLinus Torvalds 		goto bad;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	/* parse the priority groups */
7191da177e4SLinus Torvalds 	while (as.argc) {
7201da177e4SLinus Torvalds 		struct priority_group *pg;
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds 		pg = parse_priority_group(&as, m, ti);
7231da177e4SLinus Torvalds 		if (!pg) {
7241da177e4SLinus Torvalds 			r = -EINVAL;
7251da177e4SLinus Torvalds 			goto bad;
7261da177e4SLinus Torvalds 		}
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 		m->nr_valid_paths += pg->nr_pgpaths;
7291da177e4SLinus Torvalds 		list_add_tail(&pg->list, &m->priority_groups);
7301da177e4SLinus Torvalds 		pg_count++;
7311da177e4SLinus Torvalds 		pg->pg_num = pg_count;
7321da177e4SLinus Torvalds 		if (!--next_pg_num)
7331da177e4SLinus Torvalds 			m->next_pg = pg;
7341da177e4SLinus Torvalds 	}
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	if (pg_count != m->nr_priority_groups) {
7371da177e4SLinus Torvalds 		ti->error = ESTR("priority group count mismatch");
7381da177e4SLinus Torvalds 		r = -EINVAL;
7391da177e4SLinus Torvalds 		goto bad;
7401da177e4SLinus Torvalds 	}
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	ti->private = m;
7431da177e4SLinus Torvalds 	m->ti = ti;
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 	return 0;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds  bad:
7481da177e4SLinus Torvalds 	free_multipath(m);
7491da177e4SLinus Torvalds 	return r;
7501da177e4SLinus Torvalds }
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds static void multipath_dtr(struct dm_target *ti)
7531da177e4SLinus Torvalds {
7541da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
7551da177e4SLinus Torvalds 	free_multipath(m);
7561da177e4SLinus Torvalds }
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds /*
7591da177e4SLinus Torvalds  * Map bios, recording original fields for later in case we have to resubmit
7601da177e4SLinus Torvalds  */
7611da177e4SLinus Torvalds static int multipath_map(struct dm_target *ti, struct bio *bio,
7621da177e4SLinus Torvalds 			 union map_info *map_context)
7631da177e4SLinus Torvalds {
7641da177e4SLinus Torvalds 	int r;
7651da177e4SLinus Torvalds 	struct mpath_io *mpio;
7661da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
7671da177e4SLinus Torvalds 
768*f6a80ea8SAlasdair G Kergon 	if (bio_barrier(bio))
769*f6a80ea8SAlasdair G Kergon 		return -EOPNOTSUPP;
770*f6a80ea8SAlasdair G Kergon 
7711da177e4SLinus Torvalds 	mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
7721da177e4SLinus Torvalds 	dm_bio_record(&mpio->details, bio);
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 	map_context->ptr = mpio;
7751da177e4SLinus Torvalds 	bio->bi_rw |= (1 << BIO_RW_FAILFAST);
7761da177e4SLinus Torvalds 	r = map_io(m, bio, mpio, 0);
7771da177e4SLinus Torvalds 	if (r < 0)
7781da177e4SLinus Torvalds 		mempool_free(mpio, m->mpio_pool);
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	return r;
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds /*
7841da177e4SLinus Torvalds  * Take a path out of use.
7851da177e4SLinus Torvalds  */
7861da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath)
7871da177e4SLinus Torvalds {
7881da177e4SLinus Torvalds 	unsigned long flags;
7891da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	if (!pgpath->path.is_active)
7941da177e4SLinus Torvalds 		goto out;
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	DMWARN("dm-multipath: Failing path %s.", pgpath->path.dev->name);
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
7991da177e4SLinus Torvalds 	pgpath->path.is_active = 0;
8001da177e4SLinus Torvalds 	pgpath->fail_count++;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	m->nr_valid_paths--;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	if (pgpath == m->current_pgpath)
8051da177e4SLinus Torvalds 		m->current_pgpath = NULL;
8061da177e4SLinus Torvalds 
807c557308eSAlasdair G Kergon 	queue_work(kmultipathd, &m->trigger_event);
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds out:
8101da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	return 0;
8131da177e4SLinus Torvalds }
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds /*
8161da177e4SLinus Torvalds  * Reinstate a previously-failed path
8171da177e4SLinus Torvalds  */
8181da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath)
8191da177e4SLinus Torvalds {
8201da177e4SLinus Torvalds 	int r = 0;
8211da177e4SLinus Torvalds 	unsigned long flags;
8221da177e4SLinus Torvalds 	struct multipath *m = pgpath->pg->m;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	if (pgpath->path.is_active)
8271da177e4SLinus Torvalds 		goto out;
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	if (!pgpath->pg->ps.type) {
8301da177e4SLinus Torvalds 		DMWARN("Reinstate path not supported by path selector %s",
8311da177e4SLinus Torvalds 		       pgpath->pg->ps.type->name);
8321da177e4SLinus Torvalds 		r = -EINVAL;
8331da177e4SLinus Torvalds 		goto out;
8341da177e4SLinus Torvalds 	}
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
8371da177e4SLinus Torvalds 	if (r)
8381da177e4SLinus Torvalds 		goto out;
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	pgpath->path.is_active = 1;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	m->current_pgpath = NULL;
8431da177e4SLinus Torvalds 	if (!m->nr_valid_paths++)
844c557308eSAlasdair G Kergon 		queue_work(kmultipathd, &m->process_queued_ios);
8451da177e4SLinus Torvalds 
846c557308eSAlasdair G Kergon 	queue_work(kmultipathd, &m->trigger_event);
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds out:
8491da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	return r;
8521da177e4SLinus Torvalds }
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds /*
8551da177e4SLinus Torvalds  * Fail or reinstate all paths that match the provided struct dm_dev.
8561da177e4SLinus Torvalds  */
8571da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev,
8581da177e4SLinus Torvalds 		      action_fn action)
8591da177e4SLinus Torvalds {
8601da177e4SLinus Torvalds 	int r = 0;
8611da177e4SLinus Torvalds 	struct pgpath *pgpath;
8621da177e4SLinus Torvalds 	struct priority_group *pg;
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
8651da177e4SLinus Torvalds 		list_for_each_entry(pgpath, &pg->pgpaths, list) {
8661da177e4SLinus Torvalds 			if (pgpath->path.dev == dev)
8671da177e4SLinus Torvalds 				r = action(pgpath);
8681da177e4SLinus Torvalds 		}
8691da177e4SLinus Torvalds 	}
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	return r;
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds /*
8751da177e4SLinus Torvalds  * Temporarily try to avoid having to use the specified PG
8761da177e4SLinus Torvalds  */
8771da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg,
8781da177e4SLinus Torvalds 		      int bypassed)
8791da177e4SLinus Torvalds {
8801da177e4SLinus Torvalds 	unsigned long flags;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	pg->bypassed = bypassed;
8851da177e4SLinus Torvalds 	m->current_pgpath = NULL;
8861da177e4SLinus Torvalds 	m->current_pg = NULL;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
8891da177e4SLinus Torvalds 
890c557308eSAlasdair G Kergon 	queue_work(kmultipathd, &m->trigger_event);
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds /*
8941da177e4SLinus Torvalds  * Switch to using the specified PG from the next I/O that gets mapped
8951da177e4SLinus Torvalds  */
8961da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr)
8971da177e4SLinus Torvalds {
8981da177e4SLinus Torvalds 	struct priority_group *pg;
8991da177e4SLinus Torvalds 	unsigned pgnum;
9001da177e4SLinus Torvalds 	unsigned long flags;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
9031da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
9041da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to switch_pg_num");
9051da177e4SLinus Torvalds 		return -EINVAL;
9061da177e4SLinus Torvalds 	}
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9091da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
9101da177e4SLinus Torvalds 		pg->bypassed = 0;
9111da177e4SLinus Torvalds 		if (--pgnum)
9121da177e4SLinus Torvalds 			continue;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 		m->current_pgpath = NULL;
9151da177e4SLinus Torvalds 		m->current_pg = NULL;
9161da177e4SLinus Torvalds 		m->next_pg = pg;
9171da177e4SLinus Torvalds 	}
9181da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
9191da177e4SLinus Torvalds 
920c557308eSAlasdair G Kergon 	queue_work(kmultipathd, &m->trigger_event);
9211da177e4SLinus Torvalds 	return 0;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds /*
9251da177e4SLinus Torvalds  * Set/clear bypassed status of a PG.
9261da177e4SLinus Torvalds  * PGs are numbered upwards from 1 in the order they were declared.
9271da177e4SLinus Torvalds  */
9281da177e4SLinus Torvalds static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
9291da177e4SLinus Torvalds {
9301da177e4SLinus Torvalds 	struct priority_group *pg;
9311da177e4SLinus Torvalds 	unsigned pgnum;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
9341da177e4SLinus Torvalds 	    (pgnum > m->nr_priority_groups)) {
9351da177e4SLinus Torvalds 		DMWARN("invalid PG number supplied to bypass_pg");
9361da177e4SLinus Torvalds 		return -EINVAL;
9371da177e4SLinus Torvalds 	}
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds 	list_for_each_entry(pg, &m->priority_groups, list) {
9401da177e4SLinus Torvalds 		if (!--pgnum)
9411da177e4SLinus Torvalds 			break;
9421da177e4SLinus Torvalds 	}
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	bypass_pg(m, pg, bypassed);
9451da177e4SLinus Torvalds 	return 0;
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds /*
9491da177e4SLinus Torvalds  * pg_init must call this when it has completed its initialisation
9501da177e4SLinus Torvalds  */
9511da177e4SLinus Torvalds void dm_pg_init_complete(struct path *path, unsigned err_flags)
9521da177e4SLinus Torvalds {
9531da177e4SLinus Torvalds 	struct pgpath *pgpath = path_to_pgpath(path);
9541da177e4SLinus Torvalds 	struct priority_group *pg = pgpath->pg;
9551da177e4SLinus Torvalds 	struct multipath *m = pg->m;
9561da177e4SLinus Torvalds 	unsigned long flags;
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	/* We insist on failing the path if the PG is already bypassed. */
9591da177e4SLinus Torvalds 	if (err_flags && pg->bypassed)
9601da177e4SLinus Torvalds 		err_flags |= MP_FAIL_PATH;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	if (err_flags & MP_FAIL_PATH)
9631da177e4SLinus Torvalds 		fail_path(pgpath);
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	if (err_flags & MP_BYPASS_PG)
9661da177e4SLinus Torvalds 		bypass_pg(m, pg, 1);
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
9691da177e4SLinus Torvalds 	if (!err_flags)
9701da177e4SLinus Torvalds 		m->queue_io = 0;
9711da177e4SLinus Torvalds 	else {
9721da177e4SLinus Torvalds 		m->current_pgpath = NULL;
9731da177e4SLinus Torvalds 		m->current_pg = NULL;
9741da177e4SLinus Torvalds 	}
975c557308eSAlasdair G Kergon 	queue_work(kmultipathd, &m->process_queued_ios);
9761da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds /*
9801da177e4SLinus Torvalds  * end_io handling
9811da177e4SLinus Torvalds  */
9821da177e4SLinus Torvalds static int do_end_io(struct multipath *m, struct bio *bio,
9831da177e4SLinus Torvalds 		     int error, struct mpath_io *mpio)
9841da177e4SLinus Torvalds {
9851da177e4SLinus Torvalds 	struct hw_handler *hwh = &m->hw_handler;
9861da177e4SLinus Torvalds 	unsigned err_flags = MP_FAIL_PATH;	/* Default behavior */
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds 	if (!error)
9891da177e4SLinus Torvalds 		return 0;	/* I/O complete */
9901da177e4SLinus Torvalds 
9914f58802fSLars Marowsky-Bree 	if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
9924f58802fSLars Marowsky-Bree 		return error;
9934f58802fSLars Marowsky-Bree 
994*f6a80ea8SAlasdair G Kergon 	if (error == -EOPNOTSUPP)
995*f6a80ea8SAlasdair G Kergon 		return error;
996*f6a80ea8SAlasdair G Kergon 
9971da177e4SLinus Torvalds 	spin_lock(&m->lock);
9981da177e4SLinus Torvalds 	if (!m->nr_valid_paths) {
9991da177e4SLinus Torvalds 		if (!m->queue_if_no_path || m->suspended) {
10001da177e4SLinus Torvalds 			spin_unlock(&m->lock);
10011da177e4SLinus Torvalds 			return -EIO;
10021da177e4SLinus Torvalds 		} else {
10031da177e4SLinus Torvalds 			spin_unlock(&m->lock);
10041da177e4SLinus Torvalds 			goto requeue;
10051da177e4SLinus Torvalds 		}
10061da177e4SLinus Torvalds 	}
10071da177e4SLinus Torvalds 	spin_unlock(&m->lock);
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	if (hwh->type && hwh->type->error)
10101da177e4SLinus Torvalds 		err_flags = hwh->type->error(hwh, bio);
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	if (mpio->pgpath) {
10131da177e4SLinus Torvalds 		if (err_flags & MP_FAIL_PATH)
10141da177e4SLinus Torvalds 			fail_path(mpio->pgpath);
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 		if (err_flags & MP_BYPASS_PG)
10171da177e4SLinus Torvalds 			bypass_pg(m, mpio->pgpath->pg, 1);
10181da177e4SLinus Torvalds 	}
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds 	if (err_flags & MP_ERROR_IO)
10211da177e4SLinus Torvalds 		return -EIO;
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds       requeue:
10241da177e4SLinus Torvalds 	dm_bio_restore(&mpio->details, bio);
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	/* queue for the daemon to resubmit or fail */
10271da177e4SLinus Torvalds 	spin_lock(&m->lock);
10281da177e4SLinus Torvalds 	bio_list_add(&m->queued_ios, bio);
10291da177e4SLinus Torvalds 	m->queue_size++;
10301da177e4SLinus Torvalds 	if (!m->queue_io)
1031c557308eSAlasdair G Kergon 		queue_work(kmultipathd, &m->process_queued_ios);
10321da177e4SLinus Torvalds 	spin_unlock(&m->lock);
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	return 1;	/* io not complete */
10351da177e4SLinus Torvalds }
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds static int multipath_end_io(struct dm_target *ti, struct bio *bio,
10381da177e4SLinus Torvalds 			    int error, union map_info *map_context)
10391da177e4SLinus Torvalds {
10401da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
10411da177e4SLinus Torvalds 	struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
10421da177e4SLinus Torvalds 	struct pgpath *pgpath = mpio->pgpath;
10431da177e4SLinus Torvalds 	struct path_selector *ps;
10441da177e4SLinus Torvalds 	int r;
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 	r  = do_end_io(m, bio, error, mpio);
10471da177e4SLinus Torvalds 	if (pgpath) {
10481da177e4SLinus Torvalds 		ps = &pgpath->pg->ps;
10491da177e4SLinus Torvalds 		if (ps->type->end_io)
10501da177e4SLinus Torvalds 			ps->type->end_io(ps, &pgpath->path);
10511da177e4SLinus Torvalds 	}
10521da177e4SLinus Torvalds 	if (r <= 0)
10531da177e4SLinus Torvalds 		mempool_free(mpio, m->mpio_pool);
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 	return r;
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds /*
10591da177e4SLinus Torvalds  * Suspend can't complete until all the I/O is processed so if
10601da177e4SLinus Torvalds  * the last path failed we will now error any queued I/O.
10611da177e4SLinus Torvalds  */
10621da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti)
10631da177e4SLinus Torvalds {
10641da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
10651da177e4SLinus Torvalds 	unsigned long flags;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10681da177e4SLinus Torvalds 	m->suspended = 1;
10691da177e4SLinus Torvalds 	if (m->queue_if_no_path)
1070c557308eSAlasdair G Kergon 		queue_work(kmultipathd, &m->process_queued_ios);
10711da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10721da177e4SLinus Torvalds }
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti)
10751da177e4SLinus Torvalds {
10761da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
10771da177e4SLinus Torvalds 	unsigned long flags;
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
10801da177e4SLinus Torvalds 	m->suspended = 0;
10811da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
10821da177e4SLinus Torvalds }
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds /*
10851da177e4SLinus Torvalds  * Info output has the following format:
10861da177e4SLinus Torvalds  * num_multipath_feature_args [multipath_feature_args]*
10871da177e4SLinus Torvalds  * num_handler_status_args [handler_status_args]*
10881da177e4SLinus Torvalds  * num_groups init_group_number
10891da177e4SLinus Torvalds  *            [A|D|E num_ps_status_args [ps_status_args]*
10901da177e4SLinus Torvalds  *             num_paths num_selector_args
10911da177e4SLinus Torvalds  *             [path_dev A|F fail_count [selector_args]* ]+ ]+
10921da177e4SLinus Torvalds  *
10931da177e4SLinus Torvalds  * Table output has the following format (identical to the constructor string):
10941da177e4SLinus Torvalds  * num_feature_args [features_args]*
10951da177e4SLinus Torvalds  * num_handler_args hw_handler [hw_handler_args]*
10961da177e4SLinus Torvalds  * num_groups init_group_number
10971da177e4SLinus Torvalds  *     [priority selector-name num_ps_args [ps_args]*
10981da177e4SLinus Torvalds  *      num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
10991da177e4SLinus Torvalds  */
11001da177e4SLinus Torvalds static int multipath_status(struct dm_target *ti, status_type_t type,
11011da177e4SLinus Torvalds 			    char *result, unsigned int maxlen)
11021da177e4SLinus Torvalds {
11031da177e4SLinus Torvalds 	int sz = 0;
11041da177e4SLinus Torvalds 	unsigned long flags;
11051da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
11061da177e4SLinus Torvalds 	struct hw_handler *hwh = &m->hw_handler;
11071da177e4SLinus Torvalds 	struct priority_group *pg;
11081da177e4SLinus Torvalds 	struct pgpath *p;
11091da177e4SLinus Torvalds 	unsigned pg_num;
11101da177e4SLinus Torvalds 	char state;
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	spin_lock_irqsave(&m->lock, flags);
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds 	/* Features */
11151da177e4SLinus Torvalds 	if (type == STATUSTYPE_INFO)
11161da177e4SLinus Torvalds 		DMEMIT("1 %u ", m->queue_size);
11171da177e4SLinus Torvalds 	else if (m->queue_if_no_path)
11181da177e4SLinus Torvalds 		DMEMIT("1 queue_if_no_path ");
11191da177e4SLinus Torvalds 	else
11201da177e4SLinus Torvalds 		DMEMIT("0 ");
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds 	if (hwh->type && hwh->type->status)
11231da177e4SLinus Torvalds 		sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
11241da177e4SLinus Torvalds 	else if (!hwh->type || type == STATUSTYPE_INFO)
11251da177e4SLinus Torvalds 		DMEMIT("0 ");
11261da177e4SLinus Torvalds 	else
11271da177e4SLinus Torvalds 		DMEMIT("1 %s ", hwh->type->name);
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	DMEMIT("%u ", m->nr_priority_groups);
11301da177e4SLinus Torvalds 
11311da177e4SLinus Torvalds 	if (m->next_pg)
11321da177e4SLinus Torvalds 		pg_num = m->next_pg->pg_num;
11331da177e4SLinus Torvalds 	else if (m->current_pg)
11341da177e4SLinus Torvalds 		pg_num = m->current_pg->pg_num;
11351da177e4SLinus Torvalds 	else
11361da177e4SLinus Torvalds 			pg_num = 1;
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	DMEMIT("%u ", pg_num);
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 	switch (type) {
11411da177e4SLinus Torvalds 	case STATUSTYPE_INFO:
11421da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
11431da177e4SLinus Torvalds 			if (pg->bypassed)
11441da177e4SLinus Torvalds 				state = 'D';	/* Disabled */
11451da177e4SLinus Torvalds 			else if (pg == m->current_pg)
11461da177e4SLinus Torvalds 				state = 'A';	/* Currently Active */
11471da177e4SLinus Torvalds 			else
11481da177e4SLinus Torvalds 				state = 'E';	/* Enabled */
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 			DMEMIT("%c ", state);
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 			if (pg->ps.type->status)
11531da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
11541da177e4SLinus Torvalds 							  result + sz,
11551da177e4SLinus Torvalds 							  maxlen - sz);
11561da177e4SLinus Torvalds 			else
11571da177e4SLinus Torvalds 				DMEMIT("0 ");
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
11601da177e4SLinus Torvalds 			       pg->ps.type->info_args);
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
11631da177e4SLinus Torvalds 				DMEMIT("%s %s %u ", p->path.dev->name,
11641da177e4SLinus Torvalds 				       p->path.is_active ? "A" : "F",
11651da177e4SLinus Torvalds 				       p->fail_count);
11661da177e4SLinus Torvalds 				if (pg->ps.type->status)
11671da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
11681da177e4SLinus Torvalds 					      &p->path, type, result + sz,
11691da177e4SLinus Torvalds 					      maxlen - sz);
11701da177e4SLinus Torvalds 			}
11711da177e4SLinus Torvalds 		}
11721da177e4SLinus Torvalds 		break;
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	case STATUSTYPE_TABLE:
11751da177e4SLinus Torvalds 		list_for_each_entry(pg, &m->priority_groups, list) {
11761da177e4SLinus Torvalds 			DMEMIT("%s ", pg->ps.type->name);
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 			if (pg->ps.type->status)
11791da177e4SLinus Torvalds 				sz += pg->ps.type->status(&pg->ps, NULL, type,
11801da177e4SLinus Torvalds 							  result + sz,
11811da177e4SLinus Torvalds 							  maxlen - sz);
11821da177e4SLinus Torvalds 			else
11831da177e4SLinus Torvalds 				DMEMIT("0 ");
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 			DMEMIT("%u %u ", pg->nr_pgpaths,
11861da177e4SLinus Torvalds 			       pg->ps.type->table_args);
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 			list_for_each_entry(p, &pg->pgpaths, list) {
11891da177e4SLinus Torvalds 				DMEMIT("%s ", p->path.dev->name);
11901da177e4SLinus Torvalds 				if (pg->ps.type->status)
11911da177e4SLinus Torvalds 					sz += pg->ps.type->status(&pg->ps,
11921da177e4SLinus Torvalds 					      &p->path, type, result + sz,
11931da177e4SLinus Torvalds 					      maxlen - sz);
11941da177e4SLinus Torvalds 			}
11951da177e4SLinus Torvalds 		}
11961da177e4SLinus Torvalds 		break;
11971da177e4SLinus Torvalds 	}
11981da177e4SLinus Torvalds 
11991da177e4SLinus Torvalds 	spin_unlock_irqrestore(&m->lock, flags);
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 	return 0;
12021da177e4SLinus Torvalds }
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
12051da177e4SLinus Torvalds {
12061da177e4SLinus Torvalds 	int r;
12071da177e4SLinus Torvalds 	struct dm_dev *dev;
12081da177e4SLinus Torvalds 	struct multipath *m = (struct multipath *) ti->private;
12091da177e4SLinus Torvalds 	action_fn action;
12101da177e4SLinus Torvalds 
12111da177e4SLinus Torvalds 	if (argc == 1) {
12121da177e4SLinus Torvalds 		if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
12131da177e4SLinus Torvalds 			return queue_if_no_path(m, 1);
12141da177e4SLinus Torvalds 		else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
12151da177e4SLinus Torvalds 			return queue_if_no_path(m, 0);
12161da177e4SLinus Torvalds 	}
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	if (argc != 2)
12191da177e4SLinus Torvalds 		goto error;
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	if (!strnicmp(argv[0], MESG_STR("disable_group")))
12221da177e4SLinus Torvalds 		return bypass_pg_num(m, argv[1], 1);
12231da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("enable_group")))
12241da177e4SLinus Torvalds 		return bypass_pg_num(m, argv[1], 0);
12251da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("switch_group")))
12261da177e4SLinus Torvalds 		return switch_pg_num(m, argv[1]);
12271da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
12281da177e4SLinus Torvalds 		action = reinstate_path;
12291da177e4SLinus Torvalds 	else if (!strnicmp(argv[0], MESG_STR("fail_path")))
12301da177e4SLinus Torvalds 		action = fail_path;
12311da177e4SLinus Torvalds 	else
12321da177e4SLinus Torvalds 		goto error;
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds 	r = dm_get_device(ti, argv[1], ti->begin, ti->len,
12351da177e4SLinus Torvalds 			  dm_table_get_mode(ti->table), &dev);
12361da177e4SLinus Torvalds 	if (r) {
12371da177e4SLinus Torvalds 		DMWARN("dm-multipath message: error getting device %s",
12381da177e4SLinus Torvalds 		       argv[1]);
12391da177e4SLinus Torvalds 		return -EINVAL;
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds 	r = action_dev(m, dev, action);
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds 	dm_put_device(ti, dev);
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	return r;
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds error:
12491da177e4SLinus Torvalds 	DMWARN("Unrecognised multipath message received.");
12501da177e4SLinus Torvalds 	return -EINVAL;
12511da177e4SLinus Torvalds }
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds /*-----------------------------------------------------------------
12541da177e4SLinus Torvalds  * Module setup
12551da177e4SLinus Torvalds  *---------------------------------------------------------------*/
12561da177e4SLinus Torvalds static struct target_type multipath_target = {
12571da177e4SLinus Torvalds 	.name = "multipath",
12581da177e4SLinus Torvalds 	.version = {1, 0, 4},
12591da177e4SLinus Torvalds 	.module = THIS_MODULE,
12601da177e4SLinus Torvalds 	.ctr = multipath_ctr,
12611da177e4SLinus Torvalds 	.dtr = multipath_dtr,
12621da177e4SLinus Torvalds 	.map = multipath_map,
12631da177e4SLinus Torvalds 	.end_io = multipath_end_io,
12641da177e4SLinus Torvalds 	.presuspend = multipath_presuspend,
12651da177e4SLinus Torvalds 	.resume = multipath_resume,
12661da177e4SLinus Torvalds 	.status = multipath_status,
12671da177e4SLinus Torvalds 	.message = multipath_message,
12681da177e4SLinus Torvalds };
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds static int __init dm_multipath_init(void)
12711da177e4SLinus Torvalds {
12721da177e4SLinus Torvalds 	int r;
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 	/* allocate a slab for the dm_ios */
12751da177e4SLinus Torvalds 	_mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
12761da177e4SLinus Torvalds 					0, 0, NULL, NULL);
12771da177e4SLinus Torvalds 	if (!_mpio_cache)
12781da177e4SLinus Torvalds 		return -ENOMEM;
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 	r = dm_register_target(&multipath_target);
12811da177e4SLinus Torvalds 	if (r < 0) {
12821da177e4SLinus Torvalds 		DMERR("%s: register failed %d", multipath_target.name, r);
12831da177e4SLinus Torvalds 		kmem_cache_destroy(_mpio_cache);
12841da177e4SLinus Torvalds 		return -EINVAL;
12851da177e4SLinus Torvalds 	}
12861da177e4SLinus Torvalds 
1287c557308eSAlasdair G Kergon 	kmultipathd = create_workqueue("kmpathd");
1288c557308eSAlasdair G Kergon 	if (!kmultipathd) {
1289c557308eSAlasdair G Kergon 		DMERR("%s: failed to create workqueue kmpathd",
1290c557308eSAlasdair G Kergon 				multipath_target.name);
1291c557308eSAlasdair G Kergon 		dm_unregister_target(&multipath_target);
1292c557308eSAlasdair G Kergon 		kmem_cache_destroy(_mpio_cache);
1293c557308eSAlasdair G Kergon 		return -ENOMEM;
1294c557308eSAlasdair G Kergon 	}
1295c557308eSAlasdair G Kergon 
12961da177e4SLinus Torvalds 	DMINFO("dm-multipath version %u.%u.%u loaded",
12971da177e4SLinus Torvalds 	       multipath_target.version[0], multipath_target.version[1],
12981da177e4SLinus Torvalds 	       multipath_target.version[2]);
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 	return r;
13011da177e4SLinus Torvalds }
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds static void __exit dm_multipath_exit(void)
13041da177e4SLinus Torvalds {
13051da177e4SLinus Torvalds 	int r;
13061da177e4SLinus Torvalds 
1307c557308eSAlasdair G Kergon 	destroy_workqueue(kmultipathd);
1308c557308eSAlasdair G Kergon 
13091da177e4SLinus Torvalds 	r = dm_unregister_target(&multipath_target);
13101da177e4SLinus Torvalds 	if (r < 0)
13111da177e4SLinus Torvalds 		DMERR("%s: target unregister failed %d",
13121da177e4SLinus Torvalds 		      multipath_target.name, r);
13131da177e4SLinus Torvalds 	kmem_cache_destroy(_mpio_cache);
13141da177e4SLinus Torvalds }
13151da177e4SLinus Torvalds 
13161da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(dm_pg_init_complete);
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds module_init(dm_multipath_init);
13191da177e4SLinus Torvalds module_exit(dm_multipath_exit);
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target");
13221da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
13231da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1324