2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011-2013 Alexander Motin <mav@FreeBSD.org>
5 * Copyright (c) 2006-2007 Matthew Jacob <mjacob@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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.
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>
43 #include <sys/mutex.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>
52 FEATURE(geom_multipath, "GEOM multipath support");
54 SYSCTL_DECL(_kern_geom);
55 static SYSCTL_NODE(_kern_geom, OID_AUTO, multipath, CTLFLAG_RW, 0,
56 "GEOM_MULTIPATH tunables");
57 static u_int g_multipath_debug = 0;
58 SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, debug, CTLFLAG_RW,
59 &g_multipath_debug, 0, "Debug level");
60 static u_int g_multipath_exclusive = 1;
61 SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, exclusive, CTLFLAG_RW,
62 &g_multipath_exclusive, 0, "Exclusively open providers");
68 } g_multipath_kt_state;
69 static struct bio_queue_head gmtbq;
70 static struct mtx gmtbq_mtx;
72 static int g_multipath_read_metadata(struct g_consumer *cp,
73 struct g_multipath_metadata *md);
74 static int g_multipath_write_metadata(struct g_consumer *cp,
75 struct g_multipath_metadata *md);
77 static void g_multipath_orphan(struct g_consumer *);
78 static void g_multipath_resize(struct g_consumer *);
79 static void g_multipath_start(struct bio *);
80 static void g_multipath_done(struct bio *);
81 static void g_multipath_done_error(struct bio *);
82 static void g_multipath_kt(void *);
84 static int g_multipath_destroy(struct g_geom *);
86 g_multipath_destroy_geom(struct gctl_req *, struct g_class *, struct g_geom *);
88 static struct g_geom *g_multipath_find_geom(struct g_class *, const char *);
89 static int g_multipath_rotate(struct g_geom *);
91 static g_taste_t g_multipath_taste;
92 static g_ctl_req_t g_multipath_config;
93 static g_init_t g_multipath_init;
94 static g_fini_t g_multipath_fini;
95 static g_dumpconf_t g_multipath_dumpconf;
97 struct g_class g_multipath_class = {
98 .name = G_MULTIPATH_CLASS_NAME,
100 .ctlreq = g_multipath_config,
101 .taste = g_multipath_taste,
102 .destroy_geom = g_multipath_destroy_geom,
103 .init = g_multipath_init,
104 .fini = g_multipath_fini
107 #define MP_FAIL 0x00000001
108 #define MP_LOST 0x00000002
109 #define MP_NEW 0x00000004
110 #define MP_POSTED 0x00000008
111 #define MP_BAD (MP_FAIL | MP_LOST | MP_NEW)
112 #define MP_WITHER 0x00000010
113 #define MP_IDLE 0x00000020
114 #define MP_IDLE_MASK 0xffffffe0
117 g_multipath_good(struct g_geom *gp)
119 struct g_consumer *cp;
122 LIST_FOREACH(cp, &gp->consumer, consumer) {
123 if ((cp->index & MP_BAD) == 0)
130 g_multipath_fault(struct g_consumer *cp, int cause)
132 struct g_multipath_softc *sc;
133 struct g_consumer *lcp;
139 if (g_multipath_good(gp) == 0 && sc->sc_ndisks > 0) {
140 LIST_FOREACH(lcp, &gp->consumer, consumer) {
141 if (lcp->provider == NULL ||
142 (lcp->index & (MP_LOST | MP_NEW)))
144 if (sc->sc_ndisks > 1 && lcp == cp)
146 printf("GEOM_MULTIPATH: "
147 "all paths in %s were marked FAIL, restore %s\n",
148 sc->sc_name, lcp->provider->name);
149 lcp->index &= ~MP_FAIL;
152 if (cp != sc->sc_active)
154 sc->sc_active = NULL;
155 LIST_FOREACH(lcp, &gp->consumer, consumer) {
156 if ((lcp->index & MP_BAD) == 0) {
161 if (sc->sc_active == NULL) {
162 printf("GEOM_MULTIPATH: out of providers for %s\n",
164 } else if (sc->sc_active_active != 1) {
165 printf("GEOM_MULTIPATH: %s is now active path in %s\n",
166 sc->sc_active->provider->name, sc->sc_name);
170 static struct g_consumer *
171 g_multipath_choose(struct g_geom *gp, struct bio *bp)
173 struct g_multipath_softc *sc;
174 struct g_consumer *best, *cp;
177 if (sc->sc_active_active == 0 ||
178 (sc->sc_active_active == 2 && bp->bio_cmd != BIO_READ))
179 return (sc->sc_active);
181 LIST_FOREACH(cp, &gp->consumer, consumer) {
182 if (cp->index & MP_BAD)
184 cp->index += MP_IDLE;
185 if (best == NULL || cp->private < best->private ||
186 (cp->private == best->private && cp->index > best->index))
190 best->index &= ~MP_IDLE_MASK;
195 g_mpd(void *arg, int flags __unused)
198 struct g_multipath_softc *sc;
199 struct g_consumer *cp;
205 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) {
207 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
208 if (w > 0 && cp->provider != NULL &&
209 (cp->provider->geom->flags & G_GEOM_WITHER) == 0) {
210 cp->index |= MP_WITHER;
211 g_post_event(g_mpd, cp, M_WAITOK, NULL);
216 mtx_lock(&sc->sc_mtx);
218 printf("GEOM_MULTIPATH: %s removed from %s\n",
219 cp->provider->name, gp->name);
222 g_destroy_consumer(cp);
223 mtx_unlock(&sc->sc_mtx);
224 if (LIST_EMPTY(&gp->consumer))
225 g_multipath_destroy(gp);
229 g_multipath_orphan(struct g_consumer *cp)
231 struct g_multipath_softc *sc;
235 printf("GEOM_MULTIPATH: %s in %s was disconnected\n",
236 cp->provider->name, cp->geom->name);
237 sc = cp->geom->softc;
238 cnt = (uintptr_t *)&cp->private;
239 mtx_lock(&sc->sc_mtx);
241 g_multipath_fault(cp, MP_LOST);
242 if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
243 cp->index |= MP_POSTED;
244 mtx_unlock(&sc->sc_mtx);
247 mtx_unlock(&sc->sc_mtx);
251 g_multipath_resize(struct g_consumer *cp)
253 struct g_multipath_softc *sc;
255 struct g_consumer *cp1;
256 struct g_provider *pp;
257 struct g_multipath_metadata md;
258 off_t size, psize, ssize;
270 if (pp->mediasize < sc->sc_size) {
271 size = pp->mediasize;
272 ssize = pp->sectorsize;
274 size = ssize = OFF_MAX;
275 mtx_lock(&sc->sc_mtx);
276 LIST_FOREACH(cp1, &gp->consumer, consumer) {
280 if (pp->mediasize < size) {
281 size = pp->mediasize;
282 ssize = pp->sectorsize;
285 mtx_unlock(&sc->sc_mtx);
286 if (size == OFF_MAX || size == sc->sc_size)
289 psize = size - ((sc->sc_uuid[0] != 0) ? ssize : 0);
290 printf("GEOM_MULTIPATH: %s size changed from %jd to %jd\n",
291 sc->sc_name, sc->sc_pp->mediasize, psize);
292 if (sc->sc_uuid[0] != 0 && size < sc->sc_size) {
293 error = g_multipath_read_metadata(cp, &md);
295 (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) ||
296 (memcmp(md.md_uuid, sc->sc_uuid, sizeof(sc->sc_uuid)) != 0) ||
297 (strcmp(md.md_name, sc->sc_name) != 0) ||
298 (md.md_size != 0 && md.md_size != size) ||
299 (md.md_sectorsize != 0 && md.md_sectorsize != ssize)) {
300 g_multipath_destroy(gp);
305 g_resize_provider(sc->sc_pp, psize);
307 if (sc->sc_uuid[0] != 0) {
309 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
310 memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
311 strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
312 md.md_version = G_MULTIPATH_VERSION;
314 md.md_sectorsize = ssize;
315 md.md_active_active = sc->sc_active_active;
316 error = g_multipath_write_metadata(cp, &md);
318 printf("GEOM_MULTIPATH: Can't update metadata on %s "
319 "(%d)\n", pp->name, error);
324 g_multipath_start(struct bio *bp)
326 struct g_multipath_softc *sc;
328 struct g_consumer *cp;
332 gp = bp->bio_to->geom;
334 KASSERT(sc != NULL, ("NULL sc"));
335 cbp = g_clone_bio(bp);
337 g_io_deliver(bp, ENOMEM);
340 mtx_lock(&sc->sc_mtx);
341 cp = g_multipath_choose(gp, bp);
343 mtx_unlock(&sc->sc_mtx);
345 g_io_deliver(bp, ENXIO);
348 if ((uintptr_t)bp->bio_driver1 < sc->sc_ndisks)
349 bp->bio_driver1 = (void *)(uintptr_t)sc->sc_ndisks;
350 cnt = (uintptr_t *)&cp->private;
352 mtx_unlock(&sc->sc_mtx);
353 cbp->bio_done = g_multipath_done;
354 g_io_request(cbp, cp);
358 g_multipath_done(struct bio *bp)
360 struct g_multipath_softc *sc;
361 struct g_consumer *cp;
364 if (bp->bio_error == ENXIO || bp->bio_error == EIO) {
365 mtx_lock(&gmtbq_mtx);
366 bioq_insert_tail(&gmtbq, bp);
367 mtx_unlock(&gmtbq_mtx);
368 wakeup(&g_multipath_kt_state);
371 sc = cp->geom->softc;
372 cnt = (uintptr_t *)&cp->private;
373 mtx_lock(&sc->sc_mtx);
375 if (*cnt == 0 && (cp->index & MP_LOST)) {
376 if (g_post_event(g_mpd, cp, M_NOWAIT, NULL) == 0)
377 cp->index |= MP_POSTED;
378 mtx_unlock(&sc->sc_mtx);
380 mtx_unlock(&sc->sc_mtx);
386 g_multipath_done_error(struct bio *bp)
390 struct g_multipath_softc *sc;
391 struct g_consumer *cp;
392 struct g_provider *pp;
396 * If we had a failure, we have to check first to see
397 * whether the consumer it failed on was the currently
398 * active consumer (i.e., this is the first in perhaps
399 * a number of failures). If so, we then switch consumers
400 * to the next available consumer.
403 pbp = bp->bio_parent;
404 gp = pbp->bio_to->geom;
408 cnt = (uintptr_t *)&cp->private;
410 mtx_lock(&sc->sc_mtx);
411 if ((cp->index & MP_FAIL) == 0) {
412 printf("GEOM_MULTIPATH: Error %d, %s in %s marked FAIL\n",
413 bp->bio_error, pp->name, sc->sc_name);
414 g_multipath_fault(cp, MP_FAIL);
417 if (*cnt == 0 && (cp->index & (MP_LOST | MP_POSTED)) == MP_LOST) {
418 cp->index |= MP_POSTED;
419 mtx_unlock(&sc->sc_mtx);
420 g_post_event(g_mpd, cp, M_WAITOK, NULL);
422 mtx_unlock(&sc->sc_mtx);
425 * If we can fruitfully restart the I/O, do so.
427 if (pbp->bio_children < (uintptr_t)pbp->bio_driver1) {
430 g_multipath_start(pbp);
437 g_multipath_kt(void *arg)
440 g_multipath_kt_state = GKT_RUN;
441 mtx_lock(&gmtbq_mtx);
442 while (g_multipath_kt_state == GKT_RUN) {
446 bp = bioq_takefirst(&gmtbq);
449 mtx_unlock(&gmtbq_mtx);
450 g_multipath_done_error(bp);
451 mtx_lock(&gmtbq_mtx);
453 if (g_multipath_kt_state != GKT_RUN)
455 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
458 mtx_unlock(&gmtbq_mtx);
459 wakeup(&g_multipath_kt_state);
465 g_multipath_access(struct g_provider *pp, int dr, int dw, int de)
468 struct g_consumer *cp, *badcp = NULL;
469 struct g_multipath_softc *sc;
474 /* Error used if we have no valid consumers. */
475 error = (dr > 0 || dw > 0 || de > 0) ? ENXIO : 0;
477 LIST_FOREACH(cp, &gp->consumer, consumer) {
478 if (cp->index & MP_WITHER)
481 error = g_access(cp, dr, dw, de);
492 sc->sc_opened += dr + dw + de;
493 if (sc->sc_stopping && sc->sc_opened == 0)
494 g_multipath_destroy(gp);
499 LIST_FOREACH(cp, &gp->consumer, consumer) {
502 if (cp->index & MP_WITHER)
505 (void) g_access(cp, -dr, -dw, -de);
510 static struct g_geom *
511 g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md)
513 struct g_multipath_softc *sc;
515 struct g_provider *pp;
519 LIST_FOREACH(gp, &mp->geom, geom) {
521 if (sc == NULL || sc->sc_stopping)
523 if (strcmp(gp->name, md->md_name) == 0) {
524 printf("GEOM_MULTIPATH: name %s already exists\n",
530 gp = g_new_geomf(mp, "%s", md->md_name);
531 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
532 mtx_init(&sc->sc_mtx, "multipath", NULL, MTX_DEF);
533 memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid));
534 memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name));
535 sc->sc_active_active = md->md_active_active;
536 sc->sc_size = md->md_size;
538 gp->start = g_multipath_start;
539 gp->orphan = g_multipath_orphan;
540 gp->resize = g_multipath_resize;
541 gp->access = g_multipath_access;
542 gp->dumpconf = g_multipath_dumpconf;
544 pp = g_new_providerf(gp, "multipath/%s", md->md_name);
545 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
546 if (md->md_size != 0) {
547 pp->mediasize = md->md_size -
548 ((md->md_uuid[0] != 0) ? md->md_sectorsize : 0);
549 pp->sectorsize = md->md_sectorsize;
552 g_error_provider(pp, 0);
553 printf("GEOM_MULTIPATH: %s created\n", gp->name);
558 g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp)
560 struct g_multipath_softc *sc;
561 struct g_consumer *cp, *nxtcp;
562 int error, acr, acw, ace;
567 KASSERT(sc, ("no softc"));
570 * Make sure that the passed provider isn't already attached
572 LIST_FOREACH(cp, &gp->consumer, consumer) {
573 if (cp->provider == pp)
577 printf("GEOM_MULTIPATH: provider %s already attached to %s\n",
581 nxtcp = LIST_FIRST(&gp->consumer);
582 cp = g_new_consumer(gp);
583 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
586 error = g_attach(cp, pp);
588 printf("GEOM_MULTIPATH: cannot attach %s to %s",
589 pp->name, sc->sc_name);
590 g_destroy_consumer(cp);
595 * Set access permissions on new consumer to match other consumers
598 acr = sc->sc_pp->acr;
599 acw = sc->sc_pp->acw;
600 ace = sc->sc_pp->ace;
603 if (g_multipath_exclusive) {
608 error = g_access(cp, acr, acw, ace);
610 printf("GEOM_MULTIPATH: cannot set access in "
611 "attaching %s to %s (%d)\n",
612 pp->name, sc->sc_name, error);
614 g_destroy_consumer(cp);
617 if (sc->sc_size == 0) {
618 sc->sc_size = pp->mediasize -
619 ((sc->sc_uuid[0] != 0) ? pp->sectorsize : 0);
620 sc->sc_pp->mediasize = sc->sc_size;
621 sc->sc_pp->sectorsize = pp->sectorsize;
623 if (sc->sc_pp->stripesize == 0 && sc->sc_pp->stripeoffset == 0) {
624 sc->sc_pp->stripesize = pp->stripesize;
625 sc->sc_pp->stripeoffset = pp->stripeoffset;
627 sc->sc_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
628 mtx_lock(&sc->sc_mtx);
631 mtx_unlock(&sc->sc_mtx);
632 printf("GEOM_MULTIPATH: %s added to %s\n",
633 pp->name, sc->sc_name);
634 if (sc->sc_active == NULL) {
636 if (sc->sc_active_active != 1)
637 printf("GEOM_MULTIPATH: %s is now active path in %s\n",
638 pp->name, sc->sc_name);
644 g_multipath_destroy(struct g_geom *gp)
646 struct g_multipath_softc *sc;
647 struct g_consumer *cp, *cp1;
650 if (gp->softc == NULL)
653 if (!sc->sc_stopping) {
654 printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
657 if (sc->sc_opened != 0) {
658 g_wither_provider(sc->sc_pp, ENXIO);
660 return (EINPROGRESS);
662 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
663 mtx_lock(&sc->sc_mtx);
664 if ((cp->index & MP_POSTED) == 0) {
665 cp->index |= MP_POSTED;
666 mtx_unlock(&sc->sc_mtx);
669 return(0); /* Recursion happened. */
671 mtx_unlock(&sc->sc_mtx);
673 if (!LIST_EMPTY(&gp->consumer))
674 return (EINPROGRESS);
675 mtx_destroy(&sc->sc_mtx);
678 printf("GEOM_MULTIPATH: %s destroyed\n", gp->name);
679 g_wither_geom(gp, ENXIO);
684 g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
688 return (g_multipath_destroy(gp));
692 g_multipath_rotate(struct g_geom *gp)
694 struct g_consumer *lcp, *first_good_cp = NULL;
695 struct g_multipath_softc *sc = gp->softc;
696 int active_cp_seen = 0;
701 LIST_FOREACH(lcp, &gp->consumer, consumer) {
702 if ((lcp->index & MP_BAD) == 0) {
703 if (first_good_cp == NULL)
708 if (sc->sc_active == lcp)
713 if (lcp && lcp != sc->sc_active) {
715 if (sc->sc_active_active != 1)
716 printf("GEOM_MULTIPATH: %s is now active path in %s\n",
717 lcp->provider->name, sc->sc_name);
723 g_multipath_init(struct g_class *mp)
726 mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
727 kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt");
731 g_multipath_fini(struct g_class *mp)
733 if (g_multipath_kt_state == GKT_RUN) {
734 mtx_lock(&gmtbq_mtx);
735 g_multipath_kt_state = GKT_DIE;
736 wakeup(&g_multipath_kt_state);
737 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
739 mtx_unlock(&gmtbq_mtx);
744 g_multipath_read_metadata(struct g_consumer *cp,
745 struct g_multipath_metadata *md)
747 struct g_provider *pp;
752 error = g_access(cp, 1, 0, 0);
757 buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
758 pp->sectorsize, &error);
760 g_access(cp, -1, 0, 0);
763 multipath_metadata_decode(buf, md);
769 g_multipath_write_metadata(struct g_consumer *cp,
770 struct g_multipath_metadata *md)
772 struct g_provider *pp;
777 error = g_access(cp, 1, 1, 1);
782 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
783 multipath_metadata_encode(md, buf);
784 error = g_write_data(cp, pp->mediasize - pp->sectorsize,
785 buf, pp->sectorsize);
787 g_access(cp, -1, -1, -1);
792 static struct g_geom *
793 g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
795 struct g_multipath_metadata md;
796 struct g_multipath_softc *sc;
797 struct g_consumer *cp;
798 struct g_geom *gp, *gp1;
803 gp = g_new_geomf(mp, "multipath:taste");
804 gp->start = g_multipath_start;
805 gp->access = g_multipath_access;
806 gp->orphan = g_multipath_orphan;
807 cp = g_new_consumer(gp);
809 error = g_multipath_read_metadata(cp, &md);
811 g_destroy_consumer(cp);
817 if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
818 if (g_multipath_debug)
819 printf("%s is not MULTIPATH\n", pp->name);
822 if (md.md_version != G_MULTIPATH_VERSION) {
823 printf("%s has version %d multipath id- this module is version "
824 " %d: rejecting\n", pp->name, md.md_version,
825 G_MULTIPATH_VERSION);
828 if (md.md_size != 0 && md.md_size != pp->mediasize)
830 if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize)
832 if (g_multipath_debug)
833 printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
836 * Let's check if such a device already is present. We check against
837 * uuid alone first because that's the true distinguishor. If that
838 * passes, then we check for name conflicts. If there are conflicts,
841 * The whole purpose of this is to solve the problem that people don't
842 * pick good unique names, but good unique names (like uuids) are a
843 * pain to use. So, we allow people to build GEOMs with friendly names
844 * and uuids, and modify the names in case there's a collision.
847 LIST_FOREACH(gp, &mp->geom, geom) {
849 if (sc == NULL || sc->sc_stopping)
851 if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
855 LIST_FOREACH(gp1, &mp->geom, geom) {
859 if (sc == NULL || sc->sc_stopping)
861 if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
866 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
868 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
869 * with the same name (but a different UUID).
871 * If gp is NULL, then modify the name with a random number and
872 * complain, but allow the creation of the geom to continue.
874 * If gp is *not* NULL, just use the geom's name as we're attaching
875 * this disk to the (previously generated) name.
882 u_long rand = random();
884 snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
885 printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
886 sc->sc_name, sc->sc_uuid);
887 printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
889 strlcpy(md.md_name, buf, sizeof(md.md_name));
891 strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
896 gp = g_multipath_create(mp, &md);
898 printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
899 md.md_name, md.md_uuid);
908 KASSERT(sc != NULL, ("sc is NULL"));
909 error = g_multipath_add_disk(gp, pp);
912 g_multipath_destroy(gp);
919 g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp,
922 struct g_multipath_softc *sc;
924 struct g_consumer *cp;
925 struct g_provider *pp;
927 static const char devpf[6] = "/dev/";
932 mpname = gctl_get_asciiparam(req, "arg0");
933 if (mpname == NULL) {
934 gctl_error(req, "No 'arg0' argument");
937 gp = g_multipath_find_geom(mp, mpname);
939 gctl_error(req, "Device %s is invalid", mpname);
944 if (strncmp(name, devpf, 5) == 0)
946 pp = g_provider_by_name(name);
948 gctl_error(req, "Provider %s is invalid", name);
953 * Check to make sure parameters match.
955 LIST_FOREACH(cp, &gp->consumer, consumer) {
956 if (cp->provider == pp) {
957 gctl_error(req, "provider %s is already there",
962 if (sc->sc_pp->mediasize != 0 &&
963 sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0)
965 gctl_error(req, "Providers size mismatch %jd != %jd",
966 (intmax_t) sc->sc_pp->mediasize +
967 (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0),
968 (intmax_t) pp->mediasize);
971 if (sc->sc_pp->sectorsize != 0 &&
972 sc->sc_pp->sectorsize != pp->sectorsize) {
973 gctl_error(req, "Providers sectorsize mismatch %u != %u",
974 sc->sc_pp->sectorsize, pp->sectorsize);
978 error = g_multipath_add_disk(gp, pp);
980 gctl_error(req, "Provider addition error: %d", error);
984 g_multipath_ctl_prefer(struct gctl_req *req, struct g_class *mp)
987 struct g_multipath_softc *sc;
988 struct g_consumer *cp;
989 const char *name, *mpname;
990 static const char devpf[6] = "/dev/";
995 mpname = gctl_get_asciiparam(req, "arg0");
996 if (mpname == NULL) {
997 gctl_error(req, "No 'arg0' argument");
1000 gp = g_multipath_find_geom(mp, mpname);
1002 gctl_error(req, "Device %s is invalid", mpname);
1007 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1008 if (nargs == NULL) {
1009 gctl_error(req, "No 'nargs' argument");
1013 gctl_error(req, "missing device");
1017 name = gctl_get_asciiparam(req, "arg1");
1019 gctl_error(req, "No 'arg1' argument");
1022 if (strncmp(name, devpf, 5) == 0) {
1026 LIST_FOREACH(cp, &gp->consumer, consumer) {
1027 if (cp->provider != NULL
1028 && strcmp(cp->provider->name, name) == 0)
1033 gctl_error(req, "Provider %s not found", name);
1037 mtx_lock(&sc->sc_mtx);
1039 if (cp->index & MP_BAD) {
1040 gctl_error(req, "Consumer %s is invalid", name);
1041 mtx_unlock(&sc->sc_mtx);
1045 /* Here when the consumer is present and in good shape */
1048 if (!sc->sc_active_active)
1049 printf("GEOM_MULTIPATH: %s now active path in %s\n",
1050 sc->sc_active->provider->name, sc->sc_name);
1052 mtx_unlock(&sc->sc_mtx);
1056 g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp)
1058 struct g_multipath_softc *sc;
1060 const char *mpname, *name;
1062 mpname = gctl_get_asciiparam(req, "arg0");
1063 if (mpname == NULL) {
1064 gctl_error(req, "No 'arg0' argument");
1067 gp = g_multipath_find_geom(mp, mpname);
1069 gctl_error(req, "Device %s not found", mpname);
1074 name = gctl_get_asciiparam(req, "arg1");
1076 gctl_error(req, "No 'arg1' argument");
1079 g_multipath_ctl_add_name(req, mp, name);
1083 g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp)
1085 struct g_multipath_metadata md;
1086 struct g_multipath_softc *sc;
1088 const char *mpname, *name;
1090 int *nargs, i, *val;
1092 g_topology_assert();
1094 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1096 gctl_error(req, "wrong number of arguments.");
1100 mpname = gctl_get_asciiparam(req, "arg0");
1101 if (mpname == NULL) {
1102 gctl_error(req, "No 'arg0' argument");
1105 gp = g_multipath_find_geom(mp, mpname);
1107 gctl_error(req, "Device %s already exist", mpname);
1111 memset(&md, 0, sizeof(md));
1112 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1113 md.md_version = G_MULTIPATH_VERSION;
1114 strlcpy(md.md_name, mpname, sizeof(md.md_name));
1116 md.md_sectorsize = 0;
1118 md.md_active_active = 0;
1119 val = gctl_get_paraml(req, "active_active", sizeof(*val));
1120 if (val != NULL && *val != 0)
1121 md.md_active_active = 1;
1122 val = gctl_get_paraml(req, "active_read", sizeof(*val));
1123 if (val != NULL && *val != 0)
1124 md.md_active_active = 2;
1125 gp = g_multipath_create(mp, &md);
1127 gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n",
1128 md.md_name, md.md_uuid);
1133 for (i = 1; i < *nargs; i++) {
1134 snprintf(param, sizeof(param), "arg%d", i);
1135 name = gctl_get_asciiparam(req, param);
1136 g_multipath_ctl_add_name(req, mp, name);
1139 if (sc->sc_ndisks != (*nargs - 1))
1140 g_multipath_destroy(gp);
1144 g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp)
1146 struct g_multipath_softc *sc;
1148 struct g_consumer *cp;
1149 struct g_provider *pp;
1150 struct g_multipath_metadata md;
1154 g_topology_assert();
1156 name = gctl_get_asciiparam(req, "arg0");
1158 gctl_error(req, "No 'arg0' argument");
1161 gp = g_multipath_find_geom(mp, name);
1163 gctl_error(req, "Device %s is invalid", name);
1167 val = gctl_get_paraml(req, "active_active", sizeof(*val));
1168 if (val != NULL && *val != 0)
1169 sc->sc_active_active = 1;
1170 val = gctl_get_paraml(req, "active_read", sizeof(*val));
1171 if (val != NULL && *val != 0)
1172 sc->sc_active_active = 2;
1173 val = gctl_get_paraml(req, "active_passive", sizeof(*val));
1174 if (val != NULL && *val != 0)
1175 sc->sc_active_active = 0;
1176 if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
1179 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1180 memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
1181 strlcpy(md.md_name, name, sizeof(md.md_name));
1182 md.md_version = G_MULTIPATH_VERSION;
1183 md.md_size = pp->mediasize;
1184 md.md_sectorsize = pp->sectorsize;
1185 md.md_active_active = sc->sc_active_active;
1186 error = g_multipath_write_metadata(cp, &md);
1188 gctl_error(req, "Can't update metadata on %s (%d)",
1194 g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail)
1196 struct g_multipath_softc *sc;
1198 struct g_consumer *cp;
1199 const char *mpname, *name;
1202 mpname = gctl_get_asciiparam(req, "arg0");
1203 if (mpname == NULL) {
1204 gctl_error(req, "No 'arg0' argument");
1207 gp = g_multipath_find_geom(mp, mpname);
1209 gctl_error(req, "Device %s not found", mpname);
1214 name = gctl_get_asciiparam(req, "arg1");
1216 gctl_error(req, "No 'arg1' argument");
1221 mtx_lock(&sc->sc_mtx);
1222 LIST_FOREACH(cp, &gp->consumer, consumer) {
1223 if (cp->provider != NULL &&
1224 strcmp(cp->provider->name, name) == 0 &&
1225 (cp->index & MP_LOST) == 0) {
1227 if (!fail == !(cp->index & MP_FAIL))
1229 printf("GEOM_MULTIPATH: %s in %s is marked %s.\n",
1230 name, sc->sc_name, fail ? "FAIL" : "OK");
1232 g_multipath_fault(cp, MP_FAIL);
1234 cp->index &= ~MP_FAIL;
1238 mtx_unlock(&sc->sc_mtx);
1240 gctl_error(req, "Provider %s not found", name);
1244 g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp)
1246 struct g_multipath_softc *sc;
1248 struct g_consumer *cp, *cp1;
1249 const char *mpname, *name;
1253 mpname = gctl_get_asciiparam(req, "arg0");
1254 if (mpname == NULL) {
1255 gctl_error(req, "No 'arg0' argument");
1258 gp = g_multipath_find_geom(mp, mpname);
1260 gctl_error(req, "Device %s not found", mpname);
1265 name = gctl_get_asciiparam(req, "arg1");
1267 gctl_error(req, "No 'arg1' argument");
1272 mtx_lock(&sc->sc_mtx);
1273 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
1274 if (cp->provider != NULL &&
1275 strcmp(cp->provider->name, name) == 0 &&
1276 (cp->index & MP_LOST) == 0) {
1278 printf("GEOM_MULTIPATH: removing %s from %s\n",
1279 cp->provider->name, cp->geom->name);
1281 g_multipath_fault(cp, MP_LOST);
1282 cnt = (uintptr_t *)&cp->private;
1283 if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
1284 cp->index |= MP_POSTED;
1285 mtx_unlock(&sc->sc_mtx);
1288 return; /* Recursion happened. */
1289 mtx_lock(&sc->sc_mtx);
1293 mtx_unlock(&sc->sc_mtx);
1295 gctl_error(req, "Provider %s not found", name);
1298 static struct g_geom *
1299 g_multipath_find_geom(struct g_class *mp, const char *name)
1302 struct g_multipath_softc *sc;
1304 LIST_FOREACH(gp, &mp->geom, geom) {
1306 if (sc == NULL || sc->sc_stopping)
1308 if (strcmp(gp->name, name) == 0)
1315 g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp)
1321 g_topology_assert();
1323 name = gctl_get_asciiparam(req, "arg0");
1325 gctl_error(req, "No 'arg0' argument");
1328 gp = g_multipath_find_geom(mp, name);
1330 gctl_error(req, "Device %s is invalid", name);
1333 error = g_multipath_destroy(gp);
1334 if (error != 0 && error != EINPROGRESS)
1335 gctl_error(req, "failed to stop %s (err=%d)", name, error);
1339 g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1342 struct g_multipath_softc *sc;
1343 struct g_consumer *cp;
1344 struct g_provider *pp;
1349 g_topology_assert();
1351 name = gctl_get_asciiparam(req, "arg0");
1353 gctl_error(req, "No 'arg0' argument");
1356 gp = g_multipath_find_geom(mp, name);
1358 gctl_error(req, "Device %s is invalid", name);
1363 if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
1366 error = g_access(cp, 1, 1, 1);
1368 gctl_error(req, "Can't open %s (%d)", pp->name, error);
1371 g_topology_unlock();
1372 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1373 error = g_write_data(cp, pp->mediasize - pp->sectorsize,
1374 buf, pp->sectorsize);
1376 g_access(cp, -1, -1, -1);
1378 gctl_error(req, "Can't erase metadata on %s (%d)",
1383 error = g_multipath_destroy(gp);
1384 if (error != 0 && error != EINPROGRESS)
1385 gctl_error(req, "failed to destroy %s (err=%d)", name, error);
1389 g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp)
1395 g_topology_assert();
1397 name = gctl_get_asciiparam(req, "arg0");
1399 gctl_error(req, "No 'arg0' argument");
1402 gp = g_multipath_find_geom(mp, name);
1404 gctl_error(req, "Device %s is invalid", name);
1407 error = g_multipath_rotate(gp);
1409 gctl_error(req, "failed to rotate %s (err=%d)", name, error);
1414 g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp)
1418 struct g_multipath_softc *sc;
1419 struct g_consumer *cp;
1423 sb = sbuf_new_auto();
1425 g_topology_assert();
1426 name = gctl_get_asciiparam(req, "arg0");
1428 gctl_error(req, "No 'arg0' argument");
1431 gp = g_multipath_find_geom(mp, name);
1433 gctl_error(req, "Device %s is invalid", name);
1437 if (sc->sc_active_active == 1) {
1439 LIST_FOREACH(cp, &gp->consumer, consumer) {
1440 if (cp->index & MP_BAD)
1444 sbuf_cat(sb, cp->provider->name);
1448 sbuf_cat(sb, "none");
1450 } else if (sc->sc_active && sc->sc_active->provider) {
1451 sbuf_printf(sb, "%s\n", sc->sc_active->provider->name);
1453 sbuf_printf(sb, "none\n");
1456 gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1461 g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1464 g_topology_assert();
1465 version = gctl_get_paraml(req, "version", sizeof(*version));
1466 if (version == NULL) {
1467 gctl_error(req, "No 'version' argument");
1468 } else if (*version != G_MULTIPATH_VERSION) {
1469 gctl_error(req, "Userland and kernel parts are out of sync");
1470 } else if (strcmp(verb, "add") == 0) {
1471 g_multipath_ctl_add(req, mp);
1472 } else if (strcmp(verb, "prefer") == 0) {
1473 g_multipath_ctl_prefer(req, mp);
1474 } else if (strcmp(verb, "create") == 0) {
1475 g_multipath_ctl_create(req, mp);
1476 } else if (strcmp(verb, "configure") == 0) {
1477 g_multipath_ctl_configure(req, mp);
1478 } else if (strcmp(verb, "stop") == 0) {
1479 g_multipath_ctl_stop(req, mp);
1480 } else if (strcmp(verb, "destroy") == 0) {
1481 g_multipath_ctl_destroy(req, mp);
1482 } else if (strcmp(verb, "fail") == 0) {
1483 g_multipath_ctl_fail(req, mp, 1);
1484 } else if (strcmp(verb, "restore") == 0) {
1485 g_multipath_ctl_fail(req, mp, 0);
1486 } else if (strcmp(verb, "remove") == 0) {
1487 g_multipath_ctl_remove(req, mp);
1488 } else if (strcmp(verb, "rotate") == 0) {
1489 g_multipath_ctl_rotate(req, mp);
1490 } else if (strcmp(verb, "getactive") == 0) {
1491 g_multipath_ctl_getactive(req, mp);
1493 gctl_error(req, "Unknown verb %s", verb);
1498 g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1499 struct g_consumer *cp, struct g_provider *pp)
1501 struct g_multipath_softc *sc;
1504 g_topology_assert();
1510 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1511 (cp->index & MP_NEW) ? "NEW" :
1512 (cp->index & MP_LOST) ? "LOST" :
1513 (cp->index & MP_FAIL) ? "FAIL" :
1514 (sc->sc_active_active == 1 || sc->sc_active == cp) ?
1516 sc->sc_active_active == 2 ? "READ" : "PASSIVE");
1518 good = g_multipath_good(gp);
1519 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1520 good == 0 ? "BROKEN" :
1521 (good != sc->sc_ndisks || sc->sc_ndisks == 1) ?
1522 "DEGRADED" : "OPTIMAL");
1524 if (cp == NULL && pp == NULL) {
1525 sbuf_printf(sb, "%s<UUID>%s</UUID>\n", indent, sc->sc_uuid);
1526 sbuf_printf(sb, "%s<Mode>Active/%s</Mode>\n", indent,
1527 sc->sc_active_active == 2 ? "Read" :
1528 sc->sc_active_active == 1 ? "Active" : "Passive");
1529 sbuf_printf(sb, "%s<Type>%s</Type>\n", indent,
1530 sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC");
1534 DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);
1535 MODULE_VERSION(geom_multipath, 0);