geom_io.c revision 159304
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 159304 2006-06-05 21:13:22Z pjd $");
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#include <machine/atomic.h>
63
64static void
65g_bioq_lock(struct g_bioq *bq)
66{
67
68	mtx_lock(&bq->bio_queue_lock);
69}
70
71static void
72g_bioq_unlock(struct g_bioq *bq)
73{
74
75	mtx_unlock(&bq->bio_queue_lock);
76}
77
78#if 0
79static void
80g_bioq_destroy(struct g_bioq *bq)
81{
82
83	mtx_destroy(&bq->bio_queue_lock);
84}
85#endif
86
87static void
88g_bioq_init(struct g_bioq *bq)
89{
90
91	TAILQ_INIT(&bq->bio_queue);
92	mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF);
93}
94
95static struct bio *
96g_bioq_first(struct g_bioq *bq)
97{
98	struct bio *bp;
99
100	bp = TAILQ_FIRST(&bq->bio_queue);
101	if (bp != NULL) {
102		KASSERT((bp->bio_flags & BIO_ONQUEUE),
103		    ("Bio not on queue bp=%p target %p", bp, bq));
104		bp->bio_flags &= ~BIO_ONQUEUE;
105		TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
106		bq->bio_queue_length--;
107	}
108	return (bp);
109}
110
111struct bio *
112g_new_bio(void)
113{
114	struct bio *bp;
115
116	bp = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
117#ifdef KTR
118	if (KTR_COMPILE & KTR_GEOM) {
119		struct stack st;
120
121		CTR1(KTR_GEOM, "g_new_bio(): %p", bp);
122		stack_save(&st);
123		CTRSTACK(KTR_GEOM, &st, 3, 0);
124	}
125#endif
126	return (bp);
127}
128
129struct bio *
130g_alloc_bio(void)
131{
132	struct bio *bp;
133
134	bp = uma_zalloc(biozone, M_WAITOK | M_ZERO);
135#ifdef KTR
136	if (KTR_COMPILE & KTR_GEOM) {
137		struct stack st;
138
139		CTR1(KTR_GEOM, "g_alloc_bio(): %p", bp);
140		stack_save(&st);
141		CTRSTACK(KTR_GEOM, &st, 3, 0);
142	}
143#endif
144	return (bp);
145}
146
147void
148g_destroy_bio(struct bio *bp)
149{
150#ifdef KTR
151	if (KTR_COMPILE & KTR_GEOM) {
152		struct stack st;
153
154		CTR1(KTR_GEOM, "g_destroy_bio(): %p", bp);
155		stack_save(&st);
156		CTRSTACK(KTR_GEOM, &st, 3, 0);
157	}
158#endif
159	uma_zfree(biozone, bp);
160}
161
162struct bio *
163g_clone_bio(struct bio *bp)
164{
165	struct bio *bp2;
166
167	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