1 /* $NetBSD: dk.c,v 1.43 2009/01/13 13:35:53 yamt Exp $ */ 2 3 /*- 4 * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.43 2009/01/13 13:35:53 yamt Exp $"); 34 35 #include "opt_dkwedge.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/proc.h> 40 #include <sys/errno.h> 41 #include <sys/pool.h> 42 #include <sys/ioctl.h> 43 #include <sys/disklabel.h> 44 #include <sys/disk.h> 45 #include <sys/fcntl.h> 46 #include <sys/buf.h> 47 #include <sys/bufq.h> 48 #include <sys/vnode.h> 49 #include <sys/stat.h> 50 #include <sys/conf.h> 51 #include <sys/callout.h> 52 #include <sys/kernel.h> 53 #include <sys/malloc.h> 54 #include <sys/device.h> 55 #include <sys/kauth.h> 56 57 #include <miscfs/specfs/specdev.h> 58 59 MALLOC_DEFINE(M_DKWEDGE, "dkwedge", "Disk wedge structures"); 60 61 typedef enum { 62 DKW_STATE_LARVAL = 0, 63 DKW_STATE_RUNNING = 1, 64 DKW_STATE_DYING = 2, 65 DKW_STATE_DEAD = 666 66 } dkwedge_state_t; 67 68 struct dkwedge_softc { 69 struct device *sc_dev; /* pointer to our pseudo-device */ 70 struct cfdata sc_cfdata; /* our cfdata structure */ 71 uint8_t sc_wname[128]; /* wedge name (Unicode, UTF-8) */ 72 73 dkwedge_state_t sc_state; /* state this wedge is in */ 74 75 struct disk *sc_parent; /* parent disk */ 76 daddr_t sc_offset; /* LBA offset of wedge in parent */ 77 uint64_t sc_size; /* size of wedge in blocks */ 78 char sc_ptype[32]; /* partition type */ 79 dev_t sc_pdev; /* cached parent's dev_t */ 80 /* link on parent's wedge list */ 81 LIST_ENTRY(dkwedge_softc) sc_plink; 82 83 struct disk sc_dk; /* our own disk structure */ 84 struct bufq_state *sc_bufq; /* buffer queue */ 85 struct callout sc_restart_ch; /* callout to restart I/O */ 86 87 u_int sc_iopend; /* I/Os pending */ 88 int sc_flags; /* flags (splbio) */ 89 }; 90 91 #define DK_F_WAIT_DRAIN 0x0001 /* waiting for I/O to drain */ 92 93 static void dkstart(struct dkwedge_softc *); 94 static void dkiodone(struct buf *); 95 static void dkrestart(void *); 96 97 static dev_type_open(dkopen); 98 static dev_type_close(dkclose); 99 static dev_type_read(dkread); 100 static dev_type_write(dkwrite); 101 static dev_type_ioctl(dkioctl); 102 static dev_type_strategy(dkstrategy); 103 static dev_type_dump(dkdump); 104 static dev_type_size(dksize); 105 106 const struct bdevsw dk_bdevsw = { 107 dkopen, dkclose, dkstrategy, dkioctl, dkdump, dksize, D_DISK 108 }; 109 110 const struct cdevsw dk_cdevsw = { 111 dkopen, dkclose, dkread, dkwrite, dkioctl, 112 nostop, notty, nopoll, nommap, nokqfilter, D_DISK 113 }; 114 115 static struct dkwedge_softc **dkwedges; 116 static u_int ndkwedges; 117 static krwlock_t dkwedges_lock; 118 119 static LIST_HEAD(, dkwedge_discovery_method) dkwedge_discovery_methods; 120 static krwlock_t dkwedge_discovery_methods_lock; 121 122 /* 123 * dkwedge_match: 124 * 125 * Autoconfiguration match function for pseudo-device glue. 126 */ 127 static int 128 dkwedge_match(struct device *parent, struct cfdata *match, 129 void *aux) 130 { 131 132 /* Pseudo-device; always present. */ 133 return (1); 134 } 135 136 /* 137 * dkwedge_attach: 138 * 139 * Autoconfiguration attach function for pseudo-device glue. 140 */ 141 static void 142 dkwedge_attach(struct device *parent, struct device *self, 143 void *aux) 144 { 145 146 if (!pmf_device_register(self, NULL, NULL)) 147 aprint_error_dev(self, "couldn't establish power handler\n"); 148 } 149 150 /* 151 * dkwedge_detach: 152 * 153 * Autoconfiguration detach function for pseudo-device glue. 154 */ 155 static int 156 dkwedge_detach(struct device *self, int flags) 157 { 158 159 pmf_device_deregister(self); 160 /* Always succeeds. */ 161 return (0); 162 } 163 164 CFDRIVER_DECL(dk, DV_DISK, NULL); 165 CFATTACH_DECL_NEW(dk, 0, 166 dkwedge_match, dkwedge_attach, dkwedge_detach, NULL); 167 168 /* 169 * dkwedge_wait_drain: 170 * 171 * Wait for I/O on the wedge to drain. 172 * NOTE: Must be called at splbio()! 173 */ 174 static void 175 dkwedge_wait_drain(struct dkwedge_softc *sc) 176 { 177 178 while (sc->sc_iopend != 0) { 179 sc->sc_flags |= DK_F_WAIT_DRAIN; 180 (void) tsleep(&sc->sc_iopend, PRIBIO, "dkdrn", 0); 181 } 182 } 183 184 /* 185 * dkwedge_compute_pdev: 186 * 187 * Compute the parent disk's dev_t. 188 */ 189 static int 190 dkwedge_compute_pdev(const char *pname, dev_t *pdevp) 191 { 192 const char *name, *cp; 193 int punit, pmaj; 194 char devname[16]; 195 196 name = pname; 197 if ((pmaj = devsw_name2blk(name, devname, sizeof(devname))) == -1) 198 return (ENODEV); 199 200 name += strlen(devname); 201 for (cp = name, punit = 0; *cp >= '0' && *cp <= '9'; cp++) 202 punit = (punit * 10) + (*cp - '0'); 203 if (cp == name) { 204 /* Invalid parent disk name. */ 205 return (ENODEV); 206 } 207 208 *pdevp = MAKEDISKDEV(pmaj, punit, RAW_PART); 209 210 return (0); 211 } 212 213 /* 214 * dkwedge_array_expand: 215 * 216 * Expand the dkwedges array. 217 */ 218 static void 219 dkwedge_array_expand(void) 220 { 221 int newcnt = ndkwedges + 16; 222 struct dkwedge_softc **newarray, **oldarray; 223 224 newarray = malloc(newcnt * sizeof(*newarray), M_DKWEDGE, 225 M_WAITOK|M_ZERO); 226 if ((oldarray = dkwedges) != NULL) 227 memcpy(newarray, dkwedges, ndkwedges * sizeof(*newarray)); 228 dkwedges = newarray; 229 ndkwedges = newcnt; 230 if (oldarray != NULL) 231 free(oldarray, M_DKWEDGE); 232 } 233 234 /* 235 * dkwedge_add: [exported function] 236 * 237 * Add a disk wedge based on the provided information. 238 * 239 * The incoming dkw_devname[] is ignored, instead being 240 * filled in and returned to the caller. 241 */ 242 int 243 dkwedge_add(struct dkwedge_info *dkw) 244 { 245 struct dkwedge_softc *sc, *lsc; 246 struct disk *pdk; 247 u_int unit; 248 int error; 249 dev_t pdev; 250 251 dkw->dkw_parent[sizeof(dkw->dkw_parent) - 1] = '\0'; 252 pdk = disk_find(dkw->dkw_parent); 253 if (pdk == NULL) 254 return (ENODEV); 255 256 error = dkwedge_compute_pdev(pdk->dk_name, &pdev); 257 if (error) 258 return (error); 259 260 if (dkw->dkw_offset < 0) 261 return (EINVAL); 262 263 sc = malloc(sizeof(*sc), M_DKWEDGE, M_WAITOK|M_ZERO); 264 sc->sc_state = DKW_STATE_LARVAL; 265 sc->sc_parent = pdk; 266 sc->sc_pdev = pdev; 267 sc->sc_offset = dkw->dkw_offset; 268 sc->sc_size = dkw->dkw_size; 269 270 memcpy(sc->sc_wname, dkw->dkw_wname, sizeof(sc->sc_wname)); 271 sc->sc_wname[sizeof(sc->sc_wname) - 1] = '\0'; 272 273 memcpy(sc->sc_ptype, dkw->dkw_ptype, sizeof(sc->sc_ptype)); 274 sc->sc_ptype[sizeof(sc->sc_ptype) - 1] = '\0'; 275 276 bufq_alloc(&sc->sc_bufq, "fcfs", 0); 277 278 callout_init(&sc->sc_restart_ch, 0); 279 callout_setfunc(&sc->sc_restart_ch, dkrestart, sc); 280 281 /* 282 * Wedge will be added; increment the wedge count for the parent. 283 * Only allow this to happend if RAW_PART is the only thing open. 284 */ 285 mutex_enter(&pdk->dk_openlock); 286 if (pdk->dk_openmask & ~(1 << RAW_PART)) 287 error = EBUSY; 288 else { 289 /* Check for wedge overlap. */ 290 LIST_FOREACH(lsc, &pdk->dk_wedges, sc_plink) { 291 daddr_t lastblk = sc->sc_offset + sc->sc_size - 1; 292 daddr_t llastblk = lsc->sc_offset + lsc->sc_size - 1; 293 294 if (sc->sc_offset >= lsc->sc_offset && 295 sc->sc_offset <= llastblk) { 296 /* Overlaps the tail of the exsiting wedge. */ 297 break; 298 } 299 if (lastblk >= lsc->sc_offset && 300 lastblk <= llastblk) { 301 /* Overlaps the head of the existing wedge. */ 302 break; 303 } 304 } 305 if (lsc != NULL) 306 error = EINVAL; 307 else { 308 pdk->dk_nwedges++; 309 LIST_INSERT_HEAD(&pdk->dk_wedges, sc, sc_plink); 310 } 311 } 312 mutex_exit(&pdk->dk_openlock); 313 if (error) { 314 bufq_free(sc->sc_bufq); 315 free(sc, M_DKWEDGE); 316 return (error); 317 } 318 319 /* Fill in our cfdata for the pseudo-device glue. */ 320 sc->sc_cfdata.cf_name = dk_cd.cd_name; 321 sc->sc_cfdata.cf_atname = dk_ca.ca_name; 322 /* sc->sc_cfdata.cf_unit set below */ 323 sc->sc_cfdata.cf_fstate = FSTATE_STAR; 324 325 /* Insert the larval wedge into the array. */ 326 rw_enter(&dkwedges_lock, RW_WRITER); 327 for (error = 0;;) { 328 struct dkwedge_softc **scpp; 329 330 /* 331 * Check for a duplicate wname while searching for 332 * a slot. 333 */ 334 for (scpp = NULL, unit = 0; unit < ndkwedges; unit++) { 335 if (dkwedges[unit] == NULL) { 336 if (scpp == NULL) { 337 scpp = &dkwedges[unit]; 338 sc->sc_cfdata.cf_unit = unit; 339 } 340 } else { 341 /* XXX Unicode. */ 342 if (strcmp(dkwedges[unit]->sc_wname, 343 sc->sc_wname) == 0) { 344 error = EEXIST; 345 break; 346 } 347 } 348 } 349 if (error) 350 break; 351 KASSERT(unit == ndkwedges); 352 if (scpp == NULL) 353 dkwedge_array_expand(); 354 else { 355 KASSERT(scpp == &dkwedges[sc->sc_cfdata.cf_unit]); 356 *scpp = sc; 357 break; 358 } 359 } 360 rw_exit(&dkwedges_lock); 361 if (error) { 362 mutex_enter(&pdk->dk_openlock); 363 pdk->dk_nwedges--; 364 LIST_REMOVE(sc, sc_plink); 365 mutex_exit(&pdk->dk_openlock); 366 367 bufq_free(sc->sc_bufq); 368 free(sc, M_DKWEDGE); 369 return (error); 370 } 371 372 /* 373 * Now that we know the unit #, attach a pseudo-device for 374 * this wedge instance. This will provide us with the 375 * "struct device" necessary for glue to other parts of the 376 * system. 377 * 378 * This should never fail, unless we're almost totally out of 379 * memory. 380 */ 381 if ((sc->sc_dev = config_attach_pseudo(&sc->sc_cfdata)) == NULL) { 382 aprint_error("%s%u: unable to attach pseudo-device\n", 383 sc->sc_cfdata.cf_name, sc->sc_cfdata.cf_unit); 384 385 rw_enter(&dkwedges_lock, RW_WRITER); 386 dkwedges[sc->sc_cfdata.cf_unit] = NULL; 387 rw_exit(&dkwedges_lock); 388 389 mutex_enter(&pdk->dk_openlock); 390 pdk->dk_nwedges--; 391 LIST_REMOVE(sc, sc_plink); 392 mutex_exit(&pdk->dk_openlock); 393 394 bufq_free(sc->sc_bufq); 395 free(sc, M_DKWEDGE); 396 return (ENOMEM); 397 } 398 399 /* Return the devname to the caller. */ 400 strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev), 401 sizeof(dkw->dkw_devname)); 402 403 /* 404 * XXX Really ought to make the disk_attach() and the changing 405 * of state to RUNNING atomic. 406 */ 407 408 disk_init(&sc->sc_dk, device_xname(sc->sc_dev), NULL); 409 disk_attach(&sc->sc_dk); 410 411 /* Disk wedge is ready for use! */ 412 sc->sc_state = DKW_STATE_RUNNING; 413 414 /* Announce our arrival. */ 415 aprint_normal("%s at %s: %s\n", device_xname(sc->sc_dev), pdk->dk_name, 416 sc->sc_wname); /* XXX Unicode */ 417 aprint_normal("%s: %"PRIu64" blocks at %"PRId64", type: %s\n", 418 device_xname(sc->sc_dev), sc->sc_size, sc->sc_offset, sc->sc_ptype); 419 420 return (0); 421 } 422 423 /* 424 * dkwedge_del: [exported function] 425 * 426 * Delete a disk wedge based on the provided information. 427 * NOTE: We look up the wedge based on the wedge devname, 428 * not wname. 429 */ 430 int 431 dkwedge_del(struct dkwedge_info *dkw) 432 { 433 struct dkwedge_softc *sc = NULL; 434 u_int unit; 435 int bmaj, cmaj, s; 436 437 /* Find our softc. */ 438 dkw->dkw_devname[sizeof(dkw->dkw_devname) - 1] = '\0'; 439 rw_enter(&dkwedges_lock, RW_WRITER); 440 for (unit = 0; unit < ndkwedges; unit++) { 441 if ((sc = dkwedges[unit]) != NULL && 442 strcmp(device_xname(sc->sc_dev), dkw->dkw_devname) == 0 && 443 strcmp(sc->sc_parent->dk_name, dkw->dkw_parent) == 0) { 444 /* Mark the wedge as dying. */ 445 sc->sc_state = DKW_STATE_DYING; 446 break; 447 } 448 } 449 rw_exit(&dkwedges_lock); 450 if (unit == ndkwedges) 451 return (ESRCH); 452 453 KASSERT(sc != NULL); 454 455 /* Locate the wedge major numbers. */ 456 bmaj = bdevsw_lookup_major(&dk_bdevsw); 457 cmaj = cdevsw_lookup_major(&dk_cdevsw); 458 459 /* Kill any pending restart. */ 460 callout_stop(&sc->sc_restart_ch); 461 462 /* 463 * dkstart() will kill any queued buffers now that the 464 * state of the wedge is not RUNNING. Once we've done 465 * that, wait for any other pending I/O to complete. 466 */ 467 s = splbio(); 468 dkstart(sc); 469 dkwedge_wait_drain(sc); 470 splx(s); 471 472 /* Nuke the vnodes for any open instances. */ 473 vdevgone(bmaj, unit, unit, VBLK); 474 vdevgone(cmaj, unit, unit, VCHR); 475 476 /* Clean up the parent. */ 477 mutex_enter(&sc->sc_dk.dk_openlock); 478 mutex_enter(&sc->sc_parent->dk_rawlock); 479 if (sc->sc_dk.dk_openmask) { 480 if (sc->sc_parent->dk_rawopens-- == 1) { 481 KASSERT(sc->sc_parent->dk_rawvp != NULL); 482 (void) vn_close(sc->sc_parent->dk_rawvp, FREAD | FWRITE, 483 NOCRED); 484 sc->sc_parent->dk_rawvp = NULL; 485 } 486 sc->sc_dk.dk_openmask = 0; 487 } 488 mutex_exit(&sc->sc_parent->dk_rawlock); 489 mutex_exit(&sc->sc_dk.dk_openlock); 490 491 /* Announce our departure. */ 492 aprint_normal("%s at %s (%s) deleted\n", device_xname(sc->sc_dev), 493 sc->sc_parent->dk_name, 494 sc->sc_wname); /* XXX Unicode */ 495 496 /* Delete our pseudo-device. */ 497 (void) config_detach(sc->sc_dev, DETACH_FORCE | DETACH_QUIET); 498 499 mutex_enter(&sc->sc_parent->dk_openlock); 500 sc->sc_parent->dk_nwedges--; 501 LIST_REMOVE(sc, sc_plink); 502 mutex_exit(&sc->sc_parent->dk_openlock); 503 504 /* Delete our buffer queue. */ 505 bufq_free(sc->sc_bufq); 506 507 /* Detach from the disk list. */ 508 disk_detach(&sc->sc_dk); 509 disk_destroy(&sc->sc_dk); 510 511 /* Poof. */ 512 rw_enter(&dkwedges_lock, RW_WRITER); 513 dkwedges[unit] = NULL; 514 sc->sc_state = DKW_STATE_DEAD; 515 rw_exit(&dkwedges_lock); 516 517 free(sc, M_DKWEDGE); 518 519 return (0); 520 } 521 522 /* 523 * dkwedge_delall: [exported function] 524 * 525 * Delete all of the wedges on the specified disk. Used when 526 * a disk is being detached. 527 */ 528 void 529 dkwedge_delall(struct disk *pdk) 530 { 531 struct dkwedge_info dkw; 532 struct dkwedge_softc *sc; 533 534 for (;;) { 535 mutex_enter(&pdk->dk_openlock); 536 if ((sc = LIST_FIRST(&pdk->dk_wedges)) == NULL) { 537 KASSERT(pdk->dk_nwedges == 0); 538 mutex_exit(&pdk->dk_openlock); 539 return; 540 } 541 strcpy(dkw.dkw_parent, pdk->dk_name); 542 strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev), 543 sizeof(dkw.dkw_devname)); 544 mutex_exit(&pdk->dk_openlock); 545 (void) dkwedge_del(&dkw); 546 } 547 } 548 549 /* 550 * dkwedge_list: [exported function] 551 * 552 * List all of the wedges on a particular disk. 553 * If p == NULL, the buffer is in kernel space. Otherwise, it is 554 * in user space of the specified process. 555 */ 556 int 557 dkwedge_list(struct disk *pdk, struct dkwedge_list *dkwl, struct lwp *l) 558 { 559 struct uio uio; 560 struct iovec iov; 561 struct dkwedge_softc *sc; 562 struct dkwedge_info dkw; 563 struct vmspace *vm; 564 int error = 0; 565 566 iov.iov_base = dkwl->dkwl_buf; 567 iov.iov_len = dkwl->dkwl_bufsize; 568 569 uio.uio_iov = &iov; 570 uio.uio_iovcnt = 1; 571 uio.uio_offset = 0; 572 uio.uio_resid = dkwl->dkwl_bufsize; 573 uio.uio_rw = UIO_READ; 574 if (l == NULL) { 575 UIO_SETUP_SYSSPACE(&uio); 576 } else { 577 error = proc_vmspace_getref(l->l_proc, &vm); 578 if (error) { 579 return error; 580 } 581 uio.uio_vmspace = vm; 582 } 583 584 dkwl->dkwl_ncopied = 0; 585 586 mutex_enter(&pdk->dk_openlock); 587 LIST_FOREACH(sc, &pdk->dk_wedges, sc_plink) { 588 if (uio.uio_resid < sizeof(dkw)) 589 break; 590 591 if (sc->sc_state != DKW_STATE_RUNNING) 592 continue; 593 594 strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev), 595 sizeof(dkw.dkw_devname)); 596 memcpy(dkw.dkw_wname, sc->sc_wname, sizeof(dkw.dkw_wname)); 597 dkw.dkw_wname[sizeof(dkw.dkw_wname) - 1] = '\0'; 598 strcpy(dkw.dkw_parent, sc->sc_parent->dk_name); 599 dkw.dkw_offset = sc->sc_offset; 600 dkw.dkw_size = sc->sc_size; 601 strcpy(dkw.dkw_ptype, sc->sc_ptype); 602 603 error = uiomove(&dkw, sizeof(dkw), &uio); 604 if (error) 605 break; 606 dkwl->dkwl_ncopied++; 607 } 608 dkwl->dkwl_nwedges = pdk->dk_nwedges; 609 mutex_exit(&pdk->dk_openlock); 610 611 if (l != NULL) { 612 uvmspace_free(vm); 613 } 614 615 return (error); 616 } 617 618 device_t 619 dkwedge_find_by_wname(const char *wname) 620 { 621 device_t dv = NULL; 622 struct dkwedge_softc *sc; 623 int i; 624 625 rw_enter(&dkwedges_lock, RW_WRITER); 626 for (i = 0; i < ndkwedges; i++) { 627 if ((sc = dkwedges[i]) == NULL) 628 continue; 629 if (strcmp(sc->sc_wname, wname) == 0) { 630 if (dv != NULL) { 631 printf( 632 "WARNING: double match for wedge name %s " 633 "(%s, %s)\n", wname, device_xname(dv), 634 device_xname(sc->sc_dev)); 635 continue; 636 } 637 dv = sc->sc_dev; 638 } 639 } 640 rw_exit(&dkwedges_lock); 641 return dv; 642 } 643 644 void 645 dkwedge_print_wnames(void) 646 { 647 struct dkwedge_softc *sc; 648 int i; 649 650 rw_enter(&dkwedges_lock, RW_WRITER); 651 for (i = 0; i < ndkwedges; i++) { 652 if ((sc = dkwedges[i]) == NULL) 653 continue; 654 printf(" wedge:%s", sc->sc_wname); 655 } 656 rw_exit(&dkwedges_lock); 657 } 658 659 /* 660 * dkwedge_set_bootwedge 661 * 662 * Set the booted_wedge global based on the specified parent name 663 * and offset/length. 664 */ 665 void 666 dkwedge_set_bootwedge(struct device *parent, daddr_t startblk, uint64_t nblks) 667 { 668 struct dkwedge_softc *sc; 669 int i; 670 671 rw_enter(&dkwedges_lock, RW_WRITER); 672 for (i = 0; i < ndkwedges; i++) { 673 if ((sc = dkwedges[i]) == NULL) 674 continue; 675 if (strcmp(sc->sc_parent->dk_name, device_xname(parent)) == 0 && 676 sc->sc_offset == startblk && 677 sc->sc_size == nblks) { 678 if (booted_wedge) { 679 printf("WARNING: double match for boot wedge " 680 "(%s, %s)\n", 681 device_xname(booted_wedge), 682 device_xname(sc->sc_dev)); 683 continue; 684 } 685 booted_device = parent; 686 booted_wedge = sc->sc_dev; 687 booted_partition = 0; 688 } 689 } 690 /* 691 * XXX What if we don't find one? Should we create a special 692 * XXX root wedge? 693 */ 694 rw_exit(&dkwedges_lock); 695 } 696 697 /* 698 * We need a dummy object to stuff into the dkwedge discovery method link 699 * set to ensure that there is always at least one object in the set. 700 */ 701 static struct dkwedge_discovery_method dummy_discovery_method; 702 __link_set_add_bss(dkwedge_methods, dummy_discovery_method); 703 704 /* 705 * dkwedge_init: 706 * 707 * Initialize the disk wedge subsystem. 708 */ 709 void 710 dkwedge_init(void) 711 { 712 __link_set_decl(dkwedge_methods, struct dkwedge_discovery_method); 713 struct dkwedge_discovery_method * const *ddmp; 714 struct dkwedge_discovery_method *lddm, *ddm; 715 716 rw_init(&dkwedges_lock); 717 rw_init(&dkwedge_discovery_methods_lock); 718 719 if (config_cfdriver_attach(&dk_cd) != 0) 720 panic("dkwedge: unable to attach cfdriver"); 721 if (config_cfattach_attach(dk_cd.cd_name, &dk_ca) != 0) 722 panic("dkwedge: unable to attach cfattach"); 723 724 rw_enter(&dkwedge_discovery_methods_lock, RW_WRITER); 725 726 LIST_INIT(&dkwedge_discovery_methods); 727 728 __link_set_foreach(ddmp, dkwedge_methods) { 729 ddm = *ddmp; 730 if (ddm == &dummy_discovery_method) 731 continue; 732 if (LIST_EMPTY(&dkwedge_discovery_methods)) { 733 LIST_INSERT_HEAD(&dkwedge_discovery_methods, 734 ddm, ddm_list); 735 continue; 736 } 737 LIST_FOREACH(lddm, &dkwedge_discovery_methods, ddm_list) { 738 if (ddm->ddm_priority == lddm->ddm_priority) { 739 aprint_error("dk-method-%s: method \"%s\" " 740 "already exists at priority %d\n", 741 ddm->ddm_name, lddm->ddm_name, 742 lddm->ddm_priority); 743 /* Not inserted. */ 744 break; 745 } 746 if (ddm->ddm_priority < lddm->ddm_priority) { 747 /* Higher priority; insert before. */ 748 LIST_INSERT_BEFORE(lddm, ddm, ddm_list); 749 break; 750 } 751 if (LIST_NEXT(lddm, ddm_list) == NULL) { 752 /* Last one; insert after. */ 753 KASSERT(lddm->ddm_priority < ddm->ddm_priority); 754 LIST_INSERT_AFTER(lddm, ddm, ddm_list); 755 break; 756 } 757 } 758 } 759 760 rw_exit(&dkwedge_discovery_methods_lock); 761 } 762 763 #ifdef DKWEDGE_AUTODISCOVER 764 int dkwedge_autodiscover = 1; 765 #else 766 int dkwedge_autodiscover = 0; 767 #endif 768 769 /* 770 * dkwedge_discover: [exported function] 771 * 772 * Discover the wedges on a newly attached disk. 773 */ 774 void 775 dkwedge_discover(struct disk *pdk) 776 { 777 struct dkwedge_discovery_method *ddm; 778 struct vnode *vp; 779 int error; 780 dev_t pdev; 781 782 /* 783 * Require people playing with wedges to enable this explicitly. 784 */ 785 if (dkwedge_autodiscover == 0) 786 return; 787 788 rw_enter(&dkwedge_discovery_methods_lock, RW_READER); 789 790 error = dkwedge_compute_pdev(pdk->dk_name, &pdev); 791 if (error) { 792 aprint_error("%s: unable to compute pdev, error = %d\n", 793 pdk->dk_name, error); 794 goto out; 795 } 796 797 error = bdevvp(pdev, &vp); 798 if (error) { 799 aprint_error("%s: unable to find vnode for pdev, error = %d\n", 800 pdk->dk_name, error); 801 goto out; 802 } 803 804 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 805 if (error) { 806 aprint_error("%s: unable to lock vnode for pdev, error = %d\n", 807 pdk->dk_name, error); 808 vrele(vp); 809 goto out; 810 } 811 812 error = VOP_OPEN(vp, FREAD, NOCRED); 813 if (error) { 814 aprint_error("%s: unable to open device, error = %d\n", 815 pdk->dk_name, error); 816 vput(vp); 817 goto out; 818 } 819 VOP_UNLOCK(vp, 0); 820 821 /* 822 * For each supported partition map type, look to see if 823 * this map type exists. If so, parse it and add the 824 * corresponding wedges. 825 */ 826 LIST_FOREACH(ddm, &dkwedge_discovery_methods, ddm_list) { 827 error = (*ddm->ddm_discover)(pdk, vp); 828 if (error == 0) { 829 /* Successfully created wedges; we're done. */ 830 break; 831 } 832 } 833 834 error = vn_close(vp, FREAD, NOCRED); 835 if (error) { 836 aprint_error("%s: unable to close device, error = %d\n", 837 pdk->dk_name, error); 838 /* We'll just assume the vnode has been cleaned up. */ 839 } 840 out: 841 rw_exit(&dkwedge_discovery_methods_lock); 842 } 843 844 /* 845 * dkwedge_read: 846 * 847 * Read some data from the specified disk, used for 848 * partition discovery. 849 */ 850 int 851 dkwedge_read(struct disk *pdk, struct vnode *vp, daddr_t blkno, 852 void *tbuf, size_t len) 853 { 854 struct buf *bp; 855 int result; 856 857 bp = getiobuf(vp, true); 858 859 bp->b_dev = vp->v_rdev; 860 bp->b_blkno = blkno; 861 bp->b_bcount = len; 862 bp->b_resid = len; 863 bp->b_flags = B_READ; 864 bp->b_data = tbuf; 865 SET(bp->b_cflags, BC_BUSY); /* mark buffer busy */ 866 867 VOP_STRATEGY(vp, bp); 868 result = biowait(bp); 869 putiobuf(bp); 870 871 return result; 872 } 873 874 /* 875 * dkwedge_lookup: 876 * 877 * Look up a dkwedge_softc based on the provided dev_t. 878 */ 879 static struct dkwedge_softc * 880 dkwedge_lookup(dev_t dev) 881 { 882 int unit = minor(dev); 883 884 if (unit >= ndkwedges) 885 return (NULL); 886 887 KASSERT(dkwedges != NULL); 888 889 return (dkwedges[unit]); 890 } 891 892 /* 893 * dkopen: [devsw entry point] 894 * 895 * Open a wedge. 896 */ 897 static int 898 dkopen(dev_t dev, int flags, int fmt, struct lwp *l) 899 { 900 struct dkwedge_softc *sc = dkwedge_lookup(dev); 901 struct vnode *vp; 902 int error = 0; 903 904 if (sc == NULL) 905 return (ENODEV); 906 907 if (sc->sc_state != DKW_STATE_RUNNING) 908 return (ENXIO); 909 910 /* 911 * We go through a complicated little dance to only open the parent 912 * vnode once per wedge, no matter how many times the wedge is 913 * opened. The reason? We see one dkopen() per open call, but 914 * only dkclose() on the last close. 915 */ 916 mutex_enter(&sc->sc_dk.dk_openlock); 917 mutex_enter(&sc->sc_parent->dk_rawlock); 918 if (sc->sc_dk.dk_openmask == 0) { 919 if (sc->sc_parent->dk_rawopens == 0) { 920 KASSERT(sc->sc_parent->dk_rawvp == NULL); 921 error = bdevvp(sc->sc_pdev, &vp); 922 if (error) 923 goto popen_fail; 924 error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 925 if (error) { 926 vrele(vp); 927 goto popen_fail; 928 } 929 error = VOP_OPEN(vp, FREAD | FWRITE, NOCRED); 930 if (error) { 931 vput(vp); 932 goto popen_fail; 933 } 934 /* VOP_OPEN() doesn't do this for us. */ 935 mutex_enter(&vp->v_interlock); 936 vp->v_writecount++; 937 mutex_exit(&vp->v_interlock); 938 VOP_UNLOCK(vp, 0); 939 sc->sc_parent->dk_rawvp = vp; 940 } 941 sc->sc_parent->dk_rawopens++; 942 } 943 if (fmt == S_IFCHR) 944 sc->sc_dk.dk_copenmask |= 1; 945 else 946 sc->sc_dk.dk_bopenmask |= 1; 947 sc->sc_dk.dk_openmask = 948 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask; 949 950 popen_fail: 951 mutex_exit(&sc->sc_parent->dk_rawlock); 952 mutex_exit(&sc->sc_dk.dk_openlock); 953 return (error); 954 } 955 956 /* 957 * dkclose: [devsw entry point] 958 * 959 * Close a wedge. 960 */ 961 static int 962 dkclose(dev_t dev, int flags, int fmt, struct lwp *l) 963 { 964 struct dkwedge_softc *sc = dkwedge_lookup(dev); 965 int error = 0; 966 967 KASSERT(sc->sc_dk.dk_openmask != 0); 968 969 mutex_enter(&sc->sc_dk.dk_openlock); 970 mutex_enter(&sc->sc_parent->dk_rawlock); 971 972 if (fmt == S_IFCHR) 973 sc->sc_dk.dk_copenmask &= ~1; 974 else 975 sc->sc_dk.dk_bopenmask &= ~1; 976 sc->sc_dk.dk_openmask = 977 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask; 978 979 if (sc->sc_dk.dk_openmask == 0) { 980 if (sc->sc_parent->dk_rawopens-- == 1) { 981 KASSERT(sc->sc_parent->dk_rawvp != NULL); 982 error = vn_close(sc->sc_parent->dk_rawvp, 983 FREAD | FWRITE, NOCRED); 984 sc->sc_parent->dk_rawvp = NULL; 985 } 986 } 987 988 mutex_exit(&sc->sc_parent->dk_rawlock); 989 mutex_exit(&sc->sc_dk.dk_openlock); 990 991 return (error); 992 } 993 994 /* 995 * dkstragegy: [devsw entry point] 996 * 997 * Perform I/O based on the wedge I/O strategy. 998 */ 999 static void 1000 dkstrategy(struct buf *bp) 1001 { 1002 struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev); 1003 int s; 1004 1005 if (sc->sc_state != DKW_STATE_RUNNING) { 1006 bp->b_error = ENXIO; 1007 goto done; 1008 } 1009 1010 /* If it's an empty transfer, wake up the top half now. */ 1011 if (bp->b_bcount == 0) 1012 goto done; 1013 1014 /* Make sure it's in-range. */ 1015 if (bounds_check_with_mediasize(bp, DEV_BSIZE, sc->sc_size) <= 0) 1016 goto done; 1017 1018 /* Translate it to the parent's raw LBA. */ 1019 bp->b_rawblkno = bp->b_blkno + sc->sc_offset; 1020 1021 /* Place it in the queue and start I/O on the unit. */ 1022 s = splbio(); 1023 sc->sc_iopend++; 1024 bufq_put(sc->sc_bufq, bp); 1025 dkstart(sc); 1026 splx(s); 1027 return; 1028 1029 done: 1030 bp->b_resid = bp->b_bcount; 1031 biodone(bp); 1032 } 1033 1034 /* 1035 * dkstart: 1036 * 1037 * Start I/O that has been enqueued on the wedge. 1038 * NOTE: Must be called at splbio()! 1039 */ 1040 static void 1041 dkstart(struct dkwedge_softc *sc) 1042 { 1043 struct vnode *vp; 1044 struct buf *bp, *nbp; 1045 1046 /* Do as much work as has been enqueued. */ 1047 while ((bp = bufq_peek(sc->sc_bufq)) != NULL) { 1048 if (sc->sc_state != DKW_STATE_RUNNING) { 1049 (void) bufq_get(sc->sc_bufq); 1050 if (sc->sc_iopend-- == 1 && 1051 (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) { 1052 sc->sc_flags &= ~DK_F_WAIT_DRAIN; 1053 wakeup(&sc->sc_iopend); 1054 } 1055 bp->b_error = ENXIO; 1056 bp->b_resid = bp->b_bcount; 1057 biodone(bp); 1058 } 1059 1060 /* Instrumentation. */ 1061 disk_busy(&sc->sc_dk); 1062 1063 nbp = getiobuf(sc->sc_parent->dk_rawvp, false); 1064 if (nbp == NULL) { 1065 /* 1066 * No resources to run this request; leave the 1067 * buffer queued up, and schedule a timer to 1068 * restart the queue in 1/2 a second. 1069 */ 1070 disk_unbusy(&sc->sc_dk, 0, bp->b_flags & B_READ); 1071 callout_schedule(&sc->sc_restart_ch, hz / 2); 1072 return; 1073 } 1074 1075 (void) bufq_get(sc->sc_bufq); 1076 1077 nbp->b_data = bp->b_data; 1078 nbp->b_flags = bp->b_flags; 1079 nbp->b_oflags = bp->b_oflags; 1080 nbp->b_cflags = bp->b_cflags; 1081 nbp->b_iodone = dkiodone; 1082 nbp->b_proc = bp->b_proc; 1083 nbp->b_blkno = bp->b_rawblkno; 1084 nbp->b_dev = sc->sc_parent->dk_rawvp->v_rdev; 1085 nbp->b_bcount = bp->b_bcount; 1086 nbp->b_private = bp; 1087 BIO_COPYPRIO(nbp, bp); 1088 1089 vp = nbp->b_vp; 1090 if ((nbp->b_flags & B_READ) == 0) { 1091 mutex_enter(&vp->v_interlock); 1092 vp->v_numoutput++; 1093 mutex_exit(&vp->v_interlock); 1094 } 1095 VOP_STRATEGY(vp, nbp); 1096 } 1097 } 1098 1099 /* 1100 * dkiodone: 1101 * 1102 * I/O to a wedge has completed; alert the top half. 1103 * NOTE: Must be called at splbio()! 1104 */ 1105 static void 1106 dkiodone(struct buf *bp) 1107 { 1108 struct buf *obp = bp->b_private; 1109 struct dkwedge_softc *sc = dkwedge_lookup(obp->b_dev); 1110 1111 if (bp->b_error != 0) 1112 obp->b_error = bp->b_error; 1113 obp->b_resid = bp->b_resid; 1114 putiobuf(bp); 1115 1116 if (sc->sc_iopend-- == 1 && (sc->sc_flags & DK_F_WAIT_DRAIN) != 0) { 1117 sc->sc_flags &= ~DK_F_WAIT_DRAIN; 1118 wakeup(&sc->sc_iopend); 1119 } 1120 1121 disk_unbusy(&sc->sc_dk, obp->b_bcount - obp->b_resid, 1122 obp->b_flags & B_READ); 1123 1124 biodone(obp); 1125 1126 /* Kick the queue in case there is more work we can do. */ 1127 dkstart(sc); 1128 } 1129 1130 /* 1131 * dkrestart: 1132 * 1133 * Restart the work queue after it was stalled due to 1134 * a resource shortage. Invoked via a callout. 1135 */ 1136 static void 1137 dkrestart(void *v) 1138 { 1139 struct dkwedge_softc *sc = v; 1140 int s; 1141 1142 s = splbio(); 1143 dkstart(sc); 1144 splx(s); 1145 } 1146 1147 /* 1148 * dkread: [devsw entry point] 1149 * 1150 * Read from a wedge. 1151 */ 1152 static int 1153 dkread(dev_t dev, struct uio *uio, int flags) 1154 { 1155 struct dkwedge_softc *sc = dkwedge_lookup(dev); 1156 1157 if (sc->sc_state != DKW_STATE_RUNNING) 1158 return (ENXIO); 1159 1160 return (physio(dkstrategy, NULL, dev, B_READ, 1161 sc->sc_parent->dk_driver->d_minphys, uio)); 1162 } 1163 1164 /* 1165 * dkwrite: [devsw entry point] 1166 * 1167 * Write to a wedge. 1168 */ 1169 static int 1170 dkwrite(dev_t dev, struct uio *uio, int flags) 1171 { 1172 struct dkwedge_softc *sc = dkwedge_lookup(dev); 1173 1174 if (sc->sc_state != DKW_STATE_RUNNING) 1175 return (ENXIO); 1176 1177 return (physio(dkstrategy, NULL, dev, B_WRITE, 1178 sc->sc_parent->dk_driver->d_minphys, uio)); 1179 } 1180 1181 /* 1182 * dkioctl: [devsw entry point] 1183 * 1184 * Perform an ioctl request on a wedge. 1185 */ 1186 static int 1187 dkioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 1188 { 1189 struct dkwedge_softc *sc = dkwedge_lookup(dev); 1190 int error = 0; 1191 1192 if (sc->sc_state != DKW_STATE_RUNNING) 1193 return (ENXIO); 1194 1195 switch (cmd) { 1196 case DIOCCACHESYNC: 1197 /* 1198 * XXX Do we really need to care about having a writable 1199 * file descriptor here? 1200 */ 1201 if ((flag & FWRITE) == 0) 1202 error = EBADF; 1203 else 1204 error = VOP_IOCTL(sc->sc_parent->dk_rawvp, 1205 cmd, data, flag, 1206 l != NULL ? l->l_cred : NOCRED); 1207 break; 1208 case DIOCGWEDGEINFO: 1209 { 1210 struct dkwedge_info *dkw = (void *) data; 1211 1212 strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev), 1213 sizeof(dkw->dkw_devname)); 1214 memcpy(dkw->dkw_wname, sc->sc_wname, sizeof(dkw->dkw_wname)); 1215 dkw->dkw_wname[sizeof(dkw->dkw_wname) - 1] = '\0'; 1216 strcpy(dkw->dkw_parent, sc->sc_parent->dk_name); 1217 dkw->dkw_offset = sc->sc_offset; 1218 dkw->dkw_size = sc->sc_size; 1219 strcpy(dkw->dkw_ptype, sc->sc_ptype); 1220 1221 break; 1222 } 1223 1224 default: 1225 error = ENOTTY; 1226 } 1227 1228 return (error); 1229 } 1230 1231 /* 1232 * dksize: [devsw entry point] 1233 * 1234 * Query the size of a wedge for the purpose of performing a dump 1235 * or for swapping to. 1236 */ 1237 static int 1238 dksize(dev_t dev) 1239 { 1240 struct dkwedge_softc *sc = dkwedge_lookup(dev); 1241 int rv = -1; 1242 1243 if (sc == NULL) 1244 return (-1); 1245 1246 if (sc->sc_state != DKW_STATE_RUNNING) 1247 return (ENXIO); 1248 1249 mutex_enter(&sc->sc_dk.dk_openlock); 1250 mutex_enter(&sc->sc_parent->dk_rawlock); 1251 1252 /* Our content type is static, no need to open the device. */ 1253 1254 if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) == 0) { 1255 /* Saturate if we are larger than INT_MAX. */ 1256 if (sc->sc_size > INT_MAX) 1257 rv = INT_MAX; 1258 else 1259 rv = (int) sc->sc_size; 1260 } 1261 1262 mutex_exit(&sc->sc_parent->dk_rawlock); 1263 mutex_exit(&sc->sc_dk.dk_openlock); 1264 1265 return (rv); 1266 } 1267 1268 /* 1269 * dkdump: [devsw entry point] 1270 * 1271 * Perform a crash dump to a wedge. 1272 */ 1273 static int 1274 dkdump(dev_t dev, daddr_t blkno, void *va, size_t size) 1275 { 1276 struct dkwedge_softc *sc = dkwedge_lookup(dev); 1277 const struct bdevsw *bdev; 1278 int rv = 0; 1279 1280 if (sc == NULL) 1281 return (-1); 1282 1283 if (sc->sc_state != DKW_STATE_RUNNING) 1284 return (ENXIO); 1285 1286 mutex_enter(&sc->sc_dk.dk_openlock); 1287 mutex_enter(&sc->sc_parent->dk_rawlock); 1288 1289 /* Our content type is static, no need to open the device. */ 1290 1291 if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) != 0) { 1292 rv = ENXIO; 1293 goto out; 1294 } 1295 if (size % DEV_BSIZE != 0) { 1296 rv = EINVAL; 1297 goto out; 1298 } 1299 if (blkno + size / DEV_BSIZE > sc->sc_size) { 1300 printf("%s: blkno (%" PRIu64 ") + size / DEV_BSIZE (%zu) > " 1301 "sc->sc_size (%" PRIu64 ")\n", __func__, blkno, 1302 size / DEV_BSIZE, sc->sc_size); 1303 rv = EINVAL; 1304 goto out; 1305 } 1306 1307 bdev = bdevsw_lookup(sc->sc_pdev); 1308 rv = (*bdev->d_dump)(sc->sc_pdev, blkno + sc->sc_offset, va, size); 1309 1310 out: 1311 mutex_exit(&sc->sc_parent->dk_rawlock); 1312 mutex_exit(&sc->sc_dk.dk_openlock); 1313 1314 return rv; 1315 } 1316