Throw better sanity checks into vfs_hang_addrlist() for argp->ex_addrlen
[dragonfly.git] / sys / kern / kern_conf.c
CommitLineData
984263bc
MD
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: src/sys/kern/kern_conf.c,v 1.73.2.3 2003/03/10 02:18:25 imp Exp $
fabb8ceb 34 * $DragonFly: src/sys/kern/kern_conf.c,v 1.3 2003/07/21 05:50:43 dillon Exp $
984263bc
MD
35 */
36
37#include <sys/param.h>
38#include <sys/kernel.h>
39#include <sys/sysctl.h>
40#include <sys/systm.h>
41#include <sys/module.h>
42#include <sys/malloc.h>
43#include <sys/conf.h>
44#include <sys/vnode.h>
45#include <sys/queue.h>
46#include <machine/stdarg.h>
47
48#define cdevsw_ALLOCSTART (NUMCDEVSW/2)
49
fabb8ceb 50static struct cdevsw *cdevsw[NUMCDEVSW];
984263bc
MD
51
52MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage");
53
54/*
55 * This is the number of hash-buckets. Experiements with 'real-life'
56 * udev_t's show that a prime halfway between two powers of two works
57 * best.
58 */
59#define DEVT_HASH 83
60
61/* The number of dev_t's we can create before malloc(9) kick in. */
62#define DEVT_STASH 50
63
64static struct specinfo devt_stash[DEVT_STASH];
65
66static LIST_HEAD(, specinfo) dev_hash[DEVT_HASH];
67
68static LIST_HEAD(, specinfo) dev_free;
69
70static int free_devt;
71SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, "");
72
73struct cdevsw *
74devsw(dev_t dev)
75{
76 if (dev->si_devsw)
77 return (dev->si_devsw);
78 return(cdevsw[major(dev)]);
79}
80
81static void
82compile_devsw(struct cdevsw *devsw)
83{
fabb8ceb
MD
84 static lwkt_port devsw_compat_port;
85
86 /* YYY init devsw_compat_port */
87
984263bc
MD
88 if (devsw->d_open == NULL)
89 devsw->d_open = noopen;
90 if (devsw->d_close == NULL)
91 devsw->d_close = noclose;
92 if (devsw->d_read == NULL)
93 devsw->d_read = noread;
94 if (devsw->d_write == NULL)
95 devsw->d_write = nowrite;
96 if (devsw->d_ioctl == NULL)
97 devsw->d_ioctl = noioctl;
98 if (devsw->d_poll == NULL)
99 devsw->d_poll = nopoll;
100 if (devsw->d_mmap == NULL)
101 devsw->d_mmap = nommap;
102 if (devsw->d_strategy == NULL)
103 devsw->d_strategy = nostrategy;
104 if (devsw->d_dump == NULL)
105 devsw->d_dump = nodump;
106 if (devsw->d_psize == NULL)
107 devsw->d_psize = nopsize;
108 if (devsw->d_kqfilter == NULL)
109 devsw->d_kqfilter = nokqfilter;
fabb8ceb
MD
110 if (devsw->d_port == NULL)
111 devsw->d_port = &devsw_compat_port;
984263bc
MD
112}
113
114/*
115 * Add a cdevsw entry
116 */
117
118int
119cdevsw_add(struct cdevsw *newentry)
120{
984263bc
MD
121 compile_devsw(newentry);
122 if (newentry->d_maj < 0 || newentry->d_maj >= NUMCDEVSW) {
123 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
124 newentry->d_name, newentry->d_maj);
125 return (EINVAL);
126 }
984263bc
MD
127 if (cdevsw[newentry->d_maj]) {
128 printf("WARNING: \"%s\" is usurping \"%s\"'s cdevsw[]\n",
129 newentry->d_name, cdevsw[newentry->d_maj]->d_name);
130 }
131
132 cdevsw[newentry->d_maj] = newentry;
984263bc
MD
133 return (0);
134}
135
136/*
137 * Remove a cdevsw entry
138 */
139
140int
141cdevsw_remove(struct cdevsw *oldentry)
142{
143 if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) {
144 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
145 oldentry->d_name, oldentry->d_maj);
146 return EINVAL;
147 }
148
149 cdevsw[oldentry->d_maj] = NULL;
150
984263bc
MD
151 return 0;
152}
153
154/*
155 * dev_t and u_dev_t primitives
156 */
157
158int
159major(dev_t x)
160{
161 if (x == NODEV)
162 return NOUDEV;
163 return((x->si_udev >> 8) & 0xff);
164}
165
166int
167minor(dev_t x)
168{
169 if (x == NODEV)
170 return NOUDEV;
171 return(x->si_udev & 0xffff00ff);
172}
173
174int
175lminor(dev_t x)
176{
177 int i;
178
179 if (x == NODEV)
180 return NOUDEV;
181 i = minor(x);
182 return ((i & 0xff) | (i >> 8));
183}
184
984263bc
MD
185dev_t
186makedev(int x, int y)
187{
188 struct specinfo *si;
189 udev_t udev;
190 int hash;
191 static int stashed;
192
193 if (x == umajor(NOUDEV) && y == uminor(NOUDEV))
194 Debugger("makedev of NOUDEV");
195 udev = (x << 8) | y;
196 hash = udev % DEVT_HASH;
197 LIST_FOREACH(si, &dev_hash[hash], si_hash) {
198 if (si->si_udev == udev)
199 return (si);
200 }
201 if (stashed >= DEVT_STASH) {
202 MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT,
203 M_USE_RESERVE);
204 bzero(si, sizeof(*si));
205 } else if (LIST_FIRST(&dev_free)) {
206 si = LIST_FIRST(&dev_free);
207 LIST_REMOVE(si, si_hash);
208 } else {
209 si = devt_stash + stashed++;
210 si->si_flags |= SI_STASHED;
211 }
212 si->si_udev = udev;
213 LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash);
214 return (si);
215}
216
217void
218freedev(dev_t dev)
219{
220 int hash;
221
222 if (!free_devt)
223 return;
224 if (SLIST_FIRST(&dev->si_hlist))
225 return;
226 if (dev->si_devsw || dev->si_drv1 || dev->si_drv2)
227 return;
228 hash = dev->si_udev % DEVT_HASH;
229 LIST_REMOVE(dev, si_hash);
230 if (dev->si_flags & SI_STASHED) {
231 bzero(dev, sizeof(*dev));
232 LIST_INSERT_HEAD(&dev_free, dev, si_hash);
233 } else {
234 FREE(dev, M_DEVT);
235 }
236}
237
238udev_t
239dev2udev(dev_t x)
240{
241 if (x == NODEV)
242 return NOUDEV;
243 return (x->si_udev);
244}
245
246dev_t
247udev2dev(udev_t x, int b)
248{
249
250 if (x == NOUDEV)
251 return (NODEV);
252 switch (b) {
253 case 0:
254 return makedev(umajor(x), uminor(x));
255 case 1:
fabb8ceb
MD
256 printf("udev2dev: attempt to lookup block dev(%d)", x);
257 return NODEV;
984263bc
MD
258 default:
259 Debugger("udev2dev(...,X)");
260 return NODEV;
261 }
262}
263
264int
265uminor(udev_t dev)
266{
267 return(dev & 0xffff00ff);
268}
269
270int
271umajor(udev_t dev)
272{
273 return((dev & 0xff00) >> 8);
274}
275
276udev_t
277makeudev(int x, int y)
278{
279 return ((x << 8) | y);
280}
281
282dev_t
283make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, const char *fmt, ...)
284{
285 dev_t dev;
286 va_list ap;
287 int i;
288
289 compile_devsw(devsw);
290 dev = makedev(devsw->d_maj, minor);
291 va_start(ap, fmt);
292 i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
293 dev->si_name[i] = '\0';
294 va_end(ap);
295 dev->si_devsw = devsw;
296
297 return (dev);
298}
299
300void
301destroy_dev(dev_t dev)
302{
303 dev->si_drv1 = 0;
304 dev->si_drv2 = 0;
305 dev->si_devsw = 0;
306 freedev(dev);
307}
308
309const char *
310devtoname(dev_t dev)
311{
312 char *p;
313 int mynor;
314
315 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
316 p = dev->si_name;
317 if (devsw(dev))
318 sprintf(p, "#%s/", devsw(dev)->d_name);
319 else
320 sprintf(p, "#%d/", major(dev));
321 p += strlen(p);
322 mynor = minor(dev);
323 if (mynor < 0 || mynor > 255)
324 sprintf(p, "%#x", (u_int)mynor);
325 else
326 sprintf(p, "%d", mynor);
327 }
328 return (dev->si_name);
329}