geom_slice.c revision 104081
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 104081 2002-09-28 08:16:50Z 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 <string.h> 46#include <err.h> 47#else 48#include <sys/systm.h> 49#include <sys/kernel.h> 50#include <sys/malloc.h> 51#include <sys/bio.h> 52#include <sys/sysctl.h> 53#include <sys/proc.h> 54#include <sys/kthread.h> 55#include <sys/lock.h> 56#include <sys/mutex.h> 57#endif 58#include <sys/errno.h> 59#include <sys/sbuf.h> 60#include <geom/geom.h> 61#include <geom/geom_slice.h> 62#include <machine/stdarg.h> 63 64static g_orphan_t g_slice_orphan; 65static g_access_t g_slice_access; 66static g_start_t g_slice_start; 67 68static struct g_slicer * 69g_slice_init(unsigned nslice, unsigned scsize) 70{ 71 struct g_slicer *gsp; 72 73 gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO); 74 gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO); 75 gsp->slices = g_malloc(nslice * sizeof(struct g_slice), 76 M_WAITOK | M_ZERO); 77 gsp->nslice = nslice; 78 return (gsp); 79} 80 81static int 82g_slice_access(struct g_provider *pp, int dr, int dw, int de) 83{ 84 int error, i; 85 struct g_geom *gp; 86 struct g_consumer *cp; 87 struct g_provider *pp2; 88 struct g_slicer *gsp; 89 struct g_slice *gsl, *gsl2; 90 91 gp = pp->geom; 92 cp = LIST_FIRST(&gp->consumer); 93 KASSERT (cp != NULL, ("g_slice_access but no consumer")); 94 gsp = gp->softc; 95 gsl = &gsp->slices[pp->index]; 96 for (i = 0; i < gsp->nslice; i++) { 97 gsl2 = &gsp->slices[i]; 98 if (gsl2->length == 0) 99 continue; 100 if (i == pp->index) 101 continue; 102 if (gsl->offset + gsl->length <= gsl2->offset) 103 continue; 104 if (gsl2->offset + gsl2->length <= gsl->offset) 105 continue; 106 /* overlap */ 107 pp2 = gsl2->provider; 108 if ((pp->acw + dw) > 0 && pp2->ace > 0) 109 return (EPERM); 110 if ((pp->ace + de) > 0 && pp2->acw > 0) 111 return (EPERM); 112 } 113 /* On first open, grab an extra "exclusive" bit */ 114 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 115 de++; 116 /* ... and let go of it on last close */ 117 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 118 de--; 119 error = g_access_rel(cp, dr, dw, de); 120 pp->mediasize = gsp->slices[pp->index].length; 121 return (error); 122} 123 124static void 125g_slice_start(struct bio *bp) 126{ 127 struct bio *bp2; 128 struct g_provider *pp; 129 struct g_geom *gp; 130 struct g_consumer *cp; 131 struct g_slicer *gsp; 132 struct g_slice *gsl; 133 int index; 134 off_t t; 135 136 pp = bp->bio_to; 137 gp = pp->geom; 138 gsp = gp->softc; 139 cp = LIST_FIRST(&gp->consumer); 140 index = pp->index; 141 gsl = &gsp->slices[index]; 142 switch(bp->bio_cmd) { 143 case BIO_READ: 144 case BIO_WRITE: 145 case BIO_DELETE: 146 if (bp->bio_offset > gsl->length) { 147 bp->bio_error = EINVAL; /* XXX: EWHAT ? */ 148 g_io_deliver(bp); 149 return; 150 } 151 bp2 = g_clone_bio(bp); 152 if (bp2 == NULL) { 153 g_io_fail(bp, ENOMEM); 154 return; 155 } 156 if (bp2->bio_offset + bp2->bio_length > gsl->length) 157 bp2->bio_length = gsl->length - bp2->bio_offset; 158 bp2->bio_done = g_std_done; 159 bp2->bio_offset += gsl->offset; 160 g_io_request(bp2, cp); 161 return; 162 case BIO_GETATTR: 163 case BIO_SETATTR: 164 /* Give the real method a chance to override */ 165 if (gsp->start(bp)) 166 return; 167 if (g_handleattr_off_t(bp, "GEOM::mediasize", 168 gsp->slices[index].length)) 169 return; 170 if (!strcmp("GEOM::frontstuff", bp->bio_attribute)) { 171 t = gsp->cfrontstuff; 172 if (gsp->frontstuff > t) 173 t = gsp->frontstuff; 174 t -= gsl->offset; 175 if (t < 0) 176 t = 0; 177 if (t > gsl->length) 178 t = gsl->length; 179 g_handleattr_off_t(bp, "GEOM::frontstuff", t); 180 return; 181 } 182#ifdef _KERNEL 183 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { 184 struct g_kerneldump *gkd; 185 186 gkd = (struct g_kerneldump *)bp->bio_data; 187 gkd->offset += gsp->slices[index].offset; 188 if (gkd->length > gsp->slices[index].length) 189 gkd->length = gsp->slices[index].length; 190 /* now, pass it on downwards... */ 191 } 192#endif 193 bp2 = g_clone_bio(bp); 194 if (bp2 == NULL) { 195 g_io_fail(bp, ENOMEM); 196 return; 197 } 198 bp2->bio_done = g_std_done; 199 g_io_request(bp2, cp); 200 break; 201 default: 202 bp->bio_error = EOPNOTSUPP; 203 g_io_deliver(bp); 204 return; 205 } 206} 207 208void 209g_slice_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp __unused, struct g_provider *pp) 210{ 211 struct g_mbr_softc *mp; 212 struct g_slicer *gsp; 213 214 gsp = gp->softc; 215 mp = gsp->softc; 216 if (gp != NULL && (pp == NULL && cp == NULL)) { 217 sbuf_printf(sb, "%s<frontstuff>%llu</frontstuff>\n", 218 indent, (unsigned long long)gsp->frontstuff); 219 } 220 if (pp != NULL) { 221 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index); 222 sbuf_printf(sb, "%s<length>%llu</length>\n", 223 indent, (unsigned long long)gsp->slices[pp->index].length); 224 sbuf_printf(sb, "%s<seclength>%llu</seclength>\n", indent, 225 (unsigned long long)gsp->slices[pp->index].length / 512); 226 sbuf_printf(sb, "%s<offset>%llu</offset>\n", indent, 227 (unsigned long long)gsp->slices[pp->index].offset); 228 sbuf_printf(sb, "%s<secoffset>%llu</secoffset>\n", indent, 229 (unsigned long long)gsp->slices[pp->index].offset / 512); 230 } 231} 232 233int 234g_slice_config(struct g_geom *gp, int index, int how, off_t offset, off_t length, char *fmt, ...) 235{ 236 struct g_provider *pp; 237 struct g_slicer *gsp; 238 struct g_slice *gsl; 239 va_list ap; 240 struct sbuf *sb; 241 int error, acc; 242 243 g_trace(G_T_TOPOLOGY, "g_slice_config()"); 244 g_topology_assert(); 245 gsp = gp->softc; 246 error = 0; 247 if (index >= gsp->nslice) 248 return(EINVAL); 249 gsl = &gsp->slices[index]; 250 pp = gsl->provider; 251 if (pp != NULL) 252 acc = pp->acr + pp->acw + pp->ace; 253 else 254 acc = 0; 255 if (acc != 0 && how != G_SLICE_CONFIG_FORCE) { 256 if (length < gsl->length) 257 return(EBUSY); 258 if (offset != gsl->offset) 259 return(EBUSY); 260 } 261 /* XXX: check offset + length <= MEDIASIZE */ 262 if (how == G_SLICE_CONFIG_CHECK) 263 return (0); 264 gsl->length = length; 265 gsl->offset = offset; 266 if (length != 0 && pp != NULL) 267 return (0); 268 if (length == 0 && pp == NULL) 269 return (0); 270 if (length == 0 && pp != NULL) { 271 g_orphan_provider(pp, ENXIO); 272 gsl->provider = NULL; 273 gsp->nprovider--; 274 return (0); 275 } 276 va_start(ap, fmt); 277 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 278 sbuf_vprintf(sb, fmt, ap); 279 sbuf_finish(sb); 280 pp = g_new_providerf(gp, sbuf_data(sb)); 281 pp->index = index; 282 gsl->provider = pp; 283 gsp->nprovider++; 284 g_error_provider(pp, 0); 285 sbuf_delete(sb); 286 return(0); 287} 288 289struct g_provider * 290g_slice_addslice(struct g_geom *gp, int index, off_t offset, off_t length, char *fmt, ...) 291{ 292 struct g_provider *pp; 293 struct g_slicer *gsp; 294 va_list ap; 295 struct sbuf *sb; 296 297 g_trace(G_T_TOPOLOGY, "g_slice_addslice()"); 298 g_topology_assert(); 299 gsp = gp->softc; 300 va_start(ap, fmt); 301 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 302 sbuf_vprintf(sb, fmt, ap); 303 sbuf_finish(sb); 304 pp = g_new_providerf(gp, sbuf_data(sb)); 305 306 pp->index = index; 307 gsp->slices[index].length = length; 308 gsp->slices[index].offset = offset; 309 gsp->slices[index].provider = pp; 310 sbuf_delete(sb); 311 return(pp); 312} 313 314struct g_geom * 315g_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) 316{ 317 struct g_geom *gp; 318 struct g_slicer *gsp; 319 struct g_consumer *cp; 320 void **vp; 321 int error, i; 322 323 g_topology_assert(); 324 vp = (void **)extrap; 325 gp = g_new_geomf(mp, "%s", pp->name); 326 gsp = g_slice_init(slices, extra); 327 gsp->start = start; 328 gp->access = g_slice_access; 329 gp->orphan = g_slice_orphan; 330 gp->softc = gsp; 331 gp->start = g_slice_start; 332 gp->spoiled = g_std_spoiled; 333 gp->dumpconf = g_slice_dumpconf; 334 cp = g_new_consumer(gp); 335 g_attach(cp, pp); 336 error = g_access_rel(cp, 1, 0, 0); 337 if (error) { 338 g_detach(cp); 339 g_destroy_consumer(cp); 340 g_free(gsp->slices); 341 g_free(gp->softc); 342 g_destroy_geom(gp); 343 return (NULL); 344 } 345 /* Find out if there are any magic bytes on the consumer */ 346 i = sizeof gsp->cfrontstuff; 347 error = g_io_getattr("GEOM::frontstuff", cp, &i, &gsp->cfrontstuff); 348 if (error) 349 gsp->cfrontstuff = 0; 350 *vp = gsp->softc; 351 *cpp = cp; 352 return (gp); 353} 354 355static void 356g_slice_orphan(struct g_consumer *cp) 357{ 358 struct g_geom *gp; 359 struct g_provider *pp; 360 int error; 361 362 g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name); 363 g_topology_assert(); 364 KASSERT(cp->provider->error != 0, 365 ("g_slice_orphan with error == 0")); 366 367 gp = cp->geom; 368 gp->flags |= G_GEOM_WITHER; 369 error = cp->provider->error; 370 LIST_FOREACH(pp, &gp->provider, provider) 371 g_orphan_provider(pp, error); 372 return; 373} 374