geom_slice.c revision 92371
1/*- 2 * Copyright (c) 2002 Poul-Henning Kamp 3 * Copyright (c) 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7 * and NAI Labs, the Security Research Division of Network Associates, Inc. 8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9 * DARPA CHATS research program. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. The names of the authors may not be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * $FreeBSD: head/sys/geom/geom_slice.c 92371 2002-03-15 21:41:41Z phk $ 36 */ 37 38 39#include <sys/param.h> 40#ifndef _KERNEL 41#include <stdio.h> 42#include <unistd.h> 43#include <stdlib.h> 44#include <signal.h> 45#include <err.h> 46#else 47#include <sys/systm.h> 48#include <sys/kernel.h> 49#include <sys/malloc.h> 50#include <sys/bio.h> 51#include <sys/sysctl.h> 52#include <sys/proc.h> 53#include <sys/kthread.h> 54#include <sys/lock.h> 55#include <sys/mutex.h> 56#endif 57#include <sys/errno.h> 58#include <sys/sbuf.h> 59#include <geom/geom.h> 60#include <geom/geom_slice.h> 61#include <machine/stdarg.h> 62 63struct g_slicer * 64g_slice_init(unsigned nslice, unsigned scsize) 65{ 66 struct g_slicer *gsp; 67 68 gsp = g_malloc(sizeof *gsp + nslice * sizeof(struct g_slice) + scsize, 69 M_WAITOK | M_ZERO); 70 gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO); 71 gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO); 72 gsp->slices = g_malloc(nslice * sizeof(struct g_slice), M_WAITOK | M_ZERO); 73 gsp->nslice = nslice; 74 return (gsp); 75} 76 77int 78g_slice_access(struct g_provider *pp, int dr, int dw, int de) 79{ 80 int error, i; 81 struct g_geom *gp; 82 struct g_consumer *cp; 83 struct g_provider *pp2; 84 struct g_slicer *gsp; 85 struct g_slice *gsl, *gsl2; 86 87 gp = pp->geom; 88 cp = LIST_FIRST(&gp->consumer); 89 KASSERT (cp != NULL, ("g_slice_access but no consumer")); 90 gsp = gp->softc; 91 gsl = &gsp->slices[pp->index]; 92 for (i = 0; i < gsp->nslice; i++) { 93 gsl2 = &gsp->slices[i]; 94 if (gsl2->length == 0) 95 continue; 96 if (i == pp->index) 97 continue; 98 if (gsl->offset + gsl->length <= gsl2->offset) 99 continue; 100 if (gsl2->offset + gsl2->length <= gsl->offset) 101 continue; 102 /* overlap */ 103 pp2 = gsl2->provider; 104 if ((pp->acw + dw) > 0 && pp2->ace > 0) 105 return (EPERM); 106 if ((pp->ace + de) > 0 && pp2->acw > 0) 107 return (EPERM); 108 } 109 /* On first open, grab an extra "exclusive" bit */ 110 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 111 de++; 112 /* ... and let go of it on last close */ 113 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 114 de--; 115 error = g_access_rel(cp, dr, dw, de); 116 return (error); 117} 118 119void 120g_slice_start(struct bio *bp) 121{ 122 struct bio *bp2; 123 struct g_provider *pp; 124 struct g_geom *gp; 125 struct g_consumer *cp; 126 struct g_slicer *gsp; 127 struct g_slice *gsl; 128 int index; 129 130 pp = bp->bio_to; 131 gp = pp->geom; 132 gsp = gp->softc; 133 cp = LIST_FIRST(&gp->consumer); 134 index = pp->index; 135 switch(bp->bio_cmd) { 136 case BIO_READ: 137 case BIO_WRITE: 138 case BIO_DELETE: 139 gsl = &gsp->slices[index]; 140 if (bp->bio_offset > gsl->length) { 141 bp->bio_error = EINVAL; /* XXX: EWHAT ? */ 142 g_io_deliver(bp); 143 return; 144 } 145 bp2 = g_clone_bio(bp); 146 if (bp2->bio_offset + bp2->bio_length > gsl->length) 147 bp2->bio_length = gsl->length - bp2->bio_offset; 148 bp2->bio_done = g_std_done; 149 bp2->bio_offset += gsl->offset; 150 g_io_request(bp2, cp); 151 return; 152 case BIO_GETATTR: 153 case BIO_SETATTR: 154 if (g_haveattr_off_t(bp, "GEOM::mediasize", 155 gsp->slices[index].length)) 156 return; 157 if (gsp->start(bp)) 158 return; 159 bp2 = g_clone_bio(bp); 160 bp2->bio_done = g_std_done; 161 g_io_request(bp2, cp); 162 break; 163 default: 164 bp->bio_error = EOPNOTSUPP; 165 g_io_deliver(bp); 166 return; 167 } 168} 169 170void 171g_slice_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp) 172{ 173 struct g_mbr_softc *mp; 174 struct g_slicer *gsp; 175 176 gsp = gp->softc; 177 mp = gsp->softc; 178 if (pp != NULL) { 179 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index); 180 sbuf_printf(sb, "%s<length>%llu</length>\n", 181 indent, gsp->slices[pp->index].length); 182 sbuf_printf(sb, "%s<seclength>%llu</seclength>\n", 183 indent, gsp->slices[pp->index].length / 512); 184 sbuf_printf(sb, "%s<offset>%llu</offset>\n", 185 indent, gsp->slices[pp->index].offset); 186 sbuf_printf(sb, "%s<secoffset>%llu</secoffset>\n", 187 indent, gsp->slices[pp->index].offset / 512); 188 } 189} 190 191struct g_provider * 192g_slice_addslice(struct g_geom *gp, int index, off_t offset, off_t length, char *fmt, ...) 193{ 194 struct g_provider *pp; 195 struct g_slicer *gsp; 196 va_list ap; 197 struct sbuf *sb; 198 199 g_trace(G_T_TOPOLOGY, "g_slice_addslice()"); 200 g_topology_lock(); 201 gsp = gp->softc; 202 va_start(ap, fmt); 203 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 204 sbuf_vprintf(sb, fmt, ap); 205 sbuf_finish(sb); 206 pp = g_new_providerf(gp, sbuf_data(sb)); 207 208 pp->index = index; 209 gsp->slices[index].length = length; 210 gsp->slices[index].offset = offset; 211 gsp->slices[index].provider = pp; 212 sbuf_delete(sb); 213 g_topology_unlock(); 214 return(pp); 215} 216 217struct g_geom * 218g_slice_new(struct g_method *mp, int slices, struct g_provider *pp, struct g_consumer **cpp, void *extrap, int extra, g_slice_start_t *start) 219{ 220 struct g_geom *gp; 221 struct g_slicer *gsp; 222 struct g_consumer *cp; 223 void **vp; 224 int error; 225 226 g_topology_assert(); 227 vp = (void **)extrap; 228 gp = g_new_geomf(mp, "%s", pp->name); 229 gsp = g_slice_init(slices, extra); 230 gsp->start = start; 231 gp->softc = gsp; 232 gp->start = g_slice_start; 233 gp->spoiled = g_std_spoiled; 234 gp->dumpconf = g_slice_dumpconf; 235 cp = g_new_consumer(gp); 236 g_attach(cp, pp); 237 error = g_access_rel(cp, 1, 0, 0); 238 if (error) { 239 g_dettach(cp); 240 g_destroy_consumer(cp); 241 g_free(gsp->slices); 242 g_free(gp->softc); 243 g_destroy_geom(gp); 244 return (NULL); 245 } 246 *vp = gsp->softc; 247 *cpp = cp; 248 return (gp); 249} 250 251void 252g_slice_orphan(struct g_consumer *cp, struct thread *tp __unused) 253{ 254 struct g_geom *gp; 255 struct g_provider *pp; 256 int error; 257 258 g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name); 259 g_topology_assert(); 260 KASSERT(cp->provider->error != 0, 261 ("g_slice_orphan with error == 0")); 262 263 gp = cp->geom; 264 gp->flags |= G_GEOM_WITHER; 265 /* First prevent any new requests */ 266 error = cp->provider->error; 267 LIST_FOREACH(pp, &gp->provider, provider) 268 g_orphan_provider(pp, error); 269 270 return; 271} 272