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 if (sc->sc_pp != NULL)
526 sc->sc_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
527 mtx_lock(&sc->sc_mtx);
530 mtx_unlock(&sc->sc_mtx);
531 printf("GEOM_MULTIPATH: %s added to %s\n",
532 pp->name, sc->sc_name);
533 if (sc->sc_active == NULL) {
535 if (sc->sc_active_active != 1)
536 printf("GEOM_MULTIPATH: %s is now active path in %s\n",
537 pp->name, sc->sc_name);
543 g_multipath_destroy(struct g_geom *gp)
545 struct g_multipath_softc *sc;
546 struct g_consumer *cp, *cp1;
549 if (gp->softc == NULL)
552 if (!sc->sc_stopping) {
553 printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
556 if (sc->sc_opened != 0) {
557 if (sc->sc_pp != NULL) {
558 g_wither_provider(sc->sc_pp, ENXIO);
561 return (EINPROGRESS);
563 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
564 mtx_lock(&sc->sc_mtx);
565 if ((cp->index & MP_POSTED) == 0) {
566 cp->index |= MP_POSTED;
567 mtx_unlock(&sc->sc_mtx);
570 return(0); /* Recursion happened. */
572 mtx_unlock(&sc->sc_mtx);
574 if (!LIST_EMPTY(&gp->consumer))
575 return (EINPROGRESS);
576 mtx_destroy(&sc->sc_mtx);
579 printf("GEOM_MULTIPATH: %s destroyed\n", gp->name);
580 g_wither_geom(gp, ENXIO);
585 g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
589 return (g_multipath_destroy(gp));
593 g_multipath_rotate(struct g_geom *gp)
595 struct g_consumer *lcp, *first_good_cp = NULL;
596 struct g_multipath_softc *sc = gp->softc;
597 int active_cp_seen = 0;
602 LIST_FOREACH(lcp, &gp->consumer, consumer) {
603 if ((lcp->index & MP_BAD) == 0) {
604 if (first_good_cp == NULL)
609 if (sc->sc_active == lcp)
614 if (lcp && lcp != sc->sc_active) {
616 if (sc->sc_active_active != 1)
617 printf("GEOM_MULTIPATH: %s is now active path in %s\n",
618 lcp->provider->name, sc->sc_name);
624 g_multipath_init(struct g_class *mp)
627 mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
628 kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt");
632 g_multipath_fini(struct g_class *mp)
634 if (g_multipath_kt_state == GKT_RUN) {
635 mtx_lock(&gmtbq_mtx);
636 g_multipath_kt_state = GKT_DIE;
637 wakeup(&g_multipath_kt_state);
638 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
640 mtx_unlock(&gmtbq_mtx);
645 g_multipath_read_metadata(struct g_consumer *cp,
646 struct g_multipath_metadata *md)
648 struct g_provider *pp;
653 error = g_access(cp, 1, 0, 0);
658 buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
659 pp->sectorsize, &error);
661 g_access(cp, -1, 0, 0);
664 multipath_metadata_decode(buf, md);
669 static struct g_geom *
670 g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
672 struct g_multipath_metadata md;
673 struct g_multipath_softc *sc;
674 struct g_consumer *cp;
675 struct g_geom *gp, *gp1;
680 gp = g_new_geomf(mp, "multipath:taste");
681 gp->start = g_multipath_start;
682 gp->access = g_multipath_access;
683 gp->orphan = g_multipath_orphan;
684 cp = g_new_consumer(gp);
686 error = g_multipath_read_metadata(cp, &md);
688 g_destroy_consumer(cp);
694 if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
695 if (g_multipath_debug)
696 printf("%s is not MULTIPATH\n", pp->name);
699 if (md.md_version != G_MULTIPATH_VERSION) {
700 printf("%s has version %d multipath id- this module is version "
701 " %d: rejecting\n", pp->name, md.md_version,
702 G_MULTIPATH_VERSION);
705 if (md.md_size != 0 && md.md_size != pp->mediasize)
707 if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize)
709 if (g_multipath_debug)
710 printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
713 * Let's check if such a device already is present. We check against
714 * uuid alone first because that's the true distinguishor. If that
715 * passes, then we check for name conflicts. If there are conflicts,
718 * The whole purpose of this is to solve the problem that people don't
719 * pick good unique names, but good unique names (like uuids) are a
720 * pain to use. So, we allow people to build GEOMs with friendly names
721 * and uuids, and modify the names in case there's a collision.
724 LIST_FOREACH(gp, &mp->geom, geom) {
726 if (sc == NULL || sc->sc_stopping)
728 if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
732 LIST_FOREACH(gp1, &mp->geom, geom) {
736 if (sc == NULL || sc->sc_stopping)
738 if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
743 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
745 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
746 * with the same name (but a different UUID).
748 * If gp is NULL, then modify the name with a random number and
749 * complain, but allow the creation of the geom to continue.
751 * If gp is *not* NULL, just use the geom's name as we're attaching
752 * this disk to the (previously generated) name.
759 u_long rand = random();
761 snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
762 printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
763 sc->sc_name, sc->sc_uuid);
764 printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
766 strlcpy(md.md_name, buf, sizeof(md.md_name));
768 strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
773 gp = g_multipath_create(mp, &md);
775 printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
776 md.md_name, md.md_uuid);
785 KASSERT(sc != NULL, ("sc is NULL"));
786 error = g_multipath_add_disk(gp, pp);
789 g_multipath_destroy(gp);
796 g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp,
799 struct g_multipath_softc *sc;
801 struct g_consumer *cp;
802 struct g_provider *pp;
804 static const char devpf[6] = "/dev/";
808 mpname = gctl_get_asciiparam(req, "arg0");
809 if (mpname == NULL) {
810 gctl_error(req, "No 'arg0' argument");
813 gp = g_multipath_find_geom(mp, mpname);
815 gctl_error(req, "Device %s is invalid", mpname);
820 if (strncmp(name, devpf, 5) == 0)
822 pp = g_provider_by_name(name);
824 gctl_error(req, "Provider %s is invalid", name);
829 * Check to make sure parameters match.
831 LIST_FOREACH(cp, &gp->consumer, consumer) {
832 if (cp->provider == pp) {
833 gctl_error(req, "provider %s is already there",
838 if (sc->sc_pp != NULL && sc->sc_pp->mediasize != 0 &&
839 sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0)
841 gctl_error(req, "Providers size mismatch %jd != %jd",
842 (intmax_t) sc->sc_pp->mediasize +
843 (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0),
844 (intmax_t) pp->mediasize);
847 if (sc->sc_pp != NULL && sc->sc_pp->sectorsize != 0 &&
848 sc->sc_pp->sectorsize != pp->sectorsize) {
849 gctl_error(req, "Providers sectorsize mismatch %u != %u",
850 sc->sc_pp->sectorsize, pp->sectorsize);
857 (void) g_multipath_add_disk(gp, pp);
861 g_multipath_ctl_prefer(struct gctl_req *req, struct g_class *mp)
864 struct g_multipath_softc *sc;
865 struct g_consumer *cp;
866 const char *name, *mpname;
867 static const char devpf[6] = "/dev/";
872 mpname = gctl_get_asciiparam(req, "arg0");
873 if (mpname == NULL) {
874 gctl_error(req, "No 'arg0' argument");
877 gp = g_multipath_find_geom(mp, mpname);
879 gctl_error(req, "Device %s is invalid", mpname);
884 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
886 gctl_error(req, "No 'nargs' argument");
890 gctl_error(req, "missing device");
894 name = gctl_get_asciiparam(req, "arg1");
896 gctl_error(req, "No 'arg1' argument");
899 if (strncmp(name, devpf, 5) == 0) {
903 LIST_FOREACH(cp, &gp->consumer, consumer) {
904 if (cp->provider != NULL
905 && strcmp(cp->provider->name, name) == 0)
910 gctl_error(req, "Provider %s not found", name);
914 mtx_lock(&sc->sc_mtx);
916 if (cp->index & MP_BAD) {
917 gctl_error(req, "Consumer %s is invalid", name);
918 mtx_unlock(&sc->sc_mtx);
922 /* Here when the consumer is present and in good shape */
925 if (!sc->sc_active_active)
926 printf("GEOM_MULTIPATH: %s now active path in %s\n",
927 sc->sc_active->provider->name, sc->sc_name);
929 mtx_unlock(&sc->sc_mtx);
933 g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp)
935 struct g_multipath_softc *sc;
937 const char *mpname, *name;
939 mpname = gctl_get_asciiparam(req, "arg0");
940 if (mpname == NULL) {
941 gctl_error(req, "No 'arg0' argument");
944 gp = g_multipath_find_geom(mp, mpname);
946 gctl_error(req, "Device %s not found", mpname);
951 name = gctl_get_asciiparam(req, "arg1");
953 gctl_error(req, "No 'arg1' argument");
956 g_multipath_ctl_add_name(req, mp, name);
960 g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp)
962 struct g_multipath_metadata md;
963 struct g_multipath_softc *sc;
965 const char *mpname, *name;
971 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
973 gctl_error(req, "wrong number of arguments.");
977 mpname = gctl_get_asciiparam(req, "arg0");
978 if (mpname == NULL) {
979 gctl_error(req, "No 'arg0' argument");
982 gp = g_multipath_find_geom(mp, mpname);
984 gctl_error(req, "Device %s already exist", mpname);
988 memset(&md, 0, sizeof(md));
989 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
990 md.md_version = G_MULTIPATH_VERSION;
991 strlcpy(md.md_name, mpname, sizeof(md.md_name));
993 md.md_sectorsize = 0;
995 md.md_active_active = 0;
996 val = gctl_get_paraml(req, "active_active", sizeof(*val));
997 if (val != NULL && *val != 0)
998 md.md_active_active = 1;
999 val = gctl_get_paraml(req, "active_read", sizeof(*val));
1000 if (val != NULL && *val != 0)
1001 md.md_active_active = 2;
1002 gp = g_multipath_create(mp, &md);
1004 gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n",
1005 md.md_name, md.md_uuid);
1010 for (i = 1; i < *nargs; i++) {
1011 snprintf(param, sizeof(param), "arg%d", i);
1012 name = gctl_get_asciiparam(req, param);
1013 g_multipath_ctl_add_name(req, mp, name);
1016 if (sc->sc_ndisks != (*nargs - 1))
1017 g_multipath_destroy(gp);
1021 g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp)
1023 struct g_multipath_softc *sc;
1025 struct g_consumer *cp;
1026 struct g_provider *pp;
1027 struct g_multipath_metadata md;
1032 g_topology_assert();
1034 name = gctl_get_asciiparam(req, "arg0");
1036 gctl_error(req, "No 'arg0' argument");
1039 gp = g_multipath_find_geom(mp, name);
1041 gctl_error(req, "Device %s is invalid", name);
1045 val = gctl_get_paraml(req, "active_active", sizeof(*val));
1046 if (val != NULL && *val != 0)
1047 sc->sc_active_active = 1;
1048 val = gctl_get_paraml(req, "active_read", sizeof(*val));
1049 if (val != NULL && *val != 0)
1050 sc->sc_active_active = 2;
1051 val = gctl_get_paraml(req, "active_passive", sizeof(*val));
1052 if (val != NULL && *val != 0)
1053 sc->sc_active_active = 0;
1054 if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
1057 error = g_access(cp, 1, 1, 1);
1059 gctl_error(req, "Can't open %s (%d)", pp->name, error);
1062 g_topology_unlock();
1063 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1064 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1065 memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
1066 strlcpy(md.md_name, name, sizeof(md.md_name));
1067 md.md_version = G_MULTIPATH_VERSION;
1068 md.md_size = pp->mediasize;
1069 md.md_sectorsize = pp->sectorsize;
1070 md.md_active_active = sc->sc_active_active;
1071 multipath_metadata_encode(&md, buf);
1072 error = g_write_data(cp, pp->mediasize - pp->sectorsize,
1073 buf, pp->sectorsize);
1075 g_access(cp, -1, -1, -1);
1077 gctl_error(req, "Can't update metadata on %s (%d)",
1083 g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail)
1085 struct g_multipath_softc *sc;
1087 struct g_consumer *cp;
1088 const char *mpname, *name;
1091 mpname = gctl_get_asciiparam(req, "arg0");
1092 if (mpname == NULL) {
1093 gctl_error(req, "No 'arg0' argument");
1096 gp = g_multipath_find_geom(mp, mpname);
1098 gctl_error(req, "Device %s not found", mpname);
1103 name = gctl_get_asciiparam(req, "arg1");
1105 gctl_error(req, "No 'arg1' argument");
1110 mtx_lock(&sc->sc_mtx);
1111 LIST_FOREACH(cp, &gp->consumer, consumer) {
1112 if (cp->provider != NULL &&
1113 strcmp(cp->provider->name, name) == 0 &&
1114 (cp->index & MP_LOST) == 0) {
1116 if (!fail == !(cp->index & MP_FAIL))
1118 printf("GEOM_MULTIPATH: %s in %s is marked %s.\n",
1119 name, sc->sc_name, fail ? "FAIL" : "OK");
1121 g_multipath_fault(cp, MP_FAIL);
1123 cp->index &= ~MP_FAIL;
1127 mtx_unlock(&sc->sc_mtx);
1129 gctl_error(req, "Provider %s not found", name);
1133 g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp)
1135 struct g_multipath_softc *sc;
1137 struct g_consumer *cp, *cp1;
1138 const char *mpname, *name;
1142 mpname = gctl_get_asciiparam(req, "arg0");
1143 if (mpname == NULL) {
1144 gctl_error(req, "No 'arg0' argument");
1147 gp = g_multipath_find_geom(mp, mpname);
1149 gctl_error(req, "Device %s not found", mpname);
1154 name = gctl_get_asciiparam(req, "arg1");
1156 gctl_error(req, "No 'arg1' argument");
1161 mtx_lock(&sc->sc_mtx);
1162 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
1163 if (cp->provider != NULL &&
1164 strcmp(cp->provider->name, name) == 0 &&
1165 (cp->index & MP_LOST) == 0) {
1167 printf("GEOM_MULTIPATH: removing %s from %s\n",
1168 cp->provider->name, cp->geom->name);
1170 g_multipath_fault(cp, MP_LOST);
1171 cnt = (uintptr_t *)&cp->private;
1172 if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
1173 cp->index |= MP_POSTED;
1174 mtx_unlock(&sc->sc_mtx);
1177 return; /* Recursion happened. */
1178 mtx_lock(&sc->sc_mtx);
1182 mtx_unlock(&sc->sc_mtx);
1184 gctl_error(req, "Provider %s not found", name);
1187 static struct g_geom *
1188 g_multipath_find_geom(struct g_class *mp, const char *name)
1191 struct g_multipath_softc *sc;
1193 LIST_FOREACH(gp, &mp->geom, geom) {
1195 if (sc == NULL || sc->sc_stopping)
1197 if (strcmp(gp->name, name) == 0)
1204 g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp)
1210 g_topology_assert();
1212 name = gctl_get_asciiparam(req, "arg0");
1214 gctl_error(req, "No 'arg0' argument");
1217 gp = g_multipath_find_geom(mp, name);
1219 gctl_error(req, "Device %s is invalid", name);
1222 error = g_multipath_destroy(gp);
1223 if (error != 0 && error != EINPROGRESS)
1224 gctl_error(req, "failed to stop %s (err=%d)", name, error);
1228 g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1231 struct g_multipath_softc *sc;
1232 struct g_consumer *cp;
1233 struct g_provider *pp;
1238 g_topology_assert();
1240 name = gctl_get_asciiparam(req, "arg0");
1242 gctl_error(req, "No 'arg0' argument");
1245 gp = g_multipath_find_geom(mp, name);
1247 gctl_error(req, "Device %s is invalid", name);
1252 if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
1255 error = g_access(cp, 1, 1, 1);
1257 gctl_error(req, "Can't open %s (%d)", pp->name, error);
1260 g_topology_unlock();
1261 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1262 error = g_write_data(cp, pp->mediasize - pp->sectorsize,
1263 buf, pp->sectorsize);
1265 g_access(cp, -1, -1, -1);
1267 gctl_error(req, "Can't erase metadata on %s (%d)",
1272 error = g_multipath_destroy(gp);
1273 if (error != 0 && error != EINPROGRESS)
1274 gctl_error(req, "failed to destroy %s (err=%d)", name, error);
1278 g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp)
1284 g_topology_assert();
1286 name = gctl_get_asciiparam(req, "arg0");
1288 gctl_error(req, "No 'arg0' argument");
1291 gp = g_multipath_find_geom(mp, name);
1293 gctl_error(req, "Device %s is invalid", name);
1296 error = g_multipath_rotate(gp);
1298 gctl_error(req, "failed to rotate %s (err=%d)", name, error);
1303 g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp)
1307 struct g_multipath_softc *sc;
1308 struct g_consumer *cp;
1312 sb = sbuf_new_auto();
1314 g_topology_assert();
1315 name = gctl_get_asciiparam(req, "arg0");
1317 gctl_error(req, "No 'arg0' argument");
1320 gp = g_multipath_find_geom(mp, name);
1322 gctl_error(req, "Device %s is invalid", name);
1326 if (sc->sc_active_active == 1) {
1328 LIST_FOREACH(cp, &gp->consumer, consumer) {
1329 if (cp->index & MP_BAD)
1333 sbuf_cat(sb, cp->provider->name);
1337 sbuf_cat(sb, "none");
1339 } else if (sc->sc_active && sc->sc_active->provider) {
1340 sbuf_printf(sb, "%s\n", sc->sc_active->provider->name);
1342 sbuf_printf(sb, "none\n");
1345 gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1350 g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1353 g_topology_assert();
1354 version = gctl_get_paraml(req, "version", sizeof(*version));
1355 if (version == NULL) {
1356 gctl_error(req, "No 'version' argument");
1357 } else if (*version != G_MULTIPATH_VERSION) {
1358 gctl_error(req, "Userland and kernel parts are out of sync");
1359 } else if (strcmp(verb, "add") == 0) {
1360 g_multipath_ctl_add(req, mp);
1361 } else if (strcmp(verb, "prefer") == 0) {
1362 g_multipath_ctl_prefer(req, mp);
1363 } else if (strcmp(verb, "create") == 0) {
1364 g_multipath_ctl_create(req, mp);
1365 } else if (strcmp(verb, "configure") == 0) {
1366 g_multipath_ctl_configure(req, mp);
1367 } else if (strcmp(verb, "stop") == 0) {
1368 g_multipath_ctl_stop(req, mp);
1369 } else if (strcmp(verb, "destroy") == 0) {
1370 g_multipath_ctl_destroy(req, mp);
1371 } else if (strcmp(verb, "fail") == 0) {
1372 g_multipath_ctl_fail(req, mp, 1);
1373 } else if (strcmp(verb, "restore") == 0) {
1374 g_multipath_ctl_fail(req, mp, 0);
1375 } else if (strcmp(verb, "remove") == 0) {
1376 g_multipath_ctl_remove(req, mp);
1377 } else if (strcmp(verb, "rotate") == 0) {
1378 g_multipath_ctl_rotate(req, mp);
1379 } else if (strcmp(verb, "getactive") == 0) {
1380 g_multipath_ctl_getactive(req, mp);
1382 gctl_error(req, "Unknown verb %s", verb);
1387 g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1388 struct g_consumer *cp, struct g_provider *pp)
1390 struct g_multipath_softc *sc;
1393 g_topology_assert();
1399 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1400 (cp->index & MP_NEW) ? "NEW" :
1401 (cp->index & MP_LOST) ? "LOST" :
1402 (cp->index & MP_FAIL) ? "FAIL" :
1403 (sc->sc_active_active == 1 || sc->sc_active == cp) ?
1405 sc->sc_active_active == 2 ? "READ" : "PASSIVE");
1407 good = g_multipath_good(gp);
1408 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1409 good == 0 ? "BROKEN" :
1410 (good != sc->sc_ndisks || sc->sc_ndisks == 1) ?
1411 "DEGRADED" : "OPTIMAL");
1413 if (cp == NULL && pp == NULL) {
1414 sbuf_printf(sb, "%s<UUID>%s</UUID>\n", indent, sc->sc_uuid);
1415 sbuf_printf(sb, "%s<Mode>Active/%s</Mode>\n", indent,
1416 sc->sc_active_active == 2 ? "Read" :
1417 sc->sc_active_active == 1 ? "Active" : "Passive");
1418 sbuf_printf(sb, "%s<Type>%s</Type>\n", indent,
1419 sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC");
1423 DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);