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 130640 2004-06-17 17:16:53Z 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 * dev_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); |
73static struct cdev *newdev(int x, int y); |
74 |
75 |
76static void 77devlock(void) 78{ 79 if (!mtx_initialized(&devmtx)) 80 mtx_init(&devmtx, "struct cdev *", NULL, MTX_DEF); 81 mtx_lock(&devmtx); 82} 83 --- 38 unchanged lines hidden (view full) --- 122{ 123 devlock(); 124 csw->d_refcount--; 125 KASSERT(csw->d_refcount >= 0, 126 ("cdevsw_vrel(%s) gave negative count", csw->d_name)); 127 devunlock(); 128} 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 == NULL) 251 return NODEV; |
252 return((x->si_udev >> 8) & 0xff); 253} 254 255int 256minor(struct cdev *x) 257{ |
258 if (x == NULL) 259 return NODEV; |
260 return(x->si_udev & 0xffff00ff); 261} 262 263int 264dev2unit(struct cdev *x) 265{ 266 int i; 267 |
268 if (x == NULL) 269 return NODEV; |
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 * |
307newdev(int x, int y) |
308{ 309 struct cdev *si; |
310 dev_t udev; |
311 int hash; 312 |
313 if (x == umajor(NODEV) && y == uminor(NODEV)) 314 panic("newdev of NODEV"); |
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 |
340dev_t |
341dev2udev(struct cdev *x) 342{ |
343 if (x == NULL) 344 return (NODEV); |
345 return (x->si_udev); 346} 347 348struct cdev * |
349findcdev(dev_t udev) |
350{ 351 struct cdev *si; 352 int hash; 353 |
354 if (udev == NODEV) 355 return (NULL); |
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 (NULL); |
362} 363 364int |
365uminor(dev_t dev) |
366{ 367 return (dev & 0xffff00ff); 368} 369 370int |
371umajor(dev_t dev) |
372{ 373 return ((dev & 0xff00) >> 8); 374} 375 |
376static void 377find_major(struct cdevsw *devsw) 378{ 379 int i; 380 381 for (i = NUMCDEVSW - 1; i > 0; i--) 382 if (reserved_majors[i] != i) 383 break; --- 84 unchanged lines hidden (view full) --- 468 va_list ap; 469 int i; 470 471 KASSERT((minornr & ~0xffff00ff) == 0, 472 ("Invalid minor (0x%x) in make_dev", minornr)); 473 474 if (!(devsw->d_flags & D_INIT)) 475 prep_cdevsw(devsw); |
476 dev = newdev(devsw->d_maj, minornr); |
477 if (dev->si_flags & SI_CHEAPCLONE && 478 dev->si_flags & SI_NAMED && 479 dev->si_devsw == devsw) { 480 /* 481 * This is allowed as it removes races and generally 482 * simplifies cloning devices. 483 * XXX: still ?? 484 */ --- 254 unchanged lines hidden (view full) --- 739 if (u > (unit | extra)) { 740 dl = dev; 741 break; 742 } 743 de = dev; 744 } 745 if (unit == -1) 746 unit = low & CLONE_UNITMASK; |
747 dev = newdev(csw->d_maj, unit2minor(unit | extra)); |
748 KASSERT(!(dev->si_flags & SI_CLONELIST), 749 ("Dev %p should not be on clonelist", dev)); 750 if (dl != NULL) 751 LIST_INSERT_BEFORE(dl, dev, si_clone); 752 else if (de != NULL) 753 LIST_INSERT_AFTER(de, dev, si_clone); 754 else 755 LIST_INSERT_HEAD(&cd->head, dev, si_clone); --- 27 unchanged lines hidden (view full) --- 783/* 784 * Helper sysctl for devname(3). We're given a {u}struct cdev *and return 785 * the name, if any, registered by the device driver. 786 */ 787static int 788sysctl_devname(SYSCTL_HANDLER_ARGS) 789{ 790 int error; |
791 dev_t ud; |
792 struct cdev *dev; 793 794 error = SYSCTL_IN(req, &ud, sizeof (ud)); 795 if (error) 796 return (error); |
797 if (ud == NODEV) |
798 return(EINVAL); |
799 dev = findcdev(ud); 800 if (dev == NULL) |
801 error = ENOENT; 802 else 803 error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1); 804 return (error); 805} 806 807SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY, 808 NULL, 0, sysctl_devname, "", "devname(3) handler"); |