kern_conf.c revision 53006
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 53006 1999-11-08 08:10:00Z peter $
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
77/*
78 * Routine to convert from character to block device number.
79 *
80 * A minimal stub routine can always return NODEV.
81 */
82dev_t
83chrtoblk(dev_t dev)
84{
85	struct cdevsw *cd;
86
87	if((cd = devsw(dev)) != NULL) {
88          if (cd->d_bmaj != -1)
89	    return(makebdev(cd->d_bmaj,minor(dev)));
90	}
91	return(NODEV);
92}
93
94struct cdevsw *
95devsw(dev_t dev)
96{
97	if (dev->si_devsw)
98		return (dev->si_devsw);
99        return(cdevsw[major(dev)]);
100}
101
102/*
103 *  Add a cdevsw entry
104 */
105
106int
107cdevsw_add(struct cdevsw *newentry)
108{
109	int i;
110	static int setup;
111
112	if (!setup) {
113		for (i = 0; i < NUMCDEVSW; i++)
114			if (!bmaj2cmaj[i])
115				bmaj2cmaj[i] = 254;
116		setup++;
117	}
118
119	if (newentry->d_maj < 0 || newentry->d_maj >= NUMCDEVSW) {
120		printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
121		    newentry->d_name, newentry->d_maj);
122		return (EINVAL);
123	}
124	if (newentry->d_bmaj >= NUMCDEVSW) {
125		printf("%s: ERROR: driver has bogus cdevsw->d_bmaj = %d\n",
126		    newentry->d_name, newentry->d_bmaj);
127		return (EINVAL);
128	}
129	if (newentry->d_bmaj >= 0 && (newentry->d_flags & D_DISK) == 0) {
130		printf("ERROR: \"%s\" bmaj but is not a disk\n",
131		    newentry->d_name);
132		return (EINVAL);
133	}
134
135	if (cdevsw[newentry->d_maj]) {
136		printf("WARNING: \"%s\" is usurping \"%s\"'s cdevsw[]\n",
137		    newentry->d_name, cdevsw[newentry->d_maj]->d_name);
138	}
139
140	cdevsw[newentry->d_maj] = newentry;
141
142	if (newentry->d_bmaj < 0)
143		return (0);
144
145	if (bmaj2cmaj[newentry->d_bmaj] != 254) {
146		printf("WARNING: \"%s\" is usurping \"%s\"'s bmaj\n",
147		    newentry->d_name,
148		    cdevsw[bmaj2cmaj[newentry->d_bmaj]]->d_name);
149	}
150	bmaj2cmaj[newentry->d_bmaj] = newentry->d_maj;
151	return (0);
152}
153
154/*
155 *  Remove a cdevsw entry
156 */
157
158int
159cdevsw_remove(struct cdevsw *oldentry)
160{
161	if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) {
162		printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
163		    oldentry->d_name, oldentry->d_maj);
164		return EINVAL;
165	}
166
167	cdevsw[oldentry->d_maj] = NULL;
168
169	if (oldentry->d_bmaj >= 0 && oldentry->d_bmaj < NUMCDEVSW)
170		bmaj2cmaj[oldentry->d_bmaj] = 254;
171
172	return 0;
173}
174
175/*
176 * dev_t and u_dev_t primitives
177 */
178
179int
180major(dev_t x)
181{
182	if (x == NODEV)
183		return NOUDEV;
184	return((x->si_udev >> 8) & 0xff);
185}
186
187int
188minor(dev_t x)
189{
190	if (x == NODEV)
191		return NOUDEV;
192	return(x->si_udev & 0xffff00ff);
193}
194
195int
196lminor(dev_t x)
197{
198	int i;
199
200	if (x == NODEV)
201		return NOUDEV;
202	i = minor(x);
203	return ((i & 0xff) | (i >> 8));
204}
205
206dev_t
207makebdev(int x, int y)
208{
209	return (makedev(bmaj2cmaj[x], y));
210}
211
212dev_t
213makedev(int x, int y)
214{
215	struct specinfo *si;
216	udev_t	udev;
217	int hash;
218	static int stashed;
219
220	udev = (x << 8) | y;
221	hash = udev % DEVT_HASH;
222	LIST_FOREACH(si, &dev_hash[hash], si_hash) {
223		if (si->si_udev == udev)
224			return (si);
225	}
226	if (stashed >= DEVT_STASH) {
227		MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT,
228		    M_USE_RESERVE);
229		bzero(si, sizeof(*si));
230	} else if (LIST_FIRST(&dev_free)) {
231		si = LIST_FIRST(&dev_free);
232		LIST_REMOVE(si, si_hash);
233	} else {
234		si = devt_stash + stashed++;
235		si->si_flags |= SI_STASHED;
236	}
237	si->si_udev = udev;
238	LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash);
239        return (si);
240}
241
242void
243freedev(dev_t dev)
244{
245	int hash;
246
247	if (!free_devt)
248		return;
249	if (SLIST_FIRST(&dev->si_hlist))
250		return;
251	if (dev->si_devsw || dev->si_drv1 || dev->si_drv2)
252		return;
253	hash = dev->si_udev % DEVT_HASH;
254	LIST_REMOVE(dev, si_hash);
255	if (dev->si_flags & SI_STASHED) {
256		bzero(dev, sizeof(*dev));
257		LIST_INSERT_HEAD(&dev_free, dev, si_hash);
258	} else {
259		FREE(dev, M_DEVT);
260	}
261}
262
263udev_t
264dev2udev(dev_t x)
265{
266	if (x == NODEV)
267		return NOUDEV;
268	return (x->si_udev);
269}
270
271udev_t
272dev2budev(dev_t x)
273{
274	if (x == NODEV)
275		return NOUDEV;
276	else
277		return makeudev(devsw(x)->d_bmaj, minor(x));
278}
279
280dev_t
281udev2dev(udev_t x, int b)
282{
283	switch (b) {
284		case 0:
285			return makedev(umajor(x), uminor(x));
286		case 1:
287			return makebdev(umajor(x), uminor(x));
288		default:
289			Debugger("udev2dev(...,X)");
290			return NODEV;
291	}
292}
293
294int
295uminor(udev_t dev)
296{
297	return(dev & 0xffff00ff);
298}
299
300int
301umajor(udev_t dev)
302{
303	return((dev & 0xff00) >> 8);
304}
305
306udev_t
307makeudev(int x, int y)
308{
309        return ((x << 8) | y);
310}
311
312dev_t
313make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, char *fmt, ...)
314{
315	dev_t	dev;
316	va_list ap;
317	int i;
318
319	dev = makedev(devsw->d_maj, minor);
320	va_start(ap, fmt);
321	i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
322	dev->si_name[i] = '\0';
323	va_end(ap);
324	dev->si_devsw = devsw;
325
326	if (devfs_create_hook)
327		devfs_create_hook(dev, uid, gid, perms);
328	return (dev);
329}
330
331void
332destroy_dev(dev_t dev)
333{
334	if (devfs_remove_hook)
335		devfs_remove_hook(dev);
336	dev->si_drv1 = 0;
337	dev->si_drv2 = 0;
338	dev->si_devsw = 0;
339	freedev(dev);
340}
341
342const char *
343devtoname(dev_t dev)
344{
345	char *p;
346	int mynor;
347
348	if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
349		p = dev->si_name;
350		if (devsw(dev))
351			sprintf(p, "#%s/", devsw(dev)->d_name);
352		else
353			sprintf(p, "#%d/", major(dev));
354		p += strlen(p);
355		mynor = minor(dev);
356		if (mynor < 0 || mynor > 255)
357			sprintf(p, "%#x", (u_int)mynor);
358		else
359			sprintf(p, "%d", mynor);
360	}
361	return (dev->si_name);
362}
363