1 /* 2 * Copyright (c) 2009 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Alex Hornung <ahornung@gmail.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/mount.h> 38 #include <sys/vnode.h> 39 #include <sys/types.h> 40 #include <sys/lock.h> 41 #include <sys/msgport.h> 42 #include <sys/msgport2.h> 43 #include <sys/spinlock2.h> 44 #include <sys/sysctl.h> 45 #include <sys/ucred.h> 46 #include <sys/param.h> 47 #include <sys/sysref2.h> 48 #include <sys/systm.h> 49 #include <sys/devfs.h> 50 #include <sys/devfs_rules.h> 51 #include <sys/hotplug.h> 52 53 MALLOC_DEFINE(M_DEVFS, "devfs", "Device File System (devfs) allocations"); 54 DEVFS_DECLARE_CLONE_BITMAP(ops_id); 55 /* 56 * SYSREF Integration - reference counting, allocation, 57 * sysid and syslink integration. 58 */ 59 static void devfs_cdev_terminate(cdev_t dev); 60 static void devfs_cdev_lock(cdev_t dev); 61 static void devfs_cdev_unlock(cdev_t dev); 62 static struct sysref_class cdev_sysref_class = { 63 .name = "cdev", 64 .mtype = M_DEVFS, 65 .proto = SYSREF_PROTO_DEV, 66 .offset = offsetof(struct cdev, si_sysref), 67 .objsize = sizeof(struct cdev), 68 .mag_capacity = 32, 69 .flags = 0, 70 .ops = { 71 .terminate = (sysref_terminate_func_t)devfs_cdev_terminate, 72 .lock = (sysref_lock_func_t)devfs_cdev_lock, 73 .unlock = (sysref_unlock_func_t)devfs_cdev_unlock 74 } 75 }; 76 77 static struct objcache *devfs_node_cache; 78 static struct objcache *devfs_msg_cache; 79 static struct objcache *devfs_dev_cache; 80 81 static struct objcache_malloc_args devfs_node_malloc_args = { 82 sizeof(struct devfs_node), M_DEVFS }; 83 struct objcache_malloc_args devfs_msg_malloc_args = { 84 sizeof(struct devfs_msg), M_DEVFS }; 85 struct objcache_malloc_args devfs_dev_malloc_args = { 86 sizeof(struct cdev), M_DEVFS }; 87 88 static struct devfs_dev_head devfs_dev_list = 89 TAILQ_HEAD_INITIALIZER(devfs_dev_list); 90 static struct devfs_mnt_head devfs_mnt_list = 91 TAILQ_HEAD_INITIALIZER(devfs_mnt_list); 92 static struct devfs_chandler_head devfs_chandler_list = 93 TAILQ_HEAD_INITIALIZER(devfs_chandler_list); 94 static struct devfs_alias_head devfs_alias_list = 95 TAILQ_HEAD_INITIALIZER(devfs_alias_list); 96 static struct devfs_dev_ops_head devfs_dev_ops_list = 97 TAILQ_HEAD_INITIALIZER(devfs_dev_ops_list); 98 99 struct lock devfs_lock; 100 static struct lwkt_port devfs_dispose_port; 101 static struct lwkt_port devfs_msg_port; 102 static struct thread *td_core; 103 104 static struct spinlock ino_lock; 105 static ino_t d_ino; 106 static int devfs_debug_enable; 107 static int devfs_run; 108 109 static ino_t devfs_fetch_ino(void); 110 static int devfs_create_all_dev_worker(struct devfs_node *); 111 static int devfs_create_dev_worker(cdev_t, uid_t, gid_t, int); 112 static int devfs_destroy_dev_worker(cdev_t); 113 static int devfs_destroy_subnames_worker(char *); 114 static int devfs_destroy_dev_by_ops_worker(struct dev_ops *, int); 115 static int devfs_propagate_dev(cdev_t, int); 116 static int devfs_unlink_dev(cdev_t dev); 117 static void devfs_msg_exec(devfs_msg_t msg); 118 119 static int devfs_chandler_add_worker(const char *, d_clone_t *); 120 static int devfs_chandler_del_worker(const char *); 121 122 static void devfs_msg_autofree_reply(lwkt_port_t, lwkt_msg_t); 123 static void devfs_msg_core(void *); 124 125 static int devfs_find_device_by_name_worker(devfs_msg_t); 126 static int devfs_find_device_by_udev_worker(devfs_msg_t); 127 128 static int devfs_apply_reset_rules_caller(char *, int); 129 130 static int devfs_scan_callback_worker(devfs_scan_t *); 131 132 static struct devfs_node *devfs_resolve_or_create_dir(struct devfs_node *, 133 char *, size_t, int); 134 135 static int devfs_make_alias_worker(struct devfs_alias *); 136 static int devfs_alias_remove(cdev_t); 137 static int devfs_alias_reap(void); 138 static int devfs_alias_propagate(struct devfs_alias *); 139 static int devfs_alias_apply(struct devfs_node *, struct devfs_alias *); 140 static int devfs_alias_check_create(struct devfs_node *); 141 142 static int devfs_clr_subnames_flag_worker(char *, uint32_t); 143 static int devfs_destroy_subnames_without_flag_worker(char *, uint32_t); 144 145 static void *devfs_reaperp_callback(struct devfs_node *, void *); 146 static void *devfs_gc_dirs_callback(struct devfs_node *, void *); 147 static void *devfs_gc_links_callback(struct devfs_node *, struct devfs_node *); 148 static void * 149 devfs_inode_to_vnode_worker_callback(struct devfs_node *, ino_t *); 150 151 /* hotplug */ 152 void (*devfs_node_added)(struct hotplug_device*) = NULL; 153 void (*devfs_node_removed)(struct hotplug_device*) = NULL; 154 155 /* 156 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function 157 * using kvprintf 158 */ 159 int 160 devfs_debug(int level, char *fmt, ...) 161 { 162 __va_list ap; 163 164 __va_start(ap, fmt); 165 if (level <= devfs_debug_enable) 166 kvprintf(fmt, ap); 167 __va_end(ap); 168 169 return 0; 170 } 171 172 /* 173 * devfs_allocp() Allocates a new devfs node with the specified 174 * parameters. The node is also automatically linked into the topology 175 * if a parent is specified. It also calls the rule and alias stuff to 176 * be applied on the new node 177 */ 178 struct devfs_node * 179 devfs_allocp(devfs_nodetype devfsnodetype, char *name, 180 struct devfs_node *parent, struct mount *mp, cdev_t dev) 181 { 182 struct devfs_node *node = NULL; 183 size_t namlen = strlen(name); 184 185 node = objcache_get(devfs_node_cache, M_WAITOK); 186 bzero(node, sizeof(*node)); 187 188 atomic_add_long(&(DEVFS_MNTDATA(mp)->leak_count), 1); 189 190 node->d_dev = NULL; 191 node->nchildren = 1; 192 node->mp = mp; 193 node->d_dir.d_ino = devfs_fetch_ino(); 194 195 /* 196 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries 197 * respectively. 198 */ 199 node->cookie_jar = 2; 200 201 /* 202 * Access Control members 203 */ 204 node->mode = DEVFS_DEFAULT_MODE; 205 node->uid = DEVFS_DEFAULT_UID; 206 node->gid = DEVFS_DEFAULT_GID; 207 208 switch (devfsnodetype) { 209 case Proot: 210 /* 211 * Ensure that we don't recycle the root vnode by marking it as 212 * linked into the topology. 213 */ 214 node->flags |= DEVFS_NODE_LINKED; 215 case Pdir: 216 TAILQ_INIT(DEVFS_DENODE_HEAD(node)); 217 node->d_dir.d_type = DT_DIR; 218 node->nchildren = 2; 219 break; 220 221 case Plink: 222 node->d_dir.d_type = DT_LNK; 223 break; 224 225 case Preg: 226 node->d_dir.d_type = DT_REG; 227 break; 228 229 case Pdev: 230 if (dev != NULL) { 231 node->d_dir.d_type = DT_CHR; 232 node->d_dev = dev; 233 234 node->mode = dev->si_perms; 235 node->uid = dev->si_uid; 236 node->gid = dev->si_gid; 237 238 devfs_alias_check_create(node); 239 } 240 break; 241 242 default: 243 panic("devfs_allocp: unknown node type"); 244 } 245 246 node->v_node = NULL; 247 node->node_type = devfsnodetype; 248 249 /* Initialize the dirent structure of each devfs vnode */ 250 KKASSERT(namlen < 256); 251 node->d_dir.d_namlen = namlen; 252 node->d_dir.d_name = kmalloc(namlen+1, M_DEVFS, M_WAITOK); 253 memcpy(node->d_dir.d_name, name, namlen); 254 node->d_dir.d_name[namlen] = '\0'; 255 256 /* Initialize the parent node element */ 257 node->parent = parent; 258 259 /* Apply rules */ 260 devfs_rule_check_apply(node, NULL); 261 262 /* Initialize *time members */ 263 nanotime(&node->atime); 264 node->mtime = node->ctime = node->atime; 265 266 /* 267 * Associate with parent as last step, clean out namecache 268 * reference. 269 */ 270 if ((parent != NULL) && 271 ((parent->node_type == Proot) || (parent->node_type == Pdir))) { 272 parent->nchildren++; 273 node->cookie = parent->cookie_jar++; 274 node->flags |= DEVFS_NODE_LINKED; 275 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent), node, link); 276 277 /* This forces negative namecache lookups to clear */ 278 ++mp->mnt_namecache_gen; 279 } 280 281 ++DEVFS_MNTDATA(mp)->file_count; 282 283 return node; 284 } 285 286 /* 287 * devfs_allocv() allocates a new vnode based on a devfs node. 288 */ 289 int 290 devfs_allocv(struct vnode **vpp, struct devfs_node *node) 291 { 292 struct vnode *vp; 293 int error = 0; 294 295 KKASSERT(node); 296 297 try_again: 298 while ((vp = node->v_node) != NULL) { 299 error = vget(vp, LK_EXCLUSIVE); 300 if (error != ENOENT) { 301 *vpp = vp; 302 goto out; 303 } 304 } 305 306 if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0) 307 goto out; 308 309 vp = *vpp; 310 311 if (node->v_node != NULL) { 312 vp->v_type = VBAD; 313 vx_put(vp); 314 goto try_again; 315 } 316 317 vp->v_data = node; 318 node->v_node = vp; 319 320 switch (node->node_type) { 321 case Proot: 322 vsetflags(vp, VROOT); 323 /* fall through */ 324 case Pdir: 325 vp->v_type = VDIR; 326 break; 327 328 case Plink: 329 vp->v_type = VLNK; 330 break; 331 332 case Preg: 333 vp->v_type = VREG; 334 break; 335 336 case Pdev: 337 vp->v_type = VCHR; 338 KKASSERT(node->d_dev); 339 340 vp->v_uminor = node->d_dev->si_uminor; 341 vp->v_umajor = 0; 342 343 v_associate_rdev(vp, node->d_dev); 344 vp->v_ops = &node->mp->mnt_vn_spec_ops; 345 break; 346 347 default: 348 panic("devfs_allocv: unknown node type"); 349 } 350 351 out: 352 return error; 353 } 354 355 /* 356 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode 357 * based on the newly created devfs node. 358 */ 359 int 360 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype, 361 char *name, struct devfs_node *parent, cdev_t dev) 362 { 363 struct devfs_node *node; 364 365 node = devfs_allocp(devfsnodetype, name, parent, mp, dev); 366 367 if (node != NULL) 368 devfs_allocv(vpp, node); 369 else 370 *vpp = NULL; 371 372 return 0; 373 } 374 375 /* 376 * Destroy the devfs_node. The node must be unlinked from the topology. 377 * 378 * This function will also destroy any vnode association with the node 379 * and device. 380 * 381 * The cdev_t itself remains intact. 382 */ 383 int 384 devfs_freep(struct devfs_node *node) 385 { 386 struct vnode *vp; 387 388 KKASSERT(node); 389 KKASSERT(((node->flags & DEVFS_NODE_LINKED) == 0) || 390 (node->node_type == Proot)); 391 KKASSERT((node->flags & DEVFS_DESTROYED) == 0); 392 393 atomic_subtract_long(&(DEVFS_MNTDATA(node->mp)->leak_count), 1); 394 if (node->symlink_name) { 395 kfree(node->symlink_name, M_DEVFS); 396 node->symlink_name = NULL; 397 } 398 399 /* 400 * Remove the node from the orphan list if it is still on it. 401 */ 402 if (node->flags & DEVFS_ORPHANED) 403 devfs_tracer_del_orphan(node); 404 405 /* 406 * Disassociate the vnode from the node. This also prevents the 407 * vnode's reclaim code from double-freeing the node. 408 * 409 * The vget is needed to safely modify the vp. It also serves 410 * to cycle the refs and terminate the vnode if it happens to 411 * be inactive, otherwise namecache references may not get cleared. 412 */ 413 while ((vp = node->v_node) != NULL) { 414 if (vget(vp, LK_EXCLUSIVE | LK_RETRY) != 0) 415 break; 416 v_release_rdev(vp); 417 vp->v_data = NULL; 418 node->v_node = NULL; 419 cache_inval_vp(vp, CINV_DESTROY); 420 vput(vp); 421 } 422 if (node->d_dir.d_name) { 423 kfree(node->d_dir.d_name, M_DEVFS); 424 node->d_dir.d_name = NULL; 425 } 426 node->flags |= DEVFS_DESTROYED; 427 428 --DEVFS_MNTDATA(node->mp)->file_count; 429 430 objcache_put(devfs_node_cache, node); 431 432 return 0; 433 } 434 435 /* 436 * Unlink the devfs node from the topology and add it to the orphan list. 437 * The node will later be destroyed by freep. 438 * 439 * Any vnode association, including the v_rdev and v_data, remains intact 440 * until the freep. 441 */ 442 int 443 devfs_unlinkp(struct devfs_node *node) 444 { 445 struct devfs_node *parent; 446 struct hotplug_device *hpdev; 447 KKASSERT(node); 448 449 /* 450 * Add the node to the orphan list, so it is referenced somewhere, to 451 * so we don't leak it. 452 */ 453 devfs_tracer_add_orphan(node); 454 455 parent = node->parent; 456 457 /* 458 * If the parent is known we can unlink the node out of the topology 459 */ 460 if (parent) { 461 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link); 462 parent->nchildren--; 463 KKASSERT((parent->nchildren >= 0)); 464 node->flags &= ~DEVFS_NODE_LINKED; 465 } 466 /* hotplug handler */ 467 if(devfs_node_removed) { 468 hpdev = kmalloc(sizeof(struct hotplug_device), M_TEMP, M_WAITOK); 469 hpdev->dev = node->d_dev; 470 if(hpdev->dev) 471 hpdev->name = node->d_dev->si_name; 472 devfs_node_removed(hpdev); 473 kfree(hpdev, M_TEMP); 474 } 475 node->parent = NULL; 476 return 0; 477 } 478 479 void * 480 devfs_iterate_topology(struct devfs_node *node, 481 devfs_iterate_callback_t *callback, void *arg1) 482 { 483 struct devfs_node *node1, *node2; 484 void *ret = NULL; 485 486 if ((node->node_type == Proot) || (node->node_type == Pdir)) { 487 if (node->nchildren > 2) { 488 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), 489 link, node2) { 490 if ((ret = devfs_iterate_topology(node1, callback, arg1))) 491 return ret; 492 } 493 } 494 } 495 496 ret = callback(node, arg1); 497 return ret; 498 } 499 500 /* 501 * devfs_reaperp() is a recursive function that iterates through all the 502 * topology, unlinking and freeing all devfs nodes. 503 */ 504 static void * 505 devfs_reaperp_callback(struct devfs_node *node, void *unused) 506 { 507 devfs_unlinkp(node); 508 devfs_freep(node); 509 510 return NULL; 511 } 512 513 static void * 514 devfs_gc_dirs_callback(struct devfs_node *node, void *unused) 515 { 516 if (node->node_type == Pdir) { 517 if (node->nchildren == 2) { 518 devfs_unlinkp(node); 519 devfs_freep(node); 520 } 521 } 522 523 return NULL; 524 } 525 526 static void * 527 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target) 528 { 529 if ((node->node_type == Plink) && (node->link_target == target)) { 530 devfs_unlinkp(node); 531 devfs_freep(node); 532 } 533 534 return NULL; 535 } 536 537 /* 538 * devfs_gc() is devfs garbage collector. It takes care of unlinking and 539 * freeing a node, but also removes empty directories and links that link 540 * via devfs auto-link mechanism to the node being deleted. 541 */ 542 int 543 devfs_gc(struct devfs_node *node) 544 { 545 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node; 546 547 if (node->nlinks > 0) 548 devfs_iterate_topology(root_node, 549 (devfs_iterate_callback_t *)devfs_gc_links_callback, node); 550 551 devfs_unlinkp(node); 552 devfs_iterate_topology(root_node, 553 (devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL); 554 555 devfs_freep(node); 556 557 return 0; 558 } 559 560 /* 561 * devfs_create_dev() is the asynchronous entry point for device creation. 562 * It just sends a message with the relevant details to the devfs core. 563 * 564 * This function will reference the passed device. The reference is owned 565 * by devfs and represents all of the device's node associations. 566 */ 567 int 568 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms) 569 { 570 reference_dev(dev); 571 devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms); 572 573 return 0; 574 } 575 576 /* 577 * devfs_destroy_dev() is the asynchronous entry point for device destruction. 578 * It just sends a message with the relevant details to the devfs core. 579 */ 580 int 581 devfs_destroy_dev(cdev_t dev) 582 { 583 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0); 584 return 0; 585 } 586 587 /* 588 * devfs_mount_add() is the synchronous entry point for adding a new devfs 589 * mount. It sends a synchronous message with the relevant details to the 590 * devfs core. 591 */ 592 int 593 devfs_mount_add(struct devfs_mnt_data *mnt) 594 { 595 devfs_msg_t msg; 596 597 msg = devfs_msg_get(); 598 msg->mdv_mnt = mnt; 599 msg = devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg); 600 devfs_msg_put(msg); 601 602 return 0; 603 } 604 605 /* 606 * devfs_mount_del() is the synchronous entry point for removing a devfs mount. 607 * It sends a synchronous message with the relevant details to the devfs core. 608 */ 609 int 610 devfs_mount_del(struct devfs_mnt_data *mnt) 611 { 612 devfs_msg_t msg; 613 614 msg = devfs_msg_get(); 615 msg->mdv_mnt = mnt; 616 msg = devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg); 617 devfs_msg_put(msg); 618 619 return 0; 620 } 621 622 /* 623 * devfs_destroy_subnames() is the synchronous entry point for device 624 * destruction by subname. It just sends a message with the relevant details to 625 * the devfs core. 626 */ 627 int 628 devfs_destroy_subnames(char *name) 629 { 630 devfs_msg_t msg; 631 632 msg = devfs_msg_get(); 633 msg->mdv_load = name; 634 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES, msg); 635 devfs_msg_put(msg); 636 return 0; 637 } 638 639 int 640 devfs_clr_subnames_flag(char *name, uint32_t flag) 641 { 642 devfs_msg_t msg; 643 644 msg = devfs_msg_get(); 645 msg->mdv_flags.name = name; 646 msg->mdv_flags.flag = flag; 647 msg = devfs_msg_send_sync(DEVFS_CLR_SUBNAMES_FLAG, msg); 648 devfs_msg_put(msg); 649 650 return 0; 651 } 652 653 int 654 devfs_destroy_subnames_without_flag(char *name, uint32_t flag) 655 { 656 devfs_msg_t msg; 657 658 msg = devfs_msg_get(); 659 msg->mdv_flags.name = name; 660 msg->mdv_flags.flag = flag; 661 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES_WO_FLAG, msg); 662 devfs_msg_put(msg); 663 664 return 0; 665 } 666 667 /* 668 * devfs_create_all_dev is the asynchronous entry point to trigger device 669 * node creation. It just sends a message with the relevant details to 670 * the devfs core. 671 */ 672 int 673 devfs_create_all_dev(struct devfs_node *root) 674 { 675 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root); 676 return 0; 677 } 678 679 /* 680 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all 681 * devices with a specific set of dev_ops and minor. It just sends a 682 * message with the relevant details to the devfs core. 683 */ 684 int 685 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor) 686 { 687 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor); 688 return 0; 689 } 690 691 /* 692 * devfs_clone_handler_add is the synchronous entry point to add a new 693 * clone handler. It just sends a message with the relevant details to 694 * the devfs core. 695 */ 696 int 697 devfs_clone_handler_add(const char *name, d_clone_t *nhandler) 698 { 699 devfs_msg_t msg; 700 701 msg = devfs_msg_get(); 702 msg->mdv_chandler.name = name; 703 msg->mdv_chandler.nhandler = nhandler; 704 msg = devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg); 705 devfs_msg_put(msg); 706 return 0; 707 } 708 709 /* 710 * devfs_clone_handler_del is the synchronous entry point to remove a 711 * clone handler. It just sends a message with the relevant details to 712 * the devfs core. 713 */ 714 int 715 devfs_clone_handler_del(const char *name) 716 { 717 devfs_msg_t msg; 718 719 msg = devfs_msg_get(); 720 msg->mdv_chandler.name = name; 721 msg->mdv_chandler.nhandler = NULL; 722 msg = devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg); 723 devfs_msg_put(msg); 724 return 0; 725 } 726 727 /* 728 * devfs_find_device_by_name is the synchronous entry point to find a 729 * device given its name. It sends a synchronous message with the 730 * relevant details to the devfs core and returns the answer. 731 */ 732 cdev_t 733 devfs_find_device_by_name(const char *fmt, ...) 734 { 735 cdev_t found = NULL; 736 devfs_msg_t msg; 737 char *target; 738 __va_list ap; 739 740 if (fmt == NULL) 741 return NULL; 742 743 __va_start(ap, fmt); 744 kvasnrprintf(&target, PATH_MAX, 10, fmt, ap); 745 __va_end(ap); 746 747 msg = devfs_msg_get(); 748 msg->mdv_name = target; 749 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg); 750 found = msg->mdv_cdev; 751 devfs_msg_put(msg); 752 kvasfree(&target); 753 754 return found; 755 } 756 757 /* 758 * devfs_find_device_by_udev is the synchronous entry point to find a 759 * device given its udev number. It sends a synchronous message with 760 * the relevant details to the devfs core and returns the answer. 761 */ 762 cdev_t 763 devfs_find_device_by_udev(udev_t udev) 764 { 765 cdev_t found = NULL; 766 devfs_msg_t msg; 767 768 msg = devfs_msg_get(); 769 msg->mdv_udev = udev; 770 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg); 771 found = msg->mdv_cdev; 772 devfs_msg_put(msg); 773 774 devfs_debug(DEVFS_DEBUG_DEBUG, 775 "devfs_find_device_by_udev found? %s -end:3-\n", 776 ((found) ? found->si_name:"NO")); 777 return found; 778 } 779 780 struct vnode * 781 devfs_inode_to_vnode(struct mount *mp, ino_t target) 782 { 783 struct vnode *vp = NULL; 784 devfs_msg_t msg; 785 786 if (mp == NULL) 787 return NULL; 788 789 msg = devfs_msg_get(); 790 msg->mdv_ino.mp = mp; 791 msg->mdv_ino.ino = target; 792 msg = devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg); 793 vp = msg->mdv_ino.vp; 794 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 795 devfs_msg_put(msg); 796 797 return vp; 798 } 799 800 /* 801 * devfs_make_alias is the asynchronous entry point to register an alias 802 * for a device. It just sends a message with the relevant details to the 803 * devfs core. 804 */ 805 int 806 devfs_make_alias(const char *name, cdev_t dev_target) 807 { 808 struct devfs_alias *alias; 809 size_t len; 810 811 len = strlen(name); 812 813 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK); 814 alias->name = kstrdup(name, M_DEVFS); 815 alias->namlen = len; 816 alias->dev_target = dev_target; 817 818 devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias); 819 return 0; 820 } 821 822 /* 823 * devfs_apply_rules is the asynchronous entry point to trigger application 824 * of all rules. It just sends a message with the relevant details to the 825 * devfs core. 826 */ 827 int 828 devfs_apply_rules(char *mntto) 829 { 830 char *new_name; 831 832 new_name = kstrdup(mntto, M_DEVFS); 833 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name); 834 835 return 0; 836 } 837 838 /* 839 * devfs_reset_rules is the asynchronous entry point to trigger reset of all 840 * rules. It just sends a message with the relevant details to the devfs core. 841 */ 842 int 843 devfs_reset_rules(char *mntto) 844 { 845 char *new_name; 846 847 new_name = kstrdup(mntto, M_DEVFS); 848 devfs_msg_send_name(DEVFS_RESET_RULES, new_name); 849 850 return 0; 851 } 852 853 854 /* 855 * devfs_scan_callback is the asynchronous entry point to call a callback 856 * on all cdevs. 857 * It just sends a message with the relevant details to the devfs core. 858 */ 859 int 860 devfs_scan_callback(devfs_scan_t *callback) 861 { 862 devfs_msg_t msg; 863 864 KKASSERT(sizeof(callback) == sizeof(void *)); 865 866 msg = devfs_msg_get(); 867 msg->mdv_load = callback; 868 msg = devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg); 869 devfs_msg_put(msg); 870 871 return 0; 872 } 873 874 875 /* 876 * Acts as a message drain. Any message that is replied to here gets destroyed 877 * and the memory freed. 878 */ 879 static void 880 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg) 881 { 882 devfs_msg_put((devfs_msg_t)msg); 883 } 884 885 /* 886 * devfs_msg_get allocates a new devfs msg and returns it. 887 */ 888 devfs_msg_t 889 devfs_msg_get(void) 890 { 891 return objcache_get(devfs_msg_cache, M_WAITOK); 892 } 893 894 /* 895 * devfs_msg_put deallocates a given devfs msg. 896 */ 897 int 898 devfs_msg_put(devfs_msg_t msg) 899 { 900 objcache_put(devfs_msg_cache, msg); 901 return 0; 902 } 903 904 /* 905 * devfs_msg_send is the generic asynchronous message sending facility 906 * for devfs. By default the reply port is the automatic disposal port. 907 * 908 * If the current thread is the devfs_msg_port thread we execute the 909 * operation synchronously. 910 */ 911 void 912 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg) 913 { 914 lwkt_port_t port = &devfs_msg_port; 915 916 lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0); 917 918 devfs_msg->hdr.u.ms_result = cmd; 919 920 if (port->mpu_td == curthread) { 921 devfs_msg_exec(devfs_msg); 922 lwkt_replymsg(&devfs_msg->hdr, 0); 923 } else { 924 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg); 925 } 926 } 927 928 /* 929 * devfs_msg_send_sync is the generic synchronous message sending 930 * facility for devfs. It initializes a local reply port and waits 931 * for the core's answer. This answer is then returned. 932 */ 933 devfs_msg_t 934 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg) 935 { 936 struct lwkt_port rep_port; 937 devfs_msg_t msg_incoming; 938 lwkt_port_t port = &devfs_msg_port; 939 940 lwkt_initport_thread(&rep_port, curthread); 941 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0); 942 943 devfs_msg->hdr.u.ms_result = cmd; 944 945 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg); 946 msg_incoming = lwkt_waitport(&rep_port, 0); 947 948 return msg_incoming; 949 } 950 951 /* 952 * sends a message with a generic argument. 953 */ 954 void 955 devfs_msg_send_generic(uint32_t cmd, void *load) 956 { 957 devfs_msg_t devfs_msg = devfs_msg_get(); 958 959 devfs_msg->mdv_load = load; 960 devfs_msg_send(cmd, devfs_msg); 961 } 962 963 /* 964 * sends a message with a name argument. 965 */ 966 void 967 devfs_msg_send_name(uint32_t cmd, char *name) 968 { 969 devfs_msg_t devfs_msg = devfs_msg_get(); 970 971 devfs_msg->mdv_name = name; 972 devfs_msg_send(cmd, devfs_msg); 973 } 974 975 /* 976 * sends a message with a mount argument. 977 */ 978 void 979 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt) 980 { 981 devfs_msg_t devfs_msg = devfs_msg_get(); 982 983 devfs_msg->mdv_mnt = mnt; 984 devfs_msg_send(cmd, devfs_msg); 985 } 986 987 /* 988 * sends a message with an ops argument. 989 */ 990 void 991 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor) 992 { 993 devfs_msg_t devfs_msg = devfs_msg_get(); 994 995 devfs_msg->mdv_ops.ops = ops; 996 devfs_msg->mdv_ops.minor = minor; 997 devfs_msg_send(cmd, devfs_msg); 998 } 999 1000 /* 1001 * sends a message with a clone handler argument. 1002 */ 1003 void 1004 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler) 1005 { 1006 devfs_msg_t devfs_msg = devfs_msg_get(); 1007 1008 devfs_msg->mdv_chandler.name = name; 1009 devfs_msg->mdv_chandler.nhandler = handler; 1010 devfs_msg_send(cmd, devfs_msg); 1011 } 1012 1013 /* 1014 * sends a message with a device argument. 1015 */ 1016 void 1017 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms) 1018 { 1019 devfs_msg_t devfs_msg = devfs_msg_get(); 1020 1021 devfs_msg->mdv_dev.dev = dev; 1022 devfs_msg->mdv_dev.uid = uid; 1023 devfs_msg->mdv_dev.gid = gid; 1024 devfs_msg->mdv_dev.perms = perms; 1025 1026 devfs_msg_send(cmd, devfs_msg); 1027 } 1028 1029 /* 1030 * sends a message with a link argument. 1031 */ 1032 void 1033 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp) 1034 { 1035 devfs_msg_t devfs_msg = devfs_msg_get(); 1036 1037 devfs_msg->mdv_link.name = name; 1038 devfs_msg->mdv_link.target = target; 1039 devfs_msg->mdv_link.mp = mp; 1040 devfs_msg_send(cmd, devfs_msg); 1041 } 1042 1043 /* 1044 * devfs_msg_core is the main devfs thread. It handles all incoming messages 1045 * and calls the relevant worker functions. By using messages it's assured 1046 * that events occur in the correct order. 1047 */ 1048 static void 1049 devfs_msg_core(void *arg) 1050 { 1051 devfs_msg_t msg; 1052 1053 devfs_run = 1; 1054 lwkt_initport_thread(&devfs_msg_port, curthread); 1055 wakeup(td_core); 1056 1057 while (devfs_run) { 1058 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0); 1059 devfs_debug(DEVFS_DEBUG_DEBUG, 1060 "devfs_msg_core, new msg: %x\n", 1061 (unsigned int)msg->hdr.u.ms_result); 1062 devfs_msg_exec(msg); 1063 lwkt_replymsg(&msg->hdr, 0); 1064 } 1065 wakeup(td_core); 1066 lwkt_exit(); 1067 } 1068 1069 static void 1070 devfs_msg_exec(devfs_msg_t msg) 1071 { 1072 struct devfs_mnt_data *mnt; 1073 struct devfs_node *node; 1074 cdev_t dev; 1075 1076 /* 1077 * Acquire the devfs lock to ensure safety of all called functions 1078 */ 1079 lockmgr(&devfs_lock, LK_EXCLUSIVE); 1080 1081 switch (msg->hdr.u.ms_result) { 1082 case DEVFS_DEVICE_CREATE: 1083 dev = msg->mdv_dev.dev; 1084 devfs_create_dev_worker(dev, 1085 msg->mdv_dev.uid, 1086 msg->mdv_dev.gid, 1087 msg->mdv_dev.perms); 1088 break; 1089 case DEVFS_DEVICE_DESTROY: 1090 dev = msg->mdv_dev.dev; 1091 devfs_destroy_dev_worker(dev); 1092 break; 1093 case DEVFS_DESTROY_SUBNAMES: 1094 devfs_destroy_subnames_worker(msg->mdv_load); 1095 break; 1096 case DEVFS_DESTROY_DEV_BY_OPS: 1097 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops, 1098 msg->mdv_ops.minor); 1099 break; 1100 case DEVFS_CREATE_ALL_DEV: 1101 node = (struct devfs_node *)msg->mdv_load; 1102 devfs_create_all_dev_worker(node); 1103 break; 1104 case DEVFS_MOUNT_ADD: 1105 mnt = msg->mdv_mnt; 1106 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link); 1107 devfs_create_all_dev_worker(mnt->root_node); 1108 break; 1109 case DEVFS_MOUNT_DEL: 1110 mnt = msg->mdv_mnt; 1111 TAILQ_REMOVE(&devfs_mnt_list, mnt, link); 1112 devfs_iterate_topology(mnt->root_node, devfs_reaperp_callback, 1113 NULL); 1114 if (mnt->leak_count) { 1115 devfs_debug(DEVFS_DEBUG_SHOW, 1116 "Leaked %ld devfs_node elements!\n", 1117 mnt->leak_count); 1118 } 1119 break; 1120 case DEVFS_CHANDLER_ADD: 1121 devfs_chandler_add_worker(msg->mdv_chandler.name, 1122 msg->mdv_chandler.nhandler); 1123 break; 1124 case DEVFS_CHANDLER_DEL: 1125 devfs_chandler_del_worker(msg->mdv_chandler.name); 1126 break; 1127 case DEVFS_FIND_DEVICE_BY_NAME: 1128 devfs_find_device_by_name_worker(msg); 1129 break; 1130 case DEVFS_FIND_DEVICE_BY_UDEV: 1131 devfs_find_device_by_udev_worker(msg); 1132 break; 1133 case DEVFS_MAKE_ALIAS: 1134 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load); 1135 break; 1136 case DEVFS_APPLY_RULES: 1137 devfs_apply_reset_rules_caller(msg->mdv_name, 1); 1138 break; 1139 case DEVFS_RESET_RULES: 1140 devfs_apply_reset_rules_caller(msg->mdv_name, 0); 1141 break; 1142 case DEVFS_SCAN_CALLBACK: 1143 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load); 1144 break; 1145 case DEVFS_CLR_SUBNAMES_FLAG: 1146 devfs_clr_subnames_flag_worker(msg->mdv_flags.name, 1147 msg->mdv_flags.flag); 1148 break; 1149 case DEVFS_DESTROY_SUBNAMES_WO_FLAG: 1150 devfs_destroy_subnames_without_flag_worker(msg->mdv_flags.name, 1151 msg->mdv_flags.flag); 1152 break; 1153 case DEVFS_INODE_TO_VNODE: 1154 msg->mdv_ino.vp = devfs_iterate_topology( 1155 DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node, 1156 (devfs_iterate_callback_t *)devfs_inode_to_vnode_worker_callback, 1157 &msg->mdv_ino.ino); 1158 break; 1159 case DEVFS_TERMINATE_CORE: 1160 devfs_run = 0; 1161 break; 1162 case DEVFS_SYNC: 1163 break; 1164 default: 1165 devfs_debug(DEVFS_DEBUG_WARNING, 1166 "devfs_msg_core: unknown message " 1167 "received at core\n"); 1168 break; 1169 } 1170 lockmgr(&devfs_lock, LK_RELEASE); 1171 } 1172 1173 /* 1174 * Worker function to insert a new dev into the dev list and initialize its 1175 * permissions. It also calls devfs_propagate_dev which in turn propagates 1176 * the change to all mount points. 1177 * 1178 * The passed dev is already referenced. This reference is eaten by this 1179 * function and represents the dev's linkage into devfs_dev_list. 1180 */ 1181 static int 1182 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms) 1183 { 1184 KKASSERT(dev); 1185 1186 dev->si_uid = uid; 1187 dev->si_gid = gid; 1188 dev->si_perms = perms; 1189 1190 devfs_link_dev(dev); 1191 devfs_propagate_dev(dev, 1); 1192 1193 return 0; 1194 } 1195 1196 /* 1197 * Worker function to delete a dev from the dev list and free the cdev. 1198 * It also calls devfs_propagate_dev which in turn propagates the change 1199 * to all mount points. 1200 */ 1201 static int 1202 devfs_destroy_dev_worker(cdev_t dev) 1203 { 1204 int error; 1205 1206 KKASSERT(dev); 1207 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE); 1208 1209 error = devfs_unlink_dev(dev); 1210 devfs_propagate_dev(dev, 0); 1211 if (error == 0) 1212 release_dev(dev); /* link ref */ 1213 release_dev(dev); 1214 release_dev(dev); 1215 1216 return 0; 1217 } 1218 1219 /* 1220 * Worker function to destroy all devices with a certain basename. 1221 * Calls devfs_destroy_dev_worker for the actual destruction. 1222 */ 1223 static int 1224 devfs_destroy_subnames_worker(char *name) 1225 { 1226 cdev_t dev, dev1; 1227 size_t len = strlen(name); 1228 1229 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) { 1230 if ((!strncmp(dev->si_name, name, len)) && 1231 (dev->si_name[len] != '\0')) { 1232 devfs_destroy_dev_worker(dev); 1233 } 1234 } 1235 return 0; 1236 } 1237 1238 static int 1239 devfs_clr_subnames_flag_worker(char *name, uint32_t flag) 1240 { 1241 cdev_t dev, dev1; 1242 size_t len = strlen(name); 1243 1244 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) { 1245 if ((!strncmp(dev->si_name, name, len)) && 1246 (dev->si_name[len] != '\0')) { 1247 dev->si_flags &= ~flag; 1248 } 1249 } 1250 1251 return 0; 1252 } 1253 1254 static int 1255 devfs_destroy_subnames_without_flag_worker(char *name, uint32_t flag) 1256 { 1257 cdev_t dev, dev1; 1258 size_t len = strlen(name); 1259 1260 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) { 1261 if ((!strncmp(dev->si_name, name, len)) && 1262 (dev->si_name[len] != '\0')) { 1263 if (!(dev->si_flags & flag)) { 1264 devfs_destroy_dev_worker(dev); 1265 } 1266 } 1267 } 1268 1269 return 0; 1270 } 1271 1272 /* 1273 * Worker function that creates all device nodes on top of a devfs 1274 * root node. 1275 */ 1276 static int 1277 devfs_create_all_dev_worker(struct devfs_node *root) 1278 { 1279 cdev_t dev; 1280 1281 KKASSERT(root); 1282 1283 TAILQ_FOREACH(dev, &devfs_dev_list, link) { 1284 devfs_create_device_node(root, dev, NULL, NULL); 1285 } 1286 1287 return 0; 1288 } 1289 1290 /* 1291 * Worker function that destroys all devices that match a specific 1292 * dev_ops and/or minor. If minor is less than 0, it is not matched 1293 * against. It also propagates all changes. 1294 */ 1295 static int 1296 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor) 1297 { 1298 cdev_t dev, dev1; 1299 1300 KKASSERT(ops); 1301 1302 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) { 1303 if (dev->si_ops != ops) 1304 continue; 1305 if ((minor < 0) || (dev->si_uminor == minor)) { 1306 devfs_destroy_dev_worker(dev); 1307 } 1308 } 1309 1310 return 0; 1311 } 1312 1313 /* 1314 * Worker function that registers a new clone handler in devfs. 1315 */ 1316 static int 1317 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler) 1318 { 1319 struct devfs_clone_handler *chandler = NULL; 1320 u_char len = strlen(name); 1321 1322 if (len == 0) 1323 return 1; 1324 1325 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) { 1326 if (chandler->namlen != len) 1327 continue; 1328 1329 if (!memcmp(chandler->name, name, len)) { 1330 /* Clonable basename already exists */ 1331 return 1; 1332 } 1333 } 1334 1335 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO); 1336 chandler->name = kstrdup(name, M_DEVFS); 1337 chandler->namlen = len; 1338 chandler->nhandler = nhandler; 1339 1340 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link); 1341 return 0; 1342 } 1343 1344 /* 1345 * Worker function that removes a given clone handler from the 1346 * clone handler list. 1347 */ 1348 static int 1349 devfs_chandler_del_worker(const char *name) 1350 { 1351 struct devfs_clone_handler *chandler, *chandler2; 1352 u_char len = strlen(name); 1353 1354 if (len == 0) 1355 return 1; 1356 1357 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) { 1358 if (chandler->namlen != len) 1359 continue; 1360 if (memcmp(chandler->name, name, len)) 1361 continue; 1362 1363 TAILQ_REMOVE(&devfs_chandler_list, chandler, link); 1364 kfree(chandler->name, M_DEVFS); 1365 kfree(chandler, M_DEVFS); 1366 break; 1367 } 1368 1369 return 0; 1370 } 1371 1372 /* 1373 * Worker function that finds a given device name and changes 1374 * the message received accordingly so that when replied to, 1375 * the answer is returned to the caller. 1376 */ 1377 static int 1378 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg) 1379 { 1380 struct devfs_alias *alias; 1381 cdev_t dev; 1382 cdev_t found = NULL; 1383 1384 TAILQ_FOREACH(dev, &devfs_dev_list, link) { 1385 if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) { 1386 found = dev; 1387 break; 1388 } 1389 } 1390 if (found == NULL) { 1391 TAILQ_FOREACH(alias, &devfs_alias_list, link) { 1392 if (strcmp(devfs_msg->mdv_name, alias->name) == 0) { 1393 found = alias->dev_target; 1394 break; 1395 } 1396 } 1397 } 1398 devfs_msg->mdv_cdev = found; 1399 1400 return 0; 1401 } 1402 1403 /* 1404 * Worker function that finds a given device udev and changes 1405 * the message received accordingly so that when replied to, 1406 * the answer is returned to the caller. 1407 */ 1408 static int 1409 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg) 1410 { 1411 cdev_t dev, dev1; 1412 cdev_t found = NULL; 1413 1414 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) { 1415 if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) { 1416 found = dev; 1417 break; 1418 } 1419 } 1420 devfs_msg->mdv_cdev = found; 1421 1422 return 0; 1423 } 1424 1425 /* 1426 * Worker function that inserts a given alias into the 1427 * alias list, and propagates the alias to all mount 1428 * points. 1429 */ 1430 static int 1431 devfs_make_alias_worker(struct devfs_alias *alias) 1432 { 1433 struct devfs_alias *alias2; 1434 size_t len = strlen(alias->name); 1435 int found = 0; 1436 1437 TAILQ_FOREACH(alias2, &devfs_alias_list, link) { 1438 if (len != alias2->namlen) 1439 continue; 1440 1441 if (!memcmp(alias->name, alias2->name, len)) { 1442 found = 1; 1443 break; 1444 } 1445 } 1446 1447 if (!found) { 1448 /* 1449 * The alias doesn't exist yet, so we add it to the alias list 1450 */ 1451 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link); 1452 devfs_alias_propagate(alias); 1453 } else { 1454 devfs_debug(DEVFS_DEBUG_WARNING, 1455 "Warning: duplicate devfs_make_alias for %s\n", 1456 alias->name); 1457 kfree(alias->name, M_DEVFS); 1458 kfree(alias, M_DEVFS); 1459 } 1460 1461 return 0; 1462 } 1463 1464 /* 1465 * Function that removes and frees all aliases. 1466 */ 1467 static int 1468 devfs_alias_reap(void) 1469 { 1470 struct devfs_alias *alias, *alias2; 1471 1472 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) { 1473 TAILQ_REMOVE(&devfs_alias_list, alias, link); 1474 kfree(alias, M_DEVFS); 1475 } 1476 return 0; 1477 } 1478 1479 /* 1480 * Function that removes an alias matching a specific cdev and frees 1481 * it accordingly. 1482 */ 1483 static int 1484 devfs_alias_remove(cdev_t dev) 1485 { 1486 struct devfs_alias *alias, *alias2; 1487 1488 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) { 1489 if (alias->dev_target == dev) { 1490 TAILQ_REMOVE(&devfs_alias_list, alias, link); 1491 kfree(alias, M_DEVFS); 1492 } 1493 } 1494 return 0; 1495 } 1496 1497 /* 1498 * This function propagates a new alias to all mount points. 1499 */ 1500 static int 1501 devfs_alias_propagate(struct devfs_alias *alias) 1502 { 1503 struct devfs_mnt_data *mnt; 1504 1505 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) { 1506 devfs_alias_apply(mnt->root_node, alias); 1507 } 1508 return 0; 1509 } 1510 1511 /* 1512 * This function is a recursive function iterating through 1513 * all device nodes in the topology and, if applicable, 1514 * creating the relevant alias for a device node. 1515 */ 1516 static int 1517 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias) 1518 { 1519 struct devfs_node *node1, *node2; 1520 1521 KKASSERT(alias != NULL); 1522 1523 if ((node->node_type == Proot) || (node->node_type == Pdir)) { 1524 if (node->nchildren > 2) { 1525 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) { 1526 devfs_alias_apply(node1, alias); 1527 } 1528 } 1529 } else { 1530 if (node->d_dev == alias->dev_target) 1531 devfs_alias_create(alias->name, node, 0); 1532 } 1533 return 0; 1534 } 1535 1536 /* 1537 * This function checks if any alias possibly is applicable 1538 * to the given node. If so, the alias is created. 1539 */ 1540 static int 1541 devfs_alias_check_create(struct devfs_node *node) 1542 { 1543 struct devfs_alias *alias; 1544 1545 TAILQ_FOREACH(alias, &devfs_alias_list, link) { 1546 if (node->d_dev == alias->dev_target) 1547 devfs_alias_create(alias->name, node, 0); 1548 } 1549 return 0; 1550 } 1551 1552 /* 1553 * This function creates an alias with a given name 1554 * linking to a given devfs node. It also increments 1555 * the link count on the target node. 1556 */ 1557 int 1558 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based) 1559 { 1560 struct mount *mp = target->mp; 1561 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node; 1562 struct devfs_node *linknode; 1563 struct hotplug_device *hpdev; 1564 char *create_path = NULL; 1565 char *name; 1566 char *name_buf; 1567 int result = 0; 1568 1569 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE); 1570 1571 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK); 1572 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name); 1573 1574 if (create_path) 1575 parent = devfs_resolve_or_create_path(parent, create_path, 1); 1576 1577 1578 if (devfs_find_device_node_by_name(parent, name)) { 1579 devfs_debug(DEVFS_DEBUG_WARNING, 1580 "Node already exists: %s " 1581 "(devfs_make_alias_worker)!\n", 1582 name); 1583 result = 1; 1584 goto done; 1585 } 1586 1587 linknode = devfs_allocp(Plink, name, parent, mp, NULL); 1588 if (linknode == NULL) { 1589 result = 1; 1590 goto done; 1591 } 1592 1593 linknode->link_target = target; 1594 target->nlinks++; 1595 1596 if (rule_based) 1597 linknode->flags |= DEVFS_RULE_CREATED; 1598 1599 done: 1600 /* hotplug handler */ 1601 if(devfs_node_added) { 1602 hpdev = kmalloc(sizeof(struct hotplug_device), M_TEMP, M_WAITOK); 1603 hpdev->dev = target->d_dev; 1604 hpdev->name = name_orig; 1605 devfs_node_added(hpdev); 1606 kfree(hpdev, M_TEMP); 1607 } 1608 kfree(name_buf, M_TEMP); 1609 return (result); 1610 } 1611 1612 /* 1613 * This function is called by the core and handles mount point 1614 * strings. It either calls the relevant worker (devfs_apply_ 1615 * reset_rules_worker) on all mountpoints or only a specific 1616 * one. 1617 */ 1618 static int 1619 devfs_apply_reset_rules_caller(char *mountto, int apply) 1620 { 1621 struct devfs_mnt_data *mnt; 1622 1623 if (mountto[0] == '*') { 1624 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) { 1625 devfs_iterate_topology(mnt->root_node, 1626 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node), 1627 NULL); 1628 } 1629 } else { 1630 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) { 1631 if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) { 1632 devfs_iterate_topology(mnt->root_node, 1633 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node), 1634 NULL); 1635 break; 1636 } 1637 } 1638 } 1639 1640 kfree(mountto, M_DEVFS); 1641 return 0; 1642 } 1643 1644 /* 1645 * This function calls a given callback function for 1646 * every dev node in the devfs dev list. 1647 */ 1648 static int 1649 devfs_scan_callback_worker(devfs_scan_t *callback) 1650 { 1651 cdev_t dev, dev1; 1652 1653 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) { 1654 callback(dev); 1655 } 1656 1657 return 0; 1658 } 1659 1660 /* 1661 * This function tries to resolve a given directory, or if not 1662 * found and creation requested, creates the given directory. 1663 */ 1664 static struct devfs_node * 1665 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name, 1666 size_t name_len, int create) 1667 { 1668 struct devfs_node *node, *found = NULL; 1669 1670 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) { 1671 if (name_len != node->d_dir.d_namlen) 1672 continue; 1673 1674 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) { 1675 found = node; 1676 break; 1677 } 1678 } 1679 1680 if ((found == NULL) && (create)) { 1681 found = devfs_allocp(Pdir, dir_name, parent, parent->mp, NULL); 1682 } 1683 1684 return found; 1685 } 1686 1687 /* 1688 * This function tries to resolve a complete path. If creation is requested, 1689 * if a given part of the path cannot be resolved (because it doesn't exist), 1690 * it is created. 1691 */ 1692 struct devfs_node * 1693 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create) 1694 { 1695 struct devfs_node *node = parent; 1696 char *buf; 1697 size_t idx = 0; 1698 1699 if (path == NULL) 1700 return parent; 1701 1702 buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK); 1703 1704 while (*path && idx < PATH_MAX - 1) { 1705 if (*path != '/') { 1706 buf[idx++] = *path; 1707 } else { 1708 buf[idx] = '\0'; 1709 node = devfs_resolve_or_create_dir(node, buf, idx, create); 1710 if (node == NULL) { 1711 kfree(buf, M_TEMP); 1712 return NULL; 1713 } 1714 idx = 0; 1715 } 1716 ++path; 1717 } 1718 buf[idx] = '\0'; 1719 node = devfs_resolve_or_create_dir(node, buf, idx, create); 1720 kfree (buf, M_TEMP); 1721 return (node); 1722 } 1723 1724 /* 1725 * Takes a full path and strips it into a directory path and a name. 1726 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It 1727 * requires a working buffer with enough size to keep the whole 1728 * fullpath. 1729 */ 1730 int 1731 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep) 1732 { 1733 char *name = NULL; 1734 char *path = NULL; 1735 size_t len = strlen(fullpath) + 1; 1736 int i; 1737 1738 KKASSERT((fullpath != NULL) && (buf != NULL)); 1739 KKASSERT((pathp != NULL) && (namep != NULL)); 1740 1741 memcpy(buf, fullpath, len); 1742 1743 for (i = len-1; i>= 0; i--) { 1744 if (buf[i] == '/') { 1745 buf[i] = '\0'; 1746 name = &(buf[i+1]); 1747 path = buf; 1748 break; 1749 } 1750 } 1751 1752 *pathp = path; 1753 1754 if (name) { 1755 *namep = name; 1756 } else { 1757 *namep = buf; 1758 } 1759 1760 return 0; 1761 } 1762 1763 /* 1764 * This function creates a new devfs node for a given device. It can 1765 * handle a complete path as device name, and accordingly creates 1766 * the path and the final device node. 1767 * 1768 * The reference count on the passed dev remains unchanged. 1769 */ 1770 struct devfs_node * 1771 devfs_create_device_node(struct devfs_node *root, cdev_t dev, 1772 char *dev_name, char *path_fmt, ...) 1773 { 1774 struct devfs_node *parent, *node = NULL; 1775 struct hotplug_device *hpdev; 1776 char *path = NULL; 1777 char *name; 1778 char *name_buf; 1779 __va_list ap; 1780 int i, found; 1781 char *create_path = NULL; 1782 char *names = "pqrsPQRS"; 1783 1784 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK); 1785 1786 if (path_fmt != NULL) { 1787 __va_start(ap, path_fmt); 1788 kvasnrprintf(&path, PATH_MAX, 10, path_fmt, ap); 1789 __va_end(ap); 1790 } 1791 1792 parent = devfs_resolve_or_create_path(root, path, 1); 1793 KKASSERT(parent); 1794 1795 devfs_resolve_name_path( 1796 ((dev_name == NULL) && (dev))?(dev->si_name):(dev_name), 1797 name_buf, &create_path, &name); 1798 1799 if (create_path) 1800 parent = devfs_resolve_or_create_path(parent, create_path, 1); 1801 1802 1803 if (devfs_find_device_node_by_name(parent, name)) { 1804 devfs_debug(DEVFS_DEBUG_WARNING, "devfs_create_device_node: " 1805 "DEVICE %s ALREADY EXISTS!!! Ignoring creation request.\n", name); 1806 goto out; 1807 } 1808 1809 node = devfs_allocp(Pdev, name, parent, parent->mp, dev); 1810 nanotime(&parent->mtime); 1811 1812 /* 1813 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their 1814 * directory 1815 */ 1816 if ((dev) && (strlen(dev->si_name) >= 4) && 1817 (!memcmp(dev->si_name, "ptm/", 4))) { 1818 node->parent->flags |= DEVFS_HIDDEN; 1819 node->flags |= DEVFS_HIDDEN; 1820 } 1821 1822 /* 1823 * Ugly pty magic, to tag pty devices as such and hide them if needed. 1824 */ 1825 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3))) 1826 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE); 1827 1828 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) { 1829 found = 0; 1830 for (i = 0; i < strlen(names); i++) { 1831 if (name[3] == names[i]) { 1832 found = 1; 1833 break; 1834 } 1835 } 1836 if (found) 1837 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE); 1838 } 1839 /* hotplug handler */ 1840 if(devfs_node_added) { 1841 hpdev = kmalloc(sizeof(struct hotplug_device), M_TEMP, M_WAITOK); 1842 hpdev->dev = node->d_dev; 1843 hpdev->name = node->d_dev->si_name; 1844 devfs_node_added(hpdev); 1845 kfree(hpdev, M_TEMP); 1846 } 1847 1848 out: 1849 kfree(name_buf, M_TEMP); 1850 kvasfree(&path); 1851 return node; 1852 } 1853 1854 /* 1855 * This function finds a given device node in the topology with a given 1856 * cdev. 1857 */ 1858 void * 1859 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target) 1860 { 1861 if ((node->node_type == Pdev) && (node->d_dev == target)) { 1862 return node; 1863 } 1864 1865 return NULL; 1866 } 1867 1868 /* 1869 * This function finds a device node in the given parent directory by its 1870 * name and returns it. 1871 */ 1872 struct devfs_node * 1873 devfs_find_device_node_by_name(struct devfs_node *parent, char *target) 1874 { 1875 struct devfs_node *node, *found = NULL; 1876 size_t len = strlen(target); 1877 1878 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) { 1879 if (len != node->d_dir.d_namlen) 1880 continue; 1881 1882 if (!memcmp(node->d_dir.d_name, target, len)) { 1883 found = node; 1884 break; 1885 } 1886 } 1887 1888 return found; 1889 } 1890 1891 static void * 1892 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop) 1893 { 1894 struct vnode *vp = NULL; 1895 ino_t target = *inop; 1896 1897 if (node->d_dir.d_ino == target) { 1898 if (node->v_node) { 1899 vp = node->v_node; 1900 vget(vp, LK_EXCLUSIVE | LK_RETRY); 1901 vn_unlock(vp); 1902 } else { 1903 devfs_allocv(&vp, node); 1904 vn_unlock(vp); 1905 } 1906 } 1907 1908 return vp; 1909 } 1910 1911 /* 1912 * This function takes a cdev and removes its devfs node in the 1913 * given topology. The cdev remains intact. 1914 */ 1915 int 1916 devfs_destroy_device_node(struct devfs_node *root, cdev_t target) 1917 { 1918 struct devfs_node *node, *parent; 1919 char *name; 1920 char *name_buf; 1921 char *create_path = NULL; 1922 1923 KKASSERT(target); 1924 1925 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK); 1926 ksnprintf(name_buf, PATH_MAX, "%s", target->si_name); 1927 1928 devfs_resolve_name_path(target->si_name, name_buf, &create_path, &name); 1929 1930 if (create_path) 1931 parent = devfs_resolve_or_create_path(root, create_path, 0); 1932 else 1933 parent = root; 1934 1935 if (parent == NULL) 1936 return 1; 1937 1938 node = devfs_find_device_node_by_name(parent, name); 1939 1940 if (node) { 1941 nanotime(&node->parent->mtime); 1942 devfs_gc(node); 1943 } 1944 1945 kfree(name_buf, M_TEMP); 1946 1947 return 0; 1948 } 1949 1950 /* 1951 * Just set perms and ownership for given node. 1952 */ 1953 int 1954 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid, 1955 u_short mode, u_long flags) 1956 { 1957 node->mode = mode; 1958 node->uid = uid; 1959 node->gid = gid; 1960 1961 return 0; 1962 } 1963 1964 /* 1965 * Propagates a device attach/detach to all mount 1966 * points. Also takes care of automatic alias removal 1967 * for a deleted cdev. 1968 */ 1969 static int 1970 devfs_propagate_dev(cdev_t dev, int attach) 1971 { 1972 struct devfs_mnt_data *mnt; 1973 1974 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) { 1975 if (attach) { 1976 /* Device is being attached */ 1977 devfs_create_device_node(mnt->root_node, dev, 1978 NULL, NULL ); 1979 } else { 1980 /* Device is being detached */ 1981 devfs_alias_remove(dev); 1982 devfs_destroy_device_node(mnt->root_node, dev); 1983 } 1984 } 1985 return 0; 1986 } 1987 1988 /* 1989 * devfs_clone either returns a basename from a complete name by 1990 * returning the length of the name without trailing digits, or, 1991 * if clone != 0, calls the device's clone handler to get a new 1992 * device, which in turn is returned in devp. 1993 */ 1994 cdev_t 1995 devfs_clone(cdev_t dev, const char *name, size_t len, int mode, 1996 struct ucred *cred) 1997 { 1998 int error; 1999 struct devfs_clone_handler *chandler; 2000 struct dev_clone_args ap; 2001 2002 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) { 2003 if (chandler->namlen != len) 2004 continue; 2005 if ((!memcmp(chandler->name, name, len)) && (chandler->nhandler)) { 2006 lockmgr(&devfs_lock, LK_RELEASE); 2007 devfs_config(); 2008 lockmgr(&devfs_lock, LK_EXCLUSIVE); 2009 2010 ap.a_head.a_dev = dev; 2011 ap.a_dev = NULL; 2012 ap.a_name = name; 2013 ap.a_namelen = len; 2014 ap.a_mode = mode; 2015 ap.a_cred = cred; 2016 error = (chandler->nhandler)(&ap); 2017 if (error) 2018 continue; 2019 2020 return ap.a_dev; 2021 } 2022 } 2023 2024 return NULL; 2025 } 2026 2027 2028 /* 2029 * Registers a new orphan in the orphan list. 2030 */ 2031 void 2032 devfs_tracer_add_orphan(struct devfs_node *node) 2033 { 2034 struct devfs_orphan *orphan; 2035 2036 KKASSERT(node); 2037 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK); 2038 orphan->node = node; 2039 2040 KKASSERT((node->flags & DEVFS_ORPHANED) == 0); 2041 node->flags |= DEVFS_ORPHANED; 2042 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link); 2043 } 2044 2045 /* 2046 * Removes an orphan from the orphan list. 2047 */ 2048 void 2049 devfs_tracer_del_orphan(struct devfs_node *node) 2050 { 2051 struct devfs_orphan *orphan; 2052 2053 KKASSERT(node); 2054 2055 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) { 2056 if (orphan->node == node) { 2057 node->flags &= ~DEVFS_ORPHANED; 2058 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link); 2059 kfree(orphan, M_DEVFS); 2060 break; 2061 } 2062 } 2063 } 2064 2065 /* 2066 * Counts the orphans in the orphan list, and if cleanup 2067 * is specified, also frees the orphan and removes it from 2068 * the list. 2069 */ 2070 size_t 2071 devfs_tracer_orphan_count(struct mount *mp, int cleanup) 2072 { 2073 struct devfs_orphan *orphan, *orphan2; 2074 size_t count = 0; 2075 2076 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) { 2077 count++; 2078 /* 2079 * If we are instructed to clean up, we do so. 2080 */ 2081 if (cleanup) { 2082 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link); 2083 orphan->node->flags &= ~DEVFS_ORPHANED; 2084 devfs_freep(orphan->node); 2085 kfree(orphan, M_DEVFS); 2086 } 2087 } 2088 2089 return count; 2090 } 2091 2092 /* 2093 * Fetch an ino_t from the global d_ino by increasing it 2094 * while spinlocked. 2095 */ 2096 static ino_t 2097 devfs_fetch_ino(void) 2098 { 2099 ino_t ret; 2100 2101 spin_lock_wr(&ino_lock); 2102 ret = d_ino++; 2103 spin_unlock_wr(&ino_lock); 2104 2105 return ret; 2106 } 2107 2108 /* 2109 * Allocates a new cdev and initializes it's most basic 2110 * fields. 2111 */ 2112 cdev_t 2113 devfs_new_cdev(struct dev_ops *ops, int minor, struct dev_ops *bops) 2114 { 2115 cdev_t dev = sysref_alloc(&cdev_sysref_class); 2116 2117 sysref_activate(&dev->si_sysref); 2118 reference_dev(dev); 2119 bzero(dev, offsetof(struct cdev, si_sysref)); 2120 2121 dev->si_uid = 0; 2122 dev->si_gid = 0; 2123 dev->si_perms = 0; 2124 dev->si_drv1 = NULL; 2125 dev->si_drv2 = NULL; 2126 dev->si_lastread = 0; /* time_second */ 2127 dev->si_lastwrite = 0; /* time_second */ 2128 2129 dev->si_ops = ops; 2130 dev->si_flags = 0; 2131 dev->si_umajor = 0; 2132 dev->si_uminor = minor; 2133 dev->si_bops = bops; 2134 /* If there is a backing device, we reference its ops */ 2135 dev->si_inode = makeudev( 2136 devfs_reference_ops((bops)?(bops):(ops)), 2137 minor ); 2138 2139 return dev; 2140 } 2141 2142 static void 2143 devfs_cdev_terminate(cdev_t dev) 2144 { 2145 int locked = 0; 2146 2147 /* Check if it is locked already. if not, we acquire the devfs lock */ 2148 if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) { 2149 lockmgr(&devfs_lock, LK_EXCLUSIVE); 2150 locked = 1; 2151 } 2152 2153 /* Propagate destruction, just in case */ 2154 devfs_propagate_dev(dev, 0); 2155 2156 /* If we acquired the lock, we also get rid of it */ 2157 if (locked) 2158 lockmgr(&devfs_lock, LK_RELEASE); 2159 2160 /* If there is a backing device, we release the backing device's ops */ 2161 devfs_release_ops((dev->si_bops)?(dev->si_bops):(dev->si_ops)); 2162 2163 /* Finally destroy the device */ 2164 sysref_put(&dev->si_sysref); 2165 } 2166 2167 /* 2168 * Dummies for now (individual locks for MPSAFE) 2169 */ 2170 static void 2171 devfs_cdev_lock(cdev_t dev) 2172 { 2173 } 2174 2175 static void 2176 devfs_cdev_unlock(cdev_t dev) 2177 { 2178 } 2179 2180 /* 2181 * Links a given cdev into the dev list. 2182 */ 2183 int 2184 devfs_link_dev(cdev_t dev) 2185 { 2186 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0); 2187 dev->si_flags |= SI_DEVFS_LINKED; 2188 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link); 2189 2190 return 0; 2191 } 2192 2193 /* 2194 * Removes a given cdev from the dev list. The caller is responsible for 2195 * releasing the reference on the device associated with the linkage. 2196 * 2197 * Returns EALREADY if the dev has already been unlinked. 2198 */ 2199 static int 2200 devfs_unlink_dev(cdev_t dev) 2201 { 2202 if ((dev->si_flags & SI_DEVFS_LINKED)) { 2203 TAILQ_REMOVE(&devfs_dev_list, dev, link); 2204 dev->si_flags &= ~SI_DEVFS_LINKED; 2205 return (0); 2206 } 2207 return (EALREADY); 2208 } 2209 2210 int 2211 devfs_node_is_accessible(struct devfs_node *node) 2212 { 2213 if ((node) && (!(node->flags & DEVFS_HIDDEN))) 2214 return 1; 2215 else 2216 return 0; 2217 } 2218 2219 int 2220 devfs_reference_ops(struct dev_ops *ops) 2221 { 2222 int unit; 2223 struct devfs_dev_ops *found = NULL; 2224 struct devfs_dev_ops *devops; 2225 2226 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) { 2227 if (devops->ops == ops) { 2228 found = devops; 2229 break; 2230 } 2231 } 2232 2233 if (!found) { 2234 found = kmalloc(sizeof(struct devfs_dev_ops), M_DEVFS, M_WAITOK); 2235 found->ops = ops; 2236 found->ref_count = 0; 2237 TAILQ_INSERT_TAIL(&devfs_dev_ops_list, found, link); 2238 } 2239 2240 KKASSERT(found); 2241 2242 if (found->ref_count == 0) { 2243 found->id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255); 2244 if (found->id == -1) { 2245 /* Ran out of unique ids */ 2246 devfs_debug(DEVFS_DEBUG_WARNING, 2247 "devfs_reference_ops: WARNING: ran out of unique ids\n"); 2248 } 2249 } 2250 unit = found->id; 2251 ++found->ref_count; 2252 2253 return unit; 2254 } 2255 2256 void 2257 devfs_release_ops(struct dev_ops *ops) 2258 { 2259 struct devfs_dev_ops *found = NULL; 2260 struct devfs_dev_ops *devops; 2261 2262 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) { 2263 if (devops->ops == ops) { 2264 found = devops; 2265 break; 2266 } 2267 } 2268 2269 KKASSERT(found); 2270 2271 --found->ref_count; 2272 2273 if (found->ref_count == 0) { 2274 TAILQ_REMOVE(&devfs_dev_ops_list, found, link); 2275 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), found->id); 2276 kfree(found, M_DEVFS); 2277 } 2278 } 2279 2280 void 2281 devfs_config(void) 2282 { 2283 devfs_msg_t msg; 2284 2285 msg = devfs_msg_get(); 2286 msg = devfs_msg_send_sync(DEVFS_SYNC, msg); 2287 devfs_msg_put(msg); 2288 } 2289 2290 /* 2291 * Called on init of devfs; creates the objcaches and 2292 * spawns off the devfs core thread. Also initializes 2293 * locks. 2294 */ 2295 static void 2296 devfs_init(void) 2297 { 2298 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n"); 2299 /* Create objcaches for nodes, msgs and devs */ 2300 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0, 2301 NULL, NULL, NULL, 2302 objcache_malloc_alloc, 2303 objcache_malloc_free, 2304 &devfs_node_malloc_args ); 2305 2306 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0, 2307 NULL, NULL, NULL, 2308 objcache_malloc_alloc, 2309 objcache_malloc_free, 2310 &devfs_msg_malloc_args ); 2311 2312 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0, 2313 NULL, NULL, NULL, 2314 objcache_malloc_alloc, 2315 objcache_malloc_free, 2316 &devfs_dev_malloc_args ); 2317 2318 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id)); 2319 2320 /* Initialize the reply-only port which acts as a message drain */ 2321 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply); 2322 2323 /* Initialize *THE* devfs lock */ 2324 lockinit(&devfs_lock, "devfs_core lock", 0, 0); 2325 2326 2327 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL, 2328 0, 0, "devfs_msg_core"); 2329 2330 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0); 2331 2332 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n"); 2333 } 2334 2335 /* 2336 * Called on unload of devfs; takes care of destroying the core 2337 * and the objcaches. Also removes aliases that are no longer needed. 2338 */ 2339 static void 2340 devfs_uninit(void) 2341 { 2342 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n"); 2343 2344 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL); 2345 2346 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0); 2347 tsleep(td_core/*devfs_id*/, 0, "devfsc", 10000); 2348 2349 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id)); 2350 2351 /* Destroy the objcaches */ 2352 objcache_destroy(devfs_msg_cache); 2353 objcache_destroy(devfs_node_cache); 2354 objcache_destroy(devfs_dev_cache); 2355 2356 devfs_alias_reap(); 2357 } 2358 2359 /* 2360 * This is a sysctl handler to assist userland devname(3) to 2361 * find the device name for a given udev. 2362 */ 2363 static int 2364 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS) 2365 { 2366 udev_t udev; 2367 cdev_t found; 2368 int error; 2369 2370 2371 if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t)))) 2372 return (error); 2373 2374 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev); 2375 2376 if (udev == NOUDEV) 2377 return(EINVAL); 2378 2379 if ((found = devfs_find_device_by_udev(udev)) == NULL) 2380 return(ENOENT); 2381 2382 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1)); 2383 } 2384 2385 2386 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY, 2387 NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)"); 2388 2389 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs"); 2390 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable); 2391 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable, 2392 0, "Enable DevFS debugging"); 2393 2394 SYSINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, 2395 devfs_init, NULL); 2396 SYSUNINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, 2397 devfs_uninit, NULL); 2398