g_stripe.c revision 132664
1129473Spjd/*- 2129473Spjd * Copyright (c) 2003 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. 13129473Spjd * 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 132664 2004-07-26 16:10:27Z pjd $"); 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> 37129473Spjd#include <sys/sysctl.h> 38129473Spjd#include <sys/malloc.h> 39131878Spjd#include <vm/uma.h> 40129473Spjd#include <geom/geom.h> 41129473Spjd#include <geom/stripe/g_stripe.h> 42129473Spjd 43129473Spjd 44131878Spjd#define MAX_IO_SIZE (DFLTPHYS * 2) 45129473Spjdstatic MALLOC_DEFINE(M_STRIPE, "stripe data", "GEOM_STRIPE Data"); 46129473Spjd 47131878Spjdstatic uma_zone_t g_stripe_zone; 48129473Spjd 49129473Spjdstatic int g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force); 50129473Spjdstatic int g_stripe_destroy_geom(struct gctl_req *req, struct g_class *mp, 51129473Spjd struct g_geom *gp); 52129473Spjd 53129473Spjdstatic g_taste_t g_stripe_taste; 54129473Spjdstatic g_ctl_req_t g_stripe_config; 55129473Spjdstatic g_dumpconf_t g_stripe_dumpconf; 56131878Spjdstatic g_init_t g_stripe_init; 57131878Spjdstatic g_fini_t g_stripe_fini; 58129473Spjd 59129473Spjdstruct g_class g_stripe_class = { 60129473Spjd .name = G_STRIPE_CLASS_NAME, 61129473Spjd .ctlreq = g_stripe_config, 62129473Spjd .taste = g_stripe_taste, 63131878Spjd .destroy_geom = g_stripe_destroy_geom, 64131878Spjd .init = g_stripe_init, 65131878Spjd .fini = g_stripe_fini 66129473Spjd}; 67129473Spjd 68131878SpjdSYSCTL_DECL(_kern_geom); 69131878SpjdSYSCTL_NODE(_kern_geom, OID_AUTO, stripe, CTLFLAG_RW, 0, "GEOM_STRIPE stuff"); 70131878Spjdstatic u_int g_stripe_debug = 0; 71131878SpjdSYSCTL_UINT(_kern_geom_stripe, OID_AUTO, debug, CTLFLAG_RW, &g_stripe_debug, 0, 72131878Spjd "Debug level"); 73131878Spjdstatic int g_stripe_fast = 1; 74131878SpjdTUNABLE_INT("kern.geom.stripe.fast", &g_stripe_fast); 75131878Spjdstatic int 76131878Spjdg_sysctl_stripe_fast(SYSCTL_HANDLER_ARGS) 77131878Spjd{ 78131878Spjd int error, fast; 79129473Spjd 80131878Spjd fast = g_stripe_fast; 81131878Spjd error = sysctl_handle_int(oidp, &fast, sizeof(fast), req); 82131878Spjd if (error == 0 && req->newptr != NULL) 83131878Spjd g_stripe_fast = fast; 84131878Spjd return (error); 85131878Spjd} 86131878SpjdSYSCTL_PROC(_kern_geom_stripe, OID_AUTO, fast, CTLTYPE_INT | CTLFLAG_RW, 87132095Spjd NULL, 0, g_sysctl_stripe_fast, "I", "Fast, but memory-consuming, mode"); 88131878Spjdstatic u_int g_stripe_maxmem = MAX_IO_SIZE * 10; 89131878SpjdTUNABLE_INT("kern.geom.stripe.maxmem", &g_stripe_maxmem); 90131878SpjdSYSCTL_UINT(_kern_geom_stripe, OID_AUTO, maxmem, CTLFLAG_RD, &g_stripe_maxmem, 91132095Spjd 0, "Maximum memory that can be allocated in \"fast\" mode (in bytes)"); 92131878Spjd 93129473Spjd/* 94129473Spjd * Greatest Common Divisor. 95129473Spjd */ 96129473Spjdstatic u_int 97129473Spjdgcd(u_int a, u_int b) 98129473Spjd{ 99129473Spjd u_int c; 100129473Spjd 101129473Spjd while (b != 0) { 102129473Spjd c = a; 103129473Spjd a = b; 104129473Spjd b = (c % b); 105129473Spjd } 106129473Spjd return (a); 107129473Spjd} 108129473Spjd 109129473Spjd/* 110129473Spjd * Least Common Multiple. 111129473Spjd */ 112129473Spjdstatic u_int 113129473Spjdlcm(u_int a, u_int b) 114129473Spjd{ 115129473Spjd 116129473Spjd return ((a * b) / gcd(a, b)); 117129473Spjd} 118129473Spjd 119131878Spjdstatic void 120131878Spjdg_stripe_init(struct g_class *mp __unused) 121131878Spjd{ 122131878Spjd 123131878Spjd g_stripe_zone = uma_zcreate("g_stripe_zone", MAX_IO_SIZE, NULL, NULL, 124131878Spjd NULL, NULL, 0, 0); 125131878Spjd g_stripe_maxmem -= g_stripe_maxmem % MAX_IO_SIZE; 126131878Spjd uma_zone_set_max(g_stripe_zone, g_stripe_maxmem / MAX_IO_SIZE); 127131878Spjd} 128131878Spjd 129131878Spjdstatic void 130131878Spjdg_stripe_fini(struct g_class *mp __unused) 131131878Spjd{ 132131878Spjd 133131878Spjd uma_zdestroy(g_stripe_zone); 134131878Spjd} 135131878Spjd 136129473Spjd/* 137129473Spjd * Return the number of valid disks. 138129473Spjd */ 139129473Spjdstatic u_int 140129473Spjdg_stripe_nvalid(struct g_stripe_softc *sc) 141129473Spjd{ 142129473Spjd u_int i, no; 143129473Spjd 144129473Spjd no = 0; 145129473Spjd for (i = 0; i < sc->sc_ndisks; i++) { 146129473Spjd if (sc->sc_disks[i] != NULL) 147129473Spjd no++; 148129473Spjd } 149129473Spjd 150129473Spjd return (no); 151129473Spjd} 152129473Spjd 153129473Spjdstatic void 154129473Spjdg_stripe_remove_disk(struct g_consumer *cp) 155129473Spjd{ 156129473Spjd struct g_stripe_softc *sc; 157129473Spjd u_int no; 158129473Spjd 159129473Spjd KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__)); 160129473Spjd sc = (struct g_stripe_softc *)cp->private; 161129473Spjd KASSERT(sc != NULL, ("NULL sc in %s.", __func__)); 162129473Spjd no = cp->index; 163129473Spjd 164129473Spjd G_STRIPE_DEBUG(0, "Disk %s removed from %s.", cp->provider->name, 165132664Spjd sc->sc_name); 166129473Spjd 167129473Spjd sc->sc_disks[no] = NULL; 168129473Spjd if (sc->sc_provider != NULL) { 169129473Spjd g_orphan_provider(sc->sc_provider, ENXIO); 170129473Spjd sc->sc_provider = NULL; 171132664Spjd G_STRIPE_DEBUG(0, "Device %s removed.", sc->sc_name); 172129473Spjd } 173129473Spjd 174129473Spjd if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 175129473Spjd g_access(cp, -cp->acr, -cp->acw, -cp->ace); 176129473Spjd g_detach(cp); 177129473Spjd g_destroy_consumer(cp); 178129473Spjd} 179129473Spjd 180129473Spjdstatic void 181129473Spjdg_stripe_orphan(struct g_consumer *cp) 182129473Spjd{ 183129473Spjd struct g_stripe_softc *sc; 184129473Spjd struct g_geom *gp; 185129473Spjd 186129473Spjd g_topology_assert(); 187129473Spjd gp = cp->geom; 188129473Spjd sc = gp->softc; 189129473Spjd if (sc == NULL) 190129473Spjd return; 191129473Spjd 192129473Spjd g_stripe_remove_disk(cp); 193129473Spjd /* If there are no valid disks anymore, remove device. */ 194129473Spjd if (g_stripe_nvalid(sc) == 0) 195129473Spjd g_stripe_destroy(sc, 1); 196129473Spjd} 197129473Spjd 198129473Spjdstatic int 199129473Spjdg_stripe_access(struct g_provider *pp, int dr, int dw, int de) 200129473Spjd{ 201129473Spjd struct g_consumer *cp1, *cp2; 202129473Spjd struct g_stripe_softc *sc; 203129473Spjd struct g_geom *gp; 204129473Spjd int error; 205129473Spjd 206129473Spjd gp = pp->geom; 207129473Spjd sc = gp->softc; 208129473Spjd 209129473Spjd if (sc == NULL) { 210129473Spjd /* 211129473Spjd * It looks like geom is being withered. 212129473Spjd * In that case we allow only negative requests. 213129473Spjd */ 214129473Spjd KASSERT(dr <= 0 && dw <= 0 && de <= 0, 215129473Spjd ("Positive access request (device=%s).", pp->name)); 216129473Spjd if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && 217129473Spjd (pp->ace + de) == 0) { 218129473Spjd G_STRIPE_DEBUG(0, "Device %s definitely destroyed.", 219129473Spjd gp->name); 220129473Spjd } 221129473Spjd return (0); 222129473Spjd } 223129473Spjd 224129473Spjd /* On first open, grab an extra "exclusive" bit */ 225129473Spjd if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) 226129473Spjd de++; 227129473Spjd /* ... and let go of it on last close */ 228129473Spjd if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0) 229129473Spjd de--; 230129473Spjd 231129473Spjd error = ENXIO; 232129473Spjd LIST_FOREACH(cp1, &gp->consumer, consumer) { 233129473Spjd error = g_access(cp1, dr, dw, de); 234129473Spjd if (error == 0) 235129473Spjd continue; 236129473Spjd /* 237129473Spjd * If we fail here, backout all previous changes. 238129473Spjd */ 239129473Spjd LIST_FOREACH(cp2, &gp->consumer, consumer) { 240129473Spjd if (cp1 == cp2) 241129473Spjd return (error); 242129473Spjd g_access(cp2, -dr, -dw, -de); 243129473Spjd } 244129473Spjd /* NOTREACHED */ 245129473Spjd } 246129473Spjd 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; 290131878Spjd if (bp->bio_cmd == BIO_READ && bp->bio_driver1 != NULL) { 291131878Spjd g_stripe_copy(sc, bp->bio_data, bp->bio_driver1, bp->bio_offset, 292131878Spjd bp->bio_length, 1); 293131878Spjd bp->bio_data = bp->bio_driver1; 294131878Spjd bp->bio_driver1 = NULL; 295131878Spjd } 296131878Spjd g_destroy_bio(bp); 297131878Spjd pbp->bio_inbed++; 298131878Spjd if (pbp->bio_children == pbp->bio_inbed) { 299131878Spjd if (pbp->bio_caller1 != NULL) 300131878Spjd uma_zfree(g_stripe_zone, pbp->bio_caller1); 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; 333131878Spjd cbp->bio_driver1 = NULL; 334131878Spjd cbp->bio_length = length; 335131878Spjd cbp->bio_driver2 = 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); 359131878Spjd if (cbp->bio_driver1 == NULL) { 360131878Spjd cbp->bio_driver1 = 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; 385131878Spjd cbp->bio_driver1 = NULL; 386131878Spjd /* 387131878Spjd * MIN() is in case when 388131878Spjd * (bp->bio_length % sc->sc_stripesize) != 0. 389131878Spjd */ 390131878Spjd cbp->bio_length = MIN(stripesize, length); 391131878Spjd cbp->bio_driver2 = sc->sc_disks[no]; 392131878Spjd } 393131878Spjd } 394131878Spjd if (data != NULL) 395131878Spjd bp->bio_caller1 = 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); 403131878Spjd cp = cbp->bio_driver2; 404131878Spjd cbp->bio_driver2 = NULL; 405131878Spjd cbp->bio_to = cp->provider; 406131878Spjd if (cbp->bio_driver1 != NULL) { 407131878Spjd cbp->bio_data = data; 408131878Spjd if (bp->bio_cmd == BIO_WRITE) { 409131878Spjd g_stripe_copy(sc, cbp->bio_driver1, 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); 423131878Spjd if (cbp->bio_driver1 != NULL) { 424131878Spjd cbp->bio_data = cbp->bio_driver1; 425131878Spjd cbp->bio_driver1 = NULL; 426131878Spjd } 427131878Spjd g_destroy_bio(cbp); 428131878Spjd } 429131878Spjd return (error); 430131878Spjd} 431131878Spjd 432131878Spjdstatic int 433131878Spjdg_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length) 434131878Spjd{ 435131878Spjd TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 436131878Spjd struct g_stripe_softc *sc; 437129473Spjd uint32_t stripesize; 438131878Spjd struct bio *cbp; 439129473Spjd char *addr; 440131878Spjd int error; 441129473Spjd 442131878Spjd sc = bp->bio_to->geom->softc; 443131878Spjd 444131878Spjd addr = bp->bio_data; 445131878Spjd stripesize = sc->sc_stripesize; 446131878Spjd 447131878Spjd cbp = g_clone_bio(bp); 448131878Spjd if (cbp == NULL) { 449131878Spjd error = ENOMEM; 450131878Spjd goto failure; 451131878Spjd } 452131878Spjd TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 453129473Spjd /* 454131878Spjd * Fill in the component buf structure. 455131878Spjd */ 456131878Spjd cbp->bio_done = g_std_done; 457131878Spjd cbp->bio_offset = offset; 458131878Spjd cbp->bio_data = addr; 459131878Spjd cbp->bio_length = length; 460131878Spjd cbp->bio_driver2 = sc->sc_disks[no]; 461131878Spjd 462131878Spjd /* offset -= offset % stripesize; */ 463131878Spjd offset -= offset & (stripesize - 1); 464131878Spjd addr += length; 465131878Spjd length = bp->bio_length - length; 466131878Spjd for (no++; length > 0; no++, length -= stripesize, addr += stripesize) { 467131878Spjd if (no > sc->sc_ndisks - 1) { 468131878Spjd no = 0; 469131878Spjd offset += stripesize; 470131878Spjd } 471131878Spjd cbp = g_clone_bio(bp); 472131878Spjd if (cbp == NULL) { 473131878Spjd error = ENOMEM; 474131878Spjd goto failure; 475131878Spjd } 476131878Spjd TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 477131878Spjd 478131878Spjd /* 479131878Spjd * Fill in the component buf structure. 480131878Spjd */ 481131878Spjd cbp->bio_done = g_std_done; 482131878Spjd cbp->bio_offset = offset; 483131878Spjd cbp->bio_data = addr; 484131878Spjd /* 485131878Spjd * MIN() is in case when 486131878Spjd * (bp->bio_length % sc->sc_stripesize) != 0. 487131878Spjd */ 488131878Spjd cbp->bio_length = MIN(stripesize, length); 489131878Spjd 490131878Spjd cbp->bio_driver2 = sc->sc_disks[no]; 491131878Spjd } 492131878Spjd /* 493131878Spjd * Fire off all allocated requests! 494131878Spjd */ 495131878Spjd while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 496131878Spjd struct g_consumer *cp; 497131878Spjd 498131878Spjd TAILQ_REMOVE(&queue, cbp, bio_queue); 499131878Spjd cp = cbp->bio_driver2; 500131878Spjd cbp->bio_driver2 = NULL; 501131878Spjd cbp->bio_to = cp->provider; 502131878Spjd G_STRIPE_LOGREQ(cbp, "Sending request."); 503131878Spjd g_io_request(cbp, cp); 504131878Spjd } 505131878Spjd return (0); 506131878Spjdfailure: 507131878Spjd while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 508131878Spjd TAILQ_REMOVE(&queue, cbp, bio_queue); 509131878Spjd g_destroy_bio(cbp); 510131878Spjd } 511131878Spjd return (error); 512131878Spjd} 513131878Spjd 514131878Spjdstatic void 515131878Spjdg_stripe_start(struct bio *bp) 516131878Spjd{ 517131878Spjd off_t offset, start, length, nstripe; 518131878Spjd struct g_stripe_softc *sc; 519131878Spjd u_int no, stripesize; 520131878Spjd int error, fast = 0; 521131878Spjd 522131878Spjd sc = bp->bio_to->geom->softc; 523131878Spjd /* 524129473Spjd * If sc == NULL, provider's error should be set and g_stripe_start() 525129473Spjd * should not be called at all. 526129473Spjd */ 527129473Spjd KASSERT(sc != NULL, 528129473Spjd ("Provider's error should be set (error=%d)(device=%s).", 529129473Spjd bp->bio_to->error, bp->bio_to->name)); 530129473Spjd 531129473Spjd G_STRIPE_LOGREQ(bp, "Request received."); 532129473Spjd 533129473Spjd switch (bp->bio_cmd) { 534129473Spjd case BIO_READ: 535129473Spjd case BIO_WRITE: 536129473Spjd case BIO_DELETE: 537129473Spjd /* 538129473Spjd * Only those requests are supported. 539129473Spjd */ 540129473Spjd break; 541129473Spjd case BIO_GETATTR: 542129473Spjd /* To which provider it should be delivered? */ 543129473Spjd default: 544129473Spjd g_io_deliver(bp, EOPNOTSUPP); 545129473Spjd return; 546129473Spjd } 547129473Spjd 548129473Spjd stripesize = sc->sc_stripesize; 549129473Spjd 550129473Spjd /* 551131878Spjd * Calculations are quite messy, but fast I hope. 552129473Spjd */ 553129473Spjd 554129473Spjd /* Stripe number. */ 555129473Spjd /* nstripe = bp->bio_offset / stripesize; */ 556129473Spjd nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits; 557129473Spjd /* Disk number. */ 558129473Spjd no = nstripe % sc->sc_ndisks; 559129473Spjd /* Start position in stripe. */ 560129473Spjd /* start = bp->bio_offset % stripesize; */ 561129473Spjd start = bp->bio_offset & (stripesize - 1); 562129473Spjd /* Start position in disk. */ 563131878Spjd /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */ 564131878Spjd offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start; 565129473Spjd /* Length of data to operate. */ 566129473Spjd length = MIN(bp->bio_length, stripesize - start); 567129473Spjd 568131878Spjd /* 569131878Spjd * Do use "fast" mode when: 570131878Spjd * 1. "Fast" mode is ON. 571131878Spjd * and 572131878Spjd * 2. Request size is less than or equal to MAX_IO_SIZE (128kB), 573131878Spjd * which should always be true. 574131878Spjd * and 575131878Spjd * 3. Request size is bigger than stripesize * ndisks. If it isn't, 576131878Spjd * there will be no need to send more than one I/O request to 577131878Spjd * a provider, so there is nothing to optmize. 578131878Spjd */ 579131878Spjd if (g_stripe_fast && bp->bio_length <= MAX_IO_SIZE && 580131878Spjd bp->bio_length >= stripesize * sc->sc_ndisks) { 581131878Spjd fast = 1; 582129473Spjd } 583131878Spjd error = 0; 584131878Spjd if (fast) 585131878Spjd error = g_stripe_start_fast(bp, no, offset, length); 586129473Spjd /* 587131878Spjd * Do use "economic" when: 588131878Spjd * 1. "Economic" mode is ON. 589131878Spjd * or 590131878Spjd * 2. "Fast" mode failed. It can only failed if there is no memory. 591129473Spjd */ 592131878Spjd if (!fast || error != 0) 593131878Spjd error = g_stripe_start_economic(bp, no, offset, length); 594131878Spjd if (error != 0) { 595131878Spjd if (bp->bio_error == 0) 596131878Spjd bp->bio_error = error; 597131878Spjd g_io_deliver(bp, bp->bio_error); 598129473Spjd } 599129473Spjd} 600129473Spjd 601129473Spjdstatic void 602129473Spjdg_stripe_check_and_run(struct g_stripe_softc *sc) 603129473Spjd{ 604129473Spjd off_t mediasize, ms; 605129473Spjd u_int no, sectorsize = 0; 606129473Spjd 607129473Spjd if (g_stripe_nvalid(sc) != sc->sc_ndisks) 608129473Spjd return; 609129473Spjd 610132664Spjd sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s", 611132664Spjd sc->sc_name); 612129473Spjd /* 613129473Spjd * Find the smallest disk. 614129473Spjd */ 615129473Spjd mediasize = sc->sc_disks[0]->provider->mediasize; 616129473Spjd if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) 617129473Spjd mediasize -= sc->sc_disks[0]->provider->sectorsize; 618129473Spjd mediasize -= mediasize % sc->sc_stripesize; 619129473Spjd sectorsize = sc->sc_disks[0]->provider->sectorsize; 620129473Spjd for (no = 1; no < sc->sc_ndisks; no++) { 621129473Spjd ms = sc->sc_disks[no]->provider->mediasize; 622129473Spjd if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) 623129473Spjd ms -= sc->sc_disks[no]->provider->sectorsize; 624129473Spjd ms -= ms % sc->sc_stripesize; 625129473Spjd if (ms < mediasize) 626129473Spjd mediasize = ms; 627129473Spjd sectorsize = lcm(sectorsize, 628129473Spjd sc->sc_disks[no]->provider->sectorsize); 629129473Spjd } 630129473Spjd sc->sc_provider->sectorsize = sectorsize; 631129473Spjd sc->sc_provider->mediasize = mediasize * sc->sc_ndisks; 632129473Spjd g_error_provider(sc->sc_provider, 0); 633129473Spjd 634132664Spjd G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_name); 635129473Spjd} 636129473Spjd 637129473Spjdstatic int 638129473Spjdg_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md) 639129473Spjd{ 640129473Spjd struct g_provider *pp; 641129473Spjd u_char *buf; 642129473Spjd int error; 643129473Spjd 644129473Spjd g_topology_assert(); 645129473Spjd 646129473Spjd error = g_access(cp, 1, 0, 0); 647129473Spjd if (error != 0) 648129473Spjd return (error); 649129473Spjd pp = cp->provider; 650129473Spjd g_topology_unlock(); 651129473Spjd buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 652129473Spjd &error); 653129473Spjd g_topology_lock(); 654129473Spjd g_access(cp, -1, 0, 0); 655129473Spjd if (buf == NULL) 656129473Spjd return (error); 657129473Spjd 658129473Spjd /* Decode metadata. */ 659129473Spjd stripe_metadata_decode(buf, md); 660129473Spjd g_free(buf); 661129473Spjd 662129473Spjd return (0); 663129473Spjd} 664129473Spjd 665129473Spjd/* 666129473Spjd * Add disk to given device. 667129473Spjd */ 668129473Spjdstatic int 669129473Spjdg_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no) 670129473Spjd{ 671129473Spjd struct g_consumer *cp, *fcp; 672129473Spjd struct g_geom *gp; 673129473Spjd int error; 674129473Spjd 675129473Spjd /* Metadata corrupted? */ 676129473Spjd if (no >= sc->sc_ndisks) 677129473Spjd return (EINVAL); 678129473Spjd 679129473Spjd /* Check if disk is not already attached. */ 680129473Spjd if (sc->sc_disks[no] != NULL) 681129473Spjd return (EEXIST); 682129473Spjd 683129473Spjd gp = sc->sc_geom; 684129473Spjd fcp = LIST_FIRST(&gp->consumer); 685129473Spjd 686129473Spjd cp = g_new_consumer(gp); 687129473Spjd error = g_attach(cp, pp); 688129473Spjd if (error != 0) { 689129473Spjd g_destroy_consumer(cp); 690129473Spjd return (error); 691129473Spjd } 692129473Spjd 693129473Spjd if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) { 694129473Spjd error = g_access(cp, fcp->acr, fcp->acw, fcp->ace); 695129473Spjd if (error != 0) { 696129473Spjd g_detach(cp); 697129473Spjd g_destroy_consumer(cp); 698129473Spjd return (error); 699129473Spjd } 700129473Spjd } 701129473Spjd if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) { 702129473Spjd struct g_stripe_metadata md; 703129473Spjd 704129473Spjd /* Reread metadata. */ 705129473Spjd error = g_stripe_read_metadata(cp, &md); 706129473Spjd if (error != 0) 707129473Spjd goto fail; 708129473Spjd 709129473Spjd if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 || 710129473Spjd strcmp(md.md_name, sc->sc_name) != 0 || 711129473Spjd md.md_id != sc->sc_id) { 712129473Spjd G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name); 713129473Spjd goto fail; 714129473Spjd } 715129473Spjd } 716129473Spjd 717129473Spjd cp->private = sc; 718129473Spjd cp->index = no; 719129473Spjd sc->sc_disks[no] = cp; 720129473Spjd 721132664Spjd G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name); 722129473Spjd 723129473Spjd g_stripe_check_and_run(sc); 724129473Spjd 725129473Spjd return (0); 726129473Spjdfail: 727129473Spjd if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) 728129473Spjd g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace); 729129473Spjd g_detach(cp); 730129473Spjd g_destroy_consumer(cp); 731129473Spjd return (error); 732129473Spjd} 733129473Spjd 734129473Spjdstatic struct g_geom * 735129473Spjdg_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md, 736129473Spjd u_int type) 737129473Spjd{ 738129473Spjd struct g_stripe_softc *sc; 739129473Spjd struct g_geom *gp; 740129473Spjd u_int no; 741129473Spjd 742132664Spjd G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name, 743129473Spjd md->md_id); 744129473Spjd 745129473Spjd /* Two disks is minimum. */ 746132664Spjd if (md->md_all < 2) { 747132664Spjd G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name); 748129473Spjd return (NULL); 749129473Spjd } 750129473Spjd#if 0 751129473Spjd /* Stripe size have to be grater than or equal to sector size. */ 752129473Spjd if (md->md_stripesize < sectorsize) { 753132664Spjd G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name); 754129473Spjd return (NULL); 755129473Spjd } 756129473Spjd#endif 757129473Spjd /* Stripe size have to be power of 2. */ 758129473Spjd if (!powerof2(md->md_stripesize)) { 759132664Spjd G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name); 760129473Spjd return (NULL); 761129473Spjd } 762129473Spjd 763129473Spjd /* Check for duplicate unit */ 764129473Spjd LIST_FOREACH(gp, &mp->geom, geom) { 765129473Spjd sc = gp->softc; 766129473Spjd if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) { 767129473Spjd G_STRIPE_DEBUG(0, "Device %s already configured.", 768132664Spjd sc->sc_name); 769129473Spjd return (NULL); 770129473Spjd } 771129473Spjd } 772132664Spjd gp = g_new_geomf(mp, "%s", md->md_name); 773129473Spjd gp->softc = NULL; /* for a moment */ 774129473Spjd 775132662Spjd sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO); 776129473Spjd gp->start = g_stripe_start; 777129473Spjd gp->spoiled = g_stripe_orphan; 778129473Spjd gp->orphan = g_stripe_orphan; 779129473Spjd gp->access = g_stripe_access; 780129473Spjd gp->dumpconf = g_stripe_dumpconf; 781129473Spjd 782129473Spjd sc->sc_id = md->md_id; 783129473Spjd sc->sc_stripesize = md->md_stripesize; 784129473Spjd sc->sc_stripebits = BITCOUNT(sc->sc_stripesize - 1); 785129473Spjd sc->sc_ndisks = md->md_all; 786129473Spjd sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks, 787129473Spjd M_STRIPE, M_WAITOK | M_ZERO); 788129473Spjd for (no = 0; no < sc->sc_ndisks; no++) 789129473Spjd sc->sc_disks[no] = NULL; 790129473Spjd sc->sc_type = type; 791129473Spjd 792129473Spjd gp->softc = sc; 793129473Spjd sc->sc_geom = gp; 794129473Spjd sc->sc_provider = NULL; 795129473Spjd 796132664Spjd G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id); 797129473Spjd 798129473Spjd return (gp); 799129473Spjd} 800129473Spjd 801129473Spjdstatic int 802129473Spjdg_stripe_destroy(struct g_stripe_softc *sc, boolean_t force) 803129473Spjd{ 804129473Spjd struct g_provider *pp; 805129473Spjd struct g_geom *gp; 806129473Spjd u_int no; 807129473Spjd 808129473Spjd g_topology_assert(); 809129473Spjd 810129473Spjd if (sc == NULL) 811129473Spjd return (ENXIO); 812129473Spjd 813129473Spjd pp = sc->sc_provider; 814129473Spjd if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 815129473Spjd if (force) { 816129473Spjd G_STRIPE_DEBUG(0, "Device %s is still open, so it " 817129473Spjd "can't be definitely removed.", pp->name); 818129473Spjd } else { 819129473Spjd G_STRIPE_DEBUG(1, 820129473Spjd "Device %s is still open (r%dw%de%d).", pp->name, 821129473Spjd pp->acr, pp->acw, pp->ace); 822129473Spjd return (EBUSY); 823129473Spjd } 824129473Spjd } 825129473Spjd 826129473Spjd for (no = 0; no < sc->sc_ndisks; no++) { 827129473Spjd if (sc->sc_disks[no] != NULL) 828129473Spjd g_stripe_remove_disk(sc->sc_disks[no]); 829129473Spjd } 830129473Spjd 831129473Spjd gp = sc->sc_geom; 832129473Spjd gp->softc = NULL; 833129473Spjd KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)", 834129473Spjd gp->name)); 835129473Spjd free(sc->sc_disks, M_STRIPE); 836129473Spjd free(sc, M_STRIPE); 837129473Spjd 838129473Spjd pp = LIST_FIRST(&gp->provider); 839129473Spjd if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)) 840129473Spjd G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name); 841129473Spjd 842129473Spjd g_wither_geom(gp, ENXIO); 843129473Spjd 844129473Spjd return (0); 845129473Spjd} 846129473Spjd 847129473Spjdstatic int 848129473Spjdg_stripe_destroy_geom(struct gctl_req *req __unused, 849129473Spjd struct g_class *mp __unused, struct g_geom *gp) 850129473Spjd{ 851129473Spjd struct g_stripe_softc *sc; 852129473Spjd 853129473Spjd sc = gp->softc; 854129473Spjd return (g_stripe_destroy(sc, 0)); 855129473Spjd} 856129473Spjd 857129473Spjdstatic struct g_geom * 858129473Spjdg_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 859129473Spjd{ 860129473Spjd struct g_stripe_metadata md; 861129473Spjd struct g_stripe_softc *sc; 862129473Spjd struct g_consumer *cp; 863129473Spjd struct g_geom *gp; 864129473Spjd int error; 865129473Spjd 866129473Spjd g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 867129473Spjd g_topology_assert(); 868129473Spjd 869129473Spjd G_STRIPE_DEBUG(3, "Tasting %s.", pp->name); 870129473Spjd 871129473Spjd gp = g_new_geomf(mp, "stripe:taste"); 872129473Spjd gp->start = g_stripe_start; 873129473Spjd gp->access = g_stripe_access; 874129473Spjd gp->orphan = g_stripe_orphan; 875129473Spjd cp = g_new_consumer(gp); 876129473Spjd g_attach(cp, pp); 877129473Spjd 878129473Spjd error = g_stripe_read_metadata(cp, &md); 879129473Spjd g_wither_geom(gp, ENXIO); 880129473Spjd if (error != 0) 881129473Spjd return (NULL); 882129473Spjd gp = NULL; 883129473Spjd 884129473Spjd if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0) 885129473Spjd return (NULL); 886129473Spjd if (md.md_version > G_STRIPE_VERSION) { 887129473Spjd printf("geom_stripe.ko module is too old to handle %s.\n", 888129473Spjd pp->name); 889129473Spjd return (NULL); 890129473Spjd } 891129473Spjd 892129473Spjd /* 893129473Spjd * Let's check if device already exists. 894129473Spjd */ 895129473Spjd sc = NULL; 896129473Spjd LIST_FOREACH(gp, &mp->geom, geom) { 897129473Spjd sc = gp->softc; 898129473Spjd if (sc == NULL) 899129473Spjd continue; 900129473Spjd if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC) 901129473Spjd continue; 902129473Spjd if (strcmp(md.md_name, sc->sc_name) != 0) 903129473Spjd continue; 904129473Spjd if (md.md_id != sc->sc_id) 905129473Spjd continue; 906129473Spjd break; 907129473Spjd } 908129473Spjd if (gp != NULL) { 909129473Spjd G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 910129473Spjd error = g_stripe_add_disk(sc, pp, md.md_no); 911129473Spjd if (error != 0) { 912129473Spjd G_STRIPE_DEBUG(0, 913129473Spjd "Cannot add disk %s to %s (error=%d).", pp->name, 914129473Spjd gp->name, error); 915129473Spjd return (NULL); 916129473Spjd } 917129473Spjd } else { 918129473Spjd gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC); 919129473Spjd if (gp == NULL) { 920132664Spjd G_STRIPE_DEBUG(0, "Cannot create device %s.", 921129473Spjd md.md_name); 922129473Spjd return (NULL); 923129473Spjd } 924129473Spjd sc = gp->softc; 925129473Spjd G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 926129473Spjd error = g_stripe_add_disk(sc, pp, md.md_no); 927129473Spjd if (error != 0) { 928129473Spjd G_STRIPE_DEBUG(0, 929129473Spjd "Cannot add disk %s to %s (error=%d).", pp->name, 930129473Spjd gp->name, error); 931129473Spjd g_stripe_destroy(sc, 1); 932129473Spjd return (NULL); 933129473Spjd } 934129473Spjd } 935129473Spjd 936129473Spjd return (gp); 937129473Spjd} 938129473Spjd 939129473Spjdstatic void 940129473Spjdg_stripe_ctl_create(struct gctl_req *req, struct g_class *mp) 941129473Spjd{ 942129473Spjd u_int attached, no; 943129473Spjd struct g_stripe_metadata md; 944129473Spjd struct g_provider *pp; 945129473Spjd struct g_stripe_softc *sc; 946129473Spjd struct g_geom *gp; 947129473Spjd struct sbuf *sb; 948129473Spjd intmax_t *stripesize; 949129473Spjd const char *name; 950129473Spjd char param[16]; 951129473Spjd int *nargs; 952129473Spjd 953129473Spjd g_topology_assert(); 954129473Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 955129473Spjd if (nargs == NULL) { 956129473Spjd gctl_error(req, "No '%s' argument.", "nargs"); 957129473Spjd return; 958129473Spjd } 959129473Spjd if (*nargs <= 2) { 960129473Spjd gctl_error(req, "Too few arguments."); 961129473Spjd return; 962129473Spjd } 963129473Spjd 964129473Spjd strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic)); 965129473Spjd md.md_version = G_STRIPE_VERSION; 966129473Spjd name = gctl_get_asciiparam(req, "arg0"); 967129473Spjd if (name == NULL) { 968129473Spjd gctl_error(req, "No 'arg%u' argument.", 0); 969129473Spjd return; 970129473Spjd } 971129473Spjd strlcpy(md.md_name, name, sizeof(md.md_name)); 972129473Spjd md.md_id = arc4random(); 973129473Spjd md.md_no = 0; 974129473Spjd md.md_all = *nargs - 1; 975129473Spjd stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize)); 976129473Spjd if (stripesize == NULL) { 977129473Spjd gctl_error(req, "No '%s' argument.", "stripesize"); 978129473Spjd return; 979129473Spjd } 980129473Spjd md.md_stripesize = *stripesize; 981129473Spjd 982129473Spjd /* Check all providers are valid */ 983129473Spjd for (no = 1; no < *nargs; no++) { 984129473Spjd snprintf(param, sizeof(param), "arg%u", no); 985129473Spjd name = gctl_get_asciiparam(req, param); 986129473Spjd if (name == NULL) { 987129473Spjd gctl_error(req, "No 'arg%u' argument.", no); 988129473Spjd return; 989129473Spjd } 990129473Spjd if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 991129473Spjd name += strlen("/dev/"); 992129473Spjd pp = g_provider_by_name(name); 993129473Spjd if (pp == NULL) { 994129473Spjd G_STRIPE_DEBUG(1, "Disk %s is invalid.", name); 995129473Spjd gctl_error(req, "Disk %s is invalid.", name); 996129473Spjd return; 997129473Spjd } 998129473Spjd } 999129473Spjd 1000129473Spjd gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL); 1001129473Spjd if (gp == NULL) { 1002132664Spjd gctl_error(req, "Can't configure %s.", md.md_name); 1003129473Spjd return; 1004129473Spjd } 1005129473Spjd 1006129473Spjd sc = gp->softc; 1007129473Spjd sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 1008129473Spjd sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name); 1009129473Spjd for (attached = 0, no = 1; no < *nargs; no++) { 1010129473Spjd snprintf(param, sizeof(param), "arg%u", no); 1011129473Spjd name = gctl_get_asciiparam(req, param); 1012129473Spjd if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 1013129473Spjd name += strlen("/dev/"); 1014129473Spjd pp = g_provider_by_name(name); 1015129473Spjd KASSERT(pp != NULL, ("Provider %s disappear?!", name)); 1016129473Spjd if (g_stripe_add_disk(sc, pp, no - 1) != 0) { 1017129473Spjd G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.", 1018129473Spjd no, pp->name, gp->name); 1019129473Spjd sbuf_printf(sb, " %s", pp->name); 1020129473Spjd continue; 1021129473Spjd } 1022129473Spjd attached++; 1023129473Spjd } 1024129473Spjd sbuf_finish(sb); 1025129473Spjd if (md.md_all != attached) { 1026129473Spjd g_stripe_destroy(gp->softc, 1); 1027129473Spjd gctl_error(req, "%s", sbuf_data(sb)); 1028129473Spjd } 1029129473Spjd sbuf_delete(sb); 1030129473Spjd} 1031129473Spjd 1032129473Spjdstatic struct g_stripe_softc * 1033129473Spjdg_stripe_find_device(struct g_class *mp, const char *name) 1034129473Spjd{ 1035129473Spjd struct g_stripe_softc *sc; 1036129473Spjd struct g_geom *gp; 1037129473Spjd 1038129473Spjd LIST_FOREACH(gp, &mp->geom, geom) { 1039129473Spjd sc = gp->softc; 1040129473Spjd if (sc == NULL) 1041129473Spjd continue; 1042132664Spjd if (strcmp(sc->sc_name, name) == 0) 1043129473Spjd return (sc); 1044129473Spjd } 1045129473Spjd return (NULL); 1046129473Spjd} 1047129473Spjd 1048129473Spjdstatic void 1049129473Spjdg_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp) 1050129473Spjd{ 1051129473Spjd struct g_stripe_softc *sc; 1052129473Spjd int *force, *nargs, error; 1053129473Spjd const char *name; 1054129473Spjd char param[16]; 1055129473Spjd u_int i; 1056129473Spjd 1057129473Spjd g_topology_assert(); 1058129473Spjd 1059129473Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1060129473Spjd if (nargs == NULL) { 1061129473Spjd gctl_error(req, "No '%s' argument.", "nargs"); 1062129473Spjd return; 1063129473Spjd } 1064129473Spjd if (*nargs <= 0) { 1065129473Spjd gctl_error(req, "Missing device(s)."); 1066129473Spjd return; 1067129473Spjd } 1068129473Spjd force = gctl_get_paraml(req, "force", sizeof(*force)); 1069129473Spjd if (force == NULL) { 1070129473Spjd gctl_error(req, "No '%s' argument.", "force"); 1071129473Spjd return; 1072129473Spjd } 1073129473Spjd 1074129473Spjd for (i = 0; i < (u_int)*nargs; i++) { 1075129473Spjd snprintf(param, sizeof(param), "arg%u", i); 1076129473Spjd name = gctl_get_asciiparam(req, param); 1077129473Spjd if (name == NULL) { 1078129473Spjd gctl_error(req, "No 'arg%u' argument.", i); 1079129473Spjd return; 1080129473Spjd } 1081129473Spjd sc = g_stripe_find_device(mp, name); 1082129473Spjd if (sc == NULL) { 1083129473Spjd gctl_error(req, "No such device: %s.", name); 1084129473Spjd return; 1085129473Spjd } 1086129473Spjd error = g_stripe_destroy(sc, *force); 1087129473Spjd if (error != 0) { 1088129473Spjd gctl_error(req, "Cannot destroy device %s (error=%d).", 1089132664Spjd sc->sc_name, error); 1090129473Spjd return; 1091129473Spjd } 1092129473Spjd } 1093129473Spjd} 1094129473Spjd 1095129473Spjdstatic void 1096129473Spjdg_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb) 1097129473Spjd{ 1098129473Spjd uint32_t *version; 1099129473Spjd 1100129473Spjd g_topology_assert(); 1101129473Spjd 1102129473Spjd version = gctl_get_paraml(req, "version", sizeof(*version)); 1103129473Spjd if (version == NULL) { 1104129473Spjd gctl_error(req, "No '%s' argument.", "version"); 1105129473Spjd return; 1106129473Spjd } 1107129473Spjd if (*version != G_STRIPE_VERSION) { 1108129473Spjd gctl_error(req, "Userland and kernel parts are out of sync."); 1109129473Spjd return; 1110129473Spjd } 1111129473Spjd 1112129473Spjd if (strcmp(verb, "create") == 0) { 1113129473Spjd g_stripe_ctl_create(req, mp); 1114129473Spjd return; 1115131649Spjd } else if (strcmp(verb, "destroy") == 0 || 1116131649Spjd strcmp(verb, "stop") == 0) { 1117129473Spjd g_stripe_ctl_destroy(req, mp); 1118129473Spjd return; 1119129473Spjd } 1120129473Spjd 1121129473Spjd gctl_error(req, "Unknown verb."); 1122129473Spjd} 1123129473Spjd 1124129473Spjdstatic void 1125129473Spjdg_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1126129473Spjd struct g_consumer *cp, struct g_provider *pp) 1127129473Spjd{ 1128129473Spjd struct g_stripe_softc *sc; 1129129473Spjd 1130129473Spjd sc = gp->softc; 1131129747Spjd if (sc == NULL || pp == NULL) 1132129473Spjd return; 1133129747Spjd sbuf_printf(sb, "%s<id>%u</id>\n", indent, (u_int)sc->sc_id); 1134129747Spjd sbuf_printf(sb, "%s<stripesize>%u</stripesize>\n", indent, 1135129747Spjd (u_int)sc->sc_stripesize); 1136129747Spjd switch (sc->sc_type) { 1137129747Spjd case G_STRIPE_TYPE_AUTOMATIC: 1138129747Spjd sbuf_printf(sb, "%s<type>%s</type>\n", indent, "automatic"); 1139129747Spjd break; 1140129747Spjd case G_STRIPE_TYPE_MANUAL: 1141129747Spjd sbuf_printf(sb, "%s<type>%s</type>\n", indent, "manual"); 1142129747Spjd break; 1143129747Spjd default: 1144129747Spjd sbuf_printf(sb, "%s<type>%s</type>\n", indent, "unknown"); 1145129747Spjd break; 1146129473Spjd } 1147129747Spjd sbuf_printf(sb, "%s<providers>", indent); 1148129747Spjd LIST_FOREACH(cp, &gp->consumer, consumer) { 1149129747Spjd if (cp->provider == NULL) 1150129747Spjd continue; 1151129747Spjd sbuf_printf(sb, "%s", cp->provider->name); 1152129747Spjd if (LIST_NEXT(cp, consumer) != NULL) 1153129747Spjd sbuf_printf(sb, " "); 1154129747Spjd } 1155129747Spjd sbuf_printf(sb, "</providers>\n"); 1156129747Spjd sbuf_printf(sb, "%s<status>total=%u, online=%u</status>\n", indent, 1157129747Spjd sc->sc_ndisks, g_stripe_nvalid(sc)); 1158129747Spjd if (pp->error == 0) 1159129747Spjd sbuf_printf(sb, "%s<state>UP</state>\n", indent); 1160129747Spjd else 1161129747Spjd sbuf_printf(sb, "%s<state>DOWN</state>\n", indent); 1162129473Spjd} 1163129473Spjd 1164129473SpjdDECLARE_GEOM_CLASS(g_stripe_class, g_stripe); 1165