xref: /freebsd/sys/geom/multipath/g_multipath.c (revision c0b1ef66618a8f51ddfe79c90e350b7caae6a4b5)
1e770bc6bSMatt Jacob /*-
20c883cefSAlexander Motin  * Copyright (c) 2011 Alexander Motin <mav@FreeBSD.org>
3e770bc6bSMatt Jacob  * Copyright (c) 2006-2007 Matthew Jacob <mjacob@FreeBSD.org>
4e770bc6bSMatt Jacob  * All rights reserved.
5e770bc6bSMatt Jacob  *
6e770bc6bSMatt Jacob  * Redistribution and use in source and binary forms, with or without
7e770bc6bSMatt Jacob  * modification, are permitted provided that the following conditions
8e770bc6bSMatt Jacob  * are met:
9e770bc6bSMatt Jacob  * 1. Redistributions of source code must retain the above copyright
10e770bc6bSMatt Jacob  *    notice, this list of conditions and the following disclaimer.
11e770bc6bSMatt Jacob  * 2. Redistributions in binary form must reproduce the above copyright
12e770bc6bSMatt Jacob  *    notice, this list of conditions and the following disclaimer in the
13e770bc6bSMatt Jacob  *    documentation and/or other materials provided with the distribution.
14e770bc6bSMatt Jacob  *
15e770bc6bSMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16e770bc6bSMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17e770bc6bSMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18e770bc6bSMatt Jacob  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19e770bc6bSMatt Jacob  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20e770bc6bSMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21e770bc6bSMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22e770bc6bSMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23e770bc6bSMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24e770bc6bSMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25e770bc6bSMatt Jacob  * SUCH DAMAGE.
26e770bc6bSMatt Jacob  */
27e770bc6bSMatt Jacob /*
28e770bc6bSMatt Jacob  * Based upon work by Pawel Jakub Dawidek <pjd@FreeBSD.org> for all of the
29e770bc6bSMatt Jacob  * fine geom examples, and by Poul Henning Kamp <phk@FreeBSD.org> for GEOM
30e770bc6bSMatt Jacob  * itself, all of which is most gratefully acknowledged.
31e770bc6bSMatt Jacob  */
32e770bc6bSMatt Jacob 
33e770bc6bSMatt Jacob #include <sys/cdefs.h>
34e770bc6bSMatt Jacob __FBSDID("$FreeBSD$");
35e770bc6bSMatt Jacob #include <sys/param.h>
36e770bc6bSMatt Jacob #include <sys/systm.h>
37e770bc6bSMatt Jacob #include <sys/kernel.h>
38e770bc6bSMatt Jacob #include <sys/module.h>
39e770bc6bSMatt Jacob #include <sys/lock.h>
40e770bc6bSMatt Jacob #include <sys/mutex.h>
41e770bc6bSMatt Jacob #include <sys/bio.h>
425d807a0eSAndrey V. Elsukov #include <sys/sbuf.h>
43e770bc6bSMatt Jacob #include <sys/sysctl.h>
44e770bc6bSMatt Jacob #include <sys/kthread.h>
45e770bc6bSMatt Jacob #include <sys/malloc.h>
46e770bc6bSMatt Jacob #include <geom/geom.h>
47e770bc6bSMatt Jacob #include <geom/multipath/g_multipath.h>
48e770bc6bSMatt Jacob 
49cb08c2ccSAlexander Leidinger FEATURE(geom_multipath, "GEOM multipath support");
50e770bc6bSMatt Jacob 
51e770bc6bSMatt Jacob SYSCTL_DECL(_kern_geom);
526472ac3dSEd Schouten static SYSCTL_NODE(_kern_geom, OID_AUTO, multipath, CTLFLAG_RW, 0,
53e770bc6bSMatt Jacob     "GEOM_MULTIPATH tunables");
54e770bc6bSMatt Jacob static u_int g_multipath_debug = 0;
55e770bc6bSMatt Jacob SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, debug, CTLFLAG_RW,
56e770bc6bSMatt Jacob     &g_multipath_debug, 0, "Debug level");
570c883cefSAlexander Motin static u_int g_multipath_exclusive = 1;
580c883cefSAlexander Motin SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, exclusive, CTLFLAG_RW,
590c883cefSAlexander Motin     &g_multipath_exclusive, 0, "Exclusively open providers");
60e770bc6bSMatt Jacob 
61e770bc6bSMatt Jacob static enum {
62e770bc6bSMatt Jacob 	GKT_NIL,
63e770bc6bSMatt Jacob 	GKT_RUN,
64e770bc6bSMatt Jacob 	GKT_DIE
65e770bc6bSMatt Jacob } g_multipath_kt_state;
66e770bc6bSMatt Jacob static struct bio_queue_head gmtbq;
67e770bc6bSMatt Jacob static struct mtx gmtbq_mtx;
68e770bc6bSMatt Jacob 
69e770bc6bSMatt Jacob static void g_multipath_orphan(struct g_consumer *);
70e770bc6bSMatt Jacob static void g_multipath_start(struct bio *);
71e770bc6bSMatt Jacob static void g_multipath_done(struct bio *);
72e770bc6bSMatt Jacob static void g_multipath_done_error(struct bio *);
73e770bc6bSMatt Jacob static void g_multipath_kt(void *);
74e770bc6bSMatt Jacob 
75e770bc6bSMatt Jacob static int g_multipath_destroy(struct g_geom *);
76e770bc6bSMatt Jacob static int
77e770bc6bSMatt Jacob g_multipath_destroy_geom(struct gctl_req *, struct g_class *, struct g_geom *);
78e770bc6bSMatt Jacob 
792b4969ffSMatt Jacob static struct g_geom *g_multipath_find_geom(struct g_class *, const char *);
80b5dce617SMatt Jacob static int g_multipath_rotate(struct g_geom *);
81b5dce617SMatt Jacob 
82e770bc6bSMatt Jacob static g_taste_t g_multipath_taste;
83e770bc6bSMatt Jacob static g_ctl_req_t g_multipath_config;
84e770bc6bSMatt Jacob static g_init_t g_multipath_init;
85e770bc6bSMatt Jacob static g_fini_t g_multipath_fini;
860c883cefSAlexander Motin static g_dumpconf_t g_multipath_dumpconf;
87e770bc6bSMatt Jacob 
88e770bc6bSMatt Jacob struct g_class g_multipath_class = {
89e770bc6bSMatt Jacob 	.name		= G_MULTIPATH_CLASS_NAME,
90e770bc6bSMatt Jacob 	.version	= G_VERSION,
91e770bc6bSMatt Jacob 	.ctlreq		= g_multipath_config,
92e770bc6bSMatt Jacob 	.taste		= g_multipath_taste,
93e770bc6bSMatt Jacob 	.destroy_geom	= g_multipath_destroy_geom,
94e770bc6bSMatt Jacob 	.init		= g_multipath_init,
95e770bc6bSMatt Jacob 	.fini		= g_multipath_fini
96e770bc6bSMatt Jacob };
97e770bc6bSMatt Jacob 
980c883cefSAlexander Motin #define	MP_FAIL		0x00000001
990c883cefSAlexander Motin #define	MP_LOST		0x00000002
1000c883cefSAlexander Motin #define	MP_NEW		0x00000004
1010c883cefSAlexander Motin #define	MP_POSTED	0x00000008
1020c883cefSAlexander Motin #define	MP_BAD		(MP_FAIL | MP_LOST | MP_NEW)
1030c883cefSAlexander Motin #define MP_IDLE		0x00000010
1040c883cefSAlexander Motin #define MP_IDLE_MASK	0xfffffff0
1050c883cefSAlexander Motin 
1060c883cefSAlexander Motin static int
1070c883cefSAlexander Motin g_multipath_good(struct g_geom *gp)
1080c883cefSAlexander Motin {
1090c883cefSAlexander Motin 	struct g_consumer *cp;
1100c883cefSAlexander Motin 	int n = 0;
1110c883cefSAlexander Motin 
1120c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
1130c883cefSAlexander Motin 		if ((cp->index & MP_BAD) == 0)
1140c883cefSAlexander Motin 			n++;
1150c883cefSAlexander Motin 	}
1160c883cefSAlexander Motin 	return (n);
1170c883cefSAlexander Motin }
1180c883cefSAlexander Motin 
1190c883cefSAlexander Motin static void
1200c883cefSAlexander Motin g_multipath_fault(struct g_consumer *cp, int cause)
1210c883cefSAlexander Motin {
1220c883cefSAlexander Motin 	struct g_multipath_softc *sc;
1230c883cefSAlexander Motin 	struct g_consumer *lcp;
1240c883cefSAlexander Motin 	struct g_geom *gp;
1250c883cefSAlexander Motin 
1260c883cefSAlexander Motin 	gp = cp->geom;
1270c883cefSAlexander Motin 	sc = gp->softc;
1280c883cefSAlexander Motin 	cp->index |= cause;
1290c883cefSAlexander Motin 	if (g_multipath_good(gp) == 0 && sc->sc_ndisks > 0) {
1300c883cefSAlexander Motin 		LIST_FOREACH(lcp, &gp->consumer, consumer) {
1310c883cefSAlexander Motin 			if (lcp->provider == NULL ||
1320c883cefSAlexander Motin 			    (lcp->index & (MP_LOST | MP_NEW)))
1330c883cefSAlexander Motin 				continue;
1340c883cefSAlexander Motin 			if (sc->sc_ndisks > 1 && lcp == cp)
1350c883cefSAlexander Motin 				continue;
1360c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: "
1370c883cefSAlexander Motin 			    "all paths in %s were marked FAIL, restore %s\n",
1380c883cefSAlexander Motin 			    sc->sc_name, lcp->provider->name);
1390c883cefSAlexander Motin 			lcp->index &= ~MP_FAIL;
1400c883cefSAlexander Motin 		}
1410c883cefSAlexander Motin 	}
1420c883cefSAlexander Motin 	if (cp != sc->sc_active)
1430c883cefSAlexander Motin 		return;
1440c883cefSAlexander Motin 	sc->sc_active = NULL;
1450c883cefSAlexander Motin 	LIST_FOREACH(lcp, &gp->consumer, consumer) {
1460c883cefSAlexander Motin 		if ((lcp->index & MP_BAD) == 0) {
1470c883cefSAlexander Motin 			sc->sc_active = lcp;
1480c883cefSAlexander Motin 			break;
1490c883cefSAlexander Motin 		}
1500c883cefSAlexander Motin 	}
1510c883cefSAlexander Motin 	if (sc->sc_active == NULL) {
1520c883cefSAlexander Motin 		printf("GEOM_MULTIPATH: out of providers for %s\n",
1530c883cefSAlexander Motin 		    sc->sc_name);
15463297dfdSAlexander Motin 	} else if (sc->sc_active_active != 1) {
1550c883cefSAlexander Motin 		printf("GEOM_MULTIPATH: %s is now active path in %s\n",
1560c883cefSAlexander Motin 		    sc->sc_active->provider->name, sc->sc_name);
1570c883cefSAlexander Motin 	}
1580c883cefSAlexander Motin }
1590c883cefSAlexander Motin 
1600c883cefSAlexander Motin static struct g_consumer *
16163297dfdSAlexander Motin g_multipath_choose(struct g_geom *gp, struct bio *bp)
1620c883cefSAlexander Motin {
1630c883cefSAlexander Motin 	struct g_multipath_softc *sc;
1640c883cefSAlexander Motin 	struct g_consumer *best, *cp;
1650c883cefSAlexander Motin 
1660c883cefSAlexander Motin 	sc = gp->softc;
16763297dfdSAlexander Motin 	if (sc->sc_active_active == 0 ||
16863297dfdSAlexander Motin 	    (sc->sc_active_active == 2 && bp->bio_cmd != BIO_READ))
1690c883cefSAlexander Motin 		return (sc->sc_active);
1700c883cefSAlexander Motin 	best = NULL;
1710c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
1720c883cefSAlexander Motin 		if (cp->index & MP_BAD)
1730c883cefSAlexander Motin 			continue;
1740c883cefSAlexander Motin 		cp->index += MP_IDLE;
1750c883cefSAlexander Motin 		if (best == NULL || cp->private < best->private ||
1760c883cefSAlexander Motin 		    (cp->private == best->private && cp->index > best->index))
1770c883cefSAlexander Motin 			best = cp;
1780c883cefSAlexander Motin 	}
1790c883cefSAlexander Motin 	if (best != NULL)
1800c883cefSAlexander Motin 		best->index &= ~MP_IDLE_MASK;
1810c883cefSAlexander Motin 	return (best);
1820c883cefSAlexander Motin }
183e770bc6bSMatt Jacob 
184e770bc6bSMatt Jacob static void
185e770bc6bSMatt Jacob g_mpd(void *arg, int flags __unused)
186e770bc6bSMatt Jacob {
1870c883cefSAlexander Motin 	struct g_geom *gp;
1880c883cefSAlexander Motin 	struct g_multipath_softc *sc;
189e770bc6bSMatt Jacob 	struct g_consumer *cp;
1900c883cefSAlexander Motin 	int w;
191e770bc6bSMatt Jacob 
192e770bc6bSMatt Jacob 	g_topology_assert();
193e770bc6bSMatt Jacob 	cp = arg;
1940c883cefSAlexander Motin 	gp = cp->geom;
1950c883cefSAlexander Motin 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) {
1960c883cefSAlexander Motin 		w = cp->acw;
197e770bc6bSMatt Jacob 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
1980c883cefSAlexander Motin 		if (w > 0 && cp->provider != NULL &&
1990c883cefSAlexander Motin 		    (cp->provider->geom->flags & G_GEOM_WITHER) == 0) {
2000c883cefSAlexander Motin 			g_post_event(g_mpd, cp, M_WAITOK, NULL);
2010c883cefSAlexander Motin 			return;
2020c883cefSAlexander Motin 		}
2030c883cefSAlexander Motin 	}
2040c883cefSAlexander Motin 	sc = gp->softc;
2050c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
206e770bc6bSMatt Jacob 	if (cp->provider) {
207e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: %s removed from %s\n",
2080c883cefSAlexander Motin 		    cp->provider->name, gp->name);
209e770bc6bSMatt Jacob 		g_detach(cp);
210e770bc6bSMatt Jacob 	}
211e770bc6bSMatt Jacob 	g_destroy_consumer(cp);
2120c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
2130c883cefSAlexander Motin 	if (LIST_EMPTY(&gp->consumer))
2140c883cefSAlexander Motin 		g_multipath_destroy(gp);
215e770bc6bSMatt Jacob }
216e770bc6bSMatt Jacob 
217e770bc6bSMatt Jacob static void
218e770bc6bSMatt Jacob g_multipath_orphan(struct g_consumer *cp)
219e770bc6bSMatt Jacob {
2200c883cefSAlexander Motin 	struct g_multipath_softc *sc;
2210c883cefSAlexander Motin 	uintptr_t *cnt;
2220c883cefSAlexander Motin 
2230c883cefSAlexander Motin 	g_topology_assert();
2240c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s in %s was disconnected\n",
225e770bc6bSMatt Jacob 	    cp->provider->name, cp->geom->name);
2260c883cefSAlexander Motin 	sc = cp->geom->softc;
2270c883cefSAlexander Motin 	cnt = (uintptr_t *)&cp->private;
2280c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
2290c883cefSAlexander Motin 	sc->sc_ndisks--;
2300c883cefSAlexander Motin 	g_multipath_fault(cp, MP_LOST);
2310c883cefSAlexander Motin 	if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
2320c883cefSAlexander Motin 		cp->index |= MP_POSTED;
2330c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
234e770bc6bSMatt Jacob 		g_mpd(cp, 0);
2350c883cefSAlexander Motin 	} else
2360c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
237e770bc6bSMatt Jacob }
238e770bc6bSMatt Jacob 
239e770bc6bSMatt Jacob static void
240e770bc6bSMatt Jacob g_multipath_start(struct bio *bp)
241e770bc6bSMatt Jacob {
242e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
243e770bc6bSMatt Jacob 	struct g_geom *gp;
244e770bc6bSMatt Jacob 	struct g_consumer *cp;
245e770bc6bSMatt Jacob 	struct bio *cbp;
2460c883cefSAlexander Motin 	uintptr_t *cnt;
247e770bc6bSMatt Jacob 
248e770bc6bSMatt Jacob 	gp = bp->bio_to->geom;
249e770bc6bSMatt Jacob 	sc = gp->softc;
250e770bc6bSMatt Jacob 	KASSERT(sc != NULL, ("NULL sc"));
251e770bc6bSMatt Jacob 	cbp = g_clone_bio(bp);
252e770bc6bSMatt Jacob 	if (cbp == NULL) {
253e770bc6bSMatt Jacob 		g_io_deliver(bp, ENOMEM);
254e770bc6bSMatt Jacob 		return;
255e770bc6bSMatt Jacob 	}
2560c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
25763297dfdSAlexander Motin 	cp = g_multipath_choose(gp, bp);
2580c883cefSAlexander Motin 	if (cp == NULL) {
2590c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
2600c883cefSAlexander Motin 		g_destroy_bio(cbp);
2610c883cefSAlexander Motin 		g_io_deliver(bp, ENXIO);
2620c883cefSAlexander Motin 		return;
2630c883cefSAlexander Motin 	}
2640c883cefSAlexander Motin 	if ((uintptr_t)bp->bio_driver1 < sc->sc_ndisks)
2650c883cefSAlexander Motin 		bp->bio_driver1 = (void *)(uintptr_t)sc->sc_ndisks;
2660c883cefSAlexander Motin 	cnt = (uintptr_t *)&cp->private;
2670c883cefSAlexander Motin 	(*cnt)++;
2680c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
269e770bc6bSMatt Jacob 	cbp->bio_done = g_multipath_done;
270e770bc6bSMatt Jacob 	g_io_request(cbp, cp);
271e770bc6bSMatt Jacob }
272e770bc6bSMatt Jacob 
273e770bc6bSMatt Jacob static void
274e770bc6bSMatt Jacob g_multipath_done(struct bio *bp)
275e770bc6bSMatt Jacob {
2760c883cefSAlexander Motin 	struct g_multipath_softc *sc;
2770c883cefSAlexander Motin 	struct g_consumer *cp;
2780c883cefSAlexander Motin 	uintptr_t *cnt;
2790c883cefSAlexander Motin 
280e770bc6bSMatt Jacob 	if (bp->bio_error == ENXIO || bp->bio_error == EIO) {
281e770bc6bSMatt Jacob 		mtx_lock(&gmtbq_mtx);
282e770bc6bSMatt Jacob 		bioq_insert_tail(&gmtbq, bp);
283e770bc6bSMatt Jacob 		mtx_unlock(&gmtbq_mtx);
2840c883cefSAlexander Motin 		wakeup(&g_multipath_kt_state);
285e770bc6bSMatt Jacob 	} else {
2860c883cefSAlexander Motin 		cp = bp->bio_from;
2870c883cefSAlexander Motin 		sc = cp->geom->softc;
2880c883cefSAlexander Motin 		cnt = (uintptr_t *)&cp->private;
2890c883cefSAlexander Motin 		mtx_lock(&sc->sc_mtx);
2900c883cefSAlexander Motin 		(*cnt)--;
2910c883cefSAlexander Motin 		if (*cnt == 0 && (cp->index & MP_LOST)) {
2920c883cefSAlexander Motin 			cp->index |= MP_POSTED;
2930c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
2940c883cefSAlexander Motin 			g_post_event(g_mpd, cp, M_WAITOK, NULL);
2950c883cefSAlexander Motin 		} else
2960c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
297e770bc6bSMatt Jacob 		g_std_done(bp);
298e770bc6bSMatt Jacob 	}
299e770bc6bSMatt Jacob }
300e770bc6bSMatt Jacob 
301e770bc6bSMatt Jacob static void
302e770bc6bSMatt Jacob g_multipath_done_error(struct bio *bp)
303e770bc6bSMatt Jacob {
304e770bc6bSMatt Jacob 	struct bio *pbp;
305e770bc6bSMatt Jacob 	struct g_geom *gp;
306e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
307e770bc6bSMatt Jacob 	struct g_consumer *cp;
308e770bc6bSMatt Jacob 	struct g_provider *pp;
3090c883cefSAlexander Motin 	uintptr_t *cnt;
310e770bc6bSMatt Jacob 
311e770bc6bSMatt Jacob 	/*
312e770bc6bSMatt Jacob 	 * If we had a failure, we have to check first to see
313e770bc6bSMatt Jacob 	 * whether the consumer it failed on was the currently
314e770bc6bSMatt Jacob 	 * active consumer (i.e., this is the first in perhaps
315e770bc6bSMatt Jacob 	 * a number of failures). If so, we then switch consumers
316e770bc6bSMatt Jacob 	 * to the next available consumer.
317e770bc6bSMatt Jacob 	 */
318e770bc6bSMatt Jacob 
319e770bc6bSMatt Jacob 	pbp = bp->bio_parent;
320e770bc6bSMatt Jacob 	gp = pbp->bio_to->geom;
321e770bc6bSMatt Jacob 	sc = gp->softc;
322e770bc6bSMatt Jacob 	cp = bp->bio_from;
323e770bc6bSMatt Jacob 	pp = cp->provider;
3240c883cefSAlexander Motin 	cnt = (uintptr_t *)&cp->private;
325e770bc6bSMatt Jacob 
3260c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
32763297dfdSAlexander Motin 	if ((cp->index & MP_FAIL) == 0) {
3280c883cefSAlexander Motin 		printf("GEOM_MULTIPATH: Error %d, %s in %s marked FAIL\n",
3290c883cefSAlexander Motin 		    bp->bio_error, pp->name, sc->sc_name);
3300c883cefSAlexander Motin 		g_multipath_fault(cp, MP_FAIL);
33163297dfdSAlexander Motin 	}
3320c883cefSAlexander Motin 	(*cnt)--;
3330c883cefSAlexander Motin 	if (*cnt == 0 && (cp->index & (MP_LOST | MP_POSTED)) == MP_LOST) {
334e770bc6bSMatt Jacob 		cp->index |= MP_POSTED;
3350c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
3360c883cefSAlexander Motin 		g_post_event(g_mpd, cp, M_WAITOK, NULL);
3370c883cefSAlexander Motin 	} else
3380c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
339e770bc6bSMatt Jacob 
340e770bc6bSMatt Jacob 	/*
341e770bc6bSMatt Jacob 	 * If we can fruitfully restart the I/O, do so.
342e770bc6bSMatt Jacob 	 */
3430c883cefSAlexander Motin 	if (pbp->bio_children < (uintptr_t)pbp->bio_driver1) {
3440c883cefSAlexander Motin 		pbp->bio_inbed++;
345e770bc6bSMatt Jacob 		g_destroy_bio(bp);
346e770bc6bSMatt Jacob 		g_multipath_start(pbp);
347e770bc6bSMatt Jacob 	} else {
348e770bc6bSMatt Jacob 		g_std_done(bp);
349e770bc6bSMatt Jacob 	}
350e770bc6bSMatt Jacob }
351e770bc6bSMatt Jacob 
352e770bc6bSMatt Jacob static void
353e770bc6bSMatt Jacob g_multipath_kt(void *arg)
354e770bc6bSMatt Jacob {
35512f35a61SPawel Jakub Dawidek 
356e770bc6bSMatt Jacob 	g_multipath_kt_state = GKT_RUN;
357e770bc6bSMatt Jacob 	mtx_lock(&gmtbq_mtx);
358e770bc6bSMatt Jacob 	while (g_multipath_kt_state == GKT_RUN) {
359e770bc6bSMatt Jacob 		for (;;) {
360e770bc6bSMatt Jacob 			struct bio *bp;
36112f35a61SPawel Jakub Dawidek 
362e770bc6bSMatt Jacob 			bp = bioq_takefirst(&gmtbq);
36312f35a61SPawel Jakub Dawidek 			if (bp == NULL)
364e770bc6bSMatt Jacob 				break;
365e770bc6bSMatt Jacob 			mtx_unlock(&gmtbq_mtx);
366e770bc6bSMatt Jacob 			g_multipath_done_error(bp);
367e770bc6bSMatt Jacob 			mtx_lock(&gmtbq_mtx);
368e770bc6bSMatt Jacob 		}
36963297dfdSAlexander Motin 		if (g_multipath_kt_state != GKT_RUN)
37063297dfdSAlexander Motin 			break;
371e770bc6bSMatt Jacob 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
37263297dfdSAlexander Motin 		    "gkt:wait", 0);
373e770bc6bSMatt Jacob 	}
374e770bc6bSMatt Jacob 	mtx_unlock(&gmtbq_mtx);
375e770bc6bSMatt Jacob 	wakeup(&g_multipath_kt_state);
3763745c395SJulian Elischer 	kproc_exit(0);
377e770bc6bSMatt Jacob }
378e770bc6bSMatt Jacob 
379e770bc6bSMatt Jacob 
380e770bc6bSMatt Jacob static int
381e770bc6bSMatt Jacob g_multipath_access(struct g_provider *pp, int dr, int dw, int de)
382e770bc6bSMatt Jacob {
383e770bc6bSMatt Jacob 	struct g_geom *gp;
384e770bc6bSMatt Jacob 	struct g_consumer *cp, *badcp = NULL;
3850c883cefSAlexander Motin 	struct g_multipath_softc *sc;
386e770bc6bSMatt Jacob 	int error;
387e770bc6bSMatt Jacob 
388e770bc6bSMatt Jacob 	gp = pp->geom;
389e770bc6bSMatt Jacob 
390e770bc6bSMatt Jacob 	LIST_FOREACH(cp, &gp->consumer, consumer) {
391e770bc6bSMatt Jacob 		error = g_access(cp, dr, dw, de);
392e770bc6bSMatt Jacob 		if (error) {
393e770bc6bSMatt Jacob 			badcp = cp;
394e770bc6bSMatt Jacob 			goto fail;
395e770bc6bSMatt Jacob 		}
396e770bc6bSMatt Jacob 	}
3970c883cefSAlexander Motin 	sc = gp->softc;
3980c883cefSAlexander Motin 	sc->sc_opened += dr + dw + de;
3990c883cefSAlexander Motin 	if (sc->sc_stopping && sc->sc_opened == 0)
4000c883cefSAlexander Motin 		g_multipath_destroy(gp);
401e770bc6bSMatt Jacob 	return (0);
402e770bc6bSMatt Jacob 
403e770bc6bSMatt Jacob fail:
404e770bc6bSMatt Jacob 	LIST_FOREACH(cp, &gp->consumer, consumer) {
40512f35a61SPawel Jakub Dawidek 		if (cp == badcp)
406e770bc6bSMatt Jacob 			break;
407e770bc6bSMatt Jacob 		(void) g_access(cp, -dr, -dw, -de);
408e770bc6bSMatt Jacob 	}
409e770bc6bSMatt Jacob 	return (error);
410e770bc6bSMatt Jacob }
411e770bc6bSMatt Jacob 
412e770bc6bSMatt Jacob static struct g_geom *
413e770bc6bSMatt Jacob g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md)
414e770bc6bSMatt Jacob {
415e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
416e770bc6bSMatt Jacob 	struct g_geom *gp;
417e770bc6bSMatt Jacob 	struct g_provider *pp;
418e770bc6bSMatt Jacob 
419e770bc6bSMatt Jacob 	g_topology_assert();
420e770bc6bSMatt Jacob 
421e770bc6bSMatt Jacob 	LIST_FOREACH(gp, &mp->geom, geom) {
4220c883cefSAlexander Motin 		sc = gp->softc;
4230c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
4240c883cefSAlexander Motin 			continue;
425e770bc6bSMatt Jacob 		if (strcmp(gp->name, md->md_name) == 0) {
426e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: name %s already exists\n",
427e770bc6bSMatt Jacob 			    md->md_name);
428e770bc6bSMatt Jacob 			return (NULL);
429e770bc6bSMatt Jacob 		}
430e770bc6bSMatt Jacob 	}
431e770bc6bSMatt Jacob 
432e770bc6bSMatt Jacob 	gp = g_new_geomf(mp, md->md_name);
433e770bc6bSMatt Jacob 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
4340c883cefSAlexander Motin 	mtx_init(&sc->sc_mtx, "multipath", NULL, MTX_DEF);
4350c883cefSAlexander Motin 	memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid));
4360c883cefSAlexander Motin 	memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name));
4370c883cefSAlexander Motin 	sc->sc_active_active = md->md_active_active;
438e770bc6bSMatt Jacob 	gp->softc = sc;
439e770bc6bSMatt Jacob 	gp->start = g_multipath_start;
440e770bc6bSMatt Jacob 	gp->orphan = g_multipath_orphan;
441e770bc6bSMatt Jacob 	gp->access = g_multipath_access;
4420c883cefSAlexander Motin 	gp->dumpconf = g_multipath_dumpconf;
443e770bc6bSMatt Jacob 
444e770bc6bSMatt Jacob 	pp = g_new_providerf(gp, "multipath/%s", md->md_name);
4450c883cefSAlexander Motin 	if (md->md_size != 0) {
4460c883cefSAlexander Motin 		pp->mediasize = md->md_size -
4470c883cefSAlexander Motin 		    ((md->md_uuid[0] != 0) ? md->md_sectorsize : 0);
448e770bc6bSMatt Jacob 		pp->sectorsize = md->md_sectorsize;
4490c883cefSAlexander Motin 	}
4500c883cefSAlexander Motin 	sc->sc_pp = pp;
451e770bc6bSMatt Jacob 	g_error_provider(pp, 0);
4520c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s created\n", gp->name);
453e770bc6bSMatt Jacob 	return (gp);
454e770bc6bSMatt Jacob }
455e770bc6bSMatt Jacob 
456e770bc6bSMatt Jacob static int
457e770bc6bSMatt Jacob g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp)
458e770bc6bSMatt Jacob {
459e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
460e770bc6bSMatt Jacob 	struct g_consumer *cp, *nxtcp;
4610c883cefSAlexander Motin 	int error, acr, acw, ace;
462e770bc6bSMatt Jacob 
463e770bc6bSMatt Jacob 	g_topology_assert();
464e770bc6bSMatt Jacob 
465e770bc6bSMatt Jacob 	sc = gp->softc;
466e770bc6bSMatt Jacob 	KASSERT(sc, ("no softc"));
467e770bc6bSMatt Jacob 
468e770bc6bSMatt Jacob 	/*
469e770bc6bSMatt Jacob 	 * Make sure that the passed provider isn't already attached
470e770bc6bSMatt Jacob 	 */
471e770bc6bSMatt Jacob 	LIST_FOREACH(cp, &gp->consumer, consumer) {
47212f35a61SPawel Jakub Dawidek 		if (cp->provider == pp)
473e770bc6bSMatt Jacob 			break;
474e770bc6bSMatt Jacob 	}
475e770bc6bSMatt Jacob 	if (cp) {
476e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: provider %s already attached to %s\n",
477e770bc6bSMatt Jacob 		    pp->name, gp->name);
478e770bc6bSMatt Jacob 		return (EEXIST);
479e770bc6bSMatt Jacob 	}
480e770bc6bSMatt Jacob 	nxtcp = LIST_FIRST(&gp->consumer);
481e770bc6bSMatt Jacob 	cp = g_new_consumer(gp);
4820c883cefSAlexander Motin 	cp->private = NULL;
4830c883cefSAlexander Motin 	cp->index = MP_NEW;
484e770bc6bSMatt Jacob 	error = g_attach(cp, pp);
485e770bc6bSMatt Jacob 	if (error != 0) {
486e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: cannot attach %s to %s",
487e770bc6bSMatt Jacob 		    pp->name, sc->sc_name);
488e770bc6bSMatt Jacob 		g_destroy_consumer(cp);
489e770bc6bSMatt Jacob 		return (error);
490e770bc6bSMatt Jacob 	}
491e770bc6bSMatt Jacob 
492e770bc6bSMatt Jacob 	/*
493e770bc6bSMatt Jacob 	 * Set access permissions on new consumer to match other consumers
494e770bc6bSMatt Jacob 	 */
4950c883cefSAlexander Motin 	if (sc->sc_pp) {
4960c883cefSAlexander Motin 		acr = sc->sc_pp->acr;
4970c883cefSAlexander Motin 		acw = sc->sc_pp->acw;
4980c883cefSAlexander Motin 		ace = sc->sc_pp->ace;
4990c883cefSAlexander Motin 	} else
5000c883cefSAlexander Motin 		acr = acw = ace = 0;
5010c883cefSAlexander Motin 	if (g_multipath_exclusive) {
5020c883cefSAlexander Motin 		acr++;
5030c883cefSAlexander Motin 		acw++;
5040c883cefSAlexander Motin 		ace++;
5050c883cefSAlexander Motin 	}
5060c883cefSAlexander Motin 	error = g_access(cp, acr, acw, ace);
507e770bc6bSMatt Jacob 	if (error) {
508e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: cannot set access in "
5090c883cefSAlexander Motin 		    "attaching %s to %s (%d)\n",
5100c883cefSAlexander Motin 		    pp->name, sc->sc_name, error);
511e770bc6bSMatt Jacob 		g_detach(cp);
512e770bc6bSMatt Jacob 		g_destroy_consumer(cp);
513e770bc6bSMatt Jacob 		return (error);
514e770bc6bSMatt Jacob 	}
5150c883cefSAlexander Motin 	if (sc->sc_pp != NULL && sc->sc_pp->mediasize == 0) {
5160c883cefSAlexander Motin 		sc->sc_pp->mediasize = pp->mediasize -
5170c883cefSAlexander Motin 		    ((sc->sc_uuid[0] != 0) ? pp->sectorsize : 0);
5180c883cefSAlexander Motin 		sc->sc_pp->sectorsize = pp->sectorsize;
519e770bc6bSMatt Jacob 	}
5200c883cefSAlexander Motin 	if (sc->sc_pp != NULL &&
5210c883cefSAlexander Motin 	    sc->sc_pp->stripesize == 0 && sc->sc_pp->stripeoffset == 0) {
5220c883cefSAlexander Motin 		sc->sc_pp->stripesize = pp->stripesize;
5230c883cefSAlexander Motin 		sc->sc_pp->stripeoffset = pp->stripeoffset;
5240c883cefSAlexander Motin 	}
5250c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
5260c883cefSAlexander Motin 	cp->index = 0;
5270c883cefSAlexander Motin 	sc->sc_ndisks++;
5280c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
5290c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s added to %s\n",
5300c883cefSAlexander Motin 	    pp->name, sc->sc_name);
5310c883cefSAlexander Motin 	if (sc->sc_active == NULL) {
5320c883cefSAlexander Motin 		sc->sc_active = cp;
53363297dfdSAlexander Motin 		if (sc->sc_active_active != 1)
5340c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
535e770bc6bSMatt Jacob 			    pp->name, sc->sc_name);
536e770bc6bSMatt Jacob 	}
537e770bc6bSMatt Jacob 	return (0);
538e770bc6bSMatt Jacob }
539e770bc6bSMatt Jacob 
540e770bc6bSMatt Jacob static int
541e770bc6bSMatt Jacob g_multipath_destroy(struct g_geom *gp)
542e770bc6bSMatt Jacob {
5430c883cefSAlexander Motin 	struct g_multipath_softc *sc;
5440c883cefSAlexander Motin 	struct g_consumer *cp, *cp1;
545e770bc6bSMatt Jacob 
546e770bc6bSMatt Jacob 	g_topology_assert();
54712f35a61SPawel Jakub Dawidek 	if (gp->softc == NULL)
548e770bc6bSMatt Jacob 		return (ENXIO);
5490c883cefSAlexander Motin 	sc = gp->softc;
5500c883cefSAlexander Motin 	if (!sc->sc_stopping) {
551e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
5520c883cefSAlexander Motin 		sc->sc_stopping = 1;
5530c883cefSAlexander Motin 	}
5540c883cefSAlexander Motin 	if (sc->sc_opened != 0) {
5550c883cefSAlexander Motin 		if (sc->sc_pp != NULL) {
5560c883cefSAlexander Motin 			g_wither_provider(sc->sc_pp, ENXIO);
5570c883cefSAlexander Motin 			sc->sc_pp = NULL;
5580c883cefSAlexander Motin 		}
5590c883cefSAlexander Motin 		return (EINPROGRESS);
5600c883cefSAlexander Motin 	}
5610c883cefSAlexander Motin 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
5620c883cefSAlexander Motin 		mtx_lock(&sc->sc_mtx);
5630c883cefSAlexander Motin 		if ((cp->index & MP_POSTED) == 0) {
5640c883cefSAlexander Motin 			cp->index |= MP_POSTED;
5650c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
5660c883cefSAlexander Motin 			g_mpd(cp, 0);
5670c883cefSAlexander Motin 			if (cp1 == NULL)
5680c883cefSAlexander Motin 				return(0);	/* Recursion happened. */
5690c883cefSAlexander Motin 		} else
5700c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
5710c883cefSAlexander Motin 	}
5720c883cefSAlexander Motin 	if (!LIST_EMPTY(&gp->consumer))
5730c883cefSAlexander Motin 		return (EINPROGRESS);
5740c883cefSAlexander Motin 	mtx_destroy(&sc->sc_mtx);
575e770bc6bSMatt Jacob 	g_free(gp->softc);
576e770bc6bSMatt Jacob 	gp->softc = NULL;
5770c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s destroyed\n", gp->name);
578e770bc6bSMatt Jacob 	g_wither_geom(gp, ENXIO);
579e770bc6bSMatt Jacob 	return (0);
580e770bc6bSMatt Jacob }
581e770bc6bSMatt Jacob 
582e770bc6bSMatt Jacob static int
583e770bc6bSMatt Jacob g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
584e770bc6bSMatt Jacob     struct g_geom *gp)
585e770bc6bSMatt Jacob {
58612f35a61SPawel Jakub Dawidek 
587e770bc6bSMatt Jacob 	return (g_multipath_destroy(gp));
588e770bc6bSMatt Jacob }
589e770bc6bSMatt Jacob 
590b5dce617SMatt Jacob static int
591b5dce617SMatt Jacob g_multipath_rotate(struct g_geom *gp)
592b5dce617SMatt Jacob {
593b5dce617SMatt Jacob 	struct g_consumer *lcp;
594b5dce617SMatt Jacob 	struct g_multipath_softc *sc = gp->softc;
595b5dce617SMatt Jacob 
596b5dce617SMatt Jacob 	g_topology_assert();
597b5dce617SMatt Jacob 	if (sc == NULL)
598b5dce617SMatt Jacob 		return (ENXIO);
599b5dce617SMatt Jacob 	LIST_FOREACH(lcp, &gp->consumer, consumer) {
600b5dce617SMatt Jacob 		if ((lcp->index & MP_BAD) == 0) {
6010c883cefSAlexander Motin 			if (sc->sc_active != lcp)
602b5dce617SMatt Jacob 				break;
603b5dce617SMatt Jacob 		}
604b5dce617SMatt Jacob 	}
605b5dce617SMatt Jacob 	if (lcp) {
6060c883cefSAlexander Motin 		sc->sc_active = lcp;
60763297dfdSAlexander Motin 		if (sc->sc_active_active != 1)
6080c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
609b5dce617SMatt Jacob 			    lcp->provider->name, sc->sc_name);
610b5dce617SMatt Jacob 	}
611b5dce617SMatt Jacob 	return (0);
612b5dce617SMatt Jacob }
613b5dce617SMatt Jacob 
614e770bc6bSMatt Jacob static void
615e770bc6bSMatt Jacob g_multipath_init(struct g_class *mp)
616e770bc6bSMatt Jacob {
617e770bc6bSMatt Jacob 	bioq_init(&gmtbq);
618e770bc6bSMatt Jacob 	mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
61963297dfdSAlexander Motin 	kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt");
620e770bc6bSMatt Jacob }
621e770bc6bSMatt Jacob 
622e770bc6bSMatt Jacob static void
623e770bc6bSMatt Jacob g_multipath_fini(struct g_class *mp)
624e770bc6bSMatt Jacob {
625e770bc6bSMatt Jacob 	if (g_multipath_kt_state == GKT_RUN) {
626e770bc6bSMatt Jacob 		mtx_lock(&gmtbq_mtx);
627e770bc6bSMatt Jacob 		g_multipath_kt_state = GKT_DIE;
628e770bc6bSMatt Jacob 		wakeup(&g_multipath_kt_state);
629e770bc6bSMatt Jacob 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
630e770bc6bSMatt Jacob 		    "gmp:fini", 0);
631e770bc6bSMatt Jacob 		mtx_unlock(&gmtbq_mtx);
632e770bc6bSMatt Jacob 	}
633e770bc6bSMatt Jacob }
634e770bc6bSMatt Jacob 
635e770bc6bSMatt Jacob static int
636e770bc6bSMatt Jacob g_multipath_read_metadata(struct g_consumer *cp,
637e770bc6bSMatt Jacob     struct g_multipath_metadata *md)
638e770bc6bSMatt Jacob {
639e770bc6bSMatt Jacob 	struct g_provider *pp;
640e770bc6bSMatt Jacob 	u_char *buf;
641e770bc6bSMatt Jacob 	int error;
642e770bc6bSMatt Jacob 
643e770bc6bSMatt Jacob 	g_topology_assert();
644e770bc6bSMatt Jacob 	error = g_access(cp, 1, 0, 0);
64512f35a61SPawel Jakub Dawidek 	if (error != 0)
646e770bc6bSMatt Jacob 		return (error);
647e770bc6bSMatt Jacob 	pp = cp->provider;
648e770bc6bSMatt Jacob 	g_topology_unlock();
649e770bc6bSMatt Jacob 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
650e770bc6bSMatt Jacob 	    pp->sectorsize, &error);
651e770bc6bSMatt Jacob 	g_topology_lock();
652e770bc6bSMatt Jacob 	g_access(cp, -1, 0, 0);
65312f35a61SPawel Jakub Dawidek 	if (buf == NULL)
654e770bc6bSMatt Jacob 		return (error);
655e770bc6bSMatt Jacob 	multipath_metadata_decode(buf, md);
656e770bc6bSMatt Jacob 	g_free(buf);
657e770bc6bSMatt Jacob 	return (0);
658e770bc6bSMatt Jacob }
659e770bc6bSMatt Jacob 
660e770bc6bSMatt Jacob static struct g_geom *
661e770bc6bSMatt Jacob g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
662e770bc6bSMatt Jacob {
663e770bc6bSMatt Jacob 	struct g_multipath_metadata md;
664e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
665e770bc6bSMatt Jacob 	struct g_consumer *cp;
666e770bc6bSMatt Jacob 	struct g_geom *gp, *gp1;
667e770bc6bSMatt Jacob 	int error, isnew;
668e770bc6bSMatt Jacob 
669e770bc6bSMatt Jacob 	g_topology_assert();
670e770bc6bSMatt Jacob 
671e770bc6bSMatt Jacob 	gp = g_new_geomf(mp, "multipath:taste");
672e770bc6bSMatt Jacob 	gp->start = g_multipath_start;
673e770bc6bSMatt Jacob 	gp->access = g_multipath_access;
674e770bc6bSMatt Jacob 	gp->orphan = g_multipath_orphan;
675e770bc6bSMatt Jacob 	cp = g_new_consumer(gp);
676e770bc6bSMatt Jacob 	g_attach(cp, pp);
677e770bc6bSMatt Jacob 	error = g_multipath_read_metadata(cp, &md);
678e770bc6bSMatt Jacob 	g_detach(cp);
679e770bc6bSMatt Jacob 	g_destroy_consumer(cp);
680e770bc6bSMatt Jacob 	g_destroy_geom(gp);
68112f35a61SPawel Jakub Dawidek 	if (error != 0)
682e770bc6bSMatt Jacob 		return (NULL);
683e770bc6bSMatt Jacob 	gp = NULL;
684e770bc6bSMatt Jacob 
685e770bc6bSMatt Jacob 	if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
68612f35a61SPawel Jakub Dawidek 		if (g_multipath_debug)
687e770bc6bSMatt Jacob 			printf("%s is not MULTIPATH\n", pp->name);
688e770bc6bSMatt Jacob 		return (NULL);
689e770bc6bSMatt Jacob 	}
690e770bc6bSMatt Jacob 	if (md.md_version != G_MULTIPATH_VERSION) {
691e770bc6bSMatt Jacob 		printf("%s has version %d multipath id- this module is version "
692e770bc6bSMatt Jacob 		    " %d: rejecting\n", pp->name, md.md_version,
693e770bc6bSMatt Jacob 		    G_MULTIPATH_VERSION);
694e770bc6bSMatt Jacob 		return (NULL);
695e770bc6bSMatt Jacob 	}
6960c883cefSAlexander Motin 	if (md.md_size != 0 && md.md_size != pp->mediasize)
6970c883cefSAlexander Motin 		return (NULL);
6980c883cefSAlexander Motin 	if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize)
6990c883cefSAlexander Motin 		return (NULL);
70012f35a61SPawel Jakub Dawidek 	if (g_multipath_debug)
701e770bc6bSMatt Jacob 		printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
702e770bc6bSMatt Jacob 
703e770bc6bSMatt Jacob 	/*
704e770bc6bSMatt Jacob 	 * Let's check if such a device already is present. We check against
705e770bc6bSMatt Jacob 	 * uuid alone first because that's the true distinguishor. If that
706e770bc6bSMatt Jacob 	 * passes, then we check for name conflicts. If there are conflicts,
707e770bc6bSMatt Jacob 	 * modify the name.
708e770bc6bSMatt Jacob 	 *
709e770bc6bSMatt Jacob 	 * The whole purpose of this is to solve the problem that people don't
710e770bc6bSMatt Jacob 	 * pick good unique names, but good unique names (like uuids) are a
711e770bc6bSMatt Jacob 	 * pain to use. So, we allow people to build GEOMs with friendly names
712e770bc6bSMatt Jacob 	 * and uuids, and modify the names in case there's a collision.
713e770bc6bSMatt Jacob 	 */
714e770bc6bSMatt Jacob 	sc = NULL;
715e770bc6bSMatt Jacob 	LIST_FOREACH(gp, &mp->geom, geom) {
716e770bc6bSMatt Jacob 		sc = gp->softc;
7170c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
718e770bc6bSMatt Jacob 			continue;
71912f35a61SPawel Jakub Dawidek 		if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
720e770bc6bSMatt Jacob 			break;
721e770bc6bSMatt Jacob 	}
722e770bc6bSMatt Jacob 
723e770bc6bSMatt Jacob 	LIST_FOREACH(gp1, &mp->geom, geom) {
72412f35a61SPawel Jakub Dawidek 		if (gp1 == gp)
725e770bc6bSMatt Jacob 			continue;
726e770bc6bSMatt Jacob 		sc = gp1->softc;
7270c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
728e770bc6bSMatt Jacob 			continue;
72912f35a61SPawel Jakub Dawidek 		if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
730e770bc6bSMatt Jacob 			break;
731e770bc6bSMatt Jacob 	}
732e770bc6bSMatt Jacob 
733e770bc6bSMatt Jacob 	/*
734e770bc6bSMatt Jacob 	 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
735e770bc6bSMatt Jacob 	 *
736e770bc6bSMatt Jacob 	 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
737e770bc6bSMatt Jacob 	 * with the same name (but a different UUID).
738e770bc6bSMatt Jacob 	 *
739e770bc6bSMatt Jacob 	 * If gp is NULL, then modify the name with a random number and
740e770bc6bSMatt Jacob   	 * complain, but allow the creation of the geom to continue.
741e770bc6bSMatt Jacob 	 *
742e770bc6bSMatt Jacob 	 * If gp is *not* NULL, just use the geom's name as we're attaching
743e770bc6bSMatt Jacob 	 * this disk to the (previously generated) name.
744e770bc6bSMatt Jacob 	 */
745e770bc6bSMatt Jacob 
746e770bc6bSMatt Jacob 	if (gp1) {
747e770bc6bSMatt Jacob 		sc = gp1->softc;
748e770bc6bSMatt Jacob 		if (gp == NULL) {
749e770bc6bSMatt Jacob 			char buf[16];
750e770bc6bSMatt Jacob 			u_long rand = random();
751e770bc6bSMatt Jacob 
752e770bc6bSMatt Jacob 			snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
753e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
754e770bc6bSMatt Jacob 			    sc->sc_name, sc->sc_uuid);
755e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
756e770bc6bSMatt Jacob 			    md.md_uuid, buf);
757e770bc6bSMatt Jacob 			strlcpy(md.md_name, buf, sizeof(md.md_name));
758e770bc6bSMatt Jacob 		} else {
759e770bc6bSMatt Jacob 			strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
760e770bc6bSMatt Jacob 		}
761e770bc6bSMatt Jacob 	}
762e770bc6bSMatt Jacob 
763e770bc6bSMatt Jacob 	if (gp == NULL) {
764e770bc6bSMatt Jacob 		gp = g_multipath_create(mp, &md);
765e770bc6bSMatt Jacob 		if (gp == NULL) {
766e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
767e770bc6bSMatt Jacob 			    md.md_name, md.md_uuid);
768e770bc6bSMatt Jacob 			return (NULL);
769e770bc6bSMatt Jacob 		}
770e770bc6bSMatt Jacob 		isnew = 1;
771e770bc6bSMatt Jacob 	} else {
772e770bc6bSMatt Jacob 		isnew = 0;
773e770bc6bSMatt Jacob 	}
774e770bc6bSMatt Jacob 
775e770bc6bSMatt Jacob 	sc = gp->softc;
776e770bc6bSMatt Jacob 	KASSERT(sc != NULL, ("sc is NULL"));
777e770bc6bSMatt Jacob 	error = g_multipath_add_disk(gp, pp);
778e770bc6bSMatt Jacob 	if (error != 0) {
77912f35a61SPawel Jakub Dawidek 		if (isnew)
780e770bc6bSMatt Jacob 			g_multipath_destroy(gp);
781e770bc6bSMatt Jacob 		return (NULL);
782e770bc6bSMatt Jacob 	}
783e770bc6bSMatt Jacob 	return (gp);
784e770bc6bSMatt Jacob }
785e770bc6bSMatt Jacob 
786e770bc6bSMatt Jacob static void
7870c883cefSAlexander Motin g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp,
7880c883cefSAlexander Motin     const char *name)
789e770bc6bSMatt Jacob {
7900c883cefSAlexander Motin 	struct g_multipath_softc *sc;
791e770bc6bSMatt Jacob 	struct g_geom *gp;
7922b4969ffSMatt Jacob 	struct g_consumer *cp;
7930c883cefSAlexander Motin 	struct g_provider *pp;
7940c883cefSAlexander Motin 	const char *mpname;
795e770bc6bSMatt Jacob 	static const char devpf[6] = "/dev/";
796e770bc6bSMatt Jacob 
797e770bc6bSMatt Jacob 	g_topology_assert();
798e770bc6bSMatt Jacob 
799e770bc6bSMatt Jacob 	mpname = gctl_get_asciiparam(req, "arg0");
800e770bc6bSMatt Jacob         if (mpname == NULL) {
801e770bc6bSMatt Jacob                 gctl_error(req, "No 'arg0' argument");
802e770bc6bSMatt Jacob                 return;
803e770bc6bSMatt Jacob         }
8042b4969ffSMatt Jacob 	gp = g_multipath_find_geom(mp, mpname);
8052b4969ffSMatt Jacob 	if (gp == NULL) {
8062b4969ffSMatt Jacob 		gctl_error(req, "Device %s is invalid", mpname);
807e770bc6bSMatt Jacob 		return;
808e770bc6bSMatt Jacob 	}
8090c883cefSAlexander Motin 	sc = gp->softc;
810e770bc6bSMatt Jacob 
81112f35a61SPawel Jakub Dawidek 	if (strncmp(name, devpf, 5) == 0)
812e770bc6bSMatt Jacob 		name += 5;
8132b4969ffSMatt Jacob 	pp = g_provider_by_name(name);
8142b4969ffSMatt Jacob 	if (pp == NULL) {
815e770bc6bSMatt Jacob 		gctl_error(req, "Provider %s is invalid", name);
816e770bc6bSMatt Jacob 		return;
817e770bc6bSMatt Jacob 	}
818e770bc6bSMatt Jacob 
819e770bc6bSMatt Jacob 	/*
8200c883cefSAlexander Motin 	 * Check to make sure parameters match.
821e770bc6bSMatt Jacob 	 */
8220c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
8230c883cefSAlexander Motin 		if (cp->provider == pp) {
8240c883cefSAlexander Motin 			gctl_error(req, "provider %s is already there",
8250c883cefSAlexander Motin 			    pp->name);
826e770bc6bSMatt Jacob 			return;
827e770bc6bSMatt Jacob 		}
8280c883cefSAlexander Motin 	}
8290c883cefSAlexander Motin 	if (sc->sc_pp != NULL && sc->sc_pp->mediasize != 0 &&
8300c883cefSAlexander Motin 	    sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0)
8310c883cefSAlexander Motin 	     != pp->mediasize) {
8320c883cefSAlexander Motin 		gctl_error(req, "Providers size mismatch %jd != %jd",
8330c883cefSAlexander Motin 		    (intmax_t) sc->sc_pp->mediasize +
8340c883cefSAlexander Motin 			(sc->sc_uuid[0] != 0 ? pp->sectorsize : 0),
8350c883cefSAlexander Motin 		    (intmax_t) pp->mediasize);
836e770bc6bSMatt Jacob 		return;
837e770bc6bSMatt Jacob 	}
8380c883cefSAlexander Motin 	if (sc->sc_pp != NULL && sc->sc_pp->sectorsize != 0 &&
8390c883cefSAlexander Motin 	    sc->sc_pp->sectorsize != pp->sectorsize) {
8400c883cefSAlexander Motin 		gctl_error(req, "Providers sectorsize mismatch %u != %u",
8410c883cefSAlexander Motin 		    sc->sc_pp->sectorsize, pp->sectorsize);
842e770bc6bSMatt Jacob 		return;
843e770bc6bSMatt Jacob 	}
844e770bc6bSMatt Jacob 
845e770bc6bSMatt Jacob 	/*
8462b4969ffSMatt Jacob 	 * Now add....
847e770bc6bSMatt Jacob 	 */
8482b4969ffSMatt Jacob 	(void) g_multipath_add_disk(gp, pp);
849e770bc6bSMatt Jacob }
850e770bc6bSMatt Jacob 
8510c883cefSAlexander Motin static void
8520c883cefSAlexander Motin g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp)
8530c883cefSAlexander Motin {
8540c883cefSAlexander Motin 	struct g_multipath_softc *sc;
8550c883cefSAlexander Motin 	struct g_geom *gp;
8560c883cefSAlexander Motin 	const char *mpname, *name;
8570c883cefSAlexander Motin 
8580c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
8590c883cefSAlexander Motin         if (mpname == NULL) {
8600c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
8610c883cefSAlexander Motin                 return;
8620c883cefSAlexander Motin         }
8630c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
8640c883cefSAlexander Motin 	if (gp == NULL) {
8650c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
8660c883cefSAlexander Motin 		return;
8670c883cefSAlexander Motin 	}
8680c883cefSAlexander Motin 	sc = gp->softc;
8690c883cefSAlexander Motin 
8700c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
8710c883cefSAlexander Motin 	if (name == NULL) {
8720c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
8730c883cefSAlexander Motin 		return;
8740c883cefSAlexander Motin 	}
8750c883cefSAlexander Motin 	g_multipath_ctl_add_name(req, mp, name);
8760c883cefSAlexander Motin }
8770c883cefSAlexander Motin 
8780c883cefSAlexander Motin static void
8790c883cefSAlexander Motin g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp)
8800c883cefSAlexander Motin {
8810c883cefSAlexander Motin 	struct g_multipath_metadata md;
8820c883cefSAlexander Motin 	struct g_multipath_softc *sc;
8830c883cefSAlexander Motin 	struct g_geom *gp;
8840c883cefSAlexander Motin 	const char *mpname, *name;
8850c883cefSAlexander Motin 	char param[16];
88663297dfdSAlexander Motin 	int *nargs, i, *val;
8870c883cefSAlexander Motin 
8880c883cefSAlexander Motin 	g_topology_assert();
8890c883cefSAlexander Motin 
8900c883cefSAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
8910c883cefSAlexander Motin 	if (*nargs < 2) {
8920c883cefSAlexander Motin 		gctl_error(req, "wrong number of arguments.");
8930c883cefSAlexander Motin 		return;
8940c883cefSAlexander Motin 	}
8950c883cefSAlexander Motin 
8960c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
8970c883cefSAlexander Motin         if (mpname == NULL) {
8980c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
8990c883cefSAlexander Motin                 return;
9000c883cefSAlexander Motin         }
9010c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
9020c883cefSAlexander Motin 	if (gp != NULL) {
9030c883cefSAlexander Motin 		gctl_error(req, "Device %s already exist", mpname);
9040c883cefSAlexander Motin 		return;
9050c883cefSAlexander Motin 	}
9060c883cefSAlexander Motin 	sc = gp->softc;
9070c883cefSAlexander Motin 
9080c883cefSAlexander Motin 	memset(&md, 0, sizeof(md));
9090c883cefSAlexander Motin 	strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
9100c883cefSAlexander Motin 	md.md_version = G_MULTIPATH_VERSION;
9110c883cefSAlexander Motin 	strlcpy(md.md_name, mpname, sizeof(md.md_name));
9120c883cefSAlexander Motin 	md.md_size = 0;
9130c883cefSAlexander Motin 	md.md_sectorsize = 0;
9140c883cefSAlexander Motin 	md.md_uuid[0] = 0;
91563297dfdSAlexander Motin 	md.md_active_active = 0;
91663297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_active", sizeof(*val));
91763297dfdSAlexander Motin 	if (val != NULL && *val != 0)
91863297dfdSAlexander Motin 		md.md_active_active = 1;
91963297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_read", sizeof(*val));
92063297dfdSAlexander Motin 	if (val != NULL && *val != 0)
92163297dfdSAlexander Motin 		md.md_active_active = 2;
9220c883cefSAlexander Motin 	gp = g_multipath_create(mp, &md);
9230c883cefSAlexander Motin 	if (gp == NULL) {
9240c883cefSAlexander Motin 		gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n",
9250c883cefSAlexander Motin 		    md.md_name, md.md_uuid);
9260c883cefSAlexander Motin 		return;
9270c883cefSAlexander Motin 	}
9280c883cefSAlexander Motin 	sc = gp->softc;
9290c883cefSAlexander Motin 
9300c883cefSAlexander Motin 	for (i = 1; i < *nargs; i++) {
9310c883cefSAlexander Motin 		snprintf(param, sizeof(param), "arg%d", i);
9320c883cefSAlexander Motin 		name = gctl_get_asciiparam(req, param);
9330c883cefSAlexander Motin 		g_multipath_ctl_add_name(req, mp, name);
9340c883cefSAlexander Motin 	}
9350c883cefSAlexander Motin 
9360c883cefSAlexander Motin 	if (sc->sc_ndisks != (*nargs - 1))
9370c883cefSAlexander Motin 		g_multipath_destroy(gp);
9380c883cefSAlexander Motin }
9390c883cefSAlexander Motin 
9400c883cefSAlexander Motin static void
94163297dfdSAlexander Motin g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp)
94263297dfdSAlexander Motin {
94363297dfdSAlexander Motin 	struct g_multipath_softc *sc;
94463297dfdSAlexander Motin 	struct g_geom *gp;
94563297dfdSAlexander Motin 	struct g_consumer *cp;
94663297dfdSAlexander Motin 	struct g_provider *pp;
947*c0b1ef66SAlexander Motin 	struct g_multipath_metadata md;
94863297dfdSAlexander Motin 	const char *name;
94963297dfdSAlexander Motin 	int error, *val;
95063297dfdSAlexander Motin 	void *buf;
95163297dfdSAlexander Motin 
95263297dfdSAlexander Motin 	g_topology_assert();
95363297dfdSAlexander Motin 
95463297dfdSAlexander Motin 	name = gctl_get_asciiparam(req, "arg0");
95563297dfdSAlexander Motin 	if (name == NULL) {
95663297dfdSAlexander Motin 		gctl_error(req, "No 'arg0' argument");
95763297dfdSAlexander Motin 		return;
95863297dfdSAlexander Motin 	}
95963297dfdSAlexander Motin 	gp = g_multipath_find_geom(mp, name);
96063297dfdSAlexander Motin 	if (gp == NULL) {
96163297dfdSAlexander Motin 		gctl_error(req, "Device %s is invalid", name);
96263297dfdSAlexander Motin 		return;
96363297dfdSAlexander Motin 	}
96463297dfdSAlexander Motin 	sc = gp->softc;
96563297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_active", sizeof(*val));
96663297dfdSAlexander Motin 	if (val != NULL && *val != 0)
96763297dfdSAlexander Motin 		sc->sc_active_active = 1;
96863297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_read", sizeof(*val));
96963297dfdSAlexander Motin 	if (val != NULL && *val != 0)
97063297dfdSAlexander Motin 		sc->sc_active_active = 2;
97163297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_passive", sizeof(*val));
97263297dfdSAlexander Motin 	if (val != NULL && *val != 0)
97363297dfdSAlexander Motin 		sc->sc_active_active = 0;
97463297dfdSAlexander Motin 	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
97563297dfdSAlexander Motin 		cp = sc->sc_active;
97663297dfdSAlexander Motin 		pp = cp->provider;
97763297dfdSAlexander Motin 		error = g_access(cp, 1, 1, 1);
97863297dfdSAlexander Motin 		if (error != 0) {
97963297dfdSAlexander Motin 			gctl_error(req, "Can't open %s (%d)", pp->name, error);
98063297dfdSAlexander Motin 			return;
98163297dfdSAlexander Motin 		}
98263297dfdSAlexander Motin 		g_topology_unlock();
983*c0b1ef66SAlexander Motin 		buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
984*c0b1ef66SAlexander Motin 		strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
985*c0b1ef66SAlexander Motin 		memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
986*c0b1ef66SAlexander Motin 		strlcpy(md.md_name, name, sizeof(md.md_name));
987*c0b1ef66SAlexander Motin 		md.md_version = G_MULTIPATH_VERSION;
988*c0b1ef66SAlexander Motin 		md.md_size = pp->mediasize;
989*c0b1ef66SAlexander Motin 		md.md_sectorsize = pp->sectorsize;
990*c0b1ef66SAlexander Motin 		md.md_active_active = sc->sc_active_active;
991*c0b1ef66SAlexander Motin 		multipath_metadata_encode(&md, buf);
99263297dfdSAlexander Motin 		error = g_write_data(cp, pp->mediasize - pp->sectorsize,
99363297dfdSAlexander Motin 		    buf, pp->sectorsize);
99463297dfdSAlexander Motin 		g_topology_lock();
99563297dfdSAlexander Motin 		g_access(cp, -1, -1, -1);
99663297dfdSAlexander Motin 		if (error != 0)
99763297dfdSAlexander Motin 			gctl_error(req, "Can't update metadata on %s (%d)",
99863297dfdSAlexander Motin 			    pp->name, error);
99963297dfdSAlexander Motin 	}
100063297dfdSAlexander Motin }
100163297dfdSAlexander Motin 
100263297dfdSAlexander Motin static void
10030c883cefSAlexander Motin g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail)
10040c883cefSAlexander Motin {
10050c883cefSAlexander Motin 	struct g_multipath_softc *sc;
10060c883cefSAlexander Motin 	struct g_geom *gp;
10070c883cefSAlexander Motin 	struct g_consumer *cp;
10080c883cefSAlexander Motin 	const char *mpname, *name;
10090c883cefSAlexander Motin 	int found;
10100c883cefSAlexander Motin 
10110c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
10120c883cefSAlexander Motin         if (mpname == NULL) {
10130c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
10140c883cefSAlexander Motin                 return;
10150c883cefSAlexander Motin         }
10160c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
10170c883cefSAlexander Motin 	if (gp == NULL) {
10180c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
10190c883cefSAlexander Motin 		return;
10200c883cefSAlexander Motin 	}
10210c883cefSAlexander Motin 	sc = gp->softc;
10220c883cefSAlexander Motin 
10230c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
10240c883cefSAlexander Motin 	if (name == NULL) {
10250c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
10260c883cefSAlexander Motin 		return;
10270c883cefSAlexander Motin 	}
10280c883cefSAlexander Motin 
10290c883cefSAlexander Motin 	found = 0;
10300c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
10310c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
10320c883cefSAlexander Motin 		if (cp->provider != NULL &&
10330c883cefSAlexander Motin 		    strcmp(cp->provider->name, name) == 0 &&
10340c883cefSAlexander Motin 		    (cp->index & MP_LOST) == 0) {
10350c883cefSAlexander Motin 			found = 1;
103663297dfdSAlexander Motin 			if (!fail == !(cp->index & MP_FAIL))
103763297dfdSAlexander Motin 				continue;
10380c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: %s in %s is marked %s.\n",
10390c883cefSAlexander Motin 				name, sc->sc_name, fail ? "FAIL" : "OK");
10400c883cefSAlexander Motin 			if (fail) {
10410c883cefSAlexander Motin 				g_multipath_fault(cp, MP_FAIL);
10420c883cefSAlexander Motin 			} else {
10430c883cefSAlexander Motin 				cp->index &= ~MP_FAIL;
10440c883cefSAlexander Motin 			}
10450c883cefSAlexander Motin 		}
10460c883cefSAlexander Motin 	}
10470c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
10480c883cefSAlexander Motin 	if (found == 0)
10490c883cefSAlexander Motin 		gctl_error(req, "Provider %s not found", name);
10500c883cefSAlexander Motin }
10510c883cefSAlexander Motin 
10520c883cefSAlexander Motin static void
10530c883cefSAlexander Motin g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp)
10540c883cefSAlexander Motin {
10550c883cefSAlexander Motin 	struct g_multipath_softc *sc;
10560c883cefSAlexander Motin 	struct g_geom *gp;
10570c883cefSAlexander Motin 	struct g_consumer *cp, *cp1;
10580c883cefSAlexander Motin 	const char *mpname, *name;
10590c883cefSAlexander Motin 	uintptr_t *cnt;
10600c883cefSAlexander Motin 	int found;
10610c883cefSAlexander Motin 
10620c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
10630c883cefSAlexander Motin         if (mpname == NULL) {
10640c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
10650c883cefSAlexander Motin                 return;
10660c883cefSAlexander Motin         }
10670c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
10680c883cefSAlexander Motin 	if (gp == NULL) {
10690c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
10700c883cefSAlexander Motin 		return;
10710c883cefSAlexander Motin 	}
10720c883cefSAlexander Motin 	sc = gp->softc;
10730c883cefSAlexander Motin 
10740c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
10750c883cefSAlexander Motin 	if (name == NULL) {
10760c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
10770c883cefSAlexander Motin 		return;
10780c883cefSAlexander Motin 	}
10790c883cefSAlexander Motin 
10800c883cefSAlexander Motin 	found = 0;
10810c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
10820c883cefSAlexander Motin 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
10830c883cefSAlexander Motin 		if (cp->provider != NULL &&
10840c883cefSAlexander Motin 		    strcmp(cp->provider->name, name) == 0 &&
10850c883cefSAlexander Motin 		    (cp->index & MP_LOST) == 0) {
10860c883cefSAlexander Motin 			found = 1;
10870c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: removing %s from %s\n",
10880c883cefSAlexander Motin 			    cp->provider->name, cp->geom->name);
10890c883cefSAlexander Motin 			sc->sc_ndisks--;
10900c883cefSAlexander Motin 			g_multipath_fault(cp, MP_LOST);
10910c883cefSAlexander Motin 			cnt = (uintptr_t *)&cp->private;
10920c883cefSAlexander Motin 			if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
10930c883cefSAlexander Motin 				cp->index |= MP_POSTED;
10940c883cefSAlexander Motin 				mtx_unlock(&sc->sc_mtx);
10950c883cefSAlexander Motin 				g_mpd(cp, 0);
10960c883cefSAlexander Motin 				if (cp1 == NULL)
10970c883cefSAlexander Motin 					return;	/* Recursion happened. */
10980c883cefSAlexander Motin 				mtx_lock(&sc->sc_mtx);
10990c883cefSAlexander Motin 			}
11000c883cefSAlexander Motin 		}
11010c883cefSAlexander Motin 	}
11020c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
11030c883cefSAlexander Motin 	if (found == 0)
11040c883cefSAlexander Motin 		gctl_error(req, "Provider %s not found", name);
11050c883cefSAlexander Motin }
11060c883cefSAlexander Motin 
1107e770bc6bSMatt Jacob static struct g_geom *
1108e770bc6bSMatt Jacob g_multipath_find_geom(struct g_class *mp, const char *name)
1109e770bc6bSMatt Jacob {
1110e770bc6bSMatt Jacob 	struct g_geom *gp;
11110c883cefSAlexander Motin 	struct g_multipath_softc *sc;
1112e770bc6bSMatt Jacob 
1113e770bc6bSMatt Jacob 	LIST_FOREACH(gp, &mp->geom, geom) {
11140c883cefSAlexander Motin 		sc = gp->softc;
11150c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
11160c883cefSAlexander Motin 			continue;
11170c883cefSAlexander Motin 		if (strcmp(gp->name, name) == 0)
1118e770bc6bSMatt Jacob 			return (gp);
1119e770bc6bSMatt Jacob 	}
1120e770bc6bSMatt Jacob 	return (NULL);
1121e770bc6bSMatt Jacob }
1122e770bc6bSMatt Jacob 
1123e770bc6bSMatt Jacob static void
11240c883cefSAlexander Motin g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp)
1125e770bc6bSMatt Jacob {
1126e770bc6bSMatt Jacob 	struct g_geom *gp;
1127e770bc6bSMatt Jacob 	const char *name;
1128e770bc6bSMatt Jacob 	int error;
1129e770bc6bSMatt Jacob 
1130e770bc6bSMatt Jacob 	g_topology_assert();
1131e770bc6bSMatt Jacob 
1132e770bc6bSMatt Jacob 	name = gctl_get_asciiparam(req, "arg0");
1133e770bc6bSMatt Jacob         if (name == NULL) {
1134e770bc6bSMatt Jacob                 gctl_error(req, "No 'arg0' argument");
1135e770bc6bSMatt Jacob                 return;
1136e770bc6bSMatt Jacob         }
1137e770bc6bSMatt Jacob 	gp = g_multipath_find_geom(mp, name);
1138e770bc6bSMatt Jacob 	if (gp == NULL) {
1139e770bc6bSMatt Jacob 		gctl_error(req, "Device %s is invalid", name);
1140e770bc6bSMatt Jacob 		return;
1141e770bc6bSMatt Jacob 	}
1142e770bc6bSMatt Jacob 	error = g_multipath_destroy(gp);
11430c883cefSAlexander Motin 	if (error != 0 && error != EINPROGRESS)
11440c883cefSAlexander Motin 		gctl_error(req, "failed to stop %s (err=%d)", name, error);
1145e770bc6bSMatt Jacob }
11460c883cefSAlexander Motin 
11470c883cefSAlexander Motin static void
11480c883cefSAlexander Motin g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
11490c883cefSAlexander Motin {
11500c883cefSAlexander Motin 	struct g_geom *gp;
11510c883cefSAlexander Motin 	struct g_multipath_softc *sc;
11520c883cefSAlexander Motin 	struct g_consumer *cp;
11530c883cefSAlexander Motin 	struct g_provider *pp;
11540c883cefSAlexander Motin 	const char *name;
11550c883cefSAlexander Motin 	uint8_t *buf;
11560c883cefSAlexander Motin 	int error;
11570c883cefSAlexander Motin 
11580c883cefSAlexander Motin 	g_topology_assert();
11590c883cefSAlexander Motin 
11600c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg0");
11610c883cefSAlexander Motin         if (name == NULL) {
11620c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
11630c883cefSAlexander Motin                 return;
11640c883cefSAlexander Motin         }
11650c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, name);
11660c883cefSAlexander Motin 	if (gp == NULL) {
11670c883cefSAlexander Motin 		gctl_error(req, "Device %s is invalid", name);
11680c883cefSAlexander Motin 		return;
11690c883cefSAlexander Motin 	}
11700c883cefSAlexander Motin 	sc = gp->softc;
11710c883cefSAlexander Motin 
11720c883cefSAlexander Motin 	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
11730c883cefSAlexander Motin 		cp = sc->sc_active;
11740c883cefSAlexander Motin 		pp = cp->provider;
11750c883cefSAlexander Motin 		error = g_access(cp, 1, 1, 1);
11760c883cefSAlexander Motin 		if (error != 0) {
11770c883cefSAlexander Motin 			gctl_error(req, "Can't open %s (%d)", pp->name, error);
11780c883cefSAlexander Motin 			goto destroy;
11790c883cefSAlexander Motin 		}
11800c883cefSAlexander Motin 		g_topology_unlock();
11810c883cefSAlexander Motin 		buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
11820c883cefSAlexander Motin 		error = g_write_data(cp, pp->mediasize - pp->sectorsize,
11830c883cefSAlexander Motin 		    buf, pp->sectorsize);
11840c883cefSAlexander Motin 		g_topology_lock();
11850c883cefSAlexander Motin 		g_access(cp, -1, -1, -1);
11860c883cefSAlexander Motin 		if (error != 0)
11870c883cefSAlexander Motin 			gctl_error(req, "Can't erase metadata on %s (%d)",
11880c883cefSAlexander Motin 			    pp->name, error);
11890c883cefSAlexander Motin 	}
11900c883cefSAlexander Motin 
11910c883cefSAlexander Motin destroy:
11920c883cefSAlexander Motin 	error = g_multipath_destroy(gp);
11930c883cefSAlexander Motin 	if (error != 0 && error != EINPROGRESS)
11940c883cefSAlexander Motin 		gctl_error(req, "failed to destroy %s (err=%d)", name, error);
1195e770bc6bSMatt Jacob }
1196e770bc6bSMatt Jacob 
1197e770bc6bSMatt Jacob static void
1198b5dce617SMatt Jacob g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp)
1199b5dce617SMatt Jacob {
1200b5dce617SMatt Jacob 	struct g_geom *gp;
1201b5dce617SMatt Jacob 	const char *name;
1202b5dce617SMatt Jacob 	int error;
1203b5dce617SMatt Jacob 
1204b5dce617SMatt Jacob 	g_topology_assert();
1205b5dce617SMatt Jacob 
1206b5dce617SMatt Jacob 	name = gctl_get_asciiparam(req, "arg0");
1207b5dce617SMatt Jacob         if (name == NULL) {
1208b5dce617SMatt Jacob                 gctl_error(req, "No 'arg0' argument");
1209b5dce617SMatt Jacob                 return;
1210b5dce617SMatt Jacob         }
1211b5dce617SMatt Jacob 	gp = g_multipath_find_geom(mp, name);
1212b5dce617SMatt Jacob 	if (gp == NULL) {
1213b5dce617SMatt Jacob 		gctl_error(req, "Device %s is invalid", name);
1214b5dce617SMatt Jacob 		return;
1215b5dce617SMatt Jacob 	}
1216b5dce617SMatt Jacob 	error = g_multipath_rotate(gp);
1217b5dce617SMatt Jacob 	if (error != 0) {
1218b5dce617SMatt Jacob 		gctl_error(req, "failed to rotate %s (err=%d)", name, error);
1219b5dce617SMatt Jacob 	}
1220b5dce617SMatt Jacob }
1221b5dce617SMatt Jacob 
1222b5dce617SMatt Jacob static void
1223b5dce617SMatt Jacob g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp)
1224b5dce617SMatt Jacob {
1225b5dce617SMatt Jacob 	struct sbuf *sb;
1226b5dce617SMatt Jacob 	struct g_geom *gp;
1227b5dce617SMatt Jacob 	struct g_multipath_softc *sc;
12280c883cefSAlexander Motin 	struct g_consumer *cp;
1229b5dce617SMatt Jacob 	const char *name;
12300c883cefSAlexander Motin 	int empty;
1231b5dce617SMatt Jacob 
1232b5dce617SMatt Jacob 	sb = sbuf_new_auto();
1233b5dce617SMatt Jacob 
1234b5dce617SMatt Jacob 	g_topology_assert();
1235b5dce617SMatt Jacob 	name = gctl_get_asciiparam(req, "arg0");
1236b5dce617SMatt Jacob         if (name == NULL) {
1237b5dce617SMatt Jacob                 gctl_error(req, "No 'arg0' argument");
1238b5dce617SMatt Jacob                 return;
1239b5dce617SMatt Jacob         }
1240b5dce617SMatt Jacob 	gp = g_multipath_find_geom(mp, name);
1241b5dce617SMatt Jacob 	if (gp == NULL) {
1242b5dce617SMatt Jacob 		gctl_error(req, "Device %s is invalid", name);
1243b5dce617SMatt Jacob 		return;
1244b5dce617SMatt Jacob 	}
1245b5dce617SMatt Jacob 	sc = gp->softc;
124663297dfdSAlexander Motin 	if (sc->sc_active_active == 1) {
12470c883cefSAlexander Motin 		empty = 1;
12480c883cefSAlexander Motin 		LIST_FOREACH(cp, &gp->consumer, consumer) {
12490c883cefSAlexander Motin 			if (cp->index & MP_BAD)
12500c883cefSAlexander Motin 				continue;
12510c883cefSAlexander Motin 			if (!empty)
12520c883cefSAlexander Motin 				sbuf_cat(sb, " ");
12530c883cefSAlexander Motin 			sbuf_cat(sb, cp->provider->name);
12540c883cefSAlexander Motin 			empty = 0;
12550c883cefSAlexander Motin 		}
12560c883cefSAlexander Motin 		if (empty)
12570c883cefSAlexander Motin 			sbuf_cat(sb, "none");
12580c883cefSAlexander Motin 		sbuf_cat(sb, "\n");
12590c883cefSAlexander Motin 	} else if (sc->sc_active && sc->sc_active->provider) {
12600c883cefSAlexander Motin 		sbuf_printf(sb, "%s\n", sc->sc_active->provider->name);
1261b5dce617SMatt Jacob 	} else {
1262b5dce617SMatt Jacob 		sbuf_printf(sb, "none\n");
1263b5dce617SMatt Jacob 	}
1264b5dce617SMatt Jacob 	sbuf_finish(sb);
1265b5dce617SMatt Jacob 	gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1266b5dce617SMatt Jacob 	sbuf_delete(sb);
1267b5dce617SMatt Jacob }
1268b5dce617SMatt Jacob 
1269b5dce617SMatt Jacob static void
1270e770bc6bSMatt Jacob g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1271e770bc6bSMatt Jacob {
1272e770bc6bSMatt Jacob 	uint32_t *version;
1273e770bc6bSMatt Jacob 	g_topology_assert();
1274e770bc6bSMatt Jacob 	version = gctl_get_paraml(req, "version", sizeof(*version));
1275e770bc6bSMatt Jacob 	if (version == NULL) {
1276e770bc6bSMatt Jacob 		gctl_error(req, "No 'version' argument");
1277e770bc6bSMatt Jacob 	} else if (*version != G_MULTIPATH_VERSION) {
1278e770bc6bSMatt Jacob 		gctl_error(req, "Userland and kernel parts are out of sync");
12792b4969ffSMatt Jacob 	} else if (strcmp(verb, "add") == 0) {
12802b4969ffSMatt Jacob 		g_multipath_ctl_add(req, mp);
12810c883cefSAlexander Motin 	} else if (strcmp(verb, "create") == 0) {
12820c883cefSAlexander Motin 		g_multipath_ctl_create(req, mp);
128363297dfdSAlexander Motin 	} else if (strcmp(verb, "configure") == 0) {
128463297dfdSAlexander Motin 		g_multipath_ctl_configure(req, mp);
12850c883cefSAlexander Motin 	} else if (strcmp(verb, "stop") == 0) {
12860c883cefSAlexander Motin 		g_multipath_ctl_stop(req, mp);
1287e770bc6bSMatt Jacob 	} else if (strcmp(verb, "destroy") == 0) {
1288e770bc6bSMatt Jacob 		g_multipath_ctl_destroy(req, mp);
12890c883cefSAlexander Motin 	} else if (strcmp(verb, "fail") == 0) {
12900c883cefSAlexander Motin 		g_multipath_ctl_fail(req, mp, 1);
12910c883cefSAlexander Motin 	} else if (strcmp(verb, "restore") == 0) {
12920c883cefSAlexander Motin 		g_multipath_ctl_fail(req, mp, 0);
12930c883cefSAlexander Motin 	} else if (strcmp(verb, "remove") == 0) {
12940c883cefSAlexander Motin 		g_multipath_ctl_remove(req, mp);
1295b5dce617SMatt Jacob 	} else if (strcmp(verb, "rotate") == 0) {
1296b5dce617SMatt Jacob 		g_multipath_ctl_rotate(req, mp);
1297b5dce617SMatt Jacob 	} else if (strcmp(verb, "getactive") == 0) {
1298b5dce617SMatt Jacob 		g_multipath_ctl_getactive(req, mp);
1299e770bc6bSMatt Jacob 	} else {
1300e770bc6bSMatt Jacob 		gctl_error(req, "Unknown verb %s", verb);
1301e770bc6bSMatt Jacob 	}
1302e770bc6bSMatt Jacob }
13030c883cefSAlexander Motin 
13040c883cefSAlexander Motin static void
13050c883cefSAlexander Motin g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
13060c883cefSAlexander Motin     struct g_consumer *cp, struct g_provider *pp)
13070c883cefSAlexander Motin {
13080c883cefSAlexander Motin 	struct g_multipath_softc *sc;
13090c883cefSAlexander Motin 	int good;
13100c883cefSAlexander Motin 
13110c883cefSAlexander Motin 	g_topology_assert();
13120c883cefSAlexander Motin 
13130c883cefSAlexander Motin 	sc = gp->softc;
13140c883cefSAlexander Motin 	if (sc == NULL)
13150c883cefSAlexander Motin 		return;
13160c883cefSAlexander Motin 	if (cp != NULL) {
13170c883cefSAlexander Motin 		sbuf_printf(sb, "%s<State>%s</State>", indent,
13180c883cefSAlexander Motin 		    (cp->index & MP_NEW) ? "NEW" :
13190c883cefSAlexander Motin 		    (cp->index & MP_LOST) ? "LOST" :
13200c883cefSAlexander Motin 		    (cp->index & MP_FAIL) ? "FAIL" :
132163297dfdSAlexander Motin 		    (sc->sc_active_active == 1 || sc->sc_active == cp) ?
132263297dfdSAlexander Motin 		     "ACTIVE" :
132363297dfdSAlexander Motin 		     sc->sc_active_active == 2 ? "READ" : "PASSIVE");
13240c883cefSAlexander Motin 	} else {
13250c883cefSAlexander Motin 		good = g_multipath_good(gp);
13260c883cefSAlexander Motin 		sbuf_printf(sb, "%s<State>%s</State>", indent,
13270c883cefSAlexander Motin 		    good == 0 ? "BROKEN" :
13280c883cefSAlexander Motin 		    (good != sc->sc_ndisks || sc->sc_ndisks == 1) ?
13290c883cefSAlexander Motin 		    "DEGRADED" : "OPTIMAL");
13300c883cefSAlexander Motin 	}
13310c883cefSAlexander Motin 	if (cp == NULL && pp == NULL) {
13320c883cefSAlexander Motin 		sbuf_printf(sb, "%s<UUID>%s</UUID>", indent, sc->sc_uuid);
13330c883cefSAlexander Motin 		sbuf_printf(sb, "%s<Mode>Active/%s</Mode>", indent,
133463297dfdSAlexander Motin 		    sc->sc_active_active == 2 ? "Read" :
133563297dfdSAlexander Motin 		    sc->sc_active_active == 1 ? "Active" : "Passive");
13360c883cefSAlexander Motin 		sbuf_printf(sb, "%s<Type>%s</Type>", indent,
13370c883cefSAlexander Motin 		    sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC");
13380c883cefSAlexander Motin 	}
13390c883cefSAlexander Motin }
13400c883cefSAlexander Motin 
1341e770bc6bSMatt Jacob DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);
1342