g_stripe.c revision 226998
1129473Spjd/*-
2142727Spjd * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3129473Spjd * All rights reserved.
4129473Spjd *
5129473Spjd * Redistribution and use in source and binary forms, with or without
6129473Spjd * modification, are permitted provided that the following conditions
7129473Spjd * are met:
8129473Spjd * 1. Redistributions of source code must retain the above copyright
9129473Spjd *    notice, this list of conditions and the following disclaimer.
10129473Spjd * 2. Redistributions in binary form must reproduce the above copyright
11129473Spjd *    notice, this list of conditions and the following disclaimer in the
12129473Spjd *    documentation and/or other materials provided with the distribution.
13155174Spjd *
14129473Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15129473Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16129473Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17129473Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18129473Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19129473Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20129473Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21129473Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22129473Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23129473Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24129473Spjd * SUCH DAMAGE.
25129473Spjd */
26129473Spjd
27129473Spjd#include <sys/cdefs.h>
28129473Spjd__FBSDID("$FreeBSD: head/sys/geom/stripe/g_stripe.c 226998 2011-11-01 17:04:42Z mav $");
29129473Spjd
30129473Spjd#include <sys/param.h>
31129473Spjd#include <sys/systm.h>
32129473Spjd#include <sys/kernel.h>
33129473Spjd#include <sys/module.h>
34129473Spjd#include <sys/lock.h>
35129473Spjd#include <sys/mutex.h>
36129473Spjd#include <sys/bio.h>
37223921Sae#include <sys/sbuf.h>
38129473Spjd#include <sys/sysctl.h>
39129473Spjd#include <sys/malloc.h>
40131878Spjd#include <vm/uma.h>
41129473Spjd#include <geom/geom.h>
42129473Spjd#include <geom/stripe/g_stripe.h>
43129473Spjd
44219029SnetchildFEATURE(geom_stripe, "GEOM striping support");
45129473Spjd
46151897Srwatsonstatic MALLOC_DEFINE(M_STRIPE, "stripe_data", "GEOM_STRIPE Data");
47129473Spjd
48131878Spjdstatic uma_zone_t g_stripe_zone;
49129473Spjd
50129473Spjdstatic int g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force);
51129473Spjdstatic int g_stripe_destroy_geom(struct gctl_req *req, struct g_class *mp,
52129473Spjd    struct g_geom *gp);
53129473Spjd
54129473Spjdstatic g_taste_t g_stripe_taste;
55129473Spjdstatic g_ctl_req_t g_stripe_config;
56129473Spjdstatic g_dumpconf_t g_stripe_dumpconf;
57131878Spjdstatic g_init_t g_stripe_init;
58131878Spjdstatic g_fini_t g_stripe_fini;
59129473Spjd
60129473Spjdstruct g_class g_stripe_class = {
61129473Spjd	.name = G_STRIPE_CLASS_NAME,
62133318Sphk	.version = G_VERSION,
63129473Spjd	.ctlreq = g_stripe_config,
64129473Spjd	.taste = g_stripe_taste,
65131878Spjd	.destroy_geom = g_stripe_destroy_geom,
66131878Spjd	.init = g_stripe_init,
67131878Spjd	.fini = g_stripe_fini
68129473Spjd};
69129473Spjd
70131878SpjdSYSCTL_DECL(_kern_geom);
71131878SpjdSYSCTL_NODE(_kern_geom, OID_AUTO, stripe, CTLFLAG_RW, 0, "GEOM_STRIPE stuff");
72131878Spjdstatic u_int g_stripe_debug = 0;
73134528SpjdTUNABLE_INT("kern.geom.stripe.debug", &g_stripe_debug);
74131878SpjdSYSCTL_UINT(_kern_geom_stripe, OID_AUTO, debug, CTLFLAG_RW, &g_stripe_debug, 0,
75131878Spjd    "Debug level");
76138623Spjdstatic int g_stripe_fast = 0;
77131878SpjdTUNABLE_INT("kern.geom.stripe.fast", &g_stripe_fast);
78131878Spjdstatic int
79131878Spjdg_sysctl_stripe_fast(SYSCTL_HANDLER_ARGS)
80131878Spjd{
81131878Spjd	int error, fast;
82129473Spjd
83131878Spjd	fast = g_stripe_fast;
84170289Sdwmalone	error = sysctl_handle_int(oidp, &fast, 0, req);
85131878Spjd	if (error == 0 && req->newptr != NULL)
86131878Spjd		g_stripe_fast = fast;
87131878Spjd	return (error);
88131878Spjd}
89131878SpjdSYSCTL_PROC(_kern_geom_stripe, OID_AUTO, fast, CTLTYPE_INT | CTLFLAG_RW,
90132095Spjd    NULL, 0, g_sysctl_stripe_fast, "I", "Fast, but memory-consuming, mode");
91196837Smavstatic u_int g_stripe_maxmem = MAXPHYS * 100;
92131878SpjdTUNABLE_INT("kern.geom.stripe.maxmem", &g_stripe_maxmem);
93131878SpjdSYSCTL_UINT(_kern_geom_stripe, OID_AUTO, maxmem, CTLFLAG_RD, &g_stripe_maxmem,
94132095Spjd    0, "Maximum memory that can be allocated in \"fast\" mode (in bytes)");
95133205Spjdstatic u_int g_stripe_fast_failed = 0;
96133205SpjdSYSCTL_UINT(_kern_geom_stripe, OID_AUTO, fast_failed, CTLFLAG_RD,
97133205Spjd    &g_stripe_fast_failed, 0, "How many times \"fast\" mode failed");
98131878Spjd
99129473Spjd/*
100129473Spjd * Greatest Common Divisor.
101129473Spjd */
102129473Spjdstatic u_int
103129473Spjdgcd(u_int a, u_int b)
104129473Spjd{
105129473Spjd	u_int c;
106129473Spjd
107129473Spjd	while (b != 0) {
108129473Spjd		c = a;
109129473Spjd		a = b;
110129473Spjd		b = (c % b);
111129473Spjd	}
112129473Spjd	return (a);
113129473Spjd}
114129473Spjd
115129473Spjd/*
116129473Spjd * Least Common Multiple.
117129473Spjd */
118129473Spjdstatic u_int
119129473Spjdlcm(u_int a, u_int b)
120129473Spjd{
121129473Spjd
122129473Spjd	return ((a * b) / gcd(a, b));
123129473Spjd}
124129473Spjd
125131878Spjdstatic void
126131878Spjdg_stripe_init(struct g_class *mp __unused)
127131878Spjd{
128131878Spjd
129196837Smav	g_stripe_zone = uma_zcreate("g_stripe_zone", MAXPHYS, NULL, NULL,
130131878Spjd	    NULL, NULL, 0, 0);
131196837Smav	g_stripe_maxmem -= g_stripe_maxmem % MAXPHYS;
132196837Smav	uma_zone_set_max(g_stripe_zone, g_stripe_maxmem / MAXPHYS);
133131878Spjd}
134131878Spjd
135131878Spjdstatic void
136131878Spjdg_stripe_fini(struct g_class *mp __unused)
137131878Spjd{
138131878Spjd
139131878Spjd	uma_zdestroy(g_stripe_zone);
140131878Spjd}
141131878Spjd
142129473Spjd/*
143129473Spjd * Return the number of valid disks.
144129473Spjd */
145129473Spjdstatic u_int
146129473Spjdg_stripe_nvalid(struct g_stripe_softc *sc)
147129473Spjd{
148129473Spjd	u_int i, no;
149129473Spjd
150129473Spjd	no = 0;
151129473Spjd	for (i = 0; i < sc->sc_ndisks; i++) {
152129473Spjd		if (sc->sc_disks[i] != NULL)
153129473Spjd			no++;
154129473Spjd	}
155129473Spjd
156129473Spjd	return (no);
157129473Spjd}
158129473Spjd
159129473Spjdstatic void
160129473Spjdg_stripe_remove_disk(struct g_consumer *cp)
161129473Spjd{
162129473Spjd	struct g_stripe_softc *sc;
163129473Spjd
164226998Smav	g_topology_assert();
165129473Spjd	KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
166226998Smav	sc = (struct g_stripe_softc *)cp->geom->softc;
167129473Spjd	KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
168129473Spjd
169226998Smav	if (cp->private == NULL) {
170226998Smav		G_STRIPE_DEBUG(0, "Disk %s removed from %s.",
171226998Smav		    cp->provider->name, sc->sc_name);
172226998Smav		cp->private = (void *)(uintptr_t)-1;
173226998Smav	}
174129473Spjd
175129473Spjd	if (sc->sc_provider != NULL) {
176148092Spjd		sc->sc_provider->flags |= G_PF_WITHER;
177226998Smav		G_STRIPE_DEBUG(0, "Device %s deactivated.",
178226998Smav		    sc->sc_provider->name);
179129473Spjd		g_orphan_provider(sc->sc_provider, ENXIO);
180129473Spjd		sc->sc_provider = NULL;
181129473Spjd	}
182129473Spjd
183129473Spjd	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
184226998Smav		return;
185226998Smav	sc->sc_disks[cp->index] = NULL;
186226998Smav	cp->index = 0;
187129473Spjd	g_detach(cp);
188129473Spjd	g_destroy_consumer(cp);
189226998Smav	/* If there are no valid disks anymore, remove device. */
190226998Smav	if (LIST_EMPTY(&sc->sc_geom->consumer))
191226998Smav		g_stripe_destroy(sc, 1);
192129473Spjd}
193129473Spjd
194129473Spjdstatic void
195129473Spjdg_stripe_orphan(struct g_consumer *cp)
196129473Spjd{
197129473Spjd	struct g_stripe_softc *sc;
198129473Spjd	struct g_geom *gp;
199129473Spjd
200129473Spjd	g_topology_assert();
201129473Spjd	gp = cp->geom;
202129473Spjd	sc = gp->softc;
203129473Spjd	if (sc == NULL)
204129473Spjd		return;
205129473Spjd
206129473Spjd	g_stripe_remove_disk(cp);
207129473Spjd}
208129473Spjd
209129473Spjdstatic int
210129473Spjdg_stripe_access(struct g_provider *pp, int dr, int dw, int de)
211129473Spjd{
212226998Smav	struct g_consumer *cp1, *cp2, *tmp;
213129473Spjd	struct g_stripe_softc *sc;
214129473Spjd	struct g_geom *gp;
215129473Spjd	int error;
216129473Spjd
217226998Smav	g_topology_assert();
218129473Spjd	gp = pp->geom;
219129473Spjd	sc = gp->softc;
220226998Smav	KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
221129473Spjd
222129473Spjd	/* On first open, grab an extra "exclusive" bit */
223129473Spjd	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
224129473Spjd		de++;
225129473Spjd	/* ... and let go of it on last close */
226129473Spjd	if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
227129473Spjd		de--;
228129473Spjd
229226998Smav	LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) {
230129473Spjd		error = g_access(cp1, dr, dw, de);
231226998Smav		if (error != 0)
232226998Smav			goto fail;
233226998Smav		if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 &&
234226998Smav		    cp1->private != NULL) {
235226998Smav			g_stripe_remove_disk(cp1); /* May destroy geom. */
236129473Spjd		}
237129473Spjd	}
238226998Smav	return (0);
239129473Spjd
240226998Smavfail:
241226998Smav	LIST_FOREACH(cp2, &gp->consumer, consumer) {
242226998Smav		if (cp1 == cp2)
243226998Smav			break;
244226998Smav		g_access(cp2, -dr, -dw, -de);
245226998Smav	}
246129473Spjd	return (error);
247129473Spjd}
248129473Spjd
249129473Spjdstatic void
250131878Spjdg_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset,
251131878Spjd    off_t length, int mode)
252129473Spjd{
253131878Spjd	u_int stripesize;
254131878Spjd	size_t len;
255131878Spjd
256131878Spjd	stripesize = sc->sc_stripesize;
257131878Spjd	len = (size_t)(stripesize - (offset & (stripesize - 1)));
258131878Spjd	do {
259131878Spjd		bcopy(src, dst, len);
260131878Spjd		if (mode) {
261131878Spjd			dst += len + stripesize * (sc->sc_ndisks - 1);
262131878Spjd			src += len;
263131878Spjd		} else {
264131878Spjd			dst += len;
265131878Spjd			src += len + stripesize * (sc->sc_ndisks - 1);
266131878Spjd		}
267131878Spjd		length -= len;
268131878Spjd		KASSERT(length >= 0,
269131878Spjd		    ("Length < 0 (stripesize=%zu, offset=%jd, length=%jd).",
270131878Spjd		    (size_t)stripesize, (intmax_t)offset, (intmax_t)length));
271131878Spjd		if (length > stripesize)
272131878Spjd			len = stripesize;
273131878Spjd		else
274131878Spjd			len = length;
275131878Spjd	} while (length > 0);
276131878Spjd}
277131878Spjd
278131878Spjdstatic void
279131878Spjdg_stripe_done(struct bio *bp)
280131878Spjd{
281129473Spjd	struct g_stripe_softc *sc;
282131878Spjd	struct bio *pbp;
283131878Spjd
284131878Spjd	pbp = bp->bio_parent;
285131878Spjd	sc = pbp->bio_to->geom->softc;
286131878Spjd	if (pbp->bio_error == 0)
287131878Spjd		pbp->bio_error = bp->bio_error;
288131878Spjd	pbp->bio_completed += bp->bio_completed;
289133204Spjd	if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) {
290133204Spjd		g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset,
291131878Spjd		    bp->bio_length, 1);
292133204Spjd		bp->bio_data = bp->bio_caller1;
293133204Spjd		bp->bio_caller1 = NULL;
294131878Spjd	}
295131878Spjd	g_destroy_bio(bp);
296131878Spjd	pbp->bio_inbed++;
297131878Spjd	if (pbp->bio_children == pbp->bio_inbed) {
298133204Spjd		if (pbp->bio_driver1 != NULL)
299133204Spjd			uma_zfree(g_stripe_zone, pbp->bio_driver1);
300131878Spjd		g_io_deliver(pbp, pbp->bio_error);
301131878Spjd	}
302131878Spjd}
303131878Spjd
304131878Spjdstatic int
305131878Spjdg_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
306131878Spjd{
307131878Spjd	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
308131878Spjd	u_int nparts = 0, stripesize;
309131878Spjd	struct g_stripe_softc *sc;
310131878Spjd	char *addr, *data = NULL;
311129473Spjd	struct bio *cbp;
312131878Spjd	int error;
313131878Spjd
314131878Spjd	sc = bp->bio_to->geom->softc;
315131878Spjd
316131878Spjd	addr = bp->bio_data;
317131878Spjd	stripesize = sc->sc_stripesize;
318131878Spjd
319131878Spjd	cbp = g_clone_bio(bp);
320131878Spjd	if (cbp == NULL) {
321131878Spjd		error = ENOMEM;
322131878Spjd		goto failure;
323131878Spjd	}
324131878Spjd	TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
325131878Spjd	nparts++;
326131878Spjd	/*
327131878Spjd	 * Fill in the component buf structure.
328131878Spjd	 */
329131878Spjd	cbp->bio_done = g_stripe_done;
330131878Spjd	cbp->bio_offset = offset;
331131878Spjd	cbp->bio_data = addr;
332133204Spjd	cbp->bio_caller1 = NULL;
333131878Spjd	cbp->bio_length = length;
334133204Spjd	cbp->bio_caller2 = sc->sc_disks[no];
335131878Spjd
336131878Spjd	/* offset -= offset % stripesize; */
337131878Spjd	offset -= offset & (stripesize - 1);
338131878Spjd	addr += length;
339131878Spjd	length = bp->bio_length - length;
340131878Spjd	for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
341131878Spjd		if (no > sc->sc_ndisks - 1) {
342131878Spjd			no = 0;
343131878Spjd			offset += stripesize;
344131878Spjd		}
345131878Spjd		if (nparts >= sc->sc_ndisks) {
346131878Spjd			cbp = TAILQ_NEXT(cbp, bio_queue);
347131878Spjd			if (cbp == NULL)
348131878Spjd				cbp = TAILQ_FIRST(&queue);
349131878Spjd			nparts++;
350131878Spjd			/*
351131878Spjd			 * Update bio structure.
352131878Spjd			 */
353131878Spjd			/*
354131878Spjd			 * MIN() is in case when
355131878Spjd			 * (bp->bio_length % sc->sc_stripesize) != 0.
356131878Spjd			 */
357131878Spjd			cbp->bio_length += MIN(stripesize, length);
358133204Spjd			if (cbp->bio_caller1 == NULL) {
359133204Spjd				cbp->bio_caller1 = cbp->bio_data;
360131878Spjd				cbp->bio_data = NULL;
361131878Spjd				if (data == NULL) {
362131878Spjd					data = uma_zalloc(g_stripe_zone,
363131878Spjd					    M_NOWAIT);
364131878Spjd					if (data == NULL) {
365131878Spjd						error = ENOMEM;
366131878Spjd						goto failure;
367131878Spjd					}
368131878Spjd				}
369131878Spjd			}
370131878Spjd		} else {
371131878Spjd			cbp = g_clone_bio(bp);
372131878Spjd			if (cbp == NULL) {
373131878Spjd				error = ENOMEM;
374131878Spjd				goto failure;
375131878Spjd			}
376131878Spjd			TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
377131878Spjd			nparts++;
378131878Spjd			/*
379131878Spjd			 * Fill in the component buf structure.
380131878Spjd			 */
381131878Spjd			cbp->bio_done = g_stripe_done;
382131878Spjd			cbp->bio_offset = offset;
383131878Spjd			cbp->bio_data = addr;
384133204Spjd			cbp->bio_caller1 = NULL;
385131878Spjd			/*
386131878Spjd			 * MIN() is in case when
387131878Spjd			 * (bp->bio_length % sc->sc_stripesize) != 0.
388131878Spjd			 */
389131878Spjd			cbp->bio_length = MIN(stripesize, length);
390133204Spjd			cbp->bio_caller2 = sc->sc_disks[no];
391131878Spjd		}
392131878Spjd	}
393131878Spjd	if (data != NULL)
394133444Spjd		bp->bio_driver1 = data;
395131878Spjd	/*
396131878Spjd	 * Fire off all allocated requests!
397131878Spjd	 */
398131878Spjd	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
399131878Spjd		struct g_consumer *cp;
400131878Spjd
401131878Spjd		TAILQ_REMOVE(&queue, cbp, bio_queue);
402133204Spjd		cp = cbp->bio_caller2;
403133204Spjd		cbp->bio_caller2 = NULL;
404131878Spjd		cbp->bio_to = cp->provider;
405133204Spjd		if (cbp->bio_caller1 != NULL) {
406131878Spjd			cbp->bio_data = data;
407131878Spjd			if (bp->bio_cmd == BIO_WRITE) {
408133204Spjd				g_stripe_copy(sc, cbp->bio_caller1, data,
409131878Spjd				    cbp->bio_offset, cbp->bio_length, 0);
410131878Spjd			}
411131878Spjd			data += cbp->bio_length;
412131878Spjd		}
413131878Spjd		G_STRIPE_LOGREQ(cbp, "Sending request.");
414131878Spjd		g_io_request(cbp, cp);
415131878Spjd	}
416131878Spjd	return (0);
417131878Spjdfailure:
418131878Spjd	if (data != NULL)
419131878Spjd		uma_zfree(g_stripe_zone, data);
420131878Spjd	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
421131878Spjd		TAILQ_REMOVE(&queue, cbp, bio_queue);
422133204Spjd		if (cbp->bio_caller1 != NULL) {
423133204Spjd			cbp->bio_data = cbp->bio_caller1;
424133204Spjd			cbp->bio_caller1 = NULL;
425131878Spjd		}
426133201Spjd		bp->bio_children--;
427131878Spjd		g_destroy_bio(cbp);
428131878Spjd	}
429131878Spjd	return (error);
430131878Spjd}
431131878Spjd
432131878Spjdstatic int
433131878Spjdg_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
434131878Spjd{
435131878Spjd	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
436131878Spjd	struct g_stripe_softc *sc;
437129473Spjd	uint32_t stripesize;
438131878Spjd	struct bio *cbp;
439129473Spjd	char *addr;
440131878Spjd	int error;
441129473Spjd
442131878Spjd	sc = bp->bio_to->geom->softc;
443131878Spjd
444131878Spjd	addr = bp->bio_data;
445131878Spjd	stripesize = sc->sc_stripesize;
446131878Spjd
447131878Spjd	cbp = g_clone_bio(bp);
448131878Spjd	if (cbp == NULL) {
449131878Spjd		error = ENOMEM;
450131878Spjd		goto failure;
451131878Spjd	}
452131878Spjd	TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
453129473Spjd	/*
454131878Spjd	 * Fill in the component buf structure.
455131878Spjd	 */
456131878Spjd	cbp->bio_done = g_std_done;
457131878Spjd	cbp->bio_offset = offset;
458131878Spjd	cbp->bio_data = addr;
459131878Spjd	cbp->bio_length = length;
460133204Spjd	cbp->bio_caller2 = sc->sc_disks[no];
461131878Spjd
462131878Spjd	/* offset -= offset % stripesize; */
463131878Spjd	offset -= offset & (stripesize - 1);
464131878Spjd	addr += length;
465131878Spjd	length = bp->bio_length - length;
466131878Spjd	for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
467131878Spjd		if (no > sc->sc_ndisks - 1) {
468131878Spjd			no = 0;
469131878Spjd			offset += stripesize;
470131878Spjd		}
471131878Spjd		cbp = g_clone_bio(bp);
472131878Spjd		if (cbp == NULL) {
473131878Spjd			error = ENOMEM;
474131878Spjd			goto failure;
475131878Spjd		}
476131878Spjd		TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
477131878Spjd
478131878Spjd		/*
479131878Spjd		 * Fill in the component buf structure.
480131878Spjd		 */
481131878Spjd		cbp->bio_done = g_std_done;
482131878Spjd		cbp->bio_offset = offset;
483131878Spjd		cbp->bio_data = addr;
484131878Spjd		/*
485131878Spjd		 * MIN() is in case when
486131878Spjd		 * (bp->bio_length % sc->sc_stripesize) != 0.
487131878Spjd		 */
488131878Spjd		cbp->bio_length = MIN(stripesize, length);
489131878Spjd
490133204Spjd		cbp->bio_caller2 = sc->sc_disks[no];
491131878Spjd	}
492131878Spjd	/*
493131878Spjd	 * Fire off all allocated requests!
494131878Spjd	 */
495131878Spjd	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
496131878Spjd		struct g_consumer *cp;
497131878Spjd
498131878Spjd		TAILQ_REMOVE(&queue, cbp, bio_queue);
499133204Spjd		cp = cbp->bio_caller2;
500133204Spjd		cbp->bio_caller2 = NULL;
501131878Spjd		cbp->bio_to = cp->provider;
502131878Spjd		G_STRIPE_LOGREQ(cbp, "Sending request.");
503131878Spjd		g_io_request(cbp, cp);
504131878Spjd	}
505131878Spjd	return (0);
506131878Spjdfailure:
507131878Spjd	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
508131878Spjd		TAILQ_REMOVE(&queue, cbp, bio_queue);
509133201Spjd		bp->bio_children--;
510131878Spjd		g_destroy_bio(cbp);
511131878Spjd	}
512131878Spjd	return (error);
513131878Spjd}
514131878Spjd
515131878Spjdstatic void
516163836Spjdg_stripe_flush(struct g_stripe_softc *sc, struct bio *bp)
517163836Spjd{
518163836Spjd	struct bio_queue_head queue;
519163836Spjd	struct g_consumer *cp;
520163836Spjd	struct bio *cbp;
521163836Spjd	u_int no;
522163836Spjd
523163836Spjd	bioq_init(&queue);
524163836Spjd	for (no = 0; no < sc->sc_ndisks; no++) {
525163836Spjd		cbp = g_clone_bio(bp);
526163836Spjd		if (cbp == NULL) {
527163836Spjd			for (cbp = bioq_first(&queue); cbp != NULL;
528163836Spjd			    cbp = bioq_first(&queue)) {
529163836Spjd				bioq_remove(&queue, cbp);
530163836Spjd				g_destroy_bio(cbp);
531163836Spjd			}
532163836Spjd			if (bp->bio_error == 0)
533163836Spjd				bp->bio_error = ENOMEM;
534163836Spjd			g_io_deliver(bp, bp->bio_error);
535163836Spjd			return;
536163836Spjd		}
537163836Spjd		bioq_insert_tail(&queue, cbp);
538163836Spjd		cbp->bio_done = g_std_done;
539163836Spjd		cbp->bio_caller1 = sc->sc_disks[no];
540163836Spjd		cbp->bio_to = sc->sc_disks[no]->provider;
541163836Spjd	}
542163836Spjd	for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
543163836Spjd		bioq_remove(&queue, cbp);
544163836Spjd		G_STRIPE_LOGREQ(cbp, "Sending request.");
545163836Spjd		cp = cbp->bio_caller1;
546163836Spjd		cbp->bio_caller1 = NULL;
547163836Spjd		g_io_request(cbp, cp);
548163836Spjd	}
549163836Spjd}
550163836Spjd
551163836Spjdstatic void
552131878Spjdg_stripe_start(struct bio *bp)
553131878Spjd{
554131878Spjd	off_t offset, start, length, nstripe;
555131878Spjd	struct g_stripe_softc *sc;
556131878Spjd	u_int no, stripesize;
557131878Spjd	int error, fast = 0;
558131878Spjd
559131878Spjd	sc = bp->bio_to->geom->softc;
560131878Spjd	/*
561129473Spjd	 * If sc == NULL, provider's error should be set and g_stripe_start()
562129473Spjd	 * should not be called at all.
563129473Spjd	 */
564129473Spjd	KASSERT(sc != NULL,
565129473Spjd	    ("Provider's error should be set (error=%d)(device=%s).",
566129473Spjd	    bp->bio_to->error, bp->bio_to->name));
567129473Spjd
568129473Spjd	G_STRIPE_LOGREQ(bp, "Request received.");
569129473Spjd
570129473Spjd	switch (bp->bio_cmd) {
571129473Spjd	case BIO_READ:
572129473Spjd	case BIO_WRITE:
573129473Spjd	case BIO_DELETE:
574129473Spjd		break;
575163886Spjd	case BIO_FLUSH:
576163886Spjd		g_stripe_flush(sc, bp);
577163886Spjd		return;
578129473Spjd	case BIO_GETATTR:
579129473Spjd		/* To which provider it should be delivered? */
580129473Spjd	default:
581129473Spjd		g_io_deliver(bp, EOPNOTSUPP);
582129473Spjd		return;
583129473Spjd	}
584129473Spjd
585129473Spjd	stripesize = sc->sc_stripesize;
586129473Spjd
587129473Spjd	/*
588131878Spjd	 * Calculations are quite messy, but fast I hope.
589129473Spjd	 */
590129473Spjd
591129473Spjd	/* Stripe number. */
592129473Spjd	/* nstripe = bp->bio_offset / stripesize; */
593129473Spjd	nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
594129473Spjd	/* Disk number. */
595129473Spjd	no = nstripe % sc->sc_ndisks;
596129473Spjd	/* Start position in stripe. */
597129473Spjd	/* start = bp->bio_offset % stripesize; */
598129473Spjd	start = bp->bio_offset & (stripesize - 1);
599129473Spjd	/* Start position in disk. */
600131878Spjd	/* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
601131878Spjd	offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
602129473Spjd	/* Length of data to operate. */
603129473Spjd	length = MIN(bp->bio_length, stripesize - start);
604129473Spjd
605131878Spjd	/*
606131878Spjd	 * Do use "fast" mode when:
607131878Spjd	 * 1. "Fast" mode is ON.
608131878Spjd	 * and
609196837Smav	 * 2. Request size is less than or equal to MAXPHYS,
610131878Spjd	 *    which should always be true.
611131878Spjd	 * and
612131878Spjd	 * 3. Request size is bigger than stripesize * ndisks. If it isn't,
613131878Spjd	 *    there will be no need to send more than one I/O request to
614131878Spjd	 *    a provider, so there is nothing to optmize.
615131878Spjd	 */
616196837Smav	if (g_stripe_fast && bp->bio_length <= MAXPHYS &&
617131878Spjd	    bp->bio_length >= stripesize * sc->sc_ndisks) {
618131878Spjd		fast = 1;
619129473Spjd	}
620131878Spjd	error = 0;
621133205Spjd	if (fast) {
622131878Spjd		error = g_stripe_start_fast(bp, no, offset, length);
623133205Spjd		if (error != 0)
624133205Spjd			g_stripe_fast_failed++;
625133205Spjd	}
626129473Spjd	/*
627131878Spjd	 * Do use "economic" when:
628131878Spjd	 * 1. "Economic" mode is ON.
629131878Spjd	 * or
630204070Spjd	 * 2. "Fast" mode failed. It can only fail if there is no memory.
631129473Spjd	 */
632131878Spjd	if (!fast || error != 0)
633131878Spjd		error = g_stripe_start_economic(bp, no, offset, length);
634131878Spjd	if (error != 0) {
635131878Spjd		if (bp->bio_error == 0)
636131878Spjd			bp->bio_error = error;
637131878Spjd		g_io_deliver(bp, bp->bio_error);
638129473Spjd	}
639129473Spjd}
640129473Spjd
641129473Spjdstatic void
642129473Spjdg_stripe_check_and_run(struct g_stripe_softc *sc)
643129473Spjd{
644129473Spjd	off_t mediasize, ms;
645129473Spjd	u_int no, sectorsize = 0;
646129473Spjd
647226998Smav	g_topology_assert();
648129473Spjd	if (g_stripe_nvalid(sc) != sc->sc_ndisks)
649129473Spjd		return;
650129473Spjd
651132664Spjd	sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
652132664Spjd	    sc->sc_name);
653129473Spjd	/*
654129473Spjd	 * Find the smallest disk.
655129473Spjd	 */
656129473Spjd	mediasize = sc->sc_disks[0]->provider->mediasize;
657129473Spjd	if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
658129473Spjd		mediasize -= sc->sc_disks[0]->provider->sectorsize;
659129473Spjd	mediasize -= mediasize % sc->sc_stripesize;
660129473Spjd	sectorsize = sc->sc_disks[0]->provider->sectorsize;
661129473Spjd	for (no = 1; no < sc->sc_ndisks; no++) {
662129473Spjd		ms = sc->sc_disks[no]->provider->mediasize;
663129473Spjd		if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
664129473Spjd			ms -= sc->sc_disks[no]->provider->sectorsize;
665129473Spjd		ms -= ms % sc->sc_stripesize;
666129473Spjd		if (ms < mediasize)
667129473Spjd			mediasize = ms;
668129473Spjd		sectorsize = lcm(sectorsize,
669129473Spjd		    sc->sc_disks[no]->provider->sectorsize);
670129473Spjd	}
671129473Spjd	sc->sc_provider->sectorsize = sectorsize;
672129473Spjd	sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
673200933Smav	sc->sc_provider->stripesize = sc->sc_stripesize;
674200933Smav	sc->sc_provider->stripeoffset = 0;
675129473Spjd	g_error_provider(sc->sc_provider, 0);
676129473Spjd
677226998Smav	G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
678129473Spjd}
679129473Spjd
680129473Spjdstatic int
681129473Spjdg_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
682129473Spjd{
683129473Spjd	struct g_provider *pp;
684129473Spjd	u_char *buf;
685129473Spjd	int error;
686129473Spjd
687129473Spjd	g_topology_assert();
688129473Spjd
689129473Spjd	error = g_access(cp, 1, 0, 0);
690129473Spjd	if (error != 0)
691129473Spjd		return (error);
692129473Spjd	pp = cp->provider;
693129473Spjd	g_topology_unlock();
694129473Spjd	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
695129473Spjd	    &error);
696129473Spjd	g_topology_lock();
697129473Spjd	g_access(cp, -1, 0, 0);
698129473Spjd	if (buf == NULL)
699129473Spjd		return (error);
700129473Spjd
701129473Spjd	/* Decode metadata. */
702129473Spjd	stripe_metadata_decode(buf, md);
703129473Spjd	g_free(buf);
704129473Spjd
705129473Spjd	return (0);
706129473Spjd}
707129473Spjd
708129473Spjd/*
709129473Spjd * Add disk to given device.
710129473Spjd */
711129473Spjdstatic int
712129473Spjdg_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
713129473Spjd{
714129473Spjd	struct g_consumer *cp, *fcp;
715129473Spjd	struct g_geom *gp;
716129473Spjd	int error;
717129473Spjd
718226998Smav	g_topology_assert();
719129473Spjd	/* Metadata corrupted? */
720129473Spjd	if (no >= sc->sc_ndisks)
721129473Spjd		return (EINVAL);
722129473Spjd
723129473Spjd	/* Check if disk is not already attached. */
724129473Spjd	if (sc->sc_disks[no] != NULL)
725129473Spjd		return (EEXIST);
726129473Spjd
727129473Spjd	gp = sc->sc_geom;
728129473Spjd	fcp = LIST_FIRST(&gp->consumer);
729129473Spjd
730129473Spjd	cp = g_new_consumer(gp);
731226998Smav	cp->private = NULL;
732226998Smav	cp->index = no;
733129473Spjd	error = g_attach(cp, pp);
734129473Spjd	if (error != 0) {
735129473Spjd		g_destroy_consumer(cp);
736129473Spjd		return (error);
737129473Spjd	}
738129473Spjd
739129473Spjd	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
740129473Spjd		error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
741129473Spjd		if (error != 0) {
742129473Spjd			g_detach(cp);
743129473Spjd			g_destroy_consumer(cp);
744129473Spjd			return (error);
745129473Spjd		}
746129473Spjd	}
747129473Spjd	if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
748129473Spjd		struct g_stripe_metadata md;
749129473Spjd
750129473Spjd		/* Reread metadata. */
751129473Spjd		error = g_stripe_read_metadata(cp, &md);
752129473Spjd		if (error != 0)
753129473Spjd			goto fail;
754129473Spjd
755129473Spjd		if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
756129473Spjd		    strcmp(md.md_name, sc->sc_name) != 0 ||
757129473Spjd		    md.md_id != sc->sc_id) {
758129473Spjd			G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
759129473Spjd			goto fail;
760129473Spjd		}
761129473Spjd	}
762129473Spjd
763129473Spjd	sc->sc_disks[no] = cp;
764132664Spjd	G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
765129473Spjd	g_stripe_check_and_run(sc);
766129473Spjd
767129473Spjd	return (0);
768129473Spjdfail:
769129473Spjd	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
770129473Spjd		g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
771129473Spjd	g_detach(cp);
772129473Spjd	g_destroy_consumer(cp);
773129473Spjd	return (error);
774129473Spjd}
775129473Spjd
776129473Spjdstatic struct g_geom *
777129473Spjdg_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
778129473Spjd    u_int type)
779129473Spjd{
780129473Spjd	struct g_stripe_softc *sc;
781129473Spjd	struct g_geom *gp;
782129473Spjd	u_int no;
783129473Spjd
784226998Smav	g_topology_assert();
785132664Spjd	G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
786129473Spjd	    md->md_id);
787129473Spjd
788129473Spjd	/* Two disks is minimum. */
789132664Spjd	if (md->md_all < 2) {
790132664Spjd		G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
791129473Spjd		return (NULL);
792129473Spjd	}
793129473Spjd#if 0
794129473Spjd	/* Stripe size have to be grater than or equal to sector size. */
795129473Spjd	if (md->md_stripesize < sectorsize) {
796132664Spjd		G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
797129473Spjd		return (NULL);
798129473Spjd	}
799129473Spjd#endif
800129473Spjd	/* Stripe size have to be power of 2. */
801129473Spjd	if (!powerof2(md->md_stripesize)) {
802132664Spjd		G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
803129473Spjd		return (NULL);
804129473Spjd	}
805129473Spjd
806129473Spjd	/* Check for duplicate unit */
807129473Spjd	LIST_FOREACH(gp, &mp->geom, geom) {
808129473Spjd		sc = gp->softc;
809129473Spjd		if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
810129473Spjd			G_STRIPE_DEBUG(0, "Device %s already configured.",
811132664Spjd			    sc->sc_name);
812129473Spjd			return (NULL);
813129473Spjd		}
814129473Spjd	}
815132664Spjd	gp = g_new_geomf(mp, "%s", md->md_name);
816132662Spjd	sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
817129473Spjd	gp->start = g_stripe_start;
818129473Spjd	gp->spoiled = g_stripe_orphan;
819129473Spjd	gp->orphan = g_stripe_orphan;
820129473Spjd	gp->access = g_stripe_access;
821129473Spjd	gp->dumpconf = g_stripe_dumpconf;
822129473Spjd
823129473Spjd	sc->sc_id = md->md_id;
824129473Spjd	sc->sc_stripesize = md->md_stripesize;
825149300Spjd	sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
826129473Spjd	sc->sc_ndisks = md->md_all;
827129473Spjd	sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
828129473Spjd	    M_STRIPE, M_WAITOK | M_ZERO);
829129473Spjd	for (no = 0; no < sc->sc_ndisks; no++)
830129473Spjd		sc->sc_disks[no] = NULL;
831129473Spjd	sc->sc_type = type;
832129473Spjd
833129473Spjd	gp->softc = sc;
834129473Spjd	sc->sc_geom = gp;
835129473Spjd	sc->sc_provider = NULL;
836129473Spjd
837132664Spjd	G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
838129473Spjd
839129473Spjd	return (gp);
840129473Spjd}
841129473Spjd
842129473Spjdstatic int
843129473Spjdg_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
844129473Spjd{
845129473Spjd	struct g_provider *pp;
846226998Smav	struct g_consumer *cp, *cp1;
847129473Spjd	struct g_geom *gp;
848129473Spjd
849129473Spjd	g_topology_assert();
850129473Spjd
851129473Spjd	if (sc == NULL)
852129473Spjd		return (ENXIO);
853129473Spjd
854129473Spjd	pp = sc->sc_provider;
855129473Spjd	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
856129473Spjd		if (force) {
857129473Spjd			G_STRIPE_DEBUG(0, "Device %s is still open, so it "
858129473Spjd			    "can't be definitely removed.", pp->name);
859129473Spjd		} else {
860129473Spjd			G_STRIPE_DEBUG(1,
861129473Spjd			    "Device %s is still open (r%dw%de%d).", pp->name,
862129473Spjd			    pp->acr, pp->acw, pp->ace);
863129473Spjd			return (EBUSY);
864129473Spjd		}
865129473Spjd	}
866129473Spjd
867226998Smav	gp = sc->sc_geom;
868226998Smav	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
869226998Smav		g_stripe_remove_disk(cp);
870226998Smav		if (cp1 == NULL)
871226998Smav			return (0);	/* Recursion happened. */
872129473Spjd	}
873226998Smav	if (!LIST_EMPTY(&gp->consumer))
874226998Smav		return (EINPROGRESS);
875129473Spjd
876129473Spjd	gp->softc = NULL;
877129473Spjd	KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
878129473Spjd	    gp->name));
879129473Spjd	free(sc->sc_disks, M_STRIPE);
880129473Spjd	free(sc, M_STRIPE);
881226998Smav	G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
882129473Spjd	g_wither_geom(gp, ENXIO);
883129473Spjd	return (0);
884129473Spjd}
885129473Spjd
886129473Spjdstatic int
887129473Spjdg_stripe_destroy_geom(struct gctl_req *req __unused,
888129473Spjd    struct g_class *mp __unused, struct g_geom *gp)
889129473Spjd{
890129473Spjd	struct g_stripe_softc *sc;
891129473Spjd
892129473Spjd	sc = gp->softc;
893129473Spjd	return (g_stripe_destroy(sc, 0));
894129473Spjd}
895129473Spjd
896129473Spjdstatic struct g_geom *
897129473Spjdg_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
898129473Spjd{
899129473Spjd	struct g_stripe_metadata md;
900129473Spjd	struct g_stripe_softc *sc;
901129473Spjd	struct g_consumer *cp;
902129473Spjd	struct g_geom *gp;
903129473Spjd	int error;
904129473Spjd
905129473Spjd	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
906129473Spjd	g_topology_assert();
907129473Spjd
908197898Spjd	/* Skip providers that are already open for writing. */
909197898Spjd	if (pp->acw > 0)
910197898Spjd		return (NULL);
911197898Spjd
912129473Spjd	G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
913129473Spjd
914129473Spjd	gp = g_new_geomf(mp, "stripe:taste");
915129473Spjd	gp->start = g_stripe_start;
916129473Spjd	gp->access = g_stripe_access;
917129473Spjd	gp->orphan = g_stripe_orphan;
918129473Spjd	cp = g_new_consumer(gp);
919129473Spjd	g_attach(cp, pp);
920129473Spjd	error = g_stripe_read_metadata(cp, &md);
921133371Spjd	g_detach(cp);
922133371Spjd	g_destroy_consumer(cp);
923133371Spjd	g_destroy_geom(gp);
924129473Spjd	if (error != 0)
925129473Spjd		return (NULL);
926129473Spjd	gp = NULL;
927129473Spjd
928129473Spjd	if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
929129473Spjd		return (NULL);
930129473Spjd	if (md.md_version > G_STRIPE_VERSION) {
931129473Spjd		printf("geom_stripe.ko module is too old to handle %s.\n",
932129473Spjd		    pp->name);
933129473Spjd		return (NULL);
934129473Spjd	}
935133373Spjd	/*
936133373Spjd	 * Backward compatibility:
937133373Spjd	 */
938142727Spjd	/* There was no md_provider field in earlier versions of metadata. */
939133373Spjd	if (md.md_version < 2)
940133373Spjd		bzero(md.md_provider, sizeof(md.md_provider));
941142727Spjd	/* There was no md_provsize field in earlier versions of metadata. */
942142727Spjd	if (md.md_version < 3)
943142727Spjd		md.md_provsize = pp->mediasize;
944129473Spjd
945221101Smav	if (md.md_provider[0] != '\0' &&
946221101Smav	    !g_compare_names(md.md_provider, pp->name))
947133373Spjd		return (NULL);
948142727Spjd	if (md.md_provsize != pp->mediasize)
949142727Spjd		return (NULL);
950133373Spjd
951129473Spjd	/*
952129473Spjd	 * Let's check if device already exists.
953129473Spjd	 */
954129473Spjd	sc = NULL;
955129473Spjd	LIST_FOREACH(gp, &mp->geom, geom) {
956129473Spjd		sc = gp->softc;
957129473Spjd		if (sc == NULL)
958129473Spjd			continue;
959129473Spjd		if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
960129473Spjd			continue;
961129473Spjd		if (strcmp(md.md_name, sc->sc_name) != 0)
962129473Spjd			continue;
963129473Spjd		if (md.md_id != sc->sc_id)
964129473Spjd			continue;
965129473Spjd		break;
966129473Spjd	}
967129473Spjd	if (gp != NULL) {
968129473Spjd		G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
969129473Spjd		error = g_stripe_add_disk(sc, pp, md.md_no);
970129473Spjd		if (error != 0) {
971129473Spjd			G_STRIPE_DEBUG(0,
972129473Spjd			    "Cannot add disk %s to %s (error=%d).", pp->name,
973129473Spjd			    gp->name, error);
974129473Spjd			return (NULL);
975129473Spjd		}
976129473Spjd	} else {
977129473Spjd		gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
978129473Spjd		if (gp == NULL) {
979132664Spjd			G_STRIPE_DEBUG(0, "Cannot create device %s.",
980129473Spjd			    md.md_name);
981129473Spjd			return (NULL);
982129473Spjd		}
983129473Spjd		sc = gp->softc;
984129473Spjd		G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
985129473Spjd		error = g_stripe_add_disk(sc, pp, md.md_no);
986129473Spjd		if (error != 0) {
987129473Spjd			G_STRIPE_DEBUG(0,
988129473Spjd			    "Cannot add disk %s to %s (error=%d).", pp->name,
989129473Spjd			    gp->name, error);
990129473Spjd			g_stripe_destroy(sc, 1);
991129473Spjd			return (NULL);
992129473Spjd		}
993129473Spjd	}
994129473Spjd
995129473Spjd	return (gp);
996129473Spjd}
997129473Spjd
998129473Spjdstatic void
999129473Spjdg_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1000129473Spjd{
1001129473Spjd	u_int attached, no;
1002129473Spjd	struct g_stripe_metadata md;
1003129473Spjd	struct g_provider *pp;
1004129473Spjd	struct g_stripe_softc *sc;
1005129473Spjd	struct g_geom *gp;
1006129473Spjd	struct sbuf *sb;
1007129473Spjd	intmax_t *stripesize;
1008129473Spjd	const char *name;
1009129473Spjd	char param[16];
1010129473Spjd	int *nargs;
1011129473Spjd
1012129473Spjd	g_topology_assert();
1013129473Spjd	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1014129473Spjd	if (nargs == NULL) {
1015129473Spjd		gctl_error(req, "No '%s' argument.", "nargs");
1016129473Spjd		return;
1017129473Spjd	}
1018129473Spjd	if (*nargs <= 2) {
1019129473Spjd		gctl_error(req, "Too few arguments.");
1020129473Spjd		return;
1021129473Spjd	}
1022129473Spjd
1023129473Spjd	strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1024129473Spjd	md.md_version = G_STRIPE_VERSION;
1025129473Spjd	name = gctl_get_asciiparam(req, "arg0");
1026129473Spjd	if (name == NULL) {
1027129473Spjd		gctl_error(req, "No 'arg%u' argument.", 0);
1028129473Spjd		return;
1029129473Spjd	}
1030129473Spjd	strlcpy(md.md_name, name, sizeof(md.md_name));
1031129473Spjd	md.md_id = arc4random();
1032129473Spjd	md.md_no = 0;
1033129473Spjd	md.md_all = *nargs - 1;
1034129473Spjd	stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1035129473Spjd	if (stripesize == NULL) {
1036129473Spjd		gctl_error(req, "No '%s' argument.", "stripesize");
1037129473Spjd		return;
1038129473Spjd	}
1039129473Spjd	md.md_stripesize = *stripesize;
1040133373Spjd	bzero(md.md_provider, sizeof(md.md_provider));
1041142727Spjd	/* This field is not important here. */
1042142727Spjd	md.md_provsize = 0;
1043129473Spjd
1044129473Spjd	/* Check all providers are valid */
1045129473Spjd	for (no = 1; no < *nargs; no++) {
1046129473Spjd		snprintf(param, sizeof(param), "arg%u", no);
1047129473Spjd		name = gctl_get_asciiparam(req, param);
1048129473Spjd		if (name == NULL) {
1049129473Spjd			gctl_error(req, "No 'arg%u' argument.", no);
1050129473Spjd			return;
1051129473Spjd		}
1052129473Spjd		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1053129473Spjd			name += strlen("/dev/");
1054129473Spjd		pp = g_provider_by_name(name);
1055129473Spjd		if (pp == NULL) {
1056129473Spjd			G_STRIPE_DEBUG(1, "Disk %s is invalid.", name);
1057129473Spjd			gctl_error(req, "Disk %s is invalid.", name);
1058129473Spjd			return;
1059129473Spjd		}
1060129473Spjd	}
1061129473Spjd
1062129473Spjd	gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1063129473Spjd	if (gp == NULL) {
1064132664Spjd		gctl_error(req, "Can't configure %s.", md.md_name);
1065129473Spjd		return;
1066129473Spjd	}
1067129473Spjd
1068129473Spjd	sc = gp->softc;
1069181463Sdes	sb = sbuf_new_auto();
1070129473Spjd	sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1071129473Spjd	for (attached = 0, no = 1; no < *nargs; no++) {
1072129473Spjd		snprintf(param, sizeof(param), "arg%u", no);
1073129473Spjd		name = gctl_get_asciiparam(req, param);
1074146109Spjd		if (name == NULL) {
1075146109Spjd			gctl_error(req, "No 'arg%u' argument.", no);
1076146109Spjd			continue;
1077146109Spjd		}
1078129473Spjd		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1079129473Spjd			name += strlen("/dev/");
1080129473Spjd		pp = g_provider_by_name(name);
1081129473Spjd		KASSERT(pp != NULL, ("Provider %s disappear?!", name));
1082129473Spjd		if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1083129473Spjd			G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1084129473Spjd			    no, pp->name, gp->name);
1085129473Spjd			sbuf_printf(sb, " %s", pp->name);
1086129473Spjd			continue;
1087129473Spjd		}
1088129473Spjd		attached++;
1089129473Spjd	}
1090129473Spjd	sbuf_finish(sb);
1091129473Spjd	if (md.md_all != attached) {
1092129473Spjd		g_stripe_destroy(gp->softc, 1);
1093129473Spjd		gctl_error(req, "%s", sbuf_data(sb));
1094129473Spjd	}
1095129473Spjd	sbuf_delete(sb);
1096129473Spjd}
1097129473Spjd
1098129473Spjdstatic struct g_stripe_softc *
1099129473Spjdg_stripe_find_device(struct g_class *mp, const char *name)
1100129473Spjd{
1101129473Spjd	struct g_stripe_softc *sc;
1102129473Spjd	struct g_geom *gp;
1103129473Spjd
1104129473Spjd	LIST_FOREACH(gp, &mp->geom, geom) {
1105129473Spjd		sc = gp->softc;
1106129473Spjd		if (sc == NULL)
1107129473Spjd			continue;
1108132664Spjd		if (strcmp(sc->sc_name, name) == 0)
1109129473Spjd			return (sc);
1110129473Spjd	}
1111129473Spjd	return (NULL);
1112129473Spjd}
1113129473Spjd
1114129473Spjdstatic void
1115129473Spjdg_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1116129473Spjd{
1117129473Spjd	struct g_stripe_softc *sc;
1118129473Spjd	int *force, *nargs, error;
1119129473Spjd	const char *name;
1120129473Spjd	char param[16];
1121129473Spjd	u_int i;
1122129473Spjd
1123129473Spjd	g_topology_assert();
1124129473Spjd
1125129473Spjd	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1126129473Spjd	if (nargs == NULL) {
1127129473Spjd		gctl_error(req, "No '%s' argument.", "nargs");
1128129473Spjd		return;
1129129473Spjd	}
1130129473Spjd	if (*nargs <= 0) {
1131129473Spjd		gctl_error(req, "Missing device(s).");
1132129473Spjd		return;
1133129473Spjd	}
1134129473Spjd	force = gctl_get_paraml(req, "force", sizeof(*force));
1135129473Spjd	if (force == NULL) {
1136129473Spjd		gctl_error(req, "No '%s' argument.", "force");
1137129473Spjd		return;
1138129473Spjd	}
1139129473Spjd
1140129473Spjd	for (i = 0; i < (u_int)*nargs; i++) {
1141129473Spjd		snprintf(param, sizeof(param), "arg%u", i);
1142129473Spjd		name = gctl_get_asciiparam(req, param);
1143129473Spjd		if (name == NULL) {
1144129473Spjd			gctl_error(req, "No 'arg%u' argument.", i);
1145129473Spjd			return;
1146129473Spjd		}
1147129473Spjd		sc = g_stripe_find_device(mp, name);
1148129473Spjd		if (sc == NULL) {
1149129473Spjd			gctl_error(req, "No such device: %s.", name);
1150129473Spjd			return;
1151129473Spjd		}
1152129473Spjd		error = g_stripe_destroy(sc, *force);
1153129473Spjd		if (error != 0) {
1154129473Spjd			gctl_error(req, "Cannot destroy device %s (error=%d).",
1155132664Spjd			    sc->sc_name, error);
1156129473Spjd			return;
1157129473Spjd		}
1158129473Spjd	}
1159129473Spjd}
1160129473Spjd
1161129473Spjdstatic void
1162129473Spjdg_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1163129473Spjd{
1164129473Spjd	uint32_t *version;
1165129473Spjd
1166129473Spjd	g_topology_assert();
1167129473Spjd
1168129473Spjd	version = gctl_get_paraml(req, "version", sizeof(*version));
1169129473Spjd	if (version == NULL) {
1170129473Spjd		gctl_error(req, "No '%s' argument.", "version");
1171129473Spjd		return;
1172129473Spjd	}
1173129473Spjd	if (*version != G_STRIPE_VERSION) {
1174129473Spjd		gctl_error(req, "Userland and kernel parts are out of sync.");
1175129473Spjd		return;
1176129473Spjd	}
1177129473Spjd
1178129473Spjd	if (strcmp(verb, "create") == 0) {
1179129473Spjd		g_stripe_ctl_create(req, mp);
1180129473Spjd		return;
1181131649Spjd	} else if (strcmp(verb, "destroy") == 0 ||
1182131649Spjd	    strcmp(verb, "stop") == 0) {
1183129473Spjd		g_stripe_ctl_destroy(req, mp);
1184129473Spjd		return;
1185129473Spjd	}
1186129473Spjd
1187129473Spjd	gctl_error(req, "Unknown verb.");
1188129473Spjd}
1189129473Spjd
1190129473Spjdstatic void
1191129473Spjdg_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1192129473Spjd    struct g_consumer *cp, struct g_provider *pp)
1193129473Spjd{
1194129473Spjd	struct g_stripe_softc *sc;
1195129473Spjd
1196129473Spjd	sc = gp->softc;
1197132665Spjd	if (sc == NULL)
1198129473Spjd		return;
1199132665Spjd	if (pp != NULL) {
1200132665Spjd		/* Nothing here. */
1201132665Spjd	} else if (cp != NULL) {
1202134292Spjd		sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1203134292Spjd		    (u_int)cp->index);
1204132665Spjd	} else {
1205132665Spjd		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1206132665Spjd		sbuf_printf(sb, "%s<Stripesize>%u</Stripesize>\n", indent,
1207132665Spjd		    (u_int)sc->sc_stripesize);
1208132665Spjd		sbuf_printf(sb, "%s<Type>", indent);
1209132665Spjd		switch (sc->sc_type) {
1210132665Spjd		case G_STRIPE_TYPE_AUTOMATIC:
1211132665Spjd			sbuf_printf(sb, "AUTOMATIC");
1212132665Spjd			break;
1213132665Spjd		case G_STRIPE_TYPE_MANUAL:
1214132665Spjd			sbuf_printf(sb, "MANUAL");
1215132665Spjd			break;
1216132665Spjd		default:
1217132665Spjd			sbuf_printf(sb, "UNKNOWN");
1218132665Spjd			break;
1219132665Spjd		}
1220132665Spjd		sbuf_printf(sb, "</Type>\n");
1221132665Spjd		sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1222132665Spjd		    indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1223132665Spjd		sbuf_printf(sb, "%s<State>", indent);
1224132665Spjd		if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1225132665Spjd			sbuf_printf(sb, "UP");
1226132665Spjd		else
1227132665Spjd			sbuf_printf(sb, "DOWN");
1228132665Spjd		sbuf_printf(sb, "</State>\n");
1229129473Spjd	}
1230129473Spjd}
1231129473Spjd
1232129473SpjdDECLARE_GEOM_CLASS(g_stripe_class, g_stripe);
1233