g_concat.c revision 129478
18876Srgrimes/*-
24Srgrimes * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
34Srgrimes * All rights reserved.
44Srgrimes *
58876Srgrimes * Redistribution and use in source and binary forms, with or without
64Srgrimes * modification, are permitted provided that the following conditions
74Srgrimes * are met:
84Srgrimes * 1. Redistributions of source code must retain the above copyright
94Srgrimes *    notice, this list of conditions and the following disclaimer.
104Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
118876Srgrimes *    notice, this list of conditions and the following disclaimer in the
128876Srgrimes *    documentation and/or other materials provided with the distribution.
134Srgrimes *
144Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
158876Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
164Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
178876Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
184Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
194Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
204Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
214Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
228876Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
234Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
244Srgrimes * SUCH DAMAGE.
254Srgrimes */
268876Srgrimes
274Srgrimes#include <sys/cdefs.h>
28623Srgrimes__FBSDID("$FreeBSD: head/sys/geom/concat/g_concat.c 129478 2004-05-20 10:40:18Z pjd $");
294Srgrimes
304Srgrimes#include <sys/param.h>
314Srgrimes#include <sys/systm.h>
324Srgrimes#include <sys/kernel.h>
334Srgrimes#include <sys/module.h>
344Srgrimes#include <sys/lock.h>
354Srgrimes#include <sys/mutex.h>
362056Swollman#include <sys/bio.h>
372056Swollman#include <sys/sysctl.h>
382056Swollman#include <sys/malloc.h>
392056Swollman#include <geom/geom.h>
404Srgrimes#include <geom/concat/g_concat.h>
414Srgrimes
424Srgrimes
434Srgrimesstatic MALLOC_DEFINE(M_CONCAT, "concat data", "GEOM_CONCAT Data");
444Srgrimes
454SrgrimesSYSCTL_DECL(_kern_geom);
464SrgrimesSYSCTL_NODE(_kern_geom, OID_AUTO, concat, CTLFLAG_RW, 0, "GEOM_CONCAT stuff");
474Srgrimesstatic u_int g_concat_debug = 0;
484SrgrimesSYSCTL_UINT(_kern_geom_concat, OID_AUTO, debug, CTLFLAG_RW, &g_concat_debug, 0,
494Srgrimes    "Debug level");
504Srgrimes
514Srgrimesstatic int g_concat_destroy(struct g_concat_softc *sc, boolean_t force);
524Srgrimesstatic int g_concat_destroy_geom(struct gctl_req *req, struct g_class *mp,
534Srgrimes    struct g_geom *gp);
544Srgrimes
554Srgrimesstatic g_taste_t g_concat_taste;
564Srgrimesstatic g_ctl_req_t g_concat_config;
574Srgrimesstatic g_dumpconf_t g_concat_dumpconf;
584Srgrimes
594Srgrimesstruct g_class g_concat_class = {
604Srgrimes	.name = G_CONCAT_CLASS_NAME,
614Srgrimes	.ctlreq = g_concat_config,
624Srgrimes	.taste = g_concat_taste,
634Srgrimes	.destroy_geom = g_concat_destroy_geom
644Srgrimes};
654Srgrimes
664Srgrimes
674Srgrimes/*
684Srgrimes * Greatest Common Divisor.
694Srgrimes */
704Srgrimesstatic u_int
714Srgrimesgcd(u_int a, u_int b)
724Srgrimes{
734Srgrimes	u_int c;
744Srgrimes
754Srgrimes	while (b != 0) {
764Srgrimes		c = a;
774Srgrimes		a = b;
784Srgrimes		b = (c % b);
794Srgrimes	}
804Srgrimes	return (a);
814Srgrimes}
824Srgrimes
834Srgrimes/*
844Srgrimes * Least Common Multiple.
854Srgrimes */
864Srgrimesstatic u_int
874Srgrimeslcm(u_int a, u_int b)
884Srgrimes{
894Srgrimes
904Srgrimes	return ((a * b) / gcd(a, b));
914Srgrimes}
924Srgrimes
934Srgrimes/*
944Srgrimes * Return the number of valid disks.
954Srgrimes */
964Srgrimesstatic u_int
974Srgrimesg_concat_nvalid(struct g_concat_softc *sc)
984Srgrimes{
994Srgrimes	u_int i, no;
1004Srgrimes
1014Srgrimes	no = 0;
1024Srgrimes	for (i = 0; i < sc->sc_ndisks; i++) {
1034Srgrimes		if (sc->sc_disks[i].d_consumer != NULL)
1044Srgrimes			no++;
1054Srgrimes	}
1064Srgrimes
1074Srgrimes	return (no);
1084Srgrimes}
1094Srgrimes
1104Srgrimesstatic void
1114Srgrimesg_concat_remove_disk(struct g_concat_disk *disk)
1124Srgrimes{
1134Srgrimes	struct g_consumer *cp;
1144Srgrimes	struct g_concat_softc *sc;
1154Srgrimes
1164Srgrimes	KASSERT(disk->d_consumer != NULL, ("Non-valid disk in %s.", __func__));
1174Srgrimes	sc = disk->d_softc;
1184Srgrimes	cp = disk->d_consumer;
1194Srgrimes
1204Srgrimes	G_CONCAT_DEBUG(0, "Disk %s removed from %s.", cp->provider->name,
1214Srgrimes	    sc->sc_geom->name);
1224Srgrimes
1234Srgrimes	disk->d_consumer = NULL;
1244Srgrimes	if (sc->sc_provider != NULL) {
1254Srgrimes		g_orphan_provider(sc->sc_provider, ENXIO);
1264Srgrimes		sc->sc_provider = NULL;
1274Srgrimes		G_CONCAT_DEBUG(0, "Device %s removed.", sc->sc_geom->name);
1284Srgrimes	}
1294Srgrimes
1304Srgrimes	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
1314Srgrimes		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
1324Srgrimes	g_detach(cp);
1334Srgrimes	g_destroy_consumer(cp);
1344Srgrimes}
1354Srgrimes
1364Srgrimesstatic void
1374Srgrimesg_concat_orphan(struct g_consumer *cp)
1384Srgrimes{
1394Srgrimes	struct g_concat_softc *sc;
1404Srgrimes	struct g_concat_disk *disk;
1414Srgrimes	struct g_geom *gp;
1424Srgrimes
1434Srgrimes	g_topology_assert();
1444Srgrimes	gp = cp->geom;
1454Srgrimes	sc = gp->softc;
1464Srgrimes	if (sc == NULL)
1474Srgrimes		return;
1484Srgrimes
1494Srgrimes	disk = cp->private;
1504Srgrimes	if (disk == NULL)	/* Possible? */
1514Srgrimes		return;
1524Srgrimes	g_concat_remove_disk(disk);
1534Srgrimes
1544Srgrimes	/* If there are no valid disks anymore, remove device. */
1554Srgrimes	if (g_concat_nvalid(sc) == 0)
1564Srgrimes		g_concat_destroy(sc, 1);
1574Srgrimes}
1584Srgrimes
1594Srgrimesstatic int
1604Srgrimesg_concat_access(struct g_provider *pp, int dr, int dw, int de)
1614Srgrimes{
1624Srgrimes	struct g_consumer *cp1, *cp2;
1634Srgrimes	struct g_concat_softc *sc;
1644Srgrimes	struct g_geom *gp;
1654Srgrimes	int error;
1664Srgrimes
1674Srgrimes	gp = pp->geom;
1684Srgrimes	sc = gp->softc;
1694Srgrimes
1704Srgrimes	if (sc == NULL) {
1714Srgrimes		/*
1724Srgrimes		 * It looks like geom is being withered.
1734Srgrimes		 * In that case we allow only negative requests.
1744Srgrimes		 */
1754Srgrimes		KASSERT(dr <= 0 && dw <= 0 && de <= 0,
1764Srgrimes		    ("Positive access request (device=%s).", pp->name));
1774Srgrimes		if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 &&
1784Srgrimes		    (pp->ace + de) == 0) {
1794Srgrimes			G_CONCAT_DEBUG(0, "Device %s definitely destroyed.",
1804Srgrimes			    gp->name);
1814Srgrimes		}
1824Srgrimes		return (0);
1834Srgrimes	}
1844Srgrimes
1854Srgrimes	/* On first open, grab an extra "exclusive" bit */
1864Srgrimes	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
1874Srgrimes		de++;
1884Srgrimes	/* ... and let go of it on last close */
1894Srgrimes	if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 1)
1904Srgrimes		de--;
1914Srgrimes
1924Srgrimes	error = ENXIO;
1934Srgrimes	LIST_FOREACH(cp1, &gp->consumer, consumer) {
1944Srgrimes		error = g_access(cp1, dr, dw, de);
1954Srgrimes		if (error == 0)
1964Srgrimes			continue;
1974Srgrimes		/*
1984Srgrimes		 * If we fail here, backout all previous changes.
1994Srgrimes		 */
2004Srgrimes		LIST_FOREACH(cp2, &gp->consumer, consumer) {
2014Srgrimes			if (cp1 == cp2)
2024Srgrimes				return (error);
2034Srgrimes			g_access(cp2, -dr, -dw, -de);
2044Srgrimes		}
2054Srgrimes		/* NOTREACHED */
2064Srgrimes	}
2074Srgrimes
2084Srgrimes	return (error);
2094Srgrimes}
2104Srgrimes
2114Srgrimesstatic void
2124Srgrimesg_concat_start(struct bio *bp)
2134Srgrimes{
2144Srgrimes	struct g_concat_softc *sc;
2154Srgrimes	struct g_concat_disk *disk;
2164Srgrimes	struct g_provider *pp;
2174Srgrimes	off_t offset, end, length, off, len;
2184Srgrimes	struct bio *cbp;
2194Srgrimes	char *addr;
2204Srgrimes	u_int no;
2214Srgrimes
2224Srgrimes	pp = bp->bio_to;
2234Srgrimes	sc = pp->geom->softc;
2244Srgrimes	/*
2254Srgrimes	 * If sc == NULL, provider's error should be set and g_concat_start()
2264Srgrimes	 * should not be called at all.
2274Srgrimes	 */
2284Srgrimes	KASSERT(sc != NULL,
2294Srgrimes	    ("Provider's error should be set (error=%d)(device=%s).",
2304Srgrimes	    bp->bio_to->error, bp->bio_to->name));
2314Srgrimes
2324Srgrimes	G_CONCAT_LOGREQ(bp, "Request received.");
2334Srgrimes
2344Srgrimes	switch (bp->bio_cmd) {
2354Srgrimes	case BIO_READ:
2364Srgrimes	case BIO_WRITE:
2374Srgrimes	case BIO_DELETE:
2384Srgrimes		break;
2394Srgrimes	case BIO_GETATTR:
2404Srgrimes		/* To which provider it should be delivered? */
2414Srgrimes	default:
2424Srgrimes		g_io_deliver(bp, EOPNOTSUPP);
2434Srgrimes		return;
2444Srgrimes	}
2454Srgrimes
2464Srgrimes	offset = bp->bio_offset;
2474Srgrimes	length = bp->bio_length;
2484Srgrimes	addr = bp->bio_data;
2494Srgrimes	end = offset + length;
2504Srgrimes
2514Srgrimes	for (no = 0; no < sc->sc_ndisks; no++) {
2524Srgrimes		disk = &sc->sc_disks[no];
2534Srgrimes		if (disk->d_end <= offset)
2544Srgrimes			continue;
2554Srgrimes		if (disk->d_start >= end)
2564Srgrimes			break;
2574Srgrimes
2584Srgrimes		off = offset - disk->d_start;
2594Srgrimes		len = MIN(length, disk->d_end - offset);
2604Srgrimes		length -= len;
2614Srgrimes		offset += len;
2624Srgrimes
2634Srgrimes		cbp = g_clone_bio(bp);
2644Srgrimes		if (cbp == NULL) {
2654Srgrimes			if (bp->bio_error == 0)
2664Srgrimes				bp->bio_error = ENOMEM;
2674Srgrimes			return;
2684Srgrimes		}
2694Srgrimes		/*
2704Srgrimes		 * Fill in the component buf structure.
2714Srgrimes		 */
2724Srgrimes		cbp->bio_done = g_std_done;
2734Srgrimes		cbp->bio_offset = off;
2744Srgrimes		cbp->bio_data = addr;
2754Srgrimes		addr += len;
2764Srgrimes		cbp->bio_length = len;
2774Srgrimes		cbp->bio_to = disk->d_consumer->provider;
2784Srgrimes		G_CONCAT_LOGREQ(cbp, "Sending request.");
2794Srgrimes		g_io_request(cbp, disk->d_consumer);
2804Srgrimes
2814Srgrimes		if (length == 0)
2824Srgrimes			break;
2834Srgrimes	}
2844Srgrimes
2854Srgrimes	KASSERT(length == 0,
2864Srgrimes	    ("Length is still greater than 0 (class=%s, name=%s).",
2874Srgrimes	    bp->bio_to->geom->class->name, bp->bio_to->geom->name));
2884Srgrimes}
2894Srgrimes
2904Srgrimesstatic void
291798Swollmang_concat_check_and_run(struct g_concat_softc *sc)
2924Srgrimes{
2934Srgrimes	struct g_concat_disk *disk;
2944Srgrimes	u_int no, sectorsize = 0;
2954Srgrimes	off_t start;
2964Srgrimes
2974Srgrimes	if (g_concat_nvalid(sc) != sc->sc_ndisks)
2984Srgrimes		return;
2994Srgrimes
3004Srgrimes	sc->sc_provider = g_new_providerf(sc->sc_geom, "%s", sc->sc_geom->name);
3014Srgrimes	start = 0;
3024Srgrimes	for (no = 0; no < sc->sc_ndisks; no++) {
3034Srgrimes		disk = &sc->sc_disks[no];
3044Srgrimes		disk->d_start = start;
3054Srgrimes		disk->d_end = disk->d_start +
3064Srgrimes		    disk->d_consumer->provider->mediasize;
3074Srgrimes		if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC)
3084Srgrimes			disk->d_end -= disk->d_consumer->provider->sectorsize;
3094Srgrimes		start = disk->d_end;
3104Srgrimes		if (no == 0)
3114Srgrimes			sectorsize = disk->d_consumer->provider->sectorsize;
3124Srgrimes		else {
3134Srgrimes			sectorsize = lcm(sectorsize,
3144Srgrimes			    disk->d_consumer->provider->sectorsize);
3154Srgrimes		}
3164Srgrimes	}
3174Srgrimes	sc->sc_provider->sectorsize = sectorsize;
3184Srgrimes	/* We have sc->sc_disks[sc->sc_ndisks - 1].d_end in 'start'. */
3194Srgrimes	sc->sc_provider->mediasize = start;
3204Srgrimes	g_error_provider(sc->sc_provider, 0);
3214Srgrimes
3224Srgrimes	G_CONCAT_DEBUG(0, "Device %s activated.", sc->sc_geom->name);
3234Srgrimes}
3244Srgrimes
3254Srgrimesstatic int
3264Srgrimesg_concat_read_metadata(struct g_consumer *cp, struct g_concat_metadata *md)
3274Srgrimes{
3284Srgrimes	struct g_provider *pp;
3294Srgrimes	u_char *buf;
3304Srgrimes	int error;
3314Srgrimes
3324Srgrimes	g_topology_assert();
3334Srgrimes
3344Srgrimes	error = g_access(cp, 1, 0, 0);
3354Srgrimes	if (error != 0)
3364Srgrimes		return (error);
3374Srgrimes	pp = cp->provider;
3384Srgrimes	g_topology_unlock();
3394Srgrimes	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
3404Srgrimes	    &error);
3414Srgrimes	g_topology_lock();
3424Srgrimes	g_access(cp, -1, 0, 0);
3434Srgrimes	if (buf == NULL)
3444Srgrimes		return (error);
3454Srgrimes
3464Srgrimes	/* Decode metadata. */
3474Srgrimes	concat_metadata_decode(buf, md);
3484Srgrimes	g_free(buf);
3494Srgrimes
350	return (0);
351}
352
353/*
354 * Add disk to given device.
355 */
356static int
357g_concat_add_disk(struct g_concat_softc *sc, struct g_provider *pp, u_int no)
358{
359	struct g_concat_disk *disk;
360	struct g_consumer *cp, *fcp;
361	struct g_geom *gp;
362	int error;
363
364	/* Metadata corrupted? */
365	if (no >= sc->sc_ndisks)
366		return (EINVAL);
367
368	disk = &sc->sc_disks[no];
369	/* Check if disk is not already attached. */
370	if (disk->d_consumer != NULL)
371		return (EEXIST);
372
373	gp = sc->sc_geom;
374	fcp = LIST_FIRST(&gp->consumer);
375
376	cp = g_new_consumer(gp);
377	error = g_attach(cp, pp);
378	if (error != 0) {
379		g_destroy_consumer(cp);
380		return (error);
381	}
382
383	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
384		error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
385		if (error != 0) {
386			g_detach(cp);
387			g_destroy_consumer(cp);
388			return (error);
389		}
390	}
391	if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) {
392		struct g_concat_metadata md;
393
394		/* Re-read metadata. */
395		error = g_concat_read_metadata(cp, &md);
396		if (error != 0)
397			goto fail;
398
399		if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0 ||
400		    strcmp(md.md_name, sc->sc_name) != 0 ||
401		    md.md_id != sc->sc_id) {
402			G_CONCAT_DEBUG(0, "Metadata on %s changed.", pp->name);
403			goto fail;
404		}
405	}
406
407	cp->private = disk;
408	disk->d_consumer = cp;
409	disk->d_softc = sc;
410	disk->d_start = 0;	/* not yet */
411	disk->d_end = 0;	/* not yet */
412
413	G_CONCAT_DEBUG(0, "Disk %s attached to %s.", pp->name, gp->name);
414
415	g_concat_check_and_run(sc);
416
417	return (0);
418fail:
419	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
420		g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
421	g_detach(cp);
422	g_destroy_consumer(cp);
423	return (error);
424}
425
426static struct g_geom *
427g_concat_create(struct g_class *mp, const struct g_concat_metadata *md,
428    u_int type)
429{
430	struct g_concat_softc *sc;
431	struct g_geom *gp;
432	u_int no;
433
434	G_CONCAT_DEBUG(1, "Creating device %s.concat (id=%u).", md->md_name,
435	    md->md_id);
436
437	/* Two disks is minimum. */
438	if (md->md_all <= 1)
439		return (NULL);
440
441	/* Check for duplicate unit */
442	LIST_FOREACH(gp, &mp->geom, geom) {
443		sc = gp->softc;
444		if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
445			G_CONCAT_DEBUG(0, "Device %s already configured.",
446			    gp->name);
447			return (NULL);
448		}
449	}
450	gp = g_new_geomf(mp, "%s.concat", md->md_name);
451	gp->softc = NULL;	/* for a moment */
452
453	sc = malloc(sizeof(*sc), M_CONCAT, M_NOWAIT | M_ZERO);
454	if (sc == NULL) {
455		G_CONCAT_DEBUG(0, "Can't allocate memory for device %s.",
456		    gp->name);
457		g_destroy_geom(gp);
458		return (NULL);
459	}
460
461	gp->start = g_concat_start;
462	gp->spoiled = g_concat_orphan;
463	gp->orphan = g_concat_orphan;
464	gp->access = g_concat_access;
465	gp->dumpconf = g_concat_dumpconf;
466
467	strlcpy(sc->sc_name, md->md_name, sizeof(sc->sc_name));
468	sc->sc_id = md->md_id;
469	sc->sc_ndisks = md->md_all;
470	sc->sc_disks = malloc(sizeof(struct g_concat_disk) * sc->sc_ndisks,
471	    M_CONCAT, M_WAITOK | M_ZERO);
472	for (no = 0; no < sc->sc_ndisks; no++)
473		sc->sc_disks[no].d_consumer = NULL;
474	sc->sc_type = type;
475
476	gp->softc = sc;
477	sc->sc_geom = gp;
478	sc->sc_provider = NULL;
479
480	G_CONCAT_DEBUG(0, "Device %s created (id=%u).", gp->name, sc->sc_id);
481
482	return (gp);
483}
484
485static int
486g_concat_destroy(struct g_concat_softc *sc, boolean_t force)
487{
488	struct g_provider *pp;
489	struct g_geom *gp;
490	u_int no;
491
492	g_topology_assert();
493
494	if (sc == NULL)
495		return (ENXIO);
496
497	pp = sc->sc_provider;
498	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
499		if (force) {
500			G_CONCAT_DEBUG(0, "Device %s is still open, so it "
501			    "can't be definitely removed.", pp->name);
502		} else {
503			G_CONCAT_DEBUG(1,
504			    "Device %s is still open (r%dw%de%d).", pp->name,
505			    pp->acr, pp->acw, pp->ace);
506			return (EBUSY);
507		}
508	}
509
510	for (no = 0; no < sc->sc_ndisks; no++) {
511		if (sc->sc_disks[no].d_consumer != NULL)
512			g_concat_remove_disk(&sc->sc_disks[no]);
513	}
514
515	gp = sc->sc_geom;
516	gp->softc = NULL;
517	KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
518	    gp->name));
519	free(sc->sc_disks, M_CONCAT);
520	free(sc, M_CONCAT);
521
522	pp = LIST_FIRST(&gp->provider);
523	if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
524		G_CONCAT_DEBUG(0, "Device %s destroyed.", gp->name);
525
526	g_wither_geom(gp, ENXIO);
527
528	return (0);
529}
530
531static int
532g_concat_destroy_geom(struct gctl_req *req __unused,
533    struct g_class *mp __unused, struct g_geom *gp)
534{
535	struct g_concat_softc *sc;
536
537	sc = gp->softc;
538	return (g_concat_destroy(sc, 0));
539}
540
541static struct g_geom *
542g_concat_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
543{
544	struct g_concat_metadata md;
545	struct g_concat_softc *sc;
546	struct g_consumer *cp;
547	struct g_geom *gp;
548	int error;
549
550	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
551	g_topology_assert();
552
553	G_CONCAT_DEBUG(3, "Tasting %s.", pp->name);
554
555	gp = g_new_geomf(mp, "concat:taste");
556	gp->start = g_concat_start;
557	gp->access = g_concat_access;
558	gp->orphan = g_concat_orphan;
559	cp = g_new_consumer(gp);
560	g_attach(cp, pp);
561
562	error = g_concat_read_metadata(cp, &md);
563	g_wither_geom(gp, ENXIO);
564	if (error != 0)
565		return (NULL);
566	gp = NULL;
567
568	if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0)
569		return (NULL);
570	if (md.md_version > G_CONCAT_VERSION) {
571		printf("geom_concat.ko module is too old to handle %s.\n",
572		    pp->name);
573		return (NULL);
574	}
575
576	/*
577	 * Let's check if device already exists.
578	 */
579	sc = NULL;
580	LIST_FOREACH(gp, &mp->geom, geom) {
581		sc = gp->softc;
582		if (sc == NULL)
583			continue;
584		if (sc->sc_type != G_CONCAT_TYPE_AUTOMATIC)
585			continue;
586		if (strcmp(md.md_name, sc->sc_name) != 0)
587			continue;
588		if (md.md_id != sc->sc_id)
589			continue;
590		break;
591	}
592	if (gp != NULL) {
593		G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
594		error = g_concat_add_disk(sc, pp, md.md_no);
595		if (error != 0) {
596			G_CONCAT_DEBUG(0,
597			    "Cannot add disk %s to %s (error=%d).", pp->name,
598			    gp->name, error);
599			return (NULL);
600		}
601	} else {
602		gp = g_concat_create(mp, &md, G_CONCAT_TYPE_AUTOMATIC);
603		if (gp == NULL) {
604			G_CONCAT_DEBUG(0, "Cannot create device %s.concat.",
605			    md.md_name);
606			return (NULL);
607		}
608		sc = gp->softc;
609		G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
610		error = g_concat_add_disk(sc, pp, md.md_no);
611		if (error != 0) {
612			G_CONCAT_DEBUG(0,
613			    "Cannot add disk %s to %s (error=%d).", pp->name,
614			    gp->name, error);
615			g_concat_destroy(sc, 1);
616			return (NULL);
617		}
618	}
619
620	return (gp);
621}
622
623static void
624g_concat_ctl_create(struct gctl_req *req, struct g_class *mp)
625{
626	u_int attached, no;
627	struct g_concat_metadata md;
628	struct g_provider *pp;
629	struct g_concat_softc *sc;
630	struct g_geom *gp;
631	struct sbuf *sb;
632	const char *name;
633	char param[16];
634	int *nargs;
635
636	g_topology_assert();
637	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
638	if (nargs == NULL) {
639		gctl_error(req, "No '%s' argument.", "nargs");
640		return;
641	}
642	if (*nargs <= 2) {
643		gctl_error(req, "Too few arguments.");
644		return;
645	}
646
647	strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
648	md.md_version = G_CONCAT_VERSION;
649	name = gctl_get_asciiparam(req, "arg0");
650	if (name == NULL) {
651		gctl_error(req, "No 'arg%u' argument.", 0);
652		return;
653	}
654	strlcpy(md.md_name, name, sizeof(md.md_name));
655	md.md_id = arc4random();
656	md.md_no = 0;
657	md.md_all = *nargs - 1;
658
659	/* Check all providers are valid */
660	for (no = 1; no < *nargs; no++) {
661		snprintf(param, sizeof(param), "arg%u", no);
662		name = gctl_get_asciiparam(req, param);
663		if (name == NULL) {
664			gctl_error(req, "No 'arg%u' argument.", no);
665			return;
666		}
667		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
668			name += strlen("/dev/");
669		pp = g_provider_by_name(name);
670		if (pp == NULL) {
671			G_CONCAT_DEBUG(1, "Disk %s is invalid.", name);
672			gctl_error(req, "Disk %s is invalid.", name);
673			return;
674		}
675	}
676
677	gp = g_concat_create(mp, &md, G_CONCAT_TYPE_MANUAL);
678	if (gp == NULL) {
679		gctl_error(req, "Can't configure %s.concat.", md.md_name);
680		return;
681	}
682
683	sc = gp->softc;
684	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
685	sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
686	for (attached = 0, no = 1; no < *nargs; no++) {
687		snprintf(param, sizeof(param), "arg%u", no);
688		name = gctl_get_asciiparam(req, param);
689		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
690			name += strlen("/dev/");
691		pp = g_provider_by_name(name);
692		KASSERT(pp != NULL, ("Provider %s disappear?!", name));
693		if (g_concat_add_disk(sc, pp, no - 1) != 0) {
694			G_CONCAT_DEBUG(1, "Disk %u (%s) not attached to %s.",
695			    no, pp->name, gp->name);
696			sbuf_printf(sb, " %s", pp->name);
697			continue;
698		}
699		attached++;
700	}
701	sbuf_finish(sb);
702	if (md.md_all != attached) {
703		g_concat_destroy(gp->softc, 1);
704		gctl_error(req, "%s", sbuf_data(sb));
705	}
706	sbuf_delete(sb);
707}
708
709static struct g_concat_softc *
710g_concat_find_device(struct g_class *mp, const char *name)
711{
712	struct g_concat_softc *sc;
713	struct g_geom *gp;
714
715	LIST_FOREACH(gp, &mp->geom, geom) {
716		sc = gp->softc;
717		if (sc == NULL)
718			continue;
719		if (strcmp(gp->name, name) == 0 ||
720		    strcmp(sc->sc_name, name) == 0) {
721			return (sc);
722		}
723	}
724	return (NULL);
725}
726
727static void
728g_concat_ctl_destroy(struct gctl_req *req, struct g_class *mp)
729{
730	struct g_concat_softc *sc;
731	int *force, *nargs, error;
732	const char *name;
733	char param[16];
734	u_int i;
735
736	g_topology_assert();
737
738	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
739	if (nargs == NULL) {
740		gctl_error(req, "No '%s' argument.", "nargs");
741		return;
742	}
743	if (*nargs <= 0) {
744		gctl_error(req, "Missing device(s).");
745		return;
746	}
747	force = gctl_get_paraml(req, "force", sizeof(*force));
748	if (force == NULL) {
749		gctl_error(req, "No '%s' argument.", "force");
750		return;
751	}
752
753	for (i = 0; i < (u_int)*nargs; i++) {
754		snprintf(param, sizeof(param), "arg%u", i);
755		name = gctl_get_asciiparam(req, param);
756		if (name == NULL) {
757			gctl_error(req, "No 'arg%u' argument.", i);
758			return;
759		}
760		sc = g_concat_find_device(mp, name);
761		if (sc == NULL) {
762			gctl_error(req, "No such device: %s.", name);
763			return;
764		}
765		error = g_concat_destroy(sc, *force);
766		if (error != 0) {
767			gctl_error(req, "Cannot destroy device %s (error=%d).",
768			    sc->sc_geom->name, error);
769			return;
770		}
771	}
772}
773
774static void
775g_concat_config(struct gctl_req *req, struct g_class *mp, const char *verb)
776{
777	uint32_t *version;
778
779	g_topology_assert();
780
781	version = gctl_get_paraml(req, "version", sizeof(*version));
782	if (version == NULL) {
783		gctl_error(req, "No '%s' argument.", "version");
784		return;
785	}
786	if (*version != G_CONCAT_VERSION) {
787		gctl_error(req, "Userland and kernel parts are out of sync.");
788		return;
789	}
790
791	if (strcmp(verb, "create") == 0) {
792		g_concat_ctl_create(req, mp);
793		return;
794	} else if (strcmp(verb, "destroy") == 0) {
795		g_concat_ctl_destroy(req, mp);
796		return;
797	}
798	gctl_error(req, "Unknown verb.");
799}
800
801static void
802g_concat_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
803    struct g_consumer *cp, struct g_provider *pp)
804{
805	struct g_concat_softc *sc;
806
807	sc = gp->softc;
808	if (sc == NULL)
809		return;
810	if (pp == NULL && cp == NULL) {
811		sbuf_printf(sb, "%s<id>%u</id>\n", indent, (u_int)sc->sc_id);
812		switch (sc->sc_type) {
813		case G_CONCAT_TYPE_AUTOMATIC:
814			sbuf_printf(sb, "%s<type>%s</type>\n", indent,
815			    "automatic");
816			break;
817		case G_CONCAT_TYPE_MANUAL:
818			sbuf_printf(sb, "%s<type>%s</type>\n", indent,
819			    "manual");
820			break;
821		default:
822			sbuf_printf(sb, "%s<type>%s</type>\n", indent,
823			    "unknown");
824			break;
825		}
826	}
827}
828
829DECLARE_GEOM_CLASS(g_concat_class, g_concat);
830