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 110475 2003-02-06 21:50:39Z phk $
|
35 * $FreeBSD: head/sys/geom/geom_disk.c 110477 2003-02-06 22:00:47Z phk $ |
36 */ 37 38#include "opt_geom.h" 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/kernel.h> 43#include <sys/sysctl.h> 44#include <sys/bio.h> 45#include <sys/conf.h> 46#include <sys/disk.h> 47#include <sys/fcntl.h> 48#include <sys/malloc.h> 49#include <sys/sysctl.h> 50#include <sys/stdint.h> 51#include <machine/md_var.h> 52 53 54#include <sys/lock.h> 55#include <sys/mutex.h> 56#include <geom/geom.h> 57 58static g_access_t g_disk_access; 59 60struct g_class g_disk_class = { 61 "DISK", 62 NULL, 63 NULL, 64 G_CLASS_INITIALIZER 65}; 66 67DECLARE_GEOM_CLASS(g_disk_class, g_disk); 68 69static void __inline 70g_disk_lock_giant(struct disk *dp) 71{ 72 if (dp->d_flags & DISKFLAG_NOGIANT) 73 return; 74 mtx_lock(&Giant); 75} 76 77static void __inline 78g_disk_unlock_giant(struct disk *dp) 79{ 80 if (dp->d_flags & DISKFLAG_NOGIANT) 81 return; 82 mtx_unlock(&Giant); 83} 84 85static int 86g_disk_access(struct g_provider *pp, int r, int w, int e) 87{ 88 struct disk *dp; 89 dev_t dev; 90 int error; 91 92 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 93 pp->name, r, w, e); 94 g_topology_assert(); 95 r += pp->acr; 96 w += pp->acw; 97 e += pp->ace; 98 dp = pp->geom->softc; 99 dev = dp->d_dev; 100 error = 0; 101 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 102 if (dp->d_open != NULL || dp->d_copen != NULL) { 103 g_disk_lock_giant(dp); 104 if (dp->d_open != NULL) 105 error = dp->d_open(dp); 106 else 107 error = dp->d_copen(dev, FREAD|FWRITE, 0, NULL); 108 if (error != 0) 109 printf("Opened disk %s -> %d\n", 110 pp->name, error); 111 g_disk_unlock_giant(dp); 112 } 113 pp->mediasize = dp->d_mediasize; 114 pp->sectorsize = dp->d_sectorsize; 115 dp->d_flags |= DISKFLAG_OPEN; 116 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 117 if (dp->d_close != NULL || dp->d_cclose != NULL) { 118 g_disk_lock_giant(dp); 119 if (dp->d_close != NULL) 120 error = dp->d_close(dp); 121 else 122 error = dp->d_cclose(dev, FREAD|FWRITE, 0, NULL); 123 if (error != 0) 124 printf("Closed disk %s -> %d\n", 125 pp->name, error); 126 g_disk_unlock_giant(dp); 127 } 128 dp->d_flags &= ~DISKFLAG_OPEN; 129 } 130 return (error); 131} 132 133static void 134g_disk_kerneldump(struct bio *bp, struct disk *dp) 135{ 136 int error; 137 struct g_kerneldump *gkd; 138 struct dumperinfo di; 139 struct g_geom *gp; 140 141 gkd = (struct g_kerneldump*)bp->bio_data; 142 gp = bp->bio_to->geom; 143 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 144 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 145 di.dumper = dp->d_dump; 146 di.priv = dp->d_dev; 147 di.blocksize = dp->d_sectorsize; 148 di.mediaoffset = gkd->offset; 149 di.mediasize = gkd->length; 150 error = set_dumper(&di); 151 g_io_deliver(bp, error); 152} 153 154static void 155g_disk_done(struct bio *bp) 156{ 157#ifdef maybe_not 158 struct disk *dp; 159 160 dp = bp->bio_disk; 161 bp->bio_completed = bp->bio_length - bp->bio_resid; 162 if (!(dp->d_flags & DISKFLAG_NOGIANT)) { 163 DROP_GIANT(); 164 g_std_done(bp); 165 PICKUP_GIANT(); 166 } else { 167 g_std_done(bp); 168 } 169#else 170 bp->bio_completed = bp->bio_length - bp->bio_resid; 171 g_std_done(bp); 172#endif 173} 174 175static void 176g_disk_start(struct bio *bp) 177{ 178 struct bio *bp2; 179 dev_t dev; 180 struct disk *dp; 181 struct g_ioctl *gio; 182 int error; 183 184 dp = bp->bio_to->geom->softc; 185 dev = dp->d_dev; 186 error = EJUSTRETURN; 187 switch(bp->bio_cmd) { 188 case BIO_DELETE: 189 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 190 error = 0; 191 break; 192 } 193 /* fall-through */ 194 case BIO_READ: 195 case BIO_WRITE: 196 bp2 = g_clone_bio(bp);
|
197 if (bp2 == NULL) { 198 error = ENOMEM; 199 break; 200 } |
201 bp2->bio_done = g_disk_done; 202 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT; 203 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 204 bp2->bio_bcount = bp2->bio_length; 205 bp2->bio_dev = dev; 206 bp2->bio_disk = dp; 207 g_disk_lock_giant(dp); 208 dp->d_strategy(bp2); 209 g_disk_unlock_giant(dp); 210 break; 211 case BIO_GETATTR: 212 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors)) 213 break; 214 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 215 break; 216 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 217 break; 218 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 219 g_disk_kerneldump(bp, dp); 220 else if ((dp->d_ioctl != NULL || dp->d_cioctl != NULL) && 221 !strcmp(bp->bio_attribute, "GEOM::ioctl") && 222 bp->bio_length == sizeof *gio) { 223 gio = (struct g_ioctl *)bp->bio_data; 224 if (dp->d_ioctl != NULL) { 225 gio->func = (d_ioctl_t *)(dp->d_ioctl); 226 gio->dev = dp; 227 } else { 228 gio->func = dp->d_cioctl; 229 gio->dev = dp->d_dev; 230 } 231 error = EDIRIOCTL; 232 } else 233 error = ENOIOCTL; 234 break; 235 case BIO_SETATTR: 236 if (!strcmp(bp->bio_attribute, "GEOM::ioctl") && 237 bp->bio_length == sizeof *gio) { 238 gio = (struct g_ioctl *)bp->bio_data; 239 gio->func = devsw(dp->d_dev)->d_ioctl; 240 gio->dev = dp->d_dev; 241 error = EDIRIOCTL; 242 } else 243 error = ENOIOCTL; 244 break; 245 default: 246 error = EOPNOTSUPP; 247 break; 248 } 249 if (error != EJUSTRETURN) 250 g_io_deliver(bp, error); 251 return; 252} 253 254static void 255g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 256{ 257 struct disk *dp; 258 259 dp = gp->softc; 260 if (indent == NULL) { 261 sbuf_printf(sb, " hd %u", dp->d_fwheads); 262 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 263 return; 264 } 265 if (pp != NULL) { 266 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 267 indent, dp->d_fwheads); 268 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 269 indent, dp->d_fwsectors); 270 } 271} 272 273static void 274g_disk_create(void *arg) 275{ 276 struct g_geom *gp; 277 struct g_provider *pp; 278 dev_t dev; 279 280 g_topology_assert(); 281 dev = arg; 282 gp = g_new_geomf(&g_disk_class, dev->si_name); 283 gp->start = g_disk_start; 284 gp->access = g_disk_access; 285 gp->softc = dev->si_disk; 286 gp->dumpconf = g_disk_dumpconf; 287 dev->si_disk->d_geom = gp; 288 pp = g_new_providerf(gp, "%s", gp->name); 289 pp->mediasize = dev->si_disk->d_mediasize; 290 pp->sectorsize = dev->si_disk->d_sectorsize; 291 g_error_provider(pp, 0); 292 if (bootverbose) 293 printf("GEOM: new disk %s\n", gp->name); 294} 295 296 297 298dev_t 299disk_create(int unit, struct disk *dp, int flags, struct cdevsw *cdevsw, void * unused __unused) 300{ 301 dev_t dev; 302 303 dev = g_malloc(sizeof *dev, M_ZERO); 304 dp->d_dev = dev; 305 dp->d_flags = flags; 306 dp->d_devsw = cdevsw; 307 dev->si_devsw = cdevsw; 308 if (cdevsw != NULL) { 309 dp->d_copen = cdevsw->d_open; 310 dp->d_cclose = cdevsw->d_close; 311 dp->d_cioctl = cdevsw->d_ioctl; 312 dp->d_strategy = cdevsw->d_strategy; 313 dp->d_dump = (dumper_t *)cdevsw->d_dump; 314 dp->d_name = cdevsw->d_name; 315 } 316 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 317 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 318 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 319 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 320 dev->si_disk = dp; 321 dev->si_udev = 0x10002; /* XXX: Needed ? */ 322 dev->si_name = dev->__si_namebuf; 323 sprintf(dev->si_name, "%s%d", dp->d_name, unit); 324 g_call_me(g_disk_create, dev); 325 return (dev); 326} 327 328void 329disk_destroy(dev_t dev) 330{ 331 struct disk *dp; 332 struct g_geom *gp; 333 334 dp = dev->si_disk; 335 gp = dp->d_geom; 336 g_free(dev); 337 gp->flags |= G_GEOM_WITHER; 338 gp->softc = NULL; 339 g_orphan_provider(LIST_FIRST(&gp->provider), ENXIO); 340} 341 342static void 343g_kern_disks(void *p) 344{ 345 struct sbuf *sb; 346 struct g_geom *gp; 347 char *sp; 348 349 sb = p; 350 sp = ""; 351 g_topology_assert(); 352 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 353 sbuf_printf(sb, "%s%s", sp, gp->name); 354 sp = " "; 355 } 356 sbuf_finish(sb); 357 wakeup(sb); 358} 359 360static int 361sysctl_disks(SYSCTL_HANDLER_ARGS) 362{ 363 int error; 364 struct sbuf *sb; 365 366 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 367 sbuf_clear(sb); 368 g_call_me(g_kern_disks, sb); 369 do { 370 tsleep(sb, PZERO, "kern.disks", hz); 371 } while(!sbuf_done(sb)); 372 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 373 sbuf_delete(sb); 374 return error; 375} 376 377SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 378 sysctl_disks, "A", "names of available disks"); 379
|