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 <vfs/devfs/devfs.h>
49 #include <vfs/devfs/devfs_rules.h>
51 MALLOC_DEFINE(M_DEVFS, "devfs", "Device File System (devfs) allocations");
54 * SYSREF Integration - reference counting, allocation,
55 * sysid and syslink integration.
57 static void devfs_cdev_terminate(cdev_t dev);
58 static struct sysref_class cdev_sysref_class = {
61 .proto = SYSREF_PROTO_DEV,
62 .offset = offsetof(struct cdev, si_sysref),
63 .objsize = sizeof(struct cdev),
67 .terminate = (sysref_terminate_func_t)devfs_cdev_terminate
71 static struct objcache *devfs_node_cache;
72 static struct objcache *devfs_msg_cache;
73 static struct objcache *devfs_dev_cache;
75 static struct objcache_malloc_args devfs_node_malloc_args = {
76 sizeof(struct devfs_node), M_DEVFS };
77 struct objcache_malloc_args devfs_msg_malloc_args = {
78 sizeof(struct devfs_msg), M_DEVFS };
79 struct objcache_malloc_args devfs_dev_malloc_args = {
80 sizeof(struct cdev), M_DEVFS };
82 static struct devfs_dev_head devfs_dev_list = TAILQ_HEAD_INITIALIZER(devfs_dev_list);
83 static struct devfs_mnt_head devfs_mnt_list = TAILQ_HEAD_INITIALIZER(devfs_mnt_list);
84 static struct devfs_chandler_head devfs_chandler_list = TAILQ_HEAD_INITIALIZER(devfs_chandler_list);
85 static struct devfs_alias_head devfs_alias_list = TAILQ_HEAD_INITIALIZER(devfs_alias_list);
87 struct lock devfs_lock;
88 static struct lwkt_port devfs_dispose_port;
89 static struct lwkt_port devfs_msg_port;
90 static struct thread *td_core;
91 //static void *devfs_id = (void *)0xDE33A;
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 node->flags |= DEVFS_NODE_LINKED; //Make sure we don't recycle the root vnode
186 TAILQ_INIT(DEVFS_DENODE_HEAD(node));
187 node->d_dir.d_type = DT_DIR;
192 node->d_dir.d_type = DT_LNK;
196 node->d_dir.d_type = DT_REG;
201 node->d_dir.d_type = DT_CHR;
203 node->d_dir.d_ino = dev->si_inode;
205 node->mode = dev->si_perms; /* files access mode and type */
206 node->uid = dev->si_uid; /* owner user id */
207 node->gid = dev->si_gid; /* owner group id */
209 devfs_alias_check_create(node);
214 panic("devfs_allocp: unknown node type");
218 node->node_type = devfsnodetype;
220 /* Init the dirent structure of each devfs vnode */
221 KKASSERT(namlen < 256);
223 node->d_dir.d_namlen = namlen;
224 node->d_dir.d_name = kmalloc(namlen+1, M_DEVFS, M_WAITOK);
225 memcpy(node->d_dir.d_name, name, namlen);
226 node->d_dir.d_name[namlen] = '\0';
228 /* Initialize the parent node element */
229 node->parent = parent;
232 devfs_rule_check_apply(node);
235 nanotime(&node->atime);
236 node->mtime = node->ctime = node->atime;
239 * Associate with parent as last step, clean out namecache
242 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocp: about to insert node\n");
243 if ((parent != NULL) &&
244 ((parent->node_type == Proot) || (parent->node_type == Pdir))) {
245 devfs_debug(DEVFS_DEBUG_DEBUG,
246 "devfs_allocp: node inserted %p\n",
249 node->cookie = parent->cookie_jar++;
250 node->flags |= DEVFS_NODE_LINKED;
251 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent), node, link);
253 /* This forces negative namecache lookups to clear */
254 ++mp->mnt_namecache_gen;
257 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocp -end:2-\n");
262 * devfs_allocv() allocates a new vnode based on a devfs node.
265 devfs_allocv(struct vnode **vpp, struct devfs_node *node)
272 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -1-\n");
275 while ((vp = node->v_node) != NULL) {
276 error = vget(vp, LK_EXCLUSIVE);
277 if (error != ENOENT) {
279 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv, code path 2...\n");
283 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -3-\n");
285 //XXX: afaik getnewvnode doesn't return anything but 0.
287 if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0)
292 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -4-\n");
294 if (node->v_node != NULL) {
302 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -5-\n");
304 switch (node->node_type) {
321 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -6-\n");
322 KKASSERT(node->d_dev);
325 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -7-\n");
326 vp->v_uminor = node->d_dev->si_uminor;
328 vp->v_rdev = node->d_dev;
329 vp->v_ops = &node->mp->mnt_vn_spec_ops;
330 //v_associate_rdev(vp, node->d_dev);
331 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -8-\n");
333 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv: type is Pdev but d_dev is not set!!!!\n");
335 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -9-\n");
339 panic("devfs_allocv: unknown node type");
343 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -10-\n");
344 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocv -end:11-\n");
349 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
350 * based on the newly created devfs node.
353 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype,
354 char *name, struct devfs_node *parent, cdev_t dev)
356 struct devfs_node *node;
358 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocvp -1-\n");
359 node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
360 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocvp -2-\n");
362 devfs_allocv(vpp, node);
366 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_allocvp -end:3-\n");
372 * Destroy the devfs_node. The node must be unlinked from the topology.
374 * This function will also destroy any vnode association with the node
377 * The cdev_t itself remains intact.
380 devfs_freep(struct devfs_node *node)
385 KKASSERT(((node->flags & DEVFS_NODE_LINKED) == 0) ||
386 (node->node_type == Proot));
387 KKASSERT((node->flags & DEVFS_DESTROYED) == 0);
389 atomic_subtract_int(&(DEVFS_MNTDATA(node->mp)->leak_count), 1);
390 if (node->symlink_name) {
391 kfree(node->symlink_name, M_DEVFS);
392 node->symlink_name = NULL;
396 * Remove the node from the orphan list if it is still on it.
398 if (node->flags & DEVFS_ORPHANED)
399 devfs_tracer_del_orphan(node);
402 * Disassociate the vnode from the node. This also prevents the
403 * vnode's reclaim code from double-freeing the node.
405 if ((vp = node->v_node) != NULL) {
410 if (node->d_dir.d_name)
411 kfree(node->d_dir.d_name, M_DEVFS);
412 node->flags |= DEVFS_DESTROYED;
414 objcache_put(devfs_node_cache, node);
420 * Unlink the devfs node from the topology and add it to the orphan list.
421 * The node will later be destroyed by freep.
423 * Any vnode association, including the v_rdev and v_data, remains intact
427 devfs_unlinkp(struct devfs_node *node)
429 struct devfs_node *parent;
432 devfs_tracer_add_orphan(node);
433 devfs_debug(DEVFS_DEBUG_DEBUG,
434 "devfs_unlinkp for %s\n", node->d_dir.d_name);
435 parent = node->parent;
438 * If the parent is known we can unlink the node out of the topology
441 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
443 KKASSERT((parent->nchildren >= 0));
444 node->flags &= ~DEVFS_NODE_LINKED;
451 * devfs_reaperp() is a recursive function that iterates through all the
452 * topology, unlinking and freeing all devfs nodes.
455 devfs_reaperp(struct devfs_node *node)
457 struct devfs_node *node1, *node2;
459 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
460 devfs_debug(DEVFS_DEBUG_DEBUG,
461 "This node is Pdir or Proot; has %d children\n",
463 if (node->nchildren > 2) {
464 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
466 devfs_reaperp(node1);
477 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
478 * freeing a node, but also removes empty directories and links that link
479 * via devfs auto-link mechanism to the node being deleted.
482 devfs_gc(struct devfs_node *node)
484 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
486 devfs_gc_links(root_node, node, node->nlinks);
488 devfs_gc_dirs(root_node);
496 * devfs_gc_dirs() is a helper function for devfs_gc, unlinking and freeing
500 devfs_gc_dirs(struct devfs_node *node)
502 struct devfs_node *node1, *node2;
504 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
505 devfs_debug(DEVFS_DEBUG_DEBUG,
506 "This node is Pdir or Proot; has %d children\n",
508 if (node->nchildren > 2) {
509 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
511 devfs_gc_dirs(node1);
515 if (node->nchildren == 2) {
516 devfs_debug(DEVFS_DEBUG_DEBUG,
517 "This node is called %s and it is empty\n",
528 * devfs_gc_links() is a helper function for devfs_gc, unlinking and freeing
529 * eauto-linked nodes linking to the node being deleted.
532 devfs_gc_links(struct devfs_node *node, struct devfs_node *target,
535 struct devfs_node *node1, *node2;
538 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
539 devfs_debug(DEVFS_DEBUG_DEBUG, "This node is Pdir or Proot; has %d children\n", node->nchildren);
540 if (node->nchildren > 2) {
541 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
542 nlinks = devfs_gc_links(node1, target, nlinks);
545 } else if (node->link_target == target) {
552 KKASSERT(nlinks >= 0);
558 * devfs_create_dev() is the asynchronous entry point for device creation.
559 * It just sends a message with the relevant details to the devfs core.
561 * This function will reference the passed device. The reference is owned
562 * by devfs and represents all of the device's node associations.
565 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
568 devfs_debug(DEVFS_DEBUG_DEBUG,
569 "devfs_create_dev -1-, name: %s (%p)\n",
572 id = devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
573 devfs_debug(DEVFS_DEBUG_DEBUG,
574 "devfs_create_dev -end:2- (unique id: %x) / (%p)\n",
580 * devfs_destroy_dev() is the asynchronous entry point for device destruction.
581 * It just sends a message with the relevant details to the devfs core.
584 devfs_destroy_dev(cdev_t dev)
586 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
591 * devfs_mount_add() is the synchronous entry point for adding a new devfs
592 * mount. It sends a synchronous message with the relevant details to the
596 devfs_mount_add(struct devfs_mnt_data *mnt)
600 msg = devfs_msg_get();
602 msg = devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
609 * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
610 * It sends a synchronous message with the relevant details to the devfs core.
613 devfs_mount_del(struct devfs_mnt_data *mnt)
617 msg = devfs_msg_get();
619 msg = devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
626 * devfs_destroy_subnames() is the synchronous entry point for device destruction
627 * by subname. It just sends a message with the relevant details to the devfs core.
630 devfs_destroy_subnames(char *name)
634 msg = devfs_msg_get();
635 msg->mdv_load = name;
636 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES, msg);
642 devfs_clr_subnames_flag(char *name, uint32_t flag)
646 msg = devfs_msg_get();
647 msg->mdv_flags.name = name;
648 msg->mdv_flags.flag = flag;
649 msg = devfs_msg_send_sync(DEVFS_CLR_SUBNAMES_FLAG, msg);
656 devfs_destroy_subnames_without_flag(char *name, uint32_t flag)
660 msg = devfs_msg_get();
661 msg->mdv_flags.name = name;
662 msg->mdv_flags.flag = flag;
663 msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES_WO_FLAG, msg);
670 * devfs_create_all_dev is the asynchronous entry point to trigger device
671 * node creation. It just sends a message with the relevant details to
675 devfs_create_all_dev(struct devfs_node *root)
677 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
682 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
683 * devices with a specific set of dev_ops and minor. It just sends a
684 * message with the relevant details to the devfs core.
687 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
689 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
694 * devfs_clone_handler_add is the synchronous entry point to add a new
695 * clone handler. It just sends a message with the relevant details to
699 devfs_clone_handler_add(char *name, d_clone_t *nhandler)
703 msg = devfs_msg_get();
704 msg->mdv_chandler.name = name;
705 msg->mdv_chandler.nhandler = nhandler;
706 msg = devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg);
712 * devfs_clone_handler_del is the synchronous entry point to remove a
713 * clone handler. It just sends a message with the relevant details to
717 devfs_clone_handler_del(char *name)
721 msg = devfs_msg_get();
722 msg->mdv_chandler.name = name;
723 msg->mdv_chandler.nhandler = NULL;
724 msg = devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg);
730 * devfs_find_device_by_name is the synchronous entry point to find a
731 * device given its name. It sends a synchronous message with the
732 * relevant details to the devfs core and returns the answer.
735 devfs_find_device_by_name(const char *fmt, ...)
739 char target[PATH_MAX+1];
748 i = kvcprintf(fmt, NULL, target, 10, ap);
753 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_find_device_by_name: %s -1-\n", target);
754 msg = devfs_msg_get();
755 msg->mdv_name = target;
756 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
757 found = msg->mdv_cdev;
760 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_find_device_by_name found? %s -end:2-\n", (found)?"YES":"NO");
765 * devfs_find_device_by_udev is the synchronous entry point to find a
766 * device given its udev number. It sends a synchronous message with
767 * the relevant details to the devfs core and returns the answer.
770 devfs_find_device_by_udev(udev_t udev)
775 msg = devfs_msg_get();
776 msg->mdv_udev = udev;
777 msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg);
778 found = msg->mdv_cdev;
781 devfs_debug(DEVFS_DEBUG_DEBUG,
782 "devfs_find_device_by_udev found? %s -end:3-\n",
783 ((found) ? found->si_name:"NO"));
788 * devfs_make_alias is the asynchronous entry point to register an alias
789 * for a device. It just sends a message with the relevant details to the
793 devfs_make_alias(char *name, cdev_t dev_target)
795 struct devfs_alias *alias;
797 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
798 memcpy(alias->name, name, strlen(name) + 1);
799 alias->dev_target = dev_target;
801 devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
806 * devfs_apply_rules is the asynchronous entry point to trigger application
807 * of all rules. It just sends a message with the relevant details to the
811 devfs_apply_rules(char *mntto)
816 namelen = strlen(mntto) + 1;
818 new_name = kmalloc(namelen, M_DEVFS, M_WAITOK);
820 memcpy(new_name, mntto, namelen);
822 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
827 * devfs_reset_rules is the asynchronous entry point to trigger reset of all rules.
828 * It just sends a message with the relevant details to the devfs core.
831 devfs_reset_rules(char *mntto)
836 namelen = strlen(mntto) + 1;
838 new_name = kmalloc(namelen, M_DEVFS, M_WAITOK);
840 memcpy(new_name, mntto, namelen);
842 devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
848 * devfs_scan_callback is the asynchronous entry point to call a callback
850 * It just sends a message with the relevant details to the devfs core.
853 devfs_scan_callback(devfs_scan_t *callback)
857 /* Make sure that function pointers have the size of a generic pointer (innecessary) */
858 KKASSERT(sizeof(callback) == sizeof(void *));
860 msg = devfs_msg_get();
861 msg->mdv_load = callback;
862 msg = devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
870 * Acts as a message drain. Any message that is replied to here gets destroyed and
874 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
876 devfs_msg_put((devfs_msg_t)msg);
880 * devfs_msg_get allocates a new devfs msg and returns it.
885 return objcache_get(devfs_msg_cache, M_WAITOK);
889 * devfs_msg_put deallocates a given devfs msg.
892 devfs_msg_put(devfs_msg_t msg)
894 objcache_put(devfs_msg_cache, msg);
899 * devfs_msg_send is the generic asynchronous message sending facility
900 * for devfs. By default the reply port is the automatic disposal port.
903 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
905 lwkt_port_t port = &devfs_msg_port;
907 lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
909 devfs_msg->hdr.u.ms_result = cmd;
910 devfs_msg->id = atomic_fetchadd_int(&msg_id, 1);
912 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
914 return devfs_msg->id;
918 * devfs_msg_send_sync is the generic synchronous message sending
919 * facility for devfs. It initializes a local reply port and waits
920 * for the core's answer. This answer is then returned.
923 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
925 struct lwkt_port rep_port;
926 devfs_msg_t msg_incoming;
927 lwkt_port_t port = &devfs_msg_port;
929 lwkt_initport_thread(&rep_port, curthread);
930 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
932 devfs_msg->hdr.u.ms_result = cmd;
933 devfs_msg->id = atomic_fetchadd_int(&msg_id, 1);
935 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
936 msg_incoming = lwkt_waitport(&rep_port, 0);
942 * sends a message with a generic argument.
945 devfs_msg_send_generic(uint32_t cmd, void *load)
947 devfs_msg_t devfs_msg = devfs_msg_get();
948 devfs_msg->mdv_load = load;
950 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_send_generic -1- (%p)\n", load);
952 return devfs_msg_send(cmd, devfs_msg);
956 * sends a message with a name argument.
959 devfs_msg_send_name(uint32_t cmd, char *name)
961 devfs_msg_t devfs_msg = devfs_msg_get();
962 devfs_msg->mdv_name = name;
964 return devfs_msg_send(cmd, devfs_msg);
968 * sends a message with a mount argument.
971 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
973 devfs_msg_t devfs_msg = devfs_msg_get();
974 devfs_msg->mdv_mnt = mnt;
976 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_send_mp -1- (%p)\n", mnt);
978 return devfs_msg_send(cmd, devfs_msg);
982 * sends a message with an ops argument.
985 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
987 devfs_msg_t devfs_msg = devfs_msg_get();
988 devfs_msg->mdv_ops.ops = ops;
989 devfs_msg->mdv_ops.minor = minor;
991 return devfs_msg_send(cmd, devfs_msg);
995 * sends a message with a clone handler argument.
998 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
1000 devfs_msg_t devfs_msg = devfs_msg_get();
1001 devfs_msg->mdv_chandler.name = name;
1002 devfs_msg->mdv_chandler.nhandler = handler;
1004 return devfs_msg_send(cmd, devfs_msg);
1008 * sends a message with a device argument.
1011 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1013 devfs_msg_t devfs_msg = devfs_msg_get();
1014 devfs_msg->mdv_dev.dev = dev;
1015 devfs_msg->mdv_dev.uid = uid;
1016 devfs_msg->mdv_dev.gid = gid;
1017 devfs_msg->mdv_dev.perms = perms;
1019 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_send_dev -1- (%p)\n", dev);
1021 return devfs_msg_send(cmd, devfs_msg);
1025 * sends a message with a link argument.
1029 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1031 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;
1037 return devfs_msg_send(cmd, devfs_msg);
1041 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1042 * and calls the relevant worker functions. By using messages it's assured
1043 * that events occur in the correct order.
1046 devfs_msg_core(void *arg)
1051 struct devfs_mnt_data *mnt;
1052 struct devfs_node *node;
1054 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_core -1-\n");
1055 lwkt_initport_thread(&devfs_msg_port, curthread);
1056 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_core -2-\n");
1057 wakeup(td_core/*devfs_id*/);
1058 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_core -3-\n");
1061 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_core -loop:4-\n");
1062 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1063 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_core, new msg: %x (unique id: %x)\n", (unsigned int)msg->hdr.u.ms_result, msg->id);
1064 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1065 switch (msg->hdr.u.ms_result) {
1067 case DEVFS_DEVICE_CREATE:
1068 dev = msg->mdv_dev.dev;
1069 devfs_debug(DEVFS_DEBUG_DEBUG,
1070 "devfs_msg_core device create msg %s(%p)\n",
1072 devfs_create_dev_worker(dev,
1075 msg->mdv_dev.perms);
1078 case DEVFS_DEVICE_DESTROY:
1079 devfs_debug(DEVFS_DEBUG_DEBUG,
1080 "devfs_msg_core device destroy msg\n");
1081 dev = msg->mdv_dev.dev;
1082 devfs_destroy_dev_worker(dev);
1085 case DEVFS_DESTROY_SUBNAMES:
1086 devfs_destroy_subnames_worker(msg->mdv_load);
1089 case DEVFS_DESTROY_DEV_BY_OPS:
1090 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1091 msg->mdv_ops.minor);
1094 case DEVFS_CREATE_ALL_DEV:
1095 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_msg_core device create ALL msg\n");
1096 node = (struct devfs_node *)msg->mdv_load;
1097 devfs_create_all_dev_worker(node);
1100 case DEVFS_MOUNT_ADD:
1102 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1103 devfs_create_all_dev_worker(mnt->root_node);
1106 case DEVFS_MOUNT_DEL:
1108 TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1109 devfs_debug(DEVFS_DEBUG_SHOW, "There are still %d devfs_node elements!!!\n", mnt->leak_count);
1110 devfs_reaperp(mnt->root_node);
1111 devfs_debug(DEVFS_DEBUG_SHOW, "Leaked %d devfs_node elements!!!\n", mnt->leak_count);
1114 case DEVFS_CHANDLER_ADD:
1115 devfs_chandler_add_worker(msg->mdv_chandler.name, msg->mdv_chandler.nhandler);
1118 case DEVFS_CHANDLER_DEL:
1119 devfs_chandler_del_worker(msg->mdv_chandler.name);
1122 case DEVFS_FIND_DEVICE_BY_NAME:
1123 devfs_find_device_by_name_worker(msg);
1126 case DEVFS_FIND_DEVICE_BY_UDEV:
1127 devfs_find_device_by_udev_worker(msg);
1130 case DEVFS_MAKE_ALIAS:
1131 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1134 case DEVFS_APPLY_RULES:
1135 devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1138 case DEVFS_RESET_RULES:
1139 devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1142 case DEVFS_SCAN_CALLBACK:
1143 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load);
1146 case DEVFS_CLR_SUBNAMES_FLAG:
1147 devfs_clr_subnames_flag_worker(msg->mdv_flags.name,
1148 msg->mdv_flags.flag);
1151 case DEVFS_DESTROY_SUBNAMES_WO_FLAG:
1152 devfs_destroy_subnames_without_flag_worker(msg->mdv_flags.name,
1153 msg->mdv_flags.flag);
1156 case DEVFS_TERMINATE_CORE:
1162 devfs_debug(DEVFS_DEBUG_DEBUG,
1163 "devfs_msg_core: unknown message "
1164 "received at core\n");
1167 lockmgr(&devfs_lock, LK_RELEASE);
1169 lwkt_replymsg((lwkt_msg_t)msg, 0);
1171 wakeup(td_core/*devfs_id*/);
1176 * Worker function to insert a new dev into the dev list and initialize its
1177 * permissions. It also calls devfs_propagate_dev which in turn propagates
1178 * the change to all mount points.
1180 * The passed dev is already referenced. This reference is eaten by this
1181 * function and represents the dev's linkage into devfs_dev_list.
1184 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1187 devfs_debug(DEVFS_DEBUG_DEBUG,
1188 "devfs_create_dev_worker -1- -%s- (%p)\n",
1193 dev->si_perms = perms;
1195 devfs_link_dev(dev);
1196 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_dev_worker -2-\n");
1197 devfs_propagate_dev(dev, 1);
1199 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_dev_worker -end:3-\n");
1204 * Worker function to delete a dev from the dev list and free the cdev.
1205 * It also calls devfs_propagate_dev which in turn propagates the change
1206 * to all mount points.
1209 devfs_destroy_dev_worker(cdev_t dev)
1214 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1216 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_destroy_dev_worker -1- %s\n", dev->si_name);
1217 error = devfs_unlink_dev(dev);
1218 devfs_propagate_dev(dev, 0);
1220 release_dev(dev); /* link ref */
1224 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_destroy_dev_worker -end:5-\n");
1229 * Worker function to destroy all devices with a certain basename.
1230 * Calls devfs_destroy_dev_worker for the actual destruction.
1233 devfs_destroy_subnames_worker(char *name)
1236 size_t len = strlen(name);
1238 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1239 if (!strncmp(dev->si_name, name, len)) {
1240 if (dev->si_name[len] != '\0') {
1241 devfs_destroy_dev_worker(dev);
1242 /* release_dev(dev); */
1250 devfs_clr_subnames_flag_worker(char *name, uint32_t flag)
1253 size_t len = strlen(name);
1255 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1256 if (!strncmp(dev->si_name, name, len)) {
1257 if (dev->si_name[len] != '\0') {
1258 dev->si_flags &= ~flag;
1267 devfs_destroy_subnames_without_flag_worker(char *name, uint32_t flag)
1270 size_t len = strlen(name);
1272 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1273 if (!strncmp(dev->si_name, name, len)) {
1274 if (dev->si_name[len] != '\0') {
1275 if (!(dev->si_flags & flag)) {
1276 devfs_destroy_dev_worker(dev);
1286 * Worker function that creates all device nodes on top of a devfs
1290 devfs_create_all_dev_worker(struct devfs_node *root)
1295 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_all_dev_worker -1-\n");
1297 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1298 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_all_dev_worker -loop:2- -%s-\n", dev->si_name);
1299 devfs_create_device_node(root, dev, NULL, NULL);
1301 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_all_dev_worker -end:3-\n");
1306 * Worker function that destroys all devices that match a specific
1307 * dev_ops and/or minor. If minor is less than 0, it is not matched
1308 * against. It also propagates all changes.
1311 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1316 devfs_debug(DEVFS_DEBUG_DEBUG,
1317 "devfs_destroy_dev_by_ops_worker -1-\n");
1319 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1320 if (dev->si_ops != ops)
1322 if ((minor < 0) || (dev->si_uminor == minor)) {
1323 devfs_debug(DEVFS_DEBUG_DEBUG,
1324 "devfs_destroy_dev_by_ops_worker "
1327 devfs_destroy_dev_worker(dev);
1330 devfs_debug(DEVFS_DEBUG_DEBUG,
1331 "devfs_destroy_dev_by_ops_worker -end:3-\n");
1336 * Worker function that registers a new clone handler in devfs.
1339 devfs_chandler_add_worker(char *name, d_clone_t *nhandler)
1341 struct devfs_clone_handler *chandler = NULL;
1342 u_char len = strlen(name);
1347 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1348 if (chandler->namlen == len) {
1349 if (!memcmp(chandler->name, name, len)) {
1350 /* Clonable basename already exists */
1356 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1357 memcpy(chandler->name, name, len+1);
1358 chandler->namlen = len;
1359 chandler->nhandler = nhandler;
1361 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1366 * Worker function that removes a given clone handler from the
1367 * clone handler list.
1370 devfs_chandler_del_worker(char *name)
1372 struct devfs_clone_handler *chandler, *chandler2;
1373 u_char len = strlen(name);
1378 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1379 if (chandler->namlen != len)
1381 if (memcmp(chandler->name, name, len))
1383 TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1384 kfree(chandler, M_DEVFS);
1391 * Worker function that finds a given device name and changes
1392 * the message received accordingly so that when replied to,
1393 * the answer is returned to the caller.
1396 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1399 cdev_t found = NULL;
1401 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1402 if (!strcmp(devfs_msg->mdv_name, dev->si_name)) {
1407 devfs_msg->mdv_cdev = found;
1413 * Worker function that finds a given device udev and changes
1414 * the message received accordingly so that when replied to,
1415 * the answer is returned to the caller.
1418 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg)
1421 cdev_t found = NULL;
1423 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1424 if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1429 devfs_msg->mdv_cdev = found;
1435 * Worker function that inserts a given alias into the
1436 * alias list, and propagates the alias to all mount
1440 devfs_make_alias_worker(struct devfs_alias *alias)
1442 struct devfs_alias *alias2;
1443 size_t len = strlen(alias->name);
1446 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1447 if (!memcmp(alias->name, alias2->name, len)) {
1454 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1455 devfs_alias_propagate(alias);
1457 devfs_debug(DEVFS_DEBUG_DEBUG,
1458 "Warning: duplicate devfs_make_alias for %s\n",
1460 kfree(alias, M_DEVFS);
1467 * Function that removes and frees all aliases.
1470 devfs_alias_reap(void)
1472 struct devfs_alias *alias, *alias2;
1474 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1475 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1476 kfree(alias, M_DEVFS);
1482 * Function that removes an alias matching a specific cdev and frees
1486 devfs_alias_remove(cdev_t dev)
1488 struct devfs_alias *alias, *alias2;
1490 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1491 if (alias->dev_target == dev) {
1492 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1493 kfree(alias, M_DEVFS);
1500 * This function propagates a new alias to all mount points.
1503 devfs_alias_propagate(struct devfs_alias *alias)
1505 struct devfs_mnt_data *mnt;
1507 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1508 devfs_alias_apply(mnt->root_node, alias);
1514 * This function is a recursive function iterating through
1515 * all device nodes in the topology and, if applicable,
1516 * creating the relevant alias for a device node.
1519 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1521 struct devfs_node *node1, *node2;
1523 KKASSERT(alias != NULL);
1525 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1526 devfs_debug(DEVFS_DEBUG_DEBUG, "This node is Pdir or Proot; has %d children\n", node->nchildren);
1527 if (node->nchildren > 2) {
1528 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1529 devfs_alias_apply(node1, alias);
1533 if (node->d_dev == alias->dev_target)
1534 devfs_alias_create(alias->name, node);
1540 * This function checks if any alias possibly is applicable
1541 * to the given node. If so, the alias is created.
1544 devfs_alias_check_create(struct devfs_node *node)
1546 struct devfs_alias *alias;
1548 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1549 if (node->d_dev == alias->dev_target)
1550 devfs_alias_create(alias->name, node);
1556 * This function creates an alias with a given name
1557 * linking to a given devfs node. It also increments
1558 * the link count on the target node.
1561 devfs_alias_create(char *name_orig, struct devfs_node *target)
1563 struct mount *mp = target->mp;
1564 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1565 struct devfs_node *linknode;
1566 char *create_path = NULL;
1567 char *name, name_buf[PATH_MAX];
1569 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1571 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1574 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1577 if (devfs_find_device_node_by_name(parent, name)) {
1578 devfs_debug(DEVFS_DEBUG_DEBUG,
1579 "Node already exists: %s "
1580 "(devfs_make_alias_worker)!\n",
1586 linknode = devfs_allocp(Plink, name, parent, mp, NULL);
1587 if (linknode == NULL)
1590 linknode->link_target = target;
1592 //linknode->flags |= DEVFS_LINK;
1598 * This function is called by the core and handles mount point
1599 * strings. It either calls the relevant worker (devfs_apply_
1600 * reset_rules_worker) on all mountpoints or only a specific
1604 devfs_apply_reset_rules_caller(char *mountto, int apply)
1607 struct devfs_mnt_data *mnt;
1608 size_t len = strlen(mountto);
1610 if (mountto[0] != '*') {
1611 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1612 if ((len == mnt->mntonnamelen) &&
1613 (!memcmp(mnt->mp->mnt_stat.f_mntonname, mountto, len))) {
1614 devfs_apply_reset_rules_worker(mnt->root_node, apply);
1619 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1620 devfs_apply_reset_rules_worker(mnt->root_node, apply);
1624 kfree(mountto, M_DEVFS);
1629 * This worker function applies or resets, depending on the arguments, a rule
1630 * to the whole given topology. *RECURSIVE*
1633 devfs_apply_reset_rules_worker(struct devfs_node *node, int apply)
1635 struct devfs_node *node1, *node2;
1637 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1638 devfs_debug(DEVFS_DEBUG_DEBUG, "This node is Pdir or Proot; has %d children\n", node->nchildren);
1639 if (node->nchildren > 2) {
1640 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1641 devfs_apply_reset_rules_worker(node1, apply);
1647 devfs_rule_check_apply(node);
1649 devfs_rule_reset_node(node);
1656 * This function calls a given callback function for
1657 * every dev node in the devfs dev list.
1660 devfs_scan_callback_worker(devfs_scan_t *callback)
1664 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_scan_callback: %p -1-\n", callback);
1666 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1670 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_scan_callback: finished\n");
1676 * This function tries to resolve a given directory, or if not
1677 * found and creation requested, creates the given directory.
1679 static struct devfs_node *
1680 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1681 size_t name_len, int create)
1683 struct devfs_node *node, *found = NULL;
1685 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1686 if (name_len == node->d_dir.d_namlen) {
1687 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1694 if ((found == NULL) && (create)) {
1695 found = devfs_allocp(Pdir, dir_name, parent, parent->mp, NULL);
1702 * This function tries to resolve a complete path. If creation is requested,
1703 * if a given part of the path cannot be resolved (because it doesn't exist),
1707 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1709 struct devfs_node *node = parent;
1718 for (; *path != '\0' ; path++) {
1723 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1730 return devfs_resolve_or_create_dir(node, buf, idx, create);
1734 * Takes a full path and strips it into a directory path and a name.
1735 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
1736 * requires a working buffer with enough size to keep the whole
1740 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
1744 size_t len = strlen(fullpath) + 1;
1747 KKASSERT((fullpath != NULL) && (buf != NULL) && (pathp != NULL) && (namep != NULL));
1749 memcpy(buf, fullpath, len);
1751 for (i = len-1; i>= 0; i--) {
1752 if (buf[i] == '/') {
1772 * This function creates a new devfs node for a given device. It can
1773 * handle a complete path as device name, and accordingly creates
1774 * the path and the final device node.
1776 * The reference count on the passed dev remains unchanged.
1779 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
1780 char *dev_name, char *path_fmt, ...)
1782 struct devfs_node *parent, *node = NULL;
1784 char *name, name_buf[PATH_MAX];
1788 char *create_path = NULL;
1789 char *names = "pqrsPQRS";
1791 if (path_fmt != NULL) {
1792 path = kmalloc(PATH_MAX+1, M_DEVFS, M_WAITOK);
1794 __va_start(ap, path_fmt);
1795 i = kvcprintf(path_fmt, NULL, path, 10, ap);
1800 parent = devfs_resolve_or_create_path(root, path, 1);
1803 devfs_resolve_name_path(((dev_name == NULL) && (dev))?(dev->si_name):(dev_name), name_buf, &create_path, &name);
1806 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1809 if (devfs_find_device_node_by_name(parent, name)) {
1810 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_device_node: DEVICE %s ALREADY EXISTS!!! Ignoring creation request.\n", name);
1813 devfs_debug(DEVFS_DEBUG_DEBUG, "parent->d_dir.d_name=%s\n", parent->d_dir.d_name);
1814 node = devfs_allocp(Pdev, name, parent, parent->mp, dev);
1815 devfs_debug(DEVFS_DEBUG_DEBUG, "node->d_dir.d_name=%s\n", node->d_dir.d_name);
1817 /* Ugly unix98 pty magic, to hide pty master (ptm) devices and their directory */
1818 if ((dev) && (strlen(dev->si_name) >= 4) && (!memcmp(dev->si_name, "ptm/", 4))) {
1819 //node->parent->flags |= DEVFS_HIDDEN;
1820 //node->flags |= DEVFS_HIDDEN;
1822 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_device_node: marker A\n");
1823 /* Ugly pty magic, to tag pty devices as such and hide them if needed */
1824 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
1825 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1827 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_device_node: marker B\n");
1828 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
1830 for (i = 0; i < strlen(names); i++) {
1831 if (name[3] == names[i]) {
1837 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1839 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_create_device_node: marker C\n");
1842 if (path_fmt != NULL)
1843 kfree(path, M_DEVFS);
1849 * This function finds a given device node in the topology with a given
1853 devfs_find_device_node(struct devfs_node *node, cdev_t target)
1855 struct devfs_node *node1, *node2, *found = NULL;
1857 if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1858 devfs_debug(DEVFS_DEBUG_DEBUG, "This node is Pdir or Proot; has %d children\n", node->nchildren);
1859 if (node->nchildren > 2) {
1860 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1861 if ((found = devfs_find_device_node(node1, target)))
1865 } else if (node->node_type == Pdev) {
1866 if (node->d_dev == target)
1869 //devfs_debug(DEVFS_DEBUG_DEBUG, "This node is called %s\n", (found)?found->d_dir.d_name:"NOTFOUND");
1875 * This function finds a device node in the topology by its
1876 * name and returns it.
1879 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
1881 struct devfs_node *node, *found = NULL;
1882 size_t len = strlen(target);
1884 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1885 if ((len == node->d_dir.d_namlen) && (!memcmp(node->d_dir.d_name, target, len))) {
1895 * This function takes a cdev and removes its devfs node in the
1896 * given topology. The cdev remains intact.
1899 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
1901 struct devfs_node *node, *parent;
1902 char *name, name_buf[PATH_MAX];
1903 char *create_path = NULL;
1907 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_destroy_device_node\n");
1908 memcpy(name_buf, target->si_name, strlen(target->si_name)+1);
1910 devfs_resolve_name_path(target->si_name, name_buf, &create_path, &name);
1911 devfs_debug(DEVFS_DEBUG_DEBUG, "create_path: %s\n", create_path);
1912 devfs_debug(DEVFS_DEBUG_DEBUG, "name: %s\n", name);
1915 parent = devfs_resolve_or_create_path(root, create_path, 0);
1918 devfs_debug(DEVFS_DEBUG_DEBUG, "-> marker <-\n");
1921 devfs_debug(DEVFS_DEBUG_DEBUG, "->d_dir.d_name=%s\n", parent->d_dir.d_name);
1922 node = devfs_find_device_node_by_name(parent, name);
1923 devfs_debug(DEVFS_DEBUG_DEBUG,
1924 "->d_dir.d_name=%s\n",
1925 ((node) ? (node->d_dir.d_name) : "SHIT!"));
1933 * Just set perms and ownership for given node.
1936 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid, u_short mode, u_long flags)
1938 node->mode = mode; /* files access mode and type */
1939 node->uid = uid; /* owner user id */
1940 node->gid = gid; /* owner group id */
1941 //node->flags = flags;
1947 * Propagates a device attach/detach to all mount
1948 * points. Also takes care of automatic alias removal
1949 * for a deleted cdev.
1952 devfs_propagate_dev(cdev_t dev, int attach)
1954 struct devfs_mnt_data *mnt;
1956 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_propagate_dev -1-\n");
1957 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1958 devfs_debug(DEVFS_DEBUG_DEBUG,
1959 "devfs_propagate_dev -loop:2-\n");
1961 /* Device is being attached */
1962 devfs_create_device_node(mnt->root_node, dev,
1965 /* Device is being detached */
1966 devfs_alias_remove(dev);
1967 devfs_destroy_device_node(mnt->root_node, dev);
1970 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_propagate_dev -end:3-\n");
1975 * devfs_node_to_path takes a node and a buffer of a size of
1976 * at least PATH_MAX, resolves the full path from the root
1977 * node and writes it in a humanly-readable format into the
1979 * If DEVFS_STASH_DEPTH is less than the directory level up
1980 * to the root node, only the last DEVFS_STASH_DEPTH levels
1981 * of the path are resolved.
1984 devfs_node_to_path(struct devfs_node *node, char *buffer)
1986 #define DEVFS_STASH_DEPTH 32
1987 struct devfs_node *node_stash[DEVFS_STASH_DEPTH];
1989 memset(buffer, 0, PATH_MAX);
1991 for (i = 0; (i < DEVFS_STASH_DEPTH) && (node->node_type != Proot); i++) {
1992 node_stash[i] = node;
1993 node = node->parent;
1997 for (offset = 0; i >= 0; i--) {
1998 memcpy(buffer+offset, node_stash[i]->d_dir.d_name, node_stash[i]->d_dir.d_namlen);
1999 offset += node_stash[i]->d_dir.d_namlen;
2001 *(buffer+offset) = '/';
2005 #undef DEVFS_STASH_DEPTH
2010 * devfs_clone either returns a basename from a complete name by
2011 * returning the length of the name without trailing digits, or,
2012 * if clone != 0, calls the device's clone handler to get a new
2013 * device, which in turn is returned in devp.
2016 devfs_clone(char *name, size_t *namlenp, cdev_t *devp, int clone, struct ucred *cred)
2020 size_t len = *namlenp;
2022 struct devfs_clone_handler *chandler;
2023 struct dev_clone_args ap;
2026 for (; (len > 0) && (DEVFS_ISDIGIT(name[len-1])); len--);
2029 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
2030 devfs_debug(DEVFS_DEBUG_DEBUG, "len=%d, chandler->namlen=%d\n", len, chandler->namlen);
2031 devfs_debug(DEVFS_DEBUG_DEBUG, "name=%s, chandler->name=%s\n", name, chandler->name);
2032 if ((chandler->namlen == len) &&
2033 (!memcmp(chandler->name, name, len)) &&
2034 (chandler->nhandler)) {
2035 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nclone: found clone handler for the base name at %p\n", chandler->nhandler);
2041 error = (chandler->nhandler)(&ap);
2058 * Registers a new orphan in the orphan list.
2061 devfs_tracer_add_orphan(struct devfs_node *node)
2063 struct devfs_orphan *orphan;
2066 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2067 orphan->node = node;
2069 KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2070 node->flags |= DEVFS_ORPHANED;
2071 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2075 * Removes an orphan from the orphan list.
2078 devfs_tracer_del_orphan(struct devfs_node *node)
2080 struct devfs_orphan *orphan;
2084 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) {
2085 if (orphan->node == node) {
2086 node->flags &= ~DEVFS_ORPHANED;
2087 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2088 kfree(orphan, M_DEVFS);
2095 * Counts the orphans in the orphan list, and if cleanup
2096 * is specified, also frees the orphan and removes it from
2100 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2102 struct devfs_orphan *orphan, *orphan2;
2105 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) {
2108 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2109 orphan->node->flags &= ~DEVFS_ORPHANED;
2110 devfs_freep(orphan->node);
2111 kfree(orphan, M_DEVFS);
2119 * Fetch an ino_t from the global d_ino by increasing it
2123 devfs_fetch_ino(void)
2127 spin_lock_wr(&ino_lock);
2129 spin_unlock_wr(&ino_lock);
2135 * Allocates a new cdev and initializes it's most basic
2139 devfs_new_cdev(struct dev_ops *ops, int minor)
2141 cdev_t dev = sysref_alloc(&cdev_sysref_class);
2142 sysref_activate(&dev->si_sysref);
2144 devfs_debug(DEVFS_DEBUG_DEBUG,
2145 "new_cdev: clearing first %d bytes\n",
2146 offsetof(struct cdev, si_sysref));
2147 memset(dev, 0, offsetof(struct cdev, si_sysref));
2152 dev->si_drv1 = NULL;
2153 dev->si_drv2 = NULL;
2154 dev->si_lastread = 0; /* time_second */
2155 dev->si_lastwrite = 0; /* time_second */
2158 dev->si_flags = SI_HASHED | SI_ADHOC; //XXX: any real use?
2160 dev->si_uminor = minor;
2161 dev->si_inode = devfs_fetch_ino();
2167 devfs_cdev_terminate(cdev_t dev)
2171 /* Check if it is locked already. if not, we acquire the devfs lock */
2172 if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) {
2173 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2177 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_cdev_terminate: Taking care of dev->si_name=%s\n", dev->si_name);
2179 /* Propagate destruction, just in case */
2180 devfs_propagate_dev(dev, 0);
2182 /* If we acquired the lock, we also get rid of it */
2184 lockmgr(&devfs_lock, LK_RELEASE);
2186 /* Finally destroy the device */
2187 sysref_put(&dev->si_sysref);
2191 * Links a given cdev into the dev list.
2194 devfs_link_dev(cdev_t dev)
2196 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2197 dev->si_flags |= SI_DEVFS_LINKED;
2198 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2204 * Removes a given cdev from the dev list. The caller is responsible for
2205 * releasing the reference on the device associated with the linkage.
2207 * Returns EALREADY if the dev has already been unlinked.
2210 devfs_unlink_dev(cdev_t dev)
2212 if ((dev->si_flags & SI_DEVFS_LINKED)) {
2213 TAILQ_REMOVE(&devfs_dev_list, dev, link);
2214 dev->si_flags &= ~SI_DEVFS_LINKED;
2221 devfs_config(void *arg)
2225 msg = devfs_msg_get();
2227 kprintf("devfs_config: sync'ing up\n");
2228 msg = devfs_msg_send_sync(DEVFS_SYNC, msg);
2233 * Called on init of devfs; creates the objcaches and
2234 * spawns off the devfs core thread. Also initializes
2240 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2241 /* Create objcaches for nodes, msgs and devs */
2242 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2244 objcache_malloc_alloc,
2245 objcache_malloc_free,
2246 &devfs_node_malloc_args );
2248 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2250 objcache_malloc_alloc,
2251 objcache_malloc_free,
2252 &devfs_msg_malloc_args );
2254 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2256 objcache_malloc_alloc,
2257 objcache_malloc_free,
2258 &devfs_dev_malloc_args );
2260 /* Initialize the reply-only port which acts as a message drain */
2261 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2263 /* Initialize *THE* devfs lock */
2264 lockinit(&devfs_lock, "devfs_core lock", 0, 0);
2267 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2268 0, 0, "devfs_msg_core");
2270 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2272 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2276 * Called on unload of devfs; takes care of destroying the core
2277 * and the objcaches. Also removes aliases that are no longer needed.
2282 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2284 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2286 tsleep(td_core/*devfs_id*/, 0, "devfsc", 0);
2287 tsleep(td_core/*devfs_id*/, 0, "devfsc", 10000);
2289 /* Destroy the objcaches */
2290 objcache_destroy(devfs_msg_cache);
2291 objcache_destroy(devfs_node_cache);
2292 objcache_destroy(devfs_dev_cache);
2298 * This is a sysctl handler to assist userland devname(3) to
2299 * find the device name for a given udev.
2302 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2309 if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t))))
2312 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev);
2317 if ((found = devfs_find_device_by_udev(udev)) == NULL)
2320 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2324 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
2325 NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)");
2327 static SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2328 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2329 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable, 0, "Enable DevFS debugging");
2331 SYSINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, devfs_init, NULL);
2332 SYSUNINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, devfs_uninit, NULL);