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