geom_slice.c revision 105957
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 105957 2002-10-25 20:09:45Z phk $ 3692108Sphk */ 3792108Sphk 3892108Sphk 3992108Sphk#include <sys/param.h> 40105540Sphk#include <sys/stdint.h> 4192108Sphk#ifndef _KERNEL 4292108Sphk#include <stdio.h> 4392108Sphk#include <unistd.h> 4492108Sphk#include <stdlib.h> 4592108Sphk#include <signal.h> 4696952Sphk#include <string.h> 4792108Sphk#include <err.h> 4892108Sphk#else 4992108Sphk#include <sys/systm.h> 5092108Sphk#include <sys/kernel.h> 5192108Sphk#include <sys/malloc.h> 5292108Sphk#include <sys/bio.h> 5392108Sphk#include <sys/sysctl.h> 5492108Sphk#include <sys/proc.h> 5592108Sphk#include <sys/kthread.h> 5692108Sphk#include <sys/lock.h> 5792108Sphk#include <sys/mutex.h> 5892108Sphk#endif 5992108Sphk#include <sys/errno.h> 6092108Sphk#include <sys/sbuf.h> 6192108Sphk#include <geom/geom.h> 6292108Sphk#include <geom/geom_slice.h> 6392108Sphk#include <machine/stdarg.h> 6492108Sphk 6593776Sphkstatic g_orphan_t g_slice_orphan; 6693776Sphkstatic g_access_t g_slice_access; 6793776Sphkstatic g_start_t g_slice_start; 6893776Sphk 6995321Sphkstatic struct g_slicer * 7092108Sphkg_slice_init(unsigned nslice, unsigned scsize) 7192108Sphk{ 7292108Sphk struct g_slicer *gsp; 7392108Sphk 7492371Sphk gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO); 7592371Sphk gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO); 76104057Sphk gsp->slices = g_malloc(nslice * sizeof(struct g_slice), 77104057Sphk M_WAITOK | M_ZERO); 7892108Sphk gsp->nslice = nslice; 7992108Sphk return (gsp); 8092108Sphk} 8192108Sphk 8293776Sphkstatic int 8392108Sphkg_slice_access(struct g_provider *pp, int dr, int dw, int de) 8492108Sphk{ 8592108Sphk int error, i; 8692108Sphk struct g_geom *gp; 8792108Sphk struct g_consumer *cp; 8892108Sphk struct g_provider *pp2; 8992108Sphk struct g_slicer *gsp; 9092108Sphk struct g_slice *gsl, *gsl2; 9192108Sphk 9292108Sphk gp = pp->geom; 9392108Sphk cp = LIST_FIRST(&gp->consumer); 9492108Sphk KASSERT (cp != NULL, ("g_slice_access but no consumer")); 9592108Sphk gsp = gp->softc; 9692108Sphk gsl = &gsp->slices[pp->index]; 9792108Sphk for (i = 0; i < gsp->nslice; i++) { 9892108Sphk gsl2 = &gsp->slices[i]; 9992108Sphk if (gsl2->length == 0) 10092108Sphk continue; 10192108Sphk if (i == pp->index) 10292108Sphk continue; 10392108Sphk if (gsl->offset + gsl->length <= gsl2->offset) 10492108Sphk continue; 10592108Sphk if (gsl2->offset + gsl2->length <= gsl->offset) 10692108Sphk continue; 10792108Sphk /* overlap */ 10892108Sphk pp2 = gsl2->provider; 10992108Sphk if ((pp->acw + dw) > 0 && pp2->ace > 0) 11092108Sphk return (EPERM); 11192108Sphk if ((pp->ace + de) > 0 && pp2->acw > 0) 11292108Sphk return (EPERM); 11392108Sphk } 11492108Sphk /* On first open, grab an extra "exclusive" bit */ 11592108Sphk if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 11692108Sphk de++; 11792108Sphk /* ... and let go of it on last close */ 11892108Sphk if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 11992108Sphk de--; 12092108Sphk error = g_access_rel(cp, dr, dw, de); 12192108Sphk return (error); 12292108Sphk} 12392108Sphk 12493776Sphkstatic void 12592108Sphkg_slice_start(struct bio *bp) 12692108Sphk{ 12792108Sphk struct bio *bp2; 12892108Sphk struct g_provider *pp; 12992108Sphk struct g_geom *gp; 13092108Sphk struct g_consumer *cp; 13192108Sphk struct g_slicer *gsp; 13292108Sphk struct g_slice *gsl; 13392108Sphk int index; 13494287Sphk off_t t; 13592108Sphk 13692108Sphk pp = bp->bio_to; 13792108Sphk gp = pp->geom; 13892108Sphk gsp = gp->softc; 13992108Sphk cp = LIST_FIRST(&gp->consumer); 14092108Sphk index = pp->index; 14194287Sphk gsl = &gsp->slices[index]; 14292108Sphk switch(bp->bio_cmd) { 14392108Sphk case BIO_READ: 14492108Sphk case BIO_WRITE: 14592108Sphk case BIO_DELETE: 14692108Sphk if (bp->bio_offset > gsl->length) { 147104195Sphk g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */ 14892108Sphk return; 14992108Sphk } 15092108Sphk bp2 = g_clone_bio(bp); 151104057Sphk if (bp2 == NULL) { 152104195Sphk g_io_deliver(bp, ENOMEM); 153104057Sphk return; 154104057Sphk } 15592108Sphk if (bp2->bio_offset + bp2->bio_length > gsl->length) 15692108Sphk bp2->bio_length = gsl->length - bp2->bio_offset; 15792108Sphk bp2->bio_done = g_std_done; 15892108Sphk bp2->bio_offset += gsl->offset; 15992108Sphk g_io_request(bp2, cp); 16092108Sphk return; 16192108Sphk case BIO_GETATTR: 16292108Sphk case BIO_SETATTR: 16394287Sphk /* Give the real method a chance to override */ 16494287Sphk if (gsp->start(bp)) 16594287Sphk return; 16694287Sphk if (!strcmp("GEOM::frontstuff", bp->bio_attribute)) { 16794287Sphk t = gsp->cfrontstuff; 16894287Sphk if (gsp->frontstuff > t) 16994287Sphk t = gsp->frontstuff; 17094287Sphk t -= gsl->offset; 17194287Sphk if (t < 0) 17294287Sphk t = 0; 17394287Sphk if (t > gsl->length) 17494287Sphk t = gsl->length; 17598066Sphk g_handleattr_off_t(bp, "GEOM::frontstuff", t); 17692108Sphk return; 17794287Sphk } 17895310Sphk#ifdef _KERNEL 17995038Sphk if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { 18095038Sphk struct g_kerneldump *gkd; 18195038Sphk 18295038Sphk gkd = (struct g_kerneldump *)bp->bio_data; 18395038Sphk gkd->offset += gsp->slices[index].offset; 18495038Sphk if (gkd->length > gsp->slices[index].length) 18595038Sphk gkd->length = gsp->slices[index].length; 18695038Sphk /* now, pass it on downwards... */ 18795038Sphk } 18895310Sphk#endif 18992108Sphk bp2 = g_clone_bio(bp); 190104081Sphk if (bp2 == NULL) { 191104195Sphk g_io_deliver(bp, ENOMEM); 192104081Sphk return; 193104081Sphk } 19492108Sphk bp2->bio_done = g_std_done; 19592108Sphk g_io_request(bp2, cp); 19692108Sphk break; 19792108Sphk default: 198104195Sphk g_io_deliver(bp, EOPNOTSUPP); 19992108Sphk return; 20092108Sphk } 20192108Sphk} 20292108Sphk 20392108Sphkvoid 204104087Sphkg_slice_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 20592108Sphk{ 20692108Sphk struct g_slicer *gsp; 20792108Sphk 20892108Sphk gsp = gp->softc; 20995323Sphk if (gp != NULL && (pp == NULL && cp == NULL)) { 210105540Sphk sbuf_printf(sb, "%s<frontstuff>%ju</frontstuff>\n", 211105540Sphk indent, (intmax_t)gsp->frontstuff); 21294287Sphk } 21392108Sphk if (pp != NULL) { 21492108Sphk sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index); 215105540Sphk sbuf_printf(sb, "%s<length>%ju</length>\n", 216105540Sphk indent, (uintmax_t)gsp->slices[pp->index].length); 217105540Sphk sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent, 218105540Sphk (uintmax_t)gsp->slices[pp->index].length / 512); 219105540Sphk sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent, 220105540Sphk (uintmax_t)gsp->slices[pp->index].offset); 221105540Sphk sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent, 222105540Sphk (uintmax_t)gsp->slices[pp->index].offset / 512); 22392108Sphk } 22492108Sphk} 22592108Sphk 226104064Sphkint 227105542Sphkg_slice_config(struct g_geom *gp, int index, int how, off_t offset, off_t length, u_int sectorsize, char *fmt, ...) 228104064Sphk{ 229104064Sphk struct g_provider *pp; 230104064Sphk struct g_slicer *gsp; 231104064Sphk struct g_slice *gsl; 232104064Sphk va_list ap; 233104064Sphk struct sbuf *sb; 234104064Sphk int error, acc; 235104064Sphk 236104195Sphk g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)", 237104195Sphk gp->name, index, how); 238104064Sphk g_topology_assert(); 239104064Sphk gsp = gp->softc; 240104064Sphk error = 0; 241104064Sphk if (index >= gsp->nslice) 242104064Sphk return(EINVAL); 243104064Sphk gsl = &gsp->slices[index]; 244104064Sphk pp = gsl->provider; 245104064Sphk if (pp != NULL) 246104064Sphk acc = pp->acr + pp->acw + pp->ace; 247104064Sphk else 248104064Sphk acc = 0; 249104064Sphk if (acc != 0 && how != G_SLICE_CONFIG_FORCE) { 250104064Sphk if (length < gsl->length) 251104064Sphk return(EBUSY); 252104064Sphk if (offset != gsl->offset) 253104064Sphk return(EBUSY); 254104064Sphk } 255104064Sphk /* XXX: check offset + length <= MEDIASIZE */ 256104064Sphk if (how == G_SLICE_CONFIG_CHECK) 257104064Sphk return (0); 258104064Sphk gsl->length = length; 259104064Sphk gsl->offset = offset; 260105542Sphk gsl->sectorsize = sectorsize; 261105957Sphk if (length == 0) { 262105957Sphk if (pp == NULL) 263105957Sphk return (0); 264105957Sphk if (bootverbose) 265105957Sphk printf("GEOM: Deconfigure %s\n", pp->name); 266104064Sphk g_orphan_provider(pp, ENXIO); 267104064Sphk gsl->provider = NULL; 268104064Sphk gsp->nprovider--; 269104064Sphk return (0); 270104064Sphk } 271105957Sphk if (pp != NULL) { 272105957Sphk if (bootverbose) 273105957Sphk printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n", 274105957Sphk pp->name, (intmax_t)offset, (intmax_t)length, 275105957Sphk (intmax_t)(offset + length - 1)); 276105957Sphk return (0); 277105957Sphk } 278104064Sphk va_start(ap, fmt); 279104064Sphk sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 280104064Sphk sbuf_vprintf(sb, fmt, ap); 281104064Sphk sbuf_finish(sb); 282104064Sphk pp = g_new_providerf(gp, sbuf_data(sb)); 283105957Sphk if (bootverbose) 284105957Sphk printf("GEOM: Configure %s, start %jd length %jd end %jd\n", 285105957Sphk pp->name, (intmax_t)offset, (intmax_t)length, 286105957Sphk (intmax_t)(offset + length - 1)); 287104064Sphk pp->index = index; 288105542Sphk pp->mediasize = gsl->length; 289105542Sphk pp->sectorsize = gsl->sectorsize; 290104064Sphk gsl->provider = pp; 291104064Sphk gsp->nprovider++; 292104064Sphk g_error_provider(pp, 0); 293104064Sphk sbuf_delete(sb); 294104064Sphk return(0); 295104064Sphk} 296104064Sphk 29792108Sphkstruct g_provider * 298105542Sphkg_slice_addslice(struct g_geom *gp, int index, off_t offset, off_t length, u_int sectorsize, char *fmt, ...) 29992108Sphk{ 30092108Sphk struct g_provider *pp; 30192108Sphk struct g_slicer *gsp; 30292108Sphk va_list ap; 30392108Sphk struct sbuf *sb; 30492108Sphk 30592108Sphk g_trace(G_T_TOPOLOGY, "g_slice_addslice()"); 306104064Sphk g_topology_assert(); 30792108Sphk gsp = gp->softc; 30892108Sphk va_start(ap, fmt); 30992108Sphk sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 31092108Sphk sbuf_vprintf(sb, fmt, ap); 31192108Sphk sbuf_finish(sb); 31292108Sphk pp = g_new_providerf(gp, sbuf_data(sb)); 31392108Sphk 31492108Sphk pp->index = index; 31592108Sphk gsp->slices[index].length = length; 31692108Sphk gsp->slices[index].offset = offset; 31792108Sphk gsp->slices[index].provider = pp; 318105542Sphk gsp->slices[index].sectorsize = sectorsize; 319105542Sphk pp->mediasize = gsp->slices[index].length; 320105542Sphk pp->sectorsize = gsp->slices[index].sectorsize; 32192108Sphk sbuf_delete(sb); 322105957Sphk if (bootverbose) 323105957Sphk printf("GEOM: Add %s, start %jd length %jd end %jd\n", 324105957Sphk pp->name, (intmax_t)offset, (intmax_t)length, 325105957Sphk (intmax_t)(offset + length - 1)); 32692108Sphk return(pp); 32792108Sphk} 32892108Sphk 32992108Sphkstruct g_geom * 33093248Sphkg_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) 33192108Sphk{ 33292108Sphk struct g_geom *gp; 33392108Sphk struct g_slicer *gsp; 33492108Sphk struct g_consumer *cp; 33592108Sphk void **vp; 33694287Sphk int error, i; 33792108Sphk 33892108Sphk g_topology_assert(); 33992108Sphk vp = (void **)extrap; 34092108Sphk gp = g_new_geomf(mp, "%s", pp->name); 34192108Sphk gsp = g_slice_init(slices, extra); 34292108Sphk gsp->start = start; 34393776Sphk gp->access = g_slice_access; 34493776Sphk gp->orphan = g_slice_orphan; 34592108Sphk gp->softc = gsp; 34692108Sphk gp->start = g_slice_start; 34792108Sphk gp->spoiled = g_std_spoiled; 34892108Sphk gp->dumpconf = g_slice_dumpconf; 34992108Sphk cp = g_new_consumer(gp); 350105551Sphk error = g_attach(cp, pp); 351105551Sphk if (error == 0) 352105551Sphk error = g_access_rel(cp, 1, 0, 0); 35392108Sphk if (error) { 354105551Sphk if (cp->provider != NULL) 355105551Sphk g_detach(cp); 35692108Sphk g_destroy_consumer(cp); 35792371Sphk g_free(gsp->slices); 35892108Sphk g_free(gp->softc); 35992108Sphk g_destroy_geom(gp); 36092108Sphk return (NULL); 36192108Sphk } 36294287Sphk /* Find out if there are any magic bytes on the consumer */ 36394287Sphk i = sizeof gsp->cfrontstuff; 36494287Sphk error = g_io_getattr("GEOM::frontstuff", cp, &i, &gsp->cfrontstuff); 36594287Sphk if (error) 36694287Sphk gsp->cfrontstuff = 0; 36792108Sphk *vp = gsp->softc; 36892108Sphk *cpp = cp; 36992108Sphk return (gp); 37092108Sphk} 37192108Sphk 37293776Sphkstatic void 37393250Sphkg_slice_orphan(struct g_consumer *cp) 37492108Sphk{ 37592108Sphk struct g_geom *gp; 37692108Sphk struct g_provider *pp; 37792108Sphk int error; 37892108Sphk 37992108Sphk g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name); 38092108Sphk g_topology_assert(); 38192108Sphk KASSERT(cp->provider->error != 0, 38292108Sphk ("g_slice_orphan with error == 0")); 38392108Sphk 38492108Sphk gp = cp->geom; 38592108Sphk gp->flags |= G_GEOM_WITHER; 38692108Sphk error = cp->provider->error; 38792108Sphk LIST_FOREACH(pp, &gp->provider, provider) 38892108Sphk g_orphan_provider(pp, error); 38992108Sphk return; 39092108Sphk} 391