xref: /freebsd/sys/geom/geom_io.c (revision 8b522bdae65ca1b2e5db20e9aeee0b5acd89e7ed)
1dd84a43cSPoul-Henning Kamp /*-
23728855aSPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
33728855aSPedro F. Giffuni  *
4dd84a43cSPoul-Henning Kamp  * Copyright (c) 2002 Poul-Henning Kamp
5dd84a43cSPoul-Henning Kamp  * Copyright (c) 2002 Networks Associates Technology, Inc.
6ee75e7deSKonstantin Belousov  * Copyright (c) 2013 The FreeBSD Foundation
7dd84a43cSPoul-Henning Kamp  * All rights reserved.
8dd84a43cSPoul-Henning Kamp  *
9dd84a43cSPoul-Henning Kamp  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
10dd84a43cSPoul-Henning Kamp  * and NAI Labs, the Security Research Division of Network Associates, Inc.
11dd84a43cSPoul-Henning Kamp  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
12dd84a43cSPoul-Henning Kamp  * DARPA CHATS research program.
13dd84a43cSPoul-Henning Kamp  *
14ee75e7deSKonstantin Belousov  * Portions of this software were developed by Konstantin Belousov
15ee75e7deSKonstantin Belousov  * under sponsorship from the FreeBSD Foundation.
16ee75e7deSKonstantin Belousov  *
17dd84a43cSPoul-Henning Kamp  * Redistribution and use in source and binary forms, with or without
18dd84a43cSPoul-Henning Kamp  * modification, are permitted provided that the following conditions
19dd84a43cSPoul-Henning Kamp  * are met:
20dd84a43cSPoul-Henning Kamp  * 1. Redistributions of source code must retain the above copyright
21dd84a43cSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer.
22dd84a43cSPoul-Henning Kamp  * 2. Redistributions in binary form must reproduce the above copyright
23dd84a43cSPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer in the
24dd84a43cSPoul-Henning Kamp  *    documentation and/or other materials provided with the distribution.
25dd84a43cSPoul-Henning Kamp  * 3. The names of the authors may not be used to endorse or promote
26dd84a43cSPoul-Henning Kamp  *    products derived from this software without specific prior written
27dd84a43cSPoul-Henning Kamp  *    permission.
28dd84a43cSPoul-Henning Kamp  *
29dd84a43cSPoul-Henning Kamp  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
30dd84a43cSPoul-Henning Kamp  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31dd84a43cSPoul-Henning Kamp  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32dd84a43cSPoul-Henning Kamp  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
33dd84a43cSPoul-Henning Kamp  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34dd84a43cSPoul-Henning Kamp  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35dd84a43cSPoul-Henning Kamp  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36dd84a43cSPoul-Henning Kamp  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37dd84a43cSPoul-Henning Kamp  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38dd84a43cSPoul-Henning Kamp  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39dd84a43cSPoul-Henning Kamp  * SUCH DAMAGE.
40dd84a43cSPoul-Henning Kamp  */
41dd84a43cSPoul-Henning Kamp 
4250b1faefSDavid E. O'Brien #include <sys/cdefs.h>
4350b1faefSDavid E. O'Brien __FBSDID("$FreeBSD$");
44dd84a43cSPoul-Henning Kamp 
45dd84a43cSPoul-Henning Kamp #include <sys/param.h>
46dd84a43cSPoul-Henning Kamp #include <sys/systm.h>
47dd84a43cSPoul-Henning Kamp #include <sys/kernel.h>
48dd84a43cSPoul-Henning Kamp #include <sys/malloc.h>
49dd84a43cSPoul-Henning Kamp #include <sys/bio.h>
5049dbb61dSRobert Watson #include <sys/ktr.h>
5151460da8SJohn Baldwin #include <sys/proc.h>
52ac03832eSConrad Meyer #include <sys/sbuf.h>
533b378147SPawel Jakub Dawidek #include <sys/stack.h>
54ee75e7deSKonstantin Belousov #include <sys/sysctl.h>
555f518366SJeff Roberson #include <sys/vmem.h>
56ac03832eSConrad Meyer #include <machine/stdarg.h>
57dd84a43cSPoul-Henning Kamp 
58dd84a43cSPoul-Henning Kamp #include <sys/errno.h>
59dd84a43cSPoul-Henning Kamp #include <geom/geom.h>
60b1876192SPoul-Henning Kamp #include <geom/geom_int.h>
61e24cbd90SPoul-Henning Kamp #include <sys/devicestat.h>
62dd84a43cSPoul-Henning Kamp 
635ffb2c8bSPoul-Henning Kamp #include <vm/uma.h>
64ee75e7deSKonstantin Belousov #include <vm/vm.h>
65ee75e7deSKonstantin Belousov #include <vm/vm_param.h>
66ee75e7deSKonstantin Belousov #include <vm/vm_kern.h>
67ee75e7deSKonstantin Belousov #include <vm/vm_page.h>
68ee75e7deSKonstantin Belousov #include <vm/vm_object.h>
69ee75e7deSKonstantin Belousov #include <vm/vm_extern.h>
70ee75e7deSKonstantin Belousov #include <vm/vm_map.h>
715ffb2c8bSPoul-Henning Kamp 
7240ea77a0SAlexander Motin static int	g_io_transient_map_bio(struct bio *bp);
7340ea77a0SAlexander Motin 
74dd84a43cSPoul-Henning Kamp static struct g_bioq g_bio_run_down;
75dd84a43cSPoul-Henning Kamp static struct g_bioq g_bio_run_up;
76dd84a43cSPoul-Henning Kamp 
773f2e5b85SWarner Losh /*
783f2e5b85SWarner Losh  * Pace is a hint that we've had some trouble recently allocating
793f2e5b85SWarner Losh  * bios, so we should back off trying to send I/O down the stack
803f2e5b85SWarner Losh  * a bit to let the problem resolve. When pacing, we also turn
813f2e5b85SWarner Losh  * off direct dispatch to also reduce memory pressure from I/Os
823f2e5b85SWarner Losh  * there, at the expxense of some added latency while the memory
833f2e5b85SWarner Losh  * pressures exist. See g_io_schedule_down() for more details
843f2e5b85SWarner Losh  * and limitations.
853f2e5b85SWarner Losh  */
8661322a0aSAlexander Motin static volatile u_int __read_mostly pace;
873f2e5b85SWarner Losh 
8861322a0aSAlexander Motin static uma_zone_t __read_mostly biozone;
893432e4fdSPoul-Henning Kamp 
90dd84a43cSPoul-Henning Kamp #include <machine/atomic.h>
91dd84a43cSPoul-Henning Kamp 
92dd84a43cSPoul-Henning Kamp static void
93dd84a43cSPoul-Henning Kamp g_bioq_lock(struct g_bioq *bq)
94dd84a43cSPoul-Henning Kamp {
95dd84a43cSPoul-Henning Kamp 
96dd84a43cSPoul-Henning Kamp 	mtx_lock(&bq->bio_queue_lock);
97dd84a43cSPoul-Henning Kamp }
98dd84a43cSPoul-Henning Kamp 
99dd84a43cSPoul-Henning Kamp static void
100dd84a43cSPoul-Henning Kamp g_bioq_unlock(struct g_bioq *bq)
101dd84a43cSPoul-Henning Kamp {
102dd84a43cSPoul-Henning Kamp 
103dd84a43cSPoul-Henning Kamp 	mtx_unlock(&bq->bio_queue_lock);
104dd84a43cSPoul-Henning Kamp }
105dd84a43cSPoul-Henning Kamp 
106dd84a43cSPoul-Henning Kamp #if 0
107dd84a43cSPoul-Henning Kamp static void
108dd84a43cSPoul-Henning Kamp g_bioq_destroy(struct g_bioq *bq)
109dd84a43cSPoul-Henning Kamp {
110dd84a43cSPoul-Henning Kamp 
111dd84a43cSPoul-Henning Kamp 	mtx_destroy(&bq->bio_queue_lock);
112dd84a43cSPoul-Henning Kamp }
113dd84a43cSPoul-Henning Kamp #endif
114dd84a43cSPoul-Henning Kamp 
115dd84a43cSPoul-Henning Kamp static void
116dd84a43cSPoul-Henning Kamp g_bioq_init(struct g_bioq *bq)
117dd84a43cSPoul-Henning Kamp {
118dd84a43cSPoul-Henning Kamp 
119dd84a43cSPoul-Henning Kamp 	TAILQ_INIT(&bq->bio_queue);
1206008862bSJohn Baldwin 	mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF);
121dd84a43cSPoul-Henning Kamp }
122dd84a43cSPoul-Henning Kamp 
123dd84a43cSPoul-Henning Kamp static struct bio *
124dd84a43cSPoul-Henning Kamp g_bioq_first(struct g_bioq *bq)
125dd84a43cSPoul-Henning Kamp {
126dd84a43cSPoul-Henning Kamp 	struct bio *bp;
127dd84a43cSPoul-Henning Kamp 
128dd84a43cSPoul-Henning Kamp 	bp = TAILQ_FIRST(&bq->bio_queue);
129dd84a43cSPoul-Henning Kamp 	if (bp != NULL) {
130dcbd0fe5SPoul-Henning Kamp 		KASSERT((bp->bio_flags & BIO_ONQUEUE),
131dcbd0fe5SPoul-Henning Kamp 		    ("Bio not on queue bp=%p target %p", bp, bq));
132dcbd0fe5SPoul-Henning Kamp 		bp->bio_flags &= ~BIO_ONQUEUE;
133dd84a43cSPoul-Henning Kamp 		TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
134dd84a43cSPoul-Henning Kamp 		bq->bio_queue_length--;
135dd84a43cSPoul-Henning Kamp 	}
136dd84a43cSPoul-Henning Kamp 	return (bp);
137dd84a43cSPoul-Henning Kamp }
138dd84a43cSPoul-Henning Kamp 
139dd84a43cSPoul-Henning Kamp struct bio *
140dd84a43cSPoul-Henning Kamp g_new_bio(void)
141dd84a43cSPoul-Henning Kamp {
142dd84a43cSPoul-Henning Kamp 	struct bio *bp;
143dd84a43cSPoul-Henning Kamp 
1445ffb2c8bSPoul-Henning Kamp 	bp = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
1453b378147SPawel Jakub Dawidek #ifdef KTR
146b656c1b8SPawel Jakub Dawidek 	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
1473b378147SPawel Jakub Dawidek 		struct stack st;
1483b378147SPawel Jakub Dawidek 
1493b378147SPawel Jakub Dawidek 		CTR1(KTR_GEOM, "g_new_bio(): %p", bp);
1503b378147SPawel Jakub Dawidek 		stack_save(&st);
15154533f66SConrad Meyer 		CTRSTACK(KTR_GEOM, &st, 3);
1523b378147SPawel Jakub Dawidek 	}
1533b378147SPawel Jakub Dawidek #endif
154dd84a43cSPoul-Henning Kamp 	return (bp);
155dd84a43cSPoul-Henning Kamp }
156dd84a43cSPoul-Henning Kamp 
157a2033c96SPoul-Henning Kamp struct bio *
158a2033c96SPoul-Henning Kamp g_alloc_bio(void)
159a2033c96SPoul-Henning Kamp {
160a2033c96SPoul-Henning Kamp 	struct bio *bp;
161a2033c96SPoul-Henning Kamp 
162a2033c96SPoul-Henning Kamp 	bp = uma_zalloc(biozone, M_WAITOK | M_ZERO);
1633b378147SPawel Jakub Dawidek #ifdef KTR
164b656c1b8SPawel Jakub Dawidek 	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
1653b378147SPawel Jakub Dawidek 		struct stack st;
1663b378147SPawel Jakub Dawidek 
1673b378147SPawel Jakub Dawidek 		CTR1(KTR_GEOM, "g_alloc_bio(): %p", bp);
1683b378147SPawel Jakub Dawidek 		stack_save(&st);
16954533f66SConrad Meyer 		CTRSTACK(KTR_GEOM, &st, 3);
1703b378147SPawel Jakub Dawidek 	}
1713b378147SPawel Jakub Dawidek #endif
172a2033c96SPoul-Henning Kamp 	return (bp);
173a2033c96SPoul-Henning Kamp }
174a2033c96SPoul-Henning Kamp 
175dd84a43cSPoul-Henning Kamp void
176dd84a43cSPoul-Henning Kamp g_destroy_bio(struct bio *bp)
177dd84a43cSPoul-Henning Kamp {
1783b378147SPawel Jakub Dawidek #ifdef KTR
179b656c1b8SPawel Jakub Dawidek 	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
1803b378147SPawel Jakub Dawidek 		struct stack st;
181dd84a43cSPoul-Henning Kamp 
1823b378147SPawel Jakub Dawidek 		CTR1(KTR_GEOM, "g_destroy_bio(): %p", bp);
1833b378147SPawel Jakub Dawidek 		stack_save(&st);
18454533f66SConrad Meyer 		CTRSTACK(KTR_GEOM, &st, 3);
1853b378147SPawel Jakub Dawidek 	}
1863b378147SPawel Jakub Dawidek #endif
1875ffb2c8bSPoul-Henning Kamp 	uma_zfree(biozone, bp);
188dd84a43cSPoul-Henning Kamp }
189dd84a43cSPoul-Henning Kamp 
190dd84a43cSPoul-Henning Kamp struct bio *
191dd84a43cSPoul-Henning Kamp g_clone_bio(struct bio *bp)
192dd84a43cSPoul-Henning Kamp {
193dd84a43cSPoul-Henning Kamp 	struct bio *bp2;
194dd84a43cSPoul-Henning Kamp 
1955ffb2c8bSPoul-Henning Kamp 	bp2 = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
196a1bd3ee2SPoul-Henning Kamp 	if (bp2 != NULL) {
197936cc461SPoul-Henning Kamp 		bp2->bio_parent = bp;
198dd84a43cSPoul-Henning Kamp 		bp2->bio_cmd = bp->bio_cmd;
19982a6ae10SJim Harris 		/*
20082a6ae10SJim Harris 		 *  BIO_ORDERED flag may be used by disk drivers to enforce
20182a6ae10SJim Harris 		 *  ordering restrictions, so this flag needs to be cloned.
202a9934668SKenneth D. Merry 		 *  BIO_UNMAPPED and BIO_VLIST should be inherited, to properly
203a9934668SKenneth D. Merry 		 *  indicate which way the buffer is passed.
20482a6ae10SJim Harris 		 *  Other bio flags are not suitable for cloning.
20582a6ae10SJim Harris 		 */
206a9934668SKenneth D. Merry 		bp2->bio_flags = bp->bio_flags &
207a9934668SKenneth D. Merry 		    (BIO_ORDERED | BIO_UNMAPPED | BIO_VLIST);
208dd84a43cSPoul-Henning Kamp 		bp2->bio_length = bp->bio_length;
209dd84a43cSPoul-Henning Kamp 		bp2->bio_offset = bp->bio_offset;
210dd84a43cSPoul-Henning Kamp 		bp2->bio_data = bp->bio_data;
211ee75e7deSKonstantin Belousov 		bp2->bio_ma = bp->bio_ma;
212ee75e7deSKonstantin Belousov 		bp2->bio_ma_n = bp->bio_ma_n;
213ee75e7deSKonstantin Belousov 		bp2->bio_ma_offset = bp->bio_ma_offset;
214dd84a43cSPoul-Henning Kamp 		bp2->bio_attribute = bp->bio_attribute;
2159a6844d5SKenneth D. Merry 		if (bp->bio_cmd == BIO_ZONE)
2169a6844d5SKenneth D. Merry 			bcopy(&bp->bio_zone, &bp2->bio_zone,
2179a6844d5SKenneth D. Merry 			    sizeof(bp->bio_zone));
2188532d381SConrad Meyer #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
2198532d381SConrad Meyer 		bp2->bio_track_bp = bp->bio_track_bp;
2208532d381SConrad Meyer #endif
221801bb689SPoul-Henning Kamp 		bp->bio_children++;
222a1bd3ee2SPoul-Henning Kamp 	}
2233b378147SPawel Jakub Dawidek #ifdef KTR
224b656c1b8SPawel Jakub Dawidek 	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
2253b378147SPawel Jakub Dawidek 		struct stack st;
2263b378147SPawel Jakub Dawidek 
227ad572235SRuslan Ermilov 		CTR2(KTR_GEOM, "g_clone_bio(%p): %p", bp, bp2);
2283b378147SPawel Jakub Dawidek 		stack_save(&st);
22954533f66SConrad Meyer 		CTRSTACK(KTR_GEOM, &st, 3);
2303b378147SPawel Jakub Dawidek 	}
2313b378147SPawel Jakub Dawidek #endif
232dd84a43cSPoul-Henning Kamp 	return(bp2);
233dd84a43cSPoul-Henning Kamp }
234dd84a43cSPoul-Henning Kamp 
2354bec0ff1SPawel Jakub Dawidek struct bio *
2364bec0ff1SPawel Jakub Dawidek g_duplicate_bio(struct bio *bp)
2374bec0ff1SPawel Jakub Dawidek {
2384bec0ff1SPawel Jakub Dawidek 	struct bio *bp2;
2394bec0ff1SPawel Jakub Dawidek 
2404bec0ff1SPawel Jakub Dawidek 	bp2 = uma_zalloc(biozone, M_WAITOK | M_ZERO);
241a9934668SKenneth D. Merry 	bp2->bio_flags = bp->bio_flags & (BIO_UNMAPPED | BIO_VLIST);
2424bec0ff1SPawel Jakub Dawidek 	bp2->bio_parent = bp;
2434bec0ff1SPawel Jakub Dawidek 	bp2->bio_cmd = bp->bio_cmd;
2444bec0ff1SPawel Jakub Dawidek 	bp2->bio_length = bp->bio_length;
2454bec0ff1SPawel Jakub Dawidek 	bp2->bio_offset = bp->bio_offset;
2464bec0ff1SPawel Jakub Dawidek 	bp2->bio_data = bp->bio_data;
247ee75e7deSKonstantin Belousov 	bp2->bio_ma = bp->bio_ma;
248ee75e7deSKonstantin Belousov 	bp2->bio_ma_n = bp->bio_ma_n;
249ee75e7deSKonstantin Belousov 	bp2->bio_ma_offset = bp->bio_ma_offset;
2504bec0ff1SPawel Jakub Dawidek 	bp2->bio_attribute = bp->bio_attribute;
2514bec0ff1SPawel Jakub Dawidek 	bp->bio_children++;
2524bec0ff1SPawel Jakub Dawidek #ifdef KTR
253b656c1b8SPawel Jakub Dawidek 	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
2544bec0ff1SPawel Jakub Dawidek 		struct stack st;
2554bec0ff1SPawel Jakub Dawidek 
2564bec0ff1SPawel Jakub Dawidek 		CTR2(KTR_GEOM, "g_duplicate_bio(%p): %p", bp, bp2);
2574bec0ff1SPawel Jakub Dawidek 		stack_save(&st);
25854533f66SConrad Meyer 		CTRSTACK(KTR_GEOM, &st, 3);
2594bec0ff1SPawel Jakub Dawidek 	}
2604bec0ff1SPawel Jakub Dawidek #endif
2614bec0ff1SPawel Jakub Dawidek 	return(bp2);
2624bec0ff1SPawel Jakub Dawidek }
2634bec0ff1SPawel Jakub Dawidek 
264dd84a43cSPoul-Henning Kamp void
265c55f5707SWarner Losh g_reset_bio(struct bio *bp)
266c55f5707SWarner Losh {
267c55f5707SWarner Losh 
268bd4c1dd6SWarner Losh 	bzero(bp, sizeof(*bp));
269c55f5707SWarner Losh }
270c55f5707SWarner Losh 
271c55f5707SWarner Losh void
272dd84a43cSPoul-Henning Kamp g_io_init()
273dd84a43cSPoul-Henning Kamp {
274dd84a43cSPoul-Henning Kamp 
275dd84a43cSPoul-Henning Kamp 	g_bioq_init(&g_bio_run_down);
276dd84a43cSPoul-Henning Kamp 	g_bioq_init(&g_bio_run_up);
2775ffb2c8bSPoul-Henning Kamp 	biozone = uma_zcreate("g_bio", sizeof (struct bio),
2785ffb2c8bSPoul-Henning Kamp 	    NULL, NULL,
2795ffb2c8bSPoul-Henning Kamp 	    NULL, NULL,
2805ffb2c8bSPoul-Henning Kamp 	    0, 0);
281dd84a43cSPoul-Henning Kamp }
282dd84a43cSPoul-Henning Kamp 
283dd84a43cSPoul-Henning Kamp int
2840d3f37a8SPoul-Henning Kamp g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr)
285dd84a43cSPoul-Henning Kamp {
286dd84a43cSPoul-Henning Kamp 	struct bio *bp;
287dd84a43cSPoul-Henning Kamp 	int error;
288dd84a43cSPoul-Henning Kamp 
289dd84a43cSPoul-Henning Kamp 	g_trace(G_T_BIO, "bio_getattr(%s)", attr);
290a2033c96SPoul-Henning Kamp 	bp = g_alloc_bio();
291dd84a43cSPoul-Henning Kamp 	bp->bio_cmd = BIO_GETATTR;
292dd84a43cSPoul-Henning Kamp 	bp->bio_done = NULL;
293dd84a43cSPoul-Henning Kamp 	bp->bio_attribute = attr;
294dd84a43cSPoul-Henning Kamp 	bp->bio_length = *len;
295dd84a43cSPoul-Henning Kamp 	bp->bio_data = ptr;
296dd84a43cSPoul-Henning Kamp 	g_io_request(bp, cp);
29753706245SPoul-Henning Kamp 	error = biowait(bp, "ggetattr");
298dd84a43cSPoul-Henning Kamp 	*len = bp->bio_completed;
299dd84a43cSPoul-Henning Kamp 	g_destroy_bio(bp);
300dd84a43cSPoul-Henning Kamp 	return (error);
301dd84a43cSPoul-Henning Kamp }
302dd84a43cSPoul-Henning Kamp 
303c3618c65SPawel Jakub Dawidek int
3049a6844d5SKenneth D. Merry g_io_zonecmd(struct disk_zone_args *zone_args, struct g_consumer *cp)
3059a6844d5SKenneth D. Merry {
3069a6844d5SKenneth D. Merry 	struct bio *bp;
3079a6844d5SKenneth D. Merry 	int error;
3089a6844d5SKenneth D. Merry 
3099a6844d5SKenneth D. Merry 	g_trace(G_T_BIO, "bio_zone(%d)", zone_args->zone_cmd);
3109a6844d5SKenneth D. Merry 	bp = g_alloc_bio();
3119a6844d5SKenneth D. Merry 	bp->bio_cmd = BIO_ZONE;
3129a6844d5SKenneth D. Merry 	bp->bio_done = NULL;
3139a6844d5SKenneth D. Merry 	/*
3149a6844d5SKenneth D. Merry 	 * XXX KDM need to handle report zone data.
3159a6844d5SKenneth D. Merry 	 */
3169a6844d5SKenneth D. Merry 	bcopy(zone_args, &bp->bio_zone, sizeof(*zone_args));
3179a6844d5SKenneth D. Merry 	if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES)
3189a6844d5SKenneth D. Merry 		bp->bio_length =
3199a6844d5SKenneth D. Merry 		    zone_args->zone_params.report.entries_allocated *
3209a6844d5SKenneth D. Merry 		    sizeof(struct disk_zone_rep_entry);
3219a6844d5SKenneth D. Merry 	else
3229a6844d5SKenneth D. Merry 		bp->bio_length = 0;
3239a6844d5SKenneth D. Merry 
3249a6844d5SKenneth D. Merry 	g_io_request(bp, cp);
3259a6844d5SKenneth D. Merry 	error = biowait(bp, "gzone");
3269a6844d5SKenneth D. Merry 	bcopy(&bp->bio_zone, zone_args, sizeof(*zone_args));
3279a6844d5SKenneth D. Merry 	g_destroy_bio(bp);
3289a6844d5SKenneth D. Merry 	return (error);
3299a6844d5SKenneth D. Merry }
3309a6844d5SKenneth D. Merry 
331b182c792SWarner Losh /*
332b182c792SWarner Losh  * Send a BIO_SPEEDUP down the stack. This is used to tell the lower layers that
333b182c792SWarner Losh  * the upper layers have detected a resource shortage. The lower layers are
334b182c792SWarner Losh  * advised to stop delaying I/O that they might be holding for performance
335b182c792SWarner Losh  * reasons and to schedule it (non-trims) or complete it successfully (trims) as
336b182c792SWarner Losh  * quickly as it can. bio_length is the amount of the shortage.  This call
337b182c792SWarner Losh  * should be non-blocking. bio_resid is used to communicate back if the lower
338b182c792SWarner Losh  * layers couldn't find bio_length worth of I/O to schedule or discard. A length
339b182c792SWarner Losh  * of 0 means to do as much as you can (schedule the h/w queues full, discard
340b182c792SWarner Losh  * all trims). flags are a hint from the upper layers to the lower layers what
341b182c792SWarner Losh  * operation should be done.
342b182c792SWarner Losh  */
343b182c792SWarner Losh int
344b182c792SWarner Losh g_io_speedup(size_t shortage, u_int flags, size_t *resid, struct g_consumer *cp)
345b182c792SWarner Losh {
346b182c792SWarner Losh 	struct bio *bp;
347b182c792SWarner Losh 	int error;
348b182c792SWarner Losh 
349b182c792SWarner Losh 	KASSERT((flags & (BIO_SPEEDUP_TRIM | BIO_SPEEDUP_WRITE)) != 0,
350b182c792SWarner Losh 	    ("Invalid flags passed to g_io_speedup: %#x", flags));
351b182c792SWarner Losh 	g_trace(G_T_BIO, "bio_speedup(%s, %zu, %#x)", cp->provider->name,
352b182c792SWarner Losh 	    shortage, flags);
353b182c792SWarner Losh 	bp = g_new_bio();
354b182c792SWarner Losh 	if (bp == NULL)
355b182c792SWarner Losh 		return (ENOMEM);
356b182c792SWarner Losh 	bp->bio_cmd = BIO_SPEEDUP;
357b182c792SWarner Losh 	bp->bio_length = shortage;
358b182c792SWarner Losh 	bp->bio_done = NULL;
359b182c792SWarner Losh 	bp->bio_flags |= flags;
360b182c792SWarner Losh 	g_io_request(bp, cp);
361b182c792SWarner Losh 	error = biowait(bp, "gflush");
362b182c792SWarner Losh 	*resid = bp->bio_resid;
363b182c792SWarner Losh 	g_destroy_bio(bp);
364b182c792SWarner Losh 	return (error);
365b182c792SWarner Losh }
366b182c792SWarner Losh 
3679a6844d5SKenneth D. Merry int
368c3618c65SPawel Jakub Dawidek g_io_flush(struct g_consumer *cp)
369c3618c65SPawel Jakub Dawidek {
370c3618c65SPawel Jakub Dawidek 	struct bio *bp;
371c3618c65SPawel Jakub Dawidek 	int error;
372c3618c65SPawel Jakub Dawidek 
373c3618c65SPawel Jakub Dawidek 	g_trace(G_T_BIO, "bio_flush(%s)", cp->provider->name);
374c3618c65SPawel Jakub Dawidek 	bp = g_alloc_bio();
375c3618c65SPawel Jakub Dawidek 	bp->bio_cmd = BIO_FLUSH;
376f03f7a0cSJustin T. Gibbs 	bp->bio_flags |= BIO_ORDERED;
377c3618c65SPawel Jakub Dawidek 	bp->bio_done = NULL;
378c3618c65SPawel Jakub Dawidek 	bp->bio_attribute = NULL;
379c3618c65SPawel Jakub Dawidek 	bp->bio_offset = cp->provider->mediasize;
380c3618c65SPawel Jakub Dawidek 	bp->bio_length = 0;
381c3618c65SPawel Jakub Dawidek 	bp->bio_data = NULL;
382c3618c65SPawel Jakub Dawidek 	g_io_request(bp, cp);
383c3618c65SPawel Jakub Dawidek 	error = biowait(bp, "gflush");
384c3618c65SPawel Jakub Dawidek 	g_destroy_bio(bp);
385c3618c65SPawel Jakub Dawidek 	return (error);
386c3618c65SPawel Jakub Dawidek }
387c3618c65SPawel Jakub Dawidek 
388e39d70d4SPoul-Henning Kamp static int
389e39d70d4SPoul-Henning Kamp g_io_check(struct bio *bp)
390e39d70d4SPoul-Henning Kamp {
391e39d70d4SPoul-Henning Kamp 	struct g_consumer *cp;
392e39d70d4SPoul-Henning Kamp 	struct g_provider *pp;
39340ea77a0SAlexander Motin 	off_t excess;
39440ea77a0SAlexander Motin 	int error;
395e39d70d4SPoul-Henning Kamp 
3968532d381SConrad Meyer 	biotrack(bp, __func__);
3978532d381SConrad Meyer 
398e39d70d4SPoul-Henning Kamp 	cp = bp->bio_from;
399e39d70d4SPoul-Henning Kamp 	pp = bp->bio_to;
400e39d70d4SPoul-Henning Kamp 
401e39d70d4SPoul-Henning Kamp 	/* Fail if access counters dont allow the operation */
402e39d70d4SPoul-Henning Kamp 	switch(bp->bio_cmd) {
403e39d70d4SPoul-Henning Kamp 	case BIO_READ:
404e39d70d4SPoul-Henning Kamp 	case BIO_GETATTR:
405e39d70d4SPoul-Henning Kamp 		if (cp->acr == 0)
406e39d70d4SPoul-Henning Kamp 			return (EPERM);
407e39d70d4SPoul-Henning Kamp 		break;
408e39d70d4SPoul-Henning Kamp 	case BIO_WRITE:
409e39d70d4SPoul-Henning Kamp 	case BIO_DELETE:
410*8b522bdaSWarner Losh 	case BIO_SPEEDUP:
411c3618c65SPawel Jakub Dawidek 	case BIO_FLUSH:
412e39d70d4SPoul-Henning Kamp 		if (cp->acw == 0)
413e39d70d4SPoul-Henning Kamp 			return (EPERM);
414e39d70d4SPoul-Henning Kamp 		break;
4159a6844d5SKenneth D. Merry 	case BIO_ZONE:
4169a6844d5SKenneth D. Merry 		if ((bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES) ||
4179a6844d5SKenneth D. Merry 		    (bp->bio_zone.zone_cmd == DISK_ZONE_GET_PARAMS)) {
4189a6844d5SKenneth D. Merry 			if (cp->acr == 0)
4199a6844d5SKenneth D. Merry 				return (EPERM);
4209a6844d5SKenneth D. Merry 		} else if (cp->acw == 0)
4219a6844d5SKenneth D. Merry 			return (EPERM);
4229a6844d5SKenneth D. Merry 		break;
423e39d70d4SPoul-Henning Kamp 	default:
424e39d70d4SPoul-Henning Kamp 		return (EPERM);
425e39d70d4SPoul-Henning Kamp 	}
426e39d70d4SPoul-Henning Kamp 	/* if provider is marked for error, don't disturb. */
427e39d70d4SPoul-Henning Kamp 	if (pp->error)
428e39d70d4SPoul-Henning Kamp 		return (pp->error);
4293631c638SAlexander Motin 	if (cp->flags & G_CF_ORPHAN)
4303631c638SAlexander Motin 		return (ENXIO);
431e39d70d4SPoul-Henning Kamp 
432e39d70d4SPoul-Henning Kamp 	switch(bp->bio_cmd) {
433e39d70d4SPoul-Henning Kamp 	case BIO_READ:
434e39d70d4SPoul-Henning Kamp 	case BIO_WRITE:
435e39d70d4SPoul-Henning Kamp 	case BIO_DELETE:
4362a842317SAndriy Gapon 		/* Zero sectorsize or mediasize is probably a lack of media. */
4372a842317SAndriy Gapon 		if (pp->sectorsize == 0 || pp->mediasize == 0)
43843bff1a7SPoul-Henning Kamp 			return (ENXIO);
439e39d70d4SPoul-Henning Kamp 		/* Reject I/O not on sector boundary */
440e39d70d4SPoul-Henning Kamp 		if (bp->bio_offset % pp->sectorsize)
441e39d70d4SPoul-Henning Kamp 			return (EINVAL);
442e39d70d4SPoul-Henning Kamp 		/* Reject I/O not integral sector long */
443e39d70d4SPoul-Henning Kamp 		if (bp->bio_length % pp->sectorsize)
444e39d70d4SPoul-Henning Kamp 			return (EINVAL);
445d1b8bf47SPoul-Henning Kamp 		/* Reject requests before or past the end of media. */
446d1b8bf47SPoul-Henning Kamp 		if (bp->bio_offset < 0)
447d1b8bf47SPoul-Henning Kamp 			return (EIO);
448e39d70d4SPoul-Henning Kamp 		if (bp->bio_offset > pp->mediasize)
449e39d70d4SPoul-Henning Kamp 			return (EIO);
45040ea77a0SAlexander Motin 
45140ea77a0SAlexander Motin 		/* Truncate requests to the end of providers media. */
45240ea77a0SAlexander Motin 		excess = bp->bio_offset + bp->bio_length;
45340ea77a0SAlexander Motin 		if (excess > bp->bio_to->mediasize) {
45440ea77a0SAlexander Motin 			KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
45540ea77a0SAlexander Motin 			    round_page(bp->bio_ma_offset +
45640ea77a0SAlexander Motin 			    bp->bio_length) / PAGE_SIZE == bp->bio_ma_n,
45740ea77a0SAlexander Motin 			    ("excess bio %p too short", bp));
45840ea77a0SAlexander Motin 			excess -= bp->bio_to->mediasize;
45940ea77a0SAlexander Motin 			bp->bio_length -= excess;
46040ea77a0SAlexander Motin 			if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
46140ea77a0SAlexander Motin 				bp->bio_ma_n = round_page(bp->bio_ma_offset +
46240ea77a0SAlexander Motin 				    bp->bio_length) / PAGE_SIZE;
46340ea77a0SAlexander Motin 			}
46440ea77a0SAlexander Motin 			if (excess > 0)
46540ea77a0SAlexander Motin 				CTR3(KTR_GEOM, "g_down truncated bio "
46640ea77a0SAlexander Motin 				    "%p provider %s by %d", bp,
46740ea77a0SAlexander Motin 				    bp->bio_to->name, excess);
46840ea77a0SAlexander Motin 		}
46940ea77a0SAlexander Motin 
47040ea77a0SAlexander Motin 		/* Deliver zero length transfers right here. */
47140ea77a0SAlexander Motin 		if (bp->bio_length == 0) {
47240ea77a0SAlexander Motin 			CTR2(KTR_GEOM, "g_down terminated 0-length "
47340ea77a0SAlexander Motin 			    "bp %p provider %s", bp, bp->bio_to->name);
47440ea77a0SAlexander Motin 			return (0);
47540ea77a0SAlexander Motin 		}
47640ea77a0SAlexander Motin 
47740ea77a0SAlexander Motin 		if ((bp->bio_flags & BIO_UNMAPPED) != 0 &&
47840ea77a0SAlexander Motin 		    (bp->bio_to->flags & G_PF_ACCEPT_UNMAPPED) == 0 &&
47940ea77a0SAlexander Motin 		    (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
48040ea77a0SAlexander Motin 			if ((error = g_io_transient_map_bio(bp)) >= 0)
48140ea77a0SAlexander Motin 				return (error);
48240ea77a0SAlexander Motin 		}
483e39d70d4SPoul-Henning Kamp 		break;
484e39d70d4SPoul-Henning Kamp 	default:
485e39d70d4SPoul-Henning Kamp 		break;
486e39d70d4SPoul-Henning Kamp 	}
48740ea77a0SAlexander Motin 	return (EJUSTRETURN);
488e39d70d4SPoul-Henning Kamp }
489e39d70d4SPoul-Henning Kamp 
490dd84a43cSPoul-Henning Kamp void
491dd84a43cSPoul-Henning Kamp g_io_request(struct bio *bp, struct g_consumer *cp)
492dd84a43cSPoul-Henning Kamp {
493801bb689SPoul-Henning Kamp 	struct g_provider *pp;
49440ea77a0SAlexander Motin 	int direct, error, first;
4958076d204SWarner Losh 	uint8_t cmd;
496dd84a43cSPoul-Henning Kamp 
4978532d381SConrad Meyer 	biotrack(bp, __func__);
4988532d381SConrad Meyer 
499d0e17c1bSPoul-Henning Kamp 	KASSERT(cp != NULL, ("NULL cp in g_io_request"));
500d0e17c1bSPoul-Henning Kamp 	KASSERT(bp != NULL, ("NULL bp in g_io_request"));
501e060b6bdSPoul-Henning Kamp 	pp = cp->provider;
502801bb689SPoul-Henning Kamp 	KASSERT(pp != NULL, ("consumer not attached in g_io_request"));
50392ee312dSPawel Jakub Dawidek #ifdef DIAGNOSTIC
50492ee312dSPawel Jakub Dawidek 	KASSERT(bp->bio_driver1 == NULL,
50592ee312dSPawel Jakub Dawidek 	    ("bio_driver1 used by the consumer (geom %s)", cp->geom->name));
50692ee312dSPawel Jakub Dawidek 	KASSERT(bp->bio_driver2 == NULL,
50792ee312dSPawel Jakub Dawidek 	    ("bio_driver2 used by the consumer (geom %s)", cp->geom->name));
50892ee312dSPawel Jakub Dawidek 	KASSERT(bp->bio_pflags == 0,
50992ee312dSPawel Jakub Dawidek 	    ("bio_pflags used by the consumer (geom %s)", cp->geom->name));
51092ee312dSPawel Jakub Dawidek 	/*
51192ee312dSPawel Jakub Dawidek 	 * Remember consumer's private fields, so we can detect if they were
51292ee312dSPawel Jakub Dawidek 	 * modified by the provider.
51392ee312dSPawel Jakub Dawidek 	 */
51492ee312dSPawel Jakub Dawidek 	bp->_bio_caller1 = bp->bio_caller1;
51592ee312dSPawel Jakub Dawidek 	bp->_bio_caller2 = bp->bio_caller2;
51692ee312dSPawel Jakub Dawidek 	bp->_bio_cflags = bp->bio_cflags;
51792ee312dSPawel Jakub Dawidek #endif
518801bb689SPoul-Henning Kamp 
5198076d204SWarner Losh 	cmd = bp->bio_cmd;
5208076d204SWarner Losh 	if (cmd == BIO_READ || cmd == BIO_WRITE || cmd == BIO_GETATTR) {
521c3618c65SPawel Jakub Dawidek 		KASSERT(bp->bio_data != NULL,
5229a8fa125SWarner Losh 		    ("NULL bp->data in g_io_request(cmd=%hu)", bp->bio_cmd));
5231ded77b2SPawel Jakub Dawidek 	}
5248076d204SWarner Losh 	if (cmd == BIO_DELETE || cmd == BIO_FLUSH) {
5251ded77b2SPawel Jakub Dawidek 		KASSERT(bp->bio_data == NULL,
5269a8fa125SWarner Losh 		    ("non-NULL bp->data in g_io_request(cmd=%hu)",
5271ded77b2SPawel Jakub Dawidek 		    bp->bio_cmd));
528c3618c65SPawel Jakub Dawidek 	}
5298076d204SWarner Losh 	if (cmd == BIO_READ || cmd == BIO_WRITE || cmd == BIO_DELETE) {
530dcbd0fe5SPoul-Henning Kamp 		KASSERT(bp->bio_offset % cp->provider->sectorsize == 0,
531dcbd0fe5SPoul-Henning Kamp 		    ("wrong offset %jd for sectorsize %u",
532dcbd0fe5SPoul-Henning Kamp 		    bp->bio_offset, cp->provider->sectorsize));
533dcbd0fe5SPoul-Henning Kamp 		KASSERT(bp->bio_length % cp->provider->sectorsize == 0,
534dcbd0fe5SPoul-Henning Kamp 		    ("wrong length %jd for sectorsize %u",
535dcbd0fe5SPoul-Henning Kamp 		    bp->bio_length, cp->provider->sectorsize));
536dcbd0fe5SPoul-Henning Kamp 	}
537dcbd0fe5SPoul-Henning Kamp 
538f7717523SStephan Uphoff 	g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
539f7717523SStephan Uphoff 	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd);
540f7717523SStephan Uphoff 
541dd84a43cSPoul-Henning Kamp 	bp->bio_from = cp;
542801bb689SPoul-Henning Kamp 	bp->bio_to = pp;
5432fccec19SPoul-Henning Kamp 	bp->bio_error = 0;
5442fccec19SPoul-Henning Kamp 	bp->bio_completed = 0;
545dd84a43cSPoul-Henning Kamp 
54619fa21aaSPoul-Henning Kamp 	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
54719fa21aaSPoul-Henning Kamp 	    ("Bio already on queue bp=%p", bp));
548024932aaSAlexander Motin 
54940ea77a0SAlexander Motin 	if ((g_collectstats & G_STATS_CONSUMERS) != 0 ||
55040ea77a0SAlexander Motin 	    ((g_collectstats & G_STATS_PROVIDERS) != 0 && pp->stat != NULL))
55119fa21aaSPoul-Henning Kamp 		binuptime(&bp->bio_t0);
552a5be8eb5SAlexander Motin 	else
553a5be8eb5SAlexander Motin 		getbinuptime(&bp->bio_t0);
554024932aaSAlexander Motin 	if (g_collectstats & G_STATS_CONSUMERS)
555024932aaSAlexander Motin 		devstat_start_transaction(cp->stat, &bp->bio_t0);
556024932aaSAlexander Motin 	if (g_collectstats & G_STATS_PROVIDERS)
557024932aaSAlexander Motin 		devstat_start_transaction(pp->stat, &bp->bio_t0);
558024932aaSAlexander Motin #ifdef INVARIANTS
559024932aaSAlexander Motin 	atomic_add_int(&cp->nstart, 1);
560024932aaSAlexander Motin #endif
5618827c821SPoul-Henning Kamp 
56240ea77a0SAlexander Motin #ifdef GET_STACK_USAGE
563347e9d54SKonstantin Belousov 	direct = (cp->flags & G_CF_DIRECT_SEND) != 0 &&
564347e9d54SKonstantin Belousov 	    (pp->flags & G_PF_DIRECT_RECEIVE) != 0 &&
56540ea77a0SAlexander Motin 	    !g_is_geom_thread(curthread) &&
5669b349650SKonstantin Belousov 	    ((pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ||
5673f2e5b85SWarner Losh 	    (bp->bio_flags & BIO_UNMAPPED) == 0 || THREAD_CAN_SLEEP()) &&
5683f2e5b85SWarner Losh 	    pace == 0;
56940ea77a0SAlexander Motin 	if (direct) {
57040ea77a0SAlexander Motin 		/* Block direct execution if less then half of stack left. */
57140ea77a0SAlexander Motin 		size_t	st, su;
57240ea77a0SAlexander Motin 		GET_STACK_USAGE(st, su);
57340ea77a0SAlexander Motin 		if (su * 2 > st)
57440ea77a0SAlexander Motin 			direct = 0;
57540ea77a0SAlexander Motin 	}
57640ea77a0SAlexander Motin #else
57740ea77a0SAlexander Motin 	direct = 0;
57840ea77a0SAlexander Motin #endif
57940ea77a0SAlexander Motin 
58040ea77a0SAlexander Motin 	if (direct) {
58140ea77a0SAlexander Motin 		error = g_io_check(bp);
58240ea77a0SAlexander Motin 		if (error >= 0) {
58340ea77a0SAlexander Motin 			CTR3(KTR_GEOM, "g_io_request g_io_check on bp %p "
58440ea77a0SAlexander Motin 			    "provider %s returned %d", bp, bp->bio_to->name,
58540ea77a0SAlexander Motin 			    error);
58640ea77a0SAlexander Motin 			g_io_deliver(bp, error);
58740ea77a0SAlexander Motin 			return;
58840ea77a0SAlexander Motin 		}
58940ea77a0SAlexander Motin 		bp->bio_to->geom->start(bp);
59040ea77a0SAlexander Motin 	} else {
59140ea77a0SAlexander Motin 		g_bioq_lock(&g_bio_run_down);
5920d883b11SAlexander Motin 		first = TAILQ_EMPTY(&g_bio_run_down.bio_queue);
59319fa21aaSPoul-Henning Kamp 		TAILQ_INSERT_TAIL(&g_bio_run_down.bio_queue, bp, bio_queue);
59440ea77a0SAlexander Motin 		bp->bio_flags |= BIO_ONQUEUE;
59519fa21aaSPoul-Henning Kamp 		g_bio_run_down.bio_queue_length++;
59619fa21aaSPoul-Henning Kamp 		g_bioq_unlock(&g_bio_run_down);
5972fccec19SPoul-Henning Kamp 		/* Pass it on down. */
5980d883b11SAlexander Motin 		if (first)
599dd84a43cSPoul-Henning Kamp 			wakeup(&g_wait_down);
600dd84a43cSPoul-Henning Kamp 	}
60140ea77a0SAlexander Motin }
602dd84a43cSPoul-Henning Kamp 
603dd84a43cSPoul-Henning Kamp void
60472840432SPoul-Henning Kamp g_io_deliver(struct bio *bp, int error)
605dd84a43cSPoul-Henning Kamp {
606e431d66cSAlexander Motin 	struct bintime now;
607801bb689SPoul-Henning Kamp 	struct g_consumer *cp;
608801bb689SPoul-Henning Kamp 	struct g_provider *pp;
60940ea77a0SAlexander Motin 	struct mtx *mtxp;
61040ea77a0SAlexander Motin 	int direct, first;
611dd84a43cSPoul-Henning Kamp 
6128532d381SConrad Meyer 	biotrack(bp, __func__);
6138532d381SConrad Meyer 
614e060b6bdSPoul-Henning Kamp 	KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
615801bb689SPoul-Henning Kamp 	pp = bp->bio_to;
616f7eeab17SPoul-Henning Kamp 	KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver"));
617f7eeab17SPoul-Henning Kamp 	cp = bp->bio_from;
618f7eeab17SPoul-Henning Kamp 	if (cp == NULL) {
619f7eeab17SPoul-Henning Kamp 		bp->bio_error = error;
620f7eeab17SPoul-Henning Kamp 		bp->bio_done(bp);
621f7eeab17SPoul-Henning Kamp 		return;
622f7eeab17SPoul-Henning Kamp 	}
623801bb689SPoul-Henning Kamp 	KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver"));
624801bb689SPoul-Henning Kamp 	KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
625fb231f36SEdward Tomasz Napierala #ifdef DIAGNOSTIC
626fb231f36SEdward Tomasz Napierala 	/*
627fb231f36SEdward Tomasz Napierala 	 * Some classes - GJournal in particular - can modify bio's
628fb231f36SEdward Tomasz Napierala 	 * private fields while the bio is in transit; G_GEOM_VOLATILE_BIO
629fb231f36SEdward Tomasz Napierala 	 * flag means it's an expected behaviour for that particular geom.
630fb231f36SEdward Tomasz Napierala 	 */
631fb231f36SEdward Tomasz Napierala 	if ((cp->geom->flags & G_GEOM_VOLATILE_BIO) == 0) {
632fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_caller1 == bp->_bio_caller1,
633fb231f36SEdward Tomasz Napierala 		    ("bio_caller1 used by the provider %s", pp->name));
634fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_caller2 == bp->_bio_caller2,
635fb231f36SEdward Tomasz Napierala 		    ("bio_caller2 used by the provider %s", pp->name));
636fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_cflags == bp->_bio_cflags,
637fb231f36SEdward Tomasz Napierala 		    ("bio_cflags used by the provider %s", pp->name));
638fb231f36SEdward Tomasz Napierala 	}
639fb231f36SEdward Tomasz Napierala #endif
64046aeebecSPawel Jakub Dawidek 	KASSERT(bp->bio_completed >= 0, ("bio_completed can't be less than 0"));
64146aeebecSPawel Jakub Dawidek 	KASSERT(bp->bio_completed <= bp->bio_length,
64246aeebecSPawel Jakub Dawidek 	    ("bio_completed can't be greater than bio_length"));
6435ab413bfSPoul-Henning Kamp 
644dd84a43cSPoul-Henning Kamp 	g_trace(G_T_BIO,
6450355b86eSPoul-Henning Kamp "g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
646801bb689SPoul-Henning Kamp 	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error,
6470355b86eSPoul-Henning Kamp 	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
648801bb689SPoul-Henning Kamp 
64919fa21aaSPoul-Henning Kamp 	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
65019fa21aaSPoul-Henning Kamp 	    ("Bio already on queue bp=%p", bp));
65119fa21aaSPoul-Henning Kamp 
652dcbd0fe5SPoul-Henning Kamp 	/*
653dcbd0fe5SPoul-Henning Kamp 	 * XXX: next two doesn't belong here
654dcbd0fe5SPoul-Henning Kamp 	 */
655e24cbd90SPoul-Henning Kamp 	bp->bio_bcount = bp->bio_length;
656e24cbd90SPoul-Henning Kamp 	bp->bio_resid = bp->bio_bcount - bp->bio_completed;
65719fa21aaSPoul-Henning Kamp 
65840ea77a0SAlexander Motin #ifdef GET_STACK_USAGE
65940ea77a0SAlexander Motin 	direct = (pp->flags & G_PF_DIRECT_SEND) &&
66040ea77a0SAlexander Motin 		 (cp->flags & G_CF_DIRECT_RECEIVE) &&
66140ea77a0SAlexander Motin 		 !g_is_geom_thread(curthread);
66240ea77a0SAlexander Motin 	if (direct) {
66340ea77a0SAlexander Motin 		/* Block direct execution if less then half of stack left. */
66440ea77a0SAlexander Motin 		size_t	st, su;
66540ea77a0SAlexander Motin 		GET_STACK_USAGE(st, su);
66640ea77a0SAlexander Motin 		if (su * 2 > st)
66740ea77a0SAlexander Motin 			direct = 0;
66840ea77a0SAlexander Motin 	}
66940ea77a0SAlexander Motin #else
67040ea77a0SAlexander Motin 	direct = 0;
67140ea77a0SAlexander Motin #endif
67240ea77a0SAlexander Motin 
6738827c821SPoul-Henning Kamp 	/*
6748827c821SPoul-Henning Kamp 	 * The statistics collection is lockless, as such, but we
6758827c821SPoul-Henning Kamp 	 * can not update one instance of the statistics from more
6768827c821SPoul-Henning Kamp 	 * than one thread at a time, so grab the lock first.
6778827c821SPoul-Henning Kamp 	 */
67840ea77a0SAlexander Motin 	if ((g_collectstats & G_STATS_CONSUMERS) != 0 ||
67940ea77a0SAlexander Motin 	    ((g_collectstats & G_STATS_PROVIDERS) != 0 && pp->stat != NULL))
680e431d66cSAlexander Motin 		binuptime(&now);
68140ea77a0SAlexander Motin 	mtxp = mtx_pool_find(mtxpool_sleep, cp);
68240ea77a0SAlexander Motin 	mtx_lock(mtxp);
68340ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_PROVIDERS)
684e431d66cSAlexander Motin 		devstat_end_transaction_bio_bt(pp->stat, bp, &now);
68540ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_CONSUMERS)
686e431d66cSAlexander Motin 		devstat_end_transaction_bio_bt(cp->stat, bp, &now);
6879794a803SAlexander Motin #ifdef INVARIANTS
688c6ae9b5fSPoul-Henning Kamp 	cp->nend++;
6899794a803SAlexander Motin #endif
69040ea77a0SAlexander Motin 	mtx_unlock(mtxp);
69140ea77a0SAlexander Motin 
69219fa21aaSPoul-Henning Kamp 	if (error != ENOMEM) {
69319fa21aaSPoul-Henning Kamp 		bp->bio_error = error;
69440ea77a0SAlexander Motin 		if (direct) {
69540ea77a0SAlexander Motin 			biodone(bp);
69640ea77a0SAlexander Motin 		} else {
69740ea77a0SAlexander Motin 			g_bioq_lock(&g_bio_run_up);
6980d883b11SAlexander Motin 			first = TAILQ_EMPTY(&g_bio_run_up.bio_queue);
69919fa21aaSPoul-Henning Kamp 			TAILQ_INSERT_TAIL(&g_bio_run_up.bio_queue, bp, bio_queue);
700276f72c5SPoul-Henning Kamp 			bp->bio_flags |= BIO_ONQUEUE;
70119fa21aaSPoul-Henning Kamp 			g_bio_run_up.bio_queue_length++;
70219fa21aaSPoul-Henning Kamp 			g_bioq_unlock(&g_bio_run_up);
7030d883b11SAlexander Motin 			if (first)
70419fa21aaSPoul-Henning Kamp 				wakeup(&g_wait_up);
70540ea77a0SAlexander Motin 		}
70619fa21aaSPoul-Henning Kamp 		return;
70719fa21aaSPoul-Henning Kamp 	}
708dd84a43cSPoul-Henning Kamp 
7092cc9686eSPoul-Henning Kamp 	if (bootverbose)
710801bb689SPoul-Henning Kamp 		printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name);
7111b949c05SPawel Jakub Dawidek 	bp->bio_children = 0;
7121b949c05SPawel Jakub Dawidek 	bp->bio_inbed = 0;
71360114438SPawel Jakub Dawidek 	bp->bio_driver1 = NULL;
71460114438SPawel Jakub Dawidek 	bp->bio_driver2 = NULL;
71560114438SPawel Jakub Dawidek 	bp->bio_pflags = 0;
716801bb689SPoul-Henning Kamp 	g_io_request(bp, cp);
7173f2e5b85SWarner Losh 	pace = 1;
7183432e4fdSPoul-Henning Kamp 	return;
7193432e4fdSPoul-Henning Kamp }
720dd84a43cSPoul-Henning Kamp 
721ee75e7deSKonstantin Belousov SYSCTL_DECL(_kern_geom);
722ee75e7deSKonstantin Belousov 
723ee75e7deSKonstantin Belousov static long transient_maps;
724ee75e7deSKonstantin Belousov SYSCTL_LONG(_kern_geom, OID_AUTO, transient_maps, CTLFLAG_RD,
725ee75e7deSKonstantin Belousov     &transient_maps, 0,
726ee75e7deSKonstantin Belousov     "Total count of the transient mapping requests");
727ee75e7deSKonstantin Belousov u_int transient_map_retries = 10;
728ee75e7deSKonstantin Belousov SYSCTL_UINT(_kern_geom, OID_AUTO, transient_map_retries, CTLFLAG_RW,
729ee75e7deSKonstantin Belousov     &transient_map_retries, 0,
730ee75e7deSKonstantin Belousov     "Max count of retries used before giving up on creating transient map");
731ee75e7deSKonstantin Belousov int transient_map_hard_failures;
732ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, transient_map_hard_failures, CTLFLAG_RD,
733ee75e7deSKonstantin Belousov     &transient_map_hard_failures, 0,
734ee75e7deSKonstantin Belousov     "Failures to establish the transient mapping due to retry attempts "
735ee75e7deSKonstantin Belousov     "exhausted");
736ee75e7deSKonstantin Belousov int transient_map_soft_failures;
737ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, transient_map_soft_failures, CTLFLAG_RD,
738ee75e7deSKonstantin Belousov     &transient_map_soft_failures, 0,
739ee75e7deSKonstantin Belousov     "Count of retried failures to establish the transient mapping");
740ee75e7deSKonstantin Belousov int inflight_transient_maps;
741ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, inflight_transient_maps, CTLFLAG_RD,
742ee75e7deSKonstantin Belousov     &inflight_transient_maps, 0,
743ee75e7deSKonstantin Belousov     "Current count of the active transient maps");
744ee75e7deSKonstantin Belousov 
745ee75e7deSKonstantin Belousov static int
746ee75e7deSKonstantin Belousov g_io_transient_map_bio(struct bio *bp)
747ee75e7deSKonstantin Belousov {
748ee75e7deSKonstantin Belousov 	vm_offset_t addr;
749ee75e7deSKonstantin Belousov 	long size;
750ee75e7deSKonstantin Belousov 	u_int retried;
751ee75e7deSKonstantin Belousov 
7526c83fce3SKonstantin Belousov 	KASSERT(unmapped_buf_allowed, ("unmapped disabled"));
7536c83fce3SKonstantin Belousov 
754ee75e7deSKonstantin Belousov 	size = round_page(bp->bio_ma_offset + bp->bio_length);
755ee75e7deSKonstantin Belousov 	KASSERT(size / PAGE_SIZE == bp->bio_ma_n, ("Bio too short %p", bp));
756ee75e7deSKonstantin Belousov 	addr = 0;
757ee75e7deSKonstantin Belousov 	retried = 0;
758ee75e7deSKonstantin Belousov 	atomic_add_long(&transient_maps, 1);
759ee75e7deSKonstantin Belousov retry:
7605f518366SJeff Roberson 	if (vmem_alloc(transient_arena, size, M_BESTFIT | M_NOWAIT, &addr)) {
761ee75e7deSKonstantin Belousov 		if (transient_map_retries != 0 &&
762ee75e7deSKonstantin Belousov 		    retried >= transient_map_retries) {
763ee75e7deSKonstantin Belousov 			CTR2(KTR_GEOM, "g_down cannot map bp %p provider %s",
764ee75e7deSKonstantin Belousov 			    bp, bp->bio_to->name);
765ee75e7deSKonstantin Belousov 			atomic_add_int(&transient_map_hard_failures, 1);
76640ea77a0SAlexander Motin 			return (EDEADLK/* XXXKIB */);
767ee75e7deSKonstantin Belousov 		} else {
768ee75e7deSKonstantin Belousov 			/*
769ee75e7deSKonstantin Belousov 			 * Naive attempt to quisce the I/O to get more
770ee75e7deSKonstantin Belousov 			 * in-flight requests completed and defragment
7715f518366SJeff Roberson 			 * the transient_arena.
772ee75e7deSKonstantin Belousov 			 */
773ee75e7deSKonstantin Belousov 			CTR3(KTR_GEOM, "g_down retrymap bp %p provider %s r %d",
774ee75e7deSKonstantin Belousov 			    bp, bp->bio_to->name, retried);
775ee75e7deSKonstantin Belousov 			pause("g_d_tra", hz / 10);
776ee75e7deSKonstantin Belousov 			retried++;
777ee75e7deSKonstantin Belousov 			atomic_add_int(&transient_map_soft_failures, 1);
778ee75e7deSKonstantin Belousov 			goto retry;
779ee75e7deSKonstantin Belousov 		}
780ee75e7deSKonstantin Belousov 	}
781ee75e7deSKonstantin Belousov 	atomic_add_int(&inflight_transient_maps, 1);
782ee75e7deSKonstantin Belousov 	pmap_qenter((vm_offset_t)addr, bp->bio_ma, OFF_TO_IDX(size));
783ee75e7deSKonstantin Belousov 	bp->bio_data = (caddr_t)addr + bp->bio_ma_offset;
784ee75e7deSKonstantin Belousov 	bp->bio_flags |= BIO_TRANSIENT_MAPPING;
785ee75e7deSKonstantin Belousov 	bp->bio_flags &= ~BIO_UNMAPPED;
78640ea77a0SAlexander Motin 	return (EJUSTRETURN);
787ee75e7deSKonstantin Belousov }
788ee75e7deSKonstantin Belousov 
789dd84a43cSPoul-Henning Kamp void
790dd84a43cSPoul-Henning Kamp g_io_schedule_down(struct thread *tp __unused)
791dd84a43cSPoul-Henning Kamp {
792dd84a43cSPoul-Henning Kamp 	struct bio *bp;
793e39d70d4SPoul-Henning Kamp 	int error;
794dd84a43cSPoul-Henning Kamp 
795dd84a43cSPoul-Henning Kamp 	for(;;) {
796f0e185d7SPoul-Henning Kamp 		g_bioq_lock(&g_bio_run_down);
797dd84a43cSPoul-Henning Kamp 		bp = g_bioq_first(&g_bio_run_down);
798f0e185d7SPoul-Henning Kamp 		if (bp == NULL) {
79949dbb61dSRobert Watson 			CTR0(KTR_GEOM, "g_down going to sleep");
800f0e185d7SPoul-Henning Kamp 			msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock,
8017fc019afSAlexander Motin 			    PRIBIO | PDROP, "-", 0);
802f0e185d7SPoul-Henning Kamp 			continue;
803f0e185d7SPoul-Henning Kamp 		}
80449dbb61dSRobert Watson 		CTR0(KTR_GEOM, "g_down has work to do");
805f0e185d7SPoul-Henning Kamp 		g_bioq_unlock(&g_bio_run_down);
8068532d381SConrad Meyer 		biotrack(bp, __func__);
8073f2e5b85SWarner Losh 		if (pace != 0) {
8083f2e5b85SWarner Losh 			/*
8093f2e5b85SWarner Losh 			 * There has been at least one memory allocation
8103f2e5b85SWarner Losh 			 * failure since the last I/O completed. Pause 1ms to
8113f2e5b85SWarner Losh 			 * give the system a chance to free up memory. We only
8123f2e5b85SWarner Losh 			 * do this once because a large number of allocations
8133f2e5b85SWarner Losh 			 * can fail in the direct dispatch case and there's no
8143f2e5b85SWarner Losh 			 * relationship between the number of these failures and
8153f2e5b85SWarner Losh 			 * the length of the outage. If there's still an outage,
8163f2e5b85SWarner Losh 			 * we'll pause again and again until it's
8173f2e5b85SWarner Losh 			 * resolved. Older versions paused longer and once per
8183f2e5b85SWarner Losh 			 * allocation failure. This was OK for a single threaded
8193f2e5b85SWarner Losh 			 * g_down, but with direct dispatch would lead to max of
8203f2e5b85SWarner Losh 			 * 10 IOPs for minutes at a time when transient memory
8213f2e5b85SWarner Losh 			 * issues prevented allocation for a batch of requests
8223f2e5b85SWarner Losh 			 * from the upper layers.
8233f2e5b85SWarner Losh 			 *
8243f2e5b85SWarner Losh 			 * XXX This pacing is really lame. It needs to be solved
8253f2e5b85SWarner Losh 			 * by other methods. This is OK only because the worst
8263f2e5b85SWarner Losh 			 * case scenario is so rare. In the worst case scenario
8273f2e5b85SWarner Losh 			 * all memory is tied up waiting for I/O to complete
8283f2e5b85SWarner Losh 			 * which can never happen since we can't allocate bios
8293f2e5b85SWarner Losh 			 * for that I/O.
8303f2e5b85SWarner Losh 			 */
8313f2e5b85SWarner Losh 			CTR0(KTR_GEOM, "g_down pacing self");
8323f2e5b85SWarner Losh 			pause("g_down", min(hz/1000, 1));
8333f2e5b85SWarner Losh 			pace = 0;
834376ceb79SPoul-Henning Kamp 		}
83540ea77a0SAlexander Motin 		CTR2(KTR_GEOM, "g_down processing bp %p provider %s", bp,
83640ea77a0SAlexander Motin 		    bp->bio_to->name);
837e39d70d4SPoul-Henning Kamp 		error = g_io_check(bp);
83840ea77a0SAlexander Motin 		if (error >= 0) {
83949dbb61dSRobert Watson 			CTR3(KTR_GEOM, "g_down g_io_check on bp %p provider "
84049dbb61dSRobert Watson 			    "%s returned %d", bp, bp->bio_to->name, error);
841e39d70d4SPoul-Henning Kamp 			g_io_deliver(bp, error);
842e39d70d4SPoul-Henning Kamp 			continue;
843e39d70d4SPoul-Henning Kamp 		}
84451460da8SJohn Baldwin 		THREAD_NO_SLEEPING();
84549dbb61dSRobert Watson 		CTR4(KTR_GEOM, "g_down starting bp %p provider %s off %ld "
84649dbb61dSRobert Watson 		    "len %ld", bp, bp->bio_to->name, bp->bio_offset,
84749dbb61dSRobert Watson 		    bp->bio_length);
848dd84a43cSPoul-Henning Kamp 		bp->bio_to->geom->start(bp);
84951460da8SJohn Baldwin 		THREAD_SLEEPING_OK();
850dd84a43cSPoul-Henning Kamp 	}
851dd84a43cSPoul-Henning Kamp }
852dd84a43cSPoul-Henning Kamp 
853dd84a43cSPoul-Henning Kamp void
854dd84a43cSPoul-Henning Kamp g_io_schedule_up(struct thread *tp __unused)
855dd84a43cSPoul-Henning Kamp {
856dd84a43cSPoul-Henning Kamp 	struct bio *bp;
8570c4440c3SEdward Tomasz Napierala 
858dd84a43cSPoul-Henning Kamp 	for(;;) {
859f0e185d7SPoul-Henning Kamp 		g_bioq_lock(&g_bio_run_up);
8600c4440c3SEdward Tomasz Napierala 		bp = g_bioq_first(&g_bio_run_up);
8610c4440c3SEdward Tomasz Napierala 		if (bp == NULL) {
8620c4440c3SEdward Tomasz Napierala 			CTR0(KTR_GEOM, "g_up going to sleep");
8630c4440c3SEdward Tomasz Napierala 			msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock,
8640c4440c3SEdward Tomasz Napierala 			    PRIBIO | PDROP, "-", 0);
8655fcf4e43SPoul-Henning Kamp 			continue;
8665fcf4e43SPoul-Henning Kamp 		}
867f0e185d7SPoul-Henning Kamp 		g_bioq_unlock(&g_bio_run_up);
86851460da8SJohn Baldwin 		THREAD_NO_SLEEPING();
86949dbb61dSRobert Watson 		CTR4(KTR_GEOM, "g_up biodone bp %p provider %s off "
870c4901b67SSean Bruno 		    "%jd len %ld", bp, bp->bio_to->name,
87149dbb61dSRobert Watson 		    bp->bio_offset, bp->bio_length);
87253706245SPoul-Henning Kamp 		biodone(bp);
87351460da8SJohn Baldwin 		THREAD_SLEEPING_OK();
874dd84a43cSPoul-Henning Kamp 	}
875dd84a43cSPoul-Henning Kamp }
876dd84a43cSPoul-Henning Kamp 
877dd84a43cSPoul-Henning Kamp void *
878dd84a43cSPoul-Henning Kamp g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
879dd84a43cSPoul-Henning Kamp {
880dd84a43cSPoul-Henning Kamp 	struct bio *bp;
881dd84a43cSPoul-Henning Kamp 	void *ptr;
882dd84a43cSPoul-Henning Kamp 	int errorc;
883dd84a43cSPoul-Henning Kamp 
8848dd5480dSPawel Jakub Dawidek 	KASSERT(length > 0 && length >= cp->provider->sectorsize &&
8858dd5480dSPawel Jakub Dawidek 	    length <= MAXPHYS, ("g_read_data(): invalid length %jd",
8868dd5480dSPawel Jakub Dawidek 	    (intmax_t)length));
8873eb6ffdfSPoul-Henning Kamp 
888a2033c96SPoul-Henning Kamp 	bp = g_alloc_bio();
889dd84a43cSPoul-Henning Kamp 	bp->bio_cmd = BIO_READ;
890dd84a43cSPoul-Henning Kamp 	bp->bio_done = NULL;
891dd84a43cSPoul-Henning Kamp 	bp->bio_offset = offset;
892dd84a43cSPoul-Henning Kamp 	bp->bio_length = length;
893a163d034SWarner Losh 	ptr = g_malloc(length, M_WAITOK);
894dd84a43cSPoul-Henning Kamp 	bp->bio_data = ptr;
895dd84a43cSPoul-Henning Kamp 	g_io_request(bp, cp);
89653706245SPoul-Henning Kamp 	errorc = biowait(bp, "gread");
897dd84a43cSPoul-Henning Kamp 	if (error != NULL)
898dd84a43cSPoul-Henning Kamp 		*error = errorc;
899dd84a43cSPoul-Henning Kamp 	g_destroy_bio(bp);
900dd84a43cSPoul-Henning Kamp 	if (errorc) {
901dd84a43cSPoul-Henning Kamp 		g_free(ptr);
902dd84a43cSPoul-Henning Kamp 		ptr = NULL;
903dd84a43cSPoul-Henning Kamp 	}
904dd84a43cSPoul-Henning Kamp 	return (ptr);
905dd84a43cSPoul-Henning Kamp }
90690b1cd56SPoul-Henning Kamp 
907dffce215SKirk McKusick /*
908dffce215SKirk McKusick  * A read function for use by ffs_sbget when used by GEOM-layer routines.
909dffce215SKirk McKusick  */
910dffce215SKirk McKusick int
911dffce215SKirk McKusick g_use_g_read_data(void *devfd, off_t loc, void **bufp, int size)
912dffce215SKirk McKusick {
913dffce215SKirk McKusick 	struct g_consumer *cp;
914dffce215SKirk McKusick 
915efbf3964SKirk McKusick 	KASSERT(*bufp == NULL,
916efbf3964SKirk McKusick 	    ("g_use_g_read_data: non-NULL *bufp %p\n", *bufp));
917efbf3964SKirk McKusick 
918dffce215SKirk McKusick 	cp = (struct g_consumer *)devfd;
919dffce215SKirk McKusick 	/*
920dffce215SKirk McKusick 	 * Take care not to issue an invalid I/O request. The offset of
921dffce215SKirk McKusick 	 * the superblock candidate must be multiples of the provider's
922dffce215SKirk McKusick 	 * sector size, otherwise an FFS can't exist on the provider
923dffce215SKirk McKusick 	 * anyway.
924dffce215SKirk McKusick 	 */
925dffce215SKirk McKusick 	if (loc % cp->provider->sectorsize != 0)
926dffce215SKirk McKusick 		return (ENOENT);
927dffce215SKirk McKusick 	*bufp = g_read_data(cp, loc, size, NULL);
928dffce215SKirk McKusick 	if (*bufp == NULL)
929dffce215SKirk McKusick 		return (ENOENT);
930dffce215SKirk McKusick 	return (0);
931dffce215SKirk McKusick }
932dffce215SKirk McKusick 
93390b1cd56SPoul-Henning Kamp int
93490b1cd56SPoul-Henning Kamp g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
93590b1cd56SPoul-Henning Kamp {
93690b1cd56SPoul-Henning Kamp 	struct bio *bp;
93790b1cd56SPoul-Henning Kamp 	int error;
93890b1cd56SPoul-Henning Kamp 
9398dd5480dSPawel Jakub Dawidek 	KASSERT(length > 0 && length >= cp->provider->sectorsize &&
9408dd5480dSPawel Jakub Dawidek 	    length <= MAXPHYS, ("g_write_data(): invalid length %jd",
9418dd5480dSPawel Jakub Dawidek 	    (intmax_t)length));
9423eb6ffdfSPoul-Henning Kamp 
943a2033c96SPoul-Henning Kamp 	bp = g_alloc_bio();
94490b1cd56SPoul-Henning Kamp 	bp->bio_cmd = BIO_WRITE;
94590b1cd56SPoul-Henning Kamp 	bp->bio_done = NULL;
94690b1cd56SPoul-Henning Kamp 	bp->bio_offset = offset;
94790b1cd56SPoul-Henning Kamp 	bp->bio_length = length;
94890b1cd56SPoul-Henning Kamp 	bp->bio_data = ptr;
94990b1cd56SPoul-Henning Kamp 	g_io_request(bp, cp);
95090b1cd56SPoul-Henning Kamp 	error = biowait(bp, "gwrite");
95190b1cd56SPoul-Henning Kamp 	g_destroy_bio(bp);
95290b1cd56SPoul-Henning Kamp 	return (error);
95390b1cd56SPoul-Henning Kamp }
95472e33095SPawel Jakub Dawidek 
955dffce215SKirk McKusick /*
956dffce215SKirk McKusick  * A write function for use by ffs_sbput when used by GEOM-layer routines.
957dffce215SKirk McKusick  */
958dffce215SKirk McKusick int
959dffce215SKirk McKusick g_use_g_write_data(void *devfd, off_t loc, void *buf, int size)
960dffce215SKirk McKusick {
961dffce215SKirk McKusick 
962dffce215SKirk McKusick 	return (g_write_data((struct g_consumer *)devfd, loc, buf, size));
963dffce215SKirk McKusick }
964dffce215SKirk McKusick 
9652b17fb95SPawel Jakub Dawidek int
9662b17fb95SPawel Jakub Dawidek g_delete_data(struct g_consumer *cp, off_t offset, off_t length)
9672b17fb95SPawel Jakub Dawidek {
9682b17fb95SPawel Jakub Dawidek 	struct bio *bp;
9692b17fb95SPawel Jakub Dawidek 	int error;
9702b17fb95SPawel Jakub Dawidek 
971eed6cda9SPoul-Henning Kamp 	KASSERT(length > 0 && length >= cp->provider->sectorsize,
972eed6cda9SPoul-Henning Kamp 	    ("g_delete_data(): invalid length %jd", (intmax_t)length));
9732b17fb95SPawel Jakub Dawidek 
9742b17fb95SPawel Jakub Dawidek 	bp = g_alloc_bio();
9752b17fb95SPawel Jakub Dawidek 	bp->bio_cmd = BIO_DELETE;
9762b17fb95SPawel Jakub Dawidek 	bp->bio_done = NULL;
9772b17fb95SPawel Jakub Dawidek 	bp->bio_offset = offset;
9782b17fb95SPawel Jakub Dawidek 	bp->bio_length = length;
9792b17fb95SPawel Jakub Dawidek 	bp->bio_data = NULL;
9802b17fb95SPawel Jakub Dawidek 	g_io_request(bp, cp);
9812b17fb95SPawel Jakub Dawidek 	error = biowait(bp, "gdelete");
9822b17fb95SPawel Jakub Dawidek 	g_destroy_bio(bp);
9832b17fb95SPawel Jakub Dawidek 	return (error);
9842b17fb95SPawel Jakub Dawidek }
9852b17fb95SPawel Jakub Dawidek 
98672e33095SPawel Jakub Dawidek void
987ac03832eSConrad Meyer g_print_bio(const char *prefix, const struct bio *bp, const char *fmtsuffix,
988ac03832eSConrad Meyer     ...)
989ac03832eSConrad Meyer {
990ac03832eSConrad Meyer #ifndef PRINTF_BUFR_SIZE
991ac03832eSConrad Meyer #define PRINTF_BUFR_SIZE 64
992ac03832eSConrad Meyer #endif
993ac03832eSConrad Meyer 	char bufr[PRINTF_BUFR_SIZE];
994ac03832eSConrad Meyer 	struct sbuf sb, *sbp __unused;
995ac03832eSConrad Meyer 	va_list ap;
996ac03832eSConrad Meyer 
997ac03832eSConrad Meyer 	sbp = sbuf_new(&sb, bufr, sizeof(bufr), SBUF_FIXEDLEN);
998ac03832eSConrad Meyer 	KASSERT(sbp != NULL, ("sbuf_new misused?"));
999ac03832eSConrad Meyer 
1000ac03832eSConrad Meyer 	sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
1001ac03832eSConrad Meyer 
1002ac03832eSConrad Meyer 	sbuf_cat(&sb, prefix);
1003ac03832eSConrad Meyer 	g_format_bio(&sb, bp);
1004ac03832eSConrad Meyer 
1005ac03832eSConrad Meyer 	va_start(ap, fmtsuffix);
1006ac03832eSConrad Meyer 	sbuf_vprintf(&sb, fmtsuffix, ap);
1007ac03832eSConrad Meyer 	va_end(ap);
1008ac03832eSConrad Meyer 
1009ac03832eSConrad Meyer 	sbuf_nl_terminate(&sb);
1010ac03832eSConrad Meyer 
1011ac03832eSConrad Meyer 	sbuf_finish(&sb);
1012ac03832eSConrad Meyer 	sbuf_delete(&sb);
1013ac03832eSConrad Meyer }
1014ac03832eSConrad Meyer 
1015ac03832eSConrad Meyer void
1016ac03832eSConrad Meyer g_format_bio(struct sbuf *sb, const struct bio *bp)
101772e33095SPawel Jakub Dawidek {
101872e33095SPawel Jakub Dawidek 	const char *pname, *cmd = NULL;
101972e33095SPawel Jakub Dawidek 
102072e33095SPawel Jakub Dawidek 	if (bp->bio_to != NULL)
102172e33095SPawel Jakub Dawidek 		pname = bp->bio_to->name;
102272e33095SPawel Jakub Dawidek 	else
102372e33095SPawel Jakub Dawidek 		pname = "[unknown]";
102472e33095SPawel Jakub Dawidek 
102572e33095SPawel Jakub Dawidek 	switch (bp->bio_cmd) {
102672e33095SPawel Jakub Dawidek 	case BIO_GETATTR:
102772e33095SPawel Jakub Dawidek 		cmd = "GETATTR";
1028ac03832eSConrad Meyer 		sbuf_printf(sb, "%s[%s(attr=%s)]", pname, cmd,
1029ac03832eSConrad Meyer 		    bp->bio_attribute);
103072e33095SPawel Jakub Dawidek 		return;
1031c3618c65SPawel Jakub Dawidek 	case BIO_FLUSH:
1032c3618c65SPawel Jakub Dawidek 		cmd = "FLUSH";
1033ac03832eSConrad Meyer 		sbuf_printf(sb, "%s[%s]", pname, cmd);
1034c3618c65SPawel Jakub Dawidek 		return;
10359a6844d5SKenneth D. Merry 	case BIO_ZONE: {
10369a6844d5SKenneth D. Merry 		char *subcmd = NULL;
10379a6844d5SKenneth D. Merry 		cmd = "ZONE";
10389a6844d5SKenneth D. Merry 		switch (bp->bio_zone.zone_cmd) {
10399a6844d5SKenneth D. Merry 		case DISK_ZONE_OPEN:
10409a6844d5SKenneth D. Merry 			subcmd = "OPEN";
10419a6844d5SKenneth D. Merry 			break;
10429a6844d5SKenneth D. Merry 		case DISK_ZONE_CLOSE:
10439a6844d5SKenneth D. Merry 			subcmd = "CLOSE";
10449a6844d5SKenneth D. Merry 			break;
10459a6844d5SKenneth D. Merry 		case DISK_ZONE_FINISH:
10469a6844d5SKenneth D. Merry 			subcmd = "FINISH";
10479a6844d5SKenneth D. Merry 			break;
10489a6844d5SKenneth D. Merry 		case DISK_ZONE_RWP:
10499a6844d5SKenneth D. Merry 			subcmd = "RWP";
10509a6844d5SKenneth D. Merry 			break;
10519a6844d5SKenneth D. Merry 		case DISK_ZONE_REPORT_ZONES:
10529a6844d5SKenneth D. Merry 			subcmd = "REPORT ZONES";
10539a6844d5SKenneth D. Merry 			break;
10549a6844d5SKenneth D. Merry 		case DISK_ZONE_GET_PARAMS:
10559a6844d5SKenneth D. Merry 			subcmd = "GET PARAMS";
10569a6844d5SKenneth D. Merry 			break;
10579a6844d5SKenneth D. Merry 		default:
10589a6844d5SKenneth D. Merry 			subcmd = "UNKNOWN";
10599a6844d5SKenneth D. Merry 			break;
10609a6844d5SKenneth D. Merry 		}
1061ac03832eSConrad Meyer 		sbuf_printf(sb, "%s[%s,%s]", pname, cmd, subcmd);
10629a6844d5SKenneth D. Merry 		return;
10639a6844d5SKenneth D. Merry 	}
106472e33095SPawel Jakub Dawidek 	case BIO_READ:
106572e33095SPawel Jakub Dawidek 		cmd = "READ";
10667ce513a5SEdward Tomasz Napierala 		break;
106772e33095SPawel Jakub Dawidek 	case BIO_WRITE:
106872e33095SPawel Jakub Dawidek 		cmd = "WRITE";
10697ce513a5SEdward Tomasz Napierala 		break;
107072e33095SPawel Jakub Dawidek 	case BIO_DELETE:
107172e33095SPawel Jakub Dawidek 		cmd = "DELETE";
10727ce513a5SEdward Tomasz Napierala 		break;
107372e33095SPawel Jakub Dawidek 	default:
107472e33095SPawel Jakub Dawidek 		cmd = "UNKNOWN";
1075ac03832eSConrad Meyer 		sbuf_printf(sb, "%s[%s()]", pname, cmd);
107672e33095SPawel Jakub Dawidek 		return;
107772e33095SPawel Jakub Dawidek 	}
1078ac03832eSConrad Meyer 	sbuf_printf(sb, "%s[%s(offset=%jd, length=%jd)]", pname, cmd,
10797ce513a5SEdward Tomasz Napierala 	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
108072e33095SPawel Jakub Dawidek }
1081