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