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