kern_conf.c revision 51225
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: head/sys/kern/kern_conf.c 51225 1999-09-13 12:29:32Z bde $ 34 */ 35 36#include <sys/param.h> 37#include <sys/kernel.h> 38#include <sys/sysctl.h> 39#include <sys/systm.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 <machine/stdarg.h> 46 47#define cdevsw_ALLOCSTART (NUMCDEVSW/2) 48 49struct cdevsw *cdevsw[NUMCDEVSW]; 50 51static int bmaj2cmaj[NUMCDEVSW]; 52 53MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage"); 54 55/* 56 * This is the number of hash-buckets. Experiements with 'real-life' 57 * udev_t's show that a prime halfway between two powers of two works 58 * best. 59 */ 60#define DEVT_HASH 83 61 62/* The number of dev_t's we can create before malloc(9) kick in. */ 63#define DEVT_STASH 50 64 65static struct specinfo devt_stash[DEVT_STASH]; 66 67static LIST_HEAD(, specinfo) dev_hash[DEVT_HASH]; 68 69static LIST_HEAD(, specinfo) dev_free; 70 71devfs_create_t *devfs_create_hook; 72devfs_remove_t *devfs_remove_hook; 73 74static int free_devt; 75SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, ""); 76 77/* 78 * Routine to convert from character to block device number. 79 * 80 * A minimal stub routine can always return NODEV. 81 */ 82dev_t 83chrtoblk(dev_t dev) 84{ 85 struct cdevsw *cd; 86 87 if((cd = devsw(dev)) != NULL) { 88 if (cd->d_bmaj != -1) 89 return(makebdev(cd->d_bmaj,minor(dev))); 90 } 91 return(NODEV); 92} 93 94struct cdevsw * 95devsw(dev_t dev) 96{ 97 if (dev->si_devsw) 98 return (dev->si_devsw); 99 return(cdevsw[major(dev)]); 100} 101 102/* 103 * Add a cdevsw entry 104 */ 105 106int 107cdevsw_add(struct cdevsw *newentry) 108{ 109 int i; 110 static int setup; 111 112 if (!setup) { 113 for (i = 0; i < NUMCDEVSW; i++) 114 if (!bmaj2cmaj[i]) 115 bmaj2cmaj[i] = 254; 116 setup++; 117 } 118 119 if (newentry->d_maj < 0 || newentry->d_maj >= NUMCDEVSW) { 120 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n", 121 newentry->d_name, newentry->d_maj); 122 return EINVAL; 123 } 124 125 if (cdevsw[newentry->d_maj]) { 126 printf("WARNING: \"%s\" is usurping \"%s\"'s cdevsw[]\n", 127 newentry->d_name, cdevsw[newentry->d_maj]->d_name); 128 } 129 cdevsw[newentry->d_maj] = newentry; 130 131 if (newentry->d_bmaj >= 0 && newentry->d_bmaj < NUMCDEVSW) { 132 if (bmaj2cmaj[newentry->d_bmaj] != 254) { 133 printf("WARNING: \"%s\" is usurping \"%s\"'s bmaj\n", 134 newentry->d_name, 135 cdevsw[bmaj2cmaj[newentry->d_bmaj]]->d_name); 136 } 137 bmaj2cmaj[newentry->d_bmaj] = newentry->d_maj; 138 } 139 140 return 0; 141} 142 143/* 144 * Remove a cdevsw entry 145 */ 146 147int 148cdevsw_remove(struct cdevsw *oldentry) 149{ 150 if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) { 151 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n", 152 oldentry->d_name, oldentry->d_maj); 153 return EINVAL; 154 } 155 156 cdevsw[oldentry->d_maj] = NULL; 157 158 if (oldentry->d_bmaj >= 0 && oldentry->d_bmaj < NUMCDEVSW) 159 bmaj2cmaj[oldentry->d_bmaj] = 254; 160 161 return 0; 162} 163 164int 165devsw_module_handler(module_t mod, int what, void* arg) 166{ 167 struct devsw_module_data* data = (struct devsw_module_data*) arg; 168 int error = 0; 169 170 switch (what) { 171 case MOD_LOAD: 172 error = cdevsw_add(data->cdevsw); 173 if (!error && data->chainevh) 174 error = data->chainevh(mod, what, data->chainarg); 175 return error; 176 177 case MOD_UNLOAD: 178 if (data->chainevh) { 179 error = data->chainevh(mod, what, data->chainarg); 180 if (error) 181 return error; 182 } 183 cdevsw_remove(data->cdevsw); 184 return error; 185 } 186 187 if (data->chainevh) 188 return data->chainevh(mod, what, data->chainarg); 189 else 190 return 0; 191} 192 193/* 194 * dev_t and u_dev_t primitives 195 */ 196 197int 198major(dev_t x) 199{ 200 if (x == NODEV) 201 return NOUDEV; 202 return((x->si_udev >> 8) & 0xff); 203} 204 205int 206minor(dev_t x) 207{ 208 if (x == NODEV) 209 return NOUDEV; 210 return(x->si_udev & 0xffff00ff); 211} 212 213int 214lminor(dev_t x) 215{ 216 int i; 217 218 if (x == NODEV) 219 return NOUDEV; 220 i = minor(x); 221 return ((i & 0xff) | (i >> 8)); 222} 223 224dev_t 225makebdev(int x, int y) 226{ 227 return (makedev(bmaj2cmaj[x], y)); 228} 229 230dev_t 231makedev(int x, int y) 232{ 233 struct specinfo *si; 234 udev_t udev; 235 int hash; 236 static int stashed; 237 238 udev = (x << 8) | y; 239 hash = udev % DEVT_HASH; 240 LIST_FOREACH(si, &dev_hash[hash], si_hash) { 241 if (si->si_udev == udev) 242 return (si); 243 } 244 if (stashed >= DEVT_STASH) { 245 MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT, 246 M_USE_RESERVE); 247 bzero(si, sizeof(*si)); 248 } else if (LIST_FIRST(&dev_free)) { 249 si = LIST_FIRST(&dev_free); 250 LIST_REMOVE(si, si_hash); 251 } else { 252 si = devt_stash + stashed++; 253 si->si_flags |= SI_STASHED; 254 } 255 si->si_udev = udev; 256 LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash); 257 return (si); 258} 259 260void 261freedev(dev_t dev) 262{ 263 int hash; 264 265 if (!free_devt) 266 return; 267 if (SLIST_FIRST(&dev->si_hlist)) 268 return; 269 if (dev->si_devsw || dev->si_drv1 || dev->si_drv2) 270 return; 271 hash = dev->si_udev % DEVT_HASH; 272 LIST_REMOVE(dev, si_hash); 273 if (dev->si_flags & SI_STASHED) { 274 bzero(dev, sizeof(*dev)); 275 LIST_INSERT_HEAD(&dev_free, dev, si_hash); 276 } else { 277 FREE(dev, M_DEVT); 278 } 279} 280 281udev_t 282dev2udev(dev_t x) 283{ 284 if (x == NODEV) 285 return NOUDEV; 286 return (x->si_udev); 287} 288 289udev_t 290dev2budev(dev_t x) 291{ 292 if (x == NODEV) 293 return NOUDEV; 294 else 295 return makeudev(devsw(x)->d_bmaj, minor(x)); 296} 297 298dev_t 299udev2dev(udev_t x, int b) 300{ 301 switch (b) { 302 case 0: 303 return makedev(umajor(x), uminor(x)); 304 case 1: 305 return makebdev(umajor(x), uminor(x)); 306 default: 307 Debugger("udev2dev(...,X)"); 308 return NODEV; 309 } 310} 311 312int 313uminor(udev_t dev) 314{ 315 return(dev & 0xffff00ff); 316} 317 318int 319umajor(udev_t dev) 320{ 321 return((dev & 0xff00) >> 8); 322} 323 324udev_t 325makeudev(int x, int y) 326{ 327 return ((x << 8) | y); 328} 329 330dev_t 331make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, char *fmt, ...) 332{ 333 dev_t dev; 334 va_list ap; 335 int i; 336 337 dev = makedev(devsw->d_maj, minor); 338 va_start(ap, fmt); 339 i = kvprintf(fmt, NULL, dev->si_name, 32, ap); 340 dev->si_name[i] = '\0'; 341 va_end(ap); 342 dev->si_devsw = devsw; 343 344 if (devfs_create_hook) 345 devfs_create_hook(dev, uid, gid, perms); 346 return (dev); 347} 348 349void 350remove_dev(dev_t dev) 351{ 352 if (devfs_remove_hook) 353 devfs_remove_hook(dev); 354 dev->si_drv1 = 0; 355 dev->si_drv2 = 0; 356 dev->si_devsw = 0; 357 freedev(dev); 358} 359 360const char * 361devtoname(dev_t dev) 362{ 363 char *p; 364 int mynor; 365 366 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') { 367 p = dev->si_name; 368 if (devsw(dev)) 369 sprintf(p, "#%s/", devsw(dev)->d_name); 370 else 371 sprintf(p, "#%d/", major(dev)); 372 p += strlen(p); 373 mynor = minor(dev); 374 if (mynor < 0 || mynor > 255) 375 sprintf(p, "%#x", (u_int)mynor); 376 else 377 sprintf(p, "%d", mynor); 378 } 379 return (dev->si_name); 380} 381