kern_conf.c revision 111735
1/*-
2 * Copyright (c) 1999-2002 Poul-Henning Kamp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/kern/kern_conf.c 111735 2003-03-02 14:43:46Z phk $
27 */
28
29#include <sys/param.h>
30#include <sys/kernel.h>
31#include <sys/systm.h>
32#include <sys/bio.h>
33#include <sys/lock.h>
34#include <sys/mutex.h>
35#include <sys/sysctl.h>
36#include <sys/module.h>
37#include <sys/malloc.h>
38#include <sys/conf.h>
39#include <sys/vnode.h>
40#include <sys/queue.h>
41#include <sys/ctype.h>
42#include <machine/stdarg.h>
43
44static MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage");
45
46/* Built at compile time from sys/conf/majors */
47extern unsigned char reserved_majors[256];
48
49/*
50 * This is the number of hash-buckets.  Experiements with 'real-life'
51 * udev_t's show that a prime halfway between two powers of two works
52 * best.
53 */
54#define DEVT_HASH 83
55
56/* The number of dev_t's we can create before malloc(9) kick in.  */
57#define DEVT_STASH 50
58
59static struct cdev devt_stash[DEVT_STASH];
60
61static LIST_HEAD(, cdev) dev_hash[DEVT_HASH];
62
63static LIST_HEAD(, cdev) dev_free;
64
65static int ready_for_devs;
66
67static int free_devt;
68SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, "");
69
70/* Define a dead_cdevsw for use when devices leave unexpectedly. */
71
72static int
73enxio(void)
74{
75	return (ENXIO);
76}
77
78#define dead_open	(d_open_t *)enxio
79#define dead_close	(d_close_t *)enxio
80#define dead_read	(d_read_t *)enxio
81#define dead_write	(d_write_t *)enxio
82#define dead_ioctl	(d_ioctl_t *)enxio
83#define dead_poll	nopoll
84#define dead_mmap	nommap
85
86static void
87dead_strategy(struct bio *bp)
88{
89
90	biofinish(bp, NULL, ENXIO);
91}
92
93#define dead_dump	(dumper_t *)enxio
94
95#define dead_kqfilter	(d_kqfilter_t *)enxio
96
97static struct cdevsw dead_cdevsw = {
98	/* open */	dead_open,
99	/* close */	dead_close,
100	/* read */	dead_read,
101	/* write */	dead_write,
102	/* ioctl */	dead_ioctl,
103	/* poll */	dead_poll,
104	/* mmap */	dead_mmap,
105	/* strategy */	dead_strategy,
106	/* name */	"dead",
107	/* maj */	255,
108	/* dump */	dead_dump,
109	/* psize */	nopsize,
110	/* flags */	0,
111	/* kqfilter */	dead_kqfilter
112};
113
114
115struct cdevsw *
116devsw(dev_t dev)
117{
118	if (dev->si_devsw)
119		return (dev->si_devsw);
120	return (&dead_cdevsw);
121}
122
123/*
124 * dev_t and u_dev_t primitives
125 */
126
127int
128major(dev_t x)
129{
130	if (x == NODEV)
131		return NOUDEV;
132	return((x->si_udev >> 8) & 0xff);
133}
134
135int
136minor(dev_t x)
137{
138	if (x == NODEV)
139		return NOUDEV;
140	return(x->si_udev & 0xffff00ff);
141}
142
143int
144dev2unit(dev_t x)
145{
146	int i;
147
148	if (x == NODEV)
149		return NOUDEV;
150	i = minor(x);
151	return ((i & 0xff) | (i >> 8));
152}
153
154int
155unit2minor(int unit)
156{
157
158	KASSERT(unit <= 0xffffff, ("Invalid unit (%d) in unit2minor", unit));
159	return ((unit & 0xff) | ((unit << 8) & ~0xffff));
160}
161
162static dev_t
163allocdev(void)
164{
165	static int stashed;
166	struct cdev *si;
167
168	if (LIST_FIRST(&dev_free)) {
169		si = LIST_FIRST(&dev_free);
170		LIST_REMOVE(si, si_hash);
171	} else if (stashed >= DEVT_STASH) {
172		MALLOC(si, struct cdev *, sizeof(*si), M_DEVT,
173		    M_USE_RESERVE | M_ZERO | M_WAITOK);
174	} else {
175		si = devt_stash + stashed++;
176		bzero(si, sizeof *si);
177		si->si_flags |= SI_STASHED;
178	}
179	si->__si_namebuf[0] = '\0';
180	si->si_name = si->__si_namebuf;
181	LIST_INIT(&si->si_children);
182	TAILQ_INIT(&si->si_snapshots);
183	return (si);
184}
185
186dev_t
187makedev(int x, int y)
188{
189	struct cdev *si;
190	udev_t	udev;
191	int hash;
192
193	if (x == umajor(NOUDEV) && y == uminor(NOUDEV))
194		panic("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	si = allocdev();
202	si->si_udev = udev;
203	LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash);
204        return (si);
205}
206
207void
208freedev(dev_t dev)
209{
210
211	if (!free_devt)
212		return;
213	if (SLIST_FIRST(&dev->si_hlist))
214		return;
215	if (dev->si_devsw || dev->si_drv1 || dev->si_drv2)
216		return;
217	LIST_REMOVE(dev, si_hash);
218	if (dev->si_flags & SI_STASHED) {
219		bzero(dev, sizeof(*dev));
220		dev->si_flags |= SI_STASHED;
221		LIST_INSERT_HEAD(&dev_free, dev, si_hash);
222	} else {
223		FREE(dev, M_DEVT);
224	}
225}
226
227udev_t
228dev2udev(dev_t x)
229{
230	if (x == NODEV)
231		return NOUDEV;
232	return (x->si_udev);
233}
234
235dev_t
236udev2dev(udev_t x, int b)
237{
238
239	if (x == NOUDEV)
240		return (NODEV);
241	switch (b) {
242		case 0:
243			return makedev(umajor(x), uminor(x));
244		case 1:
245			return (NODEV);
246		default:
247			Debugger("udev2dev(...,X)");
248			return NODEV;
249	}
250}
251
252int
253uminor(udev_t dev)
254{
255	return(dev & 0xffff00ff);
256}
257
258int
259umajor(udev_t dev)
260{
261	return((dev & 0xff00) >> 8);
262}
263
264udev_t
265makeudev(int x, int y)
266{
267        return ((x << 8) | y);
268}
269
270dev_t
271make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, const char *fmt, ...)
272{
273	dev_t	dev;
274	va_list ap;
275	int i;
276
277	KASSERT((minor & ~0xffff00ff) == 0,
278	    ("Invalid minor (0x%x) in make_dev", minor));
279
280	if (devsw->d_maj == MAJOR_AUTO) {
281		for (i = NUMCDEVSW - 1; i > 0; i--)
282			if (reserved_majors[i] != i)
283				break;
284		KASSERT(i > 0, ("Out of major numbers (%s)", devsw->d_name));
285		printf("Allocating major#%d to \"%s\"\n", i, devsw->d_name);
286		devsw->d_maj = i;
287		reserved_majors[i] = i;
288	} else {
289		KASSERT(devsw->d_maj >= 0 && devsw->d_maj < 256,
290		    ("Invalid major (%d) in make_dev", devsw->d_maj));
291		if (reserved_majors[devsw->d_maj] != devsw->d_maj) {
292			printf("WARNING: driver \"%s\" used %s %d\n",
293			    devsw->d_name, "unreserved major device number",
294			    devsw->d_maj);
295			reserved_majors[devsw->d_maj] = devsw->d_maj;
296		}
297	}
298
299	if (!ready_for_devs) {
300		printf("WARNING: Driver mistake: make_dev(%s) called before SI_SUB_DRIVERS\n",
301		       fmt);
302		/* XXX panic here once drivers are cleaned up */
303	}
304
305	dev = makedev(devsw->d_maj, minor);
306	if (dev->si_flags & SI_NAMED) {
307		printf( "WARNING: Driver mistake: repeat make_dev(\"%s\")\n",
308		    dev->si_name);
309		panic("don't do that");
310		return (dev);
311	}
312	va_start(ap, fmt);
313	i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap);
314	if (i > (sizeof dev->__si_namebuf - 1)) {
315		printf("WARNING: Device name truncated! (%s)",
316		    dev->__si_namebuf);
317	}
318	va_end(ap);
319	dev->si_devsw = devsw;
320	dev->si_uid = uid;
321	dev->si_gid = gid;
322	dev->si_mode = perms;
323	dev->si_flags |= SI_NAMED;
324
325	devfs_create(dev);
326	return (dev);
327}
328
329int
330dev_named(dev_t pdev, const char *name)
331{
332	dev_t cdev;
333
334	if (strcmp(devtoname(pdev), name) == 0)
335		return (1);
336	LIST_FOREACH(cdev, &pdev->si_children, si_siblings)
337		if (strcmp(devtoname(cdev), name) == 0)
338			return (1);
339	return (0);
340}
341
342void
343dev_depends(dev_t pdev, dev_t cdev)
344{
345
346	cdev->si_parent = pdev;
347	cdev->si_flags |= SI_CHILD;
348	LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings);
349}
350
351dev_t
352make_dev_alias(dev_t pdev, const char *fmt, ...)
353{
354	dev_t	dev;
355	va_list ap;
356	int i;
357
358	dev = allocdev();
359	dev->si_flags |= SI_ALIAS;
360	dev->si_flags |= SI_NAMED;
361	dev_depends(pdev, dev);
362	va_start(ap, fmt);
363	i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap);
364	if (i > (sizeof dev->__si_namebuf - 1)) {
365		printf("WARNING: Device name truncated! (%s)",
366		    dev->__si_namebuf);
367	}
368	va_end(ap);
369
370	devfs_create(dev);
371	return (dev);
372}
373
374void
375revoke_and_destroy_dev(dev_t dev)
376{
377	struct vnode *vp;
378
379	GIANT_REQUIRED;
380
381	vp = SLIST_FIRST(&dev->si_hlist);
382	if (vp != NULL)
383		VOP_REVOKE(vp, REVOKEALL);
384	destroy_dev(dev);
385}
386
387void
388destroy_dev(dev_t dev)
389{
390
391	if (!(dev->si_flags & SI_NAMED)) {
392		printf( "WARNING: Driver mistake: destroy_dev on %d/%d\n",
393		    major(dev), minor(dev));
394		panic("don't do that");
395		return;
396	}
397
398	devfs_destroy(dev);
399	if (dev->si_flags & SI_CHILD) {
400		LIST_REMOVE(dev, si_siblings);
401		dev->si_flags &= ~SI_CHILD;
402	}
403	while (!LIST_EMPTY(&dev->si_children))
404		destroy_dev(LIST_FIRST(&dev->si_children));
405	dev->si_drv1 = 0;
406	dev->si_drv2 = 0;
407	dev->si_devsw = 0;
408	bzero(&dev->__si_u, sizeof(dev->__si_u));
409	dev->si_flags &= ~SI_NAMED;
410	dev->si_flags &= ~SI_ALIAS;
411	freedev(dev);
412}
413
414const char *
415devtoname(dev_t dev)
416{
417	char *p;
418	int mynor;
419
420	if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
421		p = dev->si_name;
422		if (devsw(dev))
423			sprintf(p, "#%s/", devsw(dev)->d_name);
424		else
425			sprintf(p, "#%d/", major(dev));
426		p += strlen(p);
427		mynor = minor(dev);
428		if (mynor < 0 || mynor > 255)
429			sprintf(p, "%#x", (u_int)mynor);
430		else
431			sprintf(p, "%d", mynor);
432	}
433	return (dev->si_name);
434}
435
436int
437dev_stdclone(char *name, char **namep, const char *stem, int *unit)
438{
439	int u, i;
440
441	i = strlen(stem);
442	if (bcmp(stem, name, i) != 0)
443		return (0);
444	if (!isdigit(name[i]))
445		return (0);
446	u = 0;
447	if (name[i] == '0' && isdigit(name[i+1]))
448		return (0);
449	while (isdigit(name[i])) {
450		u *= 10;
451		u += name[i++] - '0';
452	}
453	if (u > 0xffffff)
454		return (0);
455	*unit = u;
456	if (namep)
457		*namep = &name[i];
458	if (name[i])
459		return (2);
460	return (1);
461}
462
463/*
464 * Helper sysctl for devname(3).  We're given a {u}dev_t and return
465 * the name, if any, registered by the device driver.
466 */
467static int
468sysctl_devname(SYSCTL_HANDLER_ARGS)
469{
470	int error;
471	udev_t ud;
472	dev_t dev;
473
474	error = SYSCTL_IN(req, &ud, sizeof (ud));
475	if (error)
476		return (error);
477	if (ud == NOUDEV)
478		return(EINVAL);
479	dev = makedev(umajor(ud), uminor(ud));
480	if (dev->si_name[0] == '\0')
481		error = ENOENT;
482	else
483		error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1);
484	freedev(dev);
485	return (error);
486}
487
488SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
489	NULL, 0, sysctl_devname, "", "devname(3) handler");
490
491/*
492 * Set ready_for_devs; prior to this point, device creation is not allowed.
493 */
494static void
495dev_set_ready(void *junk)
496{
497	ready_for_devs = 1;
498}
499
500SYSINIT(dev_ready, SI_SUB_DEVFS, SI_ORDER_FIRST, dev_set_ready, NULL);
501