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