g_concat.c revision 197898
1/*-
2 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/geom/concat/g_concat.c 197898 2009-10-09 09:42:22Z pjd $");
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/kernel.h>
33#include <sys/module.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36#include <sys/bio.h>
37#include <sys/sysctl.h>
38#include <sys/malloc.h>
39#include <geom/geom.h>
40#include <geom/concat/g_concat.h>
41
42
43static MALLOC_DEFINE(M_CONCAT, "concat_data", "GEOM_CONCAT Data");
44
45SYSCTL_DECL(_kern_geom);
46SYSCTL_NODE(_kern_geom, OID_AUTO, concat, CTLFLAG_RW, 0, "GEOM_CONCAT stuff");
47static u_int g_concat_debug = 0;
48TUNABLE_INT("kern.geom.concat.debug", &g_concat_debug);
49SYSCTL_UINT(_kern_geom_concat, OID_AUTO, debug, CTLFLAG_RW, &g_concat_debug, 0,
50    "Debug level");
51
52static int g_concat_destroy(struct g_concat_softc *sc, boolean_t force);
53static int g_concat_destroy_geom(struct gctl_req *req, struct g_class *mp,
54    struct g_geom *gp);
55
56static g_taste_t g_concat_taste;
57static g_ctl_req_t g_concat_config;
58static g_dumpconf_t g_concat_dumpconf;
59
60struct g_class g_concat_class = {
61	.name = G_CONCAT_CLASS_NAME,
62	.version = G_VERSION,
63	.ctlreq = g_concat_config,
64	.taste = g_concat_taste,
65	.destroy_geom = g_concat_destroy_geom
66};
67
68
69/*
70 * Greatest Common Divisor.
71 */
72static u_int
73gcd(u_int a, u_int b)
74{
75	u_int c;
76
77	while (b != 0) {
78		c = a;
79		a = b;
80		b = (c % b);
81	}
82	return (a);
83}
84
85/*
86 * Least Common Multiple.
87 */
88static u_int
89lcm(u_int a, u_int b)
90{
91
92	return ((a * b) / gcd(a, b));
93}
94
95/*
96 * Return the number of valid disks.
97 */
98static u_int
99g_concat_nvalid(struct g_concat_softc *sc)
100{
101	u_int i, no;
102
103	no = 0;
104	for (i = 0; i < sc->sc_ndisks; i++) {
105		if (sc->sc_disks[i].d_consumer != NULL)
106			no++;
107	}
108
109	return (no);
110}
111
112static void
113g_concat_remove_disk(struct g_concat_disk *disk)
114{
115	struct g_consumer *cp;
116	struct g_concat_softc *sc;
117
118	KASSERT(disk->d_consumer != NULL, ("Non-valid disk in %s.", __func__));
119	sc = disk->d_softc;
120	cp = disk->d_consumer;
121
122	G_CONCAT_DEBUG(0, "Disk %s removed from %s.", cp->provider->name,
123	    sc->sc_name);
124
125	disk->d_consumer = NULL;
126	if (sc->sc_provider != NULL) {
127		sc->sc_provider->flags |= G_PF_WITHER;
128		g_orphan_provider(sc->sc_provider, ENXIO);
129		sc->sc_provider = NULL;
130		G_CONCAT_DEBUG(0, "Device %s removed.", sc->sc_name);
131	}
132
133	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
134		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
135	g_detach(cp);
136	g_destroy_consumer(cp);
137}
138
139static void
140g_concat_orphan(struct g_consumer *cp)
141{
142	struct g_concat_softc *sc;
143	struct g_concat_disk *disk;
144	struct g_geom *gp;
145
146	g_topology_assert();
147	gp = cp->geom;
148	sc = gp->softc;
149	if (sc == NULL)
150		return;
151
152	disk = cp->private;
153	if (disk == NULL)	/* Possible? */
154		return;
155	g_concat_remove_disk(disk);
156
157	/* If there are no valid disks anymore, remove device. */
158	if (g_concat_nvalid(sc) == 0)
159		g_concat_destroy(sc, 1);
160}
161
162static int
163g_concat_access(struct g_provider *pp, int dr, int dw, int de)
164{
165	struct g_consumer *cp1, *cp2;
166	struct g_concat_softc *sc;
167	struct g_geom *gp;
168	int error;
169
170	gp = pp->geom;
171	sc = gp->softc;
172
173	if (sc == NULL) {
174		/*
175		 * It looks like geom is being withered.
176		 * In that case we allow only negative requests.
177		 */
178		KASSERT(dr <= 0 && dw <= 0 && de <= 0,
179		    ("Positive access request (device=%s).", pp->name));
180		if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 &&
181		    (pp->ace + de) == 0) {
182			G_CONCAT_DEBUG(0, "Device %s definitely destroyed.",
183			    gp->name);
184		}
185		return (0);
186	}
187
188	/* On first open, grab an extra "exclusive" bit */
189	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
190		de++;
191	/* ... and let go of it on last close */
192	if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
193		de--;
194
195	error = ENXIO;
196	LIST_FOREACH(cp1, &gp->consumer, consumer) {
197		error = g_access(cp1, dr, dw, de);
198		if (error == 0)
199			continue;
200		/*
201		 * If we fail here, backout all previous changes.
202		 */
203		LIST_FOREACH(cp2, &gp->consumer, consumer) {
204			if (cp1 == cp2)
205				return (error);
206			g_access(cp2, -dr, -dw, -de);
207		}
208		/* NOTREACHED */
209	}
210
211	return (error);
212}
213
214static void
215g_concat_flush(struct g_concat_softc *sc, struct bio *bp)
216{
217	struct bio_queue_head queue;
218	struct g_consumer *cp;
219	struct bio *cbp;
220	u_int no;
221
222	bioq_init(&queue);
223	for (no = 0; no < sc->sc_ndisks; no++) {
224		cbp = g_clone_bio(bp);
225		if (cbp == NULL) {
226			for (cbp = bioq_first(&queue); cbp != NULL;
227			    cbp = bioq_first(&queue)) {
228				bioq_remove(&queue, cbp);
229				g_destroy_bio(cbp);
230			}
231			if (bp->bio_error == 0)
232				bp->bio_error = ENOMEM;
233			g_io_deliver(bp, bp->bio_error);
234			return;
235		}
236		bioq_insert_tail(&queue, cbp);
237		cbp->bio_done = g_std_done;
238		cbp->bio_caller1 = sc->sc_disks[no].d_consumer;
239		cbp->bio_to = sc->sc_disks[no].d_consumer->provider;
240	}
241	for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
242		bioq_remove(&queue, cbp);
243		G_CONCAT_LOGREQ(cbp, "Sending request.");
244		cp = cbp->bio_caller1;
245		cbp->bio_caller1 = NULL;
246		g_io_request(cbp, cp);
247	}
248}
249
250static void
251g_concat_start(struct bio *bp)
252{
253	struct bio_queue_head queue;
254	struct g_concat_softc *sc;
255	struct g_concat_disk *disk;
256	struct g_provider *pp;
257	off_t offset, end, length, off, len;
258	struct bio *cbp;
259	char *addr;
260	u_int no;
261
262	pp = bp->bio_to;
263	sc = pp->geom->softc;
264	/*
265	 * If sc == NULL, provider's error should be set and g_concat_start()
266	 * should not be called at all.
267	 */
268	KASSERT(sc != NULL,
269	    ("Provider's error should be set (error=%d)(device=%s).",
270	    bp->bio_to->error, bp->bio_to->name));
271
272	G_CONCAT_LOGREQ(bp, "Request received.");
273
274	switch (bp->bio_cmd) {
275	case BIO_READ:
276	case BIO_WRITE:
277	case BIO_DELETE:
278		break;
279	case BIO_FLUSH:
280		g_concat_flush(sc, bp);
281		return;
282	case BIO_GETATTR:
283		/* To which provider it should be delivered? */
284	default:
285		g_io_deliver(bp, EOPNOTSUPP);
286		return;
287	}
288
289	offset = bp->bio_offset;
290	length = bp->bio_length;
291	addr = bp->bio_data;
292	end = offset + length;
293
294	bioq_init(&queue);
295	for (no = 0; no < sc->sc_ndisks; no++) {
296		disk = &sc->sc_disks[no];
297		if (disk->d_end <= offset)
298			continue;
299		if (disk->d_start >= end)
300			break;
301
302		off = offset - disk->d_start;
303		len = MIN(length, disk->d_end - offset);
304		length -= len;
305		offset += len;
306
307		cbp = g_clone_bio(bp);
308		if (cbp == NULL) {
309			for (cbp = bioq_first(&queue); cbp != NULL;
310			    cbp = bioq_first(&queue)) {
311				bioq_remove(&queue, cbp);
312				g_destroy_bio(cbp);
313			}
314			if (bp->bio_error == 0)
315				bp->bio_error = ENOMEM;
316			g_io_deliver(bp, bp->bio_error);
317			return;
318		}
319		bioq_insert_tail(&queue, cbp);
320		/*
321		 * Fill in the component buf structure.
322		 */
323		cbp->bio_done = g_std_done;
324		cbp->bio_offset = off;
325		cbp->bio_data = addr;
326		addr += len;
327		cbp->bio_length = len;
328		cbp->bio_to = disk->d_consumer->provider;
329		cbp->bio_caller1 = disk;
330
331		if (length == 0)
332			break;
333	}
334	KASSERT(length == 0,
335	    ("Length is still greater than 0 (class=%s, name=%s).",
336	    bp->bio_to->geom->class->name, bp->bio_to->geom->name));
337	for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) {
338		bioq_remove(&queue, cbp);
339		G_CONCAT_LOGREQ(cbp, "Sending request.");
340		disk = cbp->bio_caller1;
341		cbp->bio_caller1 = NULL;
342		g_io_request(cbp, disk->d_consumer);
343	}
344}
345
346static void
347g_concat_check_and_run(struct g_concat_softc *sc)
348{
349	struct g_concat_disk *disk;
350	u_int no, sectorsize = 0;
351	off_t start;
352
353	if (g_concat_nvalid(sc) != sc->sc_ndisks)
354		return;
355
356	sc->sc_provider = g_new_providerf(sc->sc_geom, "concat/%s",
357	    sc->sc_name);
358	start = 0;
359	for (no = 0; no < sc->sc_ndisks; no++) {
360		disk = &sc->sc_disks[no];
361		disk->d_start = start;
362		disk->d_end = disk->d_start +
363		    disk->d_consumer->provider->mediasize;
364		if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC)
365			disk->d_end -= disk->d_consumer->provider->sectorsize;
366		start = disk->d_end;
367		if (no == 0)
368			sectorsize = disk->d_consumer->provider->sectorsize;
369		else {
370			sectorsize = lcm(sectorsize,
371			    disk->d_consumer->provider->sectorsize);
372		}
373	}
374	sc->sc_provider->sectorsize = sectorsize;
375	/* We have sc->sc_disks[sc->sc_ndisks - 1].d_end in 'start'. */
376	sc->sc_provider->mediasize = start;
377	g_error_provider(sc->sc_provider, 0);
378
379	G_CONCAT_DEBUG(0, "Device %s activated.", sc->sc_name);
380}
381
382static int
383g_concat_read_metadata(struct g_consumer *cp, struct g_concat_metadata *md)
384{
385	struct g_provider *pp;
386	u_char *buf;
387	int error;
388
389	g_topology_assert();
390
391	error = g_access(cp, 1, 0, 0);
392	if (error != 0)
393		return (error);
394	pp = cp->provider;
395	g_topology_unlock();
396	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
397	    &error);
398	g_topology_lock();
399	g_access(cp, -1, 0, 0);
400	if (buf == NULL)
401		return (error);
402
403	/* Decode metadata. */
404	concat_metadata_decode(buf, md);
405	g_free(buf);
406
407	return (0);
408}
409
410/*
411 * Add disk to given device.
412 */
413static int
414g_concat_add_disk(struct g_concat_softc *sc, struct g_provider *pp, u_int no)
415{
416	struct g_concat_disk *disk;
417	struct g_consumer *cp, *fcp;
418	struct g_geom *gp;
419	int error;
420
421	/* Metadata corrupted? */
422	if (no >= sc->sc_ndisks)
423		return (EINVAL);
424
425	disk = &sc->sc_disks[no];
426	/* Check if disk is not already attached. */
427	if (disk->d_consumer != NULL)
428		return (EEXIST);
429
430	gp = sc->sc_geom;
431	fcp = LIST_FIRST(&gp->consumer);
432
433	cp = g_new_consumer(gp);
434	error = g_attach(cp, pp);
435	if (error != 0) {
436		g_destroy_consumer(cp);
437		return (error);
438	}
439
440	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
441		error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
442		if (error != 0) {
443			g_detach(cp);
444			g_destroy_consumer(cp);
445			return (error);
446		}
447	}
448	if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) {
449		struct g_concat_metadata md;
450
451		/* Re-read metadata. */
452		error = g_concat_read_metadata(cp, &md);
453		if (error != 0)
454			goto fail;
455
456		if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0 ||
457		    strcmp(md.md_name, sc->sc_name) != 0 ||
458		    md.md_id != sc->sc_id) {
459			G_CONCAT_DEBUG(0, "Metadata on %s changed.", pp->name);
460			goto fail;
461		}
462	}
463
464	cp->private = disk;
465	disk->d_consumer = cp;
466	disk->d_softc = sc;
467	disk->d_start = 0;	/* not yet */
468	disk->d_end = 0;	/* not yet */
469
470	G_CONCAT_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
471
472	g_concat_check_and_run(sc);
473
474	return (0);
475fail:
476	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
477		g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
478	g_detach(cp);
479	g_destroy_consumer(cp);
480	return (error);
481}
482
483static struct g_geom *
484g_concat_create(struct g_class *mp, const struct g_concat_metadata *md,
485    u_int type)
486{
487	struct g_concat_softc *sc;
488	struct g_geom *gp;
489	u_int no;
490
491	G_CONCAT_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
492	    md->md_id);
493
494	/* One disks is minimum. */
495	if (md->md_all < 1)
496		return (NULL);
497
498	/* Check for duplicate unit */
499	LIST_FOREACH(gp, &mp->geom, geom) {
500		sc = gp->softc;
501		if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
502			G_CONCAT_DEBUG(0, "Device %s already configured.",
503			    gp->name);
504			return (NULL);
505		}
506	}
507	gp = g_new_geomf(mp, "%s", md->md_name);
508	gp->softc = NULL;	/* for a moment */
509
510	sc = malloc(sizeof(*sc), M_CONCAT, M_WAITOK | M_ZERO);
511	gp->start = g_concat_start;
512	gp->spoiled = g_concat_orphan;
513	gp->orphan = g_concat_orphan;
514	gp->access = g_concat_access;
515	gp->dumpconf = g_concat_dumpconf;
516
517	sc->sc_id = md->md_id;
518	sc->sc_ndisks = md->md_all;
519	sc->sc_disks = malloc(sizeof(struct g_concat_disk) * sc->sc_ndisks,
520	    M_CONCAT, M_WAITOK | M_ZERO);
521	for (no = 0; no < sc->sc_ndisks; no++)
522		sc->sc_disks[no].d_consumer = NULL;
523	sc->sc_type = type;
524
525	gp->softc = sc;
526	sc->sc_geom = gp;
527	sc->sc_provider = NULL;
528
529	G_CONCAT_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
530
531	return (gp);
532}
533
534static int
535g_concat_destroy(struct g_concat_softc *sc, boolean_t force)
536{
537	struct g_provider *pp;
538	struct g_geom *gp;
539	u_int no;
540
541	g_topology_assert();
542
543	if (sc == NULL)
544		return (ENXIO);
545
546	pp = sc->sc_provider;
547	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
548		if (force) {
549			G_CONCAT_DEBUG(0, "Device %s is still open, so it "
550			    "can't be definitely removed.", pp->name);
551		} else {
552			G_CONCAT_DEBUG(1,
553			    "Device %s is still open (r%dw%de%d).", pp->name,
554			    pp->acr, pp->acw, pp->ace);
555			return (EBUSY);
556		}
557	}
558
559	for (no = 0; no < sc->sc_ndisks; no++) {
560		if (sc->sc_disks[no].d_consumer != NULL)
561			g_concat_remove_disk(&sc->sc_disks[no]);
562	}
563
564	gp = sc->sc_geom;
565	gp->softc = NULL;
566	KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
567	    gp->name));
568	free(sc->sc_disks, M_CONCAT);
569	free(sc, M_CONCAT);
570
571	pp = LIST_FIRST(&gp->provider);
572	if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
573		G_CONCAT_DEBUG(0, "Device %s destroyed.", gp->name);
574
575	g_wither_geom(gp, ENXIO);
576
577	return (0);
578}
579
580static int
581g_concat_destroy_geom(struct gctl_req *req __unused,
582    struct g_class *mp __unused, struct g_geom *gp)
583{
584	struct g_concat_softc *sc;
585
586	sc = gp->softc;
587	return (g_concat_destroy(sc, 0));
588}
589
590static struct g_geom *
591g_concat_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
592{
593	struct g_concat_metadata md;
594	struct g_concat_softc *sc;
595	struct g_consumer *cp;
596	struct g_geom *gp;
597	int error;
598
599	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
600	g_topology_assert();
601
602	/* Skip providers that are already open for writing. */
603	if (pp->acw > 0)
604		return (NULL);
605
606	G_CONCAT_DEBUG(3, "Tasting %s.", pp->name);
607
608	gp = g_new_geomf(mp, "concat:taste");
609	gp->start = g_concat_start;
610	gp->access = g_concat_access;
611	gp->orphan = g_concat_orphan;
612	cp = g_new_consumer(gp);
613	g_attach(cp, pp);
614	error = g_concat_read_metadata(cp, &md);
615	g_detach(cp);
616	g_destroy_consumer(cp);
617	g_destroy_geom(gp);
618	if (error != 0)
619		return (NULL);
620	gp = NULL;
621
622	if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0)
623		return (NULL);
624	if (md.md_version > G_CONCAT_VERSION) {
625		printf("geom_concat.ko module is too old to handle %s.\n",
626		    pp->name);
627		return (NULL);
628	}
629	/*
630	 * Backward compatibility:
631	 */
632	/* There was no md_provider field in earlier versions of metadata. */
633	if (md.md_version < 3)
634		bzero(md.md_provider, sizeof(md.md_provider));
635	/* There was no md_provsize field in earlier versions of metadata. */
636	if (md.md_version < 4)
637		md.md_provsize = pp->mediasize;
638
639	if (md.md_provider[0] != '\0' && strcmp(md.md_provider, pp->name) != 0)
640		return (NULL);
641	if (md.md_provsize != pp->mediasize)
642		return (NULL);
643
644	/*
645	 * Let's check if device already exists.
646	 */
647	sc = NULL;
648	LIST_FOREACH(gp, &mp->geom, geom) {
649		sc = gp->softc;
650		if (sc == NULL)
651			continue;
652		if (sc->sc_type != G_CONCAT_TYPE_AUTOMATIC)
653			continue;
654		if (strcmp(md.md_name, sc->sc_name) != 0)
655			continue;
656		if (md.md_id != sc->sc_id)
657			continue;
658		break;
659	}
660	if (gp != NULL) {
661		G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
662		error = g_concat_add_disk(sc, pp, md.md_no);
663		if (error != 0) {
664			G_CONCAT_DEBUG(0,
665			    "Cannot add disk %s to %s (error=%d).", pp->name,
666			    gp->name, error);
667			return (NULL);
668		}
669	} else {
670		gp = g_concat_create(mp, &md, G_CONCAT_TYPE_AUTOMATIC);
671		if (gp == NULL) {
672			G_CONCAT_DEBUG(0, "Cannot create device %s.",
673			    md.md_name);
674			return (NULL);
675		}
676		sc = gp->softc;
677		G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
678		error = g_concat_add_disk(sc, pp, md.md_no);
679		if (error != 0) {
680			G_CONCAT_DEBUG(0,
681			    "Cannot add disk %s to %s (error=%d).", pp->name,
682			    gp->name, error);
683			g_concat_destroy(sc, 1);
684			return (NULL);
685		}
686	}
687
688	return (gp);
689}
690
691static void
692g_concat_ctl_create(struct gctl_req *req, struct g_class *mp)
693{
694	u_int attached, no;
695	struct g_concat_metadata md;
696	struct g_provider *pp;
697	struct g_concat_softc *sc;
698	struct g_geom *gp;
699	struct sbuf *sb;
700	const char *name;
701	char param[16];
702	int *nargs;
703
704	g_topology_assert();
705	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
706	if (nargs == NULL) {
707		gctl_error(req, "No '%s' argument.", "nargs");
708		return;
709	}
710	if (*nargs < 2) {
711		gctl_error(req, "Too few arguments.");
712		return;
713	}
714
715	strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
716	md.md_version = G_CONCAT_VERSION;
717	name = gctl_get_asciiparam(req, "arg0");
718	if (name == NULL) {
719		gctl_error(req, "No 'arg%u' argument.", 0);
720		return;
721	}
722	strlcpy(md.md_name, name, sizeof(md.md_name));
723	md.md_id = arc4random();
724	md.md_no = 0;
725	md.md_all = *nargs - 1;
726	bzero(md.md_provider, sizeof(md.md_provider));
727	/* This field is not important here. */
728	md.md_provsize = 0;
729
730	/* Check all providers are valid */
731	for (no = 1; no < *nargs; no++) {
732		snprintf(param, sizeof(param), "arg%u", no);
733		name = gctl_get_asciiparam(req, param);
734		if (name == NULL) {
735			gctl_error(req, "No 'arg%u' argument.", no);
736			return;
737		}
738		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
739			name += strlen("/dev/");
740		pp = g_provider_by_name(name);
741		if (pp == NULL) {
742			G_CONCAT_DEBUG(1, "Disk %s is invalid.", name);
743			gctl_error(req, "Disk %s is invalid.", name);
744			return;
745		}
746	}
747
748	gp = g_concat_create(mp, &md, G_CONCAT_TYPE_MANUAL);
749	if (gp == NULL) {
750		gctl_error(req, "Can't configure %s.", md.md_name);
751		return;
752	}
753
754	sc = gp->softc;
755	sb = sbuf_new_auto();
756	sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
757	for (attached = 0, no = 1; no < *nargs; no++) {
758		snprintf(param, sizeof(param), "arg%u", no);
759		name = gctl_get_asciiparam(req, param);
760		if (name == NULL) {
761			gctl_error(req, "No 'arg%d' argument.", no);
762			return;
763		}
764		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
765			name += strlen("/dev/");
766		pp = g_provider_by_name(name);
767		KASSERT(pp != NULL, ("Provider %s disappear?!", name));
768		if (g_concat_add_disk(sc, pp, no - 1) != 0) {
769			G_CONCAT_DEBUG(1, "Disk %u (%s) not attached to %s.",
770			    no, pp->name, gp->name);
771			sbuf_printf(sb, " %s", pp->name);
772			continue;
773		}
774		attached++;
775	}
776	sbuf_finish(sb);
777	if (md.md_all != attached) {
778		g_concat_destroy(gp->softc, 1);
779		gctl_error(req, "%s", sbuf_data(sb));
780	}
781	sbuf_delete(sb);
782}
783
784static struct g_concat_softc *
785g_concat_find_device(struct g_class *mp, const char *name)
786{
787	struct g_concat_softc *sc;
788	struct g_geom *gp;
789
790	LIST_FOREACH(gp, &mp->geom, geom) {
791		sc = gp->softc;
792		if (sc == NULL)
793			continue;
794		if (strcmp(sc->sc_name, name) == 0)
795			return (sc);
796	}
797	return (NULL);
798}
799
800static void
801g_concat_ctl_destroy(struct gctl_req *req, struct g_class *mp)
802{
803	struct g_concat_softc *sc;
804	int *force, *nargs, error;
805	const char *name;
806	char param[16];
807	u_int i;
808
809	g_topology_assert();
810
811	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
812	if (nargs == NULL) {
813		gctl_error(req, "No '%s' argument.", "nargs");
814		return;
815	}
816	if (*nargs <= 0) {
817		gctl_error(req, "Missing device(s).");
818		return;
819	}
820	force = gctl_get_paraml(req, "force", sizeof(*force));
821	if (force == NULL) {
822		gctl_error(req, "No '%s' argument.", "force");
823		return;
824	}
825
826	for (i = 0; i < (u_int)*nargs; i++) {
827		snprintf(param, sizeof(param), "arg%u", i);
828		name = gctl_get_asciiparam(req, param);
829		if (name == NULL) {
830			gctl_error(req, "No 'arg%u' argument.", i);
831			return;
832		}
833		sc = g_concat_find_device(mp, name);
834		if (sc == NULL) {
835			gctl_error(req, "No such device: %s.", name);
836			return;
837		}
838		error = g_concat_destroy(sc, *force);
839		if (error != 0) {
840			gctl_error(req, "Cannot destroy device %s (error=%d).",
841			    sc->sc_name, error);
842			return;
843		}
844	}
845}
846
847static void
848g_concat_config(struct gctl_req *req, struct g_class *mp, const char *verb)
849{
850	uint32_t *version;
851
852	g_topology_assert();
853
854	version = gctl_get_paraml(req, "version", sizeof(*version));
855	if (version == NULL) {
856		gctl_error(req, "No '%s' argument.", "version");
857		return;
858	}
859	if (*version != G_CONCAT_VERSION) {
860		gctl_error(req, "Userland and kernel parts are out of sync.");
861		return;
862	}
863
864	if (strcmp(verb, "create") == 0) {
865		g_concat_ctl_create(req, mp);
866		return;
867	} else if (strcmp(verb, "destroy") == 0 ||
868	    strcmp(verb, "stop") == 0) {
869		g_concat_ctl_destroy(req, mp);
870		return;
871	}
872	gctl_error(req, "Unknown verb.");
873}
874
875static void
876g_concat_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
877    struct g_consumer *cp, struct g_provider *pp)
878{
879	struct g_concat_softc *sc;
880
881	g_topology_assert();
882	sc = gp->softc;
883	if (sc == NULL)
884		return;
885	if (pp != NULL) {
886		/* Nothing here. */
887	} else if (cp != NULL) {
888		struct g_concat_disk *disk;
889
890		disk = cp->private;
891		if (disk == NULL)
892			return;
893		sbuf_printf(sb, "%s<End>%jd</End>\n", indent,
894		    (intmax_t)disk->d_end);
895		sbuf_printf(sb, "%s<Start>%jd</Start>\n", indent,
896		    (intmax_t)disk->d_start);
897	} else {
898		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
899		sbuf_printf(sb, "%s<Type>", indent);
900		switch (sc->sc_type) {
901		case G_CONCAT_TYPE_AUTOMATIC:
902			sbuf_printf(sb, "AUTOMATIC");
903			break;
904		case G_CONCAT_TYPE_MANUAL:
905			sbuf_printf(sb, "MANUAL");
906			break;
907		default:
908			sbuf_printf(sb, "UNKNOWN");
909			break;
910		}
911		sbuf_printf(sb, "</Type>\n");
912		sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
913		    indent, sc->sc_ndisks, g_concat_nvalid(sc));
914		sbuf_printf(sb, "%s<State>", indent);
915		if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
916			sbuf_printf(sb, "UP");
917		else
918			sbuf_printf(sb, "DOWN");
919		sbuf_printf(sb, "</State>\n");
920	}
921}
922
923DECLARE_GEOM_CLASS(g_concat_class, g_concat);
924