geom_io.c revision 159304
1218885Sdim/*-
2218885Sdim * Copyright (c) 2002 Poul-Henning Kamp
3218885Sdim * Copyright (c) 2002 Networks Associates Technology, Inc.
4218885Sdim * All rights reserved.
5218885Sdim *
6218885Sdim * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7218885Sdim * and NAI Labs, the Security Research Division of Network Associates, Inc.
8218885Sdim * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9218885Sdim * DARPA CHATS research program.
10218885Sdim *
11218885Sdim * Redistribution and use in source and binary forms, with or without
12218885Sdim * modification, are permitted provided that the following conditions
13218885Sdim * are met:
14218885Sdim * 1. Redistributions of source code must retain the above copyright
15218885Sdim *    notice, this list of conditions and the following disclaimer.
16218885Sdim * 2. Redistributions in binary form must reproduce the above copyright
17218885Sdim *    notice, this list of conditions and the following disclaimer in the
18218885Sdim *    documentation and/or other materials provided with the distribution.
19218885Sdim * 3. The names of the authors may not be used to endorse or promote
20218885Sdim *    products derived from this software without specific prior written
21218885Sdim *    permission.
22218885Sdim *
23218885Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24218885Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25218885Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26218885Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27218885Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28218885Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29218885Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30224145Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31218885Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32218885Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33218885Sdim * SUCH DAMAGE.
34218885Sdim */
35226633Sdim
36218885Sdim#include <sys/cdefs.h>
37218885Sdim__FBSDID("$FreeBSD: head/sys/geom/geom_io.c 159304 2006-06-05 21:13:22Z pjd $");
38218885Sdim
39218885Sdim#include <sys/param.h>
40218885Sdim#include <sys/systm.h>
41218885Sdim#include <sys/kernel.h>
42218885Sdim#include <sys/malloc.h>
43218885Sdim#include <sys/bio.h>
44218885Sdim#include <sys/ktr.h>
45218885Sdim#include <sys/proc.h>
46218885Sdim#include <sys/stack.h>
47218885Sdim
48218885Sdim#include <sys/errno.h>
49218885Sdim#include <geom/geom.h>
50218885Sdim#include <geom/geom_int.h>
51218885Sdim#include <sys/devicestat.h>
52218885Sdim
53218885Sdim#include <vm/uma.h>
54218885Sdim
55218885Sdimstatic struct g_bioq g_bio_run_down;
56218885Sdimstatic struct g_bioq g_bio_run_up;
57218885Sdimstatic struct g_bioq g_bio_run_task;
58218885Sdim
59218885Sdimstatic u_int pace;
60218885Sdimstatic uma_zone_t	biozone;
61218885Sdim
62218885Sdim#include <machine/atomic.h>
63218885Sdim
64218885Sdimstatic void
65218885Sdimg_bioq_lock(struct g_bioq *bq)
66218885Sdim{
67218885Sdim
68234353Sdim	mtx_lock(&bq->bio_queue_lock);
69218885Sdim}
70218885Sdim
71218885Sdimstatic void
72218885Sdimg_bioq_unlock(struct g_bioq *bq)
73218885Sdim{
74218885Sdim
75218885Sdim	mtx_unlock(&bq->bio_queue_lock);
76218885Sdim}
77218885Sdim
78218885Sdim#if 0
79218885Sdimstatic void
80218885Sdimg_bioq_destroy(struct g_bioq *bq)
81218885Sdim{
82218885Sdim
83218885Sdim	mtx_destroy(&bq->bio_queue_lock);
84218885Sdim}
85218885Sdim#endif
86218885Sdim
87218885Sdimstatic void
88218885Sdimg_bioq_init(struct g_bioq *bq)
89218885Sdim{
90218885Sdim
91218885Sdim	TAILQ_INIT(&bq->bio_queue);
92218885Sdim	mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF);
93218885Sdim}
94218885Sdim
95218885Sdimstatic struct bio *
96218885Sdimg_bioq_first(struct g_bioq *bq)
97218885Sdim{
98218885Sdim	struct bio *bp;
99218885Sdim
100218885Sdim	bp = TAILQ_FIRST(&bq->bio_queue);
101218885Sdim	if (bp != NULL) {
102218885Sdim		KASSERT((bp->bio_flags & BIO_ONQUEUE),
103218885Sdim		    ("Bio not on queue bp=%p target %p", bp, bq));
104218885Sdim		bp->bio_flags &= ~BIO_ONQUEUE;
105218885Sdim		TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
106218885Sdim		bq->bio_queue_length--;
107218885Sdim	}
108218885Sdim	return (bp);
109218885Sdim}
110218885Sdim
111218885Sdimstruct bio *
112218885Sdimg_new_bio(void)
113218885Sdim{
114218885Sdim	struct bio *bp;
115218885Sdim
116218885Sdim	bp = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
117218885Sdim#ifdef KTR
118218885Sdim	if (KTR_COMPILE & KTR_GEOM) {
119218885Sdim		struct stack st;
120218885Sdim
121218885Sdim		CTR1(KTR_GEOM, "g_new_bio(): %p", bp);
122218885Sdim		stack_save(&st);
123218885Sdim		CTRSTACK(KTR_GEOM, &st, 3, 0);
124218885Sdim	}
125218885Sdim#endif
126218885Sdim	return (bp);
127218885Sdim}
128218885Sdim
129218885Sdimstruct bio *
130218885Sdimg_alloc_bio(void)
131218885Sdim{
132218885Sdim	struct bio *bp;
133218885Sdim
134218885Sdim	bp = uma_zalloc(biozone, M_WAITOK | M_ZERO);
135218885Sdim#ifdef KTR
136218885Sdim	if (KTR_COMPILE & KTR_GEOM) {
137218885Sdim		struct stack st;
138218885Sdim
139218885Sdim		CTR1(KTR_GEOM, "g_alloc_bio(): %p", bp);
140218885Sdim		stack_save(&st);
141218885Sdim		CTRSTACK(KTR_GEOM, &st, 3, 0);
142218885Sdim	}
143218885Sdim#endif
144218885Sdim	return (bp);
145218885Sdim}
146218885Sdim
147218885Sdimvoid
148218885Sdimg_destroy_bio(struct bio *bp)
149218885Sdim{
150218885Sdim#ifdef KTR
151218885Sdim	if (KTR_COMPILE & KTR_GEOM) {
152218885Sdim		struct stack st;
153218885Sdim
154218885Sdim		CTR1(KTR_GEOM, "g_destroy_bio(): %p", bp);
155218885Sdim		stack_save(&st);
156218885Sdim		CTRSTACK(KTR_GEOM, &st, 3, 0);
157239462Sdim	}
158218885Sdim#endif
159218885Sdim	uma_zfree(biozone, bp);
160218885Sdim}
161218885Sdim
162218885Sdimstruct bio *
163218885Sdimg_clone_bio(struct bio *bp)
164218885Sdim{
165218885Sdim	struct bio *bp2;
166218885Sdim
167218885Sdim	bp2 = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
168	if (bp2 != NULL) {
169		bp2->bio_parent = bp;
170		bp2->bio_cmd = bp->bio_cmd;
171		bp2->bio_length = bp->bio_length;
172		bp2->bio_offset = bp->bio_offset;
173		bp2->bio_data = bp->bio_data;
174		bp2->bio_attribute = bp->bio_attribute;
175		bp->bio_children++;
176	}
177#ifdef KTR
178	if (KTR_COMPILE & KTR_GEOM) {
179		struct stack st;
180
181		CTR2(KTR_GEOM, "g_clone_bio(%p): %p", bp, bp2);
182		stack_save(&st);
183		CTRSTACK(KTR_GEOM, &st, 3, 0);
184	}
185#endif
186	return(bp2);
187}
188
189struct bio *
190g_duplicate_bio(struct bio *bp)
191{
192	struct bio *bp2;
193
194	bp2 = uma_zalloc(biozone, M_WAITOK | M_ZERO);
195	bp2->bio_parent = bp;
196	bp2->bio_cmd = bp->bio_cmd;
197	bp2->bio_length = bp->bio_length;
198	bp2->bio_offset = bp->bio_offset;
199	bp2->bio_data = bp->bio_data;
200	bp2->bio_attribute = bp->bio_attribute;
201	bp->bio_children++;
202#ifdef KTR
203	if (KTR_COMPILE & KTR_GEOM) {
204		struct stack st;
205
206		CTR2(KTR_GEOM, "g_duplicate_bio(%p): %p", bp, bp2);
207		stack_save(&st);
208		CTRSTACK(KTR_GEOM, &st, 3, 0);
209	}
210#endif
211	return(bp2);
212}
213
214void
215g_io_init()
216{
217
218	g_bioq_init(&g_bio_run_down);
219	g_bioq_init(&g_bio_run_up);
220	g_bioq_init(&g_bio_run_task);
221	biozone = uma_zcreate("g_bio", sizeof (struct bio),
222	    NULL, NULL,
223	    NULL, NULL,
224	    0, 0);
225}
226
227int
228g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr)
229{
230	struct bio *bp;
231	int error;
232
233	g_trace(G_T_BIO, "bio_getattr(%s)", attr);
234	bp = g_alloc_bio();
235	bp->bio_cmd = BIO_GETATTR;
236	bp->bio_done = NULL;
237	bp->bio_attribute = attr;
238	bp->bio_length = *len;
239	bp->bio_data = ptr;
240	g_io_request(bp, cp);
241	error = biowait(bp, "ggetattr");
242	*len = bp->bio_completed;
243	g_destroy_bio(bp);
244	return (error);
245}
246
247static int
248g_io_check(struct bio *bp)
249{
250	struct g_consumer *cp;
251	struct g_provider *pp;
252
253	cp = bp->bio_from;
254	pp = bp->bio_to;
255
256	/* Fail if access counters dont allow the operation */
257	switch(bp->bio_cmd) {
258	case BIO_READ:
259	case BIO_GETATTR:
260		if (cp->acr == 0)
261			return (EPERM);
262		break;
263	case BIO_WRITE:
264	case BIO_DELETE:
265		if (cp->acw == 0)
266			return (EPERM);
267		break;
268	default:
269		return (EPERM);
270	}
271	/* if provider is marked for error, don't disturb. */
272	if (pp->error)
273		return (pp->error);
274
275	switch(bp->bio_cmd) {
276	case BIO_READ:
277	case BIO_WRITE:
278	case BIO_DELETE:
279		/* Zero sectorsize is a probably lack of media */
280		if (pp->sectorsize == 0)
281			return (ENXIO);
282		/* Reject I/O not on sector boundary */
283		if (bp->bio_offset % pp->sectorsize)
284			return (EINVAL);
285		/* Reject I/O not integral sector long */
286		if (bp->bio_length % pp->sectorsize)
287			return (EINVAL);
288		/* Reject requests before or past the end of media. */
289		if (bp->bio_offset < 0)
290			return (EIO);
291		if (bp->bio_offset > pp->mediasize)
292			return (EIO);
293		break;
294	default:
295		break;
296	}
297	return (0);
298}
299
300void
301g_io_request(struct bio *bp, struct g_consumer *cp)
302{
303	struct g_provider *pp;
304
305	KASSERT(cp != NULL, ("NULL cp in g_io_request"));
306	KASSERT(bp != NULL, ("NULL bp in g_io_request"));
307	KASSERT(bp->bio_data != NULL, ("NULL bp->data in g_io_request"));
308	pp = cp->provider;
309	KASSERT(pp != NULL, ("consumer not attached in g_io_request"));
310#ifdef DIAGNOSTIC
311	KASSERT(bp->bio_driver1 == NULL,
312	    ("bio_driver1 used by the consumer (geom %s)", cp->geom->name));
313	KASSERT(bp->bio_driver2 == NULL,
314	    ("bio_driver2 used by the consumer (geom %s)", cp->geom->name));
315	KASSERT(bp->bio_pflags == 0,
316	    ("bio_pflags used by the consumer (geom %s)", cp->geom->name));
317	/*
318	 * Remember consumer's private fields, so we can detect if they were
319	 * modified by the provider.
320	 */
321	bp->_bio_caller1 = bp->bio_caller1;
322	bp->_bio_caller2 = bp->bio_caller2;
323	bp->_bio_cflags = bp->bio_cflags;
324#endif
325
326	if (bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) {
327		KASSERT(bp->bio_offset % cp->provider->sectorsize == 0,
328		    ("wrong offset %jd for sectorsize %u",
329		    bp->bio_offset, cp->provider->sectorsize));
330		KASSERT(bp->bio_length % cp->provider->sectorsize == 0,
331		    ("wrong length %jd for sectorsize %u",
332		    bp->bio_length, cp->provider->sectorsize));
333	}
334
335	g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
336	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd);
337
338	bp->bio_from = cp;
339	bp->bio_to = pp;
340	bp->bio_error = 0;
341	bp->bio_completed = 0;
342
343	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
344	    ("Bio already on queue bp=%p", bp));
345	bp->bio_flags |= BIO_ONQUEUE;
346
347	binuptime(&bp->bio_t0);
348
349	/*
350	 * The statistics collection is lockless, as such, but we
351	 * can not update one instance of the statistics from more
352	 * than one thread at a time, so grab the lock first.
353	 */
354	g_bioq_lock(&g_bio_run_down);
355	if (g_collectstats & 1)
356		devstat_start_transaction(pp->stat, &bp->bio_t0);
357	if (g_collectstats & 2)
358		devstat_start_transaction(cp->stat, &bp->bio_t0);
359
360	pp->nstart++;
361	cp->nstart++;
362	TAILQ_INSERT_TAIL(&g_bio_run_down.bio_queue, bp, bio_queue);
363	g_bio_run_down.bio_queue_length++;
364	g_bioq_unlock(&g_bio_run_down);
365
366	/* Pass it on down. */
367	wakeup(&g_wait_down);
368}
369
370void
371g_io_deliver(struct bio *bp, int error)
372{
373	struct g_consumer *cp;
374	struct g_provider *pp;
375
376	KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
377	pp = bp->bio_to;
378	KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver"));
379#ifdef DIAGNOSTIC
380	KASSERT(bp->bio_caller1 == bp->_bio_caller1,
381	    ("bio_caller1 used by the provider %s", pp->name));
382	KASSERT(bp->bio_caller2 == bp->_bio_caller2,
383	    ("bio_caller2 used by the provider %s", pp->name));
384	KASSERT(bp->bio_cflags == bp->_bio_cflags,
385	    ("bio_cflags used by the provider %s", pp->name));
386#endif
387	cp = bp->bio_from;
388	if (cp == NULL) {
389		bp->bio_error = error;
390		bp->bio_done(bp);
391		return;
392	}
393	KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver"));
394	KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
395	KASSERT(bp->bio_completed >= 0, ("bio_completed can't be less than 0"));
396	KASSERT(bp->bio_completed <= bp->bio_length,
397	    ("bio_completed can't be greater than bio_length"));
398
399	g_trace(G_T_BIO,
400"g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
401	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error,
402	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
403
404	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
405	    ("Bio already on queue bp=%p", bp));
406
407	/*
408	 * XXX: next two doesn't belong here
409	 */
410	bp->bio_bcount = bp->bio_length;
411	bp->bio_resid = bp->bio_bcount - bp->bio_completed;
412
413	/*
414	 * The statistics collection is lockless, as such, but we
415	 * can not update one instance of the statistics from more
416	 * than one thread at a time, so grab the lock first.
417	 */
418	g_bioq_lock(&g_bio_run_up);
419	if (g_collectstats & 1)
420		devstat_end_transaction_bio(pp->stat, bp);
421	if (g_collectstats & 2)
422		devstat_end_transaction_bio(cp->stat, bp);
423
424	cp->nend++;
425	pp->nend++;
426	if (error != ENOMEM) {
427		bp->bio_error = error;
428		TAILQ_INSERT_TAIL(&g_bio_run_up.bio_queue, bp, bio_queue);
429		bp->bio_flags |= BIO_ONQUEUE;
430		g_bio_run_up.bio_queue_length++;
431		g_bioq_unlock(&g_bio_run_up);
432		wakeup(&g_wait_up);
433		return;
434	}
435	g_bioq_unlock(&g_bio_run_up);
436
437	if (bootverbose)
438		printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name);
439	bp->bio_children = 0;
440	bp->bio_inbed = 0;
441	g_io_request(bp, cp);
442	pace++;
443	return;
444}
445
446void
447g_io_schedule_down(struct thread *tp __unused)
448{
449	struct bio *bp;
450	off_t excess;
451	int error;
452
453	for(;;) {
454		g_bioq_lock(&g_bio_run_down);
455		bp = g_bioq_first(&g_bio_run_down);
456		if (bp == NULL) {
457			CTR0(KTR_GEOM, "g_down going to sleep");
458			msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock,
459			    PRIBIO | PDROP, "-", hz/10);
460			continue;
461		}
462		CTR0(KTR_GEOM, "g_down has work to do");
463		g_bioq_unlock(&g_bio_run_down);
464		if (pace > 0) {
465			CTR1(KTR_GEOM, "g_down pacing self (pace %d)", pace);
466			msleep(&error, NULL, PRIBIO, "g_down", hz/10);
467			pace--;
468		}
469		error = g_io_check(bp);
470		if (error) {
471			CTR3(KTR_GEOM, "g_down g_io_check on bp %p provider "
472			    "%s returned %d", bp, bp->bio_to->name, error);
473			g_io_deliver(bp, error);
474			continue;
475		}
476		CTR2(KTR_GEOM, "g_down processing bp %p provider %s", bp,
477		    bp->bio_to->name);
478		switch (bp->bio_cmd) {
479		case BIO_READ:
480		case BIO_WRITE:
481		case BIO_DELETE:
482			/* Truncate requests to the end of providers media. */
483			/*
484			 * XXX: What if we truncate because of offset being
485			 * bad, not length?
486			 */
487			excess = bp->bio_offset + bp->bio_length;
488			if (excess > bp->bio_to->mediasize) {
489				excess -= bp->bio_to->mediasize;
490				bp->bio_length -= excess;
491				if (excess > 0)
492					CTR3(KTR_GEOM, "g_down truncated bio "
493					    "%p provider %s by %d", bp,
494					    bp->bio_to->name, excess);
495			}
496			/* Deliver zero length transfers right here. */
497			if (bp->bio_length == 0) {
498				g_io_deliver(bp, 0);
499				CTR2(KTR_GEOM, "g_down terminated 0-length "
500				    "bp %p provider %s", bp, bp->bio_to->name);
501				continue;
502			}
503			break;
504		default:
505			break;
506		}
507		THREAD_NO_SLEEPING();
508		CTR4(KTR_GEOM, "g_down starting bp %p provider %s off %ld "
509		    "len %ld", bp, bp->bio_to->name, bp->bio_offset,
510		    bp->bio_length);
511		bp->bio_to->geom->start(bp);
512		THREAD_SLEEPING_OK();
513	}
514}
515
516void
517bio_taskqueue(struct bio *bp, bio_task_t *func, void *arg)
518{
519	bp->bio_task = func;
520	bp->bio_task_arg = arg;
521	/*
522	 * The taskqueue is actually just a second queue off the "up"
523	 * queue, so we use the same lock.
524	 */
525	g_bioq_lock(&g_bio_run_up);
526	KASSERT(!(bp->bio_flags & BIO_ONQUEUE),
527	    ("Bio already on queue bp=%p target taskq", bp));
528	bp->bio_flags |= BIO_ONQUEUE;
529	TAILQ_INSERT_TAIL(&g_bio_run_task.bio_queue, bp, bio_queue);
530	g_bio_run_task.bio_queue_length++;
531	wakeup(&g_wait_up);
532	g_bioq_unlock(&g_bio_run_up);
533}
534
535
536void
537g_io_schedule_up(struct thread *tp __unused)
538{
539	struct bio *bp;
540	for(;;) {
541		g_bioq_lock(&g_bio_run_up);
542		bp = g_bioq_first(&g_bio_run_task);
543		if (bp != NULL) {
544			g_bioq_unlock(&g_bio_run_up);
545			THREAD_NO_SLEEPING();
546			CTR1(KTR_GEOM, "g_up processing task bp %p", bp);
547			bp->bio_task(bp->bio_task_arg);
548			THREAD_SLEEPING_OK();
549			continue;
550		}
551		bp = g_bioq_first(&g_bio_run_up);
552		if (bp != NULL) {
553			g_bioq_unlock(&g_bio_run_up);
554			THREAD_NO_SLEEPING();
555			CTR4(KTR_GEOM, "g_up biodone bp %p provider %s off "
556			    "%ld len %ld", bp, bp->bio_to->name,
557			    bp->bio_offset, bp->bio_length);
558			biodone(bp);
559			THREAD_SLEEPING_OK();
560			continue;
561		}
562		CTR0(KTR_GEOM, "g_up going to sleep");
563		msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock,
564		    PRIBIO | PDROP, "-", hz/10);
565	}
566}
567
568void *
569g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
570{
571	struct bio *bp;
572	void *ptr;
573	int errorc;
574
575	KASSERT(length > 0 && length >= cp->provider->sectorsize &&
576	    length <= MAXPHYS, ("g_read_data(): invalid length %jd",
577	    (intmax_t)length));
578
579	bp = g_alloc_bio();
580	bp->bio_cmd = BIO_READ;
581	bp->bio_done = NULL;
582	bp->bio_offset = offset;
583	bp->bio_length = length;
584	ptr = g_malloc(length, M_WAITOK);
585	bp->bio_data = ptr;
586	g_io_request(bp, cp);
587	errorc = biowait(bp, "gread");
588	if (error != NULL)
589		*error = errorc;
590	g_destroy_bio(bp);
591	if (errorc) {
592		g_free(ptr);
593		ptr = NULL;
594	}
595	return (ptr);
596}
597
598int
599g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
600{
601	struct bio *bp;
602	int error;
603
604	KASSERT(length > 0 && length >= cp->provider->sectorsize &&
605	    length <= MAXPHYS, ("g_write_data(): invalid length %jd",
606	    (intmax_t)length));
607
608	bp = g_alloc_bio();
609	bp->bio_cmd = BIO_WRITE;
610	bp->bio_done = NULL;
611	bp->bio_offset = offset;
612	bp->bio_length = length;
613	bp->bio_data = ptr;
614	g_io_request(bp, cp);
615	error = biowait(bp, "gwrite");
616	g_destroy_bio(bp);
617	return (error);
618}
619
620void
621g_print_bio(struct bio *bp)
622{
623	const char *pname, *cmd = NULL;
624
625	if (bp->bio_to != NULL)
626		pname = bp->bio_to->name;
627	else
628		pname = "[unknown]";
629
630	switch (bp->bio_cmd) {
631	case BIO_GETATTR:
632		cmd = "GETATTR";
633		printf("%s[%s(attr=%s)]", pname, cmd, bp->bio_attribute);
634		return;
635	case BIO_READ:
636		cmd = "READ";
637	case BIO_WRITE:
638		if (cmd == NULL)
639			cmd = "WRITE";
640	case BIO_DELETE:
641		if (cmd == NULL)
642			cmd = "DELETE";
643		printf("%s[%s(offset=%jd, length=%jd)]", pname, cmd,
644		    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
645		return;
646	default:
647		cmd = "UNKNOWN";
648		printf("%s[%s()]", pname, cmd);
649		return;
650	}
651	/* NOTREACHED */
652}
653