2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/mutex.h>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
43 #include <geom/geom.h>
44 #include <geom/geom_dbg.h>
45 #include <geom/stripe/g_stripe.h>
47 FEATURE(geom_stripe, "GEOM striping support");
49 static MALLOC_DEFINE(M_STRIPE, "stripe_data", "GEOM_STRIPE Data");
51 static uma_zone_t g_stripe_zone;
53 static int g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force);
54 static int g_stripe_destroy_geom(struct gctl_req *req, struct g_class *mp,
57 static g_taste_t g_stripe_taste;
58 static g_ctl_req_t g_stripe_config;
59 static g_dumpconf_t g_stripe_dumpconf;
60 static g_init_t g_stripe_init;
61 static g_fini_t g_stripe_fini;
63 struct g_class g_stripe_class = {
64 .name = G_STRIPE_CLASS_NAME,
66 .ctlreq = g_stripe_config,
67 .taste = g_stripe_taste,
68 .destroy_geom = g_stripe_destroy_geom,
69 .init = g_stripe_init,
73 SYSCTL_DECL(_kern_geom);
74 static SYSCTL_NODE(_kern_geom, OID_AUTO, stripe, CTLFLAG_RW, 0,
76 static u_int g_stripe_debug = 0;
77 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, debug, CTLFLAG_RWTUN, &g_stripe_debug, 0,
79 static int g_stripe_fast = 0;
81 g_sysctl_stripe_fast(SYSCTL_HANDLER_ARGS)
86 error = sysctl_handle_int(oidp, &fast, 0, req);
87 if (error == 0 && req->newptr != NULL)
91 SYSCTL_PROC(_kern_geom_stripe, OID_AUTO, fast, CTLTYPE_INT | CTLFLAG_RWTUN,
92 NULL, 0, g_sysctl_stripe_fast, "I", "Fast, but memory-consuming, mode");
93 static u_int g_stripe_maxmem = MAXPHYS * 100;
94 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, maxmem, CTLFLAG_RDTUN, &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=%ju, offset=%ju, length=%jd).",
270 (uintmax_t)stripesize, (uintmax_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 struct g_stripe_softc *sc;
312 char *addr, *data = NULL;
318 sc = bp->bio_to->geom->softc;
321 stripesize = sc->sc_stripesize;
323 cbp = g_clone_bio(bp);
328 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
331 * Fill in the component buf structure.
333 cbp->bio_done = g_stripe_done;
334 cbp->bio_offset = offset;
335 cbp->bio_data = addr;
336 cbp->bio_caller1 = NULL;
337 cbp->bio_length = length;
338 cbp->bio_caller2 = sc->sc_disks[no];
340 /* offset -= offset % stripesize; */
341 offset -= offset & (stripesize - 1);
343 length = bp->bio_length - length;
344 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
345 if (no > sc->sc_ndisks - 1) {
347 offset += stripesize;
349 if (nparts >= sc->sc_ndisks) {
350 cbp = TAILQ_NEXT(cbp, bio_queue);
352 cbp = TAILQ_FIRST(&queue);
355 * Update bio structure.
358 * MIN() is in case when
359 * (bp->bio_length % sc->sc_stripesize) != 0.
361 cbp->bio_length += MIN(stripesize, length);
362 if (cbp->bio_caller1 == NULL) {
363 cbp->bio_caller1 = cbp->bio_data;
364 cbp->bio_data = NULL;
366 data = uma_zalloc(g_stripe_zone,
375 cbp = g_clone_bio(bp);
380 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
383 * Fill in the component buf structure.
385 cbp->bio_done = g_stripe_done;
386 cbp->bio_offset = offset;
387 cbp->bio_data = addr;
388 cbp->bio_caller1 = NULL;
390 * MIN() is in case when
391 * (bp->bio_length % sc->sc_stripesize) != 0.
393 cbp->bio_length = MIN(stripesize, length);
394 cbp->bio_caller2 = sc->sc_disks[no];
398 bp->bio_driver1 = data;
400 * Fire off all allocated requests!
402 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
403 struct g_consumer *cp;
405 TAILQ_REMOVE(&queue, cbp, bio_queue);
406 cp = cbp->bio_caller2;
407 cbp->bio_caller2 = NULL;
408 cbp->bio_to = cp->provider;
409 if (cbp->bio_caller1 != NULL) {
410 cbp->bio_data = data;
411 if (bp->bio_cmd == BIO_WRITE) {
412 g_stripe_copy(sc, cbp->bio_caller1, data,
413 cbp->bio_offset, cbp->bio_length, 0);
415 data += cbp->bio_length;
417 G_STRIPE_LOGREQ(cbp, "Sending request.");
418 g_io_request(cbp, cp);
423 uma_zfree(g_stripe_zone, data);
424 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
425 TAILQ_REMOVE(&queue, cbp, bio_queue);
426 if (cbp->bio_caller1 != NULL) {
427 cbp->bio_data = cbp->bio_caller1;
428 cbp->bio_caller1 = NULL;
437 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
439 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
440 struct g_stripe_softc *sc;
446 sc = bp->bio_to->geom->softc;
448 stripesize = sc->sc_stripesize;
450 cbp = g_clone_bio(bp);
455 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
457 * Fill in the component buf structure.
459 if (bp->bio_length == length)
460 cbp->bio_done = g_std_done; /* Optimized lockless case. */
462 cbp->bio_done = g_stripe_done;
463 cbp->bio_offset = offset;
464 cbp->bio_length = length;
465 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
466 bp->bio_ma_n = round_page(bp->bio_ma_offset +
467 bp->bio_length) / PAGE_SIZE;
471 cbp->bio_caller2 = sc->sc_disks[no];
473 /* offset -= offset % stripesize; */
474 offset -= offset & (stripesize - 1);
475 if (bp->bio_cmd != BIO_DELETE)
477 length = bp->bio_length - length;
478 for (no++; length > 0; no++, length -= stripesize) {
479 if (no > sc->sc_ndisks - 1) {
481 offset += stripesize;
483 cbp = g_clone_bio(bp);
488 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
491 * Fill in the component buf structure.
493 cbp->bio_done = g_stripe_done;
494 cbp->bio_offset = offset;
496 * MIN() is in case when
497 * (bp->bio_length % sc->sc_stripesize) != 0.
499 cbp->bio_length = MIN(stripesize, length);
500 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
501 cbp->bio_ma_offset += (uintptr_t)addr;
502 cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
503 cbp->bio_ma_offset %= PAGE_SIZE;
504 cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
505 cbp->bio_length) / PAGE_SIZE;
507 cbp->bio_data = addr;
509 cbp->bio_caller2 = sc->sc_disks[no];
511 if (bp->bio_cmd != BIO_DELETE)
515 * Fire off all allocated requests!
517 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
518 struct g_consumer *cp;
520 TAILQ_REMOVE(&queue, cbp, bio_queue);
521 cp = cbp->bio_caller2;
522 cbp->bio_caller2 = NULL;
523 cbp->bio_to = cp->provider;
524 G_STRIPE_LOGREQ(cbp, "Sending request.");
525 g_io_request(cbp, cp);
529 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
530 TAILQ_REMOVE(&queue, cbp, bio_queue);
538 g_stripe_flush(struct g_stripe_softc *sc, struct bio *bp)
540 struct bio_queue_head queue;
541 struct g_consumer *cp;
546 for (no = 0; no < sc->sc_ndisks; no++) {
547 cbp = g_clone_bio(bp);
549 for (cbp = bioq_first(&queue); cbp != NULL;
550 cbp = bioq_first(&queue)) {
551 bioq_remove(&queue, cbp);
554 if (bp->bio_error == 0)
555 bp->bio_error = ENOMEM;
556 g_io_deliver(bp, bp->bio_error);
559 bioq_insert_tail(&queue, cbp);
560 cbp->bio_done = g_stripe_done;
561 cbp->bio_caller2 = sc->sc_disks[no];
562 cbp->bio_to = sc->sc_disks[no]->provider;
564 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
565 bioq_remove(&queue, cbp);
566 G_STRIPE_LOGREQ(cbp, "Sending request.");
567 cp = cbp->bio_caller2;
568 cbp->bio_caller2 = NULL;
569 g_io_request(cbp, cp);
574 g_stripe_start(struct bio *bp)
576 off_t offset, start, length, nstripe, stripesize;
577 struct g_stripe_softc *sc;
581 sc = bp->bio_to->geom->softc;
583 * If sc == NULL, provider's error should be set and g_stripe_start()
584 * should not be called at all.
587 ("Provider's error should be set (error=%d)(device=%s).",
588 bp->bio_to->error, bp->bio_to->name));
590 G_STRIPE_LOGREQ(bp, "Request received.");
592 switch (bp->bio_cmd) {
598 g_stripe_flush(sc, bp);
601 /* To which provider it should be delivered? */
603 g_io_deliver(bp, EOPNOTSUPP);
607 stripesize = sc->sc_stripesize;
610 * Calculations are quite messy, but fast I hope.
614 /* nstripe = bp->bio_offset / stripesize; */
615 nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
617 no = nstripe % sc->sc_ndisks;
618 /* Start position in stripe. */
619 /* start = bp->bio_offset % stripesize; */
620 start = bp->bio_offset & (stripesize - 1);
621 /* Start position in disk. */
622 /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
623 offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
624 /* Length of data to operate. */
625 length = MIN(bp->bio_length, stripesize - start);
628 * Do use "fast" mode when:
629 * 1. "Fast" mode is ON.
631 * 2. Request size is less than or equal to MAXPHYS,
632 * which should always be true.
634 * 3. Request size is bigger than stripesize * ndisks. If it isn't,
635 * there will be no need to send more than one I/O request to
636 * a provider, so there is nothing to optmize.
638 * 4. Request is not unmapped.
640 * 5. It is not a BIO_DELETE.
642 if (g_stripe_fast && bp->bio_length <= MAXPHYS &&
643 bp->bio_length >= stripesize * sc->sc_ndisks &&
644 (bp->bio_flags & BIO_UNMAPPED) == 0 &&
645 bp->bio_cmd != BIO_DELETE) {
650 error = g_stripe_start_fast(bp, no, offset, length);
652 g_stripe_fast_failed++;
655 * Do use "economic" when:
656 * 1. "Economic" mode is ON.
658 * 2. "Fast" mode failed. It can only fail if there is no memory.
660 if (!fast || error != 0)
661 error = g_stripe_start_economic(bp, no, offset, length);
663 if (bp->bio_error == 0)
664 bp->bio_error = error;
665 g_io_deliver(bp, bp->bio_error);
670 g_stripe_check_and_run(struct g_stripe_softc *sc)
672 struct g_provider *dp;
674 u_int no, sectorsize = 0;
677 if (g_stripe_nvalid(sc) != sc->sc_ndisks)
680 sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
682 sc->sc_provider->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
683 if (g_stripe_fast == 0)
684 sc->sc_provider->flags |= G_PF_ACCEPT_UNMAPPED;
686 * Find the smallest disk.
688 mediasize = sc->sc_disks[0]->provider->mediasize;
689 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
690 mediasize -= sc->sc_disks[0]->provider->sectorsize;
691 mediasize -= mediasize % sc->sc_stripesize;
692 sectorsize = sc->sc_disks[0]->provider->sectorsize;
693 for (no = 1; no < sc->sc_ndisks; no++) {
694 dp = sc->sc_disks[no]->provider;
696 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
697 ms -= dp->sectorsize;
698 ms -= ms % sc->sc_stripesize;
701 sectorsize = lcm(sectorsize, dp->sectorsize);
703 /* A provider underneath us doesn't support unmapped */
704 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
705 G_STRIPE_DEBUG(1, "Cancelling unmapped "
706 "because of %s.", dp->name);
707 sc->sc_provider->flags &= ~G_PF_ACCEPT_UNMAPPED;
710 sc->sc_provider->sectorsize = sectorsize;
711 sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
712 sc->sc_provider->stripesize = sc->sc_stripesize;
713 sc->sc_provider->stripeoffset = 0;
714 g_error_provider(sc->sc_provider, 0);
716 G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
720 g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
722 struct g_provider *pp;
728 error = g_access(cp, 1, 0, 0);
733 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
736 g_access(cp, -1, 0, 0);
740 /* Decode metadata. */
741 stripe_metadata_decode(buf, md);
748 * Add disk to given device.
751 g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
753 struct g_consumer *cp, *fcp;
758 /* Metadata corrupted? */
759 if (no >= sc->sc_ndisks)
762 /* Check if disk is not already attached. */
763 if (sc->sc_disks[no] != NULL)
767 fcp = LIST_FIRST(&gp->consumer);
769 cp = g_new_consumer(gp);
770 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
773 error = g_attach(cp, pp);
775 g_destroy_consumer(cp);
779 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
780 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
783 g_destroy_consumer(cp);
787 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
788 struct g_stripe_metadata md;
790 /* Reread metadata. */
791 error = g_stripe_read_metadata(cp, &md);
795 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
796 strcmp(md.md_name, sc->sc_name) != 0 ||
797 md.md_id != sc->sc_id) {
798 G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
803 sc->sc_disks[no] = cp;
804 G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
805 g_stripe_check_and_run(sc);
809 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
810 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
812 g_destroy_consumer(cp);
816 static struct g_geom *
817 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
820 struct g_stripe_softc *sc;
825 G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
828 /* Two disks is minimum. */
829 if (md->md_all < 2) {
830 G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
834 /* Stripe size have to be grater than or equal to sector size. */
835 if (md->md_stripesize < sectorsize) {
836 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
840 /* Stripe size have to be power of 2. */
841 if (!powerof2(md->md_stripesize)) {
842 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
846 /* Check for duplicate unit */
847 LIST_FOREACH(gp, &mp->geom, geom) {
849 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
850 G_STRIPE_DEBUG(0, "Device %s already configured.",
855 gp = g_new_geomf(mp, "%s", md->md_name);
856 sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
857 gp->start = g_stripe_start;
858 gp->spoiled = g_stripe_orphan;
859 gp->orphan = g_stripe_orphan;
860 gp->access = g_stripe_access;
861 gp->dumpconf = g_stripe_dumpconf;
863 sc->sc_id = md->md_id;
864 sc->sc_stripesize = md->md_stripesize;
865 sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
866 sc->sc_ndisks = md->md_all;
867 sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
868 M_STRIPE, M_WAITOK | M_ZERO);
869 for (no = 0; no < sc->sc_ndisks; no++)
870 sc->sc_disks[no] = NULL;
872 mtx_init(&sc->sc_lock, "gstripe lock", NULL, MTX_DEF);
876 sc->sc_provider = NULL;
878 G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
884 g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
886 struct g_provider *pp;
887 struct g_consumer *cp, *cp1;
895 pp = sc->sc_provider;
896 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
898 G_STRIPE_DEBUG(0, "Device %s is still open, so it "
899 "can't be definitely removed.", pp->name);
902 "Device %s is still open (r%dw%de%d).", pp->name,
903 pp->acr, pp->acw, pp->ace);
909 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
910 g_stripe_remove_disk(cp);
912 return (0); /* Recursion happened. */
914 if (!LIST_EMPTY(&gp->consumer))
915 return (EINPROGRESS);
918 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
920 free(sc->sc_disks, M_STRIPE);
921 mtx_destroy(&sc->sc_lock);
923 G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
924 g_wither_geom(gp, ENXIO);
929 g_stripe_destroy_geom(struct gctl_req *req __unused,
930 struct g_class *mp __unused, struct g_geom *gp)
932 struct g_stripe_softc *sc;
935 return (g_stripe_destroy(sc, 0));
938 static struct g_geom *
939 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
941 struct g_stripe_metadata md;
942 struct g_stripe_softc *sc;
943 struct g_consumer *cp;
947 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
950 /* Skip providers that are already open for writing. */
954 G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
956 gp = g_new_geomf(mp, "stripe:taste");
957 gp->start = g_stripe_start;
958 gp->access = g_stripe_access;
959 gp->orphan = g_stripe_orphan;
960 cp = g_new_consumer(gp);
962 error = g_stripe_read_metadata(cp, &md);
964 g_destroy_consumer(cp);
970 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
972 if (md.md_version > G_STRIPE_VERSION) {
973 printf("geom_stripe.ko module is too old to handle %s.\n",
978 * Backward compatibility:
980 /* There was no md_provider field in earlier versions of metadata. */
981 if (md.md_version < 2)
982 bzero(md.md_provider, sizeof(md.md_provider));
983 /* There was no md_provsize field in earlier versions of metadata. */
984 if (md.md_version < 3)
985 md.md_provsize = pp->mediasize;
987 if (md.md_provider[0] != '\0' &&
988 !g_compare_names(md.md_provider, pp->name))
990 if (md.md_provsize != pp->mediasize)
994 * Let's check if device already exists.
997 LIST_FOREACH(gp, &mp->geom, geom) {
1001 if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
1003 if (strcmp(md.md_name, sc->sc_name) != 0)
1005 if (md.md_id != sc->sc_id)
1010 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1011 error = g_stripe_add_disk(sc, pp, md.md_no);
1014 "Cannot add disk %s to %s (error=%d).", pp->name,
1019 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
1021 G_STRIPE_DEBUG(0, "Cannot create device %s.",
1026 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1027 error = g_stripe_add_disk(sc, pp, md.md_no);
1030 "Cannot add disk %s to %s (error=%d).", pp->name,
1032 g_stripe_destroy(sc, 1);
1041 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1044 struct g_stripe_metadata md;
1045 struct g_provider *pp;
1046 struct g_stripe_softc *sc;
1054 g_topology_assert();
1055 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1056 if (nargs == NULL) {
1057 gctl_error(req, "No '%s' argument.", "nargs");
1061 gctl_error(req, "Too few arguments.");
1065 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1066 md.md_version = G_STRIPE_VERSION;
1067 name = gctl_get_asciiparam(req, "arg0");
1069 gctl_error(req, "No 'arg%u' argument.", 0);
1072 strlcpy(md.md_name, name, sizeof(md.md_name));
1073 md.md_id = arc4random();
1075 md.md_all = *nargs - 1;
1076 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1077 if (stripesize == NULL) {
1078 gctl_error(req, "No '%s' argument.", "stripesize");
1081 md.md_stripesize = (uint32_t)*stripesize;
1082 bzero(md.md_provider, sizeof(md.md_provider));
1083 /* This field is not important here. */
1086 /* Check all providers are valid */
1087 for (no = 1; no < *nargs; no++) {
1088 snprintf(param, sizeof(param), "arg%u", no);
1089 name = gctl_get_asciiparam(req, param);
1091 gctl_error(req, "No 'arg%u' argument.", no);
1094 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1095 name += strlen("/dev/");
1096 pp = g_provider_by_name(name);
1098 G_STRIPE_DEBUG(1, "Disk %s is invalid.", name);
1099 gctl_error(req, "Disk %s is invalid.", name);
1104 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1106 gctl_error(req, "Can't configure %s.", md.md_name);
1111 sb = sbuf_new_auto();
1112 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1113 for (attached = 0, no = 1; no < *nargs; no++) {
1114 snprintf(param, sizeof(param), "arg%u", no);
1115 name = gctl_get_asciiparam(req, param);
1117 gctl_error(req, "No 'arg%u' argument.", no);
1120 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1121 name += strlen("/dev/");
1122 pp = g_provider_by_name(name);
1123 KASSERT(pp != NULL, ("Provider %s disappear?!", name));
1124 if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1125 G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1126 no, pp->name, gp->name);
1127 sbuf_printf(sb, " %s", pp->name);
1133 if (md.md_all != attached) {
1134 g_stripe_destroy(gp->softc, 1);
1135 gctl_error(req, "%s", sbuf_data(sb));
1140 static struct g_stripe_softc *
1141 g_stripe_find_device(struct g_class *mp, const char *name)
1143 struct g_stripe_softc *sc;
1146 LIST_FOREACH(gp, &mp->geom, geom) {
1150 if (strcmp(sc->sc_name, name) == 0)
1157 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1159 struct g_stripe_softc *sc;
1160 int *force, *nargs, error;
1165 g_topology_assert();
1167 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1168 if (nargs == NULL) {
1169 gctl_error(req, "No '%s' argument.", "nargs");
1173 gctl_error(req, "Missing device(s).");
1176 force = gctl_get_paraml(req, "force", sizeof(*force));
1177 if (force == NULL) {
1178 gctl_error(req, "No '%s' argument.", "force");
1182 for (i = 0; i < (u_int)*nargs; i++) {
1183 snprintf(param, sizeof(param), "arg%u", i);
1184 name = gctl_get_asciiparam(req, param);
1186 gctl_error(req, "No 'arg%u' argument.", i);
1189 sc = g_stripe_find_device(mp, name);
1191 gctl_error(req, "No such device: %s.", name);
1194 error = g_stripe_destroy(sc, *force);
1196 gctl_error(req, "Cannot destroy device %s (error=%d).",
1197 sc->sc_name, error);
1204 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1208 g_topology_assert();
1210 version = gctl_get_paraml(req, "version", sizeof(*version));
1211 if (version == NULL) {
1212 gctl_error(req, "No '%s' argument.", "version");
1215 if (*version != G_STRIPE_VERSION) {
1216 gctl_error(req, "Userland and kernel parts are out of sync.");
1220 if (strcmp(verb, "create") == 0) {
1221 g_stripe_ctl_create(req, mp);
1223 } else if (strcmp(verb, "destroy") == 0 ||
1224 strcmp(verb, "stop") == 0) {
1225 g_stripe_ctl_destroy(req, mp);
1229 gctl_error(req, "Unknown verb.");
1233 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1234 struct g_consumer *cp, struct g_provider *pp)
1236 struct g_stripe_softc *sc;
1243 } else if (cp != NULL) {
1244 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1247 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1248 sbuf_printf(sb, "%s<Stripesize>%ju</Stripesize>\n", indent,
1249 (uintmax_t)sc->sc_stripesize);
1250 sbuf_printf(sb, "%s<Type>", indent);
1251 switch (sc->sc_type) {
1252 case G_STRIPE_TYPE_AUTOMATIC:
1253 sbuf_cat(sb, "AUTOMATIC");
1255 case G_STRIPE_TYPE_MANUAL:
1256 sbuf_cat(sb, "MANUAL");
1259 sbuf_cat(sb, "UNKNOWN");
1262 sbuf_cat(sb, "</Type>\n");
1263 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1264 indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1265 sbuf_printf(sb, "%s<State>", indent);
1266 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1269 sbuf_cat(sb, "DOWN");
1270 sbuf_cat(sb, "</State>\n");
1274 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);
1275 MODULE_VERSION(geom_stripe, 0);