xref: /freebsd/sys/geom/multipath/g_multipath.c (revision d3fef0a0924fbb7ff7e29e208641e2993464dd40)
1e770bc6bSMatt Jacob /*-
2e6afd72bSAlexander Motin  * Copyright (c) 2011-2013 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>
39e6afd72bSAlexander Motin #include <sys/limits.h>
40e770bc6bSMatt Jacob #include <sys/lock.h>
41e770bc6bSMatt Jacob #include <sys/mutex.h>
42e770bc6bSMatt Jacob #include <sys/bio.h>
435d807a0eSAndrey V. Elsukov #include <sys/sbuf.h>
44e770bc6bSMatt Jacob #include <sys/sysctl.h>
45e770bc6bSMatt Jacob #include <sys/kthread.h>
46e770bc6bSMatt Jacob #include <sys/malloc.h>
47e770bc6bSMatt Jacob #include <geom/geom.h>
48e770bc6bSMatt Jacob #include <geom/multipath/g_multipath.h>
49e770bc6bSMatt Jacob 
50cb08c2ccSAlexander Leidinger FEATURE(geom_multipath, "GEOM multipath support");
51e770bc6bSMatt Jacob 
52e770bc6bSMatt Jacob SYSCTL_DECL(_kern_geom);
536472ac3dSEd Schouten static SYSCTL_NODE(_kern_geom, OID_AUTO, multipath, CTLFLAG_RW, 0,
54e770bc6bSMatt Jacob     "GEOM_MULTIPATH tunables");
55e770bc6bSMatt Jacob static u_int g_multipath_debug = 0;
56e770bc6bSMatt Jacob SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, debug, CTLFLAG_RW,
57e770bc6bSMatt Jacob     &g_multipath_debug, 0, "Debug level");
580c883cefSAlexander Motin static u_int g_multipath_exclusive = 1;
590c883cefSAlexander Motin SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, exclusive, CTLFLAG_RW,
600c883cefSAlexander Motin     &g_multipath_exclusive, 0, "Exclusively open providers");
61e770bc6bSMatt Jacob 
62e770bc6bSMatt Jacob static enum {
63e770bc6bSMatt Jacob 	GKT_NIL,
64e770bc6bSMatt Jacob 	GKT_RUN,
65e770bc6bSMatt Jacob 	GKT_DIE
66e770bc6bSMatt Jacob } g_multipath_kt_state;
67e770bc6bSMatt Jacob static struct bio_queue_head gmtbq;
68e770bc6bSMatt Jacob static struct mtx gmtbq_mtx;
69e770bc6bSMatt Jacob 
70f8c79813SAlexander Motin static int g_multipath_read_metadata(struct g_consumer *cp,
71f8c79813SAlexander Motin     struct g_multipath_metadata *md);
72f8c79813SAlexander Motin static int g_multipath_write_metadata(struct g_consumer *cp,
73f8c79813SAlexander Motin     struct g_multipath_metadata *md);
74f8c79813SAlexander Motin 
75e770bc6bSMatt Jacob static void g_multipath_orphan(struct g_consumer *);
76e6afd72bSAlexander Motin static void g_multipath_resize(struct g_consumer *);
77e770bc6bSMatt Jacob static void g_multipath_start(struct bio *);
78e770bc6bSMatt Jacob static void g_multipath_done(struct bio *);
79e770bc6bSMatt Jacob static void g_multipath_done_error(struct bio *);
80e770bc6bSMatt Jacob static void g_multipath_kt(void *);
81e770bc6bSMatt Jacob 
82e770bc6bSMatt Jacob static int g_multipath_destroy(struct g_geom *);
83e770bc6bSMatt Jacob static int
84e770bc6bSMatt Jacob g_multipath_destroy_geom(struct gctl_req *, struct g_class *, struct g_geom *);
85e770bc6bSMatt Jacob 
862b4969ffSMatt Jacob static struct g_geom *g_multipath_find_geom(struct g_class *, const char *);
87b5dce617SMatt Jacob static int g_multipath_rotate(struct g_geom *);
88b5dce617SMatt Jacob 
89e770bc6bSMatt Jacob static g_taste_t g_multipath_taste;
90e770bc6bSMatt Jacob static g_ctl_req_t g_multipath_config;
91e770bc6bSMatt Jacob static g_init_t g_multipath_init;
92e770bc6bSMatt Jacob static g_fini_t g_multipath_fini;
930c883cefSAlexander Motin static g_dumpconf_t g_multipath_dumpconf;
94e770bc6bSMatt Jacob 
95e770bc6bSMatt Jacob struct g_class g_multipath_class = {
96e770bc6bSMatt Jacob 	.name		= G_MULTIPATH_CLASS_NAME,
97e770bc6bSMatt Jacob 	.version	= G_VERSION,
98e770bc6bSMatt Jacob 	.ctlreq		= g_multipath_config,
99e770bc6bSMatt Jacob 	.taste		= g_multipath_taste,
100e770bc6bSMatt Jacob 	.destroy_geom	= g_multipath_destroy_geom,
101e770bc6bSMatt Jacob 	.init		= g_multipath_init,
102e770bc6bSMatt Jacob 	.fini		= g_multipath_fini
103e770bc6bSMatt Jacob };
104e770bc6bSMatt Jacob 
1050c883cefSAlexander Motin #define	MP_FAIL		0x00000001
1060c883cefSAlexander Motin #define	MP_LOST		0x00000002
1070c883cefSAlexander Motin #define	MP_NEW		0x00000004
1080c883cefSAlexander Motin #define	MP_POSTED	0x00000008
1090c883cefSAlexander Motin #define	MP_BAD		(MP_FAIL | MP_LOST | MP_NEW)
11025080ac4SSteven Hartland #define	MP_WITHER	0x00000010
11125080ac4SSteven Hartland #define	MP_IDLE		0x00000020
11225080ac4SSteven Hartland #define	MP_IDLE_MASK	0xffffffe0
1130c883cefSAlexander Motin 
1140c883cefSAlexander Motin static int
1150c883cefSAlexander Motin g_multipath_good(struct g_geom *gp)
1160c883cefSAlexander Motin {
1170c883cefSAlexander Motin 	struct g_consumer *cp;
1180c883cefSAlexander Motin 	int n = 0;
1190c883cefSAlexander Motin 
1200c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
1210c883cefSAlexander Motin 		if ((cp->index & MP_BAD) == 0)
1220c883cefSAlexander Motin 			n++;
1230c883cefSAlexander Motin 	}
1240c883cefSAlexander Motin 	return (n);
1250c883cefSAlexander Motin }
1260c883cefSAlexander Motin 
1270c883cefSAlexander Motin static void
1280c883cefSAlexander Motin g_multipath_fault(struct g_consumer *cp, int cause)
1290c883cefSAlexander Motin {
1300c883cefSAlexander Motin 	struct g_multipath_softc *sc;
1310c883cefSAlexander Motin 	struct g_consumer *lcp;
1320c883cefSAlexander Motin 	struct g_geom *gp;
1330c883cefSAlexander Motin 
1340c883cefSAlexander Motin 	gp = cp->geom;
1350c883cefSAlexander Motin 	sc = gp->softc;
1360c883cefSAlexander Motin 	cp->index |= cause;
1370c883cefSAlexander Motin 	if (g_multipath_good(gp) == 0 && sc->sc_ndisks > 0) {
1380c883cefSAlexander Motin 		LIST_FOREACH(lcp, &gp->consumer, consumer) {
1390c883cefSAlexander Motin 			if (lcp->provider == NULL ||
1400c883cefSAlexander Motin 			    (lcp->index & (MP_LOST | MP_NEW)))
1410c883cefSAlexander Motin 				continue;
1420c883cefSAlexander Motin 			if (sc->sc_ndisks > 1 && lcp == cp)
1430c883cefSAlexander Motin 				continue;
1440c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: "
1450c883cefSAlexander Motin 			    "all paths in %s were marked FAIL, restore %s\n",
1460c883cefSAlexander Motin 			    sc->sc_name, lcp->provider->name);
1470c883cefSAlexander Motin 			lcp->index &= ~MP_FAIL;
1480c883cefSAlexander Motin 		}
1490c883cefSAlexander Motin 	}
1500c883cefSAlexander Motin 	if (cp != sc->sc_active)
1510c883cefSAlexander Motin 		return;
1520c883cefSAlexander Motin 	sc->sc_active = NULL;
1530c883cefSAlexander Motin 	LIST_FOREACH(lcp, &gp->consumer, consumer) {
1540c883cefSAlexander Motin 		if ((lcp->index & MP_BAD) == 0) {
1550c883cefSAlexander Motin 			sc->sc_active = lcp;
1560c883cefSAlexander Motin 			break;
1570c883cefSAlexander Motin 		}
1580c883cefSAlexander Motin 	}
1590c883cefSAlexander Motin 	if (sc->sc_active == NULL) {
1600c883cefSAlexander Motin 		printf("GEOM_MULTIPATH: out of providers for %s\n",
1610c883cefSAlexander Motin 		    sc->sc_name);
16263297dfdSAlexander Motin 	} else if (sc->sc_active_active != 1) {
1630c883cefSAlexander Motin 		printf("GEOM_MULTIPATH: %s is now active path in %s\n",
1640c883cefSAlexander Motin 		    sc->sc_active->provider->name, sc->sc_name);
1650c883cefSAlexander Motin 	}
1660c883cefSAlexander Motin }
1670c883cefSAlexander Motin 
1680c883cefSAlexander Motin static struct g_consumer *
16963297dfdSAlexander Motin g_multipath_choose(struct g_geom *gp, struct bio *bp)
1700c883cefSAlexander Motin {
1710c883cefSAlexander Motin 	struct g_multipath_softc *sc;
1720c883cefSAlexander Motin 	struct g_consumer *best, *cp;
1730c883cefSAlexander Motin 
1740c883cefSAlexander Motin 	sc = gp->softc;
17563297dfdSAlexander Motin 	if (sc->sc_active_active == 0 ||
17663297dfdSAlexander Motin 	    (sc->sc_active_active == 2 && bp->bio_cmd != BIO_READ))
1770c883cefSAlexander Motin 		return (sc->sc_active);
1780c883cefSAlexander Motin 	best = NULL;
1790c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
1800c883cefSAlexander Motin 		if (cp->index & MP_BAD)
1810c883cefSAlexander Motin 			continue;
1820c883cefSAlexander Motin 		cp->index += MP_IDLE;
1830c883cefSAlexander Motin 		if (best == NULL || cp->private < best->private ||
1840c883cefSAlexander Motin 		    (cp->private == best->private && cp->index > best->index))
1850c883cefSAlexander Motin 			best = cp;
1860c883cefSAlexander Motin 	}
1870c883cefSAlexander Motin 	if (best != NULL)
1880c883cefSAlexander Motin 		best->index &= ~MP_IDLE_MASK;
1890c883cefSAlexander Motin 	return (best);
1900c883cefSAlexander Motin }
191e770bc6bSMatt Jacob 
192e770bc6bSMatt Jacob static void
193e770bc6bSMatt Jacob g_mpd(void *arg, int flags __unused)
194e770bc6bSMatt Jacob {
1950c883cefSAlexander Motin 	struct g_geom *gp;
1960c883cefSAlexander Motin 	struct g_multipath_softc *sc;
197e770bc6bSMatt Jacob 	struct g_consumer *cp;
1980c883cefSAlexander Motin 	int w;
199e770bc6bSMatt Jacob 
200e770bc6bSMatt Jacob 	g_topology_assert();
201e770bc6bSMatt Jacob 	cp = arg;
2020c883cefSAlexander Motin 	gp = cp->geom;
2030c883cefSAlexander Motin 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) {
2040c883cefSAlexander Motin 		w = cp->acw;
205e770bc6bSMatt Jacob 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
2060c883cefSAlexander Motin 		if (w > 0 && cp->provider != NULL &&
2070c883cefSAlexander Motin 		    (cp->provider->geom->flags & G_GEOM_WITHER) == 0) {
20825080ac4SSteven Hartland 			cp->index |= MP_WITHER;
2090c883cefSAlexander Motin 			g_post_event(g_mpd, cp, M_WAITOK, NULL);
2100c883cefSAlexander Motin 			return;
2110c883cefSAlexander Motin 		}
2120c883cefSAlexander Motin 	}
2130c883cefSAlexander Motin 	sc = gp->softc;
2140c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
215e770bc6bSMatt Jacob 	if (cp->provider) {
216e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: %s removed from %s\n",
2170c883cefSAlexander Motin 		    cp->provider->name, gp->name);
218e770bc6bSMatt Jacob 		g_detach(cp);
219e770bc6bSMatt Jacob 	}
220e770bc6bSMatt Jacob 	g_destroy_consumer(cp);
2210c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
2220c883cefSAlexander Motin 	if (LIST_EMPTY(&gp->consumer))
2230c883cefSAlexander Motin 		g_multipath_destroy(gp);
224e770bc6bSMatt Jacob }
225e770bc6bSMatt Jacob 
226e770bc6bSMatt Jacob static void
227e770bc6bSMatt Jacob g_multipath_orphan(struct g_consumer *cp)
228e770bc6bSMatt Jacob {
2290c883cefSAlexander Motin 	struct g_multipath_softc *sc;
2300c883cefSAlexander Motin 	uintptr_t *cnt;
2310c883cefSAlexander Motin 
2320c883cefSAlexander Motin 	g_topology_assert();
2330c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s in %s was disconnected\n",
234e770bc6bSMatt Jacob 	    cp->provider->name, cp->geom->name);
2350c883cefSAlexander Motin 	sc = cp->geom->softc;
2360c883cefSAlexander Motin 	cnt = (uintptr_t *)&cp->private;
2370c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
2380c883cefSAlexander Motin 	sc->sc_ndisks--;
2390c883cefSAlexander Motin 	g_multipath_fault(cp, MP_LOST);
2400c883cefSAlexander Motin 	if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
2410c883cefSAlexander Motin 		cp->index |= MP_POSTED;
2420c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
243e770bc6bSMatt Jacob 		g_mpd(cp, 0);
2440c883cefSAlexander Motin 	} else
2450c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
246e770bc6bSMatt Jacob }
247e770bc6bSMatt Jacob 
248e770bc6bSMatt Jacob static void
249e6afd72bSAlexander Motin g_multipath_resize(struct g_consumer *cp)
250e6afd72bSAlexander Motin {
251e6afd72bSAlexander Motin 	struct g_multipath_softc *sc;
252e6afd72bSAlexander Motin 	struct g_geom *gp;
253f8c79813SAlexander Motin 	struct g_consumer *cp1;
254e6afd72bSAlexander Motin 	struct g_provider *pp;
255e6afd72bSAlexander Motin 	struct g_multipath_metadata md;
256e6afd72bSAlexander Motin 	off_t size, psize, ssize;
257e6afd72bSAlexander Motin 	int error;
258e6afd72bSAlexander Motin 
259e6afd72bSAlexander Motin 	g_topology_assert();
260e6afd72bSAlexander Motin 
261e6afd72bSAlexander Motin 	gp = cp->geom;
262e6afd72bSAlexander Motin 	pp = cp->provider;
263e6afd72bSAlexander Motin 	sc = gp->softc;
264e6afd72bSAlexander Motin 
265e6afd72bSAlexander Motin 	if (sc->sc_stopping)
266e6afd72bSAlexander Motin 		return;
267e6afd72bSAlexander Motin 
268e6afd72bSAlexander Motin 	if (pp->mediasize < sc->sc_size) {
269e6afd72bSAlexander Motin 		size = pp->mediasize;
270e6afd72bSAlexander Motin 		ssize = pp->sectorsize;
271e6afd72bSAlexander Motin 	} else {
272e6afd72bSAlexander Motin 		size = ssize = OFF_MAX;
273e6afd72bSAlexander Motin 		mtx_lock(&sc->sc_mtx);
274f8c79813SAlexander Motin 		LIST_FOREACH(cp1, &gp->consumer, consumer) {
275f8c79813SAlexander Motin 			pp = cp1->provider;
276e6afd72bSAlexander Motin 			if (pp == NULL)
277e6afd72bSAlexander Motin 				continue;
278e6afd72bSAlexander Motin 			if (pp->mediasize < size) {
279e6afd72bSAlexander Motin 				size = pp->mediasize;
280e6afd72bSAlexander Motin 				ssize = pp->sectorsize;
281e6afd72bSAlexander Motin 			}
282e6afd72bSAlexander Motin 		}
283e6afd72bSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
284e6afd72bSAlexander Motin 		if (size == OFF_MAX || size == sc->sc_size)
285e6afd72bSAlexander Motin 			return;
286e6afd72bSAlexander Motin 	}
287e6afd72bSAlexander Motin 	psize = size - ((sc->sc_uuid[0] != 0) ? ssize : 0);
288e6afd72bSAlexander Motin 	printf("GEOM_MULTIPATH: %s size changed from %jd to %jd\n",
289e6afd72bSAlexander Motin 	    sc->sc_name, sc->sc_pp->mediasize, psize);
290f8c79813SAlexander Motin 	if (sc->sc_uuid[0] != 0 && size < sc->sc_size) {
291f8c79813SAlexander Motin 		error = g_multipath_read_metadata(cp, &md);
292f8c79813SAlexander Motin 		if (error ||
293f8c79813SAlexander Motin 		    (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) ||
294f8c79813SAlexander Motin 		    (memcmp(md.md_uuid, sc->sc_uuid, sizeof(sc->sc_uuid)) != 0) ||
295f8c79813SAlexander Motin 		    (strcmp(md.md_name, sc->sc_name) != 0) ||
296f8c79813SAlexander Motin 		    (md.md_size != 0 && md.md_size != size) ||
297f8c79813SAlexander Motin 		    (md.md_sectorsize != 0 && md.md_sectorsize != ssize)) {
298e6afd72bSAlexander Motin 			g_multipath_destroy(gp);
299e6afd72bSAlexander Motin 			return;
300e6afd72bSAlexander Motin 		}
301f8c79813SAlexander Motin 	}
302e6afd72bSAlexander Motin 	sc->sc_size = size;
303e6afd72bSAlexander Motin 	g_resize_provider(sc->sc_pp, psize);
304e6afd72bSAlexander Motin 
305f8c79813SAlexander Motin 	if (sc->sc_uuid[0] != 0) {
306e6afd72bSAlexander Motin 		pp = cp->provider;
307e6afd72bSAlexander Motin 		strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
308e6afd72bSAlexander Motin 		memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
309e6afd72bSAlexander Motin 		strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
310e6afd72bSAlexander Motin 		md.md_version = G_MULTIPATH_VERSION;
311e6afd72bSAlexander Motin 		md.md_size = size;
312f8c79813SAlexander Motin 		md.md_sectorsize = ssize;
313e6afd72bSAlexander Motin 		md.md_active_active = sc->sc_active_active;
314f8c79813SAlexander Motin 		error = g_multipath_write_metadata(cp, &md);
315e6afd72bSAlexander Motin 		if (error != 0)
316e6afd72bSAlexander Motin 			printf("GEOM_MULTIPATH: Can't update metadata on %s "
317e6afd72bSAlexander Motin 			    "(%d)\n", pp->name, error);
318e6afd72bSAlexander Motin 	}
319e6afd72bSAlexander Motin }
320e6afd72bSAlexander Motin 
321e6afd72bSAlexander Motin static void
322e770bc6bSMatt Jacob g_multipath_start(struct bio *bp)
323e770bc6bSMatt Jacob {
324e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
325e770bc6bSMatt Jacob 	struct g_geom *gp;
326e770bc6bSMatt Jacob 	struct g_consumer *cp;
327e770bc6bSMatt Jacob 	struct bio *cbp;
3280c883cefSAlexander Motin 	uintptr_t *cnt;
329e770bc6bSMatt Jacob 
330e770bc6bSMatt Jacob 	gp = bp->bio_to->geom;
331e770bc6bSMatt Jacob 	sc = gp->softc;
332e770bc6bSMatt Jacob 	KASSERT(sc != NULL, ("NULL sc"));
333e770bc6bSMatt Jacob 	cbp = g_clone_bio(bp);
334e770bc6bSMatt Jacob 	if (cbp == NULL) {
335e770bc6bSMatt Jacob 		g_io_deliver(bp, ENOMEM);
336e770bc6bSMatt Jacob 		return;
337e770bc6bSMatt Jacob 	}
3380c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
33963297dfdSAlexander Motin 	cp = g_multipath_choose(gp, bp);
3400c883cefSAlexander Motin 	if (cp == NULL) {
3410c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
3420c883cefSAlexander Motin 		g_destroy_bio(cbp);
3430c883cefSAlexander Motin 		g_io_deliver(bp, ENXIO);
3440c883cefSAlexander Motin 		return;
3450c883cefSAlexander Motin 	}
3460c883cefSAlexander Motin 	if ((uintptr_t)bp->bio_driver1 < sc->sc_ndisks)
3470c883cefSAlexander Motin 		bp->bio_driver1 = (void *)(uintptr_t)sc->sc_ndisks;
3480c883cefSAlexander Motin 	cnt = (uintptr_t *)&cp->private;
3490c883cefSAlexander Motin 	(*cnt)++;
3500c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
351e770bc6bSMatt Jacob 	cbp->bio_done = g_multipath_done;
352e770bc6bSMatt Jacob 	g_io_request(cbp, cp);
353e770bc6bSMatt Jacob }
354e770bc6bSMatt Jacob 
355e770bc6bSMatt Jacob static void
356e770bc6bSMatt Jacob g_multipath_done(struct bio *bp)
357e770bc6bSMatt Jacob {
3580c883cefSAlexander Motin 	struct g_multipath_softc *sc;
3590c883cefSAlexander Motin 	struct g_consumer *cp;
3600c883cefSAlexander Motin 	uintptr_t *cnt;
3610c883cefSAlexander Motin 
362e770bc6bSMatt Jacob 	if (bp->bio_error == ENXIO || bp->bio_error == EIO) {
363e770bc6bSMatt Jacob 		mtx_lock(&gmtbq_mtx);
364e770bc6bSMatt Jacob 		bioq_insert_tail(&gmtbq, bp);
365e770bc6bSMatt Jacob 		mtx_unlock(&gmtbq_mtx);
3660c883cefSAlexander Motin 		wakeup(&g_multipath_kt_state);
367e770bc6bSMatt Jacob 	} else {
3680c883cefSAlexander Motin 		cp = bp->bio_from;
3690c883cefSAlexander Motin 		sc = cp->geom->softc;
3700c883cefSAlexander Motin 		cnt = (uintptr_t *)&cp->private;
3710c883cefSAlexander Motin 		mtx_lock(&sc->sc_mtx);
3720c883cefSAlexander Motin 		(*cnt)--;
3730c883cefSAlexander Motin 		if (*cnt == 0 && (cp->index & MP_LOST)) {
3740ada3afcSAlexander Motin 			if (g_post_event(g_mpd, cp, M_NOWAIT, NULL) == 0)
3750c883cefSAlexander Motin 				cp->index |= MP_POSTED;
3760c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
3770c883cefSAlexander Motin 		} else
3780c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
379e770bc6bSMatt Jacob 		g_std_done(bp);
380e770bc6bSMatt Jacob 	}
381e770bc6bSMatt Jacob }
382e770bc6bSMatt Jacob 
383e770bc6bSMatt Jacob static void
384e770bc6bSMatt Jacob g_multipath_done_error(struct bio *bp)
385e770bc6bSMatt Jacob {
386e770bc6bSMatt Jacob 	struct bio *pbp;
387e770bc6bSMatt Jacob 	struct g_geom *gp;
388e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
389e770bc6bSMatt Jacob 	struct g_consumer *cp;
390e770bc6bSMatt Jacob 	struct g_provider *pp;
3910c883cefSAlexander Motin 	uintptr_t *cnt;
392e770bc6bSMatt Jacob 
393e770bc6bSMatt Jacob 	/*
394e770bc6bSMatt Jacob 	 * If we had a failure, we have to check first to see
395e770bc6bSMatt Jacob 	 * whether the consumer it failed on was the currently
396e770bc6bSMatt Jacob 	 * active consumer (i.e., this is the first in perhaps
397e770bc6bSMatt Jacob 	 * a number of failures). If so, we then switch consumers
398e770bc6bSMatt Jacob 	 * to the next available consumer.
399e770bc6bSMatt Jacob 	 */
400e770bc6bSMatt Jacob 
401e770bc6bSMatt Jacob 	pbp = bp->bio_parent;
402e770bc6bSMatt Jacob 	gp = pbp->bio_to->geom;
403e770bc6bSMatt Jacob 	sc = gp->softc;
404e770bc6bSMatt Jacob 	cp = bp->bio_from;
405e770bc6bSMatt Jacob 	pp = cp->provider;
4060c883cefSAlexander Motin 	cnt = (uintptr_t *)&cp->private;
407e770bc6bSMatt Jacob 
4080c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
40963297dfdSAlexander Motin 	if ((cp->index & MP_FAIL) == 0) {
4100c883cefSAlexander Motin 		printf("GEOM_MULTIPATH: Error %d, %s in %s marked FAIL\n",
4110c883cefSAlexander Motin 		    bp->bio_error, pp->name, sc->sc_name);
4120c883cefSAlexander Motin 		g_multipath_fault(cp, MP_FAIL);
41363297dfdSAlexander Motin 	}
4140c883cefSAlexander Motin 	(*cnt)--;
4150c883cefSAlexander Motin 	if (*cnt == 0 && (cp->index & (MP_LOST | MP_POSTED)) == MP_LOST) {
416e770bc6bSMatt Jacob 		cp->index |= MP_POSTED;
4170c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
4180c883cefSAlexander Motin 		g_post_event(g_mpd, cp, M_WAITOK, NULL);
4190c883cefSAlexander Motin 	} else
4200c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
421e770bc6bSMatt Jacob 
422e770bc6bSMatt Jacob 	/*
423e770bc6bSMatt Jacob 	 * If we can fruitfully restart the I/O, do so.
424e770bc6bSMatt Jacob 	 */
4250c883cefSAlexander Motin 	if (pbp->bio_children < (uintptr_t)pbp->bio_driver1) {
4260c883cefSAlexander Motin 		pbp->bio_inbed++;
427e770bc6bSMatt Jacob 		g_destroy_bio(bp);
428e770bc6bSMatt Jacob 		g_multipath_start(pbp);
429e770bc6bSMatt Jacob 	} else {
430e770bc6bSMatt Jacob 		g_std_done(bp);
431e770bc6bSMatt Jacob 	}
432e770bc6bSMatt Jacob }
433e770bc6bSMatt Jacob 
434e770bc6bSMatt Jacob static void
435e770bc6bSMatt Jacob g_multipath_kt(void *arg)
436e770bc6bSMatt Jacob {
43712f35a61SPawel Jakub Dawidek 
438e770bc6bSMatt Jacob 	g_multipath_kt_state = GKT_RUN;
439e770bc6bSMatt Jacob 	mtx_lock(&gmtbq_mtx);
440e770bc6bSMatt Jacob 	while (g_multipath_kt_state == GKT_RUN) {
441e770bc6bSMatt Jacob 		for (;;) {
442e770bc6bSMatt Jacob 			struct bio *bp;
44312f35a61SPawel Jakub Dawidek 
444e770bc6bSMatt Jacob 			bp = bioq_takefirst(&gmtbq);
44512f35a61SPawel Jakub Dawidek 			if (bp == NULL)
446e770bc6bSMatt Jacob 				break;
447e770bc6bSMatt Jacob 			mtx_unlock(&gmtbq_mtx);
448e770bc6bSMatt Jacob 			g_multipath_done_error(bp);
449e770bc6bSMatt Jacob 			mtx_lock(&gmtbq_mtx);
450e770bc6bSMatt Jacob 		}
45163297dfdSAlexander Motin 		if (g_multipath_kt_state != GKT_RUN)
45263297dfdSAlexander Motin 			break;
453e770bc6bSMatt Jacob 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
45463297dfdSAlexander Motin 		    "gkt:wait", 0);
455e770bc6bSMatt Jacob 	}
456e770bc6bSMatt Jacob 	mtx_unlock(&gmtbq_mtx);
457e770bc6bSMatt Jacob 	wakeup(&g_multipath_kt_state);
4583745c395SJulian Elischer 	kproc_exit(0);
459e770bc6bSMatt Jacob }
460e770bc6bSMatt Jacob 
461e770bc6bSMatt Jacob 
462e770bc6bSMatt Jacob static int
463e770bc6bSMatt Jacob g_multipath_access(struct g_provider *pp, int dr, int dw, int de)
464e770bc6bSMatt Jacob {
465e770bc6bSMatt Jacob 	struct g_geom *gp;
466e770bc6bSMatt Jacob 	struct g_consumer *cp, *badcp = NULL;
4670c883cefSAlexander Motin 	struct g_multipath_softc *sc;
468e770bc6bSMatt Jacob 	int error;
469e770bc6bSMatt Jacob 
470e770bc6bSMatt Jacob 	gp = pp->geom;
471e770bc6bSMatt Jacob 
47225080ac4SSteven Hartland 	/* Error used if we have no valid consumers. */
47380f0a89cSAlexander Motin 	error = (dr > 0 || dw > 0 || de > 0) ? ENXIO : 0;
47425080ac4SSteven Hartland 
475e770bc6bSMatt Jacob 	LIST_FOREACH(cp, &gp->consumer, consumer) {
47625080ac4SSteven Hartland 		if (cp->index & MP_WITHER)
47725080ac4SSteven Hartland 			continue;
47825080ac4SSteven Hartland 
479e770bc6bSMatt Jacob 		error = g_access(cp, dr, dw, de);
480e770bc6bSMatt Jacob 		if (error) {
481e770bc6bSMatt Jacob 			badcp = cp;
482e770bc6bSMatt Jacob 			goto fail;
483e770bc6bSMatt Jacob 		}
484e770bc6bSMatt Jacob 	}
48525080ac4SSteven Hartland 
48625080ac4SSteven Hartland 	if (error != 0)
48725080ac4SSteven Hartland 		return (error);
48825080ac4SSteven Hartland 
4890c883cefSAlexander Motin 	sc = gp->softc;
4900c883cefSAlexander Motin 	sc->sc_opened += dr + dw + de;
4910c883cefSAlexander Motin 	if (sc->sc_stopping && sc->sc_opened == 0)
4920c883cefSAlexander Motin 		g_multipath_destroy(gp);
49325080ac4SSteven Hartland 
494e770bc6bSMatt Jacob 	return (0);
495e770bc6bSMatt Jacob 
496e770bc6bSMatt Jacob fail:
497e770bc6bSMatt Jacob 	LIST_FOREACH(cp, &gp->consumer, consumer) {
49812f35a61SPawel Jakub Dawidek 		if (cp == badcp)
499e770bc6bSMatt Jacob 			break;
50025080ac4SSteven Hartland 		if (cp->index & MP_WITHER)
50125080ac4SSteven Hartland 			continue;
50225080ac4SSteven Hartland 
503e770bc6bSMatt Jacob 		(void) g_access(cp, -dr, -dw, -de);
504e770bc6bSMatt Jacob 	}
505e770bc6bSMatt Jacob 	return (error);
506e770bc6bSMatt Jacob }
507e770bc6bSMatt Jacob 
508e770bc6bSMatt Jacob static struct g_geom *
509e770bc6bSMatt Jacob g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md)
510e770bc6bSMatt Jacob {
511e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
512e770bc6bSMatt Jacob 	struct g_geom *gp;
513e770bc6bSMatt Jacob 	struct g_provider *pp;
514e770bc6bSMatt Jacob 
515e770bc6bSMatt Jacob 	g_topology_assert();
516e770bc6bSMatt Jacob 
517e770bc6bSMatt Jacob 	LIST_FOREACH(gp, &mp->geom, geom) {
5180c883cefSAlexander Motin 		sc = gp->softc;
5190c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
5200c883cefSAlexander Motin 			continue;
521e770bc6bSMatt Jacob 		if (strcmp(gp->name, md->md_name) == 0) {
522e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: name %s already exists\n",
523e770bc6bSMatt Jacob 			    md->md_name);
524e770bc6bSMatt Jacob 			return (NULL);
525e770bc6bSMatt Jacob 		}
526e770bc6bSMatt Jacob 	}
527e770bc6bSMatt Jacob 
52802c62349SJaakko Heinonen 	gp = g_new_geomf(mp, "%s", md->md_name);
529e770bc6bSMatt Jacob 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
5300c883cefSAlexander Motin 	mtx_init(&sc->sc_mtx, "multipath", NULL, MTX_DEF);
5310c883cefSAlexander Motin 	memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid));
5320c883cefSAlexander Motin 	memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name));
5330c883cefSAlexander Motin 	sc->sc_active_active = md->md_active_active;
534e6afd72bSAlexander Motin 	sc->sc_size = md->md_size;
535e770bc6bSMatt Jacob 	gp->softc = sc;
536e770bc6bSMatt Jacob 	gp->start = g_multipath_start;
537e770bc6bSMatt Jacob 	gp->orphan = g_multipath_orphan;
538e6afd72bSAlexander Motin 	gp->resize = g_multipath_resize;
539e770bc6bSMatt Jacob 	gp->access = g_multipath_access;
5400c883cefSAlexander Motin 	gp->dumpconf = g_multipath_dumpconf;
541e770bc6bSMatt Jacob 
542e770bc6bSMatt Jacob 	pp = g_new_providerf(gp, "multipath/%s", md->md_name);
54340ea77a0SAlexander Motin 	pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
5440c883cefSAlexander Motin 	if (md->md_size != 0) {
5450c883cefSAlexander Motin 		pp->mediasize = md->md_size -
5460c883cefSAlexander Motin 		    ((md->md_uuid[0] != 0) ? md->md_sectorsize : 0);
547e770bc6bSMatt Jacob 		pp->sectorsize = md->md_sectorsize;
5480c883cefSAlexander Motin 	}
5490c883cefSAlexander Motin 	sc->sc_pp = pp;
550e770bc6bSMatt Jacob 	g_error_provider(pp, 0);
5510c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s created\n", gp->name);
552e770bc6bSMatt Jacob 	return (gp);
553e770bc6bSMatt Jacob }
554e770bc6bSMatt Jacob 
555e770bc6bSMatt Jacob static int
556e770bc6bSMatt Jacob g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp)
557e770bc6bSMatt Jacob {
558e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
559e770bc6bSMatt Jacob 	struct g_consumer *cp, *nxtcp;
5600c883cefSAlexander Motin 	int error, acr, acw, ace;
561e770bc6bSMatt Jacob 
562e770bc6bSMatt Jacob 	g_topology_assert();
563e770bc6bSMatt Jacob 
564e770bc6bSMatt Jacob 	sc = gp->softc;
565e770bc6bSMatt Jacob 	KASSERT(sc, ("no softc"));
566e770bc6bSMatt Jacob 
567e770bc6bSMatt Jacob 	/*
568e770bc6bSMatt Jacob 	 * Make sure that the passed provider isn't already attached
569e770bc6bSMatt Jacob 	 */
570e770bc6bSMatt Jacob 	LIST_FOREACH(cp, &gp->consumer, consumer) {
57112f35a61SPawel Jakub Dawidek 		if (cp->provider == pp)
572e770bc6bSMatt Jacob 			break;
573e770bc6bSMatt Jacob 	}
574e770bc6bSMatt Jacob 	if (cp) {
575e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: provider %s already attached to %s\n",
576e770bc6bSMatt Jacob 		    pp->name, gp->name);
577e770bc6bSMatt Jacob 		return (EEXIST);
578e770bc6bSMatt Jacob 	}
579e770bc6bSMatt Jacob 	nxtcp = LIST_FIRST(&gp->consumer);
580e770bc6bSMatt Jacob 	cp = g_new_consumer(gp);
58140ea77a0SAlexander Motin 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
5820c883cefSAlexander Motin 	cp->private = NULL;
5830c883cefSAlexander Motin 	cp->index = MP_NEW;
584e770bc6bSMatt Jacob 	error = g_attach(cp, pp);
585e770bc6bSMatt Jacob 	if (error != 0) {
586e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: cannot attach %s to %s",
587e770bc6bSMatt Jacob 		    pp->name, sc->sc_name);
588e770bc6bSMatt Jacob 		g_destroy_consumer(cp);
589e770bc6bSMatt Jacob 		return (error);
590e770bc6bSMatt Jacob 	}
591e770bc6bSMatt Jacob 
592e770bc6bSMatt Jacob 	/*
593e770bc6bSMatt Jacob 	 * Set access permissions on new consumer to match other consumers
594e770bc6bSMatt Jacob 	 */
5950c883cefSAlexander Motin 	if (sc->sc_pp) {
5960c883cefSAlexander Motin 		acr = sc->sc_pp->acr;
5970c883cefSAlexander Motin 		acw = sc->sc_pp->acw;
5980c883cefSAlexander Motin 		ace = sc->sc_pp->ace;
5990c883cefSAlexander Motin 	} else
6000c883cefSAlexander Motin 		acr = acw = ace = 0;
6010c883cefSAlexander Motin 	if (g_multipath_exclusive) {
6020c883cefSAlexander Motin 		acr++;
6030c883cefSAlexander Motin 		acw++;
6040c883cefSAlexander Motin 		ace++;
6050c883cefSAlexander Motin 	}
6060c883cefSAlexander Motin 	error = g_access(cp, acr, acw, ace);
607e770bc6bSMatt Jacob 	if (error) {
608e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: cannot set access in "
6090c883cefSAlexander Motin 		    "attaching %s to %s (%d)\n",
6100c883cefSAlexander Motin 		    pp->name, sc->sc_name, error);
611e770bc6bSMatt Jacob 		g_detach(cp);
612e770bc6bSMatt Jacob 		g_destroy_consumer(cp);
613e770bc6bSMatt Jacob 		return (error);
614e770bc6bSMatt Jacob 	}
615e6afd72bSAlexander Motin 	if (sc->sc_size == 0) {
616e6afd72bSAlexander Motin 		sc->sc_size = pp->mediasize -
6170c883cefSAlexander Motin 		    ((sc->sc_uuid[0] != 0) ? pp->sectorsize : 0);
618e6afd72bSAlexander Motin 		sc->sc_pp->mediasize = sc->sc_size;
6190c883cefSAlexander Motin 		sc->sc_pp->sectorsize = pp->sectorsize;
620e770bc6bSMatt Jacob 	}
621e6afd72bSAlexander Motin 	if (sc->sc_pp->stripesize == 0 && sc->sc_pp->stripeoffset == 0) {
6220c883cefSAlexander Motin 		sc->sc_pp->stripesize = pp->stripesize;
6230c883cefSAlexander Motin 		sc->sc_pp->stripeoffset = pp->stripeoffset;
6240c883cefSAlexander Motin 	}
625f4673017SAlexander Motin 	sc->sc_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
6260c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
6270c883cefSAlexander Motin 	cp->index = 0;
6280c883cefSAlexander Motin 	sc->sc_ndisks++;
6290c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
6300c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s added to %s\n",
6310c883cefSAlexander Motin 	    pp->name, sc->sc_name);
6320c883cefSAlexander Motin 	if (sc->sc_active == NULL) {
6330c883cefSAlexander Motin 		sc->sc_active = cp;
63463297dfdSAlexander Motin 		if (sc->sc_active_active != 1)
6350c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
636e770bc6bSMatt Jacob 			    pp->name, sc->sc_name);
637e770bc6bSMatt Jacob 	}
638e770bc6bSMatt Jacob 	return (0);
639e770bc6bSMatt Jacob }
640e770bc6bSMatt Jacob 
641e770bc6bSMatt Jacob static int
642e770bc6bSMatt Jacob g_multipath_destroy(struct g_geom *gp)
643e770bc6bSMatt Jacob {
6440c883cefSAlexander Motin 	struct g_multipath_softc *sc;
6450c883cefSAlexander Motin 	struct g_consumer *cp, *cp1;
646e770bc6bSMatt Jacob 
647e770bc6bSMatt Jacob 	g_topology_assert();
64812f35a61SPawel Jakub Dawidek 	if (gp->softc == NULL)
649e770bc6bSMatt Jacob 		return (ENXIO);
6500c883cefSAlexander Motin 	sc = gp->softc;
6510c883cefSAlexander Motin 	if (!sc->sc_stopping) {
652e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
6530c883cefSAlexander Motin 		sc->sc_stopping = 1;
6540c883cefSAlexander Motin 	}
6550c883cefSAlexander Motin 	if (sc->sc_opened != 0) {
6560c883cefSAlexander Motin 		g_wither_provider(sc->sc_pp, ENXIO);
6570c883cefSAlexander Motin 		sc->sc_pp = NULL;
6580c883cefSAlexander Motin 		return (EINPROGRESS);
6590c883cefSAlexander Motin 	}
6600c883cefSAlexander Motin 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
6610c883cefSAlexander Motin 		mtx_lock(&sc->sc_mtx);
6620c883cefSAlexander Motin 		if ((cp->index & MP_POSTED) == 0) {
6630c883cefSAlexander Motin 			cp->index |= MP_POSTED;
6640c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
6650c883cefSAlexander Motin 			g_mpd(cp, 0);
6660c883cefSAlexander Motin 			if (cp1 == NULL)
6670c883cefSAlexander Motin 				return(0);	/* Recursion happened. */
6680c883cefSAlexander Motin 		} else
6690c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
6700c883cefSAlexander Motin 	}
6710c883cefSAlexander Motin 	if (!LIST_EMPTY(&gp->consumer))
6720c883cefSAlexander Motin 		return (EINPROGRESS);
6730c883cefSAlexander Motin 	mtx_destroy(&sc->sc_mtx);
674e770bc6bSMatt Jacob 	g_free(gp->softc);
675e770bc6bSMatt Jacob 	gp->softc = NULL;
6760c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s destroyed\n", gp->name);
677e770bc6bSMatt Jacob 	g_wither_geom(gp, ENXIO);
678e770bc6bSMatt Jacob 	return (0);
679e770bc6bSMatt Jacob }
680e770bc6bSMatt Jacob 
681e770bc6bSMatt Jacob static int
682e770bc6bSMatt Jacob g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
683e770bc6bSMatt Jacob     struct g_geom *gp)
684e770bc6bSMatt Jacob {
68512f35a61SPawel Jakub Dawidek 
686e770bc6bSMatt Jacob 	return (g_multipath_destroy(gp));
687e770bc6bSMatt Jacob }
688e770bc6bSMatt Jacob 
689b5dce617SMatt Jacob static int
690b5dce617SMatt Jacob g_multipath_rotate(struct g_geom *gp)
691b5dce617SMatt Jacob {
6928fb378d6SThomas Quinot 	struct g_consumer *lcp, *first_good_cp = NULL;
693b5dce617SMatt Jacob 	struct g_multipath_softc *sc = gp->softc;
6948fb378d6SThomas Quinot 	int active_cp_seen = 0;
695b5dce617SMatt Jacob 
696b5dce617SMatt Jacob 	g_topology_assert();
697b5dce617SMatt Jacob 	if (sc == NULL)
698b5dce617SMatt Jacob 		return (ENXIO);
699b5dce617SMatt Jacob 	LIST_FOREACH(lcp, &gp->consumer, consumer) {
700b5dce617SMatt Jacob 		if ((lcp->index & MP_BAD) == 0) {
7018fb378d6SThomas Quinot 			if (first_good_cp == NULL)
7028fb378d6SThomas Quinot 				first_good_cp = lcp;
7038fb378d6SThomas Quinot 			if (active_cp_seen)
704b5dce617SMatt Jacob 				break;
705b5dce617SMatt Jacob 		}
7068fb378d6SThomas Quinot 		if (sc->sc_active == lcp)
7078fb378d6SThomas Quinot 			active_cp_seen = 1;
708b5dce617SMatt Jacob 	}
7098fb378d6SThomas Quinot 	if (lcp == NULL)
7108fb378d6SThomas Quinot 		lcp = first_good_cp;
7118fb378d6SThomas Quinot 	if (lcp && lcp != sc->sc_active) {
7120c883cefSAlexander Motin 		sc->sc_active = lcp;
71363297dfdSAlexander Motin 		if (sc->sc_active_active != 1)
7140c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
715b5dce617SMatt Jacob 			    lcp->provider->name, sc->sc_name);
716b5dce617SMatt Jacob 	}
717b5dce617SMatt Jacob 	return (0);
718b5dce617SMatt Jacob }
719b5dce617SMatt Jacob 
720e770bc6bSMatt Jacob static void
721e770bc6bSMatt Jacob g_multipath_init(struct g_class *mp)
722e770bc6bSMatt Jacob {
723e770bc6bSMatt Jacob 	bioq_init(&gmtbq);
724e770bc6bSMatt Jacob 	mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
72563297dfdSAlexander Motin 	kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt");
726e770bc6bSMatt Jacob }
727e770bc6bSMatt Jacob 
728e770bc6bSMatt Jacob static void
729e770bc6bSMatt Jacob g_multipath_fini(struct g_class *mp)
730e770bc6bSMatt Jacob {
731e770bc6bSMatt Jacob 	if (g_multipath_kt_state == GKT_RUN) {
732e770bc6bSMatt Jacob 		mtx_lock(&gmtbq_mtx);
733e770bc6bSMatt Jacob 		g_multipath_kt_state = GKT_DIE;
734e770bc6bSMatt Jacob 		wakeup(&g_multipath_kt_state);
735e770bc6bSMatt Jacob 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
736e770bc6bSMatt Jacob 		    "gmp:fini", 0);
737e770bc6bSMatt Jacob 		mtx_unlock(&gmtbq_mtx);
738e770bc6bSMatt Jacob 	}
739e770bc6bSMatt Jacob }
740e770bc6bSMatt Jacob 
741e770bc6bSMatt Jacob static int
742e770bc6bSMatt Jacob g_multipath_read_metadata(struct g_consumer *cp,
743e770bc6bSMatt Jacob     struct g_multipath_metadata *md)
744e770bc6bSMatt Jacob {
745e770bc6bSMatt Jacob 	struct g_provider *pp;
746e770bc6bSMatt Jacob 	u_char *buf;
747e770bc6bSMatt Jacob 	int error;
748e770bc6bSMatt Jacob 
749e770bc6bSMatt Jacob 	g_topology_assert();
750e770bc6bSMatt Jacob 	error = g_access(cp, 1, 0, 0);
75112f35a61SPawel Jakub Dawidek 	if (error != 0)
752e770bc6bSMatt Jacob 		return (error);
753e770bc6bSMatt Jacob 	pp = cp->provider;
754e770bc6bSMatt Jacob 	g_topology_unlock();
755e770bc6bSMatt Jacob 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
756e770bc6bSMatt Jacob 	    pp->sectorsize, &error);
757e770bc6bSMatt Jacob 	g_topology_lock();
758e770bc6bSMatt Jacob 	g_access(cp, -1, 0, 0);
75912f35a61SPawel Jakub Dawidek 	if (buf == NULL)
760e770bc6bSMatt Jacob 		return (error);
761e770bc6bSMatt Jacob 	multipath_metadata_decode(buf, md);
762e770bc6bSMatt Jacob 	g_free(buf);
763e770bc6bSMatt Jacob 	return (0);
764e770bc6bSMatt Jacob }
765e770bc6bSMatt Jacob 
766f8c79813SAlexander Motin static int
767f8c79813SAlexander Motin g_multipath_write_metadata(struct g_consumer *cp,
768f8c79813SAlexander Motin     struct g_multipath_metadata *md)
769f8c79813SAlexander Motin {
770f8c79813SAlexander Motin 	struct g_provider *pp;
771f8c79813SAlexander Motin 	u_char *buf;
772f8c79813SAlexander Motin 	int error;
773f8c79813SAlexander Motin 
774f8c79813SAlexander Motin 	g_topology_assert();
775f8c79813SAlexander Motin 	error = g_access(cp, 1, 1, 1);
776f8c79813SAlexander Motin 	if (error != 0)
777f8c79813SAlexander Motin 		return (error);
778f8c79813SAlexander Motin 	pp = cp->provider;
779f8c79813SAlexander Motin 	g_topology_unlock();
780f8c79813SAlexander Motin 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
781f8c79813SAlexander Motin 	multipath_metadata_encode(md, buf);
782f8c79813SAlexander Motin 	error = g_write_data(cp, pp->mediasize - pp->sectorsize,
783f8c79813SAlexander Motin 	    buf, pp->sectorsize);
784f8c79813SAlexander Motin 	g_topology_lock();
785f8c79813SAlexander Motin 	g_access(cp, -1, -1, -1);
786f8c79813SAlexander Motin 	g_free(buf);
787f8c79813SAlexander Motin 	return (error);
788f8c79813SAlexander Motin }
789f8c79813SAlexander Motin 
790e770bc6bSMatt Jacob static struct g_geom *
791e770bc6bSMatt Jacob g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
792e770bc6bSMatt Jacob {
793e770bc6bSMatt Jacob 	struct g_multipath_metadata md;
794e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
795e770bc6bSMatt Jacob 	struct g_consumer *cp;
796e770bc6bSMatt Jacob 	struct g_geom *gp, *gp1;
797e770bc6bSMatt Jacob 	int error, isnew;
798e770bc6bSMatt Jacob 
799e770bc6bSMatt Jacob 	g_topology_assert();
800e770bc6bSMatt Jacob 
801e770bc6bSMatt Jacob 	gp = g_new_geomf(mp, "multipath:taste");
802e770bc6bSMatt Jacob 	gp->start = g_multipath_start;
803e770bc6bSMatt Jacob 	gp->access = g_multipath_access;
804e770bc6bSMatt Jacob 	gp->orphan = g_multipath_orphan;
805e770bc6bSMatt Jacob 	cp = g_new_consumer(gp);
806e770bc6bSMatt Jacob 	g_attach(cp, pp);
807e770bc6bSMatt Jacob 	error = g_multipath_read_metadata(cp, &md);
808e770bc6bSMatt Jacob 	g_detach(cp);
809e770bc6bSMatt Jacob 	g_destroy_consumer(cp);
810e770bc6bSMatt Jacob 	g_destroy_geom(gp);
81112f35a61SPawel Jakub Dawidek 	if (error != 0)
812e770bc6bSMatt Jacob 		return (NULL);
813e770bc6bSMatt Jacob 	gp = NULL;
814e770bc6bSMatt Jacob 
815e770bc6bSMatt Jacob 	if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
81612f35a61SPawel Jakub Dawidek 		if (g_multipath_debug)
817e770bc6bSMatt Jacob 			printf("%s is not MULTIPATH\n", pp->name);
818e770bc6bSMatt Jacob 		return (NULL);
819e770bc6bSMatt Jacob 	}
820e770bc6bSMatt Jacob 	if (md.md_version != G_MULTIPATH_VERSION) {
821e770bc6bSMatt Jacob 		printf("%s has version %d multipath id- this module is version "
822e770bc6bSMatt Jacob 		    " %d: rejecting\n", pp->name, md.md_version,
823e770bc6bSMatt Jacob 		    G_MULTIPATH_VERSION);
824e770bc6bSMatt Jacob 		return (NULL);
825e770bc6bSMatt Jacob 	}
8260c883cefSAlexander Motin 	if (md.md_size != 0 && md.md_size != pp->mediasize)
8270c883cefSAlexander Motin 		return (NULL);
8280c883cefSAlexander Motin 	if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize)
8290c883cefSAlexander Motin 		return (NULL);
83012f35a61SPawel Jakub Dawidek 	if (g_multipath_debug)
831e770bc6bSMatt Jacob 		printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
832e770bc6bSMatt Jacob 
833e770bc6bSMatt Jacob 	/*
834e770bc6bSMatt Jacob 	 * Let's check if such a device already is present. We check against
835e770bc6bSMatt Jacob 	 * uuid alone first because that's the true distinguishor. If that
836e770bc6bSMatt Jacob 	 * passes, then we check for name conflicts. If there are conflicts,
837e770bc6bSMatt Jacob 	 * modify the name.
838e770bc6bSMatt Jacob 	 *
839e770bc6bSMatt Jacob 	 * The whole purpose of this is to solve the problem that people don't
840e770bc6bSMatt Jacob 	 * pick good unique names, but good unique names (like uuids) are a
841e770bc6bSMatt Jacob 	 * pain to use. So, we allow people to build GEOMs with friendly names
842e770bc6bSMatt Jacob 	 * and uuids, and modify the names in case there's a collision.
843e770bc6bSMatt Jacob 	 */
844e770bc6bSMatt Jacob 	sc = NULL;
845e770bc6bSMatt Jacob 	LIST_FOREACH(gp, &mp->geom, geom) {
846e770bc6bSMatt Jacob 		sc = gp->softc;
8470c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
848e770bc6bSMatt Jacob 			continue;
84912f35a61SPawel Jakub Dawidek 		if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
850e770bc6bSMatt Jacob 			break;
851e770bc6bSMatt Jacob 	}
852e770bc6bSMatt Jacob 
853e770bc6bSMatt Jacob 	LIST_FOREACH(gp1, &mp->geom, geom) {
85412f35a61SPawel Jakub Dawidek 		if (gp1 == gp)
855e770bc6bSMatt Jacob 			continue;
856e770bc6bSMatt Jacob 		sc = gp1->softc;
8570c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
858e770bc6bSMatt Jacob 			continue;
85912f35a61SPawel Jakub Dawidek 		if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
860e770bc6bSMatt Jacob 			break;
861e770bc6bSMatt Jacob 	}
862e770bc6bSMatt Jacob 
863e770bc6bSMatt Jacob 	/*
864e770bc6bSMatt Jacob 	 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
865e770bc6bSMatt Jacob 	 *
866e770bc6bSMatt Jacob 	 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
867e770bc6bSMatt Jacob 	 * with the same name (but a different UUID).
868e770bc6bSMatt Jacob 	 *
869e770bc6bSMatt Jacob 	 * If gp is NULL, then modify the name with a random number and
870e770bc6bSMatt Jacob   	 * complain, but allow the creation of the geom to continue.
871e770bc6bSMatt Jacob 	 *
872e770bc6bSMatt Jacob 	 * If gp is *not* NULL, just use the geom's name as we're attaching
873e770bc6bSMatt Jacob 	 * this disk to the (previously generated) name.
874e770bc6bSMatt Jacob 	 */
875e770bc6bSMatt Jacob 
876e770bc6bSMatt Jacob 	if (gp1) {
877e770bc6bSMatt Jacob 		sc = gp1->softc;
878e770bc6bSMatt Jacob 		if (gp == NULL) {
879e770bc6bSMatt Jacob 			char buf[16];
880e770bc6bSMatt Jacob 			u_long rand = random();
881e770bc6bSMatt Jacob 
882e770bc6bSMatt Jacob 			snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
883e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
884e770bc6bSMatt Jacob 			    sc->sc_name, sc->sc_uuid);
885e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
886e770bc6bSMatt Jacob 			    md.md_uuid, buf);
887e770bc6bSMatt Jacob 			strlcpy(md.md_name, buf, sizeof(md.md_name));
888e770bc6bSMatt Jacob 		} else {
889e770bc6bSMatt Jacob 			strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
890e770bc6bSMatt Jacob 		}
891e770bc6bSMatt Jacob 	}
892e770bc6bSMatt Jacob 
893e770bc6bSMatt Jacob 	if (gp == NULL) {
894e770bc6bSMatt Jacob 		gp = g_multipath_create(mp, &md);
895e770bc6bSMatt Jacob 		if (gp == NULL) {
896e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
897e770bc6bSMatt Jacob 			    md.md_name, md.md_uuid);
898e770bc6bSMatt Jacob 			return (NULL);
899e770bc6bSMatt Jacob 		}
900e770bc6bSMatt Jacob 		isnew = 1;
901e770bc6bSMatt Jacob 	} else {
902e770bc6bSMatt Jacob 		isnew = 0;
903e770bc6bSMatt Jacob 	}
904e770bc6bSMatt Jacob 
905e770bc6bSMatt Jacob 	sc = gp->softc;
906e770bc6bSMatt Jacob 	KASSERT(sc != NULL, ("sc is NULL"));
907e770bc6bSMatt Jacob 	error = g_multipath_add_disk(gp, pp);
908e770bc6bSMatt Jacob 	if (error != 0) {
90912f35a61SPawel Jakub Dawidek 		if (isnew)
910e770bc6bSMatt Jacob 			g_multipath_destroy(gp);
911e770bc6bSMatt Jacob 		return (NULL);
912e770bc6bSMatt Jacob 	}
913e770bc6bSMatt Jacob 	return (gp);
914e770bc6bSMatt Jacob }
915e770bc6bSMatt Jacob 
916e770bc6bSMatt Jacob static void
9170c883cefSAlexander Motin g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp,
9180c883cefSAlexander Motin     const char *name)
919e770bc6bSMatt Jacob {
9200c883cefSAlexander Motin 	struct g_multipath_softc *sc;
921e770bc6bSMatt Jacob 	struct g_geom *gp;
9222b4969ffSMatt Jacob 	struct g_consumer *cp;
9230c883cefSAlexander Motin 	struct g_provider *pp;
9240c883cefSAlexander Motin 	const char *mpname;
925e770bc6bSMatt Jacob 	static const char devpf[6] = "/dev/";
926*d3fef0a0SAlexander Motin 	int error;
927e770bc6bSMatt Jacob 
928e770bc6bSMatt Jacob 	g_topology_assert();
929e770bc6bSMatt Jacob 
930e770bc6bSMatt Jacob 	mpname = gctl_get_asciiparam(req, "arg0");
931e770bc6bSMatt Jacob         if (mpname == NULL) {
932e770bc6bSMatt Jacob                 gctl_error(req, "No 'arg0' argument");
933e770bc6bSMatt Jacob                 return;
934e770bc6bSMatt Jacob         }
9352b4969ffSMatt Jacob 	gp = g_multipath_find_geom(mp, mpname);
9362b4969ffSMatt Jacob 	if (gp == NULL) {
9372b4969ffSMatt Jacob 		gctl_error(req, "Device %s is invalid", mpname);
938e770bc6bSMatt Jacob 		return;
939e770bc6bSMatt Jacob 	}
9400c883cefSAlexander Motin 	sc = gp->softc;
941e770bc6bSMatt Jacob 
94212f35a61SPawel Jakub Dawidek 	if (strncmp(name, devpf, 5) == 0)
943e770bc6bSMatt Jacob 		name += 5;
9442b4969ffSMatt Jacob 	pp = g_provider_by_name(name);
9452b4969ffSMatt Jacob 	if (pp == NULL) {
946e770bc6bSMatt Jacob 		gctl_error(req, "Provider %s is invalid", name);
947e770bc6bSMatt Jacob 		return;
948e770bc6bSMatt Jacob 	}
949e770bc6bSMatt Jacob 
950e770bc6bSMatt Jacob 	/*
9510c883cefSAlexander Motin 	 * Check to make sure parameters match.
952e770bc6bSMatt Jacob 	 */
9530c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
9540c883cefSAlexander Motin 		if (cp->provider == pp) {
9550c883cefSAlexander Motin 			gctl_error(req, "provider %s is already there",
9560c883cefSAlexander Motin 			    pp->name);
957e770bc6bSMatt Jacob 			return;
958e770bc6bSMatt Jacob 		}
9590c883cefSAlexander Motin 	}
960e6afd72bSAlexander Motin 	if (sc->sc_pp->mediasize != 0 &&
9610c883cefSAlexander Motin 	    sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0)
9620c883cefSAlexander Motin 	     != pp->mediasize) {
9630c883cefSAlexander Motin 		gctl_error(req, "Providers size mismatch %jd != %jd",
9640c883cefSAlexander Motin 		    (intmax_t) sc->sc_pp->mediasize +
9650c883cefSAlexander Motin 			(sc->sc_uuid[0] != 0 ? pp->sectorsize : 0),
9660c883cefSAlexander Motin 		    (intmax_t) pp->mediasize);
967e770bc6bSMatt Jacob 		return;
968e770bc6bSMatt Jacob 	}
969e6afd72bSAlexander Motin 	if (sc->sc_pp->sectorsize != 0 &&
9700c883cefSAlexander Motin 	    sc->sc_pp->sectorsize != pp->sectorsize) {
9710c883cefSAlexander Motin 		gctl_error(req, "Providers sectorsize mismatch %u != %u",
9720c883cefSAlexander Motin 		    sc->sc_pp->sectorsize, pp->sectorsize);
973e770bc6bSMatt Jacob 		return;
974e770bc6bSMatt Jacob 	}
975e770bc6bSMatt Jacob 
976*d3fef0a0SAlexander Motin 	error = g_multipath_add_disk(gp, pp);
977*d3fef0a0SAlexander Motin 	if (error != 0)
978*d3fef0a0SAlexander Motin 		gctl_error(req, "Provider addition error: %d", error);
979e770bc6bSMatt Jacob }
980e770bc6bSMatt Jacob 
9810c883cefSAlexander Motin static void
98271ee4ef0SThomas Quinot g_multipath_ctl_prefer(struct gctl_req *req, struct g_class *mp)
98371ee4ef0SThomas Quinot {
98471ee4ef0SThomas Quinot 	struct g_geom *gp;
98571ee4ef0SThomas Quinot 	struct g_multipath_softc *sc;
98671ee4ef0SThomas Quinot 	struct g_consumer *cp;
98771ee4ef0SThomas Quinot 	const char *name, *mpname;
98871ee4ef0SThomas Quinot 	static const char devpf[6] = "/dev/";
98971ee4ef0SThomas Quinot 	int *nargs;
99071ee4ef0SThomas Quinot 
99171ee4ef0SThomas Quinot 	g_topology_assert();
99271ee4ef0SThomas Quinot 
99371ee4ef0SThomas Quinot 	mpname = gctl_get_asciiparam(req, "arg0");
99471ee4ef0SThomas Quinot         if (mpname == NULL) {
99571ee4ef0SThomas Quinot                 gctl_error(req, "No 'arg0' argument");
99671ee4ef0SThomas Quinot                 return;
99771ee4ef0SThomas Quinot         }
99871ee4ef0SThomas Quinot 	gp = g_multipath_find_geom(mp, mpname);
99971ee4ef0SThomas Quinot 	if (gp == NULL) {
100071ee4ef0SThomas Quinot 		gctl_error(req, "Device %s is invalid", mpname);
100171ee4ef0SThomas Quinot 		return;
100271ee4ef0SThomas Quinot 	}
100371ee4ef0SThomas Quinot 	sc = gp->softc;
100471ee4ef0SThomas Quinot 
100571ee4ef0SThomas Quinot 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
100671ee4ef0SThomas Quinot 	if (nargs == NULL) {
100771ee4ef0SThomas Quinot 		gctl_error(req, "No 'nargs' argument");
100871ee4ef0SThomas Quinot 		return;
100971ee4ef0SThomas Quinot 	}
101071ee4ef0SThomas Quinot 	if (*nargs != 2) {
101171ee4ef0SThomas Quinot 		gctl_error(req, "missing device");
101271ee4ef0SThomas Quinot 		return;
101371ee4ef0SThomas Quinot 	}
101471ee4ef0SThomas Quinot 
101571ee4ef0SThomas Quinot 	name = gctl_get_asciiparam(req, "arg1");
101671ee4ef0SThomas Quinot 	if (name == NULL) {
101771ee4ef0SThomas Quinot 		gctl_error(req, "No 'arg1' argument");
101871ee4ef0SThomas Quinot 		return;
101971ee4ef0SThomas Quinot 	}
102071ee4ef0SThomas Quinot 	if (strncmp(name, devpf, 5) == 0) {
102171ee4ef0SThomas Quinot 		name += 5;
102271ee4ef0SThomas Quinot 	}
102371ee4ef0SThomas Quinot 
102471ee4ef0SThomas Quinot 	LIST_FOREACH(cp, &gp->consumer, consumer) {
102571ee4ef0SThomas Quinot 		if (cp->provider != NULL
102671ee4ef0SThomas Quinot                       && strcmp(cp->provider->name, name) == 0)
102771ee4ef0SThomas Quinot 		    break;
102871ee4ef0SThomas Quinot 	}
102971ee4ef0SThomas Quinot 
103071ee4ef0SThomas Quinot 	if (cp == NULL) {
103171ee4ef0SThomas Quinot 		gctl_error(req, "Provider %s not found", name);
103271ee4ef0SThomas Quinot 		return;
103371ee4ef0SThomas Quinot 	}
103471ee4ef0SThomas Quinot 
103571ee4ef0SThomas Quinot 	mtx_lock(&sc->sc_mtx);
103671ee4ef0SThomas Quinot 
103771ee4ef0SThomas Quinot 	if (cp->index & MP_BAD) {
103871ee4ef0SThomas Quinot 		gctl_error(req, "Consumer %s is invalid", name);
103971ee4ef0SThomas Quinot 		mtx_unlock(&sc->sc_mtx);
104071ee4ef0SThomas Quinot 		return;
104171ee4ef0SThomas Quinot 	}
104271ee4ef0SThomas Quinot 
104371ee4ef0SThomas Quinot 	/* Here when the consumer is present and in good shape */
104471ee4ef0SThomas Quinot 
104571ee4ef0SThomas Quinot 	sc->sc_active = cp;
104671ee4ef0SThomas Quinot 	if (!sc->sc_active_active)
104771ee4ef0SThomas Quinot 	    printf("GEOM_MULTIPATH: %s now active path in %s\n",
104871ee4ef0SThomas Quinot 		sc->sc_active->provider->name, sc->sc_name);
104971ee4ef0SThomas Quinot 
105071ee4ef0SThomas Quinot 	mtx_unlock(&sc->sc_mtx);
105171ee4ef0SThomas Quinot }
105271ee4ef0SThomas Quinot 
105371ee4ef0SThomas Quinot static void
10540c883cefSAlexander Motin g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp)
10550c883cefSAlexander Motin {
10560c883cefSAlexander Motin 	struct g_multipath_softc *sc;
10570c883cefSAlexander Motin 	struct g_geom *gp;
10580c883cefSAlexander Motin 	const char *mpname, *name;
10590c883cefSAlexander Motin 
10600c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
10610c883cefSAlexander Motin         if (mpname == NULL) {
10620c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
10630c883cefSAlexander Motin                 return;
10640c883cefSAlexander Motin         }
10650c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
10660c883cefSAlexander Motin 	if (gp == NULL) {
10670c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
10680c883cefSAlexander Motin 		return;
10690c883cefSAlexander Motin 	}
10700c883cefSAlexander Motin 	sc = gp->softc;
10710c883cefSAlexander Motin 
10720c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
10730c883cefSAlexander Motin 	if (name == NULL) {
10740c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
10750c883cefSAlexander Motin 		return;
10760c883cefSAlexander Motin 	}
10770c883cefSAlexander Motin 	g_multipath_ctl_add_name(req, mp, name);
10780c883cefSAlexander Motin }
10790c883cefSAlexander Motin 
10800c883cefSAlexander Motin static void
10810c883cefSAlexander Motin g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp)
10820c883cefSAlexander Motin {
10830c883cefSAlexander Motin 	struct g_multipath_metadata md;
10840c883cefSAlexander Motin 	struct g_multipath_softc *sc;
10850c883cefSAlexander Motin 	struct g_geom *gp;
10860c883cefSAlexander Motin 	const char *mpname, *name;
10870c883cefSAlexander Motin 	char param[16];
108863297dfdSAlexander Motin 	int *nargs, i, *val;
10890c883cefSAlexander Motin 
10900c883cefSAlexander Motin 	g_topology_assert();
10910c883cefSAlexander Motin 
10920c883cefSAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
10930c883cefSAlexander Motin 	if (*nargs < 2) {
10940c883cefSAlexander Motin 		gctl_error(req, "wrong number of arguments.");
10950c883cefSAlexander Motin 		return;
10960c883cefSAlexander Motin 	}
10970c883cefSAlexander Motin 
10980c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
10990c883cefSAlexander Motin         if (mpname == NULL) {
11000c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
11010c883cefSAlexander Motin                 return;
11020c883cefSAlexander Motin         }
11030c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
11040c883cefSAlexander Motin 	if (gp != NULL) {
11050c883cefSAlexander Motin 		gctl_error(req, "Device %s already exist", mpname);
11060c883cefSAlexander Motin 		return;
11070c883cefSAlexander Motin 	}
11080c883cefSAlexander Motin 
11090c883cefSAlexander Motin 	memset(&md, 0, sizeof(md));
11100c883cefSAlexander Motin 	strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
11110c883cefSAlexander Motin 	md.md_version = G_MULTIPATH_VERSION;
11120c883cefSAlexander Motin 	strlcpy(md.md_name, mpname, sizeof(md.md_name));
11130c883cefSAlexander Motin 	md.md_size = 0;
11140c883cefSAlexander Motin 	md.md_sectorsize = 0;
11150c883cefSAlexander Motin 	md.md_uuid[0] = 0;
111663297dfdSAlexander Motin 	md.md_active_active = 0;
111763297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_active", sizeof(*val));
111863297dfdSAlexander Motin 	if (val != NULL && *val != 0)
111963297dfdSAlexander Motin 		md.md_active_active = 1;
112063297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_read", sizeof(*val));
112163297dfdSAlexander Motin 	if (val != NULL && *val != 0)
112263297dfdSAlexander Motin 		md.md_active_active = 2;
11230c883cefSAlexander Motin 	gp = g_multipath_create(mp, &md);
11240c883cefSAlexander Motin 	if (gp == NULL) {
11250c883cefSAlexander Motin 		gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n",
11260c883cefSAlexander Motin 		    md.md_name, md.md_uuid);
11270c883cefSAlexander Motin 		return;
11280c883cefSAlexander Motin 	}
11290c883cefSAlexander Motin 	sc = gp->softc;
11300c883cefSAlexander Motin 
11310c883cefSAlexander Motin 	for (i = 1; i < *nargs; i++) {
11320c883cefSAlexander Motin 		snprintf(param, sizeof(param), "arg%d", i);
11330c883cefSAlexander Motin 		name = gctl_get_asciiparam(req, param);
11340c883cefSAlexander Motin 		g_multipath_ctl_add_name(req, mp, name);
11350c883cefSAlexander Motin 	}
11360c883cefSAlexander Motin 
11370c883cefSAlexander Motin 	if (sc->sc_ndisks != (*nargs - 1))
11380c883cefSAlexander Motin 		g_multipath_destroy(gp);
11390c883cefSAlexander Motin }
11400c883cefSAlexander Motin 
11410c883cefSAlexander Motin static void
114263297dfdSAlexander Motin g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp)
114363297dfdSAlexander Motin {
114463297dfdSAlexander Motin 	struct g_multipath_softc *sc;
114563297dfdSAlexander Motin 	struct g_geom *gp;
114663297dfdSAlexander Motin 	struct g_consumer *cp;
114763297dfdSAlexander Motin 	struct g_provider *pp;
1148c0b1ef66SAlexander Motin 	struct g_multipath_metadata md;
114963297dfdSAlexander Motin 	const char *name;
115063297dfdSAlexander Motin 	int error, *val;
115163297dfdSAlexander Motin 
115263297dfdSAlexander Motin 	g_topology_assert();
115363297dfdSAlexander Motin 
115463297dfdSAlexander Motin 	name = gctl_get_asciiparam(req, "arg0");
115563297dfdSAlexander Motin 	if (name == NULL) {
115663297dfdSAlexander Motin 		gctl_error(req, "No 'arg0' argument");
115763297dfdSAlexander Motin 		return;
115863297dfdSAlexander Motin 	}
115963297dfdSAlexander Motin 	gp = g_multipath_find_geom(mp, name);
116063297dfdSAlexander Motin 	if (gp == NULL) {
116163297dfdSAlexander Motin 		gctl_error(req, "Device %s is invalid", name);
116263297dfdSAlexander Motin 		return;
116363297dfdSAlexander Motin 	}
116463297dfdSAlexander Motin 	sc = gp->softc;
116563297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_active", sizeof(*val));
116663297dfdSAlexander Motin 	if (val != NULL && *val != 0)
116763297dfdSAlexander Motin 		sc->sc_active_active = 1;
116863297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_read", sizeof(*val));
116963297dfdSAlexander Motin 	if (val != NULL && *val != 0)
117063297dfdSAlexander Motin 		sc->sc_active_active = 2;
117163297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_passive", sizeof(*val));
117263297dfdSAlexander Motin 	if (val != NULL && *val != 0)
117363297dfdSAlexander Motin 		sc->sc_active_active = 0;
117463297dfdSAlexander Motin 	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
117563297dfdSAlexander Motin 		cp = sc->sc_active;
117663297dfdSAlexander Motin 		pp = cp->provider;
1177c0b1ef66SAlexander Motin 		strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1178c0b1ef66SAlexander Motin 		memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
1179c0b1ef66SAlexander Motin 		strlcpy(md.md_name, name, sizeof(md.md_name));
1180c0b1ef66SAlexander Motin 		md.md_version = G_MULTIPATH_VERSION;
1181c0b1ef66SAlexander Motin 		md.md_size = pp->mediasize;
1182c0b1ef66SAlexander Motin 		md.md_sectorsize = pp->sectorsize;
1183c0b1ef66SAlexander Motin 		md.md_active_active = sc->sc_active_active;
1184f8c79813SAlexander Motin 		error = g_multipath_write_metadata(cp, &md);
118563297dfdSAlexander Motin 		if (error != 0)
118663297dfdSAlexander Motin 			gctl_error(req, "Can't update metadata on %s (%d)",
118763297dfdSAlexander Motin 			    pp->name, error);
118863297dfdSAlexander Motin 	}
118963297dfdSAlexander Motin }
119063297dfdSAlexander Motin 
119163297dfdSAlexander Motin static void
11920c883cefSAlexander Motin g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail)
11930c883cefSAlexander Motin {
11940c883cefSAlexander Motin 	struct g_multipath_softc *sc;
11950c883cefSAlexander Motin 	struct g_geom *gp;
11960c883cefSAlexander Motin 	struct g_consumer *cp;
11970c883cefSAlexander Motin 	const char *mpname, *name;
11980c883cefSAlexander Motin 	int found;
11990c883cefSAlexander Motin 
12000c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
12010c883cefSAlexander Motin         if (mpname == NULL) {
12020c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
12030c883cefSAlexander Motin                 return;
12040c883cefSAlexander Motin         }
12050c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
12060c883cefSAlexander Motin 	if (gp == NULL) {
12070c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
12080c883cefSAlexander Motin 		return;
12090c883cefSAlexander Motin 	}
12100c883cefSAlexander Motin 	sc = gp->softc;
12110c883cefSAlexander Motin 
12120c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
12130c883cefSAlexander Motin 	if (name == NULL) {
12140c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
12150c883cefSAlexander Motin 		return;
12160c883cefSAlexander Motin 	}
12170c883cefSAlexander Motin 
12180c883cefSAlexander Motin 	found = 0;
12190c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
12200c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
12210c883cefSAlexander Motin 		if (cp->provider != NULL &&
12220c883cefSAlexander Motin 		    strcmp(cp->provider->name, name) == 0 &&
12230c883cefSAlexander Motin 		    (cp->index & MP_LOST) == 0) {
12240c883cefSAlexander Motin 			found = 1;
122563297dfdSAlexander Motin 			if (!fail == !(cp->index & MP_FAIL))
122663297dfdSAlexander Motin 				continue;
12270c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: %s in %s is marked %s.\n",
12280c883cefSAlexander Motin 				name, sc->sc_name, fail ? "FAIL" : "OK");
12290c883cefSAlexander Motin 			if (fail) {
12300c883cefSAlexander Motin 				g_multipath_fault(cp, MP_FAIL);
12310c883cefSAlexander Motin 			} else {
12320c883cefSAlexander Motin 				cp->index &= ~MP_FAIL;
12330c883cefSAlexander Motin 			}
12340c883cefSAlexander Motin 		}
12350c883cefSAlexander Motin 	}
12360c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
12370c883cefSAlexander Motin 	if (found == 0)
12380c883cefSAlexander Motin 		gctl_error(req, "Provider %s not found", name);
12390c883cefSAlexander Motin }
12400c883cefSAlexander Motin 
12410c883cefSAlexander Motin static void
12420c883cefSAlexander Motin g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp)
12430c883cefSAlexander Motin {
12440c883cefSAlexander Motin 	struct g_multipath_softc *sc;
12450c883cefSAlexander Motin 	struct g_geom *gp;
12460c883cefSAlexander Motin 	struct g_consumer *cp, *cp1;
12470c883cefSAlexander Motin 	const char *mpname, *name;
12480c883cefSAlexander Motin 	uintptr_t *cnt;
12490c883cefSAlexander Motin 	int found;
12500c883cefSAlexander Motin 
12510c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
12520c883cefSAlexander Motin         if (mpname == NULL) {
12530c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
12540c883cefSAlexander Motin                 return;
12550c883cefSAlexander Motin         }
12560c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
12570c883cefSAlexander Motin 	if (gp == NULL) {
12580c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
12590c883cefSAlexander Motin 		return;
12600c883cefSAlexander Motin 	}
12610c883cefSAlexander Motin 	sc = gp->softc;
12620c883cefSAlexander Motin 
12630c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
12640c883cefSAlexander Motin 	if (name == NULL) {
12650c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
12660c883cefSAlexander Motin 		return;
12670c883cefSAlexander Motin 	}
12680c883cefSAlexander Motin 
12690c883cefSAlexander Motin 	found = 0;
12700c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
12710c883cefSAlexander Motin 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
12720c883cefSAlexander Motin 		if (cp->provider != NULL &&
12730c883cefSAlexander Motin 		    strcmp(cp->provider->name, name) == 0 &&
12740c883cefSAlexander Motin 		    (cp->index & MP_LOST) == 0) {
12750c883cefSAlexander Motin 			found = 1;
12760c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: removing %s from %s\n",
12770c883cefSAlexander Motin 			    cp->provider->name, cp->geom->name);
12780c883cefSAlexander Motin 			sc->sc_ndisks--;
12790c883cefSAlexander Motin 			g_multipath_fault(cp, MP_LOST);
12800c883cefSAlexander Motin 			cnt = (uintptr_t *)&cp->private;
12810c883cefSAlexander Motin 			if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
12820c883cefSAlexander Motin 				cp->index |= MP_POSTED;
12830c883cefSAlexander Motin 				mtx_unlock(&sc->sc_mtx);
12840c883cefSAlexander Motin 				g_mpd(cp, 0);
12850c883cefSAlexander Motin 				if (cp1 == NULL)
12860c883cefSAlexander Motin 					return;	/* Recursion happened. */
12870c883cefSAlexander Motin 				mtx_lock(&sc->sc_mtx);
12880c883cefSAlexander Motin 			}
12890c883cefSAlexander Motin 		}
12900c883cefSAlexander Motin 	}
12910c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
12920c883cefSAlexander Motin 	if (found == 0)
12930c883cefSAlexander Motin 		gctl_error(req, "Provider %s not found", name);
12940c883cefSAlexander Motin }
12950c883cefSAlexander Motin 
1296e770bc6bSMatt Jacob static struct g_geom *
1297e770bc6bSMatt Jacob g_multipath_find_geom(struct g_class *mp, const char *name)
1298e770bc6bSMatt Jacob {
1299e770bc6bSMatt Jacob 	struct g_geom *gp;
13000c883cefSAlexander Motin 	struct g_multipath_softc *sc;
1301e770bc6bSMatt Jacob 
1302e770bc6bSMatt Jacob 	LIST_FOREACH(gp, &mp->geom, geom) {
13030c883cefSAlexander Motin 		sc = gp->softc;
13040c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
13050c883cefSAlexander Motin 			continue;
13060c883cefSAlexander Motin 		if (strcmp(gp->name, name) == 0)
1307e770bc6bSMatt Jacob 			return (gp);
1308e770bc6bSMatt Jacob 	}
1309e770bc6bSMatt Jacob 	return (NULL);
1310e770bc6bSMatt Jacob }
1311e770bc6bSMatt Jacob 
1312e770bc6bSMatt Jacob static void
13130c883cefSAlexander Motin g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp)
1314e770bc6bSMatt Jacob {
1315e770bc6bSMatt Jacob 	struct g_geom *gp;
1316e770bc6bSMatt Jacob 	const char *name;
1317e770bc6bSMatt Jacob 	int error;
1318e770bc6bSMatt Jacob 
1319e770bc6bSMatt Jacob 	g_topology_assert();
1320e770bc6bSMatt Jacob 
1321e770bc6bSMatt Jacob 	name = gctl_get_asciiparam(req, "arg0");
1322e770bc6bSMatt Jacob         if (name == NULL) {
1323e770bc6bSMatt Jacob                 gctl_error(req, "No 'arg0' argument");
1324e770bc6bSMatt Jacob                 return;
1325e770bc6bSMatt Jacob         }
1326e770bc6bSMatt Jacob 	gp = g_multipath_find_geom(mp, name);
1327e770bc6bSMatt Jacob 	if (gp == NULL) {
1328e770bc6bSMatt Jacob 		gctl_error(req, "Device %s is invalid", name);
1329e770bc6bSMatt Jacob 		return;
1330e770bc6bSMatt Jacob 	}
1331e770bc6bSMatt Jacob 	error = g_multipath_destroy(gp);
13320c883cefSAlexander Motin 	if (error != 0 && error != EINPROGRESS)
13330c883cefSAlexander Motin 		gctl_error(req, "failed to stop %s (err=%d)", name, error);
1334e770bc6bSMatt Jacob }
13350c883cefSAlexander Motin 
13360c883cefSAlexander Motin static void
13370c883cefSAlexander Motin g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
13380c883cefSAlexander Motin {
13390c883cefSAlexander Motin 	struct g_geom *gp;
13400c883cefSAlexander Motin 	struct g_multipath_softc *sc;
13410c883cefSAlexander Motin 	struct g_consumer *cp;
13420c883cefSAlexander Motin 	struct g_provider *pp;
13430c883cefSAlexander Motin 	const char *name;
13440c883cefSAlexander Motin 	uint8_t *buf;
13450c883cefSAlexander Motin 	int error;
13460c883cefSAlexander Motin 
13470c883cefSAlexander Motin 	g_topology_assert();
13480c883cefSAlexander Motin 
13490c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg0");
13500c883cefSAlexander Motin         if (name == NULL) {
13510c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
13520c883cefSAlexander Motin                 return;
13530c883cefSAlexander Motin         }
13540c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, name);
13550c883cefSAlexander Motin 	if (gp == NULL) {
13560c883cefSAlexander Motin 		gctl_error(req, "Device %s is invalid", name);
13570c883cefSAlexander Motin 		return;
13580c883cefSAlexander Motin 	}
13590c883cefSAlexander Motin 	sc = gp->softc;
13600c883cefSAlexander Motin 
13610c883cefSAlexander Motin 	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
13620c883cefSAlexander Motin 		cp = sc->sc_active;
13630c883cefSAlexander Motin 		pp = cp->provider;
13640c883cefSAlexander Motin 		error = g_access(cp, 1, 1, 1);
13650c883cefSAlexander Motin 		if (error != 0) {
13660c883cefSAlexander Motin 			gctl_error(req, "Can't open %s (%d)", pp->name, error);
13670c883cefSAlexander Motin 			goto destroy;
13680c883cefSAlexander Motin 		}
13690c883cefSAlexander Motin 		g_topology_unlock();
13700c883cefSAlexander Motin 		buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
13710c883cefSAlexander Motin 		error = g_write_data(cp, pp->mediasize - pp->sectorsize,
13720c883cefSAlexander Motin 		    buf, pp->sectorsize);
13730c883cefSAlexander Motin 		g_topology_lock();
13740c883cefSAlexander Motin 		g_access(cp, -1, -1, -1);
13750c883cefSAlexander Motin 		if (error != 0)
13760c883cefSAlexander Motin 			gctl_error(req, "Can't erase metadata on %s (%d)",
13770c883cefSAlexander Motin 			    pp->name, error);
13780c883cefSAlexander Motin 	}
13790c883cefSAlexander Motin 
13800c883cefSAlexander Motin destroy:
13810c883cefSAlexander Motin 	error = g_multipath_destroy(gp);
13820c883cefSAlexander Motin 	if (error != 0 && error != EINPROGRESS)
13830c883cefSAlexander Motin 		gctl_error(req, "failed to destroy %s (err=%d)", name, error);
1384e770bc6bSMatt Jacob }
1385e770bc6bSMatt Jacob 
1386e770bc6bSMatt Jacob static void
1387b5dce617SMatt Jacob g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp)
1388b5dce617SMatt Jacob {
1389b5dce617SMatt Jacob 	struct g_geom *gp;
1390b5dce617SMatt Jacob 	const char *name;
1391b5dce617SMatt Jacob 	int error;
1392b5dce617SMatt Jacob 
1393b5dce617SMatt Jacob 	g_topology_assert();
1394b5dce617SMatt Jacob 
1395b5dce617SMatt Jacob 	name = gctl_get_asciiparam(req, "arg0");
1396b5dce617SMatt Jacob         if (name == NULL) {
1397b5dce617SMatt Jacob                 gctl_error(req, "No 'arg0' argument");
1398b5dce617SMatt Jacob                 return;
1399b5dce617SMatt Jacob         }
1400b5dce617SMatt Jacob 	gp = g_multipath_find_geom(mp, name);
1401b5dce617SMatt Jacob 	if (gp == NULL) {
1402b5dce617SMatt Jacob 		gctl_error(req, "Device %s is invalid", name);
1403b5dce617SMatt Jacob 		return;
1404b5dce617SMatt Jacob 	}
1405b5dce617SMatt Jacob 	error = g_multipath_rotate(gp);
1406b5dce617SMatt Jacob 	if (error != 0) {
1407b5dce617SMatt Jacob 		gctl_error(req, "failed to rotate %s (err=%d)", name, error);
1408b5dce617SMatt Jacob 	}
1409b5dce617SMatt Jacob }
1410b5dce617SMatt Jacob 
1411b5dce617SMatt Jacob static void
1412b5dce617SMatt Jacob g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp)
1413b5dce617SMatt Jacob {
1414b5dce617SMatt Jacob 	struct sbuf *sb;
1415b5dce617SMatt Jacob 	struct g_geom *gp;
1416b5dce617SMatt Jacob 	struct g_multipath_softc *sc;
14170c883cefSAlexander Motin 	struct g_consumer *cp;
1418b5dce617SMatt Jacob 	const char *name;
14190c883cefSAlexander Motin 	int empty;
1420b5dce617SMatt Jacob 
1421b5dce617SMatt Jacob 	sb = sbuf_new_auto();
1422b5dce617SMatt Jacob 
1423b5dce617SMatt Jacob 	g_topology_assert();
1424b5dce617SMatt Jacob 	name = gctl_get_asciiparam(req, "arg0");
1425b5dce617SMatt Jacob         if (name == NULL) {
1426b5dce617SMatt Jacob                 gctl_error(req, "No 'arg0' argument");
1427b5dce617SMatt Jacob                 return;
1428b5dce617SMatt Jacob         }
1429b5dce617SMatt Jacob 	gp = g_multipath_find_geom(mp, name);
1430b5dce617SMatt Jacob 	if (gp == NULL) {
1431b5dce617SMatt Jacob 		gctl_error(req, "Device %s is invalid", name);
1432b5dce617SMatt Jacob 		return;
1433b5dce617SMatt Jacob 	}
1434b5dce617SMatt Jacob 	sc = gp->softc;
143563297dfdSAlexander Motin 	if (sc->sc_active_active == 1) {
14360c883cefSAlexander Motin 		empty = 1;
14370c883cefSAlexander Motin 		LIST_FOREACH(cp, &gp->consumer, consumer) {
14380c883cefSAlexander Motin 			if (cp->index & MP_BAD)
14390c883cefSAlexander Motin 				continue;
14400c883cefSAlexander Motin 			if (!empty)
14410c883cefSAlexander Motin 				sbuf_cat(sb, " ");
14420c883cefSAlexander Motin 			sbuf_cat(sb, cp->provider->name);
14430c883cefSAlexander Motin 			empty = 0;
14440c883cefSAlexander Motin 		}
14450c883cefSAlexander Motin 		if (empty)
14460c883cefSAlexander Motin 			sbuf_cat(sb, "none");
14470c883cefSAlexander Motin 		sbuf_cat(sb, "\n");
14480c883cefSAlexander Motin 	} else if (sc->sc_active && sc->sc_active->provider) {
14490c883cefSAlexander Motin 		sbuf_printf(sb, "%s\n", sc->sc_active->provider->name);
1450b5dce617SMatt Jacob 	} else {
1451b5dce617SMatt Jacob 		sbuf_printf(sb, "none\n");
1452b5dce617SMatt Jacob 	}
1453b5dce617SMatt Jacob 	sbuf_finish(sb);
1454b5dce617SMatt Jacob 	gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1455b5dce617SMatt Jacob 	sbuf_delete(sb);
1456b5dce617SMatt Jacob }
1457b5dce617SMatt Jacob 
1458b5dce617SMatt Jacob static void
1459e770bc6bSMatt Jacob g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1460e770bc6bSMatt Jacob {
1461e770bc6bSMatt Jacob 	uint32_t *version;
1462e770bc6bSMatt Jacob 	g_topology_assert();
1463e770bc6bSMatt Jacob 	version = gctl_get_paraml(req, "version", sizeof(*version));
1464e770bc6bSMatt Jacob 	if (version == NULL) {
1465e770bc6bSMatt Jacob 		gctl_error(req, "No 'version' argument");
1466e770bc6bSMatt Jacob 	} else if (*version != G_MULTIPATH_VERSION) {
1467e770bc6bSMatt Jacob 		gctl_error(req, "Userland and kernel parts are out of sync");
14682b4969ffSMatt Jacob 	} else if (strcmp(verb, "add") == 0) {
14692b4969ffSMatt Jacob 		g_multipath_ctl_add(req, mp);
147071ee4ef0SThomas Quinot 	} else if (strcmp(verb, "prefer") == 0) {
147171ee4ef0SThomas Quinot 		g_multipath_ctl_prefer(req, mp);
14720c883cefSAlexander Motin 	} else if (strcmp(verb, "create") == 0) {
14730c883cefSAlexander Motin 		g_multipath_ctl_create(req, mp);
147463297dfdSAlexander Motin 	} else if (strcmp(verb, "configure") == 0) {
147563297dfdSAlexander Motin 		g_multipath_ctl_configure(req, mp);
14760c883cefSAlexander Motin 	} else if (strcmp(verb, "stop") == 0) {
14770c883cefSAlexander Motin 		g_multipath_ctl_stop(req, mp);
1478e770bc6bSMatt Jacob 	} else if (strcmp(verb, "destroy") == 0) {
1479e770bc6bSMatt Jacob 		g_multipath_ctl_destroy(req, mp);
14800c883cefSAlexander Motin 	} else if (strcmp(verb, "fail") == 0) {
14810c883cefSAlexander Motin 		g_multipath_ctl_fail(req, mp, 1);
14820c883cefSAlexander Motin 	} else if (strcmp(verb, "restore") == 0) {
14830c883cefSAlexander Motin 		g_multipath_ctl_fail(req, mp, 0);
14840c883cefSAlexander Motin 	} else if (strcmp(verb, "remove") == 0) {
14850c883cefSAlexander Motin 		g_multipath_ctl_remove(req, mp);
1486b5dce617SMatt Jacob 	} else if (strcmp(verb, "rotate") == 0) {
1487b5dce617SMatt Jacob 		g_multipath_ctl_rotate(req, mp);
1488b5dce617SMatt Jacob 	} else if (strcmp(verb, "getactive") == 0) {
1489b5dce617SMatt Jacob 		g_multipath_ctl_getactive(req, mp);
1490e770bc6bSMatt Jacob 	} else {
1491e770bc6bSMatt Jacob 		gctl_error(req, "Unknown verb %s", verb);
1492e770bc6bSMatt Jacob 	}
1493e770bc6bSMatt Jacob }
14940c883cefSAlexander Motin 
14950c883cefSAlexander Motin static void
14960c883cefSAlexander Motin g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
14970c883cefSAlexander Motin     struct g_consumer *cp, struct g_provider *pp)
14980c883cefSAlexander Motin {
14990c883cefSAlexander Motin 	struct g_multipath_softc *sc;
15000c883cefSAlexander Motin 	int good;
15010c883cefSAlexander Motin 
15020c883cefSAlexander Motin 	g_topology_assert();
15030c883cefSAlexander Motin 
15040c883cefSAlexander Motin 	sc = gp->softc;
15050c883cefSAlexander Motin 	if (sc == NULL)
15060c883cefSAlexander Motin 		return;
15070c883cefSAlexander Motin 	if (cp != NULL) {
1508a839e332SAlexander Motin 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
15090c883cefSAlexander Motin 		    (cp->index & MP_NEW) ? "NEW" :
15100c883cefSAlexander Motin 		    (cp->index & MP_LOST) ? "LOST" :
15110c883cefSAlexander Motin 		    (cp->index & MP_FAIL) ? "FAIL" :
151263297dfdSAlexander Motin 		    (sc->sc_active_active == 1 || sc->sc_active == cp) ?
151363297dfdSAlexander Motin 		     "ACTIVE" :
151463297dfdSAlexander Motin 		     sc->sc_active_active == 2 ? "READ" : "PASSIVE");
15150c883cefSAlexander Motin 	} else {
15160c883cefSAlexander Motin 		good = g_multipath_good(gp);
1517a839e332SAlexander Motin 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
15180c883cefSAlexander Motin 		    good == 0 ? "BROKEN" :
15190c883cefSAlexander Motin 		    (good != sc->sc_ndisks || sc->sc_ndisks == 1) ?
15200c883cefSAlexander Motin 		    "DEGRADED" : "OPTIMAL");
15210c883cefSAlexander Motin 	}
15220c883cefSAlexander Motin 	if (cp == NULL && pp == NULL) {
1523a839e332SAlexander Motin 		sbuf_printf(sb, "%s<UUID>%s</UUID>\n", indent, sc->sc_uuid);
1524a839e332SAlexander Motin 		sbuf_printf(sb, "%s<Mode>Active/%s</Mode>\n", indent,
152563297dfdSAlexander Motin 		    sc->sc_active_active == 2 ? "Read" :
152663297dfdSAlexander Motin 		    sc->sc_active_active == 1 ? "Active" : "Passive");
1527a839e332SAlexander Motin 		sbuf_printf(sb, "%s<Type>%s</Type>\n", indent,
15280c883cefSAlexander Motin 		    sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC");
15290c883cefSAlexander Motin 	}
15300c883cefSAlexander Motin }
15310c883cefSAlexander Motin 
1532e770bc6bSMatt Jacob DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);
1533