geom_io.c revision 130280
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 130280 2004-06-09 19:44:44Z phk $");
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
45#include <sys/errno.h>
46#include <geom/geom.h>
47#include <geom/geom_int.h>
48#include <sys/devicestat.h>
49
50#include <vm/uma.h>
51
52static struct g_bioq g_bio_run_down;
53static struct g_bioq g_bio_run_up;
54static struct g_bioq g_bio_run_task;
55
56static u_int pace;
57static uma_zone_t	biozone;
58
59#include <machine/atomic.h>
60
61static void
62g_bioq_lock(struct g_bioq *bq)
63{
64
65	mtx_lock(&bq->bio_queue_lock);
66}
67
68static void
69g_bioq_unlock(struct g_bioq *bq)
70{
71
72	mtx_unlock(&bq->bio_queue_lock);
73}
74
75#if 0
76static void
77g_bioq_destroy(struct g_bioq *bq)
78{
79
80	mtx_destroy(&bq->bio_queue_lock);
81}
82#endif
83
84static void
85g_bioq_init(struct g_bioq *bq)
86{
87
88	TAILQ_INIT(&bq->bio_queue);
89	mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF);
90}
91
92static struct bio *
93g_bioq_first(struct g_bioq *bq)
94{
95	struct bio *bp;
96
97	bp = TAILQ_FIRST(&bq->bio_queue);
98	if (bp != NULL) {
99		TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
100		bq->bio_queue_length--;
101	}
102	return (bp);
103}
104
105static void
106g_bioq_enqueue_tail(struct bio *bp, struct g_bioq *rq)
107{
108
109	g_bioq_lock(rq);
110	TAILQ_INSERT_TAIL(&rq->bio_queue, bp, bio_queue);
111	rq->bio_queue_length++;
112	g_bioq_unlock(rq);
113}
114
115struct bio *
116g_new_bio(void)
117{
118	struct bio *bp;
119
120	bp = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
121	return (bp);
122}
123
124void
125g_destroy_bio(struct bio *bp)
126{
127
128	uma_zfree(biozone, bp);
129}
130
131struct bio *
132g_clone_bio(struct bio *bp)
133{
134	struct bio *bp2;
135
136	bp2 = uma_zalloc(biozone, M_NOWAIT | M_ZERO);
137	if (bp2 != NULL) {
138		bp2->bio_parent = bp;
139		bp2->bio_cmd = bp->bio_cmd;
140		bp2->bio_length = bp->bio_length;
141		bp2->bio_offset = bp->bio_offset;
142		bp2->bio_data = bp->bio_data;
143		bp2->bio_attribute = bp->bio_attribute;
144		bp->bio_children++;
145	}
146	return(bp2);
147}
148
149void
150g_io_init()
151{
152
153	g_bioq_init(&g_bio_run_down);
154	g_bioq_init(&g_bio_run_up);
155	g_bioq_init(&g_bio_run_task);
156	biozone = uma_zcreate("g_bio", sizeof (struct bio),
157	    NULL, NULL,
158	    NULL, NULL,
159	    0, 0);
160}
161
162int
163g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr)
164{
165	struct bio *bp;
166	int error;
167
168	g_trace(G_T_BIO, "bio_getattr(%s)", attr);
169	bp = g_new_bio();
170	bp->bio_cmd = BIO_GETATTR;
171	bp->bio_done = NULL;
172	bp->bio_attribute = attr;
173	bp->bio_length = *len;
174	bp->bio_data = ptr;
175	g_io_request(bp, cp);
176	error = biowait(bp, "ggetattr");
177	*len = bp->bio_completed;
178	g_destroy_bio(bp);
179	return (error);
180}
181
182static int
183g_io_check(struct bio *bp)
184{
185	struct g_consumer *cp;
186	struct g_provider *pp;
187
188	cp = bp->bio_from;
189	pp = bp->bio_to;
190
191	/* Fail if access counters dont allow the operation */
192	switch(bp->bio_cmd) {
193	case BIO_READ:
194	case BIO_GETATTR:
195		if (cp->acr == 0)
196			return (EPERM);
197		break;
198	case BIO_WRITE:
199	case BIO_DELETE:
200		if (cp->acw == 0)
201			return (EPERM);
202		break;
203	default:
204		return (EPERM);
205	}
206	/* if provider is marked for error, don't disturb. */
207	if (pp->error)
208		return (pp->error);
209
210	switch(bp->bio_cmd) {
211	case BIO_READ:
212	case BIO_WRITE:
213	case BIO_DELETE:
214		/* Zero sectorsize is a probably lack of media */
215		if (pp->sectorsize == 0)
216			return (ENXIO);
217		/* Reject I/O not on sector boundary */
218		if (bp->bio_offset % pp->sectorsize)
219			return (EINVAL);
220		/* Reject I/O not integral sector long */
221		if (bp->bio_length % pp->sectorsize)
222			return (EINVAL);
223		/* Reject requests before or past the end of media. */
224		if (bp->bio_offset < 0)
225			return (EIO);
226		if (bp->bio_offset > pp->mediasize)
227			return (EIO);
228		break;
229	default:
230		break;
231	}
232	return (0);
233}
234
235void
236g_io_request(struct bio *bp, struct g_consumer *cp)
237{
238	struct g_provider *pp;
239
240	KASSERT(cp != NULL, ("NULL cp in g_io_request"));
241	KASSERT(bp != NULL, ("NULL bp in g_io_request"));
242	KASSERT(bp->bio_data != NULL, ("NULL bp->data in g_io_request"));
243	pp = cp->provider;
244	KASSERT(pp != NULL, ("consumer not attached in g_io_request"));
245
246	bp->bio_from = cp;
247	bp->bio_to = pp;
248	bp->bio_error = 0;
249	bp->bio_completed = 0;
250
251	if (g_collectstats & 1)
252		devstat_start_transaction_bio(pp->stat, bp);
253	pp->nstart++;
254	if (g_collectstats & 2)
255		devstat_start_transaction_bio(cp->stat, bp);
256	cp->nstart++;
257
258	/* Pass it on down. */
259	g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
260	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd);
261	g_bioq_enqueue_tail(bp, &g_bio_run_down);
262	wakeup(&g_wait_down);
263}
264
265void
266g_io_deliver(struct bio *bp, int error)
267{
268	struct g_consumer *cp;
269	struct g_provider *pp;
270
271	KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
272	pp = bp->bio_to;
273	KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver"));
274	cp = bp->bio_from;
275	if (cp == NULL) {
276		bp->bio_error = error;
277		bp->bio_done(bp);
278		return;
279	}
280	KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver"));
281	KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
282	KASSERT(bp->bio_completed >= 0, ("bio_completed can't be less than 0"));
283	KASSERT(bp->bio_completed <= bp->bio_length,
284	    ("bio_completed can't be greater than bio_length"));
285
286	g_trace(G_T_BIO,
287"g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
288	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error,
289	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
290
291	bp->bio_bcount = bp->bio_length;
292	bp->bio_resid = bp->bio_bcount - bp->bio_completed;
293	if (g_collectstats & 1)
294		devstat_end_transaction_bio(pp->stat, bp);
295	if (g_collectstats & 2)
296		devstat_end_transaction_bio(cp->stat, bp);
297	cp->nend++;
298	pp->nend++;
299
300	if (error == ENOMEM) {
301		if (bootverbose)
302			printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name);
303		g_io_request(bp, cp);
304		pace++;
305		return;
306	}
307	bp->bio_error = error;
308	g_bioq_enqueue_tail(bp, &g_bio_run_up);
309	wakeup(&g_wait_up);
310}
311
312void
313g_io_schedule_down(struct thread *tp __unused)
314{
315	struct bio *bp;
316	off_t excess;
317	int error;
318	struct mtx mymutex;
319
320	bzero(&mymutex, sizeof mymutex);
321	mtx_init(&mymutex, "g_xdown", NULL, MTX_DEF);
322
323	for(;;) {
324		g_bioq_lock(&g_bio_run_down);
325		bp = g_bioq_first(&g_bio_run_down);
326		if (bp == NULL) {
327			msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock,
328			    PRIBIO | PDROP, "-", hz/10);
329			continue;
330		}
331		g_bioq_unlock(&g_bio_run_down);
332		if (pace > 0) {
333			msleep(&error, NULL, PRIBIO, "g_down", hz/10);
334			pace--;
335		}
336		error = g_io_check(bp);
337		if (error) {
338			g_io_deliver(bp, error);
339			continue;
340		}
341		switch (bp->bio_cmd) {
342		case BIO_READ:
343		case BIO_WRITE:
344		case BIO_DELETE:
345			/* Truncate requests to the end of providers media. */
346			excess = bp->bio_offset + bp->bio_length;
347			if (excess > bp->bio_to->mediasize) {
348				excess -= bp->bio_to->mediasize;
349				bp->bio_length -= excess;
350			}
351			/* Deliver zero length transfers right here. */
352			if (bp->bio_length == 0) {
353				g_io_deliver(bp, 0);
354				continue;
355			}
356			break;
357		default:
358			break;
359		}
360		mtx_lock(&mymutex);
361		bp->bio_to->geom->start(bp);
362		mtx_unlock(&mymutex);
363	}
364}
365
366void
367bio_taskqueue(struct bio *bp, bio_task_t *func, void *arg)
368{
369	bp->bio_task = func;
370	bp->bio_task_arg = arg;
371	/*
372	 * The taskqueue is actually just a second queue off the "up"
373	 * queue, so we use the same lock.
374	 */
375	g_bioq_lock(&g_bio_run_up);
376	TAILQ_INSERT_TAIL(&g_bio_run_task.bio_queue, bp, bio_queue);
377	g_bio_run_task.bio_queue_length++;
378	wakeup(&g_wait_up);
379	g_bioq_unlock(&g_bio_run_up);
380}
381
382
383void
384g_io_schedule_up(struct thread *tp __unused)
385{
386	struct bio *bp;
387	struct mtx mymutex;
388
389	bzero(&mymutex, sizeof mymutex);
390	mtx_init(&mymutex, "g_xup", NULL, MTX_DEF);
391	for(;;) {
392		g_bioq_lock(&g_bio_run_up);
393		bp = g_bioq_first(&g_bio_run_task);
394		if (bp != NULL) {
395			g_bioq_unlock(&g_bio_run_up);
396			mtx_lock(&mymutex);
397			bp->bio_task(bp->bio_task_arg);
398			mtx_unlock(&mymutex);
399			continue;
400		}
401		bp = g_bioq_first(&g_bio_run_up);
402		if (bp != NULL) {
403			g_bioq_unlock(&g_bio_run_up);
404			mtx_lock(&mymutex);
405			biodone(bp);
406			mtx_unlock(&mymutex);
407			continue;
408		}
409		msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock,
410		    PRIBIO | PDROP, "-", hz/10);
411	}
412}
413
414void *
415g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
416{
417	struct bio *bp;
418	void *ptr;
419	int errorc;
420
421	KASSERT(length >= 512 && length <= DFLTPHYS,
422		("g_read_data(): invalid length %jd", (intmax_t)length));
423
424	bp = g_new_bio();
425	bp->bio_cmd = BIO_READ;
426	bp->bio_done = NULL;
427	bp->bio_offset = offset;
428	bp->bio_length = length;
429	ptr = g_malloc(length, M_WAITOK);
430	bp->bio_data = ptr;
431	g_io_request(bp, cp);
432	errorc = biowait(bp, "gread");
433	if (error != NULL)
434		*error = errorc;
435	g_destroy_bio(bp);
436	if (errorc) {
437		g_free(ptr);
438		ptr = NULL;
439	}
440	return (ptr);
441}
442
443int
444g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
445{
446	struct bio *bp;
447	int error;
448
449	KASSERT(length >= 512 && length <= DFLTPHYS,
450		("g_write_data(): invalid length %jd", (intmax_t)length));
451
452	bp = g_new_bio();
453	bp->bio_cmd = BIO_WRITE;
454	bp->bio_done = NULL;
455	bp->bio_offset = offset;
456	bp->bio_length = length;
457	bp->bio_data = ptr;
458	g_io_request(bp, cp);
459	error = biowait(bp, "gwrite");
460	g_destroy_bio(bp);
461	return (error);
462}
463
464void
465g_print_bio(struct bio *bp)
466{
467	const char *pname, *cmd = NULL;
468
469	if (bp->bio_to != NULL)
470		pname = bp->bio_to->name;
471	else
472		pname = "[unknown]";
473
474	switch (bp->bio_cmd) {
475	case BIO_GETATTR:
476		cmd = "GETATTR";
477		printf("%s[%s(attr=%s)]", pname, cmd, bp->bio_attribute);
478		return;
479	case BIO_READ:
480		cmd = "READ";
481	case BIO_WRITE:
482		if (cmd == NULL)
483			cmd = "WRITE";
484	case BIO_DELETE:
485		if (cmd == NULL)
486			cmd = "DELETE";
487		printf("%s[%s(offset=%jd, length=%jd)]", pname, cmd,
488		    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
489		return;
490	default:
491		cmd = "UNKNOWN";
492		printf("%s[%s()]", pname, cmd);
493		return;
494	}
495	/* NOTREACHED */
496}
497