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>
41 #include <sys/sysctl.h>
42 #include <sys/malloc.h>
43 #include <geom/geom.h>
44 #include <geom/geom_dbg.h>
45 #include <geom/concat/g_concat.h>
47 FEATURE(geom_concat, "GEOM concatenation support");
49 static MALLOC_DEFINE(M_CONCAT, "concat_data", "GEOM_CONCAT Data");
51 SYSCTL_DECL(_kern_geom);
52 static SYSCTL_NODE(_kern_geom, OID_AUTO, concat, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
54 static u_int g_concat_debug = 0;
55 SYSCTL_UINT(_kern_geom_concat, OID_AUTO, debug, CTLFLAG_RWTUN, &g_concat_debug, 0,
58 static int g_concat_destroy(struct g_concat_softc *sc, boolean_t force);
59 static int g_concat_destroy_geom(struct gctl_req *req, struct g_class *mp,
62 static g_taste_t g_concat_taste;
63 static g_ctl_req_t g_concat_config;
64 static g_dumpconf_t g_concat_dumpconf;
66 struct g_class g_concat_class = {
67 .name = G_CONCAT_CLASS_NAME,
69 .ctlreq = g_concat_config,
70 .taste = g_concat_taste,
71 .destroy_geom = g_concat_destroy_geom
75 * Greatest Common Divisor.
91 * Least Common Multiple.
97 return ((a * b) / gcd(a, b));
101 * Return the number of valid disks.
104 g_concat_nvalid(struct g_concat_softc *sc)
107 struct g_concat_disk *disk;
109 sx_assert(&sc->sc_disks_lock, SA_LOCKED);
112 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
113 if (disk->d_consumer != NULL)
121 g_concat_remove_disk(struct g_concat_disk *disk)
123 struct g_consumer *cp;
124 struct g_concat_softc *sc;
127 KASSERT(disk->d_consumer != NULL, ("Non-valid disk in %s.", __func__));
129 cp = disk->d_consumer;
131 if (!disk->d_removed) {
132 G_CONCAT_DEBUG(0, "Disk %s removed from %s.",
133 cp->provider->name, sc->sc_name);
137 if (sc->sc_provider != NULL) {
138 G_CONCAT_DEBUG(0, "Device %s deactivated.",
139 sc->sc_provider->name);
140 g_wither_provider(sc->sc_provider, ENXIO);
141 sc->sc_provider = NULL;
144 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
146 disk->d_consumer = NULL;
148 g_destroy_consumer(cp);
149 /* If there are no valid disks anymore, remove device. */
150 if (LIST_EMPTY(&sc->sc_geom->consumer))
151 g_concat_destroy(sc, 1);
155 g_concat_orphan(struct g_consumer *cp)
157 struct g_concat_softc *sc;
158 struct g_concat_disk *disk;
168 if (disk == NULL) /* Possible? */
170 g_concat_remove_disk(disk);
174 g_concat_access(struct g_provider *pp, int dr, int dw, int de)
176 struct g_consumer *cp1, *cp2, *tmp;
177 struct g_concat_disk *disk;
179 struct g_concat_softc *sc;
186 /* On first open, grab an extra "exclusive" bit */
187 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
189 /* ... and let go of it on last close */
190 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
193 sx_slock(&sc->sc_disks_lock);
194 LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) {
195 error = g_access(cp1, dr, dw, de);
199 if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 &&
201 g_concat_remove_disk(disk); /* May destroy geom. */
204 sx_sunlock(&sc->sc_disks_lock);
208 sx_sunlock(&sc->sc_disks_lock);
209 LIST_FOREACH(cp2, &gp->consumer, consumer) {
212 g_access(cp2, -dr, -dw, -de);
218 g_concat_candelete(struct bio *bp)
220 struct g_concat_softc *sc;
221 struct g_concat_disk *disk;
224 sc = bp->bio_to->geom->softc;
225 sx_assert(&sc->sc_disks_lock, SX_LOCKED);
226 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
227 if (!disk->d_removed && disk->d_candelete)
231 g_handleattr(bp, "GEOM::candelete", &val, sizeof(val));
235 g_concat_kernel_dump(struct bio *bp)
237 struct g_concat_softc *sc;
238 struct g_concat_disk *disk;
240 struct g_kerneldump *gkd;
242 sc = bp->bio_to->geom->softc;
243 gkd = (struct g_kerneldump *)bp->bio_data;
244 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
245 if (disk->d_start <= gkd->offset &&
246 disk->d_end > gkd->offset)
250 g_io_deliver(bp, EOPNOTSUPP);
254 gkd->offset -= disk->d_start;
255 if (gkd->length > disk->d_end - disk->d_start - gkd->offset)
256 gkd->length = disk->d_end - disk->d_start - gkd->offset;
257 cbp = g_clone_bio(bp);
259 g_io_deliver(bp, ENOMEM);
262 cbp->bio_done = g_std_done;
263 g_io_request(cbp, disk->d_consumer);
264 G_CONCAT_DEBUG(1, "Kernel dump will go to %s.",
265 disk->d_consumer->provider->name);
269 g_concat_done(struct bio *bp)
271 struct g_concat_softc *sc;
274 pbp = bp->bio_parent;
275 sc = pbp->bio_to->geom->softc;
276 mtx_lock(&sc->sc_completion_lock);
277 if (pbp->bio_error == 0)
278 pbp->bio_error = bp->bio_error;
279 pbp->bio_completed += bp->bio_completed;
281 if (pbp->bio_children == pbp->bio_inbed) {
282 mtx_unlock(&sc->sc_completion_lock);
283 g_io_deliver(pbp, pbp->bio_error);
285 mtx_unlock(&sc->sc_completion_lock);
290 * Called for both BIO_FLUSH and BIO_SPEEDUP. Just pass the call down
293 g_concat_passdown(struct g_concat_softc *sc, struct bio *bp)
295 struct bio_queue_head queue;
296 struct g_consumer *cp;
298 struct g_concat_disk *disk;
300 sx_assert(&sc->sc_disks_lock, SX_LOCKED);
303 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
304 cbp = g_clone_bio(bp);
306 while ((cbp = bioq_takefirst(&queue)) != NULL)
308 if (bp->bio_error == 0)
309 bp->bio_error = ENOMEM;
310 g_io_deliver(bp, bp->bio_error);
313 bioq_insert_tail(&queue, cbp);
314 cbp->bio_done = g_concat_done;
315 cbp->bio_caller1 = disk->d_consumer;
316 cbp->bio_to = disk->d_consumer->provider;
318 while ((cbp = bioq_takefirst(&queue)) != NULL) {
319 G_CONCAT_LOGREQ(cbp, "Sending request.");
320 cp = cbp->bio_caller1;
321 cbp->bio_caller1 = NULL;
322 g_io_request(cbp, cp);
327 g_concat_start(struct bio *bp)
329 struct bio_queue_head queue;
330 struct g_concat_softc *sc;
331 struct g_concat_disk *disk;
332 struct g_provider *pp;
333 off_t offset, end, length, off, len;
338 sc = pp->geom->softc;
340 * If sc == NULL, provider's error should be set and g_concat_start()
341 * should not be called at all.
344 ("Provider's error should be set (error=%d)(device=%s).",
345 bp->bio_to->error, bp->bio_to->name));
347 G_CONCAT_LOGREQ(bp, "Request received.");
348 sx_slock(&sc->sc_disks_lock);
350 switch (bp->bio_cmd) {
357 g_concat_passdown(sc, bp);
360 if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
361 g_concat_kernel_dump(bp);
363 } else if (strcmp("GEOM::candelete", bp->bio_attribute) == 0) {
364 g_concat_candelete(bp);
367 /* To which provider it should be delivered? */
370 g_io_deliver(bp, EOPNOTSUPP);
374 offset = bp->bio_offset;
375 length = bp->bio_length;
376 if ((bp->bio_flags & BIO_UNMAPPED) != 0)
380 end = offset + length;
383 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
384 if (disk->d_end <= offset)
386 if (disk->d_start >= end)
389 off = offset - disk->d_start;
390 len = MIN(length, disk->d_end - offset);
394 cbp = g_clone_bio(bp);
396 while ((cbp = bioq_takefirst(&queue)) != NULL)
398 if (bp->bio_error == 0)
399 bp->bio_error = ENOMEM;
400 g_io_deliver(bp, bp->bio_error);
403 bioq_insert_tail(&queue, cbp);
405 * Fill in the component buf structure.
407 if (len == bp->bio_length)
408 cbp->bio_done = g_std_done;
410 cbp->bio_done = g_concat_done;
411 cbp->bio_offset = off;
412 cbp->bio_length = len;
413 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
414 cbp->bio_ma_offset += (uintptr_t)addr;
415 cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
416 cbp->bio_ma_offset %= PAGE_SIZE;
417 cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
418 cbp->bio_length) / PAGE_SIZE;
420 cbp->bio_data = addr;
422 cbp->bio_to = disk->d_consumer->provider;
423 cbp->bio_caller1 = disk;
429 ("Length is still greater than 0 (class=%s, name=%s).",
430 bp->bio_to->geom->class->name, bp->bio_to->geom->name));
431 while ((cbp = bioq_takefirst(&queue)) != NULL) {
432 G_CONCAT_LOGREQ(cbp, "Sending request.");
433 disk = cbp->bio_caller1;
434 cbp->bio_caller1 = NULL;
435 g_io_request(cbp, disk->d_consumer);
438 sx_sunlock(&sc->sc_disks_lock);
442 g_concat_check_and_run(struct g_concat_softc *sc)
444 struct g_concat_disk *disk;
445 struct g_provider *dp, *pp;
446 u_int sectorsize = 0;
451 if (g_concat_nvalid(sc) != sc->sc_ndisks)
454 pp = g_new_providerf(sc->sc_geom, "concat/%s", sc->sc_name);
455 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE |
456 G_PF_ACCEPT_UNMAPPED;
458 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
459 dp = disk->d_consumer->provider;
460 disk->d_start = start;
461 disk->d_end = disk->d_start + dp->mediasize;
462 if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC)
463 disk->d_end -= dp->sectorsize;
465 error = g_access(disk->d_consumer, 1, 0, 0);
467 error = g_getattr("GEOM::candelete", disk->d_consumer,
470 disk->d_candelete = 0;
471 (void)g_access(disk->d_consumer, -1, 0, 0);
473 G_CONCAT_DEBUG(1, "Failed to access disk %s, error %d.",
475 if (disk == TAILQ_FIRST(&sc->sc_disks))
476 sectorsize = dp->sectorsize;
478 sectorsize = lcm(sectorsize, dp->sectorsize);
480 /* A provider underneath us doesn't support unmapped */
481 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
482 G_CONCAT_DEBUG(1, "Cancelling unmapped "
483 "because of %s.", dp->name);
484 pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
487 pp->sectorsize = sectorsize;
488 /* We have sc->sc_disks[sc->sc_ndisks - 1].d_end in 'start'. */
489 pp->mediasize = start;
490 dp = TAILQ_FIRST(&sc->sc_disks)->d_consumer->provider;
491 pp->stripesize = dp->stripesize;
492 pp->stripeoffset = dp->stripeoffset;
493 sc->sc_provider = pp;
494 g_error_provider(pp, 0);
496 G_CONCAT_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
500 g_concat_read_metadata(struct g_consumer *cp, struct g_concat_metadata *md)
502 struct g_provider *pp;
508 error = g_access(cp, 1, 0, 0);
513 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
516 g_access(cp, -1, 0, 0);
520 /* Decode metadata. */
521 concat_metadata_decode(buf, md);
528 * Add disk to given device.
531 g_concat_add_disk(struct g_concat_softc *sc, struct g_provider *pp, u_int no)
533 struct g_concat_disk *disk;
534 struct g_consumer *cp, *fcp;
540 sx_slock(&sc->sc_disks_lock);
542 /* Metadata corrupted? */
543 if (no >= sc->sc_ndisks) {
544 sx_sunlock(&sc->sc_disks_lock);
548 for (disk = TAILQ_FIRST(&sc->sc_disks); no > 0; no--) {
549 disk = TAILQ_NEXT(disk, d_next);
552 /* Check if disk is not already attached. */
553 if (disk->d_consumer != NULL) {
554 sx_sunlock(&sc->sc_disks_lock);
559 fcp = LIST_FIRST(&gp->consumer);
561 cp = g_new_consumer(gp);
562 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
563 error = g_attach(cp, pp);
565 sx_sunlock(&sc->sc_disks_lock);
566 g_destroy_consumer(cp);
570 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
571 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
573 sx_sunlock(&sc->sc_disks_lock);
575 g_destroy_consumer(cp);
579 if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) {
580 struct g_concat_metadata md;
582 // temporarily give up the lock to avoid lock order violation
583 // due to topology unlock in g_concat_read_metadata
584 sx_sunlock(&sc->sc_disks_lock);
585 /* Re-read metadata. */
586 error = g_concat_read_metadata(cp, &md);
587 sx_slock(&sc->sc_disks_lock);
592 if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0 ||
593 strcmp(md.md_name, sc->sc_name) != 0 ||
594 md.md_id != sc->sc_id) {
595 G_CONCAT_DEBUG(0, "Metadata on %s changed.", pp->name);
601 disk->d_consumer = cp;
603 disk->d_start = 0; /* not yet */
604 disk->d_end = 0; /* not yet */
607 G_CONCAT_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
609 g_concat_check_and_run(sc);
610 sx_sunlock(&sc->sc_disks_lock); // need lock for check_and_run
614 sx_sunlock(&sc->sc_disks_lock);
615 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
616 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
618 g_destroy_consumer(cp);
622 static struct g_geom *
623 g_concat_create(struct g_class *mp, const struct g_concat_metadata *md,
626 struct g_concat_softc *sc;
627 struct g_concat_disk *disk;
631 G_CONCAT_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
634 /* One disks is minimum. */
638 /* Check for duplicate unit */
639 LIST_FOREACH(gp, &mp->geom, geom) {
641 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
642 G_CONCAT_DEBUG(0, "Device %s already configured.",
647 gp = g_new_geomf(mp, "%s", md->md_name);
648 sc = malloc(sizeof(*sc), M_CONCAT, M_WAITOK | M_ZERO);
649 gp->start = g_concat_start;
650 gp->spoiled = g_concat_orphan;
651 gp->orphan = g_concat_orphan;
652 gp->access = g_concat_access;
653 gp->dumpconf = g_concat_dumpconf;
655 sc->sc_id = md->md_id;
656 sc->sc_ndisks = md->md_all;
657 TAILQ_INIT(&sc->sc_disks);
658 for (no = 0; no < sc->sc_ndisks; no++) {
659 disk = malloc(sizeof(*disk), M_CONCAT, M_WAITOK | M_ZERO);
660 TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next);
663 mtx_init(&sc->sc_completion_lock, "gconcat lock", NULL, MTX_DEF);
664 sx_init(&sc->sc_disks_lock, "gconcat append lock");
668 sc->sc_provider = NULL;
670 G_CONCAT_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
676 g_concat_destroy(struct g_concat_softc *sc, boolean_t force)
678 struct g_provider *pp;
679 struct g_consumer *cp, *cp1;
681 struct g_concat_disk *disk;
688 pp = sc->sc_provider;
689 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
691 G_CONCAT_DEBUG(0, "Device %s is still open, so it "
692 "can't be definitely removed.", pp->name);
695 "Device %s is still open (r%dw%de%d).", pp->name,
696 pp->acr, pp->acw, pp->ace);
702 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
703 g_concat_remove_disk(cp->private);
705 return (0); /* Recursion happened. */
707 if (!LIST_EMPTY(&gp->consumer))
708 return (EINPROGRESS);
711 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
713 while ((disk = TAILQ_FIRST(&sc->sc_disks)) != NULL) {
714 TAILQ_REMOVE(&sc->sc_disks, disk, d_next);
715 free(disk, M_CONCAT);
717 mtx_destroy(&sc->sc_completion_lock);
718 sx_destroy(&sc->sc_disks_lock);
721 G_CONCAT_DEBUG(0, "Device %s destroyed.", gp->name);
722 g_wither_geom(gp, ENXIO);
727 g_concat_destroy_geom(struct gctl_req *req __unused,
728 struct g_class *mp __unused, struct g_geom *gp)
730 struct g_concat_softc *sc;
733 return (g_concat_destroy(sc, 0));
736 static struct g_geom *
737 g_concat_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
739 struct g_concat_metadata md;
740 struct g_concat_softc *sc;
741 struct g_consumer *cp;
745 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
748 /* Skip providers that are already open for writing. */
752 G_CONCAT_DEBUG(3, "Tasting %s.", pp->name);
754 gp = g_new_geomf(mp, "concat:taste");
755 gp->start = g_concat_start;
756 gp->access = g_concat_access;
757 gp->orphan = g_concat_orphan;
758 cp = g_new_consumer(gp);
759 error = g_attach(cp, pp);
761 error = g_concat_read_metadata(cp, &md);
764 g_destroy_consumer(cp);
770 if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0)
772 if (md.md_version > G_CONCAT_VERSION) {
773 printf("geom_concat.ko module is too old to handle %s.\n",
778 * Backward compatibility:
780 /* There was no md_provider field in earlier versions of metadata. */
781 if (md.md_version < 3)
782 bzero(md.md_provider, sizeof(md.md_provider));
783 /* There was no md_provsize field in earlier versions of metadata. */
784 if (md.md_version < 4)
785 md.md_provsize = pp->mediasize;
787 if (md.md_provider[0] != '\0' &&
788 !g_compare_names(md.md_provider, pp->name))
790 if (md.md_provsize != pp->mediasize)
794 * Let's check if device already exists.
797 LIST_FOREACH(gp, &mp->geom, geom) {
801 if (sc->sc_type != G_CONCAT_TYPE_AUTOMATIC)
803 if (strcmp(md.md_name, sc->sc_name) != 0)
805 if (md.md_id != sc->sc_id)
810 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
811 error = g_concat_add_disk(sc, pp, md.md_no);
814 "Cannot add disk %s to %s (error=%d).", pp->name,
819 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_AUTOMATIC);
821 G_CONCAT_DEBUG(0, "Cannot create device %s.",
826 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
827 error = g_concat_add_disk(sc, pp, md.md_no);
830 "Cannot add disk %s to %s (error=%d).", pp->name,
832 g_concat_destroy(sc, 1);
841 g_concat_ctl_create(struct gctl_req *req, struct g_class *mp)
844 struct g_concat_metadata md;
845 struct g_provider *pp;
846 struct g_concat_softc *sc;
854 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
856 gctl_error(req, "No '%s' argument.", "nargs");
860 gctl_error(req, "Too few arguments.");
864 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
865 md.md_version = G_CONCAT_VERSION;
866 name = gctl_get_asciiparam(req, "arg0");
868 gctl_error(req, "No 'arg%u' argument.", 0);
871 strlcpy(md.md_name, name, sizeof(md.md_name));
872 md.md_id = arc4random();
874 md.md_all = *nargs - 1;
875 bzero(md.md_provider, sizeof(md.md_provider));
876 /* This field is not important here. */
879 /* Check all providers are valid */
880 for (no = 1; no < *nargs; no++) {
881 snprintf(param, sizeof(param), "arg%u", no);
882 pp = gctl_get_provider(req, param);
887 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_MANUAL);
889 gctl_error(req, "Can't configure %s.", md.md_name);
894 sb = sbuf_new_auto();
895 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
896 for (attached = 0, no = 1; no < *nargs; no++) {
897 snprintf(param, sizeof(param), "arg%u", no);
898 pp = gctl_get_provider(req, param);
900 name = gctl_get_asciiparam(req, param);
902 sbuf_printf(sb, " %s", name);
905 if (g_concat_add_disk(sc, pp, no - 1) != 0) {
906 G_CONCAT_DEBUG(1, "Disk %u (%s) not attached to %s.",
907 no, pp->name, gp->name);
908 sbuf_printf(sb, " %s", pp->name);
914 if (md.md_all != attached) {
915 g_concat_destroy(gp->softc, 1);
916 gctl_error(req, "%s", sbuf_data(sb));
921 static struct g_concat_softc *
922 g_concat_find_device(struct g_class *mp, const char *name)
924 struct g_concat_softc *sc;
927 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
928 name += strlen(_PATH_DEV);
930 LIST_FOREACH(gp, &mp->geom, geom) {
934 if (strcmp(sc->sc_name, name) == 0)
941 g_concat_ctl_destroy(struct gctl_req *req, struct g_class *mp)
943 struct g_concat_softc *sc;
944 int *force, *nargs, error;
951 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
953 gctl_error(req, "No '%s' argument.", "nargs");
957 gctl_error(req, "Missing device(s).");
960 force = gctl_get_paraml(req, "force", sizeof(*force));
962 gctl_error(req, "No '%s' argument.", "force");
966 for (i = 0; i < (u_int)*nargs; i++) {
967 snprintf(param, sizeof(param), "arg%u", i);
968 name = gctl_get_asciiparam(req, param);
970 gctl_error(req, "No 'arg%u' argument.", i);
973 sc = g_concat_find_device(mp, name);
975 gctl_error(req, "No such device: %s.", name);
978 error = g_concat_destroy(sc, *force);
980 gctl_error(req, "Cannot destroy device %s (error=%d).",
988 g_concat_config(struct gctl_req *req, struct g_class *mp, const char *verb)
994 version = gctl_get_paraml(req, "version", sizeof(*version));
995 if (version == NULL) {
996 gctl_error(req, "No '%s' argument.", "version");
999 if (*version != G_CONCAT_VERSION) {
1000 gctl_error(req, "Userland and kernel parts are out of sync.");
1004 if (strcmp(verb, "create") == 0) {
1005 g_concat_ctl_create(req, mp);
1007 } else if (strcmp(verb, "destroy") == 0 ||
1008 strcmp(verb, "stop") == 0) {
1009 g_concat_ctl_destroy(req, mp);
1012 gctl_error(req, "Unknown verb.");
1016 g_concat_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1017 struct g_consumer *cp, struct g_provider *pp)
1019 struct g_concat_softc *sc;
1021 g_topology_assert();
1026 sx_slock(&sc->sc_disks_lock);
1029 } else if (cp != NULL) {
1030 struct g_concat_disk *disk;
1035 sbuf_printf(sb, "%s<End>%jd</End>\n", indent,
1036 (intmax_t)disk->d_end);
1037 sbuf_printf(sb, "%s<Start>%jd</Start>\n", indent,
1038 (intmax_t)disk->d_start);
1040 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1041 sbuf_printf(sb, "%s<Type>", indent);
1042 switch (sc->sc_type) {
1043 case G_CONCAT_TYPE_AUTOMATIC:
1044 sbuf_cat(sb, "AUTOMATIC");
1046 case G_CONCAT_TYPE_MANUAL:
1047 sbuf_cat(sb, "MANUAL");
1050 sbuf_cat(sb, "UNKNOWN");
1053 sbuf_cat(sb, "</Type>\n");
1054 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1055 indent, sc->sc_ndisks, g_concat_nvalid(sc));
1056 sbuf_printf(sb, "%s<State>", indent);
1057 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1060 sbuf_cat(sb, "DOWN");
1061 sbuf_cat(sb, "</State>\n");
1064 sx_sunlock(&sc->sc_disks_lock);
1067 DECLARE_GEOM_CLASS(g_concat_class, g_concat);
1068 MODULE_VERSION(geom_concat, 0);