Merge from vendor branch LIBARCHIVE:
[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.7 2007/07/02 17:15:10 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/md_var.h>
51
52 #include <sys/buf2.h>
53
54 #include <sys/stat.h>
55 #include <unistd.h>
56
57 struct vkd_softc {
58         struct bio_queue_head bio_queue;
59         struct devstat stats;
60         struct disk disk;
61         struct spinlock spin;
62         thread_t iotd;          /* dedicated io thread */
63         cdev_t dev;
64         int unit;
65         int fd;
66 };
67
68 #define CDEV_MAJOR      97
69
70 static void vkd_io_thread(void *arg);
71 static void vkd_doio(struct vkd_softc *sc, struct bio *bio);
72
73 static d_strategy_t     vkdstrategy;
74 static d_open_t         vkdopen;
75
76 static struct dev_ops vkd_ops = {
77         { "vkd", CDEV_MAJOR, D_DISK },
78         .d_open =       vkdopen,
79         .d_close =      nullclose,
80         .d_read =       physread,
81         .d_write =      physwrite,
82         .d_strategy =   vkdstrategy,
83 };
84
85 static void
86 vkdinit(void *dummy __unused)
87 {
88         struct vkdisk_info *dsk;
89         struct vkd_softc *sc;
90         struct stat st;
91         int i;
92
93         for (i = 0; i < DiskNum; i++) {
94                 /* check that the 'bus device' has been initialized */
95                 dsk = &DiskInfo[i];
96                 if (dsk == NULL || dsk->type != VKD_DISK)
97                         continue;
98                 if (dsk->fd < 0 || fstat(dsk->fd, &st) < 0)
99                         continue;
100
101                 /* and create a new device */
102                 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
103                 sc->unit = dsk->unit;
104                 sc->fd = dsk->fd;
105                 spin_init(&sc->spin);
106                 bioq_init(&sc->bio_queue);
107                 devstat_add_entry(&sc->stats, "vkd", sc->unit, DEV_BSIZE,
108                                   DEVSTAT_NO_ORDERED_TAGS,
109                                   DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
110                                   DEVSTAT_PRIORITY_DISK);
111                 sc->dev = disk_create(sc->unit, &sc->disk, &vkd_ops);
112                 sc->dev->si_drv1 = sc;
113                 sc->dev->si_iosize_max = 256 * 1024;
114                 if (ncpus > 2) {
115                         int xcpu = ncpus - 1;
116                         lwkt_create(vkd_io_thread, sc, &sc->iotd, NULL, 
117                                     0, xcpu, "vkd%d-io", sc->unit);
118                         usched_mastermask &= ~(1 << xcpu);
119                 }
120         }
121 }
122
123 SYSINIT(vkdisk, SI_SUB_DRIVERS, SI_ORDER_FIRST, vkdinit, NULL);
124
125 static int
126 vkdopen(struct dev_open_args *ap)
127 {
128         struct vkd_softc *sc;
129         struct disk_info info;
130         struct stat st;
131         cdev_t dev;
132
133         dev = ap->a_head.a_dev;
134         sc = dev->si_drv1;
135         if (fstat(sc->fd, &st) < 0 || st.st_size == 0)
136                 return(ENXIO);
137
138         bzero(&info, sizeof(info));
139         info.d_media_blksize = DEV_BSIZE;
140         info.d_media_blocks = st.st_size / info.d_media_blksize;
141
142         info.d_nheads = 1;
143         info.d_ncylinders = 1;
144         info.d_secpertrack = info.d_media_blocks;
145         info.d_secpercyl = info.d_secpertrack * info.d_nheads;
146
147         disk_setdiskinfo(&sc->disk, &info);
148         return(0);
149 }
150
151 static int
152 vkdstrategy(struct dev_strategy_args *ap)
153 {
154         struct bio *bio = ap->a_bio;
155         struct vkd_softc *sc;
156         cdev_t dev;
157
158         dev = ap->a_head.a_dev;
159         sc = dev->si_drv1;
160
161         if (sc->iotd) {
162                 spin_lock_wr(&sc->spin);
163                 bioqdisksort(&sc->bio_queue, bio);
164                 spin_unlock_wr(&sc->spin);
165                 wakeup(sc->iotd);
166         } else {
167                 bioqdisksort(&sc->bio_queue, bio);
168                 while ((bio = bioq_first(&sc->bio_queue)) != NULL) {
169                         bioq_remove(&sc->bio_queue, bio);
170                         vkd_doio(sc, bio);
171                         biodone(bio);
172                 }
173         }
174         return(0);
175 }
176
177 static
178 void
179 vkd_io_thread(void *arg)
180 {
181         struct bio *bio;
182         struct vkd_softc *sc;
183         int count = 0;
184
185         rel_mplock();
186         sc = arg;
187
188         kprintf("vkd%d I/O helper on cpu %d\n", sc->unit, mycpu->gd_cpuid);
189
190         spin_lock_wr(&sc->spin);
191         for (;;) {
192                 while ((bio = bioq_first(&sc->bio_queue)) != NULL) {
193                         bioq_remove(&sc->bio_queue, bio);
194                         spin_unlock_wr(&sc->spin);
195                         vkd_doio(sc, bio);
196                         get_mplock();
197                         biodone(bio);
198                         rel_mplock();
199                         if ((++count & 3) == 0)
200                                 lwkt_switch();
201                         spin_lock_wr(&sc->spin);
202                 }
203                 msleep(sc->iotd, &sc->spin, 0, "bioq", 0);
204         }
205         /* not reached */
206         spin_unlock_wr(&sc->spin);
207 }
208
209 static
210 void
211 vkd_doio(struct vkd_softc *sc, struct bio *bio)
212 {
213         struct buf *bp = bio->bio_buf;
214         int n;
215
216         devstat_start_transaction(&sc->stats);
217
218         switch(bp->b_cmd) {
219         case BUF_CMD_READ:
220                 n = pread(sc->fd, bp->b_data, bp->b_bcount, bio->bio_offset);
221                 break;
222         case BUF_CMD_WRITE:
223                 /* XXX HANDLE SHORT WRITE XXX */
224                 n = pwrite(sc->fd, bp->b_data, bp->b_bcount, bio->bio_offset);
225                 break;
226         default:
227                 panic("vkd: bad b_cmd %d", bp->b_cmd);
228                 break; /* not reached */
229         }
230         if (n != bp->b_bcount) {
231                 bp->b_error = EIO;
232                 bp->b_flags |= B_ERROR;
233         }
234                 
235         bp->b_resid = bp->b_bcount - n;
236         devstat_end_transaction_buf(&sc->stats, bp);
237 }
238