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