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 $
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
40 #include <sys/module.h>
41 #include <sys/malloc.h>
43 #include <sys/vnode.h>
44 #include <sys/queue.h>
45 #include <machine/stdarg.h>
47 #define cdevsw_ALLOCSTART (NUMCDEVSW/2)
49 struct cdevsw *cdevsw[NUMCDEVSW];
51 static int bmaj2cmaj[NUMCDEVSW];
53 MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage");
56 * This is the number of hash-buckets. Experiements with 'real-life'
57 * udev_t's show that a prime halfway between two powers of two works
62 /* The number of dev_t's we can create before malloc(9) kick in. */
65 static struct specinfo devt_stash[DEVT_STASH];
67 static LIST_HEAD(, specinfo) dev_hash[DEVT_HASH];
69 static LIST_HEAD(, specinfo) dev_free;
72 SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, "");
78 return (dev->si_devsw);
79 return(cdevsw[major(dev)]);
83 compile_devsw(struct cdevsw *devsw)
85 if (devsw->d_open == NULL)
86 devsw->d_open = noopen;
87 if (devsw->d_close == NULL)
88 devsw->d_close = noclose;
89 if (devsw->d_read == NULL)
90 devsw->d_read = noread;
91 if (devsw->d_write == NULL)
92 devsw->d_write = nowrite;
93 if (devsw->d_ioctl == NULL)
94 devsw->d_ioctl = noioctl;
95 if (devsw->d_poll == NULL)
96 devsw->d_poll = nopoll;
97 if (devsw->d_mmap == NULL)
98 devsw->d_mmap = nommap;
99 if (devsw->d_strategy == NULL)
100 devsw->d_strategy = nostrategy;
101 if (devsw->d_dump == NULL)
102 devsw->d_dump = nodump;
103 if (devsw->d_psize == NULL)
104 devsw->d_psize = nopsize;
105 if (devsw->d_kqfilter == NULL)
106 devsw->d_kqfilter = nokqfilter;
114 cdevsw_add(struct cdevsw *newentry)
120 for (i = 0; i < NUMCDEVSW; i++)
126 compile_devsw(newentry);
127 if (newentry->d_maj < 0 || newentry->d_maj >= NUMCDEVSW) {
128 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
129 newentry->d_name, newentry->d_maj);
132 if (newentry->d_bmaj >= NUMCDEVSW) {
133 printf("%s: ERROR: driver has bogus cdevsw->d_bmaj = %d\n",
134 newentry->d_name, newentry->d_bmaj);
137 if (newentry->d_bmaj >= 0 && (newentry->d_flags & D_DISK) == 0) {
138 printf("ERROR: \"%s\" bmaj but is not a disk\n",
143 if (cdevsw[newentry->d_maj]) {
144 printf("WARNING: \"%s\" is usurping \"%s\"'s cdevsw[]\n",
145 newentry->d_name, cdevsw[newentry->d_maj]->d_name);
148 cdevsw[newentry->d_maj] = newentry;
150 if (newentry->d_bmaj < 0)
153 if (bmaj2cmaj[newentry->d_bmaj] != 254) {
154 printf("WARNING: \"%s\" is usurping \"%s\"'s bmaj\n",
156 cdevsw[bmaj2cmaj[newentry->d_bmaj]]->d_name);
158 bmaj2cmaj[newentry->d_bmaj] = newentry->d_maj;
163 * Remove a cdevsw entry
167 cdevsw_remove(struct cdevsw *oldentry)
169 if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) {
170 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
171 oldentry->d_name, oldentry->d_maj);
175 cdevsw[oldentry->d_maj] = NULL;
177 if (oldentry->d_bmaj >= 0 && oldentry->d_bmaj < NUMCDEVSW)
178 bmaj2cmaj[oldentry->d_bmaj] = 254;
184 * dev_t and u_dev_t primitives
192 return((x->si_udev >> 8) & 0xff);
200 return(x->si_udev & 0xffff00ff);
211 return ((i & 0xff) | (i >> 8));
215 makebdev(int x, int y)
218 if (x == umajor(NOUDEV) && y == uminor(NOUDEV))
219 Debugger("makebdev of NOUDEV");
220 return (makedev(bmaj2cmaj[x], y));
224 makedev(int x, int y)
231 if (x == umajor(NOUDEV) && y == uminor(NOUDEV))
232 Debugger("makedev of NOUDEV");
234 hash = udev % DEVT_HASH;
235 LIST_FOREACH(si, &dev_hash[hash], si_hash) {
236 if (si->si_udev == udev)
239 if (stashed >= DEVT_STASH) {
240 MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT,
242 bzero(si, sizeof(*si));
243 } else if (LIST_FIRST(&dev_free)) {
244 si = LIST_FIRST(&dev_free);
245 LIST_REMOVE(si, si_hash);
247 si = devt_stash + stashed++;
248 si->si_flags |= SI_STASHED;
251 LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash);
262 if (SLIST_FIRST(&dev->si_hlist))
264 if (dev->si_devsw || dev->si_drv1 || dev->si_drv2)
266 hash = dev->si_udev % DEVT_HASH;
267 LIST_REMOVE(dev, si_hash);
268 if (dev->si_flags & SI_STASHED) {
269 bzero(dev, sizeof(*dev));
270 LIST_INSERT_HEAD(&dev_free, dev, si_hash);
285 udev2dev(udev_t x, int b)
292 return makedev(umajor(x), uminor(x));
294 return makebdev(umajor(x), uminor(x));
296 Debugger("udev2dev(...,X)");
304 return(dev & 0xffff00ff);
310 return((dev & 0xff00) >> 8);
314 makeudev(int x, int y)
316 return ((x << 8) | y);
320 make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, const char *fmt, ...)
326 compile_devsw(devsw);
327 dev = makedev(devsw->d_maj, minor);
329 i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
330 dev->si_name[i] = '\0';
332 dev->si_devsw = devsw;
338 destroy_dev(dev_t dev)
352 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
355 sprintf(p, "#%s/", devsw(dev)->d_name);
357 sprintf(p, "#%d/", major(dev));
360 if (mynor < 0 || mynor > 255)
361 sprintf(p, "%#x", (u_int)mynor);
363 sprintf(p, "%d", mynor);
365 return (dev->si_name);