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 * Copyright (c) 2013 The FreeBSD Foundation 7 * All rights reserved. 8 * 9 * This software was developed for the FreeBSD Project by Poul-Henning Kamp 10 * and NAI Labs, the Security Research Division of Network Associates, Inc. 11 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 12 * DARPA CHATS research program. 13 * 14 * Portions of this software were developed by Konstantin Belousov 15 * under sponsorship from the FreeBSD Foundation. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 3. The names of the authors may not be used to endorse or promote 26 * products derived from this software without specific prior written 27 * permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 */ 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/kernel.h> 48 #include <sys/malloc.h> 49 #include <sys/bio.h> 50 #include <sys/ktr.h> 51 #include <sys/proc.h> 52 #include <sys/sbuf.h> 53 #include <sys/stack.h> 54 #include <sys/sysctl.h> 55 #include <sys/vmem.h> 56 #include <machine/stdarg.h> 57 58 #include <sys/errno.h> 59 #include <geom/geom.h> 60 #include <geom/geom_int.h> 61 #include <sys/devicestat.h> 62 63 #include <vm/uma.h> 64 #include <vm/vm.h> 65 #include <vm/vm_param.h> 66 #include <vm/vm_kern.h> 67 #include <vm/vm_page.h> 68 #include <vm/vm_object.h> 69 #include <vm/vm_extern.h> 70 #include <vm/vm_map.h> 71 72 static int g_io_transient_map_bio(struct bio *bp); 73 74 static struct g_bioq g_bio_run_down; 75 static struct g_bioq g_bio_run_up; 76 77 /* 78 * Pace is a hint that we've had some trouble recently allocating 79 * bios, so we should back off trying to send I/O down the stack 80 * a bit to let the problem resolve. When pacing, we also turn 81 * off direct dispatch to also reduce memory pressure from I/Os 82 * there, at the expxense of some added latency while the memory 83 * pressures exist. See g_io_schedule_down() for more details 84 * and limitations. 85 */ 86 static volatile u_int __read_mostly pace; 87 88 static uma_zone_t __read_mostly biozone; 89 90 #include <machine/atomic.h> 91 92 static void 93 g_bioq_lock(struct g_bioq *bq) 94 { 95 96 mtx_lock(&bq->bio_queue_lock); 97 } 98 99 static void 100 g_bioq_unlock(struct g_bioq *bq) 101 { 102 103 mtx_unlock(&bq->bio_queue_lock); 104 } 105 106 #if 0 107 static void 108 g_bioq_destroy(struct g_bioq *bq) 109 { 110 111 mtx_destroy(&bq->bio_queue_lock); 112 } 113 #endif 114 115 static void 116 g_bioq_init(struct g_bioq *bq) 117 { 118 119 TAILQ_INIT(&bq->bio_queue); 120 mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF); 121 } 122 123 static struct bio * 124 g_bioq_first(struct g_bioq *bq) 125 { 126 struct bio *bp; 127 128 bp = TAILQ_FIRST(&bq->bio_queue); 129 if (bp != NULL) { 130 KASSERT((bp->bio_flags & BIO_ONQUEUE), 131 ("Bio not on queue bp=%p target %p", bp, bq)); 132 bp->bio_flags &= ~BIO_ONQUEUE; 133 TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue); 134 bq->bio_queue_length--; 135 } 136 return (bp); 137 } 138 139 struct bio * 140 g_new_bio(void) 141 { 142 struct bio *bp; 143 144 bp = uma_zalloc(biozone, M_NOWAIT | M_ZERO); 145 #ifdef KTR 146 if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) { 147 struct stack st; 148 149 CTR1(KTR_GEOM, "g_new_bio(): %p", bp); 150 stack_save(&st); 151 CTRSTACK(KTR_GEOM, &st, 3); 152 } 153 #endif 154 return (bp); 155 } 156 157 struct bio * 158 g_alloc_bio(void) 159 { 160 struct bio *bp; 161 162 bp = uma_zalloc(biozone, M_WAITOK | M_ZERO); 163 #ifdef KTR 164 if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) { 165 struct stack st; 166 167 CTR1(KTR_GEOM, "g_alloc_bio(): %p", bp); 168 stack_save(&st); 169 CTRSTACK(KTR_GEOM, &st, 3); 170 } 171 #endif 172 return (bp); 173 } 174 175 void 176 g_destroy_bio(struct bio *bp) 177 { 178 #ifdef KTR 179 if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) { 180 struct stack st; 181 182 CTR1(KTR_GEOM, "g_destroy_bio(): %p", bp); 183 stack_save(&st); 184 CTRSTACK(KTR_GEOM, &st, 3); 185 } 186 #endif 187 uma_zfree(biozone, bp); 188 } 189 190 struct bio * 191 g_clone_bio(struct bio *bp) 192 { 193 struct bio *bp2; 194 195 bp2 = uma_zalloc(biozone, M_NOWAIT | M_ZERO); 196 if (bp2 != NULL) { 197 bp2->bio_parent = bp; 198 bp2->bio_cmd = bp->bio_cmd; 199 /* 200 * BIO_ORDERED flag may be used by disk drivers to enforce 201 * ordering restrictions, so this flag needs to be cloned. 202 * BIO_UNMAPPED and BIO_VLIST should be inherited, to properly 203 * indicate which way the buffer is passed. 204 * Other bio flags are not suitable for cloning. 205 */ 206 bp2->bio_flags = bp->bio_flags & 207 (BIO_ORDERED | BIO_UNMAPPED | BIO_VLIST); 208 bp2->bio_length = bp->bio_length; 209 bp2->bio_offset = bp->bio_offset; 210 bp2->bio_data = bp->bio_data; 211 bp2->bio_ma = bp->bio_ma; 212 bp2->bio_ma_n = bp->bio_ma_n; 213 bp2->bio_ma_offset = bp->bio_ma_offset; 214 bp2->bio_attribute = bp->bio_attribute; 215 if (bp->bio_cmd == BIO_ZONE) 216 bcopy(&bp->bio_zone, &bp2->bio_zone, 217 sizeof(bp->bio_zone)); 218 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) 219 bp2->bio_track_bp = bp->bio_track_bp; 220 #endif 221 bp->bio_children++; 222 } 223 #ifdef KTR 224 if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) { 225 struct stack st; 226 227 CTR2(KTR_GEOM, "g_clone_bio(%p): %p", bp, bp2); 228 stack_save(&st); 229 CTRSTACK(KTR_GEOM, &st, 3); 230 } 231 #endif 232 return(bp2); 233 } 234 235 struct bio * 236 g_duplicate_bio(struct bio *bp) 237 { 238 struct bio *bp2; 239 240 bp2 = uma_zalloc(biozone, M_WAITOK | M_ZERO); 241 bp2->bio_flags = bp->bio_flags & (BIO_UNMAPPED | BIO_VLIST); 242 bp2->bio_parent = bp; 243 bp2->bio_cmd = bp->bio_cmd; 244 bp2->bio_length = bp->bio_length; 245 bp2->bio_offset = bp->bio_offset; 246 bp2->bio_data = bp->bio_data; 247 bp2->bio_ma = bp->bio_ma; 248 bp2->bio_ma_n = bp->bio_ma_n; 249 bp2->bio_ma_offset = bp->bio_ma_offset; 250 bp2->bio_attribute = bp->bio_attribute; 251 bp->bio_children++; 252 #ifdef KTR 253 if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) { 254 struct stack st; 255 256 CTR2(KTR_GEOM, "g_duplicate_bio(%p): %p", bp, bp2); 257 stack_save(&st); 258 CTRSTACK(KTR_GEOM, &st, 3); 259 } 260 #endif 261 return(bp2); 262 } 263 264 void 265 g_reset_bio(struct bio *bp) 266 { 267 268 bzero(bp, sizeof(*bp)); 269 } 270 271 void 272 g_io_init() 273 { 274 275 g_bioq_init(&g_bio_run_down); 276 g_bioq_init(&g_bio_run_up); 277 biozone = uma_zcreate("g_bio", sizeof (struct bio), 278 NULL, NULL, 279 NULL, NULL, 280 0, 0); 281 } 282 283 int 284 g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr) 285 { 286 struct bio *bp; 287 int error; 288 289 g_trace(G_T_BIO, "bio_getattr(%s)", attr); 290 bp = g_alloc_bio(); 291 bp->bio_cmd = BIO_GETATTR; 292 bp->bio_done = NULL; 293 bp->bio_attribute = attr; 294 bp->bio_length = *len; 295 bp->bio_data = ptr; 296 g_io_request(bp, cp); 297 error = biowait(bp, "ggetattr"); 298 *len = bp->bio_completed; 299 g_destroy_bio(bp); 300 return (error); 301 } 302 303 int 304 g_io_zonecmd(struct disk_zone_args *zone_args, struct g_consumer *cp) 305 { 306 struct bio *bp; 307 int error; 308 309 g_trace(G_T_BIO, "bio_zone(%d)", zone_args->zone_cmd); 310 bp = g_alloc_bio(); 311 bp->bio_cmd = BIO_ZONE; 312 bp->bio_done = NULL; 313 /* 314 * XXX KDM need to handle report zone data. 315 */ 316 bcopy(zone_args, &bp->bio_zone, sizeof(*zone_args)); 317 if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES) 318 bp->bio_length = 319 zone_args->zone_params.report.entries_allocated * 320 sizeof(struct disk_zone_rep_entry); 321 else 322 bp->bio_length = 0; 323 324 g_io_request(bp, cp); 325 error = biowait(bp, "gzone"); 326 bcopy(&bp->bio_zone, zone_args, sizeof(*zone_args)); 327 g_destroy_bio(bp); 328 return (error); 329 } 330 331 /* 332 * Send a BIO_SPEEDUP down the stack. This is used to tell the lower layers that 333 * the upper layers have detected a resource shortage. The lower layers are 334 * advised to stop delaying I/O that they might be holding for performance 335 * reasons and to schedule it (non-trims) or complete it successfully (trims) as 336 * quickly as it can. bio_length is the amount of the shortage. This call 337 * should be non-blocking. bio_resid is used to communicate back if the lower 338 * layers couldn't find bio_length worth of I/O to schedule or discard. A length 339 * of 0 means to do as much as you can (schedule the h/w queues full, discard 340 * all trims). flags are a hint from the upper layers to the lower layers what 341 * operation should be done. 342 */ 343 int 344 g_io_speedup(size_t shortage, u_int flags, size_t *resid, struct g_consumer *cp) 345 { 346 struct bio *bp; 347 int error; 348 349 KASSERT((flags & (BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE)) != 0, 350 ("Invalid flags passed to g_io_speedup: %#x", flags)); 351 g_trace(G_T_BIO, "bio_speedup(%s, %zu, %#x)", cp->provider->name, 352 shortage, flags); 353 bp = g_new_bio(); 354 if (bp == NULL) 355 return (ENOMEM); 356 bp->bio_cmd = BIO_SPEEDUP; 357 bp->bio_length = shortage; 358 bp->bio_done = NULL; 359 bp->bio_flags |= flags; 360 g_io_request(bp, cp); 361 error = biowait(bp, "gflush"); 362 *resid = bp->bio_resid; 363 g_destroy_bio(bp); 364 return (error); 365 } 366 367 int 368 g_io_flush(struct g_consumer *cp) 369 { 370 struct bio *bp; 371 int error; 372 373 g_trace(G_T_BIO, "bio_flush(%s)", cp->provider->name); 374 bp = g_alloc_bio(); 375 bp->bio_cmd = BIO_FLUSH; 376 bp->bio_flags |= BIO_ORDERED; 377 bp->bio_done = NULL; 378 bp->bio_attribute = NULL; 379 bp->bio_offset = cp->provider->mediasize; 380 bp->bio_length = 0; 381 bp->bio_data = NULL; 382 g_io_request(bp, cp); 383 error = biowait(bp, "gflush"); 384 g_destroy_bio(bp); 385 return (error); 386 } 387 388 static int 389 g_io_check(struct bio *bp) 390 { 391 struct g_consumer *cp; 392 struct g_provider *pp; 393 off_t excess; 394 int error; 395 396 biotrack(bp, __func__); 397 398 cp = bp->bio_from; 399 pp = bp->bio_to; 400 401 /* Fail if access counters dont allow the operation */ 402 switch(bp->bio_cmd) { 403 case BIO_READ: 404 case BIO_GETATTR: 405 if (cp->acr == 0) 406 return (EPERM); 407 break; 408 case BIO_WRITE: 409 case BIO_DELETE: 410 case BIO_FLUSH: 411 if (cp->acw == 0) 412 return (EPERM); 413 break; 414 case BIO_ZONE: 415 if ((bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES) || 416 (bp->bio_zone.zone_cmd == DISK_ZONE_GET_PARAMS)) { 417 if (cp->acr == 0) 418 return (EPERM); 419 } else if (cp->acw == 0) 420 return (EPERM); 421 break; 422 default: 423 return (EPERM); 424 } 425 /* if provider is marked for error, don't disturb. */ 426 if (pp->error) 427 return (pp->error); 428 if (cp->flags & G_CF_ORPHAN) 429 return (ENXIO); 430 431 switch(bp->bio_cmd) { 432 case BIO_READ: 433 case BIO_WRITE: 434 case BIO_DELETE: 435 /* Zero sectorsize or mediasize is probably a lack of media. */ 436 if (pp->sectorsize == 0 || pp->mediasize == 0) 437 return (ENXIO); 438 /* Reject I/O not on sector boundary */ 439 if (bp->bio_offset % pp->sectorsize) 440 return (EINVAL); 441 /* Reject I/O not integral sector long */ 442 if (bp->bio_length % pp->sectorsize) 443 return (EINVAL); 444 /* Reject requests before or past the end of media. */ 445 if (bp->bio_offset < 0) 446 return (EIO); 447 if (bp->bio_offset > pp->mediasize) 448 return (EIO); 449 450 /* Truncate requests to the end of providers media. */ 451 excess = bp->bio_offset + bp->bio_length; 452 if (excess > bp->bio_to->mediasize) { 453 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 || 454 round_page(bp->bio_ma_offset + 455 bp->bio_length) / PAGE_SIZE == bp->bio_ma_n, 456 ("excess bio %p too short", bp)); 457 excess -= bp->bio_to->mediasize; 458 bp->bio_length -= excess; 459 if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 460 bp->bio_ma_n = round_page(bp->bio_ma_offset + 461 bp->bio_length) / PAGE_SIZE; 462 } 463 if (excess > 0) 464 CTR3(KTR_GEOM, "g_down truncated bio " 465 "%p provider %s by %d", bp, 466 bp->bio_to->name, excess); 467 } 468 469 /* Deliver zero length transfers right here. */ 470 if (bp->bio_length == 0) { 471 CTR2(KTR_GEOM, "g_down terminated 0-length " 472 "bp %p provider %s", bp, bp->bio_to->name); 473 return (0); 474 } 475 476 if ((bp->bio_flags & BIO_UNMAPPED) != 0 && 477 (bp->bio_to->flags & G_PF_ACCEPT_UNMAPPED) == 0 && 478 (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) { 479 if ((error = g_io_transient_map_bio(bp)) >= 0) 480 return (error); 481 } 482 break; 483 default: 484 break; 485 } 486 return (EJUSTRETURN); 487 } 488 489 void 490 g_io_request(struct bio *bp, struct g_consumer *cp) 491 { 492 struct g_provider *pp; 493 int direct, error, first; 494 uint8_t cmd; 495 496 biotrack(bp, __func__); 497 498 KASSERT(cp != NULL, ("NULL cp in g_io_request")); 499 KASSERT(bp != NULL, ("NULL bp in g_io_request")); 500 pp = cp->provider; 501 KASSERT(pp != NULL, ("consumer not attached in g_io_request")); 502 #ifdef DIAGNOSTIC 503 KASSERT(bp->bio_driver1 == NULL, 504 ("bio_driver1 used by the consumer (geom %s)", cp->geom->name)); 505 KASSERT(bp->bio_driver2 == NULL, 506 ("bio_driver2 used by the consumer (geom %s)", cp->geom->name)); 507 KASSERT(bp->bio_pflags == 0, 508 ("bio_pflags used by the consumer (geom %s)", cp->geom->name)); 509 /* 510 * Remember consumer's private fields, so we can detect if they were 511 * modified by the provider. 512 */ 513 bp->_bio_caller1 = bp->bio_caller1; 514 bp->_bio_caller2 = bp->bio_caller2; 515 bp->_bio_cflags = bp->bio_cflags; 516 #endif 517 518 cmd = bp->bio_cmd; 519 if (cmd == BIO_READ || cmd == BIO_WRITE || cmd == BIO_GETATTR) { 520 KASSERT(bp->bio_data != NULL, 521 ("NULL bp->data in g_io_request(cmd=%hu)", bp->bio_cmd)); 522 } 523 if (cmd == BIO_DELETE || cmd == BIO_FLUSH) { 524 KASSERT(bp->bio_data == NULL, 525 ("non-NULL bp->data in g_io_request(cmd=%hu)", 526 bp->bio_cmd)); 527 } 528 if (cmd == BIO_READ || cmd == BIO_WRITE || cmd == BIO_DELETE) { 529 KASSERT(bp->bio_offset % cp->provider->sectorsize == 0, 530 ("wrong offset %jd for sectorsize %u", 531 bp->bio_offset, cp->provider->sectorsize)); 532 KASSERT(bp->bio_length % cp->provider->sectorsize == 0, 533 ("wrong length %jd for sectorsize %u", 534 bp->bio_length, cp->provider->sectorsize)); 535 } 536 537 g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d", 538 bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd); 539 540 bp->bio_from = cp; 541 bp->bio_to = pp; 542 bp->bio_error = 0; 543 bp->bio_completed = 0; 544 545 KASSERT(!(bp->bio_flags & BIO_ONQUEUE), 546 ("Bio already on queue bp=%p", bp)); 547 548 if ((g_collectstats & G_STATS_CONSUMERS) != 0 || 549 ((g_collectstats & G_STATS_PROVIDERS) != 0 && pp->stat != NULL)) 550 binuptime(&bp->bio_t0); 551 else 552 getbinuptime(&bp->bio_t0); 553 if (g_collectstats & G_STATS_CONSUMERS) 554 devstat_start_transaction(cp->stat, &bp->bio_t0); 555 if (g_collectstats & G_STATS_PROVIDERS) 556 devstat_start_transaction(pp->stat, &bp->bio_t0); 557 #ifdef INVARIANTS 558 atomic_add_int(&cp->nstart, 1); 559 #endif 560 561 #ifdef GET_STACK_USAGE 562 direct = (cp->flags & G_CF_DIRECT_SEND) != 0 && 563 (pp->flags & G_PF_DIRECT_RECEIVE) != 0 && 564 !g_is_geom_thread(curthread) && 565 ((pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 || 566 (bp->bio_flags & BIO_UNMAPPED) == 0 || THREAD_CAN_SLEEP()) && 567 pace == 0; 568 if (direct) { 569 /* Block direct execution if less then half of stack left. */ 570 size_t st, su; 571 GET_STACK_USAGE(st, su); 572 if (su * 2 > st) 573 direct = 0; 574 } 575 #else 576 direct = 0; 577 #endif 578 579 if (direct) { 580 error = g_io_check(bp); 581 if (error >= 0) { 582 CTR3(KTR_GEOM, "g_io_request g_io_check on bp %p " 583 "provider %s returned %d", bp, bp->bio_to->name, 584 error); 585 g_io_deliver(bp, error); 586 return; 587 } 588 bp->bio_to->geom->start(bp); 589 } else { 590 g_bioq_lock(&g_bio_run_down); 591 first = TAILQ_EMPTY(&g_bio_run_down.bio_queue); 592 TAILQ_INSERT_TAIL(&g_bio_run_down.bio_queue, bp, bio_queue); 593 bp->bio_flags |= BIO_ONQUEUE; 594 g_bio_run_down.bio_queue_length++; 595 g_bioq_unlock(&g_bio_run_down); 596 /* Pass it on down. */ 597 if (first) 598 wakeup(&g_wait_down); 599 } 600 } 601 602 void 603 g_io_deliver(struct bio *bp, int error) 604 { 605 struct bintime now; 606 struct g_consumer *cp; 607 struct g_provider *pp; 608 struct mtx *mtxp; 609 int direct, first; 610 611 biotrack(bp, __func__); 612 613 KASSERT(bp != NULL, ("NULL bp in g_io_deliver")); 614 pp = bp->bio_to; 615 KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver")); 616 cp = bp->bio_from; 617 if (cp == NULL) { 618 bp->bio_error = error; 619 bp->bio_done(bp); 620 return; 621 } 622 KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver")); 623 KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver")); 624 #ifdef DIAGNOSTIC 625 /* 626 * Some classes - GJournal in particular - can modify bio's 627 * private fields while the bio is in transit; G_GEOM_VOLATILE_BIO 628 * flag means it's an expected behaviour for that particular geom. 629 */ 630 if ((cp->geom->flags & G_GEOM_VOLATILE_BIO) == 0) { 631 KASSERT(bp->bio_caller1 == bp->_bio_caller1, 632 ("bio_caller1 used by the provider %s", pp->name)); 633 KASSERT(bp->bio_caller2 == bp->_bio_caller2, 634 ("bio_caller2 used by the provider %s", pp->name)); 635 KASSERT(bp->bio_cflags == bp->_bio_cflags, 636 ("bio_cflags used by the provider %s", pp->name)); 637 } 638 #endif 639 KASSERT(bp->bio_completed >= 0, ("bio_completed can't be less than 0")); 640 KASSERT(bp->bio_completed <= bp->bio_length, 641 ("bio_completed can't be greater than bio_length")); 642 643 g_trace(G_T_BIO, 644 "g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd", 645 bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error, 646 (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length); 647 648 KASSERT(!(bp->bio_flags & BIO_ONQUEUE), 649 ("Bio already on queue bp=%p", bp)); 650 651 /* 652 * XXX: next two doesn't belong here 653 */ 654 bp->bio_bcount = bp->bio_length; 655 bp->bio_resid = bp->bio_bcount - bp->bio_completed; 656 657 #ifdef GET_STACK_USAGE 658 direct = (pp->flags & G_PF_DIRECT_SEND) && 659 (cp->flags & G_CF_DIRECT_RECEIVE) && 660 !g_is_geom_thread(curthread); 661 if (direct) { 662 /* Block direct execution if less then half of stack left. */ 663 size_t st, su; 664 GET_STACK_USAGE(st, su); 665 if (su * 2 > st) 666 direct = 0; 667 } 668 #else 669 direct = 0; 670 #endif 671 672 /* 673 * The statistics collection is lockless, as such, but we 674 * can not update one instance of the statistics from more 675 * than one thread at a time, so grab the lock first. 676 */ 677 if ((g_collectstats & G_STATS_CONSUMERS) != 0 || 678 ((g_collectstats & G_STATS_PROVIDERS) != 0 && pp->stat != NULL)) 679 binuptime(&now); 680 mtxp = mtx_pool_find(mtxpool_sleep, cp); 681 mtx_lock(mtxp); 682 if (g_collectstats & G_STATS_PROVIDERS) 683 devstat_end_transaction_bio_bt(pp->stat, bp, &now); 684 if (g_collectstats & G_STATS_CONSUMERS) 685 devstat_end_transaction_bio_bt(cp->stat, bp, &now); 686 #ifdef INVARIANTS 687 cp->nend++; 688 #endif 689 mtx_unlock(mtxp); 690 691 if (error != ENOMEM) { 692 bp->bio_error = error; 693 if (direct) { 694 biodone(bp); 695 } else { 696 g_bioq_lock(&g_bio_run_up); 697 first = TAILQ_EMPTY(&g_bio_run_up.bio_queue); 698 TAILQ_INSERT_TAIL(&g_bio_run_up.bio_queue, bp, bio_queue); 699 bp->bio_flags |= BIO_ONQUEUE; 700 g_bio_run_up.bio_queue_length++; 701 g_bioq_unlock(&g_bio_run_up); 702 if (first) 703 wakeup(&g_wait_up); 704 } 705 return; 706 } 707 708 if (bootverbose) 709 printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name); 710 bp->bio_children = 0; 711 bp->bio_inbed = 0; 712 bp->bio_driver1 = NULL; 713 bp->bio_driver2 = NULL; 714 bp->bio_pflags = 0; 715 g_io_request(bp, cp); 716 pace = 1; 717 return; 718 } 719 720 SYSCTL_DECL(_kern_geom); 721 722 static long transient_maps; 723 SYSCTL_LONG(_kern_geom, OID_AUTO, transient_maps, CTLFLAG_RD, 724 &transient_maps, 0, 725 "Total count of the transient mapping requests"); 726 u_int transient_map_retries = 10; 727 SYSCTL_UINT(_kern_geom, OID_AUTO, transient_map_retries, CTLFLAG_RW, 728 &transient_map_retries, 0, 729 "Max count of retries used before giving up on creating transient map"); 730 int transient_map_hard_failures; 731 SYSCTL_INT(_kern_geom, OID_AUTO, transient_map_hard_failures, CTLFLAG_RD, 732 &transient_map_hard_failures, 0, 733 "Failures to establish the transient mapping due to retry attempts " 734 "exhausted"); 735 int transient_map_soft_failures; 736 SYSCTL_INT(_kern_geom, OID_AUTO, transient_map_soft_failures, CTLFLAG_RD, 737 &transient_map_soft_failures, 0, 738 "Count of retried failures to establish the transient mapping"); 739 int inflight_transient_maps; 740 SYSCTL_INT(_kern_geom, OID_AUTO, inflight_transient_maps, CTLFLAG_RD, 741 &inflight_transient_maps, 0, 742 "Current count of the active transient maps"); 743 744 static int 745 g_io_transient_map_bio(struct bio *bp) 746 { 747 vm_offset_t addr; 748 long size; 749 u_int retried; 750 751 KASSERT(unmapped_buf_allowed, ("unmapped disabled")); 752 753 size = round_page(bp->bio_ma_offset + bp->bio_length); 754 KASSERT(size / PAGE_SIZE == bp->bio_ma_n, ("Bio too short %p", bp)); 755 addr = 0; 756 retried = 0; 757 atomic_add_long(&transient_maps, 1); 758 retry: 759 if (vmem_alloc(transient_arena, size, M_BESTFIT | M_NOWAIT, &addr)) { 760 if (transient_map_retries != 0 && 761 retried >= transient_map_retries) { 762 CTR2(KTR_GEOM, "g_down cannot map bp %p provider %s", 763 bp, bp->bio_to->name); 764 atomic_add_int(&transient_map_hard_failures, 1); 765 return (EDEADLK/* XXXKIB */); 766 } else { 767 /* 768 * Naive attempt to quisce the I/O to get more 769 * in-flight requests completed and defragment 770 * the transient_arena. 771 */ 772 CTR3(KTR_GEOM, "g_down retrymap bp %p provider %s r %d", 773 bp, bp->bio_to->name, retried); 774 pause("g_d_tra", hz / 10); 775 retried++; 776 atomic_add_int(&transient_map_soft_failures, 1); 777 goto retry; 778 } 779 } 780 atomic_add_int(&inflight_transient_maps, 1); 781 pmap_qenter((vm_offset_t)addr, bp->bio_ma, OFF_TO_IDX(size)); 782 bp->bio_data = (caddr_t)addr + bp->bio_ma_offset; 783 bp->bio_flags |= BIO_TRANSIENT_MAPPING; 784 bp->bio_flags &= ~BIO_UNMAPPED; 785 return (EJUSTRETURN); 786 } 787 788 void 789 g_io_schedule_down(struct thread *tp __unused) 790 { 791 struct bio *bp; 792 int error; 793 794 for(;;) { 795 g_bioq_lock(&g_bio_run_down); 796 bp = g_bioq_first(&g_bio_run_down); 797 if (bp == NULL) { 798 CTR0(KTR_GEOM, "g_down going to sleep"); 799 msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock, 800 PRIBIO | PDROP, "-", 0); 801 continue; 802 } 803 CTR0(KTR_GEOM, "g_down has work to do"); 804 g_bioq_unlock(&g_bio_run_down); 805 biotrack(bp, __func__); 806 if (pace != 0) { 807 /* 808 * There has been at least one memory allocation 809 * failure since the last I/O completed. Pause 1ms to 810 * give the system a chance to free up memory. We only 811 * do this once because a large number of allocations 812 * can fail in the direct dispatch case and there's no 813 * relationship between the number of these failures and 814 * the length of the outage. If there's still an outage, 815 * we'll pause again and again until it's 816 * resolved. Older versions paused longer and once per 817 * allocation failure. This was OK for a single threaded 818 * g_down, but with direct dispatch would lead to max of 819 * 10 IOPs for minutes at a time when transient memory 820 * issues prevented allocation for a batch of requests 821 * from the upper layers. 822 * 823 * XXX This pacing is really lame. It needs to be solved 824 * by other methods. This is OK only because the worst 825 * case scenario is so rare. In the worst case scenario 826 * all memory is tied up waiting for I/O to complete 827 * which can never happen since we can't allocate bios 828 * for that I/O. 829 */ 830 CTR0(KTR_GEOM, "g_down pacing self"); 831 pause("g_down", min(hz/1000, 1)); 832 pace = 0; 833 } 834 CTR2(KTR_GEOM, "g_down processing bp %p provider %s", bp, 835 bp->bio_to->name); 836 error = g_io_check(bp); 837 if (error >= 0) { 838 CTR3(KTR_GEOM, "g_down g_io_check on bp %p provider " 839 "%s returned %d", bp, bp->bio_to->name, error); 840 g_io_deliver(bp, error); 841 continue; 842 } 843 THREAD_NO_SLEEPING(); 844 CTR4(KTR_GEOM, "g_down starting bp %p provider %s off %ld " 845 "len %ld", bp, bp->bio_to->name, bp->bio_offset, 846 bp->bio_length); 847 bp->bio_to->geom->start(bp); 848 THREAD_SLEEPING_OK(); 849 } 850 } 851 852 void 853 g_io_schedule_up(struct thread *tp __unused) 854 { 855 struct bio *bp; 856 857 for(;;) { 858 g_bioq_lock(&g_bio_run_up); 859 bp = g_bioq_first(&g_bio_run_up); 860 if (bp == NULL) { 861 CTR0(KTR_GEOM, "g_up going to sleep"); 862 msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock, 863 PRIBIO | PDROP, "-", 0); 864 continue; 865 } 866 g_bioq_unlock(&g_bio_run_up); 867 THREAD_NO_SLEEPING(); 868 CTR4(KTR_GEOM, "g_up biodone bp %p provider %s off " 869 "%jd len %ld", bp, bp->bio_to->name, 870 bp->bio_offset, bp->bio_length); 871 biodone(bp); 872 THREAD_SLEEPING_OK(); 873 } 874 } 875 876 void * 877 g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error) 878 { 879 struct bio *bp; 880 void *ptr; 881 int errorc; 882 883 KASSERT(length > 0 && length >= cp->provider->sectorsize && 884 length <= MAXPHYS, ("g_read_data(): invalid length %jd", 885 (intmax_t)length)); 886 887 bp = g_alloc_bio(); 888 bp->bio_cmd = BIO_READ; 889 bp->bio_done = NULL; 890 bp->bio_offset = offset; 891 bp->bio_length = length; 892 ptr = g_malloc(length, M_WAITOK); 893 bp->bio_data = ptr; 894 g_io_request(bp, cp); 895 errorc = biowait(bp, "gread"); 896 if (error != NULL) 897 *error = errorc; 898 g_destroy_bio(bp); 899 if (errorc) { 900 g_free(ptr); 901 ptr = NULL; 902 } 903 return (ptr); 904 } 905 906 /* 907 * A read function for use by ffs_sbget when used by GEOM-layer routines. 908 */ 909 int 910 g_use_g_read_data(void *devfd, off_t loc, void **bufp, int size) 911 { 912 struct g_consumer *cp; 913 914 KASSERT(*bufp == NULL, 915 ("g_use_g_read_data: non-NULL *bufp %p\n", *bufp)); 916 917 cp = (struct g_consumer *)devfd; 918 /* 919 * Take care not to issue an invalid I/O request. The offset of 920 * the superblock candidate must be multiples of the provider's 921 * sector size, otherwise an FFS can't exist on the provider 922 * anyway. 923 */ 924 if (loc % cp->provider->sectorsize != 0) 925 return (ENOENT); 926 *bufp = g_read_data(cp, loc, size, NULL); 927 if (*bufp == NULL) 928 return (ENOENT); 929 return (0); 930 } 931 932 int 933 g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length) 934 { 935 struct bio *bp; 936 int error; 937 938 KASSERT(length > 0 && length >= cp->provider->sectorsize && 939 length <= MAXPHYS, ("g_write_data(): invalid length %jd", 940 (intmax_t)length)); 941 942 bp = g_alloc_bio(); 943 bp->bio_cmd = BIO_WRITE; 944 bp->bio_done = NULL; 945 bp->bio_offset = offset; 946 bp->bio_length = length; 947 bp->bio_data = ptr; 948 g_io_request(bp, cp); 949 error = biowait(bp, "gwrite"); 950 g_destroy_bio(bp); 951 return (error); 952 } 953 954 /* 955 * A write function for use by ffs_sbput when used by GEOM-layer routines. 956 */ 957 int 958 g_use_g_write_data(void *devfd, off_t loc, void *buf, int size) 959 { 960 961 return (g_write_data((struct g_consumer *)devfd, loc, buf, size)); 962 } 963 964 int 965 g_delete_data(struct g_consumer *cp, off_t offset, off_t length) 966 { 967 struct bio *bp; 968 int error; 969 970 KASSERT(length > 0 && length >= cp->provider->sectorsize, 971 ("g_delete_data(): invalid length %jd", (intmax_t)length)); 972 973 bp = g_alloc_bio(); 974 bp->bio_cmd = BIO_DELETE; 975 bp->bio_done = NULL; 976 bp->bio_offset = offset; 977 bp->bio_length = length; 978 bp->bio_data = NULL; 979 g_io_request(bp, cp); 980 error = biowait(bp, "gdelete"); 981 g_destroy_bio(bp); 982 return (error); 983 } 984 985 void 986 g_print_bio(const char *prefix, const struct bio *bp, const char *fmtsuffix, 987 ...) 988 { 989 #ifndef PRINTF_BUFR_SIZE 990 #define PRINTF_BUFR_SIZE 64 991 #endif 992 char bufr[PRINTF_BUFR_SIZE]; 993 struct sbuf sb, *sbp __unused; 994 va_list ap; 995 996 sbp = sbuf_new(&sb, bufr, sizeof(bufr), SBUF_FIXEDLEN); 997 KASSERT(sbp != NULL, ("sbuf_new misused?")); 998 999 sbuf_set_drain(&sb, sbuf_printf_drain, NULL); 1000 1001 sbuf_cat(&sb, prefix); 1002 g_format_bio(&sb, bp); 1003 1004 va_start(ap, fmtsuffix); 1005 sbuf_vprintf(&sb, fmtsuffix, ap); 1006 va_end(ap); 1007 1008 sbuf_nl_terminate(&sb); 1009 1010 sbuf_finish(&sb); 1011 sbuf_delete(&sb); 1012 } 1013 1014 void 1015 g_format_bio(struct sbuf *sb, const struct bio *bp) 1016 { 1017 const char *pname, *cmd = NULL; 1018 1019 if (bp->bio_to != NULL) 1020 pname = bp->bio_to->name; 1021 else 1022 pname = "[unknown]"; 1023 1024 switch (bp->bio_cmd) { 1025 case BIO_GETATTR: 1026 cmd = "GETATTR"; 1027 sbuf_printf(sb, "%s[%s(attr=%s)]", pname, cmd, 1028 bp->bio_attribute); 1029 return; 1030 case BIO_FLUSH: 1031 cmd = "FLUSH"; 1032 sbuf_printf(sb, "%s[%s]", pname, cmd); 1033 return; 1034 case BIO_ZONE: { 1035 char *subcmd = NULL; 1036 cmd = "ZONE"; 1037 switch (bp->bio_zone.zone_cmd) { 1038 case DISK_ZONE_OPEN: 1039 subcmd = "OPEN"; 1040 break; 1041 case DISK_ZONE_CLOSE: 1042 subcmd = "CLOSE"; 1043 break; 1044 case DISK_ZONE_FINISH: 1045 subcmd = "FINISH"; 1046 break; 1047 case DISK_ZONE_RWP: 1048 subcmd = "RWP"; 1049 break; 1050 case DISK_ZONE_REPORT_ZONES: 1051 subcmd = "REPORT ZONES"; 1052 break; 1053 case DISK_ZONE_GET_PARAMS: 1054 subcmd = "GET PARAMS"; 1055 break; 1056 default: 1057 subcmd = "UNKNOWN"; 1058 break; 1059 } 1060 sbuf_printf(sb, "%s[%s,%s]", pname, cmd, subcmd); 1061 return; 1062 } 1063 case BIO_READ: 1064 cmd = "READ"; 1065 break; 1066 case BIO_WRITE: 1067 cmd = "WRITE"; 1068 break; 1069 case BIO_DELETE: 1070 cmd = "DELETE"; 1071 break; 1072 default: 1073 cmd = "UNKNOWN"; 1074 sbuf_printf(sb, "%s[%s()]", pname, cmd); 1075 return; 1076 } 1077 sbuf_printf(sb, "%s[%s(offset=%jd, length=%jd)]", pname, cmd, 1078 (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length); 1079 } 1080