1129473Spjd/*- 2142727Spjd * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3129473Spjd * All rights reserved. 4129473Spjd * 5129473Spjd * Redistribution and use in source and binary forms, with or without 6129473Spjd * modification, are permitted provided that the following conditions 7129473Spjd * are met: 8129473Spjd * 1. Redistributions of source code must retain the above copyright 9129473Spjd * notice, this list of conditions and the following disclaimer. 10129473Spjd * 2. Redistributions in binary form must reproduce the above copyright 11129473Spjd * notice, this list of conditions and the following disclaimer in the 12129473Spjd * documentation and/or other materials provided with the distribution. 13155174Spjd * 14129473Spjd * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15129473Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16129473Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17129473Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18129473Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19129473Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20129473Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21129473Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22129473Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23129473Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24129473Spjd * SUCH DAMAGE. 25129473Spjd */ 26129473Spjd 27129473Spjd#include <sys/cdefs.h> 28129473Spjd__FBSDID("$FreeBSD: stable/10/sys/geom/stripe/g_stripe.c 306765 2016-10-06 15:36:13Z mav $"); 29129473Spjd 30129473Spjd#include <sys/param.h> 31129473Spjd#include <sys/systm.h> 32129473Spjd#include <sys/kernel.h> 33129473Spjd#include <sys/module.h> 34129473Spjd#include <sys/lock.h> 35129473Spjd#include <sys/mutex.h> 36129473Spjd#include <sys/bio.h> 37223921Sae#include <sys/sbuf.h> 38129473Spjd#include <sys/sysctl.h> 39129473Spjd#include <sys/malloc.h> 40131878Spjd#include <vm/uma.h> 41129473Spjd#include <geom/geom.h> 42129473Spjd#include <geom/stripe/g_stripe.h> 43129473Spjd 44219029SnetchildFEATURE(geom_stripe, "GEOM striping support"); 45129473Spjd 46151897Srwatsonstatic MALLOC_DEFINE(M_STRIPE, "stripe_data", "GEOM_STRIPE Data"); 47129473Spjd 48131878Spjdstatic uma_zone_t g_stripe_zone; 49129473Spjd 50129473Spjdstatic int g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force); 51129473Spjdstatic int g_stripe_destroy_geom(struct gctl_req *req, struct g_class *mp, 52129473Spjd struct g_geom *gp); 53129473Spjd 54129473Spjdstatic g_taste_t g_stripe_taste; 55129473Spjdstatic g_ctl_req_t g_stripe_config; 56129473Spjdstatic g_dumpconf_t g_stripe_dumpconf; 57131878Spjdstatic g_init_t g_stripe_init; 58131878Spjdstatic g_fini_t g_stripe_fini; 59129473Spjd 60129473Spjdstruct g_class g_stripe_class = { 61129473Spjd .name = G_STRIPE_CLASS_NAME, 62133318Sphk .version = G_VERSION, 63129473Spjd .ctlreq = g_stripe_config, 64129473Spjd .taste = g_stripe_taste, 65131878Spjd .destroy_geom = g_stripe_destroy_geom, 66131878Spjd .init = g_stripe_init, 67131878Spjd .fini = g_stripe_fini 68129473Spjd}; 69129473Spjd 70131878SpjdSYSCTL_DECL(_kern_geom); 71227309Sedstatic SYSCTL_NODE(_kern_geom, OID_AUTO, stripe, CTLFLAG_RW, 0, 72227309Sed "GEOM_STRIPE stuff"); 73131878Spjdstatic u_int g_stripe_debug = 0; 74134528SpjdTUNABLE_INT("kern.geom.stripe.debug", &g_stripe_debug); 75131878SpjdSYSCTL_UINT(_kern_geom_stripe, OID_AUTO, debug, CTLFLAG_RW, &g_stripe_debug, 0, 76131878Spjd "Debug level"); 77138623Spjdstatic int g_stripe_fast = 0; 78131878SpjdTUNABLE_INT("kern.geom.stripe.fast", &g_stripe_fast); 79131878Spjdstatic int 80131878Spjdg_sysctl_stripe_fast(SYSCTL_HANDLER_ARGS) 81131878Spjd{ 82131878Spjd int error, fast; 83129473Spjd 84131878Spjd fast = g_stripe_fast; 85170289Sdwmalone error = sysctl_handle_int(oidp, &fast, 0, req); 86131878Spjd if (error == 0 && req->newptr != NULL) 87131878Spjd g_stripe_fast = fast; 88131878Spjd return (error); 89131878Spjd} 90131878SpjdSYSCTL_PROC(_kern_geom_stripe, OID_AUTO, fast, CTLTYPE_INT | CTLFLAG_RW, 91132095Spjd NULL, 0, g_sysctl_stripe_fast, "I", "Fast, but memory-consuming, mode"); 92196837Smavstatic u_int g_stripe_maxmem = MAXPHYS * 100; 93131878SpjdTUNABLE_INT("kern.geom.stripe.maxmem", &g_stripe_maxmem); 94131878SpjdSYSCTL_UINT(_kern_geom_stripe, OID_AUTO, maxmem, CTLFLAG_RD, &g_stripe_maxmem, 95132095Spjd 0, "Maximum memory that can be allocated in \"fast\" mode (in bytes)"); 96133205Spjdstatic u_int g_stripe_fast_failed = 0; 97133205SpjdSYSCTL_UINT(_kern_geom_stripe, OID_AUTO, fast_failed, CTLFLAG_RD, 98133205Spjd &g_stripe_fast_failed, 0, "How many times \"fast\" mode failed"); 99131878Spjd 100129473Spjd/* 101129473Spjd * Greatest Common Divisor. 102129473Spjd */ 103129473Spjdstatic u_int 104129473Spjdgcd(u_int a, u_int b) 105129473Spjd{ 106129473Spjd u_int c; 107129473Spjd 108129473Spjd while (b != 0) { 109129473Spjd c = a; 110129473Spjd a = b; 111129473Spjd b = (c % b); 112129473Spjd } 113129473Spjd return (a); 114129473Spjd} 115129473Spjd 116129473Spjd/* 117129473Spjd * Least Common Multiple. 118129473Spjd */ 119129473Spjdstatic u_int 120129473Spjdlcm(u_int a, u_int b) 121129473Spjd{ 122129473Spjd 123129473Spjd return ((a * b) / gcd(a, b)); 124129473Spjd} 125129473Spjd 126131878Spjdstatic void 127131878Spjdg_stripe_init(struct g_class *mp __unused) 128131878Spjd{ 129131878Spjd 130196837Smav g_stripe_zone = uma_zcreate("g_stripe_zone", MAXPHYS, NULL, NULL, 131131878Spjd NULL, NULL, 0, 0); 132196837Smav g_stripe_maxmem -= g_stripe_maxmem % MAXPHYS; 133196837Smav uma_zone_set_max(g_stripe_zone, g_stripe_maxmem / MAXPHYS); 134131878Spjd} 135131878Spjd 136131878Spjdstatic void 137131878Spjdg_stripe_fini(struct g_class *mp __unused) 138131878Spjd{ 139131878Spjd 140131878Spjd uma_zdestroy(g_stripe_zone); 141131878Spjd} 142131878Spjd 143129473Spjd/* 144129473Spjd * Return the number of valid disks. 145129473Spjd */ 146129473Spjdstatic u_int 147129473Spjdg_stripe_nvalid(struct g_stripe_softc *sc) 148129473Spjd{ 149129473Spjd u_int i, no; 150129473Spjd 151129473Spjd no = 0; 152129473Spjd for (i = 0; i < sc->sc_ndisks; i++) { 153129473Spjd if (sc->sc_disks[i] != NULL) 154129473Spjd no++; 155129473Spjd } 156129473Spjd 157129473Spjd return (no); 158129473Spjd} 159129473Spjd 160129473Spjdstatic void 161129473Spjdg_stripe_remove_disk(struct g_consumer *cp) 162129473Spjd{ 163129473Spjd struct g_stripe_softc *sc; 164129473Spjd 165226998Smav g_topology_assert(); 166129473Spjd KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__)); 167226998Smav sc = (struct g_stripe_softc *)cp->geom->softc; 168129473Spjd KASSERT(sc != NULL, ("NULL sc in %s.", __func__)); 169129473Spjd 170226998Smav if (cp->private == NULL) { 171226998Smav G_STRIPE_DEBUG(0, "Disk %s removed from %s.", 172226998Smav cp->provider->name, sc->sc_name); 173226998Smav cp->private = (void *)(uintptr_t)-1; 174226998Smav } 175129473Spjd 176129473Spjd if (sc->sc_provider != NULL) { 177226998Smav G_STRIPE_DEBUG(0, "Device %s deactivated.", 178226998Smav sc->sc_provider->name); 179306765Smav g_wither_provider(sc->sc_provider, ENXIO); 180129473Spjd sc->sc_provider = NULL; 181129473Spjd } 182129473Spjd 183129473Spjd if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 184226998Smav return; 185226998Smav sc->sc_disks[cp->index] = NULL; 186226998Smav cp->index = 0; 187129473Spjd g_detach(cp); 188129473Spjd g_destroy_consumer(cp); 189226998Smav /* If there are no valid disks anymore, remove device. */ 190226998Smav if (LIST_EMPTY(&sc->sc_geom->consumer)) 191226998Smav g_stripe_destroy(sc, 1); 192129473Spjd} 193129473Spjd 194129473Spjdstatic void 195129473Spjdg_stripe_orphan(struct g_consumer *cp) 196129473Spjd{ 197129473Spjd struct g_stripe_softc *sc; 198129473Spjd struct g_geom *gp; 199129473Spjd 200129473Spjd g_topology_assert(); 201129473Spjd gp = cp->geom; 202129473Spjd sc = gp->softc; 203129473Spjd if (sc == NULL) 204129473Spjd return; 205129473Spjd 206129473Spjd g_stripe_remove_disk(cp); 207129473Spjd} 208129473Spjd 209129473Spjdstatic int 210129473Spjdg_stripe_access(struct g_provider *pp, int dr, int dw, int de) 211129473Spjd{ 212226998Smav struct g_consumer *cp1, *cp2, *tmp; 213129473Spjd struct g_stripe_softc *sc; 214129473Spjd struct g_geom *gp; 215129473Spjd int error; 216129473Spjd 217226998Smav g_topology_assert(); 218129473Spjd gp = pp->geom; 219129473Spjd sc = gp->softc; 220226998Smav KASSERT(sc != NULL, ("NULL sc in %s.", __func__)); 221129473Spjd 222129473Spjd /* On first open, grab an extra "exclusive" bit */ 223129473Spjd if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) 224129473Spjd de++; 225129473Spjd /* ... and let go of it on last close */ 226129473Spjd if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0) 227129473Spjd de--; 228129473Spjd 229226998Smav LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) { 230129473Spjd error = g_access(cp1, dr, dw, de); 231226998Smav if (error != 0) 232226998Smav goto fail; 233226998Smav if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 && 234226998Smav cp1->private != NULL) { 235226998Smav g_stripe_remove_disk(cp1); /* May destroy geom. */ 236129473Spjd } 237129473Spjd } 238226998Smav return (0); 239129473Spjd 240226998Smavfail: 241226998Smav LIST_FOREACH(cp2, &gp->consumer, consumer) { 242226998Smav if (cp1 == cp2) 243226998Smav break; 244226998Smav g_access(cp2, -dr, -dw, -de); 245226998Smav } 246129473Spjd return (error); 247129473Spjd} 248129473Spjd 249129473Spjdstatic void 250131878Spjdg_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset, 251131878Spjd off_t length, int mode) 252129473Spjd{ 253131878Spjd u_int stripesize; 254131878Spjd size_t len; 255131878Spjd 256131878Spjd stripesize = sc->sc_stripesize; 257131878Spjd len = (size_t)(stripesize - (offset & (stripesize - 1))); 258131878Spjd do { 259131878Spjd bcopy(src, dst, len); 260131878Spjd if (mode) { 261131878Spjd dst += len + stripesize * (sc->sc_ndisks - 1); 262131878Spjd src += len; 263131878Spjd } else { 264131878Spjd dst += len; 265131878Spjd src += len + stripesize * (sc->sc_ndisks - 1); 266131878Spjd } 267131878Spjd length -= len; 268131878Spjd KASSERT(length >= 0, 269131878Spjd ("Length < 0 (stripesize=%zu, offset=%jd, length=%jd).", 270131878Spjd (size_t)stripesize, (intmax_t)offset, (intmax_t)length)); 271131878Spjd if (length > stripesize) 272131878Spjd len = stripesize; 273131878Spjd else 274131878Spjd len = length; 275131878Spjd } while (length > 0); 276131878Spjd} 277131878Spjd 278131878Spjdstatic void 279131878Spjdg_stripe_done(struct bio *bp) 280131878Spjd{ 281129473Spjd struct g_stripe_softc *sc; 282131878Spjd struct bio *pbp; 283131878Spjd 284131878Spjd pbp = bp->bio_parent; 285131878Spjd sc = pbp->bio_to->geom->softc; 286133204Spjd if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) { 287133204Spjd g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset, 288131878Spjd bp->bio_length, 1); 289133204Spjd bp->bio_data = bp->bio_caller1; 290133204Spjd bp->bio_caller1 = NULL; 291131878Spjd } 292260385Sscottl mtx_lock(&sc->sc_lock); 293260385Sscottl if (pbp->bio_error == 0) 294260385Sscottl pbp->bio_error = bp->bio_error; 295260385Sscottl pbp->bio_completed += bp->bio_completed; 296131878Spjd pbp->bio_inbed++; 297131878Spjd if (pbp->bio_children == pbp->bio_inbed) { 298260385Sscottl mtx_unlock(&sc->sc_lock); 299133204Spjd if (pbp->bio_driver1 != NULL) 300133204Spjd uma_zfree(g_stripe_zone, pbp->bio_driver1); 301131878Spjd g_io_deliver(pbp, pbp->bio_error); 302260385Sscottl } else 303260385Sscottl mtx_unlock(&sc->sc_lock); 304260385Sscottl g_destroy_bio(bp); 305131878Spjd} 306131878Spjd 307131878Spjdstatic int 308131878Spjdg_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length) 309131878Spjd{ 310131878Spjd TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 311131878Spjd u_int nparts = 0, stripesize; 312131878Spjd struct g_stripe_softc *sc; 313131878Spjd char *addr, *data = NULL; 314129473Spjd struct bio *cbp; 315131878Spjd int error; 316131878Spjd 317131878Spjd sc = bp->bio_to->geom->softc; 318131878Spjd 319131878Spjd addr = bp->bio_data; 320131878Spjd stripesize = sc->sc_stripesize; 321131878Spjd 322131878Spjd cbp = g_clone_bio(bp); 323131878Spjd if (cbp == NULL) { 324131878Spjd error = ENOMEM; 325131878Spjd goto failure; 326131878Spjd } 327131878Spjd TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 328131878Spjd nparts++; 329131878Spjd /* 330131878Spjd * Fill in the component buf structure. 331131878Spjd */ 332131878Spjd cbp->bio_done = g_stripe_done; 333131878Spjd cbp->bio_offset = offset; 334131878Spjd cbp->bio_data = addr; 335133204Spjd cbp->bio_caller1 = NULL; 336131878Spjd cbp->bio_length = length; 337133204Spjd cbp->bio_caller2 = sc->sc_disks[no]; 338131878Spjd 339131878Spjd /* offset -= offset % stripesize; */ 340131878Spjd offset -= offset & (stripesize - 1); 341131878Spjd addr += length; 342131878Spjd length = bp->bio_length - length; 343131878Spjd for (no++; length > 0; no++, length -= stripesize, addr += stripesize) { 344131878Spjd if (no > sc->sc_ndisks - 1) { 345131878Spjd no = 0; 346131878Spjd offset += stripesize; 347131878Spjd } 348131878Spjd if (nparts >= sc->sc_ndisks) { 349131878Spjd cbp = TAILQ_NEXT(cbp, bio_queue); 350131878Spjd if (cbp == NULL) 351131878Spjd cbp = TAILQ_FIRST(&queue); 352131878Spjd nparts++; 353131878Spjd /* 354131878Spjd * Update bio structure. 355131878Spjd */ 356131878Spjd /* 357131878Spjd * MIN() is in case when 358131878Spjd * (bp->bio_length % sc->sc_stripesize) != 0. 359131878Spjd */ 360131878Spjd cbp->bio_length += MIN(stripesize, length); 361133204Spjd if (cbp->bio_caller1 == NULL) { 362133204Spjd cbp->bio_caller1 = cbp->bio_data; 363131878Spjd cbp->bio_data = NULL; 364131878Spjd if (data == NULL) { 365131878Spjd data = uma_zalloc(g_stripe_zone, 366131878Spjd M_NOWAIT); 367131878Spjd if (data == NULL) { 368131878Spjd error = ENOMEM; 369131878Spjd goto failure; 370131878Spjd } 371131878Spjd } 372131878Spjd } 373131878Spjd } else { 374131878Spjd cbp = g_clone_bio(bp); 375131878Spjd if (cbp == NULL) { 376131878Spjd error = ENOMEM; 377131878Spjd goto failure; 378131878Spjd } 379131878Spjd TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 380131878Spjd nparts++; 381131878Spjd /* 382131878Spjd * Fill in the component buf structure. 383131878Spjd */ 384131878Spjd cbp->bio_done = g_stripe_done; 385131878Spjd cbp->bio_offset = offset; 386131878Spjd cbp->bio_data = addr; 387133204Spjd cbp->bio_caller1 = NULL; 388131878Spjd /* 389131878Spjd * MIN() is in case when 390131878Spjd * (bp->bio_length % sc->sc_stripesize) != 0. 391131878Spjd */ 392131878Spjd cbp->bio_length = MIN(stripesize, length); 393133204Spjd cbp->bio_caller2 = sc->sc_disks[no]; 394131878Spjd } 395131878Spjd } 396131878Spjd if (data != NULL) 397133444Spjd bp->bio_driver1 = data; 398131878Spjd /* 399131878Spjd * Fire off all allocated requests! 400131878Spjd */ 401131878Spjd while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 402131878Spjd struct g_consumer *cp; 403131878Spjd 404131878Spjd TAILQ_REMOVE(&queue, cbp, bio_queue); 405133204Spjd cp = cbp->bio_caller2; 406133204Spjd cbp->bio_caller2 = NULL; 407131878Spjd cbp->bio_to = cp->provider; 408133204Spjd if (cbp->bio_caller1 != NULL) { 409131878Spjd cbp->bio_data = data; 410131878Spjd if (bp->bio_cmd == BIO_WRITE) { 411133204Spjd g_stripe_copy(sc, cbp->bio_caller1, data, 412131878Spjd cbp->bio_offset, cbp->bio_length, 0); 413131878Spjd } 414131878Spjd data += cbp->bio_length; 415131878Spjd } 416131878Spjd G_STRIPE_LOGREQ(cbp, "Sending request."); 417131878Spjd g_io_request(cbp, cp); 418131878Spjd } 419131878Spjd return (0); 420131878Spjdfailure: 421131878Spjd if (data != NULL) 422131878Spjd uma_zfree(g_stripe_zone, data); 423131878Spjd while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 424131878Spjd TAILQ_REMOVE(&queue, cbp, bio_queue); 425133204Spjd if (cbp->bio_caller1 != NULL) { 426133204Spjd cbp->bio_data = cbp->bio_caller1; 427133204Spjd cbp->bio_caller1 = NULL; 428131878Spjd } 429133201Spjd bp->bio_children--; 430131878Spjd g_destroy_bio(cbp); 431131878Spjd } 432131878Spjd return (error); 433131878Spjd} 434131878Spjd 435131878Spjdstatic int 436131878Spjdg_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length) 437131878Spjd{ 438131878Spjd TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 439131878Spjd struct g_stripe_softc *sc; 440129473Spjd uint32_t stripesize; 441131878Spjd struct bio *cbp; 442129473Spjd char *addr; 443131878Spjd int error; 444129473Spjd 445131878Spjd sc = bp->bio_to->geom->softc; 446131878Spjd 447131878Spjd stripesize = sc->sc_stripesize; 448131878Spjd 449131878Spjd cbp = g_clone_bio(bp); 450131878Spjd if (cbp == NULL) { 451131878Spjd error = ENOMEM; 452131878Spjd goto failure; 453131878Spjd } 454131878Spjd TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 455129473Spjd /* 456131878Spjd * Fill in the component buf structure. 457131878Spjd */ 458260385Sscottl if (bp->bio_length == length) 459260385Sscottl cbp->bio_done = g_std_done; /* Optimized lockless case. */ 460260385Sscottl else 461260385Sscottl cbp->bio_done = g_stripe_done; 462131878Spjd cbp->bio_offset = offset; 463131878Spjd cbp->bio_length = length; 464260385Sscottl if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 465260385Sscottl bp->bio_ma_n = round_page(bp->bio_ma_offset + 466260385Sscottl bp->bio_length) / PAGE_SIZE; 467260385Sscottl addr = NULL; 468260385Sscottl } else 469260385Sscottl addr = bp->bio_data; 470133204Spjd cbp->bio_caller2 = sc->sc_disks[no]; 471131878Spjd 472131878Spjd /* offset -= offset % stripesize; */ 473131878Spjd offset -= offset & (stripesize - 1); 474265668Smav if (bp->bio_cmd != BIO_DELETE) 475265668Smav addr += length; 476131878Spjd length = bp->bio_length - length; 477265668Smav for (no++; length > 0; no++, length -= stripesize) { 478131878Spjd if (no > sc->sc_ndisks - 1) { 479131878Spjd no = 0; 480131878Spjd offset += stripesize; 481131878Spjd } 482131878Spjd cbp = g_clone_bio(bp); 483131878Spjd if (cbp == NULL) { 484131878Spjd error = ENOMEM; 485131878Spjd goto failure; 486131878Spjd } 487131878Spjd TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 488131878Spjd 489131878Spjd /* 490131878Spjd * Fill in the component buf structure. 491131878Spjd */ 492260385Sscottl cbp->bio_done = g_stripe_done; 493131878Spjd cbp->bio_offset = offset; 494131878Spjd /* 495131878Spjd * MIN() is in case when 496131878Spjd * (bp->bio_length % sc->sc_stripesize) != 0. 497131878Spjd */ 498131878Spjd cbp->bio_length = MIN(stripesize, length); 499260385Sscottl if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 500260385Sscottl cbp->bio_ma_offset += (uintptr_t)addr; 501260385Sscottl cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE; 502260385Sscottl cbp->bio_ma_offset %= PAGE_SIZE; 503260385Sscottl cbp->bio_ma_n = round_page(cbp->bio_ma_offset + 504260385Sscottl cbp->bio_length) / PAGE_SIZE; 505260385Sscottl } else 506260385Sscottl cbp->bio_data = addr; 507131878Spjd 508133204Spjd cbp->bio_caller2 = sc->sc_disks[no]; 509265668Smav 510265668Smav if (bp->bio_cmd != BIO_DELETE) 511265668Smav addr += stripesize; 512131878Spjd } 513131878Spjd /* 514131878Spjd * Fire off all allocated requests! 515131878Spjd */ 516131878Spjd while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 517131878Spjd struct g_consumer *cp; 518131878Spjd 519131878Spjd TAILQ_REMOVE(&queue, cbp, bio_queue); 520133204Spjd cp = cbp->bio_caller2; 521133204Spjd cbp->bio_caller2 = NULL; 522131878Spjd cbp->bio_to = cp->provider; 523131878Spjd G_STRIPE_LOGREQ(cbp, "Sending request."); 524131878Spjd g_io_request(cbp, cp); 525131878Spjd } 526131878Spjd return (0); 527131878Spjdfailure: 528131878Spjd while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 529131878Spjd TAILQ_REMOVE(&queue, cbp, bio_queue); 530133201Spjd bp->bio_children--; 531131878Spjd g_destroy_bio(cbp); 532131878Spjd } 533131878Spjd return (error); 534131878Spjd} 535131878Spjd 536131878Spjdstatic void 537163836Spjdg_stripe_flush(struct g_stripe_softc *sc, struct bio *bp) 538163836Spjd{ 539163836Spjd struct bio_queue_head queue; 540163836Spjd struct g_consumer *cp; 541163836Spjd struct bio *cbp; 542163836Spjd u_int no; 543163836Spjd 544163836Spjd bioq_init(&queue); 545163836Spjd for (no = 0; no < sc->sc_ndisks; no++) { 546163836Spjd cbp = g_clone_bio(bp); 547163836Spjd if (cbp == NULL) { 548163836Spjd for (cbp = bioq_first(&queue); cbp != NULL; 549163836Spjd cbp = bioq_first(&queue)) { 550163836Spjd bioq_remove(&queue, cbp); 551163836Spjd g_destroy_bio(cbp); 552163836Spjd } 553163836Spjd if (bp->bio_error == 0) 554163836Spjd bp->bio_error = ENOMEM; 555163836Spjd g_io_deliver(bp, bp->bio_error); 556163836Spjd return; 557163836Spjd } 558163836Spjd bioq_insert_tail(&queue, cbp); 559260385Sscottl cbp->bio_done = g_stripe_done; 560260385Sscottl cbp->bio_caller2 = sc->sc_disks[no]; 561163836Spjd cbp->bio_to = sc->sc_disks[no]->provider; 562163836Spjd } 563163836Spjd for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) { 564163836Spjd bioq_remove(&queue, cbp); 565163836Spjd G_STRIPE_LOGREQ(cbp, "Sending request."); 566260385Sscottl cp = cbp->bio_caller2; 567260385Sscottl cbp->bio_caller2 = NULL; 568163836Spjd g_io_request(cbp, cp); 569163836Spjd } 570163836Spjd} 571163836Spjd 572163836Spjdstatic void 573131878Spjdg_stripe_start(struct bio *bp) 574131878Spjd{ 575131878Spjd off_t offset, start, length, nstripe; 576131878Spjd struct g_stripe_softc *sc; 577131878Spjd u_int no, stripesize; 578131878Spjd int error, fast = 0; 579131878Spjd 580131878Spjd sc = bp->bio_to->geom->softc; 581131878Spjd /* 582129473Spjd * If sc == NULL, provider's error should be set and g_stripe_start() 583129473Spjd * should not be called at all. 584129473Spjd */ 585129473Spjd KASSERT(sc != NULL, 586129473Spjd ("Provider's error should be set (error=%d)(device=%s).", 587129473Spjd bp->bio_to->error, bp->bio_to->name)); 588129473Spjd 589129473Spjd G_STRIPE_LOGREQ(bp, "Request received."); 590129473Spjd 591129473Spjd switch (bp->bio_cmd) { 592129473Spjd case BIO_READ: 593129473Spjd case BIO_WRITE: 594129473Spjd case BIO_DELETE: 595129473Spjd break; 596163886Spjd case BIO_FLUSH: 597163886Spjd g_stripe_flush(sc, bp); 598163886Spjd return; 599129473Spjd case BIO_GETATTR: 600129473Spjd /* To which provider it should be delivered? */ 601129473Spjd default: 602129473Spjd g_io_deliver(bp, EOPNOTSUPP); 603129473Spjd return; 604129473Spjd } 605129473Spjd 606129473Spjd stripesize = sc->sc_stripesize; 607129473Spjd 608129473Spjd /* 609131878Spjd * Calculations are quite messy, but fast I hope. 610129473Spjd */ 611129473Spjd 612129473Spjd /* Stripe number. */ 613129473Spjd /* nstripe = bp->bio_offset / stripesize; */ 614129473Spjd nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits; 615129473Spjd /* Disk number. */ 616129473Spjd no = nstripe % sc->sc_ndisks; 617129473Spjd /* Start position in stripe. */ 618129473Spjd /* start = bp->bio_offset % stripesize; */ 619129473Spjd start = bp->bio_offset & (stripesize - 1); 620129473Spjd /* Start position in disk. */ 621131878Spjd /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */ 622131878Spjd offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start; 623129473Spjd /* Length of data to operate. */ 624129473Spjd length = MIN(bp->bio_length, stripesize - start); 625129473Spjd 626131878Spjd /* 627131878Spjd * Do use "fast" mode when: 628131878Spjd * 1. "Fast" mode is ON. 629131878Spjd * and 630196837Smav * 2. Request size is less than or equal to MAXPHYS, 631131878Spjd * which should always be true. 632131878Spjd * and 633131878Spjd * 3. Request size is bigger than stripesize * ndisks. If it isn't, 634131878Spjd * there will be no need to send more than one I/O request to 635131878Spjd * a provider, so there is nothing to optmize. 636260385Sscottl * and 637260385Sscottl * 4. Request is not unmapped. 638265668Smav * and 639265668Smav * 5. It is not a BIO_DELETE. 640131878Spjd */ 641196837Smav if (g_stripe_fast && bp->bio_length <= MAXPHYS && 642260385Sscottl bp->bio_length >= stripesize * sc->sc_ndisks && 643265668Smav (bp->bio_flags & BIO_UNMAPPED) == 0 && 644265668Smav bp->bio_cmd != BIO_DELETE) { 645131878Spjd fast = 1; 646129473Spjd } 647131878Spjd error = 0; 648133205Spjd if (fast) { 649131878Spjd error = g_stripe_start_fast(bp, no, offset, length); 650133205Spjd if (error != 0) 651133205Spjd g_stripe_fast_failed++; 652133205Spjd } 653129473Spjd /* 654131878Spjd * Do use "economic" when: 655131878Spjd * 1. "Economic" mode is ON. 656131878Spjd * or 657204070Spjd * 2. "Fast" mode failed. It can only fail if there is no memory. 658129473Spjd */ 659131878Spjd if (!fast || error != 0) 660131878Spjd error = g_stripe_start_economic(bp, no, offset, length); 661131878Spjd if (error != 0) { 662131878Spjd if (bp->bio_error == 0) 663131878Spjd bp->bio_error = error; 664131878Spjd g_io_deliver(bp, bp->bio_error); 665129473Spjd } 666129473Spjd} 667129473Spjd 668129473Spjdstatic void 669129473Spjdg_stripe_check_and_run(struct g_stripe_softc *sc) 670129473Spjd{ 671260385Sscottl struct g_provider *dp; 672129473Spjd off_t mediasize, ms; 673129473Spjd u_int no, sectorsize = 0; 674129473Spjd 675226998Smav g_topology_assert(); 676129473Spjd if (g_stripe_nvalid(sc) != sc->sc_ndisks) 677129473Spjd return; 678129473Spjd 679132664Spjd sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s", 680132664Spjd sc->sc_name); 681260385Sscottl sc->sc_provider->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 682260385Sscottl if (g_stripe_fast == 0) 683260385Sscottl sc->sc_provider->flags |= G_PF_ACCEPT_UNMAPPED; 684129473Spjd /* 685129473Spjd * Find the smallest disk. 686129473Spjd */ 687129473Spjd mediasize = sc->sc_disks[0]->provider->mediasize; 688129473Spjd if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) 689129473Spjd mediasize -= sc->sc_disks[0]->provider->sectorsize; 690129473Spjd mediasize -= mediasize % sc->sc_stripesize; 691129473Spjd sectorsize = sc->sc_disks[0]->provider->sectorsize; 692129473Spjd for (no = 1; no < sc->sc_ndisks; no++) { 693260385Sscottl dp = sc->sc_disks[no]->provider; 694260385Sscottl ms = dp->mediasize; 695129473Spjd if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) 696260385Sscottl ms -= dp->sectorsize; 697129473Spjd ms -= ms % sc->sc_stripesize; 698129473Spjd if (ms < mediasize) 699129473Spjd mediasize = ms; 700260385Sscottl sectorsize = lcm(sectorsize, dp->sectorsize); 701260385Sscottl 702260385Sscottl /* A provider underneath us doesn't support unmapped */ 703260385Sscottl if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) { 704260385Sscottl G_STRIPE_DEBUG(1, "Cancelling unmapped " 705260385Sscottl "because of %s.", dp->name); 706260385Sscottl sc->sc_provider->flags &= ~G_PF_ACCEPT_UNMAPPED; 707260385Sscottl } 708129473Spjd } 709129473Spjd sc->sc_provider->sectorsize = sectorsize; 710129473Spjd sc->sc_provider->mediasize = mediasize * sc->sc_ndisks; 711200933Smav sc->sc_provider->stripesize = sc->sc_stripesize; 712200933Smav sc->sc_provider->stripeoffset = 0; 713129473Spjd g_error_provider(sc->sc_provider, 0); 714129473Spjd 715226998Smav G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name); 716129473Spjd} 717129473Spjd 718129473Spjdstatic int 719129473Spjdg_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md) 720129473Spjd{ 721129473Spjd struct g_provider *pp; 722129473Spjd u_char *buf; 723129473Spjd int error; 724129473Spjd 725129473Spjd g_topology_assert(); 726129473Spjd 727129473Spjd error = g_access(cp, 1, 0, 0); 728129473Spjd if (error != 0) 729129473Spjd return (error); 730129473Spjd pp = cp->provider; 731129473Spjd g_topology_unlock(); 732129473Spjd buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 733129473Spjd &error); 734129473Spjd g_topology_lock(); 735129473Spjd g_access(cp, -1, 0, 0); 736129473Spjd if (buf == NULL) 737129473Spjd return (error); 738129473Spjd 739129473Spjd /* Decode metadata. */ 740129473Spjd stripe_metadata_decode(buf, md); 741129473Spjd g_free(buf); 742129473Spjd 743129473Spjd return (0); 744129473Spjd} 745129473Spjd 746129473Spjd/* 747129473Spjd * Add disk to given device. 748129473Spjd */ 749129473Spjdstatic int 750129473Spjdg_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no) 751129473Spjd{ 752129473Spjd struct g_consumer *cp, *fcp; 753129473Spjd struct g_geom *gp; 754129473Spjd int error; 755129473Spjd 756226998Smav g_topology_assert(); 757129473Spjd /* Metadata corrupted? */ 758129473Spjd if (no >= sc->sc_ndisks) 759129473Spjd return (EINVAL); 760129473Spjd 761129473Spjd /* Check if disk is not already attached. */ 762129473Spjd if (sc->sc_disks[no] != NULL) 763129473Spjd return (EEXIST); 764129473Spjd 765129473Spjd gp = sc->sc_geom; 766129473Spjd fcp = LIST_FIRST(&gp->consumer); 767129473Spjd 768129473Spjd cp = g_new_consumer(gp); 769260385Sscottl cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 770226998Smav cp->private = NULL; 771226998Smav cp->index = no; 772129473Spjd error = g_attach(cp, pp); 773129473Spjd if (error != 0) { 774129473Spjd g_destroy_consumer(cp); 775129473Spjd return (error); 776129473Spjd } 777129473Spjd 778129473Spjd if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) { 779129473Spjd error = g_access(cp, fcp->acr, fcp->acw, fcp->ace); 780129473Spjd if (error != 0) { 781129473Spjd g_detach(cp); 782129473Spjd g_destroy_consumer(cp); 783129473Spjd return (error); 784129473Spjd } 785129473Spjd } 786129473Spjd if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) { 787129473Spjd struct g_stripe_metadata md; 788129473Spjd 789129473Spjd /* Reread metadata. */ 790129473Spjd error = g_stripe_read_metadata(cp, &md); 791129473Spjd if (error != 0) 792129473Spjd goto fail; 793129473Spjd 794129473Spjd if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 || 795129473Spjd strcmp(md.md_name, sc->sc_name) != 0 || 796129473Spjd md.md_id != sc->sc_id) { 797129473Spjd G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name); 798129473Spjd goto fail; 799129473Spjd } 800129473Spjd } 801129473Spjd 802129473Spjd sc->sc_disks[no] = cp; 803132664Spjd G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name); 804129473Spjd g_stripe_check_and_run(sc); 805129473Spjd 806129473Spjd return (0); 807129473Spjdfail: 808129473Spjd if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) 809129473Spjd g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace); 810129473Spjd g_detach(cp); 811129473Spjd g_destroy_consumer(cp); 812129473Spjd return (error); 813129473Spjd} 814129473Spjd 815129473Spjdstatic struct g_geom * 816129473Spjdg_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md, 817129473Spjd u_int type) 818129473Spjd{ 819129473Spjd struct g_stripe_softc *sc; 820129473Spjd struct g_geom *gp; 821129473Spjd u_int no; 822129473Spjd 823226998Smav g_topology_assert(); 824132664Spjd G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name, 825129473Spjd md->md_id); 826129473Spjd 827129473Spjd /* Two disks is minimum. */ 828132664Spjd if (md->md_all < 2) { 829132664Spjd G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name); 830129473Spjd return (NULL); 831129473Spjd } 832129473Spjd#if 0 833129473Spjd /* Stripe size have to be grater than or equal to sector size. */ 834129473Spjd if (md->md_stripesize < sectorsize) { 835132664Spjd G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name); 836129473Spjd return (NULL); 837129473Spjd } 838129473Spjd#endif 839129473Spjd /* Stripe size have to be power of 2. */ 840129473Spjd if (!powerof2(md->md_stripesize)) { 841132664Spjd G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name); 842129473Spjd return (NULL); 843129473Spjd } 844129473Spjd 845129473Spjd /* Check for duplicate unit */ 846129473Spjd LIST_FOREACH(gp, &mp->geom, geom) { 847129473Spjd sc = gp->softc; 848129473Spjd if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) { 849129473Spjd G_STRIPE_DEBUG(0, "Device %s already configured.", 850132664Spjd sc->sc_name); 851129473Spjd return (NULL); 852129473Spjd } 853129473Spjd } 854132664Spjd gp = g_new_geomf(mp, "%s", md->md_name); 855132662Spjd sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO); 856129473Spjd gp->start = g_stripe_start; 857129473Spjd gp->spoiled = g_stripe_orphan; 858129473Spjd gp->orphan = g_stripe_orphan; 859129473Spjd gp->access = g_stripe_access; 860129473Spjd gp->dumpconf = g_stripe_dumpconf; 861129473Spjd 862129473Spjd sc->sc_id = md->md_id; 863129473Spjd sc->sc_stripesize = md->md_stripesize; 864149300Spjd sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1); 865129473Spjd sc->sc_ndisks = md->md_all; 866129473Spjd sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks, 867129473Spjd M_STRIPE, M_WAITOK | M_ZERO); 868129473Spjd for (no = 0; no < sc->sc_ndisks; no++) 869129473Spjd sc->sc_disks[no] = NULL; 870129473Spjd sc->sc_type = type; 871260385Sscottl mtx_init(&sc->sc_lock, "gstripe lock", NULL, MTX_DEF); 872129473Spjd 873129473Spjd gp->softc = sc; 874129473Spjd sc->sc_geom = gp; 875129473Spjd sc->sc_provider = NULL; 876129473Spjd 877132664Spjd G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id); 878129473Spjd 879129473Spjd return (gp); 880129473Spjd} 881129473Spjd 882129473Spjdstatic int 883129473Spjdg_stripe_destroy(struct g_stripe_softc *sc, boolean_t force) 884129473Spjd{ 885129473Spjd struct g_provider *pp; 886226998Smav struct g_consumer *cp, *cp1; 887129473Spjd struct g_geom *gp; 888129473Spjd 889129473Spjd g_topology_assert(); 890129473Spjd 891129473Spjd if (sc == NULL) 892129473Spjd return (ENXIO); 893129473Spjd 894129473Spjd pp = sc->sc_provider; 895129473Spjd if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 896129473Spjd if (force) { 897129473Spjd G_STRIPE_DEBUG(0, "Device %s is still open, so it " 898129473Spjd "can't be definitely removed.", pp->name); 899129473Spjd } else { 900129473Spjd G_STRIPE_DEBUG(1, 901129473Spjd "Device %s is still open (r%dw%de%d).", pp->name, 902129473Spjd pp->acr, pp->acw, pp->ace); 903129473Spjd return (EBUSY); 904129473Spjd } 905129473Spjd } 906129473Spjd 907226998Smav gp = sc->sc_geom; 908226998Smav LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) { 909226998Smav g_stripe_remove_disk(cp); 910226998Smav if (cp1 == NULL) 911226998Smav return (0); /* Recursion happened. */ 912129473Spjd } 913226998Smav if (!LIST_EMPTY(&gp->consumer)) 914226998Smav return (EINPROGRESS); 915129473Spjd 916129473Spjd gp->softc = NULL; 917129473Spjd KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)", 918129473Spjd gp->name)); 919129473Spjd free(sc->sc_disks, M_STRIPE); 920260385Sscottl mtx_destroy(&sc->sc_lock); 921129473Spjd free(sc, M_STRIPE); 922226998Smav G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name); 923129473Spjd g_wither_geom(gp, ENXIO); 924129473Spjd return (0); 925129473Spjd} 926129473Spjd 927129473Spjdstatic int 928129473Spjdg_stripe_destroy_geom(struct gctl_req *req __unused, 929129473Spjd struct g_class *mp __unused, struct g_geom *gp) 930129473Spjd{ 931129473Spjd struct g_stripe_softc *sc; 932129473Spjd 933129473Spjd sc = gp->softc; 934129473Spjd return (g_stripe_destroy(sc, 0)); 935129473Spjd} 936129473Spjd 937129473Spjdstatic struct g_geom * 938129473Spjdg_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 939129473Spjd{ 940129473Spjd struct g_stripe_metadata md; 941129473Spjd struct g_stripe_softc *sc; 942129473Spjd struct g_consumer *cp; 943129473Spjd struct g_geom *gp; 944129473Spjd int error; 945129473Spjd 946129473Spjd g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 947129473Spjd g_topology_assert(); 948129473Spjd 949197898Spjd /* Skip providers that are already open for writing. */ 950197898Spjd if (pp->acw > 0) 951197898Spjd return (NULL); 952197898Spjd 953129473Spjd G_STRIPE_DEBUG(3, "Tasting %s.", pp->name); 954129473Spjd 955129473Spjd gp = g_new_geomf(mp, "stripe:taste"); 956129473Spjd gp->start = g_stripe_start; 957129473Spjd gp->access = g_stripe_access; 958129473Spjd gp->orphan = g_stripe_orphan; 959129473Spjd cp = g_new_consumer(gp); 960129473Spjd g_attach(cp, pp); 961129473Spjd error = g_stripe_read_metadata(cp, &md); 962133371Spjd g_detach(cp); 963133371Spjd g_destroy_consumer(cp); 964133371Spjd g_destroy_geom(gp); 965129473Spjd if (error != 0) 966129473Spjd return (NULL); 967129473Spjd gp = NULL; 968129473Spjd 969129473Spjd if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0) 970129473Spjd return (NULL); 971129473Spjd if (md.md_version > G_STRIPE_VERSION) { 972129473Spjd printf("geom_stripe.ko module is too old to handle %s.\n", 973129473Spjd pp->name); 974129473Spjd return (NULL); 975129473Spjd } 976133373Spjd /* 977133373Spjd * Backward compatibility: 978133373Spjd */ 979142727Spjd /* There was no md_provider field in earlier versions of metadata. */ 980133373Spjd if (md.md_version < 2) 981133373Spjd bzero(md.md_provider, sizeof(md.md_provider)); 982142727Spjd /* There was no md_provsize field in earlier versions of metadata. */ 983142727Spjd if (md.md_version < 3) 984142727Spjd md.md_provsize = pp->mediasize; 985129473Spjd 986221101Smav if (md.md_provider[0] != '\0' && 987221101Smav !g_compare_names(md.md_provider, pp->name)) 988133373Spjd return (NULL); 989142727Spjd if (md.md_provsize != pp->mediasize) 990142727Spjd return (NULL); 991133373Spjd 992129473Spjd /* 993129473Spjd * Let's check if device already exists. 994129473Spjd */ 995129473Spjd sc = NULL; 996129473Spjd LIST_FOREACH(gp, &mp->geom, geom) { 997129473Spjd sc = gp->softc; 998129473Spjd if (sc == NULL) 999129473Spjd continue; 1000129473Spjd if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC) 1001129473Spjd continue; 1002129473Spjd if (strcmp(md.md_name, sc->sc_name) != 0) 1003129473Spjd continue; 1004129473Spjd if (md.md_id != sc->sc_id) 1005129473Spjd continue; 1006129473Spjd break; 1007129473Spjd } 1008129473Spjd if (gp != NULL) { 1009129473Spjd G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 1010129473Spjd error = g_stripe_add_disk(sc, pp, md.md_no); 1011129473Spjd if (error != 0) { 1012129473Spjd G_STRIPE_DEBUG(0, 1013129473Spjd "Cannot add disk %s to %s (error=%d).", pp->name, 1014129473Spjd gp->name, error); 1015129473Spjd return (NULL); 1016129473Spjd } 1017129473Spjd } else { 1018129473Spjd gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC); 1019129473Spjd if (gp == NULL) { 1020132664Spjd G_STRIPE_DEBUG(0, "Cannot create device %s.", 1021129473Spjd md.md_name); 1022129473Spjd return (NULL); 1023129473Spjd } 1024129473Spjd sc = gp->softc; 1025129473Spjd G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 1026129473Spjd error = g_stripe_add_disk(sc, pp, md.md_no); 1027129473Spjd if (error != 0) { 1028129473Spjd G_STRIPE_DEBUG(0, 1029129473Spjd "Cannot add disk %s to %s (error=%d).", pp->name, 1030129473Spjd gp->name, error); 1031129473Spjd g_stripe_destroy(sc, 1); 1032129473Spjd return (NULL); 1033129473Spjd } 1034129473Spjd } 1035129473Spjd 1036129473Spjd return (gp); 1037129473Spjd} 1038129473Spjd 1039129473Spjdstatic void 1040129473Spjdg_stripe_ctl_create(struct gctl_req *req, struct g_class *mp) 1041129473Spjd{ 1042129473Spjd u_int attached, no; 1043129473Spjd struct g_stripe_metadata md; 1044129473Spjd struct g_provider *pp; 1045129473Spjd struct g_stripe_softc *sc; 1046129473Spjd struct g_geom *gp; 1047129473Spjd struct sbuf *sb; 1048129473Spjd intmax_t *stripesize; 1049129473Spjd const char *name; 1050129473Spjd char param[16]; 1051129473Spjd int *nargs; 1052129473Spjd 1053129473Spjd g_topology_assert(); 1054129473Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1055129473Spjd if (nargs == NULL) { 1056129473Spjd gctl_error(req, "No '%s' argument.", "nargs"); 1057129473Spjd return; 1058129473Spjd } 1059129473Spjd if (*nargs <= 2) { 1060129473Spjd gctl_error(req, "Too few arguments."); 1061129473Spjd return; 1062129473Spjd } 1063129473Spjd 1064129473Spjd strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic)); 1065129473Spjd md.md_version = G_STRIPE_VERSION; 1066129473Spjd name = gctl_get_asciiparam(req, "arg0"); 1067129473Spjd if (name == NULL) { 1068129473Spjd gctl_error(req, "No 'arg%u' argument.", 0); 1069129473Spjd return; 1070129473Spjd } 1071129473Spjd strlcpy(md.md_name, name, sizeof(md.md_name)); 1072129473Spjd md.md_id = arc4random(); 1073129473Spjd md.md_no = 0; 1074129473Spjd md.md_all = *nargs - 1; 1075129473Spjd stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize)); 1076129473Spjd if (stripesize == NULL) { 1077129473Spjd gctl_error(req, "No '%s' argument.", "stripesize"); 1078129473Spjd return; 1079129473Spjd } 1080129473Spjd md.md_stripesize = *stripesize; 1081133373Spjd bzero(md.md_provider, sizeof(md.md_provider)); 1082142727Spjd /* This field is not important here. */ 1083142727Spjd md.md_provsize = 0; 1084129473Spjd 1085129473Spjd /* Check all providers are valid */ 1086129473Spjd for (no = 1; no < *nargs; no++) { 1087129473Spjd snprintf(param, sizeof(param), "arg%u", no); 1088129473Spjd name = gctl_get_asciiparam(req, param); 1089129473Spjd if (name == NULL) { 1090129473Spjd gctl_error(req, "No 'arg%u' argument.", no); 1091129473Spjd return; 1092129473Spjd } 1093129473Spjd if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 1094129473Spjd name += strlen("/dev/"); 1095129473Spjd pp = g_provider_by_name(name); 1096129473Spjd if (pp == NULL) { 1097129473Spjd G_STRIPE_DEBUG(1, "Disk %s is invalid.", name); 1098129473Spjd gctl_error(req, "Disk %s is invalid.", name); 1099129473Spjd return; 1100129473Spjd } 1101129473Spjd } 1102129473Spjd 1103129473Spjd gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL); 1104129473Spjd if (gp == NULL) { 1105132664Spjd gctl_error(req, "Can't configure %s.", md.md_name); 1106129473Spjd return; 1107129473Spjd } 1108129473Spjd 1109129473Spjd sc = gp->softc; 1110181463Sdes sb = sbuf_new_auto(); 1111129473Spjd sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name); 1112129473Spjd for (attached = 0, no = 1; no < *nargs; no++) { 1113129473Spjd snprintf(param, sizeof(param), "arg%u", no); 1114129473Spjd name = gctl_get_asciiparam(req, param); 1115146109Spjd if (name == NULL) { 1116146109Spjd gctl_error(req, "No 'arg%u' argument.", no); 1117146109Spjd continue; 1118146109Spjd } 1119129473Spjd if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 1120129473Spjd name += strlen("/dev/"); 1121129473Spjd pp = g_provider_by_name(name); 1122129473Spjd KASSERT(pp != NULL, ("Provider %s disappear?!", name)); 1123129473Spjd if (g_stripe_add_disk(sc, pp, no - 1) != 0) { 1124129473Spjd G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.", 1125129473Spjd no, pp->name, gp->name); 1126129473Spjd sbuf_printf(sb, " %s", pp->name); 1127129473Spjd continue; 1128129473Spjd } 1129129473Spjd attached++; 1130129473Spjd } 1131129473Spjd sbuf_finish(sb); 1132129473Spjd if (md.md_all != attached) { 1133129473Spjd g_stripe_destroy(gp->softc, 1); 1134129473Spjd gctl_error(req, "%s", sbuf_data(sb)); 1135129473Spjd } 1136129473Spjd sbuf_delete(sb); 1137129473Spjd} 1138129473Spjd 1139129473Spjdstatic struct g_stripe_softc * 1140129473Spjdg_stripe_find_device(struct g_class *mp, const char *name) 1141129473Spjd{ 1142129473Spjd struct g_stripe_softc *sc; 1143129473Spjd struct g_geom *gp; 1144129473Spjd 1145129473Spjd LIST_FOREACH(gp, &mp->geom, geom) { 1146129473Spjd sc = gp->softc; 1147129473Spjd if (sc == NULL) 1148129473Spjd continue; 1149132664Spjd if (strcmp(sc->sc_name, name) == 0) 1150129473Spjd return (sc); 1151129473Spjd } 1152129473Spjd return (NULL); 1153129473Spjd} 1154129473Spjd 1155129473Spjdstatic void 1156129473Spjdg_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp) 1157129473Spjd{ 1158129473Spjd struct g_stripe_softc *sc; 1159129473Spjd int *force, *nargs, error; 1160129473Spjd const char *name; 1161129473Spjd char param[16]; 1162129473Spjd u_int i; 1163129473Spjd 1164129473Spjd g_topology_assert(); 1165129473Spjd 1166129473Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1167129473Spjd if (nargs == NULL) { 1168129473Spjd gctl_error(req, "No '%s' argument.", "nargs"); 1169129473Spjd return; 1170129473Spjd } 1171129473Spjd if (*nargs <= 0) { 1172129473Spjd gctl_error(req, "Missing device(s)."); 1173129473Spjd return; 1174129473Spjd } 1175129473Spjd force = gctl_get_paraml(req, "force", sizeof(*force)); 1176129473Spjd if (force == NULL) { 1177129473Spjd gctl_error(req, "No '%s' argument.", "force"); 1178129473Spjd return; 1179129473Spjd } 1180129473Spjd 1181129473Spjd for (i = 0; i < (u_int)*nargs; i++) { 1182129473Spjd snprintf(param, sizeof(param), "arg%u", i); 1183129473Spjd name = gctl_get_asciiparam(req, param); 1184129473Spjd if (name == NULL) { 1185129473Spjd gctl_error(req, "No 'arg%u' argument.", i); 1186129473Spjd return; 1187129473Spjd } 1188129473Spjd sc = g_stripe_find_device(mp, name); 1189129473Spjd if (sc == NULL) { 1190129473Spjd gctl_error(req, "No such device: %s.", name); 1191129473Spjd return; 1192129473Spjd } 1193129473Spjd error = g_stripe_destroy(sc, *force); 1194129473Spjd if (error != 0) { 1195129473Spjd gctl_error(req, "Cannot destroy device %s (error=%d).", 1196132664Spjd sc->sc_name, error); 1197129473Spjd return; 1198129473Spjd } 1199129473Spjd } 1200129473Spjd} 1201129473Spjd 1202129473Spjdstatic void 1203129473Spjdg_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb) 1204129473Spjd{ 1205129473Spjd uint32_t *version; 1206129473Spjd 1207129473Spjd g_topology_assert(); 1208129473Spjd 1209129473Spjd version = gctl_get_paraml(req, "version", sizeof(*version)); 1210129473Spjd if (version == NULL) { 1211129473Spjd gctl_error(req, "No '%s' argument.", "version"); 1212129473Spjd return; 1213129473Spjd } 1214129473Spjd if (*version != G_STRIPE_VERSION) { 1215129473Spjd gctl_error(req, "Userland and kernel parts are out of sync."); 1216129473Spjd return; 1217129473Spjd } 1218129473Spjd 1219129473Spjd if (strcmp(verb, "create") == 0) { 1220129473Spjd g_stripe_ctl_create(req, mp); 1221129473Spjd return; 1222131649Spjd } else if (strcmp(verb, "destroy") == 0 || 1223131649Spjd strcmp(verb, "stop") == 0) { 1224129473Spjd g_stripe_ctl_destroy(req, mp); 1225129473Spjd return; 1226129473Spjd } 1227129473Spjd 1228129473Spjd gctl_error(req, "Unknown verb."); 1229129473Spjd} 1230129473Spjd 1231129473Spjdstatic void 1232129473Spjdg_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1233129473Spjd struct g_consumer *cp, struct g_provider *pp) 1234129473Spjd{ 1235129473Spjd struct g_stripe_softc *sc; 1236129473Spjd 1237129473Spjd sc = gp->softc; 1238132665Spjd if (sc == NULL) 1239129473Spjd return; 1240132665Spjd if (pp != NULL) { 1241132665Spjd /* Nothing here. */ 1242132665Spjd } else if (cp != NULL) { 1243134292Spjd sbuf_printf(sb, "%s<Number>%u</Number>\n", indent, 1244134292Spjd (u_int)cp->index); 1245132665Spjd } else { 1246132665Spjd sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id); 1247132665Spjd sbuf_printf(sb, "%s<Stripesize>%u</Stripesize>\n", indent, 1248132665Spjd (u_int)sc->sc_stripesize); 1249132665Spjd sbuf_printf(sb, "%s<Type>", indent); 1250132665Spjd switch (sc->sc_type) { 1251132665Spjd case G_STRIPE_TYPE_AUTOMATIC: 1252132665Spjd sbuf_printf(sb, "AUTOMATIC"); 1253132665Spjd break; 1254132665Spjd case G_STRIPE_TYPE_MANUAL: 1255132665Spjd sbuf_printf(sb, "MANUAL"); 1256132665Spjd break; 1257132665Spjd default: 1258132665Spjd sbuf_printf(sb, "UNKNOWN"); 1259132665Spjd break; 1260132665Spjd } 1261132665Spjd sbuf_printf(sb, "</Type>\n"); 1262132665Spjd sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n", 1263132665Spjd indent, sc->sc_ndisks, g_stripe_nvalid(sc)); 1264132665Spjd sbuf_printf(sb, "%s<State>", indent); 1265132665Spjd if (sc->sc_provider != NULL && sc->sc_provider->error == 0) 1266132665Spjd sbuf_printf(sb, "UP"); 1267132665Spjd else 1268132665Spjd sbuf_printf(sb, "DOWN"); 1269132665Spjd sbuf_printf(sb, "</State>\n"); 1270129473Spjd } 1271129473Spjd} 1272129473Spjd 1273129473SpjdDECLARE_GEOM_CLASS(g_stripe_class, g_stripe); 1274