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 = min(MAXPHYS,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;
140 info.d_serialno = kmalloc(SERNOLEN, M_TEMP, M_WAITOK | M_ZERO);
141 strlcpy(info.d_serialno, dsk->serno, SERNOLEN);
143 disk_setdiskinfo(&sc->disk, &info);
147 SYSINIT(vkdisk, SI_SUB_DRIVERS, SI_ORDER_FIRST, vkdinit, NULL);
150 vkdopen(struct dev_open_args *ap)
152 struct vkd_softc *sc;
153 /* struct disk_info info; */
157 dev = ap->a_head.a_dev;
159 if (fstat(sc->fd, &st) < 0)
163 * Devices may return a st_size of 0, try to use
166 if (st.st_size == 0) {
167 st.st_size = lseek(sc->fd, 0L, SEEK_END);
168 if (st.st_size == -1)
175 bzero(&info, sizeof(info));
176 info.d_media_blksize = DEV_BSIZE;
177 info.d_media_blocks = st.st_size / info.d_media_blksize;
180 info.d_ncylinders = 1;
181 info.d_secpertrack = info.d_media_blocks;
182 info.d_secpercyl = info.d_secpertrack * info.d_nheads;
184 disk_setdiskinfo(&sc->disk, &info); */
189 vkdstrategy(struct dev_strategy_args *ap)
191 struct bio *bio = ap->a_bio;
192 struct vkd_softc *sc;
195 dev = ap->a_head.a_dev;
198 devstat_start_transaction(&sc->stats);
199 cothread_lock(sc->cotd, 0);
200 TAILQ_INSERT_TAIL(&sc->cotd_queue, bio, bio_act);
201 cothread_signal(sc->cotd);
202 cothread_unlock(sc->cotd, 0);
209 vkd_io_intr(cothread_t cotd)
211 struct vkd_softc *sc;
213 TAILQ_HEAD(, bio) tmpq;
218 * We can't call back into the kernel while holding cothread!
221 cothread_lock(cotd, 0);
222 while ((bio = TAILQ_FIRST(&sc->cotd_done)) != NULL) {
223 TAILQ_REMOVE(&sc->cotd_done, bio, bio_act);
224 TAILQ_INSERT_TAIL(&tmpq, bio, bio_act);
226 cothread_unlock(cotd, 0);
228 while ((bio = TAILQ_FIRST(&tmpq)) != NULL) {
229 TAILQ_REMOVE(&tmpq, bio, bio_act);
230 devstat_end_transaction_buf(&sc->stats, bio->bio_buf);
236 * WARNING! This runs as a cothread and has no access to mycpu nor can it
237 * make vkernel-specific calls other then cothread_*() calls.
239 * WARNING! A signal can occur and be discarded prior to our initial
240 * call to cothread_lock(). Process pending I/O before waiting.
244 vkd_io_thread(cothread_t cotd)
247 struct vkd_softc *sc = cotd->arg;
250 cothread_lock(cotd, 1);
253 while ((bio = TAILQ_FIRST(&sc->cotd_queue)) != NULL) {
254 TAILQ_REMOVE(&sc->cotd_queue, bio, bio_act);
255 cothread_unlock(cotd, 1);
257 cothread_lock(cotd, 1);
258 TAILQ_INSERT_TAIL(&sc->cotd_done, bio, bio_act);
266 cothread_wait(cotd); /* interlocks cothread lock */
269 cothread_unlock(cotd, 1);
274 vkd_doio(struct vkd_softc *sc, struct bio *bio)
276 struct buf *bp = bio->bio_buf;
281 n = pread(sc->fd, bp->b_data, bp->b_bcount, bio->bio_offset);
284 /* XXX HANDLE SHORT WRITE XXX */
285 n = pwrite(sc->fd, bp->b_data, bp->b_bcount, bio->bio_offset);
288 if (fsync(sc->fd) < 0)
294 panic("vkd: bad b_cmd %d", bp->b_cmd);
295 break; /* not reached */
297 if (n != bp->b_bcount) {
299 bp->b_flags |= B_ERROR;
301 bp->b_resid = bp->b_bcount - n;