2 * Copyright (c) 2002 Poul-Henning Kamp
3 * Copyright (c) 2002 Networks Associates Technology, Inc.
6 * This software was developed for the FreeBSD Project by Poul-Henning Kamp
7 * and NAI Labs, the Security Research Division of Network Associates, Inc.
8 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
9 * DARPA CHATS research program.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the authors may not be used to endorse or promote
20 * products derived from this software without specific prior written
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
48 #include <sys/diskslice.h>
49 #include <sys/disklabel.h>
50 #include <sys/malloc.h>
51 #include <sys/sysctl.h>
52 #include <sys/stdint.h>
53 #include <machine/md_var.h>
57 #include <sys/mutex.h>
58 #include <geom/geom.h>
60 static g_access_t g_disk_access;
62 struct g_class g_disk_class = {
69 DECLARE_GEOM_CLASS(g_disk_class, g_disk);
72 g_disk_access(struct g_provider *pp, int r, int w, int e)
78 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
86 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
88 error = devsw(dev)->d_open(dev, 3, 0, NULL);
90 printf("Opened disk %s -> %d\n", pp->name, error);
92 pp->mediasize = dp->d_mediasize;
93 pp->sectorsize = dp->d_sectorsize;
94 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
96 error = devsw(dev)->d_close(dev, 3, 0, NULL);
98 printf("Closed disk %s -> %d\n", pp->name, error);
107 g_disk_kerneldump(struct bio *bp, struct disk *dp)
110 struct g_kerneldump *gkd;
111 struct dumperinfo di;
114 gkd = (struct g_kerneldump*)bp->bio_data;
115 gp = bp->bio_to->geom;
116 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
117 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
118 di.dumper = (dumper_t *)dp->d_devsw->d_dump;
120 di.blocksize = dp->d_sectorsize;
121 di.mediaoffset = gkd->offset;
122 di.mediasize = gkd->length;
123 error = set_dumper(&di);
124 g_io_deliver(bp, error);
128 g_disk_done(struct bio *bp)
132 bp->bio_completed = bp->bio_length - bp->bio_resid;
138 g_disk_start(struct bio *bp)
146 dp = bp->bio_to->geom->softc;
149 switch(bp->bio_cmd) {
151 if (!(devsw(dev)->d_flags & D_CANFREE)) {
158 bp2 = g_clone_bio(bp);
159 bp2->bio_done = g_disk_done;
160 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT;
161 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
162 bp2->bio_bcount = bp2->bio_length;
165 devsw(dev)->d_strategy(bp2);
169 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors))
171 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
173 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
175 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
176 g_disk_kerneldump(bp, dp);
177 else if (!strcmp(bp->bio_attribute, "GEOM::ioctl") &&
178 bp->bio_length == sizeof *gio) {
179 gio = (struct g_ioctl *)bp->bio_data;
180 gio->func = devsw(dp->d_dev)->d_ioctl;
181 gio->dev = dp->d_dev;
187 if (!strcmp(bp->bio_attribute, "GEOM::ioctl") &&
188 bp->bio_length == sizeof *gio) {
189 gio = (struct g_ioctl *)bp->bio_data;
190 gio->func = devsw(dp->d_dev)->d_ioctl;
191 gio->dev = dp->d_dev;
200 if (error != EJUSTRETURN)
201 g_io_deliver(bp, error);
206 g_disk_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
211 if (indent == NULL) {
212 sbuf_printf(sb, " hd %u", dp->d_fwheads);
213 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
217 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
218 indent, dp->d_fwheads);
219 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
220 indent, dp->d_fwsectors);
225 g_disk_create(void *arg)
228 struct g_provider *pp;
233 gp = g_new_geomf(&g_disk_class, dev->si_name);
234 gp->start = g_disk_start;
235 gp->access = g_disk_access;
236 gp->softc = dev->si_disk;
237 gp->dumpconf = g_disk_dumpconf;
238 dev->si_disk->d_softc = gp;
239 pp = g_new_providerf(gp, "%s", gp->name);
240 pp->mediasize = dev->si_disk->d_mediasize;
241 pp->sectorsize = dev->si_disk->d_sectorsize;
242 g_error_provider(pp, 0);
244 printf("GEOM: new disk %s\n", gp->name);
250 disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, struct cdevsw *proto)
254 dev = g_malloc(sizeof *dev, M_WAITOK | M_ZERO);
256 dp->d_devsw = cdevsw;
257 dev->si_devsw = cdevsw;
259 dev->si_udev = dkmakeminor(unit, WHOLE_DISK_SLICE, RAW_PART);
260 sprintf(dev->si_name, "%s%d", cdevsw->d_name, unit);
261 g_call_me(g_disk_create, dev);
265 void disk_dev_synth(dev_t dev);
268 disk_dev_synth(dev_t dev)
275 disk_destroy(dev_t dev)
283 gp->flags |= G_GEOM_WITHER;
284 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO);
288 disk_invalidate (struct disk *disk)
293 g_kern_disks(void *p)
302 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
303 sbuf_printf(sb, "%s%s", sp, gp->name);
311 sysctl_disks(SYSCTL_HANDLER_ARGS)
316 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
318 g_call_me(g_kern_disks, sb);
320 tsleep(sb, PZERO, "kern.disks", hz);
321 } while(!sbuf_done(sb));
322 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
327 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
328 sysctl_disks, "A", "names of available disks");