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$ 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 64 static g_orphan_t g_slice_orphan; 65 static g_access_t g_slice_access; 66 static g_start_t g_slice_start; 67 68 static struct g_slicer * 69 g_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 81 static int 82 g_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 124 void 125 g_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 156 static void 157 g_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 if (!strcmp("GEOM::frontstuff", bp->bio_attribute)) { 224 t = gsp->cfrontstuff; 225 if (gsp->frontstuff > t) 226 t = gsp->frontstuff; 227 t -= gsl->offset; 228 if (t < 0) 229 t = 0; 230 if (t > gsl->length) 231 t = gsl->length; 232 g_handleattr_off_t(bp, "GEOM::frontstuff", t); 233 return; 234 } 235 #ifdef _KERNEL 236 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { 237 struct g_kerneldump *gkd; 238 239 gkd = (struct g_kerneldump *)bp->bio_data; 240 gkd->offset += gsp->slices[idx].offset; 241 if (gkd->length > gsp->slices[idx].length) 242 gkd->length = gsp->slices[idx].length; 243 /* now, pass it on downwards... */ 244 } 245 #endif 246 bp2 = g_clone_bio(bp); 247 if (bp2 == NULL) { 248 g_io_deliver(bp, ENOMEM); 249 return; 250 } 251 bp2->bio_done = g_std_done; 252 g_io_request(bp2, cp); 253 break; 254 default: 255 g_io_deliver(bp, EOPNOTSUPP); 256 return; 257 } 258 } 259 260 void 261 g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 262 { 263 struct g_slicer *gsp; 264 265 gsp = gp->softc; 266 if (indent == NULL) { 267 sbuf_printf(sb, " i %u", pp->index); 268 sbuf_printf(sb, " o %ju", 269 (uintmax_t)gsp->slices[pp->index].offset); 270 return; 271 } 272 if (gp != NULL && (pp == NULL && cp == NULL)) { 273 sbuf_printf(sb, "%s<frontstuff>%ju</frontstuff>\n", 274 indent, (intmax_t)gsp->frontstuff); 275 } 276 if (pp != NULL) { 277 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index); 278 sbuf_printf(sb, "%s<length>%ju</length>\n", 279 indent, (uintmax_t)gsp->slices[pp->index].length); 280 sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent, 281 (uintmax_t)gsp->slices[pp->index].length / 512); 282 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent, 283 (uintmax_t)gsp->slices[pp->index].offset); 284 sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent, 285 (uintmax_t)gsp->slices[pp->index].offset / 512); 286 } 287 } 288 289 int 290 g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...) 291 { 292 struct g_provider *pp, *pp2; 293 struct g_slicer *gsp; 294 struct g_slice *gsl; 295 va_list ap; 296 struct sbuf *sb; 297 int error, acc; 298 299 g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)", 300 gp->name, idx, how); 301 g_topology_assert(); 302 gsp = gp->softc; 303 error = 0; 304 if (idx >= gsp->nslice) 305 return(EINVAL); 306 gsl = &gsp->slices[idx]; 307 pp = gsl->provider; 308 if (pp != NULL) 309 acc = pp->acr + pp->acw + pp->ace; 310 else 311 acc = 0; 312 if (acc != 0 && how != G_SLICE_CONFIG_FORCE) { 313 if (length < gsl->length) 314 return(EBUSY); 315 if (offset != gsl->offset) 316 return(EBUSY); 317 } 318 /* XXX: check offset + length <= MEDIASIZE */ 319 if (how == G_SLICE_CONFIG_CHECK) 320 return (0); 321 gsl->length = length; 322 gsl->offset = offset; 323 gsl->sectorsize = sectorsize; 324 if (length == 0) { 325 if (pp == NULL) 326 return (0); 327 if (bootverbose) 328 printf("GEOM: Deconfigure %s\n", pp->name); 329 g_orphan_provider(pp, ENXIO); 330 gsl->provider = NULL; 331 gsp->nprovider--; 332 return (0); 333 } 334 if (pp != NULL) { 335 if (bootverbose) 336 printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n", 337 pp->name, (intmax_t)offset, (intmax_t)length, 338 (intmax_t)(offset + length - 1)); 339 pp->mediasize = gsl->length; 340 return (0); 341 } 342 va_start(ap, fmt); 343 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 344 sbuf_vprintf(sb, fmt, ap); 345 sbuf_finish(sb); 346 pp = g_new_providerf(gp, sbuf_data(sb)); 347 pp2 = LIST_FIRST(&gp->consumer)->provider; 348 pp->flags = pp2->flags & G_PF_CANDELETE; 349 if (pp2->stripesize > 0) { 350 pp->stripesize = pp2->stripesize; 351 pp->stripeoffset = (pp2->stripeoffset + offset) % pp->stripesize; 352 } 353 if (bootverbose) 354 printf("GEOM: Configure %s, start %jd length %jd end %jd\n", 355 pp->name, (intmax_t)offset, (intmax_t)length, 356 (intmax_t)(offset + length - 1)); 357 pp->index = idx; 358 pp->mediasize = gsl->length; 359 pp->sectorsize = gsl->sectorsize; 360 gsl->provider = pp; 361 gsp->nprovider++; 362 g_error_provider(pp, 0); 363 sbuf_delete(sb); 364 return(0); 365 } 366 367 int 368 g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length) 369 { 370 struct g_slicer *gsp; 371 struct g_slice *gsl, *gsl2; 372 373 g_trace(G_T_TOPOLOGY, "g_slice_conf_hot()"); 374 g_topology_assert(); 375 gsp = gp->softc; 376 gsl = gsp->hot; 377 if(idx >= gsp->nhot) { 378 gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO); 379 if (gsp->hot != NULL) 380 bcopy(gsp->hot, gsl2, gsp->nhot * sizeof *gsl2); 381 gsp->hot = gsl2; 382 if (gsp->hot != NULL) 383 g_free(gsl); 384 gsl = gsl2; 385 gsp->nhot = idx + 1; 386 } 387 if (bootverbose) 388 printf("GEOM: Add %s hot[%d] start %jd length %jd end %jd\n", 389 gp->name, idx, (intmax_t)offset, (intmax_t)length, 390 (intmax_t)(offset + length - 1)); 391 gsl[idx].offset = offset; 392 gsl[idx].length = length; 393 return (0); 394 } 395 396 struct g_geom * 397 g_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) 398 { 399 struct g_geom *gp; 400 struct g_slicer *gsp; 401 struct g_consumer *cp; 402 void **vp; 403 int error, i; 404 405 g_topology_assert(); 406 vp = (void **)extrap; 407 gp = g_new_geomf(mp, "%s", pp->name); 408 gsp = g_slice_init(slices, extra); 409 gsp->start = start; 410 gp->access = g_slice_access; 411 gp->orphan = g_slice_orphan; 412 gp->softc = gsp; 413 gp->start = g_slice_start; 414 gp->spoiled = g_std_spoiled; 415 gp->dumpconf = g_slice_dumpconf; 416 cp = g_new_consumer(gp); 417 error = g_attach(cp, pp); 418 if (error == 0) 419 error = g_access_rel(cp, 1, 0, 0); 420 if (error) { 421 if (cp->provider != NULL) 422 g_detach(cp); 423 g_destroy_consumer(cp); 424 g_free(gsp->slices); 425 g_free(gp->softc); 426 g_destroy_geom(gp); 427 return (NULL); 428 } 429 /* Find out if there are any magic bytes on the consumer */ 430 i = sizeof gsp->cfrontstuff; 431 error = g_io_getattr("GEOM::frontstuff", cp, &i, &gsp->cfrontstuff); 432 if (error) 433 gsp->cfrontstuff = 0; 434 *vp = gsp->softc; 435 *cpp = cp; 436 return (gp); 437 } 438 439 static void 440 g_slice_orphan(struct g_consumer *cp) 441 { 442 struct g_geom *gp; 443 struct g_provider *pp; 444 int error; 445 446 g_trace(G_T_TOPOLOGY, "g_slice_orphan(%p/%s)", cp, cp->provider->name); 447 g_topology_assert(); 448 KASSERT(cp->provider->error != 0, 449 ("g_slice_orphan with error == 0")); 450 451 gp = cp->geom; 452 /* XXX: Not good enough we leak the softc and its suballocations */ 453 gp->flags |= G_GEOM_WITHER; 454 error = cp->provider->error; 455 LIST_FOREACH(pp, &gp->provider, provider) 456 g_orphan_provider(pp, error); 457 return; 458 } 459