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>
38 #include <sys/mount.h>
39 #include <sys/vnode.h>
40 #include <sys/types.h>
43 #include <sys/msgport.h>
44 #include <sys/sysctl.h>
45 #include <sys/ucred.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_related_worker(cdev_t);
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_related_flag_worker(cdev_t, uint32_t);
146 static int devfs_destroy_related_without_flag_worker(cdev_t, 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 == Nroot) || (parent->node_type == Ndir))) {
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 * devfs master lock must not be held across a vget() call, we have
297 * to hold our ad-hoc vp to avoid a free race from destroying the
298 * contents of the structure. The vget() will interlock recycles
302 while ((vp = node->v_node) != NULL) {
304 lockmgr(&devfs_lock, LK_RELEASE);
305 error = vget(vp, LK_EXCLUSIVE);
307 lockmgr(&devfs_lock, LK_EXCLUSIVE);
312 if (error != ENOENT) {
319 * devfs master lock must not be held across a getnewvnode() call.
321 lockmgr(&devfs_lock, LK_RELEASE);
322 if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0) {
323 lockmgr(&devfs_lock, LK_EXCLUSIVE);
326 lockmgr(&devfs_lock, LK_EXCLUSIVE);
330 if (node->v_node != NULL) {
339 switch (node->node_type) {
341 vsetflags(vp, VROOT);
357 KKASSERT(node->d_dev);
359 vp->v_uminor = node->d_dev->si_uminor;
360 vp->v_umajor = node->d_dev->si_umajor;
362 v_associate_rdev(vp, node->d_dev);
363 vp->v_ops = &node->mp->mnt_vn_spec_ops;
367 panic("devfs_allocv: unknown node type");
375 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
376 * based on the newly created devfs node.
379 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype,
380 char *name, struct devfs_node *parent, cdev_t dev)
382 struct devfs_node *node;
384 node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
387 devfs_allocv(vpp, node);
395 * Destroy the devfs_node. The node must be unlinked from the topology.
397 * This function will also destroy any vnode association with the node
400 * The cdev_t itself remains intact.
402 * The core lock is not necessarily held on call and must be temporarily
403 * released if it is to avoid a deadlock.
406 devfs_freep(struct devfs_node *node)
412 KKASSERT(((node->flags & DEVFS_NODE_LINKED) == 0) ||
413 (node->node_type == Nroot));
416 * Protect against double frees
418 KKASSERT((node->flags & DEVFS_DESTROYED) == 0);
419 node->flags |= DEVFS_DESTROYED;
422 * Avoid deadlocks between devfs_lock and the vnode lock when
423 * disassociating the vnode (stress2 pty vs ls -la /dev/pts).
425 * This also prevents the vnode reclaim code from double-freeing
426 * the node. The vget() is required to safely modified the vp
427 * and cycle the refs to terminate an inactive vp.
429 if (lockstatus(&devfs_lock, curthread) == LK_EXCLUSIVE) {
430 lockmgr(&devfs_lock, LK_RELEASE);
436 while ((vp = node->v_node) != NULL) {
437 if (vget(vp, LK_EXCLUSIVE | LK_RETRY) != 0)
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 * Returns a valid vp associated with the devfs alias node or NULL
476 static void *devfs_alias_getvp(struct devfs_node *node)
478 struct devfs_node *found = node;
481 while ((found->node_type == Nlink) && (found->link_target)) {
483 devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8");
487 found = found->link_target;
491 return found->v_node;
495 * Unlink the devfs node from the topology and add it to the orphan list.
496 * The node will later be destroyed by freep.
498 * Any vnode association, including the v_rdev and v_data, remains intact
502 devfs_unlinkp(struct devfs_node *node)
505 struct devfs_node *parent;
509 * Add the node to the orphan list, so it is referenced somewhere, to
510 * so we don't leak it.
512 devfs_tracer_add_orphan(node);
514 parent = node->parent;
517 * If the parent is known we can unlink the node out of the topology
520 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
522 node->flags &= ~DEVFS_NODE_LINKED;
528 * Namecache invalidation.
529 * devfs alias nodes are special: their v_node entry is always null
530 * and they use the one from their link target.
531 * We thus use the target node's vp to invalidate both alias and target
532 * entries in the namecache.
533 * Doing so for the target is not necessary but it would be more
534 * expensive to resolve only the namecache entry of the alias node
535 * from the information available in this function.
537 if (node->node_type == Nlink)
538 vp = devfs_alias_getvp(node);
543 cache_inval_vp(vp, CINV_DESTROY);
549 devfs_iterate_topology(struct devfs_node *node,
550 devfs_iterate_callback_t *callback, void *arg1)
552 struct devfs_node *node1, *node2;
555 if ((node->node_type == Nroot) || (node->node_type == Ndir)) {
556 if (node->nchildren > 2) {
557 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
559 if ((ret = devfs_iterate_topology(node1, callback, arg1)))
565 ret = callback(node, arg1);
570 devfs_alias_reaper_callback(struct devfs_node *node, void *unused)
572 if (node->node_type == Nlink) {
581 * devfs_reaperp() is a recursive function that iterates through all the
582 * topology, unlinking and freeing all devfs nodes.
585 devfs_reaperp_callback(struct devfs_node *node, void *unused)
594 devfs_gc_dirs_callback(struct devfs_node *node, void *unused)
596 if (node->node_type == Ndir) {
597 if ((node->nchildren == 2) &&
598 !(node->flags & DEVFS_USER_CREATED)) {
608 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target)
610 if ((node->node_type == Nlink) && (node->link_target == target)) {
619 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
620 * freeing a node, but also removes empty directories and links that link
621 * via devfs auto-link mechanism to the node being deleted.
624 devfs_gc(struct devfs_node *node)
626 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
628 if (node->nlinks > 0)
629 devfs_iterate_topology(root_node,
630 (devfs_iterate_callback_t *)devfs_gc_links_callback, node);
633 devfs_iterate_topology(root_node,
634 (devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL);
642 * devfs_create_dev() is the asynchronous entry point for device creation.
643 * It just sends a message with the relevant details to the devfs core.
645 * This function will reference the passed device. The reference is owned
646 * by devfs and represents all of the device's node associations.
649 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
652 devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
658 * devfs_destroy_dev() is the asynchronous entry point for device destruction.
659 * It just sends a message with the relevant details to the devfs core.
662 devfs_destroy_dev(cdev_t dev)
664 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
669 * devfs_mount_add() is the synchronous entry point for adding a new devfs
670 * mount. It sends a synchronous message with the relevant details to the
674 devfs_mount_add(struct devfs_mnt_data *mnt)
678 msg = devfs_msg_get();
680 msg = devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
687 * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
688 * It sends a synchronous message with the relevant details to the devfs core.
691 devfs_mount_del(struct devfs_mnt_data *mnt)
695 msg = devfs_msg_get();
697 msg = devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
704 * devfs_destroy_related() is the synchronous entry point for device
705 * destruction by subname. It just sends a message with the relevant details to
709 devfs_destroy_related(cdev_t dev)
713 msg = devfs_msg_get();
715 msg = devfs_msg_send_sync(DEVFS_DESTROY_RELATED, msg);
721 devfs_clr_related_flag(cdev_t dev, uint32_t flag)
725 msg = devfs_msg_get();
726 msg->mdv_flags.dev = dev;
727 msg->mdv_flags.flag = flag;
728 msg = devfs_msg_send_sync(DEVFS_CLR_RELATED_FLAG, msg);
735 devfs_destroy_related_without_flag(cdev_t dev, uint32_t flag)
739 msg = devfs_msg_get();
740 msg->mdv_flags.dev = dev;
741 msg->mdv_flags.flag = flag;
742 msg = devfs_msg_send_sync(DEVFS_DESTROY_RELATED_WO_FLAG, msg);
749 * devfs_create_all_dev is the asynchronous entry point to trigger device
750 * node creation. It just sends a message with the relevant details to
754 devfs_create_all_dev(struct devfs_node *root)
756 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
761 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
762 * devices with a specific set of dev_ops and minor. It just sends a
763 * message with the relevant details to the devfs core.
766 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
768 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
773 * devfs_clone_handler_add is the synchronous entry point to add a new
774 * clone handler. It just sends a message with the relevant details to
778 devfs_clone_handler_add(const char *name, d_clone_t *nhandler)
782 msg = devfs_msg_get();
783 msg->mdv_chandler.name = name;
784 msg->mdv_chandler.nhandler = nhandler;
785 msg = devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg);
791 * devfs_clone_handler_del is the synchronous entry point to remove a
792 * clone handler. It just sends a message with the relevant details to
796 devfs_clone_handler_del(const char *name)
800 msg = devfs_msg_get();
801 msg->mdv_chandler.name = name;
802 msg->mdv_chandler.nhandler = NULL;
803 msg = devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg);
809 * devfs_find_device_by_name is the synchronous entry point to find a
810 * device given its name. It sends a synchronous message with the
811 * relevant details to the devfs core and returns the answer.
814 devfs_find_device_by_name(const char *fmt, ...)
825 kvasnrprintf(&target, PATH_MAX, 10, fmt, ap);
828 msg = devfs_msg_get();
829 msg->mdv_name = target;
830 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
831 found = msg->mdv_cdev;
839 * devfs_find_device_by_udev is the synchronous entry point to find a
840 * device given its udev number. It sends a synchronous message with
841 * the relevant details to the devfs core and returns the answer.
844 devfs_find_device_by_udev(udev_t udev)
849 msg = devfs_msg_get();
850 msg->mdv_udev = udev;
851 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg);
852 found = msg->mdv_cdev;
855 devfs_debug(DEVFS_DEBUG_DEBUG,
856 "devfs_find_device_by_udev found? %s -end:3-\n",
857 ((found) ? found->si_name:"NO"));
862 devfs_inode_to_vnode(struct mount *mp, ino_t target)
864 struct vnode *vp = NULL;
870 msg = devfs_msg_get();
871 msg->mdv_ino.mp = mp;
872 msg->mdv_ino.ino = target;
873 msg = devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg);
874 vp = msg->mdv_ino.vp;
875 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
882 * devfs_make_alias is the asynchronous entry point to register an alias
883 * for a device. It just sends a message with the relevant details to the
887 devfs_make_alias(const char *name, cdev_t dev_target)
889 struct devfs_alias *alias;
894 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
895 alias->name = kstrdup(name, M_DEVFS);
897 alias->dev_target = dev_target;
899 devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
904 * devfs_destroy_alias is the asynchronous entry point to deregister an alias
905 * for a device. It just sends a message with the relevant details to the
909 devfs_destroy_alias(const char *name, cdev_t dev_target)
911 struct devfs_alias *alias;
916 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
917 alias->name = kstrdup(name, M_DEVFS);
919 alias->dev_target = dev_target;
921 devfs_msg_send_generic(DEVFS_DESTROY_ALIAS, alias);
926 * devfs_apply_rules is the asynchronous entry point to trigger application
927 * of all rules. It just sends a message with the relevant details to the
931 devfs_apply_rules(char *mntto)
935 new_name = kstrdup(mntto, M_DEVFS);
936 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
942 * devfs_reset_rules is the asynchronous entry point to trigger reset of all
943 * rules. It just sends a message with the relevant details to the devfs core.
946 devfs_reset_rules(char *mntto)
950 new_name = kstrdup(mntto, M_DEVFS);
951 devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
958 * devfs_scan_callback is the asynchronous entry point to call a callback
960 * It just sends a message with the relevant details to the devfs core.
963 devfs_scan_callback(devfs_scan_t *callback, void *arg)
969 msg = devfs_msg_get();
970 msg->mdv_load = callback;
971 msg->mdv_load2 = arg;
972 msg = devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
980 * Acts as a message drain. Any message that is replied to here gets destroyed
981 * and the memory freed.
984 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
986 devfs_msg_put((devfs_msg_t)msg);
990 * devfs_msg_get allocates a new devfs msg and returns it.
995 return objcache_get(devfs_msg_cache, M_WAITOK);
999 * devfs_msg_put deallocates a given devfs msg.
1002 devfs_msg_put(devfs_msg_t msg)
1004 objcache_put(devfs_msg_cache, msg);
1009 * devfs_msg_send is the generic asynchronous message sending facility
1010 * for devfs. By default the reply port is the automatic disposal port.
1012 * If the current thread is the devfs_msg_port thread we execute the
1013 * operation synchronously.
1016 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
1018 lwkt_port_t port = &devfs_msg_port;
1020 lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
1022 devfs_msg->hdr.u.ms_result = cmd;
1024 if (port->mpu_td == curthread) {
1025 devfs_msg_exec(devfs_msg);
1026 lwkt_replymsg(&devfs_msg->hdr, 0);
1028 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
1033 * devfs_msg_send_sync is the generic synchronous message sending
1034 * facility for devfs. It initializes a local reply port and waits
1035 * for the core's answer. This answer is then returned.
1038 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
1040 struct lwkt_port rep_port;
1041 devfs_msg_t msg_incoming;
1042 lwkt_port_t port = &devfs_msg_port;
1044 lwkt_initport_thread(&rep_port, curthread);
1045 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
1047 devfs_msg->hdr.u.ms_result = cmd;
1049 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
1050 msg_incoming = lwkt_waitport(&rep_port, 0);
1052 return msg_incoming;
1056 * sends a message with a generic argument.
1059 devfs_msg_send_generic(uint32_t cmd, void *load)
1061 devfs_msg_t devfs_msg = devfs_msg_get();
1063 devfs_msg->mdv_load = load;
1064 devfs_msg_send(cmd, devfs_msg);
1068 * sends a message with a name argument.
1071 devfs_msg_send_name(uint32_t cmd, char *name)
1073 devfs_msg_t devfs_msg = devfs_msg_get();
1075 devfs_msg->mdv_name = name;
1076 devfs_msg_send(cmd, devfs_msg);
1080 * sends a message with a mount argument.
1083 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
1085 devfs_msg_t devfs_msg = devfs_msg_get();
1087 devfs_msg->mdv_mnt = mnt;
1088 devfs_msg_send(cmd, devfs_msg);
1092 * sends a message with an ops argument.
1095 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
1097 devfs_msg_t devfs_msg = devfs_msg_get();
1099 devfs_msg->mdv_ops.ops = ops;
1100 devfs_msg->mdv_ops.minor = minor;
1101 devfs_msg_send(cmd, devfs_msg);
1105 * sends a message with a clone handler argument.
1108 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
1110 devfs_msg_t devfs_msg = devfs_msg_get();
1112 devfs_msg->mdv_chandler.name = name;
1113 devfs_msg->mdv_chandler.nhandler = handler;
1114 devfs_msg_send(cmd, devfs_msg);
1118 * sends a message with a device argument.
1121 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1123 devfs_msg_t devfs_msg = devfs_msg_get();
1125 devfs_msg->mdv_dev.dev = dev;
1126 devfs_msg->mdv_dev.uid = uid;
1127 devfs_msg->mdv_dev.gid = gid;
1128 devfs_msg->mdv_dev.perms = perms;
1130 devfs_msg_send(cmd, devfs_msg);
1134 * sends a message with a link argument.
1137 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1139 devfs_msg_t devfs_msg = devfs_msg_get();
1141 devfs_msg->mdv_link.name = name;
1142 devfs_msg->mdv_link.target = target;
1143 devfs_msg->mdv_link.mp = mp;
1144 devfs_msg_send(cmd, devfs_msg);
1148 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1149 * and calls the relevant worker functions. By using messages it's assured
1150 * that events occur in the correct order.
1153 devfs_msg_core(void *arg)
1157 lwkt_initport_thread(&devfs_msg_port, curthread);
1159 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1162 lockmgr(&devfs_lock, LK_RELEASE);
1164 get_mplock(); /* mpsafe yet? */
1167 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1168 devfs_debug(DEVFS_DEBUG_DEBUG,
1169 "devfs_msg_core, new msg: %x\n",
1170 (unsigned int)msg->hdr.u.ms_result);
1171 devfs_msg_exec(msg);
1172 lwkt_replymsg(&msg->hdr, 0);
1182 devfs_msg_exec(devfs_msg_t msg)
1184 struct devfs_mnt_data *mnt;
1185 struct devfs_node *node;
1189 * Acquire the devfs lock to ensure safety of all called functions
1191 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1193 switch (msg->hdr.u.ms_result) {
1194 case DEVFS_DEVICE_CREATE:
1195 dev = msg->mdv_dev.dev;
1196 devfs_create_dev_worker(dev,
1199 msg->mdv_dev.perms);
1201 case DEVFS_DEVICE_DESTROY:
1202 dev = msg->mdv_dev.dev;
1203 devfs_destroy_dev_worker(dev);
1205 case DEVFS_DESTROY_RELATED:
1206 devfs_destroy_related_worker(msg->mdv_load);
1208 case DEVFS_DESTROY_DEV_BY_OPS:
1209 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1210 msg->mdv_ops.minor);
1212 case DEVFS_CREATE_ALL_DEV:
1213 node = (struct devfs_node *)msg->mdv_load;
1214 devfs_create_all_dev_worker(node);
1216 case DEVFS_MOUNT_ADD:
1218 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1219 devfs_create_all_dev_worker(mnt->root_node);
1221 case DEVFS_MOUNT_DEL:
1223 TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1224 /* Be sure to remove all the aliases first */
1225 devfs_iterate_topology(mnt->root_node, devfs_alias_reaper_callback,
1227 devfs_iterate_topology(mnt->root_node, devfs_reaperp_callback,
1229 if (mnt->leak_count) {
1230 devfs_debug(DEVFS_DEBUG_SHOW,
1231 "Leaked %ld devfs_node elements!\n",
1235 case DEVFS_CHANDLER_ADD:
1236 devfs_chandler_add_worker(msg->mdv_chandler.name,
1237 msg->mdv_chandler.nhandler);
1239 case DEVFS_CHANDLER_DEL:
1240 devfs_chandler_del_worker(msg->mdv_chandler.name);
1242 case DEVFS_FIND_DEVICE_BY_NAME:
1243 devfs_find_device_by_name_worker(msg);
1245 case DEVFS_FIND_DEVICE_BY_UDEV:
1246 devfs_find_device_by_udev_worker(msg);
1248 case DEVFS_MAKE_ALIAS:
1249 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1251 case DEVFS_DESTROY_ALIAS:
1252 devfs_destroy_alias_worker((struct devfs_alias *)msg->mdv_load);
1254 case DEVFS_APPLY_RULES:
1255 devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1257 case DEVFS_RESET_RULES:
1258 devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1260 case DEVFS_SCAN_CALLBACK:
1261 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load,
1264 case DEVFS_CLR_RELATED_FLAG:
1265 devfs_clr_related_flag_worker(msg->mdv_flags.dev,
1266 msg->mdv_flags.flag);
1268 case DEVFS_DESTROY_RELATED_WO_FLAG:
1269 devfs_destroy_related_without_flag_worker(msg->mdv_flags.dev,
1270 msg->mdv_flags.flag);
1272 case DEVFS_INODE_TO_VNODE:
1273 msg->mdv_ino.vp = devfs_iterate_topology(
1274 DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node,
1275 (devfs_iterate_callback_t *)devfs_inode_to_vnode_worker_callback,
1278 case DEVFS_TERMINATE_CORE:
1284 devfs_debug(DEVFS_DEBUG_WARNING,
1285 "devfs_msg_core: unknown message "
1286 "received at core\n");
1289 lockmgr(&devfs_lock, LK_RELEASE);
1293 devfs_devctl_notify(cdev_t dev, const char *ev)
1295 static const char prefix[] = "cdev=";
1299 namelen = strlen(dev->si_name);
1300 data = kmalloc(namelen + sizeof(prefix), M_TEMP, M_WAITOK);
1301 memcpy(data, prefix, sizeof(prefix) - 1);
1302 memcpy(data + sizeof(prefix) - 1, dev->si_name, namelen + 1);
1303 devctl_notify("DEVFS", "CDEV", ev, data);
1304 kfree(data, M_TEMP);
1308 * Worker function to insert a new dev into the dev list and initialize its
1309 * permissions. It also calls devfs_propagate_dev which in turn propagates
1310 * the change to all mount points.
1312 * The passed dev is already referenced. This reference is eaten by this
1313 * function and represents the dev's linkage into devfs_dev_list.
1316 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1322 dev->si_perms = perms;
1324 devfs_link_dev(dev);
1325 devfs_propagate_dev(dev, 1);
1327 udev_event_attach(dev, NULL, 0);
1328 devfs_devctl_notify(dev, "CREATE");
1334 * Worker function to delete a dev from the dev list and free the cdev.
1335 * It also calls devfs_propagate_dev which in turn propagates the change
1336 * to all mount points.
1339 devfs_destroy_dev_worker(cdev_t dev)
1344 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1346 error = devfs_unlink_dev(dev);
1347 devfs_propagate_dev(dev, 0);
1349 devfs_devctl_notify(dev, "DESTROY");
1350 udev_event_detach(dev, NULL, 0);
1353 release_dev(dev); /* link ref */
1361 * Worker function to destroy all devices with a certain basename.
1362 * Calls devfs_destroy_dev_worker for the actual destruction.
1365 devfs_destroy_related_worker(cdev_t needle)
1370 devfs_debug(DEVFS_DEBUG_DEBUG, "related worker: %s\n",
1372 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1373 if (dev->si_parent == needle) {
1374 devfs_destroy_related_worker(dev);
1375 devfs_destroy_dev_worker(dev);
1383 devfs_clr_related_flag_worker(cdev_t needle, uint32_t flag)
1387 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1388 if (dev->si_parent == needle) {
1389 devfs_clr_related_flag_worker(dev, flag);
1390 dev->si_flags &= ~flag;
1398 devfs_destroy_related_without_flag_worker(cdev_t needle, uint32_t flag)
1403 devfs_debug(DEVFS_DEBUG_DEBUG, "related_wo_flag: %s\n",
1406 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1407 if (dev->si_parent == needle) {
1408 devfs_destroy_related_without_flag_worker(dev, flag);
1409 if (!(dev->si_flags & flag)) {
1410 devfs_destroy_dev_worker(dev);
1411 devfs_debug(DEVFS_DEBUG_DEBUG,
1412 "related_wo_flag: %s restart\n", dev->si_name);
1422 * Worker function that creates all device nodes on top of a devfs
1426 devfs_create_all_dev_worker(struct devfs_node *root)
1432 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1433 devfs_create_device_node(root, dev, NULL, NULL, NULL);
1440 * Worker function that destroys all devices that match a specific
1441 * dev_ops and/or minor. If minor is less than 0, it is not matched
1442 * against. It also propagates all changes.
1445 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1451 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1452 if (dev->si_ops != ops)
1454 if ((minor < 0) || (dev->si_uminor == minor)) {
1455 devfs_destroy_dev_worker(dev);
1463 * Worker function that registers a new clone handler in devfs.
1466 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler)
1468 struct devfs_clone_handler *chandler = NULL;
1469 u_char len = strlen(name);
1474 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1475 if (chandler->namlen != len)
1478 if (!memcmp(chandler->name, name, len)) {
1479 /* Clonable basename already exists */
1484 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1485 chandler->name = kstrdup(name, M_DEVFS);
1486 chandler->namlen = len;
1487 chandler->nhandler = nhandler;
1489 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1494 * Worker function that removes a given clone handler from the
1495 * clone handler list.
1498 devfs_chandler_del_worker(const char *name)
1500 struct devfs_clone_handler *chandler, *chandler2;
1501 u_char len = strlen(name);
1506 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1507 if (chandler->namlen != len)
1509 if (memcmp(chandler->name, name, len))
1512 TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1513 kfree(chandler->name, M_DEVFS);
1514 kfree(chandler, M_DEVFS);
1522 * Worker function that finds a given device name and changes
1523 * the message received accordingly so that when replied to,
1524 * the answer is returned to the caller.
1527 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1529 struct devfs_alias *alias;
1531 cdev_t found = NULL;
1533 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1534 if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1539 if (found == NULL) {
1540 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1541 if (strcmp(devfs_msg->mdv_name, alias->name) == 0) {
1542 found = alias->dev_target;
1547 devfs_msg->mdv_cdev = found;
1553 * Worker function that finds a given device udev and changes
1554 * the message received accordingly so that when replied to,
1555 * the answer is returned to the caller.
1558 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg)
1561 cdev_t found = NULL;
1563 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1564 if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1569 devfs_msg->mdv_cdev = found;
1575 * Worker function that inserts a given alias into the
1576 * alias list, and propagates the alias to all mount
1580 devfs_make_alias_worker(struct devfs_alias *alias)
1582 struct devfs_alias *alias2;
1583 size_t len = strlen(alias->name);
1586 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1587 if (len != alias2->namlen)
1590 if (!memcmp(alias->name, alias2->name, len)) {
1598 * The alias doesn't exist yet, so we add it to the alias list
1600 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1601 devfs_alias_propagate(alias, 0);
1602 udev_event_attach(alias->dev_target, alias->name, 1);
1604 devfs_debug(DEVFS_DEBUG_WARNING,
1605 "Warning: duplicate devfs_make_alias for %s\n",
1607 kfree(alias->name, M_DEVFS);
1608 kfree(alias, M_DEVFS);
1615 * Worker function that delete a given alias from the
1616 * alias list, and propagates the removal to all mount
1620 devfs_destroy_alias_worker(struct devfs_alias *alias)
1622 struct devfs_alias *alias2;
1625 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1626 if (alias->dev_target != alias2->dev_target)
1629 if (devfs_WildCmp(alias->name, alias2->name) == 0) {
1636 devfs_debug(DEVFS_DEBUG_WARNING,
1637 "Warning: devfs_destroy_alias for inexistant alias: %s\n",
1639 kfree(alias->name, M_DEVFS);
1640 kfree(alias, M_DEVFS);
1643 * The alias exists, so we delete it from the alias list
1645 TAILQ_REMOVE(&devfs_alias_list, alias2, link);
1646 devfs_alias_propagate(alias2, 1);
1647 udev_event_detach(alias2->dev_target, alias2->name, 1);
1648 kfree(alias->name, M_DEVFS);
1649 kfree(alias, M_DEVFS);
1650 kfree(alias2->name, M_DEVFS);
1651 kfree(alias2, M_DEVFS);
1658 * Function that removes and frees all aliases.
1661 devfs_alias_reap(void)
1663 struct devfs_alias *alias, *alias2;
1665 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1666 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1667 kfree(alias->name, M_DEVFS);
1668 kfree(alias, M_DEVFS);
1674 * Function that removes an alias matching a specific cdev and frees
1678 devfs_alias_remove(cdev_t dev)
1680 struct devfs_alias *alias, *alias2;
1682 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1683 if (alias->dev_target == dev) {
1684 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1685 udev_event_detach(alias->dev_target, alias->name, 1);
1686 kfree(alias->name, M_DEVFS);
1687 kfree(alias, M_DEVFS);
1694 * This function propagates an alias addition or removal to
1698 devfs_alias_propagate(struct devfs_alias *alias, int remove)
1700 struct devfs_mnt_data *mnt;
1702 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1704 devfs_destroy_node(mnt->root_node, alias->name);
1706 devfs_alias_apply(mnt->root_node, alias);
1713 * This function is a recursive function iterating through
1714 * all device nodes in the topology and, if applicable,
1715 * creating the relevant alias for a device node.
1718 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1720 struct devfs_node *node1, *node2;
1722 KKASSERT(alias != NULL);
1724 if ((node->node_type == Nroot) || (node->node_type == Ndir)) {
1725 if (node->nchildren > 2) {
1726 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1727 devfs_alias_apply(node1, alias);
1731 if (node->d_dev == alias->dev_target)
1732 devfs_alias_create(alias->name, node, 0);
1738 * This function checks if any alias possibly is applicable
1739 * to the given node. If so, the alias is created.
1742 devfs_alias_check_create(struct devfs_node *node)
1744 struct devfs_alias *alias;
1746 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1747 if (node->d_dev == alias->dev_target)
1748 devfs_alias_create(alias->name, node, 0);
1754 * This function creates an alias with a given name
1755 * linking to a given devfs node. It also increments
1756 * the link count on the target node.
1759 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based)
1761 struct mount *mp = target->mp;
1762 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1763 struct devfs_node *linknode;
1764 char *create_path = NULL;
1769 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1771 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1772 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1775 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1778 if (devfs_find_device_node_by_name(parent, name)) {
1779 devfs_debug(DEVFS_DEBUG_WARNING,
1780 "Node already exists: %s "
1781 "(devfs_make_alias_worker)!\n",
1787 linknode = devfs_allocp(Nlink, name, parent, mp, NULL);
1788 if (linknode == NULL) {
1793 linknode->link_target = target;
1797 linknode->flags |= DEVFS_RULE_CREATED;
1800 kfree(name_buf, M_TEMP);
1805 * This function is called by the core and handles mount point
1806 * strings. It either calls the relevant worker (devfs_apply_
1807 * reset_rules_worker) on all mountpoints or only a specific
1811 devfs_apply_reset_rules_caller(char *mountto, int apply)
1813 struct devfs_mnt_data *mnt;
1815 if (mountto[0] == '*') {
1816 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1817 devfs_iterate_topology(mnt->root_node,
1818 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1822 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1823 if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) {
1824 devfs_iterate_topology(mnt->root_node,
1825 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1832 kfree(mountto, M_DEVFS);
1837 * This function calls a given callback function for
1838 * every dev node in the devfs dev list.
1841 devfs_scan_callback_worker(devfs_scan_t *callback, void *arg)
1844 struct devfs_alias *alias, *alias1;
1846 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1847 callback(dev->si_name, dev, false, arg);
1849 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias1) {
1850 callback(alias->name, alias->dev_target, true, arg);
1857 * This function tries to resolve a given directory, or if not
1858 * found and creation requested, creates the given directory.
1860 static struct devfs_node *
1861 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1862 size_t name_len, int create)
1864 struct devfs_node *node, *found = NULL;
1866 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1867 if (name_len != node->d_dir.d_namlen)
1870 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1876 if ((found == NULL) && (create)) {
1877 found = devfs_allocp(Ndir, dir_name, parent, parent->mp, NULL);
1884 * This function tries to resolve a complete path. If creation is requested,
1885 * if a given part of the path cannot be resolved (because it doesn't exist),
1889 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1891 struct devfs_node *node = parent;
1898 buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1900 while (*path && idx < PATH_MAX - 1) {
1905 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1915 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1916 kfree (buf, M_TEMP);
1921 * Takes a full path and strips it into a directory path and a name.
1922 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
1923 * requires a working buffer with enough size to keep the whole
1927 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
1931 size_t len = strlen(fullpath) + 1;
1934 KKASSERT((fullpath != NULL) && (buf != NULL));
1935 KKASSERT((pathp != NULL) && (namep != NULL));
1937 memcpy(buf, fullpath, len);
1939 for (i = len-1; i>= 0; i--) {
1940 if (buf[i] == '/') {
1960 * This function creates a new devfs node for a given device. It can
1961 * handle a complete path as device name, and accordingly creates
1962 * the path and the final device node.
1964 * The reference count on the passed dev remains unchanged.
1967 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
1968 int *existsp, char *dev_name, char *path_fmt, ...)
1970 struct devfs_node *parent, *node = NULL;
1976 char *create_path = NULL;
1977 char *names = "pqrsPQRS";
1979 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1984 if (path_fmt != NULL) {
1985 __va_start(ap, path_fmt);
1986 kvasnrprintf(&path, PATH_MAX, 10, path_fmt, ap);
1990 parent = devfs_resolve_or_create_path(root, path, 1);
1993 devfs_resolve_name_path(
1994 ((dev_name == NULL) && (dev))?(dev->si_name):(dev_name),
1995 name_buf, &create_path, &name);
1998 parent = devfs_resolve_or_create_path(parent, create_path, 1);
2001 node = devfs_find_device_node_by_name(parent, name);
2003 if (node->d_dev == dev) {
2005 * Allow case where device caches dev after the
2006 * close and might desire to reuse it.
2011 devfs_debug(DEVFS_DEBUG_WARNING,
2012 "devfs_create_device_node: "
2013 "DEVICE %s ALREADY EXISTS!!! "
2014 "Ignoring creation request.\n",
2021 node = devfs_allocp(Ndev, name, parent, parent->mp, dev);
2022 nanotime(&parent->mtime);
2025 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
2028 if ((dev) && (strlen(dev->si_name) >= 4) &&
2029 (!memcmp(dev->si_name, "ptm/", 4))) {
2030 node->parent->flags |= DEVFS_HIDDEN;
2031 node->flags |= DEVFS_HIDDEN;
2035 * Ugly pty magic, to tag pty devices as such and hide them if needed.
2037 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
2038 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
2040 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
2042 for (i = 0; i < strlen(names); i++) {
2043 if (name[3] == names[i]) {
2049 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
2053 kfree(name_buf, M_TEMP);
2059 * This function finds a given device node in the topology with a given
2063 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target)
2065 if ((node->node_type == Ndev) && (node->d_dev == target)) {
2073 * This function finds a device node in the given parent directory by its
2074 * name and returns it.
2077 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
2079 struct devfs_node *node, *found = NULL;
2080 size_t len = strlen(target);
2082 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
2083 if (len != node->d_dir.d_namlen)
2086 if (!memcmp(node->d_dir.d_name, target, len)) {
2096 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop)
2098 struct vnode *vp = NULL;
2099 ino_t target = *inop;
2101 if (node->d_dir.d_ino == target) {
2104 vget(vp, LK_EXCLUSIVE | LK_RETRY);
2107 devfs_allocv(&vp, node);
2116 * This function takes a cdev and removes its devfs node in the
2117 * given topology. The cdev remains intact.
2120 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
2122 KKASSERT(target != NULL);
2123 return devfs_destroy_node(root, target->si_name);
2127 * This function takes a path to a devfs node, resolves it and
2128 * removes the devfs node from the given topology.
2131 devfs_destroy_node(struct devfs_node *root, char *target)
2133 struct devfs_node *node, *parent;
2136 char *create_path = NULL;
2140 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
2141 ksnprintf(name_buf, PATH_MAX, "%s", target);
2143 devfs_resolve_name_path(target, name_buf, &create_path, &name);
2146 parent = devfs_resolve_or_create_path(root, create_path, 0);
2150 if (parent == NULL) {
2151 kfree(name_buf, M_TEMP);
2155 node = devfs_find_device_node_by_name(parent, name);
2158 nanotime(&node->parent->mtime);
2162 kfree(name_buf, M_TEMP);
2168 * Just set perms and ownership for given node.
2171 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid,
2172 u_short mode, u_long flags)
2182 * Propagates a device attach/detach to all mount
2183 * points. Also takes care of automatic alias removal
2184 * for a deleted cdev.
2187 devfs_propagate_dev(cdev_t dev, int attach)
2189 struct devfs_mnt_data *mnt;
2191 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
2193 /* Device is being attached */
2194 devfs_create_device_node(mnt->root_node, dev,
2197 /* Device is being detached */
2198 devfs_alias_remove(dev);
2199 devfs_destroy_device_node(mnt->root_node, dev);
2206 * devfs_clone either returns a basename from a complete name by
2207 * returning the length of the name without trailing digits, or,
2208 * if clone != 0, calls the device's clone handler to get a new
2209 * device, which in turn is returned in devp.
2212 devfs_clone(cdev_t dev, const char *name, size_t len, int mode,
2216 struct devfs_clone_handler *chandler;
2217 struct dev_clone_args ap;
2219 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
2220 if (chandler->namlen != len)
2222 if ((!memcmp(chandler->name, name, len)) && (chandler->nhandler)) {
2223 lockmgr(&devfs_lock, LK_RELEASE);
2225 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2227 ap.a_head.a_dev = dev;
2233 error = (chandler->nhandler)(&ap);
2246 * Registers a new orphan in the orphan list.
2249 devfs_tracer_add_orphan(struct devfs_node *node)
2251 struct devfs_orphan *orphan;
2254 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2255 orphan->node = node;
2257 KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2258 node->flags |= DEVFS_ORPHANED;
2259 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2263 * Removes an orphan from the orphan list.
2266 devfs_tracer_del_orphan(struct devfs_node *node)
2268 struct devfs_orphan *orphan;
2272 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) {
2273 if (orphan->node == node) {
2274 node->flags &= ~DEVFS_ORPHANED;
2275 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2276 kfree(orphan, M_DEVFS);
2283 * Counts the orphans in the orphan list, and if cleanup
2284 * is specified, also frees the orphan and removes it from
2288 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2290 struct devfs_orphan *orphan, *orphan2;
2293 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) {
2296 * If we are instructed to clean up, we do so.
2299 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2300 orphan->node->flags &= ~DEVFS_ORPHANED;
2301 devfs_freep(orphan->node);
2302 kfree(orphan, M_DEVFS);
2310 * Fetch an ino_t from the global d_ino by increasing it
2314 devfs_fetch_ino(void)
2318 spin_lock(&ino_lock);
2320 spin_unlock(&ino_lock);
2326 * Allocates a new cdev and initializes it's most basic
2330 devfs_new_cdev(struct dev_ops *ops, int minor, struct dev_ops *bops)
2332 cdev_t dev = sysref_alloc(&cdev_sysref_class);
2334 sysref_activate(&dev->si_sysref);
2336 bzero(dev, offsetof(struct cdev, si_sysref));
2341 dev->si_drv1 = NULL;
2342 dev->si_drv2 = NULL;
2343 dev->si_lastread = 0; /* time_uptime */
2344 dev->si_lastwrite = 0; /* time_uptime */
2346 dev->si_dict = NULL;
2347 dev->si_parent = NULL;
2350 dev->si_uminor = minor;
2351 dev->si_bops = bops;
2354 * Since the disk subsystem is in the way, we need to
2355 * propagate the D_CANFREE from bops (and ops) to
2358 if (bops && (bops->head.flags & D_CANFREE)) {
2359 dev->si_flags |= SI_CANFREE;
2360 } else if (ops->head.flags & D_CANFREE) {
2361 dev->si_flags |= SI_CANFREE;
2364 /* If there is a backing device, we reference its ops */
2365 dev->si_inode = makeudev(
2366 devfs_reference_ops((bops)?(bops):(ops)),
2368 dev->si_umajor = umajor(dev->si_inode);
2374 devfs_cdev_terminate(cdev_t dev)
2378 /* Check if it is locked already. if not, we acquire the devfs lock */
2379 if ((lockstatus(&devfs_lock, curthread)) != LK_EXCLUSIVE) {
2380 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2385 * Make sure the node isn't linked anymore. Otherwise we've screwed
2386 * up somewhere, since normal devs are unlinked on the call to
2387 * destroy_dev and only-cdevs that have not been used for cloning
2388 * are not linked in the first place. only-cdevs used for cloning
2389 * will be linked in, too, and should only be destroyed via
2390 * destroy_dev, not destroy_only_dev, so we catch that problem, too.
2392 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2394 /* If we acquired the lock, we also get rid of it */
2396 lockmgr(&devfs_lock, LK_RELEASE);
2398 /* If there is a backing device, we release the backing device's ops */
2399 devfs_release_ops((dev->si_bops)?(dev->si_bops):(dev->si_ops));
2401 /* Finally destroy the device */
2402 sysref_put(&dev->si_sysref);
2406 * Dummies for now (individual locks for MPSAFE)
2409 devfs_cdev_lock(cdev_t dev)
2414 devfs_cdev_unlock(cdev_t dev)
2419 devfs_detached_filter_eof(struct knote *kn, long hint)
2421 kn->kn_flags |= (EV_EOF | EV_NODATA);
2426 devfs_detached_filter_detach(struct knote *kn)
2428 cdev_t dev = (cdev_t)kn->kn_hook;
2430 knote_remove(&dev->si_kqinfo.ki_note, kn);
2433 static struct filterops devfs_detached_filterops =
2434 { FILTEROP_ISFD, NULL,
2435 devfs_detached_filter_detach,
2436 devfs_detached_filter_eof };
2439 * Delegates knote filter handling responsibility to devfs
2441 * Any device that implements kqfilter event handling and could be detached
2442 * or shut down out from under the kevent subsystem must allow devfs to
2443 * assume responsibility for any knotes it may hold.
2446 devfs_assume_knotes(cdev_t dev, struct kqinfo *kqi)
2449 * Let kern/kern_event.c do the heavy lifting.
2451 knote_assume_knotes(kqi, &dev->si_kqinfo,
2452 &devfs_detached_filterops, (void *)dev);
2455 * These should probably be activated individually, but doing so
2456 * would require refactoring kq's public in-kernel interface.
2458 KNOTE(&dev->si_kqinfo.ki_note, 0);
2462 * Links a given cdev into the dev list.
2465 devfs_link_dev(cdev_t dev)
2467 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2468 dev->si_flags |= SI_DEVFS_LINKED;
2469 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2475 * Removes a given cdev from the dev list. The caller is responsible for
2476 * releasing the reference on the device associated with the linkage.
2478 * Returns EALREADY if the dev has already been unlinked.
2481 devfs_unlink_dev(cdev_t dev)
2483 if ((dev->si_flags & SI_DEVFS_LINKED)) {
2484 TAILQ_REMOVE(&devfs_dev_list, dev, link);
2485 dev->si_flags &= ~SI_DEVFS_LINKED;
2492 devfs_node_is_accessible(struct devfs_node *node)
2494 if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2501 devfs_reference_ops(struct dev_ops *ops)
2504 struct devfs_dev_ops *found = NULL;
2505 struct devfs_dev_ops *devops;
2507 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2508 if (devops->ops == ops) {
2515 found = kmalloc(sizeof(struct devfs_dev_ops), M_DEVFS, M_WAITOK);
2517 found->ref_count = 0;
2518 TAILQ_INSERT_TAIL(&devfs_dev_ops_list, found, link);
2523 if (found->ref_count == 0) {
2524 found->id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255);
2525 if (found->id == -1) {
2526 /* Ran out of unique ids */
2527 devfs_debug(DEVFS_DEBUG_WARNING,
2528 "devfs_reference_ops: WARNING: ran out of unique ids\n");
2538 devfs_release_ops(struct dev_ops *ops)
2540 struct devfs_dev_ops *found = NULL;
2541 struct devfs_dev_ops *devops;
2543 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2544 if (devops->ops == ops) {
2554 if (found->ref_count == 0) {
2555 TAILQ_REMOVE(&devfs_dev_ops_list, found, link);
2556 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), found->id);
2557 kfree(found, M_DEVFS);
2562 * Wait for asynchronous messages to complete in the devfs helper
2563 * thread, then return. Do nothing if the helper thread is dead
2564 * or we are being indirectly called from the helper thread itself.
2571 if (devfs_run && curthread != td_core) {
2572 msg = devfs_msg_get();
2573 msg = devfs_msg_send_sync(DEVFS_SYNC, msg);
2579 * Called on init of devfs; creates the objcaches and
2580 * spawns off the devfs core thread. Also initializes
2586 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2587 /* Create objcaches for nodes, msgs and devs */
2588 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2590 objcache_malloc_alloc,
2591 objcache_malloc_free,
2592 &devfs_node_malloc_args );
2594 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2596 objcache_malloc_alloc,
2597 objcache_malloc_free,
2598 &devfs_msg_malloc_args );
2600 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2602 objcache_malloc_alloc,
2603 objcache_malloc_free,
2604 &devfs_dev_malloc_args );
2606 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2608 /* Initialize the reply-only port which acts as a message drain */
2609 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2611 /* Initialize *THE* devfs lock */
2612 lockinit(&devfs_lock, "devfs_core lock", 0, 0);
2614 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2615 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2616 0, -1, "devfs_msg_core");
2617 while (devfs_run == 0)
2618 lksleep(td_core, &devfs_lock, 0, "devfsc", 0);
2619 lockmgr(&devfs_lock, LK_RELEASE);
2621 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2625 * Called on unload of devfs; takes care of destroying the core
2626 * and the objcaches. Also removes aliases that are no longer needed.
2631 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2633 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2635 tsleep(td_core, 0, "devfsc", hz*10);
2636 tsleep(td_core, 0, "devfsc", hz);
2638 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2640 /* Destroy the objcaches */
2641 objcache_destroy(devfs_msg_cache);
2642 objcache_destroy(devfs_node_cache);
2643 objcache_destroy(devfs_dev_cache);
2649 * This is a sysctl handler to assist userland devname(3) to
2650 * find the device name for a given udev.
2653 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2660 if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t))))
2663 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev);
2668 if ((found = devfs_find_device_by_udev(udev)) == NULL)
2671 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2675 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
2676 NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)");
2678 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2679 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2680 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable,
2681 0, "Enable DevFS debugging");
2683 SYSINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST,
2685 SYSUNINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY,
2686 devfs_uninit, NULL);
2689 * WildCmp() - compare wild string to sane string
2691 * Returns 0 on success, -1 on failure.
2694 wildCmp(const char **mary, int d, const char *w, const char *s)
2699 * skip fixed portion
2705 * optimize terminator
2709 if (w[1] != '?' && w[1] != '*') {
2711 * optimize * followed by non-wild
2713 for (i = 0; s + i < mary[d]; ++i) {
2714 if (s[i] == w[1] && wildCmp(mary, d + 1, w + 1, s + i) == 0)
2721 for (i = 0; s + i < mary[d]; ++i) {
2722 if (wildCmp(mary, d + 1, w + 1, s + i) == 0)
2737 if (*w == 0) /* terminator */
2750 * WildCaseCmp() - compare wild string to sane string, case insensitive
2752 * Returns 0 on success, -1 on failure.
2755 wildCaseCmp(const char **mary, int d, const char *w, const char *s)
2760 * skip fixed portion
2766 * optimize terminator
2770 if (w[1] != '?' && w[1] != '*') {
2772 * optimize * followed by non-wild
2774 for (i = 0; s + i < mary[d]; ++i) {
2775 if (s[i] == w[1] && wildCaseCmp(mary, d + 1, w + 1, s + i) == 0)
2782 for (i = 0; s + i < mary[d]; ++i) {
2783 if (wildCaseCmp(mary, d + 1, w + 1, s + i) == 0)
2797 #define tolower(x) ((x >= 'A' && x <= 'Z')?(x+('a'-'A')):(x))
2798 if (tolower(*w) != tolower(*s))
2801 if (*w == 0) /* terminator */
2812 struct cdev_privdata {
2814 cdevpriv_dtr_t cdpd_dtr;
2817 int devfs_get_cdevpriv(struct file *fp, void **datap)
2819 struct cdev_privdata *p;
2824 if ((fp->f_flag & FCDEVPRIV) == 0) {
2828 p = (struct cdev_privdata *)fp->f_data1;
2831 *datap = p->cdpd_data;
2840 int devfs_set_cdevpriv(struct file *fp, void *priv, cdevpriv_dtr_t dtr)
2842 struct cdev_privdata *p;
2848 p = kmalloc(sizeof(struct cdev_privdata), M_DEVFS, M_WAITOK);
2849 p->cdpd_data = priv;
2852 spin_lock(&fp->f_spin);
2853 if (fp->f_data1 == NULL) {
2854 atomic_set_int(&fp->f_flag, FCDEVPRIV);
2860 spin_unlock(&fp->f_spin);
2868 void devfs_clear_cdevpriv(struct file *fp)
2870 struct cdev_privdata *p;
2875 spin_lock(&fp->f_spin);
2878 if (fp->f_flag & FCDEVPRIV) {
2879 atomic_clear_int(&fp->f_flag, FCDEVPRIV);
2880 spin_unlock(&fp->f_spin);
2882 (p->cdpd_dtr)(p->cdpd_data);
2886 spin_unlock(&fp->f_spin);
2891 devfs_WildCmp(const char *w, const char *s)
2895 int slen = strlen(s);
2898 for (i = c = 0; w[i]; ++i) {
2902 mary = kmalloc(sizeof(char *) * (c + 1), M_DEVFS, M_WAITOK);
2903 for (i = 0; i < c; ++i)
2905 i = wildCmp(mary, 0, w, s);
2906 kfree(mary, M_DEVFS);
2911 devfs_WildCaseCmp(const char *w, const char *s)
2915 int slen = strlen(s);
2918 for (i = c = 0; w[i]; ++i) {
2922 mary = kmalloc(sizeof(char *) * (c + 1), M_DEVFS, M_WAITOK);
2923 for (i = 0; i < c; ++i)
2925 i = wildCaseCmp(mary, 0, w, s);
2926 kfree(mary, M_DEVFS);