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