geom_io.c revision 238886
1/*-
2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
4 * All rights reserved.
5 *
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 *    products derived from this software without specific prior written
21 *    permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD: head/sys/geom/geom_io.c 238886 2012-07-29 11:51:48Z mav $");
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/malloc.h>
43#include <sys/bio.h>
44#include <sys/ktr.h>
45#include <sys/proc.h>
46#include <sys/stack.h>
47
48#include <sys/errno.h>
49#include <geom/geom.h>
50#include <geom/geom_int.h>
51#include <sys/devicestat.h>
52
53#include <vm/uma.h>
54
55static struct g_bioq g_bio_run_down;
56static struct g_bioq g_bio_run_up;
57static struct g_bioq g_bio_run_task;
58
59static u_int pace;
60static uma_zone_t	biozone;
61
62/*
63 * The head of the list of classifiers used in g_io_request.
64 * Use g_register_classifier() and g_unregister_classifier()
65 * to add/remove entries to the list.
66 * Classifiers are invoked in registration order.
67 */
68static TAILQ_HEAD(g_classifier_tailq, g_classifier_hook)
69    g_classifier_tailq = TAILQ_HEAD_INITIALIZER(g_classifier_tailq);
70
71#include <machine/atomic.h>
72
73static void
74g_bioq_lock(struct g_bioq *bq)
75{
76
77	mtx_lock(&bq->bio_queue_lock);
78}
79
80static void
81g_bioq_unlock(struct g_bioq *bq)
82{
83
84	mtx_unlock(&bq->bio_queue_lock);
85}
86
87#if 0
88static void
89g_bioq_destroy(struct g_bioq *bq)
90{
91
92	mtx_destroy(&bq->bio_queue_lock);
93}
94#endif
95
96static void
97g_bioq_init(struct g_bioq *bq)
98{
99
100	TAILQ_INIT(&bq->bio_queue);
101	mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF);
102}
103
104static struct bio *
105g_bioq_first(struct g_bioq *bq)
106{
107	struct bio *bp;
108
109	bp = TAILQ_FIRST(&bq->bio_queue);
110	if (bp != NULL) {
111		KASSERT((bp->bio_flags & BIO_ONQUEUE),
112		    ("Bio not on queue bp=%p target %p", bp, bq));
113		bp->bio_flags &= ~BIO_ONQUEUE;
114		TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
115		bq->bio_queue_length--;
116	}
117	return (bp);
118}
119
120struct bio *
121g_new_bio(void)
122{
123	struct bio *bp;
124
125	bp = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
126#ifdef KTR
127	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
128		struct stack st;
129
130		CTR1(KTR_GEOM, "g_new_bio(): %p", bp);
131		stack_save(&st);
132		CTRSTACK(KTR_GEOM, &st, 3, 0);
133	}
134#endif
135	return (bp);
136}
137
138struct bio *
139g_alloc_bio(void)
140{
141	struct bio *bp;
142
143	bp = uma_zalloc(biozone, M_WAITOK | M_ZERO);
144#ifdef KTR
145	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
146		struct stack st;
147
148		CTR1(KTR_GEOM, "g_alloc_bio(): %p", bp);
149		stack_save(&st);
150		CTRSTACK(KTR_GEOM, &st, 3, 0);
151	}
152#endif
153	return (bp);
154}
155
156void
157g_destroy_bio(struct bio *bp)
158{
159#ifdef KTR
160	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
161		struct stack st;
162
163		CTR1(KTR_GEOM, "g_destroy_bio(): %p", bp);
164		stack_save(&st);
165		CTRSTACK(KTR_GEOM, &st, 3, 0);
166	}
167#endif
168	uma_zfree(biozone, bp);
169}
170
171struct bio *
172g_clone_bio(struct bio *bp)
173{
174	struct bio *bp2;
175
176	bp2 = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
177	if (bp2 != NULL) {
178		bp2->bio_parent = bp;
179		bp2->bio_cmd = bp->bio_cmd;
180		bp2->bio_length = bp->bio_length;
181		bp2->bio_offset = bp->bio_offset;
182		bp2->bio_data = bp->bio_data;
183		bp2->bio_attribute = bp->bio_attribute;
184		/* Inherit classification info from the parent */
185		bp2->bio_classifier1 = bp->bio_classifier1;
186		bp2->bio_classifier2 = bp->bio_classifier2;
187		bp->bio_children++;
188	}
189#ifdef KTR
190	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
191		struct stack st;
192
193		CTR2(KTR_GEOM, "g_clone_bio(%p): %p", bp, bp2);
194		stack_save(&st);
195		CTRSTACK(KTR_GEOM, &st, 3, 0);
196	}
197#endif
198	return(bp2);
199}
200
201struct bio *
202g_duplicate_bio(struct bio *bp)
203{
204	struct bio *bp2;
205
206	bp2 = uma_zalloc(biozone, M_WAITOK | M_ZERO);
207	bp2->bio_parent = bp;
208	bp2->bio_cmd = bp->bio_cmd;
209	bp2->bio_length = bp->bio_length;
210	bp2->bio_offset = bp->bio_offset;
211	bp2->bio_data = bp->bio_data;
212	bp2->bio_attribute = bp->bio_attribute;
213	bp->bio_children++;
214#ifdef KTR
215	if ((KTR_COMPILE & KTR_GEOM) && (ktr_mask & KTR_GEOM)) {
216		struct stack st;
217
218		CTR2(KTR_GEOM, "g_duplicate_bio(%p): %p", bp, bp2);
219		stack_save(&st);
220		CTRSTACK(KTR_GEOM, &st, 3, 0);
221	}
222#endif
223	return(bp2);
224}
225
226void
227g_io_init()
228{
229
230	g_bioq_init(&g_bio_run_down);
231	g_bioq_init(&g_bio_run_up);
232	g_bioq_init(&g_bio_run_task);
233	biozone = uma_zcreate("g_bio", sizeof (struct bio),
234	    NULL, NULL,
235	    NULL, NULL,
236	    0, 0);
237}
238
239int
240g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr)
241{
242	struct bio *bp;
243	int error;
244
245	g_trace(G_T_BIO, "bio_getattr(%s)", attr);
246	bp = g_alloc_bio();
247	bp->bio_cmd = BIO_GETATTR;
248	bp->bio_done = NULL;
249	bp->bio_attribute = attr;
250	bp->bio_length = *len;
251	bp->bio_data = ptr;
252	g_io_request(bp, cp);
253	error = biowait(bp, "ggetattr");
254	*len = bp->bio_completed;
255	g_destroy_bio(bp);
256	return (error);
257}
258
259int
260g_io_flush(struct g_consumer *cp)
261{
262	struct bio *bp;
263	int error;
264
265	g_trace(G_T_BIO, "bio_flush(%s)", cp->provider->name);
266	bp = g_alloc_bio();
267	bp->bio_cmd = BIO_FLUSH;
268	bp->bio_flags |= BIO_ORDERED;
269	bp->bio_done = NULL;
270	bp->bio_attribute = NULL;
271	bp->bio_offset = cp->provider->mediasize;
272	bp->bio_length = 0;
273	bp->bio_data = NULL;
274	g_io_request(bp, cp);
275	error = biowait(bp, "gflush");
276	g_destroy_bio(bp);
277	return (error);
278}
279
280static int
281g_io_check(struct bio *bp)
282{
283	struct g_consumer *cp;
284	struct g_provider *pp;
285
286	cp = bp->bio_from;
287	pp = bp->bio_to;
288
289	/* Fail if access counters dont allow the operation */
290	switch(bp->bio_cmd) {
291	case BIO_READ:
292	case BIO_GETATTR:
293		if (cp->acr == 0)
294			return (EPERM);
295		break;
296	case BIO_WRITE:
297	case BIO_DELETE:
298	case BIO_FLUSH:
299		if (cp->acw == 0)
300			return (EPERM);
301		break;
302	default:
303		return (EPERM);
304	}
305	/* if provider is marked for error, don't disturb. */
306	if (pp->error)
307		return (pp->error);
308	if (cp->flags & G_CF_ORPHAN)
309		return (ENXIO);
310
311	switch(bp->bio_cmd) {
312	case BIO_READ:
313	case BIO_WRITE:
314	case BIO_DELETE:
315		/* Zero sectorsize or mediasize is probably a lack of media. */
316		if (pp->sectorsize == 0 || pp->mediasize == 0)
317			return (ENXIO);
318		/* Reject I/O not on sector boundary */
319		if (bp->bio_offset % pp->sectorsize)
320			return (EINVAL);
321		/* Reject I/O not integral sector long */
322		if (bp->bio_length % pp->sectorsize)
323			return (EINVAL);
324		/* Reject requests before or past the end of media. */
325		if (bp->bio_offset < 0)
326			return (EIO);
327		if (bp->bio_offset > pp->mediasize)
328			return (EIO);
329		break;
330	default:
331		break;
332	}
333	return (0);
334}
335
336/*
337 * bio classification support.
338 *
339 * g_register_classifier() and g_unregister_classifier()
340 * are used to add/remove a classifier from the list.
341 * The list is protected using the g_bio_run_down lock,
342 * because the classifiers are called in this path.
343 *
344 * g_io_request() passes bio's that are not already classified
345 * (i.e. those with bio_classifier1 == NULL) to g_run_classifiers().
346 * Classifiers can store their result in the two fields
347 * bio_classifier1 and bio_classifier2.
348 * A classifier that updates one of the fields should
349 * return a non-zero value.
350 * If no classifier updates the field, g_run_classifiers() sets
351 * bio_classifier1 = BIO_NOTCLASSIFIED to avoid further calls.
352 */
353
354int
355g_register_classifier(struct g_classifier_hook *hook)
356{
357
358	g_bioq_lock(&g_bio_run_down);
359	TAILQ_INSERT_TAIL(&g_classifier_tailq, hook, link);
360	g_bioq_unlock(&g_bio_run_down);
361
362	return (0);
363}
364
365void
366g_unregister_classifier(struct g_classifier_hook *hook)
367{
368	struct g_classifier_hook *entry;
369
370	g_bioq_lock(&g_bio_run_down);
371	TAILQ_FOREACH(entry, &g_classifier_tailq, link) {
372		if (entry == hook) {
373			TAILQ_REMOVE(&g_classifier_tailq, hook, link);
374			break;
375		}
376	}
377	g_bioq_unlock(&g_bio_run_down);
378}
379
380static void
381g_run_classifiers(struct bio *bp)
382{
383	struct g_classifier_hook *hook;
384	int classified = 0;
385
386	TAILQ_FOREACH(hook, &g_classifier_tailq, link)
387		classified |= hook->func(hook->arg, bp);
388
389	if (!classified)
390		bp->bio_classifier1 = BIO_NOTCLASSIFIED;
391}
392
393void
394g_io_request(struct bio *bp, struct g_consumer *cp)
395{
396	struct g_provider *pp;
397	int first;
398
399	KASSERT(cp != NULL, ("NULL cp in g_io_request"));
400	KASSERT(bp != NULL, ("NULL bp in g_io_request"));
401	pp = cp->provider;
402	KASSERT(pp != NULL, ("consumer not attached in g_io_request"));
403#ifdef DIAGNOSTIC
404	KASSERT(bp->bio_driver1 == NULL,
405	    ("bio_driver1 used by the consumer (geom %s)", cp->geom->name));
406	KASSERT(bp->bio_driver2 == NULL,
407	    ("bio_driver2 used by the consumer (geom %s)", cp->geom->name));
408	KASSERT(bp->bio_pflags == 0,
409	    ("bio_pflags used by the consumer (geom %s)", cp->geom->name));
410	/*
411	 * Remember consumer's private fields, so we can detect if they were
412	 * modified by the provider.
413	 */
414	bp->_bio_caller1 = bp->bio_caller1;
415	bp->_bio_caller2 = bp->bio_caller2;
416	bp->_bio_cflags = bp->bio_cflags;
417#endif
418
419	if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_GETATTR)) {
420		KASSERT(bp->bio_data != NULL,
421		    ("NULL bp->data in g_io_request(cmd=%hhu)", bp->bio_cmd));
422	}
423	if (bp->bio_cmd & (BIO_DELETE|BIO_FLUSH)) {
424		KASSERT(bp->bio_data == NULL,
425		    ("non-NULL bp->data in g_io_request(cmd=%hhu)",
426		    bp->bio_cmd));
427	}
428	if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) {
429		KASSERT(bp->bio_offset % cp->provider->sectorsize == 0,
430		    ("wrong offset %jd for sectorsize %u",
431		    bp->bio_offset, cp->provider->sectorsize));
432		KASSERT(bp->bio_length % cp->provider->sectorsize == 0,
433		    ("wrong length %jd for sectorsize %u",
434		    bp->bio_length, cp->provider->sectorsize));
435	}
436
437	g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
438	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd);
439
440	bp->bio_from = cp;
441	bp->bio_to = pp;
442	bp->bio_error = 0;
443	bp->bio_completed = 0;
444
445	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
446	    ("Bio already on queue bp=%p", bp));
447	bp->bio_flags |= BIO_ONQUEUE;
448
449	if (g_collectstats)
450		binuptime(&bp->bio_t0);
451	else
452		getbinuptime(&bp->bio_t0);
453
454	/*
455	 * The statistics collection is lockless, as such, but we
456	 * can not update one instance of the statistics from more
457	 * than one thread at a time, so grab the lock first.
458	 *
459	 * We also use the lock to protect the list of classifiers.
460	 */
461	g_bioq_lock(&g_bio_run_down);
462
463	if (!TAILQ_EMPTY(&g_classifier_tailq) && !bp->bio_classifier1)
464		g_run_classifiers(bp);
465
466	if (g_collectstats & 1)
467		devstat_start_transaction(pp->stat, &bp->bio_t0);
468	if (g_collectstats & 2)
469		devstat_start_transaction(cp->stat, &bp->bio_t0);
470
471	pp->nstart++;
472	cp->nstart++;
473	first = TAILQ_EMPTY(&g_bio_run_down.bio_queue);
474	TAILQ_INSERT_TAIL(&g_bio_run_down.bio_queue, bp, bio_queue);
475	g_bio_run_down.bio_queue_length++;
476	g_bioq_unlock(&g_bio_run_down);
477
478	/* Pass it on down. */
479	if (first)
480		wakeup(&g_wait_down);
481}
482
483void
484g_io_deliver(struct bio *bp, int error)
485{
486	struct g_consumer *cp;
487	struct g_provider *pp;
488	int first;
489
490	KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
491	pp = bp->bio_to;
492	KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver"));
493	cp = bp->bio_from;
494	if (cp == NULL) {
495		bp->bio_error = error;
496		bp->bio_done(bp);
497		return;
498	}
499	KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver"));
500	KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
501#ifdef DIAGNOSTIC
502	/*
503	 * Some classes - GJournal in particular - can modify bio's
504	 * private fields while the bio is in transit; G_GEOM_VOLATILE_BIO
505	 * flag means it's an expected behaviour for that particular geom.
506	 */
507	if ((cp->geom->flags & G_GEOM_VOLATILE_BIO) == 0) {
508		KASSERT(bp->bio_caller1 == bp->_bio_caller1,
509		    ("bio_caller1 used by the provider %s", pp->name));
510		KASSERT(bp->bio_caller2 == bp->_bio_caller2,
511		    ("bio_caller2 used by the provider %s", pp->name));
512		KASSERT(bp->bio_cflags == bp->_bio_cflags,
513		    ("bio_cflags used by the provider %s", pp->name));
514	}
515#endif
516	KASSERT(bp->bio_completed >= 0, ("bio_completed can't be less than 0"));
517	KASSERT(bp->bio_completed <= bp->bio_length,
518	    ("bio_completed can't be greater than bio_length"));
519
520	g_trace(G_T_BIO,
521"g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
522	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error,
523	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
524
525	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
526	    ("Bio already on queue bp=%p", bp));
527
528	/*
529	 * XXX: next two doesn't belong here
530	 */
531	bp->bio_bcount = bp->bio_length;
532	bp->bio_resid = bp->bio_bcount - bp->bio_completed;
533
534	/*
535	 * The statistics collection is lockless, as such, but we
536	 * can not update one instance of the statistics from more
537	 * than one thread at a time, so grab the lock first.
538	 */
539	g_bioq_lock(&g_bio_run_up);
540	if (g_collectstats & 1)
541		devstat_end_transaction_bio(pp->stat, bp);
542	if (g_collectstats & 2)
543		devstat_end_transaction_bio(cp->stat, bp);
544
545	cp->nend++;
546	pp->nend++;
547	if (error != ENOMEM) {
548		bp->bio_error = error;
549		first = TAILQ_EMPTY(&g_bio_run_up.bio_queue);
550		TAILQ_INSERT_TAIL(&g_bio_run_up.bio_queue, bp, bio_queue);
551		bp->bio_flags |= BIO_ONQUEUE;
552		g_bio_run_up.bio_queue_length++;
553		g_bioq_unlock(&g_bio_run_up);
554		if (first)
555			wakeup(&g_wait_up);
556		return;
557	}
558	g_bioq_unlock(&g_bio_run_up);
559
560	if (bootverbose)
561		printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name);
562	bp->bio_children = 0;
563	bp->bio_inbed = 0;
564	g_io_request(bp, cp);
565	pace++;
566	return;
567}
568
569void
570g_io_schedule_down(struct thread *tp __unused)
571{
572	struct bio *bp;
573	off_t excess;
574	int error;
575
576	for(;;) {
577		g_bioq_lock(&g_bio_run_down);
578		bp = g_bioq_first(&g_bio_run_down);
579		if (bp == NULL) {
580			CTR0(KTR_GEOM, "g_down going to sleep");
581			msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock,
582			    PRIBIO | PDROP, "-", 0);
583			continue;
584		}
585		CTR0(KTR_GEOM, "g_down has work to do");
586		g_bioq_unlock(&g_bio_run_down);
587		if (pace > 0) {
588			CTR1(KTR_GEOM, "g_down pacing self (pace %d)", pace);
589			pause("g_down", hz/10);
590			pace--;
591		}
592		error = g_io_check(bp);
593		if (error) {
594			CTR3(KTR_GEOM, "g_down g_io_check on bp %p provider "
595			    "%s returned %d", bp, bp->bio_to->name, error);
596			g_io_deliver(bp, error);
597			continue;
598		}
599		CTR2(KTR_GEOM, "g_down processing bp %p provider %s", bp,
600		    bp->bio_to->name);
601		switch (bp->bio_cmd) {
602		case BIO_READ:
603		case BIO_WRITE:
604		case BIO_DELETE:
605			/* Truncate requests to the end of providers media. */
606			/*
607			 * XXX: What if we truncate because of offset being
608			 * bad, not length?
609			 */
610			excess = bp->bio_offset + bp->bio_length;
611			if (excess > bp->bio_to->mediasize) {
612				excess -= bp->bio_to->mediasize;
613				bp->bio_length -= excess;
614				if (excess > 0)
615					CTR3(KTR_GEOM, "g_down truncated bio "
616					    "%p provider %s by %d", bp,
617					    bp->bio_to->name, excess);
618			}
619			/* Deliver zero length transfers right here. */
620			if (bp->bio_length == 0) {
621				g_io_deliver(bp, 0);
622				CTR2(KTR_GEOM, "g_down terminated 0-length "
623				    "bp %p provider %s", bp, bp->bio_to->name);
624				continue;
625			}
626			break;
627		default:
628			break;
629		}
630		THREAD_NO_SLEEPING();
631		CTR4(KTR_GEOM, "g_down starting bp %p provider %s off %ld "
632		    "len %ld", bp, bp->bio_to->name, bp->bio_offset,
633		    bp->bio_length);
634		bp->bio_to->geom->start(bp);
635		THREAD_SLEEPING_OK();
636	}
637}
638
639void
640bio_taskqueue(struct bio *bp, bio_task_t *func, void *arg)
641{
642	bp->bio_task = func;
643	bp->bio_task_arg = arg;
644	/*
645	 * The taskqueue is actually just a second queue off the "up"
646	 * queue, so we use the same lock.
647	 */
648	g_bioq_lock(&g_bio_run_up);
649	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
650	    ("Bio already on queue bp=%p target taskq", bp));
651	bp->bio_flags |= BIO_ONQUEUE;
652	TAILQ_INSERT_TAIL(&g_bio_run_task.bio_queue, bp, bio_queue);
653	g_bio_run_task.bio_queue_length++;
654	wakeup(&g_wait_up);
655	g_bioq_unlock(&g_bio_run_up);
656}
657
658
659void
660g_io_schedule_up(struct thread *tp __unused)
661{
662	struct bio *bp;
663	for(;;) {
664		g_bioq_lock(&g_bio_run_up);
665		bp = g_bioq_first(&g_bio_run_task);
666		if (bp != NULL) {
667			g_bioq_unlock(&g_bio_run_up);
668			THREAD_NO_SLEEPING();
669			CTR1(KTR_GEOM, "g_up processing task bp %p", bp);
670			bp->bio_task(bp->bio_task_arg);
671			THREAD_SLEEPING_OK();
672			continue;
673		}
674		bp = g_bioq_first(&g_bio_run_up);
675		if (bp != NULL) {
676			g_bioq_unlock(&g_bio_run_up);
677			THREAD_NO_SLEEPING();
678			CTR4(KTR_GEOM, "g_up biodone bp %p provider %s off "
679			    "%jd len %ld", bp, bp->bio_to->name,
680			    bp->bio_offset, bp->bio_length);
681			biodone(bp);
682			THREAD_SLEEPING_OK();
683			continue;
684		}
685		CTR0(KTR_GEOM, "g_up going to sleep");
686		msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock,
687		    PRIBIO | PDROP, "-", 0);
688	}
689}
690
691void *
692g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
693{
694	struct bio *bp;
695	void *ptr;
696	int errorc;
697
698	KASSERT(length > 0 && length >= cp->provider->sectorsize &&
699	    length <= MAXPHYS, ("g_read_data(): invalid length %jd",
700	    (intmax_t)length));
701
702	bp = g_alloc_bio();
703	bp->bio_cmd = BIO_READ;
704	bp->bio_done = NULL;
705	bp->bio_offset = offset;
706	bp->bio_length = length;
707	ptr = g_malloc(length, M_WAITOK);
708	bp->bio_data = ptr;
709	g_io_request(bp, cp);
710	errorc = biowait(bp, "gread");
711	if (error != NULL)
712		*error = errorc;
713	g_destroy_bio(bp);
714	if (errorc) {
715		g_free(ptr);
716		ptr = NULL;
717	}
718	return (ptr);
719}
720
721int
722g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
723{
724	struct bio *bp;
725	int error;
726
727	KASSERT(length > 0 && length >= cp->provider->sectorsize &&
728	    length <= MAXPHYS, ("g_write_data(): invalid length %jd",
729	    (intmax_t)length));
730
731	bp = g_alloc_bio();
732	bp->bio_cmd = BIO_WRITE;
733	bp->bio_done = NULL;
734	bp->bio_offset = offset;
735	bp->bio_length = length;
736	bp->bio_data = ptr;
737	g_io_request(bp, cp);
738	error = biowait(bp, "gwrite");
739	g_destroy_bio(bp);
740	return (error);
741}
742
743int
744g_delete_data(struct g_consumer *cp, off_t offset, off_t length)
745{
746	struct bio *bp;
747	int error;
748
749	KASSERT(length > 0 && length >= cp->provider->sectorsize,
750	    ("g_delete_data(): invalid length %jd", (intmax_t)length));
751
752	bp = g_alloc_bio();
753	bp->bio_cmd = BIO_DELETE;
754	bp->bio_done = NULL;
755	bp->bio_offset = offset;
756	bp->bio_length = length;
757	bp->bio_data = NULL;
758	g_io_request(bp, cp);
759	error = biowait(bp, "gdelete");
760	g_destroy_bio(bp);
761	return (error);
762}
763
764void
765g_print_bio(struct bio *bp)
766{
767	const char *pname, *cmd = NULL;
768
769	if (bp->bio_to != NULL)
770		pname = bp->bio_to->name;
771	else
772		pname = "[unknown]";
773
774	switch (bp->bio_cmd) {
775	case BIO_GETATTR:
776		cmd = "GETATTR";
777		printf("%s[%s(attr=%s)]", pname, cmd, bp->bio_attribute);
778		return;
779	case BIO_FLUSH:
780		cmd = "FLUSH";
781		printf("%s[%s]", pname, cmd);
782		return;
783	case BIO_READ:
784		cmd = "READ";
785		break;
786	case BIO_WRITE:
787		cmd = "WRITE";
788		break;
789	case BIO_DELETE:
790		cmd = "DELETE";
791		break;
792	default:
793		cmd = "UNKNOWN";
794		printf("%s[%s()]", pname, cmd);
795		return;
796	}
797	printf("%s[%s(offset=%jd, length=%jd)]", pname, cmd,
798	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
799}
800