xref: /freebsd/sys/geom/stripe/g_stripe.c (revision af3b2549c4ba2ef00a7cbb4cb6836598bf0aefbe)
1b09121f9SPawel Jakub Dawidek /*-
2e6890985SPawel Jakub Dawidek  * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3b09121f9SPawel Jakub Dawidek  * All rights reserved.
4b09121f9SPawel Jakub Dawidek  *
5b09121f9SPawel Jakub Dawidek  * Redistribution and use in source and binary forms, with or without
6b09121f9SPawel Jakub Dawidek  * modification, are permitted provided that the following conditions
7b09121f9SPawel Jakub Dawidek  * are met:
8b09121f9SPawel Jakub Dawidek  * 1. Redistributions of source code must retain the above copyright
9b09121f9SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer.
10b09121f9SPawel Jakub Dawidek  * 2. Redistributions in binary form must reproduce the above copyright
11b09121f9SPawel Jakub Dawidek  *    notice, this list of conditions and the following disclaimer in the
12b09121f9SPawel Jakub Dawidek  *    documentation and/or other materials provided with the distribution.
13b09121f9SPawel Jakub Dawidek  *
14b09121f9SPawel Jakub Dawidek  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15b09121f9SPawel Jakub Dawidek  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16b09121f9SPawel Jakub Dawidek  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17b09121f9SPawel Jakub Dawidek  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18b09121f9SPawel Jakub Dawidek  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19b09121f9SPawel Jakub Dawidek  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20b09121f9SPawel Jakub Dawidek  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21b09121f9SPawel Jakub Dawidek  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22b09121f9SPawel Jakub Dawidek  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23b09121f9SPawel Jakub Dawidek  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24b09121f9SPawel Jakub Dawidek  * SUCH DAMAGE.
25b09121f9SPawel Jakub Dawidek  */
26b09121f9SPawel Jakub Dawidek 
27b09121f9SPawel Jakub Dawidek #include <sys/cdefs.h>
28b09121f9SPawel Jakub Dawidek __FBSDID("$FreeBSD$");
29b09121f9SPawel Jakub Dawidek 
30b09121f9SPawel Jakub Dawidek #include <sys/param.h>
31b09121f9SPawel Jakub Dawidek #include <sys/systm.h>
32b09121f9SPawel Jakub Dawidek #include <sys/kernel.h>
33b09121f9SPawel Jakub Dawidek #include <sys/module.h>
34b09121f9SPawel Jakub Dawidek #include <sys/lock.h>
35b09121f9SPawel Jakub Dawidek #include <sys/mutex.h>
36b09121f9SPawel Jakub Dawidek #include <sys/bio.h>
375d807a0eSAndrey V. Elsukov #include <sys/sbuf.h>
38b09121f9SPawel Jakub Dawidek #include <sys/sysctl.h>
39b09121f9SPawel Jakub Dawidek #include <sys/malloc.h>
404c55f05bSPawel Jakub Dawidek #include <vm/uma.h>
41b09121f9SPawel Jakub Dawidek #include <geom/geom.h>
42b09121f9SPawel Jakub Dawidek #include <geom/stripe/g_stripe.h>
43b09121f9SPawel Jakub Dawidek 
44cb08c2ccSAlexander Leidinger FEATURE(geom_stripe, "GEOM striping support");
45b09121f9SPawel Jakub Dawidek 
465bb84bc8SRobert Watson static MALLOC_DEFINE(M_STRIPE, "stripe_data", "GEOM_STRIPE Data");
47b09121f9SPawel Jakub Dawidek 
484c55f05bSPawel Jakub Dawidek static uma_zone_t g_stripe_zone;
49b09121f9SPawel Jakub Dawidek 
50b09121f9SPawel Jakub Dawidek static int g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force);
51b09121f9SPawel Jakub Dawidek static int g_stripe_destroy_geom(struct gctl_req *req, struct g_class *mp,
52b09121f9SPawel Jakub Dawidek     struct g_geom *gp);
53b09121f9SPawel Jakub Dawidek 
54b09121f9SPawel Jakub Dawidek static g_taste_t g_stripe_taste;
55b09121f9SPawel Jakub Dawidek static g_ctl_req_t g_stripe_config;
56b09121f9SPawel Jakub Dawidek static g_dumpconf_t g_stripe_dumpconf;
574c55f05bSPawel Jakub Dawidek static g_init_t g_stripe_init;
584c55f05bSPawel Jakub Dawidek static g_fini_t g_stripe_fini;
59b09121f9SPawel Jakub Dawidek 
60b09121f9SPawel Jakub Dawidek struct g_class g_stripe_class = {
61b09121f9SPawel Jakub Dawidek 	.name = G_STRIPE_CLASS_NAME,
625721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
63b09121f9SPawel Jakub Dawidek 	.ctlreq = g_stripe_config,
64b09121f9SPawel Jakub Dawidek 	.taste = g_stripe_taste,
654c55f05bSPawel Jakub Dawidek 	.destroy_geom = g_stripe_destroy_geom,
664c55f05bSPawel Jakub Dawidek 	.init = g_stripe_init,
674c55f05bSPawel Jakub Dawidek 	.fini = g_stripe_fini
68b09121f9SPawel Jakub Dawidek };
69b09121f9SPawel Jakub Dawidek 
704c55f05bSPawel Jakub Dawidek SYSCTL_DECL(_kern_geom);
716472ac3dSEd Schouten static SYSCTL_NODE(_kern_geom, OID_AUTO, stripe, CTLFLAG_RW, 0,
726472ac3dSEd Schouten     "GEOM_STRIPE stuff");
734c55f05bSPawel Jakub Dawidek static u_int g_stripe_debug = 0;
74*af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, debug, CTLFLAG_RWTUN, &g_stripe_debug, 0,
754c55f05bSPawel Jakub Dawidek     "Debug level");
7653ed4e0dSPawel Jakub Dawidek static int g_stripe_fast = 0;
774c55f05bSPawel Jakub Dawidek static int
784c55f05bSPawel Jakub Dawidek g_sysctl_stripe_fast(SYSCTL_HANDLER_ARGS)
794c55f05bSPawel Jakub Dawidek {
804c55f05bSPawel Jakub Dawidek 	int error, fast;
814c55f05bSPawel Jakub Dawidek 
824c55f05bSPawel Jakub Dawidek 	fast = g_stripe_fast;
83041b706bSDavid Malone 	error = sysctl_handle_int(oidp, &fast, 0, req);
844c55f05bSPawel Jakub Dawidek 	if (error == 0 && req->newptr != NULL)
854c55f05bSPawel Jakub Dawidek 		g_stripe_fast = fast;
864c55f05bSPawel Jakub Dawidek 	return (error);
874c55f05bSPawel Jakub Dawidek }
88*af3b2549SHans Petter Selasky SYSCTL_PROC(_kern_geom_stripe, OID_AUTO, fast, CTLTYPE_INT | CTLFLAG_RWTUN,
8971df0725SPawel Jakub Dawidek     NULL, 0, g_sysctl_stripe_fast, "I", "Fast, but memory-consuming, mode");
90af582ea7SAlexander Motin static u_int g_stripe_maxmem = MAXPHYS * 100;
91*af3b2549SHans Petter Selasky SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, maxmem, CTLFLAG_RDTUN, &g_stripe_maxmem,
9271df0725SPawel Jakub Dawidek     0, "Maximum memory that can be allocated in \"fast\" mode (in bytes)");
93cea36368SPawel Jakub Dawidek static u_int g_stripe_fast_failed = 0;
94cea36368SPawel Jakub Dawidek SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, fast_failed, CTLFLAG_RD,
95cea36368SPawel Jakub Dawidek     &g_stripe_fast_failed, 0, "How many times \"fast\" mode failed");
96b09121f9SPawel Jakub Dawidek 
97b09121f9SPawel Jakub Dawidek /*
98b09121f9SPawel Jakub Dawidek  * Greatest Common Divisor.
99b09121f9SPawel Jakub Dawidek  */
100b09121f9SPawel Jakub Dawidek static u_int
101b09121f9SPawel Jakub Dawidek gcd(u_int a, u_int b)
102b09121f9SPawel Jakub Dawidek {
103b09121f9SPawel Jakub Dawidek 	u_int c;
104b09121f9SPawel Jakub Dawidek 
105b09121f9SPawel Jakub Dawidek 	while (b != 0) {
106b09121f9SPawel Jakub Dawidek 		c = a;
107b09121f9SPawel Jakub Dawidek 		a = b;
108b09121f9SPawel Jakub Dawidek 		b = (c % b);
109b09121f9SPawel Jakub Dawidek 	}
110b09121f9SPawel Jakub Dawidek 	return (a);
111b09121f9SPawel Jakub Dawidek }
112b09121f9SPawel Jakub Dawidek 
113b09121f9SPawel Jakub Dawidek /*
114b09121f9SPawel Jakub Dawidek  * Least Common Multiple.
115b09121f9SPawel Jakub Dawidek  */
116b09121f9SPawel Jakub Dawidek static u_int
117b09121f9SPawel Jakub Dawidek lcm(u_int a, u_int b)
118b09121f9SPawel Jakub Dawidek {
119b09121f9SPawel Jakub Dawidek 
120b09121f9SPawel Jakub Dawidek 	return ((a * b) / gcd(a, b));
121b09121f9SPawel Jakub Dawidek }
122b09121f9SPawel Jakub Dawidek 
1234c55f05bSPawel Jakub Dawidek static void
1244c55f05bSPawel Jakub Dawidek g_stripe_init(struct g_class *mp __unused)
1254c55f05bSPawel Jakub Dawidek {
1264c55f05bSPawel Jakub Dawidek 
127af582ea7SAlexander Motin 	g_stripe_zone = uma_zcreate("g_stripe_zone", MAXPHYS, NULL, NULL,
1284c55f05bSPawel Jakub Dawidek 	    NULL, NULL, 0, 0);
129af582ea7SAlexander Motin 	g_stripe_maxmem -= g_stripe_maxmem % MAXPHYS;
130af582ea7SAlexander Motin 	uma_zone_set_max(g_stripe_zone, g_stripe_maxmem / MAXPHYS);
1314c55f05bSPawel Jakub Dawidek }
1324c55f05bSPawel Jakub Dawidek 
1334c55f05bSPawel Jakub Dawidek static void
1344c55f05bSPawel Jakub Dawidek g_stripe_fini(struct g_class *mp __unused)
1354c55f05bSPawel Jakub Dawidek {
1364c55f05bSPawel Jakub Dawidek 
1374c55f05bSPawel Jakub Dawidek 	uma_zdestroy(g_stripe_zone);
1384c55f05bSPawel Jakub Dawidek }
1394c55f05bSPawel Jakub Dawidek 
140b09121f9SPawel Jakub Dawidek /*
141b09121f9SPawel Jakub Dawidek  * Return the number of valid disks.
142b09121f9SPawel Jakub Dawidek  */
143b09121f9SPawel Jakub Dawidek static u_int
144b09121f9SPawel Jakub Dawidek g_stripe_nvalid(struct g_stripe_softc *sc)
145b09121f9SPawel Jakub Dawidek {
146b09121f9SPawel Jakub Dawidek 	u_int i, no;
147b09121f9SPawel Jakub Dawidek 
148b09121f9SPawel Jakub Dawidek 	no = 0;
149b09121f9SPawel Jakub Dawidek 	for (i = 0; i < sc->sc_ndisks; i++) {
150b09121f9SPawel Jakub Dawidek 		if (sc->sc_disks[i] != NULL)
151b09121f9SPawel Jakub Dawidek 			no++;
152b09121f9SPawel Jakub Dawidek 	}
153b09121f9SPawel Jakub Dawidek 
154b09121f9SPawel Jakub Dawidek 	return (no);
155b09121f9SPawel Jakub Dawidek }
156b09121f9SPawel Jakub Dawidek 
157b09121f9SPawel Jakub Dawidek static void
158b09121f9SPawel Jakub Dawidek g_stripe_remove_disk(struct g_consumer *cp)
159b09121f9SPawel Jakub Dawidek {
160b09121f9SPawel Jakub Dawidek 	struct g_stripe_softc *sc;
161b09121f9SPawel Jakub Dawidek 
1620849a53fSAlexander Motin 	g_topology_assert();
163b09121f9SPawel Jakub Dawidek 	KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
1640849a53fSAlexander Motin 	sc = (struct g_stripe_softc *)cp->geom->softc;
165b09121f9SPawel Jakub Dawidek 	KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
166b09121f9SPawel Jakub Dawidek 
1670849a53fSAlexander Motin 	if (cp->private == NULL) {
1680849a53fSAlexander Motin 		G_STRIPE_DEBUG(0, "Disk %s removed from %s.",
1690849a53fSAlexander Motin 		    cp->provider->name, sc->sc_name);
1700849a53fSAlexander Motin 		cp->private = (void *)(uintptr_t)-1;
1710849a53fSAlexander Motin 	}
172b09121f9SPawel Jakub Dawidek 
173b09121f9SPawel Jakub Dawidek 	if (sc->sc_provider != NULL) {
1740499edf4SPawel Jakub Dawidek 		sc->sc_provider->flags |= G_PF_WITHER;
1750849a53fSAlexander Motin 		G_STRIPE_DEBUG(0, "Device %s deactivated.",
1760849a53fSAlexander Motin 		    sc->sc_provider->name);
177b09121f9SPawel Jakub Dawidek 		g_orphan_provider(sc->sc_provider, ENXIO);
178b09121f9SPawel Jakub Dawidek 		sc->sc_provider = NULL;
179b09121f9SPawel Jakub Dawidek 	}
180b09121f9SPawel Jakub Dawidek 
181b09121f9SPawel Jakub Dawidek 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
1820849a53fSAlexander Motin 		return;
1830849a53fSAlexander Motin 	sc->sc_disks[cp->index] = NULL;
1840849a53fSAlexander Motin 	cp->index = 0;
185b09121f9SPawel Jakub Dawidek 	g_detach(cp);
186b09121f9SPawel Jakub Dawidek 	g_destroy_consumer(cp);
1870849a53fSAlexander Motin 	/* If there are no valid disks anymore, remove device. */
1880849a53fSAlexander Motin 	if (LIST_EMPTY(&sc->sc_geom->consumer))
1890849a53fSAlexander Motin 		g_stripe_destroy(sc, 1);
190b09121f9SPawel Jakub Dawidek }
191b09121f9SPawel Jakub Dawidek 
192b09121f9SPawel Jakub Dawidek static void
193b09121f9SPawel Jakub Dawidek g_stripe_orphan(struct g_consumer *cp)
194b09121f9SPawel Jakub Dawidek {
195b09121f9SPawel Jakub Dawidek 	struct g_stripe_softc *sc;
196b09121f9SPawel Jakub Dawidek 	struct g_geom *gp;
197b09121f9SPawel Jakub Dawidek 
198b09121f9SPawel Jakub Dawidek 	g_topology_assert();
199b09121f9SPawel Jakub Dawidek 	gp = cp->geom;
200b09121f9SPawel Jakub Dawidek 	sc = gp->softc;
201b09121f9SPawel Jakub Dawidek 	if (sc == NULL)
202b09121f9SPawel Jakub Dawidek 		return;
203b09121f9SPawel Jakub Dawidek 
204b09121f9SPawel Jakub Dawidek 	g_stripe_remove_disk(cp);
205b09121f9SPawel Jakub Dawidek }
206b09121f9SPawel Jakub Dawidek 
207b09121f9SPawel Jakub Dawidek static int
208b09121f9SPawel Jakub Dawidek g_stripe_access(struct g_provider *pp, int dr, int dw, int de)
209b09121f9SPawel Jakub Dawidek {
2100849a53fSAlexander Motin 	struct g_consumer *cp1, *cp2, *tmp;
211b09121f9SPawel Jakub Dawidek 	struct g_stripe_softc *sc;
212b09121f9SPawel Jakub Dawidek 	struct g_geom *gp;
213b09121f9SPawel Jakub Dawidek 	int error;
214b09121f9SPawel Jakub Dawidek 
2150849a53fSAlexander Motin 	g_topology_assert();
216b09121f9SPawel Jakub Dawidek 	gp = pp->geom;
217b09121f9SPawel Jakub Dawidek 	sc = gp->softc;
2180849a53fSAlexander Motin 	KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
219b09121f9SPawel Jakub Dawidek 
220b09121f9SPawel Jakub Dawidek 	/* On first open, grab an extra "exclusive" bit */
221b09121f9SPawel Jakub Dawidek 	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
222b09121f9SPawel Jakub Dawidek 		de++;
223b09121f9SPawel Jakub Dawidek 	/* ... and let go of it on last close */
224b09121f9SPawel Jakub Dawidek 	if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
225b09121f9SPawel Jakub Dawidek 		de--;
226b09121f9SPawel Jakub Dawidek 
2270849a53fSAlexander Motin 	LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) {
228b09121f9SPawel Jakub Dawidek 		error = g_access(cp1, dr, dw, de);
2290849a53fSAlexander Motin 		if (error != 0)
2300849a53fSAlexander Motin 			goto fail;
2310849a53fSAlexander Motin 		if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 &&
2320849a53fSAlexander Motin 		    cp1->private != NULL) {
2330849a53fSAlexander Motin 			g_stripe_remove_disk(cp1); /* May destroy geom. */
2340849a53fSAlexander Motin 		}
2350849a53fSAlexander Motin 	}
2360849a53fSAlexander Motin 	return (0);
2370849a53fSAlexander Motin 
2380849a53fSAlexander Motin fail:
239b09121f9SPawel Jakub Dawidek 	LIST_FOREACH(cp2, &gp->consumer, consumer) {
240b09121f9SPawel Jakub Dawidek 		if (cp1 == cp2)
2410849a53fSAlexander Motin 			break;
242b09121f9SPawel Jakub Dawidek 		g_access(cp2, -dr, -dw, -de);
243b09121f9SPawel Jakub Dawidek 	}
244b09121f9SPawel Jakub Dawidek 	return (error);
245b09121f9SPawel Jakub Dawidek }
246b09121f9SPawel Jakub Dawidek 
247b09121f9SPawel Jakub Dawidek static void
2484c55f05bSPawel Jakub Dawidek g_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset,
2494c55f05bSPawel Jakub Dawidek     off_t length, int mode)
2504c55f05bSPawel Jakub Dawidek {
2514c55f05bSPawel Jakub Dawidek 	u_int stripesize;
2524c55f05bSPawel Jakub Dawidek 	size_t len;
2534c55f05bSPawel Jakub Dawidek 
2544c55f05bSPawel Jakub Dawidek 	stripesize = sc->sc_stripesize;
2554c55f05bSPawel Jakub Dawidek 	len = (size_t)(stripesize - (offset & (stripesize - 1)));
2564c55f05bSPawel Jakub Dawidek 	do {
2574c55f05bSPawel Jakub Dawidek 		bcopy(src, dst, len);
2584c55f05bSPawel Jakub Dawidek 		if (mode) {
2594c55f05bSPawel Jakub Dawidek 			dst += len + stripesize * (sc->sc_ndisks - 1);
2604c55f05bSPawel Jakub Dawidek 			src += len;
2614c55f05bSPawel Jakub Dawidek 		} else {
2624c55f05bSPawel Jakub Dawidek 			dst += len;
2634c55f05bSPawel Jakub Dawidek 			src += len + stripesize * (sc->sc_ndisks - 1);
2644c55f05bSPawel Jakub Dawidek 		}
2654c55f05bSPawel Jakub Dawidek 		length -= len;
2664c55f05bSPawel Jakub Dawidek 		KASSERT(length >= 0,
2674c55f05bSPawel Jakub Dawidek 		    ("Length < 0 (stripesize=%zu, offset=%jd, length=%jd).",
2684c55f05bSPawel Jakub Dawidek 		    (size_t)stripesize, (intmax_t)offset, (intmax_t)length));
2694c55f05bSPawel Jakub Dawidek 		if (length > stripesize)
2704c55f05bSPawel Jakub Dawidek 			len = stripesize;
2714c55f05bSPawel Jakub Dawidek 		else
2724c55f05bSPawel Jakub Dawidek 			len = length;
2734c55f05bSPawel Jakub Dawidek 	} while (length > 0);
2744c55f05bSPawel Jakub Dawidek }
2754c55f05bSPawel Jakub Dawidek 
2764c55f05bSPawel Jakub Dawidek static void
2774c55f05bSPawel Jakub Dawidek g_stripe_done(struct bio *bp)
2784c55f05bSPawel Jakub Dawidek {
2794c55f05bSPawel Jakub Dawidek 	struct g_stripe_softc *sc;
2804c55f05bSPawel Jakub Dawidek 	struct bio *pbp;
2814c55f05bSPawel Jakub Dawidek 
2824c55f05bSPawel Jakub Dawidek 	pbp = bp->bio_parent;
2834c55f05bSPawel Jakub Dawidek 	sc = pbp->bio_to->geom->softc;
284ec704301SPawel Jakub Dawidek 	if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) {
285ec704301SPawel Jakub Dawidek 		g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset,
2864c55f05bSPawel Jakub Dawidek 		    bp->bio_length, 1);
287ec704301SPawel Jakub Dawidek 		bp->bio_data = bp->bio_caller1;
288ec704301SPawel Jakub Dawidek 		bp->bio_caller1 = NULL;
2894c55f05bSPawel Jakub Dawidek 	}
29040ea77a0SAlexander Motin 	mtx_lock(&sc->sc_lock);
29140ea77a0SAlexander Motin 	if (pbp->bio_error == 0)
29240ea77a0SAlexander Motin 		pbp->bio_error = bp->bio_error;
29340ea77a0SAlexander Motin 	pbp->bio_completed += bp->bio_completed;
2944c55f05bSPawel Jakub Dawidek 	pbp->bio_inbed++;
2954c55f05bSPawel Jakub Dawidek 	if (pbp->bio_children == pbp->bio_inbed) {
29640ea77a0SAlexander Motin 		mtx_unlock(&sc->sc_lock);
297ec704301SPawel Jakub Dawidek 		if (pbp->bio_driver1 != NULL)
298ec704301SPawel Jakub Dawidek 			uma_zfree(g_stripe_zone, pbp->bio_driver1);
2994c55f05bSPawel Jakub Dawidek 		g_io_deliver(pbp, pbp->bio_error);
30040ea77a0SAlexander Motin 	} else
30140ea77a0SAlexander Motin 		mtx_unlock(&sc->sc_lock);
30240ea77a0SAlexander Motin 	g_destroy_bio(bp);
3034c55f05bSPawel Jakub Dawidek }
3044c55f05bSPawel Jakub Dawidek 
3054c55f05bSPawel Jakub Dawidek static int
3064c55f05bSPawel Jakub Dawidek g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
3074c55f05bSPawel Jakub Dawidek {
3084c55f05bSPawel Jakub Dawidek 	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
3094c55f05bSPawel Jakub Dawidek 	u_int nparts = 0, stripesize;
3104c55f05bSPawel Jakub Dawidek 	struct g_stripe_softc *sc;
3114c55f05bSPawel Jakub Dawidek 	char *addr, *data = NULL;
3124c55f05bSPawel Jakub Dawidek 	struct bio *cbp;
3134c55f05bSPawel Jakub Dawidek 	int error;
3144c55f05bSPawel Jakub Dawidek 
3154c55f05bSPawel Jakub Dawidek 	sc = bp->bio_to->geom->softc;
3164c55f05bSPawel Jakub Dawidek 
3174c55f05bSPawel Jakub Dawidek 	addr = bp->bio_data;
3184c55f05bSPawel Jakub Dawidek 	stripesize = sc->sc_stripesize;
3194c55f05bSPawel Jakub Dawidek 
3204c55f05bSPawel Jakub Dawidek 	cbp = g_clone_bio(bp);
3214c55f05bSPawel Jakub Dawidek 	if (cbp == NULL) {
3224c55f05bSPawel Jakub Dawidek 		error = ENOMEM;
3234c55f05bSPawel Jakub Dawidek 		goto failure;
3244c55f05bSPawel Jakub Dawidek 	}
3254c55f05bSPawel Jakub Dawidek 	TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
3264c55f05bSPawel Jakub Dawidek 	nparts++;
3274c55f05bSPawel Jakub Dawidek 	/*
3284c55f05bSPawel Jakub Dawidek 	 * Fill in the component buf structure.
3294c55f05bSPawel Jakub Dawidek 	 */
3304c55f05bSPawel Jakub Dawidek 	cbp->bio_done = g_stripe_done;
3314c55f05bSPawel Jakub Dawidek 	cbp->bio_offset = offset;
3324c55f05bSPawel Jakub Dawidek 	cbp->bio_data = addr;
333ec704301SPawel Jakub Dawidek 	cbp->bio_caller1 = NULL;
3344c55f05bSPawel Jakub Dawidek 	cbp->bio_length = length;
335ec704301SPawel Jakub Dawidek 	cbp->bio_caller2 = sc->sc_disks[no];
3364c55f05bSPawel Jakub Dawidek 
3374c55f05bSPawel Jakub Dawidek 	/* offset -= offset % stripesize; */
3384c55f05bSPawel Jakub Dawidek 	offset -= offset & (stripesize - 1);
3394c55f05bSPawel Jakub Dawidek 	addr += length;
3404c55f05bSPawel Jakub Dawidek 	length = bp->bio_length - length;
3414c55f05bSPawel Jakub Dawidek 	for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
3424c55f05bSPawel Jakub Dawidek 		if (no > sc->sc_ndisks - 1) {
3434c55f05bSPawel Jakub Dawidek 			no = 0;
3444c55f05bSPawel Jakub Dawidek 			offset += stripesize;
3454c55f05bSPawel Jakub Dawidek 		}
3464c55f05bSPawel Jakub Dawidek 		if (nparts >= sc->sc_ndisks) {
3474c55f05bSPawel Jakub Dawidek 			cbp = TAILQ_NEXT(cbp, bio_queue);
3484c55f05bSPawel Jakub Dawidek 			if (cbp == NULL)
3494c55f05bSPawel Jakub Dawidek 				cbp = TAILQ_FIRST(&queue);
3504c55f05bSPawel Jakub Dawidek 			nparts++;
3514c55f05bSPawel Jakub Dawidek 			/*
3524c55f05bSPawel Jakub Dawidek 			 * Update bio structure.
3534c55f05bSPawel Jakub Dawidek 			 */
3544c55f05bSPawel Jakub Dawidek 			/*
3554c55f05bSPawel Jakub Dawidek 			 * MIN() is in case when
3564c55f05bSPawel Jakub Dawidek 			 * (bp->bio_length % sc->sc_stripesize) != 0.
3574c55f05bSPawel Jakub Dawidek 			 */
3584c55f05bSPawel Jakub Dawidek 			cbp->bio_length += MIN(stripesize, length);
359ec704301SPawel Jakub Dawidek 			if (cbp->bio_caller1 == NULL) {
360ec704301SPawel Jakub Dawidek 				cbp->bio_caller1 = cbp->bio_data;
3614c55f05bSPawel Jakub Dawidek 				cbp->bio_data = NULL;
3624c55f05bSPawel Jakub Dawidek 				if (data == NULL) {
3634c55f05bSPawel Jakub Dawidek 					data = uma_zalloc(g_stripe_zone,
3644c55f05bSPawel Jakub Dawidek 					    M_NOWAIT);
3654c55f05bSPawel Jakub Dawidek 					if (data == NULL) {
3664c55f05bSPawel Jakub Dawidek 						error = ENOMEM;
3674c55f05bSPawel Jakub Dawidek 						goto failure;
3684c55f05bSPawel Jakub Dawidek 					}
3694c55f05bSPawel Jakub Dawidek 				}
3704c55f05bSPawel Jakub Dawidek 			}
3714c55f05bSPawel Jakub Dawidek 		} else {
3724c55f05bSPawel Jakub Dawidek 			cbp = g_clone_bio(bp);
3734c55f05bSPawel Jakub Dawidek 			if (cbp == NULL) {
3744c55f05bSPawel Jakub Dawidek 				error = ENOMEM;
3754c55f05bSPawel Jakub Dawidek 				goto failure;
3764c55f05bSPawel Jakub Dawidek 			}
3774c55f05bSPawel Jakub Dawidek 			TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
3784c55f05bSPawel Jakub Dawidek 			nparts++;
3794c55f05bSPawel Jakub Dawidek 			/*
3804c55f05bSPawel Jakub Dawidek 			 * Fill in the component buf structure.
3814c55f05bSPawel Jakub Dawidek 			 */
3824c55f05bSPawel Jakub Dawidek 			cbp->bio_done = g_stripe_done;
3834c55f05bSPawel Jakub Dawidek 			cbp->bio_offset = offset;
3844c55f05bSPawel Jakub Dawidek 			cbp->bio_data = addr;
385ec704301SPawel Jakub Dawidek 			cbp->bio_caller1 = NULL;
3864c55f05bSPawel Jakub Dawidek 			/*
3874c55f05bSPawel Jakub Dawidek 			 * MIN() is in case when
3884c55f05bSPawel Jakub Dawidek 			 * (bp->bio_length % sc->sc_stripesize) != 0.
3894c55f05bSPawel Jakub Dawidek 			 */
3904c55f05bSPawel Jakub Dawidek 			cbp->bio_length = MIN(stripesize, length);
391ec704301SPawel Jakub Dawidek 			cbp->bio_caller2 = sc->sc_disks[no];
3924c55f05bSPawel Jakub Dawidek 		}
3934c55f05bSPawel Jakub Dawidek 	}
3944c55f05bSPawel Jakub Dawidek 	if (data != NULL)
3954ffa3fefSPawel Jakub Dawidek 		bp->bio_driver1 = data;
3964c55f05bSPawel Jakub Dawidek 	/*
3974c55f05bSPawel Jakub Dawidek 	 * Fire off all allocated requests!
3984c55f05bSPawel Jakub Dawidek 	 */
3994c55f05bSPawel Jakub Dawidek 	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
4004c55f05bSPawel Jakub Dawidek 		struct g_consumer *cp;
4014c55f05bSPawel Jakub Dawidek 
4024c55f05bSPawel Jakub Dawidek 		TAILQ_REMOVE(&queue, cbp, bio_queue);
403ec704301SPawel Jakub Dawidek 		cp = cbp->bio_caller2;
404ec704301SPawel Jakub Dawidek 		cbp->bio_caller2 = NULL;
4054c55f05bSPawel Jakub Dawidek 		cbp->bio_to = cp->provider;
406ec704301SPawel Jakub Dawidek 		if (cbp->bio_caller1 != NULL) {
4074c55f05bSPawel Jakub Dawidek 			cbp->bio_data = data;
4084c55f05bSPawel Jakub Dawidek 			if (bp->bio_cmd == BIO_WRITE) {
409ec704301SPawel Jakub Dawidek 				g_stripe_copy(sc, cbp->bio_caller1, data,
4104c55f05bSPawel Jakub Dawidek 				    cbp->bio_offset, cbp->bio_length, 0);
4114c55f05bSPawel Jakub Dawidek 			}
4124c55f05bSPawel Jakub Dawidek 			data += cbp->bio_length;
4134c55f05bSPawel Jakub Dawidek 		}
4144c55f05bSPawel Jakub Dawidek 		G_STRIPE_LOGREQ(cbp, "Sending request.");
4154c55f05bSPawel Jakub Dawidek 		g_io_request(cbp, cp);
4164c55f05bSPawel Jakub Dawidek 	}
4174c55f05bSPawel Jakub Dawidek 	return (0);
4184c55f05bSPawel Jakub Dawidek failure:
4194c55f05bSPawel Jakub Dawidek 	if (data != NULL)
4204c55f05bSPawel Jakub Dawidek 		uma_zfree(g_stripe_zone, data);
4214c55f05bSPawel Jakub Dawidek 	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
4224c55f05bSPawel Jakub Dawidek 		TAILQ_REMOVE(&queue, cbp, bio_queue);
423ec704301SPawel Jakub Dawidek 		if (cbp->bio_caller1 != NULL) {
424ec704301SPawel Jakub Dawidek 			cbp->bio_data = cbp->bio_caller1;
425ec704301SPawel Jakub Dawidek 			cbp->bio_caller1 = NULL;
4264c55f05bSPawel Jakub Dawidek 		}
42737abacd4SPawel Jakub Dawidek 		bp->bio_children--;
4284c55f05bSPawel Jakub Dawidek 		g_destroy_bio(cbp);
4294c55f05bSPawel Jakub Dawidek 	}
4304c55f05bSPawel Jakub Dawidek 	return (error);
4314c55f05bSPawel Jakub Dawidek }
4324c55f05bSPawel Jakub Dawidek 
4334c55f05bSPawel Jakub Dawidek static int
4344c55f05bSPawel Jakub Dawidek g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
4354c55f05bSPawel Jakub Dawidek {
4364c55f05bSPawel Jakub Dawidek 	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
4374c55f05bSPawel Jakub Dawidek 	struct g_stripe_softc *sc;
4384c55f05bSPawel Jakub Dawidek 	uint32_t stripesize;
4394c55f05bSPawel Jakub Dawidek 	struct bio *cbp;
4404c55f05bSPawel Jakub Dawidek 	char *addr;
4414c55f05bSPawel Jakub Dawidek 	int error;
4424c55f05bSPawel Jakub Dawidek 
4434c55f05bSPawel Jakub Dawidek 	sc = bp->bio_to->geom->softc;
4444c55f05bSPawel Jakub Dawidek 
4454c55f05bSPawel Jakub Dawidek 	stripesize = sc->sc_stripesize;
4464c55f05bSPawel Jakub Dawidek 
4474c55f05bSPawel Jakub Dawidek 	cbp = g_clone_bio(bp);
4484c55f05bSPawel Jakub Dawidek 	if (cbp == NULL) {
4494c55f05bSPawel Jakub Dawidek 		error = ENOMEM;
4504c55f05bSPawel Jakub Dawidek 		goto failure;
4514c55f05bSPawel Jakub Dawidek 	}
4524c55f05bSPawel Jakub Dawidek 	TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
4534c55f05bSPawel Jakub Dawidek 	/*
4544c55f05bSPawel Jakub Dawidek 	 * Fill in the component buf structure.
4554c55f05bSPawel Jakub Dawidek 	 */
45640ea77a0SAlexander Motin 	if (bp->bio_length == length)
45740ea77a0SAlexander Motin 		cbp->bio_done = g_std_done;	/* Optimized lockless case. */
45840ea77a0SAlexander Motin 	else
45940ea77a0SAlexander Motin 		cbp->bio_done = g_stripe_done;
4604c55f05bSPawel Jakub Dawidek 	cbp->bio_offset = offset;
4614c55f05bSPawel Jakub Dawidek 	cbp->bio_length = length;
46240ea77a0SAlexander Motin 	if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
46340ea77a0SAlexander Motin 		bp->bio_ma_n = round_page(bp->bio_ma_offset +
46440ea77a0SAlexander Motin 		    bp->bio_length) / PAGE_SIZE;
46540ea77a0SAlexander Motin 		addr = NULL;
46640ea77a0SAlexander Motin 	} else
46740ea77a0SAlexander Motin 		addr = bp->bio_data;
468ec704301SPawel Jakub Dawidek 	cbp->bio_caller2 = sc->sc_disks[no];
4694c55f05bSPawel Jakub Dawidek 
4704c55f05bSPawel Jakub Dawidek 	/* offset -= offset % stripesize; */
4714c55f05bSPawel Jakub Dawidek 	offset -= offset & (stripesize - 1);
47266b92c07SAlexander Motin 	if (bp->bio_cmd != BIO_DELETE)
4734c55f05bSPawel Jakub Dawidek 		addr += length;
4744c55f05bSPawel Jakub Dawidek 	length = bp->bio_length - length;
47566b92c07SAlexander Motin 	for (no++; length > 0; no++, length -= stripesize) {
4764c55f05bSPawel Jakub Dawidek 		if (no > sc->sc_ndisks - 1) {
4774c55f05bSPawel Jakub Dawidek 			no = 0;
4784c55f05bSPawel Jakub Dawidek 			offset += stripesize;
4794c55f05bSPawel Jakub Dawidek 		}
4804c55f05bSPawel Jakub Dawidek 		cbp = g_clone_bio(bp);
4814c55f05bSPawel Jakub Dawidek 		if (cbp == NULL) {
4824c55f05bSPawel Jakub Dawidek 			error = ENOMEM;
4834c55f05bSPawel Jakub Dawidek 			goto failure;
4844c55f05bSPawel Jakub Dawidek 		}
4854c55f05bSPawel Jakub Dawidek 		TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
4864c55f05bSPawel Jakub Dawidek 
4874c55f05bSPawel Jakub Dawidek 		/*
4884c55f05bSPawel Jakub Dawidek 		 * Fill in the component buf structure.
4894c55f05bSPawel Jakub Dawidek 		 */
49040ea77a0SAlexander Motin 		cbp->bio_done = g_stripe_done;
4914c55f05bSPawel Jakub Dawidek 		cbp->bio_offset = offset;
4924c55f05bSPawel Jakub Dawidek 		/*
4934c55f05bSPawel Jakub Dawidek 		 * MIN() is in case when
4944c55f05bSPawel Jakub Dawidek 		 * (bp->bio_length % sc->sc_stripesize) != 0.
4954c55f05bSPawel Jakub Dawidek 		 */
4964c55f05bSPawel Jakub Dawidek 		cbp->bio_length = MIN(stripesize, length);
49740ea77a0SAlexander Motin 		if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
49840ea77a0SAlexander Motin 			cbp->bio_ma_offset += (uintptr_t)addr;
49940ea77a0SAlexander Motin 			cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
50040ea77a0SAlexander Motin 			cbp->bio_ma_offset %= PAGE_SIZE;
50140ea77a0SAlexander Motin 			cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
50240ea77a0SAlexander Motin 			    cbp->bio_length) / PAGE_SIZE;
50340ea77a0SAlexander Motin 		} else
50440ea77a0SAlexander Motin 			cbp->bio_data = addr;
5054c55f05bSPawel Jakub Dawidek 
506ec704301SPawel Jakub Dawidek 		cbp->bio_caller2 = sc->sc_disks[no];
50766b92c07SAlexander Motin 
50866b92c07SAlexander Motin 		if (bp->bio_cmd != BIO_DELETE)
50966b92c07SAlexander Motin 			addr += stripesize;
5104c55f05bSPawel Jakub Dawidek 	}
5114c55f05bSPawel Jakub Dawidek 	/*
5124c55f05bSPawel Jakub Dawidek 	 * Fire off all allocated requests!
5134c55f05bSPawel Jakub Dawidek 	 */
5144c55f05bSPawel Jakub Dawidek 	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
5154c55f05bSPawel Jakub Dawidek 		struct g_consumer *cp;
5164c55f05bSPawel Jakub Dawidek 
5174c55f05bSPawel Jakub Dawidek 		TAILQ_REMOVE(&queue, cbp, bio_queue);
518ec704301SPawel Jakub Dawidek 		cp = cbp->bio_caller2;
519ec704301SPawel Jakub Dawidek 		cbp->bio_caller2 = NULL;
5204c55f05bSPawel Jakub Dawidek 		cbp->bio_to = cp->provider;
5214c55f05bSPawel Jakub Dawidek 		G_STRIPE_LOGREQ(cbp, "Sending request.");
5224c55f05bSPawel Jakub Dawidek 		g_io_request(cbp, cp);
5234c55f05bSPawel Jakub Dawidek 	}
5244c55f05bSPawel Jakub Dawidek 	return (0);
5254c55f05bSPawel Jakub Dawidek failure:
5264c55f05bSPawel Jakub Dawidek 	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
5274c55f05bSPawel Jakub Dawidek 		TAILQ_REMOVE(&queue, cbp, bio_queue);
52837abacd4SPawel Jakub Dawidek 		bp->bio_children--;
5294c55f05bSPawel Jakub Dawidek 		g_destroy_bio(cbp);
5304c55f05bSPawel Jakub Dawidek 	}
5314c55f05bSPawel Jakub Dawidek 	return (error);
5324c55f05bSPawel Jakub Dawidek }
5334c55f05bSPawel Jakub Dawidek 
5344c55f05bSPawel Jakub Dawidek static void
53542461fbaSPawel Jakub Dawidek g_stripe_flush(struct g_stripe_softc *sc, struct bio *bp)
53642461fbaSPawel Jakub Dawidek {
53742461fbaSPawel Jakub Dawidek 	struct bio_queue_head queue;
53842461fbaSPawel Jakub Dawidek 	struct g_consumer *cp;
53942461fbaSPawel Jakub Dawidek 	struct bio *cbp;
54042461fbaSPawel Jakub Dawidek 	u_int no;
54142461fbaSPawel Jakub Dawidek 
54242461fbaSPawel Jakub Dawidek 	bioq_init(&queue);
54342461fbaSPawel Jakub Dawidek 	for (no = 0; no < sc->sc_ndisks; no++) {
54442461fbaSPawel Jakub Dawidek 		cbp = g_clone_bio(bp);
54542461fbaSPawel Jakub Dawidek 		if (cbp == NULL) {
54642461fbaSPawel Jakub Dawidek 			for (cbp = bioq_first(&queue); cbp != NULL;
54742461fbaSPawel Jakub Dawidek 			    cbp = bioq_first(&queue)) {
54842461fbaSPawel Jakub Dawidek 				bioq_remove(&queue, cbp);
54942461fbaSPawel Jakub Dawidek 				g_destroy_bio(cbp);
55042461fbaSPawel Jakub Dawidek 			}
55142461fbaSPawel Jakub Dawidek 			if (bp->bio_error == 0)
55242461fbaSPawel Jakub Dawidek 				bp->bio_error = ENOMEM;
55342461fbaSPawel Jakub Dawidek 			g_io_deliver(bp, bp->bio_error);
55442461fbaSPawel Jakub Dawidek 			return;
55542461fbaSPawel Jakub Dawidek 		}
55642461fbaSPawel Jakub Dawidek 		bioq_insert_tail(&queue, cbp);
55740ea77a0SAlexander Motin 		cbp->bio_done = g_stripe_done;
55840ea77a0SAlexander Motin 		cbp->bio_caller2 = sc->sc_disks[no];
55942461fbaSPawel Jakub Dawidek 		cbp->bio_to = sc->sc_disks[no]->provider;
56042461fbaSPawel Jakub Dawidek 	}
56142461fbaSPawel Jakub Dawidek 	for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
56242461fbaSPawel Jakub Dawidek 		bioq_remove(&queue, cbp);
56342461fbaSPawel Jakub Dawidek 		G_STRIPE_LOGREQ(cbp, "Sending request.");
56440ea77a0SAlexander Motin 		cp = cbp->bio_caller2;
56540ea77a0SAlexander Motin 		cbp->bio_caller2 = NULL;
56642461fbaSPawel Jakub Dawidek 		g_io_request(cbp, cp);
56742461fbaSPawel Jakub Dawidek 	}
56842461fbaSPawel Jakub Dawidek }
56942461fbaSPawel Jakub Dawidek 
57042461fbaSPawel Jakub Dawidek static void
571b09121f9SPawel Jakub Dawidek g_stripe_start(struct bio *bp)
572b09121f9SPawel Jakub Dawidek {
5734c55f05bSPawel Jakub Dawidek 	off_t offset, start, length, nstripe;
574b09121f9SPawel Jakub Dawidek 	struct g_stripe_softc *sc;
5754c55f05bSPawel Jakub Dawidek 	u_int no, stripesize;
5764c55f05bSPawel Jakub Dawidek 	int error, fast = 0;
577b09121f9SPawel Jakub Dawidek 
5784c55f05bSPawel Jakub Dawidek 	sc = bp->bio_to->geom->softc;
579b09121f9SPawel Jakub Dawidek 	/*
580b09121f9SPawel Jakub Dawidek 	 * If sc == NULL, provider's error should be set and g_stripe_start()
581b09121f9SPawel Jakub Dawidek 	 * should not be called at all.
582b09121f9SPawel Jakub Dawidek 	 */
583b09121f9SPawel Jakub Dawidek 	KASSERT(sc != NULL,
584b09121f9SPawel Jakub Dawidek 	    ("Provider's error should be set (error=%d)(device=%s).",
585b09121f9SPawel Jakub Dawidek 	    bp->bio_to->error, bp->bio_to->name));
586b09121f9SPawel Jakub Dawidek 
587b09121f9SPawel Jakub Dawidek 	G_STRIPE_LOGREQ(bp, "Request received.");
588b09121f9SPawel Jakub Dawidek 
589b09121f9SPawel Jakub Dawidek 	switch (bp->bio_cmd) {
590b09121f9SPawel Jakub Dawidek 	case BIO_READ:
591b09121f9SPawel Jakub Dawidek 	case BIO_WRITE:
592b09121f9SPawel Jakub Dawidek 	case BIO_DELETE:
593b09121f9SPawel Jakub Dawidek 		break;
59442461fbaSPawel Jakub Dawidek 	case BIO_FLUSH:
59542461fbaSPawel Jakub Dawidek 		g_stripe_flush(sc, bp);
59642461fbaSPawel Jakub Dawidek 		return;
597b09121f9SPawel Jakub Dawidek 	case BIO_GETATTR:
598b09121f9SPawel Jakub Dawidek 		/* To which provider it should be delivered? */
599b09121f9SPawel Jakub Dawidek 	default:
600b09121f9SPawel Jakub Dawidek 		g_io_deliver(bp, EOPNOTSUPP);
601b09121f9SPawel Jakub Dawidek 		return;
602b09121f9SPawel Jakub Dawidek 	}
603b09121f9SPawel Jakub Dawidek 
604b09121f9SPawel Jakub Dawidek 	stripesize = sc->sc_stripesize;
605b09121f9SPawel Jakub Dawidek 
606b09121f9SPawel Jakub Dawidek 	/*
6074c55f05bSPawel Jakub Dawidek 	 * Calculations are quite messy, but fast I hope.
608b09121f9SPawel Jakub Dawidek 	 */
609b09121f9SPawel Jakub Dawidek 
610b09121f9SPawel Jakub Dawidek 	/* Stripe number. */
611b09121f9SPawel Jakub Dawidek 	/* nstripe = bp->bio_offset / stripesize; */
612b09121f9SPawel Jakub Dawidek 	nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
613b09121f9SPawel Jakub Dawidek 	/* Disk number. */
614b09121f9SPawel Jakub Dawidek 	no = nstripe % sc->sc_ndisks;
615b09121f9SPawel Jakub Dawidek 	/* Start position in stripe. */
616b09121f9SPawel Jakub Dawidek 	/* start = bp->bio_offset % stripesize; */
617b09121f9SPawel Jakub Dawidek 	start = bp->bio_offset & (stripesize - 1);
618b09121f9SPawel Jakub Dawidek 	/* Start position in disk. */
6194c55f05bSPawel Jakub Dawidek 	/* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
6204c55f05bSPawel Jakub Dawidek 	offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
621b09121f9SPawel Jakub Dawidek 	/* Length of data to operate. */
622b09121f9SPawel Jakub Dawidek 	length = MIN(bp->bio_length, stripesize - start);
623b09121f9SPawel Jakub Dawidek 
624b09121f9SPawel Jakub Dawidek 	/*
6254c55f05bSPawel Jakub Dawidek 	 * Do use "fast" mode when:
6264c55f05bSPawel Jakub Dawidek 	 * 1. "Fast" mode is ON.
6274c55f05bSPawel Jakub Dawidek 	 * and
628af582ea7SAlexander Motin 	 * 2. Request size is less than or equal to MAXPHYS,
6294c55f05bSPawel Jakub Dawidek 	 *    which should always be true.
6304c55f05bSPawel Jakub Dawidek 	 * and
6314c55f05bSPawel Jakub Dawidek 	 * 3. Request size is bigger than stripesize * ndisks. If it isn't,
6324c55f05bSPawel Jakub Dawidek 	 *    there will be no need to send more than one I/O request to
6334c55f05bSPawel Jakub Dawidek 	 *    a provider, so there is nothing to optmize.
63440ea77a0SAlexander Motin 	 * and
63540ea77a0SAlexander Motin 	 * 4. Request is not unmapped.
63666b92c07SAlexander Motin 	 * and
63766b92c07SAlexander Motin 	 * 5. It is not a BIO_DELETE.
638b09121f9SPawel Jakub Dawidek 	 */
639af582ea7SAlexander Motin 	if (g_stripe_fast && bp->bio_length <= MAXPHYS &&
64040ea77a0SAlexander Motin 	    bp->bio_length >= stripesize * sc->sc_ndisks &&
64166b92c07SAlexander Motin 	    (bp->bio_flags & BIO_UNMAPPED) == 0 &&
64266b92c07SAlexander Motin 	    bp->bio_cmd != BIO_DELETE) {
6434c55f05bSPawel Jakub Dawidek 		fast = 1;
6444c55f05bSPawel Jakub Dawidek 	}
6454c55f05bSPawel Jakub Dawidek 	error = 0;
646cea36368SPawel Jakub Dawidek 	if (fast) {
6474c55f05bSPawel Jakub Dawidek 		error = g_stripe_start_fast(bp, no, offset, length);
648cea36368SPawel Jakub Dawidek 		if (error != 0)
649cea36368SPawel Jakub Dawidek 			g_stripe_fast_failed++;
650cea36368SPawel Jakub Dawidek 	}
6514c55f05bSPawel Jakub Dawidek 	/*
6524c55f05bSPawel Jakub Dawidek 	 * Do use "economic" when:
6534c55f05bSPawel Jakub Dawidek 	 * 1. "Economic" mode is ON.
6544c55f05bSPawel Jakub Dawidek 	 * or
655f24bf752SPawel Jakub Dawidek 	 * 2. "Fast" mode failed. It can only fail if there is no memory.
6564c55f05bSPawel Jakub Dawidek 	 */
6574c55f05bSPawel Jakub Dawidek 	if (!fast || error != 0)
6584c55f05bSPawel Jakub Dawidek 		error = g_stripe_start_economic(bp, no, offset, length);
6594c55f05bSPawel Jakub Dawidek 	if (error != 0) {
660b09121f9SPawel Jakub Dawidek 		if (bp->bio_error == 0)
6614c55f05bSPawel Jakub Dawidek 			bp->bio_error = error;
662b09121f9SPawel Jakub Dawidek 		g_io_deliver(bp, bp->bio_error);
663b09121f9SPawel Jakub Dawidek 	}
664b09121f9SPawel Jakub Dawidek }
665b09121f9SPawel Jakub Dawidek 
666b09121f9SPawel Jakub Dawidek static void
667b09121f9SPawel Jakub Dawidek g_stripe_check_and_run(struct g_stripe_softc *sc)
668b09121f9SPawel Jakub Dawidek {
66940ea77a0SAlexander Motin 	struct g_provider *dp;
670b09121f9SPawel Jakub Dawidek 	off_t mediasize, ms;
671b09121f9SPawel Jakub Dawidek 	u_int no, sectorsize = 0;
672b09121f9SPawel Jakub Dawidek 
6730849a53fSAlexander Motin 	g_topology_assert();
674b09121f9SPawel Jakub Dawidek 	if (g_stripe_nvalid(sc) != sc->sc_ndisks)
675b09121f9SPawel Jakub Dawidek 		return;
676b09121f9SPawel Jakub Dawidek 
677889c5dc2SPawel Jakub Dawidek 	sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
678889c5dc2SPawel Jakub Dawidek 	    sc->sc_name);
67940ea77a0SAlexander Motin 	sc->sc_provider->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
68040ea77a0SAlexander Motin 	if (g_stripe_fast == 0)
68140ea77a0SAlexander Motin 		sc->sc_provider->flags |= G_PF_ACCEPT_UNMAPPED;
682b09121f9SPawel Jakub Dawidek 	/*
683b09121f9SPawel Jakub Dawidek 	 * Find the smallest disk.
684b09121f9SPawel Jakub Dawidek 	 */
685b09121f9SPawel Jakub Dawidek 	mediasize = sc->sc_disks[0]->provider->mediasize;
686b09121f9SPawel Jakub Dawidek 	if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
687b09121f9SPawel Jakub Dawidek 		mediasize -= sc->sc_disks[0]->provider->sectorsize;
688b09121f9SPawel Jakub Dawidek 	mediasize -= mediasize % sc->sc_stripesize;
689b09121f9SPawel Jakub Dawidek 	sectorsize = sc->sc_disks[0]->provider->sectorsize;
690b09121f9SPawel Jakub Dawidek 	for (no = 1; no < sc->sc_ndisks; no++) {
69140ea77a0SAlexander Motin 		dp = sc->sc_disks[no]->provider;
69240ea77a0SAlexander Motin 		ms = dp->mediasize;
693b09121f9SPawel Jakub Dawidek 		if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
69440ea77a0SAlexander Motin 			ms -= dp->sectorsize;
695b09121f9SPawel Jakub Dawidek 		ms -= ms % sc->sc_stripesize;
696b09121f9SPawel Jakub Dawidek 		if (ms < mediasize)
697b09121f9SPawel Jakub Dawidek 			mediasize = ms;
69840ea77a0SAlexander Motin 		sectorsize = lcm(sectorsize, dp->sectorsize);
69940ea77a0SAlexander Motin 
70040ea77a0SAlexander Motin 		/* A provider underneath us doesn't support unmapped */
70140ea77a0SAlexander Motin 		if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
70240ea77a0SAlexander Motin 			G_STRIPE_DEBUG(1, "Cancelling unmapped "
70340ea77a0SAlexander Motin 			    "because of %s.", dp->name);
70440ea77a0SAlexander Motin 			sc->sc_provider->flags &= ~G_PF_ACCEPT_UNMAPPED;
70540ea77a0SAlexander Motin 		}
706b09121f9SPawel Jakub Dawidek 	}
707b09121f9SPawel Jakub Dawidek 	sc->sc_provider->sectorsize = sectorsize;
708b09121f9SPawel Jakub Dawidek 	sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
709f00919d2SAlexander Motin 	sc->sc_provider->stripesize = sc->sc_stripesize;
710f00919d2SAlexander Motin 	sc->sc_provider->stripeoffset = 0;
711b09121f9SPawel Jakub Dawidek 	g_error_provider(sc->sc_provider, 0);
712b09121f9SPawel Jakub Dawidek 
7130849a53fSAlexander Motin 	G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
714b09121f9SPawel Jakub Dawidek }
715b09121f9SPawel Jakub Dawidek 
716b09121f9SPawel Jakub Dawidek static int
717b09121f9SPawel Jakub Dawidek g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
718b09121f9SPawel Jakub Dawidek {
719b09121f9SPawel Jakub Dawidek 	struct g_provider *pp;
720b09121f9SPawel Jakub Dawidek 	u_char *buf;
721b09121f9SPawel Jakub Dawidek 	int error;
722b09121f9SPawel Jakub Dawidek 
723b09121f9SPawel Jakub Dawidek 	g_topology_assert();
724b09121f9SPawel Jakub Dawidek 
725b09121f9SPawel Jakub Dawidek 	error = g_access(cp, 1, 0, 0);
726b09121f9SPawel Jakub Dawidek 	if (error != 0)
727b09121f9SPawel Jakub Dawidek 		return (error);
728b09121f9SPawel Jakub Dawidek 	pp = cp->provider;
729b09121f9SPawel Jakub Dawidek 	g_topology_unlock();
730b09121f9SPawel Jakub Dawidek 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
731b09121f9SPawel Jakub Dawidek 	    &error);
732b09121f9SPawel Jakub Dawidek 	g_topology_lock();
733b09121f9SPawel Jakub Dawidek 	g_access(cp, -1, 0, 0);
734b09121f9SPawel Jakub Dawidek 	if (buf == NULL)
735b09121f9SPawel Jakub Dawidek 		return (error);
736b09121f9SPawel Jakub Dawidek 
737b09121f9SPawel Jakub Dawidek 	/* Decode metadata. */
738b09121f9SPawel Jakub Dawidek 	stripe_metadata_decode(buf, md);
739b09121f9SPawel Jakub Dawidek 	g_free(buf);
740b09121f9SPawel Jakub Dawidek 
741b09121f9SPawel Jakub Dawidek 	return (0);
742b09121f9SPawel Jakub Dawidek }
743b09121f9SPawel Jakub Dawidek 
744b09121f9SPawel Jakub Dawidek /*
745b09121f9SPawel Jakub Dawidek  * Add disk to given device.
746b09121f9SPawel Jakub Dawidek  */
747b09121f9SPawel Jakub Dawidek static int
748b09121f9SPawel Jakub Dawidek g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
749b09121f9SPawel Jakub Dawidek {
750b09121f9SPawel Jakub Dawidek 	struct g_consumer *cp, *fcp;
751b09121f9SPawel Jakub Dawidek 	struct g_geom *gp;
752b09121f9SPawel Jakub Dawidek 	int error;
753b09121f9SPawel Jakub Dawidek 
7540849a53fSAlexander Motin 	g_topology_assert();
755b09121f9SPawel Jakub Dawidek 	/* Metadata corrupted? */
756b09121f9SPawel Jakub Dawidek 	if (no >= sc->sc_ndisks)
757b09121f9SPawel Jakub Dawidek 		return (EINVAL);
758b09121f9SPawel Jakub Dawidek 
759b09121f9SPawel Jakub Dawidek 	/* Check if disk is not already attached. */
760b09121f9SPawel Jakub Dawidek 	if (sc->sc_disks[no] != NULL)
761b09121f9SPawel Jakub Dawidek 		return (EEXIST);
762b09121f9SPawel Jakub Dawidek 
763b09121f9SPawel Jakub Dawidek 	gp = sc->sc_geom;
764b09121f9SPawel Jakub Dawidek 	fcp = LIST_FIRST(&gp->consumer);
765b09121f9SPawel Jakub Dawidek 
766b09121f9SPawel Jakub Dawidek 	cp = g_new_consumer(gp);
76740ea77a0SAlexander Motin 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
7680849a53fSAlexander Motin 	cp->private = NULL;
7690849a53fSAlexander Motin 	cp->index = no;
770b09121f9SPawel Jakub Dawidek 	error = g_attach(cp, pp);
771b09121f9SPawel Jakub Dawidek 	if (error != 0) {
772b09121f9SPawel Jakub Dawidek 		g_destroy_consumer(cp);
773b09121f9SPawel Jakub Dawidek 		return (error);
774b09121f9SPawel Jakub Dawidek 	}
775b09121f9SPawel Jakub Dawidek 
776b09121f9SPawel Jakub Dawidek 	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
777b09121f9SPawel Jakub Dawidek 		error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
778b09121f9SPawel Jakub Dawidek 		if (error != 0) {
779b09121f9SPawel Jakub Dawidek 			g_detach(cp);
780b09121f9SPawel Jakub Dawidek 			g_destroy_consumer(cp);
781b09121f9SPawel Jakub Dawidek 			return (error);
782b09121f9SPawel Jakub Dawidek 		}
783b09121f9SPawel Jakub Dawidek 	}
784b09121f9SPawel Jakub Dawidek 	if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
785b09121f9SPawel Jakub Dawidek 		struct g_stripe_metadata md;
786b09121f9SPawel Jakub Dawidek 
787b09121f9SPawel Jakub Dawidek 		/* Reread metadata. */
788b09121f9SPawel Jakub Dawidek 		error = g_stripe_read_metadata(cp, &md);
789b09121f9SPawel Jakub Dawidek 		if (error != 0)
790b09121f9SPawel Jakub Dawidek 			goto fail;
791b09121f9SPawel Jakub Dawidek 
792b09121f9SPawel Jakub Dawidek 		if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
793b09121f9SPawel Jakub Dawidek 		    strcmp(md.md_name, sc->sc_name) != 0 ||
794b09121f9SPawel Jakub Dawidek 		    md.md_id != sc->sc_id) {
795b09121f9SPawel Jakub Dawidek 			G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
796b09121f9SPawel Jakub Dawidek 			goto fail;
797b09121f9SPawel Jakub Dawidek 		}
798b09121f9SPawel Jakub Dawidek 	}
799b09121f9SPawel Jakub Dawidek 
800b09121f9SPawel Jakub Dawidek 	sc->sc_disks[no] = cp;
801889c5dc2SPawel Jakub Dawidek 	G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
802b09121f9SPawel Jakub Dawidek 	g_stripe_check_and_run(sc);
803b09121f9SPawel Jakub Dawidek 
804b09121f9SPawel Jakub Dawidek 	return (0);
805b09121f9SPawel Jakub Dawidek fail:
806b09121f9SPawel Jakub Dawidek 	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
807b09121f9SPawel Jakub Dawidek 		g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
808b09121f9SPawel Jakub Dawidek 	g_detach(cp);
809b09121f9SPawel Jakub Dawidek 	g_destroy_consumer(cp);
810b09121f9SPawel Jakub Dawidek 	return (error);
811b09121f9SPawel Jakub Dawidek }
812b09121f9SPawel Jakub Dawidek 
813b09121f9SPawel Jakub Dawidek static struct g_geom *
814b09121f9SPawel Jakub Dawidek g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
815b09121f9SPawel Jakub Dawidek     u_int type)
816b09121f9SPawel Jakub Dawidek {
817b09121f9SPawel Jakub Dawidek 	struct g_stripe_softc *sc;
818b09121f9SPawel Jakub Dawidek 	struct g_geom *gp;
819b09121f9SPawel Jakub Dawidek 	u_int no;
820b09121f9SPawel Jakub Dawidek 
8210849a53fSAlexander Motin 	g_topology_assert();
822889c5dc2SPawel Jakub Dawidek 	G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
823b09121f9SPawel Jakub Dawidek 	    md->md_id);
824b09121f9SPawel Jakub Dawidek 
825b09121f9SPawel Jakub Dawidek 	/* Two disks is minimum. */
826889c5dc2SPawel Jakub Dawidek 	if (md->md_all < 2) {
827889c5dc2SPawel Jakub Dawidek 		G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
828b09121f9SPawel Jakub Dawidek 		return (NULL);
829b09121f9SPawel Jakub Dawidek 	}
830b09121f9SPawel Jakub Dawidek #if 0
831b09121f9SPawel Jakub Dawidek 	/* Stripe size have to be grater than or equal to sector size. */
832b09121f9SPawel Jakub Dawidek 	if (md->md_stripesize < sectorsize) {
833889c5dc2SPawel Jakub Dawidek 		G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
834b09121f9SPawel Jakub Dawidek 		return (NULL);
835b09121f9SPawel Jakub Dawidek 	}
836b09121f9SPawel Jakub Dawidek #endif
837b09121f9SPawel Jakub Dawidek 	/* Stripe size have to be power of 2. */
838b09121f9SPawel Jakub Dawidek 	if (!powerof2(md->md_stripesize)) {
839889c5dc2SPawel Jakub Dawidek 		G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
840b09121f9SPawel Jakub Dawidek 		return (NULL);
841b09121f9SPawel Jakub Dawidek 	}
842b09121f9SPawel Jakub Dawidek 
843b09121f9SPawel Jakub Dawidek 	/* Check for duplicate unit */
844b09121f9SPawel Jakub Dawidek 	LIST_FOREACH(gp, &mp->geom, geom) {
845b09121f9SPawel Jakub Dawidek 		sc = gp->softc;
846b09121f9SPawel Jakub Dawidek 		if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
847b09121f9SPawel Jakub Dawidek 			G_STRIPE_DEBUG(0, "Device %s already configured.",
848889c5dc2SPawel Jakub Dawidek 			    sc->sc_name);
849b09121f9SPawel Jakub Dawidek 			return (NULL);
850b09121f9SPawel Jakub Dawidek 		}
851b09121f9SPawel Jakub Dawidek 	}
852889c5dc2SPawel Jakub Dawidek 	gp = g_new_geomf(mp, "%s", md->md_name);
8532017a9d3SPawel Jakub Dawidek 	sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
854b09121f9SPawel Jakub Dawidek 	gp->start = g_stripe_start;
855b09121f9SPawel Jakub Dawidek 	gp->spoiled = g_stripe_orphan;
856b09121f9SPawel Jakub Dawidek 	gp->orphan = g_stripe_orphan;
857b09121f9SPawel Jakub Dawidek 	gp->access = g_stripe_access;
858b09121f9SPawel Jakub Dawidek 	gp->dumpconf = g_stripe_dumpconf;
859b09121f9SPawel Jakub Dawidek 
860b09121f9SPawel Jakub Dawidek 	sc->sc_id = md->md_id;
861b09121f9SPawel Jakub Dawidek 	sc->sc_stripesize = md->md_stripesize;
862a95452eeSPawel Jakub Dawidek 	sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
863b09121f9SPawel Jakub Dawidek 	sc->sc_ndisks = md->md_all;
864b09121f9SPawel Jakub Dawidek 	sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
865b09121f9SPawel Jakub Dawidek 	    M_STRIPE, M_WAITOK | M_ZERO);
866b09121f9SPawel Jakub Dawidek 	for (no = 0; no < sc->sc_ndisks; no++)
867b09121f9SPawel Jakub Dawidek 		sc->sc_disks[no] = NULL;
868b09121f9SPawel Jakub Dawidek 	sc->sc_type = type;
86940ea77a0SAlexander Motin 	mtx_init(&sc->sc_lock, "gstripe lock", NULL, MTX_DEF);
870b09121f9SPawel Jakub Dawidek 
871b09121f9SPawel Jakub Dawidek 	gp->softc = sc;
872b09121f9SPawel Jakub Dawidek 	sc->sc_geom = gp;
873b09121f9SPawel Jakub Dawidek 	sc->sc_provider = NULL;
874b09121f9SPawel Jakub Dawidek 
875889c5dc2SPawel Jakub Dawidek 	G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
876b09121f9SPawel Jakub Dawidek 
877b09121f9SPawel Jakub Dawidek 	return (gp);
878b09121f9SPawel Jakub Dawidek }
879b09121f9SPawel Jakub Dawidek 
880b09121f9SPawel Jakub Dawidek static int
881b09121f9SPawel Jakub Dawidek g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
882b09121f9SPawel Jakub Dawidek {
883b09121f9SPawel Jakub Dawidek 	struct g_provider *pp;
8840849a53fSAlexander Motin 	struct g_consumer *cp, *cp1;
885b09121f9SPawel Jakub Dawidek 	struct g_geom *gp;
886b09121f9SPawel Jakub Dawidek 
887b09121f9SPawel Jakub Dawidek 	g_topology_assert();
888b09121f9SPawel Jakub Dawidek 
889b09121f9SPawel Jakub Dawidek 	if (sc == NULL)
890b09121f9SPawel Jakub Dawidek 		return (ENXIO);
891b09121f9SPawel Jakub Dawidek 
892b09121f9SPawel Jakub Dawidek 	pp = sc->sc_provider;
893b09121f9SPawel Jakub Dawidek 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
894b09121f9SPawel Jakub Dawidek 		if (force) {
895b09121f9SPawel Jakub Dawidek 			G_STRIPE_DEBUG(0, "Device %s is still open, so it "
896b09121f9SPawel Jakub Dawidek 			    "can't be definitely removed.", pp->name);
897b09121f9SPawel Jakub Dawidek 		} else {
898b09121f9SPawel Jakub Dawidek 			G_STRIPE_DEBUG(1,
899b09121f9SPawel Jakub Dawidek 			    "Device %s is still open (r%dw%de%d).", pp->name,
900b09121f9SPawel Jakub Dawidek 			    pp->acr, pp->acw, pp->ace);
901b09121f9SPawel Jakub Dawidek 			return (EBUSY);
902b09121f9SPawel Jakub Dawidek 		}
903b09121f9SPawel Jakub Dawidek 	}
904b09121f9SPawel Jakub Dawidek 
905b09121f9SPawel Jakub Dawidek 	gp = sc->sc_geom;
9060849a53fSAlexander Motin 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
9070849a53fSAlexander Motin 		g_stripe_remove_disk(cp);
9080849a53fSAlexander Motin 		if (cp1 == NULL)
9090849a53fSAlexander Motin 			return (0);	/* Recursion happened. */
9100849a53fSAlexander Motin 	}
9110849a53fSAlexander Motin 	if (!LIST_EMPTY(&gp->consumer))
9120849a53fSAlexander Motin 		return (EINPROGRESS);
9130849a53fSAlexander Motin 
914b09121f9SPawel Jakub Dawidek 	gp->softc = NULL;
915b09121f9SPawel Jakub Dawidek 	KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
916b09121f9SPawel Jakub Dawidek 	    gp->name));
917b09121f9SPawel Jakub Dawidek 	free(sc->sc_disks, M_STRIPE);
91840ea77a0SAlexander Motin 	mtx_destroy(&sc->sc_lock);
919b09121f9SPawel Jakub Dawidek 	free(sc, M_STRIPE);
920b09121f9SPawel Jakub Dawidek 	G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
921b09121f9SPawel Jakub Dawidek 	g_wither_geom(gp, ENXIO);
922b09121f9SPawel Jakub Dawidek 	return (0);
923b09121f9SPawel Jakub Dawidek }
924b09121f9SPawel Jakub Dawidek 
925b09121f9SPawel Jakub Dawidek static int
926b09121f9SPawel Jakub Dawidek g_stripe_destroy_geom(struct gctl_req *req __unused,
927b09121f9SPawel Jakub Dawidek     struct g_class *mp __unused, struct g_geom *gp)
928b09121f9SPawel Jakub Dawidek {
929b09121f9SPawel Jakub Dawidek 	struct g_stripe_softc *sc;
930b09121f9SPawel Jakub Dawidek 
931b09121f9SPawel Jakub Dawidek 	sc = gp->softc;
932b09121f9SPawel Jakub Dawidek 	return (g_stripe_destroy(sc, 0));
933b09121f9SPawel Jakub Dawidek }
934b09121f9SPawel Jakub Dawidek 
935b09121f9SPawel Jakub Dawidek static struct g_geom *
936b09121f9SPawel Jakub Dawidek g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
937b09121f9SPawel Jakub Dawidek {
938b09121f9SPawel Jakub Dawidek 	struct g_stripe_metadata md;
939b09121f9SPawel Jakub Dawidek 	struct g_stripe_softc *sc;
940b09121f9SPawel Jakub Dawidek 	struct g_consumer *cp;
941b09121f9SPawel Jakub Dawidek 	struct g_geom *gp;
942b09121f9SPawel Jakub Dawidek 	int error;
943b09121f9SPawel Jakub Dawidek 
944b09121f9SPawel Jakub Dawidek 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
945b09121f9SPawel Jakub Dawidek 	g_topology_assert();
946b09121f9SPawel Jakub Dawidek 
947f8727e71SPawel Jakub Dawidek 	/* Skip providers that are already open for writing. */
948f8727e71SPawel Jakub Dawidek 	if (pp->acw > 0)
949f8727e71SPawel Jakub Dawidek 		return (NULL);
950f8727e71SPawel Jakub Dawidek 
951b09121f9SPawel Jakub Dawidek 	G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
952b09121f9SPawel Jakub Dawidek 
953b09121f9SPawel Jakub Dawidek 	gp = g_new_geomf(mp, "stripe:taste");
954b09121f9SPawel Jakub Dawidek 	gp->start = g_stripe_start;
955b09121f9SPawel Jakub Dawidek 	gp->access = g_stripe_access;
956b09121f9SPawel Jakub Dawidek 	gp->orphan = g_stripe_orphan;
957b09121f9SPawel Jakub Dawidek 	cp = g_new_consumer(gp);
958b09121f9SPawel Jakub Dawidek 	g_attach(cp, pp);
959b09121f9SPawel Jakub Dawidek 	error = g_stripe_read_metadata(cp, &md);
9607e72a708SPawel Jakub Dawidek 	g_detach(cp);
9617e72a708SPawel Jakub Dawidek 	g_destroy_consumer(cp);
9627e72a708SPawel Jakub Dawidek 	g_destroy_geom(gp);
963b09121f9SPawel Jakub Dawidek 	if (error != 0)
964b09121f9SPawel Jakub Dawidek 		return (NULL);
965b09121f9SPawel Jakub Dawidek 	gp = NULL;
966b09121f9SPawel Jakub Dawidek 
967b09121f9SPawel Jakub Dawidek 	if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
968b09121f9SPawel Jakub Dawidek 		return (NULL);
969b09121f9SPawel Jakub Dawidek 	if (md.md_version > G_STRIPE_VERSION) {
970b09121f9SPawel Jakub Dawidek 		printf("geom_stripe.ko module is too old to handle %s.\n",
971b09121f9SPawel Jakub Dawidek 		    pp->name);
972b09121f9SPawel Jakub Dawidek 		return (NULL);
973b09121f9SPawel Jakub Dawidek 	}
9746c74f517SPawel Jakub Dawidek 	/*
9756c74f517SPawel Jakub Dawidek 	 * Backward compatibility:
9766c74f517SPawel Jakub Dawidek 	 */
977e6890985SPawel Jakub Dawidek 	/* There was no md_provider field in earlier versions of metadata. */
9786c74f517SPawel Jakub Dawidek 	if (md.md_version < 2)
9796c74f517SPawel Jakub Dawidek 		bzero(md.md_provider, sizeof(md.md_provider));
980e6890985SPawel Jakub Dawidek 	/* There was no md_provsize field in earlier versions of metadata. */
981e6890985SPawel Jakub Dawidek 	if (md.md_version < 3)
982e6890985SPawel Jakub Dawidek 		md.md_provsize = pp->mediasize;
9836c74f517SPawel Jakub Dawidek 
98490f2be24SAlexander Motin 	if (md.md_provider[0] != '\0' &&
98590f2be24SAlexander Motin 	    !g_compare_names(md.md_provider, pp->name))
9866c74f517SPawel Jakub Dawidek 		return (NULL);
987e6890985SPawel Jakub Dawidek 	if (md.md_provsize != pp->mediasize)
988e6890985SPawel Jakub Dawidek 		return (NULL);
989b09121f9SPawel Jakub Dawidek 
990b09121f9SPawel Jakub Dawidek 	/*
991b09121f9SPawel Jakub Dawidek 	 * Let's check if device already exists.
992b09121f9SPawel Jakub Dawidek 	 */
993b09121f9SPawel Jakub Dawidek 	sc = NULL;
994b09121f9SPawel Jakub Dawidek 	LIST_FOREACH(gp, &mp->geom, geom) {
995b09121f9SPawel Jakub Dawidek 		sc = gp->softc;
996b09121f9SPawel Jakub Dawidek 		if (sc == NULL)
997b09121f9SPawel Jakub Dawidek 			continue;
998b09121f9SPawel Jakub Dawidek 		if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
999b09121f9SPawel Jakub Dawidek 			continue;
1000b09121f9SPawel Jakub Dawidek 		if (strcmp(md.md_name, sc->sc_name) != 0)
1001b09121f9SPawel Jakub Dawidek 			continue;
1002b09121f9SPawel Jakub Dawidek 		if (md.md_id != sc->sc_id)
1003b09121f9SPawel Jakub Dawidek 			continue;
1004b09121f9SPawel Jakub Dawidek 		break;
1005b09121f9SPawel Jakub Dawidek 	}
1006b09121f9SPawel Jakub Dawidek 	if (gp != NULL) {
1007b09121f9SPawel Jakub Dawidek 		G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1008b09121f9SPawel Jakub Dawidek 		error = g_stripe_add_disk(sc, pp, md.md_no);
1009b09121f9SPawel Jakub Dawidek 		if (error != 0) {
1010b09121f9SPawel Jakub Dawidek 			G_STRIPE_DEBUG(0,
1011b09121f9SPawel Jakub Dawidek 			    "Cannot add disk %s to %s (error=%d).", pp->name,
1012b09121f9SPawel Jakub Dawidek 			    gp->name, error);
1013b09121f9SPawel Jakub Dawidek 			return (NULL);
1014b09121f9SPawel Jakub Dawidek 		}
1015b09121f9SPawel Jakub Dawidek 	} else {
1016b09121f9SPawel Jakub Dawidek 		gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
1017b09121f9SPawel Jakub Dawidek 		if (gp == NULL) {
1018889c5dc2SPawel Jakub Dawidek 			G_STRIPE_DEBUG(0, "Cannot create device %s.",
1019b09121f9SPawel Jakub Dawidek 			    md.md_name);
1020b09121f9SPawel Jakub Dawidek 			return (NULL);
1021b09121f9SPawel Jakub Dawidek 		}
1022b09121f9SPawel Jakub Dawidek 		sc = gp->softc;
1023b09121f9SPawel Jakub Dawidek 		G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1024b09121f9SPawel Jakub Dawidek 		error = g_stripe_add_disk(sc, pp, md.md_no);
1025b09121f9SPawel Jakub Dawidek 		if (error != 0) {
1026b09121f9SPawel Jakub Dawidek 			G_STRIPE_DEBUG(0,
1027b09121f9SPawel Jakub Dawidek 			    "Cannot add disk %s to %s (error=%d).", pp->name,
1028b09121f9SPawel Jakub Dawidek 			    gp->name, error);
1029b09121f9SPawel Jakub Dawidek 			g_stripe_destroy(sc, 1);
1030b09121f9SPawel Jakub Dawidek 			return (NULL);
1031b09121f9SPawel Jakub Dawidek 		}
1032b09121f9SPawel Jakub Dawidek 	}
1033b09121f9SPawel Jakub Dawidek 
1034b09121f9SPawel Jakub Dawidek 	return (gp);
1035b09121f9SPawel Jakub Dawidek }
1036b09121f9SPawel Jakub Dawidek 
1037b09121f9SPawel Jakub Dawidek static void
1038b09121f9SPawel Jakub Dawidek g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1039b09121f9SPawel Jakub Dawidek {
1040b09121f9SPawel Jakub Dawidek 	u_int attached, no;
1041b09121f9SPawel Jakub Dawidek 	struct g_stripe_metadata md;
1042b09121f9SPawel Jakub Dawidek 	struct g_provider *pp;
1043b09121f9SPawel Jakub Dawidek 	struct g_stripe_softc *sc;
1044b09121f9SPawel Jakub Dawidek 	struct g_geom *gp;
1045b09121f9SPawel Jakub Dawidek 	struct sbuf *sb;
1046b09121f9SPawel Jakub Dawidek 	intmax_t *stripesize;
1047b09121f9SPawel Jakub Dawidek 	const char *name;
1048b09121f9SPawel Jakub Dawidek 	char param[16];
1049b09121f9SPawel Jakub Dawidek 	int *nargs;
1050b09121f9SPawel Jakub Dawidek 
1051b09121f9SPawel Jakub Dawidek 	g_topology_assert();
1052b09121f9SPawel Jakub Dawidek 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1053b09121f9SPawel Jakub Dawidek 	if (nargs == NULL) {
1054b09121f9SPawel Jakub Dawidek 		gctl_error(req, "No '%s' argument.", "nargs");
1055b09121f9SPawel Jakub Dawidek 		return;
1056b09121f9SPawel Jakub Dawidek 	}
1057b09121f9SPawel Jakub Dawidek 	if (*nargs <= 2) {
1058b09121f9SPawel Jakub Dawidek 		gctl_error(req, "Too few arguments.");
1059b09121f9SPawel Jakub Dawidek 		return;
1060b09121f9SPawel Jakub Dawidek 	}
1061b09121f9SPawel Jakub Dawidek 
1062b09121f9SPawel Jakub Dawidek 	strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1063b09121f9SPawel Jakub Dawidek 	md.md_version = G_STRIPE_VERSION;
1064b09121f9SPawel Jakub Dawidek 	name = gctl_get_asciiparam(req, "arg0");
1065b09121f9SPawel Jakub Dawidek 	if (name == NULL) {
1066b09121f9SPawel Jakub Dawidek 		gctl_error(req, "No 'arg%u' argument.", 0);
1067b09121f9SPawel Jakub Dawidek 		return;
1068b09121f9SPawel Jakub Dawidek 	}
1069b09121f9SPawel Jakub Dawidek 	strlcpy(md.md_name, name, sizeof(md.md_name));
1070b09121f9SPawel Jakub Dawidek 	md.md_id = arc4random();
1071b09121f9SPawel Jakub Dawidek 	md.md_no = 0;
1072b09121f9SPawel Jakub Dawidek 	md.md_all = *nargs - 1;
1073b09121f9SPawel Jakub Dawidek 	stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1074b09121f9SPawel Jakub Dawidek 	if (stripesize == NULL) {
1075b09121f9SPawel Jakub Dawidek 		gctl_error(req, "No '%s' argument.", "stripesize");
1076b09121f9SPawel Jakub Dawidek 		return;
1077b09121f9SPawel Jakub Dawidek 	}
1078b09121f9SPawel Jakub Dawidek 	md.md_stripesize = *stripesize;
10796c74f517SPawel Jakub Dawidek 	bzero(md.md_provider, sizeof(md.md_provider));
1080e6890985SPawel Jakub Dawidek 	/* This field is not important here. */
1081e6890985SPawel Jakub Dawidek 	md.md_provsize = 0;
1082b09121f9SPawel Jakub Dawidek 
1083b09121f9SPawel Jakub Dawidek 	/* Check all providers are valid */
1084b09121f9SPawel Jakub Dawidek 	for (no = 1; no < *nargs; no++) {
1085b09121f9SPawel Jakub Dawidek 		snprintf(param, sizeof(param), "arg%u", no);
1086b09121f9SPawel Jakub Dawidek 		name = gctl_get_asciiparam(req, param);
1087b09121f9SPawel Jakub Dawidek 		if (name == NULL) {
1088b09121f9SPawel Jakub Dawidek 			gctl_error(req, "No 'arg%u' argument.", no);
1089b09121f9SPawel Jakub Dawidek 			return;
1090b09121f9SPawel Jakub Dawidek 		}
1091b09121f9SPawel Jakub Dawidek 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1092b09121f9SPawel Jakub Dawidek 			name += strlen("/dev/");
1093b09121f9SPawel Jakub Dawidek 		pp = g_provider_by_name(name);
1094b09121f9SPawel Jakub Dawidek 		if (pp == NULL) {
1095b09121f9SPawel Jakub Dawidek 			G_STRIPE_DEBUG(1, "Disk %s is invalid.", name);
1096b09121f9SPawel Jakub Dawidek 			gctl_error(req, "Disk %s is invalid.", name);
1097b09121f9SPawel Jakub Dawidek 			return;
1098b09121f9SPawel Jakub Dawidek 		}
1099b09121f9SPawel Jakub Dawidek 	}
1100b09121f9SPawel Jakub Dawidek 
1101b09121f9SPawel Jakub Dawidek 	gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1102b09121f9SPawel Jakub Dawidek 	if (gp == NULL) {
1103889c5dc2SPawel Jakub Dawidek 		gctl_error(req, "Can't configure %s.", md.md_name);
1104b09121f9SPawel Jakub Dawidek 		return;
1105b09121f9SPawel Jakub Dawidek 	}
1106b09121f9SPawel Jakub Dawidek 
1107b09121f9SPawel Jakub Dawidek 	sc = gp->softc;
11082616144eSDag-Erling Smørgrav 	sb = sbuf_new_auto();
1109b09121f9SPawel Jakub Dawidek 	sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1110b09121f9SPawel Jakub Dawidek 	for (attached = 0, no = 1; no < *nargs; no++) {
1111b09121f9SPawel Jakub Dawidek 		snprintf(param, sizeof(param), "arg%u", no);
1112b09121f9SPawel Jakub Dawidek 		name = gctl_get_asciiparam(req, param);
11130a3384a8SPawel Jakub Dawidek 		if (name == NULL) {
11140a3384a8SPawel Jakub Dawidek 			gctl_error(req, "No 'arg%u' argument.", no);
11150a3384a8SPawel Jakub Dawidek 			continue;
11160a3384a8SPawel Jakub Dawidek 		}
1117b09121f9SPawel Jakub Dawidek 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1118b09121f9SPawel Jakub Dawidek 			name += strlen("/dev/");
1119b09121f9SPawel Jakub Dawidek 		pp = g_provider_by_name(name);
1120b09121f9SPawel Jakub Dawidek 		KASSERT(pp != NULL, ("Provider %s disappear?!", name));
1121b09121f9SPawel Jakub Dawidek 		if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1122b09121f9SPawel Jakub Dawidek 			G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1123b09121f9SPawel Jakub Dawidek 			    no, pp->name, gp->name);
1124b09121f9SPawel Jakub Dawidek 			sbuf_printf(sb, " %s", pp->name);
1125b09121f9SPawel Jakub Dawidek 			continue;
1126b09121f9SPawel Jakub Dawidek 		}
1127b09121f9SPawel Jakub Dawidek 		attached++;
1128b09121f9SPawel Jakub Dawidek 	}
1129b09121f9SPawel Jakub Dawidek 	sbuf_finish(sb);
1130b09121f9SPawel Jakub Dawidek 	if (md.md_all != attached) {
1131b09121f9SPawel Jakub Dawidek 		g_stripe_destroy(gp->softc, 1);
1132b09121f9SPawel Jakub Dawidek 		gctl_error(req, "%s", sbuf_data(sb));
1133b09121f9SPawel Jakub Dawidek 	}
1134b09121f9SPawel Jakub Dawidek 	sbuf_delete(sb);
1135b09121f9SPawel Jakub Dawidek }
1136b09121f9SPawel Jakub Dawidek 
1137b09121f9SPawel Jakub Dawidek static struct g_stripe_softc *
1138b09121f9SPawel Jakub Dawidek g_stripe_find_device(struct g_class *mp, const char *name)
1139b09121f9SPawel Jakub Dawidek {
1140b09121f9SPawel Jakub Dawidek 	struct g_stripe_softc *sc;
1141b09121f9SPawel Jakub Dawidek 	struct g_geom *gp;
1142b09121f9SPawel Jakub Dawidek 
1143b09121f9SPawel Jakub Dawidek 	LIST_FOREACH(gp, &mp->geom, geom) {
1144b09121f9SPawel Jakub Dawidek 		sc = gp->softc;
1145b09121f9SPawel Jakub Dawidek 		if (sc == NULL)
1146b09121f9SPawel Jakub Dawidek 			continue;
1147889c5dc2SPawel Jakub Dawidek 		if (strcmp(sc->sc_name, name) == 0)
1148b09121f9SPawel Jakub Dawidek 			return (sc);
1149b09121f9SPawel Jakub Dawidek 	}
1150b09121f9SPawel Jakub Dawidek 	return (NULL);
1151b09121f9SPawel Jakub Dawidek }
1152b09121f9SPawel Jakub Dawidek 
1153b09121f9SPawel Jakub Dawidek static void
1154b09121f9SPawel Jakub Dawidek g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1155b09121f9SPawel Jakub Dawidek {
1156b09121f9SPawel Jakub Dawidek 	struct g_stripe_softc *sc;
1157b09121f9SPawel Jakub Dawidek 	int *force, *nargs, error;
1158b09121f9SPawel Jakub Dawidek 	const char *name;
1159b09121f9SPawel Jakub Dawidek 	char param[16];
1160b09121f9SPawel Jakub Dawidek 	u_int i;
1161b09121f9SPawel Jakub Dawidek 
1162b09121f9SPawel Jakub Dawidek 	g_topology_assert();
1163b09121f9SPawel Jakub Dawidek 
1164b09121f9SPawel Jakub Dawidek 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1165b09121f9SPawel Jakub Dawidek 	if (nargs == NULL) {
1166b09121f9SPawel Jakub Dawidek 		gctl_error(req, "No '%s' argument.", "nargs");
1167b09121f9SPawel Jakub Dawidek 		return;
1168b09121f9SPawel Jakub Dawidek 	}
1169b09121f9SPawel Jakub Dawidek 	if (*nargs <= 0) {
1170b09121f9SPawel Jakub Dawidek 		gctl_error(req, "Missing device(s).");
1171b09121f9SPawel Jakub Dawidek 		return;
1172b09121f9SPawel Jakub Dawidek 	}
1173b09121f9SPawel Jakub Dawidek 	force = gctl_get_paraml(req, "force", sizeof(*force));
1174b09121f9SPawel Jakub Dawidek 	if (force == NULL) {
1175b09121f9SPawel Jakub Dawidek 		gctl_error(req, "No '%s' argument.", "force");
1176b09121f9SPawel Jakub Dawidek 		return;
1177b09121f9SPawel Jakub Dawidek 	}
1178b09121f9SPawel Jakub Dawidek 
1179b09121f9SPawel Jakub Dawidek 	for (i = 0; i < (u_int)*nargs; i++) {
1180b09121f9SPawel Jakub Dawidek 		snprintf(param, sizeof(param), "arg%u", i);
1181b09121f9SPawel Jakub Dawidek 		name = gctl_get_asciiparam(req, param);
1182b09121f9SPawel Jakub Dawidek 		if (name == NULL) {
1183b09121f9SPawel Jakub Dawidek 			gctl_error(req, "No 'arg%u' argument.", i);
1184b09121f9SPawel Jakub Dawidek 			return;
1185b09121f9SPawel Jakub Dawidek 		}
1186b09121f9SPawel Jakub Dawidek 		sc = g_stripe_find_device(mp, name);
1187b09121f9SPawel Jakub Dawidek 		if (sc == NULL) {
1188b09121f9SPawel Jakub Dawidek 			gctl_error(req, "No such device: %s.", name);
1189b09121f9SPawel Jakub Dawidek 			return;
1190b09121f9SPawel Jakub Dawidek 		}
1191b09121f9SPawel Jakub Dawidek 		error = g_stripe_destroy(sc, *force);
1192b09121f9SPawel Jakub Dawidek 		if (error != 0) {
1193b09121f9SPawel Jakub Dawidek 			gctl_error(req, "Cannot destroy device %s (error=%d).",
1194889c5dc2SPawel Jakub Dawidek 			    sc->sc_name, error);
1195b09121f9SPawel Jakub Dawidek 			return;
1196b09121f9SPawel Jakub Dawidek 		}
1197b09121f9SPawel Jakub Dawidek 	}
1198b09121f9SPawel Jakub Dawidek }
1199b09121f9SPawel Jakub Dawidek 
1200b09121f9SPawel Jakub Dawidek static void
1201b09121f9SPawel Jakub Dawidek g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1202b09121f9SPawel Jakub Dawidek {
1203b09121f9SPawel Jakub Dawidek 	uint32_t *version;
1204b09121f9SPawel Jakub Dawidek 
1205b09121f9SPawel Jakub Dawidek 	g_topology_assert();
1206b09121f9SPawel Jakub Dawidek 
1207b09121f9SPawel Jakub Dawidek 	version = gctl_get_paraml(req, "version", sizeof(*version));
1208b09121f9SPawel Jakub Dawidek 	if (version == NULL) {
1209b09121f9SPawel Jakub Dawidek 		gctl_error(req, "No '%s' argument.", "version");
1210b09121f9SPawel Jakub Dawidek 		return;
1211b09121f9SPawel Jakub Dawidek 	}
1212b09121f9SPawel Jakub Dawidek 	if (*version != G_STRIPE_VERSION) {
1213b09121f9SPawel Jakub Dawidek 		gctl_error(req, "Userland and kernel parts are out of sync.");
1214b09121f9SPawel Jakub Dawidek 		return;
1215b09121f9SPawel Jakub Dawidek 	}
1216b09121f9SPawel Jakub Dawidek 
1217b09121f9SPawel Jakub Dawidek 	if (strcmp(verb, "create") == 0) {
1218b09121f9SPawel Jakub Dawidek 		g_stripe_ctl_create(req, mp);
1219b09121f9SPawel Jakub Dawidek 		return;
1220a2e31b8bSPawel Jakub Dawidek 	} else if (strcmp(verb, "destroy") == 0 ||
1221a2e31b8bSPawel Jakub Dawidek 	    strcmp(verb, "stop") == 0) {
1222b09121f9SPawel Jakub Dawidek 		g_stripe_ctl_destroy(req, mp);
1223b09121f9SPawel Jakub Dawidek 		return;
1224b09121f9SPawel Jakub Dawidek 	}
1225b09121f9SPawel Jakub Dawidek 
1226b09121f9SPawel Jakub Dawidek 	gctl_error(req, "Unknown verb.");
1227b09121f9SPawel Jakub Dawidek }
1228b09121f9SPawel Jakub Dawidek 
1229b09121f9SPawel Jakub Dawidek static void
1230b09121f9SPawel Jakub Dawidek g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1231b09121f9SPawel Jakub Dawidek     struct g_consumer *cp, struct g_provider *pp)
1232b09121f9SPawel Jakub Dawidek {
1233b09121f9SPawel Jakub Dawidek 	struct g_stripe_softc *sc;
1234b09121f9SPawel Jakub Dawidek 
1235b09121f9SPawel Jakub Dawidek 	sc = gp->softc;
12361d723f1dSPawel Jakub Dawidek 	if (sc == NULL)
1237b09121f9SPawel Jakub Dawidek 		return;
12381d723f1dSPawel Jakub Dawidek 	if (pp != NULL) {
12391d723f1dSPawel Jakub Dawidek 		/* Nothing here. */
12401d723f1dSPawel Jakub Dawidek 	} else if (cp != NULL) {
1241f0c8658dSPawel Jakub Dawidek 		sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1242f0c8658dSPawel Jakub Dawidek 		    (u_int)cp->index);
12431d723f1dSPawel Jakub Dawidek 	} else {
12441d723f1dSPawel Jakub Dawidek 		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
12451d723f1dSPawel Jakub Dawidek 		sbuf_printf(sb, "%s<Stripesize>%u</Stripesize>\n", indent,
1246b09121f9SPawel Jakub Dawidek 		    (u_int)sc->sc_stripesize);
12471d723f1dSPawel Jakub Dawidek 		sbuf_printf(sb, "%s<Type>", indent);
1248b09121f9SPawel Jakub Dawidek 		switch (sc->sc_type) {
1249b09121f9SPawel Jakub Dawidek 		case G_STRIPE_TYPE_AUTOMATIC:
12501d723f1dSPawel Jakub Dawidek 			sbuf_printf(sb, "AUTOMATIC");
1251b09121f9SPawel Jakub Dawidek 			break;
1252b09121f9SPawel Jakub Dawidek 		case G_STRIPE_TYPE_MANUAL:
12531d723f1dSPawel Jakub Dawidek 			sbuf_printf(sb, "MANUAL");
1254b09121f9SPawel Jakub Dawidek 			break;
1255b09121f9SPawel Jakub Dawidek 		default:
12561d723f1dSPawel Jakub Dawidek 			sbuf_printf(sb, "UNKNOWN");
1257b09121f9SPawel Jakub Dawidek 			break;
1258b09121f9SPawel Jakub Dawidek 		}
12591d723f1dSPawel Jakub Dawidek 		sbuf_printf(sb, "</Type>\n");
12601d723f1dSPawel Jakub Dawidek 		sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
12611d723f1dSPawel Jakub Dawidek 		    indent, sc->sc_ndisks, g_stripe_nvalid(sc));
12621d723f1dSPawel Jakub Dawidek 		sbuf_printf(sb, "%s<State>", indent);
12631d723f1dSPawel Jakub Dawidek 		if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
12641d723f1dSPawel Jakub Dawidek 			sbuf_printf(sb, "UP");
12653fb17452SPawel Jakub Dawidek 		else
12661d723f1dSPawel Jakub Dawidek 			sbuf_printf(sb, "DOWN");
12671d723f1dSPawel Jakub Dawidek 		sbuf_printf(sb, "</State>\n");
12681d723f1dSPawel Jakub Dawidek 	}
1269b09121f9SPawel Jakub Dawidek }
1270b09121f9SPawel Jakub Dawidek 
1271b09121f9SPawel Jakub Dawidek DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);
1272