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