2 * Copyright (c) 2011 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>
40 #include <sys/mutex.h>
43 #include <sys/sysctl.h>
44 #include <sys/kthread.h>
45 #include <sys/malloc.h>
46 #include <geom/geom.h>
47 #include <geom/multipath/g_multipath.h>
49 FEATURE(geom_multipath, "GEOM multipath support");
51 SYSCTL_DECL(_kern_geom);
52 static SYSCTL_NODE(_kern_geom, OID_AUTO, multipath, CTLFLAG_RW, 0,
53 "GEOM_MULTIPATH tunables");
54 static u_int g_multipath_debug = 0;
55 SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, debug, CTLFLAG_RW,
56 &g_multipath_debug, 0, "Debug level");
57 static u_int g_multipath_exclusive = 1;
58 SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, exclusive, CTLFLAG_RW,
59 &g_multipath_exclusive, 0, "Exclusively open providers");
65 } g_multipath_kt_state;
66 static struct bio_queue_head gmtbq;
67 static struct mtx gmtbq_mtx;
69 static void g_multipath_orphan(struct g_consumer *);
70 static void g_multipath_start(struct bio *);
71 static void g_multipath_done(struct bio *);
72 static void g_multipath_done_error(struct bio *);
73 static void g_multipath_kt(void *);
75 static int g_multipath_destroy(struct g_geom *);
77 g_multipath_destroy_geom(struct gctl_req *, struct g_class *, struct g_geom *);
79 static struct g_geom *g_multipath_find_geom(struct g_class *, const char *);
80 static int g_multipath_rotate(struct g_geom *);
82 static g_taste_t g_multipath_taste;
83 static g_ctl_req_t g_multipath_config;
84 static g_init_t g_multipath_init;
85 static g_fini_t g_multipath_fini;
86 static g_dumpconf_t g_multipath_dumpconf;
88 struct g_class g_multipath_class = {
89 .name = G_MULTIPATH_CLASS_NAME,
91 .ctlreq = g_multipath_config,
92 .taste = g_multipath_taste,
93 .destroy_geom = g_multipath_destroy_geom,
94 .init = g_multipath_init,
95 .fini = g_multipath_fini
98 #define MP_FAIL 0x00000001
99 #define MP_LOST 0x00000002
100 #define MP_NEW 0x00000004
101 #define MP_POSTED 0x00000008
102 #define MP_BAD (MP_FAIL | MP_LOST | MP_NEW)
103 #define MP_IDLE 0x00000010
104 #define MP_IDLE_MASK 0xfffffff0
107 g_multipath_good(struct g_geom *gp)
109 struct g_consumer *cp;
112 LIST_FOREACH(cp, &gp->consumer, consumer) {
113 if ((cp->index & MP_BAD) == 0)
120 g_multipath_fault(struct g_consumer *cp, int cause)
122 struct g_multipath_softc *sc;
123 struct g_consumer *lcp;
129 if (g_multipath_good(gp) == 0 && sc->sc_ndisks > 0) {
130 LIST_FOREACH(lcp, &gp->consumer, consumer) {
131 if (lcp->provider == NULL ||
132 (lcp->index & (MP_LOST | MP_NEW)))
134 if (sc->sc_ndisks > 1 && lcp == cp)
136 printf("GEOM_MULTIPATH: "
137 "all paths in %s were marked FAIL, restore %s\n",
138 sc->sc_name, lcp->provider->name);
139 lcp->index &= ~MP_FAIL;
142 if (cp != sc->sc_active)
144 sc->sc_active = NULL;
145 LIST_FOREACH(lcp, &gp->consumer, consumer) {
146 if ((lcp->index & MP_BAD) == 0) {
151 if (sc->sc_active == NULL) {
152 printf("GEOM_MULTIPATH: out of providers for %s\n",
154 } else if (sc->sc_active_active != 1) {
155 printf("GEOM_MULTIPATH: %s is now active path in %s\n",
156 sc->sc_active->provider->name, sc->sc_name);
160 static struct g_consumer *
161 g_multipath_choose(struct g_geom *gp, struct bio *bp)
163 struct g_multipath_softc *sc;
164 struct g_consumer *best, *cp;
167 if (sc->sc_active_active == 0 ||
168 (sc->sc_active_active == 2 && bp->bio_cmd != BIO_READ))
169 return (sc->sc_active);
171 LIST_FOREACH(cp, &gp->consumer, consumer) {
172 if (cp->index & MP_BAD)
174 cp->index += MP_IDLE;
175 if (best == NULL || cp->private < best->private ||
176 (cp->private == best->private && cp->index > best->index))
180 best->index &= ~MP_IDLE_MASK;
185 g_mpd(void *arg, int flags __unused)
188 struct g_multipath_softc *sc;
189 struct g_consumer *cp;
195 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) {
197 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
198 if (w > 0 && cp->provider != NULL &&
199 (cp->provider->geom->flags & G_GEOM_WITHER) == 0) {
200 g_post_event(g_mpd, cp, M_WAITOK, NULL);
205 mtx_lock(&sc->sc_mtx);
207 printf("GEOM_MULTIPATH: %s removed from %s\n",
208 cp->provider->name, gp->name);
211 g_destroy_consumer(cp);
212 mtx_unlock(&sc->sc_mtx);
213 if (LIST_EMPTY(&gp->consumer))
214 g_multipath_destroy(gp);
218 g_multipath_orphan(struct g_consumer *cp)
220 struct g_multipath_softc *sc;
224 printf("GEOM_MULTIPATH: %s in %s was disconnected\n",
225 cp->provider->name, cp->geom->name);
226 sc = cp->geom->softc;
227 cnt = (uintptr_t *)&cp->private;
228 mtx_lock(&sc->sc_mtx);
230 g_multipath_fault(cp, MP_LOST);
231 if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
232 cp->index |= MP_POSTED;
233 mtx_unlock(&sc->sc_mtx);
236 mtx_unlock(&sc->sc_mtx);
240 g_multipath_start(struct bio *bp)
242 struct g_multipath_softc *sc;
244 struct g_consumer *cp;
248 gp = bp->bio_to->geom;
250 KASSERT(sc != NULL, ("NULL sc"));
251 cbp = g_clone_bio(bp);
253 g_io_deliver(bp, ENOMEM);
256 mtx_lock(&sc->sc_mtx);
257 cp = g_multipath_choose(gp, bp);
259 mtx_unlock(&sc->sc_mtx);
261 g_io_deliver(bp, ENXIO);
264 if ((uintptr_t)bp->bio_driver1 < sc->sc_ndisks)
265 bp->bio_driver1 = (void *)(uintptr_t)sc->sc_ndisks;
266 cnt = (uintptr_t *)&cp->private;
268 mtx_unlock(&sc->sc_mtx);
269 cbp->bio_done = g_multipath_done;
270 g_io_request(cbp, cp);
274 g_multipath_done(struct bio *bp)
276 struct g_multipath_softc *sc;
277 struct g_consumer *cp;
280 if (bp->bio_error == ENXIO || bp->bio_error == EIO) {
281 mtx_lock(&gmtbq_mtx);
282 bioq_insert_tail(&gmtbq, bp);
283 mtx_unlock(&gmtbq_mtx);
284 wakeup(&g_multipath_kt_state);
287 sc = cp->geom->softc;
288 cnt = (uintptr_t *)&cp->private;
289 mtx_lock(&sc->sc_mtx);
291 if (*cnt == 0 && (cp->index & MP_LOST)) {
292 cp->index |= MP_POSTED;
293 mtx_unlock(&sc->sc_mtx);
294 g_post_event(g_mpd, cp, M_WAITOK, NULL);
296 mtx_unlock(&sc->sc_mtx);
302 g_multipath_done_error(struct bio *bp)
306 struct g_multipath_softc *sc;
307 struct g_consumer *cp;
308 struct g_provider *pp;
312 * If we had a failure, we have to check first to see
313 * whether the consumer it failed on was the currently
314 * active consumer (i.e., this is the first in perhaps
315 * a number of failures). If so, we then switch consumers
316 * to the next available consumer.
319 pbp = bp->bio_parent;
320 gp = pbp->bio_to->geom;
324 cnt = (uintptr_t *)&cp->private;
326 mtx_lock(&sc->sc_mtx);
327 if ((cp->index & MP_FAIL) == 0) {
328 printf("GEOM_MULTIPATH: Error %d, %s in %s marked FAIL\n",
329 bp->bio_error, pp->name, sc->sc_name);
330 g_multipath_fault(cp, MP_FAIL);
333 if (*cnt == 0 && (cp->index & (MP_LOST | MP_POSTED)) == MP_LOST) {
334 cp->index |= MP_POSTED;
335 mtx_unlock(&sc->sc_mtx);
336 g_post_event(g_mpd, cp, M_WAITOK, NULL);
338 mtx_unlock(&sc->sc_mtx);
341 * If we can fruitfully restart the I/O, do so.
343 if (pbp->bio_children < (uintptr_t)pbp->bio_driver1) {
346 g_multipath_start(pbp);
353 g_multipath_kt(void *arg)
356 g_multipath_kt_state = GKT_RUN;
357 mtx_lock(&gmtbq_mtx);
358 while (g_multipath_kt_state == GKT_RUN) {
362 bp = bioq_takefirst(&gmtbq);
365 mtx_unlock(&gmtbq_mtx);
366 g_multipath_done_error(bp);
367 mtx_lock(&gmtbq_mtx);
369 if (g_multipath_kt_state != GKT_RUN)
371 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
374 mtx_unlock(&gmtbq_mtx);
375 wakeup(&g_multipath_kt_state);
381 g_multipath_access(struct g_provider *pp, int dr, int dw, int de)
384 struct g_consumer *cp, *badcp = NULL;
385 struct g_multipath_softc *sc;
390 LIST_FOREACH(cp, &gp->consumer, consumer) {
391 error = g_access(cp, dr, dw, de);
398 sc->sc_opened += dr + dw + de;
399 if (sc->sc_stopping && sc->sc_opened == 0)
400 g_multipath_destroy(gp);
404 LIST_FOREACH(cp, &gp->consumer, consumer) {
407 (void) g_access(cp, -dr, -dw, -de);
412 static struct g_geom *
413 g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md)
415 struct g_multipath_softc *sc;
417 struct g_provider *pp;
421 LIST_FOREACH(gp, &mp->geom, geom) {
423 if (sc == NULL || sc->sc_stopping)
425 if (strcmp(gp->name, md->md_name) == 0) {
426 printf("GEOM_MULTIPATH: name %s already exists\n",
432 gp = g_new_geomf(mp, "%s", md->md_name);
433 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
434 mtx_init(&sc->sc_mtx, "multipath", NULL, MTX_DEF);
435 memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid));
436 memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name));
437 sc->sc_active_active = md->md_active_active;
439 gp->start = g_multipath_start;
440 gp->orphan = g_multipath_orphan;
441 gp->access = g_multipath_access;
442 gp->dumpconf = g_multipath_dumpconf;
444 pp = g_new_providerf(gp, "multipath/%s", md->md_name);
445 if (md->md_size != 0) {
446 pp->mediasize = md->md_size -
447 ((md->md_uuid[0] != 0) ? md->md_sectorsize : 0);
448 pp->sectorsize = md->md_sectorsize;
451 g_error_provider(pp, 0);
452 printf("GEOM_MULTIPATH: %s created\n", gp->name);
457 g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp)
459 struct g_multipath_softc *sc;
460 struct g_consumer *cp, *nxtcp;
461 int error, acr, acw, ace;
466 KASSERT(sc, ("no softc"));
469 * Make sure that the passed provider isn't already attached
471 LIST_FOREACH(cp, &gp->consumer, consumer) {
472 if (cp->provider == pp)
476 printf("GEOM_MULTIPATH: provider %s already attached to %s\n",
480 nxtcp = LIST_FIRST(&gp->consumer);
481 cp = g_new_consumer(gp);
484 error = g_attach(cp, pp);
486 printf("GEOM_MULTIPATH: cannot attach %s to %s",
487 pp->name, sc->sc_name);
488 g_destroy_consumer(cp);
493 * Set access permissions on new consumer to match other consumers
496 acr = sc->sc_pp->acr;
497 acw = sc->sc_pp->acw;
498 ace = sc->sc_pp->ace;
501 if (g_multipath_exclusive) {
506 error = g_access(cp, acr, acw, ace);
508 printf("GEOM_MULTIPATH: cannot set access in "
509 "attaching %s to %s (%d)\n",
510 pp->name, sc->sc_name, error);
512 g_destroy_consumer(cp);
515 if (sc->sc_pp != NULL && sc->sc_pp->mediasize == 0) {
516 sc->sc_pp->mediasize = pp->mediasize -
517 ((sc->sc_uuid[0] != 0) ? pp->sectorsize : 0);
518 sc->sc_pp->sectorsize = pp->sectorsize;
520 if (sc->sc_pp != NULL &&
521 sc->sc_pp->stripesize == 0 && sc->sc_pp->stripeoffset == 0) {
522 sc->sc_pp->stripesize = pp->stripesize;
523 sc->sc_pp->stripeoffset = pp->stripeoffset;
525 mtx_lock(&sc->sc_mtx);
528 mtx_unlock(&sc->sc_mtx);
529 printf("GEOM_MULTIPATH: %s added to %s\n",
530 pp->name, sc->sc_name);
531 if (sc->sc_active == NULL) {
533 if (sc->sc_active_active != 1)
534 printf("GEOM_MULTIPATH: %s is now active path in %s\n",
535 pp->name, sc->sc_name);
541 g_multipath_destroy(struct g_geom *gp)
543 struct g_multipath_softc *sc;
544 struct g_consumer *cp, *cp1;
547 if (gp->softc == NULL)
550 if (!sc->sc_stopping) {
551 printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
554 if (sc->sc_opened != 0) {
555 if (sc->sc_pp != NULL) {
556 g_wither_provider(sc->sc_pp, ENXIO);
559 return (EINPROGRESS);
561 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
562 mtx_lock(&sc->sc_mtx);
563 if ((cp->index & MP_POSTED) == 0) {
564 cp->index |= MP_POSTED;
565 mtx_unlock(&sc->sc_mtx);
568 return(0); /* Recursion happened. */
570 mtx_unlock(&sc->sc_mtx);
572 if (!LIST_EMPTY(&gp->consumer))
573 return (EINPROGRESS);
574 mtx_destroy(&sc->sc_mtx);
577 printf("GEOM_MULTIPATH: %s destroyed\n", gp->name);
578 g_wither_geom(gp, ENXIO);
583 g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
587 return (g_multipath_destroy(gp));
591 g_multipath_rotate(struct g_geom *gp)
593 struct g_consumer *lcp, *first_good_cp = NULL;
594 struct g_multipath_softc *sc = gp->softc;
595 int active_cp_seen = 0;
600 LIST_FOREACH(lcp, &gp->consumer, consumer) {
601 if ((lcp->index & MP_BAD) == 0) {
602 if (first_good_cp == NULL)
607 if (sc->sc_active == lcp)
612 if (lcp && lcp != sc->sc_active) {
614 if (sc->sc_active_active != 1)
615 printf("GEOM_MULTIPATH: %s is now active path in %s\n",
616 lcp->provider->name, sc->sc_name);
622 g_multipath_init(struct g_class *mp)
625 mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
626 kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt");
630 g_multipath_fini(struct g_class *mp)
632 if (g_multipath_kt_state == GKT_RUN) {
633 mtx_lock(&gmtbq_mtx);
634 g_multipath_kt_state = GKT_DIE;
635 wakeup(&g_multipath_kt_state);
636 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
638 mtx_unlock(&gmtbq_mtx);
643 g_multipath_read_metadata(struct g_consumer *cp,
644 struct g_multipath_metadata *md)
646 struct g_provider *pp;
651 error = g_access(cp, 1, 0, 0);
656 buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
657 pp->sectorsize, &error);
659 g_access(cp, -1, 0, 0);
662 multipath_metadata_decode(buf, md);
667 static struct g_geom *
668 g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
670 struct g_multipath_metadata md;
671 struct g_multipath_softc *sc;
672 struct g_consumer *cp;
673 struct g_geom *gp, *gp1;
678 gp = g_new_geomf(mp, "multipath:taste");
679 gp->start = g_multipath_start;
680 gp->access = g_multipath_access;
681 gp->orphan = g_multipath_orphan;
682 cp = g_new_consumer(gp);
684 error = g_multipath_read_metadata(cp, &md);
686 g_destroy_consumer(cp);
692 if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
693 if (g_multipath_debug)
694 printf("%s is not MULTIPATH\n", pp->name);
697 if (md.md_version != G_MULTIPATH_VERSION) {
698 printf("%s has version %d multipath id- this module is version "
699 " %d: rejecting\n", pp->name, md.md_version,
700 G_MULTIPATH_VERSION);
703 if (md.md_size != 0 && md.md_size != pp->mediasize)
705 if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize)
707 if (g_multipath_debug)
708 printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
711 * Let's check if such a device already is present. We check against
712 * uuid alone first because that's the true distinguishor. If that
713 * passes, then we check for name conflicts. If there are conflicts,
716 * The whole purpose of this is to solve the problem that people don't
717 * pick good unique names, but good unique names (like uuids) are a
718 * pain to use. So, we allow people to build GEOMs with friendly names
719 * and uuids, and modify the names in case there's a collision.
722 LIST_FOREACH(gp, &mp->geom, geom) {
724 if (sc == NULL || sc->sc_stopping)
726 if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
730 LIST_FOREACH(gp1, &mp->geom, geom) {
734 if (sc == NULL || sc->sc_stopping)
736 if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
741 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
743 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
744 * with the same name (but a different UUID).
746 * If gp is NULL, then modify the name with a random number and
747 * complain, but allow the creation of the geom to continue.
749 * If gp is *not* NULL, just use the geom's name as we're attaching
750 * this disk to the (previously generated) name.
757 u_long rand = random();
759 snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
760 printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
761 sc->sc_name, sc->sc_uuid);
762 printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
764 strlcpy(md.md_name, buf, sizeof(md.md_name));
766 strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
771 gp = g_multipath_create(mp, &md);
773 printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
774 md.md_name, md.md_uuid);
783 KASSERT(sc != NULL, ("sc is NULL"));
784 error = g_multipath_add_disk(gp, pp);
787 g_multipath_destroy(gp);
794 g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp,
797 struct g_multipath_softc *sc;
799 struct g_consumer *cp;
800 struct g_provider *pp;
802 static const char devpf[6] = "/dev/";
806 mpname = gctl_get_asciiparam(req, "arg0");
807 if (mpname == NULL) {
808 gctl_error(req, "No 'arg0' argument");
811 gp = g_multipath_find_geom(mp, mpname);
813 gctl_error(req, "Device %s is invalid", mpname);
818 if (strncmp(name, devpf, 5) == 0)
820 pp = g_provider_by_name(name);
822 gctl_error(req, "Provider %s is invalid", name);
827 * Check to make sure parameters match.
829 LIST_FOREACH(cp, &gp->consumer, consumer) {
830 if (cp->provider == pp) {
831 gctl_error(req, "provider %s is already there",
836 if (sc->sc_pp != NULL && sc->sc_pp->mediasize != 0 &&
837 sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0)
839 gctl_error(req, "Providers size mismatch %jd != %jd",
840 (intmax_t) sc->sc_pp->mediasize +
841 (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0),
842 (intmax_t) pp->mediasize);
845 if (sc->sc_pp != NULL && sc->sc_pp->sectorsize != 0 &&
846 sc->sc_pp->sectorsize != pp->sectorsize) {
847 gctl_error(req, "Providers sectorsize mismatch %u != %u",
848 sc->sc_pp->sectorsize, pp->sectorsize);
855 (void) g_multipath_add_disk(gp, pp);
859 g_multipath_ctl_prefer(struct gctl_req *req, struct g_class *mp)
862 struct g_multipath_softc *sc;
863 struct g_consumer *cp;
864 const char *name, *mpname;
865 static const char devpf[6] = "/dev/";
870 mpname = gctl_get_asciiparam(req, "arg0");
871 if (mpname == NULL) {
872 gctl_error(req, "No 'arg0' argument");
875 gp = g_multipath_find_geom(mp, mpname);
877 gctl_error(req, "Device %s is invalid", mpname);
882 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
884 gctl_error(req, "No 'nargs' argument");
888 gctl_error(req, "missing device");
892 name = gctl_get_asciiparam(req, "arg1");
894 gctl_error(req, "No 'arg1' argument");
897 if (strncmp(name, devpf, 5) == 0) {
901 LIST_FOREACH(cp, &gp->consumer, consumer) {
902 if (cp->provider != NULL
903 && strcmp(cp->provider->name, name) == 0)
908 gctl_error(req, "Provider %s not found", name);
912 mtx_lock(&sc->sc_mtx);
914 if (cp->index & MP_BAD) {
915 gctl_error(req, "Consumer %s is invalid", name);
916 mtx_unlock(&sc->sc_mtx);
920 /* Here when the consumer is present and in good shape */
923 if (!sc->sc_active_active)
924 printf("GEOM_MULTIPATH: %s now active path in %s\n",
925 sc->sc_active->provider->name, sc->sc_name);
927 mtx_unlock(&sc->sc_mtx);
931 g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp)
933 struct g_multipath_softc *sc;
935 const char *mpname, *name;
937 mpname = gctl_get_asciiparam(req, "arg0");
938 if (mpname == NULL) {
939 gctl_error(req, "No 'arg0' argument");
942 gp = g_multipath_find_geom(mp, mpname);
944 gctl_error(req, "Device %s not found", mpname);
949 name = gctl_get_asciiparam(req, "arg1");
951 gctl_error(req, "No 'arg1' argument");
954 g_multipath_ctl_add_name(req, mp, name);
958 g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp)
960 struct g_multipath_metadata md;
961 struct g_multipath_softc *sc;
963 const char *mpname, *name;
969 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
971 gctl_error(req, "wrong number of arguments.");
975 mpname = gctl_get_asciiparam(req, "arg0");
976 if (mpname == NULL) {
977 gctl_error(req, "No 'arg0' argument");
980 gp = g_multipath_find_geom(mp, mpname);
982 gctl_error(req, "Device %s already exist", mpname);
987 memset(&md, 0, sizeof(md));
988 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
989 md.md_version = G_MULTIPATH_VERSION;
990 strlcpy(md.md_name, mpname, sizeof(md.md_name));
992 md.md_sectorsize = 0;
994 md.md_active_active = 0;
995 val = gctl_get_paraml(req, "active_active", sizeof(*val));
996 if (val != NULL && *val != 0)
997 md.md_active_active = 1;
998 val = gctl_get_paraml(req, "active_read", sizeof(*val));
999 if (val != NULL && *val != 0)
1000 md.md_active_active = 2;
1001 gp = g_multipath_create(mp, &md);
1003 gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n",
1004 md.md_name, md.md_uuid);
1009 for (i = 1; i < *nargs; i++) {
1010 snprintf(param, sizeof(param), "arg%d", i);
1011 name = gctl_get_asciiparam(req, param);
1012 g_multipath_ctl_add_name(req, mp, name);
1015 if (sc->sc_ndisks != (*nargs - 1))
1016 g_multipath_destroy(gp);
1020 g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp)
1022 struct g_multipath_softc *sc;
1024 struct g_consumer *cp;
1025 struct g_provider *pp;
1026 struct g_multipath_metadata md;
1031 g_topology_assert();
1033 name = gctl_get_asciiparam(req, "arg0");
1035 gctl_error(req, "No 'arg0' argument");
1038 gp = g_multipath_find_geom(mp, name);
1040 gctl_error(req, "Device %s is invalid", name);
1044 val = gctl_get_paraml(req, "active_active", sizeof(*val));
1045 if (val != NULL && *val != 0)
1046 sc->sc_active_active = 1;
1047 val = gctl_get_paraml(req, "active_read", sizeof(*val));
1048 if (val != NULL && *val != 0)
1049 sc->sc_active_active = 2;
1050 val = gctl_get_paraml(req, "active_passive", sizeof(*val));
1051 if (val != NULL && *val != 0)
1052 sc->sc_active_active = 0;
1053 if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
1056 error = g_access(cp, 1, 1, 1);
1058 gctl_error(req, "Can't open %s (%d)", pp->name, error);
1061 g_topology_unlock();
1062 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1063 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1064 memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
1065 strlcpy(md.md_name, name, sizeof(md.md_name));
1066 md.md_version = G_MULTIPATH_VERSION;
1067 md.md_size = pp->mediasize;
1068 md.md_sectorsize = pp->sectorsize;
1069 md.md_active_active = sc->sc_active_active;
1070 multipath_metadata_encode(&md, buf);
1071 error = g_write_data(cp, pp->mediasize - pp->sectorsize,
1072 buf, pp->sectorsize);
1074 g_access(cp, -1, -1, -1);
1076 gctl_error(req, "Can't update metadata on %s (%d)",
1082 g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail)
1084 struct g_multipath_softc *sc;
1086 struct g_consumer *cp;
1087 const char *mpname, *name;
1090 mpname = gctl_get_asciiparam(req, "arg0");
1091 if (mpname == NULL) {
1092 gctl_error(req, "No 'arg0' argument");
1095 gp = g_multipath_find_geom(mp, mpname);
1097 gctl_error(req, "Device %s not found", mpname);
1102 name = gctl_get_asciiparam(req, "arg1");
1104 gctl_error(req, "No 'arg1' argument");
1109 mtx_lock(&sc->sc_mtx);
1110 LIST_FOREACH(cp, &gp->consumer, consumer) {
1111 if (cp->provider != NULL &&
1112 strcmp(cp->provider->name, name) == 0 &&
1113 (cp->index & MP_LOST) == 0) {
1115 if (!fail == !(cp->index & MP_FAIL))
1117 printf("GEOM_MULTIPATH: %s in %s is marked %s.\n",
1118 name, sc->sc_name, fail ? "FAIL" : "OK");
1120 g_multipath_fault(cp, MP_FAIL);
1122 cp->index &= ~MP_FAIL;
1126 mtx_unlock(&sc->sc_mtx);
1128 gctl_error(req, "Provider %s not found", name);
1132 g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp)
1134 struct g_multipath_softc *sc;
1136 struct g_consumer *cp, *cp1;
1137 const char *mpname, *name;
1141 mpname = gctl_get_asciiparam(req, "arg0");
1142 if (mpname == NULL) {
1143 gctl_error(req, "No 'arg0' argument");
1146 gp = g_multipath_find_geom(mp, mpname);
1148 gctl_error(req, "Device %s not found", mpname);
1153 name = gctl_get_asciiparam(req, "arg1");
1155 gctl_error(req, "No 'arg1' argument");
1160 mtx_lock(&sc->sc_mtx);
1161 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
1162 if (cp->provider != NULL &&
1163 strcmp(cp->provider->name, name) == 0 &&
1164 (cp->index & MP_LOST) == 0) {
1166 printf("GEOM_MULTIPATH: removing %s from %s\n",
1167 cp->provider->name, cp->geom->name);
1169 g_multipath_fault(cp, MP_LOST);
1170 cnt = (uintptr_t *)&cp->private;
1171 if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
1172 cp->index |= MP_POSTED;
1173 mtx_unlock(&sc->sc_mtx);
1176 return; /* Recursion happened. */
1177 mtx_lock(&sc->sc_mtx);
1181 mtx_unlock(&sc->sc_mtx);
1183 gctl_error(req, "Provider %s not found", name);
1186 static struct g_geom *
1187 g_multipath_find_geom(struct g_class *mp, const char *name)
1190 struct g_multipath_softc *sc;
1192 LIST_FOREACH(gp, &mp->geom, geom) {
1194 if (sc == NULL || sc->sc_stopping)
1196 if (strcmp(gp->name, name) == 0)
1203 g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp)
1209 g_topology_assert();
1211 name = gctl_get_asciiparam(req, "arg0");
1213 gctl_error(req, "No 'arg0' argument");
1216 gp = g_multipath_find_geom(mp, name);
1218 gctl_error(req, "Device %s is invalid", name);
1221 error = g_multipath_destroy(gp);
1222 if (error != 0 && error != EINPROGRESS)
1223 gctl_error(req, "failed to stop %s (err=%d)", name, error);
1227 g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1230 struct g_multipath_softc *sc;
1231 struct g_consumer *cp;
1232 struct g_provider *pp;
1237 g_topology_assert();
1239 name = gctl_get_asciiparam(req, "arg0");
1241 gctl_error(req, "No 'arg0' argument");
1244 gp = g_multipath_find_geom(mp, name);
1246 gctl_error(req, "Device %s is invalid", name);
1251 if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
1254 error = g_access(cp, 1, 1, 1);
1256 gctl_error(req, "Can't open %s (%d)", pp->name, error);
1259 g_topology_unlock();
1260 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1261 error = g_write_data(cp, pp->mediasize - pp->sectorsize,
1262 buf, pp->sectorsize);
1264 g_access(cp, -1, -1, -1);
1266 gctl_error(req, "Can't erase metadata on %s (%d)",
1271 error = g_multipath_destroy(gp);
1272 if (error != 0 && error != EINPROGRESS)
1273 gctl_error(req, "failed to destroy %s (err=%d)", name, error);
1277 g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp)
1283 g_topology_assert();
1285 name = gctl_get_asciiparam(req, "arg0");
1287 gctl_error(req, "No 'arg0' argument");
1290 gp = g_multipath_find_geom(mp, name);
1292 gctl_error(req, "Device %s is invalid", name);
1295 error = g_multipath_rotate(gp);
1297 gctl_error(req, "failed to rotate %s (err=%d)", name, error);
1302 g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp)
1306 struct g_multipath_softc *sc;
1307 struct g_consumer *cp;
1311 sb = sbuf_new_auto();
1313 g_topology_assert();
1314 name = gctl_get_asciiparam(req, "arg0");
1316 gctl_error(req, "No 'arg0' argument");
1319 gp = g_multipath_find_geom(mp, name);
1321 gctl_error(req, "Device %s is invalid", name);
1325 if (sc->sc_active_active == 1) {
1327 LIST_FOREACH(cp, &gp->consumer, consumer) {
1328 if (cp->index & MP_BAD)
1332 sbuf_cat(sb, cp->provider->name);
1336 sbuf_cat(sb, "none");
1338 } else if (sc->sc_active && sc->sc_active->provider) {
1339 sbuf_printf(sb, "%s\n", sc->sc_active->provider->name);
1341 sbuf_printf(sb, "none\n");
1344 gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1349 g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1352 g_topology_assert();
1353 version = gctl_get_paraml(req, "version", sizeof(*version));
1354 if (version == NULL) {
1355 gctl_error(req, "No 'version' argument");
1356 } else if (*version != G_MULTIPATH_VERSION) {
1357 gctl_error(req, "Userland and kernel parts are out of sync");
1358 } else if (strcmp(verb, "add") == 0) {
1359 g_multipath_ctl_add(req, mp);
1360 } else if (strcmp(verb, "prefer") == 0) {
1361 g_multipath_ctl_prefer(req, mp);
1362 } else if (strcmp(verb, "create") == 0) {
1363 g_multipath_ctl_create(req, mp);
1364 } else if (strcmp(verb, "configure") == 0) {
1365 g_multipath_ctl_configure(req, mp);
1366 } else if (strcmp(verb, "stop") == 0) {
1367 g_multipath_ctl_stop(req, mp);
1368 } else if (strcmp(verb, "destroy") == 0) {
1369 g_multipath_ctl_destroy(req, mp);
1370 } else if (strcmp(verb, "fail") == 0) {
1371 g_multipath_ctl_fail(req, mp, 1);
1372 } else if (strcmp(verb, "restore") == 0) {
1373 g_multipath_ctl_fail(req, mp, 0);
1374 } else if (strcmp(verb, "remove") == 0) {
1375 g_multipath_ctl_remove(req, mp);
1376 } else if (strcmp(verb, "rotate") == 0) {
1377 g_multipath_ctl_rotate(req, mp);
1378 } else if (strcmp(verb, "getactive") == 0) {
1379 g_multipath_ctl_getactive(req, mp);
1381 gctl_error(req, "Unknown verb %s", verb);
1386 g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1387 struct g_consumer *cp, struct g_provider *pp)
1389 struct g_multipath_softc *sc;
1392 g_topology_assert();
1398 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1399 (cp->index & MP_NEW) ? "NEW" :
1400 (cp->index & MP_LOST) ? "LOST" :
1401 (cp->index & MP_FAIL) ? "FAIL" :
1402 (sc->sc_active_active == 1 || sc->sc_active == cp) ?
1404 sc->sc_active_active == 2 ? "READ" : "PASSIVE");
1406 good = g_multipath_good(gp);
1407 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1408 good == 0 ? "BROKEN" :
1409 (good != sc->sc_ndisks || sc->sc_ndisks == 1) ?
1410 "DEGRADED" : "OPTIMAL");
1412 if (cp == NULL && pp == NULL) {
1413 sbuf_printf(sb, "%s<UUID>%s</UUID>\n", indent, sc->sc_uuid);
1414 sbuf_printf(sb, "%s<Mode>Active/%s</Mode>\n", indent,
1415 sc->sc_active_active == 2 ? "Read" :
1416 sc->sc_active_active == 1 ? "Active" : "Passive");
1417 sbuf_printf(sb, "%s<Type>%s</Type>\n", indent,
1418 sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC");
1422 DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);