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