2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
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.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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
39 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
54 #include <sys/errno.h>
55 #include <geom/geom.h>
56 #include <geom/geom_int.h>
58 static struct g_bioq g_bio_run_down;
59 static struct g_bioq g_bio_run_up;
60 static struct g_bioq g_bio_idle;
62 #include <machine/atomic.h>
65 g_bioq_lock(struct g_bioq *bq)
68 mtx_lock(&bq->bio_queue_lock);
72 g_bioq_unlock(struct g_bioq *bq)
75 mtx_unlock(&bq->bio_queue_lock);
80 g_bioq_destroy(struct g_bioq *bq)
83 mtx_destroy(&bq->bio_queue_lock);
88 g_bioq_init(struct g_bioq *bq)
91 TAILQ_INIT(&bq->bio_queue);
92 mtx_init(&bq->bio_queue_lock, "bio queue", NULL, MTX_DEF);
96 g_bioq_first(struct g_bioq *bq)
101 bp = TAILQ_FIRST(&bq->bio_queue);
103 TAILQ_REMOVE(&bq->bio_queue, bp, bio_queue);
104 bq->bio_queue_length--;
111 g_bioq_enqueue_tail(struct bio *bp, struct g_bioq *rq)
115 TAILQ_INSERT_TAIL(&rq->bio_queue, bp, bio_queue);
116 rq->bio_queue_length++;
125 bp = g_bioq_first(&g_bio_idle);
127 bp = g_malloc(sizeof *bp, M_WAITOK | M_ZERO);
128 g_trace(G_T_BIO, "g_new_bio() = %p", bp);
133 g_destroy_bio(struct bio *bp)
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);
142 g_clone_bio(struct bio *bp)
147 bp2->bio_linkage = bp;
148 bp2->bio_cmd = bp->bio_cmd;
149 bp2->bio_length = bp->bio_length;
150 bp2->bio_offset = bp->bio_offset;
151 bp2->bio_data = bp->bio_data;
152 bp2->bio_attribute = bp->bio_attribute;
153 g_trace(G_T_BIO, "g_clone_bio(%p) = %p", bp, bp2);
161 g_bioq_init(&g_bio_run_down);
162 g_bioq_init(&g_bio_run_up);
163 g_bioq_init(&g_bio_idle);
167 g_io_setattr(char *attr, struct g_consumer *cp, int len, void *ptr)
172 g_trace(G_T_BIO, "bio_setattr(%s)", attr);
175 bp->bio_cmd = BIO_SETATTR;
177 bp->bio_attribute = attr;
178 bp->bio_length = len;
180 g_io_request(bp, cp);
181 while ((bp->bio_flags & BIO_DONE) == 0) {
183 tsleep(bp, 0, "setattr", hz / 10);
186 error = bp->bio_error;
189 tsleep(&error, 0, "setattr_busy", hz);
190 } while(error == EBUSY);
196 g_io_getattr(char *attr, struct g_consumer *cp, int *len, void *ptr)
201 g_trace(G_T_BIO, "bio_getattr(%s)", attr);
204 bp->bio_cmd = BIO_GETATTR;
206 bp->bio_attribute = attr;
207 bp->bio_length = *len;
209 g_io_request(bp, cp);
210 while ((bp->bio_flags & BIO_DONE) == 0) {
212 tsleep(bp, 0, "getattr", hz / 10);
215 *len = bp->bio_completed;
216 error = bp->bio_error;
219 tsleep(&error, 0, "getattr_busy", hz);
221 } while(error == EBUSY);
226 g_io_fail(struct bio *bp, int error)
229 bp->bio_error = error;
232 "bio_fail(%p) from %p(%s) to %p(%s) cmd %d error %d\n",
233 bp, bp->bio_from, bp->bio_from->geom->name,
234 bp->bio_to, bp->bio_to->name, bp->bio_cmd, bp->bio_error);
240 g_io_request(struct bio *bp, struct g_consumer *cp)
245 KASSERT(cp != NULL, ("bio_request on thin air"));
248 bp->bio_to = cp->provider;
250 bp->bio_completed = 0;
252 /* begin_stats(&bp->stats); */
254 atomic_add_int(&cp->biocount, 1);
255 /* Fail on unattached consumers */
256 if (bp->bio_to == NULL)
257 return (g_io_fail(bp, ENXIO));
258 /* Fail if access doesn't allow operation */
259 switch(bp->bio_cmd) {
263 return (g_io_fail(bp, EPERM));
268 return (g_io_fail(bp, EPERM));
271 if ((cp->acw == 0) || (cp->ace == 0))
272 return (g_io_fail(bp, EPERM));
275 return (g_io_fail(bp, EPERM));
277 /* if provider is marked for error, don't disturb. */
278 if (bp->bio_to->error)
279 return (g_io_fail(bp, bp->bio_to->error));
280 switch(bp->bio_cmd) {
284 /* Reject requests past the end of media. */
285 if (bp->bio_offset > bp->bio_to->mediasize)
286 return (g_io_fail(bp, EIO));
287 /* Truncate requests to the end of providers media. */
288 excess = bp->bio_offset + bp->bio_length;
289 if (excess > bp->bio_to->mediasize) {
290 excess -= bp->bio_to->mediasize;
291 bp->bio_length -= excess;
293 /* Deliver zero length transfers right here. */
294 if (bp->bio_length == 0)
295 return (g_io_deliver(bp));
300 /* Pass it on down. */
301 g_trace(G_T_BIO, "bio_request(%p) from %p(%s) to %p(%s) cmd %d",
302 bp, bp->bio_from, bp->bio_from->geom->name,
303 bp->bio_to, bp->bio_to->name, bp->bio_cmd);
304 g_bioq_enqueue_tail(bp, &g_bio_run_down);
306 wakeup(&g_wait_down);
311 g_io_deliver(struct bio *bp)
315 "g_io_deliver(%p) from %p(%s) to %p(%s) cmd %d error %d",
316 bp, bp->bio_from, bp->bio_from->geom->name,
317 bp->bio_to, bp->bio_to->name, bp->bio_cmd, bp->bio_error);
318 /* finish_stats(&bp->stats); */
320 g_bioq_enqueue_tail(bp, &g_bio_run_up);
328 g_io_schedule_down(struct thread *tp __unused)
333 bp = g_bioq_first(&g_bio_run_down);
336 bp->bio_to->geom->start(bp);
341 g_io_schedule_up(struct thread *tp __unused)
344 struct g_consumer *cp;
347 bp = g_bioq_first(&g_bio_run_up);
353 bp->bio_flags |= BIO_DONE;
354 atomic_add_int(&cp->biocount, -1);
355 if (bp->bio_done != NULL) {
366 g_read_data(struct g_consumer *cp, off_t offset, off_t length, int *error)
374 bp->bio_cmd = BIO_READ;
376 bp->bio_offset = offset;
377 bp->bio_length = length;
378 ptr = g_malloc(length, M_WAITOK);
380 g_io_request(bp, cp);
381 while ((bp->bio_flags & BIO_DONE) == 0) {
383 tsleep(bp, 0, "g_read_data", hz / 10);
386 errorc = bp->bio_error;
395 tsleep(&errorc, 0, "g_read_data_busy", hz);
396 } while (errorc == EBUSY);