xref: /freebsd/sys/kern/kern_conf.c (revision 8847579c57d6aff2b3371c707dce7a2cee8389aa)
1 /*-
2  * Copyright (c) 1999-2002 Poul-Henning Kamp
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/bio.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
38 #include <sys/conf.h>
39 #include <sys/vnode.h>
40 #include <sys/queue.h>
41 #include <sys/poll.h>
42 #include <sys/ctype.h>
43 #include <sys/tty.h>
44 #include <sys/ucred.h>
45 #include <machine/stdarg.h>
46 
47 #include <fs/devfs/devfs_int.h>
48 
49 static MALLOC_DEFINE(M_DEVT, "cdev", "cdev storage");
50 
51 struct mtx devmtx;
52 static void destroy_devl(struct cdev *dev);
53 static struct cdev *make_dev_credv(struct cdevsw *devsw, int minornr,
54 	    struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt,
55 	    va_list ap);
56 
57 void
58 dev_lock(void)
59 {
60 
61 	mtx_lock(&devmtx);
62 }
63 
64 void
65 dev_unlock(void)
66 {
67 
68 	mtx_unlock(&devmtx);
69 }
70 
71 void
72 dev_ref(struct cdev *dev)
73 {
74 
75 	mtx_assert(&devmtx, MA_NOTOWNED);
76 	mtx_lock(&devmtx);
77 	dev->si_refcount++;
78 	mtx_unlock(&devmtx);
79 }
80 
81 void
82 dev_refl(struct cdev *dev)
83 {
84 
85 	mtx_assert(&devmtx, MA_OWNED);
86 	dev->si_refcount++;
87 }
88 
89 void
90 dev_rel(struct cdev *dev)
91 {
92 	int flag = 0;
93 
94 	mtx_assert(&devmtx, MA_NOTOWNED);
95 	dev_lock();
96 	dev->si_refcount--;
97 	KASSERT(dev->si_refcount >= 0,
98 	    ("dev_rel(%s) gave negative count", devtoname(dev)));
99 #if 0
100 	if (dev->si_usecount == 0 &&
101 	    (dev->si_flags & SI_CHEAPCLONE) && (dev->si_flags & SI_NAMED))
102 		;
103 	else
104 #endif
105 	if (dev->si_devsw == NULL && dev->si_refcount == 0) {
106 		LIST_REMOVE(dev, si_list);
107 		flag = 1;
108 	}
109 	dev_unlock();
110 	if (flag)
111 		devfs_free(dev);
112 }
113 
114 struct cdevsw *
115 dev_refthread(struct cdev *dev)
116 {
117 	struct cdevsw *csw;
118 
119 	mtx_assert(&devmtx, MA_NOTOWNED);
120 	dev_lock();
121 	csw = dev->si_devsw;
122 	if (csw != NULL)
123 		dev->si_threadcount++;
124 	dev_unlock();
125 	return (csw);
126 }
127 
128 void
129 dev_relthread(struct cdev *dev)
130 {
131 
132 	mtx_assert(&devmtx, MA_NOTOWNED);
133 	dev_lock();
134 	dev->si_threadcount--;
135 	dev_unlock();
136 }
137 
138 int
139 nullop(void)
140 {
141 
142 	return (0);
143 }
144 
145 int
146 eopnotsupp(void)
147 {
148 
149 	return (EOPNOTSUPP);
150 }
151 
152 static int
153 enxio(void)
154 {
155 	return (ENXIO);
156 }
157 
158 static int
159 enodev(void)
160 {
161 	return (ENODEV);
162 }
163 
164 /* Define a dead_cdevsw for use when devices leave unexpectedly. */
165 
166 #define dead_open	(d_open_t *)enxio
167 #define dead_close	(d_close_t *)enxio
168 #define dead_read	(d_read_t *)enxio
169 #define dead_write	(d_write_t *)enxio
170 #define dead_ioctl	(d_ioctl_t *)enxio
171 #define dead_poll	(d_poll_t *)enodev
172 #define dead_mmap	(d_mmap_t *)enodev
173 
174 static void
175 dead_strategy(struct bio *bp)
176 {
177 
178 	biofinish(bp, NULL, ENXIO);
179 }
180 
181 #define dead_dump	(dumper_t *)enxio
182 #define dead_kqfilter	(d_kqfilter_t *)enxio
183 
184 static struct cdevsw dead_cdevsw = {
185 	.d_version =	D_VERSION,
186 	.d_flags =	D_NEEDGIANT, /* XXX: does dead_strategy need this ? */
187 	.d_open =	dead_open,
188 	.d_close =	dead_close,
189 	.d_read =	dead_read,
190 	.d_write =	dead_write,
191 	.d_ioctl =	dead_ioctl,
192 	.d_poll =	dead_poll,
193 	.d_mmap =	dead_mmap,
194 	.d_strategy =	dead_strategy,
195 	.d_name =	"dead",
196 	.d_dump =	dead_dump,
197 	.d_kqfilter =	dead_kqfilter
198 };
199 
200 /* Default methods if driver does not specify method */
201 
202 #define null_open	(d_open_t *)nullop
203 #define null_close	(d_close_t *)nullop
204 #define no_read		(d_read_t *)enodev
205 #define no_write	(d_write_t *)enodev
206 #define no_ioctl	(d_ioctl_t *)enodev
207 #define no_mmap		(d_mmap_t *)enodev
208 #define no_kqfilter	(d_kqfilter_t *)enodev
209 
210 static void
211 no_strategy(struct bio *bp)
212 {
213 
214 	biofinish(bp, NULL, ENODEV);
215 }
216 
217 static int
218 no_poll(struct cdev *dev __unused, int events, struct thread *td __unused)
219 {
220 	/*
221 	 * Return true for read/write.  If the user asked for something
222 	 * special, return POLLNVAL, so that clients have a way of
223 	 * determining reliably whether or not the extended
224 	 * functionality is present without hard-coding knowledge
225 	 * of specific filesystem implementations.
226 	 * Stay in sync with vop_nopoll().
227 	 */
228 	if (events & ~POLLSTANDARD)
229 		return (POLLNVAL);
230 
231 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
232 }
233 
234 #define no_dump		(dumper_t *)enodev
235 
236 static int
237 giant_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
238 {
239 	int retval;
240 
241 	mtx_lock(&Giant);
242 	retval = dev->si_devsw->d_gianttrick->
243 	    d_open(dev, oflags, devtype, td);
244 	mtx_unlock(&Giant);
245 	return (retval);
246 }
247 
248 static int
249 giant_fdopen(struct cdev *dev, int oflags, struct thread *td, int fdidx)
250 {
251 	int retval;
252 
253 	mtx_lock(&Giant);
254 	retval = dev->si_devsw->d_gianttrick->
255 	    d_fdopen(dev, oflags, td, fdidx);
256 	mtx_unlock(&Giant);
257 	return (retval);
258 }
259 
260 static int
261 giant_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
262 {
263 	int retval;
264 
265 	mtx_lock(&Giant);
266 	retval = dev->si_devsw->d_gianttrick->
267 	    d_close(dev, fflag, devtype, td);
268 	mtx_unlock(&Giant);
269 	return (retval);
270 }
271 
272 static void
273 giant_strategy(struct bio *bp)
274 {
275 
276 	mtx_lock(&Giant);
277 	bp->bio_dev->si_devsw->d_gianttrick->
278 	    d_strategy(bp);
279 	mtx_unlock(&Giant);
280 }
281 
282 static int
283 giant_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
284 {
285 	int retval;
286 
287 	mtx_lock(&Giant);
288 	retval = dev->si_devsw->d_gianttrick->
289 	    d_ioctl(dev, cmd, data, fflag, td);
290 	mtx_unlock(&Giant);
291 	return (retval);
292 }
293 
294 static int
295 giant_read(struct cdev *dev, struct uio *uio, int ioflag)
296 {
297 	int retval;
298 
299 	mtx_lock(&Giant);
300 	retval = dev->si_devsw->d_gianttrick->
301 	    d_read(dev, uio, ioflag);
302 	mtx_unlock(&Giant);
303 	return (retval);
304 }
305 
306 static int
307 giant_write(struct cdev *dev, struct uio *uio, int ioflag)
308 {
309 	int retval;
310 
311 	mtx_lock(&Giant);
312 	retval = dev->si_devsw->d_gianttrick->
313 		d_write(dev, uio, ioflag);
314 	mtx_unlock(&Giant);
315 	return (retval);
316 }
317 
318 static int
319 giant_poll(struct cdev *dev, int events, struct thread *td)
320 {
321 	int retval;
322 
323 	mtx_lock(&Giant);
324 	retval = dev->si_devsw->d_gianttrick->
325 	    d_poll(dev, events, td);
326 	mtx_unlock(&Giant);
327 	return (retval);
328 }
329 
330 static int
331 giant_kqfilter(struct cdev *dev, struct knote *kn)
332 {
333 	int retval;
334 
335 	mtx_lock(&Giant);
336 	retval = dev->si_devsw->d_gianttrick->
337 	    d_kqfilter(dev, kn);
338 	mtx_unlock(&Giant);
339 	return (retval);
340 }
341 
342 static int
343 giant_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
344 {
345 	int retval;
346 
347 	mtx_lock(&Giant);
348 	retval = dev->si_devsw->d_gianttrick->
349 	    d_mmap(dev, offset, paddr, nprot);
350 	mtx_unlock(&Giant);
351 	return (retval);
352 }
353 
354 
355 /*
356  * struct cdev * and u_dev_t primitives
357  */
358 
359 int
360 minor(struct cdev *x)
361 {
362 	if (x == NULL)
363 		return NODEV;
364 	return(x->si_drv0 & MAXMINOR);
365 }
366 
367 int
368 dev2unit(struct cdev *x)
369 {
370 
371 	if (x == NULL)
372 		return NODEV;
373 	return (minor2unit(minor(x)));
374 }
375 
376 u_int
377 minor2unit(u_int _minor)
378 {
379 
380 	KASSERT((_minor & ~MAXMINOR) == 0, ("Illegal minor %x", _minor));
381 	return ((_minor & 0xff) | ((_minor >> 8) & 0xffff00));
382 }
383 
384 int
385 unit2minor(int unit)
386 {
387 
388 	KASSERT(unit <= 0xffffff, ("Invalid unit (%d) in unit2minor", unit));
389 	return ((unit & 0xff) | ((unit << 8) & ~0xffff));
390 }
391 
392 static struct cdev *
393 newdev(struct cdevsw *csw, int y, struct cdev *si)
394 {
395 	struct cdev *si2;
396 	dev_t	udev;
397 
398 	mtx_assert(&devmtx, MA_OWNED);
399 	udev = y;
400 	LIST_FOREACH(si2, &csw->d_devs, si_list) {
401 		if (si2->si_drv0 == udev) {
402 			devfs_free(si);
403 			return (si2);
404 		}
405 	}
406 	si->si_drv0 = udev;
407 	si->si_devsw = csw;
408 	LIST_INSERT_HEAD(&csw->d_devs, si, si_list);
409 	return (si);
410 }
411 
412 int
413 uminor(dev_t dev)
414 {
415 	return (dev & MAXMINOR);
416 }
417 
418 int
419 umajor(dev_t dev)
420 {
421 	return ((dev & ~MAXMINOR) >> 8);
422 }
423 
424 static void
425 fini_cdevsw(struct cdevsw *devsw)
426 {
427 	struct cdevsw *gt;
428 
429 	if (devsw->d_gianttrick != NULL) {
430 		gt = devsw->d_gianttrick;
431 		memcpy(devsw, gt, sizeof *devsw);
432 		free(gt, M_DEVT);
433 		devsw->d_gianttrick = NULL;
434 	}
435 	devsw->d_flags &= ~D_INIT;
436 }
437 
438 static void
439 prep_cdevsw(struct cdevsw *devsw)
440 {
441 	struct cdevsw *dsw2;
442 
443 	if (devsw->d_flags & D_NEEDGIANT)
444 		dsw2 = malloc(sizeof *dsw2, M_DEVT, M_WAITOK);
445 	else
446 		dsw2 = NULL;
447 	dev_lock();
448 
449 	if (devsw->d_version != D_VERSION_01) {
450 		printf(
451 		    "WARNING: Device driver \"%s\" has wrong version %s\n",
452 		    devsw->d_name == NULL ? "???" : devsw->d_name,
453 		    "and is disabled.  Recompile KLD module.");
454 		devsw->d_open = dead_open;
455 		devsw->d_close = dead_close;
456 		devsw->d_read = dead_read;
457 		devsw->d_write = dead_write;
458 		devsw->d_ioctl = dead_ioctl;
459 		devsw->d_poll = dead_poll;
460 		devsw->d_mmap = dead_mmap;
461 		devsw->d_strategy = dead_strategy;
462 		devsw->d_dump = dead_dump;
463 		devsw->d_kqfilter = dead_kqfilter;
464 	}
465 
466 	if (devsw->d_flags & D_TTY) {
467 		if (devsw->d_ioctl == NULL)	devsw->d_ioctl = ttyioctl;
468 		if (devsw->d_read == NULL)	devsw->d_read = ttyread;
469 		if (devsw->d_write == NULL)	devsw->d_write = ttywrite;
470 		if (devsw->d_kqfilter == NULL)	devsw->d_kqfilter = ttykqfilter;
471 		if (devsw->d_poll == NULL)	devsw->d_poll = ttypoll;
472 	}
473 
474 	if (devsw->d_flags & D_NEEDGIANT) {
475 		if (devsw->d_gianttrick == NULL) {
476 			memcpy(dsw2, devsw, sizeof *dsw2);
477 			devsw->d_gianttrick = dsw2;
478 		} else
479 			free(dsw2, M_DEVT);
480 	}
481 
482 #define FIXUP(member, noop, giant) 				\
483 	do {							\
484 		if (devsw->member == NULL) {			\
485 			devsw->member = noop;			\
486 		} else if (devsw->d_flags & D_NEEDGIANT)	\
487 			devsw->member = giant;			\
488 		}						\
489 	while (0)
490 
491 	FIXUP(d_open,		null_open,	giant_open);
492 	FIXUP(d_fdopen,		NULL,		giant_fdopen);
493 	FIXUP(d_close,		null_close,	giant_close);
494 	FIXUP(d_read,		no_read,	giant_read);
495 	FIXUP(d_write,		no_write,	giant_write);
496 	FIXUP(d_ioctl,		no_ioctl,	giant_ioctl);
497 	FIXUP(d_poll,		no_poll,	giant_poll);
498 	FIXUP(d_mmap,		no_mmap,	giant_mmap);
499 	FIXUP(d_strategy,	no_strategy,	giant_strategy);
500 	FIXUP(d_kqfilter,	no_kqfilter,	giant_kqfilter);
501 
502 	if (devsw->d_dump == NULL)	devsw->d_dump = no_dump;
503 
504 	LIST_INIT(&devsw->d_devs);
505 
506 	devsw->d_flags |= D_INIT;
507 
508 	dev_unlock();
509 }
510 
511 static struct cdev *
512 make_dev_credv(struct cdevsw *devsw, int minornr, struct ucred *cr, uid_t uid,
513     gid_t gid, int mode, const char *fmt, va_list ap)
514 {
515 	struct cdev *dev;
516 	int i;
517 
518 	KASSERT((minornr & ~MAXMINOR) == 0,
519 	    ("Invalid minor (0x%x) in make_dev", minornr));
520 
521 	if (!(devsw->d_flags & D_INIT))
522 		prep_cdevsw(devsw);
523 	dev = devfs_alloc();
524 	dev_lock();
525 	dev = newdev(devsw, minornr, dev);
526 	if (dev->si_flags & SI_CHEAPCLONE &&
527 	    dev->si_flags & SI_NAMED) {
528 		/*
529 		 * This is allowed as it removes races and generally
530 		 * simplifies cloning devices.
531 		 * XXX: still ??
532 		 */
533 		dev_unlock();
534 		return (dev);
535 	}
536 	KASSERT(!(dev->si_flags & SI_NAMED),
537 	    ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)",
538 	    devsw->d_name, minor(dev), devtoname(dev)));
539 
540 	i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap);
541 	if (i > (sizeof dev->__si_namebuf - 1)) {
542 		printf("WARNING: Device name truncated! (%s)\n",
543 		    dev->__si_namebuf);
544 	}
545 
546 	dev->si_flags |= SI_NAMED;
547 	if (cr != NULL)
548 		dev->si_cred = crhold(cr);
549 	else
550 		dev->si_cred = NULL;
551 	dev->si_uid = uid;
552 	dev->si_gid = gid;
553 	dev->si_mode = mode;
554 
555 	devfs_create(dev);
556 	dev_unlock();
557 	return (dev);
558 }
559 
560 struct cdev *
561 make_dev(struct cdevsw *devsw, int minornr, uid_t uid, gid_t gid, int mode,
562     const char *fmt, ...)
563 {
564 	struct cdev *dev;
565 	va_list ap;
566 
567 	va_start(ap, fmt);
568 	dev = make_dev_credv(devsw, minornr, NULL, uid, gid, mode, fmt, ap);
569 	va_end(ap);
570 	return (dev);
571 }
572 
573 struct cdev *
574 make_dev_cred(struct cdevsw *devsw, int minornr, struct ucred *cr, uid_t uid,
575     gid_t gid, int mode, const char *fmt, ...)
576 {
577 	struct cdev *dev;
578 	va_list ap;
579 
580 	va_start(ap, fmt);
581 	dev = make_dev_credv(devsw, minornr, cr, uid, gid, mode, fmt, ap);
582 	va_end(ap);
583 
584 	return (dev);
585 }
586 
587 static void
588 dev_dependsl(struct cdev *pdev, struct cdev *cdev)
589 {
590 
591 	cdev->si_parent = pdev;
592 	cdev->si_flags |= SI_CHILD;
593 	LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings);
594 }
595 
596 
597 void
598 dev_depends(struct cdev *pdev, struct cdev *cdev)
599 {
600 
601 	dev_lock();
602 	dev_dependsl(pdev, cdev);
603 	dev_unlock();
604 }
605 
606 struct cdev *
607 make_dev_alias(struct cdev *pdev, const char *fmt, ...)
608 {
609 	struct cdev *dev;
610 	va_list ap;
611 	int i;
612 
613 	dev = devfs_alloc();
614 	dev_lock();
615 	dev->si_flags |= SI_ALIAS;
616 	dev->si_flags |= SI_NAMED;
617 	va_start(ap, fmt);
618 	i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap);
619 	if (i > (sizeof dev->__si_namebuf - 1)) {
620 		printf("WARNING: Device name truncated! (%s)\n",
621 		    dev->__si_namebuf);
622 	}
623 	va_end(ap);
624 
625 	devfs_create(dev);
626 	dev_unlock();
627 	dev_depends(pdev, dev);
628 	return (dev);
629 }
630 
631 static void
632 destroy_devl(struct cdev *dev)
633 {
634 	struct cdevsw *csw;
635 
636 	mtx_assert(&devmtx, MA_OWNED);
637 	KASSERT(dev->si_flags & SI_NAMED,
638 	    ("WARNING: Driver mistake: destroy_dev on %d\n", minor(dev)));
639 
640 	devfs_destroy(dev);
641 
642 	/* Remove name marking */
643 	dev->si_flags &= ~SI_NAMED;
644 
645 	/* If we are a child, remove us from the parents list */
646 	if (dev->si_flags & SI_CHILD) {
647 		LIST_REMOVE(dev, si_siblings);
648 		dev->si_flags &= ~SI_CHILD;
649 	}
650 
651 	/* Kill our children */
652 	while (!LIST_EMPTY(&dev->si_children))
653 		destroy_devl(LIST_FIRST(&dev->si_children));
654 
655 	/* Remove from clone list */
656 	if (dev->si_flags & SI_CLONELIST) {
657 		LIST_REMOVE(dev, si_clone);
658 		dev->si_flags &= ~SI_CLONELIST;
659 	}
660 
661 	csw = dev->si_devsw;
662 	dev->si_devsw = NULL;	/* already NULL for SI_ALIAS */
663 	while (csw != NULL && csw->d_purge != NULL && dev->si_threadcount) {
664 		csw->d_purge(dev);
665 		msleep(csw, &devmtx, PRIBIO, "devprg", hz/10);
666 		if (dev->si_threadcount)
667 			printf("Still %lu threads in %s\n",
668 			    dev->si_threadcount, devtoname(dev));
669 	}
670 
671 	dev->si_drv1 = 0;
672 	dev->si_drv2 = 0;
673 	bzero(&dev->__si_u, sizeof(dev->__si_u));
674 
675 	if (!(dev->si_flags & SI_ALIAS)) {
676 		/* Remove from cdevsw list */
677 		LIST_REMOVE(dev, si_list);
678 
679 		/* If cdevsw has no more struct cdev *'s, clean it */
680 		if (LIST_EMPTY(&csw->d_devs))
681 			fini_cdevsw(csw);
682 	}
683 	dev->si_flags &= ~SI_ALIAS;
684 
685 	if (dev->si_refcount > 0) {
686 		LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list);
687 	} else {
688 		devfs_free(dev);
689 	}
690 }
691 
692 void
693 destroy_dev(struct cdev *dev)
694 {
695 
696 	dev_lock();
697 	destroy_devl(dev);
698 	dev_unlock();
699 }
700 
701 const char *
702 devtoname(struct cdev *dev)
703 {
704 	char *p;
705 	struct cdevsw *csw;
706 	int mynor;
707 
708 	if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
709 		p = dev->si_name;
710 		csw = dev_refthread(dev);
711 		if (csw != NULL) {
712 			sprintf(p, "(%s)", csw->d_name);
713 			dev_relthread(dev);
714 		}
715 		p += strlen(p);
716 		mynor = minor(dev);
717 		if (mynor < 0 || mynor > 255)
718 			sprintf(p, "/%#x", (u_int)mynor);
719 		else
720 			sprintf(p, "/%d", mynor);
721 	}
722 	return (dev->si_name);
723 }
724 
725 int
726 dev_stdclone(char *name, char **namep, const char *stem, int *unit)
727 {
728 	int u, i;
729 
730 	i = strlen(stem);
731 	if (bcmp(stem, name, i) != 0)
732 		return (0);
733 	if (!isdigit(name[i]))
734 		return (0);
735 	u = 0;
736 	if (name[i] == '0' && isdigit(name[i+1]))
737 		return (0);
738 	while (isdigit(name[i])) {
739 		u *= 10;
740 		u += name[i++] - '0';
741 	}
742 	if (u > 0xffffff)
743 		return (0);
744 	*unit = u;
745 	if (namep)
746 		*namep = &name[i];
747 	if (name[i])
748 		return (2);
749 	return (1);
750 }
751 
752 /*
753  * Helper functions for cloning device drivers.
754  *
755  * The objective here is to make it unnecessary for the device drivers to
756  * use rman or similar to manage their unit number space.  Due to the way
757  * we do "on-demand" devices, using rman or other "private" methods
758  * will be very tricky to lock down properly once we lock down this file.
759  *
760  * Instead we give the drivers these routines which puts the struct cdev *'s
761  * that are to be managed on their own list, and gives the driver the ability
762  * to ask for the first free unit number or a given specified unit number.
763  *
764  * In addition these routines support paired devices (pty, nmdm and similar)
765  * by respecting a number of "flag" bits in the minor number.
766  *
767  */
768 
769 struct clonedevs {
770 	LIST_HEAD(,cdev)	head;
771 };
772 
773 void
774 clone_setup(struct clonedevs **cdp)
775 {
776 
777 	*cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO);
778 	LIST_INIT(&(*cdp)->head);
779 }
780 
781 int
782 clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up, struct cdev **dp, u_int extra)
783 {
784 	struct clonedevs *cd;
785 	struct cdev *dev, *ndev, *dl, *de;
786 	int unit, low, u;
787 
788 	KASSERT(*cdp != NULL,
789 	    ("clone_setup() not called in driver \"%s\"", csw->d_name));
790 	KASSERT(!(extra & CLONE_UNITMASK),
791 	    ("Illegal extra bits (0x%x) in clone_create", extra));
792 	KASSERT(*up <= CLONE_UNITMASK,
793 	    ("Too high unit (0x%x) in clone_create", *up));
794 
795 	if (!(csw->d_flags & D_INIT))
796 		prep_cdevsw(csw);
797 
798 	/*
799 	 * Search the list for a lot of things in one go:
800 	 *   A preexisting match is returned immediately.
801 	 *   The lowest free unit number if we are passed -1, and the place
802 	 *	 in the list where we should insert that new element.
803 	 *   The place to insert a specified unit number, if applicable
804 	 *       the end of the list.
805 	 */
806 	unit = *up;
807 	ndev = devfs_alloc();
808 	dev_lock();
809 	low = extra;
810 	de = dl = NULL;
811 	cd = *cdp;
812 	LIST_FOREACH(dev, &cd->head, si_clone) {
813 		KASSERT(dev->si_flags & SI_CLONELIST,
814 		    ("Dev %p(%s) should be on clonelist", dev, dev->si_name));
815 		u = dev2unit(dev);
816 		if (u == (unit | extra)) {
817 			*dp = dev;
818 			devfs_free(ndev);
819 			dev_unlock();
820 			return (0);
821 		}
822 		if (unit == -1 && u == low) {
823 			low++;
824 			de = dev;
825 			continue;
826 		} else if (u < (unit | extra)) {
827 			de = dev;
828 			continue;
829 		} else if (u > (unit | extra)) {
830 			dl = dev;
831 			break;
832 		}
833 	}
834 	if (unit == -1)
835 		unit = low & CLONE_UNITMASK;
836 	dev = newdev(csw, unit2minor(unit | extra), ndev);
837 	if (dev->si_flags & SI_CLONELIST) {
838 		printf("dev %p (%s) is on clonelist\n", dev, dev->si_name);
839 		printf("unit=%d, low=%d, extra=0x%x\n", unit, low, extra);
840 		LIST_FOREACH(dev, &cd->head, si_clone) {
841 			printf("\t%p %s\n", dev, dev->si_name);
842 		}
843 		panic("foo");
844 	}
845 	KASSERT(!(dev->si_flags & SI_CLONELIST),
846 	    ("Dev %p(%s) should not be on clonelist", dev, dev->si_name));
847 	if (dl != NULL)
848 		LIST_INSERT_BEFORE(dl, dev, si_clone);
849 	else if (de != NULL)
850 		LIST_INSERT_AFTER(de, dev, si_clone);
851 	else
852 		LIST_INSERT_HEAD(&cd->head, dev, si_clone);
853 	dev->si_flags |= SI_CLONELIST;
854 	*up = unit;
855 	dev_unlock();
856 	return (1);
857 }
858 
859 /*
860  * Kill everything still on the list.  The driver should already have
861  * disposed of any softc hung of the struct cdev *'s at this time.
862  */
863 void
864 clone_cleanup(struct clonedevs **cdp)
865 {
866 	struct cdev *dev, *tdev;
867 	struct clonedevs *cd;
868 
869 	cd = *cdp;
870 	if (cd == NULL)
871 		return;
872 	dev_lock();
873 	LIST_FOREACH_SAFE(dev, &cd->head, si_clone, tdev) {
874 		KASSERT(dev->si_flags & SI_CLONELIST,
875 		    ("Dev %p(%s) should be on clonelist", dev, dev->si_name));
876 		KASSERT(dev->si_flags & SI_NAMED,
877 		    ("Driver has goofed in cloning underways udev %x", dev->si_drv0));
878 		destroy_devl(dev);
879 	}
880 	dev_unlock();
881 	free(cd, M_DEVBUF);
882 	*cdp = NULL;
883 }
884