geom_io.c revision 108297
1214152Sed/*-
2214152Sed * Copyright (c) 2002 Poul-Henning Kamp
3214152Sed * Copyright (c) 2002 Networks Associates Technology, Inc.
4214152Sed * All rights reserved.
5222656Sed *
6222656Sed * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7214152Sed * and NAI Labs, the Security Research Division of Network Associates, Inc.
8214152Sed * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9214152Sed * DARPA CHATS research program.
10214152Sed *
11214152Sed * Redistribution and use in source and binary forms, with or without
12214152Sed * modification, are permitted provided that the following conditions
13214152Sed * are met:
14214152Sed * 1. Redistributions of source code must retain the above copyright
15214152Sed *    notice, this list of conditions and the following disclaimer.
16214152Sed * 2. Redistributions in binary form must reproduce the above copyright
17214152Sed *    notice, this list of conditions and the following disclaimer in the
18214152Sed *    documentation and/or other materials provided with the distribution.
19214152Sed * 3. The names of the authors may not be used to endorse or promote
20214152Sed *    products derived from this software without specific prior written
21214152Sed *    permission.
22214152Sed *
23214152Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24214152Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25214152Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26214152Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27229135Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28214152Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29214152Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30214152Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31214152Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32214152Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33214152Sed * SUCH DAMAGE.
34214152Sed *
35214152Sed * $FreeBSD: head/sys/geom/geom_io.c 108297 2002-12-26 21:02:50Z phk $
36214152Sed */
37214152Sed
38214152Sed
39214152Sed#include <sys/param.h>
40214152Sed#include <sys/stdint.h>
41214152Sed#ifndef _KERNEL
42214152Sed#include <stdio.h>
43214152Sed#include <string.h>
44214152Sed#include <stdlib.h>
45214152Sed#include <signal.h>
46214152Sed#include <err.h>
47214152Sed#include <sched.h>
48214152Sed#else
49214152Sed#include <sys/systm.h>
50214152Sed#include <sys/kernel.h>
51214152Sed#include <sys/malloc.h>
52214152Sed#include <sys/bio.h>
53214152Sed#endif
54214152Sed
55214152Sed#include <sys/errno.h>
56214152Sed#include <geom/geom.h>
57214152Sed#include <geom/geom_int.h>
58214152Sed
59214152Sedstatic struct g_bioq g_bio_run_down;
60214152Sedstatic struct g_bioq g_bio_run_up;
61214152Sedstatic struct g_bioq g_bio_idle;
62214152Sed
63214152Sedstatic u_int pace;
64214152Sed
65214152Sed#include <machine/atomic.h>
66214152Sed
67214152Sedstatic void
68214152Sedg_bioq_lock(struct g_bioq *bq)
69214152Sed{
70214152Sed
71214152Sed	mtx_lock(&bq->bio_queue_lock);
72214152Sed}
73214152Sed
74214152Sedstatic void
75214152Sedg_bioq_unlock(struct g_bioq *bq)
76214152Sed{
77214152Sed
78214152Sed	mtx_unlock(&bq->bio_queue_lock);
79214152Sed}
80214152Sed
81214152Sed#if 0
82214152Sedstatic void
83214152Sedg_bioq_destroy(struct g_bioq *bq)
84214152Sed{
85214152Sed
86214152Sed	mtx_destroy(&bq->bio_queue_lock);
87214152Sed}
88214152Sed#endif
89214152Sed
90214152Sedstatic void
91214152Sedg_bioq_init(struct g_bioq *bq)
92214152Sed{
93214152Sed
94214152Sed	TAILQ_INIT(&bq->bio_queue);
95214152Sed	mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF);
96214152Sed}
97214152Sed
98214152Sedstatic struct bio *
99214152Sedg_bioq_first(struct g_bioq *bq)
100214152Sed{
101214152Sed	struct bio *bp;
102214152Sed
103214152Sed	g_bioq_lock(bq);
104214152Sed	bp = TAILQ_FIRST(&bq->bio_queue);
105214152Sed	if (bp != NULL) {
106214152Sed		TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
107214152Sed		bq->bio_queue_length--;
108214152Sed	}
109214152Sed	g_bioq_unlock(bq);
110214152Sed	return (bp);
111214152Sed}
112214152Sed
113214152Sedstatic void
114214152Sedg_bioq_enqueue_tail(struct bio *bp, struct g_bioq *rq)
115214152Sed{
116214152Sed
117214152Sed	g_bioq_lock(rq);
118214152Sed	TAILQ_INSERT_TAIL(&rq->bio_queue, bp, bio_queue);
119214152Sed	rq->bio_queue_length++;
120214152Sed	g_bioq_unlock(rq);
121214152Sed}
122214152Sed
123214152Sedstruct bio *
124214152Sedg_new_bio(void)
125214152Sed{
126214152Sed	struct bio *bp;
127239138Sandrew
128214152Sed	bp = g_bioq_first(&g_bio_idle);
129214152Sed	if (bp == NULL)
130214152Sed		bp = g_malloc(sizeof *bp, M_NOWAIT | M_ZERO);
131214152Sed	/* g_trace(G_T_BIO, "g_new_bio() = %p", bp); */
132214152Sed	return (bp);
133214152Sed}
134214152Sed
135214152Sedvoid
136214152Sedg_destroy_bio(struct bio *bp)
137214152Sed{
138214152Sed
139214152Sed	/* g_trace(G_T_BIO, "g_destroy_bio(%p)", bp); */
140214152Sed	bzero(bp, sizeof *bp);
141214152Sed	g_bioq_enqueue_tail(bp, &g_bio_idle);
142214152Sed}
143214152Sed
144214152Sedstruct 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_linkage = 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++;	/* XXX: atomic ? */
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_setattr(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_setattr(%s)", attr);
179	bp = g_new_bio();
180	bp->bio_cmd = BIO_SETATTR;
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, "gsetattr");
187	g_destroy_bio(bp);
188	return (error);
189}
190
191
192int
193g_io_getattr(const char *attr, struct g_consumer *cp, int *len, void *ptr)
194{
195	struct bio *bp;
196	int error;
197
198	g_trace(G_T_BIO, "bio_getattr(%s)", attr);
199	bp = g_new_bio();
200	bp->bio_cmd = BIO_GETATTR;
201	bp->bio_done = NULL;
202	bp->bio_attribute = attr;
203	bp->bio_length = *len;
204	bp->bio_data = ptr;
205	g_io_request(bp, cp);
206	error = biowait(bp, "ggetattr");
207	*len = bp->bio_completed;
208	g_destroy_bio(bp);
209	return (error);
210}
211
212void
213g_io_request(struct bio *bp, struct g_consumer *cp)
214{
215	int error;
216	off_t excess;
217
218	KASSERT(cp != NULL, ("NULL cp in g_io_request"));
219	KASSERT(bp != NULL, ("NULL bp in g_io_request"));
220	KASSERT(bp->bio_data != NULL, ("NULL bp->data in g_io_request"));
221	error = 0;
222	bp->bio_from = cp;
223	bp->bio_to = cp->provider;
224	bp->bio_error = 0;
225	bp->bio_completed = 0;
226
227	/* begin_stats(&bp->stats); */
228
229	atomic_add_int(&cp->biocount, 1);
230	/* Fail on unattached consumers */
231	if (bp->bio_to == NULL) {
232		g_io_deliver(bp, ENXIO);
233		return;
234	}
235	/* Fail if access doesn't allow operation */
236	switch(bp->bio_cmd) {
237	case BIO_READ:
238	case BIO_GETATTR:
239		if (cp->acr == 0) {
240			g_io_deliver(bp, EPERM);
241			return;
242		}
243		break;
244	case BIO_WRITE:
245	case BIO_DELETE:
246		if (cp->acw == 0) {
247			g_io_deliver(bp, EPERM);
248			return;
249		}
250		break;
251	case BIO_SETATTR:
252		/* XXX: Should ideally check for (cp->ace == 0) */
253		if ((cp->acw == 0)) {
254#ifdef DIAGNOSTIC
255			printf("setattr on %s mode (%d,%d,%d)\n",
256				cp->provider->name,
257				cp->acr, cp->acw, cp->ace);
258#endif
259			g_io_deliver(bp, EPERM);
260			return;
261		}
262		break;
263	default:
264		g_io_deliver(bp, EPERM);
265		return;
266	}
267	/* if provider is marked for error, don't disturb. */
268	if (bp->bio_to->error) {
269		g_io_deliver(bp, bp->bio_to->error);
270		return;
271	}
272	switch(bp->bio_cmd) {
273	case BIO_READ:
274	case BIO_WRITE:
275	case BIO_DELETE:
276		/* Reject I/O not on sector boundary */
277		if (bp->bio_offset % bp->bio_to->sectorsize) {
278			g_io_deliver(bp, EINVAL);
279			return;
280		}
281		/* Reject I/O not integral sector long */
282		if (bp->bio_length % bp->bio_to->sectorsize) {
283			g_io_deliver(bp, EINVAL);
284			return;
285		}
286		/* Reject requests past the end of media. */
287		if (bp->bio_offset > bp->bio_to->mediasize) {
288			g_io_deliver(bp, EIO);
289			return;
290		}
291		/* Truncate requests to the end of providers media. */
292		excess = bp->bio_offset + bp->bio_length;
293		if (excess > bp->bio_to->mediasize) {
294			excess -= bp->bio_to->mediasize;
295			bp->bio_length -= excess;
296		}
297		/* Deliver zero length transfers right here. */
298		if (bp->bio_length == 0) {
299			g_io_deliver(bp, 0);
300			return;
301		}
302		break;
303	default:
304		break;
305	}
306	/* Pass it on down. */
307	g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
308	    bp, bp->bio_from, bp->bio_from->geom->name,
309	    bp->bio_to, bp->bio_to->name, bp->bio_cmd);
310	g_bioq_enqueue_tail(bp, &g_bio_run_down);
311	wakeup(&g_wait_down);
312}
313
314void
315g_io_deliver(struct bio *bp, int error)
316{
317
318	KASSERT(bp != NULL, ("NULL bp in g_io_deliver"));
319	KASSERT(bp->bio_from != NULL, ("NULL bio_from in g_io_deliver"));
320	KASSERT(bp->bio_from->geom != NULL,
321	    ("NULL bio_from->geom in g_io_deliver"));
322	KASSERT(bp->bio_to != NULL, ("NULL bio_to in g_io_deliver"));
323
324	g_trace(G_T_BIO,
325"g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d off %jd len %jd",
326	    bp, bp->bio_from, bp->bio_from->geom->name,
327	    bp->bio_to, bp->bio_to->name, bp->bio_cmd, error,
328	    (intmax_t)bp->bio_offset, (intmax_t)bp->bio_length);
329	/* finish_stats(&bp->stats); */
330
331	if (error == ENOMEM) {
332		printf("ENOMEM %p on %p(%s)\n",
333			bp, bp->bio_to, bp->bio_to->name);
334		g_io_request(bp, bp->bio_from);
335		pace++;
336		return;
337	}
338
339	bp->bio_error = error;
340
341	g_bioq_enqueue_tail(bp, &g_bio_run_up);
342
343	wakeup(&g_wait_up);
344}
345
346void
347g_io_schedule_down(struct thread *tp __unused)
348{
349	struct bio *bp;
350
351	for(;;) {
352		bp = g_bioq_first(&g_bio_run_down);
353		if (bp == NULL)
354			break;
355		bp->bio_to->geom->start(bp);
356		if (pace) {
357			pace--;
358			break;
359		}
360	}
361}
362
363void
364g_io_schedule_up(struct thread *tp __unused)
365{
366	struct bio *bp;
367	struct g_consumer *cp;
368
369	for(;;) {
370		bp = g_bioq_first(&g_bio_run_up);
371		if (bp == NULL)
372			break;
373
374		cp = bp->bio_from;
375
376		atomic_add_int(&cp->biocount, -1);
377		biodone(bp);
378	}
379}
380
381void *
382g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
383{
384	struct bio *bp;
385	void *ptr;
386	int errorc;
387
388	bp = g_new_bio();
389	bp->bio_cmd = BIO_READ;
390	bp->bio_done = NULL;
391	bp->bio_offset = offset;
392	bp->bio_length = length;
393	ptr = g_malloc(length, M_WAITOK);
394	bp->bio_data = ptr;
395	g_io_request(bp, cp);
396	errorc = biowait(bp, "gread");
397	if (error != NULL)
398		*error = errorc;
399	g_destroy_bio(bp);
400	if (errorc) {
401		g_free(ptr);
402		ptr = NULL;
403	}
404	return (ptr);
405}
406
407int
408g_write_data(struct g_consumer *cp, off_t offset, void *ptr, off_t length)
409{
410	struct bio *bp;
411	int error;
412
413	bp = g_new_bio();
414	bp->bio_cmd = BIO_WRITE;
415	bp->bio_done = NULL;
416	bp->bio_offset = offset;
417	bp->bio_length = length;
418	bp->bio_data = ptr;
419	g_io_request(bp, cp);
420	error = biowait(bp, "gwrite");
421	g_destroy_bio(bp);
422	return (error);
423}
424