geom_slice.c revision 113712
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 113712 2003-04-19 10:00:22Z phk $ 36 */ 37 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/kernel.h> 42#include <sys/malloc.h> 43#include <sys/bio.h> 44#include <sys/sysctl.h> 45#include <sys/proc.h> 46#include <sys/kthread.h> 47#include <sys/lock.h> 48#include <sys/mutex.h> 49#include <sys/errno.h> 50#include <sys/sbuf.h> 51#include <geom/geom.h> 52#include <geom/geom_slice.h> 53#include <machine/stdarg.h> 54 55static g_orphan_t g_slice_orphan; 56static g_access_t g_slice_access; 57static g_start_t g_slice_start; 58 59static struct g_slicer * 60g_slice_init(unsigned nslice, unsigned scsize) 61{ 62 struct g_slicer *gsp; 63 64 gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO); 65 gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO); 66 gsp->slices = g_malloc(nslice * sizeof(struct g_slice), 67 M_WAITOK | M_ZERO); 68 gsp->nslice = nslice; 69 return (gsp); 70} 71 72static int 73g_slice_access(struct g_provider *pp, int dr, int dw, int de) 74{ 75 int error; 76 u_int u; 77 struct g_geom *gp; 78 struct g_consumer *cp; 79 struct g_provider *pp2; 80 struct g_slicer *gsp; 81 struct g_slice *gsl, *gsl2; 82 83 gp = pp->geom; 84 cp = LIST_FIRST(&gp->consumer); 85 KASSERT (cp != NULL, ("g_slice_access but no consumer")); 86 gsp = gp->softc; 87 gsl = &gsp->slices[pp->index]; 88 for (u = 0; u < gsp->nslice; u++) { 89 gsl2 = &gsp->slices[u]; 90 if (gsl2->length == 0) 91 continue; 92 if (u == pp->index) 93 continue; 94 if (gsl->offset + gsl->length <= gsl2->offset) 95 continue; 96 if (gsl2->offset + gsl2->length <= gsl->offset) 97 continue; 98 /* overlap */ 99 pp2 = gsl2->provider; 100 if ((pp->acw + dw) > 0 && pp2->ace > 0) 101 return (EPERM); 102 if ((pp->ace + de) > 0 && pp2->acw > 0) 103 return (EPERM); 104 } 105 /* On first open, grab an extra "exclusive" bit */ 106 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 107 de++; 108 /* ... and let go of it on last close */ 109 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 110 de--; 111 error = g_access_rel(cp, dr, dw, de); 112 return (error); 113} 114 115void 116g_slice_finish_hot(struct bio *bp) 117{ 118 struct bio *bp2; 119 struct g_geom *gp; 120 struct g_consumer *cp; 121 struct g_slicer *gsp; 122 struct g_slice *gsl; 123 int idx; 124 125 KASSERT(bp->bio_to != NULL, ("NULL bio_to in g_slice_finish_hot(%p)", bp)); 126 KASSERT(bp->bio_from != NULL, ("NULL bio_from in g_slice_finish_hot(%p)", bp)); 127 gp = bp->bio_to->geom; 128 gsp = gp->softc; 129 cp = LIST_FIRST(&gp->consumer); 130 KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp)); 131 idx = bp->bio_to->index; 132 gsl = &gsp->slices[idx]; 133 134 bp2 = g_clone_bio(bp); 135 if (bp2 == NULL) { 136 g_io_deliver(bp, ENOMEM); 137 return; 138 } 139 if (bp2->bio_offset + bp2->bio_length > gsl->length) 140 bp2->bio_length = gsl->length - bp2->bio_offset; 141 bp2->bio_done = g_std_done; 142 bp2->bio_offset += gsl->offset; 143 g_io_request(bp2, cp); 144 return; 145} 146 147static void 148g_slice_start(struct bio *bp) 149{ 150 struct bio *bp2; 151 struct g_provider *pp; 152 struct g_geom *gp; 153 struct g_consumer *cp; 154 struct g_slicer *gsp; 155 struct g_slice *gsl; 156 struct g_slice_hot *gmp; 157 int idx, error; 158 u_int m_index; 159 off_t t; 160 161 pp = bp->bio_to; 162 gp = pp->geom; 163 gsp = gp->softc; 164 cp = LIST_FIRST(&gp->consumer); 165 idx = pp->index; 166 gsl = &gsp->slices[idx]; 167 switch(bp->bio_cmd) { 168 case BIO_READ: 169 case BIO_WRITE: 170 case BIO_DELETE: 171 if (bp->bio_offset > gsl->length) { 172 g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */ 173 return; 174 } 175 /* 176 * Check if we collide with any hot spaces, and call the 177 * method once if so. 178 */ 179 t = bp->bio_offset + gsl->offset; 180 /* .ctl devices may take us negative */ 181 if (t < 0 || (t + bp->bio_length) < 0) { 182 g_io_deliver(bp, EINVAL); 183 return; 184 } 185 for (m_index = 0; m_index < gsp->nhotspot; m_index++) { 186 gmp = &gsp->hotspot[m_index]; 187 if (t >= gmp->offset + gmp->length) 188 continue; 189 if (t + bp->bio_length <= gmp->offset) 190 continue; 191 error = gsp->start(bp); 192 if (error == EJUSTRETURN) 193 return; 194 else if (error) { 195 g_io_deliver(bp, error); 196 return; 197 } 198 break; 199 } 200 bp2 = g_clone_bio(bp); 201 if (bp2 == NULL) { 202 g_io_deliver(bp, ENOMEM); 203 return; 204 } 205 if (bp2->bio_offset + bp2->bio_length > gsl->length) 206 bp2->bio_length = gsl->length - bp2->bio_offset; 207 bp2->bio_done = g_std_done; 208 bp2->bio_offset += gsl->offset; 209 g_io_request(bp2, cp); 210 return; 211 case BIO_GETATTR: 212 /* Give the real method a chance to override */ 213 if (gsp->start(bp)) 214 return; 215 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { 216 struct g_kerneldump *gkd; 217 218 gkd = (struct g_kerneldump *)bp->bio_data; 219 gkd->offset += gsp->slices[idx].offset; 220 if (gkd->length > gsp->slices[idx].length) 221 gkd->length = gsp->slices[idx].length; 222 /* now, pass it on downwards... */ 223 } 224 bp2 = g_clone_bio(bp); 225 if (bp2 == NULL) { 226 g_io_deliver(bp, ENOMEM); 227 return; 228 } 229 bp2->bio_done = g_std_done; 230 g_io_request(bp2, cp); 231 break; 232 default: 233 g_io_deliver(bp, EOPNOTSUPP); 234 return; 235 } 236} 237 238void 239g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 240{ 241 struct g_slicer *gsp; 242 243 gsp = gp->softc; 244 if (indent == NULL) { 245 sbuf_printf(sb, " i %u", pp->index); 246 sbuf_printf(sb, " o %ju", 247 (uintmax_t)gsp->slices[pp->index].offset); 248 return; 249 } 250 if (pp != NULL) { 251 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index); 252 sbuf_printf(sb, "%s<length>%ju</length>\n", 253 indent, (uintmax_t)gsp->slices[pp->index].length); 254 sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent, 255 (uintmax_t)gsp->slices[pp->index].length / 512); 256 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent, 257 (uintmax_t)gsp->slices[pp->index].offset); 258 sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent, 259 (uintmax_t)gsp->slices[pp->index].offset / 512); 260 } 261} 262 263int 264g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...) 265{ 266 struct g_provider *pp, *pp2; 267 struct g_slicer *gsp; 268 struct g_slice *gsl; 269 va_list ap; 270 struct sbuf *sb; 271 int error, acc; 272 273 g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)", 274 gp->name, idx, how); 275 g_topology_assert(); 276 gsp = gp->softc; 277 error = 0; 278 if (idx >= gsp->nslice) 279 return(EINVAL); 280 gsl = &gsp->slices[idx]; 281 pp = gsl->provider; 282 if (pp != NULL) 283 acc = pp->acr + pp->acw + pp->ace; 284 else 285 acc = 0; 286 if (acc != 0 && how != G_SLICE_CONFIG_FORCE) { 287 if (length < gsl->length) 288 return(EBUSY); 289 if (offset != gsl->offset) 290 return(EBUSY); 291 } 292 /* XXX: check offset + length <= MEDIASIZE */ 293 if (how == G_SLICE_CONFIG_CHECK) 294 return (0); 295 gsl->length = length; 296 gsl->offset = offset; 297 gsl->sectorsize = sectorsize; 298 if (length == 0) { 299 if (pp == NULL) 300 return (0); 301 if (bootverbose) 302 printf("GEOM: Deconfigure %s\n", pp->name); 303 g_orphan_provider(pp, ENXIO); 304 gsl->provider = NULL; 305 gsp->nprovider--; 306 return (0); 307 } 308 if (pp != NULL) { 309 if (bootverbose) 310 printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n", 311 pp->name, (intmax_t)offset, (intmax_t)length, 312 (intmax_t)(offset + length - 1)); 313 pp->mediasize = gsl->length; 314 return (0); 315 } 316 va_start(ap, fmt); 317 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 318 sbuf_vprintf(sb, fmt, ap); 319 sbuf_finish(sb); 320 pp = g_new_providerf(gp, sbuf_data(sb)); 321 pp2 = LIST_FIRST(&gp->consumer)->provider; 322 pp->flags = pp2->flags & G_PF_CANDELETE; 323 if (pp2->stripesize > 0) { 324 pp->stripesize = pp2->stripesize; 325 pp->stripeoffset = (pp2->stripeoffset + offset) % pp->stripesize; 326 } 327 if (bootverbose) 328 printf("GEOM: Configure %s, start %jd length %jd end %jd\n", 329 pp->name, (intmax_t)offset, (intmax_t)length, 330 (intmax_t)(offset + length - 1)); 331 pp->index = idx; 332 pp->mediasize = gsl->length; 333 pp->sectorsize = gsl->sectorsize; 334 gsl->provider = pp; 335 gsp->nprovider++; 336 g_error_provider(pp, 0); 337 sbuf_delete(sb); 338 return(0); 339} 340 341int 342g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length) 343{ 344 struct g_slicer *gsp; 345 struct g_slice_hot *gsl, *gsl2; 346 347 g_trace(G_T_TOPOLOGY, "g_slice_conf_hot()"); 348 g_topology_assert(); 349 gsp = gp->softc; 350 gsl = gsp->hotspot; 351 if(idx >= gsp->nhotspot) { 352 gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO); 353 if (gsp->hotspot != NULL) 354 bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2); 355 gsp->hotspot = gsl2; 356 if (gsp->hotspot != NULL) 357 g_free(gsl); 358 gsl = gsl2; 359 gsp->nhotspot = idx + 1; 360 } 361 if (bootverbose) 362 printf("GEOM: Add %s hot[%d] start %jd length %jd end %jd\n", 363 gp->name, idx, (intmax_t)offset, (intmax_t)length, 364 (intmax_t)(offset + length - 1)); 365 gsl[idx].offset = offset; 366 gsl[idx].length = length; 367 return (0); 368} 369 370struct g_geom * 371g_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) 372{ 373 struct g_geom *gp; 374 struct g_slicer *gsp; 375 struct g_consumer *cp; 376 void **vp; 377 int error; 378 379 g_topology_assert(); 380 vp = (void **)extrap; 381 gp = g_new_geomf(mp, "%s", pp->name); 382 gsp = g_slice_init(slices, extra); 383 gsp->start = start; 384 gp->access = g_slice_access; 385 gp->orphan = g_slice_orphan; 386 gp->softc = gsp; 387 gp->start = g_slice_start; 388 gp->spoiled = g_std_spoiled; 389 gp->dumpconf = g_slice_dumpconf; 390 cp = g_new_consumer(gp); 391 error = g_attach(cp, pp); 392 if (error == 0) 393 error = g_access_rel(cp, 1, 0, 0); 394 if (error) { 395 if (cp->provider != NULL) 396 g_detach(cp); 397 g_destroy_consumer(cp); 398 g_free(gsp->slices); 399 g_free(gp->softc); 400 g_destroy_geom(gp); 401 return (NULL); 402 } 403 *vp = gsp->softc; 404 *cpp = cp; 405 return (gp); 406} 407 408static void 409g_slice_orphan(struct g_consumer *cp) 410{ 411 struct g_geom *gp; 412 struct g_provider *pp; 413 int error; 414 415 g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name); 416 g_topology_assert(); 417 KASSERT(cp->provider->error != 0, 418 ("g_slice_orphan with error == 0")); 419 420 gp = cp->geom; 421 /* XXX: Not good enough we leak the softc and its suballocations */ 422 gp->flags |= G_GEOM_WITHER; 423 error = cp->provider->error; 424 LIST_FOREACH(pp, &gp->provider, provider) 425 g_orphan_provider(pp, error); 426 return; 427} 428