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>
41 #include <sys/sysctl.h>
42 #include <sys/kthread.h>
43 #include <sys/malloc.h>
44 #include <geom/geom.h>
45 #include <geom/multipath/g_multipath.h>
48 SYSCTL_DECL(_kern_geom);
49 SYSCTL_NODE(_kern_geom, OID_AUTO, multipath, CTLFLAG_RW, 0,
50 "GEOM_MULTIPATH tunables");
51 static u_int g_multipath_debug = 0;
52 SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, debug, CTLFLAG_RW,
53 &g_multipath_debug, 0, "Debug level");
59 } g_multipath_kt_state;
60 static struct bio_queue_head gmtbq;
61 static struct mtx gmtbq_mtx;
63 static void g_multipath_orphan(struct g_consumer *);
64 static void g_multipath_start(struct bio *);
65 static void g_multipath_done(struct bio *);
66 static void g_multipath_done_error(struct bio *);
67 static void g_multipath_kt(void *);
69 static int g_multipath_destroy(struct g_geom *);
71 g_multipath_destroy_geom(struct gctl_req *, struct g_class *, struct g_geom *);
73 static struct g_geom *g_multipath_find_geom(struct g_class *, const char *);
74 static int g_multipath_rotate(struct g_geom *);
76 static g_taste_t g_multipath_taste;
77 static g_ctl_req_t g_multipath_config;
78 static g_init_t g_multipath_init;
79 static g_fini_t g_multipath_fini;
81 struct g_class g_multipath_class = {
82 .name = G_MULTIPATH_CLASS_NAME,
84 .ctlreq = g_multipath_config,
85 .taste = g_multipath_taste,
86 .destroy_geom = g_multipath_destroy_geom,
87 .init = g_multipath_init,
88 .fini = g_multipath_fini
95 g_mpd(void *arg, int flags __unused)
97 struct g_consumer *cp;
101 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
102 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
104 printf("GEOM_MULTIPATH: %s removed from %s\n",
105 cp->provider->name, cp->geom->name);
108 g_destroy_consumer(cp);
112 g_multipath_orphan(struct g_consumer *cp)
114 if ((cp->index & MP_POSTED) == 0) {
115 cp->index |= MP_POSTED;
116 printf("GEOM_MULTIPATH: %s orphaned in %s\n",
117 cp->provider->name, cp->geom->name);
123 g_multipath_start(struct bio *bp)
125 struct g_multipath_softc *sc;
127 struct g_consumer *cp;
130 gp = bp->bio_to->geom;
132 KASSERT(sc != NULL, ("NULL sc"));
135 g_io_deliver(bp, ENXIO);
138 cbp = g_clone_bio(bp);
140 g_io_deliver(bp, ENOMEM);
143 cbp->bio_done = g_multipath_done;
144 g_io_request(cbp, cp);
148 g_multipath_done(struct bio *bp)
150 if (bp->bio_error == ENXIO || bp->bio_error == EIO) {
151 mtx_lock(&gmtbq_mtx);
152 bioq_insert_tail(&gmtbq, bp);
153 wakeup(&g_multipath_kt_state);
154 mtx_unlock(&gmtbq_mtx);
161 g_multipath_done_error(struct bio *bp)
165 struct g_multipath_softc *sc;
166 struct g_consumer *cp;
167 struct g_provider *pp;
170 * If we had a failure, we have to check first to see
171 * whether the consumer it failed on was the currently
172 * active consumer (i.e., this is the first in perhaps
173 * a number of failures). If so, we then switch consumers
174 * to the next available consumer.
178 pbp = bp->bio_parent;
179 gp = pbp->bio_to->geom;
185 if (cp->nend == cp->nstart && pp->nend == pp->nstart) {
186 cp->index |= MP_POSTED;
187 g_post_event(g_mpd, cp, M_NOWAIT, NULL);
189 if (cp == sc->cp_active) {
190 struct g_consumer *lcp;
191 printf("GEOM_MULTIPATH: %s failed in %s\n",
192 pp->name, sc->sc_name);
193 sc->cp_active = NULL;
194 LIST_FOREACH(lcp, &gp->consumer, consumer) {
195 if ((lcp->index & MP_BAD) == 0) {
200 if (sc->cp_active == NULL) {
201 printf("GEOM_MULTIPATH: out of providers for %s\n",
206 printf("GEOM_MULTIPATH: %s now active path in %s\n",
207 sc->cp_active->provider->name, sc->sc_name);
213 * If we can fruitfully restart the I/O, do so.
218 g_multipath_start(pbp);
225 g_multipath_kt(void *arg)
228 g_multipath_kt_state = GKT_RUN;
229 mtx_lock(&gmtbq_mtx);
230 while (g_multipath_kt_state == GKT_RUN) {
234 bp = bioq_takefirst(&gmtbq);
237 mtx_unlock(&gmtbq_mtx);
238 g_multipath_done_error(bp);
239 mtx_lock(&gmtbq_mtx);
241 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
242 "gkt:wait", hz / 10);
244 mtx_unlock(&gmtbq_mtx);
245 wakeup(&g_multipath_kt_state);
251 g_multipath_access(struct g_provider *pp, int dr, int dw, int de)
254 struct g_consumer *cp, *badcp = NULL;
259 LIST_FOREACH(cp, &gp->consumer, consumer) {
260 error = g_access(cp, dr, dw, de);
269 LIST_FOREACH(cp, &gp->consumer, consumer) {
272 (void) g_access(cp, -dr, -dw, -de);
277 static struct g_geom *
278 g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md)
280 struct g_multipath_softc *sc;
282 struct g_provider *pp;
286 LIST_FOREACH(gp, &mp->geom, geom) {
287 if (strcmp(gp->name, md->md_name) == 0) {
288 printf("GEOM_MULTIPATH: name %s already exists\n",
294 gp = g_new_geomf(mp, md->md_name);
298 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
300 gp->start = g_multipath_start;
301 gp->orphan = g_multipath_orphan;
302 gp->access = g_multipath_access;
303 memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid));
304 memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name));
306 pp = g_new_providerf(gp, "multipath/%s", md->md_name);
309 /* limit the provider to not have it stomp on metadata */
310 pp->mediasize = md->md_size - md->md_sectorsize;
311 pp->sectorsize = md->md_sectorsize;
313 g_error_provider(pp, 0);
317 if (gp->softc != NULL)
325 g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp)
327 struct g_multipath_softc *sc;
328 struct g_consumer *cp, *nxtcp;
334 KASSERT(sc, ("no softc"));
337 * Make sure that the passed provider isn't already attached
339 LIST_FOREACH(cp, &gp->consumer, consumer) {
340 if (cp->provider == pp)
344 printf("GEOM_MULTIPATH: provider %s already attached to %s\n",
348 nxtcp = LIST_FIRST(&gp->consumer);
349 cp = g_new_consumer(gp);
352 error = g_attach(cp, pp);
354 printf("GEOM_MULTIPATH: cannot attach %s to %s",
355 pp->name, sc->sc_name);
356 g_destroy_consumer(cp);
363 * Set access permissions on new consumer to match other consumers
365 if (nxtcp && (nxtcp->acr + nxtcp->acw + nxtcp->ace)) {
366 error = g_access(cp, nxtcp->acr, nxtcp->acw, nxtcp->ace);
368 printf("GEOM_MULTIPATH: cannot set access in "
369 "attaching %s to %s/%s (%d)\n",
370 pp->name, sc->sc_name, sc->sc_uuid, error);
372 g_destroy_consumer(cp);
376 printf("GEOM_MULTIPATH: adding %s to %s/%s\n",
377 pp->name, sc->sc_name, sc->sc_uuid);
378 if (sc->cp_active == NULL) {
380 printf("GEOM_MULTIPATH: %s now active path in %s\n",
381 pp->name, sc->sc_name);
387 g_multipath_destroy(struct g_geom *gp)
389 struct g_provider *pp;
392 if (gp->softc == NULL)
394 pp = LIST_FIRST(&gp->provider);
395 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0))
397 printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
400 g_wither_geom(gp, ENXIO);
405 g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
409 return (g_multipath_destroy(gp));
413 g_multipath_rotate(struct g_geom *gp)
415 struct g_consumer *lcp;
416 struct g_multipath_softc *sc = gp->softc;
421 LIST_FOREACH(lcp, &gp->consumer, consumer) {
422 if ((lcp->index & MP_BAD) == 0) {
423 if (sc->cp_active != lcp) {
430 printf("GEOM_MULTIPATH: %s now active path in %s\n",
431 lcp->provider->name, sc->sc_name);
437 g_multipath_init(struct g_class *mp)
440 mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
441 if (kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt") == 0)
442 g_multipath_kt_state = GKT_RUN;
446 g_multipath_fini(struct g_class *mp)
448 if (g_multipath_kt_state == GKT_RUN) {
449 mtx_lock(&gmtbq_mtx);
450 g_multipath_kt_state = GKT_DIE;
451 wakeup(&g_multipath_kt_state);
452 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
454 mtx_unlock(&gmtbq_mtx);
459 g_multipath_read_metadata(struct g_consumer *cp,
460 struct g_multipath_metadata *md)
462 struct g_provider *pp;
467 error = g_access(cp, 1, 0, 0);
472 buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
473 pp->sectorsize, &error);
475 g_access(cp, -1, 0, 0);
478 multipath_metadata_decode(buf, md);
483 static struct g_geom *
484 g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
486 struct g_multipath_metadata md;
487 struct g_multipath_softc *sc;
488 struct g_consumer *cp;
489 struct g_geom *gp, *gp1;
494 gp = g_new_geomf(mp, "multipath:taste");
495 gp->start = g_multipath_start;
496 gp->access = g_multipath_access;
497 gp->orphan = g_multipath_orphan;
498 cp = g_new_consumer(gp);
500 error = g_multipath_read_metadata(cp, &md);
502 g_destroy_consumer(cp);
508 if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
509 if (g_multipath_debug)
510 printf("%s is not MULTIPATH\n", pp->name);
513 if (md.md_version != G_MULTIPATH_VERSION) {
514 printf("%s has version %d multipath id- this module is version "
515 " %d: rejecting\n", pp->name, md.md_version,
516 G_MULTIPATH_VERSION);
519 if (g_multipath_debug)
520 printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
523 * Let's check if such a device already is present. We check against
524 * uuid alone first because that's the true distinguishor. If that
525 * passes, then we check for name conflicts. If there are conflicts,
528 * The whole purpose of this is to solve the problem that people don't
529 * pick good unique names, but good unique names (like uuids) are a
530 * pain to use. So, we allow people to build GEOMs with friendly names
531 * and uuids, and modify the names in case there's a collision.
534 LIST_FOREACH(gp, &mp->geom, geom) {
538 if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
542 LIST_FOREACH(gp1, &mp->geom, geom) {
548 if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
553 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
555 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
556 * with the same name (but a different UUID).
558 * If gp is NULL, then modify the name with a random number and
559 * complain, but allow the creation of the geom to continue.
561 * If gp is *not* NULL, just use the geom's name as we're attaching
562 * this disk to the (previously generated) name.
569 u_long rand = random();
571 snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
572 printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
573 sc->sc_name, sc->sc_uuid);
574 printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
576 strlcpy(md.md_name, buf, sizeof(md.md_name));
578 strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
583 gp = g_multipath_create(mp, &md);
585 printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
586 md.md_name, md.md_uuid);
595 KASSERT(sc != NULL, ("sc is NULL"));
596 error = g_multipath_add_disk(gp, pp);
599 g_multipath_destroy(gp);
606 g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp)
609 struct g_consumer *cp;
610 struct g_provider *pp, *pp0;
611 const char *name, *mpname;
612 static const char devpf[6] = "/dev/";
616 mpname = gctl_get_asciiparam(req, "arg0");
617 if (mpname == NULL) {
618 gctl_error(req, "No 'arg0' argument");
621 gp = g_multipath_find_geom(mp, mpname);
623 gctl_error(req, "Device %s is invalid", mpname);
627 name = gctl_get_asciiparam(req, "arg1");
629 gctl_error(req, "No 'arg1' argument");
632 if (strncmp(name, devpf, 5) == 0)
634 pp = g_provider_by_name(name);
636 gctl_error(req, "Provider %s is invalid", name);
641 * Check to make sure parameters match, if we already have one.
643 cp = LIST_FIRST(&gp->consumer);
651 gctl_error(req, "providers %s and %s are the same",
652 pp0->name, pp->name);
655 if (pp0->mediasize != pp->mediasize) {
656 gctl_error(req, "Provider %s is %jd; Provider %s is %jd",
657 pp0->name, (intmax_t) pp0->mediasize,
658 pp->name, (intmax_t) pp->mediasize);
661 if (pp0->sectorsize != pp->sectorsize) {
662 gctl_error(req, "Provider %s has sectorsize %u; Provider %s "
663 "has sectorsize %u", pp0->name, pp0->sectorsize,
664 pp->name, pp->sectorsize);
672 (void) g_multipath_add_disk(gp, pp);
675 static struct g_geom *
676 g_multipath_find_geom(struct g_class *mp, const char *name)
680 LIST_FOREACH(gp, &mp->geom, geom) {
681 if (strcmp(gp->name, name) == 0) {
689 g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
697 name = gctl_get_asciiparam(req, "arg0");
699 gctl_error(req, "No 'arg0' argument");
702 gp = g_multipath_find_geom(mp, name);
704 gctl_error(req, "Device %s is invalid", name);
707 error = g_multipath_destroy(gp);
709 gctl_error(req, "failed to destroy %s (err=%d)", name, error);
714 g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp)
722 name = gctl_get_asciiparam(req, "arg0");
724 gctl_error(req, "No 'arg0' argument");
727 gp = g_multipath_find_geom(mp, name);
729 gctl_error(req, "Device %s is invalid", name);
732 error = g_multipath_rotate(gp);
734 gctl_error(req, "failed to rotate %s (err=%d)", name, error);
739 g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp)
743 struct g_multipath_softc *sc;
746 sb = sbuf_new_auto();
749 name = gctl_get_asciiparam(req, "arg0");
751 gctl_error(req, "No 'arg0' argument");
754 gp = g_multipath_find_geom(mp, name);
756 gctl_error(req, "Device %s is invalid", name);
761 sbuf_printf(sb, "%s\n", sc->cp_active->provider->name);
763 sbuf_printf(sb, "none\n");
766 gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
771 g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
775 version = gctl_get_paraml(req, "version", sizeof(*version));
776 if (version == NULL) {
777 gctl_error(req, "No 'version' argument");
778 } else if (*version != G_MULTIPATH_VERSION) {
779 gctl_error(req, "Userland and kernel parts are out of sync");
780 } else if (strcmp(verb, "add") == 0) {
781 g_multipath_ctl_add(req, mp);
782 } else if (strcmp(verb, "destroy") == 0) {
783 g_multipath_ctl_destroy(req, mp);
784 } else if (strcmp(verb, "rotate") == 0) {
785 g_multipath_ctl_rotate(req, mp);
786 } else if (strcmp(verb, "getactive") == 0) {
787 g_multipath_ctl_getactive(req, mp);
789 gctl_error(req, "Unknown verb %s", verb);
792 DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);