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