geom_io.c revision 135876
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 36#include <sys/cdefs.h> 37__FBSDID("$FreeBSD: head/sys/geom/geom_io.c 135876 2004-09-28 11:56:37Z phk $"); 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 45#include <sys/errno.h> 46#include <geom/geom.h> 47#include <geom/geom_int.h> 48#include <sys/devicestat.h> 49 50#include <vm/uma.h> 51 52static struct g_bioq g_bio_run_down; 53static struct g_bioq g_bio_run_up; 54static struct g_bioq g_bio_run_task; 55 56static u_int pace; 57static uma_zone_t biozone; 58 59#include <machine/atomic.h> 60 61static void 62g_bioq_lock(struct g_bioq *bq) 63{ 64 65 mtx_lock(&bq->bio_queue_lock); 66} 67 68static void 69g_bioq_unlock(struct g_bioq *bq) 70{ 71 72 mtx_unlock(&bq->bio_queue_lock); 73} 74 75#if 0 76static void 77g_bioq_destroy(struct g_bioq *bq) 78{ 79 80 mtx_destroy(&bq->bio_queue_lock); 81} 82#endif 83 84static void 85g_bioq_init(struct g_bioq *bq) 86{ 87 88 TAILQ_INIT(&bq->bio_queue); 89 mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF); 90} 91 92static struct bio * 93g_bioq_first(struct g_bioq *bq) 94{ 95 struct bio *bp; 96 97 bp = TAILQ_FIRST(&bq->bio_queue); 98 if (bp != NULL) { 99 KASSERT((bp->bio_flags & BIO_ONQUEUE), 100 ("Bio not on queue bp=%p target %p", bp, bq)); 101 bp->bio_flags &= ~BIO_ONQUEUE; 102 TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue); 103 bq->bio_queue_length--; 104 } 105 return (bp); 106} 107 108struct bio * 109g_new_bio(void) 110{ 111 struct bio *bp; 112 113 bp = uma_zalloc(biozone, M_NOWAIT | M_ZERO); 114 return (bp); 115} 116 117struct bio * 118g_alloc_bio(void) 119{ 120 struct bio *bp; 121 122 bp = uma_zalloc(biozone, M_WAITOK | M_ZERO); 123 return (bp); 124} 125 126void 127g_destroy_bio(struct bio *bp) 128{ 129 130 uma_zfree(biozone, bp); 131} 132 133struct bio * 134g_clone_bio(struct bio *bp) 135{ 136 struct bio *bp2; 137 138 bp2 = uma_zalloc(biozone, M_NOWAIT | M_ZERO); 139 if (bp2 != NULL) { 140 bp2->bio_parent = bp; 141 bp2->bio_cmd = bp->bio_cmd; 142 bp2->bio_length = bp->bio_length; 143 bp2->bio_offset = bp->bio_offset; 144 bp2->bio_data = bp->bio_data; 145 bp2->bio_attribute = bp->bio_attribute; 146 bp->bio_children++; 147 } 148 return(bp2); 149} 150 151void 152g_io_init() 153{ 154 155 g_bioq_init(&g_bio_run_down); 156 g_bioq_init(&g_bio_run_up); 157 g_bioq_init(&g_bio_run_task); 158 biozone = uma_zcreate("g_bio", sizeof (struct bio), 159 NULL, NULL, 160 NULL, NULL, 161 0, 0); 162} 163 164int 165g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr) 166{ 167 struct bio *bp; 168 int error; 169 170 g_trace(G_T_BIO, "bio_getattr(%s)", attr); 171 bp = g_alloc_bio(); 172 bp->bio_cmd = BIO_GETATTR; 173 bp->bio_done = NULL; 174 bp->bio_attribute = attr; 175 bp->bio_length = *len; 176 bp->bio_data = ptr; 177 g_io_request(bp, cp); 178 error = biowait(bp, "ggetattr"); 179 *len = bp->bio_completed; 180 g_destroy_bio(bp); 181 return (error); 182} 183 184static int 185g_io_check(struct bio *bp) 186{ 187 struct g_consumer *cp; 188 struct g_provider *pp; 189 190 cp = bp->bio_from; 191 pp = bp->bio_to; 192 193 /* Fail if access counters dont allow the operation */ 194 switch(bp->bio_cmd) { 195 case BIO_READ: 196 case BIO_GETATTR: 197 if (cp->acr == 0) 198 return (EPERM); 199 break; 200 case BIO_WRITE: 201 case BIO_DELETE: 202 if (cp->acw == 0) 203 return (EPERM); 204 break; 205 default: 206 return (EPERM); 207 } 208 /* if provider is marked for error, don't disturb. */ 209 if (pp->error) 210 return (pp->error); 211 212 switch(bp->bio_cmd) { 213 case BIO_READ: 214 case BIO_WRITE: 215 case BIO_DELETE: 216 /* Zero sectorsize is a probably lack of media */ 217 if (pp->sectorsize == 0) 218 return (ENXIO); 219 /* Reject I/O not on sector boundary */ 220 if (bp->bio_offset % pp->sectorsize) 221 return (EINVAL); 222 /* Reject I/O not integral sector long */ 223 if (bp->bio_length % pp->sectorsize) 224 return (EINVAL); 225 /* Reject requests before or past the end of media. */ 226 if (bp->bio_offset < 0) 227 return (EIO); 228 if (bp->bio_offset > pp->mediasize) 229 return (EIO); 230 break; 231 default: 232 break; 233 } 234 return (0); 235} 236 237void 238g_io_request(struct bio *bp, struct g_consumer *cp) 239{ 240 struct g_provider *pp; 241 242 KASSERT(cp != NULL, ("NULL cp in g_io_request")); 243 KASSERT(bp != NULL, ("NULL bp in g_io_request")); 244 KASSERT(bp->bio_data != NULL, ("NULL bp->data in g_io_request")); 245 pp = cp->provider; 246 KASSERT(pp != NULL, ("consumer not attached in g_io_request")); 247 248 if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) { 249 KASSERT(bp->bio_offset % cp->provider->sectorsize == 0, 250 ("wrong offset %jd for sectorsize %u", 251 bp->bio_offset, cp->provider->sectorsize)); 252 KASSERT(bp->bio_length % cp->provider->sectorsize == 0, 253 ("wrong length %jd for sectorsize %u", 254 bp->bio_length, cp->provider->sectorsize)); 255 } 256 257 bp->bio_from = cp; 258 bp->bio_to = pp; 259 bp->bio_error = 0; 260 bp->bio_completed = 0; 261 262 KASSERT(!(bp->bio_flags & BIO_ONQUEUE), 263 ("Bio already on queue bp=%p", bp)); 264 bp->bio_flags |= BIO_ONQUEUE; 265 266 binuptime(&bp->bio_t0); 267 if (g_collectstats & 4) 268 g_bioq_lock(&g_bio_run_down); 269 if (g_collectstats & 1) 270 devstat_start_transaction(pp->stat, &bp->bio_t0); 271 if (g_collectstats & 2) 272 devstat_start_transaction(cp->stat, &bp->bio_t0); 273 274 if (!(g_collectstats & 4)) 275 g_bioq_lock(&g_bio_run_down); 276 pp->nstart++; 277 cp->nstart++; 278 TAILQ_INSERT_TAIL(&g_bio_run_down.bio_queue, bp, bio_queue); 279 g_bio_run_down.bio_queue_length++; 280 g_bioq_unlock(&g_bio_run_down); 281 282 /* Pass it on down. */ 283 g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d", 284 bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd); 285 wakeup(&g_wait_down); 286} 287 288void 289g_io_deliver(struct bio *bp, int error) 290{ 291 struct g_consumer *cp; 292 struct g_provider *pp; 293 294 KASSERT(bp != NULL, ("NULL bp in g_io_deliver")); 295 pp = bp->bio_to; 296 KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver")); 297 cp = bp->bio_from; 298 if (cp == NULL) { 299 bp->bio_error = error; 300 bp->bio_done(bp); 301 return; 302 } 303 KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver")); 304 KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver")); 305 KASSERT(bp->bio_completed >= 0, ("bio_completed can't be less than 0")); 306 KASSERT(bp->bio_completed <= bp->bio_length, 307 ("bio_completed can't be greater than bio_length")); 308 309 g_trace(G_T_BIO, 310"g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd", 311 bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error, 312 (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length); 313 314 KASSERT(!(bp->bio_flags & BIO_ONQUEUE), 315 ("Bio already on queue bp=%p", bp)); 316 bp->bio_flags |= BIO_ONQUEUE; 317 318 /* 319 * XXX: next two doesn't belong here 320 */ 321 bp->bio_bcount = bp->bio_length; 322 bp->bio_resid = bp->bio_bcount - bp->bio_completed; 323 324 if (g_collectstats & 4) 325 g_bioq_lock(&g_bio_run_up); 326 if (g_collectstats & 1) 327 devstat_end_transaction_bio(pp->stat, bp); 328 if (g_collectstats & 2) 329 devstat_end_transaction_bio(cp->stat, bp); 330 if (!(g_collectstats & 4)) 331 g_bioq_lock(&g_bio_run_up); 332 cp->nend++; 333 pp->nend++; 334 if (error != ENOMEM) { 335 bp->bio_error = error; 336 TAILQ_INSERT_TAIL(&g_bio_run_up.bio_queue, bp, bio_queue); 337 g_bio_run_up.bio_queue_length++; 338 g_bioq_unlock(&g_bio_run_up); 339 wakeup(&g_wait_up); 340 return; 341 } 342 g_bioq_unlock(&g_bio_run_up); 343 344 if (bootverbose) 345 printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name); 346 bp->bio_children = 0; 347 bp->bio_inbed = 0; 348 g_io_request(bp, cp); 349 pace++; 350 return; 351} 352 353void 354g_io_schedule_down(struct thread *tp __unused) 355{ 356 struct bio *bp; 357 off_t excess; 358 int error; 359#ifdef WITNESS 360 struct mtx mymutex; 361 362 bzero(&mymutex, sizeof mymutex); 363 mtx_init(&mymutex, "g_xdown", NULL, MTX_DEF); 364#endif 365 366 for(;;) { 367 g_bioq_lock(&g_bio_run_down); 368 bp = g_bioq_first(&g_bio_run_down); 369 if (bp == NULL) { 370 msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock, 371 PRIBIO | PDROP, "-", hz/10); 372 continue; 373 } 374 g_bioq_unlock(&g_bio_run_down); 375 if (pace > 0) { 376 msleep(&error, NULL, PRIBIO, "g_down", hz/10); 377 pace--; 378 } 379 error = g_io_check(bp); 380 if (error) { 381 g_io_deliver(bp, error); 382 continue; 383 } 384 switch (bp->bio_cmd) { 385 case BIO_READ: 386 case BIO_WRITE: 387 case BIO_DELETE: 388 /* Truncate requests to the end of providers media. */ 389 excess = bp->bio_offset + bp->bio_length; 390 if (excess > bp->bio_to->mediasize) { 391 excess -= bp->bio_to->mediasize; 392 bp->bio_length -= excess; 393 } 394 /* Deliver zero length transfers right here. */ 395 if (bp->bio_length == 0) { 396 g_io_deliver(bp, 0); 397 continue; 398 } 399 break; 400 default: 401 break; 402 } 403#ifdef WITNESS 404 mtx_lock(&mymutex); 405#endif 406 bp->bio_to->geom->start(bp); 407#ifdef WITNESS 408 mtx_unlock(&mymutex); 409#endif 410 } 411} 412 413void 414bio_taskqueue(struct bio *bp, bio_task_t *func, void *arg) 415{ 416 bp->bio_task = func; 417 bp->bio_task_arg = arg; 418 /* 419 * The taskqueue is actually just a second queue off the "up" 420 * queue, so we use the same lock. 421 */ 422 g_bioq_lock(&g_bio_run_up); 423 KASSERT(!(bp->bio_flags & BIO_ONQUEUE), 424 ("Bio already on queue bp=%p target taskq", bp)); 425 bp->bio_flags |= BIO_ONQUEUE; 426 TAILQ_INSERT_TAIL(&g_bio_run_task.bio_queue, bp, bio_queue); 427 g_bio_run_task.bio_queue_length++; 428 wakeup(&g_wait_up); 429 g_bioq_unlock(&g_bio_run_up); 430} 431 432 433void 434g_io_schedule_up(struct thread *tp __unused) 435{ 436 struct bio *bp; 437#ifdef WITNESS 438 struct mtx mymutex; 439 440 bzero(&mymutex, sizeof mymutex); 441 mtx_init(&mymutex, "g_xup", NULL, MTX_DEF); 442#endif 443 for(;;) { 444 g_bioq_lock(&g_bio_run_up); 445 bp = g_bioq_first(&g_bio_run_task); 446 if (bp != NULL) { 447 g_bioq_unlock(&g_bio_run_up); 448#ifdef WITNESS 449 mtx_lock(&mymutex); 450#endif 451 bp->bio_task(bp->bio_task_arg); 452#ifdef WITNESS 453 mtx_unlock(&mymutex); 454#endif 455 continue; 456 } 457 bp = g_bioq_first(&g_bio_run_up); 458 if (bp != NULL) { 459 g_bioq_unlock(&g_bio_run_up); 460#ifdef WITNESS 461 mtx_lock(&mymutex); 462#endif 463 biodone(bp); 464#ifdef WITNESS 465 mtx_unlock(&mymutex); 466#endif 467 continue; 468 } 469 msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock, 470 PRIBIO | PDROP, "-", hz/10); 471 } 472} 473 474void * 475g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error) 476{ 477 struct bio *bp; 478 void *ptr; 479 int errorc; 480 481 KASSERT(length > 0 && length >= cp->provider->sectorsize && 482 length <= MAXPHYS, ("g_read_data(): invalid length %jd", 483 (intmax_t)length)); 484 485 bp = g_alloc_bio(); 486 bp->bio_cmd = BIO_READ; 487 bp->bio_done = NULL; 488 bp->bio_offset = offset; 489 bp->bio_length = length; 490 ptr = g_malloc(length, M_WAITOK); 491 bp->bio_data = ptr; 492 g_io_request(bp, cp); 493 errorc = biowait(bp, "gread"); 494 if (error != NULL) 495 *error = errorc; 496 g_destroy_bio(bp); 497 if (errorc) { 498 g_free(ptr); 499 ptr = NULL; 500 } 501 return (ptr); 502} 503 504int 505g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length) 506{ 507 struct bio *bp; 508 int error; 509 510 KASSERT(length > 0 && length >= cp->provider->sectorsize && 511 length <= MAXPHYS, ("g_write_data(): invalid length %jd", 512 (intmax_t)length)); 513 514 bp = g_alloc_bio(); 515 bp->bio_cmd = BIO_WRITE; 516 bp->bio_done = NULL; 517 bp->bio_offset = offset; 518 bp->bio_length = length; 519 bp->bio_data = ptr; 520 g_io_request(bp, cp); 521 error = biowait(bp, "gwrite"); 522 g_destroy_bio(bp); 523 return (error); 524} 525 526void 527g_print_bio(struct bio *bp) 528{ 529 const char *pname, *cmd = NULL; 530 531 if (bp->bio_to != NULL) 532 pname = bp->bio_to->name; 533 else 534 pname = "[unknown]"; 535 536 switch (bp->bio_cmd) { 537 case BIO_GETATTR: 538 cmd = "GETATTR"; 539 printf("%s[%s(attr=%s)]", pname, cmd, bp->bio_attribute); 540 return; 541 case BIO_READ: 542 cmd = "READ"; 543 case BIO_WRITE: 544 if (cmd == NULL) 545 cmd = "WRITE"; 546 case BIO_DELETE: 547 if (cmd == NULL) 548 cmd = "DELETE"; 549 printf("%s[%s(offset=%jd, length=%jd)]", pname, cmd, 550 (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length); 551 return; 552 default: 553 cmd = "UNKNOWN"; 554 printf("%s[%s()]", pname, cmd); 555 return; 556 } 557 /* NOTREACHED */ 558} 559