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);
599 disk->d_hardcoded = md.md_provider[0] != '\0';
601 disk->d_hardcoded = false;
605 disk->d_consumer = cp;
607 disk->d_start = 0; /* not yet */
608 disk->d_end = 0; /* not yet */
611 G_CONCAT_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
613 g_concat_check_and_run(sc);
614 sx_sunlock(&sc->sc_disks_lock); // need lock for check_and_run
618 sx_sunlock(&sc->sc_disks_lock);
619 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
620 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
622 g_destroy_consumer(cp);
626 static struct g_geom *
627 g_concat_create(struct g_class *mp, const struct g_concat_metadata *md,
630 struct g_concat_softc *sc;
631 struct g_concat_disk *disk;
635 G_CONCAT_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
638 /* One disks is minimum. */
642 /* Check for duplicate unit */
643 LIST_FOREACH(gp, &mp->geom, geom) {
645 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
646 G_CONCAT_DEBUG(0, "Device %s already configured.",
651 gp = g_new_geomf(mp, "%s", md->md_name);
652 sc = malloc(sizeof(*sc), M_CONCAT, M_WAITOK | M_ZERO);
653 gp->start = g_concat_start;
654 gp->spoiled = g_concat_orphan;
655 gp->orphan = g_concat_orphan;
656 gp->access = g_concat_access;
657 gp->dumpconf = g_concat_dumpconf;
659 sc->sc_id = md->md_id;
660 sc->sc_ndisks = md->md_all;
661 TAILQ_INIT(&sc->sc_disks);
662 for (no = 0; no < sc->sc_ndisks; no++) {
663 disk = malloc(sizeof(*disk), M_CONCAT, M_WAITOK | M_ZERO);
664 TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next);
667 mtx_init(&sc->sc_completion_lock, "gconcat lock", NULL, MTX_DEF);
668 sx_init(&sc->sc_disks_lock, "gconcat append lock");
672 sc->sc_provider = NULL;
674 G_CONCAT_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
680 g_concat_destroy(struct g_concat_softc *sc, boolean_t force)
682 struct g_provider *pp;
683 struct g_consumer *cp, *cp1;
685 struct g_concat_disk *disk;
692 pp = sc->sc_provider;
693 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
695 G_CONCAT_DEBUG(0, "Device %s is still open, so it "
696 "can't be definitely removed.", pp->name);
699 "Device %s is still open (r%dw%de%d).", pp->name,
700 pp->acr, pp->acw, pp->ace);
706 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
707 g_concat_remove_disk(cp->private);
709 return (0); /* Recursion happened. */
711 if (!LIST_EMPTY(&gp->consumer))
712 return (EINPROGRESS);
715 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
717 while ((disk = TAILQ_FIRST(&sc->sc_disks)) != NULL) {
718 TAILQ_REMOVE(&sc->sc_disks, disk, d_next);
719 free(disk, M_CONCAT);
721 mtx_destroy(&sc->sc_completion_lock);
722 sx_destroy(&sc->sc_disks_lock);
725 G_CONCAT_DEBUG(0, "Device %s destroyed.", gp->name);
726 g_wither_geom(gp, ENXIO);
731 g_concat_destroy_geom(struct gctl_req *req __unused,
732 struct g_class *mp __unused, struct g_geom *gp)
734 struct g_concat_softc *sc;
737 return (g_concat_destroy(sc, 0));
740 static struct g_geom *
741 g_concat_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
743 struct g_concat_metadata md;
744 struct g_concat_softc *sc;
745 struct g_consumer *cp;
749 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
752 /* Skip providers that are already open for writing. */
756 G_CONCAT_DEBUG(3, "Tasting %s.", pp->name);
758 gp = g_new_geomf(mp, "concat:taste");
759 gp->start = g_concat_start;
760 gp->access = g_concat_access;
761 gp->orphan = g_concat_orphan;
762 cp = g_new_consumer(gp);
763 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
764 error = g_attach(cp, pp);
766 error = g_concat_read_metadata(cp, &md);
769 g_destroy_consumer(cp);
775 if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0)
777 if (md.md_version > G_CONCAT_VERSION) {
778 printf("geom_concat.ko module is too old to handle %s.\n",
783 * Backward compatibility:
785 /* There was no md_provider field in earlier versions of metadata. */
786 if (md.md_version < 3)
787 bzero(md.md_provider, sizeof(md.md_provider));
788 /* There was no md_provsize field in earlier versions of metadata. */
789 if (md.md_version < 4)
790 md.md_provsize = pp->mediasize;
792 if (md.md_provider[0] != '\0' &&
793 !g_compare_names(md.md_provider, pp->name))
795 if (md.md_provsize != pp->mediasize)
799 * Let's check if device already exists.
802 LIST_FOREACH(gp, &mp->geom, geom) {
806 if (sc->sc_type != G_CONCAT_TYPE_AUTOMATIC)
808 if (strcmp(md.md_name, sc->sc_name) != 0)
810 if (md.md_id != sc->sc_id)
815 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
816 error = g_concat_add_disk(sc, pp, md.md_no);
819 "Cannot add disk %s to %s (error=%d).", pp->name,
824 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_AUTOMATIC);
826 G_CONCAT_DEBUG(0, "Cannot create device %s.",
831 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
832 error = g_concat_add_disk(sc, pp, md.md_no);
835 "Cannot add disk %s to %s (error=%d).", pp->name,
837 g_concat_destroy(sc, 1);
846 g_concat_ctl_create(struct gctl_req *req, struct g_class *mp)
849 struct g_concat_metadata md;
850 struct g_provider *pp;
851 struct g_concat_softc *sc;
859 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
861 gctl_error(req, "No '%s' argument.", "nargs");
865 gctl_error(req, "Too few arguments.");
869 bzero(&md, sizeof(md));
870 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
871 md.md_version = G_CONCAT_VERSION;
872 name = gctl_get_asciiparam(req, "arg0");
874 gctl_error(req, "No 'arg%u' argument.", 0);
877 strlcpy(md.md_name, name, sizeof(md.md_name));
878 md.md_id = arc4random();
880 md.md_all = *nargs - 1;
881 /* This field is not important here. */
884 /* Check all providers are valid */
885 for (no = 1; no < *nargs; no++) {
886 snprintf(param, sizeof(param), "arg%u", no);
887 pp = gctl_get_provider(req, param);
892 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_MANUAL);
894 gctl_error(req, "Can't configure %s.", md.md_name);
899 sb = sbuf_new_auto();
900 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
901 for (attached = 0, no = 1; no < *nargs; no++) {
902 snprintf(param, sizeof(param), "arg%u", no);
903 pp = gctl_get_provider(req, param);
905 name = gctl_get_asciiparam(req, param);
907 sbuf_printf(sb, " %s", name);
910 if (g_concat_add_disk(sc, pp, no - 1) != 0) {
911 G_CONCAT_DEBUG(1, "Disk %u (%s) not attached to %s.",
912 no, pp->name, gp->name);
913 sbuf_printf(sb, " %s", pp->name);
919 if (md.md_all != attached) {
920 g_concat_destroy(gp->softc, 1);
921 gctl_error(req, "%s", sbuf_data(sb));
926 static struct g_concat_softc *
927 g_concat_find_device(struct g_class *mp, const char *name)
929 struct g_concat_softc *sc;
932 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
933 name += strlen(_PATH_DEV);
935 LIST_FOREACH(gp, &mp->geom, geom) {
939 if (strcmp(sc->sc_name, name) == 0)
946 g_concat_ctl_destroy(struct gctl_req *req, struct g_class *mp)
948 struct g_concat_softc *sc;
949 int *force, *nargs, error;
956 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
958 gctl_error(req, "No '%s' argument.", "nargs");
962 gctl_error(req, "Missing device(s).");
965 force = gctl_get_paraml(req, "force", sizeof(*force));
967 gctl_error(req, "No '%s' argument.", "force");
971 for (i = 0; i < (u_int)*nargs; i++) {
972 snprintf(param, sizeof(param), "arg%u", i);
973 name = gctl_get_asciiparam(req, param);
975 gctl_error(req, "No 'arg%u' argument.", i);
978 sc = g_concat_find_device(mp, name);
980 gctl_error(req, "No such device: %s.", name);
983 error = g_concat_destroy(sc, *force);
985 gctl_error(req, "Cannot destroy device %s (error=%d).",
992 static struct g_concat_disk *
993 g_concat_find_disk(struct g_concat_softc *sc, const char *name)
995 struct g_concat_disk *disk;
997 sx_assert(&sc->sc_disks_lock, SX_LOCKED);
998 if (strncmp(name, "/dev/", 5) == 0)
1000 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1001 if (disk->d_consumer == NULL)
1003 if (disk->d_consumer->provider == NULL)
1005 if (strcmp(disk->d_consumer->provider->name, name) == 0)
1012 g_concat_write_metadata(struct gctl_req *req, struct g_concat_softc *sc)
1015 struct g_concat_disk *disk;
1016 struct g_concat_metadata md;
1017 struct g_provider *pp;
1021 bzero(&md, sizeof(md));
1022 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
1023 md.md_version = G_CONCAT_VERSION;
1024 strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
1025 md.md_id = sc->sc_id;
1026 md.md_all = sc->sc_ndisks;
1027 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) {
1028 pp = disk->d_consumer->provider;
1031 if (disk->d_hardcoded)
1032 strlcpy(md.md_provider, pp->name,
1033 sizeof(md.md_provider));
1034 md.md_provsize = disk->d_consumer->provider->mediasize;
1036 sector = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1037 concat_metadata_encode(&md, sector);
1038 error = g_access(disk->d_consumer, 0, 1, 0);
1040 error = g_write_data(disk->d_consumer,
1041 pp->mediasize - pp->sectorsize, sector,
1043 (void)g_access(disk->d_consumer, 0, -1, 0);
1047 gctl_error(req, "Cannot store metadata on %s: %d",
1055 g_concat_ctl_append(struct gctl_req *req, struct g_class *mp)
1057 struct g_concat_softc *sc;
1058 struct g_consumer *cp, *fcp;
1059 struct g_provider *pp;
1061 const char *name, *cname;
1062 struct g_concat_disk *disk;
1063 int *nargs, *hardcode;
1067 g_topology_assert();
1069 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1070 if (nargs == NULL) {
1071 gctl_error(req, "No '%s' argument.", "nargs");
1075 gctl_error(req, "Invalid number of arguments.");
1078 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode));
1079 if (hardcode == NULL) {
1080 gctl_error(req, "No '%s' argument.", "hardcode");
1084 cname = gctl_get_asciiparam(req, "arg0");
1085 if (cname == NULL) {
1086 gctl_error(req, "No 'arg%u' argument.", 0);
1089 sc = g_concat_find_device(mp, cname);
1091 gctl_error(req, "No such device: %s.", cname);
1094 if (sc->sc_provider == NULL) {
1096 * this won't race with g_concat_remove_disk as both
1097 * are holding the topology lock
1099 gctl_error(req, "Device not active, can't append: %s.", cname);
1102 G_CONCAT_DEBUG(1, "Appending to %s:", cname);
1103 sx_xlock(&sc->sc_disks_lock);
1105 fcp = LIST_FIRST(&gp->consumer);
1107 name = gctl_get_asciiparam(req, "arg1");
1109 gctl_error(req, "No 'arg%u' argument.", 1);
1112 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
1113 name += strlen("/dev/");
1114 pp = g_provider_by_name(name);
1116 G_CONCAT_DEBUG(1, "Disk %s is invalid.", name);
1117 gctl_error(req, "Disk %s is invalid.", name);
1120 G_CONCAT_DEBUG(1, "Appending %s to this", name);
1122 if (g_concat_find_disk(sc, name) != NULL) {
1123 gctl_error(req, "Disk %s already appended.", name);
1127 if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) {
1128 gctl_error(req, "Providers sectorsize mismatch: %u vs %u",
1129 sc->sc_provider->sectorsize, pp->sectorsize);
1133 cp = g_new_consumer(gp);
1134 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
1135 error = g_attach(cp, pp);
1137 g_destroy_consumer(cp);
1138 gctl_error(req, "Cannot open device %s (error=%d).",
1143 error = g_access(cp, 1, 0, 0);
1145 error = g_getattr("GEOM::candelete", cp, &disk_candelete);
1148 (void)g_access(cp, -1, 0, 0);
1150 G_CONCAT_DEBUG(1, "Failed to access disk %s, error %d.", name, error);
1152 /* invoke g_access exactly as deep as all the other members currently are */
1153 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
1154 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
1157 g_destroy_consumer(cp);
1158 gctl_error(req, "Failed to access disk %s (error=%d).", name, error);
1163 disk = malloc(sizeof(*disk), M_CONCAT, M_WAITOK | M_ZERO);
1164 disk->d_consumer = cp;
1166 disk->d_start = TAILQ_LAST(&sc->sc_disks, g_concat_disks)->d_end;
1167 disk->d_end = disk->d_start + cp->provider->mediasize;
1168 disk->d_candelete = disk_candelete;
1169 disk->d_removed = 0;
1170 disk->d_hardcoded = *hardcode;
1172 TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next);
1175 if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) {
1176 /* last sector is for metadata */
1177 disk->d_end -= cp->provider->sectorsize;
1179 /* update metadata on all parts */
1180 g_concat_write_metadata(req, sc);
1183 g_resize_provider(sc->sc_provider, disk->d_end);
1186 sx_xunlock(&sc->sc_disks_lock);
1190 g_concat_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_CONCAT_VERSION) {
1202 gctl_error(req, "Userland and kernel parts are out of sync.");
1206 if (strcmp(verb, "create") == 0) {
1207 g_concat_ctl_create(req, mp);
1209 } else if (strcmp(verb, "destroy") == 0 ||
1210 strcmp(verb, "stop") == 0) {
1211 g_concat_ctl_destroy(req, mp);
1213 } else if (strcmp(verb, "append") == 0) {
1214 g_concat_ctl_append(req, mp);
1217 gctl_error(req, "Unknown verb.");
1221 g_concat_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1222 struct g_consumer *cp, struct g_provider *pp)
1224 struct g_concat_softc *sc;
1226 g_topology_assert();
1231 sx_slock(&sc->sc_disks_lock);
1234 } else if (cp != NULL) {
1235 struct g_concat_disk *disk;
1240 sbuf_printf(sb, "%s<End>%jd</End>\n", indent,
1241 (intmax_t)disk->d_end);
1242 sbuf_printf(sb, "%s<Start>%jd</Start>\n", indent,
1243 (intmax_t)disk->d_start);
1245 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
1246 sbuf_printf(sb, "%s<Type>", indent);
1247 switch (sc->sc_type) {
1248 case G_CONCAT_TYPE_AUTOMATIC:
1249 sbuf_cat(sb, "AUTOMATIC");
1251 case G_CONCAT_TYPE_MANUAL:
1252 sbuf_cat(sb, "MANUAL");
1255 sbuf_cat(sb, "UNKNOWN");
1258 sbuf_cat(sb, "</Type>\n");
1259 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
1260 indent, sc->sc_ndisks, g_concat_nvalid(sc));
1261 sbuf_printf(sb, "%s<State>", indent);
1262 if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
1265 sbuf_cat(sb, "DOWN");
1266 sbuf_cat(sb, "</State>\n");
1269 sx_sunlock(&sc->sc_disks_lock);
1272 DECLARE_GEOM_CLASS(g_concat_class, g_concat);
1273 MODULE_VERSION(geom_concat, 0);