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;
80 SYSCTL_INT(_kern_geom_stripe, OID_AUTO, fast,
81 CTLFLAG_RWTUN, &g_stripe_fast, 0,
82 "Fast, but memory-consuming, mode");
83 static u_long g_stripe_maxmem;
84 SYSCTL_ULONG(_kern_geom_stripe, OID_AUTO, maxmem,
85 CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &g_stripe_maxmem, 0,
86 "Maximum memory that can be allocated in \"fast\" mode (in bytes)");
87 static u_int g_stripe_fast_failed = 0;
88 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, fast_failed, CTLFLAG_RD,
89 &g_stripe_fast_failed, 0, "How many times \"fast\" mode failed");
92 * Greatest Common Divisor.
108 * Least Common Multiple.
111 lcm(u_int a, u_int b)
114 return ((a * b) / gcd(a, b));
118 g_stripe_init(struct g_class *mp __unused)
121 g_stripe_maxmem = maxphys * 100;
122 TUNABLE_ULONG_FETCH("kern.geom.stripe.maxmem,", &g_stripe_maxmem);
123 g_stripe_zone = uma_zcreate("g_stripe_zone", maxphys, NULL, NULL,
125 g_stripe_maxmem -= g_stripe_maxmem % maxphys;
126 uma_zone_set_max(g_stripe_zone, g_stripe_maxmem / maxphys);
130 g_stripe_fini(struct g_class *mp __unused)
133 uma_zdestroy(g_stripe_zone);
137 * Return the number of valid disks.
140 g_stripe_nvalid(struct g_stripe_softc *sc)
145 for (i = 0; i < sc->sc_ndisks; i++) {
146 if (sc->sc_disks[i] != NULL)
154 g_stripe_remove_disk(struct g_consumer *cp)
156 struct g_stripe_softc *sc;
159 KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
160 sc = (struct g_stripe_softc *)cp->geom->softc;
161 KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
163 if (cp->private == NULL) {
164 G_STRIPE_DEBUG(0, "Disk %s removed from %s.",
165 cp->provider->name, sc->sc_name);
166 cp->private = (void *)(uintptr_t)-1;
169 if (sc->sc_provider != NULL) {
170 G_STRIPE_DEBUG(0, "Device %s deactivated.",
171 sc->sc_provider->name);
172 g_wither_provider(sc->sc_provider, ENXIO);
173 sc->sc_provider = NULL;
176 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
178 sc->sc_disks[cp->index] = NULL;
181 g_destroy_consumer(cp);
182 /* If there are no valid disks anymore, remove device. */
183 if (LIST_EMPTY(&sc->sc_geom->consumer))
184 g_stripe_destroy(sc, 1);
188 g_stripe_orphan(struct g_consumer *cp)
190 struct g_stripe_softc *sc;
199 g_stripe_remove_disk(cp);
203 g_stripe_access(struct g_provider *pp, int dr, int dw, int de)
205 struct g_consumer *cp1, *cp2, *tmp;
206 struct g_stripe_softc *sc __diagused;
213 KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
215 /* On first open, grab an extra "exclusive" bit */
216 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
218 /* ... and let go of it on last close */
219 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
222 LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) {
223 error = g_access(cp1, dr, dw, de);
226 if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 &&
227 cp1->private != NULL) {
228 g_stripe_remove_disk(cp1); /* May destroy geom. */
234 LIST_FOREACH(cp2, &gp->consumer, consumer) {
237 g_access(cp2, -dr, -dw, -de);
243 g_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset,
244 off_t length, int mode)
249 stripesize = sc->sc_stripesize;
250 len = (size_t)(stripesize - (offset & (stripesize - 1)));
252 bcopy(src, dst, len);
254 dst += len + stripesize * (sc->sc_ndisks - 1);
258 src += len + stripesize * (sc->sc_ndisks - 1);
262 ("Length < 0 (stripesize=%ju, offset=%ju, length=%jd).",
263 (uintmax_t)stripesize, (uintmax_t)offset, (intmax_t)length));
264 if (length > stripesize)
268 } while (length > 0);
272 g_stripe_done(struct bio *bp)
274 struct g_stripe_softc *sc;
277 pbp = bp->bio_parent;
278 sc = pbp->bio_to->geom->softc;
279 if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) {
280 g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset,
282 bp->bio_data = bp->bio_caller1;
283 bp->bio_caller1 = NULL;
285 mtx_lock(&sc->sc_lock);
286 if (pbp->bio_error == 0)
287 pbp->bio_error = bp->bio_error;
288 pbp->bio_completed += bp->bio_completed;
290 if (pbp->bio_children == pbp->bio_inbed) {
291 mtx_unlock(&sc->sc_lock);
292 if (pbp->bio_driver1 != NULL)
293 uma_zfree(g_stripe_zone, pbp->bio_driver1);
294 if (bp->bio_cmd == BIO_SPEEDUP)
295 pbp->bio_completed = pbp->bio_length;
296 g_io_deliver(pbp, pbp->bio_error);
298 mtx_unlock(&sc->sc_lock);
303 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
305 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
306 struct g_stripe_softc *sc;
307 char *addr, *data = NULL;
313 sc = bp->bio_to->geom->softc;
316 stripesize = sc->sc_stripesize;
318 cbp = g_clone_bio(bp);
323 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
326 * Fill in the component buf structure.
328 cbp->bio_done = g_stripe_done;
329 cbp->bio_offset = offset;
330 cbp->bio_data = addr;
331 cbp->bio_caller1 = NULL;
332 cbp->bio_length = length;
333 cbp->bio_caller2 = sc->sc_disks[no];
335 /* offset -= offset % stripesize; */
336 offset -= offset & (stripesize - 1);
338 length = bp->bio_length - length;
339 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
340 if (no > sc->sc_ndisks - 1) {
342 offset += stripesize;
344 if (nparts >= sc->sc_ndisks) {
345 cbp = TAILQ_NEXT(cbp, bio_queue);
347 cbp = TAILQ_FIRST(&queue);
350 * Update bio structure.
353 * MIN() is in case when
354 * (bp->bio_length % sc->sc_stripesize) != 0.
356 cbp->bio_length += MIN(stripesize, length);
357 if (cbp->bio_caller1 == NULL) {
358 cbp->bio_caller1 = cbp->bio_data;
359 cbp->bio_data = NULL;
361 data = uma_zalloc(g_stripe_zone,
370 cbp = g_clone_bio(bp);
375 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
378 * Fill in the component buf structure.
380 cbp->bio_done = g_stripe_done;
381 cbp->bio_offset = offset;
382 cbp->bio_data = addr;
383 cbp->bio_caller1 = NULL;
385 * MIN() is in case when
386 * (bp->bio_length % sc->sc_stripesize) != 0.
388 cbp->bio_length = MIN(stripesize, length);
389 cbp->bio_caller2 = sc->sc_disks[no];
393 bp->bio_driver1 = data;
395 * Fire off all allocated requests!
397 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
398 struct g_consumer *cp;
400 TAILQ_REMOVE(&queue, cbp, bio_queue);
401 cp = cbp->bio_caller2;
402 cbp->bio_caller2 = NULL;
403 cbp->bio_to = cp->provider;
404 if (cbp->bio_caller1 != NULL) {
405 cbp->bio_data = data;
406 if (bp->bio_cmd == BIO_WRITE) {
407 g_stripe_copy(sc, cbp->bio_caller1, data,
408 cbp->bio_offset, cbp->bio_length, 0);
410 data += cbp->bio_length;
412 G_STRIPE_LOGREQ(cbp, "Sending request.");
413 g_io_request(cbp, cp);
418 uma_zfree(g_stripe_zone, data);
419 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
420 TAILQ_REMOVE(&queue, cbp, bio_queue);
421 if (cbp->bio_caller1 != NULL) {
422 cbp->bio_data = cbp->bio_caller1;
423 cbp->bio_caller1 = NULL;
432 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
434 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
435 struct g_stripe_softc *sc;
441 sc = bp->bio_to->geom->softc;
443 stripesize = sc->sc_stripesize;
445 cbp = g_clone_bio(bp);
450 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
452 * Fill in the component buf structure.
454 if (bp->bio_length == length)
455 cbp->bio_done = g_std_done; /* Optimized lockless case. */
457 cbp->bio_done = g_stripe_done;
458 cbp->bio_offset = offset;
459 cbp->bio_length = length;
460 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
461 bp->bio_ma_n = round_page(bp->bio_ma_offset +
462 bp->bio_length) / PAGE_SIZE;
466 cbp->bio_caller2 = sc->sc_disks[no];
468 /* offset -= offset % stripesize; */
469 offset -= offset & (stripesize - 1);
470 if (bp->bio_cmd != BIO_DELETE)
472 length = bp->bio_length - length;
473 for (no++; length > 0; no++, length -= stripesize) {
474 if (no > sc->sc_ndisks - 1) {
476 offset += stripesize;
478 cbp = g_clone_bio(bp);
483 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
486 * Fill in the component buf structure.
488 cbp->bio_done = g_stripe_done;
489 cbp->bio_offset = offset;
491 * MIN() is in case when
492 * (bp->bio_length % sc->sc_stripesize) != 0.
494 cbp->bio_length = MIN(stripesize, length);
495 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
496 cbp->bio_ma_offset += (uintptr_t)addr;
497 cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
498 cbp->bio_ma_offset %= PAGE_SIZE;
499 cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
500 cbp->bio_length) / PAGE_SIZE;
502 cbp->bio_data = addr;
504 cbp->bio_caller2 = sc->sc_disks[no];
506 if (bp->bio_cmd != BIO_DELETE)
510 * Fire off all allocated requests!
512 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
513 struct g_consumer *cp;
515 TAILQ_REMOVE(&queue, cbp, bio_queue);
516 cp = cbp->bio_caller2;
517 cbp->bio_caller2 = NULL;
518 cbp->bio_to = cp->provider;
519 G_STRIPE_LOGREQ(cbp, "Sending request.");
520 g_io_request(cbp, cp);
524 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
525 TAILQ_REMOVE(&queue, cbp, bio_queue);
533 g_stripe_pushdown(struct g_stripe_softc *sc, struct bio *bp)
535 struct bio_queue_head queue;
536 struct g_consumer *cp;
541 for (no = 0; no < sc->sc_ndisks; no++) {
542 cbp = g_clone_bio(bp);
544 for (cbp = bioq_first(&queue); cbp != NULL;
545 cbp = bioq_first(&queue)) {
546 bioq_remove(&queue, cbp);
549 if (bp->bio_error == 0)
550 bp->bio_error = ENOMEM;
551 g_io_deliver(bp, bp->bio_error);
554 bioq_insert_tail(&queue, cbp);
555 cbp->bio_done = g_stripe_done;
556 cbp->bio_caller2 = sc->sc_disks[no];
557 cbp->bio_to = sc->sc_disks[no]->provider;
559 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
560 bioq_remove(&queue, cbp);
561 G_STRIPE_LOGREQ(cbp, "Sending request.");
562 cp = cbp->bio_caller2;
563 cbp->bio_caller2 = NULL;
564 g_io_request(cbp, cp);
569 g_stripe_start(struct bio *bp)
571 off_t offset, start, length, nstripe, stripesize;
572 struct g_stripe_softc *sc;
576 sc = bp->bio_to->geom->softc;
578 * If sc == NULL, provider's error should be set and g_stripe_start()
579 * should not be called at all.
582 ("Provider's error should be set (error=%d)(device=%s).",
583 bp->bio_to->error, bp->bio_to->name));
585 G_STRIPE_LOGREQ(bp, "Request received.");
587 switch (bp->bio_cmd) {
594 g_stripe_pushdown(sc, bp);
597 /* To which provider it should be delivered? */
599 g_io_deliver(bp, EOPNOTSUPP);
603 stripesize = sc->sc_stripesize;
606 * Calculations are quite messy, but fast I hope.
610 /* nstripe = bp->bio_offset / stripesize; */
611 nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
613 no = nstripe % sc->sc_ndisks;
614 /* Start position in stripe. */
615 /* start = bp->bio_offset % stripesize; */
616 start = bp->bio_offset & (stripesize - 1);
617 /* Start position in disk. */
618 /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
619 offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
620 /* Length of data to operate. */
621 length = MIN(bp->bio_length, stripesize - start);
624 * Do use "fast" mode when:
625 * 1. "Fast" mode is ON.
627 * 2. Request size is less than or equal to maxphys,
628 * which should always be true.
630 * 3. Request size is bigger than stripesize * ndisks. If it isn't,
631 * there will be no need to send more than one I/O request to
632 * a provider, so there is nothing to optmize.
634 * 4. Request is not unmapped.
636 * 5. It is not a BIO_DELETE.
638 if (g_stripe_fast && bp->bio_length <= maxphys &&
639 bp->bio_length >= stripesize * sc->sc_ndisks &&
640 (bp->bio_flags & BIO_UNMAPPED) == 0 &&
641 bp->bio_cmd != BIO_DELETE) {
646 error = g_stripe_start_fast(bp, no, offset, length);
648 g_stripe_fast_failed++;
651 * Do use "economic" when:
652 * 1. "Economic" mode is ON.
654 * 2. "Fast" mode failed. It can only fail if there is no memory.
656 if (!fast || error != 0)
657 error = g_stripe_start_economic(bp, no, offset, length);
659 if (bp->bio_error == 0)
660 bp->bio_error = error;
661 g_io_deliver(bp, bp->bio_error);
666 g_stripe_check_and_run(struct g_stripe_softc *sc)
668 struct g_provider *dp;
670 u_int no, sectorsize = 0;
673 if (g_stripe_nvalid(sc) != sc->sc_ndisks)
676 sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
678 sc->sc_provider->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
679 if (g_stripe_fast == 0)
680 sc->sc_provider->flags |= G_PF_ACCEPT_UNMAPPED;
682 * Find the smallest disk.
684 mediasize = sc->sc_disks[0]->provider->mediasize;
685 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
686 mediasize -= sc->sc_disks[0]->provider->sectorsize;
687 mediasize -= mediasize % sc->sc_stripesize;
688 sectorsize = sc->sc_disks[0]->provider->sectorsize;
689 for (no = 1; no < sc->sc_ndisks; no++) {
690 dp = sc->sc_disks[no]->provider;
692 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
693 ms -= dp->sectorsize;
694 ms -= ms % sc->sc_stripesize;
697 sectorsize = lcm(sectorsize, dp->sectorsize);
699 /* A provider underneath us doesn't support unmapped */
700 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
701 G_STRIPE_DEBUG(1, "Cancelling unmapped "
702 "because of %s.", dp->name);
703 sc->sc_provider->flags &= ~G_PF_ACCEPT_UNMAPPED;
706 sc->sc_provider->sectorsize = sectorsize;
707 sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
708 sc->sc_provider->stripesize = sc->sc_stripesize;
709 sc->sc_provider->stripeoffset = 0;
710 g_error_provider(sc->sc_provider, 0);
712 G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
716 g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
718 struct g_provider *pp;
724 error = g_access(cp, 1, 0, 0);
729 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
732 g_access(cp, -1, 0, 0);
736 /* Decode metadata. */
737 stripe_metadata_decode(buf, md);
744 * Add disk to given device.
747 g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
749 struct g_consumer *cp, *fcp;
754 /* Metadata corrupted? */
755 if (no >= sc->sc_ndisks)
758 /* Check if disk is not already attached. */
759 if (sc->sc_disks[no] != NULL)
763 fcp = LIST_FIRST(&gp->consumer);
765 cp = g_new_consumer(gp);
766 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
769 error = g_attach(cp, pp);
771 g_destroy_consumer(cp);
775 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
776 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
779 g_destroy_consumer(cp);
783 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
784 struct g_stripe_metadata md;
786 /* Reread metadata. */
787 error = g_stripe_read_metadata(cp, &md);
791 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
792 strcmp(md.md_name, sc->sc_name) != 0 ||
793 md.md_id != sc->sc_id) {
794 G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
799 sc->sc_disks[no] = cp;
800 G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
801 g_stripe_check_and_run(sc);
805 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
806 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
808 g_destroy_consumer(cp);
812 static struct g_geom *
813 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
816 struct g_stripe_softc *sc;
821 G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
824 /* Two disks is minimum. */
825 if (md->md_all < 2) {
826 G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
830 /* Stripe size have to be grater than or equal to sector size. */
831 if (md->md_stripesize < sectorsize) {
832 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
836 /* Stripe size have to be power of 2. */
837 if (!powerof2(md->md_stripesize)) {
838 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
842 /* Check for duplicate unit */
843 LIST_FOREACH(gp, &mp->geom, geom) {
845 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
846 G_STRIPE_DEBUG(0, "Device %s already configured.",
851 gp = g_new_geomf(mp, "%s", md->md_name);
852 sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
853 gp->start = g_stripe_start;
854 gp->spoiled = g_stripe_orphan;
855 gp->orphan = g_stripe_orphan;
856 gp->access = g_stripe_access;
857 gp->dumpconf = g_stripe_dumpconf;
859 sc->sc_id = md->md_id;
860 sc->sc_stripesize = md->md_stripesize;
861 sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
862 sc->sc_ndisks = md->md_all;
863 sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
864 M_STRIPE, M_WAITOK | M_ZERO);
865 for (no = 0; no < sc->sc_ndisks; no++)
866 sc->sc_disks[no] = NULL;
868 mtx_init(&sc->sc_lock, "gstripe lock", NULL, MTX_DEF);
872 sc->sc_provider = NULL;
874 G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
880 g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
882 struct g_provider *pp;
883 struct g_consumer *cp, *cp1;
891 pp = sc->sc_provider;
892 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
894 G_STRIPE_DEBUG(0, "Device %s is still open, so it "
895 "can't be definitely removed.", pp->name);
898 "Device %s is still open (r%dw%de%d).", pp->name,
899 pp->acr, pp->acw, pp->ace);
905 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
906 g_stripe_remove_disk(cp);
908 return (0); /* Recursion happened. */
910 if (!LIST_EMPTY(&gp->consumer))
911 return (EINPROGRESS);
914 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
916 free(sc->sc_disks, M_STRIPE);
917 mtx_destroy(&sc->sc_lock);
919 G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
920 g_wither_geom(gp, ENXIO);
925 g_stripe_destroy_geom(struct gctl_req *req __unused,
926 struct g_class *mp __unused, struct g_geom *gp)
928 struct g_stripe_softc *sc;
931 return (g_stripe_destroy(sc, 0));
934 static struct g_geom *
935 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
937 struct g_stripe_metadata md;
938 struct g_stripe_softc *sc;
939 struct g_consumer *cp;
943 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
946 /* Skip providers that are already open for writing. */
950 G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
952 gp = g_new_geomf(mp, "stripe:taste");
953 gp->start = g_stripe_start;
954 gp->access = g_stripe_access;
955 gp->orphan = g_stripe_orphan;
956 cp = g_new_consumer(gp);
957 error = g_attach(cp, pp);
959 error = g_stripe_read_metadata(cp, &md);
962 g_destroy_consumer(cp);
968 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
970 if (md.md_version > G_STRIPE_VERSION) {
971 printf("geom_stripe.ko module is too old to handle %s.\n",
976 * Backward compatibility:
978 /* There was no md_provider field in earlier versions of metadata. */
979 if (md.md_version < 2)
980 bzero(md.md_provider, sizeof(md.md_provider));
981 /* There was no md_provsize field in earlier versions of metadata. */
982 if (md.md_version < 3)
983 md.md_provsize = pp->mediasize;
985 if (md.md_provider[0] != '\0' &&
986 !g_compare_names(md.md_provider, pp->name))
988 if (md.md_provsize != pp->mediasize)
992 * Let's check if device already exists.
995 LIST_FOREACH(gp, &mp->geom, geom) {
999 if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
1001 if (strcmp(md.md_name, sc->sc_name) != 0)
1003 if (md.md_id != sc->sc_id)
1008 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1009 error = g_stripe_add_disk(sc, pp, md.md_no);
1012 "Cannot add disk %s to %s (error=%d).", pp->name,
1017 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
1019 G_STRIPE_DEBUG(0, "Cannot create device %s.",
1024 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1025 error = g_stripe_add_disk(sc, pp, md.md_no);
1028 "Cannot add disk %s to %s (error=%d).", pp->name,
1030 g_stripe_destroy(sc, 1);
1039 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1042 struct g_stripe_metadata md;
1043 struct g_provider *pp;
1044 struct g_stripe_softc *sc;
1052 g_topology_assert();
1053 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1054 if (nargs == NULL) {
1055 gctl_error(req, "No '%s' argument.", "nargs");
1059 gctl_error(req, "Too few arguments.");
1063 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1064 md.md_version = G_STRIPE_VERSION;
1065 name = gctl_get_asciiparam(req, "arg0");
1067 gctl_error(req, "No 'arg%u' argument.", 0);
1070 strlcpy(md.md_name, name, sizeof(md.md_name));
1071 md.md_id = arc4random();
1073 md.md_all = *nargs - 1;
1074 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1075 if (stripesize == NULL) {
1076 gctl_error(req, "No '%s' argument.", "stripesize");
1079 md.md_stripesize = (uint32_t)*stripesize;
1080 bzero(md.md_provider, sizeof(md.md_provider));
1081 /* This field is not important here. */
1084 /* Check all providers are valid */
1085 for (no = 1; no < *nargs; no++) {
1086 snprintf(param, sizeof(param), "arg%u", no);
1087 pp = gctl_get_provider(req, param);
1092 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1094 gctl_error(req, "Can't configure %s.", md.md_name);
1099 sb = sbuf_new_auto();
1100 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1101 for (attached = 0, no = 1; no < *nargs; no++) {
1102 snprintf(param, sizeof(param), "arg%u", no);
1103 pp = gctl_get_provider(req, param);
1105 name = gctl_get_asciiparam(req, param);
1106 MPASS(name != NULL);
1107 sbuf_printf(sb, " %s", name);
1110 if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1111 G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1112 no, pp->name, gp->name);
1113 sbuf_printf(sb, " %s", pp->name);
1119 if (md.md_all != attached) {
1120 g_stripe_destroy(gp->softc, 1);
1121 gctl_error(req, "%s", sbuf_data(sb));
1126 static struct g_stripe_softc *
1127 g_stripe_find_device(struct g_class *mp, const char *name)
1129 struct g_stripe_softc *sc;
1132 LIST_FOREACH(gp, &mp->geom, geom) {
1136 if (strcmp(sc->sc_name, name) == 0)
1143 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1145 struct g_stripe_softc *sc;
1146 int *force, *nargs, error;
1151 g_topology_assert();
1153 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1154 if (nargs == NULL) {
1155 gctl_error(req, "No '%s' argument.", "nargs");
1159 gctl_error(req, "Missing device(s).");
1162 force = gctl_get_paraml(req, "force", sizeof(*force));
1163 if (force == NULL) {
1164 gctl_error(req, "No '%s' argument.", "force");
1168 for (i = 0; i < (u_int)*nargs; i++) {
1169 snprintf(param, sizeof(param), "arg%u", i);
1170 name = gctl_get_asciiparam(req, param);
1172 gctl_error(req, "No 'arg%u' argument.", i);
1175 sc = g_stripe_find_device(mp, name);
1177 gctl_error(req, "No such device: %s.", name);
1180 error = g_stripe_destroy(sc, *force);
1182 gctl_error(req, "Cannot destroy device %s (error=%d).",
1183 sc->sc_name, error);
1190 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1194 g_topology_assert();
1196 version = gctl_get_paraml(req, "version", sizeof(*version));
1197 if (version == NULL) {
1198 gctl_error(req, "No '%s' argument.", "version");
1201 if (*version != G_STRIPE_VERSION) {
1202 gctl_error(req, "Userland and kernel parts are out of sync.");
1206 if (strcmp(verb, "create") == 0) {
1207 g_stripe_ctl_create(req, mp);
1209 } else if (strcmp(verb, "destroy") == 0 ||
1210 strcmp(verb, "stop") == 0) {
1211 g_stripe_ctl_destroy(req, mp);
1215 gctl_error(req, "Unknown verb.");
1219 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1220 struct g_consumer *cp, struct g_provider *pp)
1222 struct g_stripe_softc *sc;
1229 } else if (cp != NULL) {
1230 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1233 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1234 sbuf_printf(sb, "%s<Stripesize>%ju</Stripesize>\n", indent,
1235 (uintmax_t)sc->sc_stripesize);
1236 sbuf_printf(sb, "%s<Type>", indent);
1237 switch (sc->sc_type) {
1238 case G_STRIPE_TYPE_AUTOMATIC:
1239 sbuf_cat(sb, "AUTOMATIC");
1241 case G_STRIPE_TYPE_MANUAL:
1242 sbuf_cat(sb, "MANUAL");
1245 sbuf_cat(sb, "UNKNOWN");
1248 sbuf_cat(sb, "</Type>\n");
1249 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1250 indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1251 sbuf_printf(sb, "%s<State>", indent);
1252 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1255 sbuf_cat(sb, "DOWN");
1256 sbuf_cat(sb, "</State>\n");
1260 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);
1261 MODULE_VERSION(geom_stripe, 0);