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