kernel - Incidental MPLOCK removal (devfs)
[dragonfly.git] / sys / vfs / devfs / devfs_core.c
blob040b956d06be6494555d8159076cb34ba754b6b7
1 /*
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
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/bus.h>
38 #include <sys/mount.h>
39 #include <sys/vnode.h>
40 #include <sys/types.h>
41 #include <sys/lock.h>
42 #include <sys/file.h>
43 #include <sys/msgport.h>
44 #include <sys/sysctl.h>
45 #include <sys/ucred.h>
46 #include <sys/devfs.h>
47 #include <sys/devfs_rules.h>
48 #include <sys/udev.h>
50 #include <sys/msgport2.h>
51 #include <sys/spinlock2.h>
52 #include <sys/sysref2.h>
54 MALLOC_DEFINE(M_DEVFS, "devfs", "Device File System (devfs) allocations");
55 DEVFS_DEFINE_CLONE_BITMAP(ops_id);
57 * SYSREF Integration - reference counting, allocation,
58 * sysid and syslink integration.
60 static void devfs_cdev_terminate(cdev_t dev);
61 static void devfs_cdev_lock(cdev_t dev);
62 static void devfs_cdev_unlock(cdev_t dev);
63 static struct sysref_class cdev_sysref_class = {
64 .name = "cdev",
65 .mtype = M_DEVFS,
66 .proto = SYSREF_PROTO_DEV,
67 .offset = offsetof(struct cdev, si_sysref),
68 .objsize = sizeof(struct cdev),
69 .nom_cache = 32,
70 .flags = 0,
71 .ops = {
72 .terminate = (sysref_terminate_func_t)devfs_cdev_terminate,
73 .lock = (sysref_lock_func_t)devfs_cdev_lock,
74 .unlock = (sysref_unlock_func_t)devfs_cdev_unlock
78 static struct objcache *devfs_node_cache;
79 static struct objcache *devfs_msg_cache;
80 static struct objcache *devfs_dev_cache;
82 static struct objcache_malloc_args devfs_node_malloc_args = {
83 sizeof(struct devfs_node), M_DEVFS };
84 struct objcache_malloc_args devfs_msg_malloc_args = {
85 sizeof(struct devfs_msg), M_DEVFS };
86 struct objcache_malloc_args devfs_dev_malloc_args = {
87 sizeof(struct cdev), M_DEVFS };
89 static struct devfs_dev_head devfs_dev_list =
90 TAILQ_HEAD_INITIALIZER(devfs_dev_list);
91 static struct devfs_mnt_head devfs_mnt_list =
92 TAILQ_HEAD_INITIALIZER(devfs_mnt_list);
93 static struct devfs_chandler_head devfs_chandler_list =
94 TAILQ_HEAD_INITIALIZER(devfs_chandler_list);
95 static struct devfs_alias_head devfs_alias_list =
96 TAILQ_HEAD_INITIALIZER(devfs_alias_list);
97 static struct devfs_dev_ops_head devfs_dev_ops_list =
98 TAILQ_HEAD_INITIALIZER(devfs_dev_ops_list);
100 struct lock devfs_lock;
101 struct lwkt_token devfs_token;
102 static struct lwkt_port devfs_dispose_port;
103 static struct lwkt_port devfs_msg_port;
104 static struct thread *td_core;
106 static struct spinlock ino_lock;
107 static ino_t d_ino;
108 static int devfs_debug_enable;
109 static int devfs_run;
111 static ino_t devfs_fetch_ino(void);
112 static int devfs_create_all_dev_worker(struct devfs_node *);
113 static int devfs_create_dev_worker(cdev_t, uid_t, gid_t, int);
114 static int devfs_destroy_dev_worker(cdev_t);
115 static int devfs_destroy_related_worker(cdev_t);
116 static int devfs_destroy_dev_by_ops_worker(struct dev_ops *, int);
117 static int devfs_propagate_dev(cdev_t, int);
118 static int devfs_unlink_dev(cdev_t dev);
119 static void devfs_msg_exec(devfs_msg_t msg);
121 static int devfs_chandler_add_worker(const char *, d_clone_t *);
122 static int devfs_chandler_del_worker(const char *);
124 static void devfs_msg_autofree_reply(lwkt_port_t, lwkt_msg_t);
125 static void devfs_msg_core(void *);
127 static int devfs_find_device_by_name_worker(devfs_msg_t);
128 static int devfs_find_device_by_udev_worker(devfs_msg_t);
130 static int devfs_apply_reset_rules_caller(char *, int);
132 static int devfs_scan_callback_worker(devfs_scan_t *, void *);
134 static struct devfs_node *devfs_resolve_or_create_dir(struct devfs_node *,
135 char *, size_t, int);
137 static int devfs_make_alias_worker(struct devfs_alias *);
138 static int devfs_destroy_alias_worker(struct devfs_alias *);
139 static int devfs_alias_remove(cdev_t);
140 static int devfs_alias_reap(void);
141 static int devfs_alias_propagate(struct devfs_alias *, int);
142 static int devfs_alias_apply(struct devfs_node *, struct devfs_alias *);
143 static int devfs_alias_check_create(struct devfs_node *);
145 static int devfs_clr_related_flag_worker(cdev_t, uint32_t);
146 static int devfs_destroy_related_without_flag_worker(cdev_t, uint32_t);
148 static void *devfs_reaperp_callback(struct devfs_node *, void *);
149 static void *devfs_gc_dirs_callback(struct devfs_node *, void *);
150 static void *devfs_gc_links_callback(struct devfs_node *, struct devfs_node *);
151 static void *
152 devfs_inode_to_vnode_worker_callback(struct devfs_node *, ino_t *);
155 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function
156 * using kvprintf
159 devfs_debug(int level, char *fmt, ...)
161 __va_list ap;
163 __va_start(ap, fmt);
164 if (level <= devfs_debug_enable)
165 kvprintf(fmt, ap);
166 __va_end(ap);
168 return 0;
172 * devfs_allocp() Allocates a new devfs node with the specified
173 * parameters. The node is also automatically linked into the topology
174 * if a parent is specified. It also calls the rule and alias stuff to
175 * be applied on the new node
177 struct devfs_node *
178 devfs_allocp(devfs_nodetype devfsnodetype, char *name,
179 struct devfs_node *parent, struct mount *mp, cdev_t dev)
181 struct devfs_node *node = NULL;
182 size_t namlen = strlen(name);
184 node = objcache_get(devfs_node_cache, M_WAITOK);
185 bzero(node, sizeof(*node));
187 atomic_add_long(&DEVFS_MNTDATA(mp)->leak_count, 1);
189 node->d_dev = NULL;
190 node->nchildren = 1;
191 node->mp = mp;
192 node->d_dir.d_ino = devfs_fetch_ino();
195 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries
196 * respectively.
198 node->cookie_jar = 2;
201 * Access Control members
203 node->mode = DEVFS_DEFAULT_MODE;
204 node->uid = DEVFS_DEFAULT_UID;
205 node->gid = DEVFS_DEFAULT_GID;
207 switch (devfsnodetype) {
208 case Nroot:
210 * Ensure that we don't recycle the root vnode by marking it as
211 * linked into the topology.
213 node->flags |= DEVFS_NODE_LINKED;
214 case Ndir:
215 TAILQ_INIT(DEVFS_DENODE_HEAD(node));
216 node->d_dir.d_type = DT_DIR;
217 node->nchildren = 2;
218 break;
220 case Nlink:
221 node->d_dir.d_type = DT_LNK;
222 break;
224 case Nreg:
225 node->d_dir.d_type = DT_REG;
226 break;
228 case Ndev:
229 if (dev != NULL) {
230 node->d_dir.d_type = DT_CHR;
231 node->d_dev = dev;
233 node->mode = dev->si_perms;
234 node->uid = dev->si_uid;
235 node->gid = dev->si_gid;
237 devfs_alias_check_create(node);
239 break;
241 default:
242 panic("devfs_allocp: unknown node type");
245 node->v_node = NULL;
246 node->node_type = devfsnodetype;
248 /* Initialize the dirent structure of each devfs vnode */
249 node->d_dir.d_namlen = namlen;
250 node->d_dir.d_name = kmalloc(namlen+1, M_DEVFS, M_WAITOK);
251 memcpy(node->d_dir.d_name, name, namlen);
252 node->d_dir.d_name[namlen] = '\0';
254 /* Initialize the parent node element */
255 node->parent = parent;
257 /* Initialize *time members */
258 nanotime(&node->atime);
259 node->mtime = node->ctime = node->atime;
262 * Associate with parent as last step, clean out namecache
263 * reference.
265 if ((parent != NULL) &&
266 ((parent->node_type == Nroot) || (parent->node_type == Ndir))) {
267 parent->nchildren++;
268 node->cookie = parent->cookie_jar++;
269 node->flags |= DEVFS_NODE_LINKED;
270 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent), node, link);
272 /* This forces negative namecache lookups to clear */
273 ++mp->mnt_namecache_gen;
277 * Apply rules (requires root node, skip if we are creating the root node)
279 if (DEVFS_MNTDATA(mp)->root_node)
280 devfs_rule_check_apply(node, NULL);
282 atomic_add_long(&DEVFS_MNTDATA(mp)->file_count, 1);
284 return node;
288 * devfs_allocv() allocates a new vnode based on a devfs node.
291 devfs_allocv(struct vnode **vpp, struct devfs_node *node)
293 struct vnode *vp;
294 int error = 0;
296 KKASSERT(node);
299 * devfs master lock must not be held across a vget() call, we have
300 * to hold our ad-hoc vp to avoid a free race from destroying the
301 * contents of the structure. The vget() will interlock recycles
302 * for us.
304 try_again:
305 while ((vp = node->v_node) != NULL) {
306 vhold(vp);
307 lockmgr(&devfs_lock, LK_RELEASE);
308 error = vget(vp, LK_EXCLUSIVE);
309 vdrop(vp);
310 lockmgr(&devfs_lock, LK_EXCLUSIVE);
311 if (error == 0) {
312 *vpp = vp;
313 goto out;
315 if (error != ENOENT) {
316 *vpp = NULL;
317 goto out;
322 * devfs master lock must not be held across a getnewvnode() call.
324 lockmgr(&devfs_lock, LK_RELEASE);
325 if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0) {
326 lockmgr(&devfs_lock, LK_EXCLUSIVE);
327 goto out;
329 lockmgr(&devfs_lock, LK_EXCLUSIVE);
331 vp = *vpp;
333 if (node->v_node != NULL) {
334 vp->v_type = VBAD;
335 vx_put(vp);
336 goto try_again;
339 vp->v_data = node;
340 node->v_node = vp;
342 switch (node->node_type) {
343 case Nroot:
344 vsetflags(vp, VROOT);
345 /* fall through */
346 case Ndir:
347 vp->v_type = VDIR;
348 break;
350 case Nlink:
351 vp->v_type = VLNK;
352 break;
354 case Nreg:
355 vp->v_type = VREG;
356 break;
358 case Ndev:
359 vp->v_type = VCHR;
360 KKASSERT(node->d_dev);
362 vp->v_uminor = node->d_dev->si_uminor;
363 vp->v_umajor = node->d_dev->si_umajor;
365 v_associate_rdev(vp, node->d_dev);
366 vp->v_ops = &node->mp->mnt_vn_spec_ops;
367 break;
369 default:
370 panic("devfs_allocv: unknown node type");
373 out:
374 return error;
378 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
379 * based on the newly created devfs node.
382 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype,
383 char *name, struct devfs_node *parent, cdev_t dev)
385 struct devfs_node *node;
387 node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
389 if (node != NULL)
390 devfs_allocv(vpp, node);
391 else
392 *vpp = NULL;
394 return 0;
398 * Destroy the devfs_node. The node must be unlinked from the topology.
400 * This function will also destroy any vnode association with the node
401 * and device.
403 * The cdev_t itself remains intact.
405 * The core lock is not necessarily held on call and must be temporarily
406 * released if it is to avoid a deadlock.
409 devfs_freep(struct devfs_node *node)
411 struct vnode *vp;
412 int relock;
414 KKASSERT(node);
415 KKASSERT(((node->flags & DEVFS_NODE_LINKED) == 0) ||
416 (node->node_type == Nroot));
419 * Protect against double frees
421 KKASSERT((node->flags & DEVFS_DESTROYED) == 0);
422 node->flags |= DEVFS_DESTROYED;
425 * Avoid deadlocks between devfs_lock and the vnode lock when
426 * disassociating the vnode (stress2 pty vs ls -la /dev/pts).
428 * This also prevents the vnode reclaim code from double-freeing
429 * the node. The vget() is required to safely modified the vp
430 * and cycle the refs to terminate an inactive vp.
432 if (lockstatus(&devfs_lock, curthread) == LK_EXCLUSIVE) {
433 lockmgr(&devfs_lock, LK_RELEASE);
434 relock = 1;
435 } else {
436 relock = 0;
439 while ((vp = node->v_node) != NULL) {
440 if (vget(vp, LK_EXCLUSIVE | LK_RETRY) != 0)
441 break;
442 v_release_rdev(vp);
443 vp->v_data = NULL;
444 node->v_node = NULL;
445 vput(vp);
449 * Remaining cleanup
451 atomic_subtract_long(&DEVFS_MNTDATA(node->mp)->leak_count, 1);
452 if (node->symlink_name) {
453 kfree(node->symlink_name, M_DEVFS);
454 node->symlink_name = NULL;
458 * Remove the node from the orphan list if it is still on it.
460 if (node->flags & DEVFS_ORPHANED)
461 devfs_tracer_del_orphan(node);
463 if (node->d_dir.d_name) {
464 kfree(node->d_dir.d_name, M_DEVFS);
465 node->d_dir.d_name = NULL;
467 atomic_subtract_long(&DEVFS_MNTDATA(node->mp)->file_count, 1);
468 objcache_put(devfs_node_cache, node);
470 if (relock)
471 lockmgr(&devfs_lock, LK_EXCLUSIVE);
473 return 0;
477 * Returns a valid vp associated with the devfs alias node or NULL
479 static void *devfs_alias_getvp(struct devfs_node *node)
481 struct devfs_node *found = node;
482 int depth = 0;
484 while ((found->node_type == Nlink) && (found->link_target)) {
485 if (depth >= 8) {
486 devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8");
487 break;
490 found = found->link_target;
491 ++depth;
494 return found->v_node;
498 * Unlink the devfs node from the topology and add it to the orphan list.
499 * The node will later be destroyed by freep.
501 * Any vnode association, including the v_rdev and v_data, remains intact
502 * until the freep.
505 devfs_unlinkp(struct devfs_node *node)
507 struct vnode *vp;
508 struct devfs_node *parent;
509 KKASSERT(node);
512 * Add the node to the orphan list, so it is referenced somewhere, to
513 * so we don't leak it.
515 devfs_tracer_add_orphan(node);
517 parent = node->parent;
520 * If the parent is known we can unlink the node out of the topology
522 if (parent) {
523 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
524 parent->nchildren--;
525 node->flags &= ~DEVFS_NODE_LINKED;
528 node->parent = NULL;
531 * Namecache invalidation.
532 * devfs alias nodes are special: their v_node entry is always null
533 * and they use the one from their link target.
534 * We thus use the target node's vp to invalidate both alias and target
535 * entries in the namecache.
536 * Doing so for the target is not necessary but it would be more
537 * expensive to resolve only the namecache entry of the alias node
538 * from the information available in this function.
540 if (node->node_type == Nlink)
541 vp = devfs_alias_getvp(node);
542 else
543 vp = node->v_node;
545 if (vp != NULL)
546 cache_inval_vp(vp, CINV_DESTROY);
548 return 0;
551 void *
552 devfs_iterate_topology(struct devfs_node *node,
553 devfs_iterate_callback_t *callback, void *arg1)
555 struct devfs_node *node1, *node2;
556 void *ret = NULL;
558 if ((node->node_type == Nroot) || (node->node_type == Ndir)) {
559 if (node->nchildren > 2) {
560 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
561 link, node2) {
562 if ((ret = devfs_iterate_topology(node1, callback, arg1)))
563 return ret;
568 ret = callback(node, arg1);
569 return ret;
572 static void *
573 devfs_alias_reaper_callback(struct devfs_node *node, void *unused)
575 if (node->node_type == Nlink) {
576 devfs_unlinkp(node);
577 devfs_freep(node);
580 return NULL;
584 * devfs_reaperp() is a recursive function that iterates through all the
585 * topology, unlinking and freeing all devfs nodes.
587 static void *
588 devfs_reaperp_callback(struct devfs_node *node, void *unused)
590 devfs_unlinkp(node);
591 devfs_freep(node);
593 return NULL;
596 static void *
597 devfs_gc_dirs_callback(struct devfs_node *node, void *unused)
599 if (node->node_type == Ndir) {
600 if ((node->nchildren == 2) &&
601 !(node->flags & DEVFS_USER_CREATED)) {
602 devfs_unlinkp(node);
603 devfs_freep(node);
607 return NULL;
610 static void *
611 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target)
613 if ((node->node_type == Nlink) && (node->link_target == target)) {
614 devfs_unlinkp(node);
615 devfs_freep(node);
618 return NULL;
622 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
623 * freeing a node, but also removes empty directories and links that link
624 * via devfs auto-link mechanism to the node being deleted.
627 devfs_gc(struct devfs_node *node)
629 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
631 if (node->nlinks > 0)
632 devfs_iterate_topology(root_node,
633 (devfs_iterate_callback_t *)devfs_gc_links_callback, node);
635 devfs_unlinkp(node);
636 devfs_iterate_topology(root_node,
637 (devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL);
639 devfs_freep(node);
641 return 0;
645 * devfs_create_dev() is the asynchronous entry point for device creation.
646 * It just sends a message with the relevant details to the devfs core.
648 * This function will reference the passed device. The reference is owned
649 * by devfs and represents all of the device's node associations.
652 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
654 reference_dev(dev);
655 devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
657 return 0;
661 * devfs_destroy_dev() is the asynchronous entry point for device destruction.
662 * It just sends a message with the relevant details to the devfs core.
665 devfs_destroy_dev(cdev_t dev)
667 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
668 return 0;
672 * devfs_mount_add() is the synchronous entry point for adding a new devfs
673 * mount. It sends a synchronous message with the relevant details to the
674 * devfs core.
677 devfs_mount_add(struct devfs_mnt_data *mnt)
679 devfs_msg_t msg;
681 msg = devfs_msg_get();
682 msg->mdv_mnt = mnt;
683 devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
684 devfs_msg_put(msg);
686 return 0;
690 * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
691 * It sends a synchronous message with the relevant details to the devfs core.
694 devfs_mount_del(struct devfs_mnt_data *mnt)
696 devfs_msg_t msg;
698 msg = devfs_msg_get();
699 msg->mdv_mnt = mnt;
700 devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
701 devfs_msg_put(msg);
703 return 0;
707 * devfs_destroy_related() is the synchronous entry point for device
708 * destruction by subname. It just sends a message with the relevant details to
709 * the devfs core.
712 devfs_destroy_related(cdev_t dev)
714 devfs_msg_t msg;
716 msg = devfs_msg_get();
717 msg->mdv_load = dev;
718 devfs_msg_send_sync(DEVFS_DESTROY_RELATED, msg);
719 devfs_msg_put(msg);
720 return 0;
724 devfs_clr_related_flag(cdev_t dev, uint32_t flag)
726 devfs_msg_t msg;
728 msg = devfs_msg_get();
729 msg->mdv_flags.dev = dev;
730 msg->mdv_flags.flag = flag;
731 devfs_msg_send_sync(DEVFS_CLR_RELATED_FLAG, msg);
732 devfs_msg_put(msg);
734 return 0;
738 devfs_destroy_related_without_flag(cdev_t dev, uint32_t flag)
740 devfs_msg_t msg;
742 msg = devfs_msg_get();
743 msg->mdv_flags.dev = dev;
744 msg->mdv_flags.flag = flag;
745 devfs_msg_send_sync(DEVFS_DESTROY_RELATED_WO_FLAG, msg);
746 devfs_msg_put(msg);
748 return 0;
752 * devfs_create_all_dev is the asynchronous entry point to trigger device
753 * node creation. It just sends a message with the relevant details to
754 * the devfs core.
757 devfs_create_all_dev(struct devfs_node *root)
759 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
760 return 0;
764 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
765 * devices with a specific set of dev_ops and minor. It just sends a
766 * message with the relevant details to the devfs core.
769 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
771 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
772 return 0;
776 * devfs_clone_handler_add is the synchronous entry point to add a new
777 * clone handler. It just sends a message with the relevant details to
778 * the devfs core.
781 devfs_clone_handler_add(const char *name, d_clone_t *nhandler)
783 devfs_msg_t msg;
785 msg = devfs_msg_get();
786 msg->mdv_chandler.name = name;
787 msg->mdv_chandler.nhandler = nhandler;
788 devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg);
789 devfs_msg_put(msg);
790 return 0;
794 * devfs_clone_handler_del is the synchronous entry point to remove a
795 * clone handler. It just sends a message with the relevant details to
796 * the devfs core.
799 devfs_clone_handler_del(const char *name)
801 devfs_msg_t msg;
803 msg = devfs_msg_get();
804 msg->mdv_chandler.name = name;
805 msg->mdv_chandler.nhandler = NULL;
806 devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg);
807 devfs_msg_put(msg);
808 return 0;
812 * devfs_find_device_by_name is the synchronous entry point to find a
813 * device given its name. It sends a synchronous message with the
814 * relevant details to the devfs core and returns the answer.
816 cdev_t
817 devfs_find_device_by_name(const char *fmt, ...)
819 cdev_t found = NULL;
820 devfs_msg_t msg;
821 char *target;
822 __va_list ap;
824 if (fmt == NULL)
825 return NULL;
827 __va_start(ap, fmt);
828 kvasnrprintf(&target, PATH_MAX, 10, fmt, ap);
829 __va_end(ap);
831 msg = devfs_msg_get();
832 msg->mdv_name = target;
833 devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
834 found = msg->mdv_cdev;
835 devfs_msg_put(msg);
836 kvasfree(&target);
838 return found;
842 * devfs_find_device_by_udev is the synchronous entry point to find a
843 * device given its udev number. It sends a synchronous message with
844 * the relevant details to the devfs core and returns the answer.
846 cdev_t
847 devfs_find_device_by_udev(udev_t udev)
849 cdev_t found = NULL;
850 devfs_msg_t msg;
852 msg = devfs_msg_get();
853 msg->mdv_udev = udev;
854 devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg);
855 found = msg->mdv_cdev;
856 devfs_msg_put(msg);
858 devfs_debug(DEVFS_DEBUG_DEBUG,
859 "devfs_find_device_by_udev found? %s -end:3-\n",
860 ((found) ? found->si_name:"NO"));
861 return found;
864 struct vnode *
865 devfs_inode_to_vnode(struct mount *mp, ino_t target)
867 struct vnode *vp = NULL;
868 devfs_msg_t msg;
870 if (mp == NULL)
871 return NULL;
873 msg = devfs_msg_get();
874 msg->mdv_ino.mp = mp;
875 msg->mdv_ino.ino = target;
876 devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg);
877 vp = msg->mdv_ino.vp;
878 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
879 devfs_msg_put(msg);
881 return vp;
885 * devfs_make_alias is the asynchronous entry point to register an alias
886 * for a device. It just sends a message with the relevant details to the
887 * devfs core.
890 devfs_make_alias(const char *name, cdev_t dev_target)
892 struct devfs_alias *alias;
893 size_t len;
895 len = strlen(name);
897 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
898 alias->name = kstrdup(name, M_DEVFS);
899 alias->namlen = len;
900 alias->dev_target = dev_target;
902 devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
903 return 0;
907 * devfs_destroy_alias is the asynchronous entry point to deregister an alias
908 * for a device. It just sends a message with the relevant details to the
909 * devfs core.
912 devfs_destroy_alias(const char *name, cdev_t dev_target)
914 struct devfs_alias *alias;
915 size_t len;
917 len = strlen(name);
919 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
920 alias->name = kstrdup(name, M_DEVFS);
921 alias->namlen = len;
922 alias->dev_target = dev_target;
924 devfs_msg_send_generic(DEVFS_DESTROY_ALIAS, alias);
925 return 0;
929 * devfs_apply_rules is the asynchronous entry point to trigger application
930 * of all rules. It just sends a message with the relevant details to the
931 * devfs core.
934 devfs_apply_rules(char *mntto)
936 char *new_name;
938 new_name = kstrdup(mntto, M_DEVFS);
939 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
941 return 0;
945 * devfs_reset_rules is the asynchronous entry point to trigger reset of all
946 * rules. It just sends a message with the relevant details to the devfs core.
949 devfs_reset_rules(char *mntto)
951 char *new_name;
953 new_name = kstrdup(mntto, M_DEVFS);
954 devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
956 return 0;
961 * devfs_scan_callback is the asynchronous entry point to call a callback
962 * on all cdevs.
963 * It just sends a message with the relevant details to the devfs core.
966 devfs_scan_callback(devfs_scan_t *callback, void *arg)
968 devfs_msg_t msg;
970 KKASSERT(callback);
972 msg = devfs_msg_get();
973 msg->mdv_load = callback;
974 msg->mdv_load2 = arg;
975 devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
976 devfs_msg_put(msg);
978 return 0;
983 * Acts as a message drain. Any message that is replied to here gets destroyed
984 * and the memory freed.
986 static void
987 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
989 devfs_msg_put((devfs_msg_t)msg);
993 * devfs_msg_get allocates a new devfs msg and returns it.
995 devfs_msg_t
996 devfs_msg_get(void)
998 return objcache_get(devfs_msg_cache, M_WAITOK);
1002 * devfs_msg_put deallocates a given devfs msg.
1005 devfs_msg_put(devfs_msg_t msg)
1007 objcache_put(devfs_msg_cache, msg);
1008 return 0;
1012 * devfs_msg_send is the generic asynchronous message sending facility
1013 * for devfs. By default the reply port is the automatic disposal port.
1015 * If the current thread is the devfs_msg_port thread we execute the
1016 * operation synchronously.
1018 void
1019 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
1021 lwkt_port_t port = &devfs_msg_port;
1023 lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
1025 devfs_msg->hdr.u.ms_result = cmd;
1027 if (port->mpu_td == curthread) {
1028 devfs_msg_exec(devfs_msg);
1029 lwkt_replymsg(&devfs_msg->hdr, 0);
1030 } else {
1031 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
1036 * devfs_msg_send_sync is the generic synchronous message sending
1037 * facility for devfs. It initializes a local reply port and waits
1038 * for the core's answer. The core will write the answer on the same
1039 * message which is sent back as reply. The caller still has a reference
1040 * to the message, so we don't need to return it.
1043 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
1045 struct lwkt_port rep_port;
1046 int error;
1047 lwkt_port_t port = &devfs_msg_port;
1049 lwkt_initport_thread(&rep_port, curthread);
1050 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
1052 devfs_msg->hdr.u.ms_result = cmd;
1054 error = lwkt_domsg(port, (lwkt_msg_t)devfs_msg, 0);
1056 return error;
1060 * sends a message with a generic argument.
1062 void
1063 devfs_msg_send_generic(uint32_t cmd, void *load)
1065 devfs_msg_t devfs_msg = devfs_msg_get();
1067 devfs_msg->mdv_load = load;
1068 devfs_msg_send(cmd, devfs_msg);
1072 * sends a message with a name argument.
1074 void
1075 devfs_msg_send_name(uint32_t cmd, char *name)
1077 devfs_msg_t devfs_msg = devfs_msg_get();
1079 devfs_msg->mdv_name = name;
1080 devfs_msg_send(cmd, devfs_msg);
1084 * sends a message with a mount argument.
1086 void
1087 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
1089 devfs_msg_t devfs_msg = devfs_msg_get();
1091 devfs_msg->mdv_mnt = mnt;
1092 devfs_msg_send(cmd, devfs_msg);
1096 * sends a message with an ops argument.
1098 void
1099 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
1101 devfs_msg_t devfs_msg = devfs_msg_get();
1103 devfs_msg->mdv_ops.ops = ops;
1104 devfs_msg->mdv_ops.minor = minor;
1105 devfs_msg_send(cmd, devfs_msg);
1109 * sends a message with a clone handler argument.
1111 void
1112 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
1114 devfs_msg_t devfs_msg = devfs_msg_get();
1116 devfs_msg->mdv_chandler.name = name;
1117 devfs_msg->mdv_chandler.nhandler = handler;
1118 devfs_msg_send(cmd, devfs_msg);
1122 * sends a message with a device argument.
1124 void
1125 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1127 devfs_msg_t devfs_msg = devfs_msg_get();
1129 devfs_msg->mdv_dev.dev = dev;
1130 devfs_msg->mdv_dev.uid = uid;
1131 devfs_msg->mdv_dev.gid = gid;
1132 devfs_msg->mdv_dev.perms = perms;
1134 devfs_msg_send(cmd, devfs_msg);
1138 * sends a message with a link argument.
1140 void
1141 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1143 devfs_msg_t devfs_msg = devfs_msg_get();
1145 devfs_msg->mdv_link.name = name;
1146 devfs_msg->mdv_link.target = target;
1147 devfs_msg->mdv_link.mp = mp;
1148 devfs_msg_send(cmd, devfs_msg);
1152 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1153 * and calls the relevant worker functions. By using messages it's assured
1154 * that events occur in the correct order.
1156 static void
1157 devfs_msg_core(void *arg)
1159 devfs_msg_t msg;
1161 lwkt_initport_thread(&devfs_msg_port, curthread);
1163 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1164 devfs_run = 1;
1165 wakeup(td_core);
1166 lockmgr(&devfs_lock, LK_RELEASE);
1168 lwkt_gettoken(&devfs_token);
1170 while (devfs_run) {
1171 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1172 devfs_debug(DEVFS_DEBUG_DEBUG,
1173 "devfs_msg_core, new msg: %x\n",
1174 (unsigned int)msg->hdr.u.ms_result);
1175 devfs_msg_exec(msg);
1176 lwkt_replymsg(&msg->hdr, 0);
1179 lwkt_reltoken(&devfs_token);
1180 wakeup(td_core);
1182 lwkt_exit();
1185 static void
1186 devfs_msg_exec(devfs_msg_t msg)
1188 struct devfs_mnt_data *mnt;
1189 struct devfs_node *node;
1190 cdev_t dev;
1193 * Acquire the devfs lock to ensure safety of all called functions
1195 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1197 switch (msg->hdr.u.ms_result) {
1198 case DEVFS_DEVICE_CREATE:
1199 dev = msg->mdv_dev.dev;
1200 devfs_create_dev_worker(dev,
1201 msg->mdv_dev.uid,
1202 msg->mdv_dev.gid,
1203 msg->mdv_dev.perms);
1204 break;
1205 case DEVFS_DEVICE_DESTROY:
1206 dev = msg->mdv_dev.dev;
1207 devfs_destroy_dev_worker(dev);
1208 break;
1209 case DEVFS_DESTROY_RELATED:
1210 devfs_destroy_related_worker(msg->mdv_load);
1211 break;
1212 case DEVFS_DESTROY_DEV_BY_OPS:
1213 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1214 msg->mdv_ops.minor);
1215 break;
1216 case DEVFS_CREATE_ALL_DEV:
1217 node = (struct devfs_node *)msg->mdv_load;
1218 devfs_create_all_dev_worker(node);
1219 break;
1220 case DEVFS_MOUNT_ADD:
1221 mnt = msg->mdv_mnt;
1222 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1223 devfs_create_all_dev_worker(mnt->root_node);
1224 break;
1225 case DEVFS_MOUNT_DEL:
1226 mnt = msg->mdv_mnt;
1227 TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1228 /* Be sure to remove all the aliases first */
1229 devfs_iterate_topology(mnt->root_node, devfs_alias_reaper_callback,
1230 NULL);
1231 devfs_iterate_topology(mnt->root_node, devfs_reaperp_callback,
1232 NULL);
1233 if (mnt->leak_count) {
1234 devfs_debug(DEVFS_DEBUG_SHOW,
1235 "Leaked %ld devfs_node elements!\n",
1236 mnt->leak_count);
1238 break;
1239 case DEVFS_CHANDLER_ADD:
1240 devfs_chandler_add_worker(msg->mdv_chandler.name,
1241 msg->mdv_chandler.nhandler);
1242 break;
1243 case DEVFS_CHANDLER_DEL:
1244 devfs_chandler_del_worker(msg->mdv_chandler.name);
1245 break;
1246 case DEVFS_FIND_DEVICE_BY_NAME:
1247 devfs_find_device_by_name_worker(msg);
1248 break;
1249 case DEVFS_FIND_DEVICE_BY_UDEV:
1250 devfs_find_device_by_udev_worker(msg);
1251 break;
1252 case DEVFS_MAKE_ALIAS:
1253 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1254 break;
1255 case DEVFS_DESTROY_ALIAS:
1256 devfs_destroy_alias_worker((struct devfs_alias *)msg->mdv_load);
1257 break;
1258 case DEVFS_APPLY_RULES:
1259 devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1260 break;
1261 case DEVFS_RESET_RULES:
1262 devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1263 break;
1264 case DEVFS_SCAN_CALLBACK:
1265 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load,
1266 msg->mdv_load2);
1267 break;
1268 case DEVFS_CLR_RELATED_FLAG:
1269 devfs_clr_related_flag_worker(msg->mdv_flags.dev,
1270 msg->mdv_flags.flag);
1271 break;
1272 case DEVFS_DESTROY_RELATED_WO_FLAG:
1273 devfs_destroy_related_without_flag_worker(msg->mdv_flags.dev,
1274 msg->mdv_flags.flag);
1275 break;
1276 case DEVFS_INODE_TO_VNODE:
1277 msg->mdv_ino.vp = devfs_iterate_topology(
1278 DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node,
1279 (devfs_iterate_callback_t *)devfs_inode_to_vnode_worker_callback,
1280 &msg->mdv_ino.ino);
1281 break;
1282 case DEVFS_TERMINATE_CORE:
1283 devfs_run = 0;
1284 break;
1285 case DEVFS_SYNC:
1286 break;
1287 default:
1288 devfs_debug(DEVFS_DEBUG_WARNING,
1289 "devfs_msg_core: unknown message "
1290 "received at core\n");
1291 break;
1293 lockmgr(&devfs_lock, LK_RELEASE);
1296 static void
1297 devfs_devctl_notify(cdev_t dev, const char *ev)
1299 static const char prefix[] = "cdev=";
1300 char *data;
1301 int namelen;
1303 namelen = strlen(dev->si_name);
1304 data = kmalloc(namelen + sizeof(prefix), M_TEMP, M_WAITOK);
1305 memcpy(data, prefix, sizeof(prefix) - 1);
1306 memcpy(data + sizeof(prefix) - 1, dev->si_name, namelen + 1);
1307 devctl_notify("DEVFS", "CDEV", ev, data);
1308 kfree(data, M_TEMP);
1312 * Worker function to insert a new dev into the dev list and initialize its
1313 * permissions. It also calls devfs_propagate_dev which in turn propagates
1314 * the change to all mount points.
1316 * The passed dev is already referenced. This reference is eaten by this
1317 * function and represents the dev's linkage into devfs_dev_list.
1319 static int
1320 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1322 KKASSERT(dev);
1324 dev->si_uid = uid;
1325 dev->si_gid = gid;
1326 dev->si_perms = perms;
1328 devfs_link_dev(dev);
1329 devfs_propagate_dev(dev, 1);
1331 udev_event_attach(dev, NULL, 0);
1332 devfs_devctl_notify(dev, "CREATE");
1334 return 0;
1338 * Worker function to delete a dev from the dev list and free the cdev.
1339 * It also calls devfs_propagate_dev which in turn propagates the change
1340 * to all mount points.
1342 static int
1343 devfs_destroy_dev_worker(cdev_t dev)
1345 int error;
1347 KKASSERT(dev);
1348 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1350 error = devfs_unlink_dev(dev);
1351 devfs_propagate_dev(dev, 0);
1353 devfs_devctl_notify(dev, "DESTROY");
1354 udev_event_detach(dev, NULL, 0);
1356 if (error == 0)
1357 release_dev(dev); /* link ref */
1358 release_dev(dev);
1359 release_dev(dev);
1361 return 0;
1365 * Worker function to destroy all devices with a certain basename.
1366 * Calls devfs_destroy_dev_worker for the actual destruction.
1368 static int
1369 devfs_destroy_related_worker(cdev_t needle)
1371 cdev_t dev;
1373 restart:
1374 devfs_debug(DEVFS_DEBUG_DEBUG, "related worker: %s\n",
1375 needle->si_name);
1376 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1377 if (dev->si_parent == needle) {
1378 devfs_destroy_related_worker(dev);
1379 devfs_destroy_dev_worker(dev);
1380 goto restart;
1383 return 0;
1386 static int
1387 devfs_clr_related_flag_worker(cdev_t needle, uint32_t flag)
1389 cdev_t dev, dev1;
1391 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1392 if (dev->si_parent == needle) {
1393 devfs_clr_related_flag_worker(dev, flag);
1394 dev->si_flags &= ~flag;
1398 return 0;
1401 static int
1402 devfs_destroy_related_without_flag_worker(cdev_t needle, uint32_t flag)
1404 cdev_t dev;
1406 restart:
1407 devfs_debug(DEVFS_DEBUG_DEBUG, "related_wo_flag: %s\n",
1408 needle->si_name);
1410 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1411 if (dev->si_parent == needle) {
1412 devfs_destroy_related_without_flag_worker(dev, flag);
1413 if (!(dev->si_flags & flag)) {
1414 devfs_destroy_dev_worker(dev);
1415 devfs_debug(DEVFS_DEBUG_DEBUG,
1416 "related_wo_flag: %s restart\n", dev->si_name);
1417 goto restart;
1422 return 0;
1426 * Worker function that creates all device nodes on top of a devfs
1427 * root node.
1429 static int
1430 devfs_create_all_dev_worker(struct devfs_node *root)
1432 cdev_t dev;
1434 KKASSERT(root);
1436 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1437 devfs_create_device_node(root, dev, NULL, NULL, NULL);
1440 return 0;
1444 * Worker function that destroys all devices that match a specific
1445 * dev_ops and/or minor. If minor is less than 0, it is not matched
1446 * against. It also propagates all changes.
1448 static int
1449 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1451 cdev_t dev, dev1;
1453 KKASSERT(ops);
1455 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1456 if (dev->si_ops != ops)
1457 continue;
1458 if ((minor < 0) || (dev->si_uminor == minor)) {
1459 devfs_destroy_dev_worker(dev);
1463 return 0;
1467 * Worker function that registers a new clone handler in devfs.
1469 static int
1470 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler)
1472 struct devfs_clone_handler *chandler = NULL;
1473 u_char len = strlen(name);
1475 if (len == 0)
1476 return 1;
1478 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1479 if (chandler->namlen != len)
1480 continue;
1482 if (!memcmp(chandler->name, name, len)) {
1483 /* Clonable basename already exists */
1484 return 1;
1488 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1489 chandler->name = kstrdup(name, M_DEVFS);
1490 chandler->namlen = len;
1491 chandler->nhandler = nhandler;
1493 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1494 return 0;
1498 * Worker function that removes a given clone handler from the
1499 * clone handler list.
1501 static int
1502 devfs_chandler_del_worker(const char *name)
1504 struct devfs_clone_handler *chandler, *chandler2;
1505 u_char len = strlen(name);
1507 if (len == 0)
1508 return 1;
1510 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1511 if (chandler->namlen != len)
1512 continue;
1513 if (memcmp(chandler->name, name, len))
1514 continue;
1516 TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1517 kfree(chandler->name, M_DEVFS);
1518 kfree(chandler, M_DEVFS);
1519 break;
1522 return 0;
1526 * Worker function that finds a given device name and changes
1527 * the message received accordingly so that when replied to,
1528 * the answer is returned to the caller.
1530 static int
1531 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1533 struct devfs_alias *alias;
1534 cdev_t dev;
1535 cdev_t found = NULL;
1537 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1538 if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1539 found = dev;
1540 break;
1543 if (found == NULL) {
1544 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1545 if (strcmp(devfs_msg->mdv_name, alias->name) == 0) {
1546 found = alias->dev_target;
1547 break;
1551 devfs_msg->mdv_cdev = found;
1553 return 0;
1557 * Worker function that finds a given device udev and changes
1558 * the message received accordingly so that when replied to,
1559 * the answer is returned to the caller.
1561 static int
1562 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg)
1564 cdev_t dev, dev1;
1565 cdev_t found = NULL;
1567 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1568 if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1569 found = dev;
1570 break;
1573 devfs_msg->mdv_cdev = found;
1575 return 0;
1579 * Worker function that inserts a given alias into the
1580 * alias list, and propagates the alias to all mount
1581 * points.
1583 static int
1584 devfs_make_alias_worker(struct devfs_alias *alias)
1586 struct devfs_alias *alias2;
1587 size_t len = strlen(alias->name);
1588 int found = 0;
1590 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1591 if (len != alias2->namlen)
1592 continue;
1594 if (!memcmp(alias->name, alias2->name, len)) {
1595 found = 1;
1596 break;
1600 if (!found) {
1602 * The alias doesn't exist yet, so we add it to the alias list
1604 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1605 devfs_alias_propagate(alias, 0);
1606 udev_event_attach(alias->dev_target, alias->name, 1);
1607 } else {
1608 devfs_debug(DEVFS_DEBUG_WARNING,
1609 "Warning: duplicate devfs_make_alias for %s\n",
1610 alias->name);
1611 kfree(alias->name, M_DEVFS);
1612 kfree(alias, M_DEVFS);
1615 return 0;
1619 * Worker function that delete a given alias from the
1620 * alias list, and propagates the removal to all mount
1621 * points.
1623 static int
1624 devfs_destroy_alias_worker(struct devfs_alias *alias)
1626 struct devfs_alias *alias2;
1627 int found = 0;
1629 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1630 if (alias->dev_target != alias2->dev_target)
1631 continue;
1633 if (devfs_WildCmp(alias->name, alias2->name) == 0) {
1634 found = 1;
1635 break;
1639 if (!found) {
1640 devfs_debug(DEVFS_DEBUG_WARNING,
1641 "Warning: devfs_destroy_alias for inexistant alias: %s\n",
1642 alias->name);
1643 kfree(alias->name, M_DEVFS);
1644 kfree(alias, M_DEVFS);
1645 } else {
1647 * The alias exists, so we delete it from the alias list
1649 TAILQ_REMOVE(&devfs_alias_list, alias2, link);
1650 devfs_alias_propagate(alias2, 1);
1651 udev_event_detach(alias2->dev_target, alias2->name, 1);
1652 kfree(alias->name, M_DEVFS);
1653 kfree(alias, M_DEVFS);
1654 kfree(alias2->name, M_DEVFS);
1655 kfree(alias2, M_DEVFS);
1658 return 0;
1662 * Function that removes and frees all aliases.
1664 static int
1665 devfs_alias_reap(void)
1667 struct devfs_alias *alias, *alias2;
1669 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1670 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1671 kfree(alias->name, M_DEVFS);
1672 kfree(alias, M_DEVFS);
1674 return 0;
1678 * Function that removes an alias matching a specific cdev and frees
1679 * it accordingly.
1681 static int
1682 devfs_alias_remove(cdev_t dev)
1684 struct devfs_alias *alias, *alias2;
1686 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1687 if (alias->dev_target == dev) {
1688 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1689 udev_event_detach(alias->dev_target, alias->name, 1);
1690 kfree(alias->name, M_DEVFS);
1691 kfree(alias, M_DEVFS);
1694 return 0;
1698 * This function propagates an alias addition or removal to
1699 * all mount points.
1701 static int
1702 devfs_alias_propagate(struct devfs_alias *alias, int remove)
1704 struct devfs_mnt_data *mnt;
1706 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1707 if (remove) {
1708 devfs_destroy_node(mnt->root_node, alias->name);
1709 } else {
1710 devfs_alias_apply(mnt->root_node, alias);
1713 return 0;
1717 * This function is a recursive function iterating through
1718 * all device nodes in the topology and, if applicable,
1719 * creating the relevant alias for a device node.
1721 static int
1722 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1724 struct devfs_node *node1, *node2;
1726 KKASSERT(alias != NULL);
1728 if ((node->node_type == Nroot) || (node->node_type == Ndir)) {
1729 if (node->nchildren > 2) {
1730 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1731 devfs_alias_apply(node1, alias);
1734 } else {
1735 if (node->d_dev == alias->dev_target)
1736 devfs_alias_create(alias->name, node, 0);
1738 return 0;
1742 * This function checks if any alias possibly is applicable
1743 * to the given node. If so, the alias is created.
1745 static int
1746 devfs_alias_check_create(struct devfs_node *node)
1748 struct devfs_alias *alias;
1750 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1751 if (node->d_dev == alias->dev_target)
1752 devfs_alias_create(alias->name, node, 0);
1754 return 0;
1758 * This function creates an alias with a given name
1759 * linking to a given devfs node. It also increments
1760 * the link count on the target node.
1763 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based)
1765 struct mount *mp = target->mp;
1766 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1767 struct devfs_node *linknode;
1768 char *create_path = NULL;
1769 char *name;
1770 char *name_buf;
1771 int result = 0;
1773 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1775 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1776 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1778 if (create_path)
1779 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1782 if (devfs_find_device_node_by_name(parent, name)) {
1783 devfs_debug(DEVFS_DEBUG_WARNING,
1784 "Node already exists: %s "
1785 "(devfs_make_alias_worker)!\n",
1786 name);
1787 result = 1;
1788 goto done;
1791 linknode = devfs_allocp(Nlink, name, parent, mp, NULL);
1792 if (linknode == NULL) {
1793 result = 1;
1794 goto done;
1797 linknode->link_target = target;
1798 target->nlinks++;
1800 if (rule_based)
1801 linknode->flags |= DEVFS_RULE_CREATED;
1803 done:
1804 kfree(name_buf, M_TEMP);
1805 return (result);
1809 * This function is called by the core and handles mount point
1810 * strings. It either calls the relevant worker (devfs_apply_
1811 * reset_rules_worker) on all mountpoints or only a specific
1812 * one.
1814 static int
1815 devfs_apply_reset_rules_caller(char *mountto, int apply)
1817 struct devfs_mnt_data *mnt;
1819 if (mountto[0] == '*') {
1820 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1821 devfs_iterate_topology(mnt->root_node,
1822 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1823 NULL);
1825 } else {
1826 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1827 if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) {
1828 devfs_iterate_topology(mnt->root_node,
1829 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1830 NULL);
1831 break;
1836 kfree(mountto, M_DEVFS);
1837 return 0;
1841 * This function calls a given callback function for
1842 * every dev node in the devfs dev list.
1844 static int
1845 devfs_scan_callback_worker(devfs_scan_t *callback, void *arg)
1847 cdev_t dev, dev1;
1848 struct devfs_alias *alias, *alias1;
1850 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1851 callback(dev->si_name, dev, false, arg);
1853 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias1) {
1854 callback(alias->name, alias->dev_target, true, arg);
1857 return 0;
1861 * This function tries to resolve a given directory, or if not
1862 * found and creation requested, creates the given directory.
1864 static struct devfs_node *
1865 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1866 size_t name_len, int create)
1868 struct devfs_node *node, *found = NULL;
1870 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1871 if (name_len != node->d_dir.d_namlen)
1872 continue;
1874 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1875 found = node;
1876 break;
1880 if ((found == NULL) && (create)) {
1881 found = devfs_allocp(Ndir, dir_name, parent, parent->mp, NULL);
1884 return found;
1888 * This function tries to resolve a complete path. If creation is requested,
1889 * if a given part of the path cannot be resolved (because it doesn't exist),
1890 * it is created.
1892 struct devfs_node *
1893 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1895 struct devfs_node *node = parent;
1896 char *buf;
1897 size_t idx = 0;
1899 if (path == NULL)
1900 return parent;
1902 buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1904 while (*path && idx < PATH_MAX - 1) {
1905 if (*path != '/') {
1906 buf[idx++] = *path;
1907 } else {
1908 buf[idx] = '\0';
1909 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1910 if (node == NULL) {
1911 kfree(buf, M_TEMP);
1912 return NULL;
1914 idx = 0;
1916 ++path;
1918 buf[idx] = '\0';
1919 node = devfs_resolve_or_create_dir(node, buf, idx, create);
1920 kfree (buf, M_TEMP);
1921 return (node);
1925 * Takes a full path and strips it into a directory path and a name.
1926 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
1927 * requires a working buffer with enough size to keep the whole
1928 * fullpath.
1931 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
1933 char *name = NULL;
1934 char *path = NULL;
1935 size_t len = strlen(fullpath) + 1;
1936 int i;
1938 KKASSERT((fullpath != NULL) && (buf != NULL));
1939 KKASSERT((pathp != NULL) && (namep != NULL));
1941 memcpy(buf, fullpath, len);
1943 for (i = len-1; i>= 0; i--) {
1944 if (buf[i] == '/') {
1945 buf[i] = '\0';
1946 name = &(buf[i+1]);
1947 path = buf;
1948 break;
1952 *pathp = path;
1954 if (name) {
1955 *namep = name;
1956 } else {
1957 *namep = buf;
1960 return 0;
1964 * This function creates a new devfs node for a given device. It can
1965 * handle a complete path as device name, and accordingly creates
1966 * the path and the final device node.
1968 * The reference count on the passed dev remains unchanged.
1970 struct devfs_node *
1971 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
1972 int *existsp, char *dev_name, char *path_fmt, ...)
1974 struct devfs_node *parent, *node = NULL;
1975 char *path = NULL;
1976 char *name;
1977 char *name_buf;
1978 __va_list ap;
1979 int i, found;
1980 char *create_path = NULL;
1981 char *names = "pqrsPQRS";
1983 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1985 if (existsp)
1986 *existsp = 0;
1988 if (path_fmt != NULL) {
1989 __va_start(ap, path_fmt);
1990 kvasnrprintf(&path, PATH_MAX, 10, path_fmt, ap);
1991 __va_end(ap);
1994 parent = devfs_resolve_or_create_path(root, path, 1);
1995 KKASSERT(parent);
1997 devfs_resolve_name_path(
1998 ((dev_name == NULL) && (dev))?(dev->si_name):(dev_name),
1999 name_buf, &create_path, &name);
2001 if (create_path)
2002 parent = devfs_resolve_or_create_path(parent, create_path, 1);
2005 node = devfs_find_device_node_by_name(parent, name);
2006 if (node) {
2007 if (node->d_dev == dev) {
2009 * Allow case where device caches dev after the
2010 * close and might desire to reuse it.
2012 if (existsp)
2013 *existsp = 1;
2014 } else {
2015 devfs_debug(DEVFS_DEBUG_WARNING,
2016 "devfs_create_device_node: "
2017 "DEVICE %s ALREADY EXISTS!!! "
2018 "Ignoring creation request.\n",
2019 name);
2020 node = NULL;
2022 goto out;
2025 node = devfs_allocp(Ndev, name, parent, parent->mp, dev);
2026 nanotime(&parent->mtime);
2029 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
2030 * directory
2032 if ((dev) && (strlen(dev->si_name) >= 4) &&
2033 (!memcmp(dev->si_name, "ptm/", 4))) {
2034 node->parent->flags |= DEVFS_HIDDEN;
2035 node->flags |= DEVFS_HIDDEN;
2039 * Ugly pty magic, to tag pty devices as such and hide them if needed.
2041 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
2042 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
2044 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
2045 found = 0;
2046 for (i = 0; i < strlen(names); i++) {
2047 if (name[3] == names[i]) {
2048 found = 1;
2049 break;
2052 if (found)
2053 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
2056 out:
2057 kfree(name_buf, M_TEMP);
2058 kvasfree(&path);
2059 return node;
2063 * This function finds a given device node in the topology with a given
2064 * cdev.
2066 void *
2067 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target)
2069 if ((node->node_type == Ndev) && (node->d_dev == target)) {
2070 return node;
2073 return NULL;
2077 * This function finds a device node in the given parent directory by its
2078 * name and returns it.
2080 struct devfs_node *
2081 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
2083 struct devfs_node *node, *found = NULL;
2084 size_t len = strlen(target);
2086 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
2087 if (len != node->d_dir.d_namlen)
2088 continue;
2090 if (!memcmp(node->d_dir.d_name, target, len)) {
2091 found = node;
2092 break;
2096 return found;
2099 static void *
2100 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop)
2102 struct vnode *vp = NULL;
2103 ino_t target = *inop;
2105 if (node->d_dir.d_ino == target) {
2106 if (node->v_node) {
2107 vp = node->v_node;
2108 vget(vp, LK_EXCLUSIVE | LK_RETRY);
2109 vn_unlock(vp);
2110 } else {
2111 devfs_allocv(&vp, node);
2112 vn_unlock(vp);
2116 return vp;
2120 * This function takes a cdev and removes its devfs node in the
2121 * given topology. The cdev remains intact.
2124 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
2126 KKASSERT(target != NULL);
2127 return devfs_destroy_node(root, target->si_name);
2131 * This function takes a path to a devfs node, resolves it and
2132 * removes the devfs node from the given topology.
2135 devfs_destroy_node(struct devfs_node *root, char *target)
2137 struct devfs_node *node, *parent;
2138 char *name;
2139 char *name_buf;
2140 char *create_path = NULL;
2142 KKASSERT(target);
2144 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
2145 ksnprintf(name_buf, PATH_MAX, "%s", target);
2147 devfs_resolve_name_path(target, name_buf, &create_path, &name);
2149 if (create_path)
2150 parent = devfs_resolve_or_create_path(root, create_path, 0);
2151 else
2152 parent = root;
2154 if (parent == NULL) {
2155 kfree(name_buf, M_TEMP);
2156 return 1;
2159 node = devfs_find_device_node_by_name(parent, name);
2161 if (node) {
2162 nanotime(&node->parent->mtime);
2163 devfs_gc(node);
2166 kfree(name_buf, M_TEMP);
2168 return 0;
2172 * Just set perms and ownership for given node.
2175 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid,
2176 u_short mode, u_long flags)
2178 node->mode = mode;
2179 node->uid = uid;
2180 node->gid = gid;
2182 return 0;
2186 * Propagates a device attach/detach to all mount
2187 * points. Also takes care of automatic alias removal
2188 * for a deleted cdev.
2190 static int
2191 devfs_propagate_dev(cdev_t dev, int attach)
2193 struct devfs_mnt_data *mnt;
2195 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
2196 if (attach) {
2197 /* Device is being attached */
2198 devfs_create_device_node(mnt->root_node, dev,
2199 NULL, NULL, NULL);
2200 } else {
2201 /* Device is being detached */
2202 devfs_alias_remove(dev);
2203 devfs_destroy_device_node(mnt->root_node, dev);
2206 return 0;
2210 * devfs_clone either returns a basename from a complete name by
2211 * returning the length of the name without trailing digits, or,
2212 * if clone != 0, calls the device's clone handler to get a new
2213 * device, which in turn is returned in devp.
2215 cdev_t
2216 devfs_clone(cdev_t dev, const char *name, size_t len, int mode,
2217 struct ucred *cred)
2219 int error;
2220 struct devfs_clone_handler *chandler;
2221 struct dev_clone_args ap;
2223 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
2224 if (chandler->namlen != len)
2225 continue;
2226 if ((!memcmp(chandler->name, name, len)) && (chandler->nhandler)) {
2227 lockmgr(&devfs_lock, LK_RELEASE);
2228 devfs_config();
2229 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2231 ap.a_head.a_dev = dev;
2232 ap.a_dev = NULL;
2233 ap.a_name = name;
2234 ap.a_namelen = len;
2235 ap.a_mode = mode;
2236 ap.a_cred = cred;
2237 error = (chandler->nhandler)(&ap);
2238 if (error)
2239 continue;
2241 return ap.a_dev;
2245 return NULL;
2250 * Registers a new orphan in the orphan list.
2252 void
2253 devfs_tracer_add_orphan(struct devfs_node *node)
2255 struct devfs_orphan *orphan;
2257 KKASSERT(node);
2258 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2259 orphan->node = node;
2261 KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2262 node->flags |= DEVFS_ORPHANED;
2263 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2267 * Removes an orphan from the orphan list.
2269 void
2270 devfs_tracer_del_orphan(struct devfs_node *node)
2272 struct devfs_orphan *orphan;
2274 KKASSERT(node);
2276 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) {
2277 if (orphan->node == node) {
2278 node->flags &= ~DEVFS_ORPHANED;
2279 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2280 kfree(orphan, M_DEVFS);
2281 break;
2287 * Counts the orphans in the orphan list, and if cleanup
2288 * is specified, also frees the orphan and removes it from
2289 * the list.
2291 size_t
2292 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2294 struct devfs_orphan *orphan, *orphan2;
2295 size_t count = 0;
2297 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) {
2298 count++;
2300 * If we are instructed to clean up, we do so.
2302 if (cleanup) {
2303 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2304 orphan->node->flags &= ~DEVFS_ORPHANED;
2305 devfs_freep(orphan->node);
2306 kfree(orphan, M_DEVFS);
2310 return count;
2314 * Fetch an ino_t from the global d_ino by increasing it
2315 * while spinlocked.
2317 static ino_t
2318 devfs_fetch_ino(void)
2320 ino_t ret;
2322 spin_lock(&ino_lock);
2323 ret = d_ino++;
2324 spin_unlock(&ino_lock);
2326 return ret;
2330 * Allocates a new cdev and initializes it's most basic
2331 * fields.
2333 cdev_t
2334 devfs_new_cdev(struct dev_ops *ops, int minor, struct dev_ops *bops)
2336 cdev_t dev = sysref_alloc(&cdev_sysref_class);
2338 sysref_activate(&dev->si_sysref);
2339 reference_dev(dev);
2340 bzero(dev, offsetof(struct cdev, si_sysref));
2342 dev->si_uid = 0;
2343 dev->si_gid = 0;
2344 dev->si_perms = 0;
2345 dev->si_drv1 = NULL;
2346 dev->si_drv2 = NULL;
2347 dev->si_lastread = 0; /* time_uptime */
2348 dev->si_lastwrite = 0; /* time_uptime */
2350 dev->si_dict = NULL;
2351 dev->si_parent = NULL;
2352 dev->si_ops = ops;
2353 dev->si_flags = 0;
2354 dev->si_uminor = minor;
2355 dev->si_bops = bops;
2358 * Since the disk subsystem is in the way, we need to
2359 * propagate the D_CANFREE from bops (and ops) to
2360 * si_flags.
2362 if (bops && (bops->head.flags & D_CANFREE)) {
2363 dev->si_flags |= SI_CANFREE;
2364 } else if (ops->head.flags & D_CANFREE) {
2365 dev->si_flags |= SI_CANFREE;
2368 /* If there is a backing device, we reference its ops */
2369 dev->si_inode = makeudev(
2370 devfs_reference_ops((bops)?(bops):(ops)),
2371 minor );
2372 dev->si_umajor = umajor(dev->si_inode);
2374 return dev;
2377 static void
2378 devfs_cdev_terminate(cdev_t dev)
2380 int locked = 0;
2382 /* Check if it is locked already. if not, we acquire the devfs lock */
2383 if ((lockstatus(&devfs_lock, curthread)) != LK_EXCLUSIVE) {
2384 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2385 locked = 1;
2389 * Make sure the node isn't linked anymore. Otherwise we've screwed
2390 * up somewhere, since normal devs are unlinked on the call to
2391 * destroy_dev and only-cdevs that have not been used for cloning
2392 * are not linked in the first place. only-cdevs used for cloning
2393 * will be linked in, too, and should only be destroyed via
2394 * destroy_dev, not destroy_only_dev, so we catch that problem, too.
2396 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2398 /* If we acquired the lock, we also get rid of it */
2399 if (locked)
2400 lockmgr(&devfs_lock, LK_RELEASE);
2402 /* If there is a backing device, we release the backing device's ops */
2403 devfs_release_ops((dev->si_bops)?(dev->si_bops):(dev->si_ops));
2405 /* Finally destroy the device */
2406 sysref_put(&dev->si_sysref);
2410 * Dummies for now (individual locks for MPSAFE)
2412 static void
2413 devfs_cdev_lock(cdev_t dev)
2417 static void
2418 devfs_cdev_unlock(cdev_t dev)
2422 static int
2423 devfs_detached_filter_eof(struct knote *kn, long hint)
2425 kn->kn_flags |= (EV_EOF | EV_NODATA);
2426 return (1);
2429 static void
2430 devfs_detached_filter_detach(struct knote *kn)
2432 cdev_t dev = (cdev_t)kn->kn_hook;
2434 knote_remove(&dev->si_kqinfo.ki_note, kn);
2437 static struct filterops devfs_detached_filterops =
2438 { FILTEROP_ISFD, NULL,
2439 devfs_detached_filter_detach,
2440 devfs_detached_filter_eof };
2443 * Delegates knote filter handling responsibility to devfs
2445 * Any device that implements kqfilter event handling and could be detached
2446 * or shut down out from under the kevent subsystem must allow devfs to
2447 * assume responsibility for any knotes it may hold.
2449 void
2450 devfs_assume_knotes(cdev_t dev, struct kqinfo *kqi)
2453 * Let kern/kern_event.c do the heavy lifting.
2455 knote_assume_knotes(kqi, &dev->si_kqinfo,
2456 &devfs_detached_filterops, (void *)dev);
2459 * These should probably be activated individually, but doing so
2460 * would require refactoring kq's public in-kernel interface.
2462 KNOTE(&dev->si_kqinfo.ki_note, 0);
2466 * Links a given cdev into the dev list.
2469 devfs_link_dev(cdev_t dev)
2471 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2472 dev->si_flags |= SI_DEVFS_LINKED;
2473 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2475 return 0;
2479 * Removes a given cdev from the dev list. The caller is responsible for
2480 * releasing the reference on the device associated with the linkage.
2482 * Returns EALREADY if the dev has already been unlinked.
2484 static int
2485 devfs_unlink_dev(cdev_t dev)
2487 if ((dev->si_flags & SI_DEVFS_LINKED)) {
2488 TAILQ_REMOVE(&devfs_dev_list, dev, link);
2489 dev->si_flags &= ~SI_DEVFS_LINKED;
2490 return (0);
2492 return (EALREADY);
2496 devfs_node_is_accessible(struct devfs_node *node)
2498 if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2499 return 1;
2500 else
2501 return 0;
2505 devfs_reference_ops(struct dev_ops *ops)
2507 int unit;
2508 struct devfs_dev_ops *found = NULL;
2509 struct devfs_dev_ops *devops;
2511 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2512 if (devops->ops == ops) {
2513 found = devops;
2514 break;
2518 if (!found) {
2519 found = kmalloc(sizeof(struct devfs_dev_ops), M_DEVFS, M_WAITOK);
2520 found->ops = ops;
2521 found->ref_count = 0;
2522 TAILQ_INSERT_TAIL(&devfs_dev_ops_list, found, link);
2525 KKASSERT(found);
2527 if (found->ref_count == 0) {
2528 found->id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255);
2529 if (found->id == -1) {
2530 /* Ran out of unique ids */
2531 devfs_debug(DEVFS_DEBUG_WARNING,
2532 "devfs_reference_ops: WARNING: ran out of unique ids\n");
2535 unit = found->id;
2536 ++found->ref_count;
2538 return unit;
2541 void
2542 devfs_release_ops(struct dev_ops *ops)
2544 struct devfs_dev_ops *found = NULL;
2545 struct devfs_dev_ops *devops;
2547 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2548 if (devops->ops == ops) {
2549 found = devops;
2550 break;
2554 KKASSERT(found);
2556 --found->ref_count;
2558 if (found->ref_count == 0) {
2559 TAILQ_REMOVE(&devfs_dev_ops_list, found, link);
2560 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), found->id);
2561 kfree(found, M_DEVFS);
2566 * Wait for asynchronous messages to complete in the devfs helper
2567 * thread, then return. Do nothing if the helper thread is dead
2568 * or we are being indirectly called from the helper thread itself.
2570 void
2571 devfs_config(void)
2573 devfs_msg_t msg;
2575 if (devfs_run && curthread != td_core) {
2576 msg = devfs_msg_get();
2577 devfs_msg_send_sync(DEVFS_SYNC, msg);
2578 devfs_msg_put(msg);
2583 * Called on init of devfs; creates the objcaches and
2584 * spawns off the devfs core thread. Also initializes
2585 * locks.
2587 static void
2588 devfs_init(void)
2590 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2591 /* Create objcaches for nodes, msgs and devs */
2592 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2593 NULL, NULL, NULL,
2594 objcache_malloc_alloc,
2595 objcache_malloc_free,
2596 &devfs_node_malloc_args );
2598 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2599 NULL, NULL, NULL,
2600 objcache_malloc_alloc,
2601 objcache_malloc_free,
2602 &devfs_msg_malloc_args );
2604 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2605 NULL, NULL, NULL,
2606 objcache_malloc_alloc,
2607 objcache_malloc_free,
2608 &devfs_dev_malloc_args );
2610 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2612 /* Initialize the reply-only port which acts as a message drain */
2613 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2615 /* Initialize *THE* devfs lock */
2616 lockinit(&devfs_lock, "devfs_core lock", 0, 0);
2617 lwkt_token_init(&devfs_token, "devfs_core");
2619 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2620 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2621 0, -1, "devfs_msg_core");
2622 while (devfs_run == 0)
2623 lksleep(td_core, &devfs_lock, 0, "devfsc", 0);
2624 lockmgr(&devfs_lock, LK_RELEASE);
2626 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2630 * Called on unload of devfs; takes care of destroying the core
2631 * and the objcaches. Also removes aliases that are no longer needed.
2633 static void
2634 devfs_uninit(void)
2636 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2638 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2639 while (devfs_run)
2640 tsleep(td_core, 0, "devfsc", hz*10);
2641 tsleep(td_core, 0, "devfsc", hz);
2643 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2645 /* Destroy the objcaches */
2646 objcache_destroy(devfs_msg_cache);
2647 objcache_destroy(devfs_node_cache);
2648 objcache_destroy(devfs_dev_cache);
2650 devfs_alias_reap();
2654 * This is a sysctl handler to assist userland devname(3) to
2655 * find the device name for a given udev.
2657 static int
2658 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2660 udev_t udev;
2661 cdev_t found;
2662 int error;
2665 if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t))))
2666 return (error);
2668 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev);
2670 if (udev == NOUDEV)
2671 return(EINVAL);
2673 if ((found = devfs_find_device_by_udev(udev)) == NULL)
2674 return(ENOENT);
2676 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2680 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
2681 NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)");
2683 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2684 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2685 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable,
2686 0, "Enable DevFS debugging");
2688 SYSINIT(vfs_devfs_register, SI_SUB_DEVFS_CORE, SI_ORDER_FIRST,
2689 devfs_init, NULL);
2690 SYSUNINIT(vfs_devfs_register, SI_SUB_DEVFS_CORE, SI_ORDER_ANY,
2691 devfs_uninit, NULL);
2694 * WildCmp() - compare wild string to sane string
2696 * Returns 0 on success, -1 on failure.
2698 static int
2699 wildCmp(const char **mary, int d, const char *w, const char *s)
2701 int i;
2704 * skip fixed portion
2706 for (;;) {
2707 switch(*w) {
2708 case '*':
2710 * optimize terminator
2712 if (w[1] == 0)
2713 return(0);
2714 if (w[1] != '?' && w[1] != '*') {
2716 * optimize * followed by non-wild
2718 for (i = 0; s + i < mary[d]; ++i) {
2719 if (s[i] == w[1] && wildCmp(mary, d + 1, w + 1, s + i) == 0)
2720 return(0);
2722 } else {
2724 * less-optimal
2726 for (i = 0; s + i < mary[d]; ++i) {
2727 if (wildCmp(mary, d + 1, w + 1, s + i) == 0)
2728 return(0);
2731 mary[d] = s;
2732 return(-1);
2733 case '?':
2734 if (*s == 0)
2735 return(-1);
2736 ++w;
2737 ++s;
2738 break;
2739 default:
2740 if (*w != *s)
2741 return(-1);
2742 if (*w == 0) /* terminator */
2743 return(0);
2744 ++w;
2745 ++s;
2746 break;
2749 /* not reached */
2750 return(-1);
2755 * WildCaseCmp() - compare wild string to sane string, case insensitive
2757 * Returns 0 on success, -1 on failure.
2759 static int
2760 wildCaseCmp(const char **mary, int d, const char *w, const char *s)
2762 int i;
2765 * skip fixed portion
2767 for (;;) {
2768 switch(*w) {
2769 case '*':
2771 * optimize terminator
2773 if (w[1] == 0)
2774 return(0);
2775 if (w[1] != '?' && w[1] != '*') {
2777 * optimize * followed by non-wild
2779 for (i = 0; s + i < mary[d]; ++i) {
2780 if (s[i] == w[1] && wildCaseCmp(mary, d + 1, w + 1, s + i) == 0)
2781 return(0);
2783 } else {
2785 * less-optimal
2787 for (i = 0; s + i < mary[d]; ++i) {
2788 if (wildCaseCmp(mary, d + 1, w + 1, s + i) == 0)
2789 return(0);
2792 mary[d] = s;
2793 return(-1);
2794 case '?':
2795 if (*s == 0)
2796 return(-1);
2797 ++w;
2798 ++s;
2799 break;
2800 default:
2801 if (*w != *s) {
2802 #define tolower(x) ((x >= 'A' && x <= 'Z')?(x+('a'-'A')):(x))
2803 if (tolower(*w) != tolower(*s))
2804 return(-1);
2806 if (*w == 0) /* terminator */
2807 return(0);
2808 ++w;
2809 ++s;
2810 break;
2813 /* not reached */
2814 return(-1);
2817 struct cdev_privdata {
2818 void *cdpd_data;
2819 cdevpriv_dtr_t cdpd_dtr;
2823 devfs_get_cdevpriv(struct file *fp, void **datap)
2825 int error;
2827 if (fp == NULL)
2828 return(EBADF);
2830 spin_lock_shared(&fp->f_spin);
2831 if (fp->f_data1 == NULL) {
2832 *datap = NULL;
2833 error = ENOENT;
2834 } else {
2835 struct cdev_privdata *p = fp->f_data1;
2837 *datap = p->cdpd_data;
2838 error = 0;
2840 spin_unlock_shared(&fp->f_spin);
2842 return (error);
2846 devfs_set_cdevpriv(struct file *fp, void *priv, cdevpriv_dtr_t dtr)
2848 struct cdev_privdata *p;
2849 int error;
2851 if (fp == NULL)
2852 return (ENOENT);
2854 p = kmalloc(sizeof(struct cdev_privdata), M_DEVFS, M_WAITOK);
2855 p->cdpd_data = priv;
2856 p->cdpd_dtr = dtr;
2858 spin_lock(&fp->f_spin);
2859 if (fp->f_data1 == NULL) {
2860 fp->f_data1 = p;
2861 error = 0;
2862 } else {
2863 error = EBUSY;
2865 spin_unlock(&fp->f_spin);
2867 if (error)
2868 kfree(p, M_DEVFS);
2870 return error;
2873 void
2874 devfs_clear_cdevpriv(struct file *fp)
2876 struct cdev_privdata *p;
2878 if (fp == NULL)
2879 return;
2881 spin_lock(&fp->f_spin);
2882 p = fp->f_data1;
2883 fp->f_data1 = NULL;
2884 spin_unlock(&fp->f_spin);
2886 if (p != NULL) {
2887 p->cdpd_dtr(p->cdpd_data);
2888 kfree(p, M_DEVFS);
2893 devfs_WildCmp(const char *w, const char *s)
2895 int i;
2896 int c;
2897 int slen = strlen(s);
2898 const char **mary;
2900 for (i = c = 0; w[i]; ++i) {
2901 if (w[i] == '*')
2902 ++c;
2904 mary = kmalloc(sizeof(char *) * (c + 1), M_DEVFS, M_WAITOK);
2905 for (i = 0; i < c; ++i)
2906 mary[i] = s + slen;
2907 i = wildCmp(mary, 0, w, s);
2908 kfree(mary, M_DEVFS);
2909 return(i);
2913 devfs_WildCaseCmp(const char *w, const char *s)
2915 int i;
2916 int c;
2917 int slen = strlen(s);
2918 const char **mary;
2920 for (i = c = 0; w[i]; ++i) {
2921 if (w[i] == '*')
2922 ++c;
2924 mary = kmalloc(sizeof(char *) * (c + 1), M_DEVFS, M_WAITOK);
2925 for (i = 0; i < c; ++i)
2926 mary[i] = s + slen;
2927 i = wildCaseCmp(mary, 0, w, s);
2928 kfree(mary, M_DEVFS);
2929 return(i);