DEVFS - Bring in Alex's GSOC kernel adjustments.
[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
191         sc = cotd->arg;
192
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);
198                 biodone(bio);
199         }
200         cothread_unlock(sc->cotd, 0);
201 }
202
203 /*
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.
206  *
207  * WARNING!  A signal can occur and be discarded prior to our initial
208  * call to cothread_lock().  Process pending I/O before waiting.
209  */
210 static
211 void
212 vkd_io_thread(cothread_t cotd)
213 {
214         struct bio *bio;
215         struct vkd_softc *sc = cotd->arg;
216         int count;
217
218         cothread_lock(cotd, 1);
219         for (;;) {
220                 count = 0;
221                 while ((bio = TAILQ_FIRST(&sc->cotd_queue)) != NULL) {
222                         TAILQ_REMOVE(&sc->cotd_queue, bio, bio_act);
223                         cothread_unlock(cotd, 1);
224                         vkd_doio(sc, bio);
225                         cothread_lock(cotd, 1);
226                         TAILQ_INSERT_TAIL(&sc->cotd_done, bio, bio_act);
227                         if (++count == 8) {
228                                 cothread_intr(cotd);
229                                 count = 0;
230                         }
231                 }
232                 if (count)
233                         cothread_intr(cotd);
234                 cothread_wait(cotd);    /* interlocks cothread lock */
235         }
236         /* NOT REACHED */
237         cothread_unlock(cotd, 1);
238 }
239
240 static
241 void
242 vkd_doio(struct vkd_softc *sc, struct bio *bio)
243 {
244         struct buf *bp = bio->bio_buf;
245         int n;
246
247         switch(bp->b_cmd) {
248         case BUF_CMD_READ:
249                 n = pread(sc->fd, bp->b_data, bp->b_bcount, bio->bio_offset);
250                 break;
251         case BUF_CMD_WRITE:
252                 /* XXX HANDLE SHORT WRITE XXX */
253                 n = pwrite(sc->fd, bp->b_data, bp->b_bcount, bio->bio_offset);
254                 break;
255         case BUF_CMD_FLUSH:
256                 if (fsync(sc->fd) < 0)
257                         n = -1;
258                 else
259                         n = bp->b_bcount;
260                 break;
261         default:
262                 panic("vkd: bad b_cmd %d", bp->b_cmd);
263                 break; /* not reached */
264         }
265         if (n != bp->b_bcount) {
266                 bp->b_error = EIO;
267                 bp->b_flags |= B_ERROR;
268         }
269         bp->b_resid = bp->b_bcount - n;
270 }
271