xref: /freebsd/sys/geom/multipath/g_multipath.c (revision d65cd7a57bf0600b722afc770838a5d0c1c3a8e1)
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 
483 static int
484 g_multipath_access(struct g_provider *pp, int dr, int dw, int de)
485 {
486 	struct g_geom *gp;
487 	struct g_consumer *cp, *badcp = NULL;
488 	struct g_multipath_softc *sc;
489 	int error;
490 
491 	gp = pp->geom;
492 
493 	/* Error used if we have no valid consumers. */
494 	error = (dr > 0 || dw > 0 || de > 0) ? ENXIO : 0;
495 
496 	LIST_FOREACH(cp, &gp->consumer, consumer) {
497 		if (cp->index & MP_WITHER)
498 			continue;
499 
500 		error = g_access(cp, dr, dw, de);
501 		if (error) {
502 			badcp = cp;
503 			goto fail;
504 		}
505 	}
506 
507 	if (error != 0)
508 		return (error);
509 
510 	sc = gp->softc;
511 	sc->sc_opened += dr + dw + de;
512 	if (sc->sc_stopping && sc->sc_opened == 0)
513 		g_multipath_destroy(gp);
514 
515 	return (0);
516 
517 fail:
518 	LIST_FOREACH(cp, &gp->consumer, consumer) {
519 		if (cp == badcp)
520 			break;
521 		if (cp->index & MP_WITHER)
522 			continue;
523 
524 		(void) g_access(cp, -dr, -dw, -de);
525 	}
526 	return (error);
527 }
528 
529 static struct g_geom *
530 g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md)
531 {
532 	struct g_multipath_softc *sc;
533 	struct g_geom *gp;
534 	struct g_provider *pp;
535 
536 	g_topology_assert();
537 
538 	LIST_FOREACH(gp, &mp->geom, geom) {
539 		sc = gp->softc;
540 		if (sc == NULL || sc->sc_stopping)
541 			continue;
542 		if (strcmp(gp->name, md->md_name) == 0) {
543 			printf("GEOM_MULTIPATH: name %s already exists\n",
544 			    md->md_name);
545 			return (NULL);
546 		}
547 	}
548 
549 	gp = g_new_geomf(mp, "%s", md->md_name);
550 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
551 	mtx_init(&sc->sc_mtx, "multipath", NULL, MTX_DEF);
552 	memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid));
553 	memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name));
554 	sc->sc_active_active = md->md_active_active;
555 	sc->sc_size = md->md_size;
556 	gp->softc = sc;
557 	gp->start = g_multipath_start;
558 	gp->orphan = g_multipath_orphan;
559 	gp->resize = g_multipath_resize;
560 	gp->access = g_multipath_access;
561 	gp->dumpconf = g_multipath_dumpconf;
562 
563 	pp = g_new_providerf(gp, "multipath/%s", md->md_name);
564 	pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
565 	if (md->md_size != 0) {
566 		pp->mediasize = md->md_size -
567 		    ((md->md_uuid[0] != 0) ? md->md_sectorsize : 0);
568 		pp->sectorsize = md->md_sectorsize;
569 	}
570 	sc->sc_pp = pp;
571 	g_error_provider(pp, 0);
572 	printf("GEOM_MULTIPATH: %s created\n", gp->name);
573 	return (gp);
574 }
575 
576 static int
577 g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp)
578 {
579 	struct g_multipath_softc *sc;
580 	struct g_consumer *cp, *nxtcp;
581 	int error, acr, acw, ace;
582 
583 	g_topology_assert();
584 
585 	sc = gp->softc;
586 	KASSERT(sc, ("no softc"));
587 
588 	/*
589 	 * Make sure that the passed provider isn't already attached
590 	 */
591 	LIST_FOREACH(cp, &gp->consumer, consumer) {
592 		if (cp->provider == pp)
593 			break;
594 	}
595 	if (cp) {
596 		printf("GEOM_MULTIPATH: provider %s already attached to %s\n",
597 		    pp->name, gp->name);
598 		return (EEXIST);
599 	}
600 	nxtcp = LIST_FIRST(&gp->consumer);
601 	cp = g_new_consumer(gp);
602 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
603 	cp->private = NULL;
604 	cp->index = MP_NEW;
605 	error = g_attach(cp, pp);
606 	if (error != 0) {
607 		printf("GEOM_MULTIPATH: cannot attach %s to %s",
608 		    pp->name, sc->sc_name);
609 		g_destroy_consumer(cp);
610 		return (error);
611 	}
612 
613 	/*
614 	 * Set access permissions on new consumer to match other consumers
615 	 */
616 	if (sc->sc_pp) {
617 		acr = sc->sc_pp->acr;
618 		acw = sc->sc_pp->acw;
619 		ace = sc->sc_pp->ace;
620 	} else
621 		acr = acw = ace = 0;
622 	if (g_multipath_exclusive) {
623 		acr++;
624 		acw++;
625 		ace++;
626 	}
627 	error = g_access(cp, acr, acw, ace);
628 	if (error) {
629 		printf("GEOM_MULTIPATH: cannot set access in "
630 		    "attaching %s to %s (%d)\n",
631 		    pp->name, sc->sc_name, error);
632 		g_detach(cp);
633 		g_destroy_consumer(cp);
634 		return (error);
635 	}
636 	if (sc->sc_size == 0) {
637 		sc->sc_size = pp->mediasize -
638 		    ((sc->sc_uuid[0] != 0) ? pp->sectorsize : 0);
639 		sc->sc_pp->mediasize = sc->sc_size;
640 		sc->sc_pp->sectorsize = pp->sectorsize;
641 	}
642 	if (sc->sc_pp->stripesize == 0 && sc->sc_pp->stripeoffset == 0) {
643 		sc->sc_pp->stripesize = pp->stripesize;
644 		sc->sc_pp->stripeoffset = pp->stripeoffset;
645 	}
646 	sc->sc_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
647 	mtx_lock(&sc->sc_mtx);
648 	cp->index = 0;
649 	sc->sc_ndisks++;
650 	mtx_unlock(&sc->sc_mtx);
651 	printf("GEOM_MULTIPATH: %s added to %s\n",
652 	    pp->name, sc->sc_name);
653 	if (sc->sc_active == NULL) {
654 		sc->sc_active = cp;
655 		if (sc->sc_active_active != 1)
656 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
657 			    pp->name, sc->sc_name);
658 	}
659 	return (0);
660 }
661 
662 static int
663 g_multipath_destroy(struct g_geom *gp)
664 {
665 	struct g_multipath_softc *sc;
666 	struct g_consumer *cp, *cp1;
667 
668 	g_topology_assert();
669 	if (gp->softc == NULL)
670 		return (ENXIO);
671 	sc = gp->softc;
672 	if (!sc->sc_stopping) {
673 		printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
674 		sc->sc_stopping = 1;
675 	}
676 	if (sc->sc_opened != 0) {
677 		g_wither_provider(sc->sc_pp, ENXIO);
678 		sc->sc_pp = NULL;
679 		return (EINPROGRESS);
680 	}
681 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
682 		mtx_lock(&sc->sc_mtx);
683 		if ((cp->index & MP_POSTED) == 0) {
684 			cp->index |= MP_POSTED;
685 			mtx_unlock(&sc->sc_mtx);
686 			g_mpd(cp, 0);
687 			if (cp1 == NULL)
688 				return(0);	/* Recursion happened. */
689 		} else
690 			mtx_unlock(&sc->sc_mtx);
691 	}
692 	if (!LIST_EMPTY(&gp->consumer))
693 		return (EINPROGRESS);
694 	mtx_destroy(&sc->sc_mtx);
695 	g_free(gp->softc);
696 	gp->softc = NULL;
697 	printf("GEOM_MULTIPATH: %s destroyed\n", gp->name);
698 	g_wither_geom(gp, ENXIO);
699 	return (0);
700 }
701 
702 static int
703 g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
704     struct g_geom *gp)
705 {
706 
707 	return (g_multipath_destroy(gp));
708 }
709 
710 static int
711 g_multipath_rotate(struct g_geom *gp)
712 {
713 	struct g_consumer *lcp, *first_good_cp = NULL;
714 	struct g_multipath_softc *sc = gp->softc;
715 	int active_cp_seen = 0;
716 
717 	g_topology_assert();
718 	if (sc == NULL)
719 		return (ENXIO);
720 	LIST_FOREACH(lcp, &gp->consumer, consumer) {
721 		if ((lcp->index & MP_BAD) == 0) {
722 			if (first_good_cp == NULL)
723 				first_good_cp = lcp;
724 			if (active_cp_seen)
725 				break;
726 		}
727 		if (sc->sc_active == lcp)
728 			active_cp_seen = 1;
729 	}
730 	if (lcp == NULL)
731 		lcp = first_good_cp;
732 	if (lcp && lcp != sc->sc_active) {
733 		sc->sc_active = lcp;
734 		if (sc->sc_active_active != 1)
735 			printf("GEOM_MULTIPATH: %s is now active path in %s\n",
736 			    lcp->provider->name, sc->sc_name);
737 	}
738 	return (0);
739 }
740 
741 static void
742 g_multipath_init(struct g_class *mp)
743 {
744 	bioq_init(&gmtbq);
745 	mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
746 	kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt");
747 }
748 
749 static void
750 g_multipath_fini(struct g_class *mp)
751 {
752 	if (g_multipath_kt_state == GKT_RUN) {
753 		mtx_lock(&gmtbq_mtx);
754 		g_multipath_kt_state = GKT_DIE;
755 		wakeup(&g_multipath_kt_state);
756 		msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
757 		    "gmp:fini", 0);
758 		mtx_unlock(&gmtbq_mtx);
759 	}
760 }
761 
762 static int
763 g_multipath_read_metadata(struct g_consumer *cp,
764     struct g_multipath_metadata *md)
765 {
766 	struct g_provider *pp;
767 	u_char *buf;
768 	int error;
769 
770 	g_topology_assert();
771 	error = g_access(cp, 1, 0, 0);
772 	if (error != 0)
773 		return (error);
774 	pp = cp->provider;
775 	g_topology_unlock();
776 	buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
777 	    pp->sectorsize, &error);
778 	g_topology_lock();
779 	g_access(cp, -1, 0, 0);
780 	if (buf == NULL)
781 		return (error);
782 	multipath_metadata_decode(buf, md);
783 	g_free(buf);
784 	return (0);
785 }
786 
787 static int
788 g_multipath_write_metadata(struct g_consumer *cp,
789     struct g_multipath_metadata *md)
790 {
791 	struct g_provider *pp;
792 	u_char *buf;
793 	int error;
794 
795 	g_topology_assert();
796 	error = g_access(cp, 1, 1, 1);
797 	if (error != 0)
798 		return (error);
799 	pp = cp->provider;
800 	g_topology_unlock();
801 	buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
802 	multipath_metadata_encode(md, buf);
803 	error = g_write_data(cp, pp->mediasize - pp->sectorsize,
804 	    buf, pp->sectorsize);
805 	g_topology_lock();
806 	g_access(cp, -1, -1, -1);
807 	g_free(buf);
808 	return (error);
809 }
810 
811 static struct g_geom *
812 g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
813 {
814 	struct g_multipath_metadata md;
815 	struct g_multipath_softc *sc;
816 	struct g_consumer *cp;
817 	struct g_geom *gp, *gp1;
818 	int error, isnew;
819 
820 	g_topology_assert();
821 
822 	gp = g_new_geomf(mp, "multipath:taste");
823 	gp->start = g_multipath_start;
824 	gp->access = g_multipath_access;
825 	gp->orphan = g_multipath_orphan;
826 	cp = g_new_consumer(gp);
827 	g_attach(cp, pp);
828 	error = g_multipath_read_metadata(cp, &md);
829 	g_detach(cp);
830 	g_destroy_consumer(cp);
831 	g_destroy_geom(gp);
832 	if (error != 0)
833 		return (NULL);
834 	gp = NULL;
835 
836 	if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
837 		if (g_multipath_debug)
838 			printf("%s is not MULTIPATH\n", pp->name);
839 		return (NULL);
840 	}
841 	if (md.md_version != G_MULTIPATH_VERSION) {
842 		printf("%s has version %d multipath id- this module is version "
843 		    " %d: rejecting\n", pp->name, md.md_version,
844 		    G_MULTIPATH_VERSION);
845 		return (NULL);
846 	}
847 	if (md.md_size != 0 && md.md_size != pp->mediasize)
848 		return (NULL);
849 	if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize)
850 		return (NULL);
851 	if (g_multipath_debug)
852 		printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
853 	SDT_PROBE2(geom, multipath, config, taste, md.md_name, md.md_uuid);
854 
855 	/*
856 	 * Let's check if such a device already is present. We check against
857 	 * uuid alone first because that's the true distinguishor. If that
858 	 * passes, then we check for name conflicts. If there are conflicts,
859 	 * modify the name.
860 	 *
861 	 * The whole purpose of this is to solve the problem that people don't
862 	 * pick good unique names, but good unique names (like uuids) are a
863 	 * pain to use. So, we allow people to build GEOMs with friendly names
864 	 * and uuids, and modify the names in case there's a collision.
865 	 */
866 	sc = NULL;
867 	LIST_FOREACH(gp, &mp->geom, geom) {
868 		sc = gp->softc;
869 		if (sc == NULL || sc->sc_stopping)
870 			continue;
871 		if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
872 			break;
873 	}
874 
875 	LIST_FOREACH(gp1, &mp->geom, geom) {
876 		if (gp1 == gp)
877 			continue;
878 		sc = gp1->softc;
879 		if (sc == NULL || sc->sc_stopping)
880 			continue;
881 		if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
882 			break;
883 	}
884 
885 	/*
886 	 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
887 	 *
888 	 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
889 	 * with the same name (but a different UUID).
890 	 *
891 	 * If gp is NULL, then modify the name with a random number and
892   	 * complain, but allow the creation of the geom to continue.
893 	 *
894 	 * If gp is *not* NULL, just use the geom's name as we're attaching
895 	 * this disk to the (previously generated) name.
896 	 */
897 
898 	if (gp1) {
899 		sc = gp1->softc;
900 		if (gp == NULL) {
901 			char buf[16];
902 			u_long rand = random();
903 
904 			snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
905 			printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
906 			    sc->sc_name, sc->sc_uuid);
907 			printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
908 			    md.md_uuid, buf);
909 			strlcpy(md.md_name, buf, sizeof(md.md_name));
910 		} else {
911 			strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
912 		}
913 	}
914 
915 	if (gp == NULL) {
916 		gp = g_multipath_create(mp, &md);
917 		if (gp == NULL) {
918 			printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
919 			    md.md_name, md.md_uuid);
920 			return (NULL);
921 		}
922 		isnew = 1;
923 	} else {
924 		isnew = 0;
925 	}
926 
927 	sc = gp->softc;
928 	KASSERT(sc != NULL, ("sc is NULL"));
929 	error = g_multipath_add_disk(gp, pp);
930 	if (error != 0) {
931 		if (isnew)
932 			g_multipath_destroy(gp);
933 		return (NULL);
934 	}
935 	return (gp);
936 }
937 
938 static void
939 g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp,
940     const char *name)
941 {
942 	struct g_multipath_softc *sc;
943 	struct g_geom *gp;
944 	struct g_consumer *cp;
945 	struct g_provider *pp;
946 	const char *mpname;
947 	static const char devpf[6] = "/dev/";
948 	int error;
949 
950 	g_topology_assert();
951 
952 	mpname = gctl_get_asciiparam(req, "arg0");
953         if (mpname == NULL) {
954                 gctl_error(req, "No 'arg0' argument");
955                 return;
956         }
957 	gp = g_multipath_find_geom(mp, mpname);
958 	if (gp == NULL) {
959 		gctl_error(req, "Device %s is invalid", mpname);
960 		return;
961 	}
962 	sc = gp->softc;
963 
964 	if (strncmp(name, devpf, 5) == 0)
965 		name += 5;
966 	pp = g_provider_by_name(name);
967 	if (pp == NULL) {
968 		gctl_error(req, "Provider %s is invalid", name);
969 		return;
970 	}
971 
972 	/*
973 	 * Check to make sure parameters match.
974 	 */
975 	LIST_FOREACH(cp, &gp->consumer, consumer) {
976 		if (cp->provider == pp) {
977 			gctl_error(req, "provider %s is already there",
978 			    pp->name);
979 			return;
980 		}
981 	}
982 	if (sc->sc_pp->mediasize != 0 &&
983 	    sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0)
984 	     != pp->mediasize) {
985 		gctl_error(req, "Providers size mismatch %jd != %jd",
986 		    (intmax_t) sc->sc_pp->mediasize +
987 			(sc->sc_uuid[0] != 0 ? pp->sectorsize : 0),
988 		    (intmax_t) pp->mediasize);
989 		return;
990 	}
991 	if (sc->sc_pp->sectorsize != 0 &&
992 	    sc->sc_pp->sectorsize != pp->sectorsize) {
993 		gctl_error(req, "Providers sectorsize mismatch %u != %u",
994 		    sc->sc_pp->sectorsize, pp->sectorsize);
995 		return;
996 	}
997 
998 	error = g_multipath_add_disk(gp, pp);
999 	if (error != 0)
1000 		gctl_error(req, "Provider addition error: %d", error);
1001 }
1002 
1003 static void
1004 g_multipath_ctl_prefer(struct gctl_req *req, struct g_class *mp)
1005 {
1006 	struct g_geom *gp;
1007 	struct g_multipath_softc *sc;
1008 	struct g_consumer *cp;
1009 	const char *name, *mpname;
1010 	static const char devpf[6] = "/dev/";
1011 	int *nargs;
1012 
1013 	g_topology_assert();
1014 
1015 	mpname = gctl_get_asciiparam(req, "arg0");
1016         if (mpname == NULL) {
1017                 gctl_error(req, "No 'arg0' argument");
1018                 return;
1019         }
1020 	gp = g_multipath_find_geom(mp, mpname);
1021 	if (gp == NULL) {
1022 		gctl_error(req, "Device %s is invalid", mpname);
1023 		return;
1024 	}
1025 	sc = gp->softc;
1026 
1027 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1028 	if (nargs == NULL) {
1029 		gctl_error(req, "No 'nargs' argument");
1030 		return;
1031 	}
1032 	if (*nargs != 2) {
1033 		gctl_error(req, "missing device");
1034 		return;
1035 	}
1036 
1037 	name = gctl_get_asciiparam(req, "arg1");
1038 	if (name == NULL) {
1039 		gctl_error(req, "No 'arg1' argument");
1040 		return;
1041 	}
1042 	if (strncmp(name, devpf, 5) == 0) {
1043 		name += 5;
1044 	}
1045 
1046 	LIST_FOREACH(cp, &gp->consumer, consumer) {
1047 		if (cp->provider != NULL
1048                       && strcmp(cp->provider->name, name) == 0)
1049 		    break;
1050 	}
1051 
1052 	if (cp == NULL) {
1053 		gctl_error(req, "Provider %s not found", name);
1054 		return;
1055 	}
1056 
1057 	mtx_lock(&sc->sc_mtx);
1058 
1059 	if (cp->index & MP_BAD) {
1060 		gctl_error(req, "Consumer %s is invalid", name);
1061 		mtx_unlock(&sc->sc_mtx);
1062 		return;
1063 	}
1064 
1065 	/* Here when the consumer is present and in good shape */
1066 
1067 	sc->sc_active = cp;
1068 	if (!sc->sc_active_active)
1069 	    printf("GEOM_MULTIPATH: %s now active path in %s\n",
1070 		sc->sc_active->provider->name, sc->sc_name);
1071 
1072 	mtx_unlock(&sc->sc_mtx);
1073 }
1074 
1075 static void
1076 g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp)
1077 {
1078 	struct g_multipath_softc *sc;
1079 	struct g_geom *gp;
1080 	const char *mpname, *name;
1081 
1082 	mpname = gctl_get_asciiparam(req, "arg0");
1083         if (mpname == NULL) {
1084                 gctl_error(req, "No 'arg0' argument");
1085                 return;
1086         }
1087 	gp = g_multipath_find_geom(mp, mpname);
1088 	if (gp == NULL) {
1089 		gctl_error(req, "Device %s not found", mpname);
1090 		return;
1091 	}
1092 	sc = gp->softc;
1093 
1094 	name = gctl_get_asciiparam(req, "arg1");
1095 	if (name == NULL) {
1096 		gctl_error(req, "No 'arg1' argument");
1097 		return;
1098 	}
1099 	g_multipath_ctl_add_name(req, mp, name);
1100 }
1101 
1102 static void
1103 g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp)
1104 {
1105 	struct g_multipath_metadata md;
1106 	struct g_multipath_softc *sc;
1107 	struct g_geom *gp;
1108 	const char *mpname, *name;
1109 	char param[16];
1110 	int *nargs, i, *val;
1111 
1112 	g_topology_assert();
1113 
1114 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1115 	if (*nargs < 2) {
1116 		gctl_error(req, "wrong number of arguments.");
1117 		return;
1118 	}
1119 
1120 	mpname = gctl_get_asciiparam(req, "arg0");
1121         if (mpname == NULL) {
1122                 gctl_error(req, "No 'arg0' argument");
1123                 return;
1124         }
1125 	gp = g_multipath_find_geom(mp, mpname);
1126 	if (gp != NULL) {
1127 		gctl_error(req, "Device %s already exist", mpname);
1128 		return;
1129 	}
1130 
1131 	memset(&md, 0, sizeof(md));
1132 	strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1133 	md.md_version = G_MULTIPATH_VERSION;
1134 	strlcpy(md.md_name, mpname, sizeof(md.md_name));
1135 	md.md_size = 0;
1136 	md.md_sectorsize = 0;
1137 	md.md_uuid[0] = 0;
1138 	md.md_active_active = 0;
1139 	val = gctl_get_paraml(req, "active_active", sizeof(*val));
1140 	if (val != NULL && *val != 0)
1141 		md.md_active_active = 1;
1142 	val = gctl_get_paraml(req, "active_read", sizeof(*val));
1143 	if (val != NULL && *val != 0)
1144 		md.md_active_active = 2;
1145 	gp = g_multipath_create(mp, &md);
1146 	if (gp == NULL) {
1147 		gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n",
1148 		    md.md_name, md.md_uuid);
1149 		return;
1150 	}
1151 	sc = gp->softc;
1152 
1153 	for (i = 1; i < *nargs; i++) {
1154 		snprintf(param, sizeof(param), "arg%d", i);
1155 		name = gctl_get_asciiparam(req, param);
1156 		g_multipath_ctl_add_name(req, mp, name);
1157 	}
1158 
1159 	if (sc->sc_ndisks != (*nargs - 1))
1160 		g_multipath_destroy(gp);
1161 }
1162 
1163 static void
1164 g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp)
1165 {
1166 	struct g_multipath_softc *sc;
1167 	struct g_geom *gp;
1168 	struct g_consumer *cp;
1169 	struct g_provider *pp;
1170 	struct g_multipath_metadata md;
1171 	const char *name;
1172 	int error, *val;
1173 
1174 	g_topology_assert();
1175 
1176 	name = gctl_get_asciiparam(req, "arg0");
1177 	if (name == NULL) {
1178 		gctl_error(req, "No 'arg0' argument");
1179 		return;
1180 	}
1181 	gp = g_multipath_find_geom(mp, name);
1182 	if (gp == NULL) {
1183 		gctl_error(req, "Device %s is invalid", name);
1184 		return;
1185 	}
1186 	sc = gp->softc;
1187 	val = gctl_get_paraml(req, "active_active", sizeof(*val));
1188 	if (val != NULL && *val != 0)
1189 		sc->sc_active_active = 1;
1190 	val = gctl_get_paraml(req, "active_read", sizeof(*val));
1191 	if (val != NULL && *val != 0)
1192 		sc->sc_active_active = 2;
1193 	val = gctl_get_paraml(req, "active_passive", sizeof(*val));
1194 	if (val != NULL && *val != 0)
1195 		sc->sc_active_active = 0;
1196 	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
1197 		cp = sc->sc_active;
1198 		pp = cp->provider;
1199 		strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1200 		memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
1201 		strlcpy(md.md_name, name, sizeof(md.md_name));
1202 		md.md_version = G_MULTIPATH_VERSION;
1203 		md.md_size = pp->mediasize;
1204 		md.md_sectorsize = pp->sectorsize;
1205 		md.md_active_active = sc->sc_active_active;
1206 		error = g_multipath_write_metadata(cp, &md);
1207 		if (error != 0)
1208 			gctl_error(req, "Can't update metadata on %s (%d)",
1209 			    pp->name, error);
1210 	}
1211 }
1212 
1213 static void
1214 g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail)
1215 {
1216 	struct g_multipath_softc *sc;
1217 	struct g_geom *gp;
1218 	struct g_consumer *cp;
1219 	const char *mpname, *name;
1220 	int found;
1221 
1222 	mpname = gctl_get_asciiparam(req, "arg0");
1223         if (mpname == NULL) {
1224                 gctl_error(req, "No 'arg0' argument");
1225                 return;
1226         }
1227 	gp = g_multipath_find_geom(mp, mpname);
1228 	if (gp == NULL) {
1229 		gctl_error(req, "Device %s not found", mpname);
1230 		return;
1231 	}
1232 	sc = gp->softc;
1233 
1234 	name = gctl_get_asciiparam(req, "arg1");
1235 	if (name == NULL) {
1236 		gctl_error(req, "No 'arg1' argument");
1237 		return;
1238 	}
1239 
1240 	found = 0;
1241 	mtx_lock(&sc->sc_mtx);
1242 	LIST_FOREACH(cp, &gp->consumer, consumer) {
1243 		if (cp->provider != NULL &&
1244 		    strcmp(cp->provider->name, name) == 0 &&
1245 		    (cp->index & MP_LOST) == 0) {
1246 			found = 1;
1247 			if (!fail == !(cp->index & MP_FAIL))
1248 				continue;
1249 			printf("GEOM_MULTIPATH: %s in %s is marked %s.\n",
1250 				name, sc->sc_name, fail ? "FAIL" : "OK");
1251 			if (fail) {
1252 				g_multipath_fault(cp, MP_FAIL);
1253 				SDT_PROBE3(geom, multipath, config, fail,
1254 				    sc->sc_name, cp->provider->name, 0);
1255 			} else {
1256 				cp->index &= ~MP_FAIL;
1257 				SDT_PROBE2(geom, multipath, config, restore,
1258 				    sc->sc_name, cp->provider->name);
1259 			}
1260 		}
1261 	}
1262 	mtx_unlock(&sc->sc_mtx);
1263 	if (found == 0)
1264 		gctl_error(req, "Provider %s not found", name);
1265 }
1266 
1267 static void
1268 g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp)
1269 {
1270 	struct g_multipath_softc *sc;
1271 	struct g_geom *gp;
1272 	struct g_consumer *cp, *cp1;
1273 	const char *mpname, *name;
1274 	uintptr_t *cnt;
1275 	int found;
1276 
1277 	mpname = gctl_get_asciiparam(req, "arg0");
1278         if (mpname == NULL) {
1279                 gctl_error(req, "No 'arg0' argument");
1280                 return;
1281         }
1282 	gp = g_multipath_find_geom(mp, mpname);
1283 	if (gp == NULL) {
1284 		gctl_error(req, "Device %s not found", mpname);
1285 		return;
1286 	}
1287 	sc = gp->softc;
1288 
1289 	name = gctl_get_asciiparam(req, "arg1");
1290 	if (name == NULL) {
1291 		gctl_error(req, "No 'arg1' argument");
1292 		return;
1293 	}
1294 
1295 	found = 0;
1296 	mtx_lock(&sc->sc_mtx);
1297 	LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
1298 		if (cp->provider != NULL &&
1299 		    strcmp(cp->provider->name, name) == 0 &&
1300 		    (cp->index & MP_LOST) == 0) {
1301 			found = 1;
1302 			printf("GEOM_MULTIPATH: removing %s from %s\n",
1303 			    cp->provider->name, cp->geom->name);
1304 			SDT_PROBE2(geom, multipath, config, remove,
1305 			    cp->geom->name, cp->provider->name);
1306 			sc->sc_ndisks--;
1307 			g_multipath_fault(cp, MP_LOST);
1308 			cnt = (uintptr_t *)&cp->private;
1309 			if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
1310 				cp->index |= MP_POSTED;
1311 				mtx_unlock(&sc->sc_mtx);
1312 				g_mpd(cp, 0);
1313 				if (cp1 == NULL)
1314 					return;	/* Recursion happened. */
1315 				mtx_lock(&sc->sc_mtx);
1316 			}
1317 		}
1318 	}
1319 	mtx_unlock(&sc->sc_mtx);
1320 	if (found == 0)
1321 		gctl_error(req, "Provider %s not found", name);
1322 }
1323 
1324 static struct g_geom *
1325 g_multipath_find_geom(struct g_class *mp, const char *name)
1326 {
1327 	struct g_geom *gp;
1328 	struct g_multipath_softc *sc;
1329 
1330 	LIST_FOREACH(gp, &mp->geom, geom) {
1331 		sc = gp->softc;
1332 		if (sc == NULL || sc->sc_stopping)
1333 			continue;
1334 		if (strcmp(gp->name, name) == 0)
1335 			return (gp);
1336 	}
1337 	return (NULL);
1338 }
1339 
1340 static void
1341 g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp)
1342 {
1343 	struct g_geom *gp;
1344 	const char *name;
1345 	int error;
1346 
1347 	g_topology_assert();
1348 
1349 	name = gctl_get_asciiparam(req, "arg0");
1350         if (name == NULL) {
1351                 gctl_error(req, "No 'arg0' argument");
1352                 return;
1353         }
1354 	gp = g_multipath_find_geom(mp, name);
1355 	if (gp == NULL) {
1356 		gctl_error(req, "Device %s is invalid", name);
1357 		return;
1358 	}
1359 	error = g_multipath_destroy(gp);
1360 	if (error != 0 && error != EINPROGRESS)
1361 		gctl_error(req, "failed to stop %s (err=%d)", name, error);
1362 }
1363 
1364 static void
1365 g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1366 {
1367 	struct g_geom *gp;
1368 	struct g_multipath_softc *sc;
1369 	struct g_consumer *cp;
1370 	struct g_provider *pp;
1371 	const char *name;
1372 	uint8_t *buf;
1373 	int error;
1374 
1375 	g_topology_assert();
1376 
1377 	name = gctl_get_asciiparam(req, "arg0");
1378         if (name == NULL) {
1379                 gctl_error(req, "No 'arg0' argument");
1380                 return;
1381         }
1382 	gp = g_multipath_find_geom(mp, name);
1383 	if (gp == NULL) {
1384 		gctl_error(req, "Device %s is invalid", name);
1385 		return;
1386 	}
1387 	sc = gp->softc;
1388 
1389 	if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
1390 		cp = sc->sc_active;
1391 		pp = cp->provider;
1392 		error = g_access(cp, 1, 1, 1);
1393 		if (error != 0) {
1394 			gctl_error(req, "Can't open %s (%d)", pp->name, error);
1395 			goto destroy;
1396 		}
1397 		g_topology_unlock();
1398 		buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1399 		error = g_write_data(cp, pp->mediasize - pp->sectorsize,
1400 		    buf, pp->sectorsize);
1401 		g_topology_lock();
1402 		g_access(cp, -1, -1, -1);
1403 		if (error != 0)
1404 			gctl_error(req, "Can't erase metadata on %s (%d)",
1405 			    pp->name, error);
1406 	}
1407 
1408 destroy:
1409 	error = g_multipath_destroy(gp);
1410 	if (error != 0 && error != EINPROGRESS)
1411 		gctl_error(req, "failed to destroy %s (err=%d)", name, error);
1412 }
1413 
1414 static void
1415 g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp)
1416 {
1417 	struct g_geom *gp;
1418 	const char *name;
1419 	int error;
1420 
1421 	g_topology_assert();
1422 
1423 	name = gctl_get_asciiparam(req, "arg0");
1424         if (name == NULL) {
1425                 gctl_error(req, "No 'arg0' argument");
1426                 return;
1427         }
1428 	gp = g_multipath_find_geom(mp, name);
1429 	if (gp == NULL) {
1430 		gctl_error(req, "Device %s is invalid", name);
1431 		return;
1432 	}
1433 	error = g_multipath_rotate(gp);
1434 	if (error != 0) {
1435 		gctl_error(req, "failed to rotate %s (err=%d)", name, error);
1436 	}
1437 }
1438 
1439 static void
1440 g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp)
1441 {
1442 	struct sbuf *sb;
1443 	struct g_geom *gp;
1444 	struct g_multipath_softc *sc;
1445 	struct g_consumer *cp;
1446 	const char *name;
1447 	int empty;
1448 
1449 	sb = sbuf_new_auto();
1450 
1451 	g_topology_assert();
1452 	name = gctl_get_asciiparam(req, "arg0");
1453         if (name == NULL) {
1454                 gctl_error(req, "No 'arg0' argument");
1455                 return;
1456         }
1457 	gp = g_multipath_find_geom(mp, name);
1458 	if (gp == NULL) {
1459 		gctl_error(req, "Device %s is invalid", name);
1460 		return;
1461 	}
1462 	sc = gp->softc;
1463 	if (sc->sc_active_active == 1) {
1464 		empty = 1;
1465 		LIST_FOREACH(cp, &gp->consumer, consumer) {
1466 			if (cp->index & MP_BAD)
1467 				continue;
1468 			if (!empty)
1469 				sbuf_cat(sb, " ");
1470 			sbuf_cat(sb, cp->provider->name);
1471 			empty = 0;
1472 		}
1473 		if (empty)
1474 			sbuf_cat(sb, "none");
1475 		sbuf_cat(sb, "\n");
1476 	} else if (sc->sc_active && sc->sc_active->provider) {
1477 		sbuf_printf(sb, "%s\n", sc->sc_active->provider->name);
1478 	} else {
1479 		sbuf_cat(sb, "none\n");
1480 	}
1481 	sbuf_finish(sb);
1482 	gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1483 	sbuf_delete(sb);
1484 }
1485 
1486 static void
1487 g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1488 {
1489 	uint32_t *version;
1490 	g_topology_assert();
1491 	version = gctl_get_paraml(req, "version", sizeof(*version));
1492 	if (version == NULL) {
1493 		gctl_error(req, "No 'version' argument");
1494 	} else if (*version != G_MULTIPATH_VERSION) {
1495 		gctl_error(req, "Userland and kernel parts are out of sync");
1496 	} else if (strcmp(verb, "add") == 0) {
1497 		g_multipath_ctl_add(req, mp);
1498 	} else if (strcmp(verb, "prefer") == 0) {
1499 		g_multipath_ctl_prefer(req, mp);
1500 	} else if (strcmp(verb, "create") == 0) {
1501 		g_multipath_ctl_create(req, mp);
1502 	} else if (strcmp(verb, "configure") == 0) {
1503 		g_multipath_ctl_configure(req, mp);
1504 	} else if (strcmp(verb, "stop") == 0) {
1505 		g_multipath_ctl_stop(req, mp);
1506 	} else if (strcmp(verb, "destroy") == 0) {
1507 		g_multipath_ctl_destroy(req, mp);
1508 	} else if (strcmp(verb, "fail") == 0) {
1509 		g_multipath_ctl_fail(req, mp, 1);
1510 	} else if (strcmp(verb, "restore") == 0) {
1511 		g_multipath_ctl_fail(req, mp, 0);
1512 	} else if (strcmp(verb, "remove") == 0) {
1513 		g_multipath_ctl_remove(req, mp);
1514 	} else if (strcmp(verb, "rotate") == 0) {
1515 		g_multipath_ctl_rotate(req, mp);
1516 	} else if (strcmp(verb, "getactive") == 0) {
1517 		g_multipath_ctl_getactive(req, mp);
1518 	} else {
1519 		gctl_error(req, "Unknown verb %s", verb);
1520 	}
1521 }
1522 
1523 static void
1524 g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1525     struct g_consumer *cp, struct g_provider *pp)
1526 {
1527 	struct g_multipath_softc *sc;
1528 	int good;
1529 
1530 	g_topology_assert();
1531 
1532 	sc = gp->softc;
1533 	if (sc == NULL)
1534 		return;
1535 	if (cp != NULL) {
1536 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1537 		    (cp->index & MP_NEW) ? "NEW" :
1538 		    (cp->index & MP_LOST) ? "LOST" :
1539 		    (cp->index & MP_FAIL) ? "FAIL" :
1540 		    (sc->sc_active_active == 1 || sc->sc_active == cp) ?
1541 		     "ACTIVE" :
1542 		     sc->sc_active_active == 2 ? "READ" : "PASSIVE");
1543 	} else {
1544 		good = g_multipath_good(gp);
1545 		sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1546 		    good == 0 ? "BROKEN" :
1547 		    (good != sc->sc_ndisks || sc->sc_ndisks == 1) ?
1548 		    "DEGRADED" : "OPTIMAL");
1549 	}
1550 	if (cp == NULL && pp == NULL) {
1551 		sbuf_printf(sb, "%s<UUID>%s</UUID>\n", indent, sc->sc_uuid);
1552 		sbuf_printf(sb, "%s<Mode>Active/%s</Mode>\n", indent,
1553 		    sc->sc_active_active == 2 ? "Read" :
1554 		    sc->sc_active_active == 1 ? "Active" : "Passive");
1555 		sbuf_printf(sb, "%s<Type>%s</Type>\n", indent,
1556 		    sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC");
1557 	}
1558 }
1559 
1560 DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);
1561 MODULE_VERSION(geom_multipath, 0);
1562