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