2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * Copyright (c) 2013 The FreeBSD Foundation
7 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
8 * and NAI Labs, the Security Research Division of Network Associates, Inc.
9 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
10 * DARPA CHATS research program.
12 * Portions of this software were developed by Konstantin Belousov
13 * under sponsorship from the FreeBSD Foundation.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. The names of the authors may not be used to endorse or promote
24 * products derived from this software without specific prior written
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
50 #include <sys/stack.h>
51 #include <sys/sysctl.h>
54 #include <sys/errno.h>
55 #include <geom/geom.h>
56 #include <geom/geom_int.h>
57 #include <sys/devicestat.h>
61 #include <vm/vm_param.h>
62 #include <vm/vm_kern.h>
63 #include <vm/vm_page.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_extern.h>
66 #include <vm/vm_map.h>
68 static int g_io_transient_map_bio(struct bio *bp);
70 static struct g_bioq g_bio_run_down;
71 static struct g_bioq g_bio_run_up;
74 * Pace is a hint that we've had some trouble recently allocating
75 * bios, so we should back off trying to send I/O down the stack
76 * a bit to let the problem resolve. When pacing, we also turn
77 * off direct dispatch to also reduce memory pressure from I/Os
78 * there, at the expxense of some added latency while the memory
79 * pressures exist. See g_io_schedule_down() for more details
82 static volatile u_int pace;
84 static uma_zone_t biozone;
87 * The head of the list of classifiers used in g_io_request.
88 * Use g_register_classifier() and g_unregister_classifier()
89 * to add/remove entries to the list.
90 * Classifiers are invoked in registration order.
92 static TAILQ_HEAD(g_classifier_tailq, g_classifier_hook)
93 g_classifier_tailq = TAILQ_HEAD_INITIALIZER(g_classifier_tailq);
95 #include <machine/atomic.h>
98 g_bioq_lock(struct g_bioq *bq)
101 mtx_lock(&bq->bio_queue_lock);
105 g_bioq_unlock(struct g_bioq *bq)
108 mtx_unlock(&bq->bio_queue_lock);
113 g_bioq_destroy(struct g_bioq *bq)
116 mtx_destroy(&bq->bio_queue_lock);
121 g_bioq_init(struct g_bioq *bq)
124 TAILQ_INIT(&bq->bio_queue);
125 mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF);
129 g_bioq_first(struct g_bioq *bq)
133 bp = TAILQ_FIRST(&bq->bio_queue);
135 KASSERT((bp->bio_flags & BIO_ONQUEUE),
136 ("Bio not on queue bp=%p target %p", bp, bq));
137 bp->bio_flags &= ~BIO_ONQUEUE;
138 TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
139 bq->bio_queue_length--;
149 bp = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
151 if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
154 CTR1(KTR_GEOM, "g_new_bio(): %p", bp);
156 CTRSTACK(KTR_GEOM, &st, 3, 0);
167 bp = uma_zalloc(biozone, M_WAITOK | M_ZERO);
169 if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
172 CTR1(KTR_GEOM, "g_alloc_bio(): %p", bp);
174 CTRSTACK(KTR_GEOM, &st, 3, 0);
181 g_destroy_bio(struct bio *bp)
184 if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
187 CTR1(KTR_GEOM, "g_destroy_bio(): %p", bp);
189 CTRSTACK(KTR_GEOM, &st, 3, 0);
192 uma_zfree(biozone, bp);
196 g_clone_bio(struct bio *bp)
200 bp2 = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
202 bp2->bio_parent = bp;
203 bp2->bio_cmd = bp->bio_cmd;
205 * BIO_ORDERED flag may be used by disk drivers to enforce
206 * ordering restrictions, so this flag needs to be cloned.
207 * BIO_UNMAPPED and BIO_VLIST should be inherited, to properly
208 * indicate which way the buffer is passed.
209 * Other bio flags are not suitable for cloning.
211 bp2->bio_flags = bp->bio_flags &
212 (BIO_ORDERED | BIO_UNMAPPED | BIO_VLIST);
213 bp2->bio_length = bp->bio_length;
214 bp2->bio_offset = bp->bio_offset;
215 bp2->bio_data = bp->bio_data;
216 bp2->bio_ma = bp->bio_ma;
217 bp2->bio_ma_n = bp->bio_ma_n;
218 bp2->bio_ma_offset = bp->bio_ma_offset;
219 bp2->bio_attribute = bp->bio_attribute;
220 if (bp->bio_cmd == BIO_ZONE)
221 bcopy(&bp->bio_zone, &bp2->bio_zone,
222 sizeof(bp->bio_zone));
223 /* Inherit classification info from the parent */
224 bp2->bio_classifier1 = bp->bio_classifier1;
225 bp2->bio_classifier2 = bp->bio_classifier2;
226 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
227 bp2->bio_track_bp = bp->bio_track_bp;
232 if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
235 CTR2(KTR_GEOM, "g_clone_bio(%p): %p", bp, bp2);
237 CTRSTACK(KTR_GEOM, &st, 3, 0);
244 g_duplicate_bio(struct bio *bp)
248 bp2 = uma_zalloc(biozone, M_WAITOK | M_ZERO);
249 bp2->bio_flags = bp->bio_flags & (BIO_UNMAPPED | BIO_VLIST);
250 bp2->bio_parent = bp;
251 bp2->bio_cmd = bp->bio_cmd;
252 bp2->bio_length = bp->bio_length;
253 bp2->bio_offset = bp->bio_offset;
254 bp2->bio_data = bp->bio_data;
255 bp2->bio_ma = bp->bio_ma;
256 bp2->bio_ma_n = bp->bio_ma_n;
257 bp2->bio_ma_offset = bp->bio_ma_offset;
258 bp2->bio_attribute = bp->bio_attribute;
261 if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
264 CTR2(KTR_GEOM, "g_duplicate_bio(%p): %p", bp, bp2);
266 CTRSTACK(KTR_GEOM, &st, 3, 0);
273 g_reset_bio(struct bio *bp)
276 bzero(bp, sizeof(*bp));
283 g_bioq_init(&g_bio_run_down);
284 g_bioq_init(&g_bio_run_up);
285 biozone = uma_zcreate("g_bio", sizeof (struct bio),
292 g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr)
297 g_trace(G_T_BIO, "bio_getattr(%s)", attr);
299 bp->bio_cmd = BIO_GETATTR;
301 bp->bio_attribute = attr;
302 bp->bio_length = *len;
304 g_io_request(bp, cp);
305 error = biowait(bp, "ggetattr");
306 *len = bp->bio_completed;
312 g_io_zonecmd(struct disk_zone_args *zone_args, struct g_consumer *cp)
317 g_trace(G_T_BIO, "bio_zone(%d)", zone_args->zone_cmd);
319 bp->bio_cmd = BIO_ZONE;
322 * XXX KDM need to handle report zone data.
324 bcopy(zone_args, &bp->bio_zone, sizeof(*zone_args));
325 if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES)
327 zone_args->zone_params.report.entries_allocated *
328 sizeof(struct disk_zone_rep_entry);
332 g_io_request(bp, cp);
333 error = biowait(bp, "gzone");
334 bcopy(&bp->bio_zone, zone_args, sizeof(*zone_args));
340 g_io_flush(struct g_consumer *cp)
345 g_trace(G_T_BIO, "bio_flush(%s)", cp->provider->name);
347 bp->bio_cmd = BIO_FLUSH;
348 bp->bio_flags |= BIO_ORDERED;
350 bp->bio_attribute = NULL;
351 bp->bio_offset = cp->provider->mediasize;
354 g_io_request(bp, cp);
355 error = biowait(bp, "gflush");
361 g_io_check(struct bio *bp)
363 struct g_consumer *cp;
364 struct g_provider *pp;
368 biotrack(bp, __func__);
373 /* Fail if access counters dont allow the operation */
374 switch(bp->bio_cmd) {
387 if ((bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES) ||
388 (bp->bio_zone.zone_cmd == DISK_ZONE_GET_PARAMS)) {
391 } else if (cp->acw == 0)
397 /* if provider is marked for error, don't disturb. */
400 if (cp->flags & G_CF_ORPHAN)
403 switch(bp->bio_cmd) {
407 /* Zero sectorsize or mediasize is probably a lack of media. */
408 if (pp->sectorsize == 0 || pp->mediasize == 0)
410 /* Reject I/O not on sector boundary */
411 if (bp->bio_offset % pp->sectorsize)
413 /* Reject I/O not integral sector long */
414 if (bp->bio_length % pp->sectorsize)
416 /* Reject requests before or past the end of media. */
417 if (bp->bio_offset < 0)
419 if (bp->bio_offset > pp->mediasize)
422 /* Truncate requests to the end of providers media. */
423 excess = bp->bio_offset + bp->bio_length;
424 if (excess > bp->bio_to->mediasize) {
425 KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
426 round_page(bp->bio_ma_offset +
427 bp->bio_length) / PAGE_SIZE == bp->bio_ma_n,
428 ("excess bio %p too short", bp));
429 excess -= bp->bio_to->mediasize;
430 bp->bio_length -= excess;
431 if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
432 bp->bio_ma_n = round_page(bp->bio_ma_offset +
433 bp->bio_length) / PAGE_SIZE;
436 CTR3(KTR_GEOM, "g_down truncated bio "
437 "%p provider %s by %d", bp,
438 bp->bio_to->name, excess);
441 /* Deliver zero length transfers right here. */
442 if (bp->bio_length == 0) {
443 CTR2(KTR_GEOM, "g_down terminated 0-length "
444 "bp %p provider %s", bp, bp->bio_to->name);
448 if ((bp->bio_flags & BIO_UNMAPPED) != 0 &&
449 (bp->bio_to->flags & G_PF_ACCEPT_UNMAPPED) == 0 &&
450 (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
451 if ((error = g_io_transient_map_bio(bp)) >= 0)
458 return (EJUSTRETURN);
462 * bio classification support.
464 * g_register_classifier() and g_unregister_classifier()
465 * are used to add/remove a classifier from the list.
466 * The list is protected using the g_bio_run_down lock,
467 * because the classifiers are called in this path.
469 * g_io_request() passes bio's that are not already classified
470 * (i.e. those with bio_classifier1 == NULL) to g_run_classifiers().
471 * Classifiers can store their result in the two fields
472 * bio_classifier1 and bio_classifier2.
473 * A classifier that updates one of the fields should
474 * return a non-zero value.
475 * If no classifier updates the field, g_run_classifiers() sets
476 * bio_classifier1 = BIO_NOTCLASSIFIED to avoid further calls.
480 g_register_classifier(struct g_classifier_hook *hook)
483 g_bioq_lock(&g_bio_run_down);
484 TAILQ_INSERT_TAIL(&g_classifier_tailq, hook, link);
485 g_bioq_unlock(&g_bio_run_down);
491 g_unregister_classifier(struct g_classifier_hook *hook)
493 struct g_classifier_hook *entry;
495 g_bioq_lock(&g_bio_run_down);
496 TAILQ_FOREACH(entry, &g_classifier_tailq, link) {
498 TAILQ_REMOVE(&g_classifier_tailq, hook, link);
502 g_bioq_unlock(&g_bio_run_down);
506 g_run_classifiers(struct bio *bp)
508 struct g_classifier_hook *hook;
511 biotrack(bp, __func__);
513 TAILQ_FOREACH(hook, &g_classifier_tailq, link)
514 classified |= hook->func(hook->arg, bp);
517 bp->bio_classifier1 = BIO_NOTCLASSIFIED;
521 g_io_request(struct bio *bp, struct g_consumer *cp)
523 struct g_provider *pp;
525 int direct, error, first;
528 biotrack(bp, __func__);
530 KASSERT(cp != NULL, ("NULL cp in g_io_request"));
531 KASSERT(bp != NULL, ("NULL bp in g_io_request"));
533 KASSERT(pp != NULL, ("consumer not attached in g_io_request"));
535 KASSERT(bp->bio_driver1 == NULL,
536 ("bio_driver1 used by the consumer (geom %s)", cp->geom->name));
537 KASSERT(bp->bio_driver2 == NULL,
538 ("bio_driver2 used by the consumer (geom %s)", cp->geom->name));
539 KASSERT(bp->bio_pflags == 0,
540 ("bio_pflags used by the consumer (geom %s)", cp->geom->name));
542 * Remember consumer's private fields, so we can detect if they were
543 * modified by the provider.
545 bp->_bio_caller1 = bp->bio_caller1;
546 bp->_bio_caller2 = bp->bio_caller2;
547 bp->_bio_cflags = bp->bio_cflags;
551 if (cmd == BIO_READ || cmd == BIO_WRITE || cmd == BIO_GETATTR) {
552 KASSERT(bp->bio_data != NULL,
553 ("NULL bp->data in g_io_request(cmd=%hu)", bp->bio_cmd));
555 if (cmd == BIO_DELETE || cmd == BIO_FLUSH) {
556 KASSERT(bp->bio_data == NULL,
557 ("non-NULL bp->data in g_io_request(cmd=%hu)",
560 if (cmd == BIO_READ || cmd == BIO_WRITE || cmd == BIO_DELETE) {
561 KASSERT(bp->bio_offset % cp->provider->sectorsize == 0,
562 ("wrong offset %jd for sectorsize %u",
563 bp->bio_offset, cp->provider->sectorsize));
564 KASSERT(bp->bio_length % cp->provider->sectorsize == 0,
565 ("wrong length %jd for sectorsize %u",
566 bp->bio_length, cp->provider->sectorsize));
569 g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
570 bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd);
575 bp->bio_completed = 0;
577 KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
578 ("Bio already on queue bp=%p", bp));
579 if ((g_collectstats & G_STATS_CONSUMERS) != 0 ||
580 ((g_collectstats & G_STATS_PROVIDERS) != 0 && pp->stat != NULL))
581 binuptime(&bp->bio_t0);
583 getbinuptime(&bp->bio_t0);
585 #ifdef GET_STACK_USAGE
586 direct = (cp->flags & G_CF_DIRECT_SEND) != 0 &&
587 (pp->flags & G_PF_DIRECT_RECEIVE) != 0 &&
588 !g_is_geom_thread(curthread) &&
589 ((pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ||
590 (bp->bio_flags & BIO_UNMAPPED) == 0 || THREAD_CAN_SLEEP()) &&
593 /* Block direct execution if less then half of stack left. */
595 GET_STACK_USAGE(st, su);
603 if (!TAILQ_EMPTY(&g_classifier_tailq) && !bp->bio_classifier1) {
604 g_bioq_lock(&g_bio_run_down);
605 g_run_classifiers(bp);
606 g_bioq_unlock(&g_bio_run_down);
610 * The statistics collection is lockless, as such, but we
611 * can not update one instance of the statistics from more
612 * than one thread at a time, so grab the lock first.
614 mtxp = mtx_pool_find(mtxpool_sleep, pp);
616 if (g_collectstats & G_STATS_PROVIDERS)
617 devstat_start_transaction(pp->stat, &bp->bio_t0);
618 if (g_collectstats & G_STATS_CONSUMERS)
619 devstat_start_transaction(cp->stat, &bp->bio_t0);
625 error = g_io_check(bp);
627 CTR3(KTR_GEOM, "g_io_request g_io_check on bp %p "
628 "provider %s returned %d", bp, bp->bio_to->name,
630 g_io_deliver(bp, error);
633 bp->bio_to->geom->start(bp);
635 g_bioq_lock(&g_bio_run_down);
636 first = TAILQ_EMPTY(&g_bio_run_down.bio_queue);
637 TAILQ_INSERT_TAIL(&g_bio_run_down.bio_queue, bp, bio_queue);
638 bp->bio_flags |= BIO_ONQUEUE;
639 g_bio_run_down.bio_queue_length++;
640 g_bioq_unlock(&g_bio_run_down);
641 /* Pass it on down. */
643 wakeup(&g_wait_down);
648 g_io_deliver(struct bio *bp, int error)
651 struct g_consumer *cp;
652 struct g_provider *pp;
656 biotrack(bp, __func__);
658 KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
660 KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver"));
663 bp->bio_error = error;
667 KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver"));
668 KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
671 * Some classes - GJournal in particular - can modify bio's
672 * private fields while the bio is in transit; G_GEOM_VOLATILE_BIO
673 * flag means it's an expected behaviour for that particular geom.
675 if ((cp->geom->flags & G_GEOM_VOLATILE_BIO) == 0) {
676 KASSERT(bp->bio_caller1 == bp->_bio_caller1,
677 ("bio_caller1 used by the provider %s", pp->name));
678 KASSERT(bp->bio_caller2 == bp->_bio_caller2,
679 ("bio_caller2 used by the provider %s", pp->name));
680 KASSERT(bp->bio_cflags == bp->_bio_cflags,
681 ("bio_cflags used by the provider %s", pp->name));
684 KASSERT(bp->bio_completed >= 0, ("bio_completed can't be less than 0"));
685 KASSERT(bp->bio_completed <= bp->bio_length,
686 ("bio_completed can't be greater than bio_length"));
689 "g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
690 bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error,
691 (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
693 KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
694 ("Bio already on queue bp=%p", bp));
697 * XXX: next two doesn't belong here
699 bp->bio_bcount = bp->bio_length;
700 bp->bio_resid = bp->bio_bcount - bp->bio_completed;
702 #ifdef GET_STACK_USAGE
703 direct = (pp->flags & G_PF_DIRECT_SEND) &&
704 (cp->flags & G_CF_DIRECT_RECEIVE) &&
705 !g_is_geom_thread(curthread);
707 /* Block direct execution if less then half of stack left. */
709 GET_STACK_USAGE(st, su);
718 * The statistics collection is lockless, as such, but we
719 * can not update one instance of the statistics from more
720 * than one thread at a time, so grab the lock first.
722 if ((g_collectstats & G_STATS_CONSUMERS) != 0 ||
723 ((g_collectstats & G_STATS_PROVIDERS) != 0 && pp->stat != NULL))
725 mtxp = mtx_pool_find(mtxpool_sleep, cp);
727 if (g_collectstats & G_STATS_PROVIDERS)
728 devstat_end_transaction_bio_bt(pp->stat, bp, &now);
729 if (g_collectstats & G_STATS_CONSUMERS)
730 devstat_end_transaction_bio_bt(cp->stat, bp, &now);
735 if (error != ENOMEM) {
736 bp->bio_error = error;
740 g_bioq_lock(&g_bio_run_up);
741 first = TAILQ_EMPTY(&g_bio_run_up.bio_queue);
742 TAILQ_INSERT_TAIL(&g_bio_run_up.bio_queue, bp, bio_queue);
743 bp->bio_flags |= BIO_ONQUEUE;
744 g_bio_run_up.bio_queue_length++;
745 g_bioq_unlock(&g_bio_run_up);
753 printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name);
754 bp->bio_children = 0;
756 bp->bio_driver1 = NULL;
757 bp->bio_driver2 = NULL;
759 g_io_request(bp, cp);
764 SYSCTL_DECL(_kern_geom);
766 static long transient_maps;
767 SYSCTL_LONG(_kern_geom, OID_AUTO, transient_maps, CTLFLAG_RD,
769 "Total count of the transient mapping requests");
770 u_int transient_map_retries = 10;
771 SYSCTL_UINT(_kern_geom, OID_AUTO, transient_map_retries, CTLFLAG_RW,
772 &transient_map_retries, 0,
773 "Max count of retries used before giving up on creating transient map");
774 int transient_map_hard_failures;
775 SYSCTL_INT(_kern_geom, OID_AUTO, transient_map_hard_failures, CTLFLAG_RD,
776 &transient_map_hard_failures, 0,
777 "Failures to establish the transient mapping due to retry attempts "
779 int transient_map_soft_failures;
780 SYSCTL_INT(_kern_geom, OID_AUTO, transient_map_soft_failures, CTLFLAG_RD,
781 &transient_map_soft_failures, 0,
782 "Count of retried failures to establish the transient mapping");
783 int inflight_transient_maps;
784 SYSCTL_INT(_kern_geom, OID_AUTO, inflight_transient_maps, CTLFLAG_RD,
785 &inflight_transient_maps, 0,
786 "Current count of the active transient maps");
789 g_io_transient_map_bio(struct bio *bp)
795 KASSERT(unmapped_buf_allowed, ("unmapped disabled"));
797 size = round_page(bp->bio_ma_offset + bp->bio_length);
798 KASSERT(size / PAGE_SIZE == bp->bio_ma_n, ("Bio too short %p", bp));
801 atomic_add_long(&transient_maps, 1);
803 if (vmem_alloc(transient_arena, size, M_BESTFIT | M_NOWAIT, &addr)) {
804 if (transient_map_retries != 0 &&
805 retried >= transient_map_retries) {
806 CTR2(KTR_GEOM, "g_down cannot map bp %p provider %s",
807 bp, bp->bio_to->name);
808 atomic_add_int(&transient_map_hard_failures, 1);
809 return (EDEADLK/* XXXKIB */);
812 * Naive attempt to quisce the I/O to get more
813 * in-flight requests completed and defragment
814 * the transient_arena.
816 CTR3(KTR_GEOM, "g_down retrymap bp %p provider %s r %d",
817 bp, bp->bio_to->name, retried);
818 pause("g_d_tra", hz / 10);
820 atomic_add_int(&transient_map_soft_failures, 1);
824 atomic_add_int(&inflight_transient_maps, 1);
825 pmap_qenter((vm_offset_t)addr, bp->bio_ma, OFF_TO_IDX(size));
826 bp->bio_data = (caddr_t)addr + bp->bio_ma_offset;
827 bp->bio_flags |= BIO_TRANSIENT_MAPPING;
828 bp->bio_flags &= ~BIO_UNMAPPED;
829 return (EJUSTRETURN);
833 g_io_schedule_down(struct thread *tp __unused)
839 g_bioq_lock(&g_bio_run_down);
840 bp = g_bioq_first(&g_bio_run_down);
842 CTR0(KTR_GEOM, "g_down going to sleep");
843 msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock,
844 PRIBIO | PDROP, "-", 0);
847 CTR0(KTR_GEOM, "g_down has work to do");
848 g_bioq_unlock(&g_bio_run_down);
849 biotrack(bp, __func__);
852 * There has been at least one memory allocation
853 * failure since the last I/O completed. Pause 1ms to
854 * give the system a chance to free up memory. We only
855 * do this once because a large number of allocations
856 * can fail in the direct dispatch case and there's no
857 * relationship between the number of these failures and
858 * the length of the outage. If there's still an outage,
859 * we'll pause again and again until it's
860 * resolved. Older versions paused longer and once per
861 * allocation failure. This was OK for a single threaded
862 * g_down, but with direct dispatch would lead to max of
863 * 10 IOPs for minutes at a time when transient memory
864 * issues prevented allocation for a batch of requests
865 * from the upper layers.
867 * XXX This pacing is really lame. It needs to be solved
868 * by other methods. This is OK only because the worst
869 * case scenario is so rare. In the worst case scenario
870 * all memory is tied up waiting for I/O to complete
871 * which can never happen since we can't allocate bios
874 CTR0(KTR_GEOM, "g_down pacing self");
875 pause("g_down", min(hz/1000, 1));
878 CTR2(KTR_GEOM, "g_down processing bp %p provider %s", bp,
880 error = g_io_check(bp);
882 CTR3(KTR_GEOM, "g_down g_io_check on bp %p provider "
883 "%s returned %d", bp, bp->bio_to->name, error);
884 g_io_deliver(bp, error);
887 THREAD_NO_SLEEPING();
888 CTR4(KTR_GEOM, "g_down starting bp %p provider %s off %ld "
889 "len %ld", bp, bp->bio_to->name, bp->bio_offset,
891 bp->bio_to->geom->start(bp);
892 THREAD_SLEEPING_OK();
897 g_io_schedule_up(struct thread *tp __unused)
902 g_bioq_lock(&g_bio_run_up);
903 bp = g_bioq_first(&g_bio_run_up);
905 CTR0(KTR_GEOM, "g_up going to sleep");
906 msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock,
907 PRIBIO | PDROP, "-", 0);
910 g_bioq_unlock(&g_bio_run_up);
911 THREAD_NO_SLEEPING();
912 CTR4(KTR_GEOM, "g_up biodone bp %p provider %s off "
913 "%jd len %ld", bp, bp->bio_to->name,
914 bp->bio_offset, bp->bio_length);
916 THREAD_SLEEPING_OK();
921 g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
927 KASSERT(length > 0 && length >= cp->provider->sectorsize &&
928 length <= MAXPHYS, ("g_read_data(): invalid length %jd",
932 bp->bio_cmd = BIO_READ;
934 bp->bio_offset = offset;
935 bp->bio_length = length;
936 ptr = g_malloc(length, M_WAITOK);
938 g_io_request(bp, cp);
939 errorc = biowait(bp, "gread");
951 g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
956 KASSERT(length > 0 && length >= cp->provider->sectorsize &&
957 length <= MAXPHYS, ("g_write_data(): invalid length %jd",
961 bp->bio_cmd = BIO_WRITE;
963 bp->bio_offset = offset;
964 bp->bio_length = length;
966 g_io_request(bp, cp);
967 error = biowait(bp, "gwrite");
973 g_delete_data(struct g_consumer *cp, off_t offset, off_t length)
978 KASSERT(length > 0 && length >= cp->provider->sectorsize,
979 ("g_delete_data(): invalid length %jd", (intmax_t)length));
982 bp->bio_cmd = BIO_DELETE;
984 bp->bio_offset = offset;
985 bp->bio_length = length;
987 g_io_request(bp, cp);
988 error = biowait(bp, "gdelete");
994 g_print_bio(struct bio *bp)
996 const char *pname, *cmd = NULL;
998 if (bp->bio_to != NULL)
999 pname = bp->bio_to->name;
1001 pname = "[unknown]";
1003 switch (bp->bio_cmd) {
1006 printf("%s[%s(attr=%s)]", pname, cmd, bp->bio_attribute);
1010 printf("%s[%s]", pname, cmd);
1013 char *subcmd = NULL;
1015 switch (bp->bio_zone.zone_cmd) {
1016 case DISK_ZONE_OPEN:
1019 case DISK_ZONE_CLOSE:
1022 case DISK_ZONE_FINISH:
1028 case DISK_ZONE_REPORT_ZONES:
1029 subcmd = "REPORT ZONES";
1031 case DISK_ZONE_GET_PARAMS:
1032 subcmd = "GET PARAMS";
1038 printf("%s[%s,%s]", pname, cmd, subcmd);
1052 printf("%s[%s()]", pname, cmd);
1055 printf("%s[%s(offset=%jd, length=%jd)]", pname, cmd,
1056 (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);