xref: /freebsd/sys/geom/geom_io.c (revision 3f2e5b8584ec984cc36a80d06e0012c2ddf37a37)
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 
74*3f2e5b85SWarner Losh /*
75*3f2e5b85SWarner Losh  * Pace is a hint that we've had some trouble recently allocating
76*3f2e5b85SWarner Losh  * bios, so we should back off trying to send I/O down the stack
77*3f2e5b85SWarner Losh  * a bit to let the problem resolve. When pacing, we also turn
78*3f2e5b85SWarner Losh  * off direct dispatch to also reduce memory pressure from I/Os
79*3f2e5b85SWarner Losh  * there, at the expxense of some added latency while the memory
80*3f2e5b85SWarner Losh  * pressures exist. See g_io_schedule_down() for more details
81*3f2e5b85SWarner Losh  * and limitations.
82*3f2e5b85SWarner Losh  */
83*3f2e5b85SWarner Losh static volatile u_int pace;
84*3f2e5b85SWarner 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.
208ee75e7deSKonstantin Belousov 		 *  BIO_UNMAPPED should be inherited, to properly indicate
209ee75e7deSKonstantin Belousov 		 *  which way the buffer is passed.
21082a6ae10SJim Harris 		 *  Other bio flags are not suitable for cloning.
21182a6ae10SJim Harris 		 */
212ee75e7deSKonstantin Belousov 		bp2->bio_flags = bp->bio_flags & (BIO_ORDERED | BIO_UNMAPPED);
213dd84a43cSPoul-Henning Kamp 		bp2->bio_length = bp->bio_length;
214dd84a43cSPoul-Henning Kamp 		bp2->bio_offset = bp->bio_offset;
215dd84a43cSPoul-Henning Kamp 		bp2->bio_data = bp->bio_data;
216ee75e7deSKonstantin Belousov 		bp2->bio_ma = bp->bio_ma;
217ee75e7deSKonstantin Belousov 		bp2->bio_ma_n = bp->bio_ma_n;
218ee75e7deSKonstantin Belousov 		bp2->bio_ma_offset = bp->bio_ma_offset;
219dd84a43cSPoul-Henning Kamp 		bp2->bio_attribute = bp->bio_attribute;
2206231f75bSLuigi Rizzo 		/* Inherit classification info from the parent */
2216231f75bSLuigi Rizzo 		bp2->bio_classifier1 = bp->bio_classifier1;
2226231f75bSLuigi Rizzo 		bp2->bio_classifier2 = bp->bio_classifier2;
223801bb689SPoul-Henning Kamp 		bp->bio_children++;
224a1bd3ee2SPoul-Henning Kamp 	}
2253b378147SPawel Jakub Dawidek #ifdef KTR
226b656c1b8SPawel Jakub Dawidek 	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
2273b378147SPawel Jakub Dawidek 		struct stack st;
2283b378147SPawel Jakub Dawidek 
229ad572235SRuslan Ermilov 		CTR2(KTR_GEOM, "g_clone_bio(%p): %p", bp, bp2);
2303b378147SPawel Jakub Dawidek 		stack_save(&st);
2313b378147SPawel Jakub Dawidek 		CTRSTACK(KTR_GEOM, &st, 3, 0);
2323b378147SPawel Jakub Dawidek 	}
2333b378147SPawel Jakub Dawidek #endif
234dd84a43cSPoul-Henning Kamp 	return(bp2);
235dd84a43cSPoul-Henning Kamp }
236dd84a43cSPoul-Henning Kamp 
2374bec0ff1SPawel Jakub Dawidek struct bio *
2384bec0ff1SPawel Jakub Dawidek g_duplicate_bio(struct bio *bp)
2394bec0ff1SPawel Jakub Dawidek {
2404bec0ff1SPawel Jakub Dawidek 	struct bio *bp2;
2414bec0ff1SPawel Jakub Dawidek 
2424bec0ff1SPawel Jakub Dawidek 	bp2 = uma_zalloc(biozone, M_WAITOK | M_ZERO);
243ee75e7deSKonstantin Belousov 	bp2->bio_flags = bp->bio_flags & BIO_UNMAPPED;
2444bec0ff1SPawel Jakub Dawidek 	bp2->bio_parent = bp;
2454bec0ff1SPawel Jakub Dawidek 	bp2->bio_cmd = bp->bio_cmd;
2464bec0ff1SPawel Jakub Dawidek 	bp2->bio_length = bp->bio_length;
2474bec0ff1SPawel Jakub Dawidek 	bp2->bio_offset = bp->bio_offset;
2484bec0ff1SPawel Jakub Dawidek 	bp2->bio_data = bp->bio_data;
249ee75e7deSKonstantin Belousov 	bp2->bio_ma = bp->bio_ma;
250ee75e7deSKonstantin Belousov 	bp2->bio_ma_n = bp->bio_ma_n;
251ee75e7deSKonstantin Belousov 	bp2->bio_ma_offset = bp->bio_ma_offset;
2524bec0ff1SPawel Jakub Dawidek 	bp2->bio_attribute = bp->bio_attribute;
2534bec0ff1SPawel Jakub Dawidek 	bp->bio_children++;
2544bec0ff1SPawel Jakub Dawidek #ifdef KTR
255b656c1b8SPawel Jakub Dawidek 	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
2564bec0ff1SPawel Jakub Dawidek 		struct stack st;
2574bec0ff1SPawel Jakub Dawidek 
2584bec0ff1SPawel Jakub Dawidek 		CTR2(KTR_GEOM, "g_duplicate_bio(%p): %p", bp, bp2);
2594bec0ff1SPawel Jakub Dawidek 		stack_save(&st);
2604bec0ff1SPawel Jakub Dawidek 		CTRSTACK(KTR_GEOM, &st, 3, 0);
2614bec0ff1SPawel Jakub Dawidek 	}
2624bec0ff1SPawel Jakub Dawidek #endif
2634bec0ff1SPawel Jakub Dawidek 	return(bp2);
2644bec0ff1SPawel Jakub Dawidek }
2654bec0ff1SPawel Jakub Dawidek 
266dd84a43cSPoul-Henning Kamp void
267dd84a43cSPoul-Henning Kamp g_io_init()
268dd84a43cSPoul-Henning Kamp {
269dd84a43cSPoul-Henning Kamp 
270dd84a43cSPoul-Henning Kamp 	g_bioq_init(&g_bio_run_down);
271dd84a43cSPoul-Henning Kamp 	g_bioq_init(&g_bio_run_up);
2725fcf4e43SPoul-Henning Kamp 	g_bioq_init(&g_bio_run_task);
2735ffb2c8bSPoul-Henning Kamp 	biozone = uma_zcreate("g_bio", sizeof (struct bio),
2745ffb2c8bSPoul-Henning Kamp 	    NULL, NULL,
2755ffb2c8bSPoul-Henning Kamp 	    NULL, NULL,
2765ffb2c8bSPoul-Henning Kamp 	    0, 0);
277dd84a43cSPoul-Henning Kamp }
278dd84a43cSPoul-Henning Kamp 
279dd84a43cSPoul-Henning Kamp int
2800d3f37a8SPoul-Henning Kamp g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr)
281dd84a43cSPoul-Henning Kamp {
282dd84a43cSPoul-Henning Kamp 	struct bio *bp;
283dd84a43cSPoul-Henning Kamp 	int error;
284dd84a43cSPoul-Henning Kamp 
285dd84a43cSPoul-Henning Kamp 	g_trace(G_T_BIO, "bio_getattr(%s)", attr);
286a2033c96SPoul-Henning Kamp 	bp = g_alloc_bio();
287dd84a43cSPoul-Henning Kamp 	bp->bio_cmd = BIO_GETATTR;
288dd84a43cSPoul-Henning Kamp 	bp->bio_done = NULL;
289dd84a43cSPoul-Henning Kamp 	bp->bio_attribute = attr;
290dd84a43cSPoul-Henning Kamp 	bp->bio_length = *len;
291dd84a43cSPoul-Henning Kamp 	bp->bio_data = ptr;
292dd84a43cSPoul-Henning Kamp 	g_io_request(bp, cp);
29353706245SPoul-Henning Kamp 	error = biowait(bp, "ggetattr");
294dd84a43cSPoul-Henning Kamp 	*len = bp->bio_completed;
295dd84a43cSPoul-Henning Kamp 	g_destroy_bio(bp);
296dd84a43cSPoul-Henning Kamp 	return (error);
297dd84a43cSPoul-Henning Kamp }
298dd84a43cSPoul-Henning Kamp 
299c3618c65SPawel Jakub Dawidek int
300c3618c65SPawel Jakub Dawidek g_io_flush(struct g_consumer *cp)
301c3618c65SPawel Jakub Dawidek {
302c3618c65SPawel Jakub Dawidek 	struct bio *bp;
303c3618c65SPawel Jakub Dawidek 	int error;
304c3618c65SPawel Jakub Dawidek 
305c3618c65SPawel Jakub Dawidek 	g_trace(G_T_BIO, "bio_flush(%s)", cp->provider->name);
306c3618c65SPawel Jakub Dawidek 	bp = g_alloc_bio();
307c3618c65SPawel Jakub Dawidek 	bp->bio_cmd = BIO_FLUSH;
308f03f7a0cSJustin T. Gibbs 	bp->bio_flags |= BIO_ORDERED;
309c3618c65SPawel Jakub Dawidek 	bp->bio_done = NULL;
310c3618c65SPawel Jakub Dawidek 	bp->bio_attribute = NULL;
311c3618c65SPawel Jakub Dawidek 	bp->bio_offset = cp->provider->mediasize;
312c3618c65SPawel Jakub Dawidek 	bp->bio_length = 0;
313c3618c65SPawel Jakub Dawidek 	bp->bio_data = NULL;
314c3618c65SPawel Jakub Dawidek 	g_io_request(bp, cp);
315c3618c65SPawel Jakub Dawidek 	error = biowait(bp, "gflush");
316c3618c65SPawel Jakub Dawidek 	g_destroy_bio(bp);
317c3618c65SPawel Jakub Dawidek 	return (error);
318c3618c65SPawel Jakub Dawidek }
319c3618c65SPawel Jakub Dawidek 
320e39d70d4SPoul-Henning Kamp static int
321e39d70d4SPoul-Henning Kamp g_io_check(struct bio *bp)
322e39d70d4SPoul-Henning Kamp {
323e39d70d4SPoul-Henning Kamp 	struct g_consumer *cp;
324e39d70d4SPoul-Henning Kamp 	struct g_provider *pp;
32540ea77a0SAlexander Motin 	off_t excess;
32640ea77a0SAlexander Motin 	int error;
327e39d70d4SPoul-Henning Kamp 
328e39d70d4SPoul-Henning Kamp 	cp = bp->bio_from;
329e39d70d4SPoul-Henning Kamp 	pp = bp->bio_to;
330e39d70d4SPoul-Henning Kamp 
331e39d70d4SPoul-Henning Kamp 	/* Fail if access counters dont allow the operation */
332e39d70d4SPoul-Henning Kamp 	switch(bp->bio_cmd) {
333e39d70d4SPoul-Henning Kamp 	case BIO_READ:
334e39d70d4SPoul-Henning Kamp 	case BIO_GETATTR:
335e39d70d4SPoul-Henning Kamp 		if (cp->acr == 0)
336e39d70d4SPoul-Henning Kamp 			return (EPERM);
337e39d70d4SPoul-Henning Kamp 		break;
338e39d70d4SPoul-Henning Kamp 	case BIO_WRITE:
339e39d70d4SPoul-Henning Kamp 	case BIO_DELETE:
340c3618c65SPawel Jakub Dawidek 	case BIO_FLUSH:
341e39d70d4SPoul-Henning Kamp 		if (cp->acw == 0)
342e39d70d4SPoul-Henning Kamp 			return (EPERM);
343e39d70d4SPoul-Henning Kamp 		break;
344e39d70d4SPoul-Henning Kamp 	default:
345e39d70d4SPoul-Henning Kamp 		return (EPERM);
346e39d70d4SPoul-Henning Kamp 	}
347e39d70d4SPoul-Henning Kamp 	/* if provider is marked for error, don't disturb. */
348e39d70d4SPoul-Henning Kamp 	if (pp->error)
349e39d70d4SPoul-Henning Kamp 		return (pp->error);
3503631c638SAlexander Motin 	if (cp->flags & G_CF_ORPHAN)
3513631c638SAlexander Motin 		return (ENXIO);
352e39d70d4SPoul-Henning Kamp 
353e39d70d4SPoul-Henning Kamp 	switch(bp->bio_cmd) {
354e39d70d4SPoul-Henning Kamp 	case BIO_READ:
355e39d70d4SPoul-Henning Kamp 	case BIO_WRITE:
356e39d70d4SPoul-Henning Kamp 	case BIO_DELETE:
3572a842317SAndriy Gapon 		/* Zero sectorsize or mediasize is probably a lack of media. */
3582a842317SAndriy Gapon 		if (pp->sectorsize == 0 || pp->mediasize == 0)
35943bff1a7SPoul-Henning Kamp 			return (ENXIO);
360e39d70d4SPoul-Henning Kamp 		/* Reject I/O not on sector boundary */
361e39d70d4SPoul-Henning Kamp 		if (bp->bio_offset % pp->sectorsize)
362e39d70d4SPoul-Henning Kamp 			return (EINVAL);
363e39d70d4SPoul-Henning Kamp 		/* Reject I/O not integral sector long */
364e39d70d4SPoul-Henning Kamp 		if (bp->bio_length % pp->sectorsize)
365e39d70d4SPoul-Henning Kamp 			return (EINVAL);
366d1b8bf47SPoul-Henning Kamp 		/* Reject requests before or past the end of media. */
367d1b8bf47SPoul-Henning Kamp 		if (bp->bio_offset < 0)
368d1b8bf47SPoul-Henning Kamp 			return (EIO);
369e39d70d4SPoul-Henning Kamp 		if (bp->bio_offset > pp->mediasize)
370e39d70d4SPoul-Henning Kamp 			return (EIO);
37140ea77a0SAlexander Motin 
37240ea77a0SAlexander Motin 		/* Truncate requests to the end of providers media. */
37340ea77a0SAlexander Motin 		excess = bp->bio_offset + bp->bio_length;
37440ea77a0SAlexander Motin 		if (excess > bp->bio_to->mediasize) {
37540ea77a0SAlexander Motin 			KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
37640ea77a0SAlexander Motin 			    round_page(bp->bio_ma_offset +
37740ea77a0SAlexander Motin 			    bp->bio_length) / PAGE_SIZE == bp->bio_ma_n,
37840ea77a0SAlexander Motin 			    ("excess bio %p too short", bp));
37940ea77a0SAlexander Motin 			excess -= bp->bio_to->mediasize;
38040ea77a0SAlexander Motin 			bp->bio_length -= excess;
38140ea77a0SAlexander Motin 			if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
38240ea77a0SAlexander Motin 				bp->bio_ma_n = round_page(bp->bio_ma_offset +
38340ea77a0SAlexander Motin 				    bp->bio_length) / PAGE_SIZE;
38440ea77a0SAlexander Motin 			}
38540ea77a0SAlexander Motin 			if (excess > 0)
38640ea77a0SAlexander Motin 				CTR3(KTR_GEOM, "g_down truncated bio "
38740ea77a0SAlexander Motin 				    "%p provider %s by %d", bp,
38840ea77a0SAlexander Motin 				    bp->bio_to->name, excess);
38940ea77a0SAlexander Motin 		}
39040ea77a0SAlexander Motin 
39140ea77a0SAlexander Motin 		/* Deliver zero length transfers right here. */
39240ea77a0SAlexander Motin 		if (bp->bio_length == 0) {
39340ea77a0SAlexander Motin 			CTR2(KTR_GEOM, "g_down terminated 0-length "
39440ea77a0SAlexander Motin 			    "bp %p provider %s", bp, bp->bio_to->name);
39540ea77a0SAlexander Motin 			return (0);
39640ea77a0SAlexander Motin 		}
39740ea77a0SAlexander Motin 
39840ea77a0SAlexander Motin 		if ((bp->bio_flags & BIO_UNMAPPED) != 0 &&
39940ea77a0SAlexander Motin 		    (bp->bio_to->flags & G_PF_ACCEPT_UNMAPPED) == 0 &&
40040ea77a0SAlexander Motin 		    (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
40140ea77a0SAlexander Motin 			if ((error = g_io_transient_map_bio(bp)) >= 0)
40240ea77a0SAlexander Motin 				return (error);
40340ea77a0SAlexander Motin 		}
404e39d70d4SPoul-Henning Kamp 		break;
405e39d70d4SPoul-Henning Kamp 	default:
406e39d70d4SPoul-Henning Kamp 		break;
407e39d70d4SPoul-Henning Kamp 	}
40840ea77a0SAlexander Motin 	return (EJUSTRETURN);
409e39d70d4SPoul-Henning Kamp }
410e39d70d4SPoul-Henning Kamp 
4116231f75bSLuigi Rizzo /*
4126231f75bSLuigi Rizzo  * bio classification support.
4136231f75bSLuigi Rizzo  *
4146231f75bSLuigi Rizzo  * g_register_classifier() and g_unregister_classifier()
4156231f75bSLuigi Rizzo  * are used to add/remove a classifier from the list.
4166231f75bSLuigi Rizzo  * The list is protected using the g_bio_run_down lock,
4176231f75bSLuigi Rizzo  * because the classifiers are called in this path.
4186231f75bSLuigi Rizzo  *
4196231f75bSLuigi Rizzo  * g_io_request() passes bio's that are not already classified
4206231f75bSLuigi Rizzo  * (i.e. those with bio_classifier1 == NULL) to g_run_classifiers().
4216231f75bSLuigi Rizzo  * Classifiers can store their result in the two fields
4226231f75bSLuigi Rizzo  * bio_classifier1 and bio_classifier2.
4236231f75bSLuigi Rizzo  * A classifier that updates one of the fields should
4246231f75bSLuigi Rizzo  * return a non-zero value.
4256231f75bSLuigi Rizzo  * If no classifier updates the field, g_run_classifiers() sets
4266231f75bSLuigi Rizzo  * bio_classifier1 = BIO_NOTCLASSIFIED to avoid further calls.
4276231f75bSLuigi Rizzo  */
4286231f75bSLuigi Rizzo 
4296231f75bSLuigi Rizzo int
4306231f75bSLuigi Rizzo g_register_classifier(struct g_classifier_hook *hook)
4316231f75bSLuigi Rizzo {
4326231f75bSLuigi Rizzo 
4336231f75bSLuigi Rizzo 	g_bioq_lock(&g_bio_run_down);
4346231f75bSLuigi Rizzo 	TAILQ_INSERT_TAIL(&g_classifier_tailq, hook, link);
4356231f75bSLuigi Rizzo 	g_bioq_unlock(&g_bio_run_down);
4366231f75bSLuigi Rizzo 
4376231f75bSLuigi Rizzo 	return (0);
4386231f75bSLuigi Rizzo }
4396231f75bSLuigi Rizzo 
4406231f75bSLuigi Rizzo void
4416231f75bSLuigi Rizzo g_unregister_classifier(struct g_classifier_hook *hook)
4426231f75bSLuigi Rizzo {
4436231f75bSLuigi Rizzo 	struct g_classifier_hook *entry;
4446231f75bSLuigi Rizzo 
4456231f75bSLuigi Rizzo 	g_bioq_lock(&g_bio_run_down);
4466231f75bSLuigi Rizzo 	TAILQ_FOREACH(entry, &g_classifier_tailq, link) {
4476231f75bSLuigi Rizzo 		if (entry == hook) {
4486231f75bSLuigi Rizzo 			TAILQ_REMOVE(&g_classifier_tailq, hook, link);
4496231f75bSLuigi Rizzo 			break;
4506231f75bSLuigi Rizzo 		}
4516231f75bSLuigi Rizzo 	}
4526231f75bSLuigi Rizzo 	g_bioq_unlock(&g_bio_run_down);
4536231f75bSLuigi Rizzo }
4546231f75bSLuigi Rizzo 
4556231f75bSLuigi Rizzo static void
4566231f75bSLuigi Rizzo g_run_classifiers(struct bio *bp)
4576231f75bSLuigi Rizzo {
4586231f75bSLuigi Rizzo 	struct g_classifier_hook *hook;
4596231f75bSLuigi Rizzo 	int classified = 0;
4606231f75bSLuigi Rizzo 
4616231f75bSLuigi Rizzo 	TAILQ_FOREACH(hook, &g_classifier_tailq, link)
4626231f75bSLuigi Rizzo 		classified |= hook->func(hook->arg, bp);
4636231f75bSLuigi Rizzo 
4646231f75bSLuigi Rizzo 	if (!classified)
4656231f75bSLuigi Rizzo 		bp->bio_classifier1 = BIO_NOTCLASSIFIED;
4666231f75bSLuigi Rizzo }
4676231f75bSLuigi Rizzo 
468dd84a43cSPoul-Henning Kamp void
469dd84a43cSPoul-Henning Kamp g_io_request(struct bio *bp, struct g_consumer *cp)
470dd84a43cSPoul-Henning Kamp {
471801bb689SPoul-Henning Kamp 	struct g_provider *pp;
47240ea77a0SAlexander Motin 	struct mtx *mtxp;
47340ea77a0SAlexander Motin 	int direct, error, first;
474dd84a43cSPoul-Henning Kamp 
475d0e17c1bSPoul-Henning Kamp 	KASSERT(cp != NULL, ("NULL cp in g_io_request"));
476d0e17c1bSPoul-Henning Kamp 	KASSERT(bp != NULL, ("NULL bp in g_io_request"));
477e060b6bdSPoul-Henning Kamp 	pp = cp->provider;
478801bb689SPoul-Henning Kamp 	KASSERT(pp != NULL, ("consumer not attached in g_io_request"));
47992ee312dSPawel Jakub Dawidek #ifdef DIAGNOSTIC
48092ee312dSPawel Jakub Dawidek 	KASSERT(bp->bio_driver1 == NULL,
48192ee312dSPawel Jakub Dawidek 	    ("bio_driver1 used by the consumer (geom %s)", cp->geom->name));
48292ee312dSPawel Jakub Dawidek 	KASSERT(bp->bio_driver2 == NULL,
48392ee312dSPawel Jakub Dawidek 	    ("bio_driver2 used by the consumer (geom %s)", cp->geom->name));
48492ee312dSPawel Jakub Dawidek 	KASSERT(bp->bio_pflags == 0,
48592ee312dSPawel Jakub Dawidek 	    ("bio_pflags used by the consumer (geom %s)", cp->geom->name));
48692ee312dSPawel Jakub Dawidek 	/*
48792ee312dSPawel Jakub Dawidek 	 * Remember consumer's private fields, so we can detect if they were
48892ee312dSPawel Jakub Dawidek 	 * modified by the provider.
48992ee312dSPawel Jakub Dawidek 	 */
49092ee312dSPawel Jakub Dawidek 	bp->_bio_caller1 = bp->bio_caller1;
49192ee312dSPawel Jakub Dawidek 	bp->_bio_caller2 = bp->bio_caller2;
49292ee312dSPawel Jakub Dawidek 	bp->_bio_cflags = bp->bio_cflags;
49392ee312dSPawel Jakub Dawidek #endif
494801bb689SPoul-Henning Kamp 
4951ded77b2SPawel Jakub Dawidek 	if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_GETATTR)) {
496c3618c65SPawel Jakub Dawidek 		KASSERT(bp->bio_data != NULL,
4971ded77b2SPawel Jakub Dawidek 		    ("NULL bp->data in g_io_request(cmd=%hhu)", bp->bio_cmd));
4981ded77b2SPawel Jakub Dawidek 	}
4991ded77b2SPawel Jakub Dawidek 	if (bp->bio_cmd & (BIO_DELETE|BIO_FLUSH)) {
5001ded77b2SPawel Jakub Dawidek 		KASSERT(bp->bio_data == NULL,
5011ded77b2SPawel Jakub Dawidek 		    ("non-NULL bp->data in g_io_request(cmd=%hhu)",
5021ded77b2SPawel Jakub Dawidek 		    bp->bio_cmd));
503c3618c65SPawel Jakub Dawidek 	}
504dcbd0fe5SPoul-Henning Kamp 	if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) {
505dcbd0fe5SPoul-Henning Kamp 		KASSERT(bp->bio_offset % cp->provider->sectorsize == 0,
506dcbd0fe5SPoul-Henning Kamp 		    ("wrong offset %jd for sectorsize %u",
507dcbd0fe5SPoul-Henning Kamp 		    bp->bio_offset, cp->provider->sectorsize));
508dcbd0fe5SPoul-Henning Kamp 		KASSERT(bp->bio_length % cp->provider->sectorsize == 0,
509dcbd0fe5SPoul-Henning Kamp 		    ("wrong length %jd for sectorsize %u",
510dcbd0fe5SPoul-Henning Kamp 		    bp->bio_length, cp->provider->sectorsize));
511dcbd0fe5SPoul-Henning Kamp 	}
512dcbd0fe5SPoul-Henning Kamp 
513f7717523SStephan Uphoff 	g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
514f7717523SStephan Uphoff 	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd);
515f7717523SStephan Uphoff 
516dd84a43cSPoul-Henning Kamp 	bp->bio_from = cp;
517801bb689SPoul-Henning Kamp 	bp->bio_to = pp;
5182fccec19SPoul-Henning Kamp 	bp->bio_error = 0;
5192fccec19SPoul-Henning Kamp 	bp->bio_completed = 0;
520dd84a43cSPoul-Henning Kamp 
52119fa21aaSPoul-Henning Kamp 	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
52219fa21aaSPoul-Henning Kamp 	    ("Bio already on queue bp=%p", bp));
52340ea77a0SAlexander Motin 	if ((g_collectstats & G_STATS_CONSUMERS) != 0 ||
52440ea77a0SAlexander Motin 	    ((g_collectstats & G_STATS_PROVIDERS) != 0 && pp->stat != NULL))
52519fa21aaSPoul-Henning Kamp 		binuptime(&bp->bio_t0);
526a5be8eb5SAlexander Motin 	else
527a5be8eb5SAlexander Motin 		getbinuptime(&bp->bio_t0);
5288827c821SPoul-Henning Kamp 
52940ea77a0SAlexander Motin #ifdef GET_STACK_USAGE
530347e9d54SKonstantin Belousov 	direct = (cp->flags & G_CF_DIRECT_SEND) != 0 &&
531347e9d54SKonstantin Belousov 	    (pp->flags & G_PF_DIRECT_RECEIVE) != 0 &&
53240ea77a0SAlexander Motin 	    !g_is_geom_thread(curthread) &&
5339b349650SKonstantin Belousov 	    ((pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ||
534*3f2e5b85SWarner Losh 	    (bp->bio_flags & BIO_UNMAPPED) == 0 || THREAD_CAN_SLEEP()) &&
535*3f2e5b85SWarner Losh 	    pace == 0;
53640ea77a0SAlexander Motin 	if (direct) {
53740ea77a0SAlexander Motin 		/* Block direct execution if less then half of stack left. */
53840ea77a0SAlexander Motin 		size_t	st, su;
53940ea77a0SAlexander Motin 		GET_STACK_USAGE(st, su);
54040ea77a0SAlexander Motin 		if (su * 2 > st)
54140ea77a0SAlexander Motin 			direct = 0;
54240ea77a0SAlexander Motin 	}
54340ea77a0SAlexander Motin #else
54440ea77a0SAlexander Motin 	direct = 0;
54540ea77a0SAlexander Motin #endif
54640ea77a0SAlexander Motin 
54740ea77a0SAlexander Motin 	if (!TAILQ_EMPTY(&g_classifier_tailq) && !bp->bio_classifier1) {
54840ea77a0SAlexander Motin 		g_bioq_lock(&g_bio_run_down);
54940ea77a0SAlexander Motin 		g_run_classifiers(bp);
55040ea77a0SAlexander Motin 		g_bioq_unlock(&g_bio_run_down);
55140ea77a0SAlexander Motin 	}
55240ea77a0SAlexander Motin 
5538827c821SPoul-Henning Kamp 	/*
5548827c821SPoul-Henning Kamp 	 * The statistics collection is lockless, as such, but we
5558827c821SPoul-Henning Kamp 	 * can not update one instance of the statistics from more
5568827c821SPoul-Henning Kamp 	 * than one thread at a time, so grab the lock first.
5578827c821SPoul-Henning Kamp 	 */
55840ea77a0SAlexander Motin 	mtxp = mtx_pool_find(mtxpool_sleep, pp);
55940ea77a0SAlexander Motin 	mtx_lock(mtxp);
56040ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_PROVIDERS)
56119fa21aaSPoul-Henning Kamp 		devstat_start_transaction(pp->stat, &bp->bio_t0);
56240ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_CONSUMERS)
56319fa21aaSPoul-Henning Kamp 		devstat_start_transaction(cp->stat, &bp->bio_t0);
56419fa21aaSPoul-Henning Kamp 	pp->nstart++;
565cf457284SPoul-Henning Kamp 	cp->nstart++;
56640ea77a0SAlexander Motin 	mtx_unlock(mtxp);
56740ea77a0SAlexander Motin 
56840ea77a0SAlexander Motin 	if (direct) {
56940ea77a0SAlexander Motin 		error = g_io_check(bp);
57040ea77a0SAlexander Motin 		if (error >= 0) {
57140ea77a0SAlexander Motin 			CTR3(KTR_GEOM, "g_io_request g_io_check on bp %p "
57240ea77a0SAlexander Motin 			    "provider %s returned %d", bp, bp->bio_to->name,
57340ea77a0SAlexander Motin 			    error);
57440ea77a0SAlexander Motin 			g_io_deliver(bp, error);
57540ea77a0SAlexander Motin 			return;
57640ea77a0SAlexander Motin 		}
57740ea77a0SAlexander Motin 		bp->bio_to->geom->start(bp);
57840ea77a0SAlexander Motin 	} else {
57940ea77a0SAlexander Motin 		g_bioq_lock(&g_bio_run_down);
5800d883b11SAlexander Motin 		first = TAILQ_EMPTY(&g_bio_run_down.bio_queue);
58119fa21aaSPoul-Henning Kamp 		TAILQ_INSERT_TAIL(&g_bio_run_down.bio_queue, bp, bio_queue);
58240ea77a0SAlexander Motin 		bp->bio_flags |= BIO_ONQUEUE;
58319fa21aaSPoul-Henning Kamp 		g_bio_run_down.bio_queue_length++;
58419fa21aaSPoul-Henning Kamp 		g_bioq_unlock(&g_bio_run_down);
5852fccec19SPoul-Henning Kamp 		/* Pass it on down. */
5860d883b11SAlexander Motin 		if (first)
587dd84a43cSPoul-Henning Kamp 			wakeup(&g_wait_down);
588dd84a43cSPoul-Henning Kamp 	}
58940ea77a0SAlexander Motin }
590dd84a43cSPoul-Henning Kamp 
591dd84a43cSPoul-Henning Kamp void
59272840432SPoul-Henning Kamp g_io_deliver(struct bio *bp, int error)
593dd84a43cSPoul-Henning Kamp {
594e431d66cSAlexander Motin 	struct bintime now;
595801bb689SPoul-Henning Kamp 	struct g_consumer *cp;
596801bb689SPoul-Henning Kamp 	struct g_provider *pp;
59740ea77a0SAlexander Motin 	struct mtx *mtxp;
59840ea77a0SAlexander Motin 	int direct, first;
599dd84a43cSPoul-Henning Kamp 
600e060b6bdSPoul-Henning Kamp 	KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
601801bb689SPoul-Henning Kamp 	pp = bp->bio_to;
602f7eeab17SPoul-Henning Kamp 	KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver"));
603f7eeab17SPoul-Henning Kamp 	cp = bp->bio_from;
604f7eeab17SPoul-Henning Kamp 	if (cp == NULL) {
605f7eeab17SPoul-Henning Kamp 		bp->bio_error = error;
606f7eeab17SPoul-Henning Kamp 		bp->bio_done(bp);
607f7eeab17SPoul-Henning Kamp 		return;
608f7eeab17SPoul-Henning Kamp 	}
609801bb689SPoul-Henning Kamp 	KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver"));
610801bb689SPoul-Henning Kamp 	KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
611fb231f36SEdward Tomasz Napierala #ifdef DIAGNOSTIC
612fb231f36SEdward Tomasz Napierala 	/*
613fb231f36SEdward Tomasz Napierala 	 * Some classes - GJournal in particular - can modify bio's
614fb231f36SEdward Tomasz Napierala 	 * private fields while the bio is in transit; G_GEOM_VOLATILE_BIO
615fb231f36SEdward Tomasz Napierala 	 * flag means it's an expected behaviour for that particular geom.
616fb231f36SEdward Tomasz Napierala 	 */
617fb231f36SEdward Tomasz Napierala 	if ((cp->geom->flags & G_GEOM_VOLATILE_BIO) == 0) {
618fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_caller1 == bp->_bio_caller1,
619fb231f36SEdward Tomasz Napierala 		    ("bio_caller1 used by the provider %s", pp->name));
620fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_caller2 == bp->_bio_caller2,
621fb231f36SEdward Tomasz Napierala 		    ("bio_caller2 used by the provider %s", pp->name));
622fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_cflags == bp->_bio_cflags,
623fb231f36SEdward Tomasz Napierala 		    ("bio_cflags used by the provider %s", pp->name));
624fb231f36SEdward Tomasz Napierala 	}
625fb231f36SEdward Tomasz Napierala #endif
62646aeebecSPawel Jakub Dawidek 	KASSERT(bp->bio_completed >= 0, ("bio_completed can't be less than 0"));
62746aeebecSPawel Jakub Dawidek 	KASSERT(bp->bio_completed <= bp->bio_length,
62846aeebecSPawel Jakub Dawidek 	    ("bio_completed can't be greater than bio_length"));
6295ab413bfSPoul-Henning Kamp 
630dd84a43cSPoul-Henning Kamp 	g_trace(G_T_BIO,
6310355b86eSPoul-Henning Kamp "g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
632801bb689SPoul-Henning Kamp 	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error,
6330355b86eSPoul-Henning Kamp 	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
634801bb689SPoul-Henning Kamp 
63519fa21aaSPoul-Henning Kamp 	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
63619fa21aaSPoul-Henning Kamp 	    ("Bio already on queue bp=%p", bp));
63719fa21aaSPoul-Henning Kamp 
638dcbd0fe5SPoul-Henning Kamp 	/*
639dcbd0fe5SPoul-Henning Kamp 	 * XXX: next two doesn't belong here
640dcbd0fe5SPoul-Henning Kamp 	 */
641e24cbd90SPoul-Henning Kamp 	bp->bio_bcount = bp->bio_length;
642e24cbd90SPoul-Henning Kamp 	bp->bio_resid = bp->bio_bcount - bp->bio_completed;
64319fa21aaSPoul-Henning Kamp 
64440ea77a0SAlexander Motin #ifdef GET_STACK_USAGE
64540ea77a0SAlexander Motin 	direct = (pp->flags & G_PF_DIRECT_SEND) &&
64640ea77a0SAlexander Motin 		 (cp->flags & G_CF_DIRECT_RECEIVE) &&
64740ea77a0SAlexander Motin 		 !g_is_geom_thread(curthread);
64840ea77a0SAlexander Motin 	if (direct) {
64940ea77a0SAlexander Motin 		/* Block direct execution if less then half of stack left. */
65040ea77a0SAlexander Motin 		size_t	st, su;
65140ea77a0SAlexander Motin 		GET_STACK_USAGE(st, su);
65240ea77a0SAlexander Motin 		if (su * 2 > st)
65340ea77a0SAlexander Motin 			direct = 0;
65440ea77a0SAlexander Motin 	}
65540ea77a0SAlexander Motin #else
65640ea77a0SAlexander Motin 	direct = 0;
65740ea77a0SAlexander Motin #endif
65840ea77a0SAlexander Motin 
6598827c821SPoul-Henning Kamp 	/*
6608827c821SPoul-Henning Kamp 	 * The statistics collection is lockless, as such, but we
6618827c821SPoul-Henning Kamp 	 * can not update one instance of the statistics from more
6628827c821SPoul-Henning Kamp 	 * than one thread at a time, so grab the lock first.
6638827c821SPoul-Henning Kamp 	 */
66440ea77a0SAlexander Motin 	if ((g_collectstats & G_STATS_CONSUMERS) != 0 ||
66540ea77a0SAlexander Motin 	    ((g_collectstats & G_STATS_PROVIDERS) != 0 && pp->stat != NULL))
666e431d66cSAlexander Motin 		binuptime(&now);
66740ea77a0SAlexander Motin 	mtxp = mtx_pool_find(mtxpool_sleep, cp);
66840ea77a0SAlexander Motin 	mtx_lock(mtxp);
66940ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_PROVIDERS)
670e431d66cSAlexander Motin 		devstat_end_transaction_bio_bt(pp->stat, bp, &now);
67140ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_CONSUMERS)
672e431d66cSAlexander Motin 		devstat_end_transaction_bio_bt(cp->stat, bp, &now);
673c6ae9b5fSPoul-Henning Kamp 	cp->nend++;
674c6ae9b5fSPoul-Henning Kamp 	pp->nend++;
67540ea77a0SAlexander Motin 	mtx_unlock(mtxp);
67640ea77a0SAlexander Motin 
67719fa21aaSPoul-Henning Kamp 	if (error != ENOMEM) {
67819fa21aaSPoul-Henning Kamp 		bp->bio_error = error;
67940ea77a0SAlexander Motin 		if (direct) {
68040ea77a0SAlexander Motin 			biodone(bp);
68140ea77a0SAlexander Motin 		} else {
68240ea77a0SAlexander Motin 			g_bioq_lock(&g_bio_run_up);
6830d883b11SAlexander Motin 			first = TAILQ_EMPTY(&g_bio_run_up.bio_queue);
68419fa21aaSPoul-Henning Kamp 			TAILQ_INSERT_TAIL(&g_bio_run_up.bio_queue, bp, bio_queue);
685276f72c5SPoul-Henning Kamp 			bp->bio_flags |= BIO_ONQUEUE;
68619fa21aaSPoul-Henning Kamp 			g_bio_run_up.bio_queue_length++;
68719fa21aaSPoul-Henning Kamp 			g_bioq_unlock(&g_bio_run_up);
6880d883b11SAlexander Motin 			if (first)
68919fa21aaSPoul-Henning Kamp 				wakeup(&g_wait_up);
69040ea77a0SAlexander Motin 		}
69119fa21aaSPoul-Henning Kamp 		return;
69219fa21aaSPoul-Henning Kamp 	}
693dd84a43cSPoul-Henning Kamp 
6942cc9686eSPoul-Henning Kamp 	if (bootverbose)
695801bb689SPoul-Henning Kamp 		printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name);
6961b949c05SPawel Jakub Dawidek 	bp->bio_children = 0;
6971b949c05SPawel Jakub Dawidek 	bp->bio_inbed = 0;
69860114438SPawel Jakub Dawidek 	bp->bio_driver1 = NULL;
69960114438SPawel Jakub Dawidek 	bp->bio_driver2 = NULL;
70060114438SPawel Jakub Dawidek 	bp->bio_pflags = 0;
701801bb689SPoul-Henning Kamp 	g_io_request(bp, cp);
702*3f2e5b85SWarner Losh 	pace = 1;
7033432e4fdSPoul-Henning Kamp 	return;
7043432e4fdSPoul-Henning Kamp }
705dd84a43cSPoul-Henning Kamp 
706ee75e7deSKonstantin Belousov SYSCTL_DECL(_kern_geom);
707ee75e7deSKonstantin Belousov 
708ee75e7deSKonstantin Belousov static long transient_maps;
709ee75e7deSKonstantin Belousov SYSCTL_LONG(_kern_geom, OID_AUTO, transient_maps, CTLFLAG_RD,
710ee75e7deSKonstantin Belousov     &transient_maps, 0,
711ee75e7deSKonstantin Belousov     "Total count of the transient mapping requests");
712ee75e7deSKonstantin Belousov u_int transient_map_retries = 10;
713ee75e7deSKonstantin Belousov SYSCTL_UINT(_kern_geom, OID_AUTO, transient_map_retries, CTLFLAG_RW,
714ee75e7deSKonstantin Belousov     &transient_map_retries, 0,
715ee75e7deSKonstantin Belousov     "Max count of retries used before giving up on creating transient map");
716ee75e7deSKonstantin Belousov int transient_map_hard_failures;
717ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, transient_map_hard_failures, CTLFLAG_RD,
718ee75e7deSKonstantin Belousov     &transient_map_hard_failures, 0,
719ee75e7deSKonstantin Belousov     "Failures to establish the transient mapping due to retry attempts "
720ee75e7deSKonstantin Belousov     "exhausted");
721ee75e7deSKonstantin Belousov int transient_map_soft_failures;
722ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, transient_map_soft_failures, CTLFLAG_RD,
723ee75e7deSKonstantin Belousov     &transient_map_soft_failures, 0,
724ee75e7deSKonstantin Belousov     "Count of retried failures to establish the transient mapping");
725ee75e7deSKonstantin Belousov int inflight_transient_maps;
726ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, inflight_transient_maps, CTLFLAG_RD,
727ee75e7deSKonstantin Belousov     &inflight_transient_maps, 0,
728ee75e7deSKonstantin Belousov     "Current count of the active transient maps");
729ee75e7deSKonstantin Belousov 
730ee75e7deSKonstantin Belousov static int
731ee75e7deSKonstantin Belousov g_io_transient_map_bio(struct bio *bp)
732ee75e7deSKonstantin Belousov {
733ee75e7deSKonstantin Belousov 	vm_offset_t addr;
734ee75e7deSKonstantin Belousov 	long size;
735ee75e7deSKonstantin Belousov 	u_int retried;
736ee75e7deSKonstantin Belousov 
7376c83fce3SKonstantin Belousov 	KASSERT(unmapped_buf_allowed, ("unmapped disabled"));
7386c83fce3SKonstantin Belousov 
739ee75e7deSKonstantin Belousov 	size = round_page(bp->bio_ma_offset + bp->bio_length);
740ee75e7deSKonstantin Belousov 	KASSERT(size / PAGE_SIZE == bp->bio_ma_n, ("Bio too short %p", bp));
741ee75e7deSKonstantin Belousov 	addr = 0;
742ee75e7deSKonstantin Belousov 	retried = 0;
743ee75e7deSKonstantin Belousov 	atomic_add_long(&transient_maps, 1);
744ee75e7deSKonstantin Belousov retry:
7455f518366SJeff Roberson 	if (vmem_alloc(transient_arena, size, M_BESTFIT | M_NOWAIT, &addr)) {
746ee75e7deSKonstantin Belousov 		if (transient_map_retries != 0 &&
747ee75e7deSKonstantin Belousov 		    retried >= transient_map_retries) {
748ee75e7deSKonstantin Belousov 			CTR2(KTR_GEOM, "g_down cannot map bp %p provider %s",
749ee75e7deSKonstantin Belousov 			    bp, bp->bio_to->name);
750ee75e7deSKonstantin Belousov 			atomic_add_int(&transient_map_hard_failures, 1);
75140ea77a0SAlexander Motin 			return (EDEADLK/* XXXKIB */);
752ee75e7deSKonstantin Belousov 		} else {
753ee75e7deSKonstantin Belousov 			/*
754ee75e7deSKonstantin Belousov 			 * Naive attempt to quisce the I/O to get more
755ee75e7deSKonstantin Belousov 			 * in-flight requests completed and defragment
7565f518366SJeff Roberson 			 * the transient_arena.
757ee75e7deSKonstantin Belousov 			 */
758ee75e7deSKonstantin Belousov 			CTR3(KTR_GEOM, "g_down retrymap bp %p provider %s r %d",
759ee75e7deSKonstantin Belousov 			    bp, bp->bio_to->name, retried);
760ee75e7deSKonstantin Belousov 			pause("g_d_tra", hz / 10);
761ee75e7deSKonstantin Belousov 			retried++;
762ee75e7deSKonstantin Belousov 			atomic_add_int(&transient_map_soft_failures, 1);
763ee75e7deSKonstantin Belousov 			goto retry;
764ee75e7deSKonstantin Belousov 		}
765ee75e7deSKonstantin Belousov 	}
766ee75e7deSKonstantin Belousov 	atomic_add_int(&inflight_transient_maps, 1);
767ee75e7deSKonstantin Belousov 	pmap_qenter((vm_offset_t)addr, bp->bio_ma, OFF_TO_IDX(size));
768ee75e7deSKonstantin Belousov 	bp->bio_data = (caddr_t)addr + bp->bio_ma_offset;
769ee75e7deSKonstantin Belousov 	bp->bio_flags |= BIO_TRANSIENT_MAPPING;
770ee75e7deSKonstantin Belousov 	bp->bio_flags &= ~BIO_UNMAPPED;
77140ea77a0SAlexander Motin 	return (EJUSTRETURN);
772ee75e7deSKonstantin Belousov }
773ee75e7deSKonstantin Belousov 
774dd84a43cSPoul-Henning Kamp void
775dd84a43cSPoul-Henning Kamp g_io_schedule_down(struct thread *tp __unused)
776dd84a43cSPoul-Henning Kamp {
777dd84a43cSPoul-Henning Kamp 	struct bio *bp;
778e39d70d4SPoul-Henning Kamp 	int error;
779dd84a43cSPoul-Henning Kamp 
780dd84a43cSPoul-Henning Kamp 	for(;;) {
781f0e185d7SPoul-Henning Kamp 		g_bioq_lock(&g_bio_run_down);
782dd84a43cSPoul-Henning Kamp 		bp = g_bioq_first(&g_bio_run_down);
783f0e185d7SPoul-Henning Kamp 		if (bp == NULL) {
78449dbb61dSRobert Watson 			CTR0(KTR_GEOM, "g_down going to sleep");
785f0e185d7SPoul-Henning Kamp 			msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock,
7867fc019afSAlexander Motin 			    PRIBIO | PDROP, "-", 0);
787f0e185d7SPoul-Henning Kamp 			continue;
788f0e185d7SPoul-Henning Kamp 		}
78949dbb61dSRobert Watson 		CTR0(KTR_GEOM, "g_down has work to do");
790f0e185d7SPoul-Henning Kamp 		g_bioq_unlock(&g_bio_run_down);
791*3f2e5b85SWarner Losh 		if (pace != 0) {
792*3f2e5b85SWarner Losh 			/*
793*3f2e5b85SWarner Losh 			 * There has been at least one memory allocation
794*3f2e5b85SWarner Losh 			 * failure since the last I/O completed. Pause 1ms to
795*3f2e5b85SWarner Losh 			 * give the system a chance to free up memory. We only
796*3f2e5b85SWarner Losh 			 * do this once because a large number of allocations
797*3f2e5b85SWarner Losh 			 * can fail in the direct dispatch case and there's no
798*3f2e5b85SWarner Losh 			 * relationship between the number of these failures and
799*3f2e5b85SWarner Losh 			 * the length of the outage. If there's still an outage,
800*3f2e5b85SWarner Losh 			 * we'll pause again and again until it's
801*3f2e5b85SWarner Losh 			 * resolved. Older versions paused longer and once per
802*3f2e5b85SWarner Losh 			 * allocation failure. This was OK for a single threaded
803*3f2e5b85SWarner Losh 			 * g_down, but with direct dispatch would lead to max of
804*3f2e5b85SWarner Losh 			 * 10 IOPs for minutes at a time when transient memory
805*3f2e5b85SWarner Losh 			 * issues prevented allocation for a batch of requests
806*3f2e5b85SWarner Losh 			 * from the upper layers.
807*3f2e5b85SWarner Losh 			 *
808*3f2e5b85SWarner Losh 			 * XXX This pacing is really lame. It needs to be solved
809*3f2e5b85SWarner Losh 			 * by other methods. This is OK only because the worst
810*3f2e5b85SWarner Losh 			 * case scenario is so rare. In the worst case scenario
811*3f2e5b85SWarner Losh 			 * all memory is tied up waiting for I/O to complete
812*3f2e5b85SWarner Losh 			 * which can never happen since we can't allocate bios
813*3f2e5b85SWarner Losh 			 * for that I/O.
814*3f2e5b85SWarner Losh 			 */
815*3f2e5b85SWarner Losh 			CTR0(KTR_GEOM, "g_down pacing self");
816*3f2e5b85SWarner Losh 			pause("g_down", min(hz/1000, 1));
817*3f2e5b85SWarner Losh 			pace = 0;
818376ceb79SPoul-Henning Kamp 		}
81940ea77a0SAlexander Motin 		CTR2(KTR_GEOM, "g_down processing bp %p provider %s", bp,
82040ea77a0SAlexander Motin 		    bp->bio_to->name);
821e39d70d4SPoul-Henning Kamp 		error = g_io_check(bp);
82240ea77a0SAlexander Motin 		if (error >= 0) {
82349dbb61dSRobert Watson 			CTR3(KTR_GEOM, "g_down g_io_check on bp %p provider "
82449dbb61dSRobert Watson 			    "%s returned %d", bp, bp->bio_to->name, error);
825e39d70d4SPoul-Henning Kamp 			g_io_deliver(bp, error);
826e39d70d4SPoul-Henning Kamp 			continue;
827e39d70d4SPoul-Henning Kamp 		}
82851460da8SJohn Baldwin 		THREAD_NO_SLEEPING();
82949dbb61dSRobert Watson 		CTR4(KTR_GEOM, "g_down starting bp %p provider %s off %ld "
83049dbb61dSRobert Watson 		    "len %ld", bp, bp->bio_to->name, bp->bio_offset,
83149dbb61dSRobert Watson 		    bp->bio_length);
832dd84a43cSPoul-Henning Kamp 		bp->bio_to->geom->start(bp);
83351460da8SJohn Baldwin 		THREAD_SLEEPING_OK();
834dd84a43cSPoul-Henning Kamp 	}
835dd84a43cSPoul-Henning Kamp }
836dd84a43cSPoul-Henning Kamp 
837dd84a43cSPoul-Henning Kamp void
8385fcf4e43SPoul-Henning Kamp bio_taskqueue(struct bio *bp, bio_task_t *func, void *arg)
8395fcf4e43SPoul-Henning Kamp {
8405fcf4e43SPoul-Henning Kamp 	bp->bio_task = func;
8415fcf4e43SPoul-Henning Kamp 	bp->bio_task_arg = arg;
8425fcf4e43SPoul-Henning Kamp 	/*
8435fcf4e43SPoul-Henning Kamp 	 * The taskqueue is actually just a second queue off the "up"
8445fcf4e43SPoul-Henning Kamp 	 * queue, so we use the same lock.
8455fcf4e43SPoul-Henning Kamp 	 */
8465fcf4e43SPoul-Henning Kamp 	g_bioq_lock(&g_bio_run_up);
847dcbd0fe5SPoul-Henning Kamp 	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
848dcbd0fe5SPoul-Henning Kamp 	    ("Bio already on queue bp=%p target taskq", bp));
849dcbd0fe5SPoul-Henning Kamp 	bp->bio_flags |= BIO_ONQUEUE;
8505fcf4e43SPoul-Henning Kamp 	TAILQ_INSERT_TAIL(&g_bio_run_task.bio_queue, bp, bio_queue);
8515fcf4e43SPoul-Henning Kamp 	g_bio_run_task.bio_queue_length++;
8525fcf4e43SPoul-Henning Kamp 	wakeup(&g_wait_up);
8535fcf4e43SPoul-Henning Kamp 	g_bioq_unlock(&g_bio_run_up);
8545fcf4e43SPoul-Henning Kamp }
8555fcf4e43SPoul-Henning Kamp 
8565fcf4e43SPoul-Henning Kamp 
8575fcf4e43SPoul-Henning Kamp void
858dd84a43cSPoul-Henning Kamp g_io_schedule_up(struct thread *tp __unused)
859dd84a43cSPoul-Henning Kamp {
860dd84a43cSPoul-Henning Kamp 	struct bio *bp;
861dd84a43cSPoul-Henning Kamp 	for(;;) {
862f0e185d7SPoul-Henning Kamp 		g_bioq_lock(&g_bio_run_up);
8635fcf4e43SPoul-Henning Kamp 		bp = g_bioq_first(&g_bio_run_task);
8645fcf4e43SPoul-Henning Kamp 		if (bp != NULL) {
8655fcf4e43SPoul-Henning Kamp 			g_bioq_unlock(&g_bio_run_up);
86651460da8SJohn Baldwin 			THREAD_NO_SLEEPING();
86749dbb61dSRobert Watson 			CTR1(KTR_GEOM, "g_up processing task bp %p", bp);
8685fcf4e43SPoul-Henning Kamp 			bp->bio_task(bp->bio_task_arg);
86951460da8SJohn Baldwin 			THREAD_SLEEPING_OK();
8705fcf4e43SPoul-Henning Kamp 			continue;
8715fcf4e43SPoul-Henning Kamp 		}
872dd84a43cSPoul-Henning Kamp 		bp = g_bioq_first(&g_bio_run_up);
873f0e185d7SPoul-Henning Kamp 		if (bp != NULL) {
874f0e185d7SPoul-Henning Kamp 			g_bioq_unlock(&g_bio_run_up);
87551460da8SJohn Baldwin 			THREAD_NO_SLEEPING();
87649dbb61dSRobert Watson 			CTR4(KTR_GEOM, "g_up biodone bp %p provider %s off "
877c4901b67SSean Bruno 			    "%jd len %ld", bp, bp->bio_to->name,
87849dbb61dSRobert Watson 			    bp->bio_offset, bp->bio_length);
87953706245SPoul-Henning Kamp 			biodone(bp);
88051460da8SJohn Baldwin 			THREAD_SLEEPING_OK();
881f0e185d7SPoul-Henning Kamp 			continue;
882f0e185d7SPoul-Henning Kamp 		}
88349dbb61dSRobert Watson 		CTR0(KTR_GEOM, "g_up going to sleep");
884f0e185d7SPoul-Henning Kamp 		msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock,
8857fc019afSAlexander Motin 		    PRIBIO | PDROP, "-", 0);
886dd84a43cSPoul-Henning Kamp 	}
887dd84a43cSPoul-Henning Kamp }
888dd84a43cSPoul-Henning Kamp 
889dd84a43cSPoul-Henning Kamp void *
890dd84a43cSPoul-Henning Kamp g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
891dd84a43cSPoul-Henning Kamp {
892dd84a43cSPoul-Henning Kamp 	struct bio *bp;
893dd84a43cSPoul-Henning Kamp 	void *ptr;
894dd84a43cSPoul-Henning Kamp 	int errorc;
895dd84a43cSPoul-Henning Kamp 
8968dd5480dSPawel Jakub Dawidek 	KASSERT(length > 0 && length >= cp->provider->sectorsize &&
8978dd5480dSPawel Jakub Dawidek 	    length <= MAXPHYS, ("g_read_data(): invalid length %jd",
8988dd5480dSPawel Jakub Dawidek 	    (intmax_t)length));
8993eb6ffdfSPoul-Henning Kamp 
900a2033c96SPoul-Henning Kamp 	bp = g_alloc_bio();
901dd84a43cSPoul-Henning Kamp 	bp->bio_cmd = BIO_READ;
902dd84a43cSPoul-Henning Kamp 	bp->bio_done = NULL;
903dd84a43cSPoul-Henning Kamp 	bp->bio_offset = offset;
904dd84a43cSPoul-Henning Kamp 	bp->bio_length = length;
905a163d034SWarner Losh 	ptr = g_malloc(length, M_WAITOK);
906dd84a43cSPoul-Henning Kamp 	bp->bio_data = ptr;
907dd84a43cSPoul-Henning Kamp 	g_io_request(bp, cp);
90853706245SPoul-Henning Kamp 	errorc = biowait(bp, "gread");
909dd84a43cSPoul-Henning Kamp 	if (error != NULL)
910dd84a43cSPoul-Henning Kamp 		*error = errorc;
911dd84a43cSPoul-Henning Kamp 	g_destroy_bio(bp);
912dd84a43cSPoul-Henning Kamp 	if (errorc) {
913dd84a43cSPoul-Henning Kamp 		g_free(ptr);
914dd84a43cSPoul-Henning Kamp 		ptr = NULL;
915dd84a43cSPoul-Henning Kamp 	}
916dd84a43cSPoul-Henning Kamp 	return (ptr);
917dd84a43cSPoul-Henning Kamp }
91890b1cd56SPoul-Henning Kamp 
91990b1cd56SPoul-Henning Kamp int
92090b1cd56SPoul-Henning Kamp g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
92190b1cd56SPoul-Henning Kamp {
92290b1cd56SPoul-Henning Kamp 	struct bio *bp;
92390b1cd56SPoul-Henning Kamp 	int error;
92490b1cd56SPoul-Henning Kamp 
9258dd5480dSPawel Jakub Dawidek 	KASSERT(length > 0 && length >= cp->provider->sectorsize &&
9268dd5480dSPawel Jakub Dawidek 	    length <= MAXPHYS, ("g_write_data(): invalid length %jd",
9278dd5480dSPawel Jakub Dawidek 	    (intmax_t)length));
9283eb6ffdfSPoul-Henning Kamp 
929a2033c96SPoul-Henning Kamp 	bp = g_alloc_bio();
93090b1cd56SPoul-Henning Kamp 	bp->bio_cmd = BIO_WRITE;
93190b1cd56SPoul-Henning Kamp 	bp->bio_done = NULL;
93290b1cd56SPoul-Henning Kamp 	bp->bio_offset = offset;
93390b1cd56SPoul-Henning Kamp 	bp->bio_length = length;
93490b1cd56SPoul-Henning Kamp 	bp->bio_data = ptr;
93590b1cd56SPoul-Henning Kamp 	g_io_request(bp, cp);
93690b1cd56SPoul-Henning Kamp 	error = biowait(bp, "gwrite");
93790b1cd56SPoul-Henning Kamp 	g_destroy_bio(bp);
93890b1cd56SPoul-Henning Kamp 	return (error);
93990b1cd56SPoul-Henning Kamp }
94072e33095SPawel Jakub Dawidek 
9412b17fb95SPawel Jakub Dawidek int
9422b17fb95SPawel Jakub Dawidek g_delete_data(struct g_consumer *cp, off_t offset, off_t length)
9432b17fb95SPawel Jakub Dawidek {
9442b17fb95SPawel Jakub Dawidek 	struct bio *bp;
9452b17fb95SPawel Jakub Dawidek 	int error;
9462b17fb95SPawel Jakub Dawidek 
947eed6cda9SPoul-Henning Kamp 	KASSERT(length > 0 && length >= cp->provider->sectorsize,
948eed6cda9SPoul-Henning Kamp 	    ("g_delete_data(): invalid length %jd", (intmax_t)length));
9492b17fb95SPawel Jakub Dawidek 
9502b17fb95SPawel Jakub Dawidek 	bp = g_alloc_bio();
9512b17fb95SPawel Jakub Dawidek 	bp->bio_cmd = BIO_DELETE;
9522b17fb95SPawel Jakub Dawidek 	bp->bio_done = NULL;
9532b17fb95SPawel Jakub Dawidek 	bp->bio_offset = offset;
9542b17fb95SPawel Jakub Dawidek 	bp->bio_length = length;
9552b17fb95SPawel Jakub Dawidek 	bp->bio_data = NULL;
9562b17fb95SPawel Jakub Dawidek 	g_io_request(bp, cp);
9572b17fb95SPawel Jakub Dawidek 	error = biowait(bp, "gdelete");
9582b17fb95SPawel Jakub Dawidek 	g_destroy_bio(bp);
9592b17fb95SPawel Jakub Dawidek 	return (error);
9602b17fb95SPawel Jakub Dawidek }
9612b17fb95SPawel Jakub Dawidek 
96272e33095SPawel Jakub Dawidek void
96372e33095SPawel Jakub Dawidek g_print_bio(struct bio *bp)
96472e33095SPawel Jakub Dawidek {
96572e33095SPawel Jakub Dawidek 	const char *pname, *cmd = NULL;
96672e33095SPawel Jakub Dawidek 
96772e33095SPawel Jakub Dawidek 	if (bp->bio_to != NULL)
96872e33095SPawel Jakub Dawidek 		pname = bp->bio_to->name;
96972e33095SPawel Jakub Dawidek 	else
97072e33095SPawel Jakub Dawidek 		pname = "[unknown]";
97172e33095SPawel Jakub Dawidek 
97272e33095SPawel Jakub Dawidek 	switch (bp->bio_cmd) {
97372e33095SPawel Jakub Dawidek 	case BIO_GETATTR:
97472e33095SPawel Jakub Dawidek 		cmd = "GETATTR";
97572e33095SPawel Jakub Dawidek 		printf("%s[%s(attr=%s)]", pname, cmd, bp->bio_attribute);
97672e33095SPawel Jakub Dawidek 		return;
977c3618c65SPawel Jakub Dawidek 	case BIO_FLUSH:
978c3618c65SPawel Jakub Dawidek 		cmd = "FLUSH";
979c3618c65SPawel Jakub Dawidek 		printf("%s[%s]", pname, cmd);
980c3618c65SPawel Jakub Dawidek 		return;
98172e33095SPawel Jakub Dawidek 	case BIO_READ:
98272e33095SPawel Jakub Dawidek 		cmd = "READ";
9837ce513a5SEdward Tomasz Napierala 		break;
98472e33095SPawel Jakub Dawidek 	case BIO_WRITE:
98572e33095SPawel Jakub Dawidek 		cmd = "WRITE";
9867ce513a5SEdward Tomasz Napierala 		break;
98772e33095SPawel Jakub Dawidek 	case BIO_DELETE:
98872e33095SPawel Jakub Dawidek 		cmd = "DELETE";
9897ce513a5SEdward Tomasz Napierala 		break;
99072e33095SPawel Jakub Dawidek 	default:
99172e33095SPawel Jakub Dawidek 		cmd = "UNKNOWN";
99272e33095SPawel Jakub Dawidek 		printf("%s[%s()]", pname, cmd);
99372e33095SPawel Jakub Dawidek 		return;
99472e33095SPawel Jakub Dawidek 	}
9957ce513a5SEdward Tomasz Napierala 	printf("%s[%s(offset=%jd, length=%jd)]", pname, cmd,
9967ce513a5SEdward Tomasz Napierala 	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
99772e33095SPawel Jakub Dawidek }
998