g_stripe.c revision 227309
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: head/sys/geom/stripe/g_stripe.c 227309 2011-11-07 15:43:11Z ed $"); 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) { 177148092Spjd sc->sc_provider->flags |= G_PF_WITHER; 178226998Smav G_STRIPE_DEBUG(0, "Device %s deactivated.", 179226998Smav sc->sc_provider->name); 180129473Spjd g_orphan_provider(sc->sc_provider, ENXIO); 181129473Spjd sc->sc_provider = NULL; 182129473Spjd } 183129473Spjd 184129473Spjd if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 185226998Smav return; 186226998Smav sc->sc_disks[cp->index] = NULL; 187226998Smav cp->index = 0; 188129473Spjd g_detach(cp); 189129473Spjd g_destroy_consumer(cp); 190226998Smav /* If there are no valid disks anymore, remove device. */ 191226998Smav if (LIST_EMPTY(&sc->sc_geom->consumer)) 192226998Smav g_stripe_destroy(sc, 1); 193129473Spjd} 194129473Spjd 195129473Spjdstatic void 196129473Spjdg_stripe_orphan(struct g_consumer *cp) 197129473Spjd{ 198129473Spjd struct g_stripe_softc *sc; 199129473Spjd struct g_geom *gp; 200129473Spjd 201129473Spjd g_topology_assert(); 202129473Spjd gp = cp->geom; 203129473Spjd sc = gp->softc; 204129473Spjd if (sc == NULL) 205129473Spjd return; 206129473Spjd 207129473Spjd g_stripe_remove_disk(cp); 208129473Spjd} 209129473Spjd 210129473Spjdstatic int 211129473Spjdg_stripe_access(struct g_provider *pp, int dr, int dw, int de) 212129473Spjd{ 213226998Smav struct g_consumer *cp1, *cp2, *tmp; 214129473Spjd struct g_stripe_softc *sc; 215129473Spjd struct g_geom *gp; 216129473Spjd int error; 217129473Spjd 218226998Smav g_topology_assert(); 219129473Spjd gp = pp->geom; 220129473Spjd sc = gp->softc; 221226998Smav KASSERT(sc != NULL, ("NULL sc in %s.", __func__)); 222129473Spjd 223129473Spjd /* On first open, grab an extra "exclusive" bit */ 224129473Spjd if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) 225129473Spjd de++; 226129473Spjd /* ... and let go of it on last close */ 227129473Spjd if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0) 228129473Spjd de--; 229129473Spjd 230226998Smav LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) { 231129473Spjd error = g_access(cp1, dr, dw, de); 232226998Smav if (error != 0) 233226998Smav goto fail; 234226998Smav if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 && 235226998Smav cp1->private != NULL) { 236226998Smav g_stripe_remove_disk(cp1); /* May destroy geom. */ 237129473Spjd } 238129473Spjd } 239226998Smav return (0); 240129473Spjd 241226998Smavfail: 242226998Smav LIST_FOREACH(cp2, &gp->consumer, consumer) { 243226998Smav if (cp1 == cp2) 244226998Smav break; 245226998Smav g_access(cp2, -dr, -dw, -de); 246226998Smav } 247129473Spjd return (error); 248129473Spjd} 249129473Spjd 250129473Spjdstatic void 251131878Spjdg_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset, 252131878Spjd off_t length, int mode) 253129473Spjd{ 254131878Spjd u_int stripesize; 255131878Spjd size_t len; 256131878Spjd 257131878Spjd stripesize = sc->sc_stripesize; 258131878Spjd len = (size_t)(stripesize - (offset & (stripesize - 1))); 259131878Spjd do { 260131878Spjd bcopy(src, dst, len); 261131878Spjd if (mode) { 262131878Spjd dst += len + stripesize * (sc->sc_ndisks - 1); 263131878Spjd src += len; 264131878Spjd } else { 265131878Spjd dst += len; 266131878Spjd src += len + stripesize * (sc->sc_ndisks - 1); 267131878Spjd } 268131878Spjd length -= len; 269131878Spjd KASSERT(length >= 0, 270131878Spjd ("Length < 0 (stripesize=%zu, offset=%jd, length=%jd).", 271131878Spjd (size_t)stripesize, (intmax_t)offset, (intmax_t)length)); 272131878Spjd if (length > stripesize) 273131878Spjd len = stripesize; 274131878Spjd else 275131878Spjd len = length; 276131878Spjd } while (length > 0); 277131878Spjd} 278131878Spjd 279131878Spjdstatic void 280131878Spjdg_stripe_done(struct bio *bp) 281131878Spjd{ 282129473Spjd struct g_stripe_softc *sc; 283131878Spjd struct bio *pbp; 284131878Spjd 285131878Spjd pbp = bp->bio_parent; 286131878Spjd sc = pbp->bio_to->geom->softc; 287131878Spjd if (pbp->bio_error == 0) 288131878Spjd pbp->bio_error = bp->bio_error; 289131878Spjd pbp->bio_completed += bp->bio_completed; 290133204Spjd if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) { 291133204Spjd g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset, 292131878Spjd bp->bio_length, 1); 293133204Spjd bp->bio_data = bp->bio_caller1; 294133204Spjd bp->bio_caller1 = NULL; 295131878Spjd } 296131878Spjd g_destroy_bio(bp); 297131878Spjd pbp->bio_inbed++; 298131878Spjd if (pbp->bio_children == pbp->bio_inbed) { 299133204Spjd if (pbp->bio_driver1 != NULL) 300133204Spjd uma_zfree(g_stripe_zone, pbp->bio_driver1); 301131878Spjd g_io_deliver(pbp, pbp->bio_error); 302131878Spjd } 303131878Spjd} 304131878Spjd 305131878Spjdstatic int 306131878Spjdg_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length) 307131878Spjd{ 308131878Spjd TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 309131878Spjd u_int nparts = 0, stripesize; 310131878Spjd struct g_stripe_softc *sc; 311131878Spjd char *addr, *data = NULL; 312129473Spjd struct bio *cbp; 313131878Spjd int error; 314131878Spjd 315131878Spjd sc = bp->bio_to->geom->softc; 316131878Spjd 317131878Spjd addr = bp->bio_data; 318131878Spjd stripesize = sc->sc_stripesize; 319131878Spjd 320131878Spjd cbp = g_clone_bio(bp); 321131878Spjd if (cbp == NULL) { 322131878Spjd error = ENOMEM; 323131878Spjd goto failure; 324131878Spjd } 325131878Spjd TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 326131878Spjd nparts++; 327131878Spjd /* 328131878Spjd * Fill in the component buf structure. 329131878Spjd */ 330131878Spjd cbp->bio_done = g_stripe_done; 331131878Spjd cbp->bio_offset = offset; 332131878Spjd cbp->bio_data = addr; 333133204Spjd cbp->bio_caller1 = NULL; 334131878Spjd cbp->bio_length = length; 335133204Spjd cbp->bio_caller2 = sc->sc_disks[no]; 336131878Spjd 337131878Spjd /* offset -= offset % stripesize; */ 338131878Spjd offset -= offset & (stripesize - 1); 339131878Spjd addr += length; 340131878Spjd length = bp->bio_length - length; 341131878Spjd for (no++; length > 0; no++, length -= stripesize, addr += stripesize) { 342131878Spjd if (no > sc->sc_ndisks - 1) { 343131878Spjd no = 0; 344131878Spjd offset += stripesize; 345131878Spjd } 346131878Spjd if (nparts >= sc->sc_ndisks) { 347131878Spjd cbp = TAILQ_NEXT(cbp, bio_queue); 348131878Spjd if (cbp == NULL) 349131878Spjd cbp = TAILQ_FIRST(&queue); 350131878Spjd nparts++; 351131878Spjd /* 352131878Spjd * Update bio structure. 353131878Spjd */ 354131878Spjd /* 355131878Spjd * MIN() is in case when 356131878Spjd * (bp->bio_length % sc->sc_stripesize) != 0. 357131878Spjd */ 358131878Spjd cbp->bio_length += MIN(stripesize, length); 359133204Spjd if (cbp->bio_caller1 == NULL) { 360133204Spjd cbp->bio_caller1 = cbp->bio_data; 361131878Spjd cbp->bio_data = NULL; 362131878Spjd if (data == NULL) { 363131878Spjd data = uma_zalloc(g_stripe_zone, 364131878Spjd M_NOWAIT); 365131878Spjd if (data == NULL) { 366131878Spjd error = ENOMEM; 367131878Spjd goto failure; 368131878Spjd } 369131878Spjd } 370131878Spjd } 371131878Spjd } else { 372131878Spjd cbp = g_clone_bio(bp); 373131878Spjd if (cbp == NULL) { 374131878Spjd error = ENOMEM; 375131878Spjd goto failure; 376131878Spjd } 377131878Spjd TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 378131878Spjd nparts++; 379131878Spjd /* 380131878Spjd * Fill in the component buf structure. 381131878Spjd */ 382131878Spjd cbp->bio_done = g_stripe_done; 383131878Spjd cbp->bio_offset = offset; 384131878Spjd cbp->bio_data = addr; 385133204Spjd cbp->bio_caller1 = NULL; 386131878Spjd /* 387131878Spjd * MIN() is in case when 388131878Spjd * (bp->bio_length % sc->sc_stripesize) != 0. 389131878Spjd */ 390131878Spjd cbp->bio_length = MIN(stripesize, length); 391133204Spjd cbp->bio_caller2 = sc->sc_disks[no]; 392131878Spjd } 393131878Spjd } 394131878Spjd if (data != NULL) 395133444Spjd bp->bio_driver1 = data; 396131878Spjd /* 397131878Spjd * Fire off all allocated requests! 398131878Spjd */ 399131878Spjd while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 400131878Spjd struct g_consumer *cp; 401131878Spjd 402131878Spjd TAILQ_REMOVE(&queue, cbp, bio_queue); 403133204Spjd cp = cbp->bio_caller2; 404133204Spjd cbp->bio_caller2 = NULL; 405131878Spjd cbp->bio_to = cp->provider; 406133204Spjd if (cbp->bio_caller1 != NULL) { 407131878Spjd cbp->bio_data = data; 408131878Spjd if (bp->bio_cmd == BIO_WRITE) { 409133204Spjd g_stripe_copy(sc, cbp->bio_caller1, data, 410131878Spjd cbp->bio_offset, cbp->bio_length, 0); 411131878Spjd } 412131878Spjd data += cbp->bio_length; 413131878Spjd } 414131878Spjd G_STRIPE_LOGREQ(cbp, "Sending request."); 415131878Spjd g_io_request(cbp, cp); 416131878Spjd } 417131878Spjd return (0); 418131878Spjdfailure: 419131878Spjd if (data != NULL) 420131878Spjd uma_zfree(g_stripe_zone, data); 421131878Spjd while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 422131878Spjd TAILQ_REMOVE(&queue, cbp, bio_queue); 423133204Spjd if (cbp->bio_caller1 != NULL) { 424133204Spjd cbp->bio_data = cbp->bio_caller1; 425133204Spjd cbp->bio_caller1 = NULL; 426131878Spjd } 427133201Spjd bp->bio_children--; 428131878Spjd g_destroy_bio(cbp); 429131878Spjd } 430131878Spjd return (error); 431131878Spjd} 432131878Spjd 433131878Spjdstatic int 434131878Spjdg_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length) 435131878Spjd{ 436131878Spjd TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 437131878Spjd struct g_stripe_softc *sc; 438129473Spjd uint32_t stripesize; 439131878Spjd struct bio *cbp; 440129473Spjd char *addr; 441131878Spjd int error; 442129473Spjd 443131878Spjd sc = bp->bio_to->geom->softc; 444131878Spjd 445131878Spjd addr = bp->bio_data; 446131878Spjd stripesize = sc->sc_stripesize; 447131878Spjd 448131878Spjd cbp = g_clone_bio(bp); 449131878Spjd if (cbp == NULL) { 450131878Spjd error = ENOMEM; 451131878Spjd goto failure; 452131878Spjd } 453131878Spjd TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 454129473Spjd /* 455131878Spjd * Fill in the component buf structure. 456131878Spjd */ 457131878Spjd cbp->bio_done = g_std_done; 458131878Spjd cbp->bio_offset = offset; 459131878Spjd cbp->bio_data = addr; 460131878Spjd cbp->bio_length = length; 461133204Spjd cbp->bio_caller2 = sc->sc_disks[no]; 462131878Spjd 463131878Spjd /* offset -= offset % stripesize; */ 464131878Spjd offset -= offset & (stripesize - 1); 465131878Spjd addr += length; 466131878Spjd length = bp->bio_length - length; 467131878Spjd for (no++; length > 0; no++, length -= stripesize, addr += stripesize) { 468131878Spjd if (no > sc->sc_ndisks - 1) { 469131878Spjd no = 0; 470131878Spjd offset += stripesize; 471131878Spjd } 472131878Spjd cbp = g_clone_bio(bp); 473131878Spjd if (cbp == NULL) { 474131878Spjd error = ENOMEM; 475131878Spjd goto failure; 476131878Spjd } 477131878Spjd TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 478131878Spjd 479131878Spjd /* 480131878Spjd * Fill in the component buf structure. 481131878Spjd */ 482131878Spjd cbp->bio_done = g_std_done; 483131878Spjd cbp->bio_offset = offset; 484131878Spjd cbp->bio_data = addr; 485131878Spjd /* 486131878Spjd * MIN() is in case when 487131878Spjd * (bp->bio_length % sc->sc_stripesize) != 0. 488131878Spjd */ 489131878Spjd cbp->bio_length = MIN(stripesize, length); 490131878Spjd 491133204Spjd cbp->bio_caller2 = sc->sc_disks[no]; 492131878Spjd } 493131878Spjd /* 494131878Spjd * Fire off all allocated requests! 495131878Spjd */ 496131878Spjd while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 497131878Spjd struct g_consumer *cp; 498131878Spjd 499131878Spjd TAILQ_REMOVE(&queue, cbp, bio_queue); 500133204Spjd cp = cbp->bio_caller2; 501133204Spjd cbp->bio_caller2 = NULL; 502131878Spjd cbp->bio_to = cp->provider; 503131878Spjd G_STRIPE_LOGREQ(cbp, "Sending request."); 504131878Spjd g_io_request(cbp, cp); 505131878Spjd } 506131878Spjd return (0); 507131878Spjdfailure: 508131878Spjd while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 509131878Spjd TAILQ_REMOVE(&queue, cbp, bio_queue); 510133201Spjd bp->bio_children--; 511131878Spjd g_destroy_bio(cbp); 512131878Spjd } 513131878Spjd return (error); 514131878Spjd} 515131878Spjd 516131878Spjdstatic void 517163836Spjdg_stripe_flush(struct g_stripe_softc *sc, struct bio *bp) 518163836Spjd{ 519163836Spjd struct bio_queue_head queue; 520163836Spjd struct g_consumer *cp; 521163836Spjd struct bio *cbp; 522163836Spjd u_int no; 523163836Spjd 524163836Spjd bioq_init(&queue); 525163836Spjd for (no = 0; no < sc->sc_ndisks; no++) { 526163836Spjd cbp = g_clone_bio(bp); 527163836Spjd if (cbp == NULL) { 528163836Spjd for (cbp = bioq_first(&queue); cbp != NULL; 529163836Spjd cbp = bioq_first(&queue)) { 530163836Spjd bioq_remove(&queue, cbp); 531163836Spjd g_destroy_bio(cbp); 532163836Spjd } 533163836Spjd if (bp->bio_error == 0) 534163836Spjd bp->bio_error = ENOMEM; 535163836Spjd g_io_deliver(bp, bp->bio_error); 536163836Spjd return; 537163836Spjd } 538163836Spjd bioq_insert_tail(&queue, cbp); 539163836Spjd cbp->bio_done = g_std_done; 540163836Spjd cbp->bio_caller1 = sc->sc_disks[no]; 541163836Spjd cbp->bio_to = sc->sc_disks[no]->provider; 542163836Spjd } 543163836Spjd for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) { 544163836Spjd bioq_remove(&queue, cbp); 545163836Spjd G_STRIPE_LOGREQ(cbp, "Sending request."); 546163836Spjd cp = cbp->bio_caller1; 547163836Spjd cbp->bio_caller1 = NULL; 548163836Spjd g_io_request(cbp, cp); 549163836Spjd } 550163836Spjd} 551163836Spjd 552163836Spjdstatic void 553131878Spjdg_stripe_start(struct bio *bp) 554131878Spjd{ 555131878Spjd off_t offset, start, length, nstripe; 556131878Spjd struct g_stripe_softc *sc; 557131878Spjd u_int no, stripesize; 558131878Spjd int error, fast = 0; 559131878Spjd 560131878Spjd sc = bp->bio_to->geom->softc; 561131878Spjd /* 562129473Spjd * If sc == NULL, provider's error should be set and g_stripe_start() 563129473Spjd * should not be called at all. 564129473Spjd */ 565129473Spjd KASSERT(sc != NULL, 566129473Spjd ("Provider's error should be set (error=%d)(device=%s).", 567129473Spjd bp->bio_to->error, bp->bio_to->name)); 568129473Spjd 569129473Spjd G_STRIPE_LOGREQ(bp, "Request received."); 570129473Spjd 571129473Spjd switch (bp->bio_cmd) { 572129473Spjd case BIO_READ: 573129473Spjd case BIO_WRITE: 574129473Spjd case BIO_DELETE: 575129473Spjd break; 576163886Spjd case BIO_FLUSH: 577163886Spjd g_stripe_flush(sc, bp); 578163886Spjd return; 579129473Spjd case BIO_GETATTR: 580129473Spjd /* To which provider it should be delivered? */ 581129473Spjd default: 582129473Spjd g_io_deliver(bp, EOPNOTSUPP); 583129473Spjd return; 584129473Spjd } 585129473Spjd 586129473Spjd stripesize = sc->sc_stripesize; 587129473Spjd 588129473Spjd /* 589131878Spjd * Calculations are quite messy, but fast I hope. 590129473Spjd */ 591129473Spjd 592129473Spjd /* Stripe number. */ 593129473Spjd /* nstripe = bp->bio_offset / stripesize; */ 594129473Spjd nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits; 595129473Spjd /* Disk number. */ 596129473Spjd no = nstripe % sc->sc_ndisks; 597129473Spjd /* Start position in stripe. */ 598129473Spjd /* start = bp->bio_offset % stripesize; */ 599129473Spjd start = bp->bio_offset & (stripesize - 1); 600129473Spjd /* Start position in disk. */ 601131878Spjd /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */ 602131878Spjd offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start; 603129473Spjd /* Length of data to operate. */ 604129473Spjd length = MIN(bp->bio_length, stripesize - start); 605129473Spjd 606131878Spjd /* 607131878Spjd * Do use "fast" mode when: 608131878Spjd * 1. "Fast" mode is ON. 609131878Spjd * and 610196837Smav * 2. Request size is less than or equal to MAXPHYS, 611131878Spjd * which should always be true. 612131878Spjd * and 613131878Spjd * 3. Request size is bigger than stripesize * ndisks. If it isn't, 614131878Spjd * there will be no need to send more than one I/O request to 615131878Spjd * a provider, so there is nothing to optmize. 616131878Spjd */ 617196837Smav if (g_stripe_fast && bp->bio_length <= MAXPHYS && 618131878Spjd bp->bio_length >= stripesize * sc->sc_ndisks) { 619131878Spjd fast = 1; 620129473Spjd } 621131878Spjd error = 0; 622133205Spjd if (fast) { 623131878Spjd error = g_stripe_start_fast(bp, no, offset, length); 624133205Spjd if (error != 0) 625133205Spjd g_stripe_fast_failed++; 626133205Spjd } 627129473Spjd /* 628131878Spjd * Do use "economic" when: 629131878Spjd * 1. "Economic" mode is ON. 630131878Spjd * or 631204070Spjd * 2. "Fast" mode failed. It can only fail if there is no memory. 632129473Spjd */ 633131878Spjd if (!fast || error != 0) 634131878Spjd error = g_stripe_start_economic(bp, no, offset, length); 635131878Spjd if (error != 0) { 636131878Spjd if (bp->bio_error == 0) 637131878Spjd bp->bio_error = error; 638131878Spjd g_io_deliver(bp, bp->bio_error); 639129473Spjd } 640129473Spjd} 641129473Spjd 642129473Spjdstatic void 643129473Spjdg_stripe_check_and_run(struct g_stripe_softc *sc) 644129473Spjd{ 645129473Spjd off_t mediasize, ms; 646129473Spjd u_int no, sectorsize = 0; 647129473Spjd 648226998Smav g_topology_assert(); 649129473Spjd if (g_stripe_nvalid(sc) != sc->sc_ndisks) 650129473Spjd return; 651129473Spjd 652132664Spjd sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s", 653132664Spjd sc->sc_name); 654129473Spjd /* 655129473Spjd * Find the smallest disk. 656129473Spjd */ 657129473Spjd mediasize = sc->sc_disks[0]->provider->mediasize; 658129473Spjd if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) 659129473Spjd mediasize -= sc->sc_disks[0]->provider->sectorsize; 660129473Spjd mediasize -= mediasize % sc->sc_stripesize; 661129473Spjd sectorsize = sc->sc_disks[0]->provider->sectorsize; 662129473Spjd for (no = 1; no < sc->sc_ndisks; no++) { 663129473Spjd ms = sc->sc_disks[no]->provider->mediasize; 664129473Spjd if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) 665129473Spjd ms -= sc->sc_disks[no]->provider->sectorsize; 666129473Spjd ms -= ms % sc->sc_stripesize; 667129473Spjd if (ms < mediasize) 668129473Spjd mediasize = ms; 669129473Spjd sectorsize = lcm(sectorsize, 670129473Spjd sc->sc_disks[no]->provider->sectorsize); 671129473Spjd } 672129473Spjd sc->sc_provider->sectorsize = sectorsize; 673129473Spjd sc->sc_provider->mediasize = mediasize * sc->sc_ndisks; 674200933Smav sc->sc_provider->stripesize = sc->sc_stripesize; 675200933Smav sc->sc_provider->stripeoffset = 0; 676129473Spjd g_error_provider(sc->sc_provider, 0); 677129473Spjd 678226998Smav G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name); 679129473Spjd} 680129473Spjd 681129473Spjdstatic int 682129473Spjdg_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md) 683129473Spjd{ 684129473Spjd struct g_provider *pp; 685129473Spjd u_char *buf; 686129473Spjd int error; 687129473Spjd 688129473Spjd g_topology_assert(); 689129473Spjd 690129473Spjd error = g_access(cp, 1, 0, 0); 691129473Spjd if (error != 0) 692129473Spjd return (error); 693129473Spjd pp = cp->provider; 694129473Spjd g_topology_unlock(); 695129473Spjd buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 696129473Spjd &error); 697129473Spjd g_topology_lock(); 698129473Spjd g_access(cp, -1, 0, 0); 699129473Spjd if (buf == NULL) 700129473Spjd return (error); 701129473Spjd 702129473Spjd /* Decode metadata. */ 703129473Spjd stripe_metadata_decode(buf, md); 704129473Spjd g_free(buf); 705129473Spjd 706129473Spjd return (0); 707129473Spjd} 708129473Spjd 709129473Spjd/* 710129473Spjd * Add disk to given device. 711129473Spjd */ 712129473Spjdstatic int 713129473Spjdg_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no) 714129473Spjd{ 715129473Spjd struct g_consumer *cp, *fcp; 716129473Spjd struct g_geom *gp; 717129473Spjd int error; 718129473Spjd 719226998Smav g_topology_assert(); 720129473Spjd /* Metadata corrupted? */ 721129473Spjd if (no >= sc->sc_ndisks) 722129473Spjd return (EINVAL); 723129473Spjd 724129473Spjd /* Check if disk is not already attached. */ 725129473Spjd if (sc->sc_disks[no] != NULL) 726129473Spjd return (EEXIST); 727129473Spjd 728129473Spjd gp = sc->sc_geom; 729129473Spjd fcp = LIST_FIRST(&gp->consumer); 730129473Spjd 731129473Spjd cp = g_new_consumer(gp); 732226998Smav cp->private = NULL; 733226998Smav cp->index = no; 734129473Spjd error = g_attach(cp, pp); 735129473Spjd if (error != 0) { 736129473Spjd g_destroy_consumer(cp); 737129473Spjd return (error); 738129473Spjd } 739129473Spjd 740129473Spjd if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) { 741129473Spjd error = g_access(cp, fcp->acr, fcp->acw, fcp->ace); 742129473Spjd if (error != 0) { 743129473Spjd g_detach(cp); 744129473Spjd g_destroy_consumer(cp); 745129473Spjd return (error); 746129473Spjd } 747129473Spjd } 748129473Spjd if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) { 749129473Spjd struct g_stripe_metadata md; 750129473Spjd 751129473Spjd /* Reread metadata. */ 752129473Spjd error = g_stripe_read_metadata(cp, &md); 753129473Spjd if (error != 0) 754129473Spjd goto fail; 755129473Spjd 756129473Spjd if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 || 757129473Spjd strcmp(md.md_name, sc->sc_name) != 0 || 758129473Spjd md.md_id != sc->sc_id) { 759129473Spjd G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name); 760129473Spjd goto fail; 761129473Spjd } 762129473Spjd } 763129473Spjd 764129473Spjd sc->sc_disks[no] = cp; 765132664Spjd G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name); 766129473Spjd g_stripe_check_and_run(sc); 767129473Spjd 768129473Spjd return (0); 769129473Spjdfail: 770129473Spjd if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) 771129473Spjd g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace); 772129473Spjd g_detach(cp); 773129473Spjd g_destroy_consumer(cp); 774129473Spjd return (error); 775129473Spjd} 776129473Spjd 777129473Spjdstatic struct g_geom * 778129473Spjdg_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md, 779129473Spjd u_int type) 780129473Spjd{ 781129473Spjd struct g_stripe_softc *sc; 782129473Spjd struct g_geom *gp; 783129473Spjd u_int no; 784129473Spjd 785226998Smav g_topology_assert(); 786132664Spjd G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name, 787129473Spjd md->md_id); 788129473Spjd 789129473Spjd /* Two disks is minimum. */ 790132664Spjd if (md->md_all < 2) { 791132664Spjd G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name); 792129473Spjd return (NULL); 793129473Spjd } 794129473Spjd#if 0 795129473Spjd /* Stripe size have to be grater than or equal to sector size. */ 796129473Spjd if (md->md_stripesize < sectorsize) { 797132664Spjd G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name); 798129473Spjd return (NULL); 799129473Spjd } 800129473Spjd#endif 801129473Spjd /* Stripe size have to be power of 2. */ 802129473Spjd if (!powerof2(md->md_stripesize)) { 803132664Spjd G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name); 804129473Spjd return (NULL); 805129473Spjd } 806129473Spjd 807129473Spjd /* Check for duplicate unit */ 808129473Spjd LIST_FOREACH(gp, &mp->geom, geom) { 809129473Spjd sc = gp->softc; 810129473Spjd if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) { 811129473Spjd G_STRIPE_DEBUG(0, "Device %s already configured.", 812132664Spjd sc->sc_name); 813129473Spjd return (NULL); 814129473Spjd } 815129473Spjd } 816132664Spjd gp = g_new_geomf(mp, "%s", md->md_name); 817132662Spjd sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO); 818129473Spjd gp->start = g_stripe_start; 819129473Spjd gp->spoiled = g_stripe_orphan; 820129473Spjd gp->orphan = g_stripe_orphan; 821129473Spjd gp->access = g_stripe_access; 822129473Spjd gp->dumpconf = g_stripe_dumpconf; 823129473Spjd 824129473Spjd sc->sc_id = md->md_id; 825129473Spjd sc->sc_stripesize = md->md_stripesize; 826149300Spjd sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1); 827129473Spjd sc->sc_ndisks = md->md_all; 828129473Spjd sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks, 829129473Spjd M_STRIPE, M_WAITOK | M_ZERO); 830129473Spjd for (no = 0; no < sc->sc_ndisks; no++) 831129473Spjd sc->sc_disks[no] = NULL; 832129473Spjd sc->sc_type = type; 833129473Spjd 834129473Spjd gp->softc = sc; 835129473Spjd sc->sc_geom = gp; 836129473Spjd sc->sc_provider = NULL; 837129473Spjd 838132664Spjd G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id); 839129473Spjd 840129473Spjd return (gp); 841129473Spjd} 842129473Spjd 843129473Spjdstatic int 844129473Spjdg_stripe_destroy(struct g_stripe_softc *sc, boolean_t force) 845129473Spjd{ 846129473Spjd struct g_provider *pp; 847226998Smav struct g_consumer *cp, *cp1; 848129473Spjd struct g_geom *gp; 849129473Spjd 850129473Spjd g_topology_assert(); 851129473Spjd 852129473Spjd if (sc == NULL) 853129473Spjd return (ENXIO); 854129473Spjd 855129473Spjd pp = sc->sc_provider; 856129473Spjd if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 857129473Spjd if (force) { 858129473Spjd G_STRIPE_DEBUG(0, "Device %s is still open, so it " 859129473Spjd "can't be definitely removed.", pp->name); 860129473Spjd } else { 861129473Spjd G_STRIPE_DEBUG(1, 862129473Spjd "Device %s is still open (r%dw%de%d).", pp->name, 863129473Spjd pp->acr, pp->acw, pp->ace); 864129473Spjd return (EBUSY); 865129473Spjd } 866129473Spjd } 867129473Spjd 868226998Smav gp = sc->sc_geom; 869226998Smav LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) { 870226998Smav g_stripe_remove_disk(cp); 871226998Smav if (cp1 == NULL) 872226998Smav return (0); /* Recursion happened. */ 873129473Spjd } 874226998Smav if (!LIST_EMPTY(&gp->consumer)) 875226998Smav return (EINPROGRESS); 876129473Spjd 877129473Spjd gp->softc = NULL; 878129473Spjd KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)", 879129473Spjd gp->name)); 880129473Spjd free(sc->sc_disks, M_STRIPE); 881129473Spjd free(sc, M_STRIPE); 882226998Smav G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name); 883129473Spjd g_wither_geom(gp, ENXIO); 884129473Spjd return (0); 885129473Spjd} 886129473Spjd 887129473Spjdstatic int 888129473Spjdg_stripe_destroy_geom(struct gctl_req *req __unused, 889129473Spjd struct g_class *mp __unused, struct g_geom *gp) 890129473Spjd{ 891129473Spjd struct g_stripe_softc *sc; 892129473Spjd 893129473Spjd sc = gp->softc; 894129473Spjd return (g_stripe_destroy(sc, 0)); 895129473Spjd} 896129473Spjd 897129473Spjdstatic struct g_geom * 898129473Spjdg_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 899129473Spjd{ 900129473Spjd struct g_stripe_metadata md; 901129473Spjd struct g_stripe_softc *sc; 902129473Spjd struct g_consumer *cp; 903129473Spjd struct g_geom *gp; 904129473Spjd int error; 905129473Spjd 906129473Spjd g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 907129473Spjd g_topology_assert(); 908129473Spjd 909197898Spjd /* Skip providers that are already open for writing. */ 910197898Spjd if (pp->acw > 0) 911197898Spjd return (NULL); 912197898Spjd 913129473Spjd G_STRIPE_DEBUG(3, "Tasting %s.", pp->name); 914129473Spjd 915129473Spjd gp = g_new_geomf(mp, "stripe:taste"); 916129473Spjd gp->start = g_stripe_start; 917129473Spjd gp->access = g_stripe_access; 918129473Spjd gp->orphan = g_stripe_orphan; 919129473Spjd cp = g_new_consumer(gp); 920129473Spjd g_attach(cp, pp); 921129473Spjd error = g_stripe_read_metadata(cp, &md); 922133371Spjd g_detach(cp); 923133371Spjd g_destroy_consumer(cp); 924133371Spjd g_destroy_geom(gp); 925129473Spjd if (error != 0) 926129473Spjd return (NULL); 927129473Spjd gp = NULL; 928129473Spjd 929129473Spjd if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0) 930129473Spjd return (NULL); 931129473Spjd if (md.md_version > G_STRIPE_VERSION) { 932129473Spjd printf("geom_stripe.ko module is too old to handle %s.\n", 933129473Spjd pp->name); 934129473Spjd return (NULL); 935129473Spjd } 936133373Spjd /* 937133373Spjd * Backward compatibility: 938133373Spjd */ 939142727Spjd /* There was no md_provider field in earlier versions of metadata. */ 940133373Spjd if (md.md_version < 2) 941133373Spjd bzero(md.md_provider, sizeof(md.md_provider)); 942142727Spjd /* There was no md_provsize field in earlier versions of metadata. */ 943142727Spjd if (md.md_version < 3) 944142727Spjd md.md_provsize = pp->mediasize; 945129473Spjd 946221101Smav if (md.md_provider[0] != '\0' && 947221101Smav !g_compare_names(md.md_provider, pp->name)) 948133373Spjd return (NULL); 949142727Spjd if (md.md_provsize != pp->mediasize) 950142727Spjd return (NULL); 951133373Spjd 952129473Spjd /* 953129473Spjd * Let's check if device already exists. 954129473Spjd */ 955129473Spjd sc = NULL; 956129473Spjd LIST_FOREACH(gp, &mp->geom, geom) { 957129473Spjd sc = gp->softc; 958129473Spjd if (sc == NULL) 959129473Spjd continue; 960129473Spjd if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC) 961129473Spjd continue; 962129473Spjd if (strcmp(md.md_name, sc->sc_name) != 0) 963129473Spjd continue; 964129473Spjd if (md.md_id != sc->sc_id) 965129473Spjd continue; 966129473Spjd break; 967129473Spjd } 968129473Spjd if (gp != NULL) { 969129473Spjd G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 970129473Spjd error = g_stripe_add_disk(sc, pp, md.md_no); 971129473Spjd if (error != 0) { 972129473Spjd G_STRIPE_DEBUG(0, 973129473Spjd "Cannot add disk %s to %s (error=%d).", pp->name, 974129473Spjd gp->name, error); 975129473Spjd return (NULL); 976129473Spjd } 977129473Spjd } else { 978129473Spjd gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC); 979129473Spjd if (gp == NULL) { 980132664Spjd G_STRIPE_DEBUG(0, "Cannot create device %s.", 981129473Spjd md.md_name); 982129473Spjd return (NULL); 983129473Spjd } 984129473Spjd sc = gp->softc; 985129473Spjd G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 986129473Spjd error = g_stripe_add_disk(sc, pp, md.md_no); 987129473Spjd if (error != 0) { 988129473Spjd G_STRIPE_DEBUG(0, 989129473Spjd "Cannot add disk %s to %s (error=%d).", pp->name, 990129473Spjd gp->name, error); 991129473Spjd g_stripe_destroy(sc, 1); 992129473Spjd return (NULL); 993129473Spjd } 994129473Spjd } 995129473Spjd 996129473Spjd return (gp); 997129473Spjd} 998129473Spjd 999129473Spjdstatic void 1000129473Spjdg_stripe_ctl_create(struct gctl_req *req, struct g_class *mp) 1001129473Spjd{ 1002129473Spjd u_int attached, no; 1003129473Spjd struct g_stripe_metadata md; 1004129473Spjd struct g_provider *pp; 1005129473Spjd struct g_stripe_softc *sc; 1006129473Spjd struct g_geom *gp; 1007129473Spjd struct sbuf *sb; 1008129473Spjd intmax_t *stripesize; 1009129473Spjd const char *name; 1010129473Spjd char param[16]; 1011129473Spjd int *nargs; 1012129473Spjd 1013129473Spjd g_topology_assert(); 1014129473Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1015129473Spjd if (nargs == NULL) { 1016129473Spjd gctl_error(req, "No '%s' argument.", "nargs"); 1017129473Spjd return; 1018129473Spjd } 1019129473Spjd if (*nargs <= 2) { 1020129473Spjd gctl_error(req, "Too few arguments."); 1021129473Spjd return; 1022129473Spjd } 1023129473Spjd 1024129473Spjd strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic)); 1025129473Spjd md.md_version = G_STRIPE_VERSION; 1026129473Spjd name = gctl_get_asciiparam(req, "arg0"); 1027129473Spjd if (name == NULL) { 1028129473Spjd gctl_error(req, "No 'arg%u' argument.", 0); 1029129473Spjd return; 1030129473Spjd } 1031129473Spjd strlcpy(md.md_name, name, sizeof(md.md_name)); 1032129473Spjd md.md_id = arc4random(); 1033129473Spjd md.md_no = 0; 1034129473Spjd md.md_all = *nargs - 1; 1035129473Spjd stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize)); 1036129473Spjd if (stripesize == NULL) { 1037129473Spjd gctl_error(req, "No '%s' argument.", "stripesize"); 1038129473Spjd return; 1039129473Spjd } 1040129473Spjd md.md_stripesize = *stripesize; 1041133373Spjd bzero(md.md_provider, sizeof(md.md_provider)); 1042142727Spjd /* This field is not important here. */ 1043142727Spjd md.md_provsize = 0; 1044129473Spjd 1045129473Spjd /* Check all providers are valid */ 1046129473Spjd for (no = 1; no < *nargs; no++) { 1047129473Spjd snprintf(param, sizeof(param), "arg%u", no); 1048129473Spjd name = gctl_get_asciiparam(req, param); 1049129473Spjd if (name == NULL) { 1050129473Spjd gctl_error(req, "No 'arg%u' argument.", no); 1051129473Spjd return; 1052129473Spjd } 1053129473Spjd if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 1054129473Spjd name += strlen("/dev/"); 1055129473Spjd pp = g_provider_by_name(name); 1056129473Spjd if (pp == NULL) { 1057129473Spjd G_STRIPE_DEBUG(1, "Disk %s is invalid.", name); 1058129473Spjd gctl_error(req, "Disk %s is invalid.", name); 1059129473Spjd return; 1060129473Spjd } 1061129473Spjd } 1062129473Spjd 1063129473Spjd gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL); 1064129473Spjd if (gp == NULL) { 1065132664Spjd gctl_error(req, "Can't configure %s.", md.md_name); 1066129473Spjd return; 1067129473Spjd } 1068129473Spjd 1069129473Spjd sc = gp->softc; 1070181463Sdes sb = sbuf_new_auto(); 1071129473Spjd sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name); 1072129473Spjd for (attached = 0, no = 1; no < *nargs; no++) { 1073129473Spjd snprintf(param, sizeof(param), "arg%u", no); 1074129473Spjd name = gctl_get_asciiparam(req, param); 1075146109Spjd if (name == NULL) { 1076146109Spjd gctl_error(req, "No 'arg%u' argument.", no); 1077146109Spjd continue; 1078146109Spjd } 1079129473Spjd if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 1080129473Spjd name += strlen("/dev/"); 1081129473Spjd pp = g_provider_by_name(name); 1082129473Spjd KASSERT(pp != NULL, ("Provider %s disappear?!", name)); 1083129473Spjd if (g_stripe_add_disk(sc, pp, no - 1) != 0) { 1084129473Spjd G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.", 1085129473Spjd no, pp->name, gp->name); 1086129473Spjd sbuf_printf(sb, " %s", pp->name); 1087129473Spjd continue; 1088129473Spjd } 1089129473Spjd attached++; 1090129473Spjd } 1091129473Spjd sbuf_finish(sb); 1092129473Spjd if (md.md_all != attached) { 1093129473Spjd g_stripe_destroy(gp->softc, 1); 1094129473Spjd gctl_error(req, "%s", sbuf_data(sb)); 1095129473Spjd } 1096129473Spjd sbuf_delete(sb); 1097129473Spjd} 1098129473Spjd 1099129473Spjdstatic struct g_stripe_softc * 1100129473Spjdg_stripe_find_device(struct g_class *mp, const char *name) 1101129473Spjd{ 1102129473Spjd struct g_stripe_softc *sc; 1103129473Spjd struct g_geom *gp; 1104129473Spjd 1105129473Spjd LIST_FOREACH(gp, &mp->geom, geom) { 1106129473Spjd sc = gp->softc; 1107129473Spjd if (sc == NULL) 1108129473Spjd continue; 1109132664Spjd if (strcmp(sc->sc_name, name) == 0) 1110129473Spjd return (sc); 1111129473Spjd } 1112129473Spjd return (NULL); 1113129473Spjd} 1114129473Spjd 1115129473Spjdstatic void 1116129473Spjdg_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp) 1117129473Spjd{ 1118129473Spjd struct g_stripe_softc *sc; 1119129473Spjd int *force, *nargs, error; 1120129473Spjd const char *name; 1121129473Spjd char param[16]; 1122129473Spjd u_int i; 1123129473Spjd 1124129473Spjd g_topology_assert(); 1125129473Spjd 1126129473Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1127129473Spjd if (nargs == NULL) { 1128129473Spjd gctl_error(req, "No '%s' argument.", "nargs"); 1129129473Spjd return; 1130129473Spjd } 1131129473Spjd if (*nargs <= 0) { 1132129473Spjd gctl_error(req, "Missing device(s)."); 1133129473Spjd return; 1134129473Spjd } 1135129473Spjd force = gctl_get_paraml(req, "force", sizeof(*force)); 1136129473Spjd if (force == NULL) { 1137129473Spjd gctl_error(req, "No '%s' argument.", "force"); 1138129473Spjd return; 1139129473Spjd } 1140129473Spjd 1141129473Spjd for (i = 0; i < (u_int)*nargs; i++) { 1142129473Spjd snprintf(param, sizeof(param), "arg%u", i); 1143129473Spjd name = gctl_get_asciiparam(req, param); 1144129473Spjd if (name == NULL) { 1145129473Spjd gctl_error(req, "No 'arg%u' argument.", i); 1146129473Spjd return; 1147129473Spjd } 1148129473Spjd sc = g_stripe_find_device(mp, name); 1149129473Spjd if (sc == NULL) { 1150129473Spjd gctl_error(req, "No such device: %s.", name); 1151129473Spjd return; 1152129473Spjd } 1153129473Spjd error = g_stripe_destroy(sc, *force); 1154129473Spjd if (error != 0) { 1155129473Spjd gctl_error(req, "Cannot destroy device %s (error=%d).", 1156132664Spjd sc->sc_name, error); 1157129473Spjd return; 1158129473Spjd } 1159129473Spjd } 1160129473Spjd} 1161129473Spjd 1162129473Spjdstatic void 1163129473Spjdg_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb) 1164129473Spjd{ 1165129473Spjd uint32_t *version; 1166129473Spjd 1167129473Spjd g_topology_assert(); 1168129473Spjd 1169129473Spjd version = gctl_get_paraml(req, "version", sizeof(*version)); 1170129473Spjd if (version == NULL) { 1171129473Spjd gctl_error(req, "No '%s' argument.", "version"); 1172129473Spjd return; 1173129473Spjd } 1174129473Spjd if (*version != G_STRIPE_VERSION) { 1175129473Spjd gctl_error(req, "Userland and kernel parts are out of sync."); 1176129473Spjd return; 1177129473Spjd } 1178129473Spjd 1179129473Spjd if (strcmp(verb, "create") == 0) { 1180129473Spjd g_stripe_ctl_create(req, mp); 1181129473Spjd return; 1182131649Spjd } else if (strcmp(verb, "destroy") == 0 || 1183131649Spjd strcmp(verb, "stop") == 0) { 1184129473Spjd g_stripe_ctl_destroy(req, mp); 1185129473Spjd return; 1186129473Spjd } 1187129473Spjd 1188129473Spjd gctl_error(req, "Unknown verb."); 1189129473Spjd} 1190129473Spjd 1191129473Spjdstatic void 1192129473Spjdg_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1193129473Spjd struct g_consumer *cp, struct g_provider *pp) 1194129473Spjd{ 1195129473Spjd struct g_stripe_softc *sc; 1196129473Spjd 1197129473Spjd sc = gp->softc; 1198132665Spjd if (sc == NULL) 1199129473Spjd return; 1200132665Spjd if (pp != NULL) { 1201132665Spjd /* Nothing here. */ 1202132665Spjd } else if (cp != NULL) { 1203134292Spjd sbuf_printf(sb, "%s<Number>%u</Number>\n", indent, 1204134292Spjd (u_int)cp->index); 1205132665Spjd } else { 1206132665Spjd sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id); 1207132665Spjd sbuf_printf(sb, "%s<Stripesize>%u</Stripesize>\n", indent, 1208132665Spjd (u_int)sc->sc_stripesize); 1209132665Spjd sbuf_printf(sb, "%s<Type>", indent); 1210132665Spjd switch (sc->sc_type) { 1211132665Spjd case G_STRIPE_TYPE_AUTOMATIC: 1212132665Spjd sbuf_printf(sb, "AUTOMATIC"); 1213132665Spjd break; 1214132665Spjd case G_STRIPE_TYPE_MANUAL: 1215132665Spjd sbuf_printf(sb, "MANUAL"); 1216132665Spjd break; 1217132665Spjd default: 1218132665Spjd sbuf_printf(sb, "UNKNOWN"); 1219132665Spjd break; 1220132665Spjd } 1221132665Spjd sbuf_printf(sb, "</Type>\n"); 1222132665Spjd sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n", 1223132665Spjd indent, sc->sc_ndisks, g_stripe_nvalid(sc)); 1224132665Spjd sbuf_printf(sb, "%s<State>", indent); 1225132665Spjd if (sc->sc_provider != NULL && sc->sc_provider->error == 0) 1226132665Spjd sbuf_printf(sb, "UP"); 1227132665Spjd else 1228132665Spjd sbuf_printf(sb, "DOWN"); 1229132665Spjd sbuf_printf(sb, "</State>\n"); 1230129473Spjd } 1231129473Spjd} 1232129473Spjd 1233129473SpjdDECLARE_GEOM_CLASS(g_stripe_class, g_stripe); 1234