geom_vinum_drive.c revision 133983
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 133983 2004-08-18 20:34:45Z 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_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 struct g_geom * 300gv_drive_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 301{ 302 struct g_geom *gp, *gp2; 303 struct g_consumer *cp; 304 struct gv_drive *d; 305 struct gv_sd *s; 306 struct gv_softc *sc; 307 struct gv_freelist *fl; 308 struct gv_hdr *vhdr; 309 int error; 310 char *buf, errstr[ERRBUFSIZ]; 311 312 vhdr = NULL; 313 d = NULL; 314 315 g_trace(G_T_TOPOLOGY, "gv_drive_taste(%s, %s)", mp->name, pp->name); 316 g_topology_assert(); 317 318 if (pp->sectorsize == 0) 319 return(NULL); 320 321 /* Find the VINUM class and its associated geom. */ 322 gp2 = find_vinum_geom(); 323 if (gp2 == NULL) 324 return (NULL); 325 sc = gp2->softc; 326 327 gp = g_new_geomf(mp, "%s.vinumdrive", pp->name); 328 gp->start = gv_drive_start; 329 gp->orphan = gv_drive_orphan; 330 gp->access = gv_drive_access; 331 gp->start = gv_drive_start; 332 333 cp = g_new_consumer(gp); 334 g_attach(cp, pp); 335 error = g_access(cp, 1, 0, 0); 336 if (error) { 337 g_detach(cp); 338 g_destroy_consumer(cp); 339 g_destroy_geom(gp); 340 return (NULL); 341 } 342 343 g_topology_unlock(); 344 345 /* Now check if the provided slice is a valid vinum drive. */ 346 do { 347 vhdr = g_read_data(cp, GV_HDR_OFFSET, GV_HDR_LEN, &error); 348 if (vhdr == NULL || error != 0) 349 break; 350 if (vhdr->magic != GV_MAGIC) { 351 g_free(vhdr); 352 break; 353 } 354 355 /* 356 * We have found a valid vinum drive. Let's see if it is 357 * already known in the configuration. There's a chance that 358 * the VINUMDRIVE class tastes before the VINUM class could 359 * taste, so parse the configuration here too, just to be on 360 * the safe side. 361 */ 362 buf = g_read_data(cp, GV_CFG_OFFSET, GV_CFG_LEN, &error); 363 if (buf == NULL || error != 0) { 364 g_free(vhdr); 365 break; 366 } 367 g_topology_lock(); 368 gv_parse_config(sc, buf, 1); 369 g_free(buf); 370 371 d = gv_find_drive(sc, vhdr->label.name); 372 373 /* We already know about this drive. */ 374 if (d != NULL) { 375 /* Check if this drive already has a geom. */ 376 if (d->geom != NULL) { 377 g_topology_unlock(); 378 break; 379 } 380 bcopy(vhdr, d->hdr, sizeof(*vhdr)); 381 382 /* This is a new drive. */ 383 } else { 384 d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO); 385 386 /* Initialize all needed variables. */ 387 d->size = pp->mediasize - GV_DATA_START; 388 d->avail = d->size; 389 d->hdr = vhdr; 390 strncpy(d->name, vhdr->label.name, GV_MAXDRIVENAME); 391 LIST_INIT(&d->subdisks); 392 LIST_INIT(&d->freelist); 393 394 /* We also need a freelist entry. */ 395 fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO); 396 fl->offset = GV_DATA_START; 397 fl->size = d->avail; 398 LIST_INSERT_HEAD(&d->freelist, fl, freelist); 399 d->freelist_entries = 1; 400 401 /* Save it into the main configuration. */ 402 LIST_INSERT_HEAD(&sc->drives, d, drive); 403 } 404 405 g_access(cp, -1, 0, 0); 406 407 gp->softc = d; 408 d->geom = gp; 409 strncpy(d->device, pp->name, GV_MAXDRIVENAME); 410 411 /* 412 * Find out which subdisks belong to this drive and crosslink 413 * them. 414 */ 415 LIST_FOREACH(s, &sc->subdisks, sd) { 416 if (!strncmp(s->drive, d->name, GV_MAXDRIVENAME)) 417 /* XXX: errors ignored */ 418 gv_sd_to_drive(sc, d, s, errstr, 419 sizeof(errstr)); 420 } 421 422 /* This drive is now up for sure. */ 423 gv_set_drive_state(d, GV_DRIVE_UP, 0); 424 425 /* 426 * If there are subdisks on this drive, we need to create 427 * providers for them. 428 */ 429 if (d->sdcount) 430 gv_drive_modify(d); 431 432 return (gp); 433 434 } while (0); 435 436 g_topology_lock(); 437 g_access(cp, -1, 0, 0); 438 439 g_detach(cp); 440 g_destroy_consumer(cp); 441 g_destroy_geom(gp); 442 return (NULL); 443} 444 445/* 446 * Modify the providers for the given drive 'd'. It is assumed that the 447 * subdisk list of 'd' is already correctly set up. 448 */ 449void 450gv_drive_modify(struct gv_drive *d) 451{ 452 struct g_geom *gp; 453 struct g_consumer *cp; 454 struct g_provider *pp, *pp2; 455 struct gv_sd *s; 456 int nsd; 457 458 KASSERT(d != NULL, ("gv_drive_modify: null d")); 459 gp = d->geom; 460 KASSERT(gp != NULL, ("gv_drive_modify: null gp")); 461 cp = LIST_FIRST(&gp->consumer); 462 KASSERT(cp != NULL, ("gv_drive_modify: null cp")); 463 pp = cp->provider; 464 KASSERT(pp != NULL, ("gv_drive_modify: null pp")); 465 466 g_topology_assert(); 467 468 nsd = 0; 469 LIST_FOREACH(s, &d->subdisks, from_drive) { 470 /* This subdisk already has a provider. */ 471 if (s->provider != NULL) 472 continue; 473 pp2 = g_new_providerf(gp, "gvinum/sd/%s", s->name); 474 pp2->mediasize = s->size; 475 pp2->sectorsize = pp->sectorsize; 476 g_error_provider(pp2, 0); 477 s->provider = pp2; 478 pp2->private = s; 479 } 480} 481 482static int 483gv_drive_destroy_geom(struct gctl_req *req, struct g_class *mp, 484 struct g_geom *gp) 485{ 486 g_trace(G_T_TOPOLOGY, "gv_drive_destroy_geom: %s", gp->name); 487 g_topology_assert(); 488 489 g_wither_geom(gp, ENXIO); 490 return (0); 491} 492 493#define VINUMDRIVE_CLASS_NAME "VINUMDRIVE" 494 495static struct g_class g_vinum_drive_class = { 496 .name = VINUMDRIVE_CLASS_NAME, 497 .version = G_VERSION, 498 .taste = gv_drive_taste, 499 .destroy_geom = gv_drive_destroy_geom 500}; 501 502DECLARE_GEOM_CLASS(g_vinum_drive_class, g_vinum_drive); 503