geom_io.c revision 113392
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 * $FreeBSD: head/sys/geom/geom_io.c 113392 2003-04-12 09:13:01Z phk $
36 */
37
38
39#include <sys/param.h>
40#ifndef _KERNEL
41#include <stdio.h>
42#include <string.h>
43#include <stdlib.h>
44#include <signal.h>
45#include <err.h>
46#include <sched.h>
47#else
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/malloc.h>
51#include <sys/bio.h>
52#endif
53
54#include <sys/errno.h>
55#include <geom/geom.h>
56#include <geom/geom_int.h>
57#include <sys/devicestat.h>
58
59static struct g_bioq g_bio_run_down;
60static struct g_bioq g_bio_run_up;
61static struct g_bioq g_bio_idle;
62
63static u_int pace;
64
65#include <machine/atomic.h>
66
67static void
68g_bioq_lock(struct g_bioq *bq)
69{
70
71	mtx_lock(&bq->bio_queue_lock);
72}
73
74static void
75g_bioq_unlock(struct g_bioq *bq)
76{
77
78	mtx_unlock(&bq->bio_queue_lock);
79}
80
81#if 0
82static void
83g_bioq_destroy(struct g_bioq *bq)
84{
85
86	mtx_destroy(&bq->bio_queue_lock);
87}
88#endif
89
90static void
91g_bioq_init(struct g_bioq *bq)
92{
93
94	TAILQ_INIT(&bq->bio_queue);
95	mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF);
96}
97
98static struct bio *
99g_bioq_first(struct g_bioq *bq)
100{
101	struct bio *bp;
102
103	bp = TAILQ_FIRST(&bq->bio_queue);
104	if (bp != NULL) {
105		TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
106		bq->bio_queue_length--;
107	}
108	return (bp);
109}
110
111static void
112g_bioq_enqueue_tail(struct bio *bp, struct g_bioq *rq)
113{
114
115	g_bioq_lock(rq);
116	TAILQ_INSERT_TAIL(&rq->bio_queue, bp, bio_queue);
117	rq->bio_queue_length++;
118	g_bioq_unlock(rq);
119}
120
121struct bio *
122g_new_bio(void)
123{
124	struct bio *bp;
125
126	g_bioq_lock(&g_bio_idle);
127	bp = g_bioq_first(&g_bio_idle);
128	g_bioq_unlock(&g_bio_idle);
129	if (bp == NULL)
130		bp = g_malloc(sizeof *bp, M_NOWAIT | M_ZERO);
131	/* g_trace(G_T_BIO, "g_new_bio() = %p", bp); */
132	return (bp);
133}
134
135void
136g_destroy_bio(struct bio *bp)
137{
138
139	/* g_trace(G_T_BIO, "g_destroy_bio(%p)", bp); */
140	bzero(bp, sizeof *bp);
141	g_bioq_enqueue_tail(bp, &g_bio_idle);
142}
143
144struct bio *
145g_clone_bio(struct bio *bp)
146{
147	struct bio *bp2;
148
149	bp2 = g_new_bio();
150	if (bp2 != NULL) {
151		bp2->bio_parent = bp;
152		bp2->bio_cmd = bp->bio_cmd;
153		bp2->bio_length = bp->bio_length;
154		bp2->bio_offset = bp->bio_offset;
155		bp2->bio_data = bp->bio_data;
156		bp2->bio_attribute = bp->bio_attribute;
157		bp->bio_children++;
158	}
159	/* g_trace(G_T_BIO, "g_clone_bio(%p) = %p", bp, bp2); */
160	return(bp2);
161}
162
163void
164g_io_init()
165{
166
167	g_bioq_init(&g_bio_run_down);
168	g_bioq_init(&g_bio_run_up);
169	g_bioq_init(&g_bio_idle);
170}
171
172int
173g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr)
174{
175	struct bio *bp;
176	int error;
177
178	g_trace(G_T_BIO, "bio_getattr(%s)", attr);
179	bp = g_new_bio();
180	bp->bio_cmd = BIO_GETATTR;
181	bp->bio_done = NULL;
182	bp->bio_attribute = attr;
183	bp->bio_length = *len;
184	bp->bio_data = ptr;
185	g_io_request(bp, cp);
186	error = biowait(bp, "ggetattr");
187	*len = bp->bio_completed;
188	g_destroy_bio(bp);
189	return (error);
190}
191
192static int
193g_io_check(struct bio *bp)
194{
195	struct g_consumer *cp;
196	struct g_provider *pp;
197
198	cp = bp->bio_from;
199	pp = bp->bio_to;
200
201	/* Fail if access counters dont allow the operation */
202	switch(bp->bio_cmd) {
203	case BIO_READ:
204	case BIO_GETATTR:
205		if (cp->acr == 0)
206			return (EPERM);
207		break;
208	case BIO_WRITE:
209	case BIO_DELETE:
210		if (cp->acw == 0)
211			return (EPERM);
212		break;
213	default:
214		return (EPERM);
215	}
216	/* if provider is marked for error, don't disturb. */
217	if (pp->error)
218		return (pp->error);
219
220	switch(bp->bio_cmd) {
221	case BIO_READ:
222	case BIO_WRITE:
223	case BIO_DELETE:
224		/* Reject I/O not on sector boundary */
225		if (bp->bio_offset % pp->sectorsize)
226			return (EINVAL);
227		/* Reject I/O not integral sector long */
228		if (bp->bio_length % pp->sectorsize)
229			return (EINVAL);
230		/* Reject requests past the end of media. */
231		if (bp->bio_offset > pp->mediasize)
232			return (EIO);
233		break;
234	default:
235		break;
236	}
237	return (0);
238}
239
240void
241g_io_request(struct bio *bp, struct g_consumer *cp)
242{
243	struct g_provider *pp;
244
245	pp = cp->provider;
246	KASSERT(cp != NULL, ("NULL cp in g_io_request"));
247	KASSERT(bp != NULL, ("NULL bp in g_io_request"));
248	KASSERT(bp->bio_data != NULL, ("NULL bp->data in g_io_request"));
249	KASSERT(pp != NULL, ("consumer not attached in g_io_request"));
250
251	bp->bio_from = cp;
252	bp->bio_to = pp;
253	bp->bio_error = 0;
254	bp->bio_completed = 0;
255
256	if (g_collectstats) {
257		devstat_start_transaction_bio(cp->stat, bp);
258		devstat_start_transaction_bio(pp->stat, bp);
259	}
260	cp->nstart++;
261	pp->nstart++;
262
263	/* Pass it on down. */
264	g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
265	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd);
266	g_bioq_enqueue_tail(bp, &g_bio_run_down);
267	wakeup(&g_wait_down);
268}
269
270void
271g_io_deliver(struct bio *bp, int error)
272{
273	struct g_consumer *cp;
274	struct g_provider *pp;
275
276	cp = bp->bio_from;
277	pp = bp->bio_to;
278	KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
279	KASSERT(cp != NULL, ("NULL bio_from in g_io_deliver"));
280	KASSERT(cp->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
281	KASSERT(pp != NULL, ("NULL bio_to in g_io_deliver"));
282
283	g_trace(G_T_BIO,
284"g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
285	    bp, cp, cp->geom->name, pp, pp->name, bp->bio_cmd, error,
286	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
287
288	bp->bio_bcount = bp->bio_length;
289	if (g_collectstats) {
290		bp->bio_resid = bp->bio_bcount - bp->bio_completed;
291		devstat_end_transaction_bio(cp->stat, bp);
292		devstat_end_transaction_bio(pp->stat, bp);
293	}
294	cp->nend++;
295	pp->nend++;
296
297	if (error == ENOMEM) {
298		printf("ENOMEM %p on %p(%s)\n", bp, pp, pp->name);
299		g_io_request(bp, cp);
300		pace++;
301		return;
302	}
303	bp->bio_error = error;
304	g_bioq_enqueue_tail(bp, &g_bio_run_up);
305	wakeup(&g_wait_up);
306}
307
308void
309g_io_schedule_down(struct thread *tp __unused)
310{
311	struct bio *bp;
312	off_t excess;
313	int error;
314	struct mtx mymutex;
315
316	bzero(&mymutex, sizeof mymutex);
317	mtx_init(&mymutex, "g_xdown", MTX_DEF, 0);
318
319	for(;;) {
320		g_bioq_lock(&g_bio_run_down);
321		bp = g_bioq_first(&g_bio_run_down);
322		if (bp == NULL) {
323			msleep(&g_wait_down, &g_bio_run_down.bio_queue_lock,
324			    PRIBIO | PDROP, "g_down", hz/10);
325			continue;
326		}
327		g_bioq_unlock(&g_bio_run_down);
328		if (pace > 0) {
329			msleep(&error, NULL, PRIBIO, "g_down", hz/10);
330			pace--;
331		}
332		error = g_io_check(bp);
333		if (error) {
334			g_io_deliver(bp, error);
335			continue;
336		}
337		switch (bp->bio_cmd) {
338		case BIO_READ:
339		case BIO_WRITE:
340		case BIO_DELETE:
341			/* Truncate requests to the end of providers media. */
342			excess = bp->bio_offset + bp->bio_length;
343			if (excess > bp->bio_to->mediasize) {
344				excess -= bp->bio_to->mediasize;
345				bp->bio_length -= excess;
346			}
347			/* Deliver zero length transfers right here. */
348			if (bp->bio_length == 0) {
349				g_io_deliver(bp, 0);
350				continue;
351			}
352			break;
353		default:
354			break;
355		}
356		mtx_lock(&mymutex);
357		bp->bio_to->geom->start(bp);
358		mtx_unlock(&mymutex);
359	}
360}
361
362void
363g_io_schedule_up(struct thread *tp __unused)
364{
365	struct bio *bp;
366	struct mtx mymutex;
367
368	bzero(&mymutex, sizeof mymutex);
369	mtx_init(&mymutex, "g_xup", MTX_DEF, 0);
370	for(;;) {
371		g_bioq_lock(&g_bio_run_up);
372		bp = g_bioq_first(&g_bio_run_up);
373		if (bp != NULL) {
374			g_bioq_unlock(&g_bio_run_up);
375			mtx_lock(&mymutex);
376			biodone(bp);
377			mtx_unlock(&mymutex);
378			continue;
379		}
380		msleep(&g_wait_up, &g_bio_run_up.bio_queue_lock,
381		    PRIBIO | PDROP, "g_up", hz/10);
382	}
383}
384
385void *
386g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
387{
388	struct bio *bp;
389	void *ptr;
390	int errorc;
391
392	bp = g_new_bio();
393	bp->bio_cmd = BIO_READ;
394	bp->bio_done = NULL;
395	bp->bio_offset = offset;
396	bp->bio_length = length;
397	ptr = g_malloc(length, M_WAITOK);
398	bp->bio_data = ptr;
399	g_io_request(bp, cp);
400	errorc = biowait(bp, "gread");
401	if (error != NULL)
402		*error = errorc;
403	g_destroy_bio(bp);
404	if (errorc) {
405		g_free(ptr);
406		ptr = NULL;
407	}
408	return (ptr);
409}
410
411int
412g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
413{
414	struct bio *bp;
415	int error;
416
417	bp = g_new_bio();
418	bp->bio_cmd = BIO_WRITE;
419	bp->bio_done = NULL;
420	bp->bio_offset = offset;
421	bp->bio_length = length;
422	bp->bio_data = ptr;
423	g_io_request(bp, cp);
424	error = biowait(bp, "gwrite");
425	g_destroy_bio(bp);
426	return (error);
427}
428