geom_disk.c revision 163833
1254219Scy/*- 2254219Scy * Copyright (c) 2002 Poul-Henning Kamp 3254219Scy * Copyright (c) 2002 Networks Associates Technology, Inc. 4254219Scy * All rights reserved. 5254219Scy * 6254219Scy * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7254219Scy * and NAI Labs, the Security Research Division of Network Associates, Inc. 8254219Scy * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9254219Scy * DARPA CHATS research program. 10254219Scy * 11254219Scy * Redistribution and use in source and binary forms, with or without 12254219Scy * modification, are permitted provided that the following conditions 13254219Scy * are met: 14254219Scy * 1. Redistributions of source code must retain the above copyright 15254219Scy * notice, this list of conditions and the following disclaimer. 16254219Scy * 2. Redistributions in binary form must reproduce the above copyright 17254219Scy * notice, this list of conditions and the following disclaimer in the 18254219Scy * documentation and/or other materials provided with the distribution. 19254219Scy * 3. The names of the authors may not be used to endorse or promote 20254219Scy * products derived from this software without specific prior written 21254219Scy * permission. 22254219Scy * 23254219Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24254219Scy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25254219Scy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26254219Scy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27254219Scy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28254219Scy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29254219Scy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30254219Scy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31254219Scy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32254219Scy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33254219Scy * SUCH DAMAGE. 34254219Scy */ 35254219Scy 36254219Scy#include <sys/cdefs.h> 37254219Scy__FBSDID("$FreeBSD: head/sys/geom/geom_disk.c 163833 2006-10-31 21:12:43Z pjd $"); 38254219Scy 39254219Scy#include "opt_geom.h" 40254219Scy 41254219Scy#include <sys/param.h> 42254219Scy#include <sys/systm.h> 43254219Scy#include <sys/kernel.h> 44254219Scy#include <sys/sysctl.h> 45254219Scy#include <sys/bio.h> 46254219Scy#include <sys/conf.h> 47254219Scy#include <sys/fcntl.h> 48254219Scy#include <sys/malloc.h> 49254219Scy#include <sys/sysctl.h> 50254219Scy#include <sys/devicestat.h> 51254219Scy#include <machine/md_var.h> 52254219Scy 53254219Scy#include <sys/lock.h> 54254219Scy#include <sys/mutex.h> 55254219Scy#include <geom/geom.h> 56254219Scy#include <geom/geom_disk.h> 57254219Scy#include <geom/geom_int.h> 58254219Scy 59254219Scystatic struct mtx g_disk_done_mtx; 60254219Scy 61254219Scystatic g_access_t g_disk_access; 62254219Scystatic g_init_t g_disk_init; 63254219Scystatic g_fini_t g_disk_fini; 64254219Scystatic g_start_t g_disk_start; 65254219Scystatic g_ioctl_t g_disk_ioctl; 66254219Scystatic g_dumpconf_t g_disk_dumpconf; 67254219Scy 68254219Scystatic struct g_class g_disk_class = { 69254219Scy .name = "DISK", 70254219Scy .version = G_VERSION, 71254219Scy .init = g_disk_init, 72254219Scy .fini = g_disk_fini, 73254219Scy .start = g_disk_start, 74254219Scy .access = g_disk_access, 75254219Scy .ioctl = g_disk_ioctl, 76254219Scy .dumpconf = g_disk_dumpconf, 77254219Scy}; 78254219Scy 79254219Scystatic void 80254219Scyg_disk_init(struct g_class *mp __unused) 81254219Scy{ 82254219Scy 83254219Scy mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF); 84254219Scy} 85254219Scy 86254219Scystatic void 87254219Scyg_disk_fini(struct g_class *mp __unused) 88254219Scy{ 89254219Scy 90254219Scy mtx_destroy(&g_disk_done_mtx); 91254219Scy} 92254219Scy 93254219ScyDECLARE_GEOM_CLASS(g_disk_class, g_disk); 94254219Scy 95254219Scystatic void __inline 96254219Scyg_disk_lock_giant(struct disk *dp) 97254219Scy{ 98254219Scy if (dp->d_flags & DISKFLAG_NEEDSGIANT) 99254219Scy mtx_lock(&Giant); 100254219Scy} 101254219Scy 102254219Scystatic void __inline 103254219Scyg_disk_unlock_giant(struct disk *dp) 104254219Scy{ 105254219Scy if (dp->d_flags & DISKFLAG_NEEDSGIANT) 106254219Scy mtx_unlock(&Giant); 107254219Scy} 108254219Scy 109254219Scystatic int 110254219Scyg_disk_access(struct g_provider *pp, int r, int w, int e) 111254219Scy{ 112254219Scy struct disk *dp; 113254219Scy int error; 114254219Scy 115254219Scy g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 116254219Scy pp->name, r, w, e); 117254219Scy g_topology_assert(); 118254219Scy dp = pp->geom->softc; 119254219Scy if (dp == NULL || dp->d_destroyed) { 120254219Scy /* 121254219Scy * Allow decreasing access count even if disk is not 122254219Scy * avaliable anymore. 123254219Scy */ 124254219Scy if (r <= 0 && w <= 0 && e <= 0) 125254219Scy return (0); 126254219Scy return (ENXIO); 127254219Scy } 128254219Scy r += pp->acr; 129254219Scy w += pp->acw; 130254219Scy e += pp->ace; 131254219Scy error = 0; 132254219Scy if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 133254219Scy if (dp->d_open != NULL) { 134254219Scy g_disk_lock_giant(dp); 135254219Scy error = dp->d_open(dp); 136254219Scy if (error != 0) 137254219Scy printf("Opened disk %s -> %d\n", 138254219Scy pp->name, error); 139254219Scy g_disk_unlock_giant(dp); 140254219Scy } 141254219Scy pp->mediasize = dp->d_mediasize; 142254219Scy pp->sectorsize = dp->d_sectorsize; 143254219Scy dp->d_flags |= DISKFLAG_OPEN; 144254219Scy if (dp->d_maxsize == 0) { 145254219Scy printf("WARNING: Disk drive %s%d has no d_maxsize\n", 146254219Scy dp->d_name, dp->d_unit); 147254219Scy dp->d_maxsize = DFLTPHYS; 148254219Scy } 149254219Scy } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 150254219Scy if (dp->d_close != NULL) { 151254219Scy g_disk_lock_giant(dp); 152254219Scy error = dp->d_close(dp); 153254219Scy if (error != 0) 154254219Scy printf("Closed disk %s -> %d\n", 155254219Scy pp->name, error); 156254219Scy g_disk_unlock_giant(dp); 157254219Scy } 158254219Scy dp->d_flags &= ~DISKFLAG_OPEN; 159254219Scy } 160254219Scy return (error); 161254219Scy} 162254219Scy 163254219Scystatic void 164254219Scyg_disk_kerneldump(struct bio *bp, struct disk *dp) 165254219Scy{ 166254219Scy int error; 167254219Scy struct g_kerneldump *gkd; 168254219Scy struct dumperinfo di; 169254219Scy struct g_geom *gp; 170254219Scy 171254219Scy gkd = (struct g_kerneldump*)bp->bio_data; 172254219Scy gp = bp->bio_to->geom; 173254219Scy g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 174254219Scy gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 175254219Scy if (dp->d_dump == NULL) { 176254219Scy g_io_deliver(bp, ENODEV); 177254219Scy return; 178254219Scy } 179254219Scy di.dumper = dp->d_dump; 180254219Scy di.priv = dp; 181254219Scy di.blocksize = dp->d_sectorsize; 182254219Scy di.mediaoffset = gkd->offset; 183254219Scy if ((gkd->offset + gkd->length) > dp->d_mediasize) 184254219Scy gkd->length = dp->d_mediasize - gkd->offset; 185254219Scy di.mediasize = gkd->length; 186254219Scy error = set_dumper(&di); 187254219Scy g_io_deliver(bp, error); 188254219Scy} 189254219Scy 190254219Scystatic void 191254219Scyg_disk_done(struct bio *bp) 192254219Scy{ 193254219Scy struct bio *bp2; 194254219Scy struct disk *dp; 195254219Scy 196254219Scy /* See "notes" for why we need a mutex here */ 197254219Scy /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 198254219Scy mtx_lock(&g_disk_done_mtx); 199254219Scy bp->bio_completed = bp->bio_length - bp->bio_resid; 200254219Scy 201254219Scy bp2 = bp->bio_parent; 202254219Scy if (bp2->bio_error == 0) 203254219Scy bp2->bio_error = bp->bio_error; 204254219Scy bp2->bio_completed += bp->bio_completed; 205254219Scy if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) && 206254219Scy (dp = bp2->bio_to->geom->softc)) { 207254219Scy devstat_end_transaction_bio(dp->d_devstat, bp); 208254219Scy } 209254219Scy g_destroy_bio(bp); 210254219Scy bp2->bio_inbed++; 211254219Scy if (bp2->bio_children == bp2->bio_inbed) { 212254219Scy bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 213254219Scy g_io_deliver(bp2, bp2->bio_error); 214254219Scy } 215254219Scy mtx_unlock(&g_disk_done_mtx); 216254219Scy} 217254219Scy 218254219Scystatic int 219254219Scyg_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td) 220254219Scy{ 221254219Scy struct g_geom *gp; 222254219Scy struct disk *dp; 223254219Scy int error; 224254219Scy 225254219Scy gp = pp->geom; 226254219Scy dp = gp->softc; 227254219Scy 228254219Scy if (dp->d_ioctl == NULL) 229254219Scy return (ENOIOCTL); 230254219Scy g_disk_lock_giant(dp); 231254219Scy error = dp->d_ioctl(dp, cmd, data, fflag, td); 232254219Scy g_disk_unlock_giant(dp); 233254219Scy return(error); 234254219Scy} 235254219Scy 236254219Scystatic void 237254219Scyg_disk_start(struct bio *bp) 238254219Scy{ 239254219Scy struct bio *bp2, *bp3; 240254219Scy struct disk *dp; 241254219Scy int error; 242254219Scy off_t off; 243254219Scy 244254219Scy dp = bp->bio_to->geom->softc; 245254219Scy if (dp == NULL || dp->d_destroyed) { 246254219Scy g_io_deliver(bp, ENXIO); 247254219Scy return; 248254219Scy } 249254219Scy error = EJUSTRETURN; 250254219Scy switch(bp->bio_cmd) { 251254219Scy case BIO_DELETE: 252254219Scy if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 253254219Scy error = 0; 254254219Scy break; 255254219Scy } 256254219Scy /* fall-through */ 257254219Scy case BIO_READ: 258254219Scy case BIO_WRITE: 259254219Scy off = 0; 260254219Scy bp3 = NULL; 261254219Scy bp2 = g_clone_bio(bp); 262254219Scy if (bp2 == NULL) { 263254219Scy error = ENOMEM; 264254219Scy break; 265254219Scy } 266254219Scy do { 267254219Scy bp2->bio_offset += off; 268254219Scy bp2->bio_length -= off; 269254219Scy bp2->bio_data += off; 270254219Scy if (bp2->bio_length > dp->d_maxsize) { 271254219Scy /* 272254219Scy * XXX: If we have a stripesize we should really 273254219Scy * use it here. 274254219Scy */ 275254219Scy bp2->bio_length = dp->d_maxsize; 276254219Scy off += dp->d_maxsize; 277254219Scy /* 278254219Scy * To avoid a race, we need to grab the next bio 279254219Scy * before we schedule this one. See "notes". 280254219Scy */ 281254219Scy bp3 = g_clone_bio(bp); 282254219Scy if (bp3 == NULL) 283254219Scy bp->bio_error = ENOMEM; 284254219Scy } 285254219Scy bp2->bio_done = g_disk_done; 286254219Scy bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 287254219Scy bp2->bio_bcount = bp2->bio_length; 288254219Scy bp2->bio_disk = dp; 289254219Scy devstat_start_transaction_bio(dp->d_devstat, bp2); 290254219Scy g_disk_lock_giant(dp); 291254219Scy dp->d_strategy(bp2); 292254219Scy g_disk_unlock_giant(dp); 293254219Scy bp2 = bp3; 294254219Scy bp3 = NULL; 295254219Scy } while (bp2 != NULL); 296254219Scy break; 297254219Scy case BIO_GETATTR: 298254219Scy if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors)) 299254219Scy break; 300254219Scy else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 301254219Scy break; 302254219Scy else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 303254219Scy break; 304254219Scy else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 305254219Scy g_disk_kerneldump(bp, dp); 306254219Scy else 307254219Scy error = ENOIOCTL; 308254219Scy break; 309254219Scy case BIO_FLUSH: 310254219Scy g_trace(G_T_TOPOLOGY, "g_disk_flushcache(%s)", 311254219Scy bp->bio_to->name); 312254219Scy if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) { 313254219Scy g_io_deliver(bp, ENODEV); 314254219Scy return; 315254219Scy } 316254219Scy bp2 = g_clone_bio(bp); 317254219Scy if (bp2 == NULL) { 318254219Scy g_io_deliver(bp, ENOMEM); 319254219Scy return; 320254219Scy } 321254219Scy bp2->bio_done = g_disk_done; 322254219Scy bp2->bio_disk = dp; 323254219Scy g_disk_lock_giant(dp); 324254219Scy dp->d_strategy(bp2); 325254219Scy g_disk_unlock_giant(dp); 326254219Scy break; 327254219Scy default: 328254219Scy error = EOPNOTSUPP; 329254219Scy break; 330254219Scy } 331254219Scy if (error != EJUSTRETURN) 332254219Scy g_io_deliver(bp, error); 333254219Scy return; 334254219Scy} 335254219Scy 336254219Scystatic void 337254219Scyg_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 338254219Scy{ 339254219Scy struct disk *dp; 340254219Scy 341254219Scy dp = gp->softc; 342254219Scy if (dp == NULL) 343254219Scy return; 344254219Scy if (indent == NULL) { 345254219Scy sbuf_printf(sb, " hd %u", dp->d_fwheads); 346254219Scy sbuf_printf(sb, " sc %u", dp->d_fwsectors); 347254219Scy return; 348254219Scy } 349254219Scy if (pp != NULL) { 350254219Scy sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 351254219Scy indent, dp->d_fwheads); 352254219Scy sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 353254219Scy indent, dp->d_fwsectors); 354254219Scy } 355254219Scy} 356254219Scy 357254219Scystatic void 358254219Scyg_disk_create(void *arg, int flag) 359{ 360 struct g_geom *gp; 361 struct g_provider *pp; 362 struct disk *dp; 363 364 if (flag == EV_CANCEL) 365 return; 366 g_topology_assert(); 367 dp = arg; 368 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 369 gp->softc = dp; 370 pp = g_new_providerf(gp, "%s", gp->name); 371 pp->mediasize = dp->d_mediasize; 372 pp->sectorsize = dp->d_sectorsize; 373 if (dp->d_flags & DISKFLAG_CANDELETE) 374 pp->flags |= G_PF_CANDELETE; 375 pp->stripeoffset = dp->d_stripeoffset; 376 pp->stripesize = dp->d_stripesize; 377 if (bootverbose) 378 printf("GEOM: new disk %s\n", gp->name); 379 dp->d_geom = gp; 380 g_error_provider(pp, 0); 381} 382 383static void 384g_disk_destroy(void *ptr, int flag) 385{ 386 struct disk *dp; 387 struct g_geom *gp; 388 389 g_topology_assert(); 390 dp = ptr; 391 gp = dp->d_geom; 392 if (gp != NULL) { 393 gp->softc = NULL; 394 g_wither_geom(gp, ENXIO); 395 } 396 g_free(dp); 397} 398 399struct disk * 400disk_alloc() 401{ 402 struct disk *dp; 403 404 dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO); 405 return (dp); 406} 407 408void 409disk_create(struct disk *dp, int version) 410{ 411 if (version != DISK_VERSION_00) { 412 printf("WARNING: Attempt to add disk %s%d %s", 413 dp->d_name, dp->d_unit, 414 " using incompatible ABI version of disk(9)\n"); 415 printf("WARNING: Ignoring disk %s%d\n", 416 dp->d_name, dp->d_unit); 417 return; 418 } 419 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 420 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 421 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 422 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 423 if (dp->d_devstat == NULL) 424 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 425 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 426 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 427 dp->d_geom = NULL; 428 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 429} 430 431void 432disk_destroy(struct disk *dp) 433{ 434 435 g_cancel_event(dp); 436 dp->d_destroyed = 1; 437 if (dp->d_devstat != NULL) 438 devstat_remove_entry(dp->d_devstat); 439 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL); 440} 441 442void 443disk_gone(struct disk *dp) 444{ 445 struct g_geom *gp; 446 struct g_provider *pp; 447 448 gp = dp->d_geom; 449 if (gp != NULL) 450 LIST_FOREACH(pp, &gp->provider, provider) 451 g_wither_provider(pp, ENXIO); 452} 453 454static void 455g_kern_disks(void *p, int flag __unused) 456{ 457 struct sbuf *sb; 458 struct g_geom *gp; 459 char *sp; 460 461 sb = p; 462 sp = ""; 463 g_topology_assert(); 464 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 465 sbuf_printf(sb, "%s%s", sp, gp->name); 466 sp = " "; 467 } 468 sbuf_finish(sb); 469} 470 471static int 472sysctl_disks(SYSCTL_HANDLER_ARGS) 473{ 474 int error; 475 struct sbuf *sb; 476 477 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 478 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 479 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 480 sbuf_delete(sb); 481 return error; 482} 483 484SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 485 sysctl_disks, "A", "names of available disks"); 486 487