geom_slice.c revision 113712
192108Sphk/*- 292108Sphk * Copyright (c) 2002 Poul-Henning Kamp 392108Sphk * Copyright (c) 2002 Networks Associates Technology, Inc. 492108Sphk * All rights reserved. 592108Sphk * 692108Sphk * This software was developed for the FreeBSD Project by Poul-Henning Kamp 792108Sphk * and NAI Labs, the Security Research Division of Network Associates, Inc. 892108Sphk * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 992108Sphk * DARPA CHATS research program. 1092108Sphk * 1192108Sphk * Redistribution and use in source and binary forms, with or without 1292108Sphk * modification, are permitted provided that the following conditions 1392108Sphk * are met: 1492108Sphk * 1. Redistributions of source code must retain the above copyright 1592108Sphk * notice, this list of conditions and the following disclaimer. 1692108Sphk * 2. Redistributions in binary form must reproduce the above copyright 1792108Sphk * notice, this list of conditions and the following disclaimer in the 1892108Sphk * documentation and/or other materials provided with the distribution. 1992108Sphk * 3. The names of the authors may not be used to endorse or promote 2092108Sphk * products derived from this software without specific prior written 2192108Sphk * permission. 2292108Sphk * 2392108Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2492108Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2592108Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2692108Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2792108Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2892108Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2992108Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3092108Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3192108Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3292108Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3392108Sphk * SUCH DAMAGE. 3492108Sphk * 3592108Sphk * $FreeBSD: head/sys/geom/geom_slice.c 113712 2003-04-19 10:00:22Z phk $ 3692108Sphk */ 3792108Sphk 3892108Sphk 3992108Sphk#include <sys/param.h> 4092108Sphk#include <sys/systm.h> 4192108Sphk#include <sys/kernel.h> 4292108Sphk#include <sys/malloc.h> 4392108Sphk#include <sys/bio.h> 4492108Sphk#include <sys/sysctl.h> 4592108Sphk#include <sys/proc.h> 4692108Sphk#include <sys/kthread.h> 4792108Sphk#include <sys/lock.h> 4892108Sphk#include <sys/mutex.h> 4992108Sphk#include <sys/errno.h> 5092108Sphk#include <sys/sbuf.h> 5192108Sphk#include <geom/geom.h> 5292108Sphk#include <geom/geom_slice.h> 5392108Sphk#include <machine/stdarg.h> 5492108Sphk 5593776Sphkstatic g_orphan_t g_slice_orphan; 5693776Sphkstatic g_access_t g_slice_access; 5793776Sphkstatic g_start_t g_slice_start; 5893776Sphk 5995321Sphkstatic struct g_slicer * 6092108Sphkg_slice_init(unsigned nslice, unsigned scsize) 6192108Sphk{ 6292108Sphk struct g_slicer *gsp; 6392108Sphk 64111119Simp gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO); 65111119Simp gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO); 66104057Sphk gsp->slices = g_malloc(nslice * sizeof(struct g_slice), 67111119Simp M_WAITOK | M_ZERO); 6892108Sphk gsp->nslice = nslice; 6992108Sphk return (gsp); 7092108Sphk} 7192108Sphk 7293776Sphkstatic int 7392108Sphkg_slice_access(struct g_provider *pp, int dr, int dw, int de) 7492108Sphk{ 75107953Sphk int error; 76107953Sphk u_int u; 7792108Sphk struct g_geom *gp; 7892108Sphk struct g_consumer *cp; 7992108Sphk struct g_provider *pp2; 8092108Sphk struct g_slicer *gsp; 8192108Sphk struct g_slice *gsl, *gsl2; 8292108Sphk 8392108Sphk gp = pp->geom; 8492108Sphk cp = LIST_FIRST(&gp->consumer); 8592108Sphk KASSERT (cp != NULL, ("g_slice_access but no consumer")); 8692108Sphk gsp = gp->softc; 8792108Sphk gsl = &gsp->slices[pp->index]; 88107953Sphk for (u = 0; u < gsp->nslice; u++) { 89107953Sphk gsl2 = &gsp->slices[u]; 9092108Sphk if (gsl2->length == 0) 9192108Sphk continue; 92107953Sphk if (u == pp->index) 9392108Sphk continue; 9492108Sphk if (gsl->offset + gsl->length <= gsl2->offset) 9592108Sphk continue; 9692108Sphk if (gsl2->offset + gsl2->length <= gsl->offset) 9792108Sphk continue; 9892108Sphk /* overlap */ 9992108Sphk pp2 = gsl2->provider; 10092108Sphk if ((pp->acw + dw) > 0 && pp2->ace > 0) 10192108Sphk return (EPERM); 10292108Sphk if ((pp->ace + de) > 0 && pp2->acw > 0) 10392108Sphk return (EPERM); 10492108Sphk } 10592108Sphk /* On first open, grab an extra "exclusive" bit */ 10692108Sphk if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 10792108Sphk de++; 10892108Sphk /* ... and let go of it on last close */ 10992108Sphk if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 11092108Sphk de--; 11192108Sphk error = g_access_rel(cp, dr, dw, de); 11292108Sphk return (error); 11392108Sphk} 11492108Sphk 115107522Sphkvoid 116107522Sphkg_slice_finish_hot(struct bio *bp) 117107522Sphk{ 118107522Sphk struct bio *bp2; 119107522Sphk struct g_geom *gp; 120107522Sphk struct g_consumer *cp; 121107522Sphk struct g_slicer *gsp; 122107522Sphk struct g_slice *gsl; 123107953Sphk int idx; 124107522Sphk 125107522Sphk KASSERT(bp->bio_to != NULL, ("NULL bio_to in g_slice_finish_hot(%p)", bp)); 126107522Sphk KASSERT(bp->bio_from != NULL, ("NULL bio_from in g_slice_finish_hot(%p)", bp)); 127107522Sphk gp = bp->bio_to->geom; 128107522Sphk gsp = gp->softc; 129107522Sphk cp = LIST_FIRST(&gp->consumer); 130107522Sphk KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp)); 131107953Sphk idx = bp->bio_to->index; 132107953Sphk gsl = &gsp->slices[idx]; 133107522Sphk 134107522Sphk bp2 = g_clone_bio(bp); 135107522Sphk if (bp2 == NULL) { 136107522Sphk g_io_deliver(bp, ENOMEM); 137107522Sphk return; 138107522Sphk } 139107522Sphk if (bp2->bio_offset + bp2->bio_length > gsl->length) 140107522Sphk bp2->bio_length = gsl->length - bp2->bio_offset; 141107522Sphk bp2->bio_done = g_std_done; 142107522Sphk bp2->bio_offset += gsl->offset; 143107522Sphk g_io_request(bp2, cp); 144107522Sphk return; 145107522Sphk} 146107522Sphk 14793776Sphkstatic void 14892108Sphkg_slice_start(struct bio *bp) 14992108Sphk{ 15092108Sphk struct bio *bp2; 15192108Sphk struct g_provider *pp; 15292108Sphk struct g_geom *gp; 15392108Sphk struct g_consumer *cp; 15492108Sphk struct g_slicer *gsp; 155113712Sphk struct g_slice *gsl; 156113712Sphk struct g_slice_hot *gmp; 157107953Sphk int idx, error; 158107522Sphk u_int m_index; 15994287Sphk off_t t; 16092108Sphk 16192108Sphk pp = bp->bio_to; 16292108Sphk gp = pp->geom; 16392108Sphk gsp = gp->softc; 16492108Sphk cp = LIST_FIRST(&gp->consumer); 165107953Sphk idx = pp->index; 166107953Sphk gsl = &gsp->slices[idx]; 16792108Sphk switch(bp->bio_cmd) { 16892108Sphk case BIO_READ: 16992108Sphk case BIO_WRITE: 17092108Sphk case BIO_DELETE: 17192108Sphk if (bp->bio_offset > gsl->length) { 172104195Sphk g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */ 17392108Sphk return; 17492108Sphk } 175107522Sphk /* 176107522Sphk * Check if we collide with any hot spaces, and call the 177107522Sphk * method once if so. 178107522Sphk */ 179107832Sphk t = bp->bio_offset + gsl->offset; 180108003Sphk /* .ctl devices may take us negative */ 181108003Sphk if (t < 0 || (t + bp->bio_length) < 0) { 182108003Sphk g_io_deliver(bp, EINVAL); 183108003Sphk return; 184108003Sphk } 185113712Sphk for (m_index = 0; m_index < gsp->nhotspot; m_index++) { 186113712Sphk gmp = &gsp->hotspot[m_index]; 187107832Sphk if (t >= gmp->offset + gmp->length) 188107522Sphk continue; 189107832Sphk if (t + bp->bio_length <= gmp->offset) 190107522Sphk continue; 191107522Sphk error = gsp->start(bp); 192107522Sphk if (error == EJUSTRETURN) 193107522Sphk return; 194107522Sphk else if (error) { 195107522Sphk g_io_deliver(bp, error); 196107522Sphk return; 197107522Sphk } 198107522Sphk break; 199107522Sphk } 20092108Sphk bp2 = g_clone_bio(bp); 201104057Sphk if (bp2 == NULL) { 202104195Sphk g_io_deliver(bp, ENOMEM); 203104057Sphk return; 204104057Sphk } 20592108Sphk if (bp2->bio_offset + bp2->bio_length > gsl->length) 20692108Sphk bp2->bio_length = gsl->length - bp2->bio_offset; 20792108Sphk bp2->bio_done = g_std_done; 20892108Sphk bp2->bio_offset += gsl->offset; 20992108Sphk g_io_request(bp2, cp); 21092108Sphk return; 21192108Sphk case BIO_GETATTR: 21294287Sphk /* Give the real method a chance to override */ 21394287Sphk if (gsp->start(bp)) 21494287Sphk return; 21595038Sphk if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { 21695038Sphk struct g_kerneldump *gkd; 21795038Sphk 21895038Sphk gkd = (struct g_kerneldump *)bp->bio_data; 219107953Sphk gkd->offset += gsp->slices[idx].offset; 220107953Sphk if (gkd->length > gsp->slices[idx].length) 221107953Sphk gkd->length = gsp->slices[idx].length; 22295038Sphk /* now, pass it on downwards... */ 22395038Sphk } 22492108Sphk bp2 = g_clone_bio(bp); 225104081Sphk if (bp2 == NULL) { 226104195Sphk g_io_deliver(bp, ENOMEM); 227104081Sphk return; 228104081Sphk } 22992108Sphk bp2->bio_done = g_std_done; 23092108Sphk g_io_request(bp2, cp); 23192108Sphk break; 23292108Sphk default: 233104195Sphk g_io_deliver(bp, EOPNOTSUPP); 23492108Sphk return; 23592108Sphk } 23692108Sphk} 23792108Sphk 23892108Sphkvoid 239107953Sphkg_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 24092108Sphk{ 24192108Sphk struct g_slicer *gsp; 24292108Sphk 24392108Sphk gsp = gp->softc; 244106101Sphk if (indent == NULL) { 245106101Sphk sbuf_printf(sb, " i %u", pp->index); 246106101Sphk sbuf_printf(sb, " o %ju", 247106101Sphk (uintmax_t)gsp->slices[pp->index].offset); 248106101Sphk return; 249106101Sphk } 25092108Sphk if (pp != NULL) { 25192108Sphk sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index); 252105540Sphk sbuf_printf(sb, "%s<length>%ju</length>\n", 253105540Sphk indent, (uintmax_t)gsp->slices[pp->index].length); 254105540Sphk sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent, 255105540Sphk (uintmax_t)gsp->slices[pp->index].length / 512); 256105540Sphk sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent, 257105540Sphk (uintmax_t)gsp->slices[pp->index].offset); 258105540Sphk sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent, 259105540Sphk (uintmax_t)gsp->slices[pp->index].offset / 512); 26092108Sphk } 26192108Sphk} 26292108Sphk 263104064Sphkint 264107953Sphkg_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...) 265104064Sphk{ 266110710Sphk struct g_provider *pp, *pp2; 267104064Sphk struct g_slicer *gsp; 268104064Sphk struct g_slice *gsl; 269104064Sphk va_list ap; 270104064Sphk struct sbuf *sb; 271104064Sphk int error, acc; 272104064Sphk 273104195Sphk g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)", 274107953Sphk gp->name, idx, how); 275104064Sphk g_topology_assert(); 276104064Sphk gsp = gp->softc; 277104064Sphk error = 0; 278107953Sphk if (idx >= gsp->nslice) 279104064Sphk return(EINVAL); 280107953Sphk gsl = &gsp->slices[idx]; 281104064Sphk pp = gsl->provider; 282104064Sphk if (pp != NULL) 283104064Sphk acc = pp->acr + pp->acw + pp->ace; 284104064Sphk else 285104064Sphk acc = 0; 286104064Sphk if (acc != 0 && how != G_SLICE_CONFIG_FORCE) { 287104064Sphk if (length < gsl->length) 288104064Sphk return(EBUSY); 289104064Sphk if (offset != gsl->offset) 290104064Sphk return(EBUSY); 291104064Sphk } 292104064Sphk /* XXX: check offset + length <= MEDIASIZE */ 293104064Sphk if (how == G_SLICE_CONFIG_CHECK) 294104064Sphk return (0); 295104064Sphk gsl->length = length; 296104064Sphk gsl->offset = offset; 297105542Sphk gsl->sectorsize = sectorsize; 298105957Sphk if (length == 0) { 299105957Sphk if (pp == NULL) 300105957Sphk return (0); 301105957Sphk if (bootverbose) 302105957Sphk printf("GEOM: Deconfigure %s\n", pp->name); 303104064Sphk g_orphan_provider(pp, ENXIO); 304104064Sphk gsl->provider = NULL; 305104064Sphk gsp->nprovider--; 306104064Sphk return (0); 307104064Sphk } 308105957Sphk if (pp != NULL) { 309105957Sphk if (bootverbose) 310105957Sphk printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n", 311105957Sphk pp->name, (intmax_t)offset, (intmax_t)length, 312105957Sphk (intmax_t)(offset + length - 1)); 313107116Sphk pp->mediasize = gsl->length; 314105957Sphk return (0); 315105957Sphk } 316104064Sphk va_start(ap, fmt); 317104064Sphk sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 318104064Sphk sbuf_vprintf(sb, fmt, ap); 319104064Sphk sbuf_finish(sb); 320104064Sphk pp = g_new_providerf(gp, sbuf_data(sb)); 321110710Sphk pp2 = LIST_FIRST(&gp->consumer)->provider; 322110710Sphk pp->flags = pp2->flags & G_PF_CANDELETE; 323110713Sphk if (pp2->stripesize > 0) { 324110713Sphk pp->stripesize = pp2->stripesize; 325110713Sphk pp->stripeoffset = (pp2->stripeoffset + offset) % pp->stripesize; 326110713Sphk } 327105957Sphk if (bootverbose) 328105957Sphk printf("GEOM: Configure %s, start %jd length %jd end %jd\n", 329105957Sphk pp->name, (intmax_t)offset, (intmax_t)length, 330105957Sphk (intmax_t)(offset + length - 1)); 331107953Sphk pp->index = idx; 332105542Sphk pp->mediasize = gsl->length; 333105542Sphk pp->sectorsize = gsl->sectorsize; 334104064Sphk gsl->provider = pp; 335104064Sphk gsp->nprovider++; 336104064Sphk g_error_provider(pp, 0); 337104064Sphk sbuf_delete(sb); 338104064Sphk return(0); 339104064Sphk} 340104064Sphk 341107522Sphkint 342107953Sphkg_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length) 343107522Sphk{ 344107522Sphk struct g_slicer *gsp; 345113712Sphk struct g_slice_hot *gsl, *gsl2; 346107522Sphk 347107522Sphk g_trace(G_T_TOPOLOGY, "g_slice_conf_hot()"); 348107522Sphk g_topology_assert(); 349107522Sphk gsp = gp->softc; 350113712Sphk gsl = gsp->hotspot; 351113712Sphk if(idx >= gsp->nhotspot) { 352111119Simp gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO); 353113712Sphk if (gsp->hotspot != NULL) 354113712Sphk bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2); 355113712Sphk gsp->hotspot = gsl2; 356113712Sphk if (gsp->hotspot != NULL) 357107522Sphk g_free(gsl); 358107522Sphk gsl = gsl2; 359113712Sphk gsp->nhotspot = idx + 1; 360107522Sphk } 361107522Sphk if (bootverbose) 362107522Sphk printf("GEOM: Add %s hot[%d] start %jd length %jd end %jd\n", 363107953Sphk gp->name, idx, (intmax_t)offset, (intmax_t)length, 364107522Sphk (intmax_t)(offset + length - 1)); 365107953Sphk gsl[idx].offset = offset; 366107953Sphk gsl[idx].length = length; 367107522Sphk return (0); 368107522Sphk} 369107522Sphk 37092108Sphkstruct g_geom * 371107522Sphkg_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) 37292108Sphk{ 37392108Sphk struct g_geom *gp; 37492108Sphk struct g_slicer *gsp; 37592108Sphk struct g_consumer *cp; 37692108Sphk void **vp; 377113390Sphk int error; 37892108Sphk 37992108Sphk g_topology_assert(); 38092108Sphk vp = (void **)extrap; 38192108Sphk gp = g_new_geomf(mp, "%s", pp->name); 38292108Sphk gsp = g_slice_init(slices, extra); 38392108Sphk gsp->start = start; 38493776Sphk gp->access = g_slice_access; 38593776Sphk gp->orphan = g_slice_orphan; 38692108Sphk gp->softc = gsp; 38792108Sphk gp->start = g_slice_start; 38892108Sphk gp->spoiled = g_std_spoiled; 38992108Sphk gp->dumpconf = g_slice_dumpconf; 39092108Sphk cp = g_new_consumer(gp); 391105551Sphk error = g_attach(cp, pp); 392105551Sphk if (error == 0) 393105551Sphk error = g_access_rel(cp, 1, 0, 0); 39492108Sphk if (error) { 395105551Sphk if (cp->provider != NULL) 396105551Sphk g_detach(cp); 39792108Sphk g_destroy_consumer(cp); 39892371Sphk g_free(gsp->slices); 39992108Sphk g_free(gp->softc); 40092108Sphk g_destroy_geom(gp); 40192108Sphk return (NULL); 40292108Sphk } 40392108Sphk *vp = gsp->softc; 40492108Sphk *cpp = cp; 40592108Sphk return (gp); 40692108Sphk} 40792108Sphk 40893776Sphkstatic void 40993250Sphkg_slice_orphan(struct g_consumer *cp) 41092108Sphk{ 41192108Sphk struct g_geom *gp; 41292108Sphk struct g_provider *pp; 41392108Sphk int error; 41492108Sphk 41592108Sphk g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name); 41692108Sphk g_topology_assert(); 41792108Sphk KASSERT(cp->provider->error != 0, 41892108Sphk ("g_slice_orphan with error == 0")); 41992108Sphk 42092108Sphk gp = cp->geom; 421107522Sphk /* XXX: Not good enough we leak the softc and its suballocations */ 42292108Sphk gp->flags |= G_GEOM_WITHER; 42392108Sphk error = cp->provider->error; 42492108Sphk LIST_FOREACH(pp, &gp->provider, provider) 42592108Sphk g_orphan_provider(pp, error); 42692108Sphk return; 42792108Sphk} 428