xref: /freebsd/sys/geom/stripe/g_stripe.c (revision 206b73d0)
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 		g_io_deliver(pbp, pbp->bio_error);
302 	} else
303 		mtx_unlock(&sc->sc_lock);
304 	g_destroy_bio(bp);
305 }
306 
307 static int
308 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
309 {
310 	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
311 	struct g_stripe_softc *sc;
312 	char *addr, *data = NULL;
313 	struct bio *cbp;
314 	off_t stripesize;
315 	u_int nparts = 0;
316 	int error;
317 
318 	sc = bp->bio_to->geom->softc;
319 
320 	addr = bp->bio_data;
321 	stripesize = sc->sc_stripesize;
322 
323 	cbp = g_clone_bio(bp);
324 	if (cbp == NULL) {
325 		error = ENOMEM;
326 		goto failure;
327 	}
328 	TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
329 	nparts++;
330 	/*
331 	 * Fill in the component buf structure.
332 	 */
333 	cbp->bio_done = g_stripe_done;
334 	cbp->bio_offset = offset;
335 	cbp->bio_data = addr;
336 	cbp->bio_caller1 = NULL;
337 	cbp->bio_length = length;
338 	cbp->bio_caller2 = sc->sc_disks[no];
339 
340 	/* offset -= offset % stripesize; */
341 	offset -= offset & (stripesize - 1);
342 	addr += length;
343 	length = bp->bio_length - length;
344 	for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
345 		if (no > sc->sc_ndisks - 1) {
346 			no = 0;
347 			offset += stripesize;
348 		}
349 		if (nparts >= sc->sc_ndisks) {
350 			cbp = TAILQ_NEXT(cbp, bio_queue);
351 			if (cbp == NULL)
352 				cbp = TAILQ_FIRST(&queue);
353 			nparts++;
354 			/*
355 			 * Update bio structure.
356 			 */
357 			/*
358 			 * MIN() is in case when
359 			 * (bp->bio_length % sc->sc_stripesize) != 0.
360 			 */
361 			cbp->bio_length += MIN(stripesize, length);
362 			if (cbp->bio_caller1 == NULL) {
363 				cbp->bio_caller1 = cbp->bio_data;
364 				cbp->bio_data = NULL;
365 				if (data == NULL) {
366 					data = uma_zalloc(g_stripe_zone,
367 					    M_NOWAIT);
368 					if (data == NULL) {
369 						error = ENOMEM;
370 						goto failure;
371 					}
372 				}
373 			}
374 		} else {
375 			cbp = g_clone_bio(bp);
376 			if (cbp == NULL) {
377 				error = ENOMEM;
378 				goto failure;
379 			}
380 			TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
381 			nparts++;
382 			/*
383 			 * Fill in the component buf structure.
384 			 */
385 			cbp->bio_done = g_stripe_done;
386 			cbp->bio_offset = offset;
387 			cbp->bio_data = addr;
388 			cbp->bio_caller1 = NULL;
389 			/*
390 			 * MIN() is in case when
391 			 * (bp->bio_length % sc->sc_stripesize) != 0.
392 			 */
393 			cbp->bio_length = MIN(stripesize, length);
394 			cbp->bio_caller2 = sc->sc_disks[no];
395 		}
396 	}
397 	if (data != NULL)
398 		bp->bio_driver1 = data;
399 	/*
400 	 * Fire off all allocated requests!
401 	 */
402 	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
403 		struct g_consumer *cp;
404 
405 		TAILQ_REMOVE(&queue, cbp, bio_queue);
406 		cp = cbp->bio_caller2;
407 		cbp->bio_caller2 = NULL;
408 		cbp->bio_to = cp->provider;
409 		if (cbp->bio_caller1 != NULL) {
410 			cbp->bio_data = data;
411 			if (bp->bio_cmd == BIO_WRITE) {
412 				g_stripe_copy(sc, cbp->bio_caller1, data,
413 				    cbp->bio_offset, cbp->bio_length, 0);
414 			}
415 			data += cbp->bio_length;
416 		}
417 		G_STRIPE_LOGREQ(cbp, "Sending request.");
418 		g_io_request(cbp, cp);
419 	}
420 	return (0);
421 failure:
422 	if (data != NULL)
423 		uma_zfree(g_stripe_zone, data);
424 	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
425 		TAILQ_REMOVE(&queue, cbp, bio_queue);
426 		if (cbp->bio_caller1 != NULL) {
427 			cbp->bio_data = cbp->bio_caller1;
428 			cbp->bio_caller1 = NULL;
429 		}
430 		bp->bio_children--;
431 		g_destroy_bio(cbp);
432 	}
433 	return (error);
434 }
435 
436 static int
437 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
438 {
439 	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
440 	struct g_stripe_softc *sc;
441 	off_t stripesize;
442 	struct bio *cbp;
443 	char *addr;
444 	int error;
445 
446 	sc = bp->bio_to->geom->softc;
447 
448 	stripesize = sc->sc_stripesize;
449 
450 	cbp = g_clone_bio(bp);
451 	if (cbp == NULL) {
452 		error = ENOMEM;
453 		goto failure;
454 	}
455 	TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
456 	/*
457 	 * Fill in the component buf structure.
458 	 */
459 	if (bp->bio_length == length)
460 		cbp->bio_done = g_std_done;	/* Optimized lockless case. */
461 	else
462 		cbp->bio_done = g_stripe_done;
463 	cbp->bio_offset = offset;
464 	cbp->bio_length = length;
465 	if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
466 		bp->bio_ma_n = round_page(bp->bio_ma_offset +
467 		    bp->bio_length) / PAGE_SIZE;
468 		addr = NULL;
469 	} else
470 		addr = bp->bio_data;
471 	cbp->bio_caller2 = sc->sc_disks[no];
472 
473 	/* offset -= offset % stripesize; */
474 	offset -= offset & (stripesize - 1);
475 	if (bp->bio_cmd != BIO_DELETE)
476 		addr += length;
477 	length = bp->bio_length - length;
478 	for (no++; length > 0; no++, length -= stripesize) {
479 		if (no > sc->sc_ndisks - 1) {
480 			no = 0;
481 			offset += stripesize;
482 		}
483 		cbp = g_clone_bio(bp);
484 		if (cbp == NULL) {
485 			error = ENOMEM;
486 			goto failure;
487 		}
488 		TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
489 
490 		/*
491 		 * Fill in the component buf structure.
492 		 */
493 		cbp->bio_done = g_stripe_done;
494 		cbp->bio_offset = offset;
495 		/*
496 		 * MIN() is in case when
497 		 * (bp->bio_length % sc->sc_stripesize) != 0.
498 		 */
499 		cbp->bio_length = MIN(stripesize, length);
500 		if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
501 			cbp->bio_ma_offset += (uintptr_t)addr;
502 			cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
503 			cbp->bio_ma_offset %= PAGE_SIZE;
504 			cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
505 			    cbp->bio_length) / PAGE_SIZE;
506 		} else
507 			cbp->bio_data = addr;
508 
509 		cbp->bio_caller2 = sc->sc_disks[no];
510 
511 		if (bp->bio_cmd != BIO_DELETE)
512 			addr += stripesize;
513 	}
514 	/*
515 	 * Fire off all allocated requests!
516 	 */
517 	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
518 		struct g_consumer *cp;
519 
520 		TAILQ_REMOVE(&queue, cbp, bio_queue);
521 		cp = cbp->bio_caller2;
522 		cbp->bio_caller2 = NULL;
523 		cbp->bio_to = cp->provider;
524 		G_STRIPE_LOGREQ(cbp, "Sending request.");
525 		g_io_request(cbp, cp);
526 	}
527 	return (0);
528 failure:
529 	while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
530 		TAILQ_REMOVE(&queue, cbp, bio_queue);
531 		bp->bio_children--;
532 		g_destroy_bio(cbp);
533 	}
534 	return (error);
535 }
536 
537 static void
538 g_stripe_flush(struct g_stripe_softc *sc, struct bio *bp)
539 {
540 	struct bio_queue_head queue;
541 	struct g_consumer *cp;
542 	struct bio *cbp;
543 	u_int no;
544 
545 	bioq_init(&queue);
546 	for (no = 0; no < sc->sc_ndisks; no++) {
547 		cbp = g_clone_bio(bp);
548 		if (cbp == NULL) {
549 			for (cbp = bioq_first(&queue); cbp != NULL;
550 			    cbp = bioq_first(&queue)) {
551 				bioq_remove(&queue, cbp);
552 				g_destroy_bio(cbp);
553 			}
554 			if (bp->bio_error == 0)
555 				bp->bio_error = ENOMEM;
556 			g_io_deliver(bp, bp->bio_error);
557 			return;
558 		}
559 		bioq_insert_tail(&queue, cbp);
560 		cbp->bio_done = g_stripe_done;
561 		cbp->bio_caller2 = sc->sc_disks[no];
562 		cbp->bio_to = sc->sc_disks[no]->provider;
563 	}
564 	for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
565 		bioq_remove(&queue, cbp);
566 		G_STRIPE_LOGREQ(cbp, "Sending request.");
567 		cp = cbp->bio_caller2;
568 		cbp->bio_caller2 = NULL;
569 		g_io_request(cbp, cp);
570 	}
571 }
572 
573 static void
574 g_stripe_start(struct bio *bp)
575 {
576 	off_t offset, start, length, nstripe, stripesize;
577 	struct g_stripe_softc *sc;
578 	u_int no;
579 	int error, fast = 0;
580 
581 	sc = bp->bio_to->geom->softc;
582 	/*
583 	 * If sc == NULL, provider's error should be set and g_stripe_start()
584 	 * should not be called at all.
585 	 */
586 	KASSERT(sc != NULL,
587 	    ("Provider's error should be set (error=%d)(device=%s).",
588 	    bp->bio_to->error, bp->bio_to->name));
589 
590 	G_STRIPE_LOGREQ(bp, "Request received.");
591 
592 	switch (bp->bio_cmd) {
593 	case BIO_READ:
594 	case BIO_WRITE:
595 	case BIO_DELETE:
596 		break;
597 	case BIO_FLUSH:
598 		g_stripe_flush(sc, bp);
599 		return;
600 	case BIO_GETATTR:
601 		/* To which provider it should be delivered? */
602 	default:
603 		g_io_deliver(bp, EOPNOTSUPP);
604 		return;
605 	}
606 
607 	stripesize = sc->sc_stripesize;
608 
609 	/*
610 	 * Calculations are quite messy, but fast I hope.
611 	 */
612 
613 	/* Stripe number. */
614 	/* nstripe = bp->bio_offset / stripesize; */
615 	nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
616 	/* Disk number. */
617 	no = nstripe % sc->sc_ndisks;
618 	/* Start position in stripe. */
619 	/* start = bp->bio_offset % stripesize; */
620 	start = bp->bio_offset & (stripesize - 1);
621 	/* Start position in disk. */
622 	/* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
623 	offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
624 	/* Length of data to operate. */
625 	length = MIN(bp->bio_length, stripesize - start);
626 
627 	/*
628 	 * Do use "fast" mode when:
629 	 * 1. "Fast" mode is ON.
630 	 * and
631 	 * 2. Request size is less than or equal to MAXPHYS,
632 	 *    which should always be true.
633 	 * and
634 	 * 3. Request size is bigger than stripesize * ndisks. If it isn't,
635 	 *    there will be no need to send more than one I/O request to
636 	 *    a provider, so there is nothing to optmize.
637 	 * and
638 	 * 4. Request is not unmapped.
639 	 * and
640 	 * 5. It is not a BIO_DELETE.
641 	 */
642 	if (g_stripe_fast && bp->bio_length <= MAXPHYS &&
643 	    bp->bio_length >= stripesize * sc->sc_ndisks &&
644 	    (bp->bio_flags & BIO_UNMAPPED) == 0 &&
645 	    bp->bio_cmd != BIO_DELETE) {
646 		fast = 1;
647 	}
648 	error = 0;
649 	if (fast) {
650 		error = g_stripe_start_fast(bp, no, offset, length);
651 		if (error != 0)
652 			g_stripe_fast_failed++;
653 	}
654 	/*
655 	 * Do use "economic" when:
656 	 * 1. "Economic" mode is ON.
657 	 * or
658 	 * 2. "Fast" mode failed. It can only fail if there is no memory.
659 	 */
660 	if (!fast || error != 0)
661 		error = g_stripe_start_economic(bp, no, offset, length);
662 	if (error != 0) {
663 		if (bp->bio_error == 0)
664 			bp->bio_error = error;
665 		g_io_deliver(bp, bp->bio_error);
666 	}
667 }
668 
669 static void
670 g_stripe_check_and_run(struct g_stripe_softc *sc)
671 {
672 	struct g_provider *dp;
673 	off_t mediasize, ms;
674 	u_int no, sectorsize = 0;
675 
676 	g_topology_assert();
677 	if (g_stripe_nvalid(sc) != sc->sc_ndisks)
678 		return;
679 
680 	sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
681 	    sc->sc_name);
682 	sc->sc_provider->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
683 	if (g_stripe_fast == 0)
684 		sc->sc_provider->flags |= G_PF_ACCEPT_UNMAPPED;
685 	/*
686 	 * Find the smallest disk.
687 	 */
688 	mediasize = sc->sc_disks[0]->provider->mediasize;
689 	if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
690 		mediasize -= sc->sc_disks[0]->provider->sectorsize;
691 	mediasize -= mediasize % sc->sc_stripesize;
692 	sectorsize = sc->sc_disks[0]->provider->sectorsize;
693 	for (no = 1; no < sc->sc_ndisks; no++) {
694 		dp = sc->sc_disks[no]->provider;
695 		ms = dp->mediasize;
696 		if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
697 			ms -= dp->sectorsize;
698 		ms -= ms % sc->sc_stripesize;
699 		if (ms < mediasize)
700 			mediasize = ms;
701 		sectorsize = lcm(sectorsize, dp->sectorsize);
702 
703 		/* A provider underneath us doesn't support unmapped */
704 		if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
705 			G_STRIPE_DEBUG(1, "Cancelling unmapped "
706 			    "because of %s.", dp->name);
707 			sc->sc_provider->flags &= ~G_PF_ACCEPT_UNMAPPED;
708 		}
709 	}
710 	sc->sc_provider->sectorsize = sectorsize;
711 	sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
712 	sc->sc_provider->stripesize = sc->sc_stripesize;
713 	sc->sc_provider->stripeoffset = 0;
714 	g_error_provider(sc->sc_provider, 0);
715 
716 	G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
717 }
718 
719 static int
720 g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
721 {
722 	struct g_provider *pp;
723 	u_char *buf;
724 	int error;
725 
726 	g_topology_assert();
727 
728 	error = g_access(cp, 1, 0, 0);
729 	if (error != 0)
730 		return (error);
731 	pp = cp->provider;
732 	g_topology_unlock();
733 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
734 	    &error);
735 	g_topology_lock();
736 	g_access(cp, -1, 0, 0);
737 	if (buf == NULL)
738 		return (error);
739 
740 	/* Decode metadata. */
741 	stripe_metadata_decode(buf, md);
742 	g_free(buf);
743 
744 	return (0);
745 }
746 
747 /*
748  * Add disk to given device.
749  */
750 static int
751 g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
752 {
753 	struct g_consumer *cp, *fcp;
754 	struct g_geom *gp;
755 	int error;
756 
757 	g_topology_assert();
758 	/* Metadata corrupted? */
759 	if (no >= sc->sc_ndisks)
760 		return (EINVAL);
761 
762 	/* Check if disk is not already attached. */
763 	if (sc->sc_disks[no] != NULL)
764 		return (EEXIST);
765 
766 	gp = sc->sc_geom;
767 	fcp = LIST_FIRST(&gp->consumer);
768 
769 	cp = g_new_consumer(gp);
770 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
771 	cp->private = NULL;
772 	cp->index = no;
773 	error = g_attach(cp, pp);
774 	if (error != 0) {
775 		g_destroy_consumer(cp);
776 		return (error);
777 	}
778 
779 	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
780 		error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
781 		if (error != 0) {
782 			g_detach(cp);
783 			g_destroy_consumer(cp);
784 			return (error);
785 		}
786 	}
787 	if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
788 		struct g_stripe_metadata md;
789 
790 		/* Reread metadata. */
791 		error = g_stripe_read_metadata(cp, &md);
792 		if (error != 0)
793 			goto fail;
794 
795 		if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
796 		    strcmp(md.md_name, sc->sc_name) != 0 ||
797 		    md.md_id != sc->sc_id) {
798 			G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
799 			goto fail;
800 		}
801 	}
802 
803 	sc->sc_disks[no] = cp;
804 	G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
805 	g_stripe_check_and_run(sc);
806 
807 	return (0);
808 fail:
809 	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
810 		g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
811 	g_detach(cp);
812 	g_destroy_consumer(cp);
813 	return (error);
814 }
815 
816 static struct g_geom *
817 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
818     u_int type)
819 {
820 	struct g_stripe_softc *sc;
821 	struct g_geom *gp;
822 	u_int no;
823 
824 	g_topology_assert();
825 	G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
826 	    md->md_id);
827 
828 	/* Two disks is minimum. */
829 	if (md->md_all < 2) {
830 		G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
831 		return (NULL);
832 	}
833 #if 0
834 	/* Stripe size have to be grater than or equal to sector size. */
835 	if (md->md_stripesize < sectorsize) {
836 		G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
837 		return (NULL);
838 	}
839 #endif
840 	/* Stripe size have to be power of 2. */
841 	if (!powerof2(md->md_stripesize)) {
842 		G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
843 		return (NULL);
844 	}
845 
846 	/* Check for duplicate unit */
847 	LIST_FOREACH(gp, &mp->geom, geom) {
848 		sc = gp->softc;
849 		if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
850 			G_STRIPE_DEBUG(0, "Device %s already configured.",
851 			    sc->sc_name);
852 			return (NULL);
853 		}
854 	}
855 	gp = g_new_geomf(mp, "%s", md->md_name);
856 	sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
857 	gp->start = g_stripe_start;
858 	gp->spoiled = g_stripe_orphan;
859 	gp->orphan = g_stripe_orphan;
860 	gp->access = g_stripe_access;
861 	gp->dumpconf = g_stripe_dumpconf;
862 
863 	sc->sc_id = md->md_id;
864 	sc->sc_stripesize = md->md_stripesize;
865 	sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
866 	sc->sc_ndisks = md->md_all;
867 	sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
868 	    M_STRIPE, M_WAITOK | M_ZERO);
869 	for (no = 0; no < sc->sc_ndisks; no++)
870 		sc->sc_disks[no] = NULL;
871 	sc->sc_type = type;
872 	mtx_init(&sc->sc_lock, "gstripe lock", NULL, MTX_DEF);
873 
874 	gp->softc = sc;
875 	sc->sc_geom = gp;
876 	sc->sc_provider = NULL;
877 
878 	G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
879 
880 	return (gp);
881 }
882 
883 static int
884 g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
885 {
886 	struct g_provider *pp;
887 	struct g_consumer *cp, *cp1;
888 	struct g_geom *gp;
889 
890 	g_topology_assert();
891 
892 	if (sc == NULL)
893 		return (ENXIO);
894 
895 	pp = sc->sc_provider;
896 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
897 		if (force) {
898 			G_STRIPE_DEBUG(0, "Device %s is still open, so it "
899 			    "can't be definitely removed.", pp->name);
900 		} else {
901 			G_STRIPE_DEBUG(1,
902 			    "Device %s is still open (r%dw%de%d).", pp->name,
903 			    pp->acr, pp->acw, pp->ace);
904 			return (EBUSY);
905 		}
906 	}
907 
908 	gp = sc->sc_geom;
909 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
910 		g_stripe_remove_disk(cp);
911 		if (cp1 == NULL)
912 			return (0);	/* Recursion happened. */
913 	}
914 	if (!LIST_EMPTY(&gp->consumer))
915 		return (EINPROGRESS);
916 
917 	gp->softc = NULL;
918 	KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
919 	    gp->name));
920 	free(sc->sc_disks, M_STRIPE);
921 	mtx_destroy(&sc->sc_lock);
922 	free(sc, M_STRIPE);
923 	G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
924 	g_wither_geom(gp, ENXIO);
925 	return (0);
926 }
927 
928 static int
929 g_stripe_destroy_geom(struct gctl_req *req __unused,
930     struct g_class *mp __unused, struct g_geom *gp)
931 {
932 	struct g_stripe_softc *sc;
933 
934 	sc = gp->softc;
935 	return (g_stripe_destroy(sc, 0));
936 }
937 
938 static struct g_geom *
939 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
940 {
941 	struct g_stripe_metadata md;
942 	struct g_stripe_softc *sc;
943 	struct g_consumer *cp;
944 	struct g_geom *gp;
945 	int error;
946 
947 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
948 	g_topology_assert();
949 
950 	/* Skip providers that are already open for writing. */
951 	if (pp->acw > 0)
952 		return (NULL);
953 
954 	G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
955 
956 	gp = g_new_geomf(mp, "stripe:taste");
957 	gp->start = g_stripe_start;
958 	gp->access = g_stripe_access;
959 	gp->orphan = g_stripe_orphan;
960 	cp = g_new_consumer(gp);
961 	g_attach(cp, pp);
962 	error = g_stripe_read_metadata(cp, &md);
963 	g_detach(cp);
964 	g_destroy_consumer(cp);
965 	g_destroy_geom(gp);
966 	if (error != 0)
967 		return (NULL);
968 	gp = NULL;
969 
970 	if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
971 		return (NULL);
972 	if (md.md_version > G_STRIPE_VERSION) {
973 		printf("geom_stripe.ko module is too old to handle %s.\n",
974 		    pp->name);
975 		return (NULL);
976 	}
977 	/*
978 	 * Backward compatibility:
979 	 */
980 	/* There was no md_provider field in earlier versions of metadata. */
981 	if (md.md_version < 2)
982 		bzero(md.md_provider, sizeof(md.md_provider));
983 	/* There was no md_provsize field in earlier versions of metadata. */
984 	if (md.md_version < 3)
985 		md.md_provsize = pp->mediasize;
986 
987 	if (md.md_provider[0] != '\0' &&
988 	    !g_compare_names(md.md_provider, pp->name))
989 		return (NULL);
990 	if (md.md_provsize != pp->mediasize)
991 		return (NULL);
992 
993 	/*
994 	 * Let's check if device already exists.
995 	 */
996 	sc = NULL;
997 	LIST_FOREACH(gp, &mp->geom, geom) {
998 		sc = gp->softc;
999 		if (sc == NULL)
1000 			continue;
1001 		if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
1002 			continue;
1003 		if (strcmp(md.md_name, sc->sc_name) != 0)
1004 			continue;
1005 		if (md.md_id != sc->sc_id)
1006 			continue;
1007 		break;
1008 	}
1009 	if (gp != NULL) {
1010 		G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1011 		error = g_stripe_add_disk(sc, pp, md.md_no);
1012 		if (error != 0) {
1013 			G_STRIPE_DEBUG(0,
1014 			    "Cannot add disk %s to %s (error=%d).", pp->name,
1015 			    gp->name, error);
1016 			return (NULL);
1017 		}
1018 	} else {
1019 		gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
1020 		if (gp == NULL) {
1021 			G_STRIPE_DEBUG(0, "Cannot create device %s.",
1022 			    md.md_name);
1023 			return (NULL);
1024 		}
1025 		sc = gp->softc;
1026 		G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1027 		error = g_stripe_add_disk(sc, pp, md.md_no);
1028 		if (error != 0) {
1029 			G_STRIPE_DEBUG(0,
1030 			    "Cannot add disk %s to %s (error=%d).", pp->name,
1031 			    gp->name, error);
1032 			g_stripe_destroy(sc, 1);
1033 			return (NULL);
1034 		}
1035 	}
1036 
1037 	return (gp);
1038 }
1039 
1040 static void
1041 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1042 {
1043 	u_int attached, no;
1044 	struct g_stripe_metadata md;
1045 	struct g_provider *pp;
1046 	struct g_stripe_softc *sc;
1047 	struct g_geom *gp;
1048 	struct sbuf *sb;
1049 	off_t *stripesize;
1050 	const char *name;
1051 	char param[16];
1052 	int *nargs;
1053 
1054 	g_topology_assert();
1055 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1056 	if (nargs == NULL) {
1057 		gctl_error(req, "No '%s' argument.", "nargs");
1058 		return;
1059 	}
1060 	if (*nargs <= 2) {
1061 		gctl_error(req, "Too few arguments.");
1062 		return;
1063 	}
1064 
1065 	strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1066 	md.md_version = G_STRIPE_VERSION;
1067 	name = gctl_get_asciiparam(req, "arg0");
1068 	if (name == NULL) {
1069 		gctl_error(req, "No 'arg%u' argument.", 0);
1070 		return;
1071 	}
1072 	strlcpy(md.md_name, name, sizeof(md.md_name));
1073 	md.md_id = arc4random();
1074 	md.md_no = 0;
1075 	md.md_all = *nargs - 1;
1076 	stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1077 	if (stripesize == NULL) {
1078 		gctl_error(req, "No '%s' argument.", "stripesize");
1079 		return;
1080 	}
1081 	md.md_stripesize = (uint32_t)*stripesize;
1082 	bzero(md.md_provider, sizeof(md.md_provider));
1083 	/* This field is not important here. */
1084 	md.md_provsize = 0;
1085 
1086 	/* Check all providers are valid */
1087 	for (no = 1; no < *nargs; no++) {
1088 		snprintf(param, sizeof(param), "arg%u", no);
1089 		name = gctl_get_asciiparam(req, param);
1090 		if (name == NULL) {
1091 			gctl_error(req, "No 'arg%u' argument.", no);
1092 			return;
1093 		}
1094 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1095 			name += strlen("/dev/");
1096 		pp = g_provider_by_name(name);
1097 		if (pp == NULL) {
1098 			G_STRIPE_DEBUG(1, "Disk %s is invalid.", name);
1099 			gctl_error(req, "Disk %s is invalid.", name);
1100 			return;
1101 		}
1102 	}
1103 
1104 	gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1105 	if (gp == NULL) {
1106 		gctl_error(req, "Can't configure %s.", md.md_name);
1107 		return;
1108 	}
1109 
1110 	sc = gp->softc;
1111 	sb = sbuf_new_auto();
1112 	sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1113 	for (attached = 0, no = 1; no < *nargs; no++) {
1114 		snprintf(param, sizeof(param), "arg%u", no);
1115 		name = gctl_get_asciiparam(req, param);
1116 		if (name == NULL) {
1117 			gctl_error(req, "No 'arg%u' argument.", no);
1118 			continue;
1119 		}
1120 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1121 			name += strlen("/dev/");
1122 		pp = g_provider_by_name(name);
1123 		KASSERT(pp != NULL, ("Provider %s disappear?!", name));
1124 		if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1125 			G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1126 			    no, pp->name, gp->name);
1127 			sbuf_printf(sb, " %s", pp->name);
1128 			continue;
1129 		}
1130 		attached++;
1131 	}
1132 	sbuf_finish(sb);
1133 	if (md.md_all != attached) {
1134 		g_stripe_destroy(gp->softc, 1);
1135 		gctl_error(req, "%s", sbuf_data(sb));
1136 	}
1137 	sbuf_delete(sb);
1138 }
1139 
1140 static struct g_stripe_softc *
1141 g_stripe_find_device(struct g_class *mp, const char *name)
1142 {
1143 	struct g_stripe_softc *sc;
1144 	struct g_geom *gp;
1145 
1146 	LIST_FOREACH(gp, &mp->geom, geom) {
1147 		sc = gp->softc;
1148 		if (sc == NULL)
1149 			continue;
1150 		if (strcmp(sc->sc_name, name) == 0)
1151 			return (sc);
1152 	}
1153 	return (NULL);
1154 }
1155 
1156 static void
1157 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1158 {
1159 	struct g_stripe_softc *sc;
1160 	int *force, *nargs, error;
1161 	const char *name;
1162 	char param[16];
1163 	u_int i;
1164 
1165 	g_topology_assert();
1166 
1167 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1168 	if (nargs == NULL) {
1169 		gctl_error(req, "No '%s' argument.", "nargs");
1170 		return;
1171 	}
1172 	if (*nargs <= 0) {
1173 		gctl_error(req, "Missing device(s).");
1174 		return;
1175 	}
1176 	force = gctl_get_paraml(req, "force", sizeof(*force));
1177 	if (force == NULL) {
1178 		gctl_error(req, "No '%s' argument.", "force");
1179 		return;
1180 	}
1181 
1182 	for (i = 0; i < (u_int)*nargs; i++) {
1183 		snprintf(param, sizeof(param), "arg%u", i);
1184 		name = gctl_get_asciiparam(req, param);
1185 		if (name == NULL) {
1186 			gctl_error(req, "No 'arg%u' argument.", i);
1187 			return;
1188 		}
1189 		sc = g_stripe_find_device(mp, name);
1190 		if (sc == NULL) {
1191 			gctl_error(req, "No such device: %s.", name);
1192 			return;
1193 		}
1194 		error = g_stripe_destroy(sc, *force);
1195 		if (error != 0) {
1196 			gctl_error(req, "Cannot destroy device %s (error=%d).",
1197 			    sc->sc_name, error);
1198 			return;
1199 		}
1200 	}
1201 }
1202 
1203 static void
1204 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1205 {
1206 	uint32_t *version;
1207 
1208 	g_topology_assert();
1209 
1210 	version = gctl_get_paraml(req, "version", sizeof(*version));
1211 	if (version == NULL) {
1212 		gctl_error(req, "No '%s' argument.", "version");
1213 		return;
1214 	}
1215 	if (*version != G_STRIPE_VERSION) {
1216 		gctl_error(req, "Userland and kernel parts are out of sync.");
1217 		return;
1218 	}
1219 
1220 	if (strcmp(verb, "create") == 0) {
1221 		g_stripe_ctl_create(req, mp);
1222 		return;
1223 	} else if (strcmp(verb, "destroy") == 0 ||
1224 	    strcmp(verb, "stop") == 0) {
1225 		g_stripe_ctl_destroy(req, mp);
1226 		return;
1227 	}
1228 
1229 	gctl_error(req, "Unknown verb.");
1230 }
1231 
1232 static void
1233 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1234     struct g_consumer *cp, struct g_provider *pp)
1235 {
1236 	struct g_stripe_softc *sc;
1237 
1238 	sc = gp->softc;
1239 	if (sc == NULL)
1240 		return;
1241 	if (pp != NULL) {
1242 		/* Nothing here. */
1243 	} else if (cp != NULL) {
1244 		sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1245 		    (u_int)cp->index);
1246 	} else {
1247 		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1248 		sbuf_printf(sb, "%s<Stripesize>%ju</Stripesize>\n", indent,
1249 		    (uintmax_t)sc->sc_stripesize);
1250 		sbuf_printf(sb, "%s<Type>", indent);
1251 		switch (sc->sc_type) {
1252 		case G_STRIPE_TYPE_AUTOMATIC:
1253 			sbuf_cat(sb, "AUTOMATIC");
1254 			break;
1255 		case G_STRIPE_TYPE_MANUAL:
1256 			sbuf_cat(sb, "MANUAL");
1257 			break;
1258 		default:
1259 			sbuf_cat(sb, "UNKNOWN");
1260 			break;
1261 		}
1262 		sbuf_cat(sb, "</Type>\n");
1263 		sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1264 		    indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1265 		sbuf_printf(sb, "%s<State>", indent);
1266 		if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1267 			sbuf_cat(sb, "UP");
1268 		else
1269 			sbuf_cat(sb, "DOWN");
1270 		sbuf_cat(sb, "</State>\n");
1271 	}
1272 }
1273 
1274 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);
1275 MODULE_VERSION(geom_stripe, 0);
1276