2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/dev/virtual/disk/vdisk.c,v 1.8 2008/03/20 02:14:56 dillon Exp $
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
48 #include <sys/devicestat.h>
50 #include <machine/cothread.h>
51 #include <machine/md_var.h>
59 struct bio_queue_head bio_queue;
63 TAILQ_HEAD(, bio) cotd_queue;
64 TAILQ_HEAD(, bio) cotd_done;
72 static void vkd_io_thread(cothread_t cotd);
73 static void vkd_io_intr(cothread_t cotd);
74 static void vkd_doio(struct vkd_softc *sc, struct bio *bio);
76 static d_strategy_t vkdstrategy;
77 static d_open_t vkdopen;
79 static struct dev_ops vkd_ops = {
80 { "vkd", CDEV_MAJOR, D_DISK },
85 .d_strategy = vkdstrategy,
89 vkdinit(void *dummy __unused)
91 struct vkdisk_info *dsk;
93 struct disk_info info;
97 for (i = 0; i < DiskNum; i++) {
98 /* check that the 'bus device' has been initialized */
100 if (dsk == NULL || dsk->type != VKD_DISK)
102 if (dsk->fd < 0 || fstat(dsk->fd, &st) < 0)
105 /* and create a new device */
106 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
107 sc->unit = dsk->unit;
109 bioq_init(&sc->bio_queue);
110 devstat_add_entry(&sc->stats, "vkd", sc->unit, DEV_BSIZE,
111 DEVSTAT_NO_ORDERED_TAGS,
112 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
113 DEVSTAT_PRIORITY_DISK);
114 sc->dev = disk_create(sc->unit, &sc->disk, &vkd_ops);
115 sc->dev->si_drv1 = sc;
116 sc->dev->si_iosize_max = 256 * 1024;
118 TAILQ_INIT(&sc->cotd_queue);
119 TAILQ_INIT(&sc->cotd_done);
120 sc->cotd = cothread_create(vkd_io_thread, vkd_io_intr, sc,
123 bzero(&info, sizeof(info));
124 info.d_media_blksize = DEV_BSIZE;
125 info.d_media_blocks = st.st_size / info.d_media_blksize;
128 info.d_ncylinders = 1;
129 info.d_secpertrack = info.d_media_blocks;
130 info.d_secpercyl = info.d_secpertrack * info.d_nheads;
132 disk_setdiskinfo(&sc->disk, &info);
136 SYSINIT(vkdisk, SI_SUB_DRIVERS, SI_ORDER_FIRST, vkdinit, NULL);
139 vkdopen(struct dev_open_args *ap)
141 struct vkd_softc *sc;
142 struct disk_info info;
146 dev = ap->a_head.a_dev;
148 if (fstat(sc->fd, &st) < 0 || st.st_size == 0)
152 bzero(&info, sizeof(info));
153 info.d_media_blksize = DEV_BSIZE;
154 info.d_media_blocks = st.st_size / info.d_media_blksize;
157 info.d_ncylinders = 1;
158 info.d_secpertrack = info.d_media_blocks;
159 info.d_secpercyl = info.d_secpertrack * info.d_nheads;
161 disk_setdiskinfo(&sc->disk, &info); */
166 vkdstrategy(struct dev_strategy_args *ap)
168 struct bio *bio = ap->a_bio;
169 struct vkd_softc *sc;
172 dev = ap->a_head.a_dev;
175 devstat_start_transaction(&sc->stats);
176 cothread_lock(sc->cotd, 0);
177 TAILQ_INSERT_TAIL(&sc->cotd_queue, bio, bio_act);
178 cothread_signal(sc->cotd);
179 cothread_unlock(sc->cotd, 0);
186 vkd_io_intr(cothread_t cotd)
188 struct vkd_softc *sc;
193 cothread_lock(cotd, 0);
194 while (!TAILQ_EMPTY(&sc->cotd_done)) {
195 bio = TAILQ_FIRST(&sc->cotd_done);
196 TAILQ_REMOVE(&sc->cotd_done, bio, bio_act);
197 devstat_end_transaction_buf(&sc->stats, bio->bio_buf);
200 cothread_unlock(sc->cotd, 0);
204 * WARNING! This runs as a cothread and has no access to mycpu nor can it
205 * make vkernel-specific calls other then cothread_*() calls.
207 * WARNING! A signal can occur and be discarded prior to our initial
208 * call to cothread_lock(). Process pending I/O before waiting.
212 vkd_io_thread(cothread_t cotd)
215 struct vkd_softc *sc = cotd->arg;
218 cothread_lock(cotd, 1);
221 while ((bio = TAILQ_FIRST(&sc->cotd_queue)) != NULL) {
222 TAILQ_REMOVE(&sc->cotd_queue, bio, bio_act);
223 cothread_unlock(cotd, 1);
225 cothread_lock(cotd, 1);
226 TAILQ_INSERT_TAIL(&sc->cotd_done, bio, bio_act);
234 cothread_wait(cotd); /* interlocks cothread lock */
237 cothread_unlock(cotd, 1);
242 vkd_doio(struct vkd_softc *sc, struct bio *bio)
244 struct buf *bp = bio->bio_buf;
249 n = pread(sc->fd, bp->b_data, bp->b_bcount, bio->bio_offset);
252 /* XXX HANDLE SHORT WRITE XXX */
253 n = pwrite(sc->fd, bp->b_data, bp->b_bcount, bio->bio_offset);
256 if (fsync(sc->fd) < 0)
262 panic("vkd: bad b_cmd %d", bp->b_cmd);
263 break; /* not reached */
265 if (n != bp->b_bcount) {
267 bp->b_flags |= B_ERROR;
269 bp->b_resid = bp->b_bcount - n;