geom_slice.c revision 93326
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 93326 2002-03-28 10:09:24Z phk $ 3692108Sphk */ 3792108Sphk 3892108Sphk 3992108Sphk#include <sys/param.h> 4092108Sphk#ifndef _KERNEL 4192108Sphk#include <stdio.h> 4292108Sphk#include <unistd.h> 4392108Sphk#include <stdlib.h> 4492108Sphk#include <signal.h> 4592108Sphk#include <err.h> 4692108Sphk#else 4792108Sphk#include <sys/systm.h> 4892108Sphk#include <sys/kernel.h> 4992108Sphk#include <sys/malloc.h> 5092108Sphk#include <sys/bio.h> 5192108Sphk#include <sys/sysctl.h> 5292108Sphk#include <sys/proc.h> 5392108Sphk#include <sys/kthread.h> 5492108Sphk#include <sys/lock.h> 5592108Sphk#include <sys/mutex.h> 5692108Sphk#endif 5792108Sphk#include <sys/errno.h> 5892108Sphk#include <sys/sbuf.h> 5992108Sphk#include <geom/geom.h> 6092108Sphk#include <geom/geom_slice.h> 6192108Sphk#include <machine/stdarg.h> 6292108Sphk 6392108Sphkstruct g_slicer * 6492108Sphkg_slice_init(unsigned nslice, unsigned scsize) 6592108Sphk{ 6692108Sphk struct g_slicer *gsp; 6792108Sphk 6892371Sphk gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO); 6992371Sphk gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO); 7092371Sphk gsp->slices = g_malloc(nslice * sizeof(struct g_slice), M_WAITOK | M_ZERO); 7192108Sphk gsp->nslice = nslice; 7292108Sphk return (gsp); 7392108Sphk} 7492108Sphk 7592108Sphkint 7692108Sphkg_slice_access(struct g_provider *pp, int dr, int dw, int de) 7792108Sphk{ 7892108Sphk int error, i; 7992108Sphk struct g_geom *gp; 8092108Sphk struct g_consumer *cp; 8192108Sphk struct g_provider *pp2; 8292108Sphk struct g_slicer *gsp; 8392108Sphk struct g_slice *gsl, *gsl2; 8492108Sphk 8592108Sphk gp = pp->geom; 8692108Sphk cp = LIST_FIRST(&gp->consumer); 8792108Sphk KASSERT (cp != NULL, ("g_slice_access but no consumer")); 8892108Sphk gsp = gp->softc; 8992108Sphk gsl = &gsp->slices[pp->index]; 9092108Sphk for (i = 0; i < gsp->nslice; i++) { 9192108Sphk gsl2 = &gsp->slices[i]; 9292108Sphk if (gsl2->length == 0) 9392108Sphk continue; 9492108Sphk if (i == pp->index) 9592108Sphk continue; 9692108Sphk if (gsl->offset + gsl->length <= gsl2->offset) 9792108Sphk continue; 9892108Sphk if (gsl2->offset + gsl2->length <= gsl->offset) 9992108Sphk continue; 10092108Sphk /* overlap */ 10192108Sphk pp2 = gsl2->provider; 10292108Sphk if ((pp->acw + dw) > 0 && pp2->ace > 0) 10392108Sphk return (EPERM); 10492108Sphk if ((pp->ace + de) > 0 && pp2->acw > 0) 10592108Sphk return (EPERM); 10692108Sphk } 10792108Sphk /* On first open, grab an extra "exclusive" bit */ 10892108Sphk if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 10992108Sphk de++; 11092108Sphk /* ... and let go of it on last close */ 11192108Sphk if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 11292108Sphk de--; 11392108Sphk error = g_access_rel(cp, dr, dw, de); 11492108Sphk return (error); 11592108Sphk} 11692108Sphk 11792108Sphkvoid 11892108Sphkg_slice_start(struct bio *bp) 11992108Sphk{ 12092108Sphk struct bio *bp2; 12192108Sphk struct g_provider *pp; 12292108Sphk struct g_geom *gp; 12392108Sphk struct g_consumer *cp; 12492108Sphk struct g_slicer *gsp; 12592108Sphk struct g_slice *gsl; 12692108Sphk int index; 12792108Sphk 12892108Sphk pp = bp->bio_to; 12992108Sphk gp = pp->geom; 13092108Sphk gsp = gp->softc; 13192108Sphk cp = LIST_FIRST(&gp->consumer); 13292108Sphk index = pp->index; 13392108Sphk switch(bp->bio_cmd) { 13492108Sphk case BIO_READ: 13592108Sphk case BIO_WRITE: 13692108Sphk case BIO_DELETE: 13792108Sphk gsl = &gsp->slices[index]; 13892108Sphk if (bp->bio_offset > gsl->length) { 13992108Sphk bp->bio_error = EINVAL; /* XXX: EWHAT ? */ 14092108Sphk g_io_deliver(bp); 14192108Sphk return; 14292108Sphk } 14392108Sphk bp2 = g_clone_bio(bp); 14492108Sphk if (bp2->bio_offset + bp2->bio_length > gsl->length) 14592108Sphk bp2->bio_length = gsl->length - bp2->bio_offset; 14692108Sphk bp2->bio_done = g_std_done; 14792108Sphk bp2->bio_offset += gsl->offset; 14892108Sphk g_io_request(bp2, cp); 14992108Sphk return; 15092108Sphk case BIO_GETATTR: 15192108Sphk case BIO_SETATTR: 15292108Sphk if (g_haveattr_off_t(bp, "GEOM::mediasize", 15392108Sphk gsp->slices[index].length)) 15492108Sphk return; 15592108Sphk if (gsp->start(bp)) 15692108Sphk return; 15792108Sphk bp2 = g_clone_bio(bp); 15892108Sphk bp2->bio_done = g_std_done; 15992108Sphk g_io_request(bp2, cp); 16092108Sphk break; 16192108Sphk default: 16292108Sphk bp->bio_error = EOPNOTSUPP; 16392108Sphk g_io_deliver(bp); 16492108Sphk return; 16592108Sphk } 16692108Sphk} 16792108Sphk 16892108Sphkvoid 16992108Sphkg_slice_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp) 17092108Sphk{ 17192108Sphk struct g_mbr_softc *mp; 17292108Sphk struct g_slicer *gsp; 17392108Sphk 17492108Sphk gsp = gp->softc; 17592108Sphk mp = gsp->softc; 17692108Sphk if (pp != NULL) { 17792108Sphk sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index); 17892108Sphk sbuf_printf(sb, "%s<length>%llu</length>\n", 17993326Sphk indent, (unsigned long long)gsp->slices[pp->index].length); 18093326Sphk sbuf_printf(sb, "%s<seclength>%llu</seclength>\n", indent, 18193326Sphk (unsigned long long)gsp->slices[pp->index].length / 512); 18293326Sphk sbuf_printf(sb, "%s<offset>%llu</offset>\n", indent, 18393326Sphk (unsigned long long)gsp->slices[pp->index].offset); 18493326Sphk sbuf_printf(sb, "%s<secoffset>%llu</secoffset>\n", indent, 18593326Sphk (unsigned long long)gsp->slices[pp->index].offset / 512); 18692108Sphk } 18792108Sphk} 18892108Sphk 18992108Sphkstruct g_provider * 19092108Sphkg_slice_addslice(struct g_geom *gp, int index, off_t offset, off_t length, char *fmt, ...) 19192108Sphk{ 19292108Sphk struct g_provider *pp; 19392108Sphk struct g_slicer *gsp; 19492108Sphk va_list ap; 19592108Sphk struct sbuf *sb; 19692108Sphk 19792108Sphk g_trace(G_T_TOPOLOGY, "g_slice_addslice()"); 19892108Sphk g_topology_lock(); 19992108Sphk gsp = gp->softc; 20092108Sphk va_start(ap, fmt); 20192108Sphk sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 20292108Sphk sbuf_vprintf(sb, fmt, ap); 20392108Sphk sbuf_finish(sb); 20492108Sphk pp = g_new_providerf(gp, sbuf_data(sb)); 20592108Sphk 20692108Sphk pp->index = index; 20792108Sphk gsp->slices[index].length = length; 20892108Sphk gsp->slices[index].offset = offset; 20992108Sphk gsp->slices[index].provider = pp; 21092108Sphk sbuf_delete(sb); 21192108Sphk g_topology_unlock(); 21292108Sphk return(pp); 21392108Sphk} 21492108Sphk 21592108Sphkstruct g_geom * 21693248Sphkg_slice_new(struct g_class *mp, int slices, struct g_provider *pp, struct g_consumer **cpp, void *extrap, int extra, g_slice_start_t *start) 21792108Sphk{ 21892108Sphk struct g_geom *gp; 21992108Sphk struct g_slicer *gsp; 22092108Sphk struct g_consumer *cp; 22192108Sphk void **vp; 22292108Sphk int error; 22392108Sphk 22492108Sphk g_topology_assert(); 22592108Sphk vp = (void **)extrap; 22692108Sphk gp = g_new_geomf(mp, "%s", pp->name); 22792108Sphk gsp = g_slice_init(slices, extra); 22892108Sphk gsp->start = start; 22992108Sphk gp->softc = gsp; 23092108Sphk gp->start = g_slice_start; 23192108Sphk gp->spoiled = g_std_spoiled; 23292108Sphk gp->dumpconf = g_slice_dumpconf; 23392108Sphk cp = g_new_consumer(gp); 23492108Sphk g_attach(cp, pp); 23592108Sphk error = g_access_rel(cp, 1, 0, 0); 23692108Sphk if (error) { 23792108Sphk g_dettach(cp); 23892108Sphk g_destroy_consumer(cp); 23992371Sphk g_free(gsp->slices); 24092108Sphk g_free(gp->softc); 24192108Sphk g_destroy_geom(gp); 24292108Sphk return (NULL); 24392108Sphk } 24492108Sphk *vp = gsp->softc; 24592108Sphk *cpp = cp; 24692108Sphk return (gp); 24792108Sphk} 24892108Sphk 24992108Sphkvoid 25093250Sphkg_slice_orphan(struct g_consumer *cp) 25192108Sphk{ 25292108Sphk struct g_geom *gp; 25392108Sphk struct g_provider *pp; 25492108Sphk int error; 25592108Sphk 25692108Sphk g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name); 25792108Sphk g_topology_assert(); 25892108Sphk KASSERT(cp->provider->error != 0, 25992108Sphk ("g_slice_orphan with error == 0")); 26092108Sphk 26192108Sphk gp = cp->geom; 26292108Sphk gp->flags |= G_GEOM_WITHER; 26392108Sphk /* First prevent any new requests */ 26492108Sphk error = cp->provider->error; 26592108Sphk LIST_FOREACH(pp, &gp->provider, provider) 26692108Sphk g_orphan_provider(pp, error); 26792108Sphk 26892108Sphk return; 26992108Sphk} 270