Add a -c file option to the vkernel to specify CD images. The first -c or -r
[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.6 2007/05/25 02:21:14 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/buf.h>
47 #include <sys/devicestat.h>
48 #include <sys/disk.h>
49 #include <machine/md_var.h>
50
51 #include <sys/buf2.h>
52
53 #include <sys/stat.h>
54 #include <unistd.h>
55
56 struct vkd_softc {
57         struct bio_queue_head bio_queue;
58         struct devstat stats;
59         struct disk disk;
60         cdev_t dev;
61         int unit;
62         int fd;
63 };
64
65 #define CDEV_MAJOR      97
66
67 static d_strategy_t     vkdstrategy;
68 static d_open_t         vkdopen;
69
70 static struct dev_ops vkd_ops = {
71         { "vkd", CDEV_MAJOR, D_DISK },
72         .d_open =       vkdopen,
73         .d_close =      nullclose,
74         .d_read =       physread,
75         .d_write =      physwrite,
76         .d_strategy =   vkdstrategy,
77 };
78
79 static void
80 vkdinit(void *dummy __unused)
81 {
82         struct vkdisk_info *dsk;
83         struct vkd_softc *sc;
84         struct stat st;
85         int i;
86
87         for (i = 0; i < DiskNum; i++) {
88                 /* check that the 'bus device' has been initialized */
89                 dsk = &DiskInfo[i];
90                 if (dsk == NULL || dsk->type != VKD_DISK)
91                         continue;
92                 if (dsk->fd < 0 || fstat(dsk->fd, &st) < 0)
93                         continue;
94
95                 /* and create a new device */
96                 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
97                 sc->unit = dsk->unit;
98                 sc->fd = dsk->fd;
99                 bioq_init(&sc->bio_queue);
100                 devstat_add_entry(&sc->stats, "vkd", sc->unit, DEV_BSIZE,
101                                   DEVSTAT_NO_ORDERED_TAGS,
102                                   DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
103                                   DEVSTAT_PRIORITY_DISK);
104                 sc->dev = disk_create(sc->unit, &sc->disk, &vkd_ops);
105                 sc->dev->si_drv1 = sc;
106                 sc->dev->si_iosize_max = 256 * 1024;
107         }
108 }
109
110 SYSINIT(vkdisk, SI_SUB_DRIVERS, SI_ORDER_FIRST, vkdinit, NULL);
111
112 static int
113 vkdopen(struct dev_open_args *ap)
114 {
115         struct vkd_softc *sc;
116         struct disk_info info;
117         struct stat st;
118         cdev_t dev;
119
120         dev = ap->a_head.a_dev;
121         sc = dev->si_drv1;
122         if (fstat(sc->fd, &st) < 0 || st.st_size == 0)
123                 return(ENXIO);
124
125         bzero(&info, sizeof(info));
126         info.d_media_blksize = DEV_BSIZE;
127         info.d_media_blocks = st.st_size / info.d_media_blksize;
128
129         info.d_nheads = 1;
130         info.d_ncylinders = 1;
131         info.d_secpertrack = info.d_media_blocks;
132         info.d_secpercyl = info.d_secpertrack * info.d_nheads;
133
134         disk_setdiskinfo(&sc->disk, &info);
135         return(0);
136 }
137
138 static int
139 vkdstrategy(struct dev_strategy_args *ap)
140 {
141         struct bio *bio = ap->a_bio;
142         struct buf *bp;
143         struct vkd_softc *sc;
144         cdev_t dev;
145         int n;
146
147         dev = ap->a_head.a_dev;
148         sc = dev->si_drv1;
149
150         bioqdisksort(&sc->bio_queue, bio);
151         while ((bio = bioq_first(&sc->bio_queue)) != NULL) {
152                 bioq_remove(&sc->bio_queue, bio);
153                 bp = bio->bio_buf;
154
155                 devstat_start_transaction(&sc->stats);
156
157                 switch(bp->b_cmd) {
158                 case BUF_CMD_READ:
159                         lseek(sc->fd, bio->bio_offset, 0);
160                         n = read(sc->fd, bp->b_data, bp->b_bcount);
161                         break;
162                 case BUF_CMD_WRITE:
163                         /* XXX HANDLE SHORT WRITE XXX */
164                         lseek(sc->fd, bio->bio_offset, 0);
165                         n = write(sc->fd, bp->b_data, bp->b_bcount);
166                         break;
167                 default:
168                         panic("vkd: bad b_cmd %d", bp->b_cmd);
169                         break; /* not reached */
170                 }
171                 if (n != bp->b_bcount) {
172                         bp->b_error = EIO;
173                         bp->b_flags |= B_ERROR;
174                 }
175                         
176                 bp->b_resid = bp->b_bcount - n;
177                 devstat_end_transaction_buf(&sc->stats, bp);
178                 biodone(bio);
179         }
180         return(0);
181 }
182