geom_slice.c revision 131408
1105109Smike/*- 21541Srgrimes * Copyright (c) 2002 Poul-Henning Kamp 31541Srgrimes * Copyright (c) 2002 Networks Associates Technology, Inc. 41541Srgrimes * All rights reserved. 51541Srgrimes * 61541Srgrimes * This software was developed for the FreeBSD Project by Poul-Henning Kamp 71541Srgrimes * and NAI Labs, the Security Research Division of Network Associates, Inc. 81541Srgrimes * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 91541Srgrimes * DARPA CHATS research program. 101541Srgrimes * 111541Srgrimes * Redistribution and use in source and binary forms, with or without 121541Srgrimes * modification, are permitted provided that the following conditions 131541Srgrimes * are met: 141541Srgrimes * 1. Redistributions of source code must retain the above copyright 151541Srgrimes * notice, this list of conditions and the following disclaimer. 161541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 171541Srgrimes * notice, this list of conditions and the following disclaimer in the 181541Srgrimes * documentation and/or other materials provided with the distribution. 191541Srgrimes * 3. The names of the authors may not be used to endorse or promote 201541Srgrimes * products derived from this software without specific prior written 211541Srgrimes * permission. 221541Srgrimes * 231541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 241541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 251541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 261541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 271541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 281541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 291541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 301541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 311541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 321541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 331541Srgrimes * SUCH DAMAGE. 341541Srgrimes */ 351541Srgrimes 361541Srgrimes#include <sys/cdefs.h> 371541Srgrimes__FBSDID("$FreeBSD: head/sys/geom/geom_slice.c 131408 2004-07-01 12:42:13Z pjd $"); 3814486Shsu 3950477Speter#include <sys/param.h> 401541Srgrimes#include <sys/systm.h> 411541Srgrimes#include <sys/kernel.h> 42105109Smike#include <sys/malloc.h> 431541Srgrimes#include <sys/bio.h> 441541Srgrimes#include <sys/sysctl.h> 459343Sbde#include <sys/proc.h> 46107376Smike#include <sys/kthread.h> 4798301Swollman#include <sys/lock.h> 489343Sbde#include <sys/mutex.h> 49105109Smike#include <sys/errno.h> 50105109Smike#include <sys/sbuf.h> 5151791Smarcel#include <geom/geom.h> 5251942Smarcel#include <geom/geom_slice.h> 5351791Smarcel#include <machine/stdarg.h> 54105106Smike 5551942Smarcelstatic g_orphan_t g_slice_orphan; 56105106Smikestatic g_access_t g_slice_access; 5751791Smarcelstatic g_start_t g_slice_start; 58105106Smike 5951791Smarcelstatic struct g_slicer * 60105106Smikeg_slice_alloc(unsigned nslice, unsigned scsize) 6151791Smarcel{ 62105106Smike struct g_slicer *gsp; 6351791Smarcel 64105106Smike gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO); 6551791Smarcel if (scsize > 0) 66105106Smike gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO); 6751791Smarcel else 6851791Smarcel gsp->softc = NULL; 69105106Smike gsp->slices = g_malloc(nslice * sizeof(struct g_slice), 7051791Smarcel M_WAITOK | M_ZERO); 71105106Smike gsp->nslice = nslice; 7251791Smarcel return (gsp); 73105106Smike} 74105106Smike 7551791Smarcelstatic void 76105106Smikeg_slice_free(struct g_slicer *gsp) 7751791Smarcel{ 78105106Smike 7951791Smarcel g_free(gsp->slices); 80105106Smike if (gsp->hotspot != NULL) 81105106Smike g_free(gsp->hotspot); 8251791Smarcel if (gsp->softc != NULL) 8351791Smarcel g_free(gsp->softc); 84105106Smike g_free(gsp); 8551791Smarcel} 86105106Smike 8751791Smarcelstatic int 88105106Smikeg_slice_access(struct g_provider *pp, int dr, int dw, int de) 89105106Smike{ 9051791Smarcel int error; 9151791Smarcel u_int u; 9251791Smarcel struct g_geom *gp; 9351791Smarcel struct g_consumer *cp; 9451791Smarcel struct g_provider *pp2; 9551791Smarcel struct g_slicer *gsp; 96105106Smike struct g_slice *gsl, *gsl2; 97105106Smike 9851791Smarcel gp = pp->geom; 99105106Smike cp = LIST_FIRST(&gp->consumer); 100105106Smike KASSERT (cp != NULL, ("g_slice_access but no consumer")); 10151791Smarcel gsp = gp->softc; 10251791Smarcel gsl = &gsp->slices[pp->index]; 10351791Smarcel for (u = 0; u < gsp->nslice; u++) { 10451791Smarcel gsl2 = &gsp->slices[u]; 105105106Smike if (gsl2->length == 0) 106105106Smike continue; 10751942Smarcel if (u == pp->index) 10851942Smarcel continue; 109105106Smike if (gsl->offset + gsl->length <= gsl2->offset) 110105106Smike continue; 11151942Smarcel if (gsl2->offset + gsl2->length <= gsl->offset) 11251942Smarcel continue; 113105106Smike /* overlap */ 114105015Smike pp2 = gsl2->provider; 115105015Smike if ((pp->acw + dw) > 0 && pp2->ace > 0) 116105015Smike return (EPERM); 1171541Srgrimes if ((pp->ace + de) > 0 && pp2->acw > 0) 118105106Smike return (EPERM); 119105106Smike } 120105106Smike /* On first open, grab an extra "exclusive" bit */ 121105106Smike if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 122105106Smike de++; 123105106Smike /* ... and let go of it on last close */ 124105106Smike if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 1259343Sbde de--; 1269343Sbde error = g_access(cp, dr, dw, de); 1279343Sbde return (error); 1289343Sbde} 1291541Srgrimes 1309343Sbde/* 1319343Sbde * XXX: It should be possible to specify here if we should finish all of the 1329343Sbde * XXX: bio, or only the non-hot bits. This would get messy if there were 1339343Sbde * XXX: two hot spots in the same bio, so for now we simply finish off the 1349343Sbde * XXX: entire bio. Modifying hot data on the way to disk is frowned on 1359343Sbde * XXX: so making that considerably harder is not a bad idea anyway. 1369343Sbde */ 1379343Sbdevoid 1389343Sbdeg_slice_finish_hot(struct bio *bp) 1399343Sbde{ 1401541Srgrimes struct bio *bp2; 141105109Smike struct g_geom *gp; 1421541Srgrimes struct g_consumer *cp; 143105015Smike struct g_slicer *gsp; 144105015Smike struct g_slice *gsl; 145105015Smike int idx; 146105015Smike 147105015Smike KASSERT(bp->bio_to != NULL, 148105015Smike ("NULL bio_to in g_slice_finish_hot(%p)", bp)); 149105015Smike KASSERT(bp->bio_from != NULL, 150105015Smike ("NULL bio_from in g_slice_finish_hot(%p)", bp)); 15148621Scracauer gp = bp->bio_to->geom; 15248621Scracauer gsp = gp->softc; 15351942Smarcel cp = LIST_FIRST(&gp->consumer); 15451942Smarcel KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp)); 15548621Scracauer idx = bp->bio_to->index; 156105015Smike gsl = &gsp->slices[idx]; 15748621Scracauer 158105015Smike bp2 = g_clone_bio(bp); 15952308Smarcel if (bp2 == NULL) { 16052308Smarcel g_io_deliver(bp, ENOMEM); 16169002Salc return; 16269002Salc } 16369002Salc if (bp2->bio_offset + bp2->bio_length > gsl->length) 16469002Salc bp2->bio_length = gsl->length - bp2->bio_offset; 16552308Smarcel bp2->bio_done = g_std_done; 166105015Smike bp2->bio_offset += gsl->offset; 167105015Smike g_io_request(bp2, cp); 168105015Smike return; 16952308Smarcel} 170105109Smike 171105015Smikestatic void 172105109Smikeg_slice_start(struct bio *bp) 173105015Smike{ 17452308Smarcel struct bio *bp2; 17552308Smarcel struct g_provider *pp; 17652308Smarcel struct g_geom *gp; 177105015Smike struct g_consumer *cp; 178105109Smike struct g_slicer *gsp; 179105015Smike struct g_slice *gsl; 180105015Smike struct g_slice_hot *ghp; 181105015Smike int idx, error; 182105015Smike u_int m_index; 183105015Smike off_t t; 18452308Smarcel 185105015Smike pp = bp->bio_to; 186105109Smike gp = pp->geom; 18751942Smarcel gsp = gp->softc; 18851942Smarcel cp = LIST_FIRST(&gp->consumer); 18952160Smarcel idx = pp->index; 19048621Scracauer gsl = &gsp->slices[idx]; 191105109Smike switch(bp->bio_cmd) { 19248621Scracauer case BIO_READ: 19348621Scracauer case BIO_WRITE: 19448621Scracauer case BIO_DELETE: 19551942Smarcel if (bp->bio_offset > gsl->length) { 196105015Smike g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */ 197105015Smike return; 19851942Smarcel } 19951942Smarcel /* 20051942Smarcel * Check if we collide with any hot spaces, and call the 20151942Smarcel * method once if so. 20251942Smarcel */ 20351791Smarcel t = bp->bio_offset + gsl->offset; 204105015Smike for (m_index = 0; m_index < gsp->nhotspot; m_index++) { 20548621Scracauer ghp = &gsp->hotspot[m_index]; 206105106Smike if (t >= ghp->offset + ghp->length) 20751942Smarcel continue; 20851942Smarcel if (t + bp->bio_length <= ghp->offset) 2091541Srgrimes continue; 2101541Srgrimes switch(bp->bio_cmd) { 2111541Srgrimes case BIO_READ: idx = ghp->ract; break; 21283045Sobrien case BIO_WRITE: idx = ghp->wact; break; 21348621Scracauer case BIO_DELETE: idx = ghp->dact; break; 21492719Salfred } 21592719Salfred switch(idx) { 21648621Scracauer case G_SLICE_HOT_ALLOW: 21751942Smarcel /* Fall out and continue normal processing */ 21851942Smarcel continue; 2191541Srgrimes case G_SLICE_HOT_DENY: 220105015Smike g_io_deliver(bp, EROFS); 22151791Smarcel return; 222105015Smike case G_SLICE_HOT_START: 22348621Scracauer error = gsp->start(bp); 22448621Scracauer if (error && error != EJUSTRETURN) 225105015Smike g_io_deliver(bp, error); 226105015Smike return; 22751791Smarcel case G_SLICE_HOT_CALL: 228105015Smike error = g_post_event(gsp->hot, bp, M_NOWAIT, 229105109Smike gp, NULL); 230105015Smike if (error) 23151791Smarcel g_io_deliver(bp, error); 232105015Smike return; 233105109Smike } 234105109Smike break; 23514331Speter } 23614331Speter bp2 = g_clone_bio(bp); 23729340Sjoerg if (bp2 == NULL) { 23848621Scracauer g_io_deliver(bp, ENOMEM); 239105015Smike return; 240105015Smike } 241105106Smike if (bp2->bio_offset + bp2->bio_length > gsl->length) 2421541Srgrimes bp2->bio_length = gsl->length - bp2->bio_offset; 2431541Srgrimes bp2->bio_done = g_std_done; 2441541Srgrimes bp2->bio_offset += gsl->offset; 245105015Smike g_io_request(bp2, cp); 2461541Srgrimes return; 247105015Smike case BIO_GETATTR: 248105109Smike /* Give the real method a chance to override */ 249105015Smike if (gsp->start != NULL && gsp->start(bp)) 2501541Srgrimes return; 251105015Smike if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { 252105109Smike struct g_kerneldump *gkd; 253105109Smike 254105109Smike gkd = (struct g_kerneldump *)bp->bio_data; 255105109Smike gkd->offset += gsp->slices[idx].offset; 256105109Smike if (gkd->length > gsp->slices[idx].length) 257105015Smike gkd->length = gsp->slices[idx].length; 258105015Smike /* now, pass it on downwards... */ 259105109Smike } 260105015Smike bp2 = g_clone_bio(bp); 26151791Smarcel if (bp2 == NULL) { 262105106Smike g_io_deliver(bp, ENOMEM); 2639343Sbde return; 264105106Smike } 265105106Smike bp2->bio_done = g_std_done; 2661541Srgrimes g_io_request(bp2, cp); 267105015Smike break; 2681541Srgrimes default: 26951942Smarcel g_io_deliver(bp, EOPNOTSUPP); 2701541Srgrimes return; 271105015Smike } 272105015Smike} 273105015Smike 274105015Smikevoid 275105015Smikeg_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 27651942Smarcel{ 277105015Smike struct g_slicer *gsp; 27851942Smarcel 27951791Smarcel gsp = gp->softc; 28051791Smarcel if (indent == NULL) { 28114331Speter sbuf_printf(sb, " i %u", pp->index); 28214331Speter sbuf_printf(sb, " o %ju", 2831541Srgrimes (uintmax_t)gsp->slices[pp->index].offset); 284105015Smike return; 2851541Srgrimes } 286105015Smike if (pp != NULL) { 28751791Smarcel sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index); 2881541Srgrimes sbuf_printf(sb, "%s<length>%ju</length>\n", 2891541Srgrimes indent, (uintmax_t)gsp->slices[pp->index].length); 2901541Srgrimes sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent, 29183045Sobrien (uintmax_t)gsp->slices[pp->index].length / 512); 29251942Smarcel sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent, 29351942Smarcel (uintmax_t)gsp->slices[pp->index].offset); 29451942Smarcel sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent, 2951541Srgrimes (uintmax_t)gsp->slices[pp->index].offset / 512); 2961541Srgrimes } 297105109Smike} 298105109Smike 299105109Smikeint 300105109Smikeg_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...) 301105109Smike{ 302105109Smike struct g_provider *pp, *pp2; 303105109Smike struct g_slicer *gsp; 304105015Smike struct g_slice *gsl; 3051541Srgrimes va_list ap; 306105950Speter struct sbuf *sb; 307105950Speter int acc; 308105950Speter 309105950Speter g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)", 310105950Speter gp->name, idx, how); 311105950Speter g_topology_assert(); 312105950Speter gsp = gp->softc; 313105950Speter if (idx >= gsp->nslice) 314105015Smike return(EINVAL); 3151541Srgrimes gsl = &gsp->slices[idx]; 3161541Srgrimes pp = gsl->provider; 3171541Srgrimes if (pp != NULL) 31883045Sobrien acc = pp->acr + pp->acw + pp->ace; 319105015Smike else 32051942Smarcel acc = 0; 32151942Smarcel if (acc != 0 && how != G_SLICE_CONFIG_FORCE) { 3221541Srgrimes if (length < gsl->length) 323105015Smike return(EBUSY); 3241541Srgrimes if (offset != gsl->offset) 325105015Smike return(EBUSY); 3261541Srgrimes } 3271541Srgrimes /* XXX: check offset + length <= MEDIASIZE */ 3281541Srgrimes if (how == G_SLICE_CONFIG_CHECK) 3291541Srgrimes return (0); 330105109Smike gsl->length = length; 331105015Smike gsl->offset = offset; 3321541Srgrimes gsl->sectorsize = sectorsize; 333105015Smike if (length == 0) { 3341541Srgrimes if (pp == NULL) 335105015Smike return (0); 3361541Srgrimes if (bootverbose) 337105015Smike printf("GEOM: Deconfigure %s\n", pp->name); 33851791Smarcel g_orphan_provider(pp, ENXIO); 33951791Smarcel gsl->provider = NULL; 34051791Smarcel gsp->nprovider--; 34151791Smarcel return (0); 34251791Smarcel } 34351791Smarcel if (pp != NULL) { 344105015Smike if (bootverbose) 34534925Sdufault printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n", 3461541Srgrimes pp->name, (intmax_t)offset, (intmax_t)length, 3471541Srgrimes (intmax_t)(offset + length - 1)); 3481541Srgrimes pp->mediasize = gsl->length; 3491541Srgrimes return (0); 3501541Srgrimes } 35192719Salfred sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 3521541Srgrimes va_start(ap, fmt); 3539343Sbde sbuf_vprintf(sb, fmt, ap); 354105109Smike va_end(ap); 355 sbuf_finish(sb); 356 pp = g_new_providerf(gp, sbuf_data(sb)); 357 pp2 = LIST_FIRST(&gp->consumer)->provider; 358 pp->flags = pp2->flags & G_PF_CANDELETE; 359 if (pp2->stripesize > 0) { 360 pp->stripesize = pp2->stripesize; 361 pp->stripeoffset = (pp2->stripeoffset + offset) % pp->stripesize; 362 } 363 if (bootverbose) 364 printf("GEOM: Configure %s, start %jd length %jd end %jd\n", 365 pp->name, (intmax_t)offset, (intmax_t)length, 366 (intmax_t)(offset + length - 1)); 367 pp->index = idx; 368 pp->mediasize = gsl->length; 369 pp->sectorsize = gsl->sectorsize; 370 gsl->provider = pp; 371 gsp->nprovider++; 372 g_error_provider(pp, 0); 373 sbuf_delete(sb); 374 return(0); 375} 376 377/* 378 * Configure "hotspots". A hotspot is a piece of the parent device which 379 * this particular slicer cares about for some reason. Typically because 380 * it contains meta-data used to configure the slicer. 381 * A hotspot is identified by its index number. The offset and length are 382 * relative to the parent device, and the three "?act" fields specify 383 * what action to take on BIO_READ, BIO_DELETE and BIO_WRITE. 384 * 385 * XXX: There may be a race relative to g_slice_start() here, if an existing 386 * XXX: hotspot is changed wile I/O is happening. Should this become a problem 387 * XXX: we can protect the hotspot stuff with a mutex. 388 */ 389 390int 391g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length, int ract, int dact, int wact) 392{ 393 struct g_slicer *gsp; 394 struct g_slice_hot *gsl, *gsl2; 395 396 g_trace(G_T_TOPOLOGY, "g_slice_conf_hot(%s, idx: %d, off: %jd, len: %jd)", 397 gp->name, idx, (intmax_t)offset, (intmax_t)length); 398 g_topology_assert(); 399 gsp = gp->softc; 400 gsl = gsp->hotspot; 401 if(idx >= gsp->nhotspot) { 402 gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO); 403 if (gsp->hotspot != NULL) 404 bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2); 405 gsp->hotspot = gsl2; 406 if (gsp->hotspot != NULL) 407 g_free(gsl); 408 gsl = gsl2; 409 gsp->nhotspot = idx + 1; 410 } 411 gsl[idx].offset = offset; 412 gsl[idx].length = length; 413 KASSERT(!((ract | dact | wact) & G_SLICE_HOT_START) 414 || gsp->start != NULL, ("G_SLICE_HOT_START but no slice->start")); 415 /* XXX: check that we _have_ a start function if HOT_START specified */ 416 gsl[idx].ract = ract; 417 gsl[idx].dact = dact; 418 gsl[idx].wact = wact; 419 return (0); 420} 421 422void 423g_slice_spoiled(struct g_consumer *cp) 424{ 425 struct g_geom *gp; 426 struct g_slicer *gsp; 427 428 g_topology_assert(); 429 gp = cp->geom; 430 g_trace(G_T_TOPOLOGY, "g_slice_spoiled(%p/%s)", cp, gp->name); 431 gsp = gp->softc; 432 gp->softc = NULL; 433 g_slice_free(gsp); 434 g_wither_geom(gp, ENXIO); 435} 436 437int 438g_slice_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp) 439{ 440 441 g_slice_spoiled(LIST_FIRST(&gp->consumer)); 442 return (0); 443} 444 445struct g_geom * 446g_slice_new(struct g_class *mp, u_int slices, struct g_provider *pp, struct g_consumer **cpp, void *extrap, int extra, g_slice_start_t *start) 447{ 448 struct g_geom *gp; 449 struct g_slicer *gsp; 450 struct g_consumer *cp; 451 void **vp; 452 int error; 453 454 g_topology_assert(); 455 vp = (void **)extrap; 456 gp = g_new_geomf(mp, "%s", pp->name); 457 gsp = g_slice_alloc(slices, extra); 458 gsp->start = start; 459 gp->access = g_slice_access; 460 gp->orphan = g_slice_orphan; 461 gp->softc = gsp; 462 gp->start = g_slice_start; 463 gp->spoiled = g_slice_spoiled; 464 gp->dumpconf = g_slice_dumpconf; 465 if (gp->class->destroy_geom == NULL) 466 gp->class->destroy_geom = g_slice_destroy_geom; 467 cp = g_new_consumer(gp); 468 error = g_attach(cp, pp); 469 if (error == 0) 470 error = g_access(cp, 1, 0, 0); 471 if (error) { 472 g_wither_geom(gp, ENXIO); 473 return (NULL); 474 } 475 if (extrap != NULL) 476 *vp = gsp->softc; 477 *cpp = cp; 478 return (gp); 479} 480 481static void 482g_slice_orphan(struct g_consumer *cp) 483{ 484 485 g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name); 486 g_topology_assert(); 487 KASSERT(cp->provider->error != 0, 488 ("g_slice_orphan with error == 0")); 489 490 /* XXX: Not good enough we leak the softc and its suballocations */ 491 g_slice_free(cp->geom->softc); 492 g_wither_geom(cp->geom, cp->provider->error); 493} 494