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.2 2003/06/17 04:28:41 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 <machine/stdarg.h>
48 #define cdevsw_ALLOCSTART (NUMCDEVSW/2)
50 struct cdevsw *cdevsw[NUMCDEVSW];
52 static int bmaj2cmaj[NUMCDEVSW];
54 MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage");
57 * This is the number of hash-buckets. Experiements with 'real-life'
58 * udev_t's show that a prime halfway between two powers of two works
63 /* The number of dev_t's we can create before malloc(9) kick in. */
66 static struct specinfo devt_stash[DEVT_STASH];
68 static LIST_HEAD(, specinfo) dev_hash[DEVT_HASH];
70 static LIST_HEAD(, specinfo) dev_free;
73 SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, "");
79 return (dev->si_devsw);
80 return(cdevsw[major(dev)]);
84 compile_devsw(struct cdevsw *devsw)
86 if (devsw->d_open == NULL)
87 devsw->d_open = noopen;
88 if (devsw->d_close == NULL)
89 devsw->d_close = noclose;
90 if (devsw->d_read == NULL)
91 devsw->d_read = noread;
92 if (devsw->d_write == NULL)
93 devsw->d_write = nowrite;
94 if (devsw->d_ioctl == NULL)
95 devsw->d_ioctl = noioctl;
96 if (devsw->d_poll == NULL)
97 devsw->d_poll = nopoll;
98 if (devsw->d_mmap == NULL)
99 devsw->d_mmap = nommap;
100 if (devsw->d_strategy == NULL)
101 devsw->d_strategy = nostrategy;
102 if (devsw->d_dump == NULL)
103 devsw->d_dump = nodump;
104 if (devsw->d_psize == NULL)
105 devsw->d_psize = nopsize;
106 if (devsw->d_kqfilter == NULL)
107 devsw->d_kqfilter = nokqfilter;
115 cdevsw_add(struct cdevsw *newentry)
121 for (i = 0; i < NUMCDEVSW; i++)
127 compile_devsw(newentry);
128 if (newentry->d_maj < 0 || newentry->d_maj >= NUMCDEVSW) {
129 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
130 newentry->d_name, newentry->d_maj);
133 if (newentry->d_bmaj >= NUMCDEVSW) {
134 printf("%s: ERROR: driver has bogus cdevsw->d_bmaj = %d\n",
135 newentry->d_name, newentry->d_bmaj);
138 if (newentry->d_bmaj >= 0 && (newentry->d_flags & D_DISK) == 0) {
139 printf("ERROR: \"%s\" bmaj but is not a disk\n",
144 if (cdevsw[newentry->d_maj]) {
145 printf("WARNING: \"%s\" is usurping \"%s\"'s cdevsw[]\n",
146 newentry->d_name, cdevsw[newentry->d_maj]->d_name);
149 cdevsw[newentry->d_maj] = newentry;
151 if (newentry->d_bmaj < 0)
154 if (bmaj2cmaj[newentry->d_bmaj] != 254) {
155 printf("WARNING: \"%s\" is usurping \"%s\"'s bmaj\n",
157 cdevsw[bmaj2cmaj[newentry->d_bmaj]]->d_name);
159 bmaj2cmaj[newentry->d_bmaj] = newentry->d_maj;
164 * Remove a cdevsw entry
168 cdevsw_remove(struct cdevsw *oldentry)
170 if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) {
171 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
172 oldentry->d_name, oldentry->d_maj);
176 cdevsw[oldentry->d_maj] = NULL;
178 if (oldentry->d_bmaj >= 0 && oldentry->d_bmaj < NUMCDEVSW)
179 bmaj2cmaj[oldentry->d_bmaj] = 254;
185 * dev_t and u_dev_t primitives
193 return((x->si_udev >> 8) & 0xff);
201 return(x->si_udev & 0xffff00ff);
212 return ((i & 0xff) | (i >> 8));
216 makebdev(int x, int y)
219 if (x == umajor(NOUDEV) && y == uminor(NOUDEV))
220 Debugger("makebdev of NOUDEV");
221 return (makedev(bmaj2cmaj[x], y));
225 makedev(int x, int y)
232 if (x == umajor(NOUDEV) && y == uminor(NOUDEV))
233 Debugger("makedev of NOUDEV");
235 hash = udev % DEVT_HASH;
236 LIST_FOREACH(si, &dev_hash[hash], si_hash) {
237 if (si->si_udev == udev)
240 if (stashed >= DEVT_STASH) {
241 MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT,
243 bzero(si, sizeof(*si));
244 } else if (LIST_FIRST(&dev_free)) {
245 si = LIST_FIRST(&dev_free);
246 LIST_REMOVE(si, si_hash);
248 si = devt_stash + stashed++;
249 si->si_flags |= SI_STASHED;
252 LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash);
263 if (SLIST_FIRST(&dev->si_hlist))
265 if (dev->si_devsw || dev->si_drv1 || dev->si_drv2)
267 hash = dev->si_udev % DEVT_HASH;
268 LIST_REMOVE(dev, si_hash);
269 if (dev->si_flags & SI_STASHED) {
270 bzero(dev, sizeof(*dev));
271 LIST_INSERT_HEAD(&dev_free, dev, si_hash);
286 udev2dev(udev_t x, int b)
293 return makedev(umajor(x), uminor(x));
295 return makebdev(umajor(x), uminor(x));
297 Debugger("udev2dev(...,X)");
305 return(dev & 0xffff00ff);
311 return((dev & 0xff00) >> 8);
315 makeudev(int x, int y)
317 return ((x << 8) | y);
321 make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, const char *fmt, ...)
327 compile_devsw(devsw);
328 dev = makedev(devsw->d_maj, minor);
330 i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
331 dev->si_name[i] = '\0';
333 dev->si_devsw = devsw;
339 destroy_dev(dev_t dev)
353 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
356 sprintf(p, "#%s/", devsw(dev)->d_name);
358 sprintf(p, "#%d/", major(dev));
361 if (mynor < 0 || mynor > 255)
362 sprintf(p, "%#x", (u_int)mynor);
364 sprintf(p, "%d", mynor);
366 return (dev->si_name);