nrelease - fix/improve livecd
[dragonfly.git] / sys / vfs / devfs / devfs_core.c
blob822efe11b448b218a92500b1f327838f5ea2a608
1 /*
2 * Copyright (c) 2009-2019 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/malloc.h>
39 #include <sys/mount.h>
40 #include <sys/vnode.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_reference_ops(struct dev_ops *ops);
113 static void devfs_release_ops(struct dev_ops *ops);
114 static int devfs_create_all_dev_worker(struct devfs_node *);
115 static int devfs_create_dev_worker(cdev_t, uid_t, gid_t, int);
116 static int devfs_destroy_dev_worker(cdev_t);
117 static int devfs_destroy_related_worker(cdev_t);
118 static int devfs_destroy_dev_by_ops_worker(struct dev_ops *, int);
119 static int devfs_propagate_dev(cdev_t, int);
120 static int devfs_unlink_dev(cdev_t dev);
121 static void devfs_msg_exec(devfs_msg_t msg);
123 static int devfs_chandler_add_worker(const char *, d_clone_t *);
124 static int devfs_chandler_del_worker(const char *);
126 static void devfs_msg_autofree_reply(lwkt_port_t, lwkt_msg_t);
127 static void devfs_msg_core(void *);
129 static int devfs_find_device_by_name_worker(devfs_msg_t);
130 static int devfs_find_device_by_devid_worker(devfs_msg_t);
132 static int devfs_apply_reset_rules_caller(char *, int);
134 static int devfs_scan_callback_worker(devfs_scan_t *, void *);
136 static struct devfs_node *devfs_resolve_or_create_dir(struct devfs_node *,
137 char *, size_t, int);
139 static int devfs_make_alias_worker(struct devfs_alias *);
140 static int devfs_destroy_alias_worker(struct devfs_alias *);
141 static int devfs_alias_remove(cdev_t);
142 static int devfs_alias_reap(void);
143 static int devfs_alias_propagate(struct devfs_alias *, int);
144 static int devfs_alias_apply(struct devfs_node *, struct devfs_alias *);
145 static int devfs_alias_check_create(struct devfs_node *);
147 static int devfs_clr_related_flag_worker(cdev_t, uint32_t);
148 static int devfs_destroy_related_without_flag_worker(cdev_t, uint32_t);
150 static void *devfs_reaperp_callback(struct devfs_node *, void *);
151 static void devfs_iterate_orphans_unmount(struct mount *mp);
152 static void *devfs_gc_dirs_callback(struct devfs_node *, void *);
153 static void *devfs_gc_links_callback(struct devfs_node *, struct devfs_node *);
154 static void *
155 devfs_inode_to_vnode_worker_callback(struct devfs_node *, ino_t *);
158 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function
159 * using kvprintf
162 devfs_debug(int level, char *fmt, ...)
164 __va_list ap;
166 __va_start(ap, fmt);
167 if (level <= devfs_debug_enable)
168 kvprintf(fmt, ap);
169 __va_end(ap);
171 return 0;
175 * devfs_allocp() Allocates a new devfs node with the specified
176 * parameters. The node is also automatically linked into the topology
177 * if a parent is specified. It also calls the rule and alias stuff to
178 * be applied on the new node
180 struct devfs_node *
181 devfs_allocp(devfs_nodetype devfsnodetype, char *name,
182 struct devfs_node *parent, struct mount *mp, cdev_t dev)
184 struct devfs_node *node = NULL;
185 size_t namlen = strlen(name);
187 node = objcache_get(devfs_node_cache, M_WAITOK);
188 bzero(node, sizeof(*node));
190 atomic_add_long(&DEVFS_MNTDATA(mp)->leak_count, 1);
192 node->d_dev = NULL;
193 node->nchildren = 1;
194 node->mp = mp;
195 node->d_dir.d_ino = devfs_fetch_ino();
198 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries
199 * respectively.
201 node->cookie_jar = 2;
204 * Access Control members
206 node->mode = DEVFS_DEFAULT_MODE;
207 node->uid = DEVFS_DEFAULT_UID;
208 node->gid = DEVFS_DEFAULT_GID;
210 switch (devfsnodetype) {
211 case Nroot:
213 * Ensure that we don't recycle the root vnode by marking it as
214 * linked into the topology.
216 node->flags |= DEVFS_NODE_LINKED;
217 case Ndir:
218 TAILQ_INIT(DEVFS_DENODE_HEAD(node));
219 node->d_dir.d_type = DT_DIR;
220 node->nchildren = 2;
221 break;
223 case Nlink:
224 node->d_dir.d_type = DT_LNK;
225 break;
227 case Nreg:
228 node->d_dir.d_type = DT_REG;
229 break;
231 case Ndev:
232 if (dev != NULL) {
233 node->d_dir.d_type = DT_CHR;
234 node->d_dev = dev;
236 node->mode = dev->si_perms;
237 node->uid = dev->si_uid;
238 node->gid = dev->si_gid;
240 devfs_alias_check_create(node);
242 break;
244 default:
245 panic("devfs_allocp: unknown node type");
248 node->v_node = NULL;
249 node->node_type = devfsnodetype;
251 /* Initialize the dirent structure of each devfs vnode */
252 node->d_dir.d_namlen = namlen;
253 node->d_dir.d_name = kmalloc(namlen+1, M_DEVFS, M_WAITOK);
254 memcpy(node->d_dir.d_name, name, namlen);
255 node->d_dir.d_name[namlen] = '\0';
257 /* Initialize the parent node element */
258 node->parent = parent;
260 /* Initialize *time members */
261 vfs_timestamp(&node->atime);
262 node->mtime = node->ctime = node->atime;
265 * Associate with parent as last step, clean out namecache
266 * reference.
268 if (parent) {
269 if (parent->node_type == Nroot ||
270 parent->node_type == Ndir) {
271 parent->nchildren++;
272 node->cookie = parent->cookie_jar++;
273 node->flags |= DEVFS_NODE_LINKED;
274 TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent), node, link);
276 /* This forces negative namecache lookups to clear */
277 ++mp->mnt_namecache_gen;
278 } else {
279 kprintf("devfs: Cannot link node %p (%s) "
280 "into %p (%s)\n",
281 node, node->d_dir.d_name,
282 parent, parent->d_dir.d_name);
283 print_backtrace(-1);
288 * Apply rules (requires root node, skip if we are creating the root
289 * node)
291 if (DEVFS_MNTDATA(mp)->root_node)
292 devfs_rule_check_apply(node, NULL);
294 atomic_add_long(&DEVFS_MNTDATA(mp)->file_count, 1);
296 return node;
300 * devfs_allocv() allocates a new vnode based on a devfs node.
303 devfs_allocv(struct vnode **vpp, struct devfs_node *node)
305 struct vnode *vp;
306 int error = 0;
308 KKASSERT(node);
311 * devfs master lock must not be held across a vget() call, we have
312 * to hold our ad-hoc vp to avoid a free race from destroying the
313 * contents of the structure. The vget() will interlock recycles
314 * for us.
316 try_again:
317 while ((vp = node->v_node) != NULL) {
318 vhold(vp);
319 lockmgr(&devfs_lock, LK_RELEASE);
320 error = vget(vp, LK_EXCLUSIVE);
321 vdrop(vp);
322 lockmgr(&devfs_lock, LK_EXCLUSIVE);
323 if (error == 0) {
324 *vpp = vp;
325 goto out;
327 if (error != ENOENT) {
328 *vpp = NULL;
329 goto out;
334 * devfs master lock must not be held across a getnewvnode() call.
336 lockmgr(&devfs_lock, LK_RELEASE);
337 if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0) {
338 lockmgr(&devfs_lock, LK_EXCLUSIVE);
339 goto out;
341 lockmgr(&devfs_lock, LK_EXCLUSIVE);
343 vp = *vpp;
345 if (node->v_node != NULL) {
346 vp->v_type = VBAD;
347 vx_put(vp);
348 goto try_again;
351 vp->v_data = node;
352 node->v_node = vp;
354 switch (node->node_type) {
355 case Nroot:
356 vsetflags(vp, VROOT);
357 /* fall through */
358 case Ndir:
359 vp->v_type = VDIR;
360 break;
362 case Nlink:
363 vp->v_type = VLNK;
364 break;
366 case Nreg:
367 vp->v_type = VREG;
368 break;
370 case Ndev:
371 vp->v_type = VCHR;
372 KKASSERT(node->d_dev);
374 vp->v_uminor = node->d_dev->si_uminor;
375 vp->v_umajor = node->d_dev->si_umajor;
377 v_associate_rdev(vp, node->d_dev);
378 vp->v_ops = &node->mp->mnt_vn_spec_ops;
379 if (node->d_dev->si_ops->head.flags & D_KVABIO)
380 vsetflags(vp, VKVABIO);
381 break;
383 default:
384 panic("devfs_allocv: unknown node type");
386 vx_downgrade(vp); /* downgrade VX lock to VN lock */
388 out:
389 return error;
393 * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
394 * based on the newly created devfs node.
397 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype,
398 char *name, struct devfs_node *parent, cdev_t dev)
400 struct devfs_node *node;
402 node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
404 if (node != NULL)
405 devfs_allocv(vpp, node);
406 else
407 *vpp = NULL;
409 return 0;
413 * Destroy the devfs_node. The node must be unlinked from the topology.
415 * This function will also destroy any vnode association with the node
416 * and device.
418 * The cdev_t itself remains intact.
420 * The core lock is not necessarily held on call and must be temporarily
421 * released if it is to avoid a deadlock.
423 void
424 devfs_freep(struct devfs_node *node)
426 struct vnode *vp;
427 int maxloops;
429 KKASSERT(node);
432 * It is possible for devfs_freep() to race a destruction due
433 * to having to release the lock below. We use DEVFS_DESTROYED
434 * to interlock the race (mediated by devfs_lock)
436 * We use NLINKSWAIT to indicate that the node couldn't be
437 * freed due to having pending nlinks. We can free
438 * the node when nlinks drops to 0. This should never print
439 * a "(null)" name, if it ever does there are still unresolved
440 * issues.
442 if (node->flags & DEVFS_DESTROYED) {
443 if ((node->flags & DEVFS_NLINKSWAIT) &&
444 node->nlinks == 0) {
445 kprintf("devfs: final node '%s' on nlinks\n",
446 node->d_dir.d_name);
447 if (node->d_dir.d_name) {
448 kfree(node->d_dir.d_name, M_DEVFS);
449 node->d_dir.d_name = NULL;
451 objcache_put(devfs_node_cache, node);
452 } else {
453 kprintf("devfs: race avoided node '%s' (%p)\n",
454 node->d_dir.d_name, node);
456 return;
458 node->flags |= DEVFS_DESTROYED;
461 * Items we have to dispose of before potentially releasing
462 * devfs_lock.
464 * Remove the node from the orphan list if it is still on it.
466 atomic_subtract_long(&DEVFS_MNTDATA(node->mp)->leak_count, 1);
467 atomic_subtract_long(&DEVFS_MNTDATA(node->mp)->file_count, 1);
468 if (node->flags & DEVFS_ORPHANED)
469 devfs_tracer_del_orphan(node);
472 * At this point only the vp points to node, and node cannot be
473 * physically freed because we own DEVFS_DESTROYED.
475 * We must dispose of the vnode without deadlocking or racing
476 * against e.g. a vnode reclaim.
478 * This also prevents the vnode reclaim code from double-freeing
479 * the node. The vget() is required to safely modified the vp
480 * and cycle the refs to terminate an inactive vp.
482 maxloops = 1000;
483 while ((vp = node->v_node) != NULL) {
484 int relock;
486 vhold(vp);
487 if (lockstatus(&devfs_lock, curthread) == LK_EXCLUSIVE) {
488 lockmgr(&devfs_lock, LK_RELEASE);
489 relock = 1;
490 } else {
491 relock = 0;
493 if (node->v_node == NULL) {
494 /* reclaim race, mediated by devfs_lock */
495 vdrop(vp);
496 } else if (vget(vp, LK_EXCLUSIVE | LK_RETRY) == 0) {
497 vdrop(vp);
498 v_release_rdev(vp);
499 vp->v_data = NULL;
500 node->v_node = NULL;
501 vput(vp);
502 } else {
503 /* reclaim race, mediated by devfs_lock */
504 vdrop(vp);
506 if (relock)
507 lockmgr(&devfs_lock, LK_EXCLUSIVE);
508 if (--maxloops == 0) {
509 kprintf("devfs_freep: livelock on node %p\n", node);
510 break;
515 * Remaining cleanup
517 if (node->symlink_name) {
518 kfree(node->symlink_name, M_DEVFS);
519 node->symlink_name = NULL;
523 * We cannot actually free the node if it still has
524 * nlinks.
526 if (node->nlinks) {
527 node->flags |= DEVFS_NLINKSWAIT;
528 } else {
529 if (node->d_dir.d_name) {
530 kfree(node->d_dir.d_name, M_DEVFS);
531 node->d_dir.d_name = NULL;
533 objcache_put(devfs_node_cache, node);
538 * Returns a valid vp associated with the devfs alias node or NULL
540 static void *devfs_alias_getvp(struct devfs_node *node)
542 struct devfs_node *found = node;
543 int depth = 0;
545 while ((found->node_type == Nlink) && (found->link_target)) {
546 if (depth >= 8) {
547 devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8");
548 break;
551 found = found->link_target;
552 ++depth;
555 return found->v_node;
559 * Unlink the devfs node from the topology and add it to the orphan list.
560 * The node will later be destroyed by freep.
562 * Any vnode association, including the v_rdev and v_data, remains intact
563 * until the freep.
565 void
566 devfs_unlinkp(struct devfs_node *node)
568 struct devfs_node *parent;
569 struct devfs_node *target;
570 struct vnode *vp;
571 KKASSERT(node);
574 * Add the node to the orphan list, so it is referenced somewhere, to
575 * so we don't leak it.
577 devfs_tracer_add_orphan(node);
579 parent = node->parent;
580 node->parent = NULL;
583 * If the parent is known we can unlink the node out of the topology
585 if (node->flags & DEVFS_NODE_LINKED) {
586 if (parent) {
587 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
588 parent->nchildren--;
589 } else if (node == DEVFS_MNTDATA(node->mp)->root_node) {
590 DEVFS_MNTDATA(node->mp)->root_node = NULL;
592 node->flags &= ~DEVFS_NODE_LINKED;
596 * Namecache invalidation.
598 * devfs alias nodes are special: their v_node entry is always null
599 * and they use the one from their link target. We thus use the
600 * target node's vp to invalidate both alias and target entries in
601 * the namecache.
603 * Doing so for the target is not necessary but it would be more
604 * expensive to resolve only the namecache entry of the alias node
605 * from the information available in this function.
607 * WARNING! We do not disassociate the vnode here. That can only
608 * be safely done in devfs_freep().
610 if (node->node_type == Nlink) {
611 if ((target = node->link_target) != NULL) {
612 vp = devfs_alias_getvp(node);
613 node->link_target = NULL;
614 target->nlinks--;
615 if (target->nlinks == 0 &&
616 (target->flags & DEVFS_DESTROYED)) {
617 devfs_freep(target);
619 } else {
620 vp = NULL;
622 } else {
623 vp = node->v_node;
626 if (vp != NULL)
627 cache_inval_vp(vp, CINV_DESTROY);
630 void *
631 devfs_iterate_topology(struct devfs_node *node,
632 devfs_iterate_callback_t *callback, void *arg1)
634 struct devfs_node *node1, *node2;
635 void *ret = NULL;
637 if (((node->node_type == Nroot) || (node->node_type == Ndir)) &&
638 node->nchildren > 2) {
639 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
640 link, node2) {
641 ret = devfs_iterate_topology(node1, callback, arg1);
642 if (ret)
643 return ret;
646 ret = callback(node, arg1);
648 return ret;
651 static void *
652 devfs_alias_reaper_callback(struct devfs_node *node, void *unused)
654 if (node->node_type == Nlink) {
655 devfs_unlinkp(node);
656 devfs_freep(node);
659 return NULL;
663 * devfs_reaperp() is a recursive function that iterates through all the
664 * topology, unlinking and freeing all devfs nodes.
666 static void *
667 devfs_reaperp_callback(struct devfs_node *node, void *unused)
669 devfs_unlinkp(node);
670 devfs_freep(node);
672 return NULL;
676 * Report any orphans that we couldn't delete. The mp and mnt_data
677 * are both disappearing, so we must also clean up the nodes a bit.
679 static void
680 devfs_iterate_orphans_unmount(struct mount *mp)
682 struct devfs_orphan *orphan;
684 while ((orphan = TAILQ_FIRST(DEVFS_ORPHANLIST(mp))) != NULL) {
685 devfs_freep(orphan->node);
686 /* orphan stale */
690 static void *
691 devfs_gc_dirs_callback(struct devfs_node *node, void *unused)
693 if (node->node_type == Ndir) {
694 if ((node->nchildren == 2) &&
695 !(node->flags & DEVFS_USER_CREATED)) {
696 devfs_unlinkp(node);
697 devfs_freep(node);
701 return NULL;
704 static void *
705 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target)
707 if ((node->node_type == Nlink) && (node->link_target == target)) {
708 devfs_unlinkp(node);
709 devfs_freep(node);
712 return NULL;
716 * devfs_gc() is devfs garbage collector. It takes care of unlinking and
717 * freeing a node, but also removes empty directories and links that link
718 * via devfs auto-link mechanism to the node being deleted.
721 devfs_gc(struct devfs_node *node)
723 struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
725 if (node->nlinks > 0)
726 devfs_iterate_topology(root_node,
727 (devfs_iterate_callback_t *)devfs_gc_links_callback, node);
729 devfs_unlinkp(node);
730 devfs_iterate_topology(root_node,
731 (devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL);
733 devfs_freep(node);
735 return 0;
739 * devfs_create_dev() is the asynchronous entry point for device creation.
740 * It just sends a message with the relevant details to the devfs core.
742 * This function will reference the passed device. The reference is owned
743 * by devfs and represents all of the device's node associations.
746 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
748 reference_dev(dev);
749 devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
751 return 0;
755 * devfs_destroy_dev() is the asynchronous entry point for device destruction.
756 * It just sends a message with the relevant details to the devfs core.
759 devfs_destroy_dev(cdev_t dev)
761 devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
762 return 0;
766 * devfs_mount_add() is the synchronous entry point for adding a new devfs
767 * mount. It sends a synchronous message with the relevant details to the
768 * devfs core.
771 devfs_mount_add(struct devfs_mnt_data *mnt)
773 devfs_msg_t msg;
775 msg = devfs_msg_get();
776 msg->mdv_mnt = mnt;
777 devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
778 devfs_msg_put(msg);
780 return 0;
784 * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
785 * It sends a synchronous message with the relevant details to the devfs core.
788 devfs_mount_del(struct devfs_mnt_data *mnt)
790 devfs_msg_t msg;
792 msg = devfs_msg_get();
793 msg->mdv_mnt = mnt;
794 devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
795 devfs_msg_put(msg);
797 return 0;
801 * devfs_destroy_related() is the synchronous entry point for device
802 * destruction by subname. It just sends a message with the relevant details to
803 * the devfs core.
806 devfs_destroy_related(cdev_t dev)
808 devfs_msg_t msg;
810 msg = devfs_msg_get();
811 msg->mdv_load = dev;
812 devfs_msg_send_sync(DEVFS_DESTROY_RELATED, msg);
813 devfs_msg_put(msg);
814 return 0;
818 devfs_clr_related_flag(cdev_t dev, uint32_t flag)
820 devfs_msg_t msg;
822 msg = devfs_msg_get();
823 msg->mdv_flags.dev = dev;
824 msg->mdv_flags.flag = flag;
825 devfs_msg_send_sync(DEVFS_CLR_RELATED_FLAG, msg);
826 devfs_msg_put(msg);
828 return 0;
832 devfs_destroy_related_without_flag(cdev_t dev, uint32_t flag)
834 devfs_msg_t msg;
836 msg = devfs_msg_get();
837 msg->mdv_flags.dev = dev;
838 msg->mdv_flags.flag = flag;
839 devfs_msg_send_sync(DEVFS_DESTROY_RELATED_WO_FLAG, msg);
840 devfs_msg_put(msg);
842 return 0;
846 * devfs_create_all_dev is the asynchronous entry point to trigger device
847 * node creation. It just sends a message with the relevant details to
848 * the devfs core.
851 devfs_create_all_dev(struct devfs_node *root)
853 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
854 return 0;
858 * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
859 * devices with a specific set of dev_ops and minor. It just sends a
860 * message with the relevant details to the devfs core.
863 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
865 devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
866 return 0;
870 * devfs_clone_handler_add is the synchronous entry point to add a new
871 * clone handler. It just sends a message with the relevant details to
872 * the devfs core.
875 devfs_clone_handler_add(const char *name, d_clone_t *nhandler)
877 devfs_msg_t msg;
879 msg = devfs_msg_get();
880 msg->mdv_chandler.name = name;
881 msg->mdv_chandler.nhandler = nhandler;
882 devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg);
883 devfs_msg_put(msg);
884 return 0;
888 * devfs_clone_handler_del is the synchronous entry point to remove a
889 * clone handler. It just sends a message with the relevant details to
890 * the devfs core.
893 devfs_clone_handler_del(const char *name)
895 devfs_msg_t msg;
897 msg = devfs_msg_get();
898 msg->mdv_chandler.name = name;
899 msg->mdv_chandler.nhandler = NULL;
900 devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg);
901 devfs_msg_put(msg);
902 return 0;
906 * devfs_find_device_by_name is the synchronous entry point to find a
907 * device given its name. It sends a synchronous message with the
908 * relevant details to the devfs core and returns the answer.
910 cdev_t
911 devfs_find_device_by_name(const char *fmt, ...)
913 cdev_t found = NULL;
914 devfs_msg_t msg;
915 char *target;
916 __va_list ap;
918 if (fmt == NULL)
919 return NULL;
921 __va_start(ap, fmt);
922 kvasnprintf(&target, PATH_MAX, fmt, ap);
923 __va_end(ap);
925 msg = devfs_msg_get();
926 msg->mdv_name = target;
927 devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
928 found = msg->mdv_cdev;
929 devfs_msg_put(msg);
930 kvasfree(&target);
932 return found;
936 * devfs_find_device_by_devid is the synchronous entry point to find a
937 * device given its udev number. It sends a synchronous message with
938 * the relevant details to the devfs core and returns the answer.
940 cdev_t
941 devfs_find_device_by_devid(dev_t udev)
943 cdev_t found = NULL;
944 devfs_msg_t msg;
946 msg = devfs_msg_get();
947 msg->mdv_udev = udev;
948 devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_DEVID, msg);
949 found = msg->mdv_cdev;
950 devfs_msg_put(msg);
952 devfs_debug(DEVFS_DEBUG_DEBUG,
953 "devfs_find_device_by_devid found? %s -end:3-\n",
954 ((found) ? found->si_name:"NO"));
955 return found;
958 struct vnode *
959 devfs_inode_to_vnode(struct mount *mp, ino_t target)
961 struct vnode *vp = NULL;
962 devfs_msg_t msg;
964 if (mp == NULL)
965 return NULL;
967 msg = devfs_msg_get();
968 msg->mdv_ino.mp = mp;
969 msg->mdv_ino.ino = target;
970 devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg);
971 vp = msg->mdv_ino.vp;
972 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
973 devfs_msg_put(msg);
975 return vp;
979 * devfs_make_alias is the asynchronous entry point to register an alias
980 * for a device. It just sends a message with the relevant details to the
981 * devfs core.
984 devfs_make_alias(const char *name, cdev_t dev_target)
986 struct devfs_alias *alias;
987 size_t len;
989 len = strlen(name);
991 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
992 alias->name = kstrdup(name, M_DEVFS);
993 alias->namlen = len;
994 alias->dev_target = dev_target;
996 devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
997 return 0;
1001 * devfs_destroy_alias is the asynchronous entry point to deregister an alias
1002 * for a device. It just sends a message with the relevant details to the
1003 * devfs core.
1006 devfs_destroy_alias(const char *name, cdev_t dev_target)
1008 struct devfs_alias *alias;
1009 size_t len;
1011 len = strlen(name);
1013 alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
1014 alias->name = kstrdup(name, M_DEVFS);
1015 alias->namlen = len;
1016 alias->dev_target = dev_target;
1018 devfs_msg_send_generic(DEVFS_DESTROY_ALIAS, alias);
1019 return 0;
1023 * devfs_apply_rules is the asynchronous entry point to trigger application
1024 * of all rules. It just sends a message with the relevant details to the
1025 * devfs core.
1028 devfs_apply_rules(char *mntto)
1030 char *new_name;
1032 new_name = kstrdup(mntto, M_DEVFS);
1033 devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
1035 return 0;
1039 * devfs_reset_rules is the asynchronous entry point to trigger reset of all
1040 * rules. It just sends a message with the relevant details to the devfs core.
1043 devfs_reset_rules(char *mntto)
1045 char *new_name;
1047 new_name = kstrdup(mntto, M_DEVFS);
1048 devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
1050 return 0;
1055 * devfs_scan_callback is the asynchronous entry point to call a callback
1056 * on all cdevs.
1057 * It just sends a message with the relevant details to the devfs core.
1060 devfs_scan_callback(devfs_scan_t *callback, void *arg)
1062 devfs_msg_t msg;
1064 KKASSERT(callback);
1066 msg = devfs_msg_get();
1067 msg->mdv_load = callback;
1068 msg->mdv_load2 = arg;
1069 devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
1070 devfs_msg_put(msg);
1072 return 0;
1077 * Acts as a message drain. Any message that is replied to here gets destroyed
1078 * and the memory freed.
1080 static void
1081 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
1083 devfs_msg_put((devfs_msg_t)msg);
1087 * devfs_msg_get allocates a new devfs msg and returns it.
1089 devfs_msg_t
1090 devfs_msg_get(void)
1092 return objcache_get(devfs_msg_cache, M_WAITOK);
1096 * devfs_msg_put deallocates a given devfs msg.
1099 devfs_msg_put(devfs_msg_t msg)
1101 objcache_put(devfs_msg_cache, msg);
1102 return 0;
1106 * devfs_msg_send is the generic asynchronous message sending facility
1107 * for devfs. By default the reply port is the automatic disposal port.
1109 * If the current thread is the devfs_msg_port thread we execute the
1110 * operation synchronously.
1112 void
1113 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
1115 lwkt_port_t port = &devfs_msg_port;
1117 lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
1119 devfs_msg->hdr.u.ms_result = cmd;
1121 if (port->mpu_td == curthread) {
1122 devfs_msg_exec(devfs_msg);
1123 lwkt_replymsg(&devfs_msg->hdr, 0);
1124 } else {
1125 lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
1130 * devfs_msg_send_sync is the generic synchronous message sending
1131 * facility for devfs. It initializes a local reply port and waits
1132 * for the core's answer. The core will write the answer on the same
1133 * message which is sent back as reply. The caller still has a reference
1134 * to the message, so we don't need to return it.
1137 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
1139 struct lwkt_port rep_port;
1140 int error;
1141 lwkt_port_t port = &devfs_msg_port;
1143 lwkt_initport_thread(&rep_port, curthread);
1144 lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
1146 devfs_msg->hdr.u.ms_result = cmd;
1148 error = lwkt_domsg(port, (lwkt_msg_t)devfs_msg, 0);
1150 return error;
1154 * sends a message with a generic argument.
1156 void
1157 devfs_msg_send_generic(uint32_t cmd, void *load)
1159 devfs_msg_t devfs_msg = devfs_msg_get();
1161 devfs_msg->mdv_load = load;
1162 devfs_msg_send(cmd, devfs_msg);
1166 * sends a message with a name argument.
1168 void
1169 devfs_msg_send_name(uint32_t cmd, char *name)
1171 devfs_msg_t devfs_msg = devfs_msg_get();
1173 devfs_msg->mdv_name = name;
1174 devfs_msg_send(cmd, devfs_msg);
1178 * sends a message with a mount argument.
1180 void
1181 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
1183 devfs_msg_t devfs_msg = devfs_msg_get();
1185 devfs_msg->mdv_mnt = mnt;
1186 devfs_msg_send(cmd, devfs_msg);
1190 * sends a message with an ops argument.
1192 void
1193 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
1195 devfs_msg_t devfs_msg = devfs_msg_get();
1197 devfs_msg->mdv_ops.ops = ops;
1198 devfs_msg->mdv_ops.minor = minor;
1199 devfs_msg_send(cmd, devfs_msg);
1203 * sends a message with a clone handler argument.
1205 void
1206 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
1208 devfs_msg_t devfs_msg = devfs_msg_get();
1210 devfs_msg->mdv_chandler.name = name;
1211 devfs_msg->mdv_chandler.nhandler = handler;
1212 devfs_msg_send(cmd, devfs_msg);
1216 * sends a message with a device argument.
1218 void
1219 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1221 devfs_msg_t devfs_msg = devfs_msg_get();
1223 devfs_msg->mdv_dev.dev = dev;
1224 devfs_msg->mdv_dev.uid = uid;
1225 devfs_msg->mdv_dev.gid = gid;
1226 devfs_msg->mdv_dev.perms = perms;
1228 devfs_msg_send(cmd, devfs_msg);
1232 * sends a message with a link argument.
1234 void
1235 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1237 devfs_msg_t devfs_msg = devfs_msg_get();
1239 devfs_msg->mdv_link.name = name;
1240 devfs_msg->mdv_link.target = target;
1241 devfs_msg->mdv_link.mp = mp;
1242 devfs_msg_send(cmd, devfs_msg);
1246 * devfs_msg_core is the main devfs thread. It handles all incoming messages
1247 * and calls the relevant worker functions. By using messages it's assured
1248 * that events occur in the correct order.
1250 static void
1251 devfs_msg_core(void *arg)
1253 devfs_msg_t msg;
1255 lwkt_initport_thread(&devfs_msg_port, curthread);
1257 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1258 devfs_run = 1;
1259 wakeup(td_core);
1260 lockmgr(&devfs_lock, LK_RELEASE);
1262 lwkt_gettoken(&devfs_token);
1264 while (devfs_run) {
1265 msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1266 devfs_debug(DEVFS_DEBUG_DEBUG,
1267 "devfs_msg_core, new msg: %x\n",
1268 (unsigned int)msg->hdr.u.ms_result);
1269 devfs_msg_exec(msg);
1270 lwkt_replymsg(&msg->hdr, 0);
1273 lwkt_reltoken(&devfs_token);
1274 wakeup(td_core);
1276 lwkt_exit();
1279 static void
1280 devfs_msg_exec(devfs_msg_t msg)
1282 struct devfs_mnt_data *mnt;
1283 struct devfs_node *node;
1284 cdev_t dev;
1287 * Acquire the devfs lock to ensure safety of all called functions
1289 lockmgr(&devfs_lock, LK_EXCLUSIVE);
1291 switch (msg->hdr.u.ms_result) {
1292 case DEVFS_DEVICE_CREATE:
1293 dev = msg->mdv_dev.dev;
1294 devfs_create_dev_worker(dev,
1295 msg->mdv_dev.uid,
1296 msg->mdv_dev.gid,
1297 msg->mdv_dev.perms);
1298 break;
1299 case DEVFS_DEVICE_DESTROY:
1300 dev = msg->mdv_dev.dev;
1301 devfs_destroy_dev_worker(dev);
1302 break;
1303 case DEVFS_DESTROY_RELATED:
1304 devfs_destroy_related_worker(msg->mdv_load);
1305 break;
1306 case DEVFS_DESTROY_DEV_BY_OPS:
1307 devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1308 msg->mdv_ops.minor);
1309 break;
1310 case DEVFS_CREATE_ALL_DEV:
1311 node = (struct devfs_node *)msg->mdv_load;
1312 devfs_create_all_dev_worker(node);
1313 break;
1314 case DEVFS_MOUNT_ADD:
1315 mnt = msg->mdv_mnt;
1316 TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1317 devfs_create_all_dev_worker(mnt->root_node);
1318 break;
1319 case DEVFS_MOUNT_DEL:
1320 mnt = msg->mdv_mnt;
1321 TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1322 /* Be sure to remove all the aliases first */
1323 devfs_iterate_topology(mnt->root_node,
1324 devfs_alias_reaper_callback,
1325 NULL);
1326 devfs_iterate_topology(mnt->root_node,
1327 devfs_reaperp_callback,
1328 NULL);
1329 devfs_iterate_orphans_unmount(mnt->mp);
1330 if (mnt->leak_count) {
1331 devfs_debug(DEVFS_DEBUG_SHOW,
1332 "Leaked %ld devfs_node elements!\n",
1333 mnt->leak_count);
1335 break;
1336 case DEVFS_CHANDLER_ADD:
1337 devfs_chandler_add_worker(msg->mdv_chandler.name,
1338 msg->mdv_chandler.nhandler);
1339 break;
1340 case DEVFS_CHANDLER_DEL:
1341 devfs_chandler_del_worker(msg->mdv_chandler.name);
1342 break;
1343 case DEVFS_FIND_DEVICE_BY_NAME:
1344 devfs_find_device_by_name_worker(msg);
1345 break;
1346 case DEVFS_FIND_DEVICE_BY_DEVID:
1347 devfs_find_device_by_devid_worker(msg);
1348 break;
1349 case DEVFS_MAKE_ALIAS:
1350 devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1351 break;
1352 case DEVFS_DESTROY_ALIAS:
1353 devfs_destroy_alias_worker((struct devfs_alias *)msg->mdv_load);
1354 break;
1355 case DEVFS_APPLY_RULES:
1356 devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1357 break;
1358 case DEVFS_RESET_RULES:
1359 devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1360 break;
1361 case DEVFS_SCAN_CALLBACK:
1362 devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load,
1363 msg->mdv_load2);
1364 break;
1365 case DEVFS_CLR_RELATED_FLAG:
1366 devfs_clr_related_flag_worker(msg->mdv_flags.dev,
1367 msg->mdv_flags.flag);
1368 break;
1369 case DEVFS_DESTROY_RELATED_WO_FLAG:
1370 devfs_destroy_related_without_flag_worker(msg->mdv_flags.dev,
1371 msg->mdv_flags.flag);
1372 break;
1373 case DEVFS_INODE_TO_VNODE:
1374 msg->mdv_ino.vp = devfs_iterate_topology(
1375 DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node,
1376 (devfs_iterate_callback_t *)devfs_inode_to_vnode_worker_callback,
1377 &msg->mdv_ino.ino);
1378 break;
1379 case DEVFS_TERMINATE_CORE:
1380 devfs_run = 0;
1381 break;
1382 case DEVFS_SYNC:
1383 break;
1384 default:
1385 devfs_debug(DEVFS_DEBUG_WARNING,
1386 "devfs_msg_core: unknown message "
1387 "received at core\n");
1388 break;
1390 lockmgr(&devfs_lock, LK_RELEASE);
1393 static void
1394 devfs_devctl_notify(cdev_t dev, const char *ev)
1396 static const char prefix[] = "cdev=";
1397 char *data;
1398 int namelen;
1400 namelen = strlen(dev->si_name);
1401 data = kmalloc(namelen + sizeof(prefix), M_TEMP, M_WAITOK);
1402 memcpy(data, prefix, sizeof(prefix) - 1);
1403 memcpy(data + sizeof(prefix) - 1, dev->si_name, namelen + 1);
1404 devctl_notify("DEVFS", "CDEV", ev, data);
1405 kfree(data, M_TEMP);
1409 * Worker function to insert a new dev into the dev list and initialize its
1410 * permissions. It also calls devfs_propagate_dev which in turn propagates
1411 * the change to all mount points.
1413 * The passed dev is already referenced. This reference is eaten by this
1414 * function and represents the dev's linkage into devfs_dev_list.
1416 static int
1417 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1419 KKASSERT(dev);
1421 dev->si_uid = uid;
1422 dev->si_gid = gid;
1423 dev->si_perms = perms;
1425 devfs_link_dev(dev);
1426 devfs_propagate_dev(dev, 1);
1428 udev_event_attach(dev, NULL, 0);
1429 devfs_devctl_notify(dev, "CREATE");
1431 return 0;
1435 * Worker function to delete a dev from the dev list and free the cdev.
1436 * It also calls devfs_propagate_dev which in turn propagates the change
1437 * to all mount points.
1439 static int
1440 devfs_destroy_dev_worker(cdev_t dev)
1442 int error;
1444 KKASSERT(dev);
1445 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1447 error = devfs_unlink_dev(dev);
1448 devfs_propagate_dev(dev, 0);
1450 devfs_devctl_notify(dev, "DESTROY");
1451 udev_event_detach(dev, NULL, 0);
1453 if (error == 0)
1454 release_dev(dev); /* link ref */
1455 release_dev(dev);
1456 release_dev(dev);
1458 return 0;
1462 * Worker function to destroy all devices with a certain basename.
1463 * Calls devfs_destroy_dev_worker for the actual destruction.
1465 static int
1466 devfs_destroy_related_worker(cdev_t needle)
1468 cdev_t dev;
1470 restart:
1471 devfs_debug(DEVFS_DEBUG_DEBUG, "related worker: %s\n",
1472 needle->si_name);
1473 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1474 if (dev->si_parent == needle) {
1475 devfs_destroy_related_worker(dev);
1476 devfs_destroy_dev_worker(dev);
1477 goto restart;
1480 return 0;
1483 static int
1484 devfs_clr_related_flag_worker(cdev_t needle, uint32_t flag)
1486 cdev_t dev, dev1;
1488 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1489 if (dev->si_parent == needle) {
1490 devfs_clr_related_flag_worker(dev, flag);
1491 dev->si_flags &= ~flag;
1495 return 0;
1498 static int
1499 devfs_destroy_related_without_flag_worker(cdev_t needle, uint32_t flag)
1501 cdev_t dev;
1503 restart:
1504 devfs_debug(DEVFS_DEBUG_DEBUG, "related_wo_flag: %s\n",
1505 needle->si_name);
1507 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1508 if (dev->si_parent == needle) {
1509 devfs_destroy_related_without_flag_worker(dev, flag);
1510 if (!(dev->si_flags & flag)) {
1511 devfs_destroy_dev_worker(dev);
1512 devfs_debug(DEVFS_DEBUG_DEBUG,
1513 "related_wo_flag: %s restart\n", dev->si_name);
1514 goto restart;
1519 return 0;
1523 * Worker function that creates all device nodes on top of a devfs
1524 * root node.
1526 static int
1527 devfs_create_all_dev_worker(struct devfs_node *root)
1529 cdev_t dev;
1531 KKASSERT(root);
1533 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1534 devfs_create_device_node(root, dev, NULL, NULL, NULL);
1537 return 0;
1541 * Worker function that destroys all devices that match a specific
1542 * dev_ops and/or minor. If minor is less than 0, it is not matched
1543 * against. It also propagates all changes.
1545 static int
1546 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1548 cdev_t dev, dev1;
1550 KKASSERT(ops);
1552 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1553 if (dev->si_ops != ops)
1554 continue;
1555 if ((minor < 0) || (dev->si_uminor == minor)) {
1556 devfs_destroy_dev_worker(dev);
1560 return 0;
1564 * Worker function that registers a new clone handler in devfs.
1566 static int
1567 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler)
1569 struct devfs_clone_handler *chandler = NULL;
1570 u_char len = strlen(name);
1572 if (len == 0)
1573 return 1;
1575 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1576 if (chandler->namlen != len)
1577 continue;
1579 if (!memcmp(chandler->name, name, len)) {
1580 /* Clonable basename already exists */
1581 return 1;
1585 chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1586 chandler->name = kstrdup(name, M_DEVFS);
1587 chandler->namlen = len;
1588 chandler->nhandler = nhandler;
1590 TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1591 return 0;
1595 * Worker function that removes a given clone handler from the
1596 * clone handler list.
1598 static int
1599 devfs_chandler_del_worker(const char *name)
1601 struct devfs_clone_handler *chandler, *chandler2;
1602 u_char len = strlen(name);
1604 if (len == 0)
1605 return 1;
1607 TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1608 if (chandler->namlen != len)
1609 continue;
1610 if (memcmp(chandler->name, name, len))
1611 continue;
1613 TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1614 kfree(chandler->name, M_DEVFS);
1615 kfree(chandler, M_DEVFS);
1616 break;
1619 return 0;
1623 * Worker function that finds a given device name and changes
1624 * the message received accordingly so that when replied to,
1625 * the answer is returned to the caller.
1627 static int
1628 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1630 struct devfs_alias *alias;
1631 cdev_t dev;
1632 cdev_t found = NULL;
1634 TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1635 if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1636 found = dev;
1637 break;
1640 if (found == NULL) {
1641 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1642 if (strcmp(devfs_msg->mdv_name, alias->name) == 0) {
1643 found = alias->dev_target;
1644 break;
1648 devfs_msg->mdv_cdev = found;
1650 return 0;
1654 * Worker function that finds a given device udev and changes
1655 * the message received accordingly so that when replied to,
1656 * the answer is returned to the caller.
1658 static int
1659 devfs_find_device_by_devid_worker(devfs_msg_t devfs_msg)
1661 cdev_t dev, dev1;
1662 cdev_t found = NULL;
1664 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1665 if (((dev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1666 found = dev;
1667 break;
1670 devfs_msg->mdv_cdev = found;
1672 return 0;
1676 * Worker function that inserts a given alias into the
1677 * alias list, and propagates the alias to all mount
1678 * points.
1680 static int
1681 devfs_make_alias_worker(struct devfs_alias *alias)
1683 struct devfs_alias *alias2;
1684 size_t len = strlen(alias->name);
1685 int found = 0;
1687 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1688 if (len != alias2->namlen)
1689 continue;
1691 if (!memcmp(alias->name, alias2->name, len)) {
1692 found = 1;
1693 break;
1697 if (!found) {
1699 * The alias doesn't exist yet, so we add it to the alias list
1701 TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1702 devfs_alias_propagate(alias, 0);
1703 udev_event_attach(alias->dev_target, alias->name, 1);
1704 } else {
1705 devfs_debug(DEVFS_DEBUG_WARNING,
1706 "Warning: duplicate devfs_make_alias for %s\n",
1707 alias->name);
1708 kfree(alias->name, M_DEVFS);
1709 kfree(alias, M_DEVFS);
1712 return 0;
1716 * Worker function that delete a given alias from the
1717 * alias list, and propagates the removal to all mount
1718 * points.
1720 static int
1721 devfs_destroy_alias_worker(struct devfs_alias *alias)
1723 struct devfs_alias *alias2;
1724 int found = 0;
1726 TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1727 if (alias->dev_target != alias2->dev_target)
1728 continue;
1730 if (devfs_WildCmp(alias->name, alias2->name) == 0) {
1731 found = 1;
1732 break;
1736 if (!found) {
1737 devfs_debug(DEVFS_DEBUG_WARNING,
1738 "Warning: devfs_destroy_alias for inexistant alias: %s\n",
1739 alias->name);
1740 kfree(alias->name, M_DEVFS);
1741 kfree(alias, M_DEVFS);
1742 } else {
1744 * The alias exists, so we delete it from the alias list
1746 TAILQ_REMOVE(&devfs_alias_list, alias2, link);
1747 devfs_alias_propagate(alias2, 1);
1748 udev_event_detach(alias2->dev_target, alias2->name, 1);
1749 kfree(alias->name, M_DEVFS);
1750 kfree(alias, M_DEVFS);
1751 kfree(alias2->name, M_DEVFS);
1752 kfree(alias2, M_DEVFS);
1755 return 0;
1759 * Function that removes and frees all aliases.
1761 static int
1762 devfs_alias_reap(void)
1764 struct devfs_alias *alias, *alias2;
1766 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1767 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1768 kfree(alias->name, M_DEVFS);
1769 kfree(alias, M_DEVFS);
1771 return 0;
1775 * Function that removes an alias matching a specific cdev and frees
1776 * it accordingly.
1778 static int
1779 devfs_alias_remove(cdev_t dev)
1781 struct devfs_alias *alias, *alias2;
1783 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1784 if (alias->dev_target == dev) {
1785 TAILQ_REMOVE(&devfs_alias_list, alias, link);
1786 udev_event_detach(alias->dev_target, alias->name, 1);
1787 kfree(alias->name, M_DEVFS);
1788 kfree(alias, M_DEVFS);
1791 return 0;
1795 * This function propagates an alias addition or removal to
1796 * all mount points.
1798 static int
1799 devfs_alias_propagate(struct devfs_alias *alias, int remove)
1801 struct devfs_mnt_data *mnt;
1803 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1804 if (remove) {
1805 devfs_destroy_node(mnt->root_node, alias->name);
1806 } else {
1807 devfs_alias_apply(mnt->root_node, alias);
1810 return 0;
1814 * This function is a recursive function iterating through
1815 * all device nodes in the topology and, if applicable,
1816 * creating the relevant alias for a device node.
1818 static int
1819 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1821 struct devfs_node *node1, *node2;
1823 KKASSERT(alias != NULL);
1825 if ((node->node_type == Nroot) || (node->node_type == Ndir)) {
1826 if (node->nchildren > 2) {
1827 TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1828 devfs_alias_apply(node1, alias);
1831 } else {
1832 if (node->d_dev == alias->dev_target)
1833 devfs_alias_create(alias->name, node, 0);
1835 return 0;
1839 * This function checks if any alias possibly is applicable
1840 * to the given node. If so, the alias is created.
1842 static int
1843 devfs_alias_check_create(struct devfs_node *node)
1845 struct devfs_alias *alias;
1847 TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1848 if (node->d_dev == alias->dev_target)
1849 devfs_alias_create(alias->name, node, 0);
1851 return 0;
1855 * This function creates an alias with a given name
1856 * linking to a given devfs node. It also increments
1857 * the link count on the target node.
1860 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based)
1862 struct mount *mp = target->mp;
1863 struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1864 struct devfs_node *linknode;
1865 char *create_path = NULL;
1866 char *name;
1867 char *name_buf;
1868 int result = 0;
1870 KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1872 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1873 devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1875 if (create_path)
1876 parent = devfs_resolve_or_create_path(parent, create_path, 1);
1879 if (devfs_find_device_node_by_name(parent, name)) {
1880 devfs_debug(DEVFS_DEBUG_WARNING,
1881 "Node already exists: %s "
1882 "(devfs_make_alias_worker)!\n",
1883 name);
1884 result = 1;
1885 goto done;
1888 linknode = devfs_allocp(Nlink, name, parent, mp, NULL);
1889 if (linknode == NULL) {
1890 result = 1;
1891 goto done;
1894 linknode->link_target = target;
1895 target->nlinks++;
1897 if (rule_based)
1898 linknode->flags |= DEVFS_RULE_CREATED;
1900 done:
1901 kfree(name_buf, M_TEMP);
1902 return (result);
1906 * This function is called by the core and handles mount point
1907 * strings. It either calls the relevant worker (devfs_apply_
1908 * reset_rules_worker) on all mountpoints or only a specific
1909 * one.
1911 static int
1912 devfs_apply_reset_rules_caller(char *mountto, int apply)
1914 struct devfs_mnt_data *mnt;
1916 if (mountto[0] == '*') {
1917 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1918 devfs_iterate_topology(mnt->root_node,
1919 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1920 NULL);
1922 } else {
1923 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1924 if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) {
1925 devfs_iterate_topology(mnt->root_node,
1926 (apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1927 NULL);
1928 break;
1933 kfree(mountto, M_DEVFS);
1934 return 0;
1938 * This function calls a given callback function for
1939 * every dev node in the devfs dev list.
1941 static int
1942 devfs_scan_callback_worker(devfs_scan_t *callback, void *arg)
1944 cdev_t dev, dev1;
1945 struct devfs_alias *alias, *alias1;
1947 TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1948 callback(dev->si_name, dev, false, arg);
1950 TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias1) {
1951 callback(alias->name, alias->dev_target, true, arg);
1954 return 0;
1958 * This function tries to resolve a given directory, or if not
1959 * found and creation requested, creates the given directory.
1961 static struct devfs_node *
1962 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1963 size_t name_len, int create)
1965 struct devfs_node *node, *found = NULL;
1967 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1968 if (name_len != node->d_dir.d_namlen)
1969 continue;
1971 if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1972 found = node;
1973 break;
1977 if ((found == NULL) && (create)) {
1978 found = devfs_allocp(Ndir, dir_name, parent, parent->mp, NULL);
1981 return found;
1985 * This function tries to resolve a complete path. If creation is requested,
1986 * if a given part of the path cannot be resolved (because it doesn't exist),
1987 * it is created.
1989 struct devfs_node *
1990 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1992 struct devfs_node *node = parent;
1993 char *buf;
1994 size_t idx = 0;
1996 if (path == NULL)
1997 return parent;
1999 buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
2001 while (*path && idx < PATH_MAX - 1) {
2002 if (*path != '/') {
2003 buf[idx++] = *path;
2004 } else {
2005 buf[idx] = '\0';
2006 node = devfs_resolve_or_create_dir(node, buf, idx, create);
2007 if (node == NULL) {
2008 kfree(buf, M_TEMP);
2009 return NULL;
2011 idx = 0;
2013 ++path;
2015 buf[idx] = '\0';
2016 node = devfs_resolve_or_create_dir(node, buf, idx, create);
2017 kfree (buf, M_TEMP);
2018 return (node);
2022 * Takes a full path and strips it into a directory path and a name.
2023 * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
2024 * requires a working buffer with enough size to keep the whole
2025 * fullpath.
2028 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
2030 char *name = NULL;
2031 char *path = NULL;
2032 size_t len = strlen(fullpath) + 1;
2033 int i;
2035 KKASSERT((fullpath != NULL) && (buf != NULL));
2036 KKASSERT((pathp != NULL) && (namep != NULL));
2038 memcpy(buf, fullpath, len);
2040 for (i = len-1; i>= 0; i--) {
2041 if (buf[i] == '/') {
2042 buf[i] = '\0';
2043 name = &(buf[i+1]);
2044 path = buf;
2045 break;
2049 *pathp = path;
2051 if (name) {
2052 *namep = name;
2053 } else {
2054 *namep = buf;
2057 return 0;
2061 * This function creates a new devfs node for a given device. It can
2062 * handle a complete path as device name, and accordingly creates
2063 * the path and the final device node.
2065 * The reference count on the passed dev remains unchanged.
2067 struct devfs_node *
2068 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
2069 int *existsp, char *dev_name, char *path_fmt, ...)
2071 struct devfs_node *parent, *node = NULL;
2072 char *path = NULL;
2073 char *name;
2074 char *name_buf;
2075 __va_list ap;
2076 int i, found;
2077 char *create_path = NULL;
2078 char *names = "pqrsPQRS";
2080 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
2082 if (existsp)
2083 *existsp = 0;
2085 if (path_fmt != NULL) {
2086 __va_start(ap, path_fmt);
2087 kvasnprintf(&path, PATH_MAX, path_fmt, ap);
2088 __va_end(ap);
2091 parent = devfs_resolve_or_create_path(root, path, 1);
2092 KKASSERT(parent);
2094 devfs_resolve_name_path(
2095 ((dev_name == NULL) && (dev))?(dev->si_name):(dev_name),
2096 name_buf, &create_path, &name);
2098 if (create_path)
2099 parent = devfs_resolve_or_create_path(parent, create_path, 1);
2102 node = devfs_find_device_node_by_name(parent, name);
2103 if (node) {
2104 if (node->d_dev == dev) {
2106 * Allow case where device caches dev after the
2107 * close and might desire to reuse it.
2109 if (existsp)
2110 *existsp = 1;
2111 } else {
2112 devfs_debug(DEVFS_DEBUG_WARNING,
2113 "devfs_create_device_node: "
2114 "DEVICE %s ALREADY EXISTS!!! "
2115 "Ignoring creation request.\n",
2116 name);
2117 node = NULL;
2119 goto out;
2122 node = devfs_allocp(Ndev, name, parent, parent->mp, dev);
2123 vfs_timestamp(&parent->mtime);
2126 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
2127 * directory
2129 if ((dev) && (strlen(dev->si_name) >= 4) &&
2130 (!memcmp(dev->si_name, "ptm/", 4))) {
2131 node->parent->flags |= DEVFS_HIDDEN;
2132 node->flags |= DEVFS_HIDDEN;
2136 * Ugly pty magic, to tag pty devices as such and hide them if needed.
2138 if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
2139 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
2141 if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
2142 found = 0;
2143 for (i = 0; i < strlen(names); i++) {
2144 if (name[3] == names[i]) {
2145 found = 1;
2146 break;
2149 if (found)
2150 node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
2153 out:
2154 kfree(name_buf, M_TEMP);
2155 kvasfree(&path);
2156 return node;
2160 * This function finds a given device node in the topology with a given
2161 * cdev.
2163 void *
2164 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target)
2166 if ((node->node_type == Ndev) && (node->d_dev == target)) {
2167 return node;
2170 return NULL;
2174 * This function finds a device node in the given parent directory by its
2175 * name and returns it.
2177 struct devfs_node *
2178 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
2180 struct devfs_node *node, *found = NULL;
2181 size_t len = strlen(target);
2183 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
2184 if (len != node->d_dir.d_namlen)
2185 continue;
2187 if (!memcmp(node->d_dir.d_name, target, len)) {
2188 found = node;
2189 break;
2193 return found;
2196 static void *
2197 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop)
2199 struct vnode *vp = NULL;
2200 ino_t target = *inop;
2202 if (node->d_dir.d_ino == target) {
2203 if (node->v_node) {
2204 vp = node->v_node;
2205 vget(vp, LK_EXCLUSIVE | LK_RETRY);
2206 vn_unlock(vp);
2207 } else {
2208 devfs_allocv(&vp, node);
2209 vn_unlock(vp);
2213 return vp;
2217 * This function takes a cdev and removes its devfs node in the
2218 * given topology. The cdev remains intact.
2221 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
2223 KKASSERT(target != NULL);
2224 return devfs_destroy_node(root, target->si_name);
2228 * This function takes a path to a devfs node, resolves it and
2229 * removes the devfs node from the given topology.
2232 devfs_destroy_node(struct devfs_node *root, char *target)
2234 struct devfs_node *node, *parent;
2235 char *name;
2236 char *name_buf;
2237 char *create_path = NULL;
2239 KKASSERT(target);
2241 name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
2242 ksnprintf(name_buf, PATH_MAX, "%s", target);
2244 devfs_resolve_name_path(target, name_buf, &create_path, &name);
2246 if (create_path)
2247 parent = devfs_resolve_or_create_path(root, create_path, 0);
2248 else
2249 parent = root;
2251 if (parent == NULL) {
2252 kfree(name_buf, M_TEMP);
2253 return 1;
2256 node = devfs_find_device_node_by_name(parent, name);
2258 if (node) {
2259 vfs_timestamp(&node->parent->mtime);
2260 devfs_gc(node);
2263 kfree(name_buf, M_TEMP);
2265 return 0;
2269 * Just set perms and ownership for given node.
2272 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid,
2273 u_short mode, u_long flags)
2275 node->mode = mode;
2276 node->uid = uid;
2277 node->gid = gid;
2279 return 0;
2283 * Propagates a device attach/detach to all mount
2284 * points. Also takes care of automatic alias removal
2285 * for a deleted cdev.
2287 static int
2288 devfs_propagate_dev(cdev_t dev, int attach)
2290 struct devfs_mnt_data *mnt;
2292 TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
2293 if (attach) {
2294 /* Device is being attached */
2295 devfs_create_device_node(mnt->root_node, dev,
2296 NULL, NULL, NULL);
2297 } else {
2298 /* Device is being detached */
2299 devfs_alias_remove(dev);
2300 devfs_destroy_device_node(mnt->root_node, dev);
2303 return 0;
2307 * devfs_clone either returns a basename from a complete name by
2308 * returning the length of the name without trailing digits, or,
2309 * if clone != 0, calls the device's clone handler to get a new
2310 * device, which in turn is returned in devp.
2312 * Caller must hold a shared devfs_lock
2314 cdev_t
2315 devfs_clone(cdev_t dev, const char *name, size_t len, int mode,
2316 struct ucred *cred)
2318 int error;
2319 struct devfs_clone_handler *chandler;
2320 struct dev_clone_args ap;
2322 TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
2323 if (chandler->namlen != len)
2324 continue;
2325 if ((!memcmp(chandler->name, name, len)) &&
2326 (chandler->nhandler)) {
2328 * We have to unlock across the config and the
2329 * callback to avoid deadlocking. The device is
2330 * likely to obtain its own lock in the callback
2331 * and might then call into devfs.
2333 lockmgr(&devfs_lock, LK_RELEASE);
2334 devfs_config();
2335 ap.a_head.a_dev = dev;
2336 ap.a_dev = NULL;
2337 ap.a_name = name;
2338 ap.a_namelen = len;
2339 ap.a_mode = mode;
2340 ap.a_cred = cred;
2341 error = (chandler->nhandler)(&ap);
2342 lockmgr(&devfs_lock, LK_SHARED);
2343 if (error)
2344 continue;
2346 return ap.a_dev;
2350 return NULL;
2355 * Registers a new orphan in the orphan list.
2357 void
2358 devfs_tracer_add_orphan(struct devfs_node *node)
2360 struct devfs_orphan *orphan;
2362 KKASSERT(node);
2363 orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2364 orphan->node = node;
2366 KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2367 node->flags |= DEVFS_ORPHANED;
2368 TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2372 * Removes an orphan from the orphan list.
2374 void
2375 devfs_tracer_del_orphan(struct devfs_node *node)
2377 struct devfs_orphan *orphan;
2379 KKASSERT(node);
2381 TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link) {
2382 if (orphan->node == node) {
2383 node->flags &= ~DEVFS_ORPHANED;
2384 TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2385 kfree(orphan, M_DEVFS);
2386 break;
2392 * Counts the orphans in the orphan list, and if cleanup
2393 * is specified, also frees the orphan and removes it from
2394 * the list.
2396 size_t
2397 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2399 struct devfs_orphan *orphan, *orphan2;
2400 size_t count = 0;
2402 TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2) {
2403 count++;
2405 * If we are instructed to clean up, we do so.
2407 if (cleanup) {
2408 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2409 orphan->node->flags &= ~DEVFS_ORPHANED;
2410 devfs_freep(orphan->node);
2411 kfree(orphan, M_DEVFS);
2415 return count;
2419 * Fetch an ino_t from the global d_ino by increasing it
2420 * while spinlocked.
2422 static ino_t
2423 devfs_fetch_ino(void)
2425 ino_t ret;
2427 spin_lock(&ino_lock);
2428 ret = d_ino++;
2429 spin_unlock(&ino_lock);
2431 return ret;
2435 * Allocates a new cdev and initializes it's most basic
2436 * fields.
2438 cdev_t
2439 devfs_new_cdev(struct dev_ops *ops, int minor, struct dev_ops *bops)
2441 cdev_t dev = sysref_alloc(&cdev_sysref_class);
2443 sysref_activate(&dev->si_sysref);
2444 reference_dev(dev);
2445 bzero(dev, offsetof(struct cdev, si_sysref));
2447 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2449 dev->si_uid = 0;
2450 dev->si_gid = 0;
2451 dev->si_perms = 0;
2452 dev->si_drv1 = NULL;
2453 dev->si_drv2 = NULL;
2454 dev->si_lastread = 0; /* time_uptime */
2455 dev->si_lastwrite = 0; /* time_uptime */
2457 dev->si_dict = NULL;
2458 dev->si_parent = NULL;
2459 dev->si_ops = ops;
2460 dev->si_flags = 0;
2461 dev->si_uminor = minor;
2462 dev->si_bops = bops;
2465 * Since the disk subsystem is in the way, we need to
2466 * propagate the D_CANFREE from bops (and ops) to
2467 * si_flags.
2469 if (bops && (bops->head.flags & D_CANFREE)) {
2470 dev->si_flags |= SI_CANFREE;
2471 } else if (ops->head.flags & D_CANFREE) {
2472 dev->si_flags |= SI_CANFREE;
2475 /* If there is a backing device, we reference its ops */
2476 if (bops == NULL)
2477 bops = ops;
2478 dev->si_inode = makeudev(devfs_reference_ops(bops), minor);
2479 dev->si_umajor = umajor(dev->si_inode);
2481 lockmgr(&devfs_lock, LK_RELEASE);
2483 return dev;
2486 static void
2487 devfs_cdev_terminate(cdev_t dev)
2490 * Make sure the node isn't linked anymore. Otherwise we've screwed
2491 * up somewhere, since normal devs are unlinked on the call to
2492 * destroy_dev and only-cdevs that have not been used for cloning
2493 * are not linked in the first place. only-cdevs used for cloning
2494 * will be linked in, too, and should only be destroyed via
2495 * destroy_dev, not destroy_only_dev, so we catch that problem, too.
2497 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2499 /* If there is a backing device, we release the backing device's ops */
2500 devfs_release_ops((dev->si_bops)?(dev->si_bops):(dev->si_ops));
2502 /* devfs_cdev_unlock() is not called, unlock ourselves */
2503 lockmgr(&devfs_lock, LK_RELEASE);
2505 /* Finally destroy the device */
2506 sysref_put(&dev->si_sysref);
2510 * Dummies for now (individual locks for MPSAFE)
2512 static void
2513 devfs_cdev_lock(cdev_t dev)
2515 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2518 static void
2519 devfs_cdev_unlock(cdev_t dev)
2521 lockmgr(&devfs_lock, LK_RELEASE);
2524 static int
2525 devfs_detached_filter_eof(struct knote *kn, long hint)
2527 kn->kn_flags |= (EV_EOF | EV_NODATA);
2528 return (1);
2531 static void
2532 devfs_detached_filter_detach(struct knote *kn)
2534 cdev_t dev = (cdev_t)kn->kn_hook;
2536 knote_remove(&dev->si_kqinfo.ki_note, kn);
2539 static struct filterops devfs_detached_filterops =
2540 { FILTEROP_ISFD, NULL,
2541 devfs_detached_filter_detach,
2542 devfs_detached_filter_eof };
2545 * Delegates knote filter handling responsibility to devfs
2547 * Any device that implements kqfilter event handling and could be detached
2548 * or shut down out from under the kevent subsystem must allow devfs to
2549 * assume responsibility for any knotes it may hold.
2551 void
2552 devfs_assume_knotes(cdev_t dev, struct kqinfo *kqi)
2555 * Let kern/kern_event.c do the heavy lifting.
2557 knote_assume_knotes(kqi, &dev->si_kqinfo,
2558 &devfs_detached_filterops, (void *)dev);
2561 * These should probably be activated individually, but doing so
2562 * would require refactoring kq's public in-kernel interface.
2564 KNOTE(&dev->si_kqinfo.ki_note, 0);
2568 * Links a given cdev into the dev list.
2571 devfs_link_dev(cdev_t dev)
2573 KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2574 dev->si_flags |= SI_DEVFS_LINKED;
2575 TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2577 return 0;
2581 * Removes a given cdev from the dev list. The caller is responsible for
2582 * releasing the reference on the device associated with the linkage.
2584 * Returns EALREADY if the dev has already been unlinked.
2586 static int
2587 devfs_unlink_dev(cdev_t dev)
2589 if ((dev->si_flags & SI_DEVFS_LINKED)) {
2590 TAILQ_REMOVE(&devfs_dev_list, dev, link);
2591 dev->si_flags &= ~SI_DEVFS_LINKED;
2592 return (0);
2594 return (EALREADY);
2598 devfs_node_is_accessible(struct devfs_node *node)
2600 if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2601 return 1;
2602 else
2603 return 0;
2607 * devfs must be locked
2609 static int
2610 devfs_reference_ops(struct dev_ops *ops)
2612 int unit;
2613 struct devfs_dev_ops *found = NULL;
2614 struct devfs_dev_ops *devops;
2616 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2617 if (devops->ops == ops) {
2618 found = devops;
2619 break;
2623 if (!found) {
2624 found = kmalloc(sizeof(struct devfs_dev_ops),
2625 M_DEVFS, M_WAITOK);
2626 found->ops = ops;
2627 found->ref_count = 0;
2628 TAILQ_INSERT_TAIL(&devfs_dev_ops_list, found, link);
2631 KKASSERT(found);
2633 if (found->ref_count == 0) {
2634 found->id =
2635 devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255);
2636 if (found->id == -1) {
2637 /* Ran out of unique ids */
2638 devfs_debug(DEVFS_DEBUG_WARNING,
2639 "devfs_reference_ops: WARNING: ran "
2640 "out of unique ids\n");
2643 unit = found->id;
2644 ++found->ref_count;
2646 return unit;
2650 * devfs must be locked
2652 static void
2653 devfs_release_ops(struct dev_ops *ops)
2655 struct devfs_dev_ops *found = NULL;
2656 struct devfs_dev_ops *devops;
2658 TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2659 if (devops->ops == ops) {
2660 found = devops;
2661 break;
2665 KKASSERT(found);
2667 --found->ref_count;
2669 if (found->ref_count == 0) {
2670 TAILQ_REMOVE(&devfs_dev_ops_list, found, link);
2671 lockmgr(&devfs_lock, LK_RELEASE);
2672 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), found->id);
2673 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2674 kfree(found, M_DEVFS);
2679 * Wait for asynchronous messages to complete in the devfs helper
2680 * thread, then return. Do nothing if the helper thread is dead
2681 * or we are being indirectly called from the helper thread itself.
2683 void
2684 devfs_config(void)
2686 devfs_msg_t msg;
2688 if (devfs_run && curthread != td_core) {
2689 msg = devfs_msg_get();
2690 devfs_msg_send_sync(DEVFS_SYNC, msg);
2691 devfs_msg_put(msg);
2696 * Called on init of devfs; creates the objcaches and
2697 * spawns off the devfs core thread. Also initializes
2698 * locks.
2700 static void
2701 devfs_init(void)
2703 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2704 /* Create objcaches for nodes, msgs and devs */
2705 devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2706 NULL, NULL, NULL,
2707 objcache_malloc_alloc,
2708 objcache_malloc_free,
2709 &devfs_node_malloc_args );
2711 devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2712 NULL, NULL, NULL,
2713 objcache_malloc_alloc,
2714 objcache_malloc_free,
2715 &devfs_msg_malloc_args );
2717 devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2718 NULL, NULL, NULL,
2719 objcache_malloc_alloc,
2720 objcache_malloc_free,
2721 &devfs_dev_malloc_args );
2723 devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2725 /* Initialize the reply-only port which acts as a message drain */
2726 lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2728 /* Initialize *THE* devfs lock */
2729 lockinit(&devfs_lock, "devfs_core lock", 0, LK_CANRECURSE);
2730 lwkt_token_init(&devfs_token, "devfs_core");
2732 lockmgr(&devfs_lock, LK_EXCLUSIVE);
2733 lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2734 0, -1, "devfs_msg_core");
2735 while (devfs_run == 0)
2736 lksleep(td_core, &devfs_lock, 0, "devfsc", 0);
2737 lockmgr(&devfs_lock, LK_RELEASE);
2739 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2743 * Called on unload of devfs; takes care of destroying the core
2744 * and the objcaches. Also removes aliases that are no longer needed.
2746 static void
2747 devfs_uninit(void)
2749 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2751 devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2752 while (devfs_run)
2753 tsleep(td_core, 0, "devfsc", hz*10);
2754 tsleep(td_core, 0, "devfsc", hz);
2756 devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2758 /* Destroy the objcaches */
2759 objcache_destroy(devfs_msg_cache);
2760 objcache_destroy(devfs_node_cache);
2761 objcache_destroy(devfs_dev_cache);
2763 devfs_alias_reap();
2767 * This is a sysctl handler to assist userland devname(3) to
2768 * find the device name for a given udev.
2770 static int
2771 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2773 dev_t udev;
2774 cdev_t found;
2775 int error;
2777 if ((error = SYSCTL_IN(req, &udev, sizeof(dev_t))))
2778 return (error);
2780 devfs_debug(DEVFS_DEBUG_DEBUG,
2781 "devfs sysctl, received udev: %d\n", udev);
2783 if (udev == NOUDEV)
2784 return(EINVAL);
2786 if ((found = devfs_find_device_by_devid(udev)) == NULL)
2787 return(ENOENT);
2789 return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2793 SYSCTL_PROC(_kern, OID_AUTO, devname,
2794 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NOLOCK,
2795 NULL, 0, devfs_sysctl_devname_helper, "",
2796 "helper for devname(3)");
2798 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2799 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2800 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable,
2801 0, "Enable DevFS debugging");
2803 SYSINIT(vfs_devfs_register, SI_SUB_DEVFS_CORE, SI_ORDER_FIRST,
2804 devfs_init, NULL);
2805 SYSUNINIT(vfs_devfs_register, SI_SUB_DEVFS_CORE, SI_ORDER_ANY,
2806 devfs_uninit, NULL);
2809 * WildCmp() - compare wild string to sane string
2811 * Returns 0 on success, -1 on failure.
2813 static int
2814 wildCmp(const char **mary, int d, const char *w, const char *s)
2816 int i;
2819 * skip fixed portion
2821 for (;;) {
2822 switch(*w) {
2823 case '*':
2825 * optimize terminator
2827 if (w[1] == 0)
2828 return(0);
2829 if (w[1] != '?' && w[1] != '*') {
2831 * optimize * followed by non-wild
2833 for (i = 0; s + i < mary[d]; ++i) {
2834 if (s[i] == w[1] && wildCmp(mary, d + 1, w + 1, s + i) == 0)
2835 return(0);
2837 } else {
2839 * less-optimal
2841 for (i = 0; s + i < mary[d]; ++i) {
2842 if (wildCmp(mary, d + 1, w + 1, s + i) == 0)
2843 return(0);
2846 mary[d] = s;
2847 return(-1);
2848 case '?':
2849 if (*s == 0)
2850 return(-1);
2851 ++w;
2852 ++s;
2853 break;
2854 default:
2855 if (*w != *s)
2856 return(-1);
2857 if (*w == 0) /* terminator */
2858 return(0);
2859 ++w;
2860 ++s;
2861 break;
2864 /* not reached */
2865 return(-1);
2870 * WildCaseCmp() - compare wild string to sane string, case insensitive
2872 * Returns 0 on success, -1 on failure.
2874 static int
2875 wildCaseCmp(const char **mary, int d, const char *w, const char *s)
2877 int i;
2880 * skip fixed portion
2882 for (;;) {
2883 switch(*w) {
2884 case '*':
2886 * optimize terminator
2888 if (w[1] == 0)
2889 return(0);
2890 if (w[1] != '?' && w[1] != '*') {
2892 * optimize * followed by non-wild
2894 for (i = 0; s + i < mary[d]; ++i) {
2895 if (s[i] == w[1] && wildCaseCmp(mary, d + 1, w + 1, s + i) == 0)
2896 return(0);
2898 } else {
2900 * less-optimal
2902 for (i = 0; s + i < mary[d]; ++i) {
2903 if (wildCaseCmp(mary, d + 1, w + 1, s + i) == 0)
2904 return(0);
2907 mary[d] = s;
2908 return(-1);
2909 case '?':
2910 if (*s == 0)
2911 return(-1);
2912 ++w;
2913 ++s;
2914 break;
2915 default:
2916 if (*w != *s) {
2917 #define tolower(x) ((x >= 'A' && x <= 'Z')?(x+('a'-'A')):(x))
2918 if (tolower(*w) != tolower(*s))
2919 return(-1);
2921 if (*w == 0) /* terminator */
2922 return(0);
2923 ++w;
2924 ++s;
2925 break;
2928 /* not reached */
2929 return(-1);
2932 struct cdev_privdata {
2933 void *cdpd_data;
2934 d_priv_dtor_t *cdpd_dtr;
2938 devfs_get_cdevpriv(struct file *fp, void **datap)
2940 int error;
2942 if (fp == NULL)
2943 return(EBADF);
2945 spin_lock_shared(&fp->f_spin);
2946 if (fp->f_data1 == NULL) {
2947 *datap = NULL;
2948 error = ENOENT;
2949 } else {
2950 struct cdev_privdata *p = fp->f_data1;
2952 *datap = p->cdpd_data;
2953 error = 0;
2955 spin_unlock_shared(&fp->f_spin);
2957 return (error);
2961 devfs_set_cdevpriv(struct file *fp, void *priv, d_priv_dtor_t *dtr)
2963 struct cdev_privdata *p;
2964 int error;
2966 if (fp == NULL)
2967 return (ENOENT);
2969 p = kmalloc(sizeof(struct cdev_privdata), M_DEVFS, M_WAITOK);
2970 p->cdpd_data = priv;
2971 p->cdpd_dtr = dtr;
2973 spin_lock(&fp->f_spin);
2974 if (fp->f_data1 == NULL) {
2975 fp->f_data1 = p;
2976 error = 0;
2977 } else {
2978 error = EBUSY;
2980 spin_unlock(&fp->f_spin);
2982 if (error)
2983 kfree(p, M_DEVFS);
2985 return error;
2988 void
2989 devfs_clear_cdevpriv(struct file *fp)
2991 struct cdev_privdata *p;
2993 if (fp == NULL)
2994 return;
2996 spin_lock(&fp->f_spin);
2997 p = fp->f_data1;
2998 fp->f_data1 = NULL;
2999 spin_unlock(&fp->f_spin);
3001 if (p != NULL) {
3002 p->cdpd_dtr(p->cdpd_data);
3003 kfree(p, M_DEVFS);
3008 devfs_WildCmp(const char *w, const char *s)
3010 int i;
3011 int c;
3012 int slen = strlen(s);
3013 const char **mary;
3015 for (i = c = 0; w[i]; ++i) {
3016 if (w[i] == '*')
3017 ++c;
3019 mary = kmalloc(sizeof(char *) * (c + 1), M_DEVFS, M_WAITOK);
3020 for (i = 0; i < c; ++i)
3021 mary[i] = s + slen;
3022 i = wildCmp(mary, 0, w, s);
3023 kfree(mary, M_DEVFS);
3024 return(i);
3028 devfs_WildCaseCmp(const char *w, const char *s)
3030 int i;
3031 int c;
3032 int slen = strlen(s);
3033 const char **mary;
3035 for (i = c = 0; w[i]; ++i) {
3036 if (w[i] == '*')
3037 ++c;
3039 mary = kmalloc(sizeof(char *) * (c + 1), M_DEVFS, M_WAITOK);
3040 for (i = 0; i < c; ++i)
3041 mary[i] = s + slen;
3042 i = wildCaseCmp(mary, 0, w, s);
3043 kfree(mary, M_DEVFS);
3044 return(i);