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