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 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 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 * $FreeBSD$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/kernel.h> 31 #include <sys/systm.h> 32 #include <sys/bio.h> 33 #include <sys/lock.h> 34 #include <sys/mutex.h> 35 #include <sys/sysctl.h> 36 #include <sys/module.h> 37 #include <sys/malloc.h> 38 #include <sys/conf.h> 39 #include <sys/vnode.h> 40 #include <sys/queue.h> 41 #include <sys/ctype.h> 42 #include <machine/stdarg.h> 43 44 static MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage"); 45 46 /* Built at compile time from sys/conf/majors */ 47 extern unsigned char reserved_majors[256]; 48 49 /* 50 * This is the number of hash-buckets. Experiements with 'real-life' 51 * udev_t's show that a prime halfway between two powers of two works 52 * best. 53 */ 54 #define DEVT_HASH 83 55 56 /* The number of dev_t's we can create before malloc(9) kick in. */ 57 #define DEVT_STASH 50 58 59 static struct cdev devt_stash[DEVT_STASH]; 60 61 static LIST_HEAD(, cdev) dev_hash[DEVT_HASH]; 62 63 static LIST_HEAD(, cdev) dev_free; 64 65 static int ready_for_devs; 66 67 static int free_devt; 68 SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, ""); 69 70 /* Define a dead_cdevsw for use when devices leave unexpectedly. */ 71 72 static int 73 enxio(void) 74 { 75 return (ENXIO); 76 } 77 78 #define dead_open (d_open_t *)enxio 79 #define dead_close (d_close_t *)enxio 80 #define dead_read (d_read_t *)enxio 81 #define dead_write (d_write_t *)enxio 82 #define dead_ioctl (d_ioctl_t *)enxio 83 #define dead_poll nopoll 84 #define dead_mmap nommap 85 86 static void 87 dead_strategy(struct bio *bp) 88 { 89 90 biofinish(bp, NULL, ENXIO); 91 } 92 93 #define dead_dump (dumper_t *)enxio 94 95 #define dead_kqfilter (d_kqfilter_t *)enxio 96 97 static struct cdevsw dead_cdevsw = { 98 .d_open = dead_open, 99 .d_close = dead_close, 100 .d_read = dead_read, 101 .d_write = dead_write, 102 .d_ioctl = dead_ioctl, 103 .d_poll = dead_poll, 104 .d_mmap = dead_mmap, 105 .d_strategy = dead_strategy, 106 .d_name = "dead", 107 .d_maj = 255, 108 .d_dump = dead_dump, 109 .d_kqfilter = dead_kqfilter 110 }; 111 112 113 struct cdevsw * 114 devsw(dev_t dev) 115 { 116 if (dev->si_devsw) 117 return (dev->si_devsw); 118 return (&dead_cdevsw); 119 } 120 121 /* 122 * dev_t and u_dev_t primitives 123 */ 124 125 int 126 major(dev_t x) 127 { 128 if (x == NODEV) 129 return NOUDEV; 130 return((x->si_udev >> 8) & 0xff); 131 } 132 133 int 134 minor(dev_t x) 135 { 136 if (x == NODEV) 137 return NOUDEV; 138 return(x->si_udev & 0xffff00ff); 139 } 140 141 int 142 dev2unit(dev_t x) 143 { 144 int i; 145 146 if (x == NODEV) 147 return NOUDEV; 148 i = minor(x); 149 return ((i & 0xff) | (i >> 8)); 150 } 151 152 int 153 unit2minor(int unit) 154 { 155 156 KASSERT(unit <= 0xffffff, ("Invalid unit (%d) in unit2minor", unit)); 157 return ((unit & 0xff) | ((unit << 8) & ~0xffff)); 158 } 159 160 static dev_t 161 allocdev(void) 162 { 163 static int stashed; 164 struct cdev *si; 165 166 if (LIST_FIRST(&dev_free)) { 167 si = LIST_FIRST(&dev_free); 168 LIST_REMOVE(si, si_hash); 169 } else if (stashed >= DEVT_STASH) { 170 MALLOC(si, struct cdev *, sizeof(*si), M_DEVT, 171 M_USE_RESERVE | M_ZERO | M_WAITOK); 172 } else { 173 si = devt_stash + stashed++; 174 bzero(si, sizeof *si); 175 si->si_flags |= SI_STASHED; 176 } 177 si->__si_namebuf[0] = '\0'; 178 si->si_name = si->__si_namebuf; 179 LIST_INIT(&si->si_children); 180 TAILQ_INIT(&si->si_snapshots); 181 return (si); 182 } 183 184 dev_t 185 makedev(int x, int y) 186 { 187 struct cdev *si; 188 udev_t udev; 189 int hash; 190 191 if (x == umajor(NOUDEV) && y == uminor(NOUDEV)) 192 panic("makedev of NOUDEV"); 193 udev = (x << 8) | y; 194 hash = udev % DEVT_HASH; 195 LIST_FOREACH(si, &dev_hash[hash], si_hash) { 196 if (si->si_udev == udev) 197 return (si); 198 } 199 si = allocdev(); 200 si->si_udev = udev; 201 LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash); 202 return (si); 203 } 204 205 void 206 freedev(dev_t dev) 207 { 208 209 if (!free_devt) 210 return; 211 if (SLIST_FIRST(&dev->si_hlist)) 212 return; 213 if (dev->si_devsw || dev->si_drv1 || dev->si_drv2) 214 return; 215 LIST_REMOVE(dev, si_hash); 216 if (dev->si_flags & SI_STASHED) { 217 bzero(dev, sizeof(*dev)); 218 dev->si_flags |= SI_STASHED; 219 LIST_INSERT_HEAD(&dev_free, dev, si_hash); 220 } else { 221 FREE(dev, M_DEVT); 222 } 223 } 224 225 udev_t 226 dev2udev(dev_t x) 227 { 228 if (x == NODEV) 229 return NOUDEV; 230 return (x->si_udev); 231 } 232 233 dev_t 234 udev2dev(udev_t x, int b) 235 { 236 237 if (x == NOUDEV) 238 return (NODEV); 239 switch (b) { 240 case 0: 241 return makedev(umajor(x), uminor(x)); 242 case 1: 243 return (NODEV); 244 default: 245 Debugger("udev2dev(...,X)"); 246 return NODEV; 247 } 248 } 249 250 int 251 uminor(udev_t dev) 252 { 253 return(dev & 0xffff00ff); 254 } 255 256 int 257 umajor(udev_t dev) 258 { 259 return((dev & 0xff00) >> 8); 260 } 261 262 udev_t 263 makeudev(int x, int y) 264 { 265 return ((x << 8) | y); 266 } 267 268 dev_t 269 make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, const char *fmt, ...) 270 { 271 dev_t dev; 272 va_list ap; 273 int i; 274 275 KASSERT((minor & ~0xffff00ff) == 0, 276 ("Invalid minor (0x%x) in make_dev", minor)); 277 278 if (devsw->d_open == NULL) devsw->d_open = noopen; 279 if (devsw->d_close == NULL) devsw->d_close = noclose; 280 if (devsw->d_read == NULL) devsw->d_read = noread; 281 if (devsw->d_write == NULL) devsw->d_write = nowrite; 282 if (devsw->d_ioctl == NULL) devsw->d_ioctl = noioctl; 283 if (devsw->d_poll == NULL) devsw->d_poll = nopoll; 284 if (devsw->d_mmap == NULL) devsw->d_mmap = nommap; 285 if (devsw->d_strategy == NULL) devsw->d_strategy = nostrategy; 286 if (devsw->d_dump == NULL) devsw->d_dump = nodump; 287 if (devsw->d_kqfilter == NULL) devsw->d_kqfilter = nokqfilter; 288 289 if (devsw->d_maj == MAJOR_AUTO) { 290 for (i = NUMCDEVSW - 1; i > 0; i--) 291 if (reserved_majors[i] != i) 292 break; 293 KASSERT(i > 0, ("Out of major numbers (%s)", devsw->d_name)); 294 printf("Allocating major#%d to \"%s\"\n", i, devsw->d_name); 295 devsw->d_maj = i; 296 reserved_majors[i] = i; 297 } else { 298 if (devsw->d_maj == 256) /* XXX: tty_cons.c is magic */ 299 devsw->d_maj = 0; 300 KASSERT(devsw->d_maj >= 0 && devsw->d_maj < 256, 301 ("Invalid major (%d) in make_dev", devsw->d_maj)); 302 if (reserved_majors[devsw->d_maj] != devsw->d_maj) { 303 printf("WARNING: driver \"%s\" used %s %d\n", 304 devsw->d_name, "unreserved major device number", 305 devsw->d_maj); 306 reserved_majors[devsw->d_maj] = devsw->d_maj; 307 } 308 } 309 310 if (!ready_for_devs) { 311 printf("WARNING: Driver mistake: make_dev(%s) called before SI_SUB_DRIVERS\n", 312 fmt); 313 /* XXX panic here once drivers are cleaned up */ 314 } 315 316 dev = makedev(devsw->d_maj, minor); 317 if (dev->si_flags & SI_NAMED) { 318 printf( "WARNING: Driver mistake: repeat make_dev(\"%s\")\n", 319 dev->si_name); 320 panic("don't do that"); 321 return (dev); 322 } 323 va_start(ap, fmt); 324 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap); 325 if (i > (sizeof dev->__si_namebuf - 1)) { 326 printf("WARNING: Device name truncated! (%s)", 327 dev->__si_namebuf); 328 } 329 va_end(ap); 330 dev->si_devsw = devsw; 331 dev->si_uid = uid; 332 dev->si_gid = gid; 333 dev->si_mode = perms; 334 dev->si_flags |= SI_NAMED; 335 336 devfs_create(dev); 337 return (dev); 338 } 339 340 int 341 dev_named(dev_t pdev, const char *name) 342 { 343 dev_t cdev; 344 345 if (strcmp(devtoname(pdev), name) == 0) 346 return (1); 347 LIST_FOREACH(cdev, &pdev->si_children, si_siblings) 348 if (strcmp(devtoname(cdev), name) == 0) 349 return (1); 350 return (0); 351 } 352 353 void 354 dev_depends(dev_t pdev, dev_t cdev) 355 { 356 357 cdev->si_parent = pdev; 358 cdev->si_flags |= SI_CHILD; 359 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings); 360 } 361 362 dev_t 363 make_dev_alias(dev_t pdev, const char *fmt, ...) 364 { 365 dev_t dev; 366 va_list ap; 367 int i; 368 369 dev = allocdev(); 370 dev->si_flags |= SI_ALIAS; 371 dev->si_flags |= SI_NAMED; 372 dev_depends(pdev, dev); 373 va_start(ap, fmt); 374 i = vsnrprintf(dev->__si_namebuf, sizeof dev->__si_namebuf, 32, fmt, ap); 375 if (i > (sizeof dev->__si_namebuf - 1)) { 376 printf("WARNING: Device name truncated! (%s)", 377 dev->__si_namebuf); 378 } 379 va_end(ap); 380 381 devfs_create(dev); 382 return (dev); 383 } 384 385 void 386 revoke_and_destroy_dev(dev_t dev) 387 { 388 struct vnode *vp; 389 390 GIANT_REQUIRED; 391 392 vp = SLIST_FIRST(&dev->si_hlist); 393 if (vp != NULL) 394 VOP_REVOKE(vp, REVOKEALL); 395 destroy_dev(dev); 396 } 397 398 void 399 destroy_dev(dev_t dev) 400 { 401 402 if (!(dev->si_flags & SI_NAMED)) { 403 printf( "WARNING: Driver mistake: destroy_dev on %d/%d\n", 404 major(dev), minor(dev)); 405 panic("don't do that"); 406 return; 407 } 408 409 devfs_destroy(dev); 410 if (dev->si_flags & SI_CHILD) { 411 LIST_REMOVE(dev, si_siblings); 412 dev->si_flags &= ~SI_CHILD; 413 } 414 while (!LIST_EMPTY(&dev->si_children)) 415 destroy_dev(LIST_FIRST(&dev->si_children)); 416 dev->si_drv1 = 0; 417 dev->si_drv2 = 0; 418 dev->si_devsw = 0; 419 bzero(&dev->__si_u, sizeof(dev->__si_u)); 420 dev->si_flags &= ~SI_NAMED; 421 dev->si_flags &= ~SI_ALIAS; 422 freedev(dev); 423 } 424 425 const char * 426 devtoname(dev_t dev) 427 { 428 char *p; 429 int mynor; 430 431 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') { 432 p = dev->si_name; 433 if (devsw(dev)) 434 sprintf(p, "#%s/", devsw(dev)->d_name); 435 else 436 sprintf(p, "#%d/", major(dev)); 437 p += strlen(p); 438 mynor = minor(dev); 439 if (mynor < 0 || mynor > 255) 440 sprintf(p, "%#x", (u_int)mynor); 441 else 442 sprintf(p, "%d", mynor); 443 } 444 return (dev->si_name); 445 } 446 447 int 448 dev_stdclone(char *name, char **namep, const char *stem, int *unit) 449 { 450 int u, i; 451 452 i = strlen(stem); 453 if (bcmp(stem, name, i) != 0) 454 return (0); 455 if (!isdigit(name[i])) 456 return (0); 457 u = 0; 458 if (name[i] == '0' && isdigit(name[i+1])) 459 return (0); 460 while (isdigit(name[i])) { 461 u *= 10; 462 u += name[i++] - '0'; 463 } 464 if (u > 0xffffff) 465 return (0); 466 *unit = u; 467 if (namep) 468 *namep = &name[i]; 469 if (name[i]) 470 return (2); 471 return (1); 472 } 473 474 /* 475 * Helper sysctl for devname(3). We're given a {u}dev_t and return 476 * the name, if any, registered by the device driver. 477 */ 478 static int 479 sysctl_devname(SYSCTL_HANDLER_ARGS) 480 { 481 int error; 482 udev_t ud; 483 dev_t dev; 484 485 error = SYSCTL_IN(req, &ud, sizeof (ud)); 486 if (error) 487 return (error); 488 if (ud == NOUDEV) 489 return(EINVAL); 490 dev = makedev(umajor(ud), uminor(ud)); 491 if (dev->si_name[0] == '\0') 492 error = ENOENT; 493 else 494 error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1); 495 freedev(dev); 496 return (error); 497 } 498 499 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY, 500 NULL, 0, sysctl_devname, "", "devname(3) handler"); 501 502 /* 503 * Set ready_for_devs; prior to this point, device creation is not allowed. 504 */ 505 static void 506 dev_set_ready(void *junk) 507 { 508 ready_for_devs = 1; 509 } 510 511 SYSINIT(dev_ready, SI_SUB_DEVFS, SI_ORDER_FIRST, dev_set_ready, NULL); 512