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