xref: /dragonfly/sys/vfs/devfs/devfs_core.c (revision 655933d6)
1 /*
2  * Copyright (c) 2009-2019 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Alex Hornung <ahornung@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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.
20  *
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.
33  */
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>
49 
50 #include <sys/msgport2.h>
51 #include <sys/spinlock2.h>
52 #include <sys/sysref2.h>
53 
54 MALLOC_DEFINE(M_DEVFS, "devfs", "Device File System (devfs) allocations");
55 DEVFS_DEFINE_CLONE_BITMAP(ops_id);
56 /*
57  * SYSREF Integration - reference counting, allocation,
58  * sysid and syslink integration.
59  */
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
75 	}
76 };
77 
78 static struct objcache	*devfs_node_cache;
79 static struct objcache 	*devfs_msg_cache;
80 static struct objcache	*devfs_dev_cache;
81 
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 };
88 
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);
99 
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;
105 
106 static struct spinlock  ino_lock;
107 static ino_t 	d_ino;
108 static int	devfs_debug_enable;
109 static int	devfs_run;
110 
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);
122 
123 static int devfs_chandler_add_worker(const char *, d_clone_t *);
124 static int devfs_chandler_del_worker(const char *);
125 
126 static void devfs_msg_autofree_reply(lwkt_port_t, lwkt_msg_t);
127 static void devfs_msg_core(void *);
128 
129 static int devfs_find_device_by_name_worker(devfs_msg_t);
130 static int devfs_find_device_by_devid_worker(devfs_msg_t);
131 
132 static int devfs_apply_reset_rules_caller(char *, int);
133 
134 static int devfs_scan_callback_worker(devfs_scan_t *, void *);
135 
136 static struct devfs_node *devfs_resolve_or_create_dir(struct devfs_node *,
137 		char *, size_t, int);
138 
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 *);
146 
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);
149 
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 *);
156 
157 /*
158  * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function
159  * using kvprintf
160  */
161 int
162 devfs_debug(int level, char *fmt, ...)
163 {
164 	__va_list ap;
165 
166 	__va_start(ap, fmt);
167 	if (level <= devfs_debug_enable)
168 		kvprintf(fmt, ap);
169 	__va_end(ap);
170 
171 	return 0;
172 }
173 
174 /*
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
179  */
180 struct devfs_node *
181 devfs_allocp(devfs_nodetype devfsnodetype, char *name,
182 	     struct devfs_node *parent, struct mount *mp, cdev_t dev)
183 {
184 	struct devfs_node *node = NULL;
185 	size_t namlen = strlen(name);
186 
187 	node = objcache_get(devfs_node_cache, M_WAITOK);
188 	bzero(node, sizeof(*node));
189 
190 	atomic_add_long(&DEVFS_MNTDATA(mp)->leak_count, 1);
191 
192 	node->d_dev = NULL;
193 	node->nchildren = 1;
194 	node->mp = mp;
195 	node->d_dir.d_ino = devfs_fetch_ino();
196 
197 	/*
198 	 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries
199 	 * respectively.
200 	 */
201 	node->cookie_jar = 2;
202 
203 	/*
204 	 * Access Control members
205 	 */
206 	node->mode = DEVFS_DEFAULT_MODE;
207 	node->uid = DEVFS_DEFAULT_UID;
208 	node->gid = DEVFS_DEFAULT_GID;
209 
210 	switch (devfsnodetype) {
211 	case Nroot:
212 		/*
213 		 * Ensure that we don't recycle the root vnode by marking it as
214 		 * linked into the topology.
215 		 */
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;
222 
223 	case Nlink:
224 		node->d_dir.d_type = DT_LNK;
225 		break;
226 
227 	case Nreg:
228 		node->d_dir.d_type = DT_REG;
229 		break;
230 
231 	case Ndev:
232 		if (dev != NULL) {
233 			node->d_dir.d_type = DT_CHR;
234 			node->d_dev = dev;
235 
236 			node->mode = dev->si_perms;
237 			node->uid = dev->si_uid;
238 			node->gid = dev->si_gid;
239 
240 			devfs_alias_check_create(node);
241 		}
242 		break;
243 
244 	default:
245 		panic("devfs_allocp: unknown node type");
246 	}
247 
248 	node->v_node = NULL;
249 	node->node_type = devfsnodetype;
250 
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';
256 
257 	/* Initialize the parent node element */
258 	node->parent = parent;
259 
260 	/* Initialize *time members */
261 	vfs_timestamp(&node->atime);
262 	node->mtime = node->ctime = node->atime;
263 
264 	/*
265 	 * Associate with parent as last step, clean out namecache
266 	 * reference.
267 	 */
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);
275 
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);
284 		}
285 	}
286 
287 	/*
288 	 * Apply rules (requires root node, skip if we are creating the root
289 	 * node)
290 	 */
291 	if (DEVFS_MNTDATA(mp)->root_node)
292 		devfs_rule_check_apply(node, NULL);
293 
294 	atomic_add_long(&DEVFS_MNTDATA(mp)->file_count, 1);
295 
296 	return node;
297 }
298 
299 /*
300  * devfs_allocv() allocates a new vnode based on a devfs node.
301  */
302 int
303 devfs_allocv(struct vnode **vpp, struct devfs_node *node)
304 {
305 	struct vnode *vp;
306 	int error = 0;
307 
308 	KKASSERT(node);
309 
310 	/*
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.
315 	 */
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;
326 		}
327 		if (error != ENOENT) {
328 			*vpp = NULL;
329 			goto out;
330 		}
331 	}
332 
333 	/*
334 	 * devfs master lock must not be held across a getnewvnode() call.
335 	 */
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;
340 	}
341 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
342 
343 	vp = *vpp;
344 
345 	if (node->v_node != NULL) {
346 		vp->v_type = VBAD;
347 		vx_put(vp);
348 		goto try_again;
349 	}
350 
351 	vp->v_data = node;
352 	node->v_node = vp;
353 
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;
361 
362 	case Nlink:
363 		vp->v_type = VLNK;
364 		break;
365 
366 	case Nreg:
367 		vp->v_type = VREG;
368 		break;
369 
370 	case Ndev:
371 		vp->v_type = VCHR;
372 		KKASSERT(node->d_dev);
373 
374 		vp->v_uminor = node->d_dev->si_uminor;
375 		vp->v_umajor = node->d_dev->si_umajor;
376 
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;
382 
383 	default:
384 		panic("devfs_allocv: unknown node type");
385 	}
386 	vx_downgrade(vp);	/* downgrade VX lock to VN lock */
387 
388 out:
389 	return error;
390 }
391 
392 /*
393  * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
394  * based on the newly created devfs node.
395  */
396 int
397 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype,
398 		char *name, struct devfs_node *parent, cdev_t dev)
399 {
400 	struct devfs_node *node;
401 
402 	node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
403 
404 	if (node != NULL)
405 		devfs_allocv(vpp, node);
406 	else
407 		*vpp = NULL;
408 
409 	return 0;
410 }
411 
412 /*
413  * Destroy the devfs_node.  The node must be unlinked from the topology.
414  *
415  * This function will also destroy any vnode association with the node
416  * and device.
417  *
418  * The cdev_t itself remains intact.
419  *
420  * The core lock is not necessarily held on call and must be temporarily
421  * released if it is to avoid a deadlock.
422  */
423 void
424 devfs_freep(struct devfs_node *node)
425 {
426 	struct vnode *vp;
427 	int maxloops;
428 
429 	KKASSERT(node);
430 
431 	/*
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)
435 	 *
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.
441 	 */
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;
450 			}
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);
455 		}
456 		return;
457 	}
458 	node->flags |= DEVFS_DESTROYED;
459 
460 	/*
461 	 * Items we have to dispose of before potentially releasing
462 	 * devfs_lock.
463 	 *
464 	 * Remove the node from the orphan list if it is still on it.
465 	 */
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);
470 
471 	/*
472 	 * At this point only the vp points to node, and node cannot be
473 	 * physically freed because we own DEVFS_DESTROYED.
474 	 *
475 	 * We must dispose of the vnode without deadlocking or racing
476 	 * against e.g. a vnode reclaim.
477 	 *
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.
481 	 */
482 	maxloops = 1000;
483 	while ((vp = node->v_node) != NULL) {
484 		int relock;
485 
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;
492 		}
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);
505 		}
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;
511 		}
512 	}
513 
514 	/*
515 	 * Remaining cleanup
516 	 */
517 	if (node->symlink_name)	{
518 		kfree(node->symlink_name, M_DEVFS);
519 		node->symlink_name = NULL;
520 	}
521 
522 	/*
523 	 * We cannot actually free the node if it still has
524 	 * nlinks.
525 	 */
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;
532 		}
533 		objcache_put(devfs_node_cache, node);
534 	}
535 }
536 
537 /*
538  * Returns a valid vp associated with the devfs alias node or NULL
539  */
540 static void *devfs_alias_getvp(struct devfs_node *node)
541 {
542 	struct devfs_node *found = node;
543 	int depth = 0;
544 
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;
549 		}
550 
551 		found = found->link_target;
552 		++depth;
553 	}
554 
555 	return found->v_node;
556 }
557 
558 /*
559  * Unlink the devfs node from the topology and add it to the orphan list.
560  * The node will later be destroyed by freep.
561  *
562  * Any vnode association, including the v_rdev and v_data, remains intact
563  * until the freep.
564  */
565 void
566 devfs_unlinkp(struct devfs_node *node)
567 {
568 	struct devfs_node *parent;
569 	struct devfs_node *target;
570 	struct vnode *vp;
571 	KKASSERT(node);
572 
573 	/*
574 	 * Add the node to the orphan list, so it is referenced somewhere, to
575 	 * so we don't leak it.
576 	 */
577 	devfs_tracer_add_orphan(node);
578 
579 	parent = node->parent;
580 	node->parent = NULL;
581 
582 	/*
583 	 * If the parent is known we can unlink the node out of the topology
584 	 */
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;
591 		}
592 		node->flags &= ~DEVFS_NODE_LINKED;
593 	}
594 
595 	/*
596 	 * Namecache invalidation.
597 	 *
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.
602 	 *
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.
606 	 *
607 	 * WARNING! We do not disassociate the vnode here.  That can only
608 	 *	    be safely done in devfs_freep().
609 	 */
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);
618 			}
619 		} else {
620 			vp = NULL;
621 		}
622 	} else {
623 		vp = node->v_node;
624 	}
625 
626 	if (vp != NULL)
627 		cache_inval_vp(vp, CINV_DESTROY);
628 }
629 
630 void *
631 devfs_iterate_topology(struct devfs_node *node,
632 		devfs_iterate_callback_t *callback, void *arg1)
633 {
634 	struct devfs_node *node1, *node2;
635 	void *ret = NULL;
636 
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;
644 		}
645 	}
646 	ret = callback(node, arg1);
647 
648 	return ret;
649 }
650 
651 static void *
652 devfs_alias_reaper_callback(struct devfs_node *node, void *unused)
653 {
654 	if (node->node_type == Nlink) {
655 		devfs_unlinkp(node);
656 		devfs_freep(node);
657 	}
658 
659 	return NULL;
660 }
661 
662 /*
663  * devfs_reaperp() is a recursive function that iterates through all the
664  * topology, unlinking and freeing all devfs nodes.
665  */
666 static void *
667 devfs_reaperp_callback(struct devfs_node *node, void *unused)
668 {
669 	devfs_unlinkp(node);
670 	devfs_freep(node);
671 
672 	return NULL;
673 }
674 
675 /*
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.
678  */
679 static void
680 devfs_iterate_orphans_unmount(struct mount *mp)
681 {
682 	struct devfs_orphan *orphan;
683 
684 	while ((orphan = TAILQ_FIRST(DEVFS_ORPHANLIST(mp))) != NULL) {
685 		devfs_freep(orphan->node);
686 		/* orphan stale */
687 	}
688 }
689 
690 static void *
691 devfs_gc_dirs_callback(struct devfs_node *node, void *unused)
692 {
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);
698 		}
699 	}
700 
701 	return NULL;
702 }
703 
704 static void *
705 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target)
706 {
707 	if ((node->node_type == Nlink) && (node->link_target == target)) {
708 		devfs_unlinkp(node);
709 		devfs_freep(node);
710 	}
711 
712 	return NULL;
713 }
714 
715 /*
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.
719  */
720 int
721 devfs_gc(struct devfs_node *node)
722 {
723 	struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
724 
725 	if (node->nlinks > 0)
726 		devfs_iterate_topology(root_node,
727 				(devfs_iterate_callback_t *)devfs_gc_links_callback, node);
728 
729 	devfs_unlinkp(node);
730 	devfs_iterate_topology(root_node,
731 			(devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL);
732 
733 	devfs_freep(node);
734 
735 	return 0;
736 }
737 
738 /*
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.
741  *
742  * This function will reference the passed device.  The reference is owned
743  * by devfs and represents all of the device's node associations.
744  */
745 int
746 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
747 {
748 	reference_dev(dev);
749 	devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
750 
751 	return 0;
752 }
753 
754 /*
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.
757  */
758 int
759 devfs_destroy_dev(cdev_t dev)
760 {
761 	devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
762 	return 0;
763 }
764 
765 /*
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.
769  */
770 int
771 devfs_mount_add(struct devfs_mnt_data *mnt)
772 {
773 	devfs_msg_t msg;
774 
775 	msg = devfs_msg_get();
776 	msg->mdv_mnt = mnt;
777 	devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
778 	devfs_msg_put(msg);
779 
780 	return 0;
781 }
782 
783 /*
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.
786  */
787 int
788 devfs_mount_del(struct devfs_mnt_data *mnt)
789 {
790 	devfs_msg_t msg;
791 
792 	msg = devfs_msg_get();
793 	msg->mdv_mnt = mnt;
794 	devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
795 	devfs_msg_put(msg);
796 
797 	return 0;
798 }
799 
800 /*
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.
804  */
805 int
806 devfs_destroy_related(cdev_t dev)
807 {
808 	devfs_msg_t msg;
809 
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;
815 }
816 
817 int
818 devfs_clr_related_flag(cdev_t dev, uint32_t flag)
819 {
820 	devfs_msg_t msg;
821 
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);
827 
828 	return 0;
829 }
830 
831 int
832 devfs_destroy_related_without_flag(cdev_t dev, uint32_t flag)
833 {
834 	devfs_msg_t msg;
835 
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);
841 
842 	return 0;
843 }
844 
845 /*
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.
849  */
850 int
851 devfs_create_all_dev(struct devfs_node *root)
852 {
853 	devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
854 	return 0;
855 }
856 
857 /*
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.
861  */
862 int
863 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
864 {
865 	devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
866 	return 0;
867 }
868 
869 /*
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.
873  */
874 int
875 devfs_clone_handler_add(const char *name, d_clone_t *nhandler)
876 {
877 	devfs_msg_t msg;
878 
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;
885 }
886 
887 /*
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.
891  */
892 int
893 devfs_clone_handler_del(const char *name)
894 {
895 	devfs_msg_t msg;
896 
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;
903 }
904 
905 /*
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.
909  */
910 cdev_t
911 devfs_find_device_by_name(const char *fmt, ...)
912 {
913 	cdev_t found = NULL;
914 	devfs_msg_t msg;
915 	char *target;
916 	__va_list ap;
917 
918 	if (fmt == NULL)
919 		return NULL;
920 
921 	__va_start(ap, fmt);
922 	kvasnprintf(&target, PATH_MAX, fmt, ap);
923 	__va_end(ap);
924 
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);
931 
932 	return found;
933 }
934 
935 /*
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.
939  */
940 cdev_t
941 devfs_find_device_by_devid(dev_t udev)
942 {
943 	cdev_t found = NULL;
944 	devfs_msg_t msg;
945 
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);
951 
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;
956 }
957 
958 struct vnode *
959 devfs_inode_to_vnode(struct mount *mp, ino_t target)
960 {
961 	struct vnode *vp = NULL;
962 	devfs_msg_t msg;
963 
964 	if (mp == NULL)
965 		return NULL;
966 
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);
974 
975 	return vp;
976 }
977 
978 /*
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.
982  */
983 int
984 devfs_make_alias(const char *name, cdev_t dev_target)
985 {
986 	struct devfs_alias *alias;
987 	size_t len;
988 
989 	len = strlen(name);
990 
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;
995 
996 	devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
997 	return 0;
998 }
999 
1000 /*
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.
1004  */
1005 int
1006 devfs_destroy_alias(const char *name, cdev_t dev_target)
1007 {
1008 	struct devfs_alias *alias;
1009 	size_t len;
1010 
1011 	len = strlen(name);
1012 
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;
1017 
1018 	devfs_msg_send_generic(DEVFS_DESTROY_ALIAS, alias);
1019 	return 0;
1020 }
1021 
1022 /*
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.
1026  */
1027 int
1028 devfs_apply_rules(char *mntto)
1029 {
1030 	char *new_name;
1031 
1032 	new_name = kstrdup(mntto, M_DEVFS);
1033 	devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
1034 
1035 	return 0;
1036 }
1037 
1038 /*
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.
1041  */
1042 int
1043 devfs_reset_rules(char *mntto)
1044 {
1045 	char *new_name;
1046 
1047 	new_name = kstrdup(mntto, M_DEVFS);
1048 	devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
1049 
1050 	return 0;
1051 }
1052 
1053 
1054 /*
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.
1058  */
1059 int
1060 devfs_scan_callback(devfs_scan_t *callback, void *arg)
1061 {
1062 	devfs_msg_t msg;
1063 
1064 	KKASSERT(callback);
1065 
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);
1071 
1072 	return 0;
1073 }
1074 
1075 
1076 /*
1077  * Acts as a message drain. Any message that is replied to here gets destroyed
1078  * and the memory freed.
1079  */
1080 static void
1081 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
1082 {
1083 	devfs_msg_put((devfs_msg_t)msg);
1084 }
1085 
1086 /*
1087  * devfs_msg_get allocates a new devfs msg and returns it.
1088  */
1089 devfs_msg_t
1090 devfs_msg_get(void)
1091 {
1092 	return objcache_get(devfs_msg_cache, M_WAITOK);
1093 }
1094 
1095 /*
1096  * devfs_msg_put deallocates a given devfs msg.
1097  */
1098 int
1099 devfs_msg_put(devfs_msg_t msg)
1100 {
1101 	objcache_put(devfs_msg_cache, msg);
1102 	return 0;
1103 }
1104 
1105 /*
1106  * devfs_msg_send is the generic asynchronous message sending facility
1107  * for devfs. By default the reply port is the automatic disposal port.
1108  *
1109  * If the current thread is the devfs_msg_port thread we execute the
1110  * operation synchronously.
1111  */
1112 void
1113 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
1114 {
1115 	lwkt_port_t port = &devfs_msg_port;
1116 
1117 	lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
1118 
1119 	devfs_msg->hdr.u.ms_result = cmd;
1120 
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);
1126 	}
1127 }
1128 
1129 /*
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.
1135  */
1136 int
1137 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
1138 {
1139 	struct lwkt_port rep_port;
1140 	int	error;
1141 	lwkt_port_t port = &devfs_msg_port;
1142 
1143 	lwkt_initport_thread(&rep_port, curthread);
1144 	lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
1145 
1146 	devfs_msg->hdr.u.ms_result = cmd;
1147 
1148 	error = lwkt_domsg(port, (lwkt_msg_t)devfs_msg, 0);
1149 
1150 	return error;
1151 }
1152 
1153 /*
1154  * sends a message with a generic argument.
1155  */
1156 void
1157 devfs_msg_send_generic(uint32_t cmd, void *load)
1158 {
1159 	devfs_msg_t devfs_msg = devfs_msg_get();
1160 
1161 	devfs_msg->mdv_load = load;
1162 	devfs_msg_send(cmd, devfs_msg);
1163 }
1164 
1165 /*
1166  * sends a message with a name argument.
1167  */
1168 void
1169 devfs_msg_send_name(uint32_t cmd, char *name)
1170 {
1171 	devfs_msg_t devfs_msg = devfs_msg_get();
1172 
1173 	devfs_msg->mdv_name = name;
1174 	devfs_msg_send(cmd, devfs_msg);
1175 }
1176 
1177 /*
1178  * sends a message with a mount argument.
1179  */
1180 void
1181 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
1182 {
1183 	devfs_msg_t devfs_msg = devfs_msg_get();
1184 
1185 	devfs_msg->mdv_mnt = mnt;
1186 	devfs_msg_send(cmd, devfs_msg);
1187 }
1188 
1189 /*
1190  * sends a message with an ops argument.
1191  */
1192 void
1193 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
1194 {
1195 	devfs_msg_t devfs_msg = devfs_msg_get();
1196 
1197 	devfs_msg->mdv_ops.ops = ops;
1198 	devfs_msg->mdv_ops.minor = minor;
1199 	devfs_msg_send(cmd, devfs_msg);
1200 }
1201 
1202 /*
1203  * sends a message with a clone handler argument.
1204  */
1205 void
1206 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
1207 {
1208 	devfs_msg_t devfs_msg = devfs_msg_get();
1209 
1210 	devfs_msg->mdv_chandler.name = name;
1211 	devfs_msg->mdv_chandler.nhandler = handler;
1212 	devfs_msg_send(cmd, devfs_msg);
1213 }
1214 
1215 /*
1216  * sends a message with a device argument.
1217  */
1218 void
1219 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1220 {
1221 	devfs_msg_t devfs_msg = devfs_msg_get();
1222 
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;
1227 
1228 	devfs_msg_send(cmd, devfs_msg);
1229 }
1230 
1231 /*
1232  * sends a message with a link argument.
1233  */
1234 void
1235 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1236 {
1237 	devfs_msg_t devfs_msg = devfs_msg_get();
1238 
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);
1243 }
1244 
1245 /*
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.
1249  */
1250 static void
1251 devfs_msg_core(void *arg)
1252 {
1253 	devfs_msg_t msg;
1254 
1255 	lwkt_initport_thread(&devfs_msg_port, curthread);
1256 
1257 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
1258 	devfs_run = 1;
1259 	wakeup(td_core);
1260 	lockmgr(&devfs_lock, LK_RELEASE);
1261 
1262 	lwkt_gettoken(&devfs_token);
1263 
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);
1271 	}
1272 
1273 	lwkt_reltoken(&devfs_token);
1274 	wakeup(td_core);
1275 
1276 	lwkt_exit();
1277 }
1278 
1279 static void
1280 devfs_msg_exec(devfs_msg_t msg)
1281 {
1282 	struct devfs_mnt_data *mnt;
1283 	struct devfs_node *node;
1284 	cdev_t	dev;
1285 
1286 	/*
1287 	 * Acquire the devfs lock to ensure safety of all called functions
1288 	 */
1289 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
1290 
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);
1334 		}
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;
1389 	}
1390 	lockmgr(&devfs_lock, LK_RELEASE);
1391 }
1392 
1393 static void
1394 devfs_devctl_notify(cdev_t dev, const char *ev)
1395 {
1396 	static const char prefix[] = "cdev=";
1397 	char *data;
1398 	int namelen;
1399 
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);
1406 }
1407 
1408 /*
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.
1412  *
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.
1415  */
1416 static int
1417 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1418 {
1419 	KKASSERT(dev);
1420 
1421 	dev->si_uid = uid;
1422 	dev->si_gid = gid;
1423 	dev->si_perms = perms;
1424 
1425 	devfs_link_dev(dev);
1426 	devfs_propagate_dev(dev, 1);
1427 
1428 	udev_event_attach(dev, NULL, 0);
1429 	devfs_devctl_notify(dev, "CREATE");
1430 
1431 	return 0;
1432 }
1433 
1434 /*
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.
1438  */
1439 static int
1440 devfs_destroy_dev_worker(cdev_t dev)
1441 {
1442 	int error;
1443 
1444 	KKASSERT(dev);
1445 	KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1446 
1447 	error = devfs_unlink_dev(dev);
1448 	devfs_propagate_dev(dev, 0);
1449 
1450 	devfs_devctl_notify(dev, "DESTROY");
1451 	udev_event_detach(dev, NULL, 0);
1452 
1453 	if (error == 0)
1454 		release_dev(dev);	/* link ref */
1455 	release_dev(dev);
1456 	release_dev(dev);
1457 
1458 	return 0;
1459 }
1460 
1461 /*
1462  * Worker function to destroy all devices with a certain basename.
1463  * Calls devfs_destroy_dev_worker for the actual destruction.
1464  */
1465 static int
1466 devfs_destroy_related_worker(cdev_t needle)
1467 {
1468 	cdev_t dev;
1469 
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;
1478 		}
1479 	}
1480 	return 0;
1481 }
1482 
1483 static int
1484 devfs_clr_related_flag_worker(cdev_t needle, uint32_t flag)
1485 {
1486 	cdev_t dev, dev1;
1487 
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;
1492 		}
1493 	}
1494 
1495 	return 0;
1496 }
1497 
1498 static int
1499 devfs_destroy_related_without_flag_worker(cdev_t needle, uint32_t flag)
1500 {
1501 	cdev_t dev;
1502 
1503 restart:
1504 	devfs_debug(DEVFS_DEBUG_DEBUG, "related_wo_flag: %s\n",
1505 	    needle->si_name);
1506 
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;
1515 			}
1516 		}
1517 	}
1518 
1519 	return 0;
1520 }
1521 
1522 /*
1523  * Worker function that creates all device nodes on top of a devfs
1524  * root node.
1525  */
1526 static int
1527 devfs_create_all_dev_worker(struct devfs_node *root)
1528 {
1529 	cdev_t dev;
1530 
1531 	KKASSERT(root);
1532 
1533 	TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1534 		devfs_create_device_node(root, dev, NULL, NULL, NULL);
1535 	}
1536 
1537 	return 0;
1538 }
1539 
1540 /*
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.
1544  */
1545 static int
1546 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1547 {
1548 	cdev_t dev, dev1;
1549 
1550 	KKASSERT(ops);
1551 
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);
1557 		}
1558 	}
1559 
1560 	return 0;
1561 }
1562 
1563 /*
1564  * Worker function that registers a new clone handler in devfs.
1565  */
1566 static int
1567 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler)
1568 {
1569 	struct devfs_clone_handler *chandler = NULL;
1570 	u_char len = strlen(name);
1571 
1572 	if (len == 0)
1573 		return 1;
1574 
1575 	TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1576 		if (chandler->namlen != len)
1577 			continue;
1578 
1579 		if (!memcmp(chandler->name, name, len)) {
1580 			/* Clonable basename already exists */
1581 			return 1;
1582 		}
1583 	}
1584 
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;
1589 
1590 	TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1591 	return 0;
1592 }
1593 
1594 /*
1595  * Worker function that removes a given clone handler from the
1596  * clone handler list.
1597  */
1598 static int
1599 devfs_chandler_del_worker(const char *name)
1600 {
1601 	struct devfs_clone_handler *chandler, *chandler2;
1602 	u_char len = strlen(name);
1603 
1604 	if (len == 0)
1605 		return 1;
1606 
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;
1612 
1613 		TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1614 		kfree(chandler->name, M_DEVFS);
1615 		kfree(chandler, M_DEVFS);
1616 		break;
1617 	}
1618 
1619 	return 0;
1620 }
1621 
1622 /*
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.
1626  */
1627 static int
1628 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1629 {
1630 	struct devfs_alias *alias;
1631 	cdev_t dev;
1632 	cdev_t found = NULL;
1633 
1634 	TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1635 		if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1636 			found = dev;
1637 			break;
1638 		}
1639 	}
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;
1645 			}
1646 		}
1647 	}
1648 	devfs_msg->mdv_cdev = found;
1649 
1650 	return 0;
1651 }
1652 
1653 /*
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.
1657  */
1658 static int
1659 devfs_find_device_by_devid_worker(devfs_msg_t devfs_msg)
1660 {
1661 	cdev_t dev, dev1;
1662 	cdev_t found = NULL;
1663 
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;
1668 		}
1669 	}
1670 	devfs_msg->mdv_cdev = found;
1671 
1672 	return 0;
1673 }
1674 
1675 /*
1676  * Worker function that inserts a given alias into the
1677  * alias list, and propagates the alias to all mount
1678  * points.
1679  */
1680 static int
1681 devfs_make_alias_worker(struct devfs_alias *alias)
1682 {
1683 	struct devfs_alias *alias2;
1684 	size_t len = strlen(alias->name);
1685 	int found = 0;
1686 
1687 	TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1688 		if (len != alias2->namlen)
1689 			continue;
1690 
1691 		if (!memcmp(alias->name, alias2->name, len)) {
1692 			found = 1;
1693 			break;
1694 		}
1695 	}
1696 
1697 	if (!found) {
1698 		/*
1699 		 * The alias doesn't exist yet, so we add it to the alias list
1700 		 */
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);
1710 	}
1711 
1712 	return 0;
1713 }
1714 
1715 /*
1716  * Worker function that delete a given alias from the
1717  * alias list, and propagates the removal to all mount
1718  * points.
1719  */
1720 static int
1721 devfs_destroy_alias_worker(struct devfs_alias *alias)
1722 {
1723 	struct devfs_alias *alias2;
1724 	int found = 0;
1725 
1726 	TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1727 		if (alias->dev_target != alias2->dev_target)
1728 			continue;
1729 
1730 		if (devfs_WildCmp(alias->name, alias2->name) == 0) {
1731 			found = 1;
1732 			break;
1733 		}
1734 	}
1735 
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 {
1743 		/*
1744 		 * The alias exists, so we delete it from the alias list
1745 		 */
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);
1753 	}
1754 
1755 	return 0;
1756 }
1757 
1758 /*
1759  * Function that removes and frees all aliases.
1760  */
1761 static int
1762 devfs_alias_reap(void)
1763 {
1764 	struct devfs_alias *alias, *alias2;
1765 
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);
1770 	}
1771 	return 0;
1772 }
1773 
1774 /*
1775  * Function that removes an alias matching a specific cdev and frees
1776  * it accordingly.
1777  */
1778 static int
1779 devfs_alias_remove(cdev_t dev)
1780 {
1781 	struct devfs_alias *alias, *alias2;
1782 
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);
1789 		}
1790 	}
1791 	return 0;
1792 }
1793 
1794 /*
1795  * This function propagates an alias addition or removal to
1796  * all mount points.
1797  */
1798 static int
1799 devfs_alias_propagate(struct devfs_alias *alias, int remove)
1800 {
1801 	struct devfs_mnt_data *mnt;
1802 
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);
1808 		}
1809 	}
1810 	return 0;
1811 }
1812 
1813 /*
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.
1817  */
1818 static int
1819 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1820 {
1821 	struct devfs_node *node1, *node2;
1822 
1823 	KKASSERT(alias != NULL);
1824 
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);
1829 			}
1830 		}
1831 	} else {
1832 		if (node->d_dev == alias->dev_target)
1833 			devfs_alias_create(alias->name, node, 0);
1834 	}
1835 	return 0;
1836 }
1837 
1838 /*
1839  * This function checks if any alias possibly is applicable
1840  * to the given node. If so, the alias is created.
1841  */
1842 static int
1843 devfs_alias_check_create(struct devfs_node *node)
1844 {
1845 	struct devfs_alias *alias;
1846 
1847 	TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1848 		if (node->d_dev == alias->dev_target)
1849 			devfs_alias_create(alias->name, node, 0);
1850 	}
1851 	return 0;
1852 }
1853 
1854 /*
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.
1858  */
1859 int
1860 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based)
1861 {
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;
1869 
1870 	KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1871 
1872 	name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1873 	devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1874 
1875 	if (create_path)
1876 		parent = devfs_resolve_or_create_path(parent, create_path, 1);
1877 
1878 
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;
1886 	}
1887 
1888 	linknode = devfs_allocp(Nlink, name, parent, mp, NULL);
1889 	if (linknode == NULL) {
1890 		result = 1;
1891 		goto done;
1892 	}
1893 
1894 	linknode->link_target = target;
1895 	target->nlinks++;
1896 
1897 	if (rule_based)
1898 		linknode->flags |= DEVFS_RULE_CREATED;
1899 
1900 done:
1901 	kfree(name_buf, M_TEMP);
1902 	return (result);
1903 }
1904 
1905 /*
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.
1910  */
1911 static int
1912 devfs_apply_reset_rules_caller(char *mountto, int apply)
1913 {
1914 	struct devfs_mnt_data *mnt;
1915 
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);
1921 		}
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;
1929 			}
1930 		}
1931 	}
1932 
1933 	kfree(mountto, M_DEVFS);
1934 	return 0;
1935 }
1936 
1937 /*
1938  * This function calls a given callback function for
1939  * every dev node in the devfs dev list.
1940  */
1941 static int
1942 devfs_scan_callback_worker(devfs_scan_t *callback, void *arg)
1943 {
1944 	cdev_t dev, dev1;
1945 	struct devfs_alias *alias, *alias1;
1946 
1947 	TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1948 		callback(dev->si_name, dev, false, arg);
1949 	}
1950 	TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias1) {
1951 		callback(alias->name, alias->dev_target, true, arg);
1952 	}
1953 
1954 	return 0;
1955 }
1956 
1957 /*
1958  * This function tries to resolve a given directory, or if not
1959  * found and creation requested, creates the given directory.
1960  */
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)
1964 {
1965 	struct devfs_node *node, *found = NULL;
1966 
1967 	TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1968 		if (name_len != node->d_dir.d_namlen)
1969 			continue;
1970 
1971 		if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1972 			found = node;
1973 			break;
1974 		}
1975 	}
1976 
1977 	if ((found == NULL) && (create)) {
1978 		found = devfs_allocp(Ndir, dir_name, parent, parent->mp, NULL);
1979 	}
1980 
1981 	return found;
1982 }
1983 
1984 /*
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.
1988  */
1989 struct devfs_node *
1990 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1991 {
1992 	struct devfs_node *node = parent;
1993 	char *buf;
1994 	size_t idx = 0;
1995 
1996 	if (path == NULL)
1997 		return parent;
1998 
1999 	buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
2000 
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;
2010 			}
2011 			idx = 0;
2012 		}
2013 		++path;
2014 	}
2015 	buf[idx] = '\0';
2016 	node = devfs_resolve_or_create_dir(node, buf, idx, create);
2017 	kfree (buf, M_TEMP);
2018 	return (node);
2019 }
2020 
2021 /*
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.
2026  */
2027 int
2028 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
2029 {
2030 	char *name = NULL;
2031 	char *path = NULL;
2032 	size_t len = strlen(fullpath) + 1;
2033 	int i;
2034 
2035 	KKASSERT((fullpath != NULL) && (buf != NULL));
2036 	KKASSERT((pathp != NULL) && (namep != NULL));
2037 
2038 	memcpy(buf, fullpath, len);
2039 
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;
2046 		}
2047 	}
2048 
2049 	*pathp = path;
2050 
2051 	if (name) {
2052 		*namep = name;
2053 	} else {
2054 		*namep = buf;
2055 	}
2056 
2057 	return 0;
2058 }
2059 
2060 /*
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.
2064  *
2065  * The reference count on the passed dev remains unchanged.
2066  */
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, ...)
2070 {
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";
2079 
2080 	name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
2081 
2082 	if (existsp)
2083 		*existsp = 0;
2084 
2085 	if (path_fmt != NULL) {
2086 		__va_start(ap, path_fmt);
2087 		kvasnprintf(&path, PATH_MAX, path_fmt, ap);
2088 		__va_end(ap);
2089 	}
2090 
2091 	parent = devfs_resolve_or_create_path(root, path, 1);
2092 	KKASSERT(parent);
2093 
2094 	devfs_resolve_name_path(
2095 			((dev_name == NULL) && (dev))?(dev->si_name):(dev_name),
2096 			name_buf, &create_path, &name);
2097 
2098 	if (create_path)
2099 		parent = devfs_resolve_or_create_path(parent, create_path, 1);
2100 
2101 
2102 	node = devfs_find_device_node_by_name(parent, name);
2103 	if (node) {
2104 		if (node->d_dev == dev) {
2105 			/*
2106 			 * Allow case where device caches dev after the
2107 			 * close and might desire to reuse it.
2108 			 */
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;
2118 		}
2119 		goto out;
2120 	}
2121 
2122 	node = devfs_allocp(Ndev, name, parent, parent->mp, dev);
2123 	vfs_timestamp(&parent->mtime);
2124 
2125 	/*
2126 	 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
2127 	 * directory
2128 	 */
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;
2133 	}
2134 
2135 	/*
2136 	 * Ugly pty magic, to tag pty devices as such and hide them if needed.
2137 	 */
2138 	if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
2139 		node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
2140 
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;
2147 			}
2148 		}
2149 		if (found)
2150 			node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
2151 	}
2152 
2153 out:
2154 	kfree(name_buf, M_TEMP);
2155 	kvasfree(&path);
2156 	return node;
2157 }
2158 
2159 /*
2160  * This function finds a given device node in the topology with a given
2161  * cdev.
2162  */
2163 void *
2164 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target)
2165 {
2166 	if ((node->node_type == Ndev) && (node->d_dev == target)) {
2167 		return node;
2168 	}
2169 
2170 	return NULL;
2171 }
2172 
2173 /*
2174  * This function finds a device node in the given parent directory by its
2175  * name and returns it.
2176  */
2177 struct devfs_node *
2178 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
2179 {
2180 	struct devfs_node *node, *found = NULL;
2181 	size_t len = strlen(target);
2182 
2183 	TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
2184 		if (len != node->d_dir.d_namlen)
2185 			continue;
2186 
2187 		if (!memcmp(node->d_dir.d_name, target, len)) {
2188 			found = node;
2189 			break;
2190 		}
2191 	}
2192 
2193 	return found;
2194 }
2195 
2196 static void *
2197 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop)
2198 {
2199 	struct vnode *vp = NULL;
2200 	ino_t target = *inop;
2201 
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);
2210 		}
2211 	}
2212 
2213 	return vp;
2214 }
2215 
2216 /*
2217  * This function takes a cdev and removes its devfs node in the
2218  * given topology.  The cdev remains intact.
2219  */
2220 int
2221 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
2222 {
2223 	KKASSERT(target != NULL);
2224 	return devfs_destroy_node(root, target->si_name);
2225 }
2226 
2227 /*
2228  * This function takes a path to a devfs node, resolves it and
2229  * removes the devfs node from the given topology.
2230  */
2231 int
2232 devfs_destroy_node(struct devfs_node *root, char *target)
2233 {
2234 	struct devfs_node *node, *parent;
2235 	char *name;
2236 	char *name_buf;
2237 	char *create_path = NULL;
2238 
2239 	KKASSERT(target);
2240 
2241 	name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
2242 	ksnprintf(name_buf, PATH_MAX, "%s", target);
2243 
2244 	devfs_resolve_name_path(target, name_buf, &create_path, &name);
2245 
2246 	if (create_path)
2247 		parent = devfs_resolve_or_create_path(root, create_path, 0);
2248 	else
2249 		parent = root;
2250 
2251 	if (parent == NULL) {
2252 		kfree(name_buf, M_TEMP);
2253 		return 1;
2254 	}
2255 
2256 	node = devfs_find_device_node_by_name(parent, name);
2257 
2258 	if (node) {
2259 		vfs_timestamp(&node->parent->mtime);
2260 		devfs_gc(node);
2261 	}
2262 
2263 	kfree(name_buf, M_TEMP);
2264 
2265 	return 0;
2266 }
2267 
2268 /*
2269  * Just set perms and ownership for given node.
2270  */
2271 int
2272 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid,
2273 		u_short mode, u_long flags)
2274 {
2275 	node->mode = mode;
2276 	node->uid = uid;
2277 	node->gid = gid;
2278 
2279 	return 0;
2280 }
2281 
2282 /*
2283  * Propagates a device attach/detach to all mount
2284  * points. Also takes care of automatic alias removal
2285  * for a deleted cdev.
2286  */
2287 static int
2288 devfs_propagate_dev(cdev_t dev, int attach)
2289 {
2290 	struct devfs_mnt_data *mnt;
2291 
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);
2301 		}
2302 	}
2303 	return 0;
2304 }
2305 
2306 /*
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.
2311  *
2312  * Caller must hold a shared devfs_lock
2313  */
2314 cdev_t
2315 devfs_clone(cdev_t dev, const char *name, size_t len, int mode,
2316 		struct ucred *cred)
2317 {
2318 	int error;
2319 	struct devfs_clone_handler *chandler;
2320 	struct dev_clone_args ap;
2321 
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)) {
2327 			/*
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.
2332 			 */
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;
2345 
2346 			return ap.a_dev;
2347 		}
2348 	}
2349 
2350 	return NULL;
2351 }
2352 
2353 
2354 /*
2355  * Registers a new orphan in the orphan list.
2356  */
2357 void
2358 devfs_tracer_add_orphan(struct devfs_node *node)
2359 {
2360 	struct devfs_orphan *orphan;
2361 
2362 	KKASSERT(node);
2363 	orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2364 	orphan->node = node;
2365 
2366 	KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2367 	node->flags |= DEVFS_ORPHANED;
2368 	TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2369 }
2370 
2371 /*
2372  * Removes an orphan from the orphan list.
2373  */
2374 void
2375 devfs_tracer_del_orphan(struct devfs_node *node)
2376 {
2377 	struct devfs_orphan *orphan;
2378 
2379 	KKASSERT(node);
2380 
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;
2387 		}
2388 	}
2389 }
2390 
2391 /*
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.
2395  */
2396 size_t
2397 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2398 {
2399 	struct devfs_orphan *orphan, *orphan2;
2400 	size_t count = 0;
2401 
2402 	TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2)	{
2403 		count++;
2404 		/*
2405 		 * If we are instructed to clean up, we do so.
2406 		 */
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);
2412 		}
2413 	}
2414 
2415 	return count;
2416 }
2417 
2418 /*
2419  * Fetch an ino_t from the global d_ino by increasing it
2420  * while spinlocked.
2421  */
2422 static ino_t
2423 devfs_fetch_ino(void)
2424 {
2425 	ino_t	ret;
2426 
2427 	spin_lock(&ino_lock);
2428 	ret = d_ino++;
2429 	spin_unlock(&ino_lock);
2430 
2431 	return ret;
2432 }
2433 
2434 /*
2435  * Allocates a new cdev and initializes it's most basic
2436  * fields.
2437  */
2438 cdev_t
2439 devfs_new_cdev(struct dev_ops *ops, int minor, struct dev_ops *bops)
2440 {
2441 	cdev_t dev = sysref_alloc(&cdev_sysref_class);
2442 
2443 	sysref_activate(&dev->si_sysref);
2444 	reference_dev(dev);
2445 	bzero(dev, offsetof(struct cdev, si_sysref));
2446 
2447 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
2448 
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 */
2456 
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;
2463 
2464 	/*
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.
2468 	 */
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;
2473 	}
2474 
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);
2480 
2481 	lockmgr(&devfs_lock, LK_RELEASE);
2482 
2483 	return dev;
2484 }
2485 
2486 static void
2487 devfs_cdev_terminate(cdev_t dev)
2488 {
2489 	/*
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.
2496 	 */
2497 	KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2498 
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));
2501 
2502 	/* devfs_cdev_unlock() is not called, unlock ourselves */
2503 	lockmgr(&devfs_lock, LK_RELEASE);
2504 
2505 	/* Finally destroy the device */
2506 	sysref_put(&dev->si_sysref);
2507 }
2508 
2509 /*
2510  * Dummies for now (individual locks for MPSAFE)
2511  */
2512 static void
2513 devfs_cdev_lock(cdev_t dev)
2514 {
2515 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
2516 }
2517 
2518 static void
2519 devfs_cdev_unlock(cdev_t dev)
2520 {
2521 	lockmgr(&devfs_lock, LK_RELEASE);
2522 }
2523 
2524 static int
2525 devfs_detached_filter_eof(struct knote *kn, long hint)
2526 {
2527 	kn->kn_flags |= (EV_EOF | EV_NODATA);
2528 	return (1);
2529 }
2530 
2531 static void
2532 devfs_detached_filter_detach(struct knote *kn)
2533 {
2534 	cdev_t dev = (cdev_t)kn->kn_hook;
2535 
2536 	knote_remove(&dev->si_kqinfo.ki_note, kn);
2537 }
2538 
2539 static struct filterops devfs_detached_filterops =
2540 	{ FILTEROP_ISFD, NULL,
2541 	  devfs_detached_filter_detach,
2542 	  devfs_detached_filter_eof };
2543 
2544 /*
2545  * Delegates knote filter handling responsibility to devfs
2546  *
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.
2550  */
2551 void
2552 devfs_assume_knotes(cdev_t dev, struct kqinfo *kqi)
2553 {
2554 	/*
2555 	 * Let kern/kern_event.c do the heavy lifting.
2556 	 */
2557 	knote_assume_knotes(kqi, &dev->si_kqinfo,
2558 			    &devfs_detached_filterops, (void *)dev);
2559 
2560 	/*
2561 	 * These should probably be activated individually, but doing so
2562 	 * would require refactoring kq's public in-kernel interface.
2563 	 */
2564 	KNOTE(&dev->si_kqinfo.ki_note, 0);
2565 }
2566 
2567 /*
2568  * Links a given cdev into the dev list.
2569  */
2570 int
2571 devfs_link_dev(cdev_t dev)
2572 {
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);
2576 
2577 	return 0;
2578 }
2579 
2580 /*
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.
2583  *
2584  * Returns EALREADY if the dev has already been unlinked.
2585  */
2586 static int
2587 devfs_unlink_dev(cdev_t dev)
2588 {
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);
2593 	}
2594 	return (EALREADY);
2595 }
2596 
2597 int
2598 devfs_node_is_accessible(struct devfs_node *node)
2599 {
2600 	if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2601 		return 1;
2602 	else
2603 		return 0;
2604 }
2605 
2606 /*
2607  * devfs must be locked
2608  */
2609 static int
2610 devfs_reference_ops(struct dev_ops *ops)
2611 {
2612 	int unit;
2613 	struct devfs_dev_ops *found = NULL;
2614 	struct devfs_dev_ops *devops;
2615 
2616 	TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2617 		if (devops->ops == ops) {
2618 			found = devops;
2619 			break;
2620 		}
2621 	}
2622 
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);
2629 	}
2630 
2631 	KKASSERT(found);
2632 
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");
2641 		}
2642 	}
2643 	unit = found->id;
2644 	++found->ref_count;
2645 
2646 	return unit;
2647 }
2648 
2649 /*
2650  * devfs must be locked
2651  */
2652 static void
2653 devfs_release_ops(struct dev_ops *ops)
2654 {
2655 	struct devfs_dev_ops *found = NULL;
2656 	struct devfs_dev_ops *devops;
2657 
2658 	TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2659 		if (devops->ops == ops) {
2660 			found = devops;
2661 			break;
2662 		}
2663 	}
2664 
2665 	KKASSERT(found);
2666 
2667 	--found->ref_count;
2668 
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);
2675 	}
2676 }
2677 
2678 /*
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.
2682  */
2683 void
2684 devfs_config(void)
2685 {
2686 	devfs_msg_t msg;
2687 
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);
2692 	}
2693 }
2694 
2695 /*
2696  * Called on init of devfs; creates the objcaches and
2697  * spawns off the devfs core thread. Also initializes
2698  * locks.
2699  */
2700 static void
2701 devfs_init(void)
2702 {
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 );
2710 
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 );
2716 
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 );
2722 
2723 	devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2724 
2725 	/* Initialize the reply-only port which acts as a message drain */
2726 	lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2727 
2728 	/* Initialize *THE* devfs lock */
2729 	lockinit(&devfs_lock, "devfs_core lock", 0, LK_CANRECURSE);
2730 	lwkt_token_init(&devfs_token, "devfs_core");
2731 
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);
2738 
2739 	devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2740 }
2741 
2742 /*
2743  * Called on unload of devfs; takes care of destroying the core
2744  * and the objcaches. Also removes aliases that are no longer needed.
2745  */
2746 static void
2747 devfs_uninit(void)
2748 {
2749 	devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2750 
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);
2755 
2756 	devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2757 
2758 	/* Destroy the objcaches */
2759 	objcache_destroy(devfs_msg_cache);
2760 	objcache_destroy(devfs_node_cache);
2761 	objcache_destroy(devfs_dev_cache);
2762 
2763 	devfs_alias_reap();
2764 }
2765 
2766 /*
2767  * This is a sysctl handler to assist userland devname(3) to
2768  * find the device name for a given udev.
2769  */
2770 static int
2771 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2772 {
2773 	dev_t 	udev;
2774 	cdev_t	found;
2775 	int		error;
2776 
2777 	if ((error = SYSCTL_IN(req, &udev, sizeof(dev_t))))
2778 		return (error);
2779 
2780 	devfs_debug(DEVFS_DEBUG_DEBUG,
2781 		    "devfs sysctl, received udev: %d\n", udev);
2782 
2783 	if (udev == NOUDEV)
2784 		return(EINVAL);
2785 
2786 	if ((found = devfs_find_device_by_devid(udev)) == NULL)
2787 		return(ENOENT);
2788 
2789 	return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2790 }
2791 
2792 
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)");
2797 
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");
2802 
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);
2807 
2808 /*
2809  * WildCmp() - compare wild string to sane string
2810  *
2811  *	Returns 0 on success, -1 on failure.
2812  */
2813 static int
2814 wildCmp(const char **mary, int d, const char *w, const char *s)
2815 {
2816     int i;
2817 
2818     /*
2819      * skip fixed portion
2820      */
2821     for (;;) {
2822 	switch(*w) {
2823 	case '*':
2824 	    /*
2825 	     * optimize terminator
2826 	     */
2827 	    if (w[1] == 0)
2828 		return(0);
2829 	    if (w[1] != '?' && w[1] != '*') {
2830 		/*
2831 		 * optimize * followed by non-wild
2832 		 */
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);
2836 		}
2837 	    } else {
2838 		/*
2839 		 * less-optimal
2840 		 */
2841 		for (i = 0; s + i < mary[d]; ++i) {
2842 		    if (wildCmp(mary, d + 1, w + 1, s + i) == 0)
2843 			return(0);
2844 		}
2845 	    }
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;
2862 	}
2863     }
2864     /* not reached */
2865     return(-1);
2866 }
2867 
2868 
2869 /*
2870  * WildCaseCmp() - compare wild string to sane string, case insensitive
2871  *
2872  *	Returns 0 on success, -1 on failure.
2873  */
2874 static int
2875 wildCaseCmp(const char **mary, int d, const char *w, const char *s)
2876 {
2877     int i;
2878 
2879     /*
2880      * skip fixed portion
2881      */
2882     for (;;) {
2883 	switch(*w) {
2884 	case '*':
2885 	    /*
2886 	     * optimize terminator
2887 	     */
2888 	    if (w[1] == 0)
2889 		return(0);
2890 	    if (w[1] != '?' && w[1] != '*') {
2891 		/*
2892 		 * optimize * followed by non-wild
2893 		 */
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);
2897 		}
2898 	    } else {
2899 		/*
2900 		 * less-optimal
2901 		 */
2902 		for (i = 0; s + i < mary[d]; ++i) {
2903 		    if (wildCaseCmp(mary, d + 1, w + 1, s + i) == 0)
2904 			return(0);
2905 		}
2906 	    }
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);
2920 	    }
2921 	    if (*w == 0)	/* terminator */
2922 		return(0);
2923 	    ++w;
2924 	    ++s;
2925 	    break;
2926 	}
2927     }
2928     /* not reached */
2929     return(-1);
2930 }
2931 
2932 struct cdev_privdata {
2933 	void		*cdpd_data;
2934 	d_priv_dtor_t	*cdpd_dtr;
2935 };
2936 
2937 int
2938 devfs_get_cdevpriv(struct file *fp, void **datap)
2939 {
2940 	int error;
2941 
2942 	if (fp == NULL)
2943 		return(EBADF);
2944 
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;
2951 
2952 		*datap = p->cdpd_data;
2953 		error = 0;
2954 	}
2955 	spin_unlock_shared(&fp->f_spin);
2956 
2957 	return (error);
2958 }
2959 
2960 int
2961 devfs_set_cdevpriv(struct file *fp, void *priv, d_priv_dtor_t *dtr)
2962 {
2963 	struct cdev_privdata *p;
2964 	int error;
2965 
2966 	if (fp == NULL)
2967 		return (ENOENT);
2968 
2969 	p = kmalloc(sizeof(struct cdev_privdata), M_DEVFS, M_WAITOK);
2970 	p->cdpd_data = priv;
2971 	p->cdpd_dtr = dtr;
2972 
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;
2979 	}
2980 	spin_unlock(&fp->f_spin);
2981 
2982 	if (error)
2983 		kfree(p, M_DEVFS);
2984 
2985 	return error;
2986 }
2987 
2988 void
2989 devfs_clear_cdevpriv(struct file *fp)
2990 {
2991 	struct cdev_privdata *p;
2992 
2993 	if (fp == NULL)
2994 		return;
2995 
2996 	spin_lock(&fp->f_spin);
2997 	p = fp->f_data1;
2998 	fp->f_data1 = NULL;
2999 	spin_unlock(&fp->f_spin);
3000 
3001 	if (p != NULL) {
3002 		p->cdpd_dtr(p->cdpd_data);
3003 		kfree(p, M_DEVFS);
3004 	}
3005 }
3006 
3007 int
3008 devfs_WildCmp(const char *w, const char *s)
3009 {
3010     int i;
3011     int c;
3012     int slen = strlen(s);
3013     const char **mary;
3014 
3015     for (i = c = 0; w[i]; ++i) {
3016 	if (w[i] == '*')
3017 	    ++c;
3018     }
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);
3025 }
3026 
3027 int
3028 devfs_WildCaseCmp(const char *w, const char *s)
3029 {
3030     int i;
3031     int c;
3032     int slen = strlen(s);
3033     const char **mary;
3034 
3035     for (i = c = 0; w[i]; ++i) {
3036 	if (w[i] == '*')
3037 	    ++c;
3038     }
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);
3045 }
3046 
3047