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 void destroy_dev_tq_giant(void *ctx, int pending); 62 static int make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw, 63 int unit, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, 64 va_list ap); 65 66 static struct cdev_priv_list cdevp_free_list = 67 TAILQ_HEAD_INITIALIZER(cdevp_free_list); 68 static SLIST_HEAD(free_cdevsw, cdevsw) cdevsw_gt_post_list = 69 SLIST_HEAD_INITIALIZER(cdevsw_gt_post_list); 70 71 void 72 dev_lock(void) 73 { 74 75 mtx_lock(&devmtx); 76 } 77 78 /* 79 * Free all the memory collected while the cdev mutex was 80 * locked. Since devmtx is after the system map mutex, free() cannot 81 * be called immediately and is postponed until cdev mutex can be 82 * dropped. 83 */ 84 static void 85 dev_unlock_and_free(void) 86 { 87 struct cdev_priv_list cdp_free; 88 struct free_cdevsw csw_free; 89 struct cdev_priv *cdp; 90 struct cdevsw *csw; 91 92 dev_lock_assert_locked(); 93 94 /* 95 * Make the local copy of the list heads while the dev_mtx is 96 * held. Free it later. 97 */ 98 TAILQ_INIT(&cdp_free); 99 TAILQ_CONCAT(&cdp_free, &cdevp_free_list, cdp_list); 100 csw_free = cdevsw_gt_post_list; 101 SLIST_INIT(&cdevsw_gt_post_list); 102 103 mtx_unlock(&devmtx); 104 105 while ((cdp = TAILQ_FIRST(&cdp_free)) != NULL) { 106 TAILQ_REMOVE(&cdp_free, cdp, cdp_list); 107 devfs_free(&cdp->cdp_c); 108 } 109 while ((csw = SLIST_FIRST(&csw_free)) != NULL) { 110 SLIST_REMOVE_HEAD(&csw_free, d_postfree_list); 111 free(csw, M_DEVT); 112 } 113 } 114 115 static void 116 dev_free_devlocked(struct cdev *cdev) 117 { 118 struct cdev_priv *cdp; 119 120 dev_lock_assert_locked(); 121 cdp = cdev2priv(cdev); 122 KASSERT((cdp->cdp_flags & CDP_UNREF_DTR) == 0, 123 ("destroy_dev() was not called after delist_dev(%p)", cdev)); 124 TAILQ_INSERT_HEAD(&cdevp_free_list, cdp, cdp_list); 125 } 126 127 static void 128 cdevsw_free_devlocked(struct cdevsw *csw) 129 { 130 131 dev_lock_assert_locked(); 132 SLIST_INSERT_HEAD(&cdevsw_gt_post_list, csw, d_postfree_list); 133 } 134 135 void 136 dev_unlock(void) 137 { 138 139 mtx_unlock(&devmtx); 140 } 141 142 void 143 dev_ref(struct cdev *dev) 144 { 145 146 dev_lock_assert_unlocked(); 147 mtx_lock(&devmtx); 148 dev->si_refcount++; 149 mtx_unlock(&devmtx); 150 } 151 152 void 153 dev_refl(struct cdev *dev) 154 { 155 156 dev_lock_assert_locked(); 157 dev->si_refcount++; 158 } 159 160 void 161 dev_rel(struct cdev *dev) 162 { 163 int flag = 0; 164 165 dev_lock_assert_unlocked(); 166 dev_lock(); 167 dev->si_refcount--; 168 KASSERT(dev->si_refcount >= 0, 169 ("dev_rel(%s) gave negative count", devtoname(dev))); 170 if (dev->si_devsw == NULL && dev->si_refcount == 0) { 171 LIST_REMOVE(dev, si_list); 172 flag = 1; 173 } 174 dev_unlock(); 175 if (flag) 176 devfs_free(dev); 177 } 178 179 struct cdevsw * 180 dev_refthread(struct cdev *dev, int *ref) 181 { 182 struct cdevsw *csw; 183 struct cdev_priv *cdp; 184 185 dev_lock_assert_unlocked(); 186 if ((dev->si_flags & SI_ETERNAL) != 0) { 187 *ref = 0; 188 return (dev->si_devsw); 189 } 190 cdp = cdev2priv(dev); 191 mtx_lock(&cdp->cdp_threadlock); 192 csw = dev->si_devsw; 193 if (csw != NULL) { 194 if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0) 195 atomic_add_long(&dev->si_threadcount, 1); 196 else 197 csw = NULL; 198 } 199 mtx_unlock(&cdp->cdp_threadlock); 200 if (csw != NULL) 201 *ref = 1; 202 return (csw); 203 } 204 205 struct cdevsw * 206 devvn_refthread(struct vnode *vp, struct cdev **devp, int *ref) 207 { 208 struct cdevsw *csw; 209 struct cdev_priv *cdp; 210 struct cdev *dev; 211 212 dev_lock_assert_unlocked(); 213 if ((vp->v_vflag & VV_ETERNALDEV) != 0) { 214 dev = vp->v_rdev; 215 if (dev == NULL) 216 return (NULL); 217 KASSERT((dev->si_flags & SI_ETERNAL) != 0, 218 ("Not eternal cdev")); 219 *ref = 0; 220 csw = dev->si_devsw; 221 KASSERT(csw != NULL, ("Eternal cdev is destroyed")); 222 *devp = dev; 223 return (csw); 224 } 225 226 csw = NULL; 227 VI_LOCK(vp); 228 dev = vp->v_rdev; 229 if (dev == NULL) { 230 VI_UNLOCK(vp); 231 return (NULL); 232 } 233 cdp = cdev2priv(dev); 234 mtx_lock(&cdp->cdp_threadlock); 235 if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0) { 236 csw = dev->si_devsw; 237 if (csw != NULL) 238 atomic_add_long(&dev->si_threadcount, 1); 239 } 240 mtx_unlock(&cdp->cdp_threadlock); 241 VI_UNLOCK(vp); 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 dev_lock_assert_unlocked(); 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("DEVFS", "CDEV", ev, data); 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 dev_lock_assert_locked(); 575 csw = args->mda_devsw; 576 si2 = NULL; 577 if (csw->d_flags & D_NEEDMINOR) { 578 /* We may want to return an existing device */ 579 LIST_FOREACH(si2, &csw->d_devs, si_list) { 580 if (dev2unit(si2) == args->mda_unit) { 581 dev_free_devlocked(si); 582 si = si2; 583 break; 584 } 585 } 586 587 /* 588 * If we're returning an existing device, we should make sure 589 * it isn't already initialized. This would have been caught 590 * in consumers anyways, but it's good to catch such a case 591 * early. We still need to complete initialization of the 592 * device, and we'll use whatever make_dev_args were passed in 593 * to do so. 594 */ 595 KASSERT(si2 == NULL || (si2->si_flags & SI_NAMED) == 0, 596 ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)", 597 args->mda_devsw->d_name, dev2unit(si2), devtoname(si2))); 598 } 599 si->si_drv0 = args->mda_unit; 600 si->si_drv1 = args->mda_si_drv1; 601 si->si_drv2 = args->mda_si_drv2; 602 /* Only push to csw->d_devs if it's not a cloned device. */ 603 if (si2 == NULL) { 604 si->si_devsw = csw; 605 LIST_INSERT_HEAD(&csw->d_devs, si, si_list); 606 } else { 607 KASSERT(si->si_devsw == csw, 608 ("%s: inconsistent devsw between clone_create() and make_dev()", 609 __func__)); 610 } 611 return (si); 612 } 613 614 static void 615 fini_cdevsw(struct cdevsw *devsw) 616 { 617 struct cdevsw *gt; 618 619 if (devsw->d_gianttrick != NULL) { 620 gt = devsw->d_gianttrick; 621 memcpy(devsw, gt, sizeof *devsw); 622 cdevsw_free_devlocked(gt); 623 devsw->d_gianttrick = NULL; 624 } 625 devsw->d_flags &= ~D_INIT; 626 } 627 628 static int 629 prep_cdevsw(struct cdevsw *devsw, int flags) 630 { 631 struct cdevsw *dsw2; 632 633 dev_lock_assert_locked(); 634 if (devsw->d_flags & D_INIT) 635 return (0); 636 if (devsw->d_flags & D_NEEDGIANT) { 637 dev_unlock(); 638 dsw2 = malloc(sizeof *dsw2, M_DEVT, 639 (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK); 640 dev_lock(); 641 if (dsw2 == NULL && !(devsw->d_flags & D_INIT)) 642 return (ENOMEM); 643 } else 644 dsw2 = NULL; 645 if (devsw->d_flags & D_INIT) { 646 if (dsw2 != NULL) 647 cdevsw_free_devlocked(dsw2); 648 return (0); 649 } 650 651 if (devsw->d_version != D_VERSION_04) { 652 printf( 653 "WARNING: Device driver \"%s\" has wrong version %s\n", 654 devsw->d_name == NULL ? "???" : devsw->d_name, 655 "and is disabled. Recompile KLD module."); 656 devsw->d_open = dead_open; 657 devsw->d_close = dead_close; 658 devsw->d_read = dead_read; 659 devsw->d_write = dead_write; 660 devsw->d_ioctl = dead_ioctl; 661 devsw->d_poll = dead_poll; 662 devsw->d_mmap = dead_mmap; 663 devsw->d_mmap_single = dead_mmap_single; 664 devsw->d_strategy = dead_strategy; 665 devsw->d_dump = dead_dump; 666 devsw->d_kqfilter = dead_kqfilter; 667 } 668 669 if ((devsw->d_flags & D_NEEDGIANT) != 0) { 670 if ((devsw->d_flags & D_GIANTOK) == 0) { 671 printf( 672 "WARNING: Device \"%s\" is Giant locked and may be " 673 "deleted before FreeBSD 14.0.\n", 674 devsw->d_name == NULL ? "???" : devsw->d_name); 675 } 676 if (devsw->d_gianttrick == NULL) { 677 memcpy(dsw2, devsw, sizeof *dsw2); 678 devsw->d_gianttrick = dsw2; 679 dsw2 = NULL; 680 } 681 } 682 683 #define FIXUP(member, noop, giant) \ 684 do { \ 685 if (devsw->member == NULL) { \ 686 devsw->member = noop; \ 687 } else if (devsw->d_flags & D_NEEDGIANT) \ 688 devsw->member = giant; \ 689 } \ 690 while (0) 691 692 FIXUP(d_open, null_open, giant_open); 693 FIXUP(d_fdopen, NULL, giant_fdopen); 694 FIXUP(d_close, null_close, giant_close); 695 FIXUP(d_read, no_read, giant_read); 696 FIXUP(d_write, no_write, giant_write); 697 FIXUP(d_ioctl, no_ioctl, giant_ioctl); 698 FIXUP(d_poll, no_poll, giant_poll); 699 FIXUP(d_mmap, no_mmap, giant_mmap); 700 FIXUP(d_strategy, no_strategy, giant_strategy); 701 FIXUP(d_kqfilter, no_kqfilter, giant_kqfilter); 702 FIXUP(d_mmap_single, no_mmap_single, giant_mmap_single); 703 704 if (devsw->d_dump == NULL) devsw->d_dump = no_dump; 705 706 LIST_INIT(&devsw->d_devs); 707 708 devsw->d_flags |= D_INIT; 709 710 if (dsw2 != NULL) 711 cdevsw_free_devlocked(dsw2); 712 return (0); 713 } 714 715 static int 716 prep_devname(struct cdev *dev, const char *fmt, va_list ap) 717 { 718 int len; 719 char *from, *q, *s, *to; 720 721 dev_lock_assert_locked(); 722 723 len = vsnrprintf(dev->si_name, sizeof(dev->si_name), 32, fmt, ap); 724 if (len > sizeof(dev->si_name) - 1) 725 return (ENAMETOOLONG); 726 727 /* Strip leading slashes. */ 728 for (from = dev->si_name; *from == '/'; from++) 729 ; 730 731 for (to = dev->si_name; *from != '\0'; from++, to++) { 732 /* 733 * Spaces and double quotation marks cause 734 * problems for the devctl(4) protocol. 735 * Reject names containing those characters. 736 */ 737 if (isspace(*from) || *from == '"') 738 return (EINVAL); 739 /* Treat multiple sequential slashes as single. */ 740 while (from[0] == '/' && from[1] == '/') 741 from++; 742 /* Trailing slash is considered invalid. */ 743 if (from[0] == '/' && from[1] == '\0') 744 return (EINVAL); 745 *to = *from; 746 } 747 *to = '\0'; 748 749 if (dev->si_name[0] == '\0') 750 return (EINVAL); 751 752 /* Disallow "." and ".." components. */ 753 for (s = dev->si_name;;) { 754 for (q = s; *q != '/' && *q != '\0'; q++) 755 ; 756 if (q - s == 1 && s[0] == '.') 757 return (EINVAL); 758 if (q - s == 2 && s[0] == '.' && s[1] == '.') 759 return (EINVAL); 760 if (*q != '/') 761 break; 762 s = q + 1; 763 } 764 765 if (devfs_dev_exists(dev->si_name) != 0) 766 return (EEXIST); 767 768 return (0); 769 } 770 771 void 772 make_dev_args_init_impl(struct make_dev_args *args, size_t sz) 773 { 774 775 bzero(args, sz); 776 args->mda_size = sz; 777 } 778 779 static int 780 make_dev_sv(struct make_dev_args *args1, struct cdev **dres, 781 const char *fmt, va_list ap) 782 { 783 struct cdev *dev, *dev_new; 784 struct make_dev_args args; 785 int res; 786 787 bzero(&args, sizeof(args)); 788 if (sizeof(args) < args1->mda_size) 789 return (EINVAL); 790 bcopy(args1, &args, args1->mda_size); 791 KASSERT((args.mda_flags & MAKEDEV_WAITOK) == 0 || 792 (args.mda_flags & MAKEDEV_NOWAIT) == 0, 793 ("make_dev_sv: both WAITOK and NOWAIT specified")); 794 dev_new = devfs_alloc(args.mda_flags); 795 if (dev_new == NULL) 796 return (ENOMEM); 797 dev_lock(); 798 res = prep_cdevsw(args.mda_devsw, args.mda_flags); 799 if (res != 0) { 800 dev_unlock(); 801 devfs_free(dev_new); 802 return (res); 803 } 804 dev = newdev(&args, dev_new); 805 if ((dev->si_flags & SI_NAMED) == 0) { 806 res = prep_devname(dev, fmt, ap); 807 if (res != 0) { 808 if ((args.mda_flags & MAKEDEV_CHECKNAME) == 0) { 809 panic( 810 "make_dev_sv: bad si_name (error=%d, si_name=%s)", 811 res, dev->si_name); 812 } 813 if (dev == dev_new) { 814 LIST_REMOVE(dev, si_list); 815 dev_unlock(); 816 devfs_free(dev); 817 } else 818 dev_unlock(); 819 return (res); 820 } 821 } 822 if ((args.mda_flags & MAKEDEV_REF) != 0) 823 dev_refl(dev); 824 if ((args.mda_flags & MAKEDEV_ETERNAL) != 0) 825 dev->si_flags |= SI_ETERNAL; 826 KASSERT(!(dev->si_flags & SI_NAMED), 827 ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)", 828 args.mda_devsw->d_name, dev2unit(dev), devtoname(dev))); 829 dev->si_flags |= SI_NAMED; 830 if (args.mda_cr != NULL) 831 dev->si_cred = crhold(args.mda_cr); 832 dev->si_uid = args.mda_uid; 833 dev->si_gid = args.mda_gid; 834 dev->si_mode = args.mda_mode; 835 836 devfs_create(dev); 837 clean_unrhdrl(devfs_inos); 838 dev_unlock_and_free(); 839 840 notify_create(dev, args.mda_flags); 841 842 *dres = dev; 843 return (0); 844 } 845 846 int 847 make_dev_s(struct make_dev_args *args, struct cdev **dres, 848 const char *fmt, ...) 849 { 850 va_list ap; 851 int res; 852 853 va_start(ap, fmt); 854 res = make_dev_sv(args, dres, fmt, ap); 855 va_end(ap); 856 return (res); 857 } 858 859 static int 860 make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw, int unit, 861 struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, 862 va_list ap) 863 { 864 struct make_dev_args args; 865 866 make_dev_args_init(&args); 867 args.mda_flags = flags; 868 args.mda_devsw = devsw; 869 args.mda_cr = cr; 870 args.mda_uid = uid; 871 args.mda_gid = gid; 872 args.mda_mode = mode; 873 args.mda_unit = unit; 874 return (make_dev_sv(&args, dres, fmt, ap)); 875 } 876 877 struct cdev * 878 make_dev(struct cdevsw *devsw, int unit, uid_t uid, gid_t gid, int mode, 879 const char *fmt, ...) 880 { 881 struct cdev *dev; 882 va_list ap; 883 int res __unused; 884 885 va_start(ap, fmt); 886 res = make_dev_credv(0, &dev, devsw, unit, NULL, uid, gid, mode, fmt, 887 ap); 888 va_end(ap); 889 KASSERT(res == 0 && dev != NULL, 890 ("make_dev: failed make_dev_credv (error=%d)", res)); 891 return (dev); 892 } 893 894 struct cdev * 895 make_dev_cred(struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid, 896 gid_t gid, int mode, const char *fmt, ...) 897 { 898 struct cdev *dev; 899 va_list ap; 900 int res __unused; 901 902 va_start(ap, fmt); 903 res = make_dev_credv(0, &dev, devsw, unit, cr, uid, gid, mode, fmt, ap); 904 va_end(ap); 905 906 KASSERT(res == 0 && dev != NULL, 907 ("make_dev_cred: failed make_dev_credv (error=%d)", res)); 908 return (dev); 909 } 910 911 struct cdev * 912 make_dev_credf(int flags, struct cdevsw *devsw, int unit, struct ucred *cr, 913 uid_t uid, gid_t gid, int mode, const char *fmt, ...) 914 { 915 struct cdev *dev; 916 va_list ap; 917 int res; 918 919 va_start(ap, fmt); 920 res = make_dev_credv(flags, &dev, devsw, unit, cr, uid, gid, mode, 921 fmt, ap); 922 va_end(ap); 923 924 KASSERT(((flags & MAKEDEV_NOWAIT) != 0 && res == ENOMEM) || 925 ((flags & MAKEDEV_CHECKNAME) != 0 && res != ENOMEM) || res == 0, 926 ("make_dev_credf: failed make_dev_credv (error=%d)", res)); 927 return (res == 0 ? dev : NULL); 928 } 929 930 int 931 make_dev_p(int flags, struct cdev **cdev, struct cdevsw *devsw, 932 struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, ...) 933 { 934 va_list ap; 935 int res; 936 937 va_start(ap, fmt); 938 res = make_dev_credv(flags, cdev, devsw, 0, cr, uid, gid, mode, 939 fmt, ap); 940 va_end(ap); 941 942 KASSERT(((flags & MAKEDEV_NOWAIT) != 0 && res == ENOMEM) || 943 ((flags & MAKEDEV_CHECKNAME) != 0 && res != ENOMEM) || res == 0, 944 ("make_dev_p: failed make_dev_credv (error=%d)", res)); 945 return (res); 946 } 947 948 static void 949 dev_dependsl(struct cdev *pdev, struct cdev *cdev) 950 { 951 952 cdev->si_parent = pdev; 953 cdev->si_flags |= SI_CHILD; 954 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings); 955 } 956 957 void 958 dev_depends(struct cdev *pdev, struct cdev *cdev) 959 { 960 961 dev_lock(); 962 dev_dependsl(pdev, cdev); 963 dev_unlock(); 964 } 965 966 static int 967 make_dev_alias_v(int flags, struct cdev **cdev, struct cdev *pdev, 968 const char *fmt, va_list ap) 969 { 970 struct cdev *dev; 971 int error; 972 973 KASSERT(pdev != NULL, ("make_dev_alias_v: pdev is NULL")); 974 KASSERT((flags & MAKEDEV_WAITOK) == 0 || (flags & MAKEDEV_NOWAIT) == 0, 975 ("make_dev_alias_v: both WAITOK and NOWAIT specified")); 976 KASSERT((flags & ~(MAKEDEV_WAITOK | MAKEDEV_NOWAIT | 977 MAKEDEV_CHECKNAME)) == 0, 978 ("make_dev_alias_v: invalid flags specified (flags=%02x)", flags)); 979 980 dev = devfs_alloc(flags); 981 if (dev == NULL) 982 return (ENOMEM); 983 dev_lock(); 984 dev->si_flags |= SI_ALIAS; 985 error = prep_devname(dev, fmt, ap); 986 if (error != 0) { 987 if ((flags & MAKEDEV_CHECKNAME) == 0) { 988 panic("make_dev_alias_v: bad si_name " 989 "(error=%d, si_name=%s)", error, dev->si_name); 990 } 991 dev_unlock(); 992 devfs_free(dev); 993 return (error); 994 } 995 dev->si_flags |= SI_NAMED; 996 devfs_create(dev); 997 dev_dependsl(pdev, dev); 998 clean_unrhdrl(devfs_inos); 999 dev_unlock(); 1000 1001 notify_create(dev, flags); 1002 *cdev = dev; 1003 1004 return (0); 1005 } 1006 1007 struct cdev * 1008 make_dev_alias(struct cdev *pdev, const char *fmt, ...) 1009 { 1010 struct cdev *dev; 1011 va_list ap; 1012 int res __unused; 1013 1014 va_start(ap, fmt); 1015 res = make_dev_alias_v(MAKEDEV_WAITOK, &dev, pdev, fmt, ap); 1016 va_end(ap); 1017 1018 KASSERT(res == 0 && dev != NULL, 1019 ("make_dev_alias: failed make_dev_alias_v (error=%d)", res)); 1020 return (dev); 1021 } 1022 1023 int 1024 make_dev_alias_p(int flags, struct cdev **cdev, struct cdev *pdev, 1025 const char *fmt, ...) 1026 { 1027 va_list ap; 1028 int res; 1029 1030 va_start(ap, fmt); 1031 res = make_dev_alias_v(flags, cdev, pdev, fmt, ap); 1032 va_end(ap); 1033 return (res); 1034 } 1035 1036 int 1037 make_dev_physpath_alias(int flags, struct cdev **cdev, struct cdev *pdev, 1038 struct cdev *old_alias, const char *physpath) 1039 { 1040 char *devfspath; 1041 int physpath_len; 1042 int max_parentpath_len; 1043 int parentpath_len; 1044 int devfspathbuf_len; 1045 int mflags; 1046 int ret; 1047 1048 *cdev = NULL; 1049 devfspath = NULL; 1050 physpath_len = strlen(physpath); 1051 ret = EINVAL; 1052 if (physpath_len == 0) 1053 goto out; 1054 1055 if (strncmp("id1,", physpath, 4) == 0) { 1056 physpath += 4; 1057 physpath_len -= 4; 1058 if (physpath_len == 0) 1059 goto out; 1060 } 1061 1062 max_parentpath_len = SPECNAMELEN - physpath_len - /*/*/1; 1063 parentpath_len = strlen(pdev->si_name); 1064 if (max_parentpath_len < parentpath_len) { 1065 if (bootverbose) 1066 printf("WARNING: Unable to alias %s " 1067 "to %s/%s - path too long\n", 1068 pdev->si_name, physpath, pdev->si_name); 1069 ret = ENAMETOOLONG; 1070 goto out; 1071 } 1072 1073 mflags = (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK; 1074 devfspathbuf_len = physpath_len + /*/*/1 + parentpath_len + /*NUL*/1; 1075 devfspath = malloc(devfspathbuf_len, M_DEVBUF, mflags); 1076 if (devfspath == NULL) { 1077 ret = ENOMEM; 1078 goto out; 1079 } 1080 1081 sprintf(devfspath, "%s/%s", physpath, pdev->si_name); 1082 if (old_alias != NULL && strcmp(old_alias->si_name, devfspath) == 0) { 1083 /* Retain the existing alias. */ 1084 *cdev = old_alias; 1085 old_alias = NULL; 1086 ret = 0; 1087 } else { 1088 ret = make_dev_alias_p(flags, cdev, pdev, "%s", devfspath); 1089 } 1090 out: 1091 if (old_alias != NULL) 1092 destroy_dev(old_alias); 1093 if (devfspath != NULL) 1094 free(devfspath, M_DEVBUF); 1095 return (ret); 1096 } 1097 1098 static void 1099 destroy_devl(struct cdev *dev) 1100 { 1101 struct cdevsw *csw; 1102 struct cdev_privdata *p; 1103 struct cdev_priv *cdp; 1104 1105 dev_lock_assert_locked(); 1106 KASSERT(dev->si_flags & SI_NAMED, 1107 ("WARNING: Driver mistake: destroy_dev on %d\n", dev2unit(dev))); 1108 KASSERT((dev->si_flags & SI_ETERNAL) == 0, 1109 ("WARNING: Driver mistake: destroy_dev on eternal %d\n", 1110 dev2unit(dev))); 1111 1112 cdp = cdev2priv(dev); 1113 if ((cdp->cdp_flags & CDP_UNREF_DTR) == 0) { 1114 /* 1115 * Avoid race with dev_rel(), e.g. from the populate 1116 * loop. If CDP_UNREF_DTR flag is set, the reference 1117 * to be dropped at the end of destroy_devl() was 1118 * already taken by delist_dev_locked(). 1119 */ 1120 dev_refl(dev); 1121 1122 devfs_destroy(dev); 1123 } 1124 1125 /* Remove name marking */ 1126 dev->si_flags &= ~SI_NAMED; 1127 1128 /* If we are a child, remove us from the parents list */ 1129 if (dev->si_flags & SI_CHILD) { 1130 LIST_REMOVE(dev, si_siblings); 1131 dev->si_flags &= ~SI_CHILD; 1132 } 1133 1134 /* Kill our children */ 1135 while (!LIST_EMPTY(&dev->si_children)) 1136 destroy_devl(LIST_FIRST(&dev->si_children)); 1137 1138 /* Remove from clone list */ 1139 if (dev->si_flags & SI_CLONELIST) { 1140 LIST_REMOVE(dev, si_clone); 1141 dev->si_flags &= ~SI_CLONELIST; 1142 } 1143 1144 mtx_lock(&cdp->cdp_threadlock); 1145 csw = dev->si_devsw; 1146 dev->si_devsw = NULL; /* already NULL for SI_ALIAS */ 1147 while (csw != NULL && csw->d_purge != NULL && dev->si_threadcount) { 1148 csw->d_purge(dev); 1149 mtx_unlock(&cdp->cdp_threadlock); 1150 msleep(csw, &devmtx, PRIBIO, "devprg", hz/10); 1151 mtx_lock(&cdp->cdp_threadlock); 1152 if (dev->si_threadcount) 1153 printf("Still %lu threads in %s\n", 1154 dev->si_threadcount, devtoname(dev)); 1155 } 1156 while (dev->si_threadcount != 0) { 1157 /* Use unique dummy wait ident */ 1158 mtx_unlock(&cdp->cdp_threadlock); 1159 msleep(&csw, &devmtx, PRIBIO, "devdrn", hz / 10); 1160 mtx_lock(&cdp->cdp_threadlock); 1161 } 1162 1163 mtx_unlock(&cdp->cdp_threadlock); 1164 dev_unlock(); 1165 if ((cdp->cdp_flags & CDP_UNREF_DTR) == 0) { 1166 /* avoid out of order notify events */ 1167 notify_destroy(dev); 1168 } 1169 mtx_lock(&cdevpriv_mtx); 1170 while ((p = LIST_FIRST(&cdp->cdp_fdpriv)) != NULL) { 1171 devfs_destroy_cdevpriv(p); 1172 mtx_lock(&cdevpriv_mtx); 1173 } 1174 mtx_unlock(&cdevpriv_mtx); 1175 dev_lock(); 1176 1177 dev->si_drv1 = 0; 1178 dev->si_drv2 = 0; 1179 1180 if (!(dev->si_flags & SI_ALIAS)) { 1181 /* Remove from cdevsw list */ 1182 LIST_REMOVE(dev, si_list); 1183 1184 /* If cdevsw has no more struct cdev *'s, clean it */ 1185 if (LIST_EMPTY(&csw->d_devs)) { 1186 fini_cdevsw(csw); 1187 wakeup(&csw->d_devs); 1188 } 1189 } 1190 dev->si_flags &= ~SI_ALIAS; 1191 cdp->cdp_flags &= ~CDP_UNREF_DTR; 1192 dev->si_refcount--; 1193 1194 if (dev->si_refcount > 0) 1195 LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list); 1196 else 1197 dev_free_devlocked(dev); 1198 } 1199 1200 static void 1201 delist_dev_locked(struct cdev *dev) 1202 { 1203 struct cdev_priv *cdp; 1204 struct cdev *child; 1205 1206 dev_lock_assert_locked(); 1207 cdp = cdev2priv(dev); 1208 if ((cdp->cdp_flags & CDP_UNREF_DTR) != 0) 1209 return; 1210 cdp->cdp_flags |= CDP_UNREF_DTR; 1211 dev_refl(dev); 1212 devfs_destroy(dev); 1213 LIST_FOREACH(child, &dev->si_children, si_siblings) 1214 delist_dev_locked(child); 1215 dev_unlock(); 1216 /* ensure the destroy event is queued in order */ 1217 notify_destroy(dev); 1218 dev_lock(); 1219 } 1220 1221 /* 1222 * This function will delist a character device and its children from 1223 * the directory listing and create a destroy event without waiting 1224 * for all character device references to go away. At some later point 1225 * destroy_dev() must be called to complete the character device 1226 * destruction. After calling this function the character device name 1227 * can instantly be re-used. 1228 */ 1229 void 1230 delist_dev(struct cdev *dev) 1231 { 1232 1233 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "delist_dev"); 1234 dev_lock(); 1235 delist_dev_locked(dev); 1236 dev_unlock(); 1237 } 1238 1239 void 1240 destroy_dev(struct cdev *dev) 1241 { 1242 1243 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "destroy_dev"); 1244 dev_lock(); 1245 destroy_devl(dev); 1246 dev_unlock_and_free(); 1247 } 1248 1249 const char * 1250 devtoname(struct cdev *dev) 1251 { 1252 1253 return (dev->si_name); 1254 } 1255 1256 int 1257 dev_stdclone(char *name, char **namep, const char *stem, int *unit) 1258 { 1259 int u, i; 1260 1261 i = strlen(stem); 1262 if (strncmp(stem, name, i) != 0) 1263 return (0); 1264 if (!isdigit(name[i])) 1265 return (0); 1266 u = 0; 1267 if (name[i] == '0' && isdigit(name[i+1])) 1268 return (0); 1269 while (isdigit(name[i])) { 1270 u *= 10; 1271 u += name[i++] - '0'; 1272 } 1273 if (u > 0xffffff) 1274 return (0); 1275 *unit = u; 1276 if (namep) 1277 *namep = &name[i]; 1278 if (name[i]) 1279 return (2); 1280 return (1); 1281 } 1282 1283 /* 1284 * Helper functions for cloning device drivers. 1285 * 1286 * The objective here is to make it unnecessary for the device drivers to 1287 * use rman or similar to manage their unit number space. Due to the way 1288 * we do "on-demand" devices, using rman or other "private" methods 1289 * will be very tricky to lock down properly once we lock down this file. 1290 * 1291 * Instead we give the drivers these routines which puts the struct cdev *'s 1292 * that are to be managed on their own list, and gives the driver the ability 1293 * to ask for the first free unit number or a given specified unit number. 1294 * 1295 * In addition these routines support paired devices (pty, nmdm and similar) 1296 * by respecting a number of "flag" bits in the minor number. 1297 * 1298 */ 1299 1300 struct clonedevs { 1301 LIST_HEAD(,cdev) head; 1302 }; 1303 1304 void 1305 clone_setup(struct clonedevs **cdp) 1306 { 1307 1308 *cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO); 1309 LIST_INIT(&(*cdp)->head); 1310 } 1311 1312 int 1313 clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up, 1314 struct cdev **dp, int extra) 1315 { 1316 struct clonedevs *cd; 1317 struct cdev *dev, *ndev, *dl, *de; 1318 struct make_dev_args args; 1319 int unit, low, u; 1320 1321 KASSERT(*cdp != NULL, 1322 ("clone_setup() not called in driver \"%s\"", csw->d_name)); 1323 KASSERT(!(extra & CLONE_UNITMASK), 1324 ("Illegal extra bits (0x%x) in clone_create", extra)); 1325 KASSERT(*up <= CLONE_UNITMASK, 1326 ("Too high unit (0x%x) in clone_create", *up)); 1327 KASSERT(csw->d_flags & D_NEEDMINOR, 1328 ("clone_create() on cdevsw without minor numbers")); 1329 1330 /* 1331 * Search the list for a lot of things in one go: 1332 * A preexisting match is returned immediately. 1333 * The lowest free unit number if we are passed -1, and the place 1334 * in the list where we should insert that new element. 1335 * The place to insert a specified unit number, if applicable 1336 * the end of the list. 1337 */ 1338 unit = *up; 1339 ndev = devfs_alloc(MAKEDEV_WAITOK); 1340 dev_lock(); 1341 prep_cdevsw(csw, MAKEDEV_WAITOK); 1342 low = extra; 1343 de = dl = NULL; 1344 cd = *cdp; 1345 LIST_FOREACH(dev, &cd->head, si_clone) { 1346 KASSERT(dev->si_flags & SI_CLONELIST, 1347 ("Dev %p(%s) should be on clonelist", dev, dev->si_name)); 1348 u = dev2unit(dev); 1349 if (u == (unit | extra)) { 1350 *dp = dev; 1351 dev_unlock(); 1352 devfs_free(ndev); 1353 return (0); 1354 } 1355 if (unit == -1 && u == low) { 1356 low++; 1357 de = dev; 1358 continue; 1359 } else if (u < (unit | extra)) { 1360 de = dev; 1361 continue; 1362 } else if (u > (unit | extra)) { 1363 dl = dev; 1364 break; 1365 } 1366 } 1367 if (unit == -1) 1368 unit = low & CLONE_UNITMASK; 1369 make_dev_args_init(&args); 1370 args.mda_unit = unit | extra; 1371 args.mda_devsw = csw; 1372 dev = newdev(&args, ndev); 1373 if (dev->si_flags & SI_CLONELIST) { 1374 printf("dev %p (%s) is on clonelist\n", dev, dev->si_name); 1375 printf("unit=%d, low=%d, extra=0x%x\n", unit, low, extra); 1376 LIST_FOREACH(dev, &cd->head, si_clone) { 1377 printf("\t%p %s\n", dev, dev->si_name); 1378 } 1379 panic("foo"); 1380 } 1381 KASSERT(!(dev->si_flags & SI_CLONELIST), 1382 ("Dev %p(%s) should not be on clonelist", dev, dev->si_name)); 1383 if (dl != NULL) 1384 LIST_INSERT_BEFORE(dl, dev, si_clone); 1385 else if (de != NULL) 1386 LIST_INSERT_AFTER(de, dev, si_clone); 1387 else 1388 LIST_INSERT_HEAD(&cd->head, dev, si_clone); 1389 dev->si_flags |= SI_CLONELIST; 1390 *up = unit; 1391 dev_unlock_and_free(); 1392 return (1); 1393 } 1394 1395 /* 1396 * Kill everything still on the list. The driver should already have 1397 * disposed of any softc hung of the struct cdev *'s at this time. 1398 */ 1399 void 1400 clone_cleanup(struct clonedevs **cdp) 1401 { 1402 struct cdev *dev; 1403 struct cdev_priv *cp; 1404 struct clonedevs *cd; 1405 1406 cd = *cdp; 1407 if (cd == NULL) 1408 return; 1409 dev_lock(); 1410 while (!LIST_EMPTY(&cd->head)) { 1411 dev = LIST_FIRST(&cd->head); 1412 LIST_REMOVE(dev, si_clone); 1413 KASSERT(dev->si_flags & SI_CLONELIST, 1414 ("Dev %p(%s) should be on clonelist", dev, dev->si_name)); 1415 dev->si_flags &= ~SI_CLONELIST; 1416 cp = cdev2priv(dev); 1417 if (!(cp->cdp_flags & CDP_SCHED_DTR)) { 1418 cp->cdp_flags |= CDP_SCHED_DTR; 1419 KASSERT(dev->si_flags & SI_NAMED, 1420 ("Driver has goofed in cloning underways udev %jx unit %x", 1421 (uintmax_t)dev2udev(dev), dev2unit(dev))); 1422 destroy_devl(dev); 1423 } 1424 } 1425 dev_unlock_and_free(); 1426 free(cd, M_DEVBUF); 1427 *cdp = NULL; 1428 } 1429 1430 static TAILQ_HEAD(, cdev_priv) dev_ddtr = 1431 TAILQ_HEAD_INITIALIZER(dev_ddtr); 1432 static TAILQ_HEAD(, cdev_priv) dev_ddtr_giant = 1433 TAILQ_HEAD_INITIALIZER(dev_ddtr_giant); 1434 static struct task dev_dtr_task = TASK_INITIALIZER(0, destroy_dev_tq, &dev_ddtr); 1435 static struct task dev_dtr_task_giant = TASK_INITIALIZER(0, destroy_dev_tq_giant, 1436 &dev_ddtr_giant); 1437 1438 static void 1439 destroy_dev_tq(void *ctx, int pending) 1440 { 1441 TAILQ_HEAD(, cdev_priv) *ddtr = ctx; 1442 struct cdev_priv *cp; 1443 struct cdev *dev; 1444 void (*cb)(void *); 1445 void *cb_arg; 1446 1447 dev_lock(); 1448 while (!TAILQ_EMPTY(ddtr)) { 1449 cp = TAILQ_FIRST(ddtr); 1450 dev = &cp->cdp_c; 1451 KASSERT(cp->cdp_flags & CDP_SCHED_DTR, 1452 ("cdev %p in dev_destroy_tq without CDP_SCHED_DTR", cp)); 1453 TAILQ_REMOVE(ddtr, cp, cdp_dtr_list); 1454 cb = cp->cdp_dtr_cb; 1455 cb_arg = cp->cdp_dtr_cb_arg; 1456 destroy_devl(dev); 1457 dev_unlock_and_free(); 1458 dev_rel(dev); 1459 if (cb != NULL) 1460 cb(cb_arg); 1461 dev_lock(); 1462 } 1463 dev_unlock(); 1464 } 1465 1466 static void 1467 destroy_dev_tq_giant(void *ctx, int pending) 1468 { 1469 mtx_lock(&Giant); 1470 destroy_dev_tq(ctx, pending); 1471 mtx_unlock(&Giant); 1472 } 1473 1474 /* 1475 * devmtx shall be locked on entry. devmtx will be unlocked after 1476 * function return. 1477 */ 1478 static int 1479 destroy_dev_sched_cbl(struct cdev *dev, void (*cb)(void *), void *arg) 1480 { 1481 struct cdev_priv *cp; 1482 bool need_giant; 1483 1484 dev_lock_assert_locked(); 1485 cp = cdev2priv(dev); 1486 if (cp->cdp_flags & CDP_SCHED_DTR) { 1487 dev_unlock(); 1488 return (0); 1489 } 1490 dev_refl(dev); 1491 cp->cdp_flags |= CDP_SCHED_DTR; 1492 cp->cdp_dtr_cb = cb; 1493 cp->cdp_dtr_cb_arg = arg; 1494 need_giant = (dev->si_devsw->d_flags & D_NEEDGIANT) != 0; 1495 if (need_giant) 1496 TAILQ_INSERT_TAIL(&dev_ddtr_giant, cp, cdp_dtr_list); 1497 else 1498 TAILQ_INSERT_TAIL(&dev_ddtr, cp, cdp_dtr_list); 1499 dev_unlock(); 1500 if (need_giant) 1501 taskqueue_enqueue(taskqueue_thread, &dev_dtr_task_giant); 1502 else 1503 taskqueue_enqueue(taskqueue_thread, &dev_dtr_task); 1504 return (1); 1505 } 1506 1507 int 1508 destroy_dev_sched_cb(struct cdev *dev, void (*cb)(void *), void *arg) 1509 { 1510 1511 dev_lock(); 1512 return (destroy_dev_sched_cbl(dev, cb, arg)); 1513 } 1514 1515 int 1516 destroy_dev_sched(struct cdev *dev) 1517 { 1518 1519 return (destroy_dev_sched_cb(dev, NULL, NULL)); 1520 } 1521 1522 void 1523 destroy_dev_drain(struct cdevsw *csw) 1524 { 1525 1526 dev_lock(); 1527 while (!LIST_EMPTY(&csw->d_devs)) { 1528 msleep(&csw->d_devs, &devmtx, PRIBIO, "devscd", hz/10); 1529 } 1530 dev_unlock(); 1531 } 1532 1533 void 1534 drain_dev_clone_events(void) 1535 { 1536 1537 sx_xlock(&clone_drain_lock); 1538 sx_xunlock(&clone_drain_lock); 1539 } 1540 1541 #include "opt_ddb.h" 1542 #ifdef DDB 1543 #include <sys/kernel.h> 1544 1545 #include <ddb/ddb.h> 1546 1547 DB_SHOW_COMMAND(cdev, db_show_cdev) 1548 { 1549 struct cdev_priv *cdp; 1550 struct cdev *dev; 1551 u_int flags; 1552 char buf[512]; 1553 1554 if (!have_addr) { 1555 TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) { 1556 dev = &cdp->cdp_c; 1557 db_printf("%s %p\n", dev->si_name, dev); 1558 if (db_pager_quit) 1559 break; 1560 } 1561 return; 1562 } 1563 1564 dev = (struct cdev *)addr; 1565 cdp = cdev2priv(dev); 1566 db_printf("dev %s ref %d use %ld thr %ld inuse %u fdpriv %p\n", 1567 dev->si_name, dev->si_refcount, dev->si_usecount, 1568 dev->si_threadcount, cdp->cdp_inuse, cdp->cdp_fdpriv.lh_first); 1569 db_printf("devsw %p si_drv0 %d si_drv1 %p si_drv2 %p\n", 1570 dev->si_devsw, dev->si_drv0, dev->si_drv1, dev->si_drv2); 1571 flags = dev->si_flags; 1572 #define SI_FLAG(flag) do { \ 1573 if (flags & (flag)) { \ 1574 if (buf[0] != '\0') \ 1575 strlcat(buf, ", ", sizeof(buf)); \ 1576 strlcat(buf, (#flag) + 3, sizeof(buf)); \ 1577 flags &= ~(flag); \ 1578 } \ 1579 } while (0) 1580 buf[0] = '\0'; 1581 SI_FLAG(SI_ETERNAL); 1582 SI_FLAG(SI_ALIAS); 1583 SI_FLAG(SI_NAMED); 1584 SI_FLAG(SI_CHILD); 1585 SI_FLAG(SI_DUMPDEV); 1586 SI_FLAG(SI_CLONELIST); 1587 db_printf("si_flags %s\n", buf); 1588 1589 flags = cdp->cdp_flags; 1590 #define CDP_FLAG(flag) do { \ 1591 if (flags & (flag)) { \ 1592 if (buf[0] != '\0') \ 1593 strlcat(buf, ", ", sizeof(buf)); \ 1594 strlcat(buf, (#flag) + 4, sizeof(buf)); \ 1595 flags &= ~(flag); \ 1596 } \ 1597 } while (0) 1598 buf[0] = '\0'; 1599 CDP_FLAG(CDP_ACTIVE); 1600 CDP_FLAG(CDP_SCHED_DTR); 1601 db_printf("cdp_flags %s\n", buf); 1602 } 1603 #endif 1604