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.3 2007/03/16 13:41:40 swildner 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/disklabel.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         cdev_t dev;
62         int unit;
63         int fd;
64 };
65
66 #define CDEV_MAJOR      97
67
68 static d_strategy_t     vkdstrategy;
69 static d_open_t         vkdopen;
70
71 static struct dev_ops vkd_ops = {
72         { "vkd", CDEV_MAJOR, D_DISK },
73         .d_open =       vkdopen,
74         .d_close =      nullclose,
75         .d_read =       physread,
76         .d_write =      physwrite,
77         .d_strategy =   vkdstrategy,
78 };
79
80 static void
81 vkdinit(void *dummy __unused)
82 {
83         struct vkdisk_info *dsk;
84         struct vkd_softc *sc;
85         struct stat st;
86         int i;
87
88         KASSERT(DiskNum <= VKDISK_MAX, ("too many disks: %d\n", DiskNum));
89         
90         for (i = 0; i < DiskNum; i++) {
91
92                 /* check that the 'bus device' has been initialized */
93                 dsk = &DiskInfo[i];
94                 if (dsk == NULL)
95                         continue;
96                 if (dsk->fd < 0 || fstat(dsk->fd, &st) < 0)
97                         continue;
98
99                 /* and create a new device */
100                 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
101                 sc->unit = dsk->unit;
102                 sc->fd = dsk->fd;
103                 bioq_init(&sc->bio_queue);
104                 devstat_add_entry(&sc->stats, "vkd", sc->unit, DEV_BSIZE,
105                                   DEVSTAT_NO_ORDERED_TAGS,
106                                   DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
107                                   DEVSTAT_PRIORITY_DISK);
108                 sc->dev = disk_create(sc->unit, &sc->disk, 0, &vkd_ops);
109                 sc->dev->si_drv1 = sc;
110                 sc->dev->si_iosize_max = 256 * 1024;
111
112 #if 0
113                 dl = &sc->disk.d_label;
114                 bzero(dl, sizeof(*dl));
115                 dl->d_secsize = DEV_BSIZE;
116                 dl->d_nsectors = st.st_size / dl->d_secsize;
117                 dl->d_ntracks = 1;
118                 dl->d_secpercyl = dl->d_nsectors;
119                 dl->d_ncylinders = 1;
120 #endif
121
122         }
123 }
124
125 SYSINIT(vkdisk, SI_SUB_DRIVERS, SI_ORDER_FIRST, vkdinit, NULL);
126
127 static int
128 vkdopen(struct dev_open_args *ap)
129 {
130         struct vkd_softc *sc;
131         struct disklabel *dl;
132         struct stat st;
133         cdev_t dev;
134
135         dev = ap->a_head.a_dev;
136         sc = dev->si_drv1;
137         if (fstat(sc->fd, &st) < 0 || st.st_size == 0)
138                 return(ENXIO);
139
140         dl = &sc->disk.d_label;
141         bzero(dl, sizeof(*dl));
142         dl->d_secsize = DEV_BSIZE;
143         dl->d_nsectors = st.st_size / dl->d_secsize;
144         dl->d_ntracks = 1;
145         dl->d_secpercyl = dl->d_nsectors;
146         dl->d_ncylinders = 1;
147         dl->d_secperunit = dl->d_nsectors;
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 buf *bp;
156         struct vkd_softc *sc;
157         cdev_t dev;
158         int n;
159
160         dev = ap->a_head.a_dev;
161         sc = dev->si_drv1;
162
163         bioqdisksort(&sc->bio_queue, bio);
164         while ((bio = bioq_first(&sc->bio_queue)) != NULL) {
165                 bioq_remove(&sc->bio_queue, bio);
166                 bp = bio->bio_buf;
167
168                 devstat_start_transaction(&sc->stats);
169
170                 switch(bp->b_cmd) {
171                 case BUF_CMD_READ:
172                         lseek(sc->fd, bio->bio_offset, 0);
173                         n = read(sc->fd, bp->b_data, bp->b_bcount);
174                         break;
175                 case BUF_CMD_WRITE:
176                         /* XXX HANDLE SHORT WRITE XXX */
177                         lseek(sc->fd, bio->bio_offset, 0);
178                         n = write(sc->fd, bp->b_data, bp->b_bcount);
179                         break;
180                 default:
181                         panic("vkd: bad b_cmd %d", bp->b_cmd);
182                         break; /* not reached */
183                 }
184                 if (n != bp->b_bcount) {
185                         bp->b_error = EIO;
186                         bp->b_flags |= B_ERROR;
187                 }
188                         
189                 bp->b_resid = bp->b_bcount - n;
190                 devstat_end_transaction_buf(&sc->stats, bp);
191                 biodone(bio);
192         }
193         return(0);
194 }
195