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 <vfs/devfs/devfs.h>
50 #include <vfs/devfs/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);
92 struct lock devfs_lock;
93 static struct lwkt_port devfs_dispose_port;
94 static struct lwkt_port devfs_msg_port;
95 static struct thread *td_core;
97 static struct spinlock ino_lock;
99 static int devfs_debug_enable;
100 static int devfs_run;
102 static ino_t devfs_fetch_ino(void);
103 static int devfs_gc_dirs(struct devfs_node *);
104 static int devfs_gc_links(struct devfs_node *, struct devfs_node *, size_t);
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(char *, d_clone_t *);
115 static int devfs_chandler_del_worker(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 struct vnode *devfs_inode_to_vnode_worker(struct devfs_node *, ino_t);
125 static int devfs_apply_reset_rules_caller(char *, int);
126 static int devfs_apply_reset_rules_worker(struct devfs_node *, int);
128 static int devfs_scan_callback_worker(devfs_scan_t *);
130 static struct devfs_node *devfs_resolve_or_create_dir(struct devfs_node *,
131 char *, size_t, int);
133 static int devfs_make_alias_worker(struct devfs_alias *);
134 static int devfs_alias_remove(cdev_t);
135 static int devfs_alias_reap(void);
136 static int devfs_alias_propagate(struct devfs_alias *);
137 static int devfs_alias_apply(struct devfs_node *, struct devfs_alias *);
138 static int devfs_alias_check_create(struct devfs_node *);
140 static int devfs_clr_subnames_flag_worker(char *, uint32_t);
141 static int devfs_destroy_subnames_without_flag_worker(char *, uint32_t);
144 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function
148 devfs_debug(int level, char *fmt, ...)
153 if (level <= devfs_debug_enable)
161 * devfs_allocp() Allocates a new devfs node with the specified
162 * parameters. The node is also automatically linked into the topology
163 * if a parent is specified. It also calls the rule and alias stuff to
164 * be applied on the new node
167 devfs_allocp(devfs_nodetype devfsnodetype, char *name,
168 struct devfs_node *parent, struct mount *mp, cdev_t dev)
170 struct devfs_node *node = NULL;
171 size_t namlen = strlen(name);
173 node = objcache_get(devfs_node_cache, M_WAITOK);
174 bzero(node, sizeof(*node));
176 atomic_add_int(&(DEVFS_MNTDATA(mp)->leak_count), 1);
181 node->d_dir.d_ino = devfs_fetch_ino();
184 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries
187 node->cookie_jar = 2;
190 * Access Control members
192 node->mode = DEVFS_DEFAULT_MODE;
193 node->uid = DEVFS_DEFAULT_UID;
194 node->gid = DEVFS_DEFAULT_GID;
196 switch (devfsnodetype) {
199 * Ensure that we don't recycle the root vnode by marking it as
200 * linked into the topology.
202 node->flags |= DEVFS_NODE_LINKED;
204 TAILQ_INIT(DEVFS_DENODE_HEAD(node));
205 node->d_dir.d_type = DT_DIR;
210 node->d_dir.d_type = DT_LNK;
214 node->d_dir.d_type = DT_REG;
219 node->d_dir.d_type = DT_CHR;
222 node->mode = dev->si_perms;
223 node->uid = dev->si_uid;
224 node->gid = dev->si_gid;
226 devfs_alias_check_create(node);
231 panic("devfs_allocp: unknown node type");
235 node->node_type = devfsnodetype;
237 /* Initialize the dirent structure of each devfs vnode */
238 KKASSERT(namlen < 256);
239 node->d_dir.d_namlen = namlen;
240 node->d_dir.d_name = kmalloc(namlen+1, M_DEVFS, M_WAITOK);
241 memcpy(node->d_dir.d_name, name, namlen);
242 node->d_dir.d_name[namlen] = '\0';
244 /* Initialize the parent node element */
245 node->parent = parent;
248 devfs_rule_check_apply(node);
250 /* Initialize *time members */
251 nanotime(&node->atime);
252 node->mtime = node->ctime = node->atime;
255 * Associate with parent as last step, clean out namecache
258 if ((parent != NULL) &&
259 ((parent->node_type == Proot) || (parent->node_type == Pdir))) {
261 node->cookie = parent->cookie_jar++;
262 node->flags |= DEVFS_NODE_LINKED;
263 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent), node, link);
265 /* This forces negative namecache lookups to clear */
266 ++mp->mnt_namecache_gen;
273 * devfs_allocv() allocates a new vnode based on a devfs node.
276 devfs_allocv(struct vnode **vpp, struct devfs_node *node)
284 while ((vp = node->v_node) != NULL) {
285 error = vget(vp, LK_EXCLUSIVE);
286 if (error != ENOENT) {
292 if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0)
297 if (node->v_node != NULL) {
306 switch (node->node_type) {
323 KKASSERT(node->d_dev);
325 vp->v_uminor = node->d_dev->si_uminor;
328 v_associate_rdev(vp, node->d_dev);
329 vp->v_ops = &node->mp->mnt_vn_spec_ops;
333 panic("devfs_allocv: unknown node type");
341 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
342 * based on the newly created devfs node.
345 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype,
346 char *name, struct devfs_node *parent, cdev_t dev)
348 struct devfs_node *node;
350 node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
353 devfs_allocv(vpp, node);
361 * Destroy the devfs_node. The node must be unlinked from the topology.
363 * This function will also destroy any vnode association with the node
366 * The cdev_t itself remains intact.
369 devfs_freep(struct devfs_node *node)
374 KKASSERT(((node->flags & DEVFS_NODE_LINKED) == 0) ||
375 (node->node_type == Proot));
376 KKASSERT((node->flags & DEVFS_DESTROYED) == 0);
378 atomic_subtract_int(&(DEVFS_MNTDATA(node->mp)->leak_count), 1);
379 if (node->symlink_name) {
380 kfree(node->symlink_name, M_DEVFS);
381 node->symlink_name = NULL;
385 * Remove the node from the orphan list if it is still on it.
387 if (node->flags & DEVFS_ORPHANED)
388 devfs_tracer_del_orphan(node);
391 * Disassociate the vnode from the node. This also prevents the
392 * vnode's reclaim code from double-freeing the node.
394 * The vget is needed to safely modify the vp. It also serves
395 * to cycle the refs and terminate the vnode if it happens to
396 * be inactive, otherwise namecache references may not get cleared.
398 while ((vp = node->v_node) != NULL) {
399 if (vget(vp, LK_EXCLUSIVE | LK_RETRY) != 0)
406 if (node->d_dir.d_name)
407 kfree(node->d_dir.d_name, M_DEVFS);
408 node->flags |= DEVFS_DESTROYED;
410 objcache_put(devfs_node_cache, node);
416 * Unlink the devfs node from the topology and add it to the orphan list.
417 * The node will later be destroyed by freep.
419 * Any vnode association, including the v_rdev and v_data, remains intact
423 devfs_unlinkp(struct devfs_node *node)
425 struct devfs_node *parent;
429 * Add the node to the orphan list, so it is referenced somewhere, to
430 * so we don't leak it.
432 devfs_tracer_add_orphan(node);
434 parent = node->parent;
437 * If the parent is known we can unlink the node out of the topology
440 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
442 KKASSERT((parent->nchildren >= 0));
443 node->flags &= ~DEVFS_NODE_LINKED;
450 * devfs_reaperp() is a recursive function that iterates through all the
451 * topology, unlinking and freeing all devfs nodes.
454 devfs_reaperp(struct devfs_node *node)
456 struct devfs_node *node1, *node2;
458 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
459 if (node->nchildren > 2) {
460 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
462 devfs_reaperp(node1);
473 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
474 * freeing a node, but also removes empty directories and links that link
475 * via devfs auto-link mechanism to the node being deleted.
478 devfs_gc(struct devfs_node *node)
480 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
482 devfs_gc_links(root_node, node, node->nlinks);
484 devfs_gc_dirs(root_node);
492 * devfs_gc_dirs() is a helper function for devfs_gc, unlinking and freeing
496 devfs_gc_dirs(struct devfs_node *node)
498 struct devfs_node *node1, *node2;
500 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
501 if (node->nchildren > 2) {
502 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
504 devfs_gc_dirs(node1);
508 if (node->nchildren == 2) {
518 * devfs_gc_links() is a helper function for devfs_gc, unlinking and freeing
519 * eauto-linked nodes linking to the node being deleted.
522 devfs_gc_links(struct devfs_node *node, struct devfs_node *target,
525 struct devfs_node *node1, *node2;
528 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
529 if (node->nchildren > 2) {
530 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
532 nlinks = devfs_gc_links(node1, target, nlinks);
535 } else if (node->link_target == target) {
542 KKASSERT(nlinks >= 0);
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(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(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, ...)
724 char target[PATH_MAX+1];
732 i = kvcprintf(fmt, NULL, target, 10, ap);
736 msg = devfs_msg_get();
737 msg->mdv_name = target;
738 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
739 found = msg->mdv_cdev;
746 * devfs_find_device_by_udev is the synchronous entry point to find a
747 * device given its udev number. It sends a synchronous message with
748 * the relevant details to the devfs core and returns the answer.
751 devfs_find_device_by_udev(udev_t udev)
756 msg = devfs_msg_get();
757 msg->mdv_udev = udev;
758 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg);
759 found = msg->mdv_cdev;
762 devfs_debug(DEVFS_DEBUG_DEBUG,
763 "devfs_find_device_by_udev found? %s -end:3-\n",
764 ((found) ? found->si_name:"NO"));
769 devfs_inode_to_vnode(struct mount *mp, ino_t target)
771 struct vnode *vp = NULL;
777 msg = devfs_msg_get();
778 msg->mdv_ino.mp = mp;
779 msg->mdv_ino.ino = target;
780 msg = devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg);
781 vp = msg->mdv_ino.vp;
782 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
789 * devfs_make_alias is the asynchronous entry point to register an alias
790 * for a device. It just sends a message with the relevant details to the
794 devfs_make_alias(char *name, cdev_t dev_target)
796 struct devfs_alias *alias;
801 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
802 alias->name = kmalloc(len + 1, M_DEVFS, M_WAITOK);
803 memcpy(alias->name, name, len + 1);
805 alias->dev_target = dev_target;
807 devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
812 * devfs_apply_rules is the asynchronous entry point to trigger application
813 * of all rules. It just sends a message with the relevant details to the
817 devfs_apply_rules(char *mntto)
822 namelen = strlen(mntto) + 1;
823 new_name = kmalloc(namelen, M_DEVFS, M_WAITOK);
824 memcpy(new_name, mntto, namelen);
825 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
831 * devfs_reset_rules is the asynchronous entry point to trigger reset of all
832 * rules. It just sends a message with the relevant details to the devfs core.
835 devfs_reset_rules(char *mntto)
840 namelen = strlen(mntto) + 1;
841 new_name = kmalloc(namelen, M_DEVFS, M_WAITOK);
842 memcpy(new_name, mntto, namelen);
843 devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
850 * devfs_scan_callback is the asynchronous entry point to call a callback
852 * It just sends a message with the relevant details to the devfs core.
855 devfs_scan_callback(devfs_scan_t *callback)
859 KKASSERT(sizeof(callback) == sizeof(void *));
861 msg = devfs_msg_get();
862 msg->mdv_load = callback;
863 msg = devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
871 * Acts as a message drain. Any message that is replied to here gets destroyed
872 * and the memory freed.
875 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
877 devfs_msg_put((devfs_msg_t)msg);
881 * devfs_msg_get allocates a new devfs msg and returns it.
886 return objcache_get(devfs_msg_cache, M_WAITOK);
890 * devfs_msg_put deallocates a given devfs msg.
893 devfs_msg_put(devfs_msg_t msg)
895 objcache_put(devfs_msg_cache, msg);
900 * devfs_msg_send is the generic asynchronous message sending facility
901 * for devfs. By default the reply port is the automatic disposal port.
903 * If the current thread is the devfs_msg_port thread we execute the
904 * operation synchronously.
907 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
909 lwkt_port_t port = &devfs_msg_port;
911 lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
913 devfs_msg->hdr.u.ms_result = cmd;
915 if (port->mpu_td == curthread) {
916 devfs_msg_exec(devfs_msg);
917 lwkt_replymsg(&devfs_msg->hdr, 0);
919 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
924 * devfs_msg_send_sync is the generic synchronous message sending
925 * facility for devfs. It initializes a local reply port and waits
926 * for the core's answer. This answer is then returned.
929 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
931 struct lwkt_port rep_port;
932 devfs_msg_t msg_incoming;
933 lwkt_port_t port = &devfs_msg_port;
935 lwkt_initport_thread(&rep_port, curthread);
936 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
938 devfs_msg->hdr.u.ms_result = cmd;
940 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
941 msg_incoming = lwkt_waitport(&rep_port, 0);
947 * sends a message with a generic argument.
950 devfs_msg_send_generic(uint32_t cmd, void *load)
952 devfs_msg_t devfs_msg = devfs_msg_get();
954 devfs_msg->mdv_load = load;
955 devfs_msg_send(cmd, devfs_msg);
959 * sends a message with a name argument.
962 devfs_msg_send_name(uint32_t cmd, char *name)
964 devfs_msg_t devfs_msg = devfs_msg_get();
966 devfs_msg->mdv_name = name;
967 devfs_msg_send(cmd, devfs_msg);
971 * sends a message with a mount argument.
974 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
976 devfs_msg_t devfs_msg = devfs_msg_get();
978 devfs_msg->mdv_mnt = mnt;
979 devfs_msg_send(cmd, devfs_msg);
983 * sends a message with an ops argument.
986 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
988 devfs_msg_t devfs_msg = devfs_msg_get();
990 devfs_msg->mdv_ops.ops = ops;
991 devfs_msg->mdv_ops.minor = minor;
992 devfs_msg_send(cmd, devfs_msg);
996 * sends a message with a clone handler argument.
999 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
1001 devfs_msg_t devfs_msg = devfs_msg_get();
1003 devfs_msg->mdv_chandler.name = name;
1004 devfs_msg->mdv_chandler.nhandler = handler;
1005 devfs_msg_send(cmd, devfs_msg);
1009 * sends a message with a device argument.
1012 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1014 devfs_msg_t devfs_msg = devfs_msg_get();
1016 devfs_msg->mdv_dev.dev = dev;
1017 devfs_msg->mdv_dev.uid = uid;
1018 devfs_msg->mdv_dev.gid = gid;
1019 devfs_msg->mdv_dev.perms = perms;
1021 devfs_msg_send(cmd, devfs_msg);
1025 * sends a message with a link argument.
1028 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1030 devfs_msg_t devfs_msg = devfs_msg_get();
1032 devfs_msg->mdv_link.name = name;
1033 devfs_msg->mdv_link.target = target;
1034 devfs_msg->mdv_link.mp = mp;
1035 devfs_msg_send(cmd, devfs_msg);
1039 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1040 * and calls the relevant worker functions. By using messages it's assured
1041 * that events occur in the correct order.
1044 devfs_msg_core(void *arg)
1049 lwkt_initport_thread(&devfs_msg_port, curthread);
1053 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1054 devfs_debug(DEVFS_DEBUG_DEBUG,
1055 "devfs_msg_core, new msg: %x\n",
1056 (unsigned int)msg->hdr.u.ms_result);
1057 devfs_msg_exec(msg);
1058 lwkt_replymsg(&msg->hdr, 0);
1065 devfs_msg_exec(devfs_msg_t msg)
1067 struct devfs_mnt_data *mnt;
1068 struct devfs_node *node;
1072 * Acquire the devfs lock to ensure safety of all called functions
1074 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1076 switch (msg->hdr.u.ms_result) {
1077 case DEVFS_DEVICE_CREATE:
1078 dev = msg->mdv_dev.dev;
1079 devfs_create_dev_worker(dev,
1082 msg->mdv_dev.perms);
1084 case DEVFS_DEVICE_DESTROY:
1085 dev = msg->mdv_dev.dev;
1086 devfs_destroy_dev_worker(dev);
1088 case DEVFS_DESTROY_SUBNAMES:
1089 devfs_destroy_subnames_worker(msg->mdv_load);
1091 case DEVFS_DESTROY_DEV_BY_OPS:
1092 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1093 msg->mdv_ops.minor);
1095 case DEVFS_CREATE_ALL_DEV:
1096 node = (struct devfs_node *)msg->mdv_load;
1097 devfs_create_all_dev_worker(node);
1099 case DEVFS_MOUNT_ADD:
1101 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1102 devfs_create_all_dev_worker(mnt->root_node);
1104 case DEVFS_MOUNT_DEL:
1106 TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1107 devfs_reaperp(mnt->root_node);
1108 if (mnt->leak_count) {
1109 devfs_debug(DEVFS_DEBUG_SHOW,
1110 "Leaked %d devfs_node elements!\n",
1114 case DEVFS_CHANDLER_ADD:
1115 devfs_chandler_add_worker(msg->mdv_chandler.name,
1116 msg->mdv_chandler.nhandler);
1118 case DEVFS_CHANDLER_DEL:
1119 devfs_chandler_del_worker(msg->mdv_chandler.name);
1121 case DEVFS_FIND_DEVICE_BY_NAME:
1122 devfs_find_device_by_name_worker(msg);
1124 case DEVFS_FIND_DEVICE_BY_UDEV:
1125 devfs_find_device_by_udev_worker(msg);
1127 case DEVFS_MAKE_ALIAS:
1128 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1130 case DEVFS_APPLY_RULES:
1131 devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1133 case DEVFS_RESET_RULES:
1134 devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1136 case DEVFS_SCAN_CALLBACK:
1137 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load);
1139 case DEVFS_CLR_SUBNAMES_FLAG:
1140 devfs_clr_subnames_flag_worker(msg->mdv_flags.name,
1141 msg->mdv_flags.flag);
1143 case DEVFS_DESTROY_SUBNAMES_WO_FLAG:
1144 devfs_destroy_subnames_without_flag_worker(msg->mdv_flags.name,
1145 msg->mdv_flags.flag);
1147 case DEVFS_INODE_TO_VNODE:
1148 msg->mdv_ino.vp = devfs_inode_to_vnode_worker(
1149 DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node,
1152 case DEVFS_TERMINATE_CORE:
1158 devfs_debug(DEVFS_DEBUG_WARNING,
1159 "devfs_msg_core: unknown message "
1160 "received at core\n");
1163 lockmgr(&devfs_lock, LK_RELEASE);
1167 * Worker function to insert a new dev into the dev list and initialize its
1168 * permissions. It also calls devfs_propagate_dev which in turn propagates
1169 * the change to all mount points.
1171 * The passed dev is already referenced. This reference is eaten by this
1172 * function and represents the dev's linkage into devfs_dev_list.
1175 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1181 dev->si_perms = perms;
1183 devfs_link_dev(dev);
1184 devfs_propagate_dev(dev, 1);
1190 * Worker function to delete a dev from the dev list and free the cdev.
1191 * It also calls devfs_propagate_dev which in turn propagates the change
1192 * to all mount points.
1195 devfs_destroy_dev_worker(cdev_t dev)
1200 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1202 error = devfs_unlink_dev(dev);
1203 devfs_propagate_dev(dev, 0);
1205 release_dev(dev); /* link ref */
1213 * Worker function to destroy all devices with a certain basename.
1214 * Calls devfs_destroy_dev_worker for the actual destruction.
1217 devfs_destroy_subnames_worker(char *name)
1220 size_t len = strlen(name);
1222 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1223 if ((!strncmp(dev->si_name, name, len)) &&
1224 (dev->si_name[len] != '\0')) {
1225 devfs_destroy_dev_worker(dev);
1232 devfs_clr_subnames_flag_worker(char *name, uint32_t flag)
1235 size_t len = strlen(name);
1237 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1238 if ((!strncmp(dev->si_name, name, len)) &&
1239 (dev->si_name[len] != '\0')) {
1240 dev->si_flags &= ~flag;
1248 devfs_destroy_subnames_without_flag_worker(char *name, uint32_t flag)
1251 size_t len = strlen(name);
1253 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1254 if ((!strncmp(dev->si_name, name, len)) &&
1255 (dev->si_name[len] != '\0')) {
1256 if (!(dev->si_flags & flag)) {
1257 devfs_destroy_dev_worker(dev);
1266 * Worker function that creates all device nodes on top of a devfs
1270 devfs_create_all_dev_worker(struct devfs_node *root)
1276 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1277 devfs_create_device_node(root, dev, NULL, NULL);
1284 * Worker function that destroys all devices that match a specific
1285 * dev_ops and/or minor. If minor is less than 0, it is not matched
1286 * against. It also propagates all changes.
1289 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1295 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1296 if (dev->si_ops != ops)
1298 if ((minor < 0) || (dev->si_uminor == minor)) {
1299 devfs_destroy_dev_worker(dev);
1307 * Worker function that registers a new clone handler in devfs.
1310 devfs_chandler_add_worker(char *name, d_clone_t *nhandler)
1312 struct devfs_clone_handler *chandler = NULL;
1313 u_char len = strlen(name);
1318 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1319 if (chandler->namlen != len)
1322 if (!memcmp(chandler->name, name, len)) {
1323 /* Clonable basename already exists */
1328 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1329 chandler->name = kmalloc(len+1, M_DEVFS, M_WAITOK);
1330 memcpy(chandler->name, name, len+1);
1331 chandler->namlen = len;
1332 chandler->nhandler = nhandler;
1334 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1339 * Worker function that removes a given clone handler from the
1340 * clone handler list.
1343 devfs_chandler_del_worker(char *name)
1345 struct devfs_clone_handler *chandler, *chandler2;
1346 u_char len = strlen(name);
1351 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1352 if (chandler->namlen != len)
1354 if (memcmp(chandler->name, name, len))
1357 TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1358 kfree(chandler->name, M_DEVFS);
1359 kfree(chandler, M_DEVFS);
1367 * Worker function that finds a given device name and changes
1368 * the message received accordingly so that when replied to,
1369 * the answer is returned to the caller.
1372 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1374 struct devfs_alias *alias;
1376 cdev_t found = NULL;
1378 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1379 if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1384 if (found == NULL) {
1385 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1386 if (strcmp(devfs_msg->mdv_name, alias->name) == 0) {
1387 found = alias->dev_target;
1392 devfs_msg->mdv_cdev = found;
1398 * Worker function that finds a given device udev and changes
1399 * the message received accordingly so that when replied to,
1400 * the answer is returned to the caller.
1403 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg)
1406 cdev_t found = NULL;
1408 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1409 if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1414 devfs_msg->mdv_cdev = found;
1420 * Worker function that inserts a given alias into the
1421 * alias list, and propagates the alias to all mount
1425 devfs_make_alias_worker(struct devfs_alias *alias)
1427 struct devfs_alias *alias2;
1428 size_t len = strlen(alias->name);
1431 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1432 if (len != alias2->namlen)
1435 if (!memcmp(alias->name, alias2->name, len)) {
1443 * The alias doesn't exist yet, so we add it to the alias list
1445 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1446 devfs_alias_propagate(alias);
1448 devfs_debug(DEVFS_DEBUG_WARNING,
1449 "Warning: duplicate devfs_make_alias for %s\n",
1451 kfree(alias->name, M_DEVFS);
1452 kfree(alias, M_DEVFS);
1459 * Function that removes and frees all aliases.
1462 devfs_alias_reap(void)
1464 struct devfs_alias *alias, *alias2;
1466 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1467 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1468 kfree(alias, M_DEVFS);
1474 * Function that removes an alias matching a specific cdev and frees
1478 devfs_alias_remove(cdev_t dev)
1480 struct devfs_alias *alias, *alias2;
1482 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1483 if (alias->dev_target == dev) {
1484 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1485 kfree(alias, M_DEVFS);
1492 * This function propagates a new alias to all mount points.
1495 devfs_alias_propagate(struct devfs_alias *alias)
1497 struct devfs_mnt_data *mnt;
1499 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1500 devfs_alias_apply(mnt->root_node, alias);
1506 * This function is a recursive function iterating through
1507 * all device nodes in the topology and, if applicable,
1508 * creating the relevant alias for a device node.
1511 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1513 struct devfs_node *node1, *node2;
1515 KKASSERT(alias != NULL);
1517 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1518 if (node->nchildren > 2) {
1519 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1520 devfs_alias_apply(node1, alias);
1524 if (node->d_dev == alias->dev_target)
1525 devfs_alias_create(alias->name, node);
1531 * This function checks if any alias possibly is applicable
1532 * to the given node. If so, the alias is created.
1535 devfs_alias_check_create(struct devfs_node *node)
1537 struct devfs_alias *alias;
1539 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1540 if (node->d_dev == alias->dev_target)
1541 devfs_alias_create(alias->name, node);
1547 * This function creates an alias with a given name
1548 * linking to a given devfs node. It also increments
1549 * the link count on the target node.
1552 devfs_alias_create(char *name_orig, struct devfs_node *target)
1554 struct mount *mp = target->mp;
1555 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1556 struct devfs_node *linknode;
1557 char *create_path = NULL;
1558 char *name, name_buf[PATH_MAX];
1560 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1562 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1565 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1568 if (devfs_find_device_node_by_name(parent, name)) {
1569 devfs_debug(DEVFS_DEBUG_WARNING,
1570 "Node already exists: %s "
1571 "(devfs_make_alias_worker)!\n",
1577 linknode = devfs_allocp(Plink, name, parent, mp, NULL);
1578 if (linknode == NULL)
1581 linknode->link_target = target;
1588 * This function is called by the core and handles mount point
1589 * strings. It either calls the relevant worker (devfs_apply_
1590 * reset_rules_worker) on all mountpoints or only a specific
1594 devfs_apply_reset_rules_caller(char *mountto, int apply)
1596 struct devfs_mnt_data *mnt;
1597 size_t len = strlen(mountto);
1599 if (mountto[0] == '*') {
1600 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1601 devfs_apply_reset_rules_worker(mnt->root_node, apply);
1604 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1605 if ((len != mnt->mntonnamelen))
1608 if (!memcmp(mnt->mp->mnt_stat.f_mntonname, mountto, len)) {
1609 devfs_apply_reset_rules_worker(mnt->root_node, apply);
1615 kfree(mountto, M_DEVFS);
1620 * This worker function applies or resets, depending on the arguments, a rule
1621 * to the whole given topology. *RECURSIVE*
1624 devfs_apply_reset_rules_worker(struct devfs_node *node, int apply)
1626 struct devfs_node *node1, *node2;
1628 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1629 if (node->nchildren > 2) {
1630 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1631 devfs_apply_reset_rules_worker(node1, apply);
1637 devfs_rule_check_apply(node);
1639 devfs_rule_reset_node(node);
1646 * This function calls a given callback function for
1647 * every dev node in the devfs dev list.
1650 devfs_scan_callback_worker(devfs_scan_t *callback)
1654 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1663 * This function tries to resolve a given directory, or if not
1664 * found and creation requested, creates the given directory.
1666 static struct devfs_node *
1667 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1668 size_t name_len, int create)
1670 struct devfs_node *node, *found = NULL;
1672 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1673 if (name_len != node->d_dir.d_namlen)
1676 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1682 if ((found == NULL) && (create)) {
1683 found = devfs_allocp(Pdir, dir_name, parent, parent->mp, NULL);
1690 * This function tries to resolve a complete path. If creation is requested,
1691 * if a given part of the path cannot be resolved (because it doesn't exist),
1695 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1697 struct devfs_node *node = parent;
1706 for (; *path != '\0' ; path++) {
1711 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1718 return devfs_resolve_or_create_dir(node, buf, idx, create);
1722 * Takes a full path and strips it into a directory path and a name.
1723 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
1724 * requires a working buffer with enough size to keep the whole
1728 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
1732 size_t len = strlen(fullpath) + 1;
1735 KKASSERT((fullpath != NULL) && (buf != NULL));
1736 KKASSERT((pathp != NULL) && (namep != NULL));
1738 memcpy(buf, fullpath, len);
1740 for (i = len-1; i>= 0; i--) {
1741 if (buf[i] == '/') {
1761 * This function creates a new devfs node for a given device. It can
1762 * handle a complete path as device name, and accordingly creates
1763 * the path and the final device node.
1765 * The reference count on the passed dev remains unchanged.
1768 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
1769 char *dev_name, char *path_fmt, ...)
1771 struct devfs_node *parent, *node = NULL;
1773 char *name, name_buf[PATH_MAX];
1777 char *create_path = NULL;
1778 char *names = "pqrsPQRS";
1780 if (path_fmt != NULL) {
1781 path = kmalloc(PATH_MAX+1, M_DEVFS, M_WAITOK);
1783 __va_start(ap, path_fmt);
1784 i = kvcprintf(path_fmt, NULL, path, 10, ap);
1789 parent = devfs_resolve_or_create_path(root, path, 1);
1792 devfs_resolve_name_path(
1793 ((dev_name == NULL) && (dev))?(dev->si_name):(dev_name),
1794 name_buf, &create_path, &name);
1797 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1800 if (devfs_find_device_node_by_name(parent, name)) {
1801 devfs_debug(DEVFS_DEBUG_WARNING, "devfs_create_device_node: "
1802 "DEVICE %s ALREADY EXISTS!!! Ignoring creation request.\n", name);
1806 node = devfs_allocp(Pdev, name, parent, parent->mp, dev);
1810 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
1813 if ((dev) && (strlen(dev->si_name) >= 4) &&
1814 (!memcmp(dev->si_name, "ptm/", 4))) {
1815 node->parent->flags |= DEVFS_HIDDEN;
1816 node->flags |= DEVFS_HIDDEN;
1821 * Ugly pty magic, to tag pty devices as such and hide them if needed.
1823 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
1824 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1826 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
1828 for (i = 0; i < strlen(names); i++) {
1829 if (name[3] == names[i]) {
1835 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1839 if (path_fmt != NULL)
1840 kfree(path, M_DEVFS);
1846 * This function finds a given device node in the topology with a given
1850 devfs_find_device_node(struct devfs_node *node, cdev_t target)
1852 struct devfs_node *node1, *node2, *found = NULL;
1854 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1855 if (node->nchildren > 2) {
1856 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1857 if ((found = devfs_find_device_node(node1, target)))
1861 } else if (node->node_type == Pdev) {
1862 if (node->d_dev == target)
1870 * This function finds a device node in the topology by its
1871 * name and returns it.
1874 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
1876 struct devfs_node *node, *found = NULL;
1877 size_t len = strlen(target);
1879 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1880 if (len != node->d_dir.d_namlen)
1883 if (!memcmp(node->d_dir.d_name, target, len)) {
1892 static struct vnode*
1893 devfs_inode_to_vnode_worker(struct devfs_node *node, ino_t target)
1895 struct devfs_node *node1, *node2;
1898 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1899 if (node->nchildren > 2) {
1900 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1901 if ((vp = devfs_inode_to_vnode_worker(node1, target)))
1907 if (node->d_dir.d_ino == target) {
1910 vget(vp, LK_EXCLUSIVE | LK_RETRY);
1913 devfs_allocv(&vp, node);
1923 * This function takes a cdev and removes its devfs node in the
1924 * given topology. The cdev remains intact.
1927 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
1929 struct devfs_node *node, *parent;
1930 char *name, name_buf[PATH_MAX];
1931 char *create_path = NULL;
1935 memcpy(name_buf, target->si_name, strlen(target->si_name)+1);
1937 devfs_resolve_name_path(target->si_name, name_buf, &create_path, &name);
1940 parent = devfs_resolve_or_create_path(root, create_path, 0);
1947 node = devfs_find_device_node_by_name(parent, name);
1956 * Just set perms and ownership for given node.
1959 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid,
1960 u_short mode, u_long flags)
1970 * Propagates a device attach/detach to all mount
1971 * points. Also takes care of automatic alias removal
1972 * for a deleted cdev.
1975 devfs_propagate_dev(cdev_t dev, int attach)
1977 struct devfs_mnt_data *mnt;
1979 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1981 /* Device is being attached */
1982 devfs_create_device_node(mnt->root_node, dev,
1985 /* Device is being detached */
1986 devfs_alias_remove(dev);
1987 devfs_destroy_device_node(mnt->root_node, dev);
1994 * devfs_node_to_path takes a node and a buffer of a size of
1995 * at least PATH_MAX, resolves the full path from the root
1996 * node and writes it in a humanly-readable format into the
1998 * If DEVFS_STASH_DEPTH is less than the directory level up
1999 * to the root node, only the last DEVFS_STASH_DEPTH levels
2000 * of the path are resolved.
2003 devfs_node_to_path(struct devfs_node *node, char *buffer)
2005 #define DEVFS_STASH_DEPTH 32
2006 struct devfs_node *node_stash[DEVFS_STASH_DEPTH];
2008 memset(buffer, 0, PATH_MAX);
2010 for (i = 0; (i < DEVFS_STASH_DEPTH) && (node->node_type != Proot); i++) {
2011 node_stash[i] = node;
2012 node = node->parent;
2016 for (offset = 0; i >= 0; i--) {
2017 memcpy(buffer+offset, node_stash[i]->d_dir.d_name,
2018 node_stash[i]->d_dir.d_namlen);
2019 offset += node_stash[i]->d_dir.d_namlen;
2021 *(buffer+offset) = '/';
2025 #undef DEVFS_STASH_DEPTH
2030 * devfs_clone either returns a basename from a complete name by
2031 * returning the length of the name without trailing digits, or,
2032 * if clone != 0, calls the device's clone handler to get a new
2033 * device, which in turn is returned in devp.
2036 devfs_clone(char *name, size_t *namlenp, cdev_t *devp, int clone,
2041 size_t len = *namlenp;
2043 struct devfs_clone_handler *chandler;
2044 struct dev_clone_args ap;
2047 for (; (len > 0) && (DEVFS_ISDIGIT(name[len-1])); len--);
2050 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
2051 if ((chandler->namlen == len) &&
2052 (!memcmp(chandler->name, name, len)) &&
2053 (chandler->nhandler)) {
2059 error = (chandler->nhandler)(&ap);
2076 * Registers a new orphan in the orphan list.
2079 devfs_tracer_add_orphan(struct devfs_node *node)
2081 struct devfs_orphan *orphan;
2084 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2085 orphan->node = node;
2087 KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2088 node->flags |= DEVFS_ORPHANED;
2089 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2093 * Removes an orphan from the orphan list.
2096 devfs_tracer_del_orphan(struct devfs_node *node)
2098 struct devfs_orphan *orphan;
2102 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) {
2103 if (orphan->node == node) {
2104 node->flags &= ~DEVFS_ORPHANED;
2105 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2106 kfree(orphan, M_DEVFS);
2113 * Counts the orphans in the orphan list, and if cleanup
2114 * is specified, also frees the orphan and removes it from
2118 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2120 struct devfs_orphan *orphan, *orphan2;
2123 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) {
2126 * If we are instructed to clean up, we do so.
2129 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2130 orphan->node->flags &= ~DEVFS_ORPHANED;
2131 devfs_freep(orphan->node);
2132 kfree(orphan, M_DEVFS);
2140 * Fetch an ino_t from the global d_ino by increasing it
2144 devfs_fetch_ino(void)
2148 spin_lock_wr(&ino_lock);
2150 spin_unlock_wr(&ino_lock);
2156 * Allocates a new cdev and initializes it's most basic
2160 devfs_new_cdev(struct dev_ops *ops, int minor)
2162 cdev_t dev = sysref_alloc(&cdev_sysref_class);
2163 sysref_activate(&dev->si_sysref);
2165 memset(dev, 0, offsetof(struct cdev, si_sysref));
2170 dev->si_drv1 = NULL;
2171 dev->si_drv2 = NULL;
2172 dev->si_lastread = 0; /* time_second */
2173 dev->si_lastwrite = 0; /* time_second */
2178 dev->si_uminor = minor;
2179 dev->si_inode = makeudev(devfs_reference_ops(ops), minor);
2185 devfs_cdev_terminate(cdev_t dev)
2189 /* Check if it is locked already. if not, we acquire the devfs lock */
2190 if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) {
2191 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2195 /* Propagate destruction, just in case */
2196 devfs_propagate_dev(dev, 0);
2198 /* If we acquired the lock, we also get rid of it */
2200 lockmgr(&devfs_lock, LK_RELEASE);
2202 devfs_release_ops(dev->si_ops);
2204 /* Finally destroy the device */
2205 sysref_put(&dev->si_sysref);
2209 * Links a given cdev into the dev list.
2212 devfs_link_dev(cdev_t dev)
2214 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2215 dev->si_flags |= SI_DEVFS_LINKED;
2216 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2222 * Removes a given cdev from the dev list. The caller is responsible for
2223 * releasing the reference on the device associated with the linkage.
2225 * Returns EALREADY if the dev has already been unlinked.
2228 devfs_unlink_dev(cdev_t dev)
2230 if ((dev->si_flags & SI_DEVFS_LINKED)) {
2231 TAILQ_REMOVE(&devfs_dev_list, dev, link);
2232 dev->si_flags &= ~SI_DEVFS_LINKED;
2239 devfs_node_is_accessible(struct devfs_node *node)
2241 if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2248 devfs_reference_ops(struct dev_ops *ops)
2252 if (ops->head.refs == 0) {
2253 ops->head.id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255);
2254 if (ops->head.id == -1) {
2255 /* Ran out of unique ids */
2256 devfs_debug(DEVFS_DEBUG_WARNING,
2257 "devfs_reference_ops: WARNING: ran out of unique ids\n");
2260 unit = ops->head.id;
2267 devfs_release_ops(struct dev_ops *ops)
2271 if (ops->head.refs == 0) {
2272 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), ops->head.id);
2281 msg = devfs_msg_get();
2282 msg = devfs_msg_send_sync(DEVFS_SYNC, msg);
2287 * Called on init of devfs; creates the objcaches and
2288 * spawns off the devfs core thread. Also initializes
2294 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2295 /* Create objcaches for nodes, msgs and devs */
2296 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2298 objcache_malloc_alloc,
2299 objcache_malloc_free,
2300 &devfs_node_malloc_args );
2302 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2304 objcache_malloc_alloc,
2305 objcache_malloc_free,
2306 &devfs_msg_malloc_args );
2308 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2310 objcache_malloc_alloc,
2311 objcache_malloc_free,
2312 &devfs_dev_malloc_args );
2314 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2316 /* Initialize the reply-only port which acts as a message drain */
2317 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2319 /* Initialize *THE* devfs lock */
2320 lockinit(&devfs_lock, "devfs_core lock", 0, 0);
2323 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2324 0, 0, "devfs_msg_core");
2326 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2328 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2332 * Called on unload of devfs; takes care of destroying the core
2333 * and the objcaches. Also removes aliases that are no longer needed.
2338 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2340 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2342 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2343 tsleep(td_core/*devfs_id*/, 0, "devfsc", 10000);
2345 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2347 /* Destroy the objcaches */
2348 objcache_destroy(devfs_msg_cache);
2349 objcache_destroy(devfs_node_cache);
2350 objcache_destroy(devfs_dev_cache);
2356 * This is a sysctl handler to assist userland devname(3) to
2357 * find the device name for a given udev.
2360 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2367 if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t))))
2370 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev);
2375 if ((found = devfs_find_device_by_udev(udev)) == NULL)
2378 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2382 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
2383 NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)");
2385 static SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2386 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2387 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable,
2388 0, "Enable DevFS debugging");
2390 SYSINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST,
2392 SYSUNINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY,
2393 devfs_uninit, NULL);