xref: /freebsd/sys/geom/concat/g_concat.c (revision aa0a1e58)
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/sysctl.h>
38 #include <sys/malloc.h>
39 #include <geom/geom.h>
40 #include <geom/concat/g_concat.h>
41 
42 FEATURE(geom_concat, "GEOM concatenation support");
43 
44 static MALLOC_DEFINE(M_CONCAT, "concat_data", "GEOM_CONCAT Data");
45 
46 SYSCTL_DECL(_kern_geom);
47 SYSCTL_NODE(_kern_geom, OID_AUTO, concat, CTLFLAG_RW, 0, "GEOM_CONCAT stuff");
48 static u_int g_concat_debug = 0;
49 TUNABLE_INT("kern.geom.concat.debug", &g_concat_debug);
50 SYSCTL_UINT(_kern_geom_concat, OID_AUTO, debug, CTLFLAG_RW, &g_concat_debug, 0,
51     "Debug level");
52 
53 static int g_concat_destroy(struct g_concat_softc *sc, boolean_t force);
54 static int g_concat_destroy_geom(struct gctl_req *req, struct g_class *mp,
55     struct g_geom *gp);
56 
57 static g_taste_t g_concat_taste;
58 static g_ctl_req_t g_concat_config;
59 static g_dumpconf_t g_concat_dumpconf;
60 
61 struct 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  */
73 static u_int
74 gcd(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  */
89 static u_int
90 lcm(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  */
99 static u_int
100 g_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 
113 static void
114 g_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 
140 static void
141 g_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 
163 static int
164 g_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 
215 static void
216 g_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 
248 static void
249 g_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 
284 static void
285 g_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 
385 static void
386 g_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 
424 static int
425 g_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  */
455 static int
456 g_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);
517 fail:
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 
525 static struct g_geom *
526 g_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 	gp->softc = NULL;	/* for a moment */
551 
552 	sc = malloc(sizeof(*sc), M_CONCAT, M_WAITOK | M_ZERO);
553 	gp->start = g_concat_start;
554 	gp->spoiled = g_concat_orphan;
555 	gp->orphan = g_concat_orphan;
556 	gp->access = g_concat_access;
557 	gp->dumpconf = g_concat_dumpconf;
558 
559 	sc->sc_id = md->md_id;
560 	sc->sc_ndisks = md->md_all;
561 	sc->sc_disks = malloc(sizeof(struct g_concat_disk) * sc->sc_ndisks,
562 	    M_CONCAT, M_WAITOK | M_ZERO);
563 	for (no = 0; no < sc->sc_ndisks; no++)
564 		sc->sc_disks[no].d_consumer = NULL;
565 	sc->sc_type = type;
566 
567 	gp->softc = sc;
568 	sc->sc_geom = gp;
569 	sc->sc_provider = NULL;
570 
571 	G_CONCAT_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
572 
573 	return (gp);
574 }
575 
576 static int
577 g_concat_destroy(struct g_concat_softc *sc, boolean_t force)
578 {
579 	struct g_provider *pp;
580 	struct g_geom *gp;
581 	u_int no;
582 
583 	g_topology_assert();
584 
585 	if (sc == NULL)
586 		return (ENXIO);
587 
588 	pp = sc->sc_provider;
589 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
590 		if (force) {
591 			G_CONCAT_DEBUG(0, "Device %s is still open, so it "
592 			    "can't be definitely removed.", pp->name);
593 		} else {
594 			G_CONCAT_DEBUG(1,
595 			    "Device %s is still open (r%dw%de%d).", pp->name,
596 			    pp->acr, pp->acw, pp->ace);
597 			return (EBUSY);
598 		}
599 	}
600 
601 	for (no = 0; no < sc->sc_ndisks; no++) {
602 		if (sc->sc_disks[no].d_consumer != NULL)
603 			g_concat_remove_disk(&sc->sc_disks[no]);
604 	}
605 
606 	gp = sc->sc_geom;
607 	gp->softc = NULL;
608 	KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
609 	    gp->name));
610 	free(sc->sc_disks, M_CONCAT);
611 	free(sc, M_CONCAT);
612 
613 	pp = LIST_FIRST(&gp->provider);
614 	if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
615 		G_CONCAT_DEBUG(0, "Device %s destroyed.", gp->name);
616 
617 	g_wither_geom(gp, ENXIO);
618 
619 	return (0);
620 }
621 
622 static int
623 g_concat_destroy_geom(struct gctl_req *req __unused,
624     struct g_class *mp __unused, struct g_geom *gp)
625 {
626 	struct g_concat_softc *sc;
627 
628 	sc = gp->softc;
629 	return (g_concat_destroy(sc, 0));
630 }
631 
632 static struct g_geom *
633 g_concat_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
634 {
635 	struct g_concat_metadata md;
636 	struct g_concat_softc *sc;
637 	struct g_consumer *cp;
638 	struct g_geom *gp;
639 	int error;
640 
641 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
642 	g_topology_assert();
643 
644 	/* Skip providers that are already open for writing. */
645 	if (pp->acw > 0)
646 		return (NULL);
647 
648 	G_CONCAT_DEBUG(3, "Tasting %s.", pp->name);
649 
650 	gp = g_new_geomf(mp, "concat:taste");
651 	gp->start = g_concat_start;
652 	gp->access = g_concat_access;
653 	gp->orphan = g_concat_orphan;
654 	cp = g_new_consumer(gp);
655 	g_attach(cp, pp);
656 	error = g_concat_read_metadata(cp, &md);
657 	g_detach(cp);
658 	g_destroy_consumer(cp);
659 	g_destroy_geom(gp);
660 	if (error != 0)
661 		return (NULL);
662 	gp = NULL;
663 
664 	if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0)
665 		return (NULL);
666 	if (md.md_version > G_CONCAT_VERSION) {
667 		printf("geom_concat.ko module is too old to handle %s.\n",
668 		    pp->name);
669 		return (NULL);
670 	}
671 	/*
672 	 * Backward compatibility:
673 	 */
674 	/* There was no md_provider field in earlier versions of metadata. */
675 	if (md.md_version < 3)
676 		bzero(md.md_provider, sizeof(md.md_provider));
677 	/* There was no md_provsize field in earlier versions of metadata. */
678 	if (md.md_version < 4)
679 		md.md_provsize = pp->mediasize;
680 
681 	if (md.md_provider[0] != '\0' && strcmp(md.md_provider, pp->name) != 0)
682 		return (NULL);
683 	if (md.md_provsize != pp->mediasize)
684 		return (NULL);
685 
686 	/*
687 	 * Let's check if device already exists.
688 	 */
689 	sc = NULL;
690 	LIST_FOREACH(gp, &mp->geom, geom) {
691 		sc = gp->softc;
692 		if (sc == NULL)
693 			continue;
694 		if (sc->sc_type != G_CONCAT_TYPE_AUTOMATIC)
695 			continue;
696 		if (strcmp(md.md_name, sc->sc_name) != 0)
697 			continue;
698 		if (md.md_id != sc->sc_id)
699 			continue;
700 		break;
701 	}
702 	if (gp != NULL) {
703 		G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
704 		error = g_concat_add_disk(sc, pp, md.md_no);
705 		if (error != 0) {
706 			G_CONCAT_DEBUG(0,
707 			    "Cannot add disk %s to %s (error=%d).", pp->name,
708 			    gp->name, error);
709 			return (NULL);
710 		}
711 	} else {
712 		gp = g_concat_create(mp, &md, G_CONCAT_TYPE_AUTOMATIC);
713 		if (gp == NULL) {
714 			G_CONCAT_DEBUG(0, "Cannot create device %s.",
715 			    md.md_name);
716 			return (NULL);
717 		}
718 		sc = gp->softc;
719 		G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
720 		error = g_concat_add_disk(sc, pp, md.md_no);
721 		if (error != 0) {
722 			G_CONCAT_DEBUG(0,
723 			    "Cannot add disk %s to %s (error=%d).", pp->name,
724 			    gp->name, error);
725 			g_concat_destroy(sc, 1);
726 			return (NULL);
727 		}
728 	}
729 
730 	return (gp);
731 }
732 
733 static void
734 g_concat_ctl_create(struct gctl_req *req, struct g_class *mp)
735 {
736 	u_int attached, no;
737 	struct g_concat_metadata md;
738 	struct g_provider *pp;
739 	struct g_concat_softc *sc;
740 	struct g_geom *gp;
741 	struct sbuf *sb;
742 	const char *name;
743 	char param[16];
744 	int *nargs;
745 
746 	g_topology_assert();
747 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
748 	if (nargs == NULL) {
749 		gctl_error(req, "No '%s' argument.", "nargs");
750 		return;
751 	}
752 	if (*nargs < 2) {
753 		gctl_error(req, "Too few arguments.");
754 		return;
755 	}
756 
757 	strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
758 	md.md_version = G_CONCAT_VERSION;
759 	name = gctl_get_asciiparam(req, "arg0");
760 	if (name == NULL) {
761 		gctl_error(req, "No 'arg%u' argument.", 0);
762 		return;
763 	}
764 	strlcpy(md.md_name, name, sizeof(md.md_name));
765 	md.md_id = arc4random();
766 	md.md_no = 0;
767 	md.md_all = *nargs - 1;
768 	bzero(md.md_provider, sizeof(md.md_provider));
769 	/* This field is not important here. */
770 	md.md_provsize = 0;
771 
772 	/* Check all providers are valid */
773 	for (no = 1; no < *nargs; no++) {
774 		snprintf(param, sizeof(param), "arg%u", no);
775 		name = gctl_get_asciiparam(req, param);
776 		if (name == NULL) {
777 			gctl_error(req, "No 'arg%u' argument.", no);
778 			return;
779 		}
780 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
781 			name += strlen("/dev/");
782 		pp = g_provider_by_name(name);
783 		if (pp == NULL) {
784 			G_CONCAT_DEBUG(1, "Disk %s is invalid.", name);
785 			gctl_error(req, "Disk %s is invalid.", name);
786 			return;
787 		}
788 	}
789 
790 	gp = g_concat_create(mp, &md, G_CONCAT_TYPE_MANUAL);
791 	if (gp == NULL) {
792 		gctl_error(req, "Can't configure %s.", md.md_name);
793 		return;
794 	}
795 
796 	sc = gp->softc;
797 	sb = sbuf_new_auto();
798 	sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
799 	for (attached = 0, no = 1; no < *nargs; no++) {
800 		snprintf(param, sizeof(param), "arg%u", no);
801 		name = gctl_get_asciiparam(req, param);
802 		if (name == NULL) {
803 			gctl_error(req, "No 'arg%d' argument.", no);
804 			return;
805 		}
806 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
807 			name += strlen("/dev/");
808 		pp = g_provider_by_name(name);
809 		KASSERT(pp != NULL, ("Provider %s disappear?!", name));
810 		if (g_concat_add_disk(sc, pp, no - 1) != 0) {
811 			G_CONCAT_DEBUG(1, "Disk %u (%s) not attached to %s.",
812 			    no, pp->name, gp->name);
813 			sbuf_printf(sb, " %s", pp->name);
814 			continue;
815 		}
816 		attached++;
817 	}
818 	sbuf_finish(sb);
819 	if (md.md_all != attached) {
820 		g_concat_destroy(gp->softc, 1);
821 		gctl_error(req, "%s", sbuf_data(sb));
822 	}
823 	sbuf_delete(sb);
824 }
825 
826 static struct g_concat_softc *
827 g_concat_find_device(struct g_class *mp, const char *name)
828 {
829 	struct g_concat_softc *sc;
830 	struct g_geom *gp;
831 
832 	LIST_FOREACH(gp, &mp->geom, geom) {
833 		sc = gp->softc;
834 		if (sc == NULL)
835 			continue;
836 		if (strcmp(sc->sc_name, name) == 0)
837 			return (sc);
838 	}
839 	return (NULL);
840 }
841 
842 static void
843 g_concat_ctl_destroy(struct gctl_req *req, struct g_class *mp)
844 {
845 	struct g_concat_softc *sc;
846 	int *force, *nargs, error;
847 	const char *name;
848 	char param[16];
849 	u_int i;
850 
851 	g_topology_assert();
852 
853 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
854 	if (nargs == NULL) {
855 		gctl_error(req, "No '%s' argument.", "nargs");
856 		return;
857 	}
858 	if (*nargs <= 0) {
859 		gctl_error(req, "Missing device(s).");
860 		return;
861 	}
862 	force = gctl_get_paraml(req, "force", sizeof(*force));
863 	if (force == NULL) {
864 		gctl_error(req, "No '%s' argument.", "force");
865 		return;
866 	}
867 
868 	for (i = 0; i < (u_int)*nargs; i++) {
869 		snprintf(param, sizeof(param), "arg%u", i);
870 		name = gctl_get_asciiparam(req, param);
871 		if (name == NULL) {
872 			gctl_error(req, "No 'arg%u' argument.", i);
873 			return;
874 		}
875 		sc = g_concat_find_device(mp, name);
876 		if (sc == NULL) {
877 			gctl_error(req, "No such device: %s.", name);
878 			return;
879 		}
880 		error = g_concat_destroy(sc, *force);
881 		if (error != 0) {
882 			gctl_error(req, "Cannot destroy device %s (error=%d).",
883 			    sc->sc_name, error);
884 			return;
885 		}
886 	}
887 }
888 
889 static void
890 g_concat_config(struct gctl_req *req, struct g_class *mp, const char *verb)
891 {
892 	uint32_t *version;
893 
894 	g_topology_assert();
895 
896 	version = gctl_get_paraml(req, "version", sizeof(*version));
897 	if (version == NULL) {
898 		gctl_error(req, "No '%s' argument.", "version");
899 		return;
900 	}
901 	if (*version != G_CONCAT_VERSION) {
902 		gctl_error(req, "Userland and kernel parts are out of sync.");
903 		return;
904 	}
905 
906 	if (strcmp(verb, "create") == 0) {
907 		g_concat_ctl_create(req, mp);
908 		return;
909 	} else if (strcmp(verb, "destroy") == 0 ||
910 	    strcmp(verb, "stop") == 0) {
911 		g_concat_ctl_destroy(req, mp);
912 		return;
913 	}
914 	gctl_error(req, "Unknown verb.");
915 }
916 
917 static void
918 g_concat_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
919     struct g_consumer *cp, struct g_provider *pp)
920 {
921 	struct g_concat_softc *sc;
922 
923 	g_topology_assert();
924 	sc = gp->softc;
925 	if (sc == NULL)
926 		return;
927 	if (pp != NULL) {
928 		/* Nothing here. */
929 	} else if (cp != NULL) {
930 		struct g_concat_disk *disk;
931 
932 		disk = cp->private;
933 		if (disk == NULL)
934 			return;
935 		sbuf_printf(sb, "%s<End>%jd</End>\n", indent,
936 		    (intmax_t)disk->d_end);
937 		sbuf_printf(sb, "%s<Start>%jd</Start>\n", indent,
938 		    (intmax_t)disk->d_start);
939 	} else {
940 		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
941 		sbuf_printf(sb, "%s<Type>", indent);
942 		switch (sc->sc_type) {
943 		case G_CONCAT_TYPE_AUTOMATIC:
944 			sbuf_printf(sb, "AUTOMATIC");
945 			break;
946 		case G_CONCAT_TYPE_MANUAL:
947 			sbuf_printf(sb, "MANUAL");
948 			break;
949 		default:
950 			sbuf_printf(sb, "UNKNOWN");
951 			break;
952 		}
953 		sbuf_printf(sb, "</Type>\n");
954 		sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
955 		    indent, sc->sc_ndisks, g_concat_nvalid(sc));
956 		sbuf_printf(sb, "%s<State>", indent);
957 		if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
958 			sbuf_printf(sb, "UP");
959 		else
960 			sbuf_printf(sb, "DOWN");
961 		sbuf_printf(sb, "</State>\n");
962 	}
963 }
964 
965 DECLARE_GEOM_CLASS(g_concat_class, g_concat);
966