geom_vinum_drive.c revision 130478
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 130478 2004-06-14 17:12:32Z 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 /* On first open, grab an extra "exclusive" bit */ 185 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 186 de++; 187 /* ... and let go of it on last close */ 188 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 189 de--; 190 error = g_access(cp, dr, dw, de); 191 if (error) { 192 printf("FOOO: g_access failed: %d\n", error); 193 } 194 return (error); 195} 196 197static void 198gv_drive_start(struct bio *bp) 199{ 200 struct bio *bp2; 201 struct g_geom *gp; 202 struct g_consumer *cp; 203 struct g_provider *pp; 204 struct gv_drive *d; 205 struct gv_sd *s; 206 207 pp = bp->bio_to; 208 gp = pp->geom; 209 cp = LIST_FIRST(&gp->consumer); 210 d = gp->softc; 211 s = pp->private; 212 213 if ((s->state == GV_SD_DOWN) || (s->state == GV_SD_STALE)) { 214 g_io_deliver(bp, ENXIO); 215 return; 216 } 217 218 switch(bp->bio_cmd) { 219 case BIO_READ: 220 case BIO_WRITE: 221 case BIO_DELETE: 222 if (bp->bio_offset > s->size) { 223 g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */ 224 return; 225 } 226 bp2 = g_clone_bio(bp); 227 if (bp2 == NULL) { 228 g_io_deliver(bp, ENOMEM); 229 return; 230 } 231 if (bp2->bio_offset + bp2->bio_length > s->size) 232 bp2->bio_length = s->size - bp2->bio_offset; 233 bp2->bio_done = g_std_done; 234 bp2->bio_offset += s->drive_offset; 235 g_io_request(bp2, cp); 236 return; 237 238 case BIO_GETATTR: 239 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { 240 struct g_kerneldump *gkd; 241 242 gkd = (struct g_kerneldump *)bp->bio_data; 243 gkd->offset += s->drive_offset; 244 if (gkd->length > s->size) 245 gkd->length = s->size; 246 /* now, pass it on downwards... */ 247 } 248 bp2 = g_clone_bio(bp); 249 if (bp2 == NULL) { 250 g_io_deliver(bp, ENOMEM); 251 return; 252 } 253 bp2->bio_done = g_std_done; 254 g_io_request(bp2, cp); 255 return; 256 257 default: 258 g_io_deliver(bp, EOPNOTSUPP); 259 return; 260 } 261} 262 263static void 264gv_drive_orphan(struct g_consumer *cp) 265{ 266 struct g_geom *gp; 267 int error; 268 269 g_topology_assert(); 270 gp = cp->geom; 271 g_trace(G_T_TOPOLOGY, "gv_drive_orphan(%s)", gp->name); 272 if (cp->acr != 0 || cp->acw != 0 || cp->ace != 0) 273 g_access(cp, -cp->acr, -cp->acw, -cp->ace); 274 error = cp->provider->error; 275 if (error == 0) 276 error = ENXIO; 277 g_detach(cp); 278 g_destroy_consumer(cp); 279 if (!LIST_EMPTY(&gp->consumer)) 280 return; 281 g_wither_geom(gp, error); 282} 283 284static struct g_geom * 285gv_drive_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 286{ 287 struct g_geom *gp, *gp2; 288 struct g_consumer *cp; 289 struct gv_drive *d; 290 struct gv_sd *s; 291 struct gv_softc *sc; 292 struct gv_freelist *fl; 293 struct gv_hdr *vhdr; 294 int error; 295 char errstr[ERRBUFSIZ]; 296 297 vhdr = NULL; 298 d = NULL; 299 300 g_trace(G_T_TOPOLOGY, "gv_drive_taste(%s, %s)", mp->name, pp->name); 301 g_topology_assert(); 302 303 if (pp->sectorsize == 0) 304 return(NULL); 305 306 /* Find the VINUM class and its associated geom. */ 307 gp2 = find_vinum_geom(); 308 if (gp2 == NULL) 309 return (NULL); 310 sc = gp2->softc; 311 312 gp = g_new_geomf(mp, "%s.vinumdrive", pp->name); 313 gp->start = gv_drive_start; 314 gp->spoiled = gv_drive_orphan; 315 gp->orphan = gv_drive_orphan; 316 gp->access = gv_drive_access; 317 gp->start = gv_drive_start; 318 319 cp = g_new_consumer(gp); 320 g_attach(cp, pp); 321 error = g_access(cp, 1, 0, 0); 322 if (error) { 323 g_detach(cp); 324 g_destroy_consumer(cp); 325 g_destroy_geom(gp); 326 return (NULL); 327 } 328 329 g_topology_unlock(); 330 331 /* Now check if the provided slice is a valid vinum drive. */ 332 do { 333 vhdr = g_read_data(cp, GV_HDR_OFFSET, GV_HDR_LEN, &error); 334 if (vhdr == NULL || error != 0) 335 break; 336 if (vhdr->magic != GV_MAGIC) { 337 g_free(vhdr); 338 break; 339 } 340 341 /* 342 * We have found a valid vinum drive. Let's see if it is 343 * already known in the configuration. 344 */ 345 g_topology_lock(); 346 g_access(cp, -1, 0, 0); 347 348 d = gv_find_drive(sc, vhdr->label.name); 349 350 /* We already know about this drive. */ 351 if (d != NULL) { 352 bcopy(vhdr, d->hdr, sizeof(*vhdr)); 353 354 /* This is a new drive. */ 355 } else { 356 d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO); 357 358 /* Initialize all needed variables. */ 359 d->size = pp->mediasize - GV_DATA_START; 360 d->avail = d->size; 361 d->hdr = vhdr; 362 strncpy(d->name, vhdr->label.name, GV_MAXDRIVENAME); 363 LIST_INIT(&d->subdisks); 364 LIST_INIT(&d->freelist); 365 366 /* We also need a freelist entry. */ 367 fl = g_malloc(sizeof(*fl), M_WAITOK | M_ZERO); 368 fl->offset = GV_DATA_START; 369 fl->size = d->avail; 370 LIST_INSERT_HEAD(&d->freelist, fl, freelist); 371 d->freelist_entries = 1; 372 373 /* Save it into the main configuration. */ 374 LIST_INSERT_HEAD(&sc->drives, d, drive); 375 } 376 377 gp->softc = d; 378 d->geom = gp; 379 strncpy(d->device, pp->name, GV_MAXDRIVENAME); 380 381 /* 382 * Find out which subdisks belong to this drive and crosslink 383 * them. 384 */ 385 LIST_FOREACH(s, &sc->subdisks, sd) { 386 if (!strncmp(s->drive, d->name, GV_MAXDRIVENAME)) 387 /* XXX: errors ignored */ 388 gv_sd_to_drive(sc, d, s, errstr, 389 sizeof(errstr)); 390 } 391 392 /* This drive is now up for sure. */ 393 gv_set_drive_state(d, GV_DRIVE_UP, 0); 394 395 /* 396 * If there are subdisks on this drive, we need to create 397 * providers for them. 398 */ 399 if (d->sdcount) 400 gv_drive_modify(d); 401 402 return (gp); 403 404 } while (0); 405 406 g_topology_lock(); 407 g_access(cp, -1, 0, 0); 408 409 g_detach(cp); 410 g_destroy_consumer(cp); 411 g_free(gp->softc); 412 g_destroy_geom(gp); 413 return (NULL); 414} 415 416/* 417 * Modify the providers for the given drive 'd'. It is assumed that the 418 * subdisk list of 'd' is already correctly set up. 419 */ 420void 421gv_drive_modify(struct gv_drive *d) 422{ 423 struct g_geom *gp; 424 struct g_consumer *cp; 425 struct g_provider *pp, *pp2; 426 struct gv_sd *s; 427 int nsd; 428 429 KASSERT(d != NULL, ("gv_drive_modify: null d")); 430 gp = d->geom; 431 KASSERT(gp != NULL, ("gv_drive_modify: null gp")); 432 cp = LIST_FIRST(&gp->consumer); 433 KASSERT(cp != NULL, ("gv_drive_modify: null cp")); 434 pp = cp->provider; 435 KASSERT(pp != NULL, ("gv_drive_modify: null pp")); 436 437 g_topology_assert(); 438 439 nsd = 0; 440 LIST_FOREACH(s, &d->subdisks, from_drive) { 441 /* This subdisk already has a provider. */ 442 if (s->provider != NULL) 443 continue; 444 pp2 = g_new_providerf(gp, "gvinum/sd/%s", s->name); 445 pp2->mediasize = s->size; 446 pp2->sectorsize = pp->sectorsize; 447 g_error_provider(pp2, 0); 448 s->provider = pp2; 449 pp2->private = s; 450 } 451} 452 453static int 454gv_drive_destroy_geom(struct gctl_req *req, struct g_class *mp, 455 struct g_geom *gp) 456{ 457 /*struct gv_drive *d;*/ 458 459 g_trace(G_T_TOPOLOGY, "gv_drive_destroy_geom: %s", gp->name); 460 g_topology_assert(); 461 462 /* g_free(sc); */ 463 g_wither_geom(gp, ENXIO); 464 return (0); 465} 466 467#define VINUMDRIVE_CLASS_NAME "VINUMDRIVE" 468 469static struct g_class g_vinum_drive_class = { 470 .name = VINUMDRIVE_CLASS_NAME, 471 .taste = gv_drive_taste, 472 .destroy_geom = gv_drive_destroy_geom 473}; 474 475DECLARE_GEOM_CLASS(g_vinum_drive_class, g_vinum_drive); 476