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