2 * Copyright (c) 2009 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>
37 #include <sys/mount.h>
38 #include <sys/vnode.h>
39 #include <sys/types.h>
41 #include <sys/msgport.h>
42 #include <sys/msgport2.h>
43 #include <sys/spinlock2.h>
44 #include <sys/sysctl.h>
45 #include <sys/ucred.h>
46 #include <sys/param.h>
47 #include <sys/sysref2.h>
48 #include <sys/systm.h>
49 #include <sys/devfs.h>
50 #include <sys/devfs_rules.h>
51 #include <sys/hotplug.h>
53 MALLOC_DEFINE(M_DEVFS
, "devfs", "Device File System (devfs) allocations");
54 DEVFS_DECLARE_CLONE_BITMAP(ops_id
);
56 * SYSREF Integration - reference counting, allocation,
57 * sysid and syslink integration.
59 static void devfs_cdev_terminate(cdev_t dev
);
60 static struct sysref_class cdev_sysref_class
= {
63 .proto
= SYSREF_PROTO_DEV
,
64 .offset
= offsetof(struct cdev
, si_sysref
),
65 .objsize
= sizeof(struct cdev
),
69 .terminate
= (sysref_terminate_func_t
)devfs_cdev_terminate
73 static struct objcache
*devfs_node_cache
;
74 static struct objcache
*devfs_msg_cache
;
75 static struct objcache
*devfs_dev_cache
;
77 static struct objcache_malloc_args devfs_node_malloc_args
= {
78 sizeof(struct devfs_node
), M_DEVFS
};
79 struct objcache_malloc_args devfs_msg_malloc_args
= {
80 sizeof(struct devfs_msg
), M_DEVFS
};
81 struct objcache_malloc_args devfs_dev_malloc_args
= {
82 sizeof(struct cdev
), M_DEVFS
};
84 static struct devfs_dev_head devfs_dev_list
=
85 TAILQ_HEAD_INITIALIZER(devfs_dev_list
);
86 static struct devfs_mnt_head devfs_mnt_list
=
87 TAILQ_HEAD_INITIALIZER(devfs_mnt_list
);
88 static struct devfs_chandler_head devfs_chandler_list
=
89 TAILQ_HEAD_INITIALIZER(devfs_chandler_list
);
90 static struct devfs_alias_head devfs_alias_list
=
91 TAILQ_HEAD_INITIALIZER(devfs_alias_list
);
92 static struct devfs_dev_ops_head devfs_dev_ops_list
=
93 TAILQ_HEAD_INITIALIZER(devfs_dev_ops_list
);
95 struct lock devfs_lock
;
96 static struct lwkt_port devfs_dispose_port
;
97 static struct lwkt_port devfs_msg_port
;
98 static struct thread
*td_core
;
100 static struct spinlock ino_lock
;
102 static int devfs_debug_enable
;
103 static int devfs_run
;
105 static ino_t
devfs_fetch_ino(void);
106 static int devfs_create_all_dev_worker(struct devfs_node
*);
107 static int devfs_create_dev_worker(cdev_t
, uid_t
, gid_t
, int);
108 static int devfs_destroy_dev_worker(cdev_t
);
109 static int devfs_destroy_subnames_worker(char *);
110 static int devfs_destroy_dev_by_ops_worker(struct dev_ops
*, int);
111 static int devfs_propagate_dev(cdev_t
, int);
112 static int devfs_unlink_dev(cdev_t dev
);
113 static void devfs_msg_exec(devfs_msg_t msg
);
115 static int devfs_chandler_add_worker(const char *, d_clone_t
*);
116 static int devfs_chandler_del_worker(const char *);
118 static void devfs_msg_autofree_reply(lwkt_port_t
, lwkt_msg_t
);
119 static void devfs_msg_core(void *);
121 static int devfs_find_device_by_name_worker(devfs_msg_t
);
122 static int devfs_find_device_by_udev_worker(devfs_msg_t
);
124 static int devfs_apply_reset_rules_caller(char *, int);
126 static int devfs_scan_callback_worker(devfs_scan_t
*);
128 static struct devfs_node
*devfs_resolve_or_create_dir(struct devfs_node
*,
129 char *, size_t, int);
131 static int devfs_make_alias_worker(struct devfs_alias
*);
132 static int devfs_alias_remove(cdev_t
);
133 static int devfs_alias_reap(void);
134 static int devfs_alias_propagate(struct devfs_alias
*);
135 static int devfs_alias_apply(struct devfs_node
*, struct devfs_alias
*);
136 static int devfs_alias_check_create(struct devfs_node
*);
138 static int devfs_clr_subnames_flag_worker(char *, uint32_t);
139 static int devfs_destroy_subnames_without_flag_worker(char *, uint32_t);
141 static void *devfs_reaperp_callback(struct devfs_node
*, void *);
142 static void *devfs_gc_dirs_callback(struct devfs_node
*, void *);
143 static void *devfs_gc_links_callback(struct devfs_node
*, struct devfs_node
*);
145 devfs_inode_to_vnode_worker_callback(struct devfs_node
*, ino_t
*);
148 void (*devfs_node_added
)(struct hotplug_device
*) = NULL
;
149 void (*devfs_node_removed
)(struct hotplug_device
*) = NULL
;
152 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function
156 devfs_debug(int level
, char *fmt
, ...)
161 if (level
<= devfs_debug_enable
)
169 * devfs_allocp() Allocates a new devfs node with the specified
170 * parameters. The node is also automatically linked into the topology
171 * if a parent is specified. It also calls the rule and alias stuff to
172 * be applied on the new node
175 devfs_allocp(devfs_nodetype devfsnodetype
, char *name
,
176 struct devfs_node
*parent
, struct mount
*mp
, cdev_t dev
)
178 struct devfs_node
*node
= NULL
;
179 size_t namlen
= strlen(name
);
181 node
= objcache_get(devfs_node_cache
, M_WAITOK
);
182 bzero(node
, sizeof(*node
));
184 atomic_add_long(&(DEVFS_MNTDATA(mp
)->leak_count
), 1);
189 node
->d_dir
.d_ino
= devfs_fetch_ino();
192 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries
195 node
->cookie_jar
= 2;
198 * Access Control members
200 node
->mode
= DEVFS_DEFAULT_MODE
;
201 node
->uid
= DEVFS_DEFAULT_UID
;
202 node
->gid
= DEVFS_DEFAULT_GID
;
204 switch (devfsnodetype
) {
207 * Ensure that we don't recycle the root vnode by marking it as
208 * linked into the topology.
210 node
->flags
|= DEVFS_NODE_LINKED
;
212 TAILQ_INIT(DEVFS_DENODE_HEAD(node
));
213 node
->d_dir
.d_type
= DT_DIR
;
218 node
->d_dir
.d_type
= DT_LNK
;
222 node
->d_dir
.d_type
= DT_REG
;
227 node
->d_dir
.d_type
= DT_CHR
;
230 node
->mode
= dev
->si_perms
;
231 node
->uid
= dev
->si_uid
;
232 node
->gid
= dev
->si_gid
;
234 devfs_alias_check_create(node
);
239 panic("devfs_allocp: unknown node type");
243 node
->node_type
= devfsnodetype
;
245 /* Initialize the dirent structure of each devfs vnode */
246 KKASSERT(namlen
< 256);
247 node
->d_dir
.d_namlen
= namlen
;
248 node
->d_dir
.d_name
= kmalloc(namlen
+1, M_DEVFS
, M_WAITOK
);
249 memcpy(node
->d_dir
.d_name
, name
, namlen
);
250 node
->d_dir
.d_name
[namlen
] = '\0';
252 /* Initialize the parent node element */
253 node
->parent
= parent
;
256 devfs_rule_check_apply(node
, NULL
);
258 /* Initialize *time members */
259 nanotime(&node
->atime
);
260 node
->mtime
= node
->ctime
= node
->atime
;
263 * Associate with parent as last step, clean out namecache
266 if ((parent
!= NULL
) &&
267 ((parent
->node_type
== Proot
) || (parent
->node_type
== Pdir
))) {
269 node
->cookie
= parent
->cookie_jar
++;
270 node
->flags
|= DEVFS_NODE_LINKED
;
271 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent
), node
, link
);
273 /* This forces negative namecache lookups to clear */
274 ++mp
->mnt_namecache_gen
;
277 ++DEVFS_MNTDATA(mp
)->file_count
;
283 * devfs_allocv() allocates a new vnode based on a devfs node.
286 devfs_allocv(struct vnode
**vpp
, struct devfs_node
*node
)
294 while ((vp
= node
->v_node
) != NULL
) {
295 error
= vget(vp
, LK_EXCLUSIVE
);
296 if (error
!= ENOENT
) {
302 if ((error
= getnewvnode(VT_DEVFS
, node
->mp
, vpp
, 0, 0)) != 0)
307 if (node
->v_node
!= NULL
) {
316 switch (node
->node_type
) {
333 KKASSERT(node
->d_dev
);
335 vp
->v_uminor
= node
->d_dev
->si_uminor
;
338 v_associate_rdev(vp
, node
->d_dev
);
339 vp
->v_ops
= &node
->mp
->mnt_vn_spec_ops
;
343 panic("devfs_allocv: unknown node type");
351 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
352 * based on the newly created devfs node.
355 devfs_allocvp(struct mount
*mp
, struct vnode
**vpp
, devfs_nodetype devfsnodetype
,
356 char *name
, struct devfs_node
*parent
, cdev_t dev
)
358 struct devfs_node
*node
;
360 node
= devfs_allocp(devfsnodetype
, name
, parent
, mp
, dev
);
363 devfs_allocv(vpp
, node
);
371 * Destroy the devfs_node. The node must be unlinked from the topology.
373 * This function will also destroy any vnode association with the node
376 * The cdev_t itself remains intact.
379 devfs_freep(struct devfs_node
*node
)
384 KKASSERT(((node
->flags
& DEVFS_NODE_LINKED
) == 0) ||
385 (node
->node_type
== Proot
));
386 KKASSERT((node
->flags
& DEVFS_DESTROYED
) == 0);
388 atomic_subtract_long(&(DEVFS_MNTDATA(node
->mp
)->leak_count
), 1);
389 if (node
->symlink_name
) {
390 kfree(node
->symlink_name
, M_DEVFS
);
391 node
->symlink_name
= NULL
;
395 * Remove the node from the orphan list if it is still on it.
397 if (node
->flags
& DEVFS_ORPHANED
)
398 devfs_tracer_del_orphan(node
);
401 * Disassociate the vnode from the node. This also prevents the
402 * vnode's reclaim code from double-freeing the node.
404 * The vget is needed to safely modify the vp. It also serves
405 * to cycle the refs and terminate the vnode if it happens to
406 * be inactive, otherwise namecache references may not get cleared.
408 while ((vp
= node
->v_node
) != NULL
) {
409 if (vget(vp
, LK_EXCLUSIVE
| LK_RETRY
) != 0)
414 cache_inval_vp(vp
, CINV_DESTROY
);
417 if (node
->d_dir
.d_name
) {
418 kfree(node
->d_dir
.d_name
, M_DEVFS
);
419 node
->d_dir
.d_name
= NULL
;
421 node
->flags
|= DEVFS_DESTROYED
;
423 --DEVFS_MNTDATA(node
->mp
)->file_count
;
425 objcache_put(devfs_node_cache
, node
);
431 * Unlink the devfs node from the topology and add it to the orphan list.
432 * The node will later be destroyed by freep.
434 * Any vnode association, including the v_rdev and v_data, remains intact
438 devfs_unlinkp(struct devfs_node
*node
)
440 struct devfs_node
*parent
;
441 struct hotplug_device
*hpdev
;
445 * Add the node to the orphan list, so it is referenced somewhere, to
446 * so we don't leak it.
448 devfs_tracer_add_orphan(node
);
450 parent
= node
->parent
;
453 * If the parent is known we can unlink the node out of the topology
456 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent
), node
, link
);
458 KKASSERT((parent
->nchildren
>= 0));
459 node
->flags
&= ~DEVFS_NODE_LINKED
;
461 /* hotplug handler */
462 if(devfs_node_removed
) {
463 hpdev
= kmalloc(sizeof(struct hotplug_device
), M_TEMP
, M_WAITOK
);
464 hpdev
->dev
= node
->d_dev
;
466 hpdev
->name
= node
->d_dev
->si_name
;
467 devfs_node_removed(hpdev
);
468 kfree(hpdev
, M_TEMP
);
475 devfs_iterate_topology(struct devfs_node
*node
,
476 devfs_iterate_callback_t
*callback
, void *arg1
)
478 struct devfs_node
*node1
, *node2
;
481 if ((node
->node_type
== Proot
) || (node
->node_type
== Pdir
)) {
482 if (node
->nchildren
> 2) {
483 TAILQ_FOREACH_MUTABLE(node1
, DEVFS_DENODE_HEAD(node
),
485 if ((ret
= devfs_iterate_topology(node1
, callback
, arg1
)))
491 ret
= callback(node
, arg1
);
496 * devfs_reaperp() is a recursive function that iterates through all the
497 * topology, unlinking and freeing all devfs nodes.
500 devfs_reaperp_callback(struct devfs_node
*node
, void *unused
)
509 devfs_gc_dirs_callback(struct devfs_node
*node
, void *unused
)
511 if (node
->node_type
== Pdir
) {
512 if (node
->nchildren
== 2) {
522 devfs_gc_links_callback(struct devfs_node
*node
, struct devfs_node
*target
)
524 if ((node
->node_type
== Plink
) && (node
->link_target
== target
)) {
533 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
534 * freeing a node, but also removes empty directories and links that link
535 * via devfs auto-link mechanism to the node being deleted.
538 devfs_gc(struct devfs_node
*node
)
540 struct devfs_node
*root_node
= DEVFS_MNTDATA(node
->mp
)->root_node
;
542 if (node
->nlinks
> 0)
543 devfs_iterate_topology(root_node
,
544 (devfs_iterate_callback_t
*)devfs_gc_links_callback
, node
);
547 devfs_iterate_topology(root_node
,
548 (devfs_iterate_callback_t
*)devfs_gc_dirs_callback
, NULL
);
556 * devfs_create_dev() is the asynchronous entry point for device creation.
557 * It just sends a message with the relevant details to the devfs core.
559 * This function will reference the passed device. The reference is owned
560 * by devfs and represents all of the device's node associations.
563 devfs_create_dev(cdev_t dev
, uid_t uid
, gid_t gid
, int perms
)
566 devfs_msg_send_dev(DEVFS_DEVICE_CREATE
, dev
, uid
, gid
, perms
);
572 * devfs_destroy_dev() is the asynchronous entry point for device destruction.
573 * It just sends a message with the relevant details to the devfs core.
576 devfs_destroy_dev(cdev_t dev
)
578 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY
, dev
, 0, 0, 0);
583 * devfs_mount_add() is the synchronous entry point for adding a new devfs
584 * mount. It sends a synchronous message with the relevant details to the
588 devfs_mount_add(struct devfs_mnt_data
*mnt
)
592 msg
= devfs_msg_get();
594 msg
= devfs_msg_send_sync(DEVFS_MOUNT_ADD
, msg
);
601 * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
602 * It sends a synchronous message with the relevant details to the devfs core.
605 devfs_mount_del(struct devfs_mnt_data
*mnt
)
609 msg
= devfs_msg_get();
611 msg
= devfs_msg_send_sync(DEVFS_MOUNT_DEL
, msg
);
618 * devfs_destroy_subnames() is the synchronous entry point for device
619 * destruction by subname. It just sends a message with the relevant details to
623 devfs_destroy_subnames(char *name
)
627 msg
= devfs_msg_get();
628 msg
->mdv_load
= name
;
629 msg
= devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES
, msg
);
635 devfs_clr_subnames_flag(char *name
, uint32_t flag
)
639 msg
= devfs_msg_get();
640 msg
->mdv_flags
.name
= name
;
641 msg
->mdv_flags
.flag
= flag
;
642 msg
= devfs_msg_send_sync(DEVFS_CLR_SUBNAMES_FLAG
, msg
);
649 devfs_destroy_subnames_without_flag(char *name
, uint32_t flag
)
653 msg
= devfs_msg_get();
654 msg
->mdv_flags
.name
= name
;
655 msg
->mdv_flags
.flag
= flag
;
656 msg
= devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES_WO_FLAG
, msg
);
663 * devfs_create_all_dev is the asynchronous entry point to trigger device
664 * node creation. It just sends a message with the relevant details to
668 devfs_create_all_dev(struct devfs_node
*root
)
670 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV
, root
);
675 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
676 * devices with a specific set of dev_ops and minor. It just sends a
677 * message with the relevant details to the devfs core.
680 devfs_destroy_dev_by_ops(struct dev_ops
*ops
, int minor
)
682 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS
, ops
, minor
);
687 * devfs_clone_handler_add is the synchronous entry point to add a new
688 * clone handler. It just sends a message with the relevant details to
692 devfs_clone_handler_add(const char *name
, d_clone_t
*nhandler
)
696 msg
= devfs_msg_get();
697 msg
->mdv_chandler
.name
= name
;
698 msg
->mdv_chandler
.nhandler
= nhandler
;
699 msg
= devfs_msg_send_sync(DEVFS_CHANDLER_ADD
, msg
);
705 * devfs_clone_handler_del is the synchronous entry point to remove a
706 * clone handler. It just sends a message with the relevant details to
710 devfs_clone_handler_del(const char *name
)
714 msg
= devfs_msg_get();
715 msg
->mdv_chandler
.name
= name
;
716 msg
->mdv_chandler
.nhandler
= NULL
;
717 msg
= devfs_msg_send_sync(DEVFS_CHANDLER_DEL
, msg
);
723 * devfs_find_device_by_name is the synchronous entry point to find a
724 * device given its name. It sends a synchronous message with the
725 * relevant details to the devfs core and returns the answer.
728 devfs_find_device_by_name(const char *fmt
, ...)
739 kvasnrprintf(&target
, PATH_MAX
, 10, fmt
, ap
);
742 msg
= devfs_msg_get();
743 msg
->mdv_name
= target
;
744 msg
= devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME
, msg
);
745 found
= msg
->mdv_cdev
;
753 * devfs_find_device_by_udev is the synchronous entry point to find a
754 * device given its udev number. It sends a synchronous message with
755 * the relevant details to the devfs core and returns the answer.
758 devfs_find_device_by_udev(udev_t udev
)
763 msg
= devfs_msg_get();
764 msg
->mdv_udev
= udev
;
765 msg
= devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV
, msg
);
766 found
= msg
->mdv_cdev
;
769 devfs_debug(DEVFS_DEBUG_DEBUG
,
770 "devfs_find_device_by_udev found? %s -end:3-\n",
771 ((found
) ? found
->si_name
:"NO"));
776 devfs_inode_to_vnode(struct mount
*mp
, ino_t target
)
778 struct vnode
*vp
= NULL
;
784 msg
= devfs_msg_get();
785 msg
->mdv_ino
.mp
= mp
;
786 msg
->mdv_ino
.ino
= target
;
787 msg
= devfs_msg_send_sync(DEVFS_INODE_TO_VNODE
, msg
);
788 vp
= msg
->mdv_ino
.vp
;
789 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
796 * devfs_make_alias is the asynchronous entry point to register an alias
797 * for a device. It just sends a message with the relevant details to the
801 devfs_make_alias(const char *name
, cdev_t dev_target
)
803 struct devfs_alias
*alias
;
808 alias
= kmalloc(sizeof(struct devfs_alias
), M_DEVFS
, M_WAITOK
);
809 alias
->name
= kstrdup(name
, M_DEVFS
);
811 alias
->dev_target
= dev_target
;
813 devfs_msg_send_generic(DEVFS_MAKE_ALIAS
, alias
);
818 * devfs_apply_rules is the asynchronous entry point to trigger application
819 * of all rules. It just sends a message with the relevant details to the
823 devfs_apply_rules(char *mntto
)
827 new_name
= kstrdup(mntto
, M_DEVFS
);
828 devfs_msg_send_name(DEVFS_APPLY_RULES
, new_name
);
834 * devfs_reset_rules is the asynchronous entry point to trigger reset of all
835 * rules. It just sends a message with the relevant details to the devfs core.
838 devfs_reset_rules(char *mntto
)
842 new_name
= kstrdup(mntto
, M_DEVFS
);
843 devfs_msg_send_name(DEVFS_RESET_RULES
, new_name
);
850 * devfs_scan_callback is the asynchronous entry point to call a callback
852 * It just sends a message with the relevant details to the devfs core.
855 devfs_scan_callback(devfs_scan_t
*callback
)
859 KKASSERT(sizeof(callback
) == sizeof(void *));
861 msg
= devfs_msg_get();
862 msg
->mdv_load
= callback
;
863 msg
= devfs_msg_send_sync(DEVFS_SCAN_CALLBACK
, msg
);
871 * Acts as a message drain. Any message that is replied to here gets destroyed
872 * and the memory freed.
875 devfs_msg_autofree_reply(lwkt_port_t port
, lwkt_msg_t msg
)
877 devfs_msg_put((devfs_msg_t
)msg
);
881 * devfs_msg_get allocates a new devfs msg and returns it.
886 return objcache_get(devfs_msg_cache
, M_WAITOK
);
890 * devfs_msg_put deallocates a given devfs msg.
893 devfs_msg_put(devfs_msg_t msg
)
895 objcache_put(devfs_msg_cache
, msg
);
900 * devfs_msg_send is the generic asynchronous message sending facility
901 * for devfs. By default the reply port is the automatic disposal port.
903 * If the current thread is the devfs_msg_port thread we execute the
904 * operation synchronously.
907 devfs_msg_send(uint32_t cmd
, devfs_msg_t devfs_msg
)
909 lwkt_port_t port
= &devfs_msg_port
;
911 lwkt_initmsg(&devfs_msg
->hdr
, &devfs_dispose_port
, 0);
913 devfs_msg
->hdr
.u
.ms_result
= cmd
;
915 if (port
->mpu_td
== curthread
) {
916 devfs_msg_exec(devfs_msg
);
917 lwkt_replymsg(&devfs_msg
->hdr
, 0);
919 lwkt_sendmsg(port
, (lwkt_msg_t
)devfs_msg
);
924 * devfs_msg_send_sync is the generic synchronous message sending
925 * facility for devfs. It initializes a local reply port and waits
926 * for the core's answer. This answer is then returned.
929 devfs_msg_send_sync(uint32_t cmd
, devfs_msg_t devfs_msg
)
931 struct lwkt_port rep_port
;
932 devfs_msg_t msg_incoming
;
933 lwkt_port_t port
= &devfs_msg_port
;
935 lwkt_initport_thread(&rep_port
, curthread
);
936 lwkt_initmsg(&devfs_msg
->hdr
, &rep_port
, 0);
938 devfs_msg
->hdr
.u
.ms_result
= cmd
;
940 lwkt_sendmsg(port
, (lwkt_msg_t
)devfs_msg
);
941 msg_incoming
= lwkt_waitport(&rep_port
, 0);
947 * sends a message with a generic argument.
950 devfs_msg_send_generic(uint32_t cmd
, void *load
)
952 devfs_msg_t devfs_msg
= devfs_msg_get();
954 devfs_msg
->mdv_load
= load
;
955 devfs_msg_send(cmd
, devfs_msg
);
959 * sends a message with a name argument.
962 devfs_msg_send_name(uint32_t cmd
, char *name
)
964 devfs_msg_t devfs_msg
= devfs_msg_get();
966 devfs_msg
->mdv_name
= name
;
967 devfs_msg_send(cmd
, devfs_msg
);
971 * sends a message with a mount argument.
974 devfs_msg_send_mount(uint32_t cmd
, struct devfs_mnt_data
*mnt
)
976 devfs_msg_t devfs_msg
= devfs_msg_get();
978 devfs_msg
->mdv_mnt
= mnt
;
979 devfs_msg_send(cmd
, devfs_msg
);
983 * sends a message with an ops argument.
986 devfs_msg_send_ops(uint32_t cmd
, struct dev_ops
*ops
, int minor
)
988 devfs_msg_t devfs_msg
= devfs_msg_get();
990 devfs_msg
->mdv_ops
.ops
= ops
;
991 devfs_msg
->mdv_ops
.minor
= minor
;
992 devfs_msg_send(cmd
, devfs_msg
);
996 * sends a message with a clone handler argument.
999 devfs_msg_send_chandler(uint32_t cmd
, char *name
, d_clone_t handler
)
1001 devfs_msg_t devfs_msg
= devfs_msg_get();
1003 devfs_msg
->mdv_chandler
.name
= name
;
1004 devfs_msg
->mdv_chandler
.nhandler
= handler
;
1005 devfs_msg_send(cmd
, devfs_msg
);
1009 * sends a message with a device argument.
1012 devfs_msg_send_dev(uint32_t cmd
, cdev_t dev
, uid_t uid
, gid_t gid
, int perms
)
1014 devfs_msg_t devfs_msg
= devfs_msg_get();
1016 devfs_msg
->mdv_dev
.dev
= dev
;
1017 devfs_msg
->mdv_dev
.uid
= uid
;
1018 devfs_msg
->mdv_dev
.gid
= gid
;
1019 devfs_msg
->mdv_dev
.perms
= perms
;
1021 devfs_msg_send(cmd
, devfs_msg
);
1025 * sends a message with a link argument.
1028 devfs_msg_send_link(uint32_t cmd
, char *name
, char *target
, struct mount
*mp
)
1030 devfs_msg_t devfs_msg
= devfs_msg_get();
1032 devfs_msg
->mdv_link
.name
= name
;
1033 devfs_msg
->mdv_link
.target
= target
;
1034 devfs_msg
->mdv_link
.mp
= mp
;
1035 devfs_msg_send(cmd
, devfs_msg
);
1039 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1040 * and calls the relevant worker functions. By using messages it's assured
1041 * that events occur in the correct order.
1044 devfs_msg_core(void *arg
)
1049 lwkt_initport_thread(&devfs_msg_port
, curthread
);
1053 msg
= (devfs_msg_t
)lwkt_waitport(&devfs_msg_port
, 0);
1054 devfs_debug(DEVFS_DEBUG_DEBUG
,
1055 "devfs_msg_core, new msg: %x\n",
1056 (unsigned int)msg
->hdr
.u
.ms_result
);
1057 devfs_msg_exec(msg
);
1058 lwkt_replymsg(&msg
->hdr
, 0);
1065 devfs_msg_exec(devfs_msg_t msg
)
1067 struct devfs_mnt_data
*mnt
;
1068 struct devfs_node
*node
;
1072 * Acquire the devfs lock to ensure safety of all called functions
1074 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
1076 switch (msg
->hdr
.u
.ms_result
) {
1077 case DEVFS_DEVICE_CREATE
:
1078 dev
= msg
->mdv_dev
.dev
;
1079 devfs_create_dev_worker(dev
,
1082 msg
->mdv_dev
.perms
);
1084 case DEVFS_DEVICE_DESTROY
:
1085 dev
= msg
->mdv_dev
.dev
;
1086 devfs_destroy_dev_worker(dev
);
1088 case DEVFS_DESTROY_SUBNAMES
:
1089 devfs_destroy_subnames_worker(msg
->mdv_load
);
1091 case DEVFS_DESTROY_DEV_BY_OPS
:
1092 devfs_destroy_dev_by_ops_worker(msg
->mdv_ops
.ops
,
1093 msg
->mdv_ops
.minor
);
1095 case DEVFS_CREATE_ALL_DEV
:
1096 node
= (struct devfs_node
*)msg
->mdv_load
;
1097 devfs_create_all_dev_worker(node
);
1099 case DEVFS_MOUNT_ADD
:
1101 TAILQ_INSERT_TAIL(&devfs_mnt_list
, mnt
, link
);
1102 devfs_create_all_dev_worker(mnt
->root_node
);
1104 case DEVFS_MOUNT_DEL
:
1106 TAILQ_REMOVE(&devfs_mnt_list
, mnt
, link
);
1107 devfs_iterate_topology(mnt
->root_node
, devfs_reaperp_callback
,
1109 if (mnt
->leak_count
) {
1110 devfs_debug(DEVFS_DEBUG_SHOW
,
1111 "Leaked %ld devfs_node elements!\n",
1115 case DEVFS_CHANDLER_ADD
:
1116 devfs_chandler_add_worker(msg
->mdv_chandler
.name
,
1117 msg
->mdv_chandler
.nhandler
);
1119 case DEVFS_CHANDLER_DEL
:
1120 devfs_chandler_del_worker(msg
->mdv_chandler
.name
);
1122 case DEVFS_FIND_DEVICE_BY_NAME
:
1123 devfs_find_device_by_name_worker(msg
);
1125 case DEVFS_FIND_DEVICE_BY_UDEV
:
1126 devfs_find_device_by_udev_worker(msg
);
1128 case DEVFS_MAKE_ALIAS
:
1129 devfs_make_alias_worker((struct devfs_alias
*)msg
->mdv_load
);
1131 case DEVFS_APPLY_RULES
:
1132 devfs_apply_reset_rules_caller(msg
->mdv_name
, 1);
1134 case DEVFS_RESET_RULES
:
1135 devfs_apply_reset_rules_caller(msg
->mdv_name
, 0);
1137 case DEVFS_SCAN_CALLBACK
:
1138 devfs_scan_callback_worker((devfs_scan_t
*)msg
->mdv_load
);
1140 case DEVFS_CLR_SUBNAMES_FLAG
:
1141 devfs_clr_subnames_flag_worker(msg
->mdv_flags
.name
,
1142 msg
->mdv_flags
.flag
);
1144 case DEVFS_DESTROY_SUBNAMES_WO_FLAG
:
1145 devfs_destroy_subnames_without_flag_worker(msg
->mdv_flags
.name
,
1146 msg
->mdv_flags
.flag
);
1148 case DEVFS_INODE_TO_VNODE
:
1149 msg
->mdv_ino
.vp
= devfs_iterate_topology(
1150 DEVFS_MNTDATA(msg
->mdv_ino
.mp
)->root_node
,
1151 (devfs_iterate_callback_t
*)devfs_inode_to_vnode_worker_callback
,
1154 case DEVFS_TERMINATE_CORE
:
1160 devfs_debug(DEVFS_DEBUG_WARNING
,
1161 "devfs_msg_core: unknown message "
1162 "received at core\n");
1165 lockmgr(&devfs_lock
, LK_RELEASE
);
1169 * Worker function to insert a new dev into the dev list and initialize its
1170 * permissions. It also calls devfs_propagate_dev which in turn propagates
1171 * the change to all mount points.
1173 * The passed dev is already referenced. This reference is eaten by this
1174 * function and represents the dev's linkage into devfs_dev_list.
1177 devfs_create_dev_worker(cdev_t dev
, uid_t uid
, gid_t gid
, int perms
)
1183 dev
->si_perms
= perms
;
1185 devfs_link_dev(dev
);
1186 devfs_propagate_dev(dev
, 1);
1192 * Worker function to delete a dev from the dev list and free the cdev.
1193 * It also calls devfs_propagate_dev which in turn propagates the change
1194 * to all mount points.
1197 devfs_destroy_dev_worker(cdev_t dev
)
1202 KKASSERT((lockstatus(&devfs_lock
, curthread
)) == LK_EXCLUSIVE
);
1204 error
= devfs_unlink_dev(dev
);
1205 devfs_propagate_dev(dev
, 0);
1207 release_dev(dev
); /* link ref */
1215 * Worker function to destroy all devices with a certain basename.
1216 * Calls devfs_destroy_dev_worker for the actual destruction.
1219 devfs_destroy_subnames_worker(char *name
)
1222 size_t len
= strlen(name
);
1224 TAILQ_FOREACH_MUTABLE(dev
, &devfs_dev_list
, link
, dev1
) {
1225 if ((!strncmp(dev
->si_name
, name
, len
)) &&
1226 (dev
->si_name
[len
] != '\0')) {
1227 devfs_destroy_dev_worker(dev
);
1234 devfs_clr_subnames_flag_worker(char *name
, uint32_t flag
)
1237 size_t len
= strlen(name
);
1239 TAILQ_FOREACH_MUTABLE(dev
, &devfs_dev_list
, link
, dev1
) {
1240 if ((!strncmp(dev
->si_name
, name
, len
)) &&
1241 (dev
->si_name
[len
] != '\0')) {
1242 dev
->si_flags
&= ~flag
;
1250 devfs_destroy_subnames_without_flag_worker(char *name
, uint32_t flag
)
1253 size_t len
= strlen(name
);
1255 TAILQ_FOREACH_MUTABLE(dev
, &devfs_dev_list
, link
, dev1
) {
1256 if ((!strncmp(dev
->si_name
, name
, len
)) &&
1257 (dev
->si_name
[len
] != '\0')) {
1258 if (!(dev
->si_flags
& flag
)) {
1259 devfs_destroy_dev_worker(dev
);
1268 * Worker function that creates all device nodes on top of a devfs
1272 devfs_create_all_dev_worker(struct devfs_node
*root
)
1278 TAILQ_FOREACH(dev
, &devfs_dev_list
, link
) {
1279 devfs_create_device_node(root
, dev
, NULL
, NULL
);
1286 * Worker function that destroys all devices that match a specific
1287 * dev_ops and/or minor. If minor is less than 0, it is not matched
1288 * against. It also propagates all changes.
1291 devfs_destroy_dev_by_ops_worker(struct dev_ops
*ops
, int minor
)
1297 TAILQ_FOREACH_MUTABLE(dev
, &devfs_dev_list
, link
, dev1
) {
1298 if (dev
->si_ops
!= ops
)
1300 if ((minor
< 0) || (dev
->si_uminor
== minor
)) {
1301 devfs_destroy_dev_worker(dev
);
1309 * Worker function that registers a new clone handler in devfs.
1312 devfs_chandler_add_worker(const char *name
, d_clone_t
*nhandler
)
1314 struct devfs_clone_handler
*chandler
= NULL
;
1315 u_char len
= strlen(name
);
1320 TAILQ_FOREACH(chandler
, &devfs_chandler_list
, link
) {
1321 if (chandler
->namlen
!= len
)
1324 if (!memcmp(chandler
->name
, name
, len
)) {
1325 /* Clonable basename already exists */
1330 chandler
= kmalloc(sizeof(*chandler
), M_DEVFS
, M_WAITOK
| M_ZERO
);
1331 chandler
->name
= kstrdup(name
, M_DEVFS
);
1332 chandler
->namlen
= len
;
1333 chandler
->nhandler
= nhandler
;
1335 TAILQ_INSERT_TAIL(&devfs_chandler_list
, chandler
, link
);
1340 * Worker function that removes a given clone handler from the
1341 * clone handler list.
1344 devfs_chandler_del_worker(const char *name
)
1346 struct devfs_clone_handler
*chandler
, *chandler2
;
1347 u_char len
= strlen(name
);
1352 TAILQ_FOREACH_MUTABLE(chandler
, &devfs_chandler_list
, link
, chandler2
) {
1353 if (chandler
->namlen
!= len
)
1355 if (memcmp(chandler
->name
, name
, len
))
1358 TAILQ_REMOVE(&devfs_chandler_list
, chandler
, link
);
1359 kfree(chandler
->name
, M_DEVFS
);
1360 kfree(chandler
, M_DEVFS
);
1368 * Worker function that finds a given device name and changes
1369 * the message received accordingly so that when replied to,
1370 * the answer is returned to the caller.
1373 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg
)
1375 struct devfs_alias
*alias
;
1377 cdev_t found
= NULL
;
1379 TAILQ_FOREACH(dev
, &devfs_dev_list
, link
) {
1380 if (strcmp(devfs_msg
->mdv_name
, dev
->si_name
) == 0) {
1385 if (found
== NULL
) {
1386 TAILQ_FOREACH(alias
, &devfs_alias_list
, link
) {
1387 if (strcmp(devfs_msg
->mdv_name
, alias
->name
) == 0) {
1388 found
= alias
->dev_target
;
1393 devfs_msg
->mdv_cdev
= found
;
1399 * Worker function that finds a given device udev and changes
1400 * the message received accordingly so that when replied to,
1401 * the answer is returned to the caller.
1404 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg
)
1407 cdev_t found
= NULL
;
1409 TAILQ_FOREACH_MUTABLE(dev
, &devfs_dev_list
, link
, dev1
) {
1410 if (((udev_t
)dev
->si_inode
) == devfs_msg
->mdv_udev
) {
1415 devfs_msg
->mdv_cdev
= found
;
1421 * Worker function that inserts a given alias into the
1422 * alias list, and propagates the alias to all mount
1426 devfs_make_alias_worker(struct devfs_alias
*alias
)
1428 struct devfs_alias
*alias2
;
1429 size_t len
= strlen(alias
->name
);
1432 TAILQ_FOREACH(alias2
, &devfs_alias_list
, link
) {
1433 if (len
!= alias2
->namlen
)
1436 if (!memcmp(alias
->name
, alias2
->name
, len
)) {
1444 * The alias doesn't exist yet, so we add it to the alias list
1446 TAILQ_INSERT_TAIL(&devfs_alias_list
, alias
, link
);
1447 devfs_alias_propagate(alias
);
1449 devfs_debug(DEVFS_DEBUG_WARNING
,
1450 "Warning: duplicate devfs_make_alias for %s\n",
1452 kfree(alias
->name
, M_DEVFS
);
1453 kfree(alias
, M_DEVFS
);
1460 * Function that removes and frees all aliases.
1463 devfs_alias_reap(void)
1465 struct devfs_alias
*alias
, *alias2
;
1467 TAILQ_FOREACH_MUTABLE(alias
, &devfs_alias_list
, link
, alias2
) {
1468 TAILQ_REMOVE(&devfs_alias_list
, alias
, link
);
1469 kfree(alias
, M_DEVFS
);
1475 * Function that removes an alias matching a specific cdev and frees
1479 devfs_alias_remove(cdev_t dev
)
1481 struct devfs_alias
*alias
, *alias2
;
1483 TAILQ_FOREACH_MUTABLE(alias
, &devfs_alias_list
, link
, alias2
) {
1484 if (alias
->dev_target
== dev
) {
1485 TAILQ_REMOVE(&devfs_alias_list
, alias
, link
);
1486 kfree(alias
, M_DEVFS
);
1493 * This function propagates a new alias to all mount points.
1496 devfs_alias_propagate(struct devfs_alias
*alias
)
1498 struct devfs_mnt_data
*mnt
;
1500 TAILQ_FOREACH(mnt
, &devfs_mnt_list
, link
) {
1501 devfs_alias_apply(mnt
->root_node
, alias
);
1507 * This function is a recursive function iterating through
1508 * all device nodes in the topology and, if applicable,
1509 * creating the relevant alias for a device node.
1512 devfs_alias_apply(struct devfs_node
*node
, struct devfs_alias
*alias
)
1514 struct devfs_node
*node1
, *node2
;
1516 KKASSERT(alias
!= NULL
);
1518 if ((node
->node_type
== Proot
) || (node
->node_type
== Pdir
)) {
1519 if (node
->nchildren
> 2) {
1520 TAILQ_FOREACH_MUTABLE(node1
, DEVFS_DENODE_HEAD(node
), link
, node2
) {
1521 devfs_alias_apply(node1
, alias
);
1525 if (node
->d_dev
== alias
->dev_target
)
1526 devfs_alias_create(alias
->name
, node
, 0);
1532 * This function checks if any alias possibly is applicable
1533 * to the given node. If so, the alias is created.
1536 devfs_alias_check_create(struct devfs_node
*node
)
1538 struct devfs_alias
*alias
;
1540 TAILQ_FOREACH(alias
, &devfs_alias_list
, link
) {
1541 if (node
->d_dev
== alias
->dev_target
)
1542 devfs_alias_create(alias
->name
, node
, 0);
1548 * This function creates an alias with a given name
1549 * linking to a given devfs node. It also increments
1550 * the link count on the target node.
1553 devfs_alias_create(char *name_orig
, struct devfs_node
*target
, int rule_based
)
1555 struct mount
*mp
= target
->mp
;
1556 struct devfs_node
*parent
= DEVFS_MNTDATA(mp
)->root_node
;
1557 struct devfs_node
*linknode
;
1558 struct hotplug_device
*hpdev
;
1559 char *create_path
= NULL
;
1564 KKASSERT((lockstatus(&devfs_lock
, curthread
)) == LK_EXCLUSIVE
);
1566 name_buf
= kmalloc(PATH_MAX
, M_TEMP
, M_WAITOK
);
1567 devfs_resolve_name_path(name_orig
, name_buf
, &create_path
, &name
);
1570 parent
= devfs_resolve_or_create_path(parent
, create_path
, 1);
1573 if (devfs_find_device_node_by_name(parent
, name
)) {
1574 devfs_debug(DEVFS_DEBUG_WARNING
,
1575 "Node already exists: %s "
1576 "(devfs_make_alias_worker)!\n",
1582 linknode
= devfs_allocp(Plink
, name
, parent
, mp
, NULL
);
1583 if (linknode
== NULL
) {
1588 linknode
->link_target
= target
;
1592 linknode
->flags
|= DEVFS_RULE_CREATED
;
1595 /* hotplug handler */
1596 if(devfs_node_added
) {
1597 hpdev
= kmalloc(sizeof(struct hotplug_device
), M_TEMP
, M_WAITOK
);
1598 hpdev
->dev
= target
->d_dev
;
1599 hpdev
->name
= name_orig
;
1600 devfs_node_added(hpdev
);
1601 kfree(hpdev
, M_TEMP
);
1603 kfree(name_buf
, M_TEMP
);
1608 * This function is called by the core and handles mount point
1609 * strings. It either calls the relevant worker (devfs_apply_
1610 * reset_rules_worker) on all mountpoints or only a specific
1614 devfs_apply_reset_rules_caller(char *mountto
, int apply
)
1616 struct devfs_mnt_data
*mnt
;
1618 if (mountto
[0] == '*') {
1619 TAILQ_FOREACH(mnt
, &devfs_mnt_list
, link
) {
1620 devfs_iterate_topology(mnt
->root_node
,
1621 (apply
)?(devfs_rule_check_apply
):(devfs_rule_reset_node
),
1625 TAILQ_FOREACH(mnt
, &devfs_mnt_list
, link
) {
1626 if (!strcmp(mnt
->mp
->mnt_stat
.f_mntonname
, mountto
)) {
1627 devfs_iterate_topology(mnt
->root_node
,
1628 (apply
)?(devfs_rule_check_apply
):(devfs_rule_reset_node
),
1635 kfree(mountto
, M_DEVFS
);
1640 * This function calls a given callback function for
1641 * every dev node in the devfs dev list.
1644 devfs_scan_callback_worker(devfs_scan_t
*callback
)
1648 TAILQ_FOREACH_MUTABLE(dev
, &devfs_dev_list
, link
, dev1
) {
1656 * This function tries to resolve a given directory, or if not
1657 * found and creation requested, creates the given directory.
1659 static struct devfs_node
*
1660 devfs_resolve_or_create_dir(struct devfs_node
*parent
, char *dir_name
,
1661 size_t name_len
, int create
)
1663 struct devfs_node
*node
, *found
= NULL
;
1665 TAILQ_FOREACH(node
, DEVFS_DENODE_HEAD(parent
), link
) {
1666 if (name_len
!= node
->d_dir
.d_namlen
)
1669 if (!memcmp(dir_name
, node
->d_dir
.d_name
, name_len
)) {
1675 if ((found
== NULL
) && (create
)) {
1676 found
= devfs_allocp(Pdir
, dir_name
, parent
, parent
->mp
, NULL
);
1683 * This function tries to resolve a complete path. If creation is requested,
1684 * if a given part of the path cannot be resolved (because it doesn't exist),
1688 devfs_resolve_or_create_path(struct devfs_node
*parent
, char *path
, int create
)
1690 struct devfs_node
*node
= parent
;
1697 buf
= kmalloc(PATH_MAX
, M_TEMP
, M_WAITOK
);
1699 while (*path
&& idx
< PATH_MAX
- 1) {
1704 node
= devfs_resolve_or_create_dir(node
, buf
, idx
, create
);
1714 node
= devfs_resolve_or_create_dir(node
, buf
, idx
, create
);
1715 kfree (buf
, M_TEMP
);
1720 * Takes a full path and strips it into a directory path and a name.
1721 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
1722 * requires a working buffer with enough size to keep the whole
1726 devfs_resolve_name_path(char *fullpath
, char *buf
, char **pathp
, char **namep
)
1730 size_t len
= strlen(fullpath
) + 1;
1733 KKASSERT((fullpath
!= NULL
) && (buf
!= NULL
));
1734 KKASSERT((pathp
!= NULL
) && (namep
!= NULL
));
1736 memcpy(buf
, fullpath
, len
);
1738 for (i
= len
-1; i
>= 0; i
--) {
1739 if (buf
[i
] == '/') {
1759 * This function creates a new devfs node for a given device. It can
1760 * handle a complete path as device name, and accordingly creates
1761 * the path and the final device node.
1763 * The reference count on the passed dev remains unchanged.
1766 devfs_create_device_node(struct devfs_node
*root
, cdev_t dev
,
1767 char *dev_name
, char *path_fmt
, ...)
1769 struct devfs_node
*parent
, *node
= NULL
;
1770 struct hotplug_device
*hpdev
;
1776 char *create_path
= NULL
;
1777 char *names
= "pqrsPQRS";
1779 name_buf
= kmalloc(PATH_MAX
, M_TEMP
, M_WAITOK
);
1781 if (path_fmt
!= NULL
) {
1782 __va_start(ap
, path_fmt
);
1783 kvasnrprintf(&path
, PATH_MAX
, 10, path_fmt
, ap
);
1787 parent
= devfs_resolve_or_create_path(root
, path
, 1);
1790 devfs_resolve_name_path(
1791 ((dev_name
== NULL
) && (dev
))?(dev
->si_name
):(dev_name
),
1792 name_buf
, &create_path
, &name
);
1795 parent
= devfs_resolve_or_create_path(parent
, create_path
, 1);
1798 if (devfs_find_device_node_by_name(parent
, name
)) {
1799 devfs_debug(DEVFS_DEBUG_WARNING
, "devfs_create_device_node: "
1800 "DEVICE %s ALREADY EXISTS!!! Ignoring creation request.\n", name
);
1804 node
= devfs_allocp(Pdev
, name
, parent
, parent
->mp
, dev
);
1805 nanotime(&parent
->mtime
);
1808 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
1811 if ((dev
) && (strlen(dev
->si_name
) >= 4) &&
1812 (!memcmp(dev
->si_name
, "ptm/", 4))) {
1813 node
->parent
->flags
|= DEVFS_HIDDEN
;
1814 node
->flags
|= DEVFS_HIDDEN
;
1818 * Ugly pty magic, to tag pty devices as such and hide them if needed.
1820 if ((strlen(name
) >= 3) && (!memcmp(name
, "pty", 3)))
1821 node
->flags
|= (DEVFS_PTY
| DEVFS_INVISIBLE
);
1823 if ((strlen(name
) >= 3) && (!memcmp(name
, "tty", 3))) {
1825 for (i
= 0; i
< strlen(names
); i
++) {
1826 if (name
[3] == names
[i
]) {
1832 node
->flags
|= (DEVFS_PTY
| DEVFS_INVISIBLE
);
1834 /* hotplug handler */
1835 if(devfs_node_added
) {
1836 hpdev
= kmalloc(sizeof(struct hotplug_device
), M_TEMP
, M_WAITOK
);
1837 hpdev
->dev
= node
->d_dev
;
1838 hpdev
->name
= node
->d_dev
->si_name
;
1839 devfs_node_added(hpdev
);
1840 kfree(hpdev
, M_TEMP
);
1844 kfree(name_buf
, M_TEMP
);
1850 * This function finds a given device node in the topology with a given
1854 devfs_find_device_node_callback(struct devfs_node
*node
, cdev_t target
)
1856 if ((node
->node_type
== Pdev
) && (node
->d_dev
== target
)) {
1864 * This function finds a device node in the given parent directory by its
1865 * name and returns it.
1868 devfs_find_device_node_by_name(struct devfs_node
*parent
, char *target
)
1870 struct devfs_node
*node
, *found
= NULL
;
1871 size_t len
= strlen(target
);
1873 TAILQ_FOREACH(node
, DEVFS_DENODE_HEAD(parent
), link
) {
1874 if (len
!= node
->d_dir
.d_namlen
)
1877 if (!memcmp(node
->d_dir
.d_name
, target
, len
)) {
1887 devfs_inode_to_vnode_worker_callback(struct devfs_node
*node
, ino_t
*inop
)
1889 struct vnode
*vp
= NULL
;
1890 ino_t target
= *inop
;
1892 if (node
->d_dir
.d_ino
== target
) {
1895 vget(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1898 devfs_allocv(&vp
, node
);
1907 * This function takes a cdev and removes its devfs node in the
1908 * given topology. The cdev remains intact.
1911 devfs_destroy_device_node(struct devfs_node
*root
, cdev_t target
)
1913 struct devfs_node
*node
, *parent
;
1916 char *create_path
= NULL
;
1920 name_buf
= kmalloc(PATH_MAX
, M_TEMP
, M_WAITOK
);
1921 ksnprintf(name_buf
, PATH_MAX
, "%s", target
->si_name
);
1923 devfs_resolve_name_path(target
->si_name
, name_buf
, &create_path
, &name
);
1926 parent
= devfs_resolve_or_create_path(root
, create_path
, 0);
1933 node
= devfs_find_device_node_by_name(parent
, name
);
1936 nanotime(&node
->parent
->mtime
);
1940 kfree(name_buf
, M_TEMP
);
1946 * Just set perms and ownership for given node.
1949 devfs_set_perms(struct devfs_node
*node
, uid_t uid
, gid_t gid
,
1950 u_short mode
, u_long flags
)
1960 * Propagates a device attach/detach to all mount
1961 * points. Also takes care of automatic alias removal
1962 * for a deleted cdev.
1965 devfs_propagate_dev(cdev_t dev
, int attach
)
1967 struct devfs_mnt_data
*mnt
;
1969 TAILQ_FOREACH(mnt
, &devfs_mnt_list
, link
) {
1971 /* Device is being attached */
1972 devfs_create_device_node(mnt
->root_node
, dev
,
1975 /* Device is being detached */
1976 devfs_alias_remove(dev
);
1977 devfs_destroy_device_node(mnt
->root_node
, dev
);
1984 * devfs_clone either returns a basename from a complete name by
1985 * returning the length of the name without trailing digits, or,
1986 * if clone != 0, calls the device's clone handler to get a new
1987 * device, which in turn is returned in devp.
1990 devfs_clone(cdev_t dev
, const char *name
, size_t len
, int mode
,
1994 struct devfs_clone_handler
*chandler
;
1995 struct dev_clone_args ap
;
1997 TAILQ_FOREACH(chandler
, &devfs_chandler_list
, link
) {
1998 if (chandler
->namlen
!= len
)
2000 if ((!memcmp(chandler
->name
, name
, len
)) && (chandler
->nhandler
)) {
2001 lockmgr(&devfs_lock
, LK_RELEASE
);
2003 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
2005 ap
.a_head
.a_dev
= dev
;
2011 error
= (chandler
->nhandler
)(&ap
);
2024 * Registers a new orphan in the orphan list.
2027 devfs_tracer_add_orphan(struct devfs_node
*node
)
2029 struct devfs_orphan
*orphan
;
2032 orphan
= kmalloc(sizeof(struct devfs_orphan
), M_DEVFS
, M_WAITOK
);
2033 orphan
->node
= node
;
2035 KKASSERT((node
->flags
& DEVFS_ORPHANED
) == 0);
2036 node
->flags
|= DEVFS_ORPHANED
;
2037 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node
->mp
), orphan
, link
);
2041 * Removes an orphan from the orphan list.
2044 devfs_tracer_del_orphan(struct devfs_node
*node
)
2046 struct devfs_orphan
*orphan
;
2050 TAILQ_FOREACH(orphan
, DEVFS_ORPHANLIST(node
->mp
), link
) {
2051 if (orphan
->node
== node
) {
2052 node
->flags
&= ~DEVFS_ORPHANED
;
2053 TAILQ_REMOVE(DEVFS_ORPHANLIST(node
->mp
), orphan
, link
);
2054 kfree(orphan
, M_DEVFS
);
2061 * Counts the orphans in the orphan list, and if cleanup
2062 * is specified, also frees the orphan and removes it from
2066 devfs_tracer_orphan_count(struct mount
*mp
, int cleanup
)
2068 struct devfs_orphan
*orphan
, *orphan2
;
2071 TAILQ_FOREACH_MUTABLE(orphan
, DEVFS_ORPHANLIST(mp
), link
, orphan2
) {
2074 * If we are instructed to clean up, we do so.
2077 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp
), orphan
, link
);
2078 orphan
->node
->flags
&= ~DEVFS_ORPHANED
;
2079 devfs_freep(orphan
->node
);
2080 kfree(orphan
, M_DEVFS
);
2088 * Fetch an ino_t from the global d_ino by increasing it
2092 devfs_fetch_ino(void)
2096 spin_lock_wr(&ino_lock
);
2098 spin_unlock_wr(&ino_lock
);
2104 * Allocates a new cdev and initializes it's most basic
2108 devfs_new_cdev(struct dev_ops
*ops
, int minor
, struct dev_ops
*bops
)
2110 cdev_t dev
= sysref_alloc(&cdev_sysref_class
);
2112 sysref_activate(&dev
->si_sysref
);
2114 bzero(dev
, offsetof(struct cdev
, si_sysref
));
2119 dev
->si_drv1
= NULL
;
2120 dev
->si_drv2
= NULL
;
2121 dev
->si_lastread
= 0; /* time_second */
2122 dev
->si_lastwrite
= 0; /* time_second */
2127 dev
->si_uminor
= minor
;
2128 dev
->si_bops
= bops
;
2129 /* If there is a backing device, we reference its ops */
2130 dev
->si_inode
= makeudev(
2131 devfs_reference_ops((bops
)?(bops
):(ops
)),
2138 devfs_cdev_terminate(cdev_t dev
)
2142 /* Check if it is locked already. if not, we acquire the devfs lock */
2143 if (!(lockstatus(&devfs_lock
, curthread
)) == LK_EXCLUSIVE
) {
2144 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
2148 /* Propagate destruction, just in case */
2149 devfs_propagate_dev(dev
, 0);
2151 /* If we acquired the lock, we also get rid of it */
2153 lockmgr(&devfs_lock
, LK_RELEASE
);
2155 /* If there is a backing device, we release the backing device's ops */
2156 devfs_release_ops((dev
->si_bops
)?(dev
->si_bops
):(dev
->si_ops
));
2158 /* Finally destroy the device */
2159 sysref_put(&dev
->si_sysref
);
2163 * Links a given cdev into the dev list.
2166 devfs_link_dev(cdev_t dev
)
2168 KKASSERT((dev
->si_flags
& SI_DEVFS_LINKED
) == 0);
2169 dev
->si_flags
|= SI_DEVFS_LINKED
;
2170 TAILQ_INSERT_TAIL(&devfs_dev_list
, dev
, link
);
2176 * Removes a given cdev from the dev list. The caller is responsible for
2177 * releasing the reference on the device associated with the linkage.
2179 * Returns EALREADY if the dev has already been unlinked.
2182 devfs_unlink_dev(cdev_t dev
)
2184 if ((dev
->si_flags
& SI_DEVFS_LINKED
)) {
2185 TAILQ_REMOVE(&devfs_dev_list
, dev
, link
);
2186 dev
->si_flags
&= ~SI_DEVFS_LINKED
;
2193 devfs_node_is_accessible(struct devfs_node
*node
)
2195 if ((node
) && (!(node
->flags
& DEVFS_HIDDEN
)))
2202 devfs_reference_ops(struct dev_ops
*ops
)
2205 struct devfs_dev_ops
*found
= NULL
;
2206 struct devfs_dev_ops
*devops
;
2208 TAILQ_FOREACH(devops
, &devfs_dev_ops_list
, link
) {
2209 if (devops
->ops
== ops
) {
2216 found
= kmalloc(sizeof(struct devfs_dev_ops
), M_DEVFS
, M_WAITOK
);
2218 found
->ref_count
= 0;
2219 TAILQ_INSERT_TAIL(&devfs_dev_ops_list
, found
, link
);
2224 if (found
->ref_count
== 0) {
2225 found
->id
= devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id
), 255);
2226 if (found
->id
== -1) {
2227 /* Ran out of unique ids */
2228 devfs_debug(DEVFS_DEBUG_WARNING
,
2229 "devfs_reference_ops: WARNING: ran out of unique ids\n");
2239 devfs_release_ops(struct dev_ops
*ops
)
2241 struct devfs_dev_ops
*found
= NULL
;
2242 struct devfs_dev_ops
*devops
;
2244 TAILQ_FOREACH(devops
, &devfs_dev_ops_list
, link
) {
2245 if (devops
->ops
== ops
) {
2255 if (found
->ref_count
== 0) {
2256 TAILQ_REMOVE(&devfs_dev_ops_list
, found
, link
);
2257 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id
), found
->id
);
2258 kfree(found
, M_DEVFS
);
2267 msg
= devfs_msg_get();
2268 msg
= devfs_msg_send_sync(DEVFS_SYNC
, msg
);
2273 * Called on init of devfs; creates the objcaches and
2274 * spawns off the devfs core thread. Also initializes
2280 devfs_debug(DEVFS_DEBUG_DEBUG
, "devfs_init() called\n");
2281 /* Create objcaches for nodes, msgs and devs */
2282 devfs_node_cache
= objcache_create("devfs-node-cache", 0, 0,
2284 objcache_malloc_alloc
,
2285 objcache_malloc_free
,
2286 &devfs_node_malloc_args
);
2288 devfs_msg_cache
= objcache_create("devfs-msg-cache", 0, 0,
2290 objcache_malloc_alloc
,
2291 objcache_malloc_free
,
2292 &devfs_msg_malloc_args
);
2294 devfs_dev_cache
= objcache_create("devfs-dev-cache", 0, 0,
2296 objcache_malloc_alloc
,
2297 objcache_malloc_free
,
2298 &devfs_dev_malloc_args
);
2300 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id
));
2302 /* Initialize the reply-only port which acts as a message drain */
2303 lwkt_initport_replyonly(&devfs_dispose_port
, devfs_msg_autofree_reply
);
2305 /* Initialize *THE* devfs lock */
2306 lockinit(&devfs_lock
, "devfs_core lock", 0, 0);
2309 lwkt_create(devfs_msg_core
, /*args*/NULL
, &td_core
, NULL
,
2310 0, 0, "devfs_msg_core");
2312 tsleep(td_core
/*devfs_id*/, 0, "devfsc", 0);
2314 devfs_debug(DEVFS_DEBUG_DEBUG
, "devfs_init finished\n");
2318 * Called on unload of devfs; takes care of destroying the core
2319 * and the objcaches. Also removes aliases that are no longer needed.
2324 devfs_debug(DEVFS_DEBUG_DEBUG
, "devfs_uninit() called\n");
2326 devfs_msg_send(DEVFS_TERMINATE_CORE
, NULL
);
2328 tsleep(td_core
/*devfs_id*/, 0, "devfsc", 0);
2329 tsleep(td_core
/*devfs_id*/, 0, "devfsc", 10000);
2331 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id
));
2333 /* Destroy the objcaches */
2334 objcache_destroy(devfs_msg_cache
);
2335 objcache_destroy(devfs_node_cache
);
2336 objcache_destroy(devfs_dev_cache
);
2342 * This is a sysctl handler to assist userland devname(3) to
2343 * find the device name for a given udev.
2346 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS
)
2353 if ((error
= SYSCTL_IN(req
, &udev
, sizeof(udev_t
))))
2356 devfs_debug(DEVFS_DEBUG_DEBUG
, "devfs sysctl, received udev: %d\n", udev
);
2361 if ((found
= devfs_find_device_by_udev(udev
)) == NULL
)
2364 return(SYSCTL_OUT(req
, found
->si_name
, strlen(found
->si_name
) + 1));
2368 SYSCTL_PROC(_kern
, OID_AUTO
, devname
, CTLTYPE_OPAQUE
|CTLFLAG_RW
|CTLFLAG_ANYBODY
,
2369 NULL
, 0, devfs_sysctl_devname_helper
, "", "helper for devname(3)");
2371 static SYSCTL_NODE(_vfs
, OID_AUTO
, devfs
, CTLFLAG_RW
, 0, "devfs");
2372 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable
);
2373 SYSCTL_INT(_vfs_devfs
, OID_AUTO
, debug
, CTLFLAG_RW
, &devfs_debug_enable
,
2374 0, "Enable DevFS debugging");
2376 SYSINIT(vfs_devfs_register
, SI_SUB_PRE_DRIVERS
, SI_ORDER_FIRST
,
2378 SYSUNINIT(vfs_devfs_register
, SI_SUB_PRE_DRIVERS
, SI_ORDER_ANY
,
2379 devfs_uninit
, NULL
);