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>
42 #include <sys/msgport.h>
43 #include <sys/sysctl.h>
44 #include <sys/ucred.h>
45 #include <sys/devfs.h>
46 #include <sys/devfs_rules.h>
49 #include <sys/msgport2.h>
50 #include <sys/spinlock2.h>
51 #include <sys/mplock2.h>
52 #include <sys/sysref2.h>
54 MALLOC_DEFINE(M_DEVFS, "devfs", "Device File System (devfs) allocations");
55 DEVFS_DECLARE_CLONE_BITMAP(ops_id);
57 * SYSREF Integration - reference counting, allocation,
58 * sysid and syslink integration.
60 static void devfs_cdev_terminate(cdev_t dev);
61 static void devfs_cdev_lock(cdev_t dev);
62 static void devfs_cdev_unlock(cdev_t dev);
63 static struct sysref_class cdev_sysref_class = {
66 .proto = SYSREF_PROTO_DEV,
67 .offset = offsetof(struct cdev, si_sysref),
68 .objsize = sizeof(struct cdev),
72 .terminate = (sysref_terminate_func_t)devfs_cdev_terminate,
73 .lock = (sysref_lock_func_t)devfs_cdev_lock,
74 .unlock = (sysref_unlock_func_t)devfs_cdev_unlock
78 static struct objcache *devfs_node_cache;
79 static struct objcache *devfs_msg_cache;
80 static struct objcache *devfs_dev_cache;
82 static struct objcache_malloc_args devfs_node_malloc_args = {
83 sizeof(struct devfs_node), M_DEVFS };
84 struct objcache_malloc_args devfs_msg_malloc_args = {
85 sizeof(struct devfs_msg), M_DEVFS };
86 struct objcache_malloc_args devfs_dev_malloc_args = {
87 sizeof(struct cdev), M_DEVFS };
89 static struct devfs_dev_head devfs_dev_list =
90 TAILQ_HEAD_INITIALIZER(devfs_dev_list);
91 static struct devfs_mnt_head devfs_mnt_list =
92 TAILQ_HEAD_INITIALIZER(devfs_mnt_list);
93 static struct devfs_chandler_head devfs_chandler_list =
94 TAILQ_HEAD_INITIALIZER(devfs_chandler_list);
95 static struct devfs_alias_head devfs_alias_list =
96 TAILQ_HEAD_INITIALIZER(devfs_alias_list);
97 static struct devfs_dev_ops_head devfs_dev_ops_list =
98 TAILQ_HEAD_INITIALIZER(devfs_dev_ops_list);
100 struct lock devfs_lock;
101 static struct lwkt_port devfs_dispose_port;
102 static struct lwkt_port devfs_msg_port;
103 static struct thread *td_core;
105 static struct spinlock ino_lock;
107 static int devfs_debug_enable;
108 static int devfs_run;
110 static ino_t devfs_fetch_ino(void);
111 static int devfs_create_all_dev_worker(struct devfs_node *);
112 static int devfs_create_dev_worker(cdev_t, uid_t, gid_t, int);
113 static int devfs_destroy_dev_worker(cdev_t);
114 static int devfs_destroy_related_worker(cdev_t);
115 static int devfs_destroy_dev_by_ops_worker(struct dev_ops *, int);
116 static int devfs_propagate_dev(cdev_t, int);
117 static int devfs_unlink_dev(cdev_t dev);
118 static void devfs_msg_exec(devfs_msg_t msg);
120 static int devfs_chandler_add_worker(const char *, d_clone_t *);
121 static int devfs_chandler_del_worker(const char *);
123 static void devfs_msg_autofree_reply(lwkt_port_t, lwkt_msg_t);
124 static void devfs_msg_core(void *);
126 static int devfs_find_device_by_name_worker(devfs_msg_t);
127 static int devfs_find_device_by_udev_worker(devfs_msg_t);
129 static int devfs_apply_reset_rules_caller(char *, int);
131 static int devfs_scan_callback_worker(devfs_scan_t *, void *);
133 static struct devfs_node *devfs_resolve_or_create_dir(struct devfs_node *,
134 char *, size_t, int);
136 static int devfs_make_alias_worker(struct devfs_alias *);
137 static int devfs_destroy_alias_worker(struct devfs_alias *);
138 static int devfs_alias_remove(cdev_t);
139 static int devfs_alias_reap(void);
140 static int devfs_alias_propagate(struct devfs_alias *, int);
141 static int devfs_alias_apply(struct devfs_node *, struct devfs_alias *);
142 static int devfs_alias_check_create(struct devfs_node *);
144 static int devfs_clr_related_flag_worker(cdev_t, uint32_t);
145 static int devfs_destroy_related_without_flag_worker(cdev_t, uint32_t);
147 static void *devfs_reaperp_callback(struct devfs_node *, void *);
148 static void *devfs_gc_dirs_callback(struct devfs_node *, void *);
149 static void *devfs_gc_links_callback(struct devfs_node *, struct devfs_node *);
151 devfs_inode_to_vnode_worker_callback(struct devfs_node *, ino_t *);
154 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function
158 devfs_debug(int level, char *fmt, ...)
163 if (level <= devfs_debug_enable)
171 * devfs_allocp() Allocates a new devfs node with the specified
172 * parameters. The node is also automatically linked into the topology
173 * if a parent is specified. It also calls the rule and alias stuff to
174 * be applied on the new node
177 devfs_allocp(devfs_nodetype devfsnodetype, char *name,
178 struct devfs_node *parent, struct mount *mp, cdev_t dev)
180 struct devfs_node *node = NULL;
181 size_t namlen = strlen(name);
183 node = objcache_get(devfs_node_cache, M_WAITOK);
184 bzero(node, sizeof(*node));
186 atomic_add_long(&DEVFS_MNTDATA(mp)->leak_count, 1);
191 node->d_dir.d_ino = devfs_fetch_ino();
194 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries
197 node->cookie_jar = 2;
200 * Access Control members
202 node->mode = DEVFS_DEFAULT_MODE;
203 node->uid = DEVFS_DEFAULT_UID;
204 node->gid = DEVFS_DEFAULT_GID;
206 switch (devfsnodetype) {
209 * Ensure that we don't recycle the root vnode by marking it as
210 * linked into the topology.
212 node->flags |= DEVFS_NODE_LINKED;
214 TAILQ_INIT(DEVFS_DENODE_HEAD(node));
215 node->d_dir.d_type = DT_DIR;
220 node->d_dir.d_type = DT_LNK;
224 node->d_dir.d_type = DT_REG;
229 node->d_dir.d_type = DT_CHR;
232 node->mode = dev->si_perms;
233 node->uid = dev->si_uid;
234 node->gid = dev->si_gid;
236 devfs_alias_check_create(node);
241 panic("devfs_allocp: unknown node type");
245 node->node_type = devfsnodetype;
247 /* Initialize the dirent structure of each devfs vnode */
248 node->d_dir.d_namlen = namlen;
249 node->d_dir.d_name = kmalloc(namlen+1, M_DEVFS, M_WAITOK);
250 memcpy(node->d_dir.d_name, name, namlen);
251 node->d_dir.d_name[namlen] = '\0';
253 /* Initialize the parent node element */
254 node->parent = parent;
256 /* Initialize *time members */
257 nanotime(&node->atime);
258 node->mtime = node->ctime = node->atime;
261 * Associate with parent as last step, clean out namecache
264 if ((parent != NULL) &&
265 ((parent->node_type == Nroot) || (parent->node_type == Ndir))) {
267 node->cookie = parent->cookie_jar++;
268 node->flags |= DEVFS_NODE_LINKED;
269 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent), node, link);
271 /* This forces negative namecache lookups to clear */
272 ++mp->mnt_namecache_gen;
276 devfs_rule_check_apply(node, NULL);
278 atomic_add_long(&DEVFS_MNTDATA(mp)->file_count, 1);
284 * devfs_allocv() allocates a new vnode based on a devfs node.
287 devfs_allocv(struct vnode **vpp, struct devfs_node *node)
295 * devfs master lock must not be held across a vget() call, we have
296 * to hold our ad-hoc vp to avoid a free race from destroying the
297 * contents of the structure. The vget() will interlock recycles
301 while ((vp = node->v_node) != NULL) {
303 lockmgr(&devfs_lock, LK_RELEASE);
304 error = vget(vp, LK_EXCLUSIVE);
306 lockmgr(&devfs_lock, LK_EXCLUSIVE);
311 if (error != ENOENT) {
318 * devfs master lock must not be held across a getnewvnode() call.
320 lockmgr(&devfs_lock, LK_RELEASE);
321 if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0) {
322 lockmgr(&devfs_lock, LK_EXCLUSIVE);
325 lockmgr(&devfs_lock, LK_EXCLUSIVE);
329 if (node->v_node != NULL) {
338 switch (node->node_type) {
340 vsetflags(vp, VROOT);
356 KKASSERT(node->d_dev);
358 vp->v_uminor = node->d_dev->si_uminor;
359 vp->v_umajor = node->d_dev->si_umajor;
361 v_associate_rdev(vp, node->d_dev);
362 vp->v_ops = &node->mp->mnt_vn_spec_ops;
366 panic("devfs_allocv: unknown node type");
374 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
375 * based on the newly created devfs node.
378 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype,
379 char *name, struct devfs_node *parent, cdev_t dev)
381 struct devfs_node *node;
383 node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
386 devfs_allocv(vpp, node);
394 * Destroy the devfs_node. The node must be unlinked from the topology.
396 * This function will also destroy any vnode association with the node
399 * The cdev_t itself remains intact.
401 * The core lock is not necessarily held on call and must be temporarily
402 * released if it is to avoid a deadlock.
405 devfs_freep(struct devfs_node *node)
411 KKASSERT(((node->flags & DEVFS_NODE_LINKED) == 0) ||
412 (node->node_type == Nroot));
415 * Protect against double frees
417 KKASSERT((node->flags & DEVFS_DESTROYED) == 0);
418 node->flags |= DEVFS_DESTROYED;
421 * Avoid deadlocks between devfs_lock and the vnode lock when
422 * disassociating the vnode (stress2 pty vs ls -la /dev/pts).
424 * This also prevents the vnode reclaim code from double-freeing
425 * the node. The vget() is required to safely modified the vp
426 * and cycle the refs to terminate an inactive vp.
428 if (lockstatus(&devfs_lock, curthread) == LK_EXCLUSIVE) {
429 lockmgr(&devfs_lock, LK_RELEASE);
435 while ((vp = node->v_node) != NULL) {
436 if (vget(vp, LK_EXCLUSIVE | LK_RETRY) != 0)
441 cache_inval_vp(vp, CINV_DESTROY);
448 atomic_subtract_long(&DEVFS_MNTDATA(node->mp)->leak_count, 1);
449 if (node->symlink_name) {
450 kfree(node->symlink_name, M_DEVFS);
451 node->symlink_name = NULL;
455 * Remove the node from the orphan list if it is still on it.
457 if (node->flags & DEVFS_ORPHANED)
458 devfs_tracer_del_orphan(node);
460 if (node->d_dir.d_name) {
461 kfree(node->d_dir.d_name, M_DEVFS);
462 node->d_dir.d_name = NULL;
464 atomic_subtract_long(&DEVFS_MNTDATA(node->mp)->file_count, 1);
465 objcache_put(devfs_node_cache, node);
468 lockmgr(&devfs_lock, LK_EXCLUSIVE);
474 * Unlink the devfs node from the topology and add it to the orphan list.
475 * The node will later be destroyed by freep.
477 * Any vnode association, including the v_rdev and v_data, remains intact
481 devfs_unlinkp(struct devfs_node *node)
483 struct devfs_node *parent;
487 * Add the node to the orphan list, so it is referenced somewhere, to
488 * so we don't leak it.
490 devfs_tracer_add_orphan(node);
492 parent = node->parent;
495 * If the parent is known we can unlink the node out of the topology
498 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
500 node->flags &= ~DEVFS_NODE_LINKED;
508 devfs_iterate_topology(struct devfs_node *node,
509 devfs_iterate_callback_t *callback, void *arg1)
511 struct devfs_node *node1, *node2;
514 if ((node->node_type == Nroot) || (node->node_type == Ndir)) {
515 if (node->nchildren > 2) {
516 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
518 if ((ret = devfs_iterate_topology(node1, callback, arg1)))
524 ret = callback(node, arg1);
529 * devfs_reaperp() is a recursive function that iterates through all the
530 * topology, unlinking and freeing all devfs nodes.
533 devfs_reaperp_callback(struct devfs_node *node, void *unused)
542 devfs_gc_dirs_callback(struct devfs_node *node, void *unused)
544 if (node->node_type == Ndir) {
545 if ((node->nchildren == 2) &&
546 !(node->flags & DEVFS_USER_CREATED)) {
556 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target)
558 if ((node->node_type == Nlink) && (node->link_target == target)) {
567 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
568 * freeing a node, but also removes empty directories and links that link
569 * via devfs auto-link mechanism to the node being deleted.
572 devfs_gc(struct devfs_node *node)
574 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
576 if (node->nlinks > 0)
577 devfs_iterate_topology(root_node,
578 (devfs_iterate_callback_t *)devfs_gc_links_callback, node);
581 devfs_iterate_topology(root_node,
582 (devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL);
590 * devfs_create_dev() is the asynchronous entry point for device creation.
591 * It just sends a message with the relevant details to the devfs core.
593 * This function will reference the passed device. The reference is owned
594 * by devfs and represents all of the device's node associations.
597 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
600 devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
606 * devfs_destroy_dev() is the asynchronous entry point for device destruction.
607 * It just sends a message with the relevant details to the devfs core.
610 devfs_destroy_dev(cdev_t dev)
612 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
617 * devfs_mount_add() is the synchronous entry point for adding a new devfs
618 * mount. It sends a synchronous message with the relevant details to the
622 devfs_mount_add(struct devfs_mnt_data *mnt)
626 msg = devfs_msg_get();
628 msg = devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
635 * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
636 * It sends a synchronous message with the relevant details to the devfs core.
639 devfs_mount_del(struct devfs_mnt_data *mnt)
643 msg = devfs_msg_get();
645 msg = devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
652 * devfs_destroy_related() is the synchronous entry point for device
653 * destruction by subname. It just sends a message with the relevant details to
657 devfs_destroy_related(cdev_t dev)
661 msg = devfs_msg_get();
663 msg = devfs_msg_send_sync(DEVFS_DESTROY_RELATED, msg);
669 devfs_clr_related_flag(cdev_t dev, uint32_t flag)
673 msg = devfs_msg_get();
674 msg->mdv_flags.dev = dev;
675 msg->mdv_flags.flag = flag;
676 msg = devfs_msg_send_sync(DEVFS_CLR_RELATED_FLAG, msg);
683 devfs_destroy_related_without_flag(cdev_t dev, uint32_t flag)
687 msg = devfs_msg_get();
688 msg->mdv_flags.dev = dev;
689 msg->mdv_flags.flag = flag;
690 msg = devfs_msg_send_sync(DEVFS_DESTROY_RELATED_WO_FLAG, msg);
697 * devfs_create_all_dev is the asynchronous entry point to trigger device
698 * node creation. It just sends a message with the relevant details to
702 devfs_create_all_dev(struct devfs_node *root)
704 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
709 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
710 * devices with a specific set of dev_ops and minor. It just sends a
711 * message with the relevant details to the devfs core.
714 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
716 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
721 * devfs_clone_handler_add is the synchronous entry point to add a new
722 * clone handler. It just sends a message with the relevant details to
726 devfs_clone_handler_add(const char *name, d_clone_t *nhandler)
730 msg = devfs_msg_get();
731 msg->mdv_chandler.name = name;
732 msg->mdv_chandler.nhandler = nhandler;
733 msg = devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg);
739 * devfs_clone_handler_del is the synchronous entry point to remove a
740 * clone handler. It just sends a message with the relevant details to
744 devfs_clone_handler_del(const char *name)
748 msg = devfs_msg_get();
749 msg->mdv_chandler.name = name;
750 msg->mdv_chandler.nhandler = NULL;
751 msg = devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg);
757 * devfs_find_device_by_name is the synchronous entry point to find a
758 * device given its name. It sends a synchronous message with the
759 * relevant details to the devfs core and returns the answer.
762 devfs_find_device_by_name(const char *fmt, ...)
773 kvasnrprintf(&target, PATH_MAX, 10, fmt, ap);
776 msg = devfs_msg_get();
777 msg->mdv_name = target;
778 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
779 found = msg->mdv_cdev;
787 * devfs_find_device_by_udev is the synchronous entry point to find a
788 * device given its udev number. It sends a synchronous message with
789 * the relevant details to the devfs core and returns the answer.
792 devfs_find_device_by_udev(udev_t udev)
797 msg = devfs_msg_get();
798 msg->mdv_udev = udev;
799 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg);
800 found = msg->mdv_cdev;
803 devfs_debug(DEVFS_DEBUG_DEBUG,
804 "devfs_find_device_by_udev found? %s -end:3-\n",
805 ((found) ? found->si_name:"NO"));
810 devfs_inode_to_vnode(struct mount *mp, ino_t target)
812 struct vnode *vp = NULL;
818 msg = devfs_msg_get();
819 msg->mdv_ino.mp = mp;
820 msg->mdv_ino.ino = target;
821 msg = devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg);
822 vp = msg->mdv_ino.vp;
823 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
830 * devfs_make_alias is the asynchronous entry point to register an alias
831 * for a device. It just sends a message with the relevant details to the
835 devfs_make_alias(const char *name, cdev_t dev_target)
837 struct devfs_alias *alias;
842 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
843 alias->name = kstrdup(name, M_DEVFS);
845 alias->dev_target = dev_target;
847 devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
852 * devfs_destroy_alias is the asynchronous entry point to deregister an alias
853 * for a device. It just sends a message with the relevant details to the
857 devfs_destroy_alias(const char *name, cdev_t dev_target)
859 struct devfs_alias *alias;
864 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
865 alias->name = kstrdup(name, M_DEVFS);
867 alias->dev_target = dev_target;
869 devfs_msg_send_generic(DEVFS_DESTROY_ALIAS, alias);
874 * devfs_apply_rules is the asynchronous entry point to trigger application
875 * of all rules. It just sends a message with the relevant details to the
879 devfs_apply_rules(char *mntto)
883 new_name = kstrdup(mntto, M_DEVFS);
884 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
890 * devfs_reset_rules is the asynchronous entry point to trigger reset of all
891 * rules. It just sends a message with the relevant details to the devfs core.
894 devfs_reset_rules(char *mntto)
898 new_name = kstrdup(mntto, M_DEVFS);
899 devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
906 * devfs_scan_callback is the asynchronous entry point to call a callback
908 * It just sends a message with the relevant details to the devfs core.
911 devfs_scan_callback(devfs_scan_t *callback, void *arg)
917 msg = devfs_msg_get();
918 msg->mdv_load = callback;
919 msg->mdv_load2 = arg;
920 msg = devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
928 * Acts as a message drain. Any message that is replied to here gets destroyed
929 * and the memory freed.
932 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
934 devfs_msg_put((devfs_msg_t)msg);
938 * devfs_msg_get allocates a new devfs msg and returns it.
943 return objcache_get(devfs_msg_cache, M_WAITOK);
947 * devfs_msg_put deallocates a given devfs msg.
950 devfs_msg_put(devfs_msg_t msg)
952 objcache_put(devfs_msg_cache, msg);
957 * devfs_msg_send is the generic asynchronous message sending facility
958 * for devfs. By default the reply port is the automatic disposal port.
960 * If the current thread is the devfs_msg_port thread we execute the
961 * operation synchronously.
964 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
966 lwkt_port_t port = &devfs_msg_port;
968 lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
970 devfs_msg->hdr.u.ms_result = cmd;
972 if (port->mpu_td == curthread) {
973 devfs_msg_exec(devfs_msg);
974 lwkt_replymsg(&devfs_msg->hdr, 0);
976 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
981 * devfs_msg_send_sync is the generic synchronous message sending
982 * facility for devfs. It initializes a local reply port and waits
983 * for the core's answer. This answer is then returned.
986 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
988 struct lwkt_port rep_port;
989 devfs_msg_t msg_incoming;
990 lwkt_port_t port = &devfs_msg_port;
992 lwkt_initport_thread(&rep_port, curthread);
993 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
995 devfs_msg->hdr.u.ms_result = cmd;
997 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
998 msg_incoming = lwkt_waitport(&rep_port, 0);
1000 return msg_incoming;
1004 * sends a message with a generic argument.
1007 devfs_msg_send_generic(uint32_t cmd, void *load)
1009 devfs_msg_t devfs_msg = devfs_msg_get();
1011 devfs_msg->mdv_load = load;
1012 devfs_msg_send(cmd, devfs_msg);
1016 * sends a message with a name argument.
1019 devfs_msg_send_name(uint32_t cmd, char *name)
1021 devfs_msg_t devfs_msg = devfs_msg_get();
1023 devfs_msg->mdv_name = name;
1024 devfs_msg_send(cmd, devfs_msg);
1028 * sends a message with a mount argument.
1031 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
1033 devfs_msg_t devfs_msg = devfs_msg_get();
1035 devfs_msg->mdv_mnt = mnt;
1036 devfs_msg_send(cmd, devfs_msg);
1040 * sends a message with an ops argument.
1043 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
1045 devfs_msg_t devfs_msg = devfs_msg_get();
1047 devfs_msg->mdv_ops.ops = ops;
1048 devfs_msg->mdv_ops.minor = minor;
1049 devfs_msg_send(cmd, devfs_msg);
1053 * sends a message with a clone handler argument.
1056 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
1058 devfs_msg_t devfs_msg = devfs_msg_get();
1060 devfs_msg->mdv_chandler.name = name;
1061 devfs_msg->mdv_chandler.nhandler = handler;
1062 devfs_msg_send(cmd, devfs_msg);
1066 * sends a message with a device argument.
1069 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1071 devfs_msg_t devfs_msg = devfs_msg_get();
1073 devfs_msg->mdv_dev.dev = dev;
1074 devfs_msg->mdv_dev.uid = uid;
1075 devfs_msg->mdv_dev.gid = gid;
1076 devfs_msg->mdv_dev.perms = perms;
1078 devfs_msg_send(cmd, devfs_msg);
1082 * sends a message with a link argument.
1085 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1087 devfs_msg_t devfs_msg = devfs_msg_get();
1089 devfs_msg->mdv_link.name = name;
1090 devfs_msg->mdv_link.target = target;
1091 devfs_msg->mdv_link.mp = mp;
1092 devfs_msg_send(cmd, devfs_msg);
1096 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1097 * and calls the relevant worker functions. By using messages it's assured
1098 * that events occur in the correct order.
1101 devfs_msg_core(void *arg)
1105 lwkt_initport_thread(&devfs_msg_port, curthread);
1107 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1110 lockmgr(&devfs_lock, LK_RELEASE);
1112 get_mplock(); /* mpsafe yet? */
1115 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1116 devfs_debug(DEVFS_DEBUG_DEBUG,
1117 "devfs_msg_core, new msg: %x\n",
1118 (unsigned int)msg->hdr.u.ms_result);
1119 devfs_msg_exec(msg);
1120 lwkt_replymsg(&msg->hdr, 0);
1130 devfs_msg_exec(devfs_msg_t msg)
1132 struct devfs_mnt_data *mnt;
1133 struct devfs_node *node;
1137 * Acquire the devfs lock to ensure safety of all called functions
1139 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1141 switch (msg->hdr.u.ms_result) {
1142 case DEVFS_DEVICE_CREATE:
1143 dev = msg->mdv_dev.dev;
1144 devfs_create_dev_worker(dev,
1147 msg->mdv_dev.perms);
1149 case DEVFS_DEVICE_DESTROY:
1150 dev = msg->mdv_dev.dev;
1151 devfs_destroy_dev_worker(dev);
1153 case DEVFS_DESTROY_RELATED:
1154 devfs_destroy_related_worker(msg->mdv_load);
1156 case DEVFS_DESTROY_DEV_BY_OPS:
1157 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1158 msg->mdv_ops.minor);
1160 case DEVFS_CREATE_ALL_DEV:
1161 node = (struct devfs_node *)msg->mdv_load;
1162 devfs_create_all_dev_worker(node);
1164 case DEVFS_MOUNT_ADD:
1166 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1167 devfs_create_all_dev_worker(mnt->root_node);
1169 case DEVFS_MOUNT_DEL:
1171 TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1172 devfs_iterate_topology(mnt->root_node, devfs_reaperp_callback,
1174 if (mnt->leak_count) {
1175 devfs_debug(DEVFS_DEBUG_SHOW,
1176 "Leaked %ld devfs_node elements!\n",
1180 case DEVFS_CHANDLER_ADD:
1181 devfs_chandler_add_worker(msg->mdv_chandler.name,
1182 msg->mdv_chandler.nhandler);
1184 case DEVFS_CHANDLER_DEL:
1185 devfs_chandler_del_worker(msg->mdv_chandler.name);
1187 case DEVFS_FIND_DEVICE_BY_NAME:
1188 devfs_find_device_by_name_worker(msg);
1190 case DEVFS_FIND_DEVICE_BY_UDEV:
1191 devfs_find_device_by_udev_worker(msg);
1193 case DEVFS_MAKE_ALIAS:
1194 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1196 case DEVFS_DESTROY_ALIAS:
1197 devfs_destroy_alias_worker((struct devfs_alias *)msg->mdv_load);
1199 case DEVFS_APPLY_RULES:
1200 devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1202 case DEVFS_RESET_RULES:
1203 devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1205 case DEVFS_SCAN_CALLBACK:
1206 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load,
1209 case DEVFS_CLR_RELATED_FLAG:
1210 devfs_clr_related_flag_worker(msg->mdv_flags.dev,
1211 msg->mdv_flags.flag);
1213 case DEVFS_DESTROY_RELATED_WO_FLAG:
1214 devfs_destroy_related_without_flag_worker(msg->mdv_flags.dev,
1215 msg->mdv_flags.flag);
1217 case DEVFS_INODE_TO_VNODE:
1218 msg->mdv_ino.vp = devfs_iterate_topology(
1219 DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node,
1220 (devfs_iterate_callback_t *)devfs_inode_to_vnode_worker_callback,
1223 case DEVFS_TERMINATE_CORE:
1229 devfs_debug(DEVFS_DEBUG_WARNING,
1230 "devfs_msg_core: unknown message "
1231 "received at core\n");
1234 lockmgr(&devfs_lock, LK_RELEASE);
1238 * Worker function to insert a new dev into the dev list and initialize its
1239 * permissions. It also calls devfs_propagate_dev which in turn propagates
1240 * the change to all mount points.
1242 * The passed dev is already referenced. This reference is eaten by this
1243 * function and represents the dev's linkage into devfs_dev_list.
1246 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1252 dev->si_perms = perms;
1254 devfs_link_dev(dev);
1255 devfs_propagate_dev(dev, 1);
1257 udev_event_attach(dev, NULL, 0);
1263 * Worker function to delete a dev from the dev list and free the cdev.
1264 * It also calls devfs_propagate_dev which in turn propagates the change
1265 * to all mount points.
1268 devfs_destroy_dev_worker(cdev_t dev)
1273 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1275 error = devfs_unlink_dev(dev);
1276 devfs_propagate_dev(dev, 0);
1278 udev_event_detach(dev, NULL, 0);
1281 release_dev(dev); /* link ref */
1289 * Worker function to destroy all devices with a certain basename.
1290 * Calls devfs_destroy_dev_worker for the actual destruction.
1293 devfs_destroy_related_worker(cdev_t needle)
1298 devfs_debug(DEVFS_DEBUG_DEBUG, "related worker: %s\n",
1300 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1301 if (dev->si_parent == needle) {
1302 devfs_destroy_related_worker(dev);
1303 devfs_destroy_dev_worker(dev);
1311 devfs_clr_related_flag_worker(cdev_t needle, uint32_t flag)
1315 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1316 if (dev->si_parent == needle) {
1317 devfs_clr_related_flag_worker(dev, flag);
1318 dev->si_flags &= ~flag;
1326 devfs_destroy_related_without_flag_worker(cdev_t needle, uint32_t flag)
1331 devfs_debug(DEVFS_DEBUG_DEBUG, "related_wo_flag: %s\n",
1334 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1335 if (dev->si_parent == needle) {
1336 devfs_destroy_related_without_flag_worker(dev, flag);
1337 if (!(dev->si_flags & flag)) {
1338 devfs_destroy_dev_worker(dev);
1339 devfs_debug(DEVFS_DEBUG_DEBUG,
1340 "related_wo_flag: %s restart\n", dev->si_name);
1350 * Worker function that creates all device nodes on top of a devfs
1354 devfs_create_all_dev_worker(struct devfs_node *root)
1360 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1361 devfs_create_device_node(root, dev, NULL, NULL);
1368 * Worker function that destroys all devices that match a specific
1369 * dev_ops and/or minor. If minor is less than 0, it is not matched
1370 * against. It also propagates all changes.
1373 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1379 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1380 if (dev->si_ops != ops)
1382 if ((minor < 0) || (dev->si_uminor == minor)) {
1383 devfs_destroy_dev_worker(dev);
1391 * Worker function that registers a new clone handler in devfs.
1394 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler)
1396 struct devfs_clone_handler *chandler = NULL;
1397 u_char len = strlen(name);
1402 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1403 if (chandler->namlen != len)
1406 if (!memcmp(chandler->name, name, len)) {
1407 /* Clonable basename already exists */
1412 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1413 chandler->name = kstrdup(name, M_DEVFS);
1414 chandler->namlen = len;
1415 chandler->nhandler = nhandler;
1417 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1422 * Worker function that removes a given clone handler from the
1423 * clone handler list.
1426 devfs_chandler_del_worker(const char *name)
1428 struct devfs_clone_handler *chandler, *chandler2;
1429 u_char len = strlen(name);
1434 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1435 if (chandler->namlen != len)
1437 if (memcmp(chandler->name, name, len))
1440 TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1441 kfree(chandler->name, M_DEVFS);
1442 kfree(chandler, M_DEVFS);
1450 * Worker function that finds a given device name and changes
1451 * the message received accordingly so that when replied to,
1452 * the answer is returned to the caller.
1455 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1457 struct devfs_alias *alias;
1459 cdev_t found = NULL;
1461 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1462 if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1467 if (found == NULL) {
1468 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1469 if (strcmp(devfs_msg->mdv_name, alias->name) == 0) {
1470 found = alias->dev_target;
1475 devfs_msg->mdv_cdev = found;
1481 * Worker function that finds a given device udev and changes
1482 * the message received accordingly so that when replied to,
1483 * the answer is returned to the caller.
1486 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg)
1489 cdev_t found = NULL;
1491 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1492 if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1497 devfs_msg->mdv_cdev = found;
1503 * Worker function that inserts a given alias into the
1504 * alias list, and propagates the alias to all mount
1508 devfs_make_alias_worker(struct devfs_alias *alias)
1510 struct devfs_alias *alias2;
1511 size_t len = strlen(alias->name);
1514 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1515 if (len != alias2->namlen)
1518 if (!memcmp(alias->name, alias2->name, len)) {
1526 * The alias doesn't exist yet, so we add it to the alias list
1528 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1529 devfs_alias_propagate(alias, 0);
1530 udev_event_attach(alias->dev_target, alias->name, 1);
1532 devfs_debug(DEVFS_DEBUG_WARNING,
1533 "Warning: duplicate devfs_make_alias for %s\n",
1535 kfree(alias->name, M_DEVFS);
1536 kfree(alias, M_DEVFS);
1543 * Worker function that delete a given alias from the
1544 * alias list, and propagates the removal to all mount
1548 devfs_destroy_alias_worker(struct devfs_alias *alias)
1550 struct devfs_alias *alias2;
1553 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1554 if (alias->dev_target != alias2->dev_target)
1557 if (devfs_WildCmp(alias->name, alias2->name) == 0) {
1564 devfs_debug(DEVFS_DEBUG_WARNING,
1565 "Warning: devfs_destroy_alias for inexistant alias: %s\n",
1567 kfree(alias->name, M_DEVFS);
1568 kfree(alias, M_DEVFS);
1571 * The alias exists, so we delete it from the alias list
1573 TAILQ_REMOVE(&devfs_alias_list, alias2, link);
1574 devfs_alias_propagate(alias2, 1);
1575 udev_event_detach(alias2->dev_target, alias2->name, 1);
1576 kfree(alias->name, M_DEVFS);
1577 kfree(alias, M_DEVFS);
1578 kfree(alias2->name, M_DEVFS);
1579 kfree(alias2, M_DEVFS);
1586 * Function that removes and frees all aliases.
1589 devfs_alias_reap(void)
1591 struct devfs_alias *alias, *alias2;
1593 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1594 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1595 kfree(alias->name, M_DEVFS);
1596 kfree(alias, M_DEVFS);
1602 * Function that removes an alias matching a specific cdev and frees
1606 devfs_alias_remove(cdev_t dev)
1608 struct devfs_alias *alias, *alias2;
1610 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1611 if (alias->dev_target == dev) {
1612 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1613 udev_event_detach(alias->dev_target, alias->name, 1);
1614 kfree(alias->name, M_DEVFS);
1615 kfree(alias, M_DEVFS);
1622 * This function propagates an alias addition or removal to
1626 devfs_alias_propagate(struct devfs_alias *alias, int remove)
1628 struct devfs_mnt_data *mnt;
1630 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1632 devfs_destroy_node(mnt->root_node, alias->name);
1634 devfs_alias_apply(mnt->root_node, alias);
1641 * This function is a recursive function iterating through
1642 * all device nodes in the topology and, if applicable,
1643 * creating the relevant alias for a device node.
1646 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1648 struct devfs_node *node1, *node2;
1650 KKASSERT(alias != NULL);
1652 if ((node->node_type == Nroot) || (node->node_type == Ndir)) {
1653 if (node->nchildren > 2) {
1654 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1655 devfs_alias_apply(node1, alias);
1659 if (node->d_dev == alias->dev_target)
1660 devfs_alias_create(alias->name, node, 0);
1666 * This function checks if any alias possibly is applicable
1667 * to the given node. If so, the alias is created.
1670 devfs_alias_check_create(struct devfs_node *node)
1672 struct devfs_alias *alias;
1674 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1675 if (node->d_dev == alias->dev_target)
1676 devfs_alias_create(alias->name, node, 0);
1682 * This function creates an alias with a given name
1683 * linking to a given devfs node. It also increments
1684 * the link count on the target node.
1687 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based)
1689 struct mount *mp = target->mp;
1690 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1691 struct devfs_node *linknode;
1692 char *create_path = NULL;
1697 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1699 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1700 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1703 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1706 if (devfs_find_device_node_by_name(parent, name)) {
1707 devfs_debug(DEVFS_DEBUG_WARNING,
1708 "Node already exists: %s "
1709 "(devfs_make_alias_worker)!\n",
1715 linknode = devfs_allocp(Nlink, name, parent, mp, NULL);
1716 if (linknode == NULL) {
1721 linknode->link_target = target;
1725 linknode->flags |= DEVFS_RULE_CREATED;
1728 kfree(name_buf, M_TEMP);
1733 * This function is called by the core and handles mount point
1734 * strings. It either calls the relevant worker (devfs_apply_
1735 * reset_rules_worker) on all mountpoints or only a specific
1739 devfs_apply_reset_rules_caller(char *mountto, int apply)
1741 struct devfs_mnt_data *mnt;
1743 if (mountto[0] == '*') {
1744 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1745 devfs_iterate_topology(mnt->root_node,
1746 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1750 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1751 if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) {
1752 devfs_iterate_topology(mnt->root_node,
1753 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1760 kfree(mountto, M_DEVFS);
1765 * This function calls a given callback function for
1766 * every dev node in the devfs dev list.
1769 devfs_scan_callback_worker(devfs_scan_t *callback, void *arg)
1772 struct devfs_alias *alias, *alias1;
1774 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1775 callback(dev->si_name, dev, false, arg);
1777 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias1) {
1778 callback(alias->name, alias->dev_target, true, arg);
1785 * This function tries to resolve a given directory, or if not
1786 * found and creation requested, creates the given directory.
1788 static struct devfs_node *
1789 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1790 size_t name_len, int create)
1792 struct devfs_node *node, *found = NULL;
1794 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1795 if (name_len != node->d_dir.d_namlen)
1798 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1804 if ((found == NULL) && (create)) {
1805 found = devfs_allocp(Ndir, dir_name, parent, parent->mp, NULL);
1812 * This function tries to resolve a complete path. If creation is requested,
1813 * if a given part of the path cannot be resolved (because it doesn't exist),
1817 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1819 struct devfs_node *node = parent;
1826 buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1828 while (*path && idx < PATH_MAX - 1) {
1833 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1843 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1844 kfree (buf, M_TEMP);
1849 * Takes a full path and strips it into a directory path and a name.
1850 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
1851 * requires a working buffer with enough size to keep the whole
1855 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
1859 size_t len = strlen(fullpath) + 1;
1862 KKASSERT((fullpath != NULL) && (buf != NULL));
1863 KKASSERT((pathp != NULL) && (namep != NULL));
1865 memcpy(buf, fullpath, len);
1867 for (i = len-1; i>= 0; i--) {
1868 if (buf[i] == '/') {
1888 * This function creates a new devfs node for a given device. It can
1889 * handle a complete path as device name, and accordingly creates
1890 * the path and the final device node.
1892 * The reference count on the passed dev remains unchanged.
1895 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
1896 char *dev_name, char *path_fmt, ...)
1898 struct devfs_node *parent, *node = NULL;
1904 char *create_path = NULL;
1905 char *names = "pqrsPQRS";
1907 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1909 if (path_fmt != NULL) {
1910 __va_start(ap, path_fmt);
1911 kvasnrprintf(&path, PATH_MAX, 10, path_fmt, ap);
1915 parent = devfs_resolve_or_create_path(root, path, 1);
1918 devfs_resolve_name_path(
1919 ((dev_name == NULL) && (dev))?(dev->si_name):(dev_name),
1920 name_buf, &create_path, &name);
1923 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1926 if (devfs_find_device_node_by_name(parent, name)) {
1927 devfs_debug(DEVFS_DEBUG_WARNING, "devfs_create_device_node: "
1928 "DEVICE %s ALREADY EXISTS!!! Ignoring creation request.\n", name);
1932 node = devfs_allocp(Ndev, name, parent, parent->mp, dev);
1933 nanotime(&parent->mtime);
1936 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
1939 if ((dev) && (strlen(dev->si_name) >= 4) &&
1940 (!memcmp(dev->si_name, "ptm/", 4))) {
1941 node->parent->flags |= DEVFS_HIDDEN;
1942 node->flags |= DEVFS_HIDDEN;
1946 * Ugly pty magic, to tag pty devices as such and hide them if needed.
1948 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
1949 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1951 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
1953 for (i = 0; i < strlen(names); i++) {
1954 if (name[3] == names[i]) {
1960 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1964 kfree(name_buf, M_TEMP);
1970 * This function finds a given device node in the topology with a given
1974 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target)
1976 if ((node->node_type == Ndev) && (node->d_dev == target)) {
1984 * This function finds a device node in the given parent directory by its
1985 * name and returns it.
1988 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
1990 struct devfs_node *node, *found = NULL;
1991 size_t len = strlen(target);
1993 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1994 if (len != node->d_dir.d_namlen)
1997 if (!memcmp(node->d_dir.d_name, target, len)) {
2007 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop)
2009 struct vnode *vp = NULL;
2010 ino_t target = *inop;
2012 if (node->d_dir.d_ino == target) {
2015 vget(vp, LK_EXCLUSIVE | LK_RETRY);
2018 devfs_allocv(&vp, node);
2027 * This function takes a cdev and removes its devfs node in the
2028 * given topology. The cdev remains intact.
2031 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
2033 KKASSERT(target != NULL);
2034 return devfs_destroy_node(root, target->si_name);
2038 * This function takes a path to a devfs node, resolves it and
2039 * removes the devfs node from the given topology.
2042 devfs_destroy_node(struct devfs_node *root, char *target)
2044 struct devfs_node *node, *parent;
2047 char *create_path = NULL;
2051 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
2052 ksnprintf(name_buf, PATH_MAX, "%s", target);
2054 devfs_resolve_name_path(target, name_buf, &create_path, &name);
2057 parent = devfs_resolve_or_create_path(root, create_path, 0);
2061 if (parent == NULL) {
2062 kfree(name_buf, M_TEMP);
2066 node = devfs_find_device_node_by_name(parent, name);
2069 nanotime(&node->parent->mtime);
2073 kfree(name_buf, M_TEMP);
2079 * Just set perms and ownership for given node.
2082 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid,
2083 u_short mode, u_long flags)
2093 * Propagates a device attach/detach to all mount
2094 * points. Also takes care of automatic alias removal
2095 * for a deleted cdev.
2098 devfs_propagate_dev(cdev_t dev, int attach)
2100 struct devfs_mnt_data *mnt;
2102 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
2104 /* Device is being attached */
2105 devfs_create_device_node(mnt->root_node, dev,
2108 /* Device is being detached */
2109 devfs_alias_remove(dev);
2110 devfs_destroy_device_node(mnt->root_node, dev);
2117 * devfs_clone either returns a basename from a complete name by
2118 * returning the length of the name without trailing digits, or,
2119 * if clone != 0, calls the device's clone handler to get a new
2120 * device, which in turn is returned in devp.
2123 devfs_clone(cdev_t dev, const char *name, size_t len, int mode,
2127 struct devfs_clone_handler *chandler;
2128 struct dev_clone_args ap;
2130 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
2131 if (chandler->namlen != len)
2133 if ((!memcmp(chandler->name, name, len)) && (chandler->nhandler)) {
2134 lockmgr(&devfs_lock, LK_RELEASE);
2136 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2138 ap.a_head.a_dev = dev;
2144 error = (chandler->nhandler)(&ap);
2157 * Registers a new orphan in the orphan list.
2160 devfs_tracer_add_orphan(struct devfs_node *node)
2162 struct devfs_orphan *orphan;
2165 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2166 orphan->node = node;
2168 KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2169 node->flags |= DEVFS_ORPHANED;
2170 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2174 * Removes an orphan from the orphan list.
2177 devfs_tracer_del_orphan(struct devfs_node *node)
2179 struct devfs_orphan *orphan;
2183 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) {
2184 if (orphan->node == node) {
2185 node->flags &= ~DEVFS_ORPHANED;
2186 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2187 kfree(orphan, M_DEVFS);
2194 * Counts the orphans in the orphan list, and if cleanup
2195 * is specified, also frees the orphan and removes it from
2199 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2201 struct devfs_orphan *orphan, *orphan2;
2204 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) {
2207 * If we are instructed to clean up, we do so.
2210 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2211 orphan->node->flags &= ~DEVFS_ORPHANED;
2212 devfs_freep(orphan->node);
2213 kfree(orphan, M_DEVFS);
2221 * Fetch an ino_t from the global d_ino by increasing it
2225 devfs_fetch_ino(void)
2229 spin_lock(&ino_lock);
2231 spin_unlock(&ino_lock);
2237 * Allocates a new cdev and initializes it's most basic
2241 devfs_new_cdev(struct dev_ops *ops, int minor, struct dev_ops *bops)
2243 cdev_t dev = sysref_alloc(&cdev_sysref_class);
2245 sysref_activate(&dev->si_sysref);
2247 bzero(dev, offsetof(struct cdev, si_sysref));
2252 dev->si_drv1 = NULL;
2253 dev->si_drv2 = NULL;
2254 dev->si_lastread = 0; /* time_uptime */
2255 dev->si_lastwrite = 0; /* time_uptime */
2257 dev->si_dict = NULL;
2258 dev->si_parent = NULL;
2261 dev->si_uminor = minor;
2262 dev->si_bops = bops;
2265 * Since the disk subsystem is in the way, we need to
2266 * propagate the D_CANFREE from bops (and ops) to
2269 if (bops && (bops->head.flags & D_CANFREE)) {
2270 dev->si_flags |= SI_CANFREE;
2271 } else if (ops->head.flags & D_CANFREE) {
2272 dev->si_flags |= SI_CANFREE;
2275 /* If there is a backing device, we reference its ops */
2276 dev->si_inode = makeudev(
2277 devfs_reference_ops((bops)?(bops):(ops)),
2279 dev->si_umajor = umajor(dev->si_inode);
2285 devfs_cdev_terminate(cdev_t dev)
2289 /* Check if it is locked already. if not, we acquire the devfs lock */
2290 if ((lockstatus(&devfs_lock, curthread)) != LK_EXCLUSIVE) {
2291 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2296 * Make sure the node isn't linked anymore. Otherwise we've screwed
2297 * up somewhere, since normal devs are unlinked on the call to
2298 * destroy_dev and only-cdevs that have not been used for cloning
2299 * are not linked in the first place. only-cdevs used for cloning
2300 * will be linked in, too, and should only be destroyed via
2301 * destroy_dev, not destroy_only_dev, so we catch that problem, too.
2303 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2305 /* If we acquired the lock, we also get rid of it */
2307 lockmgr(&devfs_lock, LK_RELEASE);
2309 /* If there is a backing device, we release the backing device's ops */
2310 devfs_release_ops((dev->si_bops)?(dev->si_bops):(dev->si_ops));
2312 /* Finally destroy the device */
2313 sysref_put(&dev->si_sysref);
2317 * Dummies for now (individual locks for MPSAFE)
2320 devfs_cdev_lock(cdev_t dev)
2325 devfs_cdev_unlock(cdev_t dev)
2330 devfs_detached_filter_eof(struct knote *kn, long hint)
2332 kn->kn_flags |= (EV_EOF | EV_NODATA);
2337 devfs_detached_filter_detach(struct knote *kn)
2339 cdev_t dev = (cdev_t)kn->kn_hook;
2341 knote_remove(&dev->si_kqinfo.ki_note, kn);
2344 static struct filterops devfs_detached_filterops =
2345 { FILTEROP_ISFD, NULL,
2346 devfs_detached_filter_detach,
2347 devfs_detached_filter_eof };
2350 * Delegates knote filter handling responsibility to devfs
2352 * Any device that implements kqfilter event handling and could be detached
2353 * or shut down out from under the kevent subsystem must allow devfs to
2354 * assume responsibility for any knotes it may hold.
2357 devfs_assume_knotes(cdev_t dev, struct kqinfo *kqi)
2360 * Let kern/kern_event.c do the heavy lifting.
2362 knote_assume_knotes(kqi, &dev->si_kqinfo,
2363 &devfs_detached_filterops, (void *)dev);
2366 * These should probably be activated individually, but doing so
2367 * would require refactoring kq's public in-kernel interface.
2369 KNOTE(&dev->si_kqinfo.ki_note, 0);
2373 * Links a given cdev into the dev list.
2376 devfs_link_dev(cdev_t dev)
2378 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2379 dev->si_flags |= SI_DEVFS_LINKED;
2380 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2386 * Removes a given cdev from the dev list. The caller is responsible for
2387 * releasing the reference on the device associated with the linkage.
2389 * Returns EALREADY if the dev has already been unlinked.
2392 devfs_unlink_dev(cdev_t dev)
2394 if ((dev->si_flags & SI_DEVFS_LINKED)) {
2395 TAILQ_REMOVE(&devfs_dev_list, dev, link);
2396 dev->si_flags &= ~SI_DEVFS_LINKED;
2403 devfs_node_is_accessible(struct devfs_node *node)
2405 if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2412 devfs_reference_ops(struct dev_ops *ops)
2415 struct devfs_dev_ops *found = NULL;
2416 struct devfs_dev_ops *devops;
2418 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2419 if (devops->ops == ops) {
2426 found = kmalloc(sizeof(struct devfs_dev_ops), M_DEVFS, M_WAITOK);
2428 found->ref_count = 0;
2429 TAILQ_INSERT_TAIL(&devfs_dev_ops_list, found, link);
2434 if (found->ref_count == 0) {
2435 found->id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255);
2436 if (found->id == -1) {
2437 /* Ran out of unique ids */
2438 devfs_debug(DEVFS_DEBUG_WARNING,
2439 "devfs_reference_ops: WARNING: ran out of unique ids\n");
2449 devfs_release_ops(struct dev_ops *ops)
2451 struct devfs_dev_ops *found = NULL;
2452 struct devfs_dev_ops *devops;
2454 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2455 if (devops->ops == ops) {
2465 if (found->ref_count == 0) {
2466 TAILQ_REMOVE(&devfs_dev_ops_list, found, link);
2467 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), found->id);
2468 kfree(found, M_DEVFS);
2473 * Wait for asynchronous messages to complete in the devfs helper
2474 * thread, then return. Do nothing if the helper thread is dead
2475 * or we are being indirectly called from the helper thread itself.
2482 if (devfs_run && curthread != td_core) {
2483 msg = devfs_msg_get();
2484 msg = devfs_msg_send_sync(DEVFS_SYNC, msg);
2490 * Called on init of devfs; creates the objcaches and
2491 * spawns off the devfs core thread. Also initializes
2497 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2498 /* Create objcaches for nodes, msgs and devs */
2499 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2501 objcache_malloc_alloc,
2502 objcache_malloc_free,
2503 &devfs_node_malloc_args );
2505 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2507 objcache_malloc_alloc,
2508 objcache_malloc_free,
2509 &devfs_msg_malloc_args );
2511 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2513 objcache_malloc_alloc,
2514 objcache_malloc_free,
2515 &devfs_dev_malloc_args );
2517 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2519 /* Initialize the reply-only port which acts as a message drain */
2520 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2522 /* Initialize *THE* devfs lock */
2523 lockinit(&devfs_lock, "devfs_core lock", 0, 0);
2525 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2526 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2527 0, -1, "devfs_msg_core");
2528 while (devfs_run == 0)
2529 lksleep(td_core, &devfs_lock, 0, "devfsc", 0);
2530 lockmgr(&devfs_lock, LK_RELEASE);
2532 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2536 * Called on unload of devfs; takes care of destroying the core
2537 * and the objcaches. Also removes aliases that are no longer needed.
2542 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2544 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2546 tsleep(td_core, 0, "devfsc", hz*10);
2547 tsleep(td_core, 0, "devfsc", hz);
2549 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2551 /* Destroy the objcaches */
2552 objcache_destroy(devfs_msg_cache);
2553 objcache_destroy(devfs_node_cache);
2554 objcache_destroy(devfs_dev_cache);
2560 * This is a sysctl handler to assist userland devname(3) to
2561 * find the device name for a given udev.
2564 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2571 if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t))))
2574 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev);
2579 if ((found = devfs_find_device_by_udev(udev)) == NULL)
2582 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2586 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
2587 NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)");
2589 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2590 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2591 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable,
2592 0, "Enable DevFS debugging");
2594 SYSINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST,
2596 SYSUNINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY,
2597 devfs_uninit, NULL);
2600 * WildCmp() - compare wild string to sane string
2602 * Returns 0 on success, -1 on failure.
2605 wildCmp(const char **mary, int d, const char *w, const char *s)
2610 * skip fixed portion
2616 * optimize terminator
2620 if (w[1] != '?' && w[1] != '*') {
2622 * optimize * followed by non-wild
2624 for (i = 0; s + i < mary[d]; ++i) {
2625 if (s[i] == w[1] && wildCmp(mary, d + 1, w + 1, s + i) == 0)
2632 for (i = 0; s + i < mary[d]; ++i) {
2633 if (wildCmp(mary, d + 1, w + 1, s + i) == 0)
2648 if (*w == 0) /* terminator */
2661 * WildCaseCmp() - compare wild string to sane string, case insensitive
2663 * Returns 0 on success, -1 on failure.
2666 wildCaseCmp(const char **mary, int d, const char *w, const char *s)
2671 * skip fixed portion
2677 * optimize terminator
2681 if (w[1] != '?' && w[1] != '*') {
2683 * optimize * followed by non-wild
2685 for (i = 0; s + i < mary[d]; ++i) {
2686 if (s[i] == w[1] && wildCaseCmp(mary, d + 1, w + 1, s + i) == 0)
2693 for (i = 0; s + i < mary[d]; ++i) {
2694 if (wildCaseCmp(mary, d + 1, w + 1, s + i) == 0)
2708 #define tolower(x) ((x >= 'A' && x <= 'Z')?(x+('a'-'A')):(x))
2709 if (tolower(*w) != tolower(*s))
2712 if (*w == 0) /* terminator */
2723 struct cdev_privdata {
2725 cdevpriv_dtr_t cdpd_dtr;
2728 int devfs_get_cdevpriv(struct file *fp, void **datap)
2730 struct cdev_privdata *p;
2735 p = (struct cdev_privdata*) fp->f_data1;
2738 *datap = p->cdpd_data;
2744 int devfs_set_cdevpriv(struct file *fp, void *priv, cdevpriv_dtr_t dtr)
2746 struct cdev_privdata *p;
2752 p = kmalloc(sizeof(struct cdev_privdata), M_DEVFS, M_WAITOK);
2753 p->cdpd_data = priv;
2756 spin_lock(&fp->f_spin);
2757 if (fp->f_data1 == NULL) {
2762 spin_unlock(&fp->f_spin);
2770 void devfs_clear_cdevpriv(struct file *fp)
2772 struct cdev_privdata *p;
2777 spin_lock(&fp->f_spin);
2780 spin_unlock(&fp->f_spin);
2783 (p->cdpd_dtr)(p->cdpd_data);
2789 devfs_WildCmp(const char *w, const char *s)
2793 int slen = strlen(s);
2796 for (i = c = 0; w[i]; ++i) {
2800 mary = kmalloc(sizeof(char *) * (c + 1), M_DEVFS, M_WAITOK);
2801 for (i = 0; i < c; ++i)
2803 i = wildCmp(mary, 0, w, s);
2804 kfree(mary, M_DEVFS);
2809 devfs_WildCaseCmp(const char *w, const char *s)
2813 int slen = strlen(s);
2816 for (i = c = 0; w[i]; ++i) {
2820 mary = kmalloc(sizeof(char *) * (c + 1), M_DEVFS, M_WAITOK);
2821 for (i = 0; i < c; ++i)
2823 i = wildCaseCmp(mary, 0, w, s);
2824 kfree(mary, M_DEVFS);