]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_conf.c
This commit was generated by cvs2svn to compensate for changes in r55289,
[FreeBSD/FreeBSD.git] / sys / kern / kern_conf.c
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$
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
49 struct cdevsw   *cdevsw[NUMCDEVSW];
50
51 static int      bmaj2cmaj[NUMCDEVSW];
52
53 MALLOC_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
65 static struct specinfo devt_stash[DEVT_STASH];
66
67 static LIST_HEAD(, specinfo) dev_hash[DEVT_HASH];
68
69 static LIST_HEAD(, specinfo) dev_free;
70
71 devfs_create_t *devfs_create_hook;
72 devfs_remove_t *devfs_remove_hook;
73
74 static int free_devt;
75 SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, "");
76
77 struct cdevsw *
78 devsw(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
89 int
90 cdevsw_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
141 int
142 cdevsw_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
162 int
163 major(dev_t x)
164 {
165         if (x == NODEV)
166                 return NOUDEV;
167         return((x->si_udev >> 8) & 0xff);
168 }
169
170 int
171 minor(dev_t x)
172 {
173         if (x == NODEV)
174                 return NOUDEV;
175         return(x->si_udev & 0xffff00ff);
176 }
177
178 int
179 lminor(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
189 dev_t
190 makebdev(int x, int y)
191 {
192         return (makedev(bmaj2cmaj[x], y));
193 }
194
195 dev_t
196 makedev(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
225 void
226 freedev(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
246 udev_t
247 dev2udev(dev_t x)
248 {
249         if (x == NODEV)
250                 return NOUDEV;
251         return (x->si_udev);
252 }
253
254 dev_t
255 udev2dev(udev_t x, int b)
256 {
257         switch (b) {
258                 case 0:
259                         return makedev(umajor(x), uminor(x));
260                 case 1:
261                         return makebdev(umajor(x), uminor(x));
262                 default:
263                         Debugger("udev2dev(...,X)");
264                         return NODEV;
265         }
266 }
267
268 int
269 uminor(udev_t dev)
270 {
271         return(dev & 0xffff00ff);
272 }
273
274 int
275 umajor(udev_t dev)
276 {
277         return((dev & 0xff00) >> 8);
278 }
279
280 udev_t
281 makeudev(int x, int y)
282 {
283         return ((x << 8) | y);
284 }
285
286 dev_t
287 make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, char *fmt, ...)
288 {
289         dev_t   dev;
290         va_list ap;
291         int i;
292
293         dev = makedev(devsw->d_maj, minor);
294         va_start(ap, fmt);
295         i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
296         dev->si_name[i] = '\0';
297         va_end(ap);
298         dev->si_devsw = devsw;
299
300         if (devfs_create_hook)
301                 devfs_create_hook(dev, uid, gid, perms);
302         return (dev);
303 }
304
305 void
306 destroy_dev(dev_t dev)
307 {
308         if (devfs_remove_hook)
309                 devfs_remove_hook(dev);
310         dev->si_drv1 = 0;
311         dev->si_drv2 = 0;
312         dev->si_devsw = 0;
313         freedev(dev);
314 }
315
316 const char *
317 devtoname(dev_t dev)
318 {
319         char *p;
320         int mynor;
321
322         if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
323                 p = dev->si_name;
324                 if (devsw(dev))
325                         sprintf(p, "#%s/", devsw(dev)->d_name);
326                 else
327                         sprintf(p, "#%d/", major(dev));
328                 p += strlen(p);
329                 mynor = minor(dev);
330                 if (mynor < 0 || mynor > 255)
331                         sprintf(p, "%#x", (u_int)mynor);
332                 else
333                         sprintf(p, "%d", mynor);
334         }
335         return (dev->si_name);
336 }