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