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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
47 #include <sys/ctype.h>
48 #include <sys/fcntl.h>
49 #include <sys/malloc.h>
50 #include <sys/sysctl.h>
51 #include <sys/devicestat.h>
52 #include <machine/md_var.h>
55 #include <sys/mutex.h>
56 #include <geom/geom.h>
57 #include <geom/geom_disk.h>
58 #include <geom/geom_int.h>
60 static struct mtx g_disk_done_mtx;
62 static g_access_t g_disk_access;
63 static g_init_t g_disk_init;
64 static g_fini_t g_disk_fini;
65 static g_start_t g_disk_start;
66 static g_ioctl_t g_disk_ioctl;
67 static g_dumpconf_t g_disk_dumpconf;
69 static struct g_class g_disk_class = {
74 .start = g_disk_start,
75 .access = g_disk_access,
76 .ioctl = g_disk_ioctl,
77 .dumpconf = g_disk_dumpconf,
81 g_disk_init(struct g_class *mp __unused)
84 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF);
88 g_disk_fini(struct g_class *mp __unused)
91 mtx_destroy(&g_disk_done_mtx);
94 DECLARE_GEOM_CLASS(g_disk_class, g_disk);
97 g_disk_lock_giant(struct disk *dp)
99 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
104 g_disk_unlock_giant(struct disk *dp)
106 if (dp->d_flags & DISKFLAG_NEEDSGIANT)
111 g_disk_access(struct g_provider *pp, int r, int w, int e)
116 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)",
119 dp = pp->geom->softc;
120 if (dp == NULL || dp->d_destroyed) {
122 * Allow decreasing access count even if disk is not
125 if (r <= 0 && w <= 0 && e <= 0)
133 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) {
134 if (dp->d_open != NULL) {
135 g_disk_lock_giant(dp);
136 error = dp->d_open(dp);
137 if (bootverbose && error != 0)
138 printf("Opened disk %s -> %d\n",
140 g_disk_unlock_giant(dp);
142 pp->mediasize = dp->d_mediasize;
143 pp->sectorsize = dp->d_sectorsize;
144 if (dp->d_flags & DISKFLAG_CANDELETE)
145 pp->flags |= G_PF_CANDELETE;
147 pp->flags &= ~G_PF_CANDELETE;
148 pp->stripeoffset = dp->d_stripeoffset;
149 pp->stripesize = dp->d_stripesize;
150 dp->d_flags |= DISKFLAG_OPEN;
151 if (dp->d_maxsize == 0) {
152 printf("WARNING: Disk drive %s%d has no d_maxsize\n",
153 dp->d_name, dp->d_unit);
154 dp->d_maxsize = DFLTPHYS;
156 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) {
157 if (dp->d_close != NULL) {
158 g_disk_lock_giant(dp);
159 error = dp->d_close(dp);
161 printf("Closed disk %s -> %d\n",
163 g_disk_unlock_giant(dp);
165 dp->d_flags &= ~DISKFLAG_OPEN;
171 g_disk_kerneldump(struct bio *bp, struct disk *dp)
174 struct g_kerneldump *gkd;
175 struct dumperinfo di;
178 gkd = (struct g_kerneldump*)bp->bio_data;
179 gp = bp->bio_to->geom;
180 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)",
181 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length);
182 if (dp->d_dump == NULL) {
183 g_io_deliver(bp, ENODEV);
186 di.dumper = dp->d_dump;
188 di.blocksize = dp->d_sectorsize;
189 di.maxiosize = dp->d_maxsize;
190 di.mediaoffset = gkd->offset;
191 if ((gkd->offset + gkd->length) > dp->d_mediasize)
192 gkd->length = dp->d_mediasize - gkd->offset;
193 di.mediasize = gkd->length;
194 error = set_dumper(&di);
195 g_io_deliver(bp, error);
199 g_disk_done(struct bio *bp)
204 /* See "notes" for why we need a mutex here */
205 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */
206 mtx_lock(&g_disk_done_mtx);
207 bp->bio_completed = bp->bio_length - bp->bio_resid;
209 bp2 = bp->bio_parent;
210 if (bp2->bio_error == 0)
211 bp2->bio_error = bp->bio_error;
212 bp2->bio_completed += bp->bio_completed;
213 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) &&
214 (dp = bp2->bio_to->geom->softc)) {
215 devstat_end_transaction_bio(dp->d_devstat, bp);
219 if (bp2->bio_children == bp2->bio_inbed) {
220 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed;
221 g_io_deliver(bp2, bp2->bio_error);
223 mtx_unlock(&g_disk_done_mtx);
227 g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td)
236 if (dp->d_ioctl == NULL)
238 g_disk_lock_giant(dp);
239 error = dp->d_ioctl(dp, cmd, data, fflag, td);
240 g_disk_unlock_giant(dp);
245 g_disk_start(struct bio *bp)
247 struct bio *bp2, *bp3;
252 dp = bp->bio_to->geom->softc;
253 if (dp == NULL || dp->d_destroyed) {
254 g_io_deliver(bp, ENXIO);
258 switch(bp->bio_cmd) {
260 if (!(dp->d_flags & DISKFLAG_CANDELETE)) {
269 bp2 = g_clone_bio(bp);
275 bp2->bio_offset += off;
276 bp2->bio_length -= off;
277 bp2->bio_data += off;
278 if (bp2->bio_length > dp->d_maxsize) {
280 * XXX: If we have a stripesize we should really
283 bp2->bio_length = dp->d_maxsize;
284 off += dp->d_maxsize;
286 * To avoid a race, we need to grab the next bio
287 * before we schedule this one. See "notes".
289 bp3 = g_clone_bio(bp);
291 bp->bio_error = ENOMEM;
293 bp2->bio_done = g_disk_done;
294 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize;
295 bp2->bio_bcount = bp2->bio_length;
297 devstat_start_transaction_bio(dp->d_devstat, bp2);
298 g_disk_lock_giant(dp);
300 g_disk_unlock_giant(dp);
303 } while (bp2 != NULL);
306 if (g_handleattr_int(bp, "GEOM::candelete",
307 (dp->d_flags & DISKFLAG_CANDELETE) != 0))
309 else if (g_handleattr_int(bp, "GEOM::fwsectors",
312 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads))
314 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0))
316 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident))
318 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump"))
319 g_disk_kerneldump(bp, dp);
324 g_trace(G_T_TOPOLOGY, "g_disk_flushcache(%s)",
326 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) {
327 g_io_deliver(bp, ENODEV);
330 bp2 = g_clone_bio(bp);
332 g_io_deliver(bp, ENOMEM);
335 bp2->bio_done = g_disk_done;
337 g_disk_lock_giant(dp);
339 g_disk_unlock_giant(dp);
345 if (error != EJUSTRETURN)
346 g_io_deliver(bp, error);
351 g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
358 if (indent == NULL) {
359 sbuf_printf(sb, " hd %u", dp->d_fwheads);
360 sbuf_printf(sb, " sc %u", dp->d_fwsectors);
364 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n",
365 indent, dp->d_fwheads);
366 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n",
367 indent, dp->d_fwsectors);
372 g_disk_create(void *arg, int flag)
375 struct g_provider *pp;
378 if (flag == EV_CANCEL)
382 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit);
384 pp = g_new_providerf(gp, "%s", gp->name);
385 pp->mediasize = dp->d_mediasize;
386 pp->sectorsize = dp->d_sectorsize;
387 if (dp->d_flags & DISKFLAG_CANDELETE)
388 pp->flags |= G_PF_CANDELETE;
389 pp->stripeoffset = dp->d_stripeoffset;
390 pp->stripesize = dp->d_stripesize;
392 printf("GEOM: new disk %s\n", gp->name);
394 g_error_provider(pp, 0);
398 g_disk_destroy(void *ptr, int flag)
408 g_wither_geom(gp, ENXIO);
414 * We only allow printable characters in disk ident,
415 * the rest is converted to 'x<HH>'.
418 g_disk_ident_adjust(char *ident, size_t size)
420 char *p, tmp[4], newid[DISK_IDENT_SIZE];
423 for (p = ident; *p != '\0'; p++) {
428 snprintf(tmp, sizeof(tmp), "x%02hhx",
429 *(unsigned char *)p);
431 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid))
435 strlcpy(ident, newid, size);
443 dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO);
448 disk_create(struct disk *dp, int version)
450 if (version != DISK_VERSION_00 && version != DISK_VERSION_01) {
451 printf("WARNING: Attempt to add disk %s%d %s",
452 dp->d_name, dp->d_unit,
453 " using incompatible ABI version of disk(9)\n");
454 printf("WARNING: Ignoring disk %s%d\n",
455 dp->d_name, dp->d_unit);
458 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy"));
459 KASSERT(dp->d_name != NULL, ("disk_create need d_name"));
460 KASSERT(*dp->d_name != 0, ("disk_create need d_name"));
461 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long"));
462 if (dp->d_devstat == NULL)
463 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit,
464 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED,
465 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX);
467 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident));
468 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL);
472 disk_destroy(struct disk *dp)
477 if (dp->d_devstat != NULL)
478 devstat_remove_entry(dp->d_devstat);
479 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL);
483 disk_gone(struct disk *dp)
486 struct g_provider *pp;
490 LIST_FOREACH(pp, &gp->provider, provider)
491 g_wither_provider(pp, ENXIO);
495 g_kern_disks(void *p, int flag __unused)
504 LIST_FOREACH(gp, &g_disk_class.geom, geom) {
505 sbuf_printf(sb, "%s%s", sp, gp->name);
512 sysctl_disks(SYSCTL_HANDLER_ARGS)
517 sb = sbuf_new_auto();
518 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL);
519 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
524 SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0,
525 sysctl_disks, "A", "names of available disks");