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