geom_io.c revision 105091
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 105091 2002-10-14 10:02:24Z 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
58static struct g_bioq g_bio_run_down;
59static struct g_bioq g_bio_run_up;
60static struct g_bioq g_bio_idle;
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	g_bioq_lock(bq);
101	bp = TAILQ_FIRST(&bq->bio_queue);
102	if (bp != NULL) {
103		TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
104		bq->bio_queue_length--;
105	}
106	g_bioq_unlock(bq);
107	return (bp);
108}
109
110static void
111g_bioq_enqueue_tail(struct bio *bp, struct g_bioq *rq)
112{
113
114	g_bioq_lock(rq);
115	TAILQ_INSERT_TAIL(&rq->bio_queue, bp, bio_queue);
116	rq->bio_queue_length++;
117	g_bioq_unlock(rq);
118}
119
120struct bio *
121g_new_bio(void)
122{
123	struct bio *bp;
124
125	bp = g_bioq_first(&g_bio_idle);
126	if (bp == NULL)
127		bp = g_malloc(sizeof *bp, M_NOWAIT | M_ZERO);
128	g_trace(G_T_BIO, "g_new_bio() = %p", bp);
129	return (bp);
130}
131
132void
133g_destroy_bio(struct bio *bp)
134{
135
136	g_trace(G_T_BIO, "g_destroy_bio(%p)", bp);
137	bzero(bp, sizeof *bp);
138	g_bioq_enqueue_tail(bp, &g_bio_idle);
139}
140
141struct bio *
142g_clone_bio(struct bio *bp)
143{
144	struct bio *bp2;
145
146	bp2 = g_new_bio();
147	if (bp2 != NULL) {
148		bp2->bio_linkage = bp;
149		bp2->bio_cmd = bp->bio_cmd;
150		bp2->bio_length = bp->bio_length;
151		bp2->bio_offset = bp->bio_offset;
152		bp2->bio_data = bp->bio_data;
153		bp2->bio_attribute = bp->bio_attribute;
154		bp->bio_children++;	/* XXX: atomic ? */
155	}
156	g_trace(G_T_BIO, "g_clone_bio(%p) = %p", bp, bp2);
157	return(bp2);
158}
159
160void
161g_io_init()
162{
163
164	g_bioq_init(&g_bio_run_down);
165	g_bioq_init(&g_bio_run_up);
166	g_bioq_init(&g_bio_idle);
167}
168
169int
170g_io_setattr(const char *attr, struct g_consumer *cp, int len, void *ptr)
171{
172	struct bio *bp;
173	int error;
174
175	g_trace(G_T_BIO, "bio_setattr(%s)", attr);
176	bp = g_new_bio();
177	bp->bio_cmd = BIO_SETATTR;
178	bp->bio_done = NULL;
179	bp->bio_attribute = attr;
180	bp->bio_length = len;
181	bp->bio_data = ptr;
182	g_io_request(bp, cp);
183	error = biowait(bp, "gsetattr");
184	g_destroy_bio(bp);
185	return (error);
186}
187
188
189int
190g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr)
191{
192	struct bio *bp;
193	int error;
194
195	g_trace(G_T_BIO, "bio_getattr(%s)", attr);
196	bp = g_new_bio();
197	bp->bio_cmd = BIO_GETATTR;
198	bp->bio_done = NULL;
199	bp->bio_attribute = attr;
200	bp->bio_length = *len;
201	bp->bio_data = ptr;
202	g_io_request(bp, cp);
203	error = biowait(bp, "ggetattr");
204	*len = bp->bio_completed;
205	g_destroy_bio(bp);
206	return (error);
207}
208
209void
210g_io_request(struct bio *bp, struct g_consumer *cp)
211{
212	int error;
213	off_t excess;
214
215	KASSERT(cp != NULL, ("NULL cp in g_io_request"));
216	KASSERT(bp != NULL, ("NULL bp in g_io_request"));
217	KASSERT(bp->bio_data != NULL, ("NULL bp->data in g_io_request"));
218	error = 0;
219	bp->bio_from = cp;
220	bp->bio_to = cp->provider;
221	bp->bio_error = 0;
222	bp->bio_completed = 0;
223
224	/* begin_stats(&bp->stats); */
225
226	atomic_add_int(&cp->biocount, 1);
227	/* Fail on unattached consumers */
228	if (bp->bio_to == NULL) {
229		g_io_deliver(bp, ENXIO);
230		return;
231	}
232	/* Fail if access doesn't allow operation */
233	switch(bp->bio_cmd) {
234	case BIO_READ:
235	case BIO_GETATTR:
236		if (cp->acr == 0) {
237			g_io_deliver(bp, EPERM);
238			return;
239		}
240		break;
241	case BIO_WRITE:
242	case BIO_DELETE:
243		if (cp->acw == 0) {
244			g_io_deliver(bp, EPERM);
245			return;
246		}
247		break;
248	case BIO_SETATTR:
249		/* XXX: Should ideally check for (cp->ace == 0) */
250		if ((cp->acw == 0)) {
251#ifdef DIAGNOSTIC
252			printf("setattr on %s mode (%d,%d,%d)\n",
253				cp->provider->name,
254				cp->acr, cp->acw, cp->ace);
255#endif
256			g_io_deliver(bp, EPERM);
257			return;
258		}
259		break;
260	default:
261		g_io_deliver(bp, EPERM);
262		return;
263	}
264	/* if provider is marked for error, don't disturb. */
265	if (bp->bio_to->error) {
266		g_io_deliver(bp, bp->bio_to->error);
267		return;
268	}
269	switch(bp->bio_cmd) {
270	case BIO_READ:
271	case BIO_WRITE:
272	case BIO_DELETE:
273		/* Reject requests past the end of media. */
274		if (bp->bio_offset > bp->bio_to->mediasize) {
275			g_io_deliver(bp, EIO);
276			return;
277		}
278		/* Truncate requests to the end of providers media. */
279		excess = bp->bio_offset + bp->bio_length;
280		if (excess > bp->bio_to->mediasize) {
281			excess -= bp->bio_to->mediasize;
282			bp->bio_length -= excess;
283		}
284		/* Deliver zero length transfers right here. */
285		if (bp->bio_length == 0) {
286			g_io_deliver(bp, 0);
287			return;
288		}
289		break;
290	default:
291		break;
292	}
293	/* Pass it on down. */
294	g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
295	    bp, bp->bio_from, bp->bio_from->geom->name,
296	    bp->bio_to, bp->bio_to->name, bp->bio_cmd);
297	g_bioq_enqueue_tail(bp, &g_bio_run_down);
298	wakeup(&g_wait_down);
299}
300
301void
302g_io_deliver(struct bio *bp, int error)
303{
304
305	KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
306	KASSERT(bp->bio_from != NULL, ("NULL bio_from in g_io_deliver"));
307	KASSERT(bp->bio_from->geom != NULL, ("NULL bio_from->geom in g_io_deliver"));
308	KASSERT(bp->bio_to != NULL, ("NULL bio_to in g_io_deliver"));
309	g_trace(G_T_BIO,
310	    "g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d",
311	    bp, bp->bio_from, bp->bio_from->geom->name,
312	    bp->bio_to, bp->bio_to->name, bp->bio_cmd, error);
313	/* finish_stats(&bp->stats); */
314
315	bp->bio_error = error;
316
317	g_bioq_enqueue_tail(bp, &g_bio_run_up);
318
319	wakeup(&g_wait_up);
320}
321
322void
323g_io_schedule_down(struct thread *tp __unused)
324{
325	struct bio *bp;
326
327	for(;;) {
328		bp = g_bioq_first(&g_bio_run_down);
329		if (bp == NULL)
330			break;
331		bp->bio_to->geom->start(bp);
332	}
333}
334
335void
336g_io_schedule_up(struct thread *tp __unused)
337{
338	struct bio *bp;
339	struct g_consumer *cp;
340
341	for(;;) {
342		bp = g_bioq_first(&g_bio_run_up);
343		if (bp == NULL)
344			break;
345
346		cp = bp->bio_from;
347
348		atomic_add_int(&cp->biocount, -1);
349		biodone(bp);
350	}
351}
352
353void *
354g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
355{
356	struct bio *bp;
357	void *ptr;
358	int errorc;
359
360	bp = g_new_bio();
361	bp->bio_cmd = BIO_READ;
362	bp->bio_done = NULL;
363	bp->bio_offset = offset;
364	bp->bio_length = length;
365	ptr = g_malloc(length, M_WAITOK);
366	bp->bio_data = ptr;
367	g_io_request(bp, cp);
368	errorc = biowait(bp, "gread");
369	if (error != NULL)
370		*error = errorc;
371	g_destroy_bio(bp);
372	if (errorc) {
373		g_free(ptr);
374		ptr = NULL;
375	}
376	return (ptr);
377}
378
379int
380g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
381{
382	struct bio *bp;
383	int error;
384
385	bp = g_new_bio();
386	bp->bio_cmd = BIO_WRITE;
387	bp->bio_done = NULL;
388	bp->bio_offset = offset;
389	bp->bio_length = length;
390	bp->bio_data = ptr;
391	g_io_request(bp, cp);
392	error = biowait(bp, "gwrite");
393	g_destroy_bio(bp);
394	return (error);
395}
396