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/sysctl.h>
43 #include <sys/ucred.h>
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/devfs.h>
47 #include <sys/devfs_rules.h>
50 #include <sys/msgport2.h>
51 #include <sys/spinlock2.h>
52 #include <sys/mplock2.h>
53 #include <sys/sysref2.h>
55 MALLOC_DEFINE(M_DEVFS, "devfs", "Device File System (devfs) allocations");
56 DEVFS_DECLARE_CLONE_BITMAP(ops_id);
58 * SYSREF Integration - reference counting, allocation,
59 * sysid and syslink integration.
61 static void devfs_cdev_terminate(cdev_t dev);
62 static void devfs_cdev_lock(cdev_t dev);
63 static void devfs_cdev_unlock(cdev_t dev);
64 static struct sysref_class cdev_sysref_class = {
67 .proto = SYSREF_PROTO_DEV,
68 .offset = offsetof(struct cdev, si_sysref),
69 .objsize = sizeof(struct cdev),
73 .terminate = (sysref_terminate_func_t)devfs_cdev_terminate,
74 .lock = (sysref_lock_func_t)devfs_cdev_lock,
75 .unlock = (sysref_unlock_func_t)devfs_cdev_unlock
79 static struct objcache *devfs_node_cache;
80 static struct objcache *devfs_msg_cache;
81 static struct objcache *devfs_dev_cache;
83 static struct objcache_malloc_args devfs_node_malloc_args = {
84 sizeof(struct devfs_node), M_DEVFS };
85 struct objcache_malloc_args devfs_msg_malloc_args = {
86 sizeof(struct devfs_msg), M_DEVFS };
87 struct objcache_malloc_args devfs_dev_malloc_args = {
88 sizeof(struct cdev), M_DEVFS };
90 static struct devfs_dev_head devfs_dev_list =
91 TAILQ_HEAD_INITIALIZER(devfs_dev_list);
92 static struct devfs_mnt_head devfs_mnt_list =
93 TAILQ_HEAD_INITIALIZER(devfs_mnt_list);
94 static struct devfs_chandler_head devfs_chandler_list =
95 TAILQ_HEAD_INITIALIZER(devfs_chandler_list);
96 static struct devfs_alias_head devfs_alias_list =
97 TAILQ_HEAD_INITIALIZER(devfs_alias_list);
98 static struct devfs_dev_ops_head devfs_dev_ops_list =
99 TAILQ_HEAD_INITIALIZER(devfs_dev_ops_list);
101 struct lock devfs_lock;
102 static struct lwkt_port devfs_dispose_port;
103 static struct lwkt_port devfs_msg_port;
104 static struct thread *td_core;
106 static struct spinlock ino_lock;
108 static int devfs_debug_enable;
109 static int devfs_run;
111 static ino_t devfs_fetch_ino(void);
112 static int devfs_create_all_dev_worker(struct devfs_node *);
113 static int devfs_create_dev_worker(cdev_t, uid_t, gid_t, int);
114 static int devfs_destroy_dev_worker(cdev_t);
115 static int devfs_destroy_subnames_worker(char *);
116 static int devfs_destroy_dev_by_ops_worker(struct dev_ops *, int);
117 static int devfs_propagate_dev(cdev_t, int);
118 static int devfs_unlink_dev(cdev_t dev);
119 static void devfs_msg_exec(devfs_msg_t msg);
121 static int devfs_chandler_add_worker(const char *, d_clone_t *);
122 static int devfs_chandler_del_worker(const char *);
124 static void devfs_msg_autofree_reply(lwkt_port_t, lwkt_msg_t);
125 static void devfs_msg_core(void *);
127 static int devfs_find_device_by_name_worker(devfs_msg_t);
128 static int devfs_find_device_by_udev_worker(devfs_msg_t);
130 static int devfs_apply_reset_rules_caller(char *, int);
132 static int devfs_scan_callback_worker(devfs_scan_t *, void *);
134 static struct devfs_node *devfs_resolve_or_create_dir(struct devfs_node *,
135 char *, size_t, int);
137 static int devfs_make_alias_worker(struct devfs_alias *);
138 static int devfs_destroy_alias_worker(struct devfs_alias *);
139 static int devfs_alias_remove(cdev_t);
140 static int devfs_alias_reap(void);
141 static int devfs_alias_propagate(struct devfs_alias *, int);
142 static int devfs_alias_apply(struct devfs_node *, struct devfs_alias *);
143 static int devfs_alias_check_create(struct devfs_node *);
145 static int devfs_clr_subnames_flag_worker(char *, uint32_t);
146 static int devfs_destroy_subnames_without_flag_worker(char *, uint32_t);
148 static void *devfs_reaperp_callback(struct devfs_node *, void *);
149 static void *devfs_gc_dirs_callback(struct devfs_node *, void *);
150 static void *devfs_gc_links_callback(struct devfs_node *, struct devfs_node *);
152 devfs_inode_to_vnode_worker_callback(struct devfs_node *, ino_t *);
155 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function
159 devfs_debug(int level, char *fmt, ...)
164 if (level <= devfs_debug_enable)
172 * devfs_allocp() Allocates a new devfs node with the specified
173 * parameters. The node is also automatically linked into the topology
174 * if a parent is specified. It also calls the rule and alias stuff to
175 * be applied on the new node
178 devfs_allocp(devfs_nodetype devfsnodetype, char *name,
179 struct devfs_node *parent, struct mount *mp, cdev_t dev)
181 struct devfs_node *node = NULL;
182 size_t namlen = strlen(name);
184 node = objcache_get(devfs_node_cache, M_WAITOK);
185 bzero(node, sizeof(*node));
187 atomic_add_long(&DEVFS_MNTDATA(mp)->leak_count, 1);
192 node->d_dir.d_ino = devfs_fetch_ino();
195 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries
198 node->cookie_jar = 2;
201 * Access Control members
203 node->mode = DEVFS_DEFAULT_MODE;
204 node->uid = DEVFS_DEFAULT_UID;
205 node->gid = DEVFS_DEFAULT_GID;
207 switch (devfsnodetype) {
210 * Ensure that we don't recycle the root vnode by marking it as
211 * linked into the topology.
213 node->flags |= DEVFS_NODE_LINKED;
215 TAILQ_INIT(DEVFS_DENODE_HEAD(node));
216 node->d_dir.d_type = DT_DIR;
221 node->d_dir.d_type = DT_LNK;
225 node->d_dir.d_type = DT_REG;
230 node->d_dir.d_type = DT_CHR;
233 node->mode = dev->si_perms;
234 node->uid = dev->si_uid;
235 node->gid = dev->si_gid;
237 devfs_alias_check_create(node);
242 panic("devfs_allocp: unknown node type");
246 node->node_type = devfsnodetype;
248 /* Initialize the dirent structure of each devfs vnode */
249 node->d_dir.d_namlen = namlen;
250 node->d_dir.d_name = kmalloc(namlen+1, M_DEVFS, M_WAITOK);
251 memcpy(node->d_dir.d_name, name, namlen);
252 node->d_dir.d_name[namlen] = '\0';
254 /* Initialize the parent node element */
255 node->parent = parent;
257 /* Initialize *time members */
258 nanotime(&node->atime);
259 node->mtime = node->ctime = node->atime;
262 * Associate with parent as last step, clean out namecache
265 if ((parent != NULL) &&
266 ((parent->node_type == Proot) || (parent->node_type == Pdir))) {
268 node->cookie = parent->cookie_jar++;
269 node->flags |= DEVFS_NODE_LINKED;
270 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent), node, link);
272 /* This forces negative namecache lookups to clear */
273 ++mp->mnt_namecache_gen;
277 devfs_rule_check_apply(node, NULL);
279 atomic_add_long(&DEVFS_MNTDATA(mp)->file_count, 1);
285 * devfs_allocv() allocates a new vnode based on a devfs node.
288 devfs_allocv(struct vnode **vpp, struct devfs_node *node)
296 while ((vp = node->v_node) != NULL) {
297 error = vget(vp, LK_EXCLUSIVE);
298 if (error != ENOENT) {
304 if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0)
309 if (node->v_node != NULL) {
318 switch (node->node_type) {
320 vsetflags(vp, VROOT);
336 KKASSERT(node->d_dev);
338 vp->v_uminor = node->d_dev->si_uminor;
341 v_associate_rdev(vp, node->d_dev);
342 vp->v_ops = &node->mp->mnt_vn_spec_ops;
346 panic("devfs_allocv: unknown node type");
354 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
355 * based on the newly created devfs node.
358 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype,
359 char *name, struct devfs_node *parent, cdev_t dev)
361 struct devfs_node *node;
363 node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
366 devfs_allocv(vpp, node);
374 * Destroy the devfs_node. The node must be unlinked from the topology.
376 * This function will also destroy any vnode association with the node
379 * The cdev_t itself remains intact.
381 * The core lock is not necessarily held on call and must be temporarily
382 * released if it is to avoid a deadlock.
385 devfs_freep(struct devfs_node *node)
391 KKASSERT(((node->flags & DEVFS_NODE_LINKED) == 0) ||
392 (node->node_type == Proot));
395 * Protect against double frees
397 KKASSERT((node->flags & DEVFS_DESTROYED) == 0);
398 node->flags |= DEVFS_DESTROYED;
401 * Avoid deadlocks between devfs_lock and the vnode lock when
402 * disassociating the vnode (stress2 pty vs ls -la /dev/pts).
404 * This also prevents the vnode reclaim code from double-freeing
405 * the node. The vget() is required to safely modified the vp
406 * and cycle the refs to terminate an inactive vp.
408 if (lockstatus(&devfs_lock, curthread) == LK_EXCLUSIVE) {
409 lockmgr(&devfs_lock, LK_RELEASE);
415 while ((vp = node->v_node) != NULL) {
416 if (vget(vp, LK_EXCLUSIVE | LK_RETRY) != 0)
421 cache_inval_vp(vp, CINV_DESTROY);
428 atomic_subtract_long(&DEVFS_MNTDATA(node->mp)->leak_count, 1);
429 if (node->symlink_name) {
430 kfree(node->symlink_name, M_DEVFS);
431 node->symlink_name = NULL;
435 * Remove the node from the orphan list if it is still on it.
437 if (node->flags & DEVFS_ORPHANED)
438 devfs_tracer_del_orphan(node);
440 if (node->d_dir.d_name) {
441 kfree(node->d_dir.d_name, M_DEVFS);
442 node->d_dir.d_name = NULL;
444 atomic_subtract_long(&DEVFS_MNTDATA(node->mp)->file_count, 1);
445 objcache_put(devfs_node_cache, node);
448 lockmgr(&devfs_lock, LK_EXCLUSIVE);
454 * Unlink the devfs node from the topology and add it to the orphan list.
455 * The node will later be destroyed by freep.
457 * Any vnode association, including the v_rdev and v_data, remains intact
461 devfs_unlinkp(struct devfs_node *node)
463 struct devfs_node *parent;
467 * Add the node to the orphan list, so it is referenced somewhere, to
468 * so we don't leak it.
470 devfs_tracer_add_orphan(node);
472 parent = node->parent;
475 * If the parent is known we can unlink the node out of the topology
478 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
480 node->flags &= ~DEVFS_NODE_LINKED;
488 devfs_iterate_topology(struct devfs_node *node,
489 devfs_iterate_callback_t *callback, void *arg1)
491 struct devfs_node *node1, *node2;
494 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
495 if (node->nchildren > 2) {
496 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
498 if ((ret = devfs_iterate_topology(node1, callback, arg1)))
504 ret = callback(node, arg1);
509 * devfs_reaperp() is a recursive function that iterates through all the
510 * topology, unlinking and freeing all devfs nodes.
513 devfs_reaperp_callback(struct devfs_node *node, void *unused)
522 devfs_gc_dirs_callback(struct devfs_node *node, void *unused)
524 if (node->node_type == Pdir) {
525 if ((node->nchildren == 2) &&
526 !(node->flags & DEVFS_USER_CREATED)) {
536 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target)
538 if ((node->node_type == Plink) && (node->link_target == target)) {
547 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
548 * freeing a node, but also removes empty directories and links that link
549 * via devfs auto-link mechanism to the node being deleted.
552 devfs_gc(struct devfs_node *node)
554 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
556 if (node->nlinks > 0)
557 devfs_iterate_topology(root_node,
558 (devfs_iterate_callback_t *)devfs_gc_links_callback, node);
561 devfs_iterate_topology(root_node,
562 (devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL);
570 * devfs_create_dev() is the asynchronous entry point for device creation.
571 * It just sends a message with the relevant details to the devfs core.
573 * This function will reference the passed device. The reference is owned
574 * by devfs and represents all of the device's node associations.
577 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
580 devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
586 * devfs_destroy_dev() is the asynchronous entry point for device destruction.
587 * It just sends a message with the relevant details to the devfs core.
590 devfs_destroy_dev(cdev_t dev)
592 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
597 * devfs_mount_add() is the synchronous entry point for adding a new devfs
598 * mount. It sends a synchronous message with the relevant details to the
602 devfs_mount_add(struct devfs_mnt_data *mnt)
606 msg = devfs_msg_get();
608 msg = devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
615 * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
616 * It sends a synchronous message with the relevant details to the devfs core.
619 devfs_mount_del(struct devfs_mnt_data *mnt)
623 msg = devfs_msg_get();
625 msg = devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
632 * devfs_destroy_subnames() is the synchronous entry point for device
633 * destruction by subname. It just sends a message with the relevant details to
637 devfs_destroy_subnames(char *name)
641 msg = devfs_msg_get();
642 msg->mdv_load = name;
643 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES, msg);
649 devfs_clr_subnames_flag(char *name, uint32_t flag)
653 msg = devfs_msg_get();
654 msg->mdv_flags.name = name;
655 msg->mdv_flags.flag = flag;
656 msg = devfs_msg_send_sync(DEVFS_CLR_SUBNAMES_FLAG, msg);
663 devfs_destroy_subnames_without_flag(char *name, uint32_t flag)
667 msg = devfs_msg_get();
668 msg->mdv_flags.name = name;
669 msg->mdv_flags.flag = flag;
670 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES_WO_FLAG, msg);
677 * devfs_create_all_dev is the asynchronous entry point to trigger device
678 * node creation. It just sends a message with the relevant details to
682 devfs_create_all_dev(struct devfs_node *root)
684 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
689 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
690 * devices with a specific set of dev_ops and minor. It just sends a
691 * message with the relevant details to the devfs core.
694 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
696 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
701 * devfs_clone_handler_add is the synchronous entry point to add a new
702 * clone handler. It just sends a message with the relevant details to
706 devfs_clone_handler_add(const char *name, d_clone_t *nhandler)
710 msg = devfs_msg_get();
711 msg->mdv_chandler.name = name;
712 msg->mdv_chandler.nhandler = nhandler;
713 msg = devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg);
719 * devfs_clone_handler_del is the synchronous entry point to remove a
720 * clone handler. It just sends a message with the relevant details to
724 devfs_clone_handler_del(const char *name)
728 msg = devfs_msg_get();
729 msg->mdv_chandler.name = name;
730 msg->mdv_chandler.nhandler = NULL;
731 msg = devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg);
737 * devfs_find_device_by_name is the synchronous entry point to find a
738 * device given its name. It sends a synchronous message with the
739 * relevant details to the devfs core and returns the answer.
742 devfs_find_device_by_name(const char *fmt, ...)
753 kvasnrprintf(&target, PATH_MAX, 10, fmt, ap);
756 msg = devfs_msg_get();
757 msg->mdv_name = target;
758 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
759 found = msg->mdv_cdev;
767 * devfs_find_device_by_udev is the synchronous entry point to find a
768 * device given its udev number. It sends a synchronous message with
769 * the relevant details to the devfs core and returns the answer.
772 devfs_find_device_by_udev(udev_t udev)
777 msg = devfs_msg_get();
778 msg->mdv_udev = udev;
779 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg);
780 found = msg->mdv_cdev;
783 devfs_debug(DEVFS_DEBUG_DEBUG,
784 "devfs_find_device_by_udev found? %s -end:3-\n",
785 ((found) ? found->si_name:"NO"));
790 devfs_inode_to_vnode(struct mount *mp, ino_t target)
792 struct vnode *vp = NULL;
798 msg = devfs_msg_get();
799 msg->mdv_ino.mp = mp;
800 msg->mdv_ino.ino = target;
801 msg = devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg);
802 vp = msg->mdv_ino.vp;
803 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
810 * devfs_make_alias is the asynchronous entry point to register an alias
811 * for a device. It just sends a message with the relevant details to the
815 devfs_make_alias(const char *name, cdev_t dev_target)
817 struct devfs_alias *alias;
822 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
823 alias->name = kstrdup(name, M_DEVFS);
825 alias->dev_target = dev_target;
827 devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
832 * devfs_destroy_alias is the asynchronous entry point to deregister an alias
833 * for a device. It just sends a message with the relevant details to the
837 devfs_destroy_alias(const char *name, cdev_t dev_target)
839 struct devfs_alias *alias;
844 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
845 alias->name = kstrdup(name, M_DEVFS);
847 alias->dev_target = dev_target;
849 devfs_msg_send_generic(DEVFS_DESTROY_ALIAS, alias);
854 * devfs_apply_rules is the asynchronous entry point to trigger application
855 * of all rules. It just sends a message with the relevant details to the
859 devfs_apply_rules(char *mntto)
863 new_name = kstrdup(mntto, M_DEVFS);
864 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
870 * devfs_reset_rules is the asynchronous entry point to trigger reset of all
871 * rules. It just sends a message with the relevant details to the devfs core.
874 devfs_reset_rules(char *mntto)
878 new_name = kstrdup(mntto, M_DEVFS);
879 devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
886 * devfs_scan_callback is the asynchronous entry point to call a callback
888 * It just sends a message with the relevant details to the devfs core.
891 devfs_scan_callback(devfs_scan_t *callback, void *arg)
895 KKASSERT(sizeof(callback) == sizeof(void *));
897 msg = devfs_msg_get();
898 msg->mdv_load = callback;
899 msg->mdv_load2 = arg;
900 msg = devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
908 * Acts as a message drain. Any message that is replied to here gets destroyed
909 * and the memory freed.
912 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
914 devfs_msg_put((devfs_msg_t)msg);
918 * devfs_msg_get allocates a new devfs msg and returns it.
923 return objcache_get(devfs_msg_cache, M_WAITOK);
927 * devfs_msg_put deallocates a given devfs msg.
930 devfs_msg_put(devfs_msg_t msg)
932 objcache_put(devfs_msg_cache, msg);
937 * devfs_msg_send is the generic asynchronous message sending facility
938 * for devfs. By default the reply port is the automatic disposal port.
940 * If the current thread is the devfs_msg_port thread we execute the
941 * operation synchronously.
944 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
946 lwkt_port_t port = &devfs_msg_port;
948 lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
950 devfs_msg->hdr.u.ms_result = cmd;
952 if (port->mpu_td == curthread) {
953 devfs_msg_exec(devfs_msg);
954 lwkt_replymsg(&devfs_msg->hdr, 0);
956 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
961 * devfs_msg_send_sync is the generic synchronous message sending
962 * facility for devfs. It initializes a local reply port and waits
963 * for the core's answer. This answer is then returned.
966 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
968 struct lwkt_port rep_port;
969 devfs_msg_t msg_incoming;
970 lwkt_port_t port = &devfs_msg_port;
972 lwkt_initport_thread(&rep_port, curthread);
973 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
975 devfs_msg->hdr.u.ms_result = cmd;
977 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
978 msg_incoming = lwkt_waitport(&rep_port, 0);
984 * sends a message with a generic argument.
987 devfs_msg_send_generic(uint32_t cmd, void *load)
989 devfs_msg_t devfs_msg = devfs_msg_get();
991 devfs_msg->mdv_load = load;
992 devfs_msg_send(cmd, devfs_msg);
996 * sends a message with a name argument.
999 devfs_msg_send_name(uint32_t cmd, char *name)
1001 devfs_msg_t devfs_msg = devfs_msg_get();
1003 devfs_msg->mdv_name = name;
1004 devfs_msg_send(cmd, devfs_msg);
1008 * sends a message with a mount argument.
1011 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
1013 devfs_msg_t devfs_msg = devfs_msg_get();
1015 devfs_msg->mdv_mnt = mnt;
1016 devfs_msg_send(cmd, devfs_msg);
1020 * sends a message with an ops argument.
1023 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
1025 devfs_msg_t devfs_msg = devfs_msg_get();
1027 devfs_msg->mdv_ops.ops = ops;
1028 devfs_msg->mdv_ops.minor = minor;
1029 devfs_msg_send(cmd, devfs_msg);
1033 * sends a message with a clone handler argument.
1036 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
1038 devfs_msg_t devfs_msg = devfs_msg_get();
1040 devfs_msg->mdv_chandler.name = name;
1041 devfs_msg->mdv_chandler.nhandler = handler;
1042 devfs_msg_send(cmd, devfs_msg);
1046 * sends a message with a device argument.
1049 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1051 devfs_msg_t devfs_msg = devfs_msg_get();
1053 devfs_msg->mdv_dev.dev = dev;
1054 devfs_msg->mdv_dev.uid = uid;
1055 devfs_msg->mdv_dev.gid = gid;
1056 devfs_msg->mdv_dev.perms = perms;
1058 devfs_msg_send(cmd, devfs_msg);
1062 * sends a message with a link argument.
1065 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1067 devfs_msg_t devfs_msg = devfs_msg_get();
1069 devfs_msg->mdv_link.name = name;
1070 devfs_msg->mdv_link.target = target;
1071 devfs_msg->mdv_link.mp = mp;
1072 devfs_msg_send(cmd, devfs_msg);
1076 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1077 * and calls the relevant worker functions. By using messages it's assured
1078 * that events occur in the correct order.
1081 devfs_msg_core(void *arg)
1085 lwkt_initport_thread(&devfs_msg_port, curthread);
1087 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1090 lockmgr(&devfs_lock, LK_RELEASE);
1092 get_mplock(); /* mpsafe yet? */
1095 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1096 devfs_debug(DEVFS_DEBUG_DEBUG,
1097 "devfs_msg_core, new msg: %x\n",
1098 (unsigned int)msg->hdr.u.ms_result);
1099 devfs_msg_exec(msg);
1100 lwkt_replymsg(&msg->hdr, 0);
1110 devfs_msg_exec(devfs_msg_t msg)
1112 struct devfs_mnt_data *mnt;
1113 struct devfs_node *node;
1117 * Acquire the devfs lock to ensure safety of all called functions
1119 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1121 switch (msg->hdr.u.ms_result) {
1122 case DEVFS_DEVICE_CREATE:
1123 dev = msg->mdv_dev.dev;
1124 devfs_create_dev_worker(dev,
1127 msg->mdv_dev.perms);
1129 case DEVFS_DEVICE_DESTROY:
1130 dev = msg->mdv_dev.dev;
1131 devfs_destroy_dev_worker(dev);
1133 case DEVFS_DESTROY_SUBNAMES:
1134 devfs_destroy_subnames_worker(msg->mdv_load);
1136 case DEVFS_DESTROY_DEV_BY_OPS:
1137 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1138 msg->mdv_ops.minor);
1140 case DEVFS_CREATE_ALL_DEV:
1141 node = (struct devfs_node *)msg->mdv_load;
1142 devfs_create_all_dev_worker(node);
1144 case DEVFS_MOUNT_ADD:
1146 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1147 devfs_create_all_dev_worker(mnt->root_node);
1149 case DEVFS_MOUNT_DEL:
1151 TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1152 devfs_iterate_topology(mnt->root_node, devfs_reaperp_callback,
1154 if (mnt->leak_count) {
1155 devfs_debug(DEVFS_DEBUG_SHOW,
1156 "Leaked %ld devfs_node elements!\n",
1160 case DEVFS_CHANDLER_ADD:
1161 devfs_chandler_add_worker(msg->mdv_chandler.name,
1162 msg->mdv_chandler.nhandler);
1164 case DEVFS_CHANDLER_DEL:
1165 devfs_chandler_del_worker(msg->mdv_chandler.name);
1167 case DEVFS_FIND_DEVICE_BY_NAME:
1168 devfs_find_device_by_name_worker(msg);
1170 case DEVFS_FIND_DEVICE_BY_UDEV:
1171 devfs_find_device_by_udev_worker(msg);
1173 case DEVFS_MAKE_ALIAS:
1174 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1176 case DEVFS_DESTROY_ALIAS:
1177 devfs_destroy_alias_worker((struct devfs_alias *)msg->mdv_load);
1179 case DEVFS_APPLY_RULES:
1180 devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1182 case DEVFS_RESET_RULES:
1183 devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1185 case DEVFS_SCAN_CALLBACK:
1186 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load,
1189 case DEVFS_CLR_SUBNAMES_FLAG:
1190 devfs_clr_subnames_flag_worker(msg->mdv_flags.name,
1191 msg->mdv_flags.flag);
1193 case DEVFS_DESTROY_SUBNAMES_WO_FLAG:
1194 devfs_destroy_subnames_without_flag_worker(msg->mdv_flags.name,
1195 msg->mdv_flags.flag);
1197 case DEVFS_INODE_TO_VNODE:
1198 msg->mdv_ino.vp = devfs_iterate_topology(
1199 DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node,
1200 (devfs_iterate_callback_t *)devfs_inode_to_vnode_worker_callback,
1203 case DEVFS_TERMINATE_CORE:
1209 devfs_debug(DEVFS_DEBUG_WARNING,
1210 "devfs_msg_core: unknown message "
1211 "received at core\n");
1214 lockmgr(&devfs_lock, LK_RELEASE);
1218 * Worker function to insert a new dev into the dev list and initialize its
1219 * permissions. It also calls devfs_propagate_dev which in turn propagates
1220 * the change to all mount points.
1222 * The passed dev is already referenced. This reference is eaten by this
1223 * function and represents the dev's linkage into devfs_dev_list.
1226 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1232 dev->si_perms = perms;
1234 devfs_link_dev(dev);
1235 devfs_propagate_dev(dev, 1);
1237 udev_event_attach(dev, NULL, 0);
1243 * Worker function to delete a dev from the dev list and free the cdev.
1244 * It also calls devfs_propagate_dev which in turn propagates the change
1245 * to all mount points.
1248 devfs_destroy_dev_worker(cdev_t dev)
1253 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1255 error = devfs_unlink_dev(dev);
1256 devfs_propagate_dev(dev, 0);
1258 udev_event_detach(dev, NULL, 0);
1261 release_dev(dev); /* link ref */
1269 * Worker function to destroy all devices with a certain basename.
1270 * Calls devfs_destroy_dev_worker for the actual destruction.
1273 devfs_destroy_subnames_worker(char *name)
1276 size_t len = strlen(name);
1278 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1279 if ((!strncmp(dev->si_name, name, len)) &&
1280 (dev->si_name[len] != '\0')) {
1281 devfs_destroy_dev_worker(dev);
1288 devfs_clr_subnames_flag_worker(char *name, uint32_t flag)
1291 size_t len = strlen(name);
1293 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1294 if ((!strncmp(dev->si_name, name, len)) &&
1295 (dev->si_name[len] != '\0')) {
1296 dev->si_flags &= ~flag;
1304 devfs_destroy_subnames_without_flag_worker(char *name, uint32_t flag)
1307 size_t len = strlen(name);
1309 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1310 if ((!strncmp(dev->si_name, name, len)) &&
1311 (dev->si_name[len] != '\0')) {
1312 if (!(dev->si_flags & flag)) {
1313 devfs_destroy_dev_worker(dev);
1322 * Worker function that creates all device nodes on top of a devfs
1326 devfs_create_all_dev_worker(struct devfs_node *root)
1332 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1333 devfs_create_device_node(root, dev, NULL, NULL);
1340 * Worker function that destroys all devices that match a specific
1341 * dev_ops and/or minor. If minor is less than 0, it is not matched
1342 * against. It also propagates all changes.
1345 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1351 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1352 if (dev->si_ops != ops)
1354 if ((minor < 0) || (dev->si_uminor == minor)) {
1355 devfs_destroy_dev_worker(dev);
1363 * Worker function that registers a new clone handler in devfs.
1366 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler)
1368 struct devfs_clone_handler *chandler = NULL;
1369 u_char len = strlen(name);
1374 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1375 if (chandler->namlen != len)
1378 if (!memcmp(chandler->name, name, len)) {
1379 /* Clonable basename already exists */
1384 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1385 chandler->name = kstrdup(name, M_DEVFS);
1386 chandler->namlen = len;
1387 chandler->nhandler = nhandler;
1389 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1394 * Worker function that removes a given clone handler from the
1395 * clone handler list.
1398 devfs_chandler_del_worker(const char *name)
1400 struct devfs_clone_handler *chandler, *chandler2;
1401 u_char len = strlen(name);
1406 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1407 if (chandler->namlen != len)
1409 if (memcmp(chandler->name, name, len))
1412 TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1413 kfree(chandler->name, M_DEVFS);
1414 kfree(chandler, M_DEVFS);
1422 * Worker function that finds a given device name and changes
1423 * the message received accordingly so that when replied to,
1424 * the answer is returned to the caller.
1427 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1429 struct devfs_alias *alias;
1431 cdev_t found = NULL;
1433 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1434 if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1439 if (found == NULL) {
1440 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1441 if (strcmp(devfs_msg->mdv_name, alias->name) == 0) {
1442 found = alias->dev_target;
1447 devfs_msg->mdv_cdev = found;
1453 * Worker function that finds a given device udev and changes
1454 * the message received accordingly so that when replied to,
1455 * the answer is returned to the caller.
1458 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg)
1461 cdev_t found = NULL;
1463 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1464 if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1469 devfs_msg->mdv_cdev = found;
1475 * Worker function that inserts a given alias into the
1476 * alias list, and propagates the alias to all mount
1480 devfs_make_alias_worker(struct devfs_alias *alias)
1482 struct devfs_alias *alias2;
1483 size_t len = strlen(alias->name);
1486 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1487 if (len != alias2->namlen)
1490 if (!memcmp(alias->name, alias2->name, len)) {
1498 * The alias doesn't exist yet, so we add it to the alias list
1500 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1501 devfs_alias_propagate(alias, 0);
1502 udev_event_attach(alias->dev_target, alias->name, 1);
1504 devfs_debug(DEVFS_DEBUG_WARNING,
1505 "Warning: duplicate devfs_make_alias for %s\n",
1507 kfree(alias->name, M_DEVFS);
1508 kfree(alias, M_DEVFS);
1515 * Worker function that delete a given alias from the
1516 * alias list, and propagates the removal to all mount
1520 devfs_destroy_alias_worker(struct devfs_alias *alias)
1522 struct devfs_alias *alias2;
1525 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1526 if (alias->dev_target != alias2->dev_target)
1529 if (devfs_WildCmp(alias->name, alias2->name) == 0) {
1536 devfs_debug(DEVFS_DEBUG_WARNING,
1537 "Warning: devfs_destroy_alias for inexistant alias: %s\n",
1539 kfree(alias->name, M_DEVFS);
1540 kfree(alias, M_DEVFS);
1543 * The alias exists, so we delete it from the alias list
1545 TAILQ_REMOVE(&devfs_alias_list, alias2, link);
1546 devfs_alias_propagate(alias2, 1);
1547 udev_event_detach(alias2->dev_target, alias2->name, 1);
1548 kfree(alias->name, M_DEVFS);
1549 kfree(alias, M_DEVFS);
1550 kfree(alias2->name, M_DEVFS);
1551 kfree(alias2, M_DEVFS);
1558 * Function that removes and frees all aliases.
1561 devfs_alias_reap(void)
1563 struct devfs_alias *alias, *alias2;
1565 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1566 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1567 kfree(alias->name, M_DEVFS);
1568 kfree(alias, M_DEVFS);
1574 * Function that removes an alias matching a specific cdev and frees
1578 devfs_alias_remove(cdev_t dev)
1580 struct devfs_alias *alias, *alias2;
1582 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1583 if (alias->dev_target == dev) {
1584 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1585 udev_event_detach(alias->dev_target, alias->name, 1);
1586 kfree(alias->name, M_DEVFS);
1587 kfree(alias, M_DEVFS);
1594 * This function propagates an alias addition or removal to
1598 devfs_alias_propagate(struct devfs_alias *alias, int remove)
1600 struct devfs_mnt_data *mnt;
1602 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1604 devfs_destroy_node(mnt->root_node, alias->name);
1606 devfs_alias_apply(mnt->root_node, alias);
1613 * This function is a recursive function iterating through
1614 * all device nodes in the topology and, if applicable,
1615 * creating the relevant alias for a device node.
1618 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1620 struct devfs_node *node1, *node2;
1622 KKASSERT(alias != NULL);
1624 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1625 if (node->nchildren > 2) {
1626 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1627 devfs_alias_apply(node1, alias);
1631 if (node->d_dev == alias->dev_target)
1632 devfs_alias_create(alias->name, node, 0);
1638 * This function checks if any alias possibly is applicable
1639 * to the given node. If so, the alias is created.
1642 devfs_alias_check_create(struct devfs_node *node)
1644 struct devfs_alias *alias;
1646 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1647 if (node->d_dev == alias->dev_target)
1648 devfs_alias_create(alias->name, node, 0);
1654 * This function creates an alias with a given name
1655 * linking to a given devfs node. It also increments
1656 * the link count on the target node.
1659 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based)
1661 struct mount *mp = target->mp;
1662 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1663 struct devfs_node *linknode;
1664 char *create_path = NULL;
1669 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1671 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1672 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1675 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1678 if (devfs_find_device_node_by_name(parent, name)) {
1679 devfs_debug(DEVFS_DEBUG_WARNING,
1680 "Node already exists: %s "
1681 "(devfs_make_alias_worker)!\n",
1687 linknode = devfs_allocp(Plink, name, parent, mp, NULL);
1688 if (linknode == NULL) {
1693 linknode->link_target = target;
1697 linknode->flags |= DEVFS_RULE_CREATED;
1700 kfree(name_buf, M_TEMP);
1705 * This function is called by the core and handles mount point
1706 * strings. It either calls the relevant worker (devfs_apply_
1707 * reset_rules_worker) on all mountpoints or only a specific
1711 devfs_apply_reset_rules_caller(char *mountto, int apply)
1713 struct devfs_mnt_data *mnt;
1715 if (mountto[0] == '*') {
1716 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1717 devfs_iterate_topology(mnt->root_node,
1718 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1722 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1723 if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) {
1724 devfs_iterate_topology(mnt->root_node,
1725 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1732 kfree(mountto, M_DEVFS);
1737 * This function calls a given callback function for
1738 * every dev node in the devfs dev list.
1741 devfs_scan_callback_worker(devfs_scan_t *callback, void *arg)
1745 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1753 * This function tries to resolve a given directory, or if not
1754 * found and creation requested, creates the given directory.
1756 static struct devfs_node *
1757 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1758 size_t name_len, int create)
1760 struct devfs_node *node, *found = NULL;
1762 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1763 if (name_len != node->d_dir.d_namlen)
1766 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1772 if ((found == NULL) && (create)) {
1773 found = devfs_allocp(Pdir, dir_name, parent, parent->mp, NULL);
1780 * This function tries to resolve a complete path. If creation is requested,
1781 * if a given part of the path cannot be resolved (because it doesn't exist),
1785 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1787 struct devfs_node *node = parent;
1794 buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1796 while (*path && idx < PATH_MAX - 1) {
1801 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1811 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1812 kfree (buf, M_TEMP);
1817 * Takes a full path and strips it into a directory path and a name.
1818 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
1819 * requires a working buffer with enough size to keep the whole
1823 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
1827 size_t len = strlen(fullpath) + 1;
1830 KKASSERT((fullpath != NULL) && (buf != NULL));
1831 KKASSERT((pathp != NULL) && (namep != NULL));
1833 memcpy(buf, fullpath, len);
1835 for (i = len-1; i>= 0; i--) {
1836 if (buf[i] == '/') {
1856 * This function creates a new devfs node for a given device. It can
1857 * handle a complete path as device name, and accordingly creates
1858 * the path and the final device node.
1860 * The reference count on the passed dev remains unchanged.
1863 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
1864 char *dev_name, char *path_fmt, ...)
1866 struct devfs_node *parent, *node = NULL;
1872 char *create_path = NULL;
1873 char *names = "pqrsPQRS";
1875 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1877 if (path_fmt != NULL) {
1878 __va_start(ap, path_fmt);
1879 kvasnrprintf(&path, PATH_MAX, 10, path_fmt, ap);
1883 parent = devfs_resolve_or_create_path(root, path, 1);
1886 devfs_resolve_name_path(
1887 ((dev_name == NULL) && (dev))?(dev->si_name):(dev_name),
1888 name_buf, &create_path, &name);
1891 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1894 if (devfs_find_device_node_by_name(parent, name)) {
1895 devfs_debug(DEVFS_DEBUG_WARNING, "devfs_create_device_node: "
1896 "DEVICE %s ALREADY EXISTS!!! Ignoring creation request.\n", name);
1900 node = devfs_allocp(Pdev, name, parent, parent->mp, dev);
1901 nanotime(&parent->mtime);
1904 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
1907 if ((dev) && (strlen(dev->si_name) >= 4) &&
1908 (!memcmp(dev->si_name, "ptm/", 4))) {
1909 node->parent->flags |= DEVFS_HIDDEN;
1910 node->flags |= DEVFS_HIDDEN;
1914 * Ugly pty magic, to tag pty devices as such and hide them if needed.
1916 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
1917 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1919 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
1921 for (i = 0; i < strlen(names); i++) {
1922 if (name[3] == names[i]) {
1928 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1932 kfree(name_buf, M_TEMP);
1938 * This function finds a given device node in the topology with a given
1942 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target)
1944 if ((node->node_type == Pdev) && (node->d_dev == target)) {
1952 * This function finds a device node in the given parent directory by its
1953 * name and returns it.
1956 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
1958 struct devfs_node *node, *found = NULL;
1959 size_t len = strlen(target);
1961 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1962 if (len != node->d_dir.d_namlen)
1965 if (!memcmp(node->d_dir.d_name, target, len)) {
1975 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop)
1977 struct vnode *vp = NULL;
1978 ino_t target = *inop;
1980 if (node->d_dir.d_ino == target) {
1983 vget(vp, LK_EXCLUSIVE | LK_RETRY);
1986 devfs_allocv(&vp, node);
1995 * This function takes a cdev and removes its devfs node in the
1996 * given topology. The cdev remains intact.
1999 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
2001 KKASSERT(target != NULL);
2002 return devfs_destroy_node(root, target->si_name);
2006 * This function takes a path to a devfs node, resolves it and
2007 * removes the devfs node from the given topology.
2010 devfs_destroy_node(struct devfs_node *root, char *target)
2012 struct devfs_node *node, *parent;
2015 char *create_path = NULL;
2019 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
2020 ksnprintf(name_buf, PATH_MAX, "%s", target);
2022 devfs_resolve_name_path(target, name_buf, &create_path, &name);
2025 parent = devfs_resolve_or_create_path(root, create_path, 0);
2029 if (parent == NULL) {
2030 kfree(name_buf, M_TEMP);
2034 node = devfs_find_device_node_by_name(parent, name);
2037 nanotime(&node->parent->mtime);
2041 kfree(name_buf, M_TEMP);
2047 * Just set perms and ownership for given node.
2050 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid,
2051 u_short mode, u_long flags)
2061 * Propagates a device attach/detach to all mount
2062 * points. Also takes care of automatic alias removal
2063 * for a deleted cdev.
2066 devfs_propagate_dev(cdev_t dev, int attach)
2068 struct devfs_mnt_data *mnt;
2070 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
2072 /* Device is being attached */
2073 devfs_create_device_node(mnt->root_node, dev,
2076 /* Device is being detached */
2077 devfs_alias_remove(dev);
2078 devfs_destroy_device_node(mnt->root_node, dev);
2085 * devfs_clone either returns a basename from a complete name by
2086 * returning the length of the name without trailing digits, or,
2087 * if clone != 0, calls the device's clone handler to get a new
2088 * device, which in turn is returned in devp.
2091 devfs_clone(cdev_t dev, const char *name, size_t len, int mode,
2095 struct devfs_clone_handler *chandler;
2096 struct dev_clone_args ap;
2098 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
2099 if (chandler->namlen != len)
2101 if ((!memcmp(chandler->name, name, len)) && (chandler->nhandler)) {
2102 lockmgr(&devfs_lock, LK_RELEASE);
2104 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2106 ap.a_head.a_dev = dev;
2112 error = (chandler->nhandler)(&ap);
2125 * Registers a new orphan in the orphan list.
2128 devfs_tracer_add_orphan(struct devfs_node *node)
2130 struct devfs_orphan *orphan;
2133 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2134 orphan->node = node;
2136 KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2137 node->flags |= DEVFS_ORPHANED;
2138 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2142 * Removes an orphan from the orphan list.
2145 devfs_tracer_del_orphan(struct devfs_node *node)
2147 struct devfs_orphan *orphan;
2151 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) {
2152 if (orphan->node == node) {
2153 node->flags &= ~DEVFS_ORPHANED;
2154 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2155 kfree(orphan, M_DEVFS);
2162 * Counts the orphans in the orphan list, and if cleanup
2163 * is specified, also frees the orphan and removes it from
2167 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2169 struct devfs_orphan *orphan, *orphan2;
2172 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) {
2175 * If we are instructed to clean up, we do so.
2178 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2179 orphan->node->flags &= ~DEVFS_ORPHANED;
2180 devfs_freep(orphan->node);
2181 kfree(orphan, M_DEVFS);
2189 * Fetch an ino_t from the global d_ino by increasing it
2193 devfs_fetch_ino(void)
2197 spin_lock(&ino_lock);
2199 spin_unlock(&ino_lock);
2205 * Allocates a new cdev and initializes it's most basic
2209 devfs_new_cdev(struct dev_ops *ops, int minor, struct dev_ops *bops)
2211 cdev_t dev = sysref_alloc(&cdev_sysref_class);
2213 sysref_activate(&dev->si_sysref);
2215 bzero(dev, offsetof(struct cdev, si_sysref));
2220 dev->si_drv1 = NULL;
2221 dev->si_drv2 = NULL;
2222 dev->si_lastread = 0; /* time_second */
2223 dev->si_lastwrite = 0; /* time_second */
2225 dev->si_dict = NULL;
2229 dev->si_uminor = minor;
2230 dev->si_bops = bops;
2233 * Since the disk subsystem is in the way, we need to
2234 * propagate the D_CANFREE from bops (and ops) to
2237 if (bops && (bops->head.flags & D_CANFREE)) {
2238 dev->si_flags |= SI_CANFREE;
2239 } else if (ops->head.flags & D_CANFREE) {
2240 dev->si_flags |= SI_CANFREE;
2243 /* If there is a backing device, we reference its ops */
2244 dev->si_inode = makeudev(
2245 devfs_reference_ops((bops)?(bops):(ops)),
2252 devfs_cdev_terminate(cdev_t dev)
2256 /* Check if it is locked already. if not, we acquire the devfs lock */
2257 if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) {
2258 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2263 * Make sure the node isn't linked anymore. Otherwise we've screwed
2264 * up somewhere, since normal devs are unlinked on the call to
2265 * destroy_dev and only-cdevs that have not been used for cloning
2266 * are not linked in the first place. only-cdevs used for cloning
2267 * will be linked in, too, and should only be destroyed via
2268 * destroy_dev, not destroy_only_dev, so we catch that problem, too.
2270 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2272 /* If we acquired the lock, we also get rid of it */
2274 lockmgr(&devfs_lock, LK_RELEASE);
2276 /* If there is a backing device, we release the backing device's ops */
2277 devfs_release_ops((dev->si_bops)?(dev->si_bops):(dev->si_ops));
2279 /* Finally destroy the device */
2280 sysref_put(&dev->si_sysref);
2284 * Dummies for now (individual locks for MPSAFE)
2287 devfs_cdev_lock(cdev_t dev)
2292 devfs_cdev_unlock(cdev_t dev)
2297 devfs_detached_filter_eof(struct knote *kn, long hint)
2299 kn->kn_flags |= EV_EOF;
2304 devfs_detached_filter_detach(struct knote *kn)
2306 cdev_t dev = (cdev_t)kn->kn_hook;
2308 knote_remove(&dev->si_kqinfo.ki_note, kn);
2311 static struct filterops devfs_detached_filterops =
2312 { FILTEROP_ISFD, NULL,
2313 devfs_detached_filter_detach,
2314 devfs_detached_filter_eof };
2317 * Delegates knote filter handling responsibility to devfs
2319 * Any device that implements kqfilter event handling and could be detached
2320 * or shut down out from under the kevent subsystem must allow devfs to
2321 * assume responsibility for any knotes it may hold.
2324 devfs_assume_knotes(cdev_t dev, struct kqinfo *kqi)
2327 * Let kern/kern_event.c do the heavy lifting.
2329 knote_assume_knotes(kqi, &dev->si_kqinfo,
2330 &devfs_detached_filterops, (void *)dev);
2333 * These should probably be activated individually, but doing so
2334 * would require refactoring kq's public in-kernel interface.
2336 KNOTE(&dev->si_kqinfo.ki_note, 0);
2340 * Links a given cdev into the dev list.
2343 devfs_link_dev(cdev_t dev)
2345 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2346 dev->si_flags |= SI_DEVFS_LINKED;
2347 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2353 * Removes a given cdev from the dev list. The caller is responsible for
2354 * releasing the reference on the device associated with the linkage.
2356 * Returns EALREADY if the dev has already been unlinked.
2359 devfs_unlink_dev(cdev_t dev)
2361 if ((dev->si_flags & SI_DEVFS_LINKED)) {
2362 TAILQ_REMOVE(&devfs_dev_list, dev, link);
2363 dev->si_flags &= ~SI_DEVFS_LINKED;
2370 devfs_node_is_accessible(struct devfs_node *node)
2372 if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2379 devfs_reference_ops(struct dev_ops *ops)
2382 struct devfs_dev_ops *found = NULL;
2383 struct devfs_dev_ops *devops;
2385 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2386 if (devops->ops == ops) {
2393 found = kmalloc(sizeof(struct devfs_dev_ops), M_DEVFS, M_WAITOK);
2395 found->ref_count = 0;
2396 TAILQ_INSERT_TAIL(&devfs_dev_ops_list, found, link);
2401 if (found->ref_count == 0) {
2402 found->id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255);
2403 if (found->id == -1) {
2404 /* Ran out of unique ids */
2405 devfs_debug(DEVFS_DEBUG_WARNING,
2406 "devfs_reference_ops: WARNING: ran out of unique ids\n");
2416 devfs_release_ops(struct dev_ops *ops)
2418 struct devfs_dev_ops *found = NULL;
2419 struct devfs_dev_ops *devops;
2421 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2422 if (devops->ops == ops) {
2432 if (found->ref_count == 0) {
2433 TAILQ_REMOVE(&devfs_dev_ops_list, found, link);
2434 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), found->id);
2435 kfree(found, M_DEVFS);
2440 * Wait for asynchronous messages to complete in the devfs helper
2441 * thread, then return. Do nothing if the helper thread is dead
2442 * or we are being indirectly called from the helper thread itself.
2449 if (devfs_run && curthread != td_core) {
2450 msg = devfs_msg_get();
2451 msg = devfs_msg_send_sync(DEVFS_SYNC, msg);
2457 * Called on init of devfs; creates the objcaches and
2458 * spawns off the devfs core thread. Also initializes
2464 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2465 /* Create objcaches for nodes, msgs and devs */
2466 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2468 objcache_malloc_alloc,
2469 objcache_malloc_free,
2470 &devfs_node_malloc_args );
2472 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2474 objcache_malloc_alloc,
2475 objcache_malloc_free,
2476 &devfs_msg_malloc_args );
2478 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2480 objcache_malloc_alloc,
2481 objcache_malloc_free,
2482 &devfs_dev_malloc_args );
2484 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2486 /* Initialize the reply-only port which acts as a message drain */
2487 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2489 /* Initialize *THE* devfs lock */
2490 lockinit(&devfs_lock, "devfs_core lock", 0, 0);
2492 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2493 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2494 0, 0, "devfs_msg_core");
2495 while (devfs_run == 0)
2496 lksleep(td_core, &devfs_lock, 0, "devfsc", 0);
2497 lockmgr(&devfs_lock, LK_RELEASE);
2499 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2503 * Called on unload of devfs; takes care of destroying the core
2504 * and the objcaches. Also removes aliases that are no longer needed.
2509 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2511 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2513 tsleep(td_core, 0, "devfsc", hz*10);
2514 tsleep(td_core, 0, "devfsc", hz);
2516 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2518 /* Destroy the objcaches */
2519 objcache_destroy(devfs_msg_cache);
2520 objcache_destroy(devfs_node_cache);
2521 objcache_destroy(devfs_dev_cache);
2527 * This is a sysctl handler to assist userland devname(3) to
2528 * find the device name for a given udev.
2531 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2538 if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t))))
2541 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev);
2546 if ((found = devfs_find_device_by_udev(udev)) == NULL)
2549 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2553 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
2554 NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)");
2556 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2557 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2558 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable,
2559 0, "Enable DevFS debugging");
2561 SYSINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST,
2563 SYSUNINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY,
2564 devfs_uninit, NULL);
2567 * WildCmp() - compare wild string to sane string
2569 * Returns 0 on success, -1 on failure.
2572 wildCmp(const char **mary, int d, const char *w, const char *s)
2577 * skip fixed portion
2583 * optimize terminator
2587 if (w[1] != '?' && w[1] != '*') {
2589 * optimize * followed by non-wild
2591 for (i = 0; s + i < mary[d]; ++i) {
2592 if (s[i] == w[1] && wildCmp(mary, d + 1, w + 1, s + i) == 0)
2599 for (i = 0; s + i < mary[d]; ++i) {
2600 if (wildCmp(mary, d + 1, w + 1, s + i) == 0)
2615 if (*w == 0) /* terminator */
2628 * WildCaseCmp() - compare wild string to sane string, case insensitive
2630 * Returns 0 on success, -1 on failure.
2633 wildCaseCmp(const char **mary, int d, const char *w, const char *s)
2638 * skip fixed portion
2644 * optimize terminator
2648 if (w[1] != '?' && w[1] != '*') {
2650 * optimize * followed by non-wild
2652 for (i = 0; s + i < mary[d]; ++i) {
2653 if (s[i] == w[1] && wildCaseCmp(mary, d + 1, w + 1, s + i) == 0)
2660 for (i = 0; s + i < mary[d]; ++i) {
2661 if (wildCaseCmp(mary, d + 1, w + 1, s + i) == 0)
2675 #define tolower(x) ((x >= 'A' && x <= 'Z')?(x+('a'-'A')):(x))
2676 if (tolower(*w) != tolower(*s))
2679 if (*w == 0) /* terminator */
2691 devfs_WildCmp(const char *w, const char *s)
2695 int slen = strlen(s);
2698 for (i = c = 0; w[i]; ++i) {
2702 mary = kmalloc(sizeof(char *) * (c + 1), M_DEVFS, M_WAITOK);
2703 for (i = 0; i < c; ++i)
2705 i = wildCmp(mary, 0, w, s);
2706 kfree(mary, M_DEVFS);
2711 devfs_WildCaseCmp(const char *w, const char *s)
2715 int slen = strlen(s);
2718 for (i = c = 0; w[i]; ++i) {
2722 mary = kmalloc(sizeof(char *) * (c + 1), M_DEVFS, M_WAITOK);
2723 for (i = 0; i < c; ++i)
2725 i = wildCaseCmp(mary, 0, w, s);
2726 kfree(mary, M_DEVFS);