xref: /freebsd/sys/geom/multipath/g_multipath.c (revision 02c62349c93f29b881498630c7a0623540255764)
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 
432*02c62349SJaakko Heinonen 	gp = g_new_geomf(mp, "%s", 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 {
5938fb378d6SThomas Quinot 	struct g_consumer *lcp, *first_good_cp = NULL;
594b5dce617SMatt Jacob 	struct g_multipath_softc *sc = gp->softc;
5958fb378d6SThomas Quinot 	int active_cp_seen = 0;
596b5dce617SMatt Jacob 
597b5dce617SMatt Jacob 	g_topology_assert();
598b5dce617SMatt Jacob 	if (sc == NULL)
599b5dce617SMatt Jacob 		return (ENXIO);
600b5dce617SMatt Jacob 	LIST_FOREACH(lcp, &gp->consumer, consumer) {
601b5dce617SMatt Jacob 		if ((lcp->index & MP_BAD) == 0) {
6028fb378d6SThomas Quinot 			if (first_good_cp == NULL)
6038fb378d6SThomas Quinot 				first_good_cp = lcp;
6048fb378d6SThomas Quinot 			if (active_cp_seen)
605b5dce617SMatt Jacob 				break;
606b5dce617SMatt Jacob 		}
6078fb378d6SThomas Quinot 		if (sc->sc_active == lcp)
6088fb378d6SThomas Quinot 			active_cp_seen = 1;
609b5dce617SMatt Jacob 	}
6108fb378d6SThomas Quinot 	if (lcp == NULL)
6118fb378d6SThomas Quinot 		lcp = first_good_cp;
6128fb378d6SThomas Quinot 	if (lcp && lcp != sc->sc_active) {
6130c883cefSAlexander Motin 		sc->sc_active = lcp;
61463297dfdSAlexander Motin 		if (sc->sc_active_active != 1)
6150c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
616b5dce617SMatt Jacob 			    lcp->provider->name, sc->sc_name);
617b5dce617SMatt Jacob 	}
618b5dce617SMatt Jacob 	return (0);
619b5dce617SMatt Jacob }
620b5dce617SMatt Jacob 
621e770bc6bSMatt Jacob static void
622e770bc6bSMatt Jacob g_multipath_init(struct g_class *mp)
623e770bc6bSMatt Jacob {
624e770bc6bSMatt Jacob 	bioq_init(&gmtbq);
625e770bc6bSMatt Jacob 	mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
62663297dfdSAlexander Motin 	kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt");
627e770bc6bSMatt Jacob }
628e770bc6bSMatt Jacob 
629e770bc6bSMatt Jacob static void
630e770bc6bSMatt Jacob g_multipath_fini(struct g_class *mp)
631e770bc6bSMatt Jacob {
632e770bc6bSMatt Jacob 	if (g_multipath_kt_state == GKT_RUN) {
633e770bc6bSMatt Jacob 		mtx_lock(&gmtbq_mtx);
634e770bc6bSMatt Jacob 		g_multipath_kt_state = GKT_DIE;
635e770bc6bSMatt Jacob 		wakeup(&g_multipath_kt_state);
636e770bc6bSMatt Jacob 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
637e770bc6bSMatt Jacob 		    "gmp:fini", 0);
638e770bc6bSMatt Jacob 		mtx_unlock(&gmtbq_mtx);
639e770bc6bSMatt Jacob 	}
640e770bc6bSMatt Jacob }
641e770bc6bSMatt Jacob 
642e770bc6bSMatt Jacob static int
643e770bc6bSMatt Jacob g_multipath_read_metadata(struct g_consumer *cp,
644e770bc6bSMatt Jacob     struct g_multipath_metadata *md)
645e770bc6bSMatt Jacob {
646e770bc6bSMatt Jacob 	struct g_provider *pp;
647e770bc6bSMatt Jacob 	u_char *buf;
648e770bc6bSMatt Jacob 	int error;
649e770bc6bSMatt Jacob 
650e770bc6bSMatt Jacob 	g_topology_assert();
651e770bc6bSMatt Jacob 	error = g_access(cp, 1, 0, 0);
65212f35a61SPawel Jakub Dawidek 	if (error != 0)
653e770bc6bSMatt Jacob 		return (error);
654e770bc6bSMatt Jacob 	pp = cp->provider;
655e770bc6bSMatt Jacob 	g_topology_unlock();
656e770bc6bSMatt Jacob 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
657e770bc6bSMatt Jacob 	    pp->sectorsize, &error);
658e770bc6bSMatt Jacob 	g_topology_lock();
659e770bc6bSMatt Jacob 	g_access(cp, -1, 0, 0);
66012f35a61SPawel Jakub Dawidek 	if (buf == NULL)
661e770bc6bSMatt Jacob 		return (error);
662e770bc6bSMatt Jacob 	multipath_metadata_decode(buf, md);
663e770bc6bSMatt Jacob 	g_free(buf);
664e770bc6bSMatt Jacob 	return (0);
665e770bc6bSMatt Jacob }
666e770bc6bSMatt Jacob 
667e770bc6bSMatt Jacob static struct g_geom *
668e770bc6bSMatt Jacob g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
669e770bc6bSMatt Jacob {
670e770bc6bSMatt Jacob 	struct g_multipath_metadata md;
671e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
672e770bc6bSMatt Jacob 	struct g_consumer *cp;
673e770bc6bSMatt Jacob 	struct g_geom *gp, *gp1;
674e770bc6bSMatt Jacob 	int error, isnew;
675e770bc6bSMatt Jacob 
676e770bc6bSMatt Jacob 	g_topology_assert();
677e770bc6bSMatt Jacob 
678e770bc6bSMatt Jacob 	gp = g_new_geomf(mp, "multipath:taste");
679e770bc6bSMatt Jacob 	gp->start = g_multipath_start;
680e770bc6bSMatt Jacob 	gp->access = g_multipath_access;
681e770bc6bSMatt Jacob 	gp->orphan = g_multipath_orphan;
682e770bc6bSMatt Jacob 	cp = g_new_consumer(gp);
683e770bc6bSMatt Jacob 	g_attach(cp, pp);
684e770bc6bSMatt Jacob 	error = g_multipath_read_metadata(cp, &md);
685e770bc6bSMatt Jacob 	g_detach(cp);
686e770bc6bSMatt Jacob 	g_destroy_consumer(cp);
687e770bc6bSMatt Jacob 	g_destroy_geom(gp);
68812f35a61SPawel Jakub Dawidek 	if (error != 0)
689e770bc6bSMatt Jacob 		return (NULL);
690e770bc6bSMatt Jacob 	gp = NULL;
691e770bc6bSMatt Jacob 
692e770bc6bSMatt Jacob 	if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
69312f35a61SPawel Jakub Dawidek 		if (g_multipath_debug)
694e770bc6bSMatt Jacob 			printf("%s is not MULTIPATH\n", pp->name);
695e770bc6bSMatt Jacob 		return (NULL);
696e770bc6bSMatt Jacob 	}
697e770bc6bSMatt Jacob 	if (md.md_version != G_MULTIPATH_VERSION) {
698e770bc6bSMatt Jacob 		printf("%s has version %d multipath id- this module is version "
699e770bc6bSMatt Jacob 		    " %d: rejecting\n", pp->name, md.md_version,
700e770bc6bSMatt Jacob 		    G_MULTIPATH_VERSION);
701e770bc6bSMatt Jacob 		return (NULL);
702e770bc6bSMatt Jacob 	}
7030c883cefSAlexander Motin 	if (md.md_size != 0 && md.md_size != pp->mediasize)
7040c883cefSAlexander Motin 		return (NULL);
7050c883cefSAlexander Motin 	if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize)
7060c883cefSAlexander Motin 		return (NULL);
70712f35a61SPawel Jakub Dawidek 	if (g_multipath_debug)
708e770bc6bSMatt Jacob 		printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
709e770bc6bSMatt Jacob 
710e770bc6bSMatt Jacob 	/*
711e770bc6bSMatt Jacob 	 * Let's check if such a device already is present. We check against
712e770bc6bSMatt Jacob 	 * uuid alone first because that's the true distinguishor. If that
713e770bc6bSMatt Jacob 	 * passes, then we check for name conflicts. If there are conflicts,
714e770bc6bSMatt Jacob 	 * modify the name.
715e770bc6bSMatt Jacob 	 *
716e770bc6bSMatt Jacob 	 * The whole purpose of this is to solve the problem that people don't
717e770bc6bSMatt Jacob 	 * pick good unique names, but good unique names (like uuids) are a
718e770bc6bSMatt Jacob 	 * pain to use. So, we allow people to build GEOMs with friendly names
719e770bc6bSMatt Jacob 	 * and uuids, and modify the names in case there's a collision.
720e770bc6bSMatt Jacob 	 */
721e770bc6bSMatt Jacob 	sc = NULL;
722e770bc6bSMatt Jacob 	LIST_FOREACH(gp, &mp->geom, geom) {
723e770bc6bSMatt Jacob 		sc = gp->softc;
7240c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
725e770bc6bSMatt Jacob 			continue;
72612f35a61SPawel Jakub Dawidek 		if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
727e770bc6bSMatt Jacob 			break;
728e770bc6bSMatt Jacob 	}
729e770bc6bSMatt Jacob 
730e770bc6bSMatt Jacob 	LIST_FOREACH(gp1, &mp->geom, geom) {
73112f35a61SPawel Jakub Dawidek 		if (gp1 == gp)
732e770bc6bSMatt Jacob 			continue;
733e770bc6bSMatt Jacob 		sc = gp1->softc;
7340c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
735e770bc6bSMatt Jacob 			continue;
73612f35a61SPawel Jakub Dawidek 		if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
737e770bc6bSMatt Jacob 			break;
738e770bc6bSMatt Jacob 	}
739e770bc6bSMatt Jacob 
740e770bc6bSMatt Jacob 	/*
741e770bc6bSMatt Jacob 	 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
742e770bc6bSMatt Jacob 	 *
743e770bc6bSMatt Jacob 	 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
744e770bc6bSMatt Jacob 	 * with the same name (but a different UUID).
745e770bc6bSMatt Jacob 	 *
746e770bc6bSMatt Jacob 	 * If gp is NULL, then modify the name with a random number and
747e770bc6bSMatt Jacob   	 * complain, but allow the creation of the geom to continue.
748e770bc6bSMatt Jacob 	 *
749e770bc6bSMatt Jacob 	 * If gp is *not* NULL, just use the geom's name as we're attaching
750e770bc6bSMatt Jacob 	 * this disk to the (previously generated) name.
751e770bc6bSMatt Jacob 	 */
752e770bc6bSMatt Jacob 
753e770bc6bSMatt Jacob 	if (gp1) {
754e770bc6bSMatt Jacob 		sc = gp1->softc;
755e770bc6bSMatt Jacob 		if (gp == NULL) {
756e770bc6bSMatt Jacob 			char buf[16];
757e770bc6bSMatt Jacob 			u_long rand = random();
758e770bc6bSMatt Jacob 
759e770bc6bSMatt Jacob 			snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
760e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
761e770bc6bSMatt Jacob 			    sc->sc_name, sc->sc_uuid);
762e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
763e770bc6bSMatt Jacob 			    md.md_uuid, buf);
764e770bc6bSMatt Jacob 			strlcpy(md.md_name, buf, sizeof(md.md_name));
765e770bc6bSMatt Jacob 		} else {
766e770bc6bSMatt Jacob 			strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
767e770bc6bSMatt Jacob 		}
768e770bc6bSMatt Jacob 	}
769e770bc6bSMatt Jacob 
770e770bc6bSMatt Jacob 	if (gp == NULL) {
771e770bc6bSMatt Jacob 		gp = g_multipath_create(mp, &md);
772e770bc6bSMatt Jacob 		if (gp == NULL) {
773e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
774e770bc6bSMatt Jacob 			    md.md_name, md.md_uuid);
775e770bc6bSMatt Jacob 			return (NULL);
776e770bc6bSMatt Jacob 		}
777e770bc6bSMatt Jacob 		isnew = 1;
778e770bc6bSMatt Jacob 	} else {
779e770bc6bSMatt Jacob 		isnew = 0;
780e770bc6bSMatt Jacob 	}
781e770bc6bSMatt Jacob 
782e770bc6bSMatt Jacob 	sc = gp->softc;
783e770bc6bSMatt Jacob 	KASSERT(sc != NULL, ("sc is NULL"));
784e770bc6bSMatt Jacob 	error = g_multipath_add_disk(gp, pp);
785e770bc6bSMatt Jacob 	if (error != 0) {
78612f35a61SPawel Jakub Dawidek 		if (isnew)
787e770bc6bSMatt Jacob 			g_multipath_destroy(gp);
788e770bc6bSMatt Jacob 		return (NULL);
789e770bc6bSMatt Jacob 	}
790e770bc6bSMatt Jacob 	return (gp);
791e770bc6bSMatt Jacob }
792e770bc6bSMatt Jacob 
793e770bc6bSMatt Jacob static void
7940c883cefSAlexander Motin g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp,
7950c883cefSAlexander Motin     const char *name)
796e770bc6bSMatt Jacob {
7970c883cefSAlexander Motin 	struct g_multipath_softc *sc;
798e770bc6bSMatt Jacob 	struct g_geom *gp;
7992b4969ffSMatt Jacob 	struct g_consumer *cp;
8000c883cefSAlexander Motin 	struct g_provider *pp;
8010c883cefSAlexander Motin 	const char *mpname;
802e770bc6bSMatt Jacob 	static const char devpf[6] = "/dev/";
803e770bc6bSMatt Jacob 
804e770bc6bSMatt Jacob 	g_topology_assert();
805e770bc6bSMatt Jacob 
806e770bc6bSMatt Jacob 	mpname = gctl_get_asciiparam(req, "arg0");
807e770bc6bSMatt Jacob         if (mpname == NULL) {
808e770bc6bSMatt Jacob                 gctl_error(req, "No 'arg0' argument");
809e770bc6bSMatt Jacob                 return;
810e770bc6bSMatt Jacob         }
8112b4969ffSMatt Jacob 	gp = g_multipath_find_geom(mp, mpname);
8122b4969ffSMatt Jacob 	if (gp == NULL) {
8132b4969ffSMatt Jacob 		gctl_error(req, "Device %s is invalid", mpname);
814e770bc6bSMatt Jacob 		return;
815e770bc6bSMatt Jacob 	}
8160c883cefSAlexander Motin 	sc = gp->softc;
817e770bc6bSMatt Jacob 
81812f35a61SPawel Jakub Dawidek 	if (strncmp(name, devpf, 5) == 0)
819e770bc6bSMatt Jacob 		name += 5;
8202b4969ffSMatt Jacob 	pp = g_provider_by_name(name);
8212b4969ffSMatt Jacob 	if (pp == NULL) {
822e770bc6bSMatt Jacob 		gctl_error(req, "Provider %s is invalid", name);
823e770bc6bSMatt Jacob 		return;
824e770bc6bSMatt Jacob 	}
825e770bc6bSMatt Jacob 
826e770bc6bSMatt Jacob 	/*
8270c883cefSAlexander Motin 	 * Check to make sure parameters match.
828e770bc6bSMatt Jacob 	 */
8290c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
8300c883cefSAlexander Motin 		if (cp->provider == pp) {
8310c883cefSAlexander Motin 			gctl_error(req, "provider %s is already there",
8320c883cefSAlexander Motin 			    pp->name);
833e770bc6bSMatt Jacob 			return;
834e770bc6bSMatt Jacob 		}
8350c883cefSAlexander Motin 	}
8360c883cefSAlexander Motin 	if (sc->sc_pp != NULL && sc->sc_pp->mediasize != 0 &&
8370c883cefSAlexander Motin 	    sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0)
8380c883cefSAlexander Motin 	     != pp->mediasize) {
8390c883cefSAlexander Motin 		gctl_error(req, "Providers size mismatch %jd != %jd",
8400c883cefSAlexander Motin 		    (intmax_t) sc->sc_pp->mediasize +
8410c883cefSAlexander Motin 			(sc->sc_uuid[0] != 0 ? pp->sectorsize : 0),
8420c883cefSAlexander Motin 		    (intmax_t) pp->mediasize);
843e770bc6bSMatt Jacob 		return;
844e770bc6bSMatt Jacob 	}
8450c883cefSAlexander Motin 	if (sc->sc_pp != NULL && sc->sc_pp->sectorsize != 0 &&
8460c883cefSAlexander Motin 	    sc->sc_pp->sectorsize != pp->sectorsize) {
8470c883cefSAlexander Motin 		gctl_error(req, "Providers sectorsize mismatch %u != %u",
8480c883cefSAlexander Motin 		    sc->sc_pp->sectorsize, pp->sectorsize);
849e770bc6bSMatt Jacob 		return;
850e770bc6bSMatt Jacob 	}
851e770bc6bSMatt Jacob 
852e770bc6bSMatt Jacob 	/*
8532b4969ffSMatt Jacob 	 * Now add....
854e770bc6bSMatt Jacob 	 */
8552b4969ffSMatt Jacob 	(void) g_multipath_add_disk(gp, pp);
856e770bc6bSMatt Jacob }
857e770bc6bSMatt Jacob 
8580c883cefSAlexander Motin static void
85971ee4ef0SThomas Quinot g_multipath_ctl_prefer(struct gctl_req *req, struct g_class *mp)
86071ee4ef0SThomas Quinot {
86171ee4ef0SThomas Quinot 	struct g_geom *gp;
86271ee4ef0SThomas Quinot 	struct g_multipath_softc *sc;
86371ee4ef0SThomas Quinot 	struct g_consumer *cp;
86471ee4ef0SThomas Quinot 	const char *name, *mpname;
86571ee4ef0SThomas Quinot 	static const char devpf[6] = "/dev/";
86671ee4ef0SThomas Quinot 	int *nargs;
86771ee4ef0SThomas Quinot 
86871ee4ef0SThomas Quinot 	g_topology_assert();
86971ee4ef0SThomas Quinot 
87071ee4ef0SThomas Quinot 	mpname = gctl_get_asciiparam(req, "arg0");
87171ee4ef0SThomas Quinot         if (mpname == NULL) {
87271ee4ef0SThomas Quinot                 gctl_error(req, "No 'arg0' argument");
87371ee4ef0SThomas Quinot                 return;
87471ee4ef0SThomas Quinot         }
87571ee4ef0SThomas Quinot 	gp = g_multipath_find_geom(mp, mpname);
87671ee4ef0SThomas Quinot 	if (gp == NULL) {
87771ee4ef0SThomas Quinot 		gctl_error(req, "Device %s is invalid", mpname);
87871ee4ef0SThomas Quinot 		return;
87971ee4ef0SThomas Quinot 	}
88071ee4ef0SThomas Quinot 	sc = gp->softc;
88171ee4ef0SThomas Quinot 
88271ee4ef0SThomas Quinot 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
88371ee4ef0SThomas Quinot 	if (nargs == NULL) {
88471ee4ef0SThomas Quinot 		gctl_error(req, "No 'nargs' argument");
88571ee4ef0SThomas Quinot 		return;
88671ee4ef0SThomas Quinot 	}
88771ee4ef0SThomas Quinot 	if (*nargs != 2) {
88871ee4ef0SThomas Quinot 		gctl_error(req, "missing device");
88971ee4ef0SThomas Quinot 		return;
89071ee4ef0SThomas Quinot 	}
89171ee4ef0SThomas Quinot 
89271ee4ef0SThomas Quinot 	name = gctl_get_asciiparam(req, "arg1");
89371ee4ef0SThomas Quinot 	if (name == NULL) {
89471ee4ef0SThomas Quinot 		gctl_error(req, "No 'arg1' argument");
89571ee4ef0SThomas Quinot 		return;
89671ee4ef0SThomas Quinot 	}
89771ee4ef0SThomas Quinot 	if (strncmp(name, devpf, 5) == 0) {
89871ee4ef0SThomas Quinot 		name += 5;
89971ee4ef0SThomas Quinot 	}
90071ee4ef0SThomas Quinot 
90171ee4ef0SThomas Quinot 	LIST_FOREACH(cp, &gp->consumer, consumer) {
90271ee4ef0SThomas Quinot 		if (cp->provider != NULL
90371ee4ef0SThomas Quinot                       && strcmp(cp->provider->name, name) == 0)
90471ee4ef0SThomas Quinot 		    break;
90571ee4ef0SThomas Quinot 	}
90671ee4ef0SThomas Quinot 
90771ee4ef0SThomas Quinot 	if (cp == NULL) {
90871ee4ef0SThomas Quinot 		gctl_error(req, "Provider %s not found", name);
90971ee4ef0SThomas Quinot 		return;
91071ee4ef0SThomas Quinot 	}
91171ee4ef0SThomas Quinot 
91271ee4ef0SThomas Quinot 	mtx_lock(&sc->sc_mtx);
91371ee4ef0SThomas Quinot 
91471ee4ef0SThomas Quinot 	if (cp->index & MP_BAD) {
91571ee4ef0SThomas Quinot 		gctl_error(req, "Consumer %s is invalid", name);
91671ee4ef0SThomas Quinot 		mtx_unlock(&sc->sc_mtx);
91771ee4ef0SThomas Quinot 		return;
91871ee4ef0SThomas Quinot 	}
91971ee4ef0SThomas Quinot 
92071ee4ef0SThomas Quinot 	/* Here when the consumer is present and in good shape */
92171ee4ef0SThomas Quinot 
92271ee4ef0SThomas Quinot 	sc->sc_active = cp;
92371ee4ef0SThomas Quinot 	if (!sc->sc_active_active)
92471ee4ef0SThomas Quinot 	    printf("GEOM_MULTIPATH: %s now active path in %s\n",
92571ee4ef0SThomas Quinot 		sc->sc_active->provider->name, sc->sc_name);
92671ee4ef0SThomas Quinot 
92771ee4ef0SThomas Quinot 	mtx_unlock(&sc->sc_mtx);
92871ee4ef0SThomas Quinot }
92971ee4ef0SThomas Quinot 
93071ee4ef0SThomas Quinot static void
9310c883cefSAlexander Motin g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp)
9320c883cefSAlexander Motin {
9330c883cefSAlexander Motin 	struct g_multipath_softc *sc;
9340c883cefSAlexander Motin 	struct g_geom *gp;
9350c883cefSAlexander Motin 	const char *mpname, *name;
9360c883cefSAlexander Motin 
9370c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
9380c883cefSAlexander Motin         if (mpname == NULL) {
9390c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
9400c883cefSAlexander Motin                 return;
9410c883cefSAlexander Motin         }
9420c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
9430c883cefSAlexander Motin 	if (gp == NULL) {
9440c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
9450c883cefSAlexander Motin 		return;
9460c883cefSAlexander Motin 	}
9470c883cefSAlexander Motin 	sc = gp->softc;
9480c883cefSAlexander Motin 
9490c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
9500c883cefSAlexander Motin 	if (name == NULL) {
9510c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
9520c883cefSAlexander Motin 		return;
9530c883cefSAlexander Motin 	}
9540c883cefSAlexander Motin 	g_multipath_ctl_add_name(req, mp, name);
9550c883cefSAlexander Motin }
9560c883cefSAlexander Motin 
9570c883cefSAlexander Motin static void
9580c883cefSAlexander Motin g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp)
9590c883cefSAlexander Motin {
9600c883cefSAlexander Motin 	struct g_multipath_metadata md;
9610c883cefSAlexander Motin 	struct g_multipath_softc *sc;
9620c883cefSAlexander Motin 	struct g_geom *gp;
9630c883cefSAlexander Motin 	const char *mpname, *name;
9640c883cefSAlexander Motin 	char param[16];
96563297dfdSAlexander Motin 	int *nargs, i, *val;
9660c883cefSAlexander Motin 
9670c883cefSAlexander Motin 	g_topology_assert();
9680c883cefSAlexander Motin 
9690c883cefSAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
9700c883cefSAlexander Motin 	if (*nargs < 2) {
9710c883cefSAlexander Motin 		gctl_error(req, "wrong number of arguments.");
9720c883cefSAlexander Motin 		return;
9730c883cefSAlexander Motin 	}
9740c883cefSAlexander Motin 
9750c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
9760c883cefSAlexander Motin         if (mpname == NULL) {
9770c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
9780c883cefSAlexander Motin                 return;
9790c883cefSAlexander Motin         }
9800c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
9810c883cefSAlexander Motin 	if (gp != NULL) {
9820c883cefSAlexander Motin 		gctl_error(req, "Device %s already exist", mpname);
9830c883cefSAlexander Motin 		return;
9840c883cefSAlexander Motin 	}
9850c883cefSAlexander Motin 	sc = gp->softc;
9860c883cefSAlexander Motin 
9870c883cefSAlexander Motin 	memset(&md, 0, sizeof(md));
9880c883cefSAlexander Motin 	strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
9890c883cefSAlexander Motin 	md.md_version = G_MULTIPATH_VERSION;
9900c883cefSAlexander Motin 	strlcpy(md.md_name, mpname, sizeof(md.md_name));
9910c883cefSAlexander Motin 	md.md_size = 0;
9920c883cefSAlexander Motin 	md.md_sectorsize = 0;
9930c883cefSAlexander Motin 	md.md_uuid[0] = 0;
99463297dfdSAlexander Motin 	md.md_active_active = 0;
99563297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_active", sizeof(*val));
99663297dfdSAlexander Motin 	if (val != NULL && *val != 0)
99763297dfdSAlexander Motin 		md.md_active_active = 1;
99863297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_read", sizeof(*val));
99963297dfdSAlexander Motin 	if (val != NULL && *val != 0)
100063297dfdSAlexander Motin 		md.md_active_active = 2;
10010c883cefSAlexander Motin 	gp = g_multipath_create(mp, &md);
10020c883cefSAlexander Motin 	if (gp == NULL) {
10030c883cefSAlexander Motin 		gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n",
10040c883cefSAlexander Motin 		    md.md_name, md.md_uuid);
10050c883cefSAlexander Motin 		return;
10060c883cefSAlexander Motin 	}
10070c883cefSAlexander Motin 	sc = gp->softc;
10080c883cefSAlexander Motin 
10090c883cefSAlexander Motin 	for (i = 1; i < *nargs; i++) {
10100c883cefSAlexander Motin 		snprintf(param, sizeof(param), "arg%d", i);
10110c883cefSAlexander Motin 		name = gctl_get_asciiparam(req, param);
10120c883cefSAlexander Motin 		g_multipath_ctl_add_name(req, mp, name);
10130c883cefSAlexander Motin 	}
10140c883cefSAlexander Motin 
10150c883cefSAlexander Motin 	if (sc->sc_ndisks != (*nargs - 1))
10160c883cefSAlexander Motin 		g_multipath_destroy(gp);
10170c883cefSAlexander Motin }
10180c883cefSAlexander Motin 
10190c883cefSAlexander Motin static void
102063297dfdSAlexander Motin g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp)
102163297dfdSAlexander Motin {
102263297dfdSAlexander Motin 	struct g_multipath_softc *sc;
102363297dfdSAlexander Motin 	struct g_geom *gp;
102463297dfdSAlexander Motin 	struct g_consumer *cp;
102563297dfdSAlexander Motin 	struct g_provider *pp;
1026c0b1ef66SAlexander Motin 	struct g_multipath_metadata md;
102763297dfdSAlexander Motin 	const char *name;
102863297dfdSAlexander Motin 	int error, *val;
102963297dfdSAlexander Motin 	void *buf;
103063297dfdSAlexander Motin 
103163297dfdSAlexander Motin 	g_topology_assert();
103263297dfdSAlexander Motin 
103363297dfdSAlexander Motin 	name = gctl_get_asciiparam(req, "arg0");
103463297dfdSAlexander Motin 	if (name == NULL) {
103563297dfdSAlexander Motin 		gctl_error(req, "No 'arg0' argument");
103663297dfdSAlexander Motin 		return;
103763297dfdSAlexander Motin 	}
103863297dfdSAlexander Motin 	gp = g_multipath_find_geom(mp, name);
103963297dfdSAlexander Motin 	if (gp == NULL) {
104063297dfdSAlexander Motin 		gctl_error(req, "Device %s is invalid", name);
104163297dfdSAlexander Motin 		return;
104263297dfdSAlexander Motin 	}
104363297dfdSAlexander Motin 	sc = gp->softc;
104463297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_active", sizeof(*val));
104563297dfdSAlexander Motin 	if (val != NULL && *val != 0)
104663297dfdSAlexander Motin 		sc->sc_active_active = 1;
104763297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_read", sizeof(*val));
104863297dfdSAlexander Motin 	if (val != NULL && *val != 0)
104963297dfdSAlexander Motin 		sc->sc_active_active = 2;
105063297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_passive", sizeof(*val));
105163297dfdSAlexander Motin 	if (val != NULL && *val != 0)
105263297dfdSAlexander Motin 		sc->sc_active_active = 0;
105363297dfdSAlexander Motin 	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
105463297dfdSAlexander Motin 		cp = sc->sc_active;
105563297dfdSAlexander Motin 		pp = cp->provider;
105663297dfdSAlexander Motin 		error = g_access(cp, 1, 1, 1);
105763297dfdSAlexander Motin 		if (error != 0) {
105863297dfdSAlexander Motin 			gctl_error(req, "Can't open %s (%d)", pp->name, error);
105963297dfdSAlexander Motin 			return;
106063297dfdSAlexander Motin 		}
106163297dfdSAlexander Motin 		g_topology_unlock();
1062c0b1ef66SAlexander Motin 		buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1063c0b1ef66SAlexander Motin 		strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1064c0b1ef66SAlexander Motin 		memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
1065c0b1ef66SAlexander Motin 		strlcpy(md.md_name, name, sizeof(md.md_name));
1066c0b1ef66SAlexander Motin 		md.md_version = G_MULTIPATH_VERSION;
1067c0b1ef66SAlexander Motin 		md.md_size = pp->mediasize;
1068c0b1ef66SAlexander Motin 		md.md_sectorsize = pp->sectorsize;
1069c0b1ef66SAlexander Motin 		md.md_active_active = sc->sc_active_active;
1070c0b1ef66SAlexander Motin 		multipath_metadata_encode(&md, buf);
107163297dfdSAlexander Motin 		error = g_write_data(cp, pp->mediasize - pp->sectorsize,
107263297dfdSAlexander Motin 		    buf, pp->sectorsize);
107363297dfdSAlexander Motin 		g_topology_lock();
107463297dfdSAlexander Motin 		g_access(cp, -1, -1, -1);
107563297dfdSAlexander Motin 		if (error != 0)
107663297dfdSAlexander Motin 			gctl_error(req, "Can't update metadata on %s (%d)",
107763297dfdSAlexander Motin 			    pp->name, error);
107863297dfdSAlexander Motin 	}
107963297dfdSAlexander Motin }
108063297dfdSAlexander Motin 
108163297dfdSAlexander Motin static void
10820c883cefSAlexander Motin g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail)
10830c883cefSAlexander Motin {
10840c883cefSAlexander Motin 	struct g_multipath_softc *sc;
10850c883cefSAlexander Motin 	struct g_geom *gp;
10860c883cefSAlexander Motin 	struct g_consumer *cp;
10870c883cefSAlexander Motin 	const char *mpname, *name;
10880c883cefSAlexander Motin 	int found;
10890c883cefSAlexander Motin 
10900c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
10910c883cefSAlexander Motin         if (mpname == NULL) {
10920c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
10930c883cefSAlexander Motin                 return;
10940c883cefSAlexander Motin         }
10950c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
10960c883cefSAlexander Motin 	if (gp == NULL) {
10970c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
10980c883cefSAlexander Motin 		return;
10990c883cefSAlexander Motin 	}
11000c883cefSAlexander Motin 	sc = gp->softc;
11010c883cefSAlexander Motin 
11020c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
11030c883cefSAlexander Motin 	if (name == NULL) {
11040c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
11050c883cefSAlexander Motin 		return;
11060c883cefSAlexander Motin 	}
11070c883cefSAlexander Motin 
11080c883cefSAlexander Motin 	found = 0;
11090c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
11100c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
11110c883cefSAlexander Motin 		if (cp->provider != NULL &&
11120c883cefSAlexander Motin 		    strcmp(cp->provider->name, name) == 0 &&
11130c883cefSAlexander Motin 		    (cp->index & MP_LOST) == 0) {
11140c883cefSAlexander Motin 			found = 1;
111563297dfdSAlexander Motin 			if (!fail == !(cp->index & MP_FAIL))
111663297dfdSAlexander Motin 				continue;
11170c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: %s in %s is marked %s.\n",
11180c883cefSAlexander Motin 				name, sc->sc_name, fail ? "FAIL" : "OK");
11190c883cefSAlexander Motin 			if (fail) {
11200c883cefSAlexander Motin 				g_multipath_fault(cp, MP_FAIL);
11210c883cefSAlexander Motin 			} else {
11220c883cefSAlexander Motin 				cp->index &= ~MP_FAIL;
11230c883cefSAlexander Motin 			}
11240c883cefSAlexander Motin 		}
11250c883cefSAlexander Motin 	}
11260c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
11270c883cefSAlexander Motin 	if (found == 0)
11280c883cefSAlexander Motin 		gctl_error(req, "Provider %s not found", name);
11290c883cefSAlexander Motin }
11300c883cefSAlexander Motin 
11310c883cefSAlexander Motin static void
11320c883cefSAlexander Motin g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp)
11330c883cefSAlexander Motin {
11340c883cefSAlexander Motin 	struct g_multipath_softc *sc;
11350c883cefSAlexander Motin 	struct g_geom *gp;
11360c883cefSAlexander Motin 	struct g_consumer *cp, *cp1;
11370c883cefSAlexander Motin 	const char *mpname, *name;
11380c883cefSAlexander Motin 	uintptr_t *cnt;
11390c883cefSAlexander Motin 	int found;
11400c883cefSAlexander Motin 
11410c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
11420c883cefSAlexander Motin         if (mpname == NULL) {
11430c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
11440c883cefSAlexander Motin                 return;
11450c883cefSAlexander Motin         }
11460c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
11470c883cefSAlexander Motin 	if (gp == NULL) {
11480c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
11490c883cefSAlexander Motin 		return;
11500c883cefSAlexander Motin 	}
11510c883cefSAlexander Motin 	sc = gp->softc;
11520c883cefSAlexander Motin 
11530c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
11540c883cefSAlexander Motin 	if (name == NULL) {
11550c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
11560c883cefSAlexander Motin 		return;
11570c883cefSAlexander Motin 	}
11580c883cefSAlexander Motin 
11590c883cefSAlexander Motin 	found = 0;
11600c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
11610c883cefSAlexander Motin 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
11620c883cefSAlexander Motin 		if (cp->provider != NULL &&
11630c883cefSAlexander Motin 		    strcmp(cp->provider->name, name) == 0 &&
11640c883cefSAlexander Motin 		    (cp->index & MP_LOST) == 0) {
11650c883cefSAlexander Motin 			found = 1;
11660c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: removing %s from %s\n",
11670c883cefSAlexander Motin 			    cp->provider->name, cp->geom->name);
11680c883cefSAlexander Motin 			sc->sc_ndisks--;
11690c883cefSAlexander Motin 			g_multipath_fault(cp, MP_LOST);
11700c883cefSAlexander Motin 			cnt = (uintptr_t *)&cp->private;
11710c883cefSAlexander Motin 			if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
11720c883cefSAlexander Motin 				cp->index |= MP_POSTED;
11730c883cefSAlexander Motin 				mtx_unlock(&sc->sc_mtx);
11740c883cefSAlexander Motin 				g_mpd(cp, 0);
11750c883cefSAlexander Motin 				if (cp1 == NULL)
11760c883cefSAlexander Motin 					return;	/* Recursion happened. */
11770c883cefSAlexander Motin 				mtx_lock(&sc->sc_mtx);
11780c883cefSAlexander Motin 			}
11790c883cefSAlexander Motin 		}
11800c883cefSAlexander Motin 	}
11810c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
11820c883cefSAlexander Motin 	if (found == 0)
11830c883cefSAlexander Motin 		gctl_error(req, "Provider %s not found", name);
11840c883cefSAlexander Motin }
11850c883cefSAlexander Motin 
1186e770bc6bSMatt Jacob static struct g_geom *
1187e770bc6bSMatt Jacob g_multipath_find_geom(struct g_class *mp, const char *name)
1188e770bc6bSMatt Jacob {
1189e770bc6bSMatt Jacob 	struct g_geom *gp;
11900c883cefSAlexander Motin 	struct g_multipath_softc *sc;
1191e770bc6bSMatt Jacob 
1192e770bc6bSMatt Jacob 	LIST_FOREACH(gp, &mp->geom, geom) {
11930c883cefSAlexander Motin 		sc = gp->softc;
11940c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
11950c883cefSAlexander Motin 			continue;
11960c883cefSAlexander Motin 		if (strcmp(gp->name, name) == 0)
1197e770bc6bSMatt Jacob 			return (gp);
1198e770bc6bSMatt Jacob 	}
1199e770bc6bSMatt Jacob 	return (NULL);
1200e770bc6bSMatt Jacob }
1201e770bc6bSMatt Jacob 
1202e770bc6bSMatt Jacob static void
12030c883cefSAlexander Motin g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp)
1204e770bc6bSMatt Jacob {
1205e770bc6bSMatt Jacob 	struct g_geom *gp;
1206e770bc6bSMatt Jacob 	const char *name;
1207e770bc6bSMatt Jacob 	int error;
1208e770bc6bSMatt Jacob 
1209e770bc6bSMatt Jacob 	g_topology_assert();
1210e770bc6bSMatt Jacob 
1211e770bc6bSMatt Jacob 	name = gctl_get_asciiparam(req, "arg0");
1212e770bc6bSMatt Jacob         if (name == NULL) {
1213e770bc6bSMatt Jacob                 gctl_error(req, "No 'arg0' argument");
1214e770bc6bSMatt Jacob                 return;
1215e770bc6bSMatt Jacob         }
1216e770bc6bSMatt Jacob 	gp = g_multipath_find_geom(mp, name);
1217e770bc6bSMatt Jacob 	if (gp == NULL) {
1218e770bc6bSMatt Jacob 		gctl_error(req, "Device %s is invalid", name);
1219e770bc6bSMatt Jacob 		return;
1220e770bc6bSMatt Jacob 	}
1221e770bc6bSMatt Jacob 	error = g_multipath_destroy(gp);
12220c883cefSAlexander Motin 	if (error != 0 && error != EINPROGRESS)
12230c883cefSAlexander Motin 		gctl_error(req, "failed to stop %s (err=%d)", name, error);
1224e770bc6bSMatt Jacob }
12250c883cefSAlexander Motin 
12260c883cefSAlexander Motin static void
12270c883cefSAlexander Motin g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
12280c883cefSAlexander Motin {
12290c883cefSAlexander Motin 	struct g_geom *gp;
12300c883cefSAlexander Motin 	struct g_multipath_softc *sc;
12310c883cefSAlexander Motin 	struct g_consumer *cp;
12320c883cefSAlexander Motin 	struct g_provider *pp;
12330c883cefSAlexander Motin 	const char *name;
12340c883cefSAlexander Motin 	uint8_t *buf;
12350c883cefSAlexander Motin 	int error;
12360c883cefSAlexander Motin 
12370c883cefSAlexander Motin 	g_topology_assert();
12380c883cefSAlexander Motin 
12390c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg0");
12400c883cefSAlexander Motin         if (name == NULL) {
12410c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
12420c883cefSAlexander Motin                 return;
12430c883cefSAlexander Motin         }
12440c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, name);
12450c883cefSAlexander Motin 	if (gp == NULL) {
12460c883cefSAlexander Motin 		gctl_error(req, "Device %s is invalid", name);
12470c883cefSAlexander Motin 		return;
12480c883cefSAlexander Motin 	}
12490c883cefSAlexander Motin 	sc = gp->softc;
12500c883cefSAlexander Motin 
12510c883cefSAlexander Motin 	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
12520c883cefSAlexander Motin 		cp = sc->sc_active;
12530c883cefSAlexander Motin 		pp = cp->provider;
12540c883cefSAlexander Motin 		error = g_access(cp, 1, 1, 1);
12550c883cefSAlexander Motin 		if (error != 0) {
12560c883cefSAlexander Motin 			gctl_error(req, "Can't open %s (%d)", pp->name, error);
12570c883cefSAlexander Motin 			goto destroy;
12580c883cefSAlexander Motin 		}
12590c883cefSAlexander Motin 		g_topology_unlock();
12600c883cefSAlexander Motin 		buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
12610c883cefSAlexander Motin 		error = g_write_data(cp, pp->mediasize - pp->sectorsize,
12620c883cefSAlexander Motin 		    buf, pp->sectorsize);
12630c883cefSAlexander Motin 		g_topology_lock();
12640c883cefSAlexander Motin 		g_access(cp, -1, -1, -1);
12650c883cefSAlexander Motin 		if (error != 0)
12660c883cefSAlexander Motin 			gctl_error(req, "Can't erase metadata on %s (%d)",
12670c883cefSAlexander Motin 			    pp->name, error);
12680c883cefSAlexander Motin 	}
12690c883cefSAlexander Motin 
12700c883cefSAlexander Motin destroy:
12710c883cefSAlexander Motin 	error = g_multipath_destroy(gp);
12720c883cefSAlexander Motin 	if (error != 0 && error != EINPROGRESS)
12730c883cefSAlexander Motin 		gctl_error(req, "failed to destroy %s (err=%d)", name, error);
1274e770bc6bSMatt Jacob }
1275e770bc6bSMatt Jacob 
1276e770bc6bSMatt Jacob static void
1277b5dce617SMatt Jacob g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp)
1278b5dce617SMatt Jacob {
1279b5dce617SMatt Jacob 	struct g_geom *gp;
1280b5dce617SMatt Jacob 	const char *name;
1281b5dce617SMatt Jacob 	int error;
1282b5dce617SMatt Jacob 
1283b5dce617SMatt Jacob 	g_topology_assert();
1284b5dce617SMatt Jacob 
1285b5dce617SMatt Jacob 	name = gctl_get_asciiparam(req, "arg0");
1286b5dce617SMatt Jacob         if (name == NULL) {
1287b5dce617SMatt Jacob                 gctl_error(req, "No 'arg0' argument");
1288b5dce617SMatt Jacob                 return;
1289b5dce617SMatt Jacob         }
1290b5dce617SMatt Jacob 	gp = g_multipath_find_geom(mp, name);
1291b5dce617SMatt Jacob 	if (gp == NULL) {
1292b5dce617SMatt Jacob 		gctl_error(req, "Device %s is invalid", name);
1293b5dce617SMatt Jacob 		return;
1294b5dce617SMatt Jacob 	}
1295b5dce617SMatt Jacob 	error = g_multipath_rotate(gp);
1296b5dce617SMatt Jacob 	if (error != 0) {
1297b5dce617SMatt Jacob 		gctl_error(req, "failed to rotate %s (err=%d)", name, error);
1298b5dce617SMatt Jacob 	}
1299b5dce617SMatt Jacob }
1300b5dce617SMatt Jacob 
1301b5dce617SMatt Jacob static void
1302b5dce617SMatt Jacob g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp)
1303b5dce617SMatt Jacob {
1304b5dce617SMatt Jacob 	struct sbuf *sb;
1305b5dce617SMatt Jacob 	struct g_geom *gp;
1306b5dce617SMatt Jacob 	struct g_multipath_softc *sc;
13070c883cefSAlexander Motin 	struct g_consumer *cp;
1308b5dce617SMatt Jacob 	const char *name;
13090c883cefSAlexander Motin 	int empty;
1310b5dce617SMatt Jacob 
1311b5dce617SMatt Jacob 	sb = sbuf_new_auto();
1312b5dce617SMatt Jacob 
1313b5dce617SMatt Jacob 	g_topology_assert();
1314b5dce617SMatt Jacob 	name = gctl_get_asciiparam(req, "arg0");
1315b5dce617SMatt Jacob         if (name == NULL) {
1316b5dce617SMatt Jacob                 gctl_error(req, "No 'arg0' argument");
1317b5dce617SMatt Jacob                 return;
1318b5dce617SMatt Jacob         }
1319b5dce617SMatt Jacob 	gp = g_multipath_find_geom(mp, name);
1320b5dce617SMatt Jacob 	if (gp == NULL) {
1321b5dce617SMatt Jacob 		gctl_error(req, "Device %s is invalid", name);
1322b5dce617SMatt Jacob 		return;
1323b5dce617SMatt Jacob 	}
1324b5dce617SMatt Jacob 	sc = gp->softc;
132563297dfdSAlexander Motin 	if (sc->sc_active_active == 1) {
13260c883cefSAlexander Motin 		empty = 1;
13270c883cefSAlexander Motin 		LIST_FOREACH(cp, &gp->consumer, consumer) {
13280c883cefSAlexander Motin 			if (cp->index & MP_BAD)
13290c883cefSAlexander Motin 				continue;
13300c883cefSAlexander Motin 			if (!empty)
13310c883cefSAlexander Motin 				sbuf_cat(sb, " ");
13320c883cefSAlexander Motin 			sbuf_cat(sb, cp->provider->name);
13330c883cefSAlexander Motin 			empty = 0;
13340c883cefSAlexander Motin 		}
13350c883cefSAlexander Motin 		if (empty)
13360c883cefSAlexander Motin 			sbuf_cat(sb, "none");
13370c883cefSAlexander Motin 		sbuf_cat(sb, "\n");
13380c883cefSAlexander Motin 	} else if (sc->sc_active && sc->sc_active->provider) {
13390c883cefSAlexander Motin 		sbuf_printf(sb, "%s\n", sc->sc_active->provider->name);
1340b5dce617SMatt Jacob 	} else {
1341b5dce617SMatt Jacob 		sbuf_printf(sb, "none\n");
1342b5dce617SMatt Jacob 	}
1343b5dce617SMatt Jacob 	sbuf_finish(sb);
1344b5dce617SMatt Jacob 	gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1345b5dce617SMatt Jacob 	sbuf_delete(sb);
1346b5dce617SMatt Jacob }
1347b5dce617SMatt Jacob 
1348b5dce617SMatt Jacob static void
1349e770bc6bSMatt Jacob g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1350e770bc6bSMatt Jacob {
1351e770bc6bSMatt Jacob 	uint32_t *version;
1352e770bc6bSMatt Jacob 	g_topology_assert();
1353e770bc6bSMatt Jacob 	version = gctl_get_paraml(req, "version", sizeof(*version));
1354e770bc6bSMatt Jacob 	if (version == NULL) {
1355e770bc6bSMatt Jacob 		gctl_error(req, "No 'version' argument");
1356e770bc6bSMatt Jacob 	} else if (*version != G_MULTIPATH_VERSION) {
1357e770bc6bSMatt Jacob 		gctl_error(req, "Userland and kernel parts are out of sync");
13582b4969ffSMatt Jacob 	} else if (strcmp(verb, "add") == 0) {
13592b4969ffSMatt Jacob 		g_multipath_ctl_add(req, mp);
136071ee4ef0SThomas Quinot 	} else if (strcmp(verb, "prefer") == 0) {
136171ee4ef0SThomas Quinot 		g_multipath_ctl_prefer(req, mp);
13620c883cefSAlexander Motin 	} else if (strcmp(verb, "create") == 0) {
13630c883cefSAlexander Motin 		g_multipath_ctl_create(req, mp);
136463297dfdSAlexander Motin 	} else if (strcmp(verb, "configure") == 0) {
136563297dfdSAlexander Motin 		g_multipath_ctl_configure(req, mp);
13660c883cefSAlexander Motin 	} else if (strcmp(verb, "stop") == 0) {
13670c883cefSAlexander Motin 		g_multipath_ctl_stop(req, mp);
1368e770bc6bSMatt Jacob 	} else if (strcmp(verb, "destroy") == 0) {
1369e770bc6bSMatt Jacob 		g_multipath_ctl_destroy(req, mp);
13700c883cefSAlexander Motin 	} else if (strcmp(verb, "fail") == 0) {
13710c883cefSAlexander Motin 		g_multipath_ctl_fail(req, mp, 1);
13720c883cefSAlexander Motin 	} else if (strcmp(verb, "restore") == 0) {
13730c883cefSAlexander Motin 		g_multipath_ctl_fail(req, mp, 0);
13740c883cefSAlexander Motin 	} else if (strcmp(verb, "remove") == 0) {
13750c883cefSAlexander Motin 		g_multipath_ctl_remove(req, mp);
1376b5dce617SMatt Jacob 	} else if (strcmp(verb, "rotate") == 0) {
1377b5dce617SMatt Jacob 		g_multipath_ctl_rotate(req, mp);
1378b5dce617SMatt Jacob 	} else if (strcmp(verb, "getactive") == 0) {
1379b5dce617SMatt Jacob 		g_multipath_ctl_getactive(req, mp);
1380e770bc6bSMatt Jacob 	} else {
1381e770bc6bSMatt Jacob 		gctl_error(req, "Unknown verb %s", verb);
1382e770bc6bSMatt Jacob 	}
1383e770bc6bSMatt Jacob }
13840c883cefSAlexander Motin 
13850c883cefSAlexander Motin static void
13860c883cefSAlexander Motin g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
13870c883cefSAlexander Motin     struct g_consumer *cp, struct g_provider *pp)
13880c883cefSAlexander Motin {
13890c883cefSAlexander Motin 	struct g_multipath_softc *sc;
13900c883cefSAlexander Motin 	int good;
13910c883cefSAlexander Motin 
13920c883cefSAlexander Motin 	g_topology_assert();
13930c883cefSAlexander Motin 
13940c883cefSAlexander Motin 	sc = gp->softc;
13950c883cefSAlexander Motin 	if (sc == NULL)
13960c883cefSAlexander Motin 		return;
13970c883cefSAlexander Motin 	if (cp != NULL) {
1398a839e332SAlexander Motin 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
13990c883cefSAlexander Motin 		    (cp->index & MP_NEW) ? "NEW" :
14000c883cefSAlexander Motin 		    (cp->index & MP_LOST) ? "LOST" :
14010c883cefSAlexander Motin 		    (cp->index & MP_FAIL) ? "FAIL" :
140263297dfdSAlexander Motin 		    (sc->sc_active_active == 1 || sc->sc_active == cp) ?
140363297dfdSAlexander Motin 		     "ACTIVE" :
140463297dfdSAlexander Motin 		     sc->sc_active_active == 2 ? "READ" : "PASSIVE");
14050c883cefSAlexander Motin 	} else {
14060c883cefSAlexander Motin 		good = g_multipath_good(gp);
1407a839e332SAlexander Motin 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
14080c883cefSAlexander Motin 		    good == 0 ? "BROKEN" :
14090c883cefSAlexander Motin 		    (good != sc->sc_ndisks || sc->sc_ndisks == 1) ?
14100c883cefSAlexander Motin 		    "DEGRADED" : "OPTIMAL");
14110c883cefSAlexander Motin 	}
14120c883cefSAlexander Motin 	if (cp == NULL && pp == NULL) {
1413a839e332SAlexander Motin 		sbuf_printf(sb, "%s<UUID>%s</UUID>\n", indent, sc->sc_uuid);
1414a839e332SAlexander Motin 		sbuf_printf(sb, "%s<Mode>Active/%s</Mode>\n", indent,
141563297dfdSAlexander Motin 		    sc->sc_active_active == 2 ? "Read" :
141663297dfdSAlexander Motin 		    sc->sc_active_active == 1 ? "Active" : "Passive");
1417a839e332SAlexander Motin 		sbuf_printf(sb, "%s<Type>%s</Type>\n", indent,
14180c883cefSAlexander Motin 		    sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC");
14190c883cefSAlexander Motin 	}
14200c883cefSAlexander Motin }
14210c883cefSAlexander Motin 
1422e770bc6bSMatt Jacob DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);
1423