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->private;
168 KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
171 G_STRIPE_DEBUG(0, "Disk %s removed from %s.", cp->provider->name,
174 sc->sc_disks[no] = NULL;
175 if (sc->sc_provider != NULL) {
176 sc->sc_provider->flags |= G_PF_WITHER;
177 g_orphan_provider(sc->sc_provider, ENXIO);
178 sc->sc_provider = NULL;
179 G_STRIPE_DEBUG(0, "Device %s removed.", sc->sc_name);
182 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
183 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
185 g_destroy_consumer(cp);
189 g_stripe_orphan(struct g_consumer *cp)
191 struct g_stripe_softc *sc;
200 g_stripe_remove_disk(cp);
201 /* If there are no valid disks anymore, remove device. */
202 if (g_stripe_nvalid(sc) == 0)
203 g_stripe_destroy(sc, 1);
207 g_stripe_access(struct g_provider *pp, int dr, int dw, int de)
209 struct g_consumer *cp1, *cp2;
210 struct g_stripe_softc *sc;
219 * It looks like geom is being withered.
220 * In that case we allow only negative requests.
222 KASSERT(dr <= 0 && dw <= 0 && de <= 0,
223 ("Positive access request (device=%s).", pp->name));
224 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 &&
225 (pp->ace + de) == 0) {
226 G_STRIPE_DEBUG(0, "Device %s definitely destroyed.",
232 /* On first open, grab an extra "exclusive" bit */
233 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
235 /* ... and let go of it on last close */
236 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
240 LIST_FOREACH(cp1, &gp->consumer, consumer) {
241 error = g_access(cp1, dr, dw, de);
245 * If we fail here, backout all previous changes.
247 LIST_FOREACH(cp2, &gp->consumer, consumer) {
250 g_access(cp2, -dr, -dw, -de);
259 g_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset,
260 off_t length, int mode)
265 stripesize = sc->sc_stripesize;
266 len = (size_t)(stripesize - (offset & (stripesize - 1)));
268 bcopy(src, dst, len);
270 dst += len + stripesize * (sc->sc_ndisks - 1);
274 src += len + stripesize * (sc->sc_ndisks - 1);
278 ("Length < 0 (stripesize=%zu, offset=%jd, length=%jd).",
279 (size_t)stripesize, (intmax_t)offset, (intmax_t)length));
280 if (length > stripesize)
284 } while (length > 0);
288 g_stripe_done(struct bio *bp)
290 struct g_stripe_softc *sc;
293 pbp = bp->bio_parent;
294 sc = pbp->bio_to->geom->softc;
295 if (pbp->bio_error == 0)
296 pbp->bio_error = bp->bio_error;
297 pbp->bio_completed += bp->bio_completed;
298 if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) {
299 g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset,
301 bp->bio_data = bp->bio_caller1;
302 bp->bio_caller1 = NULL;
306 if (pbp->bio_children == pbp->bio_inbed) {
307 if (pbp->bio_driver1 != NULL)
308 uma_zfree(g_stripe_zone, pbp->bio_driver1);
309 g_io_deliver(pbp, pbp->bio_error);
314 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
316 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
317 u_int nparts = 0, stripesize;
318 struct g_stripe_softc *sc;
319 char *addr, *data = NULL;
323 sc = bp->bio_to->geom->softc;
326 stripesize = sc->sc_stripesize;
328 cbp = g_clone_bio(bp);
333 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
336 * Fill in the component buf structure.
338 cbp->bio_done = g_stripe_done;
339 cbp->bio_offset = offset;
340 cbp->bio_data = addr;
341 cbp->bio_caller1 = NULL;
342 cbp->bio_length = length;
343 cbp->bio_caller2 = sc->sc_disks[no];
345 /* offset -= offset % stripesize; */
346 offset -= offset & (stripesize - 1);
348 length = bp->bio_length - length;
349 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
350 if (no > sc->sc_ndisks - 1) {
352 offset += stripesize;
354 if (nparts >= sc->sc_ndisks) {
355 cbp = TAILQ_NEXT(cbp, bio_queue);
357 cbp = TAILQ_FIRST(&queue);
360 * Update bio structure.
363 * MIN() is in case when
364 * (bp->bio_length % sc->sc_stripesize) != 0.
366 cbp->bio_length += MIN(stripesize, length);
367 if (cbp->bio_caller1 == NULL) {
368 cbp->bio_caller1 = cbp->bio_data;
369 cbp->bio_data = NULL;
371 data = uma_zalloc(g_stripe_zone,
380 cbp = g_clone_bio(bp);
385 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
388 * Fill in the component buf structure.
390 cbp->bio_done = g_stripe_done;
391 cbp->bio_offset = offset;
392 cbp->bio_data = addr;
393 cbp->bio_caller1 = NULL;
395 * MIN() is in case when
396 * (bp->bio_length % sc->sc_stripesize) != 0.
398 cbp->bio_length = MIN(stripesize, length);
399 cbp->bio_caller2 = sc->sc_disks[no];
403 bp->bio_driver1 = data;
405 * Fire off all allocated requests!
407 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
408 struct g_consumer *cp;
410 TAILQ_REMOVE(&queue, cbp, bio_queue);
411 cp = cbp->bio_caller2;
412 cbp->bio_caller2 = NULL;
413 cbp->bio_to = cp->provider;
414 if (cbp->bio_caller1 != NULL) {
415 cbp->bio_data = data;
416 if (bp->bio_cmd == BIO_WRITE) {
417 g_stripe_copy(sc, cbp->bio_caller1, data,
418 cbp->bio_offset, cbp->bio_length, 0);
420 data += cbp->bio_length;
422 G_STRIPE_LOGREQ(cbp, "Sending request.");
423 g_io_request(cbp, cp);
428 uma_zfree(g_stripe_zone, data);
429 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
430 TAILQ_REMOVE(&queue, cbp, bio_queue);
431 if (cbp->bio_caller1 != NULL) {
432 cbp->bio_data = cbp->bio_caller1;
433 cbp->bio_caller1 = NULL;
442 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
444 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
445 struct g_stripe_softc *sc;
451 sc = bp->bio_to->geom->softc;
454 stripesize = sc->sc_stripesize;
456 cbp = g_clone_bio(bp);
461 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
463 * Fill in the component buf structure.
465 cbp->bio_done = g_std_done;
466 cbp->bio_offset = offset;
467 cbp->bio_data = addr;
468 cbp->bio_length = length;
469 cbp->bio_caller2 = sc->sc_disks[no];
471 /* offset -= offset % stripesize; */
472 offset -= offset & (stripesize - 1);
474 length = bp->bio_length - length;
475 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
476 if (no > sc->sc_ndisks - 1) {
478 offset += stripesize;
480 cbp = g_clone_bio(bp);
485 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
488 * Fill in the component buf structure.
490 cbp->bio_done = g_std_done;
491 cbp->bio_offset = offset;
492 cbp->bio_data = addr;
494 * MIN() is in case when
495 * (bp->bio_length % sc->sc_stripesize) != 0.
497 cbp->bio_length = MIN(stripesize, length);
499 cbp->bio_caller2 = sc->sc_disks[no];
502 * Fire off all allocated requests!
504 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
505 struct g_consumer *cp;
507 TAILQ_REMOVE(&queue, cbp, bio_queue);
508 cp = cbp->bio_caller2;
509 cbp->bio_caller2 = NULL;
510 cbp->bio_to = cp->provider;
511 G_STRIPE_LOGREQ(cbp, "Sending request.");
512 g_io_request(cbp, cp);
516 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
517 TAILQ_REMOVE(&queue, cbp, bio_queue);
525 g_stripe_flush(struct g_stripe_softc *sc, struct bio *bp)
527 struct bio_queue_head queue;
528 struct g_consumer *cp;
533 for (no = 0; no < sc->sc_ndisks; no++) {
534 cbp = g_clone_bio(bp);
536 for (cbp = bioq_first(&queue); cbp != NULL;
537 cbp = bioq_first(&queue)) {
538 bioq_remove(&queue, cbp);
541 if (bp->bio_error == 0)
542 bp->bio_error = ENOMEM;
543 g_io_deliver(bp, bp->bio_error);
546 bioq_insert_tail(&queue, cbp);
547 cbp->bio_done = g_std_done;
548 cbp->bio_caller1 = sc->sc_disks[no];
549 cbp->bio_to = sc->sc_disks[no]->provider;
551 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
552 bioq_remove(&queue, cbp);
553 G_STRIPE_LOGREQ(cbp, "Sending request.");
554 cp = cbp->bio_caller1;
555 cbp->bio_caller1 = NULL;
556 g_io_request(cbp, cp);
561 g_stripe_start(struct bio *bp)
563 off_t offset, start, length, nstripe;
564 struct g_stripe_softc *sc;
565 u_int no, stripesize;
568 sc = bp->bio_to->geom->softc;
570 * If sc == NULL, provider's error should be set and g_stripe_start()
571 * should not be called at all.
574 ("Provider's error should be set (error=%d)(device=%s).",
575 bp->bio_to->error, bp->bio_to->name));
577 G_STRIPE_LOGREQ(bp, "Request received.");
579 switch (bp->bio_cmd) {
585 g_stripe_flush(sc, bp);
588 /* To which provider it should be delivered? */
590 g_io_deliver(bp, EOPNOTSUPP);
594 stripesize = sc->sc_stripesize;
597 * Calculations are quite messy, but fast I hope.
601 /* nstripe = bp->bio_offset / stripesize; */
602 nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
604 no = nstripe % sc->sc_ndisks;
605 /* Start position in stripe. */
606 /* start = bp->bio_offset % stripesize; */
607 start = bp->bio_offset & (stripesize - 1);
608 /* Start position in disk. */
609 /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
610 offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
611 /* Length of data to operate. */
612 length = MIN(bp->bio_length, stripesize - start);
615 * Do use "fast" mode when:
616 * 1. "Fast" mode is ON.
618 * 2. Request size is less than or equal to MAXPHYS,
619 * which should always be true.
621 * 3. Request size is bigger than stripesize * ndisks. If it isn't,
622 * there will be no need to send more than one I/O request to
623 * a provider, so there is nothing to optmize.
625 if (g_stripe_fast && bp->bio_length <= MAXPHYS &&
626 bp->bio_length >= stripesize * sc->sc_ndisks) {
631 error = g_stripe_start_fast(bp, no, offset, length);
633 g_stripe_fast_failed++;
636 * Do use "economic" when:
637 * 1. "Economic" mode is ON.
639 * 2. "Fast" mode failed. It can only fail if there is no memory.
641 if (!fast || error != 0)
642 error = g_stripe_start_economic(bp, no, offset, length);
644 if (bp->bio_error == 0)
645 bp->bio_error = error;
646 g_io_deliver(bp, bp->bio_error);
651 g_stripe_check_and_run(struct g_stripe_softc *sc)
654 u_int no, sectorsize = 0;
656 if (g_stripe_nvalid(sc) != sc->sc_ndisks)
659 sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
662 * Find the smallest disk.
664 mediasize = sc->sc_disks[0]->provider->mediasize;
665 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
666 mediasize -= sc->sc_disks[0]->provider->sectorsize;
667 mediasize -= mediasize % sc->sc_stripesize;
668 sectorsize = sc->sc_disks[0]->provider->sectorsize;
669 for (no = 1; no < sc->sc_ndisks; no++) {
670 ms = sc->sc_disks[no]->provider->mediasize;
671 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
672 ms -= sc->sc_disks[no]->provider->sectorsize;
673 ms -= ms % sc->sc_stripesize;
676 sectorsize = lcm(sectorsize,
677 sc->sc_disks[no]->provider->sectorsize);
679 sc->sc_provider->sectorsize = sectorsize;
680 sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
681 sc->sc_provider->stripesize = sc->sc_stripesize;
682 sc->sc_provider->stripeoffset = 0;
683 g_error_provider(sc->sc_provider, 0);
685 G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_name);
689 g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
691 struct g_provider *pp;
697 error = g_access(cp, 1, 0, 0);
702 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
705 g_access(cp, -1, 0, 0);
709 /* Decode metadata. */
710 stripe_metadata_decode(buf, md);
717 * Add disk to given device.
720 g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
722 struct g_consumer *cp, *fcp;
726 /* Metadata corrupted? */
727 if (no >= sc->sc_ndisks)
730 /* Check if disk is not already attached. */
731 if (sc->sc_disks[no] != NULL)
735 fcp = LIST_FIRST(&gp->consumer);
737 cp = g_new_consumer(gp);
738 error = g_attach(cp, pp);
740 g_destroy_consumer(cp);
744 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
745 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
748 g_destroy_consumer(cp);
752 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
753 struct g_stripe_metadata md;
755 /* Reread metadata. */
756 error = g_stripe_read_metadata(cp, &md);
760 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
761 strcmp(md.md_name, sc->sc_name) != 0 ||
762 md.md_id != sc->sc_id) {
763 G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
770 sc->sc_disks[no] = cp;
772 G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
774 g_stripe_check_and_run(sc);
778 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
779 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
781 g_destroy_consumer(cp);
785 static struct g_geom *
786 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
789 struct g_stripe_softc *sc;
793 G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
796 /* Two disks is minimum. */
797 if (md->md_all < 2) {
798 G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
802 /* Stripe size have to be grater than or equal to sector size. */
803 if (md->md_stripesize < sectorsize) {
804 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
808 /* Stripe size have to be power of 2. */
809 if (!powerof2(md->md_stripesize)) {
810 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
814 /* Check for duplicate unit */
815 LIST_FOREACH(gp, &mp->geom, geom) {
817 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
818 G_STRIPE_DEBUG(0, "Device %s already configured.",
823 gp = g_new_geomf(mp, "%s", md->md_name);
824 sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
825 gp->start = g_stripe_start;
826 gp->spoiled = g_stripe_orphan;
827 gp->orphan = g_stripe_orphan;
828 gp->access = g_stripe_access;
829 gp->dumpconf = g_stripe_dumpconf;
831 sc->sc_id = md->md_id;
832 sc->sc_stripesize = md->md_stripesize;
833 sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
834 sc->sc_ndisks = md->md_all;
835 sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
836 M_STRIPE, M_WAITOK | M_ZERO);
837 for (no = 0; no < sc->sc_ndisks; no++)
838 sc->sc_disks[no] = NULL;
843 sc->sc_provider = NULL;
845 G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
851 g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
853 struct g_provider *pp;
862 pp = sc->sc_provider;
863 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
865 G_STRIPE_DEBUG(0, "Device %s is still open, so it "
866 "can't be definitely removed.", pp->name);
869 "Device %s is still open (r%dw%de%d).", pp->name,
870 pp->acr, pp->acw, pp->ace);
875 for (no = 0; no < sc->sc_ndisks; no++) {
876 if (sc->sc_disks[no] != NULL)
877 g_stripe_remove_disk(sc->sc_disks[no]);
882 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
884 free(sc->sc_disks, M_STRIPE);
887 pp = LIST_FIRST(&gp->provider);
888 if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
889 G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
891 g_wither_geom(gp, ENXIO);
897 g_stripe_destroy_geom(struct gctl_req *req __unused,
898 struct g_class *mp __unused, struct g_geom *gp)
900 struct g_stripe_softc *sc;
903 return (g_stripe_destroy(sc, 0));
906 static struct g_geom *
907 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
909 struct g_stripe_metadata md;
910 struct g_stripe_softc *sc;
911 struct g_consumer *cp;
915 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
918 /* Skip providers that are already open for writing. */
922 G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
924 gp = g_new_geomf(mp, "stripe:taste");
925 gp->start = g_stripe_start;
926 gp->access = g_stripe_access;
927 gp->orphan = g_stripe_orphan;
928 cp = g_new_consumer(gp);
930 error = g_stripe_read_metadata(cp, &md);
932 g_destroy_consumer(cp);
938 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
940 if (md.md_version > G_STRIPE_VERSION) {
941 printf("geom_stripe.ko module is too old to handle %s.\n",
946 * Backward compatibility:
948 /* There was no md_provider field in earlier versions of metadata. */
949 if (md.md_version < 2)
950 bzero(md.md_provider, sizeof(md.md_provider));
951 /* There was no md_provsize field in earlier versions of metadata. */
952 if (md.md_version < 3)
953 md.md_provsize = pp->mediasize;
955 if (md.md_provider[0] != '\0' &&
956 !g_compare_names(md.md_provider, pp->name))
958 if (md.md_provsize != pp->mediasize)
962 * Let's check if device already exists.
965 LIST_FOREACH(gp, &mp->geom, geom) {
969 if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
971 if (strcmp(md.md_name, sc->sc_name) != 0)
973 if (md.md_id != sc->sc_id)
978 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
979 error = g_stripe_add_disk(sc, pp, md.md_no);
982 "Cannot add disk %s to %s (error=%d).", pp->name,
987 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
989 G_STRIPE_DEBUG(0, "Cannot create device %s.",
994 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
995 error = g_stripe_add_disk(sc, pp, md.md_no);
998 "Cannot add disk %s to %s (error=%d).", pp->name,
1000 g_stripe_destroy(sc, 1);
1009 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1012 struct g_stripe_metadata md;
1013 struct g_provider *pp;
1014 struct g_stripe_softc *sc;
1017 intmax_t *stripesize;
1022 g_topology_assert();
1023 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1024 if (nargs == NULL) {
1025 gctl_error(req, "No '%s' argument.", "nargs");
1029 gctl_error(req, "Too few arguments.");
1033 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1034 md.md_version = G_STRIPE_VERSION;
1035 name = gctl_get_asciiparam(req, "arg0");
1037 gctl_error(req, "No 'arg%u' argument.", 0);
1040 strlcpy(md.md_name, name, sizeof(md.md_name));
1041 md.md_id = arc4random();
1043 md.md_all = *nargs - 1;
1044 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1045 if (stripesize == NULL) {
1046 gctl_error(req, "No '%s' argument.", "stripesize");
1049 md.md_stripesize = *stripesize;
1050 bzero(md.md_provider, sizeof(md.md_provider));
1051 /* This field is not important here. */
1054 /* Check all providers are valid */
1055 for (no = 1; no < *nargs; no++) {
1056 snprintf(param, sizeof(param), "arg%u", no);
1057 name = gctl_get_asciiparam(req, param);
1059 gctl_error(req, "No 'arg%u' argument.", no);
1062 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1063 name += strlen("/dev/");
1064 pp = g_provider_by_name(name);
1066 G_STRIPE_DEBUG(1, "Disk %s is invalid.", name);
1067 gctl_error(req, "Disk %s is invalid.", name);
1072 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1074 gctl_error(req, "Can't configure %s.", md.md_name);
1079 sb = sbuf_new_auto();
1080 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1081 for (attached = 0, no = 1; no < *nargs; no++) {
1082 snprintf(param, sizeof(param), "arg%u", no);
1083 name = gctl_get_asciiparam(req, param);
1085 gctl_error(req, "No 'arg%u' argument.", no);
1088 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1089 name += strlen("/dev/");
1090 pp = g_provider_by_name(name);
1091 KASSERT(pp != NULL, ("Provider %s disappear?!", name));
1092 if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1093 G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1094 no, pp->name, gp->name);
1095 sbuf_printf(sb, " %s", pp->name);
1101 if (md.md_all != attached) {
1102 g_stripe_destroy(gp->softc, 1);
1103 gctl_error(req, "%s", sbuf_data(sb));
1108 static struct g_stripe_softc *
1109 g_stripe_find_device(struct g_class *mp, const char *name)
1111 struct g_stripe_softc *sc;
1114 LIST_FOREACH(gp, &mp->geom, geom) {
1118 if (strcmp(sc->sc_name, name) == 0)
1125 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1127 struct g_stripe_softc *sc;
1128 int *force, *nargs, error;
1133 g_topology_assert();
1135 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1136 if (nargs == NULL) {
1137 gctl_error(req, "No '%s' argument.", "nargs");
1141 gctl_error(req, "Missing device(s).");
1144 force = gctl_get_paraml(req, "force", sizeof(*force));
1145 if (force == NULL) {
1146 gctl_error(req, "No '%s' argument.", "force");
1150 for (i = 0; i < (u_int)*nargs; i++) {
1151 snprintf(param, sizeof(param), "arg%u", i);
1152 name = gctl_get_asciiparam(req, param);
1154 gctl_error(req, "No 'arg%u' argument.", i);
1157 sc = g_stripe_find_device(mp, name);
1159 gctl_error(req, "No such device: %s.", name);
1162 error = g_stripe_destroy(sc, *force);
1164 gctl_error(req, "Cannot destroy device %s (error=%d).",
1165 sc->sc_name, error);
1172 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1176 g_topology_assert();
1178 version = gctl_get_paraml(req, "version", sizeof(*version));
1179 if (version == NULL) {
1180 gctl_error(req, "No '%s' argument.", "version");
1183 if (*version != G_STRIPE_VERSION) {
1184 gctl_error(req, "Userland and kernel parts are out of sync.");
1188 if (strcmp(verb, "create") == 0) {
1189 g_stripe_ctl_create(req, mp);
1191 } else if (strcmp(verb, "destroy") == 0 ||
1192 strcmp(verb, "stop") == 0) {
1193 g_stripe_ctl_destroy(req, mp);
1197 gctl_error(req, "Unknown verb.");
1201 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1202 struct g_consumer *cp, struct g_provider *pp)
1204 struct g_stripe_softc *sc;
1211 } else if (cp != NULL) {
1212 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1215 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1216 sbuf_printf(sb, "%s<Stripesize>%u</Stripesize>\n", indent,
1217 (u_int)sc->sc_stripesize);
1218 sbuf_printf(sb, "%s<Type>", indent);
1219 switch (sc->sc_type) {
1220 case G_STRIPE_TYPE_AUTOMATIC:
1221 sbuf_printf(sb, "AUTOMATIC");
1223 case G_STRIPE_TYPE_MANUAL:
1224 sbuf_printf(sb, "MANUAL");
1227 sbuf_printf(sb, "UNKNOWN");
1230 sbuf_printf(sb, "</Type>\n");
1231 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1232 indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1233 sbuf_printf(sb, "%s<State>", indent);
1234 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1235 sbuf_printf(sb, "UP");
1237 sbuf_printf(sb, "DOWN");
1238 sbuf_printf(sb, "</State>\n");
1242 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);