1e770bc6bSMatt Jacob /*- 2*e6afd72bSAlexander Motin * Copyright (c) 2011-2013 Alexander Motin <mav@FreeBSD.org> 3e770bc6bSMatt Jacob * Copyright (c) 2006-2007 Matthew Jacob <mjacob@FreeBSD.org> 4e770bc6bSMatt Jacob * All rights reserved. 5e770bc6bSMatt Jacob * 6e770bc6bSMatt Jacob * Redistribution and use in source and binary forms, with or without 7e770bc6bSMatt Jacob * modification, are permitted provided that the following conditions 8e770bc6bSMatt Jacob * are met: 9e770bc6bSMatt Jacob * 1. Redistributions of source code must retain the above copyright 10e770bc6bSMatt Jacob * notice, this list of conditions and the following disclaimer. 11e770bc6bSMatt Jacob * 2. Redistributions in binary form must reproduce the above copyright 12e770bc6bSMatt Jacob * notice, this list of conditions and the following disclaimer in the 13e770bc6bSMatt Jacob * documentation and/or other materials provided with the distribution. 14e770bc6bSMatt Jacob * 15e770bc6bSMatt Jacob * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 16e770bc6bSMatt Jacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17e770bc6bSMatt Jacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18e770bc6bSMatt Jacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 19e770bc6bSMatt Jacob * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20e770bc6bSMatt Jacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21e770bc6bSMatt Jacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22e770bc6bSMatt Jacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23e770bc6bSMatt Jacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24e770bc6bSMatt Jacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25e770bc6bSMatt Jacob * SUCH DAMAGE. 26e770bc6bSMatt Jacob */ 27e770bc6bSMatt Jacob /* 28e770bc6bSMatt Jacob * Based upon work by Pawel Jakub Dawidek <pjd@FreeBSD.org> for all of the 29e770bc6bSMatt Jacob * fine geom examples, and by Poul Henning Kamp <phk@FreeBSD.org> for GEOM 30e770bc6bSMatt Jacob * itself, all of which is most gratefully acknowledged. 31e770bc6bSMatt Jacob */ 32e770bc6bSMatt Jacob 33e770bc6bSMatt Jacob #include <sys/cdefs.h> 34e770bc6bSMatt Jacob __FBSDID("$FreeBSD$"); 35e770bc6bSMatt Jacob #include <sys/param.h> 36e770bc6bSMatt Jacob #include <sys/systm.h> 37e770bc6bSMatt Jacob #include <sys/kernel.h> 38e770bc6bSMatt Jacob #include <sys/module.h> 39*e6afd72bSAlexander Motin #include <sys/limits.h> 40e770bc6bSMatt Jacob #include <sys/lock.h> 41e770bc6bSMatt Jacob #include <sys/mutex.h> 42e770bc6bSMatt Jacob #include <sys/bio.h> 435d807a0eSAndrey V. Elsukov #include <sys/sbuf.h> 44e770bc6bSMatt Jacob #include <sys/sysctl.h> 45e770bc6bSMatt Jacob #include <sys/kthread.h> 46e770bc6bSMatt Jacob #include <sys/malloc.h> 47e770bc6bSMatt Jacob #include <geom/geom.h> 48e770bc6bSMatt Jacob #include <geom/multipath/g_multipath.h> 49e770bc6bSMatt Jacob 50cb08c2ccSAlexander Leidinger FEATURE(geom_multipath, "GEOM multipath support"); 51e770bc6bSMatt Jacob 52e770bc6bSMatt Jacob SYSCTL_DECL(_kern_geom); 536472ac3dSEd Schouten static SYSCTL_NODE(_kern_geom, OID_AUTO, multipath, CTLFLAG_RW, 0, 54e770bc6bSMatt Jacob "GEOM_MULTIPATH tunables"); 55e770bc6bSMatt Jacob static u_int g_multipath_debug = 0; 56e770bc6bSMatt Jacob SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, debug, CTLFLAG_RW, 57e770bc6bSMatt Jacob &g_multipath_debug, 0, "Debug level"); 580c883cefSAlexander Motin static u_int g_multipath_exclusive = 1; 590c883cefSAlexander Motin SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, exclusive, CTLFLAG_RW, 600c883cefSAlexander Motin &g_multipath_exclusive, 0, "Exclusively open providers"); 61e770bc6bSMatt Jacob 62e770bc6bSMatt Jacob static enum { 63e770bc6bSMatt Jacob GKT_NIL, 64e770bc6bSMatt Jacob GKT_RUN, 65e770bc6bSMatt Jacob GKT_DIE 66e770bc6bSMatt Jacob } g_multipath_kt_state; 67e770bc6bSMatt Jacob static struct bio_queue_head gmtbq; 68e770bc6bSMatt Jacob static struct mtx gmtbq_mtx; 69e770bc6bSMatt Jacob 70e770bc6bSMatt Jacob static void g_multipath_orphan(struct g_consumer *); 71*e6afd72bSAlexander Motin static void g_multipath_resize(struct g_consumer *); 72e770bc6bSMatt Jacob static void g_multipath_start(struct bio *); 73e770bc6bSMatt Jacob static void g_multipath_done(struct bio *); 74e770bc6bSMatt Jacob static void g_multipath_done_error(struct bio *); 75e770bc6bSMatt Jacob static void g_multipath_kt(void *); 76e770bc6bSMatt Jacob 77e770bc6bSMatt Jacob static int g_multipath_destroy(struct g_geom *); 78e770bc6bSMatt Jacob static int 79e770bc6bSMatt Jacob g_multipath_destroy_geom(struct gctl_req *, struct g_class *, struct g_geom *); 80e770bc6bSMatt Jacob 812b4969ffSMatt Jacob static struct g_geom *g_multipath_find_geom(struct g_class *, const char *); 82b5dce617SMatt Jacob static int g_multipath_rotate(struct g_geom *); 83b5dce617SMatt Jacob 84e770bc6bSMatt Jacob static g_taste_t g_multipath_taste; 85e770bc6bSMatt Jacob static g_ctl_req_t g_multipath_config; 86e770bc6bSMatt Jacob static g_init_t g_multipath_init; 87e770bc6bSMatt Jacob static g_fini_t g_multipath_fini; 880c883cefSAlexander Motin static g_dumpconf_t g_multipath_dumpconf; 89e770bc6bSMatt Jacob 90e770bc6bSMatt Jacob struct g_class g_multipath_class = { 91e770bc6bSMatt Jacob .name = G_MULTIPATH_CLASS_NAME, 92e770bc6bSMatt Jacob .version = G_VERSION, 93e770bc6bSMatt Jacob .ctlreq = g_multipath_config, 94e770bc6bSMatt Jacob .taste = g_multipath_taste, 95e770bc6bSMatt Jacob .destroy_geom = g_multipath_destroy_geom, 96e770bc6bSMatt Jacob .init = g_multipath_init, 97e770bc6bSMatt Jacob .fini = g_multipath_fini 98e770bc6bSMatt Jacob }; 99e770bc6bSMatt Jacob 1000c883cefSAlexander Motin #define MP_FAIL 0x00000001 1010c883cefSAlexander Motin #define MP_LOST 0x00000002 1020c883cefSAlexander Motin #define MP_NEW 0x00000004 1030c883cefSAlexander Motin #define MP_POSTED 0x00000008 1040c883cefSAlexander Motin #define MP_BAD (MP_FAIL | MP_LOST | MP_NEW) 1050c883cefSAlexander Motin #define MP_IDLE 0x00000010 1060c883cefSAlexander Motin #define MP_IDLE_MASK 0xfffffff0 1070c883cefSAlexander Motin 1080c883cefSAlexander Motin static int 1090c883cefSAlexander Motin g_multipath_good(struct g_geom *gp) 1100c883cefSAlexander Motin { 1110c883cefSAlexander Motin struct g_consumer *cp; 1120c883cefSAlexander Motin int n = 0; 1130c883cefSAlexander Motin 1140c883cefSAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer) { 1150c883cefSAlexander Motin if ((cp->index & MP_BAD) == 0) 1160c883cefSAlexander Motin n++; 1170c883cefSAlexander Motin } 1180c883cefSAlexander Motin return (n); 1190c883cefSAlexander Motin } 1200c883cefSAlexander Motin 1210c883cefSAlexander Motin static void 1220c883cefSAlexander Motin g_multipath_fault(struct g_consumer *cp, int cause) 1230c883cefSAlexander Motin { 1240c883cefSAlexander Motin struct g_multipath_softc *sc; 1250c883cefSAlexander Motin struct g_consumer *lcp; 1260c883cefSAlexander Motin struct g_geom *gp; 1270c883cefSAlexander Motin 1280c883cefSAlexander Motin gp = cp->geom; 1290c883cefSAlexander Motin sc = gp->softc; 1300c883cefSAlexander Motin cp->index |= cause; 1310c883cefSAlexander Motin if (g_multipath_good(gp) == 0 && sc->sc_ndisks > 0) { 1320c883cefSAlexander Motin LIST_FOREACH(lcp, &gp->consumer, consumer) { 1330c883cefSAlexander Motin if (lcp->provider == NULL || 1340c883cefSAlexander Motin (lcp->index & (MP_LOST | MP_NEW))) 1350c883cefSAlexander Motin continue; 1360c883cefSAlexander Motin if (sc->sc_ndisks > 1 && lcp == cp) 1370c883cefSAlexander Motin continue; 1380c883cefSAlexander Motin printf("GEOM_MULTIPATH: " 1390c883cefSAlexander Motin "all paths in %s were marked FAIL, restore %s\n", 1400c883cefSAlexander Motin sc->sc_name, lcp->provider->name); 1410c883cefSAlexander Motin lcp->index &= ~MP_FAIL; 1420c883cefSAlexander Motin } 1430c883cefSAlexander Motin } 1440c883cefSAlexander Motin if (cp != sc->sc_active) 1450c883cefSAlexander Motin return; 1460c883cefSAlexander Motin sc->sc_active = NULL; 1470c883cefSAlexander Motin LIST_FOREACH(lcp, &gp->consumer, consumer) { 1480c883cefSAlexander Motin if ((lcp->index & MP_BAD) == 0) { 1490c883cefSAlexander Motin sc->sc_active = lcp; 1500c883cefSAlexander Motin break; 1510c883cefSAlexander Motin } 1520c883cefSAlexander Motin } 1530c883cefSAlexander Motin if (sc->sc_active == NULL) { 1540c883cefSAlexander Motin printf("GEOM_MULTIPATH: out of providers for %s\n", 1550c883cefSAlexander Motin sc->sc_name); 15663297dfdSAlexander Motin } else if (sc->sc_active_active != 1) { 1570c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s is now active path in %s\n", 1580c883cefSAlexander Motin sc->sc_active->provider->name, sc->sc_name); 1590c883cefSAlexander Motin } 1600c883cefSAlexander Motin } 1610c883cefSAlexander Motin 1620c883cefSAlexander Motin static struct g_consumer * 16363297dfdSAlexander Motin g_multipath_choose(struct g_geom *gp, struct bio *bp) 1640c883cefSAlexander Motin { 1650c883cefSAlexander Motin struct g_multipath_softc *sc; 1660c883cefSAlexander Motin struct g_consumer *best, *cp; 1670c883cefSAlexander Motin 1680c883cefSAlexander Motin sc = gp->softc; 16963297dfdSAlexander Motin if (sc->sc_active_active == 0 || 17063297dfdSAlexander Motin (sc->sc_active_active == 2 && bp->bio_cmd != BIO_READ)) 1710c883cefSAlexander Motin return (sc->sc_active); 1720c883cefSAlexander Motin best = NULL; 1730c883cefSAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer) { 1740c883cefSAlexander Motin if (cp->index & MP_BAD) 1750c883cefSAlexander Motin continue; 1760c883cefSAlexander Motin cp->index += MP_IDLE; 1770c883cefSAlexander Motin if (best == NULL || cp->private < best->private || 1780c883cefSAlexander Motin (cp->private == best->private && cp->index > best->index)) 1790c883cefSAlexander Motin best = cp; 1800c883cefSAlexander Motin } 1810c883cefSAlexander Motin if (best != NULL) 1820c883cefSAlexander Motin best->index &= ~MP_IDLE_MASK; 1830c883cefSAlexander Motin return (best); 1840c883cefSAlexander Motin } 185e770bc6bSMatt Jacob 186e770bc6bSMatt Jacob static void 187e770bc6bSMatt Jacob g_mpd(void *arg, int flags __unused) 188e770bc6bSMatt Jacob { 1890c883cefSAlexander Motin struct g_geom *gp; 1900c883cefSAlexander Motin struct g_multipath_softc *sc; 191e770bc6bSMatt Jacob struct g_consumer *cp; 1920c883cefSAlexander Motin int w; 193e770bc6bSMatt Jacob 194e770bc6bSMatt Jacob g_topology_assert(); 195e770bc6bSMatt Jacob cp = arg; 1960c883cefSAlexander Motin gp = cp->geom; 1970c883cefSAlexander Motin if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) { 1980c883cefSAlexander Motin w = cp->acw; 199e770bc6bSMatt Jacob g_access(cp, -cp->acr, -cp->acw, -cp->ace); 2000c883cefSAlexander Motin if (w > 0 && cp->provider != NULL && 2010c883cefSAlexander Motin (cp->provider->geom->flags & G_GEOM_WITHER) == 0) { 2020c883cefSAlexander Motin g_post_event(g_mpd, cp, M_WAITOK, NULL); 2030c883cefSAlexander Motin return; 2040c883cefSAlexander Motin } 2050c883cefSAlexander Motin } 2060c883cefSAlexander Motin sc = gp->softc; 2070c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 208e770bc6bSMatt Jacob if (cp->provider) { 209e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: %s removed from %s\n", 2100c883cefSAlexander Motin cp->provider->name, gp->name); 211e770bc6bSMatt Jacob g_detach(cp); 212e770bc6bSMatt Jacob } 213e770bc6bSMatt Jacob g_destroy_consumer(cp); 2140c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 2150c883cefSAlexander Motin if (LIST_EMPTY(&gp->consumer)) 2160c883cefSAlexander Motin g_multipath_destroy(gp); 217e770bc6bSMatt Jacob } 218e770bc6bSMatt Jacob 219e770bc6bSMatt Jacob static void 220e770bc6bSMatt Jacob g_multipath_orphan(struct g_consumer *cp) 221e770bc6bSMatt Jacob { 2220c883cefSAlexander Motin struct g_multipath_softc *sc; 2230c883cefSAlexander Motin uintptr_t *cnt; 2240c883cefSAlexander Motin 2250c883cefSAlexander Motin g_topology_assert(); 2260c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s in %s was disconnected\n", 227e770bc6bSMatt Jacob cp->provider->name, cp->geom->name); 2280c883cefSAlexander Motin sc = cp->geom->softc; 2290c883cefSAlexander Motin cnt = (uintptr_t *)&cp->private; 2300c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 2310c883cefSAlexander Motin sc->sc_ndisks--; 2320c883cefSAlexander Motin g_multipath_fault(cp, MP_LOST); 2330c883cefSAlexander Motin if (*cnt == 0 && (cp->index & MP_POSTED) == 0) { 2340c883cefSAlexander Motin cp->index |= MP_POSTED; 2350c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 236e770bc6bSMatt Jacob g_mpd(cp, 0); 2370c883cefSAlexander Motin } else 2380c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 239e770bc6bSMatt Jacob } 240e770bc6bSMatt Jacob 241e770bc6bSMatt Jacob static void 242*e6afd72bSAlexander Motin g_multipath_resize(struct g_consumer *cp) 243*e6afd72bSAlexander Motin { 244*e6afd72bSAlexander Motin struct g_multipath_softc *sc; 245*e6afd72bSAlexander Motin struct g_geom *gp; 246*e6afd72bSAlexander Motin struct g_provider *pp; 247*e6afd72bSAlexander Motin struct g_multipath_metadata md; 248*e6afd72bSAlexander Motin off_t size, psize, ssize; 249*e6afd72bSAlexander Motin int error; 250*e6afd72bSAlexander Motin void *buf; 251*e6afd72bSAlexander Motin 252*e6afd72bSAlexander Motin g_topology_assert(); 253*e6afd72bSAlexander Motin 254*e6afd72bSAlexander Motin gp = cp->geom; 255*e6afd72bSAlexander Motin pp = cp->provider; 256*e6afd72bSAlexander Motin sc = gp->softc; 257*e6afd72bSAlexander Motin 258*e6afd72bSAlexander Motin if (sc->sc_stopping) 259*e6afd72bSAlexander Motin return; 260*e6afd72bSAlexander Motin 261*e6afd72bSAlexander Motin if (pp->mediasize < sc->sc_size) { 262*e6afd72bSAlexander Motin size = pp->mediasize; 263*e6afd72bSAlexander Motin ssize = pp->sectorsize; 264*e6afd72bSAlexander Motin } else { 265*e6afd72bSAlexander Motin size = ssize = OFF_MAX; 266*e6afd72bSAlexander Motin mtx_lock(&sc->sc_mtx); 267*e6afd72bSAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer) { 268*e6afd72bSAlexander Motin pp = cp->provider; 269*e6afd72bSAlexander Motin if (pp == NULL) 270*e6afd72bSAlexander Motin continue; 271*e6afd72bSAlexander Motin if (pp->mediasize < size) { 272*e6afd72bSAlexander Motin size = pp->mediasize; 273*e6afd72bSAlexander Motin ssize = pp->sectorsize; 274*e6afd72bSAlexander Motin } 275*e6afd72bSAlexander Motin } 276*e6afd72bSAlexander Motin mtx_unlock(&sc->sc_mtx); 277*e6afd72bSAlexander Motin if (size == OFF_MAX || size == sc->sc_size) 278*e6afd72bSAlexander Motin return; 279*e6afd72bSAlexander Motin } 280*e6afd72bSAlexander Motin psize = size - ((sc->sc_uuid[0] != 0) ? ssize : 0); 281*e6afd72bSAlexander Motin printf("GEOM_MULTIPATH: %s size changed from %jd to %jd\n", 282*e6afd72bSAlexander Motin sc->sc_name, sc->sc_pp->mediasize, psize); 283*e6afd72bSAlexander Motin if (sc->sc_uuid[0] != 0 && psize < sc->sc_pp->mediasize) { 284*e6afd72bSAlexander Motin g_multipath_destroy(gp); 285*e6afd72bSAlexander Motin return; 286*e6afd72bSAlexander Motin } 287*e6afd72bSAlexander Motin sc->sc_size = size; 288*e6afd72bSAlexander Motin g_resize_provider(sc->sc_pp, psize); 289*e6afd72bSAlexander Motin 290*e6afd72bSAlexander Motin if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) { 291*e6afd72bSAlexander Motin cp = sc->sc_active; 292*e6afd72bSAlexander Motin pp = cp->provider; 293*e6afd72bSAlexander Motin error = g_access(cp, 1, 1, 1); 294*e6afd72bSAlexander Motin if (error != 0) { 295*e6afd72bSAlexander Motin printf("GEOM_MULTIPATH: Can't open %s (%d)\n", 296*e6afd72bSAlexander Motin pp->name, error); 297*e6afd72bSAlexander Motin return; 298*e6afd72bSAlexander Motin } 299*e6afd72bSAlexander Motin g_topology_unlock(); 300*e6afd72bSAlexander Motin buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 301*e6afd72bSAlexander Motin strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic)); 302*e6afd72bSAlexander Motin memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid)); 303*e6afd72bSAlexander Motin strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name)); 304*e6afd72bSAlexander Motin md.md_version = G_MULTIPATH_VERSION; 305*e6afd72bSAlexander Motin md.md_size = size; 306*e6afd72bSAlexander Motin md.md_sectorsize = pp->sectorsize; 307*e6afd72bSAlexander Motin md.md_active_active = sc->sc_active_active; 308*e6afd72bSAlexander Motin multipath_metadata_encode(&md, buf); 309*e6afd72bSAlexander Motin error = g_write_data(cp, pp->mediasize - pp->sectorsize, 310*e6afd72bSAlexander Motin buf, pp->sectorsize); 311*e6afd72bSAlexander Motin g_topology_lock(); 312*e6afd72bSAlexander Motin g_access(cp, -1, -1, -1); 313*e6afd72bSAlexander Motin if (error != 0) 314*e6afd72bSAlexander Motin printf("GEOM_MULTIPATH: Can't update metadata on %s " 315*e6afd72bSAlexander Motin "(%d)\n", pp->name, error); 316*e6afd72bSAlexander Motin } 317*e6afd72bSAlexander Motin } 318*e6afd72bSAlexander Motin 319*e6afd72bSAlexander Motin static void 320e770bc6bSMatt Jacob g_multipath_start(struct bio *bp) 321e770bc6bSMatt Jacob { 322e770bc6bSMatt Jacob struct g_multipath_softc *sc; 323e770bc6bSMatt Jacob struct g_geom *gp; 324e770bc6bSMatt Jacob struct g_consumer *cp; 325e770bc6bSMatt Jacob struct bio *cbp; 3260c883cefSAlexander Motin uintptr_t *cnt; 327e770bc6bSMatt Jacob 328e770bc6bSMatt Jacob gp = bp->bio_to->geom; 329e770bc6bSMatt Jacob sc = gp->softc; 330e770bc6bSMatt Jacob KASSERT(sc != NULL, ("NULL sc")); 331e770bc6bSMatt Jacob cbp = g_clone_bio(bp); 332e770bc6bSMatt Jacob if (cbp == NULL) { 333e770bc6bSMatt Jacob g_io_deliver(bp, ENOMEM); 334e770bc6bSMatt Jacob return; 335e770bc6bSMatt Jacob } 3360c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 33763297dfdSAlexander Motin cp = g_multipath_choose(gp, bp); 3380c883cefSAlexander Motin if (cp == NULL) { 3390c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 3400c883cefSAlexander Motin g_destroy_bio(cbp); 3410c883cefSAlexander Motin g_io_deliver(bp, ENXIO); 3420c883cefSAlexander Motin return; 3430c883cefSAlexander Motin } 3440c883cefSAlexander Motin if ((uintptr_t)bp->bio_driver1 < sc->sc_ndisks) 3450c883cefSAlexander Motin bp->bio_driver1 = (void *)(uintptr_t)sc->sc_ndisks; 3460c883cefSAlexander Motin cnt = (uintptr_t *)&cp->private; 3470c883cefSAlexander Motin (*cnt)++; 3480c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 349e770bc6bSMatt Jacob cbp->bio_done = g_multipath_done; 350e770bc6bSMatt Jacob g_io_request(cbp, cp); 351e770bc6bSMatt Jacob } 352e770bc6bSMatt Jacob 353e770bc6bSMatt Jacob static void 354e770bc6bSMatt Jacob g_multipath_done(struct bio *bp) 355e770bc6bSMatt Jacob { 3560c883cefSAlexander Motin struct g_multipath_softc *sc; 3570c883cefSAlexander Motin struct g_consumer *cp; 3580c883cefSAlexander Motin uintptr_t *cnt; 3590c883cefSAlexander Motin 360e770bc6bSMatt Jacob if (bp->bio_error == ENXIO || bp->bio_error == EIO) { 361e770bc6bSMatt Jacob mtx_lock(&gmtbq_mtx); 362e770bc6bSMatt Jacob bioq_insert_tail(&gmtbq, bp); 363e770bc6bSMatt Jacob mtx_unlock(&gmtbq_mtx); 3640c883cefSAlexander Motin wakeup(&g_multipath_kt_state); 365e770bc6bSMatt Jacob } else { 3660c883cefSAlexander Motin cp = bp->bio_from; 3670c883cefSAlexander Motin sc = cp->geom->softc; 3680c883cefSAlexander Motin cnt = (uintptr_t *)&cp->private; 3690c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 3700c883cefSAlexander Motin (*cnt)--; 3710c883cefSAlexander Motin if (*cnt == 0 && (cp->index & MP_LOST)) { 3720c883cefSAlexander Motin cp->index |= MP_POSTED; 3730c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 3740c883cefSAlexander Motin g_post_event(g_mpd, cp, M_WAITOK, NULL); 3750c883cefSAlexander Motin } else 3760c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 377e770bc6bSMatt Jacob g_std_done(bp); 378e770bc6bSMatt Jacob } 379e770bc6bSMatt Jacob } 380e770bc6bSMatt Jacob 381e770bc6bSMatt Jacob static void 382e770bc6bSMatt Jacob g_multipath_done_error(struct bio *bp) 383e770bc6bSMatt Jacob { 384e770bc6bSMatt Jacob struct bio *pbp; 385e770bc6bSMatt Jacob struct g_geom *gp; 386e770bc6bSMatt Jacob struct g_multipath_softc *sc; 387e770bc6bSMatt Jacob struct g_consumer *cp; 388e770bc6bSMatt Jacob struct g_provider *pp; 3890c883cefSAlexander Motin uintptr_t *cnt; 390e770bc6bSMatt Jacob 391e770bc6bSMatt Jacob /* 392e770bc6bSMatt Jacob * If we had a failure, we have to check first to see 393e770bc6bSMatt Jacob * whether the consumer it failed on was the currently 394e770bc6bSMatt Jacob * active consumer (i.e., this is the first in perhaps 395e770bc6bSMatt Jacob * a number of failures). If so, we then switch consumers 396e770bc6bSMatt Jacob * to the next available consumer. 397e770bc6bSMatt Jacob */ 398e770bc6bSMatt Jacob 399e770bc6bSMatt Jacob pbp = bp->bio_parent; 400e770bc6bSMatt Jacob gp = pbp->bio_to->geom; 401e770bc6bSMatt Jacob sc = gp->softc; 402e770bc6bSMatt Jacob cp = bp->bio_from; 403e770bc6bSMatt Jacob pp = cp->provider; 4040c883cefSAlexander Motin cnt = (uintptr_t *)&cp->private; 405e770bc6bSMatt Jacob 4060c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 40763297dfdSAlexander Motin if ((cp->index & MP_FAIL) == 0) { 4080c883cefSAlexander Motin printf("GEOM_MULTIPATH: Error %d, %s in %s marked FAIL\n", 4090c883cefSAlexander Motin bp->bio_error, pp->name, sc->sc_name); 4100c883cefSAlexander Motin g_multipath_fault(cp, MP_FAIL); 41163297dfdSAlexander Motin } 4120c883cefSAlexander Motin (*cnt)--; 4130c883cefSAlexander Motin if (*cnt == 0 && (cp->index & (MP_LOST | MP_POSTED)) == MP_LOST) { 414e770bc6bSMatt Jacob cp->index |= MP_POSTED; 4150c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 4160c883cefSAlexander Motin g_post_event(g_mpd, cp, M_WAITOK, NULL); 4170c883cefSAlexander Motin } else 4180c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 419e770bc6bSMatt Jacob 420e770bc6bSMatt Jacob /* 421e770bc6bSMatt Jacob * If we can fruitfully restart the I/O, do so. 422e770bc6bSMatt Jacob */ 4230c883cefSAlexander Motin if (pbp->bio_children < (uintptr_t)pbp->bio_driver1) { 4240c883cefSAlexander Motin pbp->bio_inbed++; 425e770bc6bSMatt Jacob g_destroy_bio(bp); 426e770bc6bSMatt Jacob g_multipath_start(pbp); 427e770bc6bSMatt Jacob } else { 428e770bc6bSMatt Jacob g_std_done(bp); 429e770bc6bSMatt Jacob } 430e770bc6bSMatt Jacob } 431e770bc6bSMatt Jacob 432e770bc6bSMatt Jacob static void 433e770bc6bSMatt Jacob g_multipath_kt(void *arg) 434e770bc6bSMatt Jacob { 43512f35a61SPawel Jakub Dawidek 436e770bc6bSMatt Jacob g_multipath_kt_state = GKT_RUN; 437e770bc6bSMatt Jacob mtx_lock(&gmtbq_mtx); 438e770bc6bSMatt Jacob while (g_multipath_kt_state == GKT_RUN) { 439e770bc6bSMatt Jacob for (;;) { 440e770bc6bSMatt Jacob struct bio *bp; 44112f35a61SPawel Jakub Dawidek 442e770bc6bSMatt Jacob bp = bioq_takefirst(&gmtbq); 44312f35a61SPawel Jakub Dawidek if (bp == NULL) 444e770bc6bSMatt Jacob break; 445e770bc6bSMatt Jacob mtx_unlock(&gmtbq_mtx); 446e770bc6bSMatt Jacob g_multipath_done_error(bp); 447e770bc6bSMatt Jacob mtx_lock(&gmtbq_mtx); 448e770bc6bSMatt Jacob } 44963297dfdSAlexander Motin if (g_multipath_kt_state != GKT_RUN) 45063297dfdSAlexander Motin break; 451e770bc6bSMatt Jacob msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO, 45263297dfdSAlexander Motin "gkt:wait", 0); 453e770bc6bSMatt Jacob } 454e770bc6bSMatt Jacob mtx_unlock(&gmtbq_mtx); 455e770bc6bSMatt Jacob wakeup(&g_multipath_kt_state); 4563745c395SJulian Elischer kproc_exit(0); 457e770bc6bSMatt Jacob } 458e770bc6bSMatt Jacob 459e770bc6bSMatt Jacob 460e770bc6bSMatt Jacob static int 461e770bc6bSMatt Jacob g_multipath_access(struct g_provider *pp, int dr, int dw, int de) 462e770bc6bSMatt Jacob { 463e770bc6bSMatt Jacob struct g_geom *gp; 464e770bc6bSMatt Jacob struct g_consumer *cp, *badcp = NULL; 4650c883cefSAlexander Motin struct g_multipath_softc *sc; 466e770bc6bSMatt Jacob int error; 467e770bc6bSMatt Jacob 468e770bc6bSMatt Jacob gp = pp->geom; 469e770bc6bSMatt Jacob 470e770bc6bSMatt Jacob LIST_FOREACH(cp, &gp->consumer, consumer) { 471e770bc6bSMatt Jacob error = g_access(cp, dr, dw, de); 472e770bc6bSMatt Jacob if (error) { 473e770bc6bSMatt Jacob badcp = cp; 474e770bc6bSMatt Jacob goto fail; 475e770bc6bSMatt Jacob } 476e770bc6bSMatt Jacob } 4770c883cefSAlexander Motin sc = gp->softc; 4780c883cefSAlexander Motin sc->sc_opened += dr + dw + de; 4790c883cefSAlexander Motin if (sc->sc_stopping && sc->sc_opened == 0) 4800c883cefSAlexander Motin g_multipath_destroy(gp); 481e770bc6bSMatt Jacob return (0); 482e770bc6bSMatt Jacob 483e770bc6bSMatt Jacob fail: 484e770bc6bSMatt Jacob LIST_FOREACH(cp, &gp->consumer, consumer) { 48512f35a61SPawel Jakub Dawidek if (cp == badcp) 486e770bc6bSMatt Jacob break; 487e770bc6bSMatt Jacob (void) g_access(cp, -dr, -dw, -de); 488e770bc6bSMatt Jacob } 489e770bc6bSMatt Jacob return (error); 490e770bc6bSMatt Jacob } 491e770bc6bSMatt Jacob 492e770bc6bSMatt Jacob static struct g_geom * 493e770bc6bSMatt Jacob g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md) 494e770bc6bSMatt Jacob { 495e770bc6bSMatt Jacob struct g_multipath_softc *sc; 496e770bc6bSMatt Jacob struct g_geom *gp; 497e770bc6bSMatt Jacob struct g_provider *pp; 498e770bc6bSMatt Jacob 499e770bc6bSMatt Jacob g_topology_assert(); 500e770bc6bSMatt Jacob 501e770bc6bSMatt Jacob LIST_FOREACH(gp, &mp->geom, geom) { 5020c883cefSAlexander Motin sc = gp->softc; 5030c883cefSAlexander Motin if (sc == NULL || sc->sc_stopping) 5040c883cefSAlexander Motin continue; 505e770bc6bSMatt Jacob if (strcmp(gp->name, md->md_name) == 0) { 506e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: name %s already exists\n", 507e770bc6bSMatt Jacob md->md_name); 508e770bc6bSMatt Jacob return (NULL); 509e770bc6bSMatt Jacob } 510e770bc6bSMatt Jacob } 511e770bc6bSMatt Jacob 51202c62349SJaakko Heinonen gp = g_new_geomf(mp, "%s", md->md_name); 513e770bc6bSMatt Jacob sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 5140c883cefSAlexander Motin mtx_init(&sc->sc_mtx, "multipath", NULL, MTX_DEF); 5150c883cefSAlexander Motin memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid)); 5160c883cefSAlexander Motin memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name)); 5170c883cefSAlexander Motin sc->sc_active_active = md->md_active_active; 518*e6afd72bSAlexander Motin sc->sc_size = md->md_size; 519e770bc6bSMatt Jacob gp->softc = sc; 520e770bc6bSMatt Jacob gp->start = g_multipath_start; 521e770bc6bSMatt Jacob gp->orphan = g_multipath_orphan; 522*e6afd72bSAlexander Motin gp->resize = g_multipath_resize; 523e770bc6bSMatt Jacob gp->access = g_multipath_access; 5240c883cefSAlexander Motin gp->dumpconf = g_multipath_dumpconf; 525e770bc6bSMatt Jacob 526e770bc6bSMatt Jacob pp = g_new_providerf(gp, "multipath/%s", md->md_name); 52740ea77a0SAlexander Motin pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 5280c883cefSAlexander Motin if (md->md_size != 0) { 5290c883cefSAlexander Motin pp->mediasize = md->md_size - 5300c883cefSAlexander Motin ((md->md_uuid[0] != 0) ? md->md_sectorsize : 0); 531e770bc6bSMatt Jacob pp->sectorsize = md->md_sectorsize; 5320c883cefSAlexander Motin } 5330c883cefSAlexander Motin sc->sc_pp = pp; 534e770bc6bSMatt Jacob g_error_provider(pp, 0); 5350c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s created\n", gp->name); 536e770bc6bSMatt Jacob return (gp); 537e770bc6bSMatt Jacob } 538e770bc6bSMatt Jacob 539e770bc6bSMatt Jacob static int 540e770bc6bSMatt Jacob g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp) 541e770bc6bSMatt Jacob { 542e770bc6bSMatt Jacob struct g_multipath_softc *sc; 543e770bc6bSMatt Jacob struct g_consumer *cp, *nxtcp; 5440c883cefSAlexander Motin int error, acr, acw, ace; 545e770bc6bSMatt Jacob 546e770bc6bSMatt Jacob g_topology_assert(); 547e770bc6bSMatt Jacob 548e770bc6bSMatt Jacob sc = gp->softc; 549e770bc6bSMatt Jacob KASSERT(sc, ("no softc")); 550e770bc6bSMatt Jacob 551e770bc6bSMatt Jacob /* 552e770bc6bSMatt Jacob * Make sure that the passed provider isn't already attached 553e770bc6bSMatt Jacob */ 554e770bc6bSMatt Jacob LIST_FOREACH(cp, &gp->consumer, consumer) { 55512f35a61SPawel Jakub Dawidek if (cp->provider == pp) 556e770bc6bSMatt Jacob break; 557e770bc6bSMatt Jacob } 558e770bc6bSMatt Jacob if (cp) { 559e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: provider %s already attached to %s\n", 560e770bc6bSMatt Jacob pp->name, gp->name); 561e770bc6bSMatt Jacob return (EEXIST); 562e770bc6bSMatt Jacob } 563e770bc6bSMatt Jacob nxtcp = LIST_FIRST(&gp->consumer); 564e770bc6bSMatt Jacob cp = g_new_consumer(gp); 56540ea77a0SAlexander Motin cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 5660c883cefSAlexander Motin cp->private = NULL; 5670c883cefSAlexander Motin cp->index = MP_NEW; 568e770bc6bSMatt Jacob error = g_attach(cp, pp); 569e770bc6bSMatt Jacob if (error != 0) { 570e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: cannot attach %s to %s", 571e770bc6bSMatt Jacob pp->name, sc->sc_name); 572e770bc6bSMatt Jacob g_destroy_consumer(cp); 573e770bc6bSMatt Jacob return (error); 574e770bc6bSMatt Jacob } 575e770bc6bSMatt Jacob 576e770bc6bSMatt Jacob /* 577e770bc6bSMatt Jacob * Set access permissions on new consumer to match other consumers 578e770bc6bSMatt Jacob */ 5790c883cefSAlexander Motin if (sc->sc_pp) { 5800c883cefSAlexander Motin acr = sc->sc_pp->acr; 5810c883cefSAlexander Motin acw = sc->sc_pp->acw; 5820c883cefSAlexander Motin ace = sc->sc_pp->ace; 5830c883cefSAlexander Motin } else 5840c883cefSAlexander Motin acr = acw = ace = 0; 5850c883cefSAlexander Motin if (g_multipath_exclusive) { 5860c883cefSAlexander Motin acr++; 5870c883cefSAlexander Motin acw++; 5880c883cefSAlexander Motin ace++; 5890c883cefSAlexander Motin } 5900c883cefSAlexander Motin error = g_access(cp, acr, acw, ace); 591e770bc6bSMatt Jacob if (error) { 592e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: cannot set access in " 5930c883cefSAlexander Motin "attaching %s to %s (%d)\n", 5940c883cefSAlexander Motin pp->name, sc->sc_name, error); 595e770bc6bSMatt Jacob g_detach(cp); 596e770bc6bSMatt Jacob g_destroy_consumer(cp); 597e770bc6bSMatt Jacob return (error); 598e770bc6bSMatt Jacob } 599*e6afd72bSAlexander Motin if (sc->sc_size == 0) { 600*e6afd72bSAlexander Motin sc->sc_size = pp->mediasize - 6010c883cefSAlexander Motin ((sc->sc_uuid[0] != 0) ? pp->sectorsize : 0); 602*e6afd72bSAlexander Motin sc->sc_pp->mediasize = sc->sc_size; 6030c883cefSAlexander Motin sc->sc_pp->sectorsize = pp->sectorsize; 604e770bc6bSMatt Jacob } 605*e6afd72bSAlexander Motin if (sc->sc_pp->stripesize == 0 && sc->sc_pp->stripeoffset == 0) { 6060c883cefSAlexander Motin sc->sc_pp->stripesize = pp->stripesize; 6070c883cefSAlexander Motin sc->sc_pp->stripeoffset = pp->stripeoffset; 6080c883cefSAlexander Motin } 609f4673017SAlexander Motin sc->sc_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED; 6100c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 6110c883cefSAlexander Motin cp->index = 0; 6120c883cefSAlexander Motin sc->sc_ndisks++; 6130c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 6140c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s added to %s\n", 6150c883cefSAlexander Motin pp->name, sc->sc_name); 6160c883cefSAlexander Motin if (sc->sc_active == NULL) { 6170c883cefSAlexander Motin sc->sc_active = cp; 61863297dfdSAlexander Motin if (sc->sc_active_active != 1) 6190c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s is now active path in %s\n", 620e770bc6bSMatt Jacob pp->name, sc->sc_name); 621e770bc6bSMatt Jacob } 622e770bc6bSMatt Jacob return (0); 623e770bc6bSMatt Jacob } 624e770bc6bSMatt Jacob 625e770bc6bSMatt Jacob static int 626e770bc6bSMatt Jacob g_multipath_destroy(struct g_geom *gp) 627e770bc6bSMatt Jacob { 6280c883cefSAlexander Motin struct g_multipath_softc *sc; 6290c883cefSAlexander Motin struct g_consumer *cp, *cp1; 630e770bc6bSMatt Jacob 631e770bc6bSMatt Jacob g_topology_assert(); 63212f35a61SPawel Jakub Dawidek if (gp->softc == NULL) 633e770bc6bSMatt Jacob return (ENXIO); 6340c883cefSAlexander Motin sc = gp->softc; 6350c883cefSAlexander Motin if (!sc->sc_stopping) { 636e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: destroying %s\n", gp->name); 6370c883cefSAlexander Motin sc->sc_stopping = 1; 6380c883cefSAlexander Motin } 6390c883cefSAlexander Motin if (sc->sc_opened != 0) { 6400c883cefSAlexander Motin g_wither_provider(sc->sc_pp, ENXIO); 6410c883cefSAlexander Motin sc->sc_pp = NULL; 6420c883cefSAlexander Motin return (EINPROGRESS); 6430c883cefSAlexander Motin } 6440c883cefSAlexander Motin LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) { 6450c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 6460c883cefSAlexander Motin if ((cp->index & MP_POSTED) == 0) { 6470c883cefSAlexander Motin cp->index |= MP_POSTED; 6480c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 6490c883cefSAlexander Motin g_mpd(cp, 0); 6500c883cefSAlexander Motin if (cp1 == NULL) 6510c883cefSAlexander Motin return(0); /* Recursion happened. */ 6520c883cefSAlexander Motin } else 6530c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 6540c883cefSAlexander Motin } 6550c883cefSAlexander Motin if (!LIST_EMPTY(&gp->consumer)) 6560c883cefSAlexander Motin return (EINPROGRESS); 6570c883cefSAlexander Motin mtx_destroy(&sc->sc_mtx); 658e770bc6bSMatt Jacob g_free(gp->softc); 659e770bc6bSMatt Jacob gp->softc = NULL; 6600c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s destroyed\n", gp->name); 661e770bc6bSMatt Jacob g_wither_geom(gp, ENXIO); 662e770bc6bSMatt Jacob return (0); 663e770bc6bSMatt Jacob } 664e770bc6bSMatt Jacob 665e770bc6bSMatt Jacob static int 666e770bc6bSMatt Jacob g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp, 667e770bc6bSMatt Jacob struct g_geom *gp) 668e770bc6bSMatt Jacob { 66912f35a61SPawel Jakub Dawidek 670e770bc6bSMatt Jacob return (g_multipath_destroy(gp)); 671e770bc6bSMatt Jacob } 672e770bc6bSMatt Jacob 673b5dce617SMatt Jacob static int 674b5dce617SMatt Jacob g_multipath_rotate(struct g_geom *gp) 675b5dce617SMatt Jacob { 6768fb378d6SThomas Quinot struct g_consumer *lcp, *first_good_cp = NULL; 677b5dce617SMatt Jacob struct g_multipath_softc *sc = gp->softc; 6788fb378d6SThomas Quinot int active_cp_seen = 0; 679b5dce617SMatt Jacob 680b5dce617SMatt Jacob g_topology_assert(); 681b5dce617SMatt Jacob if (sc == NULL) 682b5dce617SMatt Jacob return (ENXIO); 683b5dce617SMatt Jacob LIST_FOREACH(lcp, &gp->consumer, consumer) { 684b5dce617SMatt Jacob if ((lcp->index & MP_BAD) == 0) { 6858fb378d6SThomas Quinot if (first_good_cp == NULL) 6868fb378d6SThomas Quinot first_good_cp = lcp; 6878fb378d6SThomas Quinot if (active_cp_seen) 688b5dce617SMatt Jacob break; 689b5dce617SMatt Jacob } 6908fb378d6SThomas Quinot if (sc->sc_active == lcp) 6918fb378d6SThomas Quinot active_cp_seen = 1; 692b5dce617SMatt Jacob } 6938fb378d6SThomas Quinot if (lcp == NULL) 6948fb378d6SThomas Quinot lcp = first_good_cp; 6958fb378d6SThomas Quinot if (lcp && lcp != sc->sc_active) { 6960c883cefSAlexander Motin sc->sc_active = lcp; 69763297dfdSAlexander Motin if (sc->sc_active_active != 1) 6980c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s is now active path in %s\n", 699b5dce617SMatt Jacob lcp->provider->name, sc->sc_name); 700b5dce617SMatt Jacob } 701b5dce617SMatt Jacob return (0); 702b5dce617SMatt Jacob } 703b5dce617SMatt Jacob 704e770bc6bSMatt Jacob static void 705e770bc6bSMatt Jacob g_multipath_init(struct g_class *mp) 706e770bc6bSMatt Jacob { 707e770bc6bSMatt Jacob bioq_init(&gmtbq); 708e770bc6bSMatt Jacob mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF); 70963297dfdSAlexander Motin kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt"); 710e770bc6bSMatt Jacob } 711e770bc6bSMatt Jacob 712e770bc6bSMatt Jacob static void 713e770bc6bSMatt Jacob g_multipath_fini(struct g_class *mp) 714e770bc6bSMatt Jacob { 715e770bc6bSMatt Jacob if (g_multipath_kt_state == GKT_RUN) { 716e770bc6bSMatt Jacob mtx_lock(&gmtbq_mtx); 717e770bc6bSMatt Jacob g_multipath_kt_state = GKT_DIE; 718e770bc6bSMatt Jacob wakeup(&g_multipath_kt_state); 719e770bc6bSMatt Jacob msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO, 720e770bc6bSMatt Jacob "gmp:fini", 0); 721e770bc6bSMatt Jacob mtx_unlock(&gmtbq_mtx); 722e770bc6bSMatt Jacob } 723e770bc6bSMatt Jacob } 724e770bc6bSMatt Jacob 725e770bc6bSMatt Jacob static int 726e770bc6bSMatt Jacob g_multipath_read_metadata(struct g_consumer *cp, 727e770bc6bSMatt Jacob struct g_multipath_metadata *md) 728e770bc6bSMatt Jacob { 729e770bc6bSMatt Jacob struct g_provider *pp; 730e770bc6bSMatt Jacob u_char *buf; 731e770bc6bSMatt Jacob int error; 732e770bc6bSMatt Jacob 733e770bc6bSMatt Jacob g_topology_assert(); 734e770bc6bSMatt Jacob error = g_access(cp, 1, 0, 0); 73512f35a61SPawel Jakub Dawidek if (error != 0) 736e770bc6bSMatt Jacob return (error); 737e770bc6bSMatt Jacob pp = cp->provider; 738e770bc6bSMatt Jacob g_topology_unlock(); 739e770bc6bSMatt Jacob buf = g_read_data(cp, pp->mediasize - pp->sectorsize, 740e770bc6bSMatt Jacob pp->sectorsize, &error); 741e770bc6bSMatt Jacob g_topology_lock(); 742e770bc6bSMatt Jacob g_access(cp, -1, 0, 0); 74312f35a61SPawel Jakub Dawidek if (buf == NULL) 744e770bc6bSMatt Jacob return (error); 745e770bc6bSMatt Jacob multipath_metadata_decode(buf, md); 746e770bc6bSMatt Jacob g_free(buf); 747e770bc6bSMatt Jacob return (0); 748e770bc6bSMatt Jacob } 749e770bc6bSMatt Jacob 750e770bc6bSMatt Jacob static struct g_geom * 751e770bc6bSMatt Jacob g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 752e770bc6bSMatt Jacob { 753e770bc6bSMatt Jacob struct g_multipath_metadata md; 754e770bc6bSMatt Jacob struct g_multipath_softc *sc; 755e770bc6bSMatt Jacob struct g_consumer *cp; 756e770bc6bSMatt Jacob struct g_geom *gp, *gp1; 757e770bc6bSMatt Jacob int error, isnew; 758e770bc6bSMatt Jacob 759e770bc6bSMatt Jacob g_topology_assert(); 760e770bc6bSMatt Jacob 761e770bc6bSMatt Jacob gp = g_new_geomf(mp, "multipath:taste"); 762e770bc6bSMatt Jacob gp->start = g_multipath_start; 763e770bc6bSMatt Jacob gp->access = g_multipath_access; 764e770bc6bSMatt Jacob gp->orphan = g_multipath_orphan; 765e770bc6bSMatt Jacob cp = g_new_consumer(gp); 766e770bc6bSMatt Jacob g_attach(cp, pp); 767e770bc6bSMatt Jacob error = g_multipath_read_metadata(cp, &md); 768e770bc6bSMatt Jacob g_detach(cp); 769e770bc6bSMatt Jacob g_destroy_consumer(cp); 770e770bc6bSMatt Jacob g_destroy_geom(gp); 77112f35a61SPawel Jakub Dawidek if (error != 0) 772e770bc6bSMatt Jacob return (NULL); 773e770bc6bSMatt Jacob gp = NULL; 774e770bc6bSMatt Jacob 775e770bc6bSMatt Jacob if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) { 77612f35a61SPawel Jakub Dawidek if (g_multipath_debug) 777e770bc6bSMatt Jacob printf("%s is not MULTIPATH\n", pp->name); 778e770bc6bSMatt Jacob return (NULL); 779e770bc6bSMatt Jacob } 780e770bc6bSMatt Jacob if (md.md_version != G_MULTIPATH_VERSION) { 781e770bc6bSMatt Jacob printf("%s has version %d multipath id- this module is version " 782e770bc6bSMatt Jacob " %d: rejecting\n", pp->name, md.md_version, 783e770bc6bSMatt Jacob G_MULTIPATH_VERSION); 784e770bc6bSMatt Jacob return (NULL); 785e770bc6bSMatt Jacob } 7860c883cefSAlexander Motin if (md.md_size != 0 && md.md_size != pp->mediasize) 7870c883cefSAlexander Motin return (NULL); 7880c883cefSAlexander Motin if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize) 7890c883cefSAlexander Motin return (NULL); 79012f35a61SPawel Jakub Dawidek if (g_multipath_debug) 791e770bc6bSMatt Jacob printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid); 792e770bc6bSMatt Jacob 793e770bc6bSMatt Jacob /* 794e770bc6bSMatt Jacob * Let's check if such a device already is present. We check against 795e770bc6bSMatt Jacob * uuid alone first because that's the true distinguishor. If that 796e770bc6bSMatt Jacob * passes, then we check for name conflicts. If there are conflicts, 797e770bc6bSMatt Jacob * modify the name. 798e770bc6bSMatt Jacob * 799e770bc6bSMatt Jacob * The whole purpose of this is to solve the problem that people don't 800e770bc6bSMatt Jacob * pick good unique names, but good unique names (like uuids) are a 801e770bc6bSMatt Jacob * pain to use. So, we allow people to build GEOMs with friendly names 802e770bc6bSMatt Jacob * and uuids, and modify the names in case there's a collision. 803e770bc6bSMatt Jacob */ 804e770bc6bSMatt Jacob sc = NULL; 805e770bc6bSMatt Jacob LIST_FOREACH(gp, &mp->geom, geom) { 806e770bc6bSMatt Jacob sc = gp->softc; 8070c883cefSAlexander Motin if (sc == NULL || sc->sc_stopping) 808e770bc6bSMatt Jacob continue; 80912f35a61SPawel Jakub Dawidek if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0) 810e770bc6bSMatt Jacob break; 811e770bc6bSMatt Jacob } 812e770bc6bSMatt Jacob 813e770bc6bSMatt Jacob LIST_FOREACH(gp1, &mp->geom, geom) { 81412f35a61SPawel Jakub Dawidek if (gp1 == gp) 815e770bc6bSMatt Jacob continue; 816e770bc6bSMatt Jacob sc = gp1->softc; 8170c883cefSAlexander Motin if (sc == NULL || sc->sc_stopping) 818e770bc6bSMatt Jacob continue; 81912f35a61SPawel Jakub Dawidek if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0) 820e770bc6bSMatt Jacob break; 821e770bc6bSMatt Jacob } 822e770bc6bSMatt Jacob 823e770bc6bSMatt Jacob /* 824e770bc6bSMatt Jacob * If gp is NULL, we had no extant MULTIPATH geom with this uuid. 825e770bc6bSMatt Jacob * 826e770bc6bSMatt Jacob * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant 827e770bc6bSMatt Jacob * with the same name (but a different UUID). 828e770bc6bSMatt Jacob * 829e770bc6bSMatt Jacob * If gp is NULL, then modify the name with a random number and 830e770bc6bSMatt Jacob * complain, but allow the creation of the geom to continue. 831e770bc6bSMatt Jacob * 832e770bc6bSMatt Jacob * If gp is *not* NULL, just use the geom's name as we're attaching 833e770bc6bSMatt Jacob * this disk to the (previously generated) name. 834e770bc6bSMatt Jacob */ 835e770bc6bSMatt Jacob 836e770bc6bSMatt Jacob if (gp1) { 837e770bc6bSMatt Jacob sc = gp1->softc; 838e770bc6bSMatt Jacob if (gp == NULL) { 839e770bc6bSMatt Jacob char buf[16]; 840e770bc6bSMatt Jacob u_long rand = random(); 841e770bc6bSMatt Jacob 842e770bc6bSMatt Jacob snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand); 843e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: geom %s/%s exists already\n", 844e770bc6bSMatt Jacob sc->sc_name, sc->sc_uuid); 845e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n", 846e770bc6bSMatt Jacob md.md_uuid, buf); 847e770bc6bSMatt Jacob strlcpy(md.md_name, buf, sizeof(md.md_name)); 848e770bc6bSMatt Jacob } else { 849e770bc6bSMatt Jacob strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name)); 850e770bc6bSMatt Jacob } 851e770bc6bSMatt Jacob } 852e770bc6bSMatt Jacob 853e770bc6bSMatt Jacob if (gp == NULL) { 854e770bc6bSMatt Jacob gp = g_multipath_create(mp, &md); 855e770bc6bSMatt Jacob if (gp == NULL) { 856e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: cannot create geom %s/%s\n", 857e770bc6bSMatt Jacob md.md_name, md.md_uuid); 858e770bc6bSMatt Jacob return (NULL); 859e770bc6bSMatt Jacob } 860e770bc6bSMatt Jacob isnew = 1; 861e770bc6bSMatt Jacob } else { 862e770bc6bSMatt Jacob isnew = 0; 863e770bc6bSMatt Jacob } 864e770bc6bSMatt Jacob 865e770bc6bSMatt Jacob sc = gp->softc; 866e770bc6bSMatt Jacob KASSERT(sc != NULL, ("sc is NULL")); 867e770bc6bSMatt Jacob error = g_multipath_add_disk(gp, pp); 868e770bc6bSMatt Jacob if (error != 0) { 86912f35a61SPawel Jakub Dawidek if (isnew) 870e770bc6bSMatt Jacob g_multipath_destroy(gp); 871e770bc6bSMatt Jacob return (NULL); 872e770bc6bSMatt Jacob } 873e770bc6bSMatt Jacob return (gp); 874e770bc6bSMatt Jacob } 875e770bc6bSMatt Jacob 876e770bc6bSMatt Jacob static void 8770c883cefSAlexander Motin g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp, 8780c883cefSAlexander Motin const char *name) 879e770bc6bSMatt Jacob { 8800c883cefSAlexander Motin struct g_multipath_softc *sc; 881e770bc6bSMatt Jacob struct g_geom *gp; 8822b4969ffSMatt Jacob struct g_consumer *cp; 8830c883cefSAlexander Motin struct g_provider *pp; 8840c883cefSAlexander Motin const char *mpname; 885e770bc6bSMatt Jacob static const char devpf[6] = "/dev/"; 886e770bc6bSMatt Jacob 887e770bc6bSMatt Jacob g_topology_assert(); 888e770bc6bSMatt Jacob 889e770bc6bSMatt Jacob mpname = gctl_get_asciiparam(req, "arg0"); 890e770bc6bSMatt Jacob if (mpname == NULL) { 891e770bc6bSMatt Jacob gctl_error(req, "No 'arg0' argument"); 892e770bc6bSMatt Jacob return; 893e770bc6bSMatt Jacob } 8942b4969ffSMatt Jacob gp = g_multipath_find_geom(mp, mpname); 8952b4969ffSMatt Jacob if (gp == NULL) { 8962b4969ffSMatt Jacob gctl_error(req, "Device %s is invalid", mpname); 897e770bc6bSMatt Jacob return; 898e770bc6bSMatt Jacob } 8990c883cefSAlexander Motin sc = gp->softc; 900e770bc6bSMatt Jacob 90112f35a61SPawel Jakub Dawidek if (strncmp(name, devpf, 5) == 0) 902e770bc6bSMatt Jacob name += 5; 9032b4969ffSMatt Jacob pp = g_provider_by_name(name); 9042b4969ffSMatt Jacob if (pp == NULL) { 905e770bc6bSMatt Jacob gctl_error(req, "Provider %s is invalid", name); 906e770bc6bSMatt Jacob return; 907e770bc6bSMatt Jacob } 908e770bc6bSMatt Jacob 909e770bc6bSMatt Jacob /* 9100c883cefSAlexander Motin * Check to make sure parameters match. 911e770bc6bSMatt Jacob */ 9120c883cefSAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer) { 9130c883cefSAlexander Motin if (cp->provider == pp) { 9140c883cefSAlexander Motin gctl_error(req, "provider %s is already there", 9150c883cefSAlexander Motin pp->name); 916e770bc6bSMatt Jacob return; 917e770bc6bSMatt Jacob } 9180c883cefSAlexander Motin } 919*e6afd72bSAlexander Motin if (sc->sc_pp->mediasize != 0 && 9200c883cefSAlexander Motin sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0) 9210c883cefSAlexander Motin != pp->mediasize) { 9220c883cefSAlexander Motin gctl_error(req, "Providers size mismatch %jd != %jd", 9230c883cefSAlexander Motin (intmax_t) sc->sc_pp->mediasize + 9240c883cefSAlexander Motin (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0), 9250c883cefSAlexander Motin (intmax_t) pp->mediasize); 926e770bc6bSMatt Jacob return; 927e770bc6bSMatt Jacob } 928*e6afd72bSAlexander Motin if (sc->sc_pp->sectorsize != 0 && 9290c883cefSAlexander Motin sc->sc_pp->sectorsize != pp->sectorsize) { 9300c883cefSAlexander Motin gctl_error(req, "Providers sectorsize mismatch %u != %u", 9310c883cefSAlexander Motin sc->sc_pp->sectorsize, pp->sectorsize); 932e770bc6bSMatt Jacob return; 933e770bc6bSMatt Jacob } 934e770bc6bSMatt Jacob 935e770bc6bSMatt Jacob /* 9362b4969ffSMatt Jacob * Now add.... 937e770bc6bSMatt Jacob */ 9382b4969ffSMatt Jacob (void) g_multipath_add_disk(gp, pp); 939e770bc6bSMatt Jacob } 940e770bc6bSMatt Jacob 9410c883cefSAlexander Motin static void 94271ee4ef0SThomas Quinot g_multipath_ctl_prefer(struct gctl_req *req, struct g_class *mp) 94371ee4ef0SThomas Quinot { 94471ee4ef0SThomas Quinot struct g_geom *gp; 94571ee4ef0SThomas Quinot struct g_multipath_softc *sc; 94671ee4ef0SThomas Quinot struct g_consumer *cp; 94771ee4ef0SThomas Quinot const char *name, *mpname; 94871ee4ef0SThomas Quinot static const char devpf[6] = "/dev/"; 94971ee4ef0SThomas Quinot int *nargs; 95071ee4ef0SThomas Quinot 95171ee4ef0SThomas Quinot g_topology_assert(); 95271ee4ef0SThomas Quinot 95371ee4ef0SThomas Quinot mpname = gctl_get_asciiparam(req, "arg0"); 95471ee4ef0SThomas Quinot if (mpname == NULL) { 95571ee4ef0SThomas Quinot gctl_error(req, "No 'arg0' argument"); 95671ee4ef0SThomas Quinot return; 95771ee4ef0SThomas Quinot } 95871ee4ef0SThomas Quinot gp = g_multipath_find_geom(mp, mpname); 95971ee4ef0SThomas Quinot if (gp == NULL) { 96071ee4ef0SThomas Quinot gctl_error(req, "Device %s is invalid", mpname); 96171ee4ef0SThomas Quinot return; 96271ee4ef0SThomas Quinot } 96371ee4ef0SThomas Quinot sc = gp->softc; 96471ee4ef0SThomas Quinot 96571ee4ef0SThomas Quinot nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 96671ee4ef0SThomas Quinot if (nargs == NULL) { 96771ee4ef0SThomas Quinot gctl_error(req, "No 'nargs' argument"); 96871ee4ef0SThomas Quinot return; 96971ee4ef0SThomas Quinot } 97071ee4ef0SThomas Quinot if (*nargs != 2) { 97171ee4ef0SThomas Quinot gctl_error(req, "missing device"); 97271ee4ef0SThomas Quinot return; 97371ee4ef0SThomas Quinot } 97471ee4ef0SThomas Quinot 97571ee4ef0SThomas Quinot name = gctl_get_asciiparam(req, "arg1"); 97671ee4ef0SThomas Quinot if (name == NULL) { 97771ee4ef0SThomas Quinot gctl_error(req, "No 'arg1' argument"); 97871ee4ef0SThomas Quinot return; 97971ee4ef0SThomas Quinot } 98071ee4ef0SThomas Quinot if (strncmp(name, devpf, 5) == 0) { 98171ee4ef0SThomas Quinot name += 5; 98271ee4ef0SThomas Quinot } 98371ee4ef0SThomas Quinot 98471ee4ef0SThomas Quinot LIST_FOREACH(cp, &gp->consumer, consumer) { 98571ee4ef0SThomas Quinot if (cp->provider != NULL 98671ee4ef0SThomas Quinot && strcmp(cp->provider->name, name) == 0) 98771ee4ef0SThomas Quinot break; 98871ee4ef0SThomas Quinot } 98971ee4ef0SThomas Quinot 99071ee4ef0SThomas Quinot if (cp == NULL) { 99171ee4ef0SThomas Quinot gctl_error(req, "Provider %s not found", name); 99271ee4ef0SThomas Quinot return; 99371ee4ef0SThomas Quinot } 99471ee4ef0SThomas Quinot 99571ee4ef0SThomas Quinot mtx_lock(&sc->sc_mtx); 99671ee4ef0SThomas Quinot 99771ee4ef0SThomas Quinot if (cp->index & MP_BAD) { 99871ee4ef0SThomas Quinot gctl_error(req, "Consumer %s is invalid", name); 99971ee4ef0SThomas Quinot mtx_unlock(&sc->sc_mtx); 100071ee4ef0SThomas Quinot return; 100171ee4ef0SThomas Quinot } 100271ee4ef0SThomas Quinot 100371ee4ef0SThomas Quinot /* Here when the consumer is present and in good shape */ 100471ee4ef0SThomas Quinot 100571ee4ef0SThomas Quinot sc->sc_active = cp; 100671ee4ef0SThomas Quinot if (!sc->sc_active_active) 100771ee4ef0SThomas Quinot printf("GEOM_MULTIPATH: %s now active path in %s\n", 100871ee4ef0SThomas Quinot sc->sc_active->provider->name, sc->sc_name); 100971ee4ef0SThomas Quinot 101071ee4ef0SThomas Quinot mtx_unlock(&sc->sc_mtx); 101171ee4ef0SThomas Quinot } 101271ee4ef0SThomas Quinot 101371ee4ef0SThomas Quinot static void 10140c883cefSAlexander Motin g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp) 10150c883cefSAlexander Motin { 10160c883cefSAlexander Motin struct g_multipath_softc *sc; 10170c883cefSAlexander Motin struct g_geom *gp; 10180c883cefSAlexander Motin const char *mpname, *name; 10190c883cefSAlexander Motin 10200c883cefSAlexander Motin mpname = gctl_get_asciiparam(req, "arg0"); 10210c883cefSAlexander Motin if (mpname == NULL) { 10220c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 10230c883cefSAlexander Motin return; 10240c883cefSAlexander Motin } 10250c883cefSAlexander Motin gp = g_multipath_find_geom(mp, mpname); 10260c883cefSAlexander Motin if (gp == NULL) { 10270c883cefSAlexander Motin gctl_error(req, "Device %s not found", mpname); 10280c883cefSAlexander Motin return; 10290c883cefSAlexander Motin } 10300c883cefSAlexander Motin sc = gp->softc; 10310c883cefSAlexander Motin 10320c883cefSAlexander Motin name = gctl_get_asciiparam(req, "arg1"); 10330c883cefSAlexander Motin if (name == NULL) { 10340c883cefSAlexander Motin gctl_error(req, "No 'arg1' argument"); 10350c883cefSAlexander Motin return; 10360c883cefSAlexander Motin } 10370c883cefSAlexander Motin g_multipath_ctl_add_name(req, mp, name); 10380c883cefSAlexander Motin } 10390c883cefSAlexander Motin 10400c883cefSAlexander Motin static void 10410c883cefSAlexander Motin g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp) 10420c883cefSAlexander Motin { 10430c883cefSAlexander Motin struct g_multipath_metadata md; 10440c883cefSAlexander Motin struct g_multipath_softc *sc; 10450c883cefSAlexander Motin struct g_geom *gp; 10460c883cefSAlexander Motin const char *mpname, *name; 10470c883cefSAlexander Motin char param[16]; 104863297dfdSAlexander Motin int *nargs, i, *val; 10490c883cefSAlexander Motin 10500c883cefSAlexander Motin g_topology_assert(); 10510c883cefSAlexander Motin 10520c883cefSAlexander Motin nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 10530c883cefSAlexander Motin if (*nargs < 2) { 10540c883cefSAlexander Motin gctl_error(req, "wrong number of arguments."); 10550c883cefSAlexander Motin return; 10560c883cefSAlexander Motin } 10570c883cefSAlexander Motin 10580c883cefSAlexander Motin mpname = gctl_get_asciiparam(req, "arg0"); 10590c883cefSAlexander Motin if (mpname == NULL) { 10600c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 10610c883cefSAlexander Motin return; 10620c883cefSAlexander Motin } 10630c883cefSAlexander Motin gp = g_multipath_find_geom(mp, mpname); 10640c883cefSAlexander Motin if (gp != NULL) { 10650c883cefSAlexander Motin gctl_error(req, "Device %s already exist", mpname); 10660c883cefSAlexander Motin return; 10670c883cefSAlexander Motin } 10680c883cefSAlexander Motin sc = gp->softc; 10690c883cefSAlexander Motin 10700c883cefSAlexander Motin memset(&md, 0, sizeof(md)); 10710c883cefSAlexander Motin strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic)); 10720c883cefSAlexander Motin md.md_version = G_MULTIPATH_VERSION; 10730c883cefSAlexander Motin strlcpy(md.md_name, mpname, sizeof(md.md_name)); 10740c883cefSAlexander Motin md.md_size = 0; 10750c883cefSAlexander Motin md.md_sectorsize = 0; 10760c883cefSAlexander Motin md.md_uuid[0] = 0; 107763297dfdSAlexander Motin md.md_active_active = 0; 107863297dfdSAlexander Motin val = gctl_get_paraml(req, "active_active", sizeof(*val)); 107963297dfdSAlexander Motin if (val != NULL && *val != 0) 108063297dfdSAlexander Motin md.md_active_active = 1; 108163297dfdSAlexander Motin val = gctl_get_paraml(req, "active_read", sizeof(*val)); 108263297dfdSAlexander Motin if (val != NULL && *val != 0) 108363297dfdSAlexander Motin md.md_active_active = 2; 10840c883cefSAlexander Motin gp = g_multipath_create(mp, &md); 10850c883cefSAlexander Motin if (gp == NULL) { 10860c883cefSAlexander Motin gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n", 10870c883cefSAlexander Motin md.md_name, md.md_uuid); 10880c883cefSAlexander Motin return; 10890c883cefSAlexander Motin } 10900c883cefSAlexander Motin sc = gp->softc; 10910c883cefSAlexander Motin 10920c883cefSAlexander Motin for (i = 1; i < *nargs; i++) { 10930c883cefSAlexander Motin snprintf(param, sizeof(param), "arg%d", i); 10940c883cefSAlexander Motin name = gctl_get_asciiparam(req, param); 10950c883cefSAlexander Motin g_multipath_ctl_add_name(req, mp, name); 10960c883cefSAlexander Motin } 10970c883cefSAlexander Motin 10980c883cefSAlexander Motin if (sc->sc_ndisks != (*nargs - 1)) 10990c883cefSAlexander Motin g_multipath_destroy(gp); 11000c883cefSAlexander Motin } 11010c883cefSAlexander Motin 11020c883cefSAlexander Motin static void 110363297dfdSAlexander Motin g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp) 110463297dfdSAlexander Motin { 110563297dfdSAlexander Motin struct g_multipath_softc *sc; 110663297dfdSAlexander Motin struct g_geom *gp; 110763297dfdSAlexander Motin struct g_consumer *cp; 110863297dfdSAlexander Motin struct g_provider *pp; 1109c0b1ef66SAlexander Motin struct g_multipath_metadata md; 111063297dfdSAlexander Motin const char *name; 111163297dfdSAlexander Motin int error, *val; 111263297dfdSAlexander Motin void *buf; 111363297dfdSAlexander Motin 111463297dfdSAlexander Motin g_topology_assert(); 111563297dfdSAlexander Motin 111663297dfdSAlexander Motin name = gctl_get_asciiparam(req, "arg0"); 111763297dfdSAlexander Motin if (name == NULL) { 111863297dfdSAlexander Motin gctl_error(req, "No 'arg0' argument"); 111963297dfdSAlexander Motin return; 112063297dfdSAlexander Motin } 112163297dfdSAlexander Motin gp = g_multipath_find_geom(mp, name); 112263297dfdSAlexander Motin if (gp == NULL) { 112363297dfdSAlexander Motin gctl_error(req, "Device %s is invalid", name); 112463297dfdSAlexander Motin return; 112563297dfdSAlexander Motin } 112663297dfdSAlexander Motin sc = gp->softc; 112763297dfdSAlexander Motin val = gctl_get_paraml(req, "active_active", sizeof(*val)); 112863297dfdSAlexander Motin if (val != NULL && *val != 0) 112963297dfdSAlexander Motin sc->sc_active_active = 1; 113063297dfdSAlexander Motin val = gctl_get_paraml(req, "active_read", sizeof(*val)); 113163297dfdSAlexander Motin if (val != NULL && *val != 0) 113263297dfdSAlexander Motin sc->sc_active_active = 2; 113363297dfdSAlexander Motin val = gctl_get_paraml(req, "active_passive", sizeof(*val)); 113463297dfdSAlexander Motin if (val != NULL && *val != 0) 113563297dfdSAlexander Motin sc->sc_active_active = 0; 113663297dfdSAlexander Motin if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) { 113763297dfdSAlexander Motin cp = sc->sc_active; 113863297dfdSAlexander Motin pp = cp->provider; 113963297dfdSAlexander Motin error = g_access(cp, 1, 1, 1); 114063297dfdSAlexander Motin if (error != 0) { 114163297dfdSAlexander Motin gctl_error(req, "Can't open %s (%d)", pp->name, error); 114263297dfdSAlexander Motin return; 114363297dfdSAlexander Motin } 114463297dfdSAlexander Motin g_topology_unlock(); 1145c0b1ef66SAlexander Motin buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 1146c0b1ef66SAlexander Motin strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic)); 1147c0b1ef66SAlexander Motin memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid)); 1148c0b1ef66SAlexander Motin strlcpy(md.md_name, name, sizeof(md.md_name)); 1149c0b1ef66SAlexander Motin md.md_version = G_MULTIPATH_VERSION; 1150c0b1ef66SAlexander Motin md.md_size = pp->mediasize; 1151c0b1ef66SAlexander Motin md.md_sectorsize = pp->sectorsize; 1152c0b1ef66SAlexander Motin md.md_active_active = sc->sc_active_active; 1153c0b1ef66SAlexander Motin multipath_metadata_encode(&md, buf); 115463297dfdSAlexander Motin error = g_write_data(cp, pp->mediasize - pp->sectorsize, 115563297dfdSAlexander Motin buf, pp->sectorsize); 115663297dfdSAlexander Motin g_topology_lock(); 115763297dfdSAlexander Motin g_access(cp, -1, -1, -1); 115863297dfdSAlexander Motin if (error != 0) 115963297dfdSAlexander Motin gctl_error(req, "Can't update metadata on %s (%d)", 116063297dfdSAlexander Motin pp->name, error); 116163297dfdSAlexander Motin } 116263297dfdSAlexander Motin } 116363297dfdSAlexander Motin 116463297dfdSAlexander Motin static void 11650c883cefSAlexander Motin g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail) 11660c883cefSAlexander Motin { 11670c883cefSAlexander Motin struct g_multipath_softc *sc; 11680c883cefSAlexander Motin struct g_geom *gp; 11690c883cefSAlexander Motin struct g_consumer *cp; 11700c883cefSAlexander Motin const char *mpname, *name; 11710c883cefSAlexander Motin int found; 11720c883cefSAlexander Motin 11730c883cefSAlexander Motin mpname = gctl_get_asciiparam(req, "arg0"); 11740c883cefSAlexander Motin if (mpname == NULL) { 11750c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 11760c883cefSAlexander Motin return; 11770c883cefSAlexander Motin } 11780c883cefSAlexander Motin gp = g_multipath_find_geom(mp, mpname); 11790c883cefSAlexander Motin if (gp == NULL) { 11800c883cefSAlexander Motin gctl_error(req, "Device %s not found", mpname); 11810c883cefSAlexander Motin return; 11820c883cefSAlexander Motin } 11830c883cefSAlexander Motin sc = gp->softc; 11840c883cefSAlexander Motin 11850c883cefSAlexander Motin name = gctl_get_asciiparam(req, "arg1"); 11860c883cefSAlexander Motin if (name == NULL) { 11870c883cefSAlexander Motin gctl_error(req, "No 'arg1' argument"); 11880c883cefSAlexander Motin return; 11890c883cefSAlexander Motin } 11900c883cefSAlexander Motin 11910c883cefSAlexander Motin found = 0; 11920c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 11930c883cefSAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer) { 11940c883cefSAlexander Motin if (cp->provider != NULL && 11950c883cefSAlexander Motin strcmp(cp->provider->name, name) == 0 && 11960c883cefSAlexander Motin (cp->index & MP_LOST) == 0) { 11970c883cefSAlexander Motin found = 1; 119863297dfdSAlexander Motin if (!fail == !(cp->index & MP_FAIL)) 119963297dfdSAlexander Motin continue; 12000c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s in %s is marked %s.\n", 12010c883cefSAlexander Motin name, sc->sc_name, fail ? "FAIL" : "OK"); 12020c883cefSAlexander Motin if (fail) { 12030c883cefSAlexander Motin g_multipath_fault(cp, MP_FAIL); 12040c883cefSAlexander Motin } else { 12050c883cefSAlexander Motin cp->index &= ~MP_FAIL; 12060c883cefSAlexander Motin } 12070c883cefSAlexander Motin } 12080c883cefSAlexander Motin } 12090c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 12100c883cefSAlexander Motin if (found == 0) 12110c883cefSAlexander Motin gctl_error(req, "Provider %s not found", name); 12120c883cefSAlexander Motin } 12130c883cefSAlexander Motin 12140c883cefSAlexander Motin static void 12150c883cefSAlexander Motin g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp) 12160c883cefSAlexander Motin { 12170c883cefSAlexander Motin struct g_multipath_softc *sc; 12180c883cefSAlexander Motin struct g_geom *gp; 12190c883cefSAlexander Motin struct g_consumer *cp, *cp1; 12200c883cefSAlexander Motin const char *mpname, *name; 12210c883cefSAlexander Motin uintptr_t *cnt; 12220c883cefSAlexander Motin int found; 12230c883cefSAlexander Motin 12240c883cefSAlexander Motin mpname = gctl_get_asciiparam(req, "arg0"); 12250c883cefSAlexander Motin if (mpname == NULL) { 12260c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 12270c883cefSAlexander Motin return; 12280c883cefSAlexander Motin } 12290c883cefSAlexander Motin gp = g_multipath_find_geom(mp, mpname); 12300c883cefSAlexander Motin if (gp == NULL) { 12310c883cefSAlexander Motin gctl_error(req, "Device %s not found", mpname); 12320c883cefSAlexander Motin return; 12330c883cefSAlexander Motin } 12340c883cefSAlexander Motin sc = gp->softc; 12350c883cefSAlexander Motin 12360c883cefSAlexander Motin name = gctl_get_asciiparam(req, "arg1"); 12370c883cefSAlexander Motin if (name == NULL) { 12380c883cefSAlexander Motin gctl_error(req, "No 'arg1' argument"); 12390c883cefSAlexander Motin return; 12400c883cefSAlexander Motin } 12410c883cefSAlexander Motin 12420c883cefSAlexander Motin found = 0; 12430c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 12440c883cefSAlexander Motin LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) { 12450c883cefSAlexander Motin if (cp->provider != NULL && 12460c883cefSAlexander Motin strcmp(cp->provider->name, name) == 0 && 12470c883cefSAlexander Motin (cp->index & MP_LOST) == 0) { 12480c883cefSAlexander Motin found = 1; 12490c883cefSAlexander Motin printf("GEOM_MULTIPATH: removing %s from %s\n", 12500c883cefSAlexander Motin cp->provider->name, cp->geom->name); 12510c883cefSAlexander Motin sc->sc_ndisks--; 12520c883cefSAlexander Motin g_multipath_fault(cp, MP_LOST); 12530c883cefSAlexander Motin cnt = (uintptr_t *)&cp->private; 12540c883cefSAlexander Motin if (*cnt == 0 && (cp->index & MP_POSTED) == 0) { 12550c883cefSAlexander Motin cp->index |= MP_POSTED; 12560c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 12570c883cefSAlexander Motin g_mpd(cp, 0); 12580c883cefSAlexander Motin if (cp1 == NULL) 12590c883cefSAlexander Motin return; /* Recursion happened. */ 12600c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 12610c883cefSAlexander Motin } 12620c883cefSAlexander Motin } 12630c883cefSAlexander Motin } 12640c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 12650c883cefSAlexander Motin if (found == 0) 12660c883cefSAlexander Motin gctl_error(req, "Provider %s not found", name); 12670c883cefSAlexander Motin } 12680c883cefSAlexander Motin 1269e770bc6bSMatt Jacob static struct g_geom * 1270e770bc6bSMatt Jacob g_multipath_find_geom(struct g_class *mp, const char *name) 1271e770bc6bSMatt Jacob { 1272e770bc6bSMatt Jacob struct g_geom *gp; 12730c883cefSAlexander Motin struct g_multipath_softc *sc; 1274e770bc6bSMatt Jacob 1275e770bc6bSMatt Jacob LIST_FOREACH(gp, &mp->geom, geom) { 12760c883cefSAlexander Motin sc = gp->softc; 12770c883cefSAlexander Motin if (sc == NULL || sc->sc_stopping) 12780c883cefSAlexander Motin continue; 12790c883cefSAlexander Motin if (strcmp(gp->name, name) == 0) 1280e770bc6bSMatt Jacob return (gp); 1281e770bc6bSMatt Jacob } 1282e770bc6bSMatt Jacob return (NULL); 1283e770bc6bSMatt Jacob } 1284e770bc6bSMatt Jacob 1285e770bc6bSMatt Jacob static void 12860c883cefSAlexander Motin g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp) 1287e770bc6bSMatt Jacob { 1288e770bc6bSMatt Jacob struct g_geom *gp; 1289e770bc6bSMatt Jacob const char *name; 1290e770bc6bSMatt Jacob int error; 1291e770bc6bSMatt Jacob 1292e770bc6bSMatt Jacob g_topology_assert(); 1293e770bc6bSMatt Jacob 1294e770bc6bSMatt Jacob name = gctl_get_asciiparam(req, "arg0"); 1295e770bc6bSMatt Jacob if (name == NULL) { 1296e770bc6bSMatt Jacob gctl_error(req, "No 'arg0' argument"); 1297e770bc6bSMatt Jacob return; 1298e770bc6bSMatt Jacob } 1299e770bc6bSMatt Jacob gp = g_multipath_find_geom(mp, name); 1300e770bc6bSMatt Jacob if (gp == NULL) { 1301e770bc6bSMatt Jacob gctl_error(req, "Device %s is invalid", name); 1302e770bc6bSMatt Jacob return; 1303e770bc6bSMatt Jacob } 1304e770bc6bSMatt Jacob error = g_multipath_destroy(gp); 13050c883cefSAlexander Motin if (error != 0 && error != EINPROGRESS) 13060c883cefSAlexander Motin gctl_error(req, "failed to stop %s (err=%d)", name, error); 1307e770bc6bSMatt Jacob } 13080c883cefSAlexander Motin 13090c883cefSAlexander Motin static void 13100c883cefSAlexander Motin g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp) 13110c883cefSAlexander Motin { 13120c883cefSAlexander Motin struct g_geom *gp; 13130c883cefSAlexander Motin struct g_multipath_softc *sc; 13140c883cefSAlexander Motin struct g_consumer *cp; 13150c883cefSAlexander Motin struct g_provider *pp; 13160c883cefSAlexander Motin const char *name; 13170c883cefSAlexander Motin uint8_t *buf; 13180c883cefSAlexander Motin int error; 13190c883cefSAlexander Motin 13200c883cefSAlexander Motin g_topology_assert(); 13210c883cefSAlexander Motin 13220c883cefSAlexander Motin name = gctl_get_asciiparam(req, "arg0"); 13230c883cefSAlexander Motin if (name == NULL) { 13240c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 13250c883cefSAlexander Motin return; 13260c883cefSAlexander Motin } 13270c883cefSAlexander Motin gp = g_multipath_find_geom(mp, name); 13280c883cefSAlexander Motin if (gp == NULL) { 13290c883cefSAlexander Motin gctl_error(req, "Device %s is invalid", name); 13300c883cefSAlexander Motin return; 13310c883cefSAlexander Motin } 13320c883cefSAlexander Motin sc = gp->softc; 13330c883cefSAlexander Motin 13340c883cefSAlexander Motin if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) { 13350c883cefSAlexander Motin cp = sc->sc_active; 13360c883cefSAlexander Motin pp = cp->provider; 13370c883cefSAlexander Motin error = g_access(cp, 1, 1, 1); 13380c883cefSAlexander Motin if (error != 0) { 13390c883cefSAlexander Motin gctl_error(req, "Can't open %s (%d)", pp->name, error); 13400c883cefSAlexander Motin goto destroy; 13410c883cefSAlexander Motin } 13420c883cefSAlexander Motin g_topology_unlock(); 13430c883cefSAlexander Motin buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 13440c883cefSAlexander Motin error = g_write_data(cp, pp->mediasize - pp->sectorsize, 13450c883cefSAlexander Motin buf, pp->sectorsize); 13460c883cefSAlexander Motin g_topology_lock(); 13470c883cefSAlexander Motin g_access(cp, -1, -1, -1); 13480c883cefSAlexander Motin if (error != 0) 13490c883cefSAlexander Motin gctl_error(req, "Can't erase metadata on %s (%d)", 13500c883cefSAlexander Motin pp->name, error); 13510c883cefSAlexander Motin } 13520c883cefSAlexander Motin 13530c883cefSAlexander Motin destroy: 13540c883cefSAlexander Motin error = g_multipath_destroy(gp); 13550c883cefSAlexander Motin if (error != 0 && error != EINPROGRESS) 13560c883cefSAlexander Motin gctl_error(req, "failed to destroy %s (err=%d)", name, error); 1357e770bc6bSMatt Jacob } 1358e770bc6bSMatt Jacob 1359e770bc6bSMatt Jacob static void 1360b5dce617SMatt Jacob g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp) 1361b5dce617SMatt Jacob { 1362b5dce617SMatt Jacob struct g_geom *gp; 1363b5dce617SMatt Jacob const char *name; 1364b5dce617SMatt Jacob int error; 1365b5dce617SMatt Jacob 1366b5dce617SMatt Jacob g_topology_assert(); 1367b5dce617SMatt Jacob 1368b5dce617SMatt Jacob name = gctl_get_asciiparam(req, "arg0"); 1369b5dce617SMatt Jacob if (name == NULL) { 1370b5dce617SMatt Jacob gctl_error(req, "No 'arg0' argument"); 1371b5dce617SMatt Jacob return; 1372b5dce617SMatt Jacob } 1373b5dce617SMatt Jacob gp = g_multipath_find_geom(mp, name); 1374b5dce617SMatt Jacob if (gp == NULL) { 1375b5dce617SMatt Jacob gctl_error(req, "Device %s is invalid", name); 1376b5dce617SMatt Jacob return; 1377b5dce617SMatt Jacob } 1378b5dce617SMatt Jacob error = g_multipath_rotate(gp); 1379b5dce617SMatt Jacob if (error != 0) { 1380b5dce617SMatt Jacob gctl_error(req, "failed to rotate %s (err=%d)", name, error); 1381b5dce617SMatt Jacob } 1382b5dce617SMatt Jacob } 1383b5dce617SMatt Jacob 1384b5dce617SMatt Jacob static void 1385b5dce617SMatt Jacob g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp) 1386b5dce617SMatt Jacob { 1387b5dce617SMatt Jacob struct sbuf *sb; 1388b5dce617SMatt Jacob struct g_geom *gp; 1389b5dce617SMatt Jacob struct g_multipath_softc *sc; 13900c883cefSAlexander Motin struct g_consumer *cp; 1391b5dce617SMatt Jacob const char *name; 13920c883cefSAlexander Motin int empty; 1393b5dce617SMatt Jacob 1394b5dce617SMatt Jacob sb = sbuf_new_auto(); 1395b5dce617SMatt Jacob 1396b5dce617SMatt Jacob g_topology_assert(); 1397b5dce617SMatt Jacob name = gctl_get_asciiparam(req, "arg0"); 1398b5dce617SMatt Jacob if (name == NULL) { 1399b5dce617SMatt Jacob gctl_error(req, "No 'arg0' argument"); 1400b5dce617SMatt Jacob return; 1401b5dce617SMatt Jacob } 1402b5dce617SMatt Jacob gp = g_multipath_find_geom(mp, name); 1403b5dce617SMatt Jacob if (gp == NULL) { 1404b5dce617SMatt Jacob gctl_error(req, "Device %s is invalid", name); 1405b5dce617SMatt Jacob return; 1406b5dce617SMatt Jacob } 1407b5dce617SMatt Jacob sc = gp->softc; 140863297dfdSAlexander Motin if (sc->sc_active_active == 1) { 14090c883cefSAlexander Motin empty = 1; 14100c883cefSAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer) { 14110c883cefSAlexander Motin if (cp->index & MP_BAD) 14120c883cefSAlexander Motin continue; 14130c883cefSAlexander Motin if (!empty) 14140c883cefSAlexander Motin sbuf_cat(sb, " "); 14150c883cefSAlexander Motin sbuf_cat(sb, cp->provider->name); 14160c883cefSAlexander Motin empty = 0; 14170c883cefSAlexander Motin } 14180c883cefSAlexander Motin if (empty) 14190c883cefSAlexander Motin sbuf_cat(sb, "none"); 14200c883cefSAlexander Motin sbuf_cat(sb, "\n"); 14210c883cefSAlexander Motin } else if (sc->sc_active && sc->sc_active->provider) { 14220c883cefSAlexander Motin sbuf_printf(sb, "%s\n", sc->sc_active->provider->name); 1423b5dce617SMatt Jacob } else { 1424b5dce617SMatt Jacob sbuf_printf(sb, "none\n"); 1425b5dce617SMatt Jacob } 1426b5dce617SMatt Jacob sbuf_finish(sb); 1427b5dce617SMatt Jacob gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1); 1428b5dce617SMatt Jacob sbuf_delete(sb); 1429b5dce617SMatt Jacob } 1430b5dce617SMatt Jacob 1431b5dce617SMatt Jacob static void 1432e770bc6bSMatt Jacob g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb) 1433e770bc6bSMatt Jacob { 1434e770bc6bSMatt Jacob uint32_t *version; 1435e770bc6bSMatt Jacob g_topology_assert(); 1436e770bc6bSMatt Jacob version = gctl_get_paraml(req, "version", sizeof(*version)); 1437e770bc6bSMatt Jacob if (version == NULL) { 1438e770bc6bSMatt Jacob gctl_error(req, "No 'version' argument"); 1439e770bc6bSMatt Jacob } else if (*version != G_MULTIPATH_VERSION) { 1440e770bc6bSMatt Jacob gctl_error(req, "Userland and kernel parts are out of sync"); 14412b4969ffSMatt Jacob } else if (strcmp(verb, "add") == 0) { 14422b4969ffSMatt Jacob g_multipath_ctl_add(req, mp); 144371ee4ef0SThomas Quinot } else if (strcmp(verb, "prefer") == 0) { 144471ee4ef0SThomas Quinot g_multipath_ctl_prefer(req, mp); 14450c883cefSAlexander Motin } else if (strcmp(verb, "create") == 0) { 14460c883cefSAlexander Motin g_multipath_ctl_create(req, mp); 144763297dfdSAlexander Motin } else if (strcmp(verb, "configure") == 0) { 144863297dfdSAlexander Motin g_multipath_ctl_configure(req, mp); 14490c883cefSAlexander Motin } else if (strcmp(verb, "stop") == 0) { 14500c883cefSAlexander Motin g_multipath_ctl_stop(req, mp); 1451e770bc6bSMatt Jacob } else if (strcmp(verb, "destroy") == 0) { 1452e770bc6bSMatt Jacob g_multipath_ctl_destroy(req, mp); 14530c883cefSAlexander Motin } else if (strcmp(verb, "fail") == 0) { 14540c883cefSAlexander Motin g_multipath_ctl_fail(req, mp, 1); 14550c883cefSAlexander Motin } else if (strcmp(verb, "restore") == 0) { 14560c883cefSAlexander Motin g_multipath_ctl_fail(req, mp, 0); 14570c883cefSAlexander Motin } else if (strcmp(verb, "remove") == 0) { 14580c883cefSAlexander Motin g_multipath_ctl_remove(req, mp); 1459b5dce617SMatt Jacob } else if (strcmp(verb, "rotate") == 0) { 1460b5dce617SMatt Jacob g_multipath_ctl_rotate(req, mp); 1461b5dce617SMatt Jacob } else if (strcmp(verb, "getactive") == 0) { 1462b5dce617SMatt Jacob g_multipath_ctl_getactive(req, mp); 1463e770bc6bSMatt Jacob } else { 1464e770bc6bSMatt Jacob gctl_error(req, "Unknown verb %s", verb); 1465e770bc6bSMatt Jacob } 1466e770bc6bSMatt Jacob } 14670c883cefSAlexander Motin 14680c883cefSAlexander Motin static void 14690c883cefSAlexander Motin g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 14700c883cefSAlexander Motin struct g_consumer *cp, struct g_provider *pp) 14710c883cefSAlexander Motin { 14720c883cefSAlexander Motin struct g_multipath_softc *sc; 14730c883cefSAlexander Motin int good; 14740c883cefSAlexander Motin 14750c883cefSAlexander Motin g_topology_assert(); 14760c883cefSAlexander Motin 14770c883cefSAlexander Motin sc = gp->softc; 14780c883cefSAlexander Motin if (sc == NULL) 14790c883cefSAlexander Motin return; 14800c883cefSAlexander Motin if (cp != NULL) { 1481a839e332SAlexander Motin sbuf_printf(sb, "%s<State>%s</State>\n", indent, 14820c883cefSAlexander Motin (cp->index & MP_NEW) ? "NEW" : 14830c883cefSAlexander Motin (cp->index & MP_LOST) ? "LOST" : 14840c883cefSAlexander Motin (cp->index & MP_FAIL) ? "FAIL" : 148563297dfdSAlexander Motin (sc->sc_active_active == 1 || sc->sc_active == cp) ? 148663297dfdSAlexander Motin "ACTIVE" : 148763297dfdSAlexander Motin sc->sc_active_active == 2 ? "READ" : "PASSIVE"); 14880c883cefSAlexander Motin } else { 14890c883cefSAlexander Motin good = g_multipath_good(gp); 1490a839e332SAlexander Motin sbuf_printf(sb, "%s<State>%s</State>\n", indent, 14910c883cefSAlexander Motin good == 0 ? "BROKEN" : 14920c883cefSAlexander Motin (good != sc->sc_ndisks || sc->sc_ndisks == 1) ? 14930c883cefSAlexander Motin "DEGRADED" : "OPTIMAL"); 14940c883cefSAlexander Motin } 14950c883cefSAlexander Motin if (cp == NULL && pp == NULL) { 1496a839e332SAlexander Motin sbuf_printf(sb, "%s<UUID>%s</UUID>\n", indent, sc->sc_uuid); 1497a839e332SAlexander Motin sbuf_printf(sb, "%s<Mode>Active/%s</Mode>\n", indent, 149863297dfdSAlexander Motin sc->sc_active_active == 2 ? "Read" : 149963297dfdSAlexander Motin sc->sc_active_active == 1 ? "Active" : "Passive"); 1500a839e332SAlexander Motin sbuf_printf(sb, "%s<Type>%s</Type>\n", indent, 15010c883cefSAlexander Motin sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC"); 15020c883cefSAlexander Motin } 15030c883cefSAlexander Motin } 15040c883cefSAlexander Motin 1505e770bc6bSMatt Jacob DECLARE_GEOM_CLASS(g_multipath_class, g_multipath); 1506