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 g_taste_t g_multipath_taste;
74 static g_ctl_req_t g_multipath_config;
75 static g_init_t g_multipath_init;
76 static g_fini_t g_multipath_fini;
78 struct g_class g_multipath_class = {
79 .name = G_MULTIPATH_CLASS_NAME,
81 .ctlreq = g_multipath_config,
82 .taste = g_multipath_taste,
83 .destroy_geom = g_multipath_destroy_geom,
84 .init = g_multipath_init,
85 .fini = g_multipath_fini
92 g_mpd(void *arg, int flags __unused)
94 struct g_consumer *cp;
98 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) {
99 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
102 printf("GEOM_MULTIPATH: %s removed from %s\n",
103 cp->provider->name, cp->geom->name);
106 g_destroy_consumer(cp);
110 g_multipath_orphan(struct g_consumer *cp)
112 if ((cp->index & MP_POSTED) == 0) {
113 cp->index |= MP_POSTED;
114 printf("GEOM_MULTIPATH: %s orphaned in %s\n",
115 cp->provider->name, cp->geom->name);
121 g_multipath_start(struct bio *bp)
123 struct g_multipath_softc *sc;
125 struct g_consumer *cp;
128 gp = bp->bio_to->geom;
130 KASSERT(sc != NULL, ("NULL sc"));
133 g_io_deliver(bp, ENXIO);
136 cbp = g_clone_bio(bp);
138 g_io_deliver(bp, ENOMEM);
141 cbp->bio_done = g_multipath_done;
142 g_io_request(cbp, cp);
146 g_multipath_done(struct bio *bp)
148 if (bp->bio_error == ENXIO || bp->bio_error == EIO) {
149 mtx_lock(&gmtbq_mtx);
150 bioq_insert_tail(&gmtbq, bp);
151 wakeup(&g_multipath_kt_state);
152 mtx_unlock(&gmtbq_mtx);
159 g_multipath_done_error(struct bio *bp)
163 struct g_multipath_softc *sc;
164 struct g_consumer *cp;
165 struct g_provider *pp;
168 * If we had a failure, we have to check first to see
169 * whether the consumer it failed on was the currently
170 * active consumer (i.e., this is the first in perhaps
171 * a number of failures). If so, we then switch consumers
172 * to the next available consumer.
176 pbp = bp->bio_parent;
177 gp = pbp->bio_to->geom;
183 if (cp->nend == cp->nstart && pp->nend == pp->nstart) {
184 cp->index |= MP_POSTED;
185 g_post_event(g_mpd, cp, M_NOWAIT, NULL);
187 if (cp == sc->cp_active) {
188 struct g_consumer *lcp;
189 printf("GEOM_MULTIPATH: %s failed in %s\n",
190 pp->name, sc->sc_name);
191 sc->cp_active = NULL;
192 LIST_FOREACH(lcp, &gp->consumer, consumer) {
193 if ((lcp->index & MP_BAD) == 0) {
198 if (sc->cp_active == NULL) {
199 printf("GEOM_MULTIPATH: out of providers for %s\n",
203 printf("GEOM_MULTIPATH: %s now active path in %s\n",
204 sc->cp_active->provider->name, sc->sc_name);
210 * If we can fruitfully restart the I/O, do so.
215 g_multipath_start(pbp);
222 g_multipath_kt(void *arg)
224 g_multipath_kt_state = GKT_RUN;
225 mtx_lock(&gmtbq_mtx);
226 while (g_multipath_kt_state == GKT_RUN) {
229 bp = bioq_takefirst(&gmtbq);
233 mtx_unlock(&gmtbq_mtx);
234 g_multipath_done_error(bp);
235 mtx_lock(&gmtbq_mtx);
237 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
238 "gkt:wait", hz / 10);
240 mtx_unlock(&gmtbq_mtx);
241 wakeup(&g_multipath_kt_state);
247 g_multipath_access(struct g_provider *pp, int dr, int dw, int de)
250 struct g_consumer *cp, *badcp = NULL;
255 LIST_FOREACH(cp, &gp->consumer, consumer) {
256 error = g_access(cp, dr, dw, de);
265 LIST_FOREACH(cp, &gp->consumer, consumer) {
269 (void) g_access(cp, -dr, -dw, -de);
274 static struct g_geom *
275 g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md)
277 struct g_multipath_softc *sc;
279 struct g_provider *pp;
283 LIST_FOREACH(gp, &mp->geom, geom) {
284 if (strcmp(gp->name, md->md_name) == 0) {
285 printf("GEOM_MULTIPATH: name %s already exists\n",
291 gp = g_new_geomf(mp, md->md_name);
296 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
302 gp->start = g_multipath_start;
303 gp->orphan = g_multipath_orphan;
304 gp->access = g_multipath_access;
305 memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid));
306 memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name));
308 pp = g_new_providerf(gp, "multipath/%s", md->md_name);
312 /* limit the provider to not have it stomp on metadata */
313 pp->mediasize = md->md_size - md->md_sectorsize;
314 pp->sectorsize = md->md_sectorsize;
316 g_error_provider(pp, 0);
320 if (gp->softc != NULL) {
329 g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp)
331 struct g_multipath_softc *sc;
332 struct g_consumer *cp, *nxtcp;
338 KASSERT(sc, ("no softc"));
341 * Make sure that the passed provider isn't already attached
343 LIST_FOREACH(cp, &gp->consumer, consumer) {
344 if (cp->provider == pp) {
349 printf("GEOM_MULTIPATH: provider %s already attached to %s\n",
353 nxtcp = LIST_FIRST(&gp->consumer);
354 cp = g_new_consumer(gp);
358 error = g_attach(cp, pp);
360 printf("GEOM_MULTIPATH: cannot attach %s to %s",
361 pp->name, sc->sc_name);
362 g_destroy_consumer(cp);
369 * Set access permissions on new consumer to match other consumers
371 if (nxtcp && (nxtcp->acr + nxtcp->acw + nxtcp->ace)) {
372 error = g_access(cp, nxtcp->acr, nxtcp->acw, nxtcp->ace);
374 printf("GEOM_MULTIPATH: cannot set access in "
375 "attaching %s to %s/%s (%d)\n",
376 pp->name, sc->sc_name, sc->sc_uuid, error);
378 g_destroy_consumer(cp);
382 printf("GEOM_MULTIPATH: adding %s to %s/%s\n",
383 pp->name, sc->sc_name, sc->sc_uuid);
384 if (sc->cp_active == NULL) {
386 printf("GEOM_MULTIPATH: %s now active path in %s\n",
387 pp->name, sc->sc_name);
393 g_multipath_destroy(struct g_geom *gp)
395 struct g_provider *pp;
398 if (gp->softc == NULL) {
401 pp = LIST_FIRST(&gp->provider);
402 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
405 printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
408 g_wither_geom(gp, ENXIO);
413 g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
416 return (g_multipath_destroy(gp));
420 g_multipath_init(struct g_class *mp)
423 mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
424 if (kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt") == 0) {
425 g_multipath_kt_state = GKT_RUN;
430 g_multipath_fini(struct g_class *mp)
432 if (g_multipath_kt_state == GKT_RUN) {
433 mtx_lock(&gmtbq_mtx);
434 g_multipath_kt_state = GKT_DIE;
435 wakeup(&g_multipath_kt_state);
436 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
438 mtx_unlock(&gmtbq_mtx);
443 g_multipath_read_metadata(struct g_consumer *cp,
444 struct g_multipath_metadata *md)
446 struct g_provider *pp;
451 error = g_access(cp, 1, 0, 0);
457 buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
458 pp->sectorsize, &error);
460 g_access(cp, -1, 0, 0);
464 multipath_metadata_decode(buf, md);
469 static struct g_geom *
470 g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
472 struct g_multipath_metadata md;
473 struct g_multipath_softc *sc;
474 struct g_consumer *cp;
475 struct g_geom *gp, *gp1;
480 gp = g_new_geomf(mp, "multipath:taste");
481 gp->start = g_multipath_start;
482 gp->access = g_multipath_access;
483 gp->orphan = g_multipath_orphan;
484 cp = g_new_consumer(gp);
486 error = g_multipath_read_metadata(cp, &md);
488 g_destroy_consumer(cp);
495 if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
496 if (g_multipath_debug) {
497 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);
512 * Let's check if such a device already is present. We check against
513 * uuid alone first because that's the true distinguishor. If that
514 * passes, then we check for name conflicts. If there are conflicts,
517 * The whole purpose of this is to solve the problem that people don't
518 * pick good unique names, but good unique names (like uuids) are a
519 * pain to use. So, we allow people to build GEOMs with friendly names
520 * and uuids, and modify the names in case there's a collision.
523 LIST_FOREACH(gp, &mp->geom, geom) {
528 if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0) {
533 LIST_FOREACH(gp1, &mp->geom, geom) {
541 if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0) {
547 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
549 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
550 * with the same name (but a different UUID).
552 * If gp is NULL, then modify the name with a random number and
553 * complain, but allow the creation of the geom to continue.
555 * If gp is *not* NULL, just use the geom's name as we're attaching
556 * this disk to the (previously generated) name.
563 u_long rand = random();
565 snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
566 printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
567 sc->sc_name, sc->sc_uuid);
568 printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
570 strlcpy(md.md_name, buf, sizeof (md.md_name));
572 strlcpy(md.md_name, sc->sc_name, sizeof (md.md_name));
577 gp = g_multipath_create(mp, &md);
579 printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
580 md.md_name, md.md_uuid);
589 KASSERT(sc != NULL, ("sc is NULL"));
590 error = g_multipath_add_disk(gp, pp);
593 g_multipath_destroy(gp);
601 g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp)
604 struct g_provider *pp0, *pp1;
605 struct g_multipath_metadata md;
606 const char *name, *mpname, *uuid;
607 static const char devpf[6] = "/dev/";
612 mpname = gctl_get_asciiparam(req, "arg0");
613 if (mpname == NULL) {
614 gctl_error(req, "No 'arg0' argument");
618 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
620 gctl_error(req, "No 'nargs' argument");
624 gctl_error(req, "missing device or uuid arguments");
628 name = gctl_get_asciiparam(req, "arg1");
630 gctl_error(req, "No 'arg1' argument");
633 if (strncmp(name, devpf, 5) == 0) {
636 pp0 = g_provider_by_name(name);
638 gctl_error(req, "Provider %s is invalid", name);
642 name = gctl_get_asciiparam(req, "arg2");
644 gctl_error(req, "No 'arg2' argument");
647 if (strncmp(name, devpf, 5) == 0) {
650 pp1 = g_provider_by_name(name);
652 gctl_error(req, "Provider %s is invalid", name);
656 uuid = gctl_get_asciiparam(req, "arg3");
658 gctl_error(req, "No uuid argument");
661 if (strlen(uuid) != 36) {
662 gctl_error(req, "Malformed uuid argument");
667 * Check to make sure parameters from the two providers are the same
670 gctl_error(req, "providers %s and %s are the same",
671 pp0->name, pp1->name);
674 if (pp0->mediasize != pp1->mediasize) {
675 gctl_error(req, "Provider %s is %jd; Provider %s is %jd",
676 pp0->name, (intmax_t) pp0->mediasize,
677 pp1->name, (intmax_t) pp1->mediasize);
680 if (pp0->sectorsize != pp1->sectorsize) {
681 gctl_error(req, "Provider %s has sectorsize %u; Provider %s "
682 "has sectorsize %u", pp0->name, pp0->sectorsize,
683 pp1->name, pp1->sectorsize);
688 * cons up enough of a metadata structure to use.
690 memset(&md, 0, sizeof(md));
691 md.md_size = pp0->mediasize;
692 md.md_sectorsize = pp0->sectorsize;
693 strncpy(md.md_name, mpname, sizeof (md.md_name));
694 strncpy(md.md_uuid, uuid, sizeof (md.md_uuid));
696 gp = g_multipath_create(mp, &md);
700 error = g_multipath_add_disk(gp, pp0);
702 g_multipath_destroy(gp);
705 error = g_multipath_add_disk(gp, pp1);
707 g_multipath_destroy(gp);
712 static struct g_geom *
713 g_multipath_find_geom(struct g_class *mp, const char *name)
717 LIST_FOREACH(gp, &mp->geom, geom) {
718 if (strcmp(gp->name, name) == 0) {
726 g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
734 name = gctl_get_asciiparam(req, "arg0");
736 gctl_error(req, "No 'arg0' argument");
739 gp = g_multipath_find_geom(mp, name);
741 gctl_error(req, "Device %s is invalid", name);
744 error = g_multipath_destroy(gp);
746 gctl_error(req, "failed to destroy %s (err=%d)", name, error);
751 g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
755 version = gctl_get_paraml(req, "version", sizeof(*version));
756 if (version == NULL) {
757 gctl_error(req, "No 'version' argument");
758 } else if (*version != G_MULTIPATH_VERSION) {
759 gctl_error(req, "Userland and kernel parts are out of sync");
760 } else if (strcmp(verb, "create") == 0) {
761 g_multipath_ctl_create(req, mp);
762 } else if (strcmp(verb, "destroy") == 0) {
763 g_multipath_ctl_destroy(req, mp);
765 gctl_error(req, "Unknown verb %s", verb);
768 DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);