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