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