1e770bc6bSMatt Jacob /*- 23728855aSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 33728855aSPedro F. Giffuni * 4e6afd72bSAlexander Motin * Copyright (c) 2011-2013 Alexander Motin <mav@FreeBSD.org> 5e770bc6bSMatt Jacob * Copyright (c) 2006-2007 Matthew Jacob <mjacob@FreeBSD.org> 6e770bc6bSMatt Jacob * All rights reserved. 7e770bc6bSMatt Jacob * 8e770bc6bSMatt Jacob * Redistribution and use in source and binary forms, with or without 9e770bc6bSMatt Jacob * modification, are permitted provided that the following conditions 10e770bc6bSMatt Jacob * are met: 11e770bc6bSMatt Jacob * 1. Redistributions of source code must retain the above copyright 12e770bc6bSMatt Jacob * notice, this list of conditions and the following disclaimer. 13e770bc6bSMatt Jacob * 2. Redistributions in binary form must reproduce the above copyright 14e770bc6bSMatt Jacob * notice, this list of conditions and the following disclaimer in the 15e770bc6bSMatt Jacob * documentation and/or other materials provided with the distribution. 16e770bc6bSMatt Jacob * 17e770bc6bSMatt Jacob * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 18e770bc6bSMatt Jacob * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19e770bc6bSMatt Jacob * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20e770bc6bSMatt Jacob * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 21e770bc6bSMatt Jacob * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22e770bc6bSMatt Jacob * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23e770bc6bSMatt Jacob * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24e770bc6bSMatt Jacob * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25e770bc6bSMatt Jacob * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26e770bc6bSMatt Jacob * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27e770bc6bSMatt Jacob * SUCH DAMAGE. 28e770bc6bSMatt Jacob */ 29e770bc6bSMatt Jacob /* 30e770bc6bSMatt Jacob * Based upon work by Pawel Jakub Dawidek <pjd@FreeBSD.org> for all of the 31e770bc6bSMatt Jacob * fine geom examples, and by Poul Henning Kamp <phk@FreeBSD.org> for GEOM 32e770bc6bSMatt Jacob * itself, all of which is most gratefully acknowledged. 33e770bc6bSMatt Jacob */ 34e770bc6bSMatt Jacob 35e770bc6bSMatt Jacob #include <sys/cdefs.h> 36e770bc6bSMatt Jacob __FBSDID("$FreeBSD$"); 37e770bc6bSMatt Jacob #include <sys/param.h> 38e770bc6bSMatt Jacob #include <sys/systm.h> 39e770bc6bSMatt Jacob #include <sys/kernel.h> 40e770bc6bSMatt Jacob #include <sys/module.h> 41e6afd72bSAlexander Motin #include <sys/limits.h> 42e770bc6bSMatt Jacob #include <sys/lock.h> 43e770bc6bSMatt Jacob #include <sys/mutex.h> 44e770bc6bSMatt Jacob #include <sys/bio.h> 455d807a0eSAndrey V. Elsukov #include <sys/sbuf.h> 4667f72211SAlan Somers #include <sys/sdt.h> 47e770bc6bSMatt Jacob #include <sys/sysctl.h> 48e770bc6bSMatt Jacob #include <sys/kthread.h> 49e770bc6bSMatt Jacob #include <sys/malloc.h> 50e770bc6bSMatt Jacob #include <geom/geom.h> 51e770bc6bSMatt Jacob #include <geom/multipath/g_multipath.h> 52e770bc6bSMatt Jacob 53cb08c2ccSAlexander Leidinger FEATURE(geom_multipath, "GEOM multipath support"); 54e770bc6bSMatt Jacob 55e770bc6bSMatt Jacob SYSCTL_DECL(_kern_geom); 567029da5cSPawel Biernacki static SYSCTL_NODE(_kern_geom, OID_AUTO, multipath, 577029da5cSPawel Biernacki CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 58e770bc6bSMatt Jacob "GEOM_MULTIPATH tunables"); 59e770bc6bSMatt Jacob static u_int g_multipath_debug = 0; 60e770bc6bSMatt Jacob SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, debug, CTLFLAG_RW, 61e770bc6bSMatt Jacob &g_multipath_debug, 0, "Debug level"); 620c883cefSAlexander Motin static u_int g_multipath_exclusive = 1; 630c883cefSAlexander Motin SYSCTL_UINT(_kern_geom_multipath, OID_AUTO, exclusive, CTLFLAG_RW, 640c883cefSAlexander Motin &g_multipath_exclusive, 0, "Exclusively open providers"); 65e770bc6bSMatt Jacob 6667f72211SAlan Somers SDT_PROVIDER_DECLARE(geom); 6767f72211SAlan Somers SDT_PROBE_DEFINE2(geom, multipath, config, restore, "char*", "char*"); 6867f72211SAlan Somers SDT_PROBE_DEFINE2(geom, multipath, config, remove, "char*", "char*"); 6967f72211SAlan Somers SDT_PROBE_DEFINE2(geom, multipath, config, disconnect, "char*", "char*"); 7067f72211SAlan Somers SDT_PROBE_DEFINE3(geom, multipath, config, fail, "char*", "char*", "int"); 7167f72211SAlan Somers SDT_PROBE_DEFINE2(geom, multipath, config, taste, "char*", "char*"); 7267f72211SAlan Somers SDT_PROBE_DEFINE2(geom, multipath, io, restart, "struct bio*", "struct bio*"); 7367f72211SAlan Somers 74e770bc6bSMatt Jacob static enum { 75e770bc6bSMatt Jacob GKT_NIL, 76e770bc6bSMatt Jacob GKT_RUN, 77e770bc6bSMatt Jacob GKT_DIE 78e770bc6bSMatt Jacob } g_multipath_kt_state; 79e770bc6bSMatt Jacob static struct bio_queue_head gmtbq; 80e770bc6bSMatt Jacob static struct mtx gmtbq_mtx; 81e770bc6bSMatt Jacob 82f8c79813SAlexander Motin static int g_multipath_read_metadata(struct g_consumer *cp, 83f8c79813SAlexander Motin struct g_multipath_metadata *md); 84f8c79813SAlexander Motin static int g_multipath_write_metadata(struct g_consumer *cp, 85f8c79813SAlexander Motin struct g_multipath_metadata *md); 86f8c79813SAlexander Motin 87e770bc6bSMatt Jacob static void g_multipath_orphan(struct g_consumer *); 88e6afd72bSAlexander Motin static void g_multipath_resize(struct g_consumer *); 89e770bc6bSMatt Jacob static void g_multipath_start(struct bio *); 90e770bc6bSMatt Jacob static void g_multipath_done(struct bio *); 91e770bc6bSMatt Jacob static void g_multipath_done_error(struct bio *); 92e770bc6bSMatt Jacob static void g_multipath_kt(void *); 93e770bc6bSMatt Jacob 94e770bc6bSMatt Jacob static int g_multipath_destroy(struct g_geom *); 95e770bc6bSMatt Jacob static int 96e770bc6bSMatt Jacob g_multipath_destroy_geom(struct gctl_req *, struct g_class *, struct g_geom *); 97e770bc6bSMatt Jacob 982b4969ffSMatt Jacob static struct g_geom *g_multipath_find_geom(struct g_class *, const char *); 99b5dce617SMatt Jacob static int g_multipath_rotate(struct g_geom *); 100b5dce617SMatt Jacob 101e770bc6bSMatt Jacob static g_taste_t g_multipath_taste; 102e770bc6bSMatt Jacob static g_ctl_req_t g_multipath_config; 103e770bc6bSMatt Jacob static g_init_t g_multipath_init; 104e770bc6bSMatt Jacob static g_fini_t g_multipath_fini; 1050c883cefSAlexander Motin static g_dumpconf_t g_multipath_dumpconf; 106e770bc6bSMatt Jacob 107e770bc6bSMatt Jacob struct g_class g_multipath_class = { 108e770bc6bSMatt Jacob .name = G_MULTIPATH_CLASS_NAME, 109e770bc6bSMatt Jacob .version = G_VERSION, 110e770bc6bSMatt Jacob .ctlreq = g_multipath_config, 111e770bc6bSMatt Jacob .taste = g_multipath_taste, 112e770bc6bSMatt Jacob .destroy_geom = g_multipath_destroy_geom, 113e770bc6bSMatt Jacob .init = g_multipath_init, 114e770bc6bSMatt Jacob .fini = g_multipath_fini 115e770bc6bSMatt Jacob }; 116e770bc6bSMatt Jacob 1170c883cefSAlexander Motin #define MP_FAIL 0x00000001 1180c883cefSAlexander Motin #define MP_LOST 0x00000002 1190c883cefSAlexander Motin #define MP_NEW 0x00000004 1200c883cefSAlexander Motin #define MP_POSTED 0x00000008 1210c883cefSAlexander Motin #define MP_BAD (MP_FAIL | MP_LOST | MP_NEW) 12225080ac4SSteven Hartland #define MP_WITHER 0x00000010 12325080ac4SSteven Hartland #define MP_IDLE 0x00000020 12425080ac4SSteven Hartland #define MP_IDLE_MASK 0xffffffe0 1250c883cefSAlexander Motin 1260c883cefSAlexander Motin static int 1270c883cefSAlexander Motin g_multipath_good(struct g_geom *gp) 1280c883cefSAlexander Motin { 1290c883cefSAlexander Motin struct g_consumer *cp; 1300c883cefSAlexander Motin int n = 0; 1310c883cefSAlexander Motin 1320c883cefSAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer) { 1330c883cefSAlexander Motin if ((cp->index & MP_BAD) == 0) 1340c883cefSAlexander Motin n++; 1350c883cefSAlexander Motin } 1360c883cefSAlexander Motin return (n); 1370c883cefSAlexander Motin } 1380c883cefSAlexander Motin 1390c883cefSAlexander Motin static void 1400c883cefSAlexander Motin g_multipath_fault(struct g_consumer *cp, int cause) 1410c883cefSAlexander Motin { 1420c883cefSAlexander Motin struct g_multipath_softc *sc; 1430c883cefSAlexander Motin struct g_consumer *lcp; 1440c883cefSAlexander Motin struct g_geom *gp; 1450c883cefSAlexander Motin 1460c883cefSAlexander Motin gp = cp->geom; 1470c883cefSAlexander Motin sc = gp->softc; 1480c883cefSAlexander Motin cp->index |= cause; 1490c883cefSAlexander Motin if (g_multipath_good(gp) == 0 && sc->sc_ndisks > 0) { 1500c883cefSAlexander Motin LIST_FOREACH(lcp, &gp->consumer, consumer) { 1510c883cefSAlexander Motin if (lcp->provider == NULL || 1520c883cefSAlexander Motin (lcp->index & (MP_LOST | MP_NEW))) 1530c883cefSAlexander Motin continue; 1540c883cefSAlexander Motin if (sc->sc_ndisks > 1 && lcp == cp) 1550c883cefSAlexander Motin continue; 1560c883cefSAlexander Motin printf("GEOM_MULTIPATH: " 1570c883cefSAlexander Motin "all paths in %s were marked FAIL, restore %s\n", 1580c883cefSAlexander Motin sc->sc_name, lcp->provider->name); 15967f72211SAlan Somers SDT_PROBE2(geom, multipath, config, restore, 16067f72211SAlan Somers sc->sc_name, lcp->provider->name); 1610c883cefSAlexander Motin lcp->index &= ~MP_FAIL; 1620c883cefSAlexander Motin } 1630c883cefSAlexander Motin } 1640c883cefSAlexander Motin if (cp != sc->sc_active) 1650c883cefSAlexander Motin return; 1660c883cefSAlexander Motin sc->sc_active = NULL; 1670c883cefSAlexander Motin LIST_FOREACH(lcp, &gp->consumer, consumer) { 1680c883cefSAlexander Motin if ((lcp->index & MP_BAD) == 0) { 1690c883cefSAlexander Motin sc->sc_active = lcp; 1700c883cefSAlexander Motin break; 1710c883cefSAlexander Motin } 1720c883cefSAlexander Motin } 1730c883cefSAlexander Motin if (sc->sc_active == NULL) { 1740c883cefSAlexander Motin printf("GEOM_MULTIPATH: out of providers for %s\n", 1750c883cefSAlexander Motin sc->sc_name); 17663297dfdSAlexander Motin } else if (sc->sc_active_active != 1) { 1770c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s is now active path in %s\n", 1780c883cefSAlexander Motin sc->sc_active->provider->name, sc->sc_name); 1790c883cefSAlexander Motin } 1800c883cefSAlexander Motin } 1810c883cefSAlexander Motin 1820c883cefSAlexander Motin static struct g_consumer * 18363297dfdSAlexander Motin g_multipath_choose(struct g_geom *gp, struct bio *bp) 1840c883cefSAlexander Motin { 1850c883cefSAlexander Motin struct g_multipath_softc *sc; 1860c883cefSAlexander Motin struct g_consumer *best, *cp; 1870c883cefSAlexander Motin 1880c883cefSAlexander Motin sc = gp->softc; 18963297dfdSAlexander Motin if (sc->sc_active_active == 0 || 19063297dfdSAlexander Motin (sc->sc_active_active == 2 && bp->bio_cmd != BIO_READ)) 1910c883cefSAlexander Motin return (sc->sc_active); 1920c883cefSAlexander Motin best = NULL; 1930c883cefSAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer) { 1940c883cefSAlexander Motin if (cp->index & MP_BAD) 1950c883cefSAlexander Motin continue; 1960c883cefSAlexander Motin cp->index += MP_IDLE; 1970c883cefSAlexander Motin if (best == NULL || cp->private < best->private || 1980c883cefSAlexander Motin (cp->private == best->private && cp->index > best->index)) 1990c883cefSAlexander Motin best = cp; 2000c883cefSAlexander Motin } 2010c883cefSAlexander Motin if (best != NULL) 2020c883cefSAlexander Motin best->index &= ~MP_IDLE_MASK; 2030c883cefSAlexander Motin return (best); 2040c883cefSAlexander Motin } 205e770bc6bSMatt Jacob 206e770bc6bSMatt Jacob static void 207e770bc6bSMatt Jacob g_mpd(void *arg, int flags __unused) 208e770bc6bSMatt Jacob { 2090c883cefSAlexander Motin struct g_geom *gp; 2100c883cefSAlexander Motin struct g_multipath_softc *sc; 211e770bc6bSMatt Jacob struct g_consumer *cp; 2120c883cefSAlexander Motin int w; 213e770bc6bSMatt Jacob 214e770bc6bSMatt Jacob g_topology_assert(); 215e770bc6bSMatt Jacob cp = arg; 2160c883cefSAlexander Motin gp = cp->geom; 2170c883cefSAlexander Motin if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) { 2180c883cefSAlexander Motin w = cp->acw; 219e770bc6bSMatt Jacob g_access(cp, -cp->acr, -cp->acw, -cp->ace); 2200c883cefSAlexander Motin if (w > 0 && cp->provider != NULL && 2210c883cefSAlexander Motin (cp->provider->geom->flags & G_GEOM_WITHER) == 0) { 22225080ac4SSteven Hartland cp->index |= MP_WITHER; 2230c883cefSAlexander Motin g_post_event(g_mpd, cp, M_WAITOK, NULL); 2240c883cefSAlexander Motin return; 2250c883cefSAlexander Motin } 2260c883cefSAlexander Motin } 2270c883cefSAlexander Motin sc = gp->softc; 2280c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 229e770bc6bSMatt Jacob if (cp->provider) { 230e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: %s removed from %s\n", 2310c883cefSAlexander Motin cp->provider->name, gp->name); 23267f72211SAlan Somers SDT_PROBE2(geom, multipath, config, remove, 23367f72211SAlan Somers gp->name, cp->provider->name); 234e770bc6bSMatt Jacob g_detach(cp); 235e770bc6bSMatt Jacob } 236e770bc6bSMatt Jacob g_destroy_consumer(cp); 2370c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 2380c883cefSAlexander Motin if (LIST_EMPTY(&gp->consumer)) 2390c883cefSAlexander Motin g_multipath_destroy(gp); 240e770bc6bSMatt Jacob } 241e770bc6bSMatt Jacob 242e770bc6bSMatt Jacob static void 243e770bc6bSMatt Jacob g_multipath_orphan(struct g_consumer *cp) 244e770bc6bSMatt Jacob { 2450c883cefSAlexander Motin struct g_multipath_softc *sc; 2460c883cefSAlexander Motin uintptr_t *cnt; 2470c883cefSAlexander Motin 2480c883cefSAlexander Motin g_topology_assert(); 2490c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s in %s was disconnected\n", 250e770bc6bSMatt Jacob cp->provider->name, cp->geom->name); 25167f72211SAlan Somers SDT_PROBE2(geom, multipath, config, disconnect, 25267f72211SAlan Somers cp->geom->name, cp->provider->name); 2530c883cefSAlexander Motin sc = cp->geom->softc; 2540c883cefSAlexander Motin cnt = (uintptr_t *)&cp->private; 2550c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 2560c883cefSAlexander Motin sc->sc_ndisks--; 2570c883cefSAlexander Motin g_multipath_fault(cp, MP_LOST); 2580c883cefSAlexander Motin if (*cnt == 0 && (cp->index & MP_POSTED) == 0) { 2590c883cefSAlexander Motin cp->index |= MP_POSTED; 2600c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 261e770bc6bSMatt Jacob g_mpd(cp, 0); 2620c883cefSAlexander Motin } else 2630c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 264e770bc6bSMatt Jacob } 265e770bc6bSMatt Jacob 266e770bc6bSMatt Jacob static void 267e6afd72bSAlexander Motin g_multipath_resize(struct g_consumer *cp) 268e6afd72bSAlexander Motin { 269e6afd72bSAlexander Motin struct g_multipath_softc *sc; 270e6afd72bSAlexander Motin struct g_geom *gp; 271f8c79813SAlexander Motin struct g_consumer *cp1; 272e6afd72bSAlexander Motin struct g_provider *pp; 273e6afd72bSAlexander Motin struct g_multipath_metadata md; 274e6afd72bSAlexander Motin off_t size, psize, ssize; 275e6afd72bSAlexander Motin int error; 276e6afd72bSAlexander Motin 277e6afd72bSAlexander Motin g_topology_assert(); 278e6afd72bSAlexander Motin 279e6afd72bSAlexander Motin gp = cp->geom; 280e6afd72bSAlexander Motin pp = cp->provider; 281e6afd72bSAlexander Motin sc = gp->softc; 282e6afd72bSAlexander Motin 283e6afd72bSAlexander Motin if (sc->sc_stopping) 284e6afd72bSAlexander Motin return; 285e6afd72bSAlexander Motin 286e6afd72bSAlexander Motin if (pp->mediasize < sc->sc_size) { 287e6afd72bSAlexander Motin size = pp->mediasize; 288e6afd72bSAlexander Motin ssize = pp->sectorsize; 289e6afd72bSAlexander Motin } else { 290e6afd72bSAlexander Motin size = ssize = OFF_MAX; 291e6afd72bSAlexander Motin mtx_lock(&sc->sc_mtx); 292f8c79813SAlexander Motin LIST_FOREACH(cp1, &gp->consumer, consumer) { 293f8c79813SAlexander Motin pp = cp1->provider; 294e6afd72bSAlexander Motin if (pp == NULL) 295e6afd72bSAlexander Motin continue; 296e6afd72bSAlexander Motin if (pp->mediasize < size) { 297e6afd72bSAlexander Motin size = pp->mediasize; 298e6afd72bSAlexander Motin ssize = pp->sectorsize; 299e6afd72bSAlexander Motin } 300e6afd72bSAlexander Motin } 301e6afd72bSAlexander Motin mtx_unlock(&sc->sc_mtx); 302e6afd72bSAlexander Motin if (size == OFF_MAX || size == sc->sc_size) 303e6afd72bSAlexander Motin return; 304e6afd72bSAlexander Motin } 305e6afd72bSAlexander Motin psize = size - ((sc->sc_uuid[0] != 0) ? ssize : 0); 306e6afd72bSAlexander Motin printf("GEOM_MULTIPATH: %s size changed from %jd to %jd\n", 307e6afd72bSAlexander Motin sc->sc_name, sc->sc_pp->mediasize, psize); 308f8c79813SAlexander Motin if (sc->sc_uuid[0] != 0 && size < sc->sc_size) { 309f8c79813SAlexander Motin error = g_multipath_read_metadata(cp, &md); 310f8c79813SAlexander Motin if (error || 311f8c79813SAlexander Motin (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) || 312f8c79813SAlexander Motin (memcmp(md.md_uuid, sc->sc_uuid, sizeof(sc->sc_uuid)) != 0) || 313f8c79813SAlexander Motin (strcmp(md.md_name, sc->sc_name) != 0) || 314f8c79813SAlexander Motin (md.md_size != 0 && md.md_size != size) || 315f8c79813SAlexander Motin (md.md_sectorsize != 0 && md.md_sectorsize != ssize)) { 316e6afd72bSAlexander Motin g_multipath_destroy(gp); 317e6afd72bSAlexander Motin return; 318e6afd72bSAlexander Motin } 319f8c79813SAlexander Motin } 320e6afd72bSAlexander Motin sc->sc_size = size; 321e6afd72bSAlexander Motin g_resize_provider(sc->sc_pp, psize); 322e6afd72bSAlexander Motin 323f8c79813SAlexander Motin if (sc->sc_uuid[0] != 0) { 324e6afd72bSAlexander Motin pp = cp->provider; 325e6afd72bSAlexander Motin strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic)); 326e6afd72bSAlexander Motin memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid)); 327e6afd72bSAlexander Motin strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name)); 328e6afd72bSAlexander Motin md.md_version = G_MULTIPATH_VERSION; 329e6afd72bSAlexander Motin md.md_size = size; 330f8c79813SAlexander Motin md.md_sectorsize = ssize; 331e6afd72bSAlexander Motin md.md_active_active = sc->sc_active_active; 332f8c79813SAlexander Motin error = g_multipath_write_metadata(cp, &md); 333e6afd72bSAlexander Motin if (error != 0) 334e6afd72bSAlexander Motin printf("GEOM_MULTIPATH: Can't update metadata on %s " 335e6afd72bSAlexander Motin "(%d)\n", pp->name, error); 336e6afd72bSAlexander Motin } 337e6afd72bSAlexander Motin } 338e6afd72bSAlexander Motin 339e6afd72bSAlexander Motin static void 340e770bc6bSMatt Jacob g_multipath_start(struct bio *bp) 341e770bc6bSMatt Jacob { 342e770bc6bSMatt Jacob struct g_multipath_softc *sc; 343e770bc6bSMatt Jacob struct g_geom *gp; 344e770bc6bSMatt Jacob struct g_consumer *cp; 345e770bc6bSMatt Jacob struct bio *cbp; 3460c883cefSAlexander Motin uintptr_t *cnt; 347e770bc6bSMatt Jacob 348e770bc6bSMatt Jacob gp = bp->bio_to->geom; 349e770bc6bSMatt Jacob sc = gp->softc; 350e770bc6bSMatt Jacob KASSERT(sc != NULL, ("NULL sc")); 351e770bc6bSMatt Jacob cbp = g_clone_bio(bp); 352e770bc6bSMatt Jacob if (cbp == NULL) { 353e770bc6bSMatt Jacob g_io_deliver(bp, ENOMEM); 354e770bc6bSMatt Jacob return; 355e770bc6bSMatt Jacob } 3560c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 35763297dfdSAlexander Motin cp = g_multipath_choose(gp, bp); 3580c883cefSAlexander Motin if (cp == NULL) { 3590c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 3600c883cefSAlexander Motin g_destroy_bio(cbp); 3610c883cefSAlexander Motin g_io_deliver(bp, ENXIO); 3620c883cefSAlexander Motin return; 3630c883cefSAlexander Motin } 3640c883cefSAlexander Motin if ((uintptr_t)bp->bio_driver1 < sc->sc_ndisks) 3650c883cefSAlexander Motin bp->bio_driver1 = (void *)(uintptr_t)sc->sc_ndisks; 3660c883cefSAlexander Motin cnt = (uintptr_t *)&cp->private; 3670c883cefSAlexander Motin (*cnt)++; 3680c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 369e770bc6bSMatt Jacob cbp->bio_done = g_multipath_done; 370e770bc6bSMatt Jacob g_io_request(cbp, cp); 371e770bc6bSMatt Jacob } 372e770bc6bSMatt Jacob 373e770bc6bSMatt Jacob static void 374e770bc6bSMatt Jacob g_multipath_done(struct bio *bp) 375e770bc6bSMatt Jacob { 3760c883cefSAlexander Motin struct g_multipath_softc *sc; 3770c883cefSAlexander Motin struct g_consumer *cp; 3780c883cefSAlexander Motin uintptr_t *cnt; 3790c883cefSAlexander Motin 380e770bc6bSMatt Jacob if (bp->bio_error == ENXIO || bp->bio_error == EIO) { 381e770bc6bSMatt Jacob mtx_lock(&gmtbq_mtx); 382e770bc6bSMatt Jacob bioq_insert_tail(&gmtbq, bp); 383e770bc6bSMatt Jacob mtx_unlock(&gmtbq_mtx); 3840c883cefSAlexander Motin wakeup(&g_multipath_kt_state); 385e770bc6bSMatt Jacob } else { 3860c883cefSAlexander Motin cp = bp->bio_from; 3870c883cefSAlexander Motin sc = cp->geom->softc; 3880c883cefSAlexander Motin cnt = (uintptr_t *)&cp->private; 3890c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 3900c883cefSAlexander Motin (*cnt)--; 3910c883cefSAlexander Motin if (*cnt == 0 && (cp->index & MP_LOST)) { 3920ada3afcSAlexander Motin if (g_post_event(g_mpd, cp, M_NOWAIT, NULL) == 0) 3930c883cefSAlexander Motin cp->index |= MP_POSTED; 3940c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 3950c883cefSAlexander Motin } else 3960c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 397e770bc6bSMatt Jacob g_std_done(bp); 398e770bc6bSMatt Jacob } 399e770bc6bSMatt Jacob } 400e770bc6bSMatt Jacob 401e770bc6bSMatt Jacob static void 402e770bc6bSMatt Jacob g_multipath_done_error(struct bio *bp) 403e770bc6bSMatt Jacob { 404e770bc6bSMatt Jacob struct bio *pbp; 405e770bc6bSMatt Jacob struct g_geom *gp; 406e770bc6bSMatt Jacob struct g_multipath_softc *sc; 407e770bc6bSMatt Jacob struct g_consumer *cp; 408e770bc6bSMatt Jacob struct g_provider *pp; 4090c883cefSAlexander Motin uintptr_t *cnt; 410e770bc6bSMatt Jacob 411e770bc6bSMatt Jacob /* 412e770bc6bSMatt Jacob * If we had a failure, we have to check first to see 413e770bc6bSMatt Jacob * whether the consumer it failed on was the currently 414e770bc6bSMatt Jacob * active consumer (i.e., this is the first in perhaps 415e770bc6bSMatt Jacob * a number of failures). If so, we then switch consumers 416e770bc6bSMatt Jacob * to the next available consumer. 417e770bc6bSMatt Jacob */ 418e770bc6bSMatt Jacob 419e770bc6bSMatt Jacob pbp = bp->bio_parent; 420e770bc6bSMatt Jacob gp = pbp->bio_to->geom; 421e770bc6bSMatt Jacob sc = gp->softc; 422e770bc6bSMatt Jacob cp = bp->bio_from; 423e770bc6bSMatt Jacob pp = cp->provider; 4240c883cefSAlexander Motin cnt = (uintptr_t *)&cp->private; 425e770bc6bSMatt Jacob 4260c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 42763297dfdSAlexander Motin if ((cp->index & MP_FAIL) == 0) { 4280c883cefSAlexander Motin printf("GEOM_MULTIPATH: Error %d, %s in %s marked FAIL\n", 4290c883cefSAlexander Motin bp->bio_error, pp->name, sc->sc_name); 43067f72211SAlan Somers SDT_PROBE3(geom, multipath, config, fail, 43167f72211SAlan Somers sc->sc_name, pp->name, bp->bio_error); 4320c883cefSAlexander Motin g_multipath_fault(cp, MP_FAIL); 43363297dfdSAlexander Motin } 4340c883cefSAlexander Motin (*cnt)--; 4350c883cefSAlexander Motin if (*cnt == 0 && (cp->index & (MP_LOST | MP_POSTED)) == MP_LOST) { 436e770bc6bSMatt Jacob cp->index |= MP_POSTED; 4370c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 4380c883cefSAlexander Motin g_post_event(g_mpd, cp, M_WAITOK, NULL); 4390c883cefSAlexander Motin } else 4400c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 441e770bc6bSMatt Jacob 442e770bc6bSMatt Jacob /* 443e770bc6bSMatt Jacob * If we can fruitfully restart the I/O, do so. 444e770bc6bSMatt Jacob */ 4450c883cefSAlexander Motin if (pbp->bio_children < (uintptr_t)pbp->bio_driver1) { 4460c883cefSAlexander Motin pbp->bio_inbed++; 44767f72211SAlan Somers SDT_PROBE2(geom, multipath, io, restart, bp, pbp); 448e770bc6bSMatt Jacob g_destroy_bio(bp); 449e770bc6bSMatt Jacob g_multipath_start(pbp); 450e770bc6bSMatt Jacob } else { 451e770bc6bSMatt Jacob g_std_done(bp); 452e770bc6bSMatt Jacob } 453e770bc6bSMatt Jacob } 454e770bc6bSMatt Jacob 455e770bc6bSMatt Jacob static void 456e770bc6bSMatt Jacob g_multipath_kt(void *arg) 457e770bc6bSMatt Jacob { 45812f35a61SPawel Jakub Dawidek 459e770bc6bSMatt Jacob g_multipath_kt_state = GKT_RUN; 460e770bc6bSMatt Jacob mtx_lock(&gmtbq_mtx); 461e770bc6bSMatt Jacob while (g_multipath_kt_state == GKT_RUN) { 462e770bc6bSMatt Jacob for (;;) { 463e770bc6bSMatt Jacob struct bio *bp; 46412f35a61SPawel Jakub Dawidek 465e770bc6bSMatt Jacob bp = bioq_takefirst(&gmtbq); 46612f35a61SPawel Jakub Dawidek if (bp == NULL) 467e770bc6bSMatt Jacob break; 468e770bc6bSMatt Jacob mtx_unlock(&gmtbq_mtx); 469e770bc6bSMatt Jacob g_multipath_done_error(bp); 470e770bc6bSMatt Jacob mtx_lock(&gmtbq_mtx); 471e770bc6bSMatt Jacob } 47263297dfdSAlexander Motin if (g_multipath_kt_state != GKT_RUN) 47363297dfdSAlexander Motin break; 474e770bc6bSMatt Jacob msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO, 47563297dfdSAlexander Motin "gkt:wait", 0); 476e770bc6bSMatt Jacob } 477e770bc6bSMatt Jacob mtx_unlock(&gmtbq_mtx); 478e770bc6bSMatt Jacob wakeup(&g_multipath_kt_state); 4793745c395SJulian Elischer kproc_exit(0); 480e770bc6bSMatt Jacob } 481e770bc6bSMatt Jacob 482e770bc6bSMatt Jacob static int 483e770bc6bSMatt Jacob g_multipath_access(struct g_provider *pp, int dr, int dw, int de) 484e770bc6bSMatt Jacob { 485e770bc6bSMatt Jacob struct g_geom *gp; 486e770bc6bSMatt Jacob struct g_consumer *cp, *badcp = NULL; 4870c883cefSAlexander Motin struct g_multipath_softc *sc; 488e770bc6bSMatt Jacob int error; 489e770bc6bSMatt Jacob 490e770bc6bSMatt Jacob gp = pp->geom; 491e770bc6bSMatt Jacob 49225080ac4SSteven Hartland /* Error used if we have no valid consumers. */ 49380f0a89cSAlexander Motin error = (dr > 0 || dw > 0 || de > 0) ? ENXIO : 0; 49425080ac4SSteven Hartland 495e770bc6bSMatt Jacob LIST_FOREACH(cp, &gp->consumer, consumer) { 49625080ac4SSteven Hartland if (cp->index & MP_WITHER) 49725080ac4SSteven Hartland continue; 49825080ac4SSteven Hartland 499e770bc6bSMatt Jacob error = g_access(cp, dr, dw, de); 500e770bc6bSMatt Jacob if (error) { 501e770bc6bSMatt Jacob badcp = cp; 502e770bc6bSMatt Jacob goto fail; 503e770bc6bSMatt Jacob } 504e770bc6bSMatt Jacob } 50525080ac4SSteven Hartland 50625080ac4SSteven Hartland if (error != 0) 50725080ac4SSteven Hartland return (error); 50825080ac4SSteven Hartland 5090c883cefSAlexander Motin sc = gp->softc; 5100c883cefSAlexander Motin sc->sc_opened += dr + dw + de; 5110c883cefSAlexander Motin if (sc->sc_stopping && sc->sc_opened == 0) 5120c883cefSAlexander Motin g_multipath_destroy(gp); 51325080ac4SSteven Hartland 514e770bc6bSMatt Jacob return (0); 515e770bc6bSMatt Jacob 516e770bc6bSMatt Jacob fail: 517e770bc6bSMatt Jacob LIST_FOREACH(cp, &gp->consumer, consumer) { 51812f35a61SPawel Jakub Dawidek if (cp == badcp) 519e770bc6bSMatt Jacob break; 52025080ac4SSteven Hartland if (cp->index & MP_WITHER) 52125080ac4SSteven Hartland continue; 52225080ac4SSteven Hartland 523e770bc6bSMatt Jacob (void) g_access(cp, -dr, -dw, -de); 524e770bc6bSMatt Jacob } 525e770bc6bSMatt Jacob return (error); 526e770bc6bSMatt Jacob } 527e770bc6bSMatt Jacob 528e770bc6bSMatt Jacob static struct g_geom * 529e770bc6bSMatt Jacob g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md) 530e770bc6bSMatt Jacob { 531e770bc6bSMatt Jacob struct g_multipath_softc *sc; 532e770bc6bSMatt Jacob struct g_geom *gp; 533e770bc6bSMatt Jacob struct g_provider *pp; 534e770bc6bSMatt Jacob 535e770bc6bSMatt Jacob g_topology_assert(); 536e770bc6bSMatt Jacob 537e770bc6bSMatt Jacob LIST_FOREACH(gp, &mp->geom, geom) { 5380c883cefSAlexander Motin sc = gp->softc; 5390c883cefSAlexander Motin if (sc == NULL || sc->sc_stopping) 5400c883cefSAlexander Motin continue; 541e770bc6bSMatt Jacob if (strcmp(gp->name, md->md_name) == 0) { 542e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: name %s already exists\n", 543e770bc6bSMatt Jacob md->md_name); 544e770bc6bSMatt Jacob return (NULL); 545e770bc6bSMatt Jacob } 546e770bc6bSMatt Jacob } 547e770bc6bSMatt Jacob 54802c62349SJaakko Heinonen gp = g_new_geomf(mp, "%s", md->md_name); 549e770bc6bSMatt Jacob sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 5500c883cefSAlexander Motin mtx_init(&sc->sc_mtx, "multipath", NULL, MTX_DEF); 5510c883cefSAlexander Motin memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid)); 5520c883cefSAlexander Motin memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name)); 5530c883cefSAlexander Motin sc->sc_active_active = md->md_active_active; 554e6afd72bSAlexander Motin sc->sc_size = md->md_size; 555e770bc6bSMatt Jacob gp->softc = sc; 556e770bc6bSMatt Jacob gp->start = g_multipath_start; 557e770bc6bSMatt Jacob gp->orphan = g_multipath_orphan; 558e6afd72bSAlexander Motin gp->resize = g_multipath_resize; 559e770bc6bSMatt Jacob gp->access = g_multipath_access; 5600c883cefSAlexander Motin gp->dumpconf = g_multipath_dumpconf; 561e770bc6bSMatt Jacob 562e770bc6bSMatt Jacob pp = g_new_providerf(gp, "multipath/%s", md->md_name); 56340ea77a0SAlexander Motin pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 5640c883cefSAlexander Motin if (md->md_size != 0) { 5650c883cefSAlexander Motin pp->mediasize = md->md_size - 5660c883cefSAlexander Motin ((md->md_uuid[0] != 0) ? md->md_sectorsize : 0); 567e770bc6bSMatt Jacob pp->sectorsize = md->md_sectorsize; 5680c883cefSAlexander Motin } 5690c883cefSAlexander Motin sc->sc_pp = pp; 570e770bc6bSMatt Jacob g_error_provider(pp, 0); 5710c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s created\n", gp->name); 572e770bc6bSMatt Jacob return (gp); 573e770bc6bSMatt Jacob } 574e770bc6bSMatt Jacob 575e770bc6bSMatt Jacob static int 576e770bc6bSMatt Jacob g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp) 577e770bc6bSMatt Jacob { 578e770bc6bSMatt Jacob struct g_multipath_softc *sc; 579e770bc6bSMatt Jacob struct g_consumer *cp, *nxtcp; 5800c883cefSAlexander Motin int error, acr, acw, ace; 581e770bc6bSMatt Jacob 582e770bc6bSMatt Jacob g_topology_assert(); 583e770bc6bSMatt Jacob 584e770bc6bSMatt Jacob sc = gp->softc; 585e770bc6bSMatt Jacob KASSERT(sc, ("no softc")); 586e770bc6bSMatt Jacob 587e770bc6bSMatt Jacob /* 588e770bc6bSMatt Jacob * Make sure that the passed provider isn't already attached 589e770bc6bSMatt Jacob */ 590e770bc6bSMatt Jacob LIST_FOREACH(cp, &gp->consumer, consumer) { 59112f35a61SPawel Jakub Dawidek if (cp->provider == pp) 592e770bc6bSMatt Jacob break; 593e770bc6bSMatt Jacob } 594e770bc6bSMatt Jacob if (cp) { 595e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: provider %s already attached to %s\n", 596e770bc6bSMatt Jacob pp->name, gp->name); 597e770bc6bSMatt Jacob return (EEXIST); 598e770bc6bSMatt Jacob } 599e770bc6bSMatt Jacob nxtcp = LIST_FIRST(&gp->consumer); 600e770bc6bSMatt Jacob cp = g_new_consumer(gp); 60140ea77a0SAlexander Motin cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 6020c883cefSAlexander Motin cp->private = NULL; 6030c883cefSAlexander Motin cp->index = MP_NEW; 604e770bc6bSMatt Jacob error = g_attach(cp, pp); 605e770bc6bSMatt Jacob if (error != 0) { 606e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: cannot attach %s to %s", 607e770bc6bSMatt Jacob pp->name, sc->sc_name); 608e770bc6bSMatt Jacob g_destroy_consumer(cp); 609e770bc6bSMatt Jacob return (error); 610e770bc6bSMatt Jacob } 611e770bc6bSMatt Jacob 612e770bc6bSMatt Jacob /* 613e770bc6bSMatt Jacob * Set access permissions on new consumer to match other consumers 614e770bc6bSMatt Jacob */ 6150c883cefSAlexander Motin if (sc->sc_pp) { 6160c883cefSAlexander Motin acr = sc->sc_pp->acr; 6170c883cefSAlexander Motin acw = sc->sc_pp->acw; 6180c883cefSAlexander Motin ace = sc->sc_pp->ace; 6190c883cefSAlexander Motin } else 6200c883cefSAlexander Motin acr = acw = ace = 0; 6210c883cefSAlexander Motin if (g_multipath_exclusive) { 6220c883cefSAlexander Motin acr++; 6230c883cefSAlexander Motin acw++; 6240c883cefSAlexander Motin ace++; 6250c883cefSAlexander Motin } 6260c883cefSAlexander Motin error = g_access(cp, acr, acw, ace); 627e770bc6bSMatt Jacob if (error) { 628e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: cannot set access in " 6290c883cefSAlexander Motin "attaching %s to %s (%d)\n", 6300c883cefSAlexander Motin pp->name, sc->sc_name, error); 631e770bc6bSMatt Jacob g_detach(cp); 632e770bc6bSMatt Jacob g_destroy_consumer(cp); 633e770bc6bSMatt Jacob return (error); 634e770bc6bSMatt Jacob } 635e6afd72bSAlexander Motin if (sc->sc_size == 0) { 636e6afd72bSAlexander Motin sc->sc_size = pp->mediasize - 6370c883cefSAlexander Motin ((sc->sc_uuid[0] != 0) ? pp->sectorsize : 0); 638e6afd72bSAlexander Motin sc->sc_pp->mediasize = sc->sc_size; 6390c883cefSAlexander Motin sc->sc_pp->sectorsize = pp->sectorsize; 640e770bc6bSMatt Jacob } 641e6afd72bSAlexander Motin if (sc->sc_pp->stripesize == 0 && sc->sc_pp->stripeoffset == 0) { 6420c883cefSAlexander Motin sc->sc_pp->stripesize = pp->stripesize; 6430c883cefSAlexander Motin sc->sc_pp->stripeoffset = pp->stripeoffset; 6440c883cefSAlexander Motin } 645f4673017SAlexander Motin sc->sc_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED; 6460c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 6470c883cefSAlexander Motin cp->index = 0; 6480c883cefSAlexander Motin sc->sc_ndisks++; 6490c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 6500c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s added to %s\n", 6510c883cefSAlexander Motin pp->name, sc->sc_name); 6520c883cefSAlexander Motin if (sc->sc_active == NULL) { 6530c883cefSAlexander Motin sc->sc_active = cp; 65463297dfdSAlexander Motin if (sc->sc_active_active != 1) 6550c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s is now active path in %s\n", 656e770bc6bSMatt Jacob pp->name, sc->sc_name); 657e770bc6bSMatt Jacob } 658e770bc6bSMatt Jacob return (0); 659e770bc6bSMatt Jacob } 660e770bc6bSMatt Jacob 661e770bc6bSMatt Jacob static int 662e770bc6bSMatt Jacob g_multipath_destroy(struct g_geom *gp) 663e770bc6bSMatt Jacob { 6640c883cefSAlexander Motin struct g_multipath_softc *sc; 6650c883cefSAlexander Motin struct g_consumer *cp, *cp1; 666e770bc6bSMatt Jacob 667e770bc6bSMatt Jacob g_topology_assert(); 66812f35a61SPawel Jakub Dawidek if (gp->softc == NULL) 669e770bc6bSMatt Jacob return (ENXIO); 6700c883cefSAlexander Motin sc = gp->softc; 6710c883cefSAlexander Motin if (!sc->sc_stopping) { 672e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: destroying %s\n", gp->name); 6730c883cefSAlexander Motin sc->sc_stopping = 1; 6740c883cefSAlexander Motin } 6750c883cefSAlexander Motin if (sc->sc_opened != 0) { 6760c883cefSAlexander Motin g_wither_provider(sc->sc_pp, ENXIO); 6770c883cefSAlexander Motin sc->sc_pp = NULL; 6780c883cefSAlexander Motin return (EINPROGRESS); 6790c883cefSAlexander Motin } 6800c883cefSAlexander Motin LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) { 6810c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 6820c883cefSAlexander Motin if ((cp->index & MP_POSTED) == 0) { 6830c883cefSAlexander Motin cp->index |= MP_POSTED; 6840c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 6850c883cefSAlexander Motin g_mpd(cp, 0); 6860c883cefSAlexander Motin if (cp1 == NULL) 6870c883cefSAlexander Motin return(0); /* Recursion happened. */ 6880c883cefSAlexander Motin } else 6890c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 6900c883cefSAlexander Motin } 6910c883cefSAlexander Motin if (!LIST_EMPTY(&gp->consumer)) 6920c883cefSAlexander Motin return (EINPROGRESS); 6930c883cefSAlexander Motin mtx_destroy(&sc->sc_mtx); 694e770bc6bSMatt Jacob g_free(gp->softc); 695e770bc6bSMatt Jacob gp->softc = NULL; 6960c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s destroyed\n", gp->name); 697e770bc6bSMatt Jacob g_wither_geom(gp, ENXIO); 698e770bc6bSMatt Jacob return (0); 699e770bc6bSMatt Jacob } 700e770bc6bSMatt Jacob 701e770bc6bSMatt Jacob static int 702e770bc6bSMatt Jacob g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp, 703e770bc6bSMatt Jacob struct g_geom *gp) 704e770bc6bSMatt Jacob { 70512f35a61SPawel Jakub Dawidek 706e770bc6bSMatt Jacob return (g_multipath_destroy(gp)); 707e770bc6bSMatt Jacob } 708e770bc6bSMatt Jacob 709b5dce617SMatt Jacob static int 710b5dce617SMatt Jacob g_multipath_rotate(struct g_geom *gp) 711b5dce617SMatt Jacob { 7128fb378d6SThomas Quinot struct g_consumer *lcp, *first_good_cp = NULL; 713b5dce617SMatt Jacob struct g_multipath_softc *sc = gp->softc; 7148fb378d6SThomas Quinot int active_cp_seen = 0; 715b5dce617SMatt Jacob 716b5dce617SMatt Jacob g_topology_assert(); 717b5dce617SMatt Jacob if (sc == NULL) 718b5dce617SMatt Jacob return (ENXIO); 719b5dce617SMatt Jacob LIST_FOREACH(lcp, &gp->consumer, consumer) { 720b5dce617SMatt Jacob if ((lcp->index & MP_BAD) == 0) { 7218fb378d6SThomas Quinot if (first_good_cp == NULL) 7228fb378d6SThomas Quinot first_good_cp = lcp; 7238fb378d6SThomas Quinot if (active_cp_seen) 724b5dce617SMatt Jacob break; 725b5dce617SMatt Jacob } 7268fb378d6SThomas Quinot if (sc->sc_active == lcp) 7278fb378d6SThomas Quinot active_cp_seen = 1; 728b5dce617SMatt Jacob } 7298fb378d6SThomas Quinot if (lcp == NULL) 7308fb378d6SThomas Quinot lcp = first_good_cp; 7318fb378d6SThomas Quinot if (lcp && lcp != sc->sc_active) { 7320c883cefSAlexander Motin sc->sc_active = lcp; 73363297dfdSAlexander Motin if (sc->sc_active_active != 1) 7340c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s is now active path in %s\n", 735b5dce617SMatt Jacob lcp->provider->name, sc->sc_name); 736b5dce617SMatt Jacob } 737b5dce617SMatt Jacob return (0); 738b5dce617SMatt Jacob } 739b5dce617SMatt Jacob 740e770bc6bSMatt Jacob static void 741e770bc6bSMatt Jacob g_multipath_init(struct g_class *mp) 742e770bc6bSMatt Jacob { 743e770bc6bSMatt Jacob bioq_init(&gmtbq); 744e770bc6bSMatt Jacob mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF); 74563297dfdSAlexander Motin kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt"); 746e770bc6bSMatt Jacob } 747e770bc6bSMatt Jacob 748e770bc6bSMatt Jacob static void 749e770bc6bSMatt Jacob g_multipath_fini(struct g_class *mp) 750e770bc6bSMatt Jacob { 751e770bc6bSMatt Jacob if (g_multipath_kt_state == GKT_RUN) { 752e770bc6bSMatt Jacob mtx_lock(&gmtbq_mtx); 753e770bc6bSMatt Jacob g_multipath_kt_state = GKT_DIE; 754e770bc6bSMatt Jacob wakeup(&g_multipath_kt_state); 755e770bc6bSMatt Jacob msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO, 756e770bc6bSMatt Jacob "gmp:fini", 0); 757e770bc6bSMatt Jacob mtx_unlock(&gmtbq_mtx); 758e770bc6bSMatt Jacob } 759e770bc6bSMatt Jacob } 760e770bc6bSMatt Jacob 761e770bc6bSMatt Jacob static int 762e770bc6bSMatt Jacob g_multipath_read_metadata(struct g_consumer *cp, 763e770bc6bSMatt Jacob struct g_multipath_metadata *md) 764e770bc6bSMatt Jacob { 765e770bc6bSMatt Jacob struct g_provider *pp; 766e770bc6bSMatt Jacob u_char *buf; 767e770bc6bSMatt Jacob int error; 768e770bc6bSMatt Jacob 769e770bc6bSMatt Jacob g_topology_assert(); 770e770bc6bSMatt Jacob error = g_access(cp, 1, 0, 0); 77112f35a61SPawel Jakub Dawidek if (error != 0) 772e770bc6bSMatt Jacob return (error); 773e770bc6bSMatt Jacob pp = cp->provider; 774e770bc6bSMatt Jacob g_topology_unlock(); 775e770bc6bSMatt Jacob buf = g_read_data(cp, pp->mediasize - pp->sectorsize, 776e770bc6bSMatt Jacob pp->sectorsize, &error); 777e770bc6bSMatt Jacob g_topology_lock(); 778e770bc6bSMatt Jacob g_access(cp, -1, 0, 0); 77912f35a61SPawel Jakub Dawidek if (buf == NULL) 780e770bc6bSMatt Jacob return (error); 781e770bc6bSMatt Jacob multipath_metadata_decode(buf, md); 782e770bc6bSMatt Jacob g_free(buf); 783e770bc6bSMatt Jacob return (0); 784e770bc6bSMatt Jacob } 785e770bc6bSMatt Jacob 786f8c79813SAlexander Motin static int 787f8c79813SAlexander Motin g_multipath_write_metadata(struct g_consumer *cp, 788f8c79813SAlexander Motin struct g_multipath_metadata *md) 789f8c79813SAlexander Motin { 790f8c79813SAlexander Motin struct g_provider *pp; 791f8c79813SAlexander Motin u_char *buf; 792f8c79813SAlexander Motin int error; 793f8c79813SAlexander Motin 794f8c79813SAlexander Motin g_topology_assert(); 795f8c79813SAlexander Motin error = g_access(cp, 1, 1, 1); 796f8c79813SAlexander Motin if (error != 0) 797f8c79813SAlexander Motin return (error); 798f8c79813SAlexander Motin pp = cp->provider; 799f8c79813SAlexander Motin g_topology_unlock(); 800f8c79813SAlexander Motin buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 801f8c79813SAlexander Motin multipath_metadata_encode(md, buf); 802f8c79813SAlexander Motin error = g_write_data(cp, pp->mediasize - pp->sectorsize, 803f8c79813SAlexander Motin buf, pp->sectorsize); 804f8c79813SAlexander Motin g_topology_lock(); 805f8c79813SAlexander Motin g_access(cp, -1, -1, -1); 806f8c79813SAlexander Motin g_free(buf); 807f8c79813SAlexander Motin return (error); 808f8c79813SAlexander Motin } 809f8c79813SAlexander Motin 810e770bc6bSMatt Jacob static struct g_geom * 811e770bc6bSMatt Jacob g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 812e770bc6bSMatt Jacob { 813e770bc6bSMatt Jacob struct g_multipath_metadata md; 814e770bc6bSMatt Jacob struct g_multipath_softc *sc; 815e770bc6bSMatt Jacob struct g_consumer *cp; 816e770bc6bSMatt Jacob struct g_geom *gp, *gp1; 817e770bc6bSMatt Jacob int error, isnew; 818e770bc6bSMatt Jacob 819e770bc6bSMatt Jacob g_topology_assert(); 820e770bc6bSMatt Jacob 821e770bc6bSMatt Jacob gp = g_new_geomf(mp, "multipath:taste"); 822e770bc6bSMatt Jacob gp->start = g_multipath_start; 823e770bc6bSMatt Jacob gp->access = g_multipath_access; 824e770bc6bSMatt Jacob gp->orphan = g_multipath_orphan; 825e770bc6bSMatt Jacob cp = g_new_consumer(gp); 826*d22ff249SEdward Tomasz Napierala error = g_attach(cp, pp); 827*d22ff249SEdward Tomasz Napierala if (error == 0) { 828e770bc6bSMatt Jacob error = g_multipath_read_metadata(cp, &md); 829e770bc6bSMatt Jacob g_detach(cp); 830*d22ff249SEdward Tomasz Napierala } 831e770bc6bSMatt Jacob g_destroy_consumer(cp); 832e770bc6bSMatt Jacob g_destroy_geom(gp); 83312f35a61SPawel Jakub Dawidek if (error != 0) 834e770bc6bSMatt Jacob return (NULL); 835e770bc6bSMatt Jacob gp = NULL; 836e770bc6bSMatt Jacob 837e770bc6bSMatt Jacob if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) { 83812f35a61SPawel Jakub Dawidek if (g_multipath_debug) 839e770bc6bSMatt Jacob printf("%s is not MULTIPATH\n", pp->name); 840e770bc6bSMatt Jacob return (NULL); 841e770bc6bSMatt Jacob } 842e770bc6bSMatt Jacob if (md.md_version != G_MULTIPATH_VERSION) { 843e770bc6bSMatt Jacob printf("%s has version %d multipath id- this module is version " 844e770bc6bSMatt Jacob " %d: rejecting\n", pp->name, md.md_version, 845e770bc6bSMatt Jacob G_MULTIPATH_VERSION); 846e770bc6bSMatt Jacob return (NULL); 847e770bc6bSMatt Jacob } 8480c883cefSAlexander Motin if (md.md_size != 0 && md.md_size != pp->mediasize) 8490c883cefSAlexander Motin return (NULL); 8500c883cefSAlexander Motin if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize) 8510c883cefSAlexander Motin return (NULL); 85212f35a61SPawel Jakub Dawidek if (g_multipath_debug) 853e770bc6bSMatt Jacob printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid); 85467f72211SAlan Somers SDT_PROBE2(geom, multipath, config, taste, md.md_name, md.md_uuid); 855e770bc6bSMatt Jacob 856e770bc6bSMatt Jacob /* 857e770bc6bSMatt Jacob * Let's check if such a device already is present. We check against 858e770bc6bSMatt Jacob * uuid alone first because that's the true distinguishor. If that 859e770bc6bSMatt Jacob * passes, then we check for name conflicts. If there are conflicts, 860e770bc6bSMatt Jacob * modify the name. 861e770bc6bSMatt Jacob * 862e770bc6bSMatt Jacob * The whole purpose of this is to solve the problem that people don't 863e770bc6bSMatt Jacob * pick good unique names, but good unique names (like uuids) are a 864e770bc6bSMatt Jacob * pain to use. So, we allow people to build GEOMs with friendly names 865e770bc6bSMatt Jacob * and uuids, and modify the names in case there's a collision. 866e770bc6bSMatt Jacob */ 867e770bc6bSMatt Jacob sc = NULL; 868e770bc6bSMatt Jacob LIST_FOREACH(gp, &mp->geom, geom) { 869e770bc6bSMatt Jacob sc = gp->softc; 8700c883cefSAlexander Motin if (sc == NULL || sc->sc_stopping) 871e770bc6bSMatt Jacob continue; 87212f35a61SPawel Jakub Dawidek if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0) 873e770bc6bSMatt Jacob break; 874e770bc6bSMatt Jacob } 875e770bc6bSMatt Jacob 876e770bc6bSMatt Jacob LIST_FOREACH(gp1, &mp->geom, geom) { 87712f35a61SPawel Jakub Dawidek if (gp1 == gp) 878e770bc6bSMatt Jacob continue; 879e770bc6bSMatt Jacob sc = gp1->softc; 8800c883cefSAlexander Motin if (sc == NULL || sc->sc_stopping) 881e770bc6bSMatt Jacob continue; 88212f35a61SPawel Jakub Dawidek if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0) 883e770bc6bSMatt Jacob break; 884e770bc6bSMatt Jacob } 885e770bc6bSMatt Jacob 886e770bc6bSMatt Jacob /* 887e770bc6bSMatt Jacob * If gp is NULL, we had no extant MULTIPATH geom with this uuid. 888e770bc6bSMatt Jacob * 889e770bc6bSMatt Jacob * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant 890e770bc6bSMatt Jacob * with the same name (but a different UUID). 891e770bc6bSMatt Jacob * 892e770bc6bSMatt Jacob * If gp is NULL, then modify the name with a random number and 893e770bc6bSMatt Jacob * complain, but allow the creation of the geom to continue. 894e770bc6bSMatt Jacob * 895e770bc6bSMatt Jacob * If gp is *not* NULL, just use the geom's name as we're attaching 896e770bc6bSMatt Jacob * this disk to the (previously generated) name. 897e770bc6bSMatt Jacob */ 898e770bc6bSMatt Jacob 899e770bc6bSMatt Jacob if (gp1) { 900e770bc6bSMatt Jacob sc = gp1->softc; 901e770bc6bSMatt Jacob if (gp == NULL) { 902e770bc6bSMatt Jacob char buf[16]; 903e770bc6bSMatt Jacob u_long rand = random(); 904e770bc6bSMatt Jacob 905e770bc6bSMatt Jacob snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand); 906e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: geom %s/%s exists already\n", 907e770bc6bSMatt Jacob sc->sc_name, sc->sc_uuid); 908e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n", 909e770bc6bSMatt Jacob md.md_uuid, buf); 910e770bc6bSMatt Jacob strlcpy(md.md_name, buf, sizeof(md.md_name)); 911e770bc6bSMatt Jacob } else { 912e770bc6bSMatt Jacob strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name)); 913e770bc6bSMatt Jacob } 914e770bc6bSMatt Jacob } 915e770bc6bSMatt Jacob 916e770bc6bSMatt Jacob if (gp == NULL) { 917e770bc6bSMatt Jacob gp = g_multipath_create(mp, &md); 918e770bc6bSMatt Jacob if (gp == NULL) { 919e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: cannot create geom %s/%s\n", 920e770bc6bSMatt Jacob md.md_name, md.md_uuid); 921e770bc6bSMatt Jacob return (NULL); 922e770bc6bSMatt Jacob } 923e770bc6bSMatt Jacob isnew = 1; 924e770bc6bSMatt Jacob } else { 925e770bc6bSMatt Jacob isnew = 0; 926e770bc6bSMatt Jacob } 927e770bc6bSMatt Jacob 928e770bc6bSMatt Jacob sc = gp->softc; 929e770bc6bSMatt Jacob KASSERT(sc != NULL, ("sc is NULL")); 930e770bc6bSMatt Jacob error = g_multipath_add_disk(gp, pp); 931e770bc6bSMatt Jacob if (error != 0) { 93212f35a61SPawel Jakub Dawidek if (isnew) 933e770bc6bSMatt Jacob g_multipath_destroy(gp); 934e770bc6bSMatt Jacob return (NULL); 935e770bc6bSMatt Jacob } 936e770bc6bSMatt Jacob return (gp); 937e770bc6bSMatt Jacob } 938e770bc6bSMatt Jacob 939e770bc6bSMatt Jacob static void 9400c883cefSAlexander Motin g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp, 9410c883cefSAlexander Motin const char *name) 942e770bc6bSMatt Jacob { 9430c883cefSAlexander Motin struct g_multipath_softc *sc; 944e770bc6bSMatt Jacob struct g_geom *gp; 9452b4969ffSMatt Jacob struct g_consumer *cp; 9460c883cefSAlexander Motin struct g_provider *pp; 9470c883cefSAlexander Motin const char *mpname; 9488510f61aSXin LI static const char devpf[6] = _PATH_DEV; 949d3fef0a0SAlexander Motin int error; 950e770bc6bSMatt Jacob 951e770bc6bSMatt Jacob g_topology_assert(); 952e770bc6bSMatt Jacob 953e770bc6bSMatt Jacob mpname = gctl_get_asciiparam(req, "arg0"); 954e770bc6bSMatt Jacob if (mpname == NULL) { 955e770bc6bSMatt Jacob gctl_error(req, "No 'arg0' argument"); 956e770bc6bSMatt Jacob return; 957e770bc6bSMatt Jacob } 9582b4969ffSMatt Jacob gp = g_multipath_find_geom(mp, mpname); 9592b4969ffSMatt Jacob if (gp == NULL) { 9602b4969ffSMatt Jacob gctl_error(req, "Device %s is invalid", mpname); 961e770bc6bSMatt Jacob return; 962e770bc6bSMatt Jacob } 9630c883cefSAlexander Motin sc = gp->softc; 964e770bc6bSMatt Jacob 96512f35a61SPawel Jakub Dawidek if (strncmp(name, devpf, 5) == 0) 966e770bc6bSMatt Jacob name += 5; 9672b4969ffSMatt Jacob pp = g_provider_by_name(name); 9682b4969ffSMatt Jacob if (pp == NULL) { 969e770bc6bSMatt Jacob gctl_error(req, "Provider %s is invalid", name); 970e770bc6bSMatt Jacob return; 971e770bc6bSMatt Jacob } 972e770bc6bSMatt Jacob 973e770bc6bSMatt Jacob /* 9740c883cefSAlexander Motin * Check to make sure parameters match. 975e770bc6bSMatt Jacob */ 9760c883cefSAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer) { 9770c883cefSAlexander Motin if (cp->provider == pp) { 9780c883cefSAlexander Motin gctl_error(req, "provider %s is already there", 9790c883cefSAlexander Motin pp->name); 980e770bc6bSMatt Jacob return; 981e770bc6bSMatt Jacob } 9820c883cefSAlexander Motin } 983e6afd72bSAlexander Motin if (sc->sc_pp->mediasize != 0 && 9840c883cefSAlexander Motin sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0) 9850c883cefSAlexander Motin != pp->mediasize) { 9860c883cefSAlexander Motin gctl_error(req, "Providers size mismatch %jd != %jd", 9870c883cefSAlexander Motin (intmax_t) sc->sc_pp->mediasize + 9880c883cefSAlexander Motin (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0), 9890c883cefSAlexander Motin (intmax_t) pp->mediasize); 990e770bc6bSMatt Jacob return; 991e770bc6bSMatt Jacob } 992e6afd72bSAlexander Motin if (sc->sc_pp->sectorsize != 0 && 9930c883cefSAlexander Motin sc->sc_pp->sectorsize != pp->sectorsize) { 9940c883cefSAlexander Motin gctl_error(req, "Providers sectorsize mismatch %u != %u", 9950c883cefSAlexander Motin sc->sc_pp->sectorsize, pp->sectorsize); 996e770bc6bSMatt Jacob return; 997e770bc6bSMatt Jacob } 998e770bc6bSMatt Jacob 999d3fef0a0SAlexander Motin error = g_multipath_add_disk(gp, pp); 1000d3fef0a0SAlexander Motin if (error != 0) 1001d3fef0a0SAlexander Motin gctl_error(req, "Provider addition error: %d", error); 1002e770bc6bSMatt Jacob } 1003e770bc6bSMatt Jacob 10040c883cefSAlexander Motin static void 100571ee4ef0SThomas Quinot g_multipath_ctl_prefer(struct gctl_req *req, struct g_class *mp) 100671ee4ef0SThomas Quinot { 100771ee4ef0SThomas Quinot struct g_geom *gp; 100871ee4ef0SThomas Quinot struct g_multipath_softc *sc; 100971ee4ef0SThomas Quinot struct g_consumer *cp; 101071ee4ef0SThomas Quinot const char *name, *mpname; 10118510f61aSXin LI static const char devpf[6] = _PATH_DEV; 101271ee4ef0SThomas Quinot int *nargs; 101371ee4ef0SThomas Quinot 101471ee4ef0SThomas Quinot g_topology_assert(); 101571ee4ef0SThomas Quinot 101671ee4ef0SThomas Quinot mpname = gctl_get_asciiparam(req, "arg0"); 101771ee4ef0SThomas Quinot if (mpname == NULL) { 101871ee4ef0SThomas Quinot gctl_error(req, "No 'arg0' argument"); 101971ee4ef0SThomas Quinot return; 102071ee4ef0SThomas Quinot } 102171ee4ef0SThomas Quinot gp = g_multipath_find_geom(mp, mpname); 102271ee4ef0SThomas Quinot if (gp == NULL) { 102371ee4ef0SThomas Quinot gctl_error(req, "Device %s is invalid", mpname); 102471ee4ef0SThomas Quinot return; 102571ee4ef0SThomas Quinot } 102671ee4ef0SThomas Quinot sc = gp->softc; 102771ee4ef0SThomas Quinot 102871ee4ef0SThomas Quinot nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 102971ee4ef0SThomas Quinot if (nargs == NULL) { 103071ee4ef0SThomas Quinot gctl_error(req, "No 'nargs' argument"); 103171ee4ef0SThomas Quinot return; 103271ee4ef0SThomas Quinot } 103371ee4ef0SThomas Quinot if (*nargs != 2) { 103471ee4ef0SThomas Quinot gctl_error(req, "missing device"); 103571ee4ef0SThomas Quinot return; 103671ee4ef0SThomas Quinot } 103771ee4ef0SThomas Quinot 103871ee4ef0SThomas Quinot name = gctl_get_asciiparam(req, "arg1"); 103971ee4ef0SThomas Quinot if (name == NULL) { 104071ee4ef0SThomas Quinot gctl_error(req, "No 'arg1' argument"); 104171ee4ef0SThomas Quinot return; 104271ee4ef0SThomas Quinot } 104371ee4ef0SThomas Quinot if (strncmp(name, devpf, 5) == 0) { 104471ee4ef0SThomas Quinot name += 5; 104571ee4ef0SThomas Quinot } 104671ee4ef0SThomas Quinot 104771ee4ef0SThomas Quinot LIST_FOREACH(cp, &gp->consumer, consumer) { 104871ee4ef0SThomas Quinot if (cp->provider != NULL 104971ee4ef0SThomas Quinot && strcmp(cp->provider->name, name) == 0) 105071ee4ef0SThomas Quinot break; 105171ee4ef0SThomas Quinot } 105271ee4ef0SThomas Quinot 105371ee4ef0SThomas Quinot if (cp == NULL) { 105471ee4ef0SThomas Quinot gctl_error(req, "Provider %s not found", name); 105571ee4ef0SThomas Quinot return; 105671ee4ef0SThomas Quinot } 105771ee4ef0SThomas Quinot 105871ee4ef0SThomas Quinot mtx_lock(&sc->sc_mtx); 105971ee4ef0SThomas Quinot 106071ee4ef0SThomas Quinot if (cp->index & MP_BAD) { 106171ee4ef0SThomas Quinot gctl_error(req, "Consumer %s is invalid", name); 106271ee4ef0SThomas Quinot mtx_unlock(&sc->sc_mtx); 106371ee4ef0SThomas Quinot return; 106471ee4ef0SThomas Quinot } 106571ee4ef0SThomas Quinot 106671ee4ef0SThomas Quinot /* Here when the consumer is present and in good shape */ 106771ee4ef0SThomas Quinot 106871ee4ef0SThomas Quinot sc->sc_active = cp; 106971ee4ef0SThomas Quinot if (!sc->sc_active_active) 107071ee4ef0SThomas Quinot printf("GEOM_MULTIPATH: %s now active path in %s\n", 107171ee4ef0SThomas Quinot sc->sc_active->provider->name, sc->sc_name); 107271ee4ef0SThomas Quinot 107371ee4ef0SThomas Quinot mtx_unlock(&sc->sc_mtx); 107471ee4ef0SThomas Quinot } 107571ee4ef0SThomas Quinot 107671ee4ef0SThomas Quinot static void 10770c883cefSAlexander Motin g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp) 10780c883cefSAlexander Motin { 10790c883cefSAlexander Motin struct g_multipath_softc *sc; 10800c883cefSAlexander Motin struct g_geom *gp; 10810c883cefSAlexander Motin const char *mpname, *name; 10820c883cefSAlexander Motin 10830c883cefSAlexander Motin mpname = gctl_get_asciiparam(req, "arg0"); 10840c883cefSAlexander Motin if (mpname == NULL) { 10850c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 10860c883cefSAlexander Motin return; 10870c883cefSAlexander Motin } 10880c883cefSAlexander Motin gp = g_multipath_find_geom(mp, mpname); 10890c883cefSAlexander Motin if (gp == NULL) { 10900c883cefSAlexander Motin gctl_error(req, "Device %s not found", mpname); 10910c883cefSAlexander Motin return; 10920c883cefSAlexander Motin } 10930c883cefSAlexander Motin sc = gp->softc; 10940c883cefSAlexander Motin 10950c883cefSAlexander Motin name = gctl_get_asciiparam(req, "arg1"); 10960c883cefSAlexander Motin if (name == NULL) { 10970c883cefSAlexander Motin gctl_error(req, "No 'arg1' argument"); 10980c883cefSAlexander Motin return; 10990c883cefSAlexander Motin } 11000c883cefSAlexander Motin g_multipath_ctl_add_name(req, mp, name); 11010c883cefSAlexander Motin } 11020c883cefSAlexander Motin 11030c883cefSAlexander Motin static void 11040c883cefSAlexander Motin g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp) 11050c883cefSAlexander Motin { 11060c883cefSAlexander Motin struct g_multipath_metadata md; 11070c883cefSAlexander Motin struct g_multipath_softc *sc; 11080c883cefSAlexander Motin struct g_geom *gp; 11090c883cefSAlexander Motin const char *mpname, *name; 11100c883cefSAlexander Motin char param[16]; 111163297dfdSAlexander Motin int *nargs, i, *val; 11120c883cefSAlexander Motin 11130c883cefSAlexander Motin g_topology_assert(); 11140c883cefSAlexander Motin 11150c883cefSAlexander Motin nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 11160c883cefSAlexander Motin if (*nargs < 2) { 11170c883cefSAlexander Motin gctl_error(req, "wrong number of arguments."); 11180c883cefSAlexander Motin return; 11190c883cefSAlexander Motin } 11200c883cefSAlexander Motin 11210c883cefSAlexander Motin mpname = gctl_get_asciiparam(req, "arg0"); 11220c883cefSAlexander Motin if (mpname == NULL) { 11230c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 11240c883cefSAlexander Motin return; 11250c883cefSAlexander Motin } 11260c883cefSAlexander Motin gp = g_multipath_find_geom(mp, mpname); 11270c883cefSAlexander Motin if (gp != NULL) { 11280c883cefSAlexander Motin gctl_error(req, "Device %s already exist", mpname); 11290c883cefSAlexander Motin return; 11300c883cefSAlexander Motin } 11310c883cefSAlexander Motin 11320c883cefSAlexander Motin memset(&md, 0, sizeof(md)); 11330c883cefSAlexander Motin strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic)); 11340c883cefSAlexander Motin md.md_version = G_MULTIPATH_VERSION; 11350c883cefSAlexander Motin strlcpy(md.md_name, mpname, sizeof(md.md_name)); 11360c883cefSAlexander Motin md.md_size = 0; 11370c883cefSAlexander Motin md.md_sectorsize = 0; 11380c883cefSAlexander Motin md.md_uuid[0] = 0; 113963297dfdSAlexander Motin md.md_active_active = 0; 114063297dfdSAlexander Motin val = gctl_get_paraml(req, "active_active", sizeof(*val)); 114163297dfdSAlexander Motin if (val != NULL && *val != 0) 114263297dfdSAlexander Motin md.md_active_active = 1; 114363297dfdSAlexander Motin val = gctl_get_paraml(req, "active_read", sizeof(*val)); 114463297dfdSAlexander Motin if (val != NULL && *val != 0) 114563297dfdSAlexander Motin md.md_active_active = 2; 11460c883cefSAlexander Motin gp = g_multipath_create(mp, &md); 11470c883cefSAlexander Motin if (gp == NULL) { 11480c883cefSAlexander Motin gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n", 11490c883cefSAlexander Motin md.md_name, md.md_uuid); 11500c883cefSAlexander Motin return; 11510c883cefSAlexander Motin } 11520c883cefSAlexander Motin sc = gp->softc; 11530c883cefSAlexander Motin 11540c883cefSAlexander Motin for (i = 1; i < *nargs; i++) { 11550c883cefSAlexander Motin snprintf(param, sizeof(param), "arg%d", i); 11560c883cefSAlexander Motin name = gctl_get_asciiparam(req, param); 11570c883cefSAlexander Motin g_multipath_ctl_add_name(req, mp, name); 11580c883cefSAlexander Motin } 11590c883cefSAlexander Motin 11600c883cefSAlexander Motin if (sc->sc_ndisks != (*nargs - 1)) 11610c883cefSAlexander Motin g_multipath_destroy(gp); 11620c883cefSAlexander Motin } 11630c883cefSAlexander Motin 11640c883cefSAlexander Motin static void 116563297dfdSAlexander Motin g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp) 116663297dfdSAlexander Motin { 116763297dfdSAlexander Motin struct g_multipath_softc *sc; 116863297dfdSAlexander Motin struct g_geom *gp; 116963297dfdSAlexander Motin struct g_consumer *cp; 117063297dfdSAlexander Motin struct g_provider *pp; 1171c0b1ef66SAlexander Motin struct g_multipath_metadata md; 117263297dfdSAlexander Motin const char *name; 117363297dfdSAlexander Motin int error, *val; 117463297dfdSAlexander Motin 117563297dfdSAlexander Motin g_topology_assert(); 117663297dfdSAlexander Motin 117763297dfdSAlexander Motin name = gctl_get_asciiparam(req, "arg0"); 117863297dfdSAlexander Motin if (name == NULL) { 117963297dfdSAlexander Motin gctl_error(req, "No 'arg0' argument"); 118063297dfdSAlexander Motin return; 118163297dfdSAlexander Motin } 118263297dfdSAlexander Motin gp = g_multipath_find_geom(mp, name); 118363297dfdSAlexander Motin if (gp == NULL) { 118463297dfdSAlexander Motin gctl_error(req, "Device %s is invalid", name); 118563297dfdSAlexander Motin return; 118663297dfdSAlexander Motin } 118763297dfdSAlexander Motin sc = gp->softc; 118863297dfdSAlexander Motin val = gctl_get_paraml(req, "active_active", sizeof(*val)); 118963297dfdSAlexander Motin if (val != NULL && *val != 0) 119063297dfdSAlexander Motin sc->sc_active_active = 1; 119163297dfdSAlexander Motin val = gctl_get_paraml(req, "active_read", sizeof(*val)); 119263297dfdSAlexander Motin if (val != NULL && *val != 0) 119363297dfdSAlexander Motin sc->sc_active_active = 2; 119463297dfdSAlexander Motin val = gctl_get_paraml(req, "active_passive", sizeof(*val)); 119563297dfdSAlexander Motin if (val != NULL && *val != 0) 119663297dfdSAlexander Motin sc->sc_active_active = 0; 119763297dfdSAlexander Motin if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) { 119863297dfdSAlexander Motin cp = sc->sc_active; 119963297dfdSAlexander Motin pp = cp->provider; 1200c0b1ef66SAlexander Motin strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic)); 1201c0b1ef66SAlexander Motin memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid)); 1202c0b1ef66SAlexander Motin strlcpy(md.md_name, name, sizeof(md.md_name)); 1203c0b1ef66SAlexander Motin md.md_version = G_MULTIPATH_VERSION; 1204c0b1ef66SAlexander Motin md.md_size = pp->mediasize; 1205c0b1ef66SAlexander Motin md.md_sectorsize = pp->sectorsize; 1206c0b1ef66SAlexander Motin md.md_active_active = sc->sc_active_active; 1207f8c79813SAlexander Motin error = g_multipath_write_metadata(cp, &md); 120863297dfdSAlexander Motin if (error != 0) 120963297dfdSAlexander Motin gctl_error(req, "Can't update metadata on %s (%d)", 121063297dfdSAlexander Motin pp->name, error); 121163297dfdSAlexander Motin } 121263297dfdSAlexander Motin } 121363297dfdSAlexander Motin 121463297dfdSAlexander Motin static void 12150c883cefSAlexander Motin g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail) 12160c883cefSAlexander Motin { 12170c883cefSAlexander Motin struct g_multipath_softc *sc; 12180c883cefSAlexander Motin struct g_geom *gp; 12190c883cefSAlexander Motin struct g_consumer *cp; 12200c883cefSAlexander Motin const char *mpname, *name; 12210c883cefSAlexander Motin int found; 12220c883cefSAlexander Motin 12230c883cefSAlexander Motin mpname = gctl_get_asciiparam(req, "arg0"); 12240c883cefSAlexander Motin if (mpname == NULL) { 12250c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 12260c883cefSAlexander Motin return; 12270c883cefSAlexander Motin } 12280c883cefSAlexander Motin gp = g_multipath_find_geom(mp, mpname); 12290c883cefSAlexander Motin if (gp == NULL) { 12300c883cefSAlexander Motin gctl_error(req, "Device %s not found", mpname); 12310c883cefSAlexander Motin return; 12320c883cefSAlexander Motin } 12330c883cefSAlexander Motin sc = gp->softc; 12340c883cefSAlexander Motin 12350c883cefSAlexander Motin name = gctl_get_asciiparam(req, "arg1"); 12360c883cefSAlexander Motin if (name == NULL) { 12370c883cefSAlexander Motin gctl_error(req, "No 'arg1' argument"); 12380c883cefSAlexander Motin return; 12390c883cefSAlexander Motin } 12400c883cefSAlexander Motin 12410c883cefSAlexander Motin found = 0; 12420c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 12430c883cefSAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer) { 12440c883cefSAlexander Motin if (cp->provider != NULL && 12450c883cefSAlexander Motin strcmp(cp->provider->name, name) == 0 && 12460c883cefSAlexander Motin (cp->index & MP_LOST) == 0) { 12470c883cefSAlexander Motin found = 1; 124863297dfdSAlexander Motin if (!fail == !(cp->index & MP_FAIL)) 124963297dfdSAlexander Motin continue; 12500c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s in %s is marked %s.\n", 12510c883cefSAlexander Motin name, sc->sc_name, fail ? "FAIL" : "OK"); 12520c883cefSAlexander Motin if (fail) { 12530c883cefSAlexander Motin g_multipath_fault(cp, MP_FAIL); 125467f72211SAlan Somers SDT_PROBE3(geom, multipath, config, fail, 125567f72211SAlan Somers sc->sc_name, cp->provider->name, 0); 12560c883cefSAlexander Motin } else { 12570c883cefSAlexander Motin cp->index &= ~MP_FAIL; 125867f72211SAlan Somers SDT_PROBE2(geom, multipath, config, restore, 125967f72211SAlan Somers sc->sc_name, cp->provider->name); 12600c883cefSAlexander Motin } 12610c883cefSAlexander Motin } 12620c883cefSAlexander Motin } 12630c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 12640c883cefSAlexander Motin if (found == 0) 12650c883cefSAlexander Motin gctl_error(req, "Provider %s not found", name); 12660c883cefSAlexander Motin } 12670c883cefSAlexander Motin 12680c883cefSAlexander Motin static void 12690c883cefSAlexander Motin g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp) 12700c883cefSAlexander Motin { 12710c883cefSAlexander Motin struct g_multipath_softc *sc; 12720c883cefSAlexander Motin struct g_geom *gp; 12730c883cefSAlexander Motin struct g_consumer *cp, *cp1; 12740c883cefSAlexander Motin const char *mpname, *name; 12750c883cefSAlexander Motin uintptr_t *cnt; 12760c883cefSAlexander Motin int found; 12770c883cefSAlexander Motin 12780c883cefSAlexander Motin mpname = gctl_get_asciiparam(req, "arg0"); 12790c883cefSAlexander Motin if (mpname == NULL) { 12800c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 12810c883cefSAlexander Motin return; 12820c883cefSAlexander Motin } 12830c883cefSAlexander Motin gp = g_multipath_find_geom(mp, mpname); 12840c883cefSAlexander Motin if (gp == NULL) { 12850c883cefSAlexander Motin gctl_error(req, "Device %s not found", mpname); 12860c883cefSAlexander Motin return; 12870c883cefSAlexander Motin } 12880c883cefSAlexander Motin sc = gp->softc; 12890c883cefSAlexander Motin 12900c883cefSAlexander Motin name = gctl_get_asciiparam(req, "arg1"); 12910c883cefSAlexander Motin if (name == NULL) { 12920c883cefSAlexander Motin gctl_error(req, "No 'arg1' argument"); 12930c883cefSAlexander Motin return; 12940c883cefSAlexander Motin } 12950c883cefSAlexander Motin 12960c883cefSAlexander Motin found = 0; 12970c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 12980c883cefSAlexander Motin LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) { 12990c883cefSAlexander Motin if (cp->provider != NULL && 13000c883cefSAlexander Motin strcmp(cp->provider->name, name) == 0 && 13010c883cefSAlexander Motin (cp->index & MP_LOST) == 0) { 13020c883cefSAlexander Motin found = 1; 13030c883cefSAlexander Motin printf("GEOM_MULTIPATH: removing %s from %s\n", 13040c883cefSAlexander Motin cp->provider->name, cp->geom->name); 130567f72211SAlan Somers SDT_PROBE2(geom, multipath, config, remove, 130667f72211SAlan Somers cp->geom->name, cp->provider->name); 13070c883cefSAlexander Motin sc->sc_ndisks--; 13080c883cefSAlexander Motin g_multipath_fault(cp, MP_LOST); 13090c883cefSAlexander Motin cnt = (uintptr_t *)&cp->private; 13100c883cefSAlexander Motin if (*cnt == 0 && (cp->index & MP_POSTED) == 0) { 13110c883cefSAlexander Motin cp->index |= MP_POSTED; 13120c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 13130c883cefSAlexander Motin g_mpd(cp, 0); 13140c883cefSAlexander Motin if (cp1 == NULL) 13150c883cefSAlexander Motin return; /* Recursion happened. */ 13160c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 13170c883cefSAlexander Motin } 13180c883cefSAlexander Motin } 13190c883cefSAlexander Motin } 13200c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 13210c883cefSAlexander Motin if (found == 0) 13220c883cefSAlexander Motin gctl_error(req, "Provider %s not found", name); 13230c883cefSAlexander Motin } 13240c883cefSAlexander Motin 1325e770bc6bSMatt Jacob static struct g_geom * 1326e770bc6bSMatt Jacob g_multipath_find_geom(struct g_class *mp, const char *name) 1327e770bc6bSMatt Jacob { 1328e770bc6bSMatt Jacob struct g_geom *gp; 13290c883cefSAlexander Motin struct g_multipath_softc *sc; 1330e770bc6bSMatt Jacob 1331e770bc6bSMatt Jacob LIST_FOREACH(gp, &mp->geom, geom) { 13320c883cefSAlexander Motin sc = gp->softc; 13330c883cefSAlexander Motin if (sc == NULL || sc->sc_stopping) 13340c883cefSAlexander Motin continue; 13350c883cefSAlexander Motin if (strcmp(gp->name, name) == 0) 1336e770bc6bSMatt Jacob return (gp); 1337e770bc6bSMatt Jacob } 1338e770bc6bSMatt Jacob return (NULL); 1339e770bc6bSMatt Jacob } 1340e770bc6bSMatt Jacob 1341e770bc6bSMatt Jacob static void 13420c883cefSAlexander Motin g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp) 1343e770bc6bSMatt Jacob { 1344e770bc6bSMatt Jacob struct g_geom *gp; 1345e770bc6bSMatt Jacob const char *name; 1346e770bc6bSMatt Jacob int error; 1347e770bc6bSMatt Jacob 1348e770bc6bSMatt Jacob g_topology_assert(); 1349e770bc6bSMatt Jacob 1350e770bc6bSMatt Jacob name = gctl_get_asciiparam(req, "arg0"); 1351e770bc6bSMatt Jacob if (name == NULL) { 1352e770bc6bSMatt Jacob gctl_error(req, "No 'arg0' argument"); 1353e770bc6bSMatt Jacob return; 1354e770bc6bSMatt Jacob } 1355e770bc6bSMatt Jacob gp = g_multipath_find_geom(mp, name); 1356e770bc6bSMatt Jacob if (gp == NULL) { 1357e770bc6bSMatt Jacob gctl_error(req, "Device %s is invalid", name); 1358e770bc6bSMatt Jacob return; 1359e770bc6bSMatt Jacob } 1360e770bc6bSMatt Jacob error = g_multipath_destroy(gp); 13610c883cefSAlexander Motin if (error != 0 && error != EINPROGRESS) 13620c883cefSAlexander Motin gctl_error(req, "failed to stop %s (err=%d)", name, error); 1363e770bc6bSMatt Jacob } 13640c883cefSAlexander Motin 13650c883cefSAlexander Motin static void 13660c883cefSAlexander Motin g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp) 13670c883cefSAlexander Motin { 13680c883cefSAlexander Motin struct g_geom *gp; 13690c883cefSAlexander Motin struct g_multipath_softc *sc; 13700c883cefSAlexander Motin struct g_consumer *cp; 13710c883cefSAlexander Motin struct g_provider *pp; 13720c883cefSAlexander Motin const char *name; 13730c883cefSAlexander Motin uint8_t *buf; 13740c883cefSAlexander Motin int error; 13750c883cefSAlexander Motin 13760c883cefSAlexander Motin g_topology_assert(); 13770c883cefSAlexander Motin 13780c883cefSAlexander Motin name = gctl_get_asciiparam(req, "arg0"); 13790c883cefSAlexander Motin if (name == NULL) { 13800c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 13810c883cefSAlexander Motin return; 13820c883cefSAlexander Motin } 13830c883cefSAlexander Motin gp = g_multipath_find_geom(mp, name); 13840c883cefSAlexander Motin if (gp == NULL) { 13850c883cefSAlexander Motin gctl_error(req, "Device %s is invalid", name); 13860c883cefSAlexander Motin return; 13870c883cefSAlexander Motin } 13880c883cefSAlexander Motin sc = gp->softc; 13890c883cefSAlexander Motin 13900c883cefSAlexander Motin if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) { 13910c883cefSAlexander Motin cp = sc->sc_active; 13920c883cefSAlexander Motin pp = cp->provider; 13930c883cefSAlexander Motin error = g_access(cp, 1, 1, 1); 13940c883cefSAlexander Motin if (error != 0) { 13950c883cefSAlexander Motin gctl_error(req, "Can't open %s (%d)", pp->name, error); 13960c883cefSAlexander Motin goto destroy; 13970c883cefSAlexander Motin } 13980c883cefSAlexander Motin g_topology_unlock(); 13990c883cefSAlexander Motin buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 14000c883cefSAlexander Motin error = g_write_data(cp, pp->mediasize - pp->sectorsize, 14010c883cefSAlexander Motin buf, pp->sectorsize); 14020c883cefSAlexander Motin g_topology_lock(); 14030c883cefSAlexander Motin g_access(cp, -1, -1, -1); 14040c883cefSAlexander Motin if (error != 0) 14050c883cefSAlexander Motin gctl_error(req, "Can't erase metadata on %s (%d)", 14060c883cefSAlexander Motin pp->name, error); 14070c883cefSAlexander Motin } 14080c883cefSAlexander Motin 14090c883cefSAlexander Motin destroy: 14100c883cefSAlexander Motin error = g_multipath_destroy(gp); 14110c883cefSAlexander Motin if (error != 0 && error != EINPROGRESS) 14120c883cefSAlexander Motin gctl_error(req, "failed to destroy %s (err=%d)", name, error); 1413e770bc6bSMatt Jacob } 1414e770bc6bSMatt Jacob 1415e770bc6bSMatt Jacob static void 1416b5dce617SMatt Jacob g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp) 1417b5dce617SMatt Jacob { 1418b5dce617SMatt Jacob struct g_geom *gp; 1419b5dce617SMatt Jacob const char *name; 1420b5dce617SMatt Jacob int error; 1421b5dce617SMatt Jacob 1422b5dce617SMatt Jacob g_topology_assert(); 1423b5dce617SMatt Jacob 1424b5dce617SMatt Jacob name = gctl_get_asciiparam(req, "arg0"); 1425b5dce617SMatt Jacob if (name == NULL) { 1426b5dce617SMatt Jacob gctl_error(req, "No 'arg0' argument"); 1427b5dce617SMatt Jacob return; 1428b5dce617SMatt Jacob } 1429b5dce617SMatt Jacob gp = g_multipath_find_geom(mp, name); 1430b5dce617SMatt Jacob if (gp == NULL) { 1431b5dce617SMatt Jacob gctl_error(req, "Device %s is invalid", name); 1432b5dce617SMatt Jacob return; 1433b5dce617SMatt Jacob } 1434b5dce617SMatt Jacob error = g_multipath_rotate(gp); 1435b5dce617SMatt Jacob if (error != 0) { 1436b5dce617SMatt Jacob gctl_error(req, "failed to rotate %s (err=%d)", name, error); 1437b5dce617SMatt Jacob } 1438b5dce617SMatt Jacob } 1439b5dce617SMatt Jacob 1440b5dce617SMatt Jacob static void 1441b5dce617SMatt Jacob g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp) 1442b5dce617SMatt Jacob { 1443b5dce617SMatt Jacob struct sbuf *sb; 1444b5dce617SMatt Jacob struct g_geom *gp; 1445b5dce617SMatt Jacob struct g_multipath_softc *sc; 14460c883cefSAlexander Motin struct g_consumer *cp; 1447b5dce617SMatt Jacob const char *name; 14480c883cefSAlexander Motin int empty; 1449b5dce617SMatt Jacob 1450b5dce617SMatt Jacob sb = sbuf_new_auto(); 1451b5dce617SMatt Jacob 1452b5dce617SMatt Jacob g_topology_assert(); 1453b5dce617SMatt Jacob name = gctl_get_asciiparam(req, "arg0"); 1454b5dce617SMatt Jacob if (name == NULL) { 1455b5dce617SMatt Jacob gctl_error(req, "No 'arg0' argument"); 1456b5dce617SMatt Jacob return; 1457b5dce617SMatt Jacob } 1458b5dce617SMatt Jacob gp = g_multipath_find_geom(mp, name); 1459b5dce617SMatt Jacob if (gp == NULL) { 1460b5dce617SMatt Jacob gctl_error(req, "Device %s is invalid", name); 1461b5dce617SMatt Jacob return; 1462b5dce617SMatt Jacob } 1463b5dce617SMatt Jacob sc = gp->softc; 146463297dfdSAlexander Motin if (sc->sc_active_active == 1) { 14650c883cefSAlexander Motin empty = 1; 14660c883cefSAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer) { 14670c883cefSAlexander Motin if (cp->index & MP_BAD) 14680c883cefSAlexander Motin continue; 14690c883cefSAlexander Motin if (!empty) 14700c883cefSAlexander Motin sbuf_cat(sb, " "); 14710c883cefSAlexander Motin sbuf_cat(sb, cp->provider->name); 14720c883cefSAlexander Motin empty = 0; 14730c883cefSAlexander Motin } 14740c883cefSAlexander Motin if (empty) 14750c883cefSAlexander Motin sbuf_cat(sb, "none"); 14760c883cefSAlexander Motin sbuf_cat(sb, "\n"); 14770c883cefSAlexander Motin } else if (sc->sc_active && sc->sc_active->provider) { 14780c883cefSAlexander Motin sbuf_printf(sb, "%s\n", sc->sc_active->provider->name); 1479b5dce617SMatt Jacob } else { 148049ee0fceSAlexander Motin sbuf_cat(sb, "none\n"); 1481b5dce617SMatt Jacob } 1482b5dce617SMatt Jacob sbuf_finish(sb); 1483b5dce617SMatt Jacob gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1); 1484b5dce617SMatt Jacob sbuf_delete(sb); 1485b5dce617SMatt Jacob } 1486b5dce617SMatt Jacob 1487b5dce617SMatt Jacob static void 1488e770bc6bSMatt Jacob g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb) 1489e770bc6bSMatt Jacob { 1490e770bc6bSMatt Jacob uint32_t *version; 1491e770bc6bSMatt Jacob g_topology_assert(); 1492e770bc6bSMatt Jacob version = gctl_get_paraml(req, "version", sizeof(*version)); 1493e770bc6bSMatt Jacob if (version == NULL) { 1494e770bc6bSMatt Jacob gctl_error(req, "No 'version' argument"); 1495e770bc6bSMatt Jacob } else if (*version != G_MULTIPATH_VERSION) { 1496e770bc6bSMatt Jacob gctl_error(req, "Userland and kernel parts are out of sync"); 14972b4969ffSMatt Jacob } else if (strcmp(verb, "add") == 0) { 14982b4969ffSMatt Jacob g_multipath_ctl_add(req, mp); 149971ee4ef0SThomas Quinot } else if (strcmp(verb, "prefer") == 0) { 150071ee4ef0SThomas Quinot g_multipath_ctl_prefer(req, mp); 15010c883cefSAlexander Motin } else if (strcmp(verb, "create") == 0) { 15020c883cefSAlexander Motin g_multipath_ctl_create(req, mp); 150363297dfdSAlexander Motin } else if (strcmp(verb, "configure") == 0) { 150463297dfdSAlexander Motin g_multipath_ctl_configure(req, mp); 15050c883cefSAlexander Motin } else if (strcmp(verb, "stop") == 0) { 15060c883cefSAlexander Motin g_multipath_ctl_stop(req, mp); 1507e770bc6bSMatt Jacob } else if (strcmp(verb, "destroy") == 0) { 1508e770bc6bSMatt Jacob g_multipath_ctl_destroy(req, mp); 15090c883cefSAlexander Motin } else if (strcmp(verb, "fail") == 0) { 15100c883cefSAlexander Motin g_multipath_ctl_fail(req, mp, 1); 15110c883cefSAlexander Motin } else if (strcmp(verb, "restore") == 0) { 15120c883cefSAlexander Motin g_multipath_ctl_fail(req, mp, 0); 15130c883cefSAlexander Motin } else if (strcmp(verb, "remove") == 0) { 15140c883cefSAlexander Motin g_multipath_ctl_remove(req, mp); 1515b5dce617SMatt Jacob } else if (strcmp(verb, "rotate") == 0) { 1516b5dce617SMatt Jacob g_multipath_ctl_rotate(req, mp); 1517b5dce617SMatt Jacob } else if (strcmp(verb, "getactive") == 0) { 1518b5dce617SMatt Jacob g_multipath_ctl_getactive(req, mp); 1519e770bc6bSMatt Jacob } else { 1520e770bc6bSMatt Jacob gctl_error(req, "Unknown verb %s", verb); 1521e770bc6bSMatt Jacob } 1522e770bc6bSMatt Jacob } 15230c883cefSAlexander Motin 15240c883cefSAlexander Motin static void 15250c883cefSAlexander Motin g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 15260c883cefSAlexander Motin struct g_consumer *cp, struct g_provider *pp) 15270c883cefSAlexander Motin { 15280c883cefSAlexander Motin struct g_multipath_softc *sc; 15290c883cefSAlexander Motin int good; 15300c883cefSAlexander Motin 15310c883cefSAlexander Motin g_topology_assert(); 15320c883cefSAlexander Motin 15330c883cefSAlexander Motin sc = gp->softc; 15340c883cefSAlexander Motin if (sc == NULL) 15350c883cefSAlexander Motin return; 15360c883cefSAlexander Motin if (cp != NULL) { 1537a839e332SAlexander Motin sbuf_printf(sb, "%s<State>%s</State>\n", indent, 15380c883cefSAlexander Motin (cp->index & MP_NEW) ? "NEW" : 15390c883cefSAlexander Motin (cp->index & MP_LOST) ? "LOST" : 15400c883cefSAlexander Motin (cp->index & MP_FAIL) ? "FAIL" : 154163297dfdSAlexander Motin (sc->sc_active_active == 1 || sc->sc_active == cp) ? 154263297dfdSAlexander Motin "ACTIVE" : 154363297dfdSAlexander Motin sc->sc_active_active == 2 ? "READ" : "PASSIVE"); 15440c883cefSAlexander Motin } else { 15450c883cefSAlexander Motin good = g_multipath_good(gp); 1546a839e332SAlexander Motin sbuf_printf(sb, "%s<State>%s</State>\n", indent, 15470c883cefSAlexander Motin good == 0 ? "BROKEN" : 15480c883cefSAlexander Motin (good != sc->sc_ndisks || sc->sc_ndisks == 1) ? 15490c883cefSAlexander Motin "DEGRADED" : "OPTIMAL"); 15500c883cefSAlexander Motin } 15510c883cefSAlexander Motin if (cp == NULL && pp == NULL) { 1552a839e332SAlexander Motin sbuf_printf(sb, "%s<UUID>%s</UUID>\n", indent, sc->sc_uuid); 1553a839e332SAlexander Motin sbuf_printf(sb, "%s<Mode>Active/%s</Mode>\n", indent, 155463297dfdSAlexander Motin sc->sc_active_active == 2 ? "Read" : 155563297dfdSAlexander Motin sc->sc_active_active == 1 ? "Active" : "Passive"); 1556a839e332SAlexander Motin sbuf_printf(sb, "%s<Type>%s</Type>\n", indent, 15570c883cefSAlexander Motin sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC"); 15580c883cefSAlexander Motin } 15590c883cefSAlexander Motin } 15600c883cefSAlexander Motin 1561e770bc6bSMatt Jacob DECLARE_GEOM_CLASS(g_multipath_class, g_multipath); 156274d6c131SKyle Evans MODULE_VERSION(geom_multipath, 0); 1563