xref: /freebsd/sys/kern/kern_conf.c (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
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 (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 	mtx_assert(&devmtx, MA_NOTOWNED);
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 	mtx_assert(&devmtx, MA_NOTOWNED);
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 	mtx_assert(&devmtx, MA_NOTOWNED);
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_f("DEVFS", "CDEV", ev, data, mflags);
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 	mtx_assert(&devmtx, MA_OWNED);
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 	mtx_assert(&devmtx, MA_OWNED);
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) {
669 		printf("WARNING: Device \"%s\" is Giant locked and may be "
670 		    "deleted before FreeBSD 13.0.\n",
671 		    devsw->d_name == NULL ? "???" : devsw->d_name);
672 		if (devsw->d_gianttrick == NULL) {
673 			memcpy(dsw2, devsw, sizeof *dsw2);
674 			devsw->d_gianttrick = dsw2;
675 			dsw2 = NULL;
676 		}
677 	}
678 
679 #define FIXUP(member, noop, giant) 				\
680 	do {							\
681 		if (devsw->member == NULL) {			\
682 			devsw->member = noop;			\
683 		} else if (devsw->d_flags & D_NEEDGIANT)	\
684 			devsw->member = giant;			\
685 		}						\
686 	while (0)
687 
688 	FIXUP(d_open,		null_open,	giant_open);
689 	FIXUP(d_fdopen,		NULL,		giant_fdopen);
690 	FIXUP(d_close,		null_close,	giant_close);
691 	FIXUP(d_read,		no_read,	giant_read);
692 	FIXUP(d_write,		no_write,	giant_write);
693 	FIXUP(d_ioctl,		no_ioctl,	giant_ioctl);
694 	FIXUP(d_poll,		no_poll,	giant_poll);
695 	FIXUP(d_mmap,		no_mmap,	giant_mmap);
696 	FIXUP(d_strategy,	no_strategy,	giant_strategy);
697 	FIXUP(d_kqfilter,	no_kqfilter,	giant_kqfilter);
698 	FIXUP(d_mmap_single,	no_mmap_single,	giant_mmap_single);
699 
700 	if (devsw->d_dump == NULL)	devsw->d_dump = no_dump;
701 
702 	LIST_INIT(&devsw->d_devs);
703 
704 	devsw->d_flags |= D_INIT;
705 
706 	if (dsw2 != NULL)
707 		cdevsw_free_devlocked(dsw2);
708 	return (0);
709 }
710 
711 static int
712 prep_devname(struct cdev *dev, const char *fmt, va_list ap)
713 {
714 	int len;
715 	char *from, *q, *s, *to;
716 
717 	mtx_assert(&devmtx, MA_OWNED);
718 
719 	len = vsnrprintf(dev->si_name, sizeof(dev->si_name), 32, fmt, ap);
720 	if (len > sizeof(dev->si_name) - 1)
721 		return (ENAMETOOLONG);
722 
723 	/* Strip leading slashes. */
724 	for (from = dev->si_name; *from == '/'; from++)
725 		;
726 
727 	for (to = dev->si_name; *from != '\0'; from++, to++) {
728 		/*
729 		 * Spaces and double quotation marks cause
730 		 * problems for the devctl(4) protocol.
731 		 * Reject names containing those characters.
732 		 */
733 		if (isspace(*from) || *from == '"')
734 			return (EINVAL);
735 		/* Treat multiple sequential slashes as single. */
736 		while (from[0] == '/' && from[1] == '/')
737 			from++;
738 		/* Trailing slash is considered invalid. */
739 		if (from[0] == '/' && from[1] == '\0')
740 			return (EINVAL);
741 		*to = *from;
742 	}
743 	*to = '\0';
744 
745 	if (dev->si_name[0] == '\0')
746 		return (EINVAL);
747 
748 	/* Disallow "." and ".." components. */
749 	for (s = dev->si_name;;) {
750 		for (q = s; *q != '/' && *q != '\0'; q++)
751 			;
752 		if (q - s == 1 && s[0] == '.')
753 			return (EINVAL);
754 		if (q - s == 2 && s[0] == '.' && s[1] == '.')
755 			return (EINVAL);
756 		if (*q != '/')
757 			break;
758 		s = q + 1;
759 	}
760 
761 	if (devfs_dev_exists(dev->si_name) != 0)
762 		return (EEXIST);
763 
764 	return (0);
765 }
766 
767 void
768 make_dev_args_init_impl(struct make_dev_args *args, size_t sz)
769 {
770 
771 	bzero(args, sz);
772 	args->mda_size = sz;
773 }
774 
775 static int
776 make_dev_sv(struct make_dev_args *args1, struct cdev **dres,
777     const char *fmt, va_list ap)
778 {
779 	struct cdev *dev, *dev_new;
780 	struct make_dev_args args;
781 	int res;
782 
783 	bzero(&args, sizeof(args));
784 	if (sizeof(args) < args1->mda_size)
785 		return (EINVAL);
786 	bcopy(args1, &args, args1->mda_size);
787 	KASSERT((args.mda_flags & MAKEDEV_WAITOK) == 0 ||
788 	    (args.mda_flags & MAKEDEV_NOWAIT) == 0,
789 	    ("make_dev_sv: both WAITOK and NOWAIT specified"));
790 	dev_new = devfs_alloc(args.mda_flags);
791 	if (dev_new == NULL)
792 		return (ENOMEM);
793 	dev_lock();
794 	res = prep_cdevsw(args.mda_devsw, args.mda_flags);
795 	if (res != 0) {
796 		dev_unlock();
797 		devfs_free(dev_new);
798 		return (res);
799 	}
800 	dev = newdev(&args, dev_new);
801 	if ((dev->si_flags & SI_NAMED) == 0) {
802 		res = prep_devname(dev, fmt, ap);
803 		if (res != 0) {
804 			if ((args.mda_flags & MAKEDEV_CHECKNAME) == 0) {
805 				panic(
806 			"make_dev_sv: bad si_name (error=%d, si_name=%s)",
807 				    res, dev->si_name);
808 			}
809 			if (dev == dev_new) {
810 				LIST_REMOVE(dev, si_list);
811 				dev_unlock();
812 				devfs_free(dev);
813 			} else
814 				dev_unlock();
815 			return (res);
816 		}
817 	}
818 	if ((args.mda_flags & MAKEDEV_REF) != 0)
819 		dev_refl(dev);
820 	if ((args.mda_flags & MAKEDEV_ETERNAL) != 0)
821 		dev->si_flags |= SI_ETERNAL;
822 	KASSERT(!(dev->si_flags & SI_NAMED),
823 	    ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)",
824 	    args.mda_devsw->d_name, dev2unit(dev), devtoname(dev)));
825 	dev->si_flags |= SI_NAMED;
826 	if (args.mda_cr != NULL)
827 		dev->si_cred = crhold(args.mda_cr);
828 	dev->si_uid = args.mda_uid;
829 	dev->si_gid = args.mda_gid;
830 	dev->si_mode = args.mda_mode;
831 
832 	devfs_create(dev);
833 	clean_unrhdrl(devfs_inos);
834 	dev_unlock_and_free();
835 
836 	notify_create(dev, args.mda_flags);
837 
838 	*dres = dev;
839 	return (0);
840 }
841 
842 int
843 make_dev_s(struct make_dev_args *args, struct cdev **dres,
844     const char *fmt, ...)
845 {
846 	va_list ap;
847 	int res;
848 
849 	va_start(ap, fmt);
850 	res = make_dev_sv(args, dres, fmt, ap);
851 	va_end(ap);
852 	return (res);
853 }
854 
855 static int
856 make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw, int unit,
857     struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,
858     va_list ap)
859 {
860 	struct make_dev_args args;
861 
862 	make_dev_args_init(&args);
863 	args.mda_flags = flags;
864 	args.mda_devsw = devsw;
865 	args.mda_cr = cr;
866 	args.mda_uid = uid;
867 	args.mda_gid = gid;
868 	args.mda_mode = mode;
869 	args.mda_unit = unit;
870 	return (make_dev_sv(&args, dres, fmt, ap));
871 }
872 
873 struct cdev *
874 make_dev(struct cdevsw *devsw, int unit, uid_t uid, gid_t gid, int mode,
875     const char *fmt, ...)
876 {
877 	struct cdev *dev;
878 	va_list ap;
879 	int res __unused;
880 
881 	va_start(ap, fmt);
882 	res = make_dev_credv(0, &dev, devsw, unit, NULL, uid, gid, mode, fmt,
883 		      ap);
884 	va_end(ap);
885 	KASSERT(res == 0 && dev != NULL,
886 	    ("make_dev: failed make_dev_credv (error=%d)", res));
887 	return (dev);
888 }
889 
890 struct cdev *
891 make_dev_cred(struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid,
892     gid_t gid, int mode, const char *fmt, ...)
893 {
894 	struct cdev *dev;
895 	va_list ap;
896 	int res __unused;
897 
898 	va_start(ap, fmt);
899 	res = make_dev_credv(0, &dev, devsw, unit, cr, uid, gid, mode, fmt, ap);
900 	va_end(ap);
901 
902 	KASSERT(res == 0 && dev != NULL,
903 	    ("make_dev_cred: failed make_dev_credv (error=%d)", res));
904 	return (dev);
905 }
906 
907 struct cdev *
908 make_dev_credf(int flags, struct cdevsw *devsw, int unit, struct ucred *cr,
909     uid_t uid, gid_t gid, int mode, const char *fmt, ...)
910 {
911 	struct cdev *dev;
912 	va_list ap;
913 	int res;
914 
915 	va_start(ap, fmt);
916 	res = make_dev_credv(flags, &dev, devsw, unit, cr, uid, gid, mode,
917 	    fmt, ap);
918 	va_end(ap);
919 
920 	KASSERT(((flags & MAKEDEV_NOWAIT) != 0 && res == ENOMEM) ||
921 	    ((flags & MAKEDEV_CHECKNAME) != 0 && res != ENOMEM) || res == 0,
922 	    ("make_dev_credf: failed make_dev_credv (error=%d)", res));
923 	return (res == 0 ? dev : NULL);
924 }
925 
926 int
927 make_dev_p(int flags, struct cdev **cdev, struct cdevsw *devsw,
928     struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, ...)
929 {
930 	va_list ap;
931 	int res;
932 
933 	va_start(ap, fmt);
934 	res = make_dev_credv(flags, cdev, devsw, 0, cr, uid, gid, mode,
935 	    fmt, ap);
936 	va_end(ap);
937 
938 	KASSERT(((flags & MAKEDEV_NOWAIT) != 0 && res == ENOMEM) ||
939 	    ((flags & MAKEDEV_CHECKNAME) != 0 && res != ENOMEM) || res == 0,
940 	    ("make_dev_p: failed make_dev_credv (error=%d)", res));
941 	return (res);
942 }
943 
944 static void
945 dev_dependsl(struct cdev *pdev, struct cdev *cdev)
946 {
947 
948 	cdev->si_parent = pdev;
949 	cdev->si_flags |= SI_CHILD;
950 	LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings);
951 }
952 
953 
954 void
955 dev_depends(struct cdev *pdev, struct cdev *cdev)
956 {
957 
958 	dev_lock();
959 	dev_dependsl(pdev, cdev);
960 	dev_unlock();
961 }
962 
963 static int
964 make_dev_alias_v(int flags, struct cdev **cdev, struct cdev *pdev,
965     const char *fmt, va_list ap)
966 {
967 	struct cdev *dev;
968 	int error;
969 
970 	KASSERT(pdev != NULL, ("make_dev_alias_v: pdev is NULL"));
971 	KASSERT((flags & MAKEDEV_WAITOK) == 0 || (flags & MAKEDEV_NOWAIT) == 0,
972 	    ("make_dev_alias_v: both WAITOK and NOWAIT specified"));
973 	KASSERT((flags & ~(MAKEDEV_WAITOK | MAKEDEV_NOWAIT |
974 	    MAKEDEV_CHECKNAME)) == 0,
975 	    ("make_dev_alias_v: invalid flags specified (flags=%02x)", flags));
976 
977 	dev = devfs_alloc(flags);
978 	if (dev == NULL)
979 		return (ENOMEM);
980 	dev_lock();
981 	dev->si_flags |= SI_ALIAS;
982 	error = prep_devname(dev, fmt, ap);
983 	if (error != 0) {
984 		if ((flags & MAKEDEV_CHECKNAME) == 0) {
985 			panic("make_dev_alias_v: bad si_name "
986 			    "(error=%d, si_name=%s)", error, dev->si_name);
987 		}
988 		dev_unlock();
989 		devfs_free(dev);
990 		return (error);
991 	}
992 	dev->si_flags |= SI_NAMED;
993 	devfs_create(dev);
994 	dev_dependsl(pdev, dev);
995 	clean_unrhdrl(devfs_inos);
996 	dev_unlock();
997 
998 	notify_create(dev, flags);
999 	*cdev = dev;
1000 
1001 	return (0);
1002 }
1003 
1004 struct cdev *
1005 make_dev_alias(struct cdev *pdev, const char *fmt, ...)
1006 {
1007 	struct cdev *dev;
1008 	va_list ap;
1009 	int res __unused;
1010 
1011 	va_start(ap, fmt);
1012 	res = make_dev_alias_v(MAKEDEV_WAITOK, &dev, pdev, fmt, ap);
1013 	va_end(ap);
1014 
1015 	KASSERT(res == 0 && dev != NULL,
1016 	    ("make_dev_alias: failed make_dev_alias_v (error=%d)", res));
1017 	return (dev);
1018 }
1019 
1020 int
1021 make_dev_alias_p(int flags, struct cdev **cdev, struct cdev *pdev,
1022     const char *fmt, ...)
1023 {
1024 	va_list ap;
1025 	int res;
1026 
1027 	va_start(ap, fmt);
1028 	res = make_dev_alias_v(flags, cdev, pdev, fmt, ap);
1029 	va_end(ap);
1030 	return (res);
1031 }
1032 
1033 int
1034 make_dev_physpath_alias(int flags, struct cdev **cdev, struct cdev *pdev,
1035     struct cdev *old_alias, const char *physpath)
1036 {
1037 	char *devfspath;
1038 	int physpath_len;
1039 	int max_parentpath_len;
1040 	int parentpath_len;
1041 	int devfspathbuf_len;
1042 	int mflags;
1043 	int ret;
1044 
1045 	*cdev = NULL;
1046 	devfspath = NULL;
1047 	physpath_len = strlen(physpath);
1048 	ret = EINVAL;
1049 	if (physpath_len == 0)
1050 		goto out;
1051 
1052 	if (strncmp("id1,", physpath, 4) == 0) {
1053 		physpath += 4;
1054 		physpath_len -= 4;
1055 		if (physpath_len == 0)
1056 			goto out;
1057 	}
1058 
1059 	max_parentpath_len = SPECNAMELEN - physpath_len - /*/*/1;
1060 	parentpath_len = strlen(pdev->si_name);
1061 	if (max_parentpath_len < parentpath_len) {
1062 		if (bootverbose)
1063 			printf("WARNING: Unable to alias %s "
1064 			    "to %s/%s - path too long\n",
1065 			    pdev->si_name, physpath, pdev->si_name);
1066 		ret = ENAMETOOLONG;
1067 		goto out;
1068 	}
1069 
1070 	mflags = (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK;
1071 	devfspathbuf_len = physpath_len + /*/*/1 + parentpath_len + /*NUL*/1;
1072 	devfspath = malloc(devfspathbuf_len, M_DEVBUF, mflags);
1073 	if (devfspath == NULL) {
1074 		ret = ENOMEM;
1075 		goto out;
1076 	}
1077 
1078 	sprintf(devfspath, "%s/%s", physpath, pdev->si_name);
1079 	if (old_alias != NULL && strcmp(old_alias->si_name, devfspath) == 0) {
1080 		/* Retain the existing alias. */
1081 		*cdev = old_alias;
1082 		old_alias = NULL;
1083 		ret = 0;
1084 	} else {
1085 		ret = make_dev_alias_p(flags, cdev, pdev, "%s", devfspath);
1086 	}
1087 out:
1088 	if (old_alias != NULL)
1089 		destroy_dev(old_alias);
1090 	if (devfspath != NULL)
1091 		free(devfspath, M_DEVBUF);
1092 	return (ret);
1093 }
1094 
1095 static void
1096 destroy_devl(struct cdev *dev)
1097 {
1098 	struct cdevsw *csw;
1099 	struct cdev_privdata *p;
1100 	struct cdev_priv *cdp;
1101 
1102 	mtx_assert(&devmtx, MA_OWNED);
1103 	KASSERT(dev->si_flags & SI_NAMED,
1104 	    ("WARNING: Driver mistake: destroy_dev on %d\n", dev2unit(dev)));
1105 	KASSERT((dev->si_flags & SI_ETERNAL) == 0,
1106 	    ("WARNING: Driver mistake: destroy_dev on eternal %d\n",
1107 	     dev2unit(dev)));
1108 
1109 	cdp = cdev2priv(dev);
1110 	if ((cdp->cdp_flags & CDP_UNREF_DTR) == 0) {
1111 		/*
1112 		 * Avoid race with dev_rel(), e.g. from the populate
1113 		 * loop.  If CDP_UNREF_DTR flag is set, the reference
1114 		 * to be dropped at the end of destroy_devl() was
1115 		 * already taken by delist_dev_locked().
1116 		 */
1117 		dev_refl(dev);
1118 
1119 		devfs_destroy(dev);
1120 	}
1121 
1122 	/* Remove name marking */
1123 	dev->si_flags &= ~SI_NAMED;
1124 
1125 	/* If we are a child, remove us from the parents list */
1126 	if (dev->si_flags & SI_CHILD) {
1127 		LIST_REMOVE(dev, si_siblings);
1128 		dev->si_flags &= ~SI_CHILD;
1129 	}
1130 
1131 	/* Kill our children */
1132 	while (!LIST_EMPTY(&dev->si_children))
1133 		destroy_devl(LIST_FIRST(&dev->si_children));
1134 
1135 	/* Remove from clone list */
1136 	if (dev->si_flags & SI_CLONELIST) {
1137 		LIST_REMOVE(dev, si_clone);
1138 		dev->si_flags &= ~SI_CLONELIST;
1139 	}
1140 
1141 	mtx_lock(&cdp->cdp_threadlock);
1142 	csw = dev->si_devsw;
1143 	dev->si_devsw = NULL;	/* already NULL for SI_ALIAS */
1144 	while (csw != NULL && csw->d_purge != NULL && dev->si_threadcount) {
1145 		csw->d_purge(dev);
1146 		mtx_unlock(&cdp->cdp_threadlock);
1147 		msleep(csw, &devmtx, PRIBIO, "devprg", hz/10);
1148 		mtx_lock(&cdp->cdp_threadlock);
1149 		if (dev->si_threadcount)
1150 			printf("Still %lu threads in %s\n",
1151 			    dev->si_threadcount, devtoname(dev));
1152 	}
1153 	while (dev->si_threadcount != 0) {
1154 		/* Use unique dummy wait ident */
1155 		mtx_unlock(&cdp->cdp_threadlock);
1156 		msleep(&csw, &devmtx, PRIBIO, "devdrn", hz / 10);
1157 		mtx_lock(&cdp->cdp_threadlock);
1158 	}
1159 
1160 	mtx_unlock(&cdp->cdp_threadlock);
1161 	dev_unlock();
1162 	if ((cdp->cdp_flags & CDP_UNREF_DTR) == 0) {
1163 		/* avoid out of order notify events */
1164 		notify_destroy(dev);
1165 	}
1166 	mtx_lock(&cdevpriv_mtx);
1167 	while ((p = LIST_FIRST(&cdp->cdp_fdpriv)) != NULL) {
1168 		devfs_destroy_cdevpriv(p);
1169 		mtx_lock(&cdevpriv_mtx);
1170 	}
1171 	mtx_unlock(&cdevpriv_mtx);
1172 	dev_lock();
1173 
1174 	dev->si_drv1 = 0;
1175 	dev->si_drv2 = 0;
1176 	bzero(&dev->__si_u, sizeof(dev->__si_u));
1177 
1178 	if (!(dev->si_flags & SI_ALIAS)) {
1179 		/* Remove from cdevsw list */
1180 		LIST_REMOVE(dev, si_list);
1181 
1182 		/* If cdevsw has no more struct cdev *'s, clean it */
1183 		if (LIST_EMPTY(&csw->d_devs)) {
1184 			fini_cdevsw(csw);
1185 			wakeup(&csw->d_devs);
1186 		}
1187 	}
1188 	dev->si_flags &= ~SI_ALIAS;
1189 	cdp->cdp_flags &= ~CDP_UNREF_DTR;
1190 	dev->si_refcount--;
1191 
1192 	if (dev->si_refcount > 0)
1193 		LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list);
1194 	else
1195 		dev_free_devlocked(dev);
1196 }
1197 
1198 static void
1199 delist_dev_locked(struct cdev *dev)
1200 {
1201 	struct cdev_priv *cdp;
1202 	struct cdev *child;
1203 
1204 	mtx_assert(&devmtx, MA_OWNED);
1205 	cdp = cdev2priv(dev);
1206 	if ((cdp->cdp_flags & CDP_UNREF_DTR) != 0)
1207 		return;
1208 	cdp->cdp_flags |= CDP_UNREF_DTR;
1209 	dev_refl(dev);
1210 	devfs_destroy(dev);
1211 	LIST_FOREACH(child, &dev->si_children, si_siblings)
1212 		delist_dev_locked(child);
1213 	dev_unlock();
1214 	/* ensure the destroy event is queued in order */
1215 	notify_destroy(dev);
1216 	dev_lock();
1217 }
1218 
1219 /*
1220  * This function will delist a character device and its children from
1221  * the directory listing and create a destroy event without waiting
1222  * for all character device references to go away. At some later point
1223  * destroy_dev() must be called to complete the character device
1224  * destruction. After calling this function the character device name
1225  * can instantly be re-used.
1226  */
1227 void
1228 delist_dev(struct cdev *dev)
1229 {
1230 
1231 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "delist_dev");
1232 	dev_lock();
1233 	delist_dev_locked(dev);
1234 	dev_unlock();
1235 }
1236 
1237 void
1238 destroy_dev(struct cdev *dev)
1239 {
1240 
1241 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "destroy_dev");
1242 	dev_lock();
1243 	destroy_devl(dev);
1244 	dev_unlock_and_free();
1245 }
1246 
1247 const char *
1248 devtoname(struct cdev *dev)
1249 {
1250 
1251 	return (dev->si_name);
1252 }
1253 
1254 int
1255 dev_stdclone(char *name, char **namep, const char *stem, int *unit)
1256 {
1257 	int u, i;
1258 
1259 	i = strlen(stem);
1260 	if (bcmp(stem, name, i) != 0)
1261 		return (0);
1262 	if (!isdigit(name[i]))
1263 		return (0);
1264 	u = 0;
1265 	if (name[i] == '0' && isdigit(name[i+1]))
1266 		return (0);
1267 	while (isdigit(name[i])) {
1268 		u *= 10;
1269 		u += name[i++] - '0';
1270 	}
1271 	if (u > 0xffffff)
1272 		return (0);
1273 	*unit = u;
1274 	if (namep)
1275 		*namep = &name[i];
1276 	if (name[i])
1277 		return (2);
1278 	return (1);
1279 }
1280 
1281 /*
1282  * Helper functions for cloning device drivers.
1283  *
1284  * The objective here is to make it unnecessary for the device drivers to
1285  * use rman or similar to manage their unit number space.  Due to the way
1286  * we do "on-demand" devices, using rman or other "private" methods
1287  * will be very tricky to lock down properly once we lock down this file.
1288  *
1289  * Instead we give the drivers these routines which puts the struct cdev *'s
1290  * that are to be managed on their own list, and gives the driver the ability
1291  * to ask for the first free unit number or a given specified unit number.
1292  *
1293  * In addition these routines support paired devices (pty, nmdm and similar)
1294  * by respecting a number of "flag" bits in the minor number.
1295  *
1296  */
1297 
1298 struct clonedevs {
1299 	LIST_HEAD(,cdev)	head;
1300 };
1301 
1302 void
1303 clone_setup(struct clonedevs **cdp)
1304 {
1305 
1306 	*cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO);
1307 	LIST_INIT(&(*cdp)->head);
1308 }
1309 
1310 int
1311 clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up,
1312     struct cdev **dp, int extra)
1313 {
1314 	struct clonedevs *cd;
1315 	struct cdev *dev, *ndev, *dl, *de;
1316 	struct make_dev_args args;
1317 	int unit, low, u;
1318 
1319 	KASSERT(*cdp != NULL,
1320 	    ("clone_setup() not called in driver \"%s\"", csw->d_name));
1321 	KASSERT(!(extra & CLONE_UNITMASK),
1322 	    ("Illegal extra bits (0x%x) in clone_create", extra));
1323 	KASSERT(*up <= CLONE_UNITMASK,
1324 	    ("Too high unit (0x%x) in clone_create", *up));
1325 	KASSERT(csw->d_flags & D_NEEDMINOR,
1326 	    ("clone_create() on cdevsw without minor numbers"));
1327 
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 	mtx_assert(&devmtx, MA_OWNED);
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