geom_disk.c revision 133314
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 36#include <sys/cdefs.h> 37__FBSDID("$FreeBSD: head/sys/geom/geom_disk.c 133314 2004-08-08 06:49:07Z phk $"); 38 39#include "opt_geom.h" 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/fcntl.h> 48#include <sys/malloc.h> 49#include <sys/sysctl.h> 50#include <sys/devicestat.h> 51#include <machine/md_var.h> 52 53#include <sys/lock.h> 54#include <sys/mutex.h> 55#include <geom/geom.h> 56#include <geom/geom_disk.h> 57#include <geom/geom_int.h> 58 59static struct mtx g_disk_done_mtx; 60 61static g_access_t g_disk_access; 62static g_init_t g_disk_init; 63static g_fini_t g_disk_fini; 64static g_start_t g_disk_start; 65static g_ioctl_t g_disk_ioctl; 66static g_dumpconf_t g_disk_dumpconf; 67 68struct g_class g_disk_class = { 69 .name = "DISK", 70 .init = g_disk_init, 71 .fini = g_disk_fini, 72 .start = g_disk_start, 73 .access = g_disk_access, 74 .ioctl = g_disk_ioctl, 75 .dumpconf = g_disk_dumpconf, 76}; 77 78static void 79g_disk_init(struct g_class *mp __unused) 80{ 81 82 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF); 83} 84 85static void 86g_disk_fini(struct g_class *mp __unused) 87{ 88 89 mtx_destroy(&g_disk_done_mtx); 90} 91 92DECLARE_GEOM_CLASS(g_disk_class, g_disk); 93 94static void __inline 95g_disk_lock_giant(struct disk *dp) 96{ 97 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 98 mtx_lock(&Giant); 99} 100 101static void __inline 102g_disk_unlock_giant(struct disk *dp) 103{ 104 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 105 mtx_unlock(&Giant); 106} 107 108static int 109g_disk_access(struct g_provider *pp, int r, int w, int e) 110{ 111 struct disk *dp; 112 int error; 113 114 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 115 pp->name, r, w, e); 116 g_topology_assert(); 117 dp = pp->geom->softc; 118 if (dp == NULL || dp->d_destroyed) { 119 /* 120 * Allow decreasing access count even if disk is not 121 * avaliable anymore. 122 */ 123 if (r <= 0 && w <= 0 && e <= 0) 124 return (0); 125 return (ENXIO); 126 } 127 r += pp->acr; 128 w += pp->acw; 129 e += pp->ace; 130 error = 0; 131 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 132 if (dp->d_open != NULL) { 133 g_disk_lock_giant(dp); 134 error = dp->d_open(dp); 135 if (error != 0) 136 printf("Opened disk %s -> %d\n", 137 pp->name, error); 138 g_disk_unlock_giant(dp); 139 } 140 pp->mediasize = dp->d_mediasize; 141 pp->sectorsize = dp->d_sectorsize; 142 dp->d_flags |= DISKFLAG_OPEN; 143 if (dp->d_maxsize == 0) { 144 printf("WARNING: Disk drive %s%d has no d_maxsize\n", 145 dp->d_name, dp->d_unit); 146 dp->d_maxsize = DFLTPHYS; 147 } 148 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 149 if (dp->d_close != NULL) { 150 g_disk_lock_giant(dp); 151 error = dp->d_close(dp); 152 if (error != 0) 153 printf("Closed disk %s -> %d\n", 154 pp->name, error); 155 g_disk_unlock_giant(dp); 156 } 157 dp->d_flags &= ~DISKFLAG_OPEN; 158 } 159 return (error); 160} 161 162static void 163g_disk_kerneldump(struct bio *bp, struct disk *dp) 164{ 165 int error; 166 struct g_kerneldump *gkd; 167 struct dumperinfo di; 168 struct g_geom *gp; 169 170 gkd = (struct g_kerneldump*)bp->bio_data; 171 gp = bp->bio_to->geom; 172 g_trace(G_T_TOPOLOGY, "g_disk_kernedump(%s, %jd, %jd)", 173 gp->name, (intmax_t)gkd->offset, (intmax_t)gkd->length); 174 if (dp->d_dump == NULL) { 175 g_io_deliver(bp, ENODEV); 176 return; 177 } 178 di.dumper = dp->d_dump; 179 di.priv = dp; 180 di.blocksize = dp->d_sectorsize; 181 di.mediaoffset = gkd->offset; 182 di.mediasize = gkd->length; 183 error = set_dumper(&di); 184 g_io_deliver(bp, error); 185} 186 187static void 188g_disk_done(struct bio *bp) 189{ 190 struct bio *bp2; 191 struct disk *dp; 192 193 /* See "notes" for why we need a mutex here */ 194 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 195 mtx_lock(&g_disk_done_mtx); 196 bp->bio_completed = bp->bio_length - bp->bio_resid; 197 198 bp2 = bp->bio_parent; 199 if (bp2->bio_error == 0) 200 bp2->bio_error = bp->bio_error; 201 bp2->bio_completed += bp->bio_completed; 202 g_destroy_bio(bp); 203 bp2->bio_inbed++; 204 if (bp2->bio_children == bp2->bio_inbed) { 205 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 206 if ((dp = bp2->bio_to->geom->softc)) 207 devstat_end_transaction_bio(dp->d_devstat, bp2); 208 g_io_deliver(bp2, bp2->bio_error); 209 } 210 mtx_unlock(&g_disk_done_mtx); 211} 212 213static int 214g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, struct thread *td) 215{ 216 struct g_geom *gp; 217 struct disk *dp; 218 int error; 219 220 gp = pp->geom; 221 dp = gp->softc; 222 223 if (dp->d_ioctl == NULL) 224 return (ENOIOCTL); 225 g_disk_lock_giant(dp); 226 error = dp->d_ioctl(dp, cmd, data, 0, td); 227 g_disk_unlock_giant(dp); 228 return(error); 229} 230 231static void 232g_disk_start(struct bio *bp) 233{ 234 struct bio *bp2, *bp3; 235 struct disk *dp; 236 int error; 237 off_t off; 238 239 dp = bp->bio_to->geom->softc; 240 if (dp == NULL || dp->d_destroyed) 241 g_io_deliver(bp, ENXIO); 242 error = EJUSTRETURN; 243 switch(bp->bio_cmd) { 244 case BIO_DELETE: 245 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 246 error = 0; 247 break; 248 } 249 /* fall-through */ 250 case BIO_READ: 251 case BIO_WRITE: 252 off = 0; 253 bp3 = NULL; 254 bp2 = g_clone_bio(bp); 255 if (bp2 == NULL) { 256 error = ENOMEM; 257 break; 258 } 259 devstat_start_transaction_bio(dp->d_devstat, bp); 260 do { 261 bp2->bio_offset += off; 262 bp2->bio_length -= off; 263 bp2->bio_data += off; 264 if (bp2->bio_length > dp->d_maxsize) { 265 /* 266 * XXX: If we have a stripesize we should really 267 * use it here. 268 */ 269 bp2->bio_length = dp->d_maxsize; 270 off += dp->d_maxsize; 271 /* 272 * To avoid a race, we need to grab the next bio 273 * before we schedule this one. See "notes". 274 */ 275 bp3 = g_clone_bio(bp); 276 if (bp3 == NULL) 277 bp->bio_error = ENOMEM; 278 } 279 bp2->bio_done = g_disk_done; 280 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 281 bp2->bio_bcount = bp2->bio_length; 282 bp2->bio_disk = dp; 283 g_disk_lock_giant(dp); 284 dp->d_strategy(bp2); 285 g_disk_unlock_giant(dp); 286 bp2 = bp3; 287 bp3 = NULL; 288 } while (bp2 != NULL); 289 break; 290 case BIO_GETATTR: 291 if (g_handleattr_int(bp, "GEOM::fwsectors", dp->d_fwsectors)) 292 break; 293 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 294 break; 295 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 296 break; 297 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 298 g_disk_kerneldump(bp, dp); 299 else 300 error = ENOIOCTL; 301 break; 302 default: 303 error = EOPNOTSUPP; 304 break; 305 } 306 if (error != EJUSTRETURN) 307 g_io_deliver(bp, error); 308 return; 309} 310 311static void 312g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 313{ 314 struct disk *dp; 315 316 dp = gp->softc; 317 if (dp == NULL) 318 return; 319 if (indent == NULL) { 320 sbuf_printf(sb, " hd %u", dp->d_fwheads); 321 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 322 return; 323 } 324 if (pp != NULL) { 325 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 326 indent, dp->d_fwheads); 327 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 328 indent, dp->d_fwsectors); 329 } 330} 331 332static void 333g_disk_create(void *arg, int flag) 334{ 335 struct g_geom *gp; 336 struct g_provider *pp; 337 struct disk *dp; 338 339 if (flag == EV_CANCEL) 340 return; 341 g_topology_assert(); 342 dp = arg; 343 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 344 gp->softc = dp; 345 pp = g_new_providerf(gp, "%s", gp->name); 346 pp->mediasize = dp->d_mediasize; 347 pp->sectorsize = dp->d_sectorsize; 348 if (dp->d_flags & DISKFLAG_CANDELETE) 349 pp->flags |= G_PF_CANDELETE; 350 pp->stripeoffset = dp->d_stripeoffset; 351 pp->stripesize = dp->d_stripesize; 352 if (bootverbose) 353 printf("GEOM: new disk %s\n", gp->name); 354 dp->d_geom = gp; 355 g_error_provider(pp, 0); 356} 357 358static void 359g_disk_destroy(void *ptr, int flag) 360{ 361 struct disk *dp; 362 struct g_geom *gp; 363 364 g_topology_assert(); 365 dp = ptr; 366 gp = dp->d_geom; 367 gp->softc = NULL; 368 g_wither_geom(gp, ENXIO); 369 g_free(dp); 370} 371 372struct disk * 373disk_alloc() 374{ 375 struct disk *dp; 376 377 dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO); 378 return (dp); 379} 380 381void 382disk_create(struct disk *dp, int version) 383{ 384 if (version != DISK_VERSION_00) { 385 printf("WARNING: Attempt to add disk %s%d %s", 386 dp->d_name, dp->d_unit, 387 " using incompatible ABI version of disk(9)\n"); 388 printf("WARNING: Ignoring disk %s%d\n", 389 dp->d_name, dp->d_unit); 390 return; 391 } 392 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 393 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 394 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 395 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 396 if (dp->d_devstat == NULL) 397 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 398 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 399 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 400 dp->d_geom = NULL; 401 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 402} 403 404void 405disk_destroy(struct disk *dp) 406{ 407 408 g_cancel_event(dp); 409 dp->d_destroyed = 1; 410 if (dp->d_devstat != NULL) 411 devstat_remove_entry(dp->d_devstat); 412 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL); 413} 414 415static void 416g_kern_disks(void *p, int flag __unused) 417{ 418 struct sbuf *sb; 419 struct g_geom *gp; 420 char *sp; 421 422 sb = p; 423 sp = ""; 424 g_topology_assert(); 425 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 426 sbuf_printf(sb, "%s%s", sp, gp->name); 427 sp = " "; 428 } 429 sbuf_finish(sb); 430} 431 432static int 433sysctl_disks(SYSCTL_HANDLER_ARGS) 434{ 435 int error; 436 struct sbuf *sb; 437 438 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 439 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 440 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 441 sbuf_delete(sb); 442 return error; 443} 444 445SYSCTL_PROC(_kern, OID_AUTO, disks, CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NOLOCK, 0, 0, 446 sysctl_disks, "A", "names of available disks"); 447 448