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$");
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_done(struct bio *bp)
243{
244	struct g_concat_softc *sc;
245	struct bio *pbp;
246
247	pbp = bp->bio_parent;
248	sc = pbp->bio_to->geom->softc;
249	mtx_lock(&sc->sc_lock);
250	if (pbp->bio_error == 0)
251		pbp->bio_error = bp->bio_error;
252	pbp->bio_completed += bp->bio_completed;
253	pbp->bio_inbed++;
254	if (pbp->bio_children == pbp->bio_inbed) {
255		mtx_unlock(&sc->sc_lock);
256		g_io_deliver(pbp, pbp->bio_error);
257	} else
258		mtx_unlock(&sc->sc_lock);
259	g_destroy_bio(bp);
260}
261
262static void
263g_concat_flush(struct g_concat_softc *sc, struct bio *bp)
264{
265	struct bio_queue_head queue;
266	struct g_consumer *cp;
267	struct bio *cbp;
268	u_int no;
269
270	bioq_init(&queue);
271	for (no = 0; no < sc->sc_ndisks; no++) {
272		cbp = g_clone_bio(bp);
273		if (cbp == NULL) {
274			while ((cbp = bioq_takefirst(&queue)) != NULL)
275				g_destroy_bio(cbp);
276			if (bp->bio_error == 0)
277				bp->bio_error = ENOMEM;
278			g_io_deliver(bp, bp->bio_error);
279			return;
280		}
281		bioq_insert_tail(&queue, cbp);
282		cbp->bio_done = g_concat_done;
283		cbp->bio_caller1 = sc->sc_disks[no].d_consumer;
284		cbp->bio_to = sc->sc_disks[no].d_consumer->provider;
285	}
286	while ((cbp = bioq_takefirst(&queue)) != NULL) {
287		G_CONCAT_LOGREQ(cbp, "Sending request.");
288		cp = cbp->bio_caller1;
289		cbp->bio_caller1 = NULL;
290		g_io_request(cbp, cp);
291	}
292}
293
294static void
295g_concat_start(struct bio *bp)
296{
297	struct bio_queue_head queue;
298	struct g_concat_softc *sc;
299	struct g_concat_disk *disk;
300	struct g_provider *pp;
301	off_t offset, end, length, off, len;
302	struct bio *cbp;
303	char *addr;
304	u_int no;
305
306	pp = bp->bio_to;
307	sc = pp->geom->softc;
308	/*
309	 * If sc == NULL, provider's error should be set and g_concat_start()
310	 * should not be called at all.
311	 */
312	KASSERT(sc != NULL,
313	    ("Provider's error should be set (error=%d)(device=%s).",
314	    bp->bio_to->error, bp->bio_to->name));
315
316	G_CONCAT_LOGREQ(bp, "Request received.");
317
318	switch (bp->bio_cmd) {
319	case BIO_READ:
320	case BIO_WRITE:
321	case BIO_DELETE:
322		break;
323	case BIO_FLUSH:
324		g_concat_flush(sc, bp);
325		return;
326	case BIO_GETATTR:
327		if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) {
328			g_concat_kernel_dump(bp);
329			return;
330		}
331		/* To which provider it should be delivered? */
332		/* FALLTHROUGH */
333	default:
334		g_io_deliver(bp, EOPNOTSUPP);
335		return;
336	}
337
338	offset = bp->bio_offset;
339	length = bp->bio_length;
340	if ((bp->bio_flags & BIO_UNMAPPED) != 0)
341		addr = NULL;
342	else
343		addr = bp->bio_data;
344	end = offset + length;
345
346	bioq_init(&queue);
347	for (no = 0; no < sc->sc_ndisks; no++) {
348		disk = &sc->sc_disks[no];
349		if (disk->d_end <= offset)
350			continue;
351		if (disk->d_start >= end)
352			break;
353
354		off = offset - disk->d_start;
355		len = MIN(length, disk->d_end - offset);
356		length -= len;
357		offset += len;
358
359		cbp = g_clone_bio(bp);
360		if (cbp == NULL) {
361			while ((cbp = bioq_takefirst(&queue)) != NULL)
362				g_destroy_bio(cbp);
363			if (bp->bio_error == 0)
364				bp->bio_error = ENOMEM;
365			g_io_deliver(bp, bp->bio_error);
366			return;
367		}
368		bioq_insert_tail(&queue, cbp);
369		/*
370		 * Fill in the component buf structure.
371		 */
372		if (len == bp->bio_length)
373			cbp->bio_done = g_std_done;
374		else
375			cbp->bio_done = g_concat_done;
376		cbp->bio_offset = off;
377		cbp->bio_length = len;
378		if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
379			cbp->bio_ma_offset += (uintptr_t)addr;
380			cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE;
381			cbp->bio_ma_offset %= PAGE_SIZE;
382			cbp->bio_ma_n = round_page(cbp->bio_ma_offset +
383			    cbp->bio_length) / PAGE_SIZE;
384		} else
385			cbp->bio_data = addr;
386		addr += len;
387		cbp->bio_to = disk->d_consumer->provider;
388		cbp->bio_caller1 = disk;
389
390		if (length == 0)
391			break;
392	}
393	KASSERT(length == 0,
394	    ("Length is still greater than 0 (class=%s, name=%s).",
395	    bp->bio_to->geom->class->name, bp->bio_to->geom->name));
396	while ((cbp = bioq_takefirst(&queue)) != NULL) {
397		G_CONCAT_LOGREQ(cbp, "Sending request.");
398		disk = cbp->bio_caller1;
399		cbp->bio_caller1 = NULL;
400		g_io_request(cbp, disk->d_consumer);
401	}
402}
403
404static void
405g_concat_check_and_run(struct g_concat_softc *sc)
406{
407	struct g_concat_disk *disk;
408	struct g_provider *dp, *pp;
409	u_int no, sectorsize = 0;
410	off_t start;
411
412	g_topology_assert();
413	if (g_concat_nvalid(sc) != sc->sc_ndisks)
414		return;
415
416	pp = g_new_providerf(sc->sc_geom, "concat/%s", sc->sc_name);
417	pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE |
418	    G_PF_ACCEPT_UNMAPPED;
419	start = 0;
420	for (no = 0; no < sc->sc_ndisks; no++) {
421		disk = &sc->sc_disks[no];
422		dp = disk->d_consumer->provider;
423		disk->d_start = start;
424		disk->d_end = disk->d_start + dp->mediasize;
425		if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC)
426			disk->d_end -= dp->sectorsize;
427		start = disk->d_end;
428		if (no == 0)
429			sectorsize = dp->sectorsize;
430		else
431			sectorsize = lcm(sectorsize, dp->sectorsize);
432
433		/* A provider underneath us doesn't support unmapped */
434		if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) {
435			G_CONCAT_DEBUG(1, "Cancelling unmapped "
436			    "because of %s.", dp->name);
437			pp->flags &= ~G_PF_ACCEPT_UNMAPPED;
438		}
439	}
440	pp->sectorsize = sectorsize;
441	/* We have sc->sc_disks[sc->sc_ndisks - 1].d_end in 'start'. */
442	pp->mediasize = start;
443	pp->stripesize = sc->sc_disks[0].d_consumer->provider->stripesize;
444	pp->stripeoffset = sc->sc_disks[0].d_consumer->provider->stripeoffset;
445	sc->sc_provider = pp;
446	g_error_provider(pp, 0);
447
448	G_CONCAT_DEBUG(0, "Device %s activated.", sc->sc_provider->name);
449}
450
451static int
452g_concat_read_metadata(struct g_consumer *cp, struct g_concat_metadata *md)
453{
454	struct g_provider *pp;
455	u_char *buf;
456	int error;
457
458	g_topology_assert();
459
460	error = g_access(cp, 1, 0, 0);
461	if (error != 0)
462		return (error);
463	pp = cp->provider;
464	g_topology_unlock();
465	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
466	    &error);
467	g_topology_lock();
468	g_access(cp, -1, 0, 0);
469	if (buf == NULL)
470		return (error);
471
472	/* Decode metadata. */
473	concat_metadata_decode(buf, md);
474	g_free(buf);
475
476	return (0);
477}
478
479/*
480 * Add disk to given device.
481 */
482static int
483g_concat_add_disk(struct g_concat_softc *sc, struct g_provider *pp, u_int no)
484{
485	struct g_concat_disk *disk;
486	struct g_consumer *cp, *fcp;
487	struct g_geom *gp;
488	int error;
489
490	g_topology_assert();
491	/* Metadata corrupted? */
492	if (no >= sc->sc_ndisks)
493		return (EINVAL);
494
495	disk = &sc->sc_disks[no];
496	/* Check if disk is not already attached. */
497	if (disk->d_consumer != NULL)
498		return (EEXIST);
499
500	gp = sc->sc_geom;
501	fcp = LIST_FIRST(&gp->consumer);
502
503	cp = g_new_consumer(gp);
504	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
505	error = g_attach(cp, pp);
506	if (error != 0) {
507		g_destroy_consumer(cp);
508		return (error);
509	}
510
511	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
512		error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
513		if (error != 0) {
514			g_detach(cp);
515			g_destroy_consumer(cp);
516			return (error);
517		}
518	}
519	if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) {
520		struct g_concat_metadata md;
521
522		/* Re-read metadata. */
523		error = g_concat_read_metadata(cp, &md);
524		if (error != 0)
525			goto fail;
526
527		if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0 ||
528		    strcmp(md.md_name, sc->sc_name) != 0 ||
529		    md.md_id != sc->sc_id) {
530			G_CONCAT_DEBUG(0, "Metadata on %s changed.", pp->name);
531			goto fail;
532		}
533	}
534
535	cp->private = disk;
536	disk->d_consumer = cp;
537	disk->d_softc = sc;
538	disk->d_start = 0;	/* not yet */
539	disk->d_end = 0;	/* not yet */
540	disk->d_removed = 0;
541
542	G_CONCAT_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
543
544	g_concat_check_and_run(sc);
545
546	return (0);
547fail:
548	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
549		g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
550	g_detach(cp);
551	g_destroy_consumer(cp);
552	return (error);
553}
554
555static struct g_geom *
556g_concat_create(struct g_class *mp, const struct g_concat_metadata *md,
557    u_int type)
558{
559	struct g_concat_softc *sc;
560	struct g_geom *gp;
561	u_int no;
562
563	G_CONCAT_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
564	    md->md_id);
565
566	/* One disks is minimum. */
567	if (md->md_all < 1)
568		return (NULL);
569
570	/* Check for duplicate unit */
571	LIST_FOREACH(gp, &mp->geom, geom) {
572		sc = gp->softc;
573		if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
574			G_CONCAT_DEBUG(0, "Device %s already configured.",
575			    gp->name);
576			return (NULL);
577		}
578	}
579	gp = g_new_geomf(mp, "%s", md->md_name);
580	sc = malloc(sizeof(*sc), M_CONCAT, M_WAITOK | M_ZERO);
581	gp->start = g_concat_start;
582	gp->spoiled = g_concat_orphan;
583	gp->orphan = g_concat_orphan;
584	gp->access = g_concat_access;
585	gp->dumpconf = g_concat_dumpconf;
586
587	sc->sc_id = md->md_id;
588	sc->sc_ndisks = md->md_all;
589	sc->sc_disks = malloc(sizeof(struct g_concat_disk) * sc->sc_ndisks,
590	    M_CONCAT, M_WAITOK | M_ZERO);
591	for (no = 0; no < sc->sc_ndisks; no++)
592		sc->sc_disks[no].d_consumer = NULL;
593	sc->sc_type = type;
594	mtx_init(&sc->sc_lock, "gconcat lock", NULL, MTX_DEF);
595
596	gp->softc = sc;
597	sc->sc_geom = gp;
598	sc->sc_provider = NULL;
599
600	G_CONCAT_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
601
602	return (gp);
603}
604
605static int
606g_concat_destroy(struct g_concat_softc *sc, boolean_t force)
607{
608	struct g_provider *pp;
609	struct g_consumer *cp, *cp1;
610	struct g_geom *gp;
611
612	g_topology_assert();
613
614	if (sc == NULL)
615		return (ENXIO);
616
617	pp = sc->sc_provider;
618	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
619		if (force) {
620			G_CONCAT_DEBUG(0, "Device %s is still open, so it "
621			    "can't be definitely removed.", pp->name);
622		} else {
623			G_CONCAT_DEBUG(1,
624			    "Device %s is still open (r%dw%de%d).", pp->name,
625			    pp->acr, pp->acw, pp->ace);
626			return (EBUSY);
627		}
628	}
629
630	gp = sc->sc_geom;
631	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
632		g_concat_remove_disk(cp->private);
633		if (cp1 == NULL)
634			return (0);	/* Recursion happened. */
635	}
636	if (!LIST_EMPTY(&gp->consumer))
637		return (EINPROGRESS);
638
639	gp->softc = NULL;
640	KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
641	    gp->name));
642	free(sc->sc_disks, M_CONCAT);
643	mtx_destroy(&sc->sc_lock);
644	free(sc, M_CONCAT);
645
646	G_CONCAT_DEBUG(0, "Device %s destroyed.", gp->name);
647	g_wither_geom(gp, ENXIO);
648	return (0);
649}
650
651static int
652g_concat_destroy_geom(struct gctl_req *req __unused,
653    struct g_class *mp __unused, struct g_geom *gp)
654{
655	struct g_concat_softc *sc;
656
657	sc = gp->softc;
658	return (g_concat_destroy(sc, 0));
659}
660
661static struct g_geom *
662g_concat_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
663{
664	struct g_concat_metadata md;
665	struct g_concat_softc *sc;
666	struct g_consumer *cp;
667	struct g_geom *gp;
668	int error;
669
670	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
671	g_topology_assert();
672
673	/* Skip providers that are already open for writing. */
674	if (pp->acw > 0)
675		return (NULL);
676
677	G_CONCAT_DEBUG(3, "Tasting %s.", pp->name);
678
679	gp = g_new_geomf(mp, "concat:taste");
680	gp->start = g_concat_start;
681	gp->access = g_concat_access;
682	gp->orphan = g_concat_orphan;
683	cp = g_new_consumer(gp);
684	g_attach(cp, pp);
685	error = g_concat_read_metadata(cp, &md);
686	g_detach(cp);
687	g_destroy_consumer(cp);
688	g_destroy_geom(gp);
689	if (error != 0)
690		return (NULL);
691	gp = NULL;
692
693	if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0)
694		return (NULL);
695	if (md.md_version > G_CONCAT_VERSION) {
696		printf("geom_concat.ko module is too old to handle %s.\n",
697		    pp->name);
698		return (NULL);
699	}
700	/*
701	 * Backward compatibility:
702	 */
703	/* There was no md_provider field in earlier versions of metadata. */
704	if (md.md_version < 3)
705		bzero(md.md_provider, sizeof(md.md_provider));
706	/* There was no md_provsize field in earlier versions of metadata. */
707	if (md.md_version < 4)
708		md.md_provsize = pp->mediasize;
709
710	if (md.md_provider[0] != '\0' &&
711	    !g_compare_names(md.md_provider, pp->name))
712		return (NULL);
713	if (md.md_provsize != pp->mediasize)
714		return (NULL);
715
716	/*
717	 * Let's check if device already exists.
718	 */
719	sc = NULL;
720	LIST_FOREACH(gp, &mp->geom, geom) {
721		sc = gp->softc;
722		if (sc == NULL)
723			continue;
724		if (sc->sc_type != G_CONCAT_TYPE_AUTOMATIC)
725			continue;
726		if (strcmp(md.md_name, sc->sc_name) != 0)
727			continue;
728		if (md.md_id != sc->sc_id)
729			continue;
730		break;
731	}
732	if (gp != NULL) {
733		G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
734		error = g_concat_add_disk(sc, pp, md.md_no);
735		if (error != 0) {
736			G_CONCAT_DEBUG(0,
737			    "Cannot add disk %s to %s (error=%d).", pp->name,
738			    gp->name, error);
739			return (NULL);
740		}
741	} else {
742		gp = g_concat_create(mp, &md, G_CONCAT_TYPE_AUTOMATIC);
743		if (gp == NULL) {
744			G_CONCAT_DEBUG(0, "Cannot create device %s.",
745			    md.md_name);
746			return (NULL);
747		}
748		sc = gp->softc;
749		G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
750		error = g_concat_add_disk(sc, pp, md.md_no);
751		if (error != 0) {
752			G_CONCAT_DEBUG(0,
753			    "Cannot add disk %s to %s (error=%d).", pp->name,
754			    gp->name, error);
755			g_concat_destroy(sc, 1);
756			return (NULL);
757		}
758	}
759
760	return (gp);
761}
762
763static void
764g_concat_ctl_create(struct gctl_req *req, struct g_class *mp)
765{
766	u_int attached, no;
767	struct g_concat_metadata md;
768	struct g_provider *pp;
769	struct g_concat_softc *sc;
770	struct g_geom *gp;
771	struct sbuf *sb;
772	const char *name;
773	char param[16];
774	int *nargs;
775
776	g_topology_assert();
777	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
778	if (nargs == NULL) {
779		gctl_error(req, "No '%s' argument.", "nargs");
780		return;
781	}
782	if (*nargs < 2) {
783		gctl_error(req, "Too few arguments.");
784		return;
785	}
786
787	strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
788	md.md_version = G_CONCAT_VERSION;
789	name = gctl_get_asciiparam(req, "arg0");
790	if (name == NULL) {
791		gctl_error(req, "No 'arg%u' argument.", 0);
792		return;
793	}
794	strlcpy(md.md_name, name, sizeof(md.md_name));
795	md.md_id = arc4random();
796	md.md_no = 0;
797	md.md_all = *nargs - 1;
798	bzero(md.md_provider, sizeof(md.md_provider));
799	/* This field is not important here. */
800	md.md_provsize = 0;
801
802	/* Check all providers are valid */
803	for (no = 1; no < *nargs; no++) {
804		snprintf(param, sizeof(param), "arg%u", no);
805		name = gctl_get_asciiparam(req, param);
806		if (name == NULL) {
807			gctl_error(req, "No 'arg%u' argument.", no);
808			return;
809		}
810		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
811			name += strlen("/dev/");
812		pp = g_provider_by_name(name);
813		if (pp == NULL) {
814			G_CONCAT_DEBUG(1, "Disk %s is invalid.", name);
815			gctl_error(req, "Disk %s is invalid.", name);
816			return;
817		}
818	}
819
820	gp = g_concat_create(mp, &md, G_CONCAT_TYPE_MANUAL);
821	if (gp == NULL) {
822		gctl_error(req, "Can't configure %s.", md.md_name);
823		return;
824	}
825
826	sc = gp->softc;
827	sb = sbuf_new_auto();
828	sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
829	for (attached = 0, no = 1; no < *nargs; no++) {
830		snprintf(param, sizeof(param), "arg%u", no);
831		name = gctl_get_asciiparam(req, param);
832		if (name == NULL) {
833			gctl_error(req, "No 'arg%d' argument.", no);
834			return;
835		}
836		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
837			name += strlen("/dev/");
838		pp = g_provider_by_name(name);
839		KASSERT(pp != NULL, ("Provider %s disappear?!", name));
840		if (g_concat_add_disk(sc, pp, no - 1) != 0) {
841			G_CONCAT_DEBUG(1, "Disk %u (%s) not attached to %s.",
842			    no, pp->name, gp->name);
843			sbuf_printf(sb, " %s", pp->name);
844			continue;
845		}
846		attached++;
847	}
848	sbuf_finish(sb);
849	if (md.md_all != attached) {
850		g_concat_destroy(gp->softc, 1);
851		gctl_error(req, "%s", sbuf_data(sb));
852	}
853	sbuf_delete(sb);
854}
855
856static struct g_concat_softc *
857g_concat_find_device(struct g_class *mp, const char *name)
858{
859	struct g_concat_softc *sc;
860	struct g_geom *gp;
861
862	LIST_FOREACH(gp, &mp->geom, geom) {
863		sc = gp->softc;
864		if (sc == NULL)
865			continue;
866		if (strcmp(sc->sc_name, name) == 0)
867			return (sc);
868	}
869	return (NULL);
870}
871
872static void
873g_concat_ctl_destroy(struct gctl_req *req, struct g_class *mp)
874{
875	struct g_concat_softc *sc;
876	int *force, *nargs, error;
877	const char *name;
878	char param[16];
879	u_int i;
880
881	g_topology_assert();
882
883	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
884	if (nargs == NULL) {
885		gctl_error(req, "No '%s' argument.", "nargs");
886		return;
887	}
888	if (*nargs <= 0) {
889		gctl_error(req, "Missing device(s).");
890		return;
891	}
892	force = gctl_get_paraml(req, "force", sizeof(*force));
893	if (force == NULL) {
894		gctl_error(req, "No '%s' argument.", "force");
895		return;
896	}
897
898	for (i = 0; i < (u_int)*nargs; i++) {
899		snprintf(param, sizeof(param), "arg%u", i);
900		name = gctl_get_asciiparam(req, param);
901		if (name == NULL) {
902			gctl_error(req, "No 'arg%u' argument.", i);
903			return;
904		}
905		sc = g_concat_find_device(mp, name);
906		if (sc == NULL) {
907			gctl_error(req, "No such device: %s.", name);
908			return;
909		}
910		error = g_concat_destroy(sc, *force);
911		if (error != 0) {
912			gctl_error(req, "Cannot destroy device %s (error=%d).",
913			    sc->sc_name, error);
914			return;
915		}
916	}
917}
918
919static void
920g_concat_config(struct gctl_req *req, struct g_class *mp, const char *verb)
921{
922	uint32_t *version;
923
924	g_topology_assert();
925
926	version = gctl_get_paraml(req, "version", sizeof(*version));
927	if (version == NULL) {
928		gctl_error(req, "No '%s' argument.", "version");
929		return;
930	}
931	if (*version != G_CONCAT_VERSION) {
932		gctl_error(req, "Userland and kernel parts are out of sync.");
933		return;
934	}
935
936	if (strcmp(verb, "create") == 0) {
937		g_concat_ctl_create(req, mp);
938		return;
939	} else if (strcmp(verb, "destroy") == 0 ||
940	    strcmp(verb, "stop") == 0) {
941		g_concat_ctl_destroy(req, mp);
942		return;
943	}
944	gctl_error(req, "Unknown verb.");
945}
946
947static void
948g_concat_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
949    struct g_consumer *cp, struct g_provider *pp)
950{
951	struct g_concat_softc *sc;
952
953	g_topology_assert();
954	sc = gp->softc;
955	if (sc == NULL)
956		return;
957	if (pp != NULL) {
958		/* Nothing here. */
959	} else if (cp != NULL) {
960		struct g_concat_disk *disk;
961
962		disk = cp->private;
963		if (disk == NULL)
964			return;
965		sbuf_printf(sb, "%s<End>%jd</End>\n", indent,
966		    (intmax_t)disk->d_end);
967		sbuf_printf(sb, "%s<Start>%jd</Start>\n", indent,
968		    (intmax_t)disk->d_start);
969	} else {
970		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
971		sbuf_printf(sb, "%s<Type>", indent);
972		switch (sc->sc_type) {
973		case G_CONCAT_TYPE_AUTOMATIC:
974			sbuf_printf(sb, "AUTOMATIC");
975			break;
976		case G_CONCAT_TYPE_MANUAL:
977			sbuf_printf(sb, "MANUAL");
978			break;
979		default:
980			sbuf_printf(sb, "UNKNOWN");
981			break;
982		}
983		sbuf_printf(sb, "</Type>\n");
984		sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
985		    indent, sc->sc_ndisks, g_concat_nvalid(sc));
986		sbuf_printf(sb, "%s<State>", indent);
987		if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
988			sbuf_printf(sb, "UP");
989		else
990			sbuf_printf(sb, "DOWN");
991		sbuf_printf(sb, "</State>\n");
992	}
993}
994
995DECLARE_GEOM_CLASS(g_concat_class, g_concat);
996