xref: /freebsd/sys/geom/concat/g_concat.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
1 /*-
2  * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/bio.h>
37 #include <sys/sysctl.h>
38 #include <sys/malloc.h>
39 #include <geom/geom.h>
40 #include <geom/concat/g_concat.h>
41 
42 
43 static MALLOC_DEFINE(M_CONCAT, "concat data", "GEOM_CONCAT Data");
44 
45 SYSCTL_DECL(_kern_geom);
46 SYSCTL_NODE(_kern_geom, OID_AUTO, concat, CTLFLAG_RW, 0, "GEOM_CONCAT stuff");
47 static u_int g_concat_debug = 0;
48 TUNABLE_INT("kern.geom.concat.debug", &g_concat_debug);
49 SYSCTL_UINT(_kern_geom_concat, OID_AUTO, debug, CTLFLAG_RW, &g_concat_debug, 0,
50     "Debug level");
51 
52 static int g_concat_destroy(struct g_concat_softc *sc, boolean_t force);
53 static int g_concat_destroy_geom(struct gctl_req *req, struct g_class *mp,
54     struct g_geom *gp);
55 
56 static g_taste_t g_concat_taste;
57 static g_ctl_req_t g_concat_config;
58 static g_dumpconf_t g_concat_dumpconf;
59 
60 struct g_class g_concat_class = {
61 	.name = G_CONCAT_CLASS_NAME,
62 	.version = G_VERSION,
63 	.ctlreq = g_concat_config,
64 	.taste = g_concat_taste,
65 	.destroy_geom = g_concat_destroy_geom
66 };
67 
68 
69 /*
70  * Greatest Common Divisor.
71  */
72 static u_int
73 gcd(u_int a, u_int b)
74 {
75 	u_int c;
76 
77 	while (b != 0) {
78 		c = a;
79 		a = b;
80 		b = (c % b);
81 	}
82 	return (a);
83 }
84 
85 /*
86  * Least Common Multiple.
87  */
88 static u_int
89 lcm(u_int a, u_int b)
90 {
91 
92 	return ((a * b) / gcd(a, b));
93 }
94 
95 /*
96  * Return the number of valid disks.
97  */
98 static u_int
99 g_concat_nvalid(struct g_concat_softc *sc)
100 {
101 	u_int i, no;
102 
103 	no = 0;
104 	for (i = 0; i < sc->sc_ndisks; i++) {
105 		if (sc->sc_disks[i].d_consumer != NULL)
106 			no++;
107 	}
108 
109 	return (no);
110 }
111 
112 static void
113 g_concat_remove_disk(struct g_concat_disk *disk)
114 {
115 	struct g_consumer *cp;
116 	struct g_concat_softc *sc;
117 
118 	KASSERT(disk->d_consumer != NULL, ("Non-valid disk in %s.", __func__));
119 	sc = disk->d_softc;
120 	cp = disk->d_consumer;
121 
122 	G_CONCAT_DEBUG(0, "Disk %s removed from %s.", cp->provider->name,
123 	    sc->sc_name);
124 
125 	disk->d_consumer = NULL;
126 	if (sc->sc_provider != NULL) {
127 		g_orphan_provider(sc->sc_provider, ENXIO);
128 		sc->sc_provider = NULL;
129 		G_CONCAT_DEBUG(0, "Device %s removed.", sc->sc_name);
130 	}
131 
132 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
133 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
134 	g_detach(cp);
135 	g_destroy_consumer(cp);
136 }
137 
138 static void
139 g_concat_orphan(struct g_consumer *cp)
140 {
141 	struct g_concat_softc *sc;
142 	struct g_concat_disk *disk;
143 	struct g_geom *gp;
144 
145 	g_topology_assert();
146 	gp = cp->geom;
147 	sc = gp->softc;
148 	if (sc == NULL)
149 		return;
150 
151 	disk = cp->private;
152 	if (disk == NULL)	/* Possible? */
153 		return;
154 	g_concat_remove_disk(disk);
155 
156 	/* If there are no valid disks anymore, remove device. */
157 	if (g_concat_nvalid(sc) == 0)
158 		g_concat_destroy(sc, 1);
159 }
160 
161 static int
162 g_concat_access(struct g_provider *pp, int dr, int dw, int de)
163 {
164 	struct g_consumer *cp1, *cp2;
165 	struct g_concat_softc *sc;
166 	struct g_geom *gp;
167 	int error;
168 
169 	gp = pp->geom;
170 	sc = gp->softc;
171 
172 	if (sc == NULL) {
173 		/*
174 		 * It looks like geom is being withered.
175 		 * In that case we allow only negative requests.
176 		 */
177 		KASSERT(dr <= 0 && dw <= 0 && de <= 0,
178 		    ("Positive access request (device=%s).", pp->name));
179 		if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 &&
180 		    (pp->ace + de) == 0) {
181 			G_CONCAT_DEBUG(0, "Device %s definitely destroyed.",
182 			    gp->name);
183 		}
184 		return (0);
185 	}
186 
187 	/* On first open, grab an extra "exclusive" bit */
188 	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)
189 		de++;
190 	/* ... and let go of it on last close */
191 	if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0)
192 		de--;
193 
194 	error = ENXIO;
195 	LIST_FOREACH(cp1, &gp->consumer, consumer) {
196 		error = g_access(cp1, dr, dw, de);
197 		if (error == 0)
198 			continue;
199 		/*
200 		 * If we fail here, backout all previous changes.
201 		 */
202 		LIST_FOREACH(cp2, &gp->consumer, consumer) {
203 			if (cp1 == cp2)
204 				return (error);
205 			g_access(cp2, -dr, -dw, -de);
206 		}
207 		/* NOTREACHED */
208 	}
209 
210 	return (error);
211 }
212 
213 static void
214 g_concat_start(struct bio *bp)
215 {
216 	struct g_concat_softc *sc;
217 	struct g_concat_disk *disk;
218 	struct g_provider *pp;
219 	off_t offset, end, length, off, len;
220 	struct bio *cbp;
221 	char *addr;
222 	u_int no;
223 
224 	pp = bp->bio_to;
225 	sc = pp->geom->softc;
226 	/*
227 	 * If sc == NULL, provider's error should be set and g_concat_start()
228 	 * should not be called at all.
229 	 */
230 	KASSERT(sc != NULL,
231 	    ("Provider's error should be set (error=%d)(device=%s).",
232 	    bp->bio_to->error, bp->bio_to->name));
233 
234 	G_CONCAT_LOGREQ(bp, "Request received.");
235 
236 	switch (bp->bio_cmd) {
237 	case BIO_READ:
238 	case BIO_WRITE:
239 	case BIO_DELETE:
240 		break;
241 	case BIO_GETATTR:
242 		/* To which provider it should be delivered? */
243 	default:
244 		g_io_deliver(bp, EOPNOTSUPP);
245 		return;
246 	}
247 
248 	offset = bp->bio_offset;
249 	length = bp->bio_length;
250 	addr = bp->bio_data;
251 	end = offset + length;
252 
253 	for (no = 0; no < sc->sc_ndisks; no++) {
254 		disk = &sc->sc_disks[no];
255 		if (disk->d_end <= offset)
256 			continue;
257 		if (disk->d_start >= end)
258 			break;
259 
260 		off = offset - disk->d_start;
261 		len = MIN(length, disk->d_end - offset);
262 		length -= len;
263 		offset += len;
264 
265 		cbp = g_clone_bio(bp);
266 		if (cbp == NULL) {
267 			if (bp->bio_error == 0)
268 				bp->bio_error = ENOMEM;
269 			return;
270 		}
271 		/*
272 		 * Fill in the component buf structure.
273 		 */
274 		cbp->bio_done = g_std_done;
275 		cbp->bio_offset = off;
276 		cbp->bio_data = addr;
277 		addr += len;
278 		cbp->bio_length = len;
279 		cbp->bio_to = disk->d_consumer->provider;
280 		G_CONCAT_LOGREQ(cbp, "Sending request.");
281 		g_io_request(cbp, disk->d_consumer);
282 
283 		if (length == 0)
284 			break;
285 	}
286 
287 	KASSERT(length == 0,
288 	    ("Length is still greater than 0 (class=%s, name=%s).",
289 	    bp->bio_to->geom->class->name, bp->bio_to->geom->name));
290 }
291 
292 static void
293 g_concat_check_and_run(struct g_concat_softc *sc)
294 {
295 	struct g_concat_disk *disk;
296 	u_int no, sectorsize = 0;
297 	off_t start;
298 
299 	if (g_concat_nvalid(sc) != sc->sc_ndisks)
300 		return;
301 
302 	sc->sc_provider = g_new_providerf(sc->sc_geom, "concat/%s",
303 	    sc->sc_name);
304 	start = 0;
305 	for (no = 0; no < sc->sc_ndisks; no++) {
306 		disk = &sc->sc_disks[no];
307 		disk->d_start = start;
308 		disk->d_end = disk->d_start +
309 		    disk->d_consumer->provider->mediasize;
310 		if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC)
311 			disk->d_end -= disk->d_consumer->provider->sectorsize;
312 		start = disk->d_end;
313 		if (no == 0)
314 			sectorsize = disk->d_consumer->provider->sectorsize;
315 		else {
316 			sectorsize = lcm(sectorsize,
317 			    disk->d_consumer->provider->sectorsize);
318 		}
319 	}
320 	sc->sc_provider->sectorsize = sectorsize;
321 	/* We have sc->sc_disks[sc->sc_ndisks - 1].d_end in 'start'. */
322 	sc->sc_provider->mediasize = start;
323 	g_error_provider(sc->sc_provider, 0);
324 
325 	G_CONCAT_DEBUG(0, "Device %s activated.", sc->sc_name);
326 }
327 
328 static int
329 g_concat_read_metadata(struct g_consumer *cp, struct g_concat_metadata *md)
330 {
331 	struct g_provider *pp;
332 	u_char *buf;
333 	int error;
334 
335 	g_topology_assert();
336 
337 	error = g_access(cp, 1, 0, 0);
338 	if (error != 0)
339 		return (error);
340 	pp = cp->provider;
341 	g_topology_unlock();
342 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize,
343 	    &error);
344 	g_topology_lock();
345 	g_access(cp, -1, 0, 0);
346 	if (buf == NULL)
347 		return (error);
348 
349 	/* Decode metadata. */
350 	concat_metadata_decode(buf, md);
351 	g_free(buf);
352 
353 	return (0);
354 }
355 
356 /*
357  * Add disk to given device.
358  */
359 static int
360 g_concat_add_disk(struct g_concat_softc *sc, struct g_provider *pp, u_int no)
361 {
362 	struct g_concat_disk *disk;
363 	struct g_consumer *cp, *fcp;
364 	struct g_geom *gp;
365 	int error;
366 
367 	/* Metadata corrupted? */
368 	if (no >= sc->sc_ndisks)
369 		return (EINVAL);
370 
371 	disk = &sc->sc_disks[no];
372 	/* Check if disk is not already attached. */
373 	if (disk->d_consumer != NULL)
374 		return (EEXIST);
375 
376 	gp = sc->sc_geom;
377 	fcp = LIST_FIRST(&gp->consumer);
378 
379 	cp = g_new_consumer(gp);
380 	error = g_attach(cp, pp);
381 	if (error != 0) {
382 		g_destroy_consumer(cp);
383 		return (error);
384 	}
385 
386 	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) {
387 		error = g_access(cp, fcp->acr, fcp->acw, fcp->ace);
388 		if (error != 0) {
389 			g_detach(cp);
390 			g_destroy_consumer(cp);
391 			return (error);
392 		}
393 	}
394 	if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) {
395 		struct g_concat_metadata md;
396 
397 		/* Re-read metadata. */
398 		error = g_concat_read_metadata(cp, &md);
399 		if (error != 0)
400 			goto fail;
401 
402 		if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0 ||
403 		    strcmp(md.md_name, sc->sc_name) != 0 ||
404 		    md.md_id != sc->sc_id) {
405 			G_CONCAT_DEBUG(0, "Metadata on %s changed.", pp->name);
406 			goto fail;
407 		}
408 	}
409 
410 	cp->private = disk;
411 	disk->d_consumer = cp;
412 	disk->d_softc = sc;
413 	disk->d_start = 0;	/* not yet */
414 	disk->d_end = 0;	/* not yet */
415 
416 	G_CONCAT_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name);
417 
418 	g_concat_check_and_run(sc);
419 
420 	return (0);
421 fail:
422 	if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0))
423 		g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace);
424 	g_detach(cp);
425 	g_destroy_consumer(cp);
426 	return (error);
427 }
428 
429 static struct g_geom *
430 g_concat_create(struct g_class *mp, const struct g_concat_metadata *md,
431     u_int type)
432 {
433 	struct g_concat_softc *sc;
434 	struct g_geom *gp;
435 	u_int no;
436 
437 	G_CONCAT_DEBUG(1, "Creating device %s (id=%u).", md->md_name,
438 	    md->md_id);
439 
440 	/* Two disks is minimum. */
441 	if (md->md_all <= 1)
442 		return (NULL);
443 
444 	/* Check for duplicate unit */
445 	LIST_FOREACH(gp, &mp->geom, geom) {
446 		sc = gp->softc;
447 		if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) {
448 			G_CONCAT_DEBUG(0, "Device %s already configured.",
449 			    gp->name);
450 			return (NULL);
451 		}
452 	}
453 	gp = g_new_geomf(mp, "%s", md->md_name);
454 	gp->softc = NULL;	/* for a moment */
455 
456 	sc = malloc(sizeof(*sc), M_CONCAT, M_WAITOK | M_ZERO);
457 	gp->start = g_concat_start;
458 	gp->spoiled = g_concat_orphan;
459 	gp->orphan = g_concat_orphan;
460 	gp->access = g_concat_access;
461 	gp->dumpconf = g_concat_dumpconf;
462 
463 	sc->sc_id = md->md_id;
464 	sc->sc_ndisks = md->md_all;
465 	sc->sc_disks = malloc(sizeof(struct g_concat_disk) * sc->sc_ndisks,
466 	    M_CONCAT, M_WAITOK | M_ZERO);
467 	for (no = 0; no < sc->sc_ndisks; no++)
468 		sc->sc_disks[no].d_consumer = NULL;
469 	sc->sc_type = type;
470 
471 	gp->softc = sc;
472 	sc->sc_geom = gp;
473 	sc->sc_provider = NULL;
474 
475 	G_CONCAT_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id);
476 
477 	return (gp);
478 }
479 
480 static int
481 g_concat_destroy(struct g_concat_softc *sc, boolean_t force)
482 {
483 	struct g_provider *pp;
484 	struct g_geom *gp;
485 	u_int no;
486 
487 	g_topology_assert();
488 
489 	if (sc == NULL)
490 		return (ENXIO);
491 
492 	pp = sc->sc_provider;
493 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
494 		if (force) {
495 			G_CONCAT_DEBUG(0, "Device %s is still open, so it "
496 			    "can't be definitely removed.", pp->name);
497 		} else {
498 			G_CONCAT_DEBUG(1,
499 			    "Device %s is still open (r%dw%de%d).", pp->name,
500 			    pp->acr, pp->acw, pp->ace);
501 			return (EBUSY);
502 		}
503 	}
504 
505 	for (no = 0; no < sc->sc_ndisks; no++) {
506 		if (sc->sc_disks[no].d_consumer != NULL)
507 			g_concat_remove_disk(&sc->sc_disks[no]);
508 	}
509 
510 	gp = sc->sc_geom;
511 	gp->softc = NULL;
512 	KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)",
513 	    gp->name));
514 	free(sc->sc_disks, M_CONCAT);
515 	free(sc, M_CONCAT);
516 
517 	pp = LIST_FIRST(&gp->provider);
518 	if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0))
519 		G_CONCAT_DEBUG(0, "Device %s destroyed.", gp->name);
520 
521 	g_wither_geom(gp, ENXIO);
522 
523 	return (0);
524 }
525 
526 static int
527 g_concat_destroy_geom(struct gctl_req *req __unused,
528     struct g_class *mp __unused, struct g_geom *gp)
529 {
530 	struct g_concat_softc *sc;
531 
532 	sc = gp->softc;
533 	return (g_concat_destroy(sc, 0));
534 }
535 
536 static struct g_geom *
537 g_concat_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
538 {
539 	struct g_concat_metadata md;
540 	struct g_concat_softc *sc;
541 	struct g_consumer *cp;
542 	struct g_geom *gp;
543 	int error;
544 
545 	g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name);
546 	g_topology_assert();
547 
548 	G_CONCAT_DEBUG(3, "Tasting %s.", pp->name);
549 
550 	gp = g_new_geomf(mp, "concat:taste");
551 	gp->start = g_concat_start;
552 	gp->access = g_concat_access;
553 	gp->orphan = g_concat_orphan;
554 	cp = g_new_consumer(gp);
555 	g_attach(cp, pp);
556 	error = g_concat_read_metadata(cp, &md);
557 	g_detach(cp);
558 	g_destroy_consumer(cp);
559 	g_destroy_geom(gp);
560 	if (error != 0)
561 		return (NULL);
562 	gp = NULL;
563 
564 	if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0)
565 		return (NULL);
566 	if (md.md_version > G_CONCAT_VERSION) {
567 		printf("geom_concat.ko module is too old to handle %s.\n",
568 		    pp->name);
569 		return (NULL);
570 	}
571 	/*
572 	 * Backward compatibility:
573 	 * There was no md_provider field in earlier versions of metadata.
574 	 */
575 	if (md.md_version < 3)
576 		bzero(md.md_provider, sizeof(md.md_provider));
577 
578 	if (md.md_provider[0] != '\0' && strcmp(md.md_provider, pp->name) != 0)
579 		return (NULL);
580 
581 	/*
582 	 * Let's check if device already exists.
583 	 */
584 	sc = NULL;
585 	LIST_FOREACH(gp, &mp->geom, geom) {
586 		sc = gp->softc;
587 		if (sc == NULL)
588 			continue;
589 		if (sc->sc_type != G_CONCAT_TYPE_AUTOMATIC)
590 			continue;
591 		if (strcmp(md.md_name, sc->sc_name) != 0)
592 			continue;
593 		if (md.md_id != sc->sc_id)
594 			continue;
595 		break;
596 	}
597 	if (gp != NULL) {
598 		G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
599 		error = g_concat_add_disk(sc, pp, md.md_no);
600 		if (error != 0) {
601 			G_CONCAT_DEBUG(0,
602 			    "Cannot add disk %s to %s (error=%d).", pp->name,
603 			    gp->name, error);
604 			return (NULL);
605 		}
606 	} else {
607 		gp = g_concat_create(mp, &md, G_CONCAT_TYPE_AUTOMATIC);
608 		if (gp == NULL) {
609 			G_CONCAT_DEBUG(0, "Cannot create device %s.",
610 			    md.md_name);
611 			return (NULL);
612 		}
613 		sc = gp->softc;
614 		G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
615 		error = g_concat_add_disk(sc, pp, md.md_no);
616 		if (error != 0) {
617 			G_CONCAT_DEBUG(0,
618 			    "Cannot add disk %s to %s (error=%d).", pp->name,
619 			    gp->name, error);
620 			g_concat_destroy(sc, 1);
621 			return (NULL);
622 		}
623 	}
624 
625 	return (gp);
626 }
627 
628 static void
629 g_concat_ctl_create(struct gctl_req *req, struct g_class *mp)
630 {
631 	u_int attached, no;
632 	struct g_concat_metadata md;
633 	struct g_provider *pp;
634 	struct g_concat_softc *sc;
635 	struct g_geom *gp;
636 	struct sbuf *sb;
637 	const char *name;
638 	char param[16];
639 	int *nargs;
640 
641 	g_topology_assert();
642 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
643 	if (nargs == NULL) {
644 		gctl_error(req, "No '%s' argument.", "nargs");
645 		return;
646 	}
647 	if (*nargs <= 2) {
648 		gctl_error(req, "Too few arguments.");
649 		return;
650 	}
651 
652 	strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
653 	md.md_version = G_CONCAT_VERSION;
654 	name = gctl_get_asciiparam(req, "arg0");
655 	if (name == NULL) {
656 		gctl_error(req, "No 'arg%u' argument.", 0);
657 		return;
658 	}
659 	strlcpy(md.md_name, name, sizeof(md.md_name));
660 	md.md_id = arc4random();
661 	md.md_no = 0;
662 	md.md_all = *nargs - 1;
663 	bzero(md.md_provider, sizeof(md.md_provider));
664 
665 	/* Check all providers are valid */
666 	for (no = 1; no < *nargs; no++) {
667 		snprintf(param, sizeof(param), "arg%u", no);
668 		name = gctl_get_asciiparam(req, param);
669 		if (name == NULL) {
670 			gctl_error(req, "No 'arg%u' argument.", no);
671 			return;
672 		}
673 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
674 			name += strlen("/dev/");
675 		pp = g_provider_by_name(name);
676 		if (pp == NULL) {
677 			G_CONCAT_DEBUG(1, "Disk %s is invalid.", name);
678 			gctl_error(req, "Disk %s is invalid.", name);
679 			return;
680 		}
681 	}
682 
683 	gp = g_concat_create(mp, &md, G_CONCAT_TYPE_MANUAL);
684 	if (gp == NULL) {
685 		gctl_error(req, "Can't configure %s.", md.md_name);
686 		return;
687 	}
688 
689 	sc = gp->softc;
690 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
691 	sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
692 	for (attached = 0, no = 1; no < *nargs; no++) {
693 		snprintf(param, sizeof(param), "arg%u", no);
694 		name = gctl_get_asciiparam(req, param);
695 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
696 			name += strlen("/dev/");
697 		pp = g_provider_by_name(name);
698 		KASSERT(pp != NULL, ("Provider %s disappear?!", name));
699 		if (g_concat_add_disk(sc, pp, no - 1) != 0) {
700 			G_CONCAT_DEBUG(1, "Disk %u (%s) not attached to %s.",
701 			    no, pp->name, gp->name);
702 			sbuf_printf(sb, " %s", pp->name);
703 			continue;
704 		}
705 		attached++;
706 	}
707 	sbuf_finish(sb);
708 	if (md.md_all != attached) {
709 		g_concat_destroy(gp->softc, 1);
710 		gctl_error(req, "%s", sbuf_data(sb));
711 	}
712 	sbuf_delete(sb);
713 }
714 
715 static struct g_concat_softc *
716 g_concat_find_device(struct g_class *mp, const char *name)
717 {
718 	struct g_concat_softc *sc;
719 	struct g_geom *gp;
720 
721 	LIST_FOREACH(gp, &mp->geom, geom) {
722 		sc = gp->softc;
723 		if (sc == NULL)
724 			continue;
725 		if (strcmp(sc->sc_name, name) == 0)
726 			return (sc);
727 	}
728 	return (NULL);
729 }
730 
731 static void
732 g_concat_ctl_destroy(struct gctl_req *req, struct g_class *mp)
733 {
734 	struct g_concat_softc *sc;
735 	int *force, *nargs, error;
736 	const char *name;
737 	char param[16];
738 	u_int i;
739 
740 	g_topology_assert();
741 
742 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
743 	if (nargs == NULL) {
744 		gctl_error(req, "No '%s' argument.", "nargs");
745 		return;
746 	}
747 	if (*nargs <= 0) {
748 		gctl_error(req, "Missing device(s).");
749 		return;
750 	}
751 	force = gctl_get_paraml(req, "force", sizeof(*force));
752 	if (force == NULL) {
753 		gctl_error(req, "No '%s' argument.", "force");
754 		return;
755 	}
756 
757 	for (i = 0; i < (u_int)*nargs; i++) {
758 		snprintf(param, sizeof(param), "arg%u", i);
759 		name = gctl_get_asciiparam(req, param);
760 		if (name == NULL) {
761 			gctl_error(req, "No 'arg%u' argument.", i);
762 			return;
763 		}
764 		sc = g_concat_find_device(mp, name);
765 		if (sc == NULL) {
766 			gctl_error(req, "No such device: %s.", name);
767 			return;
768 		}
769 		error = g_concat_destroy(sc, *force);
770 		if (error != 0) {
771 			gctl_error(req, "Cannot destroy device %s (error=%d).",
772 			    sc->sc_name, error);
773 			return;
774 		}
775 	}
776 }
777 
778 static void
779 g_concat_config(struct gctl_req *req, struct g_class *mp, const char *verb)
780 {
781 	uint32_t *version;
782 
783 	g_topology_assert();
784 
785 	version = gctl_get_paraml(req, "version", sizeof(*version));
786 	if (version == NULL) {
787 		gctl_error(req, "No '%s' argument.", "version");
788 		return;
789 	}
790 	if (*version != G_CONCAT_VERSION) {
791 		gctl_error(req, "Userland and kernel parts are out of sync.");
792 		return;
793 	}
794 
795 	if (strcmp(verb, "create") == 0) {
796 		g_concat_ctl_create(req, mp);
797 		return;
798 	} else if (strcmp(verb, "destroy") == 0 ||
799 	    strcmp(verb, "stop") == 0) {
800 		g_concat_ctl_destroy(req, mp);
801 		return;
802 	}
803 	gctl_error(req, "Unknown verb.");
804 }
805 
806 static void
807 g_concat_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
808     struct g_consumer *cp, struct g_provider *pp)
809 {
810 	struct g_concat_softc *sc;
811 
812 	g_topology_assert();
813 	sc = gp->softc;
814 	if (sc == NULL)
815 		return;
816 	if (pp != NULL) {
817 		/* Nothing here. */
818 	} else if (cp != NULL) {
819 		struct g_concat_disk *disk;
820 
821 		disk = cp->private;
822 		if (disk == NULL)
823 			return;
824 		sbuf_printf(sb, "%s<End>%jd</End>\n", indent,
825 		    (intmax_t)disk->d_end);
826 		sbuf_printf(sb, "%s<Start>%jd</Start>\n", indent,
827 		    (intmax_t)disk->d_start);
828 	} else {
829 		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
830 		sbuf_printf(sb, "%s<Type>", indent);
831 		switch (sc->sc_type) {
832 		case G_CONCAT_TYPE_AUTOMATIC:
833 			sbuf_printf(sb, "AUTOMATIC");
834 			break;
835 		case G_CONCAT_TYPE_MANUAL:
836 			sbuf_printf(sb, "MANUAL");
837 			break;
838 		default:
839 			sbuf_printf(sb, "UNKNOWN");
840 			break;
841 		}
842 		sbuf_printf(sb, "</Type>\n");
843 		sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
844 		    indent, sc->sc_ndisks, g_concat_nvalid(sc));
845 		sbuf_printf(sb, "%s<State>", indent);
846 		if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
847 			sbuf_printf(sb, "UP");
848 		else
849 			sbuf_printf(sb, "DOWN");
850 		sbuf_printf(sb, "</State>\n");
851 	}
852 }
853 
854 DECLARE_GEOM_CLASS(g_concat_class, g_concat);
855