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