xref: /freebsd/sys/geom/stripe/g_stripe.c (revision d38c30c092828f4882ce13b08d0bd3fd6dc7afb5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/bio.h>
39 #include <sys/sbuf.h>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
42 #include <vm/uma.h>
43 #include <geom/geom.h>
44 #include <geom/geom_dbg.h>
45 #include <geom/stripe/g_stripe.h>
46 
47 FEATURE(geom_stripe, "GEOM striping support");
48 
49 static MALLOC_DEFINE(M_STRIPE, "stripe_data", "GEOM_STRIPE Data");
50 
51 static uma_zone_t g_stripe_zone;
52 
53 static int g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force);
54 static int g_stripe_destroy_geom(struct gctl_req *req, struct g_class *mp,
55     struct g_geom *gp);
56 
57 static g_taste_t g_stripe_taste;
58 static g_ctl_req_t g_stripe_config;
59 static g_dumpconf_t g_stripe_dumpconf;
60 static g_init_t g_stripe_init;
61 static g_fini_t g_stripe_fini;
62 
63 struct g_class g_stripe_class = {
64 	.name = G_STRIPE_CLASS_NAME,
65 	.version = G_VERSION,
66 	.ctlreq = g_stripe_config,
67 	.taste = g_stripe_taste,
68 	.destroy_geom = g_stripe_destroy_geom,
69 	.init = g_stripe_init,
70 	.fini = g_stripe_fini
71 };
72 
73 SYSCTL_DECL(_kern_geom);
74 static SYSCTL_NODE(_kern_geom, OID_AUTO, stripe, CTLFLAG_RW, 0,
75     "GEOM_STRIPE stuff");
76 static u_int g_stripe_debug = 0;
77 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, debug, CTLFLAG_RWTUN, &g_stripe_debug, 0,
78     "Debug level");
79 static int g_stripe_fast = 0;
80 static int
81 g_sysctl_stripe_fast(SYSCTL_HANDLER_ARGS)
82 {
83 	int error, fast;
84 
85 	fast = g_stripe_fast;
86 	error = sysctl_handle_int(oidp, &fast, 0, req);
87 	if (error == 0 && req->newptr != NULL)
88 		g_stripe_fast = fast;
89 	return (error);
90 }
91 SYSCTL_PROC(_kern_geom_stripe, OID_AUTO, fast, CTLTYPE_INT | CTLFLAG_RWTUN,
92     NULL, 0, g_sysctl_stripe_fast, "I", "Fast, but memory-consuming, mode");
93 static u_int g_stripe_maxmem = MAXPHYS * 100;
94 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, maxmem, CTLFLAG_RDTUN, &g_stripe_maxmem,
95     0, "Maximum memory that can be allocated in \"fast\" mode (in bytes)");
96 static u_int g_stripe_fast_failed = 0;
97 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, fast_failed, CTLFLAG_RD,
98     &g_stripe_fast_failed, 0, "How many times \"fast\" mode failed");
99 
100 /*
101  * Greatest Common Divisor.
102  */
103 static u_int
104 gcd(u_int a, u_int b)
105 {
106 	u_int c;
107 
108 	while (b != 0) {
109 		c = a;
110 		a = b;
111 		b = (c % b);
112 	}
113 	return (a);
114 }
115 
116 /*
117  * Least Common Multiple.
118  */
119 static u_int
120 lcm(u_int a, u_int b)
121 {
122 
123 	return ((a * b) / gcd(a, b));
124 }
125 
126 static void
127 g_stripe_init(struct g_class *mp __unused)
128 {
129 
130 	g_stripe_zone = uma_zcreate("g_stripe_zone", MAXPHYS, NULL, NULL,
131 	    NULL, NULL, 0, 0);
132 	g_stripe_maxmem -= g_stripe_maxmem % MAXPHYS;
133 	uma_zone_set_max(g_stripe_zone, g_stripe_maxmem / MAXPHYS);
134 }
135 
136 static void
137 g_stripe_fini(struct g_class *mp __unused)
138 {
139 
140 	uma_zdestroy(g_stripe_zone);
141 }
142 
143 /*
144  * Return the number of valid disks.
145  */
146 static u_int
147 g_stripe_nvalid(struct g_stripe_softc *sc)
148 {
149 	u_int i, no;
150 
151 	no = 0;
152 	for (i = 0; i < sc->sc_ndisks; i++) {
153 		if (sc->sc_disks[i] != NULL)
154 			no++;
155 	}
156 
157 	return (no);
158 }
159 
160 static void
161 g_stripe_remove_disk(struct g_consumer *cp)
162 {
163 	struct g_stripe_softc *sc;
164 
165 	g_topology_assert();
166 	KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
167 	sc = (struct g_stripe_softc *)cp->geom->softc;
168 	KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
169 
170 	if (cp->private == NULL) {
171 		G_STRIPE_DEBUG(0, "Disk %s removed from %s.",
172 		    cp->provider->name, sc->sc_name);
173 		cp->private = (void *)(uintptr_t)-1;
174 	}
175 
176 	if (sc->sc_provider != NULL) {
177 		G_STRIPE_DEBUG(0, "Device %s deactivated.",
178 		    sc->sc_provider->name);
179 		g_wither_provider(sc->sc_provider, ENXIO);
180 		sc->sc_provider = NULL;
181 	}
182 
183 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
184 		return;
185 	sc->sc_disks[cp->index] = NULL;
186 	cp->index = 0;
187 	g_detach(cp);
188 	g_destroy_consumer(cp);
189 	/* If there are no valid disks anymore, remove device. */
190 	if (LIST_EMPTY(&sc->sc_geom->consumer))
191 		g_stripe_destroy(sc, 1);
192 }
193 
194 static void
195 g_stripe_orphan(struct g_consumer *cp)
196 {
197 	struct g_stripe_softc *sc;
198 	struct g_geom *gp;
199 
200 	g_topology_assert();
201 	gp = cp->geom;
202 	sc = gp->softc;
203 	if (sc == NULL)
204 		return;
205 
206 	g_stripe_remove_disk(cp);
207 }
208 
209 static int
210 g_stripe_access(struct g_provider *pp, int dr, int dw, int de)
211 {
212 	struct g_consumer *cp1, *cp2, *tmp;
213 	struct g_stripe_softc *sc;
214 	struct g_geom *gp;
215 	int error;
216 
217 	g_topology_assert();
218 	gp = pp->geom;
219 	sc = gp->softc;
220 	KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
221 
222 	/* On first open, grab an extra "exclusive" bit */
223 	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
224 		de++;
225 	/* ... and let go of it on last close */
226 	if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
227 		de--;
228 
229 	LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) {
230 		error = g_access(cp1, dr, dw, de);
231 		if (error != 0)
232 			goto fail;
233 		if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 &&
234 		    cp1->private != NULL) {
235 			g_stripe_remove_disk(cp1); /* May destroy geom. */
236 		}
237 	}
238 	return (0);
239 
240 fail:
241 	LIST_FOREACH(cp2, &gp->consumer, consumer) {
242 		if (cp1 == cp2)
243 			break;
244 		g_access(cp2, -dr, -dw, -de);
245 	}
246 	return (error);
247 }
248 
249 static void
250 g_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset,
251     off_t length, int mode)
252 {
253 	off_t stripesize;
254 	size_t len;
255 
256 	stripesize = sc->sc_stripesize;
257 	len = (size_t)(stripesize - (offset & (stripesize - 1)));
258 	do {
259 		bcopy(src, dst, len);
260 		if (mode) {
261 			dst += len + stripesize * (sc->sc_ndisks - 1);
262 			src += len;
263 		} else {
264 			dst += len;
265 			src += len + stripesize * (sc->sc_ndisks - 1);
266 		}
267 		length -= len;
268 		KASSERT(length >= 0,
269 		    ("Length < 0 (stripesize=%ju, offset=%ju, length=%jd).",
270 		    (uintmax_t)stripesize, (uintmax_t)offset, (intmax_t)length));
271 		if (length > stripesize)
272 			len = stripesize;
273 		else
274 			len = length;
275 	} while (length > 0);
276 }
277 
278 static void
279 g_stripe_done(struct bio *bp)
280 {
281 	struct g_stripe_softc *sc;
282 	struct bio *pbp;
283 
284 	pbp = bp->bio_parent;
285 	sc = pbp->bio_to->geom->softc;
286 	if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) {
287 		g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset,
288 		    bp->bio_length, 1);
289 		bp->bio_data = bp->bio_caller1;
290 		bp->bio_caller1 = NULL;
291 	}
292 	mtx_lock(&sc->sc_lock);
293 	if (pbp->bio_error == 0)
294 		pbp->bio_error = bp->bio_error;
295 	pbp->bio_completed += bp->bio_completed;
296 	pbp->bio_inbed++;
297 	if (pbp->bio_children == pbp->bio_inbed) {
298 		mtx_unlock(&sc->sc_lock);
299 		if (pbp->bio_driver1 != NULL)
300 			uma_zfree(g_stripe_zone, pbp->bio_driver1);
301 		if (bp->bio_cmd == BIO_SPEEDUP)
302 			pbp->bio_completed = pbp->bio_length;
303 		g_io_deliver(pbp, pbp->bio_error);
304 	} else
305 		mtx_unlock(&sc->sc_lock);
306 	g_destroy_bio(bp);
307 }
308 
309 static int
310 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
311 {
312 	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
313 	struct g_stripe_softc *sc;
314 	char *addr, *data = NULL;
315 	struct bio *cbp;
316 	off_t stripesize;
317 	u_int nparts = 0;
318 	int error;
319 
320 	sc = bp->bio_to->geom->softc;
321 
322 	addr = bp->bio_data;
323 	stripesize = sc->sc_stripesize;
324 
325 	cbp = g_clone_bio(bp);
326 	if (cbp == NULL) {
327 		error = ENOMEM;
328 		goto failure;
329 	}
330 	TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
331 	nparts++;
332 	/*
333 	 * Fill in the component buf structure.
334 	 */
335 	cbp->bio_done = g_stripe_done;
336 	cbp->bio_offset = offset;
337 	cbp->bio_data = addr;
338 	cbp->bio_caller1 = NULL;
339 	cbp->bio_length = length;
340 	cbp->bio_caller2 = sc->sc_disks[no];
341 
342 	/* offset -= offset % stripesize; */
343 	offset -= offset & (stripesize - 1);
344 	addr += length;
345 	length = bp->bio_length - length;
346 	for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
347 		if (no > sc->sc_ndisks - 1) {
348 			no = 0;
349 			offset += stripesize;
350 		}
351 		if (nparts >= sc->sc_ndisks) {
352 			cbp = TAILQ_NEXT(cbp, bio_queue);
353 			if (cbp == NULL)
354 				cbp = TAILQ_FIRST(&queue);
355 			nparts++;
356 			/*
357 			 * Update bio structure.
358 			 */
359 			/*
360 			 * MIN() is in case when
361 			 * (bp->bio_length % sc->sc_stripesize) != 0.
362 			 */
363 			cbp->bio_length += MIN(stripesize, length);
364 			if (cbp->bio_caller1 == NULL) {
365 				cbp->bio_caller1 = cbp->bio_data;
366 				cbp->bio_data = NULL;
367 				if (data == NULL) {
368 					data = uma_zalloc(g_stripe_zone,
369 					    M_NOWAIT);
370 					if (data == NULL) {
371 						error = ENOMEM;
372 						goto failure;
373 					}
374 				}
375 			}
376 		} else {
377 			cbp = g_clone_bio(bp);
378 			if (cbp == NULL) {
379 				error = ENOMEM;
380 				goto failure;
381 			}
382 			TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
383 			nparts++;
384 			/*
385 			 * Fill in the component buf structure.
386 			 */
387 			cbp->bio_done = g_stripe_done;
388 			cbp->bio_offset = offset;
389 			cbp->bio_data = addr;
390 			cbp->bio_caller1 = NULL;
391 			/*
392 			 * MIN() is in case when
393 			 * (bp->bio_length % sc->sc_stripesize) != 0.
394 			 */
395 			cbp->bio_length = MIN(stripesize, length);
396 			cbp->bio_caller2 = sc->sc_disks[no];
397 		}
398 	}
399 	if (data != NULL)
400 		bp->bio_driver1 = data;
401 	/*
402 	 * Fire off all allocated requests!
403 	 */
404 	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
405 		struct g_consumer *cp;
406 
407 		TAILQ_REMOVE(&queue, cbp, bio_queue);
408 		cp = cbp->bio_caller2;
409 		cbp->bio_caller2 = NULL;
410 		cbp->bio_to = cp->provider;
411 		if (cbp->bio_caller1 != NULL) {
412 			cbp->bio_data = data;
413 			if (bp->bio_cmd == BIO_WRITE) {
414 				g_stripe_copy(sc, cbp->bio_caller1, data,
415 				    cbp->bio_offset, cbp->bio_length, 0);
416 			}
417 			data += cbp->bio_length;
418 		}
419 		G_STRIPE_LOGREQ(cbp, "Sending request.");
420 		g_io_request(cbp, cp);
421 	}
422 	return (0);
423 failure:
424 	if (data != NULL)
425 		uma_zfree(g_stripe_zone, data);
426 	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
427 		TAILQ_REMOVE(&queue, cbp, bio_queue);
428 		if (cbp->bio_caller1 != NULL) {
429 			cbp->bio_data = cbp->bio_caller1;
430 			cbp->bio_caller1 = NULL;
431 		}
432 		bp->bio_children--;
433 		g_destroy_bio(cbp);
434 	}
435 	return (error);
436 }
437 
438 static int
439 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
440 {
441 	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
442 	struct g_stripe_softc *sc;
443 	off_t stripesize;
444 	struct bio *cbp;
445 	char *addr;
446 	int error;
447 
448 	sc = bp->bio_to->geom->softc;
449 
450 	stripesize = sc->sc_stripesize;
451 
452 	cbp = g_clone_bio(bp);
453 	if (cbp == NULL) {
454 		error = ENOMEM;
455 		goto failure;
456 	}
457 	TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
458 	/*
459 	 * Fill in the component buf structure.
460 	 */
461 	if (bp->bio_length == length)
462 		cbp->bio_done = g_std_done;	/* Optimized lockless case. */
463 	else
464 		cbp->bio_done = g_stripe_done;
465 	cbp->bio_offset = offset;
466 	cbp->bio_length = length;
467 	if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
468 		bp->bio_ma_n = round_page(bp->bio_ma_offset +
469 		    bp->bio_length) / PAGE_SIZE;
470 		addr = NULL;
471 	} else
472 		addr = bp->bio_data;
473 	cbp->bio_caller2 = sc->sc_disks[no];
474 
475 	/* offset -= offset % stripesize; */
476 	offset -= offset & (stripesize - 1);
477 	if (bp->bio_cmd != BIO_DELETE)
478 		addr += length;
479 	length = bp->bio_length - length;
480 	for (no++; length > 0; no++, length -= stripesize) {
481 		if (no > sc->sc_ndisks - 1) {
482 			no = 0;
483 			offset += stripesize;
484 		}
485 		cbp = g_clone_bio(bp);
486 		if (cbp == NULL) {
487 			error = ENOMEM;
488 			goto failure;
489 		}
490 		TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
491 
492 		/*
493 		 * Fill in the component buf structure.
494 		 */
495 		cbp->bio_done = g_stripe_done;
496 		cbp->bio_offset = offset;
497 		/*
498 		 * MIN() is in case when
499 		 * (bp->bio_length % sc->sc_stripesize) != 0.
500 		 */
501 		cbp->bio_length = MIN(stripesize, length);
502 		if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
503 			cbp->bio_ma_offset += (uintptr_t)addr;
504 			cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
505 			cbp->bio_ma_offset %= PAGE_SIZE;
506 			cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
507 			    cbp->bio_length) / PAGE_SIZE;
508 		} else
509 			cbp->bio_data = addr;
510 
511 		cbp->bio_caller2 = sc->sc_disks[no];
512 
513 		if (bp->bio_cmd != BIO_DELETE)
514 			addr += stripesize;
515 	}
516 	/*
517 	 * Fire off all allocated requests!
518 	 */
519 	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
520 		struct g_consumer *cp;
521 
522 		TAILQ_REMOVE(&queue, cbp, bio_queue);
523 		cp = cbp->bio_caller2;
524 		cbp->bio_caller2 = NULL;
525 		cbp->bio_to = cp->provider;
526 		G_STRIPE_LOGREQ(cbp, "Sending request.");
527 		g_io_request(cbp, cp);
528 	}
529 	return (0);
530 failure:
531 	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
532 		TAILQ_REMOVE(&queue, cbp, bio_queue);
533 		bp->bio_children--;
534 		g_destroy_bio(cbp);
535 	}
536 	return (error);
537 }
538 
539 static void
540 g_stripe_pushdown(struct g_stripe_softc *sc, struct bio *bp)
541 {
542 	struct bio_queue_head queue;
543 	struct g_consumer *cp;
544 	struct bio *cbp;
545 	u_int no;
546 
547 	bioq_init(&queue);
548 	for (no = 0; no < sc->sc_ndisks; no++) {
549 		cbp = g_clone_bio(bp);
550 		if (cbp == NULL) {
551 			for (cbp = bioq_first(&queue); cbp != NULL;
552 			    cbp = bioq_first(&queue)) {
553 				bioq_remove(&queue, cbp);
554 				g_destroy_bio(cbp);
555 			}
556 			if (bp->bio_error == 0)
557 				bp->bio_error = ENOMEM;
558 			g_io_deliver(bp, bp->bio_error);
559 			return;
560 		}
561 		bioq_insert_tail(&queue, cbp);
562 		cbp->bio_done = g_stripe_done;
563 		cbp->bio_caller2 = sc->sc_disks[no];
564 		cbp->bio_to = sc->sc_disks[no]->provider;
565 	}
566 	for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
567 		bioq_remove(&queue, cbp);
568 		G_STRIPE_LOGREQ(cbp, "Sending request.");
569 		cp = cbp->bio_caller2;
570 		cbp->bio_caller2 = NULL;
571 		g_io_request(cbp, cp);
572 	}
573 }
574 
575 static void
576 g_stripe_start(struct bio *bp)
577 {
578 	off_t offset, start, length, nstripe, stripesize;
579 	struct g_stripe_softc *sc;
580 	u_int no;
581 	int error, fast = 0;
582 
583 	sc = bp->bio_to->geom->softc;
584 	/*
585 	 * If sc == NULL, provider's error should be set and g_stripe_start()
586 	 * should not be called at all.
587 	 */
588 	KASSERT(sc != NULL,
589 	    ("Provider's error should be set (error=%d)(device=%s).",
590 	    bp->bio_to->error, bp->bio_to->name));
591 
592 	G_STRIPE_LOGREQ(bp, "Request received.");
593 
594 	switch (bp->bio_cmd) {
595 	case BIO_READ:
596 	case BIO_WRITE:
597 	case BIO_DELETE:
598 		break;
599 	case BIO_SPEEDUP:
600 	case BIO_FLUSH:
601 		g_stripe_pushdown(sc, bp);
602 		return;
603 	case BIO_GETATTR:
604 		/* To which provider it should be delivered? */
605 	default:
606 		g_io_deliver(bp, EOPNOTSUPP);
607 		return;
608 	}
609 
610 	stripesize = sc->sc_stripesize;
611 
612 	/*
613 	 * Calculations are quite messy, but fast I hope.
614 	 */
615 
616 	/* Stripe number. */
617 	/* nstripe = bp->bio_offset / stripesize; */
618 	nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
619 	/* Disk number. */
620 	no = nstripe % sc->sc_ndisks;
621 	/* Start position in stripe. */
622 	/* start = bp->bio_offset % stripesize; */
623 	start = bp->bio_offset & (stripesize - 1);
624 	/* Start position in disk. */
625 	/* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
626 	offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
627 	/* Length of data to operate. */
628 	length = MIN(bp->bio_length, stripesize - start);
629 
630 	/*
631 	 * Do use "fast" mode when:
632 	 * 1. "Fast" mode is ON.
633 	 * and
634 	 * 2. Request size is less than or equal to MAXPHYS,
635 	 *    which should always be true.
636 	 * and
637 	 * 3. Request size is bigger than stripesize * ndisks. If it isn't,
638 	 *    there will be no need to send more than one I/O request to
639 	 *    a provider, so there is nothing to optmize.
640 	 * and
641 	 * 4. Request is not unmapped.
642 	 * and
643 	 * 5. It is not a BIO_DELETE.
644 	 */
645 	if (g_stripe_fast && bp->bio_length <= MAXPHYS &&
646 	    bp->bio_length >= stripesize * sc->sc_ndisks &&
647 	    (bp->bio_flags & BIO_UNMAPPED) == 0 &&
648 	    bp->bio_cmd != BIO_DELETE) {
649 		fast = 1;
650 	}
651 	error = 0;
652 	if (fast) {
653 		error = g_stripe_start_fast(bp, no, offset, length);
654 		if (error != 0)
655 			g_stripe_fast_failed++;
656 	}
657 	/*
658 	 * Do use "economic" when:
659 	 * 1. "Economic" mode is ON.
660 	 * or
661 	 * 2. "Fast" mode failed. It can only fail if there is no memory.
662 	 */
663 	if (!fast || error != 0)
664 		error = g_stripe_start_economic(bp, no, offset, length);
665 	if (error != 0) {
666 		if (bp->bio_error == 0)
667 			bp->bio_error = error;
668 		g_io_deliver(bp, bp->bio_error);
669 	}
670 }
671 
672 static void
673 g_stripe_check_and_run(struct g_stripe_softc *sc)
674 {
675 	struct g_provider *dp;
676 	off_t mediasize, ms;
677 	u_int no, sectorsize = 0;
678 
679 	g_topology_assert();
680 	if (g_stripe_nvalid(sc) != sc->sc_ndisks)
681 		return;
682 
683 	sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
684 	    sc->sc_name);
685 	sc->sc_provider->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
686 	if (g_stripe_fast == 0)
687 		sc->sc_provider->flags |= G_PF_ACCEPT_UNMAPPED;
688 	/*
689 	 * Find the smallest disk.
690 	 */
691 	mediasize = sc->sc_disks[0]->provider->mediasize;
692 	if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
693 		mediasize -= sc->sc_disks[0]->provider->sectorsize;
694 	mediasize -= mediasize % sc->sc_stripesize;
695 	sectorsize = sc->sc_disks[0]->provider->sectorsize;
696 	for (no = 1; no < sc->sc_ndisks; no++) {
697 		dp = sc->sc_disks[no]->provider;
698 		ms = dp->mediasize;
699 		if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
700 			ms -= dp->sectorsize;
701 		ms -= ms % sc->sc_stripesize;
702 		if (ms < mediasize)
703 			mediasize = ms;
704 		sectorsize = lcm(sectorsize, dp->sectorsize);
705 
706 		/* A provider underneath us doesn't support unmapped */
707 		if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
708 			G_STRIPE_DEBUG(1, "Cancelling unmapped "
709 			    "because of %s.", dp->name);
710 			sc->sc_provider->flags &= ~G_PF_ACCEPT_UNMAPPED;
711 		}
712 	}
713 	sc->sc_provider->sectorsize = sectorsize;
714 	sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
715 	sc->sc_provider->stripesize = sc->sc_stripesize;
716 	sc->sc_provider->stripeoffset = 0;
717 	g_error_provider(sc->sc_provider, 0);
718 
719 	G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
720 }
721 
722 static int
723 g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
724 {
725 	struct g_provider *pp;
726 	u_char *buf;
727 	int error;
728 
729 	g_topology_assert();
730 
731 	error = g_access(cp, 1, 0, 0);
732 	if (error != 0)
733 		return (error);
734 	pp = cp->provider;
735 	g_topology_unlock();
736 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
737 	    &error);
738 	g_topology_lock();
739 	g_access(cp, -1, 0, 0);
740 	if (buf == NULL)
741 		return (error);
742 
743 	/* Decode metadata. */
744 	stripe_metadata_decode(buf, md);
745 	g_free(buf);
746 
747 	return (0);
748 }
749 
750 /*
751  * Add disk to given device.
752  */
753 static int
754 g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
755 {
756 	struct g_consumer *cp, *fcp;
757 	struct g_geom *gp;
758 	int error;
759 
760 	g_topology_assert();
761 	/* Metadata corrupted? */
762 	if (no >= sc->sc_ndisks)
763 		return (EINVAL);
764 
765 	/* Check if disk is not already attached. */
766 	if (sc->sc_disks[no] != NULL)
767 		return (EEXIST);
768 
769 	gp = sc->sc_geom;
770 	fcp = LIST_FIRST(&gp->consumer);
771 
772 	cp = g_new_consumer(gp);
773 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
774 	cp->private = NULL;
775 	cp->index = no;
776 	error = g_attach(cp, pp);
777 	if (error != 0) {
778 		g_destroy_consumer(cp);
779 		return (error);
780 	}
781 
782 	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
783 		error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
784 		if (error != 0) {
785 			g_detach(cp);
786 			g_destroy_consumer(cp);
787 			return (error);
788 		}
789 	}
790 	if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
791 		struct g_stripe_metadata md;
792 
793 		/* Reread metadata. */
794 		error = g_stripe_read_metadata(cp, &md);
795 		if (error != 0)
796 			goto fail;
797 
798 		if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
799 		    strcmp(md.md_name, sc->sc_name) != 0 ||
800 		    md.md_id != sc->sc_id) {
801 			G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
802 			goto fail;
803 		}
804 	}
805 
806 	sc->sc_disks[no] = cp;
807 	G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
808 	g_stripe_check_and_run(sc);
809 
810 	return (0);
811 fail:
812 	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
813 		g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
814 	g_detach(cp);
815 	g_destroy_consumer(cp);
816 	return (error);
817 }
818 
819 static struct g_geom *
820 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
821     u_int type)
822 {
823 	struct g_stripe_softc *sc;
824 	struct g_geom *gp;
825 	u_int no;
826 
827 	g_topology_assert();
828 	G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
829 	    md->md_id);
830 
831 	/* Two disks is minimum. */
832 	if (md->md_all < 2) {
833 		G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
834 		return (NULL);
835 	}
836 #if 0
837 	/* Stripe size have to be grater than or equal to sector size. */
838 	if (md->md_stripesize < sectorsize) {
839 		G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
840 		return (NULL);
841 	}
842 #endif
843 	/* Stripe size have to be power of 2. */
844 	if (!powerof2(md->md_stripesize)) {
845 		G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
846 		return (NULL);
847 	}
848 
849 	/* Check for duplicate unit */
850 	LIST_FOREACH(gp, &mp->geom, geom) {
851 		sc = gp->softc;
852 		if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
853 			G_STRIPE_DEBUG(0, "Device %s already configured.",
854 			    sc->sc_name);
855 			return (NULL);
856 		}
857 	}
858 	gp = g_new_geomf(mp, "%s", md->md_name);
859 	sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
860 	gp->start = g_stripe_start;
861 	gp->spoiled = g_stripe_orphan;
862 	gp->orphan = g_stripe_orphan;
863 	gp->access = g_stripe_access;
864 	gp->dumpconf = g_stripe_dumpconf;
865 
866 	sc->sc_id = md->md_id;
867 	sc->sc_stripesize = md->md_stripesize;
868 	sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
869 	sc->sc_ndisks = md->md_all;
870 	sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
871 	    M_STRIPE, M_WAITOK | M_ZERO);
872 	for (no = 0; no < sc->sc_ndisks; no++)
873 		sc->sc_disks[no] = NULL;
874 	sc->sc_type = type;
875 	mtx_init(&sc->sc_lock, "gstripe lock", NULL, MTX_DEF);
876 
877 	gp->softc = sc;
878 	sc->sc_geom = gp;
879 	sc->sc_provider = NULL;
880 
881 	G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
882 
883 	return (gp);
884 }
885 
886 static int
887 g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
888 {
889 	struct g_provider *pp;
890 	struct g_consumer *cp, *cp1;
891 	struct g_geom *gp;
892 
893 	g_topology_assert();
894 
895 	if (sc == NULL)
896 		return (ENXIO);
897 
898 	pp = sc->sc_provider;
899 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
900 		if (force) {
901 			G_STRIPE_DEBUG(0, "Device %s is still open, so it "
902 			    "can't be definitely removed.", pp->name);
903 		} else {
904 			G_STRIPE_DEBUG(1,
905 			    "Device %s is still open (r%dw%de%d).", pp->name,
906 			    pp->acr, pp->acw, pp->ace);
907 			return (EBUSY);
908 		}
909 	}
910 
911 	gp = sc->sc_geom;
912 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
913 		g_stripe_remove_disk(cp);
914 		if (cp1 == NULL)
915 			return (0);	/* Recursion happened. */
916 	}
917 	if (!LIST_EMPTY(&gp->consumer))
918 		return (EINPROGRESS);
919 
920 	gp->softc = NULL;
921 	KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
922 	    gp->name));
923 	free(sc->sc_disks, M_STRIPE);
924 	mtx_destroy(&sc->sc_lock);
925 	free(sc, M_STRIPE);
926 	G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
927 	g_wither_geom(gp, ENXIO);
928 	return (0);
929 }
930 
931 static int
932 g_stripe_destroy_geom(struct gctl_req *req __unused,
933     struct g_class *mp __unused, struct g_geom *gp)
934 {
935 	struct g_stripe_softc *sc;
936 
937 	sc = gp->softc;
938 	return (g_stripe_destroy(sc, 0));
939 }
940 
941 static struct g_geom *
942 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
943 {
944 	struct g_stripe_metadata md;
945 	struct g_stripe_softc *sc;
946 	struct g_consumer *cp;
947 	struct g_geom *gp;
948 	int error;
949 
950 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
951 	g_topology_assert();
952 
953 	/* Skip providers that are already open for writing. */
954 	if (pp->acw > 0)
955 		return (NULL);
956 
957 	G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
958 
959 	gp = g_new_geomf(mp, "stripe:taste");
960 	gp->start = g_stripe_start;
961 	gp->access = g_stripe_access;
962 	gp->orphan = g_stripe_orphan;
963 	cp = g_new_consumer(gp);
964 	g_attach(cp, pp);
965 	error = g_stripe_read_metadata(cp, &md);
966 	g_detach(cp);
967 	g_destroy_consumer(cp);
968 	g_destroy_geom(gp);
969 	if (error != 0)
970 		return (NULL);
971 	gp = NULL;
972 
973 	if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
974 		return (NULL);
975 	if (md.md_version > G_STRIPE_VERSION) {
976 		printf("geom_stripe.ko module is too old to handle %s.\n",
977 		    pp->name);
978 		return (NULL);
979 	}
980 	/*
981 	 * Backward compatibility:
982 	 */
983 	/* There was no md_provider field in earlier versions of metadata. */
984 	if (md.md_version < 2)
985 		bzero(md.md_provider, sizeof(md.md_provider));
986 	/* There was no md_provsize field in earlier versions of metadata. */
987 	if (md.md_version < 3)
988 		md.md_provsize = pp->mediasize;
989 
990 	if (md.md_provider[0] != '\0' &&
991 	    !g_compare_names(md.md_provider, pp->name))
992 		return (NULL);
993 	if (md.md_provsize != pp->mediasize)
994 		return (NULL);
995 
996 	/*
997 	 * Let's check if device already exists.
998 	 */
999 	sc = NULL;
1000 	LIST_FOREACH(gp, &mp->geom, geom) {
1001 		sc = gp->softc;
1002 		if (sc == NULL)
1003 			continue;
1004 		if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
1005 			continue;
1006 		if (strcmp(md.md_name, sc->sc_name) != 0)
1007 			continue;
1008 		if (md.md_id != sc->sc_id)
1009 			continue;
1010 		break;
1011 	}
1012 	if (gp != NULL) {
1013 		G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1014 		error = g_stripe_add_disk(sc, pp, md.md_no);
1015 		if (error != 0) {
1016 			G_STRIPE_DEBUG(0,
1017 			    "Cannot add disk %s to %s (error=%d).", pp->name,
1018 			    gp->name, error);
1019 			return (NULL);
1020 		}
1021 	} else {
1022 		gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
1023 		if (gp == NULL) {
1024 			G_STRIPE_DEBUG(0, "Cannot create device %s.",
1025 			    md.md_name);
1026 			return (NULL);
1027 		}
1028 		sc = gp->softc;
1029 		G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1030 		error = g_stripe_add_disk(sc, pp, md.md_no);
1031 		if (error != 0) {
1032 			G_STRIPE_DEBUG(0,
1033 			    "Cannot add disk %s to %s (error=%d).", pp->name,
1034 			    gp->name, error);
1035 			g_stripe_destroy(sc, 1);
1036 			return (NULL);
1037 		}
1038 	}
1039 
1040 	return (gp);
1041 }
1042 
1043 static void
1044 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1045 {
1046 	u_int attached, no;
1047 	struct g_stripe_metadata md;
1048 	struct g_provider *pp;
1049 	struct g_stripe_softc *sc;
1050 	struct g_geom *gp;
1051 	struct sbuf *sb;
1052 	off_t *stripesize;
1053 	const char *name;
1054 	char param[16];
1055 	int *nargs;
1056 
1057 	g_topology_assert();
1058 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1059 	if (nargs == NULL) {
1060 		gctl_error(req, "No '%s' argument.", "nargs");
1061 		return;
1062 	}
1063 	if (*nargs <= 2) {
1064 		gctl_error(req, "Too few arguments.");
1065 		return;
1066 	}
1067 
1068 	strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1069 	md.md_version = G_STRIPE_VERSION;
1070 	name = gctl_get_asciiparam(req, "arg0");
1071 	if (name == NULL) {
1072 		gctl_error(req, "No 'arg%u' argument.", 0);
1073 		return;
1074 	}
1075 	strlcpy(md.md_name, name, sizeof(md.md_name));
1076 	md.md_id = arc4random();
1077 	md.md_no = 0;
1078 	md.md_all = *nargs - 1;
1079 	stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1080 	if (stripesize == NULL) {
1081 		gctl_error(req, "No '%s' argument.", "stripesize");
1082 		return;
1083 	}
1084 	md.md_stripesize = (uint32_t)*stripesize;
1085 	bzero(md.md_provider, sizeof(md.md_provider));
1086 	/* This field is not important here. */
1087 	md.md_provsize = 0;
1088 
1089 	/* Check all providers are valid */
1090 	for (no = 1; no < *nargs; no++) {
1091 		snprintf(param, sizeof(param), "arg%u", no);
1092 		name = gctl_get_asciiparam(req, param);
1093 		if (name == NULL) {
1094 			gctl_error(req, "No 'arg%u' argument.", no);
1095 			return;
1096 		}
1097 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1098 			name += strlen("/dev/");
1099 		pp = g_provider_by_name(name);
1100 		if (pp == NULL) {
1101 			G_STRIPE_DEBUG(1, "Disk %s is invalid.", name);
1102 			gctl_error(req, "Disk %s is invalid.", name);
1103 			return;
1104 		}
1105 	}
1106 
1107 	gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1108 	if (gp == NULL) {
1109 		gctl_error(req, "Can't configure %s.", md.md_name);
1110 		return;
1111 	}
1112 
1113 	sc = gp->softc;
1114 	sb = sbuf_new_auto();
1115 	sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1116 	for (attached = 0, no = 1; no < *nargs; no++) {
1117 		snprintf(param, sizeof(param), "arg%u", no);
1118 		name = gctl_get_asciiparam(req, param);
1119 		if (name == NULL) {
1120 			gctl_error(req, "No 'arg%u' argument.", no);
1121 			continue;
1122 		}
1123 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1124 			name += strlen("/dev/");
1125 		pp = g_provider_by_name(name);
1126 		KASSERT(pp != NULL, ("Provider %s disappear?!", name));
1127 		if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1128 			G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1129 			    no, pp->name, gp->name);
1130 			sbuf_printf(sb, " %s", pp->name);
1131 			continue;
1132 		}
1133 		attached++;
1134 	}
1135 	sbuf_finish(sb);
1136 	if (md.md_all != attached) {
1137 		g_stripe_destroy(gp->softc, 1);
1138 		gctl_error(req, "%s", sbuf_data(sb));
1139 	}
1140 	sbuf_delete(sb);
1141 }
1142 
1143 static struct g_stripe_softc *
1144 g_stripe_find_device(struct g_class *mp, const char *name)
1145 {
1146 	struct g_stripe_softc *sc;
1147 	struct g_geom *gp;
1148 
1149 	LIST_FOREACH(gp, &mp->geom, geom) {
1150 		sc = gp->softc;
1151 		if (sc == NULL)
1152 			continue;
1153 		if (strcmp(sc->sc_name, name) == 0)
1154 			return (sc);
1155 	}
1156 	return (NULL);
1157 }
1158 
1159 static void
1160 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1161 {
1162 	struct g_stripe_softc *sc;
1163 	int *force, *nargs, error;
1164 	const char *name;
1165 	char param[16];
1166 	u_int i;
1167 
1168 	g_topology_assert();
1169 
1170 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1171 	if (nargs == NULL) {
1172 		gctl_error(req, "No '%s' argument.", "nargs");
1173 		return;
1174 	}
1175 	if (*nargs <= 0) {
1176 		gctl_error(req, "Missing device(s).");
1177 		return;
1178 	}
1179 	force = gctl_get_paraml(req, "force", sizeof(*force));
1180 	if (force == NULL) {
1181 		gctl_error(req, "No '%s' argument.", "force");
1182 		return;
1183 	}
1184 
1185 	for (i = 0; i < (u_int)*nargs; i++) {
1186 		snprintf(param, sizeof(param), "arg%u", i);
1187 		name = gctl_get_asciiparam(req, param);
1188 		if (name == NULL) {
1189 			gctl_error(req, "No 'arg%u' argument.", i);
1190 			return;
1191 		}
1192 		sc = g_stripe_find_device(mp, name);
1193 		if (sc == NULL) {
1194 			gctl_error(req, "No such device: %s.", name);
1195 			return;
1196 		}
1197 		error = g_stripe_destroy(sc, *force);
1198 		if (error != 0) {
1199 			gctl_error(req, "Cannot destroy device %s (error=%d).",
1200 			    sc->sc_name, error);
1201 			return;
1202 		}
1203 	}
1204 }
1205 
1206 static void
1207 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1208 {
1209 	uint32_t *version;
1210 
1211 	g_topology_assert();
1212 
1213 	version = gctl_get_paraml(req, "version", sizeof(*version));
1214 	if (version == NULL) {
1215 		gctl_error(req, "No '%s' argument.", "version");
1216 		return;
1217 	}
1218 	if (*version != G_STRIPE_VERSION) {
1219 		gctl_error(req, "Userland and kernel parts are out of sync.");
1220 		return;
1221 	}
1222 
1223 	if (strcmp(verb, "create") == 0) {
1224 		g_stripe_ctl_create(req, mp);
1225 		return;
1226 	} else if (strcmp(verb, "destroy") == 0 ||
1227 	    strcmp(verb, "stop") == 0) {
1228 		g_stripe_ctl_destroy(req, mp);
1229 		return;
1230 	}
1231 
1232 	gctl_error(req, "Unknown verb.");
1233 }
1234 
1235 static void
1236 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1237     struct g_consumer *cp, struct g_provider *pp)
1238 {
1239 	struct g_stripe_softc *sc;
1240 
1241 	sc = gp->softc;
1242 	if (sc == NULL)
1243 		return;
1244 	if (pp != NULL) {
1245 		/* Nothing here. */
1246 	} else if (cp != NULL) {
1247 		sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1248 		    (u_int)cp->index);
1249 	} else {
1250 		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1251 		sbuf_printf(sb, "%s<Stripesize>%ju</Stripesize>\n", indent,
1252 		    (uintmax_t)sc->sc_stripesize);
1253 		sbuf_printf(sb, "%s<Type>", indent);
1254 		switch (sc->sc_type) {
1255 		case G_STRIPE_TYPE_AUTOMATIC:
1256 			sbuf_cat(sb, "AUTOMATIC");
1257 			break;
1258 		case G_STRIPE_TYPE_MANUAL:
1259 			sbuf_cat(sb, "MANUAL");
1260 			break;
1261 		default:
1262 			sbuf_cat(sb, "UNKNOWN");
1263 			break;
1264 		}
1265 		sbuf_cat(sb, "</Type>\n");
1266 		sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1267 		    indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1268 		sbuf_printf(sb, "%s<State>", indent);
1269 		if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1270 			sbuf_cat(sb, "UP");
1271 		else
1272 			sbuf_cat(sb, "DOWN");
1273 		sbuf_cat(sb, "</State>\n");
1274 	}
1275 }
1276 
1277 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);
1278 MODULE_VERSION(geom_stripe, 0);
1279