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>
| 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>
|
47#include <sys/ctype.h> 48#include <sys/fcntl.h> 49#include <sys/malloc.h> 50#include <sys/sysctl.h> 51#include <sys/devicestat.h> 52#include <machine/md_var.h> 53 54#include <sys/lock.h> 55#include <sys/mutex.h> 56#include <geom/geom.h> 57#include <geom/geom_disk.h> 58#include <geom/geom_int.h> 59 60static struct mtx g_disk_done_mtx; 61 62static g_access_t g_disk_access; 63static g_init_t g_disk_init; 64static g_fini_t g_disk_fini; 65static g_start_t g_disk_start; 66static g_ioctl_t g_disk_ioctl; 67static g_dumpconf_t g_disk_dumpconf; 68 69static struct g_class g_disk_class = { 70 .name = "DISK", 71 .version = G_VERSION, 72 .init = g_disk_init, 73 .fini = g_disk_fini, 74 .start = g_disk_start, 75 .access = g_disk_access, 76 .ioctl = g_disk_ioctl, 77 .dumpconf = g_disk_dumpconf, 78}; 79 80static void 81g_disk_init(struct g_class *mp __unused) 82{ 83 84 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF); 85} 86 87static void 88g_disk_fini(struct g_class *mp __unused) 89{ 90 91 mtx_destroy(&g_disk_done_mtx); 92} 93 94DECLARE_GEOM_CLASS(g_disk_class, g_disk); 95 96static void __inline 97g_disk_lock_giant(struct disk *dp) 98{ 99 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 100 mtx_lock(&Giant); 101} 102 103static void __inline 104g_disk_unlock_giant(struct disk *dp) 105{ 106 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 107 mtx_unlock(&Giant); 108} 109 110static int 111g_disk_access(struct g_provider *pp, int r, int w, int e) 112{ 113 struct disk *dp; 114 int error; 115 116 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 117 pp->name, r, w, e); 118 g_topology_assert(); 119 dp = pp->geom->softc; 120 if (dp == NULL || dp->d_destroyed) { 121 /* 122 * Allow decreasing access count even if disk is not 123 * avaliable anymore. 124 */ 125 if (r <= 0 && w <= 0 && e <= 0) 126 return (0); 127 return (ENXIO); 128 } 129 r += pp->acr; 130 w += pp->acw; 131 e += pp->ace; 132 error = 0; 133 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 134 if (dp->d_open != NULL) { 135 g_disk_lock_giant(dp); 136 error = dp->d_open(dp); 137 if (bootverbose && error != 0) 138 printf("Opened disk %s -> %d\n", 139 pp->name, error); 140 g_disk_unlock_giant(dp); 141 } 142 pp->mediasize = dp->d_mediasize; 143 pp->sectorsize = dp->d_sectorsize; 144 dp->d_flags |= DISKFLAG_OPEN; 145 if (dp->d_maxsize == 0) { 146 printf("WARNING: Disk drive %s%d has no d_maxsize\n", 147 dp->d_name, dp->d_unit); 148 dp->d_maxsize = DFLTPHYS; 149 } 150 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 151 if (dp->d_close != NULL) { 152 g_disk_lock_giant(dp); 153 error = dp->d_close(dp); 154 if (error != 0) 155 printf("Closed disk %s -> %d\n", 156 pp->name, error); 157 g_disk_unlock_giant(dp); 158 } 159 dp->d_flags &= ~DISKFLAG_OPEN; 160 } 161 return (error); 162} 163 164static void 165g_disk_kerneldump(struct bio *bp, struct disk *dp)
| 46#include <sys/ctype.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 68static struct g_class g_disk_class = { 69 .name = "DISK", 70 .version = G_VERSION, 71 .init = g_disk_init, 72 .fini = g_disk_fini, 73 .start = g_disk_start, 74 .access = g_disk_access, 75 .ioctl = g_disk_ioctl, 76 .dumpconf = g_disk_dumpconf, 77}; 78 79static void 80g_disk_init(struct g_class *mp __unused) 81{ 82 83 mtx_init(&g_disk_done_mtx, "g_disk_done", NULL, MTX_DEF); 84} 85 86static void 87g_disk_fini(struct g_class *mp __unused) 88{ 89 90 mtx_destroy(&g_disk_done_mtx); 91} 92 93DECLARE_GEOM_CLASS(g_disk_class, g_disk); 94 95static void __inline 96g_disk_lock_giant(struct disk *dp) 97{ 98 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 99 mtx_lock(&Giant); 100} 101 102static void __inline 103g_disk_unlock_giant(struct disk *dp) 104{ 105 if (dp->d_flags & DISKFLAG_NEEDSGIANT) 106 mtx_unlock(&Giant); 107} 108 109static int 110g_disk_access(struct g_provider *pp, int r, int w, int e) 111{ 112 struct disk *dp; 113 int error; 114 115 g_trace(G_T_ACCESS, "g_disk_access(%s, %d, %d, %d)", 116 pp->name, r, w, e); 117 g_topology_assert(); 118 dp = pp->geom->softc; 119 if (dp == NULL || dp->d_destroyed) { 120 /* 121 * Allow decreasing access count even if disk is not 122 * avaliable anymore. 123 */ 124 if (r <= 0 && w <= 0 && e <= 0) 125 return (0); 126 return (ENXIO); 127 } 128 r += pp->acr; 129 w += pp->acw; 130 e += pp->ace; 131 error = 0; 132 if ((pp->acr + pp->acw + pp->ace) == 0 && (r + w + e) > 0) { 133 if (dp->d_open != NULL) { 134 g_disk_lock_giant(dp); 135 error = dp->d_open(dp); 136 if (bootverbose && error != 0) 137 printf("Opened disk %s -> %d\n", 138 pp->name, error); 139 g_disk_unlock_giant(dp); 140 } 141 pp->mediasize = dp->d_mediasize; 142 pp->sectorsize = dp->d_sectorsize; 143 dp->d_flags |= DISKFLAG_OPEN; 144 if (dp->d_maxsize == 0) { 145 printf("WARNING: Disk drive %s%d has no d_maxsize\n", 146 dp->d_name, dp->d_unit); 147 dp->d_maxsize = DFLTPHYS; 148 } 149 } else if ((pp->acr + pp->acw + pp->ace) > 0 && (r + w + e) == 0) { 150 if (dp->d_close != NULL) { 151 g_disk_lock_giant(dp); 152 error = dp->d_close(dp); 153 if (error != 0) 154 printf("Closed disk %s -> %d\n", 155 pp->name, error); 156 g_disk_unlock_giant(dp); 157 } 158 dp->d_flags &= ~DISKFLAG_OPEN; 159 } 160 return (error); 161} 162 163static void 164g_disk_kerneldump(struct bio *bp, struct disk *dp)
|
190} 191 192static void 193g_disk_done(struct bio *bp) 194{ 195 struct bio *bp2; 196 struct disk *dp; 197 198 /* See "notes" for why we need a mutex here */ 199 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 200 mtx_lock(&g_disk_done_mtx); 201 bp->bio_completed = bp->bio_length - bp->bio_resid; 202 203 bp2 = bp->bio_parent; 204 if (bp2->bio_error == 0) 205 bp2->bio_error = bp->bio_error; 206 bp2->bio_completed += bp->bio_completed; 207 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) && 208 (dp = bp2->bio_to->geom->softc)) { 209 devstat_end_transaction_bio(dp->d_devstat, bp); 210 } 211 g_destroy_bio(bp); 212 bp2->bio_inbed++; 213 if (bp2->bio_children == bp2->bio_inbed) { 214 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 215 g_io_deliver(bp2, bp2->bio_error); 216 } 217 mtx_unlock(&g_disk_done_mtx); 218} 219 220static int 221g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td) 222{ 223 struct g_geom *gp; 224 struct disk *dp; 225 int error; 226 227 gp = pp->geom; 228 dp = gp->softc; 229 230 if (dp->d_ioctl == NULL) 231 return (ENOIOCTL); 232 g_disk_lock_giant(dp); 233 error = dp->d_ioctl(dp, cmd, data, fflag, td); 234 g_disk_unlock_giant(dp); 235 return(error); 236} 237 238static void 239g_disk_start(struct bio *bp) 240{ 241 struct bio *bp2, *bp3; 242 struct disk *dp; 243 int error; 244 off_t off; 245 246 dp = bp->bio_to->geom->softc; 247 if (dp == NULL || dp->d_destroyed) { 248 g_io_deliver(bp, ENXIO); 249 return; 250 } 251 error = EJUSTRETURN; 252 switch(bp->bio_cmd) { 253 case BIO_DELETE: 254 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 255 error = 0; 256 break; 257 } 258 /* fall-through */ 259 case BIO_READ: 260 case BIO_WRITE: 261 off = 0; 262 bp3 = NULL; 263 bp2 = g_clone_bio(bp); 264 if (bp2 == NULL) { 265 error = ENOMEM; 266 break; 267 } 268 do { 269 bp2->bio_offset += off; 270 bp2->bio_length -= off; 271 bp2->bio_data += off; 272 if (bp2->bio_length > dp->d_maxsize) { 273 /* 274 * XXX: If we have a stripesize we should really 275 * use it here. 276 */ 277 bp2->bio_length = dp->d_maxsize; 278 off += dp->d_maxsize; 279 /* 280 * To avoid a race, we need to grab the next bio 281 * before we schedule this one. See "notes". 282 */ 283 bp3 = g_clone_bio(bp); 284 if (bp3 == NULL) 285 bp->bio_error = ENOMEM; 286 } 287 bp2->bio_done = g_disk_done; 288 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 289 bp2->bio_bcount = bp2->bio_length; 290 bp2->bio_disk = dp; 291 devstat_start_transaction_bio(dp->d_devstat, bp2); 292 g_disk_lock_giant(dp); 293 dp->d_strategy(bp2); 294 g_disk_unlock_giant(dp); 295 bp2 = bp3; 296 bp3 = NULL; 297 } while (bp2 != NULL); 298 break; 299 case BIO_GETATTR: 300 if (g_handleattr_int(bp, "GEOM::candelete", 301 (dp->d_flags & DISKFLAG_CANDELETE) != 0)) 302 break; 303 else if (g_handleattr_int(bp, "GEOM::fwsectors", 304 dp->d_fwsectors)) 305 break; 306 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 307 break; 308 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 309 break; 310 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident)) 311 break; 312 else if (g_handleattr(bp, "GEOM::hba_vendor", 313 &dp->d_hba_vendor, 2)) 314 break; 315 else if (g_handleattr(bp, "GEOM::hba_device", 316 &dp->d_hba_device, 2)) 317 break; 318 else if (g_handleattr(bp, "GEOM::hba_subvendor", 319 &dp->d_hba_subvendor, 2)) 320 break; 321 else if (g_handleattr(bp, "GEOM::hba_subdevice", 322 &dp->d_hba_subdevice, 2)) 323 break; 324 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 325 g_disk_kerneldump(bp, dp); 326 else 327 error = ENOIOCTL; 328 break; 329 case BIO_FLUSH: 330 g_trace(G_T_TOPOLOGY, "g_disk_flushcache(%s)", 331 bp->bio_to->name); 332 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) { 333 g_io_deliver(bp, ENODEV); 334 return; 335 } 336 bp2 = g_clone_bio(bp); 337 if (bp2 == NULL) { 338 g_io_deliver(bp, ENOMEM); 339 return; 340 } 341 bp2->bio_done = g_disk_done; 342 bp2->bio_disk = dp; 343 g_disk_lock_giant(dp); 344 dp->d_strategy(bp2); 345 g_disk_unlock_giant(dp); 346 break; 347 default: 348 error = EOPNOTSUPP; 349 break; 350 } 351 if (error != EJUSTRETURN) 352 g_io_deliver(bp, error); 353 return; 354} 355 356static void 357g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 358{ 359 struct disk *dp; 360 361 dp = gp->softc; 362 if (dp == NULL) 363 return; 364 if (indent == NULL) { 365 sbuf_printf(sb, " hd %u", dp->d_fwheads); 366 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 367 return; 368 } 369 if (pp != NULL) { 370 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 371 indent, dp->d_fwheads); 372 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 373 indent, dp->d_fwsectors); 374 sbuf_printf(sb, "%s<ident>%s</ident>\n", indent, dp->d_ident); 375 sbuf_printf(sb, "%s<descr>%s</descr>\n", indent, dp->d_descr); 376 } 377} 378 379static void 380g_disk_create(void *arg, int flag) 381{ 382 struct g_geom *gp; 383 struct g_provider *pp; 384 struct disk *dp; 385 386 if (flag == EV_CANCEL) 387 return; 388 g_topology_assert(); 389 dp = arg; 390 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 391 gp->softc = dp; 392 pp = g_new_providerf(gp, "%s", gp->name); 393 pp->mediasize = dp->d_mediasize; 394 pp->sectorsize = dp->d_sectorsize; 395 if (dp->d_flags & DISKFLAG_CANDELETE) 396 pp->flags |= G_PF_CANDELETE; 397 pp->stripeoffset = dp->d_stripeoffset; 398 pp->stripesize = dp->d_stripesize; 399 if (bootverbose) 400 printf("GEOM: new disk %s\n", gp->name); 401 dp->d_geom = gp; 402 g_error_provider(pp, 0); 403} 404 405static void 406g_disk_destroy(void *ptr, int flag) 407{ 408 struct disk *dp; 409 struct g_geom *gp; 410 411 g_topology_assert(); 412 dp = ptr; 413 gp = dp->d_geom; 414 if (gp != NULL) { 415 gp->softc = NULL; 416 g_wither_geom(gp, ENXIO); 417 } 418 g_free(dp); 419} 420 421/* 422 * We only allow printable characters in disk ident, 423 * the rest is converted to 'x<HH>'. 424 */ 425static void 426g_disk_ident_adjust(char *ident, size_t size) 427{ 428 char *p, tmp[4], newid[DISK_IDENT_SIZE]; 429 430 newid[0] = '\0'; 431 for (p = ident; *p != '\0'; p++) { 432 if (isprint(*p)) { 433 tmp[0] = *p; 434 tmp[1] = '\0'; 435 } else { 436 snprintf(tmp, sizeof(tmp), "x%02hhx", 437 *(unsigned char *)p); 438 } 439 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid)) 440 break; 441 } 442 bzero(ident, size); 443 strlcpy(ident, newid, size); 444} 445 446struct disk * 447disk_alloc() 448{ 449 struct disk *dp; 450 451 dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO); 452 return (dp); 453} 454 455void 456disk_create(struct disk *dp, int version) 457{ 458 if (version != DISK_VERSION_00 && version != DISK_VERSION_01) { 459 printf("WARNING: Attempt to add disk %s%d %s", 460 dp->d_name, dp->d_unit, 461 " using incompatible ABI version of disk(9)\n"); 462 printf("WARNING: Ignoring disk %s%d\n", 463 dp->d_name, dp->d_unit); 464 return; 465 } 466 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 467 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 468 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 469 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 470 if (dp->d_devstat == NULL) 471 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 472 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 473 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 474 dp->d_geom = NULL; 475 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident)); 476 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 477} 478 479void 480disk_destroy(struct disk *dp) 481{ 482 483 g_cancel_event(dp); 484 dp->d_destroyed = 1; 485 if (dp->d_devstat != NULL) 486 devstat_remove_entry(dp->d_devstat); 487 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL); 488} 489 490void 491disk_gone(struct disk *dp) 492{ 493 struct g_geom *gp; 494 struct g_provider *pp; 495 496 gp = dp->d_geom; 497 if (gp != NULL) 498 LIST_FOREACH(pp, &gp->provider, provider) 499 g_wither_provider(pp, ENXIO); 500} 501 502static void 503g_kern_disks(void *p, int flag __unused) 504{ 505 struct sbuf *sb; 506 struct g_geom *gp; 507 char *sp; 508 509 sb = p; 510 sp = ""; 511 g_topology_assert(); 512 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 513 sbuf_printf(sb, "%s%s", sp, gp->name); 514 sp = " "; 515 } 516 sbuf_finish(sb); 517} 518 519static int 520sysctl_disks(SYSCTL_HANDLER_ARGS) 521{ 522 int error; 523 struct sbuf *sb; 524 525 sb = sbuf_new_auto(); 526 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 527 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 528 sbuf_delete(sb); 529 return error; 530} 531 532SYSCTL_PROC(_kern, OID_AUTO, disks, 533 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 534 sysctl_disks, "A", "names of available disks"); 535
| 186} 187 188static void 189g_disk_done(struct bio *bp) 190{ 191 struct bio *bp2; 192 struct disk *dp; 193 194 /* See "notes" for why we need a mutex here */ 195 /* XXX: will witness accept a mix of Giant/unGiant drivers here ? */ 196 mtx_lock(&g_disk_done_mtx); 197 bp->bio_completed = bp->bio_length - bp->bio_resid; 198 199 bp2 = bp->bio_parent; 200 if (bp2->bio_error == 0) 201 bp2->bio_error = bp->bio_error; 202 bp2->bio_completed += bp->bio_completed; 203 if ((bp->bio_cmd & (BIO_READ|BIO_WRITE|BIO_DELETE)) && 204 (dp = bp2->bio_to->geom->softc)) { 205 devstat_end_transaction_bio(dp->d_devstat, bp); 206 } 207 g_destroy_bio(bp); 208 bp2->bio_inbed++; 209 if (bp2->bio_children == bp2->bio_inbed) { 210 bp2->bio_resid = bp2->bio_bcount - bp2->bio_completed; 211 g_io_deliver(bp2, bp2->bio_error); 212 } 213 mtx_unlock(&g_disk_done_mtx); 214} 215 216static int 217g_disk_ioctl(struct g_provider *pp, u_long cmd, void * data, int fflag, struct thread *td) 218{ 219 struct g_geom *gp; 220 struct disk *dp; 221 int error; 222 223 gp = pp->geom; 224 dp = gp->softc; 225 226 if (dp->d_ioctl == NULL) 227 return (ENOIOCTL); 228 g_disk_lock_giant(dp); 229 error = dp->d_ioctl(dp, cmd, data, fflag, td); 230 g_disk_unlock_giant(dp); 231 return(error); 232} 233 234static void 235g_disk_start(struct bio *bp) 236{ 237 struct bio *bp2, *bp3; 238 struct disk *dp; 239 int error; 240 off_t off; 241 242 dp = bp->bio_to->geom->softc; 243 if (dp == NULL || dp->d_destroyed) { 244 g_io_deliver(bp, ENXIO); 245 return; 246 } 247 error = EJUSTRETURN; 248 switch(bp->bio_cmd) { 249 case BIO_DELETE: 250 if (!(dp->d_flags & DISKFLAG_CANDELETE)) { 251 error = 0; 252 break; 253 } 254 /* fall-through */ 255 case BIO_READ: 256 case BIO_WRITE: 257 off = 0; 258 bp3 = NULL; 259 bp2 = g_clone_bio(bp); 260 if (bp2 == NULL) { 261 error = ENOMEM; 262 break; 263 } 264 do { 265 bp2->bio_offset += off; 266 bp2->bio_length -= off; 267 bp2->bio_data += off; 268 if (bp2->bio_length > dp->d_maxsize) { 269 /* 270 * XXX: If we have a stripesize we should really 271 * use it here. 272 */ 273 bp2->bio_length = dp->d_maxsize; 274 off += dp->d_maxsize; 275 /* 276 * To avoid a race, we need to grab the next bio 277 * before we schedule this one. See "notes". 278 */ 279 bp3 = g_clone_bio(bp); 280 if (bp3 == NULL) 281 bp->bio_error = ENOMEM; 282 } 283 bp2->bio_done = g_disk_done; 284 bp2->bio_pblkno = bp2->bio_offset / dp->d_sectorsize; 285 bp2->bio_bcount = bp2->bio_length; 286 bp2->bio_disk = dp; 287 devstat_start_transaction_bio(dp->d_devstat, bp2); 288 g_disk_lock_giant(dp); 289 dp->d_strategy(bp2); 290 g_disk_unlock_giant(dp); 291 bp2 = bp3; 292 bp3 = NULL; 293 } while (bp2 != NULL); 294 break; 295 case BIO_GETATTR: 296 if (g_handleattr_int(bp, "GEOM::candelete", 297 (dp->d_flags & DISKFLAG_CANDELETE) != 0)) 298 break; 299 else if (g_handleattr_int(bp, "GEOM::fwsectors", 300 dp->d_fwsectors)) 301 break; 302 else if (g_handleattr_int(bp, "GEOM::fwheads", dp->d_fwheads)) 303 break; 304 else if (g_handleattr_off_t(bp, "GEOM::frontstuff", 0)) 305 break; 306 else if (g_handleattr_str(bp, "GEOM::ident", dp->d_ident)) 307 break; 308 else if (g_handleattr(bp, "GEOM::hba_vendor", 309 &dp->d_hba_vendor, 2)) 310 break; 311 else if (g_handleattr(bp, "GEOM::hba_device", 312 &dp->d_hba_device, 2)) 313 break; 314 else if (g_handleattr(bp, "GEOM::hba_subvendor", 315 &dp->d_hba_subvendor, 2)) 316 break; 317 else if (g_handleattr(bp, "GEOM::hba_subdevice", 318 &dp->d_hba_subdevice, 2)) 319 break; 320 else if (!strcmp(bp->bio_attribute, "GEOM::kerneldump")) 321 g_disk_kerneldump(bp, dp); 322 else 323 error = ENOIOCTL; 324 break; 325 case BIO_FLUSH: 326 g_trace(G_T_TOPOLOGY, "g_disk_flushcache(%s)", 327 bp->bio_to->name); 328 if (!(dp->d_flags & DISKFLAG_CANFLUSHCACHE)) { 329 g_io_deliver(bp, ENODEV); 330 return; 331 } 332 bp2 = g_clone_bio(bp); 333 if (bp2 == NULL) { 334 g_io_deliver(bp, ENOMEM); 335 return; 336 } 337 bp2->bio_done = g_disk_done; 338 bp2->bio_disk = dp; 339 g_disk_lock_giant(dp); 340 dp->d_strategy(bp2); 341 g_disk_unlock_giant(dp); 342 break; 343 default: 344 error = EOPNOTSUPP; 345 break; 346 } 347 if (error != EJUSTRETURN) 348 g_io_deliver(bp, error); 349 return; 350} 351 352static void 353g_disk_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp) 354{ 355 struct disk *dp; 356 357 dp = gp->softc; 358 if (dp == NULL) 359 return; 360 if (indent == NULL) { 361 sbuf_printf(sb, " hd %u", dp->d_fwheads); 362 sbuf_printf(sb, " sc %u", dp->d_fwsectors); 363 return; 364 } 365 if (pp != NULL) { 366 sbuf_printf(sb, "%s<fwheads>%u</fwheads>\n", 367 indent, dp->d_fwheads); 368 sbuf_printf(sb, "%s<fwsectors>%u</fwsectors>\n", 369 indent, dp->d_fwsectors); 370 sbuf_printf(sb, "%s<ident>%s</ident>\n", indent, dp->d_ident); 371 sbuf_printf(sb, "%s<descr>%s</descr>\n", indent, dp->d_descr); 372 } 373} 374 375static void 376g_disk_create(void *arg, int flag) 377{ 378 struct g_geom *gp; 379 struct g_provider *pp; 380 struct disk *dp; 381 382 if (flag == EV_CANCEL) 383 return; 384 g_topology_assert(); 385 dp = arg; 386 gp = g_new_geomf(&g_disk_class, "%s%d", dp->d_name, dp->d_unit); 387 gp->softc = dp; 388 pp = g_new_providerf(gp, "%s", gp->name); 389 pp->mediasize = dp->d_mediasize; 390 pp->sectorsize = dp->d_sectorsize; 391 if (dp->d_flags & DISKFLAG_CANDELETE) 392 pp->flags |= G_PF_CANDELETE; 393 pp->stripeoffset = dp->d_stripeoffset; 394 pp->stripesize = dp->d_stripesize; 395 if (bootverbose) 396 printf("GEOM: new disk %s\n", gp->name); 397 dp->d_geom = gp; 398 g_error_provider(pp, 0); 399} 400 401static void 402g_disk_destroy(void *ptr, int flag) 403{ 404 struct disk *dp; 405 struct g_geom *gp; 406 407 g_topology_assert(); 408 dp = ptr; 409 gp = dp->d_geom; 410 if (gp != NULL) { 411 gp->softc = NULL; 412 g_wither_geom(gp, ENXIO); 413 } 414 g_free(dp); 415} 416 417/* 418 * We only allow printable characters in disk ident, 419 * the rest is converted to 'x<HH>'. 420 */ 421static void 422g_disk_ident_adjust(char *ident, size_t size) 423{ 424 char *p, tmp[4], newid[DISK_IDENT_SIZE]; 425 426 newid[0] = '\0'; 427 for (p = ident; *p != '\0'; p++) { 428 if (isprint(*p)) { 429 tmp[0] = *p; 430 tmp[1] = '\0'; 431 } else { 432 snprintf(tmp, sizeof(tmp), "x%02hhx", 433 *(unsigned char *)p); 434 } 435 if (strlcat(newid, tmp, sizeof(newid)) >= sizeof(newid)) 436 break; 437 } 438 bzero(ident, size); 439 strlcpy(ident, newid, size); 440} 441 442struct disk * 443disk_alloc() 444{ 445 struct disk *dp; 446 447 dp = g_malloc(sizeof *dp, M_WAITOK | M_ZERO); 448 return (dp); 449} 450 451void 452disk_create(struct disk *dp, int version) 453{ 454 if (version != DISK_VERSION_00 && version != DISK_VERSION_01) { 455 printf("WARNING: Attempt to add disk %s%d %s", 456 dp->d_name, dp->d_unit, 457 " using incompatible ABI version of disk(9)\n"); 458 printf("WARNING: Ignoring disk %s%d\n", 459 dp->d_name, dp->d_unit); 460 return; 461 } 462 KASSERT(dp->d_strategy != NULL, ("disk_create need d_strategy")); 463 KASSERT(dp->d_name != NULL, ("disk_create need d_name")); 464 KASSERT(*dp->d_name != 0, ("disk_create need d_name")); 465 KASSERT(strlen(dp->d_name) < SPECNAMELEN - 4, ("disk name too long")); 466 if (dp->d_devstat == NULL) 467 dp->d_devstat = devstat_new_entry(dp->d_name, dp->d_unit, 468 dp->d_sectorsize, DEVSTAT_ALL_SUPPORTED, 469 DEVSTAT_TYPE_DIRECT, DEVSTAT_PRIORITY_MAX); 470 dp->d_geom = NULL; 471 g_disk_ident_adjust(dp->d_ident, sizeof(dp->d_ident)); 472 g_post_event(g_disk_create, dp, M_WAITOK, dp, NULL); 473} 474 475void 476disk_destroy(struct disk *dp) 477{ 478 479 g_cancel_event(dp); 480 dp->d_destroyed = 1; 481 if (dp->d_devstat != NULL) 482 devstat_remove_entry(dp->d_devstat); 483 g_post_event(g_disk_destroy, dp, M_WAITOK, NULL); 484} 485 486void 487disk_gone(struct disk *dp) 488{ 489 struct g_geom *gp; 490 struct g_provider *pp; 491 492 gp = dp->d_geom; 493 if (gp != NULL) 494 LIST_FOREACH(pp, &gp->provider, provider) 495 g_wither_provider(pp, ENXIO); 496} 497 498static void 499g_kern_disks(void *p, int flag __unused) 500{ 501 struct sbuf *sb; 502 struct g_geom *gp; 503 char *sp; 504 505 sb = p; 506 sp = ""; 507 g_topology_assert(); 508 LIST_FOREACH(gp, &g_disk_class.geom, geom) { 509 sbuf_printf(sb, "%s%s", sp, gp->name); 510 sp = " "; 511 } 512 sbuf_finish(sb); 513} 514 515static int 516sysctl_disks(SYSCTL_HANDLER_ARGS) 517{ 518 int error; 519 struct sbuf *sb; 520 521 sb = sbuf_new_auto(); 522 g_waitfor_event(g_kern_disks, sb, M_WAITOK, NULL); 523 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 524 sbuf_delete(sb); 525 return error; 526} 527 528SYSCTL_PROC(_kern, OID_AUTO, disks, 529 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 530 sysctl_disks, "A", "names of available disks"); 531
|