1/*- 2 * Copyright (c) 2002 Poul-Henning Kamp 3 * Copyright (c) 2002 Networks Associates Technology, Inc. 4 * All rights reserved. 5 * 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. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 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 21 * permission. 22 * 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 33 * SUCH DAMAGE. 34 *
|
35 * $FreeBSD: head/sys/geom/geom_disk.c 106101 2002-10-28 22:43:54Z phk $
|
35 * $FreeBSD: head/sys/geom/geom_disk.c 107953 2002-12-16 22:33:27Z phk $ |
36 */ 37 38#include "opt_geom.h" 39#ifndef NO_GEOM 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/kernel.h> 44#include <sys/sysctl.h> 45#include <sys/bio.h> 46#include <sys/conf.h> 47#include <sys/disk.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> 54 55 56#include <sys/lock.h> 57#include <sys/mutex.h> 58#include <geom/geom.h> 59 60static g_access_t g_disk_access; 61 62struct g_class g_disk_class = { 63 "DISK", 64 NULL, 65 NULL, 66 G_CLASS_INITIALIZER 67}; 68 69DECLARE_GEOM_CLASS(g_disk_class, g_disk); 70 71static int 72g_disk_access(struct g_provider *pp, int r, int w, int e) 73{ 74 struct disk *dp; 75 dev_t dev; 76 int error; 77 78 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 79 pp->name, r, w, e); 80 g_topology_assert(); 81 r += pp->acr; 82 w += pp->acw; 83 e += pp->ace; 84 dp = pp->geom->softc; 85 dev = dp->d_dev; 86 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 87 mtx_lock(&Giant); 88 error = devsw(dev)->d_open(dev, 3, 0, NULL); 89 if (error != 0) 90 printf("Opened disk %s -> %d\n", pp->name, error); 91 mtx_unlock(&Giant); 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) { 95 mtx_lock(&Giant); 96 error = devsw(dev)->d_close(dev, 3, 0, NULL); 97 if (error != 0) 98 printf("Closed disk %s -> %d\n", pp->name, error); 99 mtx_unlock(&Giant); 100 } else { 101 error = 0; 102 } 103 return (error); 104} 105 106static void 107g_disk_kerneldump(struct bio *bp, struct disk *dp) 108{ 109 int error; 110 struct g_kerneldump *gkd; 111 struct dumperinfo di; 112 struct g_geom *gp; 113 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; 119 di.priv = dp->d_dev; 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); 125} 126 127static void 128g_disk_done(struct bio *bp) 129{ 130 131 mtx_unlock(&Giant); 132 bp->bio_completed = bp->bio_length - bp->bio_resid; 133 g_std_done(bp); 134 mtx_lock(&Giant); 135} 136 137static void 138g_disk_start(struct bio *bp) 139{ 140 struct bio *bp2; 141 dev_t dev; 142 struct disk *dp; 143 struct g_ioctl *gio; 144 int error; 145 146 dp = bp->bio_to->geom->softc; 147 dev = dp->d_dev; 148 error = EJUSTRETURN; 149 switch(bp->bio_cmd) { 150 case BIO_DELETE: 151 if (!(devsw(dev)->d_flags & D_CANFREE)) { 152 error = 0; 153 break; 154 } 155 /* fall-through */ 156 case BIO_READ: 157 case BIO_WRITE: 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; 163 bp2->bio_dev = dev; 164 mtx_lock(&Giant); 165 devsw(dev)->d_strategy(bp2); 166 mtx_unlock(&Giant); 167 break; 168 case BIO_GETATTR: 169 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors)) 170 break; 171 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 172 break; 173 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 174 break; 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; 182 error = EDIRIOCTL; 183 } else 184 error = ENOIOCTL; 185 break; 186 case BIO_SETATTR: 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; 192 error = EDIRIOCTL; 193 } else 194 error = ENOIOCTL; 195 break; 196 default: 197 error = EOPNOTSUPP; 198 break; 199 } 200 if (error != EJUSTRETURN) 201 g_io_deliver(bp, error); 202 return; 203} 204 205static void
|
206g_disk_dumpconf(struct sbuf *sb, char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp)
|
206g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) |
207{ 208 struct disk *dp; 209 210 dp = gp->softc; 211 if (indent == NULL) { 212 sbuf_printf(sb, " hd %u", dp->d_fwheads); 213 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 214 return; 215 } 216 if (pp != NULL) { 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); 221 } 222} 223 224static void 225g_disk_create(void *arg) 226{ 227 struct g_geom *gp; 228 struct g_provider *pp; 229 dev_t dev; 230 231 g_topology_assert(); 232 dev = arg; 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); 243 if (bootverbose) 244 printf("GEOM: new disk %s\n", gp->name); 245} 246 247 248 249dev_t 250disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, struct cdevsw *proto) 251{ 252 dev_t dev; 253 254 dev = g_malloc(sizeof *dev, M_WAITOK | M_ZERO); 255 dp->d_dev = dev; 256 dp->d_devsw = cdevsw; 257 dev->si_devsw = cdevsw; 258 dev->si_disk = dp; 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); 262 return (dev); 263} 264 265void disk_dev_synth(dev_t dev); 266 267void 268disk_dev_synth(dev_t dev) 269{ 270 271 return; 272} 273 274void 275disk_destroy(dev_t dev) 276{ 277 struct disk *dp; 278 struct g_geom *gp; 279 280 dp = dev->si_disk; 281 gp = dp->d_softc; 282 g_free(dev); 283 gp->flags |= G_GEOM_WITHER; 284 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO); 285} 286 287void 288disk_invalidate (struct disk *disk) 289{ 290} 291 292static void 293g_kern_disks(void *p) 294{ 295 struct sbuf *sb; 296 struct g_geom *gp; 297 char *sp; 298 299 sb = p; 300 sp = ""; 301 g_topology_assert(); 302 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 303 sbuf_printf(sb, "%s%s", sp, gp->name); 304 sp = " "; 305 } 306 sbuf_finish(sb); 307 wakeup(sb); 308} 309 310static int 311sysctl_disks(SYSCTL_HANDLER_ARGS) 312{ 313 int error; 314 struct sbuf *sb; 315 316 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 317 sbuf_clear(sb); 318 g_call_me(g_kern_disks, sb); 319 do { 320 tsleep(sb, PZERO, "kern.disks", hz); 321 } while(!sbuf_done(sb)); 322 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 323 sbuf_delete(sb); 324 return error; 325} 326 327SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 328 sysctl_disks, "A", "names of available disks"); 329 330#endif
|