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