xref: /freebsd/sys/geom/geom_io.c (revision 9a6844d55fe33a5c55973951843be9aac013693f)
1dd84a43cSPoul-Henning Kamp /*-
2dd84a43cSPoul-Henning Kamp  * Copyright (c) 2002 Poul-Henning Kamp
3dd84a43cSPoul-Henning Kamp  * Copyright (c) 2002 Networks Associates Technology, Inc.
4ee75e7deSKonstantin Belousov  * Copyright (c) 2013 The FreeBSD Foundation
5dd84a43cSPoul-Henning Kamp  * All rights reserved.
6dd84a43cSPoul-Henning Kamp  *
7dd84a43cSPoul-Henning Kamp  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
8dd84a43cSPoul-Henning Kamp  * and NAI Labs, the Security Research Division of Network Associates, Inc.
9dd84a43cSPoul-Henning Kamp  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
10dd84a43cSPoul-Henning Kamp  * DARPA CHATS research program.
11dd84a43cSPoul-Henning Kamp  *
12ee75e7deSKonstantin Belousov  * Portions of this software were developed by Konstantin Belousov
13ee75e7deSKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
14ee75e7deSKonstantin Belousov  *
15dd84a43cSPoul-Henning Kamp  * Redistribution and use in source and binary forms, with or without
16dd84a43cSPoul-Henning Kamp  * modification, are permitted provided that the following conditions
17dd84a43cSPoul-Henning Kamp  * are met:
18dd84a43cSPoul-Henning Kamp  * 1. Redistributions of source code must retain the above copyright
19dd84a43cSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer.
20dd84a43cSPoul-Henning Kamp  * 2. Redistributions in binary form must reproduce the above copyright
21dd84a43cSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer in the
22dd84a43cSPoul-Henning Kamp  *    documentation and/or other materials provided with the distribution.
23dd84a43cSPoul-Henning Kamp  * 3. The names of the authors may not be used to endorse or promote
24dd84a43cSPoul-Henning Kamp  *    products derived from this software without specific prior written
25dd84a43cSPoul-Henning Kamp  *    permission.
26dd84a43cSPoul-Henning Kamp  *
27dd84a43cSPoul-Henning Kamp  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28dd84a43cSPoul-Henning Kamp  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29dd84a43cSPoul-Henning Kamp  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30dd84a43cSPoul-Henning Kamp  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31dd84a43cSPoul-Henning Kamp  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32dd84a43cSPoul-Henning Kamp  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33dd84a43cSPoul-Henning Kamp  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34dd84a43cSPoul-Henning Kamp  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35dd84a43cSPoul-Henning Kamp  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36dd84a43cSPoul-Henning Kamp  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37dd84a43cSPoul-Henning Kamp  * SUCH DAMAGE.
38dd84a43cSPoul-Henning Kamp  */
39dd84a43cSPoul-Henning Kamp 
4050b1faefSDavid E. O'Brien #include <sys/cdefs.h>
4150b1faefSDavid E. O'Brien __FBSDID("$FreeBSD$");
42dd84a43cSPoul-Henning Kamp 
43dd84a43cSPoul-Henning Kamp #include <sys/param.h>
44dd84a43cSPoul-Henning Kamp #include <sys/systm.h>
45dd84a43cSPoul-Henning Kamp #include <sys/kernel.h>
46dd84a43cSPoul-Henning Kamp #include <sys/malloc.h>
47dd84a43cSPoul-Henning Kamp #include <sys/bio.h>
4849dbb61dSRobert Watson #include <sys/ktr.h>
4951460da8SJohn Baldwin #include <sys/proc.h>
503b378147SPawel Jakub Dawidek #include <sys/stack.h>
51ee75e7deSKonstantin Belousov #include <sys/sysctl.h>
525f518366SJeff Roberson #include <sys/vmem.h>
53dd84a43cSPoul-Henning Kamp 
54dd84a43cSPoul-Henning Kamp #include <sys/errno.h>
55dd84a43cSPoul-Henning Kamp #include <geom/geom.h>
56b1876192SPoul-Henning Kamp #include <geom/geom_int.h>
57e24cbd90SPoul-Henning Kamp #include <sys/devicestat.h>
58dd84a43cSPoul-Henning Kamp 
595ffb2c8bSPoul-Henning Kamp #include <vm/uma.h>
60ee75e7deSKonstantin Belousov #include <vm/vm.h>
61ee75e7deSKonstantin Belousov #include <vm/vm_param.h>
62ee75e7deSKonstantin Belousov #include <vm/vm_kern.h>
63ee75e7deSKonstantin Belousov #include <vm/vm_page.h>
64ee75e7deSKonstantin Belousov #include <vm/vm_object.h>
65ee75e7deSKonstantin Belousov #include <vm/vm_extern.h>
66ee75e7deSKonstantin Belousov #include <vm/vm_map.h>
675ffb2c8bSPoul-Henning Kamp 
6840ea77a0SAlexander Motin static int	g_io_transient_map_bio(struct bio *bp);
6940ea77a0SAlexander Motin 
70dd84a43cSPoul-Henning Kamp static struct g_bioq g_bio_run_down;
71dd84a43cSPoul-Henning Kamp static struct g_bioq g_bio_run_up;
725fcf4e43SPoul-Henning Kamp static struct g_bioq g_bio_run_task;
73dd84a43cSPoul-Henning Kamp 
743f2e5b85SWarner Losh /*
753f2e5b85SWarner Losh  * Pace is a hint that we've had some trouble recently allocating
763f2e5b85SWarner Losh  * bios, so we should back off trying to send I/O down the stack
773f2e5b85SWarner Losh  * a bit to let the problem resolve. When pacing, we also turn
783f2e5b85SWarner Losh  * off direct dispatch to also reduce memory pressure from I/Os
793f2e5b85SWarner Losh  * there, at the expxense of some added latency while the memory
803f2e5b85SWarner Losh  * pressures exist. See g_io_schedule_down() for more details
813f2e5b85SWarner Losh  * and limitations.
823f2e5b85SWarner Losh  */
833f2e5b85SWarner Losh static volatile u_int pace;
843f2e5b85SWarner Losh 
855ffb2c8bSPoul-Henning Kamp static uma_zone_t	biozone;
863432e4fdSPoul-Henning Kamp 
876231f75bSLuigi Rizzo /*
886231f75bSLuigi Rizzo  * The head of the list of classifiers used in g_io_request.
896231f75bSLuigi Rizzo  * Use g_register_classifier() and g_unregister_classifier()
906231f75bSLuigi Rizzo  * to add/remove entries to the list.
916231f75bSLuigi Rizzo  * Classifiers are invoked in registration order.
926231f75bSLuigi Rizzo  */
936231f75bSLuigi Rizzo static TAILQ_HEAD(g_classifier_tailq, g_classifier_hook)
946231f75bSLuigi Rizzo     g_classifier_tailq = TAILQ_HEAD_INITIALIZER(g_classifier_tailq);
956231f75bSLuigi Rizzo 
96dd84a43cSPoul-Henning Kamp #include <machine/atomic.h>
97dd84a43cSPoul-Henning Kamp 
98dd84a43cSPoul-Henning Kamp static void
99dd84a43cSPoul-Henning Kamp g_bioq_lock(struct g_bioq *bq)
100dd84a43cSPoul-Henning Kamp {
101dd84a43cSPoul-Henning Kamp 
102dd84a43cSPoul-Henning Kamp 	mtx_lock(&bq->bio_queue_lock);
103dd84a43cSPoul-Henning Kamp }
104dd84a43cSPoul-Henning Kamp 
105dd84a43cSPoul-Henning Kamp static void
106dd84a43cSPoul-Henning Kamp g_bioq_unlock(struct g_bioq *bq)
107dd84a43cSPoul-Henning Kamp {
108dd84a43cSPoul-Henning Kamp 
109dd84a43cSPoul-Henning Kamp 	mtx_unlock(&bq->bio_queue_lock);
110dd84a43cSPoul-Henning Kamp }
111dd84a43cSPoul-Henning Kamp 
112dd84a43cSPoul-Henning Kamp #if 0
113dd84a43cSPoul-Henning Kamp static void
114dd84a43cSPoul-Henning Kamp g_bioq_destroy(struct g_bioq *bq)
115dd84a43cSPoul-Henning Kamp {
116dd84a43cSPoul-Henning Kamp 
117dd84a43cSPoul-Henning Kamp 	mtx_destroy(&bq->bio_queue_lock);
118dd84a43cSPoul-Henning Kamp }
119dd84a43cSPoul-Henning Kamp #endif
120dd84a43cSPoul-Henning Kamp 
121dd84a43cSPoul-Henning Kamp static void
122dd84a43cSPoul-Henning Kamp g_bioq_init(struct g_bioq *bq)
123dd84a43cSPoul-Henning Kamp {
124dd84a43cSPoul-Henning Kamp 
125dd84a43cSPoul-Henning Kamp 	TAILQ_INIT(&bq->bio_queue);
1266008862bSJohn Baldwin 	mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF);
127dd84a43cSPoul-Henning Kamp }
128dd84a43cSPoul-Henning Kamp 
129dd84a43cSPoul-Henning Kamp static struct bio *
130dd84a43cSPoul-Henning Kamp g_bioq_first(struct g_bioq *bq)
131dd84a43cSPoul-Henning Kamp {
132dd84a43cSPoul-Henning Kamp 	struct bio *bp;
133dd84a43cSPoul-Henning Kamp 
134dd84a43cSPoul-Henning Kamp 	bp = TAILQ_FIRST(&bq->bio_queue);
135dd84a43cSPoul-Henning Kamp 	if (bp != NULL) {
136dcbd0fe5SPoul-Henning Kamp 		KASSERT((bp->bio_flags & BIO_ONQUEUE),
137dcbd0fe5SPoul-Henning Kamp 		    ("Bio not on queue bp=%p target %p", bp, bq));
138dcbd0fe5SPoul-Henning Kamp 		bp->bio_flags &= ~BIO_ONQUEUE;
139dd84a43cSPoul-Henning Kamp 		TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
140dd84a43cSPoul-Henning Kamp 		bq->bio_queue_length--;
141dd84a43cSPoul-Henning Kamp 	}
142dd84a43cSPoul-Henning Kamp 	return (bp);
143dd84a43cSPoul-Henning Kamp }
144dd84a43cSPoul-Henning Kamp 
145dd84a43cSPoul-Henning Kamp struct bio *
146dd84a43cSPoul-Henning Kamp g_new_bio(void)
147dd84a43cSPoul-Henning Kamp {
148dd84a43cSPoul-Henning Kamp 	struct bio *bp;
149dd84a43cSPoul-Henning Kamp 
1505ffb2c8bSPoul-Henning Kamp 	bp = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
1513b378147SPawel Jakub Dawidek #ifdef KTR
152b656c1b8SPawel Jakub Dawidek 	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
1533b378147SPawel Jakub Dawidek 		struct stack st;
1543b378147SPawel Jakub Dawidek 
1553b378147SPawel Jakub Dawidek 		CTR1(KTR_GEOM, "g_new_bio(): %p", bp);
1563b378147SPawel Jakub Dawidek 		stack_save(&st);
1573b378147SPawel Jakub Dawidek 		CTRSTACK(KTR_GEOM, &st, 3, 0);
1583b378147SPawel Jakub Dawidek 	}
1593b378147SPawel Jakub Dawidek #endif
160dd84a43cSPoul-Henning Kamp 	return (bp);
161dd84a43cSPoul-Henning Kamp }
162dd84a43cSPoul-Henning Kamp 
163a2033c96SPoul-Henning Kamp struct bio *
164a2033c96SPoul-Henning Kamp g_alloc_bio(void)
165a2033c96SPoul-Henning Kamp {
166a2033c96SPoul-Henning Kamp 	struct bio *bp;
167a2033c96SPoul-Henning Kamp 
168a2033c96SPoul-Henning Kamp 	bp = uma_zalloc(biozone, M_WAITOK | M_ZERO);
1693b378147SPawel Jakub Dawidek #ifdef KTR
170b656c1b8SPawel Jakub Dawidek 	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
1713b378147SPawel Jakub Dawidek 		struct stack st;
1723b378147SPawel Jakub Dawidek 
1733b378147SPawel Jakub Dawidek 		CTR1(KTR_GEOM, "g_alloc_bio(): %p", bp);
1743b378147SPawel Jakub Dawidek 		stack_save(&st);
1753b378147SPawel Jakub Dawidek 		CTRSTACK(KTR_GEOM, &st, 3, 0);
1763b378147SPawel Jakub Dawidek 	}
1773b378147SPawel Jakub Dawidek #endif
178a2033c96SPoul-Henning Kamp 	return (bp);
179a2033c96SPoul-Henning Kamp }
180a2033c96SPoul-Henning Kamp 
181dd84a43cSPoul-Henning Kamp void
182dd84a43cSPoul-Henning Kamp g_destroy_bio(struct bio *bp)
183dd84a43cSPoul-Henning Kamp {
1843b378147SPawel Jakub Dawidek #ifdef KTR
185b656c1b8SPawel Jakub Dawidek 	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
1863b378147SPawel Jakub Dawidek 		struct stack st;
187dd84a43cSPoul-Henning Kamp 
1883b378147SPawel Jakub Dawidek 		CTR1(KTR_GEOM, "g_destroy_bio(): %p", bp);
1893b378147SPawel Jakub Dawidek 		stack_save(&st);
1903b378147SPawel Jakub Dawidek 		CTRSTACK(KTR_GEOM, &st, 3, 0);
1913b378147SPawel Jakub Dawidek 	}
1923b378147SPawel Jakub Dawidek #endif
1935ffb2c8bSPoul-Henning Kamp 	uma_zfree(biozone, bp);
194dd84a43cSPoul-Henning Kamp }
195dd84a43cSPoul-Henning Kamp 
196dd84a43cSPoul-Henning Kamp struct bio *
197dd84a43cSPoul-Henning Kamp g_clone_bio(struct bio *bp)
198dd84a43cSPoul-Henning Kamp {
199dd84a43cSPoul-Henning Kamp 	struct bio *bp2;
200dd84a43cSPoul-Henning Kamp 
2015ffb2c8bSPoul-Henning Kamp 	bp2 = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
202a1bd3ee2SPoul-Henning Kamp 	if (bp2 != NULL) {
203936cc461SPoul-Henning Kamp 		bp2->bio_parent = bp;
204dd84a43cSPoul-Henning Kamp 		bp2->bio_cmd = bp->bio_cmd;
20582a6ae10SJim Harris 		/*
20682a6ae10SJim Harris 		 *  BIO_ORDERED flag may be used by disk drivers to enforce
20782a6ae10SJim Harris 		 *  ordering restrictions, so this flag needs to be cloned.
208a9934668SKenneth D. Merry 		 *  BIO_UNMAPPED and BIO_VLIST should be inherited, to properly
209a9934668SKenneth D. Merry 		 *  indicate which way the buffer is passed.
21082a6ae10SJim Harris 		 *  Other bio flags are not suitable for cloning.
21182a6ae10SJim Harris 		 */
212a9934668SKenneth D. Merry 		bp2->bio_flags = bp->bio_flags &
213a9934668SKenneth D. Merry 		    (BIO_ORDERED | BIO_UNMAPPED | BIO_VLIST);
214dd84a43cSPoul-Henning Kamp 		bp2->bio_length = bp->bio_length;
215dd84a43cSPoul-Henning Kamp 		bp2->bio_offset = bp->bio_offset;
216dd84a43cSPoul-Henning Kamp 		bp2->bio_data = bp->bio_data;
217ee75e7deSKonstantin Belousov 		bp2->bio_ma = bp->bio_ma;
218ee75e7deSKonstantin Belousov 		bp2->bio_ma_n = bp->bio_ma_n;
219ee75e7deSKonstantin Belousov 		bp2->bio_ma_offset = bp->bio_ma_offset;
220dd84a43cSPoul-Henning Kamp 		bp2->bio_attribute = bp->bio_attribute;
221*9a6844d5SKenneth D. Merry 		if (bp->bio_cmd == BIO_ZONE)
222*9a6844d5SKenneth D. Merry 			bcopy(&bp->bio_zone, &bp2->bio_zone,
223*9a6844d5SKenneth D. Merry 			    sizeof(bp->bio_zone));
2246231f75bSLuigi Rizzo 		/* Inherit classification info from the parent */
2256231f75bSLuigi Rizzo 		bp2->bio_classifier1 = bp->bio_classifier1;
2266231f75bSLuigi Rizzo 		bp2->bio_classifier2 = bp->bio_classifier2;
227801bb689SPoul-Henning Kamp 		bp->bio_children++;
228a1bd3ee2SPoul-Henning Kamp 	}
2293b378147SPawel Jakub Dawidek #ifdef KTR
230b656c1b8SPawel Jakub Dawidek 	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
2313b378147SPawel Jakub Dawidek 		struct stack st;
2323b378147SPawel Jakub Dawidek 
233ad572235SRuslan Ermilov 		CTR2(KTR_GEOM, "g_clone_bio(%p): %p", bp, bp2);
2343b378147SPawel Jakub Dawidek 		stack_save(&st);
2353b378147SPawel Jakub Dawidek 		CTRSTACK(KTR_GEOM, &st, 3, 0);
2363b378147SPawel Jakub Dawidek 	}
2373b378147SPawel Jakub Dawidek #endif
238dd84a43cSPoul-Henning Kamp 	return(bp2);
239dd84a43cSPoul-Henning Kamp }
240dd84a43cSPoul-Henning Kamp 
2414bec0ff1SPawel Jakub Dawidek struct bio *
2424bec0ff1SPawel Jakub Dawidek g_duplicate_bio(struct bio *bp)
2434bec0ff1SPawel Jakub Dawidek {
2444bec0ff1SPawel Jakub Dawidek 	struct bio *bp2;
2454bec0ff1SPawel Jakub Dawidek 
2464bec0ff1SPawel Jakub Dawidek 	bp2 = uma_zalloc(biozone, M_WAITOK | M_ZERO);
247a9934668SKenneth D. Merry 	bp2->bio_flags = bp->bio_flags & (BIO_UNMAPPED | BIO_VLIST);
2484bec0ff1SPawel Jakub Dawidek 	bp2->bio_parent = bp;
2494bec0ff1SPawel Jakub Dawidek 	bp2->bio_cmd = bp->bio_cmd;
2504bec0ff1SPawel Jakub Dawidek 	bp2->bio_length = bp->bio_length;
2514bec0ff1SPawel Jakub Dawidek 	bp2->bio_offset = bp->bio_offset;
2524bec0ff1SPawel Jakub Dawidek 	bp2->bio_data = bp->bio_data;
253ee75e7deSKonstantin Belousov 	bp2->bio_ma = bp->bio_ma;
254ee75e7deSKonstantin Belousov 	bp2->bio_ma_n = bp->bio_ma_n;
255ee75e7deSKonstantin Belousov 	bp2->bio_ma_offset = bp->bio_ma_offset;
2564bec0ff1SPawel Jakub Dawidek 	bp2->bio_attribute = bp->bio_attribute;
2574bec0ff1SPawel Jakub Dawidek 	bp->bio_children++;
2584bec0ff1SPawel Jakub Dawidek #ifdef KTR
259b656c1b8SPawel Jakub Dawidek 	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
2604bec0ff1SPawel Jakub Dawidek 		struct stack st;
2614bec0ff1SPawel Jakub Dawidek 
2624bec0ff1SPawel Jakub Dawidek 		CTR2(KTR_GEOM, "g_duplicate_bio(%p): %p", bp, bp2);
2634bec0ff1SPawel Jakub Dawidek 		stack_save(&st);
2644bec0ff1SPawel Jakub Dawidek 		CTRSTACK(KTR_GEOM, &st, 3, 0);
2654bec0ff1SPawel Jakub Dawidek 	}
2664bec0ff1SPawel Jakub Dawidek #endif
2674bec0ff1SPawel Jakub Dawidek 	return(bp2);
2684bec0ff1SPawel Jakub Dawidek }
2694bec0ff1SPawel Jakub Dawidek 
270dd84a43cSPoul-Henning Kamp void
271c55f5707SWarner Losh g_reset_bio(struct bio *bp)
272c55f5707SWarner Losh {
273c55f5707SWarner Losh 
274bd4c1dd6SWarner Losh 	bzero(bp, sizeof(*bp));
275c55f5707SWarner Losh }
276c55f5707SWarner Losh 
277c55f5707SWarner Losh void
278dd84a43cSPoul-Henning Kamp g_io_init()
279dd84a43cSPoul-Henning Kamp {
280dd84a43cSPoul-Henning Kamp 
281dd84a43cSPoul-Henning Kamp 	g_bioq_init(&g_bio_run_down);
282dd84a43cSPoul-Henning Kamp 	g_bioq_init(&g_bio_run_up);
2835fcf4e43SPoul-Henning Kamp 	g_bioq_init(&g_bio_run_task);
2845ffb2c8bSPoul-Henning Kamp 	biozone = uma_zcreate("g_bio", sizeof (struct bio),
2855ffb2c8bSPoul-Henning Kamp 	    NULL, NULL,
2865ffb2c8bSPoul-Henning Kamp 	    NULL, NULL,
2875ffb2c8bSPoul-Henning Kamp 	    0, 0);
288dd84a43cSPoul-Henning Kamp }
289dd84a43cSPoul-Henning Kamp 
290dd84a43cSPoul-Henning Kamp int
2910d3f37a8SPoul-Henning Kamp g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr)
292dd84a43cSPoul-Henning Kamp {
293dd84a43cSPoul-Henning Kamp 	struct bio *bp;
294dd84a43cSPoul-Henning Kamp 	int error;
295dd84a43cSPoul-Henning Kamp 
296dd84a43cSPoul-Henning Kamp 	g_trace(G_T_BIO, "bio_getattr(%s)", attr);
297a2033c96SPoul-Henning Kamp 	bp = g_alloc_bio();
298dd84a43cSPoul-Henning Kamp 	bp->bio_cmd = BIO_GETATTR;
299dd84a43cSPoul-Henning Kamp 	bp->bio_done = NULL;
300dd84a43cSPoul-Henning Kamp 	bp->bio_attribute = attr;
301dd84a43cSPoul-Henning Kamp 	bp->bio_length = *len;
302dd84a43cSPoul-Henning Kamp 	bp->bio_data = ptr;
303dd84a43cSPoul-Henning Kamp 	g_io_request(bp, cp);
30453706245SPoul-Henning Kamp 	error = biowait(bp, "ggetattr");
305dd84a43cSPoul-Henning Kamp 	*len = bp->bio_completed;
306dd84a43cSPoul-Henning Kamp 	g_destroy_bio(bp);
307dd84a43cSPoul-Henning Kamp 	return (error);
308dd84a43cSPoul-Henning Kamp }
309dd84a43cSPoul-Henning Kamp 
310c3618c65SPawel Jakub Dawidek int
311*9a6844d5SKenneth D. Merry g_io_zonecmd(struct disk_zone_args *zone_args, struct g_consumer *cp)
312*9a6844d5SKenneth D. Merry {
313*9a6844d5SKenneth D. Merry 	struct bio *bp;
314*9a6844d5SKenneth D. Merry 	int error;
315*9a6844d5SKenneth D. Merry 
316*9a6844d5SKenneth D. Merry 	g_trace(G_T_BIO, "bio_zone(%d)", zone_args->zone_cmd);
317*9a6844d5SKenneth D. Merry 	bp = g_alloc_bio();
318*9a6844d5SKenneth D. Merry 	bp->bio_cmd = BIO_ZONE;
319*9a6844d5SKenneth D. Merry 	bp->bio_done = NULL;
320*9a6844d5SKenneth D. Merry 	/*
321*9a6844d5SKenneth D. Merry 	 * XXX KDM need to handle report zone data.
322*9a6844d5SKenneth D. Merry 	 */
323*9a6844d5SKenneth D. Merry 	bcopy(zone_args, &bp->bio_zone, sizeof(*zone_args));
324*9a6844d5SKenneth D. Merry 	if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES)
325*9a6844d5SKenneth D. Merry 		bp->bio_length =
326*9a6844d5SKenneth D. Merry 		    zone_args->zone_params.report.entries_allocated *
327*9a6844d5SKenneth D. Merry 		    sizeof(struct disk_zone_rep_entry);
328*9a6844d5SKenneth D. Merry 	else
329*9a6844d5SKenneth D. Merry 		bp->bio_length = 0;
330*9a6844d5SKenneth D. Merry 
331*9a6844d5SKenneth D. Merry 	g_io_request(bp, cp);
332*9a6844d5SKenneth D. Merry 	error = biowait(bp, "gzone");
333*9a6844d5SKenneth D. Merry 	bcopy(&bp->bio_zone, zone_args, sizeof(*zone_args));
334*9a6844d5SKenneth D. Merry 	g_destroy_bio(bp);
335*9a6844d5SKenneth D. Merry 	return (error);
336*9a6844d5SKenneth D. Merry }
337*9a6844d5SKenneth D. Merry 
338*9a6844d5SKenneth D. Merry int
339c3618c65SPawel Jakub Dawidek g_io_flush(struct g_consumer *cp)
340c3618c65SPawel Jakub Dawidek {
341c3618c65SPawel Jakub Dawidek 	struct bio *bp;
342c3618c65SPawel Jakub Dawidek 	int error;
343c3618c65SPawel Jakub Dawidek 
344c3618c65SPawel Jakub Dawidek 	g_trace(G_T_BIO, "bio_flush(%s)", cp->provider->name);
345c3618c65SPawel Jakub Dawidek 	bp = g_alloc_bio();
346c3618c65SPawel Jakub Dawidek 	bp->bio_cmd = BIO_FLUSH;
347f03f7a0cSJustin T. Gibbs 	bp->bio_flags |= BIO_ORDERED;
348c3618c65SPawel Jakub Dawidek 	bp->bio_done = NULL;
349c3618c65SPawel Jakub Dawidek 	bp->bio_attribute = NULL;
350c3618c65SPawel Jakub Dawidek 	bp->bio_offset = cp->provider->mediasize;
351c3618c65SPawel Jakub Dawidek 	bp->bio_length = 0;
352c3618c65SPawel Jakub Dawidek 	bp->bio_data = NULL;
353c3618c65SPawel Jakub Dawidek 	g_io_request(bp, cp);
354c3618c65SPawel Jakub Dawidek 	error = biowait(bp, "gflush");
355c3618c65SPawel Jakub Dawidek 	g_destroy_bio(bp);
356c3618c65SPawel Jakub Dawidek 	return (error);
357c3618c65SPawel Jakub Dawidek }
358c3618c65SPawel Jakub Dawidek 
359e39d70d4SPoul-Henning Kamp static int
360e39d70d4SPoul-Henning Kamp g_io_check(struct bio *bp)
361e39d70d4SPoul-Henning Kamp {
362e39d70d4SPoul-Henning Kamp 	struct g_consumer *cp;
363e39d70d4SPoul-Henning Kamp 	struct g_provider *pp;
36440ea77a0SAlexander Motin 	off_t excess;
36540ea77a0SAlexander Motin 	int error;
366e39d70d4SPoul-Henning Kamp 
367e39d70d4SPoul-Henning Kamp 	cp = bp->bio_from;
368e39d70d4SPoul-Henning Kamp 	pp = bp->bio_to;
369e39d70d4SPoul-Henning Kamp 
370e39d70d4SPoul-Henning Kamp 	/* Fail if access counters dont allow the operation */
371e39d70d4SPoul-Henning Kamp 	switch(bp->bio_cmd) {
372e39d70d4SPoul-Henning Kamp 	case BIO_READ:
373e39d70d4SPoul-Henning Kamp 	case BIO_GETATTR:
374e39d70d4SPoul-Henning Kamp 		if (cp->acr == 0)
375e39d70d4SPoul-Henning Kamp 			return (EPERM);
376e39d70d4SPoul-Henning Kamp 		break;
377e39d70d4SPoul-Henning Kamp 	case BIO_WRITE:
378e39d70d4SPoul-Henning Kamp 	case BIO_DELETE:
379c3618c65SPawel Jakub Dawidek 	case BIO_FLUSH:
380e39d70d4SPoul-Henning Kamp 		if (cp->acw == 0)
381e39d70d4SPoul-Henning Kamp 			return (EPERM);
382e39d70d4SPoul-Henning Kamp 		break;
383*9a6844d5SKenneth D. Merry 	case BIO_ZONE:
384*9a6844d5SKenneth D. Merry 		if ((bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES) ||
385*9a6844d5SKenneth D. Merry 		    (bp->bio_zone.zone_cmd == DISK_ZONE_GET_PARAMS)) {
386*9a6844d5SKenneth D. Merry 			if (cp->acr == 0)
387*9a6844d5SKenneth D. Merry 				return (EPERM);
388*9a6844d5SKenneth D. Merry 		} else if (cp->acw == 0)
389*9a6844d5SKenneth D. Merry 			return (EPERM);
390*9a6844d5SKenneth D. Merry 		break;
391e39d70d4SPoul-Henning Kamp 	default:
392e39d70d4SPoul-Henning Kamp 		return (EPERM);
393e39d70d4SPoul-Henning Kamp 	}
394e39d70d4SPoul-Henning Kamp 	/* if provider is marked for error, don't disturb. */
395e39d70d4SPoul-Henning Kamp 	if (pp->error)
396e39d70d4SPoul-Henning Kamp 		return (pp->error);
3973631c638SAlexander Motin 	if (cp->flags & G_CF_ORPHAN)
3983631c638SAlexander Motin 		return (ENXIO);
399e39d70d4SPoul-Henning Kamp 
400e39d70d4SPoul-Henning Kamp 	switch(bp->bio_cmd) {
401e39d70d4SPoul-Henning Kamp 	case BIO_READ:
402e39d70d4SPoul-Henning Kamp 	case BIO_WRITE:
403e39d70d4SPoul-Henning Kamp 	case BIO_DELETE:
4042a842317SAndriy Gapon 		/* Zero sectorsize or mediasize is probably a lack of media. */
4052a842317SAndriy Gapon 		if (pp->sectorsize == 0 || pp->mediasize == 0)
40643bff1a7SPoul-Henning Kamp 			return (ENXIO);
407e39d70d4SPoul-Henning Kamp 		/* Reject I/O not on sector boundary */
408e39d70d4SPoul-Henning Kamp 		if (bp->bio_offset % pp->sectorsize)
409e39d70d4SPoul-Henning Kamp 			return (EINVAL);
410e39d70d4SPoul-Henning Kamp 		/* Reject I/O not integral sector long */
411e39d70d4SPoul-Henning Kamp 		if (bp->bio_length % pp->sectorsize)
412e39d70d4SPoul-Henning Kamp 			return (EINVAL);
413d1b8bf47SPoul-Henning Kamp 		/* Reject requests before or past the end of media. */
414d1b8bf47SPoul-Henning Kamp 		if (bp->bio_offset < 0)
415d1b8bf47SPoul-Henning Kamp 			return (EIO);
416e39d70d4SPoul-Henning Kamp 		if (bp->bio_offset > pp->mediasize)
417e39d70d4SPoul-Henning Kamp 			return (EIO);
41840ea77a0SAlexander Motin 
41940ea77a0SAlexander Motin 		/* Truncate requests to the end of providers media. */
42040ea77a0SAlexander Motin 		excess = bp->bio_offset + bp->bio_length;
42140ea77a0SAlexander Motin 		if (excess > bp->bio_to->mediasize) {
42240ea77a0SAlexander Motin 			KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
42340ea77a0SAlexander Motin 			    round_page(bp->bio_ma_offset +
42440ea77a0SAlexander Motin 			    bp->bio_length) / PAGE_SIZE == bp->bio_ma_n,
42540ea77a0SAlexander Motin 			    ("excess bio %p too short", bp));
42640ea77a0SAlexander Motin 			excess -= bp->bio_to->mediasize;
42740ea77a0SAlexander Motin 			bp->bio_length -= excess;
42840ea77a0SAlexander Motin 			if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
42940ea77a0SAlexander Motin 				bp->bio_ma_n = round_page(bp->bio_ma_offset +
43040ea77a0SAlexander Motin 				    bp->bio_length) / PAGE_SIZE;
43140ea77a0SAlexander Motin 			}
43240ea77a0SAlexander Motin 			if (excess > 0)
43340ea77a0SAlexander Motin 				CTR3(KTR_GEOM, "g_down truncated bio "
43440ea77a0SAlexander Motin 				    "%p provider %s by %d", bp,
43540ea77a0SAlexander Motin 				    bp->bio_to->name, excess);
43640ea77a0SAlexander Motin 		}
43740ea77a0SAlexander Motin 
43840ea77a0SAlexander Motin 		/* Deliver zero length transfers right here. */
43940ea77a0SAlexander Motin 		if (bp->bio_length == 0) {
44040ea77a0SAlexander Motin 			CTR2(KTR_GEOM, "g_down terminated 0-length "
44140ea77a0SAlexander Motin 			    "bp %p provider %s", bp, bp->bio_to->name);
44240ea77a0SAlexander Motin 			return (0);
44340ea77a0SAlexander Motin 		}
44440ea77a0SAlexander Motin 
44540ea77a0SAlexander Motin 		if ((bp->bio_flags & BIO_UNMAPPED) != 0 &&
44640ea77a0SAlexander Motin 		    (bp->bio_to->flags & G_PF_ACCEPT_UNMAPPED) == 0 &&
44740ea77a0SAlexander Motin 		    (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
44840ea77a0SAlexander Motin 			if ((error = g_io_transient_map_bio(bp)) >= 0)
44940ea77a0SAlexander Motin 				return (error);
45040ea77a0SAlexander Motin 		}
451e39d70d4SPoul-Henning Kamp 		break;
452e39d70d4SPoul-Henning Kamp 	default:
453e39d70d4SPoul-Henning Kamp 		break;
454e39d70d4SPoul-Henning Kamp 	}
45540ea77a0SAlexander Motin 	return (EJUSTRETURN);
456e39d70d4SPoul-Henning Kamp }
457e39d70d4SPoul-Henning Kamp 
4586231f75bSLuigi Rizzo /*
4596231f75bSLuigi Rizzo  * bio classification support.
4606231f75bSLuigi Rizzo  *
4616231f75bSLuigi Rizzo  * g_register_classifier() and g_unregister_classifier()
4626231f75bSLuigi Rizzo  * are used to add/remove a classifier from the list.
4636231f75bSLuigi Rizzo  * The list is protected using the g_bio_run_down lock,
4646231f75bSLuigi Rizzo  * because the classifiers are called in this path.
4656231f75bSLuigi Rizzo  *
4666231f75bSLuigi Rizzo  * g_io_request() passes bio's that are not already classified
4676231f75bSLuigi Rizzo  * (i.e. those with bio_classifier1 == NULL) to g_run_classifiers().
4686231f75bSLuigi Rizzo  * Classifiers can store their result in the two fields
4696231f75bSLuigi Rizzo  * bio_classifier1 and bio_classifier2.
4706231f75bSLuigi Rizzo  * A classifier that updates one of the fields should
4716231f75bSLuigi Rizzo  * return a non-zero value.
4726231f75bSLuigi Rizzo  * If no classifier updates the field, g_run_classifiers() sets
4736231f75bSLuigi Rizzo  * bio_classifier1 = BIO_NOTCLASSIFIED to avoid further calls.
4746231f75bSLuigi Rizzo  */
4756231f75bSLuigi Rizzo 
4766231f75bSLuigi Rizzo int
4776231f75bSLuigi Rizzo g_register_classifier(struct g_classifier_hook *hook)
4786231f75bSLuigi Rizzo {
4796231f75bSLuigi Rizzo 
4806231f75bSLuigi Rizzo 	g_bioq_lock(&g_bio_run_down);
4816231f75bSLuigi Rizzo 	TAILQ_INSERT_TAIL(&g_classifier_tailq, hook, link);
4826231f75bSLuigi Rizzo 	g_bioq_unlock(&g_bio_run_down);
4836231f75bSLuigi Rizzo 
4846231f75bSLuigi Rizzo 	return (0);
4856231f75bSLuigi Rizzo }
4866231f75bSLuigi Rizzo 
4876231f75bSLuigi Rizzo void
4886231f75bSLuigi Rizzo g_unregister_classifier(struct g_classifier_hook *hook)
4896231f75bSLuigi Rizzo {
4906231f75bSLuigi Rizzo 	struct g_classifier_hook *entry;
4916231f75bSLuigi Rizzo 
4926231f75bSLuigi Rizzo 	g_bioq_lock(&g_bio_run_down);
4936231f75bSLuigi Rizzo 	TAILQ_FOREACH(entry, &g_classifier_tailq, link) {
4946231f75bSLuigi Rizzo 		if (entry == hook) {
4956231f75bSLuigi Rizzo 			TAILQ_REMOVE(&g_classifier_tailq, hook, link);
4966231f75bSLuigi Rizzo 			break;
4976231f75bSLuigi Rizzo 		}
4986231f75bSLuigi Rizzo 	}
4996231f75bSLuigi Rizzo 	g_bioq_unlock(&g_bio_run_down);
5006231f75bSLuigi Rizzo }
5016231f75bSLuigi Rizzo 
5026231f75bSLuigi Rizzo static void
5036231f75bSLuigi Rizzo g_run_classifiers(struct bio *bp)
5046231f75bSLuigi Rizzo {
5056231f75bSLuigi Rizzo 	struct g_classifier_hook *hook;
5066231f75bSLuigi Rizzo 	int classified = 0;
5076231f75bSLuigi Rizzo 
5086231f75bSLuigi Rizzo 	TAILQ_FOREACH(hook, &g_classifier_tailq, link)
5096231f75bSLuigi Rizzo 		classified |= hook->func(hook->arg, bp);
5106231f75bSLuigi Rizzo 
5116231f75bSLuigi Rizzo 	if (!classified)
5126231f75bSLuigi Rizzo 		bp->bio_classifier1 = BIO_NOTCLASSIFIED;
5136231f75bSLuigi Rizzo }
5146231f75bSLuigi Rizzo 
515dd84a43cSPoul-Henning Kamp void
516dd84a43cSPoul-Henning Kamp g_io_request(struct bio *bp, struct g_consumer *cp)
517dd84a43cSPoul-Henning Kamp {
518801bb689SPoul-Henning Kamp 	struct g_provider *pp;
51940ea77a0SAlexander Motin 	struct mtx *mtxp;
52040ea77a0SAlexander Motin 	int direct, error, first;
5218076d204SWarner Losh 	uint8_t cmd;
522dd84a43cSPoul-Henning Kamp 
523d0e17c1bSPoul-Henning Kamp 	KASSERT(cp != NULL, ("NULL cp in g_io_request"));
524d0e17c1bSPoul-Henning Kamp 	KASSERT(bp != NULL, ("NULL bp in g_io_request"));
525e060b6bdSPoul-Henning Kamp 	pp = cp->provider;
526801bb689SPoul-Henning Kamp 	KASSERT(pp != NULL, ("consumer not attached in g_io_request"));
52792ee312dSPawel Jakub Dawidek #ifdef DIAGNOSTIC
52892ee312dSPawel Jakub Dawidek 	KASSERT(bp->bio_driver1 == NULL,
52992ee312dSPawel Jakub Dawidek 	    ("bio_driver1 used by the consumer (geom %s)", cp->geom->name));
53092ee312dSPawel Jakub Dawidek 	KASSERT(bp->bio_driver2 == NULL,
53192ee312dSPawel Jakub Dawidek 	    ("bio_driver2 used by the consumer (geom %s)", cp->geom->name));
53292ee312dSPawel Jakub Dawidek 	KASSERT(bp->bio_pflags == 0,
53392ee312dSPawel Jakub Dawidek 	    ("bio_pflags used by the consumer (geom %s)", cp->geom->name));
53492ee312dSPawel Jakub Dawidek 	/*
53592ee312dSPawel Jakub Dawidek 	 * Remember consumer's private fields, so we can detect if they were
53692ee312dSPawel Jakub Dawidek 	 * modified by the provider.
53792ee312dSPawel Jakub Dawidek 	 */
53892ee312dSPawel Jakub Dawidek 	bp->_bio_caller1 = bp->bio_caller1;
53992ee312dSPawel Jakub Dawidek 	bp->_bio_caller2 = bp->bio_caller2;
54092ee312dSPawel Jakub Dawidek 	bp->_bio_cflags = bp->bio_cflags;
54192ee312dSPawel Jakub Dawidek #endif
542801bb689SPoul-Henning Kamp 
5438076d204SWarner Losh 	cmd = bp->bio_cmd;
5448076d204SWarner Losh 	if (cmd == BIO_READ || cmd == BIO_WRITE || cmd == BIO_GETATTR) {
545c3618c65SPawel Jakub Dawidek 		KASSERT(bp->bio_data != NULL,
5469a8fa125SWarner Losh 		    ("NULL bp->data in g_io_request(cmd=%hu)", bp->bio_cmd));
5471ded77b2SPawel Jakub Dawidek 	}
5488076d204SWarner Losh 	if (cmd == BIO_DELETE || cmd == BIO_FLUSH) {
5491ded77b2SPawel Jakub Dawidek 		KASSERT(bp->bio_data == NULL,
5509a8fa125SWarner Losh 		    ("non-NULL bp->data in g_io_request(cmd=%hu)",
5511ded77b2SPawel Jakub Dawidek 		    bp->bio_cmd));
552c3618c65SPawel Jakub Dawidek 	}
5538076d204SWarner Losh 	if (cmd == BIO_READ || cmd == BIO_WRITE || cmd == BIO_DELETE) {
554dcbd0fe5SPoul-Henning Kamp 		KASSERT(bp->bio_offset % cp->provider->sectorsize == 0,
555dcbd0fe5SPoul-Henning Kamp 		    ("wrong offset %jd for sectorsize %u",
556dcbd0fe5SPoul-Henning Kamp 		    bp->bio_offset, cp->provider->sectorsize));
557dcbd0fe5SPoul-Henning Kamp 		KASSERT(bp->bio_length % cp->provider->sectorsize == 0,
558dcbd0fe5SPoul-Henning Kamp 		    ("wrong length %jd for sectorsize %u",
559dcbd0fe5SPoul-Henning Kamp 		    bp->bio_length, cp->provider->sectorsize));
560dcbd0fe5SPoul-Henning Kamp 	}
561dcbd0fe5SPoul-Henning Kamp 
562f7717523SStephan Uphoff 	g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
563f7717523SStephan Uphoff 	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd);
564f7717523SStephan Uphoff 
565dd84a43cSPoul-Henning Kamp 	bp->bio_from = cp;
566801bb689SPoul-Henning Kamp 	bp->bio_to = pp;
5672fccec19SPoul-Henning Kamp 	bp->bio_error = 0;
5682fccec19SPoul-Henning Kamp 	bp->bio_completed = 0;
569dd84a43cSPoul-Henning Kamp 
57019fa21aaSPoul-Henning Kamp 	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
57119fa21aaSPoul-Henning Kamp 	    ("Bio already on queue bp=%p", bp));
57240ea77a0SAlexander Motin 	if ((g_collectstats & G_STATS_CONSUMERS) != 0 ||
57340ea77a0SAlexander Motin 	    ((g_collectstats & G_STATS_PROVIDERS) != 0 && pp->stat != NULL))
57419fa21aaSPoul-Henning Kamp 		binuptime(&bp->bio_t0);
575a5be8eb5SAlexander Motin 	else
576a5be8eb5SAlexander Motin 		getbinuptime(&bp->bio_t0);
5778827c821SPoul-Henning Kamp 
57840ea77a0SAlexander Motin #ifdef GET_STACK_USAGE
579347e9d54SKonstantin Belousov 	direct = (cp->flags & G_CF_DIRECT_SEND) != 0 &&
580347e9d54SKonstantin Belousov 	    (pp->flags & G_PF_DIRECT_RECEIVE) != 0 &&
58140ea77a0SAlexander Motin 	    !g_is_geom_thread(curthread) &&
5829b349650SKonstantin Belousov 	    ((pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ||
5833f2e5b85SWarner Losh 	    (bp->bio_flags & BIO_UNMAPPED) == 0 || THREAD_CAN_SLEEP()) &&
5843f2e5b85SWarner Losh 	    pace == 0;
58540ea77a0SAlexander Motin 	if (direct) {
58640ea77a0SAlexander Motin 		/* Block direct execution if less then half of stack left. */
58740ea77a0SAlexander Motin 		size_t	st, su;
58840ea77a0SAlexander Motin 		GET_STACK_USAGE(st, su);
58940ea77a0SAlexander Motin 		if (su * 2 > st)
59040ea77a0SAlexander Motin 			direct = 0;
59140ea77a0SAlexander Motin 	}
59240ea77a0SAlexander Motin #else
59340ea77a0SAlexander Motin 	direct = 0;
59440ea77a0SAlexander Motin #endif
59540ea77a0SAlexander Motin 
59640ea77a0SAlexander Motin 	if (!TAILQ_EMPTY(&g_classifier_tailq) && !bp->bio_classifier1) {
59740ea77a0SAlexander Motin 		g_bioq_lock(&g_bio_run_down);
59840ea77a0SAlexander Motin 		g_run_classifiers(bp);
59940ea77a0SAlexander Motin 		g_bioq_unlock(&g_bio_run_down);
60040ea77a0SAlexander Motin 	}
60140ea77a0SAlexander Motin 
6028827c821SPoul-Henning Kamp 	/*
6038827c821SPoul-Henning Kamp 	 * The statistics collection is lockless, as such, but we
6048827c821SPoul-Henning Kamp 	 * can not update one instance of the statistics from more
6058827c821SPoul-Henning Kamp 	 * than one thread at a time, so grab the lock first.
6068827c821SPoul-Henning Kamp 	 */
60740ea77a0SAlexander Motin 	mtxp = mtx_pool_find(mtxpool_sleep, pp);
60840ea77a0SAlexander Motin 	mtx_lock(mtxp);
60940ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_PROVIDERS)
61019fa21aaSPoul-Henning Kamp 		devstat_start_transaction(pp->stat, &bp->bio_t0);
61140ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_CONSUMERS)
61219fa21aaSPoul-Henning Kamp 		devstat_start_transaction(cp->stat, &bp->bio_t0);
61319fa21aaSPoul-Henning Kamp 	pp->nstart++;
614cf457284SPoul-Henning Kamp 	cp->nstart++;
61540ea77a0SAlexander Motin 	mtx_unlock(mtxp);
61640ea77a0SAlexander Motin 
61740ea77a0SAlexander Motin 	if (direct) {
61840ea77a0SAlexander Motin 		error = g_io_check(bp);
61940ea77a0SAlexander Motin 		if (error >= 0) {
62040ea77a0SAlexander Motin 			CTR3(KTR_GEOM, "g_io_request g_io_check on bp %p "
62140ea77a0SAlexander Motin 			    "provider %s returned %d", bp, bp->bio_to->name,
62240ea77a0SAlexander Motin 			    error);
62340ea77a0SAlexander Motin 			g_io_deliver(bp, error);
62440ea77a0SAlexander Motin 			return;
62540ea77a0SAlexander Motin 		}
62640ea77a0SAlexander Motin 		bp->bio_to->geom->start(bp);
62740ea77a0SAlexander Motin 	} else {
62840ea77a0SAlexander Motin 		g_bioq_lock(&g_bio_run_down);
6290d883b11SAlexander Motin 		first = TAILQ_EMPTY(&g_bio_run_down.bio_queue);
63019fa21aaSPoul-Henning Kamp 		TAILQ_INSERT_TAIL(&g_bio_run_down.bio_queue, bp, bio_queue);
63140ea77a0SAlexander Motin 		bp->bio_flags |= BIO_ONQUEUE;
63219fa21aaSPoul-Henning Kamp 		g_bio_run_down.bio_queue_length++;
63319fa21aaSPoul-Henning Kamp 		g_bioq_unlock(&g_bio_run_down);
6342fccec19SPoul-Henning Kamp 		/* Pass it on down. */
6350d883b11SAlexander Motin 		if (first)
636dd84a43cSPoul-Henning Kamp 			wakeup(&g_wait_down);
637dd84a43cSPoul-Henning Kamp 	}
63840ea77a0SAlexander Motin }
639dd84a43cSPoul-Henning Kamp 
640dd84a43cSPoul-Henning Kamp void
64172840432SPoul-Henning Kamp g_io_deliver(struct bio *bp, int error)
642dd84a43cSPoul-Henning Kamp {
643e431d66cSAlexander Motin 	struct bintime now;
644801bb689SPoul-Henning Kamp 	struct g_consumer *cp;
645801bb689SPoul-Henning Kamp 	struct g_provider *pp;
64640ea77a0SAlexander Motin 	struct mtx *mtxp;
64740ea77a0SAlexander Motin 	int direct, first;
648dd84a43cSPoul-Henning Kamp 
649e060b6bdSPoul-Henning Kamp 	KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
650801bb689SPoul-Henning Kamp 	pp = bp->bio_to;
651f7eeab17SPoul-Henning Kamp 	KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver"));
652f7eeab17SPoul-Henning Kamp 	cp = bp->bio_from;
653f7eeab17SPoul-Henning Kamp 	if (cp == NULL) {
654f7eeab17SPoul-Henning Kamp 		bp->bio_error = error;
655f7eeab17SPoul-Henning Kamp 		bp->bio_done(bp);
656f7eeab17SPoul-Henning Kamp 		return;
657f7eeab17SPoul-Henning Kamp 	}
658801bb689SPoul-Henning Kamp 	KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver"));
659801bb689SPoul-Henning Kamp 	KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
660fb231f36SEdward Tomasz Napierala #ifdef DIAGNOSTIC
661fb231f36SEdward Tomasz Napierala 	/*
662fb231f36SEdward Tomasz Napierala 	 * Some classes - GJournal in particular - can modify bio's
663fb231f36SEdward Tomasz Napierala 	 * private fields while the bio is in transit; G_GEOM_VOLATILE_BIO
664fb231f36SEdward Tomasz Napierala 	 * flag means it's an expected behaviour for that particular geom.
665fb231f36SEdward Tomasz Napierala 	 */
666fb231f36SEdward Tomasz Napierala 	if ((cp->geom->flags & G_GEOM_VOLATILE_BIO) == 0) {
667fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_caller1 == bp->_bio_caller1,
668fb231f36SEdward Tomasz Napierala 		    ("bio_caller1 used by the provider %s", pp->name));
669fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_caller2 == bp->_bio_caller2,
670fb231f36SEdward Tomasz Napierala 		    ("bio_caller2 used by the provider %s", pp->name));
671fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_cflags == bp->_bio_cflags,
672fb231f36SEdward Tomasz Napierala 		    ("bio_cflags used by the provider %s", pp->name));
673fb231f36SEdward Tomasz Napierala 	}
674fb231f36SEdward Tomasz Napierala #endif
67546aeebecSPawel Jakub Dawidek 	KASSERT(bp->bio_completed >= 0, ("bio_completed can't be less than 0"));
67646aeebecSPawel Jakub Dawidek 	KASSERT(bp->bio_completed <= bp->bio_length,
67746aeebecSPawel Jakub Dawidek 	    ("bio_completed can't be greater than bio_length"));
6785ab413bfSPoul-Henning Kamp 
679dd84a43cSPoul-Henning Kamp 	g_trace(G_T_BIO,
6800355b86eSPoul-Henning Kamp "g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
681801bb689SPoul-Henning Kamp 	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error,
6820355b86eSPoul-Henning Kamp 	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
683801bb689SPoul-Henning Kamp 
68419fa21aaSPoul-Henning Kamp 	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
68519fa21aaSPoul-Henning Kamp 	    ("Bio already on queue bp=%p", bp));
68619fa21aaSPoul-Henning Kamp 
687dcbd0fe5SPoul-Henning Kamp 	/*
688dcbd0fe5SPoul-Henning Kamp 	 * XXX: next two doesn't belong here
689dcbd0fe5SPoul-Henning Kamp 	 */
690e24cbd90SPoul-Henning Kamp 	bp->bio_bcount = bp->bio_length;
691e24cbd90SPoul-Henning Kamp 	bp->bio_resid = bp->bio_bcount - bp->bio_completed;
69219fa21aaSPoul-Henning Kamp 
69340ea77a0SAlexander Motin #ifdef GET_STACK_USAGE
69440ea77a0SAlexander Motin 	direct = (pp->flags & G_PF_DIRECT_SEND) &&
69540ea77a0SAlexander Motin 		 (cp->flags & G_CF_DIRECT_RECEIVE) &&
69640ea77a0SAlexander Motin 		 !g_is_geom_thread(curthread);
69740ea77a0SAlexander Motin 	if (direct) {
69840ea77a0SAlexander Motin 		/* Block direct execution if less then half of stack left. */
69940ea77a0SAlexander Motin 		size_t	st, su;
70040ea77a0SAlexander Motin 		GET_STACK_USAGE(st, su);
70140ea77a0SAlexander Motin 		if (su * 2 > st)
70240ea77a0SAlexander Motin 			direct = 0;
70340ea77a0SAlexander Motin 	}
70440ea77a0SAlexander Motin #else
70540ea77a0SAlexander Motin 	direct = 0;
70640ea77a0SAlexander Motin #endif
70740ea77a0SAlexander Motin 
7088827c821SPoul-Henning Kamp 	/*
7098827c821SPoul-Henning Kamp 	 * The statistics collection is lockless, as such, but we
7108827c821SPoul-Henning Kamp 	 * can not update one instance of the statistics from more
7118827c821SPoul-Henning Kamp 	 * than one thread at a time, so grab the lock first.
7128827c821SPoul-Henning Kamp 	 */
71340ea77a0SAlexander Motin 	if ((g_collectstats & G_STATS_CONSUMERS) != 0 ||
71440ea77a0SAlexander Motin 	    ((g_collectstats & G_STATS_PROVIDERS) != 0 && pp->stat != NULL))
715e431d66cSAlexander Motin 		binuptime(&now);
71640ea77a0SAlexander Motin 	mtxp = mtx_pool_find(mtxpool_sleep, cp);
71740ea77a0SAlexander Motin 	mtx_lock(mtxp);
71840ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_PROVIDERS)
719e431d66cSAlexander Motin 		devstat_end_transaction_bio_bt(pp->stat, bp, &now);
72040ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_CONSUMERS)
721e431d66cSAlexander Motin 		devstat_end_transaction_bio_bt(cp->stat, bp, &now);
722c6ae9b5fSPoul-Henning Kamp 	cp->nend++;
723c6ae9b5fSPoul-Henning Kamp 	pp->nend++;
72440ea77a0SAlexander Motin 	mtx_unlock(mtxp);
72540ea77a0SAlexander Motin 
72619fa21aaSPoul-Henning Kamp 	if (error != ENOMEM) {
72719fa21aaSPoul-Henning Kamp 		bp->bio_error = error;
72840ea77a0SAlexander Motin 		if (direct) {
72940ea77a0SAlexander Motin 			biodone(bp);
73040ea77a0SAlexander Motin 		} else {
73140ea77a0SAlexander Motin 			g_bioq_lock(&g_bio_run_up);
7320d883b11SAlexander Motin 			first = TAILQ_EMPTY(&g_bio_run_up.bio_queue);
73319fa21aaSPoul-Henning Kamp 			TAILQ_INSERT_TAIL(&g_bio_run_up.bio_queue, bp, bio_queue);
734276f72c5SPoul-Henning Kamp 			bp->bio_flags |= BIO_ONQUEUE;
73519fa21aaSPoul-Henning Kamp 			g_bio_run_up.bio_queue_length++;
73619fa21aaSPoul-Henning Kamp 			g_bioq_unlock(&g_bio_run_up);
7370d883b11SAlexander Motin 			if (first)
73819fa21aaSPoul-Henning Kamp 				wakeup(&g_wait_up);
73940ea77a0SAlexander Motin 		}
74019fa21aaSPoul-Henning Kamp 		return;
74119fa21aaSPoul-Henning Kamp 	}
742dd84a43cSPoul-Henning Kamp 
7432cc9686eSPoul-Henning Kamp 	if (bootverbose)
744801bb689SPoul-Henning Kamp 		printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name);
7451b949c05SPawel Jakub Dawidek 	bp->bio_children = 0;
7461b949c05SPawel Jakub Dawidek 	bp->bio_inbed = 0;
74760114438SPawel Jakub Dawidek 	bp->bio_driver1 = NULL;
74860114438SPawel Jakub Dawidek 	bp->bio_driver2 = NULL;
74960114438SPawel Jakub Dawidek 	bp->bio_pflags = 0;
750801bb689SPoul-Henning Kamp 	g_io_request(bp, cp);
7513f2e5b85SWarner Losh 	pace = 1;
7523432e4fdSPoul-Henning Kamp 	return;
7533432e4fdSPoul-Henning Kamp }
754dd84a43cSPoul-Henning Kamp 
755ee75e7deSKonstantin Belousov SYSCTL_DECL(_kern_geom);
756ee75e7deSKonstantin Belousov 
757ee75e7deSKonstantin Belousov static long transient_maps;
758ee75e7deSKonstantin Belousov SYSCTL_LONG(_kern_geom, OID_AUTO, transient_maps, CTLFLAG_RD,
759ee75e7deSKonstantin Belousov     &transient_maps, 0,
760ee75e7deSKonstantin Belousov     "Total count of the transient mapping requests");
761ee75e7deSKonstantin Belousov u_int transient_map_retries = 10;
762ee75e7deSKonstantin Belousov SYSCTL_UINT(_kern_geom, OID_AUTO, transient_map_retries, CTLFLAG_RW,
763ee75e7deSKonstantin Belousov     &transient_map_retries, 0,
764ee75e7deSKonstantin Belousov     "Max count of retries used before giving up on creating transient map");
765ee75e7deSKonstantin Belousov int transient_map_hard_failures;
766ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, transient_map_hard_failures, CTLFLAG_RD,
767ee75e7deSKonstantin Belousov     &transient_map_hard_failures, 0,
768ee75e7deSKonstantin Belousov     "Failures to establish the transient mapping due to retry attempts "
769ee75e7deSKonstantin Belousov     "exhausted");
770ee75e7deSKonstantin Belousov int transient_map_soft_failures;
771ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, transient_map_soft_failures, CTLFLAG_RD,
772ee75e7deSKonstantin Belousov     &transient_map_soft_failures, 0,
773ee75e7deSKonstantin Belousov     "Count of retried failures to establish the transient mapping");
774ee75e7deSKonstantin Belousov int inflight_transient_maps;
775ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, inflight_transient_maps, CTLFLAG_RD,
776ee75e7deSKonstantin Belousov     &inflight_transient_maps, 0,
777ee75e7deSKonstantin Belousov     "Current count of the active transient maps");
778ee75e7deSKonstantin Belousov 
779ee75e7deSKonstantin Belousov static int
780ee75e7deSKonstantin Belousov g_io_transient_map_bio(struct bio *bp)
781ee75e7deSKonstantin Belousov {
782ee75e7deSKonstantin Belousov 	vm_offset_t addr;
783ee75e7deSKonstantin Belousov 	long size;
784ee75e7deSKonstantin Belousov 	u_int retried;
785ee75e7deSKonstantin Belousov 
7866c83fce3SKonstantin Belousov 	KASSERT(unmapped_buf_allowed, ("unmapped disabled"));
7876c83fce3SKonstantin Belousov 
788ee75e7deSKonstantin Belousov 	size = round_page(bp->bio_ma_offset + bp->bio_length);
789ee75e7deSKonstantin Belousov 	KASSERT(size / PAGE_SIZE == bp->bio_ma_n, ("Bio too short %p", bp));
790ee75e7deSKonstantin Belousov 	addr = 0;
791ee75e7deSKonstantin Belousov 	retried = 0;
792ee75e7deSKonstantin Belousov 	atomic_add_long(&transient_maps, 1);
793ee75e7deSKonstantin Belousov retry:
7945f518366SJeff Roberson 	if (vmem_alloc(transient_arena, size, M_BESTFIT | M_NOWAIT, &addr)) {
795ee75e7deSKonstantin Belousov 		if (transient_map_retries != 0 &&
796ee75e7deSKonstantin Belousov 		    retried >= transient_map_retries) {
797ee75e7deSKonstantin Belousov 			CTR2(KTR_GEOM, "g_down cannot map bp %p provider %s",
798ee75e7deSKonstantin Belousov 			    bp, bp->bio_to->name);
799ee75e7deSKonstantin Belousov 			atomic_add_int(&transient_map_hard_failures, 1);
80040ea77a0SAlexander Motin 			return (EDEADLK/* XXXKIB */);
801ee75e7deSKonstantin Belousov 		} else {
802ee75e7deSKonstantin Belousov 			/*
803ee75e7deSKonstantin Belousov 			 * Naive attempt to quisce the I/O to get more
804ee75e7deSKonstantin Belousov 			 * in-flight requests completed and defragment
8055f518366SJeff Roberson 			 * the transient_arena.
806ee75e7deSKonstantin Belousov 			 */
807ee75e7deSKonstantin Belousov 			CTR3(KTR_GEOM, "g_down retrymap bp %p provider %s r %d",
808ee75e7deSKonstantin Belousov 			    bp, bp->bio_to->name, retried);
809ee75e7deSKonstantin Belousov 			pause("g_d_tra", hz / 10);
810ee75e7deSKonstantin Belousov 			retried++;
811ee75e7deSKonstantin Belousov 			atomic_add_int(&transient_map_soft_failures, 1);
812ee75e7deSKonstantin Belousov 			goto retry;
813ee75e7deSKonstantin Belousov 		}
814ee75e7deSKonstantin Belousov 	}
815ee75e7deSKonstantin Belousov 	atomic_add_int(&inflight_transient_maps, 1);
816ee75e7deSKonstantin Belousov 	pmap_qenter((vm_offset_t)addr, bp->bio_ma, OFF_TO_IDX(size));
817ee75e7deSKonstantin Belousov 	bp->bio_data = (caddr_t)addr + bp->bio_ma_offset;
818ee75e7deSKonstantin Belousov 	bp->bio_flags |= BIO_TRANSIENT_MAPPING;
819ee75e7deSKonstantin Belousov 	bp->bio_flags &= ~BIO_UNMAPPED;
82040ea77a0SAlexander Motin 	return (EJUSTRETURN);
821ee75e7deSKonstantin Belousov }
822ee75e7deSKonstantin Belousov 
823dd84a43cSPoul-Henning Kamp void
824dd84a43cSPoul-Henning Kamp g_io_schedule_down(struct thread *tp __unused)
825dd84a43cSPoul-Henning Kamp {
826dd84a43cSPoul-Henning Kamp 	struct bio *bp;
827e39d70d4SPoul-Henning Kamp 	int error;
828dd84a43cSPoul-Henning Kamp 
829dd84a43cSPoul-Henning Kamp 	for(;;) {
830f0e185d7SPoul-Henning Kamp 		g_bioq_lock(&g_bio_run_down);
831dd84a43cSPoul-Henning Kamp 		bp = g_bioq_first(&g_bio_run_down);
832f0e185d7SPoul-Henning Kamp 		if (bp == NULL) {
83349dbb61dSRobert Watson 			CTR0(KTR_GEOM, "g_down going to sleep");
834f0e185d7SPoul-Henning Kamp 			msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock,
8357fc019afSAlexander Motin 			    PRIBIO | PDROP, "-", 0);
836f0e185d7SPoul-Henning Kamp 			continue;
837f0e185d7SPoul-Henning Kamp 		}
83849dbb61dSRobert Watson 		CTR0(KTR_GEOM, "g_down has work to do");
839f0e185d7SPoul-Henning Kamp 		g_bioq_unlock(&g_bio_run_down);
8403f2e5b85SWarner Losh 		if (pace != 0) {
8413f2e5b85SWarner Losh 			/*
8423f2e5b85SWarner Losh 			 * There has been at least one memory allocation
8433f2e5b85SWarner Losh 			 * failure since the last I/O completed. Pause 1ms to
8443f2e5b85SWarner Losh 			 * give the system a chance to free up memory. We only
8453f2e5b85SWarner Losh 			 * do this once because a large number of allocations
8463f2e5b85SWarner Losh 			 * can fail in the direct dispatch case and there's no
8473f2e5b85SWarner Losh 			 * relationship between the number of these failures and
8483f2e5b85SWarner Losh 			 * the length of the outage. If there's still an outage,
8493f2e5b85SWarner Losh 			 * we'll pause again and again until it's
8503f2e5b85SWarner Losh 			 * resolved. Older versions paused longer and once per
8513f2e5b85SWarner Losh 			 * allocation failure. This was OK for a single threaded
8523f2e5b85SWarner Losh 			 * g_down, but with direct dispatch would lead to max of
8533f2e5b85SWarner Losh 			 * 10 IOPs for minutes at a time when transient memory
8543f2e5b85SWarner Losh 			 * issues prevented allocation for a batch of requests
8553f2e5b85SWarner Losh 			 * from the upper layers.
8563f2e5b85SWarner Losh 			 *
8573f2e5b85SWarner Losh 			 * XXX This pacing is really lame. It needs to be solved
8583f2e5b85SWarner Losh 			 * by other methods. This is OK only because the worst
8593f2e5b85SWarner Losh 			 * case scenario is so rare. In the worst case scenario
8603f2e5b85SWarner Losh 			 * all memory is tied up waiting for I/O to complete
8613f2e5b85SWarner Losh 			 * which can never happen since we can't allocate bios
8623f2e5b85SWarner Losh 			 * for that I/O.
8633f2e5b85SWarner Losh 			 */
8643f2e5b85SWarner Losh 			CTR0(KTR_GEOM, "g_down pacing self");
8653f2e5b85SWarner Losh 			pause("g_down", min(hz/1000, 1));
8663f2e5b85SWarner Losh 			pace = 0;
867376ceb79SPoul-Henning Kamp 		}
86840ea77a0SAlexander Motin 		CTR2(KTR_GEOM, "g_down processing bp %p provider %s", bp,
86940ea77a0SAlexander Motin 		    bp->bio_to->name);
870e39d70d4SPoul-Henning Kamp 		error = g_io_check(bp);
87140ea77a0SAlexander Motin 		if (error >= 0) {
87249dbb61dSRobert Watson 			CTR3(KTR_GEOM, "g_down g_io_check on bp %p provider "
87349dbb61dSRobert Watson 			    "%s returned %d", bp, bp->bio_to->name, error);
874e39d70d4SPoul-Henning Kamp 			g_io_deliver(bp, error);
875e39d70d4SPoul-Henning Kamp 			continue;
876e39d70d4SPoul-Henning Kamp 		}
87751460da8SJohn Baldwin 		THREAD_NO_SLEEPING();
87849dbb61dSRobert Watson 		CTR4(KTR_GEOM, "g_down starting bp %p provider %s off %ld "
87949dbb61dSRobert Watson 		    "len %ld", bp, bp->bio_to->name, bp->bio_offset,
88049dbb61dSRobert Watson 		    bp->bio_length);
881dd84a43cSPoul-Henning Kamp 		bp->bio_to->geom->start(bp);
88251460da8SJohn Baldwin 		THREAD_SLEEPING_OK();
883dd84a43cSPoul-Henning Kamp 	}
884dd84a43cSPoul-Henning Kamp }
885dd84a43cSPoul-Henning Kamp 
886dd84a43cSPoul-Henning Kamp void
8875fcf4e43SPoul-Henning Kamp bio_taskqueue(struct bio *bp, bio_task_t *func, void *arg)
8885fcf4e43SPoul-Henning Kamp {
8895fcf4e43SPoul-Henning Kamp 	bp->bio_task = func;
8905fcf4e43SPoul-Henning Kamp 	bp->bio_task_arg = arg;
8915fcf4e43SPoul-Henning Kamp 	/*
8925fcf4e43SPoul-Henning Kamp 	 * The taskqueue is actually just a second queue off the "up"
8935fcf4e43SPoul-Henning Kamp 	 * queue, so we use the same lock.
8945fcf4e43SPoul-Henning Kamp 	 */
8955fcf4e43SPoul-Henning Kamp 	g_bioq_lock(&g_bio_run_up);
896dcbd0fe5SPoul-Henning Kamp 	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
897dcbd0fe5SPoul-Henning Kamp 	    ("Bio already on queue bp=%p target taskq", bp));
898dcbd0fe5SPoul-Henning Kamp 	bp->bio_flags |= BIO_ONQUEUE;
8995fcf4e43SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&g_bio_run_task.bio_queue, bp, bio_queue);
9005fcf4e43SPoul-Henning Kamp 	g_bio_run_task.bio_queue_length++;
9015fcf4e43SPoul-Henning Kamp 	wakeup(&g_wait_up);
9025fcf4e43SPoul-Henning Kamp 	g_bioq_unlock(&g_bio_run_up);
9035fcf4e43SPoul-Henning Kamp }
9045fcf4e43SPoul-Henning Kamp 
9055fcf4e43SPoul-Henning Kamp 
9065fcf4e43SPoul-Henning Kamp void
907dd84a43cSPoul-Henning Kamp g_io_schedule_up(struct thread *tp __unused)
908dd84a43cSPoul-Henning Kamp {
909dd84a43cSPoul-Henning Kamp 	struct bio *bp;
910dd84a43cSPoul-Henning Kamp 	for(;;) {
911f0e185d7SPoul-Henning Kamp 		g_bioq_lock(&g_bio_run_up);
9125fcf4e43SPoul-Henning Kamp 		bp = g_bioq_first(&g_bio_run_task);
9135fcf4e43SPoul-Henning Kamp 		if (bp != NULL) {
9145fcf4e43SPoul-Henning Kamp 			g_bioq_unlock(&g_bio_run_up);
91551460da8SJohn Baldwin 			THREAD_NO_SLEEPING();
91649dbb61dSRobert Watson 			CTR1(KTR_GEOM, "g_up processing task bp %p", bp);
9175fcf4e43SPoul-Henning Kamp 			bp->bio_task(bp->bio_task_arg);
91851460da8SJohn Baldwin 			THREAD_SLEEPING_OK();
9195fcf4e43SPoul-Henning Kamp 			continue;
9205fcf4e43SPoul-Henning Kamp 		}
921dd84a43cSPoul-Henning Kamp 		bp = g_bioq_first(&g_bio_run_up);
922f0e185d7SPoul-Henning Kamp 		if (bp != NULL) {
923f0e185d7SPoul-Henning Kamp 			g_bioq_unlock(&g_bio_run_up);
92451460da8SJohn Baldwin 			THREAD_NO_SLEEPING();
92549dbb61dSRobert Watson 			CTR4(KTR_GEOM, "g_up biodone bp %p provider %s off "
926c4901b67SSean Bruno 			    "%jd len %ld", bp, bp->bio_to->name,
92749dbb61dSRobert Watson 			    bp->bio_offset, bp->bio_length);
92853706245SPoul-Henning Kamp 			biodone(bp);
92951460da8SJohn Baldwin 			THREAD_SLEEPING_OK();
930f0e185d7SPoul-Henning Kamp 			continue;
931f0e185d7SPoul-Henning Kamp 		}
93249dbb61dSRobert Watson 		CTR0(KTR_GEOM, "g_up going to sleep");
933f0e185d7SPoul-Henning Kamp 		msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock,
9347fc019afSAlexander Motin 		    PRIBIO | PDROP, "-", 0);
935dd84a43cSPoul-Henning Kamp 	}
936dd84a43cSPoul-Henning Kamp }
937dd84a43cSPoul-Henning Kamp 
938dd84a43cSPoul-Henning Kamp void *
939dd84a43cSPoul-Henning Kamp g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
940dd84a43cSPoul-Henning Kamp {
941dd84a43cSPoul-Henning Kamp 	struct bio *bp;
942dd84a43cSPoul-Henning Kamp 	void *ptr;
943dd84a43cSPoul-Henning Kamp 	int errorc;
944dd84a43cSPoul-Henning Kamp 
9458dd5480dSPawel Jakub Dawidek 	KASSERT(length > 0 && length >= cp->provider->sectorsize &&
9468dd5480dSPawel Jakub Dawidek 	    length <= MAXPHYS, ("g_read_data(): invalid length %jd",
9478dd5480dSPawel Jakub Dawidek 	    (intmax_t)length));
9483eb6ffdfSPoul-Henning Kamp 
949a2033c96SPoul-Henning Kamp 	bp = g_alloc_bio();
950dd84a43cSPoul-Henning Kamp 	bp->bio_cmd = BIO_READ;
951dd84a43cSPoul-Henning Kamp 	bp->bio_done = NULL;
952dd84a43cSPoul-Henning Kamp 	bp->bio_offset = offset;
953dd84a43cSPoul-Henning Kamp 	bp->bio_length = length;
954a163d034SWarner Losh 	ptr = g_malloc(length, M_WAITOK);
955dd84a43cSPoul-Henning Kamp 	bp->bio_data = ptr;
956dd84a43cSPoul-Henning Kamp 	g_io_request(bp, cp);
95753706245SPoul-Henning Kamp 	errorc = biowait(bp, "gread");
958dd84a43cSPoul-Henning Kamp 	if (error != NULL)
959dd84a43cSPoul-Henning Kamp 		*error = errorc;
960dd84a43cSPoul-Henning Kamp 	g_destroy_bio(bp);
961dd84a43cSPoul-Henning Kamp 	if (errorc) {
962dd84a43cSPoul-Henning Kamp 		g_free(ptr);
963dd84a43cSPoul-Henning Kamp 		ptr = NULL;
964dd84a43cSPoul-Henning Kamp 	}
965dd84a43cSPoul-Henning Kamp 	return (ptr);
966dd84a43cSPoul-Henning Kamp }
96790b1cd56SPoul-Henning Kamp 
96890b1cd56SPoul-Henning Kamp int
96990b1cd56SPoul-Henning Kamp g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
97090b1cd56SPoul-Henning Kamp {
97190b1cd56SPoul-Henning Kamp 	struct bio *bp;
97290b1cd56SPoul-Henning Kamp 	int error;
97390b1cd56SPoul-Henning Kamp 
9748dd5480dSPawel Jakub Dawidek 	KASSERT(length > 0 && length >= cp->provider->sectorsize &&
9758dd5480dSPawel Jakub Dawidek 	    length <= MAXPHYS, ("g_write_data(): invalid length %jd",
9768dd5480dSPawel Jakub Dawidek 	    (intmax_t)length));
9773eb6ffdfSPoul-Henning Kamp 
978a2033c96SPoul-Henning Kamp 	bp = g_alloc_bio();
97990b1cd56SPoul-Henning Kamp 	bp->bio_cmd = BIO_WRITE;
98090b1cd56SPoul-Henning Kamp 	bp->bio_done = NULL;
98190b1cd56SPoul-Henning Kamp 	bp->bio_offset = offset;
98290b1cd56SPoul-Henning Kamp 	bp->bio_length = length;
98390b1cd56SPoul-Henning Kamp 	bp->bio_data = ptr;
98490b1cd56SPoul-Henning Kamp 	g_io_request(bp, cp);
98590b1cd56SPoul-Henning Kamp 	error = biowait(bp, "gwrite");
98690b1cd56SPoul-Henning Kamp 	g_destroy_bio(bp);
98790b1cd56SPoul-Henning Kamp 	return (error);
98890b1cd56SPoul-Henning Kamp }
98972e33095SPawel Jakub Dawidek 
9902b17fb95SPawel Jakub Dawidek int
9912b17fb95SPawel Jakub Dawidek g_delete_data(struct g_consumer *cp, off_t offset, off_t length)
9922b17fb95SPawel Jakub Dawidek {
9932b17fb95SPawel Jakub Dawidek 	struct bio *bp;
9942b17fb95SPawel Jakub Dawidek 	int error;
9952b17fb95SPawel Jakub Dawidek 
996eed6cda9SPoul-Henning Kamp 	KASSERT(length > 0 && length >= cp->provider->sectorsize,
997eed6cda9SPoul-Henning Kamp 	    ("g_delete_data(): invalid length %jd", (intmax_t)length));
9982b17fb95SPawel Jakub Dawidek 
9992b17fb95SPawel Jakub Dawidek 	bp = g_alloc_bio();
10002b17fb95SPawel Jakub Dawidek 	bp->bio_cmd = BIO_DELETE;
10012b17fb95SPawel Jakub Dawidek 	bp->bio_done = NULL;
10022b17fb95SPawel Jakub Dawidek 	bp->bio_offset = offset;
10032b17fb95SPawel Jakub Dawidek 	bp->bio_length = length;
10042b17fb95SPawel Jakub Dawidek 	bp->bio_data = NULL;
10052b17fb95SPawel Jakub Dawidek 	g_io_request(bp, cp);
10062b17fb95SPawel Jakub Dawidek 	error = biowait(bp, "gdelete");
10072b17fb95SPawel Jakub Dawidek 	g_destroy_bio(bp);
10082b17fb95SPawel Jakub Dawidek 	return (error);
10092b17fb95SPawel Jakub Dawidek }
10102b17fb95SPawel Jakub Dawidek 
101172e33095SPawel Jakub Dawidek void
101272e33095SPawel Jakub Dawidek g_print_bio(struct bio *bp)
101372e33095SPawel Jakub Dawidek {
101472e33095SPawel Jakub Dawidek 	const char *pname, *cmd = NULL;
101572e33095SPawel Jakub Dawidek 
101672e33095SPawel Jakub Dawidek 	if (bp->bio_to != NULL)
101772e33095SPawel Jakub Dawidek 		pname = bp->bio_to->name;
101872e33095SPawel Jakub Dawidek 	else
101972e33095SPawel Jakub Dawidek 		pname = "[unknown]";
102072e33095SPawel Jakub Dawidek 
102172e33095SPawel Jakub Dawidek 	switch (bp->bio_cmd) {
102272e33095SPawel Jakub Dawidek 	case BIO_GETATTR:
102372e33095SPawel Jakub Dawidek 		cmd = "GETATTR";
102472e33095SPawel Jakub Dawidek 		printf("%s[%s(attr=%s)]", pname, cmd, bp->bio_attribute);
102572e33095SPawel Jakub Dawidek 		return;
1026c3618c65SPawel Jakub Dawidek 	case BIO_FLUSH:
1027c3618c65SPawel Jakub Dawidek 		cmd = "FLUSH";
1028c3618c65SPawel Jakub Dawidek 		printf("%s[%s]", pname, cmd);
1029c3618c65SPawel Jakub Dawidek 		return;
1030*9a6844d5SKenneth D. Merry 	case BIO_ZONE: {
1031*9a6844d5SKenneth D. Merry 		char *subcmd = NULL;
1032*9a6844d5SKenneth D. Merry 		cmd = "ZONE";
1033*9a6844d5SKenneth D. Merry 		switch (bp->bio_zone.zone_cmd) {
1034*9a6844d5SKenneth D. Merry 		case DISK_ZONE_OPEN:
1035*9a6844d5SKenneth D. Merry 			subcmd = "OPEN";
1036*9a6844d5SKenneth D. Merry 			break;
1037*9a6844d5SKenneth D. Merry 		case DISK_ZONE_CLOSE:
1038*9a6844d5SKenneth D. Merry 			subcmd = "CLOSE";
1039*9a6844d5SKenneth D. Merry 			break;
1040*9a6844d5SKenneth D. Merry 		case DISK_ZONE_FINISH:
1041*9a6844d5SKenneth D. Merry 			subcmd = "FINISH";
1042*9a6844d5SKenneth D. Merry 			break;
1043*9a6844d5SKenneth D. Merry 		case DISK_ZONE_RWP:
1044*9a6844d5SKenneth D. Merry 			subcmd = "RWP";
1045*9a6844d5SKenneth D. Merry 			break;
1046*9a6844d5SKenneth D. Merry 		case DISK_ZONE_REPORT_ZONES:
1047*9a6844d5SKenneth D. Merry 			subcmd = "REPORT ZONES";
1048*9a6844d5SKenneth D. Merry 			break;
1049*9a6844d5SKenneth D. Merry 		case DISK_ZONE_GET_PARAMS:
1050*9a6844d5SKenneth D. Merry 			subcmd = "GET PARAMS";
1051*9a6844d5SKenneth D. Merry 			break;
1052*9a6844d5SKenneth D. Merry 		default:
1053*9a6844d5SKenneth D. Merry 			subcmd = "UNKNOWN";
1054*9a6844d5SKenneth D. Merry 			break;
1055*9a6844d5SKenneth D. Merry 		}
1056*9a6844d5SKenneth D. Merry 		printf("%s[%s,%s]", pname, cmd, subcmd);
1057*9a6844d5SKenneth D. Merry 		return;
1058*9a6844d5SKenneth D. Merry 	}
105972e33095SPawel Jakub Dawidek 	case BIO_READ:
106072e33095SPawel Jakub Dawidek 		cmd = "READ";
10617ce513a5SEdward Tomasz Napierala 		break;
106272e33095SPawel Jakub Dawidek 	case BIO_WRITE:
106372e33095SPawel Jakub Dawidek 		cmd = "WRITE";
10647ce513a5SEdward Tomasz Napierala 		break;
106572e33095SPawel Jakub Dawidek 	case BIO_DELETE:
106672e33095SPawel Jakub Dawidek 		cmd = "DELETE";
10677ce513a5SEdward Tomasz Napierala 		break;
106872e33095SPawel Jakub Dawidek 	default:
106972e33095SPawel Jakub Dawidek 		cmd = "UNKNOWN";
107072e33095SPawel Jakub Dawidek 		printf("%s[%s()]", pname, cmd);
107172e33095SPawel Jakub Dawidek 		return;
107272e33095SPawel Jakub Dawidek 	}
10737ce513a5SEdward Tomasz Napierala 	printf("%s[%s(offset=%jd, length=%jd)]", pname, cmd,
10747ce513a5SEdward Tomasz Napierala 	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
107572e33095SPawel Jakub Dawidek }
1076