xref: /freebsd/sys/geom/multipath/g_multipath.c (revision 4ed925457ab06e83238a5db33e89ccc94b99a713)
1 /*-
2  * Copyright (c) 2006-2007 Matthew Jacob <mjacob@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  * Based upon work by Pawel Jakub Dawidek <pjd@FreeBSD.org> for all of the
28  * fine geom examples, and by Poul Henning Kamp <phk@FreeBSD.org> for GEOM
29  * itself, all of which is most gratefully acknowledged.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/bio.h>
41 #include <sys/sysctl.h>
42 #include <sys/kthread.h>
43 #include <sys/malloc.h>
44 #include <geom/geom.h>
45 #include <geom/multipath/g_multipath.h>
46 
47 
48 SYSCTL_DECL(_kern_geom);
49 SYSCTL_NODE(_kern_geom, OID_AUTO, multipath, CTLFLAG_RW, 0,
50     "GEOM_MULTIPATH tunables");
51 static u_int g_multipath_debug = 0;
52 SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, debug, CTLFLAG_RW,
53     &g_multipath_debug, 0, "Debug level");
54 
55 static enum {
56 	GKT_NIL,
57 	GKT_RUN,
58 	GKT_DIE
59 } g_multipath_kt_state;
60 static struct bio_queue_head gmtbq;
61 static struct mtx gmtbq_mtx;
62 
63 static void g_multipath_orphan(struct g_consumer *);
64 static void g_multipath_start(struct bio *);
65 static void g_multipath_done(struct bio *);
66 static void g_multipath_done_error(struct bio *);
67 static void g_multipath_kt(void *);
68 
69 static int g_multipath_destroy(struct g_geom *);
70 static int
71 g_multipath_destroy_geom(struct gctl_req *, struct g_class *, struct g_geom *);
72 
73 static g_taste_t g_multipath_taste;
74 static g_ctl_req_t g_multipath_config;
75 static g_init_t g_multipath_init;
76 static g_fini_t g_multipath_fini;
77 
78 struct g_class g_multipath_class = {
79 	.name		= G_MULTIPATH_CLASS_NAME,
80 	.version	= G_VERSION,
81 	.ctlreq		= g_multipath_config,
82 	.taste		= g_multipath_taste,
83 	.destroy_geom	= g_multipath_destroy_geom,
84 	.init		= g_multipath_init,
85 	.fini		= g_multipath_fini
86 };
87 
88 #define	MP_BAD		0x1
89 #define	MP_POSTED	0x2
90 
91 static void
92 g_mpd(void *arg, int flags __unused)
93 {
94 	struct g_consumer *cp;
95 
96 	g_topology_assert();
97 	cp = arg;
98 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
99 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
100 	if (cp->provider) {
101 		printf("GEOM_MULTIPATH: %s removed from %s\n",
102 		    cp->provider->name, cp->geom->name);
103 		g_detach(cp);
104 	}
105 	g_destroy_consumer(cp);
106 }
107 
108 static void
109 g_multipath_orphan(struct g_consumer *cp)
110 {
111 	if ((cp->index & MP_POSTED) == 0) {
112 		cp->index |= MP_POSTED;
113 		printf("GEOM_MULTIPATH: %s orphaned in %s\n",
114 		    cp->provider->name, cp->geom->name);
115 		g_mpd(cp, 0);
116 	}
117 }
118 
119 static void
120 g_multipath_start(struct bio *bp)
121 {
122 	struct g_multipath_softc *sc;
123 	struct g_geom *gp;
124 	struct g_consumer *cp;
125 	struct bio *cbp;
126 
127 	gp = bp->bio_to->geom;
128 	sc = gp->softc;
129 	KASSERT(sc != NULL, ("NULL sc"));
130 	cp = sc->cp_active;
131 	if (cp == NULL) {
132 		g_io_deliver(bp, ENXIO);
133 		return;
134 	}
135 	cbp = g_clone_bio(bp);
136 	if (cbp == NULL) {
137 		g_io_deliver(bp, ENOMEM);
138 		return;
139 	}
140 	cbp->bio_done = g_multipath_done;
141 	g_io_request(cbp, cp);
142 }
143 
144 static void
145 g_multipath_done(struct bio *bp)
146 {
147 	if (bp->bio_error == ENXIO || bp->bio_error == EIO) {
148 		mtx_lock(&gmtbq_mtx);
149 		bioq_insert_tail(&gmtbq, bp);
150 		wakeup(&g_multipath_kt_state);
151 		mtx_unlock(&gmtbq_mtx);
152 	} else {
153 		g_std_done(bp);
154 	}
155 }
156 
157 static void
158 g_multipath_done_error(struct bio *bp)
159 {
160 	struct bio *pbp;
161 	struct g_geom *gp;
162 	struct g_multipath_softc *sc;
163 	struct g_consumer *cp;
164 	struct g_provider *pp;
165 
166 	/*
167 	 * If we had a failure, we have to check first to see
168 	 * whether the consumer it failed on was the currently
169 	 * active consumer (i.e., this is the first in perhaps
170 	 * a number of failures). If so, we then switch consumers
171 	 * to the next available consumer.
172 	 */
173 
174 	g_topology_lock();
175 	pbp = bp->bio_parent;
176 	gp = pbp->bio_to->geom;
177 	sc = gp->softc;
178 	cp = bp->bio_from;
179 	pp = cp->provider;
180 
181 	cp->index |= MP_BAD;
182 	if (cp->nend == cp->nstart && pp->nend == pp->nstart) {
183 		cp->index |= MP_POSTED;
184 		g_post_event(g_mpd, cp, M_NOWAIT, NULL);
185 	}
186 	if (cp == sc->cp_active) {
187 		struct g_consumer *lcp;
188 		printf("GEOM_MULTIPATH: %s failed in %s\n",
189 		    pp->name, sc->sc_name);
190 		sc->cp_active = NULL;
191 		LIST_FOREACH(lcp, &gp->consumer, consumer) {
192 			if ((lcp->index & MP_BAD) == 0) {
193 				sc->cp_active = lcp;
194 				break;
195 			}
196 		}
197 		if (sc->cp_active == NULL) {
198 			printf("GEOM_MULTIPATH: out of providers for %s\n",
199 			    sc->sc_name);
200 			g_topology_unlock();
201 			return;
202 		} else {
203 			printf("GEOM_MULTIPATH: %s now active path in %s\n",
204 			    sc->cp_active->provider->name, sc->sc_name);
205 		}
206 	}
207 	g_topology_unlock();
208 
209 	/*
210 	 * If we can fruitfully restart the I/O, do so.
211 	 */
212 	if (sc->cp_active) {
213 		g_destroy_bio(bp);
214 		pbp->bio_children--;
215 		g_multipath_start(pbp);
216 	} else {
217 		g_std_done(bp);
218 	}
219 }
220 
221 static void
222 g_multipath_kt(void *arg)
223 {
224 
225 	g_multipath_kt_state = GKT_RUN;
226 	mtx_lock(&gmtbq_mtx);
227 	while (g_multipath_kt_state == GKT_RUN) {
228 		for (;;) {
229 			struct bio *bp;
230 
231 			bp = bioq_takefirst(&gmtbq);
232 			if (bp == NULL)
233 				break;
234 			mtx_unlock(&gmtbq_mtx);
235 			g_multipath_done_error(bp);
236 			mtx_lock(&gmtbq_mtx);
237 		}
238 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
239 		    "gkt:wait", hz / 10);
240 	}
241 	mtx_unlock(&gmtbq_mtx);
242 	wakeup(&g_multipath_kt_state);
243 	kproc_exit(0);
244 }
245 
246 
247 static int
248 g_multipath_access(struct g_provider *pp, int dr, int dw, int de)
249 {
250 	struct g_geom *gp;
251 	struct g_consumer *cp, *badcp = NULL;
252 	int error;
253 
254 	gp = pp->geom;
255 
256 	LIST_FOREACH(cp, &gp->consumer, consumer) {
257 		error = g_access(cp, dr, dw, de);
258 		if (error) {
259 			badcp = cp;
260 			goto fail;
261 		}
262 	}
263 	return (0);
264 
265 fail:
266 	LIST_FOREACH(cp, &gp->consumer, consumer) {
267 		if (cp == badcp)
268 			break;
269 		(void) g_access(cp, -dr, -dw, -de);
270 	}
271 	return (error);
272 }
273 
274 static struct g_geom *
275 g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md)
276 {
277 	struct g_multipath_softc *sc;
278 	struct g_geom *gp;
279 	struct g_provider *pp;
280 
281 	g_topology_assert();
282 
283 	LIST_FOREACH(gp, &mp->geom, geom) {
284 		if (strcmp(gp->name, md->md_name) == 0) {
285 			printf("GEOM_MULTIPATH: name %s already exists\n",
286 			    md->md_name);
287 			return (NULL);
288 		}
289 	}
290 
291 	gp = g_new_geomf(mp, md->md_name);
292 	if (gp == NULL)
293 		goto fail;
294 
295 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
296 	gp->softc = sc;
297 	gp->start = g_multipath_start;
298 	gp->orphan = g_multipath_orphan;
299 	gp->access = g_multipath_access;
300 	memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid));
301 	memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name));
302 
303 	pp = g_new_providerf(gp, "multipath/%s", md->md_name);
304 	if (pp == NULL)
305 		goto fail;
306 	/* limit the provider to not have it stomp on metadata */
307 	pp->mediasize = md->md_size - md->md_sectorsize;
308 	pp->sectorsize = md->md_sectorsize;
309 	sc->pp = pp;
310 	g_error_provider(pp, 0);
311 	return (gp);
312 fail:
313 	if (gp != NULL) {
314 		if (gp->softc != NULL)
315 			g_free(gp->softc);
316 		g_destroy_geom(gp);
317 	}
318 	return (NULL);
319 }
320 
321 static int
322 g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp)
323 {
324 	struct g_multipath_softc *sc;
325 	struct g_consumer *cp, *nxtcp;
326 	int error;
327 
328 	g_topology_assert();
329 
330 	sc = gp->softc;
331 	KASSERT(sc, ("no softc"));
332 
333 	/*
334 	 * Make sure that the passed provider isn't already attached
335 	 */
336 	LIST_FOREACH(cp, &gp->consumer, consumer) {
337 		if (cp->provider == pp)
338 			break;
339 	}
340 	if (cp) {
341 		printf("GEOM_MULTIPATH: provider %s already attached to %s\n",
342 		    pp->name, gp->name);
343 		return (EEXIST);
344 	}
345 	nxtcp = LIST_FIRST(&gp->consumer);
346 	cp = g_new_consumer(gp);
347 	if (cp == NULL)
348 		return (ENOMEM);
349 	error = g_attach(cp, pp);
350 	if (error != 0) {
351 		printf("GEOM_MULTIPATH: cannot attach %s to %s",
352 		    pp->name, sc->sc_name);
353 		g_destroy_consumer(cp);
354 		return (error);
355 	}
356 	cp->private = sc;
357 	cp->index = 0;
358 
359 	/*
360 	 * Set access permissions on new consumer to match other consumers
361 	 */
362 	if (nxtcp && (nxtcp->acr + nxtcp->acw +  nxtcp->ace)) {
363 		error = g_access(cp, nxtcp->acr, nxtcp->acw, nxtcp->ace);
364 		if (error) {
365 			printf("GEOM_MULTIPATH: cannot set access in "
366 			    "attaching %s to %s/%s (%d)\n",
367 			    pp->name, sc->sc_name, sc->sc_uuid, error);
368 			g_detach(cp);
369 			g_destroy_consumer(cp);
370 			return (error);
371 		}
372 	}
373 	printf("GEOM_MULTIPATH: adding %s to %s/%s\n",
374 	    pp->name, sc->sc_name, sc->sc_uuid);
375 	if (sc->cp_active == NULL) {
376 		sc->cp_active = cp;
377 		printf("GEOM_MULTIPATH: %s now active path in %s\n",
378 		    pp->name, sc->sc_name);
379 	}
380 	return (0);
381 }
382 
383 static int
384 g_multipath_destroy(struct g_geom *gp)
385 {
386 	struct g_provider *pp;
387 
388 	g_topology_assert();
389 	if (gp->softc == NULL)
390 		return (ENXIO);
391 	pp = LIST_FIRST(&gp->provider);
392 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0))
393 		return (EBUSY);
394 	printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
395 	g_free(gp->softc);
396 	gp->softc = NULL;
397 	g_wither_geom(gp, ENXIO);
398 	return (0);
399 }
400 
401 static int
402 g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
403     struct g_geom *gp)
404 {
405 
406 	return (g_multipath_destroy(gp));
407 }
408 
409 static void
410 g_multipath_init(struct g_class *mp)
411 {
412 	bioq_init(&gmtbq);
413 	mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
414 	if (kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt") == 0)
415 		g_multipath_kt_state = GKT_RUN;
416 }
417 
418 static void
419 g_multipath_fini(struct g_class *mp)
420 {
421 	if (g_multipath_kt_state == GKT_RUN) {
422 		mtx_lock(&gmtbq_mtx);
423 		g_multipath_kt_state = GKT_DIE;
424 		wakeup(&g_multipath_kt_state);
425 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
426 		    "gmp:fini", 0);
427 		mtx_unlock(&gmtbq_mtx);
428 	}
429 }
430 
431 static int
432 g_multipath_read_metadata(struct g_consumer *cp,
433     struct g_multipath_metadata *md)
434 {
435 	struct g_provider *pp;
436 	u_char *buf;
437 	int error;
438 
439 	g_topology_assert();
440 	error = g_access(cp, 1, 0, 0);
441 	if (error != 0)
442 		return (error);
443 	pp = cp->provider;
444 	g_topology_unlock();
445 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
446 	    pp->sectorsize, &error);
447 	g_topology_lock();
448 	g_access(cp, -1, 0, 0);
449 	if (buf == NULL)
450 		return (error);
451 	multipath_metadata_decode(buf, md);
452 	g_free(buf);
453 	return (0);
454 }
455 
456 static struct g_geom *
457 g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
458 {
459 	struct g_multipath_metadata md;
460 	struct g_multipath_softc *sc;
461 	struct g_consumer *cp;
462 	struct g_geom *gp, *gp1;
463 	int error, isnew;
464 
465 	g_topology_assert();
466 
467 	gp = g_new_geomf(mp, "multipath:taste");
468 	gp->start = g_multipath_start;
469 	gp->access = g_multipath_access;
470 	gp->orphan = g_multipath_orphan;
471 	cp = g_new_consumer(gp);
472 	g_attach(cp, pp);
473 	error = g_multipath_read_metadata(cp, &md);
474 	g_detach(cp);
475 	g_destroy_consumer(cp);
476 	g_destroy_geom(gp);
477 	if (error != 0)
478 		return (NULL);
479 	gp = NULL;
480 
481 	if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
482 		if (g_multipath_debug)
483 			printf("%s is not MULTIPATH\n", pp->name);
484 		return (NULL);
485 	}
486 	if (md.md_version != G_MULTIPATH_VERSION) {
487 		printf("%s has version %d multipath id- this module is version "
488 		    " %d: rejecting\n", pp->name, md.md_version,
489 		    G_MULTIPATH_VERSION);
490 		return (NULL);
491 	}
492 	if (g_multipath_debug)
493 		printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
494 
495 	/*
496 	 * Let's check if such a device already is present. We check against
497 	 * uuid alone first because that's the true distinguishor. If that
498 	 * passes, then we check for name conflicts. If there are conflicts,
499 	 * modify the name.
500 	 *
501 	 * The whole purpose of this is to solve the problem that people don't
502 	 * pick good unique names, but good unique names (like uuids) are a
503 	 * pain to use. So, we allow people to build GEOMs with friendly names
504 	 * and uuids, and modify the names in case there's a collision.
505 	 */
506 	sc = NULL;
507 	LIST_FOREACH(gp, &mp->geom, geom) {
508 		sc = gp->softc;
509 		if (sc == NULL)
510 			continue;
511 		if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
512 			break;
513 	}
514 
515 	LIST_FOREACH(gp1, &mp->geom, geom) {
516 		if (gp1 == gp)
517 			continue;
518 		sc = gp1->softc;
519 		if (sc == NULL)
520 			continue;
521 		if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
522 			break;
523 	}
524 
525 	/*
526 	 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
527 	 *
528 	 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
529 	 * with the same name (but a different UUID).
530 	 *
531 	 * If gp is NULL, then modify the name with a random number and
532   	 * complain, but allow the creation of the geom to continue.
533 	 *
534 	 * If gp is *not* NULL, just use the geom's name as we're attaching
535 	 * this disk to the (previously generated) name.
536 	 */
537 
538 	if (gp1) {
539 		sc = gp1->softc;
540 		if (gp == NULL) {
541 			char buf[16];
542 			u_long rand = random();
543 
544 			snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
545 			printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
546 			    sc->sc_name, sc->sc_uuid);
547 			printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
548 			    md.md_uuid, buf);
549 			strlcpy(md.md_name, buf, sizeof(md.md_name));
550 		} else {
551 			strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
552 		}
553 	}
554 
555 	if (gp == NULL) {
556 		gp = g_multipath_create(mp, &md);
557 		if (gp == NULL) {
558 			printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
559 			    md.md_name, md.md_uuid);
560 			return (NULL);
561 		}
562 		isnew = 1;
563 	} else {
564 		isnew = 0;
565 	}
566 
567 	sc = gp->softc;
568 	KASSERT(sc != NULL, ("sc is NULL"));
569 	error = g_multipath_add_disk(gp, pp);
570 	if (error != 0) {
571 		if (isnew)
572 			g_multipath_destroy(gp);
573 		return (NULL);
574 	}
575 	return (gp);
576 }
577 
578 static void
579 g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp)
580 {
581 	struct g_geom *gp;
582 	struct g_provider *pp0, *pp1;
583 	struct g_multipath_metadata md;
584 	const char *name, *mpname, *uuid;
585 	static const char devpf[6] = "/dev/";
586 	int *nargs, error;
587 
588 	g_topology_assert();
589 
590 	mpname = gctl_get_asciiparam(req, "arg0");
591         if (mpname == NULL) {
592                 gctl_error(req, "No 'arg0' argument");
593                 return;
594         }
595 
596 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
597 	if (nargs == NULL) {
598 		gctl_error(req, "No 'nargs' argument");
599 		return;
600 	}
601 	if (*nargs != 4) {
602 		gctl_error(req, "missing device or uuid arguments");
603 		return;
604 	}
605 
606 	name = gctl_get_asciiparam(req, "arg1");
607 	if (name == NULL) {
608 		gctl_error(req, "No 'arg1' argument");
609 		return;
610 	}
611 	if (strncmp(name, devpf, 5) == 0)
612 		name += 5;
613 	pp0 = g_provider_by_name(name);
614 	if (pp0 == NULL) {
615 		gctl_error(req, "Provider %s is invalid", name);
616 		return;
617 	}
618 
619 	name = gctl_get_asciiparam(req, "arg2");
620 	if (name == NULL) {
621 		gctl_error(req, "No 'arg2' argument");
622 		return;
623 	}
624 	if (strncmp(name, devpf, 5) == 0)
625 		name += 5;
626 	pp1 = g_provider_by_name(name);
627 	if (pp1 == NULL) {
628 		gctl_error(req, "Provider %s is invalid", name);
629 		return;
630 	}
631 
632 	uuid = gctl_get_asciiparam(req, "arg3");
633 	if (uuid == NULL) {
634 		gctl_error(req, "No uuid argument");
635 		return;
636 	}
637 	if (strlen(uuid) != 36) {
638 		gctl_error(req, "Malformed uuid argument");
639 		return;
640 	}
641 
642 	/*
643 	 * Check to make sure parameters from the two providers are the same
644 	 */
645 	if (pp0 == pp1) {
646 		gctl_error(req, "providers %s and %s are the same",
647 		    pp0->name, pp1->name);
648 		return;
649 	}
650     	if (pp0->mediasize != pp1->mediasize) {
651 		gctl_error(req, "Provider %s is %jd; Provider %s is %jd",
652 		    pp0->name, (intmax_t) pp0->mediasize,
653 		    pp1->name, (intmax_t) pp1->mediasize);
654 		return;
655 	}
656     	if (pp0->sectorsize != pp1->sectorsize) {
657 		gctl_error(req, "Provider %s has sectorsize %u; Provider %s "
658 		    "has sectorsize %u", pp0->name, pp0->sectorsize,
659 		    pp1->name, pp1->sectorsize);
660 		return;
661 	}
662 
663 	/*
664 	 * cons up enough of a metadata structure to use.
665 	 */
666 	memset(&md, 0, sizeof(md));
667 	md.md_size = pp0->mediasize;
668 	md.md_sectorsize = pp0->sectorsize;
669 	strlcpy(md.md_name, mpname, sizeof(md.md_name));
670 	strlcpy(md.md_uuid, uuid, sizeof(md.md_uuid));
671 
672 	gp = g_multipath_create(mp, &md);
673 	if (gp == NULL)
674 		return;
675 	error = g_multipath_add_disk(gp, pp0);
676 	if (error) {
677 		g_multipath_destroy(gp);
678 		return;
679 	}
680 	error = g_multipath_add_disk(gp, pp1);
681 	if (error) {
682 		g_multipath_destroy(gp);
683 		return;
684 	}
685 }
686 
687 static struct g_geom *
688 g_multipath_find_geom(struct g_class *mp, const char *name)
689 {
690 	struct g_geom *gp;
691 
692 	LIST_FOREACH(gp, &mp->geom, geom) {
693 		if (strcmp(gp->name, name) == 0) {
694 			return (gp);
695 		}
696 	}
697 	return (NULL);
698 }
699 
700 static void
701 g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
702 {
703 	struct g_geom *gp;
704 	const char *name;
705 	int error;
706 
707 	g_topology_assert();
708 
709 	name = gctl_get_asciiparam(req, "arg0");
710         if (name == NULL) {
711                 gctl_error(req, "No 'arg0' argument");
712                 return;
713         }
714 	gp = g_multipath_find_geom(mp, name);
715 	if (gp == NULL) {
716 		gctl_error(req, "Device %s is invalid", name);
717 		return;
718 	}
719 	error = g_multipath_destroy(gp);
720 	if (error != 0) {
721 		gctl_error(req, "failed to destroy %s (err=%d)", name, error);
722 	}
723 }
724 
725 static void
726 g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
727 {
728 	uint32_t *version;
729 	g_topology_assert();
730 	version = gctl_get_paraml(req, "version", sizeof(*version));
731 	if (version == NULL) {
732 		gctl_error(req, "No 'version' argument");
733 	} else if (*version != G_MULTIPATH_VERSION) {
734 		gctl_error(req, "Userland and kernel parts are out of sync");
735 	} else if (strcmp(verb, "create") == 0) {
736 		g_multipath_ctl_create(req, mp);
737 	} else if (strcmp(verb, "destroy") == 0) {
738 		g_multipath_ctl_destroy(req, mp);
739 	} else {
740 		gctl_error(req, "Unknown verb %s", verb);
741 	}
742 }
743 DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);
744