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 = TAILQ_HEAD_INITIALIZER(devfs_dev_list);
84 static struct devfs_mnt_head devfs_mnt_list = TAILQ_HEAD_INITIALIZER(devfs_mnt_list);
85 static struct devfs_chandler_head devfs_chandler_list = TAILQ_HEAD_INITIALIZER(devfs_chandler_list);
86 static struct devfs_alias_head devfs_alias_list = TAILQ_HEAD_INITIALIZER(devfs_alias_list);
88 struct lock devfs_lock;
89 static struct lwkt_port devfs_dispose_port;
90 static struct lwkt_port devfs_msg_port;
91 static struct thread *td_core;
93 static ino_t d_ino = 0;
94 static __uint32_t msg_id = 0;
95 static struct spinlock ino_lock;
96 static int devfs_debug_enable = 0;
98 static ino_t devfs_fetch_ino(void);
99 static int devfs_gc_dirs(struct devfs_node *);
100 static int devfs_gc_links(struct devfs_node *, struct devfs_node *, size_t);
101 static int devfs_create_all_dev_worker(struct devfs_node *);
102 static int devfs_create_dev_worker(cdev_t, uid_t, gid_t, int);
103 static int devfs_destroy_dev_worker(cdev_t);
104 static int devfs_destroy_subnames_worker(char *);
105 static int devfs_destroy_dev_by_ops_worker(struct dev_ops *, int);
106 static int devfs_propagate_dev(cdev_t, int);
107 static int devfs_unlink_dev(cdev_t dev);
109 static int devfs_chandler_add_worker(char *, d_clone_t *);
110 static int devfs_chandler_del_worker(char *);
112 static void devfs_msg_autofree_reply(lwkt_port_t, lwkt_msg_t);
113 static void devfs_msg_core(void *);
115 static int devfs_find_device_by_name_worker(devfs_msg_t);
116 static int devfs_find_device_by_udev_worker(devfs_msg_t);
118 static int devfs_apply_reset_rules_caller(char *, int);
119 static int devfs_apply_reset_rules_worker(struct devfs_node *, int);
121 static int devfs_scan_callback_worker(devfs_scan_t *);
123 static struct devfs_node *devfs_resolve_or_create_dir(struct devfs_node *, char *, size_t, int);
125 static int devfs_make_alias_worker(struct devfs_alias *);
126 static int devfs_alias_remove(cdev_t);
127 static int devfs_alias_reap(void);
128 static int devfs_alias_propagate(struct devfs_alias *);
129 static int devfs_alias_apply(struct devfs_node *, struct devfs_alias *);
130 static int devfs_alias_check_create(struct devfs_node *);
132 static int devfs_clr_subnames_flag_worker(char *, uint32_t);
133 static int devfs_destroy_subnames_without_flag_worker(char *, uint32_t);
136 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function using kvprintf
139 devfs_debug(int level, char *fmt, ...)
144 if (level <= devfs_debug_enable)
152 * devfs_allocp() Allocates a new devfs node with the specified
153 * parameters. The node is also automatically linked into the topology
154 * if a parent is specified. It also calls the rule and alias stuff to
155 * be applied on the new node
158 devfs_allocp(devfs_nodetype devfsnodetype, char *name,
159 struct devfs_node *parent, struct mount *mp, cdev_t dev)
161 struct devfs_node *node = NULL;
162 size_t namlen = strlen(name);
163 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocp -1- for %s\n", name?name:"NULL");
165 node = objcache_get(devfs_node_cache, M_WAITOK);
167 bzero(node, sizeof(*node));
169 atomic_add_int(&(DEVFS_MNTDATA(mp)->leak_count), 1);
174 node->d_dir.d_ino = devfs_fetch_ino();
175 node->cookie_jar = 2; /* Leave 0 and 1 for '.' and '..', respectively */
177 /* Access Control members */
178 node->mode = DEVFS_DEFAULT_MODE; /* files access mode and type */
179 node->uid = DEVFS_DEFAULT_UID; /* owner user id */
180 node->gid = DEVFS_DEFAULT_GID; /* owner group id */
182 switch (devfsnodetype) {
184 /* Ensure that we don't recycle the root vnode */
185 node->flags |= DEVFS_NODE_LINKED;
187 TAILQ_INIT(DEVFS_DENODE_HEAD(node));
188 node->d_dir.d_type = DT_DIR;
193 node->d_dir.d_type = DT_LNK;
197 node->d_dir.d_type = DT_REG;
202 node->d_dir.d_type = DT_CHR;
204 node->d_dir.d_ino = dev->si_inode;
206 node->mode = dev->si_perms; /* files access mode and type */
207 node->uid = dev->si_uid; /* owner user id */
208 node->gid = dev->si_gid; /* owner group id */
210 devfs_alias_check_create(node);
215 panic("devfs_allocp: unknown node type");
219 node->node_type = devfsnodetype;
221 /* Init the dirent structure of each devfs vnode */
222 KKASSERT(namlen < 256);
224 node->d_dir.d_namlen = namlen;
225 node->d_dir.d_name = kmalloc(namlen+1, M_DEVFS, M_WAITOK);
226 memcpy(node->d_dir.d_name, name, namlen);
227 node->d_dir.d_name[namlen] = '\0';
229 /* Initialize the parent node element */
230 node->parent = parent;
233 devfs_rule_check_apply(node);
236 nanotime(&node->atime);
237 node->mtime = node->ctime = node->atime;
240 * Associate with parent as last step, clean out namecache
243 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocp: about to insert node\n");
244 if ((parent != NULL) &&
245 ((parent->node_type == Proot) || (parent->node_type == Pdir))) {
246 devfs_debug(DEVFS_DEBUG_DEBUG,
247 "devfs_allocp: node inserted %p\n",
250 node->cookie = parent->cookie_jar++;
251 node->flags |= DEVFS_NODE_LINKED;
252 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent), node, link);
254 /* This forces negative namecache lookups to clear */
255 ++mp->mnt_namecache_gen;
258 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocp -end:2-\n");
263 * devfs_allocv() allocates a new vnode based on a devfs node.
266 devfs_allocv(struct vnode **vpp, struct devfs_node *node)
273 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -1-\n");
276 while ((vp = node->v_node) != NULL) {
277 error = vget(vp, LK_EXCLUSIVE);
278 if (error != ENOENT) {
280 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv, code path 2...\n");
284 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -3-\n");
286 if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0)
291 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -4-\n");
293 if (node->v_node != NULL) {
301 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -5-\n");
303 switch (node->node_type) {
320 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -6-\n");
321 KKASSERT(node->d_dev);
324 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -7-\n");
325 vp->v_uminor = node->d_dev->si_uminor;
328 vp->v_rdev = node->d_dev;
330 v_associate_rdev(vp, node->d_dev);
331 vp->v_ops = &node->mp->mnt_vn_spec_ops;
332 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -8-\n");
334 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv: type is Pdev but d_dev is not set!!!!\n");
336 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -9-\n");
340 panic("devfs_allocv: unknown node type");
344 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -10-\n");
345 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -end:11-\n");
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 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocvp -1-\n");
360 node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
361 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocvp -2-\n");
363 devfs_allocv(vpp, node);
367 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocvp -end:3-\n");
373 * Destroy the devfs_node. The node must be unlinked from the topology.
375 * This function will also destroy any vnode association with the node
378 * The cdev_t itself remains intact.
381 devfs_freep(struct devfs_node *node)
386 KKASSERT(((node->flags & DEVFS_NODE_LINKED) == 0) ||
387 (node->node_type == Proot));
388 KKASSERT((node->flags & DEVFS_DESTROYED) == 0);
390 atomic_subtract_int(&(DEVFS_MNTDATA(node->mp)->leak_count), 1);
391 if (node->symlink_name) {
392 kfree(node->symlink_name, M_DEVFS);
393 node->symlink_name = NULL;
397 * Remove the node from the orphan list if it is still on it.
399 if (node->flags & DEVFS_ORPHANED)
400 devfs_tracer_del_orphan(node);
403 * Disassociate the vnode from the node. This also prevents the
404 * vnode's reclaim code from double-freeing the node.
406 if ((vp = node->v_node) != NULL) {
414 if (node->d_dir.d_name)
415 kfree(node->d_dir.d_name, M_DEVFS);
416 node->flags |= DEVFS_DESTROYED;
418 objcache_put(devfs_node_cache, node);
424 * Unlink the devfs node from the topology and add it to the orphan list.
425 * The node will later be destroyed by freep.
427 * Any vnode association, including the v_rdev and v_data, remains intact
431 devfs_unlinkp(struct devfs_node *node)
433 struct devfs_node *parent;
436 devfs_tracer_add_orphan(node);
437 devfs_debug(DEVFS_DEBUG_DEBUG,
438 "devfs_unlinkp for %s\n", node->d_dir.d_name);
439 parent = node->parent;
442 * If the parent is known we can unlink the node out of the topology
445 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
447 KKASSERT((parent->nchildren >= 0));
448 node->flags &= ~DEVFS_NODE_LINKED;
455 * devfs_reaperp() is a recursive function that iterates through all the
456 * topology, unlinking and freeing all devfs nodes.
459 devfs_reaperp(struct devfs_node *node)
461 struct devfs_node *node1, *node2;
463 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
464 devfs_debug(DEVFS_DEBUG_DEBUG,
465 "This node is Pdir or Proot; has %d children\n",
467 if (node->nchildren > 2) {
468 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
470 devfs_reaperp(node1);
481 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
482 * freeing a node, but also removes empty directories and links that link
483 * via devfs auto-link mechanism to the node being deleted.
486 devfs_gc(struct devfs_node *node)
488 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
490 devfs_gc_links(root_node, node, node->nlinks);
492 devfs_gc_dirs(root_node);
500 * devfs_gc_dirs() is a helper function for devfs_gc, unlinking and freeing
504 devfs_gc_dirs(struct devfs_node *node)
506 struct devfs_node *node1, *node2;
508 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
509 devfs_debug(DEVFS_DEBUG_DEBUG,
510 "This node is Pdir or Proot; has %d children\n",
512 if (node->nchildren > 2) {
513 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
515 devfs_gc_dirs(node1);
519 if (node->nchildren == 2) {
520 devfs_debug(DEVFS_DEBUG_DEBUG,
521 "This node is called %s and it is empty\n",
532 * devfs_gc_links() is a helper function for devfs_gc, unlinking and freeing
533 * eauto-linked nodes linking to the node being deleted.
536 devfs_gc_links(struct devfs_node *node, struct devfs_node *target,
539 struct devfs_node *node1, *node2;
542 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
543 devfs_debug(DEVFS_DEBUG_DEBUG, "This node is Pdir or Proot; has %d children\n", node->nchildren);
544 if (node->nchildren > 2) {
545 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
546 nlinks = devfs_gc_links(node1, target, nlinks);
549 } else if (node->link_target == target) {
556 KKASSERT(nlinks >= 0);
562 * devfs_create_dev() is the asynchronous entry point for device creation.
563 * It just sends a message with the relevant details to the devfs core.
565 * This function will reference the passed device. The reference is owned
566 * by devfs and represents all of the device's node associations.
569 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
572 devfs_debug(DEVFS_DEBUG_DEBUG,
573 "devfs_create_dev -1-, name: %s (%p)\n",
576 id = devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
577 devfs_debug(DEVFS_DEBUG_DEBUG,
578 "devfs_create_dev -end:2- (unique id: %x) / (%p)\n",
584 * devfs_destroy_dev() is the asynchronous entry point for device destruction.
585 * It just sends a message with the relevant details to the devfs core.
588 devfs_destroy_dev(cdev_t dev)
590 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
595 * devfs_mount_add() is the synchronous entry point for adding a new devfs
596 * mount. It sends a synchronous message with the relevant details to the
600 devfs_mount_add(struct devfs_mnt_data *mnt)
604 msg = devfs_msg_get();
606 msg = devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
613 * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
614 * It sends a synchronous message with the relevant details to the devfs core.
617 devfs_mount_del(struct devfs_mnt_data *mnt)
621 msg = devfs_msg_get();
623 msg = devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
630 * devfs_destroy_subnames() is the synchronous entry point for device destruction
631 * by subname. It just sends a message with the relevant details to the devfs core.
634 devfs_destroy_subnames(char *name)
638 msg = devfs_msg_get();
639 msg->mdv_load = name;
640 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES, msg);
646 devfs_clr_subnames_flag(char *name, uint32_t flag)
650 msg = devfs_msg_get();
651 msg->mdv_flags.name = name;
652 msg->mdv_flags.flag = flag;
653 msg = devfs_msg_send_sync(DEVFS_CLR_SUBNAMES_FLAG, msg);
660 devfs_destroy_subnames_without_flag(char *name, uint32_t flag)
664 msg = devfs_msg_get();
665 msg->mdv_flags.name = name;
666 msg->mdv_flags.flag = flag;
667 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES_WO_FLAG, msg);
674 * devfs_create_all_dev is the asynchronous entry point to trigger device
675 * node creation. It just sends a message with the relevant details to
679 devfs_create_all_dev(struct devfs_node *root)
681 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
686 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
687 * devices with a specific set of dev_ops and minor. It just sends a
688 * message with the relevant details to the devfs core.
691 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
693 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
698 * devfs_clone_handler_add is the synchronous entry point to add a new
699 * clone handler. It just sends a message with the relevant details to
703 devfs_clone_handler_add(char *name, d_clone_t *nhandler)
707 msg = devfs_msg_get();
708 msg->mdv_chandler.name = name;
709 msg->mdv_chandler.nhandler = nhandler;
710 msg = devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg);
716 * devfs_clone_handler_del is the synchronous entry point to remove a
717 * clone handler. It just sends a message with the relevant details to
721 devfs_clone_handler_del(char *name)
725 msg = devfs_msg_get();
726 msg->mdv_chandler.name = name;
727 msg->mdv_chandler.nhandler = NULL;
728 msg = devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg);
734 * devfs_find_device_by_name is the synchronous entry point to find a
735 * device given its name. It sends a synchronous message with the
736 * relevant details to the devfs core and returns the answer.
739 devfs_find_device_by_name(const char *fmt, ...)
743 char target[PATH_MAX+1];
752 i = kvcprintf(fmt, NULL, target, 10, ap);
757 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_find_device_by_name: %s -1-\n", target);
758 msg = devfs_msg_get();
759 msg->mdv_name = target;
760 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
761 found = msg->mdv_cdev;
764 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_find_device_by_name found? %s -end:2-\n", (found)?"YES":"NO");
769 * devfs_find_device_by_udev is the synchronous entry point to find a
770 * device given its udev number. It sends a synchronous message with
771 * the relevant details to the devfs core and returns the answer.
774 devfs_find_device_by_udev(udev_t udev)
779 msg = devfs_msg_get();
780 msg->mdv_udev = udev;
781 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg);
782 found = msg->mdv_cdev;
785 devfs_debug(DEVFS_DEBUG_DEBUG,
786 "devfs_find_device_by_udev found? %s -end:3-\n",
787 ((found) ? found->si_name:"NO"));
792 * devfs_make_alias is the asynchronous entry point to register an alias
793 * for a device. It just sends a message with the relevant details to the
797 devfs_make_alias(char *name, cdev_t dev_target)
799 struct devfs_alias *alias;
801 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
802 memcpy(alias->name, name, strlen(name) + 1);
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)
820 namelen = strlen(mntto) + 1;
822 new_name = kmalloc(namelen, M_DEVFS, M_WAITOK);
824 memcpy(new_name, mntto, namelen);
826 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
831 * devfs_reset_rules is the asynchronous entry point to trigger reset of all rules.
832 * It just sends a message with the relevant details to the devfs core.
835 devfs_reset_rules(char *mntto)
840 namelen = strlen(mntto) + 1;
842 new_name = kmalloc(namelen, M_DEVFS, M_WAITOK);
844 memcpy(new_name, mntto, namelen);
846 devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
852 * devfs_scan_callback is the asynchronous entry point to call a callback
854 * It just sends a message with the relevant details to the devfs core.
857 devfs_scan_callback(devfs_scan_t *callback)
861 /* Make sure that function pointers have the size of a generic pointer (innecessary) */
862 KKASSERT(sizeof(callback) == sizeof(void *));
864 msg = devfs_msg_get();
865 msg->mdv_load = callback;
866 msg = devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
874 * Acts as a message drain. Any message that is replied to here gets destroyed and
878 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
880 devfs_msg_put((devfs_msg_t)msg);
884 * devfs_msg_get allocates a new devfs msg and returns it.
889 return objcache_get(devfs_msg_cache, M_WAITOK);
893 * devfs_msg_put deallocates a given devfs msg.
896 devfs_msg_put(devfs_msg_t msg)
898 objcache_put(devfs_msg_cache, msg);
903 * devfs_msg_send is the generic asynchronous message sending facility
904 * for devfs. By default the reply port is the automatic disposal port.
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;
914 devfs_msg->id = atomic_fetchadd_int(&msg_id, 1);
916 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
918 return devfs_msg->id;
922 * devfs_msg_send_sync is the generic synchronous message sending
923 * facility for devfs. It initializes a local reply port and waits
924 * for the core's answer. This answer is then returned.
927 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
929 struct lwkt_port rep_port;
930 devfs_msg_t msg_incoming;
931 lwkt_port_t port = &devfs_msg_port;
933 lwkt_initport_thread(&rep_port, curthread);
934 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
936 devfs_msg->hdr.u.ms_result = cmd;
937 devfs_msg->id = atomic_fetchadd_int(&msg_id, 1);
939 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
940 msg_incoming = lwkt_waitport(&rep_port, 0);
946 * sends a message with a generic argument.
949 devfs_msg_send_generic(uint32_t cmd, void *load)
951 devfs_msg_t devfs_msg = devfs_msg_get();
952 devfs_msg->mdv_load = load;
954 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_send_generic -1- (%p)\n", load);
956 return devfs_msg_send(cmd, devfs_msg);
960 * sends a message with a name argument.
963 devfs_msg_send_name(uint32_t cmd, char *name)
965 devfs_msg_t devfs_msg = devfs_msg_get();
966 devfs_msg->mdv_name = name;
968 return devfs_msg_send(cmd, devfs_msg);
972 * sends a message with a mount argument.
975 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
977 devfs_msg_t devfs_msg = devfs_msg_get();
978 devfs_msg->mdv_mnt = mnt;
980 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_send_mp -1- (%p)\n", mnt);
982 return devfs_msg_send(cmd, devfs_msg);
986 * sends a message with an ops argument.
989 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
991 devfs_msg_t devfs_msg = devfs_msg_get();
992 devfs_msg->mdv_ops.ops = ops;
993 devfs_msg->mdv_ops.minor = minor;
995 return devfs_msg_send(cmd, devfs_msg);
999 * sends a message with a clone handler argument.
1002 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
1004 devfs_msg_t devfs_msg = devfs_msg_get();
1005 devfs_msg->mdv_chandler.name = name;
1006 devfs_msg->mdv_chandler.nhandler = handler;
1008 return devfs_msg_send(cmd, devfs_msg);
1012 * sends a message with a device argument.
1015 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1017 devfs_msg_t devfs_msg = devfs_msg_get();
1018 devfs_msg->mdv_dev.dev = dev;
1019 devfs_msg->mdv_dev.uid = uid;
1020 devfs_msg->mdv_dev.gid = gid;
1021 devfs_msg->mdv_dev.perms = perms;
1023 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_send_dev -1- (%p)\n", dev);
1025 return devfs_msg_send(cmd, devfs_msg);
1029 * sends a message with a link argument.
1032 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1034 devfs_msg_t devfs_msg = devfs_msg_get();
1035 devfs_msg->mdv_link.name = name;
1036 devfs_msg->mdv_link.target = target;
1037 devfs_msg->mdv_link.mp = mp;
1040 return devfs_msg_send(cmd, devfs_msg);
1044 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1045 * and calls the relevant worker functions. By using messages it's assured
1046 * that events occur in the correct order.
1049 devfs_msg_core(void *arg)
1054 struct devfs_mnt_data *mnt;
1055 struct devfs_node *node;
1057 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_core -1-\n");
1058 lwkt_initport_thread(&devfs_msg_port, curthread);
1059 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_core -2-\n");
1060 wakeup(td_core/*devfs_id*/);
1061 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_core -3-\n");
1064 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_core -loop:4-\n");
1065 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1066 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_core, new msg: %x (unique id: %x)\n", (unsigned int)msg->hdr.u.ms_result, msg->id);
1067 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1068 switch (msg->hdr.u.ms_result) {
1070 case DEVFS_DEVICE_CREATE:
1071 dev = msg->mdv_dev.dev;
1072 devfs_debug(DEVFS_DEBUG_DEBUG,
1073 "devfs_msg_core device create msg %s(%p)\n",
1075 devfs_create_dev_worker(dev,
1078 msg->mdv_dev.perms);
1081 case DEVFS_DEVICE_DESTROY:
1082 devfs_debug(DEVFS_DEBUG_DEBUG,
1083 "devfs_msg_core device destroy msg\n");
1084 dev = msg->mdv_dev.dev;
1085 devfs_destroy_dev_worker(dev);
1088 case DEVFS_DESTROY_SUBNAMES:
1089 devfs_destroy_subnames_worker(msg->mdv_load);
1092 case DEVFS_DESTROY_DEV_BY_OPS:
1093 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1094 msg->mdv_ops.minor);
1097 case DEVFS_CREATE_ALL_DEV:
1098 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_core device create ALL msg\n");
1099 node = (struct devfs_node *)msg->mdv_load;
1100 devfs_create_all_dev_worker(node);
1103 case DEVFS_MOUNT_ADD:
1105 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1106 devfs_create_all_dev_worker(mnt->root_node);
1109 case DEVFS_MOUNT_DEL:
1111 TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1112 devfs_reaperp(mnt->root_node);
1113 if (mnt->leak_count) {
1114 devfs_debug(DEVFS_DEBUG_SHOW,
1115 "Leaked %d devfs_node elements!\n",
1120 case DEVFS_CHANDLER_ADD:
1121 devfs_chandler_add_worker(msg->mdv_chandler.name, msg->mdv_chandler.nhandler);
1124 case DEVFS_CHANDLER_DEL:
1125 devfs_chandler_del_worker(msg->mdv_chandler.name);
1128 case DEVFS_FIND_DEVICE_BY_NAME:
1129 devfs_find_device_by_name_worker(msg);
1132 case DEVFS_FIND_DEVICE_BY_UDEV:
1133 devfs_find_device_by_udev_worker(msg);
1136 case DEVFS_MAKE_ALIAS:
1137 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1140 case DEVFS_APPLY_RULES:
1141 devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1144 case DEVFS_RESET_RULES:
1145 devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1148 case DEVFS_SCAN_CALLBACK:
1149 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load);
1152 case DEVFS_CLR_SUBNAMES_FLAG:
1153 devfs_clr_subnames_flag_worker(msg->mdv_flags.name,
1154 msg->mdv_flags.flag);
1157 case DEVFS_DESTROY_SUBNAMES_WO_FLAG:
1158 devfs_destroy_subnames_without_flag_worker(msg->mdv_flags.name,
1159 msg->mdv_flags.flag);
1162 case DEVFS_TERMINATE_CORE:
1168 devfs_debug(DEVFS_DEBUG_DEBUG,
1169 "devfs_msg_core: unknown message "
1170 "received at core\n");
1173 lockmgr(&devfs_lock, LK_RELEASE);
1175 lwkt_replymsg((lwkt_msg_t)msg, 0);
1177 wakeup(td_core/*devfs_id*/);
1182 * Worker function to insert a new dev into the dev list and initialize its
1183 * permissions. It also calls devfs_propagate_dev which in turn propagates
1184 * the change to all mount points.
1186 * The passed dev is already referenced. This reference is eaten by this
1187 * function and represents the dev's linkage into devfs_dev_list.
1190 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1193 devfs_debug(DEVFS_DEBUG_DEBUG,
1194 "devfs_create_dev_worker -1- -%s- (%p)\n",
1199 dev->si_perms = perms;
1201 devfs_link_dev(dev);
1202 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_dev_worker -2-\n");
1203 devfs_propagate_dev(dev, 1);
1205 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_dev_worker -end:3-\n");
1210 * Worker function to delete a dev from the dev list and free the cdev.
1211 * It also calls devfs_propagate_dev which in turn propagates the change
1212 * to all mount points.
1215 devfs_destroy_dev_worker(cdev_t dev)
1220 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1222 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_destroy_dev_worker -1- %s\n", dev->si_name);
1223 error = devfs_unlink_dev(dev);
1224 devfs_propagate_dev(dev, 0);
1226 release_dev(dev); /* link ref */
1230 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_destroy_dev_worker -end:5-\n");
1235 * Worker function to destroy all devices with a certain basename.
1236 * Calls devfs_destroy_dev_worker for the actual destruction.
1239 devfs_destroy_subnames_worker(char *name)
1242 size_t len = strlen(name);
1244 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1245 if (!strncmp(dev->si_name, name, len)) {
1246 if (dev->si_name[len] != '\0') {
1247 devfs_destroy_dev_worker(dev);
1248 /* release_dev(dev); */
1256 devfs_clr_subnames_flag_worker(char *name, uint32_t flag)
1259 size_t len = strlen(name);
1261 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1262 if (!strncmp(dev->si_name, name, len)) {
1263 if (dev->si_name[len] != '\0') {
1264 dev->si_flags &= ~flag;
1273 devfs_destroy_subnames_without_flag_worker(char *name, uint32_t flag)
1276 size_t len = strlen(name);
1278 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1279 if (!strncmp(dev->si_name, name, len)) {
1280 if (dev->si_name[len] != '\0') {
1281 if (!(dev->si_flags & flag)) {
1282 devfs_destroy_dev_worker(dev);
1292 * Worker function that creates all device nodes on top of a devfs
1296 devfs_create_all_dev_worker(struct devfs_node *root)
1301 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_all_dev_worker -1-\n");
1303 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1304 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_all_dev_worker -loop:2- -%s-\n", dev->si_name);
1305 devfs_create_device_node(root, dev, NULL, NULL);
1307 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_all_dev_worker -end:3-\n");
1312 * Worker function that destroys all devices that match a specific
1313 * dev_ops and/or minor. If minor is less than 0, it is not matched
1314 * against. It also propagates all changes.
1317 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1322 devfs_debug(DEVFS_DEBUG_DEBUG,
1323 "devfs_destroy_dev_by_ops_worker -1-\n");
1325 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1326 if (dev->si_ops != ops)
1328 if ((minor < 0) || (dev->si_uminor == minor)) {
1329 devfs_debug(DEVFS_DEBUG_DEBUG,
1330 "devfs_destroy_dev_by_ops_worker "
1333 devfs_destroy_dev_worker(dev);
1336 devfs_debug(DEVFS_DEBUG_DEBUG,
1337 "devfs_destroy_dev_by_ops_worker -end:3-\n");
1342 * Worker function that registers a new clone handler in devfs.
1345 devfs_chandler_add_worker(char *name, d_clone_t *nhandler)
1347 struct devfs_clone_handler *chandler = NULL;
1348 u_char len = strlen(name);
1353 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1354 if (chandler->namlen == len) {
1355 if (!memcmp(chandler->name, name, len)) {
1356 /* Clonable basename already exists */
1362 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1363 memcpy(chandler->name, name, len+1);
1364 chandler->namlen = len;
1365 chandler->nhandler = nhandler;
1367 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1372 * Worker function that removes a given clone handler from the
1373 * clone handler list.
1376 devfs_chandler_del_worker(char *name)
1378 struct devfs_clone_handler *chandler, *chandler2;
1379 u_char len = strlen(name);
1384 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1385 if (chandler->namlen != len)
1387 if (memcmp(chandler->name, name, len))
1389 TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1390 kfree(chandler, M_DEVFS);
1397 * Worker function that finds a given device name and changes
1398 * the message received accordingly so that when replied to,
1399 * the answer is returned to the caller.
1402 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1404 struct devfs_alias *alias;
1406 cdev_t found = NULL;
1408 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1409 if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1414 if (found == NULL) {
1415 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1416 if (strcmp(devfs_msg->mdv_name, alias->name) == 0) {
1417 found = alias->dev_target;
1422 devfs_msg->mdv_cdev = found;
1428 * Worker function that finds a given device udev and changes
1429 * the message received accordingly so that when replied to,
1430 * the answer is returned to the caller.
1433 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg)
1436 cdev_t found = NULL;
1438 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1439 if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1444 devfs_msg->mdv_cdev = found;
1450 * Worker function that inserts a given alias into the
1451 * alias list, and propagates the alias to all mount
1455 devfs_make_alias_worker(struct devfs_alias *alias)
1457 struct devfs_alias *alias2;
1458 size_t len = strlen(alias->name);
1461 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1462 if (!memcmp(alias->name, alias2->name, len)) { /* XXX */
1469 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1470 devfs_alias_propagate(alias);
1472 devfs_debug(DEVFS_DEBUG_DEBUG,
1473 "Warning: duplicate devfs_make_alias for %s\n",
1475 kfree(alias, M_DEVFS);
1482 * Function that removes and frees all aliases.
1485 devfs_alias_reap(void)
1487 struct devfs_alias *alias, *alias2;
1489 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1490 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1491 kfree(alias, M_DEVFS);
1497 * Function that removes an alias matching a specific cdev and frees
1501 devfs_alias_remove(cdev_t dev)
1503 struct devfs_alias *alias, *alias2;
1505 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1506 if (alias->dev_target == dev) {
1507 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1508 kfree(alias, M_DEVFS);
1515 * This function propagates a new alias to all mount points.
1518 devfs_alias_propagate(struct devfs_alias *alias)
1520 struct devfs_mnt_data *mnt;
1522 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1523 devfs_alias_apply(mnt->root_node, alias);
1529 * This function is a recursive function iterating through
1530 * all device nodes in the topology and, if applicable,
1531 * creating the relevant alias for a device node.
1534 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1536 struct devfs_node *node1, *node2;
1538 KKASSERT(alias != NULL);
1540 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1541 devfs_debug(DEVFS_DEBUG_DEBUG, "This node is Pdir or Proot; has %d children\n", node->nchildren);
1542 if (node->nchildren > 2) {
1543 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1544 devfs_alias_apply(node1, alias);
1548 if (node->d_dev == alias->dev_target)
1549 devfs_alias_create(alias->name, node);
1555 * This function checks if any alias possibly is applicable
1556 * to the given node. If so, the alias is created.
1559 devfs_alias_check_create(struct devfs_node *node)
1561 struct devfs_alias *alias;
1563 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1564 if (node->d_dev == alias->dev_target)
1565 devfs_alias_create(alias->name, node);
1571 * This function creates an alias with a given name
1572 * linking to a given devfs node. It also increments
1573 * the link count on the target node.
1576 devfs_alias_create(char *name_orig, struct devfs_node *target)
1578 struct mount *mp = target->mp;
1579 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1580 struct devfs_node *linknode;
1581 char *create_path = NULL;
1582 char *name, name_buf[PATH_MAX];
1584 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1586 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1589 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1592 if (devfs_find_device_node_by_name(parent, name)) {
1593 devfs_debug(DEVFS_DEBUG_DEBUG,
1594 "Node already exists: %s "
1595 "(devfs_make_alias_worker)!\n",
1601 linknode = devfs_allocp(Plink, name, parent, mp, NULL);
1602 if (linknode == NULL)
1605 linknode->link_target = target;
1608 linknode->flags |= DEVFS_LINK;
1615 * This function is called by the core and handles mount point
1616 * strings. It either calls the relevant worker (devfs_apply_
1617 * reset_rules_worker) on all mountpoints or only a specific
1621 devfs_apply_reset_rules_caller(char *mountto, int apply)
1623 struct devfs_mnt_data *mnt;
1624 size_t len = strlen(mountto);
1626 if (mountto[0] != '*') {
1627 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1628 if ((len == mnt->mntonnamelen) &&
1629 (!memcmp(mnt->mp->mnt_stat.f_mntonname, mountto, len))) {
1630 devfs_apply_reset_rules_worker(mnt->root_node, apply);
1635 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1636 devfs_apply_reset_rules_worker(mnt->root_node, apply);
1640 kfree(mountto, M_DEVFS);
1645 * This worker function applies or resets, depending on the arguments, a rule
1646 * to the whole given topology. *RECURSIVE*
1649 devfs_apply_reset_rules_worker(struct devfs_node *node, int apply)
1651 struct devfs_node *node1, *node2;
1653 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1654 devfs_debug(DEVFS_DEBUG_DEBUG, "This node is Pdir or Proot; has %d children\n", node->nchildren);
1655 if (node->nchildren > 2) {
1656 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1657 devfs_apply_reset_rules_worker(node1, apply);
1663 devfs_rule_check_apply(node);
1665 devfs_rule_reset_node(node);
1672 * This function calls a given callback function for
1673 * every dev node in the devfs dev list.
1676 devfs_scan_callback_worker(devfs_scan_t *callback)
1680 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_scan_callback: %p -1-\n", callback);
1682 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1686 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_scan_callback: finished\n");
1692 * This function tries to resolve a given directory, or if not
1693 * found and creation requested, creates the given directory.
1695 static struct devfs_node *
1696 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1697 size_t name_len, int create)
1699 struct devfs_node *node, *found = NULL;
1701 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1702 if (name_len == node->d_dir.d_namlen) {
1703 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1710 if ((found == NULL) && (create)) {
1711 found = devfs_allocp(Pdir, dir_name, parent, parent->mp, NULL);
1718 * This function tries to resolve a complete path. If creation is requested,
1719 * if a given part of the path cannot be resolved (because it doesn't exist),
1723 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1725 struct devfs_node *node = parent;
1734 for (; *path != '\0' ; path++) {
1739 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1746 return devfs_resolve_or_create_dir(node, buf, idx, create);
1750 * Takes a full path and strips it into a directory path and a name.
1751 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
1752 * requires a working buffer with enough size to keep the whole
1756 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
1760 size_t len = strlen(fullpath) + 1;
1763 KKASSERT((fullpath != NULL) && (buf != NULL) && (pathp != NULL) && (namep != NULL));
1765 memcpy(buf, fullpath, len);
1767 for (i = len-1; i>= 0; i--) {
1768 if (buf[i] == '/') {
1788 * This function creates a new devfs node for a given device. It can
1789 * handle a complete path as device name, and accordingly creates
1790 * the path and the final device node.
1792 * The reference count on the passed dev remains unchanged.
1795 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
1796 char *dev_name, char *path_fmt, ...)
1798 struct devfs_node *parent, *node = NULL;
1800 char *name, name_buf[PATH_MAX];
1804 char *create_path = NULL;
1805 char *names = "pqrsPQRS";
1807 if (path_fmt != NULL) {
1808 path = kmalloc(PATH_MAX+1, M_DEVFS, M_WAITOK);
1810 __va_start(ap, path_fmt);
1811 i = kvcprintf(path_fmt, NULL, path, 10, ap);
1816 parent = devfs_resolve_or_create_path(root, path, 1);
1819 devfs_resolve_name_path(((dev_name == NULL) && (dev))?(dev->si_name):(dev_name), name_buf, &create_path, &name);
1822 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1825 if (devfs_find_device_node_by_name(parent, name)) {
1826 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_device_node: "
1827 "DEVICE %s ALREADY EXISTS!!! Ignoring creation request.\n", name);
1830 devfs_debug(DEVFS_DEBUG_DEBUG, "parent->d_dir.d_name=%s\n", parent->d_dir.d_name);
1831 node = devfs_allocp(Pdev, name, parent, parent->mp, dev);
1832 devfs_debug(DEVFS_DEBUG_DEBUG, "node->d_dir.d_name=%s\n", node->d_dir.d_name);
1835 /* Ugly unix98 pty magic, to hide pty master (ptm) devices and their directory */
1836 if ((dev) && (strlen(dev->si_name) >= 4) && (!memcmp(dev->si_name, "ptm/", 4))) {
1837 node->parent->flags |= DEVFS_HIDDEN;
1838 node->flags |= DEVFS_HIDDEN;
1841 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_device_node: marker A\n");
1842 /* Ugly pty magic, to tag pty devices as such and hide them if needed */
1843 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
1844 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1846 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_device_node: marker B\n");
1847 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
1849 for (i = 0; i < strlen(names); i++) {
1850 if (name[3] == names[i]) {
1856 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1858 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_device_node: marker C\n");
1861 if (path_fmt != NULL)
1862 kfree(path, M_DEVFS);
1868 * This function finds a given device node in the topology with a given
1872 devfs_find_device_node(struct devfs_node *node, cdev_t target)
1874 struct devfs_node *node1, *node2, *found = NULL;
1876 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1877 devfs_debug(DEVFS_DEBUG_DEBUG, "This node is Pdir or Proot; has %d children\n", node->nchildren);
1878 if (node->nchildren > 2) {
1879 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1880 if ((found = devfs_find_device_node(node1, target)))
1884 } else if (node->node_type == Pdev) {
1885 if (node->d_dev == target)
1893 * This function finds a device node in the topology by its
1894 * name and returns it.
1897 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
1899 struct devfs_node *node, *found = NULL;
1900 size_t len = strlen(target);
1902 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1903 if ((len == node->d_dir.d_namlen) && (!memcmp(node->d_dir.d_name, target, len))) {
1913 * This function takes a cdev and removes its devfs node in the
1914 * given topology. The cdev remains intact.
1917 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
1919 struct devfs_node *node, *parent;
1920 char *name, name_buf[PATH_MAX];
1921 char *create_path = NULL;
1925 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_destroy_device_node\n");
1926 memcpy(name_buf, target->si_name, strlen(target->si_name)+1);
1928 devfs_resolve_name_path(target->si_name, name_buf, &create_path, &name);
1929 devfs_debug(DEVFS_DEBUG_DEBUG, "create_path: %s\n", create_path);
1930 devfs_debug(DEVFS_DEBUG_DEBUG, "name: %s\n", name);
1933 parent = devfs_resolve_or_create_path(root, create_path, 0);
1936 devfs_debug(DEVFS_DEBUG_DEBUG, "-> marker <-\n");
1939 devfs_debug(DEVFS_DEBUG_DEBUG, "->d_dir.d_name=%s\n", parent->d_dir.d_name);
1940 node = devfs_find_device_node_by_name(parent, name);
1941 devfs_debug(DEVFS_DEBUG_DEBUG,
1942 "->d_dir.d_name=%s\n",
1943 ((node) ? (node->d_dir.d_name) : "SHIT!"));
1951 * Just set perms and ownership for given node.
1954 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid, u_short mode, u_long flags)
1956 node->mode = mode; /* files access mode and type */
1957 node->uid = uid; /* owner user id */
1958 node->gid = gid; /* owner group id */
1964 * Propagates a device attach/detach to all mount
1965 * points. Also takes care of automatic alias removal
1966 * for a deleted cdev.
1969 devfs_propagate_dev(cdev_t dev, int attach)
1971 struct devfs_mnt_data *mnt;
1973 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_propagate_dev -1-\n");
1974 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1975 devfs_debug(DEVFS_DEBUG_DEBUG,
1976 "devfs_propagate_dev -loop:2-\n");
1978 /* Device is being attached */
1979 devfs_create_device_node(mnt->root_node, dev,
1982 /* Device is being detached */
1983 devfs_alias_remove(dev);
1984 devfs_destroy_device_node(mnt->root_node, dev);
1987 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_propagate_dev -end:3-\n");
1992 * devfs_node_to_path takes a node and a buffer of a size of
1993 * at least PATH_MAX, resolves the full path from the root
1994 * node and writes it in a humanly-readable format into the
1996 * If DEVFS_STASH_DEPTH is less than the directory level up
1997 * to the root node, only the last DEVFS_STASH_DEPTH levels
1998 * of the path are resolved.
2001 devfs_node_to_path(struct devfs_node *node, char *buffer)
2003 #define DEVFS_STASH_DEPTH 32
2004 struct devfs_node *node_stash[DEVFS_STASH_DEPTH];
2006 memset(buffer, 0, PATH_MAX);
2008 for (i = 0; (i < DEVFS_STASH_DEPTH) && (node->node_type != Proot); i++) {
2009 node_stash[i] = node;
2010 node = node->parent;
2014 for (offset = 0; i >= 0; i--) {
2015 memcpy(buffer+offset, node_stash[i]->d_dir.d_name, node_stash[i]->d_dir.d_namlen);
2016 offset += node_stash[i]->d_dir.d_namlen;
2018 *(buffer+offset) = '/';
2022 #undef DEVFS_STASH_DEPTH
2027 * devfs_clone either returns a basename from a complete name by
2028 * returning the length of the name without trailing digits, or,
2029 * if clone != 0, calls the device's clone handler to get a new
2030 * device, which in turn is returned in devp.
2033 devfs_clone(char *name, size_t *namlenp, cdev_t *devp, int clone, struct ucred *cred)
2037 size_t len = *namlenp;
2039 struct devfs_clone_handler *chandler;
2040 struct dev_clone_args ap;
2043 for (; (len > 0) && (DEVFS_ISDIGIT(name[len-1])); len--);
2046 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
2047 devfs_debug(DEVFS_DEBUG_DEBUG, "len=%d, chandler->namlen=%d\n", len, chandler->namlen);
2048 devfs_debug(DEVFS_DEBUG_DEBUG, "name=%s, chandler->name=%s\n", name, chandler->name);
2049 if ((chandler->namlen == len) &&
2050 (!memcmp(chandler->name, name, len)) &&
2051 (chandler->nhandler)) {
2052 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nclone: found clone handler for the base name at %p\n", chandler->nhandler);
2058 error = (chandler->nhandler)(&ap);
2075 * Registers a new orphan in the orphan list.
2078 devfs_tracer_add_orphan(struct devfs_node *node)
2080 struct devfs_orphan *orphan;
2083 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2084 orphan->node = node;
2086 KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2087 node->flags |= DEVFS_ORPHANED;
2088 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2092 * Removes an orphan from the orphan list.
2095 devfs_tracer_del_orphan(struct devfs_node *node)
2097 struct devfs_orphan *orphan;
2101 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) {
2102 if (orphan->node == node) {
2103 node->flags &= ~DEVFS_ORPHANED;
2104 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2105 kfree(orphan, M_DEVFS);
2112 * Counts the orphans in the orphan list, and if cleanup
2113 * is specified, also frees the orphan and removes it from
2117 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2119 struct devfs_orphan *orphan, *orphan2;
2122 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) {
2125 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2126 orphan->node->flags &= ~DEVFS_ORPHANED;
2127 devfs_freep(orphan->node);
2128 kfree(orphan, M_DEVFS);
2136 * Fetch an ino_t from the global d_ino by increasing it
2140 devfs_fetch_ino(void)
2144 spin_lock_wr(&ino_lock);
2146 spin_unlock_wr(&ino_lock);
2152 * Allocates a new cdev and initializes it's most basic
2156 devfs_new_cdev(struct dev_ops *ops, int minor)
2158 cdev_t dev = sysref_alloc(&cdev_sysref_class);
2159 sysref_activate(&dev->si_sysref);
2161 devfs_debug(DEVFS_DEBUG_DEBUG,
2162 "new_cdev: clearing first %d bytes\n",
2163 offsetof(struct cdev, si_sysref));
2164 memset(dev, 0, offsetof(struct cdev, si_sysref));
2169 dev->si_drv1 = NULL;
2170 dev->si_drv2 = NULL;
2171 dev->si_lastread = 0; /* time_second */
2172 dev->si_lastwrite = 0; /* time_second */
2177 dev->si_uminor = minor;
2178 dev->si_inode = makeudev(devfs_reference_ops(ops), minor);
2184 devfs_cdev_terminate(cdev_t dev)
2188 /* Check if it is locked already. if not, we acquire the devfs lock */
2189 if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) {
2190 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2194 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_cdev_terminate: Taking care of dev->si_name=%s\n", dev->si_name);
2196 /* Propagate destruction, just in case */
2197 devfs_propagate_dev(dev, 0);
2199 /* If we acquired the lock, we also get rid of it */
2201 lockmgr(&devfs_lock, LK_RELEASE);
2203 devfs_release_ops(dev->si_ops);
2205 /* Finally destroy the device */
2206 sysref_put(&dev->si_sysref);
2210 * Links a given cdev into the dev list.
2213 devfs_link_dev(cdev_t dev)
2215 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2216 dev->si_flags |= SI_DEVFS_LINKED;
2217 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2223 * Removes a given cdev from the dev list. The caller is responsible for
2224 * releasing the reference on the device associated with the linkage.
2226 * Returns EALREADY if the dev has already been unlinked.
2229 devfs_unlink_dev(cdev_t dev)
2231 if ((dev->si_flags & SI_DEVFS_LINKED)) {
2232 TAILQ_REMOVE(&devfs_dev_list, dev, link);
2233 dev->si_flags &= ~SI_DEVFS_LINKED;
2240 devfs_node_is_accessible(struct devfs_node *node)
2242 if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2249 devfs_reference_ops(struct dev_ops *ops)
2253 if (ops->head.refs == 0) {
2254 ops->head.id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255);
2255 if (ops->head.id == -1) {
2256 /* Ran out of unique ids */
2257 kprintf("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);
2277 devfs_config(void *arg)
2281 msg = devfs_msg_get();
2283 kprintf("devfs_config: sync'ing up\n");
2284 msg = devfs_msg_send_sync(DEVFS_SYNC, msg);
2289 * Called on init of devfs; creates the objcaches and
2290 * spawns off the devfs core thread. Also initializes
2296 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2297 /* Create objcaches for nodes, msgs and devs */
2298 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2300 objcache_malloc_alloc,
2301 objcache_malloc_free,
2302 &devfs_node_malloc_args );
2304 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2306 objcache_malloc_alloc,
2307 objcache_malloc_free,
2308 &devfs_msg_malloc_args );
2310 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2312 objcache_malloc_alloc,
2313 objcache_malloc_free,
2314 &devfs_dev_malloc_args );
2316 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2318 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(ops_id), 0);
2321 /* Initialize the reply-only port which acts as a message drain */
2322 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2324 /* Initialize *THE* devfs lock */
2325 lockinit(&devfs_lock, "devfs_core lock", 0, 0);
2328 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2329 0, 0, "devfs_msg_core");
2331 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2333 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2337 * Called on unload of devfs; takes care of destroying the core
2338 * and the objcaches. Also removes aliases that are no longer needed.
2343 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2345 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2347 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2348 tsleep(td_core/*devfs_id*/, 0, "devfsc", 10000);
2350 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2352 /* Destroy the objcaches */
2353 objcache_destroy(devfs_msg_cache);
2354 objcache_destroy(devfs_node_cache);
2355 objcache_destroy(devfs_dev_cache);
2361 * This is a sysctl handler to assist userland devname(3) to
2362 * find the device name for a given udev.
2365 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2372 if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t))))
2375 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev);
2380 if ((found = devfs_find_device_by_udev(udev)) == NULL)
2383 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2387 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
2388 NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)");
2390 static SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2391 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2392 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable, 0, "Enable DevFS debugging");
2394 SYSINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, devfs_init, NULL);
2395 SYSUNINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, devfs_uninit, NULL);