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
--- 11 unchanged lines hidden (view full) ---
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
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/kern/kern_conf.c 130585 2004-06-16 09:47:26Z phk $");
29
30#include <sys/param.h>
31#include <sys/kernel.h>
32#include <sys/systm.h>
33#include <sys/bio.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36#include <sys/sysctl.h>
--- 9 unchanged lines hidden (view full) ---
46
47static MALLOC_DEFINE(M_DEVT, "struct cdev *", "struct cdev *storage");
48
49/* Built at compile time from sys/conf/majors */
50extern unsigned char reserved_majors[256];
51
52/*
53 * This is the number of hash-buckets. Experiements with 'real-life'
54 * udev_t's show that a prime halfway between two powers of two works
55 * best.
56 */
57#define DEVT_HASH 83
58
59/* The number of struct cdev *'s we can create before malloc(9) kick in. */
60#define DEVT_STASH 50
61
62static struct cdev devt_stash[DEVT_STASH];
63
64static LIST_HEAD(, cdev) dev_hash[DEVT_HASH];
65
66static LIST_HEAD(, cdev) dev_free;
67
68static int free_devt;
69SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, "");
70
71static struct mtx devmtx;
72static void freedev(struct cdev *dev);
73
74static void
75devlock(void)
76{
77 if (!mtx_initialized(&devmtx))
78 mtx_init(&devmtx, "struct cdev *", NULL, MTX_DEF);
79 mtx_lock(&devmtx);
80}
81
--- 38 unchanged lines hidden (view full) ---
120{
121 devlock();
122 csw->d_refcount--;
123 KASSERT(csw->d_refcount >= 0,
124 ("cdevsw_vrel(%s) gave negative count", csw->d_name));
125 devunlock();
126}
127
128static struct cdev *makedev(int x, int y);
129
130int
131nullop(void)
132{
133
134 return (0);
135}
136
137int
--- 104 unchanged lines hidden (view full) ---
242
243/*
244 * struct cdev *and u_dev_t primitives
245 */
246
247int
248major(struct cdev *x)
249{
250 if (x == NODEV)
251 return NOUDEV;
252 return((x->si_udev >> 8) & 0xff);
253}
254
255int
256minor(struct cdev *x)
257{
258 if (x == NODEV)
259 return NOUDEV;
260 return(x->si_udev & 0xffff00ff);
261}
262
263int
264dev2unit(struct cdev *x)
265{
266 int i;
267
268 if (x == NODEV)
269 return NOUDEV;
270 i = minor(x);
271 return ((i & 0xff) | (i >> 8));
272}
273
274int
275unit2minor(int unit)
276{
277
--- 21 unchanged lines hidden (view full) ---
299 si->__si_namebuf[0] = '\0';
300 si->si_name = si->__si_namebuf;
301 LIST_INIT(&si->si_children);
302 TAILQ_INIT(&si->si_snapshots);
303 return (si);
304}
305
306static struct cdev *
307makedev(int x, int y)
308{
309 struct cdev *si;
310 udev_t udev;
311 int hash;
312
313 if (x == umajor(NOUDEV) && y == uminor(NOUDEV))
314 panic("makedev of NOUDEV");
315 udev = (x << 8) | y;
316 hash = udev % DEVT_HASH;
317 LIST_FOREACH(si, &dev_hash[hash], si_hash) {
318 if (si->si_udev == udev)
319 return (si);
320 }
321 si = allocdev();
322 si->si_udev = udev;
--- 9 unchanged lines hidden (view full) ---
332 bzero(dev, sizeof(*dev));
333 dev->si_flags |= SI_STASHED;
334 LIST_INSERT_HEAD(&dev_free, dev, si_hash);
335 } else {
336 FREE(dev, M_DEVT);
337 }
338}
339
340udev_t
341dev2udev(struct cdev *x)
342{
343 if (x == NODEV)
344 return (NOUDEV);
345 return (x->si_udev);
346}
347
348struct cdev *
349udev2dev(udev_t udev)
350{
351 struct cdev *si;
352 int hash;
353
354 if (udev == NOUDEV)
355 return (NODEV);
356 hash = udev % DEVT_HASH;
357 LIST_FOREACH(si, &dev_hash[hash], si_hash) {
358 if (si->si_udev == udev)
359 return (si);
360 }
361 return (NODEV);
362}
363
364int
365uminor(udev_t dev)
366{
367 return (dev & 0xffff00ff);
368}
369
370int
371umajor(udev_t dev)
372{
373 return ((dev & 0xff00) >> 8);
374}
375
376udev_t
377makeudev(int x, int y)
378{
379 return ((x << 8) | y);
380}
381
382static void
383find_major(struct cdevsw *devsw)
384{
385 int i;
386
387 for (i = NUMCDEVSW - 1; i > 0; i--)
388 if (reserved_majors[i] != i)
389 break;
--- 84 unchanged lines hidden (view full) ---
474 va_list ap;
475 int i;
476
477 KASSERT((minornr & ~0xffff00ff) == 0,
478 ("Invalid minor (0x%x) in make_dev", minornr));
479
480 if (!(devsw->d_flags & D_INIT))
481 prep_cdevsw(devsw);
482 dev = makedev(devsw->d_maj, minornr);
483 if (dev->si_flags & SI_CHEAPCLONE &&
484 dev->si_flags & SI_NAMED &&
485 dev->si_devsw == devsw) {
486 /*
487 * This is allowed as it removes races and generally
488 * simplifies cloning devices.
489 * XXX: still ??
490 */
--- 254 unchanged lines hidden (view full) ---
745 if (u > (unit | extra)) {
746 dl = dev;
747 break;
748 }
749 de = dev;
750 }
751 if (unit == -1)
752 unit = low & CLONE_UNITMASK;
753 dev = makedev(csw->d_maj, unit2minor(unit | extra));
754 KASSERT(!(dev->si_flags & SI_CLONELIST),
755 ("Dev %p should not be on clonelist", dev));
756 if (dl != NULL)
757 LIST_INSERT_BEFORE(dl, dev, si_clone);
758 else if (de != NULL)
759 LIST_INSERT_AFTER(de, dev, si_clone);
760 else
761 LIST_INSERT_HEAD(&cd->head, dev, si_clone);
--- 27 unchanged lines hidden (view full) ---
789/*
790 * Helper sysctl for devname(3). We're given a {u}struct cdev *and return
791 * the name, if any, registered by the device driver.
792 */
793static int
794sysctl_devname(SYSCTL_HANDLER_ARGS)
795{
796 int error;
797 udev_t ud;
798 struct cdev *dev;
799
800 error = SYSCTL_IN(req, &ud, sizeof (ud));
801 if (error)
802 return (error);
803 if (ud == NOUDEV)
804 return(EINVAL);
805 dev = udev2dev(ud);
806 if (dev == NODEV)
807 error = ENOENT;
808 else
809 error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1);
810 return (error);
811}
812
813SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
814 NULL, 0, sysctl_devname, "", "devname(3) handler");
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
--- 11 unchanged lines hidden (view full) ---
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
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/kern/kern_conf.c 130585 2004-06-16 09:47:26Z phk $");
29
30#include <sys/param.h>
31#include <sys/kernel.h>
32#include <sys/systm.h>
33#include <sys/bio.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36#include <sys/sysctl.h>
--- 9 unchanged lines hidden (view full) ---
46
47static MALLOC_DEFINE(M_DEVT, "struct cdev *", "struct cdev *storage");
48
49/* Built at compile time from sys/conf/majors */
50extern unsigned char reserved_majors[256];
51
52/*
53 * This is the number of hash-buckets. Experiements with 'real-life'
54 * udev_t's show that a prime halfway between two powers of two works
55 * best.
56 */
57#define DEVT_HASH 83
58
59/* The number of struct cdev *'s we can create before malloc(9) kick in. */
60#define DEVT_STASH 50
61
62static struct cdev devt_stash[DEVT_STASH];
63
64static LIST_HEAD(, cdev) dev_hash[DEVT_HASH];
65
66static LIST_HEAD(, cdev) dev_free;
67
68static int free_devt;
69SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, "");
70
71static struct mtx devmtx;
72static void freedev(struct cdev *dev);
73
74static void
75devlock(void)
76{
77 if (!mtx_initialized(&devmtx))
78 mtx_init(&devmtx, "struct cdev *", NULL, MTX_DEF);
79 mtx_lock(&devmtx);
80}
81
--- 38 unchanged lines hidden (view full) ---
120{
121 devlock();
122 csw->d_refcount--;
123 KASSERT(csw->d_refcount >= 0,
124 ("cdevsw_vrel(%s) gave negative count", csw->d_name));
125 devunlock();
126}
127
128static struct cdev *makedev(int x, int y);
129
130int
131nullop(void)
132{
133
134 return (0);
135}
136
137int
--- 104 unchanged lines hidden (view full) ---
242
243/*
244 * struct cdev *and u_dev_t primitives
245 */
246
247int
248major(struct cdev *x)
249{
250 if (x == NODEV)
251 return NOUDEV;
252 return((x->si_udev >> 8) & 0xff);
253}
254
255int
256minor(struct cdev *x)
257{
258 if (x == NODEV)
259 return NOUDEV;
260 return(x->si_udev & 0xffff00ff);
261}
262
263int
264dev2unit(struct cdev *x)
265{
266 int i;
267
268 if (x == NODEV)
269 return NOUDEV;
270 i = minor(x);
271 return ((i & 0xff) | (i >> 8));
272}
273
274int
275unit2minor(int unit)
276{
277
--- 21 unchanged lines hidden (view full) ---
299 si->__si_namebuf[0] = '\0';
300 si->si_name = si->__si_namebuf;
301 LIST_INIT(&si->si_children);
302 TAILQ_INIT(&si->si_snapshots);
303 return (si);
304}
305
306static struct cdev *
307makedev(int x, int y)
308{
309 struct cdev *si;
310 udev_t udev;
311 int hash;
312
313 if (x == umajor(NOUDEV) && y == uminor(NOUDEV))
314 panic("makedev of NOUDEV");
315 udev = (x << 8) | y;
316 hash = udev % DEVT_HASH;
317 LIST_FOREACH(si, &dev_hash[hash], si_hash) {
318 if (si->si_udev == udev)
319 return (si);
320 }
321 si = allocdev();
322 si->si_udev = udev;
--- 9 unchanged lines hidden (view full) ---
332 bzero(dev, sizeof(*dev));
333 dev->si_flags |= SI_STASHED;
334 LIST_INSERT_HEAD(&dev_free, dev, si_hash);
335 } else {
336 FREE(dev, M_DEVT);
337 }
338}
339
340udev_t
341dev2udev(struct cdev *x)
342{
343 if (x == NODEV)
344 return (NOUDEV);
345 return (x->si_udev);
346}
347
348struct cdev *
349udev2dev(udev_t udev)
350{
351 struct cdev *si;
352 int hash;
353
354 if (udev == NOUDEV)
355 return (NODEV);
356 hash = udev % DEVT_HASH;
357 LIST_FOREACH(si, &dev_hash[hash], si_hash) {
358 if (si->si_udev == udev)
359 return (si);
360 }
361 return (NODEV);
362}
363
364int
365uminor(udev_t dev)
366{
367 return (dev & 0xffff00ff);
368}
369
370int
371umajor(udev_t dev)
372{
373 return ((dev & 0xff00) >> 8);
374}
375
376udev_t
377makeudev(int x, int y)
378{
379 return ((x << 8) | y);
380}
381
382static void
383find_major(struct cdevsw *devsw)
384{
385 int i;
386
387 for (i = NUMCDEVSW - 1; i > 0; i--)
388 if (reserved_majors[i] != i)
389 break;
--- 84 unchanged lines hidden (view full) ---
474 va_list ap;
475 int i;
476
477 KASSERT((minornr & ~0xffff00ff) == 0,
478 ("Invalid minor (0x%x) in make_dev", minornr));
479
480 if (!(devsw->d_flags & D_INIT))
481 prep_cdevsw(devsw);
482 dev = makedev(devsw->d_maj, minornr);
483 if (dev->si_flags & SI_CHEAPCLONE &&
484 dev->si_flags & SI_NAMED &&
485 dev->si_devsw == devsw) {
486 /*
487 * This is allowed as it removes races and generally
488 * simplifies cloning devices.
489 * XXX: still ??
490 */
--- 254 unchanged lines hidden (view full) ---
745 if (u > (unit | extra)) {
746 dl = dev;
747 break;
748 }
749 de = dev;
750 }
751 if (unit == -1)
752 unit = low & CLONE_UNITMASK;
753 dev = makedev(csw->d_maj, unit2minor(unit | extra));
754 KASSERT(!(dev->si_flags & SI_CLONELIST),
755 ("Dev %p should not be on clonelist", dev));
756 if (dl != NULL)
757 LIST_INSERT_BEFORE(dl, dev, si_clone);
758 else if (de != NULL)
759 LIST_INSERT_AFTER(de, dev, si_clone);
760 else
761 LIST_INSERT_HEAD(&cd->head, dev, si_clone);
--- 27 unchanged lines hidden (view full) ---
789/*
790 * Helper sysctl for devname(3). We're given a {u}struct cdev *and return
791 * the name, if any, registered by the device driver.
792 */
793static int
794sysctl_devname(SYSCTL_HANDLER_ARGS)
795{
796 int error;
797 udev_t ud;
798 struct cdev *dev;
799
800 error = SYSCTL_IN(req, &ud, sizeof (ud));
801 if (error)
802 return (error);
803 if (ud == NOUDEV)
804 return(EINVAL);
805 dev = udev2dev(ud);
806 if (dev == NODEV)
807 error = ENOENT;
808 else
809 error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1);
810 return (error);
811}
812
813SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
814 NULL, 0, sysctl_devname, "", "devname(3) handler");