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 if (bp->bio_cmd == BIO_SPEEDUP)
302 pbp->bio_completed = pbp->bio_length;
303 g_io_deliver(pbp, pbp->bio_error);
305 mtx_unlock(&sc->sc_lock);
310 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length)
312 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
313 struct g_stripe_softc *sc;
314 char *addr, *data = NULL;
320 sc = bp->bio_to->geom->softc;
323 stripesize = sc->sc_stripesize;
325 cbp = g_clone_bio(bp);
330 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
333 * Fill in the component buf structure.
335 cbp->bio_done = g_stripe_done;
336 cbp->bio_offset = offset;
337 cbp->bio_data = addr;
338 cbp->bio_caller1 = NULL;
339 cbp->bio_length = length;
340 cbp->bio_caller2 = sc->sc_disks[no];
342 /* offset -= offset % stripesize; */
343 offset -= offset & (stripesize - 1);
345 length = bp->bio_length - length;
346 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) {
347 if (no > sc->sc_ndisks - 1) {
349 offset += stripesize;
351 if (nparts >= sc->sc_ndisks) {
352 cbp = TAILQ_NEXT(cbp, bio_queue);
354 cbp = TAILQ_FIRST(&queue);
357 * Update bio structure.
360 * MIN() is in case when
361 * (bp->bio_length % sc->sc_stripesize) != 0.
363 cbp->bio_length += MIN(stripesize, length);
364 if (cbp->bio_caller1 == NULL) {
365 cbp->bio_caller1 = cbp->bio_data;
366 cbp->bio_data = NULL;
368 data = uma_zalloc(g_stripe_zone,
377 cbp = g_clone_bio(bp);
382 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
385 * Fill in the component buf structure.
387 cbp->bio_done = g_stripe_done;
388 cbp->bio_offset = offset;
389 cbp->bio_data = addr;
390 cbp->bio_caller1 = NULL;
392 * MIN() is in case when
393 * (bp->bio_length % sc->sc_stripesize) != 0.
395 cbp->bio_length = MIN(stripesize, length);
396 cbp->bio_caller2 = sc->sc_disks[no];
400 bp->bio_driver1 = data;
402 * Fire off all allocated requests!
404 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
405 struct g_consumer *cp;
407 TAILQ_REMOVE(&queue, cbp, bio_queue);
408 cp = cbp->bio_caller2;
409 cbp->bio_caller2 = NULL;
410 cbp->bio_to = cp->provider;
411 if (cbp->bio_caller1 != NULL) {
412 cbp->bio_data = data;
413 if (bp->bio_cmd == BIO_WRITE) {
414 g_stripe_copy(sc, cbp->bio_caller1, data,
415 cbp->bio_offset, cbp->bio_length, 0);
417 data += cbp->bio_length;
419 G_STRIPE_LOGREQ(cbp, "Sending request.");
420 g_io_request(cbp, cp);
425 uma_zfree(g_stripe_zone, data);
426 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
427 TAILQ_REMOVE(&queue, cbp, bio_queue);
428 if (cbp->bio_caller1 != NULL) {
429 cbp->bio_data = cbp->bio_caller1;
430 cbp->bio_caller1 = NULL;
439 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length)
441 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
442 struct g_stripe_softc *sc;
448 sc = bp->bio_to->geom->softc;
450 stripesize = sc->sc_stripesize;
452 cbp = g_clone_bio(bp);
457 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
459 * Fill in the component buf structure.
461 if (bp->bio_length == length)
462 cbp->bio_done = g_std_done; /* Optimized lockless case. */
464 cbp->bio_done = g_stripe_done;
465 cbp->bio_offset = offset;
466 cbp->bio_length = length;
467 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
468 bp->bio_ma_n = round_page(bp->bio_ma_offset +
469 bp->bio_length) / PAGE_SIZE;
473 cbp->bio_caller2 = sc->sc_disks[no];
475 /* offset -= offset % stripesize; */
476 offset -= offset & (stripesize - 1);
477 if (bp->bio_cmd != BIO_DELETE)
479 length = bp->bio_length - length;
480 for (no++; length > 0; no++, length -= stripesize) {
481 if (no > sc->sc_ndisks - 1) {
483 offset += stripesize;
485 cbp = g_clone_bio(bp);
490 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue);
493 * Fill in the component buf structure.
495 cbp->bio_done = g_stripe_done;
496 cbp->bio_offset = offset;
498 * MIN() is in case when
499 * (bp->bio_length % sc->sc_stripesize) != 0.
501 cbp->bio_length = MIN(stripesize, length);
502 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
503 cbp->bio_ma_offset += (uintptr_t)addr;
504 cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
505 cbp->bio_ma_offset %= PAGE_SIZE;
506 cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
507 cbp->bio_length) / PAGE_SIZE;
509 cbp->bio_data = addr;
511 cbp->bio_caller2 = sc->sc_disks[no];
513 if (bp->bio_cmd != BIO_DELETE)
517 * Fire off all allocated requests!
519 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
520 struct g_consumer *cp;
522 TAILQ_REMOVE(&queue, cbp, bio_queue);
523 cp = cbp->bio_caller2;
524 cbp->bio_caller2 = NULL;
525 cbp->bio_to = cp->provider;
526 G_STRIPE_LOGREQ(cbp, "Sending request.");
527 g_io_request(cbp, cp);
531 while ((cbp = TAILQ_FIRST(&queue)) != NULL) {
532 TAILQ_REMOVE(&queue, cbp, bio_queue);
540 g_stripe_pushdown(struct g_stripe_softc *sc, struct bio *bp)
542 struct bio_queue_head queue;
543 struct g_consumer *cp;
548 for (no = 0; no < sc->sc_ndisks; no++) {
549 cbp = g_clone_bio(bp);
551 for (cbp = bioq_first(&queue); cbp != NULL;
552 cbp = bioq_first(&queue)) {
553 bioq_remove(&queue, cbp);
556 if (bp->bio_error == 0)
557 bp->bio_error = ENOMEM;
558 g_io_deliver(bp, bp->bio_error);
561 bioq_insert_tail(&queue, cbp);
562 cbp->bio_done = g_stripe_done;
563 cbp->bio_caller2 = sc->sc_disks[no];
564 cbp->bio_to = sc->sc_disks[no]->provider;
566 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
567 bioq_remove(&queue, cbp);
568 G_STRIPE_LOGREQ(cbp, "Sending request.");
569 cp = cbp->bio_caller2;
570 cbp->bio_caller2 = NULL;
571 g_io_request(cbp, cp);
576 g_stripe_start(struct bio *bp)
578 off_t offset, start, length, nstripe, stripesize;
579 struct g_stripe_softc *sc;
583 sc = bp->bio_to->geom->softc;
585 * If sc == NULL, provider's error should be set and g_stripe_start()
586 * should not be called at all.
589 ("Provider's error should be set (error=%d)(device=%s).",
590 bp->bio_to->error, bp->bio_to->name));
592 G_STRIPE_LOGREQ(bp, "Request received.");
594 switch (bp->bio_cmd) {
601 g_stripe_pushdown(sc, bp);
604 /* To which provider it should be delivered? */
606 g_io_deliver(bp, EOPNOTSUPP);
610 stripesize = sc->sc_stripesize;
613 * Calculations are quite messy, but fast I hope.
617 /* nstripe = bp->bio_offset / stripesize; */
618 nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits;
620 no = nstripe % sc->sc_ndisks;
621 /* Start position in stripe. */
622 /* start = bp->bio_offset % stripesize; */
623 start = bp->bio_offset & (stripesize - 1);
624 /* Start position in disk. */
625 /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */
626 offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start;
627 /* Length of data to operate. */
628 length = MIN(bp->bio_length, stripesize - start);
631 * Do use "fast" mode when:
632 * 1. "Fast" mode is ON.
634 * 2. Request size is less than or equal to MAXPHYS,
635 * which should always be true.
637 * 3. Request size is bigger than stripesize * ndisks. If it isn't,
638 * there will be no need to send more than one I/O request to
639 * a provider, so there is nothing to optmize.
641 * 4. Request is not unmapped.
643 * 5. It is not a BIO_DELETE.
645 if (g_stripe_fast && bp->bio_length <= MAXPHYS &&
646 bp->bio_length >= stripesize * sc->sc_ndisks &&
647 (bp->bio_flags & BIO_UNMAPPED) == 0 &&
648 bp->bio_cmd != BIO_DELETE) {
653 error = g_stripe_start_fast(bp, no, offset, length);
655 g_stripe_fast_failed++;
658 * Do use "economic" when:
659 * 1. "Economic" mode is ON.
661 * 2. "Fast" mode failed. It can only fail if there is no memory.
663 if (!fast || error != 0)
664 error = g_stripe_start_economic(bp, no, offset, length);
666 if (bp->bio_error == 0)
667 bp->bio_error = error;
668 g_io_deliver(bp, bp->bio_error);
673 g_stripe_check_and_run(struct g_stripe_softc *sc)
675 struct g_provider *dp;
677 u_int no, sectorsize = 0;
680 if (g_stripe_nvalid(sc) != sc->sc_ndisks)
683 sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s",
685 sc->sc_provider->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
686 if (g_stripe_fast == 0)
687 sc->sc_provider->flags |= G_PF_ACCEPT_UNMAPPED;
689 * Find the smallest disk.
691 mediasize = sc->sc_disks[0]->provider->mediasize;
692 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
693 mediasize -= sc->sc_disks[0]->provider->sectorsize;
694 mediasize -= mediasize % sc->sc_stripesize;
695 sectorsize = sc->sc_disks[0]->provider->sectorsize;
696 for (no = 1; no < sc->sc_ndisks; no++) {
697 dp = sc->sc_disks[no]->provider;
699 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC)
700 ms -= dp->sectorsize;
701 ms -= ms % sc->sc_stripesize;
704 sectorsize = lcm(sectorsize, dp->sectorsize);
706 /* A provider underneath us doesn't support unmapped */
707 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
708 G_STRIPE_DEBUG(1, "Cancelling unmapped "
709 "because of %s.", dp->name);
710 sc->sc_provider->flags &= ~G_PF_ACCEPT_UNMAPPED;
713 sc->sc_provider->sectorsize = sectorsize;
714 sc->sc_provider->mediasize = mediasize * sc->sc_ndisks;
715 sc->sc_provider->stripesize = sc->sc_stripesize;
716 sc->sc_provider->stripeoffset = 0;
717 g_error_provider(sc->sc_provider, 0);
719 G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
723 g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md)
725 struct g_provider *pp;
731 error = g_access(cp, 1, 0, 0);
736 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
739 g_access(cp, -1, 0, 0);
743 /* Decode metadata. */
744 stripe_metadata_decode(buf, md);
751 * Add disk to given device.
754 g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no)
756 struct g_consumer *cp, *fcp;
761 /* Metadata corrupted? */
762 if (no >= sc->sc_ndisks)
765 /* Check if disk is not already attached. */
766 if (sc->sc_disks[no] != NULL)
770 fcp = LIST_FIRST(&gp->consumer);
772 cp = g_new_consumer(gp);
773 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
776 error = g_attach(cp, pp);
778 g_destroy_consumer(cp);
782 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
783 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
786 g_destroy_consumer(cp);
790 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) {
791 struct g_stripe_metadata md;
793 /* Reread metadata. */
794 error = g_stripe_read_metadata(cp, &md);
798 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 ||
799 strcmp(md.md_name, sc->sc_name) != 0 ||
800 md.md_id != sc->sc_id) {
801 G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name);
806 sc->sc_disks[no] = cp;
807 G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
808 g_stripe_check_and_run(sc);
812 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
813 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
815 g_destroy_consumer(cp);
819 static struct g_geom *
820 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md,
823 struct g_stripe_softc *sc;
828 G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
831 /* Two disks is minimum. */
832 if (md->md_all < 2) {
833 G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name);
837 /* Stripe size have to be grater than or equal to sector size. */
838 if (md->md_stripesize < sectorsize) {
839 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
843 /* Stripe size have to be power of 2. */
844 if (!powerof2(md->md_stripesize)) {
845 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name);
849 /* Check for duplicate unit */
850 LIST_FOREACH(gp, &mp->geom, geom) {
852 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
853 G_STRIPE_DEBUG(0, "Device %s already configured.",
858 gp = g_new_geomf(mp, "%s", md->md_name);
859 sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO);
860 gp->start = g_stripe_start;
861 gp->spoiled = g_stripe_orphan;
862 gp->orphan = g_stripe_orphan;
863 gp->access = g_stripe_access;
864 gp->dumpconf = g_stripe_dumpconf;
866 sc->sc_id = md->md_id;
867 sc->sc_stripesize = md->md_stripesize;
868 sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1);
869 sc->sc_ndisks = md->md_all;
870 sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks,
871 M_STRIPE, M_WAITOK | M_ZERO);
872 for (no = 0; no < sc->sc_ndisks; no++)
873 sc->sc_disks[no] = NULL;
875 mtx_init(&sc->sc_lock, "gstripe lock", NULL, MTX_DEF);
879 sc->sc_provider = NULL;
881 G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
887 g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force)
889 struct g_provider *pp;
890 struct g_consumer *cp, *cp1;
898 pp = sc->sc_provider;
899 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
901 G_STRIPE_DEBUG(0, "Device %s is still open, so it "
902 "can't be definitely removed.", pp->name);
905 "Device %s is still open (r%dw%de%d).", pp->name,
906 pp->acr, pp->acw, pp->ace);
912 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
913 g_stripe_remove_disk(cp);
915 return (0); /* Recursion happened. */
917 if (!LIST_EMPTY(&gp->consumer))
918 return (EINPROGRESS);
921 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
923 free(sc->sc_disks, M_STRIPE);
924 mtx_destroy(&sc->sc_lock);
926 G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name);
927 g_wither_geom(gp, ENXIO);
932 g_stripe_destroy_geom(struct gctl_req *req __unused,
933 struct g_class *mp __unused, struct g_geom *gp)
935 struct g_stripe_softc *sc;
938 return (g_stripe_destroy(sc, 0));
941 static struct g_geom *
942 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
944 struct g_stripe_metadata md;
945 struct g_stripe_softc *sc;
946 struct g_consumer *cp;
950 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
953 /* Skip providers that are already open for writing. */
957 G_STRIPE_DEBUG(3, "Tasting %s.", pp->name);
959 gp = g_new_geomf(mp, "stripe:taste");
960 gp->start = g_stripe_start;
961 gp->access = g_stripe_access;
962 gp->orphan = g_stripe_orphan;
963 cp = g_new_consumer(gp);
965 error = g_stripe_read_metadata(cp, &md);
967 g_destroy_consumer(cp);
973 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0)
975 if (md.md_version > G_STRIPE_VERSION) {
976 printf("geom_stripe.ko module is too old to handle %s.\n",
981 * Backward compatibility:
983 /* There was no md_provider field in earlier versions of metadata. */
984 if (md.md_version < 2)
985 bzero(md.md_provider, sizeof(md.md_provider));
986 /* There was no md_provsize field in earlier versions of metadata. */
987 if (md.md_version < 3)
988 md.md_provsize = pp->mediasize;
990 if (md.md_provider[0] != '\0' &&
991 !g_compare_names(md.md_provider, pp->name))
993 if (md.md_provsize != pp->mediasize)
997 * Let's check if device already exists.
1000 LIST_FOREACH(gp, &mp->geom, geom) {
1004 if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC)
1006 if (strcmp(md.md_name, sc->sc_name) != 0)
1008 if (md.md_id != sc->sc_id)
1013 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1014 error = g_stripe_add_disk(sc, pp, md.md_no);
1017 "Cannot add disk %s to %s (error=%d).", pp->name,
1022 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC);
1024 G_STRIPE_DEBUG(0, "Cannot create device %s.",
1029 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
1030 error = g_stripe_add_disk(sc, pp, md.md_no);
1033 "Cannot add disk %s to %s (error=%d).", pp->name,
1035 g_stripe_destroy(sc, 1);
1044 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp)
1047 struct g_stripe_metadata md;
1048 struct g_provider *pp;
1049 struct g_stripe_softc *sc;
1057 g_topology_assert();
1058 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1059 if (nargs == NULL) {
1060 gctl_error(req, "No '%s' argument.", "nargs");
1064 gctl_error(req, "Too few arguments.");
1068 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic));
1069 md.md_version = G_STRIPE_VERSION;
1070 name = gctl_get_asciiparam(req, "arg0");
1072 gctl_error(req, "No 'arg%u' argument.", 0);
1075 strlcpy(md.md_name, name, sizeof(md.md_name));
1076 md.md_id = arc4random();
1078 md.md_all = *nargs - 1;
1079 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
1080 if (stripesize == NULL) {
1081 gctl_error(req, "No '%s' argument.", "stripesize");
1084 md.md_stripesize = (uint32_t)*stripesize;
1085 bzero(md.md_provider, sizeof(md.md_provider));
1086 /* This field is not important here. */
1089 /* Check all providers are valid */
1090 for (no = 1; no < *nargs; no++) {
1091 snprintf(param, sizeof(param), "arg%u", no);
1092 name = gctl_get_asciiparam(req, param);
1094 gctl_error(req, "No 'arg%u' argument.", no);
1097 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1098 name += strlen("/dev/");
1099 pp = g_provider_by_name(name);
1101 G_STRIPE_DEBUG(1, "Disk %s is invalid.", name);
1102 gctl_error(req, "Disk %s is invalid.", name);
1107 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL);
1109 gctl_error(req, "Can't configure %s.", md.md_name);
1114 sb = sbuf_new_auto();
1115 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
1116 for (attached = 0, no = 1; no < *nargs; no++) {
1117 snprintf(param, sizeof(param), "arg%u", no);
1118 name = gctl_get_asciiparam(req, param);
1120 gctl_error(req, "No 'arg%u' argument.", no);
1123 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1124 name += strlen("/dev/");
1125 pp = g_provider_by_name(name);
1126 KASSERT(pp != NULL, ("Provider %s disappear?!", name));
1127 if (g_stripe_add_disk(sc, pp, no - 1) != 0) {
1128 G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.",
1129 no, pp->name, gp->name);
1130 sbuf_printf(sb, " %s", pp->name);
1136 if (md.md_all != attached) {
1137 g_stripe_destroy(gp->softc, 1);
1138 gctl_error(req, "%s", sbuf_data(sb));
1143 static struct g_stripe_softc *
1144 g_stripe_find_device(struct g_class *mp, const char *name)
1146 struct g_stripe_softc *sc;
1149 LIST_FOREACH(gp, &mp->geom, geom) {
1153 if (strcmp(sc->sc_name, name) == 0)
1160 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1162 struct g_stripe_softc *sc;
1163 int *force, *nargs, error;
1168 g_topology_assert();
1170 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1171 if (nargs == NULL) {
1172 gctl_error(req, "No '%s' argument.", "nargs");
1176 gctl_error(req, "Missing device(s).");
1179 force = gctl_get_paraml(req, "force", sizeof(*force));
1180 if (force == NULL) {
1181 gctl_error(req, "No '%s' argument.", "force");
1185 for (i = 0; i < (u_int)*nargs; i++) {
1186 snprintf(param, sizeof(param), "arg%u", i);
1187 name = gctl_get_asciiparam(req, param);
1189 gctl_error(req, "No 'arg%u' argument.", i);
1192 sc = g_stripe_find_device(mp, name);
1194 gctl_error(req, "No such device: %s.", name);
1197 error = g_stripe_destroy(sc, *force);
1199 gctl_error(req, "Cannot destroy device %s (error=%d).",
1200 sc->sc_name, error);
1207 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1211 g_topology_assert();
1213 version = gctl_get_paraml(req, "version", sizeof(*version));
1214 if (version == NULL) {
1215 gctl_error(req, "No '%s' argument.", "version");
1218 if (*version != G_STRIPE_VERSION) {
1219 gctl_error(req, "Userland and kernel parts are out of sync.");
1223 if (strcmp(verb, "create") == 0) {
1224 g_stripe_ctl_create(req, mp);
1226 } else if (strcmp(verb, "destroy") == 0 ||
1227 strcmp(verb, "stop") == 0) {
1228 g_stripe_ctl_destroy(req, mp);
1232 gctl_error(req, "Unknown verb.");
1236 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1237 struct g_consumer *cp, struct g_provider *pp)
1239 struct g_stripe_softc *sc;
1246 } else if (cp != NULL) {
1247 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent,
1250 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1251 sbuf_printf(sb, "%s<Stripesize>%ju</Stripesize>\n", indent,
1252 (uintmax_t)sc->sc_stripesize);
1253 sbuf_printf(sb, "%s<Type>", indent);
1254 switch (sc->sc_type) {
1255 case G_STRIPE_TYPE_AUTOMATIC:
1256 sbuf_cat(sb, "AUTOMATIC");
1258 case G_STRIPE_TYPE_MANUAL:
1259 sbuf_cat(sb, "MANUAL");
1262 sbuf_cat(sb, "UNKNOWN");
1265 sbuf_cat(sb, "</Type>\n");
1266 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1267 indent, sc->sc_ndisks, g_stripe_nvalid(sc));
1268 sbuf_printf(sb, "%s<State>", indent);
1269 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1272 sbuf_cat(sb, "DOWN");
1273 sbuf_cat(sb, "</State>\n");
1277 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe);
1278 MODULE_VERSION(geom_stripe, 0);