geom_vinum_drive.c revision 149501
1130389Sle/*- 2142020Sle * Copyright (c) 2004, 2005 Lukas Ertl 3130389Sle * All rights reserved. 4130389Sle * 5130389Sle * Redistribution and use in source and binary forms, with or without 6130389Sle * modification, are permitted provided that the following conditions 7130389Sle * are met: 8130389Sle * 1. Redistributions of source code must retain the above copyright 9130389Sle * notice, this list of conditions and the following disclaimer. 10130389Sle * 2. Redistributions in binary form must reproduce the above copyright 11130389Sle * notice, this list of conditions and the following disclaimer in the 12130389Sle * documentation and/or other materials provided with the distribution. 13130389Sle * 14130389Sle * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15130389Sle * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16130389Sle * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17130389Sle * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18130389Sle * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19130389Sle * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20130389Sle * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21130389Sle * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22130389Sle * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23130389Sle * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24130389Sle * SUCH DAMAGE. 25130389Sle */ 26130389Sle 27130389Sle#include <sys/cdefs.h> 28130389Sle__FBSDID("$FreeBSD: head/sys/geom/vinum/geom_vinum_drive.c 149501 2005-08-26 14:40:32Z le $"); 29130389Sle 30130389Sle#include <sys/param.h> 31130389Sle#include <sys/bio.h> 32130389Sle#include <sys/errno.h> 33130389Sle#include <sys/conf.h> 34130389Sle#include <sys/kernel.h> 35130389Sle#include <sys/kthread.h> 36130389Sle#include <sys/libkern.h> 37130389Sle#include <sys/lock.h> 38130389Sle#include <sys/malloc.h> 39130389Sle#include <sys/module.h> 40130389Sle#include <sys/mutex.h> 41130389Sle#include <sys/sbuf.h> 42130389Sle#include <sys/systm.h> 43130389Sle#include <sys/time.h> 44130389Sle 45130389Sle#include <geom/geom.h> 46130389Sle#include <geom/vinum/geom_vinum_var.h> 47130389Sle#include <geom/vinum/geom_vinum.h> 48130389Sle#include <geom/vinum/geom_vinum_share.h> 49130389Sle 50146325Slestatic void gv_drive_dead(void *, int); 51135173Slestatic void gv_drive_worker(void *); 52130389Slevoid gv_drive_modify(struct gv_drive *); 53130389Sle 54130389Slevoid 55134407Slegv_config_new_drive(struct gv_drive *d) 56134407Sle{ 57134407Sle struct gv_hdr *vhdr; 58134407Sle struct gv_freelist *fl; 59134407Sle 60134407Sle KASSERT(d != NULL, ("config_new_drive: NULL d")); 61134407Sle 62134407Sle vhdr = g_malloc(sizeof(*vhdr), M_WAITOK | M_ZERO); 63134407Sle vhdr->magic = GV_MAGIC; 64134407Sle vhdr->config_length = GV_CFG_LEN; 65134407Sle 66134407Sle bcopy(hostname, vhdr->label.sysname, GV_HOSTNAME_LEN); 67134407Sle strncpy(vhdr->label.name, d->name, GV_MAXDRIVENAME); 68134407Sle microtime(&vhdr->label.date_of_birth); 69134407Sle 70134407Sle d->hdr = vhdr; 71134407Sle 72134407Sle LIST_INIT(&d->subdisks); 73134407Sle LIST_INIT(&d->freelist); 74134407Sle 75134407Sle fl = g_malloc(sizeof(struct gv_freelist), M_WAITOK | M_ZERO); 76134407Sle fl->offset = GV_DATA_START; 77134407Sle fl->size = d->avail; 78134407Sle LIST_INSERT_HEAD(&d->freelist, fl, freelist); 79134407Sle d->freelist_entries = 1; 80135173Sle 81135173Sle TAILQ_INIT(&d->bqueue); 82135173Sle mtx_init(&d->bqueue_mtx, "gv_drive", NULL, MTX_DEF); 83135173Sle kthread_create(gv_drive_worker, d, NULL, 0, 0, "gv_d %s", d->name); 84135173Sle d->flags |= GV_DRIVE_THREAD_ACTIVE; 85134407Sle} 86134407Sle 87134407Slevoid 88130389Slegv_save_config_all(struct gv_softc *sc) 89130389Sle{ 90130389Sle struct gv_drive *d; 91130389Sle 92130389Sle g_topology_assert(); 93130389Sle 94130389Sle LIST_FOREACH(d, &sc->drives, drive) { 95130389Sle if (d->geom == NULL) 96130389Sle continue; 97130389Sle gv_save_config(NULL, d, sc); 98130389Sle } 99130389Sle} 100130389Sle 101130389Sle/* Save the vinum configuration back to disk. */ 102130389Slevoid 103130389Slegv_save_config(struct g_consumer *cp, struct gv_drive *d, struct gv_softc *sc) 104130389Sle{ 105130389Sle struct g_geom *gp; 106130389Sle struct g_consumer *cp2; 107130389Sle struct gv_hdr *vhdr, *hdr; 108130389Sle struct sbuf *sb; 109130389Sle int error; 110130389Sle 111130389Sle g_topology_assert(); 112130389Sle 113130389Sle KASSERT(d != NULL, ("gv_save_config: null d")); 114130389Sle KASSERT(sc != NULL, ("gv_save_config: null sc")); 115130389Sle 116149094Sle /* 117149094Sle * We can't save the config on a drive that isn't up, but drives that 118149094Sle * were just created aren't officially up yet, so we check a special 119149094Sle * flag. 120149094Sle */ 121149094Sle if ((d->state != GV_DRIVE_UP) && !(d->flags && GV_DRIVE_NEWBORN)) 122146325Sle return; 123146325Sle 124130389Sle if (cp == NULL) { 125130389Sle gp = d->geom; 126130389Sle KASSERT(gp != NULL, ("gv_save_config: null gp")); 127130389Sle cp2 = LIST_FIRST(&gp->consumer); 128130389Sle KASSERT(cp2 != NULL, ("gv_save_config: null cp2")); 129130389Sle } else 130130389Sle cp2 = cp; 131130389Sle 132130389Sle vhdr = g_malloc(GV_HDR_LEN, M_WAITOK | M_ZERO); 133130389Sle vhdr->magic = GV_MAGIC; 134130389Sle vhdr->config_length = GV_CFG_LEN; 135130389Sle 136130389Sle hdr = d->hdr; 137130389Sle if (hdr == NULL) { 138146325Sle printf("GEOM_VINUM: drive %s has NULL hdr\n", d->name); 139130389Sle g_free(vhdr); 140130389Sle return; 141130389Sle } 142130389Sle microtime(&hdr->label.last_update); 143130389Sle bcopy(&hdr->label, &vhdr->label, sizeof(struct gv_label)); 144130389Sle 145130389Sle sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN); 146130389Sle gv_format_config(sc, sb, 1, NULL); 147130389Sle sbuf_finish(sb); 148130389Sle 149130389Sle error = g_access(cp2, 0, 1, 0); 150130389Sle if (error) { 151146325Sle printf("GEOM_VINUM: g_access failed on drive %s, errno %d\n", 152146325Sle d->name, error); 153130389Sle sbuf_delete(sb); 154146325Sle g_free(vhdr); 155130389Sle return; 156130389Sle } 157130389Sle g_topology_unlock(); 158130389Sle 159130389Sle do { 160130389Sle error = g_write_data(cp2, GV_HDR_OFFSET, vhdr, GV_HDR_LEN); 161130389Sle if (error) { 162146325Sle printf("GEOM_VINUM: writing vhdr failed on drive %s, " 163146325Sle "errno %d", d->name, error); 164130389Sle break; 165130389Sle } 166130389Sle 167130389Sle error = g_write_data(cp2, GV_CFG_OFFSET, sbuf_data(sb), 168130389Sle GV_CFG_LEN); 169130389Sle if (error) { 170146325Sle printf("GEOM_VINUM: writing first config copy failed " 171146325Sle "on drive %s, errno %d", d->name, error); 172130389Sle break; 173130389Sle } 174130389Sle 175130389Sle error = g_write_data(cp2, GV_CFG_OFFSET + GV_CFG_LEN, 176130389Sle sbuf_data(sb), GV_CFG_LEN); 177130389Sle if (error) 178146325Sle printf("GEOM_VINUM: writing second config copy failed " 179146325Sle "on drive %s, errno %d", d->name, error); 180130389Sle } while (0); 181130389Sle 182130389Sle g_topology_lock(); 183130389Sle g_access(cp2, 0, -1, 0); 184130389Sle sbuf_delete(sb); 185130389Sle g_free(vhdr); 186130389Sle 187130389Sle if (d->geom != NULL) 188130389Sle gv_drive_modify(d); 189130389Sle} 190130389Sle 191130389Sle/* This resembles g_slice_access(). */ 192130389Slestatic int 193130389Slegv_drive_access(struct g_provider *pp, int dr, int dw, int de) 194130389Sle{ 195130389Sle struct g_geom *gp; 196130389Sle struct g_consumer *cp; 197130389Sle struct g_provider *pp2; 198130389Sle struct gv_drive *d; 199130389Sle struct gv_sd *s, *s2; 200130389Sle int error; 201130389Sle 202130389Sle gp = pp->geom; 203130389Sle cp = LIST_FIRST(&gp->consumer); 204135173Sle if (cp == NULL) 205135173Sle return (0); 206130389Sle 207130389Sle d = gp->softc; 208146325Sle if (d == NULL) 209146325Sle return (0); 210130389Sle 211130389Sle s = pp->private; 212130389Sle KASSERT(s != NULL, ("gv_drive_access: NULL s")); 213130389Sle 214130389Sle LIST_FOREACH(s2, &d->subdisks, from_drive) { 215130389Sle if (s == s2) 216130389Sle continue; 217130389Sle if (s->drive_offset + s->size <= s2->drive_offset) 218130389Sle continue; 219130389Sle if (s2->drive_offset + s2->size <= s->drive_offset) 220130389Sle continue; 221130389Sle 222130389Sle /* Overlap. */ 223130389Sle pp2 = s2->provider; 224130389Sle KASSERT(s2 != NULL, ("gv_drive_access: NULL s2")); 225146325Sle if ((pp->acw + dw) > 0 && pp2->ace > 0) 226130389Sle return (EPERM); 227146325Sle if ((pp->ace + de) > 0 && pp2->acw > 0) 228130389Sle return (EPERM); 229130389Sle } 230130389Sle 231130389Sle error = g_access(cp, dr, dw, de); 232130389Sle return (error); 233130389Sle} 234130389Sle 235130389Slestatic void 236135173Slegv_drive_done(struct bio *bp) 237135173Sle{ 238135173Sle struct gv_drive *d; 239135173Sle struct gv_bioq *bq; 240135173Sle 241135173Sle /* Put the BIO on the worker queue again. */ 242135173Sle d = bp->bio_from->geom->softc; 243135173Sle bp->bio_cflags |= GV_BIO_DONE; 244135173Sle bq = g_malloc(sizeof(*bq), M_NOWAIT | M_ZERO); 245135173Sle bq->bp = bp; 246135173Sle mtx_lock(&d->bqueue_mtx); 247135173Sle TAILQ_INSERT_TAIL(&d->bqueue, bq, queue); 248135173Sle wakeup(d); 249135173Sle mtx_unlock(&d->bqueue_mtx); 250135173Sle} 251135173Sle 252135173Sle 253135173Slestatic void 254130389Slegv_drive_start(struct bio *bp) 255130389Sle{ 256130389Sle struct gv_drive *d; 257130389Sle struct gv_sd *s; 258135173Sle struct gv_bioq *bq; 259130389Sle 260135173Sle switch (bp->bio_cmd) { 261135173Sle case BIO_READ: 262135173Sle case BIO_WRITE: 263135173Sle case BIO_DELETE: 264135173Sle break; 265135173Sle case BIO_GETATTR: 266135173Sle default: 267135173Sle g_io_deliver(bp, EOPNOTSUPP); 268135173Sle return; 269135173Sle } 270130389Sle 271135173Sle s = bp->bio_to->private; 272130389Sle if ((s->state == GV_SD_DOWN) || (s->state == GV_SD_STALE)) { 273130389Sle g_io_deliver(bp, ENXIO); 274130389Sle return; 275130389Sle } 276130389Sle 277135173Sle d = bp->bio_to->geom->softc; 278130389Sle 279135173Sle /* 280135173Sle * Put the BIO on the worker queue, where the worker thread will pick 281135173Sle * it up. 282135173Sle */ 283135173Sle bq = g_malloc(sizeof(*bq), M_NOWAIT | M_ZERO); 284135173Sle bq->bp = bp; 285135173Sle mtx_lock(&d->bqueue_mtx); 286135173Sle TAILQ_INSERT_TAIL(&d->bqueue, bq, queue); 287135173Sle wakeup(d); 288135173Sle mtx_unlock(&d->bqueue_mtx); 289130389Sle 290135173Sle} 291135173Sle 292135173Slestatic void 293135173Slegv_drive_worker(void *arg) 294135173Sle{ 295135173Sle struct bio *bp, *cbp; 296135173Sle struct g_geom *gp; 297135173Sle struct g_provider *pp; 298135173Sle struct gv_drive *d; 299135173Sle struct gv_sd *s; 300135173Sle struct gv_bioq *bq, *bq2; 301135173Sle int error; 302135173Sle 303135173Sle d = arg; 304135173Sle 305135173Sle mtx_lock(&d->bqueue_mtx); 306135173Sle for (;;) { 307135173Sle /* We were signaled to exit. */ 308135173Sle if (d->flags & GV_DRIVE_THREAD_DIE) 309135173Sle break; 310135173Sle 311135173Sle /* Take the first BIO from out queue. */ 312135173Sle bq = TAILQ_FIRST(&d->bqueue); 313135173Sle if (bq == NULL) { 314135173Sle msleep(d, &d->bqueue_mtx, PRIBIO, "-", hz/10); 315135173Sle continue; 316135173Sle } 317135173Sle TAILQ_REMOVE(&d->bqueue, bq, queue); 318135173Sle mtx_unlock(&d->bqueue_mtx); 319135173Sle 320135173Sle bp = bq->bp; 321135173Sle g_free(bq); 322135173Sle pp = bp->bio_to; 323135173Sle gp = pp->geom; 324135173Sle 325135173Sle /* Completed request. */ 326135173Sle if (bp->bio_cflags & GV_BIO_DONE) { 327135173Sle error = bp->bio_error; 328135173Sle 329135173Sle /* Deliver the original request. */ 330135173Sle g_std_done(bp); 331135173Sle 332135173Sle /* The request had an error, we need to clean up. */ 333135173Sle if (error != 0) { 334135173Sle g_topology_lock(); 335135173Sle gv_set_drive_state(d, GV_DRIVE_DOWN, 336135173Sle GV_SETSTATE_FORCE | GV_SETSTATE_CONFIG); 337135173Sle g_topology_unlock(); 338146325Sle g_post_event(gv_drive_dead, d, M_WAITOK, d, 339146325Sle NULL); 340135173Sle } 341135173Sle 342135173Sle /* New request, needs to be sent downwards. */ 343135173Sle } else { 344135173Sle s = pp->private; 345135173Sle 346135173Sle if ((s->state == GV_SD_DOWN) || 347135173Sle (s->state == GV_SD_STALE)) { 348135173Sle g_io_deliver(bp, ENXIO); 349135173Sle mtx_lock(&d->bqueue_mtx); 350135173Sle continue; 351135173Sle } 352135173Sle if (bp->bio_offset > s->size) { 353135173Sle g_io_deliver(bp, EINVAL); 354135173Sle mtx_lock(&d->bqueue_mtx); 355135173Sle continue; 356135173Sle } 357135173Sle 358135173Sle cbp = g_clone_bio(bp); 359135173Sle if (cbp == NULL) { 360135173Sle g_io_deliver(bp, ENOMEM); 361135173Sle mtx_lock(&d->bqueue_mtx); 362135173Sle continue; 363135173Sle } 364135173Sle if (cbp->bio_offset + cbp->bio_length > s->size) 365135173Sle cbp->bio_length = s->size - 366135173Sle cbp->bio_offset; 367135173Sle cbp->bio_done = gv_drive_done; 368135173Sle cbp->bio_offset += s->drive_offset; 369135173Sle g_io_request(cbp, LIST_FIRST(&gp->consumer)); 370130389Sle } 371130389Sle 372135173Sle mtx_lock(&d->bqueue_mtx); 373130389Sle } 374135173Sle 375135173Sle TAILQ_FOREACH_SAFE(bq, &d->bqueue, queue, bq2) { 376135173Sle TAILQ_REMOVE(&d->bqueue, bq, queue); 377135173Sle mtx_unlock(&d->bqueue_mtx); 378135173Sle bp = bq->bp; 379135173Sle g_free(bq); 380135173Sle if (bp->bio_cflags & GV_BIO_DONE) 381135173Sle g_std_done(bp); 382135173Sle else 383135173Sle g_io_deliver(bp, ENXIO); 384135173Sle mtx_lock(&d->bqueue_mtx); 385135173Sle } 386135173Sle mtx_unlock(&d->bqueue_mtx); 387135173Sle d->flags |= GV_DRIVE_THREAD_DEAD; 388135173Sle 389135173Sle kthread_exit(ENXIO); 390130389Sle} 391130389Sle 392135173Sle 393130389Slestatic void 394130389Slegv_drive_orphan(struct g_consumer *cp) 395130389Sle{ 396130389Sle struct g_geom *gp; 397130597Sle struct gv_drive *d; 398130389Sle 399130389Sle g_topology_assert(); 400130389Sle gp = cp->geom; 401130389Sle g_trace(G_T_TOPOLOGY, "gv_drive_orphan(%s)", gp->name); 402130597Sle d = gp->softc; 403130697Sle if (d != NULL) { 404135162Sle gv_set_drive_state(d, GV_DRIVE_DOWN, 405135162Sle GV_SETSTATE_FORCE | GV_SETSTATE_CONFIG); 406146325Sle g_post_event(gv_drive_dead, d, M_WAITOK, d, NULL); 407146325Sle } else 408146325Sle g_wither_geom(gp, ENXIO); 409130389Sle} 410130389Sle 411130389Slestatic struct g_geom * 412130389Slegv_drive_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 413130389Sle{ 414130389Sle struct g_geom *gp, *gp2; 415130389Sle struct g_consumer *cp; 416130389Sle struct gv_drive *d; 417130389Sle struct gv_sd *s; 418130389Sle struct gv_softc *sc; 419130389Sle struct gv_freelist *fl; 420130389Sle struct gv_hdr *vhdr; 421130389Sle int error; 422149501Sle char errstr[ERRBUFSIZ]; 423130389Sle 424130389Sle vhdr = NULL; 425130389Sle d = NULL; 426130389Sle 427130389Sle g_trace(G_T_TOPOLOGY, "gv_drive_taste(%s, %s)", mp->name, pp->name); 428130389Sle g_topology_assert(); 429130389Sle 430130389Sle /* Find the VINUM class and its associated geom. */ 431130389Sle gp2 = find_vinum_geom(); 432130389Sle if (gp2 == NULL) 433130389Sle return (NULL); 434130389Sle sc = gp2->softc; 435130389Sle 436130389Sle gp = g_new_geomf(mp, "%s.vinumdrive", pp->name); 437133983Sle gp->start = gv_drive_start; 438133983Sle gp->orphan = gv_drive_orphan; 439133983Sle gp->access = gv_drive_access; 440133983Sle gp->start = gv_drive_start; 441130389Sle 442130389Sle cp = g_new_consumer(gp); 443130389Sle g_attach(cp, pp); 444130389Sle error = g_access(cp, 1, 0, 0); 445130389Sle if (error) { 446130389Sle g_detach(cp); 447130389Sle g_destroy_consumer(cp); 448130389Sle g_destroy_geom(gp); 449130389Sle return (NULL); 450130389Sle } 451130389Sle 452130389Sle g_topology_unlock(); 453130389Sle 454130389Sle /* Now check if the provided slice is a valid vinum drive. */ 455130389Sle do { 456135161Sle vhdr = g_read_data(cp, GV_HDR_OFFSET, pp->sectorsize, &error); 457130389Sle if (vhdr == NULL || error != 0) 458130389Sle break; 459130389Sle if (vhdr->magic != GV_MAGIC) { 460130389Sle g_free(vhdr); 461130389Sle break; 462130389Sle } 463130389Sle 464149501Sle g_topology_lock(); 465149501Sle 466130389Sle /* 467130389Sle * We have found a valid vinum drive. Let's see if it is 468149501Sle * already known in the configuration. 469130389Sle */ 470130389Sle d = gv_find_drive(sc, vhdr->label.name); 471130389Sle 472130389Sle /* We already know about this drive. */ 473130389Sle if (d != NULL) { 474133983Sle /* Check if this drive already has a geom. */ 475133983Sle if (d->geom != NULL) { 476133983Sle g_topology_unlock(); 477133983Sle break; 478133983Sle } 479130389Sle bcopy(vhdr, d->hdr, sizeof(*vhdr)); 480130389Sle 481130389Sle /* This is a new drive. */ 482130389Sle } else { 483130389Sle d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO); 484130389Sle 485130389Sle /* Initialize all needed variables. */ 486130389Sle d->size = pp->mediasize - GV_DATA_START; 487130389Sle d->avail = d->size; 488130389Sle d->hdr = vhdr; 489130389Sle strncpy(d->name, vhdr->label.name, GV_MAXDRIVENAME); 490130389Sle LIST_INIT(&d->subdisks); 491130389Sle LIST_INIT(&d->freelist); 492130389Sle 493130389Sle /* We also need a freelist entry. */ 494130389Sle fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO); 495130389Sle fl->offset = GV_DATA_START; 496130389Sle fl->size = d->avail; 497130389Sle LIST_INSERT_HEAD(&d->freelist, fl, freelist); 498130389Sle d->freelist_entries = 1; 499130389Sle 500135173Sle TAILQ_INIT(&d->bqueue); 501140475Sle 502140475Sle /* Save it into the main configuration. */ 503140475Sle LIST_INSERT_HEAD(&sc->drives, d, drive); 504140475Sle } 505140475Sle 506140475Sle /* 507140475Sle * Create a bio queue mutex and a worker thread, if necessary. 508140475Sle */ 509140475Sle if (mtx_initialized(&d->bqueue_mtx) == 0) 510135173Sle mtx_init(&d->bqueue_mtx, "gv_drive", NULL, MTX_DEF); 511140475Sle 512140475Sle if (!(d->flags & GV_DRIVE_THREAD_ACTIVE)) { 513135173Sle kthread_create(gv_drive_worker, d, NULL, 0, 0, 514135173Sle "gv_d %s", d->name); 515135173Sle d->flags |= GV_DRIVE_THREAD_ACTIVE; 516130389Sle } 517130389Sle 518133983Sle g_access(cp, -1, 0, 0); 519132617Sle 520130389Sle gp->softc = d; 521130389Sle d->geom = gp; 522135173Sle d->vinumconf = sc; 523130389Sle strncpy(d->device, pp->name, GV_MAXDRIVENAME); 524130389Sle 525130389Sle /* 526130389Sle * Find out which subdisks belong to this drive and crosslink 527130389Sle * them. 528130389Sle */ 529130389Sle LIST_FOREACH(s, &sc->subdisks, sd) { 530130389Sle if (!strncmp(s->drive, d->name, GV_MAXDRIVENAME)) 531130389Sle /* XXX: errors ignored */ 532130389Sle gv_sd_to_drive(sc, d, s, errstr, 533130389Sle sizeof(errstr)); 534130389Sle } 535130389Sle 536130389Sle /* This drive is now up for sure. */ 537130389Sle gv_set_drive_state(d, GV_DRIVE_UP, 0); 538130389Sle 539130389Sle /* 540130389Sle * If there are subdisks on this drive, we need to create 541130389Sle * providers for them. 542130389Sle */ 543130389Sle if (d->sdcount) 544130389Sle gv_drive_modify(d); 545130389Sle 546130389Sle return (gp); 547130389Sle 548130389Sle } while (0); 549130389Sle 550130389Sle g_topology_lock(); 551130389Sle g_access(cp, -1, 0, 0); 552130389Sle 553130389Sle g_detach(cp); 554130389Sle g_destroy_consumer(cp); 555130389Sle g_destroy_geom(gp); 556130389Sle return (NULL); 557130389Sle} 558130389Sle 559130389Sle/* 560130389Sle * Modify the providers for the given drive 'd'. It is assumed that the 561130389Sle * subdisk list of 'd' is already correctly set up. 562130389Sle */ 563130389Slevoid 564130389Slegv_drive_modify(struct gv_drive *d) 565130389Sle{ 566130389Sle struct g_geom *gp; 567130389Sle struct g_consumer *cp; 568130389Sle struct g_provider *pp, *pp2; 569130389Sle struct gv_sd *s; 570130389Sle 571130389Sle KASSERT(d != NULL, ("gv_drive_modify: null d")); 572130389Sle gp = d->geom; 573130389Sle KASSERT(gp != NULL, ("gv_drive_modify: null gp")); 574130389Sle cp = LIST_FIRST(&gp->consumer); 575130389Sle KASSERT(cp != NULL, ("gv_drive_modify: null cp")); 576130389Sle pp = cp->provider; 577130389Sle KASSERT(pp != NULL, ("gv_drive_modify: null pp")); 578130389Sle 579130389Sle g_topology_assert(); 580130389Sle 581130389Sle LIST_FOREACH(s, &d->subdisks, from_drive) { 582130389Sle /* This subdisk already has a provider. */ 583130389Sle if (s->provider != NULL) 584130389Sle continue; 585130389Sle pp2 = g_new_providerf(gp, "gvinum/sd/%s", s->name); 586130389Sle pp2->mediasize = s->size; 587130389Sle pp2->sectorsize = pp->sectorsize; 588130389Sle g_error_provider(pp2, 0); 589130389Sle s->provider = pp2; 590130389Sle pp2->private = s; 591130389Sle } 592130389Sle} 593130389Sle 594146325Slestatic void 595146325Slegv_drive_dead(void *arg, int flag) 596146325Sle{ 597146325Sle struct g_geom *gp; 598146325Sle struct g_consumer *cp; 599146325Sle struct gv_drive *d; 600146325Sle struct gv_sd *s; 601146325Sle 602146325Sle g_topology_assert(); 603146325Sle KASSERT(arg != NULL, ("gv_drive_dead: NULL arg")); 604146325Sle 605146325Sle if (flag == EV_CANCEL) 606146325Sle return; 607146325Sle 608146325Sle d = arg; 609146325Sle if (d->state != GV_DRIVE_DOWN) 610146325Sle return; 611146325Sle 612146325Sle g_trace(G_T_TOPOLOGY, "gv_drive_dead(%s)", d->name); 613146325Sle 614146325Sle gp = d->geom; 615146325Sle if (gp == NULL) 616146325Sle return; 617146325Sle 618146325Sle LIST_FOREACH(cp, &gp->consumer, consumer) { 619146325Sle if (cp->nstart != cp->nend) { 620146325Sle printf("GEOM_VINUM: dead drive '%s' has still " 621146325Sle "active requests, can't detach consumer\n", 622146325Sle d->name); 623146325Sle g_post_event(gv_drive_dead, d, M_WAITOK, d, 624146325Sle NULL); 625146325Sle return; 626146325Sle } 627146325Sle if (cp->acr != 0 || cp->acw != 0 || cp->ace != 0) 628146325Sle g_access(cp, -cp->acr, -cp->acw, -cp->ace); 629146325Sle } 630146325Sle 631146325Sle printf("GEOM_VINUM: lost drive '%s'\n", d->name); 632146325Sle d->geom = NULL; 633146325Sle LIST_FOREACH(s, &d->subdisks, from_drive) { 634146325Sle s->provider = NULL; 635146325Sle s->consumer = NULL; 636146325Sle } 637146325Sle gv_kill_drive_thread(d); 638146325Sle gp->softc = NULL; 639146325Sle g_wither_geom(gp, ENXIO); 640146325Sle} 641146325Sle 642130389Slestatic int 643130389Slegv_drive_destroy_geom(struct gctl_req *req, struct g_class *mp, 644130389Sle struct g_geom *gp) 645130389Sle{ 646135173Sle struct gv_drive *d; 647135173Sle 648130389Sle g_trace(G_T_TOPOLOGY, "gv_drive_destroy_geom: %s", gp->name); 649130389Sle g_topology_assert(); 650130389Sle 651135173Sle d = gp->softc; 652135173Sle gv_kill_drive_thread(d); 653135173Sle 654130389Sle g_wither_geom(gp, ENXIO); 655130389Sle return (0); 656130389Sle} 657130389Sle 658130389Sle#define VINUMDRIVE_CLASS_NAME "VINUMDRIVE" 659130389Sle 660130389Slestatic struct g_class g_vinum_drive_class = { 661130389Sle .name = VINUMDRIVE_CLASS_NAME, 662133318Sphk .version = G_VERSION, 663130389Sle .taste = gv_drive_taste, 664130389Sle .destroy_geom = gv_drive_destroy_geom 665130389Sle}; 666130389Sle 667130389SleDECLARE_GEOM_CLASS(g_vinum_drive_class, g_vinum_drive); 668