1 /*- 2 * Copyright (c) 1999-2002 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/kernel.h> 32 #include <sys/systm.h> 33 #include <sys/bio.h> 34 #include <sys/lock.h> 35 #include <sys/mutex.h> 36 #include <sys/module.h> 37 #include <sys/malloc.h> 38 #include <sys/conf.h> 39 #include <sys/vnode.h> 40 #include <sys/queue.h> 41 #include <sys/poll.h> 42 #include <sys/sx.h> 43 #include <sys/ctype.h> 44 #include <sys/tty.h> 45 #include <sys/ucred.h> 46 #include <sys/taskqueue.h> 47 #include <machine/stdarg.h> 48 49 #include <fs/devfs/devfs_int.h> 50 51 static MALLOC_DEFINE(M_DEVT, "cdev", "cdev storage"); 52 53 struct mtx devmtx; 54 static void destroy_devl(struct cdev *dev); 55 static int destroy_dev_sched_cbl(struct cdev *dev, 56 void (*cb)(void *), void *arg); 57 static struct cdev *make_dev_credv(int flags, 58 struct cdevsw *devsw, int minornr, 59 struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, 60 va_list ap); 61 62 static struct cdev_priv_list cdevp_free_list = 63 TAILQ_HEAD_INITIALIZER(cdevp_free_list); 64 65 void 66 dev_lock(void) 67 { 68 69 mtx_lock(&devmtx); 70 } 71 72 static void 73 dev_unlock_and_free(void) 74 { 75 struct cdev_priv *cdp; 76 77 mtx_assert(&devmtx, MA_OWNED); 78 while ((cdp = TAILQ_FIRST(&cdevp_free_list)) != NULL) { 79 TAILQ_REMOVE(&cdevp_free_list, cdp, cdp_list); 80 mtx_unlock(&devmtx); 81 devfs_free(&cdp->cdp_c); 82 mtx_lock(&devmtx); 83 } 84 mtx_unlock(&devmtx); 85 } 86 87 static void 88 dev_free_devlocked(struct cdev *cdev) 89 { 90 struct cdev_priv *cdp; 91 92 mtx_assert(&devmtx, MA_OWNED); 93 cdp = cdev->si_priv; 94 TAILQ_INSERT_HEAD(&cdevp_free_list, cdp, cdp_list); 95 } 96 97 void 98 dev_unlock(void) 99 { 100 101 mtx_unlock(&devmtx); 102 } 103 104 void 105 dev_ref(struct cdev *dev) 106 { 107 108 mtx_assert(&devmtx, MA_NOTOWNED); 109 mtx_lock(&devmtx); 110 dev->si_refcount++; 111 mtx_unlock(&devmtx); 112 } 113 114 void 115 dev_refl(struct cdev *dev) 116 { 117 118 mtx_assert(&devmtx, MA_OWNED); 119 dev->si_refcount++; 120 } 121 122 void 123 dev_rel(struct cdev *dev) 124 { 125 int flag = 0; 126 127 mtx_assert(&devmtx, MA_NOTOWNED); 128 dev_lock(); 129 dev->si_refcount--; 130 KASSERT(dev->si_refcount >= 0, 131 ("dev_rel(%s) gave negative count", devtoname(dev))); 132 #if 0 133 if (dev->si_usecount == 0 && 134 (dev->si_flags & SI_CHEAPCLONE) && (dev->si_flags & SI_NAMED)) 135 ; 136 else 137 #endif 138 if (dev->si_devsw == NULL && dev->si_refcount == 0) { 139 LIST_REMOVE(dev, si_list); 140 flag = 1; 141 } 142 dev_unlock(); 143 if (flag) 144 devfs_free(dev); 145 } 146 147 struct cdevsw * 148 dev_refthread(struct cdev *dev) 149 { 150 struct cdevsw *csw; 151 struct cdev_priv *cdp; 152 153 mtx_assert(&devmtx, MA_NOTOWNED); 154 dev_lock(); 155 csw = dev->si_devsw; 156 if (csw != NULL) { 157 cdp = dev->si_priv; 158 if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0) 159 dev->si_threadcount++; 160 else 161 csw = NULL; 162 } 163 dev_unlock(); 164 return (csw); 165 } 166 167 struct cdevsw * 168 devvn_refthread(struct vnode *vp, struct cdev **devp) 169 { 170 struct cdevsw *csw; 171 struct cdev_priv *cdp; 172 173 mtx_assert(&devmtx, MA_NOTOWNED); 174 csw = NULL; 175 dev_lock(); 176 *devp = vp->v_rdev; 177 if (*devp != NULL) { 178 cdp = (*devp)->si_priv; 179 if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0) { 180 csw = (*devp)->si_devsw; 181 if (csw != NULL) 182 (*devp)->si_threadcount++; 183 } 184 } 185 dev_unlock(); 186 return (csw); 187 } 188 189 void 190 dev_relthread(struct cdev *dev) 191 { 192 193 mtx_assert(&devmtx, MA_NOTOWNED); 194 dev_lock(); 195 dev->si_threadcount--; 196 dev_unlock(); 197 } 198 199 int 200 nullop(void) 201 { 202 203 return (0); 204 } 205 206 int 207 eopnotsupp(void) 208 { 209 210 return (EOPNOTSUPP); 211 } 212 213 static int 214 enxio(void) 215 { 216 return (ENXIO); 217 } 218 219 static int 220 enodev(void) 221 { 222 return (ENODEV); 223 } 224 225 /* Define a dead_cdevsw for use when devices leave unexpectedly. */ 226 227 #define dead_open (d_open_t *)enxio 228 #define dead_close (d_close_t *)enxio 229 #define dead_read (d_read_t *)enxio 230 #define dead_write (d_write_t *)enxio 231 #define dead_ioctl (d_ioctl_t *)enxio 232 #define dead_poll (d_poll_t *)enodev 233 #define dead_mmap (d_mmap_t *)enodev 234 235 static void 236 dead_strategy(struct bio *bp) 237 { 238 239 biofinish(bp, NULL, ENXIO); 240 } 241 242 #define dead_dump (dumper_t *)enxio 243 #define dead_kqfilter (d_kqfilter_t *)enxio 244 245 static struct cdevsw dead_cdevsw = { 246 .d_version = D_VERSION, 247 .d_flags = D_NEEDGIANT, /* XXX: does dead_strategy need this ? */ 248 .d_open = dead_open, 249 .d_close = dead_close, 250 .d_read = dead_read, 251 .d_write = dead_write, 252 .d_ioctl = dead_ioctl, 253 .d_poll = dead_poll, 254 .d_mmap = dead_mmap, 255 .d_strategy = dead_strategy, 256 .d_name = "dead", 257 .d_dump = dead_dump, 258 .d_kqfilter = dead_kqfilter 259 }; 260 261 /* Default methods if driver does not specify method */ 262 263 #define null_open (d_open_t *)nullop 264 #define null_close (d_close_t *)nullop 265 #define no_read (d_read_t *)enodev 266 #define no_write (d_write_t *)enodev 267 #define no_ioctl (d_ioctl_t *)enodev 268 #define no_mmap (d_mmap_t *)enodev 269 #define no_kqfilter (d_kqfilter_t *)enodev 270 271 static void 272 no_strategy(struct bio *bp) 273 { 274 275 biofinish(bp, NULL, ENODEV); 276 } 277 278 static int 279 no_poll(struct cdev *dev __unused, int events, struct thread *td __unused) 280 { 281 /* 282 * Return true for read/write. If the user asked for something 283 * special, return POLLNVAL, so that clients have a way of 284 * determining reliably whether or not the extended 285 * functionality is present without hard-coding knowledge 286 * of specific filesystem implementations. 287 * Stay in sync with vop_nopoll(). 288 */ 289 if (events & ~POLLSTANDARD) 290 return (POLLNVAL); 291 292 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)); 293 } 294 295 #define no_dump (dumper_t *)enodev 296 297 static int 298 giant_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 299 { 300 int retval; 301 302 mtx_lock(&Giant); 303 retval = dev->si_devsw->d_gianttrick-> 304 d_open(dev, oflags, devtype, td); 305 mtx_unlock(&Giant); 306 return (retval); 307 } 308 309 static int 310 giant_fdopen(struct cdev *dev, int oflags, struct thread *td, struct file *fp) 311 { 312 int retval; 313 314 mtx_lock(&Giant); 315 retval = dev->si_devsw->d_gianttrick-> 316 d_fdopen(dev, oflags, td, fp); 317 mtx_unlock(&Giant); 318 return (retval); 319 } 320 321 static int 322 giant_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 323 { 324 int retval; 325 326 mtx_lock(&Giant); 327 retval = dev->si_devsw->d_gianttrick-> 328 d_close(dev, fflag, devtype, td); 329 mtx_unlock(&Giant); 330 return (retval); 331 } 332 333 static void 334 giant_strategy(struct bio *bp) 335 { 336 337 mtx_lock(&Giant); 338 bp->bio_dev->si_devsw->d_gianttrick-> 339 d_strategy(bp); 340 mtx_unlock(&Giant); 341 } 342 343 static int 344 giant_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 345 { 346 int retval; 347 348 mtx_lock(&Giant); 349 retval = dev->si_devsw->d_gianttrick-> 350 d_ioctl(dev, cmd, data, fflag, td); 351 mtx_unlock(&Giant); 352 return (retval); 353 } 354 355 static int 356 giant_read(struct cdev *dev, struct uio *uio, int ioflag) 357 { 358 int retval; 359 360 mtx_lock(&Giant); 361 retval = dev->si_devsw->d_gianttrick-> 362 d_read(dev, uio, ioflag); 363 mtx_unlock(&Giant); 364 return (retval); 365 } 366 367 static int 368 giant_write(struct cdev *dev, struct uio *uio, int ioflag) 369 { 370 int retval; 371 372 mtx_lock(&Giant); 373 retval = dev->si_devsw->d_gianttrick-> 374 d_write(dev, uio, ioflag); 375 mtx_unlock(&Giant); 376 return (retval); 377 } 378 379 static int 380 giant_poll(struct cdev *dev, int events, struct thread *td) 381 { 382 int retval; 383 384 mtx_lock(&Giant); 385 retval = dev->si_devsw->d_gianttrick-> 386 d_poll(dev, events, td); 387 mtx_unlock(&Giant); 388 return (retval); 389 } 390 391 static int 392 giant_kqfilter(struct cdev *dev, struct knote *kn) 393 { 394 int retval; 395 396 mtx_lock(&Giant); 397 retval = dev->si_devsw->d_gianttrick-> 398 d_kqfilter(dev, kn); 399 mtx_unlock(&Giant); 400 return (retval); 401 } 402 403 static int 404 giant_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot) 405 { 406 int retval; 407 408 mtx_lock(&Giant); 409 retval = dev->si_devsw->d_gianttrick-> 410 d_mmap(dev, offset, paddr, nprot); 411 mtx_unlock(&Giant); 412 return (retval); 413 } 414 415 416 /* 417 * struct cdev * and u_dev_t primitives 418 */ 419 420 int 421 minor(struct cdev *x) 422 { 423 if (x == NULL) 424 return NODEV; 425 return(x->si_drv0 & MAXMINOR); 426 } 427 428 int 429 dev2unit(struct cdev *x) 430 { 431 432 if (x == NULL) 433 return NODEV; 434 return (minor2unit(minor(x))); 435 } 436 437 u_int 438 minor2unit(u_int _minor) 439 { 440 441 KASSERT((_minor & ~MAXMINOR) == 0, ("Illegal minor %x", _minor)); 442 return ((_minor & 0xff) | ((_minor >> 8) & 0xffff00)); 443 } 444 445 int 446 unit2minor(int unit) 447 { 448 449 KASSERT(unit <= 0xffffff, ("Invalid unit (%d) in unit2minor", unit)); 450 return ((unit & 0xff) | ((unit << 8) & ~0xffff)); 451 } 452 453 static struct cdev * 454 newdev(struct cdevsw *csw, int y, struct cdev *si) 455 { 456 struct cdev *si2; 457 dev_t udev; 458 459 mtx_assert(&devmtx, MA_OWNED); 460 udev = y; 461 LIST_FOREACH(si2, &csw->d_devs, si_list) { 462 if (si2->si_drv0 == udev) { 463 dev_free_devlocked(si); 464 return (si2); 465 } 466 } 467 si->si_drv0 = udev; 468 si->si_devsw = csw; 469 LIST_INSERT_HEAD(&csw->d_devs, si, si_list); 470 return (si); 471 } 472 473 int 474 uminor(dev_t dev) 475 { 476 return (dev & MAXMINOR); 477 } 478 479 int 480 umajor(dev_t dev) 481 { 482 return ((dev & ~MAXMINOR) >> 8); 483 } 484 485 static void 486 fini_cdevsw(struct cdevsw *devsw) 487 { 488 struct cdevsw *gt; 489 490 if (devsw->d_gianttrick != NULL) { 491 gt = devsw->d_gianttrick; 492 memcpy(devsw, gt, sizeof *devsw); 493 free(gt, M_DEVT); 494 devsw->d_gianttrick = NULL; 495 } 496 devsw->d_flags &= ~D_INIT; 497 } 498 499 static void 500 prep_cdevsw(struct cdevsw *devsw) 501 { 502 struct cdevsw *dsw2; 503 504 if (devsw->d_flags & D_NEEDGIANT) 505 dsw2 = malloc(sizeof *dsw2, M_DEVT, M_WAITOK); 506 else 507 dsw2 = NULL; 508 dev_lock(); 509 510 if (devsw->d_version != D_VERSION_01) { 511 printf( 512 "WARNING: Device driver \"%s\" has wrong version %s\n", 513 devsw->d_name == NULL ? "???" : devsw->d_name, 514 "and is disabled. Recompile KLD module."); 515 devsw->d_open = dead_open; 516 devsw->d_close = dead_close; 517 devsw->d_read = dead_read; 518 devsw->d_write = dead_write; 519 devsw->d_ioctl = dead_ioctl; 520 devsw->d_poll = dead_poll; 521 devsw->d_mmap = dead_mmap; 522 devsw->d_strategy = dead_strategy; 523 devsw->d_dump = dead_dump; 524 devsw->d_kqfilter = dead_kqfilter; 525 } 526 527 if (devsw->d_flags & D_TTY) { 528 if (devsw->d_ioctl == NULL) devsw->d_ioctl = ttyioctl; 529 if (devsw->d_read == NULL) devsw->d_read = ttyread; 530 if (devsw->d_write == NULL) devsw->d_write = ttywrite; 531 if (devsw->d_kqfilter == NULL) devsw->d_kqfilter = ttykqfilter; 532 if (devsw->d_poll == NULL) devsw->d_poll = ttypoll; 533 } 534 535 if (devsw->d_flags & D_NEEDGIANT) { 536 if (devsw->d_gianttrick == NULL) { 537 memcpy(dsw2, devsw, sizeof *dsw2); 538 devsw->d_gianttrick = dsw2; 539 } else 540 free(dsw2, M_DEVT); 541 } 542 543 #define FIXUP(member, noop, giant) \ 544 do { \ 545 if (devsw->member == NULL) { \ 546 devsw->member = noop; \ 547 } else if (devsw->d_flags & D_NEEDGIANT) \ 548 devsw->member = giant; \ 549 } \ 550 while (0) 551 552 FIXUP(d_open, null_open, giant_open); 553 FIXUP(d_fdopen, NULL, giant_fdopen); 554 FIXUP(d_close, null_close, giant_close); 555 FIXUP(d_read, no_read, giant_read); 556 FIXUP(d_write, no_write, giant_write); 557 FIXUP(d_ioctl, no_ioctl, giant_ioctl); 558 FIXUP(d_poll, no_poll, giant_poll); 559 FIXUP(d_mmap, no_mmap, giant_mmap); 560 FIXUP(d_strategy, no_strategy, giant_strategy); 561 FIXUP(d_kqfilter, no_kqfilter, giant_kqfilter); 562 563 if (devsw->d_dump == NULL) devsw->d_dump = no_dump; 564 565 LIST_INIT(&devsw->d_devs); 566 567 devsw->d_flags |= D_INIT; 568 569 dev_unlock(); 570 } 571 572 struct cdev * 573 make_dev_credv(int flags, struct cdevsw *devsw, int minornr, 574 struct ucred *cr, uid_t uid, 575 gid_t gid, int mode, const char *fmt, va_list ap) 576 { 577 struct cdev *dev; 578 int i; 579 580 KASSERT((minornr & ~MAXMINOR) == 0, 581 ("Invalid minor (0x%x) in make_dev", minornr)); 582 583 if (!(devsw->d_flags & D_INIT)) 584 prep_cdevsw(devsw); 585 dev = devfs_alloc(); 586 dev_lock(); 587 dev = newdev(devsw, minornr, dev); 588 if (flags & MAKEDEV_REF) 589 dev_refl(dev); 590 if (dev->si_flags & SI_CHEAPCLONE && 591 dev->si_flags & SI_NAMED) { 592 /* 593 * This is allowed as it removes races and generally 594 * simplifies cloning devices. 595 * XXX: still ?? 596 */ 597 dev_unlock_and_free(); 598 return (dev); 599 } 600 KASSERT(!(dev->si_flags & SI_NAMED), 601 ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)", 602 devsw->d_name, minor(dev), devtoname(dev))); 603 604 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap); 605 if (i > (sizeof dev->__si_namebuf - 1)) { 606 printf("WARNING: Device name truncated! (%s)\n", 607 dev->__si_namebuf); 608 } 609 610 dev->si_flags |= SI_NAMED; 611 #ifdef MAC 612 if (cr != NULL) 613 dev->si_cred = crhold(cr); 614 else 615 #endif 616 dev->si_cred = NULL; 617 dev->si_uid = uid; 618 dev->si_gid = gid; 619 dev->si_mode = mode; 620 621 devfs_create(dev); 622 clean_unrhdrl(devfs_inos); 623 dev_unlock(); 624 return (dev); 625 } 626 627 struct cdev * 628 make_dev(struct cdevsw *devsw, int minornr, uid_t uid, gid_t gid, int mode, 629 const char *fmt, ...) 630 { 631 struct cdev *dev; 632 va_list ap; 633 634 va_start(ap, fmt); 635 dev = make_dev_credv(0, devsw, minornr, NULL, uid, gid, mode, fmt, ap); 636 va_end(ap); 637 return (dev); 638 } 639 640 struct cdev * 641 make_dev_cred(struct cdevsw *devsw, int minornr, struct ucred *cr, uid_t uid, 642 gid_t gid, int mode, const char *fmt, ...) 643 { 644 struct cdev *dev; 645 va_list ap; 646 647 va_start(ap, fmt); 648 dev = make_dev_credv(0, devsw, minornr, cr, uid, gid, mode, fmt, ap); 649 va_end(ap); 650 651 return (dev); 652 } 653 654 struct cdev * 655 make_dev_credf(int flags, struct cdevsw *devsw, int minornr, 656 struct ucred *cr, uid_t uid, 657 gid_t gid, int mode, const char *fmt, ...) 658 { 659 struct cdev *dev; 660 va_list ap; 661 662 va_start(ap, fmt); 663 dev = make_dev_credv(flags, devsw, minornr, cr, uid, gid, mode, 664 fmt, ap); 665 va_end(ap); 666 667 return (dev); 668 } 669 670 static void 671 dev_dependsl(struct cdev *pdev, struct cdev *cdev) 672 { 673 674 cdev->si_parent = pdev; 675 cdev->si_flags |= SI_CHILD; 676 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings); 677 } 678 679 680 void 681 dev_depends(struct cdev *pdev, struct cdev *cdev) 682 { 683 684 dev_lock(); 685 dev_dependsl(pdev, cdev); 686 dev_unlock(); 687 } 688 689 struct cdev * 690 make_dev_alias(struct cdev *pdev, const char *fmt, ...) 691 { 692 struct cdev *dev; 693 va_list ap; 694 int i; 695 696 dev = devfs_alloc(); 697 dev_lock(); 698 dev->si_flags |= SI_ALIAS; 699 dev->si_flags |= SI_NAMED; 700 va_start(ap, fmt); 701 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap); 702 if (i > (sizeof dev->__si_namebuf - 1)) { 703 printf("WARNING: Device name truncated! (%s)\n", 704 dev->__si_namebuf); 705 } 706 va_end(ap); 707 708 devfs_create(dev); 709 clean_unrhdrl(devfs_inos); 710 dev_unlock(); 711 dev_depends(pdev, dev); 712 return (dev); 713 } 714 715 static void 716 destroy_devl(struct cdev *dev) 717 { 718 struct cdevsw *csw; 719 720 mtx_assert(&devmtx, MA_OWNED); 721 KASSERT(dev->si_flags & SI_NAMED, 722 ("WARNING: Driver mistake: destroy_dev on %d\n", minor(dev))); 723 724 devfs_destroy(dev); 725 726 /* Remove name marking */ 727 dev->si_flags &= ~SI_NAMED; 728 729 /* If we are a child, remove us from the parents list */ 730 if (dev->si_flags & SI_CHILD) { 731 LIST_REMOVE(dev, si_siblings); 732 dev->si_flags &= ~SI_CHILD; 733 } 734 735 /* Kill our children */ 736 while (!LIST_EMPTY(&dev->si_children)) 737 destroy_devl(LIST_FIRST(&dev->si_children)); 738 739 /* Remove from clone list */ 740 if (dev->si_flags & SI_CLONELIST) { 741 LIST_REMOVE(dev, si_clone); 742 dev->si_flags &= ~SI_CLONELIST; 743 } 744 745 dev->si_refcount++; /* Avoid race with dev_rel() */ 746 csw = dev->si_devsw; 747 dev->si_devsw = NULL; /* already NULL for SI_ALIAS */ 748 while (csw != NULL && csw->d_purge != NULL && dev->si_threadcount) { 749 csw->d_purge(dev); 750 msleep(csw, &devmtx, PRIBIO, "devprg", hz/10); 751 if (dev->si_threadcount) 752 printf("Still %lu threads in %s\n", 753 dev->si_threadcount, devtoname(dev)); 754 } 755 while (dev->si_threadcount != 0) { 756 /* Use unique dummy wait ident */ 757 msleep(&csw, &devmtx, PRIBIO, "devdrn", hz / 10); 758 } 759 760 dev->si_drv1 = 0; 761 dev->si_drv2 = 0; 762 bzero(&dev->__si_u, sizeof(dev->__si_u)); 763 764 if (!(dev->si_flags & SI_ALIAS)) { 765 /* Remove from cdevsw list */ 766 LIST_REMOVE(dev, si_list); 767 768 /* If cdevsw has no more struct cdev *'s, clean it */ 769 if (LIST_EMPTY(&csw->d_devs)) { 770 fini_cdevsw(csw); 771 wakeup(&csw->d_devs); 772 } 773 } 774 dev->si_flags &= ~SI_ALIAS; 775 dev->si_refcount--; /* Avoid race with dev_rel() */ 776 777 if (dev->si_refcount > 0) { 778 LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list); 779 } else { 780 dev_free_devlocked(dev); 781 } 782 } 783 784 void 785 destroy_dev(struct cdev *dev) 786 { 787 788 dev_lock(); 789 destroy_devl(dev); 790 dev_unlock_and_free(); 791 } 792 793 const char * 794 devtoname(struct cdev *dev) 795 { 796 char *p; 797 struct cdevsw *csw; 798 int mynor; 799 800 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') { 801 p = dev->si_name; 802 csw = dev_refthread(dev); 803 if (csw != NULL) { 804 sprintf(p, "(%s)", csw->d_name); 805 dev_relthread(dev); 806 } 807 p += strlen(p); 808 mynor = minor(dev); 809 if (mynor < 0 || mynor > 255) 810 sprintf(p, "/%#x", (u_int)mynor); 811 else 812 sprintf(p, "/%d", mynor); 813 } 814 return (dev->si_name); 815 } 816 817 int 818 dev_stdclone(char *name, char **namep, const char *stem, int *unit) 819 { 820 int u, i; 821 822 i = strlen(stem); 823 if (bcmp(stem, name, i) != 0) 824 return (0); 825 if (!isdigit(name[i])) 826 return (0); 827 u = 0; 828 if (name[i] == '0' && isdigit(name[i+1])) 829 return (0); 830 while (isdigit(name[i])) { 831 u *= 10; 832 u += name[i++] - '0'; 833 } 834 if (u > 0xffffff) 835 return (0); 836 *unit = u; 837 if (namep) 838 *namep = &name[i]; 839 if (name[i]) 840 return (2); 841 return (1); 842 } 843 844 /* 845 * Helper functions for cloning device drivers. 846 * 847 * The objective here is to make it unnecessary for the device drivers to 848 * use rman or similar to manage their unit number space. Due to the way 849 * we do "on-demand" devices, using rman or other "private" methods 850 * will be very tricky to lock down properly once we lock down this file. 851 * 852 * Instead we give the drivers these routines which puts the struct cdev *'s 853 * that are to be managed on their own list, and gives the driver the ability 854 * to ask for the first free unit number or a given specified unit number. 855 * 856 * In addition these routines support paired devices (pty, nmdm and similar) 857 * by respecting a number of "flag" bits in the minor number. 858 * 859 */ 860 861 struct clonedevs { 862 LIST_HEAD(,cdev) head; 863 }; 864 865 void 866 clone_setup(struct clonedevs **cdp) 867 { 868 869 *cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO); 870 LIST_INIT(&(*cdp)->head); 871 } 872 873 int 874 clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up, struct cdev **dp, int extra) 875 { 876 struct clonedevs *cd; 877 struct cdev *dev, *ndev, *dl, *de; 878 int unit, low, u; 879 880 KASSERT(*cdp != NULL, 881 ("clone_setup() not called in driver \"%s\"", csw->d_name)); 882 KASSERT(!(extra & CLONE_UNITMASK), 883 ("Illegal extra bits (0x%x) in clone_create", extra)); 884 KASSERT(*up <= CLONE_UNITMASK, 885 ("Too high unit (0x%x) in clone_create", *up)); 886 887 if (!(csw->d_flags & D_INIT)) 888 prep_cdevsw(csw); 889 890 /* 891 * Search the list for a lot of things in one go: 892 * A preexisting match is returned immediately. 893 * The lowest free unit number if we are passed -1, and the place 894 * in the list where we should insert that new element. 895 * The place to insert a specified unit number, if applicable 896 * the end of the list. 897 */ 898 unit = *up; 899 ndev = devfs_alloc(); 900 dev_lock(); 901 low = extra; 902 de = dl = NULL; 903 cd = *cdp; 904 LIST_FOREACH(dev, &cd->head, si_clone) { 905 KASSERT(dev->si_flags & SI_CLONELIST, 906 ("Dev %p(%s) should be on clonelist", dev, dev->si_name)); 907 u = dev2unit(dev); 908 if (u == (unit | extra)) { 909 *dp = dev; 910 dev_unlock(); 911 devfs_free(ndev); 912 return (0); 913 } 914 if (unit == -1 && u == low) { 915 low++; 916 de = dev; 917 continue; 918 } else if (u < (unit | extra)) { 919 de = dev; 920 continue; 921 } else if (u > (unit | extra)) { 922 dl = dev; 923 break; 924 } 925 } 926 if (unit == -1) 927 unit = low & CLONE_UNITMASK; 928 dev = newdev(csw, unit2minor(unit | extra), ndev); 929 if (dev->si_flags & SI_CLONELIST) { 930 printf("dev %p (%s) is on clonelist\n", dev, dev->si_name); 931 printf("unit=%d, low=%d, extra=0x%x\n", unit, low, extra); 932 LIST_FOREACH(dev, &cd->head, si_clone) { 933 printf("\t%p %s\n", dev, dev->si_name); 934 } 935 panic("foo"); 936 } 937 KASSERT(!(dev->si_flags & SI_CLONELIST), 938 ("Dev %p(%s) should not be on clonelist", dev, dev->si_name)); 939 if (dl != NULL) 940 LIST_INSERT_BEFORE(dl, dev, si_clone); 941 else if (de != NULL) 942 LIST_INSERT_AFTER(de, dev, si_clone); 943 else 944 LIST_INSERT_HEAD(&cd->head, dev, si_clone); 945 dev->si_flags |= SI_CLONELIST; 946 *up = unit; 947 dev_unlock_and_free(); 948 return (1); 949 } 950 951 /* 952 * Kill everything still on the list. The driver should already have 953 * disposed of any softc hung of the struct cdev *'s at this time. 954 */ 955 void 956 clone_cleanup(struct clonedevs **cdp) 957 { 958 struct cdev *dev; 959 struct cdev_priv *cp; 960 struct clonedevs *cd; 961 962 cd = *cdp; 963 if (cd == NULL) 964 return; 965 dev_lock(); 966 while (!LIST_EMPTY(&cd->head)) { 967 dev = LIST_FIRST(&cd->head); 968 LIST_REMOVE(dev, si_clone); 969 KASSERT(dev->si_flags & SI_CLONELIST, 970 ("Dev %p(%s) should be on clonelist", dev, dev->si_name)); 971 dev->si_flags &= ~SI_CLONELIST; 972 cp = dev->si_priv; 973 if (!(cp->cdp_flags & CDP_SCHED_DTR)) { 974 cp->cdp_flags |= CDP_SCHED_DTR; 975 KASSERT(dev->si_flags & SI_NAMED, 976 ("Driver has goofed in cloning underways udev %x", dev->si_drv0)); 977 destroy_devl(dev); 978 } 979 } 980 dev_unlock(); 981 free(cd, M_DEVBUF); 982 *cdp = NULL; 983 } 984 985 static TAILQ_HEAD(, cdev_priv) dev_ddtr = 986 TAILQ_HEAD_INITIALIZER(dev_ddtr); 987 static struct task dev_dtr_task; 988 989 static void 990 destroy_dev_tq(void *ctx, int pending) 991 { 992 struct cdev_priv *cp; 993 struct cdev *dev; 994 void (*cb)(void *); 995 void *cb_arg; 996 997 dev_lock(); 998 while (!TAILQ_EMPTY(&dev_ddtr)) { 999 cp = TAILQ_FIRST(&dev_ddtr); 1000 dev = &cp->cdp_c; 1001 KASSERT(cp->cdp_flags & CDP_SCHED_DTR, 1002 ("cdev %p in dev_destroy_tq without CDP_SCHED_DTR", cp)); 1003 TAILQ_REMOVE(&dev_ddtr, cp, cdp_dtr_list); 1004 cb = cp->cdp_dtr_cb; 1005 cb_arg = cp->cdp_dtr_cb_arg; 1006 destroy_devl(dev); 1007 dev_unlock(); 1008 dev_rel(dev); 1009 if (cb != NULL) 1010 cb(cb_arg); 1011 dev_lock(); 1012 } 1013 dev_unlock(); 1014 } 1015 1016 /* 1017 * devmtx shall be locked on entry. devmtx will be unlocked after 1018 * function return. 1019 */ 1020 static int 1021 destroy_dev_sched_cbl(struct cdev *dev, void (*cb)(void *), void *arg) 1022 { 1023 struct cdev_priv *cp; 1024 1025 mtx_assert(&devmtx, MA_OWNED); 1026 cp = dev->si_priv; 1027 if (cp->cdp_flags & CDP_SCHED_DTR) { 1028 dev_unlock(); 1029 return (0); 1030 } 1031 dev_refl(dev); 1032 cp->cdp_flags |= CDP_SCHED_DTR; 1033 cp->cdp_dtr_cb = cb; 1034 cp->cdp_dtr_cb_arg = arg; 1035 TAILQ_INSERT_TAIL(&dev_ddtr, cp, cdp_dtr_list); 1036 dev_unlock(); 1037 taskqueue_enqueue(taskqueue_swi_giant, &dev_dtr_task); 1038 return (1); 1039 } 1040 1041 int 1042 destroy_dev_sched_cb(struct cdev *dev, void (*cb)(void *), void *arg) 1043 { 1044 dev_lock(); 1045 return (destroy_dev_sched_cbl(dev, cb, arg)); 1046 } 1047 1048 int 1049 destroy_dev_sched(struct cdev *dev) 1050 { 1051 return (destroy_dev_sched_cb(dev, NULL, NULL)); 1052 } 1053 1054 void 1055 destroy_dev_drain(struct cdevsw *csw) 1056 { 1057 1058 dev_lock(); 1059 while (!LIST_EMPTY(&csw->d_devs)) { 1060 msleep(&csw->d_devs, &devmtx, PRIBIO, "devscd", hz/10); 1061 } 1062 dev_unlock(); 1063 } 1064 1065 void 1066 drain_dev_clone_events(void) 1067 { 1068 1069 sx_xlock(&clone_drain_lock); 1070 sx_xunlock(&clone_drain_lock); 1071 } 1072 1073 static void 1074 devdtr_init(void *dummy __unused) 1075 { 1076 1077 TASK_INIT(&dev_dtr_task, 0, destroy_dev_tq, NULL); 1078 } 1079 1080 SYSINIT(devdtr, SI_SUB_DEVFS, SI_ORDER_SECOND, devdtr_init, NULL); 1081