kern_conf.c revision 53896
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 53896 1999-11-29 20:50:58Z phk $
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(, specinfo) dev_hash[DEVT_HASH];
68
69static LIST_HEAD(, 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	return (makedev(bmaj2cmaj[x], y));
193}
194
195dev_t
196makedev(int x, int y)
197{
198	struct specinfo *si;
199	udev_t	udev;
200	int hash;
201	static int stashed;
202
203	udev = (x << 8) | y;
204	hash = udev % DEVT_HASH;
205	LIST_FOREACH(si, &dev_hash[hash], si_hash) {
206		if (si->si_udev == udev)
207			return (si);
208	}
209	if (stashed >= DEVT_STASH) {
210		MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT,
211		    M_USE_RESERVE);
212		bzero(si, sizeof(*si));
213	} else if (LIST_FIRST(&dev_free)) {
214		si = LIST_FIRST(&dev_free);
215		LIST_REMOVE(si, si_hash);
216	} else {
217		si = devt_stash + stashed++;
218		si->si_flags |= SI_STASHED;
219	}
220	si->si_udev = udev;
221	LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash);
222        return (si);
223}
224
225void
226freedev(dev_t dev)
227{
228	int hash;
229
230	if (!free_devt)
231		return;
232	if (SLIST_FIRST(&dev->si_hlist))
233		return;
234	if (dev->si_devsw || dev->si_drv1 || dev->si_drv2)
235		return;
236	hash = dev->si_udev % DEVT_HASH;
237	LIST_REMOVE(dev, si_hash);
238	if (dev->si_flags & SI_STASHED) {
239		bzero(dev, sizeof(*dev));
240		LIST_INSERT_HEAD(&dev_free, dev, si_hash);
241	} else {
242		FREE(dev, M_DEVT);
243	}
244}
245
246udev_t
247dev2udev(dev_t x)
248{
249	if (x == NODEV)
250		return NOUDEV;
251	return (x->si_udev);
252}
253
254udev_t
255dev2budev(dev_t x)
256{
257	if (x == NODEV)
258		return NOUDEV;
259	else
260		return makeudev(devsw(x)->d_bmaj, minor(x));
261}
262
263dev_t
264udev2dev(udev_t x, int b)
265{
266	switch (b) {
267		case 0:
268			return makedev(umajor(x), uminor(x));
269		case 1:
270			return makebdev(umajor(x), uminor(x));
271		default:
272			Debugger("udev2dev(...,X)");
273			return NODEV;
274	}
275}
276
277int
278uminor(udev_t dev)
279{
280	return(dev & 0xffff00ff);
281}
282
283int
284umajor(udev_t dev)
285{
286	return((dev & 0xff00) >> 8);
287}
288
289udev_t
290makeudev(int x, int y)
291{
292        return ((x << 8) | y);
293}
294
295dev_t
296make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, char *fmt, ...)
297{
298	dev_t	dev;
299	va_list ap;
300	int i;
301
302	dev = makedev(devsw->d_maj, minor);
303	va_start(ap, fmt);
304	i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
305	dev->si_name[i] = '\0';
306	va_end(ap);
307	dev->si_devsw = devsw;
308
309	if (devfs_create_hook)
310		devfs_create_hook(dev, uid, gid, perms);
311	return (dev);
312}
313
314void
315destroy_dev(dev_t dev)
316{
317	if (devfs_remove_hook)
318		devfs_remove_hook(dev);
319	dev->si_drv1 = 0;
320	dev->si_drv2 = 0;
321	dev->si_devsw = 0;
322	freedev(dev);
323}
324
325const char *
326devtoname(dev_t dev)
327{
328	char *p;
329	int mynor;
330
331	if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
332		p = dev->si_name;
333		if (devsw(dev))
334			sprintf(p, "#%s/", devsw(dev)->d_name);
335		else
336			sprintf(p, "#%d/", major(dev));
337		p += strlen(p);
338		mynor = minor(dev);
339		if (mynor < 0 || mynor > 255)
340			sprintf(p, "%#x", (u_int)mynor);
341		else
342			sprintf(p, "%d", mynor);
343	}
344	return (dev->si_name);
345}
346