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