xref: /freebsd/sys/kern/kern_conf.c (revision 8fa113e5fc65fe6abc757f0089f477a87ee4d185)
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/systm.h>
39 #include <sys/sysctl.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 <sys/ctype.h>
46 #include <machine/stdarg.h>
47 
48 #define cdevsw_ALLOCSTART	(NUMCDEVSW/2)
49 
50 static struct cdevsw 	*cdevsw[NUMCDEVSW];
51 
52 static MALLOC_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 
64 static struct specinfo devt_stash[DEVT_STASH];
65 
66 static LIST_HEAD(, specinfo) dev_hash[DEVT_HASH];
67 
68 static LIST_HEAD(, specinfo) dev_free;
69 
70 devfs_create_t *devfs_create_hook;
71 devfs_destroy_t *devfs_destroy_hook;
72 int devfs_present;
73 
74 static int free_devt;
75 SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, "");
76 
77 /* XXX: This is a hack */
78 void disk_dev_synth(dev_t dev);
79 
80 struct cdevsw *
81 devsw(dev_t dev)
82 {
83 	if (dev->si_devsw)
84 		return (dev->si_devsw);
85 	/* XXX: Hack around our backwards disk code */
86 	disk_dev_synth(dev);
87 	if (dev->si_devsw)
88 		return (dev->si_devsw);
89 	if (devfs_present)
90 		return (NULL);
91         return(cdevsw[major(dev)]);
92 }
93 
94 /*
95  *  Add a cdevsw entry
96  */
97 
98 int
99 cdevsw_add(struct cdevsw *newentry)
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 
108 	if (cdevsw[newentry->d_maj]) {
109 		printf("WARNING: \"%s\" is usurping \"%s\"'s cdevsw[]\n",
110 		    newentry->d_name, cdevsw[newentry->d_maj]->d_name);
111 	}
112 
113 	cdevsw[newentry->d_maj] = newentry;
114 
115 	return (0);
116 }
117 
118 /*
119  *  Remove a cdevsw entry
120  */
121 
122 int
123 cdevsw_remove(struct cdevsw *oldentry)
124 {
125 	if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) {
126 		printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
127 		    oldentry->d_name, oldentry->d_maj);
128 		return EINVAL;
129 	}
130 
131 	cdevsw[oldentry->d_maj] = NULL;
132 
133 	return 0;
134 }
135 
136 /*
137  * dev_t and u_dev_t primitives
138  */
139 
140 int
141 major(dev_t x)
142 {
143 	if (x == NODEV)
144 		return NOUDEV;
145 	return((x->si_udev >> 8) & 0xff);
146 }
147 
148 int
149 minor(dev_t x)
150 {
151 	if (x == NODEV)
152 		return NOUDEV;
153 	return(x->si_udev & 0xffff00ff);
154 }
155 
156 int
157 dev2unit(dev_t x)
158 {
159 	int i;
160 
161 	if (x == NODEV)
162 		return NOUDEV;
163 	i = minor(x);
164 	return ((i & 0xff) | (i >> 8));
165 }
166 
167 int
168 unit2minor(int unit)
169 {
170 
171 	KASSERT(unit <= 0xffffff, ("Invalid unit (%d) in unit2minor", unit));
172 	return ((unit & 0xff) | ((unit << 8) & ~0xffff));
173 }
174 
175 static dev_t
176 allocdev(void)
177 {
178 	static int stashed;
179 	struct specinfo *si;
180 
181 	if (stashed >= DEVT_STASH) {
182 		MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT,
183 		    M_USE_RESERVE | M_ZERO);
184 	} else if (LIST_FIRST(&dev_free)) {
185 		si = LIST_FIRST(&dev_free);
186 		LIST_REMOVE(si, si_hash);
187 	} else {
188 		si = devt_stash + stashed++;
189 		bzero(si, sizeof *si);
190 	si->si_flags |= SI_STASHED;
191 	}
192 	LIST_INIT(&si->si_children);
193 	TAILQ_INIT(&si->si_snapshots);
194 	return (si);
195 }
196 
197 dev_t
198 makedev(int x, int y)
199 {
200 	struct specinfo *si;
201 	udev_t	udev;
202 	int hash;
203 
204 	if (x == umajor(NOUDEV) && y == uminor(NOUDEV))
205 		panic("makedev of NOUDEV");
206 	udev = (x << 8) | y;
207 	hash = udev % DEVT_HASH;
208 	LIST_FOREACH(si, &dev_hash[hash], si_hash) {
209 		if (si->si_udev == udev)
210 			return (si);
211 	}
212 	si = allocdev();
213 	si->si_udev = udev;
214 	LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash);
215         return (si);
216 }
217 
218 void
219 freedev(dev_t dev)
220 {
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 	LIST_REMOVE(dev, si_hash);
229 	if (dev->si_flags & SI_STASHED) {
230 		bzero(dev, sizeof(*dev));
231 		dev->si_flags |= SI_STASHED;
232 		LIST_INSERT_HEAD(&dev_free, dev, si_hash);
233 	} else {
234 		FREE(dev, M_DEVT);
235 	}
236 }
237 
238 udev_t
239 dev2udev(dev_t x)
240 {
241 	if (x == NODEV)
242 		return NOUDEV;
243 	return (x->si_udev);
244 }
245 
246 dev_t
247 udev2dev(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:
256 			return (NODEV);
257 		default:
258 			Debugger("udev2dev(...,X)");
259 			return NODEV;
260 	}
261 }
262 
263 int
264 uminor(udev_t dev)
265 {
266 	return(dev & 0xffff00ff);
267 }
268 
269 int
270 umajor(udev_t dev)
271 {
272 	return((dev & 0xff00) >> 8);
273 }
274 
275 udev_t
276 makeudev(int x, int y)
277 {
278         return ((x << 8) | y);
279 }
280 
281 dev_t
282 make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, const char *fmt, ...)
283 {
284 	dev_t	dev;
285 	va_list ap;
286 	int i;
287 
288 	KASSERT(umajor(makeudev(devsw->d_maj, minor)) == devsw->d_maj,
289 	    ("Invalid minor (%d) in make_dev", minor));
290 
291 	dev = makedev(devsw->d_maj, minor);
292 	if (dev->si_flags & SI_NAMED) {
293 		printf( "WARNING: Driver mistake: repeat make_dev(\"%s\")\n",
294 		    dev->si_name);
295 		panic("don't do that");
296 		return (dev);
297 	}
298 	va_start(ap, fmt);
299 	i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
300 	dev->si_name[i] = '\0';
301 	va_end(ap);
302 	dev->si_devsw = devsw;
303 	dev->si_uid = uid;
304 	dev->si_gid = gid;
305 	dev->si_mode = perms;
306 	dev->si_flags |= SI_NAMED;
307 
308 	if (devfs_create_hook)
309 		devfs_create_hook(dev);
310 	return (dev);
311 }
312 
313 int
314 dev_named(dev_t pdev, const char *name)
315 {
316 	dev_t cdev;
317 
318 	if (strcmp(devtoname(pdev), name) == 0)
319 		return (1);
320 	LIST_FOREACH(cdev, &pdev->si_children, si_siblings)
321 		if (strcmp(devtoname(cdev), name) == 0)
322 			return (1);
323 	return (0);
324 }
325 
326 void
327 dev_depends(dev_t pdev, dev_t cdev)
328 {
329 
330 	cdev->si_parent = pdev;
331 	cdev->si_flags |= SI_CHILD;
332 	LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings);
333 }
334 
335 dev_t
336 make_dev_alias(dev_t pdev, const char *fmt, ...)
337 {
338 	dev_t	dev;
339 	va_list ap;
340 	int i;
341 
342 	dev = allocdev();
343 	dev->si_flags |= SI_ALIAS;
344 	dev->si_flags |= SI_NAMED;
345 	dev_depends(pdev, dev);
346 	va_start(ap, fmt);
347 	i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
348 	dev->si_name[i] = '\0';
349 	va_end(ap);
350 
351 	if (devfs_create_hook)
352 		devfs_create_hook(dev);
353 	return (dev);
354 }
355 
356 void
357 destroy_dev(dev_t dev)
358 {
359 
360 	if (!(dev->si_flags & SI_NAMED)) {
361 		printf( "WARNING: Driver mistake: destroy_dev on %d/%d\n",
362 		    major(dev), minor(dev));
363 		panic("don't do that");
364 		return;
365 	}
366 
367 	if (devfs_destroy_hook)
368 		devfs_destroy_hook(dev);
369 	if (dev->si_flags & SI_CHILD) {
370 		LIST_REMOVE(dev, si_siblings);
371 		dev->si_flags &= ~SI_CHILD;
372 	}
373 	while (!LIST_EMPTY(&dev->si_children))
374 		destroy_dev(LIST_FIRST(&dev->si_children));
375 	dev->si_drv1 = 0;
376 	dev->si_drv2 = 0;
377 	dev->si_devsw = 0;
378 	dev->si_flags &= ~SI_NAMED;
379 	dev->si_flags &= ~SI_ALIAS;
380 	freedev(dev);
381 }
382 
383 const char *
384 devtoname(dev_t dev)
385 {
386 	char *p;
387 	int mynor;
388 
389 	if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
390 		p = dev->si_name;
391 		if (devsw(dev))
392 			sprintf(p, "#%s/", devsw(dev)->d_name);
393 		else
394 			sprintf(p, "#%d/", major(dev));
395 		p += strlen(p);
396 		mynor = minor(dev);
397 		if (mynor < 0 || mynor > 255)
398 			sprintf(p, "%#x", (u_int)mynor);
399 		else
400 			sprintf(p, "%d", mynor);
401 	}
402 	return (dev->si_name);
403 }
404 
405 int
406 dev_stdclone(char *name, char **namep, char *stem, int *unit)
407 {
408 	int u, i;
409 
410 	i = strlen(stem);
411 	if (bcmp(stem, name, i) != 0)
412 		return (0);
413 	if (!isdigit(name[i]))
414 		return (0);
415 	u = 0;
416 	if (name[i] == '0' && isdigit(name[i+1]))
417 		return (0);
418 	while (isdigit(name[i])) {
419 		u *= 10;
420 		u += name[i++] - '0';
421 	}
422 	*unit = u;
423 	if (namep)
424 		*namep = &name[i];
425 	if (name[i])
426 		return (2);
427 	return (1);
428 }
429 
430 /*
431  * Helper sysctl for devname(3).  We're given a {u}dev_t and return
432  * the name, if any, registered by the device driver.
433  */
434 static int
435 sysctl_devname(SYSCTL_HANDLER_ARGS)
436 {
437 	int error;
438 	udev_t ud;
439 	dev_t dev;
440 
441 	error = SYSCTL_IN(req, &ud, sizeof (ud));
442 	if (error)
443 		return (error);
444 	if (ud == NOUDEV)
445 		return(EINVAL);
446 	dev = makedev(umajor(ud), uminor(ud));
447 	if (dev->si_name[0] == '\0')
448 		error = ENOENT;
449 	else
450 		error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1);
451 	freedev(dev);
452 	return (error);
453 }
454 
455 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
456 	NULL, 0, sysctl_devname, "", "devname(3) handler");
457 
458