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