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));
391 KKASSERT((node->flags & DEVFS_DESTROYED) == 0);
393 atomic_subtract_long(&(DEVFS_MNTDATA(node->mp)->leak_count), 1);
394 if (node->symlink_name) {
395 kfree(node->symlink_name, M_DEVFS);
396 node->symlink_name = NULL;
400 * Remove the node from the orphan list if it is still on it.
402 if (node->flags & DEVFS_ORPHANED)
403 devfs_tracer_del_orphan(node);
406 * Disassociate the vnode from the node. This also prevents the
407 * vnode's reclaim code from double-freeing the node.
409 * The vget is needed to safely modify the vp. It also serves
410 * to cycle the refs and terminate the vnode if it happens to
411 * be inactive, otherwise namecache references may not get cleared.
413 while ((vp = node->v_node) != NULL) {
414 if (vget(vp, LK_EXCLUSIVE | LK_RETRY) != 0)
419 cache_inval_vp(vp, CINV_DESTROY);
422 if (node->d_dir.d_name) {
423 kfree(node->d_dir.d_name, M_DEVFS);
424 node->d_dir.d_name = NULL;
426 node->flags |= DEVFS_DESTROYED;
428 --DEVFS_MNTDATA(node->mp)->file_count;
430 objcache_put(devfs_node_cache, node);
436 * Unlink the devfs node from the topology and add it to the orphan list.
437 * The node will later be destroyed by freep.
439 * Any vnode association, including the v_rdev and v_data, remains intact
443 devfs_unlinkp(struct devfs_node *node)
445 struct devfs_node *parent;
446 struct hotplug_device *hpdev;
450 * Add the node to the orphan list, so it is referenced somewhere, to
451 * so we don't leak it.
453 devfs_tracer_add_orphan(node);
455 parent = node->parent;
458 * If the parent is known we can unlink the node out of the topology
461 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
463 node->flags &= ~DEVFS_NODE_LINKED;
465 /* hotplug handler */
466 if(devfs_node_removed) {
467 hpdev = kmalloc(sizeof(struct hotplug_device), M_TEMP, M_WAITOK);
468 hpdev->dev = node->d_dev;
470 hpdev->name = node->d_dev->si_name;
471 devfs_node_removed(hpdev);
472 kfree(hpdev, M_TEMP);
479 devfs_iterate_topology(struct devfs_node *node,
480 devfs_iterate_callback_t *callback, void *arg1)
482 struct devfs_node *node1, *node2;
485 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
486 if (node->nchildren > 2) {
487 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
489 if ((ret = devfs_iterate_topology(node1, callback, arg1)))
495 ret = callback(node, arg1);
500 * devfs_reaperp() is a recursive function that iterates through all the
501 * topology, unlinking and freeing all devfs nodes.
504 devfs_reaperp_callback(struct devfs_node *node, void *unused)
513 devfs_gc_dirs_callback(struct devfs_node *node, void *unused)
515 if (node->node_type == Pdir) {
516 if ((node->nchildren == 2) &&
517 !(node->flags & DEVFS_USER_CREATED)) {
527 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target)
529 if ((node->node_type == Plink) && (node->link_target == target)) {
538 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
539 * freeing a node, but also removes empty directories and links that link
540 * via devfs auto-link mechanism to the node being deleted.
543 devfs_gc(struct devfs_node *node)
545 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
547 if (node->nlinks > 0)
548 devfs_iterate_topology(root_node,
549 (devfs_iterate_callback_t *)devfs_gc_links_callback, node);
552 devfs_iterate_topology(root_node,
553 (devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL);
561 * devfs_create_dev() is the asynchronous entry point for device creation.
562 * It just sends a message with the relevant details to the devfs core.
564 * This function will reference the passed device. The reference is owned
565 * by devfs and represents all of the device's node associations.
568 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
571 devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
577 * devfs_destroy_dev() is the asynchronous entry point for device destruction.
578 * It just sends a message with the relevant details to the devfs core.
581 devfs_destroy_dev(cdev_t dev)
583 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
588 * devfs_mount_add() is the synchronous entry point for adding a new devfs
589 * mount. It sends a synchronous message with the relevant details to the
593 devfs_mount_add(struct devfs_mnt_data *mnt)
597 msg = devfs_msg_get();
599 msg = devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
606 * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
607 * It sends a synchronous message with the relevant details to the devfs core.
610 devfs_mount_del(struct devfs_mnt_data *mnt)
614 msg = devfs_msg_get();
616 msg = devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
623 * devfs_destroy_subnames() is the synchronous entry point for device
624 * destruction by subname. It just sends a message with the relevant details to
628 devfs_destroy_subnames(char *name)
632 msg = devfs_msg_get();
633 msg->mdv_load = name;
634 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES, msg);
640 devfs_clr_subnames_flag(char *name, uint32_t flag)
644 msg = devfs_msg_get();
645 msg->mdv_flags.name = name;
646 msg->mdv_flags.flag = flag;
647 msg = devfs_msg_send_sync(DEVFS_CLR_SUBNAMES_FLAG, msg);
654 devfs_destroy_subnames_without_flag(char *name, uint32_t flag)
658 msg = devfs_msg_get();
659 msg->mdv_flags.name = name;
660 msg->mdv_flags.flag = flag;
661 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES_WO_FLAG, msg);
668 * devfs_create_all_dev is the asynchronous entry point to trigger device
669 * node creation. It just sends a message with the relevant details to
673 devfs_create_all_dev(struct devfs_node *root)
675 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
680 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
681 * devices with a specific set of dev_ops and minor. It just sends a
682 * message with the relevant details to the devfs core.
685 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
687 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
692 * devfs_clone_handler_add is the synchronous entry point to add a new
693 * clone handler. It just sends a message with the relevant details to
697 devfs_clone_handler_add(const char *name, d_clone_t *nhandler)
701 msg = devfs_msg_get();
702 msg->mdv_chandler.name = name;
703 msg->mdv_chandler.nhandler = nhandler;
704 msg = devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg);
710 * devfs_clone_handler_del is the synchronous entry point to remove a
711 * clone handler. It just sends a message with the relevant details to
715 devfs_clone_handler_del(const char *name)
719 msg = devfs_msg_get();
720 msg->mdv_chandler.name = name;
721 msg->mdv_chandler.nhandler = NULL;
722 msg = devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg);
728 * devfs_find_device_by_name is the synchronous entry point to find a
729 * device given its name. It sends a synchronous message with the
730 * relevant details to the devfs core and returns the answer.
733 devfs_find_device_by_name(const char *fmt, ...)
744 kvasnrprintf(&target, PATH_MAX, 10, fmt, ap);
747 msg = devfs_msg_get();
748 msg->mdv_name = target;
749 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
750 found = msg->mdv_cdev;
758 * devfs_find_device_by_udev is the synchronous entry point to find a
759 * device given its udev number. It sends a synchronous message with
760 * the relevant details to the devfs core and returns the answer.
763 devfs_find_device_by_udev(udev_t udev)
768 msg = devfs_msg_get();
769 msg->mdv_udev = udev;
770 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg);
771 found = msg->mdv_cdev;
774 devfs_debug(DEVFS_DEBUG_DEBUG,
775 "devfs_find_device_by_udev found? %s -end:3-\n",
776 ((found) ? found->si_name:"NO"));
781 devfs_inode_to_vnode(struct mount *mp, ino_t target)
783 struct vnode *vp = NULL;
789 msg = devfs_msg_get();
790 msg->mdv_ino.mp = mp;
791 msg->mdv_ino.ino = target;
792 msg = devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg);
793 vp = msg->mdv_ino.vp;
794 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
801 * devfs_make_alias is the asynchronous entry point to register an alias
802 * for a device. It just sends a message with the relevant details to the
806 devfs_make_alias(const char *name, cdev_t dev_target)
808 struct devfs_alias *alias;
813 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
814 alias->name = kstrdup(name, M_DEVFS);
816 alias->dev_target = dev_target;
818 devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
823 * devfs_apply_rules is the asynchronous entry point to trigger application
824 * of all rules. It just sends a message with the relevant details to the
828 devfs_apply_rules(char *mntto)
832 new_name = kstrdup(mntto, M_DEVFS);
833 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
839 * devfs_reset_rules is the asynchronous entry point to trigger reset of all
840 * rules. It just sends a message with the relevant details to the devfs core.
843 devfs_reset_rules(char *mntto)
847 new_name = kstrdup(mntto, M_DEVFS);
848 devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
855 * devfs_scan_callback is the asynchronous entry point to call a callback
857 * It just sends a message with the relevant details to the devfs core.
860 devfs_scan_callback(devfs_scan_t *callback, void *arg)
864 KKASSERT(sizeof(callback) == sizeof(void *));
866 msg = devfs_msg_get();
867 msg->mdv_load = callback;
868 msg->mdv_load2 = arg;
869 msg = devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
877 * Acts as a message drain. Any message that is replied to here gets destroyed
878 * and the memory freed.
881 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
883 devfs_msg_put((devfs_msg_t)msg);
887 * devfs_msg_get allocates a new devfs msg and returns it.
892 return objcache_get(devfs_msg_cache, M_WAITOK);
896 * devfs_msg_put deallocates a given devfs msg.
899 devfs_msg_put(devfs_msg_t msg)
901 objcache_put(devfs_msg_cache, msg);
906 * devfs_msg_send is the generic asynchronous message sending facility
907 * for devfs. By default the reply port is the automatic disposal port.
909 * If the current thread is the devfs_msg_port thread we execute the
910 * operation synchronously.
913 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
915 lwkt_port_t port = &devfs_msg_port;
917 lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
919 devfs_msg->hdr.u.ms_result = cmd;
921 if (port->mpu_td == curthread) {
922 devfs_msg_exec(devfs_msg);
923 lwkt_replymsg(&devfs_msg->hdr, 0);
925 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
930 * devfs_msg_send_sync is the generic synchronous message sending
931 * facility for devfs. It initializes a local reply port and waits
932 * for the core's answer. This answer is then returned.
935 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
937 struct lwkt_port rep_port;
938 devfs_msg_t msg_incoming;
939 lwkt_port_t port = &devfs_msg_port;
941 lwkt_initport_thread(&rep_port, curthread);
942 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
944 devfs_msg->hdr.u.ms_result = cmd;
946 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
947 msg_incoming = lwkt_waitport(&rep_port, 0);
953 * sends a message with a generic argument.
956 devfs_msg_send_generic(uint32_t cmd, void *load)
958 devfs_msg_t devfs_msg = devfs_msg_get();
960 devfs_msg->mdv_load = load;
961 devfs_msg_send(cmd, devfs_msg);
965 * sends a message with a name argument.
968 devfs_msg_send_name(uint32_t cmd, char *name)
970 devfs_msg_t devfs_msg = devfs_msg_get();
972 devfs_msg->mdv_name = name;
973 devfs_msg_send(cmd, devfs_msg);
977 * sends a message with a mount argument.
980 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
982 devfs_msg_t devfs_msg = devfs_msg_get();
984 devfs_msg->mdv_mnt = mnt;
985 devfs_msg_send(cmd, devfs_msg);
989 * sends a message with an ops argument.
992 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
994 devfs_msg_t devfs_msg = devfs_msg_get();
996 devfs_msg->mdv_ops.ops = ops;
997 devfs_msg->mdv_ops.minor = minor;
998 devfs_msg_send(cmd, devfs_msg);
1002 * sends a message with a clone handler argument.
1005 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
1007 devfs_msg_t devfs_msg = devfs_msg_get();
1009 devfs_msg->mdv_chandler.name = name;
1010 devfs_msg->mdv_chandler.nhandler = handler;
1011 devfs_msg_send(cmd, devfs_msg);
1015 * sends a message with a device argument.
1018 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1020 devfs_msg_t devfs_msg = devfs_msg_get();
1022 devfs_msg->mdv_dev.dev = dev;
1023 devfs_msg->mdv_dev.uid = uid;
1024 devfs_msg->mdv_dev.gid = gid;
1025 devfs_msg->mdv_dev.perms = perms;
1027 devfs_msg_send(cmd, devfs_msg);
1031 * sends a message with a link argument.
1034 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1036 devfs_msg_t devfs_msg = devfs_msg_get();
1038 devfs_msg->mdv_link.name = name;
1039 devfs_msg->mdv_link.target = target;
1040 devfs_msg->mdv_link.mp = mp;
1041 devfs_msg_send(cmd, devfs_msg);
1045 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1046 * and calls the relevant worker functions. By using messages it's assured
1047 * that events occur in the correct order.
1050 devfs_msg_core(void *arg)
1055 lwkt_initport_thread(&devfs_msg_port, curthread);
1059 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1060 devfs_debug(DEVFS_DEBUG_DEBUG,
1061 "devfs_msg_core, new msg: %x\n",
1062 (unsigned int)msg->hdr.u.ms_result);
1063 devfs_msg_exec(msg);
1064 lwkt_replymsg(&msg->hdr, 0);
1071 devfs_msg_exec(devfs_msg_t msg)
1073 struct devfs_mnt_data *mnt;
1074 struct devfs_node *node;
1078 * Acquire the devfs lock to ensure safety of all called functions
1080 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1082 switch (msg->hdr.u.ms_result) {
1083 case DEVFS_DEVICE_CREATE:
1084 dev = msg->mdv_dev.dev;
1085 devfs_create_dev_worker(dev,
1088 msg->mdv_dev.perms);
1090 case DEVFS_DEVICE_DESTROY:
1091 dev = msg->mdv_dev.dev;
1092 devfs_destroy_dev_worker(dev);
1094 case DEVFS_DESTROY_SUBNAMES:
1095 devfs_destroy_subnames_worker(msg->mdv_load);
1097 case DEVFS_DESTROY_DEV_BY_OPS:
1098 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1099 msg->mdv_ops.minor);
1101 case DEVFS_CREATE_ALL_DEV:
1102 node = (struct devfs_node *)msg->mdv_load;
1103 devfs_create_all_dev_worker(node);
1105 case DEVFS_MOUNT_ADD:
1107 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1108 devfs_create_all_dev_worker(mnt->root_node);
1110 case DEVFS_MOUNT_DEL:
1112 TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1113 devfs_iterate_topology(mnt->root_node, devfs_reaperp_callback,
1115 if (mnt->leak_count) {
1116 devfs_debug(DEVFS_DEBUG_SHOW,
1117 "Leaked %ld devfs_node elements!\n",
1121 case DEVFS_CHANDLER_ADD:
1122 devfs_chandler_add_worker(msg->mdv_chandler.name,
1123 msg->mdv_chandler.nhandler);
1125 case DEVFS_CHANDLER_DEL:
1126 devfs_chandler_del_worker(msg->mdv_chandler.name);
1128 case DEVFS_FIND_DEVICE_BY_NAME:
1129 devfs_find_device_by_name_worker(msg);
1131 case DEVFS_FIND_DEVICE_BY_UDEV:
1132 devfs_find_device_by_udev_worker(msg);
1134 case DEVFS_MAKE_ALIAS:
1135 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1137 case DEVFS_APPLY_RULES:
1138 devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1140 case DEVFS_RESET_RULES:
1141 devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1143 case DEVFS_SCAN_CALLBACK:
1144 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load,
1147 case DEVFS_CLR_SUBNAMES_FLAG:
1148 devfs_clr_subnames_flag_worker(msg->mdv_flags.name,
1149 msg->mdv_flags.flag);
1151 case DEVFS_DESTROY_SUBNAMES_WO_FLAG:
1152 devfs_destroy_subnames_without_flag_worker(msg->mdv_flags.name,
1153 msg->mdv_flags.flag);
1155 case DEVFS_INODE_TO_VNODE:
1156 msg->mdv_ino.vp = devfs_iterate_topology(
1157 DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node,
1158 (devfs_iterate_callback_t *)devfs_inode_to_vnode_worker_callback,
1161 case DEVFS_TERMINATE_CORE:
1167 devfs_debug(DEVFS_DEBUG_WARNING,
1168 "devfs_msg_core: unknown message "
1169 "received at core\n");
1172 lockmgr(&devfs_lock, LK_RELEASE);
1176 * Worker function to insert a new dev into the dev list and initialize its
1177 * permissions. It also calls devfs_propagate_dev which in turn propagates
1178 * the change to all mount points.
1180 * The passed dev is already referenced. This reference is eaten by this
1181 * function and represents the dev's linkage into devfs_dev_list.
1184 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1190 dev->si_perms = perms;
1192 devfs_link_dev(dev);
1193 devfs_propagate_dev(dev, 1);
1195 udev_event_attach(dev, NULL, 0);
1201 * Worker function to delete a dev from the dev list and free the cdev.
1202 * It also calls devfs_propagate_dev which in turn propagates the change
1203 * to all mount points.
1206 devfs_destroy_dev_worker(cdev_t dev)
1211 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1213 error = devfs_unlink_dev(dev);
1214 devfs_propagate_dev(dev, 0);
1216 udev_event_detach(dev, NULL, 0);
1219 release_dev(dev); /* link ref */
1227 * Worker function to destroy all devices with a certain basename.
1228 * Calls devfs_destroy_dev_worker for the actual destruction.
1231 devfs_destroy_subnames_worker(char *name)
1234 size_t len = strlen(name);
1236 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1237 if ((!strncmp(dev->si_name, name, len)) &&
1238 (dev->si_name[len] != '\0')) {
1239 devfs_destroy_dev_worker(dev);
1246 devfs_clr_subnames_flag_worker(char *name, uint32_t flag)
1249 size_t len = strlen(name);
1251 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1252 if ((!strncmp(dev->si_name, name, len)) &&
1253 (dev->si_name[len] != '\0')) {
1254 dev->si_flags &= ~flag;
1262 devfs_destroy_subnames_without_flag_worker(char *name, uint32_t flag)
1265 size_t len = strlen(name);
1267 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1268 if ((!strncmp(dev->si_name, name, len)) &&
1269 (dev->si_name[len] != '\0')) {
1270 if (!(dev->si_flags & flag)) {
1271 devfs_destroy_dev_worker(dev);
1280 * Worker function that creates all device nodes on top of a devfs
1284 devfs_create_all_dev_worker(struct devfs_node *root)
1290 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1291 devfs_create_device_node(root, dev, NULL, NULL);
1298 * Worker function that destroys all devices that match a specific
1299 * dev_ops and/or minor. If minor is less than 0, it is not matched
1300 * against. It also propagates all changes.
1303 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1309 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1310 if (dev->si_ops != ops)
1312 if ((minor < 0) || (dev->si_uminor == minor)) {
1313 devfs_destroy_dev_worker(dev);
1321 * Worker function that registers a new clone handler in devfs.
1324 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler)
1326 struct devfs_clone_handler *chandler = NULL;
1327 u_char len = strlen(name);
1332 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1333 if (chandler->namlen != len)
1336 if (!memcmp(chandler->name, name, len)) {
1337 /* Clonable basename already exists */
1342 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1343 chandler->name = kstrdup(name, M_DEVFS);
1344 chandler->namlen = len;
1345 chandler->nhandler = nhandler;
1347 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1352 * Worker function that removes a given clone handler from the
1353 * clone handler list.
1356 devfs_chandler_del_worker(const char *name)
1358 struct devfs_clone_handler *chandler, *chandler2;
1359 u_char len = strlen(name);
1364 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1365 if (chandler->namlen != len)
1367 if (memcmp(chandler->name, name, len))
1370 TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1371 kfree(chandler->name, M_DEVFS);
1372 kfree(chandler, M_DEVFS);
1380 * Worker function that finds a given device name and changes
1381 * the message received accordingly so that when replied to,
1382 * the answer is returned to the caller.
1385 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1387 struct devfs_alias *alias;
1389 cdev_t found = NULL;
1391 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1392 if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1397 if (found == NULL) {
1398 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1399 if (strcmp(devfs_msg->mdv_name, alias->name) == 0) {
1400 found = alias->dev_target;
1405 devfs_msg->mdv_cdev = found;
1411 * Worker function that finds a given device udev and changes
1412 * the message received accordingly so that when replied to,
1413 * the answer is returned to the caller.
1416 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg)
1419 cdev_t found = NULL;
1421 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1422 if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1427 devfs_msg->mdv_cdev = found;
1433 * Worker function that inserts a given alias into the
1434 * alias list, and propagates the alias to all mount
1438 devfs_make_alias_worker(struct devfs_alias *alias)
1440 struct devfs_alias *alias2;
1441 size_t len = strlen(alias->name);
1444 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1445 if (len != alias2->namlen)
1448 if (!memcmp(alias->name, alias2->name, len)) {
1456 * The alias doesn't exist yet, so we add it to the alias list
1458 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1459 devfs_alias_propagate(alias);
1460 udev_event_attach(alias->dev_target, alias->name, 1);
1462 devfs_debug(DEVFS_DEBUG_WARNING,
1463 "Warning: duplicate devfs_make_alias for %s\n",
1465 kfree(alias->name, M_DEVFS);
1466 kfree(alias, M_DEVFS);
1473 * Function that removes and frees all aliases.
1476 devfs_alias_reap(void)
1478 struct devfs_alias *alias, *alias2;
1480 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1481 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1482 kfree(alias, M_DEVFS);
1488 * Function that removes an alias matching a specific cdev and frees
1492 devfs_alias_remove(cdev_t dev)
1494 struct devfs_alias *alias, *alias2;
1496 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1497 if (alias->dev_target == dev) {
1498 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1499 udev_event_detach(alias->dev_target, alias->name, 1);
1500 kfree(alias, M_DEVFS);
1507 * This function propagates a new alias to all mount points.
1510 devfs_alias_propagate(struct devfs_alias *alias)
1512 struct devfs_mnt_data *mnt;
1514 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1515 devfs_alias_apply(mnt->root_node, alias);
1521 * This function is a recursive function iterating through
1522 * all device nodes in the topology and, if applicable,
1523 * creating the relevant alias for a device node.
1526 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1528 struct devfs_node *node1, *node2;
1530 KKASSERT(alias != NULL);
1532 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1533 if (node->nchildren > 2) {
1534 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1535 devfs_alias_apply(node1, alias);
1539 if (node->d_dev == alias->dev_target)
1540 devfs_alias_create(alias->name, node, 0);
1546 * This function checks if any alias possibly is applicable
1547 * to the given node. If so, the alias is created.
1550 devfs_alias_check_create(struct devfs_node *node)
1552 struct devfs_alias *alias;
1554 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1555 if (node->d_dev == alias->dev_target)
1556 devfs_alias_create(alias->name, node, 0);
1562 * This function creates an alias with a given name
1563 * linking to a given devfs node. It also increments
1564 * the link count on the target node.
1567 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based)
1569 struct mount *mp = target->mp;
1570 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1571 struct devfs_node *linknode;
1572 struct hotplug_device *hpdev;
1573 char *create_path = NULL;
1578 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1580 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1581 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1584 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1587 if (devfs_find_device_node_by_name(parent, name)) {
1588 devfs_debug(DEVFS_DEBUG_WARNING,
1589 "Node already exists: %s "
1590 "(devfs_make_alias_worker)!\n",
1596 linknode = devfs_allocp(Plink, name, parent, mp, NULL);
1597 if (linknode == NULL) {
1602 linknode->link_target = target;
1606 linknode->flags |= DEVFS_RULE_CREATED;
1609 /* hotplug handler */
1610 if(devfs_node_added) {
1611 hpdev = kmalloc(sizeof(struct hotplug_device), M_TEMP, M_WAITOK);
1612 hpdev->dev = target->d_dev;
1613 hpdev->name = name_orig;
1614 devfs_node_added(hpdev);
1615 kfree(hpdev, M_TEMP);
1617 kfree(name_buf, M_TEMP);
1622 * This function is called by the core and handles mount point
1623 * strings. It either calls the relevant worker (devfs_apply_
1624 * reset_rules_worker) on all mountpoints or only a specific
1628 devfs_apply_reset_rules_caller(char *mountto, int apply)
1630 struct devfs_mnt_data *mnt;
1632 if (mountto[0] == '*') {
1633 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1634 devfs_iterate_topology(mnt->root_node,
1635 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1639 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1640 if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) {
1641 devfs_iterate_topology(mnt->root_node,
1642 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1649 kfree(mountto, M_DEVFS);
1654 * This function calls a given callback function for
1655 * every dev node in the devfs dev list.
1658 devfs_scan_callback_worker(devfs_scan_t *callback, void *arg)
1662 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1670 * This function tries to resolve a given directory, or if not
1671 * found and creation requested, creates the given directory.
1673 static struct devfs_node *
1674 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1675 size_t name_len, int create)
1677 struct devfs_node *node, *found = NULL;
1679 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1680 if (name_len != node->d_dir.d_namlen)
1683 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1689 if ((found == NULL) && (create)) {
1690 found = devfs_allocp(Pdir, dir_name, parent, parent->mp, NULL);
1697 * This function tries to resolve a complete path. If creation is requested,
1698 * if a given part of the path cannot be resolved (because it doesn't exist),
1702 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1704 struct devfs_node *node = parent;
1711 buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1713 while (*path && idx < PATH_MAX - 1) {
1718 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1728 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1729 kfree (buf, M_TEMP);
1734 * Takes a full path and strips it into a directory path and a name.
1735 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
1736 * requires a working buffer with enough size to keep the whole
1740 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
1744 size_t len = strlen(fullpath) + 1;
1747 KKASSERT((fullpath != NULL) && (buf != NULL));
1748 KKASSERT((pathp != NULL) && (namep != NULL));
1750 memcpy(buf, fullpath, len);
1752 for (i = len-1; i>= 0; i--) {
1753 if (buf[i] == '/') {
1773 * This function creates a new devfs node for a given device. It can
1774 * handle a complete path as device name, and accordingly creates
1775 * the path and the final device node.
1777 * The reference count on the passed dev remains unchanged.
1780 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
1781 char *dev_name, char *path_fmt, ...)
1783 struct devfs_node *parent, *node = NULL;
1784 struct hotplug_device *hpdev;
1790 char *create_path = NULL;
1791 char *names = "pqrsPQRS";
1793 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1795 if (path_fmt != NULL) {
1796 __va_start(ap, path_fmt);
1797 kvasnrprintf(&path, PATH_MAX, 10, path_fmt, ap);
1801 parent = devfs_resolve_or_create_path(root, path, 1);
1804 devfs_resolve_name_path(
1805 ((dev_name == NULL) && (dev))?(dev->si_name):(dev_name),
1806 name_buf, &create_path, &name);
1809 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1812 if (devfs_find_device_node_by_name(parent, name)) {
1813 devfs_debug(DEVFS_DEBUG_WARNING, "devfs_create_device_node: "
1814 "DEVICE %s ALREADY EXISTS!!! Ignoring creation request.\n", name);
1818 node = devfs_allocp(Pdev, name, parent, parent->mp, dev);
1819 nanotime(&parent->mtime);
1822 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
1825 if ((dev) && (strlen(dev->si_name) >= 4) &&
1826 (!memcmp(dev->si_name, "ptm/", 4))) {
1827 node->parent->flags |= DEVFS_HIDDEN;
1828 node->flags |= DEVFS_HIDDEN;
1832 * Ugly pty magic, to tag pty devices as such and hide them if needed.
1834 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
1835 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1837 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
1839 for (i = 0; i < strlen(names); i++) {
1840 if (name[3] == names[i]) {
1846 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1848 /* hotplug handler */
1849 if(devfs_node_added) {
1850 hpdev = kmalloc(sizeof(struct hotplug_device), M_TEMP, M_WAITOK);
1851 hpdev->dev = node->d_dev;
1852 hpdev->name = node->d_dev->si_name;
1853 devfs_node_added(hpdev);
1854 kfree(hpdev, M_TEMP);
1858 kfree(name_buf, M_TEMP);
1864 * This function finds a given device node in the topology with a given
1868 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target)
1870 if ((node->node_type == Pdev) && (node->d_dev == target)) {
1878 * This function finds a device node in the given parent directory by its
1879 * name and returns it.
1882 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
1884 struct devfs_node *node, *found = NULL;
1885 size_t len = strlen(target);
1887 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1888 if (len != node->d_dir.d_namlen)
1891 if (!memcmp(node->d_dir.d_name, target, len)) {
1901 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop)
1903 struct vnode *vp = NULL;
1904 ino_t target = *inop;
1906 if (node->d_dir.d_ino == target) {
1909 vget(vp, LK_EXCLUSIVE | LK_RETRY);
1912 devfs_allocv(&vp, node);
1921 * This function takes a cdev and removes its devfs node in the
1922 * given topology. The cdev remains intact.
1925 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
1927 struct devfs_node *node, *parent;
1930 char *create_path = NULL;
1934 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1935 ksnprintf(name_buf, PATH_MAX, "%s", target->si_name);
1937 devfs_resolve_name_path(target->si_name, name_buf, &create_path, &name);
1940 parent = devfs_resolve_or_create_path(root, create_path, 0);
1944 if (parent == NULL) {
1945 kfree(name_buf, M_TEMP);
1949 node = devfs_find_device_node_by_name(parent, name);
1952 nanotime(&node->parent->mtime);
1956 kfree(name_buf, M_TEMP);
1962 * Just set perms and ownership for given node.
1965 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid,
1966 u_short mode, u_long flags)
1976 * Propagates a device attach/detach to all mount
1977 * points. Also takes care of automatic alias removal
1978 * for a deleted cdev.
1981 devfs_propagate_dev(cdev_t dev, int attach)
1983 struct devfs_mnt_data *mnt;
1985 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1987 /* Device is being attached */
1988 devfs_create_device_node(mnt->root_node, dev,
1991 /* Device is being detached */
1992 devfs_alias_remove(dev);
1993 devfs_destroy_device_node(mnt->root_node, dev);
2000 * devfs_clone either returns a basename from a complete name by
2001 * returning the length of the name without trailing digits, or,
2002 * if clone != 0, calls the device's clone handler to get a new
2003 * device, which in turn is returned in devp.
2006 devfs_clone(cdev_t dev, const char *name, size_t len, int mode,
2010 struct devfs_clone_handler *chandler;
2011 struct dev_clone_args ap;
2013 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
2014 if (chandler->namlen != len)
2016 if ((!memcmp(chandler->name, name, len)) && (chandler->nhandler)) {
2017 lockmgr(&devfs_lock, LK_RELEASE);
2019 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2021 ap.a_head.a_dev = dev;
2027 error = (chandler->nhandler)(&ap);
2040 * Registers a new orphan in the orphan list.
2043 devfs_tracer_add_orphan(struct devfs_node *node)
2045 struct devfs_orphan *orphan;
2048 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2049 orphan->node = node;
2051 KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2052 node->flags |= DEVFS_ORPHANED;
2053 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2057 * Removes an orphan from the orphan list.
2060 devfs_tracer_del_orphan(struct devfs_node *node)
2062 struct devfs_orphan *orphan;
2066 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) {
2067 if (orphan->node == node) {
2068 node->flags &= ~DEVFS_ORPHANED;
2069 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2070 kfree(orphan, M_DEVFS);
2077 * Counts the orphans in the orphan list, and if cleanup
2078 * is specified, also frees the orphan and removes it from
2082 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2084 struct devfs_orphan *orphan, *orphan2;
2087 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) {
2090 * If we are instructed to clean up, we do so.
2093 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2094 orphan->node->flags &= ~DEVFS_ORPHANED;
2095 devfs_freep(orphan->node);
2096 kfree(orphan, M_DEVFS);
2104 * Fetch an ino_t from the global d_ino by increasing it
2108 devfs_fetch_ino(void)
2112 spin_lock_wr(&ino_lock);
2114 spin_unlock_wr(&ino_lock);
2120 * Allocates a new cdev and initializes it's most basic
2124 devfs_new_cdev(struct dev_ops *ops, int minor, struct dev_ops *bops)
2126 cdev_t dev = sysref_alloc(&cdev_sysref_class);
2128 sysref_activate(&dev->si_sysref);
2130 bzero(dev, offsetof(struct cdev, si_sysref));
2135 dev->si_drv1 = NULL;
2136 dev->si_drv2 = NULL;
2137 dev->si_lastread = 0; /* time_second */
2138 dev->si_lastwrite = 0; /* time_second */
2140 dev->si_dict = NULL;
2144 dev->si_uminor = minor;
2145 dev->si_bops = bops;
2146 /* If there is a backing device, we reference its ops */
2147 dev->si_inode = makeudev(
2148 devfs_reference_ops((bops)?(bops):(ops)),
2155 devfs_cdev_terminate(cdev_t dev)
2159 /* Check if it is locked already. if not, we acquire the devfs lock */
2160 if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) {
2161 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2165 /* Propagate destruction, just in case */
2166 devfs_propagate_dev(dev, 0);
2168 /* If we acquired the lock, we also get rid of it */
2170 lockmgr(&devfs_lock, LK_RELEASE);
2172 /* If there is a backing device, we release the backing device's ops */
2173 devfs_release_ops((dev->si_bops)?(dev->si_bops):(dev->si_ops));
2175 /* Finally destroy the device */
2176 sysref_put(&dev->si_sysref);
2180 * Dummies for now (individual locks for MPSAFE)
2183 devfs_cdev_lock(cdev_t dev)
2188 devfs_cdev_unlock(cdev_t dev)
2193 * Links a given cdev into the dev list.
2196 devfs_link_dev(cdev_t dev)
2198 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2199 dev->si_flags |= SI_DEVFS_LINKED;
2200 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2206 * Removes a given cdev from the dev list. The caller is responsible for
2207 * releasing the reference on the device associated with the linkage.
2209 * Returns EALREADY if the dev has already been unlinked.
2212 devfs_unlink_dev(cdev_t dev)
2214 if ((dev->si_flags & SI_DEVFS_LINKED)) {
2215 TAILQ_REMOVE(&devfs_dev_list, dev, link);
2216 dev->si_flags &= ~SI_DEVFS_LINKED;
2223 devfs_node_is_accessible(struct devfs_node *node)
2225 if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2232 devfs_reference_ops(struct dev_ops *ops)
2235 struct devfs_dev_ops *found = NULL;
2236 struct devfs_dev_ops *devops;
2238 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2239 if (devops->ops == ops) {
2246 found = kmalloc(sizeof(struct devfs_dev_ops), M_DEVFS, M_WAITOK);
2248 found->ref_count = 0;
2249 TAILQ_INSERT_TAIL(&devfs_dev_ops_list, found, link);
2254 if (found->ref_count == 0) {
2255 found->id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255);
2256 if (found->id == -1) {
2257 /* Ran out of unique ids */
2258 devfs_debug(DEVFS_DEBUG_WARNING,
2259 "devfs_reference_ops: WARNING: ran out of unique ids\n");
2269 devfs_release_ops(struct dev_ops *ops)
2271 struct devfs_dev_ops *found = NULL;
2272 struct devfs_dev_ops *devops;
2274 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2275 if (devops->ops == ops) {
2285 if (found->ref_count == 0) {
2286 TAILQ_REMOVE(&devfs_dev_ops_list, found, link);
2287 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), found->id);
2288 kfree(found, M_DEVFS);
2297 msg = devfs_msg_get();
2298 msg = devfs_msg_send_sync(DEVFS_SYNC, msg);
2303 * Called on init of devfs; creates the objcaches and
2304 * spawns off the devfs core thread. Also initializes
2310 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2311 /* Create objcaches for nodes, msgs and devs */
2312 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2314 objcache_malloc_alloc,
2315 objcache_malloc_free,
2316 &devfs_node_malloc_args );
2318 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2320 objcache_malloc_alloc,
2321 objcache_malloc_free,
2322 &devfs_msg_malloc_args );
2324 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2326 objcache_malloc_alloc,
2327 objcache_malloc_free,
2328 &devfs_dev_malloc_args );
2330 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2332 /* Initialize the reply-only port which acts as a message drain */
2333 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2335 /* Initialize *THE* devfs lock */
2336 lockinit(&devfs_lock, "devfs_core lock", 0, 0);
2339 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2340 0, 0, "devfs_msg_core");
2342 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2344 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2348 * Called on unload of devfs; takes care of destroying the core
2349 * and the objcaches. Also removes aliases that are no longer needed.
2354 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2356 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2358 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2359 tsleep(td_core/*devfs_id*/, 0, "devfsc", 10000);
2361 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2363 /* Destroy the objcaches */
2364 objcache_destroy(devfs_msg_cache);
2365 objcache_destroy(devfs_node_cache);
2366 objcache_destroy(devfs_dev_cache);
2372 * This is a sysctl handler to assist userland devname(3) to
2373 * find the device name for a given udev.
2376 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2383 if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t))))
2386 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev);
2391 if ((found = devfs_find_device_by_udev(udev)) == NULL)
2394 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2398 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
2399 NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)");
2401 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2402 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2403 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable,
2404 0, "Enable DevFS debugging");
2406 SYSINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST,
2408 SYSUNINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY,
2409 devfs_uninit, NULL);