2 * Copyright (c) 2011-2013 Alexander Motin <mav@FreeBSD.org>
3 * Copyright (c) 2006-2007 Matthew Jacob <mjacob@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Based upon work by Pawel Jakub Dawidek <pjd@FreeBSD.org> for all of the
29 * fine geom examples, and by Poul Henning Kamp <phk@FreeBSD.org> for GEOM
30 * itself, all of which is most gratefully acknowledged.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/limits.h>
41 #include <sys/mutex.h>
44 #include <sys/sysctl.h>
45 #include <sys/kthread.h>
46 #include <sys/malloc.h>
47 #include <geom/geom.h>
48 #include <geom/multipath/g_multipath.h>
50 FEATURE(geom_multipath, "GEOM multipath support");
52 SYSCTL_DECL(_kern_geom);
53 static SYSCTL_NODE(_kern_geom, OID_AUTO, multipath, CTLFLAG_RW, 0,
54 "GEOM_MULTIPATH tunables");
55 static u_int g_multipath_debug = 0;
56 SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, debug, CTLFLAG_RW,
57 &g_multipath_debug, 0, "Debug level");
58 static u_int g_multipath_exclusive = 1;
59 SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, exclusive, CTLFLAG_RW,
60 &g_multipath_exclusive, 0, "Exclusively open providers");
66 } g_multipath_kt_state;
67 static struct bio_queue_head gmtbq;
68 static struct mtx gmtbq_mtx;
70 static int g_multipath_read_metadata(struct g_consumer *cp,
71 struct g_multipath_metadata *md);
72 static int g_multipath_write_metadata(struct g_consumer *cp,
73 struct g_multipath_metadata *md);
75 static void g_multipath_orphan(struct g_consumer *);
76 static void g_multipath_resize(struct g_consumer *);
77 static void g_multipath_start(struct bio *);
78 static void g_multipath_done(struct bio *);
79 static void g_multipath_done_error(struct bio *);
80 static void g_multipath_kt(void *);
82 static int g_multipath_destroy(struct g_geom *);
84 g_multipath_destroy_geom(struct gctl_req *, struct g_class *, struct g_geom *);
86 static struct g_geom *g_multipath_find_geom(struct g_class *, const char *);
87 static int g_multipath_rotate(struct g_geom *);
89 static g_taste_t g_multipath_taste;
90 static g_ctl_req_t g_multipath_config;
91 static g_init_t g_multipath_init;
92 static g_fini_t g_multipath_fini;
93 static g_dumpconf_t g_multipath_dumpconf;
95 struct g_class g_multipath_class = {
96 .name = G_MULTIPATH_CLASS_NAME,
98 .ctlreq = g_multipath_config,
99 .taste = g_multipath_taste,
100 .destroy_geom = g_multipath_destroy_geom,
101 .init = g_multipath_init,
102 .fini = g_multipath_fini
105 #define MP_FAIL 0x00000001
106 #define MP_LOST 0x00000002
107 #define MP_NEW 0x00000004
108 #define MP_POSTED 0x00000008
109 #define MP_BAD (MP_FAIL | MP_LOST | MP_NEW)
110 #define MP_WITHER 0x00000010
111 #define MP_IDLE 0x00000020
112 #define MP_IDLE_MASK 0xffffffe0
115 g_multipath_good(struct g_geom *gp)
117 struct g_consumer *cp;
120 LIST_FOREACH(cp, &gp->consumer, consumer) {
121 if ((cp->index & MP_BAD) == 0)
128 g_multipath_fault(struct g_consumer *cp, int cause)
130 struct g_multipath_softc *sc;
131 struct g_consumer *lcp;
137 if (g_multipath_good(gp) == 0 && sc->sc_ndisks > 0) {
138 LIST_FOREACH(lcp, &gp->consumer, consumer) {
139 if (lcp->provider == NULL ||
140 (lcp->index & (MP_LOST | MP_NEW)))
142 if (sc->sc_ndisks > 1 && lcp == cp)
144 printf("GEOM_MULTIPATH: "
145 "all paths in %s were marked FAIL, restore %s\n",
146 sc->sc_name, lcp->provider->name);
147 lcp->index &= ~MP_FAIL;
150 if (cp != sc->sc_active)
152 sc->sc_active = NULL;
153 LIST_FOREACH(lcp, &gp->consumer, consumer) {
154 if ((lcp->index & MP_BAD) == 0) {
159 if (sc->sc_active == NULL) {
160 printf("GEOM_MULTIPATH: out of providers for %s\n",
162 } else if (sc->sc_active_active != 1) {
163 printf("GEOM_MULTIPATH: %s is now active path in %s\n",
164 sc->sc_active->provider->name, sc->sc_name);
168 static struct g_consumer *
169 g_multipath_choose(struct g_geom *gp, struct bio *bp)
171 struct g_multipath_softc *sc;
172 struct g_consumer *best, *cp;
175 if (sc->sc_active_active == 0 ||
176 (sc->sc_active_active == 2 && bp->bio_cmd != BIO_READ))
177 return (sc->sc_active);
179 LIST_FOREACH(cp, &gp->consumer, consumer) {
180 if (cp->index & MP_BAD)
182 cp->index += MP_IDLE;
183 if (best == NULL || cp->private < best->private ||
184 (cp->private == best->private && cp->index > best->index))
188 best->index &= ~MP_IDLE_MASK;
193 g_mpd(void *arg, int flags __unused)
196 struct g_multipath_softc *sc;
197 struct g_consumer *cp;
203 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) {
205 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
206 if (w > 0 && cp->provider != NULL &&
207 (cp->provider->geom->flags & G_GEOM_WITHER) == 0) {
208 cp->index |= MP_WITHER;
209 g_post_event(g_mpd, cp, M_WAITOK, NULL);
214 mtx_lock(&sc->sc_mtx);
216 printf("GEOM_MULTIPATH: %s removed from %s\n",
217 cp->provider->name, gp->name);
220 g_destroy_consumer(cp);
221 mtx_unlock(&sc->sc_mtx);
222 if (LIST_EMPTY(&gp->consumer))
223 g_multipath_destroy(gp);
227 g_multipath_orphan(struct g_consumer *cp)
229 struct g_multipath_softc *sc;
233 printf("GEOM_MULTIPATH: %s in %s was disconnected\n",
234 cp->provider->name, cp->geom->name);
235 sc = cp->geom->softc;
236 cnt = (uintptr_t *)&cp->private;
237 mtx_lock(&sc->sc_mtx);
239 g_multipath_fault(cp, MP_LOST);
240 if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
241 cp->index |= MP_POSTED;
242 mtx_unlock(&sc->sc_mtx);
245 mtx_unlock(&sc->sc_mtx);
249 g_multipath_resize(struct g_consumer *cp)
251 struct g_multipath_softc *sc;
253 struct g_consumer *cp1;
254 struct g_provider *pp;
255 struct g_multipath_metadata md;
256 off_t size, psize, ssize;
268 if (pp->mediasize < sc->sc_size) {
269 size = pp->mediasize;
270 ssize = pp->sectorsize;
272 size = ssize = OFF_MAX;
273 mtx_lock(&sc->sc_mtx);
274 LIST_FOREACH(cp1, &gp->consumer, consumer) {
278 if (pp->mediasize < size) {
279 size = pp->mediasize;
280 ssize = pp->sectorsize;
283 mtx_unlock(&sc->sc_mtx);
284 if (size == OFF_MAX || size == sc->sc_size)
287 psize = size - ((sc->sc_uuid[0] != 0) ? ssize : 0);
288 printf("GEOM_MULTIPATH: %s size changed from %jd to %jd\n",
289 sc->sc_name, sc->sc_pp->mediasize, psize);
290 if (sc->sc_uuid[0] != 0 && size < sc->sc_size) {
291 error = g_multipath_read_metadata(cp, &md);
293 (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) ||
294 (memcmp(md.md_uuid, sc->sc_uuid, sizeof(sc->sc_uuid)) != 0) ||
295 (strcmp(md.md_name, sc->sc_name) != 0) ||
296 (md.md_size != 0 && md.md_size != size) ||
297 (md.md_sectorsize != 0 && md.md_sectorsize != ssize)) {
298 g_multipath_destroy(gp);
303 g_resize_provider(sc->sc_pp, psize);
305 if (sc->sc_uuid[0] != 0) {
307 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
308 memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
309 strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
310 md.md_version = G_MULTIPATH_VERSION;
312 md.md_sectorsize = ssize;
313 md.md_active_active = sc->sc_active_active;
314 error = g_multipath_write_metadata(cp, &md);
316 printf("GEOM_MULTIPATH: Can't update metadata on %s "
317 "(%d)\n", pp->name, error);
322 g_multipath_start(struct bio *bp)
324 struct g_multipath_softc *sc;
326 struct g_consumer *cp;
330 gp = bp->bio_to->geom;
332 KASSERT(sc != NULL, ("NULL sc"));
333 cbp = g_clone_bio(bp);
335 g_io_deliver(bp, ENOMEM);
338 mtx_lock(&sc->sc_mtx);
339 cp = g_multipath_choose(gp, bp);
341 mtx_unlock(&sc->sc_mtx);
343 g_io_deliver(bp, ENXIO);
346 if ((uintptr_t)bp->bio_driver1 < sc->sc_ndisks)
347 bp->bio_driver1 = (void *)(uintptr_t)sc->sc_ndisks;
348 cnt = (uintptr_t *)&cp->private;
350 mtx_unlock(&sc->sc_mtx);
351 cbp->bio_done = g_multipath_done;
352 g_io_request(cbp, cp);
356 g_multipath_done(struct bio *bp)
358 struct g_multipath_softc *sc;
359 struct g_consumer *cp;
362 if (bp->bio_error == ENXIO || bp->bio_error == EIO) {
363 mtx_lock(&gmtbq_mtx);
364 bioq_insert_tail(&gmtbq, bp);
365 mtx_unlock(&gmtbq_mtx);
366 wakeup(&g_multipath_kt_state);
369 sc = cp->geom->softc;
370 cnt = (uintptr_t *)&cp->private;
371 mtx_lock(&sc->sc_mtx);
373 if (*cnt == 0 && (cp->index & MP_LOST)) {
374 if (g_post_event(g_mpd, cp, M_NOWAIT, NULL) == 0)
375 cp->index |= MP_POSTED;
376 mtx_unlock(&sc->sc_mtx);
378 mtx_unlock(&sc->sc_mtx);
384 g_multipath_done_error(struct bio *bp)
388 struct g_multipath_softc *sc;
389 struct g_consumer *cp;
390 struct g_provider *pp;
394 * If we had a failure, we have to check first to see
395 * whether the consumer it failed on was the currently
396 * active consumer (i.e., this is the first in perhaps
397 * a number of failures). If so, we then switch consumers
398 * to the next available consumer.
401 pbp = bp->bio_parent;
402 gp = pbp->bio_to->geom;
406 cnt = (uintptr_t *)&cp->private;
408 mtx_lock(&sc->sc_mtx);
409 if ((cp->index & MP_FAIL) == 0) {
410 printf("GEOM_MULTIPATH: Error %d, %s in %s marked FAIL\n",
411 bp->bio_error, pp->name, sc->sc_name);
412 g_multipath_fault(cp, MP_FAIL);
415 if (*cnt == 0 && (cp->index & (MP_LOST | MP_POSTED)) == MP_LOST) {
416 cp->index |= MP_POSTED;
417 mtx_unlock(&sc->sc_mtx);
418 g_post_event(g_mpd, cp, M_WAITOK, NULL);
420 mtx_unlock(&sc->sc_mtx);
423 * If we can fruitfully restart the I/O, do so.
425 if (pbp->bio_children < (uintptr_t)pbp->bio_driver1) {
428 g_multipath_start(pbp);
435 g_multipath_kt(void *arg)
438 g_multipath_kt_state = GKT_RUN;
439 mtx_lock(&gmtbq_mtx);
440 while (g_multipath_kt_state == GKT_RUN) {
444 bp = bioq_takefirst(&gmtbq);
447 mtx_unlock(&gmtbq_mtx);
448 g_multipath_done_error(bp);
449 mtx_lock(&gmtbq_mtx);
451 if (g_multipath_kt_state != GKT_RUN)
453 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
456 mtx_unlock(&gmtbq_mtx);
457 wakeup(&g_multipath_kt_state);
463 g_multipath_access(struct g_provider *pp, int dr, int dw, int de)
466 struct g_consumer *cp, *badcp = NULL;
467 struct g_multipath_softc *sc;
472 /* Error used if we have no valid consumers. */
473 error = (dr > 0 || dw > 0 || de > 0) ? ENXIO : 0;
475 LIST_FOREACH(cp, &gp->consumer, consumer) {
476 if (cp->index & MP_WITHER)
479 error = g_access(cp, dr, dw, de);
490 sc->sc_opened += dr + dw + de;
491 if (sc->sc_stopping && sc->sc_opened == 0)
492 g_multipath_destroy(gp);
497 LIST_FOREACH(cp, &gp->consumer, consumer) {
500 if (cp->index & MP_WITHER)
503 (void) g_access(cp, -dr, -dw, -de);
508 static struct g_geom *
509 g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md)
511 struct g_multipath_softc *sc;
513 struct g_provider *pp;
517 LIST_FOREACH(gp, &mp->geom, geom) {
519 if (sc == NULL || sc->sc_stopping)
521 if (strcmp(gp->name, md->md_name) == 0) {
522 printf("GEOM_MULTIPATH: name %s already exists\n",
528 gp = g_new_geomf(mp, "%s", md->md_name);
529 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
530 mtx_init(&sc->sc_mtx, "multipath", NULL, MTX_DEF);
531 memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid));
532 memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name));
533 sc->sc_active_active = md->md_active_active;
534 sc->sc_size = md->md_size;
536 gp->start = g_multipath_start;
537 gp->orphan = g_multipath_orphan;
538 gp->resize = g_multipath_resize;
539 gp->access = g_multipath_access;
540 gp->dumpconf = g_multipath_dumpconf;
542 pp = g_new_providerf(gp, "multipath/%s", md->md_name);
543 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
544 if (md->md_size != 0) {
545 pp->mediasize = md->md_size -
546 ((md->md_uuid[0] != 0) ? md->md_sectorsize : 0);
547 pp->sectorsize = md->md_sectorsize;
550 g_error_provider(pp, 0);
551 printf("GEOM_MULTIPATH: %s created\n", gp->name);
556 g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp)
558 struct g_multipath_softc *sc;
559 struct g_consumer *cp, *nxtcp;
560 int error, acr, acw, ace;
565 KASSERT(sc, ("no softc"));
568 * Make sure that the passed provider isn't already attached
570 LIST_FOREACH(cp, &gp->consumer, consumer) {
571 if (cp->provider == pp)
575 printf("GEOM_MULTIPATH: provider %s already attached to %s\n",
579 nxtcp = LIST_FIRST(&gp->consumer);
580 cp = g_new_consumer(gp);
581 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
584 error = g_attach(cp, pp);
586 printf("GEOM_MULTIPATH: cannot attach %s to %s",
587 pp->name, sc->sc_name);
588 g_destroy_consumer(cp);
593 * Set access permissions on new consumer to match other consumers
596 acr = sc->sc_pp->acr;
597 acw = sc->sc_pp->acw;
598 ace = sc->sc_pp->ace;
601 if (g_multipath_exclusive) {
606 error = g_access(cp, acr, acw, ace);
608 printf("GEOM_MULTIPATH: cannot set access in "
609 "attaching %s to %s (%d)\n",
610 pp->name, sc->sc_name, error);
612 g_destroy_consumer(cp);
615 if (sc->sc_size == 0) {
616 sc->sc_size = pp->mediasize -
617 ((sc->sc_uuid[0] != 0) ? pp->sectorsize : 0);
618 sc->sc_pp->mediasize = sc->sc_size;
619 sc->sc_pp->sectorsize = pp->sectorsize;
621 if (sc->sc_pp->stripesize == 0 && sc->sc_pp->stripeoffset == 0) {
622 sc->sc_pp->stripesize = pp->stripesize;
623 sc->sc_pp->stripeoffset = pp->stripeoffset;
625 sc->sc_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
626 mtx_lock(&sc->sc_mtx);
629 mtx_unlock(&sc->sc_mtx);
630 printf("GEOM_MULTIPATH: %s added to %s\n",
631 pp->name, sc->sc_name);
632 if (sc->sc_active == NULL) {
634 if (sc->sc_active_active != 1)
635 printf("GEOM_MULTIPATH: %s is now active path in %s\n",
636 pp->name, sc->sc_name);
642 g_multipath_destroy(struct g_geom *gp)
644 struct g_multipath_softc *sc;
645 struct g_consumer *cp, *cp1;
648 if (gp->softc == NULL)
651 if (!sc->sc_stopping) {
652 printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
655 if (sc->sc_opened != 0) {
656 g_wither_provider(sc->sc_pp, ENXIO);
658 return (EINPROGRESS);
660 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
661 mtx_lock(&sc->sc_mtx);
662 if ((cp->index & MP_POSTED) == 0) {
663 cp->index |= MP_POSTED;
664 mtx_unlock(&sc->sc_mtx);
667 return(0); /* Recursion happened. */
669 mtx_unlock(&sc->sc_mtx);
671 if (!LIST_EMPTY(&gp->consumer))
672 return (EINPROGRESS);
673 mtx_destroy(&sc->sc_mtx);
676 printf("GEOM_MULTIPATH: %s destroyed\n", gp->name);
677 g_wither_geom(gp, ENXIO);
682 g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
686 return (g_multipath_destroy(gp));
690 g_multipath_rotate(struct g_geom *gp)
692 struct g_consumer *lcp, *first_good_cp = NULL;
693 struct g_multipath_softc *sc = gp->softc;
694 int active_cp_seen = 0;
699 LIST_FOREACH(lcp, &gp->consumer, consumer) {
700 if ((lcp->index & MP_BAD) == 0) {
701 if (first_good_cp == NULL)
706 if (sc->sc_active == lcp)
711 if (lcp && lcp != sc->sc_active) {
713 if (sc->sc_active_active != 1)
714 printf("GEOM_MULTIPATH: %s is now active path in %s\n",
715 lcp->provider->name, sc->sc_name);
721 g_multipath_init(struct g_class *mp)
724 mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
725 kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt");
729 g_multipath_fini(struct g_class *mp)
731 if (g_multipath_kt_state == GKT_RUN) {
732 mtx_lock(&gmtbq_mtx);
733 g_multipath_kt_state = GKT_DIE;
734 wakeup(&g_multipath_kt_state);
735 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
737 mtx_unlock(&gmtbq_mtx);
742 g_multipath_read_metadata(struct g_consumer *cp,
743 struct g_multipath_metadata *md)
745 struct g_provider *pp;
750 error = g_access(cp, 1, 0, 0);
755 buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
756 pp->sectorsize, &error);
758 g_access(cp, -1, 0, 0);
761 multipath_metadata_decode(buf, md);
767 g_multipath_write_metadata(struct g_consumer *cp,
768 struct g_multipath_metadata *md)
770 struct g_provider *pp;
775 error = g_access(cp, 1, 1, 1);
780 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
781 multipath_metadata_encode(md, buf);
782 error = g_write_data(cp, pp->mediasize - pp->sectorsize,
783 buf, pp->sectorsize);
785 g_access(cp, -1, -1, -1);
790 static struct g_geom *
791 g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
793 struct g_multipath_metadata md;
794 struct g_multipath_softc *sc;
795 struct g_consumer *cp;
796 struct g_geom *gp, *gp1;
801 gp = g_new_geomf(mp, "multipath:taste");
802 gp->start = g_multipath_start;
803 gp->access = g_multipath_access;
804 gp->orphan = g_multipath_orphan;
805 cp = g_new_consumer(gp);
807 error = g_multipath_read_metadata(cp, &md);
809 g_destroy_consumer(cp);
815 if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
816 if (g_multipath_debug)
817 printf("%s is not MULTIPATH\n", pp->name);
820 if (md.md_version != G_MULTIPATH_VERSION) {
821 printf("%s has version %d multipath id- this module is version "
822 " %d: rejecting\n", pp->name, md.md_version,
823 G_MULTIPATH_VERSION);
826 if (md.md_size != 0 && md.md_size != pp->mediasize)
828 if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize)
830 if (g_multipath_debug)
831 printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
834 * Let's check if such a device already is present. We check against
835 * uuid alone first because that's the true distinguishor. If that
836 * passes, then we check for name conflicts. If there are conflicts,
839 * The whole purpose of this is to solve the problem that people don't
840 * pick good unique names, but good unique names (like uuids) are a
841 * pain to use. So, we allow people to build GEOMs with friendly names
842 * and uuids, and modify the names in case there's a collision.
845 LIST_FOREACH(gp, &mp->geom, geom) {
847 if (sc == NULL || sc->sc_stopping)
849 if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
853 LIST_FOREACH(gp1, &mp->geom, geom) {
857 if (sc == NULL || sc->sc_stopping)
859 if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
864 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
866 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
867 * with the same name (but a different UUID).
869 * If gp is NULL, then modify the name with a random number and
870 * complain, but allow the creation of the geom to continue.
872 * If gp is *not* NULL, just use the geom's name as we're attaching
873 * this disk to the (previously generated) name.
880 u_long rand = random();
882 snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
883 printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
884 sc->sc_name, sc->sc_uuid);
885 printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
887 strlcpy(md.md_name, buf, sizeof(md.md_name));
889 strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
894 gp = g_multipath_create(mp, &md);
896 printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
897 md.md_name, md.md_uuid);
906 KASSERT(sc != NULL, ("sc is NULL"));
907 error = g_multipath_add_disk(gp, pp);
910 g_multipath_destroy(gp);
917 g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp,
920 struct g_multipath_softc *sc;
922 struct g_consumer *cp;
923 struct g_provider *pp;
925 static const char devpf[6] = "/dev/";
930 mpname = gctl_get_asciiparam(req, "arg0");
931 if (mpname == NULL) {
932 gctl_error(req, "No 'arg0' argument");
935 gp = g_multipath_find_geom(mp, mpname);
937 gctl_error(req, "Device %s is invalid", mpname);
942 if (strncmp(name, devpf, 5) == 0)
944 pp = g_provider_by_name(name);
946 gctl_error(req, "Provider %s is invalid", name);
951 * Check to make sure parameters match.
953 LIST_FOREACH(cp, &gp->consumer, consumer) {
954 if (cp->provider == pp) {
955 gctl_error(req, "provider %s is already there",
960 if (sc->sc_pp->mediasize != 0 &&
961 sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0)
963 gctl_error(req, "Providers size mismatch %jd != %jd",
964 (intmax_t) sc->sc_pp->mediasize +
965 (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0),
966 (intmax_t) pp->mediasize);
969 if (sc->sc_pp->sectorsize != 0 &&
970 sc->sc_pp->sectorsize != pp->sectorsize) {
971 gctl_error(req, "Providers sectorsize mismatch %u != %u",
972 sc->sc_pp->sectorsize, pp->sectorsize);
976 error = g_multipath_add_disk(gp, pp);
978 gctl_error(req, "Provider addition error: %d", error);
982 g_multipath_ctl_prefer(struct gctl_req *req, struct g_class *mp)
985 struct g_multipath_softc *sc;
986 struct g_consumer *cp;
987 const char *name, *mpname;
988 static const char devpf[6] = "/dev/";
993 mpname = gctl_get_asciiparam(req, "arg0");
994 if (mpname == NULL) {
995 gctl_error(req, "No 'arg0' argument");
998 gp = g_multipath_find_geom(mp, mpname);
1000 gctl_error(req, "Device %s is invalid", mpname);
1005 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1006 if (nargs == NULL) {
1007 gctl_error(req, "No 'nargs' argument");
1011 gctl_error(req, "missing device");
1015 name = gctl_get_asciiparam(req, "arg1");
1017 gctl_error(req, "No 'arg1' argument");
1020 if (strncmp(name, devpf, 5) == 0) {
1024 LIST_FOREACH(cp, &gp->consumer, consumer) {
1025 if (cp->provider != NULL
1026 && strcmp(cp->provider->name, name) == 0)
1031 gctl_error(req, "Provider %s not found", name);
1035 mtx_lock(&sc->sc_mtx);
1037 if (cp->index & MP_BAD) {
1038 gctl_error(req, "Consumer %s is invalid", name);
1039 mtx_unlock(&sc->sc_mtx);
1043 /* Here when the consumer is present and in good shape */
1046 if (!sc->sc_active_active)
1047 printf("GEOM_MULTIPATH: %s now active path in %s\n",
1048 sc->sc_active->provider->name, sc->sc_name);
1050 mtx_unlock(&sc->sc_mtx);
1054 g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp)
1056 struct g_multipath_softc *sc;
1058 const char *mpname, *name;
1060 mpname = gctl_get_asciiparam(req, "arg0");
1061 if (mpname == NULL) {
1062 gctl_error(req, "No 'arg0' argument");
1065 gp = g_multipath_find_geom(mp, mpname);
1067 gctl_error(req, "Device %s not found", mpname);
1072 name = gctl_get_asciiparam(req, "arg1");
1074 gctl_error(req, "No 'arg1' argument");
1077 g_multipath_ctl_add_name(req, mp, name);
1081 g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp)
1083 struct g_multipath_metadata md;
1084 struct g_multipath_softc *sc;
1086 const char *mpname, *name;
1088 int *nargs, i, *val;
1090 g_topology_assert();
1092 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1094 gctl_error(req, "wrong number of arguments.");
1098 mpname = gctl_get_asciiparam(req, "arg0");
1099 if (mpname == NULL) {
1100 gctl_error(req, "No 'arg0' argument");
1103 gp = g_multipath_find_geom(mp, mpname);
1105 gctl_error(req, "Device %s already exist", mpname);
1109 memset(&md, 0, sizeof(md));
1110 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1111 md.md_version = G_MULTIPATH_VERSION;
1112 strlcpy(md.md_name, mpname, sizeof(md.md_name));
1114 md.md_sectorsize = 0;
1116 md.md_active_active = 0;
1117 val = gctl_get_paraml(req, "active_active", sizeof(*val));
1118 if (val != NULL && *val != 0)
1119 md.md_active_active = 1;
1120 val = gctl_get_paraml(req, "active_read", sizeof(*val));
1121 if (val != NULL && *val != 0)
1122 md.md_active_active = 2;
1123 gp = g_multipath_create(mp, &md);
1125 gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n",
1126 md.md_name, md.md_uuid);
1131 for (i = 1; i < *nargs; i++) {
1132 snprintf(param, sizeof(param), "arg%d", i);
1133 name = gctl_get_asciiparam(req, param);
1134 g_multipath_ctl_add_name(req, mp, name);
1137 if (sc->sc_ndisks != (*nargs - 1))
1138 g_multipath_destroy(gp);
1142 g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp)
1144 struct g_multipath_softc *sc;
1146 struct g_consumer *cp;
1147 struct g_provider *pp;
1148 struct g_multipath_metadata md;
1152 g_topology_assert();
1154 name = gctl_get_asciiparam(req, "arg0");
1156 gctl_error(req, "No 'arg0' argument");
1159 gp = g_multipath_find_geom(mp, name);
1161 gctl_error(req, "Device %s is invalid", name);
1165 val = gctl_get_paraml(req, "active_active", sizeof(*val));
1166 if (val != NULL && *val != 0)
1167 sc->sc_active_active = 1;
1168 val = gctl_get_paraml(req, "active_read", sizeof(*val));
1169 if (val != NULL && *val != 0)
1170 sc->sc_active_active = 2;
1171 val = gctl_get_paraml(req, "active_passive", sizeof(*val));
1172 if (val != NULL && *val != 0)
1173 sc->sc_active_active = 0;
1174 if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
1177 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1178 memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
1179 strlcpy(md.md_name, name, sizeof(md.md_name));
1180 md.md_version = G_MULTIPATH_VERSION;
1181 md.md_size = pp->mediasize;
1182 md.md_sectorsize = pp->sectorsize;
1183 md.md_active_active = sc->sc_active_active;
1184 error = g_multipath_write_metadata(cp, &md);
1186 gctl_error(req, "Can't update metadata on %s (%d)",
1192 g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail)
1194 struct g_multipath_softc *sc;
1196 struct g_consumer *cp;
1197 const char *mpname, *name;
1200 mpname = gctl_get_asciiparam(req, "arg0");
1201 if (mpname == NULL) {
1202 gctl_error(req, "No 'arg0' argument");
1205 gp = g_multipath_find_geom(mp, mpname);
1207 gctl_error(req, "Device %s not found", mpname);
1212 name = gctl_get_asciiparam(req, "arg1");
1214 gctl_error(req, "No 'arg1' argument");
1219 mtx_lock(&sc->sc_mtx);
1220 LIST_FOREACH(cp, &gp->consumer, consumer) {
1221 if (cp->provider != NULL &&
1222 strcmp(cp->provider->name, name) == 0 &&
1223 (cp->index & MP_LOST) == 0) {
1225 if (!fail == !(cp->index & MP_FAIL))
1227 printf("GEOM_MULTIPATH: %s in %s is marked %s.\n",
1228 name, sc->sc_name, fail ? "FAIL" : "OK");
1230 g_multipath_fault(cp, MP_FAIL);
1232 cp->index &= ~MP_FAIL;
1236 mtx_unlock(&sc->sc_mtx);
1238 gctl_error(req, "Provider %s not found", name);
1242 g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp)
1244 struct g_multipath_softc *sc;
1246 struct g_consumer *cp, *cp1;
1247 const char *mpname, *name;
1251 mpname = gctl_get_asciiparam(req, "arg0");
1252 if (mpname == NULL) {
1253 gctl_error(req, "No 'arg0' argument");
1256 gp = g_multipath_find_geom(mp, mpname);
1258 gctl_error(req, "Device %s not found", mpname);
1263 name = gctl_get_asciiparam(req, "arg1");
1265 gctl_error(req, "No 'arg1' argument");
1270 mtx_lock(&sc->sc_mtx);
1271 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
1272 if (cp->provider != NULL &&
1273 strcmp(cp->provider->name, name) == 0 &&
1274 (cp->index & MP_LOST) == 0) {
1276 printf("GEOM_MULTIPATH: removing %s from %s\n",
1277 cp->provider->name, cp->geom->name);
1279 g_multipath_fault(cp, MP_LOST);
1280 cnt = (uintptr_t *)&cp->private;
1281 if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
1282 cp->index |= MP_POSTED;
1283 mtx_unlock(&sc->sc_mtx);
1286 return; /* Recursion happened. */
1287 mtx_lock(&sc->sc_mtx);
1291 mtx_unlock(&sc->sc_mtx);
1293 gctl_error(req, "Provider %s not found", name);
1296 static struct g_geom *
1297 g_multipath_find_geom(struct g_class *mp, const char *name)
1300 struct g_multipath_softc *sc;
1302 LIST_FOREACH(gp, &mp->geom, geom) {
1304 if (sc == NULL || sc->sc_stopping)
1306 if (strcmp(gp->name, name) == 0)
1313 g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp)
1319 g_topology_assert();
1321 name = gctl_get_asciiparam(req, "arg0");
1323 gctl_error(req, "No 'arg0' argument");
1326 gp = g_multipath_find_geom(mp, name);
1328 gctl_error(req, "Device %s is invalid", name);
1331 error = g_multipath_destroy(gp);
1332 if (error != 0 && error != EINPROGRESS)
1333 gctl_error(req, "failed to stop %s (err=%d)", name, error);
1337 g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1340 struct g_multipath_softc *sc;
1341 struct g_consumer *cp;
1342 struct g_provider *pp;
1347 g_topology_assert();
1349 name = gctl_get_asciiparam(req, "arg0");
1351 gctl_error(req, "No 'arg0' argument");
1354 gp = g_multipath_find_geom(mp, name);
1356 gctl_error(req, "Device %s is invalid", name);
1361 if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
1364 error = g_access(cp, 1, 1, 1);
1366 gctl_error(req, "Can't open %s (%d)", pp->name, error);
1369 g_topology_unlock();
1370 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1371 error = g_write_data(cp, pp->mediasize - pp->sectorsize,
1372 buf, pp->sectorsize);
1374 g_access(cp, -1, -1, -1);
1376 gctl_error(req, "Can't erase metadata on %s (%d)",
1381 error = g_multipath_destroy(gp);
1382 if (error != 0 && error != EINPROGRESS)
1383 gctl_error(req, "failed to destroy %s (err=%d)", name, error);
1387 g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp)
1393 g_topology_assert();
1395 name = gctl_get_asciiparam(req, "arg0");
1397 gctl_error(req, "No 'arg0' argument");
1400 gp = g_multipath_find_geom(mp, name);
1402 gctl_error(req, "Device %s is invalid", name);
1405 error = g_multipath_rotate(gp);
1407 gctl_error(req, "failed to rotate %s (err=%d)", name, error);
1412 g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp)
1416 struct g_multipath_softc *sc;
1417 struct g_consumer *cp;
1421 sb = sbuf_new_auto();
1423 g_topology_assert();
1424 name = gctl_get_asciiparam(req, "arg0");
1426 gctl_error(req, "No 'arg0' argument");
1429 gp = g_multipath_find_geom(mp, name);
1431 gctl_error(req, "Device %s is invalid", name);
1435 if (sc->sc_active_active == 1) {
1437 LIST_FOREACH(cp, &gp->consumer, consumer) {
1438 if (cp->index & MP_BAD)
1442 sbuf_cat(sb, cp->provider->name);
1446 sbuf_cat(sb, "none");
1448 } else if (sc->sc_active && sc->sc_active->provider) {
1449 sbuf_printf(sb, "%s\n", sc->sc_active->provider->name);
1451 sbuf_printf(sb, "none\n");
1454 gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1459 g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1462 g_topology_assert();
1463 version = gctl_get_paraml(req, "version", sizeof(*version));
1464 if (version == NULL) {
1465 gctl_error(req, "No 'version' argument");
1466 } else if (*version != G_MULTIPATH_VERSION) {
1467 gctl_error(req, "Userland and kernel parts are out of sync");
1468 } else if (strcmp(verb, "add") == 0) {
1469 g_multipath_ctl_add(req, mp);
1470 } else if (strcmp(verb, "prefer") == 0) {
1471 g_multipath_ctl_prefer(req, mp);
1472 } else if (strcmp(verb, "create") == 0) {
1473 g_multipath_ctl_create(req, mp);
1474 } else if (strcmp(verb, "configure") == 0) {
1475 g_multipath_ctl_configure(req, mp);
1476 } else if (strcmp(verb, "stop") == 0) {
1477 g_multipath_ctl_stop(req, mp);
1478 } else if (strcmp(verb, "destroy") == 0) {
1479 g_multipath_ctl_destroy(req, mp);
1480 } else if (strcmp(verb, "fail") == 0) {
1481 g_multipath_ctl_fail(req, mp, 1);
1482 } else if (strcmp(verb, "restore") == 0) {
1483 g_multipath_ctl_fail(req, mp, 0);
1484 } else if (strcmp(verb, "remove") == 0) {
1485 g_multipath_ctl_remove(req, mp);
1486 } else if (strcmp(verb, "rotate") == 0) {
1487 g_multipath_ctl_rotate(req, mp);
1488 } else if (strcmp(verb, "getactive") == 0) {
1489 g_multipath_ctl_getactive(req, mp);
1491 gctl_error(req, "Unknown verb %s", verb);
1496 g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1497 struct g_consumer *cp, struct g_provider *pp)
1499 struct g_multipath_softc *sc;
1502 g_topology_assert();
1508 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1509 (cp->index & MP_NEW) ? "NEW" :
1510 (cp->index & MP_LOST) ? "LOST" :
1511 (cp->index & MP_FAIL) ? "FAIL" :
1512 (sc->sc_active_active == 1 || sc->sc_active == cp) ?
1514 sc->sc_active_active == 2 ? "READ" : "PASSIVE");
1516 good = g_multipath_good(gp);
1517 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1518 good == 0 ? "BROKEN" :
1519 (good != sc->sc_ndisks || sc->sc_ndisks == 1) ?
1520 "DEGRADED" : "OPTIMAL");
1522 if (cp == NULL && pp == NULL) {
1523 sbuf_printf(sb, "%s<UUID>%s</UUID>\n", indent, sc->sc_uuid);
1524 sbuf_printf(sb, "%s<Mode>Active/%s</Mode>\n", indent,
1525 sc->sc_active_active == 2 ? "Read" :
1526 sc->sc_active_active == 1 ? "Active" : "Passive");
1527 sbuf_printf(sb, "%s<Type>%s</Type>\n", indent,
1528 sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC");
1532 DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);