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