2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 * $DragonFly: src/sys/dev/virtual/disk/vdisk.c,v 1.1 2007/01/07 02:41:20 dillon Exp $
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>
47 #include <sys/devicestat.h>
48 #include <sys/disklabel.h>
50 #include <machine/md_var.h>
58 struct bio_queue_head bio_queue;
70 static d_strategy_t vdstrategy;
71 static d_open_t vdopen;
73 static struct dev_ops vd_ops = {
74 { "vd", CDEV_MAJOR, D_DISK },
79 .d_strategy = vdstrategy,
83 vdinit(void *dummy __unused)
88 if (RootImageFd < 0 || fstat(RootImageFd, &st) < 0)
91 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK|M_ZERO);
94 bioq_init(&sc->bio_queue);
95 devstat_add_entry(&sc->stats, "vd", sc->unit, DEV_BSIZE,
96 DEVSTAT_NO_ORDERED_TAGS,
97 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
98 DEVSTAT_PRIORITY_DISK);
99 sc->dev = disk_create(sc->unit, &sc->disk, 0, &vd_ops);
100 sc->dev->si_drv1 = sc;
101 sc->dev->si_iosize_max = 256 * 1024;
103 dl = &sc->disk.d_label;
104 bzero(dl, sizeof(*dl));
105 dl->d_secsize = DEV_BSIZE;
106 dl->d_nsectors = st.st_size / dl->d_secsize;
108 dl->d_secpercyl = dl->d_nsectors;
109 dl->d_ncylinders = 1;
113 SYSINIT(vdisk, SI_SUB_DRIVERS, SI_ORDER_FIRST, vdinit, NULL);
116 vdopen(struct dev_open_args *ap)
119 struct disklabel *dl;
123 dev = ap->a_head.a_dev;
127 if (fstat(sc->fd, &st) < 0 || st.st_size == 0)
130 dl = &sc->disk.d_label;
131 bzero(dl, sizeof(*dl));
132 dl->d_secsize = DEV_BSIZE;
133 dl->d_nsectors = st.st_size / dl->d_secsize;
135 dl->d_secpercyl = dl->d_nsectors;
136 dl->d_ncylinders = 1;
137 dl->d_secperunit = dl->d_nsectors;
142 vdstrategy(struct dev_strategy_args *ap)
144 struct bio *bio = ap->a_bio;
150 dev = ap->a_head.a_dev;
153 bioqdisksort(&sc->bio_queue, bio);
154 while ((bio = bioq_first(&sc->bio_queue)) != NULL) {
155 bioq_remove(&sc->bio_queue, bio);
158 devstat_start_transaction(&sc->stats);
162 lseek(sc->fd, bio->bio_offset, 0);
163 n = read(sc->fd, bp->b_data, bp->b_bcount);
166 /* XXX HANDLE SHORT WRITE XXX */
167 lseek(sc->fd, bio->bio_offset, 0);
168 n = write(sc->fd, bp->b_data, bp->b_bcount);
171 panic("vd: bad b_cmd %d", bp->b_cmd);
172 break; /* not reached */
174 if (n != bp->b_bcount) {
176 bp->b_flags |= B_ERROR;
179 bp->b_resid = bp->b_bcount - n;
180 devstat_end_transaction_buf(&sc->stats, bp);