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: src/sys/kern/kern_conf.c,v 1.73.2.3 2003/03/10 02:18:25 imp Exp $ 34 * $DragonFly: src/sys/kern/kern_conf.c,v 1.23 2007/05/09 00:53:34 dillon Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/kernel.h> 39 #include <sys/sysctl.h> 40 #include <sys/systm.h> 41 #include <sys/module.h> 42 #include <sys/malloc.h> 43 #include <sys/conf.h> 44 #include <sys/vnode.h> 45 #include <sys/queue.h> 46 #include <sys/device.h> 47 #include <machine/stdarg.h> 48 49 #include <sys/sysref2.h> 50 51 static void cdev_terminate(struct cdev *dev); 52 53 MALLOC_DEFINE(M_DEVT, "cdev_t", "dev_t storage"); 54 55 /* 56 * SYSREF Integration - reference counting, allocation, 57 * sysid and syslink integration. 58 */ 59 static struct sysref_class cdev_sysref_class = { 60 .name = "cdev", 61 .mtype = M_DEVT, 62 .proto = SYSREF_PROTO_DEV, 63 .offset = offsetof(struct cdev, si_sysref), 64 .objsize = sizeof(struct cdev), 65 .mag_capacity = 32, 66 .flags = 0, 67 .ops = { 68 .terminate = (sysref_terminate_func_t)cdev_terminate 69 } 70 }; 71 72 /* 73 * This is the number of hash-buckets. Experiements with 'real-life' 74 * udev_t's show that a prime halfway between two powers of two works 75 * best. 76 */ 77 #define DEVT_HASH 128 /* must be power of 2 */ 78 static LIST_HEAD(, cdev) dev_hash[DEVT_HASH]; 79 80 static int free_devt; 81 SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, ""); 82 int dev_ref_debug = 0; 83 SYSCTL_INT(_debug, OID_AUTO, dev_refs, CTLFLAG_RW, &dev_ref_debug, 0, ""); 84 85 /* 86 * cdev_t and u_dev_t primitives. Note that the major number is always 87 * extracted from si_umajor, not from si_devsw, because si_devsw is replaced 88 * when a device is destroyed. 89 */ 90 int 91 major(cdev_t dev) 92 { 93 if (dev == NULL) 94 return NOUDEV; 95 return(dev->si_umajor); 96 } 97 98 int 99 minor(cdev_t dev) 100 { 101 if (dev == NULL) 102 return NOUDEV; 103 return(dev->si_uminor); 104 } 105 106 /* 107 * Compatibility function with old udev_t format to convert the 108 * non-consecutive minor space into a consecutive minor space. 109 */ 110 int 111 lminor(cdev_t dev) 112 { 113 int y; 114 115 if (dev == NULL) 116 return NOUDEV; 117 y = dev->si_uminor; 118 if (y & 0x0000ff00) 119 return NOUDEV; 120 return ((y & 0xff) | (y >> 8)); 121 } 122 123 /* 124 * This is a bit complex because devices are always created relative to 125 * a particular cdevsw, including 'hidden' cdevsw's (such as the raw device 126 * backing a disk subsystem overlay), so we have to compare both the 127 * devsw and udev fields to locate the correct device. 128 * 129 * The device is created if it does not already exist. If SI_ADHOC is not 130 * set the device will be referenced (once) and SI_ADHOC will be set. 131 * The caller must explicitly add additional references to the device if 132 * the caller wishes to track additional references. 133 * 134 * NOTE: The passed ops vector must normally match the device. This is 135 * because the kernel may create shadow devices that are INVISIBLE TO 136 * USERLAND. For example, the block device backing a disk is created 137 * as a shadow underneath the user-visible disklabel management device. 138 * Sometimes a device ops vector can be overridden, such as by /dev/console. 139 * In this case and this case only we allow a match when the ops vector 140 * otherwise would not match. 141 */ 142 static 143 int 144 __devthash(int x, int y) 145 { 146 return(((x << 2) ^ y) & (DEVT_HASH - 1)); 147 } 148 149 static 150 cdev_t 151 hashdev(struct dev_ops *ops, int x, int y, int allow_intercept) 152 { 153 struct cdev *si; 154 int hash; 155 156 hash = __devthash(x, y); 157 LIST_FOREACH(si, &dev_hash[hash], si_hash) { 158 if (si->si_umajor == x && si->si_uminor == y) { 159 if (si->si_ops == ops) 160 return (si); 161 if (allow_intercept && (si->si_flags & SI_INTERCEPTED)) 162 return (si); 163 } 164 } 165 si = sysref_alloc(&cdev_sysref_class); 166 si->si_ops = ops; 167 si->si_flags |= SI_HASHED | SI_ADHOC; 168 si->si_umajor = x; 169 si->si_uminor = y; 170 LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash); 171 sysref_activate(&si->si_sysref); 172 173 dev_dclone(si); 174 if (ops != &dead_dev_ops) 175 ++ops->head.refs; 176 if (dev_ref_debug) { 177 kprintf("create dev %p %s(minor=%08x) refs=%d\n", 178 si, devtoname(si), y, 179 si->si_sysref.refcnt); 180 } 181 return (si); 182 } 183 184 /* 185 * Convert a device pointer to an old style device number. Return NOUDEV 186 * if the device is invalid or if the device (maj,min) cannot be converted 187 * to an old style udev_t. 188 */ 189 udev_t 190 dev2udev(cdev_t dev) 191 { 192 if (dev == NULL) 193 return NOUDEV; 194 if ((dev->si_umajor & 0xffffff00) || 195 (dev->si_uminor & 0x0000ff00)) { 196 return NOUDEV; 197 } 198 return((dev->si_umajor << 8) | dev->si_uminor); 199 } 200 201 /* 202 * Convert a device number to a device pointer. The device is referenced 203 * ad-hoc, meaning that the caller should call reference_dev() if it wishes 204 * to keep ahold of the returned structure long term. 205 * 206 * The returned device is associated with the currently installed cdevsw 207 * for the requested major number. NULL is returned if the major number 208 * has not been registered. 209 */ 210 cdev_t 211 udev2dev(udev_t x, int b) 212 { 213 cdev_t dev; 214 struct dev_ops *ops; 215 216 if (x == NOUDEV || b != 0) 217 return(NULL); 218 ops = dev_ops_get(umajor(x), uminor(x)); 219 if (ops == NULL) 220 return(NULL); 221 dev = hashdev(ops, umajor(x), uminor(x), TRUE); 222 return(dev); 223 } 224 225 int 226 dev_is_good(cdev_t dev) 227 { 228 if (dev != NULL && dev->si_ops != &dead_dev_ops) 229 return(1); 230 return(0); 231 } 232 233 /* 234 * Various user device number extraction and conversion routines 235 */ 236 int 237 uminor(udev_t dev) 238 { 239 if (dev == NOUDEV) 240 return(-1); 241 return(dev & 0xffff00ff); 242 } 243 244 int 245 umajor(udev_t dev) 246 { 247 if (dev == NOUDEV) 248 return(-1); 249 return((dev & 0xff00) >> 8); 250 } 251 252 udev_t 253 makeudev(int x, int y) 254 { 255 if ((x & 0xffffff00) || (y & 0x0000ff00)) 256 return NOUDEV; 257 return ((x << 8) | y); 258 } 259 260 /* 261 * Create an internal or external device. 262 * 263 * Device majors can be overloaded and used directly by the kernel without 264 * conflict, but userland will only see the particular device major that 265 * has been installed with dev_ops_add(). 266 * 267 * This routine creates and returns an unreferenced ad-hoc entry for the 268 * device which will remain intact until the device is destroyed. If the 269 * caller intends to store the device pointer it must call reference_dev() 270 * to retain a real reference to the device. 271 * 272 * If an entry already exists, this function will set (or override) 273 * its cred requirements and name (XXX DEVFS interface). 274 */ 275 cdev_t 276 make_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid, 277 int perms, const char *fmt, ...) 278 { 279 cdev_t dev; 280 __va_list ap; 281 int i; 282 283 /* 284 * compile the cdevsw and install the device 285 */ 286 compile_dev_ops(ops); 287 dev = hashdev(ops, ops->head.maj, minor, FALSE); 288 289 /* 290 * Set additional fields (XXX DEVFS interface goes here) 291 */ 292 __va_start(ap, fmt); 293 i = kvcprintf(fmt, NULL, dev->si_name, 32, ap); 294 dev->si_name[i] = '\0'; 295 dev->si_uid = uid; 296 __va_end(ap); 297 298 return (dev); 299 } 300 301 /* 302 * This function is similar to make_dev() but no cred information or name 303 * need be specified. 304 */ 305 cdev_t 306 make_adhoc_dev(struct dev_ops *ops, int minor) 307 { 308 cdev_t dev; 309 310 dev = hashdev(ops, ops->head.maj, minor, FALSE); 311 return(dev); 312 } 313 314 /* 315 * This function is similar to make_dev() except the new device is created 316 * using an old device as a template. 317 */ 318 cdev_t 319 make_sub_dev(cdev_t odev, int minor) 320 { 321 cdev_t dev; 322 323 dev = hashdev(odev->si_ops, odev->si_umajor, minor, FALSE); 324 325 /* 326 * Copy cred requirements and name info XXX DEVFS. 327 */ 328 if (dev->si_name[0] == 0 && odev->si_name[0]) 329 bcopy(odev->si_name, dev->si_name, sizeof(dev->si_name)); 330 return (dev); 331 } 332 333 cdev_t 334 get_dev(int x, int y) 335 { 336 cdev_t dev; 337 struct dev_ops *ops; 338 339 if (x == NOUDEV) 340 return(NULL); 341 ops = dev_ops_get(x, y); 342 if (ops == NULL) 343 return(NULL); 344 dev = hashdev(ops, x, y, TRUE); 345 return(dev); 346 } 347 348 /* 349 * destroy_dev() removes the adhoc association for a device and revectors 350 * its ops to &dead_dev_ops. 351 * 352 * This routine releases the reference count associated with the ADHOC 353 * entry, plus releases the reference count held by the caller. What this 354 * means is that you should not call destroy_dev(make_dev(...)), because 355 * make_dev() does not bump the reference count (beyond what it needs to 356 * create the ad-hoc association). Any procedure that intends to destroy 357 * a device must have its own reference to it first. 358 */ 359 void 360 destroy_dev(cdev_t dev) 361 { 362 int hash; 363 364 if (dev == NULL) 365 return; 366 if ((dev->si_flags & SI_ADHOC) == 0) { 367 release_dev(dev); 368 return; 369 } 370 if (dev_ref_debug) { 371 kprintf("destroy dev %p %s(minor=%08x) refs=%d\n", 372 dev, devtoname(dev), dev->si_uminor, 373 dev->si_sysref.refcnt); 374 } 375 if (dev->si_sysref.refcnt < 2) { 376 kprintf("destroy_dev(): too few references on device! " 377 "%p %s(minor=%08x) refs=%d\n", 378 dev, devtoname(dev), dev->si_uminor, 379 dev->si_sysref.refcnt); 380 } 381 dev->si_flags &= ~SI_ADHOC; 382 if (dev->si_flags & SI_HASHED) { 383 hash = __devthash(dev->si_umajor, dev->si_uminor); 384 LIST_REMOVE(dev, si_hash); 385 dev->si_flags &= ~SI_HASHED; 386 } 387 388 /* 389 * We have to release the ops reference before we replace the 390 * device switch with dead_dev_ops. 391 */ 392 if (dead_dev_ops.d_strategy == NULL) 393 compile_dev_ops(&dead_dev_ops); 394 if (dev->si_ops && dev->si_ops != &dead_dev_ops) 395 dev_ops_release(dev->si_ops); 396 dev->si_drv1 = NULL; 397 dev->si_drv2 = NULL; 398 dev->si_ops = &dead_dev_ops; 399 sysref_put(&dev->si_sysref); /* release adhoc association */ 400 release_dev(dev); /* release callers reference */ 401 } 402 403 /* 404 * Destroy all ad-hoc device associations associated with a domain within a 405 * device switch. Only the minor numbers are included in the mask/match 406 * values. 407 * 408 * Unlike the ops functions whos link structures do not contain 409 * any major bits, this function scans through the dev list via 410 * si_umajor/si_uminor. 411 * 412 * The caller must not include any major bits in the match value. 413 */ 414 void 415 destroy_all_devs(struct dev_ops *ops, u_int mask, u_int match) 416 { 417 int i; 418 cdev_t dev; 419 cdev_t ndev; 420 421 for (i = 0; i < DEVT_HASH; ++i) { 422 ndev = LIST_FIRST(&dev_hash[i]); 423 while ((dev = ndev) != NULL) { 424 ndev = LIST_NEXT(dev, si_hash); 425 if (dev->si_ops == ops && 426 ((u_int)dev->si_uminor & mask) == match 427 ) { 428 KKASSERT(dev->si_flags & SI_ADHOC); 429 reference_dev(dev); 430 destroy_dev(dev); 431 } 432 } 433 } 434 } 435 436 /* 437 * Add a reference to a device. Callers generally add their own references 438 * when they are going to store a device node in a variable for long periods 439 * of time, to prevent a disassociation from free()ing the node. 440 * 441 * Also note that a caller that intends to call destroy_dev() must first 442 * obtain a reference on the device. The ad-hoc reference you get with 443 * make_dev() and friends is NOT sufficient to be able to call destroy_dev(). 444 */ 445 cdev_t 446 reference_dev(cdev_t dev) 447 { 448 if (dev != NULL) { 449 sysref_get(&dev->si_sysref); 450 if (dev_ref_debug) { 451 kprintf("reference dev %p %s(minor=%08x) refs=%d\n", 452 dev, devtoname(dev), dev->si_uminor, 453 dev->si_sysref.refcnt); 454 } 455 } 456 return(dev); 457 } 458 459 /* 460 * release a reference on a device. The device will be terminated when the 461 * last reference has been released. 462 * 463 * NOTE: we must use si_umajor to figure out the original major number, 464 * because si_ops could already be pointing at dead_dev_ops. 465 */ 466 void 467 release_dev(cdev_t dev) 468 { 469 if (dev == NULL) 470 return; 471 sysref_put(&dev->si_sysref); 472 } 473 474 static 475 void 476 cdev_terminate(struct cdev *dev) 477 { 478 int messedup = 0; 479 480 if (dev_ref_debug) { 481 kprintf("release dev %p %s(minor=%08x) refs=%d\n", 482 dev, devtoname(dev), dev->si_uminor, 483 dev->si_sysref.refcnt); 484 } 485 if (dev->si_flags & SI_ADHOC) { 486 kprintf("Warning: illegal final release on ADHOC" 487 " device %p(%s), the device was never" 488 " destroyed!\n", 489 dev, devtoname(dev)); 490 messedup = 1; 491 } 492 if (dev->si_flags & SI_HASHED) { 493 kprintf("Warning: last release on device, no call" 494 " to destroy_dev() was made! dev %p(%s)\n", 495 dev, devtoname(dev)); 496 reference_dev(dev); 497 destroy_dev(dev); 498 messedup = 1; 499 } 500 if (SLIST_FIRST(&dev->si_hlist) != NULL) { 501 kprintf("Warning: last release on device, vnode" 502 " associations still exist! dev %p(%s)\n", 503 dev, devtoname(dev)); 504 messedup = 1; 505 } 506 if (dev->si_ops && dev->si_ops != &dead_dev_ops) { 507 dev_ops_release(dev->si_ops); 508 dev->si_ops = NULL; 509 } 510 if (messedup == 0) 511 sysref_put(&dev->si_sysref); 512 } 513 514 const char * 515 devtoname(cdev_t dev) 516 { 517 int mynor; 518 int len; 519 char *p; 520 const char *dname; 521 522 if (dev == NULL) 523 return("#nodev"); 524 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') { 525 p = dev->si_name; 526 len = sizeof(dev->si_name); 527 if ((dname = dev_dname(dev)) != NULL) 528 ksnprintf(p, len, "#%s/", dname); 529 else 530 ksnprintf(p, len, "#%d/", major(dev)); 531 len -= strlen(p); 532 p += strlen(p); 533 mynor = minor(dev); 534 if (mynor < 0 || mynor > 255) 535 ksnprintf(p, len, "%#x", (u_int)mynor); 536 else 537 ksnprintf(p, len, "%d", mynor); 538 } 539 return (dev->si_name); 540 } 541 542