2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/mount.h>
39 #include <sys/vnode.h>
40 #include <sys/types.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>
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
= {
66 .proto
= SYSREF_PROTO_DEV
,
67 .offset
= offsetof(struct cdev
, si_sysref
),
68 .objsize
= sizeof(struct cdev
),
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
;
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
*);
152 devfs_inode_to_vnode_worker_callback(struct devfs_node
*, ino_t
*);
155 * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function
159 devfs_debug(int level
, char *fmt
, ...)
164 if (level
<= devfs_debug_enable
)
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
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);
192 node
->d_dir
.d_ino
= devfs_fetch_ino();
195 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries
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
) {
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
;
215 TAILQ_INIT(DEVFS_DENODE_HEAD(node
));
216 node
->d_dir
.d_type
= DT_DIR
;
221 node
->d_dir
.d_type
= DT_LNK
;
225 node
->d_dir
.d_type
= DT_REG
;
230 node
->d_dir
.d_type
= DT_CHR
;
233 node
->mode
= dev
->si_perms
;
234 node
->uid
= dev
->si_uid
;
235 node
->gid
= dev
->si_gid
;
237 devfs_alias_check_create(node
);
242 panic("devfs_allocp: unknown node type");
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
265 if ((parent
!= NULL
) &&
266 ((parent
->node_type
== Nroot
) || (parent
->node_type
== Ndir
))) {
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);
288 * devfs_allocv() allocates a new vnode based on a devfs node.
291 devfs_allocv(struct vnode
**vpp
, struct devfs_node
*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
305 while ((vp
= node
->v_node
) != NULL
) {
307 lockmgr(&devfs_lock
, LK_RELEASE
);
308 error
= vget(vp
, LK_EXCLUSIVE
);
310 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
315 if (error
!= ENOENT
) {
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
);
329 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
333 if (node
->v_node
!= NULL
) {
342 switch (node
->node_type
) {
344 vsetflags(vp
, VROOT
);
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
;
370 panic("devfs_allocv: unknown node type");
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
);
390 devfs_allocv(vpp
, node
);
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
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
)
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
);
439 while ((vp
= node
->v_node
) != NULL
) {
440 if (vget(vp
, LK_EXCLUSIVE
| LK_RETRY
) != 0)
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
);
471 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
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
;
484 while ((found
->node_type
== Nlink
) && (found
->link_target
)) {
486 devfs_debug(DEVFS_DEBUG_SHOW
, "Recursive link or depth >= 8");
490 found
= found
->link_target
;
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
505 devfs_unlinkp(struct devfs_node
*node
)
508 struct devfs_node
*parent
;
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
523 TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent
), node
, link
);
525 node
->flags
&= ~DEVFS_NODE_LINKED
;
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
);
546 cache_inval_vp(vp
, CINV_DESTROY
);
552 devfs_iterate_topology(struct devfs_node
*node
,
553 devfs_iterate_callback_t
*callback
, void *arg1
)
555 struct devfs_node
*node1
, *node2
;
558 if ((node
->node_type
== Nroot
) || (node
->node_type
== Ndir
)) {
559 if (node
->nchildren
> 2) {
560 TAILQ_FOREACH_MUTABLE(node1
, DEVFS_DENODE_HEAD(node
),
562 if ((ret
= devfs_iterate_topology(node1
, callback
, arg1
)))
568 ret
= callback(node
, arg1
);
573 devfs_alias_reaper_callback(struct devfs_node
*node
, void *unused
)
575 if (node
->node_type
== Nlink
) {
584 * devfs_reaperp() is a recursive function that iterates through all the
585 * topology, unlinking and freeing all devfs nodes.
588 devfs_reaperp_callback(struct devfs_node
*node
, void *unused
)
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
)) {
611 devfs_gc_links_callback(struct devfs_node
*node
, struct devfs_node
*target
)
613 if ((node
->node_type
== Nlink
) && (node
->link_target
== target
)) {
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
);
636 devfs_iterate_topology(root_node
,
637 (devfs_iterate_callback_t
*)devfs_gc_dirs_callback
, NULL
);
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
)
655 devfs_msg_send_dev(DEVFS_DEVICE_CREATE
, dev
, uid
, gid
, perms
);
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);
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
677 devfs_mount_add(struct devfs_mnt_data
*mnt
)
681 msg
= devfs_msg_get();
683 devfs_msg_send_sync(DEVFS_MOUNT_ADD
, msg
);
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
)
698 msg
= devfs_msg_get();
700 devfs_msg_send_sync(DEVFS_MOUNT_DEL
, msg
);
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
712 devfs_destroy_related(cdev_t dev
)
716 msg
= devfs_msg_get();
718 devfs_msg_send_sync(DEVFS_DESTROY_RELATED
, msg
);
724 devfs_clr_related_flag(cdev_t dev
, uint32_t flag
)
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
);
738 devfs_destroy_related_without_flag(cdev_t dev
, uint32_t flag
)
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
);
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
757 devfs_create_all_dev(struct devfs_node
*root
)
759 devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV
, root
);
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
);
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
781 devfs_clone_handler_add(const char *name
, d_clone_t
*nhandler
)
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
);
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
799 devfs_clone_handler_del(const char *name
)
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
);
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.
817 devfs_find_device_by_name(const char *fmt
, ...)
828 kvasnrprintf(&target
, PATH_MAX
, 10, fmt
, 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
;
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.
847 devfs_find_device_by_udev(udev_t udev
)
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
;
858 devfs_debug(DEVFS_DEBUG_DEBUG
,
859 "devfs_find_device_by_udev found? %s -end:3-\n",
860 ((found
) ? found
->si_name
:"NO"));
865 devfs_inode_to_vnode(struct mount
*mp
, ino_t target
)
867 struct vnode
*vp
= 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
);
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
890 devfs_make_alias(const char *name
, cdev_t dev_target
)
892 struct devfs_alias
*alias
;
897 alias
= kmalloc(sizeof(struct devfs_alias
), M_DEVFS
, M_WAITOK
);
898 alias
->name
= kstrdup(name
, M_DEVFS
);
900 alias
->dev_target
= dev_target
;
902 devfs_msg_send_generic(DEVFS_MAKE_ALIAS
, alias
);
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
912 devfs_destroy_alias(const char *name
, cdev_t dev_target
)
914 struct devfs_alias
*alias
;
919 alias
= kmalloc(sizeof(struct devfs_alias
), M_DEVFS
, M_WAITOK
);
920 alias
->name
= kstrdup(name
, M_DEVFS
);
922 alias
->dev_target
= dev_target
;
924 devfs_msg_send_generic(DEVFS_DESTROY_ALIAS
, alias
);
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
934 devfs_apply_rules(char *mntto
)
938 new_name
= kstrdup(mntto
, M_DEVFS
);
939 devfs_msg_send_name(DEVFS_APPLY_RULES
, new_name
);
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
)
953 new_name
= kstrdup(mntto
, M_DEVFS
);
954 devfs_msg_send_name(DEVFS_RESET_RULES
, new_name
);
961 * devfs_scan_callback is the asynchronous entry point to call a callback
963 * It just sends a message with the relevant details to the devfs core.
966 devfs_scan_callback(devfs_scan_t
*callback
, void *arg
)
972 msg
= devfs_msg_get();
973 msg
->mdv_load
= callback
;
974 msg
->mdv_load2
= arg
;
975 devfs_msg_send_sync(DEVFS_SCAN_CALLBACK
, msg
);
983 * Acts as a message drain. Any message that is replied to here gets destroyed
984 * and the memory freed.
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.
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
);
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.
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);
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
;
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);
1060 * sends a message with a generic argument.
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.
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.
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.
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.
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.
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.
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.
1157 devfs_msg_core(void *arg
)
1161 lwkt_initport_thread(&devfs_msg_port
, curthread
);
1163 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
1166 lockmgr(&devfs_lock
, LK_RELEASE
);
1168 lwkt_gettoken(&devfs_token
);
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
);
1186 devfs_msg_exec(devfs_msg_t msg
)
1188 struct devfs_mnt_data
*mnt
;
1189 struct devfs_node
*node
;
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
,
1203 msg
->mdv_dev
.perms
);
1205 case DEVFS_DEVICE_DESTROY
:
1206 dev
= msg
->mdv_dev
.dev
;
1207 devfs_destroy_dev_worker(dev
);
1209 case DEVFS_DESTROY_RELATED
:
1210 devfs_destroy_related_worker(msg
->mdv_load
);
1212 case DEVFS_DESTROY_DEV_BY_OPS
:
1213 devfs_destroy_dev_by_ops_worker(msg
->mdv_ops
.ops
,
1214 msg
->mdv_ops
.minor
);
1216 case DEVFS_CREATE_ALL_DEV
:
1217 node
= (struct devfs_node
*)msg
->mdv_load
;
1218 devfs_create_all_dev_worker(node
);
1220 case DEVFS_MOUNT_ADD
:
1222 TAILQ_INSERT_TAIL(&devfs_mnt_list
, mnt
, link
);
1223 devfs_create_all_dev_worker(mnt
->root_node
);
1225 case DEVFS_MOUNT_DEL
:
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
,
1231 devfs_iterate_topology(mnt
->root_node
, devfs_reaperp_callback
,
1233 if (mnt
->leak_count
) {
1234 devfs_debug(DEVFS_DEBUG_SHOW
,
1235 "Leaked %ld devfs_node elements!\n",
1239 case DEVFS_CHANDLER_ADD
:
1240 devfs_chandler_add_worker(msg
->mdv_chandler
.name
,
1241 msg
->mdv_chandler
.nhandler
);
1243 case DEVFS_CHANDLER_DEL
:
1244 devfs_chandler_del_worker(msg
->mdv_chandler
.name
);
1246 case DEVFS_FIND_DEVICE_BY_NAME
:
1247 devfs_find_device_by_name_worker(msg
);
1249 case DEVFS_FIND_DEVICE_BY_UDEV
:
1250 devfs_find_device_by_udev_worker(msg
);
1252 case DEVFS_MAKE_ALIAS
:
1253 devfs_make_alias_worker((struct devfs_alias
*)msg
->mdv_load
);
1255 case DEVFS_DESTROY_ALIAS
:
1256 devfs_destroy_alias_worker((struct devfs_alias
*)msg
->mdv_load
);
1258 case DEVFS_APPLY_RULES
:
1259 devfs_apply_reset_rules_caller(msg
->mdv_name
, 1);
1261 case DEVFS_RESET_RULES
:
1262 devfs_apply_reset_rules_caller(msg
->mdv_name
, 0);
1264 case DEVFS_SCAN_CALLBACK
:
1265 devfs_scan_callback_worker((devfs_scan_t
*)msg
->mdv_load
,
1268 case DEVFS_CLR_RELATED_FLAG
:
1269 devfs_clr_related_flag_worker(msg
->mdv_flags
.dev
,
1270 msg
->mdv_flags
.flag
);
1272 case DEVFS_DESTROY_RELATED_WO_FLAG
:
1273 devfs_destroy_related_without_flag_worker(msg
->mdv_flags
.dev
,
1274 msg
->mdv_flags
.flag
);
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
,
1282 case DEVFS_TERMINATE_CORE
:
1288 devfs_debug(DEVFS_DEBUG_WARNING
,
1289 "devfs_msg_core: unknown message "
1290 "received at core\n");
1293 lockmgr(&devfs_lock
, LK_RELEASE
);
1297 devfs_devctl_notify(cdev_t dev
, const char *ev
)
1299 static const char prefix
[] = "cdev=";
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.
1320 devfs_create_dev_worker(cdev_t dev
, uid_t uid
, gid_t gid
, int perms
)
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");
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.
1343 devfs_destroy_dev_worker(cdev_t 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);
1357 release_dev(dev
); /* link ref */
1365 * Worker function to destroy all devices with a certain basename.
1366 * Calls devfs_destroy_dev_worker for the actual destruction.
1369 devfs_destroy_related_worker(cdev_t needle
)
1374 devfs_debug(DEVFS_DEBUG_DEBUG
, "related worker: %s\n",
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
);
1387 devfs_clr_related_flag_worker(cdev_t needle
, uint32_t flag
)
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
;
1402 devfs_destroy_related_without_flag_worker(cdev_t needle
, uint32_t flag
)
1407 devfs_debug(DEVFS_DEBUG_DEBUG
, "related_wo_flag: %s\n",
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
);
1426 * Worker function that creates all device nodes on top of a devfs
1430 devfs_create_all_dev_worker(struct devfs_node
*root
)
1436 TAILQ_FOREACH(dev
, &devfs_dev_list
, link
) {
1437 devfs_create_device_node(root
, dev
, NULL
, NULL
, NULL
);
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.
1449 devfs_destroy_dev_by_ops_worker(struct dev_ops
*ops
, int minor
)
1455 TAILQ_FOREACH_MUTABLE(dev
, &devfs_dev_list
, link
, dev1
) {
1456 if (dev
->si_ops
!= ops
)
1458 if ((minor
< 0) || (dev
->si_uminor
== minor
)) {
1459 devfs_destroy_dev_worker(dev
);
1467 * Worker function that registers a new clone handler in devfs.
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
);
1478 TAILQ_FOREACH(chandler
, &devfs_chandler_list
, link
) {
1479 if (chandler
->namlen
!= len
)
1482 if (!memcmp(chandler
->name
, name
, len
)) {
1483 /* Clonable basename already exists */
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
);
1498 * Worker function that removes a given clone handler from the
1499 * clone handler list.
1502 devfs_chandler_del_worker(const char *name
)
1504 struct devfs_clone_handler
*chandler
, *chandler2
;
1505 u_char len
= strlen(name
);
1510 TAILQ_FOREACH_MUTABLE(chandler
, &devfs_chandler_list
, link
, chandler2
) {
1511 if (chandler
->namlen
!= len
)
1513 if (memcmp(chandler
->name
, name
, len
))
1516 TAILQ_REMOVE(&devfs_chandler_list
, chandler
, link
);
1517 kfree(chandler
->name
, M_DEVFS
);
1518 kfree(chandler
, M_DEVFS
);
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.
1531 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg
)
1533 struct devfs_alias
*alias
;
1535 cdev_t found
= NULL
;
1537 TAILQ_FOREACH(dev
, &devfs_dev_list
, link
) {
1538 if (strcmp(devfs_msg
->mdv_name
, dev
->si_name
) == 0) {
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
;
1551 devfs_msg
->mdv_cdev
= found
;
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.
1562 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg
)
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
) {
1573 devfs_msg
->mdv_cdev
= found
;
1579 * Worker function that inserts a given alias into the
1580 * alias list, and propagates the alias to all mount
1584 devfs_make_alias_worker(struct devfs_alias
*alias
)
1586 struct devfs_alias
*alias2
;
1587 size_t len
= strlen(alias
->name
);
1590 TAILQ_FOREACH(alias2
, &devfs_alias_list
, link
) {
1591 if (len
!= alias2
->namlen
)
1594 if (!memcmp(alias
->name
, alias2
->name
, len
)) {
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);
1608 devfs_debug(DEVFS_DEBUG_WARNING
,
1609 "Warning: duplicate devfs_make_alias for %s\n",
1611 kfree(alias
->name
, M_DEVFS
);
1612 kfree(alias
, M_DEVFS
);
1619 * Worker function that delete a given alias from the
1620 * alias list, and propagates the removal to all mount
1624 devfs_destroy_alias_worker(struct devfs_alias
*alias
)
1626 struct devfs_alias
*alias2
;
1629 TAILQ_FOREACH(alias2
, &devfs_alias_list
, link
) {
1630 if (alias
->dev_target
!= alias2
->dev_target
)
1633 if (devfs_WildCmp(alias
->name
, alias2
->name
) == 0) {
1640 devfs_debug(DEVFS_DEBUG_WARNING
,
1641 "Warning: devfs_destroy_alias for inexistant alias: %s\n",
1643 kfree(alias
->name
, M_DEVFS
);
1644 kfree(alias
, M_DEVFS
);
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
);
1662 * Function that removes and frees all aliases.
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
);
1678 * Function that removes an alias matching a specific cdev and frees
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
);
1698 * This function propagates an alias addition or removal to
1702 devfs_alias_propagate(struct devfs_alias
*alias
, int remove
)
1704 struct devfs_mnt_data
*mnt
;
1706 TAILQ_FOREACH(mnt
, &devfs_mnt_list
, link
) {
1708 devfs_destroy_node(mnt
->root_node
, alias
->name
);
1710 devfs_alias_apply(mnt
->root_node
, alias
);
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.
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
);
1735 if (node
->d_dev
== alias
->dev_target
)
1736 devfs_alias_create(alias
->name
, node
, 0);
1742 * This function checks if any alias possibly is applicable
1743 * to the given node. If so, the alias is created.
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);
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
;
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
);
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",
1791 linknode
= devfs_allocp(Nlink
, name
, parent
, mp
, NULL
);
1792 if (linknode
== NULL
) {
1797 linknode
->link_target
= target
;
1801 linknode
->flags
|= DEVFS_RULE_CREATED
;
1804 kfree(name_buf
, M_TEMP
);
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
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
),
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
),
1836 kfree(mountto
, M_DEVFS
);
1841 * This function calls a given callback function for
1842 * every dev node in the devfs dev list.
1845 devfs_scan_callback_worker(devfs_scan_t
*callback
, void *arg
)
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
);
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
)
1874 if (!memcmp(dir_name
, node
->d_dir
.d_name
, name_len
)) {
1880 if ((found
== NULL
) && (create
)) {
1881 found
= devfs_allocp(Ndir
, dir_name
, parent
, parent
->mp
, NULL
);
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),
1893 devfs_resolve_or_create_path(struct devfs_node
*parent
, char *path
, int create
)
1895 struct devfs_node
*node
= parent
;
1902 buf
= kmalloc(PATH_MAX
, M_TEMP
, M_WAITOK
);
1904 while (*path
&& idx
< PATH_MAX
- 1) {
1909 node
= devfs_resolve_or_create_dir(node
, buf
, idx
, create
);
1919 node
= devfs_resolve_or_create_dir(node
, buf
, idx
, create
);
1920 kfree (buf
, M_TEMP
);
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
1931 devfs_resolve_name_path(char *fullpath
, char *buf
, char **pathp
, char **namep
)
1935 size_t len
= strlen(fullpath
) + 1;
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
] == '/') {
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.
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
;
1980 char *create_path
= NULL
;
1981 char *names
= "pqrsPQRS";
1983 name_buf
= kmalloc(PATH_MAX
, M_TEMP
, M_WAITOK
);
1988 if (path_fmt
!= NULL
) {
1989 __va_start(ap
, path_fmt
);
1990 kvasnrprintf(&path
, PATH_MAX
, 10, path_fmt
, ap
);
1994 parent
= devfs_resolve_or_create_path(root
, path
, 1);
1997 devfs_resolve_name_path(
1998 ((dev_name
== NULL
) && (dev
))?(dev
->si_name
):(dev_name
),
1999 name_buf
, &create_path
, &name
);
2002 parent
= devfs_resolve_or_create_path(parent
, create_path
, 1);
2005 node
= devfs_find_device_node_by_name(parent
, name
);
2007 if (node
->d_dev
== dev
) {
2009 * Allow case where device caches dev after the
2010 * close and might desire to reuse it.
2015 devfs_debug(DEVFS_DEBUG_WARNING
,
2016 "devfs_create_device_node: "
2017 "DEVICE %s ALREADY EXISTS!!! "
2018 "Ignoring creation request.\n",
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
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))) {
2046 for (i
= 0; i
< strlen(names
); i
++) {
2047 if (name
[3] == names
[i
]) {
2053 node
->flags
|= (DEVFS_PTY
| DEVFS_INVISIBLE
);
2057 kfree(name_buf
, M_TEMP
);
2063 * This function finds a given device node in the topology with a given
2067 devfs_find_device_node_callback(struct devfs_node
*node
, cdev_t target
)
2069 if ((node
->node_type
== Ndev
) && (node
->d_dev
== target
)) {
2077 * This function finds a device node in the given parent directory by its
2078 * name and returns it.
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
)
2090 if (!memcmp(node
->d_dir
.d_name
, target
, len
)) {
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
) {
2108 vget(vp
, LK_EXCLUSIVE
| LK_RETRY
);
2111 devfs_allocv(&vp
, node
);
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
;
2140 char *create_path
= NULL
;
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
);
2150 parent
= devfs_resolve_or_create_path(root
, create_path
, 0);
2154 if (parent
== NULL
) {
2155 kfree(name_buf
, M_TEMP
);
2159 node
= devfs_find_device_node_by_name(parent
, name
);
2162 nanotime(&node
->parent
->mtime
);
2166 kfree(name_buf
, M_TEMP
);
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
)
2186 * Propagates a device attach/detach to all mount
2187 * points. Also takes care of automatic alias removal
2188 * for a deleted cdev.
2191 devfs_propagate_dev(cdev_t dev
, int attach
)
2193 struct devfs_mnt_data
*mnt
;
2195 TAILQ_FOREACH(mnt
, &devfs_mnt_list
, link
) {
2197 /* Device is being attached */
2198 devfs_create_device_node(mnt
->root_node
, dev
,
2201 /* Device is being detached */
2202 devfs_alias_remove(dev
);
2203 devfs_destroy_device_node(mnt
->root_node
, dev
);
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.
2216 devfs_clone(cdev_t dev
, const char *name
, size_t len
, int mode
,
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
)
2226 if ((!memcmp(chandler
->name
, name
, len
)) && (chandler
->nhandler
)) {
2227 lockmgr(&devfs_lock
, LK_RELEASE
);
2229 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
2231 ap
.a_head
.a_dev
= dev
;
2237 error
= (chandler
->nhandler
)(&ap
);
2250 * Registers a new orphan in the orphan list.
2253 devfs_tracer_add_orphan(struct devfs_node
*node
)
2255 struct devfs_orphan
*orphan
;
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.
2270 devfs_tracer_del_orphan(struct devfs_node
*node
)
2272 struct devfs_orphan
*orphan
;
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
);
2287 * Counts the orphans in the orphan list, and if cleanup
2288 * is specified, also frees the orphan and removes it from
2292 devfs_tracer_orphan_count(struct mount
*mp
, int cleanup
)
2294 struct devfs_orphan
*orphan
, *orphan2
;
2297 TAILQ_FOREACH_MUTABLE(orphan
, DEVFS_ORPHANLIST(mp
), link
, orphan2
) {
2300 * If we are instructed to clean up, we do so.
2303 TAILQ_REMOVE(DEVFS_ORPHANLIST(mp
), orphan
, link
);
2304 orphan
->node
->flags
&= ~DEVFS_ORPHANED
;
2305 devfs_freep(orphan
->node
);
2306 kfree(orphan
, M_DEVFS
);
2314 * Fetch an ino_t from the global d_ino by increasing it
2318 devfs_fetch_ino(void)
2322 spin_lock(&ino_lock
);
2324 spin_unlock(&ino_lock
);
2330 * Allocates a new cdev and initializes it's most basic
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
);
2340 bzero(dev
, offsetof(struct cdev
, si_sysref
));
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
;
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
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
)),
2372 dev
->si_umajor
= umajor(dev
->si_inode
);
2378 devfs_cdev_terminate(cdev_t dev
)
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
);
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 */
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)
2413 devfs_cdev_lock(cdev_t dev
)
2418 devfs_cdev_unlock(cdev_t dev
)
2423 devfs_detached_filter_eof(struct knote
*kn
, long hint
)
2425 kn
->kn_flags
|= (EV_EOF
| EV_NODATA
);
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.
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
);
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.
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
;
2496 devfs_node_is_accessible(struct devfs_node
*node
)
2498 if ((node
) && (!(node
->flags
& DEVFS_HIDDEN
)))
2505 devfs_reference_ops(struct dev_ops
*ops
)
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
) {
2519 found
= kmalloc(sizeof(struct devfs_dev_ops
), M_DEVFS
, M_WAITOK
);
2521 found
->ref_count
= 0;
2522 TAILQ_INSERT_TAIL(&devfs_dev_ops_list
, found
, link
);
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");
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
) {
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.
2575 if (devfs_run
&& curthread
!= td_core
) {
2576 msg
= devfs_msg_get();
2577 devfs_msg_send_sync(DEVFS_SYNC
, msg
);
2583 * Called on init of devfs; creates the objcaches and
2584 * spawns off the devfs core thread. Also initializes
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,
2594 objcache_malloc_alloc
,
2595 objcache_malloc_free
,
2596 &devfs_node_malloc_args
);
2598 devfs_msg_cache
= objcache_create("devfs-msg-cache", 0, 0,
2600 objcache_malloc_alloc
,
2601 objcache_malloc_free
,
2602 &devfs_msg_malloc_args
);
2604 devfs_dev_cache
= objcache_create("devfs-dev-cache", 0, 0,
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.
2636 devfs_debug(DEVFS_DEBUG_DEBUG
, "devfs_uninit() called\n");
2638 devfs_msg_send(DEVFS_TERMINATE_CORE
, NULL
);
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
);
2654 * This is a sysctl handler to assist userland devname(3) to
2655 * find the device name for a given udev.
2658 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS
)
2665 if ((error
= SYSCTL_IN(req
, &udev
, sizeof(udev_t
))))
2668 devfs_debug(DEVFS_DEBUG_DEBUG
, "devfs sysctl, received udev: %d\n", udev
);
2673 if ((found
= devfs_find_device_by_udev(udev
)) == NULL
)
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
,
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.
2699 wildCmp(const char **mary
, int d
, const char *w
, const char *s
)
2704 * skip fixed portion
2710 * optimize terminator
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)
2726 for (i
= 0; s
+ i
< mary
[d
]; ++i
) {
2727 if (wildCmp(mary
, d
+ 1, w
+ 1, s
+ i
) == 0)
2742 if (*w
== 0) /* terminator */
2755 * WildCaseCmp() - compare wild string to sane string, case insensitive
2757 * Returns 0 on success, -1 on failure.
2760 wildCaseCmp(const char **mary
, int d
, const char *w
, const char *s
)
2765 * skip fixed portion
2771 * optimize terminator
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)
2787 for (i
= 0; s
+ i
< mary
[d
]; ++i
) {
2788 if (wildCaseCmp(mary
, d
+ 1, w
+ 1, s
+ i
) == 0)
2802 #define tolower(x) ((x >= 'A' && x <= 'Z')?(x+('a'-'A')):(x))
2803 if (tolower(*w
) != tolower(*s
))
2806 if (*w
== 0) /* terminator */
2817 struct cdev_privdata
{
2819 cdevpriv_dtr_t cdpd_dtr
;
2823 devfs_get_cdevpriv(struct file
*fp
, void **datap
)
2830 spin_lock_shared(&fp
->f_spin
);
2831 if (fp
->f_data1
== NULL
) {
2835 struct cdev_privdata
*p
= fp
->f_data1
;
2837 *datap
= p
->cdpd_data
;
2840 spin_unlock_shared(&fp
->f_spin
);
2846 devfs_set_cdevpriv(struct file
*fp
, void *priv
, cdevpriv_dtr_t dtr
)
2848 struct cdev_privdata
*p
;
2854 p
= kmalloc(sizeof(struct cdev_privdata
), M_DEVFS
, M_WAITOK
);
2855 p
->cdpd_data
= priv
;
2858 spin_lock(&fp
->f_spin
);
2859 if (fp
->f_data1
== NULL
) {
2865 spin_unlock(&fp
->f_spin
);
2874 devfs_clear_cdevpriv(struct file
*fp
)
2876 struct cdev_privdata
*p
;
2881 spin_lock(&fp
->f_spin
);
2884 spin_unlock(&fp
->f_spin
);
2887 p
->cdpd_dtr(p
->cdpd_data
);
2893 devfs_WildCmp(const char *w
, const char *s
)
2897 int slen
= strlen(s
);
2900 for (i
= c
= 0; w
[i
]; ++i
) {
2904 mary
= kmalloc(sizeof(char *) * (c
+ 1), M_DEVFS
, M_WAITOK
);
2905 for (i
= 0; i
< c
; ++i
)
2907 i
= wildCmp(mary
, 0, w
, s
);
2908 kfree(mary
, M_DEVFS
);
2913 devfs_WildCaseCmp(const char *w
, const char *s
)
2917 int slen
= strlen(s
);
2920 for (i
= c
= 0; w
[i
]; ++i
) {
2924 mary
= kmalloc(sizeof(char *) * (c
+ 1), M_DEVFS
, M_WAITOK
);
2925 for (i
= 0; i
< c
; ++i
)
2927 i
= wildCaseCmp(mary
, 0, w
, s
);
2928 kfree(mary
, M_DEVFS
);