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 TUNABLE_INT("kern.geom.stripe.debug", &g_stripe_debug);
75 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, debug, CTLFLAG_RW, &g_stripe_debug, 0,
77 static int g_stripe_fast = 0;
78 TUNABLE_INT("kern.geom.stripe.fast", &g_stripe_fast);
80 g_sysctl_stripe_fast(SYSCTL_HANDLER_ARGS)
85 error = sysctl_handle_int(oidp, &fast, 0, req);
86 if (error == 0 && req->newptr != NULL)
90 SYSCTL_PROC(_kern_geom_stripe, OID_AUTO, fast, CTLTYPE_INT | CTLFLAG_RW,
91 NULL, 0, g_sysctl_stripe_fast, "I", "Fast, but memory-consuming, mode");
92 static u_int g_stripe_maxmem = MAXPHYS * 100;
93 TUNABLE_INT("kern.geom.stripe.maxmem", &g_stripe_maxmem);
94 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, maxmem, CTLFLAG_RD, &g_stripe_maxmem,
95 0, "Maximum memory that can be allocated in \"fast\" mode (in bytes)");
96 static u_int g_stripe_fast_failed = 0;
97 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, fast_failed, CTLFLAG_RD,
98 &g_stripe_fast_failed, 0, "How many times \"fast\" mode failed");
101 * Greatest Common Divisor.
104 gcd(u_int a, u_int b)
117 * Least Common Multiple.
120 lcm(u_int a, u_int b)
123 return ((a * b) / gcd(a, b));
127 g_stripe_init(struct g_class *mp __unused)
130 g_stripe_zone = uma_zcreate("g_stripe_zone", MAXPHYS, NULL, NULL,
132 g_stripe_maxmem -= g_stripe_maxmem % MAXPHYS;
133 uma_zone_set_max(g_stripe_zone, g_stripe_maxmem / MAXPHYS);
137 g_stripe_fini(struct g_class *mp __unused)
140 uma_zdestroy(g_stripe_zone);
144 * Return the number of valid disks.
147 g_stripe_nvalid(struct g_stripe_softc *sc)
152 for (i = 0; i < sc->sc_ndisks; i++) {
153 if (sc->sc_disks[i] != NULL)
161 g_stripe_remove_disk(struct g_consumer *cp)
163 struct g_stripe_softc *sc;
166 KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
167 sc = (struct g_stripe_softc *)cp->geom->softc;
168 KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
170 if (cp->private == NULL) {
171 G_STRIPE_DEBUG(0, "Disk %s removed from %s.",
172 cp->provider->name, sc->sc_name);
173 cp->private = (void *)(uintptr_t)-1;
176 if (sc->sc_provider != NULL) {
177 G_STRIPE_DEBUG(0, "Device %s deactivated.",
178 sc->sc_provider->name);
179 g_wither_provider(sc->sc_provider, ENXIO);
180 sc->sc_provider = NULL;
183 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
185 sc->sc_disks[cp->index] = NULL;
188 g_destroy_consumer(cp);
189 /* If there are no valid disks anymore, remove device. */
190 if (LIST_EMPTY(&sc->sc_geom->consumer))
191 g_stripe_destroy(sc, 1);
195 g_stripe_orphan(struct g_consumer *cp)
197 struct g_stripe_softc *sc;
206 g_stripe_remove_disk(cp);
210 g_stripe_access(struct g_provider *pp, int dr, int dw, int de)
212 struct g_consumer *cp1, *cp2, *tmp;
213 struct g_stripe_softc *sc;
220 KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
222 /* On first open, grab an extra "exclusive" bit */
223 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
225 /* ... and let go of it on last close */
226 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
229 LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) {
230 error = g_access(cp1, dr, dw, de);
233 if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 &&
234 cp1->private != NULL) {
235 g_stripe_remove_disk(cp1); /* May destroy geom. */
241 LIST_FOREACH(cp2, &gp->consumer, consumer) {
244 g_access(cp2, -dr, -dw, -de);
250 g_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset,
251 off_t length, int mode)
256 stripesize = sc->sc_stripesize;
257 len = (size_t)(stripesize - (offset & (stripesize - 1)));
259 bcopy(src, dst, len);
261 dst += len + stripesize * (sc->sc_ndisks - 1);
265 src += len + stripesize * (sc->sc_ndisks - 1);
269 ("Length < 0 (stripesize=%zu, offset=%jd, length=%jd).",
270 (size_t)stripesize, (intmax_t)offset, (intmax_t)length));
271 if (length > stripesize)
275 } while (length > 0);
279 g_stripe_done(struct bio *bp)
281 struct g_stripe_softc *sc;
284 pbp = bp->bio_parent;
285 sc = pbp->bio_to->geom->softc;
286 if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) {
287 g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset,
289 bp->bio_data = bp->bio_caller1;
290 bp->bio_caller1 = NULL;
292 mtx_lock(&sc->sc_lock);
293 if (pbp->bio_error == 0)
294 pbp->bio_error = bp->bio_error;
295 pbp->bio_completed += bp->bio_completed;
297 if (pbp->bio_children == pbp->bio_inbed) {
298 mtx_unlock(&sc->sc_lock);
299 if (pbp->bio_driver1 != NULL)
300 uma_zfree(g_stripe_zone, pbp->bio_driver1);
301 g_io_deliver(pbp, pbp->bio_error);
303 mtx_unlock(&sc->sc_lock);
308 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
310 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
311 u_int nparts = 0, stripesize;
312 struct g_stripe_softc *sc;
313 char *addr, *data = NULL;
317 sc = bp->bio_to->geom->softc;
320 stripesize = sc->sc_stripesize;
322 cbp = g_clone_bio(bp);
327 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
330 * Fill in the component buf structure.
332 cbp->bio_done = g_stripe_done;
333 cbp->bio_offset = offset;
334 cbp->bio_data = addr;
335 cbp->bio_caller1 = NULL;
336 cbp->bio_length = length;
337 cbp->bio_caller2 = sc->sc_disks[no];
339 /* offset -= offset % stripesize; */
340 offset -= offset & (stripesize - 1);
342 length = bp->bio_length - length;
343 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
344 if (no > sc->sc_ndisks - 1) {
346 offset += stripesize;
348 if (nparts >= sc->sc_ndisks) {
349 cbp = TAILQ_NEXT(cbp, bio_queue);
351 cbp = TAILQ_FIRST(&queue);
354 * Update bio structure.
357 * MIN() is in case when
358 * (bp->bio_length % sc->sc_stripesize) != 0.
360 cbp->bio_length += MIN(stripesize, length);
361 if (cbp->bio_caller1 == NULL) {
362 cbp->bio_caller1 = cbp->bio_data;
363 cbp->bio_data = NULL;
365 data = uma_zalloc(g_stripe_zone,
374 cbp = g_clone_bio(bp);
379 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
382 * Fill in the component buf structure.
384 cbp->bio_done = g_stripe_done;
385 cbp->bio_offset = offset;
386 cbp->bio_data = addr;
387 cbp->bio_caller1 = NULL;
389 * MIN() is in case when
390 * (bp->bio_length % sc->sc_stripesize) != 0.
392 cbp->bio_length = MIN(stripesize, length);
393 cbp->bio_caller2 = sc->sc_disks[no];
397 bp->bio_driver1 = data;
399 * Fire off all allocated requests!
401 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
402 struct g_consumer *cp;
404 TAILQ_REMOVE(&queue, cbp, bio_queue);
405 cp = cbp->bio_caller2;
406 cbp->bio_caller2 = NULL;
407 cbp->bio_to = cp->provider;
408 if (cbp->bio_caller1 != NULL) {
409 cbp->bio_data = data;
410 if (bp->bio_cmd == BIO_WRITE) {
411 g_stripe_copy(sc, cbp->bio_caller1, data,
412 cbp->bio_offset, cbp->bio_length, 0);
414 data += cbp->bio_length;
416 G_STRIPE_LOGREQ(cbp, "Sending request.");
417 g_io_request(cbp, cp);
422 uma_zfree(g_stripe_zone, data);
423 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
424 TAILQ_REMOVE(&queue, cbp, bio_queue);
425 if (cbp->bio_caller1 != NULL) {
426 cbp->bio_data = cbp->bio_caller1;
427 cbp->bio_caller1 = NULL;
436 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
438 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
439 struct g_stripe_softc *sc;
445 sc = bp->bio_to->geom->softc;
447 stripesize = sc->sc_stripesize;
449 cbp = g_clone_bio(bp);
454 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
456 * Fill in the component buf structure.
458 if (bp->bio_length == length)
459 cbp->bio_done = g_std_done; /* Optimized lockless case. */
461 cbp->bio_done = g_stripe_done;
462 cbp->bio_offset = offset;
463 cbp->bio_length = length;
464 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
465 bp->bio_ma_n = round_page(bp->bio_ma_offset +
466 bp->bio_length) / PAGE_SIZE;
470 cbp->bio_caller2 = sc->sc_disks[no];
472 /* offset -= offset % stripesize; */
473 offset -= offset & (stripesize - 1);
474 if (bp->bio_cmd != BIO_DELETE)
476 length = bp->bio_length - length;
477 for (no++; length > 0; no++, length -= stripesize) {
478 if (no > sc->sc_ndisks - 1) {
480 offset += stripesize;
482 cbp = g_clone_bio(bp);
487 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
490 * Fill in the component buf structure.
492 cbp->bio_done = g_stripe_done;
493 cbp->bio_offset = offset;
495 * MIN() is in case when
496 * (bp->bio_length % sc->sc_stripesize) != 0.
498 cbp->bio_length = MIN(stripesize, length);
499 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
500 cbp->bio_ma_offset += (uintptr_t)addr;
501 cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
502 cbp->bio_ma_offset %= PAGE_SIZE;
503 cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
504 cbp->bio_length) / PAGE_SIZE;
506 cbp->bio_data = addr;
508 cbp->bio_caller2 = sc->sc_disks[no];
510 if (bp->bio_cmd != BIO_DELETE)
514 * Fire off all allocated requests!
516 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
517 struct g_consumer *cp;
519 TAILQ_REMOVE(&queue, cbp, bio_queue);
520 cp = cbp->bio_caller2;
521 cbp->bio_caller2 = NULL;
522 cbp->bio_to = cp->provider;
523 G_STRIPE_LOGREQ(cbp, "Sending request.");
524 g_io_request(cbp, cp);
528 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
529 TAILQ_REMOVE(&queue, cbp, bio_queue);
537 g_stripe_flush(struct g_stripe_softc *sc, struct bio *bp)
539 struct bio_queue_head queue;
540 struct g_consumer *cp;
545 for (no = 0; no < sc->sc_ndisks; no++) {
546 cbp = g_clone_bio(bp);
548 for (cbp = bioq_first(&queue); cbp != NULL;
549 cbp = bioq_first(&queue)) {
550 bioq_remove(&queue, cbp);
553 if (bp->bio_error == 0)
554 bp->bio_error = ENOMEM;
555 g_io_deliver(bp, bp->bio_error);
558 bioq_insert_tail(&queue, cbp);
559 cbp->bio_done = g_stripe_done;
560 cbp->bio_caller2 = sc->sc_disks[no];
561 cbp->bio_to = sc->sc_disks[no]->provider;
563 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
564 bioq_remove(&queue, cbp);
565 G_STRIPE_LOGREQ(cbp, "Sending request.");
566 cp = cbp->bio_caller2;
567 cbp->bio_caller2 = NULL;
568 g_io_request(cbp, cp);
573 g_stripe_start(struct bio *bp)
575 off_t offset, start, length, nstripe;
576 struct g_stripe_softc *sc;
577 u_int no, stripesize;
580 sc = bp->bio_to->geom->softc;
582 * If sc == NULL, provider's error should be set and g_stripe_start()
583 * should not be called at all.
586 ("Provider's error should be set (error=%d)(device=%s).",
587 bp->bio_to->error, bp->bio_to->name));
589 G_STRIPE_LOGREQ(bp, "Request received.");
591 switch (bp->bio_cmd) {
597 g_stripe_flush(sc, bp);
600 /* To which provider it should be delivered? */
602 g_io_deliver(bp, EOPNOTSUPP);
606 stripesize = sc->sc_stripesize;
609 * Calculations are quite messy, but fast I hope.
613 /* nstripe = bp->bio_offset / stripesize; */
614 nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
616 no = nstripe % sc->sc_ndisks;
617 /* Start position in stripe. */
618 /* start = bp->bio_offset % stripesize; */
619 start = bp->bio_offset & (stripesize - 1);
620 /* Start position in disk. */
621 /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
622 offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
623 /* Length of data to operate. */
624 length = MIN(bp->bio_length, stripesize - start);
627 * Do use "fast" mode when:
628 * 1. "Fast" mode is ON.
630 * 2. Request size is less than or equal to MAXPHYS,
631 * which should always be true.
633 * 3. Request size is bigger than stripesize * ndisks. If it isn't,
634 * there will be no need to send more than one I/O request to
635 * a provider, so there is nothing to optmize.
637 * 4. Request is not unmapped.
639 * 5. It is not a BIO_DELETE.
641 if (g_stripe_fast && bp->bio_length <= MAXPHYS &&
642 bp->bio_length >= stripesize * sc->sc_ndisks &&
643 (bp->bio_flags & BIO_UNMAPPED) == 0 &&
644 bp->bio_cmd != BIO_DELETE) {
649 error = g_stripe_start_fast(bp, no, offset, length);
651 g_stripe_fast_failed++;
654 * Do use "economic" when:
655 * 1. "Economic" mode is ON.
657 * 2. "Fast" mode failed. It can only fail if there is no memory.
659 if (!fast || error != 0)
660 error = g_stripe_start_economic(bp, no, offset, length);
662 if (bp->bio_error == 0)
663 bp->bio_error = error;
664 g_io_deliver(bp, bp->bio_error);
669 g_stripe_check_and_run(struct g_stripe_softc *sc)
671 struct g_provider *dp;
673 u_int no, sectorsize = 0;
676 if (g_stripe_nvalid(sc) != sc->sc_ndisks)
679 sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
681 sc->sc_provider->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
682 if (g_stripe_fast == 0)
683 sc->sc_provider->flags |= G_PF_ACCEPT_UNMAPPED;
685 * Find the smallest disk.
687 mediasize = sc->sc_disks[0]->provider->mediasize;
688 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
689 mediasize -= sc->sc_disks[0]->provider->sectorsize;
690 mediasize -= mediasize % sc->sc_stripesize;
691 sectorsize = sc->sc_disks[0]->provider->sectorsize;
692 for (no = 1; no < sc->sc_ndisks; no++) {
693 dp = sc->sc_disks[no]->provider;
695 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
696 ms -= dp->sectorsize;
697 ms -= ms % sc->sc_stripesize;
700 sectorsize = lcm(sectorsize, dp->sectorsize);
702 /* A provider underneath us doesn't support unmapped */
703 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
704 G_STRIPE_DEBUG(1, "Cancelling unmapped "
705 "because of %s.", dp->name);
706 sc->sc_provider->flags &= ~G_PF_ACCEPT_UNMAPPED;
709 sc->sc_provider->sectorsize = sectorsize;
710 sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
711 sc->sc_provider->stripesize = sc->sc_stripesize;
712 sc->sc_provider->stripeoffset = 0;
713 g_error_provider(sc->sc_provider, 0);
715 G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
719 g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
721 struct g_provider *pp;
727 error = g_access(cp, 1, 0, 0);
732 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
735 g_access(cp, -1, 0, 0);
739 /* Decode metadata. */
740 stripe_metadata_decode(buf, md);
747 * Add disk to given device.
750 g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
752 struct g_consumer *cp, *fcp;
757 /* Metadata corrupted? */
758 if (no >= sc->sc_ndisks)
761 /* Check if disk is not already attached. */
762 if (sc->sc_disks[no] != NULL)
766 fcp = LIST_FIRST(&gp->consumer);
768 cp = g_new_consumer(gp);
769 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
772 error = g_attach(cp, pp);
774 g_destroy_consumer(cp);
778 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
779 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
782 g_destroy_consumer(cp);
786 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
787 struct g_stripe_metadata md;
789 /* Reread metadata. */
790 error = g_stripe_read_metadata(cp, &md);
794 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
795 strcmp(md.md_name, sc->sc_name) != 0 ||
796 md.md_id != sc->sc_id) {
797 G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
802 sc->sc_disks[no] = cp;
803 G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
804 g_stripe_check_and_run(sc);
808 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
809 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
811 g_destroy_consumer(cp);
815 static struct g_geom *
816 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
819 struct g_stripe_softc *sc;
824 G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
827 /* Two disks is minimum. */
828 if (md->md_all < 2) {
829 G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
833 /* Stripe size have to be grater than or equal to sector size. */
834 if (md->md_stripesize < sectorsize) {
835 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
839 /* Stripe size have to be power of 2. */
840 if (!powerof2(md->md_stripesize)) {
841 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
845 /* Check for duplicate unit */
846 LIST_FOREACH(gp, &mp->geom, geom) {
848 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
849 G_STRIPE_DEBUG(0, "Device %s already configured.",
854 gp = g_new_geomf(mp, "%s", md->md_name);
855 sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
856 gp->start = g_stripe_start;
857 gp->spoiled = g_stripe_orphan;
858 gp->orphan = g_stripe_orphan;
859 gp->access = g_stripe_access;
860 gp->dumpconf = g_stripe_dumpconf;
862 sc->sc_id = md->md_id;
863 sc->sc_stripesize = md->md_stripesize;
864 sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
865 sc->sc_ndisks = md->md_all;
866 sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
867 M_STRIPE, M_WAITOK | M_ZERO);
868 for (no = 0; no < sc->sc_ndisks; no++)
869 sc->sc_disks[no] = NULL;
871 mtx_init(&sc->sc_lock, "gstripe lock", NULL, MTX_DEF);
875 sc->sc_provider = NULL;
877 G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
883 g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
885 struct g_provider *pp;
886 struct g_consumer *cp, *cp1;
894 pp = sc->sc_provider;
895 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
897 G_STRIPE_DEBUG(0, "Device %s is still open, so it "
898 "can't be definitely removed.", pp->name);
901 "Device %s is still open (r%dw%de%d).", pp->name,
902 pp->acr, pp->acw, pp->ace);
908 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
909 g_stripe_remove_disk(cp);
911 return (0); /* Recursion happened. */
913 if (!LIST_EMPTY(&gp->consumer))
914 return (EINPROGRESS);
917 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
919 free(sc->sc_disks, M_STRIPE);
920 mtx_destroy(&sc->sc_lock);
922 G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
923 g_wither_geom(gp, ENXIO);
928 g_stripe_destroy_geom(struct gctl_req *req __unused,
929 struct g_class *mp __unused, struct g_geom *gp)
931 struct g_stripe_softc *sc;
934 return (g_stripe_destroy(sc, 0));
937 static struct g_geom *
938 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
940 struct g_stripe_metadata md;
941 struct g_stripe_softc *sc;
942 struct g_consumer *cp;
946 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
949 /* Skip providers that are already open for writing. */
953 G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
955 gp = g_new_geomf(mp, "stripe:taste");
956 gp->start = g_stripe_start;
957 gp->access = g_stripe_access;
958 gp->orphan = g_stripe_orphan;
959 cp = g_new_consumer(gp);
961 error = g_stripe_read_metadata(cp, &md);
963 g_destroy_consumer(cp);
969 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
971 if (md.md_version > G_STRIPE_VERSION) {
972 printf("geom_stripe.ko module is too old to handle %s.\n",
977 * Backward compatibility:
979 /* There was no md_provider field in earlier versions of metadata. */
980 if (md.md_version < 2)
981 bzero(md.md_provider, sizeof(md.md_provider));
982 /* There was no md_provsize field in earlier versions of metadata. */
983 if (md.md_version < 3)
984 md.md_provsize = pp->mediasize;
986 if (md.md_provider[0] != '\0' &&
987 !g_compare_names(md.md_provider, pp->name))
989 if (md.md_provsize != pp->mediasize)
993 * Let's check if device already exists.
996 LIST_FOREACH(gp, &mp->geom, geom) {
1000 if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
1002 if (strcmp(md.md_name, sc->sc_name) != 0)
1004 if (md.md_id != sc->sc_id)
1009 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1010 error = g_stripe_add_disk(sc, pp, md.md_no);
1013 "Cannot add disk %s to %s (error=%d).", pp->name,
1018 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
1020 G_STRIPE_DEBUG(0, "Cannot create device %s.",
1025 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1026 error = g_stripe_add_disk(sc, pp, md.md_no);
1029 "Cannot add disk %s to %s (error=%d).", pp->name,
1031 g_stripe_destroy(sc, 1);
1040 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1043 struct g_stripe_metadata md;
1044 struct g_provider *pp;
1045 struct g_stripe_softc *sc;
1048 intmax_t *stripesize;
1053 g_topology_assert();
1054 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1055 if (nargs == NULL) {
1056 gctl_error(req, "No '%s' argument.", "nargs");
1060 gctl_error(req, "Too few arguments.");
1064 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1065 md.md_version = G_STRIPE_VERSION;
1066 name = gctl_get_asciiparam(req, "arg0");
1068 gctl_error(req, "No 'arg%u' argument.", 0);
1071 strlcpy(md.md_name, name, sizeof(md.md_name));
1072 md.md_id = arc4random();
1074 md.md_all = *nargs - 1;
1075 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1076 if (stripesize == NULL) {
1077 gctl_error(req, "No '%s' argument.", "stripesize");
1080 md.md_stripesize = *stripesize;
1081 bzero(md.md_provider, sizeof(md.md_provider));
1082 /* This field is not important here. */
1085 /* Check all providers are valid */
1086 for (no = 1; no < *nargs; no++) {
1087 snprintf(param, sizeof(param), "arg%u", no);
1088 name = gctl_get_asciiparam(req, param);
1090 gctl_error(req, "No 'arg%u' argument.", no);
1093 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1094 name += strlen("/dev/");
1095 pp = g_provider_by_name(name);
1097 G_STRIPE_DEBUG(1, "Disk %s is invalid.", name);
1098 gctl_error(req, "Disk %s is invalid.", name);
1103 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1105 gctl_error(req, "Can't configure %s.", md.md_name);
1110 sb = sbuf_new_auto();
1111 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1112 for (attached = 0, no = 1; no < *nargs; no++) {
1113 snprintf(param, sizeof(param), "arg%u", no);
1114 name = gctl_get_asciiparam(req, param);
1116 gctl_error(req, "No 'arg%u' argument.", no);
1119 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1120 name += strlen("/dev/");
1121 pp = g_provider_by_name(name);
1122 KASSERT(pp != NULL, ("Provider %s disappear?!", name));
1123 if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1124 G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1125 no, pp->name, gp->name);
1126 sbuf_printf(sb, " %s", pp->name);
1132 if (md.md_all != attached) {
1133 g_stripe_destroy(gp->softc, 1);
1134 gctl_error(req, "%s", sbuf_data(sb));
1139 static struct g_stripe_softc *
1140 g_stripe_find_device(struct g_class *mp, const char *name)
1142 struct g_stripe_softc *sc;
1145 LIST_FOREACH(gp, &mp->geom, geom) {
1149 if (strcmp(sc->sc_name, name) == 0)
1156 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1158 struct g_stripe_softc *sc;
1159 int *force, *nargs, error;
1164 g_topology_assert();
1166 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1167 if (nargs == NULL) {
1168 gctl_error(req, "No '%s' argument.", "nargs");
1172 gctl_error(req, "Missing device(s).");
1175 force = gctl_get_paraml(req, "force", sizeof(*force));
1176 if (force == NULL) {
1177 gctl_error(req, "No '%s' argument.", "force");
1181 for (i = 0; i < (u_int)*nargs; i++) {
1182 snprintf(param, sizeof(param), "arg%u", i);
1183 name = gctl_get_asciiparam(req, param);
1185 gctl_error(req, "No 'arg%u' argument.", i);
1188 sc = g_stripe_find_device(mp, name);
1190 gctl_error(req, "No such device: %s.", name);
1193 error = g_stripe_destroy(sc, *force);
1195 gctl_error(req, "Cannot destroy device %s (error=%d).",
1196 sc->sc_name, error);
1203 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1207 g_topology_assert();
1209 version = gctl_get_paraml(req, "version", sizeof(*version));
1210 if (version == NULL) {
1211 gctl_error(req, "No '%s' argument.", "version");
1214 if (*version != G_STRIPE_VERSION) {
1215 gctl_error(req, "Userland and kernel parts are out of sync.");
1219 if (strcmp(verb, "create") == 0) {
1220 g_stripe_ctl_create(req, mp);
1222 } else if (strcmp(verb, "destroy") == 0 ||
1223 strcmp(verb, "stop") == 0) {
1224 g_stripe_ctl_destroy(req, mp);
1228 gctl_error(req, "Unknown verb.");
1232 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1233 struct g_consumer *cp, struct g_provider *pp)
1235 struct g_stripe_softc *sc;
1242 } else if (cp != NULL) {
1243 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1246 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1247 sbuf_printf(sb, "%s<Stripesize>%u</Stripesize>\n", indent,
1248 (u_int)sc->sc_stripesize);
1249 sbuf_printf(sb, "%s<Type>", indent);
1250 switch (sc->sc_type) {
1251 case G_STRIPE_TYPE_AUTOMATIC:
1252 sbuf_printf(sb, "AUTOMATIC");
1254 case G_STRIPE_TYPE_MANUAL:
1255 sbuf_printf(sb, "MANUAL");
1258 sbuf_printf(sb, "UNKNOWN");
1261 sbuf_printf(sb, "</Type>\n");
1262 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1263 indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1264 sbuf_printf(sb, "%s<State>", indent);
1265 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1266 sbuf_printf(sb, "UP");
1268 sbuf_printf(sb, "DOWN");
1269 sbuf_printf(sb, "</State>\n");
1273 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);