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