2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/mount.h>
38 #include <sys/vnode.h>
39 #include <sys/types.h>
41 #include <sys/msgport.h>
42 #include <sys/msgport2.h>
43 #include <sys/spinlock2.h>
44 #include <sys/sysctl.h>
45 #include <sys/ucred.h>
46 #include <sys/param.h>
47 #include <sys/sysref2.h>
48 #include <sys/systm.h>
49 #include <sys/devfs.h>
50 #include <sys/devfs_rules.h>
52 MALLOC_DEFINE(M_DEVFS, "devfs", "Device File System (devfs) allocations");
53 DEVFS_DECLARE_CLONE_BITMAP(ops_id);
55 * SYSREF Integration - reference counting, allocation,
56 * sysid and syslink integration.
58 static void devfs_cdev_terminate(cdev_t dev);
59 static struct sysref_class cdev_sysref_class = {
62 .proto = SYSREF_PROTO_DEV,
63 .offset = offsetof(struct cdev, si_sysref),
64 .objsize = sizeof(struct cdev),
68 .terminate = (sysref_terminate_func_t)devfs_cdev_terminate
72 static struct objcache *devfs_node_cache;
73 static struct objcache *devfs_msg_cache;
74 static struct objcache *devfs_dev_cache;
76 static struct objcache_malloc_args devfs_node_malloc_args = {
77 sizeof(struct devfs_node), M_DEVFS };
78 struct objcache_malloc_args devfs_msg_malloc_args = {
79 sizeof(struct devfs_msg), M_DEVFS };
80 struct objcache_malloc_args devfs_dev_malloc_args = {
81 sizeof(struct cdev), M_DEVFS };
83 static struct devfs_dev_head devfs_dev_list =
84 TAILQ_HEAD_INITIALIZER(devfs_dev_list);
85 static struct devfs_mnt_head devfs_mnt_list =
86 TAILQ_HEAD_INITIALIZER(devfs_mnt_list);
87 static struct devfs_chandler_head devfs_chandler_list =
88 TAILQ_HEAD_INITIALIZER(devfs_chandler_list);
89 static struct devfs_alias_head devfs_alias_list =
90 TAILQ_HEAD_INITIALIZER(devfs_alias_list);
91 static struct devfs_dev_ops_head devfs_dev_ops_list =
92 TAILQ_HEAD_INITIALIZER(devfs_dev_ops_list);
94 struct lock devfs_lock;
95 static struct lwkt_port devfs_dispose_port;
96 static struct lwkt_port devfs_msg_port;
97 static struct thread *td_core;
99 static struct spinlock ino_lock;
101 static int devfs_debug_enable;
102 static int devfs_run;
104 static ino_t devfs_fetch_ino(void);
105 static int devfs_create_all_dev_worker(struct devfs_node *);
106 static int devfs_create_dev_worker(cdev_t, uid_t, gid_t, int);
107 static int devfs_destroy_dev_worker(cdev_t);
108 static int devfs_destroy_subnames_worker(char *);
109 static int devfs_destroy_dev_by_ops_worker(struct dev_ops *, int);
110 static int devfs_propagate_dev(cdev_t, int);
111 static int devfs_unlink_dev(cdev_t dev);
112 static void devfs_msg_exec(devfs_msg_t msg);
114 static int devfs_chandler_add_worker(const char *, d_clone_t *);
115 static int devfs_chandler_del_worker(const char *);
117 static void devfs_msg_autofree_reply(lwkt_port_t, lwkt_msg_t);
118 static void devfs_msg_core(void *);
120 static int devfs_find_device_by_name_worker(devfs_msg_t);
121 static int devfs_find_device_by_udev_worker(devfs_msg_t);
123 static int devfs_apply_reset_rules_caller(char *, int);
125 static int devfs_scan_callback_worker(devfs_scan_t *);
127 static struct devfs_node *devfs_resolve_or_create_dir(struct devfs_node *,
128 char *, size_t, int);
130 static int devfs_make_alias_worker(struct devfs_alias *);
131 static int devfs_alias_remove(cdev_t);
132 static int devfs_alias_reap(void);
133 static int devfs_alias_propagate(struct devfs_alias *);
134 static int devfs_alias_apply(struct devfs_node *, struct devfs_alias *);
135 static int devfs_alias_check_create(struct devfs_node *);
137 static int devfs_clr_subnames_flag_worker(char *, uint32_t);
138 static int devfs_destroy_subnames_without_flag_worker(char *, uint32_t);
140 static void *devfs_reaperp_callback(struct devfs_node *, void *);
141 static void *devfs_gc_dirs_callback(struct devfs_node *, void *);
142 static void *devfs_gc_links_callback(struct devfs_node *, struct devfs_node *);
144 devfs_inode_to_vnode_worker_callback(struct devfs_node *, ino_t *);
147 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function
151 devfs_debug(int level, char *fmt, ...)
156 if (level <= devfs_debug_enable)
164 * devfs_allocp() Allocates a new devfs node with the specified
165 * parameters. The node is also automatically linked into the topology
166 * if a parent is specified. It also calls the rule and alias stuff to
167 * be applied on the new node
170 devfs_allocp(devfs_nodetype devfsnodetype, char *name,
171 struct devfs_node *parent, struct mount *mp, cdev_t dev)
173 struct devfs_node *node = NULL;
174 size_t namlen = strlen(name);
176 node = objcache_get(devfs_node_cache, M_WAITOK);
177 bzero(node, sizeof(*node));
179 atomic_add_long(&(DEVFS_MNTDATA(mp)->leak_count), 1);
184 node->d_dir.d_ino = devfs_fetch_ino();
187 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries
190 node->cookie_jar = 2;
193 * Access Control members
195 node->mode = DEVFS_DEFAULT_MODE;
196 node->uid = DEVFS_DEFAULT_UID;
197 node->gid = DEVFS_DEFAULT_GID;
199 switch (devfsnodetype) {
202 * Ensure that we don't recycle the root vnode by marking it as
203 * linked into the topology.
205 node->flags |= DEVFS_NODE_LINKED;
207 TAILQ_INIT(DEVFS_DENODE_HEAD(node));
208 node->d_dir.d_type = DT_DIR;
213 node->d_dir.d_type = DT_LNK;
217 node->d_dir.d_type = DT_REG;
222 node->d_dir.d_type = DT_CHR;
225 node->mode = dev->si_perms;
226 node->uid = dev->si_uid;
227 node->gid = dev->si_gid;
229 devfs_alias_check_create(node);
234 panic("devfs_allocp: unknown node type");
238 node->node_type = devfsnodetype;
240 /* Initialize the dirent structure of each devfs vnode */
241 KKASSERT(namlen < 256);
242 node->d_dir.d_namlen = namlen;
243 node->d_dir.d_name = kmalloc(namlen+1, M_DEVFS, M_WAITOK);
244 memcpy(node->d_dir.d_name, name, namlen);
245 node->d_dir.d_name[namlen] = '\0';
247 /* Initialize the parent node element */
248 node->parent = parent;
251 devfs_rule_check_apply(node, NULL);
253 /* Initialize *time members */
254 nanotime(&node->atime);
255 node->mtime = node->ctime = node->atime;
258 * Associate with parent as last step, clean out namecache
261 if ((parent != NULL) &&
262 ((parent->node_type == Proot) || (parent->node_type == Pdir))) {
264 node->cookie = parent->cookie_jar++;
265 node->flags |= DEVFS_NODE_LINKED;
266 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent), node, link);
268 /* This forces negative namecache lookups to clear */
269 ++mp->mnt_namecache_gen;
276 * devfs_allocv() allocates a new vnode based on a devfs node.
279 devfs_allocv(struct vnode **vpp, struct devfs_node *node)
287 while ((vp = node->v_node) != NULL) {
288 error = vget(vp, LK_EXCLUSIVE);
289 if (error != ENOENT) {
295 if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0)
300 if (node->v_node != NULL) {
309 switch (node->node_type) {
326 KKASSERT(node->d_dev);
328 vp->v_uminor = node->d_dev->si_uminor;
331 v_associate_rdev(vp, node->d_dev);
332 vp->v_ops = &node->mp->mnt_vn_spec_ops;
336 panic("devfs_allocv: unknown node type");
344 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
345 * based on the newly created devfs node.
348 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype,
349 char *name, struct devfs_node *parent, cdev_t dev)
351 struct devfs_node *node;
353 node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
356 devfs_allocv(vpp, node);
364 * Destroy the devfs_node. The node must be unlinked from the topology.
366 * This function will also destroy any vnode association with the node
369 * The cdev_t itself remains intact.
372 devfs_freep(struct devfs_node *node)
377 KKASSERT(((node->flags & DEVFS_NODE_LINKED) == 0) ||
378 (node->node_type == Proot));
379 KKASSERT((node->flags & DEVFS_DESTROYED) == 0);
381 atomic_subtract_long(&(DEVFS_MNTDATA(node->mp)->leak_count), 1);
382 if (node->symlink_name) {
383 kfree(node->symlink_name, M_DEVFS);
384 node->symlink_name = NULL;
388 * Remove the node from the orphan list if it is still on it.
390 if (node->flags & DEVFS_ORPHANED)
391 devfs_tracer_del_orphan(node);
394 * Disassociate the vnode from the node. This also prevents the
395 * vnode's reclaim code from double-freeing the node.
397 * The vget is needed to safely modify the vp. It also serves
398 * to cycle the refs and terminate the vnode if it happens to
399 * be inactive, otherwise namecache references may not get cleared.
401 while ((vp = node->v_node) != NULL) {
402 if (vget(vp, LK_EXCLUSIVE | LK_RETRY) != 0)
407 cache_inval_vp(vp, CINV_DESTROY);
410 if (node->d_dir.d_name) {
411 kfree(node->d_dir.d_name, M_DEVFS);
412 node->d_dir.d_name = NULL;
414 node->flags |= DEVFS_DESTROYED;
416 objcache_put(devfs_node_cache, node);
422 * Unlink the devfs node from the topology and add it to the orphan list.
423 * The node will later be destroyed by freep.
425 * Any vnode association, including the v_rdev and v_data, remains intact
429 devfs_unlinkp(struct devfs_node *node)
431 struct devfs_node *parent;
435 * Add the node to the orphan list, so it is referenced somewhere, to
436 * so we don't leak it.
438 devfs_tracer_add_orphan(node);
440 parent = node->parent;
443 * If the parent is known we can unlink the node out of the topology
446 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
448 KKASSERT((parent->nchildren >= 0));
449 node->flags &= ~DEVFS_NODE_LINKED;
456 devfs_iterate_topology(struct devfs_node *node,
457 devfs_iterate_callback_t *callback, void *arg1)
459 struct devfs_node *node1, *node2;
462 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
463 if (node->nchildren > 2) {
464 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
466 if ((ret = devfs_iterate_topology(node1, callback, arg1)))
472 ret = callback(node, arg1);
477 * devfs_reaperp() is a recursive function that iterates through all the
478 * topology, unlinking and freeing all devfs nodes.
481 devfs_reaperp_callback(struct devfs_node *node, void *unused)
490 devfs_gc_dirs_callback(struct devfs_node *node, void *unused)
492 if (node->node_type == Pdir) {
493 if (node->nchildren == 2) {
503 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target)
505 if ((node->node_type == Plink) && (node->link_target == target)) {
514 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
515 * freeing a node, but also removes empty directories and links that link
516 * via devfs auto-link mechanism to the node being deleted.
519 devfs_gc(struct devfs_node *node)
521 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
523 if (node->nlinks > 0)
524 devfs_iterate_topology(root_node,
525 (devfs_iterate_callback_t *)devfs_gc_links_callback, node);
528 devfs_iterate_topology(root_node,
529 (devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL);
537 * devfs_create_dev() is the asynchronous entry point for device creation.
538 * It just sends a message with the relevant details to the devfs core.
540 * This function will reference the passed device. The reference is owned
541 * by devfs and represents all of the device's node associations.
544 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
547 devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
553 * devfs_destroy_dev() is the asynchronous entry point for device destruction.
554 * It just sends a message with the relevant details to the devfs core.
557 devfs_destroy_dev(cdev_t dev)
559 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
564 * devfs_mount_add() is the synchronous entry point for adding a new devfs
565 * mount. It sends a synchronous message with the relevant details to the
569 devfs_mount_add(struct devfs_mnt_data *mnt)
573 msg = devfs_msg_get();
575 msg = devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
582 * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
583 * It sends a synchronous message with the relevant details to the devfs core.
586 devfs_mount_del(struct devfs_mnt_data *mnt)
590 msg = devfs_msg_get();
592 msg = devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
599 * devfs_destroy_subnames() is the synchronous entry point for device
600 * destruction by subname. It just sends a message with the relevant details to
604 devfs_destroy_subnames(char *name)
608 msg = devfs_msg_get();
609 msg->mdv_load = name;
610 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES, msg);
616 devfs_clr_subnames_flag(char *name, uint32_t flag)
620 msg = devfs_msg_get();
621 msg->mdv_flags.name = name;
622 msg->mdv_flags.flag = flag;
623 msg = devfs_msg_send_sync(DEVFS_CLR_SUBNAMES_FLAG, msg);
630 devfs_destroy_subnames_without_flag(char *name, uint32_t flag)
634 msg = devfs_msg_get();
635 msg->mdv_flags.name = name;
636 msg->mdv_flags.flag = flag;
637 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES_WO_FLAG, msg);
644 * devfs_create_all_dev is the asynchronous entry point to trigger device
645 * node creation. It just sends a message with the relevant details to
649 devfs_create_all_dev(struct devfs_node *root)
651 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
656 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
657 * devices with a specific set of dev_ops and minor. It just sends a
658 * message with the relevant details to the devfs core.
661 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
663 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
668 * devfs_clone_handler_add is the synchronous entry point to add a new
669 * clone handler. It just sends a message with the relevant details to
673 devfs_clone_handler_add(const char *name, d_clone_t *nhandler)
677 msg = devfs_msg_get();
678 msg->mdv_chandler.name = name;
679 msg->mdv_chandler.nhandler = nhandler;
680 msg = devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg);
686 * devfs_clone_handler_del is the synchronous entry point to remove a
687 * clone handler. It just sends a message with the relevant details to
691 devfs_clone_handler_del(const char *name)
695 msg = devfs_msg_get();
696 msg->mdv_chandler.name = name;
697 msg->mdv_chandler.nhandler = NULL;
698 msg = devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg);
704 * devfs_find_device_by_name is the synchronous entry point to find a
705 * device given its name. It sends a synchronous message with the
706 * relevant details to the devfs core and returns the answer.
709 devfs_find_device_by_name(const char *fmt, ...)
720 kvasnrprintf(&target, PATH_MAX, 10, fmt, ap);
723 msg = devfs_msg_get();
724 msg->mdv_name = target;
725 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
726 found = msg->mdv_cdev;
734 * devfs_find_device_by_udev is the synchronous entry point to find a
735 * device given its udev number. It sends a synchronous message with
736 * the relevant details to the devfs core and returns the answer.
739 devfs_find_device_by_udev(udev_t udev)
744 msg = devfs_msg_get();
745 msg->mdv_udev = udev;
746 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg);
747 found = msg->mdv_cdev;
750 devfs_debug(DEVFS_DEBUG_DEBUG,
751 "devfs_find_device_by_udev found? %s -end:3-\n",
752 ((found) ? found->si_name:"NO"));
757 devfs_inode_to_vnode(struct mount *mp, ino_t target)
759 struct vnode *vp = NULL;
765 msg = devfs_msg_get();
766 msg->mdv_ino.mp = mp;
767 msg->mdv_ino.ino = target;
768 msg = devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg);
769 vp = msg->mdv_ino.vp;
770 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
777 * devfs_make_alias is the asynchronous entry point to register an alias
778 * for a device. It just sends a message with the relevant details to the
782 devfs_make_alias(const char *name, cdev_t dev_target)
784 struct devfs_alias *alias;
789 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
790 alias->name = kstrdup(name, M_DEVFS);
792 alias->dev_target = dev_target;
794 devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
799 * devfs_apply_rules is the asynchronous entry point to trigger application
800 * of all rules. It just sends a message with the relevant details to the
804 devfs_apply_rules(char *mntto)
808 new_name = kstrdup(mntto, M_DEVFS);
809 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
815 * devfs_reset_rules is the asynchronous entry point to trigger reset of all
816 * rules. It just sends a message with the relevant details to the devfs core.
819 devfs_reset_rules(char *mntto)
823 new_name = kstrdup(mntto, M_DEVFS);
824 devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
831 * devfs_scan_callback is the asynchronous entry point to call a callback
833 * It just sends a message with the relevant details to the devfs core.
836 devfs_scan_callback(devfs_scan_t *callback)
840 KKASSERT(sizeof(callback) == sizeof(void *));
842 msg = devfs_msg_get();
843 msg->mdv_load = callback;
844 msg = devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
852 * Acts as a message drain. Any message that is replied to here gets destroyed
853 * and the memory freed.
856 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
858 devfs_msg_put((devfs_msg_t)msg);
862 * devfs_msg_get allocates a new devfs msg and returns it.
867 return objcache_get(devfs_msg_cache, M_WAITOK);
871 * devfs_msg_put deallocates a given devfs msg.
874 devfs_msg_put(devfs_msg_t msg)
876 objcache_put(devfs_msg_cache, msg);
881 * devfs_msg_send is the generic asynchronous message sending facility
882 * for devfs. By default the reply port is the automatic disposal port.
884 * If the current thread is the devfs_msg_port thread we execute the
885 * operation synchronously.
888 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
890 lwkt_port_t port = &devfs_msg_port;
892 lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
894 devfs_msg->hdr.u.ms_result = cmd;
896 if (port->mpu_td == curthread) {
897 devfs_msg_exec(devfs_msg);
898 lwkt_replymsg(&devfs_msg->hdr, 0);
900 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
905 * devfs_msg_send_sync is the generic synchronous message sending
906 * facility for devfs. It initializes a local reply port and waits
907 * for the core's answer. This answer is then returned.
910 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
912 struct lwkt_port rep_port;
913 devfs_msg_t msg_incoming;
914 lwkt_port_t port = &devfs_msg_port;
916 lwkt_initport_thread(&rep_port, curthread);
917 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
919 devfs_msg->hdr.u.ms_result = cmd;
921 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
922 msg_incoming = lwkt_waitport(&rep_port, 0);
928 * sends a message with a generic argument.
931 devfs_msg_send_generic(uint32_t cmd, void *load)
933 devfs_msg_t devfs_msg = devfs_msg_get();
935 devfs_msg->mdv_load = load;
936 devfs_msg_send(cmd, devfs_msg);
940 * sends a message with a name argument.
943 devfs_msg_send_name(uint32_t cmd, char *name)
945 devfs_msg_t devfs_msg = devfs_msg_get();
947 devfs_msg->mdv_name = name;
948 devfs_msg_send(cmd, devfs_msg);
952 * sends a message with a mount argument.
955 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
957 devfs_msg_t devfs_msg = devfs_msg_get();
959 devfs_msg->mdv_mnt = mnt;
960 devfs_msg_send(cmd, devfs_msg);
964 * sends a message with an ops argument.
967 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
969 devfs_msg_t devfs_msg = devfs_msg_get();
971 devfs_msg->mdv_ops.ops = ops;
972 devfs_msg->mdv_ops.minor = minor;
973 devfs_msg_send(cmd, devfs_msg);
977 * sends a message with a clone handler argument.
980 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
982 devfs_msg_t devfs_msg = devfs_msg_get();
984 devfs_msg->mdv_chandler.name = name;
985 devfs_msg->mdv_chandler.nhandler = handler;
986 devfs_msg_send(cmd, devfs_msg);
990 * sends a message with a device argument.
993 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
995 devfs_msg_t devfs_msg = devfs_msg_get();
997 devfs_msg->mdv_dev.dev = dev;
998 devfs_msg->mdv_dev.uid = uid;
999 devfs_msg->mdv_dev.gid = gid;
1000 devfs_msg->mdv_dev.perms = perms;
1002 devfs_msg_send(cmd, devfs_msg);
1006 * sends a message with a link argument.
1009 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1011 devfs_msg_t devfs_msg = devfs_msg_get();
1013 devfs_msg->mdv_link.name = name;
1014 devfs_msg->mdv_link.target = target;
1015 devfs_msg->mdv_link.mp = mp;
1016 devfs_msg_send(cmd, devfs_msg);
1020 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1021 * and calls the relevant worker functions. By using messages it's assured
1022 * that events occur in the correct order.
1025 devfs_msg_core(void *arg)
1030 lwkt_initport_thread(&devfs_msg_port, curthread);
1034 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1035 devfs_debug(DEVFS_DEBUG_DEBUG,
1036 "devfs_msg_core, new msg: %x\n",
1037 (unsigned int)msg->hdr.u.ms_result);
1038 devfs_msg_exec(msg);
1039 lwkt_replymsg(&msg->hdr, 0);
1046 devfs_msg_exec(devfs_msg_t msg)
1048 struct devfs_mnt_data *mnt;
1049 struct devfs_node *node;
1053 * Acquire the devfs lock to ensure safety of all called functions
1055 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1057 switch (msg->hdr.u.ms_result) {
1058 case DEVFS_DEVICE_CREATE:
1059 dev = msg->mdv_dev.dev;
1060 devfs_create_dev_worker(dev,
1063 msg->mdv_dev.perms);
1065 case DEVFS_DEVICE_DESTROY:
1066 dev = msg->mdv_dev.dev;
1067 devfs_destroy_dev_worker(dev);
1069 case DEVFS_DESTROY_SUBNAMES:
1070 devfs_destroy_subnames_worker(msg->mdv_load);
1072 case DEVFS_DESTROY_DEV_BY_OPS:
1073 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1074 msg->mdv_ops.minor);
1076 case DEVFS_CREATE_ALL_DEV:
1077 node = (struct devfs_node *)msg->mdv_load;
1078 devfs_create_all_dev_worker(node);
1080 case DEVFS_MOUNT_ADD:
1082 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1083 devfs_create_all_dev_worker(mnt->root_node);
1085 case DEVFS_MOUNT_DEL:
1087 TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1088 devfs_iterate_topology(mnt->root_node, devfs_reaperp_callback,
1090 if (mnt->leak_count) {
1091 devfs_debug(DEVFS_DEBUG_SHOW,
1092 "Leaked %ld devfs_node elements!\n",
1096 case DEVFS_CHANDLER_ADD:
1097 devfs_chandler_add_worker(msg->mdv_chandler.name,
1098 msg->mdv_chandler.nhandler);
1100 case DEVFS_CHANDLER_DEL:
1101 devfs_chandler_del_worker(msg->mdv_chandler.name);
1103 case DEVFS_FIND_DEVICE_BY_NAME:
1104 devfs_find_device_by_name_worker(msg);
1106 case DEVFS_FIND_DEVICE_BY_UDEV:
1107 devfs_find_device_by_udev_worker(msg);
1109 case DEVFS_MAKE_ALIAS:
1110 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1112 case DEVFS_APPLY_RULES:
1113 devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1115 case DEVFS_RESET_RULES:
1116 devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1118 case DEVFS_SCAN_CALLBACK:
1119 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load);
1121 case DEVFS_CLR_SUBNAMES_FLAG:
1122 devfs_clr_subnames_flag_worker(msg->mdv_flags.name,
1123 msg->mdv_flags.flag);
1125 case DEVFS_DESTROY_SUBNAMES_WO_FLAG:
1126 devfs_destroy_subnames_without_flag_worker(msg->mdv_flags.name,
1127 msg->mdv_flags.flag);
1129 case DEVFS_INODE_TO_VNODE:
1130 msg->mdv_ino.vp = devfs_iterate_topology(
1131 DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node,
1132 (devfs_iterate_callback_t *)devfs_inode_to_vnode_worker_callback,
1135 case DEVFS_TERMINATE_CORE:
1141 devfs_debug(DEVFS_DEBUG_WARNING,
1142 "devfs_msg_core: unknown message "
1143 "received at core\n");
1146 lockmgr(&devfs_lock, LK_RELEASE);
1150 * Worker function to insert a new dev into the dev list and initialize its
1151 * permissions. It also calls devfs_propagate_dev which in turn propagates
1152 * the change to all mount points.
1154 * The passed dev is already referenced. This reference is eaten by this
1155 * function and represents the dev's linkage into devfs_dev_list.
1158 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1164 dev->si_perms = perms;
1166 devfs_link_dev(dev);
1167 devfs_propagate_dev(dev, 1);
1173 * Worker function to delete a dev from the dev list and free the cdev.
1174 * It also calls devfs_propagate_dev which in turn propagates the change
1175 * to all mount points.
1178 devfs_destroy_dev_worker(cdev_t dev)
1183 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1185 error = devfs_unlink_dev(dev);
1186 devfs_propagate_dev(dev, 0);
1188 release_dev(dev); /* link ref */
1196 * Worker function to destroy all devices with a certain basename.
1197 * Calls devfs_destroy_dev_worker for the actual destruction.
1200 devfs_destroy_subnames_worker(char *name)
1203 size_t len = strlen(name);
1205 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1206 if ((!strncmp(dev->si_name, name, len)) &&
1207 (dev->si_name[len] != '\0')) {
1208 devfs_destroy_dev_worker(dev);
1215 devfs_clr_subnames_flag_worker(char *name, uint32_t flag)
1218 size_t len = strlen(name);
1220 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1221 if ((!strncmp(dev->si_name, name, len)) &&
1222 (dev->si_name[len] != '\0')) {
1223 dev->si_flags &= ~flag;
1231 devfs_destroy_subnames_without_flag_worker(char *name, uint32_t flag)
1234 size_t len = strlen(name);
1236 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1237 if ((!strncmp(dev->si_name, name, len)) &&
1238 (dev->si_name[len] != '\0')) {
1239 if (!(dev->si_flags & flag)) {
1240 devfs_destroy_dev_worker(dev);
1249 * Worker function that creates all device nodes on top of a devfs
1253 devfs_create_all_dev_worker(struct devfs_node *root)
1259 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1260 devfs_create_device_node(root, dev, NULL, NULL);
1267 * Worker function that destroys all devices that match a specific
1268 * dev_ops and/or minor. If minor is less than 0, it is not matched
1269 * against. It also propagates all changes.
1272 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1278 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1279 if (dev->si_ops != ops)
1281 if ((minor < 0) || (dev->si_uminor == minor)) {
1282 devfs_destroy_dev_worker(dev);
1290 * Worker function that registers a new clone handler in devfs.
1293 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler)
1295 struct devfs_clone_handler *chandler = NULL;
1296 u_char len = strlen(name);
1301 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1302 if (chandler->namlen != len)
1305 if (!memcmp(chandler->name, name, len)) {
1306 /* Clonable basename already exists */
1311 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1312 chandler->name = kstrdup(name, M_DEVFS);
1313 chandler->namlen = len;
1314 chandler->nhandler = nhandler;
1316 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1321 * Worker function that removes a given clone handler from the
1322 * clone handler list.
1325 devfs_chandler_del_worker(const char *name)
1327 struct devfs_clone_handler *chandler, *chandler2;
1328 u_char len = strlen(name);
1333 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1334 if (chandler->namlen != len)
1336 if (memcmp(chandler->name, name, len))
1339 TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1340 kfree(chandler->name, M_DEVFS);
1341 kfree(chandler, M_DEVFS);
1349 * Worker function that finds a given device name and changes
1350 * the message received accordingly so that when replied to,
1351 * the answer is returned to the caller.
1354 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1356 struct devfs_alias *alias;
1358 cdev_t found = NULL;
1360 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1361 if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1366 if (found == NULL) {
1367 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1368 if (strcmp(devfs_msg->mdv_name, alias->name) == 0) {
1369 found = alias->dev_target;
1374 devfs_msg->mdv_cdev = found;
1380 * Worker function that finds a given device udev and changes
1381 * the message received accordingly so that when replied to,
1382 * the answer is returned to the caller.
1385 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg)
1388 cdev_t found = NULL;
1390 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1391 if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1396 devfs_msg->mdv_cdev = found;
1402 * Worker function that inserts a given alias into the
1403 * alias list, and propagates the alias to all mount
1407 devfs_make_alias_worker(struct devfs_alias *alias)
1409 struct devfs_alias *alias2;
1410 size_t len = strlen(alias->name);
1413 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1414 if (len != alias2->namlen)
1417 if (!memcmp(alias->name, alias2->name, len)) {
1425 * The alias doesn't exist yet, so we add it to the alias list
1427 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1428 devfs_alias_propagate(alias);
1430 devfs_debug(DEVFS_DEBUG_WARNING,
1431 "Warning: duplicate devfs_make_alias for %s\n",
1433 kfree(alias->name, M_DEVFS);
1434 kfree(alias, M_DEVFS);
1441 * Function that removes and frees all aliases.
1444 devfs_alias_reap(void)
1446 struct devfs_alias *alias, *alias2;
1448 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1449 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1450 kfree(alias, M_DEVFS);
1456 * Function that removes an alias matching a specific cdev and frees
1460 devfs_alias_remove(cdev_t dev)
1462 struct devfs_alias *alias, *alias2;
1464 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1465 if (alias->dev_target == dev) {
1466 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1467 kfree(alias, M_DEVFS);
1474 * This function propagates a new alias to all mount points.
1477 devfs_alias_propagate(struct devfs_alias *alias)
1479 struct devfs_mnt_data *mnt;
1481 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1482 devfs_alias_apply(mnt->root_node, alias);
1488 * This function is a recursive function iterating through
1489 * all device nodes in the topology and, if applicable,
1490 * creating the relevant alias for a device node.
1493 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1495 struct devfs_node *node1, *node2;
1497 KKASSERT(alias != NULL);
1499 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1500 if (node->nchildren > 2) {
1501 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1502 devfs_alias_apply(node1, alias);
1506 if (node->d_dev == alias->dev_target)
1507 devfs_alias_create(alias->name, node, 0);
1513 * This function checks if any alias possibly is applicable
1514 * to the given node. If so, the alias is created.
1517 devfs_alias_check_create(struct devfs_node *node)
1519 struct devfs_alias *alias;
1521 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1522 if (node->d_dev == alias->dev_target)
1523 devfs_alias_create(alias->name, node, 0);
1529 * This function creates an alias with a given name
1530 * linking to a given devfs node. It also increments
1531 * the link count on the target node.
1534 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based)
1536 struct mount *mp = target->mp;
1537 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1538 struct devfs_node *linknode;
1539 char *create_path = NULL;
1544 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1546 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1547 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1550 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1553 if (devfs_find_device_node_by_name(parent, name)) {
1554 devfs_debug(DEVFS_DEBUG_WARNING,
1555 "Node already exists: %s "
1556 "(devfs_make_alias_worker)!\n",
1562 linknode = devfs_allocp(Plink, name, parent, mp, NULL);
1563 if (linknode == NULL) {
1568 linknode->link_target = target;
1572 linknode->flags |= DEVFS_RULE_CREATED;
1575 kfree(name_buf, M_TEMP);
1580 * This function is called by the core and handles mount point
1581 * strings. It either calls the relevant worker (devfs_apply_
1582 * reset_rules_worker) on all mountpoints or only a specific
1586 devfs_apply_reset_rules_caller(char *mountto, int apply)
1588 struct devfs_mnt_data *mnt;
1590 if (mountto[0] == '*') {
1591 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1592 devfs_iterate_topology(mnt->root_node,
1593 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1597 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1598 if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) {
1599 devfs_iterate_topology(mnt->root_node,
1600 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1607 kfree(mountto, M_DEVFS);
1612 * This function calls a given callback function for
1613 * every dev node in the devfs dev list.
1616 devfs_scan_callback_worker(devfs_scan_t *callback)
1620 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1628 * This function tries to resolve a given directory, or if not
1629 * found and creation requested, creates the given directory.
1631 static struct devfs_node *
1632 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1633 size_t name_len, int create)
1635 struct devfs_node *node, *found = NULL;
1637 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1638 if (name_len != node->d_dir.d_namlen)
1641 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1647 if ((found == NULL) && (create)) {
1648 found = devfs_allocp(Pdir, dir_name, parent, parent->mp, NULL);
1655 * This function tries to resolve a complete path. If creation is requested,
1656 * if a given part of the path cannot be resolved (because it doesn't exist),
1660 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1662 struct devfs_node *node = parent;
1669 buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1671 while (*path && idx < PATH_MAX - 1) {
1676 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1686 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1687 kfree (buf, M_TEMP);
1692 * Takes a full path and strips it into a directory path and a name.
1693 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
1694 * requires a working buffer with enough size to keep the whole
1698 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
1702 size_t len = strlen(fullpath) + 1;
1705 KKASSERT((fullpath != NULL) && (buf != NULL));
1706 KKASSERT((pathp != NULL) && (namep != NULL));
1708 memcpy(buf, fullpath, len);
1710 for (i = len-1; i>= 0; i--) {
1711 if (buf[i] == '/') {
1731 * This function creates a new devfs node for a given device. It can
1732 * handle a complete path as device name, and accordingly creates
1733 * the path and the final device node.
1735 * The reference count on the passed dev remains unchanged.
1738 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
1739 char *dev_name, char *path_fmt, ...)
1741 struct devfs_node *parent, *node = NULL;
1747 char *create_path = NULL;
1748 char *names = "pqrsPQRS";
1750 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1752 if (path_fmt != NULL) {
1753 __va_start(ap, path_fmt);
1754 kvasnrprintf(&path, PATH_MAX, 10, path_fmt, ap);
1758 parent = devfs_resolve_or_create_path(root, path, 1);
1761 devfs_resolve_name_path(
1762 ((dev_name == NULL) && (dev))?(dev->si_name):(dev_name),
1763 name_buf, &create_path, &name);
1766 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1769 if (devfs_find_device_node_by_name(parent, name)) {
1770 devfs_debug(DEVFS_DEBUG_WARNING, "devfs_create_device_node: "
1771 "DEVICE %s ALREADY EXISTS!!! Ignoring creation request.\n", name);
1775 node = devfs_allocp(Pdev, name, parent, parent->mp, dev);
1776 nanotime(&parent->mtime);
1779 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
1782 if ((dev) && (strlen(dev->si_name) >= 4) &&
1783 (!memcmp(dev->si_name, "ptm/", 4))) {
1784 node->parent->flags |= DEVFS_HIDDEN;
1785 node->flags |= DEVFS_HIDDEN;
1789 * Ugly pty magic, to tag pty devices as such and hide them if needed.
1791 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
1792 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1794 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
1796 for (i = 0; i < strlen(names); i++) {
1797 if (name[3] == names[i]) {
1803 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1807 kfree(name_buf, M_TEMP);
1813 * This function finds a given device node in the topology with a given
1817 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target)
1819 if ((node->node_type == Pdev) && (node->d_dev == target)) {
1827 * This function finds a device node in the given parent directory by its
1828 * name and returns it.
1831 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
1833 struct devfs_node *node, *found = NULL;
1834 size_t len = strlen(target);
1836 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1837 if (len != node->d_dir.d_namlen)
1840 if (!memcmp(node->d_dir.d_name, target, len)) {
1850 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop)
1852 struct vnode *vp = NULL;
1853 ino_t target = *inop;
1855 if (node->d_dir.d_ino == target) {
1858 vget(vp, LK_EXCLUSIVE | LK_RETRY);
1861 devfs_allocv(&vp, node);
1870 * This function takes a cdev and removes its devfs node in the
1871 * given topology. The cdev remains intact.
1874 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
1876 struct devfs_node *node, *parent;
1879 char *create_path = NULL;
1883 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1884 ksnprintf(name_buf, PATH_MAX, "%s", target->si_name);
1886 devfs_resolve_name_path(target->si_name, name_buf, &create_path, &name);
1889 parent = devfs_resolve_or_create_path(root, create_path, 0);
1896 node = devfs_find_device_node_by_name(parent, name);
1899 nanotime(&node->parent->mtime);
1902 kfree(name_buf, M_TEMP);
1908 * Just set perms and ownership for given node.
1911 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid,
1912 u_short mode, u_long flags)
1922 * Propagates a device attach/detach to all mount
1923 * points. Also takes care of automatic alias removal
1924 * for a deleted cdev.
1927 devfs_propagate_dev(cdev_t dev, int attach)
1929 struct devfs_mnt_data *mnt;
1931 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1933 /* Device is being attached */
1934 devfs_create_device_node(mnt->root_node, dev,
1937 /* Device is being detached */
1938 devfs_alias_remove(dev);
1939 devfs_destroy_device_node(mnt->root_node, dev);
1946 * devfs_clone either returns a basename from a complete name by
1947 * returning the length of the name without trailing digits, or,
1948 * if clone != 0, calls the device's clone handler to get a new
1949 * device, which in turn is returned in devp.
1952 devfs_clone(cdev_t dev, const char *name, size_t len, int mode,
1956 struct devfs_clone_handler *chandler;
1957 struct dev_clone_args ap;
1959 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1960 if (chandler->namlen != len)
1962 if ((!memcmp(chandler->name, name, len)) && (chandler->nhandler)) {
1963 lockmgr(&devfs_lock, LK_RELEASE);
1965 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1967 ap.a_head.a_dev = dev;
1973 error = (chandler->nhandler)(&ap);
1986 * Registers a new orphan in the orphan list.
1989 devfs_tracer_add_orphan(struct devfs_node *node)
1991 struct devfs_orphan *orphan;
1994 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
1995 orphan->node = node;
1997 KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
1998 node->flags |= DEVFS_ORPHANED;
1999 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2003 * Removes an orphan from the orphan list.
2006 devfs_tracer_del_orphan(struct devfs_node *node)
2008 struct devfs_orphan *orphan;
2012 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) {
2013 if (orphan->node == node) {
2014 node->flags &= ~DEVFS_ORPHANED;
2015 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2016 kfree(orphan, M_DEVFS);
2023 * Counts the orphans in the orphan list, and if cleanup
2024 * is specified, also frees the orphan and removes it from
2028 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2030 struct devfs_orphan *orphan, *orphan2;
2033 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) {
2036 * If we are instructed to clean up, we do so.
2039 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2040 orphan->node->flags &= ~DEVFS_ORPHANED;
2041 devfs_freep(orphan->node);
2042 kfree(orphan, M_DEVFS);
2050 * Fetch an ino_t from the global d_ino by increasing it
2054 devfs_fetch_ino(void)
2058 spin_lock_wr(&ino_lock);
2060 spin_unlock_wr(&ino_lock);
2066 * Allocates a new cdev and initializes it's most basic
2070 devfs_new_cdev(struct dev_ops *ops, int minor, cdev_t rdev)
2072 cdev_t dev = sysref_alloc(&cdev_sysref_class);
2074 sysref_activate(&dev->si_sysref);
2076 bzero(dev, offsetof(struct cdev, si_sysref));
2081 dev->si_drv1 = NULL;
2082 dev->si_drv2 = NULL;
2083 dev->si_lastread = 0; /* time_second */
2084 dev->si_lastwrite = 0; /* time_second */
2089 dev->si_uminor = minor;
2090 dev->si_rdev = rdev;
2091 /* If there is a backing device, we reference its ops */
2092 dev->si_inode = makeudev(
2093 devfs_reference_ops((rdev)?(rdev->si_ops):(ops)),
2100 devfs_cdev_terminate(cdev_t dev)
2104 /* Check if it is locked already. if not, we acquire the devfs lock */
2105 if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) {
2106 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2110 /* Propagate destruction, just in case */
2111 devfs_propagate_dev(dev, 0);
2113 /* If we acquired the lock, we also get rid of it */
2115 lockmgr(&devfs_lock, LK_RELEASE);
2117 /* If there is a backing device, we release the backing device's ops */
2118 devfs_release_ops((dev->si_rdev)?(dev->si_rdev->si_ops):(dev->si_ops));
2120 /* Finally destroy the device */
2121 sysref_put(&dev->si_sysref);
2125 * Links a given cdev into the dev list.
2128 devfs_link_dev(cdev_t dev)
2130 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2131 dev->si_flags |= SI_DEVFS_LINKED;
2132 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2138 * Removes a given cdev from the dev list. The caller is responsible for
2139 * releasing the reference on the device associated with the linkage.
2141 * Returns EALREADY if the dev has already been unlinked.
2144 devfs_unlink_dev(cdev_t dev)
2146 if ((dev->si_flags & SI_DEVFS_LINKED)) {
2147 TAILQ_REMOVE(&devfs_dev_list, dev, link);
2148 dev->si_flags &= ~SI_DEVFS_LINKED;
2155 devfs_node_is_accessible(struct devfs_node *node)
2157 if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2164 devfs_reference_ops(struct dev_ops *ops)
2167 struct devfs_dev_ops *found = NULL;
2168 struct devfs_dev_ops *devops;
2170 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2171 if (devops->ops == ops) {
2178 found = kmalloc(sizeof(struct devfs_dev_ops), M_DEVFS, M_WAITOK);
2180 found->ref_count = 0;
2181 TAILQ_INSERT_TAIL(&devfs_dev_ops_list, found, link);
2186 if (found->ref_count == 0) {
2187 found->id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255);
2188 if (found->id == -1) {
2189 /* Ran out of unique ids */
2190 devfs_debug(DEVFS_DEBUG_WARNING,
2191 "devfs_reference_ops: WARNING: ran out of unique ids\n");
2201 devfs_release_ops(struct dev_ops *ops)
2203 struct devfs_dev_ops *found = NULL;
2204 struct devfs_dev_ops *devops;
2206 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2207 if (devops->ops == ops) {
2217 if (found->ref_count == 0) {
2218 TAILQ_REMOVE(&devfs_dev_ops_list, found, link);
2219 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), found->id);
2220 kfree(found, M_DEVFS);
2229 msg = devfs_msg_get();
2230 msg = devfs_msg_send_sync(DEVFS_SYNC, msg);
2235 * Called on init of devfs; creates the objcaches and
2236 * spawns off the devfs core thread. Also initializes
2242 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2243 /* Create objcaches for nodes, msgs and devs */
2244 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2246 objcache_malloc_alloc,
2247 objcache_malloc_free,
2248 &devfs_node_malloc_args );
2250 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2252 objcache_malloc_alloc,
2253 objcache_malloc_free,
2254 &devfs_msg_malloc_args );
2256 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2258 objcache_malloc_alloc,
2259 objcache_malloc_free,
2260 &devfs_dev_malloc_args );
2262 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2264 /* Initialize the reply-only port which acts as a message drain */
2265 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2267 /* Initialize *THE* devfs lock */
2268 lockinit(&devfs_lock, "devfs_core lock", 0, 0);
2271 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2272 0, 0, "devfs_msg_core");
2274 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2276 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2280 * Called on unload of devfs; takes care of destroying the core
2281 * and the objcaches. Also removes aliases that are no longer needed.
2286 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2288 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2290 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2291 tsleep(td_core/*devfs_id*/, 0, "devfsc", 10000);
2293 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2295 /* Destroy the objcaches */
2296 objcache_destroy(devfs_msg_cache);
2297 objcache_destroy(devfs_node_cache);
2298 objcache_destroy(devfs_dev_cache);
2304 * This is a sysctl handler to assist userland devname(3) to
2305 * find the device name for a given udev.
2308 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2315 if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t))))
2318 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev);
2323 if ((found = devfs_find_device_by_udev(udev)) == NULL)
2326 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2330 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
2331 NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)");
2333 static SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2334 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2335 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable,
2336 0, "Enable DevFS debugging");
2338 SYSINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST,
2340 SYSUNINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY,
2341 devfs_uninit, NULL);