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 void (*devfs_node_added)(cdev_t) = NULL;
148 void (*devfs_node_removed)(cdev_t) = NULL;
151 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function
155 devfs_debug(int level, char *fmt, ...)
160 if (level <= devfs_debug_enable)
168 * devfs_allocp() Allocates a new devfs node with the specified
169 * parameters. The node is also automatically linked into the topology
170 * if a parent is specified. It also calls the rule and alias stuff to
171 * be applied on the new node
174 devfs_allocp(devfs_nodetype devfsnodetype, char *name,
175 struct devfs_node *parent, struct mount *mp, cdev_t dev)
177 struct devfs_node *node = NULL;
178 size_t namlen = strlen(name);
180 node = objcache_get(devfs_node_cache, M_WAITOK);
181 bzero(node, sizeof(*node));
183 atomic_add_long(&(DEVFS_MNTDATA(mp)->leak_count), 1);
188 node->d_dir.d_ino = devfs_fetch_ino();
191 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries
194 node->cookie_jar = 2;
197 * Access Control members
199 node->mode = DEVFS_DEFAULT_MODE;
200 node->uid = DEVFS_DEFAULT_UID;
201 node->gid = DEVFS_DEFAULT_GID;
203 switch (devfsnodetype) {
206 * Ensure that we don't recycle the root vnode by marking it as
207 * linked into the topology.
209 node->flags |= DEVFS_NODE_LINKED;
211 TAILQ_INIT(DEVFS_DENODE_HEAD(node));
212 node->d_dir.d_type = DT_DIR;
217 node->d_dir.d_type = DT_LNK;
221 node->d_dir.d_type = DT_REG;
226 node->d_dir.d_type = DT_CHR;
229 node->mode = dev->si_perms;
230 node->uid = dev->si_uid;
231 node->gid = dev->si_gid;
233 devfs_alias_check_create(node);
238 panic("devfs_allocp: unknown node type");
242 node->node_type = devfsnodetype;
244 /* Initialize the dirent structure of each devfs vnode */
245 KKASSERT(namlen < 256);
246 node->d_dir.d_namlen = namlen;
247 node->d_dir.d_name = kmalloc(namlen+1, M_DEVFS, M_WAITOK);
248 memcpy(node->d_dir.d_name, name, namlen);
249 node->d_dir.d_name[namlen] = '\0';
251 /* Initialize the parent node element */
252 node->parent = parent;
255 devfs_rule_check_apply(node, NULL);
257 /* Initialize *time members */
258 nanotime(&node->atime);
259 node->mtime = node->ctime = node->atime;
262 * Associate with parent as last step, clean out namecache
265 if ((parent != NULL) &&
266 ((parent->node_type == Proot) || (parent->node_type == Pdir))) {
268 node->cookie = parent->cookie_jar++;
269 node->flags |= DEVFS_NODE_LINKED;
270 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent), node, link);
272 /* This forces negative namecache lookups to clear */
273 ++mp->mnt_namecache_gen;
276 ++DEVFS_MNTDATA(mp)->file_count;
282 * devfs_allocv() allocates a new vnode based on a devfs node.
285 devfs_allocv(struct vnode **vpp, struct devfs_node *node)
293 while ((vp = node->v_node) != NULL) {
294 error = vget(vp, LK_EXCLUSIVE);
295 if (error != ENOENT) {
301 if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0)
306 if (node->v_node != NULL) {
315 switch (node->node_type) {
332 KKASSERT(node->d_dev);
334 vp->v_uminor = node->d_dev->si_uminor;
337 v_associate_rdev(vp, node->d_dev);
338 vp->v_ops = &node->mp->mnt_vn_spec_ops;
342 panic("devfs_allocv: unknown node type");
350 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
351 * based on the newly created devfs node.
354 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype,
355 char *name, struct devfs_node *parent, cdev_t dev)
357 struct devfs_node *node;
359 node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
362 devfs_allocv(vpp, node);
370 * Destroy the devfs_node. The node must be unlinked from the topology.
372 * This function will also destroy any vnode association with the node
375 * The cdev_t itself remains intact.
378 devfs_freep(struct devfs_node *node)
383 KKASSERT(((node->flags & DEVFS_NODE_LINKED) == 0) ||
384 (node->node_type == Proot));
385 KKASSERT((node->flags & DEVFS_DESTROYED) == 0);
387 atomic_subtract_long(&(DEVFS_MNTDATA(node->mp)->leak_count), 1);
388 if (node->symlink_name) {
389 kfree(node->symlink_name, M_DEVFS);
390 node->symlink_name = NULL;
394 * Remove the node from the orphan list if it is still on it.
396 if (node->flags & DEVFS_ORPHANED)
397 devfs_tracer_del_orphan(node);
400 * Disassociate the vnode from the node. This also prevents the
401 * vnode's reclaim code from double-freeing the node.
403 * The vget is needed to safely modify the vp. It also serves
404 * to cycle the refs and terminate the vnode if it happens to
405 * be inactive, otherwise namecache references may not get cleared.
407 while ((vp = node->v_node) != NULL) {
408 if (vget(vp, LK_EXCLUSIVE | LK_RETRY) != 0)
413 cache_inval_vp(vp, CINV_DESTROY);
416 if (node->d_dir.d_name) {
417 kfree(node->d_dir.d_name, M_DEVFS);
418 node->d_dir.d_name = NULL;
420 node->flags |= DEVFS_DESTROYED;
422 --DEVFS_MNTDATA(node->mp)->file_count;
424 objcache_put(devfs_node_cache, node);
430 * Unlink the devfs node from the topology and add it to the orphan list.
431 * The node will later be destroyed by freep.
433 * Any vnode association, including the v_rdev and v_data, remains intact
437 devfs_unlinkp(struct devfs_node *node)
439 struct devfs_node *parent;
443 * Add the node to the orphan list, so it is referenced somewhere, to
444 * so we don't leak it.
446 devfs_tracer_add_orphan(node);
448 parent = node->parent;
451 * If the parent is known we can unlink the node out of the topology
454 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
456 KKASSERT((parent->nchildren >= 0));
457 node->flags &= ~DEVFS_NODE_LINKED;
459 /* hotplug handler */
460 if(devfs_node_removed)
461 devfs_node_removed(node->d_dev);
467 devfs_iterate_topology(struct devfs_node *node,
468 devfs_iterate_callback_t *callback, void *arg1)
470 struct devfs_node *node1, *node2;
473 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
474 if (node->nchildren > 2) {
475 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
477 if ((ret = devfs_iterate_topology(node1, callback, arg1)))
483 ret = callback(node, arg1);
488 * devfs_reaperp() is a recursive function that iterates through all the
489 * topology, unlinking and freeing all devfs nodes.
492 devfs_reaperp_callback(struct devfs_node *node, void *unused)
501 devfs_gc_dirs_callback(struct devfs_node *node, void *unused)
503 if (node->node_type == Pdir) {
504 if (node->nchildren == 2) {
514 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target)
516 if ((node->node_type == Plink) && (node->link_target == target)) {
525 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
526 * freeing a node, but also removes empty directories and links that link
527 * via devfs auto-link mechanism to the node being deleted.
530 devfs_gc(struct devfs_node *node)
532 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
534 if (node->nlinks > 0)
535 devfs_iterate_topology(root_node,
536 (devfs_iterate_callback_t *)devfs_gc_links_callback, node);
539 devfs_iterate_topology(root_node,
540 (devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL);
548 * devfs_create_dev() is the asynchronous entry point for device creation.
549 * It just sends a message with the relevant details to the devfs core.
551 * This function will reference the passed device. The reference is owned
552 * by devfs and represents all of the device's node associations.
555 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
558 devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
564 * devfs_destroy_dev() is the asynchronous entry point for device destruction.
565 * It just sends a message with the relevant details to the devfs core.
568 devfs_destroy_dev(cdev_t dev)
570 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
575 * devfs_mount_add() is the synchronous entry point for adding a new devfs
576 * mount. It sends a synchronous message with the relevant details to the
580 devfs_mount_add(struct devfs_mnt_data *mnt)
584 msg = devfs_msg_get();
586 msg = devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
593 * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
594 * It sends a synchronous message with the relevant details to the devfs core.
597 devfs_mount_del(struct devfs_mnt_data *mnt)
601 msg = devfs_msg_get();
603 msg = devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
610 * devfs_destroy_subnames() is the synchronous entry point for device
611 * destruction by subname. It just sends a message with the relevant details to
615 devfs_destroy_subnames(char *name)
619 msg = devfs_msg_get();
620 msg->mdv_load = name;
621 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES, msg);
627 devfs_clr_subnames_flag(char *name, uint32_t flag)
631 msg = devfs_msg_get();
632 msg->mdv_flags.name = name;
633 msg->mdv_flags.flag = flag;
634 msg = devfs_msg_send_sync(DEVFS_CLR_SUBNAMES_FLAG, msg);
641 devfs_destroy_subnames_without_flag(char *name, uint32_t flag)
645 msg = devfs_msg_get();
646 msg->mdv_flags.name = name;
647 msg->mdv_flags.flag = flag;
648 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES_WO_FLAG, msg);
655 * devfs_create_all_dev is the asynchronous entry point to trigger device
656 * node creation. It just sends a message with the relevant details to
660 devfs_create_all_dev(struct devfs_node *root)
662 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
667 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
668 * devices with a specific set of dev_ops and minor. It just sends a
669 * message with the relevant details to the devfs core.
672 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
674 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
679 * devfs_clone_handler_add is the synchronous entry point to add a new
680 * clone handler. It just sends a message with the relevant details to
684 devfs_clone_handler_add(const char *name, d_clone_t *nhandler)
688 msg = devfs_msg_get();
689 msg->mdv_chandler.name = name;
690 msg->mdv_chandler.nhandler = nhandler;
691 msg = devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg);
697 * devfs_clone_handler_del is the synchronous entry point to remove a
698 * clone handler. It just sends a message with the relevant details to
702 devfs_clone_handler_del(const char *name)
706 msg = devfs_msg_get();
707 msg->mdv_chandler.name = name;
708 msg->mdv_chandler.nhandler = NULL;
709 msg = devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg);
715 * devfs_find_device_by_name is the synchronous entry point to find a
716 * device given its name. It sends a synchronous message with the
717 * relevant details to the devfs core and returns the answer.
720 devfs_find_device_by_name(const char *fmt, ...)
731 kvasnrprintf(&target, PATH_MAX, 10, fmt, ap);
734 msg = devfs_msg_get();
735 msg->mdv_name = target;
736 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
737 found = msg->mdv_cdev;
745 * devfs_find_device_by_udev is the synchronous entry point to find a
746 * device given its udev number. It sends a synchronous message with
747 * the relevant details to the devfs core and returns the answer.
750 devfs_find_device_by_udev(udev_t udev)
755 msg = devfs_msg_get();
756 msg->mdv_udev = udev;
757 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg);
758 found = msg->mdv_cdev;
761 devfs_debug(DEVFS_DEBUG_DEBUG,
762 "devfs_find_device_by_udev found? %s -end:3-\n",
763 ((found) ? found->si_name:"NO"));
768 devfs_inode_to_vnode(struct mount *mp, ino_t target)
770 struct vnode *vp = NULL;
776 msg = devfs_msg_get();
777 msg->mdv_ino.mp = mp;
778 msg->mdv_ino.ino = target;
779 msg = devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg);
780 vp = msg->mdv_ino.vp;
781 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
788 * devfs_make_alias is the asynchronous entry point to register an alias
789 * for a device. It just sends a message with the relevant details to the
793 devfs_make_alias(const char *name, cdev_t dev_target)
795 struct devfs_alias *alias;
800 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
801 alias->name = kstrdup(name, M_DEVFS);
803 alias->dev_target = dev_target;
805 devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
810 * devfs_apply_rules is the asynchronous entry point to trigger application
811 * of all rules. It just sends a message with the relevant details to the
815 devfs_apply_rules(char *mntto)
819 new_name = kstrdup(mntto, M_DEVFS);
820 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
826 * devfs_reset_rules is the asynchronous entry point to trigger reset of all
827 * rules. It just sends a message with the relevant details to the devfs core.
830 devfs_reset_rules(char *mntto)
834 new_name = kstrdup(mntto, M_DEVFS);
835 devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
842 * devfs_scan_callback is the asynchronous entry point to call a callback
844 * It just sends a message with the relevant details to the devfs core.
847 devfs_scan_callback(devfs_scan_t *callback)
851 KKASSERT(sizeof(callback) == sizeof(void *));
853 msg = devfs_msg_get();
854 msg->mdv_load = callback;
855 msg = devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
863 * Acts as a message drain. Any message that is replied to here gets destroyed
864 * and the memory freed.
867 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
869 devfs_msg_put((devfs_msg_t)msg);
873 * devfs_msg_get allocates a new devfs msg and returns it.
878 return objcache_get(devfs_msg_cache, M_WAITOK);
882 * devfs_msg_put deallocates a given devfs msg.
885 devfs_msg_put(devfs_msg_t msg)
887 objcache_put(devfs_msg_cache, msg);
892 * devfs_msg_send is the generic asynchronous message sending facility
893 * for devfs. By default the reply port is the automatic disposal port.
895 * If the current thread is the devfs_msg_port thread we execute the
896 * operation synchronously.
899 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
901 lwkt_port_t port = &devfs_msg_port;
903 lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
905 devfs_msg->hdr.u.ms_result = cmd;
907 if (port->mpu_td == curthread) {
908 devfs_msg_exec(devfs_msg);
909 lwkt_replymsg(&devfs_msg->hdr, 0);
911 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
916 * devfs_msg_send_sync is the generic synchronous message sending
917 * facility for devfs. It initializes a local reply port and waits
918 * for the core's answer. This answer is then returned.
921 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
923 struct lwkt_port rep_port;
924 devfs_msg_t msg_incoming;
925 lwkt_port_t port = &devfs_msg_port;
927 lwkt_initport_thread(&rep_port, curthread);
928 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
930 devfs_msg->hdr.u.ms_result = cmd;
932 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
933 msg_incoming = lwkt_waitport(&rep_port, 0);
939 * sends a message with a generic argument.
942 devfs_msg_send_generic(uint32_t cmd, void *load)
944 devfs_msg_t devfs_msg = devfs_msg_get();
946 devfs_msg->mdv_load = load;
947 devfs_msg_send(cmd, devfs_msg);
951 * sends a message with a name argument.
954 devfs_msg_send_name(uint32_t cmd, char *name)
956 devfs_msg_t devfs_msg = devfs_msg_get();
958 devfs_msg->mdv_name = name;
959 devfs_msg_send(cmd, devfs_msg);
963 * sends a message with a mount argument.
966 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
968 devfs_msg_t devfs_msg = devfs_msg_get();
970 devfs_msg->mdv_mnt = mnt;
971 devfs_msg_send(cmd, devfs_msg);
975 * sends a message with an ops argument.
978 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
980 devfs_msg_t devfs_msg = devfs_msg_get();
982 devfs_msg->mdv_ops.ops = ops;
983 devfs_msg->mdv_ops.minor = minor;
984 devfs_msg_send(cmd, devfs_msg);
988 * sends a message with a clone handler argument.
991 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
993 devfs_msg_t devfs_msg = devfs_msg_get();
995 devfs_msg->mdv_chandler.name = name;
996 devfs_msg->mdv_chandler.nhandler = handler;
997 devfs_msg_send(cmd, devfs_msg);
1001 * sends a message with a device argument.
1004 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1006 devfs_msg_t devfs_msg = devfs_msg_get();
1008 devfs_msg->mdv_dev.dev = dev;
1009 devfs_msg->mdv_dev.uid = uid;
1010 devfs_msg->mdv_dev.gid = gid;
1011 devfs_msg->mdv_dev.perms = perms;
1013 devfs_msg_send(cmd, devfs_msg);
1017 * sends a message with a link argument.
1020 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1022 devfs_msg_t devfs_msg = devfs_msg_get();
1024 devfs_msg->mdv_link.name = name;
1025 devfs_msg->mdv_link.target = target;
1026 devfs_msg->mdv_link.mp = mp;
1027 devfs_msg_send(cmd, devfs_msg);
1031 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1032 * and calls the relevant worker functions. By using messages it's assured
1033 * that events occur in the correct order.
1036 devfs_msg_core(void *arg)
1041 lwkt_initport_thread(&devfs_msg_port, curthread);
1045 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1046 devfs_debug(DEVFS_DEBUG_DEBUG,
1047 "devfs_msg_core, new msg: %x\n",
1048 (unsigned int)msg->hdr.u.ms_result);
1049 devfs_msg_exec(msg);
1050 lwkt_replymsg(&msg->hdr, 0);
1057 devfs_msg_exec(devfs_msg_t msg)
1059 struct devfs_mnt_data *mnt;
1060 struct devfs_node *node;
1064 * Acquire the devfs lock to ensure safety of all called functions
1066 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1068 switch (msg->hdr.u.ms_result) {
1069 case DEVFS_DEVICE_CREATE:
1070 dev = msg->mdv_dev.dev;
1071 devfs_create_dev_worker(dev,
1074 msg->mdv_dev.perms);
1076 case DEVFS_DEVICE_DESTROY:
1077 dev = msg->mdv_dev.dev;
1078 devfs_destroy_dev_worker(dev);
1080 case DEVFS_DESTROY_SUBNAMES:
1081 devfs_destroy_subnames_worker(msg->mdv_load);
1083 case DEVFS_DESTROY_DEV_BY_OPS:
1084 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1085 msg->mdv_ops.minor);
1087 case DEVFS_CREATE_ALL_DEV:
1088 node = (struct devfs_node *)msg->mdv_load;
1089 devfs_create_all_dev_worker(node);
1091 case DEVFS_MOUNT_ADD:
1093 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1094 devfs_create_all_dev_worker(mnt->root_node);
1096 case DEVFS_MOUNT_DEL:
1098 TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1099 devfs_iterate_topology(mnt->root_node, devfs_reaperp_callback,
1101 if (mnt->leak_count) {
1102 devfs_debug(DEVFS_DEBUG_SHOW,
1103 "Leaked %ld devfs_node elements!\n",
1107 case DEVFS_CHANDLER_ADD:
1108 devfs_chandler_add_worker(msg->mdv_chandler.name,
1109 msg->mdv_chandler.nhandler);
1111 case DEVFS_CHANDLER_DEL:
1112 devfs_chandler_del_worker(msg->mdv_chandler.name);
1114 case DEVFS_FIND_DEVICE_BY_NAME:
1115 devfs_find_device_by_name_worker(msg);
1117 case DEVFS_FIND_DEVICE_BY_UDEV:
1118 devfs_find_device_by_udev_worker(msg);
1120 case DEVFS_MAKE_ALIAS:
1121 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1123 case DEVFS_APPLY_RULES:
1124 devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1126 case DEVFS_RESET_RULES:
1127 devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1129 case DEVFS_SCAN_CALLBACK:
1130 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load);
1132 case DEVFS_CLR_SUBNAMES_FLAG:
1133 devfs_clr_subnames_flag_worker(msg->mdv_flags.name,
1134 msg->mdv_flags.flag);
1136 case DEVFS_DESTROY_SUBNAMES_WO_FLAG:
1137 devfs_destroy_subnames_without_flag_worker(msg->mdv_flags.name,
1138 msg->mdv_flags.flag);
1140 case DEVFS_INODE_TO_VNODE:
1141 msg->mdv_ino.vp = devfs_iterate_topology(
1142 DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node,
1143 (devfs_iterate_callback_t *)devfs_inode_to_vnode_worker_callback,
1146 case DEVFS_TERMINATE_CORE:
1152 devfs_debug(DEVFS_DEBUG_WARNING,
1153 "devfs_msg_core: unknown message "
1154 "received at core\n");
1157 lockmgr(&devfs_lock, LK_RELEASE);
1161 * Worker function to insert a new dev into the dev list and initialize its
1162 * permissions. It also calls devfs_propagate_dev which in turn propagates
1163 * the change to all mount points.
1165 * The passed dev is already referenced. This reference is eaten by this
1166 * function and represents the dev's linkage into devfs_dev_list.
1169 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1175 dev->si_perms = perms;
1177 devfs_link_dev(dev);
1178 devfs_propagate_dev(dev, 1);
1184 * Worker function to delete a dev from the dev list and free the cdev.
1185 * It also calls devfs_propagate_dev which in turn propagates the change
1186 * to all mount points.
1189 devfs_destroy_dev_worker(cdev_t dev)
1194 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1196 error = devfs_unlink_dev(dev);
1197 devfs_propagate_dev(dev, 0);
1199 release_dev(dev); /* link ref */
1207 * Worker function to destroy all devices with a certain basename.
1208 * Calls devfs_destroy_dev_worker for the actual destruction.
1211 devfs_destroy_subnames_worker(char *name)
1214 size_t len = strlen(name);
1216 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1217 if ((!strncmp(dev->si_name, name, len)) &&
1218 (dev->si_name[len] != '\0')) {
1219 devfs_destroy_dev_worker(dev);
1226 devfs_clr_subnames_flag_worker(char *name, uint32_t flag)
1229 size_t len = strlen(name);
1231 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1232 if ((!strncmp(dev->si_name, name, len)) &&
1233 (dev->si_name[len] != '\0')) {
1234 dev->si_flags &= ~flag;
1242 devfs_destroy_subnames_without_flag_worker(char *name, uint32_t flag)
1245 size_t len = strlen(name);
1247 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1248 if ((!strncmp(dev->si_name, name, len)) &&
1249 (dev->si_name[len] != '\0')) {
1250 if (!(dev->si_flags & flag)) {
1251 devfs_destroy_dev_worker(dev);
1260 * Worker function that creates all device nodes on top of a devfs
1264 devfs_create_all_dev_worker(struct devfs_node *root)
1270 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1271 devfs_create_device_node(root, dev, NULL, NULL);
1278 * Worker function that destroys all devices that match a specific
1279 * dev_ops and/or minor. If minor is less than 0, it is not matched
1280 * against. It also propagates all changes.
1283 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1289 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1290 if (dev->si_ops != ops)
1292 if ((minor < 0) || (dev->si_uminor == minor)) {
1293 devfs_destroy_dev_worker(dev);
1301 * Worker function that registers a new clone handler in devfs.
1304 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler)
1306 struct devfs_clone_handler *chandler = NULL;
1307 u_char len = strlen(name);
1312 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1313 if (chandler->namlen != len)
1316 if (!memcmp(chandler->name, name, len)) {
1317 /* Clonable basename already exists */
1322 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1323 chandler->name = kstrdup(name, M_DEVFS);
1324 chandler->namlen = len;
1325 chandler->nhandler = nhandler;
1327 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1332 * Worker function that removes a given clone handler from the
1333 * clone handler list.
1336 devfs_chandler_del_worker(const char *name)
1338 struct devfs_clone_handler *chandler, *chandler2;
1339 u_char len = strlen(name);
1344 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1345 if (chandler->namlen != len)
1347 if (memcmp(chandler->name, name, len))
1350 TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1351 kfree(chandler->name, M_DEVFS);
1352 kfree(chandler, M_DEVFS);
1360 * Worker function that finds a given device name and changes
1361 * the message received accordingly so that when replied to,
1362 * the answer is returned to the caller.
1365 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1367 struct devfs_alias *alias;
1369 cdev_t found = NULL;
1371 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1372 if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1377 if (found == NULL) {
1378 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1379 if (strcmp(devfs_msg->mdv_name, alias->name) == 0) {
1380 found = alias->dev_target;
1385 devfs_msg->mdv_cdev = found;
1391 * Worker function that finds a given device udev and changes
1392 * the message received accordingly so that when replied to,
1393 * the answer is returned to the caller.
1396 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg)
1399 cdev_t found = NULL;
1401 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1402 if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1407 devfs_msg->mdv_cdev = found;
1413 * Worker function that inserts a given alias into the
1414 * alias list, and propagates the alias to all mount
1418 devfs_make_alias_worker(struct devfs_alias *alias)
1420 struct devfs_alias *alias2;
1421 size_t len = strlen(alias->name);
1424 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1425 if (len != alias2->namlen)
1428 if (!memcmp(alias->name, alias2->name, len)) {
1436 * The alias doesn't exist yet, so we add it to the alias list
1438 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1439 devfs_alias_propagate(alias);
1441 devfs_debug(DEVFS_DEBUG_WARNING,
1442 "Warning: duplicate devfs_make_alias for %s\n",
1444 kfree(alias->name, M_DEVFS);
1445 kfree(alias, M_DEVFS);
1452 * Function that removes and frees all aliases.
1455 devfs_alias_reap(void)
1457 struct devfs_alias *alias, *alias2;
1459 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1460 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1461 kfree(alias, M_DEVFS);
1467 * Function that removes an alias matching a specific cdev and frees
1471 devfs_alias_remove(cdev_t dev)
1473 struct devfs_alias *alias, *alias2;
1475 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1476 if (alias->dev_target == dev) {
1477 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1478 kfree(alias, M_DEVFS);
1485 * This function propagates a new alias to all mount points.
1488 devfs_alias_propagate(struct devfs_alias *alias)
1490 struct devfs_mnt_data *mnt;
1492 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1493 devfs_alias_apply(mnt->root_node, alias);
1499 * This function is a recursive function iterating through
1500 * all device nodes in the topology and, if applicable,
1501 * creating the relevant alias for a device node.
1504 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1506 struct devfs_node *node1, *node2;
1508 KKASSERT(alias != NULL);
1510 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1511 if (node->nchildren > 2) {
1512 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1513 devfs_alias_apply(node1, alias);
1517 if (node->d_dev == alias->dev_target)
1518 devfs_alias_create(alias->name, node, 0);
1524 * This function checks if any alias possibly is applicable
1525 * to the given node. If so, the alias is created.
1528 devfs_alias_check_create(struct devfs_node *node)
1530 struct devfs_alias *alias;
1532 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1533 if (node->d_dev == alias->dev_target)
1534 devfs_alias_create(alias->name, node, 0);
1540 * This function creates an alias with a given name
1541 * linking to a given devfs node. It also increments
1542 * the link count on the target node.
1545 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based)
1547 struct mount *mp = target->mp;
1548 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1549 struct devfs_node *linknode;
1550 char *create_path = NULL;
1555 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1557 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1558 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1561 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1564 if (devfs_find_device_node_by_name(parent, name)) {
1565 devfs_debug(DEVFS_DEBUG_WARNING,
1566 "Node already exists: %s "
1567 "(devfs_make_alias_worker)!\n",
1573 linknode = devfs_allocp(Plink, name, parent, mp, NULL);
1574 if (linknode == NULL) {
1579 linknode->link_target = target;
1583 linknode->flags |= DEVFS_RULE_CREATED;
1586 /* hotplug handler */
1587 if(devfs_node_added)
1588 devfs_node_added(target->d_dev);
1589 kfree(name_buf, M_TEMP);
1594 * This function is called by the core and handles mount point
1595 * strings. It either calls the relevant worker (devfs_apply_
1596 * reset_rules_worker) on all mountpoints or only a specific
1600 devfs_apply_reset_rules_caller(char *mountto, int apply)
1602 struct devfs_mnt_data *mnt;
1604 if (mountto[0] == '*') {
1605 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1606 devfs_iterate_topology(mnt->root_node,
1607 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1611 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1612 if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) {
1613 devfs_iterate_topology(mnt->root_node,
1614 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1621 kfree(mountto, M_DEVFS);
1626 * This function calls a given callback function for
1627 * every dev node in the devfs dev list.
1630 devfs_scan_callback_worker(devfs_scan_t *callback)
1634 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1642 * This function tries to resolve a given directory, or if not
1643 * found and creation requested, creates the given directory.
1645 static struct devfs_node *
1646 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1647 size_t name_len, int create)
1649 struct devfs_node *node, *found = NULL;
1651 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1652 if (name_len != node->d_dir.d_namlen)
1655 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1661 if ((found == NULL) && (create)) {
1662 found = devfs_allocp(Pdir, dir_name, parent, parent->mp, NULL);
1669 * This function tries to resolve a complete path. If creation is requested,
1670 * if a given part of the path cannot be resolved (because it doesn't exist),
1674 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1676 struct devfs_node *node = parent;
1683 buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1685 while (*path && idx < PATH_MAX - 1) {
1690 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1700 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1701 kfree (buf, M_TEMP);
1706 * Takes a full path and strips it into a directory path and a name.
1707 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
1708 * requires a working buffer with enough size to keep the whole
1712 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
1716 size_t len = strlen(fullpath) + 1;
1719 KKASSERT((fullpath != NULL) && (buf != NULL));
1720 KKASSERT((pathp != NULL) && (namep != NULL));
1722 memcpy(buf, fullpath, len);
1724 for (i = len-1; i>= 0; i--) {
1725 if (buf[i] == '/') {
1745 * This function creates a new devfs node for a given device. It can
1746 * handle a complete path as device name, and accordingly creates
1747 * the path and the final device node.
1749 * The reference count on the passed dev remains unchanged.
1752 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
1753 char *dev_name, char *path_fmt, ...)
1755 struct devfs_node *parent, *node = NULL;
1761 char *create_path = NULL;
1762 char *names = "pqrsPQRS";
1764 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1766 if (path_fmt != NULL) {
1767 __va_start(ap, path_fmt);
1768 kvasnrprintf(&path, PATH_MAX, 10, path_fmt, ap);
1772 parent = devfs_resolve_or_create_path(root, path, 1);
1775 devfs_resolve_name_path(
1776 ((dev_name == NULL) && (dev))?(dev->si_name):(dev_name),
1777 name_buf, &create_path, &name);
1780 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1783 if (devfs_find_device_node_by_name(parent, name)) {
1784 devfs_debug(DEVFS_DEBUG_WARNING, "devfs_create_device_node: "
1785 "DEVICE %s ALREADY EXISTS!!! Ignoring creation request.\n", name);
1789 node = devfs_allocp(Pdev, name, parent, parent->mp, dev);
1790 nanotime(&parent->mtime);
1793 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
1796 if ((dev) && (strlen(dev->si_name) >= 4) &&
1797 (!memcmp(dev->si_name, "ptm/", 4))) {
1798 node->parent->flags |= DEVFS_HIDDEN;
1799 node->flags |= DEVFS_HIDDEN;
1803 * Ugly pty magic, to tag pty devices as such and hide them if needed.
1805 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
1806 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1808 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
1810 for (i = 0; i < strlen(names); i++) {
1811 if (name[3] == names[i]) {
1817 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1819 /* hotplug handler */
1820 if(devfs_node_added)
1821 devfs_node_added(node->d_dev);
1824 kfree(name_buf, M_TEMP);
1830 * This function finds a given device node in the topology with a given
1834 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target)
1836 if ((node->node_type == Pdev) && (node->d_dev == target)) {
1844 * This function finds a device node in the given parent directory by its
1845 * name and returns it.
1848 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
1850 struct devfs_node *node, *found = NULL;
1851 size_t len = strlen(target);
1853 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1854 if (len != node->d_dir.d_namlen)
1857 if (!memcmp(node->d_dir.d_name, target, len)) {
1867 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop)
1869 struct vnode *vp = NULL;
1870 ino_t target = *inop;
1872 if (node->d_dir.d_ino == target) {
1875 vget(vp, LK_EXCLUSIVE | LK_RETRY);
1878 devfs_allocv(&vp, node);
1887 * This function takes a cdev and removes its devfs node in the
1888 * given topology. The cdev remains intact.
1891 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
1893 struct devfs_node *node, *parent;
1896 char *create_path = NULL;
1900 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1901 ksnprintf(name_buf, PATH_MAX, "%s", target->si_name);
1903 devfs_resolve_name_path(target->si_name, name_buf, &create_path, &name);
1906 parent = devfs_resolve_or_create_path(root, create_path, 0);
1913 node = devfs_find_device_node_by_name(parent, name);
1916 nanotime(&node->parent->mtime);
1920 kfree(name_buf, M_TEMP);
1926 * Just set perms and ownership for given node.
1929 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid,
1930 u_short mode, u_long flags)
1940 * Propagates a device attach/detach to all mount
1941 * points. Also takes care of automatic alias removal
1942 * for a deleted cdev.
1945 devfs_propagate_dev(cdev_t dev, int attach)
1947 struct devfs_mnt_data *mnt;
1949 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1951 /* Device is being attached */
1952 devfs_create_device_node(mnt->root_node, dev,
1955 /* Device is being detached */
1956 devfs_alias_remove(dev);
1957 devfs_destroy_device_node(mnt->root_node, dev);
1964 * devfs_clone either returns a basename from a complete name by
1965 * returning the length of the name without trailing digits, or,
1966 * if clone != 0, calls the device's clone handler to get a new
1967 * device, which in turn is returned in devp.
1970 devfs_clone(cdev_t dev, const char *name, size_t len, int mode,
1974 struct devfs_clone_handler *chandler;
1975 struct dev_clone_args ap;
1977 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1978 if (chandler->namlen != len)
1980 if ((!memcmp(chandler->name, name, len)) && (chandler->nhandler)) {
1981 lockmgr(&devfs_lock, LK_RELEASE);
1983 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1985 ap.a_head.a_dev = dev;
1991 error = (chandler->nhandler)(&ap);
2004 * Registers a new orphan in the orphan list.
2007 devfs_tracer_add_orphan(struct devfs_node *node)
2009 struct devfs_orphan *orphan;
2012 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2013 orphan->node = node;
2015 KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2016 node->flags |= DEVFS_ORPHANED;
2017 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2021 * Removes an orphan from the orphan list.
2024 devfs_tracer_del_orphan(struct devfs_node *node)
2026 struct devfs_orphan *orphan;
2030 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) {
2031 if (orphan->node == node) {
2032 node->flags &= ~DEVFS_ORPHANED;
2033 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2034 kfree(orphan, M_DEVFS);
2041 * Counts the orphans in the orphan list, and if cleanup
2042 * is specified, also frees the orphan and removes it from
2046 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2048 struct devfs_orphan *orphan, *orphan2;
2051 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) {
2054 * If we are instructed to clean up, we do so.
2057 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2058 orphan->node->flags &= ~DEVFS_ORPHANED;
2059 devfs_freep(orphan->node);
2060 kfree(orphan, M_DEVFS);
2068 * Fetch an ino_t from the global d_ino by increasing it
2072 devfs_fetch_ino(void)
2076 spin_lock_wr(&ino_lock);
2078 spin_unlock_wr(&ino_lock);
2084 * Allocates a new cdev and initializes it's most basic
2088 devfs_new_cdev(struct dev_ops *ops, int minor, struct dev_ops *bops)
2090 cdev_t dev = sysref_alloc(&cdev_sysref_class);
2092 sysref_activate(&dev->si_sysref);
2094 bzero(dev, offsetof(struct cdev, si_sysref));
2099 dev->si_drv1 = NULL;
2100 dev->si_drv2 = NULL;
2101 dev->si_lastread = 0; /* time_second */
2102 dev->si_lastwrite = 0; /* time_second */
2107 dev->si_uminor = minor;
2108 dev->si_bops = bops;
2109 /* If there is a backing device, we reference its ops */
2110 dev->si_inode = makeudev(
2111 devfs_reference_ops((bops)?(bops):(ops)),
2118 devfs_cdev_terminate(cdev_t dev)
2122 /* Check if it is locked already. if not, we acquire the devfs lock */
2123 if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) {
2124 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2128 /* Propagate destruction, just in case */
2129 devfs_propagate_dev(dev, 0);
2131 /* If we acquired the lock, we also get rid of it */
2133 lockmgr(&devfs_lock, LK_RELEASE);
2135 /* If there is a backing device, we release the backing device's ops */
2136 devfs_release_ops((dev->si_bops)?(dev->si_bops):(dev->si_ops));
2138 /* Finally destroy the device */
2139 sysref_put(&dev->si_sysref);
2143 * Links a given cdev into the dev list.
2146 devfs_link_dev(cdev_t dev)
2148 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2149 dev->si_flags |= SI_DEVFS_LINKED;
2150 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2156 * Removes a given cdev from the dev list. The caller is responsible for
2157 * releasing the reference on the device associated with the linkage.
2159 * Returns EALREADY if the dev has already been unlinked.
2162 devfs_unlink_dev(cdev_t dev)
2164 if ((dev->si_flags & SI_DEVFS_LINKED)) {
2165 TAILQ_REMOVE(&devfs_dev_list, dev, link);
2166 dev->si_flags &= ~SI_DEVFS_LINKED;
2173 devfs_node_is_accessible(struct devfs_node *node)
2175 if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2182 devfs_reference_ops(struct dev_ops *ops)
2185 struct devfs_dev_ops *found = NULL;
2186 struct devfs_dev_ops *devops;
2188 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2189 if (devops->ops == ops) {
2196 found = kmalloc(sizeof(struct devfs_dev_ops), M_DEVFS, M_WAITOK);
2198 found->ref_count = 0;
2199 TAILQ_INSERT_TAIL(&devfs_dev_ops_list, found, link);
2204 if (found->ref_count == 0) {
2205 found->id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255);
2206 if (found->id == -1) {
2207 /* Ran out of unique ids */
2208 devfs_debug(DEVFS_DEBUG_WARNING,
2209 "devfs_reference_ops: WARNING: ran out of unique ids\n");
2219 devfs_release_ops(struct dev_ops *ops)
2221 struct devfs_dev_ops *found = NULL;
2222 struct devfs_dev_ops *devops;
2224 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2225 if (devops->ops == ops) {
2235 if (found->ref_count == 0) {
2236 TAILQ_REMOVE(&devfs_dev_ops_list, found, link);
2237 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), found->id);
2238 kfree(found, M_DEVFS);
2247 msg = devfs_msg_get();
2248 msg = devfs_msg_send_sync(DEVFS_SYNC, msg);
2253 * Called on init of devfs; creates the objcaches and
2254 * spawns off the devfs core thread. Also initializes
2260 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2261 /* Create objcaches for nodes, msgs and devs */
2262 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2264 objcache_malloc_alloc,
2265 objcache_malloc_free,
2266 &devfs_node_malloc_args );
2268 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2270 objcache_malloc_alloc,
2271 objcache_malloc_free,
2272 &devfs_msg_malloc_args );
2274 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2276 objcache_malloc_alloc,
2277 objcache_malloc_free,
2278 &devfs_dev_malloc_args );
2280 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2282 /* Initialize the reply-only port which acts as a message drain */
2283 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2285 /* Initialize *THE* devfs lock */
2286 lockinit(&devfs_lock, "devfs_core lock", 0, 0);
2289 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2290 0, 0, "devfs_msg_core");
2292 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2294 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2298 * Called on unload of devfs; takes care of destroying the core
2299 * and the objcaches. Also removes aliases that are no longer needed.
2304 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2306 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2308 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2309 tsleep(td_core/*devfs_id*/, 0, "devfsc", 10000);
2311 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2313 /* Destroy the objcaches */
2314 objcache_destroy(devfs_msg_cache);
2315 objcache_destroy(devfs_node_cache);
2316 objcache_destroy(devfs_dev_cache);
2322 * This is a sysctl handler to assist userland devname(3) to
2323 * find the device name for a given udev.
2326 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2333 if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t))))
2336 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev);
2341 if ((found = devfs_find_device_by_udev(udev)) == NULL)
2344 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2348 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
2349 NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)");
2351 static SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2352 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2353 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable,
2354 0, "Enable DevFS debugging");
2356 SYSINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST,
2358 SYSUNINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY,
2359 devfs_uninit, NULL);