geom_vinum_drive.c revision 133318
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 133318 2004-08-08 07:57:53Z phk $"); 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_save_config_all(struct gv_softc *sc) 54{ 55 struct gv_drive *d; 56 57 g_topology_assert(); 58 59 LIST_FOREACH(d, &sc->drives, drive) { 60 if (d->geom == NULL) 61 continue; 62 gv_save_config(NULL, d, sc); 63 } 64} 65 66/* Save the vinum configuration back to disk. */ 67void 68gv_save_config(struct g_consumer *cp, struct gv_drive *d, struct gv_softc *sc) 69{ 70 struct g_geom *gp; 71 struct g_consumer *cp2; 72 struct gv_hdr *vhdr, *hdr; 73 struct sbuf *sb; 74 int error; 75 76 g_topology_assert(); 77 78 KASSERT(d != NULL, ("gv_save_config: null d")); 79 KASSERT(sc != NULL, ("gv_save_config: null sc")); 80 81 if (cp == NULL) { 82 gp = d->geom; 83 KASSERT(gp != NULL, ("gv_save_config: null gp")); 84 cp2 = LIST_FIRST(&gp->consumer); 85 KASSERT(cp2 != NULL, ("gv_save_config: null cp2")); 86 } else 87 cp2 = cp; 88 89 vhdr = g_malloc(GV_HDR_LEN, M_WAITOK | M_ZERO); 90 vhdr->magic = GV_MAGIC; 91 vhdr->config_length = GV_CFG_LEN; 92 93 hdr = d->hdr; 94 if (hdr == NULL) { 95 printf("NULL hdr!!!\n"); 96 g_free(vhdr); 97 return; 98 } 99 microtime(&hdr->label.last_update); 100 bcopy(&hdr->label, &vhdr->label, sizeof(struct gv_label)); 101 102 sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN); 103 gv_format_config(sc, sb, 1, NULL); 104 sbuf_finish(sb); 105 106 error = g_access(cp2, 0, 1, 0); 107 if (error) { 108 printf("g_access failed: %d\n", error); 109 sbuf_delete(sb); 110 return; 111 } 112 g_topology_unlock(); 113 114 do { 115 error = g_write_data(cp2, GV_HDR_OFFSET, vhdr, GV_HDR_LEN); 116 if (error) { 117 printf("writing vhdr failed: %d", error); 118 break; 119 } 120 121 error = g_write_data(cp2, GV_CFG_OFFSET, sbuf_data(sb), 122 GV_CFG_LEN); 123 if (error) { 124 printf("writing first config copy failed: %d", error); 125 break; 126 } 127 128 error = g_write_data(cp2, GV_CFG_OFFSET + GV_CFG_LEN, 129 sbuf_data(sb), GV_CFG_LEN); 130 if (error) 131 printf("writing second config copy failed: %d", error); 132 } while (0); 133 134 g_topology_lock(); 135 g_access(cp2, 0, -1, 0); 136 sbuf_delete(sb); 137 g_free(vhdr); 138 139 if (d->geom != NULL) 140 gv_drive_modify(d); 141} 142 143/* This resembles g_slice_access(). */ 144static int 145gv_drive_access(struct g_provider *pp, int dr, int dw, int de) 146{ 147 struct g_geom *gp; 148 struct g_consumer *cp; 149 struct g_provider *pp2; 150 struct gv_drive *d; 151 struct gv_sd *s, *s2; 152 int error; 153 154 gp = pp->geom; 155 cp = LIST_FIRST(&gp->consumer); 156 KASSERT(cp != NULL, ("gv_drive_access: NULL cp")); 157 158 d = gp->softc; 159 160 s = pp->private; 161 KASSERT(s != NULL, ("gv_drive_access: NULL s")); 162 163 LIST_FOREACH(s2, &d->subdisks, from_drive) { 164 if (s == s2) 165 continue; 166 if (s->drive_offset + s->size <= s2->drive_offset) 167 continue; 168 if (s2->drive_offset + s2->size <= s->drive_offset) 169 continue; 170 171 /* Overlap. */ 172 pp2 = s2->provider; 173 KASSERT(s2 != NULL, ("gv_drive_access: NULL s2")); 174 if ((pp->acw + dw) > 0 && pp2->ace > 0) { 175 printf("FOOO: permission denied - e\n"); 176 return (EPERM); 177 } 178 if ((pp->ace + de) > 0 && pp2->acw > 0) { 179 printf("FOOO: permission denied - w\n"); 180 return (EPERM); 181 } 182 } 183 184#if 0 185 /* On first open, grab an extra "exclusive" bit */ 186 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 187 de++; 188 /* ... and let go of it on last close */ 189 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 190 de--; 191#endif 192 error = g_access(cp, dr, dw, de); 193 if (error) { 194 printf("FOOO: g_access failed: %d\n", error); 195 } 196 return (error); 197} 198 199static void 200gv_drive_start(struct bio *bp) 201{ 202 struct bio *bp2; 203 struct g_geom *gp; 204 struct g_consumer *cp; 205 struct g_provider *pp; 206 struct gv_drive *d; 207 struct gv_sd *s; 208 209 pp = bp->bio_to; 210 gp = pp->geom; 211 cp = LIST_FIRST(&gp->consumer); 212 d = gp->softc; 213 s = pp->private; 214 215 if ((s->state == GV_SD_DOWN) || (s->state == GV_SD_STALE)) { 216 g_io_deliver(bp, ENXIO); 217 return; 218 } 219 220 switch(bp->bio_cmd) { 221 case BIO_READ: 222 case BIO_WRITE: 223 case BIO_DELETE: 224 if (bp->bio_offset > s->size) { 225 g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */ 226 return; 227 } 228 bp2 = g_clone_bio(bp); 229 if (bp2 == NULL) { 230 g_io_deliver(bp, ENOMEM); 231 return; 232 } 233 if (bp2->bio_offset + bp2->bio_length > s->size) 234 bp2->bio_length = s->size - bp2->bio_offset; 235 bp2->bio_done = g_std_done; 236 bp2->bio_offset += s->drive_offset; 237 g_io_request(bp2, cp); 238 return; 239 240 case BIO_GETATTR: 241 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { 242 struct g_kerneldump *gkd; 243 244 gkd = (struct g_kerneldump *)bp->bio_data; 245 gkd->offset += s->drive_offset; 246 if (gkd->length > s->size) 247 gkd->length = s->size; 248 /* now, pass it on downwards... */ 249 } 250 bp2 = g_clone_bio(bp); 251 if (bp2 == NULL) { 252 g_io_deliver(bp, ENOMEM); 253 return; 254 } 255 bp2->bio_done = g_std_done; 256 g_io_request(bp2, cp); 257 return; 258 259 default: 260 g_io_deliver(bp, EOPNOTSUPP); 261 return; 262 } 263} 264 265static void 266gv_drive_orphan(struct g_consumer *cp) 267{ 268 struct g_geom *gp; 269 struct gv_drive *d; 270 struct gv_sd *s; 271 int error; 272 273 g_topology_assert(); 274 gp = cp->geom; 275 g_trace(G_T_TOPOLOGY, "gv_drive_orphan(%s)", gp->name); 276 if (cp->acr != 0 || cp->acw != 0 || cp->ace != 0) 277 g_access(cp, -cp->acr, -cp->acw, -cp->ace); 278 error = cp->provider->error; 279 if (error == 0) 280 error = ENXIO; 281 g_detach(cp); 282 g_destroy_consumer(cp); 283 if (!LIST_EMPTY(&gp->consumer)) 284 return; 285 d = gp->softc; 286 if (d != NULL) { 287 printf("gvinum: lost drive '%s'\n", d->name); 288 d->geom = NULL; 289 LIST_FOREACH(s, &d->subdisks, from_drive) { 290 s->provider = NULL; 291 s->consumer = NULL; 292 } 293 gv_set_drive_state(d, GV_DRIVE_DOWN, GV_SETSTATE_FORCE); 294 } 295 gp->softc = NULL; 296 g_wither_geom(gp, error); 297} 298 299static void 300gv_drive_taste_orphan(struct g_consumer *cp) 301{ 302 KASSERT(1 == 0, ("gv_drive_taste_orphan called: %s", cp->geom->name)); 303} 304 305static struct g_geom * 306gv_drive_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 307{ 308 struct g_geom *gp, *gp2; 309 struct g_consumer *cp; 310 struct gv_drive *d; 311 struct gv_sd *s; 312 struct gv_softc *sc; 313 struct gv_freelist *fl; 314 struct gv_hdr *vhdr; 315 int error; 316 char *buf, errstr[ERRBUFSIZ]; 317 318 vhdr = NULL; 319 d = NULL; 320 321 g_trace(G_T_TOPOLOGY, "gv_drive_taste(%s, %s)", mp->name, pp->name); 322 g_topology_assert(); 323 324 if (pp->sectorsize == 0) 325 return(NULL); 326 327 /* Find the VINUM class and its associated geom. */ 328 gp2 = find_vinum_geom(); 329 if (gp2 == NULL) 330 return (NULL); 331 sc = gp2->softc; 332 333 gp = g_new_geomf(mp, "%s.vinumdrive", pp->name); 334 gp->orphan = gv_drive_taste_orphan; 335 336 cp = g_new_consumer(gp); 337 g_attach(cp, pp); 338 error = g_access(cp, 1, 0, 0); 339 if (error) { 340 g_detach(cp); 341 g_destroy_consumer(cp); 342 g_destroy_geom(gp); 343 return (NULL); 344 } 345 346 g_topology_unlock(); 347 348 /* Now check if the provided slice is a valid vinum drive. */ 349 do { 350 vhdr = g_read_data(cp, GV_HDR_OFFSET, GV_HDR_LEN, &error); 351 if (vhdr == NULL || error != 0) 352 break; 353 if (vhdr->magic != GV_MAGIC) { 354 g_free(vhdr); 355 break; 356 } 357 358 /* 359 * We have found a valid vinum drive. Let's see if it is 360 * already known in the configuration. There's a chance that 361 * the VINUMDRIVE class tastes before the VINUM class could 362 * taste, so parse the configuration here too, just to be on 363 * the safe side. 364 */ 365 buf = g_read_data(cp, GV_CFG_OFFSET, GV_CFG_LEN, &error); 366 if (buf == NULL || error != 0) { 367 g_free(vhdr); 368 break; 369 } 370 gv_parse_config(sc, buf, 1); 371 g_free(buf); 372 373 g_topology_lock(); 374 g_access(cp, -1, 0, 0); 375 g_detach(cp); 376 g_wither_geom(gp, ENXIO); 377 gp = NULL; 378 379 d = gv_find_drive(sc, vhdr->label.name); 380 381 /* We already know about this drive. */ 382 if (d != NULL) { 383 bcopy(vhdr, d->hdr, sizeof(*vhdr)); 384 385 /* This is a new drive. */ 386 } else { 387 d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO); 388 389 /* Initialize all needed variables. */ 390 d->size = pp->mediasize - GV_DATA_START; 391 d->avail = d->size; 392 d->hdr = vhdr; 393 strncpy(d->name, vhdr->label.name, GV_MAXDRIVENAME); 394 LIST_INIT(&d->subdisks); 395 LIST_INIT(&d->freelist); 396 397 /* We also need a freelist entry. */ 398 fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO); 399 fl->offset = GV_DATA_START; 400 fl->size = d->avail; 401 LIST_INSERT_HEAD(&d->freelist, fl, freelist); 402 d->freelist_entries = 1; 403 404 /* Save it into the main configuration. */ 405 LIST_INSERT_HEAD(&sc->drives, d, drive); 406 } 407 408 gp = g_new_geomf(mp, "%s.vinumdrive", pp->name); 409 gp->start = gv_drive_start; 410 gp->orphan = gv_drive_orphan; 411 gp->access = gv_drive_access; 412 gp->start = gv_drive_start; 413 414 cp = g_new_consumer(gp); 415 g_attach(cp, pp); 416 error = g_access(cp, 1, 1, 1); 417 if (error) { 418 g_free(vhdr); 419 g_detach(cp); 420 g_destroy_consumer(cp); 421 g_destroy_geom(gp); 422 return (NULL); 423 } 424 425 gp->softc = d; 426 d->geom = gp; 427 strncpy(d->device, pp->name, GV_MAXDRIVENAME); 428 429 /* 430 * Find out which subdisks belong to this drive and crosslink 431 * them. 432 */ 433 LIST_FOREACH(s, &sc->subdisks, sd) { 434 if (!strncmp(s->drive, d->name, GV_MAXDRIVENAME)) 435 /* XXX: errors ignored */ 436 gv_sd_to_drive(sc, d, s, errstr, 437 sizeof(errstr)); 438 } 439 440 /* This drive is now up for sure. */ 441 gv_set_drive_state(d, GV_DRIVE_UP, 0); 442 443 /* 444 * If there are subdisks on this drive, we need to create 445 * providers for them. 446 */ 447 if (d->sdcount) 448 gv_drive_modify(d); 449 450 return (gp); 451 452 } while (0); 453 454 g_topology_lock(); 455 g_access(cp, -1, 0, 0); 456 457 g_detach(cp); 458 g_destroy_consumer(cp); 459 g_destroy_geom(gp); 460 return (NULL); 461} 462 463/* 464 * Modify the providers for the given drive 'd'. It is assumed that the 465 * subdisk list of 'd' is already correctly set up. 466 */ 467void 468gv_drive_modify(struct gv_drive *d) 469{ 470 struct g_geom *gp; 471 struct g_consumer *cp; 472 struct g_provider *pp, *pp2; 473 struct gv_sd *s; 474 int nsd; 475 476 KASSERT(d != NULL, ("gv_drive_modify: null d")); 477 gp = d->geom; 478 KASSERT(gp != NULL, ("gv_drive_modify: null gp")); 479 cp = LIST_FIRST(&gp->consumer); 480 KASSERT(cp != NULL, ("gv_drive_modify: null cp")); 481 pp = cp->provider; 482 KASSERT(pp != NULL, ("gv_drive_modify: null pp")); 483 484 g_topology_assert(); 485 486 nsd = 0; 487 LIST_FOREACH(s, &d->subdisks, from_drive) { 488 /* This subdisk already has a provider. */ 489 if (s->provider != NULL) 490 continue; 491 pp2 = g_new_providerf(gp, "gvinum/sd/%s", s->name); 492 pp2->mediasize = s->size; 493 pp2->sectorsize = pp->sectorsize; 494 g_error_provider(pp2, 0); 495 s->provider = pp2; 496 pp2->private = s; 497 } 498} 499 500static int 501gv_drive_destroy_geom(struct gctl_req *req, struct g_class *mp, 502 struct g_geom *gp) 503{ 504 g_trace(G_T_TOPOLOGY, "gv_drive_destroy_geom: %s", gp->name); 505 g_topology_assert(); 506 507 g_wither_geom(gp, ENXIO); 508 return (0); 509} 510 511#define VINUMDRIVE_CLASS_NAME "VINUMDRIVE" 512 513static struct g_class g_vinum_drive_class = { 514 .name = VINUMDRIVE_CLASS_NAME, 515 .version = G_VERSION, 516 .taste = gv_drive_taste, 517 .destroy_geom = gv_drive_destroy_geom 518}; 519 520DECLARE_GEOM_CLASS(g_vinum_drive_class, g_vinum_drive); 521