xref: /freebsd/sys/geom/geom_io.c (revision 9794a803fd12da9807592c4148baa58666966d6c)
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:
410c3618c65SPawel Jakub Dawidek 	case BIO_FLUSH:
411e39d70d4SPoul-Henning Kamp 		if (cp->acw == 0)
412e39d70d4SPoul-Henning Kamp 			return (EPERM);
413e39d70d4SPoul-Henning Kamp 		break;
4149a6844d5SKenneth D. Merry 	case BIO_ZONE:
4159a6844d5SKenneth D. Merry 		if ((bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES) ||
4169a6844d5SKenneth D. Merry 		    (bp->bio_zone.zone_cmd == DISK_ZONE_GET_PARAMS)) {
4179a6844d5SKenneth D. Merry 			if (cp->acr == 0)
4189a6844d5SKenneth D. Merry 				return (EPERM);
4199a6844d5SKenneth D. Merry 		} else if (cp->acw == 0)
4209a6844d5SKenneth D. Merry 			return (EPERM);
4219a6844d5SKenneth D. Merry 		break;
422e39d70d4SPoul-Henning Kamp 	default:
423e39d70d4SPoul-Henning Kamp 		return (EPERM);
424e39d70d4SPoul-Henning Kamp 	}
425e39d70d4SPoul-Henning Kamp 	/* if provider is marked for error, don't disturb. */
426e39d70d4SPoul-Henning Kamp 	if (pp->error)
427e39d70d4SPoul-Henning Kamp 		return (pp->error);
4283631c638SAlexander Motin 	if (cp->flags & G_CF_ORPHAN)
4293631c638SAlexander Motin 		return (ENXIO);
430e39d70d4SPoul-Henning Kamp 
431e39d70d4SPoul-Henning Kamp 	switch(bp->bio_cmd) {
432e39d70d4SPoul-Henning Kamp 	case BIO_READ:
433e39d70d4SPoul-Henning Kamp 	case BIO_WRITE:
434e39d70d4SPoul-Henning Kamp 	case BIO_DELETE:
4352a842317SAndriy Gapon 		/* Zero sectorsize or mediasize is probably a lack of media. */
4362a842317SAndriy Gapon 		if (pp->sectorsize == 0 || pp->mediasize == 0)
43743bff1a7SPoul-Henning Kamp 			return (ENXIO);
438e39d70d4SPoul-Henning Kamp 		/* Reject I/O not on sector boundary */
439e39d70d4SPoul-Henning Kamp 		if (bp->bio_offset % pp->sectorsize)
440e39d70d4SPoul-Henning Kamp 			return (EINVAL);
441e39d70d4SPoul-Henning Kamp 		/* Reject I/O not integral sector long */
442e39d70d4SPoul-Henning Kamp 		if (bp->bio_length % pp->sectorsize)
443e39d70d4SPoul-Henning Kamp 			return (EINVAL);
444d1b8bf47SPoul-Henning Kamp 		/* Reject requests before or past the end of media. */
445d1b8bf47SPoul-Henning Kamp 		if (bp->bio_offset < 0)
446d1b8bf47SPoul-Henning Kamp 			return (EIO);
447e39d70d4SPoul-Henning Kamp 		if (bp->bio_offset > pp->mediasize)
448e39d70d4SPoul-Henning Kamp 			return (EIO);
44940ea77a0SAlexander Motin 
45040ea77a0SAlexander Motin 		/* Truncate requests to the end of providers media. */
45140ea77a0SAlexander Motin 		excess = bp->bio_offset + bp->bio_length;
45240ea77a0SAlexander Motin 		if (excess > bp->bio_to->mediasize) {
45340ea77a0SAlexander Motin 			KASSERT((bp->bio_flags & BIO_UNMAPPED) == 0 ||
45440ea77a0SAlexander Motin 			    round_page(bp->bio_ma_offset +
45540ea77a0SAlexander Motin 			    bp->bio_length) / PAGE_SIZE == bp->bio_ma_n,
45640ea77a0SAlexander Motin 			    ("excess bio %p too short", bp));
45740ea77a0SAlexander Motin 			excess -= bp->bio_to->mediasize;
45840ea77a0SAlexander Motin 			bp->bio_length -= excess;
45940ea77a0SAlexander Motin 			if ((bp->bio_flags & BIO_UNMAPPED) != 0) {
46040ea77a0SAlexander Motin 				bp->bio_ma_n = round_page(bp->bio_ma_offset +
46140ea77a0SAlexander Motin 				    bp->bio_length) / PAGE_SIZE;
46240ea77a0SAlexander Motin 			}
46340ea77a0SAlexander Motin 			if (excess > 0)
46440ea77a0SAlexander Motin 				CTR3(KTR_GEOM, "g_down truncated bio "
46540ea77a0SAlexander Motin 				    "%p provider %s by %d", bp,
46640ea77a0SAlexander Motin 				    bp->bio_to->name, excess);
46740ea77a0SAlexander Motin 		}
46840ea77a0SAlexander Motin 
46940ea77a0SAlexander Motin 		/* Deliver zero length transfers right here. */
47040ea77a0SAlexander Motin 		if (bp->bio_length == 0) {
47140ea77a0SAlexander Motin 			CTR2(KTR_GEOM, "g_down terminated 0-length "
47240ea77a0SAlexander Motin 			    "bp %p provider %s", bp, bp->bio_to->name);
47340ea77a0SAlexander Motin 			return (0);
47440ea77a0SAlexander Motin 		}
47540ea77a0SAlexander Motin 
47640ea77a0SAlexander Motin 		if ((bp->bio_flags & BIO_UNMAPPED) != 0 &&
47740ea77a0SAlexander Motin 		    (bp->bio_to->flags & G_PF_ACCEPT_UNMAPPED) == 0 &&
47840ea77a0SAlexander Motin 		    (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
47940ea77a0SAlexander Motin 			if ((error = g_io_transient_map_bio(bp)) >= 0)
48040ea77a0SAlexander Motin 				return (error);
48140ea77a0SAlexander Motin 		}
482e39d70d4SPoul-Henning Kamp 		break;
483e39d70d4SPoul-Henning Kamp 	default:
484e39d70d4SPoul-Henning Kamp 		break;
485e39d70d4SPoul-Henning Kamp 	}
48640ea77a0SAlexander Motin 	return (EJUSTRETURN);
487e39d70d4SPoul-Henning Kamp }
488e39d70d4SPoul-Henning Kamp 
489dd84a43cSPoul-Henning Kamp void
490dd84a43cSPoul-Henning Kamp g_io_request(struct bio *bp, struct g_consumer *cp)
491dd84a43cSPoul-Henning Kamp {
492801bb689SPoul-Henning Kamp 	struct g_provider *pp;
49340ea77a0SAlexander Motin 	struct mtx *mtxp;
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));
54840ea77a0SAlexander Motin 	if ((g_collectstats & G_STATS_CONSUMERS) != 0 ||
54940ea77a0SAlexander Motin 	    ((g_collectstats & G_STATS_PROVIDERS) != 0 && pp->stat != NULL))
55019fa21aaSPoul-Henning Kamp 		binuptime(&bp->bio_t0);
551a5be8eb5SAlexander Motin 	else
552a5be8eb5SAlexander Motin 		getbinuptime(&bp->bio_t0);
5538827c821SPoul-Henning Kamp 
55440ea77a0SAlexander Motin #ifdef GET_STACK_USAGE
555347e9d54SKonstantin Belousov 	direct = (cp->flags & G_CF_DIRECT_SEND) != 0 &&
556347e9d54SKonstantin Belousov 	    (pp->flags & G_PF_DIRECT_RECEIVE) != 0 &&
55740ea77a0SAlexander Motin 	    !g_is_geom_thread(curthread) &&
5589b349650SKonstantin Belousov 	    ((pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ||
5593f2e5b85SWarner Losh 	    (bp->bio_flags & BIO_UNMAPPED) == 0 || THREAD_CAN_SLEEP()) &&
5603f2e5b85SWarner Losh 	    pace == 0;
56140ea77a0SAlexander Motin 	if (direct) {
56240ea77a0SAlexander Motin 		/* Block direct execution if less then half of stack left. */
56340ea77a0SAlexander Motin 		size_t	st, su;
56440ea77a0SAlexander Motin 		GET_STACK_USAGE(st, su);
56540ea77a0SAlexander Motin 		if (su * 2 > st)
56640ea77a0SAlexander Motin 			direct = 0;
56740ea77a0SAlexander Motin 	}
56840ea77a0SAlexander Motin #else
56940ea77a0SAlexander Motin 	direct = 0;
57040ea77a0SAlexander Motin #endif
57140ea77a0SAlexander Motin 
5728827c821SPoul-Henning Kamp 	/*
5738827c821SPoul-Henning Kamp 	 * The statistics collection is lockless, as such, but we
5748827c821SPoul-Henning Kamp 	 * can not update one instance of the statistics from more
5758827c821SPoul-Henning Kamp 	 * than one thread at a time, so grab the lock first.
5768827c821SPoul-Henning Kamp 	 */
57740ea77a0SAlexander Motin 	mtxp = mtx_pool_find(mtxpool_sleep, pp);
57840ea77a0SAlexander Motin 	mtx_lock(mtxp);
57940ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_PROVIDERS)
58019fa21aaSPoul-Henning Kamp 		devstat_start_transaction(pp->stat, &bp->bio_t0);
58140ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_CONSUMERS)
58219fa21aaSPoul-Henning Kamp 		devstat_start_transaction(cp->stat, &bp->bio_t0);
583*9794a803SAlexander Motin #ifdef INVARIANTS
584cf457284SPoul-Henning Kamp 	cp->nstart++;
585*9794a803SAlexander Motin #endif
58640ea77a0SAlexander Motin 	mtx_unlock(mtxp);
58740ea77a0SAlexander Motin 
58840ea77a0SAlexander Motin 	if (direct) {
58940ea77a0SAlexander Motin 		error = g_io_check(bp);
59040ea77a0SAlexander Motin 		if (error >= 0) {
59140ea77a0SAlexander Motin 			CTR3(KTR_GEOM, "g_io_request g_io_check on bp %p "
59240ea77a0SAlexander Motin 			    "provider %s returned %d", bp, bp->bio_to->name,
59340ea77a0SAlexander Motin 			    error);
59440ea77a0SAlexander Motin 			g_io_deliver(bp, error);
59540ea77a0SAlexander Motin 			return;
59640ea77a0SAlexander Motin 		}
59740ea77a0SAlexander Motin 		bp->bio_to->geom->start(bp);
59840ea77a0SAlexander Motin 	} else {
59940ea77a0SAlexander Motin 		g_bioq_lock(&g_bio_run_down);
6000d883b11SAlexander Motin 		first = TAILQ_EMPTY(&g_bio_run_down.bio_queue);
60119fa21aaSPoul-Henning Kamp 		TAILQ_INSERT_TAIL(&g_bio_run_down.bio_queue, bp, bio_queue);
60240ea77a0SAlexander Motin 		bp->bio_flags |= BIO_ONQUEUE;
60319fa21aaSPoul-Henning Kamp 		g_bio_run_down.bio_queue_length++;
60419fa21aaSPoul-Henning Kamp 		g_bioq_unlock(&g_bio_run_down);
6052fccec19SPoul-Henning Kamp 		/* Pass it on down. */
6060d883b11SAlexander Motin 		if (first)
607dd84a43cSPoul-Henning Kamp 			wakeup(&g_wait_down);
608dd84a43cSPoul-Henning Kamp 	}
60940ea77a0SAlexander Motin }
610dd84a43cSPoul-Henning Kamp 
611dd84a43cSPoul-Henning Kamp void
61272840432SPoul-Henning Kamp g_io_deliver(struct bio *bp, int error)
613dd84a43cSPoul-Henning Kamp {
614e431d66cSAlexander Motin 	struct bintime now;
615801bb689SPoul-Henning Kamp 	struct g_consumer *cp;
616801bb689SPoul-Henning Kamp 	struct g_provider *pp;
61740ea77a0SAlexander Motin 	struct mtx *mtxp;
61840ea77a0SAlexander Motin 	int direct, first;
619dd84a43cSPoul-Henning Kamp 
6208532d381SConrad Meyer 	biotrack(bp, __func__);
6218532d381SConrad Meyer 
622e060b6bdSPoul-Henning Kamp 	KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
623801bb689SPoul-Henning Kamp 	pp = bp->bio_to;
624f7eeab17SPoul-Henning Kamp 	KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver"));
625f7eeab17SPoul-Henning Kamp 	cp = bp->bio_from;
626f7eeab17SPoul-Henning Kamp 	if (cp == NULL) {
627f7eeab17SPoul-Henning Kamp 		bp->bio_error = error;
628f7eeab17SPoul-Henning Kamp 		bp->bio_done(bp);
629f7eeab17SPoul-Henning Kamp 		return;
630f7eeab17SPoul-Henning Kamp 	}
631801bb689SPoul-Henning Kamp 	KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver"));
632801bb689SPoul-Henning Kamp 	KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
633fb231f36SEdward Tomasz Napierala #ifdef DIAGNOSTIC
634fb231f36SEdward Tomasz Napierala 	/*
635fb231f36SEdward Tomasz Napierala 	 * Some classes - GJournal in particular - can modify bio's
636fb231f36SEdward Tomasz Napierala 	 * private fields while the bio is in transit; G_GEOM_VOLATILE_BIO
637fb231f36SEdward Tomasz Napierala 	 * flag means it's an expected behaviour for that particular geom.
638fb231f36SEdward Tomasz Napierala 	 */
639fb231f36SEdward Tomasz Napierala 	if ((cp->geom->flags & G_GEOM_VOLATILE_BIO) == 0) {
640fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_caller1 == bp->_bio_caller1,
641fb231f36SEdward Tomasz Napierala 		    ("bio_caller1 used by the provider %s", pp->name));
642fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_caller2 == bp->_bio_caller2,
643fb231f36SEdward Tomasz Napierala 		    ("bio_caller2 used by the provider %s", pp->name));
644fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_cflags == bp->_bio_cflags,
645fb231f36SEdward Tomasz Napierala 		    ("bio_cflags used by the provider %s", pp->name));
646fb231f36SEdward Tomasz Napierala 	}
647fb231f36SEdward Tomasz Napierala #endif
64846aeebecSPawel Jakub Dawidek 	KASSERT(bp->bio_completed >= 0, ("bio_completed can't be less than 0"));
64946aeebecSPawel Jakub Dawidek 	KASSERT(bp->bio_completed <= bp->bio_length,
65046aeebecSPawel Jakub Dawidek 	    ("bio_completed can't be greater than bio_length"));
6515ab413bfSPoul-Henning Kamp 
652dd84a43cSPoul-Henning Kamp 	g_trace(G_T_BIO,
6530355b86eSPoul-Henning Kamp "g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
654801bb689SPoul-Henning Kamp 	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error,
6550355b86eSPoul-Henning Kamp 	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
656801bb689SPoul-Henning Kamp 
65719fa21aaSPoul-Henning Kamp 	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
65819fa21aaSPoul-Henning Kamp 	    ("Bio already on queue bp=%p", bp));
65919fa21aaSPoul-Henning Kamp 
660dcbd0fe5SPoul-Henning Kamp 	/*
661dcbd0fe5SPoul-Henning Kamp 	 * XXX: next two doesn't belong here
662dcbd0fe5SPoul-Henning Kamp 	 */
663e24cbd90SPoul-Henning Kamp 	bp->bio_bcount = bp->bio_length;
664e24cbd90SPoul-Henning Kamp 	bp->bio_resid = bp->bio_bcount - bp->bio_completed;
66519fa21aaSPoul-Henning Kamp 
66640ea77a0SAlexander Motin #ifdef GET_STACK_USAGE
66740ea77a0SAlexander Motin 	direct = (pp->flags & G_PF_DIRECT_SEND) &&
66840ea77a0SAlexander Motin 		 (cp->flags & G_CF_DIRECT_RECEIVE) &&
66940ea77a0SAlexander Motin 		 !g_is_geom_thread(curthread);
67040ea77a0SAlexander Motin 	if (direct) {
67140ea77a0SAlexander Motin 		/* Block direct execution if less then half of stack left. */
67240ea77a0SAlexander Motin 		size_t	st, su;
67340ea77a0SAlexander Motin 		GET_STACK_USAGE(st, su);
67440ea77a0SAlexander Motin 		if (su * 2 > st)
67540ea77a0SAlexander Motin 			direct = 0;
67640ea77a0SAlexander Motin 	}
67740ea77a0SAlexander Motin #else
67840ea77a0SAlexander Motin 	direct = 0;
67940ea77a0SAlexander Motin #endif
68040ea77a0SAlexander Motin 
6818827c821SPoul-Henning Kamp 	/*
6828827c821SPoul-Henning Kamp 	 * The statistics collection is lockless, as such, but we
6838827c821SPoul-Henning Kamp 	 * can not update one instance of the statistics from more
6848827c821SPoul-Henning Kamp 	 * than one thread at a time, so grab the lock first.
6858827c821SPoul-Henning Kamp 	 */
68640ea77a0SAlexander Motin 	if ((g_collectstats & G_STATS_CONSUMERS) != 0 ||
68740ea77a0SAlexander Motin 	    ((g_collectstats & G_STATS_PROVIDERS) != 0 && pp->stat != NULL))
688e431d66cSAlexander Motin 		binuptime(&now);
68940ea77a0SAlexander Motin 	mtxp = mtx_pool_find(mtxpool_sleep, cp);
69040ea77a0SAlexander Motin 	mtx_lock(mtxp);
69140ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_PROVIDERS)
692e431d66cSAlexander Motin 		devstat_end_transaction_bio_bt(pp->stat, bp, &now);
69340ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_CONSUMERS)
694e431d66cSAlexander Motin 		devstat_end_transaction_bio_bt(cp->stat, bp, &now);
695*9794a803SAlexander Motin #ifdef INVARIANTS
696c6ae9b5fSPoul-Henning Kamp 	cp->nend++;
697*9794a803SAlexander Motin #endif
69840ea77a0SAlexander Motin 	mtx_unlock(mtxp);
69940ea77a0SAlexander Motin 
70019fa21aaSPoul-Henning Kamp 	if (error != ENOMEM) {
70119fa21aaSPoul-Henning Kamp 		bp->bio_error = error;
70240ea77a0SAlexander Motin 		if (direct) {
70340ea77a0SAlexander Motin 			biodone(bp);
70440ea77a0SAlexander Motin 		} else {
70540ea77a0SAlexander Motin 			g_bioq_lock(&g_bio_run_up);
7060d883b11SAlexander Motin 			first = TAILQ_EMPTY(&g_bio_run_up.bio_queue);
70719fa21aaSPoul-Henning Kamp 			TAILQ_INSERT_TAIL(&g_bio_run_up.bio_queue, bp, bio_queue);
708276f72c5SPoul-Henning Kamp 			bp->bio_flags |= BIO_ONQUEUE;
70919fa21aaSPoul-Henning Kamp 			g_bio_run_up.bio_queue_length++;
71019fa21aaSPoul-Henning Kamp 			g_bioq_unlock(&g_bio_run_up);
7110d883b11SAlexander Motin 			if (first)
71219fa21aaSPoul-Henning Kamp 				wakeup(&g_wait_up);
71340ea77a0SAlexander Motin 		}
71419fa21aaSPoul-Henning Kamp 		return;
71519fa21aaSPoul-Henning Kamp 	}
716dd84a43cSPoul-Henning Kamp 
7172cc9686eSPoul-Henning Kamp 	if (bootverbose)
718801bb689SPoul-Henning Kamp 		printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name);
7191b949c05SPawel Jakub Dawidek 	bp->bio_children = 0;
7201b949c05SPawel Jakub Dawidek 	bp->bio_inbed = 0;
72160114438SPawel Jakub Dawidek 	bp->bio_driver1 = NULL;
72260114438SPawel Jakub Dawidek 	bp->bio_driver2 = NULL;
72360114438SPawel Jakub Dawidek 	bp->bio_pflags = 0;
724801bb689SPoul-Henning Kamp 	g_io_request(bp, cp);
7253f2e5b85SWarner Losh 	pace = 1;
7263432e4fdSPoul-Henning Kamp 	return;
7273432e4fdSPoul-Henning Kamp }
728dd84a43cSPoul-Henning Kamp 
729ee75e7deSKonstantin Belousov SYSCTL_DECL(_kern_geom);
730ee75e7deSKonstantin Belousov 
731ee75e7deSKonstantin Belousov static long transient_maps;
732ee75e7deSKonstantin Belousov SYSCTL_LONG(_kern_geom, OID_AUTO, transient_maps, CTLFLAG_RD,
733ee75e7deSKonstantin Belousov     &transient_maps, 0,
734ee75e7deSKonstantin Belousov     "Total count of the transient mapping requests");
735ee75e7deSKonstantin Belousov u_int transient_map_retries = 10;
736ee75e7deSKonstantin Belousov SYSCTL_UINT(_kern_geom, OID_AUTO, transient_map_retries, CTLFLAG_RW,
737ee75e7deSKonstantin Belousov     &transient_map_retries, 0,
738ee75e7deSKonstantin Belousov     "Max count of retries used before giving up on creating transient map");
739ee75e7deSKonstantin Belousov int transient_map_hard_failures;
740ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, transient_map_hard_failures, CTLFLAG_RD,
741ee75e7deSKonstantin Belousov     &transient_map_hard_failures, 0,
742ee75e7deSKonstantin Belousov     "Failures to establish the transient mapping due to retry attempts "
743ee75e7deSKonstantin Belousov     "exhausted");
744ee75e7deSKonstantin Belousov int transient_map_soft_failures;
745ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, transient_map_soft_failures, CTLFLAG_RD,
746ee75e7deSKonstantin Belousov     &transient_map_soft_failures, 0,
747ee75e7deSKonstantin Belousov     "Count of retried failures to establish the transient mapping");
748ee75e7deSKonstantin Belousov int inflight_transient_maps;
749ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, inflight_transient_maps, CTLFLAG_RD,
750ee75e7deSKonstantin Belousov     &inflight_transient_maps, 0,
751ee75e7deSKonstantin Belousov     "Current count of the active transient maps");
752ee75e7deSKonstantin Belousov 
753ee75e7deSKonstantin Belousov static int
754ee75e7deSKonstantin Belousov g_io_transient_map_bio(struct bio *bp)
755ee75e7deSKonstantin Belousov {
756ee75e7deSKonstantin Belousov 	vm_offset_t addr;
757ee75e7deSKonstantin Belousov 	long size;
758ee75e7deSKonstantin Belousov 	u_int retried;
759ee75e7deSKonstantin Belousov 
7606c83fce3SKonstantin Belousov 	KASSERT(unmapped_buf_allowed, ("unmapped disabled"));
7616c83fce3SKonstantin Belousov 
762ee75e7deSKonstantin Belousov 	size = round_page(bp->bio_ma_offset + bp->bio_length);
763ee75e7deSKonstantin Belousov 	KASSERT(size / PAGE_SIZE == bp->bio_ma_n, ("Bio too short %p", bp));
764ee75e7deSKonstantin Belousov 	addr = 0;
765ee75e7deSKonstantin Belousov 	retried = 0;
766ee75e7deSKonstantin Belousov 	atomic_add_long(&transient_maps, 1);
767ee75e7deSKonstantin Belousov retry:
7685f518366SJeff Roberson 	if (vmem_alloc(transient_arena, size, M_BESTFIT | M_NOWAIT, &addr)) {
769ee75e7deSKonstantin Belousov 		if (transient_map_retries != 0 &&
770ee75e7deSKonstantin Belousov 		    retried >= transient_map_retries) {
771ee75e7deSKonstantin Belousov 			CTR2(KTR_GEOM, "g_down cannot map bp %p provider %s",
772ee75e7deSKonstantin Belousov 			    bp, bp->bio_to->name);
773ee75e7deSKonstantin Belousov 			atomic_add_int(&transient_map_hard_failures, 1);
77440ea77a0SAlexander Motin 			return (EDEADLK/* XXXKIB */);
775ee75e7deSKonstantin Belousov 		} else {
776ee75e7deSKonstantin Belousov 			/*
777ee75e7deSKonstantin Belousov 			 * Naive attempt to quisce the I/O to get more
778ee75e7deSKonstantin Belousov 			 * in-flight requests completed and defragment
7795f518366SJeff Roberson 			 * the transient_arena.
780ee75e7deSKonstantin Belousov 			 */
781ee75e7deSKonstantin Belousov 			CTR3(KTR_GEOM, "g_down retrymap bp %p provider %s r %d",
782ee75e7deSKonstantin Belousov 			    bp, bp->bio_to->name, retried);
783ee75e7deSKonstantin Belousov 			pause("g_d_tra", hz / 10);
784ee75e7deSKonstantin Belousov 			retried++;
785ee75e7deSKonstantin Belousov 			atomic_add_int(&transient_map_soft_failures, 1);
786ee75e7deSKonstantin Belousov 			goto retry;
787ee75e7deSKonstantin Belousov 		}
788ee75e7deSKonstantin Belousov 	}
789ee75e7deSKonstantin Belousov 	atomic_add_int(&inflight_transient_maps, 1);
790ee75e7deSKonstantin Belousov 	pmap_qenter((vm_offset_t)addr, bp->bio_ma, OFF_TO_IDX(size));
791ee75e7deSKonstantin Belousov 	bp->bio_data = (caddr_t)addr + bp->bio_ma_offset;
792ee75e7deSKonstantin Belousov 	bp->bio_flags |= BIO_TRANSIENT_MAPPING;
793ee75e7deSKonstantin Belousov 	bp->bio_flags &= ~BIO_UNMAPPED;
79440ea77a0SAlexander Motin 	return (EJUSTRETURN);
795ee75e7deSKonstantin Belousov }
796ee75e7deSKonstantin Belousov 
797dd84a43cSPoul-Henning Kamp void
798dd84a43cSPoul-Henning Kamp g_io_schedule_down(struct thread *tp __unused)
799dd84a43cSPoul-Henning Kamp {
800dd84a43cSPoul-Henning Kamp 	struct bio *bp;
801e39d70d4SPoul-Henning Kamp 	int error;
802dd84a43cSPoul-Henning Kamp 
803dd84a43cSPoul-Henning Kamp 	for(;;) {
804f0e185d7SPoul-Henning Kamp 		g_bioq_lock(&g_bio_run_down);
805dd84a43cSPoul-Henning Kamp 		bp = g_bioq_first(&g_bio_run_down);
806f0e185d7SPoul-Henning Kamp 		if (bp == NULL) {
80749dbb61dSRobert Watson 			CTR0(KTR_GEOM, "g_down going to sleep");
808f0e185d7SPoul-Henning Kamp 			msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock,
8097fc019afSAlexander Motin 			    PRIBIO | PDROP, "-", 0);
810f0e185d7SPoul-Henning Kamp 			continue;
811f0e185d7SPoul-Henning Kamp 		}
81249dbb61dSRobert Watson 		CTR0(KTR_GEOM, "g_down has work to do");
813f0e185d7SPoul-Henning Kamp 		g_bioq_unlock(&g_bio_run_down);
8148532d381SConrad Meyer 		biotrack(bp, __func__);
8153f2e5b85SWarner Losh 		if (pace != 0) {
8163f2e5b85SWarner Losh 			/*
8173f2e5b85SWarner Losh 			 * There has been at least one memory allocation
8183f2e5b85SWarner Losh 			 * failure since the last I/O completed. Pause 1ms to
8193f2e5b85SWarner Losh 			 * give the system a chance to free up memory. We only
8203f2e5b85SWarner Losh 			 * do this once because a large number of allocations
8213f2e5b85SWarner Losh 			 * can fail in the direct dispatch case and there's no
8223f2e5b85SWarner Losh 			 * relationship between the number of these failures and
8233f2e5b85SWarner Losh 			 * the length of the outage. If there's still an outage,
8243f2e5b85SWarner Losh 			 * we'll pause again and again until it's
8253f2e5b85SWarner Losh 			 * resolved. Older versions paused longer and once per
8263f2e5b85SWarner Losh 			 * allocation failure. This was OK for a single threaded
8273f2e5b85SWarner Losh 			 * g_down, but with direct dispatch would lead to max of
8283f2e5b85SWarner Losh 			 * 10 IOPs for minutes at a time when transient memory
8293f2e5b85SWarner Losh 			 * issues prevented allocation for a batch of requests
8303f2e5b85SWarner Losh 			 * from the upper layers.
8313f2e5b85SWarner Losh 			 *
8323f2e5b85SWarner Losh 			 * XXX This pacing is really lame. It needs to be solved
8333f2e5b85SWarner Losh 			 * by other methods. This is OK only because the worst
8343f2e5b85SWarner Losh 			 * case scenario is so rare. In the worst case scenario
8353f2e5b85SWarner Losh 			 * all memory is tied up waiting for I/O to complete
8363f2e5b85SWarner Losh 			 * which can never happen since we can't allocate bios
8373f2e5b85SWarner Losh 			 * for that I/O.
8383f2e5b85SWarner Losh 			 */
8393f2e5b85SWarner Losh 			CTR0(KTR_GEOM, "g_down pacing self");
8403f2e5b85SWarner Losh 			pause("g_down", min(hz/1000, 1));
8413f2e5b85SWarner Losh 			pace = 0;
842376ceb79SPoul-Henning Kamp 		}
84340ea77a0SAlexander Motin 		CTR2(KTR_GEOM, "g_down processing bp %p provider %s", bp,
84440ea77a0SAlexander Motin 		    bp->bio_to->name);
845e39d70d4SPoul-Henning Kamp 		error = g_io_check(bp);
84640ea77a0SAlexander Motin 		if (error >= 0) {
84749dbb61dSRobert Watson 			CTR3(KTR_GEOM, "g_down g_io_check on bp %p provider "
84849dbb61dSRobert Watson 			    "%s returned %d", bp, bp->bio_to->name, error);
849e39d70d4SPoul-Henning Kamp 			g_io_deliver(bp, error);
850e39d70d4SPoul-Henning Kamp 			continue;
851e39d70d4SPoul-Henning Kamp 		}
85251460da8SJohn Baldwin 		THREAD_NO_SLEEPING();
85349dbb61dSRobert Watson 		CTR4(KTR_GEOM, "g_down starting bp %p provider %s off %ld "
85449dbb61dSRobert Watson 		    "len %ld", bp, bp->bio_to->name, bp->bio_offset,
85549dbb61dSRobert Watson 		    bp->bio_length);
856dd84a43cSPoul-Henning Kamp 		bp->bio_to->geom->start(bp);
85751460da8SJohn Baldwin 		THREAD_SLEEPING_OK();
858dd84a43cSPoul-Henning Kamp 	}
859dd84a43cSPoul-Henning Kamp }
860dd84a43cSPoul-Henning Kamp 
861dd84a43cSPoul-Henning Kamp void
862dd84a43cSPoul-Henning Kamp g_io_schedule_up(struct thread *tp __unused)
863dd84a43cSPoul-Henning Kamp {
864dd84a43cSPoul-Henning Kamp 	struct bio *bp;
8650c4440c3SEdward Tomasz Napierala 
866dd84a43cSPoul-Henning Kamp 	for(;;) {
867f0e185d7SPoul-Henning Kamp 		g_bioq_lock(&g_bio_run_up);
8680c4440c3SEdward Tomasz Napierala 		bp = g_bioq_first(&g_bio_run_up);
8690c4440c3SEdward Tomasz Napierala 		if (bp == NULL) {
8700c4440c3SEdward Tomasz Napierala 			CTR0(KTR_GEOM, "g_up going to sleep");
8710c4440c3SEdward Tomasz Napierala 			msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock,
8720c4440c3SEdward Tomasz Napierala 			    PRIBIO | PDROP, "-", 0);
8735fcf4e43SPoul-Henning Kamp 			continue;
8745fcf4e43SPoul-Henning Kamp 		}
875f0e185d7SPoul-Henning Kamp 		g_bioq_unlock(&g_bio_run_up);
87651460da8SJohn Baldwin 		THREAD_NO_SLEEPING();
87749dbb61dSRobert Watson 		CTR4(KTR_GEOM, "g_up biodone bp %p provider %s off "
878c4901b67SSean Bruno 		    "%jd len %ld", bp, bp->bio_to->name,
87949dbb61dSRobert Watson 		    bp->bio_offset, bp->bio_length);
88053706245SPoul-Henning Kamp 		biodone(bp);
88151460da8SJohn Baldwin 		THREAD_SLEEPING_OK();
882dd84a43cSPoul-Henning Kamp 	}
883dd84a43cSPoul-Henning Kamp }
884dd84a43cSPoul-Henning Kamp 
885dd84a43cSPoul-Henning Kamp void *
886dd84a43cSPoul-Henning Kamp g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
887dd84a43cSPoul-Henning Kamp {
888dd84a43cSPoul-Henning Kamp 	struct bio *bp;
889dd84a43cSPoul-Henning Kamp 	void *ptr;
890dd84a43cSPoul-Henning Kamp 	int errorc;
891dd84a43cSPoul-Henning Kamp 
8928dd5480dSPawel Jakub Dawidek 	KASSERT(length > 0 && length >= cp->provider->sectorsize &&
8938dd5480dSPawel Jakub Dawidek 	    length <= MAXPHYS, ("g_read_data(): invalid length %jd",
8948dd5480dSPawel Jakub Dawidek 	    (intmax_t)length));
8953eb6ffdfSPoul-Henning Kamp 
896a2033c96SPoul-Henning Kamp 	bp = g_alloc_bio();
897dd84a43cSPoul-Henning Kamp 	bp->bio_cmd = BIO_READ;
898dd84a43cSPoul-Henning Kamp 	bp->bio_done = NULL;
899dd84a43cSPoul-Henning Kamp 	bp->bio_offset = offset;
900dd84a43cSPoul-Henning Kamp 	bp->bio_length = length;
901a163d034SWarner Losh 	ptr = g_malloc(length, M_WAITOK);
902dd84a43cSPoul-Henning Kamp 	bp->bio_data = ptr;
903dd84a43cSPoul-Henning Kamp 	g_io_request(bp, cp);
90453706245SPoul-Henning Kamp 	errorc = biowait(bp, "gread");
905dd84a43cSPoul-Henning Kamp 	if (error != NULL)
906dd84a43cSPoul-Henning Kamp 		*error = errorc;
907dd84a43cSPoul-Henning Kamp 	g_destroy_bio(bp);
908dd84a43cSPoul-Henning Kamp 	if (errorc) {
909dd84a43cSPoul-Henning Kamp 		g_free(ptr);
910dd84a43cSPoul-Henning Kamp 		ptr = NULL;
911dd84a43cSPoul-Henning Kamp 	}
912dd84a43cSPoul-Henning Kamp 	return (ptr);
913dd84a43cSPoul-Henning Kamp }
91490b1cd56SPoul-Henning Kamp 
915dffce215SKirk McKusick /*
916dffce215SKirk McKusick  * A read function for use by ffs_sbget when used by GEOM-layer routines.
917dffce215SKirk McKusick  */
918dffce215SKirk McKusick int
919dffce215SKirk McKusick g_use_g_read_data(void *devfd, off_t loc, void **bufp, int size)
920dffce215SKirk McKusick {
921dffce215SKirk McKusick 	struct g_consumer *cp;
922dffce215SKirk McKusick 
923efbf3964SKirk McKusick 	KASSERT(*bufp == NULL,
924efbf3964SKirk McKusick 	    ("g_use_g_read_data: non-NULL *bufp %p\n", *bufp));
925efbf3964SKirk McKusick 
926dffce215SKirk McKusick 	cp = (struct g_consumer *)devfd;
927dffce215SKirk McKusick 	/*
928dffce215SKirk McKusick 	 * Take care not to issue an invalid I/O request. The offset of
929dffce215SKirk McKusick 	 * the superblock candidate must be multiples of the provider's
930dffce215SKirk McKusick 	 * sector size, otherwise an FFS can't exist on the provider
931dffce215SKirk McKusick 	 * anyway.
932dffce215SKirk McKusick 	 */
933dffce215SKirk McKusick 	if (loc % cp->provider->sectorsize != 0)
934dffce215SKirk McKusick 		return (ENOENT);
935dffce215SKirk McKusick 	*bufp = g_read_data(cp, loc, size, NULL);
936dffce215SKirk McKusick 	if (*bufp == NULL)
937dffce215SKirk McKusick 		return (ENOENT);
938dffce215SKirk McKusick 	return (0);
939dffce215SKirk McKusick }
940dffce215SKirk McKusick 
94190b1cd56SPoul-Henning Kamp int
94290b1cd56SPoul-Henning Kamp g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
94390b1cd56SPoul-Henning Kamp {
94490b1cd56SPoul-Henning Kamp 	struct bio *bp;
94590b1cd56SPoul-Henning Kamp 	int error;
94690b1cd56SPoul-Henning Kamp 
9478dd5480dSPawel Jakub Dawidek 	KASSERT(length > 0 && length >= cp->provider->sectorsize &&
9488dd5480dSPawel Jakub Dawidek 	    length <= MAXPHYS, ("g_write_data(): invalid length %jd",
9498dd5480dSPawel Jakub Dawidek 	    (intmax_t)length));
9503eb6ffdfSPoul-Henning Kamp 
951a2033c96SPoul-Henning Kamp 	bp = g_alloc_bio();
95290b1cd56SPoul-Henning Kamp 	bp->bio_cmd = BIO_WRITE;
95390b1cd56SPoul-Henning Kamp 	bp->bio_done = NULL;
95490b1cd56SPoul-Henning Kamp 	bp->bio_offset = offset;
95590b1cd56SPoul-Henning Kamp 	bp->bio_length = length;
95690b1cd56SPoul-Henning Kamp 	bp->bio_data = ptr;
95790b1cd56SPoul-Henning Kamp 	g_io_request(bp, cp);
95890b1cd56SPoul-Henning Kamp 	error = biowait(bp, "gwrite");
95990b1cd56SPoul-Henning Kamp 	g_destroy_bio(bp);
96090b1cd56SPoul-Henning Kamp 	return (error);
96190b1cd56SPoul-Henning Kamp }
96272e33095SPawel Jakub Dawidek 
963dffce215SKirk McKusick /*
964dffce215SKirk McKusick  * A write function for use by ffs_sbput when used by GEOM-layer routines.
965dffce215SKirk McKusick  */
966dffce215SKirk McKusick int
967dffce215SKirk McKusick g_use_g_write_data(void *devfd, off_t loc, void *buf, int size)
968dffce215SKirk McKusick {
969dffce215SKirk McKusick 
970dffce215SKirk McKusick 	return (g_write_data((struct g_consumer *)devfd, loc, buf, size));
971dffce215SKirk McKusick }
972dffce215SKirk McKusick 
9732b17fb95SPawel Jakub Dawidek int
9742b17fb95SPawel Jakub Dawidek g_delete_data(struct g_consumer *cp, off_t offset, off_t length)
9752b17fb95SPawel Jakub Dawidek {
9762b17fb95SPawel Jakub Dawidek 	struct bio *bp;
9772b17fb95SPawel Jakub Dawidek 	int error;
9782b17fb95SPawel Jakub Dawidek 
979eed6cda9SPoul-Henning Kamp 	KASSERT(length > 0 && length >= cp->provider->sectorsize,
980eed6cda9SPoul-Henning Kamp 	    ("g_delete_data(): invalid length %jd", (intmax_t)length));
9812b17fb95SPawel Jakub Dawidek 
9822b17fb95SPawel Jakub Dawidek 	bp = g_alloc_bio();
9832b17fb95SPawel Jakub Dawidek 	bp->bio_cmd = BIO_DELETE;
9842b17fb95SPawel Jakub Dawidek 	bp->bio_done = NULL;
9852b17fb95SPawel Jakub Dawidek 	bp->bio_offset = offset;
9862b17fb95SPawel Jakub Dawidek 	bp->bio_length = length;
9872b17fb95SPawel Jakub Dawidek 	bp->bio_data = NULL;
9882b17fb95SPawel Jakub Dawidek 	g_io_request(bp, cp);
9892b17fb95SPawel Jakub Dawidek 	error = biowait(bp, "gdelete");
9902b17fb95SPawel Jakub Dawidek 	g_destroy_bio(bp);
9912b17fb95SPawel Jakub Dawidek 	return (error);
9922b17fb95SPawel Jakub Dawidek }
9932b17fb95SPawel Jakub Dawidek 
99472e33095SPawel Jakub Dawidek void
995ac03832eSConrad Meyer g_print_bio(const char *prefix, const struct bio *bp, const char *fmtsuffix,
996ac03832eSConrad Meyer     ...)
997ac03832eSConrad Meyer {
998ac03832eSConrad Meyer #ifndef PRINTF_BUFR_SIZE
999ac03832eSConrad Meyer #define PRINTF_BUFR_SIZE 64
1000ac03832eSConrad Meyer #endif
1001ac03832eSConrad Meyer 	char bufr[PRINTF_BUFR_SIZE];
1002ac03832eSConrad Meyer 	struct sbuf sb, *sbp __unused;
1003ac03832eSConrad Meyer 	va_list ap;
1004ac03832eSConrad Meyer 
1005ac03832eSConrad Meyer 	sbp = sbuf_new(&sb, bufr, sizeof(bufr), SBUF_FIXEDLEN);
1006ac03832eSConrad Meyer 	KASSERT(sbp != NULL, ("sbuf_new misused?"));
1007ac03832eSConrad Meyer 
1008ac03832eSConrad Meyer 	sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
1009ac03832eSConrad Meyer 
1010ac03832eSConrad Meyer 	sbuf_cat(&sb, prefix);
1011ac03832eSConrad Meyer 	g_format_bio(&sb, bp);
1012ac03832eSConrad Meyer 
1013ac03832eSConrad Meyer 	va_start(ap, fmtsuffix);
1014ac03832eSConrad Meyer 	sbuf_vprintf(&sb, fmtsuffix, ap);
1015ac03832eSConrad Meyer 	va_end(ap);
1016ac03832eSConrad Meyer 
1017ac03832eSConrad Meyer 	sbuf_nl_terminate(&sb);
1018ac03832eSConrad Meyer 
1019ac03832eSConrad Meyer 	sbuf_finish(&sb);
1020ac03832eSConrad Meyer 	sbuf_delete(&sb);
1021ac03832eSConrad Meyer }
1022ac03832eSConrad Meyer 
1023ac03832eSConrad Meyer void
1024ac03832eSConrad Meyer g_format_bio(struct sbuf *sb, const struct bio *bp)
102572e33095SPawel Jakub Dawidek {
102672e33095SPawel Jakub Dawidek 	const char *pname, *cmd = NULL;
102772e33095SPawel Jakub Dawidek 
102872e33095SPawel Jakub Dawidek 	if (bp->bio_to != NULL)
102972e33095SPawel Jakub Dawidek 		pname = bp->bio_to->name;
103072e33095SPawel Jakub Dawidek 	else
103172e33095SPawel Jakub Dawidek 		pname = "[unknown]";
103272e33095SPawel Jakub Dawidek 
103372e33095SPawel Jakub Dawidek 	switch (bp->bio_cmd) {
103472e33095SPawel Jakub Dawidek 	case BIO_GETATTR:
103572e33095SPawel Jakub Dawidek 		cmd = "GETATTR";
1036ac03832eSConrad Meyer 		sbuf_printf(sb, "%s[%s(attr=%s)]", pname, cmd,
1037ac03832eSConrad Meyer 		    bp->bio_attribute);
103872e33095SPawel Jakub Dawidek 		return;
1039c3618c65SPawel Jakub Dawidek 	case BIO_FLUSH:
1040c3618c65SPawel Jakub Dawidek 		cmd = "FLUSH";
1041ac03832eSConrad Meyer 		sbuf_printf(sb, "%s[%s]", pname, cmd);
1042c3618c65SPawel Jakub Dawidek 		return;
10439a6844d5SKenneth D. Merry 	case BIO_ZONE: {
10449a6844d5SKenneth D. Merry 		char *subcmd = NULL;
10459a6844d5SKenneth D. Merry 		cmd = "ZONE";
10469a6844d5SKenneth D. Merry 		switch (bp->bio_zone.zone_cmd) {
10479a6844d5SKenneth D. Merry 		case DISK_ZONE_OPEN:
10489a6844d5SKenneth D. Merry 			subcmd = "OPEN";
10499a6844d5SKenneth D. Merry 			break;
10509a6844d5SKenneth D. Merry 		case DISK_ZONE_CLOSE:
10519a6844d5SKenneth D. Merry 			subcmd = "CLOSE";
10529a6844d5SKenneth D. Merry 			break;
10539a6844d5SKenneth D. Merry 		case DISK_ZONE_FINISH:
10549a6844d5SKenneth D. Merry 			subcmd = "FINISH";
10559a6844d5SKenneth D. Merry 			break;
10569a6844d5SKenneth D. Merry 		case DISK_ZONE_RWP:
10579a6844d5SKenneth D. Merry 			subcmd = "RWP";
10589a6844d5SKenneth D. Merry 			break;
10599a6844d5SKenneth D. Merry 		case DISK_ZONE_REPORT_ZONES:
10609a6844d5SKenneth D. Merry 			subcmd = "REPORT ZONES";
10619a6844d5SKenneth D. Merry 			break;
10629a6844d5SKenneth D. Merry 		case DISK_ZONE_GET_PARAMS:
10639a6844d5SKenneth D. Merry 			subcmd = "GET PARAMS";
10649a6844d5SKenneth D. Merry 			break;
10659a6844d5SKenneth D. Merry 		default:
10669a6844d5SKenneth D. Merry 			subcmd = "UNKNOWN";
10679a6844d5SKenneth D. Merry 			break;
10689a6844d5SKenneth D. Merry 		}
1069ac03832eSConrad Meyer 		sbuf_printf(sb, "%s[%s,%s]", pname, cmd, subcmd);
10709a6844d5SKenneth D. Merry 		return;
10719a6844d5SKenneth D. Merry 	}
107272e33095SPawel Jakub Dawidek 	case BIO_READ:
107372e33095SPawel Jakub Dawidek 		cmd = "READ";
10747ce513a5SEdward Tomasz Napierala 		break;
107572e33095SPawel Jakub Dawidek 	case BIO_WRITE:
107672e33095SPawel Jakub Dawidek 		cmd = "WRITE";
10777ce513a5SEdward Tomasz Napierala 		break;
107872e33095SPawel Jakub Dawidek 	case BIO_DELETE:
107972e33095SPawel Jakub Dawidek 		cmd = "DELETE";
10807ce513a5SEdward Tomasz Napierala 		break;
108172e33095SPawel Jakub Dawidek 	default:
108272e33095SPawel Jakub Dawidek 		cmd = "UNKNOWN";
1083ac03832eSConrad Meyer 		sbuf_printf(sb, "%s[%s()]", pname, cmd);
108472e33095SPawel Jakub Dawidek 		return;
108572e33095SPawel Jakub Dawidek 	}
1086ac03832eSConrad Meyer 	sbuf_printf(sb, "%s[%s(offset=%jd, length=%jd)]", pname, cmd,
10877ce513a5SEdward Tomasz Napierala 	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
108872e33095SPawel Jakub Dawidek }
1089