70f225f1328604f69827287bc5963a443fa7f0e0
[dragonfly.git] / sys / dev / virtual / disk / vdisk.c
1 /*
2  * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
16  *    distribution.
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.
20  * 
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
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/dev/virtual/disk/vdisk.c,v 1.8 2008/03/20 02:14:56 dillon Exp $
35  */
36
37 /*
38  * Virtual disk driver
39  */
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>
45 #include <sys/conf.h>
46 #include <sys/bus.h>
47 #include <sys/buf.h>
48 #include <sys/devicestat.h>
49 #include <sys/disk.h>
50 #include <machine/cothread.h>
51 #include <machine/md_var.h>
52
53 #include <sys/buf2.h>
54
55 #include <sys/stat.h>
56 #include <unistd.h>
57
58 struct vkd_softc {
59         struct bio_queue_head bio_queue;
60         struct devstat stats;
61         struct disk disk;
62         cothread_t      cotd;
63         TAILQ_HEAD(, bio) cotd_queue;
64         TAILQ_HEAD(, bio) cotd_done;
65         cdev_t dev;
66         int unit;
67         int fd;
68 };
69
70 #define CDEV_MAJOR      97
71
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);
75
76 static d_strategy_t     vkdstrategy;
77 static d_open_t         vkdopen;
78
79 static struct dev_ops vkd_ops = {
80         { "vkd", CDEV_MAJOR, D_DISK },
81         .d_open =       vkdopen,
82         .d_close =      nullclose,
83         .d_read =       physread,
84         .d_write =      physwrite,
85         .d_strategy =   vkdstrategy,
86 };
87
88 static void
89 vkdinit(void *dummy __unused)
90 {
91         struct vkdisk_info *dsk;
92         struct vkd_softc *sc;
93         struct disk_info info;
94         struct stat st;
95         int i;
96
97         for (i = 0; i < DiskNum; i++) {
98                 /* check that the 'bus device' has been initialized */
99                 dsk = &DiskInfo[i];
100                 if (dsk == NULL || dsk->type != VKD_DISK)
101                         continue;
102                 if (dsk->fd < 0 || fstat(dsk->fd, &st) < 0)
103                         continue;
104
105                 /* and create a new device */
106                 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
107                 sc->unit = dsk->unit;
108                 sc->fd = dsk->fd;
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;
117
118                 TAILQ_INIT(&sc->cotd_queue);
119                 TAILQ_INIT(&sc->cotd_done);
120                 sc->cotd = cothread_create(vkd_io_thread, vkd_io_intr, sc, 
121                                            "vkd");
122
123                 bzero(&info, sizeof(info));
124                 info.d_media_blksize = DEV_BSIZE;
125                 info.d_media_blocks = st.st_size / info.d_media_blksize;
126
127                 info.d_nheads = 1;
128                 info.d_ncylinders = 1;
129                 info.d_secpertrack = info.d_media_blocks;
130                 info.d_secpercyl = info.d_secpertrack * info.d_nheads;
131
132                 disk_setdiskinfo(&sc->disk, &info);
133         }
134 }
135
136 SYSINIT(vkdisk, SI_SUB_DRIVERS, SI_ORDER_FIRST, vkdinit, NULL);
137
138 static int
139 vkdopen(struct dev_open_args *ap)
140 {
141         struct vkd_softc *sc;
142         /* struct disk_info info; */
143         struct stat st;
144         cdev_t dev;
145
146         dev = ap->a_head.a_dev;
147         sc = dev->si_drv1;
148         if (fstat(sc->fd, &st) < 0 || st.st_size == 0)
149                 return(ENXIO);
150
151 /*
152         bzero(&info, sizeof(info));
153         info.d_media_blksize = DEV_BSIZE;
154         info.d_media_blocks = st.st_size / info.d_media_blksize;
155
156         info.d_nheads = 1;
157         info.d_ncylinders = 1;
158         info.d_secpertrack = info.d_media_blocks;
159         info.d_secpercyl = info.d_secpertrack * info.d_nheads;
160
161         disk_setdiskinfo(&sc->disk, &info); */
162         return(0);
163 }
164
165 static int
166 vkdstrategy(struct dev_strategy_args *ap)
167 {
168         struct bio *bio = ap->a_bio;
169         struct vkd_softc *sc;
170         cdev_t dev;
171
172         dev = ap->a_head.a_dev;
173         sc = dev->si_drv1;
174
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);
180
181         return(0);
182 }
183
184 static
185 void
186 vkd_io_intr(cothread_t cotd)
187 {
188         struct vkd_softc *sc;
189         struct bio *bio;
190         TAILQ_HEAD(, bio) tmpq;
191
192         sc = cotd->arg;
193
194         /*
195          * We can't call back into the kernel while holding cothread!
196          */
197         TAILQ_INIT(&tmpq);
198         cothread_lock(cotd, 0);
199         while ((bio = TAILQ_FIRST(&sc->cotd_done)) != NULL) {
200                 TAILQ_REMOVE(&sc->cotd_done, bio, bio_act);
201                 TAILQ_INSERT_TAIL(&tmpq, bio, bio_act);
202         }
203         cothread_unlock(cotd, 0);
204
205         while ((bio = TAILQ_FIRST(&tmpq)) != NULL) {
206                 TAILQ_REMOVE(&tmpq, bio, bio_act);
207                 devstat_end_transaction_buf(&sc->stats, bio->bio_buf);
208                 biodone(bio);
209         }
210 }
211
212 /*
213  * WARNING!  This runs as a cothread and has no access to mycpu nor can it
214  * make vkernel-specific calls other then cothread_*() calls.
215  *
216  * WARNING!  A signal can occur and be discarded prior to our initial
217  * call to cothread_lock().  Process pending I/O before waiting.
218  */
219 static
220 void
221 vkd_io_thread(cothread_t cotd)
222 {
223         struct bio *bio;
224         struct vkd_softc *sc = cotd->arg;
225         int count;
226
227         cothread_lock(cotd, 1);
228         for (;;) {
229                 count = 0;
230                 while ((bio = TAILQ_FIRST(&sc->cotd_queue)) != NULL) {
231                         TAILQ_REMOVE(&sc->cotd_queue, bio, bio_act);
232                         cothread_unlock(cotd, 1);
233                         vkd_doio(sc, bio);
234                         cothread_lock(cotd, 1);
235                         TAILQ_INSERT_TAIL(&sc->cotd_done, bio, bio_act);
236                         if (++count == 8) {
237                                 cothread_intr(cotd);
238                                 count = 0;
239                         }
240                 }
241                 if (count)
242                         cothread_intr(cotd);
243                 cothread_wait(cotd);    /* interlocks cothread lock */
244         }
245         /* NOT REACHED */
246         cothread_unlock(cotd, 1);
247 }
248
249 static
250 void
251 vkd_doio(struct vkd_softc *sc, struct bio *bio)
252 {
253         struct buf *bp = bio->bio_buf;
254         int n;
255
256         switch(bp->b_cmd) {
257         case BUF_CMD_READ:
258                 n = pread(sc->fd, bp->b_data, bp->b_bcount, bio->bio_offset);
259                 break;
260         case BUF_CMD_WRITE:
261                 /* XXX HANDLE SHORT WRITE XXX */
262                 n = pwrite(sc->fd, bp->b_data, bp->b_bcount, bio->bio_offset);
263                 break;
264         case BUF_CMD_FLUSH:
265                 if (fsync(sc->fd) < 0)
266                         n = -1;
267                 else
268                         n = bp->b_bcount;
269                 break;
270         default:
271                 panic("vkd: bad b_cmd %d", bp->b_cmd);
272                 break; /* not reached */
273         }
274         if (n != bp->b_bcount) {
275                 bp->b_error = EIO;
276                 bp->b_flags |= B_ERROR;
277         }
278         bp->b_resid = bp->b_bcount - n;
279 }
280