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/bus.h> 38 #include <sys/mount.h> 39 #include <sys/vnode.h> 40 #include <sys/types.h> 41 #include <sys/lock.h> 42 #include <sys/file.h> 43 #include <sys/msgport.h> 44 #include <sys/sysctl.h> 45 #include <sys/ucred.h> 46 #include <sys/devfs.h> 47 #include <sys/devfs_rules.h> 48 #include <sys/udev.h> 49 50 #include <sys/msgport2.h> 51 #include <sys/spinlock2.h> 52 #include <sys/mplock2.h> 53 #include <sys/sysref2.h> 54 55 MALLOC_DEFINE(M_DEVFS, "devfs", "Device File System (devfs) allocations"); 56 DEVFS_DECLARE_CLONE_BITMAP(ops_id); 57 /* 58 * SYSREF Integration - reference counting, allocation, 59 * sysid and syslink integration. 60 */ 61 static void devfs_cdev_terminate(cdev_t dev); 62 static void devfs_cdev_lock(cdev_t dev); 63 static void devfs_cdev_unlock(cdev_t dev); 64 static struct sysref_class cdev_sysref_class = { 65 .name = "cdev", 66 .mtype = M_DEVFS, 67 .proto = SYSREF_PROTO_DEV, 68 .offset = offsetof(struct cdev, si_sysref), 69 .objsize = sizeof(struct cdev), 70 .nom_cache = 32, 71 .flags = 0, 72 .ops = { 73 .terminate = (sysref_terminate_func_t)devfs_cdev_terminate, 74 .lock = (sysref_lock_func_t)devfs_cdev_lock, 75 .unlock = (sysref_unlock_func_t)devfs_cdev_unlock 76 } 77 }; 78 79 static struct objcache *devfs_node_cache; 80 static struct objcache *devfs_msg_cache; 81 static struct objcache *devfs_dev_cache; 82 83 static struct objcache_malloc_args devfs_node_malloc_args = { 84 sizeof(struct devfs_node), M_DEVFS }; 85 struct objcache_malloc_args devfs_msg_malloc_args = { 86 sizeof(struct devfs_msg), M_DEVFS }; 87 struct objcache_malloc_args devfs_dev_malloc_args = { 88 sizeof(struct cdev), M_DEVFS }; 89 90 static struct devfs_dev_head devfs_dev_list = 91 TAILQ_HEAD_INITIALIZER(devfs_dev_list); 92 static struct devfs_mnt_head devfs_mnt_list = 93 TAILQ_HEAD_INITIALIZER(devfs_mnt_list); 94 static struct devfs_chandler_head devfs_chandler_list = 95 TAILQ_HEAD_INITIALIZER(devfs_chandler_list); 96 static struct devfs_alias_head devfs_alias_list = 97 TAILQ_HEAD_INITIALIZER(devfs_alias_list); 98 static struct devfs_dev_ops_head devfs_dev_ops_list = 99 TAILQ_HEAD_INITIALIZER(devfs_dev_ops_list); 100 101 struct lock devfs_lock; 102 static struct lwkt_port devfs_dispose_port; 103 static struct lwkt_port devfs_msg_port; 104 static struct thread *td_core; 105 106 static struct spinlock ino_lock; 107 static ino_t d_ino; 108 static int devfs_debug_enable; 109 static int devfs_run; 110 111 static ino_t devfs_fetch_ino(void); 112 static int devfs_create_all_dev_worker(struct devfs_node *); 113 static int devfs_create_dev_worker(cdev_t, uid_t, gid_t, int); 114 static int devfs_destroy_dev_worker(cdev_t); 115 static int devfs_destroy_related_worker(cdev_t); 116 static int devfs_destroy_dev_by_ops_worker(struct dev_ops *, int); 117 static int devfs_propagate_dev(cdev_t, int); 118 static int devfs_unlink_dev(cdev_t dev); 119 static void devfs_msg_exec(devfs_msg_t msg); 120 121 static int devfs_chandler_add_worker(const char *, d_clone_t *); 122 static int devfs_chandler_del_worker(const char *); 123 124 static void devfs_msg_autofree_reply(lwkt_port_t, lwkt_msg_t); 125 static void devfs_msg_core(void *); 126 127 static int devfs_find_device_by_name_worker(devfs_msg_t); 128 static int devfs_find_device_by_udev_worker(devfs_msg_t); 129 130 static int devfs_apply_reset_rules_caller(char *, int); 131 132 static int devfs_scan_callback_worker(devfs_scan_t *, void *); 133 134 static struct devfs_node *devfs_resolve_or_create_dir(struct devfs_node *, 135 char *, size_t, int); 136 137 static int devfs_make_alias_worker(struct devfs_alias *); 138 static int devfs_destroy_alias_worker(struct devfs_alias *); 139 static int devfs_alias_remove(cdev_t); 140 static int devfs_alias_reap(void); 141 static int devfs_alias_propagate(struct devfs_alias *, int); 142 static int devfs_alias_apply(struct devfs_node *, struct devfs_alias *); 143 static int devfs_alias_check_create(struct devfs_node *); 144 145 static int devfs_clr_related_flag_worker(cdev_t, uint32_t); 146 static int devfs_destroy_related_without_flag_worker(cdev_t, uint32_t); 147 148 static void *devfs_reaperp_callback(struct devfs_node *, void *); 149 static void *devfs_gc_dirs_callback(struct devfs_node *, void *); 150 static void *devfs_gc_links_callback(struct devfs_node *, struct devfs_node *); 151 static void * 152 devfs_inode_to_vnode_worker_callback(struct devfs_node *, ino_t *); 153 154 /* 155 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function 156 * using kvprintf 157 */ 158 int 159 devfs_debug(int level, char *fmt, ...) 160 { 161 __va_list ap; 162 163 __va_start(ap, fmt); 164 if (level <= devfs_debug_enable) 165 kvprintf(fmt, ap); 166 __va_end(ap); 167 168 return 0; 169 } 170 171 /* 172 * devfs_allocp() Allocates a new devfs node with the specified 173 * parameters. The node is also automatically linked into the topology 174 * if a parent is specified. It also calls the rule and alias stuff to 175 * be applied on the new node 176 */ 177 struct devfs_node * 178 devfs_allocp(devfs_nodetype devfsnodetype, char *name, 179 struct devfs_node *parent, struct mount *mp, cdev_t dev) 180 { 181 struct devfs_node *node = NULL; 182 size_t namlen = strlen(name); 183 184 node = objcache_get(devfs_node_cache, M_WAITOK); 185 bzero(node, sizeof(*node)); 186 187 atomic_add_long(&DEVFS_MNTDATA(mp)->leak_count, 1); 188 189 node->d_dev = NULL; 190 node->nchildren = 1; 191 node->mp = mp; 192 node->d_dir.d_ino = devfs_fetch_ino(); 193 194 /* 195 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries 196 * respectively. 197 */ 198 node->cookie_jar = 2; 199 200 /* 201 * Access Control members 202 */ 203 node->mode = DEVFS_DEFAULT_MODE; 204 node->uid = DEVFS_DEFAULT_UID; 205 node->gid = DEVFS_DEFAULT_GID; 206 207 switch (devfsnodetype) { 208 case Nroot: 209 /* 210 * Ensure that we don't recycle the root vnode by marking it as 211 * linked into the topology. 212 */ 213 node->flags |= DEVFS_NODE_LINKED; 214 case Ndir: 215 TAILQ_INIT(DEVFS_DENODE_HEAD(node)); 216 node->d_dir.d_type = DT_DIR; 217 node->nchildren = 2; 218 break; 219 220 case Nlink: 221 node->d_dir.d_type = DT_LNK; 222 break; 223 224 case Nreg: 225 node->d_dir.d_type = DT_REG; 226 break; 227 228 case Ndev: 229 if (dev != NULL) { 230 node->d_dir.d_type = DT_CHR; 231 node->d_dev = dev; 232 233 node->mode = dev->si_perms; 234 node->uid = dev->si_uid; 235 node->gid = dev->si_gid; 236 237 devfs_alias_check_create(node); 238 } 239 break; 240 241 default: 242 panic("devfs_allocp: unknown node type"); 243 } 244 245 node->v_node = NULL; 246 node->node_type = devfsnodetype; 247 248 /* Initialize the dirent structure of each devfs vnode */ 249 node->d_dir.d_namlen = namlen; 250 node->d_dir.d_name = kmalloc(namlen+1, M_DEVFS, M_WAITOK); 251 memcpy(node->d_dir.d_name, name, namlen); 252 node->d_dir.d_name[namlen] = '\0'; 253 254 /* Initialize the parent node element */ 255 node->parent = parent; 256 257 /* Initialize *time members */ 258 nanotime(&node->atime); 259 node->mtime = node->ctime = node->atime; 260 261 /* 262 * Associate with parent as last step, clean out namecache 263 * reference. 264 */ 265 if ((parent != NULL) && 266 ((parent->node_type == Nroot) || (parent->node_type == Ndir))) { 267 parent->nchildren++; 268 node->cookie = parent->cookie_jar++; 269 node->flags |= DEVFS_NODE_LINKED; 270 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent), node, link); 271 272 /* This forces negative namecache lookups to clear */ 273 ++mp->mnt_namecache_gen; 274 } 275 276 /* Apply rules */ 277 devfs_rule_check_apply(node, NULL); 278 279 atomic_add_long(&DEVFS_MNTDATA(mp)->file_count, 1); 280 281 return node; 282 } 283 284 /* 285 * devfs_allocv() allocates a new vnode based on a devfs node. 286 */ 287 int 288 devfs_allocv(struct vnode **vpp, struct devfs_node *node) 289 { 290 struct vnode *vp; 291 int error = 0; 292 293 KKASSERT(node); 294 295 /* 296 * devfs master lock must not be held across a vget() call, we have 297 * to hold our ad-hoc vp to avoid a free race from destroying the 298 * contents of the structure. The vget() will interlock recycles 299 * for us. 300 */ 301 try_again: 302 while ((vp = node->v_node) != NULL) { 303 vhold(vp); 304 lockmgr(&devfs_lock, LK_RELEASE); 305 error = vget(vp, LK_EXCLUSIVE); 306 vdrop(vp); 307 lockmgr(&devfs_lock, LK_EXCLUSIVE); 308 if (error == 0) { 309 *vpp = vp; 310 goto out; 311 } 312 if (error != ENOENT) { 313 *vpp = NULL; 314 goto out; 315 } 316 } 317 318 /* 319 * devfs master lock must not be held across a getnewvnode() call. 320 */ 321 lockmgr(&devfs_lock, LK_RELEASE); 322 if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0) { 323 lockmgr(&devfs_lock, LK_EXCLUSIVE); 324 goto out; 325 } 326 lockmgr(&devfs_lock, LK_EXCLUSIVE); 327 328 vp = *vpp; 329 330 if (node->v_node != NULL) { 331 vp->v_type = VBAD; 332 vx_put(vp); 333 goto try_again; 334 } 335 336 vp->v_data = node; 337 node->v_node = vp; 338 339 switch (node->node_type) { 340 case Nroot: 341 vsetflags(vp, VROOT); 342 /* fall through */ 343 case Ndir: 344 vp->v_type = VDIR; 345 break; 346 347 case Nlink: 348 vp->v_type = VLNK; 349 break; 350 351 case Nreg: 352 vp->v_type = VREG; 353 break; 354 355 case Ndev: 356 vp->v_type = VCHR; 357 KKASSERT(node->d_dev); 358 359 vp->v_uminor = node->d_dev->si_uminor; 360 vp->v_umajor = node->d_dev->si_umajor; 361 362 v_associate_rdev(vp, node->d_dev); 363 vp->v_ops = &node->mp->mnt_vn_spec_ops; 364 break; 365 366 default: 367 panic("devfs_allocv: unknown node type"); 368 } 369 370 out: 371 return error; 372 } 373 374 /* 375 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode 376 * based on the newly created devfs node. 377 */ 378 int 379 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype, 380 char *name, struct devfs_node *parent, cdev_t dev) 381 { 382 struct devfs_node *node; 383 384 node = devfs_allocp(devfsnodetype, name, parent, mp, dev); 385 386 if (node != NULL) 387 devfs_allocv(vpp, node); 388 else 389 *vpp = NULL; 390 391 return 0; 392 } 393 394 /* 395 * Destroy the devfs_node. The node must be unlinked from the topology. 396 * 397 * This function will also destroy any vnode association with the node 398 * and device. 399 * 400 * The cdev_t itself remains intact. 401 * 402 * The core lock is not necessarily held on call and must be temporarily 403 * released if it is to avoid a deadlock. 404 */ 405 int 406 devfs_freep(struct devfs_node *node) 407 { 408 struct vnode *vp; 409 int relock; 410 411 KKASSERT(node); 412 KKASSERT(((node->flags & DEVFS_NODE_LINKED) == 0) || 413 (node->node_type == Nroot)); 414 415 /* 416 * Protect against double frees 417 */ 418 KKASSERT((node->flags & DEVFS_DESTROYED) == 0); 419 node->flags |= DEVFS_DESTROYED; 420 421 /* 422 * Avoid deadlocks between devfs_lock and the vnode lock when 423 * disassociating the vnode (stress2 pty vs ls -la /dev/pts). 424 * 425 * This also prevents the vnode reclaim code from double-freeing 426 * the node. The vget() is required to safely modified the vp 427 * and cycle the refs to terminate an inactive vp. 428 */ 429 if (lockstatus(&devfs_lock, curthread) == LK_EXCLUSIVE) { 430 lockmgr(&devfs_lock, LK_RELEASE); 431 relock = 1; 432 } else { 433 relock = 0; 434 } 435 436 while ((vp = node->v_node) != NULL) { 437 if (vget(vp, LK_EXCLUSIVE | LK_RETRY) != 0) 438 break; 439 v_release_rdev(vp); 440 vp->v_data = NULL; 441 node->v_node = NULL; 442 vput(vp); 443 } 444 445 /* 446 * Remaining cleanup 447 */ 448 atomic_subtract_long(&DEVFS_MNTDATA(node->mp)->leak_count, 1); 449 if (node->symlink_name) { 450 kfree(node->symlink_name, M_DEVFS); 451 node->symlink_name = NULL; 452 } 453 454 /* 455 * Remove the node from the orphan list if it is still on it. 456 */ 457 if (node->flags & DEVFS_ORPHANED) 458 devfs_tracer_del_orphan(node); 459 460 if (node->d_dir.d_name) { 461 kfree(node->d_dir.d_name, M_DEVFS); 462 node->d_dir.d_name = NULL; 463 } 464 atomic_subtract_long(&DEVFS_MNTDATA(node->mp)->file_count, 1); 465 objcache_put(devfs_node_cache, node); 466 467 if (relock) 468 lockmgr(&devfs_lock, LK_EXCLUSIVE); 469 470 return 0; 471 } 472 473 /* 474 * Returns a valid vp associated with the devfs alias node or NULL 475 */ 476 static void *devfs_alias_getvp(struct devfs_node *node) 477 { 478 struct devfs_node *found = node; 479 int depth = 0; 480 481 while ((found->node_type == Nlink) && (found->link_target)) { 482 if (depth >= 8) { 483 devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8"); 484 break; 485 } 486 487 found = found->link_target; 488 ++depth; 489 } 490 491 return found->v_node; 492 } 493 494 /* 495 * Unlink the devfs node from the topology and add it to the orphan list. 496 * The node will later be destroyed by freep. 497 * 498 * Any vnode association, including the v_rdev and v_data, remains intact 499 * until the freep. 500 */ 501 int 502 devfs_unlinkp(struct devfs_node *node) 503 { 504 struct vnode *vp; 505 struct devfs_node *parent; 506 KKASSERT(node); 507 508 /* 509 * Add the node to the orphan list, so it is referenced somewhere, to 510 * so we don't leak it. 511 */ 512 devfs_tracer_add_orphan(node); 513 514 parent = node->parent; 515 516 /* 517 * If the parent is known we can unlink the node out of the topology 518 */ 519 if (parent) { 520 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link); 521 parent->nchildren--; 522 node->flags &= ~DEVFS_NODE_LINKED; 523 } 524 525 node->parent = NULL; 526 527 /* 528 * Namecache invalidation. 529 * devfs alias nodes are special: their v_node entry is always null 530 * and they use the one from their link target. 531 * We thus use the target node's vp to invalidate both alias and target 532 * entries in the namecache. 533 * Doing so for the target is not necessary but it would be more 534 * expensive to resolve only the namecache entry of the alias node 535 * from the information available in this function. 536 */ 537 if (node->node_type == Nlink) 538 vp = devfs_alias_getvp(node); 539 else 540 vp = node->v_node; 541 542 if (vp != NULL) 543 cache_inval_vp(vp, CINV_DESTROY); 544 545 return 0; 546 } 547 548 void * 549 devfs_iterate_topology(struct devfs_node *node, 550 devfs_iterate_callback_t *callback, void *arg1) 551 { 552 struct devfs_node *node1, *node2; 553 void *ret = NULL; 554 555 if ((node->node_type == Nroot) || (node->node_type == Ndir)) { 556 if (node->nchildren > 2) { 557 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), 558 link, node2) { 559 if ((ret = devfs_iterate_topology(node1, callback, arg1))) 560 return ret; 561 } 562 } 563 } 564 565 ret = callback(node, arg1); 566 return ret; 567 } 568 569 static void * 570 devfs_alias_reaper_callback(struct devfs_node *node, void *unused) 571 { 572 if (node->node_type == Nlink) { 573 devfs_unlinkp(node); 574 devfs_freep(node); 575 } 576 577 return NULL; 578 } 579 580 /* 581 * devfs_reaperp() is a recursive function that iterates through all the 582 * topology, unlinking and freeing all devfs nodes. 583 */ 584 static void * 585 devfs_reaperp_callback(struct devfs_node *node, void *unused) 586 { 587 devfs_unlinkp(node); 588 devfs_freep(node); 589 590 return NULL; 591 } 592 593 static void * 594 devfs_gc_dirs_callback(struct devfs_node *node, void *unused) 595 { 596 if (node->node_type == Ndir) { 597 if ((node->nchildren == 2) && 598 !(node->flags & DEVFS_USER_CREATED)) { 599 devfs_unlinkp(node); 600 devfs_freep(node); 601 } 602 } 603 604 return NULL; 605 } 606 607 static void * 608 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target) 609 { 610 if ((node->node_type == Nlink) && (node->link_target == target)) { 611 devfs_unlinkp(node); 612 devfs_freep(node); 613 } 614 615 return NULL; 616 } 617 618 /* 619 * devfs_gc() is devfs garbage collector. It takes care of unlinking and 620 * freeing a node, but also removes empty directories and links that link 621 * via devfs auto-link mechanism to the node being deleted. 622 */ 623 int 624 devfs_gc(struct devfs_node *node) 625 { 626 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node; 627 628 if (node->nlinks > 0) 629 devfs_iterate_topology(root_node, 630 (devfs_iterate_callback_t *)devfs_gc_links_callback, node); 631 632 devfs_unlinkp(node); 633 devfs_iterate_topology(root_node, 634 (devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL); 635 636 devfs_freep(node); 637 638 return 0; 639 } 640 641 /* 642 * devfs_create_dev() is the asynchronous entry point for device creation. 643 * It just sends a message with the relevant details to the devfs core. 644 * 645 * This function will reference the passed device. The reference is owned 646 * by devfs and represents all of the device's node associations. 647 */ 648 int 649 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms) 650 { 651 reference_dev(dev); 652 devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms); 653 654 return 0; 655 } 656 657 /* 658 * devfs_destroy_dev() is the asynchronous entry point for device destruction. 659 * It just sends a message with the relevant details to the devfs core. 660 */ 661 int 662 devfs_destroy_dev(cdev_t dev) 663 { 664 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0); 665 return 0; 666 } 667 668 /* 669 * devfs_mount_add() is the synchronous entry point for adding a new devfs 670 * mount. It sends a synchronous message with the relevant details to the 671 * devfs core. 672 */ 673 int 674 devfs_mount_add(struct devfs_mnt_data *mnt) 675 { 676 devfs_msg_t msg; 677 678 msg = devfs_msg_get(); 679 msg->mdv_mnt = mnt; 680 devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg); 681 devfs_msg_put(msg); 682 683 return 0; 684 } 685 686 /* 687 * devfs_mount_del() is the synchronous entry point for removing a devfs mount. 688 * It sends a synchronous message with the relevant details to the devfs core. 689 */ 690 int 691 devfs_mount_del(struct devfs_mnt_data *mnt) 692 { 693 devfs_msg_t msg; 694 695 msg = devfs_msg_get(); 696 msg->mdv_mnt = mnt; 697 devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg); 698 devfs_msg_put(msg); 699 700 return 0; 701 } 702 703 /* 704 * devfs_destroy_related() is the synchronous entry point for device 705 * destruction by subname. It just sends a message with the relevant details to 706 * the devfs core. 707 */ 708 int 709 devfs_destroy_related(cdev_t dev) 710 { 711 devfs_msg_t msg; 712 713 msg = devfs_msg_get(); 714 msg->mdv_load = dev; 715 devfs_msg_send_sync(DEVFS_DESTROY_RELATED, msg); 716 devfs_msg_put(msg); 717 return 0; 718 } 719 720 int 721 devfs_clr_related_flag(cdev_t dev, uint32_t flag) 722 { 723 devfs_msg_t msg; 724 725 msg = devfs_msg_get(); 726 msg->mdv_flags.dev = dev; 727 msg->mdv_flags.flag = flag; 728 devfs_msg_send_sync(DEVFS_CLR_RELATED_FLAG, msg); 729 devfs_msg_put(msg); 730 731 return 0; 732 } 733 734 int 735 devfs_destroy_related_without_flag(cdev_t dev, uint32_t flag) 736 { 737 devfs_msg_t msg; 738 739 msg = devfs_msg_get(); 740 msg->mdv_flags.dev = dev; 741 msg->mdv_flags.flag = flag; 742 devfs_msg_send_sync(DEVFS_DESTROY_RELATED_WO_FLAG, msg); 743 devfs_msg_put(msg); 744 745 return 0; 746 } 747 748 /* 749 * devfs_create_all_dev is the asynchronous entry point to trigger device 750 * node creation. It just sends a message with the relevant details to 751 * the devfs core. 752 */ 753 int 754 devfs_create_all_dev(struct devfs_node *root) 755 { 756 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root); 757 return 0; 758 } 759 760 /* 761 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all 762 * devices with a specific set of dev_ops and minor. It just sends a 763 * message with the relevant details to the devfs core. 764 */ 765 int 766 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor) 767 { 768 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor); 769 return 0; 770 } 771 772 /* 773 * devfs_clone_handler_add is the synchronous entry point to add a new 774 * clone handler. It just sends a message with the relevant details to 775 * the devfs core. 776 */ 777 int 778 devfs_clone_handler_add(const char *name, d_clone_t *nhandler) 779 { 780 devfs_msg_t msg; 781 782 msg = devfs_msg_get(); 783 msg->mdv_chandler.name = name; 784 msg->mdv_chandler.nhandler = nhandler; 785 devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg); 786 devfs_msg_put(msg); 787 return 0; 788 } 789 790 /* 791 * devfs_clone_handler_del is the synchronous entry point to remove a 792 * clone handler. It just sends a message with the relevant details to 793 * the devfs core. 794 */ 795 int 796 devfs_clone_handler_del(const char *name) 797 { 798 devfs_msg_t msg; 799 800 msg = devfs_msg_get(); 801 msg->mdv_chandler.name = name; 802 msg->mdv_chandler.nhandler = NULL; 803 devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg); 804 devfs_msg_put(msg); 805 return 0; 806 } 807 808 /* 809 * devfs_find_device_by_name is the synchronous entry point to find a 810 * device given its name. It sends a synchronous message with the 811 * relevant details to the devfs core and returns the answer. 812 */ 813 cdev_t 814 devfs_find_device_by_name(const char *fmt, ...) 815 { 816 cdev_t found = NULL; 817 devfs_msg_t msg; 818 char *target; 819 __va_list ap; 820 821 if (fmt == NULL) 822 return NULL; 823 824 __va_start(ap, fmt); 825 kvasnrprintf(&target, PATH_MAX, 10, fmt, ap); 826 __va_end(ap); 827 828 msg = devfs_msg_get(); 829 msg->mdv_name = target; 830 devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg); 831 found = msg->mdv_cdev; 832 devfs_msg_put(msg); 833 kvasfree(&target); 834 835 return found; 836 } 837 838 /* 839 * devfs_find_device_by_udev is the synchronous entry point to find a 840 * device given its udev number. It sends a synchronous message with 841 * the relevant details to the devfs core and returns the answer. 842 */ 843 cdev_t 844 devfs_find_device_by_udev(udev_t udev) 845 { 846 cdev_t found = NULL; 847 devfs_msg_t msg; 848 849 msg = devfs_msg_get(); 850 msg->mdv_udev = udev; 851 devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg); 852 found = msg->mdv_cdev; 853 devfs_msg_put(msg); 854 855 devfs_debug(DEVFS_DEBUG_DEBUG, 856 "devfs_find_device_by_udev found? %s -end:3-\n", 857 ((found) ? found->si_name:"NO")); 858 return found; 859 } 860 861 struct vnode * 862 devfs_inode_to_vnode(struct mount *mp, ino_t target) 863 { 864 struct vnode *vp = NULL; 865 devfs_msg_t msg; 866 867 if (mp == NULL) 868 return NULL; 869 870 msg = devfs_msg_get(); 871 msg->mdv_ino.mp = mp; 872 msg->mdv_ino.ino = target; 873 devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg); 874 vp = msg->mdv_ino.vp; 875 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 876 devfs_msg_put(msg); 877 878 return vp; 879 } 880 881 /* 882 * devfs_make_alias is the asynchronous entry point to register an alias 883 * for a device. It just sends a message with the relevant details to the 884 * devfs core. 885 */ 886 int 887 devfs_make_alias(const char *name, cdev_t dev_target) 888 { 889 struct devfs_alias *alias; 890 size_t len; 891 892 len = strlen(name); 893 894 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK); 895 alias->name = kstrdup(name, M_DEVFS); 896 alias->namlen = len; 897 alias->dev_target = dev_target; 898 899 devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias); 900 return 0; 901 } 902 903 /* 904 * devfs_destroy_alias is the asynchronous entry point to deregister an alias 905 * for a device. It just sends a message with the relevant details to the 906 * devfs core. 907 */ 908 int 909 devfs_destroy_alias(const char *name, cdev_t dev_target) 910 { 911 struct devfs_alias *alias; 912 size_t len; 913 914 len = strlen(name); 915 916 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK); 917 alias->name = kstrdup(name, M_DEVFS); 918 alias->namlen = len; 919 alias->dev_target = dev_target; 920 921 devfs_msg_send_generic(DEVFS_DESTROY_ALIAS, alias); 922 return 0; 923 } 924 925 /* 926 * devfs_apply_rules is the asynchronous entry point to trigger application 927 * of all rules. It just sends a message with the relevant details to the 928 * devfs core. 929 */ 930 int 931 devfs_apply_rules(char *mntto) 932 { 933 char *new_name; 934 935 new_name = kstrdup(mntto, M_DEVFS); 936 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name); 937 938 return 0; 939 } 940 941 /* 942 * devfs_reset_rules is the asynchronous entry point to trigger reset of all 943 * rules. It just sends a message with the relevant details to the devfs core. 944 */ 945 int 946 devfs_reset_rules(char *mntto) 947 { 948 char *new_name; 949 950 new_name = kstrdup(mntto, M_DEVFS); 951 devfs_msg_send_name(DEVFS_RESET_RULES, new_name); 952 953 return 0; 954 } 955 956 957 /* 958 * devfs_scan_callback is the asynchronous entry point to call a callback 959 * on all cdevs. 960 * It just sends a message with the relevant details to the devfs core. 961 */ 962 int 963 devfs_scan_callback(devfs_scan_t *callback, void *arg) 964 { 965 devfs_msg_t msg; 966 967 KKASSERT(callback); 968 969 msg = devfs_msg_get(); 970 msg->mdv_load = callback; 971 msg->mdv_load2 = arg; 972 devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg); 973 devfs_msg_put(msg); 974 975 return 0; 976 } 977 978 979 /* 980 * Acts as a message drain. Any message that is replied to here gets destroyed 981 * and the memory freed. 982 */ 983 static void 984 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg) 985 { 986 devfs_msg_put((devfs_msg_t)msg); 987 } 988 989 /* 990 * devfs_msg_get allocates a new devfs msg and returns it. 991 */ 992 devfs_msg_t 993 devfs_msg_get(void) 994 { 995 return objcache_get(devfs_msg_cache, M_WAITOK); 996 } 997 998 /* 999 * devfs_msg_put deallocates a given devfs msg. 1000 */ 1001 int 1002 devfs_msg_put(devfs_msg_t msg) 1003 { 1004 objcache_put(devfs_msg_cache, msg); 1005 return 0; 1006 } 1007 1008 /* 1009 * devfs_msg_send is the generic asynchronous message sending facility 1010 * for devfs. By default the reply port is the automatic disposal port. 1011 * 1012 * If the current thread is the devfs_msg_port thread we execute the 1013 * operation synchronously. 1014 */ 1015 void 1016 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg) 1017 { 1018 lwkt_port_t port = &devfs_msg_port; 1019 1020 lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0); 1021 1022 devfs_msg->hdr.u.ms_result = cmd; 1023 1024 if (port->mpu_td == curthread) { 1025 devfs_msg_exec(devfs_msg); 1026 lwkt_replymsg(&devfs_msg->hdr, 0); 1027 } else { 1028 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg); 1029 } 1030 } 1031 1032 /* 1033 * devfs_msg_send_sync is the generic synchronous message sending 1034 * facility for devfs. It initializes a local reply port and waits 1035 * for the core's answer. The core will write the answer on the same 1036 * message which is sent back as reply. The caller still has a reference 1037 * to the message, so we don't need to return it. 1038 */ 1039 int 1040 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg) 1041 { 1042 struct lwkt_port rep_port; 1043 int error; 1044 lwkt_port_t port = &devfs_msg_port; 1045 1046 lwkt_initport_thread(&rep_port, curthread); 1047 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0); 1048 1049 devfs_msg->hdr.u.ms_result = cmd; 1050 1051 error = lwkt_domsg(port, (lwkt_msg_t)devfs_msg, 0); 1052 1053 return error; 1054 } 1055 1056 /* 1057 * sends a message with a generic argument. 1058 */ 1059 void 1060 devfs_msg_send_generic(uint32_t cmd, void *load) 1061 { 1062 devfs_msg_t devfs_msg = devfs_msg_get(); 1063 1064 devfs_msg->mdv_load = load; 1065 devfs_msg_send(cmd, devfs_msg); 1066 } 1067 1068 /* 1069 * sends a message with a name argument. 1070 */ 1071 void 1072 devfs_msg_send_name(uint32_t cmd, char *name) 1073 { 1074 devfs_msg_t devfs_msg = devfs_msg_get(); 1075 1076 devfs_msg->mdv_name = name; 1077 devfs_msg_send(cmd, devfs_msg); 1078 } 1079 1080 /* 1081 * sends a message with a mount argument. 1082 */ 1083 void 1084 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt) 1085 { 1086 devfs_msg_t devfs_msg = devfs_msg_get(); 1087 1088 devfs_msg->mdv_mnt = mnt; 1089 devfs_msg_send(cmd, devfs_msg); 1090 } 1091 1092 /* 1093 * sends a message with an ops argument. 1094 */ 1095 void 1096 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor) 1097 { 1098 devfs_msg_t devfs_msg = devfs_msg_get(); 1099 1100 devfs_msg->mdv_ops.ops = ops; 1101 devfs_msg->mdv_ops.minor = minor; 1102 devfs_msg_send(cmd, devfs_msg); 1103 } 1104 1105 /* 1106 * sends a message with a clone handler argument. 1107 */ 1108 void 1109 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler) 1110 { 1111 devfs_msg_t devfs_msg = devfs_msg_get(); 1112 1113 devfs_msg->mdv_chandler.name = name; 1114 devfs_msg->mdv_chandler.nhandler = handler; 1115 devfs_msg_send(cmd, devfs_msg); 1116 } 1117 1118 /* 1119 * sends a message with a device argument. 1120 */ 1121 void 1122 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms) 1123 { 1124 devfs_msg_t devfs_msg = devfs_msg_get(); 1125 1126 devfs_msg->mdv_dev.dev = dev; 1127 devfs_msg->mdv_dev.uid = uid; 1128 devfs_msg->mdv_dev.gid = gid; 1129 devfs_msg->mdv_dev.perms = perms; 1130 1131 devfs_msg_send(cmd, devfs_msg); 1132 } 1133 1134 /* 1135 * sends a message with a link argument. 1136 */ 1137 void 1138 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp) 1139 { 1140 devfs_msg_t devfs_msg = devfs_msg_get(); 1141 1142 devfs_msg->mdv_link.name = name; 1143 devfs_msg->mdv_link.target = target; 1144 devfs_msg->mdv_link.mp = mp; 1145 devfs_msg_send(cmd, devfs_msg); 1146 } 1147 1148 /* 1149 * devfs_msg_core is the main devfs thread. It handles all incoming messages 1150 * and calls the relevant worker functions. By using messages it's assured 1151 * that events occur in the correct order. 1152 */ 1153 static void 1154 devfs_msg_core(void *arg) 1155 { 1156 devfs_msg_t msg; 1157 1158 lwkt_initport_thread(&devfs_msg_port, curthread); 1159 1160 lockmgr(&devfs_lock, LK_EXCLUSIVE); 1161 devfs_run = 1; 1162 wakeup(td_core); 1163 lockmgr(&devfs_lock, LK_RELEASE); 1164 1165 get_mplock(); /* mpsafe yet? */ 1166 1167 while (devfs_run) { 1168 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0); 1169 devfs_debug(DEVFS_DEBUG_DEBUG, 1170 "devfs_msg_core, new msg: %x\n", 1171 (unsigned int)msg->hdr.u.ms_result); 1172 devfs_msg_exec(msg); 1173 lwkt_replymsg(&msg->hdr, 0); 1174 } 1175 1176 rel_mplock(); 1177 wakeup(td_core); 1178 1179 lwkt_exit(); 1180 } 1181 1182 static void 1183 devfs_msg_exec(devfs_msg_t msg) 1184 { 1185 struct devfs_mnt_data *mnt; 1186 struct devfs_node *node; 1187 cdev_t dev; 1188 1189 /* 1190 * Acquire the devfs lock to ensure safety of all called functions 1191 */ 1192 lockmgr(&devfs_lock, LK_EXCLUSIVE); 1193 1194 switch (msg->hdr.u.ms_result) { 1195 case DEVFS_DEVICE_CREATE: 1196 dev = msg->mdv_dev.dev; 1197 devfs_create_dev_worker(dev, 1198 msg->mdv_dev.uid, 1199 msg->mdv_dev.gid, 1200 msg->mdv_dev.perms); 1201 break; 1202 case DEVFS_DEVICE_DESTROY: 1203 dev = msg->mdv_dev.dev; 1204 devfs_destroy_dev_worker(dev); 1205 break; 1206 case DEVFS_DESTROY_RELATED: 1207 devfs_destroy_related_worker(msg->mdv_load); 1208 break; 1209 case DEVFS_DESTROY_DEV_BY_OPS: 1210 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops, 1211 msg->mdv_ops.minor); 1212 break; 1213 case DEVFS_CREATE_ALL_DEV: 1214 node = (struct devfs_node *)msg->mdv_load; 1215 devfs_create_all_dev_worker(node); 1216 break; 1217 case DEVFS_MOUNT_ADD: 1218 mnt = msg->mdv_mnt; 1219 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link); 1220 devfs_create_all_dev_worker(mnt->root_node); 1221 break; 1222 case DEVFS_MOUNT_DEL: 1223 mnt = msg->mdv_mnt; 1224 TAILQ_REMOVE(&devfs_mnt_list, mnt, link); 1225 /* Be sure to remove all the aliases first */ 1226 devfs_iterate_topology(mnt->root_node, devfs_alias_reaper_callback, 1227 NULL); 1228 devfs_iterate_topology(mnt->root_node, devfs_reaperp_callback, 1229 NULL); 1230 if (mnt->leak_count) { 1231 devfs_debug(DEVFS_DEBUG_SHOW, 1232 "Leaked %ld devfs_node elements!\n", 1233 mnt->leak_count); 1234 } 1235 break; 1236 case DEVFS_CHANDLER_ADD: 1237 devfs_chandler_add_worker(msg->mdv_chandler.name, 1238 msg->mdv_chandler.nhandler); 1239 break; 1240 case DEVFS_CHANDLER_DEL: 1241 devfs_chandler_del_worker(msg->mdv_chandler.name); 1242 break; 1243 case DEVFS_FIND_DEVICE_BY_NAME: 1244 devfs_find_device_by_name_worker(msg); 1245 break; 1246 case DEVFS_FIND_DEVICE_BY_UDEV: 1247 devfs_find_device_by_udev_worker(msg); 1248 break; 1249 case DEVFS_MAKE_ALIAS: 1250 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load); 1251 break; 1252 case DEVFS_DESTROY_ALIAS: 1253 devfs_destroy_alias_worker((struct devfs_alias *)msg->mdv_load); 1254 break; 1255 case DEVFS_APPLY_RULES: 1256 devfs_apply_reset_rules_caller(msg->mdv_name, 1); 1257 break; 1258 case DEVFS_RESET_RULES: 1259 devfs_apply_reset_rules_caller(msg->mdv_name, 0); 1260 break; 1261 case DEVFS_SCAN_CALLBACK: 1262 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load, 1263 msg->mdv_load2); 1264 break; 1265 case DEVFS_CLR_RELATED_FLAG: 1266 devfs_clr_related_flag_worker(msg->mdv_flags.dev, 1267 msg->mdv_flags.flag); 1268 break; 1269 case DEVFS_DESTROY_RELATED_WO_FLAG: 1270 devfs_destroy_related_without_flag_worker(msg->mdv_flags.dev, 1271 msg->mdv_flags.flag); 1272 break; 1273 case DEVFS_INODE_TO_VNODE: 1274 msg->mdv_ino.vp = devfs_iterate_topology( 1275 DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node, 1276 (devfs_iterate_callback_t *)devfs_inode_to_vnode_worker_callback, 1277 &msg->mdv_ino.ino); 1278 break; 1279 case DEVFS_TERMINATE_CORE: 1280 devfs_run = 0; 1281 break; 1282 case DEVFS_SYNC: 1283 break; 1284 default: 1285 devfs_debug(DEVFS_DEBUG_WARNING, 1286 "devfs_msg_core: unknown message " 1287 "received at core\n"); 1288 break; 1289 } 1290 lockmgr(&devfs_lock, LK_RELEASE); 1291 } 1292 1293 static void 1294 devfs_devctl_notify(cdev_t dev, const char *ev) 1295 { 1296 static const char prefix[] = "cdev="; 1297 char *data; 1298 int namelen; 1299 1300 namelen = strlen(dev->si_name); 1301 data = kmalloc(namelen + sizeof(prefix), M_TEMP, M_WAITOK); 1302 memcpy(data, prefix, sizeof(prefix) - 1); 1303 memcpy(data + sizeof(prefix) - 1, dev->si_name, namelen + 1); 1304 devctl_notify("DEVFS", "CDEV", ev, data); 1305 kfree(data, M_TEMP); 1306 } 1307 1308 /* 1309 * Worker function to insert a new dev into the dev list and initialize its 1310 * permissions. It also calls devfs_propagate_dev which in turn propagates 1311 * the change to all mount points. 1312 * 1313 * The passed dev is already referenced. This reference is eaten by this 1314 * function and represents the dev's linkage into devfs_dev_list. 1315 */ 1316 static int 1317 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms) 1318 { 1319 KKASSERT(dev); 1320 1321 dev->si_uid = uid; 1322 dev->si_gid = gid; 1323 dev->si_perms = perms; 1324 1325 devfs_link_dev(dev); 1326 devfs_propagate_dev(dev, 1); 1327 1328 udev_event_attach(dev, NULL, 0); 1329 devfs_devctl_notify(dev, "CREATE"); 1330 1331 return 0; 1332 } 1333 1334 /* 1335 * Worker function to delete a dev from the dev list and free the cdev. 1336 * It also calls devfs_propagate_dev which in turn propagates the change 1337 * to all mount points. 1338 */ 1339 static int 1340 devfs_destroy_dev_worker(cdev_t dev) 1341 { 1342 int error; 1343 1344 KKASSERT(dev); 1345 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE); 1346 1347 error = devfs_unlink_dev(dev); 1348 devfs_propagate_dev(dev, 0); 1349 1350 devfs_devctl_notify(dev, "DESTROY"); 1351 udev_event_detach(dev, NULL, 0); 1352 1353 if (error == 0) 1354 release_dev(dev); /* link ref */ 1355 release_dev(dev); 1356 release_dev(dev); 1357 1358 return 0; 1359 } 1360 1361 /* 1362 * Worker function to destroy all devices with a certain basename. 1363 * Calls devfs_destroy_dev_worker for the actual destruction. 1364 */ 1365 static int 1366 devfs_destroy_related_worker(cdev_t needle) 1367 { 1368 cdev_t dev; 1369 1370 restart: 1371 devfs_debug(DEVFS_DEBUG_DEBUG, "related worker: %s\n", 1372 needle->si_name); 1373 TAILQ_FOREACH(dev, &devfs_dev_list, link) { 1374 if (dev->si_parent == needle) { 1375 devfs_destroy_related_worker(dev); 1376 devfs_destroy_dev_worker(dev); 1377 goto restart; 1378 } 1379 } 1380 return 0; 1381 } 1382 1383 static int 1384 devfs_clr_related_flag_worker(cdev_t needle, uint32_t flag) 1385 { 1386 cdev_t dev, dev1; 1387 1388 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) { 1389 if (dev->si_parent == needle) { 1390 devfs_clr_related_flag_worker(dev, flag); 1391 dev->si_flags &= ~flag; 1392 } 1393 } 1394 1395 return 0; 1396 } 1397 1398 static int 1399 devfs_destroy_related_without_flag_worker(cdev_t needle, uint32_t flag) 1400 { 1401 cdev_t dev; 1402 1403 restart: 1404 devfs_debug(DEVFS_DEBUG_DEBUG, "related_wo_flag: %s\n", 1405 needle->si_name); 1406 1407 TAILQ_FOREACH(dev, &devfs_dev_list, link) { 1408 if (dev->si_parent == needle) { 1409 devfs_destroy_related_without_flag_worker(dev, flag); 1410 if (!(dev->si_flags & flag)) { 1411 devfs_destroy_dev_worker(dev); 1412 devfs_debug(DEVFS_DEBUG_DEBUG, 1413 "related_wo_flag: %s restart\n", dev->si_name); 1414 goto restart; 1415 } 1416 } 1417 } 1418 1419 return 0; 1420 } 1421 1422 /* 1423 * Worker function that creates all device nodes on top of a devfs 1424 * root node. 1425 */ 1426 static int 1427 devfs_create_all_dev_worker(struct devfs_node *root) 1428 { 1429 cdev_t dev; 1430 1431 KKASSERT(root); 1432 1433 TAILQ_FOREACH(dev, &devfs_dev_list, link) { 1434 devfs_create_device_node(root, dev, NULL, NULL, NULL); 1435 } 1436 1437 return 0; 1438 } 1439 1440 /* 1441 * Worker function that destroys all devices that match a specific 1442 * dev_ops and/or minor. If minor is less than 0, it is not matched 1443 * against. It also propagates all changes. 1444 */ 1445 static int 1446 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor) 1447 { 1448 cdev_t dev, dev1; 1449 1450 KKASSERT(ops); 1451 1452 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) { 1453 if (dev->si_ops != ops) 1454 continue; 1455 if ((minor < 0) || (dev->si_uminor == minor)) { 1456 devfs_destroy_dev_worker(dev); 1457 } 1458 } 1459 1460 return 0; 1461 } 1462 1463 /* 1464 * Worker function that registers a new clone handler in devfs. 1465 */ 1466 static int 1467 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler) 1468 { 1469 struct devfs_clone_handler *chandler = NULL; 1470 u_char len = strlen(name); 1471 1472 if (len == 0) 1473 return 1; 1474 1475 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) { 1476 if (chandler->namlen != len) 1477 continue; 1478 1479 if (!memcmp(chandler->name, name, len)) { 1480 /* Clonable basename already exists */ 1481 return 1; 1482 } 1483 } 1484 1485 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO); 1486 chandler->name = kstrdup(name, M_DEVFS); 1487 chandler->namlen = len; 1488 chandler->nhandler = nhandler; 1489 1490 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link); 1491 return 0; 1492 } 1493 1494 /* 1495 * Worker function that removes a given clone handler from the 1496 * clone handler list. 1497 */ 1498 static int 1499 devfs_chandler_del_worker(const char *name) 1500 { 1501 struct devfs_clone_handler *chandler, *chandler2; 1502 u_char len = strlen(name); 1503 1504 if (len == 0) 1505 return 1; 1506 1507 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) { 1508 if (chandler->namlen != len) 1509 continue; 1510 if (memcmp(chandler->name, name, len)) 1511 continue; 1512 1513 TAILQ_REMOVE(&devfs_chandler_list, chandler, link); 1514 kfree(chandler->name, M_DEVFS); 1515 kfree(chandler, M_DEVFS); 1516 break; 1517 } 1518 1519 return 0; 1520 } 1521 1522 /* 1523 * Worker function that finds a given device name and changes 1524 * the message received accordingly so that when replied to, 1525 * the answer is returned to the caller. 1526 */ 1527 static int 1528 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg) 1529 { 1530 struct devfs_alias *alias; 1531 cdev_t dev; 1532 cdev_t found = NULL; 1533 1534 TAILQ_FOREACH(dev, &devfs_dev_list, link) { 1535 if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) { 1536 found = dev; 1537 break; 1538 } 1539 } 1540 if (found == NULL) { 1541 TAILQ_FOREACH(alias, &devfs_alias_list, link) { 1542 if (strcmp(devfs_msg->mdv_name, alias->name) == 0) { 1543 found = alias->dev_target; 1544 break; 1545 } 1546 } 1547 } 1548 devfs_msg->mdv_cdev = found; 1549 1550 return 0; 1551 } 1552 1553 /* 1554 * Worker function that finds a given device udev and changes 1555 * the message received accordingly so that when replied to, 1556 * the answer is returned to the caller. 1557 */ 1558 static int 1559 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg) 1560 { 1561 cdev_t dev, dev1; 1562 cdev_t found = NULL; 1563 1564 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) { 1565 if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) { 1566 found = dev; 1567 break; 1568 } 1569 } 1570 devfs_msg->mdv_cdev = found; 1571 1572 return 0; 1573 } 1574 1575 /* 1576 * Worker function that inserts a given alias into the 1577 * alias list, and propagates the alias to all mount 1578 * points. 1579 */ 1580 static int 1581 devfs_make_alias_worker(struct devfs_alias *alias) 1582 { 1583 struct devfs_alias *alias2; 1584 size_t len = strlen(alias->name); 1585 int found = 0; 1586 1587 TAILQ_FOREACH(alias2, &devfs_alias_list, link) { 1588 if (len != alias2->namlen) 1589 continue; 1590 1591 if (!memcmp(alias->name, alias2->name, len)) { 1592 found = 1; 1593 break; 1594 } 1595 } 1596 1597 if (!found) { 1598 /* 1599 * The alias doesn't exist yet, so we add it to the alias list 1600 */ 1601 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link); 1602 devfs_alias_propagate(alias, 0); 1603 udev_event_attach(alias->dev_target, alias->name, 1); 1604 } else { 1605 devfs_debug(DEVFS_DEBUG_WARNING, 1606 "Warning: duplicate devfs_make_alias for %s\n", 1607 alias->name); 1608 kfree(alias->name, M_DEVFS); 1609 kfree(alias, M_DEVFS); 1610 } 1611 1612 return 0; 1613 } 1614 1615 /* 1616 * Worker function that delete a given alias from the 1617 * alias list, and propagates the removal to all mount 1618 * points. 1619 */ 1620 static int 1621 devfs_destroy_alias_worker(struct devfs_alias *alias) 1622 { 1623 struct devfs_alias *alias2; 1624 int found = 0; 1625 1626 TAILQ_FOREACH(alias2, &devfs_alias_list, link) { 1627 if (alias->dev_target != alias2->dev_target) 1628 continue; 1629 1630 if (devfs_WildCmp(alias->name, alias2->name) == 0) { 1631 found = 1; 1632 break; 1633 } 1634 } 1635 1636 if (!found) { 1637 devfs_debug(DEVFS_DEBUG_WARNING, 1638 "Warning: devfs_destroy_alias for inexistant alias: %s\n", 1639 alias->name); 1640 kfree(alias->name, M_DEVFS); 1641 kfree(alias, M_DEVFS); 1642 } else { 1643 /* 1644 * The alias exists, so we delete it from the alias list 1645 */ 1646 TAILQ_REMOVE(&devfs_alias_list, alias2, link); 1647 devfs_alias_propagate(alias2, 1); 1648 udev_event_detach(alias2->dev_target, alias2->name, 1); 1649 kfree(alias->name, M_DEVFS); 1650 kfree(alias, M_DEVFS); 1651 kfree(alias2->name, M_DEVFS); 1652 kfree(alias2, M_DEVFS); 1653 } 1654 1655 return 0; 1656 } 1657 1658 /* 1659 * Function that removes and frees all aliases. 1660 */ 1661 static int 1662 devfs_alias_reap(void) 1663 { 1664 struct devfs_alias *alias, *alias2; 1665 1666 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) { 1667 TAILQ_REMOVE(&devfs_alias_list, alias, link); 1668 kfree(alias->name, M_DEVFS); 1669 kfree(alias, M_DEVFS); 1670 } 1671 return 0; 1672 } 1673 1674 /* 1675 * Function that removes an alias matching a specific cdev and frees 1676 * it accordingly. 1677 */ 1678 static int 1679 devfs_alias_remove(cdev_t dev) 1680 { 1681 struct devfs_alias *alias, *alias2; 1682 1683 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) { 1684 if (alias->dev_target == dev) { 1685 TAILQ_REMOVE(&devfs_alias_list, alias, link); 1686 udev_event_detach(alias->dev_target, alias->name, 1); 1687 kfree(alias->name, M_DEVFS); 1688 kfree(alias, M_DEVFS); 1689 } 1690 } 1691 return 0; 1692 } 1693 1694 /* 1695 * This function propagates an alias addition or removal to 1696 * all mount points. 1697 */ 1698 static int 1699 devfs_alias_propagate(struct devfs_alias *alias, int remove) 1700 { 1701 struct devfs_mnt_data *mnt; 1702 1703 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) { 1704 if (remove) { 1705 devfs_destroy_node(mnt->root_node, alias->name); 1706 } else { 1707 devfs_alias_apply(mnt->root_node, alias); 1708 } 1709 } 1710 return 0; 1711 } 1712 1713 /* 1714 * This function is a recursive function iterating through 1715 * all device nodes in the topology and, if applicable, 1716 * creating the relevant alias for a device node. 1717 */ 1718 static int 1719 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias) 1720 { 1721 struct devfs_node *node1, *node2; 1722 1723 KKASSERT(alias != NULL); 1724 1725 if ((node->node_type == Nroot) || (node->node_type == Ndir)) { 1726 if (node->nchildren > 2) { 1727 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) { 1728 devfs_alias_apply(node1, alias); 1729 } 1730 } 1731 } else { 1732 if (node->d_dev == alias->dev_target) 1733 devfs_alias_create(alias->name, node, 0); 1734 } 1735 return 0; 1736 } 1737 1738 /* 1739 * This function checks if any alias possibly is applicable 1740 * to the given node. If so, the alias is created. 1741 */ 1742 static int 1743 devfs_alias_check_create(struct devfs_node *node) 1744 { 1745 struct devfs_alias *alias; 1746 1747 TAILQ_FOREACH(alias, &devfs_alias_list, link) { 1748 if (node->d_dev == alias->dev_target) 1749 devfs_alias_create(alias->name, node, 0); 1750 } 1751 return 0; 1752 } 1753 1754 /* 1755 * This function creates an alias with a given name 1756 * linking to a given devfs node. It also increments 1757 * the link count on the target node. 1758 */ 1759 int 1760 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based) 1761 { 1762 struct mount *mp = target->mp; 1763 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node; 1764 struct devfs_node *linknode; 1765 char *create_path = NULL; 1766 char *name; 1767 char *name_buf; 1768 int result = 0; 1769 1770 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE); 1771 1772 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK); 1773 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name); 1774 1775 if (create_path) 1776 parent = devfs_resolve_or_create_path(parent, create_path, 1); 1777 1778 1779 if (devfs_find_device_node_by_name(parent, name)) { 1780 devfs_debug(DEVFS_DEBUG_WARNING, 1781 "Node already exists: %s " 1782 "(devfs_make_alias_worker)!\n", 1783 name); 1784 result = 1; 1785 goto done; 1786 } 1787 1788 linknode = devfs_allocp(Nlink, name, parent, mp, NULL); 1789 if (linknode == NULL) { 1790 result = 1; 1791 goto done; 1792 } 1793 1794 linknode->link_target = target; 1795 target->nlinks++; 1796 1797 if (rule_based) 1798 linknode->flags |= DEVFS_RULE_CREATED; 1799 1800 done: 1801 kfree(name_buf, M_TEMP); 1802 return (result); 1803 } 1804 1805 /* 1806 * This function is called by the core and handles mount point 1807 * strings. It either calls the relevant worker (devfs_apply_ 1808 * reset_rules_worker) on all mountpoints or only a specific 1809 * one. 1810 */ 1811 static int 1812 devfs_apply_reset_rules_caller(char *mountto, int apply) 1813 { 1814 struct devfs_mnt_data *mnt; 1815 1816 if (mountto[0] == '*') { 1817 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) { 1818 devfs_iterate_topology(mnt->root_node, 1819 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node), 1820 NULL); 1821 } 1822 } else { 1823 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) { 1824 if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) { 1825 devfs_iterate_topology(mnt->root_node, 1826 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node), 1827 NULL); 1828 break; 1829 } 1830 } 1831 } 1832 1833 kfree(mountto, M_DEVFS); 1834 return 0; 1835 } 1836 1837 /* 1838 * This function calls a given callback function for 1839 * every dev node in the devfs dev list. 1840 */ 1841 static int 1842 devfs_scan_callback_worker(devfs_scan_t *callback, void *arg) 1843 { 1844 cdev_t dev, dev1; 1845 struct devfs_alias *alias, *alias1; 1846 1847 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) { 1848 callback(dev->si_name, dev, false, arg); 1849 } 1850 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias1) { 1851 callback(alias->name, alias->dev_target, true, arg); 1852 } 1853 1854 return 0; 1855 } 1856 1857 /* 1858 * This function tries to resolve a given directory, or if not 1859 * found and creation requested, creates the given directory. 1860 */ 1861 static struct devfs_node * 1862 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name, 1863 size_t name_len, int create) 1864 { 1865 struct devfs_node *node, *found = NULL; 1866 1867 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) { 1868 if (name_len != node->d_dir.d_namlen) 1869 continue; 1870 1871 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) { 1872 found = node; 1873 break; 1874 } 1875 } 1876 1877 if ((found == NULL) && (create)) { 1878 found = devfs_allocp(Ndir, dir_name, parent, parent->mp, NULL); 1879 } 1880 1881 return found; 1882 } 1883 1884 /* 1885 * This function tries to resolve a complete path. If creation is requested, 1886 * if a given part of the path cannot be resolved (because it doesn't exist), 1887 * it is created. 1888 */ 1889 struct devfs_node * 1890 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create) 1891 { 1892 struct devfs_node *node = parent; 1893 char *buf; 1894 size_t idx = 0; 1895 1896 if (path == NULL) 1897 return parent; 1898 1899 buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK); 1900 1901 while (*path && idx < PATH_MAX - 1) { 1902 if (*path != '/') { 1903 buf[idx++] = *path; 1904 } else { 1905 buf[idx] = '\0'; 1906 node = devfs_resolve_or_create_dir(node, buf, idx, create); 1907 if (node == NULL) { 1908 kfree(buf, M_TEMP); 1909 return NULL; 1910 } 1911 idx = 0; 1912 } 1913 ++path; 1914 } 1915 buf[idx] = '\0'; 1916 node = devfs_resolve_or_create_dir(node, buf, idx, create); 1917 kfree (buf, M_TEMP); 1918 return (node); 1919 } 1920 1921 /* 1922 * Takes a full path and strips it into a directory path and a name. 1923 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It 1924 * requires a working buffer with enough size to keep the whole 1925 * fullpath. 1926 */ 1927 int 1928 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep) 1929 { 1930 char *name = NULL; 1931 char *path = NULL; 1932 size_t len = strlen(fullpath) + 1; 1933 int i; 1934 1935 KKASSERT((fullpath != NULL) && (buf != NULL)); 1936 KKASSERT((pathp != NULL) && (namep != NULL)); 1937 1938 memcpy(buf, fullpath, len); 1939 1940 for (i = len-1; i>= 0; i--) { 1941 if (buf[i] == '/') { 1942 buf[i] = '\0'; 1943 name = &(buf[i+1]); 1944 path = buf; 1945 break; 1946 } 1947 } 1948 1949 *pathp = path; 1950 1951 if (name) { 1952 *namep = name; 1953 } else { 1954 *namep = buf; 1955 } 1956 1957 return 0; 1958 } 1959 1960 /* 1961 * This function creates a new devfs node for a given device. It can 1962 * handle a complete path as device name, and accordingly creates 1963 * the path and the final device node. 1964 * 1965 * The reference count on the passed dev remains unchanged. 1966 */ 1967 struct devfs_node * 1968 devfs_create_device_node(struct devfs_node *root, cdev_t dev, 1969 int *existsp, char *dev_name, char *path_fmt, ...) 1970 { 1971 struct devfs_node *parent, *node = NULL; 1972 char *path = NULL; 1973 char *name; 1974 char *name_buf; 1975 __va_list ap; 1976 int i, found; 1977 char *create_path = NULL; 1978 char *names = "pqrsPQRS"; 1979 1980 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK); 1981 1982 if (existsp) 1983 *existsp = 0; 1984 1985 if (path_fmt != NULL) { 1986 __va_start(ap, path_fmt); 1987 kvasnrprintf(&path, PATH_MAX, 10, path_fmt, ap); 1988 __va_end(ap); 1989 } 1990 1991 parent = devfs_resolve_or_create_path(root, path, 1); 1992 KKASSERT(parent); 1993 1994 devfs_resolve_name_path( 1995 ((dev_name == NULL) && (dev))?(dev->si_name):(dev_name), 1996 name_buf, &create_path, &name); 1997 1998 if (create_path) 1999 parent = devfs_resolve_or_create_path(parent, create_path, 1); 2000 2001 2002 node = devfs_find_device_node_by_name(parent, name); 2003 if (node) { 2004 if (node->d_dev == dev) { 2005 /* 2006 * Allow case where device caches dev after the 2007 * close and might desire to reuse it. 2008 */ 2009 if (existsp) 2010 *existsp = 1; 2011 } else { 2012 devfs_debug(DEVFS_DEBUG_WARNING, 2013 "devfs_create_device_node: " 2014 "DEVICE %s ALREADY EXISTS!!! " 2015 "Ignoring creation request.\n", 2016 name); 2017 node = NULL; 2018 } 2019 goto out; 2020 } 2021 2022 node = devfs_allocp(Ndev, name, parent, parent->mp, dev); 2023 nanotime(&parent->mtime); 2024 2025 /* 2026 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their 2027 * directory 2028 */ 2029 if ((dev) && (strlen(dev->si_name) >= 4) && 2030 (!memcmp(dev->si_name, "ptm/", 4))) { 2031 node->parent->flags |= DEVFS_HIDDEN; 2032 node->flags |= DEVFS_HIDDEN; 2033 } 2034 2035 /* 2036 * Ugly pty magic, to tag pty devices as such and hide them if needed. 2037 */ 2038 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3))) 2039 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE); 2040 2041 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) { 2042 found = 0; 2043 for (i = 0; i < strlen(names); i++) { 2044 if (name[3] == names[i]) { 2045 found = 1; 2046 break; 2047 } 2048 } 2049 if (found) 2050 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE); 2051 } 2052 2053 out: 2054 kfree(name_buf, M_TEMP); 2055 kvasfree(&path); 2056 return node; 2057 } 2058 2059 /* 2060 * This function finds a given device node in the topology with a given 2061 * cdev. 2062 */ 2063 void * 2064 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target) 2065 { 2066 if ((node->node_type == Ndev) && (node->d_dev == target)) { 2067 return node; 2068 } 2069 2070 return NULL; 2071 } 2072 2073 /* 2074 * This function finds a device node in the given parent directory by its 2075 * name and returns it. 2076 */ 2077 struct devfs_node * 2078 devfs_find_device_node_by_name(struct devfs_node *parent, char *target) 2079 { 2080 struct devfs_node *node, *found = NULL; 2081 size_t len = strlen(target); 2082 2083 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) { 2084 if (len != node->d_dir.d_namlen) 2085 continue; 2086 2087 if (!memcmp(node->d_dir.d_name, target, len)) { 2088 found = node; 2089 break; 2090 } 2091 } 2092 2093 return found; 2094 } 2095 2096 static void * 2097 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop) 2098 { 2099 struct vnode *vp = NULL; 2100 ino_t target = *inop; 2101 2102 if (node->d_dir.d_ino == target) { 2103 if (node->v_node) { 2104 vp = node->v_node; 2105 vget(vp, LK_EXCLUSIVE | LK_RETRY); 2106 vn_unlock(vp); 2107 } else { 2108 devfs_allocv(&vp, node); 2109 vn_unlock(vp); 2110 } 2111 } 2112 2113 return vp; 2114 } 2115 2116 /* 2117 * This function takes a cdev and removes its devfs node in the 2118 * given topology. The cdev remains intact. 2119 */ 2120 int 2121 devfs_destroy_device_node(struct devfs_node *root, cdev_t target) 2122 { 2123 KKASSERT(target != NULL); 2124 return devfs_destroy_node(root, target->si_name); 2125 } 2126 2127 /* 2128 * This function takes a path to a devfs node, resolves it and 2129 * removes the devfs node from the given topology. 2130 */ 2131 int 2132 devfs_destroy_node(struct devfs_node *root, char *target) 2133 { 2134 struct devfs_node *node, *parent; 2135 char *name; 2136 char *name_buf; 2137 char *create_path = NULL; 2138 2139 KKASSERT(target); 2140 2141 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK); 2142 ksnprintf(name_buf, PATH_MAX, "%s", target); 2143 2144 devfs_resolve_name_path(target, name_buf, &create_path, &name); 2145 2146 if (create_path) 2147 parent = devfs_resolve_or_create_path(root, create_path, 0); 2148 else 2149 parent = root; 2150 2151 if (parent == NULL) { 2152 kfree(name_buf, M_TEMP); 2153 return 1; 2154 } 2155 2156 node = devfs_find_device_node_by_name(parent, name); 2157 2158 if (node) { 2159 nanotime(&node->parent->mtime); 2160 devfs_gc(node); 2161 } 2162 2163 kfree(name_buf, M_TEMP); 2164 2165 return 0; 2166 } 2167 2168 /* 2169 * Just set perms and ownership for given node. 2170 */ 2171 int 2172 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid, 2173 u_short mode, u_long flags) 2174 { 2175 node->mode = mode; 2176 node->uid = uid; 2177 node->gid = gid; 2178 2179 return 0; 2180 } 2181 2182 /* 2183 * Propagates a device attach/detach to all mount 2184 * points. Also takes care of automatic alias removal 2185 * for a deleted cdev. 2186 */ 2187 static int 2188 devfs_propagate_dev(cdev_t dev, int attach) 2189 { 2190 struct devfs_mnt_data *mnt; 2191 2192 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) { 2193 if (attach) { 2194 /* Device is being attached */ 2195 devfs_create_device_node(mnt->root_node, dev, 2196 NULL, NULL, NULL); 2197 } else { 2198 /* Device is being detached */ 2199 devfs_alias_remove(dev); 2200 devfs_destroy_device_node(mnt->root_node, dev); 2201 } 2202 } 2203 return 0; 2204 } 2205 2206 /* 2207 * devfs_clone either returns a basename from a complete name by 2208 * returning the length of the name without trailing digits, or, 2209 * if clone != 0, calls the device's clone handler to get a new 2210 * device, which in turn is returned in devp. 2211 */ 2212 cdev_t 2213 devfs_clone(cdev_t dev, const char *name, size_t len, int mode, 2214 struct ucred *cred) 2215 { 2216 int error; 2217 struct devfs_clone_handler *chandler; 2218 struct dev_clone_args ap; 2219 2220 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) { 2221 if (chandler->namlen != len) 2222 continue; 2223 if ((!memcmp(chandler->name, name, len)) && (chandler->nhandler)) { 2224 lockmgr(&devfs_lock, LK_RELEASE); 2225 devfs_config(); 2226 lockmgr(&devfs_lock, LK_EXCLUSIVE); 2227 2228 ap.a_head.a_dev = dev; 2229 ap.a_dev = NULL; 2230 ap.a_name = name; 2231 ap.a_namelen = len; 2232 ap.a_mode = mode; 2233 ap.a_cred = cred; 2234 error = (chandler->nhandler)(&ap); 2235 if (error) 2236 continue; 2237 2238 return ap.a_dev; 2239 } 2240 } 2241 2242 return NULL; 2243 } 2244 2245 2246 /* 2247 * Registers a new orphan in the orphan list. 2248 */ 2249 void 2250 devfs_tracer_add_orphan(struct devfs_node *node) 2251 { 2252 struct devfs_orphan *orphan; 2253 2254 KKASSERT(node); 2255 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK); 2256 orphan->node = node; 2257 2258 KKASSERT((node->flags & DEVFS_ORPHANED) == 0); 2259 node->flags |= DEVFS_ORPHANED; 2260 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link); 2261 } 2262 2263 /* 2264 * Removes an orphan from the orphan list. 2265 */ 2266 void 2267 devfs_tracer_del_orphan(struct devfs_node *node) 2268 { 2269 struct devfs_orphan *orphan; 2270 2271 KKASSERT(node); 2272 2273 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) { 2274 if (orphan->node == node) { 2275 node->flags &= ~DEVFS_ORPHANED; 2276 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link); 2277 kfree(orphan, M_DEVFS); 2278 break; 2279 } 2280 } 2281 } 2282 2283 /* 2284 * Counts the orphans in the orphan list, and if cleanup 2285 * is specified, also frees the orphan and removes it from 2286 * the list. 2287 */ 2288 size_t 2289 devfs_tracer_orphan_count(struct mount *mp, int cleanup) 2290 { 2291 struct devfs_orphan *orphan, *orphan2; 2292 size_t count = 0; 2293 2294 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) { 2295 count++; 2296 /* 2297 * If we are instructed to clean up, we do so. 2298 */ 2299 if (cleanup) { 2300 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link); 2301 orphan->node->flags &= ~DEVFS_ORPHANED; 2302 devfs_freep(orphan->node); 2303 kfree(orphan, M_DEVFS); 2304 } 2305 } 2306 2307 return count; 2308 } 2309 2310 /* 2311 * Fetch an ino_t from the global d_ino by increasing it 2312 * while spinlocked. 2313 */ 2314 static ino_t 2315 devfs_fetch_ino(void) 2316 { 2317 ino_t ret; 2318 2319 spin_lock(&ino_lock); 2320 ret = d_ino++; 2321 spin_unlock(&ino_lock); 2322 2323 return ret; 2324 } 2325 2326 /* 2327 * Allocates a new cdev and initializes it's most basic 2328 * fields. 2329 */ 2330 cdev_t 2331 devfs_new_cdev(struct dev_ops *ops, int minor, struct dev_ops *bops) 2332 { 2333 cdev_t dev = sysref_alloc(&cdev_sysref_class); 2334 2335 sysref_activate(&dev->si_sysref); 2336 reference_dev(dev); 2337 bzero(dev, offsetof(struct cdev, si_sysref)); 2338 2339 dev->si_uid = 0; 2340 dev->si_gid = 0; 2341 dev->si_perms = 0; 2342 dev->si_drv1 = NULL; 2343 dev->si_drv2 = NULL; 2344 dev->si_lastread = 0; /* time_uptime */ 2345 dev->si_lastwrite = 0; /* time_uptime */ 2346 2347 dev->si_dict = NULL; 2348 dev->si_parent = NULL; 2349 dev->si_ops = ops; 2350 dev->si_flags = 0; 2351 dev->si_uminor = minor; 2352 dev->si_bops = bops; 2353 2354 /* 2355 * Since the disk subsystem is in the way, we need to 2356 * propagate the D_CANFREE from bops (and ops) to 2357 * si_flags. 2358 */ 2359 if (bops && (bops->head.flags & D_CANFREE)) { 2360 dev->si_flags |= SI_CANFREE; 2361 } else if (ops->head.flags & D_CANFREE) { 2362 dev->si_flags |= SI_CANFREE; 2363 } 2364 2365 /* If there is a backing device, we reference its ops */ 2366 dev->si_inode = makeudev( 2367 devfs_reference_ops((bops)?(bops):(ops)), 2368 minor ); 2369 dev->si_umajor = umajor(dev->si_inode); 2370 2371 return dev; 2372 } 2373 2374 static void 2375 devfs_cdev_terminate(cdev_t dev) 2376 { 2377 int locked = 0; 2378 2379 /* Check if it is locked already. if not, we acquire the devfs lock */ 2380 if ((lockstatus(&devfs_lock, curthread)) != LK_EXCLUSIVE) { 2381 lockmgr(&devfs_lock, LK_EXCLUSIVE); 2382 locked = 1; 2383 } 2384 2385 /* 2386 * Make sure the node isn't linked anymore. Otherwise we've screwed 2387 * up somewhere, since normal devs are unlinked on the call to 2388 * destroy_dev and only-cdevs that have not been used for cloning 2389 * are not linked in the first place. only-cdevs used for cloning 2390 * will be linked in, too, and should only be destroyed via 2391 * destroy_dev, not destroy_only_dev, so we catch that problem, too. 2392 */ 2393 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0); 2394 2395 /* If we acquired the lock, we also get rid of it */ 2396 if (locked) 2397 lockmgr(&devfs_lock, LK_RELEASE); 2398 2399 /* If there is a backing device, we release the backing device's ops */ 2400 devfs_release_ops((dev->si_bops)?(dev->si_bops):(dev->si_ops)); 2401 2402 /* Finally destroy the device */ 2403 sysref_put(&dev->si_sysref); 2404 } 2405 2406 /* 2407 * Dummies for now (individual locks for MPSAFE) 2408 */ 2409 static void 2410 devfs_cdev_lock(cdev_t dev) 2411 { 2412 } 2413 2414 static void 2415 devfs_cdev_unlock(cdev_t dev) 2416 { 2417 } 2418 2419 static int 2420 devfs_detached_filter_eof(struct knote *kn, long hint) 2421 { 2422 kn->kn_flags |= (EV_EOF | EV_NODATA); 2423 return (1); 2424 } 2425 2426 static void 2427 devfs_detached_filter_detach(struct knote *kn) 2428 { 2429 cdev_t dev = (cdev_t)kn->kn_hook; 2430 2431 knote_remove(&dev->si_kqinfo.ki_note, kn); 2432 } 2433 2434 static struct filterops devfs_detached_filterops = 2435 { FILTEROP_ISFD, NULL, 2436 devfs_detached_filter_detach, 2437 devfs_detached_filter_eof }; 2438 2439 /* 2440 * Delegates knote filter handling responsibility to devfs 2441 * 2442 * Any device that implements kqfilter event handling and could be detached 2443 * or shut down out from under the kevent subsystem must allow devfs to 2444 * assume responsibility for any knotes it may hold. 2445 */ 2446 void 2447 devfs_assume_knotes(cdev_t dev, struct kqinfo *kqi) 2448 { 2449 /* 2450 * Let kern/kern_event.c do the heavy lifting. 2451 */ 2452 knote_assume_knotes(kqi, &dev->si_kqinfo, 2453 &devfs_detached_filterops, (void *)dev); 2454 2455 /* 2456 * These should probably be activated individually, but doing so 2457 * would require refactoring kq's public in-kernel interface. 2458 */ 2459 KNOTE(&dev->si_kqinfo.ki_note, 0); 2460 } 2461 2462 /* 2463 * Links a given cdev into the dev list. 2464 */ 2465 int 2466 devfs_link_dev(cdev_t dev) 2467 { 2468 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0); 2469 dev->si_flags |= SI_DEVFS_LINKED; 2470 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link); 2471 2472 return 0; 2473 } 2474 2475 /* 2476 * Removes a given cdev from the dev list. The caller is responsible for 2477 * releasing the reference on the device associated with the linkage. 2478 * 2479 * Returns EALREADY if the dev has already been unlinked. 2480 */ 2481 static int 2482 devfs_unlink_dev(cdev_t dev) 2483 { 2484 if ((dev->si_flags & SI_DEVFS_LINKED)) { 2485 TAILQ_REMOVE(&devfs_dev_list, dev, link); 2486 dev->si_flags &= ~SI_DEVFS_LINKED; 2487 return (0); 2488 } 2489 return (EALREADY); 2490 } 2491 2492 int 2493 devfs_node_is_accessible(struct devfs_node *node) 2494 { 2495 if ((node) && (!(node->flags & DEVFS_HIDDEN))) 2496 return 1; 2497 else 2498 return 0; 2499 } 2500 2501 int 2502 devfs_reference_ops(struct dev_ops *ops) 2503 { 2504 int unit; 2505 struct devfs_dev_ops *found = NULL; 2506 struct devfs_dev_ops *devops; 2507 2508 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) { 2509 if (devops->ops == ops) { 2510 found = devops; 2511 break; 2512 } 2513 } 2514 2515 if (!found) { 2516 found = kmalloc(sizeof(struct devfs_dev_ops), M_DEVFS, M_WAITOK); 2517 found->ops = ops; 2518 found->ref_count = 0; 2519 TAILQ_INSERT_TAIL(&devfs_dev_ops_list, found, link); 2520 } 2521 2522 KKASSERT(found); 2523 2524 if (found->ref_count == 0) { 2525 found->id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255); 2526 if (found->id == -1) { 2527 /* Ran out of unique ids */ 2528 devfs_debug(DEVFS_DEBUG_WARNING, 2529 "devfs_reference_ops: WARNING: ran out of unique ids\n"); 2530 } 2531 } 2532 unit = found->id; 2533 ++found->ref_count; 2534 2535 return unit; 2536 } 2537 2538 void 2539 devfs_release_ops(struct dev_ops *ops) 2540 { 2541 struct devfs_dev_ops *found = NULL; 2542 struct devfs_dev_ops *devops; 2543 2544 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) { 2545 if (devops->ops == ops) { 2546 found = devops; 2547 break; 2548 } 2549 } 2550 2551 KKASSERT(found); 2552 2553 --found->ref_count; 2554 2555 if (found->ref_count == 0) { 2556 TAILQ_REMOVE(&devfs_dev_ops_list, found, link); 2557 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), found->id); 2558 kfree(found, M_DEVFS); 2559 } 2560 } 2561 2562 /* 2563 * Wait for asynchronous messages to complete in the devfs helper 2564 * thread, then return. Do nothing if the helper thread is dead 2565 * or we are being indirectly called from the helper thread itself. 2566 */ 2567 void 2568 devfs_config(void) 2569 { 2570 devfs_msg_t msg; 2571 2572 if (devfs_run && curthread != td_core) { 2573 msg = devfs_msg_get(); 2574 devfs_msg_send_sync(DEVFS_SYNC, msg); 2575 devfs_msg_put(msg); 2576 } 2577 } 2578 2579 /* 2580 * Called on init of devfs; creates the objcaches and 2581 * spawns off the devfs core thread. Also initializes 2582 * locks. 2583 */ 2584 static void 2585 devfs_init(void) 2586 { 2587 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n"); 2588 /* Create objcaches for nodes, msgs and devs */ 2589 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0, 2590 NULL, NULL, NULL, 2591 objcache_malloc_alloc, 2592 objcache_malloc_free, 2593 &devfs_node_malloc_args ); 2594 2595 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0, 2596 NULL, NULL, NULL, 2597 objcache_malloc_alloc, 2598 objcache_malloc_free, 2599 &devfs_msg_malloc_args ); 2600 2601 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0, 2602 NULL, NULL, NULL, 2603 objcache_malloc_alloc, 2604 objcache_malloc_free, 2605 &devfs_dev_malloc_args ); 2606 2607 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id)); 2608 2609 /* Initialize the reply-only port which acts as a message drain */ 2610 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply); 2611 2612 /* Initialize *THE* devfs lock */ 2613 lockinit(&devfs_lock, "devfs_core lock", 0, 0); 2614 2615 lockmgr(&devfs_lock, LK_EXCLUSIVE); 2616 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL, 2617 0, -1, "devfs_msg_core"); 2618 while (devfs_run == 0) 2619 lksleep(td_core, &devfs_lock, 0, "devfsc", 0); 2620 lockmgr(&devfs_lock, LK_RELEASE); 2621 2622 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n"); 2623 } 2624 2625 /* 2626 * Called on unload of devfs; takes care of destroying the core 2627 * and the objcaches. Also removes aliases that are no longer needed. 2628 */ 2629 static void 2630 devfs_uninit(void) 2631 { 2632 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n"); 2633 2634 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL); 2635 while (devfs_run) 2636 tsleep(td_core, 0, "devfsc", hz*10); 2637 tsleep(td_core, 0, "devfsc", hz); 2638 2639 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id)); 2640 2641 /* Destroy the objcaches */ 2642 objcache_destroy(devfs_msg_cache); 2643 objcache_destroy(devfs_node_cache); 2644 objcache_destroy(devfs_dev_cache); 2645 2646 devfs_alias_reap(); 2647 } 2648 2649 /* 2650 * This is a sysctl handler to assist userland devname(3) to 2651 * find the device name for a given udev. 2652 */ 2653 static int 2654 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS) 2655 { 2656 udev_t udev; 2657 cdev_t found; 2658 int error; 2659 2660 2661 if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t)))) 2662 return (error); 2663 2664 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev); 2665 2666 if (udev == NOUDEV) 2667 return(EINVAL); 2668 2669 if ((found = devfs_find_device_by_udev(udev)) == NULL) 2670 return(ENOENT); 2671 2672 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1)); 2673 } 2674 2675 2676 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY, 2677 NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)"); 2678 2679 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs"); 2680 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable); 2681 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable, 2682 0, "Enable DevFS debugging"); 2683 2684 SYSINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, 2685 devfs_init, NULL); 2686 SYSUNINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, 2687 devfs_uninit, NULL); 2688 2689 /* 2690 * WildCmp() - compare wild string to sane string 2691 * 2692 * Returns 0 on success, -1 on failure. 2693 */ 2694 static int 2695 wildCmp(const char **mary, int d, const char *w, const char *s) 2696 { 2697 int i; 2698 2699 /* 2700 * skip fixed portion 2701 */ 2702 for (;;) { 2703 switch(*w) { 2704 case '*': 2705 /* 2706 * optimize terminator 2707 */ 2708 if (w[1] == 0) 2709 return(0); 2710 if (w[1] != '?' && w[1] != '*') { 2711 /* 2712 * optimize * followed by non-wild 2713 */ 2714 for (i = 0; s + i < mary[d]; ++i) { 2715 if (s[i] == w[1] && wildCmp(mary, d + 1, w + 1, s + i) == 0) 2716 return(0); 2717 } 2718 } else { 2719 /* 2720 * less-optimal 2721 */ 2722 for (i = 0; s + i < mary[d]; ++i) { 2723 if (wildCmp(mary, d + 1, w + 1, s + i) == 0) 2724 return(0); 2725 } 2726 } 2727 mary[d] = s; 2728 return(-1); 2729 case '?': 2730 if (*s == 0) 2731 return(-1); 2732 ++w; 2733 ++s; 2734 break; 2735 default: 2736 if (*w != *s) 2737 return(-1); 2738 if (*w == 0) /* terminator */ 2739 return(0); 2740 ++w; 2741 ++s; 2742 break; 2743 } 2744 } 2745 /* not reached */ 2746 return(-1); 2747 } 2748 2749 2750 /* 2751 * WildCaseCmp() - compare wild string to sane string, case insensitive 2752 * 2753 * Returns 0 on success, -1 on failure. 2754 */ 2755 static int 2756 wildCaseCmp(const char **mary, int d, const char *w, const char *s) 2757 { 2758 int i; 2759 2760 /* 2761 * skip fixed portion 2762 */ 2763 for (;;) { 2764 switch(*w) { 2765 case '*': 2766 /* 2767 * optimize terminator 2768 */ 2769 if (w[1] == 0) 2770 return(0); 2771 if (w[1] != '?' && w[1] != '*') { 2772 /* 2773 * optimize * followed by non-wild 2774 */ 2775 for (i = 0; s + i < mary[d]; ++i) { 2776 if (s[i] == w[1] && wildCaseCmp(mary, d + 1, w + 1, s + i) == 0) 2777 return(0); 2778 } 2779 } else { 2780 /* 2781 * less-optimal 2782 */ 2783 for (i = 0; s + i < mary[d]; ++i) { 2784 if (wildCaseCmp(mary, d + 1, w + 1, s + i) == 0) 2785 return(0); 2786 } 2787 } 2788 mary[d] = s; 2789 return(-1); 2790 case '?': 2791 if (*s == 0) 2792 return(-1); 2793 ++w; 2794 ++s; 2795 break; 2796 default: 2797 if (*w != *s) { 2798 #define tolower(x) ((x >= 'A' && x <= 'Z')?(x+('a'-'A')):(x)) 2799 if (tolower(*w) != tolower(*s)) 2800 return(-1); 2801 } 2802 if (*w == 0) /* terminator */ 2803 return(0); 2804 ++w; 2805 ++s; 2806 break; 2807 } 2808 } 2809 /* not reached */ 2810 return(-1); 2811 } 2812 2813 struct cdev_privdata { 2814 void *cdpd_data; 2815 cdevpriv_dtr_t cdpd_dtr; 2816 }; 2817 2818 int 2819 devfs_get_cdevpriv(struct file *fp, void **datap) 2820 { 2821 int error; 2822 2823 if (fp == NULL) 2824 return(EBADF); 2825 2826 spin_lock_shared(&fp->f_spin); 2827 if (fp->f_data1 == NULL) { 2828 *datap = NULL; 2829 error = ENOENT; 2830 } else { 2831 struct cdev_privdata *p = fp->f_data1; 2832 2833 *datap = p->cdpd_data; 2834 error = 0; 2835 } 2836 spin_unlock_shared(&fp->f_spin); 2837 2838 return (error); 2839 } 2840 2841 int 2842 devfs_set_cdevpriv(struct file *fp, void *priv, cdevpriv_dtr_t dtr) 2843 { 2844 struct cdev_privdata *p; 2845 int error; 2846 2847 if (fp == NULL) 2848 return (ENOENT); 2849 2850 p = kmalloc(sizeof(struct cdev_privdata), M_DEVFS, M_WAITOK); 2851 p->cdpd_data = priv; 2852 p->cdpd_dtr = dtr; 2853 2854 spin_lock(&fp->f_spin); 2855 if (fp->f_data1 == NULL) { 2856 fp->f_data1 = p; 2857 error = 0; 2858 } else { 2859 error = EBUSY; 2860 } 2861 spin_unlock(&fp->f_spin); 2862 2863 if (error) 2864 kfree(p, M_DEVFS); 2865 2866 return error; 2867 } 2868 2869 void 2870 devfs_clear_cdevpriv(struct file *fp) 2871 { 2872 struct cdev_privdata *p; 2873 2874 if (fp == NULL) 2875 return; 2876 2877 spin_lock(&fp->f_spin); 2878 p = fp->f_data1; 2879 fp->f_data1 = NULL; 2880 spin_unlock(&fp->f_spin); 2881 2882 if (p != NULL) { 2883 p->cdpd_dtr(p->cdpd_data); 2884 kfree(p, M_DEVFS); 2885 } 2886 } 2887 2888 int 2889 devfs_WildCmp(const char *w, const char *s) 2890 { 2891 int i; 2892 int c; 2893 int slen = strlen(s); 2894 const char **mary; 2895 2896 for (i = c = 0; w[i]; ++i) { 2897 if (w[i] == '*') 2898 ++c; 2899 } 2900 mary = kmalloc(sizeof(char *) * (c + 1), M_DEVFS, M_WAITOK); 2901 for (i = 0; i < c; ++i) 2902 mary[i] = s + slen; 2903 i = wildCmp(mary, 0, w, s); 2904 kfree(mary, M_DEVFS); 2905 return(i); 2906 } 2907 2908 int 2909 devfs_WildCaseCmp(const char *w, const char *s) 2910 { 2911 int i; 2912 int c; 2913 int slen = strlen(s); 2914 const char **mary; 2915 2916 for (i = c = 0; w[i]; ++i) { 2917 if (w[i] == '*') 2918 ++c; 2919 } 2920 mary = kmalloc(sizeof(char *) * (c + 1), M_DEVFS, M_WAITOK); 2921 for (i = 0; i < c; ++i) 2922 mary[i] = s + slen; 2923 i = wildCaseCmp(mary, 0, w, s); 2924 kfree(mary, M_DEVFS); 2925 return(i); 2926 } 2927 2928