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 | CTLFLAG_MPSAFE, 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,
92 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, NULL, 0,
93 g_sysctl_stripe_fast, "I",
94 "Fast, but memory-consuming, mode");
95 static u_long g_stripe_maxmem;
96 SYSCTL_ULONG(_kern_geom_stripe, OID_AUTO, maxmem,
97 CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &g_stripe_maxmem, 0,
98 "Maximum memory that can be allocated in \"fast\" mode (in bytes)");
99 static u_int g_stripe_fast_failed = 0;
100 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, fast_failed, CTLFLAG_RD,
101 &g_stripe_fast_failed, 0, "How many times \"fast\" mode failed");
104 * Greatest Common Divisor.
107 gcd(u_int a, u_int b)
120 * Least Common Multiple.
123 lcm(u_int a, u_int b)
126 return ((a * b) / gcd(a, b));
130 g_stripe_init(struct g_class *mp __unused)
133 g_stripe_maxmem = maxphys * 100;
134 TUNABLE_ULONG_FETCH("kern.geom.stripe.maxmem,", &g_stripe_maxmem);
135 g_stripe_zone = uma_zcreate("g_stripe_zone", maxphys, NULL, NULL,
137 g_stripe_maxmem -= g_stripe_maxmem % maxphys;
138 uma_zone_set_max(g_stripe_zone, g_stripe_maxmem / maxphys);
142 g_stripe_fini(struct g_class *mp __unused)
145 uma_zdestroy(g_stripe_zone);
149 * Return the number of valid disks.
152 g_stripe_nvalid(struct g_stripe_softc *sc)
157 for (i = 0; i < sc->sc_ndisks; i++) {
158 if (sc->sc_disks[i] != NULL)
166 g_stripe_remove_disk(struct g_consumer *cp)
168 struct g_stripe_softc *sc;
171 KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
172 sc = (struct g_stripe_softc *)cp->geom->softc;
173 KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
175 if (cp->private == NULL) {
176 G_STRIPE_DEBUG(0, "Disk %s removed from %s.",
177 cp->provider->name, sc->sc_name);
178 cp->private = (void *)(uintptr_t)-1;
181 if (sc->sc_provider != NULL) {
182 G_STRIPE_DEBUG(0, "Device %s deactivated.",
183 sc->sc_provider->name);
184 g_wither_provider(sc->sc_provider, ENXIO);
185 sc->sc_provider = NULL;
188 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
190 sc->sc_disks[cp->index] = NULL;
193 g_destroy_consumer(cp);
194 /* If there are no valid disks anymore, remove device. */
195 if (LIST_EMPTY(&sc->sc_geom->consumer))
196 g_stripe_destroy(sc, 1);
200 g_stripe_orphan(struct g_consumer *cp)
202 struct g_stripe_softc *sc;
211 g_stripe_remove_disk(cp);
215 g_stripe_access(struct g_provider *pp, int dr, int dw, int de)
217 struct g_consumer *cp1, *cp2, *tmp;
218 struct g_stripe_softc *sc;
225 KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
227 /* On first open, grab an extra "exclusive" bit */
228 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
230 /* ... and let go of it on last close */
231 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
234 LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) {
235 error = g_access(cp1, dr, dw, de);
238 if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 &&
239 cp1->private != NULL) {
240 g_stripe_remove_disk(cp1); /* May destroy geom. */
246 LIST_FOREACH(cp2, &gp->consumer, consumer) {
249 g_access(cp2, -dr, -dw, -de);
255 g_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset,
256 off_t length, int mode)
261 stripesize = sc->sc_stripesize;
262 len = (size_t)(stripesize - (offset & (stripesize - 1)));
264 bcopy(src, dst, len);
266 dst += len + stripesize * (sc->sc_ndisks - 1);
270 src += len + stripesize * (sc->sc_ndisks - 1);
274 ("Length < 0 (stripesize=%ju, offset=%ju, length=%jd).",
275 (uintmax_t)stripesize, (uintmax_t)offset, (intmax_t)length));
276 if (length > stripesize)
280 } while (length > 0);
284 g_stripe_done(struct bio *bp)
286 struct g_stripe_softc *sc;
289 pbp = bp->bio_parent;
290 sc = pbp->bio_to->geom->softc;
291 if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) {
292 g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset,
294 bp->bio_data = bp->bio_caller1;
295 bp->bio_caller1 = NULL;
297 mtx_lock(&sc->sc_lock);
298 if (pbp->bio_error == 0)
299 pbp->bio_error = bp->bio_error;
300 pbp->bio_completed += bp->bio_completed;
302 if (pbp->bio_children == pbp->bio_inbed) {
303 mtx_unlock(&sc->sc_lock);
304 if (pbp->bio_driver1 != NULL)
305 uma_zfree(g_stripe_zone, pbp->bio_driver1);
306 if (bp->bio_cmd == BIO_SPEEDUP)
307 pbp->bio_completed = pbp->bio_length;
308 g_io_deliver(pbp, pbp->bio_error);
310 mtx_unlock(&sc->sc_lock);
315 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
317 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
318 struct g_stripe_softc *sc;
319 char *addr, *data = NULL;
325 sc = bp->bio_to->geom->softc;
328 stripesize = sc->sc_stripesize;
330 cbp = g_clone_bio(bp);
335 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
338 * Fill in the component buf structure.
340 cbp->bio_done = g_stripe_done;
341 cbp->bio_offset = offset;
342 cbp->bio_data = addr;
343 cbp->bio_caller1 = NULL;
344 cbp->bio_length = length;
345 cbp->bio_caller2 = sc->sc_disks[no];
347 /* offset -= offset % stripesize; */
348 offset -= offset & (stripesize - 1);
350 length = bp->bio_length - length;
351 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
352 if (no > sc->sc_ndisks - 1) {
354 offset += stripesize;
356 if (nparts >= sc->sc_ndisks) {
357 cbp = TAILQ_NEXT(cbp, bio_queue);
359 cbp = TAILQ_FIRST(&queue);
362 * Update bio structure.
365 * MIN() is in case when
366 * (bp->bio_length % sc->sc_stripesize) != 0.
368 cbp->bio_length += MIN(stripesize, length);
369 if (cbp->bio_caller1 == NULL) {
370 cbp->bio_caller1 = cbp->bio_data;
371 cbp->bio_data = NULL;
373 data = uma_zalloc(g_stripe_zone,
382 cbp = g_clone_bio(bp);
387 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
390 * Fill in the component buf structure.
392 cbp->bio_done = g_stripe_done;
393 cbp->bio_offset = offset;
394 cbp->bio_data = addr;
395 cbp->bio_caller1 = NULL;
397 * MIN() is in case when
398 * (bp->bio_length % sc->sc_stripesize) != 0.
400 cbp->bio_length = MIN(stripesize, length);
401 cbp->bio_caller2 = sc->sc_disks[no];
405 bp->bio_driver1 = data;
407 * Fire off all allocated requests!
409 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
410 struct g_consumer *cp;
412 TAILQ_REMOVE(&queue, cbp, bio_queue);
413 cp = cbp->bio_caller2;
414 cbp->bio_caller2 = NULL;
415 cbp->bio_to = cp->provider;
416 if (cbp->bio_caller1 != NULL) {
417 cbp->bio_data = data;
418 if (bp->bio_cmd == BIO_WRITE) {
419 g_stripe_copy(sc, cbp->bio_caller1, data,
420 cbp->bio_offset, cbp->bio_length, 0);
422 data += cbp->bio_length;
424 G_STRIPE_LOGREQ(cbp, "Sending request.");
425 g_io_request(cbp, cp);
430 uma_zfree(g_stripe_zone, data);
431 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
432 TAILQ_REMOVE(&queue, cbp, bio_queue);
433 if (cbp->bio_caller1 != NULL) {
434 cbp->bio_data = cbp->bio_caller1;
435 cbp->bio_caller1 = NULL;
444 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
446 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
447 struct g_stripe_softc *sc;
453 sc = bp->bio_to->geom->softc;
455 stripesize = sc->sc_stripesize;
457 cbp = g_clone_bio(bp);
462 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
464 * Fill in the component buf structure.
466 if (bp->bio_length == length)
467 cbp->bio_done = g_std_done; /* Optimized lockless case. */
469 cbp->bio_done = g_stripe_done;
470 cbp->bio_offset = offset;
471 cbp->bio_length = length;
472 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
473 bp->bio_ma_n = round_page(bp->bio_ma_offset +
474 bp->bio_length) / PAGE_SIZE;
478 cbp->bio_caller2 = sc->sc_disks[no];
480 /* offset -= offset % stripesize; */
481 offset -= offset & (stripesize - 1);
482 if (bp->bio_cmd != BIO_DELETE)
484 length = bp->bio_length - length;
485 for (no++; length > 0; no++, length -= stripesize) {
486 if (no > sc->sc_ndisks - 1) {
488 offset += stripesize;
490 cbp = g_clone_bio(bp);
495 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
498 * Fill in the component buf structure.
500 cbp->bio_done = g_stripe_done;
501 cbp->bio_offset = offset;
503 * MIN() is in case when
504 * (bp->bio_length % sc->sc_stripesize) != 0.
506 cbp->bio_length = MIN(stripesize, length);
507 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
508 cbp->bio_ma_offset += (uintptr_t)addr;
509 cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
510 cbp->bio_ma_offset %= PAGE_SIZE;
511 cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
512 cbp->bio_length) / PAGE_SIZE;
514 cbp->bio_data = addr;
516 cbp->bio_caller2 = sc->sc_disks[no];
518 if (bp->bio_cmd != BIO_DELETE)
522 * Fire off all allocated requests!
524 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
525 struct g_consumer *cp;
527 TAILQ_REMOVE(&queue, cbp, bio_queue);
528 cp = cbp->bio_caller2;
529 cbp->bio_caller2 = NULL;
530 cbp->bio_to = cp->provider;
531 G_STRIPE_LOGREQ(cbp, "Sending request.");
532 g_io_request(cbp, cp);
536 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
537 TAILQ_REMOVE(&queue, cbp, bio_queue);
545 g_stripe_pushdown(struct g_stripe_softc *sc, struct bio *bp)
547 struct bio_queue_head queue;
548 struct g_consumer *cp;
553 for (no = 0; no < sc->sc_ndisks; no++) {
554 cbp = g_clone_bio(bp);
556 for (cbp = bioq_first(&queue); cbp != NULL;
557 cbp = bioq_first(&queue)) {
558 bioq_remove(&queue, cbp);
561 if (bp->bio_error == 0)
562 bp->bio_error = ENOMEM;
563 g_io_deliver(bp, bp->bio_error);
566 bioq_insert_tail(&queue, cbp);
567 cbp->bio_done = g_stripe_done;
568 cbp->bio_caller2 = sc->sc_disks[no];
569 cbp->bio_to = sc->sc_disks[no]->provider;
571 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
572 bioq_remove(&queue, cbp);
573 G_STRIPE_LOGREQ(cbp, "Sending request.");
574 cp = cbp->bio_caller2;
575 cbp->bio_caller2 = NULL;
576 g_io_request(cbp, cp);
581 g_stripe_start(struct bio *bp)
583 off_t offset, start, length, nstripe, stripesize;
584 struct g_stripe_softc *sc;
588 sc = bp->bio_to->geom->softc;
590 * If sc == NULL, provider's error should be set and g_stripe_start()
591 * should not be called at all.
594 ("Provider's error should be set (error=%d)(device=%s).",
595 bp->bio_to->error, bp->bio_to->name));
597 G_STRIPE_LOGREQ(bp, "Request received.");
599 switch (bp->bio_cmd) {
606 g_stripe_pushdown(sc, bp);
609 /* To which provider it should be delivered? */
611 g_io_deliver(bp, EOPNOTSUPP);
615 stripesize = sc->sc_stripesize;
618 * Calculations are quite messy, but fast I hope.
622 /* nstripe = bp->bio_offset / stripesize; */
623 nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
625 no = nstripe % sc->sc_ndisks;
626 /* Start position in stripe. */
627 /* start = bp->bio_offset % stripesize; */
628 start = bp->bio_offset & (stripesize - 1);
629 /* Start position in disk. */
630 /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
631 offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
632 /* Length of data to operate. */
633 length = MIN(bp->bio_length, stripesize - start);
636 * Do use "fast" mode when:
637 * 1. "Fast" mode is ON.
639 * 2. Request size is less than or equal to maxphys,
640 * which should always be true.
642 * 3. Request size is bigger than stripesize * ndisks. If it isn't,
643 * there will be no need to send more than one I/O request to
644 * a provider, so there is nothing to optmize.
646 * 4. Request is not unmapped.
648 * 5. It is not a BIO_DELETE.
650 if (g_stripe_fast && bp->bio_length <= maxphys &&
651 bp->bio_length >= stripesize * sc->sc_ndisks &&
652 (bp->bio_flags & BIO_UNMAPPED) == 0 &&
653 bp->bio_cmd != BIO_DELETE) {
658 error = g_stripe_start_fast(bp, no, offset, length);
660 g_stripe_fast_failed++;
663 * Do use "economic" when:
664 * 1. "Economic" mode is ON.
666 * 2. "Fast" mode failed. It can only fail if there is no memory.
668 if (!fast || error != 0)
669 error = g_stripe_start_economic(bp, no, offset, length);
671 if (bp->bio_error == 0)
672 bp->bio_error = error;
673 g_io_deliver(bp, bp->bio_error);
678 g_stripe_check_and_run(struct g_stripe_softc *sc)
680 struct g_provider *dp;
682 u_int no, sectorsize = 0;
685 if (g_stripe_nvalid(sc) != sc->sc_ndisks)
688 sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
690 sc->sc_provider->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
691 if (g_stripe_fast == 0)
692 sc->sc_provider->flags |= G_PF_ACCEPT_UNMAPPED;
694 * Find the smallest disk.
696 mediasize = sc->sc_disks[0]->provider->mediasize;
697 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
698 mediasize -= sc->sc_disks[0]->provider->sectorsize;
699 mediasize -= mediasize % sc->sc_stripesize;
700 sectorsize = sc->sc_disks[0]->provider->sectorsize;
701 for (no = 1; no < sc->sc_ndisks; no++) {
702 dp = sc->sc_disks[no]->provider;
704 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
705 ms -= dp->sectorsize;
706 ms -= ms % sc->sc_stripesize;
709 sectorsize = lcm(sectorsize, dp->sectorsize);
711 /* A provider underneath us doesn't support unmapped */
712 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
713 G_STRIPE_DEBUG(1, "Cancelling unmapped "
714 "because of %s.", dp->name);
715 sc->sc_provider->flags &= ~G_PF_ACCEPT_UNMAPPED;
718 sc->sc_provider->sectorsize = sectorsize;
719 sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
720 sc->sc_provider->stripesize = sc->sc_stripesize;
721 sc->sc_provider->stripeoffset = 0;
722 g_error_provider(sc->sc_provider, 0);
724 G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
728 g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
730 struct g_provider *pp;
736 error = g_access(cp, 1, 0, 0);
741 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
744 g_access(cp, -1, 0, 0);
748 /* Decode metadata. */
749 stripe_metadata_decode(buf, md);
756 * Add disk to given device.
759 g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
761 struct g_consumer *cp, *fcp;
766 /* Metadata corrupted? */
767 if (no >= sc->sc_ndisks)
770 /* Check if disk is not already attached. */
771 if (sc->sc_disks[no] != NULL)
775 fcp = LIST_FIRST(&gp->consumer);
777 cp = g_new_consumer(gp);
778 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
781 error = g_attach(cp, pp);
783 g_destroy_consumer(cp);
787 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
788 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
791 g_destroy_consumer(cp);
795 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
796 struct g_stripe_metadata md;
798 /* Reread metadata. */
799 error = g_stripe_read_metadata(cp, &md);
803 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
804 strcmp(md.md_name, sc->sc_name) != 0 ||
805 md.md_id != sc->sc_id) {
806 G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
811 sc->sc_disks[no] = cp;
812 G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
813 g_stripe_check_and_run(sc);
817 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
818 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
820 g_destroy_consumer(cp);
824 static struct g_geom *
825 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
828 struct g_stripe_softc *sc;
833 G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
836 /* Two disks is minimum. */
837 if (md->md_all < 2) {
838 G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
842 /* Stripe size have to be grater than or equal to sector size. */
843 if (md->md_stripesize < sectorsize) {
844 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
848 /* Stripe size have to be power of 2. */
849 if (!powerof2(md->md_stripesize)) {
850 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
854 /* Check for duplicate unit */
855 LIST_FOREACH(gp, &mp->geom, geom) {
857 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
858 G_STRIPE_DEBUG(0, "Device %s already configured.",
863 gp = g_new_geomf(mp, "%s", md->md_name);
864 sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
865 gp->start = g_stripe_start;
866 gp->spoiled = g_stripe_orphan;
867 gp->orphan = g_stripe_orphan;
868 gp->access = g_stripe_access;
869 gp->dumpconf = g_stripe_dumpconf;
871 sc->sc_id = md->md_id;
872 sc->sc_stripesize = md->md_stripesize;
873 sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
874 sc->sc_ndisks = md->md_all;
875 sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
876 M_STRIPE, M_WAITOK | M_ZERO);
877 for (no = 0; no < sc->sc_ndisks; no++)
878 sc->sc_disks[no] = NULL;
880 mtx_init(&sc->sc_lock, "gstripe lock", NULL, MTX_DEF);
884 sc->sc_provider = NULL;
886 G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
892 g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
894 struct g_provider *pp;
895 struct g_consumer *cp, *cp1;
903 pp = sc->sc_provider;
904 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
906 G_STRIPE_DEBUG(0, "Device %s is still open, so it "
907 "can't be definitely removed.", pp->name);
910 "Device %s is still open (r%dw%de%d).", pp->name,
911 pp->acr, pp->acw, pp->ace);
917 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
918 g_stripe_remove_disk(cp);
920 return (0); /* Recursion happened. */
922 if (!LIST_EMPTY(&gp->consumer))
923 return (EINPROGRESS);
926 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
928 free(sc->sc_disks, M_STRIPE);
929 mtx_destroy(&sc->sc_lock);
931 G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
932 g_wither_geom(gp, ENXIO);
937 g_stripe_destroy_geom(struct gctl_req *req __unused,
938 struct g_class *mp __unused, struct g_geom *gp)
940 struct g_stripe_softc *sc;
943 return (g_stripe_destroy(sc, 0));
946 static struct g_geom *
947 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
949 struct g_stripe_metadata md;
950 struct g_stripe_softc *sc;
951 struct g_consumer *cp;
955 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
958 /* Skip providers that are already open for writing. */
962 G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
964 gp = g_new_geomf(mp, "stripe:taste");
965 gp->start = g_stripe_start;
966 gp->access = g_stripe_access;
967 gp->orphan = g_stripe_orphan;
968 cp = g_new_consumer(gp);
969 error = g_attach(cp, pp);
971 error = g_stripe_read_metadata(cp, &md);
974 g_destroy_consumer(cp);
980 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
982 if (md.md_version > G_STRIPE_VERSION) {
983 printf("geom_stripe.ko module is too old to handle %s.\n",
988 * Backward compatibility:
990 /* There was no md_provider field in earlier versions of metadata. */
991 if (md.md_version < 2)
992 bzero(md.md_provider, sizeof(md.md_provider));
993 /* There was no md_provsize field in earlier versions of metadata. */
994 if (md.md_version < 3)
995 md.md_provsize = pp->mediasize;
997 if (md.md_provider[0] != '\0' &&
998 !g_compare_names(md.md_provider, pp->name))
1000 if (md.md_provsize != pp->mediasize)
1004 * Let's check if device already exists.
1007 LIST_FOREACH(gp, &mp->geom, geom) {
1011 if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
1013 if (strcmp(md.md_name, sc->sc_name) != 0)
1015 if (md.md_id != sc->sc_id)
1020 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1021 error = g_stripe_add_disk(sc, pp, md.md_no);
1024 "Cannot add disk %s to %s (error=%d).", pp->name,
1029 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
1031 G_STRIPE_DEBUG(0, "Cannot create device %s.",
1036 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1037 error = g_stripe_add_disk(sc, pp, md.md_no);
1040 "Cannot add disk %s to %s (error=%d).", pp->name,
1042 g_stripe_destroy(sc, 1);
1051 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1054 struct g_stripe_metadata md;
1055 struct g_provider *pp;
1056 struct g_stripe_softc *sc;
1064 g_topology_assert();
1065 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1066 if (nargs == NULL) {
1067 gctl_error(req, "No '%s' argument.", "nargs");
1071 gctl_error(req, "Too few arguments.");
1075 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1076 md.md_version = G_STRIPE_VERSION;
1077 name = gctl_get_asciiparam(req, "arg0");
1079 gctl_error(req, "No 'arg%u' argument.", 0);
1082 strlcpy(md.md_name, name, sizeof(md.md_name));
1083 md.md_id = arc4random();
1085 md.md_all = *nargs - 1;
1086 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1087 if (stripesize == NULL) {
1088 gctl_error(req, "No '%s' argument.", "stripesize");
1091 md.md_stripesize = (uint32_t)*stripesize;
1092 bzero(md.md_provider, sizeof(md.md_provider));
1093 /* This field is not important here. */
1096 /* Check all providers are valid */
1097 for (no = 1; no < *nargs; no++) {
1098 snprintf(param, sizeof(param), "arg%u", no);
1099 pp = gctl_get_provider(req, param);
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 pp = gctl_get_provider(req, param);
1117 name = gctl_get_asciiparam(req, param);
1118 MPASS(name != NULL);
1119 sbuf_printf(sb, " %s", name);
1122 if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1123 G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1124 no, pp->name, gp->name);
1125 sbuf_printf(sb, " %s", pp->name);
1131 if (md.md_all != attached) {
1132 g_stripe_destroy(gp->softc, 1);
1133 gctl_error(req, "%s", sbuf_data(sb));
1138 static struct g_stripe_softc *
1139 g_stripe_find_device(struct g_class *mp, const char *name)
1141 struct g_stripe_softc *sc;
1144 LIST_FOREACH(gp, &mp->geom, geom) {
1148 if (strcmp(sc->sc_name, name) == 0)
1155 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1157 struct g_stripe_softc *sc;
1158 int *force, *nargs, error;
1163 g_topology_assert();
1165 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1166 if (nargs == NULL) {
1167 gctl_error(req, "No '%s' argument.", "nargs");
1171 gctl_error(req, "Missing device(s).");
1174 force = gctl_get_paraml(req, "force", sizeof(*force));
1175 if (force == NULL) {
1176 gctl_error(req, "No '%s' argument.", "force");
1180 for (i = 0; i < (u_int)*nargs; i++) {
1181 snprintf(param, sizeof(param), "arg%u", i);
1182 name = gctl_get_asciiparam(req, param);
1184 gctl_error(req, "No 'arg%u' argument.", i);
1187 sc = g_stripe_find_device(mp, name);
1189 gctl_error(req, "No such device: %s.", name);
1192 error = g_stripe_destroy(sc, *force);
1194 gctl_error(req, "Cannot destroy device %s (error=%d).",
1195 sc->sc_name, error);
1202 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1206 g_topology_assert();
1208 version = gctl_get_paraml(req, "version", sizeof(*version));
1209 if (version == NULL) {
1210 gctl_error(req, "No '%s' argument.", "version");
1213 if (*version != G_STRIPE_VERSION) {
1214 gctl_error(req, "Userland and kernel parts are out of sync.");
1218 if (strcmp(verb, "create") == 0) {
1219 g_stripe_ctl_create(req, mp);
1221 } else if (strcmp(verb, "destroy") == 0 ||
1222 strcmp(verb, "stop") == 0) {
1223 g_stripe_ctl_destroy(req, mp);
1227 gctl_error(req, "Unknown verb.");
1231 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1232 struct g_consumer *cp, struct g_provider *pp)
1234 struct g_stripe_softc *sc;
1241 } else if (cp != NULL) {
1242 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1245 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1246 sbuf_printf(sb, "%s<Stripesize>%ju</Stripesize>\n", indent,
1247 (uintmax_t)sc->sc_stripesize);
1248 sbuf_printf(sb, "%s<Type>", indent);
1249 switch (sc->sc_type) {
1250 case G_STRIPE_TYPE_AUTOMATIC:
1251 sbuf_cat(sb, "AUTOMATIC");
1253 case G_STRIPE_TYPE_MANUAL:
1254 sbuf_cat(sb, "MANUAL");
1257 sbuf_cat(sb, "UNKNOWN");
1260 sbuf_cat(sb, "</Type>\n");
1261 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1262 indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1263 sbuf_printf(sb, "%s<State>", indent);
1264 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1267 sbuf_cat(sb, "DOWN");
1268 sbuf_cat(sb, "</State>\n");
1272 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);
1273 MODULE_VERSION(geom_stripe, 0);