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/bus.h> 34 #include <sys/bio.h> 35 #include <sys/lock.h> 36 #include <sys/mutex.h> 37 #include <sys/module.h> 38 #include <sys/malloc.h> 39 #include <sys/conf.h> 40 #include <sys/vnode.h> 41 #include <sys/queue.h> 42 #include <sys/poll.h> 43 #include <sys/sx.h> 44 #include <sys/ctype.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 #include <vm/vm.h> 51 52 static MALLOC_DEFINE(M_DEVT, "cdev", "cdev storage"); 53 54 struct mtx devmtx; 55 static void destroy_devl(struct cdev *dev); 56 static int destroy_dev_sched_cbl(struct cdev *dev, 57 void (*cb)(void *), void *arg); 58 static void destroy_dev_tq(void *ctx, int pending); 59 static int make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw, 60 int unit, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, 61 va_list ap); 62 63 static struct cdev_priv_list cdevp_free_list = 64 TAILQ_HEAD_INITIALIZER(cdevp_free_list); 65 static SLIST_HEAD(free_cdevsw, cdevsw) cdevsw_gt_post_list = 66 SLIST_HEAD_INITIALIZER(cdevsw_gt_post_list); 67 68 void 69 dev_lock(void) 70 { 71 72 mtx_lock(&devmtx); 73 } 74 75 /* 76 * Free all the memory collected while the cdev mutex was 77 * locked. Since devmtx is after the system map mutex, free() cannot 78 * be called immediately and is postponed until cdev mutex can be 79 * dropped. 80 */ 81 static void 82 dev_unlock_and_free(void) 83 { 84 struct cdev_priv_list cdp_free; 85 struct free_cdevsw csw_free; 86 struct cdev_priv *cdp; 87 struct cdevsw *csw; 88 89 mtx_assert(&devmtx, MA_OWNED); 90 91 /* 92 * Make the local copy of the list heads while the dev_mtx is 93 * held. Free it later. 94 */ 95 TAILQ_INIT(&cdp_free); 96 TAILQ_CONCAT(&cdp_free, &cdevp_free_list, cdp_list); 97 csw_free = cdevsw_gt_post_list; 98 SLIST_INIT(&cdevsw_gt_post_list); 99 100 mtx_unlock(&devmtx); 101 102 while ((cdp = TAILQ_FIRST(&cdp_free)) != NULL) { 103 TAILQ_REMOVE(&cdp_free, cdp, cdp_list); 104 devfs_free(&cdp->cdp_c); 105 } 106 while ((csw = SLIST_FIRST(&csw_free)) != NULL) { 107 SLIST_REMOVE_HEAD(&csw_free, d_postfree_list); 108 free(csw, M_DEVT); 109 } 110 } 111 112 static void 113 dev_free_devlocked(struct cdev *cdev) 114 { 115 struct cdev_priv *cdp; 116 117 mtx_assert(&devmtx, MA_OWNED); 118 cdp = cdev2priv(cdev); 119 KASSERT((cdp->cdp_flags & CDP_UNREF_DTR) == 0, 120 ("destroy_dev() was not called after delist_dev(%p)", cdev)); 121 TAILQ_INSERT_HEAD(&cdevp_free_list, cdp, cdp_list); 122 } 123 124 static void 125 cdevsw_free_devlocked(struct cdevsw *csw) 126 { 127 128 mtx_assert(&devmtx, MA_OWNED); 129 SLIST_INSERT_HEAD(&cdevsw_gt_post_list, csw, d_postfree_list); 130 } 131 132 void 133 dev_unlock(void) 134 { 135 136 mtx_unlock(&devmtx); 137 } 138 139 void 140 dev_ref(struct cdev *dev) 141 { 142 143 mtx_assert(&devmtx, MA_NOTOWNED); 144 mtx_lock(&devmtx); 145 dev->si_refcount++; 146 mtx_unlock(&devmtx); 147 } 148 149 void 150 dev_refl(struct cdev *dev) 151 { 152 153 mtx_assert(&devmtx, MA_OWNED); 154 dev->si_refcount++; 155 } 156 157 void 158 dev_rel(struct cdev *dev) 159 { 160 int flag = 0; 161 162 mtx_assert(&devmtx, MA_NOTOWNED); 163 dev_lock(); 164 dev->si_refcount--; 165 KASSERT(dev->si_refcount >= 0, 166 ("dev_rel(%s) gave negative count", devtoname(dev))); 167 #if 0 168 if (dev->si_usecount == 0 && 169 (dev->si_flags & SI_CHEAPCLONE) && (dev->si_flags & SI_NAMED)) 170 ; 171 else 172 #endif 173 if (dev->si_devsw == NULL && dev->si_refcount == 0) { 174 LIST_REMOVE(dev, si_list); 175 flag = 1; 176 } 177 dev_unlock(); 178 if (flag) 179 devfs_free(dev); 180 } 181 182 struct cdevsw * 183 dev_refthread(struct cdev *dev, int *ref) 184 { 185 struct cdevsw *csw; 186 struct cdev_priv *cdp; 187 188 mtx_assert(&devmtx, MA_NOTOWNED); 189 if ((dev->si_flags & SI_ETERNAL) != 0) { 190 *ref = 0; 191 return (dev->si_devsw); 192 } 193 dev_lock(); 194 csw = dev->si_devsw; 195 if (csw != NULL) { 196 cdp = cdev2priv(dev); 197 if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0) 198 atomic_add_long(&dev->si_threadcount, 1); 199 else 200 csw = NULL; 201 } 202 dev_unlock(); 203 *ref = 1; 204 return (csw); 205 } 206 207 struct cdevsw * 208 devvn_refthread(struct vnode *vp, struct cdev **devp, int *ref) 209 { 210 struct cdevsw *csw; 211 struct cdev_priv *cdp; 212 struct cdev *dev; 213 214 mtx_assert(&devmtx, MA_NOTOWNED); 215 if ((vp->v_vflag & VV_ETERNALDEV) != 0) { 216 dev = vp->v_rdev; 217 if (dev == NULL) 218 return (NULL); 219 KASSERT((dev->si_flags & SI_ETERNAL) != 0, 220 ("Not eternal cdev")); 221 *ref = 0; 222 csw = dev->si_devsw; 223 KASSERT(csw != NULL, ("Eternal cdev is destroyed")); 224 *devp = dev; 225 return (csw); 226 } 227 228 csw = NULL; 229 dev_lock(); 230 dev = vp->v_rdev; 231 if (dev == NULL) { 232 dev_unlock(); 233 return (NULL); 234 } 235 cdp = cdev2priv(dev); 236 if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0) { 237 csw = dev->si_devsw; 238 if (csw != NULL) 239 atomic_add_long(&dev->si_threadcount, 1); 240 } 241 dev_unlock(); 242 if (csw != NULL) { 243 *devp = dev; 244 *ref = 1; 245 } 246 return (csw); 247 } 248 249 void 250 dev_relthread(struct cdev *dev, int ref) 251 { 252 253 mtx_assert(&devmtx, MA_NOTOWNED); 254 if (!ref) 255 return; 256 KASSERT(dev->si_threadcount > 0, 257 ("%s threadcount is wrong", dev->si_name)); 258 atomic_subtract_rel_long(&dev->si_threadcount, 1); 259 } 260 261 int 262 nullop(void) 263 { 264 265 return (0); 266 } 267 268 int 269 eopnotsupp(void) 270 { 271 272 return (EOPNOTSUPP); 273 } 274 275 static int 276 enxio(void) 277 { 278 return (ENXIO); 279 } 280 281 static int 282 enodev(void) 283 { 284 return (ENODEV); 285 } 286 287 /* Define a dead_cdevsw for use when devices leave unexpectedly. */ 288 289 #define dead_open (d_open_t *)enxio 290 #define dead_close (d_close_t *)enxio 291 #define dead_read (d_read_t *)enxio 292 #define dead_write (d_write_t *)enxio 293 #define dead_ioctl (d_ioctl_t *)enxio 294 #define dead_poll (d_poll_t *)enodev 295 #define dead_mmap (d_mmap_t *)enodev 296 297 static void 298 dead_strategy(struct bio *bp) 299 { 300 301 biofinish(bp, NULL, ENXIO); 302 } 303 304 #define dead_dump (dumper_t *)enxio 305 #define dead_kqfilter (d_kqfilter_t *)enxio 306 #define dead_mmap_single (d_mmap_single_t *)enodev 307 308 static struct cdevsw dead_cdevsw = { 309 .d_version = D_VERSION, 310 .d_open = dead_open, 311 .d_close = dead_close, 312 .d_read = dead_read, 313 .d_write = dead_write, 314 .d_ioctl = dead_ioctl, 315 .d_poll = dead_poll, 316 .d_mmap = dead_mmap, 317 .d_strategy = dead_strategy, 318 .d_name = "dead", 319 .d_dump = dead_dump, 320 .d_kqfilter = dead_kqfilter, 321 .d_mmap_single = dead_mmap_single 322 }; 323 324 /* Default methods if driver does not specify method */ 325 326 #define null_open (d_open_t *)nullop 327 #define null_close (d_close_t *)nullop 328 #define no_read (d_read_t *)enodev 329 #define no_write (d_write_t *)enodev 330 #define no_ioctl (d_ioctl_t *)enodev 331 #define no_mmap (d_mmap_t *)enodev 332 #define no_kqfilter (d_kqfilter_t *)enodev 333 #define no_mmap_single (d_mmap_single_t *)enodev 334 335 static void 336 no_strategy(struct bio *bp) 337 { 338 339 biofinish(bp, NULL, ENODEV); 340 } 341 342 static int 343 no_poll(struct cdev *dev __unused, int events, struct thread *td __unused) 344 { 345 346 return (poll_no_poll(events)); 347 } 348 349 #define no_dump (dumper_t *)enodev 350 351 static int 352 giant_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 353 { 354 struct cdevsw *dsw; 355 int ref, retval; 356 357 dsw = dev_refthread(dev, &ref); 358 if (dsw == NULL) 359 return (ENXIO); 360 mtx_lock(&Giant); 361 retval = dsw->d_gianttrick->d_open(dev, oflags, devtype, td); 362 mtx_unlock(&Giant); 363 dev_relthread(dev, ref); 364 return (retval); 365 } 366 367 static int 368 giant_fdopen(struct cdev *dev, int oflags, struct thread *td, struct file *fp) 369 { 370 struct cdevsw *dsw; 371 int ref, retval; 372 373 dsw = dev_refthread(dev, &ref); 374 if (dsw == NULL) 375 return (ENXIO); 376 mtx_lock(&Giant); 377 retval = dsw->d_gianttrick->d_fdopen(dev, oflags, td, fp); 378 mtx_unlock(&Giant); 379 dev_relthread(dev, ref); 380 return (retval); 381 } 382 383 static int 384 giant_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 385 { 386 struct cdevsw *dsw; 387 int ref, retval; 388 389 dsw = dev_refthread(dev, &ref); 390 if (dsw == NULL) 391 return (ENXIO); 392 mtx_lock(&Giant); 393 retval = dsw->d_gianttrick->d_close(dev, fflag, devtype, td); 394 mtx_unlock(&Giant); 395 dev_relthread(dev, ref); 396 return (retval); 397 } 398 399 static void 400 giant_strategy(struct bio *bp) 401 { 402 struct cdevsw *dsw; 403 struct cdev *dev; 404 int ref; 405 406 dev = bp->bio_dev; 407 dsw = dev_refthread(dev, &ref); 408 if (dsw == NULL) { 409 biofinish(bp, NULL, ENXIO); 410 return; 411 } 412 mtx_lock(&Giant); 413 dsw->d_gianttrick->d_strategy(bp); 414 mtx_unlock(&Giant); 415 dev_relthread(dev, ref); 416 } 417 418 static int 419 giant_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 420 { 421 struct cdevsw *dsw; 422 int ref, retval; 423 424 dsw = dev_refthread(dev, &ref); 425 if (dsw == NULL) 426 return (ENXIO); 427 mtx_lock(&Giant); 428 retval = dsw->d_gianttrick->d_ioctl(dev, cmd, data, fflag, td); 429 mtx_unlock(&Giant); 430 dev_relthread(dev, ref); 431 return (retval); 432 } 433 434 static int 435 giant_read(struct cdev *dev, struct uio *uio, int ioflag) 436 { 437 struct cdevsw *dsw; 438 int ref, retval; 439 440 dsw = dev_refthread(dev, &ref); 441 if (dsw == NULL) 442 return (ENXIO); 443 mtx_lock(&Giant); 444 retval = dsw->d_gianttrick->d_read(dev, uio, ioflag); 445 mtx_unlock(&Giant); 446 dev_relthread(dev, ref); 447 return (retval); 448 } 449 450 static int 451 giant_write(struct cdev *dev, struct uio *uio, int ioflag) 452 { 453 struct cdevsw *dsw; 454 int ref, retval; 455 456 dsw = dev_refthread(dev, &ref); 457 if (dsw == NULL) 458 return (ENXIO); 459 mtx_lock(&Giant); 460 retval = dsw->d_gianttrick->d_write(dev, uio, ioflag); 461 mtx_unlock(&Giant); 462 dev_relthread(dev, ref); 463 return (retval); 464 } 465 466 static int 467 giant_poll(struct cdev *dev, int events, struct thread *td) 468 { 469 struct cdevsw *dsw; 470 int ref, retval; 471 472 dsw = dev_refthread(dev, &ref); 473 if (dsw == NULL) 474 return (ENXIO); 475 mtx_lock(&Giant); 476 retval = dsw->d_gianttrick->d_poll(dev, events, td); 477 mtx_unlock(&Giant); 478 dev_relthread(dev, ref); 479 return (retval); 480 } 481 482 static int 483 giant_kqfilter(struct cdev *dev, struct knote *kn) 484 { 485 struct cdevsw *dsw; 486 int ref, retval; 487 488 dsw = dev_refthread(dev, &ref); 489 if (dsw == NULL) 490 return (ENXIO); 491 mtx_lock(&Giant); 492 retval = dsw->d_gianttrick->d_kqfilter(dev, kn); 493 mtx_unlock(&Giant); 494 dev_relthread(dev, ref); 495 return (retval); 496 } 497 498 static int 499 giant_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot, 500 vm_memattr_t *memattr) 501 { 502 struct cdevsw *dsw; 503 int ref, retval; 504 505 dsw = dev_refthread(dev, &ref); 506 if (dsw == NULL) 507 return (ENXIO); 508 mtx_lock(&Giant); 509 retval = dsw->d_gianttrick->d_mmap(dev, offset, paddr, nprot, 510 memattr); 511 mtx_unlock(&Giant); 512 dev_relthread(dev, ref); 513 return (retval); 514 } 515 516 static int 517 giant_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size, 518 vm_object_t *object, int nprot) 519 { 520 struct cdevsw *dsw; 521 int ref, retval; 522 523 dsw = dev_refthread(dev, &ref); 524 if (dsw == NULL) 525 return (ENXIO); 526 mtx_lock(&Giant); 527 retval = dsw->d_gianttrick->d_mmap_single(dev, offset, size, object, 528 nprot); 529 mtx_unlock(&Giant); 530 dev_relthread(dev, ref); 531 return (retval); 532 } 533 534 static void 535 notify(struct cdev *dev, const char *ev, int flags) 536 { 537 static const char prefix[] = "cdev="; 538 char *data; 539 int namelen, mflags; 540 541 if (cold) 542 return; 543 mflags = (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK; 544 namelen = strlen(dev->si_name); 545 data = malloc(namelen + sizeof(prefix), M_TEMP, mflags); 546 if (data == NULL) 547 return; 548 memcpy(data, prefix, sizeof(prefix) - 1); 549 memcpy(data + sizeof(prefix) - 1, dev->si_name, namelen + 1); 550 devctl_notify_f("DEVFS", "CDEV", ev, data, mflags); 551 free(data, M_TEMP); 552 } 553 554 static void 555 notify_create(struct cdev *dev, int flags) 556 { 557 558 notify(dev, "CREATE", flags); 559 } 560 561 static void 562 notify_destroy(struct cdev *dev) 563 { 564 565 notify(dev, "DESTROY", MAKEDEV_WAITOK); 566 } 567 568 static struct cdev * 569 newdev(struct make_dev_args *args, struct cdev *si) 570 { 571 struct cdev *si2; 572 struct cdevsw *csw; 573 574 mtx_assert(&devmtx, MA_OWNED); 575 csw = args->mda_devsw; 576 if (csw->d_flags & D_NEEDMINOR) { 577 /* We may want to return an existing device */ 578 LIST_FOREACH(si2, &csw->d_devs, si_list) { 579 if (dev2unit(si2) == args->mda_unit) { 580 dev_free_devlocked(si); 581 return (si2); 582 } 583 } 584 } 585 si->si_drv0 = args->mda_unit; 586 si->si_devsw = csw; 587 si->si_drv1 = args->mda_si_drv1; 588 si->si_drv2 = args->mda_si_drv2; 589 LIST_INSERT_HEAD(&csw->d_devs, si, si_list); 590 return (si); 591 } 592 593 static void 594 fini_cdevsw(struct cdevsw *devsw) 595 { 596 struct cdevsw *gt; 597 598 if (devsw->d_gianttrick != NULL) { 599 gt = devsw->d_gianttrick; 600 memcpy(devsw, gt, sizeof *devsw); 601 cdevsw_free_devlocked(gt); 602 devsw->d_gianttrick = NULL; 603 } 604 devsw->d_flags &= ~D_INIT; 605 } 606 607 static int 608 prep_cdevsw(struct cdevsw *devsw, int flags) 609 { 610 struct cdevsw *dsw2; 611 612 mtx_assert(&devmtx, MA_OWNED); 613 if (devsw->d_flags & D_INIT) 614 return (0); 615 if (devsw->d_flags & D_NEEDGIANT) { 616 dev_unlock(); 617 dsw2 = malloc(sizeof *dsw2, M_DEVT, 618 (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK); 619 dev_lock(); 620 if (dsw2 == NULL && !(devsw->d_flags & D_INIT)) 621 return (ENOMEM); 622 } else 623 dsw2 = NULL; 624 if (devsw->d_flags & D_INIT) { 625 if (dsw2 != NULL) 626 cdevsw_free_devlocked(dsw2); 627 return (0); 628 } 629 630 if (devsw->d_version != D_VERSION_03) { 631 printf( 632 "WARNING: Device driver \"%s\" has wrong version %s\n", 633 devsw->d_name == NULL ? "???" : devsw->d_name, 634 "and is disabled. Recompile KLD module."); 635 devsw->d_open = dead_open; 636 devsw->d_close = dead_close; 637 devsw->d_read = dead_read; 638 devsw->d_write = dead_write; 639 devsw->d_ioctl = dead_ioctl; 640 devsw->d_poll = dead_poll; 641 devsw->d_mmap = dead_mmap; 642 devsw->d_mmap_single = dead_mmap_single; 643 devsw->d_strategy = dead_strategy; 644 devsw->d_dump = dead_dump; 645 devsw->d_kqfilter = dead_kqfilter; 646 } 647 648 if (devsw->d_flags & D_NEEDGIANT) { 649 if (devsw->d_gianttrick == NULL) { 650 memcpy(dsw2, devsw, sizeof *dsw2); 651 devsw->d_gianttrick = dsw2; 652 dsw2 = NULL; 653 } 654 } 655 656 #define FIXUP(member, noop, giant) \ 657 do { \ 658 if (devsw->member == NULL) { \ 659 devsw->member = noop; \ 660 } else if (devsw->d_flags & D_NEEDGIANT) \ 661 devsw->member = giant; \ 662 } \ 663 while (0) 664 665 FIXUP(d_open, null_open, giant_open); 666 FIXUP(d_fdopen, NULL, giant_fdopen); 667 FIXUP(d_close, null_close, giant_close); 668 FIXUP(d_read, no_read, giant_read); 669 FIXUP(d_write, no_write, giant_write); 670 FIXUP(d_ioctl, no_ioctl, giant_ioctl); 671 FIXUP(d_poll, no_poll, giant_poll); 672 FIXUP(d_mmap, no_mmap, giant_mmap); 673 FIXUP(d_strategy, no_strategy, giant_strategy); 674 FIXUP(d_kqfilter, no_kqfilter, giant_kqfilter); 675 FIXUP(d_mmap_single, no_mmap_single, giant_mmap_single); 676 677 if (devsw->d_dump == NULL) devsw->d_dump = no_dump; 678 679 LIST_INIT(&devsw->d_devs); 680 681 devsw->d_flags |= D_INIT; 682 683 if (dsw2 != NULL) 684 cdevsw_free_devlocked(dsw2); 685 return (0); 686 } 687 688 static int 689 prep_devname(struct cdev *dev, const char *fmt, va_list ap) 690 { 691 int len; 692 char *from, *q, *s, *to; 693 694 mtx_assert(&devmtx, MA_OWNED); 695 696 len = vsnrprintf(dev->si_name, sizeof(dev->si_name), 32, fmt, ap); 697 if (len > sizeof(dev->si_name) - 1) 698 return (ENAMETOOLONG); 699 700 /* Strip leading slashes. */ 701 for (from = dev->si_name; *from == '/'; from++) 702 ; 703 704 for (to = dev->si_name; *from != '\0'; from++, to++) { 705 /* 706 * Spaces and double quotation marks cause 707 * problems for the devctl(4) protocol. 708 * Reject names containing those characters. 709 */ 710 if (isspace(*from) || *from == '"') 711 return (EINVAL); 712 /* Treat multiple sequential slashes as single. */ 713 while (from[0] == '/' && from[1] == '/') 714 from++; 715 /* Trailing slash is considered invalid. */ 716 if (from[0] == '/' && from[1] == '\0') 717 return (EINVAL); 718 *to = *from; 719 } 720 *to = '\0'; 721 722 if (dev->si_name[0] == '\0') 723 return (EINVAL); 724 725 /* Disallow "." and ".." components. */ 726 for (s = dev->si_name;;) { 727 for (q = s; *q != '/' && *q != '\0'; q++) 728 ; 729 if (q - s == 1 && s[0] == '.') 730 return (EINVAL); 731 if (q - s == 2 && s[0] == '.' && s[1] == '.') 732 return (EINVAL); 733 if (*q != '/') 734 break; 735 s = q + 1; 736 } 737 738 if (devfs_dev_exists(dev->si_name) != 0) 739 return (EEXIST); 740 741 return (0); 742 } 743 744 void 745 make_dev_args_init_impl(struct make_dev_args *args, size_t sz) 746 { 747 748 bzero(args, sz); 749 args->mda_size = sz; 750 } 751 752 static int 753 make_dev_sv(struct make_dev_args *args1, struct cdev **dres, 754 const char *fmt, va_list ap) 755 { 756 struct cdev *dev, *dev_new; 757 struct make_dev_args args; 758 int res; 759 760 bzero(&args, sizeof(args)); 761 if (sizeof(args) < args1->mda_size) 762 return (EINVAL); 763 bcopy(args1, &args, args1->mda_size); 764 KASSERT((args.mda_flags & MAKEDEV_WAITOK) == 0 || 765 (args.mda_flags & MAKEDEV_NOWAIT) == 0, 766 ("make_dev_sv: both WAITOK and NOWAIT specified")); 767 dev_new = devfs_alloc(args.mda_flags); 768 if (dev_new == NULL) 769 return (ENOMEM); 770 dev_lock(); 771 res = prep_cdevsw(args.mda_devsw, args.mda_flags); 772 if (res != 0) { 773 dev_unlock(); 774 devfs_free(dev_new); 775 return (res); 776 } 777 dev = newdev(&args, dev_new); 778 if ((dev->si_flags & SI_NAMED) == 0) { 779 res = prep_devname(dev, fmt, ap); 780 if (res != 0) { 781 if ((args.mda_flags & MAKEDEV_CHECKNAME) == 0) { 782 panic( 783 "make_dev_sv: bad si_name (error=%d, si_name=%s)", 784 res, dev->si_name); 785 } 786 if (dev == dev_new) { 787 LIST_REMOVE(dev, si_list); 788 dev_unlock(); 789 devfs_free(dev); 790 } else 791 dev_unlock(); 792 return (res); 793 } 794 } 795 if ((args.mda_flags & MAKEDEV_REF) != 0) 796 dev_refl(dev); 797 if ((args.mda_flags & MAKEDEV_ETERNAL) != 0) 798 dev->si_flags |= SI_ETERNAL; 799 if (dev->si_flags & SI_CHEAPCLONE && 800 dev->si_flags & SI_NAMED) { 801 /* 802 * This is allowed as it removes races and generally 803 * simplifies cloning devices. 804 * XXX: still ?? 805 */ 806 dev_unlock_and_free(); 807 *dres = dev; 808 return (0); 809 } 810 KASSERT(!(dev->si_flags & SI_NAMED), 811 ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)", 812 args.mda_devsw->d_name, dev2unit(dev), devtoname(dev))); 813 dev->si_flags |= SI_NAMED; 814 if (args.mda_cr != NULL) 815 dev->si_cred = crhold(args.mda_cr); 816 dev->si_uid = args.mda_uid; 817 dev->si_gid = args.mda_gid; 818 dev->si_mode = args.mda_mode; 819 820 devfs_create(dev); 821 clean_unrhdrl(devfs_inos); 822 dev_unlock_and_free(); 823 824 notify_create(dev, args.mda_flags); 825 826 *dres = dev; 827 return (0); 828 } 829 830 int 831 make_dev_s(struct make_dev_args *args, struct cdev **dres, 832 const char *fmt, ...) 833 { 834 va_list ap; 835 int res; 836 837 va_start(ap, fmt); 838 res = make_dev_sv(args, dres, fmt, ap); 839 va_end(ap); 840 return (res); 841 } 842 843 static int 844 make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw, int unit, 845 struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, 846 va_list ap) 847 { 848 struct make_dev_args args; 849 850 make_dev_args_init(&args); 851 args.mda_flags = flags; 852 args.mda_devsw = devsw; 853 args.mda_cr = cr; 854 args.mda_uid = uid; 855 args.mda_gid = gid; 856 args.mda_mode = mode; 857 args.mda_unit = unit; 858 return (make_dev_sv(&args, dres, fmt, ap)); 859 } 860 861 struct cdev * 862 make_dev(struct cdevsw *devsw, int unit, uid_t uid, gid_t gid, int mode, 863 const char *fmt, ...) 864 { 865 struct cdev *dev; 866 va_list ap; 867 int res; 868 869 va_start(ap, fmt); 870 res = make_dev_credv(0, &dev, devsw, unit, NULL, uid, gid, mode, fmt, 871 ap); 872 va_end(ap); 873 KASSERT(res == 0 && dev != NULL, 874 ("make_dev: failed make_dev_credv (error=%d)", res)); 875 return (dev); 876 } 877 878 struct cdev * 879 make_dev_cred(struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid, 880 gid_t gid, int mode, const char *fmt, ...) 881 { 882 struct cdev *dev; 883 va_list ap; 884 int res; 885 886 va_start(ap, fmt); 887 res = make_dev_credv(0, &dev, devsw, unit, cr, uid, gid, mode, fmt, ap); 888 va_end(ap); 889 890 KASSERT(res == 0 && dev != NULL, 891 ("make_dev_cred: failed make_dev_credv (error=%d)", res)); 892 return (dev); 893 } 894 895 struct cdev * 896 make_dev_credf(int flags, struct cdevsw *devsw, int unit, struct ucred *cr, 897 uid_t uid, gid_t gid, int mode, const char *fmt, ...) 898 { 899 struct cdev *dev; 900 va_list ap; 901 int res; 902 903 va_start(ap, fmt); 904 res = make_dev_credv(flags, &dev, devsw, unit, cr, uid, gid, mode, 905 fmt, ap); 906 va_end(ap); 907 908 KASSERT(((flags & MAKEDEV_NOWAIT) != 0 && res == ENOMEM) || 909 ((flags & MAKEDEV_CHECKNAME) != 0 && res != ENOMEM) || res == 0, 910 ("make_dev_credf: failed make_dev_credv (error=%d)", res)); 911 return (res == 0 ? dev : NULL); 912 } 913 914 int 915 make_dev_p(int flags, struct cdev **cdev, struct cdevsw *devsw, 916 struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, ...) 917 { 918 va_list ap; 919 int res; 920 921 va_start(ap, fmt); 922 res = make_dev_credv(flags, cdev, devsw, 0, cr, uid, gid, mode, 923 fmt, ap); 924 va_end(ap); 925 926 KASSERT(((flags & MAKEDEV_NOWAIT) != 0 && res == ENOMEM) || 927 ((flags & MAKEDEV_CHECKNAME) != 0 && res != ENOMEM) || res == 0, 928 ("make_dev_p: failed make_dev_credv (error=%d)", res)); 929 return (res); 930 } 931 932 static void 933 dev_dependsl(struct cdev *pdev, struct cdev *cdev) 934 { 935 936 cdev->si_parent = pdev; 937 cdev->si_flags |= SI_CHILD; 938 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings); 939 } 940 941 942 void 943 dev_depends(struct cdev *pdev, struct cdev *cdev) 944 { 945 946 dev_lock(); 947 dev_dependsl(pdev, cdev); 948 dev_unlock(); 949 } 950 951 static int 952 make_dev_alias_v(int flags, struct cdev **cdev, struct cdev *pdev, 953 const char *fmt, va_list ap) 954 { 955 struct cdev *dev; 956 int error; 957 958 KASSERT(pdev != NULL, ("make_dev_alias_v: pdev is NULL")); 959 KASSERT((flags & MAKEDEV_WAITOK) == 0 || (flags & MAKEDEV_NOWAIT) == 0, 960 ("make_dev_alias_v: both WAITOK and NOWAIT specified")); 961 KASSERT((flags & ~(MAKEDEV_WAITOK | MAKEDEV_NOWAIT | 962 MAKEDEV_CHECKNAME)) == 0, 963 ("make_dev_alias_v: invalid flags specified (flags=%02x)", flags)); 964 965 dev = devfs_alloc(flags); 966 if (dev == NULL) 967 return (ENOMEM); 968 dev_lock(); 969 dev->si_flags |= SI_ALIAS; 970 error = prep_devname(dev, fmt, ap); 971 if (error != 0) { 972 if ((flags & MAKEDEV_CHECKNAME) == 0) { 973 panic("make_dev_alias_v: bad si_name " 974 "(error=%d, si_name=%s)", error, dev->si_name); 975 } 976 dev_unlock(); 977 devfs_free(dev); 978 return (error); 979 } 980 dev->si_flags |= SI_NAMED; 981 devfs_create(dev); 982 dev_dependsl(pdev, dev); 983 clean_unrhdrl(devfs_inos); 984 dev_unlock(); 985 986 notify_create(dev, flags); 987 *cdev = dev; 988 989 return (0); 990 } 991 992 struct cdev * 993 make_dev_alias(struct cdev *pdev, const char *fmt, ...) 994 { 995 struct cdev *dev; 996 va_list ap; 997 int res; 998 999 va_start(ap, fmt); 1000 res = make_dev_alias_v(MAKEDEV_WAITOK, &dev, pdev, fmt, ap); 1001 va_end(ap); 1002 1003 KASSERT(res == 0 && dev != NULL, 1004 ("make_dev_alias: failed make_dev_alias_v (error=%d)", res)); 1005 return (dev); 1006 } 1007 1008 int 1009 make_dev_alias_p(int flags, struct cdev **cdev, struct cdev *pdev, 1010 const char *fmt, ...) 1011 { 1012 va_list ap; 1013 int res; 1014 1015 va_start(ap, fmt); 1016 res = make_dev_alias_v(flags, cdev, pdev, fmt, ap); 1017 va_end(ap); 1018 return (res); 1019 } 1020 1021 int 1022 make_dev_physpath_alias(int flags, struct cdev **cdev, struct cdev *pdev, 1023 struct cdev *old_alias, const char *physpath) 1024 { 1025 char *devfspath; 1026 int physpath_len; 1027 int max_parentpath_len; 1028 int parentpath_len; 1029 int devfspathbuf_len; 1030 int mflags; 1031 int ret; 1032 1033 *cdev = NULL; 1034 devfspath = NULL; 1035 physpath_len = strlen(physpath); 1036 ret = EINVAL; 1037 if (physpath_len == 0) 1038 goto out; 1039 1040 if (strncmp("id1,", physpath, 4) == 0) { 1041 physpath += 4; 1042 physpath_len -= 4; 1043 if (physpath_len == 0) 1044 goto out; 1045 } 1046 1047 max_parentpath_len = SPECNAMELEN - physpath_len - /*/*/1; 1048 parentpath_len = strlen(pdev->si_name); 1049 if (max_parentpath_len < parentpath_len) { 1050 if (bootverbose) 1051 printf("WARNING: Unable to alias %s " 1052 "to %s/%s - path too long\n", 1053 pdev->si_name, physpath, pdev->si_name); 1054 ret = ENAMETOOLONG; 1055 goto out; 1056 } 1057 1058 mflags = (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK; 1059 devfspathbuf_len = physpath_len + /*/*/1 + parentpath_len + /*NUL*/1; 1060 devfspath = malloc(devfspathbuf_len, M_DEVBUF, mflags); 1061 if (devfspath == NULL) { 1062 ret = ENOMEM; 1063 goto out; 1064 } 1065 1066 sprintf(devfspath, "%s/%s", physpath, pdev->si_name); 1067 if (old_alias != NULL && strcmp(old_alias->si_name, devfspath) == 0) { 1068 /* Retain the existing alias. */ 1069 *cdev = old_alias; 1070 old_alias = NULL; 1071 ret = 0; 1072 } else { 1073 ret = make_dev_alias_p(flags, cdev, pdev, "%s", devfspath); 1074 } 1075 out: 1076 if (old_alias != NULL) 1077 destroy_dev(old_alias); 1078 if (devfspath != NULL) 1079 free(devfspath, M_DEVBUF); 1080 return (ret); 1081 } 1082 1083 static void 1084 destroy_devl(struct cdev *dev) 1085 { 1086 struct cdevsw *csw; 1087 struct cdev_privdata *p; 1088 struct cdev_priv *cdp; 1089 1090 mtx_assert(&devmtx, MA_OWNED); 1091 KASSERT(dev->si_flags & SI_NAMED, 1092 ("WARNING: Driver mistake: destroy_dev on %d\n", dev2unit(dev))); 1093 KASSERT((dev->si_flags & SI_ETERNAL) == 0, 1094 ("WARNING: Driver mistake: destroy_dev on eternal %d\n", 1095 dev2unit(dev))); 1096 1097 cdp = cdev2priv(dev); 1098 if ((cdp->cdp_flags & CDP_UNREF_DTR) == 0) { 1099 /* 1100 * Avoid race with dev_rel(), e.g. from the populate 1101 * loop. If CDP_UNREF_DTR flag is set, the reference 1102 * to be dropped at the end of destroy_devl() was 1103 * already taken by delist_dev_locked(). 1104 */ 1105 dev_refl(dev); 1106 1107 devfs_destroy(dev); 1108 } 1109 1110 /* Remove name marking */ 1111 dev->si_flags &= ~SI_NAMED; 1112 1113 /* If we are a child, remove us from the parents list */ 1114 if (dev->si_flags & SI_CHILD) { 1115 LIST_REMOVE(dev, si_siblings); 1116 dev->si_flags &= ~SI_CHILD; 1117 } 1118 1119 /* Kill our children */ 1120 while (!LIST_EMPTY(&dev->si_children)) 1121 destroy_devl(LIST_FIRST(&dev->si_children)); 1122 1123 /* Remove from clone list */ 1124 if (dev->si_flags & SI_CLONELIST) { 1125 LIST_REMOVE(dev, si_clone); 1126 dev->si_flags &= ~SI_CLONELIST; 1127 } 1128 1129 csw = dev->si_devsw; 1130 dev->si_devsw = NULL; /* already NULL for SI_ALIAS */ 1131 while (csw != NULL && csw->d_purge != NULL && dev->si_threadcount) { 1132 csw->d_purge(dev); 1133 msleep(csw, &devmtx, PRIBIO, "devprg", hz/10); 1134 if (dev->si_threadcount) 1135 printf("Still %lu threads in %s\n", 1136 dev->si_threadcount, devtoname(dev)); 1137 } 1138 while (dev->si_threadcount != 0) { 1139 /* Use unique dummy wait ident */ 1140 msleep(&csw, &devmtx, PRIBIO, "devdrn", hz / 10); 1141 } 1142 1143 dev_unlock(); 1144 if ((cdp->cdp_flags & CDP_UNREF_DTR) == 0) { 1145 /* avoid out of order notify events */ 1146 notify_destroy(dev); 1147 } 1148 mtx_lock(&cdevpriv_mtx); 1149 while ((p = LIST_FIRST(&cdp->cdp_fdpriv)) != NULL) { 1150 devfs_destroy_cdevpriv(p); 1151 mtx_lock(&cdevpriv_mtx); 1152 } 1153 mtx_unlock(&cdevpriv_mtx); 1154 dev_lock(); 1155 1156 dev->si_drv1 = 0; 1157 dev->si_drv2 = 0; 1158 bzero(&dev->__si_u, sizeof(dev->__si_u)); 1159 1160 if (!(dev->si_flags & SI_ALIAS)) { 1161 /* Remove from cdevsw list */ 1162 LIST_REMOVE(dev, si_list); 1163 1164 /* If cdevsw has no more struct cdev *'s, clean it */ 1165 if (LIST_EMPTY(&csw->d_devs)) { 1166 fini_cdevsw(csw); 1167 wakeup(&csw->d_devs); 1168 } 1169 } 1170 dev->si_flags &= ~SI_ALIAS; 1171 cdp->cdp_flags &= ~CDP_UNREF_DTR; 1172 dev->si_refcount--; 1173 1174 if (dev->si_refcount > 0) 1175 LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list); 1176 else 1177 dev_free_devlocked(dev); 1178 } 1179 1180 static void 1181 delist_dev_locked(struct cdev *dev) 1182 { 1183 struct cdev_priv *cdp; 1184 struct cdev *child; 1185 1186 mtx_assert(&devmtx, MA_OWNED); 1187 cdp = cdev2priv(dev); 1188 if ((cdp->cdp_flags & CDP_UNREF_DTR) != 0) 1189 return; 1190 cdp->cdp_flags |= CDP_UNREF_DTR; 1191 dev_refl(dev); 1192 devfs_destroy(dev); 1193 LIST_FOREACH(child, &dev->si_children, si_siblings) 1194 delist_dev_locked(child); 1195 dev_unlock(); 1196 /* ensure the destroy event is queued in order */ 1197 notify_destroy(dev); 1198 dev_lock(); 1199 } 1200 1201 /* 1202 * This function will delist a character device and its children from 1203 * the directory listing and create a destroy event without waiting 1204 * for all character device references to go away. At some later point 1205 * destroy_dev() must be called to complete the character device 1206 * destruction. After calling this function the character device name 1207 * can instantly be re-used. 1208 */ 1209 void 1210 delist_dev(struct cdev *dev) 1211 { 1212 1213 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "delist_dev"); 1214 dev_lock(); 1215 delist_dev_locked(dev); 1216 dev_unlock(); 1217 } 1218 1219 void 1220 destroy_dev(struct cdev *dev) 1221 { 1222 1223 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "destroy_dev"); 1224 dev_lock(); 1225 destroy_devl(dev); 1226 dev_unlock_and_free(); 1227 } 1228 1229 const char * 1230 devtoname(struct cdev *dev) 1231 { 1232 1233 return (dev->si_name); 1234 } 1235 1236 int 1237 dev_stdclone(char *name, char **namep, const char *stem, int *unit) 1238 { 1239 int u, i; 1240 1241 i = strlen(stem); 1242 if (bcmp(stem, name, i) != 0) 1243 return (0); 1244 if (!isdigit(name[i])) 1245 return (0); 1246 u = 0; 1247 if (name[i] == '0' && isdigit(name[i+1])) 1248 return (0); 1249 while (isdigit(name[i])) { 1250 u *= 10; 1251 u += name[i++] - '0'; 1252 } 1253 if (u > 0xffffff) 1254 return (0); 1255 *unit = u; 1256 if (namep) 1257 *namep = &name[i]; 1258 if (name[i]) 1259 return (2); 1260 return (1); 1261 } 1262 1263 /* 1264 * Helper functions for cloning device drivers. 1265 * 1266 * The objective here is to make it unnecessary for the device drivers to 1267 * use rman or similar to manage their unit number space. Due to the way 1268 * we do "on-demand" devices, using rman or other "private" methods 1269 * will be very tricky to lock down properly once we lock down this file. 1270 * 1271 * Instead we give the drivers these routines which puts the struct cdev *'s 1272 * that are to be managed on their own list, and gives the driver the ability 1273 * to ask for the first free unit number or a given specified unit number. 1274 * 1275 * In addition these routines support paired devices (pty, nmdm and similar) 1276 * by respecting a number of "flag" bits in the minor number. 1277 * 1278 */ 1279 1280 struct clonedevs { 1281 LIST_HEAD(,cdev) head; 1282 }; 1283 1284 void 1285 clone_setup(struct clonedevs **cdp) 1286 { 1287 1288 *cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO); 1289 LIST_INIT(&(*cdp)->head); 1290 } 1291 1292 int 1293 clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up, 1294 struct cdev **dp, int extra) 1295 { 1296 struct clonedevs *cd; 1297 struct cdev *dev, *ndev, *dl, *de; 1298 struct make_dev_args args; 1299 int unit, low, u; 1300 1301 KASSERT(*cdp != NULL, 1302 ("clone_setup() not called in driver \"%s\"", csw->d_name)); 1303 KASSERT(!(extra & CLONE_UNITMASK), 1304 ("Illegal extra bits (0x%x) in clone_create", extra)); 1305 KASSERT(*up <= CLONE_UNITMASK, 1306 ("Too high unit (0x%x) in clone_create", *up)); 1307 KASSERT(csw->d_flags & D_NEEDMINOR, 1308 ("clone_create() on cdevsw without minor numbers")); 1309 1310 1311 /* 1312 * Search the list for a lot of things in one go: 1313 * A preexisting match is returned immediately. 1314 * The lowest free unit number if we are passed -1, and the place 1315 * in the list where we should insert that new element. 1316 * The place to insert a specified unit number, if applicable 1317 * the end of the list. 1318 */ 1319 unit = *up; 1320 ndev = devfs_alloc(MAKEDEV_WAITOK); 1321 dev_lock(); 1322 prep_cdevsw(csw, MAKEDEV_WAITOK); 1323 low = extra; 1324 de = dl = NULL; 1325 cd = *cdp; 1326 LIST_FOREACH(dev, &cd->head, si_clone) { 1327 KASSERT(dev->si_flags & SI_CLONELIST, 1328 ("Dev %p(%s) should be on clonelist", dev, dev->si_name)); 1329 u = dev2unit(dev); 1330 if (u == (unit | extra)) { 1331 *dp = dev; 1332 dev_unlock(); 1333 devfs_free(ndev); 1334 return (0); 1335 } 1336 if (unit == -1 && u == low) { 1337 low++; 1338 de = dev; 1339 continue; 1340 } else if (u < (unit | extra)) { 1341 de = dev; 1342 continue; 1343 } else if (u > (unit | extra)) { 1344 dl = dev; 1345 break; 1346 } 1347 } 1348 if (unit == -1) 1349 unit = low & CLONE_UNITMASK; 1350 make_dev_args_init(&args); 1351 args.mda_unit = unit | extra; 1352 args.mda_devsw = csw; 1353 dev = newdev(&args, ndev); 1354 if (dev->si_flags & SI_CLONELIST) { 1355 printf("dev %p (%s) is on clonelist\n", dev, dev->si_name); 1356 printf("unit=%d, low=%d, extra=0x%x\n", unit, low, extra); 1357 LIST_FOREACH(dev, &cd->head, si_clone) { 1358 printf("\t%p %s\n", dev, dev->si_name); 1359 } 1360 panic("foo"); 1361 } 1362 KASSERT(!(dev->si_flags & SI_CLONELIST), 1363 ("Dev %p(%s) should not be on clonelist", dev, dev->si_name)); 1364 if (dl != NULL) 1365 LIST_INSERT_BEFORE(dl, dev, si_clone); 1366 else if (de != NULL) 1367 LIST_INSERT_AFTER(de, dev, si_clone); 1368 else 1369 LIST_INSERT_HEAD(&cd->head, dev, si_clone); 1370 dev->si_flags |= SI_CLONELIST; 1371 *up = unit; 1372 dev_unlock_and_free(); 1373 return (1); 1374 } 1375 1376 /* 1377 * Kill everything still on the list. The driver should already have 1378 * disposed of any softc hung of the struct cdev *'s at this time. 1379 */ 1380 void 1381 clone_cleanup(struct clonedevs **cdp) 1382 { 1383 struct cdev *dev; 1384 struct cdev_priv *cp; 1385 struct clonedevs *cd; 1386 1387 cd = *cdp; 1388 if (cd == NULL) 1389 return; 1390 dev_lock(); 1391 while (!LIST_EMPTY(&cd->head)) { 1392 dev = LIST_FIRST(&cd->head); 1393 LIST_REMOVE(dev, si_clone); 1394 KASSERT(dev->si_flags & SI_CLONELIST, 1395 ("Dev %p(%s) should be on clonelist", dev, dev->si_name)); 1396 dev->si_flags &= ~SI_CLONELIST; 1397 cp = cdev2priv(dev); 1398 if (!(cp->cdp_flags & CDP_SCHED_DTR)) { 1399 cp->cdp_flags |= CDP_SCHED_DTR; 1400 KASSERT(dev->si_flags & SI_NAMED, 1401 ("Driver has goofed in cloning underways udev %jx unit %x", 1402 (uintmax_t)dev2udev(dev), dev2unit(dev))); 1403 destroy_devl(dev); 1404 } 1405 } 1406 dev_unlock_and_free(); 1407 free(cd, M_DEVBUF); 1408 *cdp = NULL; 1409 } 1410 1411 static TAILQ_HEAD(, cdev_priv) dev_ddtr = 1412 TAILQ_HEAD_INITIALIZER(dev_ddtr); 1413 static struct task dev_dtr_task = TASK_INITIALIZER(0, destroy_dev_tq, NULL); 1414 1415 static void 1416 destroy_dev_tq(void *ctx, int pending) 1417 { 1418 struct cdev_priv *cp; 1419 struct cdev *dev; 1420 void (*cb)(void *); 1421 void *cb_arg; 1422 1423 dev_lock(); 1424 while (!TAILQ_EMPTY(&dev_ddtr)) { 1425 cp = TAILQ_FIRST(&dev_ddtr); 1426 dev = &cp->cdp_c; 1427 KASSERT(cp->cdp_flags & CDP_SCHED_DTR, 1428 ("cdev %p in dev_destroy_tq without CDP_SCHED_DTR", cp)); 1429 TAILQ_REMOVE(&dev_ddtr, cp, cdp_dtr_list); 1430 cb = cp->cdp_dtr_cb; 1431 cb_arg = cp->cdp_dtr_cb_arg; 1432 destroy_devl(dev); 1433 dev_unlock_and_free(); 1434 dev_rel(dev); 1435 if (cb != NULL) 1436 cb(cb_arg); 1437 dev_lock(); 1438 } 1439 dev_unlock(); 1440 } 1441 1442 /* 1443 * devmtx shall be locked on entry. devmtx will be unlocked after 1444 * function return. 1445 */ 1446 static int 1447 destroy_dev_sched_cbl(struct cdev *dev, void (*cb)(void *), void *arg) 1448 { 1449 struct cdev_priv *cp; 1450 1451 mtx_assert(&devmtx, MA_OWNED); 1452 cp = cdev2priv(dev); 1453 if (cp->cdp_flags & CDP_SCHED_DTR) { 1454 dev_unlock(); 1455 return (0); 1456 } 1457 dev_refl(dev); 1458 cp->cdp_flags |= CDP_SCHED_DTR; 1459 cp->cdp_dtr_cb = cb; 1460 cp->cdp_dtr_cb_arg = arg; 1461 TAILQ_INSERT_TAIL(&dev_ddtr, cp, cdp_dtr_list); 1462 dev_unlock(); 1463 taskqueue_enqueue(taskqueue_swi_giant, &dev_dtr_task); 1464 return (1); 1465 } 1466 1467 int 1468 destroy_dev_sched_cb(struct cdev *dev, void (*cb)(void *), void *arg) 1469 { 1470 1471 dev_lock(); 1472 return (destroy_dev_sched_cbl(dev, cb, arg)); 1473 } 1474 1475 int 1476 destroy_dev_sched(struct cdev *dev) 1477 { 1478 1479 return (destroy_dev_sched_cb(dev, NULL, NULL)); 1480 } 1481 1482 void 1483 destroy_dev_drain(struct cdevsw *csw) 1484 { 1485 1486 dev_lock(); 1487 while (!LIST_EMPTY(&csw->d_devs)) { 1488 msleep(&csw->d_devs, &devmtx, PRIBIO, "devscd", hz/10); 1489 } 1490 dev_unlock(); 1491 } 1492 1493 void 1494 drain_dev_clone_events(void) 1495 { 1496 1497 sx_xlock(&clone_drain_lock); 1498 sx_xunlock(&clone_drain_lock); 1499 } 1500 1501 #include "opt_ddb.h" 1502 #ifdef DDB 1503 #include <sys/kernel.h> 1504 1505 #include <ddb/ddb.h> 1506 1507 DB_SHOW_COMMAND(cdev, db_show_cdev) 1508 { 1509 struct cdev_priv *cdp; 1510 struct cdev *dev; 1511 u_int flags; 1512 char buf[512]; 1513 1514 if (!have_addr) { 1515 TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) { 1516 dev = &cdp->cdp_c; 1517 db_printf("%s %p\n", dev->si_name, dev); 1518 if (db_pager_quit) 1519 break; 1520 } 1521 return; 1522 } 1523 1524 dev = (struct cdev *)addr; 1525 cdp = cdev2priv(dev); 1526 db_printf("dev %s ref %d use %ld thr %ld inuse %u fdpriv %p\n", 1527 dev->si_name, dev->si_refcount, dev->si_usecount, 1528 dev->si_threadcount, cdp->cdp_inuse, cdp->cdp_fdpriv.lh_first); 1529 db_printf("devsw %p si_drv0 %d si_drv1 %p si_drv2 %p\n", 1530 dev->si_devsw, dev->si_drv0, dev->si_drv1, dev->si_drv2); 1531 flags = dev->si_flags; 1532 #define SI_FLAG(flag) do { \ 1533 if (flags & (flag)) { \ 1534 if (buf[0] != '\0') \ 1535 strlcat(buf, ", ", sizeof(buf)); \ 1536 strlcat(buf, (#flag) + 3, sizeof(buf)); \ 1537 flags &= ~(flag); \ 1538 } \ 1539 } while (0) 1540 buf[0] = '\0'; 1541 SI_FLAG(SI_ETERNAL); 1542 SI_FLAG(SI_ALIAS); 1543 SI_FLAG(SI_NAMED); 1544 SI_FLAG(SI_CHEAPCLONE); 1545 SI_FLAG(SI_CHILD); 1546 SI_FLAG(SI_DUMPDEV); 1547 SI_FLAG(SI_CLONELIST); 1548 db_printf("si_flags %s\n", buf); 1549 1550 flags = cdp->cdp_flags; 1551 #define CDP_FLAG(flag) do { \ 1552 if (flags & (flag)) { \ 1553 if (buf[0] != '\0') \ 1554 strlcat(buf, ", ", sizeof(buf)); \ 1555 strlcat(buf, (#flag) + 4, sizeof(buf)); \ 1556 flags &= ~(flag); \ 1557 } \ 1558 } while (0) 1559 buf[0] = '\0'; 1560 CDP_FLAG(CDP_ACTIVE); 1561 CDP_FLAG(CDP_SCHED_DTR); 1562 db_printf("cdp_flags %s\n", buf); 1563 } 1564 #endif 1565