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 sc->sc_provider->flags |= G_PF_WITHER;
178 G_STRIPE_DEBUG(0, "Device %s deactivated.",
179 sc->sc_provider->name);
180 g_orphan_provider(sc->sc_provider, ENXIO);
181 sc->sc_provider = NULL;
184 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
186 sc->sc_disks[cp->index] = NULL;
189 g_destroy_consumer(cp);
190 /* If there are no valid disks anymore, remove device. */
191 if (LIST_EMPTY(&sc->sc_geom->consumer))
192 g_stripe_destroy(sc, 1);
196 g_stripe_orphan(struct g_consumer *cp)
198 struct g_stripe_softc *sc;
207 g_stripe_remove_disk(cp);
211 g_stripe_access(struct g_provider *pp, int dr, int dw, int de)
213 struct g_consumer *cp1, *cp2, *tmp;
214 struct g_stripe_softc *sc;
221 KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
223 /* On first open, grab an extra "exclusive" bit */
224 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
226 /* ... and let go of it on last close */
227 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
230 LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) {
231 error = g_access(cp1, dr, dw, de);
234 if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 &&
235 cp1->private != NULL) {
236 g_stripe_remove_disk(cp1); /* May destroy geom. */
242 LIST_FOREACH(cp2, &gp->consumer, consumer) {
245 g_access(cp2, -dr, -dw, -de);
251 g_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset,
252 off_t length, int mode)
257 stripesize = sc->sc_stripesize;
258 len = (size_t)(stripesize - (offset & (stripesize - 1)));
260 bcopy(src, dst, len);
262 dst += len + stripesize * (sc->sc_ndisks - 1);
266 src += len + stripesize * (sc->sc_ndisks - 1);
270 ("Length < 0 (stripesize=%zu, offset=%jd, length=%jd).",
271 (size_t)stripesize, (intmax_t)offset, (intmax_t)length));
272 if (length > stripesize)
276 } while (length > 0);
280 g_stripe_done(struct bio *bp)
282 struct g_stripe_softc *sc;
285 pbp = bp->bio_parent;
286 sc = pbp->bio_to->geom->softc;
287 if (pbp->bio_error == 0)
288 pbp->bio_error = bp->bio_error;
289 pbp->bio_completed += bp->bio_completed;
290 if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) {
291 g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset,
293 bp->bio_data = bp->bio_caller1;
294 bp->bio_caller1 = NULL;
298 if (pbp->bio_children == pbp->bio_inbed) {
299 if (pbp->bio_driver1 != NULL)
300 uma_zfree(g_stripe_zone, pbp->bio_driver1);
301 g_io_deliver(pbp, pbp->bio_error);
306 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
308 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
309 u_int nparts = 0, stripesize;
310 struct g_stripe_softc *sc;
311 char *addr, *data = NULL;
315 sc = bp->bio_to->geom->softc;
318 stripesize = sc->sc_stripesize;
320 cbp = g_clone_bio(bp);
325 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
328 * Fill in the component buf structure.
330 cbp->bio_done = g_stripe_done;
331 cbp->bio_offset = offset;
332 cbp->bio_data = addr;
333 cbp->bio_caller1 = NULL;
334 cbp->bio_length = length;
335 cbp->bio_caller2 = sc->sc_disks[no];
337 /* offset -= offset % stripesize; */
338 offset -= offset & (stripesize - 1);
340 length = bp->bio_length - length;
341 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
342 if (no > sc->sc_ndisks - 1) {
344 offset += stripesize;
346 if (nparts >= sc->sc_ndisks) {
347 cbp = TAILQ_NEXT(cbp, bio_queue);
349 cbp = TAILQ_FIRST(&queue);
352 * Update bio structure.
355 * MIN() is in case when
356 * (bp->bio_length % sc->sc_stripesize) != 0.
358 cbp->bio_length += MIN(stripesize, length);
359 if (cbp->bio_caller1 == NULL) {
360 cbp->bio_caller1 = cbp->bio_data;
361 cbp->bio_data = NULL;
363 data = uma_zalloc(g_stripe_zone,
372 cbp = g_clone_bio(bp);
377 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
380 * Fill in the component buf structure.
382 cbp->bio_done = g_stripe_done;
383 cbp->bio_offset = offset;
384 cbp->bio_data = addr;
385 cbp->bio_caller1 = NULL;
387 * MIN() is in case when
388 * (bp->bio_length % sc->sc_stripesize) != 0.
390 cbp->bio_length = MIN(stripesize, length);
391 cbp->bio_caller2 = sc->sc_disks[no];
395 bp->bio_driver1 = data;
397 * Fire off all allocated requests!
399 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
400 struct g_consumer *cp;
402 TAILQ_REMOVE(&queue, cbp, bio_queue);
403 cp = cbp->bio_caller2;
404 cbp->bio_caller2 = NULL;
405 cbp->bio_to = cp->provider;
406 if (cbp->bio_caller1 != NULL) {
407 cbp->bio_data = data;
408 if (bp->bio_cmd == BIO_WRITE) {
409 g_stripe_copy(sc, cbp->bio_caller1, data,
410 cbp->bio_offset, cbp->bio_length, 0);
412 data += cbp->bio_length;
414 G_STRIPE_LOGREQ(cbp, "Sending request.");
415 g_io_request(cbp, cp);
420 uma_zfree(g_stripe_zone, data);
421 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
422 TAILQ_REMOVE(&queue, cbp, bio_queue);
423 if (cbp->bio_caller1 != NULL) {
424 cbp->bio_data = cbp->bio_caller1;
425 cbp->bio_caller1 = NULL;
434 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
436 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
437 struct g_stripe_softc *sc;
443 sc = bp->bio_to->geom->softc;
446 stripesize = sc->sc_stripesize;
448 cbp = g_clone_bio(bp);
453 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
455 * Fill in the component buf structure.
457 cbp->bio_done = g_std_done;
458 cbp->bio_offset = offset;
459 cbp->bio_data = addr;
460 cbp->bio_length = length;
461 cbp->bio_caller2 = sc->sc_disks[no];
463 /* offset -= offset % stripesize; */
464 offset -= offset & (stripesize - 1);
466 length = bp->bio_length - length;
467 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
468 if (no > sc->sc_ndisks - 1) {
470 offset += stripesize;
472 cbp = g_clone_bio(bp);
477 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
480 * Fill in the component buf structure.
482 cbp->bio_done = g_std_done;
483 cbp->bio_offset = offset;
484 cbp->bio_data = addr;
486 * MIN() is in case when
487 * (bp->bio_length % sc->sc_stripesize) != 0.
489 cbp->bio_length = MIN(stripesize, length);
491 cbp->bio_caller2 = sc->sc_disks[no];
494 * Fire off all allocated requests!
496 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
497 struct g_consumer *cp;
499 TAILQ_REMOVE(&queue, cbp, bio_queue);
500 cp = cbp->bio_caller2;
501 cbp->bio_caller2 = NULL;
502 cbp->bio_to = cp->provider;
503 G_STRIPE_LOGREQ(cbp, "Sending request.");
504 g_io_request(cbp, cp);
508 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
509 TAILQ_REMOVE(&queue, cbp, bio_queue);
517 g_stripe_flush(struct g_stripe_softc *sc, struct bio *bp)
519 struct bio_queue_head queue;
520 struct g_consumer *cp;
525 for (no = 0; no < sc->sc_ndisks; no++) {
526 cbp = g_clone_bio(bp);
528 for (cbp = bioq_first(&queue); cbp != NULL;
529 cbp = bioq_first(&queue)) {
530 bioq_remove(&queue, cbp);
533 if (bp->bio_error == 0)
534 bp->bio_error = ENOMEM;
535 g_io_deliver(bp, bp->bio_error);
538 bioq_insert_tail(&queue, cbp);
539 cbp->bio_done = g_std_done;
540 cbp->bio_caller1 = sc->sc_disks[no];
541 cbp->bio_to = sc->sc_disks[no]->provider;
543 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
544 bioq_remove(&queue, cbp);
545 G_STRIPE_LOGREQ(cbp, "Sending request.");
546 cp = cbp->bio_caller1;
547 cbp->bio_caller1 = NULL;
548 g_io_request(cbp, cp);
553 g_stripe_start(struct bio *bp)
555 off_t offset, start, length, nstripe;
556 struct g_stripe_softc *sc;
557 u_int no, stripesize;
560 sc = bp->bio_to->geom->softc;
562 * If sc == NULL, provider's error should be set and g_stripe_start()
563 * should not be called at all.
566 ("Provider's error should be set (error=%d)(device=%s).",
567 bp->bio_to->error, bp->bio_to->name));
569 G_STRIPE_LOGREQ(bp, "Request received.");
571 switch (bp->bio_cmd) {
577 g_stripe_flush(sc, bp);
580 /* To which provider it should be delivered? */
582 g_io_deliver(bp, EOPNOTSUPP);
586 stripesize = sc->sc_stripesize;
589 * Calculations are quite messy, but fast I hope.
593 /* nstripe = bp->bio_offset / stripesize; */
594 nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
596 no = nstripe % sc->sc_ndisks;
597 /* Start position in stripe. */
598 /* start = bp->bio_offset % stripesize; */
599 start = bp->bio_offset & (stripesize - 1);
600 /* Start position in disk. */
601 /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
602 offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
603 /* Length of data to operate. */
604 length = MIN(bp->bio_length, stripesize - start);
607 * Do use "fast" mode when:
608 * 1. "Fast" mode is ON.
610 * 2. Request size is less than or equal to MAXPHYS,
611 * which should always be true.
613 * 3. Request size is bigger than stripesize * ndisks. If it isn't,
614 * there will be no need to send more than one I/O request to
615 * a provider, so there is nothing to optmize.
617 if (g_stripe_fast && bp->bio_length <= MAXPHYS &&
618 bp->bio_length >= stripesize * sc->sc_ndisks) {
623 error = g_stripe_start_fast(bp, no, offset, length);
625 g_stripe_fast_failed++;
628 * Do use "economic" when:
629 * 1. "Economic" mode is ON.
631 * 2. "Fast" mode failed. It can only fail if there is no memory.
633 if (!fast || error != 0)
634 error = g_stripe_start_economic(bp, no, offset, length);
636 if (bp->bio_error == 0)
637 bp->bio_error = error;
638 g_io_deliver(bp, bp->bio_error);
643 g_stripe_check_and_run(struct g_stripe_softc *sc)
646 u_int no, sectorsize = 0;
649 if (g_stripe_nvalid(sc) != sc->sc_ndisks)
652 sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
655 * Find the smallest disk.
657 mediasize = sc->sc_disks[0]->provider->mediasize;
658 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
659 mediasize -= sc->sc_disks[0]->provider->sectorsize;
660 mediasize -= mediasize % sc->sc_stripesize;
661 sectorsize = sc->sc_disks[0]->provider->sectorsize;
662 for (no = 1; no < sc->sc_ndisks; no++) {
663 ms = sc->sc_disks[no]->provider->mediasize;
664 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
665 ms -= sc->sc_disks[no]->provider->sectorsize;
666 ms -= ms % sc->sc_stripesize;
669 sectorsize = lcm(sectorsize,
670 sc->sc_disks[no]->provider->sectorsize);
672 sc->sc_provider->sectorsize = sectorsize;
673 sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
674 sc->sc_provider->stripesize = sc->sc_stripesize;
675 sc->sc_provider->stripeoffset = 0;
676 g_error_provider(sc->sc_provider, 0);
678 G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
682 g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
684 struct g_provider *pp;
690 error = g_access(cp, 1, 0, 0);
695 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
698 g_access(cp, -1, 0, 0);
702 /* Decode metadata. */
703 stripe_metadata_decode(buf, md);
710 * Add disk to given device.
713 g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
715 struct g_consumer *cp, *fcp;
720 /* Metadata corrupted? */
721 if (no >= sc->sc_ndisks)
724 /* Check if disk is not already attached. */
725 if (sc->sc_disks[no] != NULL)
729 fcp = LIST_FIRST(&gp->consumer);
731 cp = g_new_consumer(gp);
734 error = g_attach(cp, pp);
736 g_destroy_consumer(cp);
740 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
741 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
744 g_destroy_consumer(cp);
748 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
749 struct g_stripe_metadata md;
751 /* Reread metadata. */
752 error = g_stripe_read_metadata(cp, &md);
756 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
757 strcmp(md.md_name, sc->sc_name) != 0 ||
758 md.md_id != sc->sc_id) {
759 G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
764 sc->sc_disks[no] = cp;
765 G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
766 g_stripe_check_and_run(sc);
770 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
771 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
773 g_destroy_consumer(cp);
777 static struct g_geom *
778 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
781 struct g_stripe_softc *sc;
786 G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
789 /* Two disks is minimum. */
790 if (md->md_all < 2) {
791 G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
795 /* Stripe size have to be grater than or equal to sector size. */
796 if (md->md_stripesize < sectorsize) {
797 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
801 /* Stripe size have to be power of 2. */
802 if (!powerof2(md->md_stripesize)) {
803 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
807 /* Check for duplicate unit */
808 LIST_FOREACH(gp, &mp->geom, geom) {
810 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
811 G_STRIPE_DEBUG(0, "Device %s already configured.",
816 gp = g_new_geomf(mp, "%s", md->md_name);
817 sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
818 gp->start = g_stripe_start;
819 gp->spoiled = g_stripe_orphan;
820 gp->orphan = g_stripe_orphan;
821 gp->access = g_stripe_access;
822 gp->dumpconf = g_stripe_dumpconf;
824 sc->sc_id = md->md_id;
825 sc->sc_stripesize = md->md_stripesize;
826 sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
827 sc->sc_ndisks = md->md_all;
828 sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
829 M_STRIPE, M_WAITOK | M_ZERO);
830 for (no = 0; no < sc->sc_ndisks; no++)
831 sc->sc_disks[no] = NULL;
836 sc->sc_provider = NULL;
838 G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
844 g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
846 struct g_provider *pp;
847 struct g_consumer *cp, *cp1;
855 pp = sc->sc_provider;
856 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
858 G_STRIPE_DEBUG(0, "Device %s is still open, so it "
859 "can't be definitely removed.", pp->name);
862 "Device %s is still open (r%dw%de%d).", pp->name,
863 pp->acr, pp->acw, pp->ace);
869 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
870 g_stripe_remove_disk(cp);
872 return (0); /* Recursion happened. */
874 if (!LIST_EMPTY(&gp->consumer))
875 return (EINPROGRESS);
878 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
880 free(sc->sc_disks, M_STRIPE);
882 G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
883 g_wither_geom(gp, ENXIO);
888 g_stripe_destroy_geom(struct gctl_req *req __unused,
889 struct g_class *mp __unused, struct g_geom *gp)
891 struct g_stripe_softc *sc;
894 return (g_stripe_destroy(sc, 0));
897 static struct g_geom *
898 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
900 struct g_stripe_metadata md;
901 struct g_stripe_softc *sc;
902 struct g_consumer *cp;
906 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
909 /* Skip providers that are already open for writing. */
913 G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
915 gp = g_new_geomf(mp, "stripe:taste");
916 gp->start = g_stripe_start;
917 gp->access = g_stripe_access;
918 gp->orphan = g_stripe_orphan;
919 cp = g_new_consumer(gp);
921 error = g_stripe_read_metadata(cp, &md);
923 g_destroy_consumer(cp);
929 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
931 if (md.md_version > G_STRIPE_VERSION) {
932 printf("geom_stripe.ko module is too old to handle %s.\n",
937 * Backward compatibility:
939 /* There was no md_provider field in earlier versions of metadata. */
940 if (md.md_version < 2)
941 bzero(md.md_provider, sizeof(md.md_provider));
942 /* There was no md_provsize field in earlier versions of metadata. */
943 if (md.md_version < 3)
944 md.md_provsize = pp->mediasize;
946 if (md.md_provider[0] != '\0' &&
947 !g_compare_names(md.md_provider, pp->name))
949 if (md.md_provsize != pp->mediasize)
953 * Let's check if device already exists.
956 LIST_FOREACH(gp, &mp->geom, geom) {
960 if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
962 if (strcmp(md.md_name, sc->sc_name) != 0)
964 if (md.md_id != sc->sc_id)
969 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
970 error = g_stripe_add_disk(sc, pp, md.md_no);
973 "Cannot add disk %s to %s (error=%d).", pp->name,
978 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
980 G_STRIPE_DEBUG(0, "Cannot create device %s.",
985 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
986 error = g_stripe_add_disk(sc, pp, md.md_no);
989 "Cannot add disk %s to %s (error=%d).", pp->name,
991 g_stripe_destroy(sc, 1);
1000 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1003 struct g_stripe_metadata md;
1004 struct g_provider *pp;
1005 struct g_stripe_softc *sc;
1008 intmax_t *stripesize;
1013 g_topology_assert();
1014 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1015 if (nargs == NULL) {
1016 gctl_error(req, "No '%s' argument.", "nargs");
1020 gctl_error(req, "Too few arguments.");
1024 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1025 md.md_version = G_STRIPE_VERSION;
1026 name = gctl_get_asciiparam(req, "arg0");
1028 gctl_error(req, "No 'arg%u' argument.", 0);
1031 strlcpy(md.md_name, name, sizeof(md.md_name));
1032 md.md_id = arc4random();
1034 md.md_all = *nargs - 1;
1035 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1036 if (stripesize == NULL) {
1037 gctl_error(req, "No '%s' argument.", "stripesize");
1040 md.md_stripesize = *stripesize;
1041 bzero(md.md_provider, sizeof(md.md_provider));
1042 /* This field is not important here. */
1045 /* Check all providers are valid */
1046 for (no = 1; no < *nargs; no++) {
1047 snprintf(param, sizeof(param), "arg%u", no);
1048 name = gctl_get_asciiparam(req, param);
1050 gctl_error(req, "No 'arg%u' argument.", no);
1053 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1054 name += strlen("/dev/");
1055 pp = g_provider_by_name(name);
1057 G_STRIPE_DEBUG(1, "Disk %s is invalid.", name);
1058 gctl_error(req, "Disk %s is invalid.", name);
1063 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1065 gctl_error(req, "Can't configure %s.", md.md_name);
1070 sb = sbuf_new_auto();
1071 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1072 for (attached = 0, no = 1; no < *nargs; no++) {
1073 snprintf(param, sizeof(param), "arg%u", no);
1074 name = gctl_get_asciiparam(req, param);
1076 gctl_error(req, "No 'arg%u' argument.", no);
1079 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1080 name += strlen("/dev/");
1081 pp = g_provider_by_name(name);
1082 KASSERT(pp != NULL, ("Provider %s disappear?!", name));
1083 if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1084 G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1085 no, pp->name, gp->name);
1086 sbuf_printf(sb, " %s", pp->name);
1092 if (md.md_all != attached) {
1093 g_stripe_destroy(gp->softc, 1);
1094 gctl_error(req, "%s", sbuf_data(sb));
1099 static struct g_stripe_softc *
1100 g_stripe_find_device(struct g_class *mp, const char *name)
1102 struct g_stripe_softc *sc;
1105 LIST_FOREACH(gp, &mp->geom, geom) {
1109 if (strcmp(sc->sc_name, name) == 0)
1116 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1118 struct g_stripe_softc *sc;
1119 int *force, *nargs, error;
1124 g_topology_assert();
1126 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1127 if (nargs == NULL) {
1128 gctl_error(req, "No '%s' argument.", "nargs");
1132 gctl_error(req, "Missing device(s).");
1135 force = gctl_get_paraml(req, "force", sizeof(*force));
1136 if (force == NULL) {
1137 gctl_error(req, "No '%s' argument.", "force");
1141 for (i = 0; i < (u_int)*nargs; i++) {
1142 snprintf(param, sizeof(param), "arg%u", i);
1143 name = gctl_get_asciiparam(req, param);
1145 gctl_error(req, "No 'arg%u' argument.", i);
1148 sc = g_stripe_find_device(mp, name);
1150 gctl_error(req, "No such device: %s.", name);
1153 error = g_stripe_destroy(sc, *force);
1155 gctl_error(req, "Cannot destroy device %s (error=%d).",
1156 sc->sc_name, error);
1163 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1167 g_topology_assert();
1169 version = gctl_get_paraml(req, "version", sizeof(*version));
1170 if (version == NULL) {
1171 gctl_error(req, "No '%s' argument.", "version");
1174 if (*version != G_STRIPE_VERSION) {
1175 gctl_error(req, "Userland and kernel parts are out of sync.");
1179 if (strcmp(verb, "create") == 0) {
1180 g_stripe_ctl_create(req, mp);
1182 } else if (strcmp(verb, "destroy") == 0 ||
1183 strcmp(verb, "stop") == 0) {
1184 g_stripe_ctl_destroy(req, mp);
1188 gctl_error(req, "Unknown verb.");
1192 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1193 struct g_consumer *cp, struct g_provider *pp)
1195 struct g_stripe_softc *sc;
1202 } else if (cp != NULL) {
1203 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1206 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1207 sbuf_printf(sb, "%s<Stripesize>%u</Stripesize>\n", indent,
1208 (u_int)sc->sc_stripesize);
1209 sbuf_printf(sb, "%s<Type>", indent);
1210 switch (sc->sc_type) {
1211 case G_STRIPE_TYPE_AUTOMATIC:
1212 sbuf_printf(sb, "AUTOMATIC");
1214 case G_STRIPE_TYPE_MANUAL:
1215 sbuf_printf(sb, "MANUAL");
1218 sbuf_printf(sb, "UNKNOWN");
1221 sbuf_printf(sb, "</Type>\n");
1222 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1223 indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1224 sbuf_printf(sb, "%s<State>", indent);
1225 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1226 sbuf_printf(sb, "UP");
1228 sbuf_printf(sb, "DOWN");
1229 sbuf_printf(sb, "</State>\n");
1233 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);