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/systm.h> 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 #include <sys/sysctl.h> 42 #include <sys/module.h> 43 #include <sys/malloc.h> 44 #include <sys/conf.h> 45 #include <sys/vnode.h> 46 #include <sys/queue.h> 47 #include <sys/ctype.h> 48 #include <machine/stdarg.h> 49 50 #define cdevsw_ALLOCSTART (NUMCDEVSW/2) 51 52 static struct cdevsw *cdevsw[NUMCDEVSW]; 53 54 static MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage"); 55 56 /* 57 * This is the number of hash-buckets. Experiements with 'real-life' 58 * udev_t's show that a prime halfway between two powers of two works 59 * best. 60 */ 61 #define DEVT_HASH 83 62 63 /* The number of dev_t's we can create before malloc(9) kick in. */ 64 #define DEVT_STASH 50 65 66 static struct specinfo devt_stash[DEVT_STASH]; 67 68 static LIST_HEAD(, specinfo) dev_hash[DEVT_HASH]; 69 70 static LIST_HEAD(, specinfo) dev_free; 71 72 devfs_create_t *devfs_create_hook; 73 devfs_destroy_t *devfs_destroy_hook; 74 int devfs_present; 75 76 static int ready_for_devs; 77 78 static int free_devt; 79 SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, ""); 80 81 /* XXX: This is a hack */ 82 void disk_dev_synth(dev_t dev); 83 84 struct cdevsw * 85 devsw(dev_t dev) 86 { 87 if (dev->si_devsw) 88 return (dev->si_devsw); 89 /* XXX: Hack around our backwards disk code */ 90 disk_dev_synth(dev); 91 if (dev->si_devsw) 92 return (dev->si_devsw); 93 if (devfs_present) 94 return (NULL); 95 return(cdevsw[major(dev)]); 96 } 97 98 /* 99 * Add a cdevsw entry 100 */ 101 102 int 103 cdevsw_add(struct cdevsw *newentry) 104 { 105 106 if (newentry->d_maj < 0 || newentry->d_maj >= NUMCDEVSW) { 107 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n", 108 newentry->d_name, newentry->d_maj); 109 return (EINVAL); 110 } 111 112 if (cdevsw[newentry->d_maj]) { 113 printf("WARNING: \"%s\" is usurping \"%s\"'s cdevsw[]\n", 114 newentry->d_name, cdevsw[newentry->d_maj]->d_name); 115 } 116 117 cdevsw[newentry->d_maj] = newentry; 118 119 return (0); 120 } 121 122 /* 123 * Remove a cdevsw entry 124 */ 125 126 int 127 cdevsw_remove(struct cdevsw *oldentry) 128 { 129 if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) { 130 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n", 131 oldentry->d_name, oldentry->d_maj); 132 return EINVAL; 133 } 134 135 cdevsw[oldentry->d_maj] = NULL; 136 137 return 0; 138 } 139 140 /* 141 * dev_t and u_dev_t primitives 142 */ 143 144 int 145 major(dev_t x) 146 { 147 if (x == NODEV) 148 return NOUDEV; 149 return((x->si_udev >> 8) & 0xff); 150 } 151 152 int 153 minor(dev_t x) 154 { 155 if (x == NODEV) 156 return NOUDEV; 157 return(x->si_udev & 0xffff00ff); 158 } 159 160 int 161 dev2unit(dev_t x) 162 { 163 int i; 164 165 if (x == NODEV) 166 return NOUDEV; 167 i = minor(x); 168 return ((i & 0xff) | (i >> 8)); 169 } 170 171 int 172 unit2minor(int unit) 173 { 174 175 KASSERT(unit <= 0xffffff, ("Invalid unit (%d) in unit2minor", unit)); 176 return ((unit & 0xff) | ((unit << 8) & ~0xffff)); 177 } 178 179 static dev_t 180 allocdev(void) 181 { 182 static int stashed; 183 struct specinfo *si; 184 185 if (stashed >= DEVT_STASH) { 186 MALLOC(si, struct specinfo *, sizeof(*si), M_DEVT, 187 M_USE_RESERVE | M_ZERO); 188 } else if (LIST_FIRST(&dev_free)) { 189 si = LIST_FIRST(&dev_free); 190 LIST_REMOVE(si, si_hash); 191 } else { 192 si = devt_stash + stashed++; 193 bzero(si, sizeof *si); 194 si->si_flags |= SI_STASHED; 195 } 196 LIST_INIT(&si->si_children); 197 TAILQ_INIT(&si->si_snapshots); 198 return (si); 199 } 200 201 dev_t 202 makedev(int x, int y) 203 { 204 struct specinfo *si; 205 udev_t udev; 206 int hash; 207 208 if (x == umajor(NOUDEV) && y == uminor(NOUDEV)) 209 panic("makedev of NOUDEV"); 210 udev = (x << 8) | y; 211 hash = udev % DEVT_HASH; 212 LIST_FOREACH(si, &dev_hash[hash], si_hash) { 213 if (si->si_udev == udev) 214 return (si); 215 } 216 si = allocdev(); 217 si->si_udev = udev; 218 LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash); 219 return (si); 220 } 221 222 void 223 freedev(dev_t dev) 224 { 225 226 if (!free_devt) 227 return; 228 if (SLIST_FIRST(&dev->si_hlist)) 229 return; 230 if (dev->si_devsw || dev->si_drv1 || dev->si_drv2) 231 return; 232 LIST_REMOVE(dev, si_hash); 233 if (dev->si_flags & SI_STASHED) { 234 bzero(dev, sizeof(*dev)); 235 dev->si_flags |= SI_STASHED; 236 LIST_INSERT_HEAD(&dev_free, dev, si_hash); 237 } else { 238 FREE(dev, M_DEVT); 239 } 240 } 241 242 udev_t 243 dev2udev(dev_t x) 244 { 245 if (x == NODEV) 246 return NOUDEV; 247 return (x->si_udev); 248 } 249 250 dev_t 251 udev2dev(udev_t x, int b) 252 { 253 254 if (x == NOUDEV) 255 return (NODEV); 256 switch (b) { 257 case 0: 258 return makedev(umajor(x), uminor(x)); 259 case 1: 260 return (NODEV); 261 default: 262 Debugger("udev2dev(...,X)"); 263 return NODEV; 264 } 265 } 266 267 int 268 uminor(udev_t dev) 269 { 270 return(dev & 0xffff00ff); 271 } 272 273 int 274 umajor(udev_t dev) 275 { 276 return((dev & 0xff00) >> 8); 277 } 278 279 udev_t 280 makeudev(int x, int y) 281 { 282 return ((x << 8) | y); 283 } 284 285 dev_t 286 make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, const char *fmt, ...) 287 { 288 dev_t dev; 289 va_list ap; 290 int i; 291 292 KASSERT(umajor(makeudev(devsw->d_maj, minor)) == devsw->d_maj, 293 ("Invalid minor (%d) in make_dev", minor)); 294 295 if (!ready_for_devs) { 296 printf("WARNING: Driver mistake: make_dev(%s) called before SI_SUB_DRIVERS\n", 297 fmt); 298 /* XXX panic here once drivers are cleaned up */ 299 } 300 301 dev = makedev(devsw->d_maj, minor); 302 if (dev->si_flags & SI_NAMED) { 303 printf( "WARNING: Driver mistake: repeat make_dev(\"%s\")\n", 304 dev->si_name); 305 panic("don't do that"); 306 return (dev); 307 } 308 va_start(ap, fmt); 309 i = kvprintf(fmt, NULL, dev->si_name, 32, ap); 310 dev->si_name[i] = '\0'; 311 va_end(ap); 312 dev->si_devsw = devsw; 313 dev->si_uid = uid; 314 dev->si_gid = gid; 315 dev->si_mode = perms; 316 dev->si_flags |= SI_NAMED; 317 318 if (devfs_create_hook) 319 devfs_create_hook(dev); 320 return (dev); 321 } 322 323 int 324 dev_named(dev_t pdev, const char *name) 325 { 326 dev_t cdev; 327 328 if (strcmp(devtoname(pdev), name) == 0) 329 return (1); 330 LIST_FOREACH(cdev, &pdev->si_children, si_siblings) 331 if (strcmp(devtoname(cdev), name) == 0) 332 return (1); 333 return (0); 334 } 335 336 void 337 dev_depends(dev_t pdev, dev_t cdev) 338 { 339 340 cdev->si_parent = pdev; 341 cdev->si_flags |= SI_CHILD; 342 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings); 343 } 344 345 dev_t 346 make_dev_alias(dev_t pdev, const char *fmt, ...) 347 { 348 dev_t dev; 349 va_list ap; 350 int i; 351 352 dev = allocdev(); 353 dev->si_flags |= SI_ALIAS; 354 dev->si_flags |= SI_NAMED; 355 dev_depends(pdev, dev); 356 va_start(ap, fmt); 357 i = kvprintf(fmt, NULL, dev->si_name, 32, ap); 358 dev->si_name[i] = '\0'; 359 va_end(ap); 360 361 if (devfs_create_hook) 362 devfs_create_hook(dev); 363 return (dev); 364 } 365 366 void 367 revoke_and_destroy_dev(dev_t dev) 368 { 369 struct vnode *vp; 370 371 GIANT_REQUIRED; 372 373 vp = SLIST_FIRST(&dev->si_hlist); 374 if (vp != NULL) 375 VOP_REVOKE(vp, REVOKEALL); 376 destroy_dev(dev); 377 } 378 379 void 380 destroy_dev(dev_t dev) 381 { 382 383 if (!(dev->si_flags & SI_NAMED)) { 384 printf( "WARNING: Driver mistake: destroy_dev on %d/%d\n", 385 major(dev), minor(dev)); 386 panic("don't do that"); 387 return; 388 } 389 390 if (devfs_destroy_hook) 391 devfs_destroy_hook(dev); 392 if (dev->si_flags & SI_CHILD) { 393 LIST_REMOVE(dev, si_siblings); 394 dev->si_flags &= ~SI_CHILD; 395 } 396 while (!LIST_EMPTY(&dev->si_children)) 397 destroy_dev(LIST_FIRST(&dev->si_children)); 398 dev->si_drv1 = 0; 399 dev->si_drv2 = 0; 400 dev->si_devsw = 0; 401 dev->si_flags &= ~SI_NAMED; 402 dev->si_flags &= ~SI_ALIAS; 403 freedev(dev); 404 } 405 406 const char * 407 devtoname(dev_t dev) 408 { 409 char *p; 410 int mynor; 411 412 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') { 413 p = dev->si_name; 414 if (devsw(dev)) 415 sprintf(p, "#%s/", devsw(dev)->d_name); 416 else 417 sprintf(p, "#%d/", major(dev)); 418 p += strlen(p); 419 mynor = minor(dev); 420 if (mynor < 0 || mynor > 255) 421 sprintf(p, "%#x", (u_int)mynor); 422 else 423 sprintf(p, "%d", mynor); 424 } 425 return (dev->si_name); 426 } 427 428 int 429 dev_stdclone(char *name, char **namep, char *stem, int *unit) 430 { 431 int u, i; 432 433 i = strlen(stem); 434 if (bcmp(stem, name, i) != 0) 435 return (0); 436 if (!isdigit(name[i])) 437 return (0); 438 u = 0; 439 if (name[i] == '0' && isdigit(name[i+1])) 440 return (0); 441 while (isdigit(name[i])) { 442 u *= 10; 443 u += name[i++] - '0'; 444 } 445 *unit = u; 446 if (namep) 447 *namep = &name[i]; 448 if (name[i]) 449 return (2); 450 return (1); 451 } 452 453 /* 454 * Helper sysctl for devname(3). We're given a {u}dev_t and return 455 * the name, if any, registered by the device driver. 456 */ 457 static int 458 sysctl_devname(SYSCTL_HANDLER_ARGS) 459 { 460 int error; 461 udev_t ud; 462 dev_t dev; 463 464 error = SYSCTL_IN(req, &ud, sizeof (ud)); 465 if (error) 466 return (error); 467 if (ud == NOUDEV) 468 return(EINVAL); 469 dev = makedev(umajor(ud), uminor(ud)); 470 if (dev->si_name[0] == '\0') 471 error = ENOENT; 472 else 473 error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1); 474 freedev(dev); 475 return (error); 476 } 477 478 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY, 479 NULL, 0, sysctl_devname, "", "devname(3) handler"); 480 481 /* 482 * Set ready_for_devs; prior to this point, device creation is not allowed. 483 */ 484 static void 485 dev_set_ready(void *junk) 486 { 487 ready_for_devs = 1; 488 } 489 490 SYSINIT(dev_ready, SI_SUB_DEVFS, SI_ORDER_FIRST, dev_set_ready, NULL); 491