xref: /freebsd/sys/geom/geom_io.c (revision d91d2b513eb30a226e87f0e52e2f9f232a2e1ca3)
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.
202c6213befSGleb Smirnoff 		 *  BIO_UNMAPPED, BIO_VLIST, and BIO_SWAP should be inherited,
203c6213befSGleb Smirnoff 		 *  to properly 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 &
207c6213befSGleb Smirnoff 		    (BIO_ORDERED | BIO_UNMAPPED | BIO_VLIST | BIO_SWAP);
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);
241c6213befSGleb Smirnoff 	bp2->bio_flags = bp->bio_flags & (BIO_UNMAPPED | BIO_VLIST | BIO_SWAP);
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
344f4499487SMark Johnston g_io_speedup(off_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));
351f4499487SMark Johnston 	g_trace(G_T_BIO, "bio_speedup(%s, %jd, %#x)", cp->provider->name,
352f4499487SMark Johnston 	    (intmax_t)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:
4108b522bdaSWarner 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)
5558b220f89SAlexander Motin 		devstat_start_transaction_bio_t0(cp->stat, bp);
556024932aaSAlexander Motin 	if (g_collectstats & G_STATS_PROVIDERS)
5578b220f89SAlexander Motin 		devstat_start_transaction_bio_t0(pp->stat, bp);
558024932aaSAlexander Motin #ifdef INVARIANTS
559024932aaSAlexander Motin 	atomic_add_int(&cp->nstart, 1);
560024932aaSAlexander Motin #endif
5618827c821SPoul-Henning Kamp 
562347e9d54SKonstantin Belousov 	direct = (cp->flags & G_CF_DIRECT_SEND) != 0 &&
563347e9d54SKonstantin Belousov 	    (pp->flags & G_PF_DIRECT_RECEIVE) != 0 &&
56440ea77a0SAlexander Motin 	    !g_is_geom_thread(curthread) &&
5659b349650SKonstantin Belousov 	    ((pp->flags & G_PF_ACCEPT_UNMAPPED) != 0 ||
5663f2e5b85SWarner Losh 	    (bp->bio_flags & BIO_UNMAPPED) == 0 || THREAD_CAN_SLEEP()) &&
5673f2e5b85SWarner Losh 	    pace == 0;
56840ea77a0SAlexander Motin 	if (direct) {
56940ea77a0SAlexander Motin 		/* Block direct execution if less then half of stack left. */
57040ea77a0SAlexander Motin 		size_t	st, su;
57140ea77a0SAlexander Motin 		GET_STACK_USAGE(st, su);
57240ea77a0SAlexander Motin 		if (su * 2 > st)
57340ea77a0SAlexander Motin 			direct = 0;
57440ea77a0SAlexander Motin 	}
57540ea77a0SAlexander Motin 
57640ea77a0SAlexander Motin 	if (direct) {
57740ea77a0SAlexander Motin 		error = g_io_check(bp);
57840ea77a0SAlexander Motin 		if (error >= 0) {
57940ea77a0SAlexander Motin 			CTR3(KTR_GEOM, "g_io_request g_io_check on bp %p "
58040ea77a0SAlexander Motin 			    "provider %s returned %d", bp, bp->bio_to->name,
58140ea77a0SAlexander Motin 			    error);
58240ea77a0SAlexander Motin 			g_io_deliver(bp, error);
58340ea77a0SAlexander Motin 			return;
58440ea77a0SAlexander Motin 		}
58540ea77a0SAlexander Motin 		bp->bio_to->geom->start(bp);
58640ea77a0SAlexander Motin 	} else {
58740ea77a0SAlexander Motin 		g_bioq_lock(&g_bio_run_down);
5880d883b11SAlexander Motin 		first = TAILQ_EMPTY(&g_bio_run_down.bio_queue);
58919fa21aaSPoul-Henning Kamp 		TAILQ_INSERT_TAIL(&g_bio_run_down.bio_queue, bp, bio_queue);
59040ea77a0SAlexander Motin 		bp->bio_flags |= BIO_ONQUEUE;
59119fa21aaSPoul-Henning Kamp 		g_bio_run_down.bio_queue_length++;
59219fa21aaSPoul-Henning Kamp 		g_bioq_unlock(&g_bio_run_down);
5932fccec19SPoul-Henning Kamp 		/* Pass it on down. */
5940d883b11SAlexander Motin 		if (first)
595dd84a43cSPoul-Henning Kamp 			wakeup(&g_wait_down);
596dd84a43cSPoul-Henning Kamp 	}
59740ea77a0SAlexander Motin }
598dd84a43cSPoul-Henning Kamp 
599dd84a43cSPoul-Henning Kamp void
60072840432SPoul-Henning Kamp g_io_deliver(struct bio *bp, int error)
601dd84a43cSPoul-Henning Kamp {
602e431d66cSAlexander Motin 	struct bintime now;
603801bb689SPoul-Henning Kamp 	struct g_consumer *cp;
604801bb689SPoul-Henning Kamp 	struct g_provider *pp;
60540ea77a0SAlexander Motin 	struct mtx *mtxp;
60640ea77a0SAlexander Motin 	int direct, first;
607dd84a43cSPoul-Henning Kamp 
6088532d381SConrad Meyer 	biotrack(bp, __func__);
6098532d381SConrad Meyer 
610e060b6bdSPoul-Henning Kamp 	KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
611801bb689SPoul-Henning Kamp 	pp = bp->bio_to;
612f7eeab17SPoul-Henning Kamp 	KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver"));
613f7eeab17SPoul-Henning Kamp 	cp = bp->bio_from;
614f7eeab17SPoul-Henning Kamp 	if (cp == NULL) {
615f7eeab17SPoul-Henning Kamp 		bp->bio_error = error;
616f7eeab17SPoul-Henning Kamp 		bp->bio_done(bp);
617f7eeab17SPoul-Henning Kamp 		return;
618f7eeab17SPoul-Henning Kamp 	}
619801bb689SPoul-Henning Kamp 	KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver"));
620801bb689SPoul-Henning Kamp 	KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
621fb231f36SEdward Tomasz Napierala #ifdef DIAGNOSTIC
622fb231f36SEdward Tomasz Napierala 	/*
623fb231f36SEdward Tomasz Napierala 	 * Some classes - GJournal in particular - can modify bio's
624fb231f36SEdward Tomasz Napierala 	 * private fields while the bio is in transit; G_GEOM_VOLATILE_BIO
625fb231f36SEdward Tomasz Napierala 	 * flag means it's an expected behaviour for that particular geom.
626fb231f36SEdward Tomasz Napierala 	 */
627fb231f36SEdward Tomasz Napierala 	if ((cp->geom->flags & G_GEOM_VOLATILE_BIO) == 0) {
628fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_caller1 == bp->_bio_caller1,
629fb231f36SEdward Tomasz Napierala 		    ("bio_caller1 used by the provider %s", pp->name));
630fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_caller2 == bp->_bio_caller2,
631fb231f36SEdward Tomasz Napierala 		    ("bio_caller2 used by the provider %s", pp->name));
632fb231f36SEdward Tomasz Napierala 		KASSERT(bp->bio_cflags == bp->_bio_cflags,
633fb231f36SEdward Tomasz Napierala 		    ("bio_cflags used by the provider %s", pp->name));
634fb231f36SEdward Tomasz Napierala 	}
635fb231f36SEdward Tomasz Napierala #endif
63646aeebecSPawel Jakub Dawidek 	KASSERT(bp->bio_completed >= 0, ("bio_completed can't be less than 0"));
63746aeebecSPawel Jakub Dawidek 	KASSERT(bp->bio_completed <= bp->bio_length,
63846aeebecSPawel Jakub Dawidek 	    ("bio_completed can't be greater than bio_length"));
6395ab413bfSPoul-Henning Kamp 
640dd84a43cSPoul-Henning Kamp 	g_trace(G_T_BIO,
6410355b86eSPoul-Henning Kamp "g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
642801bb689SPoul-Henning Kamp 	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error,
6430355b86eSPoul-Henning Kamp 	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
644801bb689SPoul-Henning Kamp 
64519fa21aaSPoul-Henning Kamp 	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
64619fa21aaSPoul-Henning Kamp 	    ("Bio already on queue bp=%p", bp));
64719fa21aaSPoul-Henning Kamp 
648dcbd0fe5SPoul-Henning Kamp 	/*
649dcbd0fe5SPoul-Henning Kamp 	 * XXX: next two doesn't belong here
650dcbd0fe5SPoul-Henning Kamp 	 */
651e24cbd90SPoul-Henning Kamp 	bp->bio_bcount = bp->bio_length;
652e24cbd90SPoul-Henning Kamp 	bp->bio_resid = bp->bio_bcount - bp->bio_completed;
65319fa21aaSPoul-Henning Kamp 
65440ea77a0SAlexander Motin 	direct = (pp->flags & G_PF_DIRECT_SEND) &&
65540ea77a0SAlexander Motin 		 (cp->flags & G_CF_DIRECT_RECEIVE) &&
65640ea77a0SAlexander Motin 		 !g_is_geom_thread(curthread);
65740ea77a0SAlexander Motin 	if (direct) {
65840ea77a0SAlexander Motin 		/* Block direct execution if less then half of stack left. */
65940ea77a0SAlexander Motin 		size_t	st, su;
66040ea77a0SAlexander Motin 		GET_STACK_USAGE(st, su);
66140ea77a0SAlexander Motin 		if (su * 2 > st)
66240ea77a0SAlexander Motin 			direct = 0;
66340ea77a0SAlexander Motin 	}
66440ea77a0SAlexander Motin 
6658827c821SPoul-Henning Kamp 	/*
6668827c821SPoul-Henning Kamp 	 * The statistics collection is lockless, as such, but we
6678827c821SPoul-Henning Kamp 	 * can not update one instance of the statistics from more
6688827c821SPoul-Henning Kamp 	 * than one thread at a time, so grab the lock first.
6698827c821SPoul-Henning Kamp 	 */
67040ea77a0SAlexander Motin 	if ((g_collectstats & G_STATS_CONSUMERS) != 0 ||
67140ea77a0SAlexander Motin 	    ((g_collectstats & G_STATS_PROVIDERS) != 0 && pp->stat != NULL))
672e431d66cSAlexander Motin 		binuptime(&now);
67306bd74e1SAlexander Motin 	mtxp = mtx_pool_find(mtxpool_sleep, pp);
67440ea77a0SAlexander Motin 	mtx_lock(mtxp);
67540ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_PROVIDERS)
676e431d66cSAlexander Motin 		devstat_end_transaction_bio_bt(pp->stat, bp, &now);
67740ea77a0SAlexander Motin 	if (g_collectstats & G_STATS_CONSUMERS)
678e431d66cSAlexander Motin 		devstat_end_transaction_bio_bt(cp->stat, bp, &now);
6799794a803SAlexander Motin #ifdef INVARIANTS
680c6ae9b5fSPoul-Henning Kamp 	cp->nend++;
6819794a803SAlexander Motin #endif
68240ea77a0SAlexander Motin 	mtx_unlock(mtxp);
68340ea77a0SAlexander Motin 
68419fa21aaSPoul-Henning Kamp 	if (error != ENOMEM) {
68519fa21aaSPoul-Henning Kamp 		bp->bio_error = error;
68640ea77a0SAlexander Motin 		if (direct) {
68740ea77a0SAlexander Motin 			biodone(bp);
68840ea77a0SAlexander Motin 		} else {
68940ea77a0SAlexander Motin 			g_bioq_lock(&g_bio_run_up);
6900d883b11SAlexander Motin 			first = TAILQ_EMPTY(&g_bio_run_up.bio_queue);
69119fa21aaSPoul-Henning Kamp 			TAILQ_INSERT_TAIL(&g_bio_run_up.bio_queue, bp, bio_queue);
692276f72c5SPoul-Henning Kamp 			bp->bio_flags |= BIO_ONQUEUE;
69319fa21aaSPoul-Henning Kamp 			g_bio_run_up.bio_queue_length++;
69419fa21aaSPoul-Henning Kamp 			g_bioq_unlock(&g_bio_run_up);
6950d883b11SAlexander Motin 			if (first)
69619fa21aaSPoul-Henning Kamp 				wakeup(&g_wait_up);
69740ea77a0SAlexander Motin 		}
69819fa21aaSPoul-Henning Kamp 		return;
69919fa21aaSPoul-Henning Kamp 	}
700dd84a43cSPoul-Henning Kamp 
7012cc9686eSPoul-Henning Kamp 	if (bootverbose)
702801bb689SPoul-Henning Kamp 		printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name);
7031b949c05SPawel Jakub Dawidek 	bp->bio_children = 0;
7041b949c05SPawel Jakub Dawidek 	bp->bio_inbed = 0;
70560114438SPawel Jakub Dawidek 	bp->bio_driver1 = NULL;
70660114438SPawel Jakub Dawidek 	bp->bio_driver2 = NULL;
70760114438SPawel Jakub Dawidek 	bp->bio_pflags = 0;
708801bb689SPoul-Henning Kamp 	g_io_request(bp, cp);
7093f2e5b85SWarner Losh 	pace = 1;
7103432e4fdSPoul-Henning Kamp 	return;
7113432e4fdSPoul-Henning Kamp }
712dd84a43cSPoul-Henning Kamp 
713ee75e7deSKonstantin Belousov SYSCTL_DECL(_kern_geom);
714ee75e7deSKonstantin Belousov 
715ee75e7deSKonstantin Belousov static long transient_maps;
716ee75e7deSKonstantin Belousov SYSCTL_LONG(_kern_geom, OID_AUTO, transient_maps, CTLFLAG_RD,
717ee75e7deSKonstantin Belousov     &transient_maps, 0,
718ee75e7deSKonstantin Belousov     "Total count of the transient mapping requests");
719ee75e7deSKonstantin Belousov u_int transient_map_retries = 10;
720ee75e7deSKonstantin Belousov SYSCTL_UINT(_kern_geom, OID_AUTO, transient_map_retries, CTLFLAG_RW,
721ee75e7deSKonstantin Belousov     &transient_map_retries, 0,
722ee75e7deSKonstantin Belousov     "Max count of retries used before giving up on creating transient map");
723ee75e7deSKonstantin Belousov int transient_map_hard_failures;
724ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, transient_map_hard_failures, CTLFLAG_RD,
725ee75e7deSKonstantin Belousov     &transient_map_hard_failures, 0,
726ee75e7deSKonstantin Belousov     "Failures to establish the transient mapping due to retry attempts "
727ee75e7deSKonstantin Belousov     "exhausted");
728ee75e7deSKonstantin Belousov int transient_map_soft_failures;
729ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, transient_map_soft_failures, CTLFLAG_RD,
730ee75e7deSKonstantin Belousov     &transient_map_soft_failures, 0,
731ee75e7deSKonstantin Belousov     "Count of retried failures to establish the transient mapping");
732ee75e7deSKonstantin Belousov int inflight_transient_maps;
733ee75e7deSKonstantin Belousov SYSCTL_INT(_kern_geom, OID_AUTO, inflight_transient_maps, CTLFLAG_RD,
734ee75e7deSKonstantin Belousov     &inflight_transient_maps, 0,
735ee75e7deSKonstantin Belousov     "Current count of the active transient maps");
736ee75e7deSKonstantin Belousov 
737ee75e7deSKonstantin Belousov static int
738ee75e7deSKonstantin Belousov g_io_transient_map_bio(struct bio *bp)
739ee75e7deSKonstantin Belousov {
740ee75e7deSKonstantin Belousov 	vm_offset_t addr;
741ee75e7deSKonstantin Belousov 	long size;
742ee75e7deSKonstantin Belousov 	u_int retried;
743ee75e7deSKonstantin Belousov 
7446c83fce3SKonstantin Belousov 	KASSERT(unmapped_buf_allowed, ("unmapped disabled"));
7456c83fce3SKonstantin Belousov 
746ee75e7deSKonstantin Belousov 	size = round_page(bp->bio_ma_offset + bp->bio_length);
747ee75e7deSKonstantin Belousov 	KASSERT(size / PAGE_SIZE == bp->bio_ma_n, ("Bio too short %p", bp));
748ee75e7deSKonstantin Belousov 	addr = 0;
749ee75e7deSKonstantin Belousov 	retried = 0;
750ee75e7deSKonstantin Belousov 	atomic_add_long(&transient_maps, 1);
751ee75e7deSKonstantin Belousov retry:
7525f518366SJeff Roberson 	if (vmem_alloc(transient_arena, size, M_BESTFIT | M_NOWAIT, &addr)) {
753ee75e7deSKonstantin Belousov 		if (transient_map_retries != 0 &&
754ee75e7deSKonstantin Belousov 		    retried >= transient_map_retries) {
755ee75e7deSKonstantin Belousov 			CTR2(KTR_GEOM, "g_down cannot map bp %p provider %s",
756ee75e7deSKonstantin Belousov 			    bp, bp->bio_to->name);
757ee75e7deSKonstantin Belousov 			atomic_add_int(&transient_map_hard_failures, 1);
75840ea77a0SAlexander Motin 			return (EDEADLK/* XXXKIB */);
759ee75e7deSKonstantin Belousov 		} else {
760ee75e7deSKonstantin Belousov 			/*
761ee75e7deSKonstantin Belousov 			 * Naive attempt to quisce the I/O to get more
762ee75e7deSKonstantin Belousov 			 * in-flight requests completed and defragment
7635f518366SJeff Roberson 			 * the transient_arena.
764ee75e7deSKonstantin Belousov 			 */
765ee75e7deSKonstantin Belousov 			CTR3(KTR_GEOM, "g_down retrymap bp %p provider %s r %d",
766ee75e7deSKonstantin Belousov 			    bp, bp->bio_to->name, retried);
767ee75e7deSKonstantin Belousov 			pause("g_d_tra", hz / 10);
768ee75e7deSKonstantin Belousov 			retried++;
769ee75e7deSKonstantin Belousov 			atomic_add_int(&transient_map_soft_failures, 1);
770ee75e7deSKonstantin Belousov 			goto retry;
771ee75e7deSKonstantin Belousov 		}
772ee75e7deSKonstantin Belousov 	}
773ee75e7deSKonstantin Belousov 	atomic_add_int(&inflight_transient_maps, 1);
774ee75e7deSKonstantin Belousov 	pmap_qenter((vm_offset_t)addr, bp->bio_ma, OFF_TO_IDX(size));
775ee75e7deSKonstantin Belousov 	bp->bio_data = (caddr_t)addr + bp->bio_ma_offset;
776ee75e7deSKonstantin Belousov 	bp->bio_flags |= BIO_TRANSIENT_MAPPING;
777ee75e7deSKonstantin Belousov 	bp->bio_flags &= ~BIO_UNMAPPED;
77840ea77a0SAlexander Motin 	return (EJUSTRETURN);
779ee75e7deSKonstantin Belousov }
780ee75e7deSKonstantin Belousov 
781dd84a43cSPoul-Henning Kamp void
782dd84a43cSPoul-Henning Kamp g_io_schedule_down(struct thread *tp __unused)
783dd84a43cSPoul-Henning Kamp {
784dd84a43cSPoul-Henning Kamp 	struct bio *bp;
785e39d70d4SPoul-Henning Kamp 	int error;
786dd84a43cSPoul-Henning Kamp 
787dd84a43cSPoul-Henning Kamp 	for(;;) {
788f0e185d7SPoul-Henning Kamp 		g_bioq_lock(&g_bio_run_down);
789dd84a43cSPoul-Henning Kamp 		bp = g_bioq_first(&g_bio_run_down);
790f0e185d7SPoul-Henning Kamp 		if (bp == NULL) {
79149dbb61dSRobert Watson 			CTR0(KTR_GEOM, "g_down going to sleep");
792f0e185d7SPoul-Henning Kamp 			msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock,
7937fc019afSAlexander Motin 			    PRIBIO | PDROP, "-", 0);
794f0e185d7SPoul-Henning Kamp 			continue;
795f0e185d7SPoul-Henning Kamp 		}
79649dbb61dSRobert Watson 		CTR0(KTR_GEOM, "g_down has work to do");
797f0e185d7SPoul-Henning Kamp 		g_bioq_unlock(&g_bio_run_down);
7988532d381SConrad Meyer 		biotrack(bp, __func__);
7993f2e5b85SWarner Losh 		if (pace != 0) {
8003f2e5b85SWarner Losh 			/*
8013f2e5b85SWarner Losh 			 * There has been at least one memory allocation
8023f2e5b85SWarner Losh 			 * failure since the last I/O completed. Pause 1ms to
8033f2e5b85SWarner Losh 			 * give the system a chance to free up memory. We only
8043f2e5b85SWarner Losh 			 * do this once because a large number of allocations
8053f2e5b85SWarner Losh 			 * can fail in the direct dispatch case and there's no
8063f2e5b85SWarner Losh 			 * relationship between the number of these failures and
8073f2e5b85SWarner Losh 			 * the length of the outage. If there's still an outage,
8083f2e5b85SWarner Losh 			 * we'll pause again and again until it's
8093f2e5b85SWarner Losh 			 * resolved. Older versions paused longer and once per
8103f2e5b85SWarner Losh 			 * allocation failure. This was OK for a single threaded
8113f2e5b85SWarner Losh 			 * g_down, but with direct dispatch would lead to max of
8123f2e5b85SWarner Losh 			 * 10 IOPs for minutes at a time when transient memory
8133f2e5b85SWarner Losh 			 * issues prevented allocation for a batch of requests
8143f2e5b85SWarner Losh 			 * from the upper layers.
8153f2e5b85SWarner Losh 			 *
8163f2e5b85SWarner Losh 			 * XXX This pacing is really lame. It needs to be solved
8173f2e5b85SWarner Losh 			 * by other methods. This is OK only because the worst
8183f2e5b85SWarner Losh 			 * case scenario is so rare. In the worst case scenario
8193f2e5b85SWarner Losh 			 * all memory is tied up waiting for I/O to complete
8203f2e5b85SWarner Losh 			 * which can never happen since we can't allocate bios
8213f2e5b85SWarner Losh 			 * for that I/O.
8223f2e5b85SWarner Losh 			 */
8233f2e5b85SWarner Losh 			CTR0(KTR_GEOM, "g_down pacing self");
8243f2e5b85SWarner Losh 			pause("g_down", min(hz/1000, 1));
8253f2e5b85SWarner Losh 			pace = 0;
826376ceb79SPoul-Henning Kamp 		}
82740ea77a0SAlexander Motin 		CTR2(KTR_GEOM, "g_down processing bp %p provider %s", bp,
82840ea77a0SAlexander Motin 		    bp->bio_to->name);
829e39d70d4SPoul-Henning Kamp 		error = g_io_check(bp);
83040ea77a0SAlexander Motin 		if (error >= 0) {
83149dbb61dSRobert Watson 			CTR3(KTR_GEOM, "g_down g_io_check on bp %p provider "
83249dbb61dSRobert Watson 			    "%s returned %d", bp, bp->bio_to->name, error);
833e39d70d4SPoul-Henning Kamp 			g_io_deliver(bp, error);
834e39d70d4SPoul-Henning Kamp 			continue;
835e39d70d4SPoul-Henning Kamp 		}
83651460da8SJohn Baldwin 		THREAD_NO_SLEEPING();
83749dbb61dSRobert Watson 		CTR4(KTR_GEOM, "g_down starting bp %p provider %s off %ld "
83849dbb61dSRobert Watson 		    "len %ld", bp, bp->bio_to->name, bp->bio_offset,
83949dbb61dSRobert Watson 		    bp->bio_length);
840dd84a43cSPoul-Henning Kamp 		bp->bio_to->geom->start(bp);
84151460da8SJohn Baldwin 		THREAD_SLEEPING_OK();
842dd84a43cSPoul-Henning Kamp 	}
843dd84a43cSPoul-Henning Kamp }
844dd84a43cSPoul-Henning Kamp 
845dd84a43cSPoul-Henning Kamp void
846dd84a43cSPoul-Henning Kamp g_io_schedule_up(struct thread *tp __unused)
847dd84a43cSPoul-Henning Kamp {
848dd84a43cSPoul-Henning Kamp 	struct bio *bp;
8490c4440c3SEdward Tomasz Napierala 
850dd84a43cSPoul-Henning Kamp 	for(;;) {
851f0e185d7SPoul-Henning Kamp 		g_bioq_lock(&g_bio_run_up);
8520c4440c3SEdward Tomasz Napierala 		bp = g_bioq_first(&g_bio_run_up);
8530c4440c3SEdward Tomasz Napierala 		if (bp == NULL) {
8540c4440c3SEdward Tomasz Napierala 			CTR0(KTR_GEOM, "g_up going to sleep");
8550c4440c3SEdward Tomasz Napierala 			msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock,
8560c4440c3SEdward Tomasz Napierala 			    PRIBIO | PDROP, "-", 0);
8575fcf4e43SPoul-Henning Kamp 			continue;
8585fcf4e43SPoul-Henning Kamp 		}
859f0e185d7SPoul-Henning Kamp 		g_bioq_unlock(&g_bio_run_up);
86051460da8SJohn Baldwin 		THREAD_NO_SLEEPING();
86149dbb61dSRobert Watson 		CTR4(KTR_GEOM, "g_up biodone bp %p provider %s off "
862c4901b67SSean Bruno 		    "%jd len %ld", bp, bp->bio_to->name,
86349dbb61dSRobert Watson 		    bp->bio_offset, bp->bio_length);
86453706245SPoul-Henning Kamp 		biodone(bp);
86551460da8SJohn Baldwin 		THREAD_SLEEPING_OK();
866dd84a43cSPoul-Henning Kamp 	}
867dd84a43cSPoul-Henning Kamp }
868dd84a43cSPoul-Henning Kamp 
869dd84a43cSPoul-Henning Kamp void *
870dd84a43cSPoul-Henning Kamp g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
871dd84a43cSPoul-Henning Kamp {
872dd84a43cSPoul-Henning Kamp 	struct bio *bp;
873dd84a43cSPoul-Henning Kamp 	void *ptr;
874dd84a43cSPoul-Henning Kamp 	int errorc;
875dd84a43cSPoul-Henning Kamp 
8768dd5480dSPawel Jakub Dawidek 	KASSERT(length > 0 && length >= cp->provider->sectorsize &&
877cd853791SKonstantin Belousov 	    length <= maxphys, ("g_read_data(): invalid length %jd",
8788dd5480dSPawel Jakub Dawidek 	    (intmax_t)length));
8793eb6ffdfSPoul-Henning Kamp 
880a2033c96SPoul-Henning Kamp 	bp = g_alloc_bio();
881dd84a43cSPoul-Henning Kamp 	bp->bio_cmd = BIO_READ;
882dd84a43cSPoul-Henning Kamp 	bp->bio_done = NULL;
883dd84a43cSPoul-Henning Kamp 	bp->bio_offset = offset;
884dd84a43cSPoul-Henning Kamp 	bp->bio_length = length;
885a163d034SWarner Losh 	ptr = g_malloc(length, M_WAITOK);
886dd84a43cSPoul-Henning Kamp 	bp->bio_data = ptr;
887dd84a43cSPoul-Henning Kamp 	g_io_request(bp, cp);
88853706245SPoul-Henning Kamp 	errorc = biowait(bp, "gread");
889*d91d2b51SMark Johnston 	if (errorc == 0 && bp->bio_completed != length)
890*d91d2b51SMark Johnston 		errorc = EIO;
891dd84a43cSPoul-Henning Kamp 	if (error != NULL)
892dd84a43cSPoul-Henning Kamp 		*error = errorc;
893dd84a43cSPoul-Henning Kamp 	g_destroy_bio(bp);
894dd84a43cSPoul-Henning Kamp 	if (errorc) {
895dd84a43cSPoul-Henning Kamp 		g_free(ptr);
896dd84a43cSPoul-Henning Kamp 		ptr = NULL;
897dd84a43cSPoul-Henning Kamp 	}
898dd84a43cSPoul-Henning Kamp 	return (ptr);
899dd84a43cSPoul-Henning Kamp }
90090b1cd56SPoul-Henning Kamp 
901dffce215SKirk McKusick /*
902dffce215SKirk McKusick  * A read function for use by ffs_sbget when used by GEOM-layer routines.
903dffce215SKirk McKusick  */
904dffce215SKirk McKusick int
905dffce215SKirk McKusick g_use_g_read_data(void *devfd, off_t loc, void **bufp, int size)
906dffce215SKirk McKusick {
907dffce215SKirk McKusick 	struct g_consumer *cp;
908dffce215SKirk McKusick 
909efbf3964SKirk McKusick 	KASSERT(*bufp == NULL,
910efbf3964SKirk McKusick 	    ("g_use_g_read_data: non-NULL *bufp %p\n", *bufp));
911efbf3964SKirk McKusick 
912dffce215SKirk McKusick 	cp = (struct g_consumer *)devfd;
913dffce215SKirk McKusick 	/*
914dffce215SKirk McKusick 	 * Take care not to issue an invalid I/O request. The offset of
915dffce215SKirk McKusick 	 * the superblock candidate must be multiples of the provider's
916dffce215SKirk McKusick 	 * sector size, otherwise an FFS can't exist on the provider
917dffce215SKirk McKusick 	 * anyway.
918dffce215SKirk McKusick 	 */
919dffce215SKirk McKusick 	if (loc % cp->provider->sectorsize != 0)
920dffce215SKirk McKusick 		return (ENOENT);
921dffce215SKirk McKusick 	*bufp = g_read_data(cp, loc, size, NULL);
922dffce215SKirk McKusick 	if (*bufp == NULL)
923dffce215SKirk McKusick 		return (ENOENT);
924dffce215SKirk McKusick 	return (0);
925dffce215SKirk McKusick }
926dffce215SKirk McKusick 
92790b1cd56SPoul-Henning Kamp int
92890b1cd56SPoul-Henning Kamp g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
92990b1cd56SPoul-Henning Kamp {
93090b1cd56SPoul-Henning Kamp 	struct bio *bp;
93190b1cd56SPoul-Henning Kamp 	int error;
93290b1cd56SPoul-Henning Kamp 
9338dd5480dSPawel Jakub Dawidek 	KASSERT(length > 0 && length >= cp->provider->sectorsize &&
934cd853791SKonstantin Belousov 	    length <= maxphys, ("g_write_data(): invalid length %jd",
9358dd5480dSPawel Jakub Dawidek 	    (intmax_t)length));
9363eb6ffdfSPoul-Henning Kamp 
937a2033c96SPoul-Henning Kamp 	bp = g_alloc_bio();
93890b1cd56SPoul-Henning Kamp 	bp->bio_cmd = BIO_WRITE;
93990b1cd56SPoul-Henning Kamp 	bp->bio_done = NULL;
94090b1cd56SPoul-Henning Kamp 	bp->bio_offset = offset;
94190b1cd56SPoul-Henning Kamp 	bp->bio_length = length;
94290b1cd56SPoul-Henning Kamp 	bp->bio_data = ptr;
94390b1cd56SPoul-Henning Kamp 	g_io_request(bp, cp);
94490b1cd56SPoul-Henning Kamp 	error = biowait(bp, "gwrite");
945*d91d2b51SMark Johnston 	if (error == 0 && bp->bio_completed != length)
946*d91d2b51SMark Johnston 		error = EIO;
94790b1cd56SPoul-Henning Kamp 	g_destroy_bio(bp);
94890b1cd56SPoul-Henning Kamp 	return (error);
94990b1cd56SPoul-Henning Kamp }
95072e33095SPawel Jakub Dawidek 
951dffce215SKirk McKusick /*
952dffce215SKirk McKusick  * A write function for use by ffs_sbput when used by GEOM-layer routines.
953dffce215SKirk McKusick  */
954dffce215SKirk McKusick int
955dffce215SKirk McKusick g_use_g_write_data(void *devfd, off_t loc, void *buf, int size)
956dffce215SKirk McKusick {
957dffce215SKirk McKusick 
958dffce215SKirk McKusick 	return (g_write_data((struct g_consumer *)devfd, loc, buf, size));
959dffce215SKirk McKusick }
960dffce215SKirk McKusick 
9612b17fb95SPawel Jakub Dawidek int
9622b17fb95SPawel Jakub Dawidek g_delete_data(struct g_consumer *cp, off_t offset, off_t length)
9632b17fb95SPawel Jakub Dawidek {
9642b17fb95SPawel Jakub Dawidek 	struct bio *bp;
9652b17fb95SPawel Jakub Dawidek 	int error;
9662b17fb95SPawel Jakub Dawidek 
967eed6cda9SPoul-Henning Kamp 	KASSERT(length > 0 && length >= cp->provider->sectorsize,
968eed6cda9SPoul-Henning Kamp 	    ("g_delete_data(): invalid length %jd", (intmax_t)length));
9692b17fb95SPawel Jakub Dawidek 
9702b17fb95SPawel Jakub Dawidek 	bp = g_alloc_bio();
9712b17fb95SPawel Jakub Dawidek 	bp->bio_cmd = BIO_DELETE;
9722b17fb95SPawel Jakub Dawidek 	bp->bio_done = NULL;
9732b17fb95SPawel Jakub Dawidek 	bp->bio_offset = offset;
9742b17fb95SPawel Jakub Dawidek 	bp->bio_length = length;
9752b17fb95SPawel Jakub Dawidek 	bp->bio_data = NULL;
9762b17fb95SPawel Jakub Dawidek 	g_io_request(bp, cp);
9772b17fb95SPawel Jakub Dawidek 	error = biowait(bp, "gdelete");
978*d91d2b51SMark Johnston 	if (error == 0 && bp->bio_completed != length)
979*d91d2b51SMark Johnston 		error = EIO;
9802b17fb95SPawel Jakub Dawidek 	g_destroy_bio(bp);
9812b17fb95SPawel Jakub Dawidek 	return (error);
9822b17fb95SPawel Jakub Dawidek }
9832b17fb95SPawel Jakub Dawidek 
98472e33095SPawel Jakub Dawidek void
985ac03832eSConrad Meyer g_print_bio(const char *prefix, const struct bio *bp, const char *fmtsuffix,
986ac03832eSConrad Meyer     ...)
987ac03832eSConrad Meyer {
988ac03832eSConrad Meyer #ifndef PRINTF_BUFR_SIZE
989ac03832eSConrad Meyer #define PRINTF_BUFR_SIZE 64
990ac03832eSConrad Meyer #endif
991ac03832eSConrad Meyer 	char bufr[PRINTF_BUFR_SIZE];
992ac03832eSConrad Meyer 	struct sbuf sb, *sbp __unused;
993ac03832eSConrad Meyer 	va_list ap;
994ac03832eSConrad Meyer 
995ac03832eSConrad Meyer 	sbp = sbuf_new(&sb, bufr, sizeof(bufr), SBUF_FIXEDLEN);
996ac03832eSConrad Meyer 	KASSERT(sbp != NULL, ("sbuf_new misused?"));
997ac03832eSConrad Meyer 
998ac03832eSConrad Meyer 	sbuf_set_drain(&sb, sbuf_printf_drain, NULL);
999ac03832eSConrad Meyer 
1000ac03832eSConrad Meyer 	sbuf_cat(&sb, prefix);
1001ac03832eSConrad Meyer 	g_format_bio(&sb, bp);
1002ac03832eSConrad Meyer 
1003ac03832eSConrad Meyer 	va_start(ap, fmtsuffix);
1004ac03832eSConrad Meyer 	sbuf_vprintf(&sb, fmtsuffix, ap);
1005ac03832eSConrad Meyer 	va_end(ap);
1006ac03832eSConrad Meyer 
1007ac03832eSConrad Meyer 	sbuf_nl_terminate(&sb);
1008ac03832eSConrad Meyer 
1009ac03832eSConrad Meyer 	sbuf_finish(&sb);
1010ac03832eSConrad Meyer 	sbuf_delete(&sb);
1011ac03832eSConrad Meyer }
1012ac03832eSConrad Meyer 
1013ac03832eSConrad Meyer void
1014ac03832eSConrad Meyer g_format_bio(struct sbuf *sb, const struct bio *bp)
101572e33095SPawel Jakub Dawidek {
101672e33095SPawel Jakub Dawidek 	const char *pname, *cmd = NULL;
101772e33095SPawel Jakub Dawidek 
101872e33095SPawel Jakub Dawidek 	if (bp->bio_to != NULL)
101972e33095SPawel Jakub Dawidek 		pname = bp->bio_to->name;
102072e33095SPawel Jakub Dawidek 	else
102172e33095SPawel Jakub Dawidek 		pname = "[unknown]";
102272e33095SPawel Jakub Dawidek 
102372e33095SPawel Jakub Dawidek 	switch (bp->bio_cmd) {
102472e33095SPawel Jakub Dawidek 	case BIO_GETATTR:
102572e33095SPawel Jakub Dawidek 		cmd = "GETATTR";
1026ac03832eSConrad Meyer 		sbuf_printf(sb, "%s[%s(attr=%s)]", pname, cmd,
1027ac03832eSConrad Meyer 		    bp->bio_attribute);
102872e33095SPawel Jakub Dawidek 		return;
1029c3618c65SPawel Jakub Dawidek 	case BIO_FLUSH:
1030c3618c65SPawel Jakub Dawidek 		cmd = "FLUSH";
1031ac03832eSConrad Meyer 		sbuf_printf(sb, "%s[%s]", pname, cmd);
1032c3618c65SPawel Jakub Dawidek 		return;
10339a6844d5SKenneth D. Merry 	case BIO_ZONE: {
10349a6844d5SKenneth D. Merry 		char *subcmd = NULL;
10359a6844d5SKenneth D. Merry 		cmd = "ZONE";
10369a6844d5SKenneth D. Merry 		switch (bp->bio_zone.zone_cmd) {
10379a6844d5SKenneth D. Merry 		case DISK_ZONE_OPEN:
10389a6844d5SKenneth D. Merry 			subcmd = "OPEN";
10399a6844d5SKenneth D. Merry 			break;
10409a6844d5SKenneth D. Merry 		case DISK_ZONE_CLOSE:
10419a6844d5SKenneth D. Merry 			subcmd = "CLOSE";
10429a6844d5SKenneth D. Merry 			break;
10439a6844d5SKenneth D. Merry 		case DISK_ZONE_FINISH:
10449a6844d5SKenneth D. Merry 			subcmd = "FINISH";
10459a6844d5SKenneth D. Merry 			break;
10469a6844d5SKenneth D. Merry 		case DISK_ZONE_RWP:
10479a6844d5SKenneth D. Merry 			subcmd = "RWP";
10489a6844d5SKenneth D. Merry 			break;
10499a6844d5SKenneth D. Merry 		case DISK_ZONE_REPORT_ZONES:
10509a6844d5SKenneth D. Merry 			subcmd = "REPORT ZONES";
10519a6844d5SKenneth D. Merry 			break;
10529a6844d5SKenneth D. Merry 		case DISK_ZONE_GET_PARAMS:
10539a6844d5SKenneth D. Merry 			subcmd = "GET PARAMS";
10549a6844d5SKenneth D. Merry 			break;
10559a6844d5SKenneth D. Merry 		default:
10569a6844d5SKenneth D. Merry 			subcmd = "UNKNOWN";
10579a6844d5SKenneth D. Merry 			break;
10589a6844d5SKenneth D. Merry 		}
1059ac03832eSConrad Meyer 		sbuf_printf(sb, "%s[%s,%s]", pname, cmd, subcmd);
10609a6844d5SKenneth D. Merry 		return;
10619a6844d5SKenneth D. Merry 	}
106272e33095SPawel Jakub Dawidek 	case BIO_READ:
106372e33095SPawel Jakub Dawidek 		cmd = "READ";
10647ce513a5SEdward Tomasz Napierala 		break;
106572e33095SPawel Jakub Dawidek 	case BIO_WRITE:
106672e33095SPawel Jakub Dawidek 		cmd = "WRITE";
10677ce513a5SEdward Tomasz Napierala 		break;
106872e33095SPawel Jakub Dawidek 	case BIO_DELETE:
106972e33095SPawel Jakub Dawidek 		cmd = "DELETE";
10707ce513a5SEdward Tomasz Napierala 		break;
107172e33095SPawel Jakub Dawidek 	default:
107272e33095SPawel Jakub Dawidek 		cmd = "UNKNOWN";
1073ac03832eSConrad Meyer 		sbuf_printf(sb, "%s[%s()]", pname, cmd);
107472e33095SPawel Jakub Dawidek 		return;
107572e33095SPawel Jakub Dawidek 	}
1076ac03832eSConrad Meyer 	sbuf_printf(sb, "%s[%s(offset=%jd, length=%jd)]", pname, cmd,
10777ce513a5SEdward Tomasz Napierala 	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
107872e33095SPawel Jakub Dawidek }
1079