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