geom_disk.c revision 115214
1112158Sdas/*- 2112158Sdas * Copyright (c) 2002 Poul-Henning Kamp 3112158Sdas * Copyright (c) 2002 Networks Associates Technology, Inc. 4112158Sdas * All rights reserved. 5112158Sdas * 6112158Sdas * This software was developed for the FreeBSD Project by Poul-Henning Kamp 7112158Sdas * and NAI Labs, the Security Research Division of Network Associates, Inc. 8112158Sdas * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the 9112158Sdas * DARPA CHATS research program. 10112158Sdas * 11112158Sdas * Redistribution and use in source and binary forms, with or without 12112158Sdas * modification, are permitted provided that the following conditions 13112158Sdas * are met: 14112158Sdas * 1. Redistributions of source code must retain the above copyright 15112158Sdas * notice, this list of conditions and the following disclaimer. 16112158Sdas * 2. Redistributions in binary form must reproduce the above copyright 17112158Sdas * notice, this list of conditions and the following disclaimer in the 18112158Sdas * documentation and/or other materials provided with the distribution. 19112158Sdas * 3. The names of the authors may not be used to endorse or promote 20112158Sdas * products derived from this software without specific prior written 21112158Sdas * permission. 22112158Sdas * 23112158Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 24112158Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25112158Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26112158Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 27112158Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28112158Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29165743Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30165743Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31112158Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32112158Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33112158Sdas * SUCH DAMAGE. 34112158Sdas * 35112158Sdas * $FreeBSD: head/sys/geom/geom_disk.c 115214 2003-05-21 18:52:29Z phk $ 36112158Sdas */ 37112158Sdas 38112158Sdas#include "opt_geom.h" 39112158Sdas 40112158Sdas#include <sys/param.h> 41112158Sdas#include <sys/systm.h> 42112158Sdas#include <sys/kernel.h> 43112158Sdas#include <sys/sysctl.h> 44112158Sdas#include <sys/bio.h> 45112158Sdas#include <sys/conf.h> 46112158Sdas#include <sys/fcntl.h> 47112158Sdas#include <sys/malloc.h> 48112158Sdas#include <sys/sysctl.h> 49112158Sdas#include <sys/devicestat.h> 50112158Sdas#include <machine/md_var.h> 51112158Sdas 52112158Sdas#include <sys/lock.h> 53112158Sdas#include <sys/mutex.h> 54112158Sdas#include <geom/geom.h> 55112158Sdas#include <geom/geom_disk.h> 56112158Sdas#include <geom/geom_int.h> 57112158Sdas 58112158Sdasstatic struct mtx g_disk_done_mtx; 59112158Sdas 60112158Sdasstatic g_access_t g_disk_access; 61112158Sdas 62112158Sdasstruct g_class g_disk_class = { 63112158Sdas .name = "DISK", 64112158Sdas G_CLASS_INITIALIZER 65112158Sdas}; 66112158Sdas 67112158Sdasstatic void 68112158Sdasg_disk_init(void) 69112158Sdas{ 70112158Sdas mtx_unlock(&Giant); 71112158Sdas g_add_class(&g_disk_class); 72112158Sdas mtx_init(&g_disk_done_mtx, "g_disk_done", MTX_DEF, 0); 73112158Sdas mtx_lock(&Giant); 74112158Sdas} 75112158Sdas 76112158SdasDECLARE_GEOM_CLASS_INIT(g_disk_class, g_disk, g_disk_init); 77112158Sdas 78112158Sdasstatic void __inline 79112158Sdasg_disk_lock_giant(struct disk *dp) 80112158Sdas{ 81112158Sdas if (dp->d_flags & DISKFLAG_NOGIANT) 82112158Sdas return; 83112158Sdas mtx_lock(&Giant); 84112158Sdas} 85112158Sdas 86112158Sdasstatic void __inline 87112158Sdasg_disk_unlock_giant(struct disk *dp) 88112158Sdas{ 89112158Sdas if (dp->d_flags & DISKFLAG_NOGIANT) 90112158Sdas return; 91112158Sdas mtx_unlock(&Giant); 92112158Sdas} 93112158Sdas 94112158Sdasstatic int 95112158Sdasg_disk_access(struct g_provider *pp, int r, int w, int e) 96112158Sdas{ 97112158Sdas struct disk *dp; 98112158Sdas int error; 99112158Sdas 100112158Sdas g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 101112158Sdas pp->name, r, w, e); 102112158Sdas g_topology_assert(); 103112158Sdas r += pp->acr; 104112158Sdas w += pp->acw; 105112158Sdas e += pp->ace; 106112158Sdas dp = pp->geom->softc; 107112158Sdas if (dp == NULL) 108112158Sdas return (ENXIO); 109112158Sdas error = 0; 110112158Sdas if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 111112158Sdas if (dp->d_open != NULL) { 112112158Sdas g_disk_lock_giant(dp); 113112158Sdas error = dp->d_open(dp); 114112158Sdas if (error != 0) 115112158Sdas printf("Opened disk %s -> %d\n", 116112158Sdas pp->name, error); 117112158Sdas g_disk_unlock_giant(dp); 118112158Sdas } 119112158Sdas pp->mediasize = dp->d_mediasize; 120112158Sdas pp->sectorsize = dp->d_sectorsize; 121112158Sdas dp->d_flags |= DISKFLAG_OPEN; 122112158Sdas if (dp->d_maxsize == 0) { 123112158Sdas printf("WARNING: Disk drive %s%d has no d_maxsize\n", 124112158Sdas dp->d_name, dp->d_unit); 125112158Sdas dp->d_maxsize = DFLTPHYS; 126112158Sdas } 127112158Sdas } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 128112158Sdas if (dp->d_close != NULL) { 129112158Sdas g_disk_lock_giant(dp); 130112158Sdas error = dp->d_close(dp); 131112158Sdas if (error != 0) 132112158Sdas printf("Closed disk %s -> %d\n", 133112158Sdas pp->name, error); 134112158Sdas g_disk_unlock_giant(dp); 135112158Sdas } 136112158Sdas dp->d_flags &= ~DISKFLAG_OPEN; 137112158Sdas } 138112158Sdas return (error); 139112158Sdas} 140112158Sdas 141112158Sdasstatic void 142112158Sdasg_disk_kerneldump(struct bio *bp, struct disk *dp) 143112158Sdas{ 144112158Sdas int error; 145112158Sdas struct g_kerneldump *gkd; 146112158Sdas struct dumperinfo di; 147112158Sdas struct g_geom *gp; 148112158Sdas 149112158Sdas gkd = (struct g_kerneldump*)bp->bio_data; 150112158Sdas gp = bp->bio_to->geom; 151112158Sdas g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 152112158Sdas gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 153112158Sdas di.dumper = dp->d_dump; 154112158Sdas di.priv = dp; 155112158Sdas di.blocksize = dp->d_sectorsize; 156112158Sdas di.mediaoffset = gkd->offset; 157112158Sdas di.mediasize = gkd->length; 158112158Sdas error = set_dumper(&di); 159112158Sdas g_io_deliver(bp, error); 160112158Sdas} 161112158Sdas 162112158Sdasstatic void 163112158Sdasg_disk_done(struct bio *bp) 164112158Sdas{ 165112158Sdas struct bio *bp2; 166112158Sdas struct disk *dp; 167112158Sdas 168112158Sdas /* See "notes" for why we need a mutex here */ 169112158Sdas /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 170112158Sdas mtx_lock(&g_disk_done_mtx); 171112158Sdas bp->bio_completed = bp->bio_length - bp->bio_resid; 172112158Sdas 173112158Sdas bp2 = bp->bio_parent; 174112158Sdas dp = bp2->bio_to->geom->softc; 175112158Sdas if (bp2->bio_error == 0) 176112158Sdas bp2->bio_error = bp->bio_error; 177112158Sdas bp2->bio_completed += bp->bio_completed; 178112158Sdas g_destroy_bio(bp); 179112158Sdas bp2->bio_inbed++; 180112158Sdas if (bp2->bio_children == bp2->bio_inbed) { 181112158Sdas bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 182112158Sdas devstat_end_transaction_bio(dp->d_devstat, bp2); 183112158Sdas g_io_deliver(bp2, bp2->bio_error); 184112158Sdas } 185112158Sdas mtx_unlock(&g_disk_done_mtx); 186112158Sdas} 187112158Sdas 188112158Sdasstatic void 189112158Sdasg_disk_start(struct bio *bp) 190112158Sdas{ 191112158Sdas struct bio *bp2, *bp3; 192 struct disk *dp; 193 struct g_ioctl *gio; 194 int error; 195 off_t off; 196 197 dp = bp->bio_to->geom->softc; 198 if (dp == NULL) 199 g_io_deliver(bp, ENXIO); 200 error = EJUSTRETURN; 201 switch(bp->bio_cmd) { 202 case BIO_DELETE: 203 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 204 error = 0; 205 break; 206 } 207 /* fall-through */ 208 case BIO_READ: 209 case BIO_WRITE: 210 off = 0; 211 bp3 = NULL; 212 bp2 = g_clone_bio(bp); 213 if (bp2 == NULL) { 214 error = ENOMEM; 215 break; 216 } 217 devstat_start_transaction_bio(dp->d_devstat, bp); 218 do { 219 bp2->bio_offset += off; 220 bp2->bio_length -= off; 221 bp2->bio_data += off; 222 if (bp2->bio_length > dp->d_maxsize) { 223 /* 224 * XXX: If we have a stripesize we should really 225 * use it here. 226 */ 227 bp2->bio_length = dp->d_maxsize; 228 off += dp->d_maxsize; 229 /* 230 * To avoid a race, we need to grab the next bio 231 * before we schedule this one. See "notes". 232 */ 233 bp3 = g_clone_bio(bp); 234 if (bp3 == NULL) 235 bp->bio_error = ENOMEM; 236 } 237 bp2->bio_done = g_disk_done; 238 bp2->bio_blkno = bp2->bio_offset >> DEV_BSHIFT; 239 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 240 bp2->bio_bcount = bp2->bio_length; 241 bp2->bio_disk = dp; 242 g_disk_lock_giant(dp); 243 dp->d_strategy(bp2); 244 g_disk_unlock_giant(dp); 245 bp2 = bp3; 246 bp3 = NULL; 247 } while (bp2 != NULL); 248 break; 249 case BIO_GETATTR: 250 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors)) 251 break; 252 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 253 break; 254 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 255 break; 256 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 257 g_disk_kerneldump(bp, dp); 258 else if ((g_debugflags & G_F_DISKIOCTL) && 259 (dp->d_ioctl != NULL) && 260 !strcmp(bp->bio_attribute, "GEOM::ioctl") && 261 bp->bio_length == sizeof *gio) { 262 gio = (struct g_ioctl *)bp->bio_data; 263 gio->dev = dp; 264 gio->func = (d_ioctl_t *)(dp->d_ioctl); 265 error = EDIRIOCTL; 266 } else 267 error = ENOIOCTL; 268 break; 269 default: 270 error = EOPNOTSUPP; 271 break; 272 } 273 if (error != EJUSTRETURN) 274 g_io_deliver(bp, error); 275 return; 276} 277 278static void 279g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 280{ 281 struct disk *dp; 282 283 dp = gp->softc; 284 if (indent == NULL) { 285 sbuf_printf(sb, " hd %u", dp->d_fwheads); 286 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 287 return; 288 } 289 if (pp != NULL) { 290 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 291 indent, dp->d_fwheads); 292 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 293 indent, dp->d_fwsectors); 294 } 295} 296 297static void 298g_disk_create(void *arg, int flag __unused) 299{ 300 struct g_geom *gp; 301 struct g_provider *pp; 302 struct disk *dp; 303 304 g_topology_assert(); 305 dp = arg; 306 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 307 gp->start = g_disk_start; 308 gp->access = g_disk_access; 309 gp->softc = dp; 310 gp->dumpconf = g_disk_dumpconf; 311 pp = g_new_providerf(gp, "%s", gp->name); 312 pp->mediasize = dp->d_mediasize; 313 pp->sectorsize = dp->d_sectorsize; 314 if (dp->d_flags & DISKFLAG_CANDELETE) 315 pp->flags |= G_PF_CANDELETE; 316 pp->stripeoffset = dp->d_stripeoffset; 317 pp->stripesize = dp->d_stripesize; 318 if (bootverbose) 319 printf("GEOM: new disk %s\n", gp->name); 320 dp->d_geom = gp; 321 g_error_provider(pp, 0); 322} 323 324 325 326void 327disk_create(int unit, struct disk *dp, int flags, void *unused __unused, void * unused2 __unused) 328{ 329 330 dp->d_unit = unit; 331 dp->d_flags = flags; 332 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 333 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 334 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 335 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 336 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 337 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 338 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 339 dp->d_geom = NULL; 340 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 341} 342 343/* 344 * XXX: There is a race if disk_destroy() is called while the g_disk_create() 345 * XXX: event is running. I belive the current result is that disk_destroy() 346 * XXX: actually doesn't do anything. Considering that the driver owns the 347 * XXX: struct disk and is likely to free it in a few moments, this can 348 * XXX: hardly be said to be optimal. To what extent we can sleep in 349 * XXX: disk_create() and disk_destroy() is currently undefined (but generally 350 * XXX: undesirable) so any solution seems to involve an intrusive decision. 351 */ 352 353static void 354disk_destroy_event(void *ptr, int flag) 355{ 356 357 g_topology_assert(); 358 g_wither_geom(ptr, ENXIO); 359} 360 361void 362disk_destroy(struct disk *dp) 363{ 364 struct g_geom *gp; 365 366 g_cancel_event(dp); 367 gp = dp->d_geom; 368 if (gp == NULL) 369 return; 370 gp->softc = NULL; 371 devstat_remove_entry(dp->d_devstat); 372 g_post_event(disk_destroy_event, gp, M_WAITOK, NULL, NULL); 373} 374 375static void 376g_kern_disks(void *p, int flag __unused) 377{ 378 struct sbuf *sb; 379 struct g_geom *gp; 380 char *sp; 381 382 sb = p; 383 sp = ""; 384 g_topology_assert(); 385 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 386 sbuf_printf(sb, "%s%s", sp, gp->name); 387 sp = " "; 388 } 389 sbuf_finish(sb); 390} 391 392static int 393sysctl_disks(SYSCTL_HANDLER_ARGS) 394{ 395 int error; 396 struct sbuf *sb; 397 398 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 399 sbuf_clear(sb); 400 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 401 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 402 sbuf_delete(sb); 403 return error; 404} 405 406SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 407 sysctl_disks, "A", "names of available disks"); 408 409