geom_vinum_drive.c revision 135162
1276789Sdim/*- 2276789Sdim * Copyright (c) 2004 Lukas Ertl 3276789Sdim * All rights reserved. 4276789Sdim * 5276789Sdim * Redistribution and use in source and binary forms, with or without 6276789Sdim * modification, are permitted provided that the following conditions 7276789Sdim * are met: 8276789Sdim * 1. Redistributions of source code must retain the above copyright 9276789Sdim * notice, this list of conditions and the following disclaimer. 10276789Sdim * 2. Redistributions in binary form must reproduce the above copyright 11276789Sdim * notice, this list of conditions and the following disclaimer in the 12276789Sdim * documentation and/or other materials provided with the distribution. 13276789Sdim * 14276789Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15276789Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16276789Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17276789Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18276789Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19276789Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20276789Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21276789Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22276789Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23276789Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24276789Sdim * SUCH DAMAGE. 25276789Sdim */ 26276789Sdim 27276789Sdim#include <sys/cdefs.h> 28276789Sdim__FBSDID("$FreeBSD: head/sys/geom/vinum/geom_vinum_drive.c 135162 2004-09-13 17:33:52Z le $"); 29276789Sdim 30276789Sdim#include <sys/param.h> 31276789Sdim#include <sys/bio.h> 32276789Sdim#include <sys/errno.h> 33276789Sdim#include <sys/conf.h> 34276789Sdim#include <sys/kernel.h> 35276789Sdim#include <sys/kthread.h> 36276789Sdim#include <sys/libkern.h> 37276789Sdim#include <sys/lock.h> 38276789Sdim#include <sys/malloc.h> 39276789Sdim#include <sys/module.h> 40276789Sdim#include <sys/mutex.h> 41276789Sdim#include <sys/sbuf.h> 42276789Sdim#include <sys/systm.h> 43276789Sdim#include <sys/time.h> 44276789Sdim 45276789Sdim#include <geom/geom.h> 46276789Sdim#include <geom/vinum/geom_vinum_var.h> 47276789Sdim#include <geom/vinum/geom_vinum.h> 48276789Sdim#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, 322 GV_SETSTATE_FORCE | GV_SETSTATE_CONFIG); 323 } 324 gp->softc = NULL; 325 g_wither_geom(gp, error); 326} 327 328static struct g_geom * 329gv_drive_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 330{ 331 struct g_geom *gp, *gp2; 332 struct g_consumer *cp; 333 struct gv_drive *d; 334 struct gv_sd *s; 335 struct gv_softc *sc; 336 struct gv_freelist *fl; 337 struct gv_hdr *vhdr; 338 int error; 339 char *buf, errstr[ERRBUFSIZ]; 340 341 vhdr = NULL; 342 d = NULL; 343 344 g_trace(G_T_TOPOLOGY, "gv_drive_taste(%s, %s)", mp->name, pp->name); 345 g_topology_assert(); 346 347 if (pp->sectorsize == 0) 348 return(NULL); 349 350 /* Find the VINUM class and its associated geom. */ 351 gp2 = find_vinum_geom(); 352 if (gp2 == NULL) 353 return (NULL); 354 sc = gp2->softc; 355 356 gp = g_new_geomf(mp, "%s.vinumdrive", pp->name); 357 gp->start = gv_drive_start; 358 gp->orphan = gv_drive_orphan; 359 gp->access = gv_drive_access; 360 gp->start = gv_drive_start; 361 362 cp = g_new_consumer(gp); 363 g_attach(cp, pp); 364 error = g_access(cp, 1, 0, 0); 365 if (error) { 366 g_detach(cp); 367 g_destroy_consumer(cp); 368 g_destroy_geom(gp); 369 return (NULL); 370 } 371 372 g_topology_unlock(); 373 374 /* Now check if the provided slice is a valid vinum drive. */ 375 do { 376 vhdr = g_read_data(cp, GV_HDR_OFFSET, pp->sectorsize, &error); 377 if (vhdr == NULL || error != 0) 378 break; 379 if (vhdr->magic != GV_MAGIC) { 380 g_free(vhdr); 381 break; 382 } 383 384 /* 385 * We have found a valid vinum drive. Let's see if it is 386 * already known in the configuration. There's a chance that 387 * the VINUMDRIVE class tastes before the VINUM class could 388 * taste, so parse the configuration here too, just to be on 389 * the safe side. 390 */ 391 buf = g_read_data(cp, GV_CFG_OFFSET, GV_CFG_LEN, &error); 392 if (buf == NULL || error != 0) { 393 g_free(vhdr); 394 break; 395 } 396 g_topology_lock(); 397 gv_parse_config(sc, buf, 1); 398 g_free(buf); 399 400 d = gv_find_drive(sc, vhdr->label.name); 401 402 /* We already know about this drive. */ 403 if (d != NULL) { 404 /* Check if this drive already has a geom. */ 405 if (d->geom != NULL) { 406 g_topology_unlock(); 407 break; 408 } 409 bcopy(vhdr, d->hdr, sizeof(*vhdr)); 410 411 /* This is a new drive. */ 412 } else { 413 d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO); 414 415 /* Initialize all needed variables. */ 416 d->size = pp->mediasize - GV_DATA_START; 417 d->avail = d->size; 418 d->hdr = vhdr; 419 strncpy(d->name, vhdr->label.name, GV_MAXDRIVENAME); 420 LIST_INIT(&d->subdisks); 421 LIST_INIT(&d->freelist); 422 423 /* We also need a freelist entry. */ 424 fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO); 425 fl->offset = GV_DATA_START; 426 fl->size = d->avail; 427 LIST_INSERT_HEAD(&d->freelist, fl, freelist); 428 d->freelist_entries = 1; 429 430 /* Save it into the main configuration. */ 431 LIST_INSERT_HEAD(&sc->drives, d, drive); 432 } 433 434 g_access(cp, -1, 0, 0); 435 436 gp->softc = d; 437 d->geom = gp; 438 strncpy(d->device, pp->name, GV_MAXDRIVENAME); 439 440 /* 441 * Find out which subdisks belong to this drive and crosslink 442 * them. 443 */ 444 LIST_FOREACH(s, &sc->subdisks, sd) { 445 if (!strncmp(s->drive, d->name, GV_MAXDRIVENAME)) 446 /* XXX: errors ignored */ 447 gv_sd_to_drive(sc, d, s, errstr, 448 sizeof(errstr)); 449 } 450 451 /* This drive is now up for sure. */ 452 gv_set_drive_state(d, GV_DRIVE_UP, 0); 453 454 /* 455 * If there are subdisks on this drive, we need to create 456 * providers for them. 457 */ 458 if (d->sdcount) 459 gv_drive_modify(d); 460 461 return (gp); 462 463 } while (0); 464 465 g_topology_lock(); 466 g_access(cp, -1, 0, 0); 467 468 g_detach(cp); 469 g_destroy_consumer(cp); 470 g_destroy_geom(gp); 471 return (NULL); 472} 473 474/* 475 * Modify the providers for the given drive 'd'. It is assumed that the 476 * subdisk list of 'd' is already correctly set up. 477 */ 478void 479gv_drive_modify(struct gv_drive *d) 480{ 481 struct g_geom *gp; 482 struct g_consumer *cp; 483 struct g_provider *pp, *pp2; 484 struct gv_sd *s; 485 int nsd; 486 487 KASSERT(d != NULL, ("gv_drive_modify: null d")); 488 gp = d->geom; 489 KASSERT(gp != NULL, ("gv_drive_modify: null gp")); 490 cp = LIST_FIRST(&gp->consumer); 491 KASSERT(cp != NULL, ("gv_drive_modify: null cp")); 492 pp = cp->provider; 493 KASSERT(pp != NULL, ("gv_drive_modify: null pp")); 494 495 g_topology_assert(); 496 497 nsd = 0; 498 LIST_FOREACH(s, &d->subdisks, from_drive) { 499 /* This subdisk already has a provider. */ 500 if (s->provider != NULL) 501 continue; 502 pp2 = g_new_providerf(gp, "gvinum/sd/%s", s->name); 503 pp2->mediasize = s->size; 504 pp2->sectorsize = pp->sectorsize; 505 g_error_provider(pp2, 0); 506 s->provider = pp2; 507 pp2->private = s; 508 } 509} 510 511static int 512gv_drive_destroy_geom(struct gctl_req *req, struct g_class *mp, 513 struct g_geom *gp) 514{ 515 g_trace(G_T_TOPOLOGY, "gv_drive_destroy_geom: %s", gp->name); 516 g_topology_assert(); 517 518 g_wither_geom(gp, ENXIO); 519 return (0); 520} 521 522#define VINUMDRIVE_CLASS_NAME "VINUMDRIVE" 523 524static struct g_class g_vinum_drive_class = { 525 .name = VINUMDRIVE_CLASS_NAME, 526 .version = G_VERSION, 527 .taste = gv_drive_taste, 528 .destroy_geom = gv_drive_destroy_geom 529}; 530 531DECLARE_GEOM_CLASS(g_vinum_drive_class, g_vinum_drive); 532