geom_vinum_drive.c revision 135161
1/*- 2 * Copyright (c) 2004 Lukas Ertl 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/geom/vinum/geom_vinum_drive.c 135161 2004-09-13 17:27:58Z le $"); 29 30#include <sys/param.h> 31#include <sys/bio.h> 32#include <sys/errno.h> 33#include <sys/conf.h> 34#include <sys/kernel.h> 35#include <sys/kthread.h> 36#include <sys/libkern.h> 37#include <sys/lock.h> 38#include <sys/malloc.h> 39#include <sys/module.h> 40#include <sys/mutex.h> 41#include <sys/sbuf.h> 42#include <sys/systm.h> 43#include <sys/time.h> 44 45#include <geom/geom.h> 46#include <geom/vinum/geom_vinum_var.h> 47#include <geom/vinum/geom_vinum.h> 48#include <geom/vinum/geom_vinum_share.h> 49 50void gv_drive_modify(struct gv_drive *); 51 52void 53gv_config_new_drive(struct gv_drive *d) 54{ 55 struct gv_hdr *vhdr; 56 struct gv_freelist *fl; 57 58 KASSERT(d != NULL, ("config_new_drive: NULL d")); 59 60 vhdr = g_malloc(sizeof(*vhdr), M_WAITOK | M_ZERO); 61 vhdr->magic = GV_MAGIC; 62 vhdr->config_length = GV_CFG_LEN; 63 64 bcopy(hostname, vhdr->label.sysname, GV_HOSTNAME_LEN); 65 strncpy(vhdr->label.name, d->name, GV_MAXDRIVENAME); 66 microtime(&vhdr->label.date_of_birth); 67 68 d->hdr = vhdr; 69 70 LIST_INIT(&d->subdisks); 71 LIST_INIT(&d->freelist); 72 73 fl = g_malloc(sizeof(struct gv_freelist), M_WAITOK | M_ZERO); 74 fl->offset = GV_DATA_START; 75 fl->size = d->avail; 76 LIST_INSERT_HEAD(&d->freelist, fl, freelist); 77 d->freelist_entries = 1; 78} 79 80void 81gv_save_config_all(struct gv_softc *sc) 82{ 83 struct gv_drive *d; 84 85 g_topology_assert(); 86 87 LIST_FOREACH(d, &sc->drives, drive) { 88 if (d->geom == NULL) 89 continue; 90 gv_save_config(NULL, d, sc); 91 } 92} 93 94/* Save the vinum configuration back to disk. */ 95void 96gv_save_config(struct g_consumer *cp, struct gv_drive *d, struct gv_softc *sc) 97{ 98 struct g_geom *gp; 99 struct g_consumer *cp2; 100 struct gv_hdr *vhdr, *hdr; 101 struct sbuf *sb; 102 int error; 103 104 g_topology_assert(); 105 106 KASSERT(d != NULL, ("gv_save_config: null d")); 107 KASSERT(sc != NULL, ("gv_save_config: null sc")); 108 109 if (cp == NULL) { 110 gp = d->geom; 111 KASSERT(gp != NULL, ("gv_save_config: null gp")); 112 cp2 = LIST_FIRST(&gp->consumer); 113 KASSERT(cp2 != NULL, ("gv_save_config: null cp2")); 114 } else 115 cp2 = cp; 116 117 vhdr = g_malloc(GV_HDR_LEN, M_WAITOK | M_ZERO); 118 vhdr->magic = GV_MAGIC; 119 vhdr->config_length = GV_CFG_LEN; 120 121 hdr = d->hdr; 122 if (hdr == NULL) { 123 printf("NULL hdr!!!\n"); 124 g_free(vhdr); 125 return; 126 } 127 microtime(&hdr->label.last_update); 128 bcopy(&hdr->label, &vhdr->label, sizeof(struct gv_label)); 129 130 sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN); 131 gv_format_config(sc, sb, 1, NULL); 132 sbuf_finish(sb); 133 134 error = g_access(cp2, 0, 1, 0); 135 if (error) { 136 printf("g_access failed: %d\n", error); 137 sbuf_delete(sb); 138 return; 139 } 140 g_topology_unlock(); 141 142 do { 143 error = g_write_data(cp2, GV_HDR_OFFSET, vhdr, GV_HDR_LEN); 144 if (error) { 145 printf("writing vhdr failed: %d", error); 146 break; 147 } 148 149 error = g_write_data(cp2, GV_CFG_OFFSET, sbuf_data(sb), 150 GV_CFG_LEN); 151 if (error) { 152 printf("writing first config copy failed: %d", error); 153 break; 154 } 155 156 error = g_write_data(cp2, GV_CFG_OFFSET + GV_CFG_LEN, 157 sbuf_data(sb), GV_CFG_LEN); 158 if (error) 159 printf("writing second config copy failed: %d", error); 160 } while (0); 161 162 g_topology_lock(); 163 g_access(cp2, 0, -1, 0); 164 sbuf_delete(sb); 165 g_free(vhdr); 166 167 if (d->geom != NULL) 168 gv_drive_modify(d); 169} 170 171/* This resembles g_slice_access(). */ 172static int 173gv_drive_access(struct g_provider *pp, int dr, int dw, int de) 174{ 175 struct g_geom *gp; 176 struct g_consumer *cp; 177 struct g_provider *pp2; 178 struct gv_drive *d; 179 struct gv_sd *s, *s2; 180 int error; 181 182 gp = pp->geom; 183 cp = LIST_FIRST(&gp->consumer); 184 KASSERT(cp != NULL, ("gv_drive_access: NULL cp")); 185 186 d = gp->softc; 187 188 s = pp->private; 189 KASSERT(s != NULL, ("gv_drive_access: NULL s")); 190 191 LIST_FOREACH(s2, &d->subdisks, from_drive) { 192 if (s == s2) 193 continue; 194 if (s->drive_offset + s->size <= s2->drive_offset) 195 continue; 196 if (s2->drive_offset + s2->size <= s->drive_offset) 197 continue; 198 199 /* Overlap. */ 200 pp2 = s2->provider; 201 KASSERT(s2 != NULL, ("gv_drive_access: NULL s2")); 202 if ((pp->acw + dw) > 0 && pp2->ace > 0) { 203 printf("FOOO: permission denied - e\n"); 204 return (EPERM); 205 } 206 if ((pp->ace + de) > 0 && pp2->acw > 0) { 207 printf("FOOO: permission denied - w\n"); 208 return (EPERM); 209 } 210 } 211 212#if 0 213 /* On first open, grab an extra "exclusive" bit */ 214 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 215 de++; 216 /* ... and let go of it on last close */ 217 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 218 de--; 219#endif 220 error = g_access(cp, dr, dw, de); 221 if (error) { 222 printf("FOOO: g_access failed: %d\n", error); 223 } 224 return (error); 225} 226 227static void 228gv_drive_start(struct bio *bp) 229{ 230 struct bio *bp2; 231 struct g_geom *gp; 232 struct g_consumer *cp; 233 struct g_provider *pp; 234 struct gv_drive *d; 235 struct gv_sd *s; 236 237 pp = bp->bio_to; 238 gp = pp->geom; 239 cp = LIST_FIRST(&gp->consumer); 240 d = gp->softc; 241 s = pp->private; 242 243 if ((s->state == GV_SD_DOWN) || (s->state == GV_SD_STALE)) { 244 g_io_deliver(bp, ENXIO); 245 return; 246 } 247 248 switch(bp->bio_cmd) { 249 case BIO_READ: 250 case BIO_WRITE: 251 case BIO_DELETE: 252 if (bp->bio_offset > s->size) { 253 g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */ 254 return; 255 } 256 bp2 = g_clone_bio(bp); 257 if (bp2 == NULL) { 258 g_io_deliver(bp, ENOMEM); 259 return; 260 } 261 if (bp2->bio_offset + bp2->bio_length > s->size) 262 bp2->bio_length = s->size - bp2->bio_offset; 263 bp2->bio_done = g_std_done; 264 bp2->bio_offset += s->drive_offset; 265 g_io_request(bp2, cp); 266 return; 267 268 case BIO_GETATTR: 269 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { 270 struct g_kerneldump *gkd; 271 272 gkd = (struct g_kerneldump *)bp->bio_data; 273 gkd->offset += s->drive_offset; 274 if (gkd->length > s->size) 275 gkd->length = s->size; 276 /* now, pass it on downwards... */ 277 } 278 bp2 = g_clone_bio(bp); 279 if (bp2 == NULL) { 280 g_io_deliver(bp, ENOMEM); 281 return; 282 } 283 bp2->bio_done = g_std_done; 284 g_io_request(bp2, cp); 285 return; 286 287 default: 288 g_io_deliver(bp, EOPNOTSUPP); 289 return; 290 } 291} 292 293static void 294gv_drive_orphan(struct g_consumer *cp) 295{ 296 struct g_geom *gp; 297 struct gv_drive *d; 298 struct gv_sd *s; 299 int error; 300 301 g_topology_assert(); 302 gp = cp->geom; 303 g_trace(G_T_TOPOLOGY, "gv_drive_orphan(%s)", gp->name); 304 if (cp->acr != 0 || cp->acw != 0 || cp->ace != 0) 305 g_access(cp, -cp->acr, -cp->acw, -cp->ace); 306 error = cp->provider->error; 307 if (error == 0) 308 error = ENXIO; 309 g_detach(cp); 310 g_destroy_consumer(cp); 311 if (!LIST_EMPTY(&gp->consumer)) 312 return; 313 d = gp->softc; 314 if (d != NULL) { 315 printf("gvinum: lost drive '%s'\n", d->name); 316 d->geom = NULL; 317 LIST_FOREACH(s, &d->subdisks, from_drive) { 318 s->provider = NULL; 319 s->consumer = NULL; 320 } 321 gv_set_drive_state(d, GV_DRIVE_DOWN, GV_SETSTATE_FORCE); 322 } 323 gp->softc = NULL; 324 g_wither_geom(gp, error); 325} 326 327static struct g_geom * 328gv_drive_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 329{ 330 struct g_geom *gp, *gp2; 331 struct g_consumer *cp; 332 struct gv_drive *d; 333 struct gv_sd *s; 334 struct gv_softc *sc; 335 struct gv_freelist *fl; 336 struct gv_hdr *vhdr; 337 int error; 338 char *buf, errstr[ERRBUFSIZ]; 339 340 vhdr = NULL; 341 d = NULL; 342 343 g_trace(G_T_TOPOLOGY, "gv_drive_taste(%s, %s)", mp->name, pp->name); 344 g_topology_assert(); 345 346 if (pp->sectorsize == 0) 347 return(NULL); 348 349 /* Find the VINUM class and its associated geom. */ 350 gp2 = find_vinum_geom(); 351 if (gp2 == NULL) 352 return (NULL); 353 sc = gp2->softc; 354 355 gp = g_new_geomf(mp, "%s.vinumdrive", pp->name); 356 gp->start = gv_drive_start; 357 gp->orphan = gv_drive_orphan; 358 gp->access = gv_drive_access; 359 gp->start = gv_drive_start; 360 361 cp = g_new_consumer(gp); 362 g_attach(cp, pp); 363 error = g_access(cp, 1, 0, 0); 364 if (error) { 365 g_detach(cp); 366 g_destroy_consumer(cp); 367 g_destroy_geom(gp); 368 return (NULL); 369 } 370 371 g_topology_unlock(); 372 373 /* Now check if the provided slice is a valid vinum drive. */ 374 do { 375 vhdr = g_read_data(cp, GV_HDR_OFFSET, pp->sectorsize, &error); 376 if (vhdr == NULL || error != 0) 377 break; 378 if (vhdr->magic != GV_MAGIC) { 379 g_free(vhdr); 380 break; 381 } 382 383 /* 384 * We have found a valid vinum drive. Let's see if it is 385 * already known in the configuration. There's a chance that 386 * the VINUMDRIVE class tastes before the VINUM class could 387 * taste, so parse the configuration here too, just to be on 388 * the safe side. 389 */ 390 buf = g_read_data(cp, GV_CFG_OFFSET, GV_CFG_LEN, &error); 391 if (buf == NULL || error != 0) { 392 g_free(vhdr); 393 break; 394 } 395 g_topology_lock(); 396 gv_parse_config(sc, buf, 1); 397 g_free(buf); 398 399 d = gv_find_drive(sc, vhdr->label.name); 400 401 /* We already know about this drive. */ 402 if (d != NULL) { 403 /* Check if this drive already has a geom. */ 404 if (d->geom != NULL) { 405 g_topology_unlock(); 406 break; 407 } 408 bcopy(vhdr, d->hdr, sizeof(*vhdr)); 409 410 /* This is a new drive. */ 411 } else { 412 d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO); 413 414 /* Initialize all needed variables. */ 415 d->size = pp->mediasize - GV_DATA_START; 416 d->avail = d->size; 417 d->hdr = vhdr; 418 strncpy(d->name, vhdr->label.name, GV_MAXDRIVENAME); 419 LIST_INIT(&d->subdisks); 420 LIST_INIT(&d->freelist); 421 422 /* We also need a freelist entry. */ 423 fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO); 424 fl->offset = GV_DATA_START; 425 fl->size = d->avail; 426 LIST_INSERT_HEAD(&d->freelist, fl, freelist); 427 d->freelist_entries = 1; 428 429 /* Save it into the main configuration. */ 430 LIST_INSERT_HEAD(&sc->drives, d, drive); 431 } 432 433 g_access(cp, -1, 0, 0); 434 435 gp->softc = d; 436 d->geom = gp; 437 strncpy(d->device, pp->name, GV_MAXDRIVENAME); 438 439 /* 440 * Find out which subdisks belong to this drive and crosslink 441 * them. 442 */ 443 LIST_FOREACH(s, &sc->subdisks, sd) { 444 if (!strncmp(s->drive, d->name, GV_MAXDRIVENAME)) 445 /* XXX: errors ignored */ 446 gv_sd_to_drive(sc, d, s, errstr, 447 sizeof(errstr)); 448 } 449 450 /* This drive is now up for sure. */ 451 gv_set_drive_state(d, GV_DRIVE_UP, 0); 452 453 /* 454 * If there are subdisks on this drive, we need to create 455 * providers for them. 456 */ 457 if (d->sdcount) 458 gv_drive_modify(d); 459 460 return (gp); 461 462 } while (0); 463 464 g_topology_lock(); 465 g_access(cp, -1, 0, 0); 466 467 g_detach(cp); 468 g_destroy_consumer(cp); 469 g_destroy_geom(gp); 470 return (NULL); 471} 472 473/* 474 * Modify the providers for the given drive 'd'. It is assumed that the 475 * subdisk list of 'd' is already correctly set up. 476 */ 477void 478gv_drive_modify(struct gv_drive *d) 479{ 480 struct g_geom *gp; 481 struct g_consumer *cp; 482 struct g_provider *pp, *pp2; 483 struct gv_sd *s; 484 int nsd; 485 486 KASSERT(d != NULL, ("gv_drive_modify: null d")); 487 gp = d->geom; 488 KASSERT(gp != NULL, ("gv_drive_modify: null gp")); 489 cp = LIST_FIRST(&gp->consumer); 490 KASSERT(cp != NULL, ("gv_drive_modify: null cp")); 491 pp = cp->provider; 492 KASSERT(pp != NULL, ("gv_drive_modify: null pp")); 493 494 g_topology_assert(); 495 496 nsd = 0; 497 LIST_FOREACH(s, &d->subdisks, from_drive) { 498 /* This subdisk already has a provider. */ 499 if (s->provider != NULL) 500 continue; 501 pp2 = g_new_providerf(gp, "gvinum/sd/%s", s->name); 502 pp2->mediasize = s->size; 503 pp2->sectorsize = pp->sectorsize; 504 g_error_provider(pp2, 0); 505 s->provider = pp2; 506 pp2->private = s; 507 } 508} 509 510static int 511gv_drive_destroy_geom(struct gctl_req *req, struct g_class *mp, 512 struct g_geom *gp) 513{ 514 g_trace(G_T_TOPOLOGY, "gv_drive_destroy_geom: %s", gp->name); 515 g_topology_assert(); 516 517 g_wither_geom(gp, ENXIO); 518 return (0); 519} 520 521#define VINUMDRIVE_CLASS_NAME "VINUMDRIVE" 522 523static struct g_class g_vinum_drive_class = { 524 .name = VINUMDRIVE_CLASS_NAME, 525 .version = G_VERSION, 526 .taste = gv_drive_taste, 527 .destroy_geom = gv_drive_destroy_geom 528}; 529 530DECLARE_GEOM_CLASS(g_vinum_drive_class, g_vinum_drive); 531