xref: /freebsd/sys/geom/concat/g_concat.c (revision 71651a2743acfa3756ab1e7c3983e6861c6fada1)
1 /*-
2  * Copyright (c) 2004-2005 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 	 */
574 	/* There was no md_provider field in earlier versions of metadata. */
575 	if (md.md_version < 3)
576 		bzero(md.md_provider, sizeof(md.md_provider));
577 	/* There was no md_provsize field in earlier versions of metadata. */
578 	if (md.md_version < 4)
579 		md.md_provsize = pp->mediasize;
580 
581 	if (md.md_provider[0] != '\0' && strcmp(md.md_provider, pp->name) != 0)
582 		return (NULL);
583 	if (md.md_provsize != pp->mediasize)
584 		return (NULL);
585 
586 	/*
587 	 * Let's check if device already exists.
588 	 */
589 	sc = NULL;
590 	LIST_FOREACH(gp, &mp->geom, geom) {
591 		sc = gp->softc;
592 		if (sc == NULL)
593 			continue;
594 		if (sc->sc_type != G_CONCAT_TYPE_AUTOMATIC)
595 			continue;
596 		if (strcmp(md.md_name, sc->sc_name) != 0)
597 			continue;
598 		if (md.md_id != sc->sc_id)
599 			continue;
600 		break;
601 	}
602 	if (gp != NULL) {
603 		G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
604 		error = g_concat_add_disk(sc, pp, md.md_no);
605 		if (error != 0) {
606 			G_CONCAT_DEBUG(0,
607 			    "Cannot add disk %s to %s (error=%d).", pp->name,
608 			    gp->name, error);
609 			return (NULL);
610 		}
611 	} else {
612 		gp = g_concat_create(mp, &md, G_CONCAT_TYPE_AUTOMATIC);
613 		if (gp == NULL) {
614 			G_CONCAT_DEBUG(0, "Cannot create device %s.",
615 			    md.md_name);
616 			return (NULL);
617 		}
618 		sc = gp->softc;
619 		G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name);
620 		error = g_concat_add_disk(sc, pp, md.md_no);
621 		if (error != 0) {
622 			G_CONCAT_DEBUG(0,
623 			    "Cannot add disk %s to %s (error=%d).", pp->name,
624 			    gp->name, error);
625 			g_concat_destroy(sc, 1);
626 			return (NULL);
627 		}
628 	}
629 
630 	return (gp);
631 }
632 
633 static void
634 g_concat_ctl_create(struct gctl_req *req, struct g_class *mp)
635 {
636 	u_int attached, no;
637 	struct g_concat_metadata md;
638 	struct g_provider *pp;
639 	struct g_concat_softc *sc;
640 	struct g_geom *gp;
641 	struct sbuf *sb;
642 	const char *name;
643 	char param[16];
644 	int *nargs;
645 
646 	g_topology_assert();
647 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
648 	if (nargs == NULL) {
649 		gctl_error(req, "No '%s' argument.", "nargs");
650 		return;
651 	}
652 	if (*nargs <= 2) {
653 		gctl_error(req, "Too few arguments.");
654 		return;
655 	}
656 
657 	strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic));
658 	md.md_version = G_CONCAT_VERSION;
659 	name = gctl_get_asciiparam(req, "arg0");
660 	if (name == NULL) {
661 		gctl_error(req, "No 'arg%u' argument.", 0);
662 		return;
663 	}
664 	strlcpy(md.md_name, name, sizeof(md.md_name));
665 	md.md_id = arc4random();
666 	md.md_no = 0;
667 	md.md_all = *nargs - 1;
668 	bzero(md.md_provider, sizeof(md.md_provider));
669 	/* This field is not important here. */
670 	md.md_provsize = 0;
671 
672 	/* Check all providers are valid */
673 	for (no = 1; no < *nargs; no++) {
674 		snprintf(param, sizeof(param), "arg%u", no);
675 		name = gctl_get_asciiparam(req, param);
676 		if (name == NULL) {
677 			gctl_error(req, "No 'arg%u' argument.", no);
678 			return;
679 		}
680 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
681 			name += strlen("/dev/");
682 		pp = g_provider_by_name(name);
683 		if (pp == NULL) {
684 			G_CONCAT_DEBUG(1, "Disk %s is invalid.", name);
685 			gctl_error(req, "Disk %s is invalid.", name);
686 			return;
687 		}
688 	}
689 
690 	gp = g_concat_create(mp, &md, G_CONCAT_TYPE_MANUAL);
691 	if (gp == NULL) {
692 		gctl_error(req, "Can't configure %s.", md.md_name);
693 		return;
694 	}
695 
696 	sc = gp->softc;
697 	sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
698 	sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name);
699 	for (attached = 0, no = 1; no < *nargs; no++) {
700 		snprintf(param, sizeof(param), "arg%u", no);
701 		name = gctl_get_asciiparam(req, param);
702 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
703 			name += strlen("/dev/");
704 		pp = g_provider_by_name(name);
705 		KASSERT(pp != NULL, ("Provider %s disappear?!", name));
706 		if (g_concat_add_disk(sc, pp, no - 1) != 0) {
707 			G_CONCAT_DEBUG(1, "Disk %u (%s) not attached to %s.",
708 			    no, pp->name, gp->name);
709 			sbuf_printf(sb, " %s", pp->name);
710 			continue;
711 		}
712 		attached++;
713 	}
714 	sbuf_finish(sb);
715 	if (md.md_all != attached) {
716 		g_concat_destroy(gp->softc, 1);
717 		gctl_error(req, "%s", sbuf_data(sb));
718 	}
719 	sbuf_delete(sb);
720 }
721 
722 static struct g_concat_softc *
723 g_concat_find_device(struct g_class *mp, const char *name)
724 {
725 	struct g_concat_softc *sc;
726 	struct g_geom *gp;
727 
728 	LIST_FOREACH(gp, &mp->geom, geom) {
729 		sc = gp->softc;
730 		if (sc == NULL)
731 			continue;
732 		if (strcmp(sc->sc_name, name) == 0)
733 			return (sc);
734 	}
735 	return (NULL);
736 }
737 
738 static void
739 g_concat_ctl_destroy(struct gctl_req *req, struct g_class *mp)
740 {
741 	struct g_concat_softc *sc;
742 	int *force, *nargs, error;
743 	const char *name;
744 	char param[16];
745 	u_int i;
746 
747 	g_topology_assert();
748 
749 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
750 	if (nargs == NULL) {
751 		gctl_error(req, "No '%s' argument.", "nargs");
752 		return;
753 	}
754 	if (*nargs <= 0) {
755 		gctl_error(req, "Missing device(s).");
756 		return;
757 	}
758 	force = gctl_get_paraml(req, "force", sizeof(*force));
759 	if (force == NULL) {
760 		gctl_error(req, "No '%s' argument.", "force");
761 		return;
762 	}
763 
764 	for (i = 0; i < (u_int)*nargs; i++) {
765 		snprintf(param, sizeof(param), "arg%u", i);
766 		name = gctl_get_asciiparam(req, param);
767 		if (name == NULL) {
768 			gctl_error(req, "No 'arg%u' argument.", i);
769 			return;
770 		}
771 		sc = g_concat_find_device(mp, name);
772 		if (sc == NULL) {
773 			gctl_error(req, "No such device: %s.", name);
774 			return;
775 		}
776 		error = g_concat_destroy(sc, *force);
777 		if (error != 0) {
778 			gctl_error(req, "Cannot destroy device %s (error=%d).",
779 			    sc->sc_name, error);
780 			return;
781 		}
782 	}
783 }
784 
785 static void
786 g_concat_config(struct gctl_req *req, struct g_class *mp, const char *verb)
787 {
788 	uint32_t *version;
789 
790 	g_topology_assert();
791 
792 	version = gctl_get_paraml(req, "version", sizeof(*version));
793 	if (version == NULL) {
794 		gctl_error(req, "No '%s' argument.", "version");
795 		return;
796 	}
797 	if (*version != G_CONCAT_VERSION) {
798 		gctl_error(req, "Userland and kernel parts are out of sync.");
799 		return;
800 	}
801 
802 	if (strcmp(verb, "create") == 0) {
803 		g_concat_ctl_create(req, mp);
804 		return;
805 	} else if (strcmp(verb, "destroy") == 0 ||
806 	    strcmp(verb, "stop") == 0) {
807 		g_concat_ctl_destroy(req, mp);
808 		return;
809 	}
810 	gctl_error(req, "Unknown verb.");
811 }
812 
813 static void
814 g_concat_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
815     struct g_consumer *cp, struct g_provider *pp)
816 {
817 	struct g_concat_softc *sc;
818 
819 	g_topology_assert();
820 	sc = gp->softc;
821 	if (sc == NULL)
822 		return;
823 	if (pp != NULL) {
824 		/* Nothing here. */
825 	} else if (cp != NULL) {
826 		struct g_concat_disk *disk;
827 
828 		disk = cp->private;
829 		if (disk == NULL)
830 			return;
831 		sbuf_printf(sb, "%s<End>%jd</End>\n", indent,
832 		    (intmax_t)disk->d_end);
833 		sbuf_printf(sb, "%s<Start>%jd</Start>\n", indent,
834 		    (intmax_t)disk->d_start);
835 	} else {
836 		sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id);
837 		sbuf_printf(sb, "%s<Type>", indent);
838 		switch (sc->sc_type) {
839 		case G_CONCAT_TYPE_AUTOMATIC:
840 			sbuf_printf(sb, "AUTOMATIC");
841 			break;
842 		case G_CONCAT_TYPE_MANUAL:
843 			sbuf_printf(sb, "MANUAL");
844 			break;
845 		default:
846 			sbuf_printf(sb, "UNKNOWN");
847 			break;
848 		}
849 		sbuf_printf(sb, "</Type>\n");
850 		sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n",
851 		    indent, sc->sc_ndisks, g_concat_nvalid(sc));
852 		sbuf_printf(sb, "%s<State>", indent);
853 		if (sc->sc_provider != NULL && sc->sc_provider->error == 0)
854 			sbuf_printf(sb, "UP");
855 		else
856 			sbuf_printf(sb, "DOWN");
857 		sbuf_printf(sb, "</State>\n");
858 	}
859 }
860 
861 DECLARE_GEOM_CLASS(g_concat_class, g_concat);
862