xref: /freebsd/sys/geom/multipath/g_multipath.c (revision 49ee0fcea52c43f055ce8d63bc6f72dbb91c509d)
1e770bc6bSMatt Jacob /*-
23728855aSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
33728855aSPedro F. Giffuni  *
4e6afd72bSAlexander Motin  * Copyright (c) 2011-2013 Alexander Motin <mav@FreeBSD.org>
5e770bc6bSMatt Jacob  * Copyright (c) 2006-2007 Matthew Jacob <mjacob@FreeBSD.org>
6e770bc6bSMatt Jacob  * All rights reserved.
7e770bc6bSMatt Jacob  *
8e770bc6bSMatt Jacob  * Redistribution and use in source and binary forms, with or without
9e770bc6bSMatt Jacob  * modification, are permitted provided that the following conditions
10e770bc6bSMatt Jacob  * are met:
11e770bc6bSMatt Jacob  * 1. Redistributions of source code must retain the above copyright
12e770bc6bSMatt Jacob  *    notice, this list of conditions and the following disclaimer.
13e770bc6bSMatt Jacob  * 2. Redistributions in binary form must reproduce the above copyright
14e770bc6bSMatt Jacob  *    notice, this list of conditions and the following disclaimer in the
15e770bc6bSMatt Jacob  *    documentation and/or other materials provided with the distribution.
16e770bc6bSMatt Jacob  *
17e770bc6bSMatt Jacob  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18e770bc6bSMatt Jacob  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19e770bc6bSMatt Jacob  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20e770bc6bSMatt Jacob  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21e770bc6bSMatt Jacob  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22e770bc6bSMatt Jacob  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23e770bc6bSMatt Jacob  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24e770bc6bSMatt Jacob  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25e770bc6bSMatt Jacob  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26e770bc6bSMatt Jacob  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27e770bc6bSMatt Jacob  * SUCH DAMAGE.
28e770bc6bSMatt Jacob  */
29e770bc6bSMatt Jacob /*
30e770bc6bSMatt Jacob  * Based upon work by Pawel Jakub Dawidek <pjd@FreeBSD.org> for all of the
31e770bc6bSMatt Jacob  * fine geom examples, and by Poul Henning Kamp <phk@FreeBSD.org> for GEOM
32e770bc6bSMatt Jacob  * itself, all of which is most gratefully acknowledged.
33e770bc6bSMatt Jacob  */
34e770bc6bSMatt Jacob 
35e770bc6bSMatt Jacob #include <sys/cdefs.h>
36e770bc6bSMatt Jacob __FBSDID("$FreeBSD$");
37e770bc6bSMatt Jacob #include <sys/param.h>
38e770bc6bSMatt Jacob #include <sys/systm.h>
39e770bc6bSMatt Jacob #include <sys/kernel.h>
40e770bc6bSMatt Jacob #include <sys/module.h>
41e6afd72bSAlexander Motin #include <sys/limits.h>
42e770bc6bSMatt Jacob #include <sys/lock.h>
43e770bc6bSMatt Jacob #include <sys/mutex.h>
44e770bc6bSMatt Jacob #include <sys/bio.h>
455d807a0eSAndrey V. Elsukov #include <sys/sbuf.h>
46e770bc6bSMatt Jacob #include <sys/sysctl.h>
47e770bc6bSMatt Jacob #include <sys/kthread.h>
48e770bc6bSMatt Jacob #include <sys/malloc.h>
49e770bc6bSMatt Jacob #include <geom/geom.h>
50e770bc6bSMatt Jacob #include <geom/multipath/g_multipath.h>
51e770bc6bSMatt Jacob 
52cb08c2ccSAlexander Leidinger FEATURE(geom_multipath, "GEOM multipath support");
53e770bc6bSMatt Jacob 
54e770bc6bSMatt Jacob SYSCTL_DECL(_kern_geom);
556472ac3dSEd Schouten static SYSCTL_NODE(_kern_geom, OID_AUTO, multipath, CTLFLAG_RW, 0,
56e770bc6bSMatt Jacob     "GEOM_MULTIPATH tunables");
57e770bc6bSMatt Jacob static u_int g_multipath_debug = 0;
58e770bc6bSMatt Jacob SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, debug, CTLFLAG_RW,
59e770bc6bSMatt Jacob     &g_multipath_debug, 0, "Debug level");
600c883cefSAlexander Motin static u_int g_multipath_exclusive = 1;
610c883cefSAlexander Motin SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, exclusive, CTLFLAG_RW,
620c883cefSAlexander Motin     &g_multipath_exclusive, 0, "Exclusively open providers");
63e770bc6bSMatt Jacob 
64e770bc6bSMatt Jacob static enum {
65e770bc6bSMatt Jacob 	GKT_NIL,
66e770bc6bSMatt Jacob 	GKT_RUN,
67e770bc6bSMatt Jacob 	GKT_DIE
68e770bc6bSMatt Jacob } g_multipath_kt_state;
69e770bc6bSMatt Jacob static struct bio_queue_head gmtbq;
70e770bc6bSMatt Jacob static struct mtx gmtbq_mtx;
71e770bc6bSMatt Jacob 
72f8c79813SAlexander Motin static int g_multipath_read_metadata(struct g_consumer *cp,
73f8c79813SAlexander Motin     struct g_multipath_metadata *md);
74f8c79813SAlexander Motin static int g_multipath_write_metadata(struct g_consumer *cp,
75f8c79813SAlexander Motin     struct g_multipath_metadata *md);
76f8c79813SAlexander Motin 
77e770bc6bSMatt Jacob static void g_multipath_orphan(struct g_consumer *);
78e6afd72bSAlexander Motin static void g_multipath_resize(struct g_consumer *);
79e770bc6bSMatt Jacob static void g_multipath_start(struct bio *);
80e770bc6bSMatt Jacob static void g_multipath_done(struct bio *);
81e770bc6bSMatt Jacob static void g_multipath_done_error(struct bio *);
82e770bc6bSMatt Jacob static void g_multipath_kt(void *);
83e770bc6bSMatt Jacob 
84e770bc6bSMatt Jacob static int g_multipath_destroy(struct g_geom *);
85e770bc6bSMatt Jacob static int
86e770bc6bSMatt Jacob g_multipath_destroy_geom(struct gctl_req *, struct g_class *, struct g_geom *);
87e770bc6bSMatt Jacob 
882b4969ffSMatt Jacob static struct g_geom *g_multipath_find_geom(struct g_class *, const char *);
89b5dce617SMatt Jacob static int g_multipath_rotate(struct g_geom *);
90b5dce617SMatt Jacob 
91e770bc6bSMatt Jacob static g_taste_t g_multipath_taste;
92e770bc6bSMatt Jacob static g_ctl_req_t g_multipath_config;
93e770bc6bSMatt Jacob static g_init_t g_multipath_init;
94e770bc6bSMatt Jacob static g_fini_t g_multipath_fini;
950c883cefSAlexander Motin static g_dumpconf_t g_multipath_dumpconf;
96e770bc6bSMatt Jacob 
97e770bc6bSMatt Jacob struct g_class g_multipath_class = {
98e770bc6bSMatt Jacob 	.name		= G_MULTIPATH_CLASS_NAME,
99e770bc6bSMatt Jacob 	.version	= G_VERSION,
100e770bc6bSMatt Jacob 	.ctlreq		= g_multipath_config,
101e770bc6bSMatt Jacob 	.taste		= g_multipath_taste,
102e770bc6bSMatt Jacob 	.destroy_geom	= g_multipath_destroy_geom,
103e770bc6bSMatt Jacob 	.init		= g_multipath_init,
104e770bc6bSMatt Jacob 	.fini		= g_multipath_fini
105e770bc6bSMatt Jacob };
106e770bc6bSMatt Jacob 
1070c883cefSAlexander Motin #define	MP_FAIL		0x00000001
1080c883cefSAlexander Motin #define	MP_LOST		0x00000002
1090c883cefSAlexander Motin #define	MP_NEW		0x00000004
1100c883cefSAlexander Motin #define	MP_POSTED	0x00000008
1110c883cefSAlexander Motin #define	MP_BAD		(MP_FAIL | MP_LOST | MP_NEW)
11225080ac4SSteven Hartland #define	MP_WITHER	0x00000010
11325080ac4SSteven Hartland #define	MP_IDLE		0x00000020
11425080ac4SSteven Hartland #define	MP_IDLE_MASK	0xffffffe0
1150c883cefSAlexander Motin 
1160c883cefSAlexander Motin static int
1170c883cefSAlexander Motin g_multipath_good(struct g_geom *gp)
1180c883cefSAlexander Motin {
1190c883cefSAlexander Motin 	struct g_consumer *cp;
1200c883cefSAlexander Motin 	int n = 0;
1210c883cefSAlexander Motin 
1220c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
1230c883cefSAlexander Motin 		if ((cp->index & MP_BAD) == 0)
1240c883cefSAlexander Motin 			n++;
1250c883cefSAlexander Motin 	}
1260c883cefSAlexander Motin 	return (n);
1270c883cefSAlexander Motin }
1280c883cefSAlexander Motin 
1290c883cefSAlexander Motin static void
1300c883cefSAlexander Motin g_multipath_fault(struct g_consumer *cp, int cause)
1310c883cefSAlexander Motin {
1320c883cefSAlexander Motin 	struct g_multipath_softc *sc;
1330c883cefSAlexander Motin 	struct g_consumer *lcp;
1340c883cefSAlexander Motin 	struct g_geom *gp;
1350c883cefSAlexander Motin 
1360c883cefSAlexander Motin 	gp = cp->geom;
1370c883cefSAlexander Motin 	sc = gp->softc;
1380c883cefSAlexander Motin 	cp->index |= cause;
1390c883cefSAlexander Motin 	if (g_multipath_good(gp) == 0 && sc->sc_ndisks > 0) {
1400c883cefSAlexander Motin 		LIST_FOREACH(lcp, &gp->consumer, consumer) {
1410c883cefSAlexander Motin 			if (lcp->provider == NULL ||
1420c883cefSAlexander Motin 			    (lcp->index & (MP_LOST | MP_NEW)))
1430c883cefSAlexander Motin 				continue;
1440c883cefSAlexander Motin 			if (sc->sc_ndisks > 1 && lcp == cp)
1450c883cefSAlexander Motin 				continue;
1460c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: "
1470c883cefSAlexander Motin 			    "all paths in %s were marked FAIL, restore %s\n",
1480c883cefSAlexander Motin 			    sc->sc_name, lcp->provider->name);
1490c883cefSAlexander Motin 			lcp->index &= ~MP_FAIL;
1500c883cefSAlexander Motin 		}
1510c883cefSAlexander Motin 	}
1520c883cefSAlexander Motin 	if (cp != sc->sc_active)
1530c883cefSAlexander Motin 		return;
1540c883cefSAlexander Motin 	sc->sc_active = NULL;
1550c883cefSAlexander Motin 	LIST_FOREACH(lcp, &gp->consumer, consumer) {
1560c883cefSAlexander Motin 		if ((lcp->index & MP_BAD) == 0) {
1570c883cefSAlexander Motin 			sc->sc_active = lcp;
1580c883cefSAlexander Motin 			break;
1590c883cefSAlexander Motin 		}
1600c883cefSAlexander Motin 	}
1610c883cefSAlexander Motin 	if (sc->sc_active == NULL) {
1620c883cefSAlexander Motin 		printf("GEOM_MULTIPATH: out of providers for %s\n",
1630c883cefSAlexander Motin 		    sc->sc_name);
16463297dfdSAlexander Motin 	} else if (sc->sc_active_active != 1) {
1650c883cefSAlexander Motin 		printf("GEOM_MULTIPATH: %s is now active path in %s\n",
1660c883cefSAlexander Motin 		    sc->sc_active->provider->name, sc->sc_name);
1670c883cefSAlexander Motin 	}
1680c883cefSAlexander Motin }
1690c883cefSAlexander Motin 
1700c883cefSAlexander Motin static struct g_consumer *
17163297dfdSAlexander Motin g_multipath_choose(struct g_geom *gp, struct bio *bp)
1720c883cefSAlexander Motin {
1730c883cefSAlexander Motin 	struct g_multipath_softc *sc;
1740c883cefSAlexander Motin 	struct g_consumer *best, *cp;
1750c883cefSAlexander Motin 
1760c883cefSAlexander Motin 	sc = gp->softc;
17763297dfdSAlexander Motin 	if (sc->sc_active_active == 0 ||
17863297dfdSAlexander Motin 	    (sc->sc_active_active == 2 && bp->bio_cmd != BIO_READ))
1790c883cefSAlexander Motin 		return (sc->sc_active);
1800c883cefSAlexander Motin 	best = NULL;
1810c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
1820c883cefSAlexander Motin 		if (cp->index & MP_BAD)
1830c883cefSAlexander Motin 			continue;
1840c883cefSAlexander Motin 		cp->index += MP_IDLE;
1850c883cefSAlexander Motin 		if (best == NULL || cp->private < best->private ||
1860c883cefSAlexander Motin 		    (cp->private == best->private && cp->index > best->index))
1870c883cefSAlexander Motin 			best = cp;
1880c883cefSAlexander Motin 	}
1890c883cefSAlexander Motin 	if (best != NULL)
1900c883cefSAlexander Motin 		best->index &= ~MP_IDLE_MASK;
1910c883cefSAlexander Motin 	return (best);
1920c883cefSAlexander Motin }
193e770bc6bSMatt Jacob 
194e770bc6bSMatt Jacob static void
195e770bc6bSMatt Jacob g_mpd(void *arg, int flags __unused)
196e770bc6bSMatt Jacob {
1970c883cefSAlexander Motin 	struct g_geom *gp;
1980c883cefSAlexander Motin 	struct g_multipath_softc *sc;
199e770bc6bSMatt Jacob 	struct g_consumer *cp;
2000c883cefSAlexander Motin 	int w;
201e770bc6bSMatt Jacob 
202e770bc6bSMatt Jacob 	g_topology_assert();
203e770bc6bSMatt Jacob 	cp = arg;
2040c883cefSAlexander Motin 	gp = cp->geom;
2050c883cefSAlexander Motin 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) {
2060c883cefSAlexander Motin 		w = cp->acw;
207e770bc6bSMatt Jacob 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
2080c883cefSAlexander Motin 		if (w > 0 && cp->provider != NULL &&
2090c883cefSAlexander Motin 		    (cp->provider->geom->flags & G_GEOM_WITHER) == 0) {
21025080ac4SSteven Hartland 			cp->index |= MP_WITHER;
2110c883cefSAlexander Motin 			g_post_event(g_mpd, cp, M_WAITOK, NULL);
2120c883cefSAlexander Motin 			return;
2130c883cefSAlexander Motin 		}
2140c883cefSAlexander Motin 	}
2150c883cefSAlexander Motin 	sc = gp->softc;
2160c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
217e770bc6bSMatt Jacob 	if (cp->provider) {
218e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: %s removed from %s\n",
2190c883cefSAlexander Motin 		    cp->provider->name, gp->name);
220e770bc6bSMatt Jacob 		g_detach(cp);
221e770bc6bSMatt Jacob 	}
222e770bc6bSMatt Jacob 	g_destroy_consumer(cp);
2230c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
2240c883cefSAlexander Motin 	if (LIST_EMPTY(&gp->consumer))
2250c883cefSAlexander Motin 		g_multipath_destroy(gp);
226e770bc6bSMatt Jacob }
227e770bc6bSMatt Jacob 
228e770bc6bSMatt Jacob static void
229e770bc6bSMatt Jacob g_multipath_orphan(struct g_consumer *cp)
230e770bc6bSMatt Jacob {
2310c883cefSAlexander Motin 	struct g_multipath_softc *sc;
2320c883cefSAlexander Motin 	uintptr_t *cnt;
2330c883cefSAlexander Motin 
2340c883cefSAlexander Motin 	g_topology_assert();
2350c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s in %s was disconnected\n",
236e770bc6bSMatt Jacob 	    cp->provider->name, cp->geom->name);
2370c883cefSAlexander Motin 	sc = cp->geom->softc;
2380c883cefSAlexander Motin 	cnt = (uintptr_t *)&cp->private;
2390c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
2400c883cefSAlexander Motin 	sc->sc_ndisks--;
2410c883cefSAlexander Motin 	g_multipath_fault(cp, MP_LOST);
2420c883cefSAlexander Motin 	if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
2430c883cefSAlexander Motin 		cp->index |= MP_POSTED;
2440c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
245e770bc6bSMatt Jacob 		g_mpd(cp, 0);
2460c883cefSAlexander Motin 	} else
2470c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
248e770bc6bSMatt Jacob }
249e770bc6bSMatt Jacob 
250e770bc6bSMatt Jacob static void
251e6afd72bSAlexander Motin g_multipath_resize(struct g_consumer *cp)
252e6afd72bSAlexander Motin {
253e6afd72bSAlexander Motin 	struct g_multipath_softc *sc;
254e6afd72bSAlexander Motin 	struct g_geom *gp;
255f8c79813SAlexander Motin 	struct g_consumer *cp1;
256e6afd72bSAlexander Motin 	struct g_provider *pp;
257e6afd72bSAlexander Motin 	struct g_multipath_metadata md;
258e6afd72bSAlexander Motin 	off_t size, psize, ssize;
259e6afd72bSAlexander Motin 	int error;
260e6afd72bSAlexander Motin 
261e6afd72bSAlexander Motin 	g_topology_assert();
262e6afd72bSAlexander Motin 
263e6afd72bSAlexander Motin 	gp = cp->geom;
264e6afd72bSAlexander Motin 	pp = cp->provider;
265e6afd72bSAlexander Motin 	sc = gp->softc;
266e6afd72bSAlexander Motin 
267e6afd72bSAlexander Motin 	if (sc->sc_stopping)
268e6afd72bSAlexander Motin 		return;
269e6afd72bSAlexander Motin 
270e6afd72bSAlexander Motin 	if (pp->mediasize < sc->sc_size) {
271e6afd72bSAlexander Motin 		size = pp->mediasize;
272e6afd72bSAlexander Motin 		ssize = pp->sectorsize;
273e6afd72bSAlexander Motin 	} else {
274e6afd72bSAlexander Motin 		size = ssize = OFF_MAX;
275e6afd72bSAlexander Motin 		mtx_lock(&sc->sc_mtx);
276f8c79813SAlexander Motin 		LIST_FOREACH(cp1, &gp->consumer, consumer) {
277f8c79813SAlexander Motin 			pp = cp1->provider;
278e6afd72bSAlexander Motin 			if (pp == NULL)
279e6afd72bSAlexander Motin 				continue;
280e6afd72bSAlexander Motin 			if (pp->mediasize < size) {
281e6afd72bSAlexander Motin 				size = pp->mediasize;
282e6afd72bSAlexander Motin 				ssize = pp->sectorsize;
283e6afd72bSAlexander Motin 			}
284e6afd72bSAlexander Motin 		}
285e6afd72bSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
286e6afd72bSAlexander Motin 		if (size == OFF_MAX || size == sc->sc_size)
287e6afd72bSAlexander Motin 			return;
288e6afd72bSAlexander Motin 	}
289e6afd72bSAlexander Motin 	psize = size - ((sc->sc_uuid[0] != 0) ? ssize : 0);
290e6afd72bSAlexander Motin 	printf("GEOM_MULTIPATH: %s size changed from %jd to %jd\n",
291e6afd72bSAlexander Motin 	    sc->sc_name, sc->sc_pp->mediasize, psize);
292f8c79813SAlexander Motin 	if (sc->sc_uuid[0] != 0 && size < sc->sc_size) {
293f8c79813SAlexander Motin 		error = g_multipath_read_metadata(cp, &md);
294f8c79813SAlexander Motin 		if (error ||
295f8c79813SAlexander Motin 		    (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) ||
296f8c79813SAlexander Motin 		    (memcmp(md.md_uuid, sc->sc_uuid, sizeof(sc->sc_uuid)) != 0) ||
297f8c79813SAlexander Motin 		    (strcmp(md.md_name, sc->sc_name) != 0) ||
298f8c79813SAlexander Motin 		    (md.md_size != 0 && md.md_size != size) ||
299f8c79813SAlexander Motin 		    (md.md_sectorsize != 0 && md.md_sectorsize != ssize)) {
300e6afd72bSAlexander Motin 			g_multipath_destroy(gp);
301e6afd72bSAlexander Motin 			return;
302e6afd72bSAlexander Motin 		}
303f8c79813SAlexander Motin 	}
304e6afd72bSAlexander Motin 	sc->sc_size = size;
305e6afd72bSAlexander Motin 	g_resize_provider(sc->sc_pp, psize);
306e6afd72bSAlexander Motin 
307f8c79813SAlexander Motin 	if (sc->sc_uuid[0] != 0) {
308e6afd72bSAlexander Motin 		pp = cp->provider;
309e6afd72bSAlexander Motin 		strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
310e6afd72bSAlexander Motin 		memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
311e6afd72bSAlexander Motin 		strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
312e6afd72bSAlexander Motin 		md.md_version = G_MULTIPATH_VERSION;
313e6afd72bSAlexander Motin 		md.md_size = size;
314f8c79813SAlexander Motin 		md.md_sectorsize = ssize;
315e6afd72bSAlexander Motin 		md.md_active_active = sc->sc_active_active;
316f8c79813SAlexander Motin 		error = g_multipath_write_metadata(cp, &md);
317e6afd72bSAlexander Motin 		if (error != 0)
318e6afd72bSAlexander Motin 			printf("GEOM_MULTIPATH: Can't update metadata on %s "
319e6afd72bSAlexander Motin 			    "(%d)\n", pp->name, error);
320e6afd72bSAlexander Motin 	}
321e6afd72bSAlexander Motin }
322e6afd72bSAlexander Motin 
323e6afd72bSAlexander Motin static void
324e770bc6bSMatt Jacob g_multipath_start(struct bio *bp)
325e770bc6bSMatt Jacob {
326e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
327e770bc6bSMatt Jacob 	struct g_geom *gp;
328e770bc6bSMatt Jacob 	struct g_consumer *cp;
329e770bc6bSMatt Jacob 	struct bio *cbp;
3300c883cefSAlexander Motin 	uintptr_t *cnt;
331e770bc6bSMatt Jacob 
332e770bc6bSMatt Jacob 	gp = bp->bio_to->geom;
333e770bc6bSMatt Jacob 	sc = gp->softc;
334e770bc6bSMatt Jacob 	KASSERT(sc != NULL, ("NULL sc"));
335e770bc6bSMatt Jacob 	cbp = g_clone_bio(bp);
336e770bc6bSMatt Jacob 	if (cbp == NULL) {
337e770bc6bSMatt Jacob 		g_io_deliver(bp, ENOMEM);
338e770bc6bSMatt Jacob 		return;
339e770bc6bSMatt Jacob 	}
3400c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
34163297dfdSAlexander Motin 	cp = g_multipath_choose(gp, bp);
3420c883cefSAlexander Motin 	if (cp == NULL) {
3430c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
3440c883cefSAlexander Motin 		g_destroy_bio(cbp);
3450c883cefSAlexander Motin 		g_io_deliver(bp, ENXIO);
3460c883cefSAlexander Motin 		return;
3470c883cefSAlexander Motin 	}
3480c883cefSAlexander Motin 	if ((uintptr_t)bp->bio_driver1 < sc->sc_ndisks)
3490c883cefSAlexander Motin 		bp->bio_driver1 = (void *)(uintptr_t)sc->sc_ndisks;
3500c883cefSAlexander Motin 	cnt = (uintptr_t *)&cp->private;
3510c883cefSAlexander Motin 	(*cnt)++;
3520c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
353e770bc6bSMatt Jacob 	cbp->bio_done = g_multipath_done;
354e770bc6bSMatt Jacob 	g_io_request(cbp, cp);
355e770bc6bSMatt Jacob }
356e770bc6bSMatt Jacob 
357e770bc6bSMatt Jacob static void
358e770bc6bSMatt Jacob g_multipath_done(struct bio *bp)
359e770bc6bSMatt Jacob {
3600c883cefSAlexander Motin 	struct g_multipath_softc *sc;
3610c883cefSAlexander Motin 	struct g_consumer *cp;
3620c883cefSAlexander Motin 	uintptr_t *cnt;
3630c883cefSAlexander Motin 
364e770bc6bSMatt Jacob 	if (bp->bio_error == ENXIO || bp->bio_error == EIO) {
365e770bc6bSMatt Jacob 		mtx_lock(&gmtbq_mtx);
366e770bc6bSMatt Jacob 		bioq_insert_tail(&gmtbq, bp);
367e770bc6bSMatt Jacob 		mtx_unlock(&gmtbq_mtx);
3680c883cefSAlexander Motin 		wakeup(&g_multipath_kt_state);
369e770bc6bSMatt Jacob 	} else {
3700c883cefSAlexander Motin 		cp = bp->bio_from;
3710c883cefSAlexander Motin 		sc = cp->geom->softc;
3720c883cefSAlexander Motin 		cnt = (uintptr_t *)&cp->private;
3730c883cefSAlexander Motin 		mtx_lock(&sc->sc_mtx);
3740c883cefSAlexander Motin 		(*cnt)--;
3750c883cefSAlexander Motin 		if (*cnt == 0 && (cp->index & MP_LOST)) {
3760ada3afcSAlexander Motin 			if (g_post_event(g_mpd, cp, M_NOWAIT, NULL) == 0)
3770c883cefSAlexander Motin 				cp->index |= MP_POSTED;
3780c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
3790c883cefSAlexander Motin 		} else
3800c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
381e770bc6bSMatt Jacob 		g_std_done(bp);
382e770bc6bSMatt Jacob 	}
383e770bc6bSMatt Jacob }
384e770bc6bSMatt Jacob 
385e770bc6bSMatt Jacob static void
386e770bc6bSMatt Jacob g_multipath_done_error(struct bio *bp)
387e770bc6bSMatt Jacob {
388e770bc6bSMatt Jacob 	struct bio *pbp;
389e770bc6bSMatt Jacob 	struct g_geom *gp;
390e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
391e770bc6bSMatt Jacob 	struct g_consumer *cp;
392e770bc6bSMatt Jacob 	struct g_provider *pp;
3930c883cefSAlexander Motin 	uintptr_t *cnt;
394e770bc6bSMatt Jacob 
395e770bc6bSMatt Jacob 	/*
396e770bc6bSMatt Jacob 	 * If we had a failure, we have to check first to see
397e770bc6bSMatt Jacob 	 * whether the consumer it failed on was the currently
398e770bc6bSMatt Jacob 	 * active consumer (i.e., this is the first in perhaps
399e770bc6bSMatt Jacob 	 * a number of failures). If so, we then switch consumers
400e770bc6bSMatt Jacob 	 * to the next available consumer.
401e770bc6bSMatt Jacob 	 */
402e770bc6bSMatt Jacob 
403e770bc6bSMatt Jacob 	pbp = bp->bio_parent;
404e770bc6bSMatt Jacob 	gp = pbp->bio_to->geom;
405e770bc6bSMatt Jacob 	sc = gp->softc;
406e770bc6bSMatt Jacob 	cp = bp->bio_from;
407e770bc6bSMatt Jacob 	pp = cp->provider;
4080c883cefSAlexander Motin 	cnt = (uintptr_t *)&cp->private;
409e770bc6bSMatt Jacob 
4100c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
41163297dfdSAlexander Motin 	if ((cp->index & MP_FAIL) == 0) {
4120c883cefSAlexander Motin 		printf("GEOM_MULTIPATH: Error %d, %s in %s marked FAIL\n",
4130c883cefSAlexander Motin 		    bp->bio_error, pp->name, sc->sc_name);
4140c883cefSAlexander Motin 		g_multipath_fault(cp, MP_FAIL);
41563297dfdSAlexander Motin 	}
4160c883cefSAlexander Motin 	(*cnt)--;
4170c883cefSAlexander Motin 	if (*cnt == 0 && (cp->index & (MP_LOST | MP_POSTED)) == MP_LOST) {
418e770bc6bSMatt Jacob 		cp->index |= MP_POSTED;
4190c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
4200c883cefSAlexander Motin 		g_post_event(g_mpd, cp, M_WAITOK, NULL);
4210c883cefSAlexander Motin 	} else
4220c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
423e770bc6bSMatt Jacob 
424e770bc6bSMatt Jacob 	/*
425e770bc6bSMatt Jacob 	 * If we can fruitfully restart the I/O, do so.
426e770bc6bSMatt Jacob 	 */
4270c883cefSAlexander Motin 	if (pbp->bio_children < (uintptr_t)pbp->bio_driver1) {
4280c883cefSAlexander Motin 		pbp->bio_inbed++;
429e770bc6bSMatt Jacob 		g_destroy_bio(bp);
430e770bc6bSMatt Jacob 		g_multipath_start(pbp);
431e770bc6bSMatt Jacob 	} else {
432e770bc6bSMatt Jacob 		g_std_done(bp);
433e770bc6bSMatt Jacob 	}
434e770bc6bSMatt Jacob }
435e770bc6bSMatt Jacob 
436e770bc6bSMatt Jacob static void
437e770bc6bSMatt Jacob g_multipath_kt(void *arg)
438e770bc6bSMatt Jacob {
43912f35a61SPawel Jakub Dawidek 
440e770bc6bSMatt Jacob 	g_multipath_kt_state = GKT_RUN;
441e770bc6bSMatt Jacob 	mtx_lock(&gmtbq_mtx);
442e770bc6bSMatt Jacob 	while (g_multipath_kt_state == GKT_RUN) {
443e770bc6bSMatt Jacob 		for (;;) {
444e770bc6bSMatt Jacob 			struct bio *bp;
44512f35a61SPawel Jakub Dawidek 
446e770bc6bSMatt Jacob 			bp = bioq_takefirst(&gmtbq);
44712f35a61SPawel Jakub Dawidek 			if (bp == NULL)
448e770bc6bSMatt Jacob 				break;
449e770bc6bSMatt Jacob 			mtx_unlock(&gmtbq_mtx);
450e770bc6bSMatt Jacob 			g_multipath_done_error(bp);
451e770bc6bSMatt Jacob 			mtx_lock(&gmtbq_mtx);
452e770bc6bSMatt Jacob 		}
45363297dfdSAlexander Motin 		if (g_multipath_kt_state != GKT_RUN)
45463297dfdSAlexander Motin 			break;
455e770bc6bSMatt Jacob 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
45663297dfdSAlexander Motin 		    "gkt:wait", 0);
457e770bc6bSMatt Jacob 	}
458e770bc6bSMatt Jacob 	mtx_unlock(&gmtbq_mtx);
459e770bc6bSMatt Jacob 	wakeup(&g_multipath_kt_state);
4603745c395SJulian Elischer 	kproc_exit(0);
461e770bc6bSMatt Jacob }
462e770bc6bSMatt Jacob 
463e770bc6bSMatt Jacob 
464e770bc6bSMatt Jacob static int
465e770bc6bSMatt Jacob g_multipath_access(struct g_provider *pp, int dr, int dw, int de)
466e770bc6bSMatt Jacob {
467e770bc6bSMatt Jacob 	struct g_geom *gp;
468e770bc6bSMatt Jacob 	struct g_consumer *cp, *badcp = NULL;
4690c883cefSAlexander Motin 	struct g_multipath_softc *sc;
470e770bc6bSMatt Jacob 	int error;
471e770bc6bSMatt Jacob 
472e770bc6bSMatt Jacob 	gp = pp->geom;
473e770bc6bSMatt Jacob 
47425080ac4SSteven Hartland 	/* Error used if we have no valid consumers. */
47580f0a89cSAlexander Motin 	error = (dr > 0 || dw > 0 || de > 0) ? ENXIO : 0;
47625080ac4SSteven Hartland 
477e770bc6bSMatt Jacob 	LIST_FOREACH(cp, &gp->consumer, consumer) {
47825080ac4SSteven Hartland 		if (cp->index & MP_WITHER)
47925080ac4SSteven Hartland 			continue;
48025080ac4SSteven Hartland 
481e770bc6bSMatt Jacob 		error = g_access(cp, dr, dw, de);
482e770bc6bSMatt Jacob 		if (error) {
483e770bc6bSMatt Jacob 			badcp = cp;
484e770bc6bSMatt Jacob 			goto fail;
485e770bc6bSMatt Jacob 		}
486e770bc6bSMatt Jacob 	}
48725080ac4SSteven Hartland 
48825080ac4SSteven Hartland 	if (error != 0)
48925080ac4SSteven Hartland 		return (error);
49025080ac4SSteven Hartland 
4910c883cefSAlexander Motin 	sc = gp->softc;
4920c883cefSAlexander Motin 	sc->sc_opened += dr + dw + de;
4930c883cefSAlexander Motin 	if (sc->sc_stopping && sc->sc_opened == 0)
4940c883cefSAlexander Motin 		g_multipath_destroy(gp);
49525080ac4SSteven Hartland 
496e770bc6bSMatt Jacob 	return (0);
497e770bc6bSMatt Jacob 
498e770bc6bSMatt Jacob fail:
499e770bc6bSMatt Jacob 	LIST_FOREACH(cp, &gp->consumer, consumer) {
50012f35a61SPawel Jakub Dawidek 		if (cp == badcp)
501e770bc6bSMatt Jacob 			break;
50225080ac4SSteven Hartland 		if (cp->index & MP_WITHER)
50325080ac4SSteven Hartland 			continue;
50425080ac4SSteven Hartland 
505e770bc6bSMatt Jacob 		(void) g_access(cp, -dr, -dw, -de);
506e770bc6bSMatt Jacob 	}
507e770bc6bSMatt Jacob 	return (error);
508e770bc6bSMatt Jacob }
509e770bc6bSMatt Jacob 
510e770bc6bSMatt Jacob static struct g_geom *
511e770bc6bSMatt Jacob g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md)
512e770bc6bSMatt Jacob {
513e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
514e770bc6bSMatt Jacob 	struct g_geom *gp;
515e770bc6bSMatt Jacob 	struct g_provider *pp;
516e770bc6bSMatt Jacob 
517e770bc6bSMatt Jacob 	g_topology_assert();
518e770bc6bSMatt Jacob 
519e770bc6bSMatt Jacob 	LIST_FOREACH(gp, &mp->geom, geom) {
5200c883cefSAlexander Motin 		sc = gp->softc;
5210c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
5220c883cefSAlexander Motin 			continue;
523e770bc6bSMatt Jacob 		if (strcmp(gp->name, md->md_name) == 0) {
524e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: name %s already exists\n",
525e770bc6bSMatt Jacob 			    md->md_name);
526e770bc6bSMatt Jacob 			return (NULL);
527e770bc6bSMatt Jacob 		}
528e770bc6bSMatt Jacob 	}
529e770bc6bSMatt Jacob 
53002c62349SJaakko Heinonen 	gp = g_new_geomf(mp, "%s", md->md_name);
531e770bc6bSMatt Jacob 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
5320c883cefSAlexander Motin 	mtx_init(&sc->sc_mtx, "multipath", NULL, MTX_DEF);
5330c883cefSAlexander Motin 	memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid));
5340c883cefSAlexander Motin 	memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name));
5350c883cefSAlexander Motin 	sc->sc_active_active = md->md_active_active;
536e6afd72bSAlexander Motin 	sc->sc_size = md->md_size;
537e770bc6bSMatt Jacob 	gp->softc = sc;
538e770bc6bSMatt Jacob 	gp->start = g_multipath_start;
539e770bc6bSMatt Jacob 	gp->orphan = g_multipath_orphan;
540e6afd72bSAlexander Motin 	gp->resize = g_multipath_resize;
541e770bc6bSMatt Jacob 	gp->access = g_multipath_access;
5420c883cefSAlexander Motin 	gp->dumpconf = g_multipath_dumpconf;
543e770bc6bSMatt Jacob 
544e770bc6bSMatt Jacob 	pp = g_new_providerf(gp, "multipath/%s", md->md_name);
54540ea77a0SAlexander Motin 	pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
5460c883cefSAlexander Motin 	if (md->md_size != 0) {
5470c883cefSAlexander Motin 		pp->mediasize = md->md_size -
5480c883cefSAlexander Motin 		    ((md->md_uuid[0] != 0) ? md->md_sectorsize : 0);
549e770bc6bSMatt Jacob 		pp->sectorsize = md->md_sectorsize;
5500c883cefSAlexander Motin 	}
5510c883cefSAlexander Motin 	sc->sc_pp = pp;
552e770bc6bSMatt Jacob 	g_error_provider(pp, 0);
5530c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s created\n", gp->name);
554e770bc6bSMatt Jacob 	return (gp);
555e770bc6bSMatt Jacob }
556e770bc6bSMatt Jacob 
557e770bc6bSMatt Jacob static int
558e770bc6bSMatt Jacob g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp)
559e770bc6bSMatt Jacob {
560e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
561e770bc6bSMatt Jacob 	struct g_consumer *cp, *nxtcp;
5620c883cefSAlexander Motin 	int error, acr, acw, ace;
563e770bc6bSMatt Jacob 
564e770bc6bSMatt Jacob 	g_topology_assert();
565e770bc6bSMatt Jacob 
566e770bc6bSMatt Jacob 	sc = gp->softc;
567e770bc6bSMatt Jacob 	KASSERT(sc, ("no softc"));
568e770bc6bSMatt Jacob 
569e770bc6bSMatt Jacob 	/*
570e770bc6bSMatt Jacob 	 * Make sure that the passed provider isn't already attached
571e770bc6bSMatt Jacob 	 */
572e770bc6bSMatt Jacob 	LIST_FOREACH(cp, &gp->consumer, consumer) {
57312f35a61SPawel Jakub Dawidek 		if (cp->provider == pp)
574e770bc6bSMatt Jacob 			break;
575e770bc6bSMatt Jacob 	}
576e770bc6bSMatt Jacob 	if (cp) {
577e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: provider %s already attached to %s\n",
578e770bc6bSMatt Jacob 		    pp->name, gp->name);
579e770bc6bSMatt Jacob 		return (EEXIST);
580e770bc6bSMatt Jacob 	}
581e770bc6bSMatt Jacob 	nxtcp = LIST_FIRST(&gp->consumer);
582e770bc6bSMatt Jacob 	cp = g_new_consumer(gp);
58340ea77a0SAlexander Motin 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
5840c883cefSAlexander Motin 	cp->private = NULL;
5850c883cefSAlexander Motin 	cp->index = MP_NEW;
586e770bc6bSMatt Jacob 	error = g_attach(cp, pp);
587e770bc6bSMatt Jacob 	if (error != 0) {
588e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: cannot attach %s to %s",
589e770bc6bSMatt Jacob 		    pp->name, sc->sc_name);
590e770bc6bSMatt Jacob 		g_destroy_consumer(cp);
591e770bc6bSMatt Jacob 		return (error);
592e770bc6bSMatt Jacob 	}
593e770bc6bSMatt Jacob 
594e770bc6bSMatt Jacob 	/*
595e770bc6bSMatt Jacob 	 * Set access permissions on new consumer to match other consumers
596e770bc6bSMatt Jacob 	 */
5970c883cefSAlexander Motin 	if (sc->sc_pp) {
5980c883cefSAlexander Motin 		acr = sc->sc_pp->acr;
5990c883cefSAlexander Motin 		acw = sc->sc_pp->acw;
6000c883cefSAlexander Motin 		ace = sc->sc_pp->ace;
6010c883cefSAlexander Motin 	} else
6020c883cefSAlexander Motin 		acr = acw = ace = 0;
6030c883cefSAlexander Motin 	if (g_multipath_exclusive) {
6040c883cefSAlexander Motin 		acr++;
6050c883cefSAlexander Motin 		acw++;
6060c883cefSAlexander Motin 		ace++;
6070c883cefSAlexander Motin 	}
6080c883cefSAlexander Motin 	error = g_access(cp, acr, acw, ace);
609e770bc6bSMatt Jacob 	if (error) {
610e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: cannot set access in "
6110c883cefSAlexander Motin 		    "attaching %s to %s (%d)\n",
6120c883cefSAlexander Motin 		    pp->name, sc->sc_name, error);
613e770bc6bSMatt Jacob 		g_detach(cp);
614e770bc6bSMatt Jacob 		g_destroy_consumer(cp);
615e770bc6bSMatt Jacob 		return (error);
616e770bc6bSMatt Jacob 	}
617e6afd72bSAlexander Motin 	if (sc->sc_size == 0) {
618e6afd72bSAlexander Motin 		sc->sc_size = pp->mediasize -
6190c883cefSAlexander Motin 		    ((sc->sc_uuid[0] != 0) ? pp->sectorsize : 0);
620e6afd72bSAlexander Motin 		sc->sc_pp->mediasize = sc->sc_size;
6210c883cefSAlexander Motin 		sc->sc_pp->sectorsize = pp->sectorsize;
622e770bc6bSMatt Jacob 	}
623e6afd72bSAlexander Motin 	if (sc->sc_pp->stripesize == 0 && sc->sc_pp->stripeoffset == 0) {
6240c883cefSAlexander Motin 		sc->sc_pp->stripesize = pp->stripesize;
6250c883cefSAlexander Motin 		sc->sc_pp->stripeoffset = pp->stripeoffset;
6260c883cefSAlexander Motin 	}
627f4673017SAlexander Motin 	sc->sc_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
6280c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
6290c883cefSAlexander Motin 	cp->index = 0;
6300c883cefSAlexander Motin 	sc->sc_ndisks++;
6310c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
6320c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s added to %s\n",
6330c883cefSAlexander Motin 	    pp->name, sc->sc_name);
6340c883cefSAlexander Motin 	if (sc->sc_active == NULL) {
6350c883cefSAlexander Motin 		sc->sc_active = cp;
63663297dfdSAlexander Motin 		if (sc->sc_active_active != 1)
6370c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
638e770bc6bSMatt Jacob 			    pp->name, sc->sc_name);
639e770bc6bSMatt Jacob 	}
640e770bc6bSMatt Jacob 	return (0);
641e770bc6bSMatt Jacob }
642e770bc6bSMatt Jacob 
643e770bc6bSMatt Jacob static int
644e770bc6bSMatt Jacob g_multipath_destroy(struct g_geom *gp)
645e770bc6bSMatt Jacob {
6460c883cefSAlexander Motin 	struct g_multipath_softc *sc;
6470c883cefSAlexander Motin 	struct g_consumer *cp, *cp1;
648e770bc6bSMatt Jacob 
649e770bc6bSMatt Jacob 	g_topology_assert();
65012f35a61SPawel Jakub Dawidek 	if (gp->softc == NULL)
651e770bc6bSMatt Jacob 		return (ENXIO);
6520c883cefSAlexander Motin 	sc = gp->softc;
6530c883cefSAlexander Motin 	if (!sc->sc_stopping) {
654e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
6550c883cefSAlexander Motin 		sc->sc_stopping = 1;
6560c883cefSAlexander Motin 	}
6570c883cefSAlexander Motin 	if (sc->sc_opened != 0) {
6580c883cefSAlexander Motin 		g_wither_provider(sc->sc_pp, ENXIO);
6590c883cefSAlexander Motin 		sc->sc_pp = NULL;
6600c883cefSAlexander Motin 		return (EINPROGRESS);
6610c883cefSAlexander Motin 	}
6620c883cefSAlexander Motin 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
6630c883cefSAlexander Motin 		mtx_lock(&sc->sc_mtx);
6640c883cefSAlexander Motin 		if ((cp->index & MP_POSTED) == 0) {
6650c883cefSAlexander Motin 			cp->index |= MP_POSTED;
6660c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
6670c883cefSAlexander Motin 			g_mpd(cp, 0);
6680c883cefSAlexander Motin 			if (cp1 == NULL)
6690c883cefSAlexander Motin 				return(0);	/* Recursion happened. */
6700c883cefSAlexander Motin 		} else
6710c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
6720c883cefSAlexander Motin 	}
6730c883cefSAlexander Motin 	if (!LIST_EMPTY(&gp->consumer))
6740c883cefSAlexander Motin 		return (EINPROGRESS);
6750c883cefSAlexander Motin 	mtx_destroy(&sc->sc_mtx);
676e770bc6bSMatt Jacob 	g_free(gp->softc);
677e770bc6bSMatt Jacob 	gp->softc = NULL;
6780c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s destroyed\n", gp->name);
679e770bc6bSMatt Jacob 	g_wither_geom(gp, ENXIO);
680e770bc6bSMatt Jacob 	return (0);
681e770bc6bSMatt Jacob }
682e770bc6bSMatt Jacob 
683e770bc6bSMatt Jacob static int
684e770bc6bSMatt Jacob g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
685e770bc6bSMatt Jacob     struct g_geom *gp)
686e770bc6bSMatt Jacob {
68712f35a61SPawel Jakub Dawidek 
688e770bc6bSMatt Jacob 	return (g_multipath_destroy(gp));
689e770bc6bSMatt Jacob }
690e770bc6bSMatt Jacob 
691b5dce617SMatt Jacob static int
692b5dce617SMatt Jacob g_multipath_rotate(struct g_geom *gp)
693b5dce617SMatt Jacob {
6948fb378d6SThomas Quinot 	struct g_consumer *lcp, *first_good_cp = NULL;
695b5dce617SMatt Jacob 	struct g_multipath_softc *sc = gp->softc;
6968fb378d6SThomas Quinot 	int active_cp_seen = 0;
697b5dce617SMatt Jacob 
698b5dce617SMatt Jacob 	g_topology_assert();
699b5dce617SMatt Jacob 	if (sc == NULL)
700b5dce617SMatt Jacob 		return (ENXIO);
701b5dce617SMatt Jacob 	LIST_FOREACH(lcp, &gp->consumer, consumer) {
702b5dce617SMatt Jacob 		if ((lcp->index & MP_BAD) == 0) {
7038fb378d6SThomas Quinot 			if (first_good_cp == NULL)
7048fb378d6SThomas Quinot 				first_good_cp = lcp;
7058fb378d6SThomas Quinot 			if (active_cp_seen)
706b5dce617SMatt Jacob 				break;
707b5dce617SMatt Jacob 		}
7088fb378d6SThomas Quinot 		if (sc->sc_active == lcp)
7098fb378d6SThomas Quinot 			active_cp_seen = 1;
710b5dce617SMatt Jacob 	}
7118fb378d6SThomas Quinot 	if (lcp == NULL)
7128fb378d6SThomas Quinot 		lcp = first_good_cp;
7138fb378d6SThomas Quinot 	if (lcp && lcp != sc->sc_active) {
7140c883cefSAlexander Motin 		sc->sc_active = lcp;
71563297dfdSAlexander Motin 		if (sc->sc_active_active != 1)
7160c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
717b5dce617SMatt Jacob 			    lcp->provider->name, sc->sc_name);
718b5dce617SMatt Jacob 	}
719b5dce617SMatt Jacob 	return (0);
720b5dce617SMatt Jacob }
721b5dce617SMatt Jacob 
722e770bc6bSMatt Jacob static void
723e770bc6bSMatt Jacob g_multipath_init(struct g_class *mp)
724e770bc6bSMatt Jacob {
725e770bc6bSMatt Jacob 	bioq_init(&gmtbq);
726e770bc6bSMatt Jacob 	mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
72763297dfdSAlexander Motin 	kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt");
728e770bc6bSMatt Jacob }
729e770bc6bSMatt Jacob 
730e770bc6bSMatt Jacob static void
731e770bc6bSMatt Jacob g_multipath_fini(struct g_class *mp)
732e770bc6bSMatt Jacob {
733e770bc6bSMatt Jacob 	if (g_multipath_kt_state == GKT_RUN) {
734e770bc6bSMatt Jacob 		mtx_lock(&gmtbq_mtx);
735e770bc6bSMatt Jacob 		g_multipath_kt_state = GKT_DIE;
736e770bc6bSMatt Jacob 		wakeup(&g_multipath_kt_state);
737e770bc6bSMatt Jacob 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
738e770bc6bSMatt Jacob 		    "gmp:fini", 0);
739e770bc6bSMatt Jacob 		mtx_unlock(&gmtbq_mtx);
740e770bc6bSMatt Jacob 	}
741e770bc6bSMatt Jacob }
742e770bc6bSMatt Jacob 
743e770bc6bSMatt Jacob static int
744e770bc6bSMatt Jacob g_multipath_read_metadata(struct g_consumer *cp,
745e770bc6bSMatt Jacob     struct g_multipath_metadata *md)
746e770bc6bSMatt Jacob {
747e770bc6bSMatt Jacob 	struct g_provider *pp;
748e770bc6bSMatt Jacob 	u_char *buf;
749e770bc6bSMatt Jacob 	int error;
750e770bc6bSMatt Jacob 
751e770bc6bSMatt Jacob 	g_topology_assert();
752e770bc6bSMatt Jacob 	error = g_access(cp, 1, 0, 0);
75312f35a61SPawel Jakub Dawidek 	if (error != 0)
754e770bc6bSMatt Jacob 		return (error);
755e770bc6bSMatt Jacob 	pp = cp->provider;
756e770bc6bSMatt Jacob 	g_topology_unlock();
757e770bc6bSMatt Jacob 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
758e770bc6bSMatt Jacob 	    pp->sectorsize, &error);
759e770bc6bSMatt Jacob 	g_topology_lock();
760e770bc6bSMatt Jacob 	g_access(cp, -1, 0, 0);
76112f35a61SPawel Jakub Dawidek 	if (buf == NULL)
762e770bc6bSMatt Jacob 		return (error);
763e770bc6bSMatt Jacob 	multipath_metadata_decode(buf, md);
764e770bc6bSMatt Jacob 	g_free(buf);
765e770bc6bSMatt Jacob 	return (0);
766e770bc6bSMatt Jacob }
767e770bc6bSMatt Jacob 
768f8c79813SAlexander Motin static int
769f8c79813SAlexander Motin g_multipath_write_metadata(struct g_consumer *cp,
770f8c79813SAlexander Motin     struct g_multipath_metadata *md)
771f8c79813SAlexander Motin {
772f8c79813SAlexander Motin 	struct g_provider *pp;
773f8c79813SAlexander Motin 	u_char *buf;
774f8c79813SAlexander Motin 	int error;
775f8c79813SAlexander Motin 
776f8c79813SAlexander Motin 	g_topology_assert();
777f8c79813SAlexander Motin 	error = g_access(cp, 1, 1, 1);
778f8c79813SAlexander Motin 	if (error != 0)
779f8c79813SAlexander Motin 		return (error);
780f8c79813SAlexander Motin 	pp = cp->provider;
781f8c79813SAlexander Motin 	g_topology_unlock();
782f8c79813SAlexander Motin 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
783f8c79813SAlexander Motin 	multipath_metadata_encode(md, buf);
784f8c79813SAlexander Motin 	error = g_write_data(cp, pp->mediasize - pp->sectorsize,
785f8c79813SAlexander Motin 	    buf, pp->sectorsize);
786f8c79813SAlexander Motin 	g_topology_lock();
787f8c79813SAlexander Motin 	g_access(cp, -1, -1, -1);
788f8c79813SAlexander Motin 	g_free(buf);
789f8c79813SAlexander Motin 	return (error);
790f8c79813SAlexander Motin }
791f8c79813SAlexander Motin 
792e770bc6bSMatt Jacob static struct g_geom *
793e770bc6bSMatt Jacob g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
794e770bc6bSMatt Jacob {
795e770bc6bSMatt Jacob 	struct g_multipath_metadata md;
796e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
797e770bc6bSMatt Jacob 	struct g_consumer *cp;
798e770bc6bSMatt Jacob 	struct g_geom *gp, *gp1;
799e770bc6bSMatt Jacob 	int error, isnew;
800e770bc6bSMatt Jacob 
801e770bc6bSMatt Jacob 	g_topology_assert();
802e770bc6bSMatt Jacob 
803e770bc6bSMatt Jacob 	gp = g_new_geomf(mp, "multipath:taste");
804e770bc6bSMatt Jacob 	gp->start = g_multipath_start;
805e770bc6bSMatt Jacob 	gp->access = g_multipath_access;
806e770bc6bSMatt Jacob 	gp->orphan = g_multipath_orphan;
807e770bc6bSMatt Jacob 	cp = g_new_consumer(gp);
808e770bc6bSMatt Jacob 	g_attach(cp, pp);
809e770bc6bSMatt Jacob 	error = g_multipath_read_metadata(cp, &md);
810e770bc6bSMatt Jacob 	g_detach(cp);
811e770bc6bSMatt Jacob 	g_destroy_consumer(cp);
812e770bc6bSMatt Jacob 	g_destroy_geom(gp);
81312f35a61SPawel Jakub Dawidek 	if (error != 0)
814e770bc6bSMatt Jacob 		return (NULL);
815e770bc6bSMatt Jacob 	gp = NULL;
816e770bc6bSMatt Jacob 
817e770bc6bSMatt Jacob 	if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
81812f35a61SPawel Jakub Dawidek 		if (g_multipath_debug)
819e770bc6bSMatt Jacob 			printf("%s is not MULTIPATH\n", pp->name);
820e770bc6bSMatt Jacob 		return (NULL);
821e770bc6bSMatt Jacob 	}
822e770bc6bSMatt Jacob 	if (md.md_version != G_MULTIPATH_VERSION) {
823e770bc6bSMatt Jacob 		printf("%s has version %d multipath id- this module is version "
824e770bc6bSMatt Jacob 		    " %d: rejecting\n", pp->name, md.md_version,
825e770bc6bSMatt Jacob 		    G_MULTIPATH_VERSION);
826e770bc6bSMatt Jacob 		return (NULL);
827e770bc6bSMatt Jacob 	}
8280c883cefSAlexander Motin 	if (md.md_size != 0 && md.md_size != pp->mediasize)
8290c883cefSAlexander Motin 		return (NULL);
8300c883cefSAlexander Motin 	if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize)
8310c883cefSAlexander Motin 		return (NULL);
83212f35a61SPawel Jakub Dawidek 	if (g_multipath_debug)
833e770bc6bSMatt Jacob 		printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
834e770bc6bSMatt Jacob 
835e770bc6bSMatt Jacob 	/*
836e770bc6bSMatt Jacob 	 * Let's check if such a device already is present. We check against
837e770bc6bSMatt Jacob 	 * uuid alone first because that's the true distinguishor. If that
838e770bc6bSMatt Jacob 	 * passes, then we check for name conflicts. If there are conflicts,
839e770bc6bSMatt Jacob 	 * modify the name.
840e770bc6bSMatt Jacob 	 *
841e770bc6bSMatt Jacob 	 * The whole purpose of this is to solve the problem that people don't
842e770bc6bSMatt Jacob 	 * pick good unique names, but good unique names (like uuids) are a
843e770bc6bSMatt Jacob 	 * pain to use. So, we allow people to build GEOMs with friendly names
844e770bc6bSMatt Jacob 	 * and uuids, and modify the names in case there's a collision.
845e770bc6bSMatt Jacob 	 */
846e770bc6bSMatt Jacob 	sc = NULL;
847e770bc6bSMatt Jacob 	LIST_FOREACH(gp, &mp->geom, geom) {
848e770bc6bSMatt Jacob 		sc = gp->softc;
8490c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
850e770bc6bSMatt Jacob 			continue;
85112f35a61SPawel Jakub Dawidek 		if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
852e770bc6bSMatt Jacob 			break;
853e770bc6bSMatt Jacob 	}
854e770bc6bSMatt Jacob 
855e770bc6bSMatt Jacob 	LIST_FOREACH(gp1, &mp->geom, geom) {
85612f35a61SPawel Jakub Dawidek 		if (gp1 == gp)
857e770bc6bSMatt Jacob 			continue;
858e770bc6bSMatt Jacob 		sc = gp1->softc;
8590c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
860e770bc6bSMatt Jacob 			continue;
86112f35a61SPawel Jakub Dawidek 		if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
862e770bc6bSMatt Jacob 			break;
863e770bc6bSMatt Jacob 	}
864e770bc6bSMatt Jacob 
865e770bc6bSMatt Jacob 	/*
866e770bc6bSMatt Jacob 	 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
867e770bc6bSMatt Jacob 	 *
868e770bc6bSMatt Jacob 	 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
869e770bc6bSMatt Jacob 	 * with the same name (but a different UUID).
870e770bc6bSMatt Jacob 	 *
871e770bc6bSMatt Jacob 	 * If gp is NULL, then modify the name with a random number and
872e770bc6bSMatt Jacob   	 * complain, but allow the creation of the geom to continue.
873e770bc6bSMatt Jacob 	 *
874e770bc6bSMatt Jacob 	 * If gp is *not* NULL, just use the geom's name as we're attaching
875e770bc6bSMatt Jacob 	 * this disk to the (previously generated) name.
876e770bc6bSMatt Jacob 	 */
877e770bc6bSMatt Jacob 
878e770bc6bSMatt Jacob 	if (gp1) {
879e770bc6bSMatt Jacob 		sc = gp1->softc;
880e770bc6bSMatt Jacob 		if (gp == NULL) {
881e770bc6bSMatt Jacob 			char buf[16];
882e770bc6bSMatt Jacob 			u_long rand = random();
883e770bc6bSMatt Jacob 
884e770bc6bSMatt Jacob 			snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
885e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
886e770bc6bSMatt Jacob 			    sc->sc_name, sc->sc_uuid);
887e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
888e770bc6bSMatt Jacob 			    md.md_uuid, buf);
889e770bc6bSMatt Jacob 			strlcpy(md.md_name, buf, sizeof(md.md_name));
890e770bc6bSMatt Jacob 		} else {
891e770bc6bSMatt Jacob 			strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
892e770bc6bSMatt Jacob 		}
893e770bc6bSMatt Jacob 	}
894e770bc6bSMatt Jacob 
895e770bc6bSMatt Jacob 	if (gp == NULL) {
896e770bc6bSMatt Jacob 		gp = g_multipath_create(mp, &md);
897e770bc6bSMatt Jacob 		if (gp == NULL) {
898e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
899e770bc6bSMatt Jacob 			    md.md_name, md.md_uuid);
900e770bc6bSMatt Jacob 			return (NULL);
901e770bc6bSMatt Jacob 		}
902e770bc6bSMatt Jacob 		isnew = 1;
903e770bc6bSMatt Jacob 	} else {
904e770bc6bSMatt Jacob 		isnew = 0;
905e770bc6bSMatt Jacob 	}
906e770bc6bSMatt Jacob 
907e770bc6bSMatt Jacob 	sc = gp->softc;
908e770bc6bSMatt Jacob 	KASSERT(sc != NULL, ("sc is NULL"));
909e770bc6bSMatt Jacob 	error = g_multipath_add_disk(gp, pp);
910e770bc6bSMatt Jacob 	if (error != 0) {
91112f35a61SPawel Jakub Dawidek 		if (isnew)
912e770bc6bSMatt Jacob 			g_multipath_destroy(gp);
913e770bc6bSMatt Jacob 		return (NULL);
914e770bc6bSMatt Jacob 	}
915e770bc6bSMatt Jacob 	return (gp);
916e770bc6bSMatt Jacob }
917e770bc6bSMatt Jacob 
918e770bc6bSMatt Jacob static void
9190c883cefSAlexander Motin g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp,
9200c883cefSAlexander Motin     const char *name)
921e770bc6bSMatt Jacob {
9220c883cefSAlexander Motin 	struct g_multipath_softc *sc;
923e770bc6bSMatt Jacob 	struct g_geom *gp;
9242b4969ffSMatt Jacob 	struct g_consumer *cp;
9250c883cefSAlexander Motin 	struct g_provider *pp;
9260c883cefSAlexander Motin 	const char *mpname;
927e770bc6bSMatt Jacob 	static const char devpf[6] = "/dev/";
928d3fef0a0SAlexander Motin 	int error;
929e770bc6bSMatt Jacob 
930e770bc6bSMatt Jacob 	g_topology_assert();
931e770bc6bSMatt Jacob 
932e770bc6bSMatt Jacob 	mpname = gctl_get_asciiparam(req, "arg0");
933e770bc6bSMatt Jacob         if (mpname == NULL) {
934e770bc6bSMatt Jacob                 gctl_error(req, "No 'arg0' argument");
935e770bc6bSMatt Jacob                 return;
936e770bc6bSMatt Jacob         }
9372b4969ffSMatt Jacob 	gp = g_multipath_find_geom(mp, mpname);
9382b4969ffSMatt Jacob 	if (gp == NULL) {
9392b4969ffSMatt Jacob 		gctl_error(req, "Device %s is invalid", mpname);
940e770bc6bSMatt Jacob 		return;
941e770bc6bSMatt Jacob 	}
9420c883cefSAlexander Motin 	sc = gp->softc;
943e770bc6bSMatt Jacob 
94412f35a61SPawel Jakub Dawidek 	if (strncmp(name, devpf, 5) == 0)
945e770bc6bSMatt Jacob 		name += 5;
9462b4969ffSMatt Jacob 	pp = g_provider_by_name(name);
9472b4969ffSMatt Jacob 	if (pp == NULL) {
948e770bc6bSMatt Jacob 		gctl_error(req, "Provider %s is invalid", name);
949e770bc6bSMatt Jacob 		return;
950e770bc6bSMatt Jacob 	}
951e770bc6bSMatt Jacob 
952e770bc6bSMatt Jacob 	/*
9530c883cefSAlexander Motin 	 * Check to make sure parameters match.
954e770bc6bSMatt Jacob 	 */
9550c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
9560c883cefSAlexander Motin 		if (cp->provider == pp) {
9570c883cefSAlexander Motin 			gctl_error(req, "provider %s is already there",
9580c883cefSAlexander Motin 			    pp->name);
959e770bc6bSMatt Jacob 			return;
960e770bc6bSMatt Jacob 		}
9610c883cefSAlexander Motin 	}
962e6afd72bSAlexander Motin 	if (sc->sc_pp->mediasize != 0 &&
9630c883cefSAlexander Motin 	    sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0)
9640c883cefSAlexander Motin 	     != pp->mediasize) {
9650c883cefSAlexander Motin 		gctl_error(req, "Providers size mismatch %jd != %jd",
9660c883cefSAlexander Motin 		    (intmax_t) sc->sc_pp->mediasize +
9670c883cefSAlexander Motin 			(sc->sc_uuid[0] != 0 ? pp->sectorsize : 0),
9680c883cefSAlexander Motin 		    (intmax_t) pp->mediasize);
969e770bc6bSMatt Jacob 		return;
970e770bc6bSMatt Jacob 	}
971e6afd72bSAlexander Motin 	if (sc->sc_pp->sectorsize != 0 &&
9720c883cefSAlexander Motin 	    sc->sc_pp->sectorsize != pp->sectorsize) {
9730c883cefSAlexander Motin 		gctl_error(req, "Providers sectorsize mismatch %u != %u",
9740c883cefSAlexander Motin 		    sc->sc_pp->sectorsize, pp->sectorsize);
975e770bc6bSMatt Jacob 		return;
976e770bc6bSMatt Jacob 	}
977e770bc6bSMatt Jacob 
978d3fef0a0SAlexander Motin 	error = g_multipath_add_disk(gp, pp);
979d3fef0a0SAlexander Motin 	if (error != 0)
980d3fef0a0SAlexander Motin 		gctl_error(req, "Provider addition error: %d", error);
981e770bc6bSMatt Jacob }
982e770bc6bSMatt Jacob 
9830c883cefSAlexander Motin static void
98471ee4ef0SThomas Quinot g_multipath_ctl_prefer(struct gctl_req *req, struct g_class *mp)
98571ee4ef0SThomas Quinot {
98671ee4ef0SThomas Quinot 	struct g_geom *gp;
98771ee4ef0SThomas Quinot 	struct g_multipath_softc *sc;
98871ee4ef0SThomas Quinot 	struct g_consumer *cp;
98971ee4ef0SThomas Quinot 	const char *name, *mpname;
99071ee4ef0SThomas Quinot 	static const char devpf[6] = "/dev/";
99171ee4ef0SThomas Quinot 	int *nargs;
99271ee4ef0SThomas Quinot 
99371ee4ef0SThomas Quinot 	g_topology_assert();
99471ee4ef0SThomas Quinot 
99571ee4ef0SThomas Quinot 	mpname = gctl_get_asciiparam(req, "arg0");
99671ee4ef0SThomas Quinot         if (mpname == NULL) {
99771ee4ef0SThomas Quinot                 gctl_error(req, "No 'arg0' argument");
99871ee4ef0SThomas Quinot                 return;
99971ee4ef0SThomas Quinot         }
100071ee4ef0SThomas Quinot 	gp = g_multipath_find_geom(mp, mpname);
100171ee4ef0SThomas Quinot 	if (gp == NULL) {
100271ee4ef0SThomas Quinot 		gctl_error(req, "Device %s is invalid", mpname);
100371ee4ef0SThomas Quinot 		return;
100471ee4ef0SThomas Quinot 	}
100571ee4ef0SThomas Quinot 	sc = gp->softc;
100671ee4ef0SThomas Quinot 
100771ee4ef0SThomas Quinot 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
100871ee4ef0SThomas Quinot 	if (nargs == NULL) {
100971ee4ef0SThomas Quinot 		gctl_error(req, "No 'nargs' argument");
101071ee4ef0SThomas Quinot 		return;
101171ee4ef0SThomas Quinot 	}
101271ee4ef0SThomas Quinot 	if (*nargs != 2) {
101371ee4ef0SThomas Quinot 		gctl_error(req, "missing device");
101471ee4ef0SThomas Quinot 		return;
101571ee4ef0SThomas Quinot 	}
101671ee4ef0SThomas Quinot 
101771ee4ef0SThomas Quinot 	name = gctl_get_asciiparam(req, "arg1");
101871ee4ef0SThomas Quinot 	if (name == NULL) {
101971ee4ef0SThomas Quinot 		gctl_error(req, "No 'arg1' argument");
102071ee4ef0SThomas Quinot 		return;
102171ee4ef0SThomas Quinot 	}
102271ee4ef0SThomas Quinot 	if (strncmp(name, devpf, 5) == 0) {
102371ee4ef0SThomas Quinot 		name += 5;
102471ee4ef0SThomas Quinot 	}
102571ee4ef0SThomas Quinot 
102671ee4ef0SThomas Quinot 	LIST_FOREACH(cp, &gp->consumer, consumer) {
102771ee4ef0SThomas Quinot 		if (cp->provider != NULL
102871ee4ef0SThomas Quinot                       && strcmp(cp->provider->name, name) == 0)
102971ee4ef0SThomas Quinot 		    break;
103071ee4ef0SThomas Quinot 	}
103171ee4ef0SThomas Quinot 
103271ee4ef0SThomas Quinot 	if (cp == NULL) {
103371ee4ef0SThomas Quinot 		gctl_error(req, "Provider %s not found", name);
103471ee4ef0SThomas Quinot 		return;
103571ee4ef0SThomas Quinot 	}
103671ee4ef0SThomas Quinot 
103771ee4ef0SThomas Quinot 	mtx_lock(&sc->sc_mtx);
103871ee4ef0SThomas Quinot 
103971ee4ef0SThomas Quinot 	if (cp->index & MP_BAD) {
104071ee4ef0SThomas Quinot 		gctl_error(req, "Consumer %s is invalid", name);
104171ee4ef0SThomas Quinot 		mtx_unlock(&sc->sc_mtx);
104271ee4ef0SThomas Quinot 		return;
104371ee4ef0SThomas Quinot 	}
104471ee4ef0SThomas Quinot 
104571ee4ef0SThomas Quinot 	/* Here when the consumer is present and in good shape */
104671ee4ef0SThomas Quinot 
104771ee4ef0SThomas Quinot 	sc->sc_active = cp;
104871ee4ef0SThomas Quinot 	if (!sc->sc_active_active)
104971ee4ef0SThomas Quinot 	    printf("GEOM_MULTIPATH: %s now active path in %s\n",
105071ee4ef0SThomas Quinot 		sc->sc_active->provider->name, sc->sc_name);
105171ee4ef0SThomas Quinot 
105271ee4ef0SThomas Quinot 	mtx_unlock(&sc->sc_mtx);
105371ee4ef0SThomas Quinot }
105471ee4ef0SThomas Quinot 
105571ee4ef0SThomas Quinot static void
10560c883cefSAlexander Motin g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp)
10570c883cefSAlexander Motin {
10580c883cefSAlexander Motin 	struct g_multipath_softc *sc;
10590c883cefSAlexander Motin 	struct g_geom *gp;
10600c883cefSAlexander Motin 	const char *mpname, *name;
10610c883cefSAlexander Motin 
10620c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
10630c883cefSAlexander Motin         if (mpname == NULL) {
10640c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
10650c883cefSAlexander Motin                 return;
10660c883cefSAlexander Motin         }
10670c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
10680c883cefSAlexander Motin 	if (gp == NULL) {
10690c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
10700c883cefSAlexander Motin 		return;
10710c883cefSAlexander Motin 	}
10720c883cefSAlexander Motin 	sc = gp->softc;
10730c883cefSAlexander Motin 
10740c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
10750c883cefSAlexander Motin 	if (name == NULL) {
10760c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
10770c883cefSAlexander Motin 		return;
10780c883cefSAlexander Motin 	}
10790c883cefSAlexander Motin 	g_multipath_ctl_add_name(req, mp, name);
10800c883cefSAlexander Motin }
10810c883cefSAlexander Motin 
10820c883cefSAlexander Motin static void
10830c883cefSAlexander Motin g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp)
10840c883cefSAlexander Motin {
10850c883cefSAlexander Motin 	struct g_multipath_metadata md;
10860c883cefSAlexander Motin 	struct g_multipath_softc *sc;
10870c883cefSAlexander Motin 	struct g_geom *gp;
10880c883cefSAlexander Motin 	const char *mpname, *name;
10890c883cefSAlexander Motin 	char param[16];
109063297dfdSAlexander Motin 	int *nargs, i, *val;
10910c883cefSAlexander Motin 
10920c883cefSAlexander Motin 	g_topology_assert();
10930c883cefSAlexander Motin 
10940c883cefSAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
10950c883cefSAlexander Motin 	if (*nargs < 2) {
10960c883cefSAlexander Motin 		gctl_error(req, "wrong number of arguments.");
10970c883cefSAlexander Motin 		return;
10980c883cefSAlexander Motin 	}
10990c883cefSAlexander Motin 
11000c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
11010c883cefSAlexander Motin         if (mpname == NULL) {
11020c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
11030c883cefSAlexander Motin                 return;
11040c883cefSAlexander Motin         }
11050c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
11060c883cefSAlexander Motin 	if (gp != NULL) {
11070c883cefSAlexander Motin 		gctl_error(req, "Device %s already exist", mpname);
11080c883cefSAlexander Motin 		return;
11090c883cefSAlexander Motin 	}
11100c883cefSAlexander Motin 
11110c883cefSAlexander Motin 	memset(&md, 0, sizeof(md));
11120c883cefSAlexander Motin 	strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
11130c883cefSAlexander Motin 	md.md_version = G_MULTIPATH_VERSION;
11140c883cefSAlexander Motin 	strlcpy(md.md_name, mpname, sizeof(md.md_name));
11150c883cefSAlexander Motin 	md.md_size = 0;
11160c883cefSAlexander Motin 	md.md_sectorsize = 0;
11170c883cefSAlexander Motin 	md.md_uuid[0] = 0;
111863297dfdSAlexander Motin 	md.md_active_active = 0;
111963297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_active", sizeof(*val));
112063297dfdSAlexander Motin 	if (val != NULL && *val != 0)
112163297dfdSAlexander Motin 		md.md_active_active = 1;
112263297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_read", sizeof(*val));
112363297dfdSAlexander Motin 	if (val != NULL && *val != 0)
112463297dfdSAlexander Motin 		md.md_active_active = 2;
11250c883cefSAlexander Motin 	gp = g_multipath_create(mp, &md);
11260c883cefSAlexander Motin 	if (gp == NULL) {
11270c883cefSAlexander Motin 		gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n",
11280c883cefSAlexander Motin 		    md.md_name, md.md_uuid);
11290c883cefSAlexander Motin 		return;
11300c883cefSAlexander Motin 	}
11310c883cefSAlexander Motin 	sc = gp->softc;
11320c883cefSAlexander Motin 
11330c883cefSAlexander Motin 	for (i = 1; i < *nargs; i++) {
11340c883cefSAlexander Motin 		snprintf(param, sizeof(param), "arg%d", i);
11350c883cefSAlexander Motin 		name = gctl_get_asciiparam(req, param);
11360c883cefSAlexander Motin 		g_multipath_ctl_add_name(req, mp, name);
11370c883cefSAlexander Motin 	}
11380c883cefSAlexander Motin 
11390c883cefSAlexander Motin 	if (sc->sc_ndisks != (*nargs - 1))
11400c883cefSAlexander Motin 		g_multipath_destroy(gp);
11410c883cefSAlexander Motin }
11420c883cefSAlexander Motin 
11430c883cefSAlexander Motin static void
114463297dfdSAlexander Motin g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp)
114563297dfdSAlexander Motin {
114663297dfdSAlexander Motin 	struct g_multipath_softc *sc;
114763297dfdSAlexander Motin 	struct g_geom *gp;
114863297dfdSAlexander Motin 	struct g_consumer *cp;
114963297dfdSAlexander Motin 	struct g_provider *pp;
1150c0b1ef66SAlexander Motin 	struct g_multipath_metadata md;
115163297dfdSAlexander Motin 	const char *name;
115263297dfdSAlexander Motin 	int error, *val;
115363297dfdSAlexander Motin 
115463297dfdSAlexander Motin 	g_topology_assert();
115563297dfdSAlexander Motin 
115663297dfdSAlexander Motin 	name = gctl_get_asciiparam(req, "arg0");
115763297dfdSAlexander Motin 	if (name == NULL) {
115863297dfdSAlexander Motin 		gctl_error(req, "No 'arg0' argument");
115963297dfdSAlexander Motin 		return;
116063297dfdSAlexander Motin 	}
116163297dfdSAlexander Motin 	gp = g_multipath_find_geom(mp, name);
116263297dfdSAlexander Motin 	if (gp == NULL) {
116363297dfdSAlexander Motin 		gctl_error(req, "Device %s is invalid", name);
116463297dfdSAlexander Motin 		return;
116563297dfdSAlexander Motin 	}
116663297dfdSAlexander Motin 	sc = gp->softc;
116763297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_active", sizeof(*val));
116863297dfdSAlexander Motin 	if (val != NULL && *val != 0)
116963297dfdSAlexander Motin 		sc->sc_active_active = 1;
117063297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_read", sizeof(*val));
117163297dfdSAlexander Motin 	if (val != NULL && *val != 0)
117263297dfdSAlexander Motin 		sc->sc_active_active = 2;
117363297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_passive", sizeof(*val));
117463297dfdSAlexander Motin 	if (val != NULL && *val != 0)
117563297dfdSAlexander Motin 		sc->sc_active_active = 0;
117663297dfdSAlexander Motin 	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
117763297dfdSAlexander Motin 		cp = sc->sc_active;
117863297dfdSAlexander Motin 		pp = cp->provider;
1179c0b1ef66SAlexander Motin 		strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1180c0b1ef66SAlexander Motin 		memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
1181c0b1ef66SAlexander Motin 		strlcpy(md.md_name, name, sizeof(md.md_name));
1182c0b1ef66SAlexander Motin 		md.md_version = G_MULTIPATH_VERSION;
1183c0b1ef66SAlexander Motin 		md.md_size = pp->mediasize;
1184c0b1ef66SAlexander Motin 		md.md_sectorsize = pp->sectorsize;
1185c0b1ef66SAlexander Motin 		md.md_active_active = sc->sc_active_active;
1186f8c79813SAlexander Motin 		error = g_multipath_write_metadata(cp, &md);
118763297dfdSAlexander Motin 		if (error != 0)
118863297dfdSAlexander Motin 			gctl_error(req, "Can't update metadata on %s (%d)",
118963297dfdSAlexander Motin 			    pp->name, error);
119063297dfdSAlexander Motin 	}
119163297dfdSAlexander Motin }
119263297dfdSAlexander Motin 
119363297dfdSAlexander Motin static void
11940c883cefSAlexander Motin g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail)
11950c883cefSAlexander Motin {
11960c883cefSAlexander Motin 	struct g_multipath_softc *sc;
11970c883cefSAlexander Motin 	struct g_geom *gp;
11980c883cefSAlexander Motin 	struct g_consumer *cp;
11990c883cefSAlexander Motin 	const char *mpname, *name;
12000c883cefSAlexander Motin 	int found;
12010c883cefSAlexander Motin 
12020c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
12030c883cefSAlexander Motin         if (mpname == NULL) {
12040c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
12050c883cefSAlexander Motin                 return;
12060c883cefSAlexander Motin         }
12070c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
12080c883cefSAlexander Motin 	if (gp == NULL) {
12090c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
12100c883cefSAlexander Motin 		return;
12110c883cefSAlexander Motin 	}
12120c883cefSAlexander Motin 	sc = gp->softc;
12130c883cefSAlexander Motin 
12140c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
12150c883cefSAlexander Motin 	if (name == NULL) {
12160c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
12170c883cefSAlexander Motin 		return;
12180c883cefSAlexander Motin 	}
12190c883cefSAlexander Motin 
12200c883cefSAlexander Motin 	found = 0;
12210c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
12220c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
12230c883cefSAlexander Motin 		if (cp->provider != NULL &&
12240c883cefSAlexander Motin 		    strcmp(cp->provider->name, name) == 0 &&
12250c883cefSAlexander Motin 		    (cp->index & MP_LOST) == 0) {
12260c883cefSAlexander Motin 			found = 1;
122763297dfdSAlexander Motin 			if (!fail == !(cp->index & MP_FAIL))
122863297dfdSAlexander Motin 				continue;
12290c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: %s in %s is marked %s.\n",
12300c883cefSAlexander Motin 				name, sc->sc_name, fail ? "FAIL" : "OK");
12310c883cefSAlexander Motin 			if (fail) {
12320c883cefSAlexander Motin 				g_multipath_fault(cp, MP_FAIL);
12330c883cefSAlexander Motin 			} else {
12340c883cefSAlexander Motin 				cp->index &= ~MP_FAIL;
12350c883cefSAlexander Motin 			}
12360c883cefSAlexander Motin 		}
12370c883cefSAlexander Motin 	}
12380c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
12390c883cefSAlexander Motin 	if (found == 0)
12400c883cefSAlexander Motin 		gctl_error(req, "Provider %s not found", name);
12410c883cefSAlexander Motin }
12420c883cefSAlexander Motin 
12430c883cefSAlexander Motin static void
12440c883cefSAlexander Motin g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp)
12450c883cefSAlexander Motin {
12460c883cefSAlexander Motin 	struct g_multipath_softc *sc;
12470c883cefSAlexander Motin 	struct g_geom *gp;
12480c883cefSAlexander Motin 	struct g_consumer *cp, *cp1;
12490c883cefSAlexander Motin 	const char *mpname, *name;
12500c883cefSAlexander Motin 	uintptr_t *cnt;
12510c883cefSAlexander Motin 	int found;
12520c883cefSAlexander Motin 
12530c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
12540c883cefSAlexander Motin         if (mpname == NULL) {
12550c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
12560c883cefSAlexander Motin                 return;
12570c883cefSAlexander Motin         }
12580c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
12590c883cefSAlexander Motin 	if (gp == NULL) {
12600c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
12610c883cefSAlexander Motin 		return;
12620c883cefSAlexander Motin 	}
12630c883cefSAlexander Motin 	sc = gp->softc;
12640c883cefSAlexander Motin 
12650c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
12660c883cefSAlexander Motin 	if (name == NULL) {
12670c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
12680c883cefSAlexander Motin 		return;
12690c883cefSAlexander Motin 	}
12700c883cefSAlexander Motin 
12710c883cefSAlexander Motin 	found = 0;
12720c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
12730c883cefSAlexander Motin 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
12740c883cefSAlexander Motin 		if (cp->provider != NULL &&
12750c883cefSAlexander Motin 		    strcmp(cp->provider->name, name) == 0 &&
12760c883cefSAlexander Motin 		    (cp->index & MP_LOST) == 0) {
12770c883cefSAlexander Motin 			found = 1;
12780c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: removing %s from %s\n",
12790c883cefSAlexander Motin 			    cp->provider->name, cp->geom->name);
12800c883cefSAlexander Motin 			sc->sc_ndisks--;
12810c883cefSAlexander Motin 			g_multipath_fault(cp, MP_LOST);
12820c883cefSAlexander Motin 			cnt = (uintptr_t *)&cp->private;
12830c883cefSAlexander Motin 			if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
12840c883cefSAlexander Motin 				cp->index |= MP_POSTED;
12850c883cefSAlexander Motin 				mtx_unlock(&sc->sc_mtx);
12860c883cefSAlexander Motin 				g_mpd(cp, 0);
12870c883cefSAlexander Motin 				if (cp1 == NULL)
12880c883cefSAlexander Motin 					return;	/* Recursion happened. */
12890c883cefSAlexander Motin 				mtx_lock(&sc->sc_mtx);
12900c883cefSAlexander Motin 			}
12910c883cefSAlexander Motin 		}
12920c883cefSAlexander Motin 	}
12930c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
12940c883cefSAlexander Motin 	if (found == 0)
12950c883cefSAlexander Motin 		gctl_error(req, "Provider %s not found", name);
12960c883cefSAlexander Motin }
12970c883cefSAlexander Motin 
1298e770bc6bSMatt Jacob static struct g_geom *
1299e770bc6bSMatt Jacob g_multipath_find_geom(struct g_class *mp, const char *name)
1300e770bc6bSMatt Jacob {
1301e770bc6bSMatt Jacob 	struct g_geom *gp;
13020c883cefSAlexander Motin 	struct g_multipath_softc *sc;
1303e770bc6bSMatt Jacob 
1304e770bc6bSMatt Jacob 	LIST_FOREACH(gp, &mp->geom, geom) {
13050c883cefSAlexander Motin 		sc = gp->softc;
13060c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
13070c883cefSAlexander Motin 			continue;
13080c883cefSAlexander Motin 		if (strcmp(gp->name, name) == 0)
1309e770bc6bSMatt Jacob 			return (gp);
1310e770bc6bSMatt Jacob 	}
1311e770bc6bSMatt Jacob 	return (NULL);
1312e770bc6bSMatt Jacob }
1313e770bc6bSMatt Jacob 
1314e770bc6bSMatt Jacob static void
13150c883cefSAlexander Motin g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp)
1316e770bc6bSMatt Jacob {
1317e770bc6bSMatt Jacob 	struct g_geom *gp;
1318e770bc6bSMatt Jacob 	const char *name;
1319e770bc6bSMatt Jacob 	int error;
1320e770bc6bSMatt Jacob 
1321e770bc6bSMatt Jacob 	g_topology_assert();
1322e770bc6bSMatt Jacob 
1323e770bc6bSMatt Jacob 	name = gctl_get_asciiparam(req, "arg0");
1324e770bc6bSMatt Jacob         if (name == NULL) {
1325e770bc6bSMatt Jacob                 gctl_error(req, "No 'arg0' argument");
1326e770bc6bSMatt Jacob                 return;
1327e770bc6bSMatt Jacob         }
1328e770bc6bSMatt Jacob 	gp = g_multipath_find_geom(mp, name);
1329e770bc6bSMatt Jacob 	if (gp == NULL) {
1330e770bc6bSMatt Jacob 		gctl_error(req, "Device %s is invalid", name);
1331e770bc6bSMatt Jacob 		return;
1332e770bc6bSMatt Jacob 	}
1333e770bc6bSMatt Jacob 	error = g_multipath_destroy(gp);
13340c883cefSAlexander Motin 	if (error != 0 && error != EINPROGRESS)
13350c883cefSAlexander Motin 		gctl_error(req, "failed to stop %s (err=%d)", name, error);
1336e770bc6bSMatt Jacob }
13370c883cefSAlexander Motin 
13380c883cefSAlexander Motin static void
13390c883cefSAlexander Motin g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
13400c883cefSAlexander Motin {
13410c883cefSAlexander Motin 	struct g_geom *gp;
13420c883cefSAlexander Motin 	struct g_multipath_softc *sc;
13430c883cefSAlexander Motin 	struct g_consumer *cp;
13440c883cefSAlexander Motin 	struct g_provider *pp;
13450c883cefSAlexander Motin 	const char *name;
13460c883cefSAlexander Motin 	uint8_t *buf;
13470c883cefSAlexander Motin 	int error;
13480c883cefSAlexander Motin 
13490c883cefSAlexander Motin 	g_topology_assert();
13500c883cefSAlexander Motin 
13510c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg0");
13520c883cefSAlexander Motin         if (name == NULL) {
13530c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
13540c883cefSAlexander Motin                 return;
13550c883cefSAlexander Motin         }
13560c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, name);
13570c883cefSAlexander Motin 	if (gp == NULL) {
13580c883cefSAlexander Motin 		gctl_error(req, "Device %s is invalid", name);
13590c883cefSAlexander Motin 		return;
13600c883cefSAlexander Motin 	}
13610c883cefSAlexander Motin 	sc = gp->softc;
13620c883cefSAlexander Motin 
13630c883cefSAlexander Motin 	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
13640c883cefSAlexander Motin 		cp = sc->sc_active;
13650c883cefSAlexander Motin 		pp = cp->provider;
13660c883cefSAlexander Motin 		error = g_access(cp, 1, 1, 1);
13670c883cefSAlexander Motin 		if (error != 0) {
13680c883cefSAlexander Motin 			gctl_error(req, "Can't open %s (%d)", pp->name, error);
13690c883cefSAlexander Motin 			goto destroy;
13700c883cefSAlexander Motin 		}
13710c883cefSAlexander Motin 		g_topology_unlock();
13720c883cefSAlexander Motin 		buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
13730c883cefSAlexander Motin 		error = g_write_data(cp, pp->mediasize - pp->sectorsize,
13740c883cefSAlexander Motin 		    buf, pp->sectorsize);
13750c883cefSAlexander Motin 		g_topology_lock();
13760c883cefSAlexander Motin 		g_access(cp, -1, -1, -1);
13770c883cefSAlexander Motin 		if (error != 0)
13780c883cefSAlexander Motin 			gctl_error(req, "Can't erase metadata on %s (%d)",
13790c883cefSAlexander Motin 			    pp->name, error);
13800c883cefSAlexander Motin 	}
13810c883cefSAlexander Motin 
13820c883cefSAlexander Motin destroy:
13830c883cefSAlexander Motin 	error = g_multipath_destroy(gp);
13840c883cefSAlexander Motin 	if (error != 0 && error != EINPROGRESS)
13850c883cefSAlexander Motin 		gctl_error(req, "failed to destroy %s (err=%d)", name, error);
1386e770bc6bSMatt Jacob }
1387e770bc6bSMatt Jacob 
1388e770bc6bSMatt Jacob static void
1389b5dce617SMatt Jacob g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp)
1390b5dce617SMatt Jacob {
1391b5dce617SMatt Jacob 	struct g_geom *gp;
1392b5dce617SMatt Jacob 	const char *name;
1393b5dce617SMatt Jacob 	int error;
1394b5dce617SMatt Jacob 
1395b5dce617SMatt Jacob 	g_topology_assert();
1396b5dce617SMatt Jacob 
1397b5dce617SMatt Jacob 	name = gctl_get_asciiparam(req, "arg0");
1398b5dce617SMatt Jacob         if (name == NULL) {
1399b5dce617SMatt Jacob                 gctl_error(req, "No 'arg0' argument");
1400b5dce617SMatt Jacob                 return;
1401b5dce617SMatt Jacob         }
1402b5dce617SMatt Jacob 	gp = g_multipath_find_geom(mp, name);
1403b5dce617SMatt Jacob 	if (gp == NULL) {
1404b5dce617SMatt Jacob 		gctl_error(req, "Device %s is invalid", name);
1405b5dce617SMatt Jacob 		return;
1406b5dce617SMatt Jacob 	}
1407b5dce617SMatt Jacob 	error = g_multipath_rotate(gp);
1408b5dce617SMatt Jacob 	if (error != 0) {
1409b5dce617SMatt Jacob 		gctl_error(req, "failed to rotate %s (err=%d)", name, error);
1410b5dce617SMatt Jacob 	}
1411b5dce617SMatt Jacob }
1412b5dce617SMatt Jacob 
1413b5dce617SMatt Jacob static void
1414b5dce617SMatt Jacob g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp)
1415b5dce617SMatt Jacob {
1416b5dce617SMatt Jacob 	struct sbuf *sb;
1417b5dce617SMatt Jacob 	struct g_geom *gp;
1418b5dce617SMatt Jacob 	struct g_multipath_softc *sc;
14190c883cefSAlexander Motin 	struct g_consumer *cp;
1420b5dce617SMatt Jacob 	const char *name;
14210c883cefSAlexander Motin 	int empty;
1422b5dce617SMatt Jacob 
1423b5dce617SMatt Jacob 	sb = sbuf_new_auto();
1424b5dce617SMatt Jacob 
1425b5dce617SMatt Jacob 	g_topology_assert();
1426b5dce617SMatt Jacob 	name = gctl_get_asciiparam(req, "arg0");
1427b5dce617SMatt Jacob         if (name == NULL) {
1428b5dce617SMatt Jacob                 gctl_error(req, "No 'arg0' argument");
1429b5dce617SMatt Jacob                 return;
1430b5dce617SMatt Jacob         }
1431b5dce617SMatt Jacob 	gp = g_multipath_find_geom(mp, name);
1432b5dce617SMatt Jacob 	if (gp == NULL) {
1433b5dce617SMatt Jacob 		gctl_error(req, "Device %s is invalid", name);
1434b5dce617SMatt Jacob 		return;
1435b5dce617SMatt Jacob 	}
1436b5dce617SMatt Jacob 	sc = gp->softc;
143763297dfdSAlexander Motin 	if (sc->sc_active_active == 1) {
14380c883cefSAlexander Motin 		empty = 1;
14390c883cefSAlexander Motin 		LIST_FOREACH(cp, &gp->consumer, consumer) {
14400c883cefSAlexander Motin 			if (cp->index & MP_BAD)
14410c883cefSAlexander Motin 				continue;
14420c883cefSAlexander Motin 			if (!empty)
14430c883cefSAlexander Motin 				sbuf_cat(sb, " ");
14440c883cefSAlexander Motin 			sbuf_cat(sb, cp->provider->name);
14450c883cefSAlexander Motin 			empty = 0;
14460c883cefSAlexander Motin 		}
14470c883cefSAlexander Motin 		if (empty)
14480c883cefSAlexander Motin 			sbuf_cat(sb, "none");
14490c883cefSAlexander Motin 		sbuf_cat(sb, "\n");
14500c883cefSAlexander Motin 	} else if (sc->sc_active && sc->sc_active->provider) {
14510c883cefSAlexander Motin 		sbuf_printf(sb, "%s\n", sc->sc_active->provider->name);
1452b5dce617SMatt Jacob 	} else {
1453*49ee0fceSAlexander Motin 		sbuf_cat(sb, "none\n");
1454b5dce617SMatt Jacob 	}
1455b5dce617SMatt Jacob 	sbuf_finish(sb);
1456b5dce617SMatt Jacob 	gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1457b5dce617SMatt Jacob 	sbuf_delete(sb);
1458b5dce617SMatt Jacob }
1459b5dce617SMatt Jacob 
1460b5dce617SMatt Jacob static void
1461e770bc6bSMatt Jacob g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1462e770bc6bSMatt Jacob {
1463e770bc6bSMatt Jacob 	uint32_t *version;
1464e770bc6bSMatt Jacob 	g_topology_assert();
1465e770bc6bSMatt Jacob 	version = gctl_get_paraml(req, "version", sizeof(*version));
1466e770bc6bSMatt Jacob 	if (version == NULL) {
1467e770bc6bSMatt Jacob 		gctl_error(req, "No 'version' argument");
1468e770bc6bSMatt Jacob 	} else if (*version != G_MULTIPATH_VERSION) {
1469e770bc6bSMatt Jacob 		gctl_error(req, "Userland and kernel parts are out of sync");
14702b4969ffSMatt Jacob 	} else if (strcmp(verb, "add") == 0) {
14712b4969ffSMatt Jacob 		g_multipath_ctl_add(req, mp);
147271ee4ef0SThomas Quinot 	} else if (strcmp(verb, "prefer") == 0) {
147371ee4ef0SThomas Quinot 		g_multipath_ctl_prefer(req, mp);
14740c883cefSAlexander Motin 	} else if (strcmp(verb, "create") == 0) {
14750c883cefSAlexander Motin 		g_multipath_ctl_create(req, mp);
147663297dfdSAlexander Motin 	} else if (strcmp(verb, "configure") == 0) {
147763297dfdSAlexander Motin 		g_multipath_ctl_configure(req, mp);
14780c883cefSAlexander Motin 	} else if (strcmp(verb, "stop") == 0) {
14790c883cefSAlexander Motin 		g_multipath_ctl_stop(req, mp);
1480e770bc6bSMatt Jacob 	} else if (strcmp(verb, "destroy") == 0) {
1481e770bc6bSMatt Jacob 		g_multipath_ctl_destroy(req, mp);
14820c883cefSAlexander Motin 	} else if (strcmp(verb, "fail") == 0) {
14830c883cefSAlexander Motin 		g_multipath_ctl_fail(req, mp, 1);
14840c883cefSAlexander Motin 	} else if (strcmp(verb, "restore") == 0) {
14850c883cefSAlexander Motin 		g_multipath_ctl_fail(req, mp, 0);
14860c883cefSAlexander Motin 	} else if (strcmp(verb, "remove") == 0) {
14870c883cefSAlexander Motin 		g_multipath_ctl_remove(req, mp);
1488b5dce617SMatt Jacob 	} else if (strcmp(verb, "rotate") == 0) {
1489b5dce617SMatt Jacob 		g_multipath_ctl_rotate(req, mp);
1490b5dce617SMatt Jacob 	} else if (strcmp(verb, "getactive") == 0) {
1491b5dce617SMatt Jacob 		g_multipath_ctl_getactive(req, mp);
1492e770bc6bSMatt Jacob 	} else {
1493e770bc6bSMatt Jacob 		gctl_error(req, "Unknown verb %s", verb);
1494e770bc6bSMatt Jacob 	}
1495e770bc6bSMatt Jacob }
14960c883cefSAlexander Motin 
14970c883cefSAlexander Motin static void
14980c883cefSAlexander Motin g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
14990c883cefSAlexander Motin     struct g_consumer *cp, struct g_provider *pp)
15000c883cefSAlexander Motin {
15010c883cefSAlexander Motin 	struct g_multipath_softc *sc;
15020c883cefSAlexander Motin 	int good;
15030c883cefSAlexander Motin 
15040c883cefSAlexander Motin 	g_topology_assert();
15050c883cefSAlexander Motin 
15060c883cefSAlexander Motin 	sc = gp->softc;
15070c883cefSAlexander Motin 	if (sc == NULL)
15080c883cefSAlexander Motin 		return;
15090c883cefSAlexander Motin 	if (cp != NULL) {
1510a839e332SAlexander Motin 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
15110c883cefSAlexander Motin 		    (cp->index & MP_NEW) ? "NEW" :
15120c883cefSAlexander Motin 		    (cp->index & MP_LOST) ? "LOST" :
15130c883cefSAlexander Motin 		    (cp->index & MP_FAIL) ? "FAIL" :
151463297dfdSAlexander Motin 		    (sc->sc_active_active == 1 || sc->sc_active == cp) ?
151563297dfdSAlexander Motin 		     "ACTIVE" :
151663297dfdSAlexander Motin 		     sc->sc_active_active == 2 ? "READ" : "PASSIVE");
15170c883cefSAlexander Motin 	} else {
15180c883cefSAlexander Motin 		good = g_multipath_good(gp);
1519a839e332SAlexander Motin 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
15200c883cefSAlexander Motin 		    good == 0 ? "BROKEN" :
15210c883cefSAlexander Motin 		    (good != sc->sc_ndisks || sc->sc_ndisks == 1) ?
15220c883cefSAlexander Motin 		    "DEGRADED" : "OPTIMAL");
15230c883cefSAlexander Motin 	}
15240c883cefSAlexander Motin 	if (cp == NULL && pp == NULL) {
1525a839e332SAlexander Motin 		sbuf_printf(sb, "%s<UUID>%s</UUID>\n", indent, sc->sc_uuid);
1526a839e332SAlexander Motin 		sbuf_printf(sb, "%s<Mode>Active/%s</Mode>\n", indent,
152763297dfdSAlexander Motin 		    sc->sc_active_active == 2 ? "Read" :
152863297dfdSAlexander Motin 		    sc->sc_active_active == 1 ? "Active" : "Passive");
1529a839e332SAlexander Motin 		sbuf_printf(sb, "%s<Type>%s</Type>\n", indent,
15300c883cefSAlexander Motin 		    sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC");
15310c883cefSAlexander Motin 	}
15320c883cefSAlexander Motin }
15330c883cefSAlexander Motin 
1534e770bc6bSMatt Jacob DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);
153574d6c131SKyle Evans MODULE_VERSION(geom_multipath, 0);
1536