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