geom_slice.c revision 113390
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 113390 2003-04-12 08:41:26Z 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; 85 u_int u; 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 (u = 0; u < gsp->nslice; u++) { 98 gsl2 = &gsp->slices[u]; 99 if (gsl2->length == 0) 100 continue; 101 if (u == 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 return (error); 122} 123 124void 125g_slice_finish_hot(struct bio *bp) 126{ 127 struct bio *bp2; 128 struct g_geom *gp; 129 struct g_consumer *cp; 130 struct g_slicer *gsp; 131 struct g_slice *gsl; 132 int idx; 133 134 KASSERT(bp->bio_to != NULL, ("NULL bio_to in g_slice_finish_hot(%p)", bp)); 135 KASSERT(bp->bio_from != NULL, ("NULL bio_from in g_slice_finish_hot(%p)", bp)); 136 gp = bp->bio_to->geom; 137 gsp = gp->softc; 138 cp = LIST_FIRST(&gp->consumer); 139 KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp)); 140 idx = bp->bio_to->index; 141 gsl = &gsp->slices[idx]; 142 143 bp2 = g_clone_bio(bp); 144 if (bp2 == NULL) { 145 g_io_deliver(bp, ENOMEM); 146 return; 147 } 148 if (bp2->bio_offset + bp2->bio_length > gsl->length) 149 bp2->bio_length = gsl->length - bp2->bio_offset; 150 bp2->bio_done = g_std_done; 151 bp2->bio_offset += gsl->offset; 152 g_io_request(bp2, cp); 153 return; 154} 155 156static void 157g_slice_start(struct bio *bp) 158{ 159 struct bio *bp2; 160 struct g_provider *pp; 161 struct g_geom *gp; 162 struct g_consumer *cp; 163 struct g_slicer *gsp; 164 struct g_slice *gsl, *gmp; 165 int idx, error; 166 u_int m_index; 167 off_t t; 168 169 pp = bp->bio_to; 170 gp = pp->geom; 171 gsp = gp->softc; 172 cp = LIST_FIRST(&gp->consumer); 173 idx = pp->index; 174 gsl = &gsp->slices[idx]; 175 switch(bp->bio_cmd) { 176 case BIO_READ: 177 case BIO_WRITE: 178 case BIO_DELETE: 179 if (bp->bio_offset > gsl->length) { 180 g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */ 181 return; 182 } 183 /* 184 * Check if we collide with any hot spaces, and call the 185 * method once if so. 186 */ 187 t = bp->bio_offset + gsl->offset; 188 /* .ctl devices may take us negative */ 189 if (t < 0 || (t + bp->bio_length) < 0) { 190 g_io_deliver(bp, EINVAL); 191 return; 192 } 193 for (m_index = 0; m_index < gsp->nhot; m_index++) { 194 gmp = &gsp->hot[m_index]; 195 if (t >= gmp->offset + gmp->length) 196 continue; 197 if (t + bp->bio_length <= gmp->offset) 198 continue; 199 error = gsp->start(bp); 200 if (error == EJUSTRETURN) 201 return; 202 else if (error) { 203 g_io_deliver(bp, error); 204 return; 205 } 206 break; 207 } 208 bp2 = g_clone_bio(bp); 209 if (bp2 == NULL) { 210 g_io_deliver(bp, ENOMEM); 211 return; 212 } 213 if (bp2->bio_offset + bp2->bio_length > gsl->length) 214 bp2->bio_length = gsl->length - bp2->bio_offset; 215 bp2->bio_done = g_std_done; 216 bp2->bio_offset += gsl->offset; 217 g_io_request(bp2, cp); 218 return; 219 case BIO_GETATTR: 220 /* Give the real method a chance to override */ 221 if (gsp->start(bp)) 222 return; 223#ifdef _KERNEL 224 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { 225 struct g_kerneldump *gkd; 226 227 gkd = (struct g_kerneldump *)bp->bio_data; 228 gkd->offset += gsp->slices[idx].offset; 229 if (gkd->length > gsp->slices[idx].length) 230 gkd->length = gsp->slices[idx].length; 231 /* now, pass it on downwards... */ 232 } 233#endif 234 bp2 = g_clone_bio(bp); 235 if (bp2 == NULL) { 236 g_io_deliver(bp, ENOMEM); 237 return; 238 } 239 bp2->bio_done = g_std_done; 240 g_io_request(bp2, cp); 241 break; 242 default: 243 g_io_deliver(bp, EOPNOTSUPP); 244 return; 245 } 246} 247 248void 249g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 250{ 251 struct g_slicer *gsp; 252 253 gsp = gp->softc; 254 if (indent == NULL) { 255 sbuf_printf(sb, " i %u", pp->index); 256 sbuf_printf(sb, " o %ju", 257 (uintmax_t)gsp->slices[pp->index].offset); 258 return; 259 } 260 if (pp != NULL) { 261 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index); 262 sbuf_printf(sb, "%s<length>%ju</length>\n", 263 indent, (uintmax_t)gsp->slices[pp->index].length); 264 sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent, 265 (uintmax_t)gsp->slices[pp->index].length / 512); 266 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent, 267 (uintmax_t)gsp->slices[pp->index].offset); 268 sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent, 269 (uintmax_t)gsp->slices[pp->index].offset / 512); 270 } 271} 272 273int 274g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...) 275{ 276 struct g_provider *pp, *pp2; 277 struct g_slicer *gsp; 278 struct g_slice *gsl; 279 va_list ap; 280 struct sbuf *sb; 281 int error, acc; 282 283 g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)", 284 gp->name, idx, how); 285 g_topology_assert(); 286 gsp = gp->softc; 287 error = 0; 288 if (idx >= gsp->nslice) 289 return(EINVAL); 290 gsl = &gsp->slices[idx]; 291 pp = gsl->provider; 292 if (pp != NULL) 293 acc = pp->acr + pp->acw + pp->ace; 294 else 295 acc = 0; 296 if (acc != 0 && how != G_SLICE_CONFIG_FORCE) { 297 if (length < gsl->length) 298 return(EBUSY); 299 if (offset != gsl->offset) 300 return(EBUSY); 301 } 302 /* XXX: check offset + length <= MEDIASIZE */ 303 if (how == G_SLICE_CONFIG_CHECK) 304 return (0); 305 gsl->length = length; 306 gsl->offset = offset; 307 gsl->sectorsize = sectorsize; 308 if (length == 0) { 309 if (pp == NULL) 310 return (0); 311 if (bootverbose) 312 printf("GEOM: Deconfigure %s\n", pp->name); 313 g_orphan_provider(pp, ENXIO); 314 gsl->provider = NULL; 315 gsp->nprovider--; 316 return (0); 317 } 318 if (pp != NULL) { 319 if (bootverbose) 320 printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n", 321 pp->name, (intmax_t)offset, (intmax_t)length, 322 (intmax_t)(offset + length - 1)); 323 pp->mediasize = gsl->length; 324 return (0); 325 } 326 va_start(ap, fmt); 327 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 328 sbuf_vprintf(sb, fmt, ap); 329 sbuf_finish(sb); 330 pp = g_new_providerf(gp, sbuf_data(sb)); 331 pp2 = LIST_FIRST(&gp->consumer)->provider; 332 pp->flags = pp2->flags & G_PF_CANDELETE; 333 if (pp2->stripesize > 0) { 334 pp->stripesize = pp2->stripesize; 335 pp->stripeoffset = (pp2->stripeoffset + offset) % pp->stripesize; 336 } 337 if (bootverbose) 338 printf("GEOM: Configure %s, start %jd length %jd end %jd\n", 339 pp->name, (intmax_t)offset, (intmax_t)length, 340 (intmax_t)(offset + length - 1)); 341 pp->index = idx; 342 pp->mediasize = gsl->length; 343 pp->sectorsize = gsl->sectorsize; 344 gsl->provider = pp; 345 gsp->nprovider++; 346 g_error_provider(pp, 0); 347 sbuf_delete(sb); 348 return(0); 349} 350 351int 352g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length) 353{ 354 struct g_slicer *gsp; 355 struct g_slice *gsl, *gsl2; 356 357 g_trace(G_T_TOPOLOGY, "g_slice_conf_hot()"); 358 g_topology_assert(); 359 gsp = gp->softc; 360 gsl = gsp->hot; 361 if(idx >= gsp->nhot) { 362 gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO); 363 if (gsp->hot != NULL) 364 bcopy(gsp->hot, gsl2, gsp->nhot * sizeof *gsl2); 365 gsp->hot = gsl2; 366 if (gsp->hot != NULL) 367 g_free(gsl); 368 gsl = gsl2; 369 gsp->nhot = idx + 1; 370 } 371 if (bootverbose) 372 printf("GEOM: Add %s hot[%d] start %jd length %jd end %jd\n", 373 gp->name, idx, (intmax_t)offset, (intmax_t)length, 374 (intmax_t)(offset + length - 1)); 375 gsl[idx].offset = offset; 376 gsl[idx].length = length; 377 return (0); 378} 379 380struct g_geom * 381g_slice_new(struct g_class *mp, u_int slices, struct g_provider *pp, struct g_consumer **cpp, void *extrap, int extra, g_slice_start_t *start) 382{ 383 struct g_geom *gp; 384 struct g_slicer *gsp; 385 struct g_consumer *cp; 386 void **vp; 387 int error; 388 389 g_topology_assert(); 390 vp = (void **)extrap; 391 gp = g_new_geomf(mp, "%s", pp->name); 392 gsp = g_slice_init(slices, extra); 393 gsp->start = start; 394 gp->access = g_slice_access; 395 gp->orphan = g_slice_orphan; 396 gp->softc = gsp; 397 gp->start = g_slice_start; 398 gp->spoiled = g_std_spoiled; 399 gp->dumpconf = g_slice_dumpconf; 400 cp = g_new_consumer(gp); 401 error = g_attach(cp, pp); 402 if (error == 0) 403 error = g_access_rel(cp, 1, 0, 0); 404 if (error) { 405 if (cp->provider != NULL) 406 g_detach(cp); 407 g_destroy_consumer(cp); 408 g_free(gsp->slices); 409 g_free(gp->softc); 410 g_destroy_geom(gp); 411 return (NULL); 412 } 413 *vp = gsp->softc; 414 *cpp = cp; 415 return (gp); 416} 417 418static void 419g_slice_orphan(struct g_consumer *cp) 420{ 421 struct g_geom *gp; 422 struct g_provider *pp; 423 int error; 424 425 g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name); 426 g_topology_assert(); 427 KASSERT(cp->provider->error != 0, 428 ("g_slice_orphan with error == 0")); 429 430 gp = cp->geom; 431 /* XXX: Not good enough we leak the softc and its suballocations */ 432 gp->flags |= G_GEOM_WITHER; 433 error = cp->provider->error; 434 LIST_FOREACH(pp, &gp->provider, provider) 435 g_orphan_provider(pp, error); 436 return; 437} 438