g_stripe.c revision 264313
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 264313 2014-04-10 10:12:56Z 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) { 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; 287133204Spjd if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) { 288133204Spjd g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset, 289131878Spjd bp->bio_length, 1); 290133204Spjd bp->bio_data = bp->bio_caller1; 291133204Spjd bp->bio_caller1 = NULL; 292131878Spjd } 293256880Smav mtx_lock(&sc->sc_lock); 294256880Smav if (pbp->bio_error == 0) 295256880Smav pbp->bio_error = bp->bio_error; 296256880Smav pbp->bio_completed += bp->bio_completed; 297131878Spjd pbp->bio_inbed++; 298131878Spjd if (pbp->bio_children == pbp->bio_inbed) { 299256880Smav mtx_unlock(&sc->sc_lock); 300133204Spjd if (pbp->bio_driver1 != NULL) 301133204Spjd uma_zfree(g_stripe_zone, pbp->bio_driver1); 302131878Spjd g_io_deliver(pbp, pbp->bio_error); 303256880Smav } else 304256880Smav mtx_unlock(&sc->sc_lock); 305256880Smav g_destroy_bio(bp); 306131878Spjd} 307131878Spjd 308131878Spjdstatic int 309131878Spjdg_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length) 310131878Spjd{ 311131878Spjd TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 312131878Spjd u_int nparts = 0, stripesize; 313131878Spjd struct g_stripe_softc *sc; 314131878Spjd char *addr, *data = NULL; 315129473Spjd struct bio *cbp; 316131878Spjd int error; 317131878Spjd 318131878Spjd sc = bp->bio_to->geom->softc; 319131878Spjd 320131878Spjd addr = bp->bio_data; 321131878Spjd stripesize = sc->sc_stripesize; 322131878Spjd 323131878Spjd cbp = g_clone_bio(bp); 324131878Spjd if (cbp == NULL) { 325131878Spjd error = ENOMEM; 326131878Spjd goto failure; 327131878Spjd } 328131878Spjd TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 329131878Spjd nparts++; 330131878Spjd /* 331131878Spjd * Fill in the component buf structure. 332131878Spjd */ 333131878Spjd cbp->bio_done = g_stripe_done; 334131878Spjd cbp->bio_offset = offset; 335131878Spjd cbp->bio_data = addr; 336133204Spjd cbp->bio_caller1 = NULL; 337131878Spjd cbp->bio_length = length; 338133204Spjd cbp->bio_caller2 = sc->sc_disks[no]; 339131878Spjd 340131878Spjd /* offset -= offset % stripesize; */ 341131878Spjd offset -= offset & (stripesize - 1); 342131878Spjd addr += length; 343131878Spjd length = bp->bio_length - length; 344131878Spjd for (no++; length > 0; no++, length -= stripesize, addr += stripesize) { 345131878Spjd if (no > sc->sc_ndisks - 1) { 346131878Spjd no = 0; 347131878Spjd offset += stripesize; 348131878Spjd } 349131878Spjd if (nparts >= sc->sc_ndisks) { 350131878Spjd cbp = TAILQ_NEXT(cbp, bio_queue); 351131878Spjd if (cbp == NULL) 352131878Spjd cbp = TAILQ_FIRST(&queue); 353131878Spjd nparts++; 354131878Spjd /* 355131878Spjd * Update bio structure. 356131878Spjd */ 357131878Spjd /* 358131878Spjd * MIN() is in case when 359131878Spjd * (bp->bio_length % sc->sc_stripesize) != 0. 360131878Spjd */ 361131878Spjd cbp->bio_length += MIN(stripesize, length); 362133204Spjd if (cbp->bio_caller1 == NULL) { 363133204Spjd cbp->bio_caller1 = cbp->bio_data; 364131878Spjd cbp->bio_data = NULL; 365131878Spjd if (data == NULL) { 366131878Spjd data = uma_zalloc(g_stripe_zone, 367131878Spjd M_NOWAIT); 368131878Spjd if (data == NULL) { 369131878Spjd error = ENOMEM; 370131878Spjd goto failure; 371131878Spjd } 372131878Spjd } 373131878Spjd } 374131878Spjd } else { 375131878Spjd cbp = g_clone_bio(bp); 376131878Spjd if (cbp == NULL) { 377131878Spjd error = ENOMEM; 378131878Spjd goto failure; 379131878Spjd } 380131878Spjd TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 381131878Spjd nparts++; 382131878Spjd /* 383131878Spjd * Fill in the component buf structure. 384131878Spjd */ 385131878Spjd cbp->bio_done = g_stripe_done; 386131878Spjd cbp->bio_offset = offset; 387131878Spjd cbp->bio_data = addr; 388133204Spjd cbp->bio_caller1 = NULL; 389131878Spjd /* 390131878Spjd * MIN() is in case when 391131878Spjd * (bp->bio_length % sc->sc_stripesize) != 0. 392131878Spjd */ 393131878Spjd cbp->bio_length = MIN(stripesize, length); 394133204Spjd cbp->bio_caller2 = sc->sc_disks[no]; 395131878Spjd } 396131878Spjd } 397131878Spjd if (data != NULL) 398133444Spjd bp->bio_driver1 = data; 399131878Spjd /* 400131878Spjd * Fire off all allocated requests! 401131878Spjd */ 402131878Spjd while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 403131878Spjd struct g_consumer *cp; 404131878Spjd 405131878Spjd TAILQ_REMOVE(&queue, cbp, bio_queue); 406133204Spjd cp = cbp->bio_caller2; 407133204Spjd cbp->bio_caller2 = NULL; 408131878Spjd cbp->bio_to = cp->provider; 409133204Spjd if (cbp->bio_caller1 != NULL) { 410131878Spjd cbp->bio_data = data; 411131878Spjd if (bp->bio_cmd == BIO_WRITE) { 412133204Spjd g_stripe_copy(sc, cbp->bio_caller1, data, 413131878Spjd cbp->bio_offset, cbp->bio_length, 0); 414131878Spjd } 415131878Spjd data += cbp->bio_length; 416131878Spjd } 417131878Spjd G_STRIPE_LOGREQ(cbp, "Sending request."); 418131878Spjd g_io_request(cbp, cp); 419131878Spjd } 420131878Spjd return (0); 421131878Spjdfailure: 422131878Spjd if (data != NULL) 423131878Spjd uma_zfree(g_stripe_zone, data); 424131878Spjd while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 425131878Spjd TAILQ_REMOVE(&queue, cbp, bio_queue); 426133204Spjd if (cbp->bio_caller1 != NULL) { 427133204Spjd cbp->bio_data = cbp->bio_caller1; 428133204Spjd cbp->bio_caller1 = NULL; 429131878Spjd } 430133201Spjd bp->bio_children--; 431131878Spjd g_destroy_bio(cbp); 432131878Spjd } 433131878Spjd return (error); 434131878Spjd} 435131878Spjd 436131878Spjdstatic int 437131878Spjdg_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length) 438131878Spjd{ 439131878Spjd TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 440131878Spjd struct g_stripe_softc *sc; 441129473Spjd uint32_t stripesize; 442131878Spjd struct bio *cbp; 443129473Spjd char *addr; 444131878Spjd int error; 445129473Spjd 446131878Spjd sc = bp->bio_to->geom->softc; 447131878Spjd 448131878Spjd stripesize = sc->sc_stripesize; 449131878Spjd 450131878Spjd cbp = g_clone_bio(bp); 451131878Spjd if (cbp == NULL) { 452131878Spjd error = ENOMEM; 453131878Spjd goto failure; 454131878Spjd } 455131878Spjd TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 456129473Spjd /* 457131878Spjd * Fill in the component buf structure. 458131878Spjd */ 459256880Smav if (bp->bio_length == length) 460256880Smav cbp->bio_done = g_std_done; /* Optimized lockless case. */ 461256880Smav else 462256880Smav cbp->bio_done = g_stripe_done; 463131878Spjd cbp->bio_offset = offset; 464131878Spjd cbp->bio_length = length; 465256880Smav if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 466256880Smav bp->bio_ma_n = round_page(bp->bio_ma_offset + 467256880Smav bp->bio_length) / PAGE_SIZE; 468256880Smav addr = NULL; 469256880Smav } else 470256880Smav addr = bp->bio_data; 471133204Spjd cbp->bio_caller2 = sc->sc_disks[no]; 472131878Spjd 473131878Spjd /* offset -= offset % stripesize; */ 474131878Spjd offset -= offset & (stripesize - 1); 475264313Smav if (bp->bio_cmd != BIO_DELETE) 476264313Smav addr += length; 477131878Spjd length = bp->bio_length - length; 478264313Smav for (no++; length > 0; no++, length -= stripesize) { 479131878Spjd if (no > sc->sc_ndisks - 1) { 480131878Spjd no = 0; 481131878Spjd offset += stripesize; 482131878Spjd } 483131878Spjd cbp = g_clone_bio(bp); 484131878Spjd if (cbp == NULL) { 485131878Spjd error = ENOMEM; 486131878Spjd goto failure; 487131878Spjd } 488131878Spjd TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 489131878Spjd 490131878Spjd /* 491131878Spjd * Fill in the component buf structure. 492131878Spjd */ 493256880Smav cbp->bio_done = g_stripe_done; 494131878Spjd cbp->bio_offset = offset; 495131878Spjd /* 496131878Spjd * MIN() is in case when 497131878Spjd * (bp->bio_length % sc->sc_stripesize) != 0. 498131878Spjd */ 499131878Spjd cbp->bio_length = MIN(stripesize, length); 500256880Smav if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 501256880Smav cbp->bio_ma_offset += (uintptr_t)addr; 502256880Smav cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE; 503256880Smav cbp->bio_ma_offset %= PAGE_SIZE; 504256880Smav cbp->bio_ma_n = round_page(cbp->bio_ma_offset + 505256880Smav cbp->bio_length) / PAGE_SIZE; 506256880Smav } else 507256880Smav cbp->bio_data = addr; 508131878Spjd 509133204Spjd cbp->bio_caller2 = sc->sc_disks[no]; 510264313Smav 511264313Smav if (bp->bio_cmd != BIO_DELETE) 512264313Smav addr += stripesize; 513131878Spjd } 514131878Spjd /* 515131878Spjd * Fire off all allocated requests! 516131878Spjd */ 517131878Spjd while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 518131878Spjd struct g_consumer *cp; 519131878Spjd 520131878Spjd TAILQ_REMOVE(&queue, cbp, bio_queue); 521133204Spjd cp = cbp->bio_caller2; 522133204Spjd cbp->bio_caller2 = NULL; 523131878Spjd cbp->bio_to = cp->provider; 524131878Spjd G_STRIPE_LOGREQ(cbp, "Sending request."); 525131878Spjd g_io_request(cbp, cp); 526131878Spjd } 527131878Spjd return (0); 528131878Spjdfailure: 529131878Spjd while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 530131878Spjd TAILQ_REMOVE(&queue, cbp, bio_queue); 531133201Spjd bp->bio_children--; 532131878Spjd g_destroy_bio(cbp); 533131878Spjd } 534131878Spjd return (error); 535131878Spjd} 536131878Spjd 537131878Spjdstatic void 538163836Spjdg_stripe_flush(struct g_stripe_softc *sc, struct bio *bp) 539163836Spjd{ 540163836Spjd struct bio_queue_head queue; 541163836Spjd struct g_consumer *cp; 542163836Spjd struct bio *cbp; 543163836Spjd u_int no; 544163836Spjd 545163836Spjd bioq_init(&queue); 546163836Spjd for (no = 0; no < sc->sc_ndisks; no++) { 547163836Spjd cbp = g_clone_bio(bp); 548163836Spjd if (cbp == NULL) { 549163836Spjd for (cbp = bioq_first(&queue); cbp != NULL; 550163836Spjd cbp = bioq_first(&queue)) { 551163836Spjd bioq_remove(&queue, cbp); 552163836Spjd g_destroy_bio(cbp); 553163836Spjd } 554163836Spjd if (bp->bio_error == 0) 555163836Spjd bp->bio_error = ENOMEM; 556163836Spjd g_io_deliver(bp, bp->bio_error); 557163836Spjd return; 558163836Spjd } 559163836Spjd bioq_insert_tail(&queue, cbp); 560256880Smav cbp->bio_done = g_stripe_done; 561256880Smav cbp->bio_caller2 = sc->sc_disks[no]; 562163836Spjd cbp->bio_to = sc->sc_disks[no]->provider; 563163836Spjd } 564163836Spjd for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) { 565163836Spjd bioq_remove(&queue, cbp); 566163836Spjd G_STRIPE_LOGREQ(cbp, "Sending request."); 567256880Smav cp = cbp->bio_caller2; 568256880Smav cbp->bio_caller2 = NULL; 569163836Spjd g_io_request(cbp, cp); 570163836Spjd } 571163836Spjd} 572163836Spjd 573163836Spjdstatic void 574131878Spjdg_stripe_start(struct bio *bp) 575131878Spjd{ 576131878Spjd off_t offset, start, length, nstripe; 577131878Spjd struct g_stripe_softc *sc; 578131878Spjd u_int no, stripesize; 579131878Spjd int error, fast = 0; 580131878Spjd 581131878Spjd sc = bp->bio_to->geom->softc; 582131878Spjd /* 583129473Spjd * If sc == NULL, provider's error should be set and g_stripe_start() 584129473Spjd * should not be called at all. 585129473Spjd */ 586129473Spjd KASSERT(sc != NULL, 587129473Spjd ("Provider's error should be set (error=%d)(device=%s).", 588129473Spjd bp->bio_to->error, bp->bio_to->name)); 589129473Spjd 590129473Spjd G_STRIPE_LOGREQ(bp, "Request received."); 591129473Spjd 592129473Spjd switch (bp->bio_cmd) { 593129473Spjd case BIO_READ: 594129473Spjd case BIO_WRITE: 595129473Spjd case BIO_DELETE: 596129473Spjd break; 597163886Spjd case BIO_FLUSH: 598163886Spjd g_stripe_flush(sc, bp); 599163886Spjd return; 600129473Spjd case BIO_GETATTR: 601129473Spjd /* To which provider it should be delivered? */ 602129473Spjd default: 603129473Spjd g_io_deliver(bp, EOPNOTSUPP); 604129473Spjd return; 605129473Spjd } 606129473Spjd 607129473Spjd stripesize = sc->sc_stripesize; 608129473Spjd 609129473Spjd /* 610131878Spjd * Calculations are quite messy, but fast I hope. 611129473Spjd */ 612129473Spjd 613129473Spjd /* Stripe number. */ 614129473Spjd /* nstripe = bp->bio_offset / stripesize; */ 615129473Spjd nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits; 616129473Spjd /* Disk number. */ 617129473Spjd no = nstripe % sc->sc_ndisks; 618129473Spjd /* Start position in stripe. */ 619129473Spjd /* start = bp->bio_offset % stripesize; */ 620129473Spjd start = bp->bio_offset & (stripesize - 1); 621129473Spjd /* Start position in disk. */ 622131878Spjd /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */ 623131878Spjd offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start; 624129473Spjd /* Length of data to operate. */ 625129473Spjd length = MIN(bp->bio_length, stripesize - start); 626129473Spjd 627131878Spjd /* 628131878Spjd * Do use "fast" mode when: 629131878Spjd * 1. "Fast" mode is ON. 630131878Spjd * and 631196837Smav * 2. Request size is less than or equal to MAXPHYS, 632131878Spjd * which should always be true. 633131878Spjd * and 634131878Spjd * 3. Request size is bigger than stripesize * ndisks. If it isn't, 635131878Spjd * there will be no need to send more than one I/O request to 636131878Spjd * a provider, so there is nothing to optmize. 637256880Smav * and 638256880Smav * 4. Request is not unmapped. 639264313Smav * and 640264313Smav * 5. It is not a BIO_DELETE. 641131878Spjd */ 642196837Smav if (g_stripe_fast && bp->bio_length <= MAXPHYS && 643256880Smav bp->bio_length >= stripesize * sc->sc_ndisks && 644264313Smav (bp->bio_flags & BIO_UNMAPPED) == 0 && 645264313Smav bp->bio_cmd != BIO_DELETE) { 646131878Spjd fast = 1; 647129473Spjd } 648131878Spjd error = 0; 649133205Spjd if (fast) { 650131878Spjd error = g_stripe_start_fast(bp, no, offset, length); 651133205Spjd if (error != 0) 652133205Spjd g_stripe_fast_failed++; 653133205Spjd } 654129473Spjd /* 655131878Spjd * Do use "economic" when: 656131878Spjd * 1. "Economic" mode is ON. 657131878Spjd * or 658204070Spjd * 2. "Fast" mode failed. It can only fail if there is no memory. 659129473Spjd */ 660131878Spjd if (!fast || error != 0) 661131878Spjd error = g_stripe_start_economic(bp, no, offset, length); 662131878Spjd if (error != 0) { 663131878Spjd if (bp->bio_error == 0) 664131878Spjd bp->bio_error = error; 665131878Spjd g_io_deliver(bp, bp->bio_error); 666129473Spjd } 667129473Spjd} 668129473Spjd 669129473Spjdstatic void 670129473Spjdg_stripe_check_and_run(struct g_stripe_softc *sc) 671129473Spjd{ 672256880Smav struct g_provider *dp; 673129473Spjd off_t mediasize, ms; 674129473Spjd u_int no, sectorsize = 0; 675129473Spjd 676226998Smav g_topology_assert(); 677129473Spjd if (g_stripe_nvalid(sc) != sc->sc_ndisks) 678129473Spjd return; 679129473Spjd 680132664Spjd sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s", 681132664Spjd sc->sc_name); 682256880Smav sc->sc_provider->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 683256880Smav if (g_stripe_fast == 0) 684256880Smav sc->sc_provider->flags |= G_PF_ACCEPT_UNMAPPED; 685129473Spjd /* 686129473Spjd * Find the smallest disk. 687129473Spjd */ 688129473Spjd mediasize = sc->sc_disks[0]->provider->mediasize; 689129473Spjd if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) 690129473Spjd mediasize -= sc->sc_disks[0]->provider->sectorsize; 691129473Spjd mediasize -= mediasize % sc->sc_stripesize; 692129473Spjd sectorsize = sc->sc_disks[0]->provider->sectorsize; 693129473Spjd for (no = 1; no < sc->sc_ndisks; no++) { 694256880Smav dp = sc->sc_disks[no]->provider; 695256880Smav ms = dp->mediasize; 696129473Spjd if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) 697256880Smav ms -= dp->sectorsize; 698129473Spjd ms -= ms % sc->sc_stripesize; 699129473Spjd if (ms < mediasize) 700129473Spjd mediasize = ms; 701256880Smav sectorsize = lcm(sectorsize, dp->sectorsize); 702256880Smav 703256880Smav /* A provider underneath us doesn't support unmapped */ 704256880Smav if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) { 705256880Smav G_STRIPE_DEBUG(1, "Cancelling unmapped " 706256880Smav "because of %s.", dp->name); 707256880Smav sc->sc_provider->flags &= ~G_PF_ACCEPT_UNMAPPED; 708256880Smav } 709129473Spjd } 710129473Spjd sc->sc_provider->sectorsize = sectorsize; 711129473Spjd sc->sc_provider->mediasize = mediasize * sc->sc_ndisks; 712200933Smav sc->sc_provider->stripesize = sc->sc_stripesize; 713200933Smav sc->sc_provider->stripeoffset = 0; 714129473Spjd g_error_provider(sc->sc_provider, 0); 715129473Spjd 716226998Smav G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_provider->name); 717129473Spjd} 718129473Spjd 719129473Spjdstatic int 720129473Spjdg_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md) 721129473Spjd{ 722129473Spjd struct g_provider *pp; 723129473Spjd u_char *buf; 724129473Spjd int error; 725129473Spjd 726129473Spjd g_topology_assert(); 727129473Spjd 728129473Spjd error = g_access(cp, 1, 0, 0); 729129473Spjd if (error != 0) 730129473Spjd return (error); 731129473Spjd pp = cp->provider; 732129473Spjd g_topology_unlock(); 733129473Spjd buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 734129473Spjd &error); 735129473Spjd g_topology_lock(); 736129473Spjd g_access(cp, -1, 0, 0); 737129473Spjd if (buf == NULL) 738129473Spjd return (error); 739129473Spjd 740129473Spjd /* Decode metadata. */ 741129473Spjd stripe_metadata_decode(buf, md); 742129473Spjd g_free(buf); 743129473Spjd 744129473Spjd return (0); 745129473Spjd} 746129473Spjd 747129473Spjd/* 748129473Spjd * Add disk to given device. 749129473Spjd */ 750129473Spjdstatic int 751129473Spjdg_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no) 752129473Spjd{ 753129473Spjd struct g_consumer *cp, *fcp; 754129473Spjd struct g_geom *gp; 755129473Spjd int error; 756129473Spjd 757226998Smav g_topology_assert(); 758129473Spjd /* Metadata corrupted? */ 759129473Spjd if (no >= sc->sc_ndisks) 760129473Spjd return (EINVAL); 761129473Spjd 762129473Spjd /* Check if disk is not already attached. */ 763129473Spjd if (sc->sc_disks[no] != NULL) 764129473Spjd return (EEXIST); 765129473Spjd 766129473Spjd gp = sc->sc_geom; 767129473Spjd fcp = LIST_FIRST(&gp->consumer); 768129473Spjd 769129473Spjd cp = g_new_consumer(gp); 770256880Smav cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 771226998Smav cp->private = NULL; 772226998Smav cp->index = no; 773129473Spjd error = g_attach(cp, pp); 774129473Spjd if (error != 0) { 775129473Spjd g_destroy_consumer(cp); 776129473Spjd return (error); 777129473Spjd } 778129473Spjd 779129473Spjd if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) { 780129473Spjd error = g_access(cp, fcp->acr, fcp->acw, fcp->ace); 781129473Spjd if (error != 0) { 782129473Spjd g_detach(cp); 783129473Spjd g_destroy_consumer(cp); 784129473Spjd return (error); 785129473Spjd } 786129473Spjd } 787129473Spjd if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) { 788129473Spjd struct g_stripe_metadata md; 789129473Spjd 790129473Spjd /* Reread metadata. */ 791129473Spjd error = g_stripe_read_metadata(cp, &md); 792129473Spjd if (error != 0) 793129473Spjd goto fail; 794129473Spjd 795129473Spjd if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 || 796129473Spjd strcmp(md.md_name, sc->sc_name) != 0 || 797129473Spjd md.md_id != sc->sc_id) { 798129473Spjd G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name); 799129473Spjd goto fail; 800129473Spjd } 801129473Spjd } 802129473Spjd 803129473Spjd sc->sc_disks[no] = cp; 804132664Spjd G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name); 805129473Spjd g_stripe_check_and_run(sc); 806129473Spjd 807129473Spjd return (0); 808129473Spjdfail: 809129473Spjd if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) 810129473Spjd g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace); 811129473Spjd g_detach(cp); 812129473Spjd g_destroy_consumer(cp); 813129473Spjd return (error); 814129473Spjd} 815129473Spjd 816129473Spjdstatic struct g_geom * 817129473Spjdg_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md, 818129473Spjd u_int type) 819129473Spjd{ 820129473Spjd struct g_stripe_softc *sc; 821129473Spjd struct g_geom *gp; 822129473Spjd u_int no; 823129473Spjd 824226998Smav g_topology_assert(); 825132664Spjd G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name, 826129473Spjd md->md_id); 827129473Spjd 828129473Spjd /* Two disks is minimum. */ 829132664Spjd if (md->md_all < 2) { 830132664Spjd G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name); 831129473Spjd return (NULL); 832129473Spjd } 833129473Spjd#if 0 834129473Spjd /* Stripe size have to be grater than or equal to sector size. */ 835129473Spjd if (md->md_stripesize < sectorsize) { 836132664Spjd G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name); 837129473Spjd return (NULL); 838129473Spjd } 839129473Spjd#endif 840129473Spjd /* Stripe size have to be power of 2. */ 841129473Spjd if (!powerof2(md->md_stripesize)) { 842132664Spjd G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name); 843129473Spjd return (NULL); 844129473Spjd } 845129473Spjd 846129473Spjd /* Check for duplicate unit */ 847129473Spjd LIST_FOREACH(gp, &mp->geom, geom) { 848129473Spjd sc = gp->softc; 849129473Spjd if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) { 850129473Spjd G_STRIPE_DEBUG(0, "Device %s already configured.", 851132664Spjd sc->sc_name); 852129473Spjd return (NULL); 853129473Spjd } 854129473Spjd } 855132664Spjd gp = g_new_geomf(mp, "%s", md->md_name); 856132662Spjd sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO); 857129473Spjd gp->start = g_stripe_start; 858129473Spjd gp->spoiled = g_stripe_orphan; 859129473Spjd gp->orphan = g_stripe_orphan; 860129473Spjd gp->access = g_stripe_access; 861129473Spjd gp->dumpconf = g_stripe_dumpconf; 862129473Spjd 863129473Spjd sc->sc_id = md->md_id; 864129473Spjd sc->sc_stripesize = md->md_stripesize; 865149300Spjd sc->sc_stripebits = bitcount32(sc->sc_stripesize - 1); 866129473Spjd sc->sc_ndisks = md->md_all; 867129473Spjd sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks, 868129473Spjd M_STRIPE, M_WAITOK | M_ZERO); 869129473Spjd for (no = 0; no < sc->sc_ndisks; no++) 870129473Spjd sc->sc_disks[no] = NULL; 871129473Spjd sc->sc_type = type; 872256880Smav mtx_init(&sc->sc_lock, "gstripe lock", NULL, MTX_DEF); 873129473Spjd 874129473Spjd gp->softc = sc; 875129473Spjd sc->sc_geom = gp; 876129473Spjd sc->sc_provider = NULL; 877129473Spjd 878132664Spjd G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id); 879129473Spjd 880129473Spjd return (gp); 881129473Spjd} 882129473Spjd 883129473Spjdstatic int 884129473Spjdg_stripe_destroy(struct g_stripe_softc *sc, boolean_t force) 885129473Spjd{ 886129473Spjd struct g_provider *pp; 887226998Smav struct g_consumer *cp, *cp1; 888129473Spjd struct g_geom *gp; 889129473Spjd 890129473Spjd g_topology_assert(); 891129473Spjd 892129473Spjd if (sc == NULL) 893129473Spjd return (ENXIO); 894129473Spjd 895129473Spjd pp = sc->sc_provider; 896129473Spjd if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 897129473Spjd if (force) { 898129473Spjd G_STRIPE_DEBUG(0, "Device %s is still open, so it " 899129473Spjd "can't be definitely removed.", pp->name); 900129473Spjd } else { 901129473Spjd G_STRIPE_DEBUG(1, 902129473Spjd "Device %s is still open (r%dw%de%d).", pp->name, 903129473Spjd pp->acr, pp->acw, pp->ace); 904129473Spjd return (EBUSY); 905129473Spjd } 906129473Spjd } 907129473Spjd 908226998Smav gp = sc->sc_geom; 909226998Smav LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) { 910226998Smav g_stripe_remove_disk(cp); 911226998Smav if (cp1 == NULL) 912226998Smav return (0); /* Recursion happened. */ 913129473Spjd } 914226998Smav if (!LIST_EMPTY(&gp->consumer)) 915226998Smav return (EINPROGRESS); 916129473Spjd 917129473Spjd gp->softc = NULL; 918129473Spjd KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)", 919129473Spjd gp->name)); 920129473Spjd free(sc->sc_disks, M_STRIPE); 921256880Smav mtx_destroy(&sc->sc_lock); 922129473Spjd free(sc, M_STRIPE); 923226998Smav G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name); 924129473Spjd g_wither_geom(gp, ENXIO); 925129473Spjd return (0); 926129473Spjd} 927129473Spjd 928129473Spjdstatic int 929129473Spjdg_stripe_destroy_geom(struct gctl_req *req __unused, 930129473Spjd struct g_class *mp __unused, struct g_geom *gp) 931129473Spjd{ 932129473Spjd struct g_stripe_softc *sc; 933129473Spjd 934129473Spjd sc = gp->softc; 935129473Spjd return (g_stripe_destroy(sc, 0)); 936129473Spjd} 937129473Spjd 938129473Spjdstatic struct g_geom * 939129473Spjdg_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 940129473Spjd{ 941129473Spjd struct g_stripe_metadata md; 942129473Spjd struct g_stripe_softc *sc; 943129473Spjd struct g_consumer *cp; 944129473Spjd struct g_geom *gp; 945129473Spjd int error; 946129473Spjd 947129473Spjd g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 948129473Spjd g_topology_assert(); 949129473Spjd 950197898Spjd /* Skip providers that are already open for writing. */ 951197898Spjd if (pp->acw > 0) 952197898Spjd return (NULL); 953197898Spjd 954129473Spjd G_STRIPE_DEBUG(3, "Tasting %s.", pp->name); 955129473Spjd 956129473Spjd gp = g_new_geomf(mp, "stripe:taste"); 957129473Spjd gp->start = g_stripe_start; 958129473Spjd gp->access = g_stripe_access; 959129473Spjd gp->orphan = g_stripe_orphan; 960129473Spjd cp = g_new_consumer(gp); 961129473Spjd g_attach(cp, pp); 962129473Spjd error = g_stripe_read_metadata(cp, &md); 963133371Spjd g_detach(cp); 964133371Spjd g_destroy_consumer(cp); 965133371Spjd g_destroy_geom(gp); 966129473Spjd if (error != 0) 967129473Spjd return (NULL); 968129473Spjd gp = NULL; 969129473Spjd 970129473Spjd if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0) 971129473Spjd return (NULL); 972129473Spjd if (md.md_version > G_STRIPE_VERSION) { 973129473Spjd printf("geom_stripe.ko module is too old to handle %s.\n", 974129473Spjd pp->name); 975129473Spjd return (NULL); 976129473Spjd } 977133373Spjd /* 978133373Spjd * Backward compatibility: 979133373Spjd */ 980142727Spjd /* There was no md_provider field in earlier versions of metadata. */ 981133373Spjd if (md.md_version < 2) 982133373Spjd bzero(md.md_provider, sizeof(md.md_provider)); 983142727Spjd /* There was no md_provsize field in earlier versions of metadata. */ 984142727Spjd if (md.md_version < 3) 985142727Spjd md.md_provsize = pp->mediasize; 986129473Spjd 987221101Smav if (md.md_provider[0] != '\0' && 988221101Smav !g_compare_names(md.md_provider, pp->name)) 989133373Spjd return (NULL); 990142727Spjd if (md.md_provsize != pp->mediasize) 991142727Spjd return (NULL); 992133373Spjd 993129473Spjd /* 994129473Spjd * Let's check if device already exists. 995129473Spjd */ 996129473Spjd sc = NULL; 997129473Spjd LIST_FOREACH(gp, &mp->geom, geom) { 998129473Spjd sc = gp->softc; 999129473Spjd if (sc == NULL) 1000129473Spjd continue; 1001129473Spjd if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC) 1002129473Spjd continue; 1003129473Spjd if (strcmp(md.md_name, sc->sc_name) != 0) 1004129473Spjd continue; 1005129473Spjd if (md.md_id != sc->sc_id) 1006129473Spjd continue; 1007129473Spjd break; 1008129473Spjd } 1009129473Spjd if (gp != NULL) { 1010129473Spjd G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 1011129473Spjd error = g_stripe_add_disk(sc, pp, md.md_no); 1012129473Spjd if (error != 0) { 1013129473Spjd G_STRIPE_DEBUG(0, 1014129473Spjd "Cannot add disk %s to %s (error=%d).", pp->name, 1015129473Spjd gp->name, error); 1016129473Spjd return (NULL); 1017129473Spjd } 1018129473Spjd } else { 1019129473Spjd gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC); 1020129473Spjd if (gp == NULL) { 1021132664Spjd G_STRIPE_DEBUG(0, "Cannot create device %s.", 1022129473Spjd md.md_name); 1023129473Spjd return (NULL); 1024129473Spjd } 1025129473Spjd sc = gp->softc; 1026129473Spjd G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 1027129473Spjd error = g_stripe_add_disk(sc, pp, md.md_no); 1028129473Spjd if (error != 0) { 1029129473Spjd G_STRIPE_DEBUG(0, 1030129473Spjd "Cannot add disk %s to %s (error=%d).", pp->name, 1031129473Spjd gp->name, error); 1032129473Spjd g_stripe_destroy(sc, 1); 1033129473Spjd return (NULL); 1034129473Spjd } 1035129473Spjd } 1036129473Spjd 1037129473Spjd return (gp); 1038129473Spjd} 1039129473Spjd 1040129473Spjdstatic void 1041129473Spjdg_stripe_ctl_create(struct gctl_req *req, struct g_class *mp) 1042129473Spjd{ 1043129473Spjd u_int attached, no; 1044129473Spjd struct g_stripe_metadata md; 1045129473Spjd struct g_provider *pp; 1046129473Spjd struct g_stripe_softc *sc; 1047129473Spjd struct g_geom *gp; 1048129473Spjd struct sbuf *sb; 1049129473Spjd intmax_t *stripesize; 1050129473Spjd const char *name; 1051129473Spjd char param[16]; 1052129473Spjd int *nargs; 1053129473Spjd 1054129473Spjd g_topology_assert(); 1055129473Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1056129473Spjd if (nargs == NULL) { 1057129473Spjd gctl_error(req, "No '%s' argument.", "nargs"); 1058129473Spjd return; 1059129473Spjd } 1060129473Spjd if (*nargs <= 2) { 1061129473Spjd gctl_error(req, "Too few arguments."); 1062129473Spjd return; 1063129473Spjd } 1064129473Spjd 1065129473Spjd strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic)); 1066129473Spjd md.md_version = G_STRIPE_VERSION; 1067129473Spjd name = gctl_get_asciiparam(req, "arg0"); 1068129473Spjd if (name == NULL) { 1069129473Spjd gctl_error(req, "No 'arg%u' argument.", 0); 1070129473Spjd return; 1071129473Spjd } 1072129473Spjd strlcpy(md.md_name, name, sizeof(md.md_name)); 1073129473Spjd md.md_id = arc4random(); 1074129473Spjd md.md_no = 0; 1075129473Spjd md.md_all = *nargs - 1; 1076129473Spjd stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize)); 1077129473Spjd if (stripesize == NULL) { 1078129473Spjd gctl_error(req, "No '%s' argument.", "stripesize"); 1079129473Spjd return; 1080129473Spjd } 1081129473Spjd md.md_stripesize = *stripesize; 1082133373Spjd bzero(md.md_provider, sizeof(md.md_provider)); 1083142727Spjd /* This field is not important here. */ 1084142727Spjd md.md_provsize = 0; 1085129473Spjd 1086129473Spjd /* Check all providers are valid */ 1087129473Spjd for (no = 1; no < *nargs; no++) { 1088129473Spjd snprintf(param, sizeof(param), "arg%u", no); 1089129473Spjd name = gctl_get_asciiparam(req, param); 1090129473Spjd if (name == NULL) { 1091129473Spjd gctl_error(req, "No 'arg%u' argument.", no); 1092129473Spjd return; 1093129473Spjd } 1094129473Spjd if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 1095129473Spjd name += strlen("/dev/"); 1096129473Spjd pp = g_provider_by_name(name); 1097129473Spjd if (pp == NULL) { 1098129473Spjd G_STRIPE_DEBUG(1, "Disk %s is invalid.", name); 1099129473Spjd gctl_error(req, "Disk %s is invalid.", name); 1100129473Spjd return; 1101129473Spjd } 1102129473Spjd } 1103129473Spjd 1104129473Spjd gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL); 1105129473Spjd if (gp == NULL) { 1106132664Spjd gctl_error(req, "Can't configure %s.", md.md_name); 1107129473Spjd return; 1108129473Spjd } 1109129473Spjd 1110129473Spjd sc = gp->softc; 1111181463Sdes sb = sbuf_new_auto(); 1112129473Spjd sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name); 1113129473Spjd for (attached = 0, no = 1; no < *nargs; no++) { 1114129473Spjd snprintf(param, sizeof(param), "arg%u", no); 1115129473Spjd name = gctl_get_asciiparam(req, param); 1116146109Spjd if (name == NULL) { 1117146109Spjd gctl_error(req, "No 'arg%u' argument.", no); 1118146109Spjd continue; 1119146109Spjd } 1120129473Spjd if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 1121129473Spjd name += strlen("/dev/"); 1122129473Spjd pp = g_provider_by_name(name); 1123129473Spjd KASSERT(pp != NULL, ("Provider %s disappear?!", name)); 1124129473Spjd if (g_stripe_add_disk(sc, pp, no - 1) != 0) { 1125129473Spjd G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.", 1126129473Spjd no, pp->name, gp->name); 1127129473Spjd sbuf_printf(sb, " %s", pp->name); 1128129473Spjd continue; 1129129473Spjd } 1130129473Spjd attached++; 1131129473Spjd } 1132129473Spjd sbuf_finish(sb); 1133129473Spjd if (md.md_all != attached) { 1134129473Spjd g_stripe_destroy(gp->softc, 1); 1135129473Spjd gctl_error(req, "%s", sbuf_data(sb)); 1136129473Spjd } 1137129473Spjd sbuf_delete(sb); 1138129473Spjd} 1139129473Spjd 1140129473Spjdstatic struct g_stripe_softc * 1141129473Spjdg_stripe_find_device(struct g_class *mp, const char *name) 1142129473Spjd{ 1143129473Spjd struct g_stripe_softc *sc; 1144129473Spjd struct g_geom *gp; 1145129473Spjd 1146129473Spjd LIST_FOREACH(gp, &mp->geom, geom) { 1147129473Spjd sc = gp->softc; 1148129473Spjd if (sc == NULL) 1149129473Spjd continue; 1150132664Spjd if (strcmp(sc->sc_name, name) == 0) 1151129473Spjd return (sc); 1152129473Spjd } 1153129473Spjd return (NULL); 1154129473Spjd} 1155129473Spjd 1156129473Spjdstatic void 1157129473Spjdg_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp) 1158129473Spjd{ 1159129473Spjd struct g_stripe_softc *sc; 1160129473Spjd int *force, *nargs, error; 1161129473Spjd const char *name; 1162129473Spjd char param[16]; 1163129473Spjd u_int i; 1164129473Spjd 1165129473Spjd g_topology_assert(); 1166129473Spjd 1167129473Spjd nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1168129473Spjd if (nargs == NULL) { 1169129473Spjd gctl_error(req, "No '%s' argument.", "nargs"); 1170129473Spjd return; 1171129473Spjd } 1172129473Spjd if (*nargs <= 0) { 1173129473Spjd gctl_error(req, "Missing device(s)."); 1174129473Spjd return; 1175129473Spjd } 1176129473Spjd force = gctl_get_paraml(req, "force", sizeof(*force)); 1177129473Spjd if (force == NULL) { 1178129473Spjd gctl_error(req, "No '%s' argument.", "force"); 1179129473Spjd return; 1180129473Spjd } 1181129473Spjd 1182129473Spjd for (i = 0; i < (u_int)*nargs; i++) { 1183129473Spjd snprintf(param, sizeof(param), "arg%u", i); 1184129473Spjd name = gctl_get_asciiparam(req, param); 1185129473Spjd if (name == NULL) { 1186129473Spjd gctl_error(req, "No 'arg%u' argument.", i); 1187129473Spjd return; 1188129473Spjd } 1189129473Spjd sc = g_stripe_find_device(mp, name); 1190129473Spjd if (sc == NULL) { 1191129473Spjd gctl_error(req, "No such device: %s.", name); 1192129473Spjd return; 1193129473Spjd } 1194129473Spjd error = g_stripe_destroy(sc, *force); 1195129473Spjd if (error != 0) { 1196129473Spjd gctl_error(req, "Cannot destroy device %s (error=%d).", 1197132664Spjd sc->sc_name, error); 1198129473Spjd return; 1199129473Spjd } 1200129473Spjd } 1201129473Spjd} 1202129473Spjd 1203129473Spjdstatic void 1204129473Spjdg_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb) 1205129473Spjd{ 1206129473Spjd uint32_t *version; 1207129473Spjd 1208129473Spjd g_topology_assert(); 1209129473Spjd 1210129473Spjd version = gctl_get_paraml(req, "version", sizeof(*version)); 1211129473Spjd if (version == NULL) { 1212129473Spjd gctl_error(req, "No '%s' argument.", "version"); 1213129473Spjd return; 1214129473Spjd } 1215129473Spjd if (*version != G_STRIPE_VERSION) { 1216129473Spjd gctl_error(req, "Userland and kernel parts are out of sync."); 1217129473Spjd return; 1218129473Spjd } 1219129473Spjd 1220129473Spjd if (strcmp(verb, "create") == 0) { 1221129473Spjd g_stripe_ctl_create(req, mp); 1222129473Spjd return; 1223131649Spjd } else if (strcmp(verb, "destroy") == 0 || 1224131649Spjd strcmp(verb, "stop") == 0) { 1225129473Spjd g_stripe_ctl_destroy(req, mp); 1226129473Spjd return; 1227129473Spjd } 1228129473Spjd 1229129473Spjd gctl_error(req, "Unknown verb."); 1230129473Spjd} 1231129473Spjd 1232129473Spjdstatic void 1233129473Spjdg_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1234129473Spjd struct g_consumer *cp, struct g_provider *pp) 1235129473Spjd{ 1236129473Spjd struct g_stripe_softc *sc; 1237129473Spjd 1238129473Spjd sc = gp->softc; 1239132665Spjd if (sc == NULL) 1240129473Spjd return; 1241132665Spjd if (pp != NULL) { 1242132665Spjd /* Nothing here. */ 1243132665Spjd } else if (cp != NULL) { 1244134292Spjd sbuf_printf(sb, "%s<Number>%u</Number>\n", indent, 1245134292Spjd (u_int)cp->index); 1246132665Spjd } else { 1247132665Spjd sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id); 1248132665Spjd sbuf_printf(sb, "%s<Stripesize>%u</Stripesize>\n", indent, 1249132665Spjd (u_int)sc->sc_stripesize); 1250132665Spjd sbuf_printf(sb, "%s<Type>", indent); 1251132665Spjd switch (sc->sc_type) { 1252132665Spjd case G_STRIPE_TYPE_AUTOMATIC: 1253132665Spjd sbuf_printf(sb, "AUTOMATIC"); 1254132665Spjd break; 1255132665Spjd case G_STRIPE_TYPE_MANUAL: 1256132665Spjd sbuf_printf(sb, "MANUAL"); 1257132665Spjd break; 1258132665Spjd default: 1259132665Spjd sbuf_printf(sb, "UNKNOWN"); 1260132665Spjd break; 1261132665Spjd } 1262132665Spjd sbuf_printf(sb, "</Type>\n"); 1263132665Spjd sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n", 1264132665Spjd indent, sc->sc_ndisks, g_stripe_nvalid(sc)); 1265132665Spjd sbuf_printf(sb, "%s<State>", indent); 1266132665Spjd if (sc->sc_provider != NULL && sc->sc_provider->error == 0) 1267132665Spjd sbuf_printf(sb, "UP"); 1268132665Spjd else 1269132665Spjd sbuf_printf(sb, "DOWN"); 1270132665Spjd sbuf_printf(sb, "</State>\n"); 1271129473Spjd } 1272129473Spjd} 1273129473Spjd 1274129473SpjdDECLARE_GEOM_CLASS(g_stripe_class, g_stripe); 1275