1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2002 Poul-Henning Kamp 5 * Copyright (c) 2002 Networks Associates Technology, Inc. 6 * All rights reserved. 7 * 8 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 9 * and NAI Labs, the Security Research Division of Network Associates, Inc. 10 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 11 * DARPA CHATS research program. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. The names of the authors may not be used to endorse or promote 22 * products derived from this software without specific prior written 23 * permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/malloc.h> 42 #include <sys/bio.h> 43 #include <sys/sysctl.h> 44 #include <sys/proc.h> 45 #include <sys/lock.h> 46 #include <sys/mutex.h> 47 #include <sys/errno.h> 48 #include <sys/sbuf.h> 49 #include <geom/geom.h> 50 #include <geom/geom_slice.h> 51 #include <machine/stdarg.h> 52 53 static g_access_t g_slice_access; 54 static g_start_t g_slice_start; 55 56 static struct g_slicer * 57 g_slice_alloc(unsigned nslice, unsigned scsize) 58 { 59 struct g_slicer *gsp; 60 61 gsp = g_malloc(sizeof *gsp, M_WAITOK | M_ZERO); 62 if (scsize > 0) 63 gsp->softc = g_malloc(scsize, M_WAITOK | M_ZERO); 64 else 65 gsp->softc = NULL; 66 gsp->slices = g_malloc(nslice * sizeof(struct g_slice), 67 M_WAITOK | M_ZERO); 68 gsp->nslice = nslice; 69 return (gsp); 70 } 71 72 static void 73 g_slice_free(struct g_geom *gp) 74 { 75 struct g_slicer *gsp; 76 77 gsp = gp->softc; 78 gp->softc = NULL; 79 80 /* 81 * We can get multiple spoiled events before wither-washer 82 * detaches our consumer, so this can get called multiple 83 * times. 84 */ 85 if (gsp == NULL) 86 return; 87 g_free(gsp->slices); 88 g_free(gsp->hotspot); 89 g_free(gsp->softc); 90 g_free(gsp); 91 } 92 93 static int 94 g_slice_access(struct g_provider *pp, int dr, int dw, int de) 95 { 96 int error; 97 u_int u; 98 struct g_geom *gp; 99 struct g_consumer *cp; 100 struct g_provider *pp2; 101 struct g_slicer *gsp; 102 struct g_slice *gsl, *gsl2; 103 104 gp = pp->geom; 105 cp = LIST_FIRST(&gp->consumer); 106 KASSERT (cp != NULL, ("g_slice_access but no consumer")); 107 gsp = gp->softc; 108 if (dr > 0 || dw > 0 || de > 0) { 109 gsl = &gsp->slices[pp->index]; 110 for (u = 0; u < gsp->nslice; u++) { 111 gsl2 = &gsp->slices[u]; 112 if (gsl2->length == 0) 113 continue; 114 if (u == pp->index) 115 continue; 116 if (gsl->offset + gsl->length <= gsl2->offset) 117 continue; 118 if (gsl2->offset + gsl2->length <= gsl->offset) 119 continue; 120 /* overlap */ 121 pp2 = gsl2->provider; 122 if ((pp->acw + dw) > 0 && pp2->ace > 0) 123 return (EPERM); 124 if ((pp->ace + de) > 0 && pp2->acw > 0) 125 return (EPERM); 126 } 127 } 128 /* On first open, grab an extra "exclusive" bit */ 129 if (cp->acr == 0 && cp->acw == 0 && cp->ace == 0) 130 de++; 131 /* ... and let go of it on last close */ 132 if ((cp->acr + dr) == 0 && (cp->acw + dw) == 0 && (cp->ace + de) == 1) 133 de--; 134 error = g_access(cp, dr, dw, de); 135 136 /* 137 * Free the softc if all providers have been closed and this geom 138 * is being removed. 139 */ 140 if (error == 0 && (gp->flags & G_GEOM_WITHER) != 0 && 141 (cp->acr + cp->acw + cp->ace) == 0) 142 g_slice_free(gp); 143 144 return (error); 145 } 146 147 /* 148 * XXX: It should be possible to specify here if we should finish all of the 149 * XXX: bio, or only the non-hot bits. This would get messy if there were 150 * XXX: two hot spots in the same bio, so for now we simply finish off the 151 * XXX: entire bio. Modifying hot data on the way to disk is frowned on 152 * XXX: so making that considerably harder is not a bad idea anyway. 153 */ 154 void 155 g_slice_finish_hot(struct bio *bp) 156 { 157 struct bio *bp2; 158 struct g_geom *gp; 159 struct g_consumer *cp; 160 struct g_slicer *gsp; 161 struct g_slice *gsl; 162 int idx; 163 164 KASSERT(bp->bio_to != NULL, 165 ("NULL bio_to in g_slice_finish_hot(%p)", bp)); 166 KASSERT(bp->bio_from != NULL, 167 ("NULL bio_from in g_slice_finish_hot(%p)", bp)); 168 gp = bp->bio_to->geom; 169 gsp = gp->softc; 170 cp = LIST_FIRST(&gp->consumer); 171 KASSERT(cp != NULL, ("NULL consumer in g_slice_finish_hot(%p)", bp)); 172 idx = bp->bio_to->index; 173 gsl = &gsp->slices[idx]; 174 175 bp2 = g_clone_bio(bp); 176 if (bp2 == NULL) { 177 g_io_deliver(bp, ENOMEM); 178 return; 179 } 180 if (bp2->bio_offset + bp2->bio_length > gsl->length) 181 bp2->bio_length = gsl->length - bp2->bio_offset; 182 bp2->bio_done = g_std_done; 183 bp2->bio_offset += gsl->offset; 184 g_io_request(bp2, cp); 185 return; 186 } 187 188 static void 189 g_slice_done(struct bio *bp) 190 { 191 192 KASSERT(bp->bio_cmd == BIO_GETATTR && 193 strcmp(bp->bio_attribute, "GEOM::ident") == 0, 194 ("bio_cmd=0x%x bio_attribute=%s", bp->bio_cmd, bp->bio_attribute)); 195 196 if (bp->bio_error == 0 && bp->bio_data[0] != '\0') { 197 char idx[8]; 198 199 /* Add index to the ident received. */ 200 snprintf(idx, sizeof(idx), "s%d", 201 bp->bio_parent->bio_to->index); 202 if (strlcat(bp->bio_data, idx, bp->bio_length) >= 203 bp->bio_length) { 204 bp->bio_error = EFAULT; 205 } 206 } 207 g_std_done(bp); 208 } 209 210 static void 211 g_slice_start(struct bio *bp) 212 { 213 struct bio *bp2; 214 struct g_provider *pp; 215 struct g_geom *gp; 216 struct g_consumer *cp; 217 struct g_slicer *gsp; 218 struct g_slice *gsl; 219 struct g_slice_hot *ghp; 220 int idx, error; 221 u_int m_index; 222 off_t t; 223 224 pp = bp->bio_to; 225 gp = pp->geom; 226 gsp = gp->softc; 227 cp = LIST_FIRST(&gp->consumer); 228 idx = pp->index; 229 gsl = &gsp->slices[idx]; 230 switch(bp->bio_cmd) { 231 case BIO_READ: 232 case BIO_WRITE: 233 case BIO_DELETE: 234 if (bp->bio_offset > gsl->length) { 235 g_io_deliver(bp, EINVAL); /* XXX: EWHAT ? */ 236 return; 237 } 238 /* 239 * Check if we collide with any hot spaces, and call the 240 * method once if so. 241 */ 242 t = bp->bio_offset + gsl->offset; 243 for (m_index = 0; m_index < gsp->nhotspot; m_index++) { 244 ghp = &gsp->hotspot[m_index]; 245 if (t >= ghp->offset + ghp->length) 246 continue; 247 if (t + bp->bio_length <= ghp->offset) 248 continue; 249 switch(bp->bio_cmd) { 250 case BIO_READ: idx = ghp->ract; break; 251 case BIO_WRITE: idx = ghp->wact; break; 252 case BIO_DELETE: idx = ghp->dact; break; 253 } 254 switch(idx) { 255 case G_SLICE_HOT_ALLOW: 256 /* Fall out and continue normal processing */ 257 continue; 258 case G_SLICE_HOT_DENY: 259 g_io_deliver(bp, EROFS); 260 return; 261 case G_SLICE_HOT_START: 262 error = gsp->start(bp); 263 if (error && error != EJUSTRETURN) 264 g_io_deliver(bp, error); 265 return; 266 case G_SLICE_HOT_CALL: 267 error = g_post_event(gsp->hot, bp, M_NOWAIT, 268 gp, NULL); 269 if (error) 270 g_io_deliver(bp, error); 271 return; 272 } 273 break; 274 } 275 bp2 = g_clone_bio(bp); 276 if (bp2 == NULL) { 277 g_io_deliver(bp, ENOMEM); 278 return; 279 } 280 if (bp2->bio_offset + bp2->bio_length > gsl->length) 281 bp2->bio_length = gsl->length - bp2->bio_offset; 282 bp2->bio_done = g_std_done; 283 bp2->bio_offset += gsl->offset; 284 g_io_request(bp2, cp); 285 return; 286 case BIO_GETATTR: 287 /* Give the real method a chance to override */ 288 if (gsp->start != NULL && gsp->start(bp)) 289 return; 290 if (!strcmp("GEOM::ident", bp->bio_attribute)) { 291 bp2 = g_clone_bio(bp); 292 if (bp2 == NULL) { 293 g_io_deliver(bp, ENOMEM); 294 return; 295 } 296 bp2->bio_done = g_slice_done; 297 g_io_request(bp2, cp); 298 return; 299 } 300 if (!strcmp("GEOM::kerneldump", bp->bio_attribute)) { 301 struct g_kerneldump *gkd; 302 303 gkd = (struct g_kerneldump *)bp->bio_data; 304 gkd->offset += gsp->slices[idx].offset; 305 if (gkd->length > gsp->slices[idx].length) 306 gkd->length = gsp->slices[idx].length; 307 /* now, pass it on downwards... */ 308 } 309 /* FALLTHROUGH */ 310 case BIO_SPEEDUP: 311 case BIO_FLUSH: 312 bp2 = g_clone_bio(bp); 313 if (bp2 == NULL) { 314 g_io_deliver(bp, ENOMEM); 315 return; 316 } 317 bp2->bio_done = g_std_done; 318 g_io_request(bp2, cp); 319 break; 320 default: 321 g_io_deliver(bp, EOPNOTSUPP); 322 return; 323 } 324 } 325 326 void 327 g_slice_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 328 { 329 struct g_slicer *gsp; 330 331 gsp = gp->softc; 332 if (indent == NULL) { 333 sbuf_printf(sb, " i %u", pp->index); 334 sbuf_printf(sb, " o %ju", 335 (uintmax_t)gsp->slices[pp->index].offset); 336 return; 337 } 338 if (pp != NULL) { 339 sbuf_printf(sb, "%s<index>%u</index>\n", indent, pp->index); 340 sbuf_printf(sb, "%s<length>%ju</length>\n", 341 indent, (uintmax_t)gsp->slices[pp->index].length); 342 sbuf_printf(sb, "%s<seclength>%ju</seclength>\n", indent, 343 (uintmax_t)gsp->slices[pp->index].length / 512); 344 sbuf_printf(sb, "%s<offset>%ju</offset>\n", indent, 345 (uintmax_t)gsp->slices[pp->index].offset); 346 sbuf_printf(sb, "%s<secoffset>%ju</secoffset>\n", indent, 347 (uintmax_t)gsp->slices[pp->index].offset / 512); 348 } 349 } 350 351 int 352 g_slice_config(struct g_geom *gp, u_int idx, int how, off_t offset, off_t length, u_int sectorsize, const char *fmt, ...) 353 { 354 struct g_provider *pp, *pp2; 355 struct g_slicer *gsp; 356 struct g_slice *gsl; 357 va_list ap; 358 struct sbuf *sb; 359 int acc; 360 361 g_trace(G_T_TOPOLOGY, "g_slice_config(%s, %d, %d)", 362 gp->name, idx, how); 363 g_topology_assert(); 364 gsp = gp->softc; 365 if (idx >= gsp->nslice) 366 return(EINVAL); 367 gsl = &gsp->slices[idx]; 368 pp = gsl->provider; 369 if (pp != NULL) 370 acc = pp->acr + pp->acw + pp->ace; 371 else 372 acc = 0; 373 if (acc != 0 && how != G_SLICE_CONFIG_FORCE) { 374 if (length < gsl->length) 375 return(EBUSY); 376 if (offset != gsl->offset) 377 return(EBUSY); 378 } 379 /* XXX: check offset + length <= MEDIASIZE */ 380 if (how == G_SLICE_CONFIG_CHECK) 381 return (0); 382 gsl->length = length; 383 gsl->offset = offset; 384 gsl->sectorsize = sectorsize; 385 if (length == 0) { 386 if (pp == NULL) 387 return (0); 388 if (bootverbose) 389 printf("GEOM: Deconfigure %s\n", pp->name); 390 g_wither_provider(pp, ENXIO); 391 gsl->provider = NULL; 392 gsp->nprovider--; 393 return (0); 394 } 395 if (pp != NULL) { 396 if (bootverbose) 397 printf("GEOM: Reconfigure %s, start %jd length %jd end %jd\n", 398 pp->name, (intmax_t)offset, (intmax_t)length, 399 (intmax_t)(offset + length - 1)); 400 g_resize_provider(pp, gsl->length); 401 return (0); 402 } 403 sb = sbuf_new_auto(); 404 va_start(ap, fmt); 405 sbuf_vprintf(sb, fmt, ap); 406 va_end(ap); 407 sbuf_finish(sb); 408 pp = g_new_providerf(gp, "%s", sbuf_data(sb)); 409 pp2 = LIST_FIRST(&gp->consumer)->provider; 410 pp->stripesize = pp2->stripesize; 411 pp->stripeoffset = pp2->stripeoffset + offset; 412 if (pp->stripesize > 0) 413 pp->stripeoffset %= pp->stripesize; 414 if (gsp->nhotspot == 0) { 415 pp->flags |= pp2->flags & G_PF_ACCEPT_UNMAPPED; 416 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 417 } 418 if (0 && bootverbose) 419 printf("GEOM: Configure %s, start %jd length %jd end %jd\n", 420 pp->name, (intmax_t)offset, (intmax_t)length, 421 (intmax_t)(offset + length - 1)); 422 pp->index = idx; 423 pp->mediasize = gsl->length; 424 pp->sectorsize = gsl->sectorsize; 425 gsl->provider = pp; 426 gsp->nprovider++; 427 g_error_provider(pp, 0); 428 sbuf_delete(sb); 429 return(0); 430 } 431 432 /* 433 * Configure "hotspots". A hotspot is a piece of the parent device which 434 * this particular slicer cares about for some reason. Typically because 435 * it contains meta-data used to configure the slicer. 436 * A hotspot is identified by its index number. The offset and length are 437 * relative to the parent device, and the three "?act" fields specify 438 * what action to take on BIO_READ, BIO_DELETE and BIO_WRITE. 439 * 440 * XXX: There may be a race relative to g_slice_start() here, if an existing 441 * XXX: hotspot is changed wile I/O is happening. Should this become a problem 442 * XXX: we can protect the hotspot stuff with a mutex. 443 */ 444 445 int 446 g_slice_conf_hot(struct g_geom *gp, u_int idx, off_t offset, off_t length, int ract, int dact, int wact) 447 { 448 struct g_slicer *gsp; 449 struct g_slice_hot *gsl, *gsl2; 450 struct g_consumer *cp; 451 struct g_provider *pp; 452 453 g_trace(G_T_TOPOLOGY, "g_slice_conf_hot(%s, idx: %d, off: %jd, len: %jd)", 454 gp->name, idx, (intmax_t)offset, (intmax_t)length); 455 g_topology_assert(); 456 gsp = gp->softc; 457 /* Deny unmapped I/O and direct dispatch if hotspots are used. */ 458 if (gsp->nhotspot == 0) { 459 LIST_FOREACH(pp, &gp->provider, provider) 460 pp->flags &= ~(G_PF_ACCEPT_UNMAPPED | 461 G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE); 462 LIST_FOREACH(cp, &gp->consumer, consumer) 463 cp->flags &= ~(G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE); 464 } 465 gsl = gsp->hotspot; 466 if(idx >= gsp->nhotspot) { 467 gsl2 = g_malloc((idx + 1) * sizeof *gsl2, M_WAITOK | M_ZERO); 468 if (gsp->hotspot != NULL) 469 bcopy(gsp->hotspot, gsl2, gsp->nhotspot * sizeof *gsl2); 470 gsp->hotspot = gsl2; 471 if (gsp->hotspot != NULL) 472 g_free(gsl); 473 gsl = gsl2; 474 gsp->nhotspot = idx + 1; 475 } 476 gsl[idx].offset = offset; 477 gsl[idx].length = length; 478 KASSERT(!((ract | dact | wact) & G_SLICE_HOT_START) 479 || gsp->start != NULL, ("G_SLICE_HOT_START but no slice->start")); 480 /* XXX: check that we _have_ a start function if HOT_START specified */ 481 gsl[idx].ract = ract; 482 gsl[idx].dact = dact; 483 gsl[idx].wact = wact; 484 return (0); 485 } 486 487 void 488 g_slice_orphan(struct g_consumer *cp) 489 { 490 struct g_geom *gp; 491 492 g_topology_assert(); 493 gp = cp->geom; 494 g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, gp->name); 495 g_wither_geom(gp, ENXIO); 496 497 /* 498 * We can safely free the softc now if there are no accesses, 499 * otherwise g_slice_access() will do that after the last close. 500 */ 501 if ((cp->acr + cp->acw + cp->ace) == 0) 502 g_slice_free(gp); 503 } 504 505 void 506 g_slice_spoiled(struct g_consumer *cp) 507 { 508 509 g_trace(G_T_TOPOLOGY, "%s(%p/%s)", __func__, cp, cp->geom->name); 510 cp->flags |= G_CF_ORPHAN; 511 g_slice_orphan(cp); 512 } 513 514 int 515 g_slice_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp) 516 { 517 518 g_slice_spoiled(LIST_FIRST(&gp->consumer)); 519 return (0); 520 } 521 522 struct g_geom * 523 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) 524 { 525 struct g_geom *gp; 526 struct g_slicer *gsp; 527 struct g_consumer *cp; 528 void **vp; 529 int error; 530 531 g_topology_assert(); 532 vp = (void **)extrap; 533 gp = g_new_geomf(mp, "%s", pp->name); 534 gsp = g_slice_alloc(slices, extra); 535 gsp->start = start; 536 gp->softc = gsp; 537 gp->start = g_slice_start; 538 gp->access = g_slice_access; 539 gp->orphan = g_slice_orphan; 540 gp->spoiled = g_slice_spoiled; 541 if (gp->dumpconf == NULL) 542 gp->dumpconf = g_slice_dumpconf; 543 if (gp->class->destroy_geom == NULL) 544 gp->class->destroy_geom = g_slice_destroy_geom; 545 cp = g_new_consumer(gp); 546 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 547 error = g_attach(cp, pp); 548 if (error == 0) 549 error = g_access(cp, 1, 0, 0); 550 if (error) { 551 g_wither_geom(gp, ENXIO); 552 return (NULL); 553 } 554 if (extrap != NULL) 555 *vp = gsp->softc; 556 *cpp = cp; 557 return (gp); 558 } 559