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 483e770bc6bSMatt Jacob static int 484e770bc6bSMatt Jacob g_multipath_access(struct g_provider *pp, int dr, int dw, int de) 485e770bc6bSMatt Jacob { 486e770bc6bSMatt Jacob struct g_geom *gp; 487e770bc6bSMatt Jacob struct g_consumer *cp, *badcp = NULL; 4880c883cefSAlexander Motin struct g_multipath_softc *sc; 489e770bc6bSMatt Jacob int error; 490e770bc6bSMatt Jacob 491e770bc6bSMatt Jacob gp = pp->geom; 492e770bc6bSMatt Jacob 49325080ac4SSteven Hartland /* Error used if we have no valid consumers. */ 49480f0a89cSAlexander Motin error = (dr > 0 || dw > 0 || de > 0) ? ENXIO : 0; 49525080ac4SSteven Hartland 496e770bc6bSMatt Jacob LIST_FOREACH(cp, &gp->consumer, consumer) { 49725080ac4SSteven Hartland if (cp->index & MP_WITHER) 49825080ac4SSteven Hartland continue; 49925080ac4SSteven Hartland 500e770bc6bSMatt Jacob error = g_access(cp, dr, dw, de); 501e770bc6bSMatt Jacob if (error) { 502e770bc6bSMatt Jacob badcp = cp; 503e770bc6bSMatt Jacob goto fail; 504e770bc6bSMatt Jacob } 505e770bc6bSMatt Jacob } 50625080ac4SSteven Hartland 50725080ac4SSteven Hartland if (error != 0) 50825080ac4SSteven Hartland return (error); 50925080ac4SSteven Hartland 5100c883cefSAlexander Motin sc = gp->softc; 5110c883cefSAlexander Motin sc->sc_opened += dr + dw + de; 5120c883cefSAlexander Motin if (sc->sc_stopping && sc->sc_opened == 0) 5130c883cefSAlexander Motin g_multipath_destroy(gp); 51425080ac4SSteven Hartland 515e770bc6bSMatt Jacob return (0); 516e770bc6bSMatt Jacob 517e770bc6bSMatt Jacob fail: 518e770bc6bSMatt Jacob LIST_FOREACH(cp, &gp->consumer, consumer) { 51912f35a61SPawel Jakub Dawidek if (cp == badcp) 520e770bc6bSMatt Jacob break; 52125080ac4SSteven Hartland if (cp->index & MP_WITHER) 52225080ac4SSteven Hartland continue; 52325080ac4SSteven Hartland 524e770bc6bSMatt Jacob (void) g_access(cp, -dr, -dw, -de); 525e770bc6bSMatt Jacob } 526e770bc6bSMatt Jacob return (error); 527e770bc6bSMatt Jacob } 528e770bc6bSMatt Jacob 529e770bc6bSMatt Jacob static struct g_geom * 530e770bc6bSMatt Jacob g_multipath_create(struct g_class *mp, struct g_multipath_metadata *md) 531e770bc6bSMatt Jacob { 532e770bc6bSMatt Jacob struct g_multipath_softc *sc; 533e770bc6bSMatt Jacob struct g_geom *gp; 534e770bc6bSMatt Jacob struct g_provider *pp; 535e770bc6bSMatt Jacob 536e770bc6bSMatt Jacob g_topology_assert(); 537e770bc6bSMatt Jacob 538e770bc6bSMatt Jacob LIST_FOREACH(gp, &mp->geom, geom) { 5390c883cefSAlexander Motin sc = gp->softc; 5400c883cefSAlexander Motin if (sc == NULL || sc->sc_stopping) 5410c883cefSAlexander Motin continue; 542e770bc6bSMatt Jacob if (strcmp(gp->name, md->md_name) == 0) { 543e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: name %s already exists\n", 544e770bc6bSMatt Jacob md->md_name); 545e770bc6bSMatt Jacob return (NULL); 546e770bc6bSMatt Jacob } 547e770bc6bSMatt Jacob } 548e770bc6bSMatt Jacob 54902c62349SJaakko Heinonen gp = g_new_geomf(mp, "%s", md->md_name); 550e770bc6bSMatt Jacob sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 5510c883cefSAlexander Motin mtx_init(&sc->sc_mtx, "multipath", NULL, MTX_DEF); 5520c883cefSAlexander Motin memcpy(sc->sc_uuid, md->md_uuid, sizeof (sc->sc_uuid)); 5530c883cefSAlexander Motin memcpy(sc->sc_name, md->md_name, sizeof (sc->sc_name)); 5540c883cefSAlexander Motin sc->sc_active_active = md->md_active_active; 555e6afd72bSAlexander Motin sc->sc_size = md->md_size; 556e770bc6bSMatt Jacob gp->softc = sc; 557e770bc6bSMatt Jacob gp->start = g_multipath_start; 558e770bc6bSMatt Jacob gp->orphan = g_multipath_orphan; 559e6afd72bSAlexander Motin gp->resize = g_multipath_resize; 560e770bc6bSMatt Jacob gp->access = g_multipath_access; 5610c883cefSAlexander Motin gp->dumpconf = g_multipath_dumpconf; 562e770bc6bSMatt Jacob 563e770bc6bSMatt Jacob pp = g_new_providerf(gp, "multipath/%s", md->md_name); 56440ea77a0SAlexander Motin pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 5650c883cefSAlexander Motin if (md->md_size != 0) { 5660c883cefSAlexander Motin pp->mediasize = md->md_size - 5670c883cefSAlexander Motin ((md->md_uuid[0] != 0) ? md->md_sectorsize : 0); 568e770bc6bSMatt Jacob pp->sectorsize = md->md_sectorsize; 5690c883cefSAlexander Motin } 5700c883cefSAlexander Motin sc->sc_pp = pp; 571e770bc6bSMatt Jacob g_error_provider(pp, 0); 5720c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s created\n", gp->name); 573e770bc6bSMatt Jacob return (gp); 574e770bc6bSMatt Jacob } 575e770bc6bSMatt Jacob 576e770bc6bSMatt Jacob static int 577e770bc6bSMatt Jacob g_multipath_add_disk(struct g_geom *gp, struct g_provider *pp) 578e770bc6bSMatt Jacob { 579e770bc6bSMatt Jacob struct g_multipath_softc *sc; 580e770bc6bSMatt Jacob struct g_consumer *cp, *nxtcp; 5810c883cefSAlexander Motin int error, acr, acw, ace; 582e770bc6bSMatt Jacob 583e770bc6bSMatt Jacob g_topology_assert(); 584e770bc6bSMatt Jacob 585e770bc6bSMatt Jacob sc = gp->softc; 586e770bc6bSMatt Jacob KASSERT(sc, ("no softc")); 587e770bc6bSMatt Jacob 588e770bc6bSMatt Jacob /* 589e770bc6bSMatt Jacob * Make sure that the passed provider isn't already attached 590e770bc6bSMatt Jacob */ 591e770bc6bSMatt Jacob LIST_FOREACH(cp, &gp->consumer, consumer) { 59212f35a61SPawel Jakub Dawidek if (cp->provider == pp) 593e770bc6bSMatt Jacob break; 594e770bc6bSMatt Jacob } 595e770bc6bSMatt Jacob if (cp) { 596e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: provider %s already attached to %s\n", 597e770bc6bSMatt Jacob pp->name, gp->name); 598e770bc6bSMatt Jacob return (EEXIST); 599e770bc6bSMatt Jacob } 600e770bc6bSMatt Jacob nxtcp = LIST_FIRST(&gp->consumer); 601e770bc6bSMatt Jacob cp = g_new_consumer(gp); 60240ea77a0SAlexander Motin cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 6030c883cefSAlexander Motin cp->private = NULL; 6040c883cefSAlexander Motin cp->index = MP_NEW; 605e770bc6bSMatt Jacob error = g_attach(cp, pp); 606e770bc6bSMatt Jacob if (error != 0) { 607e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: cannot attach %s to %s", 608e770bc6bSMatt Jacob pp->name, sc->sc_name); 609e770bc6bSMatt Jacob g_destroy_consumer(cp); 610e770bc6bSMatt Jacob return (error); 611e770bc6bSMatt Jacob } 612e770bc6bSMatt Jacob 613e770bc6bSMatt Jacob /* 614e770bc6bSMatt Jacob * Set access permissions on new consumer to match other consumers 615e770bc6bSMatt Jacob */ 6160c883cefSAlexander Motin if (sc->sc_pp) { 6170c883cefSAlexander Motin acr = sc->sc_pp->acr; 6180c883cefSAlexander Motin acw = sc->sc_pp->acw; 6190c883cefSAlexander Motin ace = sc->sc_pp->ace; 6200c883cefSAlexander Motin } else 6210c883cefSAlexander Motin acr = acw = ace = 0; 6220c883cefSAlexander Motin if (g_multipath_exclusive) { 6230c883cefSAlexander Motin acr++; 6240c883cefSAlexander Motin acw++; 6250c883cefSAlexander Motin ace++; 6260c883cefSAlexander Motin } 6270c883cefSAlexander Motin error = g_access(cp, acr, acw, ace); 628e770bc6bSMatt Jacob if (error) { 629e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: cannot set access in " 6300c883cefSAlexander Motin "attaching %s to %s (%d)\n", 6310c883cefSAlexander Motin pp->name, sc->sc_name, error); 632e770bc6bSMatt Jacob g_detach(cp); 633e770bc6bSMatt Jacob g_destroy_consumer(cp); 634e770bc6bSMatt Jacob return (error); 635e770bc6bSMatt Jacob } 636e6afd72bSAlexander Motin if (sc->sc_size == 0) { 637e6afd72bSAlexander Motin sc->sc_size = pp->mediasize - 6380c883cefSAlexander Motin ((sc->sc_uuid[0] != 0) ? pp->sectorsize : 0); 639e6afd72bSAlexander Motin sc->sc_pp->mediasize = sc->sc_size; 6400c883cefSAlexander Motin sc->sc_pp->sectorsize = pp->sectorsize; 641e770bc6bSMatt Jacob } 642e6afd72bSAlexander Motin if (sc->sc_pp->stripesize == 0 && sc->sc_pp->stripeoffset == 0) { 6430c883cefSAlexander Motin sc->sc_pp->stripesize = pp->stripesize; 6440c883cefSAlexander Motin sc->sc_pp->stripeoffset = pp->stripeoffset; 6450c883cefSAlexander Motin } 646f4673017SAlexander Motin sc->sc_pp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED; 6470c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 6480c883cefSAlexander Motin cp->index = 0; 6490c883cefSAlexander Motin sc->sc_ndisks++; 6500c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 6510c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s added to %s\n", 6520c883cefSAlexander Motin pp->name, sc->sc_name); 6530c883cefSAlexander Motin if (sc->sc_active == NULL) { 6540c883cefSAlexander Motin sc->sc_active = cp; 65563297dfdSAlexander Motin if (sc->sc_active_active != 1) 6560c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s is now active path in %s\n", 657e770bc6bSMatt Jacob pp->name, sc->sc_name); 658e770bc6bSMatt Jacob } 659e770bc6bSMatt Jacob return (0); 660e770bc6bSMatt Jacob } 661e770bc6bSMatt Jacob 662e770bc6bSMatt Jacob static int 663e770bc6bSMatt Jacob g_multipath_destroy(struct g_geom *gp) 664e770bc6bSMatt Jacob { 6650c883cefSAlexander Motin struct g_multipath_softc *sc; 6660c883cefSAlexander Motin struct g_consumer *cp, *cp1; 667e770bc6bSMatt Jacob 668e770bc6bSMatt Jacob g_topology_assert(); 66912f35a61SPawel Jakub Dawidek if (gp->softc == NULL) 670e770bc6bSMatt Jacob return (ENXIO); 6710c883cefSAlexander Motin sc = gp->softc; 6720c883cefSAlexander Motin if (!sc->sc_stopping) { 673e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: destroying %s\n", gp->name); 6740c883cefSAlexander Motin sc->sc_stopping = 1; 6750c883cefSAlexander Motin } 6760c883cefSAlexander Motin if (sc->sc_opened != 0) { 6770c883cefSAlexander Motin g_wither_provider(sc->sc_pp, ENXIO); 6780c883cefSAlexander Motin sc->sc_pp = NULL; 6790c883cefSAlexander Motin return (EINPROGRESS); 6800c883cefSAlexander Motin } 6810c883cefSAlexander Motin LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) { 6820c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 6830c883cefSAlexander Motin if ((cp->index & MP_POSTED) == 0) { 6840c883cefSAlexander Motin cp->index |= MP_POSTED; 6850c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 6860c883cefSAlexander Motin g_mpd(cp, 0); 6870c883cefSAlexander Motin if (cp1 == NULL) 6880c883cefSAlexander Motin return(0); /* Recursion happened. */ 6890c883cefSAlexander Motin } else 6900c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 6910c883cefSAlexander Motin } 6920c883cefSAlexander Motin if (!LIST_EMPTY(&gp->consumer)) 6930c883cefSAlexander Motin return (EINPROGRESS); 6940c883cefSAlexander Motin mtx_destroy(&sc->sc_mtx); 695e770bc6bSMatt Jacob g_free(gp->softc); 696e770bc6bSMatt Jacob gp->softc = NULL; 6970c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s destroyed\n", gp->name); 698e770bc6bSMatt Jacob g_wither_geom(gp, ENXIO); 699e770bc6bSMatt Jacob return (0); 700e770bc6bSMatt Jacob } 701e770bc6bSMatt Jacob 702e770bc6bSMatt Jacob static int 703e770bc6bSMatt Jacob g_multipath_destroy_geom(struct gctl_req *req, struct g_class *mp, 704e770bc6bSMatt Jacob struct g_geom *gp) 705e770bc6bSMatt Jacob { 70612f35a61SPawel Jakub Dawidek 707e770bc6bSMatt Jacob return (g_multipath_destroy(gp)); 708e770bc6bSMatt Jacob } 709e770bc6bSMatt Jacob 710b5dce617SMatt Jacob static int 711b5dce617SMatt Jacob g_multipath_rotate(struct g_geom *gp) 712b5dce617SMatt Jacob { 7138fb378d6SThomas Quinot struct g_consumer *lcp, *first_good_cp = NULL; 714b5dce617SMatt Jacob struct g_multipath_softc *sc = gp->softc; 7158fb378d6SThomas Quinot int active_cp_seen = 0; 716b5dce617SMatt Jacob 717b5dce617SMatt Jacob g_topology_assert(); 718b5dce617SMatt Jacob if (sc == NULL) 719b5dce617SMatt Jacob return (ENXIO); 720b5dce617SMatt Jacob LIST_FOREACH(lcp, &gp->consumer, consumer) { 721b5dce617SMatt Jacob if ((lcp->index & MP_BAD) == 0) { 7228fb378d6SThomas Quinot if (first_good_cp == NULL) 7238fb378d6SThomas Quinot first_good_cp = lcp; 7248fb378d6SThomas Quinot if (active_cp_seen) 725b5dce617SMatt Jacob break; 726b5dce617SMatt Jacob } 7278fb378d6SThomas Quinot if (sc->sc_active == lcp) 7288fb378d6SThomas Quinot active_cp_seen = 1; 729b5dce617SMatt Jacob } 7308fb378d6SThomas Quinot if (lcp == NULL) 7318fb378d6SThomas Quinot lcp = first_good_cp; 7328fb378d6SThomas Quinot if (lcp && lcp != sc->sc_active) { 7330c883cefSAlexander Motin sc->sc_active = lcp; 73463297dfdSAlexander Motin if (sc->sc_active_active != 1) 7350c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s is now active path in %s\n", 736b5dce617SMatt Jacob lcp->provider->name, sc->sc_name); 737b5dce617SMatt Jacob } 738b5dce617SMatt Jacob return (0); 739b5dce617SMatt Jacob } 740b5dce617SMatt Jacob 741e770bc6bSMatt Jacob static void 742e770bc6bSMatt Jacob g_multipath_init(struct g_class *mp) 743e770bc6bSMatt Jacob { 744e770bc6bSMatt Jacob bioq_init(&gmtbq); 745e770bc6bSMatt Jacob mtx_init(&gmtbq_mtx, "gmtbq", NULL, MTX_DEF); 74663297dfdSAlexander Motin kproc_create(g_multipath_kt, mp, NULL, 0, 0, "g_mp_kt"); 747e770bc6bSMatt Jacob } 748e770bc6bSMatt Jacob 749e770bc6bSMatt Jacob static void 750e770bc6bSMatt Jacob g_multipath_fini(struct g_class *mp) 751e770bc6bSMatt Jacob { 752e770bc6bSMatt Jacob if (g_multipath_kt_state == GKT_RUN) { 753e770bc6bSMatt Jacob mtx_lock(&gmtbq_mtx); 754e770bc6bSMatt Jacob g_multipath_kt_state = GKT_DIE; 755e770bc6bSMatt Jacob wakeup(&g_multipath_kt_state); 756e770bc6bSMatt Jacob msleep(&g_multipath_kt_state, &gmtbq_mtx, PRIBIO, 757e770bc6bSMatt Jacob "gmp:fini", 0); 758e770bc6bSMatt Jacob mtx_unlock(&gmtbq_mtx); 759e770bc6bSMatt Jacob } 760e770bc6bSMatt Jacob } 761e770bc6bSMatt Jacob 762e770bc6bSMatt Jacob static int 763e770bc6bSMatt Jacob g_multipath_read_metadata(struct g_consumer *cp, 764e770bc6bSMatt Jacob struct g_multipath_metadata *md) 765e770bc6bSMatt Jacob { 766e770bc6bSMatt Jacob struct g_provider *pp; 767e770bc6bSMatt Jacob u_char *buf; 768e770bc6bSMatt Jacob int error; 769e770bc6bSMatt Jacob 770e770bc6bSMatt Jacob g_topology_assert(); 771e770bc6bSMatt Jacob error = g_access(cp, 1, 0, 0); 77212f35a61SPawel Jakub Dawidek if (error != 0) 773e770bc6bSMatt Jacob return (error); 774e770bc6bSMatt Jacob pp = cp->provider; 775e770bc6bSMatt Jacob g_topology_unlock(); 776e770bc6bSMatt Jacob buf = g_read_data(cp, pp->mediasize - pp->sectorsize, 777e770bc6bSMatt Jacob pp->sectorsize, &error); 778e770bc6bSMatt Jacob g_topology_lock(); 779e770bc6bSMatt Jacob g_access(cp, -1, 0, 0); 78012f35a61SPawel Jakub Dawidek if (buf == NULL) 781e770bc6bSMatt Jacob return (error); 782e770bc6bSMatt Jacob multipath_metadata_decode(buf, md); 783e770bc6bSMatt Jacob g_free(buf); 784e770bc6bSMatt Jacob return (0); 785e770bc6bSMatt Jacob } 786e770bc6bSMatt Jacob 787f8c79813SAlexander Motin static int 788f8c79813SAlexander Motin g_multipath_write_metadata(struct g_consumer *cp, 789f8c79813SAlexander Motin struct g_multipath_metadata *md) 790f8c79813SAlexander Motin { 791f8c79813SAlexander Motin struct g_provider *pp; 792f8c79813SAlexander Motin u_char *buf; 793f8c79813SAlexander Motin int error; 794f8c79813SAlexander Motin 795f8c79813SAlexander Motin g_topology_assert(); 796f8c79813SAlexander Motin error = g_access(cp, 1, 1, 1); 797f8c79813SAlexander Motin if (error != 0) 798f8c79813SAlexander Motin return (error); 799f8c79813SAlexander Motin pp = cp->provider; 800f8c79813SAlexander Motin g_topology_unlock(); 801f8c79813SAlexander Motin buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 802f8c79813SAlexander Motin multipath_metadata_encode(md, buf); 803f8c79813SAlexander Motin error = g_write_data(cp, pp->mediasize - pp->sectorsize, 804f8c79813SAlexander Motin buf, pp->sectorsize); 805f8c79813SAlexander Motin g_topology_lock(); 806f8c79813SAlexander Motin g_access(cp, -1, -1, -1); 807f8c79813SAlexander Motin g_free(buf); 808f8c79813SAlexander Motin return (error); 809f8c79813SAlexander Motin } 810f8c79813SAlexander Motin 811e770bc6bSMatt Jacob static struct g_geom * 812e770bc6bSMatt Jacob g_multipath_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 813e770bc6bSMatt Jacob { 814e770bc6bSMatt Jacob struct g_multipath_metadata md; 815e770bc6bSMatt Jacob struct g_multipath_softc *sc; 816e770bc6bSMatt Jacob struct g_consumer *cp; 817e770bc6bSMatt Jacob struct g_geom *gp, *gp1; 818e770bc6bSMatt Jacob int error, isnew; 819e770bc6bSMatt Jacob 820e770bc6bSMatt Jacob g_topology_assert(); 821e770bc6bSMatt Jacob 822e770bc6bSMatt Jacob gp = g_new_geomf(mp, "multipath:taste"); 823e770bc6bSMatt Jacob gp->start = g_multipath_start; 824e770bc6bSMatt Jacob gp->access = g_multipath_access; 825e770bc6bSMatt Jacob gp->orphan = g_multipath_orphan; 826e770bc6bSMatt Jacob cp = g_new_consumer(gp); 827e770bc6bSMatt Jacob g_attach(cp, pp); 828e770bc6bSMatt Jacob error = g_multipath_read_metadata(cp, &md); 829e770bc6bSMatt Jacob g_detach(cp); 830e770bc6bSMatt Jacob g_destroy_consumer(cp); 831e770bc6bSMatt Jacob g_destroy_geom(gp); 83212f35a61SPawel Jakub Dawidek if (error != 0) 833e770bc6bSMatt Jacob return (NULL); 834e770bc6bSMatt Jacob gp = NULL; 835e770bc6bSMatt Jacob 836e770bc6bSMatt Jacob if (strcmp(md.md_magic, G_MULTIPATH_MAGIC) != 0) { 83712f35a61SPawel Jakub Dawidek if (g_multipath_debug) 838e770bc6bSMatt Jacob printf("%s is not MULTIPATH\n", pp->name); 839e770bc6bSMatt Jacob return (NULL); 840e770bc6bSMatt Jacob } 841e770bc6bSMatt Jacob if (md.md_version != G_MULTIPATH_VERSION) { 842e770bc6bSMatt Jacob printf("%s has version %d multipath id- this module is version " 843e770bc6bSMatt Jacob " %d: rejecting\n", pp->name, md.md_version, 844e770bc6bSMatt Jacob G_MULTIPATH_VERSION); 845e770bc6bSMatt Jacob return (NULL); 846e770bc6bSMatt Jacob } 8470c883cefSAlexander Motin if (md.md_size != 0 && md.md_size != pp->mediasize) 8480c883cefSAlexander Motin return (NULL); 8490c883cefSAlexander Motin if (md.md_sectorsize != 0 && md.md_sectorsize != pp->sectorsize) 8500c883cefSAlexander Motin return (NULL); 85112f35a61SPawel Jakub Dawidek if (g_multipath_debug) 852e770bc6bSMatt Jacob printf("MULTIPATH: %s/%s\n", md.md_name, md.md_uuid); 85367f72211SAlan Somers SDT_PROBE2(geom, multipath, config, taste, md.md_name, md.md_uuid); 854e770bc6bSMatt Jacob 855e770bc6bSMatt Jacob /* 856e770bc6bSMatt Jacob * Let's check if such a device already is present. We check against 857e770bc6bSMatt Jacob * uuid alone first because that's the true distinguishor. If that 858e770bc6bSMatt Jacob * passes, then we check for name conflicts. If there are conflicts, 859e770bc6bSMatt Jacob * modify the name. 860e770bc6bSMatt Jacob * 861e770bc6bSMatt Jacob * The whole purpose of this is to solve the problem that people don't 862e770bc6bSMatt Jacob * pick good unique names, but good unique names (like uuids) are a 863e770bc6bSMatt Jacob * pain to use. So, we allow people to build GEOMs with friendly names 864e770bc6bSMatt Jacob * and uuids, and modify the names in case there's a collision. 865e770bc6bSMatt Jacob */ 866e770bc6bSMatt Jacob sc = NULL; 867e770bc6bSMatt Jacob LIST_FOREACH(gp, &mp->geom, geom) { 868e770bc6bSMatt Jacob sc = gp->softc; 8690c883cefSAlexander Motin if (sc == NULL || sc->sc_stopping) 870e770bc6bSMatt Jacob continue; 87112f35a61SPawel Jakub Dawidek if (strncmp(md.md_uuid, sc->sc_uuid, sizeof(md.md_uuid)) == 0) 872e770bc6bSMatt Jacob break; 873e770bc6bSMatt Jacob } 874e770bc6bSMatt Jacob 875e770bc6bSMatt Jacob LIST_FOREACH(gp1, &mp->geom, geom) { 87612f35a61SPawel Jakub Dawidek if (gp1 == gp) 877e770bc6bSMatt Jacob continue; 878e770bc6bSMatt Jacob sc = gp1->softc; 8790c883cefSAlexander Motin if (sc == NULL || sc->sc_stopping) 880e770bc6bSMatt Jacob continue; 88112f35a61SPawel Jakub Dawidek if (strncmp(md.md_name, sc->sc_name, sizeof(md.md_name)) == 0) 882e770bc6bSMatt Jacob break; 883e770bc6bSMatt Jacob } 884e770bc6bSMatt Jacob 885e770bc6bSMatt Jacob /* 886e770bc6bSMatt Jacob * If gp is NULL, we had no extant MULTIPATH geom with this uuid. 887e770bc6bSMatt Jacob * 888e770bc6bSMatt Jacob * If gp1 is *not* NULL, that means we have a MULTIPATH geom extant 889e770bc6bSMatt Jacob * with the same name (but a different UUID). 890e770bc6bSMatt Jacob * 891e770bc6bSMatt Jacob * If gp is NULL, then modify the name with a random number and 892e770bc6bSMatt Jacob * complain, but allow the creation of the geom to continue. 893e770bc6bSMatt Jacob * 894e770bc6bSMatt Jacob * If gp is *not* NULL, just use the geom's name as we're attaching 895e770bc6bSMatt Jacob * this disk to the (previously generated) name. 896e770bc6bSMatt Jacob */ 897e770bc6bSMatt Jacob 898e770bc6bSMatt Jacob if (gp1) { 899e770bc6bSMatt Jacob sc = gp1->softc; 900e770bc6bSMatt Jacob if (gp == NULL) { 901e770bc6bSMatt Jacob char buf[16]; 902e770bc6bSMatt Jacob u_long rand = random(); 903e770bc6bSMatt Jacob 904e770bc6bSMatt Jacob snprintf(buf, sizeof (buf), "%s-%lu", md.md_name, rand); 905e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: geom %s/%s exists already\n", 906e770bc6bSMatt Jacob sc->sc_name, sc->sc_uuid); 907e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: %s will be (temporarily) %s\n", 908e770bc6bSMatt Jacob md.md_uuid, buf); 909e770bc6bSMatt Jacob strlcpy(md.md_name, buf, sizeof(md.md_name)); 910e770bc6bSMatt Jacob } else { 911e770bc6bSMatt Jacob strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name)); 912e770bc6bSMatt Jacob } 913e770bc6bSMatt Jacob } 914e770bc6bSMatt Jacob 915e770bc6bSMatt Jacob if (gp == NULL) { 916e770bc6bSMatt Jacob gp = g_multipath_create(mp, &md); 917e770bc6bSMatt Jacob if (gp == NULL) { 918e770bc6bSMatt Jacob printf("GEOM_MULTIPATH: cannot create geom %s/%s\n", 919e770bc6bSMatt Jacob md.md_name, md.md_uuid); 920e770bc6bSMatt Jacob return (NULL); 921e770bc6bSMatt Jacob } 922e770bc6bSMatt Jacob isnew = 1; 923e770bc6bSMatt Jacob } else { 924e770bc6bSMatt Jacob isnew = 0; 925e770bc6bSMatt Jacob } 926e770bc6bSMatt Jacob 927e770bc6bSMatt Jacob sc = gp->softc; 928e770bc6bSMatt Jacob KASSERT(sc != NULL, ("sc is NULL")); 929e770bc6bSMatt Jacob error = g_multipath_add_disk(gp, pp); 930e770bc6bSMatt Jacob if (error != 0) { 93112f35a61SPawel Jakub Dawidek if (isnew) 932e770bc6bSMatt Jacob g_multipath_destroy(gp); 933e770bc6bSMatt Jacob return (NULL); 934e770bc6bSMatt Jacob } 935e770bc6bSMatt Jacob return (gp); 936e770bc6bSMatt Jacob } 937e770bc6bSMatt Jacob 938e770bc6bSMatt Jacob static void 9390c883cefSAlexander Motin g_multipath_ctl_add_name(struct gctl_req *req, struct g_class *mp, 9400c883cefSAlexander Motin const char *name) 941e770bc6bSMatt Jacob { 9420c883cefSAlexander Motin struct g_multipath_softc *sc; 943e770bc6bSMatt Jacob struct g_geom *gp; 9442b4969ffSMatt Jacob struct g_consumer *cp; 9450c883cefSAlexander Motin struct g_provider *pp; 9460c883cefSAlexander Motin const char *mpname; 947*8510f61aSXin LI static const char devpf[6] = _PATH_DEV; 948d3fef0a0SAlexander Motin int error; 949e770bc6bSMatt Jacob 950e770bc6bSMatt Jacob g_topology_assert(); 951e770bc6bSMatt Jacob 952e770bc6bSMatt Jacob mpname = gctl_get_asciiparam(req, "arg0"); 953e770bc6bSMatt Jacob if (mpname == NULL) { 954e770bc6bSMatt Jacob gctl_error(req, "No 'arg0' argument"); 955e770bc6bSMatt Jacob return; 956e770bc6bSMatt Jacob } 9572b4969ffSMatt Jacob gp = g_multipath_find_geom(mp, mpname); 9582b4969ffSMatt Jacob if (gp == NULL) { 9592b4969ffSMatt Jacob gctl_error(req, "Device %s is invalid", mpname); 960e770bc6bSMatt Jacob return; 961e770bc6bSMatt Jacob } 9620c883cefSAlexander Motin sc = gp->softc; 963e770bc6bSMatt Jacob 96412f35a61SPawel Jakub Dawidek if (strncmp(name, devpf, 5) == 0) 965e770bc6bSMatt Jacob name += 5; 9662b4969ffSMatt Jacob pp = g_provider_by_name(name); 9672b4969ffSMatt Jacob if (pp == NULL) { 968e770bc6bSMatt Jacob gctl_error(req, "Provider %s is invalid", name); 969e770bc6bSMatt Jacob return; 970e770bc6bSMatt Jacob } 971e770bc6bSMatt Jacob 972e770bc6bSMatt Jacob /* 9730c883cefSAlexander Motin * Check to make sure parameters match. 974e770bc6bSMatt Jacob */ 9750c883cefSAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer) { 9760c883cefSAlexander Motin if (cp->provider == pp) { 9770c883cefSAlexander Motin gctl_error(req, "provider %s is already there", 9780c883cefSAlexander Motin pp->name); 979e770bc6bSMatt Jacob return; 980e770bc6bSMatt Jacob } 9810c883cefSAlexander Motin } 982e6afd72bSAlexander Motin if (sc->sc_pp->mediasize != 0 && 9830c883cefSAlexander Motin sc->sc_pp->mediasize + (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0) 9840c883cefSAlexander Motin != pp->mediasize) { 9850c883cefSAlexander Motin gctl_error(req, "Providers size mismatch %jd != %jd", 9860c883cefSAlexander Motin (intmax_t) sc->sc_pp->mediasize + 9870c883cefSAlexander Motin (sc->sc_uuid[0] != 0 ? pp->sectorsize : 0), 9880c883cefSAlexander Motin (intmax_t) pp->mediasize); 989e770bc6bSMatt Jacob return; 990e770bc6bSMatt Jacob } 991e6afd72bSAlexander Motin if (sc->sc_pp->sectorsize != 0 && 9920c883cefSAlexander Motin sc->sc_pp->sectorsize != pp->sectorsize) { 9930c883cefSAlexander Motin gctl_error(req, "Providers sectorsize mismatch %u != %u", 9940c883cefSAlexander Motin sc->sc_pp->sectorsize, pp->sectorsize); 995e770bc6bSMatt Jacob return; 996e770bc6bSMatt Jacob } 997e770bc6bSMatt Jacob 998d3fef0a0SAlexander Motin error = g_multipath_add_disk(gp, pp); 999d3fef0a0SAlexander Motin if (error != 0) 1000d3fef0a0SAlexander Motin gctl_error(req, "Provider addition error: %d", error); 1001e770bc6bSMatt Jacob } 1002e770bc6bSMatt Jacob 10030c883cefSAlexander Motin static void 100471ee4ef0SThomas Quinot g_multipath_ctl_prefer(struct gctl_req *req, struct g_class *mp) 100571ee4ef0SThomas Quinot { 100671ee4ef0SThomas Quinot struct g_geom *gp; 100771ee4ef0SThomas Quinot struct g_multipath_softc *sc; 100871ee4ef0SThomas Quinot struct g_consumer *cp; 100971ee4ef0SThomas Quinot const char *name, *mpname; 1010*8510f61aSXin LI static const char devpf[6] = _PATH_DEV; 101171ee4ef0SThomas Quinot int *nargs; 101271ee4ef0SThomas Quinot 101371ee4ef0SThomas Quinot g_topology_assert(); 101471ee4ef0SThomas Quinot 101571ee4ef0SThomas Quinot mpname = gctl_get_asciiparam(req, "arg0"); 101671ee4ef0SThomas Quinot if (mpname == NULL) { 101771ee4ef0SThomas Quinot gctl_error(req, "No 'arg0' argument"); 101871ee4ef0SThomas Quinot return; 101971ee4ef0SThomas Quinot } 102071ee4ef0SThomas Quinot gp = g_multipath_find_geom(mp, mpname); 102171ee4ef0SThomas Quinot if (gp == NULL) { 102271ee4ef0SThomas Quinot gctl_error(req, "Device %s is invalid", mpname); 102371ee4ef0SThomas Quinot return; 102471ee4ef0SThomas Quinot } 102571ee4ef0SThomas Quinot sc = gp->softc; 102671ee4ef0SThomas Quinot 102771ee4ef0SThomas Quinot nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 102871ee4ef0SThomas Quinot if (nargs == NULL) { 102971ee4ef0SThomas Quinot gctl_error(req, "No 'nargs' argument"); 103071ee4ef0SThomas Quinot return; 103171ee4ef0SThomas Quinot } 103271ee4ef0SThomas Quinot if (*nargs != 2) { 103371ee4ef0SThomas Quinot gctl_error(req, "missing device"); 103471ee4ef0SThomas Quinot return; 103571ee4ef0SThomas Quinot } 103671ee4ef0SThomas Quinot 103771ee4ef0SThomas Quinot name = gctl_get_asciiparam(req, "arg1"); 103871ee4ef0SThomas Quinot if (name == NULL) { 103971ee4ef0SThomas Quinot gctl_error(req, "No 'arg1' argument"); 104071ee4ef0SThomas Quinot return; 104171ee4ef0SThomas Quinot } 104271ee4ef0SThomas Quinot if (strncmp(name, devpf, 5) == 0) { 104371ee4ef0SThomas Quinot name += 5; 104471ee4ef0SThomas Quinot } 104571ee4ef0SThomas Quinot 104671ee4ef0SThomas Quinot LIST_FOREACH(cp, &gp->consumer, consumer) { 104771ee4ef0SThomas Quinot if (cp->provider != NULL 104871ee4ef0SThomas Quinot && strcmp(cp->provider->name, name) == 0) 104971ee4ef0SThomas Quinot break; 105071ee4ef0SThomas Quinot } 105171ee4ef0SThomas Quinot 105271ee4ef0SThomas Quinot if (cp == NULL) { 105371ee4ef0SThomas Quinot gctl_error(req, "Provider %s not found", name); 105471ee4ef0SThomas Quinot return; 105571ee4ef0SThomas Quinot } 105671ee4ef0SThomas Quinot 105771ee4ef0SThomas Quinot mtx_lock(&sc->sc_mtx); 105871ee4ef0SThomas Quinot 105971ee4ef0SThomas Quinot if (cp->index & MP_BAD) { 106071ee4ef0SThomas Quinot gctl_error(req, "Consumer %s is invalid", name); 106171ee4ef0SThomas Quinot mtx_unlock(&sc->sc_mtx); 106271ee4ef0SThomas Quinot return; 106371ee4ef0SThomas Quinot } 106471ee4ef0SThomas Quinot 106571ee4ef0SThomas Quinot /* Here when the consumer is present and in good shape */ 106671ee4ef0SThomas Quinot 106771ee4ef0SThomas Quinot sc->sc_active = cp; 106871ee4ef0SThomas Quinot if (!sc->sc_active_active) 106971ee4ef0SThomas Quinot printf("GEOM_MULTIPATH: %s now active path in %s\n", 107071ee4ef0SThomas Quinot sc->sc_active->provider->name, sc->sc_name); 107171ee4ef0SThomas Quinot 107271ee4ef0SThomas Quinot mtx_unlock(&sc->sc_mtx); 107371ee4ef0SThomas Quinot } 107471ee4ef0SThomas Quinot 107571ee4ef0SThomas Quinot static void 10760c883cefSAlexander Motin g_multipath_ctl_add(struct gctl_req *req, struct g_class *mp) 10770c883cefSAlexander Motin { 10780c883cefSAlexander Motin struct g_multipath_softc *sc; 10790c883cefSAlexander Motin struct g_geom *gp; 10800c883cefSAlexander Motin const char *mpname, *name; 10810c883cefSAlexander Motin 10820c883cefSAlexander Motin mpname = gctl_get_asciiparam(req, "arg0"); 10830c883cefSAlexander Motin if (mpname == NULL) { 10840c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 10850c883cefSAlexander Motin return; 10860c883cefSAlexander Motin } 10870c883cefSAlexander Motin gp = g_multipath_find_geom(mp, mpname); 10880c883cefSAlexander Motin if (gp == NULL) { 10890c883cefSAlexander Motin gctl_error(req, "Device %s not found", mpname); 10900c883cefSAlexander Motin return; 10910c883cefSAlexander Motin } 10920c883cefSAlexander Motin sc = gp->softc; 10930c883cefSAlexander Motin 10940c883cefSAlexander Motin name = gctl_get_asciiparam(req, "arg1"); 10950c883cefSAlexander Motin if (name == NULL) { 10960c883cefSAlexander Motin gctl_error(req, "No 'arg1' argument"); 10970c883cefSAlexander Motin return; 10980c883cefSAlexander Motin } 10990c883cefSAlexander Motin g_multipath_ctl_add_name(req, mp, name); 11000c883cefSAlexander Motin } 11010c883cefSAlexander Motin 11020c883cefSAlexander Motin static void 11030c883cefSAlexander Motin g_multipath_ctl_create(struct gctl_req *req, struct g_class *mp) 11040c883cefSAlexander Motin { 11050c883cefSAlexander Motin struct g_multipath_metadata md; 11060c883cefSAlexander Motin struct g_multipath_softc *sc; 11070c883cefSAlexander Motin struct g_geom *gp; 11080c883cefSAlexander Motin const char *mpname, *name; 11090c883cefSAlexander Motin char param[16]; 111063297dfdSAlexander Motin int *nargs, i, *val; 11110c883cefSAlexander Motin 11120c883cefSAlexander Motin g_topology_assert(); 11130c883cefSAlexander Motin 11140c883cefSAlexander Motin nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 11150c883cefSAlexander Motin if (*nargs < 2) { 11160c883cefSAlexander Motin gctl_error(req, "wrong number of arguments."); 11170c883cefSAlexander Motin return; 11180c883cefSAlexander Motin } 11190c883cefSAlexander Motin 11200c883cefSAlexander Motin mpname = gctl_get_asciiparam(req, "arg0"); 11210c883cefSAlexander Motin if (mpname == NULL) { 11220c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 11230c883cefSAlexander Motin return; 11240c883cefSAlexander Motin } 11250c883cefSAlexander Motin gp = g_multipath_find_geom(mp, mpname); 11260c883cefSAlexander Motin if (gp != NULL) { 11270c883cefSAlexander Motin gctl_error(req, "Device %s already exist", mpname); 11280c883cefSAlexander Motin return; 11290c883cefSAlexander Motin } 11300c883cefSAlexander Motin 11310c883cefSAlexander Motin memset(&md, 0, sizeof(md)); 11320c883cefSAlexander Motin strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic)); 11330c883cefSAlexander Motin md.md_version = G_MULTIPATH_VERSION; 11340c883cefSAlexander Motin strlcpy(md.md_name, mpname, sizeof(md.md_name)); 11350c883cefSAlexander Motin md.md_size = 0; 11360c883cefSAlexander Motin md.md_sectorsize = 0; 11370c883cefSAlexander Motin md.md_uuid[0] = 0; 113863297dfdSAlexander Motin md.md_active_active = 0; 113963297dfdSAlexander Motin val = gctl_get_paraml(req, "active_active", sizeof(*val)); 114063297dfdSAlexander Motin if (val != NULL && *val != 0) 114163297dfdSAlexander Motin md.md_active_active = 1; 114263297dfdSAlexander Motin val = gctl_get_paraml(req, "active_read", sizeof(*val)); 114363297dfdSAlexander Motin if (val != NULL && *val != 0) 114463297dfdSAlexander Motin md.md_active_active = 2; 11450c883cefSAlexander Motin gp = g_multipath_create(mp, &md); 11460c883cefSAlexander Motin if (gp == NULL) { 11470c883cefSAlexander Motin gctl_error(req, "GEOM_MULTIPATH: cannot create geom %s/%s\n", 11480c883cefSAlexander Motin md.md_name, md.md_uuid); 11490c883cefSAlexander Motin return; 11500c883cefSAlexander Motin } 11510c883cefSAlexander Motin sc = gp->softc; 11520c883cefSAlexander Motin 11530c883cefSAlexander Motin for (i = 1; i < *nargs; i++) { 11540c883cefSAlexander Motin snprintf(param, sizeof(param), "arg%d", i); 11550c883cefSAlexander Motin name = gctl_get_asciiparam(req, param); 11560c883cefSAlexander Motin g_multipath_ctl_add_name(req, mp, name); 11570c883cefSAlexander Motin } 11580c883cefSAlexander Motin 11590c883cefSAlexander Motin if (sc->sc_ndisks != (*nargs - 1)) 11600c883cefSAlexander Motin g_multipath_destroy(gp); 11610c883cefSAlexander Motin } 11620c883cefSAlexander Motin 11630c883cefSAlexander Motin static void 116463297dfdSAlexander Motin g_multipath_ctl_configure(struct gctl_req *req, struct g_class *mp) 116563297dfdSAlexander Motin { 116663297dfdSAlexander Motin struct g_multipath_softc *sc; 116763297dfdSAlexander Motin struct g_geom *gp; 116863297dfdSAlexander Motin struct g_consumer *cp; 116963297dfdSAlexander Motin struct g_provider *pp; 1170c0b1ef66SAlexander Motin struct g_multipath_metadata md; 117163297dfdSAlexander Motin const char *name; 117263297dfdSAlexander Motin int error, *val; 117363297dfdSAlexander Motin 117463297dfdSAlexander Motin g_topology_assert(); 117563297dfdSAlexander Motin 117663297dfdSAlexander Motin name = gctl_get_asciiparam(req, "arg0"); 117763297dfdSAlexander Motin if (name == NULL) { 117863297dfdSAlexander Motin gctl_error(req, "No 'arg0' argument"); 117963297dfdSAlexander Motin return; 118063297dfdSAlexander Motin } 118163297dfdSAlexander Motin gp = g_multipath_find_geom(mp, name); 118263297dfdSAlexander Motin if (gp == NULL) { 118363297dfdSAlexander Motin gctl_error(req, "Device %s is invalid", name); 118463297dfdSAlexander Motin return; 118563297dfdSAlexander Motin } 118663297dfdSAlexander Motin sc = gp->softc; 118763297dfdSAlexander Motin val = gctl_get_paraml(req, "active_active", sizeof(*val)); 118863297dfdSAlexander Motin if (val != NULL && *val != 0) 118963297dfdSAlexander Motin sc->sc_active_active = 1; 119063297dfdSAlexander Motin val = gctl_get_paraml(req, "active_read", sizeof(*val)); 119163297dfdSAlexander Motin if (val != NULL && *val != 0) 119263297dfdSAlexander Motin sc->sc_active_active = 2; 119363297dfdSAlexander Motin val = gctl_get_paraml(req, "active_passive", sizeof(*val)); 119463297dfdSAlexander Motin if (val != NULL && *val != 0) 119563297dfdSAlexander Motin sc->sc_active_active = 0; 119663297dfdSAlexander Motin if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) { 119763297dfdSAlexander Motin cp = sc->sc_active; 119863297dfdSAlexander Motin pp = cp->provider; 1199c0b1ef66SAlexander Motin strlcpy(md.md_magic, G_MULTIPATH_MAGIC, sizeof(md.md_magic)); 1200c0b1ef66SAlexander Motin memcpy(md.md_uuid, sc->sc_uuid, sizeof (sc->sc_uuid)); 1201c0b1ef66SAlexander Motin strlcpy(md.md_name, name, sizeof(md.md_name)); 1202c0b1ef66SAlexander Motin md.md_version = G_MULTIPATH_VERSION; 1203c0b1ef66SAlexander Motin md.md_size = pp->mediasize; 1204c0b1ef66SAlexander Motin md.md_sectorsize = pp->sectorsize; 1205c0b1ef66SAlexander Motin md.md_active_active = sc->sc_active_active; 1206f8c79813SAlexander Motin error = g_multipath_write_metadata(cp, &md); 120763297dfdSAlexander Motin if (error != 0) 120863297dfdSAlexander Motin gctl_error(req, "Can't update metadata on %s (%d)", 120963297dfdSAlexander Motin pp->name, error); 121063297dfdSAlexander Motin } 121163297dfdSAlexander Motin } 121263297dfdSAlexander Motin 121363297dfdSAlexander Motin static void 12140c883cefSAlexander Motin g_multipath_ctl_fail(struct gctl_req *req, struct g_class *mp, int fail) 12150c883cefSAlexander Motin { 12160c883cefSAlexander Motin struct g_multipath_softc *sc; 12170c883cefSAlexander Motin struct g_geom *gp; 12180c883cefSAlexander Motin struct g_consumer *cp; 12190c883cefSAlexander Motin const char *mpname, *name; 12200c883cefSAlexander Motin int found; 12210c883cefSAlexander Motin 12220c883cefSAlexander Motin mpname = gctl_get_asciiparam(req, "arg0"); 12230c883cefSAlexander Motin if (mpname == NULL) { 12240c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 12250c883cefSAlexander Motin return; 12260c883cefSAlexander Motin } 12270c883cefSAlexander Motin gp = g_multipath_find_geom(mp, mpname); 12280c883cefSAlexander Motin if (gp == NULL) { 12290c883cefSAlexander Motin gctl_error(req, "Device %s not found", mpname); 12300c883cefSAlexander Motin return; 12310c883cefSAlexander Motin } 12320c883cefSAlexander Motin sc = gp->softc; 12330c883cefSAlexander Motin 12340c883cefSAlexander Motin name = gctl_get_asciiparam(req, "arg1"); 12350c883cefSAlexander Motin if (name == NULL) { 12360c883cefSAlexander Motin gctl_error(req, "No 'arg1' argument"); 12370c883cefSAlexander Motin return; 12380c883cefSAlexander Motin } 12390c883cefSAlexander Motin 12400c883cefSAlexander Motin found = 0; 12410c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 12420c883cefSAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer) { 12430c883cefSAlexander Motin if (cp->provider != NULL && 12440c883cefSAlexander Motin strcmp(cp->provider->name, name) == 0 && 12450c883cefSAlexander Motin (cp->index & MP_LOST) == 0) { 12460c883cefSAlexander Motin found = 1; 124763297dfdSAlexander Motin if (!fail == !(cp->index & MP_FAIL)) 124863297dfdSAlexander Motin continue; 12490c883cefSAlexander Motin printf("GEOM_MULTIPATH: %s in %s is marked %s.\n", 12500c883cefSAlexander Motin name, sc->sc_name, fail ? "FAIL" : "OK"); 12510c883cefSAlexander Motin if (fail) { 12520c883cefSAlexander Motin g_multipath_fault(cp, MP_FAIL); 125367f72211SAlan Somers SDT_PROBE3(geom, multipath, config, fail, 125467f72211SAlan Somers sc->sc_name, cp->provider->name, 0); 12550c883cefSAlexander Motin } else { 12560c883cefSAlexander Motin cp->index &= ~MP_FAIL; 125767f72211SAlan Somers SDT_PROBE2(geom, multipath, config, restore, 125867f72211SAlan Somers sc->sc_name, cp->provider->name); 12590c883cefSAlexander Motin } 12600c883cefSAlexander Motin } 12610c883cefSAlexander Motin } 12620c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 12630c883cefSAlexander Motin if (found == 0) 12640c883cefSAlexander Motin gctl_error(req, "Provider %s not found", name); 12650c883cefSAlexander Motin } 12660c883cefSAlexander Motin 12670c883cefSAlexander Motin static void 12680c883cefSAlexander Motin g_multipath_ctl_remove(struct gctl_req *req, struct g_class *mp) 12690c883cefSAlexander Motin { 12700c883cefSAlexander Motin struct g_multipath_softc *sc; 12710c883cefSAlexander Motin struct g_geom *gp; 12720c883cefSAlexander Motin struct g_consumer *cp, *cp1; 12730c883cefSAlexander Motin const char *mpname, *name; 12740c883cefSAlexander Motin uintptr_t *cnt; 12750c883cefSAlexander Motin int found; 12760c883cefSAlexander Motin 12770c883cefSAlexander Motin mpname = gctl_get_asciiparam(req, "arg0"); 12780c883cefSAlexander Motin if (mpname == NULL) { 12790c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 12800c883cefSAlexander Motin return; 12810c883cefSAlexander Motin } 12820c883cefSAlexander Motin gp = g_multipath_find_geom(mp, mpname); 12830c883cefSAlexander Motin if (gp == NULL) { 12840c883cefSAlexander Motin gctl_error(req, "Device %s not found", mpname); 12850c883cefSAlexander Motin return; 12860c883cefSAlexander Motin } 12870c883cefSAlexander Motin sc = gp->softc; 12880c883cefSAlexander Motin 12890c883cefSAlexander Motin name = gctl_get_asciiparam(req, "arg1"); 12900c883cefSAlexander Motin if (name == NULL) { 12910c883cefSAlexander Motin gctl_error(req, "No 'arg1' argument"); 12920c883cefSAlexander Motin return; 12930c883cefSAlexander Motin } 12940c883cefSAlexander Motin 12950c883cefSAlexander Motin found = 0; 12960c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 12970c883cefSAlexander Motin LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) { 12980c883cefSAlexander Motin if (cp->provider != NULL && 12990c883cefSAlexander Motin strcmp(cp->provider->name, name) == 0 && 13000c883cefSAlexander Motin (cp->index & MP_LOST) == 0) { 13010c883cefSAlexander Motin found = 1; 13020c883cefSAlexander Motin printf("GEOM_MULTIPATH: removing %s from %s\n", 13030c883cefSAlexander Motin cp->provider->name, cp->geom->name); 130467f72211SAlan Somers SDT_PROBE2(geom, multipath, config, remove, 130567f72211SAlan Somers cp->geom->name, cp->provider->name); 13060c883cefSAlexander Motin sc->sc_ndisks--; 13070c883cefSAlexander Motin g_multipath_fault(cp, MP_LOST); 13080c883cefSAlexander Motin cnt = (uintptr_t *)&cp->private; 13090c883cefSAlexander Motin if (*cnt == 0 && (cp->index & MP_POSTED) == 0) { 13100c883cefSAlexander Motin cp->index |= MP_POSTED; 13110c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 13120c883cefSAlexander Motin g_mpd(cp, 0); 13130c883cefSAlexander Motin if (cp1 == NULL) 13140c883cefSAlexander Motin return; /* Recursion happened. */ 13150c883cefSAlexander Motin mtx_lock(&sc->sc_mtx); 13160c883cefSAlexander Motin } 13170c883cefSAlexander Motin } 13180c883cefSAlexander Motin } 13190c883cefSAlexander Motin mtx_unlock(&sc->sc_mtx); 13200c883cefSAlexander Motin if (found == 0) 13210c883cefSAlexander Motin gctl_error(req, "Provider %s not found", name); 13220c883cefSAlexander Motin } 13230c883cefSAlexander Motin 1324e770bc6bSMatt Jacob static struct g_geom * 1325e770bc6bSMatt Jacob g_multipath_find_geom(struct g_class *mp, const char *name) 1326e770bc6bSMatt Jacob { 1327e770bc6bSMatt Jacob struct g_geom *gp; 13280c883cefSAlexander Motin struct g_multipath_softc *sc; 1329e770bc6bSMatt Jacob 1330e770bc6bSMatt Jacob LIST_FOREACH(gp, &mp->geom, geom) { 13310c883cefSAlexander Motin sc = gp->softc; 13320c883cefSAlexander Motin if (sc == NULL || sc->sc_stopping) 13330c883cefSAlexander Motin continue; 13340c883cefSAlexander Motin if (strcmp(gp->name, name) == 0) 1335e770bc6bSMatt Jacob return (gp); 1336e770bc6bSMatt Jacob } 1337e770bc6bSMatt Jacob return (NULL); 1338e770bc6bSMatt Jacob } 1339e770bc6bSMatt Jacob 1340e770bc6bSMatt Jacob static void 13410c883cefSAlexander Motin g_multipath_ctl_stop(struct gctl_req *req, struct g_class *mp) 1342e770bc6bSMatt Jacob { 1343e770bc6bSMatt Jacob struct g_geom *gp; 1344e770bc6bSMatt Jacob const char *name; 1345e770bc6bSMatt Jacob int error; 1346e770bc6bSMatt Jacob 1347e770bc6bSMatt Jacob g_topology_assert(); 1348e770bc6bSMatt Jacob 1349e770bc6bSMatt Jacob name = gctl_get_asciiparam(req, "arg0"); 1350e770bc6bSMatt Jacob if (name == NULL) { 1351e770bc6bSMatt Jacob gctl_error(req, "No 'arg0' argument"); 1352e770bc6bSMatt Jacob return; 1353e770bc6bSMatt Jacob } 1354e770bc6bSMatt Jacob gp = g_multipath_find_geom(mp, name); 1355e770bc6bSMatt Jacob if (gp == NULL) { 1356e770bc6bSMatt Jacob gctl_error(req, "Device %s is invalid", name); 1357e770bc6bSMatt Jacob return; 1358e770bc6bSMatt Jacob } 1359e770bc6bSMatt Jacob error = g_multipath_destroy(gp); 13600c883cefSAlexander Motin if (error != 0 && error != EINPROGRESS) 13610c883cefSAlexander Motin gctl_error(req, "failed to stop %s (err=%d)", name, error); 1362e770bc6bSMatt Jacob } 13630c883cefSAlexander Motin 13640c883cefSAlexander Motin static void 13650c883cefSAlexander Motin g_multipath_ctl_destroy(struct gctl_req *req, struct g_class *mp) 13660c883cefSAlexander Motin { 13670c883cefSAlexander Motin struct g_geom *gp; 13680c883cefSAlexander Motin struct g_multipath_softc *sc; 13690c883cefSAlexander Motin struct g_consumer *cp; 13700c883cefSAlexander Motin struct g_provider *pp; 13710c883cefSAlexander Motin const char *name; 13720c883cefSAlexander Motin uint8_t *buf; 13730c883cefSAlexander Motin int error; 13740c883cefSAlexander Motin 13750c883cefSAlexander Motin g_topology_assert(); 13760c883cefSAlexander Motin 13770c883cefSAlexander Motin name = gctl_get_asciiparam(req, "arg0"); 13780c883cefSAlexander Motin if (name == NULL) { 13790c883cefSAlexander Motin gctl_error(req, "No 'arg0' argument"); 13800c883cefSAlexander Motin return; 13810c883cefSAlexander Motin } 13820c883cefSAlexander Motin gp = g_multipath_find_geom(mp, name); 13830c883cefSAlexander Motin if (gp == NULL) { 13840c883cefSAlexander Motin gctl_error(req, "Device %s is invalid", name); 13850c883cefSAlexander Motin return; 13860c883cefSAlexander Motin } 13870c883cefSAlexander Motin sc = gp->softc; 13880c883cefSAlexander Motin 13890c883cefSAlexander Motin if (sc->sc_uuid[0] != 0 && sc->sc_active != NULL) { 13900c883cefSAlexander Motin cp = sc->sc_active; 13910c883cefSAlexander Motin pp = cp->provider; 13920c883cefSAlexander Motin error = g_access(cp, 1, 1, 1); 13930c883cefSAlexander Motin if (error != 0) { 13940c883cefSAlexander Motin gctl_error(req, "Can't open %s (%d)", pp->name, error); 13950c883cefSAlexander Motin goto destroy; 13960c883cefSAlexander Motin } 13970c883cefSAlexander Motin g_topology_unlock(); 13980c883cefSAlexander Motin buf = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 13990c883cefSAlexander Motin error = g_write_data(cp, pp->mediasize - pp->sectorsize, 14000c883cefSAlexander Motin buf, pp->sectorsize); 14010c883cefSAlexander Motin g_topology_lock(); 14020c883cefSAlexander Motin g_access(cp, -1, -1, -1); 14030c883cefSAlexander Motin if (error != 0) 14040c883cefSAlexander Motin gctl_error(req, "Can't erase metadata on %s (%d)", 14050c883cefSAlexander Motin pp->name, error); 14060c883cefSAlexander Motin } 14070c883cefSAlexander Motin 14080c883cefSAlexander Motin destroy: 14090c883cefSAlexander Motin error = g_multipath_destroy(gp); 14100c883cefSAlexander Motin if (error != 0 && error != EINPROGRESS) 14110c883cefSAlexander Motin gctl_error(req, "failed to destroy %s (err=%d)", name, error); 1412e770bc6bSMatt Jacob } 1413e770bc6bSMatt Jacob 1414e770bc6bSMatt Jacob static void 1415b5dce617SMatt Jacob g_multipath_ctl_rotate(struct gctl_req *req, struct g_class *mp) 1416b5dce617SMatt Jacob { 1417b5dce617SMatt Jacob struct g_geom *gp; 1418b5dce617SMatt Jacob const char *name; 1419b5dce617SMatt Jacob int error; 1420b5dce617SMatt Jacob 1421b5dce617SMatt Jacob g_topology_assert(); 1422b5dce617SMatt Jacob 1423b5dce617SMatt Jacob name = gctl_get_asciiparam(req, "arg0"); 1424b5dce617SMatt Jacob if (name == NULL) { 1425b5dce617SMatt Jacob gctl_error(req, "No 'arg0' argument"); 1426b5dce617SMatt Jacob return; 1427b5dce617SMatt Jacob } 1428b5dce617SMatt Jacob gp = g_multipath_find_geom(mp, name); 1429b5dce617SMatt Jacob if (gp == NULL) { 1430b5dce617SMatt Jacob gctl_error(req, "Device %s is invalid", name); 1431b5dce617SMatt Jacob return; 1432b5dce617SMatt Jacob } 1433b5dce617SMatt Jacob error = g_multipath_rotate(gp); 1434b5dce617SMatt Jacob if (error != 0) { 1435b5dce617SMatt Jacob gctl_error(req, "failed to rotate %s (err=%d)", name, error); 1436b5dce617SMatt Jacob } 1437b5dce617SMatt Jacob } 1438b5dce617SMatt Jacob 1439b5dce617SMatt Jacob static void 1440b5dce617SMatt Jacob g_multipath_ctl_getactive(struct gctl_req *req, struct g_class *mp) 1441b5dce617SMatt Jacob { 1442b5dce617SMatt Jacob struct sbuf *sb; 1443b5dce617SMatt Jacob struct g_geom *gp; 1444b5dce617SMatt Jacob struct g_multipath_softc *sc; 14450c883cefSAlexander Motin struct g_consumer *cp; 1446b5dce617SMatt Jacob const char *name; 14470c883cefSAlexander Motin int empty; 1448b5dce617SMatt Jacob 1449b5dce617SMatt Jacob sb = sbuf_new_auto(); 1450b5dce617SMatt Jacob 1451b5dce617SMatt Jacob g_topology_assert(); 1452b5dce617SMatt Jacob name = gctl_get_asciiparam(req, "arg0"); 1453b5dce617SMatt Jacob if (name == NULL) { 1454b5dce617SMatt Jacob gctl_error(req, "No 'arg0' argument"); 1455b5dce617SMatt Jacob return; 1456b5dce617SMatt Jacob } 1457b5dce617SMatt Jacob gp = g_multipath_find_geom(mp, name); 1458b5dce617SMatt Jacob if (gp == NULL) { 1459b5dce617SMatt Jacob gctl_error(req, "Device %s is invalid", name); 1460b5dce617SMatt Jacob return; 1461b5dce617SMatt Jacob } 1462b5dce617SMatt Jacob sc = gp->softc; 146363297dfdSAlexander Motin if (sc->sc_active_active == 1) { 14640c883cefSAlexander Motin empty = 1; 14650c883cefSAlexander Motin LIST_FOREACH(cp, &gp->consumer, consumer) { 14660c883cefSAlexander Motin if (cp->index & MP_BAD) 14670c883cefSAlexander Motin continue; 14680c883cefSAlexander Motin if (!empty) 14690c883cefSAlexander Motin sbuf_cat(sb, " "); 14700c883cefSAlexander Motin sbuf_cat(sb, cp->provider->name); 14710c883cefSAlexander Motin empty = 0; 14720c883cefSAlexander Motin } 14730c883cefSAlexander Motin if (empty) 14740c883cefSAlexander Motin sbuf_cat(sb, "none"); 14750c883cefSAlexander Motin sbuf_cat(sb, "\n"); 14760c883cefSAlexander Motin } else if (sc->sc_active && sc->sc_active->provider) { 14770c883cefSAlexander Motin sbuf_printf(sb, "%s\n", sc->sc_active->provider->name); 1478b5dce617SMatt Jacob } else { 147949ee0fceSAlexander Motin sbuf_cat(sb, "none\n"); 1480b5dce617SMatt Jacob } 1481b5dce617SMatt Jacob sbuf_finish(sb); 1482b5dce617SMatt Jacob gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1); 1483b5dce617SMatt Jacob sbuf_delete(sb); 1484b5dce617SMatt Jacob } 1485b5dce617SMatt Jacob 1486b5dce617SMatt Jacob static void 1487e770bc6bSMatt Jacob g_multipath_config(struct gctl_req *req, struct g_class *mp, const char *verb) 1488e770bc6bSMatt Jacob { 1489e770bc6bSMatt Jacob uint32_t *version; 1490e770bc6bSMatt Jacob g_topology_assert(); 1491e770bc6bSMatt Jacob version = gctl_get_paraml(req, "version", sizeof(*version)); 1492e770bc6bSMatt Jacob if (version == NULL) { 1493e770bc6bSMatt Jacob gctl_error(req, "No 'version' argument"); 1494e770bc6bSMatt Jacob } else if (*version != G_MULTIPATH_VERSION) { 1495e770bc6bSMatt Jacob gctl_error(req, "Userland and kernel parts are out of sync"); 14962b4969ffSMatt Jacob } else if (strcmp(verb, "add") == 0) { 14972b4969ffSMatt Jacob g_multipath_ctl_add(req, mp); 149871ee4ef0SThomas Quinot } else if (strcmp(verb, "prefer") == 0) { 149971ee4ef0SThomas Quinot g_multipath_ctl_prefer(req, mp); 15000c883cefSAlexander Motin } else if (strcmp(verb, "create") == 0) { 15010c883cefSAlexander Motin g_multipath_ctl_create(req, mp); 150263297dfdSAlexander Motin } else if (strcmp(verb, "configure") == 0) { 150363297dfdSAlexander Motin g_multipath_ctl_configure(req, mp); 15040c883cefSAlexander Motin } else if (strcmp(verb, "stop") == 0) { 15050c883cefSAlexander Motin g_multipath_ctl_stop(req, mp); 1506e770bc6bSMatt Jacob } else if (strcmp(verb, "destroy") == 0) { 1507e770bc6bSMatt Jacob g_multipath_ctl_destroy(req, mp); 15080c883cefSAlexander Motin } else if (strcmp(verb, "fail") == 0) { 15090c883cefSAlexander Motin g_multipath_ctl_fail(req, mp, 1); 15100c883cefSAlexander Motin } else if (strcmp(verb, "restore") == 0) { 15110c883cefSAlexander Motin g_multipath_ctl_fail(req, mp, 0); 15120c883cefSAlexander Motin } else if (strcmp(verb, "remove") == 0) { 15130c883cefSAlexander Motin g_multipath_ctl_remove(req, mp); 1514b5dce617SMatt Jacob } else if (strcmp(verb, "rotate") == 0) { 1515b5dce617SMatt Jacob g_multipath_ctl_rotate(req, mp); 1516b5dce617SMatt Jacob } else if (strcmp(verb, "getactive") == 0) { 1517b5dce617SMatt Jacob g_multipath_ctl_getactive(req, mp); 1518e770bc6bSMatt Jacob } else { 1519e770bc6bSMatt Jacob gctl_error(req, "Unknown verb %s", verb); 1520e770bc6bSMatt Jacob } 1521e770bc6bSMatt Jacob } 15220c883cefSAlexander Motin 15230c883cefSAlexander Motin static void 15240c883cefSAlexander Motin g_multipath_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 15250c883cefSAlexander Motin struct g_consumer *cp, struct g_provider *pp) 15260c883cefSAlexander Motin { 15270c883cefSAlexander Motin struct g_multipath_softc *sc; 15280c883cefSAlexander Motin int good; 15290c883cefSAlexander Motin 15300c883cefSAlexander Motin g_topology_assert(); 15310c883cefSAlexander Motin 15320c883cefSAlexander Motin sc = gp->softc; 15330c883cefSAlexander Motin if (sc == NULL) 15340c883cefSAlexander Motin return; 15350c883cefSAlexander Motin if (cp != NULL) { 1536a839e332SAlexander Motin sbuf_printf(sb, "%s<State>%s</State>\n", indent, 15370c883cefSAlexander Motin (cp->index & MP_NEW) ? "NEW" : 15380c883cefSAlexander Motin (cp->index & MP_LOST) ? "LOST" : 15390c883cefSAlexander Motin (cp->index & MP_FAIL) ? "FAIL" : 154063297dfdSAlexander Motin (sc->sc_active_active == 1 || sc->sc_active == cp) ? 154163297dfdSAlexander Motin "ACTIVE" : 154263297dfdSAlexander Motin sc->sc_active_active == 2 ? "READ" : "PASSIVE"); 15430c883cefSAlexander Motin } else { 15440c883cefSAlexander Motin good = g_multipath_good(gp); 1545a839e332SAlexander Motin sbuf_printf(sb, "%s<State>%s</State>\n", indent, 15460c883cefSAlexander Motin good == 0 ? "BROKEN" : 15470c883cefSAlexander Motin (good != sc->sc_ndisks || sc->sc_ndisks == 1) ? 15480c883cefSAlexander Motin "DEGRADED" : "OPTIMAL"); 15490c883cefSAlexander Motin } 15500c883cefSAlexander Motin if (cp == NULL && pp == NULL) { 1551a839e332SAlexander Motin sbuf_printf(sb, "%s<UUID>%s</UUID>\n", indent, sc->sc_uuid); 1552a839e332SAlexander Motin sbuf_printf(sb, "%s<Mode>Active/%s</Mode>\n", indent, 155363297dfdSAlexander Motin sc->sc_active_active == 2 ? "Read" : 155463297dfdSAlexander Motin sc->sc_active_active == 1 ? "Active" : "Passive"); 1555a839e332SAlexander Motin sbuf_printf(sb, "%s<Type>%s</Type>\n", indent, 15560c883cefSAlexander Motin sc->sc_uuid[0] == 0 ? "MANUAL" : "AUTOMATIC"); 15570c883cefSAlexander Motin } 15580c883cefSAlexander Motin } 15590c883cefSAlexander Motin 1560e770bc6bSMatt Jacob DECLARE_GEOM_CLASS(g_multipath_class, g_multipath); 156174d6c131SKyle Evans MODULE_VERSION(geom_multipath, 0); 1562