2 * Copyright (c) 2009-2019 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
40 #include <sys/vnode.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>
50 #include <sys/msgport2.h>
51 #include <sys/spinlock2.h>
52 #include <sys/sysref2.h>
54 MALLOC_DEFINE(M_DEVFS
, "devfs", "Device File System (devfs) allocations");
55 DEVFS_DEFINE_CLONE_BITMAP(ops_id
);
57 * SYSREF Integration - reference counting, allocation,
58 * sysid and syslink integration.
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
= {
66 .proto
= SYSREF_PROTO_DEV
,
67 .offset
= offsetof(struct cdev
, si_sysref
),
68 .objsize
= sizeof(struct cdev
),
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
78 static struct objcache
*devfs_node_cache
;
79 static struct objcache
*devfs_msg_cache
;
80 static struct objcache
*devfs_dev_cache
;
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
};
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
);
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
;
106 static struct spinlock ino_lock
;
108 static int devfs_debug_enable
;
109 static int devfs_run
;
111 static ino_t
devfs_fetch_ino(void);
112 static int devfs_reference_ops(struct dev_ops
*ops
);
113 static void devfs_release_ops(struct dev_ops
*ops
);
114 static int devfs_create_all_dev_worker(struct devfs_node
*);
115 static int devfs_create_dev_worker(cdev_t
, uid_t
, gid_t
, int);
116 static int devfs_destroy_dev_worker(cdev_t
);
117 static int devfs_destroy_related_worker(cdev_t
);
118 static int devfs_destroy_dev_by_ops_worker(struct dev_ops
*, int);
119 static int devfs_propagate_dev(cdev_t
, int);
120 static int devfs_unlink_dev(cdev_t dev
);
121 static void devfs_msg_exec(devfs_msg_t msg
);
123 static int devfs_chandler_add_worker(const char *, d_clone_t
*);
124 static int devfs_chandler_del_worker(const char *);
126 static void devfs_msg_autofree_reply(lwkt_port_t
, lwkt_msg_t
);
127 static void devfs_msg_core(void *);
129 static int devfs_find_device_by_name_worker(devfs_msg_t
);
130 static int devfs_find_device_by_devid_worker(devfs_msg_t
);
132 static int devfs_apply_reset_rules_caller(char *, int);
134 static int devfs_scan_callback_worker(devfs_scan_t
*, void *);
136 static struct devfs_node
*devfs_resolve_or_create_dir(struct devfs_node
*,
137 char *, size_t, int);
139 static int devfs_make_alias_worker(struct devfs_alias
*);
140 static int devfs_destroy_alias_worker(struct devfs_alias
*);
141 static int devfs_alias_remove(cdev_t
);
142 static int devfs_alias_reap(void);
143 static int devfs_alias_propagate(struct devfs_alias
*, int);
144 static int devfs_alias_apply(struct devfs_node
*, struct devfs_alias
*);
145 static int devfs_alias_check_create(struct devfs_node
*);
147 static int devfs_clr_related_flag_worker(cdev_t
, uint32_t);
148 static int devfs_destroy_related_without_flag_worker(cdev_t
, uint32_t);
150 static void *devfs_reaperp_callback(struct devfs_node
*, void *);
151 static void devfs_iterate_orphans_unmount(struct mount
*mp
);
152 static void *devfs_gc_dirs_callback(struct devfs_node
*, void *);
153 static void *devfs_gc_links_callback(struct devfs_node
*, struct devfs_node
*);
155 devfs_inode_to_vnode_worker_callback(struct devfs_node
*, ino_t
*);
158 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function
162 devfs_debug(int level
, char *fmt
, ...)
167 if (level
<= devfs_debug_enable
)
175 * devfs_allocp() Allocates a new devfs node with the specified
176 * parameters. The node is also automatically linked into the topology
177 * if a parent is specified. It also calls the rule and alias stuff to
178 * be applied on the new node
181 devfs_allocp(devfs_nodetype devfsnodetype
, char *name
,
182 struct devfs_node
*parent
, struct mount
*mp
, cdev_t dev
)
184 struct devfs_node
*node
= NULL
;
185 size_t namlen
= strlen(name
);
187 node
= objcache_get(devfs_node_cache
, M_WAITOK
);
188 bzero(node
, sizeof(*node
));
190 atomic_add_long(&DEVFS_MNTDATA(mp
)->leak_count
, 1);
195 node
->d_dir
.d_ino
= devfs_fetch_ino();
198 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries
201 node
->cookie_jar
= 2;
204 * Access Control members
206 node
->mode
= DEVFS_DEFAULT_MODE
;
207 node
->uid
= DEVFS_DEFAULT_UID
;
208 node
->gid
= DEVFS_DEFAULT_GID
;
210 switch (devfsnodetype
) {
213 * Ensure that we don't recycle the root vnode by marking it as
214 * linked into the topology.
216 node
->flags
|= DEVFS_NODE_LINKED
;
218 TAILQ_INIT(DEVFS_DENODE_HEAD(node
));
219 node
->d_dir
.d_type
= DT_DIR
;
224 node
->d_dir
.d_type
= DT_LNK
;
228 node
->d_dir
.d_type
= DT_REG
;
233 node
->d_dir
.d_type
= DT_CHR
;
236 node
->mode
= dev
->si_perms
;
237 node
->uid
= dev
->si_uid
;
238 node
->gid
= dev
->si_gid
;
240 devfs_alias_check_create(node
);
245 panic("devfs_allocp: unknown node type");
249 node
->node_type
= devfsnodetype
;
251 /* Initialize the dirent structure of each devfs vnode */
252 node
->d_dir
.d_namlen
= namlen
;
253 node
->d_dir
.d_name
= kmalloc(namlen
+1, M_DEVFS
, M_WAITOK
);
254 memcpy(node
->d_dir
.d_name
, name
, namlen
);
255 node
->d_dir
.d_name
[namlen
] = '\0';
257 /* Initialize the parent node element */
258 node
->parent
= parent
;
260 /* Initialize *time members */
261 vfs_timestamp(&node
->atime
);
262 node
->mtime
= node
->ctime
= node
->atime
;
265 * Associate with parent as last step, clean out namecache
269 if (parent
->node_type
== Nroot
||
270 parent
->node_type
== Ndir
) {
272 node
->cookie
= parent
->cookie_jar
++;
273 node
->flags
|= DEVFS_NODE_LINKED
;
274 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent
), node
, link
);
276 /* This forces negative namecache lookups to clear */
277 ++mp
->mnt_namecache_gen
;
279 kprintf("devfs: Cannot link node %p (%s) "
281 node
, node
->d_dir
.d_name
,
282 parent
, parent
->d_dir
.d_name
);
288 * Apply rules (requires root node, skip if we are creating the root
291 if (DEVFS_MNTDATA(mp
)->root_node
)
292 devfs_rule_check_apply(node
, NULL
);
294 atomic_add_long(&DEVFS_MNTDATA(mp
)->file_count
, 1);
300 * devfs_allocv() allocates a new vnode based on a devfs node.
303 devfs_allocv(struct vnode
**vpp
, struct devfs_node
*node
)
311 * devfs master lock must not be held across a vget() call, we have
312 * to hold our ad-hoc vp to avoid a free race from destroying the
313 * contents of the structure. The vget() will interlock recycles
317 while ((vp
= node
->v_node
) != NULL
) {
319 lockmgr(&devfs_lock
, LK_RELEASE
);
320 error
= vget(vp
, LK_EXCLUSIVE
);
322 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
327 if (error
!= ENOENT
) {
334 * devfs master lock must not be held across a getnewvnode() call.
336 lockmgr(&devfs_lock
, LK_RELEASE
);
337 if ((error
= getnewvnode(VT_DEVFS
, node
->mp
, vpp
, 0, 0)) != 0) {
338 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
341 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
345 if (node
->v_node
!= NULL
) {
354 switch (node
->node_type
) {
356 vsetflags(vp
, VROOT
);
372 KKASSERT(node
->d_dev
);
374 vp
->v_uminor
= node
->d_dev
->si_uminor
;
375 vp
->v_umajor
= node
->d_dev
->si_umajor
;
377 v_associate_rdev(vp
, node
->d_dev
);
378 vp
->v_ops
= &node
->mp
->mnt_vn_spec_ops
;
379 if (node
->d_dev
->si_ops
->head
.flags
& D_KVABIO
)
380 vsetflags(vp
, VKVABIO
);
384 panic("devfs_allocv: unknown node type");
386 vx_downgrade(vp
); /* downgrade VX lock to VN lock */
393 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
394 * based on the newly created devfs node.
397 devfs_allocvp(struct mount
*mp
, struct vnode
**vpp
, devfs_nodetype devfsnodetype
,
398 char *name
, struct devfs_node
*parent
, cdev_t dev
)
400 struct devfs_node
*node
;
402 node
= devfs_allocp(devfsnodetype
, name
, parent
, mp
, dev
);
405 devfs_allocv(vpp
, node
);
413 * Destroy the devfs_node. The node must be unlinked from the topology.
415 * This function will also destroy any vnode association with the node
418 * The cdev_t itself remains intact.
420 * The core lock is not necessarily held on call and must be temporarily
421 * released if it is to avoid a deadlock.
424 devfs_freep(struct devfs_node
*node
)
432 * It is possible for devfs_freep() to race a destruction due
433 * to having to release the lock below. We use DEVFS_DESTROYED
434 * to interlock the race (mediated by devfs_lock)
436 * We use NLINKSWAIT to indicate that the node couldn't be
437 * freed due to having pending nlinks. We can free
438 * the node when nlinks drops to 0. This should never print
439 * a "(null)" name, if it ever does there are still unresolved
442 if (node
->flags
& DEVFS_DESTROYED
) {
443 if ((node
->flags
& DEVFS_NLINKSWAIT
) &&
445 kprintf("devfs: final node '%s' on nlinks\n",
447 if (node
->d_dir
.d_name
) {
448 kfree(node
->d_dir
.d_name
, M_DEVFS
);
449 node
->d_dir
.d_name
= NULL
;
451 objcache_put(devfs_node_cache
, node
);
453 kprintf("devfs: race avoided node '%s' (%p)\n",
454 node
->d_dir
.d_name
, node
);
458 node
->flags
|= DEVFS_DESTROYED
;
461 * Items we have to dispose of before potentially releasing
464 * Remove the node from the orphan list if it is still on it.
466 atomic_subtract_long(&DEVFS_MNTDATA(node
->mp
)->leak_count
, 1);
467 atomic_subtract_long(&DEVFS_MNTDATA(node
->mp
)->file_count
, 1);
468 if (node
->flags
& DEVFS_ORPHANED
)
469 devfs_tracer_del_orphan(node
);
472 * At this point only the vp points to node, and node cannot be
473 * physically freed because we own DEVFS_DESTROYED.
475 * We must dispose of the vnode without deadlocking or racing
476 * against e.g. a vnode reclaim.
478 * This also prevents the vnode reclaim code from double-freeing
479 * the node. The vget() is required to safely modified the vp
480 * and cycle the refs to terminate an inactive vp.
483 while ((vp
= node
->v_node
) != NULL
) {
487 if (lockstatus(&devfs_lock
, curthread
) == LK_EXCLUSIVE
) {
488 lockmgr(&devfs_lock
, LK_RELEASE
);
493 if (node
->v_node
== NULL
) {
494 /* reclaim race, mediated by devfs_lock */
496 } else if (vget(vp
, LK_EXCLUSIVE
| LK_RETRY
) == 0) {
503 /* reclaim race, mediated by devfs_lock */
507 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
508 if (--maxloops
== 0) {
509 kprintf("devfs_freep: livelock on node %p\n", node
);
517 if (node
->symlink_name
) {
518 kfree(node
->symlink_name
, M_DEVFS
);
519 node
->symlink_name
= NULL
;
523 * We cannot actually free the node if it still has
527 node
->flags
|= DEVFS_NLINKSWAIT
;
529 if (node
->d_dir
.d_name
) {
530 kfree(node
->d_dir
.d_name
, M_DEVFS
);
531 node
->d_dir
.d_name
= NULL
;
533 objcache_put(devfs_node_cache
, node
);
538 * Returns a valid vp associated with the devfs alias node or NULL
540 static void *devfs_alias_getvp(struct devfs_node
*node
)
542 struct devfs_node
*found
= node
;
545 while ((found
->node_type
== Nlink
) && (found
->link_target
)) {
547 devfs_debug(DEVFS_DEBUG_SHOW
, "Recursive link or depth >= 8");
551 found
= found
->link_target
;
555 return found
->v_node
;
559 * Unlink the devfs node from the topology and add it to the orphan list.
560 * The node will later be destroyed by freep.
562 * Any vnode association, including the v_rdev and v_data, remains intact
566 devfs_unlinkp(struct devfs_node
*node
)
568 struct devfs_node
*parent
;
569 struct devfs_node
*target
;
574 * Add the node to the orphan list, so it is referenced somewhere, to
575 * so we don't leak it.
577 devfs_tracer_add_orphan(node
);
579 parent
= node
->parent
;
583 * If the parent is known we can unlink the node out of the topology
585 if (node
->flags
& DEVFS_NODE_LINKED
) {
587 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent
), node
, link
);
589 } else if (node
== DEVFS_MNTDATA(node
->mp
)->root_node
) {
590 DEVFS_MNTDATA(node
->mp
)->root_node
= NULL
;
592 node
->flags
&= ~DEVFS_NODE_LINKED
;
596 * Namecache invalidation.
598 * devfs alias nodes are special: their v_node entry is always null
599 * and they use the one from their link target. We thus use the
600 * target node's vp to invalidate both alias and target entries in
603 * Doing so for the target is not necessary but it would be more
604 * expensive to resolve only the namecache entry of the alias node
605 * from the information available in this function.
607 * WARNING! We do not disassociate the vnode here. That can only
608 * be safely done in devfs_freep().
610 if (node
->node_type
== Nlink
) {
611 if ((target
= node
->link_target
) != NULL
) {
612 vp
= devfs_alias_getvp(node
);
613 node
->link_target
= NULL
;
615 if (target
->nlinks
== 0 &&
616 (target
->flags
& DEVFS_DESTROYED
)) {
627 cache_inval_vp(vp
, CINV_DESTROY
);
631 devfs_iterate_topology(struct devfs_node
*node
,
632 devfs_iterate_callback_t
*callback
, void *arg1
)
634 struct devfs_node
*node1
, *node2
;
637 if (((node
->node_type
== Nroot
) || (node
->node_type
== Ndir
)) &&
638 node
->nchildren
> 2) {
639 TAILQ_FOREACH_MUTABLE(node1
, DEVFS_DENODE_HEAD(node
),
641 ret
= devfs_iterate_topology(node1
, callback
, arg1
);
646 ret
= callback(node
, arg1
);
652 devfs_alias_reaper_callback(struct devfs_node
*node
, void *unused
)
654 if (node
->node_type
== Nlink
) {
663 * devfs_reaperp() is a recursive function that iterates through all the
664 * topology, unlinking and freeing all devfs nodes.
667 devfs_reaperp_callback(struct devfs_node
*node
, void *unused
)
676 * Report any orphans that we couldn't delete. The mp and mnt_data
677 * are both disappearing, so we must also clean up the nodes a bit.
680 devfs_iterate_orphans_unmount(struct mount
*mp
)
682 struct devfs_orphan
*orphan
;
684 while ((orphan
= TAILQ_FIRST(DEVFS_ORPHANLIST(mp
))) != NULL
) {
685 devfs_freep(orphan
->node
);
691 devfs_gc_dirs_callback(struct devfs_node
*node
, void *unused
)
693 if (node
->node_type
== Ndir
) {
694 if ((node
->nchildren
== 2) &&
695 !(node
->flags
& DEVFS_USER_CREATED
)) {
705 devfs_gc_links_callback(struct devfs_node
*node
, struct devfs_node
*target
)
707 if ((node
->node_type
== Nlink
) && (node
->link_target
== target
)) {
716 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
717 * freeing a node, but also removes empty directories and links that link
718 * via devfs auto-link mechanism to the node being deleted.
721 devfs_gc(struct devfs_node
*node
)
723 struct devfs_node
*root_node
= DEVFS_MNTDATA(node
->mp
)->root_node
;
725 if (node
->nlinks
> 0)
726 devfs_iterate_topology(root_node
,
727 (devfs_iterate_callback_t
*)devfs_gc_links_callback
, node
);
730 devfs_iterate_topology(root_node
,
731 (devfs_iterate_callback_t
*)devfs_gc_dirs_callback
, NULL
);
739 * devfs_create_dev() is the asynchronous entry point for device creation.
740 * It just sends a message with the relevant details to the devfs core.
742 * This function will reference the passed device. The reference is owned
743 * by devfs and represents all of the device's node associations.
746 devfs_create_dev(cdev_t dev
, uid_t uid
, gid_t gid
, int perms
)
749 devfs_msg_send_dev(DEVFS_DEVICE_CREATE
, dev
, uid
, gid
, perms
);
755 * devfs_destroy_dev() is the asynchronous entry point for device destruction.
756 * It just sends a message with the relevant details to the devfs core.
759 devfs_destroy_dev(cdev_t dev
)
761 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY
, dev
, 0, 0, 0);
766 * devfs_mount_add() is the synchronous entry point for adding a new devfs
767 * mount. It sends a synchronous message with the relevant details to the
771 devfs_mount_add(struct devfs_mnt_data
*mnt
)
775 msg
= devfs_msg_get();
777 devfs_msg_send_sync(DEVFS_MOUNT_ADD
, msg
);
784 * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
785 * It sends a synchronous message with the relevant details to the devfs core.
788 devfs_mount_del(struct devfs_mnt_data
*mnt
)
792 msg
= devfs_msg_get();
794 devfs_msg_send_sync(DEVFS_MOUNT_DEL
, msg
);
801 * devfs_destroy_related() is the synchronous entry point for device
802 * destruction by subname. It just sends a message with the relevant details to
806 devfs_destroy_related(cdev_t dev
)
810 msg
= devfs_msg_get();
812 devfs_msg_send_sync(DEVFS_DESTROY_RELATED
, msg
);
818 devfs_clr_related_flag(cdev_t dev
, uint32_t flag
)
822 msg
= devfs_msg_get();
823 msg
->mdv_flags
.dev
= dev
;
824 msg
->mdv_flags
.flag
= flag
;
825 devfs_msg_send_sync(DEVFS_CLR_RELATED_FLAG
, msg
);
832 devfs_destroy_related_without_flag(cdev_t dev
, uint32_t flag
)
836 msg
= devfs_msg_get();
837 msg
->mdv_flags
.dev
= dev
;
838 msg
->mdv_flags
.flag
= flag
;
839 devfs_msg_send_sync(DEVFS_DESTROY_RELATED_WO_FLAG
, msg
);
846 * devfs_create_all_dev is the asynchronous entry point to trigger device
847 * node creation. It just sends a message with the relevant details to
851 devfs_create_all_dev(struct devfs_node
*root
)
853 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV
, root
);
858 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
859 * devices with a specific set of dev_ops and minor. It just sends a
860 * message with the relevant details to the devfs core.
863 devfs_destroy_dev_by_ops(struct dev_ops
*ops
, int minor
)
865 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS
, ops
, minor
);
870 * devfs_clone_handler_add is the synchronous entry point to add a new
871 * clone handler. It just sends a message with the relevant details to
875 devfs_clone_handler_add(const char *name
, d_clone_t
*nhandler
)
879 msg
= devfs_msg_get();
880 msg
->mdv_chandler
.name
= name
;
881 msg
->mdv_chandler
.nhandler
= nhandler
;
882 devfs_msg_send_sync(DEVFS_CHANDLER_ADD
, msg
);
888 * devfs_clone_handler_del is the synchronous entry point to remove a
889 * clone handler. It just sends a message with the relevant details to
893 devfs_clone_handler_del(const char *name
)
897 msg
= devfs_msg_get();
898 msg
->mdv_chandler
.name
= name
;
899 msg
->mdv_chandler
.nhandler
= NULL
;
900 devfs_msg_send_sync(DEVFS_CHANDLER_DEL
, msg
);
906 * devfs_find_device_by_name is the synchronous entry point to find a
907 * device given its name. It sends a synchronous message with the
908 * relevant details to the devfs core and returns the answer.
911 devfs_find_device_by_name(const char *fmt
, ...)
922 kvasnprintf(&target
, PATH_MAX
, fmt
, ap
);
925 msg
= devfs_msg_get();
926 msg
->mdv_name
= target
;
927 devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME
, msg
);
928 found
= msg
->mdv_cdev
;
936 * devfs_find_device_by_devid is the synchronous entry point to find a
937 * device given its udev number. It sends a synchronous message with
938 * the relevant details to the devfs core and returns the answer.
941 devfs_find_device_by_devid(dev_t udev
)
946 msg
= devfs_msg_get();
947 msg
->mdv_udev
= udev
;
948 devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_DEVID
, msg
);
949 found
= msg
->mdv_cdev
;
952 devfs_debug(DEVFS_DEBUG_DEBUG
,
953 "devfs_find_device_by_devid found? %s -end:3-\n",
954 ((found
) ? found
->si_name
:"NO"));
959 devfs_inode_to_vnode(struct mount
*mp
, ino_t target
)
961 struct vnode
*vp
= NULL
;
967 msg
= devfs_msg_get();
968 msg
->mdv_ino
.mp
= mp
;
969 msg
->mdv_ino
.ino
= target
;
970 devfs_msg_send_sync(DEVFS_INODE_TO_VNODE
, msg
);
971 vp
= msg
->mdv_ino
.vp
;
972 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
979 * devfs_make_alias is the asynchronous entry point to register an alias
980 * for a device. It just sends a message with the relevant details to the
984 devfs_make_alias(const char *name
, cdev_t dev_target
)
986 struct devfs_alias
*alias
;
991 alias
= kmalloc(sizeof(struct devfs_alias
), M_DEVFS
, M_WAITOK
);
992 alias
->name
= kstrdup(name
, M_DEVFS
);
994 alias
->dev_target
= dev_target
;
996 devfs_msg_send_generic(DEVFS_MAKE_ALIAS
, alias
);
1001 * devfs_destroy_alias is the asynchronous entry point to deregister an alias
1002 * for a device. It just sends a message with the relevant details to the
1006 devfs_destroy_alias(const char *name
, cdev_t dev_target
)
1008 struct devfs_alias
*alias
;
1013 alias
= kmalloc(sizeof(struct devfs_alias
), M_DEVFS
, M_WAITOK
);
1014 alias
->name
= kstrdup(name
, M_DEVFS
);
1015 alias
->namlen
= len
;
1016 alias
->dev_target
= dev_target
;
1018 devfs_msg_send_generic(DEVFS_DESTROY_ALIAS
, alias
);
1023 * devfs_apply_rules is the asynchronous entry point to trigger application
1024 * of all rules. It just sends a message with the relevant details to the
1028 devfs_apply_rules(char *mntto
)
1032 new_name
= kstrdup(mntto
, M_DEVFS
);
1033 devfs_msg_send_name(DEVFS_APPLY_RULES
, new_name
);
1039 * devfs_reset_rules is the asynchronous entry point to trigger reset of all
1040 * rules. It just sends a message with the relevant details to the devfs core.
1043 devfs_reset_rules(char *mntto
)
1047 new_name
= kstrdup(mntto
, M_DEVFS
);
1048 devfs_msg_send_name(DEVFS_RESET_RULES
, new_name
);
1055 * devfs_scan_callback is the asynchronous entry point to call a callback
1057 * It just sends a message with the relevant details to the devfs core.
1060 devfs_scan_callback(devfs_scan_t
*callback
, void *arg
)
1066 msg
= devfs_msg_get();
1067 msg
->mdv_load
= callback
;
1068 msg
->mdv_load2
= arg
;
1069 devfs_msg_send_sync(DEVFS_SCAN_CALLBACK
, msg
);
1077 * Acts as a message drain. Any message that is replied to here gets destroyed
1078 * and the memory freed.
1081 devfs_msg_autofree_reply(lwkt_port_t port
, lwkt_msg_t msg
)
1083 devfs_msg_put((devfs_msg_t
)msg
);
1087 * devfs_msg_get allocates a new devfs msg and returns it.
1092 return objcache_get(devfs_msg_cache
, M_WAITOK
);
1096 * devfs_msg_put deallocates a given devfs msg.
1099 devfs_msg_put(devfs_msg_t msg
)
1101 objcache_put(devfs_msg_cache
, msg
);
1106 * devfs_msg_send is the generic asynchronous message sending facility
1107 * for devfs. By default the reply port is the automatic disposal port.
1109 * If the current thread is the devfs_msg_port thread we execute the
1110 * operation synchronously.
1113 devfs_msg_send(uint32_t cmd
, devfs_msg_t devfs_msg
)
1115 lwkt_port_t port
= &devfs_msg_port
;
1117 lwkt_initmsg(&devfs_msg
->hdr
, &devfs_dispose_port
, 0);
1119 devfs_msg
->hdr
.u
.ms_result
= cmd
;
1121 if (port
->mpu_td
== curthread
) {
1122 devfs_msg_exec(devfs_msg
);
1123 lwkt_replymsg(&devfs_msg
->hdr
, 0);
1125 lwkt_sendmsg(port
, (lwkt_msg_t
)devfs_msg
);
1130 * devfs_msg_send_sync is the generic synchronous message sending
1131 * facility for devfs. It initializes a local reply port and waits
1132 * for the core's answer. The core will write the answer on the same
1133 * message which is sent back as reply. The caller still has a reference
1134 * to the message, so we don't need to return it.
1137 devfs_msg_send_sync(uint32_t cmd
, devfs_msg_t devfs_msg
)
1139 struct lwkt_port rep_port
;
1141 lwkt_port_t port
= &devfs_msg_port
;
1143 lwkt_initport_thread(&rep_port
, curthread
);
1144 lwkt_initmsg(&devfs_msg
->hdr
, &rep_port
, 0);
1146 devfs_msg
->hdr
.u
.ms_result
= cmd
;
1148 error
= lwkt_domsg(port
, (lwkt_msg_t
)devfs_msg
, 0);
1154 * sends a message with a generic argument.
1157 devfs_msg_send_generic(uint32_t cmd
, void *load
)
1159 devfs_msg_t devfs_msg
= devfs_msg_get();
1161 devfs_msg
->mdv_load
= load
;
1162 devfs_msg_send(cmd
, devfs_msg
);
1166 * sends a message with a name argument.
1169 devfs_msg_send_name(uint32_t cmd
, char *name
)
1171 devfs_msg_t devfs_msg
= devfs_msg_get();
1173 devfs_msg
->mdv_name
= name
;
1174 devfs_msg_send(cmd
, devfs_msg
);
1178 * sends a message with a mount argument.
1181 devfs_msg_send_mount(uint32_t cmd
, struct devfs_mnt_data
*mnt
)
1183 devfs_msg_t devfs_msg
= devfs_msg_get();
1185 devfs_msg
->mdv_mnt
= mnt
;
1186 devfs_msg_send(cmd
, devfs_msg
);
1190 * sends a message with an ops argument.
1193 devfs_msg_send_ops(uint32_t cmd
, struct dev_ops
*ops
, int minor
)
1195 devfs_msg_t devfs_msg
= devfs_msg_get();
1197 devfs_msg
->mdv_ops
.ops
= ops
;
1198 devfs_msg
->mdv_ops
.minor
= minor
;
1199 devfs_msg_send(cmd
, devfs_msg
);
1203 * sends a message with a clone handler argument.
1206 devfs_msg_send_chandler(uint32_t cmd
, char *name
, d_clone_t handler
)
1208 devfs_msg_t devfs_msg
= devfs_msg_get();
1210 devfs_msg
->mdv_chandler
.name
= name
;
1211 devfs_msg
->mdv_chandler
.nhandler
= handler
;
1212 devfs_msg_send(cmd
, devfs_msg
);
1216 * sends a message with a device argument.
1219 devfs_msg_send_dev(uint32_t cmd
, cdev_t dev
, uid_t uid
, gid_t gid
, int perms
)
1221 devfs_msg_t devfs_msg
= devfs_msg_get();
1223 devfs_msg
->mdv_dev
.dev
= dev
;
1224 devfs_msg
->mdv_dev
.uid
= uid
;
1225 devfs_msg
->mdv_dev
.gid
= gid
;
1226 devfs_msg
->mdv_dev
.perms
= perms
;
1228 devfs_msg_send(cmd
, devfs_msg
);
1232 * sends a message with a link argument.
1235 devfs_msg_send_link(uint32_t cmd
, char *name
, char *target
, struct mount
*mp
)
1237 devfs_msg_t devfs_msg
= devfs_msg_get();
1239 devfs_msg
->mdv_link
.name
= name
;
1240 devfs_msg
->mdv_link
.target
= target
;
1241 devfs_msg
->mdv_link
.mp
= mp
;
1242 devfs_msg_send(cmd
, devfs_msg
);
1246 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1247 * and calls the relevant worker functions. By using messages it's assured
1248 * that events occur in the correct order.
1251 devfs_msg_core(void *arg
)
1255 lwkt_initport_thread(&devfs_msg_port
, curthread
);
1257 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
1260 lockmgr(&devfs_lock
, LK_RELEASE
);
1262 lwkt_gettoken(&devfs_token
);
1265 msg
= (devfs_msg_t
)lwkt_waitport(&devfs_msg_port
, 0);
1266 devfs_debug(DEVFS_DEBUG_DEBUG
,
1267 "devfs_msg_core, new msg: %x\n",
1268 (unsigned int)msg
->hdr
.u
.ms_result
);
1269 devfs_msg_exec(msg
);
1270 lwkt_replymsg(&msg
->hdr
, 0);
1273 lwkt_reltoken(&devfs_token
);
1280 devfs_msg_exec(devfs_msg_t msg
)
1282 struct devfs_mnt_data
*mnt
;
1283 struct devfs_node
*node
;
1287 * Acquire the devfs lock to ensure safety of all called functions
1289 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
1291 switch (msg
->hdr
.u
.ms_result
) {
1292 case DEVFS_DEVICE_CREATE
:
1293 dev
= msg
->mdv_dev
.dev
;
1294 devfs_create_dev_worker(dev
,
1297 msg
->mdv_dev
.perms
);
1299 case DEVFS_DEVICE_DESTROY
:
1300 dev
= msg
->mdv_dev
.dev
;
1301 devfs_destroy_dev_worker(dev
);
1303 case DEVFS_DESTROY_RELATED
:
1304 devfs_destroy_related_worker(msg
->mdv_load
);
1306 case DEVFS_DESTROY_DEV_BY_OPS
:
1307 devfs_destroy_dev_by_ops_worker(msg
->mdv_ops
.ops
,
1308 msg
->mdv_ops
.minor
);
1310 case DEVFS_CREATE_ALL_DEV
:
1311 node
= (struct devfs_node
*)msg
->mdv_load
;
1312 devfs_create_all_dev_worker(node
);
1314 case DEVFS_MOUNT_ADD
:
1316 TAILQ_INSERT_TAIL(&devfs_mnt_list
, mnt
, link
);
1317 devfs_create_all_dev_worker(mnt
->root_node
);
1319 case DEVFS_MOUNT_DEL
:
1321 TAILQ_REMOVE(&devfs_mnt_list
, mnt
, link
);
1322 /* Be sure to remove all the aliases first */
1323 devfs_iterate_topology(mnt
->root_node
,
1324 devfs_alias_reaper_callback
,
1326 devfs_iterate_topology(mnt
->root_node
,
1327 devfs_reaperp_callback
,
1329 devfs_iterate_orphans_unmount(mnt
->mp
);
1330 if (mnt
->leak_count
) {
1331 devfs_debug(DEVFS_DEBUG_SHOW
,
1332 "Leaked %ld devfs_node elements!\n",
1336 case DEVFS_CHANDLER_ADD
:
1337 devfs_chandler_add_worker(msg
->mdv_chandler
.name
,
1338 msg
->mdv_chandler
.nhandler
);
1340 case DEVFS_CHANDLER_DEL
:
1341 devfs_chandler_del_worker(msg
->mdv_chandler
.name
);
1343 case DEVFS_FIND_DEVICE_BY_NAME
:
1344 devfs_find_device_by_name_worker(msg
);
1346 case DEVFS_FIND_DEVICE_BY_DEVID
:
1347 devfs_find_device_by_devid_worker(msg
);
1349 case DEVFS_MAKE_ALIAS
:
1350 devfs_make_alias_worker((struct devfs_alias
*)msg
->mdv_load
);
1352 case DEVFS_DESTROY_ALIAS
:
1353 devfs_destroy_alias_worker((struct devfs_alias
*)msg
->mdv_load
);
1355 case DEVFS_APPLY_RULES
:
1356 devfs_apply_reset_rules_caller(msg
->mdv_name
, 1);
1358 case DEVFS_RESET_RULES
:
1359 devfs_apply_reset_rules_caller(msg
->mdv_name
, 0);
1361 case DEVFS_SCAN_CALLBACK
:
1362 devfs_scan_callback_worker((devfs_scan_t
*)msg
->mdv_load
,
1365 case DEVFS_CLR_RELATED_FLAG
:
1366 devfs_clr_related_flag_worker(msg
->mdv_flags
.dev
,
1367 msg
->mdv_flags
.flag
);
1369 case DEVFS_DESTROY_RELATED_WO_FLAG
:
1370 devfs_destroy_related_without_flag_worker(msg
->mdv_flags
.dev
,
1371 msg
->mdv_flags
.flag
);
1373 case DEVFS_INODE_TO_VNODE
:
1374 msg
->mdv_ino
.vp
= devfs_iterate_topology(
1375 DEVFS_MNTDATA(msg
->mdv_ino
.mp
)->root_node
,
1376 (devfs_iterate_callback_t
*)devfs_inode_to_vnode_worker_callback
,
1379 case DEVFS_TERMINATE_CORE
:
1385 devfs_debug(DEVFS_DEBUG_WARNING
,
1386 "devfs_msg_core: unknown message "
1387 "received at core\n");
1390 lockmgr(&devfs_lock
, LK_RELEASE
);
1394 devfs_devctl_notify(cdev_t dev
, const char *ev
)
1396 static const char prefix
[] = "cdev=";
1400 namelen
= strlen(dev
->si_name
);
1401 data
= kmalloc(namelen
+ sizeof(prefix
), M_TEMP
, M_WAITOK
);
1402 memcpy(data
, prefix
, sizeof(prefix
) - 1);
1403 memcpy(data
+ sizeof(prefix
) - 1, dev
->si_name
, namelen
+ 1);
1404 devctl_notify("DEVFS", "CDEV", ev
, data
);
1405 kfree(data
, M_TEMP
);
1409 * Worker function to insert a new dev into the dev list and initialize its
1410 * permissions. It also calls devfs_propagate_dev which in turn propagates
1411 * the change to all mount points.
1413 * The passed dev is already referenced. This reference is eaten by this
1414 * function and represents the dev's linkage into devfs_dev_list.
1417 devfs_create_dev_worker(cdev_t dev
, uid_t uid
, gid_t gid
, int perms
)
1423 dev
->si_perms
= perms
;
1425 devfs_link_dev(dev
);
1426 devfs_propagate_dev(dev
, 1);
1428 udev_event_attach(dev
, NULL
, 0);
1429 devfs_devctl_notify(dev
, "CREATE");
1435 * Worker function to delete a dev from the dev list and free the cdev.
1436 * It also calls devfs_propagate_dev which in turn propagates the change
1437 * to all mount points.
1440 devfs_destroy_dev_worker(cdev_t dev
)
1445 KKASSERT((lockstatus(&devfs_lock
, curthread
)) == LK_EXCLUSIVE
);
1447 error
= devfs_unlink_dev(dev
);
1448 devfs_propagate_dev(dev
, 0);
1450 devfs_devctl_notify(dev
, "DESTROY");
1451 udev_event_detach(dev
, NULL
, 0);
1454 release_dev(dev
); /* link ref */
1462 * Worker function to destroy all devices with a certain basename.
1463 * Calls devfs_destroy_dev_worker for the actual destruction.
1466 devfs_destroy_related_worker(cdev_t needle
)
1471 devfs_debug(DEVFS_DEBUG_DEBUG
, "related worker: %s\n",
1473 TAILQ_FOREACH(dev
, &devfs_dev_list
, link
) {
1474 if (dev
->si_parent
== needle
) {
1475 devfs_destroy_related_worker(dev
);
1476 devfs_destroy_dev_worker(dev
);
1484 devfs_clr_related_flag_worker(cdev_t needle
, uint32_t flag
)
1488 TAILQ_FOREACH_MUTABLE(dev
, &devfs_dev_list
, link
, dev1
) {
1489 if (dev
->si_parent
== needle
) {
1490 devfs_clr_related_flag_worker(dev
, flag
);
1491 dev
->si_flags
&= ~flag
;
1499 devfs_destroy_related_without_flag_worker(cdev_t needle
, uint32_t flag
)
1504 devfs_debug(DEVFS_DEBUG_DEBUG
, "related_wo_flag: %s\n",
1507 TAILQ_FOREACH(dev
, &devfs_dev_list
, link
) {
1508 if (dev
->si_parent
== needle
) {
1509 devfs_destroy_related_without_flag_worker(dev
, flag
);
1510 if (!(dev
->si_flags
& flag
)) {
1511 devfs_destroy_dev_worker(dev
);
1512 devfs_debug(DEVFS_DEBUG_DEBUG
,
1513 "related_wo_flag: %s restart\n", dev
->si_name
);
1523 * Worker function that creates all device nodes on top of a devfs
1527 devfs_create_all_dev_worker(struct devfs_node
*root
)
1533 TAILQ_FOREACH(dev
, &devfs_dev_list
, link
) {
1534 devfs_create_device_node(root
, dev
, NULL
, NULL
, NULL
);
1541 * Worker function that destroys all devices that match a specific
1542 * dev_ops and/or minor. If minor is less than 0, it is not matched
1543 * against. It also propagates all changes.
1546 devfs_destroy_dev_by_ops_worker(struct dev_ops
*ops
, int minor
)
1552 TAILQ_FOREACH_MUTABLE(dev
, &devfs_dev_list
, link
, dev1
) {
1553 if (dev
->si_ops
!= ops
)
1555 if ((minor
< 0) || (dev
->si_uminor
== minor
)) {
1556 devfs_destroy_dev_worker(dev
);
1564 * Worker function that registers a new clone handler in devfs.
1567 devfs_chandler_add_worker(const char *name
, d_clone_t
*nhandler
)
1569 struct devfs_clone_handler
*chandler
= NULL
;
1570 u_char len
= strlen(name
);
1575 TAILQ_FOREACH(chandler
, &devfs_chandler_list
, link
) {
1576 if (chandler
->namlen
!= len
)
1579 if (!memcmp(chandler
->name
, name
, len
)) {
1580 /* Clonable basename already exists */
1585 chandler
= kmalloc(sizeof(*chandler
), M_DEVFS
, M_WAITOK
| M_ZERO
);
1586 chandler
->name
= kstrdup(name
, M_DEVFS
);
1587 chandler
->namlen
= len
;
1588 chandler
->nhandler
= nhandler
;
1590 TAILQ_INSERT_TAIL(&devfs_chandler_list
, chandler
, link
);
1595 * Worker function that removes a given clone handler from the
1596 * clone handler list.
1599 devfs_chandler_del_worker(const char *name
)
1601 struct devfs_clone_handler
*chandler
, *chandler2
;
1602 u_char len
= strlen(name
);
1607 TAILQ_FOREACH_MUTABLE(chandler
, &devfs_chandler_list
, link
, chandler2
) {
1608 if (chandler
->namlen
!= len
)
1610 if (memcmp(chandler
->name
, name
, len
))
1613 TAILQ_REMOVE(&devfs_chandler_list
, chandler
, link
);
1614 kfree(chandler
->name
, M_DEVFS
);
1615 kfree(chandler
, M_DEVFS
);
1623 * Worker function that finds a given device name and changes
1624 * the message received accordingly so that when replied to,
1625 * the answer is returned to the caller.
1628 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg
)
1630 struct devfs_alias
*alias
;
1632 cdev_t found
= NULL
;
1634 TAILQ_FOREACH(dev
, &devfs_dev_list
, link
) {
1635 if (strcmp(devfs_msg
->mdv_name
, dev
->si_name
) == 0) {
1640 if (found
== NULL
) {
1641 TAILQ_FOREACH(alias
, &devfs_alias_list
, link
) {
1642 if (strcmp(devfs_msg
->mdv_name
, alias
->name
) == 0) {
1643 found
= alias
->dev_target
;
1648 devfs_msg
->mdv_cdev
= found
;
1654 * Worker function that finds a given device udev and changes
1655 * the message received accordingly so that when replied to,
1656 * the answer is returned to the caller.
1659 devfs_find_device_by_devid_worker(devfs_msg_t devfs_msg
)
1662 cdev_t found
= NULL
;
1664 TAILQ_FOREACH_MUTABLE(dev
, &devfs_dev_list
, link
, dev1
) {
1665 if (((dev_t
)dev
->si_inode
) == devfs_msg
->mdv_udev
) {
1670 devfs_msg
->mdv_cdev
= found
;
1676 * Worker function that inserts a given alias into the
1677 * alias list, and propagates the alias to all mount
1681 devfs_make_alias_worker(struct devfs_alias
*alias
)
1683 struct devfs_alias
*alias2
;
1684 size_t len
= strlen(alias
->name
);
1687 TAILQ_FOREACH(alias2
, &devfs_alias_list
, link
) {
1688 if (len
!= alias2
->namlen
)
1691 if (!memcmp(alias
->name
, alias2
->name
, len
)) {
1699 * The alias doesn't exist yet, so we add it to the alias list
1701 TAILQ_INSERT_TAIL(&devfs_alias_list
, alias
, link
);
1702 devfs_alias_propagate(alias
, 0);
1703 udev_event_attach(alias
->dev_target
, alias
->name
, 1);
1705 devfs_debug(DEVFS_DEBUG_WARNING
,
1706 "Warning: duplicate devfs_make_alias for %s\n",
1708 kfree(alias
->name
, M_DEVFS
);
1709 kfree(alias
, M_DEVFS
);
1716 * Worker function that delete a given alias from the
1717 * alias list, and propagates the removal to all mount
1721 devfs_destroy_alias_worker(struct devfs_alias
*alias
)
1723 struct devfs_alias
*alias2
;
1726 TAILQ_FOREACH(alias2
, &devfs_alias_list
, link
) {
1727 if (alias
->dev_target
!= alias2
->dev_target
)
1730 if (devfs_WildCmp(alias
->name
, alias2
->name
) == 0) {
1737 devfs_debug(DEVFS_DEBUG_WARNING
,
1738 "Warning: devfs_destroy_alias for inexistant alias: %s\n",
1740 kfree(alias
->name
, M_DEVFS
);
1741 kfree(alias
, M_DEVFS
);
1744 * The alias exists, so we delete it from the alias list
1746 TAILQ_REMOVE(&devfs_alias_list
, alias2
, link
);
1747 devfs_alias_propagate(alias2
, 1);
1748 udev_event_detach(alias2
->dev_target
, alias2
->name
, 1);
1749 kfree(alias
->name
, M_DEVFS
);
1750 kfree(alias
, M_DEVFS
);
1751 kfree(alias2
->name
, M_DEVFS
);
1752 kfree(alias2
, M_DEVFS
);
1759 * Function that removes and frees all aliases.
1762 devfs_alias_reap(void)
1764 struct devfs_alias
*alias
, *alias2
;
1766 TAILQ_FOREACH_MUTABLE(alias
, &devfs_alias_list
, link
, alias2
) {
1767 TAILQ_REMOVE(&devfs_alias_list
, alias
, link
);
1768 kfree(alias
->name
, M_DEVFS
);
1769 kfree(alias
, M_DEVFS
);
1775 * Function that removes an alias matching a specific cdev and frees
1779 devfs_alias_remove(cdev_t dev
)
1781 struct devfs_alias
*alias
, *alias2
;
1783 TAILQ_FOREACH_MUTABLE(alias
, &devfs_alias_list
, link
, alias2
) {
1784 if (alias
->dev_target
== dev
) {
1785 TAILQ_REMOVE(&devfs_alias_list
, alias
, link
);
1786 udev_event_detach(alias
->dev_target
, alias
->name
, 1);
1787 kfree(alias
->name
, M_DEVFS
);
1788 kfree(alias
, M_DEVFS
);
1795 * This function propagates an alias addition or removal to
1799 devfs_alias_propagate(struct devfs_alias
*alias
, int remove
)
1801 struct devfs_mnt_data
*mnt
;
1803 TAILQ_FOREACH(mnt
, &devfs_mnt_list
, link
) {
1805 devfs_destroy_node(mnt
->root_node
, alias
->name
);
1807 devfs_alias_apply(mnt
->root_node
, alias
);
1814 * This function is a recursive function iterating through
1815 * all device nodes in the topology and, if applicable,
1816 * creating the relevant alias for a device node.
1819 devfs_alias_apply(struct devfs_node
*node
, struct devfs_alias
*alias
)
1821 struct devfs_node
*node1
, *node2
;
1823 KKASSERT(alias
!= NULL
);
1825 if ((node
->node_type
== Nroot
) || (node
->node_type
== Ndir
)) {
1826 if (node
->nchildren
> 2) {
1827 TAILQ_FOREACH_MUTABLE(node1
, DEVFS_DENODE_HEAD(node
), link
, node2
) {
1828 devfs_alias_apply(node1
, alias
);
1832 if (node
->d_dev
== alias
->dev_target
)
1833 devfs_alias_create(alias
->name
, node
, 0);
1839 * This function checks if any alias possibly is applicable
1840 * to the given node. If so, the alias is created.
1843 devfs_alias_check_create(struct devfs_node
*node
)
1845 struct devfs_alias
*alias
;
1847 TAILQ_FOREACH(alias
, &devfs_alias_list
, link
) {
1848 if (node
->d_dev
== alias
->dev_target
)
1849 devfs_alias_create(alias
->name
, node
, 0);
1855 * This function creates an alias with a given name
1856 * linking to a given devfs node. It also increments
1857 * the link count on the target node.
1860 devfs_alias_create(char *name_orig
, struct devfs_node
*target
, int rule_based
)
1862 struct mount
*mp
= target
->mp
;
1863 struct devfs_node
*parent
= DEVFS_MNTDATA(mp
)->root_node
;
1864 struct devfs_node
*linknode
;
1865 char *create_path
= NULL
;
1870 KKASSERT((lockstatus(&devfs_lock
, curthread
)) == LK_EXCLUSIVE
);
1872 name_buf
= kmalloc(PATH_MAX
, M_TEMP
, M_WAITOK
);
1873 devfs_resolve_name_path(name_orig
, name_buf
, &create_path
, &name
);
1876 parent
= devfs_resolve_or_create_path(parent
, create_path
, 1);
1879 if (devfs_find_device_node_by_name(parent
, name
)) {
1880 devfs_debug(DEVFS_DEBUG_WARNING
,
1881 "Node already exists: %s "
1882 "(devfs_make_alias_worker)!\n",
1888 linknode
= devfs_allocp(Nlink
, name
, parent
, mp
, NULL
);
1889 if (linknode
== NULL
) {
1894 linknode
->link_target
= target
;
1898 linknode
->flags
|= DEVFS_RULE_CREATED
;
1901 kfree(name_buf
, M_TEMP
);
1906 * This function is called by the core and handles mount point
1907 * strings. It either calls the relevant worker (devfs_apply_
1908 * reset_rules_worker) on all mountpoints or only a specific
1912 devfs_apply_reset_rules_caller(char *mountto
, int apply
)
1914 struct devfs_mnt_data
*mnt
;
1916 if (mountto
[0] == '*') {
1917 TAILQ_FOREACH(mnt
, &devfs_mnt_list
, link
) {
1918 devfs_iterate_topology(mnt
->root_node
,
1919 (apply
)?(devfs_rule_check_apply
):(devfs_rule_reset_node
),
1923 TAILQ_FOREACH(mnt
, &devfs_mnt_list
, link
) {
1924 if (!strcmp(mnt
->mp
->mnt_stat
.f_mntonname
, mountto
)) {
1925 devfs_iterate_topology(mnt
->root_node
,
1926 (apply
)?(devfs_rule_check_apply
):(devfs_rule_reset_node
),
1933 kfree(mountto
, M_DEVFS
);
1938 * This function calls a given callback function for
1939 * every dev node in the devfs dev list.
1942 devfs_scan_callback_worker(devfs_scan_t
*callback
, void *arg
)
1945 struct devfs_alias
*alias
, *alias1
;
1947 TAILQ_FOREACH_MUTABLE(dev
, &devfs_dev_list
, link
, dev1
) {
1948 callback(dev
->si_name
, dev
, false, arg
);
1950 TAILQ_FOREACH_MUTABLE(alias
, &devfs_alias_list
, link
, alias1
) {
1951 callback(alias
->name
, alias
->dev_target
, true, arg
);
1958 * This function tries to resolve a given directory, or if not
1959 * found and creation requested, creates the given directory.
1961 static struct devfs_node
*
1962 devfs_resolve_or_create_dir(struct devfs_node
*parent
, char *dir_name
,
1963 size_t name_len
, int create
)
1965 struct devfs_node
*node
, *found
= NULL
;
1967 TAILQ_FOREACH(node
, DEVFS_DENODE_HEAD(parent
), link
) {
1968 if (name_len
!= node
->d_dir
.d_namlen
)
1971 if (!memcmp(dir_name
, node
->d_dir
.d_name
, name_len
)) {
1977 if ((found
== NULL
) && (create
)) {
1978 found
= devfs_allocp(Ndir
, dir_name
, parent
, parent
->mp
, NULL
);
1985 * This function tries to resolve a complete path. If creation is requested,
1986 * if a given part of the path cannot be resolved (because it doesn't exist),
1990 devfs_resolve_or_create_path(struct devfs_node
*parent
, char *path
, int create
)
1992 struct devfs_node
*node
= parent
;
1999 buf
= kmalloc(PATH_MAX
, M_TEMP
, M_WAITOK
);
2001 while (*path
&& idx
< PATH_MAX
- 1) {
2006 node
= devfs_resolve_or_create_dir(node
, buf
, idx
, create
);
2016 node
= devfs_resolve_or_create_dir(node
, buf
, idx
, create
);
2017 kfree (buf
, M_TEMP
);
2022 * Takes a full path and strips it into a directory path and a name.
2023 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
2024 * requires a working buffer with enough size to keep the whole
2028 devfs_resolve_name_path(char *fullpath
, char *buf
, char **pathp
, char **namep
)
2032 size_t len
= strlen(fullpath
) + 1;
2035 KKASSERT((fullpath
!= NULL
) && (buf
!= NULL
));
2036 KKASSERT((pathp
!= NULL
) && (namep
!= NULL
));
2038 memcpy(buf
, fullpath
, len
);
2040 for (i
= len
-1; i
>= 0; i
--) {
2041 if (buf
[i
] == '/') {
2061 * This function creates a new devfs node for a given device. It can
2062 * handle a complete path as device name, and accordingly creates
2063 * the path and the final device node.
2065 * The reference count on the passed dev remains unchanged.
2068 devfs_create_device_node(struct devfs_node
*root
, cdev_t dev
,
2069 int *existsp
, char *dev_name
, char *path_fmt
, ...)
2071 struct devfs_node
*parent
, *node
= NULL
;
2077 char *create_path
= NULL
;
2078 char *names
= "pqrsPQRS";
2080 name_buf
= kmalloc(PATH_MAX
, M_TEMP
, M_WAITOK
);
2085 if (path_fmt
!= NULL
) {
2086 __va_start(ap
, path_fmt
);
2087 kvasnprintf(&path
, PATH_MAX
, path_fmt
, ap
);
2091 parent
= devfs_resolve_or_create_path(root
, path
, 1);
2094 devfs_resolve_name_path(
2095 ((dev_name
== NULL
) && (dev
))?(dev
->si_name
):(dev_name
),
2096 name_buf
, &create_path
, &name
);
2099 parent
= devfs_resolve_or_create_path(parent
, create_path
, 1);
2102 node
= devfs_find_device_node_by_name(parent
, name
);
2104 if (node
->d_dev
== dev
) {
2106 * Allow case where device caches dev after the
2107 * close and might desire to reuse it.
2112 devfs_debug(DEVFS_DEBUG_WARNING
,
2113 "devfs_create_device_node: "
2114 "DEVICE %s ALREADY EXISTS!!! "
2115 "Ignoring creation request.\n",
2122 node
= devfs_allocp(Ndev
, name
, parent
, parent
->mp
, dev
);
2123 vfs_timestamp(&parent
->mtime
);
2126 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
2129 if ((dev
) && (strlen(dev
->si_name
) >= 4) &&
2130 (!memcmp(dev
->si_name
, "ptm/", 4))) {
2131 node
->parent
->flags
|= DEVFS_HIDDEN
;
2132 node
->flags
|= DEVFS_HIDDEN
;
2136 * Ugly pty magic, to tag pty devices as such and hide them if needed.
2138 if ((strlen(name
) >= 3) && (!memcmp(name
, "pty", 3)))
2139 node
->flags
|= (DEVFS_PTY
| DEVFS_INVISIBLE
);
2141 if ((strlen(name
) >= 3) && (!memcmp(name
, "tty", 3))) {
2143 for (i
= 0; i
< strlen(names
); i
++) {
2144 if (name
[3] == names
[i
]) {
2150 node
->flags
|= (DEVFS_PTY
| DEVFS_INVISIBLE
);
2154 kfree(name_buf
, M_TEMP
);
2160 * This function finds a given device node in the topology with a given
2164 devfs_find_device_node_callback(struct devfs_node
*node
, cdev_t target
)
2166 if ((node
->node_type
== Ndev
) && (node
->d_dev
== target
)) {
2174 * This function finds a device node in the given parent directory by its
2175 * name and returns it.
2178 devfs_find_device_node_by_name(struct devfs_node
*parent
, char *target
)
2180 struct devfs_node
*node
, *found
= NULL
;
2181 size_t len
= strlen(target
);
2183 TAILQ_FOREACH(node
, DEVFS_DENODE_HEAD(parent
), link
) {
2184 if (len
!= node
->d_dir
.d_namlen
)
2187 if (!memcmp(node
->d_dir
.d_name
, target
, len
)) {
2197 devfs_inode_to_vnode_worker_callback(struct devfs_node
*node
, ino_t
*inop
)
2199 struct vnode
*vp
= NULL
;
2200 ino_t target
= *inop
;
2202 if (node
->d_dir
.d_ino
== target
) {
2205 vget(vp
, LK_EXCLUSIVE
| LK_RETRY
);
2208 devfs_allocv(&vp
, node
);
2217 * This function takes a cdev and removes its devfs node in the
2218 * given topology. The cdev remains intact.
2221 devfs_destroy_device_node(struct devfs_node
*root
, cdev_t target
)
2223 KKASSERT(target
!= NULL
);
2224 return devfs_destroy_node(root
, target
->si_name
);
2228 * This function takes a path to a devfs node, resolves it and
2229 * removes the devfs node from the given topology.
2232 devfs_destroy_node(struct devfs_node
*root
, char *target
)
2234 struct devfs_node
*node
, *parent
;
2237 char *create_path
= NULL
;
2241 name_buf
= kmalloc(PATH_MAX
, M_TEMP
, M_WAITOK
);
2242 ksnprintf(name_buf
, PATH_MAX
, "%s", target
);
2244 devfs_resolve_name_path(target
, name_buf
, &create_path
, &name
);
2247 parent
= devfs_resolve_or_create_path(root
, create_path
, 0);
2251 if (parent
== NULL
) {
2252 kfree(name_buf
, M_TEMP
);
2256 node
= devfs_find_device_node_by_name(parent
, name
);
2259 vfs_timestamp(&node
->parent
->mtime
);
2263 kfree(name_buf
, M_TEMP
);
2269 * Just set perms and ownership for given node.
2272 devfs_set_perms(struct devfs_node
*node
, uid_t uid
, gid_t gid
,
2273 u_short mode
, u_long flags
)
2283 * Propagates a device attach/detach to all mount
2284 * points. Also takes care of automatic alias removal
2285 * for a deleted cdev.
2288 devfs_propagate_dev(cdev_t dev
, int attach
)
2290 struct devfs_mnt_data
*mnt
;
2292 TAILQ_FOREACH(mnt
, &devfs_mnt_list
, link
) {
2294 /* Device is being attached */
2295 devfs_create_device_node(mnt
->root_node
, dev
,
2298 /* Device is being detached */
2299 devfs_alias_remove(dev
);
2300 devfs_destroy_device_node(mnt
->root_node
, dev
);
2307 * devfs_clone either returns a basename from a complete name by
2308 * returning the length of the name without trailing digits, or,
2309 * if clone != 0, calls the device's clone handler to get a new
2310 * device, which in turn is returned in devp.
2312 * Caller must hold a shared devfs_lock
2315 devfs_clone(cdev_t dev
, const char *name
, size_t len
, int mode
,
2319 struct devfs_clone_handler
*chandler
;
2320 struct dev_clone_args ap
;
2322 TAILQ_FOREACH(chandler
, &devfs_chandler_list
, link
) {
2323 if (chandler
->namlen
!= len
)
2325 if ((!memcmp(chandler
->name
, name
, len
)) &&
2326 (chandler
->nhandler
)) {
2328 * We have to unlock across the config and the
2329 * callback to avoid deadlocking. The device is
2330 * likely to obtain its own lock in the callback
2331 * and might then call into devfs.
2333 lockmgr(&devfs_lock
, LK_RELEASE
);
2335 ap
.a_head
.a_dev
= dev
;
2341 error
= (chandler
->nhandler
)(&ap
);
2342 lockmgr(&devfs_lock
, LK_SHARED
);
2355 * Registers a new orphan in the orphan list.
2358 devfs_tracer_add_orphan(struct devfs_node
*node
)
2360 struct devfs_orphan
*orphan
;
2363 orphan
= kmalloc(sizeof(struct devfs_orphan
), M_DEVFS
, M_WAITOK
);
2364 orphan
->node
= node
;
2366 KKASSERT((node
->flags
& DEVFS_ORPHANED
) == 0);
2367 node
->flags
|= DEVFS_ORPHANED
;
2368 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node
->mp
), orphan
, link
);
2372 * Removes an orphan from the orphan list.
2375 devfs_tracer_del_orphan(struct devfs_node
*node
)
2377 struct devfs_orphan
*orphan
;
2381 TAILQ_FOREACH(orphan
, DEVFS_ORPHANLIST(node
->mp
), link
) {
2382 if (orphan
->node
== node
) {
2383 node
->flags
&= ~DEVFS_ORPHANED
;
2384 TAILQ_REMOVE(DEVFS_ORPHANLIST(node
->mp
), orphan
, link
);
2385 kfree(orphan
, M_DEVFS
);
2392 * Counts the orphans in the orphan list, and if cleanup
2393 * is specified, also frees the orphan and removes it from
2397 devfs_tracer_orphan_count(struct mount
*mp
, int cleanup
)
2399 struct devfs_orphan
*orphan
, *orphan2
;
2402 TAILQ_FOREACH_MUTABLE(orphan
, DEVFS_ORPHANLIST(mp
), link
, orphan2
) {
2405 * If we are instructed to clean up, we do so.
2408 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp
), orphan
, link
);
2409 orphan
->node
->flags
&= ~DEVFS_ORPHANED
;
2410 devfs_freep(orphan
->node
);
2411 kfree(orphan
, M_DEVFS
);
2419 * Fetch an ino_t from the global d_ino by increasing it
2423 devfs_fetch_ino(void)
2427 spin_lock(&ino_lock
);
2429 spin_unlock(&ino_lock
);
2435 * Allocates a new cdev and initializes it's most basic
2439 devfs_new_cdev(struct dev_ops
*ops
, int minor
, struct dev_ops
*bops
)
2441 cdev_t dev
= sysref_alloc(&cdev_sysref_class
);
2443 sysref_activate(&dev
->si_sysref
);
2445 bzero(dev
, offsetof(struct cdev
, si_sysref
));
2447 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
2452 dev
->si_drv1
= NULL
;
2453 dev
->si_drv2
= NULL
;
2454 dev
->si_lastread
= 0; /* time_uptime */
2455 dev
->si_lastwrite
= 0; /* time_uptime */
2457 dev
->si_dict
= NULL
;
2458 dev
->si_parent
= NULL
;
2461 dev
->si_uminor
= minor
;
2462 dev
->si_bops
= bops
;
2465 * Since the disk subsystem is in the way, we need to
2466 * propagate the D_CANFREE from bops (and ops) to
2469 if (bops
&& (bops
->head
.flags
& D_CANFREE
)) {
2470 dev
->si_flags
|= SI_CANFREE
;
2471 } else if (ops
->head
.flags
& D_CANFREE
) {
2472 dev
->si_flags
|= SI_CANFREE
;
2475 /* If there is a backing device, we reference its ops */
2478 dev
->si_inode
= makeudev(devfs_reference_ops(bops
), minor
);
2479 dev
->si_umajor
= umajor(dev
->si_inode
);
2481 lockmgr(&devfs_lock
, LK_RELEASE
);
2487 devfs_cdev_terminate(cdev_t dev
)
2490 * Make sure the node isn't linked anymore. Otherwise we've screwed
2491 * up somewhere, since normal devs are unlinked on the call to
2492 * destroy_dev and only-cdevs that have not been used for cloning
2493 * are not linked in the first place. only-cdevs used for cloning
2494 * will be linked in, too, and should only be destroyed via
2495 * destroy_dev, not destroy_only_dev, so we catch that problem, too.
2497 KKASSERT((dev
->si_flags
& SI_DEVFS_LINKED
) == 0);
2499 /* If there is a backing device, we release the backing device's ops */
2500 devfs_release_ops((dev
->si_bops
)?(dev
->si_bops
):(dev
->si_ops
));
2502 /* devfs_cdev_unlock() is not called, unlock ourselves */
2503 lockmgr(&devfs_lock
, LK_RELEASE
);
2505 /* Finally destroy the device */
2506 sysref_put(&dev
->si_sysref
);
2510 * Dummies for now (individual locks for MPSAFE)
2513 devfs_cdev_lock(cdev_t dev
)
2515 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
2519 devfs_cdev_unlock(cdev_t dev
)
2521 lockmgr(&devfs_lock
, LK_RELEASE
);
2525 devfs_detached_filter_eof(struct knote
*kn
, long hint
)
2527 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
);
2532 devfs_detached_filter_detach(struct knote
*kn
)
2534 cdev_t dev
= (cdev_t
)kn
->kn_hook
;
2536 knote_remove(&dev
->si_kqinfo
.ki_note
, kn
);
2539 static struct filterops devfs_detached_filterops
=
2540 { FILTEROP_ISFD
, NULL
,
2541 devfs_detached_filter_detach
,
2542 devfs_detached_filter_eof
};
2545 * Delegates knote filter handling responsibility to devfs
2547 * Any device that implements kqfilter event handling and could be detached
2548 * or shut down out from under the kevent subsystem must allow devfs to
2549 * assume responsibility for any knotes it may hold.
2552 devfs_assume_knotes(cdev_t dev
, struct kqinfo
*kqi
)
2555 * Let kern/kern_event.c do the heavy lifting.
2557 knote_assume_knotes(kqi
, &dev
->si_kqinfo
,
2558 &devfs_detached_filterops
, (void *)dev
);
2561 * These should probably be activated individually, but doing so
2562 * would require refactoring kq's public in-kernel interface.
2564 KNOTE(&dev
->si_kqinfo
.ki_note
, 0);
2568 * Links a given cdev into the dev list.
2571 devfs_link_dev(cdev_t dev
)
2573 KKASSERT((dev
->si_flags
& SI_DEVFS_LINKED
) == 0);
2574 dev
->si_flags
|= SI_DEVFS_LINKED
;
2575 TAILQ_INSERT_TAIL(&devfs_dev_list
, dev
, link
);
2581 * Removes a given cdev from the dev list. The caller is responsible for
2582 * releasing the reference on the device associated with the linkage.
2584 * Returns EALREADY if the dev has already been unlinked.
2587 devfs_unlink_dev(cdev_t dev
)
2589 if ((dev
->si_flags
& SI_DEVFS_LINKED
)) {
2590 TAILQ_REMOVE(&devfs_dev_list
, dev
, link
);
2591 dev
->si_flags
&= ~SI_DEVFS_LINKED
;
2598 devfs_node_is_accessible(struct devfs_node
*node
)
2600 if ((node
) && (!(node
->flags
& DEVFS_HIDDEN
)))
2607 * devfs must be locked
2610 devfs_reference_ops(struct dev_ops
*ops
)
2613 struct devfs_dev_ops
*found
= NULL
;
2614 struct devfs_dev_ops
*devops
;
2616 TAILQ_FOREACH(devops
, &devfs_dev_ops_list
, link
) {
2617 if (devops
->ops
== ops
) {
2624 found
= kmalloc(sizeof(struct devfs_dev_ops
),
2627 found
->ref_count
= 0;
2628 TAILQ_INSERT_TAIL(&devfs_dev_ops_list
, found
, link
);
2633 if (found
->ref_count
== 0) {
2635 devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id
), 255);
2636 if (found
->id
== -1) {
2637 /* Ran out of unique ids */
2638 devfs_debug(DEVFS_DEBUG_WARNING
,
2639 "devfs_reference_ops: WARNING: ran "
2640 "out of unique ids\n");
2650 * devfs must be locked
2653 devfs_release_ops(struct dev_ops
*ops
)
2655 struct devfs_dev_ops
*found
= NULL
;
2656 struct devfs_dev_ops
*devops
;
2658 TAILQ_FOREACH(devops
, &devfs_dev_ops_list
, link
) {
2659 if (devops
->ops
== ops
) {
2669 if (found
->ref_count
== 0) {
2670 TAILQ_REMOVE(&devfs_dev_ops_list
, found
, link
);
2671 lockmgr(&devfs_lock
, LK_RELEASE
);
2672 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id
), found
->id
);
2673 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
2674 kfree(found
, M_DEVFS
);
2679 * Wait for asynchronous messages to complete in the devfs helper
2680 * thread, then return. Do nothing if the helper thread is dead
2681 * or we are being indirectly called from the helper thread itself.
2688 if (devfs_run
&& curthread
!= td_core
) {
2689 msg
= devfs_msg_get();
2690 devfs_msg_send_sync(DEVFS_SYNC
, msg
);
2696 * Called on init of devfs; creates the objcaches and
2697 * spawns off the devfs core thread. Also initializes
2703 devfs_debug(DEVFS_DEBUG_DEBUG
, "devfs_init() called\n");
2704 /* Create objcaches for nodes, msgs and devs */
2705 devfs_node_cache
= objcache_create("devfs-node-cache", 0, 0,
2707 objcache_malloc_alloc
,
2708 objcache_malloc_free
,
2709 &devfs_node_malloc_args
);
2711 devfs_msg_cache
= objcache_create("devfs-msg-cache", 0, 0,
2713 objcache_malloc_alloc
,
2714 objcache_malloc_free
,
2715 &devfs_msg_malloc_args
);
2717 devfs_dev_cache
= objcache_create("devfs-dev-cache", 0, 0,
2719 objcache_malloc_alloc
,
2720 objcache_malloc_free
,
2721 &devfs_dev_malloc_args
);
2723 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id
));
2725 /* Initialize the reply-only port which acts as a message drain */
2726 lwkt_initport_replyonly(&devfs_dispose_port
, devfs_msg_autofree_reply
);
2728 /* Initialize *THE* devfs lock */
2729 lockinit(&devfs_lock
, "devfs_core lock", 0, LK_CANRECURSE
);
2730 lwkt_token_init(&devfs_token
, "devfs_core");
2732 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
2733 lwkt_create(devfs_msg_core
, /*args*/NULL
, &td_core
, NULL
,
2734 0, -1, "devfs_msg_core");
2735 while (devfs_run
== 0)
2736 lksleep(td_core
, &devfs_lock
, 0, "devfsc", 0);
2737 lockmgr(&devfs_lock
, LK_RELEASE
);
2739 devfs_debug(DEVFS_DEBUG_DEBUG
, "devfs_init finished\n");
2743 * Called on unload of devfs; takes care of destroying the core
2744 * and the objcaches. Also removes aliases that are no longer needed.
2749 devfs_debug(DEVFS_DEBUG_DEBUG
, "devfs_uninit() called\n");
2751 devfs_msg_send(DEVFS_TERMINATE_CORE
, NULL
);
2753 tsleep(td_core
, 0, "devfsc", hz
*10);
2754 tsleep(td_core
, 0, "devfsc", hz
);
2756 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id
));
2758 /* Destroy the objcaches */
2759 objcache_destroy(devfs_msg_cache
);
2760 objcache_destroy(devfs_node_cache
);
2761 objcache_destroy(devfs_dev_cache
);
2767 * This is a sysctl handler to assist userland devname(3) to
2768 * find the device name for a given udev.
2771 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS
)
2777 if ((error
= SYSCTL_IN(req
, &udev
, sizeof(dev_t
))))
2780 devfs_debug(DEVFS_DEBUG_DEBUG
,
2781 "devfs sysctl, received udev: %d\n", udev
);
2786 if ((found
= devfs_find_device_by_devid(udev
)) == NULL
)
2789 return(SYSCTL_OUT(req
, found
->si_name
, strlen(found
->si_name
) + 1));
2793 SYSCTL_PROC(_kern
, OID_AUTO
, devname
,
2794 CTLTYPE_OPAQUE
| CTLFLAG_RW
| CTLFLAG_ANYBODY
| CTLFLAG_NOLOCK
,
2795 NULL
, 0, devfs_sysctl_devname_helper
, "",
2796 "helper for devname(3)");
2798 SYSCTL_NODE(_vfs
, OID_AUTO
, devfs
, CTLFLAG_RW
, 0, "devfs");
2799 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable
);
2800 SYSCTL_INT(_vfs_devfs
, OID_AUTO
, debug
, CTLFLAG_RW
, &devfs_debug_enable
,
2801 0, "Enable DevFS debugging");
2803 SYSINIT(vfs_devfs_register
, SI_SUB_DEVFS_CORE
, SI_ORDER_FIRST
,
2805 SYSUNINIT(vfs_devfs_register
, SI_SUB_DEVFS_CORE
, SI_ORDER_ANY
,
2806 devfs_uninit
, NULL
);
2809 * WildCmp() - compare wild string to sane string
2811 * Returns 0 on success, -1 on failure.
2814 wildCmp(const char **mary
, int d
, const char *w
, const char *s
)
2819 * skip fixed portion
2825 * optimize terminator
2829 if (w
[1] != '?' && w
[1] != '*') {
2831 * optimize * followed by non-wild
2833 for (i
= 0; s
+ i
< mary
[d
]; ++i
) {
2834 if (s
[i
] == w
[1] && wildCmp(mary
, d
+ 1, w
+ 1, s
+ i
) == 0)
2841 for (i
= 0; s
+ i
< mary
[d
]; ++i
) {
2842 if (wildCmp(mary
, d
+ 1, w
+ 1, s
+ i
) == 0)
2857 if (*w
== 0) /* terminator */
2870 * WildCaseCmp() - compare wild string to sane string, case insensitive
2872 * Returns 0 on success, -1 on failure.
2875 wildCaseCmp(const char **mary
, int d
, const char *w
, const char *s
)
2880 * skip fixed portion
2886 * optimize terminator
2890 if (w
[1] != '?' && w
[1] != '*') {
2892 * optimize * followed by non-wild
2894 for (i
= 0; s
+ i
< mary
[d
]; ++i
) {
2895 if (s
[i
] == w
[1] && wildCaseCmp(mary
, d
+ 1, w
+ 1, s
+ i
) == 0)
2902 for (i
= 0; s
+ i
< mary
[d
]; ++i
) {
2903 if (wildCaseCmp(mary
, d
+ 1, w
+ 1, s
+ i
) == 0)
2917 #define tolower(x) ((x >= 'A' && x <= 'Z')?(x+('a'-'A')):(x))
2918 if (tolower(*w
) != tolower(*s
))
2921 if (*w
== 0) /* terminator */
2932 struct cdev_privdata
{
2934 d_priv_dtor_t
*cdpd_dtr
;
2938 devfs_get_cdevpriv(struct file
*fp
, void **datap
)
2945 spin_lock_shared(&fp
->f_spin
);
2946 if (fp
->f_data1
== NULL
) {
2950 struct cdev_privdata
*p
= fp
->f_data1
;
2952 *datap
= p
->cdpd_data
;
2955 spin_unlock_shared(&fp
->f_spin
);
2961 devfs_set_cdevpriv(struct file
*fp
, void *priv
, d_priv_dtor_t
*dtr
)
2963 struct cdev_privdata
*p
;
2969 p
= kmalloc(sizeof(struct cdev_privdata
), M_DEVFS
, M_WAITOK
);
2970 p
->cdpd_data
= priv
;
2973 spin_lock(&fp
->f_spin
);
2974 if (fp
->f_data1
== NULL
) {
2980 spin_unlock(&fp
->f_spin
);
2989 devfs_clear_cdevpriv(struct file
*fp
)
2991 struct cdev_privdata
*p
;
2996 spin_lock(&fp
->f_spin
);
2999 spin_unlock(&fp
->f_spin
);
3002 p
->cdpd_dtr(p
->cdpd_data
);
3008 devfs_WildCmp(const char *w
, const char *s
)
3012 int slen
= strlen(s
);
3015 for (i
= c
= 0; w
[i
]; ++i
) {
3019 mary
= kmalloc(sizeof(char *) * (c
+ 1), M_DEVFS
, M_WAITOK
);
3020 for (i
= 0; i
< c
; ++i
)
3022 i
= wildCmp(mary
, 0, w
, s
);
3023 kfree(mary
, M_DEVFS
);
3028 devfs_WildCaseCmp(const char *w
, const char *s
)
3032 int slen
= strlen(s
);
3035 for (i
= c
= 0; w
[i
]; ++i
) {
3039 mary
= kmalloc(sizeof(char *) * (c
+ 1), M_DEVFS
, M_WAITOK
);
3040 for (i
= 0; i
< c
; ++i
)
3042 i
= wildCaseCmp(mary
, 0, w
, s
);
3043 kfree(mary
, M_DEVFS
);