geom_vinum_drive.c revision 134407
1130389Sle/*- 2130389Sle * Copyright (c) 2004 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 134407 2004-08-27 21:32:18Z 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 50130389Slevoid gv_drive_modify(struct gv_drive *); 51130389Sle 52130389Slevoid 53134407Slegv_config_new_drive(struct gv_drive *d) 54134407Sle{ 55134407Sle struct gv_hdr *vhdr; 56134407Sle struct gv_freelist *fl; 57134407Sle 58134407Sle KASSERT(d != NULL, ("config_new_drive: NULL d")); 59134407Sle 60134407Sle vhdr = g_malloc(sizeof(*vhdr), M_WAITOK | M_ZERO); 61134407Sle vhdr->magic = GV_MAGIC; 62134407Sle vhdr->config_length = GV_CFG_LEN; 63134407Sle 64134407Sle bcopy(hostname, vhdr->label.sysname, GV_HOSTNAME_LEN); 65134407Sle strncpy(vhdr->label.name, d->name, GV_MAXDRIVENAME); 66134407Sle microtime(&vhdr->label.date_of_birth); 67134407Sle 68134407Sle d->hdr = vhdr; 69134407Sle 70134407Sle LIST_INIT(&d->subdisks); 71134407Sle LIST_INIT(&d->freelist); 72134407Sle 73134407Sle fl = g_malloc(sizeof(struct gv_freelist), M_WAITOK | M_ZERO); 74134407Sle fl->offset = GV_DATA_START; 75134407Sle fl->size = d->avail; 76134407Sle LIST_INSERT_HEAD(&d->freelist, fl, freelist); 77134407Sle d->freelist_entries = 1; 78134407Sle} 79134407Sle 80134407Slevoid 81130389Slegv_save_config_all(struct gv_softc *sc) 82130389Sle{ 83130389Sle struct gv_drive *d; 84130389Sle 85130389Sle g_topology_assert(); 86130389Sle 87130389Sle LIST_FOREACH(d, &sc->drives, drive) { 88130389Sle if (d->geom == NULL) 89130389Sle continue; 90130389Sle gv_save_config(NULL, d, sc); 91130389Sle } 92130389Sle} 93130389Sle 94130389Sle/* Save the vinum configuration back to disk. */ 95130389Slevoid 96130389Slegv_save_config(struct g_consumer *cp, struct gv_drive *d, struct gv_softc *sc) 97130389Sle{ 98130389Sle struct g_geom *gp; 99130389Sle struct g_consumer *cp2; 100130389Sle struct gv_hdr *vhdr, *hdr; 101130389Sle struct sbuf *sb; 102130389Sle int error; 103130389Sle 104130389Sle g_topology_assert(); 105130389Sle 106130389Sle KASSERT(d != NULL, ("gv_save_config: null d")); 107130389Sle KASSERT(sc != NULL, ("gv_save_config: null sc")); 108130389Sle 109130389Sle if (cp == NULL) { 110130389Sle gp = d->geom; 111130389Sle KASSERT(gp != NULL, ("gv_save_config: null gp")); 112130389Sle cp2 = LIST_FIRST(&gp->consumer); 113130389Sle KASSERT(cp2 != NULL, ("gv_save_config: null cp2")); 114130389Sle } else 115130389Sle cp2 = cp; 116130389Sle 117130389Sle vhdr = g_malloc(GV_HDR_LEN, M_WAITOK | M_ZERO); 118130389Sle vhdr->magic = GV_MAGIC; 119130389Sle vhdr->config_length = GV_CFG_LEN; 120130389Sle 121130389Sle hdr = d->hdr; 122130389Sle if (hdr == NULL) { 123130389Sle printf("NULL hdr!!!\n"); 124130389Sle g_free(vhdr); 125130389Sle return; 126130389Sle } 127130389Sle microtime(&hdr->label.last_update); 128130389Sle bcopy(&hdr->label, &vhdr->label, sizeof(struct gv_label)); 129130389Sle 130130389Sle sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN); 131130389Sle gv_format_config(sc, sb, 1, NULL); 132130389Sle sbuf_finish(sb); 133130389Sle 134130389Sle error = g_access(cp2, 0, 1, 0); 135130389Sle if (error) { 136130389Sle printf("g_access failed: %d\n", error); 137130389Sle sbuf_delete(sb); 138130389Sle return; 139130389Sle } 140130389Sle g_topology_unlock(); 141130389Sle 142130389Sle do { 143130389Sle error = g_write_data(cp2, GV_HDR_OFFSET, vhdr, GV_HDR_LEN); 144130389Sle if (error) { 145130389Sle printf("writing vhdr failed: %d", error); 146130389Sle break; 147130389Sle } 148130389Sle 149130389Sle error = g_write_data(cp2, GV_CFG_OFFSET, sbuf_data(sb), 150130389Sle GV_CFG_LEN); 151130389Sle if (error) { 152130389Sle printf("writing first config copy failed: %d", error); 153130389Sle break; 154130389Sle } 155130389Sle 156130389Sle error = g_write_data(cp2, GV_CFG_OFFSET + GV_CFG_LEN, 157130389Sle sbuf_data(sb), GV_CFG_LEN); 158130389Sle if (error) 159130389Sle printf("writing second config copy failed: %d", error); 160130389Sle } while (0); 161130389Sle 162130389Sle g_topology_lock(); 163130389Sle g_access(cp2, 0, -1, 0); 164130389Sle sbuf_delete(sb); 165130389Sle g_free(vhdr); 166130389Sle 167130389Sle if (d->geom != NULL) 168130389Sle gv_drive_modify(d); 169130389Sle} 170130389Sle 171130389Sle/* This resembles g_slice_access(). */ 172130389Slestatic int 173130389Slegv_drive_access(struct g_provider *pp, int dr, int dw, int de) 174130389Sle{ 175130389Sle struct g_geom *gp; 176130389Sle struct g_consumer *cp; 177130389Sle struct g_provider *pp2; 178130389Sle struct gv_drive *d; 179130389Sle struct gv_sd *s, *s2; 180130389Sle int error; 181130389Sle 182130389Sle gp = pp->geom; 183130389Sle cp = LIST_FIRST(&gp->consumer); 184130389Sle KASSERT(cp != NULL, ("gv_drive_access: NULL cp")); 185130389Sle 186130389Sle d = gp->softc; 187130389Sle 188130389Sle s = pp->private; 189130389Sle KASSERT(s != NULL, ("gv_drive_access: NULL s")); 190130389Sle 191130389Sle LIST_FOREACH(s2, &d->subdisks, from_drive) { 192130389Sle if (s == s2) 193130389Sle continue; 194130389Sle if (s->drive_offset + s->size <= s2->drive_offset) 195130389Sle continue; 196130389Sle if (s2->drive_offset + s2->size <= s->drive_offset) 197130389Sle continue; 198130389Sle 199130389Sle /* Overlap. */ 200130389Sle pp2 = s2->provider; 201130389Sle KASSERT(s2 != NULL, ("gv_drive_access: NULL s2")); 202130389Sle if ((pp->acw + dw) > 0 && pp2->ace > 0) { 203130389Sle printf("FOOO: permission denied - e\n"); 204130389Sle return (EPERM); 205130389Sle } 206130389Sle if ((pp->ace + de) > 0 && pp2->acw > 0) { 207130389Sle printf("FOOO: permission denied - w\n"); 208130389Sle return (EPERM); 209130389Sle } 210130389Sle } 211130389Sle 212132617Sle#if 0 213130389Sle /* On first open, grab an extra "exclusive" bit */ 214130389Sle if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 215130389Sle de++; 216130389Sle /* ... and let go of it on last close */ 217130389Sle if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 218130389Sle de--; 219132617Sle#endif 220130389Sle error = g_access(cp, dr, dw, de); 221130389Sle if (error) { 222130389Sle printf("FOOO: g_access failed: %d\n", error); 223130389Sle } 224130389Sle return (error); 225130389Sle} 226130389Sle 227130389Slestatic void 228130389Slegv_drive_start(struct bio *bp) 229130389Sle{ 230130389Sle struct bio *bp2; 231130389Sle struct g_geom *gp; 232130389Sle struct g_consumer *cp; 233130389Sle struct g_provider *pp; 234130389Sle struct gv_drive *d; 235130389Sle struct gv_sd *s; 236130389Sle 237130389Sle pp = bp->bio_to; 238130389Sle gp = pp->geom; 239130389Sle cp = LIST_FIRST(&gp->consumer); 240130389Sle d = gp->softc; 241130389Sle s = pp->private; 242130389Sle 243130389Sle if ((s->state == GV_SD_DOWN) || (s->state == GV_SD_STALE)) { 244130389Sle g_io_deliver(bp, ENXIO); 245130389Sle return; 246130389Sle } 247130389Sle 248130389Sle switch(bp->bio_cmd) { 249130389Sle case BIO_READ: 250130389Sle case BIO_WRITE: 251130389Sle case BIO_DELETE: 252130389Sle if (bp->bio_offset > s->size) { 253130389Sle g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */ 254130389Sle return; 255130389Sle } 256130389Sle bp2 = g_clone_bio(bp); 257130389Sle if (bp2 == NULL) { 258130389Sle g_io_deliver(bp, ENOMEM); 259130389Sle return; 260130389Sle } 261130389Sle if (bp2->bio_offset + bp2->bio_length > s->size) 262130389Sle bp2->bio_length = s->size - bp2->bio_offset; 263130389Sle bp2->bio_done = g_std_done; 264130389Sle bp2->bio_offset += s->drive_offset; 265130389Sle g_io_request(bp2, cp); 266130389Sle return; 267130389Sle 268130389Sle case BIO_GETATTR: 269130389Sle if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { 270130389Sle struct g_kerneldump *gkd; 271130389Sle 272130389Sle gkd = (struct g_kerneldump *)bp->bio_data; 273130389Sle gkd->offset += s->drive_offset; 274130389Sle if (gkd->length > s->size) 275130389Sle gkd->length = s->size; 276130389Sle /* now, pass it on downwards... */ 277130389Sle } 278130389Sle bp2 = g_clone_bio(bp); 279130389Sle if (bp2 == NULL) { 280130389Sle g_io_deliver(bp, ENOMEM); 281130389Sle return; 282130389Sle } 283130389Sle bp2->bio_done = g_std_done; 284130389Sle g_io_request(bp2, cp); 285130389Sle return; 286130389Sle 287130389Sle default: 288130389Sle g_io_deliver(bp, EOPNOTSUPP); 289130389Sle return; 290130389Sle } 291130389Sle} 292130389Sle 293130389Slestatic void 294130389Slegv_drive_orphan(struct g_consumer *cp) 295130389Sle{ 296130389Sle struct g_geom *gp; 297130597Sle struct gv_drive *d; 298130597Sle struct gv_sd *s; 299130389Sle int error; 300130389Sle 301130389Sle g_topology_assert(); 302130389Sle gp = cp->geom; 303130389Sle g_trace(G_T_TOPOLOGY, "gv_drive_orphan(%s)", gp->name); 304130389Sle if (cp->acr != 0 || cp->acw != 0 || cp->ace != 0) 305130389Sle g_access(cp, -cp->acr, -cp->acw, -cp->ace); 306130389Sle error = cp->provider->error; 307130389Sle if (error == 0) 308130389Sle error = ENXIO; 309130389Sle g_detach(cp); 310130389Sle g_destroy_consumer(cp); 311130389Sle if (!LIST_EMPTY(&gp->consumer)) 312130389Sle return; 313130597Sle d = gp->softc; 314130697Sle if (d != NULL) { 315130697Sle printf("gvinum: lost drive '%s'\n", d->name); 316130697Sle d->geom = NULL; 317130697Sle LIST_FOREACH(s, &d->subdisks, from_drive) { 318130697Sle s->provider = NULL; 319130697Sle s->consumer = NULL; 320130697Sle } 321130697Sle gv_set_drive_state(d, GV_DRIVE_DOWN, GV_SETSTATE_FORCE); 322130597Sle } 323130597Sle gp->softc = NULL; 324130389Sle g_wither_geom(gp, error); 325130389Sle} 326130389Sle 327130389Slestatic struct g_geom * 328130389Slegv_drive_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 329130389Sle{ 330130389Sle struct g_geom *gp, *gp2; 331130389Sle struct g_consumer *cp; 332130389Sle struct gv_drive *d; 333130389Sle struct gv_sd *s; 334130389Sle struct gv_softc *sc; 335130389Sle struct gv_freelist *fl; 336130389Sle struct gv_hdr *vhdr; 337130389Sle int error; 338132642Sle char *buf, errstr[ERRBUFSIZ]; 339130389Sle 340130389Sle vhdr = NULL; 341130389Sle d = NULL; 342130389Sle 343130389Sle g_trace(G_T_TOPOLOGY, "gv_drive_taste(%s, %s)", mp->name, pp->name); 344130389Sle g_topology_assert(); 345130389Sle 346130389Sle if (pp->sectorsize == 0) 347130389Sle return(NULL); 348130389Sle 349130389Sle /* Find the VINUM class and its associated geom. */ 350130389Sle gp2 = find_vinum_geom(); 351130389Sle if (gp2 == NULL) 352130389Sle return (NULL); 353130389Sle sc = gp2->softc; 354130389Sle 355130389Sle gp = g_new_geomf(mp, "%s.vinumdrive", pp->name); 356133983Sle gp->start = gv_drive_start; 357133983Sle gp->orphan = gv_drive_orphan; 358133983Sle gp->access = gv_drive_access; 359133983Sle gp->start = gv_drive_start; 360130389Sle 361130389Sle cp = g_new_consumer(gp); 362130389Sle g_attach(cp, pp); 363130389Sle error = g_access(cp, 1, 0, 0); 364130389Sle if (error) { 365130389Sle g_detach(cp); 366130389Sle g_destroy_consumer(cp); 367130389Sle g_destroy_geom(gp); 368130389Sle return (NULL); 369130389Sle } 370130389Sle 371130389Sle g_topology_unlock(); 372130389Sle 373130389Sle /* Now check if the provided slice is a valid vinum drive. */ 374130389Sle do { 375130389Sle vhdr = g_read_data(cp, GV_HDR_OFFSET, GV_HDR_LEN, &error); 376130389Sle if (vhdr == NULL || error != 0) 377130389Sle break; 378130389Sle if (vhdr->magic != GV_MAGIC) { 379130389Sle g_free(vhdr); 380130389Sle break; 381130389Sle } 382130389Sle 383130389Sle /* 384130389Sle * We have found a valid vinum drive. Let's see if it is 385132642Sle * already known in the configuration. There's a chance that 386132642Sle * the VINUMDRIVE class tastes before the VINUM class could 387132642Sle * taste, so parse the configuration here too, just to be on 388132642Sle * the safe side. 389130389Sle */ 390132642Sle buf = g_read_data(cp, GV_CFG_OFFSET, GV_CFG_LEN, &error); 391132642Sle if (buf == NULL || error != 0) { 392132642Sle g_free(vhdr); 393132642Sle break; 394132642Sle } 395133449Sle g_topology_lock(); 396132642Sle gv_parse_config(sc, buf, 1); 397132642Sle g_free(buf); 398132642Sle 399130389Sle d = gv_find_drive(sc, vhdr->label.name); 400130389Sle 401130389Sle /* We already know about this drive. */ 402130389Sle if (d != NULL) { 403133983Sle /* Check if this drive already has a geom. */ 404133983Sle if (d->geom != NULL) { 405133983Sle g_topology_unlock(); 406133983Sle break; 407133983Sle } 408130389Sle bcopy(vhdr, d->hdr, sizeof(*vhdr)); 409130389Sle 410130389Sle /* This is a new drive. */ 411130389Sle } else { 412130389Sle d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO); 413130389Sle 414130389Sle /* Initialize all needed variables. */ 415130389Sle d->size = pp->mediasize - GV_DATA_START; 416130389Sle d->avail = d->size; 417130389Sle d->hdr = vhdr; 418130389Sle strncpy(d->name, vhdr->label.name, GV_MAXDRIVENAME); 419130389Sle LIST_INIT(&d->subdisks); 420130389Sle LIST_INIT(&d->freelist); 421130389Sle 422130389Sle /* We also need a freelist entry. */ 423130389Sle fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO); 424130389Sle fl->offset = GV_DATA_START; 425130389Sle fl->size = d->avail; 426130389Sle LIST_INSERT_HEAD(&d->freelist, fl, freelist); 427130389Sle d->freelist_entries = 1; 428130389Sle 429130389Sle /* Save it into the main configuration. */ 430130389Sle LIST_INSERT_HEAD(&sc->drives, d, drive); 431130389Sle } 432130389Sle 433133983Sle g_access(cp, -1, 0, 0); 434132617Sle 435130389Sle gp->softc = d; 436130389Sle d->geom = gp; 437130389Sle strncpy(d->device, pp->name, GV_MAXDRIVENAME); 438130389Sle 439130389Sle /* 440130389Sle * Find out which subdisks belong to this drive and crosslink 441130389Sle * them. 442130389Sle */ 443130389Sle LIST_FOREACH(s, &sc->subdisks, sd) { 444130389Sle if (!strncmp(s->drive, d->name, GV_MAXDRIVENAME)) 445130389Sle /* XXX: errors ignored */ 446130389Sle gv_sd_to_drive(sc, d, s, errstr, 447130389Sle sizeof(errstr)); 448130389Sle } 449130389Sle 450130389Sle /* This drive is now up for sure. */ 451130389Sle gv_set_drive_state(d, GV_DRIVE_UP, 0); 452130389Sle 453130389Sle /* 454130389Sle * If there are subdisks on this drive, we need to create 455130389Sle * providers for them. 456130389Sle */ 457130389Sle if (d->sdcount) 458130389Sle gv_drive_modify(d); 459130389Sle 460130389Sle return (gp); 461130389Sle 462130389Sle } while (0); 463130389Sle 464130389Sle g_topology_lock(); 465130389Sle g_access(cp, -1, 0, 0); 466130389Sle 467130389Sle g_detach(cp); 468130389Sle g_destroy_consumer(cp); 469130389Sle g_destroy_geom(gp); 470130389Sle return (NULL); 471130389Sle} 472130389Sle 473130389Sle/* 474130389Sle * Modify the providers for the given drive 'd'. It is assumed that the 475130389Sle * subdisk list of 'd' is already correctly set up. 476130389Sle */ 477130389Slevoid 478130389Slegv_drive_modify(struct gv_drive *d) 479130389Sle{ 480130389Sle struct g_geom *gp; 481130389Sle struct g_consumer *cp; 482130389Sle struct g_provider *pp, *pp2; 483130389Sle struct gv_sd *s; 484130389Sle int nsd; 485130389Sle 486130389Sle KASSERT(d != NULL, ("gv_drive_modify: null d")); 487130389Sle gp = d->geom; 488130389Sle KASSERT(gp != NULL, ("gv_drive_modify: null gp")); 489130389Sle cp = LIST_FIRST(&gp->consumer); 490130389Sle KASSERT(cp != NULL, ("gv_drive_modify: null cp")); 491130389Sle pp = cp->provider; 492130389Sle KASSERT(pp != NULL, ("gv_drive_modify: null pp")); 493130389Sle 494130389Sle g_topology_assert(); 495130389Sle 496130389Sle nsd = 0; 497130389Sle LIST_FOREACH(s, &d->subdisks, from_drive) { 498130389Sle /* This subdisk already has a provider. */ 499130389Sle if (s->provider != NULL) 500130389Sle continue; 501130389Sle pp2 = g_new_providerf(gp, "gvinum/sd/%s", s->name); 502130389Sle pp2->mediasize = s->size; 503130389Sle pp2->sectorsize = pp->sectorsize; 504130389Sle g_error_provider(pp2, 0); 505130389Sle s->provider = pp2; 506130389Sle pp2->private = s; 507130389Sle } 508130389Sle} 509130389Sle 510130389Slestatic int 511130389Slegv_drive_destroy_geom(struct gctl_req *req, struct g_class *mp, 512130389Sle struct g_geom *gp) 513130389Sle{ 514130389Sle g_trace(G_T_TOPOLOGY, "gv_drive_destroy_geom: %s", gp->name); 515130389Sle g_topology_assert(); 516130389Sle 517130389Sle g_wither_geom(gp, ENXIO); 518130389Sle return (0); 519130389Sle} 520130389Sle 521130389Sle#define VINUMDRIVE_CLASS_NAME "VINUMDRIVE" 522130389Sle 523130389Slestatic struct g_class g_vinum_drive_class = { 524130389Sle .name = VINUMDRIVE_CLASS_NAME, 525133318Sphk .version = G_VERSION, 526130389Sle .taste = gv_drive_taste, 527130389Sle .destroy_geom = gv_drive_destroy_geom 528130389Sle}; 529130389Sle 530130389SleDECLARE_GEOM_CLASS(g_vinum_drive_class, g_vinum_drive); 531