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 SYSCTL_NODE(_kern_geom, OID_AUTO, stripe, CTLFLAG_RW, 0, "GEOM_STRIPE stuff");
72 static u_int g_stripe_debug = 0;
73 TUNABLE_INT("kern.geom.stripe.debug", &g_stripe_debug);
74 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, debug, CTLFLAG_RW, &g_stripe_debug, 0,
76 static int g_stripe_fast = 0;
77 TUNABLE_INT("kern.geom.stripe.fast", &g_stripe_fast);
79 g_sysctl_stripe_fast(SYSCTL_HANDLER_ARGS)
84 error = sysctl_handle_int(oidp, &fast, 0, req);
85 if (error == 0 && req->newptr != NULL)
89 SYSCTL_PROC(_kern_geom_stripe, OID_AUTO, fast, CTLTYPE_INT | CTLFLAG_RW,
90 NULL, 0, g_sysctl_stripe_fast, "I", "Fast, but memory-consuming, mode");
91 static u_int g_stripe_maxmem = MAXPHYS * 100;
92 TUNABLE_INT("kern.geom.stripe.maxmem", &g_stripe_maxmem);
93 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, maxmem, CTLFLAG_RD, &g_stripe_maxmem,
94 0, "Maximum memory that can be allocated in \"fast\" mode (in bytes)");
95 static u_int g_stripe_fast_failed = 0;
96 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, fast_failed, CTLFLAG_RD,
97 &g_stripe_fast_failed, 0, "How many times \"fast\" mode failed");
100 * Greatest Common Divisor.
103 gcd(u_int a, u_int b)
116 * Least Common Multiple.
119 lcm(u_int a, u_int b)
122 return ((a * b) / gcd(a, b));
126 g_stripe_init(struct g_class *mp __unused)
129 g_stripe_zone = uma_zcreate("g_stripe_zone", MAXPHYS, NULL, NULL,
131 g_stripe_maxmem -= g_stripe_maxmem % MAXPHYS;
132 uma_zone_set_max(g_stripe_zone, g_stripe_maxmem / MAXPHYS);
136 g_stripe_fini(struct g_class *mp __unused)
139 uma_zdestroy(g_stripe_zone);
143 * Return the number of valid disks.
146 g_stripe_nvalid(struct g_stripe_softc *sc)
151 for (i = 0; i < sc->sc_ndisks; i++) {
152 if (sc->sc_disks[i] != NULL)
160 g_stripe_remove_disk(struct g_consumer *cp)
162 struct g_stripe_softc *sc;
165 KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
166 sc = (struct g_stripe_softc *)cp->private;
167 KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
170 G_STRIPE_DEBUG(0, "Disk %s removed from %s.", cp->provider->name,
173 sc->sc_disks[no] = NULL;
174 if (sc->sc_provider != NULL) {
175 sc->sc_provider->flags |= G_PF_WITHER;
176 g_orphan_provider(sc->sc_provider, ENXIO);
177 sc->sc_provider = NULL;
178 G_STRIPE_DEBUG(0, "Device %s removed.", sc->sc_name);
181 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
182 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
184 g_destroy_consumer(cp);
188 g_stripe_orphan(struct g_consumer *cp)
190 struct g_stripe_softc *sc;
199 g_stripe_remove_disk(cp);
200 /* If there are no valid disks anymore, remove device. */
201 if (g_stripe_nvalid(sc) == 0)
202 g_stripe_destroy(sc, 1);
206 g_stripe_access(struct g_provider *pp, int dr, int dw, int de)
208 struct g_consumer *cp1, *cp2;
209 struct g_stripe_softc *sc;
218 * It looks like geom is being withered.
219 * In that case we allow only negative requests.
221 KASSERT(dr <= 0 && dw <= 0 && de <= 0,
222 ("Positive access request (device=%s).", pp->name));
223 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 &&
224 (pp->ace + de) == 0) {
225 G_STRIPE_DEBUG(0, "Device %s definitely destroyed.",
231 /* On first open, grab an extra "exclusive" bit */
232 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
234 /* ... and let go of it on last close */
235 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
239 LIST_FOREACH(cp1, &gp->consumer, consumer) {
240 error = g_access(cp1, dr, dw, de);
244 * If we fail here, backout all previous changes.
246 LIST_FOREACH(cp2, &gp->consumer, consumer) {
249 g_access(cp2, -dr, -dw, -de);
258 g_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset,
259 off_t length, int mode)
264 stripesize = sc->sc_stripesize;
265 len = (size_t)(stripesize - (offset & (stripesize - 1)));
267 bcopy(src, dst, len);
269 dst += len + stripesize * (sc->sc_ndisks - 1);
273 src += len + stripesize * (sc->sc_ndisks - 1);
277 ("Length < 0 (stripesize=%zu, offset=%jd, length=%jd).",
278 (size_t)stripesize, (intmax_t)offset, (intmax_t)length));
279 if (length > stripesize)
283 } while (length > 0);
287 g_stripe_done(struct bio *bp)
289 struct g_stripe_softc *sc;
292 pbp = bp->bio_parent;
293 sc = pbp->bio_to->geom->softc;
294 if (pbp->bio_error == 0)
295 pbp->bio_error = bp->bio_error;
296 pbp->bio_completed += bp->bio_completed;
297 if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) {
298 g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset,
300 bp->bio_data = bp->bio_caller1;
301 bp->bio_caller1 = NULL;
305 if (pbp->bio_children == pbp->bio_inbed) {
306 if (pbp->bio_driver1 != NULL)
307 uma_zfree(g_stripe_zone, pbp->bio_driver1);
308 g_io_deliver(pbp, pbp->bio_error);
313 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
315 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
316 u_int nparts = 0, stripesize;
317 struct g_stripe_softc *sc;
318 char *addr, *data = NULL;
322 sc = bp->bio_to->geom->softc;
325 stripesize = sc->sc_stripesize;
327 cbp = g_clone_bio(bp);
332 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
335 * Fill in the component buf structure.
337 cbp->bio_done = g_stripe_done;
338 cbp->bio_offset = offset;
339 cbp->bio_data = addr;
340 cbp->bio_caller1 = NULL;
341 cbp->bio_length = length;
342 cbp->bio_caller2 = sc->sc_disks[no];
344 /* offset -= offset % stripesize; */
345 offset -= offset & (stripesize - 1);
347 length = bp->bio_length - length;
348 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
349 if (no > sc->sc_ndisks - 1) {
351 offset += stripesize;
353 if (nparts >= sc->sc_ndisks) {
354 cbp = TAILQ_NEXT(cbp, bio_queue);
356 cbp = TAILQ_FIRST(&queue);
359 * Update bio structure.
362 * MIN() is in case when
363 * (bp->bio_length % sc->sc_stripesize) != 0.
365 cbp->bio_length += MIN(stripesize, length);
366 if (cbp->bio_caller1 == NULL) {
367 cbp->bio_caller1 = cbp->bio_data;
368 cbp->bio_data = NULL;
370 data = uma_zalloc(g_stripe_zone,
379 cbp = g_clone_bio(bp);
384 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
387 * Fill in the component buf structure.
389 cbp->bio_done = g_stripe_done;
390 cbp->bio_offset = offset;
391 cbp->bio_data = addr;
392 cbp->bio_caller1 = NULL;
394 * MIN() is in case when
395 * (bp->bio_length % sc->sc_stripesize) != 0.
397 cbp->bio_length = MIN(stripesize, length);
398 cbp->bio_caller2 = sc->sc_disks[no];
402 bp->bio_driver1 = data;
404 * Fire off all allocated requests!
406 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
407 struct g_consumer *cp;
409 TAILQ_REMOVE(&queue, cbp, bio_queue);
410 cp = cbp->bio_caller2;
411 cbp->bio_caller2 = NULL;
412 cbp->bio_to = cp->provider;
413 if (cbp->bio_caller1 != NULL) {
414 cbp->bio_data = data;
415 if (bp->bio_cmd == BIO_WRITE) {
416 g_stripe_copy(sc, cbp->bio_caller1, data,
417 cbp->bio_offset, cbp->bio_length, 0);
419 data += cbp->bio_length;
421 G_STRIPE_LOGREQ(cbp, "Sending request.");
422 g_io_request(cbp, cp);
427 uma_zfree(g_stripe_zone, data);
428 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
429 TAILQ_REMOVE(&queue, cbp, bio_queue);
430 if (cbp->bio_caller1 != NULL) {
431 cbp->bio_data = cbp->bio_caller1;
432 cbp->bio_caller1 = NULL;
441 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
443 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
444 struct g_stripe_softc *sc;
450 sc = bp->bio_to->geom->softc;
453 stripesize = sc->sc_stripesize;
455 cbp = g_clone_bio(bp);
460 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
462 * Fill in the component buf structure.
464 cbp->bio_done = g_std_done;
465 cbp->bio_offset = offset;
466 cbp->bio_data = addr;
467 cbp->bio_length = length;
468 cbp->bio_caller2 = sc->sc_disks[no];
470 /* offset -= offset % stripesize; */
471 offset -= offset & (stripesize - 1);
473 length = bp->bio_length - length;
474 for (no++; length > 0; no++, length -= stripesize, addr += 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_std_done;
490 cbp->bio_offset = offset;
491 cbp->bio_data = addr;
493 * MIN() is in case when
494 * (bp->bio_length % sc->sc_stripesize) != 0.
496 cbp->bio_length = MIN(stripesize, length);
498 cbp->bio_caller2 = sc->sc_disks[no];
501 * Fire off all allocated requests!
503 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
504 struct g_consumer *cp;
506 TAILQ_REMOVE(&queue, cbp, bio_queue);
507 cp = cbp->bio_caller2;
508 cbp->bio_caller2 = NULL;
509 cbp->bio_to = cp->provider;
510 G_STRIPE_LOGREQ(cbp, "Sending request.");
511 g_io_request(cbp, cp);
515 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
516 TAILQ_REMOVE(&queue, cbp, bio_queue);
524 g_stripe_flush(struct g_stripe_softc *sc, struct bio *bp)
526 struct bio_queue_head queue;
527 struct g_consumer *cp;
532 for (no = 0; no < sc->sc_ndisks; no++) {
533 cbp = g_clone_bio(bp);
535 for (cbp = bioq_first(&queue); cbp != NULL;
536 cbp = bioq_first(&queue)) {
537 bioq_remove(&queue, cbp);
540 if (bp->bio_error == 0)
541 bp->bio_error = ENOMEM;
542 g_io_deliver(bp, bp->bio_error);
545 bioq_insert_tail(&queue, cbp);
546 cbp->bio_done = g_std_done;
547 cbp->bio_caller1 = sc->sc_disks[no];
548 cbp->bio_to = sc->sc_disks[no]->provider;
550 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
551 bioq_remove(&queue, cbp);
552 G_STRIPE_LOGREQ(cbp, "Sending request.");
553 cp = cbp->bio_caller1;
554 cbp->bio_caller1 = NULL;
555 g_io_request(cbp, cp);
560 g_stripe_start(struct bio *bp)
562 off_t offset, start, length, nstripe;
563 struct g_stripe_softc *sc;
564 u_int no, stripesize;
567 sc = bp->bio_to->geom->softc;
569 * If sc == NULL, provider's error should be set and g_stripe_start()
570 * should not be called at all.
573 ("Provider's error should be set (error=%d)(device=%s).",
574 bp->bio_to->error, bp->bio_to->name));
576 G_STRIPE_LOGREQ(bp, "Request received.");
578 switch (bp->bio_cmd) {
584 g_stripe_flush(sc, bp);
587 /* To which provider it should be delivered? */
589 g_io_deliver(bp, EOPNOTSUPP);
593 stripesize = sc->sc_stripesize;
596 * Calculations are quite messy, but fast I hope.
600 /* nstripe = bp->bio_offset / stripesize; */
601 nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
603 no = nstripe % sc->sc_ndisks;
604 /* Start position in stripe. */
605 /* start = bp->bio_offset % stripesize; */
606 start = bp->bio_offset & (stripesize - 1);
607 /* Start position in disk. */
608 /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
609 offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
610 /* Length of data to operate. */
611 length = MIN(bp->bio_length, stripesize - start);
614 * Do use "fast" mode when:
615 * 1. "Fast" mode is ON.
617 * 2. Request size is less than or equal to MAXPHYS,
618 * which should always be true.
620 * 3. Request size is bigger than stripesize * ndisks. If it isn't,
621 * there will be no need to send more than one I/O request to
622 * a provider, so there is nothing to optmize.
624 if (g_stripe_fast && bp->bio_length <= MAXPHYS &&
625 bp->bio_length >= stripesize * sc->sc_ndisks) {
630 error = g_stripe_start_fast(bp, no, offset, length);
632 g_stripe_fast_failed++;
635 * Do use "economic" when:
636 * 1. "Economic" mode is ON.
638 * 2. "Fast" mode failed. It can only fail if there is no memory.
640 if (!fast || error != 0)
641 error = g_stripe_start_economic(bp, no, offset, length);
643 if (bp->bio_error == 0)
644 bp->bio_error = error;
645 g_io_deliver(bp, bp->bio_error);
650 g_stripe_check_and_run(struct g_stripe_softc *sc)
653 u_int no, sectorsize = 0;
655 if (g_stripe_nvalid(sc) != sc->sc_ndisks)
658 sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
661 * Find the smallest disk.
663 mediasize = sc->sc_disks[0]->provider->mediasize;
664 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
665 mediasize -= sc->sc_disks[0]->provider->sectorsize;
666 mediasize -= mediasize % sc->sc_stripesize;
667 sectorsize = sc->sc_disks[0]->provider->sectorsize;
668 for (no = 1; no < sc->sc_ndisks; no++) {
669 ms = sc->sc_disks[no]->provider->mediasize;
670 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
671 ms -= sc->sc_disks[no]->provider->sectorsize;
672 ms -= ms % sc->sc_stripesize;
675 sectorsize = lcm(sectorsize,
676 sc->sc_disks[no]->provider->sectorsize);
678 sc->sc_provider->sectorsize = sectorsize;
679 sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
680 sc->sc_provider->stripesize = sc->sc_stripesize;
681 sc->sc_provider->stripeoffset = 0;
682 g_error_provider(sc->sc_provider, 0);
684 G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_name);
688 g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
690 struct g_provider *pp;
696 error = g_access(cp, 1, 0, 0);
701 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
704 g_access(cp, -1, 0, 0);
708 /* Decode metadata. */
709 stripe_metadata_decode(buf, md);
716 * Add disk to given device.
719 g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
721 struct g_consumer *cp, *fcp;
725 /* Metadata corrupted? */
726 if (no >= sc->sc_ndisks)
729 /* Check if disk is not already attached. */
730 if (sc->sc_disks[no] != NULL)
734 fcp = LIST_FIRST(&gp->consumer);
736 cp = g_new_consumer(gp);
737 error = g_attach(cp, pp);
739 g_destroy_consumer(cp);
743 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
744 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
747 g_destroy_consumer(cp);
751 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
752 struct g_stripe_metadata md;
754 /* Reread metadata. */
755 error = g_stripe_read_metadata(cp, &md);
759 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
760 strcmp(md.md_name, sc->sc_name) != 0 ||
761 md.md_id != sc->sc_id) {
762 G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
769 sc->sc_disks[no] = cp;
771 G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
773 g_stripe_check_and_run(sc);
777 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
778 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
780 g_destroy_consumer(cp);
784 static struct g_geom *
785 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
788 struct g_stripe_softc *sc;
792 G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
795 /* Two disks is minimum. */
796 if (md->md_all < 2) {
797 G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
801 /* Stripe size have to be grater than or equal to sector size. */
802 if (md->md_stripesize < sectorsize) {
803 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
807 /* Stripe size have to be power of 2. */
808 if (!powerof2(md->md_stripesize)) {
809 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
813 /* Check for duplicate unit */
814 LIST_FOREACH(gp, &mp->geom, geom) {
816 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
817 G_STRIPE_DEBUG(0, "Device %s already configured.",
822 gp = g_new_geomf(mp, "%s", md->md_name);
823 sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
824 gp->start = g_stripe_start;
825 gp->spoiled = g_stripe_orphan;
826 gp->orphan = g_stripe_orphan;
827 gp->access = g_stripe_access;
828 gp->dumpconf = g_stripe_dumpconf;
830 sc->sc_id = md->md_id;
831 sc->sc_stripesize = md->md_stripesize;
832 sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
833 sc->sc_ndisks = md->md_all;
834 sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
835 M_STRIPE, M_WAITOK | M_ZERO);
836 for (no = 0; no < sc->sc_ndisks; no++)
837 sc->sc_disks[no] = NULL;
842 sc->sc_provider = NULL;
844 G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
850 g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
852 struct g_provider *pp;
861 pp = sc->sc_provider;
862 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
864 G_STRIPE_DEBUG(0, "Device %s is still open, so it "
865 "can't be definitely removed.", pp->name);
868 "Device %s is still open (r%dw%de%d).", pp->name,
869 pp->acr, pp->acw, pp->ace);
874 for (no = 0; no < sc->sc_ndisks; no++) {
875 if (sc->sc_disks[no] != NULL)
876 g_stripe_remove_disk(sc->sc_disks[no]);
881 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
883 free(sc->sc_disks, M_STRIPE);
886 pp = LIST_FIRST(&gp->provider);
887 if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
888 G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
890 g_wither_geom(gp, ENXIO);
896 g_stripe_destroy_geom(struct gctl_req *req __unused,
897 struct g_class *mp __unused, struct g_geom *gp)
899 struct g_stripe_softc *sc;
902 return (g_stripe_destroy(sc, 0));
905 static struct g_geom *
906 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
908 struct g_stripe_metadata md;
909 struct g_stripe_softc *sc;
910 struct g_consumer *cp;
914 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
917 /* Skip providers that are already open for writing. */
921 G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
923 gp = g_new_geomf(mp, "stripe:taste");
924 gp->start = g_stripe_start;
925 gp->access = g_stripe_access;
926 gp->orphan = g_stripe_orphan;
927 cp = g_new_consumer(gp);
929 error = g_stripe_read_metadata(cp, &md);
931 g_destroy_consumer(cp);
937 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
939 if (md.md_version > G_STRIPE_VERSION) {
940 printf("geom_stripe.ko module is too old to handle %s.\n",
945 * Backward compatibility:
947 /* There was no md_provider field in earlier versions of metadata. */
948 if (md.md_version < 2)
949 bzero(md.md_provider, sizeof(md.md_provider));
950 /* There was no md_provsize field in earlier versions of metadata. */
951 if (md.md_version < 3)
952 md.md_provsize = pp->mediasize;
954 if (md.md_provider[0] != '\0' &&
955 !g_compare_names(md.md_provider, pp->name))
957 if (md.md_provsize != pp->mediasize)
961 * Let's check if device already exists.
964 LIST_FOREACH(gp, &mp->geom, geom) {
968 if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
970 if (strcmp(md.md_name, sc->sc_name) != 0)
972 if (md.md_id != sc->sc_id)
977 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
978 error = g_stripe_add_disk(sc, pp, md.md_no);
981 "Cannot add disk %s to %s (error=%d).", pp->name,
986 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
988 G_STRIPE_DEBUG(0, "Cannot create device %s.",
993 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
994 error = g_stripe_add_disk(sc, pp, md.md_no);
997 "Cannot add disk %s to %s (error=%d).", pp->name,
999 g_stripe_destroy(sc, 1);
1008 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1011 struct g_stripe_metadata md;
1012 struct g_provider *pp;
1013 struct g_stripe_softc *sc;
1016 intmax_t *stripesize;
1021 g_topology_assert();
1022 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1023 if (nargs == NULL) {
1024 gctl_error(req, "No '%s' argument.", "nargs");
1028 gctl_error(req, "Too few arguments.");
1032 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1033 md.md_version = G_STRIPE_VERSION;
1034 name = gctl_get_asciiparam(req, "arg0");
1036 gctl_error(req, "No 'arg%u' argument.", 0);
1039 strlcpy(md.md_name, name, sizeof(md.md_name));
1040 md.md_id = arc4random();
1042 md.md_all = *nargs - 1;
1043 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1044 if (stripesize == NULL) {
1045 gctl_error(req, "No '%s' argument.", "stripesize");
1048 md.md_stripesize = *stripesize;
1049 bzero(md.md_provider, sizeof(md.md_provider));
1050 /* This field is not important here. */
1053 /* Check all providers are valid */
1054 for (no = 1; no < *nargs; no++) {
1055 snprintf(param, sizeof(param), "arg%u", no);
1056 name = gctl_get_asciiparam(req, param);
1058 gctl_error(req, "No 'arg%u' argument.", no);
1061 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1062 name += strlen("/dev/");
1063 pp = g_provider_by_name(name);
1065 G_STRIPE_DEBUG(1, "Disk %s is invalid.", name);
1066 gctl_error(req, "Disk %s is invalid.", name);
1071 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1073 gctl_error(req, "Can't configure %s.", md.md_name);
1078 sb = sbuf_new_auto();
1079 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1080 for (attached = 0, no = 1; no < *nargs; no++) {
1081 snprintf(param, sizeof(param), "arg%u", no);
1082 name = gctl_get_asciiparam(req, param);
1084 gctl_error(req, "No 'arg%u' argument.", no);
1087 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1088 name += strlen("/dev/");
1089 pp = g_provider_by_name(name);
1090 KASSERT(pp != NULL, ("Provider %s disappear?!", name));
1091 if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1092 G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1093 no, pp->name, gp->name);
1094 sbuf_printf(sb, " %s", pp->name);
1100 if (md.md_all != attached) {
1101 g_stripe_destroy(gp->softc, 1);
1102 gctl_error(req, "%s", sbuf_data(sb));
1107 static struct g_stripe_softc *
1108 g_stripe_find_device(struct g_class *mp, const char *name)
1110 struct g_stripe_softc *sc;
1113 LIST_FOREACH(gp, &mp->geom, geom) {
1117 if (strcmp(sc->sc_name, name) == 0)
1124 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1126 struct g_stripe_softc *sc;
1127 int *force, *nargs, error;
1132 g_topology_assert();
1134 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1135 if (nargs == NULL) {
1136 gctl_error(req, "No '%s' argument.", "nargs");
1140 gctl_error(req, "Missing device(s).");
1143 force = gctl_get_paraml(req, "force", sizeof(*force));
1144 if (force == NULL) {
1145 gctl_error(req, "No '%s' argument.", "force");
1149 for (i = 0; i < (u_int)*nargs; i++) {
1150 snprintf(param, sizeof(param), "arg%u", i);
1151 name = gctl_get_asciiparam(req, param);
1153 gctl_error(req, "No 'arg%u' argument.", i);
1156 sc = g_stripe_find_device(mp, name);
1158 gctl_error(req, "No such device: %s.", name);
1161 error = g_stripe_destroy(sc, *force);
1163 gctl_error(req, "Cannot destroy device %s (error=%d).",
1164 sc->sc_name, error);
1171 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1175 g_topology_assert();
1177 version = gctl_get_paraml(req, "version", sizeof(*version));
1178 if (version == NULL) {
1179 gctl_error(req, "No '%s' argument.", "version");
1182 if (*version != G_STRIPE_VERSION) {
1183 gctl_error(req, "Userland and kernel parts are out of sync.");
1187 if (strcmp(verb, "create") == 0) {
1188 g_stripe_ctl_create(req, mp);
1190 } else if (strcmp(verb, "destroy") == 0 ||
1191 strcmp(verb, "stop") == 0) {
1192 g_stripe_ctl_destroy(req, mp);
1196 gctl_error(req, "Unknown verb.");
1200 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1201 struct g_consumer *cp, struct g_provider *pp)
1203 struct g_stripe_softc *sc;
1210 } else if (cp != NULL) {
1211 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1214 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1215 sbuf_printf(sb, "%s<Stripesize>%u</Stripesize>\n", indent,
1216 (u_int)sc->sc_stripesize);
1217 sbuf_printf(sb, "%s<Type>", indent);
1218 switch (sc->sc_type) {
1219 case G_STRIPE_TYPE_AUTOMATIC:
1220 sbuf_printf(sb, "AUTOMATIC");
1222 case G_STRIPE_TYPE_MANUAL:
1223 sbuf_printf(sb, "MANUAL");
1226 sbuf_printf(sb, "UNKNOWN");
1229 sbuf_printf(sb, "</Type>\n");
1230 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1231 indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1232 sbuf_printf(sb, "%s<State>", indent);
1233 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1234 sbuf_printf(sb, "UP");
1236 sbuf_printf(sb, "DOWN");
1237 sbuf_printf(sb, "</State>\n");
1241 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);