2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/mount.h>
38 #include <sys/vnode.h>
39 #include <sys/types.h>
41 #include <sys/msgport.h>
42 #include <sys/msgport2.h>
43 #include <sys/spinlock2.h>
44 #include <sys/sysctl.h>
45 #include <sys/ucred.h>
46 #include <sys/param.h>
47 #include <sys/sysref2.h>
48 #include <sys/systm.h>
49 #include <sys/devfs.h>
50 #include <sys/devfs_rules.h>
51 #include <sys/hotplug.h>
53 MALLOC_DEFINE(M_DEVFS, "devfs", "Device File System (devfs) allocations");
54 DEVFS_DECLARE_CLONE_BITMAP(ops_id);
56 * SYSREF Integration - reference counting, allocation,
57 * sysid and syslink integration.
59 static void devfs_cdev_terminate(cdev_t dev);
60 static void devfs_cdev_lock(cdev_t dev);
61 static void devfs_cdev_unlock(cdev_t dev);
62 static struct sysref_class cdev_sysref_class = {
65 .proto = SYSREF_PROTO_DEV,
66 .offset = offsetof(struct cdev, si_sysref),
67 .objsize = sizeof(struct cdev),
71 .terminate = (sysref_terminate_func_t)devfs_cdev_terminate,
72 .lock = (sysref_lock_func_t)devfs_cdev_lock,
73 .unlock = (sysref_unlock_func_t)devfs_cdev_unlock
77 static struct objcache *devfs_node_cache;
78 static struct objcache *devfs_msg_cache;
79 static struct objcache *devfs_dev_cache;
81 static struct objcache_malloc_args devfs_node_malloc_args = {
82 sizeof(struct devfs_node), M_DEVFS };
83 struct objcache_malloc_args devfs_msg_malloc_args = {
84 sizeof(struct devfs_msg), M_DEVFS };
85 struct objcache_malloc_args devfs_dev_malloc_args = {
86 sizeof(struct cdev), M_DEVFS };
88 static struct devfs_dev_head devfs_dev_list =
89 TAILQ_HEAD_INITIALIZER(devfs_dev_list);
90 static struct devfs_mnt_head devfs_mnt_list =
91 TAILQ_HEAD_INITIALIZER(devfs_mnt_list);
92 static struct devfs_chandler_head devfs_chandler_list =
93 TAILQ_HEAD_INITIALIZER(devfs_chandler_list);
94 static struct devfs_alias_head devfs_alias_list =
95 TAILQ_HEAD_INITIALIZER(devfs_alias_list);
96 static struct devfs_dev_ops_head devfs_dev_ops_list =
97 TAILQ_HEAD_INITIALIZER(devfs_dev_ops_list);
99 struct lock devfs_lock;
100 static struct lwkt_port devfs_dispose_port;
101 static struct lwkt_port devfs_msg_port;
102 static struct thread *td_core;
104 static struct spinlock ino_lock;
106 static int devfs_debug_enable;
107 static int devfs_run;
109 static ino_t devfs_fetch_ino(void);
110 static int devfs_create_all_dev_worker(struct devfs_node *);
111 static int devfs_create_dev_worker(cdev_t, uid_t, gid_t, int);
112 static int devfs_destroy_dev_worker(cdev_t);
113 static int devfs_destroy_subnames_worker(char *);
114 static int devfs_destroy_dev_by_ops_worker(struct dev_ops *, int);
115 static int devfs_propagate_dev(cdev_t, int);
116 static int devfs_unlink_dev(cdev_t dev);
117 static void devfs_msg_exec(devfs_msg_t msg);
119 static int devfs_chandler_add_worker(const char *, d_clone_t *);
120 static int devfs_chandler_del_worker(const char *);
122 static void devfs_msg_autofree_reply(lwkt_port_t, lwkt_msg_t);
123 static void devfs_msg_core(void *);
125 static int devfs_find_device_by_name_worker(devfs_msg_t);
126 static int devfs_find_device_by_udev_worker(devfs_msg_t);
128 static int devfs_apply_reset_rules_caller(char *, int);
130 static int devfs_scan_callback_worker(devfs_scan_t *);
132 static struct devfs_node *devfs_resolve_or_create_dir(struct devfs_node *,
133 char *, size_t, int);
135 static int devfs_make_alias_worker(struct devfs_alias *);
136 static int devfs_alias_remove(cdev_t);
137 static int devfs_alias_reap(void);
138 static int devfs_alias_propagate(struct devfs_alias *);
139 static int devfs_alias_apply(struct devfs_node *, struct devfs_alias *);
140 static int devfs_alias_check_create(struct devfs_node *);
142 static int devfs_clr_subnames_flag_worker(char *, uint32_t);
143 static int devfs_destroy_subnames_without_flag_worker(char *, uint32_t);
145 static void *devfs_reaperp_callback(struct devfs_node *, void *);
146 static void *devfs_gc_dirs_callback(struct devfs_node *, void *);
147 static void *devfs_gc_links_callback(struct devfs_node *, struct devfs_node *);
149 devfs_inode_to_vnode_worker_callback(struct devfs_node *, ino_t *);
152 void (*devfs_node_added)(struct hotplug_device*) = NULL;
153 void (*devfs_node_removed)(struct hotplug_device*) = NULL;
156 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function
160 devfs_debug(int level, char *fmt, ...)
165 if (level <= devfs_debug_enable)
173 * devfs_allocp() Allocates a new devfs node with the specified
174 * parameters. The node is also automatically linked into the topology
175 * if a parent is specified. It also calls the rule and alias stuff to
176 * be applied on the new node
179 devfs_allocp(devfs_nodetype devfsnodetype, char *name,
180 struct devfs_node *parent, struct mount *mp, cdev_t dev)
182 struct devfs_node *node = NULL;
183 size_t namlen = strlen(name);
185 node = objcache_get(devfs_node_cache, M_WAITOK);
186 bzero(node, sizeof(*node));
188 atomic_add_long(&(DEVFS_MNTDATA(mp)->leak_count), 1);
193 node->d_dir.d_ino = devfs_fetch_ino();
196 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries
199 node->cookie_jar = 2;
202 * Access Control members
204 node->mode = DEVFS_DEFAULT_MODE;
205 node->uid = DEVFS_DEFAULT_UID;
206 node->gid = DEVFS_DEFAULT_GID;
208 switch (devfsnodetype) {
211 * Ensure that we don't recycle the root vnode by marking it as
212 * linked into the topology.
214 node->flags |= DEVFS_NODE_LINKED;
216 TAILQ_INIT(DEVFS_DENODE_HEAD(node));
217 node->d_dir.d_type = DT_DIR;
222 node->d_dir.d_type = DT_LNK;
226 node->d_dir.d_type = DT_REG;
231 node->d_dir.d_type = DT_CHR;
234 node->mode = dev->si_perms;
235 node->uid = dev->si_uid;
236 node->gid = dev->si_gid;
238 devfs_alias_check_create(node);
243 panic("devfs_allocp: unknown node type");
247 node->node_type = devfsnodetype;
249 /* Initialize the dirent structure of each devfs vnode */
250 node->d_dir.d_namlen = namlen;
251 node->d_dir.d_name = kmalloc(namlen+1, M_DEVFS, M_WAITOK);
252 memcpy(node->d_dir.d_name, name, namlen);
253 node->d_dir.d_name[namlen] = '\0';
255 /* Initialize the parent node element */
256 node->parent = parent;
259 devfs_rule_check_apply(node, NULL);
261 /* Initialize *time members */
262 nanotime(&node->atime);
263 node->mtime = node->ctime = node->atime;
266 * Associate with parent as last step, clean out namecache
269 if ((parent != NULL) &&
270 ((parent->node_type == Proot) || (parent->node_type == Pdir))) {
272 node->cookie = parent->cookie_jar++;
273 node->flags |= DEVFS_NODE_LINKED;
274 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent), node, link);
276 /* This forces negative namecache lookups to clear */
277 ++mp->mnt_namecache_gen;
280 ++DEVFS_MNTDATA(mp)->file_count;
286 * devfs_allocv() allocates a new vnode based on a devfs node.
289 devfs_allocv(struct vnode **vpp, struct devfs_node *node)
297 while ((vp = node->v_node) != NULL) {
298 error = vget(vp, LK_EXCLUSIVE);
299 if (error != ENOENT) {
305 if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0)
310 if (node->v_node != NULL) {
319 switch (node->node_type) {
321 vsetflags(vp, VROOT);
337 KKASSERT(node->d_dev);
339 vp->v_uminor = node->d_dev->si_uminor;
342 v_associate_rdev(vp, node->d_dev);
343 vp->v_ops = &node->mp->mnt_vn_spec_ops;
347 panic("devfs_allocv: unknown node type");
355 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
356 * based on the newly created devfs node.
359 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype,
360 char *name, struct devfs_node *parent, cdev_t dev)
362 struct devfs_node *node;
364 node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
367 devfs_allocv(vpp, node);
375 * Destroy the devfs_node. The node must be unlinked from the topology.
377 * This function will also destroy any vnode association with the node
380 * The cdev_t itself remains intact.
383 devfs_freep(struct devfs_node *node)
388 KKASSERT(((node->flags & DEVFS_NODE_LINKED) == 0) ||
389 (node->node_type == Proot));
390 KKASSERT((node->flags & DEVFS_DESTROYED) == 0);
392 atomic_subtract_long(&(DEVFS_MNTDATA(node->mp)->leak_count), 1);
393 if (node->symlink_name) {
394 kfree(node->symlink_name, M_DEVFS);
395 node->symlink_name = NULL;
399 * Remove the node from the orphan list if it is still on it.
401 if (node->flags & DEVFS_ORPHANED)
402 devfs_tracer_del_orphan(node);
405 * Disassociate the vnode from the node. This also prevents the
406 * vnode's reclaim code from double-freeing the node.
408 * The vget is needed to safely modify the vp. It also serves
409 * to cycle the refs and terminate the vnode if it happens to
410 * be inactive, otherwise namecache references may not get cleared.
412 while ((vp = node->v_node) != NULL) {
413 if (vget(vp, LK_EXCLUSIVE | LK_RETRY) != 0)
418 cache_inval_vp(vp, CINV_DESTROY);
421 if (node->d_dir.d_name) {
422 kfree(node->d_dir.d_name, M_DEVFS);
423 node->d_dir.d_name = NULL;
425 node->flags |= DEVFS_DESTROYED;
427 --DEVFS_MNTDATA(node->mp)->file_count;
429 objcache_put(devfs_node_cache, node);
435 * Unlink the devfs node from the topology and add it to the orphan list.
436 * The node will later be destroyed by freep.
438 * Any vnode association, including the v_rdev and v_data, remains intact
442 devfs_unlinkp(struct devfs_node *node)
444 struct devfs_node *parent;
445 struct hotplug_device *hpdev;
449 * Add the node to the orphan list, so it is referenced somewhere, to
450 * so we don't leak it.
452 devfs_tracer_add_orphan(node);
454 parent = node->parent;
457 * If the parent is known we can unlink the node out of the topology
460 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
462 KKASSERT((parent->nchildren >= 0));
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) {
526 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target)
528 if ((node->node_type == Plink) && (node->link_target == target)) {
537 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
538 * freeing a node, but also removes empty directories and links that link
539 * via devfs auto-link mechanism to the node being deleted.
542 devfs_gc(struct devfs_node *node)
544 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
546 if (node->nlinks > 0)
547 devfs_iterate_topology(root_node,
548 (devfs_iterate_callback_t *)devfs_gc_links_callback, node);
551 devfs_iterate_topology(root_node,
552 (devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL);
560 * devfs_create_dev() is the asynchronous entry point for device creation.
561 * It just sends a message with the relevant details to the devfs core.
563 * This function will reference the passed device. The reference is owned
564 * by devfs and represents all of the device's node associations.
567 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
570 devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
576 * devfs_destroy_dev() is the asynchronous entry point for device destruction.
577 * It just sends a message with the relevant details to the devfs core.
580 devfs_destroy_dev(cdev_t dev)
582 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
587 * devfs_mount_add() is the synchronous entry point for adding a new devfs
588 * mount. It sends a synchronous message with the relevant details to the
592 devfs_mount_add(struct devfs_mnt_data *mnt)
596 msg = devfs_msg_get();
598 msg = devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
605 * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
606 * It sends a synchronous message with the relevant details to the devfs core.
609 devfs_mount_del(struct devfs_mnt_data *mnt)
613 msg = devfs_msg_get();
615 msg = devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
622 * devfs_destroy_subnames() is the synchronous entry point for device
623 * destruction by subname. It just sends a message with the relevant details to
627 devfs_destroy_subnames(char *name)
631 msg = devfs_msg_get();
632 msg->mdv_load = name;
633 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES, msg);
639 devfs_clr_subnames_flag(char *name, uint32_t flag)
643 msg = devfs_msg_get();
644 msg->mdv_flags.name = name;
645 msg->mdv_flags.flag = flag;
646 msg = devfs_msg_send_sync(DEVFS_CLR_SUBNAMES_FLAG, msg);
653 devfs_destroy_subnames_without_flag(char *name, uint32_t flag)
657 msg = devfs_msg_get();
658 msg->mdv_flags.name = name;
659 msg->mdv_flags.flag = flag;
660 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES_WO_FLAG, msg);
667 * devfs_create_all_dev is the asynchronous entry point to trigger device
668 * node creation. It just sends a message with the relevant details to
672 devfs_create_all_dev(struct devfs_node *root)
674 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
679 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
680 * devices with a specific set of dev_ops and minor. It just sends a
681 * message with the relevant details to the devfs core.
684 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
686 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
691 * devfs_clone_handler_add is the synchronous entry point to add a new
692 * clone handler. It just sends a message with the relevant details to
696 devfs_clone_handler_add(const char *name, d_clone_t *nhandler)
700 msg = devfs_msg_get();
701 msg->mdv_chandler.name = name;
702 msg->mdv_chandler.nhandler = nhandler;
703 msg = devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg);
709 * devfs_clone_handler_del is the synchronous entry point to remove a
710 * clone handler. It just sends a message with the relevant details to
714 devfs_clone_handler_del(const char *name)
718 msg = devfs_msg_get();
719 msg->mdv_chandler.name = name;
720 msg->mdv_chandler.nhandler = NULL;
721 msg = devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg);
727 * devfs_find_device_by_name is the synchronous entry point to find a
728 * device given its name. It sends a synchronous message with the
729 * relevant details to the devfs core and returns the answer.
732 devfs_find_device_by_name(const char *fmt, ...)
743 kvasnrprintf(&target, PATH_MAX, 10, fmt, ap);
746 msg = devfs_msg_get();
747 msg->mdv_name = target;
748 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
749 found = msg->mdv_cdev;
757 * devfs_find_device_by_udev is the synchronous entry point to find a
758 * device given its udev number. It sends a synchronous message with
759 * the relevant details to the devfs core and returns the answer.
762 devfs_find_device_by_udev(udev_t udev)
767 msg = devfs_msg_get();
768 msg->mdv_udev = udev;
769 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg);
770 found = msg->mdv_cdev;
773 devfs_debug(DEVFS_DEBUG_DEBUG,
774 "devfs_find_device_by_udev found? %s -end:3-\n",
775 ((found) ? found->si_name:"NO"));
780 devfs_inode_to_vnode(struct mount *mp, ino_t target)
782 struct vnode *vp = NULL;
788 msg = devfs_msg_get();
789 msg->mdv_ino.mp = mp;
790 msg->mdv_ino.ino = target;
791 msg = devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg);
792 vp = msg->mdv_ino.vp;
793 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
800 * devfs_make_alias is the asynchronous entry point to register an alias
801 * for a device. It just sends a message with the relevant details to the
805 devfs_make_alias(const char *name, cdev_t dev_target)
807 struct devfs_alias *alias;
812 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
813 alias->name = kstrdup(name, M_DEVFS);
815 alias->dev_target = dev_target;
817 devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
822 * devfs_apply_rules is the asynchronous entry point to trigger application
823 * of all rules. It just sends a message with the relevant details to the
827 devfs_apply_rules(char *mntto)
831 new_name = kstrdup(mntto, M_DEVFS);
832 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
838 * devfs_reset_rules is the asynchronous entry point to trigger reset of all
839 * rules. It just sends a message with the relevant details to the devfs core.
842 devfs_reset_rules(char *mntto)
846 new_name = kstrdup(mntto, M_DEVFS);
847 devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
854 * devfs_scan_callback is the asynchronous entry point to call a callback
856 * It just sends a message with the relevant details to the devfs core.
859 devfs_scan_callback(devfs_scan_t *callback)
863 KKASSERT(sizeof(callback) == sizeof(void *));
865 msg = devfs_msg_get();
866 msg->mdv_load = callback;
867 msg = devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
875 * Acts as a message drain. Any message that is replied to here gets destroyed
876 * and the memory freed.
879 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
881 devfs_msg_put((devfs_msg_t)msg);
885 * devfs_msg_get allocates a new devfs msg and returns it.
890 return objcache_get(devfs_msg_cache, M_WAITOK);
894 * devfs_msg_put deallocates a given devfs msg.
897 devfs_msg_put(devfs_msg_t msg)
899 objcache_put(devfs_msg_cache, msg);
904 * devfs_msg_send is the generic asynchronous message sending facility
905 * for devfs. By default the reply port is the automatic disposal port.
907 * If the current thread is the devfs_msg_port thread we execute the
908 * operation synchronously.
911 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
913 lwkt_port_t port = &devfs_msg_port;
915 lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
917 devfs_msg->hdr.u.ms_result = cmd;
919 if (port->mpu_td == curthread) {
920 devfs_msg_exec(devfs_msg);
921 lwkt_replymsg(&devfs_msg->hdr, 0);
923 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
928 * devfs_msg_send_sync is the generic synchronous message sending
929 * facility for devfs. It initializes a local reply port and waits
930 * for the core's answer. This answer is then returned.
933 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
935 struct lwkt_port rep_port;
936 devfs_msg_t msg_incoming;
937 lwkt_port_t port = &devfs_msg_port;
939 lwkt_initport_thread(&rep_port, curthread);
940 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
942 devfs_msg->hdr.u.ms_result = cmd;
944 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
945 msg_incoming = lwkt_waitport(&rep_port, 0);
951 * sends a message with a generic argument.
954 devfs_msg_send_generic(uint32_t cmd, void *load)
956 devfs_msg_t devfs_msg = devfs_msg_get();
958 devfs_msg->mdv_load = load;
959 devfs_msg_send(cmd, devfs_msg);
963 * sends a message with a name argument.
966 devfs_msg_send_name(uint32_t cmd, char *name)
968 devfs_msg_t devfs_msg = devfs_msg_get();
970 devfs_msg->mdv_name = name;
971 devfs_msg_send(cmd, devfs_msg);
975 * sends a message with a mount argument.
978 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
980 devfs_msg_t devfs_msg = devfs_msg_get();
982 devfs_msg->mdv_mnt = mnt;
983 devfs_msg_send(cmd, devfs_msg);
987 * sends a message with an ops argument.
990 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
992 devfs_msg_t devfs_msg = devfs_msg_get();
994 devfs_msg->mdv_ops.ops = ops;
995 devfs_msg->mdv_ops.minor = minor;
996 devfs_msg_send(cmd, devfs_msg);
1000 * sends a message with a clone handler argument.
1003 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
1005 devfs_msg_t devfs_msg = devfs_msg_get();
1007 devfs_msg->mdv_chandler.name = name;
1008 devfs_msg->mdv_chandler.nhandler = handler;
1009 devfs_msg_send(cmd, devfs_msg);
1013 * sends a message with a device argument.
1016 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1018 devfs_msg_t devfs_msg = devfs_msg_get();
1020 devfs_msg->mdv_dev.dev = dev;
1021 devfs_msg->mdv_dev.uid = uid;
1022 devfs_msg->mdv_dev.gid = gid;
1023 devfs_msg->mdv_dev.perms = perms;
1025 devfs_msg_send(cmd, devfs_msg);
1029 * sends a message with a link argument.
1032 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1034 devfs_msg_t devfs_msg = devfs_msg_get();
1036 devfs_msg->mdv_link.name = name;
1037 devfs_msg->mdv_link.target = target;
1038 devfs_msg->mdv_link.mp = mp;
1039 devfs_msg_send(cmd, devfs_msg);
1043 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1044 * and calls the relevant worker functions. By using messages it's assured
1045 * that events occur in the correct order.
1048 devfs_msg_core(void *arg)
1053 lwkt_initport_thread(&devfs_msg_port, curthread);
1057 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1058 devfs_debug(DEVFS_DEBUG_DEBUG,
1059 "devfs_msg_core, new msg: %x\n",
1060 (unsigned int)msg->hdr.u.ms_result);
1061 devfs_msg_exec(msg);
1062 lwkt_replymsg(&msg->hdr, 0);
1069 devfs_msg_exec(devfs_msg_t msg)
1071 struct devfs_mnt_data *mnt;
1072 struct devfs_node *node;
1076 * Acquire the devfs lock to ensure safety of all called functions
1078 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1080 switch (msg->hdr.u.ms_result) {
1081 case DEVFS_DEVICE_CREATE:
1082 dev = msg->mdv_dev.dev;
1083 devfs_create_dev_worker(dev,
1086 msg->mdv_dev.perms);
1088 case DEVFS_DEVICE_DESTROY:
1089 dev = msg->mdv_dev.dev;
1090 devfs_destroy_dev_worker(dev);
1092 case DEVFS_DESTROY_SUBNAMES:
1093 devfs_destroy_subnames_worker(msg->mdv_load);
1095 case DEVFS_DESTROY_DEV_BY_OPS:
1096 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1097 msg->mdv_ops.minor);
1099 case DEVFS_CREATE_ALL_DEV:
1100 node = (struct devfs_node *)msg->mdv_load;
1101 devfs_create_all_dev_worker(node);
1103 case DEVFS_MOUNT_ADD:
1105 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1106 devfs_create_all_dev_worker(mnt->root_node);
1108 case DEVFS_MOUNT_DEL:
1110 TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1111 devfs_iterate_topology(mnt->root_node, devfs_reaperp_callback,
1113 if (mnt->leak_count) {
1114 devfs_debug(DEVFS_DEBUG_SHOW,
1115 "Leaked %ld devfs_node elements!\n",
1119 case DEVFS_CHANDLER_ADD:
1120 devfs_chandler_add_worker(msg->mdv_chandler.name,
1121 msg->mdv_chandler.nhandler);
1123 case DEVFS_CHANDLER_DEL:
1124 devfs_chandler_del_worker(msg->mdv_chandler.name);
1126 case DEVFS_FIND_DEVICE_BY_NAME:
1127 devfs_find_device_by_name_worker(msg);
1129 case DEVFS_FIND_DEVICE_BY_UDEV:
1130 devfs_find_device_by_udev_worker(msg);
1132 case DEVFS_MAKE_ALIAS:
1133 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1135 case DEVFS_APPLY_RULES:
1136 devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1138 case DEVFS_RESET_RULES:
1139 devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1141 case DEVFS_SCAN_CALLBACK:
1142 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load);
1144 case DEVFS_CLR_SUBNAMES_FLAG:
1145 devfs_clr_subnames_flag_worker(msg->mdv_flags.name,
1146 msg->mdv_flags.flag);
1148 case DEVFS_DESTROY_SUBNAMES_WO_FLAG:
1149 devfs_destroy_subnames_without_flag_worker(msg->mdv_flags.name,
1150 msg->mdv_flags.flag);
1152 case DEVFS_INODE_TO_VNODE:
1153 msg->mdv_ino.vp = devfs_iterate_topology(
1154 DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node,
1155 (devfs_iterate_callback_t *)devfs_inode_to_vnode_worker_callback,
1158 case DEVFS_TERMINATE_CORE:
1164 devfs_debug(DEVFS_DEBUG_WARNING,
1165 "devfs_msg_core: unknown message "
1166 "received at core\n");
1169 lockmgr(&devfs_lock, LK_RELEASE);
1173 * Worker function to insert a new dev into the dev list and initialize its
1174 * permissions. It also calls devfs_propagate_dev which in turn propagates
1175 * the change to all mount points.
1177 * The passed dev is already referenced. This reference is eaten by this
1178 * function and represents the dev's linkage into devfs_dev_list.
1181 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1187 dev->si_perms = perms;
1189 devfs_link_dev(dev);
1190 devfs_propagate_dev(dev, 1);
1196 * Worker function to delete a dev from the dev list and free the cdev.
1197 * It also calls devfs_propagate_dev which in turn propagates the change
1198 * to all mount points.
1201 devfs_destroy_dev_worker(cdev_t dev)
1206 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1208 error = devfs_unlink_dev(dev);
1209 devfs_propagate_dev(dev, 0);
1211 release_dev(dev); /* link ref */
1219 * Worker function to destroy all devices with a certain basename.
1220 * Calls devfs_destroy_dev_worker for the actual destruction.
1223 devfs_destroy_subnames_worker(char *name)
1226 size_t len = strlen(name);
1228 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1229 if ((!strncmp(dev->si_name, name, len)) &&
1230 (dev->si_name[len] != '\0')) {
1231 devfs_destroy_dev_worker(dev);
1238 devfs_clr_subnames_flag_worker(char *name, uint32_t flag)
1241 size_t len = strlen(name);
1243 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1244 if ((!strncmp(dev->si_name, name, len)) &&
1245 (dev->si_name[len] != '\0')) {
1246 dev->si_flags &= ~flag;
1254 devfs_destroy_subnames_without_flag_worker(char *name, uint32_t flag)
1257 size_t len = strlen(name);
1259 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1260 if ((!strncmp(dev->si_name, name, len)) &&
1261 (dev->si_name[len] != '\0')) {
1262 if (!(dev->si_flags & flag)) {
1263 devfs_destroy_dev_worker(dev);
1272 * Worker function that creates all device nodes on top of a devfs
1276 devfs_create_all_dev_worker(struct devfs_node *root)
1282 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1283 devfs_create_device_node(root, dev, NULL, NULL);
1290 * Worker function that destroys all devices that match a specific
1291 * dev_ops and/or minor. If minor is less than 0, it is not matched
1292 * against. It also propagates all changes.
1295 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1301 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1302 if (dev->si_ops != ops)
1304 if ((minor < 0) || (dev->si_uminor == minor)) {
1305 devfs_destroy_dev_worker(dev);
1313 * Worker function that registers a new clone handler in devfs.
1316 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler)
1318 struct devfs_clone_handler *chandler = NULL;
1319 u_char len = strlen(name);
1324 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1325 if (chandler->namlen != len)
1328 if (!memcmp(chandler->name, name, len)) {
1329 /* Clonable basename already exists */
1334 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1335 chandler->name = kstrdup(name, M_DEVFS);
1336 chandler->namlen = len;
1337 chandler->nhandler = nhandler;
1339 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1344 * Worker function that removes a given clone handler from the
1345 * clone handler list.
1348 devfs_chandler_del_worker(const char *name)
1350 struct devfs_clone_handler *chandler, *chandler2;
1351 u_char len = strlen(name);
1356 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1357 if (chandler->namlen != len)
1359 if (memcmp(chandler->name, name, len))
1362 TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1363 kfree(chandler->name, M_DEVFS);
1364 kfree(chandler, M_DEVFS);
1372 * Worker function that finds a given device name and changes
1373 * the message received accordingly so that when replied to,
1374 * the answer is returned to the caller.
1377 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1379 struct devfs_alias *alias;
1381 cdev_t found = NULL;
1383 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1384 if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1389 if (found == NULL) {
1390 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1391 if (strcmp(devfs_msg->mdv_name, alias->name) == 0) {
1392 found = alias->dev_target;
1397 devfs_msg->mdv_cdev = found;
1403 * Worker function that finds a given device udev and changes
1404 * the message received accordingly so that when replied to,
1405 * the answer is returned to the caller.
1408 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg)
1411 cdev_t found = NULL;
1413 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1414 if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1419 devfs_msg->mdv_cdev = found;
1425 * Worker function that inserts a given alias into the
1426 * alias list, and propagates the alias to all mount
1430 devfs_make_alias_worker(struct devfs_alias *alias)
1432 struct devfs_alias *alias2;
1433 size_t len = strlen(alias->name);
1436 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1437 if (len != alias2->namlen)
1440 if (!memcmp(alias->name, alias2->name, len)) {
1448 * The alias doesn't exist yet, so we add it to the alias list
1450 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1451 devfs_alias_propagate(alias);
1453 devfs_debug(DEVFS_DEBUG_WARNING,
1454 "Warning: duplicate devfs_make_alias for %s\n",
1456 kfree(alias->name, M_DEVFS);
1457 kfree(alias, M_DEVFS);
1464 * Function that removes and frees all aliases.
1467 devfs_alias_reap(void)
1469 struct devfs_alias *alias, *alias2;
1471 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1472 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1473 kfree(alias, M_DEVFS);
1479 * Function that removes an alias matching a specific cdev and frees
1483 devfs_alias_remove(cdev_t dev)
1485 struct devfs_alias *alias, *alias2;
1487 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1488 if (alias->dev_target == dev) {
1489 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1490 kfree(alias, M_DEVFS);
1497 * This function propagates a new alias to all mount points.
1500 devfs_alias_propagate(struct devfs_alias *alias)
1502 struct devfs_mnt_data *mnt;
1504 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1505 devfs_alias_apply(mnt->root_node, alias);
1511 * This function is a recursive function iterating through
1512 * all device nodes in the topology and, if applicable,
1513 * creating the relevant alias for a device node.
1516 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1518 struct devfs_node *node1, *node2;
1520 KKASSERT(alias != NULL);
1522 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1523 if (node->nchildren > 2) {
1524 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1525 devfs_alias_apply(node1, alias);
1529 if (node->d_dev == alias->dev_target)
1530 devfs_alias_create(alias->name, node, 0);
1536 * This function checks if any alias possibly is applicable
1537 * to the given node. If so, the alias is created.
1540 devfs_alias_check_create(struct devfs_node *node)
1542 struct devfs_alias *alias;
1544 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1545 if (node->d_dev == alias->dev_target)
1546 devfs_alias_create(alias->name, node, 0);
1552 * This function creates an alias with a given name
1553 * linking to a given devfs node. It also increments
1554 * the link count on the target node.
1557 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based)
1559 struct mount *mp = target->mp;
1560 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1561 struct devfs_node *linknode;
1562 struct hotplug_device *hpdev;
1563 char *create_path = NULL;
1568 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1570 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1571 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1574 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1577 if (devfs_find_device_node_by_name(parent, name)) {
1578 devfs_debug(DEVFS_DEBUG_WARNING,
1579 "Node already exists: %s "
1580 "(devfs_make_alias_worker)!\n",
1586 linknode = devfs_allocp(Plink, name, parent, mp, NULL);
1587 if (linknode == NULL) {
1592 linknode->link_target = target;
1596 linknode->flags |= DEVFS_RULE_CREATED;
1599 /* hotplug handler */
1600 if(devfs_node_added) {
1601 hpdev = kmalloc(sizeof(struct hotplug_device), M_TEMP, M_WAITOK);
1602 hpdev->dev = target->d_dev;
1603 hpdev->name = name_orig;
1604 devfs_node_added(hpdev);
1605 kfree(hpdev, M_TEMP);
1607 kfree(name_buf, M_TEMP);
1612 * This function is called by the core and handles mount point
1613 * strings. It either calls the relevant worker (devfs_apply_
1614 * reset_rules_worker) on all mountpoints or only a specific
1618 devfs_apply_reset_rules_caller(char *mountto, int apply)
1620 struct devfs_mnt_data *mnt;
1622 if (mountto[0] == '*') {
1623 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1624 devfs_iterate_topology(mnt->root_node,
1625 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1629 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1630 if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) {
1631 devfs_iterate_topology(mnt->root_node,
1632 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1639 kfree(mountto, M_DEVFS);
1644 * This function calls a given callback function for
1645 * every dev node in the devfs dev list.
1648 devfs_scan_callback_worker(devfs_scan_t *callback)
1652 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1660 * This function tries to resolve a given directory, or if not
1661 * found and creation requested, creates the given directory.
1663 static struct devfs_node *
1664 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1665 size_t name_len, int create)
1667 struct devfs_node *node, *found = NULL;
1669 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1670 if (name_len != node->d_dir.d_namlen)
1673 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1679 if ((found == NULL) && (create)) {
1680 found = devfs_allocp(Pdir, dir_name, parent, parent->mp, NULL);
1687 * This function tries to resolve a complete path. If creation is requested,
1688 * if a given part of the path cannot be resolved (because it doesn't exist),
1692 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1694 struct devfs_node *node = parent;
1701 buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1703 while (*path && idx < PATH_MAX - 1) {
1708 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1718 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1719 kfree (buf, M_TEMP);
1724 * Takes a full path and strips it into a directory path and a name.
1725 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
1726 * requires a working buffer with enough size to keep the whole
1730 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
1734 size_t len = strlen(fullpath) + 1;
1737 KKASSERT((fullpath != NULL) && (buf != NULL));
1738 KKASSERT((pathp != NULL) && (namep != NULL));
1740 memcpy(buf, fullpath, len);
1742 for (i = len-1; i>= 0; i--) {
1743 if (buf[i] == '/') {
1763 * This function creates a new devfs node for a given device. It can
1764 * handle a complete path as device name, and accordingly creates
1765 * the path and the final device node.
1767 * The reference count on the passed dev remains unchanged.
1770 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
1771 char *dev_name, char *path_fmt, ...)
1773 struct devfs_node *parent, *node = NULL;
1774 struct hotplug_device *hpdev;
1780 char *create_path = NULL;
1781 char *names = "pqrsPQRS";
1783 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1785 if (path_fmt != NULL) {
1786 __va_start(ap, path_fmt);
1787 kvasnrprintf(&path, PATH_MAX, 10, path_fmt, ap);
1791 parent = devfs_resolve_or_create_path(root, path, 1);
1794 devfs_resolve_name_path(
1795 ((dev_name == NULL) && (dev))?(dev->si_name):(dev_name),
1796 name_buf, &create_path, &name);
1799 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1802 if (devfs_find_device_node_by_name(parent, name)) {
1803 devfs_debug(DEVFS_DEBUG_WARNING, "devfs_create_device_node: "
1804 "DEVICE %s ALREADY EXISTS!!! Ignoring creation request.\n", name);
1808 node = devfs_allocp(Pdev, name, parent, parent->mp, dev);
1809 nanotime(&parent->mtime);
1812 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
1815 if ((dev) && (strlen(dev->si_name) >= 4) &&
1816 (!memcmp(dev->si_name, "ptm/", 4))) {
1817 node->parent->flags |= DEVFS_HIDDEN;
1818 node->flags |= DEVFS_HIDDEN;
1822 * Ugly pty magic, to tag pty devices as such and hide them if needed.
1824 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
1825 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1827 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
1829 for (i = 0; i < strlen(names); i++) {
1830 if (name[3] == names[i]) {
1836 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1838 /* hotplug handler */
1839 if(devfs_node_added) {
1840 hpdev = kmalloc(sizeof(struct hotplug_device), M_TEMP, M_WAITOK);
1841 hpdev->dev = node->d_dev;
1842 hpdev->name = node->d_dev->si_name;
1843 devfs_node_added(hpdev);
1844 kfree(hpdev, M_TEMP);
1848 kfree(name_buf, M_TEMP);
1854 * This function finds a given device node in the topology with a given
1858 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target)
1860 if ((node->node_type == Pdev) && (node->d_dev == target)) {
1868 * This function finds a device node in the given parent directory by its
1869 * name and returns it.
1872 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
1874 struct devfs_node *node, *found = NULL;
1875 size_t len = strlen(target);
1877 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1878 if (len != node->d_dir.d_namlen)
1881 if (!memcmp(node->d_dir.d_name, target, len)) {
1891 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop)
1893 struct vnode *vp = NULL;
1894 ino_t target = *inop;
1896 if (node->d_dir.d_ino == target) {
1899 vget(vp, LK_EXCLUSIVE | LK_RETRY);
1902 devfs_allocv(&vp, node);
1911 * This function takes a cdev and removes its devfs node in the
1912 * given topology. The cdev remains intact.
1915 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
1917 struct devfs_node *node, *parent;
1920 char *create_path = NULL;
1924 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1925 ksnprintf(name_buf, PATH_MAX, "%s", target->si_name);
1927 devfs_resolve_name_path(target->si_name, name_buf, &create_path, &name);
1930 parent = devfs_resolve_or_create_path(root, create_path, 0);
1934 if (parent == NULL) {
1935 kfree(name_buf, M_TEMP);
1939 node = devfs_find_device_node_by_name(parent, name);
1942 nanotime(&node->parent->mtime);
1946 kfree(name_buf, M_TEMP);
1952 * Just set perms and ownership for given node.
1955 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid,
1956 u_short mode, u_long flags)
1966 * Propagates a device attach/detach to all mount
1967 * points. Also takes care of automatic alias removal
1968 * for a deleted cdev.
1971 devfs_propagate_dev(cdev_t dev, int attach)
1973 struct devfs_mnt_data *mnt;
1975 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1977 /* Device is being attached */
1978 devfs_create_device_node(mnt->root_node, dev,
1981 /* Device is being detached */
1982 devfs_alias_remove(dev);
1983 devfs_destroy_device_node(mnt->root_node, dev);
1990 * devfs_clone either returns a basename from a complete name by
1991 * returning the length of the name without trailing digits, or,
1992 * if clone != 0, calls the device's clone handler to get a new
1993 * device, which in turn is returned in devp.
1996 devfs_clone(cdev_t dev, const char *name, size_t len, int mode,
2000 struct devfs_clone_handler *chandler;
2001 struct dev_clone_args ap;
2003 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
2004 if (chandler->namlen != len)
2006 if ((!memcmp(chandler->name, name, len)) && (chandler->nhandler)) {
2007 lockmgr(&devfs_lock, LK_RELEASE);
2009 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2011 ap.a_head.a_dev = dev;
2017 error = (chandler->nhandler)(&ap);
2030 * Registers a new orphan in the orphan list.
2033 devfs_tracer_add_orphan(struct devfs_node *node)
2035 struct devfs_orphan *orphan;
2038 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2039 orphan->node = node;
2041 KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2042 node->flags |= DEVFS_ORPHANED;
2043 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2047 * Removes an orphan from the orphan list.
2050 devfs_tracer_del_orphan(struct devfs_node *node)
2052 struct devfs_orphan *orphan;
2056 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) {
2057 if (orphan->node == node) {
2058 node->flags &= ~DEVFS_ORPHANED;
2059 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2060 kfree(orphan, M_DEVFS);
2067 * Counts the orphans in the orphan list, and if cleanup
2068 * is specified, also frees the orphan and removes it from
2072 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2074 struct devfs_orphan *orphan, *orphan2;
2077 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) {
2080 * If we are instructed to clean up, we do so.
2083 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2084 orphan->node->flags &= ~DEVFS_ORPHANED;
2085 devfs_freep(orphan->node);
2086 kfree(orphan, M_DEVFS);
2094 * Fetch an ino_t from the global d_ino by increasing it
2098 devfs_fetch_ino(void)
2102 spin_lock_wr(&ino_lock);
2104 spin_unlock_wr(&ino_lock);
2110 * Allocates a new cdev and initializes it's most basic
2114 devfs_new_cdev(struct dev_ops *ops, int minor, struct dev_ops *bops)
2116 cdev_t dev = sysref_alloc(&cdev_sysref_class);
2118 sysref_activate(&dev->si_sysref);
2120 bzero(dev, offsetof(struct cdev, si_sysref));
2125 dev->si_drv1 = NULL;
2126 dev->si_drv2 = NULL;
2127 dev->si_lastread = 0; /* time_second */
2128 dev->si_lastwrite = 0; /* time_second */
2133 dev->si_uminor = minor;
2134 dev->si_bops = bops;
2135 /* If there is a backing device, we reference its ops */
2136 dev->si_inode = makeudev(
2137 devfs_reference_ops((bops)?(bops):(ops)),
2144 devfs_cdev_terminate(cdev_t dev)
2148 /* Check if it is locked already. if not, we acquire the devfs lock */
2149 if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) {
2150 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2154 /* Propagate destruction, just in case */
2155 devfs_propagate_dev(dev, 0);
2157 /* If we acquired the lock, we also get rid of it */
2159 lockmgr(&devfs_lock, LK_RELEASE);
2161 /* If there is a backing device, we release the backing device's ops */
2162 devfs_release_ops((dev->si_bops)?(dev->si_bops):(dev->si_ops));
2164 /* Finally destroy the device */
2165 sysref_put(&dev->si_sysref);
2169 * Dummies for now (individual locks for MPSAFE)
2172 devfs_cdev_lock(cdev_t dev)
2177 devfs_cdev_unlock(cdev_t dev)
2182 * Links a given cdev into the dev list.
2185 devfs_link_dev(cdev_t dev)
2187 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2188 dev->si_flags |= SI_DEVFS_LINKED;
2189 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2195 * Removes a given cdev from the dev list. The caller is responsible for
2196 * releasing the reference on the device associated with the linkage.
2198 * Returns EALREADY if the dev has already been unlinked.
2201 devfs_unlink_dev(cdev_t dev)
2203 if ((dev->si_flags & SI_DEVFS_LINKED)) {
2204 TAILQ_REMOVE(&devfs_dev_list, dev, link);
2205 dev->si_flags &= ~SI_DEVFS_LINKED;
2212 devfs_node_is_accessible(struct devfs_node *node)
2214 if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2221 devfs_reference_ops(struct dev_ops *ops)
2224 struct devfs_dev_ops *found = NULL;
2225 struct devfs_dev_ops *devops;
2227 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2228 if (devops->ops == ops) {
2235 found = kmalloc(sizeof(struct devfs_dev_ops), M_DEVFS, M_WAITOK);
2237 found->ref_count = 0;
2238 TAILQ_INSERT_TAIL(&devfs_dev_ops_list, found, link);
2243 if (found->ref_count == 0) {
2244 found->id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255);
2245 if (found->id == -1) {
2246 /* Ran out of unique ids */
2247 devfs_debug(DEVFS_DEBUG_WARNING,
2248 "devfs_reference_ops: WARNING: ran out of unique ids\n");
2258 devfs_release_ops(struct dev_ops *ops)
2260 struct devfs_dev_ops *found = NULL;
2261 struct devfs_dev_ops *devops;
2263 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2264 if (devops->ops == ops) {
2274 if (found->ref_count == 0) {
2275 TAILQ_REMOVE(&devfs_dev_ops_list, found, link);
2276 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), found->id);
2277 kfree(found, M_DEVFS);
2286 msg = devfs_msg_get();
2287 msg = devfs_msg_send_sync(DEVFS_SYNC, msg);
2292 * Called on init of devfs; creates the objcaches and
2293 * spawns off the devfs core thread. Also initializes
2299 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2300 /* Create objcaches for nodes, msgs and devs */
2301 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2303 objcache_malloc_alloc,
2304 objcache_malloc_free,
2305 &devfs_node_malloc_args );
2307 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2309 objcache_malloc_alloc,
2310 objcache_malloc_free,
2311 &devfs_msg_malloc_args );
2313 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2315 objcache_malloc_alloc,
2316 objcache_malloc_free,
2317 &devfs_dev_malloc_args );
2319 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2321 /* Initialize the reply-only port which acts as a message drain */
2322 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2324 /* Initialize *THE* devfs lock */
2325 lockinit(&devfs_lock, "devfs_core lock", 0, 0);
2328 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2329 0, 0, "devfs_msg_core");
2331 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2333 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2337 * Called on unload of devfs; takes care of destroying the core
2338 * and the objcaches. Also removes aliases that are no longer needed.
2343 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2345 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2347 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2348 tsleep(td_core/*devfs_id*/, 0, "devfsc", 10000);
2350 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2352 /* Destroy the objcaches */
2353 objcache_destroy(devfs_msg_cache);
2354 objcache_destroy(devfs_node_cache);
2355 objcache_destroy(devfs_dev_cache);
2361 * This is a sysctl handler to assist userland devname(3) to
2362 * find the device name for a given udev.
2365 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2372 if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t))))
2375 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev);
2380 if ((found = devfs_find_device_by_udev(udev)) == NULL)
2383 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2387 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
2388 NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)");
2390 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2391 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2392 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable,
2393 0, "Enable DevFS debugging");
2395 SYSINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST,
2397 SYSUNINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY,
2398 devfs_uninit, NULL);