xref: /freebsd/sys/geom/multipath/g_multipath.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1e770bc6bSMatt Jacob /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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/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>
4467f72211SAlan Somers #include <sys/sdt.h>
45e770bc6bSMatt Jacob #include <sys/sysctl.h>
46e770bc6bSMatt Jacob #include <sys/kthread.h>
47e770bc6bSMatt Jacob #include <sys/malloc.h>
48e770bc6bSMatt Jacob #include <geom/geom.h>
49e770bc6bSMatt Jacob #include <geom/multipath/g_multipath.h>
50e770bc6bSMatt Jacob 
51cb08c2ccSAlexander Leidinger FEATURE(geom_multipath, "GEOM multipath support");
52e770bc6bSMatt Jacob 
53e770bc6bSMatt Jacob SYSCTL_DECL(_kern_geom);
547029da5cSPawel Biernacki static SYSCTL_NODE(_kern_geom, OID_AUTO, multipath,
557029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 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 
6467f72211SAlan Somers SDT_PROVIDER_DECLARE(geom);
6567f72211SAlan Somers SDT_PROBE_DEFINE2(geom, multipath, config, restore, "char*", "char*");
6667f72211SAlan Somers SDT_PROBE_DEFINE2(geom, multipath, config, remove, "char*", "char*");
6767f72211SAlan Somers SDT_PROBE_DEFINE2(geom, multipath, config, disconnect, "char*", "char*");
6867f72211SAlan Somers SDT_PROBE_DEFINE3(geom, multipath, config, fail, "char*", "char*", "int");
6967f72211SAlan Somers SDT_PROBE_DEFINE2(geom, multipath, config, taste, "char*", "char*");
7067f72211SAlan Somers SDT_PROBE_DEFINE2(geom, multipath, io, restart, "struct bio*", "struct bio*");
7167f72211SAlan Somers 
72e770bc6bSMatt Jacob static enum {
73e770bc6bSMatt Jacob 	GKT_NIL,
74e770bc6bSMatt Jacob 	GKT_RUN,
75e770bc6bSMatt Jacob 	GKT_DIE
76e770bc6bSMatt Jacob } g_multipath_kt_state;
77e770bc6bSMatt Jacob static struct bio_queue_head gmtbq;
78e770bc6bSMatt Jacob static struct mtx gmtbq_mtx;
79e770bc6bSMatt Jacob 
80f8c79813SAlexander Motin static int g_multipath_read_metadata(struct g_consumer *cp,
81f8c79813SAlexander Motin     struct g_multipath_metadata *md);
82f8c79813SAlexander Motin static int g_multipath_write_metadata(struct g_consumer *cp,
83f8c79813SAlexander Motin     struct g_multipath_metadata *md);
84f8c79813SAlexander Motin 
85e770bc6bSMatt Jacob static void g_multipath_orphan(struct g_consumer *);
86e6afd72bSAlexander Motin static void g_multipath_resize(struct g_consumer *);
87e770bc6bSMatt Jacob static void g_multipath_start(struct bio *);
88e770bc6bSMatt Jacob static void g_multipath_done(struct bio *);
89e770bc6bSMatt Jacob static void g_multipath_done_error(struct bio *);
90e770bc6bSMatt Jacob static void g_multipath_kt(void *);
91e770bc6bSMatt Jacob 
92e770bc6bSMatt Jacob static int g_multipath_destroy(struct g_geom *);
93e770bc6bSMatt Jacob static int
94e770bc6bSMatt Jacob g_multipath_destroy_geom(struct gctl_req *, struct g_class *, struct g_geom *);
95e770bc6bSMatt Jacob 
962b4969ffSMatt Jacob static struct g_geom *g_multipath_find_geom(struct g_class *, const char *);
97b5dce617SMatt Jacob static int g_multipath_rotate(struct g_geom *);
98b5dce617SMatt Jacob 
99e770bc6bSMatt Jacob static g_taste_t g_multipath_taste;
100e770bc6bSMatt Jacob static g_ctl_req_t g_multipath_config;
101e770bc6bSMatt Jacob static g_init_t g_multipath_init;
102e770bc6bSMatt Jacob static g_fini_t g_multipath_fini;
1030c883cefSAlexander Motin static g_dumpconf_t g_multipath_dumpconf;
104e770bc6bSMatt Jacob 
105e770bc6bSMatt Jacob struct g_class g_multipath_class = {
106e770bc6bSMatt Jacob 	.name		= G_MULTIPATH_CLASS_NAME,
107e770bc6bSMatt Jacob 	.version	= G_VERSION,
108e770bc6bSMatt Jacob 	.ctlreq		= g_multipath_config,
109e770bc6bSMatt Jacob 	.taste		= g_multipath_taste,
110e770bc6bSMatt Jacob 	.destroy_geom	= g_multipath_destroy_geom,
111e770bc6bSMatt Jacob 	.init		= g_multipath_init,
112e770bc6bSMatt Jacob 	.fini		= g_multipath_fini
113e770bc6bSMatt Jacob };
114e770bc6bSMatt Jacob 
1150c883cefSAlexander Motin #define	MP_FAIL		0x00000001
1160c883cefSAlexander Motin #define	MP_LOST		0x00000002
1170c883cefSAlexander Motin #define	MP_NEW		0x00000004
1180c883cefSAlexander Motin #define	MP_POSTED	0x00000008
1190c883cefSAlexander Motin #define	MP_BAD		(MP_FAIL | MP_LOST | MP_NEW)
12025080ac4SSteven Hartland #define	MP_WITHER	0x00000010
12125080ac4SSteven Hartland #define	MP_IDLE		0x00000020
12225080ac4SSteven Hartland #define	MP_IDLE_MASK	0xffffffe0
1230c883cefSAlexander Motin 
1240c883cefSAlexander Motin static int
g_multipath_good(struct g_geom * gp)1250c883cefSAlexander Motin g_multipath_good(struct g_geom *gp)
1260c883cefSAlexander Motin {
1270c883cefSAlexander Motin 	struct g_consumer *cp;
1280c883cefSAlexander Motin 	int n = 0;
1290c883cefSAlexander Motin 
1300c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
1310c883cefSAlexander Motin 		if ((cp->index & MP_BAD) == 0)
1320c883cefSAlexander Motin 			n++;
1330c883cefSAlexander Motin 	}
1340c883cefSAlexander Motin 	return (n);
1350c883cefSAlexander Motin }
1360c883cefSAlexander Motin 
1370c883cefSAlexander Motin static void
g_multipath_fault(struct g_consumer * cp,int cause)1380c883cefSAlexander Motin g_multipath_fault(struct g_consumer *cp, int cause)
1390c883cefSAlexander Motin {
1400c883cefSAlexander Motin 	struct g_multipath_softc *sc;
1410c883cefSAlexander Motin 	struct g_consumer *lcp;
1420c883cefSAlexander Motin 	struct g_geom *gp;
1430c883cefSAlexander Motin 
1440c883cefSAlexander Motin 	gp = cp->geom;
1450c883cefSAlexander Motin 	sc = gp->softc;
1460c883cefSAlexander Motin 	cp->index |= cause;
1470c883cefSAlexander Motin 	if (g_multipath_good(gp) == 0 && sc->sc_ndisks > 0) {
1480c883cefSAlexander Motin 		LIST_FOREACH(lcp, &gp->consumer, consumer) {
1490c883cefSAlexander Motin 			if (lcp->provider == NULL ||
1500c883cefSAlexander Motin 			    (lcp->index & (MP_LOST | MP_NEW)))
1510c883cefSAlexander Motin 				continue;
1520c883cefSAlexander Motin 			if (sc->sc_ndisks > 1 && lcp == cp)
1530c883cefSAlexander Motin 				continue;
1540c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: "
1550c883cefSAlexander Motin 			    "all paths in %s were marked FAIL, restore %s\n",
1560c883cefSAlexander Motin 			    sc->sc_name, lcp->provider->name);
15767f72211SAlan Somers 			SDT_PROBE2(geom, multipath, config, restore,
15867f72211SAlan Somers 			    sc->sc_name, lcp->provider->name);
1590c883cefSAlexander Motin 			lcp->index &= ~MP_FAIL;
1600c883cefSAlexander Motin 		}
1610c883cefSAlexander Motin 	}
1620c883cefSAlexander Motin 	if (cp != sc->sc_active)
1630c883cefSAlexander Motin 		return;
1640c883cefSAlexander Motin 	sc->sc_active = NULL;
1650c883cefSAlexander Motin 	LIST_FOREACH(lcp, &gp->consumer, consumer) {
1660c883cefSAlexander Motin 		if ((lcp->index & MP_BAD) == 0) {
1670c883cefSAlexander Motin 			sc->sc_active = lcp;
1680c883cefSAlexander Motin 			break;
1690c883cefSAlexander Motin 		}
1700c883cefSAlexander Motin 	}
1710c883cefSAlexander Motin 	if (sc->sc_active == NULL) {
1720c883cefSAlexander Motin 		printf("GEOM_MULTIPATH: out of providers for %s\n",
1730c883cefSAlexander Motin 		    sc->sc_name);
17463297dfdSAlexander Motin 	} else if (sc->sc_active_active != 1) {
1750c883cefSAlexander Motin 		printf("GEOM_MULTIPATH: %s is now active path in %s\n",
1760c883cefSAlexander Motin 		    sc->sc_active->provider->name, sc->sc_name);
1770c883cefSAlexander Motin 	}
1780c883cefSAlexander Motin }
1790c883cefSAlexander Motin 
1800c883cefSAlexander Motin static struct g_consumer *
g_multipath_choose(struct g_geom * gp,struct bio * bp)18163297dfdSAlexander Motin g_multipath_choose(struct g_geom *gp, struct bio *bp)
1820c883cefSAlexander Motin {
1830c883cefSAlexander Motin 	struct g_multipath_softc *sc;
1840c883cefSAlexander Motin 	struct g_consumer *best, *cp;
1850c883cefSAlexander Motin 
1860c883cefSAlexander Motin 	sc = gp->softc;
18763297dfdSAlexander Motin 	if (sc->sc_active_active == 0 ||
18863297dfdSAlexander Motin 	    (sc->sc_active_active == 2 && bp->bio_cmd != BIO_READ))
1890c883cefSAlexander Motin 		return (sc->sc_active);
1900c883cefSAlexander Motin 	best = NULL;
1910c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
1920c883cefSAlexander Motin 		if (cp->index & MP_BAD)
1930c883cefSAlexander Motin 			continue;
1940c883cefSAlexander Motin 		cp->index += MP_IDLE;
1950c883cefSAlexander Motin 		if (best == NULL || cp->private < best->private ||
1960c883cefSAlexander Motin 		    (cp->private == best->private && cp->index > best->index))
1970c883cefSAlexander Motin 			best = cp;
1980c883cefSAlexander Motin 	}
1990c883cefSAlexander Motin 	if (best != NULL)
2000c883cefSAlexander Motin 		best->index &= ~MP_IDLE_MASK;
2010c883cefSAlexander Motin 	return (best);
2020c883cefSAlexander Motin }
203e770bc6bSMatt Jacob 
204e770bc6bSMatt Jacob static void
g_mpd(void * arg,int flags __unused)205e770bc6bSMatt Jacob g_mpd(void *arg, int flags __unused)
206e770bc6bSMatt Jacob {
2070c883cefSAlexander Motin 	struct g_geom *gp;
2080c883cefSAlexander Motin 	struct g_multipath_softc *sc;
209e770bc6bSMatt Jacob 	struct g_consumer *cp;
2100c883cefSAlexander Motin 	int w;
211e770bc6bSMatt Jacob 
212e770bc6bSMatt Jacob 	g_topology_assert();
213e770bc6bSMatt Jacob 	cp = arg;
2140c883cefSAlexander Motin 	gp = cp->geom;
2150c883cefSAlexander Motin 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) {
2160c883cefSAlexander Motin 		w = cp->acw;
217e770bc6bSMatt Jacob 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
2180c883cefSAlexander Motin 		if (w > 0 && cp->provider != NULL &&
2190c883cefSAlexander Motin 		    (cp->provider->geom->flags & G_GEOM_WITHER) == 0) {
22025080ac4SSteven Hartland 			cp->index |= MP_WITHER;
2210c883cefSAlexander Motin 			g_post_event(g_mpd, cp, M_WAITOK, NULL);
2220c883cefSAlexander Motin 			return;
2230c883cefSAlexander Motin 		}
2240c883cefSAlexander Motin 	}
2250c883cefSAlexander Motin 	sc = gp->softc;
2260c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
227e770bc6bSMatt Jacob 	if (cp->provider) {
228e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: %s removed from %s\n",
2290c883cefSAlexander Motin 		    cp->provider->name, gp->name);
23067f72211SAlan Somers 		SDT_PROBE2(geom, multipath, config, remove,
23167f72211SAlan Somers 		    gp->name, cp->provider->name);
232e770bc6bSMatt Jacob 		g_detach(cp);
233e770bc6bSMatt Jacob 	}
234e770bc6bSMatt Jacob 	g_destroy_consumer(cp);
2350c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
2360c883cefSAlexander Motin 	if (LIST_EMPTY(&gp->consumer))
2370c883cefSAlexander Motin 		g_multipath_destroy(gp);
238e770bc6bSMatt Jacob }
239e770bc6bSMatt Jacob 
240e770bc6bSMatt Jacob static void
g_multipath_orphan(struct g_consumer * cp)241e770bc6bSMatt Jacob g_multipath_orphan(struct g_consumer *cp)
242e770bc6bSMatt Jacob {
2430c883cefSAlexander Motin 	struct g_multipath_softc *sc;
2440c883cefSAlexander Motin 	uintptr_t *cnt;
2450c883cefSAlexander Motin 
2460c883cefSAlexander Motin 	g_topology_assert();
2470c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s in %s was disconnected\n",
248e770bc6bSMatt Jacob 	    cp->provider->name, cp->geom->name);
24967f72211SAlan Somers 	SDT_PROBE2(geom, multipath, config, disconnect,
25067f72211SAlan Somers 	    cp->geom->name, cp->provider->name);
2510c883cefSAlexander Motin 	sc = cp->geom->softc;
2520c883cefSAlexander Motin 	cnt = (uintptr_t *)&cp->private;
2530c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
2540c883cefSAlexander Motin 	sc->sc_ndisks--;
2550c883cefSAlexander Motin 	g_multipath_fault(cp, MP_LOST);
2560c883cefSAlexander Motin 	if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
2570c883cefSAlexander Motin 		cp->index |= MP_POSTED;
2580c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
259e770bc6bSMatt Jacob 		g_mpd(cp, 0);
2600c883cefSAlexander Motin 	} else
2610c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
262e770bc6bSMatt Jacob }
263e770bc6bSMatt Jacob 
264e770bc6bSMatt Jacob static void
g_multipath_resize(struct g_consumer * cp)265e6afd72bSAlexander Motin g_multipath_resize(struct g_consumer *cp)
266e6afd72bSAlexander Motin {
267e6afd72bSAlexander Motin 	struct g_multipath_softc *sc;
268e6afd72bSAlexander Motin 	struct g_geom *gp;
269f8c79813SAlexander Motin 	struct g_consumer *cp1;
270e6afd72bSAlexander Motin 	struct g_provider *pp;
271e6afd72bSAlexander Motin 	struct g_multipath_metadata md;
272e6afd72bSAlexander Motin 	off_t size, psize, ssize;
273e6afd72bSAlexander Motin 	int error;
274e6afd72bSAlexander Motin 
275e6afd72bSAlexander Motin 	g_topology_assert();
276e6afd72bSAlexander Motin 
277e6afd72bSAlexander Motin 	gp = cp->geom;
278e6afd72bSAlexander Motin 	pp = cp->provider;
279e6afd72bSAlexander Motin 	sc = gp->softc;
280e6afd72bSAlexander Motin 
281e6afd72bSAlexander Motin 	if (sc->sc_stopping)
282e6afd72bSAlexander Motin 		return;
283e6afd72bSAlexander Motin 
284e6afd72bSAlexander Motin 	if (pp->mediasize < sc->sc_size) {
285e6afd72bSAlexander Motin 		size = pp->mediasize;
286e6afd72bSAlexander Motin 		ssize = pp->sectorsize;
287e6afd72bSAlexander Motin 	} else {
288e6afd72bSAlexander Motin 		size = ssize = OFF_MAX;
289e6afd72bSAlexander Motin 		mtx_lock(&sc->sc_mtx);
290f8c79813SAlexander Motin 		LIST_FOREACH(cp1, &gp->consumer, consumer) {
291f8c79813SAlexander Motin 			pp = cp1->provider;
292e6afd72bSAlexander Motin 			if (pp == NULL)
293e6afd72bSAlexander Motin 				continue;
294e6afd72bSAlexander Motin 			if (pp->mediasize < size) {
295e6afd72bSAlexander Motin 				size = pp->mediasize;
296e6afd72bSAlexander Motin 				ssize = pp->sectorsize;
297e6afd72bSAlexander Motin 			}
298e6afd72bSAlexander Motin 		}
299e6afd72bSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
300e6afd72bSAlexander Motin 		if (size == OFF_MAX || size == sc->sc_size)
301e6afd72bSAlexander Motin 			return;
302e6afd72bSAlexander Motin 	}
303e6afd72bSAlexander Motin 	psize = size - ((sc->sc_uuid[0] != 0) ? ssize : 0);
304e6afd72bSAlexander Motin 	printf("GEOM_MULTIPATH: %s size changed from %jd to %jd\n",
305e6afd72bSAlexander Motin 	    sc->sc_name, sc->sc_pp->mediasize, psize);
306f8c79813SAlexander Motin 	if (sc->sc_uuid[0] != 0 && size < sc->sc_size) {
307f8c79813SAlexander Motin 		error = g_multipath_read_metadata(cp, &md);
308f8c79813SAlexander Motin 		if (error ||
309f8c79813SAlexander Motin 		    (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) ||
310f8c79813SAlexander Motin 		    (memcmp(md.md_uuid, sc->sc_uuid, sizeof(sc->sc_uuid)) != 0) ||
311f8c79813SAlexander Motin 		    (strcmp(md.md_name, sc->sc_name) != 0) ||
312f8c79813SAlexander Motin 		    (md.md_size != 0 && md.md_size != size) ||
313f8c79813SAlexander Motin 		    (md.md_sectorsize != 0 && md.md_sectorsize != ssize)) {
314e6afd72bSAlexander Motin 			g_multipath_destroy(gp);
315e6afd72bSAlexander Motin 			return;
316e6afd72bSAlexander Motin 		}
317f8c79813SAlexander Motin 	}
318e6afd72bSAlexander Motin 	sc->sc_size = size;
319e6afd72bSAlexander Motin 	g_resize_provider(sc->sc_pp, psize);
320e6afd72bSAlexander Motin 
321f8c79813SAlexander Motin 	if (sc->sc_uuid[0] != 0) {
322e6afd72bSAlexander Motin 		pp = cp->provider;
323e6afd72bSAlexander Motin 		strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
324e6afd72bSAlexander Motin 		memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
325e6afd72bSAlexander Motin 		strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
326e6afd72bSAlexander Motin 		md.md_version = G_MULTIPATH_VERSION;
327e6afd72bSAlexander Motin 		md.md_size = size;
328f8c79813SAlexander Motin 		md.md_sectorsize = ssize;
329e6afd72bSAlexander Motin 		md.md_active_active = sc->sc_active_active;
330f8c79813SAlexander Motin 		error = g_multipath_write_metadata(cp, &md);
331e6afd72bSAlexander Motin 		if (error != 0)
332e6afd72bSAlexander Motin 			printf("GEOM_MULTIPATH: Can't update metadata on %s "
333e6afd72bSAlexander Motin 			    "(%d)\n", pp->name, error);
334e6afd72bSAlexander Motin 	}
335e6afd72bSAlexander Motin }
336e6afd72bSAlexander Motin 
337e6afd72bSAlexander Motin static void
g_multipath_start(struct bio * bp)338e770bc6bSMatt Jacob g_multipath_start(struct bio *bp)
339e770bc6bSMatt Jacob {
340e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
341e770bc6bSMatt Jacob 	struct g_geom *gp;
342e770bc6bSMatt Jacob 	struct g_consumer *cp;
343e770bc6bSMatt Jacob 	struct bio *cbp;
3440c883cefSAlexander Motin 	uintptr_t *cnt;
345e770bc6bSMatt Jacob 
346e770bc6bSMatt Jacob 	gp = bp->bio_to->geom;
347e770bc6bSMatt Jacob 	sc = gp->softc;
348e770bc6bSMatt Jacob 	KASSERT(sc != NULL, ("NULL sc"));
349e770bc6bSMatt Jacob 	cbp = g_clone_bio(bp);
350e770bc6bSMatt Jacob 	if (cbp == NULL) {
351e770bc6bSMatt Jacob 		g_io_deliver(bp, ENOMEM);
352e770bc6bSMatt Jacob 		return;
353e770bc6bSMatt Jacob 	}
3540c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
35563297dfdSAlexander Motin 	cp = g_multipath_choose(gp, bp);
3560c883cefSAlexander Motin 	if (cp == NULL) {
3570c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
3580c883cefSAlexander Motin 		g_destroy_bio(cbp);
3590c883cefSAlexander Motin 		g_io_deliver(bp, ENXIO);
3600c883cefSAlexander Motin 		return;
3610c883cefSAlexander Motin 	}
3620c883cefSAlexander Motin 	if ((uintptr_t)bp->bio_driver1 < sc->sc_ndisks)
3630c883cefSAlexander Motin 		bp->bio_driver1 = (void *)(uintptr_t)sc->sc_ndisks;
3640c883cefSAlexander Motin 	cnt = (uintptr_t *)&cp->private;
3650c883cefSAlexander Motin 	(*cnt)++;
3660c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
367e770bc6bSMatt Jacob 	cbp->bio_done = g_multipath_done;
368e770bc6bSMatt Jacob 	g_io_request(cbp, cp);
369e770bc6bSMatt Jacob }
370e770bc6bSMatt Jacob 
371e770bc6bSMatt Jacob static void
g_multipath_done(struct bio * bp)372e770bc6bSMatt Jacob g_multipath_done(struct bio *bp)
373e770bc6bSMatt Jacob {
3740c883cefSAlexander Motin 	struct g_multipath_softc *sc;
3750c883cefSAlexander Motin 	struct g_consumer *cp;
3760c883cefSAlexander Motin 	uintptr_t *cnt;
3770c883cefSAlexander Motin 
378e770bc6bSMatt Jacob 	if (bp->bio_error == ENXIO || bp->bio_error == EIO) {
379e770bc6bSMatt Jacob 		mtx_lock(&gmtbq_mtx);
380e770bc6bSMatt Jacob 		bioq_insert_tail(&gmtbq, bp);
381e770bc6bSMatt Jacob 		mtx_unlock(&gmtbq_mtx);
3820c883cefSAlexander Motin 		wakeup(&g_multipath_kt_state);
383e770bc6bSMatt Jacob 	} else {
3840c883cefSAlexander Motin 		cp = bp->bio_from;
3850c883cefSAlexander Motin 		sc = cp->geom->softc;
3860c883cefSAlexander Motin 		cnt = (uintptr_t *)&cp->private;
3870c883cefSAlexander Motin 		mtx_lock(&sc->sc_mtx);
3880c883cefSAlexander Motin 		(*cnt)--;
3890c883cefSAlexander Motin 		if (*cnt == 0 && (cp->index & MP_LOST)) {
3900ada3afcSAlexander Motin 			if (g_post_event(g_mpd, cp, M_NOWAIT, NULL) == 0)
3910c883cefSAlexander Motin 				cp->index |= MP_POSTED;
3920c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
3930c883cefSAlexander Motin 		} else
3940c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
395420dbe76SAlan Somers 		if (bp->bio_error == 0 &&
396420dbe76SAlan Somers 			bp->bio_cmd == BIO_GETATTR &&
397420dbe76SAlan Somers 			!strcmp(bp->bio_attribute, "GEOM::physpath"))
398420dbe76SAlan Somers 		{
399420dbe76SAlan Somers 			strlcat(bp->bio_data, "/mp", bp->bio_length);
400420dbe76SAlan Somers 		}
401e770bc6bSMatt Jacob 		g_std_done(bp);
402e770bc6bSMatt Jacob 	}
403e770bc6bSMatt Jacob }
404e770bc6bSMatt Jacob 
405e770bc6bSMatt Jacob static void
g_multipath_done_error(struct bio * bp)406e770bc6bSMatt Jacob g_multipath_done_error(struct bio *bp)
407e770bc6bSMatt Jacob {
408e770bc6bSMatt Jacob 	struct bio *pbp;
409e770bc6bSMatt Jacob 	struct g_geom *gp;
410e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
411e770bc6bSMatt Jacob 	struct g_consumer *cp;
412e770bc6bSMatt Jacob 	struct g_provider *pp;
4130c883cefSAlexander Motin 	uintptr_t *cnt;
414e770bc6bSMatt Jacob 
415e770bc6bSMatt Jacob 	/*
416e770bc6bSMatt Jacob 	 * If we had a failure, we have to check first to see
417e770bc6bSMatt Jacob 	 * whether the consumer it failed on was the currently
418e770bc6bSMatt Jacob 	 * active consumer (i.e., this is the first in perhaps
419e770bc6bSMatt Jacob 	 * a number of failures). If so, we then switch consumers
420e770bc6bSMatt Jacob 	 * to the next available consumer.
421e770bc6bSMatt Jacob 	 */
422e770bc6bSMatt Jacob 
423e770bc6bSMatt Jacob 	pbp = bp->bio_parent;
424e770bc6bSMatt Jacob 	gp = pbp->bio_to->geom;
425e770bc6bSMatt Jacob 	sc = gp->softc;
426e770bc6bSMatt Jacob 	cp = bp->bio_from;
427e770bc6bSMatt Jacob 	pp = cp->provider;
4280c883cefSAlexander Motin 	cnt = (uintptr_t *)&cp->private;
429e770bc6bSMatt Jacob 
4300c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
43163297dfdSAlexander Motin 	if ((cp->index & MP_FAIL) == 0) {
4320c883cefSAlexander Motin 		printf("GEOM_MULTIPATH: Error %d, %s in %s marked FAIL\n",
4330c883cefSAlexander Motin 		    bp->bio_error, pp->name, sc->sc_name);
43467f72211SAlan Somers 		SDT_PROBE3(geom, multipath, config, fail,
43567f72211SAlan Somers 		    sc->sc_name, pp->name, bp->bio_error);
4360c883cefSAlexander Motin 		g_multipath_fault(cp, MP_FAIL);
43763297dfdSAlexander Motin 	}
4380c883cefSAlexander Motin 	(*cnt)--;
4390c883cefSAlexander Motin 	if (*cnt == 0 && (cp->index & (MP_LOST | MP_POSTED)) == MP_LOST) {
440e770bc6bSMatt Jacob 		cp->index |= MP_POSTED;
4410c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
4420c883cefSAlexander Motin 		g_post_event(g_mpd, cp, M_WAITOK, NULL);
4430c883cefSAlexander Motin 	} else
4440c883cefSAlexander Motin 		mtx_unlock(&sc->sc_mtx);
445e770bc6bSMatt Jacob 
446e770bc6bSMatt Jacob 	/*
447e770bc6bSMatt Jacob 	 * If we can fruitfully restart the I/O, do so.
448e770bc6bSMatt Jacob 	 */
4490c883cefSAlexander Motin 	if (pbp->bio_children < (uintptr_t)pbp->bio_driver1) {
4500c883cefSAlexander Motin 		pbp->bio_inbed++;
45167f72211SAlan Somers 		SDT_PROBE2(geom, multipath, io, restart, bp, pbp);
452e770bc6bSMatt Jacob 		g_destroy_bio(bp);
453e770bc6bSMatt Jacob 		g_multipath_start(pbp);
454e770bc6bSMatt Jacob 	} else {
455e770bc6bSMatt Jacob 		g_std_done(bp);
456e770bc6bSMatt Jacob 	}
457e770bc6bSMatt Jacob }
458e770bc6bSMatt Jacob 
459e770bc6bSMatt Jacob static void
g_multipath_kt(void * arg)460e770bc6bSMatt Jacob g_multipath_kt(void *arg)
461e770bc6bSMatt Jacob {
46212f35a61SPawel Jakub Dawidek 
463e770bc6bSMatt Jacob 	g_multipath_kt_state = GKT_RUN;
464e770bc6bSMatt Jacob 	mtx_lock(&gmtbq_mtx);
465e770bc6bSMatt Jacob 	while (g_multipath_kt_state == GKT_RUN) {
466e770bc6bSMatt Jacob 		for (;;) {
467e770bc6bSMatt Jacob 			struct bio *bp;
46812f35a61SPawel Jakub Dawidek 
469e770bc6bSMatt Jacob 			bp = bioq_takefirst(&gmtbq);
47012f35a61SPawel Jakub Dawidek 			if (bp == NULL)
471e770bc6bSMatt Jacob 				break;
472e770bc6bSMatt Jacob 			mtx_unlock(&gmtbq_mtx);
473e770bc6bSMatt Jacob 			g_multipath_done_error(bp);
474e770bc6bSMatt Jacob 			mtx_lock(&gmtbq_mtx);
475e770bc6bSMatt Jacob 		}
47663297dfdSAlexander Motin 		if (g_multipath_kt_state != GKT_RUN)
47763297dfdSAlexander Motin 			break;
478e770bc6bSMatt Jacob 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
47963297dfdSAlexander Motin 		    "gkt:wait", 0);
480e770bc6bSMatt Jacob 	}
481e770bc6bSMatt Jacob 	mtx_unlock(&gmtbq_mtx);
482e770bc6bSMatt Jacob 	wakeup(&g_multipath_kt_state);
4833745c395SJulian Elischer 	kproc_exit(0);
484e770bc6bSMatt Jacob }
485e770bc6bSMatt Jacob 
486e770bc6bSMatt Jacob static int
g_multipath_access(struct g_provider * pp,int dr,int dw,int de)487e770bc6bSMatt Jacob g_multipath_access(struct g_provider *pp, int dr, int dw, int de)
488e770bc6bSMatt Jacob {
489e770bc6bSMatt Jacob 	struct g_geom *gp;
490e770bc6bSMatt Jacob 	struct g_consumer *cp, *badcp = NULL;
4910c883cefSAlexander Motin 	struct g_multipath_softc *sc;
492e770bc6bSMatt Jacob 	int error;
493e770bc6bSMatt Jacob 
494e770bc6bSMatt Jacob 	gp = pp->geom;
495e770bc6bSMatt Jacob 
49625080ac4SSteven Hartland 	/* Error used if we have no valid consumers. */
49780f0a89cSAlexander Motin 	error = (dr > 0 || dw > 0 || de > 0) ? ENXIO : 0;
49825080ac4SSteven Hartland 
499e770bc6bSMatt Jacob 	LIST_FOREACH(cp, &gp->consumer, consumer) {
50025080ac4SSteven Hartland 		if (cp->index & MP_WITHER)
50125080ac4SSteven Hartland 			continue;
50225080ac4SSteven Hartland 
503e770bc6bSMatt Jacob 		error = g_access(cp, dr, dw, de);
504e770bc6bSMatt Jacob 		if (error) {
505e770bc6bSMatt Jacob 			badcp = cp;
506e770bc6bSMatt Jacob 			goto fail;
507e770bc6bSMatt Jacob 		}
508e770bc6bSMatt Jacob 	}
50925080ac4SSteven Hartland 
51025080ac4SSteven Hartland 	if (error != 0)
51125080ac4SSteven Hartland 		return (error);
51225080ac4SSteven Hartland 
5130c883cefSAlexander Motin 	sc = gp->softc;
5140c883cefSAlexander Motin 	sc->sc_opened += dr + dw + de;
5150c883cefSAlexander Motin 	if (sc->sc_stopping && sc->sc_opened == 0)
5160c883cefSAlexander Motin 		g_multipath_destroy(gp);
51725080ac4SSteven Hartland 
518e770bc6bSMatt Jacob 	return (0);
519e770bc6bSMatt Jacob 
520e770bc6bSMatt Jacob fail:
521e770bc6bSMatt Jacob 	LIST_FOREACH(cp, &gp->consumer, consumer) {
52212f35a61SPawel Jakub Dawidek 		if (cp == badcp)
523e770bc6bSMatt Jacob 			break;
52425080ac4SSteven Hartland 		if (cp->index & MP_WITHER)
52525080ac4SSteven Hartland 			continue;
52625080ac4SSteven Hartland 
527e770bc6bSMatt Jacob 		(void) g_access(cp, -dr, -dw, -de);
528e770bc6bSMatt Jacob 	}
529e770bc6bSMatt Jacob 	return (error);
530e770bc6bSMatt Jacob }
531e770bc6bSMatt Jacob 
532e770bc6bSMatt Jacob static struct g_geom *
g_multipath_create(struct g_class * mp,struct g_multipath_metadata * md)533e770bc6bSMatt Jacob g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md)
534e770bc6bSMatt Jacob {
535e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
536e770bc6bSMatt Jacob 	struct g_geom *gp;
537e770bc6bSMatt Jacob 	struct g_provider *pp;
538e770bc6bSMatt Jacob 
539e770bc6bSMatt Jacob 	g_topology_assert();
540e770bc6bSMatt Jacob 
541e770bc6bSMatt Jacob 	LIST_FOREACH(gp, &mp->geom, geom) {
5420c883cefSAlexander Motin 		sc = gp->softc;
5430c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
5440c883cefSAlexander Motin 			continue;
545e770bc6bSMatt Jacob 		if (strcmp(gp->name, md->md_name) == 0) {
546e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: name %s already exists\n",
547e770bc6bSMatt Jacob 			    md->md_name);
548e770bc6bSMatt Jacob 			return (NULL);
549e770bc6bSMatt Jacob 		}
550e770bc6bSMatt Jacob 	}
551e770bc6bSMatt Jacob 
55202c62349SJaakko Heinonen 	gp = g_new_geomf(mp, "%s", md->md_name);
553e770bc6bSMatt Jacob 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
5540c883cefSAlexander Motin 	mtx_init(&sc->sc_mtx, "multipath", NULL, MTX_DEF);
5550c883cefSAlexander Motin 	memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid));
5560c883cefSAlexander Motin 	memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name));
5570c883cefSAlexander Motin 	sc->sc_active_active = md->md_active_active;
558e6afd72bSAlexander Motin 	sc->sc_size = md->md_size;
559e770bc6bSMatt Jacob 	gp->softc = sc;
560e770bc6bSMatt Jacob 	gp->start = g_multipath_start;
561e770bc6bSMatt Jacob 	gp->orphan = g_multipath_orphan;
562e6afd72bSAlexander Motin 	gp->resize = g_multipath_resize;
563e770bc6bSMatt Jacob 	gp->access = g_multipath_access;
5640c883cefSAlexander Motin 	gp->dumpconf = g_multipath_dumpconf;
565e770bc6bSMatt Jacob 
566e770bc6bSMatt Jacob 	pp = g_new_providerf(gp, "multipath/%s", md->md_name);
56740ea77a0SAlexander Motin 	pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
5680c883cefSAlexander Motin 	if (md->md_size != 0) {
5690c883cefSAlexander Motin 		pp->mediasize = md->md_size -
5700c883cefSAlexander Motin 		    ((md->md_uuid[0] != 0) ? md->md_sectorsize : 0);
571e770bc6bSMatt Jacob 		pp->sectorsize = md->md_sectorsize;
5720c883cefSAlexander Motin 	}
5730c883cefSAlexander Motin 	sc->sc_pp = pp;
574e770bc6bSMatt Jacob 	g_error_provider(pp, 0);
5750c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s created\n", gp->name);
576e770bc6bSMatt Jacob 	return (gp);
577e770bc6bSMatt Jacob }
578e770bc6bSMatt Jacob 
579e770bc6bSMatt Jacob static int
g_multipath_add_disk(struct g_geom * gp,struct g_provider * pp)580e770bc6bSMatt Jacob g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp)
581e770bc6bSMatt Jacob {
582e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
583b74fdaafSMateusz Guzik 	struct g_consumer *cp;
5840c883cefSAlexander Motin 	int error, acr, acw, ace;
585e770bc6bSMatt Jacob 
586e770bc6bSMatt Jacob 	g_topology_assert();
587e770bc6bSMatt Jacob 
588e770bc6bSMatt Jacob 	sc = gp->softc;
589e770bc6bSMatt Jacob 	KASSERT(sc, ("no softc"));
590e770bc6bSMatt Jacob 
591e770bc6bSMatt Jacob 	/*
592e770bc6bSMatt Jacob 	 * Make sure that the passed provider isn't already attached
593e770bc6bSMatt Jacob 	 */
594e770bc6bSMatt Jacob 	LIST_FOREACH(cp, &gp->consumer, consumer) {
59512f35a61SPawel Jakub Dawidek 		if (cp->provider == pp)
596e770bc6bSMatt Jacob 			break;
597e770bc6bSMatt Jacob 	}
598e770bc6bSMatt Jacob 	if (cp) {
599e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: provider %s already attached to %s\n",
600e770bc6bSMatt Jacob 		    pp->name, gp->name);
601e770bc6bSMatt Jacob 		return (EEXIST);
602e770bc6bSMatt Jacob 	}
603e770bc6bSMatt Jacob 	cp = g_new_consumer(gp);
60440ea77a0SAlexander Motin 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
6050c883cefSAlexander Motin 	cp->private = NULL;
6060c883cefSAlexander Motin 	cp->index = MP_NEW;
607e770bc6bSMatt Jacob 	error = g_attach(cp, pp);
608e770bc6bSMatt Jacob 	if (error != 0) {
609e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: cannot attach %s to %s",
610e770bc6bSMatt Jacob 		    pp->name, sc->sc_name);
611e770bc6bSMatt Jacob 		g_destroy_consumer(cp);
612e770bc6bSMatt Jacob 		return (error);
613e770bc6bSMatt Jacob 	}
614e770bc6bSMatt Jacob 
615e770bc6bSMatt Jacob 	/*
616e770bc6bSMatt Jacob 	 * Set access permissions on new consumer to match other consumers
617e770bc6bSMatt Jacob 	 */
6180c883cefSAlexander Motin 	if (sc->sc_pp) {
6190c883cefSAlexander Motin 		acr = sc->sc_pp->acr;
6200c883cefSAlexander Motin 		acw = sc->sc_pp->acw;
6210c883cefSAlexander Motin 		ace = sc->sc_pp->ace;
6220c883cefSAlexander Motin 	} else
6230c883cefSAlexander Motin 		acr = acw = ace = 0;
6240c883cefSAlexander Motin 	if (g_multipath_exclusive) {
6250c883cefSAlexander Motin 		acr++;
6260c883cefSAlexander Motin 		acw++;
6270c883cefSAlexander Motin 		ace++;
6280c883cefSAlexander Motin 	}
6290c883cefSAlexander Motin 	error = g_access(cp, acr, acw, ace);
630e770bc6bSMatt Jacob 	if (error) {
631e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: cannot set access in "
6320c883cefSAlexander Motin 		    "attaching %s to %s (%d)\n",
6330c883cefSAlexander Motin 		    pp->name, sc->sc_name, error);
634e770bc6bSMatt Jacob 		g_detach(cp);
635e770bc6bSMatt Jacob 		g_destroy_consumer(cp);
636e770bc6bSMatt Jacob 		return (error);
637e770bc6bSMatt Jacob 	}
638e6afd72bSAlexander Motin 	if (sc->sc_size == 0) {
639e6afd72bSAlexander Motin 		sc->sc_size = pp->mediasize -
6400c883cefSAlexander Motin 		    ((sc->sc_uuid[0] != 0) ? pp->sectorsize : 0);
641e6afd72bSAlexander Motin 		sc->sc_pp->mediasize = sc->sc_size;
6420c883cefSAlexander Motin 		sc->sc_pp->sectorsize = pp->sectorsize;
643e770bc6bSMatt Jacob 	}
644e6afd72bSAlexander Motin 	if (sc->sc_pp->stripesize == 0 && sc->sc_pp->stripeoffset == 0) {
6450c883cefSAlexander Motin 		sc->sc_pp->stripesize = pp->stripesize;
6460c883cefSAlexander Motin 		sc->sc_pp->stripeoffset = pp->stripeoffset;
6470c883cefSAlexander Motin 	}
648f4673017SAlexander Motin 	sc->sc_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
6490c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
6500c883cefSAlexander Motin 	cp->index = 0;
6510c883cefSAlexander Motin 	sc->sc_ndisks++;
6520c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
6530c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s added to %s\n",
6540c883cefSAlexander Motin 	    pp->name, sc->sc_name);
6550c883cefSAlexander Motin 	if (sc->sc_active == NULL) {
6560c883cefSAlexander Motin 		sc->sc_active = cp;
65763297dfdSAlexander Motin 		if (sc->sc_active_active != 1)
6580c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
659e770bc6bSMatt Jacob 			    pp->name, sc->sc_name);
660e770bc6bSMatt Jacob 	}
661e770bc6bSMatt Jacob 	return (0);
662e770bc6bSMatt Jacob }
663e770bc6bSMatt Jacob 
664e770bc6bSMatt Jacob static int
g_multipath_destroy(struct g_geom * gp)665e770bc6bSMatt Jacob g_multipath_destroy(struct g_geom *gp)
666e770bc6bSMatt Jacob {
6670c883cefSAlexander Motin 	struct g_multipath_softc *sc;
6680c883cefSAlexander Motin 	struct g_consumer *cp, *cp1;
669e770bc6bSMatt Jacob 
670e770bc6bSMatt Jacob 	g_topology_assert();
67112f35a61SPawel Jakub Dawidek 	if (gp->softc == NULL)
672e770bc6bSMatt Jacob 		return (ENXIO);
6730c883cefSAlexander Motin 	sc = gp->softc;
6740c883cefSAlexander Motin 	if (!sc->sc_stopping) {
675e770bc6bSMatt Jacob 		printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
6760c883cefSAlexander Motin 		sc->sc_stopping = 1;
6770c883cefSAlexander Motin 	}
6780c883cefSAlexander Motin 	if (sc->sc_opened != 0) {
6790c883cefSAlexander Motin 		g_wither_provider(sc->sc_pp, ENXIO);
6800c883cefSAlexander Motin 		sc->sc_pp = NULL;
6810c883cefSAlexander Motin 		return (EINPROGRESS);
6820c883cefSAlexander Motin 	}
6830c883cefSAlexander Motin 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
6840c883cefSAlexander Motin 		mtx_lock(&sc->sc_mtx);
6850c883cefSAlexander Motin 		if ((cp->index & MP_POSTED) == 0) {
6860c883cefSAlexander Motin 			cp->index |= MP_POSTED;
6870c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
6880c883cefSAlexander Motin 			g_mpd(cp, 0);
6890c883cefSAlexander Motin 			if (cp1 == NULL)
6900c883cefSAlexander Motin 				return(0);	/* Recursion happened. */
6910c883cefSAlexander Motin 		} else
6920c883cefSAlexander Motin 			mtx_unlock(&sc->sc_mtx);
6930c883cefSAlexander Motin 	}
6940c883cefSAlexander Motin 	if (!LIST_EMPTY(&gp->consumer))
6950c883cefSAlexander Motin 		return (EINPROGRESS);
6960c883cefSAlexander Motin 	mtx_destroy(&sc->sc_mtx);
697e770bc6bSMatt Jacob 	g_free(gp->softc);
698e770bc6bSMatt Jacob 	gp->softc = NULL;
6990c883cefSAlexander Motin 	printf("GEOM_MULTIPATH: %s destroyed\n", gp->name);
700e770bc6bSMatt Jacob 	g_wither_geom(gp, ENXIO);
701e770bc6bSMatt Jacob 	return (0);
702e770bc6bSMatt Jacob }
703e770bc6bSMatt Jacob 
704e770bc6bSMatt Jacob static int
g_multipath_destroy_geom(struct gctl_req * req,struct g_class * mp,struct g_geom * gp)705e770bc6bSMatt Jacob g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
706e770bc6bSMatt Jacob     struct g_geom *gp)
707e770bc6bSMatt Jacob {
70812f35a61SPawel Jakub Dawidek 
709e770bc6bSMatt Jacob 	return (g_multipath_destroy(gp));
710e770bc6bSMatt Jacob }
711e770bc6bSMatt Jacob 
712b5dce617SMatt Jacob static int
g_multipath_rotate(struct g_geom * gp)713b5dce617SMatt Jacob g_multipath_rotate(struct g_geom *gp)
714b5dce617SMatt Jacob {
7158fb378d6SThomas Quinot 	struct g_consumer *lcp, *first_good_cp = NULL;
716b5dce617SMatt Jacob 	struct g_multipath_softc *sc = gp->softc;
7178fb378d6SThomas Quinot 	int active_cp_seen = 0;
718b5dce617SMatt Jacob 
719b5dce617SMatt Jacob 	g_topology_assert();
720b5dce617SMatt Jacob 	if (sc == NULL)
721b5dce617SMatt Jacob 		return (ENXIO);
722b5dce617SMatt Jacob 	LIST_FOREACH(lcp, &gp->consumer, consumer) {
723b5dce617SMatt Jacob 		if ((lcp->index & MP_BAD) == 0) {
7248fb378d6SThomas Quinot 			if (first_good_cp == NULL)
7258fb378d6SThomas Quinot 				first_good_cp = lcp;
7268fb378d6SThomas Quinot 			if (active_cp_seen)
727b5dce617SMatt Jacob 				break;
728b5dce617SMatt Jacob 		}
7298fb378d6SThomas Quinot 		if (sc->sc_active == lcp)
7308fb378d6SThomas Quinot 			active_cp_seen = 1;
731b5dce617SMatt Jacob 	}
7328fb378d6SThomas Quinot 	if (lcp == NULL)
7338fb378d6SThomas Quinot 		lcp = first_good_cp;
7348fb378d6SThomas Quinot 	if (lcp && lcp != sc->sc_active) {
7350c883cefSAlexander Motin 		sc->sc_active = lcp;
73663297dfdSAlexander Motin 		if (sc->sc_active_active != 1)
7370c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
738b5dce617SMatt Jacob 			    lcp->provider->name, sc->sc_name);
739b5dce617SMatt Jacob 	}
740b5dce617SMatt Jacob 	return (0);
741b5dce617SMatt Jacob }
742b5dce617SMatt Jacob 
743e770bc6bSMatt Jacob static void
g_multipath_init(struct g_class * mp)744e770bc6bSMatt Jacob g_multipath_init(struct g_class *mp)
745e770bc6bSMatt Jacob {
746e770bc6bSMatt Jacob 	bioq_init(&gmtbq);
747e770bc6bSMatt Jacob 	mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
74863297dfdSAlexander Motin 	kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt");
749e770bc6bSMatt Jacob }
750e770bc6bSMatt Jacob 
751e770bc6bSMatt Jacob static void
g_multipath_fini(struct g_class * mp)752e770bc6bSMatt Jacob g_multipath_fini(struct g_class *mp)
753e770bc6bSMatt Jacob {
754e770bc6bSMatt Jacob 	if (g_multipath_kt_state == GKT_RUN) {
755e770bc6bSMatt Jacob 		mtx_lock(&gmtbq_mtx);
756e770bc6bSMatt Jacob 		g_multipath_kt_state = GKT_DIE;
757e770bc6bSMatt Jacob 		wakeup(&g_multipath_kt_state);
758e770bc6bSMatt Jacob 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
759e770bc6bSMatt Jacob 		    "gmp:fini", 0);
760e770bc6bSMatt Jacob 		mtx_unlock(&gmtbq_mtx);
761e770bc6bSMatt Jacob 	}
762e770bc6bSMatt Jacob }
763e770bc6bSMatt Jacob 
764e770bc6bSMatt Jacob static int
g_multipath_read_metadata(struct g_consumer * cp,struct g_multipath_metadata * md)765e770bc6bSMatt Jacob g_multipath_read_metadata(struct g_consumer *cp,
766e770bc6bSMatt Jacob     struct g_multipath_metadata *md)
767e770bc6bSMatt Jacob {
768e770bc6bSMatt Jacob 	struct g_provider *pp;
769e770bc6bSMatt Jacob 	u_char *buf;
770e770bc6bSMatt Jacob 	int error;
771e770bc6bSMatt Jacob 
772e770bc6bSMatt Jacob 	g_topology_assert();
773e770bc6bSMatt Jacob 	error = g_access(cp, 1, 0, 0);
77412f35a61SPawel Jakub Dawidek 	if (error != 0)
775e770bc6bSMatt Jacob 		return (error);
776e770bc6bSMatt Jacob 	pp = cp->provider;
777e770bc6bSMatt Jacob 	g_topology_unlock();
778e770bc6bSMatt Jacob 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
779e770bc6bSMatt Jacob 	    pp->sectorsize, &error);
780e770bc6bSMatt Jacob 	g_topology_lock();
781e770bc6bSMatt Jacob 	g_access(cp, -1, 0, 0);
78212f35a61SPawel Jakub Dawidek 	if (buf == NULL)
783e770bc6bSMatt Jacob 		return (error);
784e770bc6bSMatt Jacob 	multipath_metadata_decode(buf, md);
785e770bc6bSMatt Jacob 	g_free(buf);
786e770bc6bSMatt Jacob 	return (0);
787e770bc6bSMatt Jacob }
788e770bc6bSMatt Jacob 
789f8c79813SAlexander Motin static int
g_multipath_write_metadata(struct g_consumer * cp,struct g_multipath_metadata * md)790f8c79813SAlexander Motin g_multipath_write_metadata(struct g_consumer *cp,
791f8c79813SAlexander Motin     struct g_multipath_metadata *md)
792f8c79813SAlexander Motin {
793f8c79813SAlexander Motin 	struct g_provider *pp;
794f8c79813SAlexander Motin 	u_char *buf;
795f8c79813SAlexander Motin 	int error;
796f8c79813SAlexander Motin 
797f8c79813SAlexander Motin 	g_topology_assert();
798f8c79813SAlexander Motin 	error = g_access(cp, 1, 1, 1);
799f8c79813SAlexander Motin 	if (error != 0)
800f8c79813SAlexander Motin 		return (error);
801f8c79813SAlexander Motin 	pp = cp->provider;
802f8c79813SAlexander Motin 	g_topology_unlock();
803f8c79813SAlexander Motin 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
804f8c79813SAlexander Motin 	multipath_metadata_encode(md, buf);
805f8c79813SAlexander Motin 	error = g_write_data(cp, pp->mediasize - pp->sectorsize,
806f8c79813SAlexander Motin 	    buf, pp->sectorsize);
807f8c79813SAlexander Motin 	g_topology_lock();
808f8c79813SAlexander Motin 	g_access(cp, -1, -1, -1);
809f8c79813SAlexander Motin 	g_free(buf);
810f8c79813SAlexander Motin 	return (error);
811f8c79813SAlexander Motin }
812f8c79813SAlexander Motin 
813e770bc6bSMatt Jacob static struct g_geom *
g_multipath_taste(struct g_class * mp,struct g_provider * pp,int flags __unused)814e770bc6bSMatt Jacob g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
815e770bc6bSMatt Jacob {
816e770bc6bSMatt Jacob 	struct g_multipath_metadata md;
817e770bc6bSMatt Jacob 	struct g_multipath_softc *sc;
818e770bc6bSMatt Jacob 	struct g_consumer *cp;
819e770bc6bSMatt Jacob 	struct g_geom *gp, *gp1;
820e770bc6bSMatt Jacob 	int error, isnew;
821e770bc6bSMatt Jacob 
822e770bc6bSMatt Jacob 	g_topology_assert();
823e770bc6bSMatt Jacob 
824e770bc6bSMatt Jacob 	gp = g_new_geomf(mp, "multipath:taste");
825e770bc6bSMatt Jacob 	gp->start = g_multipath_start;
826e770bc6bSMatt Jacob 	gp->access = g_multipath_access;
827e770bc6bSMatt Jacob 	gp->orphan = g_multipath_orphan;
828e770bc6bSMatt Jacob 	cp = g_new_consumer(gp);
82910ae42ccSAlexander Motin 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
830d22ff249SEdward Tomasz Napierala 	error = g_attach(cp, pp);
831d22ff249SEdward Tomasz Napierala 	if (error == 0) {
832e770bc6bSMatt Jacob 		error = g_multipath_read_metadata(cp, &md);
833e770bc6bSMatt Jacob 		g_detach(cp);
834d22ff249SEdward Tomasz Napierala 	}
835e770bc6bSMatt Jacob 	g_destroy_consumer(cp);
836e770bc6bSMatt Jacob 	g_destroy_geom(gp);
83712f35a61SPawel Jakub Dawidek 	if (error != 0)
838e770bc6bSMatt Jacob 		return (NULL);
839e770bc6bSMatt Jacob 	gp = NULL;
840e770bc6bSMatt Jacob 
841e770bc6bSMatt Jacob 	if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
84212f35a61SPawel Jakub Dawidek 		if (g_multipath_debug)
843e770bc6bSMatt Jacob 			printf("%s is not MULTIPATH\n", pp->name);
844e770bc6bSMatt Jacob 		return (NULL);
845e770bc6bSMatt Jacob 	}
846e770bc6bSMatt Jacob 	if (md.md_version != G_MULTIPATH_VERSION) {
847e770bc6bSMatt Jacob 		printf("%s has version %d multipath id- this module is version "
848e770bc6bSMatt Jacob 		    " %d: rejecting\n", pp->name, md.md_version,
849e770bc6bSMatt Jacob 		    G_MULTIPATH_VERSION);
850e770bc6bSMatt Jacob 		return (NULL);
851e770bc6bSMatt Jacob 	}
8520c883cefSAlexander Motin 	if (md.md_size != 0 && md.md_size != pp->mediasize)
8530c883cefSAlexander Motin 		return (NULL);
8540c883cefSAlexander Motin 	if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize)
8550c883cefSAlexander Motin 		return (NULL);
85612f35a61SPawel Jakub Dawidek 	if (g_multipath_debug)
857e770bc6bSMatt Jacob 		printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
85867f72211SAlan Somers 	SDT_PROBE2(geom, multipath, config, taste, md.md_name, md.md_uuid);
859e770bc6bSMatt Jacob 
860e770bc6bSMatt Jacob 	/*
861e770bc6bSMatt Jacob 	 * Let's check if such a device already is present. We check against
862e770bc6bSMatt Jacob 	 * uuid alone first because that's the true distinguishor. If that
863e770bc6bSMatt Jacob 	 * passes, then we check for name conflicts. If there are conflicts,
864e770bc6bSMatt Jacob 	 * modify the name.
865e770bc6bSMatt Jacob 	 *
866e770bc6bSMatt Jacob 	 * The whole purpose of this is to solve the problem that people don't
867e770bc6bSMatt Jacob 	 * pick good unique names, but good unique names (like uuids) are a
868e770bc6bSMatt Jacob 	 * pain to use. So, we allow people to build GEOMs with friendly names
869e770bc6bSMatt Jacob 	 * and uuids, and modify the names in case there's a collision.
870e770bc6bSMatt Jacob 	 */
871e770bc6bSMatt Jacob 	sc = NULL;
872e770bc6bSMatt Jacob 	LIST_FOREACH(gp, &mp->geom, geom) {
873e770bc6bSMatt Jacob 		sc = gp->softc;
8740c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
875e770bc6bSMatt Jacob 			continue;
87612f35a61SPawel Jakub Dawidek 		if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
877e770bc6bSMatt Jacob 			break;
878e770bc6bSMatt Jacob 	}
879e770bc6bSMatt Jacob 
880e770bc6bSMatt Jacob 	LIST_FOREACH(gp1, &mp->geom, geom) {
88112f35a61SPawel Jakub Dawidek 		if (gp1 == gp)
882e770bc6bSMatt Jacob 			continue;
883e770bc6bSMatt Jacob 		sc = gp1->softc;
8840c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
885e770bc6bSMatt Jacob 			continue;
88612f35a61SPawel Jakub Dawidek 		if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
887e770bc6bSMatt Jacob 			break;
888e770bc6bSMatt Jacob 	}
889e770bc6bSMatt Jacob 
890e770bc6bSMatt Jacob 	/*
891e770bc6bSMatt Jacob 	 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
892e770bc6bSMatt Jacob 	 *
893e770bc6bSMatt Jacob 	 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
894e770bc6bSMatt Jacob 	 * with the same name (but a different UUID).
895e770bc6bSMatt Jacob 	 *
896e770bc6bSMatt Jacob 	 * If gp is NULL, then modify the name with a random number and
897e770bc6bSMatt Jacob   	 * complain, but allow the creation of the geom to continue.
898e770bc6bSMatt Jacob 	 *
899e770bc6bSMatt Jacob 	 * If gp is *not* NULL, just use the geom's name as we're attaching
900e770bc6bSMatt Jacob 	 * this disk to the (previously generated) name.
901e770bc6bSMatt Jacob 	 */
902e770bc6bSMatt Jacob 
903e770bc6bSMatt Jacob 	if (gp1) {
904e770bc6bSMatt Jacob 		sc = gp1->softc;
905e770bc6bSMatt Jacob 		if (gp == NULL) {
906e770bc6bSMatt Jacob 			char buf[16];
907e770bc6bSMatt Jacob 			u_long rand = random();
908e770bc6bSMatt Jacob 
909e770bc6bSMatt Jacob 			snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
910e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
911e770bc6bSMatt Jacob 			    sc->sc_name, sc->sc_uuid);
912e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
913e770bc6bSMatt Jacob 			    md.md_uuid, buf);
914e770bc6bSMatt Jacob 			strlcpy(md.md_name, buf, sizeof(md.md_name));
915e770bc6bSMatt Jacob 		} else {
916e770bc6bSMatt Jacob 			strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
917e770bc6bSMatt Jacob 		}
918e770bc6bSMatt Jacob 	}
919e770bc6bSMatt Jacob 
920e770bc6bSMatt Jacob 	if (gp == NULL) {
921e770bc6bSMatt Jacob 		gp = g_multipath_create(mp, &md);
922e770bc6bSMatt Jacob 		if (gp == NULL) {
923e770bc6bSMatt Jacob 			printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
924e770bc6bSMatt Jacob 			    md.md_name, md.md_uuid);
925e770bc6bSMatt Jacob 			return (NULL);
926e770bc6bSMatt Jacob 		}
927e770bc6bSMatt Jacob 		isnew = 1;
928e770bc6bSMatt Jacob 	} else {
929e770bc6bSMatt Jacob 		isnew = 0;
930e770bc6bSMatt Jacob 	}
931e770bc6bSMatt Jacob 
932e770bc6bSMatt Jacob 	sc = gp->softc;
933e770bc6bSMatt Jacob 	KASSERT(sc != NULL, ("sc is NULL"));
934e770bc6bSMatt Jacob 	error = g_multipath_add_disk(gp, pp);
935e770bc6bSMatt Jacob 	if (error != 0) {
93612f35a61SPawel Jakub Dawidek 		if (isnew)
937e770bc6bSMatt Jacob 			g_multipath_destroy(gp);
938e770bc6bSMatt Jacob 		return (NULL);
939e770bc6bSMatt Jacob 	}
940e770bc6bSMatt Jacob 	return (gp);
941e770bc6bSMatt Jacob }
942e770bc6bSMatt Jacob 
943e770bc6bSMatt Jacob static void
g_multipath_ctl_add_name(struct gctl_req * req,struct g_class * mp,const char * name)9440c883cefSAlexander Motin g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp,
9450c883cefSAlexander Motin     const char *name)
946e770bc6bSMatt Jacob {
9470c883cefSAlexander Motin 	struct g_multipath_softc *sc;
948e770bc6bSMatt Jacob 	struct g_geom *gp;
9492b4969ffSMatt Jacob 	struct g_consumer *cp;
9500c883cefSAlexander Motin 	struct g_provider *pp;
9510c883cefSAlexander Motin 	const char *mpname;
9528510f61aSXin LI 	static const char devpf[6] = _PATH_DEV;
953d3fef0a0SAlexander Motin 	int error;
954e770bc6bSMatt Jacob 
955e770bc6bSMatt Jacob 	g_topology_assert();
956e770bc6bSMatt Jacob 
957e770bc6bSMatt Jacob 	mpname = gctl_get_asciiparam(req, "arg0");
958e770bc6bSMatt Jacob         if (mpname == NULL) {
959e770bc6bSMatt Jacob                 gctl_error(req, "No 'arg0' argument");
960e770bc6bSMatt Jacob                 return;
961e770bc6bSMatt Jacob         }
9622b4969ffSMatt Jacob 	gp = g_multipath_find_geom(mp, mpname);
9632b4969ffSMatt Jacob 	if (gp == NULL) {
9642b4969ffSMatt Jacob 		gctl_error(req, "Device %s is invalid", mpname);
965e770bc6bSMatt Jacob 		return;
966e770bc6bSMatt Jacob 	}
9670c883cefSAlexander Motin 	sc = gp->softc;
968e770bc6bSMatt Jacob 
96912f35a61SPawel Jakub Dawidek 	if (strncmp(name, devpf, 5) == 0)
970e770bc6bSMatt Jacob 		name += 5;
9712b4969ffSMatt Jacob 	pp = g_provider_by_name(name);
9722b4969ffSMatt Jacob 	if (pp == NULL) {
973e770bc6bSMatt Jacob 		gctl_error(req, "Provider %s is invalid", name);
974e770bc6bSMatt Jacob 		return;
975e770bc6bSMatt Jacob 	}
976e770bc6bSMatt Jacob 
977e770bc6bSMatt Jacob 	/*
9780c883cefSAlexander Motin 	 * Check to make sure parameters match.
979e770bc6bSMatt Jacob 	 */
9800c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
9810c883cefSAlexander Motin 		if (cp->provider == pp) {
9820c883cefSAlexander Motin 			gctl_error(req, "provider %s is already there",
9830c883cefSAlexander Motin 			    pp->name);
984e770bc6bSMatt Jacob 			return;
985e770bc6bSMatt Jacob 		}
9860c883cefSAlexander Motin 	}
987e6afd72bSAlexander Motin 	if (sc->sc_pp->mediasize != 0 &&
9880c883cefSAlexander Motin 	    sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0)
9890c883cefSAlexander Motin 	     != pp->mediasize) {
9900c883cefSAlexander Motin 		gctl_error(req, "Providers size mismatch %jd != %jd",
9910c883cefSAlexander Motin 		    (intmax_t) sc->sc_pp->mediasize +
9920c883cefSAlexander Motin 			(sc->sc_uuid[0] != 0 ? pp->sectorsize : 0),
9930c883cefSAlexander Motin 		    (intmax_t) pp->mediasize);
994e770bc6bSMatt Jacob 		return;
995e770bc6bSMatt Jacob 	}
996e6afd72bSAlexander Motin 	if (sc->sc_pp->sectorsize != 0 &&
9970c883cefSAlexander Motin 	    sc->sc_pp->sectorsize != pp->sectorsize) {
9980c883cefSAlexander Motin 		gctl_error(req, "Providers sectorsize mismatch %u != %u",
9990c883cefSAlexander Motin 		    sc->sc_pp->sectorsize, pp->sectorsize);
1000e770bc6bSMatt Jacob 		return;
1001e770bc6bSMatt Jacob 	}
1002e770bc6bSMatt Jacob 
1003d3fef0a0SAlexander Motin 	error = g_multipath_add_disk(gp, pp);
1004d3fef0a0SAlexander Motin 	if (error != 0)
1005d3fef0a0SAlexander Motin 		gctl_error(req, "Provider addition error: %d", error);
1006e770bc6bSMatt Jacob }
1007e770bc6bSMatt Jacob 
10080c883cefSAlexander Motin static void
g_multipath_ctl_prefer(struct gctl_req * req,struct g_class * mp)100971ee4ef0SThomas Quinot g_multipath_ctl_prefer(struct gctl_req *req, struct g_class *mp)
101071ee4ef0SThomas Quinot {
101171ee4ef0SThomas Quinot 	struct g_geom *gp;
101271ee4ef0SThomas Quinot 	struct g_multipath_softc *sc;
101371ee4ef0SThomas Quinot 	struct g_consumer *cp;
101471ee4ef0SThomas Quinot 	const char *name, *mpname;
10158510f61aSXin LI 	static const char devpf[6] = _PATH_DEV;
101671ee4ef0SThomas Quinot 	int *nargs;
101771ee4ef0SThomas Quinot 
101871ee4ef0SThomas Quinot 	g_topology_assert();
101971ee4ef0SThomas Quinot 
102071ee4ef0SThomas Quinot 	mpname = gctl_get_asciiparam(req, "arg0");
102171ee4ef0SThomas Quinot         if (mpname == NULL) {
102271ee4ef0SThomas Quinot                 gctl_error(req, "No 'arg0' argument");
102371ee4ef0SThomas Quinot                 return;
102471ee4ef0SThomas Quinot         }
102571ee4ef0SThomas Quinot 	gp = g_multipath_find_geom(mp, mpname);
102671ee4ef0SThomas Quinot 	if (gp == NULL) {
102771ee4ef0SThomas Quinot 		gctl_error(req, "Device %s is invalid", mpname);
102871ee4ef0SThomas Quinot 		return;
102971ee4ef0SThomas Quinot 	}
103071ee4ef0SThomas Quinot 	sc = gp->softc;
103171ee4ef0SThomas Quinot 
103271ee4ef0SThomas Quinot 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
103371ee4ef0SThomas Quinot 	if (nargs == NULL) {
103471ee4ef0SThomas Quinot 		gctl_error(req, "No 'nargs' argument");
103571ee4ef0SThomas Quinot 		return;
103671ee4ef0SThomas Quinot 	}
103771ee4ef0SThomas Quinot 	if (*nargs != 2) {
103871ee4ef0SThomas Quinot 		gctl_error(req, "missing device");
103971ee4ef0SThomas Quinot 		return;
104071ee4ef0SThomas Quinot 	}
104171ee4ef0SThomas Quinot 
104271ee4ef0SThomas Quinot 	name = gctl_get_asciiparam(req, "arg1");
104371ee4ef0SThomas Quinot 	if (name == NULL) {
104471ee4ef0SThomas Quinot 		gctl_error(req, "No 'arg1' argument");
104571ee4ef0SThomas Quinot 		return;
104671ee4ef0SThomas Quinot 	}
104771ee4ef0SThomas Quinot 	if (strncmp(name, devpf, 5) == 0) {
104871ee4ef0SThomas Quinot 		name += 5;
104971ee4ef0SThomas Quinot 	}
105071ee4ef0SThomas Quinot 
105171ee4ef0SThomas Quinot 	LIST_FOREACH(cp, &gp->consumer, consumer) {
105271ee4ef0SThomas Quinot 		if (cp->provider != NULL
105371ee4ef0SThomas Quinot                       && strcmp(cp->provider->name, name) == 0)
105471ee4ef0SThomas Quinot 		    break;
105571ee4ef0SThomas Quinot 	}
105671ee4ef0SThomas Quinot 
105771ee4ef0SThomas Quinot 	if (cp == NULL) {
105871ee4ef0SThomas Quinot 		gctl_error(req, "Provider %s not found", name);
105971ee4ef0SThomas Quinot 		return;
106071ee4ef0SThomas Quinot 	}
106171ee4ef0SThomas Quinot 
106271ee4ef0SThomas Quinot 	mtx_lock(&sc->sc_mtx);
106371ee4ef0SThomas Quinot 
106471ee4ef0SThomas Quinot 	if (cp->index & MP_BAD) {
106571ee4ef0SThomas Quinot 		gctl_error(req, "Consumer %s is invalid", name);
106671ee4ef0SThomas Quinot 		mtx_unlock(&sc->sc_mtx);
106771ee4ef0SThomas Quinot 		return;
106871ee4ef0SThomas Quinot 	}
106971ee4ef0SThomas Quinot 
107071ee4ef0SThomas Quinot 	/* Here when the consumer is present and in good shape */
107171ee4ef0SThomas Quinot 
107271ee4ef0SThomas Quinot 	sc->sc_active = cp;
107371ee4ef0SThomas Quinot 	if (!sc->sc_active_active)
107471ee4ef0SThomas Quinot 	    printf("GEOM_MULTIPATH: %s now active path in %s\n",
107571ee4ef0SThomas Quinot 		sc->sc_active->provider->name, sc->sc_name);
107671ee4ef0SThomas Quinot 
107771ee4ef0SThomas Quinot 	mtx_unlock(&sc->sc_mtx);
107871ee4ef0SThomas Quinot }
107971ee4ef0SThomas Quinot 
108071ee4ef0SThomas Quinot static void
g_multipath_ctl_add(struct gctl_req * req,struct g_class * mp)10810c883cefSAlexander Motin g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp)
10820c883cefSAlexander Motin {
10830c883cefSAlexander Motin 	struct g_geom *gp;
10840c883cefSAlexander Motin 	const char *mpname, *name;
10850c883cefSAlexander Motin 
10860c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
10870c883cefSAlexander Motin         if (mpname == NULL) {
10880c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
10890c883cefSAlexander Motin                 return;
10900c883cefSAlexander Motin         }
10910c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
10920c883cefSAlexander Motin 	if (gp == NULL) {
10930c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
10940c883cefSAlexander Motin 		return;
10950c883cefSAlexander Motin 	}
10960c883cefSAlexander Motin 
10970c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
10980c883cefSAlexander Motin 	if (name == NULL) {
10990c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
11000c883cefSAlexander Motin 		return;
11010c883cefSAlexander Motin 	}
11020c883cefSAlexander Motin 	g_multipath_ctl_add_name(req, mp, name);
11030c883cefSAlexander Motin }
11040c883cefSAlexander Motin 
11050c883cefSAlexander Motin static void
g_multipath_ctl_create(struct gctl_req * req,struct g_class * mp)11060c883cefSAlexander Motin g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp)
11070c883cefSAlexander Motin {
11080c883cefSAlexander Motin 	struct g_multipath_metadata md;
11090c883cefSAlexander Motin 	struct g_multipath_softc *sc;
11100c883cefSAlexander Motin 	struct g_geom *gp;
11110c883cefSAlexander Motin 	const char *mpname, *name;
11120c883cefSAlexander Motin 	char param[16];
111363297dfdSAlexander Motin 	int *nargs, i, *val;
11140c883cefSAlexander Motin 
11150c883cefSAlexander Motin 	g_topology_assert();
11160c883cefSAlexander Motin 
11170c883cefSAlexander Motin 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
11180c883cefSAlexander Motin 	if (*nargs < 2) {
11190c883cefSAlexander Motin 		gctl_error(req, "wrong number of arguments.");
11200c883cefSAlexander Motin 		return;
11210c883cefSAlexander Motin 	}
11220c883cefSAlexander Motin 
11230c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
11240c883cefSAlexander Motin         if (mpname == NULL) {
11250c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
11260c883cefSAlexander Motin                 return;
11270c883cefSAlexander Motin         }
11280c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
11290c883cefSAlexander Motin 	if (gp != NULL) {
11300c883cefSAlexander Motin 		gctl_error(req, "Device %s already exist", mpname);
11310c883cefSAlexander Motin 		return;
11320c883cefSAlexander Motin 	}
11330c883cefSAlexander Motin 
11340c883cefSAlexander Motin 	memset(&md, 0, sizeof(md));
11350c883cefSAlexander Motin 	strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
11360c883cefSAlexander Motin 	md.md_version = G_MULTIPATH_VERSION;
11370c883cefSAlexander Motin 	strlcpy(md.md_name, mpname, sizeof(md.md_name));
11380c883cefSAlexander Motin 	md.md_size = 0;
11390c883cefSAlexander Motin 	md.md_sectorsize = 0;
11400c883cefSAlexander Motin 	md.md_uuid[0] = 0;
114163297dfdSAlexander Motin 	md.md_active_active = 0;
114263297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_active", sizeof(*val));
114363297dfdSAlexander Motin 	if (val != NULL && *val != 0)
114463297dfdSAlexander Motin 		md.md_active_active = 1;
114563297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_read", sizeof(*val));
114663297dfdSAlexander Motin 	if (val != NULL && *val != 0)
114763297dfdSAlexander Motin 		md.md_active_active = 2;
11480c883cefSAlexander Motin 	gp = g_multipath_create(mp, &md);
11490c883cefSAlexander Motin 	if (gp == NULL) {
11500c883cefSAlexander Motin 		gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n",
11510c883cefSAlexander Motin 		    md.md_name, md.md_uuid);
11520c883cefSAlexander Motin 		return;
11530c883cefSAlexander Motin 	}
11540c883cefSAlexander Motin 	sc = gp->softc;
11550c883cefSAlexander Motin 
11560c883cefSAlexander Motin 	for (i = 1; i < *nargs; i++) {
11570c883cefSAlexander Motin 		snprintf(param, sizeof(param), "arg%d", i);
11580c883cefSAlexander Motin 		name = gctl_get_asciiparam(req, param);
11590c883cefSAlexander Motin 		g_multipath_ctl_add_name(req, mp, name);
11600c883cefSAlexander Motin 	}
11610c883cefSAlexander Motin 
11620c883cefSAlexander Motin 	if (sc->sc_ndisks != (*nargs - 1))
11630c883cefSAlexander Motin 		g_multipath_destroy(gp);
11640c883cefSAlexander Motin }
11650c883cefSAlexander Motin 
11660c883cefSAlexander Motin static void
g_multipath_ctl_configure(struct gctl_req * req,struct g_class * mp)116763297dfdSAlexander Motin g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp)
116863297dfdSAlexander Motin {
116963297dfdSAlexander Motin 	struct g_multipath_softc *sc;
117063297dfdSAlexander Motin 	struct g_geom *gp;
117163297dfdSAlexander Motin 	struct g_consumer *cp;
117263297dfdSAlexander Motin 	struct g_provider *pp;
1173c0b1ef66SAlexander Motin 	struct g_multipath_metadata md;
117463297dfdSAlexander Motin 	const char *name;
117563297dfdSAlexander Motin 	int error, *val;
117663297dfdSAlexander Motin 
117763297dfdSAlexander Motin 	g_topology_assert();
117863297dfdSAlexander Motin 
117963297dfdSAlexander Motin 	name = gctl_get_asciiparam(req, "arg0");
118063297dfdSAlexander Motin 	if (name == NULL) {
118163297dfdSAlexander Motin 		gctl_error(req, "No 'arg0' argument");
118263297dfdSAlexander Motin 		return;
118363297dfdSAlexander Motin 	}
118463297dfdSAlexander Motin 	gp = g_multipath_find_geom(mp, name);
118563297dfdSAlexander Motin 	if (gp == NULL) {
118663297dfdSAlexander Motin 		gctl_error(req, "Device %s is invalid", name);
118763297dfdSAlexander Motin 		return;
118863297dfdSAlexander Motin 	}
118963297dfdSAlexander Motin 	sc = gp->softc;
119063297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_active", sizeof(*val));
119163297dfdSAlexander Motin 	if (val != NULL && *val != 0)
119263297dfdSAlexander Motin 		sc->sc_active_active = 1;
119363297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_read", sizeof(*val));
119463297dfdSAlexander Motin 	if (val != NULL && *val != 0)
119563297dfdSAlexander Motin 		sc->sc_active_active = 2;
119663297dfdSAlexander Motin 	val = gctl_get_paraml(req, "active_passive", sizeof(*val));
119763297dfdSAlexander Motin 	if (val != NULL && *val != 0)
119863297dfdSAlexander Motin 		sc->sc_active_active = 0;
119963297dfdSAlexander Motin 	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
120063297dfdSAlexander Motin 		cp = sc->sc_active;
120163297dfdSAlexander Motin 		pp = cp->provider;
1202c0b1ef66SAlexander Motin 		strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1203c0b1ef66SAlexander Motin 		memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
1204c0b1ef66SAlexander Motin 		strlcpy(md.md_name, name, sizeof(md.md_name));
1205c0b1ef66SAlexander Motin 		md.md_version = G_MULTIPATH_VERSION;
1206c0b1ef66SAlexander Motin 		md.md_size = pp->mediasize;
1207c0b1ef66SAlexander Motin 		md.md_sectorsize = pp->sectorsize;
1208c0b1ef66SAlexander Motin 		md.md_active_active = sc->sc_active_active;
1209f8c79813SAlexander Motin 		error = g_multipath_write_metadata(cp, &md);
121063297dfdSAlexander Motin 		if (error != 0)
121163297dfdSAlexander Motin 			gctl_error(req, "Can't update metadata on %s (%d)",
121263297dfdSAlexander Motin 			    pp->name, error);
121363297dfdSAlexander Motin 	}
121463297dfdSAlexander Motin }
121563297dfdSAlexander Motin 
121663297dfdSAlexander Motin static void
g_multipath_ctl_fail(struct gctl_req * req,struct g_class * mp,int fail)12170c883cefSAlexander Motin g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail)
12180c883cefSAlexander Motin {
12190c883cefSAlexander Motin 	struct g_multipath_softc *sc;
12200c883cefSAlexander Motin 	struct g_geom *gp;
12210c883cefSAlexander Motin 	struct g_consumer *cp;
12220c883cefSAlexander Motin 	const char *mpname, *name;
12230c883cefSAlexander Motin 	int found;
12240c883cefSAlexander Motin 
12250c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
12260c883cefSAlexander Motin         if (mpname == NULL) {
12270c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
12280c883cefSAlexander Motin                 return;
12290c883cefSAlexander Motin         }
12300c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
12310c883cefSAlexander Motin 	if (gp == NULL) {
12320c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
12330c883cefSAlexander Motin 		return;
12340c883cefSAlexander Motin 	}
12350c883cefSAlexander Motin 	sc = gp->softc;
12360c883cefSAlexander Motin 
12370c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
12380c883cefSAlexander Motin 	if (name == NULL) {
12390c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
12400c883cefSAlexander Motin 		return;
12410c883cefSAlexander Motin 	}
12420c883cefSAlexander Motin 
12430c883cefSAlexander Motin 	found = 0;
12440c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
12450c883cefSAlexander Motin 	LIST_FOREACH(cp, &gp->consumer, consumer) {
12460c883cefSAlexander Motin 		if (cp->provider != NULL &&
12470c883cefSAlexander Motin 		    strcmp(cp->provider->name, name) == 0 &&
12480c883cefSAlexander Motin 		    (cp->index & MP_LOST) == 0) {
12490c883cefSAlexander Motin 			found = 1;
125063297dfdSAlexander Motin 			if (!fail == !(cp->index & MP_FAIL))
125163297dfdSAlexander Motin 				continue;
12520c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: %s in %s is marked %s.\n",
12530c883cefSAlexander Motin 				name, sc->sc_name, fail ? "FAIL" : "OK");
12540c883cefSAlexander Motin 			if (fail) {
12550c883cefSAlexander Motin 				g_multipath_fault(cp, MP_FAIL);
125667f72211SAlan Somers 				SDT_PROBE3(geom, multipath, config, fail,
125767f72211SAlan Somers 				    sc->sc_name, cp->provider->name, 0);
12580c883cefSAlexander Motin 			} else {
12590c883cefSAlexander Motin 				cp->index &= ~MP_FAIL;
126067f72211SAlan Somers 				SDT_PROBE2(geom, multipath, config, restore,
126167f72211SAlan Somers 				    sc->sc_name, cp->provider->name);
12620c883cefSAlexander Motin 			}
12630c883cefSAlexander Motin 		}
12640c883cefSAlexander Motin 	}
12650c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
12660c883cefSAlexander Motin 	if (found == 0)
12670c883cefSAlexander Motin 		gctl_error(req, "Provider %s not found", name);
12680c883cefSAlexander Motin }
12690c883cefSAlexander Motin 
12700c883cefSAlexander Motin static void
g_multipath_ctl_remove(struct gctl_req * req,struct g_class * mp)12710c883cefSAlexander Motin g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp)
12720c883cefSAlexander Motin {
12730c883cefSAlexander Motin 	struct g_multipath_softc *sc;
12740c883cefSAlexander Motin 	struct g_geom *gp;
12750c883cefSAlexander Motin 	struct g_consumer *cp, *cp1;
12760c883cefSAlexander Motin 	const char *mpname, *name;
12770c883cefSAlexander Motin 	uintptr_t *cnt;
12780c883cefSAlexander Motin 	int found;
12790c883cefSAlexander Motin 
12800c883cefSAlexander Motin 	mpname = gctl_get_asciiparam(req, "arg0");
12810c883cefSAlexander Motin         if (mpname == NULL) {
12820c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
12830c883cefSAlexander Motin                 return;
12840c883cefSAlexander Motin         }
12850c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, mpname);
12860c883cefSAlexander Motin 	if (gp == NULL) {
12870c883cefSAlexander Motin 		gctl_error(req, "Device %s not found", mpname);
12880c883cefSAlexander Motin 		return;
12890c883cefSAlexander Motin 	}
12900c883cefSAlexander Motin 	sc = gp->softc;
12910c883cefSAlexander Motin 
12920c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg1");
12930c883cefSAlexander Motin 	if (name == NULL) {
12940c883cefSAlexander Motin 		gctl_error(req, "No 'arg1' argument");
12950c883cefSAlexander Motin 		return;
12960c883cefSAlexander Motin 	}
12970c883cefSAlexander Motin 
12980c883cefSAlexander Motin 	found = 0;
12990c883cefSAlexander Motin 	mtx_lock(&sc->sc_mtx);
13000c883cefSAlexander Motin 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
13010c883cefSAlexander Motin 		if (cp->provider != NULL &&
13020c883cefSAlexander Motin 		    strcmp(cp->provider->name, name) == 0 &&
13030c883cefSAlexander Motin 		    (cp->index & MP_LOST) == 0) {
13040c883cefSAlexander Motin 			found = 1;
13050c883cefSAlexander Motin 			printf("GEOM_MULTIPATH: removing %s from %s\n",
13060c883cefSAlexander Motin 			    cp->provider->name, cp->geom->name);
130767f72211SAlan Somers 			SDT_PROBE2(geom, multipath, config, remove,
130867f72211SAlan Somers 			    cp->geom->name, cp->provider->name);
13090c883cefSAlexander Motin 			sc->sc_ndisks--;
13100c883cefSAlexander Motin 			g_multipath_fault(cp, MP_LOST);
13110c883cefSAlexander Motin 			cnt = (uintptr_t *)&cp->private;
13120c883cefSAlexander Motin 			if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
13130c883cefSAlexander Motin 				cp->index |= MP_POSTED;
13140c883cefSAlexander Motin 				mtx_unlock(&sc->sc_mtx);
13150c883cefSAlexander Motin 				g_mpd(cp, 0);
13160c883cefSAlexander Motin 				if (cp1 == NULL)
13170c883cefSAlexander Motin 					return;	/* Recursion happened. */
13180c883cefSAlexander Motin 				mtx_lock(&sc->sc_mtx);
13190c883cefSAlexander Motin 			}
13200c883cefSAlexander Motin 		}
13210c883cefSAlexander Motin 	}
13220c883cefSAlexander Motin 	mtx_unlock(&sc->sc_mtx);
13230c883cefSAlexander Motin 	if (found == 0)
13240c883cefSAlexander Motin 		gctl_error(req, "Provider %s not found", name);
13250c883cefSAlexander Motin }
13260c883cefSAlexander Motin 
1327e770bc6bSMatt Jacob static struct g_geom *
g_multipath_find_geom(struct g_class * mp,const char * name)1328e770bc6bSMatt Jacob g_multipath_find_geom(struct g_class *mp, const char *name)
1329e770bc6bSMatt Jacob {
1330e770bc6bSMatt Jacob 	struct g_geom *gp;
13310c883cefSAlexander Motin 	struct g_multipath_softc *sc;
1332e770bc6bSMatt Jacob 
1333e770bc6bSMatt Jacob 	LIST_FOREACH(gp, &mp->geom, geom) {
13340c883cefSAlexander Motin 		sc = gp->softc;
13350c883cefSAlexander Motin 		if (sc == NULL || sc->sc_stopping)
13360c883cefSAlexander Motin 			continue;
13370c883cefSAlexander Motin 		if (strcmp(gp->name, name) == 0)
1338e770bc6bSMatt Jacob 			return (gp);
1339e770bc6bSMatt Jacob 	}
1340e770bc6bSMatt Jacob 	return (NULL);
1341e770bc6bSMatt Jacob }
1342e770bc6bSMatt Jacob 
1343e770bc6bSMatt Jacob static void
g_multipath_ctl_stop(struct gctl_req * req,struct g_class * mp)13440c883cefSAlexander Motin g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp)
1345e770bc6bSMatt Jacob {
1346e770bc6bSMatt Jacob 	struct g_geom *gp;
1347e770bc6bSMatt Jacob 	const char *name;
1348e770bc6bSMatt Jacob 	int error;
1349e770bc6bSMatt Jacob 
1350e770bc6bSMatt Jacob 	g_topology_assert();
1351e770bc6bSMatt Jacob 
1352e770bc6bSMatt Jacob 	name = gctl_get_asciiparam(req, "arg0");
1353e770bc6bSMatt Jacob         if (name == NULL) {
1354e770bc6bSMatt Jacob                 gctl_error(req, "No 'arg0' argument");
1355e770bc6bSMatt Jacob                 return;
1356e770bc6bSMatt Jacob         }
1357e770bc6bSMatt Jacob 	gp = g_multipath_find_geom(mp, name);
1358e770bc6bSMatt Jacob 	if (gp == NULL) {
1359e770bc6bSMatt Jacob 		gctl_error(req, "Device %s is invalid", name);
1360e770bc6bSMatt Jacob 		return;
1361e770bc6bSMatt Jacob 	}
1362e770bc6bSMatt Jacob 	error = g_multipath_destroy(gp);
13630c883cefSAlexander Motin 	if (error != 0 && error != EINPROGRESS)
13640c883cefSAlexander Motin 		gctl_error(req, "failed to stop %s (err=%d)", name, error);
1365e770bc6bSMatt Jacob }
13660c883cefSAlexander Motin 
13670c883cefSAlexander Motin static void
g_multipath_ctl_destroy(struct gctl_req * req,struct g_class * mp)13680c883cefSAlexander Motin g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
13690c883cefSAlexander Motin {
13700c883cefSAlexander Motin 	struct g_geom *gp;
13710c883cefSAlexander Motin 	struct g_multipath_softc *sc;
13720c883cefSAlexander Motin 	struct g_consumer *cp;
13730c883cefSAlexander Motin 	struct g_provider *pp;
13740c883cefSAlexander Motin 	const char *name;
13750c883cefSAlexander Motin 	uint8_t *buf;
13760c883cefSAlexander Motin 	int error;
13770c883cefSAlexander Motin 
13780c883cefSAlexander Motin 	g_topology_assert();
13790c883cefSAlexander Motin 
13800c883cefSAlexander Motin 	name = gctl_get_asciiparam(req, "arg0");
13810c883cefSAlexander Motin         if (name == NULL) {
13820c883cefSAlexander Motin                 gctl_error(req, "No 'arg0' argument");
13830c883cefSAlexander Motin                 return;
13840c883cefSAlexander Motin         }
13850c883cefSAlexander Motin 	gp = g_multipath_find_geom(mp, name);
13860c883cefSAlexander Motin 	if (gp == NULL) {
13870c883cefSAlexander Motin 		gctl_error(req, "Device %s is invalid", name);
13880c883cefSAlexander Motin 		return;
13890c883cefSAlexander Motin 	}
13900c883cefSAlexander Motin 	sc = gp->softc;
13910c883cefSAlexander Motin 
13920c883cefSAlexander Motin 	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
13930c883cefSAlexander Motin 		cp = sc->sc_active;
13940c883cefSAlexander Motin 		pp = cp->provider;
13950c883cefSAlexander Motin 		error = g_access(cp, 1, 1, 1);
13960c883cefSAlexander Motin 		if (error != 0) {
13970c883cefSAlexander Motin 			gctl_error(req, "Can't open %s (%d)", pp->name, error);
13980c883cefSAlexander Motin 			goto destroy;
13990c883cefSAlexander Motin 		}
14000c883cefSAlexander Motin 		g_topology_unlock();
14010c883cefSAlexander Motin 		buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
14020c883cefSAlexander Motin 		error = g_write_data(cp, pp->mediasize - pp->sectorsize,
14030c883cefSAlexander Motin 		    buf, pp->sectorsize);
14040c883cefSAlexander Motin 		g_topology_lock();
14050c883cefSAlexander Motin 		g_access(cp, -1, -1, -1);
14060c883cefSAlexander Motin 		if (error != 0)
14070c883cefSAlexander Motin 			gctl_error(req, "Can't erase metadata on %s (%d)",
14080c883cefSAlexander Motin 			    pp->name, error);
14090c883cefSAlexander Motin 	}
14100c883cefSAlexander Motin 
14110c883cefSAlexander Motin destroy:
14120c883cefSAlexander Motin 	error = g_multipath_destroy(gp);
14130c883cefSAlexander Motin 	if (error != 0 && error != EINPROGRESS)
14140c883cefSAlexander Motin 		gctl_error(req, "failed to destroy %s (err=%d)", name, error);
1415e770bc6bSMatt Jacob }
1416e770bc6bSMatt Jacob 
1417e770bc6bSMatt Jacob static void
g_multipath_ctl_rotate(struct gctl_req * req,struct g_class * mp)1418b5dce617SMatt Jacob g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp)
1419b5dce617SMatt Jacob {
1420b5dce617SMatt Jacob 	struct g_geom *gp;
1421b5dce617SMatt Jacob 	const char *name;
1422b5dce617SMatt Jacob 	int error;
1423b5dce617SMatt Jacob 
1424b5dce617SMatt Jacob 	g_topology_assert();
1425b5dce617SMatt Jacob 
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 	error = g_multipath_rotate(gp);
1437b5dce617SMatt Jacob 	if (error != 0) {
1438b5dce617SMatt Jacob 		gctl_error(req, "failed to rotate %s (err=%d)", name, error);
1439b5dce617SMatt Jacob 	}
1440b5dce617SMatt Jacob }
1441b5dce617SMatt Jacob 
1442b5dce617SMatt Jacob static void
g_multipath_ctl_getactive(struct gctl_req * req,struct g_class * mp)1443b5dce617SMatt Jacob g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp)
1444b5dce617SMatt Jacob {
1445b5dce617SMatt Jacob 	struct sbuf *sb;
1446b5dce617SMatt Jacob 	struct g_geom *gp;
1447b5dce617SMatt Jacob 	struct g_multipath_softc *sc;
14480c883cefSAlexander Motin 	struct g_consumer *cp;
1449b5dce617SMatt Jacob 	const char *name;
14500c883cefSAlexander Motin 	int empty;
1451b5dce617SMatt Jacob 
1452b5dce617SMatt Jacob 	sb = sbuf_new_auto();
1453b5dce617SMatt Jacob 
1454b5dce617SMatt Jacob 	g_topology_assert();
1455b5dce617SMatt Jacob 	name = gctl_get_asciiparam(req, "arg0");
1456b5dce617SMatt Jacob         if (name == NULL) {
1457b5dce617SMatt Jacob                 gctl_error(req, "No 'arg0' argument");
1458b5dce617SMatt Jacob                 return;
1459b5dce617SMatt Jacob         }
1460b5dce617SMatt Jacob 	gp = g_multipath_find_geom(mp, name);
1461b5dce617SMatt Jacob 	if (gp == NULL) {
1462b5dce617SMatt Jacob 		gctl_error(req, "Device %s is invalid", name);
1463b5dce617SMatt Jacob 		return;
1464b5dce617SMatt Jacob 	}
1465b5dce617SMatt Jacob 	sc = gp->softc;
146663297dfdSAlexander Motin 	if (sc->sc_active_active == 1) {
14670c883cefSAlexander Motin 		empty = 1;
14680c883cefSAlexander Motin 		LIST_FOREACH(cp, &gp->consumer, consumer) {
14690c883cefSAlexander Motin 			if (cp->index & MP_BAD)
14700c883cefSAlexander Motin 				continue;
14710c883cefSAlexander Motin 			if (!empty)
14720c883cefSAlexander Motin 				sbuf_cat(sb, " ");
14730c883cefSAlexander Motin 			sbuf_cat(sb, cp->provider->name);
14740c883cefSAlexander Motin 			empty = 0;
14750c883cefSAlexander Motin 		}
14760c883cefSAlexander Motin 		if (empty)
14770c883cefSAlexander Motin 			sbuf_cat(sb, "none");
14780c883cefSAlexander Motin 		sbuf_cat(sb, "\n");
14790c883cefSAlexander Motin 	} else if (sc->sc_active && sc->sc_active->provider) {
14800c883cefSAlexander Motin 		sbuf_printf(sb, "%s\n", sc->sc_active->provider->name);
1481b5dce617SMatt Jacob 	} else {
148249ee0fceSAlexander Motin 		sbuf_cat(sb, "none\n");
1483b5dce617SMatt Jacob 	}
1484b5dce617SMatt Jacob 	sbuf_finish(sb);
1485b5dce617SMatt Jacob 	gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1486b5dce617SMatt Jacob 	sbuf_delete(sb);
1487b5dce617SMatt Jacob }
1488b5dce617SMatt Jacob 
1489b5dce617SMatt Jacob static void
g_multipath_config(struct gctl_req * req,struct g_class * mp,const char * verb)1490e770bc6bSMatt Jacob g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1491e770bc6bSMatt Jacob {
1492e770bc6bSMatt Jacob 	uint32_t *version;
1493e770bc6bSMatt Jacob 	g_topology_assert();
1494e770bc6bSMatt Jacob 	version = gctl_get_paraml(req, "version", sizeof(*version));
1495e770bc6bSMatt Jacob 	if (version == NULL) {
1496e770bc6bSMatt Jacob 		gctl_error(req, "No 'version' argument");
1497e770bc6bSMatt Jacob 	} else if (*version != G_MULTIPATH_VERSION) {
1498e770bc6bSMatt Jacob 		gctl_error(req, "Userland and kernel parts are out of sync");
14992b4969ffSMatt Jacob 	} else if (strcmp(verb, "add") == 0) {
15002b4969ffSMatt Jacob 		g_multipath_ctl_add(req, mp);
150171ee4ef0SThomas Quinot 	} else if (strcmp(verb, "prefer") == 0) {
150271ee4ef0SThomas Quinot 		g_multipath_ctl_prefer(req, mp);
15030c883cefSAlexander Motin 	} else if (strcmp(verb, "create") == 0) {
15040c883cefSAlexander Motin 		g_multipath_ctl_create(req, mp);
150563297dfdSAlexander Motin 	} else if (strcmp(verb, "configure") == 0) {
150663297dfdSAlexander Motin 		g_multipath_ctl_configure(req, mp);
15070c883cefSAlexander Motin 	} else if (strcmp(verb, "stop") == 0) {
15080c883cefSAlexander Motin 		g_multipath_ctl_stop(req, mp);
1509e770bc6bSMatt Jacob 	} else if (strcmp(verb, "destroy") == 0) {
1510e770bc6bSMatt Jacob 		g_multipath_ctl_destroy(req, mp);
15110c883cefSAlexander Motin 	} else if (strcmp(verb, "fail") == 0) {
15120c883cefSAlexander Motin 		g_multipath_ctl_fail(req, mp, 1);
15130c883cefSAlexander Motin 	} else if (strcmp(verb, "restore") == 0) {
15140c883cefSAlexander Motin 		g_multipath_ctl_fail(req, mp, 0);
15150c883cefSAlexander Motin 	} else if (strcmp(verb, "remove") == 0) {
15160c883cefSAlexander Motin 		g_multipath_ctl_remove(req, mp);
1517b5dce617SMatt Jacob 	} else if (strcmp(verb, "rotate") == 0) {
1518b5dce617SMatt Jacob 		g_multipath_ctl_rotate(req, mp);
1519b5dce617SMatt Jacob 	} else if (strcmp(verb, "getactive") == 0) {
1520b5dce617SMatt Jacob 		g_multipath_ctl_getactive(req, mp);
1521e770bc6bSMatt Jacob 	} else {
1522e770bc6bSMatt Jacob 		gctl_error(req, "Unknown verb %s", verb);
1523e770bc6bSMatt Jacob 	}
1524e770bc6bSMatt Jacob }
15250c883cefSAlexander Motin 
15260c883cefSAlexander Motin static void
g_multipath_dumpconf(struct sbuf * sb,const char * indent,struct g_geom * gp,struct g_consumer * cp,struct g_provider * pp)15270c883cefSAlexander Motin g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
15280c883cefSAlexander Motin     struct g_consumer *cp, struct g_provider *pp)
15290c883cefSAlexander Motin {
15300c883cefSAlexander Motin 	struct g_multipath_softc *sc;
15310c883cefSAlexander Motin 	int good;
15320c883cefSAlexander Motin 
15330c883cefSAlexander Motin 	g_topology_assert();
15340c883cefSAlexander Motin 
15350c883cefSAlexander Motin 	sc = gp->softc;
15360c883cefSAlexander Motin 	if (sc == NULL)
15370c883cefSAlexander Motin 		return;
15380c883cefSAlexander Motin 	if (cp != NULL) {
1539a839e332SAlexander Motin 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
15400c883cefSAlexander Motin 		    (cp->index & MP_NEW) ? "NEW" :
15410c883cefSAlexander Motin 		    (cp->index & MP_LOST) ? "LOST" :
15420c883cefSAlexander Motin 		    (cp->index & MP_FAIL) ? "FAIL" :
154363297dfdSAlexander Motin 		    (sc->sc_active_active == 1 || sc->sc_active == cp) ?
154463297dfdSAlexander Motin 		     "ACTIVE" :
154563297dfdSAlexander Motin 		     sc->sc_active_active == 2 ? "READ" : "PASSIVE");
15460c883cefSAlexander Motin 	} else {
15470c883cefSAlexander Motin 		good = g_multipath_good(gp);
1548a839e332SAlexander Motin 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
15490c883cefSAlexander Motin 		    good == 0 ? "BROKEN" :
15500c883cefSAlexander Motin 		    (good != sc->sc_ndisks || sc->sc_ndisks == 1) ?
15510c883cefSAlexander Motin 		    "DEGRADED" : "OPTIMAL");
15520c883cefSAlexander Motin 	}
15530c883cefSAlexander Motin 	if (cp == NULL && pp == NULL) {
1554a839e332SAlexander Motin 		sbuf_printf(sb, "%s<UUID>%s</UUID>\n", indent, sc->sc_uuid);
1555a839e332SAlexander Motin 		sbuf_printf(sb, "%s<Mode>Active/%s</Mode>\n", indent,
155663297dfdSAlexander Motin 		    sc->sc_active_active == 2 ? "Read" :
155763297dfdSAlexander Motin 		    sc->sc_active_active == 1 ? "Active" : "Passive");
1558a839e332SAlexander Motin 		sbuf_printf(sb, "%s<Type>%s</Type>\n", indent,
15590c883cefSAlexander Motin 		    sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC");
15600c883cefSAlexander Motin 	}
15610c883cefSAlexander Motin }
15620c883cefSAlexander Motin 
1563e770bc6bSMatt Jacob DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);
156474d6c131SKyle Evans MODULE_VERSION(geom_multipath, 0);
1565