2 * Copyright (c) 2006-2007 Matthew Jacob <mjacob@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Based upon work by Pawel Jakub Dawidek <pjd@FreeBSD.org> for all of the
28 * fine geom examples, and by Poul Henning Kamp <phk@FreeBSD.org> for GEOM
29 * itself, all of which is most gratefully acknowledged.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/mutex.h>
42 #include <sys/sysctl.h>
43 #include <sys/kthread.h>
44 #include <sys/malloc.h>
45 #include <geom/geom.h>
46 #include <geom/multipath/g_multipath.h>
48 FEATURE(geom_multipath, "GEOM multipath support");
50 SYSCTL_DECL(_kern_geom);
51 SYSCTL_NODE(_kern_geom, OID_AUTO, multipath, CTLFLAG_RW, 0,
52 "GEOM_MULTIPATH tunables");
53 static u_int g_multipath_debug = 0;
54 SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, debug, CTLFLAG_RW,
55 &g_multipath_debug, 0, "Debug level");
61 } g_multipath_kt_state;
62 static struct bio_queue_head gmtbq;
63 static struct mtx gmtbq_mtx;
65 static void g_multipath_orphan(struct g_consumer *);
66 static void g_multipath_start(struct bio *);
67 static void g_multipath_done(struct bio *);
68 static void g_multipath_done_error(struct bio *);
69 static void g_multipath_kt(void *);
71 static int g_multipath_destroy(struct g_geom *);
73 g_multipath_destroy_geom(struct gctl_req *, struct g_class *, struct g_geom *);
75 static struct g_geom *g_multipath_find_geom(struct g_class *, const char *);
76 static int g_multipath_rotate(struct g_geom *);
78 static g_taste_t g_multipath_taste;
79 static g_ctl_req_t g_multipath_config;
80 static g_init_t g_multipath_init;
81 static g_fini_t g_multipath_fini;
83 struct g_class g_multipath_class = {
84 .name = G_MULTIPATH_CLASS_NAME,
86 .ctlreq = g_multipath_config,
87 .taste = g_multipath_taste,
88 .destroy_geom = g_multipath_destroy_geom,
89 .init = g_multipath_init,
90 .fini = g_multipath_fini
97 g_mpd(void *arg, int flags __unused)
99 struct g_consumer *cp;
103 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
104 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
106 printf("GEOM_MULTIPATH: %s removed from %s\n",
107 cp->provider->name, cp->geom->name);
110 g_destroy_consumer(cp);
114 g_multipath_orphan(struct g_consumer *cp)
116 if ((cp->index & MP_POSTED) == 0) {
117 cp->index |= MP_POSTED;
118 printf("GEOM_MULTIPATH: %s orphaned in %s\n",
119 cp->provider->name, cp->geom->name);
125 g_multipath_start(struct bio *bp)
127 struct g_multipath_softc *sc;
129 struct g_consumer *cp;
132 gp = bp->bio_to->geom;
134 KASSERT(sc != NULL, ("NULL sc"));
137 g_io_deliver(bp, ENXIO);
140 cbp = g_clone_bio(bp);
142 g_io_deliver(bp, ENOMEM);
145 cbp->bio_done = g_multipath_done;
146 g_io_request(cbp, cp);
150 g_multipath_done(struct bio *bp)
152 if (bp->bio_error == ENXIO || bp->bio_error == EIO) {
153 mtx_lock(&gmtbq_mtx);
154 bioq_insert_tail(&gmtbq, bp);
155 wakeup(&g_multipath_kt_state);
156 mtx_unlock(&gmtbq_mtx);
163 g_multipath_done_error(struct bio *bp)
167 struct g_multipath_softc *sc;
168 struct g_consumer *cp;
169 struct g_provider *pp;
172 * If we had a failure, we have to check first to see
173 * whether the consumer it failed on was the currently
174 * active consumer (i.e., this is the first in perhaps
175 * a number of failures). If so, we then switch consumers
176 * to the next available consumer.
180 pbp = bp->bio_parent;
181 gp = pbp->bio_to->geom;
187 if (cp->nend == cp->nstart && pp->nend == pp->nstart) {
188 cp->index |= MP_POSTED;
189 g_post_event(g_mpd, cp, M_NOWAIT, NULL);
191 if (cp == sc->cp_active) {
192 struct g_consumer *lcp;
193 printf("GEOM_MULTIPATH: %s failed in %s\n",
194 pp->name, sc->sc_name);
195 sc->cp_active = NULL;
196 LIST_FOREACH(lcp, &gp->consumer, consumer) {
197 if ((lcp->index & MP_BAD) == 0) {
202 if (sc->cp_active == NULL || sc->cp_active->provider == NULL) {
203 printf("GEOM_MULTIPATH: out of providers for %s\n",
208 printf("GEOM_MULTIPATH: %s now active path in %s\n",
209 sc->cp_active->provider->name, sc->sc_name);
215 * If we can fruitfully restart the I/O, do so.
220 g_multipath_start(pbp);
227 g_multipath_kt(void *arg)
230 g_multipath_kt_state = GKT_RUN;
231 mtx_lock(&gmtbq_mtx);
232 while (g_multipath_kt_state == GKT_RUN) {
236 bp = bioq_takefirst(&gmtbq);
239 mtx_unlock(&gmtbq_mtx);
240 g_multipath_done_error(bp);
241 mtx_lock(&gmtbq_mtx);
243 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
244 "gkt:wait", hz / 10);
246 mtx_unlock(&gmtbq_mtx);
247 wakeup(&g_multipath_kt_state);
253 g_multipath_access(struct g_provider *pp, int dr, int dw, int de)
256 struct g_consumer *cp, *badcp = NULL;
261 LIST_FOREACH(cp, &gp->consumer, consumer) {
262 error = g_access(cp, dr, dw, de);
271 LIST_FOREACH(cp, &gp->consumer, consumer) {
274 (void) g_access(cp, -dr, -dw, -de);
279 static struct g_geom *
280 g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md)
282 struct g_multipath_softc *sc;
284 struct g_provider *pp;
288 LIST_FOREACH(gp, &mp->geom, geom) {
289 if (strcmp(gp->name, md->md_name) == 0) {
290 printf("GEOM_MULTIPATH: name %s already exists\n",
296 gp = g_new_geomf(mp, md->md_name);
297 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
299 gp->start = g_multipath_start;
300 gp->orphan = g_multipath_orphan;
301 gp->access = g_multipath_access;
302 memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid));
303 memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name));
305 pp = g_new_providerf(gp, "multipath/%s", md->md_name);
306 /* limit the provider to not have it stomp on metadata */
307 pp->mediasize = md->md_size - md->md_sectorsize;
308 pp->sectorsize = md->md_sectorsize;
310 g_error_provider(pp, 0);
315 g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp)
317 struct g_multipath_softc *sc;
318 struct g_consumer *cp, *nxtcp;
324 KASSERT(sc, ("no softc"));
327 * Make sure that the passed provider isn't already attached
329 LIST_FOREACH(cp, &gp->consumer, consumer) {
330 if (cp->provider == pp)
334 printf("GEOM_MULTIPATH: provider %s already attached to %s\n",
338 nxtcp = LIST_FIRST(&gp->consumer);
339 cp = g_new_consumer(gp);
340 error = g_attach(cp, pp);
342 printf("GEOM_MULTIPATH: cannot attach %s to %s",
343 pp->name, sc->sc_name);
344 g_destroy_consumer(cp);
351 * Set access permissions on new consumer to match other consumers
353 if (nxtcp && (nxtcp->acr + nxtcp->acw + nxtcp->ace)) {
354 error = g_access(cp, nxtcp->acr, nxtcp->acw, nxtcp->ace);
356 printf("GEOM_MULTIPATH: cannot set access in "
357 "attaching %s to %s/%s (%d)\n",
358 pp->name, sc->sc_name, sc->sc_uuid, error);
360 g_destroy_consumer(cp);
364 printf("GEOM_MULTIPATH: adding %s to %s/%s\n",
365 pp->name, sc->sc_name, sc->sc_uuid);
366 if (sc->cp_active == NULL) {
368 printf("GEOM_MULTIPATH: %s now active path in %s\n",
369 pp->name, sc->sc_name);
375 g_multipath_destroy(struct g_geom *gp)
377 struct g_provider *pp;
380 if (gp->softc == NULL)
382 pp = LIST_FIRST(&gp->provider);
383 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0))
385 printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
388 g_wither_geom(gp, ENXIO);
393 g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
397 return (g_multipath_destroy(gp));
401 g_multipath_rotate(struct g_geom *gp)
403 struct g_consumer *lcp;
404 struct g_multipath_softc *sc = gp->softc;
409 LIST_FOREACH(lcp, &gp->consumer, consumer) {
410 if ((lcp->index & MP_BAD) == 0) {
411 if (sc->cp_active != lcp) {
418 printf("GEOM_MULTIPATH: %s now active path in %s\n",
419 lcp->provider->name, sc->sc_name);
425 g_multipath_init(struct g_class *mp)
428 mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
429 if (kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt") == 0)
430 g_multipath_kt_state = GKT_RUN;
434 g_multipath_fini(struct g_class *mp)
436 if (g_multipath_kt_state == GKT_RUN) {
437 mtx_lock(&gmtbq_mtx);
438 g_multipath_kt_state = GKT_DIE;
439 wakeup(&g_multipath_kt_state);
440 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
442 mtx_unlock(&gmtbq_mtx);
447 g_multipath_read_metadata(struct g_consumer *cp,
448 struct g_multipath_metadata *md)
450 struct g_provider *pp;
455 error = g_access(cp, 1, 0, 0);
460 buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
461 pp->sectorsize, &error);
463 g_access(cp, -1, 0, 0);
466 multipath_metadata_decode(buf, md);
471 static struct g_geom *
472 g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
474 struct g_multipath_metadata md;
475 struct g_multipath_softc *sc;
476 struct g_consumer *cp;
477 struct g_geom *gp, *gp1;
482 gp = g_new_geomf(mp, "multipath:taste");
483 gp->start = g_multipath_start;
484 gp->access = g_multipath_access;
485 gp->orphan = g_multipath_orphan;
486 cp = g_new_consumer(gp);
488 error = g_multipath_read_metadata(cp, &md);
490 g_destroy_consumer(cp);
496 if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
497 if (g_multipath_debug)
498 printf("%s is not MULTIPATH\n", pp->name);
501 if (md.md_version != G_MULTIPATH_VERSION) {
502 printf("%s has version %d multipath id- this module is version "
503 " %d: rejecting\n", pp->name, md.md_version,
504 G_MULTIPATH_VERSION);
507 if (g_multipath_debug)
508 printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
511 * Let's check if such a device already is present. We check against
512 * uuid alone first because that's the true distinguishor. If that
513 * passes, then we check for name conflicts. If there are conflicts,
516 * The whole purpose of this is to solve the problem that people don't
517 * pick good unique names, but good unique names (like uuids) are a
518 * pain to use. So, we allow people to build GEOMs with friendly names
519 * and uuids, and modify the names in case there's a collision.
522 LIST_FOREACH(gp, &mp->geom, geom) {
526 if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
530 LIST_FOREACH(gp1, &mp->geom, geom) {
536 if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
541 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
543 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
544 * with the same name (but a different UUID).
546 * If gp is NULL, then modify the name with a random number and
547 * complain, but allow the creation of the geom to continue.
549 * If gp is *not* NULL, just use the geom's name as we're attaching
550 * this disk to the (previously generated) name.
557 u_long rand = random();
559 snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
560 printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
561 sc->sc_name, sc->sc_uuid);
562 printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
564 strlcpy(md.md_name, buf, sizeof(md.md_name));
566 strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
571 gp = g_multipath_create(mp, &md);
573 printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
574 md.md_name, md.md_uuid);
583 KASSERT(sc != NULL, ("sc is NULL"));
584 error = g_multipath_add_disk(gp, pp);
587 g_multipath_destroy(gp);
594 g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp)
597 struct g_consumer *cp;
598 struct g_provider *pp, *pp0;
599 const char *name, *mpname;
600 static const char devpf[6] = "/dev/";
604 mpname = gctl_get_asciiparam(req, "arg0");
605 if (mpname == NULL) {
606 gctl_error(req, "No 'arg0' argument");
609 gp = g_multipath_find_geom(mp, mpname);
611 gctl_error(req, "Device %s is invalid", mpname);
615 name = gctl_get_asciiparam(req, "arg1");
617 gctl_error(req, "No 'arg1' argument");
620 if (strncmp(name, devpf, 5) == 0)
622 pp = g_provider_by_name(name);
624 gctl_error(req, "Provider %s is invalid", name);
629 * Check to make sure parameters match, if we already have one.
631 cp = LIST_FIRST(&gp->consumer);
639 gctl_error(req, "providers %s and %s are the same",
640 pp0->name, pp->name);
643 if (pp0->mediasize != pp->mediasize) {
644 gctl_error(req, "Provider %s is %jd; Provider %s is %jd",
645 pp0->name, (intmax_t) pp0->mediasize,
646 pp->name, (intmax_t) pp->mediasize);
649 if (pp0->sectorsize != pp->sectorsize) {
650 gctl_error(req, "Provider %s has sectorsize %u; Provider %s "
651 "has sectorsize %u", pp0->name, pp0->sectorsize,
652 pp->name, pp->sectorsize);
660 (void) g_multipath_add_disk(gp, pp);
663 static struct g_geom *
664 g_multipath_find_geom(struct g_class *mp, const char *name)
668 LIST_FOREACH(gp, &mp->geom, geom) {
669 if (strcmp(gp->name, name) == 0) {
677 g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
685 name = gctl_get_asciiparam(req, "arg0");
687 gctl_error(req, "No 'arg0' argument");
690 gp = g_multipath_find_geom(mp, name);
692 gctl_error(req, "Device %s is invalid", name);
695 error = g_multipath_destroy(gp);
697 gctl_error(req, "failed to destroy %s (err=%d)", name, error);
702 g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp)
710 name = gctl_get_asciiparam(req, "arg0");
712 gctl_error(req, "No 'arg0' argument");
715 gp = g_multipath_find_geom(mp, name);
717 gctl_error(req, "Device %s is invalid", name);
720 error = g_multipath_rotate(gp);
722 gctl_error(req, "failed to rotate %s (err=%d)", name, error);
727 g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp)
731 struct g_multipath_softc *sc;
734 sb = sbuf_new_auto();
737 name = gctl_get_asciiparam(req, "arg0");
739 gctl_error(req, "No 'arg0' argument");
742 gp = g_multipath_find_geom(mp, name);
744 gctl_error(req, "Device %s is invalid", name);
748 if (sc->cp_active && sc->cp_active->provider) {
749 sbuf_printf(sb, "%s\n", sc->cp_active->provider->name);
751 sbuf_printf(sb, "none\n");
754 gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
759 g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
763 version = gctl_get_paraml(req, "version", sizeof(*version));
764 if (version == NULL) {
765 gctl_error(req, "No 'version' argument");
766 } else if (*version != G_MULTIPATH_VERSION) {
767 gctl_error(req, "Userland and kernel parts are out of sync");
768 } else if (strcmp(verb, "add") == 0) {
769 g_multipath_ctl_add(req, mp);
770 } else if (strcmp(verb, "destroy") == 0) {
771 g_multipath_ctl_destroy(req, mp);
772 } else if (strcmp(verb, "rotate") == 0) {
773 g_multipath_ctl_rotate(req, mp);
774 } else if (strcmp(verb, "getactive") == 0) {
775 g_multipath_ctl_getactive(req, mp);
777 gctl_error(req, "Unknown verb %s", verb);
780 DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);