xref: /freebsd/sys/geom/multipath/g_multipath.c (revision 06c3fb27)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011-2013 Alexander Motin <mav@FreeBSD.org>
5  * Copyright (c) 2006-2007 Matthew Jacob <mjacob@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 /*
30  * Based upon work by Pawel Jakub Dawidek <pjd@FreeBSD.org> for all of the
31  * fine geom examples, and by Poul Henning Kamp <phk@FreeBSD.org> for GEOM
32  * itself, all of which is most gratefully acknowledged.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/limits.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/bio.h>
43 #include <sys/sbuf.h>
44 #include <sys/sdt.h>
45 #include <sys/sysctl.h>
46 #include <sys/kthread.h>
47 #include <sys/malloc.h>
48 #include <geom/geom.h>
49 #include <geom/multipath/g_multipath.h>
50 
51 FEATURE(geom_multipath, "GEOM multipath support");
52 
53 SYSCTL_DECL(_kern_geom);
54 static SYSCTL_NODE(_kern_geom, OID_AUTO, multipath,
55     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
56     "GEOM_MULTIPATH tunables");
57 static u_int g_multipath_debug = 0;
58 SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, debug, CTLFLAG_RW,
59     &g_multipath_debug, 0, "Debug level");
60 static u_int g_multipath_exclusive = 1;
61 SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, exclusive, CTLFLAG_RW,
62     &g_multipath_exclusive, 0, "Exclusively open providers");
63 
64 SDT_PROVIDER_DECLARE(geom);
65 SDT_PROBE_DEFINE2(geom, multipath, config, restore, "char*", "char*");
66 SDT_PROBE_DEFINE2(geom, multipath, config, remove, "char*", "char*");
67 SDT_PROBE_DEFINE2(geom, multipath, config, disconnect, "char*", "char*");
68 SDT_PROBE_DEFINE3(geom, multipath, config, fail, "char*", "char*", "int");
69 SDT_PROBE_DEFINE2(geom, multipath, config, taste, "char*", "char*");
70 SDT_PROBE_DEFINE2(geom, multipath, io, restart, "struct bio*", "struct bio*");
71 
72 static enum {
73 	GKT_NIL,
74 	GKT_RUN,
75 	GKT_DIE
76 } g_multipath_kt_state;
77 static struct bio_queue_head gmtbq;
78 static struct mtx gmtbq_mtx;
79 
80 static int g_multipath_read_metadata(struct g_consumer *cp,
81     struct g_multipath_metadata *md);
82 static int g_multipath_write_metadata(struct g_consumer *cp,
83     struct g_multipath_metadata *md);
84 
85 static void g_multipath_orphan(struct g_consumer *);
86 static void g_multipath_resize(struct g_consumer *);
87 static void g_multipath_start(struct bio *);
88 static void g_multipath_done(struct bio *);
89 static void g_multipath_done_error(struct bio *);
90 static void g_multipath_kt(void *);
91 
92 static int g_multipath_destroy(struct g_geom *);
93 static int
94 g_multipath_destroy_geom(struct gctl_req *, struct g_class *, struct g_geom *);
95 
96 static struct g_geom *g_multipath_find_geom(struct g_class *, const char *);
97 static int g_multipath_rotate(struct g_geom *);
98 
99 static g_taste_t g_multipath_taste;
100 static g_ctl_req_t g_multipath_config;
101 static g_init_t g_multipath_init;
102 static g_fini_t g_multipath_fini;
103 static g_dumpconf_t g_multipath_dumpconf;
104 
105 struct g_class g_multipath_class = {
106 	.name		= G_MULTIPATH_CLASS_NAME,
107 	.version	= G_VERSION,
108 	.ctlreq		= g_multipath_config,
109 	.taste		= g_multipath_taste,
110 	.destroy_geom	= g_multipath_destroy_geom,
111 	.init		= g_multipath_init,
112 	.fini		= g_multipath_fini
113 };
114 
115 #define	MP_FAIL		0x00000001
116 #define	MP_LOST		0x00000002
117 #define	MP_NEW		0x00000004
118 #define	MP_POSTED	0x00000008
119 #define	MP_BAD		(MP_FAIL | MP_LOST | MP_NEW)
120 #define	MP_WITHER	0x00000010
121 #define	MP_IDLE		0x00000020
122 #define	MP_IDLE_MASK	0xffffffe0
123 
124 static int
125 g_multipath_good(struct g_geom *gp)
126 {
127 	struct g_consumer *cp;
128 	int n = 0;
129 
130 	LIST_FOREACH(cp, &gp->consumer, consumer) {
131 		if ((cp->index & MP_BAD) == 0)
132 			n++;
133 	}
134 	return (n);
135 }
136 
137 static void
138 g_multipath_fault(struct g_consumer *cp, int cause)
139 {
140 	struct g_multipath_softc *sc;
141 	struct g_consumer *lcp;
142 	struct g_geom *gp;
143 
144 	gp = cp->geom;
145 	sc = gp->softc;
146 	cp->index |= cause;
147 	if (g_multipath_good(gp) == 0 && sc->sc_ndisks > 0) {
148 		LIST_FOREACH(lcp, &gp->consumer, consumer) {
149 			if (lcp->provider == NULL ||
150 			    (lcp->index & (MP_LOST | MP_NEW)))
151 				continue;
152 			if (sc->sc_ndisks > 1 && lcp == cp)
153 				continue;
154 			printf("GEOM_MULTIPATH: "
155 			    "all paths in %s were marked FAIL, restore %s\n",
156 			    sc->sc_name, lcp->provider->name);
157 			SDT_PROBE2(geom, multipath, config, restore,
158 			    sc->sc_name, lcp->provider->name);
159 			lcp->index &= ~MP_FAIL;
160 		}
161 	}
162 	if (cp != sc->sc_active)
163 		return;
164 	sc->sc_active = NULL;
165 	LIST_FOREACH(lcp, &gp->consumer, consumer) {
166 		if ((lcp->index & MP_BAD) == 0) {
167 			sc->sc_active = lcp;
168 			break;
169 		}
170 	}
171 	if (sc->sc_active == NULL) {
172 		printf("GEOM_MULTIPATH: out of providers for %s\n",
173 		    sc->sc_name);
174 	} else if (sc->sc_active_active != 1) {
175 		printf("GEOM_MULTIPATH: %s is now active path in %s\n",
176 		    sc->sc_active->provider->name, sc->sc_name);
177 	}
178 }
179 
180 static struct g_consumer *
181 g_multipath_choose(struct g_geom *gp, struct bio *bp)
182 {
183 	struct g_multipath_softc *sc;
184 	struct g_consumer *best, *cp;
185 
186 	sc = gp->softc;
187 	if (sc->sc_active_active == 0 ||
188 	    (sc->sc_active_active == 2 && bp->bio_cmd != BIO_READ))
189 		return (sc->sc_active);
190 	best = NULL;
191 	LIST_FOREACH(cp, &gp->consumer, consumer) {
192 		if (cp->index & MP_BAD)
193 			continue;
194 		cp->index += MP_IDLE;
195 		if (best == NULL || cp->private < best->private ||
196 		    (cp->private == best->private && cp->index > best->index))
197 			best = cp;
198 	}
199 	if (best != NULL)
200 		best->index &= ~MP_IDLE_MASK;
201 	return (best);
202 }
203 
204 static void
205 g_mpd(void *arg, int flags __unused)
206 {
207 	struct g_geom *gp;
208 	struct g_multipath_softc *sc;
209 	struct g_consumer *cp;
210 	int w;
211 
212 	g_topology_assert();
213 	cp = arg;
214 	gp = cp->geom;
215 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) {
216 		w = cp->acw;
217 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
218 		if (w > 0 && cp->provider != NULL &&
219 		    (cp->provider->geom->flags & G_GEOM_WITHER) == 0) {
220 			cp->index |= MP_WITHER;
221 			g_post_event(g_mpd, cp, M_WAITOK, NULL);
222 			return;
223 		}
224 	}
225 	sc = gp->softc;
226 	mtx_lock(&sc->sc_mtx);
227 	if (cp->provider) {
228 		printf("GEOM_MULTIPATH: %s removed from %s\n",
229 		    cp->provider->name, gp->name);
230 		SDT_PROBE2(geom, multipath, config, remove,
231 		    gp->name, cp->provider->name);
232 		g_detach(cp);
233 	}
234 	g_destroy_consumer(cp);
235 	mtx_unlock(&sc->sc_mtx);
236 	if (LIST_EMPTY(&gp->consumer))
237 		g_multipath_destroy(gp);
238 }
239 
240 static void
241 g_multipath_orphan(struct g_consumer *cp)
242 {
243 	struct g_multipath_softc *sc;
244 	uintptr_t *cnt;
245 
246 	g_topology_assert();
247 	printf("GEOM_MULTIPATH: %s in %s was disconnected\n",
248 	    cp->provider->name, cp->geom->name);
249 	SDT_PROBE2(geom, multipath, config, disconnect,
250 	    cp->geom->name, cp->provider->name);
251 	sc = cp->geom->softc;
252 	cnt = (uintptr_t *)&cp->private;
253 	mtx_lock(&sc->sc_mtx);
254 	sc->sc_ndisks--;
255 	g_multipath_fault(cp, MP_LOST);
256 	if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
257 		cp->index |= MP_POSTED;
258 		mtx_unlock(&sc->sc_mtx);
259 		g_mpd(cp, 0);
260 	} else
261 		mtx_unlock(&sc->sc_mtx);
262 }
263 
264 static void
265 g_multipath_resize(struct g_consumer *cp)
266 {
267 	struct g_multipath_softc *sc;
268 	struct g_geom *gp;
269 	struct g_consumer *cp1;
270 	struct g_provider *pp;
271 	struct g_multipath_metadata md;
272 	off_t size, psize, ssize;
273 	int error;
274 
275 	g_topology_assert();
276 
277 	gp = cp->geom;
278 	pp = cp->provider;
279 	sc = gp->softc;
280 
281 	if (sc->sc_stopping)
282 		return;
283 
284 	if (pp->mediasize < sc->sc_size) {
285 		size = pp->mediasize;
286 		ssize = pp->sectorsize;
287 	} else {
288 		size = ssize = OFF_MAX;
289 		mtx_lock(&sc->sc_mtx);
290 		LIST_FOREACH(cp1, &gp->consumer, consumer) {
291 			pp = cp1->provider;
292 			if (pp == NULL)
293 				continue;
294 			if (pp->mediasize < size) {
295 				size = pp->mediasize;
296 				ssize = pp->sectorsize;
297 			}
298 		}
299 		mtx_unlock(&sc->sc_mtx);
300 		if (size == OFF_MAX || size == sc->sc_size)
301 			return;
302 	}
303 	psize = size - ((sc->sc_uuid[0] != 0) ? ssize : 0);
304 	printf("GEOM_MULTIPATH: %s size changed from %jd to %jd\n",
305 	    sc->sc_name, sc->sc_pp->mediasize, psize);
306 	if (sc->sc_uuid[0] != 0 && size < sc->sc_size) {
307 		error = g_multipath_read_metadata(cp, &md);
308 		if (error ||
309 		    (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) ||
310 		    (memcmp(md.md_uuid, sc->sc_uuid, sizeof(sc->sc_uuid)) != 0) ||
311 		    (strcmp(md.md_name, sc->sc_name) != 0) ||
312 		    (md.md_size != 0 && md.md_size != size) ||
313 		    (md.md_sectorsize != 0 && md.md_sectorsize != ssize)) {
314 			g_multipath_destroy(gp);
315 			return;
316 		}
317 	}
318 	sc->sc_size = size;
319 	g_resize_provider(sc->sc_pp, psize);
320 
321 	if (sc->sc_uuid[0] != 0) {
322 		pp = cp->provider;
323 		strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
324 		memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
325 		strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
326 		md.md_version = G_MULTIPATH_VERSION;
327 		md.md_size = size;
328 		md.md_sectorsize = ssize;
329 		md.md_active_active = sc->sc_active_active;
330 		error = g_multipath_write_metadata(cp, &md);
331 		if (error != 0)
332 			printf("GEOM_MULTIPATH: Can't update metadata on %s "
333 			    "(%d)\n", pp->name, error);
334 	}
335 }
336 
337 static void
338 g_multipath_start(struct bio *bp)
339 {
340 	struct g_multipath_softc *sc;
341 	struct g_geom *gp;
342 	struct g_consumer *cp;
343 	struct bio *cbp;
344 	uintptr_t *cnt;
345 
346 	gp = bp->bio_to->geom;
347 	sc = gp->softc;
348 	KASSERT(sc != NULL, ("NULL sc"));
349 	cbp = g_clone_bio(bp);
350 	if (cbp == NULL) {
351 		g_io_deliver(bp, ENOMEM);
352 		return;
353 	}
354 	mtx_lock(&sc->sc_mtx);
355 	cp = g_multipath_choose(gp, bp);
356 	if (cp == NULL) {
357 		mtx_unlock(&sc->sc_mtx);
358 		g_destroy_bio(cbp);
359 		g_io_deliver(bp, ENXIO);
360 		return;
361 	}
362 	if ((uintptr_t)bp->bio_driver1 < sc->sc_ndisks)
363 		bp->bio_driver1 = (void *)(uintptr_t)sc->sc_ndisks;
364 	cnt = (uintptr_t *)&cp->private;
365 	(*cnt)++;
366 	mtx_unlock(&sc->sc_mtx);
367 	cbp->bio_done = g_multipath_done;
368 	g_io_request(cbp, cp);
369 }
370 
371 static void
372 g_multipath_done(struct bio *bp)
373 {
374 	struct g_multipath_softc *sc;
375 	struct g_consumer *cp;
376 	uintptr_t *cnt;
377 
378 	if (bp->bio_error == ENXIO || bp->bio_error == EIO) {
379 		mtx_lock(&gmtbq_mtx);
380 		bioq_insert_tail(&gmtbq, bp);
381 		mtx_unlock(&gmtbq_mtx);
382 		wakeup(&g_multipath_kt_state);
383 	} else {
384 		cp = bp->bio_from;
385 		sc = cp->geom->softc;
386 		cnt = (uintptr_t *)&cp->private;
387 		mtx_lock(&sc->sc_mtx);
388 		(*cnt)--;
389 		if (*cnt == 0 && (cp->index & MP_LOST)) {
390 			if (g_post_event(g_mpd, cp, M_NOWAIT, NULL) == 0)
391 				cp->index |= MP_POSTED;
392 			mtx_unlock(&sc->sc_mtx);
393 		} else
394 			mtx_unlock(&sc->sc_mtx);
395 		if (bp->bio_error == 0 &&
396 			bp->bio_cmd == BIO_GETATTR &&
397 			!strcmp(bp->bio_attribute, "GEOM::physpath"))
398 		{
399 			strlcat(bp->bio_data, "/mp", bp->bio_length);
400 		}
401 		g_std_done(bp);
402 	}
403 }
404 
405 static void
406 g_multipath_done_error(struct bio *bp)
407 {
408 	struct bio *pbp;
409 	struct g_geom *gp;
410 	struct g_multipath_softc *sc;
411 	struct g_consumer *cp;
412 	struct g_provider *pp;
413 	uintptr_t *cnt;
414 
415 	/*
416 	 * If we had a failure, we have to check first to see
417 	 * whether the consumer it failed on was the currently
418 	 * active consumer (i.e., this is the first in perhaps
419 	 * a number of failures). If so, we then switch consumers
420 	 * to the next available consumer.
421 	 */
422 
423 	pbp = bp->bio_parent;
424 	gp = pbp->bio_to->geom;
425 	sc = gp->softc;
426 	cp = bp->bio_from;
427 	pp = cp->provider;
428 	cnt = (uintptr_t *)&cp->private;
429 
430 	mtx_lock(&sc->sc_mtx);
431 	if ((cp->index & MP_FAIL) == 0) {
432 		printf("GEOM_MULTIPATH: Error %d, %s in %s marked FAIL\n",
433 		    bp->bio_error, pp->name, sc->sc_name);
434 		SDT_PROBE3(geom, multipath, config, fail,
435 		    sc->sc_name, pp->name, bp->bio_error);
436 		g_multipath_fault(cp, MP_FAIL);
437 	}
438 	(*cnt)--;
439 	if (*cnt == 0 && (cp->index & (MP_LOST | MP_POSTED)) == MP_LOST) {
440 		cp->index |= MP_POSTED;
441 		mtx_unlock(&sc->sc_mtx);
442 		g_post_event(g_mpd, cp, M_WAITOK, NULL);
443 	} else
444 		mtx_unlock(&sc->sc_mtx);
445 
446 	/*
447 	 * If we can fruitfully restart the I/O, do so.
448 	 */
449 	if (pbp->bio_children < (uintptr_t)pbp->bio_driver1) {
450 		pbp->bio_inbed++;
451 		SDT_PROBE2(geom, multipath, io, restart, bp, pbp);
452 		g_destroy_bio(bp);
453 		g_multipath_start(pbp);
454 	} else {
455 		g_std_done(bp);
456 	}
457 }
458 
459 static void
460 g_multipath_kt(void *arg)
461 {
462 
463 	g_multipath_kt_state = GKT_RUN;
464 	mtx_lock(&gmtbq_mtx);
465 	while (g_multipath_kt_state == GKT_RUN) {
466 		for (;;) {
467 			struct bio *bp;
468 
469 			bp = bioq_takefirst(&gmtbq);
470 			if (bp == NULL)
471 				break;
472 			mtx_unlock(&gmtbq_mtx);
473 			g_multipath_done_error(bp);
474 			mtx_lock(&gmtbq_mtx);
475 		}
476 		if (g_multipath_kt_state != GKT_RUN)
477 			break;
478 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
479 		    "gkt:wait", 0);
480 	}
481 	mtx_unlock(&gmtbq_mtx);
482 	wakeup(&g_multipath_kt_state);
483 	kproc_exit(0);
484 }
485 
486 static int
487 g_multipath_access(struct g_provider *pp, int dr, int dw, int de)
488 {
489 	struct g_geom *gp;
490 	struct g_consumer *cp, *badcp = NULL;
491 	struct g_multipath_softc *sc;
492 	int error;
493 
494 	gp = pp->geom;
495 
496 	/* Error used if we have no valid consumers. */
497 	error = (dr > 0 || dw > 0 || de > 0) ? ENXIO : 0;
498 
499 	LIST_FOREACH(cp, &gp->consumer, consumer) {
500 		if (cp->index & MP_WITHER)
501 			continue;
502 
503 		error = g_access(cp, dr, dw, de);
504 		if (error) {
505 			badcp = cp;
506 			goto fail;
507 		}
508 	}
509 
510 	if (error != 0)
511 		return (error);
512 
513 	sc = gp->softc;
514 	sc->sc_opened += dr + dw + de;
515 	if (sc->sc_stopping && sc->sc_opened == 0)
516 		g_multipath_destroy(gp);
517 
518 	return (0);
519 
520 fail:
521 	LIST_FOREACH(cp, &gp->consumer, consumer) {
522 		if (cp == badcp)
523 			break;
524 		if (cp->index & MP_WITHER)
525 			continue;
526 
527 		(void) g_access(cp, -dr, -dw, -de);
528 	}
529 	return (error);
530 }
531 
532 static struct g_geom *
533 g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md)
534 {
535 	struct g_multipath_softc *sc;
536 	struct g_geom *gp;
537 	struct g_provider *pp;
538 
539 	g_topology_assert();
540 
541 	LIST_FOREACH(gp, &mp->geom, geom) {
542 		sc = gp->softc;
543 		if (sc == NULL || sc->sc_stopping)
544 			continue;
545 		if (strcmp(gp->name, md->md_name) == 0) {
546 			printf("GEOM_MULTIPATH: name %s already exists\n",
547 			    md->md_name);
548 			return (NULL);
549 		}
550 	}
551 
552 	gp = g_new_geomf(mp, "%s", md->md_name);
553 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
554 	mtx_init(&sc->sc_mtx, "multipath", NULL, MTX_DEF);
555 	memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid));
556 	memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name));
557 	sc->sc_active_active = md->md_active_active;
558 	sc->sc_size = md->md_size;
559 	gp->softc = sc;
560 	gp->start = g_multipath_start;
561 	gp->orphan = g_multipath_orphan;
562 	gp->resize = g_multipath_resize;
563 	gp->access = g_multipath_access;
564 	gp->dumpconf = g_multipath_dumpconf;
565 
566 	pp = g_new_providerf(gp, "multipath/%s", md->md_name);
567 	pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
568 	if (md->md_size != 0) {
569 		pp->mediasize = md->md_size -
570 		    ((md->md_uuid[0] != 0) ? md->md_sectorsize : 0);
571 		pp->sectorsize = md->md_sectorsize;
572 	}
573 	sc->sc_pp = pp;
574 	g_error_provider(pp, 0);
575 	printf("GEOM_MULTIPATH: %s created\n", gp->name);
576 	return (gp);
577 }
578 
579 static int
580 g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp)
581 {
582 	struct g_multipath_softc *sc;
583 	struct g_consumer *cp;
584 	int error, acr, acw, ace;
585 
586 	g_topology_assert();
587 
588 	sc = gp->softc;
589 	KASSERT(sc, ("no softc"));
590 
591 	/*
592 	 * Make sure that the passed provider isn't already attached
593 	 */
594 	LIST_FOREACH(cp, &gp->consumer, consumer) {
595 		if (cp->provider == pp)
596 			break;
597 	}
598 	if (cp) {
599 		printf("GEOM_MULTIPATH: provider %s already attached to %s\n",
600 		    pp->name, gp->name);
601 		return (EEXIST);
602 	}
603 	cp = g_new_consumer(gp);
604 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
605 	cp->private = NULL;
606 	cp->index = MP_NEW;
607 	error = g_attach(cp, pp);
608 	if (error != 0) {
609 		printf("GEOM_MULTIPATH: cannot attach %s to %s",
610 		    pp->name, sc->sc_name);
611 		g_destroy_consumer(cp);
612 		return (error);
613 	}
614 
615 	/*
616 	 * Set access permissions on new consumer to match other consumers
617 	 */
618 	if (sc->sc_pp) {
619 		acr = sc->sc_pp->acr;
620 		acw = sc->sc_pp->acw;
621 		ace = sc->sc_pp->ace;
622 	} else
623 		acr = acw = ace = 0;
624 	if (g_multipath_exclusive) {
625 		acr++;
626 		acw++;
627 		ace++;
628 	}
629 	error = g_access(cp, acr, acw, ace);
630 	if (error) {
631 		printf("GEOM_MULTIPATH: cannot set access in "
632 		    "attaching %s to %s (%d)\n",
633 		    pp->name, sc->sc_name, error);
634 		g_detach(cp);
635 		g_destroy_consumer(cp);
636 		return (error);
637 	}
638 	if (sc->sc_size == 0) {
639 		sc->sc_size = pp->mediasize -
640 		    ((sc->sc_uuid[0] != 0) ? pp->sectorsize : 0);
641 		sc->sc_pp->mediasize = sc->sc_size;
642 		sc->sc_pp->sectorsize = pp->sectorsize;
643 	}
644 	if (sc->sc_pp->stripesize == 0 && sc->sc_pp->stripeoffset == 0) {
645 		sc->sc_pp->stripesize = pp->stripesize;
646 		sc->sc_pp->stripeoffset = pp->stripeoffset;
647 	}
648 	sc->sc_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
649 	mtx_lock(&sc->sc_mtx);
650 	cp->index = 0;
651 	sc->sc_ndisks++;
652 	mtx_unlock(&sc->sc_mtx);
653 	printf("GEOM_MULTIPATH: %s added to %s\n",
654 	    pp->name, sc->sc_name);
655 	if (sc->sc_active == NULL) {
656 		sc->sc_active = cp;
657 		if (sc->sc_active_active != 1)
658 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
659 			    pp->name, sc->sc_name);
660 	}
661 	return (0);
662 }
663 
664 static int
665 g_multipath_destroy(struct g_geom *gp)
666 {
667 	struct g_multipath_softc *sc;
668 	struct g_consumer *cp, *cp1;
669 
670 	g_topology_assert();
671 	if (gp->softc == NULL)
672 		return (ENXIO);
673 	sc = gp->softc;
674 	if (!sc->sc_stopping) {
675 		printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
676 		sc->sc_stopping = 1;
677 	}
678 	if (sc->sc_opened != 0) {
679 		g_wither_provider(sc->sc_pp, ENXIO);
680 		sc->sc_pp = NULL;
681 		return (EINPROGRESS);
682 	}
683 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
684 		mtx_lock(&sc->sc_mtx);
685 		if ((cp->index & MP_POSTED) == 0) {
686 			cp->index |= MP_POSTED;
687 			mtx_unlock(&sc->sc_mtx);
688 			g_mpd(cp, 0);
689 			if (cp1 == NULL)
690 				return(0);	/* Recursion happened. */
691 		} else
692 			mtx_unlock(&sc->sc_mtx);
693 	}
694 	if (!LIST_EMPTY(&gp->consumer))
695 		return (EINPROGRESS);
696 	mtx_destroy(&sc->sc_mtx);
697 	g_free(gp->softc);
698 	gp->softc = NULL;
699 	printf("GEOM_MULTIPATH: %s destroyed\n", gp->name);
700 	g_wither_geom(gp, ENXIO);
701 	return (0);
702 }
703 
704 static int
705 g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
706     struct g_geom *gp)
707 {
708 
709 	return (g_multipath_destroy(gp));
710 }
711 
712 static int
713 g_multipath_rotate(struct g_geom *gp)
714 {
715 	struct g_consumer *lcp, *first_good_cp = NULL;
716 	struct g_multipath_softc *sc = gp->softc;
717 	int active_cp_seen = 0;
718 
719 	g_topology_assert();
720 	if (sc == NULL)
721 		return (ENXIO);
722 	LIST_FOREACH(lcp, &gp->consumer, consumer) {
723 		if ((lcp->index & MP_BAD) == 0) {
724 			if (first_good_cp == NULL)
725 				first_good_cp = lcp;
726 			if (active_cp_seen)
727 				break;
728 		}
729 		if (sc->sc_active == lcp)
730 			active_cp_seen = 1;
731 	}
732 	if (lcp == NULL)
733 		lcp = first_good_cp;
734 	if (lcp && lcp != sc->sc_active) {
735 		sc->sc_active = lcp;
736 		if (sc->sc_active_active != 1)
737 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
738 			    lcp->provider->name, sc->sc_name);
739 	}
740 	return (0);
741 }
742 
743 static void
744 g_multipath_init(struct g_class *mp)
745 {
746 	bioq_init(&gmtbq);
747 	mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
748 	kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt");
749 }
750 
751 static void
752 g_multipath_fini(struct g_class *mp)
753 {
754 	if (g_multipath_kt_state == GKT_RUN) {
755 		mtx_lock(&gmtbq_mtx);
756 		g_multipath_kt_state = GKT_DIE;
757 		wakeup(&g_multipath_kt_state);
758 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
759 		    "gmp:fini", 0);
760 		mtx_unlock(&gmtbq_mtx);
761 	}
762 }
763 
764 static int
765 g_multipath_read_metadata(struct g_consumer *cp,
766     struct g_multipath_metadata *md)
767 {
768 	struct g_provider *pp;
769 	u_char *buf;
770 	int error;
771 
772 	g_topology_assert();
773 	error = g_access(cp, 1, 0, 0);
774 	if (error != 0)
775 		return (error);
776 	pp = cp->provider;
777 	g_topology_unlock();
778 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
779 	    pp->sectorsize, &error);
780 	g_topology_lock();
781 	g_access(cp, -1, 0, 0);
782 	if (buf == NULL)
783 		return (error);
784 	multipath_metadata_decode(buf, md);
785 	g_free(buf);
786 	return (0);
787 }
788 
789 static int
790 g_multipath_write_metadata(struct g_consumer *cp,
791     struct g_multipath_metadata *md)
792 {
793 	struct g_provider *pp;
794 	u_char *buf;
795 	int error;
796 
797 	g_topology_assert();
798 	error = g_access(cp, 1, 1, 1);
799 	if (error != 0)
800 		return (error);
801 	pp = cp->provider;
802 	g_topology_unlock();
803 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
804 	multipath_metadata_encode(md, buf);
805 	error = g_write_data(cp, pp->mediasize - pp->sectorsize,
806 	    buf, pp->sectorsize);
807 	g_topology_lock();
808 	g_access(cp, -1, -1, -1);
809 	g_free(buf);
810 	return (error);
811 }
812 
813 static struct g_geom *
814 g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
815 {
816 	struct g_multipath_metadata md;
817 	struct g_multipath_softc *sc;
818 	struct g_consumer *cp;
819 	struct g_geom *gp, *gp1;
820 	int error, isnew;
821 
822 	g_topology_assert();
823 
824 	gp = g_new_geomf(mp, "multipath:taste");
825 	gp->start = g_multipath_start;
826 	gp->access = g_multipath_access;
827 	gp->orphan = g_multipath_orphan;
828 	cp = g_new_consumer(gp);
829 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
830 	error = g_attach(cp, pp);
831 	if (error == 0) {
832 		error = g_multipath_read_metadata(cp, &md);
833 		g_detach(cp);
834 	}
835 	g_destroy_consumer(cp);
836 	g_destroy_geom(gp);
837 	if (error != 0)
838 		return (NULL);
839 	gp = NULL;
840 
841 	if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
842 		if (g_multipath_debug)
843 			printf("%s is not MULTIPATH\n", pp->name);
844 		return (NULL);
845 	}
846 	if (md.md_version != G_MULTIPATH_VERSION) {
847 		printf("%s has version %d multipath id- this module is version "
848 		    " %d: rejecting\n", pp->name, md.md_version,
849 		    G_MULTIPATH_VERSION);
850 		return (NULL);
851 	}
852 	if (md.md_size != 0 && md.md_size != pp->mediasize)
853 		return (NULL);
854 	if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize)
855 		return (NULL);
856 	if (g_multipath_debug)
857 		printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
858 	SDT_PROBE2(geom, multipath, config, taste, md.md_name, md.md_uuid);
859 
860 	/*
861 	 * Let's check if such a device already is present. We check against
862 	 * uuid alone first because that's the true distinguishor. If that
863 	 * passes, then we check for name conflicts. If there are conflicts,
864 	 * modify the name.
865 	 *
866 	 * The whole purpose of this is to solve the problem that people don't
867 	 * pick good unique names, but good unique names (like uuids) are a
868 	 * pain to use. So, we allow people to build GEOMs with friendly names
869 	 * and uuids, and modify the names in case there's a collision.
870 	 */
871 	sc = NULL;
872 	LIST_FOREACH(gp, &mp->geom, geom) {
873 		sc = gp->softc;
874 		if (sc == NULL || sc->sc_stopping)
875 			continue;
876 		if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
877 			break;
878 	}
879 
880 	LIST_FOREACH(gp1, &mp->geom, geom) {
881 		if (gp1 == gp)
882 			continue;
883 		sc = gp1->softc;
884 		if (sc == NULL || sc->sc_stopping)
885 			continue;
886 		if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
887 			break;
888 	}
889 
890 	/*
891 	 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
892 	 *
893 	 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
894 	 * with the same name (but a different UUID).
895 	 *
896 	 * If gp is NULL, then modify the name with a random number and
897   	 * complain, but allow the creation of the geom to continue.
898 	 *
899 	 * If gp is *not* NULL, just use the geom's name as we're attaching
900 	 * this disk to the (previously generated) name.
901 	 */
902 
903 	if (gp1) {
904 		sc = gp1->softc;
905 		if (gp == NULL) {
906 			char buf[16];
907 			u_long rand = random();
908 
909 			snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
910 			printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
911 			    sc->sc_name, sc->sc_uuid);
912 			printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
913 			    md.md_uuid, buf);
914 			strlcpy(md.md_name, buf, sizeof(md.md_name));
915 		} else {
916 			strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
917 		}
918 	}
919 
920 	if (gp == NULL) {
921 		gp = g_multipath_create(mp, &md);
922 		if (gp == NULL) {
923 			printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
924 			    md.md_name, md.md_uuid);
925 			return (NULL);
926 		}
927 		isnew = 1;
928 	} else {
929 		isnew = 0;
930 	}
931 
932 	sc = gp->softc;
933 	KASSERT(sc != NULL, ("sc is NULL"));
934 	error = g_multipath_add_disk(gp, pp);
935 	if (error != 0) {
936 		if (isnew)
937 			g_multipath_destroy(gp);
938 		return (NULL);
939 	}
940 	return (gp);
941 }
942 
943 static void
944 g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp,
945     const char *name)
946 {
947 	struct g_multipath_softc *sc;
948 	struct g_geom *gp;
949 	struct g_consumer *cp;
950 	struct g_provider *pp;
951 	const char *mpname;
952 	static const char devpf[6] = _PATH_DEV;
953 	int error;
954 
955 	g_topology_assert();
956 
957 	mpname = gctl_get_asciiparam(req, "arg0");
958         if (mpname == NULL) {
959                 gctl_error(req, "No 'arg0' argument");
960                 return;
961         }
962 	gp = g_multipath_find_geom(mp, mpname);
963 	if (gp == NULL) {
964 		gctl_error(req, "Device %s is invalid", mpname);
965 		return;
966 	}
967 	sc = gp->softc;
968 
969 	if (strncmp(name, devpf, 5) == 0)
970 		name += 5;
971 	pp = g_provider_by_name(name);
972 	if (pp == NULL) {
973 		gctl_error(req, "Provider %s is invalid", name);
974 		return;
975 	}
976 
977 	/*
978 	 * Check to make sure parameters match.
979 	 */
980 	LIST_FOREACH(cp, &gp->consumer, consumer) {
981 		if (cp->provider == pp) {
982 			gctl_error(req, "provider %s is already there",
983 			    pp->name);
984 			return;
985 		}
986 	}
987 	if (sc->sc_pp->mediasize != 0 &&
988 	    sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0)
989 	     != pp->mediasize) {
990 		gctl_error(req, "Providers size mismatch %jd != %jd",
991 		    (intmax_t) sc->sc_pp->mediasize +
992 			(sc->sc_uuid[0] != 0 ? pp->sectorsize : 0),
993 		    (intmax_t) pp->mediasize);
994 		return;
995 	}
996 	if (sc->sc_pp->sectorsize != 0 &&
997 	    sc->sc_pp->sectorsize != pp->sectorsize) {
998 		gctl_error(req, "Providers sectorsize mismatch %u != %u",
999 		    sc->sc_pp->sectorsize, pp->sectorsize);
1000 		return;
1001 	}
1002 
1003 	error = g_multipath_add_disk(gp, pp);
1004 	if (error != 0)
1005 		gctl_error(req, "Provider addition error: %d", error);
1006 }
1007 
1008 static void
1009 g_multipath_ctl_prefer(struct gctl_req *req, struct g_class *mp)
1010 {
1011 	struct g_geom *gp;
1012 	struct g_multipath_softc *sc;
1013 	struct g_consumer *cp;
1014 	const char *name, *mpname;
1015 	static const char devpf[6] = _PATH_DEV;
1016 	int *nargs;
1017 
1018 	g_topology_assert();
1019 
1020 	mpname = gctl_get_asciiparam(req, "arg0");
1021         if (mpname == NULL) {
1022                 gctl_error(req, "No 'arg0' argument");
1023                 return;
1024         }
1025 	gp = g_multipath_find_geom(mp, mpname);
1026 	if (gp == NULL) {
1027 		gctl_error(req, "Device %s is invalid", mpname);
1028 		return;
1029 	}
1030 	sc = gp->softc;
1031 
1032 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1033 	if (nargs == NULL) {
1034 		gctl_error(req, "No 'nargs' argument");
1035 		return;
1036 	}
1037 	if (*nargs != 2) {
1038 		gctl_error(req, "missing device");
1039 		return;
1040 	}
1041 
1042 	name = gctl_get_asciiparam(req, "arg1");
1043 	if (name == NULL) {
1044 		gctl_error(req, "No 'arg1' argument");
1045 		return;
1046 	}
1047 	if (strncmp(name, devpf, 5) == 0) {
1048 		name += 5;
1049 	}
1050 
1051 	LIST_FOREACH(cp, &gp->consumer, consumer) {
1052 		if (cp->provider != NULL
1053                       && strcmp(cp->provider->name, name) == 0)
1054 		    break;
1055 	}
1056 
1057 	if (cp == NULL) {
1058 		gctl_error(req, "Provider %s not found", name);
1059 		return;
1060 	}
1061 
1062 	mtx_lock(&sc->sc_mtx);
1063 
1064 	if (cp->index & MP_BAD) {
1065 		gctl_error(req, "Consumer %s is invalid", name);
1066 		mtx_unlock(&sc->sc_mtx);
1067 		return;
1068 	}
1069 
1070 	/* Here when the consumer is present and in good shape */
1071 
1072 	sc->sc_active = cp;
1073 	if (!sc->sc_active_active)
1074 	    printf("GEOM_MULTIPATH: %s now active path in %s\n",
1075 		sc->sc_active->provider->name, sc->sc_name);
1076 
1077 	mtx_unlock(&sc->sc_mtx);
1078 }
1079 
1080 static void
1081 g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp)
1082 {
1083 	struct g_geom *gp;
1084 	const char *mpname, *name;
1085 
1086 	mpname = gctl_get_asciiparam(req, "arg0");
1087         if (mpname == NULL) {
1088                 gctl_error(req, "No 'arg0' argument");
1089                 return;
1090         }
1091 	gp = g_multipath_find_geom(mp, mpname);
1092 	if (gp == NULL) {
1093 		gctl_error(req, "Device %s not found", mpname);
1094 		return;
1095 	}
1096 
1097 	name = gctl_get_asciiparam(req, "arg1");
1098 	if (name == NULL) {
1099 		gctl_error(req, "No 'arg1' argument");
1100 		return;
1101 	}
1102 	g_multipath_ctl_add_name(req, mp, name);
1103 }
1104 
1105 static void
1106 g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp)
1107 {
1108 	struct g_multipath_metadata md;
1109 	struct g_multipath_softc *sc;
1110 	struct g_geom *gp;
1111 	const char *mpname, *name;
1112 	char param[16];
1113 	int *nargs, i, *val;
1114 
1115 	g_topology_assert();
1116 
1117 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1118 	if (*nargs < 2) {
1119 		gctl_error(req, "wrong number of arguments.");
1120 		return;
1121 	}
1122 
1123 	mpname = gctl_get_asciiparam(req, "arg0");
1124         if (mpname == NULL) {
1125                 gctl_error(req, "No 'arg0' argument");
1126                 return;
1127         }
1128 	gp = g_multipath_find_geom(mp, mpname);
1129 	if (gp != NULL) {
1130 		gctl_error(req, "Device %s already exist", mpname);
1131 		return;
1132 	}
1133 
1134 	memset(&md, 0, sizeof(md));
1135 	strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1136 	md.md_version = G_MULTIPATH_VERSION;
1137 	strlcpy(md.md_name, mpname, sizeof(md.md_name));
1138 	md.md_size = 0;
1139 	md.md_sectorsize = 0;
1140 	md.md_uuid[0] = 0;
1141 	md.md_active_active = 0;
1142 	val = gctl_get_paraml(req, "active_active", sizeof(*val));
1143 	if (val != NULL && *val != 0)
1144 		md.md_active_active = 1;
1145 	val = gctl_get_paraml(req, "active_read", sizeof(*val));
1146 	if (val != NULL && *val != 0)
1147 		md.md_active_active = 2;
1148 	gp = g_multipath_create(mp, &md);
1149 	if (gp == NULL) {
1150 		gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n",
1151 		    md.md_name, md.md_uuid);
1152 		return;
1153 	}
1154 	sc = gp->softc;
1155 
1156 	for (i = 1; i < *nargs; i++) {
1157 		snprintf(param, sizeof(param), "arg%d", i);
1158 		name = gctl_get_asciiparam(req, param);
1159 		g_multipath_ctl_add_name(req, mp, name);
1160 	}
1161 
1162 	if (sc->sc_ndisks != (*nargs - 1))
1163 		g_multipath_destroy(gp);
1164 }
1165 
1166 static void
1167 g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp)
1168 {
1169 	struct g_multipath_softc *sc;
1170 	struct g_geom *gp;
1171 	struct g_consumer *cp;
1172 	struct g_provider *pp;
1173 	struct g_multipath_metadata md;
1174 	const char *name;
1175 	int error, *val;
1176 
1177 	g_topology_assert();
1178 
1179 	name = gctl_get_asciiparam(req, "arg0");
1180 	if (name == NULL) {
1181 		gctl_error(req, "No 'arg0' argument");
1182 		return;
1183 	}
1184 	gp = g_multipath_find_geom(mp, name);
1185 	if (gp == NULL) {
1186 		gctl_error(req, "Device %s is invalid", name);
1187 		return;
1188 	}
1189 	sc = gp->softc;
1190 	val = gctl_get_paraml(req, "active_active", sizeof(*val));
1191 	if (val != NULL && *val != 0)
1192 		sc->sc_active_active = 1;
1193 	val = gctl_get_paraml(req, "active_read", sizeof(*val));
1194 	if (val != NULL && *val != 0)
1195 		sc->sc_active_active = 2;
1196 	val = gctl_get_paraml(req, "active_passive", sizeof(*val));
1197 	if (val != NULL && *val != 0)
1198 		sc->sc_active_active = 0;
1199 	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
1200 		cp = sc->sc_active;
1201 		pp = cp->provider;
1202 		strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1203 		memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
1204 		strlcpy(md.md_name, name, sizeof(md.md_name));
1205 		md.md_version = G_MULTIPATH_VERSION;
1206 		md.md_size = pp->mediasize;
1207 		md.md_sectorsize = pp->sectorsize;
1208 		md.md_active_active = sc->sc_active_active;
1209 		error = g_multipath_write_metadata(cp, &md);
1210 		if (error != 0)
1211 			gctl_error(req, "Can't update metadata on %s (%d)",
1212 			    pp->name, error);
1213 	}
1214 }
1215 
1216 static void
1217 g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail)
1218 {
1219 	struct g_multipath_softc *sc;
1220 	struct g_geom *gp;
1221 	struct g_consumer *cp;
1222 	const char *mpname, *name;
1223 	int found;
1224 
1225 	mpname = gctl_get_asciiparam(req, "arg0");
1226         if (mpname == NULL) {
1227                 gctl_error(req, "No 'arg0' argument");
1228                 return;
1229         }
1230 	gp = g_multipath_find_geom(mp, mpname);
1231 	if (gp == NULL) {
1232 		gctl_error(req, "Device %s not found", mpname);
1233 		return;
1234 	}
1235 	sc = gp->softc;
1236 
1237 	name = gctl_get_asciiparam(req, "arg1");
1238 	if (name == NULL) {
1239 		gctl_error(req, "No 'arg1' argument");
1240 		return;
1241 	}
1242 
1243 	found = 0;
1244 	mtx_lock(&sc->sc_mtx);
1245 	LIST_FOREACH(cp, &gp->consumer, consumer) {
1246 		if (cp->provider != NULL &&
1247 		    strcmp(cp->provider->name, name) == 0 &&
1248 		    (cp->index & MP_LOST) == 0) {
1249 			found = 1;
1250 			if (!fail == !(cp->index & MP_FAIL))
1251 				continue;
1252 			printf("GEOM_MULTIPATH: %s in %s is marked %s.\n",
1253 				name, sc->sc_name, fail ? "FAIL" : "OK");
1254 			if (fail) {
1255 				g_multipath_fault(cp, MP_FAIL);
1256 				SDT_PROBE3(geom, multipath, config, fail,
1257 				    sc->sc_name, cp->provider->name, 0);
1258 			} else {
1259 				cp->index &= ~MP_FAIL;
1260 				SDT_PROBE2(geom, multipath, config, restore,
1261 				    sc->sc_name, cp->provider->name);
1262 			}
1263 		}
1264 	}
1265 	mtx_unlock(&sc->sc_mtx);
1266 	if (found == 0)
1267 		gctl_error(req, "Provider %s not found", name);
1268 }
1269 
1270 static void
1271 g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp)
1272 {
1273 	struct g_multipath_softc *sc;
1274 	struct g_geom *gp;
1275 	struct g_consumer *cp, *cp1;
1276 	const char *mpname, *name;
1277 	uintptr_t *cnt;
1278 	int found;
1279 
1280 	mpname = gctl_get_asciiparam(req, "arg0");
1281         if (mpname == NULL) {
1282                 gctl_error(req, "No 'arg0' argument");
1283                 return;
1284         }
1285 	gp = g_multipath_find_geom(mp, mpname);
1286 	if (gp == NULL) {
1287 		gctl_error(req, "Device %s not found", mpname);
1288 		return;
1289 	}
1290 	sc = gp->softc;
1291 
1292 	name = gctl_get_asciiparam(req, "arg1");
1293 	if (name == NULL) {
1294 		gctl_error(req, "No 'arg1' argument");
1295 		return;
1296 	}
1297 
1298 	found = 0;
1299 	mtx_lock(&sc->sc_mtx);
1300 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
1301 		if (cp->provider != NULL &&
1302 		    strcmp(cp->provider->name, name) == 0 &&
1303 		    (cp->index & MP_LOST) == 0) {
1304 			found = 1;
1305 			printf("GEOM_MULTIPATH: removing %s from %s\n",
1306 			    cp->provider->name, cp->geom->name);
1307 			SDT_PROBE2(geom, multipath, config, remove,
1308 			    cp->geom->name, cp->provider->name);
1309 			sc->sc_ndisks--;
1310 			g_multipath_fault(cp, MP_LOST);
1311 			cnt = (uintptr_t *)&cp->private;
1312 			if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
1313 				cp->index |= MP_POSTED;
1314 				mtx_unlock(&sc->sc_mtx);
1315 				g_mpd(cp, 0);
1316 				if (cp1 == NULL)
1317 					return;	/* Recursion happened. */
1318 				mtx_lock(&sc->sc_mtx);
1319 			}
1320 		}
1321 	}
1322 	mtx_unlock(&sc->sc_mtx);
1323 	if (found == 0)
1324 		gctl_error(req, "Provider %s not found", name);
1325 }
1326 
1327 static struct g_geom *
1328 g_multipath_find_geom(struct g_class *mp, const char *name)
1329 {
1330 	struct g_geom *gp;
1331 	struct g_multipath_softc *sc;
1332 
1333 	LIST_FOREACH(gp, &mp->geom, geom) {
1334 		sc = gp->softc;
1335 		if (sc == NULL || sc->sc_stopping)
1336 			continue;
1337 		if (strcmp(gp->name, name) == 0)
1338 			return (gp);
1339 	}
1340 	return (NULL);
1341 }
1342 
1343 static void
1344 g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp)
1345 {
1346 	struct g_geom *gp;
1347 	const char *name;
1348 	int error;
1349 
1350 	g_topology_assert();
1351 
1352 	name = gctl_get_asciiparam(req, "arg0");
1353         if (name == NULL) {
1354                 gctl_error(req, "No 'arg0' argument");
1355                 return;
1356         }
1357 	gp = g_multipath_find_geom(mp, name);
1358 	if (gp == NULL) {
1359 		gctl_error(req, "Device %s is invalid", name);
1360 		return;
1361 	}
1362 	error = g_multipath_destroy(gp);
1363 	if (error != 0 && error != EINPROGRESS)
1364 		gctl_error(req, "failed to stop %s (err=%d)", name, error);
1365 }
1366 
1367 static void
1368 g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1369 {
1370 	struct g_geom *gp;
1371 	struct g_multipath_softc *sc;
1372 	struct g_consumer *cp;
1373 	struct g_provider *pp;
1374 	const char *name;
1375 	uint8_t *buf;
1376 	int error;
1377 
1378 	g_topology_assert();
1379 
1380 	name = gctl_get_asciiparam(req, "arg0");
1381         if (name == NULL) {
1382                 gctl_error(req, "No 'arg0' argument");
1383                 return;
1384         }
1385 	gp = g_multipath_find_geom(mp, name);
1386 	if (gp == NULL) {
1387 		gctl_error(req, "Device %s is invalid", name);
1388 		return;
1389 	}
1390 	sc = gp->softc;
1391 
1392 	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
1393 		cp = sc->sc_active;
1394 		pp = cp->provider;
1395 		error = g_access(cp, 1, 1, 1);
1396 		if (error != 0) {
1397 			gctl_error(req, "Can't open %s (%d)", pp->name, error);
1398 			goto destroy;
1399 		}
1400 		g_topology_unlock();
1401 		buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1402 		error = g_write_data(cp, pp->mediasize - pp->sectorsize,
1403 		    buf, pp->sectorsize);
1404 		g_topology_lock();
1405 		g_access(cp, -1, -1, -1);
1406 		if (error != 0)
1407 			gctl_error(req, "Can't erase metadata on %s (%d)",
1408 			    pp->name, error);
1409 	}
1410 
1411 destroy:
1412 	error = g_multipath_destroy(gp);
1413 	if (error != 0 && error != EINPROGRESS)
1414 		gctl_error(req, "failed to destroy %s (err=%d)", name, error);
1415 }
1416 
1417 static void
1418 g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp)
1419 {
1420 	struct g_geom *gp;
1421 	const char *name;
1422 	int error;
1423 
1424 	g_topology_assert();
1425 
1426 	name = gctl_get_asciiparam(req, "arg0");
1427         if (name == NULL) {
1428                 gctl_error(req, "No 'arg0' argument");
1429                 return;
1430         }
1431 	gp = g_multipath_find_geom(mp, name);
1432 	if (gp == NULL) {
1433 		gctl_error(req, "Device %s is invalid", name);
1434 		return;
1435 	}
1436 	error = g_multipath_rotate(gp);
1437 	if (error != 0) {
1438 		gctl_error(req, "failed to rotate %s (err=%d)", name, error);
1439 	}
1440 }
1441 
1442 static void
1443 g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp)
1444 {
1445 	struct sbuf *sb;
1446 	struct g_geom *gp;
1447 	struct g_multipath_softc *sc;
1448 	struct g_consumer *cp;
1449 	const char *name;
1450 	int empty;
1451 
1452 	sb = sbuf_new_auto();
1453 
1454 	g_topology_assert();
1455 	name = gctl_get_asciiparam(req, "arg0");
1456         if (name == NULL) {
1457                 gctl_error(req, "No 'arg0' argument");
1458                 return;
1459         }
1460 	gp = g_multipath_find_geom(mp, name);
1461 	if (gp == NULL) {
1462 		gctl_error(req, "Device %s is invalid", name);
1463 		return;
1464 	}
1465 	sc = gp->softc;
1466 	if (sc->sc_active_active == 1) {
1467 		empty = 1;
1468 		LIST_FOREACH(cp, &gp->consumer, consumer) {
1469 			if (cp->index & MP_BAD)
1470 				continue;
1471 			if (!empty)
1472 				sbuf_cat(sb, " ");
1473 			sbuf_cat(sb, cp->provider->name);
1474 			empty = 0;
1475 		}
1476 		if (empty)
1477 			sbuf_cat(sb, "none");
1478 		sbuf_cat(sb, "\n");
1479 	} else if (sc->sc_active && sc->sc_active->provider) {
1480 		sbuf_printf(sb, "%s\n", sc->sc_active->provider->name);
1481 	} else {
1482 		sbuf_cat(sb, "none\n");
1483 	}
1484 	sbuf_finish(sb);
1485 	gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1486 	sbuf_delete(sb);
1487 }
1488 
1489 static void
1490 g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1491 {
1492 	uint32_t *version;
1493 	g_topology_assert();
1494 	version = gctl_get_paraml(req, "version", sizeof(*version));
1495 	if (version == NULL) {
1496 		gctl_error(req, "No 'version' argument");
1497 	} else if (*version != G_MULTIPATH_VERSION) {
1498 		gctl_error(req, "Userland and kernel parts are out of sync");
1499 	} else if (strcmp(verb, "add") == 0) {
1500 		g_multipath_ctl_add(req, mp);
1501 	} else if (strcmp(verb, "prefer") == 0) {
1502 		g_multipath_ctl_prefer(req, mp);
1503 	} else if (strcmp(verb, "create") == 0) {
1504 		g_multipath_ctl_create(req, mp);
1505 	} else if (strcmp(verb, "configure") == 0) {
1506 		g_multipath_ctl_configure(req, mp);
1507 	} else if (strcmp(verb, "stop") == 0) {
1508 		g_multipath_ctl_stop(req, mp);
1509 	} else if (strcmp(verb, "destroy") == 0) {
1510 		g_multipath_ctl_destroy(req, mp);
1511 	} else if (strcmp(verb, "fail") == 0) {
1512 		g_multipath_ctl_fail(req, mp, 1);
1513 	} else if (strcmp(verb, "restore") == 0) {
1514 		g_multipath_ctl_fail(req, mp, 0);
1515 	} else if (strcmp(verb, "remove") == 0) {
1516 		g_multipath_ctl_remove(req, mp);
1517 	} else if (strcmp(verb, "rotate") == 0) {
1518 		g_multipath_ctl_rotate(req, mp);
1519 	} else if (strcmp(verb, "getactive") == 0) {
1520 		g_multipath_ctl_getactive(req, mp);
1521 	} else {
1522 		gctl_error(req, "Unknown verb %s", verb);
1523 	}
1524 }
1525 
1526 static void
1527 g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1528     struct g_consumer *cp, struct g_provider *pp)
1529 {
1530 	struct g_multipath_softc *sc;
1531 	int good;
1532 
1533 	g_topology_assert();
1534 
1535 	sc = gp->softc;
1536 	if (sc == NULL)
1537 		return;
1538 	if (cp != NULL) {
1539 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1540 		    (cp->index & MP_NEW) ? "NEW" :
1541 		    (cp->index & MP_LOST) ? "LOST" :
1542 		    (cp->index & MP_FAIL) ? "FAIL" :
1543 		    (sc->sc_active_active == 1 || sc->sc_active == cp) ?
1544 		     "ACTIVE" :
1545 		     sc->sc_active_active == 2 ? "READ" : "PASSIVE");
1546 	} else {
1547 		good = g_multipath_good(gp);
1548 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1549 		    good == 0 ? "BROKEN" :
1550 		    (good != sc->sc_ndisks || sc->sc_ndisks == 1) ?
1551 		    "DEGRADED" : "OPTIMAL");
1552 	}
1553 	if (cp == NULL && pp == NULL) {
1554 		sbuf_printf(sb, "%s<UUID>%s</UUID>\n", indent, sc->sc_uuid);
1555 		sbuf_printf(sb, "%s<Mode>Active/%s</Mode>\n", indent,
1556 		    sc->sc_active_active == 2 ? "Read" :
1557 		    sc->sc_active_active == 1 ? "Active" : "Passive");
1558 		sbuf_printf(sb, "%s<Type>%s</Type>\n", indent,
1559 		    sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC");
1560 	}
1561 }
1562 
1563 DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);
1564 MODULE_VERSION(geom_multipath, 0);
1565