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>
54 MALLOC_DEFINE(M_DEVFS, "devfs", "Device File System (devfs) allocations");
55 DEVFS_DECLARE_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 static struct lwkt_port devfs_dispose_port;
102 static struct lwkt_port devfs_msg_port;
103 static struct thread *td_core;
105 static struct spinlock ino_lock;
107 static int devfs_debug_enable;
108 static int devfs_run;
110 static ino_t devfs_fetch_ino(void);
111 static int devfs_create_all_dev_worker(struct devfs_node *);
112 static int devfs_create_dev_worker(cdev_t, uid_t, gid_t, int);
113 static int devfs_destroy_dev_worker(cdev_t);
114 static int devfs_destroy_subnames_worker(char *);
115 static int devfs_destroy_dev_by_ops_worker(struct dev_ops *, int);
116 static int devfs_propagate_dev(cdev_t, int);
117 static int devfs_unlink_dev(cdev_t dev);
118 static void devfs_msg_exec(devfs_msg_t msg);
120 static int devfs_chandler_add_worker(const char *, d_clone_t *);
121 static int devfs_chandler_del_worker(const char *);
123 static void devfs_msg_autofree_reply(lwkt_port_t, lwkt_msg_t);
124 static void devfs_msg_core(void *);
126 static int devfs_find_device_by_name_worker(devfs_msg_t);
127 static int devfs_find_device_by_udev_worker(devfs_msg_t);
129 static int devfs_apply_reset_rules_caller(char *, int);
131 static int devfs_scan_callback_worker(devfs_scan_t *, void *);
133 static struct devfs_node *devfs_resolve_or_create_dir(struct devfs_node *,
134 char *, size_t, int);
136 static int devfs_make_alias_worker(struct devfs_alias *);
137 static int devfs_alias_remove(cdev_t);
138 static int devfs_alias_reap(void);
139 static int devfs_alias_propagate(struct devfs_alias *);
140 static int devfs_alias_apply(struct devfs_node *, struct devfs_alias *);
141 static int devfs_alias_check_create(struct devfs_node *);
143 static int devfs_clr_subnames_flag_worker(char *, uint32_t);
144 static int devfs_destroy_subnames_without_flag_worker(char *, uint32_t);
146 static void *devfs_reaperp_callback(struct devfs_node *, void *);
147 static void *devfs_gc_dirs_callback(struct devfs_node *, void *);
148 static void *devfs_gc_links_callback(struct devfs_node *, struct devfs_node *);
150 devfs_inode_to_vnode_worker_callback(struct devfs_node *, ino_t *);
153 void (*devfs_node_added)(struct hotplug_device*) = NULL;
154 void (*devfs_node_removed)(struct hotplug_device*) = NULL;
157 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function
161 devfs_debug(int level, char *fmt, ...)
166 if (level <= devfs_debug_enable)
174 * devfs_allocp() Allocates a new devfs node with the specified
175 * parameters. The node is also automatically linked into the topology
176 * if a parent is specified. It also calls the rule and alias stuff to
177 * be applied on the new node
180 devfs_allocp(devfs_nodetype devfsnodetype, char *name,
181 struct devfs_node *parent, struct mount *mp, cdev_t dev)
183 struct devfs_node *node = NULL;
184 size_t namlen = strlen(name);
186 node = objcache_get(devfs_node_cache, M_WAITOK);
187 bzero(node, sizeof(*node));
189 atomic_add_long(&(DEVFS_MNTDATA(mp)->leak_count), 1);
194 node->d_dir.d_ino = devfs_fetch_ino();
197 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries
200 node->cookie_jar = 2;
203 * Access Control members
205 node->mode = DEVFS_DEFAULT_MODE;
206 node->uid = DEVFS_DEFAULT_UID;
207 node->gid = DEVFS_DEFAULT_GID;
209 switch (devfsnodetype) {
212 * Ensure that we don't recycle the root vnode by marking it as
213 * linked into the topology.
215 node->flags |= DEVFS_NODE_LINKED;
217 TAILQ_INIT(DEVFS_DENODE_HEAD(node));
218 node->d_dir.d_type = DT_DIR;
223 node->d_dir.d_type = DT_LNK;
227 node->d_dir.d_type = DT_REG;
232 node->d_dir.d_type = DT_CHR;
235 node->mode = dev->si_perms;
236 node->uid = dev->si_uid;
237 node->gid = dev->si_gid;
239 devfs_alias_check_create(node);
244 panic("devfs_allocp: unknown node type");
248 node->node_type = devfsnodetype;
250 /* Initialize the dirent structure of each devfs vnode */
251 node->d_dir.d_namlen = namlen;
252 node->d_dir.d_name = kmalloc(namlen+1, M_DEVFS, M_WAITOK);
253 memcpy(node->d_dir.d_name, name, namlen);
254 node->d_dir.d_name[namlen] = '\0';
256 /* Initialize the parent node element */
257 node->parent = parent;
260 devfs_rule_check_apply(node, NULL);
262 /* Initialize *time members */
263 nanotime(&node->atime);
264 node->mtime = node->ctime = node->atime;
267 * Associate with parent as last step, clean out namecache
270 if ((parent != NULL) &&
271 ((parent->node_type == Proot) || (parent->node_type == Pdir))) {
273 node->cookie = parent->cookie_jar++;
274 node->flags |= DEVFS_NODE_LINKED;
275 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent), node, link);
277 /* This forces negative namecache lookups to clear */
278 ++mp->mnt_namecache_gen;
281 ++DEVFS_MNTDATA(mp)->file_count;
287 * devfs_allocv() allocates a new vnode based on a devfs node.
290 devfs_allocv(struct vnode **vpp, struct devfs_node *node)
298 while ((vp = node->v_node) != NULL) {
299 error = vget(vp, LK_EXCLUSIVE);
300 if (error != ENOENT) {
306 if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0)
311 if (node->v_node != NULL) {
320 switch (node->node_type) {
322 vsetflags(vp, VROOT);
338 KKASSERT(node->d_dev);
340 vp->v_uminor = node->d_dev->si_uminor;
343 v_associate_rdev(vp, node->d_dev);
344 vp->v_ops = &node->mp->mnt_vn_spec_ops;
348 panic("devfs_allocv: unknown node type");
356 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
357 * based on the newly created devfs node.
360 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype,
361 char *name, struct devfs_node *parent, cdev_t dev)
363 struct devfs_node *node;
365 node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
368 devfs_allocv(vpp, node);
376 * Destroy the devfs_node. The node must be unlinked from the topology.
378 * This function will also destroy any vnode association with the node
381 * The cdev_t itself remains intact.
384 devfs_freep(struct devfs_node *node)
389 KKASSERT(((node->flags & DEVFS_NODE_LINKED) == 0) ||
390 (node->node_type == Proot));
393 * Protect against double frees
395 KKASSERT((node->flags & DEVFS_DESTROYED) == 0);
396 node->flags |= DEVFS_DESTROYED;
399 * Avoid deadlocks between devfs_lock and the vnode lock when
400 * disassociating the vnode (stress2 pty vs ls -la /dev/pts).
402 * This also prevents the vnode reclaim code from double-freeing
403 * the node. The vget() is required to safely modified the vp
404 * and cycle the refs to terminate an inactive vp.
406 lockmgr(&devfs_lock, LK_RELEASE);
408 while ((vp = node->v_node) != NULL) {
409 if (vget(vp, LK_EXCLUSIVE | LK_RETRY) != 0)
414 cache_inval_vp(vp, CINV_DESTROY);
417 lockmgr(&devfs_lock, LK_EXCLUSIVE);
422 atomic_subtract_long(&(DEVFS_MNTDATA(node->mp)->leak_count), 1);
423 if (node->symlink_name) {
424 kfree(node->symlink_name, M_DEVFS);
425 node->symlink_name = NULL;
429 * Remove the node from the orphan list if it is still on it.
431 if (node->flags & DEVFS_ORPHANED)
432 devfs_tracer_del_orphan(node);
434 if (node->d_dir.d_name) {
435 kfree(node->d_dir.d_name, M_DEVFS);
436 node->d_dir.d_name = NULL;
438 --DEVFS_MNTDATA(node->mp)->file_count;
439 objcache_put(devfs_node_cache, node);
445 * Unlink the devfs node from the topology and add it to the orphan list.
446 * The node will later be destroyed by freep.
448 * Any vnode association, including the v_rdev and v_data, remains intact
452 devfs_unlinkp(struct devfs_node *node)
454 struct devfs_node *parent;
455 struct hotplug_device *hpdev;
459 * Add the node to the orphan list, so it is referenced somewhere, to
460 * so we don't leak it.
462 devfs_tracer_add_orphan(node);
464 parent = node->parent;
467 * If the parent is known we can unlink the node out of the topology
470 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
472 node->flags &= ~DEVFS_NODE_LINKED;
474 /* hotplug handler */
475 if(devfs_node_removed) {
476 hpdev = kmalloc(sizeof(struct hotplug_device), M_TEMP, M_WAITOK);
477 hpdev->dev = node->d_dev;
479 hpdev->name = node->d_dev->si_name;
480 devfs_node_removed(hpdev);
481 kfree(hpdev, M_TEMP);
488 devfs_iterate_topology(struct devfs_node *node,
489 devfs_iterate_callback_t *callback, void *arg1)
491 struct devfs_node *node1, *node2;
494 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
495 if (node->nchildren > 2) {
496 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
498 if ((ret = devfs_iterate_topology(node1, callback, arg1)))
504 ret = callback(node, arg1);
509 * devfs_reaperp() is a recursive function that iterates through all the
510 * topology, unlinking and freeing all devfs nodes.
513 devfs_reaperp_callback(struct devfs_node *node, void *unused)
522 devfs_gc_dirs_callback(struct devfs_node *node, void *unused)
524 if (node->node_type == Pdir) {
525 if ((node->nchildren == 2) &&
526 !(node->flags & DEVFS_USER_CREATED)) {
536 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target)
538 if ((node->node_type == Plink) && (node->link_target == target)) {
547 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
548 * freeing a node, but also removes empty directories and links that link
549 * via devfs auto-link mechanism to the node being deleted.
552 devfs_gc(struct devfs_node *node)
554 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
556 if (node->nlinks > 0)
557 devfs_iterate_topology(root_node,
558 (devfs_iterate_callback_t *)devfs_gc_links_callback, node);
561 devfs_iterate_topology(root_node,
562 (devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL);
570 * devfs_create_dev() is the asynchronous entry point for device creation.
571 * It just sends a message with the relevant details to the devfs core.
573 * This function will reference the passed device. The reference is owned
574 * by devfs and represents all of the device's node associations.
577 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
580 devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
586 * devfs_destroy_dev() is the asynchronous entry point for device destruction.
587 * It just sends a message with the relevant details to the devfs core.
590 devfs_destroy_dev(cdev_t dev)
592 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
597 * devfs_mount_add() is the synchronous entry point for adding a new devfs
598 * mount. It sends a synchronous message with the relevant details to the
602 devfs_mount_add(struct devfs_mnt_data *mnt)
606 msg = devfs_msg_get();
608 msg = devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
615 * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
616 * It sends a synchronous message with the relevant details to the devfs core.
619 devfs_mount_del(struct devfs_mnt_data *mnt)
623 msg = devfs_msg_get();
625 msg = devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
632 * devfs_destroy_subnames() is the synchronous entry point for device
633 * destruction by subname. It just sends a message with the relevant details to
637 devfs_destroy_subnames(char *name)
641 msg = devfs_msg_get();
642 msg->mdv_load = name;
643 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES, msg);
649 devfs_clr_subnames_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_CLR_SUBNAMES_FLAG, msg);
663 devfs_destroy_subnames_without_flag(char *name, uint32_t flag)
667 msg = devfs_msg_get();
668 msg->mdv_flags.name = name;
669 msg->mdv_flags.flag = flag;
670 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES_WO_FLAG, msg);
677 * devfs_create_all_dev is the asynchronous entry point to trigger device
678 * node creation. It just sends a message with the relevant details to
682 devfs_create_all_dev(struct devfs_node *root)
684 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
689 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
690 * devices with a specific set of dev_ops and minor. It just sends a
691 * message with the relevant details to the devfs core.
694 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
696 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
701 * devfs_clone_handler_add is the synchronous entry point to add a new
702 * clone handler. It just sends a message with the relevant details to
706 devfs_clone_handler_add(const char *name, d_clone_t *nhandler)
710 msg = devfs_msg_get();
711 msg->mdv_chandler.name = name;
712 msg->mdv_chandler.nhandler = nhandler;
713 msg = devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg);
719 * devfs_clone_handler_del is the synchronous entry point to remove a
720 * clone handler. It just sends a message with the relevant details to
724 devfs_clone_handler_del(const char *name)
728 msg = devfs_msg_get();
729 msg->mdv_chandler.name = name;
730 msg->mdv_chandler.nhandler = NULL;
731 msg = devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg);
737 * devfs_find_device_by_name is the synchronous entry point to find a
738 * device given its name. It sends a synchronous message with the
739 * relevant details to the devfs core and returns the answer.
742 devfs_find_device_by_name(const char *fmt, ...)
753 kvasnrprintf(&target, PATH_MAX, 10, fmt, ap);
756 msg = devfs_msg_get();
757 msg->mdv_name = target;
758 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
759 found = msg->mdv_cdev;
767 * devfs_find_device_by_udev is the synchronous entry point to find a
768 * device given its udev number. It sends a synchronous message with
769 * the relevant details to the devfs core and returns the answer.
772 devfs_find_device_by_udev(udev_t udev)
777 msg = devfs_msg_get();
778 msg->mdv_udev = udev;
779 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg);
780 found = msg->mdv_cdev;
783 devfs_debug(DEVFS_DEBUG_DEBUG,
784 "devfs_find_device_by_udev found? %s -end:3-\n",
785 ((found) ? found->si_name:"NO"));
790 devfs_inode_to_vnode(struct mount *mp, ino_t target)
792 struct vnode *vp = NULL;
798 msg = devfs_msg_get();
799 msg->mdv_ino.mp = mp;
800 msg->mdv_ino.ino = target;
801 msg = devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg);
802 vp = msg->mdv_ino.vp;
803 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
810 * devfs_make_alias is the asynchronous entry point to register an alias
811 * for a device. It just sends a message with the relevant details to the
815 devfs_make_alias(const char *name, cdev_t dev_target)
817 struct devfs_alias *alias;
822 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
823 alias->name = kstrdup(name, M_DEVFS);
825 alias->dev_target = dev_target;
827 devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
832 * devfs_apply_rules is the asynchronous entry point to trigger application
833 * of all rules. It just sends a message with the relevant details to the
837 devfs_apply_rules(char *mntto)
841 new_name = kstrdup(mntto, M_DEVFS);
842 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
848 * devfs_reset_rules is the asynchronous entry point to trigger reset of all
849 * rules. It just sends a message with the relevant details to the devfs core.
852 devfs_reset_rules(char *mntto)
856 new_name = kstrdup(mntto, M_DEVFS);
857 devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
864 * devfs_scan_callback is the asynchronous entry point to call a callback
866 * It just sends a message with the relevant details to the devfs core.
869 devfs_scan_callback(devfs_scan_t *callback, void *arg)
873 KKASSERT(sizeof(callback) == sizeof(void *));
875 msg = devfs_msg_get();
876 msg->mdv_load = callback;
877 msg->mdv_load2 = arg;
878 msg = devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
886 * Acts as a message drain. Any message that is replied to here gets destroyed
887 * and the memory freed.
890 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
892 devfs_msg_put((devfs_msg_t)msg);
896 * devfs_msg_get allocates a new devfs msg and returns it.
901 return objcache_get(devfs_msg_cache, M_WAITOK);
905 * devfs_msg_put deallocates a given devfs msg.
908 devfs_msg_put(devfs_msg_t msg)
910 objcache_put(devfs_msg_cache, msg);
915 * devfs_msg_send is the generic asynchronous message sending facility
916 * for devfs. By default the reply port is the automatic disposal port.
918 * If the current thread is the devfs_msg_port thread we execute the
919 * operation synchronously.
922 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
924 lwkt_port_t port = &devfs_msg_port;
926 lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
928 devfs_msg->hdr.u.ms_result = cmd;
930 if (port->mpu_td == curthread) {
931 devfs_msg_exec(devfs_msg);
932 lwkt_replymsg(&devfs_msg->hdr, 0);
934 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
939 * devfs_msg_send_sync is the generic synchronous message sending
940 * facility for devfs. It initializes a local reply port and waits
941 * for the core's answer. This answer is then returned.
944 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
946 struct lwkt_port rep_port;
947 devfs_msg_t msg_incoming;
948 lwkt_port_t port = &devfs_msg_port;
950 lwkt_initport_thread(&rep_port, curthread);
951 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
953 devfs_msg->hdr.u.ms_result = cmd;
955 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
956 msg_incoming = lwkt_waitport(&rep_port, 0);
962 * sends a message with a generic argument.
965 devfs_msg_send_generic(uint32_t cmd, void *load)
967 devfs_msg_t devfs_msg = devfs_msg_get();
969 devfs_msg->mdv_load = load;
970 devfs_msg_send(cmd, devfs_msg);
974 * sends a message with a name argument.
977 devfs_msg_send_name(uint32_t cmd, char *name)
979 devfs_msg_t devfs_msg = devfs_msg_get();
981 devfs_msg->mdv_name = name;
982 devfs_msg_send(cmd, devfs_msg);
986 * sends a message with a mount argument.
989 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
991 devfs_msg_t devfs_msg = devfs_msg_get();
993 devfs_msg->mdv_mnt = mnt;
994 devfs_msg_send(cmd, devfs_msg);
998 * sends a message with an ops argument.
1001 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
1003 devfs_msg_t devfs_msg = devfs_msg_get();
1005 devfs_msg->mdv_ops.ops = ops;
1006 devfs_msg->mdv_ops.minor = minor;
1007 devfs_msg_send(cmd, devfs_msg);
1011 * sends a message with a clone handler argument.
1014 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
1016 devfs_msg_t devfs_msg = devfs_msg_get();
1018 devfs_msg->mdv_chandler.name = name;
1019 devfs_msg->mdv_chandler.nhandler = handler;
1020 devfs_msg_send(cmd, devfs_msg);
1024 * sends a message with a device argument.
1027 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1029 devfs_msg_t devfs_msg = devfs_msg_get();
1031 devfs_msg->mdv_dev.dev = dev;
1032 devfs_msg->mdv_dev.uid = uid;
1033 devfs_msg->mdv_dev.gid = gid;
1034 devfs_msg->mdv_dev.perms = perms;
1036 devfs_msg_send(cmd, devfs_msg);
1040 * sends a message with a link argument.
1043 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1045 devfs_msg_t devfs_msg = devfs_msg_get();
1047 devfs_msg->mdv_link.name = name;
1048 devfs_msg->mdv_link.target = target;
1049 devfs_msg->mdv_link.mp = mp;
1050 devfs_msg_send(cmd, devfs_msg);
1054 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1055 * and calls the relevant worker functions. By using messages it's assured
1056 * that events occur in the correct order.
1059 devfs_msg_core(void *arg)
1064 lwkt_initport_thread(&devfs_msg_port, curthread);
1068 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1069 devfs_debug(DEVFS_DEBUG_DEBUG,
1070 "devfs_msg_core, new msg: %x\n",
1071 (unsigned int)msg->hdr.u.ms_result);
1072 devfs_msg_exec(msg);
1073 lwkt_replymsg(&msg->hdr, 0);
1080 devfs_msg_exec(devfs_msg_t msg)
1082 struct devfs_mnt_data *mnt;
1083 struct devfs_node *node;
1087 * Acquire the devfs lock to ensure safety of all called functions
1089 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1091 switch (msg->hdr.u.ms_result) {
1092 case DEVFS_DEVICE_CREATE:
1093 dev = msg->mdv_dev.dev;
1094 devfs_create_dev_worker(dev,
1097 msg->mdv_dev.perms);
1099 case DEVFS_DEVICE_DESTROY:
1100 dev = msg->mdv_dev.dev;
1101 devfs_destroy_dev_worker(dev);
1103 case DEVFS_DESTROY_SUBNAMES:
1104 devfs_destroy_subnames_worker(msg->mdv_load);
1106 case DEVFS_DESTROY_DEV_BY_OPS:
1107 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1108 msg->mdv_ops.minor);
1110 case DEVFS_CREATE_ALL_DEV:
1111 node = (struct devfs_node *)msg->mdv_load;
1112 devfs_create_all_dev_worker(node);
1114 case DEVFS_MOUNT_ADD:
1116 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1117 devfs_create_all_dev_worker(mnt->root_node);
1119 case DEVFS_MOUNT_DEL:
1121 TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1122 devfs_iterate_topology(mnt->root_node, devfs_reaperp_callback,
1124 if (mnt->leak_count) {
1125 devfs_debug(DEVFS_DEBUG_SHOW,
1126 "Leaked %ld devfs_node elements!\n",
1130 case DEVFS_CHANDLER_ADD:
1131 devfs_chandler_add_worker(msg->mdv_chandler.name,
1132 msg->mdv_chandler.nhandler);
1134 case DEVFS_CHANDLER_DEL:
1135 devfs_chandler_del_worker(msg->mdv_chandler.name);
1137 case DEVFS_FIND_DEVICE_BY_NAME:
1138 devfs_find_device_by_name_worker(msg);
1140 case DEVFS_FIND_DEVICE_BY_UDEV:
1141 devfs_find_device_by_udev_worker(msg);
1143 case DEVFS_MAKE_ALIAS:
1144 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1146 case DEVFS_APPLY_RULES:
1147 devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1149 case DEVFS_RESET_RULES:
1150 devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1152 case DEVFS_SCAN_CALLBACK:
1153 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load,
1156 case DEVFS_CLR_SUBNAMES_FLAG:
1157 devfs_clr_subnames_flag_worker(msg->mdv_flags.name,
1158 msg->mdv_flags.flag);
1160 case DEVFS_DESTROY_SUBNAMES_WO_FLAG:
1161 devfs_destroy_subnames_without_flag_worker(msg->mdv_flags.name,
1162 msg->mdv_flags.flag);
1164 case DEVFS_INODE_TO_VNODE:
1165 msg->mdv_ino.vp = devfs_iterate_topology(
1166 DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node,
1167 (devfs_iterate_callback_t *)devfs_inode_to_vnode_worker_callback,
1170 case DEVFS_TERMINATE_CORE:
1176 devfs_debug(DEVFS_DEBUG_WARNING,
1177 "devfs_msg_core: unknown message "
1178 "received at core\n");
1181 lockmgr(&devfs_lock, LK_RELEASE);
1185 * Worker function to insert a new dev into the dev list and initialize its
1186 * permissions. It also calls devfs_propagate_dev which in turn propagates
1187 * the change to all mount points.
1189 * The passed dev is already referenced. This reference is eaten by this
1190 * function and represents the dev's linkage into devfs_dev_list.
1193 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1199 dev->si_perms = perms;
1201 devfs_link_dev(dev);
1202 devfs_propagate_dev(dev, 1);
1204 udev_event_attach(dev, NULL, 0);
1210 * Worker function to delete a dev from the dev list and free the cdev.
1211 * It also calls devfs_propagate_dev which in turn propagates the change
1212 * to all mount points.
1215 devfs_destroy_dev_worker(cdev_t dev)
1220 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1222 error = devfs_unlink_dev(dev);
1223 devfs_propagate_dev(dev, 0);
1225 udev_event_detach(dev, NULL, 0);
1228 release_dev(dev); /* link ref */
1236 * Worker function to destroy all devices with a certain basename.
1237 * Calls devfs_destroy_dev_worker for the actual destruction.
1240 devfs_destroy_subnames_worker(char *name)
1243 size_t len = strlen(name);
1245 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1246 if ((!strncmp(dev->si_name, name, len)) &&
1247 (dev->si_name[len] != '\0')) {
1248 devfs_destroy_dev_worker(dev);
1255 devfs_clr_subnames_flag_worker(char *name, uint32_t flag)
1258 size_t len = strlen(name);
1260 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1261 if ((!strncmp(dev->si_name, name, len)) &&
1262 (dev->si_name[len] != '\0')) {
1263 dev->si_flags &= ~flag;
1271 devfs_destroy_subnames_without_flag_worker(char *name, uint32_t flag)
1274 size_t len = strlen(name);
1276 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1277 if ((!strncmp(dev->si_name, name, len)) &&
1278 (dev->si_name[len] != '\0')) {
1279 if (!(dev->si_flags & flag)) {
1280 devfs_destroy_dev_worker(dev);
1289 * Worker function that creates all device nodes on top of a devfs
1293 devfs_create_all_dev_worker(struct devfs_node *root)
1299 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1300 devfs_create_device_node(root, dev, NULL, NULL);
1307 * Worker function that destroys all devices that match a specific
1308 * dev_ops and/or minor. If minor is less than 0, it is not matched
1309 * against. It also propagates all changes.
1312 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1318 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1319 if (dev->si_ops != ops)
1321 if ((minor < 0) || (dev->si_uminor == minor)) {
1322 devfs_destroy_dev_worker(dev);
1330 * Worker function that registers a new clone handler in devfs.
1333 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler)
1335 struct devfs_clone_handler *chandler = NULL;
1336 u_char len = strlen(name);
1341 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1342 if (chandler->namlen != len)
1345 if (!memcmp(chandler->name, name, len)) {
1346 /* Clonable basename already exists */
1351 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1352 chandler->name = kstrdup(name, M_DEVFS);
1353 chandler->namlen = len;
1354 chandler->nhandler = nhandler;
1356 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1361 * Worker function that removes a given clone handler from the
1362 * clone handler list.
1365 devfs_chandler_del_worker(const char *name)
1367 struct devfs_clone_handler *chandler, *chandler2;
1368 u_char len = strlen(name);
1373 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1374 if (chandler->namlen != len)
1376 if (memcmp(chandler->name, name, len))
1379 TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1380 kfree(chandler->name, M_DEVFS);
1381 kfree(chandler, M_DEVFS);
1389 * Worker function that finds a given device name and changes
1390 * the message received accordingly so that when replied to,
1391 * the answer is returned to the caller.
1394 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1396 struct devfs_alias *alias;
1398 cdev_t found = NULL;
1400 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1401 if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1406 if (found == NULL) {
1407 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1408 if (strcmp(devfs_msg->mdv_name, alias->name) == 0) {
1409 found = alias->dev_target;
1414 devfs_msg->mdv_cdev = found;
1420 * Worker function that finds a given device udev and changes
1421 * the message received accordingly so that when replied to,
1422 * the answer is returned to the caller.
1425 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg)
1428 cdev_t found = NULL;
1430 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1431 if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1436 devfs_msg->mdv_cdev = found;
1442 * Worker function that inserts a given alias into the
1443 * alias list, and propagates the alias to all mount
1447 devfs_make_alias_worker(struct devfs_alias *alias)
1449 struct devfs_alias *alias2;
1450 size_t len = strlen(alias->name);
1453 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1454 if (len != alias2->namlen)
1457 if (!memcmp(alias->name, alias2->name, len)) {
1465 * The alias doesn't exist yet, so we add it to the alias list
1467 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1468 devfs_alias_propagate(alias);
1469 udev_event_attach(alias->dev_target, alias->name, 1);
1471 devfs_debug(DEVFS_DEBUG_WARNING,
1472 "Warning: duplicate devfs_make_alias for %s\n",
1474 kfree(alias->name, M_DEVFS);
1475 kfree(alias, M_DEVFS);
1482 * Function that removes and frees all aliases.
1485 devfs_alias_reap(void)
1487 struct devfs_alias *alias, *alias2;
1489 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1490 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1491 kfree(alias, M_DEVFS);
1497 * Function that removes an alias matching a specific cdev and frees
1501 devfs_alias_remove(cdev_t dev)
1503 struct devfs_alias *alias, *alias2;
1505 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1506 if (alias->dev_target == dev) {
1507 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1508 udev_event_detach(alias->dev_target, alias->name, 1);
1509 kfree(alias, M_DEVFS);
1516 * This function propagates a new alias to all mount points.
1519 devfs_alias_propagate(struct devfs_alias *alias)
1521 struct devfs_mnt_data *mnt;
1523 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1524 devfs_alias_apply(mnt->root_node, alias);
1530 * This function is a recursive function iterating through
1531 * all device nodes in the topology and, if applicable,
1532 * creating the relevant alias for a device node.
1535 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1537 struct devfs_node *node1, *node2;
1539 KKASSERT(alias != NULL);
1541 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1542 if (node->nchildren > 2) {
1543 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1544 devfs_alias_apply(node1, alias);
1548 if (node->d_dev == alias->dev_target)
1549 devfs_alias_create(alias->name, node, 0);
1555 * This function checks if any alias possibly is applicable
1556 * to the given node. If so, the alias is created.
1559 devfs_alias_check_create(struct devfs_node *node)
1561 struct devfs_alias *alias;
1563 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1564 if (node->d_dev == alias->dev_target)
1565 devfs_alias_create(alias->name, node, 0);
1571 * This function creates an alias with a given name
1572 * linking to a given devfs node. It also increments
1573 * the link count on the target node.
1576 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based)
1578 struct mount *mp = target->mp;
1579 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1580 struct devfs_node *linknode;
1581 struct hotplug_device *hpdev;
1582 char *create_path = NULL;
1587 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1589 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1590 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1593 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1596 if (devfs_find_device_node_by_name(parent, name)) {
1597 devfs_debug(DEVFS_DEBUG_WARNING,
1598 "Node already exists: %s "
1599 "(devfs_make_alias_worker)!\n",
1605 linknode = devfs_allocp(Plink, name, parent, mp, NULL);
1606 if (linknode == NULL) {
1611 linknode->link_target = target;
1615 linknode->flags |= DEVFS_RULE_CREATED;
1618 /* hotplug handler */
1619 if(devfs_node_added) {
1620 hpdev = kmalloc(sizeof(struct hotplug_device), M_TEMP, M_WAITOK);
1621 hpdev->dev = target->d_dev;
1622 hpdev->name = name_orig;
1623 devfs_node_added(hpdev);
1624 kfree(hpdev, M_TEMP);
1626 kfree(name_buf, M_TEMP);
1631 * This function is called by the core and handles mount point
1632 * strings. It either calls the relevant worker (devfs_apply_
1633 * reset_rules_worker) on all mountpoints or only a specific
1637 devfs_apply_reset_rules_caller(char *mountto, int apply)
1639 struct devfs_mnt_data *mnt;
1641 if (mountto[0] == '*') {
1642 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1643 devfs_iterate_topology(mnt->root_node,
1644 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1648 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1649 if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) {
1650 devfs_iterate_topology(mnt->root_node,
1651 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1658 kfree(mountto, M_DEVFS);
1663 * This function calls a given callback function for
1664 * every dev node in the devfs dev list.
1667 devfs_scan_callback_worker(devfs_scan_t *callback, void *arg)
1671 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1679 * This function tries to resolve a given directory, or if not
1680 * found and creation requested, creates the given directory.
1682 static struct devfs_node *
1683 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1684 size_t name_len, int create)
1686 struct devfs_node *node, *found = NULL;
1688 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1689 if (name_len != node->d_dir.d_namlen)
1692 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1698 if ((found == NULL) && (create)) {
1699 found = devfs_allocp(Pdir, dir_name, parent, parent->mp, NULL);
1706 * This function tries to resolve a complete path. If creation is requested,
1707 * if a given part of the path cannot be resolved (because it doesn't exist),
1711 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1713 struct devfs_node *node = parent;
1720 buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1722 while (*path && idx < PATH_MAX - 1) {
1727 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1737 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1738 kfree (buf, M_TEMP);
1743 * Takes a full path and strips it into a directory path and a name.
1744 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
1745 * requires a working buffer with enough size to keep the whole
1749 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
1753 size_t len = strlen(fullpath) + 1;
1756 KKASSERT((fullpath != NULL) && (buf != NULL));
1757 KKASSERT((pathp != NULL) && (namep != NULL));
1759 memcpy(buf, fullpath, len);
1761 for (i = len-1; i>= 0; i--) {
1762 if (buf[i] == '/') {
1782 * This function creates a new devfs node for a given device. It can
1783 * handle a complete path as device name, and accordingly creates
1784 * the path and the final device node.
1786 * The reference count on the passed dev remains unchanged.
1789 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
1790 char *dev_name, char *path_fmt, ...)
1792 struct devfs_node *parent, *node = NULL;
1793 struct hotplug_device *hpdev;
1799 char *create_path = NULL;
1800 char *names = "pqrsPQRS";
1802 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1804 if (path_fmt != NULL) {
1805 __va_start(ap, path_fmt);
1806 kvasnrprintf(&path, PATH_MAX, 10, path_fmt, ap);
1810 parent = devfs_resolve_or_create_path(root, path, 1);
1813 devfs_resolve_name_path(
1814 ((dev_name == NULL) && (dev))?(dev->si_name):(dev_name),
1815 name_buf, &create_path, &name);
1818 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1821 if (devfs_find_device_node_by_name(parent, name)) {
1822 devfs_debug(DEVFS_DEBUG_WARNING, "devfs_create_device_node: "
1823 "DEVICE %s ALREADY EXISTS!!! Ignoring creation request.\n", name);
1827 node = devfs_allocp(Pdev, name, parent, parent->mp, dev);
1828 nanotime(&parent->mtime);
1831 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
1834 if ((dev) && (strlen(dev->si_name) >= 4) &&
1835 (!memcmp(dev->si_name, "ptm/", 4))) {
1836 node->parent->flags |= DEVFS_HIDDEN;
1837 node->flags |= DEVFS_HIDDEN;
1841 * Ugly pty magic, to tag pty devices as such and hide them if needed.
1843 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
1844 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1846 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
1848 for (i = 0; i < strlen(names); i++) {
1849 if (name[3] == names[i]) {
1855 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1857 /* hotplug handler */
1858 if(devfs_node_added) {
1859 hpdev = kmalloc(sizeof(struct hotplug_device), M_TEMP, M_WAITOK);
1860 hpdev->dev = node->d_dev;
1861 hpdev->name = node->d_dev->si_name;
1862 devfs_node_added(hpdev);
1863 kfree(hpdev, M_TEMP);
1867 kfree(name_buf, M_TEMP);
1873 * This function finds a given device node in the topology with a given
1877 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target)
1879 if ((node->node_type == Pdev) && (node->d_dev == target)) {
1887 * This function finds a device node in the given parent directory by its
1888 * name and returns it.
1891 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
1893 struct devfs_node *node, *found = NULL;
1894 size_t len = strlen(target);
1896 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1897 if (len != node->d_dir.d_namlen)
1900 if (!memcmp(node->d_dir.d_name, target, len)) {
1910 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop)
1912 struct vnode *vp = NULL;
1913 ino_t target = *inop;
1915 if (node->d_dir.d_ino == target) {
1918 vget(vp, LK_EXCLUSIVE | LK_RETRY);
1921 devfs_allocv(&vp, node);
1930 * This function takes a cdev and removes its devfs node in the
1931 * given topology. The cdev remains intact.
1934 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
1936 struct devfs_node *node, *parent;
1939 char *create_path = NULL;
1943 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1944 ksnprintf(name_buf, PATH_MAX, "%s", target->si_name);
1946 devfs_resolve_name_path(target->si_name, name_buf, &create_path, &name);
1949 parent = devfs_resolve_or_create_path(root, create_path, 0);
1953 if (parent == NULL) {
1954 kfree(name_buf, M_TEMP);
1958 node = devfs_find_device_node_by_name(parent, name);
1961 nanotime(&node->parent->mtime);
1965 kfree(name_buf, M_TEMP);
1971 * Just set perms and ownership for given node.
1974 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid,
1975 u_short mode, u_long flags)
1985 * Propagates a device attach/detach to all mount
1986 * points. Also takes care of automatic alias removal
1987 * for a deleted cdev.
1990 devfs_propagate_dev(cdev_t dev, int attach)
1992 struct devfs_mnt_data *mnt;
1994 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1996 /* Device is being attached */
1997 devfs_create_device_node(mnt->root_node, dev,
2000 /* Device is being detached */
2001 devfs_alias_remove(dev);
2002 devfs_destroy_device_node(mnt->root_node, dev);
2009 * devfs_clone either returns a basename from a complete name by
2010 * returning the length of the name without trailing digits, or,
2011 * if clone != 0, calls the device's clone handler to get a new
2012 * device, which in turn is returned in devp.
2015 devfs_clone(cdev_t dev, const char *name, size_t len, int mode,
2019 struct devfs_clone_handler *chandler;
2020 struct dev_clone_args ap;
2022 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
2023 if (chandler->namlen != len)
2025 if ((!memcmp(chandler->name, name, len)) && (chandler->nhandler)) {
2026 lockmgr(&devfs_lock, LK_RELEASE);
2028 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2030 ap.a_head.a_dev = dev;
2036 error = (chandler->nhandler)(&ap);
2049 * Registers a new orphan in the orphan list.
2052 devfs_tracer_add_orphan(struct devfs_node *node)
2054 struct devfs_orphan *orphan;
2057 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2058 orphan->node = node;
2060 KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2061 node->flags |= DEVFS_ORPHANED;
2062 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2066 * Removes an orphan from the orphan list.
2069 devfs_tracer_del_orphan(struct devfs_node *node)
2071 struct devfs_orphan *orphan;
2075 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) {
2076 if (orphan->node == node) {
2077 node->flags &= ~DEVFS_ORPHANED;
2078 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2079 kfree(orphan, M_DEVFS);
2086 * Counts the orphans in the orphan list, and if cleanup
2087 * is specified, also frees the orphan and removes it from
2091 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2093 struct devfs_orphan *orphan, *orphan2;
2096 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) {
2099 * If we are instructed to clean up, we do so.
2102 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2103 orphan->node->flags &= ~DEVFS_ORPHANED;
2104 devfs_freep(orphan->node);
2105 kfree(orphan, M_DEVFS);
2113 * Fetch an ino_t from the global d_ino by increasing it
2117 devfs_fetch_ino(void)
2121 spin_lock_wr(&ino_lock);
2123 spin_unlock_wr(&ino_lock);
2129 * Allocates a new cdev and initializes it's most basic
2133 devfs_new_cdev(struct dev_ops *ops, int minor, struct dev_ops *bops)
2135 cdev_t dev = sysref_alloc(&cdev_sysref_class);
2137 sysref_activate(&dev->si_sysref);
2139 bzero(dev, offsetof(struct cdev, si_sysref));
2144 dev->si_drv1 = NULL;
2145 dev->si_drv2 = NULL;
2146 dev->si_lastread = 0; /* time_second */
2147 dev->si_lastwrite = 0; /* time_second */
2149 dev->si_dict = NULL;
2153 dev->si_uminor = minor;
2154 dev->si_bops = bops;
2155 /* If there is a backing device, we reference its ops */
2156 dev->si_inode = makeudev(
2157 devfs_reference_ops((bops)?(bops):(ops)),
2164 devfs_cdev_terminate(cdev_t dev)
2168 /* Check if it is locked already. if not, we acquire the devfs lock */
2169 if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) {
2170 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2174 /* Propagate destruction, just in case */
2175 devfs_propagate_dev(dev, 0);
2177 /* If we acquired the lock, we also get rid of it */
2179 lockmgr(&devfs_lock, LK_RELEASE);
2181 /* If there is a backing device, we release the backing device's ops */
2182 devfs_release_ops((dev->si_bops)?(dev->si_bops):(dev->si_ops));
2184 /* Finally destroy the device */
2185 sysref_put(&dev->si_sysref);
2189 * Dummies for now (individual locks for MPSAFE)
2192 devfs_cdev_lock(cdev_t dev)
2197 devfs_cdev_unlock(cdev_t dev)
2202 * Links a given cdev into the dev list.
2205 devfs_link_dev(cdev_t dev)
2207 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2208 dev->si_flags |= SI_DEVFS_LINKED;
2209 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2215 * Removes a given cdev from the dev list. The caller is responsible for
2216 * releasing the reference on the device associated with the linkage.
2218 * Returns EALREADY if the dev has already been unlinked.
2221 devfs_unlink_dev(cdev_t dev)
2223 if ((dev->si_flags & SI_DEVFS_LINKED)) {
2224 TAILQ_REMOVE(&devfs_dev_list, dev, link);
2225 dev->si_flags &= ~SI_DEVFS_LINKED;
2232 devfs_node_is_accessible(struct devfs_node *node)
2234 if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2241 devfs_reference_ops(struct dev_ops *ops)
2244 struct devfs_dev_ops *found = NULL;
2245 struct devfs_dev_ops *devops;
2247 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2248 if (devops->ops == ops) {
2255 found = kmalloc(sizeof(struct devfs_dev_ops), M_DEVFS, M_WAITOK);
2257 found->ref_count = 0;
2258 TAILQ_INSERT_TAIL(&devfs_dev_ops_list, found, link);
2263 if (found->ref_count == 0) {
2264 found->id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255);
2265 if (found->id == -1) {
2266 /* Ran out of unique ids */
2267 devfs_debug(DEVFS_DEBUG_WARNING,
2268 "devfs_reference_ops: WARNING: ran out of unique ids\n");
2278 devfs_release_ops(struct dev_ops *ops)
2280 struct devfs_dev_ops *found = NULL;
2281 struct devfs_dev_ops *devops;
2283 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2284 if (devops->ops == ops) {
2294 if (found->ref_count == 0) {
2295 TAILQ_REMOVE(&devfs_dev_ops_list, found, link);
2296 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), found->id);
2297 kfree(found, M_DEVFS);
2302 * Wait for asynchronous messages to complete in the devfs helper
2303 * thread, then return. Do nothing if the helper thread is dead
2312 msg = devfs_msg_get();
2313 msg = devfs_msg_send_sync(DEVFS_SYNC, msg);
2319 * Called on init of devfs; creates the objcaches and
2320 * spawns off the devfs core thread. Also initializes
2326 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2327 /* Create objcaches for nodes, msgs and devs */
2328 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2330 objcache_malloc_alloc,
2331 objcache_malloc_free,
2332 &devfs_node_malloc_args );
2334 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2336 objcache_malloc_alloc,
2337 objcache_malloc_free,
2338 &devfs_msg_malloc_args );
2340 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2342 objcache_malloc_alloc,
2343 objcache_malloc_free,
2344 &devfs_dev_malloc_args );
2346 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2348 /* Initialize the reply-only port which acts as a message drain */
2349 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2351 /* Initialize *THE* devfs lock */
2352 lockinit(&devfs_lock, "devfs_core lock", 0, 0);
2355 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2356 0, 0, "devfs_msg_core");
2358 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2360 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2364 * Called on unload of devfs; takes care of destroying the core
2365 * and the objcaches. Also removes aliases that are no longer needed.
2370 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2372 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2374 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2375 tsleep(td_core/*devfs_id*/, 0, "devfsc", 10000);
2377 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2379 /* Destroy the objcaches */
2380 objcache_destroy(devfs_msg_cache);
2381 objcache_destroy(devfs_node_cache);
2382 objcache_destroy(devfs_dev_cache);
2388 * This is a sysctl handler to assist userland devname(3) to
2389 * find the device name for a given udev.
2392 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2399 if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t))))
2402 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev);
2407 if ((found = devfs_find_device_by_udev(udev)) == NULL)
2410 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2414 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
2415 NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)");
2417 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2418 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2419 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable,
2420 0, "Enable DevFS debugging");
2422 SYSINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST,
2424 SYSUNINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY,
2425 devfs_uninit, NULL);