2 * Parts Copyright (c) 1995 Terrence R. Lambert
3 * Copyright (c) 1995 Julian R. Elischer
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Terrence R. Lambert.
17 * 4. The name Terrence R. Lambert may not be used to endorse or promote
18 * products derived from this software without specific prior written
21 * THIS SOFTWARE IS PROVIDED BY Julian R. Elischer ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/sys/kern/kern_conf.c,v 1.73.2.3 2003/03/10 02:18:25 imp Exp $
34 * $DragonFly: src/sys/kern/kern_conf.c,v 1.4 2003/07/22 17:03:33 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
40 #include <sys/systm.h>
41 #include <sys/module.h>
42 #include <sys/malloc.h>
44 #include <sys/vnode.h>
45 #include <sys/queue.h>
46 #include <sys/device.h>
47 #include <machine/stdarg.h>
49 #define cdevsw_ALLOCSTART (NUMCDEVSW/2)
51 MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage");
54 * This is the number of hash-buckets. Experiements with 'real-life'
55 * udev_t's show that a prime halfway between two powers of two works
60 /* The number of dev_t's we can create before malloc(9) kick in. */
63 static struct specinfo devt_stash[DEVT_STASH];
65 static LIST_HEAD(, specinfo) dev_hash[DEVT_HASH];
67 static LIST_HEAD(, specinfo) dev_free;
70 SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, "");
73 * dev_t and u_dev_t primitives
81 return((x->si_udev >> 8) & 0xff);
89 return(x->si_udev & 0xffff00ff);
100 return ((i & 0xff) | (i >> 8));
104 makedev(int x, int y)
111 if (x == umajor(NOUDEV) && y == uminor(NOUDEV))
112 Debugger("makedev of NOUDEV");
114 hash = udev % DEVT_HASH;
115 LIST_FOREACH(si, &dev_hash[hash], si_hash) {
116 if (si->si_udev == udev)
119 if (stashed >= DEVT_STASH) {
120 MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT,
122 bzero(si, sizeof(*si));
123 } else if (LIST_FIRST(&dev_free)) {
124 si = LIST_FIRST(&dev_free);
125 LIST_REMOVE(si, si_hash);
127 si = devt_stash + stashed++;
128 si->si_flags |= SI_STASHED;
131 LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash);
142 if (SLIST_FIRST(&dev->si_hlist))
144 if (dev->si_devsw || dev->si_drv1 || dev->si_drv2)
146 hash = dev->si_udev % DEVT_HASH;
147 LIST_REMOVE(dev, si_hash);
148 if (dev->si_flags & SI_STASHED) {
149 bzero(dev, sizeof(*dev));
150 LIST_INSERT_HEAD(&dev_free, dev, si_hash);
165 udev2dev(udev_t x, int b)
172 return makedev(umajor(x), uminor(x));
174 printf("udev2dev: attempt to lookup block dev(%d)", x);
177 Debugger("udev2dev(...,X)");
185 return(dev & 0xffff00ff);
191 return((dev & 0xff00) >> 8);
195 makeudev(int x, int y)
197 return ((x << 8) | y);
201 make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, const char *fmt, ...)
207 compile_devsw(devsw);
208 dev = makedev(devsw->d_maj, minor);
210 i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
211 dev->si_name[i] = '\0';
213 dev->si_devsw = devsw;
219 destroy_dev(dev_t dev)
235 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
237 len = sizeof(dev->si_name);
238 if ((dname = dev_dname(dev)) != NULL)
239 snprintf(p, len, "#%s/", dname);
241 snprintf(p, len, "#%d/", major(dev));
245 if (mynor < 0 || mynor > 255)
246 snprintf(p, len, "%#x", (u_int)mynor);
248 snprintf(p, len, "%d", mynor);
250 return (dev->si_name);