2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011-2013 Alexander Motin <mav@FreeBSD.org>
5 * Copyright (c) 2006-2007 Matthew Jacob <mjacob@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Based upon work by Pawel Jakub Dawidek <pjd@FreeBSD.org> for all of the
31 * fine geom examples, and by Poul Henning Kamp <phk@FreeBSD.org> for GEOM
32 * itself, all of which is most gratefully acknowledged.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/limits.h>
43 #include <sys/mutex.h>
47 #include <sys/sysctl.h>
48 #include <sys/kthread.h>
49 #include <sys/malloc.h>
50 #include <geom/geom.h>
51 #include <geom/multipath/g_multipath.h>
53 FEATURE(geom_multipath, "GEOM multipath support");
55 SYSCTL_DECL(_kern_geom);
56 static SYSCTL_NODE(_kern_geom, OID_AUTO, multipath,
57 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
58 "GEOM_MULTIPATH tunables");
59 static u_int g_multipath_debug = 0;
60 SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, debug, CTLFLAG_RW,
61 &g_multipath_debug, 0, "Debug level");
62 static u_int g_multipath_exclusive = 1;
63 SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, exclusive, CTLFLAG_RW,
64 &g_multipath_exclusive, 0, "Exclusively open providers");
66 SDT_PROVIDER_DECLARE(geom);
67 SDT_PROBE_DEFINE2(geom, multipath, config, restore, "char*", "char*");
68 SDT_PROBE_DEFINE2(geom, multipath, config, remove, "char*", "char*");
69 SDT_PROBE_DEFINE2(geom, multipath, config, disconnect, "char*", "char*");
70 SDT_PROBE_DEFINE3(geom, multipath, config, fail, "char*", "char*", "int");
71 SDT_PROBE_DEFINE2(geom, multipath, config, taste, "char*", "char*");
72 SDT_PROBE_DEFINE2(geom, multipath, io, restart, "struct bio*", "struct bio*");
78 } g_multipath_kt_state;
79 static struct bio_queue_head gmtbq;
80 static struct mtx gmtbq_mtx;
82 static int g_multipath_read_metadata(struct g_consumer *cp,
83 struct g_multipath_metadata *md);
84 static int g_multipath_write_metadata(struct g_consumer *cp,
85 struct g_multipath_metadata *md);
87 static void g_multipath_orphan(struct g_consumer *);
88 static void g_multipath_resize(struct g_consumer *);
89 static void g_multipath_start(struct bio *);
90 static void g_multipath_done(struct bio *);
91 static void g_multipath_done_error(struct bio *);
92 static void g_multipath_kt(void *);
94 static int g_multipath_destroy(struct g_geom *);
96 g_multipath_destroy_geom(struct gctl_req *, struct g_class *, struct g_geom *);
98 static struct g_geom *g_multipath_find_geom(struct g_class *, const char *);
99 static int g_multipath_rotate(struct g_geom *);
101 static g_taste_t g_multipath_taste;
102 static g_ctl_req_t g_multipath_config;
103 static g_init_t g_multipath_init;
104 static g_fini_t g_multipath_fini;
105 static g_dumpconf_t g_multipath_dumpconf;
107 struct g_class g_multipath_class = {
108 .name = G_MULTIPATH_CLASS_NAME,
109 .version = G_VERSION,
110 .ctlreq = g_multipath_config,
111 .taste = g_multipath_taste,
112 .destroy_geom = g_multipath_destroy_geom,
113 .init = g_multipath_init,
114 .fini = g_multipath_fini
117 #define MP_FAIL 0x00000001
118 #define MP_LOST 0x00000002
119 #define MP_NEW 0x00000004
120 #define MP_POSTED 0x00000008
121 #define MP_BAD (MP_FAIL | MP_LOST | MP_NEW)
122 #define MP_WITHER 0x00000010
123 #define MP_IDLE 0x00000020
124 #define MP_IDLE_MASK 0xffffffe0
127 g_multipath_good(struct g_geom *gp)
129 struct g_consumer *cp;
132 LIST_FOREACH(cp, &gp->consumer, consumer) {
133 if ((cp->index & MP_BAD) == 0)
140 g_multipath_fault(struct g_consumer *cp, int cause)
142 struct g_multipath_softc *sc;
143 struct g_consumer *lcp;
149 if (g_multipath_good(gp) == 0 && sc->sc_ndisks > 0) {
150 LIST_FOREACH(lcp, &gp->consumer, consumer) {
151 if (lcp->provider == NULL ||
152 (lcp->index & (MP_LOST | MP_NEW)))
154 if (sc->sc_ndisks > 1 && lcp == cp)
156 printf("GEOM_MULTIPATH: "
157 "all paths in %s were marked FAIL, restore %s\n",
158 sc->sc_name, lcp->provider->name);
159 SDT_PROBE2(geom, multipath, config, restore,
160 sc->sc_name, lcp->provider->name);
161 lcp->index &= ~MP_FAIL;
164 if (cp != sc->sc_active)
166 sc->sc_active = NULL;
167 LIST_FOREACH(lcp, &gp->consumer, consumer) {
168 if ((lcp->index & MP_BAD) == 0) {
173 if (sc->sc_active == NULL) {
174 printf("GEOM_MULTIPATH: out of providers for %s\n",
176 } else if (sc->sc_active_active != 1) {
177 printf("GEOM_MULTIPATH: %s is now active path in %s\n",
178 sc->sc_active->provider->name, sc->sc_name);
182 static struct g_consumer *
183 g_multipath_choose(struct g_geom *gp, struct bio *bp)
185 struct g_multipath_softc *sc;
186 struct g_consumer *best, *cp;
189 if (sc->sc_active_active == 0 ||
190 (sc->sc_active_active == 2 && bp->bio_cmd != BIO_READ))
191 return (sc->sc_active);
193 LIST_FOREACH(cp, &gp->consumer, consumer) {
194 if (cp->index & MP_BAD)
196 cp->index += MP_IDLE;
197 if (best == NULL || cp->private < best->private ||
198 (cp->private == best->private && cp->index > best->index))
202 best->index &= ~MP_IDLE_MASK;
207 g_mpd(void *arg, int flags __unused)
210 struct g_multipath_softc *sc;
211 struct g_consumer *cp;
217 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) {
219 g_access(cp, -cp->acr, -cp->acw, -cp->ace);
220 if (w > 0 && cp->provider != NULL &&
221 (cp->provider->geom->flags & G_GEOM_WITHER) == 0) {
222 cp->index |= MP_WITHER;
223 g_post_event(g_mpd, cp, M_WAITOK, NULL);
228 mtx_lock(&sc->sc_mtx);
230 printf("GEOM_MULTIPATH: %s removed from %s\n",
231 cp->provider->name, gp->name);
232 SDT_PROBE2(geom, multipath, config, remove,
233 gp->name, cp->provider->name);
236 g_destroy_consumer(cp);
237 mtx_unlock(&sc->sc_mtx);
238 if (LIST_EMPTY(&gp->consumer))
239 g_multipath_destroy(gp);
243 g_multipath_orphan(struct g_consumer *cp)
245 struct g_multipath_softc *sc;
249 printf("GEOM_MULTIPATH: %s in %s was disconnected\n",
250 cp->provider->name, cp->geom->name);
251 SDT_PROBE2(geom, multipath, config, disconnect,
252 cp->geom->name, cp->provider->name);
253 sc = cp->geom->softc;
254 cnt = (uintptr_t *)&cp->private;
255 mtx_lock(&sc->sc_mtx);
257 g_multipath_fault(cp, MP_LOST);
258 if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
259 cp->index |= MP_POSTED;
260 mtx_unlock(&sc->sc_mtx);
263 mtx_unlock(&sc->sc_mtx);
267 g_multipath_resize(struct g_consumer *cp)
269 struct g_multipath_softc *sc;
271 struct g_consumer *cp1;
272 struct g_provider *pp;
273 struct g_multipath_metadata md;
274 off_t size, psize, ssize;
286 if (pp->mediasize < sc->sc_size) {
287 size = pp->mediasize;
288 ssize = pp->sectorsize;
290 size = ssize = OFF_MAX;
291 mtx_lock(&sc->sc_mtx);
292 LIST_FOREACH(cp1, &gp->consumer, consumer) {
296 if (pp->mediasize < size) {
297 size = pp->mediasize;
298 ssize = pp->sectorsize;
301 mtx_unlock(&sc->sc_mtx);
302 if (size == OFF_MAX || size == sc->sc_size)
305 psize = size - ((sc->sc_uuid[0] != 0) ? ssize : 0);
306 printf("GEOM_MULTIPATH: %s size changed from %jd to %jd\n",
307 sc->sc_name, sc->sc_pp->mediasize, psize);
308 if (sc->sc_uuid[0] != 0 && size < sc->sc_size) {
309 error = g_multipath_read_metadata(cp, &md);
311 (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) ||
312 (memcmp(md.md_uuid, sc->sc_uuid, sizeof(sc->sc_uuid)) != 0) ||
313 (strcmp(md.md_name, sc->sc_name) != 0) ||
314 (md.md_size != 0 && md.md_size != size) ||
315 (md.md_sectorsize != 0 && md.md_sectorsize != ssize)) {
316 g_multipath_destroy(gp);
321 g_resize_provider(sc->sc_pp, psize);
323 if (sc->sc_uuid[0] != 0) {
325 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
326 memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
327 strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
328 md.md_version = G_MULTIPATH_VERSION;
330 md.md_sectorsize = ssize;
331 md.md_active_active = sc->sc_active_active;
332 error = g_multipath_write_metadata(cp, &md);
334 printf("GEOM_MULTIPATH: Can't update metadata on %s "
335 "(%d)\n", pp->name, error);
340 g_multipath_start(struct bio *bp)
342 struct g_multipath_softc *sc;
344 struct g_consumer *cp;
348 gp = bp->bio_to->geom;
350 KASSERT(sc != NULL, ("NULL sc"));
351 cbp = g_clone_bio(bp);
353 g_io_deliver(bp, ENOMEM);
356 mtx_lock(&sc->sc_mtx);
357 cp = g_multipath_choose(gp, bp);
359 mtx_unlock(&sc->sc_mtx);
361 g_io_deliver(bp, ENXIO);
364 if ((uintptr_t)bp->bio_driver1 < sc->sc_ndisks)
365 bp->bio_driver1 = (void *)(uintptr_t)sc->sc_ndisks;
366 cnt = (uintptr_t *)&cp->private;
368 mtx_unlock(&sc->sc_mtx);
369 cbp->bio_done = g_multipath_done;
370 g_io_request(cbp, cp);
374 g_multipath_done(struct bio *bp)
376 struct g_multipath_softc *sc;
377 struct g_consumer *cp;
380 if (bp->bio_error == ENXIO || bp->bio_error == EIO) {
381 mtx_lock(&gmtbq_mtx);
382 bioq_insert_tail(&gmtbq, bp);
383 mtx_unlock(&gmtbq_mtx);
384 wakeup(&g_multipath_kt_state);
387 sc = cp->geom->softc;
388 cnt = (uintptr_t *)&cp->private;
389 mtx_lock(&sc->sc_mtx);
391 if (*cnt == 0 && (cp->index & MP_LOST)) {
392 if (g_post_event(g_mpd, cp, M_NOWAIT, NULL) == 0)
393 cp->index |= MP_POSTED;
394 mtx_unlock(&sc->sc_mtx);
396 mtx_unlock(&sc->sc_mtx);
402 g_multipath_done_error(struct bio *bp)
406 struct g_multipath_softc *sc;
407 struct g_consumer *cp;
408 struct g_provider *pp;
412 * If we had a failure, we have to check first to see
413 * whether the consumer it failed on was the currently
414 * active consumer (i.e., this is the first in perhaps
415 * a number of failures). If so, we then switch consumers
416 * to the next available consumer.
419 pbp = bp->bio_parent;
420 gp = pbp->bio_to->geom;
424 cnt = (uintptr_t *)&cp->private;
426 mtx_lock(&sc->sc_mtx);
427 if ((cp->index & MP_FAIL) == 0) {
428 printf("GEOM_MULTIPATH: Error %d, %s in %s marked FAIL\n",
429 bp->bio_error, pp->name, sc->sc_name);
430 SDT_PROBE3(geom, multipath, config, fail,
431 sc->sc_name, pp->name, bp->bio_error);
432 g_multipath_fault(cp, MP_FAIL);
435 if (*cnt == 0 && (cp->index & (MP_LOST | MP_POSTED)) == MP_LOST) {
436 cp->index |= MP_POSTED;
437 mtx_unlock(&sc->sc_mtx);
438 g_post_event(g_mpd, cp, M_WAITOK, NULL);
440 mtx_unlock(&sc->sc_mtx);
443 * If we can fruitfully restart the I/O, do so.
445 if (pbp->bio_children < (uintptr_t)pbp->bio_driver1) {
447 SDT_PROBE2(geom, multipath, io, restart, bp, pbp);
449 g_multipath_start(pbp);
456 g_multipath_kt(void *arg)
459 g_multipath_kt_state = GKT_RUN;
460 mtx_lock(&gmtbq_mtx);
461 while (g_multipath_kt_state == GKT_RUN) {
465 bp = bioq_takefirst(&gmtbq);
468 mtx_unlock(&gmtbq_mtx);
469 g_multipath_done_error(bp);
470 mtx_lock(&gmtbq_mtx);
472 if (g_multipath_kt_state != GKT_RUN)
474 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
477 mtx_unlock(&gmtbq_mtx);
478 wakeup(&g_multipath_kt_state);
483 g_multipath_access(struct g_provider *pp, int dr, int dw, int de)
486 struct g_consumer *cp, *badcp = NULL;
487 struct g_multipath_softc *sc;
492 /* Error used if we have no valid consumers. */
493 error = (dr > 0 || dw > 0 || de > 0) ? ENXIO : 0;
495 LIST_FOREACH(cp, &gp->consumer, consumer) {
496 if (cp->index & MP_WITHER)
499 error = g_access(cp, dr, dw, de);
510 sc->sc_opened += dr + dw + de;
511 if (sc->sc_stopping && sc->sc_opened == 0)
512 g_multipath_destroy(gp);
517 LIST_FOREACH(cp, &gp->consumer, consumer) {
520 if (cp->index & MP_WITHER)
523 (void) g_access(cp, -dr, -dw, -de);
528 static struct g_geom *
529 g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md)
531 struct g_multipath_softc *sc;
533 struct g_provider *pp;
537 LIST_FOREACH(gp, &mp->geom, geom) {
539 if (sc == NULL || sc->sc_stopping)
541 if (strcmp(gp->name, md->md_name) == 0) {
542 printf("GEOM_MULTIPATH: name %s already exists\n",
548 gp = g_new_geomf(mp, "%s", md->md_name);
549 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
550 mtx_init(&sc->sc_mtx, "multipath", NULL, MTX_DEF);
551 memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid));
552 memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name));
553 sc->sc_active_active = md->md_active_active;
554 sc->sc_size = md->md_size;
556 gp->start = g_multipath_start;
557 gp->orphan = g_multipath_orphan;
558 gp->resize = g_multipath_resize;
559 gp->access = g_multipath_access;
560 gp->dumpconf = g_multipath_dumpconf;
562 pp = g_new_providerf(gp, "multipath/%s", md->md_name);
563 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
564 if (md->md_size != 0) {
565 pp->mediasize = md->md_size -
566 ((md->md_uuid[0] != 0) ? md->md_sectorsize : 0);
567 pp->sectorsize = md->md_sectorsize;
570 g_error_provider(pp, 0);
571 printf("GEOM_MULTIPATH: %s created\n", gp->name);
576 g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp)
578 struct g_multipath_softc *sc;
579 struct g_consumer *cp, *nxtcp;
580 int error, acr, acw, ace;
585 KASSERT(sc, ("no softc"));
588 * Make sure that the passed provider isn't already attached
590 LIST_FOREACH(cp, &gp->consumer, consumer) {
591 if (cp->provider == pp)
595 printf("GEOM_MULTIPATH: provider %s already attached to %s\n",
599 nxtcp = LIST_FIRST(&gp->consumer);
600 cp = g_new_consumer(gp);
601 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
604 error = g_attach(cp, pp);
606 printf("GEOM_MULTIPATH: cannot attach %s to %s",
607 pp->name, sc->sc_name);
608 g_destroy_consumer(cp);
613 * Set access permissions on new consumer to match other consumers
616 acr = sc->sc_pp->acr;
617 acw = sc->sc_pp->acw;
618 ace = sc->sc_pp->ace;
621 if (g_multipath_exclusive) {
626 error = g_access(cp, acr, acw, ace);
628 printf("GEOM_MULTIPATH: cannot set access in "
629 "attaching %s to %s (%d)\n",
630 pp->name, sc->sc_name, error);
632 g_destroy_consumer(cp);
635 if (sc->sc_size == 0) {
636 sc->sc_size = pp->mediasize -
637 ((sc->sc_uuid[0] != 0) ? pp->sectorsize : 0);
638 sc->sc_pp->mediasize = sc->sc_size;
639 sc->sc_pp->sectorsize = pp->sectorsize;
641 if (sc->sc_pp->stripesize == 0 && sc->sc_pp->stripeoffset == 0) {
642 sc->sc_pp->stripesize = pp->stripesize;
643 sc->sc_pp->stripeoffset = pp->stripeoffset;
645 sc->sc_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
646 mtx_lock(&sc->sc_mtx);
649 mtx_unlock(&sc->sc_mtx);
650 printf("GEOM_MULTIPATH: %s added to %s\n",
651 pp->name, sc->sc_name);
652 if (sc->sc_active == NULL) {
654 if (sc->sc_active_active != 1)
655 printf("GEOM_MULTIPATH: %s is now active path in %s\n",
656 pp->name, sc->sc_name);
662 g_multipath_destroy(struct g_geom *gp)
664 struct g_multipath_softc *sc;
665 struct g_consumer *cp, *cp1;
668 if (gp->softc == NULL)
671 if (!sc->sc_stopping) {
672 printf("GEOM_MULTIPATH: destroying %s\n", gp->name);
675 if (sc->sc_opened != 0) {
676 g_wither_provider(sc->sc_pp, ENXIO);
678 return (EINPROGRESS);
680 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
681 mtx_lock(&sc->sc_mtx);
682 if ((cp->index & MP_POSTED) == 0) {
683 cp->index |= MP_POSTED;
684 mtx_unlock(&sc->sc_mtx);
687 return(0); /* Recursion happened. */
689 mtx_unlock(&sc->sc_mtx);
691 if (!LIST_EMPTY(&gp->consumer))
692 return (EINPROGRESS);
693 mtx_destroy(&sc->sc_mtx);
696 printf("GEOM_MULTIPATH: %s destroyed\n", gp->name);
697 g_wither_geom(gp, ENXIO);
702 g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp,
706 return (g_multipath_destroy(gp));
710 g_multipath_rotate(struct g_geom *gp)
712 struct g_consumer *lcp, *first_good_cp = NULL;
713 struct g_multipath_softc *sc = gp->softc;
714 int active_cp_seen = 0;
719 LIST_FOREACH(lcp, &gp->consumer, consumer) {
720 if ((lcp->index & MP_BAD) == 0) {
721 if (first_good_cp == NULL)
726 if (sc->sc_active == lcp)
731 if (lcp && lcp != sc->sc_active) {
733 if (sc->sc_active_active != 1)
734 printf("GEOM_MULTIPATH: %s is now active path in %s\n",
735 lcp->provider->name, sc->sc_name);
741 g_multipath_init(struct g_class *mp)
744 mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF);
745 kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt");
749 g_multipath_fini(struct g_class *mp)
751 if (g_multipath_kt_state == GKT_RUN) {
752 mtx_lock(&gmtbq_mtx);
753 g_multipath_kt_state = GKT_DIE;
754 wakeup(&g_multipath_kt_state);
755 msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO,
757 mtx_unlock(&gmtbq_mtx);
762 g_multipath_read_metadata(struct g_consumer *cp,
763 struct g_multipath_metadata *md)
765 struct g_provider *pp;
770 error = g_access(cp, 1, 0, 0);
775 buf = g_read_data(cp, pp->mediasize - pp->sectorsize,
776 pp->sectorsize, &error);
778 g_access(cp, -1, 0, 0);
781 multipath_metadata_decode(buf, md);
787 g_multipath_write_metadata(struct g_consumer *cp,
788 struct g_multipath_metadata *md)
790 struct g_provider *pp;
795 error = g_access(cp, 1, 1, 1);
800 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
801 multipath_metadata_encode(md, buf);
802 error = g_write_data(cp, pp->mediasize - pp->sectorsize,
803 buf, pp->sectorsize);
805 g_access(cp, -1, -1, -1);
810 static struct g_geom *
811 g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused)
813 struct g_multipath_metadata md;
814 struct g_multipath_softc *sc;
815 struct g_consumer *cp;
816 struct g_geom *gp, *gp1;
821 gp = g_new_geomf(mp, "multipath:taste");
822 gp->start = g_multipath_start;
823 gp->access = g_multipath_access;
824 gp->orphan = g_multipath_orphan;
825 cp = g_new_consumer(gp);
826 error = g_attach(cp, pp);
828 error = g_multipath_read_metadata(cp, &md);
831 g_destroy_consumer(cp);
837 if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) {
838 if (g_multipath_debug)
839 printf("%s is not MULTIPATH\n", pp->name);
842 if (md.md_version != G_MULTIPATH_VERSION) {
843 printf("%s has version %d multipath id- this module is version "
844 " %d: rejecting\n", pp->name, md.md_version,
845 G_MULTIPATH_VERSION);
848 if (md.md_size != 0 && md.md_size != pp->mediasize)
850 if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize)
852 if (g_multipath_debug)
853 printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid);
854 SDT_PROBE2(geom, multipath, config, taste, md.md_name, md.md_uuid);
857 * Let's check if such a device already is present. We check against
858 * uuid alone first because that's the true distinguishor. If that
859 * passes, then we check for name conflicts. If there are conflicts,
862 * The whole purpose of this is to solve the problem that people don't
863 * pick good unique names, but good unique names (like uuids) are a
864 * pain to use. So, we allow people to build GEOMs with friendly names
865 * and uuids, and modify the names in case there's a collision.
868 LIST_FOREACH(gp, &mp->geom, geom) {
870 if (sc == NULL || sc->sc_stopping)
872 if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0)
876 LIST_FOREACH(gp1, &mp->geom, geom) {
880 if (sc == NULL || sc->sc_stopping)
882 if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0)
887 * If gp is NULL, we had no extant MULTIPATH geom with this uuid.
889 * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant
890 * with the same name (but a different UUID).
892 * If gp is NULL, then modify the name with a random number and
893 * complain, but allow the creation of the geom to continue.
895 * If gp is *not* NULL, just use the geom's name as we're attaching
896 * this disk to the (previously generated) name.
903 u_long rand = random();
905 snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand);
906 printf("GEOM_MULTIPATH: geom %s/%s exists already\n",
907 sc->sc_name, sc->sc_uuid);
908 printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n",
910 strlcpy(md.md_name, buf, sizeof(md.md_name));
912 strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name));
917 gp = g_multipath_create(mp, &md);
919 printf("GEOM_MULTIPATH: cannot create geom %s/%s\n",
920 md.md_name, md.md_uuid);
929 KASSERT(sc != NULL, ("sc is NULL"));
930 error = g_multipath_add_disk(gp, pp);
933 g_multipath_destroy(gp);
940 g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp,
943 struct g_multipath_softc *sc;
945 struct g_consumer *cp;
946 struct g_provider *pp;
948 static const char devpf[6] = _PATH_DEV;
953 mpname = gctl_get_asciiparam(req, "arg0");
954 if (mpname == NULL) {
955 gctl_error(req, "No 'arg0' argument");
958 gp = g_multipath_find_geom(mp, mpname);
960 gctl_error(req, "Device %s is invalid", mpname);
965 if (strncmp(name, devpf, 5) == 0)
967 pp = g_provider_by_name(name);
969 gctl_error(req, "Provider %s is invalid", name);
974 * Check to make sure parameters match.
976 LIST_FOREACH(cp, &gp->consumer, consumer) {
977 if (cp->provider == pp) {
978 gctl_error(req, "provider %s is already there",
983 if (sc->sc_pp->mediasize != 0 &&
984 sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0)
986 gctl_error(req, "Providers size mismatch %jd != %jd",
987 (intmax_t) sc->sc_pp->mediasize +
988 (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0),
989 (intmax_t) pp->mediasize);
992 if (sc->sc_pp->sectorsize != 0 &&
993 sc->sc_pp->sectorsize != pp->sectorsize) {
994 gctl_error(req, "Providers sectorsize mismatch %u != %u",
995 sc->sc_pp->sectorsize, pp->sectorsize);
999 error = g_multipath_add_disk(gp, pp);
1001 gctl_error(req, "Provider addition error: %d", error);
1005 g_multipath_ctl_prefer(struct gctl_req *req, struct g_class *mp)
1008 struct g_multipath_softc *sc;
1009 struct g_consumer *cp;
1010 const char *name, *mpname;
1011 static const char devpf[6] = _PATH_DEV;
1014 g_topology_assert();
1016 mpname = gctl_get_asciiparam(req, "arg0");
1017 if (mpname == NULL) {
1018 gctl_error(req, "No 'arg0' argument");
1021 gp = g_multipath_find_geom(mp, mpname);
1023 gctl_error(req, "Device %s is invalid", mpname);
1028 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1029 if (nargs == NULL) {
1030 gctl_error(req, "No 'nargs' argument");
1034 gctl_error(req, "missing device");
1038 name = gctl_get_asciiparam(req, "arg1");
1040 gctl_error(req, "No 'arg1' argument");
1043 if (strncmp(name, devpf, 5) == 0) {
1047 LIST_FOREACH(cp, &gp->consumer, consumer) {
1048 if (cp->provider != NULL
1049 && strcmp(cp->provider->name, name) == 0)
1054 gctl_error(req, "Provider %s not found", name);
1058 mtx_lock(&sc->sc_mtx);
1060 if (cp->index & MP_BAD) {
1061 gctl_error(req, "Consumer %s is invalid", name);
1062 mtx_unlock(&sc->sc_mtx);
1066 /* Here when the consumer is present and in good shape */
1069 if (!sc->sc_active_active)
1070 printf("GEOM_MULTIPATH: %s now active path in %s\n",
1071 sc->sc_active->provider->name, sc->sc_name);
1073 mtx_unlock(&sc->sc_mtx);
1077 g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp)
1079 struct g_multipath_softc *sc;
1081 const char *mpname, *name;
1083 mpname = gctl_get_asciiparam(req, "arg0");
1084 if (mpname == NULL) {
1085 gctl_error(req, "No 'arg0' argument");
1088 gp = g_multipath_find_geom(mp, mpname);
1090 gctl_error(req, "Device %s not found", mpname);
1095 name = gctl_get_asciiparam(req, "arg1");
1097 gctl_error(req, "No 'arg1' argument");
1100 g_multipath_ctl_add_name(req, mp, name);
1104 g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp)
1106 struct g_multipath_metadata md;
1107 struct g_multipath_softc *sc;
1109 const char *mpname, *name;
1111 int *nargs, i, *val;
1113 g_topology_assert();
1115 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
1117 gctl_error(req, "wrong number of arguments.");
1121 mpname = gctl_get_asciiparam(req, "arg0");
1122 if (mpname == NULL) {
1123 gctl_error(req, "No 'arg0' argument");
1126 gp = g_multipath_find_geom(mp, mpname);
1128 gctl_error(req, "Device %s already exist", mpname);
1132 memset(&md, 0, sizeof(md));
1133 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1134 md.md_version = G_MULTIPATH_VERSION;
1135 strlcpy(md.md_name, mpname, sizeof(md.md_name));
1137 md.md_sectorsize = 0;
1139 md.md_active_active = 0;
1140 val = gctl_get_paraml(req, "active_active", sizeof(*val));
1141 if (val != NULL && *val != 0)
1142 md.md_active_active = 1;
1143 val = gctl_get_paraml(req, "active_read", sizeof(*val));
1144 if (val != NULL && *val != 0)
1145 md.md_active_active = 2;
1146 gp = g_multipath_create(mp, &md);
1148 gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n",
1149 md.md_name, md.md_uuid);
1154 for (i = 1; i < *nargs; i++) {
1155 snprintf(param, sizeof(param), "arg%d", i);
1156 name = gctl_get_asciiparam(req, param);
1157 g_multipath_ctl_add_name(req, mp, name);
1160 if (sc->sc_ndisks != (*nargs - 1))
1161 g_multipath_destroy(gp);
1165 g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp)
1167 struct g_multipath_softc *sc;
1169 struct g_consumer *cp;
1170 struct g_provider *pp;
1171 struct g_multipath_metadata md;
1175 g_topology_assert();
1177 name = gctl_get_asciiparam(req, "arg0");
1179 gctl_error(req, "No 'arg0' argument");
1182 gp = g_multipath_find_geom(mp, name);
1184 gctl_error(req, "Device %s is invalid", name);
1188 val = gctl_get_paraml(req, "active_active", sizeof(*val));
1189 if (val != NULL && *val != 0)
1190 sc->sc_active_active = 1;
1191 val = gctl_get_paraml(req, "active_read", sizeof(*val));
1192 if (val != NULL && *val != 0)
1193 sc->sc_active_active = 2;
1194 val = gctl_get_paraml(req, "active_passive", sizeof(*val));
1195 if (val != NULL && *val != 0)
1196 sc->sc_active_active = 0;
1197 if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
1200 strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic));
1201 memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid));
1202 strlcpy(md.md_name, name, sizeof(md.md_name));
1203 md.md_version = G_MULTIPATH_VERSION;
1204 md.md_size = pp->mediasize;
1205 md.md_sectorsize = pp->sectorsize;
1206 md.md_active_active = sc->sc_active_active;
1207 error = g_multipath_write_metadata(cp, &md);
1209 gctl_error(req, "Can't update metadata on %s (%d)",
1215 g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail)
1217 struct g_multipath_softc *sc;
1219 struct g_consumer *cp;
1220 const char *mpname, *name;
1223 mpname = gctl_get_asciiparam(req, "arg0");
1224 if (mpname == NULL) {
1225 gctl_error(req, "No 'arg0' argument");
1228 gp = g_multipath_find_geom(mp, mpname);
1230 gctl_error(req, "Device %s not found", mpname);
1235 name = gctl_get_asciiparam(req, "arg1");
1237 gctl_error(req, "No 'arg1' argument");
1242 mtx_lock(&sc->sc_mtx);
1243 LIST_FOREACH(cp, &gp->consumer, consumer) {
1244 if (cp->provider != NULL &&
1245 strcmp(cp->provider->name, name) == 0 &&
1246 (cp->index & MP_LOST) == 0) {
1248 if (!fail == !(cp->index & MP_FAIL))
1250 printf("GEOM_MULTIPATH: %s in %s is marked %s.\n",
1251 name, sc->sc_name, fail ? "FAIL" : "OK");
1253 g_multipath_fault(cp, MP_FAIL);
1254 SDT_PROBE3(geom, multipath, config, fail,
1255 sc->sc_name, cp->provider->name, 0);
1257 cp->index &= ~MP_FAIL;
1258 SDT_PROBE2(geom, multipath, config, restore,
1259 sc->sc_name, cp->provider->name);
1263 mtx_unlock(&sc->sc_mtx);
1265 gctl_error(req, "Provider %s not found", name);
1269 g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp)
1271 struct g_multipath_softc *sc;
1273 struct g_consumer *cp, *cp1;
1274 const char *mpname, *name;
1278 mpname = gctl_get_asciiparam(req, "arg0");
1279 if (mpname == NULL) {
1280 gctl_error(req, "No 'arg0' argument");
1283 gp = g_multipath_find_geom(mp, mpname);
1285 gctl_error(req, "Device %s not found", mpname);
1290 name = gctl_get_asciiparam(req, "arg1");
1292 gctl_error(req, "No 'arg1' argument");
1297 mtx_lock(&sc->sc_mtx);
1298 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) {
1299 if (cp->provider != NULL &&
1300 strcmp(cp->provider->name, name) == 0 &&
1301 (cp->index & MP_LOST) == 0) {
1303 printf("GEOM_MULTIPATH: removing %s from %s\n",
1304 cp->provider->name, cp->geom->name);
1305 SDT_PROBE2(geom, multipath, config, remove,
1306 cp->geom->name, cp->provider->name);
1308 g_multipath_fault(cp, MP_LOST);
1309 cnt = (uintptr_t *)&cp->private;
1310 if (*cnt == 0 && (cp->index & MP_POSTED) == 0) {
1311 cp->index |= MP_POSTED;
1312 mtx_unlock(&sc->sc_mtx);
1315 return; /* Recursion happened. */
1316 mtx_lock(&sc->sc_mtx);
1320 mtx_unlock(&sc->sc_mtx);
1322 gctl_error(req, "Provider %s not found", name);
1325 static struct g_geom *
1326 g_multipath_find_geom(struct g_class *mp, const char *name)
1329 struct g_multipath_softc *sc;
1331 LIST_FOREACH(gp, &mp->geom, geom) {
1333 if (sc == NULL || sc->sc_stopping)
1335 if (strcmp(gp->name, name) == 0)
1342 g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp)
1348 g_topology_assert();
1350 name = gctl_get_asciiparam(req, "arg0");
1352 gctl_error(req, "No 'arg0' argument");
1355 gp = g_multipath_find_geom(mp, name);
1357 gctl_error(req, "Device %s is invalid", name);
1360 error = g_multipath_destroy(gp);
1361 if (error != 0 && error != EINPROGRESS)
1362 gctl_error(req, "failed to stop %s (err=%d)", name, error);
1366 g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp)
1369 struct g_multipath_softc *sc;
1370 struct g_consumer *cp;
1371 struct g_provider *pp;
1376 g_topology_assert();
1378 name = gctl_get_asciiparam(req, "arg0");
1380 gctl_error(req, "No 'arg0' argument");
1383 gp = g_multipath_find_geom(mp, name);
1385 gctl_error(req, "Device %s is invalid", name);
1390 if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) {
1393 error = g_access(cp, 1, 1, 1);
1395 gctl_error(req, "Can't open %s (%d)", pp->name, error);
1398 g_topology_unlock();
1399 buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO);
1400 error = g_write_data(cp, pp->mediasize - pp->sectorsize,
1401 buf, pp->sectorsize);
1403 g_access(cp, -1, -1, -1);
1405 gctl_error(req, "Can't erase metadata on %s (%d)",
1410 error = g_multipath_destroy(gp);
1411 if (error != 0 && error != EINPROGRESS)
1412 gctl_error(req, "failed to destroy %s (err=%d)", name, error);
1416 g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp)
1422 g_topology_assert();
1424 name = gctl_get_asciiparam(req, "arg0");
1426 gctl_error(req, "No 'arg0' argument");
1429 gp = g_multipath_find_geom(mp, name);
1431 gctl_error(req, "Device %s is invalid", name);
1434 error = g_multipath_rotate(gp);
1436 gctl_error(req, "failed to rotate %s (err=%d)", name, error);
1441 g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp)
1445 struct g_multipath_softc *sc;
1446 struct g_consumer *cp;
1450 sb = sbuf_new_auto();
1452 g_topology_assert();
1453 name = gctl_get_asciiparam(req, "arg0");
1455 gctl_error(req, "No 'arg0' argument");
1458 gp = g_multipath_find_geom(mp, name);
1460 gctl_error(req, "Device %s is invalid", name);
1464 if (sc->sc_active_active == 1) {
1466 LIST_FOREACH(cp, &gp->consumer, consumer) {
1467 if (cp->index & MP_BAD)
1471 sbuf_cat(sb, cp->provider->name);
1475 sbuf_cat(sb, "none");
1477 } else if (sc->sc_active && sc->sc_active->provider) {
1478 sbuf_printf(sb, "%s\n", sc->sc_active->provider->name);
1480 sbuf_cat(sb, "none\n");
1483 gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
1488 g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb)
1491 g_topology_assert();
1492 version = gctl_get_paraml(req, "version", sizeof(*version));
1493 if (version == NULL) {
1494 gctl_error(req, "No 'version' argument");
1495 } else if (*version != G_MULTIPATH_VERSION) {
1496 gctl_error(req, "Userland and kernel parts are out of sync");
1497 } else if (strcmp(verb, "add") == 0) {
1498 g_multipath_ctl_add(req, mp);
1499 } else if (strcmp(verb, "prefer") == 0) {
1500 g_multipath_ctl_prefer(req, mp);
1501 } else if (strcmp(verb, "create") == 0) {
1502 g_multipath_ctl_create(req, mp);
1503 } else if (strcmp(verb, "configure") == 0) {
1504 g_multipath_ctl_configure(req, mp);
1505 } else if (strcmp(verb, "stop") == 0) {
1506 g_multipath_ctl_stop(req, mp);
1507 } else if (strcmp(verb, "destroy") == 0) {
1508 g_multipath_ctl_destroy(req, mp);
1509 } else if (strcmp(verb, "fail") == 0) {
1510 g_multipath_ctl_fail(req, mp, 1);
1511 } else if (strcmp(verb, "restore") == 0) {
1512 g_multipath_ctl_fail(req, mp, 0);
1513 } else if (strcmp(verb, "remove") == 0) {
1514 g_multipath_ctl_remove(req, mp);
1515 } else if (strcmp(verb, "rotate") == 0) {
1516 g_multipath_ctl_rotate(req, mp);
1517 } else if (strcmp(verb, "getactive") == 0) {
1518 g_multipath_ctl_getactive(req, mp);
1520 gctl_error(req, "Unknown verb %s", verb);
1525 g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
1526 struct g_consumer *cp, struct g_provider *pp)
1528 struct g_multipath_softc *sc;
1531 g_topology_assert();
1537 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1538 (cp->index & MP_NEW) ? "NEW" :
1539 (cp->index & MP_LOST) ? "LOST" :
1540 (cp->index & MP_FAIL) ? "FAIL" :
1541 (sc->sc_active_active == 1 || sc->sc_active == cp) ?
1543 sc->sc_active_active == 2 ? "READ" : "PASSIVE");
1545 good = g_multipath_good(gp);
1546 sbuf_printf(sb, "%s<State>%s</State>\n", indent,
1547 good == 0 ? "BROKEN" :
1548 (good != sc->sc_ndisks || sc->sc_ndisks == 1) ?
1549 "DEGRADED" : "OPTIMAL");
1551 if (cp == NULL && pp == NULL) {
1552 sbuf_printf(sb, "%s<UUID>%s</UUID>\n", indent, sc->sc_uuid);
1553 sbuf_printf(sb, "%s<Mode>Active/%s</Mode>\n", indent,
1554 sc->sc_active_active == 2 ? "Read" :
1555 sc->sc_active_active == 1 ? "Active" : "Passive");
1556 sbuf_printf(sb, "%s<Type>%s</Type>\n", indent,
1557 sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC");
1561 DECLARE_GEOM_CLASS(g_multipath_class, g_multipath);
1562 MODULE_VERSION(geom_multipath, 0);