kern_conf.c revision 60833
1/*-
2 * Parts Copyright (c) 1995 Terrence R. Lambert
3 * Copyright (c) 1995 Julian R. Elischer
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
19 *    permission.
20 *
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
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD: head/sys/kern/kern_conf.c 60833 2000-05-23 20:41:01Z jake $
34 */
35
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>
42#include <sys/conf.h>
43#include <sys/vnode.h>
44#include <sys/queue.h>
45#include <machine/stdarg.h>
46
47#define cdevsw_ALLOCSTART	(NUMCDEVSW/2)
48
49struct cdevsw 	*cdevsw[NUMCDEVSW];
50
51static int	bmaj2cmaj[NUMCDEVSW];
52
53MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage");
54
55/*
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
58 * best.
59 */
60#define DEVT_HASH 83
61
62/* The number of dev_t's we can create before malloc(9) kick in.  */
63#define DEVT_STASH 50
64
65static struct specinfo devt_stash[DEVT_STASH];
66
67static LIST_HEAD(, struct specinfo) dev_hash[DEVT_HASH];
68
69static LIST_HEAD(, struct specinfo) dev_free;
70
71devfs_create_t *devfs_create_hook;
72devfs_remove_t *devfs_remove_hook;
73
74static int free_devt;
75SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, "");
76
77struct cdevsw *
78devsw(dev_t dev)
79{
80	if (dev->si_devsw)
81		return (dev->si_devsw);
82        return(cdevsw[major(dev)]);
83}
84
85/*
86 *  Add a cdevsw entry
87 */
88
89int
90cdevsw_add(struct cdevsw *newentry)
91{
92	int i;
93	static int setup;
94
95	if (!setup) {
96		for (i = 0; i < NUMCDEVSW; i++)
97			if (!bmaj2cmaj[i])
98				bmaj2cmaj[i] = 254;
99		setup++;
100	}
101
102	if (newentry->d_maj < 0 || newentry->d_maj >= NUMCDEVSW) {
103		printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
104		    newentry->d_name, newentry->d_maj);
105		return (EINVAL);
106	}
107	if (newentry->d_bmaj >= NUMCDEVSW) {
108		printf("%s: ERROR: driver has bogus cdevsw->d_bmaj = %d\n",
109		    newentry->d_name, newentry->d_bmaj);
110		return (EINVAL);
111	}
112	if (newentry->d_bmaj >= 0 && (newentry->d_flags & D_DISK) == 0) {
113		printf("ERROR: \"%s\" bmaj but is not a disk\n",
114		    newentry->d_name);
115		return (EINVAL);
116	}
117
118	if (cdevsw[newentry->d_maj]) {
119		printf("WARNING: \"%s\" is usurping \"%s\"'s cdevsw[]\n",
120		    newentry->d_name, cdevsw[newentry->d_maj]->d_name);
121	}
122
123	cdevsw[newentry->d_maj] = newentry;
124
125	if (newentry->d_bmaj < 0)
126		return (0);
127
128	if (bmaj2cmaj[newentry->d_bmaj] != 254) {
129		printf("WARNING: \"%s\" is usurping \"%s\"'s bmaj\n",
130		    newentry->d_name,
131		    cdevsw[bmaj2cmaj[newentry->d_bmaj]]->d_name);
132	}
133	bmaj2cmaj[newentry->d_bmaj] = newentry->d_maj;
134	return (0);
135}
136
137/*
138 *  Remove a cdevsw entry
139 */
140
141int
142cdevsw_remove(struct cdevsw *oldentry)
143{
144	if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) {
145		printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
146		    oldentry->d_name, oldentry->d_maj);
147		return EINVAL;
148	}
149
150	cdevsw[oldentry->d_maj] = NULL;
151
152	if (oldentry->d_bmaj >= 0 && oldentry->d_bmaj < NUMCDEVSW)
153		bmaj2cmaj[oldentry->d_bmaj] = 254;
154
155	return 0;
156}
157
158/*
159 * dev_t and u_dev_t primitives
160 */
161
162int
163major(dev_t x)
164{
165	if (x == NODEV)
166		return NOUDEV;
167	return((x->si_udev >> 8) & 0xff);
168}
169
170int
171minor(dev_t x)
172{
173	if (x == NODEV)
174		return NOUDEV;
175	return(x->si_udev & 0xffff00ff);
176}
177
178int
179lminor(dev_t x)
180{
181	int i;
182
183	if (x == NODEV)
184		return NOUDEV;
185	i = minor(x);
186	return ((i & 0xff) | (i >> 8));
187}
188
189dev_t
190makebdev(int x, int y)
191{
192
193	if (x == umajor(NOUDEV) && y == uminor(NOUDEV))
194		Debugger("makebdev of NOUDEV");
195	return (makedev(bmaj2cmaj[x], y));
196}
197
198dev_t
199makedev(int x, int y)
200{
201	struct specinfo *si;
202	udev_t	udev;
203	int hash;
204	static int stashed;
205
206	if (x == umajor(NOUDEV) && y == uminor(NOUDEV))
207		Debugger("makedev of NOUDEV");
208	udev = (x << 8) | y;
209	hash = udev % DEVT_HASH;
210	LIST_FOREACH(si, &dev_hash[hash], si_hash) {
211		if (si->si_udev == udev)
212			return (si);
213	}
214	if (stashed >= DEVT_STASH) {
215		MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT,
216		    M_USE_RESERVE);
217		bzero(si, sizeof(*si));
218	} else if (LIST_FIRST(&dev_free)) {
219		si = LIST_FIRST(&dev_free);
220		LIST_REMOVE(si, si_hash);
221	} else {
222		si = devt_stash + stashed++;
223		si->si_flags |= SI_STASHED;
224	}
225	si->si_udev = udev;
226	LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash);
227        return (si);
228}
229
230void
231freedev(dev_t dev)
232{
233	int hash;
234
235	if (!free_devt)
236		return;
237	if (SLIST_FIRST(&dev->si_hlist))
238		return;
239	if (dev->si_devsw || dev->si_drv1 || dev->si_drv2)
240		return;
241	hash = dev->si_udev % DEVT_HASH;
242	LIST_REMOVE(dev, si_hash);
243	if (dev->si_flags & SI_STASHED) {
244		bzero(dev, sizeof(*dev));
245		LIST_INSERT_HEAD(&dev_free, dev, si_hash);
246	} else {
247		FREE(dev, M_DEVT);
248	}
249}
250
251udev_t
252dev2udev(dev_t x)
253{
254	if (x == NODEV)
255		return NOUDEV;
256	return (x->si_udev);
257}
258
259dev_t
260udev2dev(udev_t x, int b)
261{
262
263	if (x == NOUDEV)
264		return (NODEV);
265	switch (b) {
266		case 0:
267			return makedev(umajor(x), uminor(x));
268		case 1:
269			return makebdev(umajor(x), uminor(x));
270		default:
271			Debugger("udev2dev(...,X)");
272			return NODEV;
273	}
274}
275
276int
277uminor(udev_t dev)
278{
279	return(dev & 0xffff00ff);
280}
281
282int
283umajor(udev_t dev)
284{
285	return((dev & 0xff00) >> 8);
286}
287
288udev_t
289makeudev(int x, int y)
290{
291        return ((x << 8) | y);
292}
293
294dev_t
295make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, char *fmt, ...)
296{
297	dev_t	dev;
298	va_list ap;
299	int i;
300
301	dev = makedev(devsw->d_maj, minor);
302	va_start(ap, fmt);
303	i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
304	dev->si_name[i] = '\0';
305	va_end(ap);
306	dev->si_devsw = devsw;
307
308	if (devfs_create_hook)
309		devfs_create_hook(dev, uid, gid, perms);
310	return (dev);
311}
312
313void
314destroy_dev(dev_t dev)
315{
316	if (devfs_remove_hook)
317		devfs_remove_hook(dev);
318	dev->si_drv1 = 0;
319	dev->si_drv2 = 0;
320	dev->si_devsw = 0;
321	freedev(dev);
322}
323
324const char *
325devtoname(dev_t dev)
326{
327	char *p;
328	int mynor;
329
330	if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
331		p = dev->si_name;
332		if (devsw(dev))
333			sprintf(p, "#%s/", devsw(dev)->d_name);
334		else
335			sprintf(p, "#%d/", major(dev));
336		p += strlen(p);
337		mynor = minor(dev);
338		if (mynor < 0 || mynor > 255)
339			sprintf(p, "%#x", (u_int)mynor);
340		else
341			sprintf(p, "%d", mynor);
342	}
343	return (dev->si_name);
344}
345