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_int g_stripe_maxmem = MAXPHYS * 100;
96 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, maxmem, CTLFLAG_RDTUN, &g_stripe_maxmem,
97 0, "Maximum memory that can be allocated in \"fast\" mode (in bytes)");
98 static u_int g_stripe_fast_failed = 0;
99 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, fast_failed, CTLFLAG_RD,
100 &g_stripe_fast_failed, 0, "How many times \"fast\" mode failed");
103 * Greatest Common Divisor.
106 gcd(u_int a, u_int b)
119 * Least Common Multiple.
122 lcm(u_int a, u_int b)
125 return ((a * b) / gcd(a, b));
129 g_stripe_init(struct g_class *mp __unused)
132 g_stripe_zone = uma_zcreate("g_stripe_zone", MAXPHYS, NULL, NULL,
134 g_stripe_maxmem -= g_stripe_maxmem % MAXPHYS;
135 uma_zone_set_max(g_stripe_zone, g_stripe_maxmem / MAXPHYS);
139 g_stripe_fini(struct g_class *mp __unused)
142 uma_zdestroy(g_stripe_zone);
146 * Return the number of valid disks.
149 g_stripe_nvalid(struct g_stripe_softc *sc)
154 for (i = 0; i < sc->sc_ndisks; i++) {
155 if (sc->sc_disks[i] != NULL)
163 g_stripe_remove_disk(struct g_consumer *cp)
165 struct g_stripe_softc *sc;
168 KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__));
169 sc = (struct g_stripe_softc *)cp->geom->softc;
170 KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
172 if (cp->private == NULL) {
173 G_STRIPE_DEBUG(0, "Disk %s removed from %s.",
174 cp->provider->name, sc->sc_name);
175 cp->private = (void *)(uintptr_t)-1;
178 if (sc->sc_provider != NULL) {
179 G_STRIPE_DEBUG(0, "Device %s deactivated.",
180 sc->sc_provider->name);
181 g_wither_provider(sc->sc_provider, ENXIO);
182 sc->sc_provider = NULL;
185 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
187 sc->sc_disks[cp->index] = NULL;
190 g_destroy_consumer(cp);
191 /* If there are no valid disks anymore, remove device. */
192 if (LIST_EMPTY(&sc->sc_geom->consumer))
193 g_stripe_destroy(sc, 1);
197 g_stripe_orphan(struct g_consumer *cp)
199 struct g_stripe_softc *sc;
208 g_stripe_remove_disk(cp);
212 g_stripe_access(struct g_provider *pp, int dr, int dw, int de)
214 struct g_consumer *cp1, *cp2, *tmp;
215 struct g_stripe_softc *sc;
222 KASSERT(sc != NULL, ("NULL sc in %s.", __func__));
224 /* On first open, grab an extra "exclusive" bit */
225 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
227 /* ... and let go of it on last close */
228 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
231 LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) {
232 error = g_access(cp1, dr, dw, de);
235 if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 &&
236 cp1->private != NULL) {
237 g_stripe_remove_disk(cp1); /* May destroy geom. */
243 LIST_FOREACH(cp2, &gp->consumer, consumer) {
246 g_access(cp2, -dr, -dw, -de);
252 g_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset,
253 off_t length, int mode)
258 stripesize = sc->sc_stripesize;
259 len = (size_t)(stripesize - (offset & (stripesize - 1)));
261 bcopy(src, dst, len);
263 dst += len + stripesize * (sc->sc_ndisks - 1);
267 src += len + stripesize * (sc->sc_ndisks - 1);
271 ("Length < 0 (stripesize=%ju, offset=%ju, length=%jd).",
272 (uintmax_t)stripesize, (uintmax_t)offset, (intmax_t)length));
273 if (length > stripesize)
277 } while (length > 0);
281 g_stripe_done(struct bio *bp)
283 struct g_stripe_softc *sc;
286 pbp = bp->bio_parent;
287 sc = pbp->bio_to->geom->softc;
288 if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) {
289 g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset,
291 bp->bio_data = bp->bio_caller1;
292 bp->bio_caller1 = NULL;
294 mtx_lock(&sc->sc_lock);
295 if (pbp->bio_error == 0)
296 pbp->bio_error = bp->bio_error;
297 pbp->bio_completed += bp->bio_completed;
299 if (pbp->bio_children == pbp->bio_inbed) {
300 mtx_unlock(&sc->sc_lock);
301 if (pbp->bio_driver1 != NULL)
302 uma_zfree(g_stripe_zone, pbp->bio_driver1);
303 if (bp->bio_cmd == BIO_SPEEDUP)
304 pbp->bio_completed = pbp->bio_length;
305 g_io_deliver(pbp, pbp->bio_error);
307 mtx_unlock(&sc->sc_lock);
312 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
314 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
315 struct g_stripe_softc *sc;
316 char *addr, *data = NULL;
322 sc = bp->bio_to->geom->softc;
325 stripesize = sc->sc_stripesize;
327 cbp = g_clone_bio(bp);
332 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
335 * Fill in the component buf structure.
337 cbp->bio_done = g_stripe_done;
338 cbp->bio_offset = offset;
339 cbp->bio_data = addr;
340 cbp->bio_caller1 = NULL;
341 cbp->bio_length = length;
342 cbp->bio_caller2 = sc->sc_disks[no];
344 /* offset -= offset % stripesize; */
345 offset -= offset & (stripesize - 1);
347 length = bp->bio_length - length;
348 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
349 if (no > sc->sc_ndisks - 1) {
351 offset += stripesize;
353 if (nparts >= sc->sc_ndisks) {
354 cbp = TAILQ_NEXT(cbp, bio_queue);
356 cbp = TAILQ_FIRST(&queue);
359 * Update bio structure.
362 * MIN() is in case when
363 * (bp->bio_length % sc->sc_stripesize) != 0.
365 cbp->bio_length += MIN(stripesize, length);
366 if (cbp->bio_caller1 == NULL) {
367 cbp->bio_caller1 = cbp->bio_data;
368 cbp->bio_data = NULL;
370 data = uma_zalloc(g_stripe_zone,
379 cbp = g_clone_bio(bp);
384 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
387 * Fill in the component buf structure.
389 cbp->bio_done = g_stripe_done;
390 cbp->bio_offset = offset;
391 cbp->bio_data = addr;
392 cbp->bio_caller1 = NULL;
394 * MIN() is in case when
395 * (bp->bio_length % sc->sc_stripesize) != 0.
397 cbp->bio_length = MIN(stripesize, length);
398 cbp->bio_caller2 = sc->sc_disks[no];
402 bp->bio_driver1 = data;
404 * Fire off all allocated requests!
406 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
407 struct g_consumer *cp;
409 TAILQ_REMOVE(&queue, cbp, bio_queue);
410 cp = cbp->bio_caller2;
411 cbp->bio_caller2 = NULL;
412 cbp->bio_to = cp->provider;
413 if (cbp->bio_caller1 != NULL) {
414 cbp->bio_data = data;
415 if (bp->bio_cmd == BIO_WRITE) {
416 g_stripe_copy(sc, cbp->bio_caller1, data,
417 cbp->bio_offset, cbp->bio_length, 0);
419 data += cbp->bio_length;
421 G_STRIPE_LOGREQ(cbp, "Sending request.");
422 g_io_request(cbp, cp);
427 uma_zfree(g_stripe_zone, data);
428 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
429 TAILQ_REMOVE(&queue, cbp, bio_queue);
430 if (cbp->bio_caller1 != NULL) {
431 cbp->bio_data = cbp->bio_caller1;
432 cbp->bio_caller1 = NULL;
441 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
443 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
444 struct g_stripe_softc *sc;
450 sc = bp->bio_to->geom->softc;
452 stripesize = sc->sc_stripesize;
454 cbp = g_clone_bio(bp);
459 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
461 * Fill in the component buf structure.
463 if (bp->bio_length == length)
464 cbp->bio_done = g_std_done; /* Optimized lockless case. */
466 cbp->bio_done = g_stripe_done;
467 cbp->bio_offset = offset;
468 cbp->bio_length = length;
469 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
470 bp->bio_ma_n = round_page(bp->bio_ma_offset +
471 bp->bio_length) / PAGE_SIZE;
475 cbp->bio_caller2 = sc->sc_disks[no];
477 /* offset -= offset % stripesize; */
478 offset -= offset & (stripesize - 1);
479 if (bp->bio_cmd != BIO_DELETE)
481 length = bp->bio_length - length;
482 for (no++; length > 0; no++, length -= stripesize) {
483 if (no > sc->sc_ndisks - 1) {
485 offset += stripesize;
487 cbp = g_clone_bio(bp);
492 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
495 * Fill in the component buf structure.
497 cbp->bio_done = g_stripe_done;
498 cbp->bio_offset = offset;
500 * MIN() is in case when
501 * (bp->bio_length % sc->sc_stripesize) != 0.
503 cbp->bio_length = MIN(stripesize, length);
504 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
505 cbp->bio_ma_offset += (uintptr_t)addr;
506 cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
507 cbp->bio_ma_offset %= PAGE_SIZE;
508 cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
509 cbp->bio_length) / PAGE_SIZE;
511 cbp->bio_data = addr;
513 cbp->bio_caller2 = sc->sc_disks[no];
515 if (bp->bio_cmd != BIO_DELETE)
519 * Fire off all allocated requests!
521 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
522 struct g_consumer *cp;
524 TAILQ_REMOVE(&queue, cbp, bio_queue);
525 cp = cbp->bio_caller2;
526 cbp->bio_caller2 = NULL;
527 cbp->bio_to = cp->provider;
528 G_STRIPE_LOGREQ(cbp, "Sending request.");
529 g_io_request(cbp, cp);
533 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
534 TAILQ_REMOVE(&queue, cbp, bio_queue);
542 g_stripe_pushdown(struct g_stripe_softc *sc, struct bio *bp)
544 struct bio_queue_head queue;
545 struct g_consumer *cp;
550 for (no = 0; no < sc->sc_ndisks; no++) {
551 cbp = g_clone_bio(bp);
553 for (cbp = bioq_first(&queue); cbp != NULL;
554 cbp = bioq_first(&queue)) {
555 bioq_remove(&queue, cbp);
558 if (bp->bio_error == 0)
559 bp->bio_error = ENOMEM;
560 g_io_deliver(bp, bp->bio_error);
563 bioq_insert_tail(&queue, cbp);
564 cbp->bio_done = g_stripe_done;
565 cbp->bio_caller2 = sc->sc_disks[no];
566 cbp->bio_to = sc->sc_disks[no]->provider;
568 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
569 bioq_remove(&queue, cbp);
570 G_STRIPE_LOGREQ(cbp, "Sending request.");
571 cp = cbp->bio_caller2;
572 cbp->bio_caller2 = NULL;
573 g_io_request(cbp, cp);
578 g_stripe_start(struct bio *bp)
580 off_t offset, start, length, nstripe, stripesize;
581 struct g_stripe_softc *sc;
585 sc = bp->bio_to->geom->softc;
587 * If sc == NULL, provider's error should be set and g_stripe_start()
588 * should not be called at all.
591 ("Provider's error should be set (error=%d)(device=%s).",
592 bp->bio_to->error, bp->bio_to->name));
594 G_STRIPE_LOGREQ(bp, "Request received.");
596 switch (bp->bio_cmd) {
603 g_stripe_pushdown(sc, bp);
606 /* To which provider it should be delivered? */
608 g_io_deliver(bp, EOPNOTSUPP);
612 stripesize = sc->sc_stripesize;
615 * Calculations are quite messy, but fast I hope.
619 /* nstripe = bp->bio_offset / stripesize; */
620 nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
622 no = nstripe % sc->sc_ndisks;
623 /* Start position in stripe. */
624 /* start = bp->bio_offset % stripesize; */
625 start = bp->bio_offset & (stripesize - 1);
626 /* Start position in disk. */
627 /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
628 offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
629 /* Length of data to operate. */
630 length = MIN(bp->bio_length, stripesize - start);
633 * Do use "fast" mode when:
634 * 1. "Fast" mode is ON.
636 * 2. Request size is less than or equal to MAXPHYS,
637 * which should always be true.
639 * 3. Request size is bigger than stripesize * ndisks. If it isn't,
640 * there will be no need to send more than one I/O request to
641 * a provider, so there is nothing to optmize.
643 * 4. Request is not unmapped.
645 * 5. It is not a BIO_DELETE.
647 if (g_stripe_fast && bp->bio_length <= MAXPHYS &&
648 bp->bio_length >= stripesize * sc->sc_ndisks &&
649 (bp->bio_flags & BIO_UNMAPPED) == 0 &&
650 bp->bio_cmd != BIO_DELETE) {
655 error = g_stripe_start_fast(bp, no, offset, length);
657 g_stripe_fast_failed++;
660 * Do use "economic" when:
661 * 1. "Economic" mode is ON.
663 * 2. "Fast" mode failed. It can only fail if there is no memory.
665 if (!fast || error != 0)
666 error = g_stripe_start_economic(bp, no, offset, length);
668 if (bp->bio_error == 0)
669 bp->bio_error = error;
670 g_io_deliver(bp, bp->bio_error);
675 g_stripe_check_and_run(struct g_stripe_softc *sc)
677 struct g_provider *dp;
679 u_int no, sectorsize = 0;
682 if (g_stripe_nvalid(sc) != sc->sc_ndisks)
685 sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
687 sc->sc_provider->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
688 if (g_stripe_fast == 0)
689 sc->sc_provider->flags |= G_PF_ACCEPT_UNMAPPED;
691 * Find the smallest disk.
693 mediasize = sc->sc_disks[0]->provider->mediasize;
694 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
695 mediasize -= sc->sc_disks[0]->provider->sectorsize;
696 mediasize -= mediasize % sc->sc_stripesize;
697 sectorsize = sc->sc_disks[0]->provider->sectorsize;
698 for (no = 1; no < sc->sc_ndisks; no++) {
699 dp = sc->sc_disks[no]->provider;
701 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
702 ms -= dp->sectorsize;
703 ms -= ms % sc->sc_stripesize;
706 sectorsize = lcm(sectorsize, dp->sectorsize);
708 /* A provider underneath us doesn't support unmapped */
709 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
710 G_STRIPE_DEBUG(1, "Cancelling unmapped "
711 "because of %s.", dp->name);
712 sc->sc_provider->flags &= ~G_PF_ACCEPT_UNMAPPED;
715 sc->sc_provider->sectorsize = sectorsize;
716 sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
717 sc->sc_provider->stripesize = sc->sc_stripesize;
718 sc->sc_provider->stripeoffset = 0;
719 g_error_provider(sc->sc_provider, 0);
721 G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
725 g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
727 struct g_provider *pp;
733 error = g_access(cp, 1, 0, 0);
738 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
741 g_access(cp, -1, 0, 0);
745 /* Decode metadata. */
746 stripe_metadata_decode(buf, md);
753 * Add disk to given device.
756 g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
758 struct g_consumer *cp, *fcp;
763 /* Metadata corrupted? */
764 if (no >= sc->sc_ndisks)
767 /* Check if disk is not already attached. */
768 if (sc->sc_disks[no] != NULL)
772 fcp = LIST_FIRST(&gp->consumer);
774 cp = g_new_consumer(gp);
775 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
778 error = g_attach(cp, pp);
780 g_destroy_consumer(cp);
784 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
785 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
788 g_destroy_consumer(cp);
792 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
793 struct g_stripe_metadata md;
795 /* Reread metadata. */
796 error = g_stripe_read_metadata(cp, &md);
800 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
801 strcmp(md.md_name, sc->sc_name) != 0 ||
802 md.md_id != sc->sc_id) {
803 G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
808 sc->sc_disks[no] = cp;
809 G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
810 g_stripe_check_and_run(sc);
814 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
815 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
817 g_destroy_consumer(cp);
821 static struct g_geom *
822 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
825 struct g_stripe_softc *sc;
830 G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
833 /* Two disks is minimum. */
834 if (md->md_all < 2) {
835 G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
839 /* Stripe size have to be grater than or equal to sector size. */
840 if (md->md_stripesize < sectorsize) {
841 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
845 /* Stripe size have to be power of 2. */
846 if (!powerof2(md->md_stripesize)) {
847 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
851 /* Check for duplicate unit */
852 LIST_FOREACH(gp, &mp->geom, geom) {
854 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
855 G_STRIPE_DEBUG(0, "Device %s already configured.",
860 gp = g_new_geomf(mp, "%s", md->md_name);
861 sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
862 gp->start = g_stripe_start;
863 gp->spoiled = g_stripe_orphan;
864 gp->orphan = g_stripe_orphan;
865 gp->access = g_stripe_access;
866 gp->dumpconf = g_stripe_dumpconf;
868 sc->sc_id = md->md_id;
869 sc->sc_stripesize = md->md_stripesize;
870 sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
871 sc->sc_ndisks = md->md_all;
872 sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
873 M_STRIPE, M_WAITOK | M_ZERO);
874 for (no = 0; no < sc->sc_ndisks; no++)
875 sc->sc_disks[no] = NULL;
877 mtx_init(&sc->sc_lock, "gstripe lock", NULL, MTX_DEF);
881 sc->sc_provider = NULL;
883 G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
889 g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
891 struct g_provider *pp;
892 struct g_consumer *cp, *cp1;
900 pp = sc->sc_provider;
901 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
903 G_STRIPE_DEBUG(0, "Device %s is still open, so it "
904 "can't be definitely removed.", pp->name);
907 "Device %s is still open (r%dw%de%d).", pp->name,
908 pp->acr, pp->acw, pp->ace);
914 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
915 g_stripe_remove_disk(cp);
917 return (0); /* Recursion happened. */
919 if (!LIST_EMPTY(&gp->consumer))
920 return (EINPROGRESS);
923 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
925 free(sc->sc_disks, M_STRIPE);
926 mtx_destroy(&sc->sc_lock);
928 G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
929 g_wither_geom(gp, ENXIO);
934 g_stripe_destroy_geom(struct gctl_req *req __unused,
935 struct g_class *mp __unused, struct g_geom *gp)
937 struct g_stripe_softc *sc;
940 return (g_stripe_destroy(sc, 0));
943 static struct g_geom *
944 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
946 struct g_stripe_metadata md;
947 struct g_stripe_softc *sc;
948 struct g_consumer *cp;
952 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
955 /* Skip providers that are already open for writing. */
959 G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
961 gp = g_new_geomf(mp, "stripe:taste");
962 gp->start = g_stripe_start;
963 gp->access = g_stripe_access;
964 gp->orphan = g_stripe_orphan;
965 cp = g_new_consumer(gp);
967 error = g_stripe_read_metadata(cp, &md);
969 g_destroy_consumer(cp);
975 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
977 if (md.md_version > G_STRIPE_VERSION) {
978 printf("geom_stripe.ko module is too old to handle %s.\n",
983 * Backward compatibility:
985 /* There was no md_provider field in earlier versions of metadata. */
986 if (md.md_version < 2)
987 bzero(md.md_provider, sizeof(md.md_provider));
988 /* There was no md_provsize field in earlier versions of metadata. */
989 if (md.md_version < 3)
990 md.md_provsize = pp->mediasize;
992 if (md.md_provider[0] != '\0' &&
993 !g_compare_names(md.md_provider, pp->name))
995 if (md.md_provsize != pp->mediasize)
999 * Let's check if device already exists.
1002 LIST_FOREACH(gp, &mp->geom, geom) {
1006 if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
1008 if (strcmp(md.md_name, sc->sc_name) != 0)
1010 if (md.md_id != sc->sc_id)
1015 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1016 error = g_stripe_add_disk(sc, pp, md.md_no);
1019 "Cannot add disk %s to %s (error=%d).", pp->name,
1024 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
1026 G_STRIPE_DEBUG(0, "Cannot create device %s.",
1031 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1032 error = g_stripe_add_disk(sc, pp, md.md_no);
1035 "Cannot add disk %s to %s (error=%d).", pp->name,
1037 g_stripe_destroy(sc, 1);
1046 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1049 struct g_stripe_metadata md;
1050 struct g_provider *pp;
1051 struct g_stripe_softc *sc;
1059 g_topology_assert();
1060 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1061 if (nargs == NULL) {
1062 gctl_error(req, "No '%s' argument.", "nargs");
1066 gctl_error(req, "Too few arguments.");
1070 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1071 md.md_version = G_STRIPE_VERSION;
1072 name = gctl_get_asciiparam(req, "arg0");
1074 gctl_error(req, "No 'arg%u' argument.", 0);
1077 strlcpy(md.md_name, name, sizeof(md.md_name));
1078 md.md_id = arc4random();
1080 md.md_all = *nargs - 1;
1081 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1082 if (stripesize == NULL) {
1083 gctl_error(req, "No '%s' argument.", "stripesize");
1086 md.md_stripesize = (uint32_t)*stripesize;
1087 bzero(md.md_provider, sizeof(md.md_provider));
1088 /* This field is not important here. */
1091 /* Check all providers are valid */
1092 for (no = 1; no < *nargs; no++) {
1093 snprintf(param, sizeof(param), "arg%u", no);
1094 pp = gctl_get_provider(req, param);
1099 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1101 gctl_error(req, "Can't configure %s.", md.md_name);
1106 sb = sbuf_new_auto();
1107 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1108 for (attached = 0, no = 1; no < *nargs; no++) {
1109 snprintf(param, sizeof(param), "arg%u", no);
1110 pp = gctl_get_provider(req, param);
1112 name = gctl_get_asciiparam(req, param);
1113 MPASS(name != NULL);
1114 sbuf_printf(sb, " %s", name);
1117 if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1118 G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1119 no, pp->name, gp->name);
1120 sbuf_printf(sb, " %s", pp->name);
1126 if (md.md_all != attached) {
1127 g_stripe_destroy(gp->softc, 1);
1128 gctl_error(req, "%s", sbuf_data(sb));
1133 static struct g_stripe_softc *
1134 g_stripe_find_device(struct g_class *mp, const char *name)
1136 struct g_stripe_softc *sc;
1139 LIST_FOREACH(gp, &mp->geom, geom) {
1143 if (strcmp(sc->sc_name, name) == 0)
1150 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1152 struct g_stripe_softc *sc;
1153 int *force, *nargs, error;
1158 g_topology_assert();
1160 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1161 if (nargs == NULL) {
1162 gctl_error(req, "No '%s' argument.", "nargs");
1166 gctl_error(req, "Missing device(s).");
1169 force = gctl_get_paraml(req, "force", sizeof(*force));
1170 if (force == NULL) {
1171 gctl_error(req, "No '%s' argument.", "force");
1175 for (i = 0; i < (u_int)*nargs; i++) {
1176 snprintf(param, sizeof(param), "arg%u", i);
1177 name = gctl_get_asciiparam(req, param);
1179 gctl_error(req, "No 'arg%u' argument.", i);
1182 sc = g_stripe_find_device(mp, name);
1184 gctl_error(req, "No such device: %s.", name);
1187 error = g_stripe_destroy(sc, *force);
1189 gctl_error(req, "Cannot destroy device %s (error=%d).",
1190 sc->sc_name, error);
1197 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1201 g_topology_assert();
1203 version = gctl_get_paraml(req, "version", sizeof(*version));
1204 if (version == NULL) {
1205 gctl_error(req, "No '%s' argument.", "version");
1208 if (*version != G_STRIPE_VERSION) {
1209 gctl_error(req, "Userland and kernel parts are out of sync.");
1213 if (strcmp(verb, "create") == 0) {
1214 g_stripe_ctl_create(req, mp);
1216 } else if (strcmp(verb, "destroy") == 0 ||
1217 strcmp(verb, "stop") == 0) {
1218 g_stripe_ctl_destroy(req, mp);
1222 gctl_error(req, "Unknown verb.");
1226 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1227 struct g_consumer *cp, struct g_provider *pp)
1229 struct g_stripe_softc *sc;
1236 } else if (cp != NULL) {
1237 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1240 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1241 sbuf_printf(sb, "%s<Stripesize>%ju</Stripesize>\n", indent,
1242 (uintmax_t)sc->sc_stripesize);
1243 sbuf_printf(sb, "%s<Type>", indent);
1244 switch (sc->sc_type) {
1245 case G_STRIPE_TYPE_AUTOMATIC:
1246 sbuf_cat(sb, "AUTOMATIC");
1248 case G_STRIPE_TYPE_MANUAL:
1249 sbuf_cat(sb, "MANUAL");
1252 sbuf_cat(sb, "UNKNOWN");
1255 sbuf_cat(sb, "</Type>\n");
1256 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1257 indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1258 sbuf_printf(sb, "%s<State>", indent);
1259 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1262 sbuf_cat(sb, "DOWN");
1263 sbuf_cat(sb, "</State>\n");
1267 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);
1268 MODULE_VERSION(geom_stripe, 0);