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
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
47 #include <sys/devicestat.h>
49 #include <machine/cothread.h>
50 #include <machine/md_var.h>
58 struct bio_queue_head bio_queue;
62 TAILQ_HEAD(, bio) cotd_queue;
63 TAILQ_HEAD(, bio) cotd_done;
69 static void vkd_io_thread(cothread_t cotd);
70 static void vkd_io_intr(cothread_t cotd);
71 static void vkd_doio(struct vkd_softc *sc, struct bio *bio);
73 static d_strategy_t vkdstrategy;
74 static d_open_t vkdopen;
76 static struct dev_ops vkd_ops = {
82 .d_strategy = vkdstrategy,
86 vkdinit(void *dummy __unused)
88 struct vkdisk_info *dsk;
90 struct disk_info info;
94 for (i = 0; i < DiskNum; i++) {
95 /* check that the 'bus device' has been initialized */
97 if (dsk == NULL || dsk->type != VKD_DISK)
99 if (dsk->fd < 0 || fstat(dsk->fd, &st) < 0)
103 * Devices may return a st_size of 0, try to use
106 if (st.st_size == 0) {
107 st.st_size = lseek(dsk->fd, 0L, SEEK_END);
108 if (st.st_size == -1)
112 /* and create a new device */
113 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
114 sc->unit = dsk->unit;
116 bioq_init(&sc->bio_queue);
117 devstat_add_entry(&sc->stats, "vkd", sc->unit, DEV_BSIZE,
118 DEVSTAT_NO_ORDERED_TAGS,
119 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
120 DEVSTAT_PRIORITY_DISK);
121 sc->dev = disk_create(sc->unit, &sc->disk, &vkd_ops);
122 sc->dev->si_drv1 = sc;
123 sc->dev->si_iosize_max = 256 * 1024;
125 TAILQ_INIT(&sc->cotd_queue);
126 TAILQ_INIT(&sc->cotd_done);
127 sc->cotd = cothread_create(vkd_io_thread, vkd_io_intr, sc,
130 bzero(&info, sizeof(info));
131 info.d_media_blksize = DEV_BSIZE;
132 info.d_media_blocks = st.st_size / info.d_media_blksize;
135 info.d_ncylinders = 1;
136 info.d_secpertrack = info.d_media_blocks;
137 info.d_secpercyl = info.d_secpertrack * info.d_nheads;
139 disk_setdiskinfo(&sc->disk, &info);
143 SYSINIT(vkdisk, SI_SUB_DRIVERS, SI_ORDER_FIRST, vkdinit, NULL);
146 vkdopen(struct dev_open_args *ap)
148 struct vkd_softc *sc;
149 /* struct disk_info info; */
153 dev = ap->a_head.a_dev;
155 if (fstat(sc->fd, &st) < 0)
159 * Devices may return a st_size of 0, try to use
162 if (st.st_size == 0) {
163 st.st_size = lseek(sc->fd, 0L, SEEK_END);
164 if (st.st_size == -1)
171 bzero(&info, sizeof(info));
172 info.d_media_blksize = DEV_BSIZE;
173 info.d_media_blocks = st.st_size / info.d_media_blksize;
176 info.d_ncylinders = 1;
177 info.d_secpertrack = info.d_media_blocks;
178 info.d_secpercyl = info.d_secpertrack * info.d_nheads;
180 disk_setdiskinfo(&sc->disk, &info); */
185 vkdstrategy(struct dev_strategy_args *ap)
187 struct bio *bio = ap->a_bio;
188 struct vkd_softc *sc;
191 dev = ap->a_head.a_dev;
194 devstat_start_transaction(&sc->stats);
195 cothread_lock(sc->cotd, 0);
196 TAILQ_INSERT_TAIL(&sc->cotd_queue, bio, bio_act);
197 cothread_signal(sc->cotd);
198 cothread_unlock(sc->cotd, 0);
205 vkd_io_intr(cothread_t cotd)
207 struct vkd_softc *sc;
209 TAILQ_HEAD(, bio) tmpq;
214 * We can't call back into the kernel while holding cothread!
217 cothread_lock(cotd, 0);
218 while ((bio = TAILQ_FIRST(&sc->cotd_done)) != NULL) {
219 TAILQ_REMOVE(&sc->cotd_done, bio, bio_act);
220 TAILQ_INSERT_TAIL(&tmpq, bio, bio_act);
222 cothread_unlock(cotd, 0);
224 while ((bio = TAILQ_FIRST(&tmpq)) != NULL) {
225 TAILQ_REMOVE(&tmpq, bio, bio_act);
226 devstat_end_transaction_buf(&sc->stats, bio->bio_buf);
232 * WARNING! This runs as a cothread and has no access to mycpu nor can it
233 * make vkernel-specific calls other then cothread_*() calls.
235 * WARNING! A signal can occur and be discarded prior to our initial
236 * call to cothread_lock(). Process pending I/O before waiting.
240 vkd_io_thread(cothread_t cotd)
243 struct vkd_softc *sc = cotd->arg;
246 cothread_lock(cotd, 1);
249 while ((bio = TAILQ_FIRST(&sc->cotd_queue)) != NULL) {
250 TAILQ_REMOVE(&sc->cotd_queue, bio, bio_act);
251 cothread_unlock(cotd, 1);
253 cothread_lock(cotd, 1);
254 TAILQ_INSERT_TAIL(&sc->cotd_done, bio, bio_act);
262 cothread_wait(cotd); /* interlocks cothread lock */
265 cothread_unlock(cotd, 1);
270 vkd_doio(struct vkd_softc *sc, struct bio *bio)
272 struct buf *bp = bio->bio_buf;
277 n = pread(sc->fd, bp->b_data, bp->b_bcount, bio->bio_offset);
280 /* XXX HANDLE SHORT WRITE XXX */
281 n = pwrite(sc->fd, bp->b_data, bp->b_bcount, bio->bio_offset);
284 if (fsync(sc->fd) < 0)
290 panic("vkd: bad b_cmd %d", bp->b_cmd);
291 break; /* not reached */
293 if (n != bp->b_bcount) {
295 bp->b_flags |= B_ERROR;
297 bp->b_resid = bp->b_bcount - n;