2 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
35 #include <sys/mutex.h>
38 #include <sys/sysctl.h>
39 #include <sys/malloc.h>
41 #include <geom/geom.h>
42 #include <geom/stripe/g_stripe.h>
44 FEATURE(geom_stripe, "GEOM striping support");
46 static MALLOC_DEFINE(M_STRIPE, "stripe_data", "GEOM_STRIPE Data");
48 static uma_zone_t g_stripe_zone;
50 static int g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force);
51 static int g_stripe_destroy_geom(struct gctl_req *req, struct g_class *mp,
54 static g_taste_t g_stripe_taste;
55 static g_ctl_req_t g_stripe_config;
56 static g_dumpconf_t g_stripe_dumpconf;
57 static g_init_t g_stripe_init;
58 static g_fini_t g_stripe_fini;
60 struct g_class g_stripe_class = {
61 .name = G_STRIPE_CLASS_NAME,
63 .ctlreq = g_stripe_config,
64 .taste = g_stripe_taste,
65 .destroy_geom = g_stripe_destroy_geom,
66 .init = g_stripe_init,
70 SYSCTL_DECL(_kern_geom);
71 static SYSCTL_NODE(_kern_geom, OID_AUTO, stripe, CTLFLAG_RW, 0,
73 static u_int g_stripe_debug = 0;
74 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, debug, CTLFLAG_RWTUN, &g_stripe_debug, 0,
76 static int g_stripe_fast = 0;
78 g_sysctl_stripe_fast(SYSCTL_HANDLER_ARGS)
83 error = sysctl_handle_int(oidp, &fast, 0, req);
84 if (error == 0 && req->newptr != NULL)
88 SYSCTL_PROC(_kern_geom_stripe, OID_AUTO, fast, CTLTYPE_INT | CTLFLAG_RWTUN,
89 NULL, 0, g_sysctl_stripe_fast, "I", "Fast, but memory-consuming, mode");
90 static u_int g_stripe_maxmem = MAXPHYS * 100;
91 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, maxmem, CTLFLAG_RDTUN, &g_stripe_maxmem,
92 0, "Maximum memory that can be allocated in \"fast\" mode (in bytes)");
93 static u_int g_stripe_fast_failed = 0;
94 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, fast_failed, CTLFLAG_RD,
95 &g_stripe_fast_failed, 0, "How many times \"fast\" mode failed");
98 * Greatest Common Divisor.
101 gcd(u_int a, u_int b)
114 * Least Common Multiple.
117 lcm(u_int a, u_int b)
120 return ((a * b) / gcd(a, b));
124 g_stripe_init(struct g_class *mp __unused)
127 g_stripe_zone = uma_zcreate("g_stripe_zone", MAXPHYS, NULL, NULL,
129 g_stripe_maxmem -= g_stripe_maxmem % MAXPHYS;
130 uma_zone_set_max(g_stripe_zone, g_stripe_maxmem / MAXPHYS);
134 g_stripe_fini(struct g_class *mp __unused)
137 uma_zdestroy(g_stripe_zone);
141 * Return the number of valid disks.
144 g_stripe_nvalid(struct g_stripe_softc *sc)
149 for (i = 0; i < sc->sc_ndisks; i++) {
150 if (sc->sc_disks[i] != NULL)
158 g_stripe_remove_disk(struct g_consumer *cp)
160 struct g_stripe_softc *sc;
163 KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
164 sc = (struct g_stripe_softc *)cp->geom->softc;
165 KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
167 if (cp->private == NULL) {
168 G_STRIPE_DEBUG(0, "Disk %s removed from %s.",
169 cp->provider->name, sc->sc_name);
170 cp->private = (void *)(uintptr_t)-1;
173 if (sc->sc_provider != NULL) {
174 G_STRIPE_DEBUG(0, "Device %s deactivated.",
175 sc->sc_provider->name);
176 g_wither_provider(sc->sc_provider, ENXIO);
177 sc->sc_provider = NULL;
180 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
182 sc->sc_disks[cp->index] = NULL;
185 g_destroy_consumer(cp);
186 /* If there are no valid disks anymore, remove device. */
187 if (LIST_EMPTY(&sc->sc_geom->consumer))
188 g_stripe_destroy(sc, 1);
192 g_stripe_orphan(struct g_consumer *cp)
194 struct g_stripe_softc *sc;
203 g_stripe_remove_disk(cp);
207 g_stripe_access(struct g_provider *pp, int dr, int dw, int de)
209 struct g_consumer *cp1, *cp2, *tmp;
210 struct g_stripe_softc *sc;
217 KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
219 /* On first open, grab an extra "exclusive" bit */
220 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
222 /* ... and let go of it on last close */
223 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
226 LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) {
227 error = g_access(cp1, dr, dw, de);
230 if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 &&
231 cp1->private != NULL) {
232 g_stripe_remove_disk(cp1); /* May destroy geom. */
238 LIST_FOREACH(cp2, &gp->consumer, consumer) {
241 g_access(cp2, -dr, -dw, -de);
247 g_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset,
248 off_t length, int mode)
253 stripesize = sc->sc_stripesize;
254 len = (size_t)(stripesize - (offset & (stripesize - 1)));
256 bcopy(src, dst, len);
258 dst += len + stripesize * (sc->sc_ndisks - 1);
262 src += len + stripesize * (sc->sc_ndisks - 1);
266 ("Length < 0 (stripesize=%zu, offset=%jd, length=%jd).",
267 (size_t)stripesize, (intmax_t)offset, (intmax_t)length));
268 if (length > stripesize)
272 } while (length > 0);
276 g_stripe_done(struct bio *bp)
278 struct g_stripe_softc *sc;
281 pbp = bp->bio_parent;
282 sc = pbp->bio_to->geom->softc;
283 if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) {
284 g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset,
286 bp->bio_data = bp->bio_caller1;
287 bp->bio_caller1 = NULL;
289 mtx_lock(&sc->sc_lock);
290 if (pbp->bio_error == 0)
291 pbp->bio_error = bp->bio_error;
292 pbp->bio_completed += bp->bio_completed;
294 if (pbp->bio_children == pbp->bio_inbed) {
295 mtx_unlock(&sc->sc_lock);
296 if (pbp->bio_driver1 != NULL)
297 uma_zfree(g_stripe_zone, pbp->bio_driver1);
298 g_io_deliver(pbp, pbp->bio_error);
300 mtx_unlock(&sc->sc_lock);
305 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
307 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
308 u_int nparts = 0, stripesize;
309 struct g_stripe_softc *sc;
310 char *addr, *data = NULL;
314 sc = bp->bio_to->geom->softc;
317 stripesize = sc->sc_stripesize;
319 cbp = g_clone_bio(bp);
324 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
327 * Fill in the component buf structure.
329 cbp->bio_done = g_stripe_done;
330 cbp->bio_offset = offset;
331 cbp->bio_data = addr;
332 cbp->bio_caller1 = NULL;
333 cbp->bio_length = length;
334 cbp->bio_caller2 = sc->sc_disks[no];
336 /* offset -= offset % stripesize; */
337 offset -= offset & (stripesize - 1);
339 length = bp->bio_length - length;
340 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
341 if (no > sc->sc_ndisks - 1) {
343 offset += stripesize;
345 if (nparts >= sc->sc_ndisks) {
346 cbp = TAILQ_NEXT(cbp, bio_queue);
348 cbp = TAILQ_FIRST(&queue);
351 * Update bio structure.
354 * MIN() is in case when
355 * (bp->bio_length % sc->sc_stripesize) != 0.
357 cbp->bio_length += MIN(stripesize, length);
358 if (cbp->bio_caller1 == NULL) {
359 cbp->bio_caller1 = cbp->bio_data;
360 cbp->bio_data = NULL;
362 data = uma_zalloc(g_stripe_zone,
371 cbp = g_clone_bio(bp);
376 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
379 * Fill in the component buf structure.
381 cbp->bio_done = g_stripe_done;
382 cbp->bio_offset = offset;
383 cbp->bio_data = addr;
384 cbp->bio_caller1 = NULL;
386 * MIN() is in case when
387 * (bp->bio_length % sc->sc_stripesize) != 0.
389 cbp->bio_length = MIN(stripesize, length);
390 cbp->bio_caller2 = sc->sc_disks[no];
394 bp->bio_driver1 = data;
396 * Fire off all allocated requests!
398 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
399 struct g_consumer *cp;
401 TAILQ_REMOVE(&queue, cbp, bio_queue);
402 cp = cbp->bio_caller2;
403 cbp->bio_caller2 = NULL;
404 cbp->bio_to = cp->provider;
405 if (cbp->bio_caller1 != NULL) {
406 cbp->bio_data = data;
407 if (bp->bio_cmd == BIO_WRITE) {
408 g_stripe_copy(sc, cbp->bio_caller1, data,
409 cbp->bio_offset, cbp->bio_length, 0);
411 data += cbp->bio_length;
413 G_STRIPE_LOGREQ(cbp, "Sending request.");
414 g_io_request(cbp, cp);
419 uma_zfree(g_stripe_zone, data);
420 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
421 TAILQ_REMOVE(&queue, cbp, bio_queue);
422 if (cbp->bio_caller1 != NULL) {
423 cbp->bio_data = cbp->bio_caller1;
424 cbp->bio_caller1 = NULL;
433 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
435 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
436 struct g_stripe_softc *sc;
442 sc = bp->bio_to->geom->softc;
444 stripesize = sc->sc_stripesize;
446 cbp = g_clone_bio(bp);
451 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
453 * Fill in the component buf structure.
455 if (bp->bio_length == length)
456 cbp->bio_done = g_std_done; /* Optimized lockless case. */
458 cbp->bio_done = g_stripe_done;
459 cbp->bio_offset = offset;
460 cbp->bio_length = length;
461 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
462 bp->bio_ma_n = round_page(bp->bio_ma_offset +
463 bp->bio_length) / PAGE_SIZE;
467 cbp->bio_caller2 = sc->sc_disks[no];
469 /* offset -= offset % stripesize; */
470 offset -= offset & (stripesize - 1);
471 if (bp->bio_cmd != BIO_DELETE)
473 length = bp->bio_length - length;
474 for (no++; length > 0; no++, length -= stripesize) {
475 if (no > sc->sc_ndisks - 1) {
477 offset += stripesize;
479 cbp = g_clone_bio(bp);
484 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
487 * Fill in the component buf structure.
489 cbp->bio_done = g_stripe_done;
490 cbp->bio_offset = offset;
492 * MIN() is in case when
493 * (bp->bio_length % sc->sc_stripesize) != 0.
495 cbp->bio_length = MIN(stripesize, length);
496 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
497 cbp->bio_ma_offset += (uintptr_t)addr;
498 cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
499 cbp->bio_ma_offset %= PAGE_SIZE;
500 cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
501 cbp->bio_length) / PAGE_SIZE;
503 cbp->bio_data = addr;
505 cbp->bio_caller2 = sc->sc_disks[no];
507 if (bp->bio_cmd != BIO_DELETE)
511 * Fire off all allocated requests!
513 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
514 struct g_consumer *cp;
516 TAILQ_REMOVE(&queue, cbp, bio_queue);
517 cp = cbp->bio_caller2;
518 cbp->bio_caller2 = NULL;
519 cbp->bio_to = cp->provider;
520 G_STRIPE_LOGREQ(cbp, "Sending request.");
521 g_io_request(cbp, cp);
525 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
526 TAILQ_REMOVE(&queue, cbp, bio_queue);
534 g_stripe_flush(struct g_stripe_softc *sc, struct bio *bp)
536 struct bio_queue_head queue;
537 struct g_consumer *cp;
542 for (no = 0; no < sc->sc_ndisks; no++) {
543 cbp = g_clone_bio(bp);
545 for (cbp = bioq_first(&queue); cbp != NULL;
546 cbp = bioq_first(&queue)) {
547 bioq_remove(&queue, cbp);
550 if (bp->bio_error == 0)
551 bp->bio_error = ENOMEM;
552 g_io_deliver(bp, bp->bio_error);
555 bioq_insert_tail(&queue, cbp);
556 cbp->bio_done = g_stripe_done;
557 cbp->bio_caller2 = sc->sc_disks[no];
558 cbp->bio_to = sc->sc_disks[no]->provider;
560 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
561 bioq_remove(&queue, cbp);
562 G_STRIPE_LOGREQ(cbp, "Sending request.");
563 cp = cbp->bio_caller2;
564 cbp->bio_caller2 = NULL;
565 g_io_request(cbp, cp);
570 g_stripe_start(struct bio *bp)
572 off_t offset, start, length, nstripe;
573 struct g_stripe_softc *sc;
574 u_int no, stripesize;
577 sc = bp->bio_to->geom->softc;
579 * If sc == NULL, provider's error should be set and g_stripe_start()
580 * should not be called at all.
583 ("Provider's error should be set (error=%d)(device=%s).",
584 bp->bio_to->error, bp->bio_to->name));
586 G_STRIPE_LOGREQ(bp, "Request received.");
588 switch (bp->bio_cmd) {
594 g_stripe_flush(sc, bp);
597 /* To which provider it should be delivered? */
599 g_io_deliver(bp, EOPNOTSUPP);
603 stripesize = sc->sc_stripesize;
606 * Calculations are quite messy, but fast I hope.
610 /* nstripe = bp->bio_offset / stripesize; */
611 nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
613 no = nstripe % sc->sc_ndisks;
614 /* Start position in stripe. */
615 /* start = bp->bio_offset % stripesize; */
616 start = bp->bio_offset & (stripesize - 1);
617 /* Start position in disk. */
618 /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
619 offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
620 /* Length of data to operate. */
621 length = MIN(bp->bio_length, stripesize - start);
624 * Do use "fast" mode when:
625 * 1. "Fast" mode is ON.
627 * 2. Request size is less than or equal to MAXPHYS,
628 * which should always be true.
630 * 3. Request size is bigger than stripesize * ndisks. If it isn't,
631 * there will be no need to send more than one I/O request to
632 * a provider, so there is nothing to optmize.
634 * 4. Request is not unmapped.
636 * 5. It is not a BIO_DELETE.
638 if (g_stripe_fast && bp->bio_length <= MAXPHYS &&
639 bp->bio_length >= stripesize * sc->sc_ndisks &&
640 (bp->bio_flags & BIO_UNMAPPED) == 0 &&
641 bp->bio_cmd != BIO_DELETE) {
646 error = g_stripe_start_fast(bp, no, offset, length);
648 g_stripe_fast_failed++;
651 * Do use "economic" when:
652 * 1. "Economic" mode is ON.
654 * 2. "Fast" mode failed. It can only fail if there is no memory.
656 if (!fast || error != 0)
657 error = g_stripe_start_economic(bp, no, offset, length);
659 if (bp->bio_error == 0)
660 bp->bio_error = error;
661 g_io_deliver(bp, bp->bio_error);
666 g_stripe_check_and_run(struct g_stripe_softc *sc)
668 struct g_provider *dp;
670 u_int no, sectorsize = 0;
673 if (g_stripe_nvalid(sc) != sc->sc_ndisks)
676 sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
678 sc->sc_provider->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
679 if (g_stripe_fast == 0)
680 sc->sc_provider->flags |= G_PF_ACCEPT_UNMAPPED;
682 * Find the smallest disk.
684 mediasize = sc->sc_disks[0]->provider->mediasize;
685 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
686 mediasize -= sc->sc_disks[0]->provider->sectorsize;
687 mediasize -= mediasize % sc->sc_stripesize;
688 sectorsize = sc->sc_disks[0]->provider->sectorsize;
689 for (no = 1; no < sc->sc_ndisks; no++) {
690 dp = sc->sc_disks[no]->provider;
692 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
693 ms -= dp->sectorsize;
694 ms -= ms % sc->sc_stripesize;
697 sectorsize = lcm(sectorsize, dp->sectorsize);
699 /* A provider underneath us doesn't support unmapped */
700 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
701 G_STRIPE_DEBUG(1, "Cancelling unmapped "
702 "because of %s.", dp->name);
703 sc->sc_provider->flags &= ~G_PF_ACCEPT_UNMAPPED;
706 sc->sc_provider->sectorsize = sectorsize;
707 sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
708 sc->sc_provider->stripesize = sc->sc_stripesize;
709 sc->sc_provider->stripeoffset = 0;
710 g_error_provider(sc->sc_provider, 0);
712 G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
716 g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
718 struct g_provider *pp;
724 error = g_access(cp, 1, 0, 0);
729 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
732 g_access(cp, -1, 0, 0);
736 /* Decode metadata. */
737 stripe_metadata_decode(buf, md);
744 * Add disk to given device.
747 g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
749 struct g_consumer *cp, *fcp;
754 /* Metadata corrupted? */
755 if (no >= sc->sc_ndisks)
758 /* Check if disk is not already attached. */
759 if (sc->sc_disks[no] != NULL)
763 fcp = LIST_FIRST(&gp->consumer);
765 cp = g_new_consumer(gp);
766 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
769 error = g_attach(cp, pp);
771 g_destroy_consumer(cp);
775 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
776 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
779 g_destroy_consumer(cp);
783 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
784 struct g_stripe_metadata md;
786 /* Reread metadata. */
787 error = g_stripe_read_metadata(cp, &md);
791 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
792 strcmp(md.md_name, sc->sc_name) != 0 ||
793 md.md_id != sc->sc_id) {
794 G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
799 sc->sc_disks[no] = cp;
800 G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
801 g_stripe_check_and_run(sc);
805 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
806 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
808 g_destroy_consumer(cp);
812 static struct g_geom *
813 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
816 struct g_stripe_softc *sc;
821 G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
824 /* Two disks is minimum. */
825 if (md->md_all < 2) {
826 G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
830 /* Stripe size have to be grater than or equal to sector size. */
831 if (md->md_stripesize < sectorsize) {
832 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
836 /* Stripe size have to be power of 2. */
837 if (!powerof2(md->md_stripesize)) {
838 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
842 /* Check for duplicate unit */
843 LIST_FOREACH(gp, &mp->geom, geom) {
845 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
846 G_STRIPE_DEBUG(0, "Device %s already configured.",
851 gp = g_new_geomf(mp, "%s", md->md_name);
852 sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
853 gp->start = g_stripe_start;
854 gp->spoiled = g_stripe_orphan;
855 gp->orphan = g_stripe_orphan;
856 gp->access = g_stripe_access;
857 gp->dumpconf = g_stripe_dumpconf;
859 sc->sc_id = md->md_id;
860 sc->sc_stripesize = md->md_stripesize;
861 sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
862 sc->sc_ndisks = md->md_all;
863 sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
864 M_STRIPE, M_WAITOK | M_ZERO);
865 for (no = 0; no < sc->sc_ndisks; no++)
866 sc->sc_disks[no] = NULL;
868 mtx_init(&sc->sc_lock, "gstripe lock", NULL, MTX_DEF);
872 sc->sc_provider = NULL;
874 G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
880 g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
882 struct g_provider *pp;
883 struct g_consumer *cp, *cp1;
891 pp = sc->sc_provider;
892 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
894 G_STRIPE_DEBUG(0, "Device %s is still open, so it "
895 "can't be definitely removed.", pp->name);
898 "Device %s is still open (r%dw%de%d).", pp->name,
899 pp->acr, pp->acw, pp->ace);
905 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
906 g_stripe_remove_disk(cp);
908 return (0); /* Recursion happened. */
910 if (!LIST_EMPTY(&gp->consumer))
911 return (EINPROGRESS);
914 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
916 free(sc->sc_disks, M_STRIPE);
917 mtx_destroy(&sc->sc_lock);
919 G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
920 g_wither_geom(gp, ENXIO);
925 g_stripe_destroy_geom(struct gctl_req *req __unused,
926 struct g_class *mp __unused, struct g_geom *gp)
928 struct g_stripe_softc *sc;
931 return (g_stripe_destroy(sc, 0));
934 static struct g_geom *
935 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
937 struct g_stripe_metadata md;
938 struct g_stripe_softc *sc;
939 struct g_consumer *cp;
943 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
946 /* Skip providers that are already open for writing. */
950 G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
952 gp = g_new_geomf(mp, "stripe:taste");
953 gp->start = g_stripe_start;
954 gp->access = g_stripe_access;
955 gp->orphan = g_stripe_orphan;
956 cp = g_new_consumer(gp);
958 error = g_stripe_read_metadata(cp, &md);
960 g_destroy_consumer(cp);
966 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
968 if (md.md_version > G_STRIPE_VERSION) {
969 printf("geom_stripe.ko module is too old to handle %s.\n",
974 * Backward compatibility:
976 /* There was no md_provider field in earlier versions of metadata. */
977 if (md.md_version < 2)
978 bzero(md.md_provider, sizeof(md.md_provider));
979 /* There was no md_provsize field in earlier versions of metadata. */
980 if (md.md_version < 3)
981 md.md_provsize = pp->mediasize;
983 if (md.md_provider[0] != '\0' &&
984 !g_compare_names(md.md_provider, pp->name))
986 if (md.md_provsize != pp->mediasize)
990 * Let's check if device already exists.
993 LIST_FOREACH(gp, &mp->geom, geom) {
997 if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
999 if (strcmp(md.md_name, sc->sc_name) != 0)
1001 if (md.md_id != sc->sc_id)
1006 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1007 error = g_stripe_add_disk(sc, pp, md.md_no);
1010 "Cannot add disk %s to %s (error=%d).", pp->name,
1015 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
1017 G_STRIPE_DEBUG(0, "Cannot create device %s.",
1022 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1023 error = g_stripe_add_disk(sc, pp, md.md_no);
1026 "Cannot add disk %s to %s (error=%d).", pp->name,
1028 g_stripe_destroy(sc, 1);
1037 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1040 struct g_stripe_metadata md;
1041 struct g_provider *pp;
1042 struct g_stripe_softc *sc;
1045 intmax_t *stripesize;
1050 g_topology_assert();
1051 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1052 if (nargs == NULL) {
1053 gctl_error(req, "No '%s' argument.", "nargs");
1057 gctl_error(req, "Too few arguments.");
1061 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1062 md.md_version = G_STRIPE_VERSION;
1063 name = gctl_get_asciiparam(req, "arg0");
1065 gctl_error(req, "No 'arg%u' argument.", 0);
1068 strlcpy(md.md_name, name, sizeof(md.md_name));
1069 md.md_id = arc4random();
1071 md.md_all = *nargs - 1;
1072 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1073 if (stripesize == NULL) {
1074 gctl_error(req, "No '%s' argument.", "stripesize");
1077 md.md_stripesize = *stripesize;
1078 bzero(md.md_provider, sizeof(md.md_provider));
1079 /* This field is not important here. */
1082 /* Check all providers are valid */
1083 for (no = 1; no < *nargs; no++) {
1084 snprintf(param, sizeof(param), "arg%u", no);
1085 name = gctl_get_asciiparam(req, param);
1087 gctl_error(req, "No 'arg%u' argument.", no);
1090 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1091 name += strlen("/dev/");
1092 pp = g_provider_by_name(name);
1094 G_STRIPE_DEBUG(1, "Disk %s is invalid.", name);
1095 gctl_error(req, "Disk %s is invalid.", name);
1100 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1102 gctl_error(req, "Can't configure %s.", md.md_name);
1107 sb = sbuf_new_auto();
1108 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1109 for (attached = 0, no = 1; no < *nargs; no++) {
1110 snprintf(param, sizeof(param), "arg%u", no);
1111 name = gctl_get_asciiparam(req, param);
1113 gctl_error(req, "No 'arg%u' argument.", no);
1116 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1117 name += strlen("/dev/");
1118 pp = g_provider_by_name(name);
1119 KASSERT(pp != NULL, ("Provider %s disappear?!", name));
1120 if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1121 G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1122 no, pp->name, gp->name);
1123 sbuf_printf(sb, " %s", pp->name);
1129 if (md.md_all != attached) {
1130 g_stripe_destroy(gp->softc, 1);
1131 gctl_error(req, "%s", sbuf_data(sb));
1136 static struct g_stripe_softc *
1137 g_stripe_find_device(struct g_class *mp, const char *name)
1139 struct g_stripe_softc *sc;
1142 LIST_FOREACH(gp, &mp->geom, geom) {
1146 if (strcmp(sc->sc_name, name) == 0)
1153 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1155 struct g_stripe_softc *sc;
1156 int *force, *nargs, error;
1161 g_topology_assert();
1163 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1164 if (nargs == NULL) {
1165 gctl_error(req, "No '%s' argument.", "nargs");
1169 gctl_error(req, "Missing device(s).");
1172 force = gctl_get_paraml(req, "force", sizeof(*force));
1173 if (force == NULL) {
1174 gctl_error(req, "No '%s' argument.", "force");
1178 for (i = 0; i < (u_int)*nargs; i++) {
1179 snprintf(param, sizeof(param), "arg%u", i);
1180 name = gctl_get_asciiparam(req, param);
1182 gctl_error(req, "No 'arg%u' argument.", i);
1185 sc = g_stripe_find_device(mp, name);
1187 gctl_error(req, "No such device: %s.", name);
1190 error = g_stripe_destroy(sc, *force);
1192 gctl_error(req, "Cannot destroy device %s (error=%d).",
1193 sc->sc_name, error);
1200 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1204 g_topology_assert();
1206 version = gctl_get_paraml(req, "version", sizeof(*version));
1207 if (version == NULL) {
1208 gctl_error(req, "No '%s' argument.", "version");
1211 if (*version != G_STRIPE_VERSION) {
1212 gctl_error(req, "Userland and kernel parts are out of sync.");
1216 if (strcmp(verb, "create") == 0) {
1217 g_stripe_ctl_create(req, mp);
1219 } else if (strcmp(verb, "destroy") == 0 ||
1220 strcmp(verb, "stop") == 0) {
1221 g_stripe_ctl_destroy(req, mp);
1225 gctl_error(req, "Unknown verb.");
1229 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1230 struct g_consumer *cp, struct g_provider *pp)
1232 struct g_stripe_softc *sc;
1239 } else if (cp != NULL) {
1240 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1243 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1244 sbuf_printf(sb, "%s<Stripesize>%u</Stripesize>\n", indent,
1245 (u_int)sc->sc_stripesize);
1246 sbuf_printf(sb, "%s<Type>", indent);
1247 switch (sc->sc_type) {
1248 case G_STRIPE_TYPE_AUTOMATIC:
1249 sbuf_printf(sb, "AUTOMATIC");
1251 case G_STRIPE_TYPE_MANUAL:
1252 sbuf_printf(sb, "MANUAL");
1255 sbuf_printf(sb, "UNKNOWN");
1258 sbuf_printf(sb, "</Type>\n");
1259 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1260 indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1261 sbuf_printf(sb, "%s<State>", indent);
1262 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1263 sbuf_printf(sb, "UP");
1265 sbuf_printf(sb, "DOWN");
1266 sbuf_printf(sb, "</State>\n");
1270 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);