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