xref: /dragonfly/sys/vfs/devfs/devfs_core.c (revision 74ad0aa1)
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 	nanotime(&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 
387 out:
388 	return error;
389 }
390 
391 /*
392  * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
393  * based on the newly created devfs node.
394  */
395 int
396 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype,
397 		char *name, struct devfs_node *parent, cdev_t dev)
398 {
399 	struct devfs_node *node;
400 
401 	node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
402 
403 	if (node != NULL)
404 		devfs_allocv(vpp, node);
405 	else
406 		*vpp = NULL;
407 
408 	return 0;
409 }
410 
411 /*
412  * Destroy the devfs_node.  The node must be unlinked from the topology.
413  *
414  * This function will also destroy any vnode association with the node
415  * and device.
416  *
417  * The cdev_t itself remains intact.
418  *
419  * The core lock is not necessarily held on call and must be temporarily
420  * released if it is to avoid a deadlock.
421  */
422 void
423 devfs_freep(struct devfs_node *node)
424 {
425 	struct vnode *vp;
426 	int maxloops;
427 
428 	KKASSERT(node);
429 
430 	/*
431 	 * It is possible for devfs_freep() to race a destruction due
432 	 * to having to release the lock below.  We use DEVFS_DESTROYED
433 	 * to interlock the race (mediated by devfs_lock)
434 	 *
435 	 * We use NLINKSWAIT to indicate that the node couldn't be
436 	 * freed due to having pending nlinks.  We can free
437 	 * the node when nlinks drops to 0.  This should never print
438 	 * a "(null)" name, if it ever does there are still unresolved
439 	 * issues.
440 	 */
441 	if (node->flags & DEVFS_DESTROYED) {
442 		if ((node->flags & DEVFS_NLINKSWAIT) &&
443 		    node->nlinks == 0) {
444 			kprintf("devfs: final node '%s' on nlinks\n",
445 				node->d_dir.d_name);
446 			if (node->d_dir.d_name) {
447 				kfree(node->d_dir.d_name, M_DEVFS);
448 				node->d_dir.d_name = NULL;
449 			}
450 			objcache_put(devfs_node_cache, node);
451 		} else {
452 			kprintf("devfs: race avoided node '%s' (%p)\n",
453 				node->d_dir.d_name, node);
454 		}
455 		return;
456 	}
457 	node->flags |= DEVFS_DESTROYED;
458 
459 	/*
460 	 * Items we have to dispose of before potentially releasing
461 	 * devfs_lock.
462 	 *
463 	 * Remove the node from the orphan list if it is still on it.
464 	 */
465 	atomic_subtract_long(&DEVFS_MNTDATA(node->mp)->leak_count, 1);
466 	atomic_subtract_long(&DEVFS_MNTDATA(node->mp)->file_count, 1);
467 	if (node->flags & DEVFS_ORPHANED)
468 		devfs_tracer_del_orphan(node);
469 
470 	/*
471 	 * At this point only the vp points to node, and node cannot be
472 	 * physically freed because we own DEVFS_DESTROYED.
473 	 *
474 	 * We must dispose of the vnode without deadlocking or racing
475 	 * against e.g. a vnode reclaim.
476 	 *
477 	 * This also prevents the vnode reclaim code from double-freeing
478 	 * the node.  The vget() is required to safely modified the vp
479 	 * and cycle the refs to terminate an inactive vp.
480 	 */
481 	maxloops = 1000;
482 	while ((vp = node->v_node) != NULL) {
483 		int relock;
484 
485 		vhold(vp);
486 		if (lockstatus(&devfs_lock, curthread) == LK_EXCLUSIVE) {
487 			lockmgr(&devfs_lock, LK_RELEASE);
488 			relock = 1;
489 		} else {
490 			relock = 0;
491 		}
492 		if (node->v_node == NULL) {
493 			/* reclaim race, mediated by devfs_lock */
494 			vdrop(vp);
495 		} else if (vget(vp, LK_EXCLUSIVE | LK_RETRY) == 0) {
496 			vdrop(vp);
497 			v_release_rdev(vp);
498 			vp->v_data = NULL;
499 			node->v_node = NULL;
500 			vput(vp);
501 		} else {
502 			/* reclaim race, mediated by devfs_lock */
503 			vdrop(vp);
504 		}
505 		if (relock)
506 			lockmgr(&devfs_lock, LK_EXCLUSIVE);
507 		if (--maxloops == 0) {
508 			kprintf("devfs_freep: livelock on node %p\n", node);
509 			break;
510 		}
511 	}
512 
513 	/*
514 	 * Remaining cleanup
515 	 */
516 	if (node->symlink_name)	{
517 		kfree(node->symlink_name, M_DEVFS);
518 		node->symlink_name = NULL;
519 	}
520 
521 	/*
522 	 * We cannot actually free the node if it still has
523 	 * nlinks.
524 	 */
525 	if (node->nlinks) {
526 		node->flags |= DEVFS_NLINKSWAIT;
527 	} else {
528 		if (node->d_dir.d_name) {
529 			kfree(node->d_dir.d_name, M_DEVFS);
530 			node->d_dir.d_name = NULL;
531 		}
532 		objcache_put(devfs_node_cache, node);
533 	}
534 }
535 
536 /*
537  * Returns a valid vp associated with the devfs alias node or NULL
538  */
539 static void *devfs_alias_getvp(struct devfs_node *node)
540 {
541 	struct devfs_node *found = node;
542 	int depth = 0;
543 
544 	while ((found->node_type == Nlink) && (found->link_target)) {
545 		if (depth >= 8) {
546 			devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8");
547 			break;
548 		}
549 
550 		found = found->link_target;
551 		++depth;
552 	}
553 
554 	return found->v_node;
555 }
556 
557 /*
558  * Unlink the devfs node from the topology and add it to the orphan list.
559  * The node will later be destroyed by freep.
560  *
561  * Any vnode association, including the v_rdev and v_data, remains intact
562  * until the freep.
563  */
564 void
565 devfs_unlinkp(struct devfs_node *node)
566 {
567 	struct devfs_node *parent;
568 	struct devfs_node *target;
569 	struct vnode *vp;
570 	KKASSERT(node);
571 
572 	/*
573 	 * Add the node to the orphan list, so it is referenced somewhere, to
574 	 * so we don't leak it.
575 	 */
576 	devfs_tracer_add_orphan(node);
577 
578 	parent = node->parent;
579 	node->parent = NULL;
580 
581 	/*
582 	 * If the parent is known we can unlink the node out of the topology
583 	 */
584 	if (node->flags & DEVFS_NODE_LINKED) {
585 		if (parent) {
586 			TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
587 			parent->nchildren--;
588 		} else if (node == DEVFS_MNTDATA(node->mp)->root_node) {
589 			DEVFS_MNTDATA(node->mp)->root_node = NULL;
590 		}
591 		node->flags &= ~DEVFS_NODE_LINKED;
592 	}
593 
594 	/*
595 	 * Namecache invalidation.
596 	 *
597 	 * devfs alias nodes are special: their v_node entry is always null
598 	 * and they use the one from their link target.  We thus use the
599 	 * target node's vp to invalidate both alias and target entries in
600 	 * the namecache.
601 	 *
602 	 * Doing so for the target is not necessary but it would be more
603 	 * expensive to resolve only the namecache entry of the alias node
604 	 * from the information available in this function.
605 	 *
606 	 * WARNING! We do not disassociate the vnode here.  That can only
607 	 *	    be safely done in devfs_freep().
608 	 */
609 	if (node->node_type == Nlink) {
610 		if ((target = node->link_target) != NULL) {
611 			vp = devfs_alias_getvp(node);
612 			node->link_target = NULL;
613 			target->nlinks--;
614 			if (target->nlinks == 0 &&
615 			    (target->flags & DEVFS_DESTROYED)) {
616 				devfs_freep(target);
617 			}
618 		} else {
619 			vp = NULL;
620 		}
621 	} else {
622 		vp = node->v_node;
623 	}
624 
625 	if (vp != NULL)
626 		cache_inval_vp(vp, CINV_DESTROY);
627 }
628 
629 void *
630 devfs_iterate_topology(struct devfs_node *node,
631 		devfs_iterate_callback_t *callback, void *arg1)
632 {
633 	struct devfs_node *node1, *node2;
634 	void *ret = NULL;
635 
636 	if (((node->node_type == Nroot) || (node->node_type == Ndir)) &&
637 	    node->nchildren > 2) {
638 		TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
639 				      link, node2) {
640 			ret = devfs_iterate_topology(node1, callback, arg1);
641 			if (ret)
642 				return ret;
643 		}
644 	}
645 	ret = callback(node, arg1);
646 
647 	return ret;
648 }
649 
650 static void *
651 devfs_alias_reaper_callback(struct devfs_node *node, void *unused)
652 {
653 	if (node->node_type == Nlink) {
654 		devfs_unlinkp(node);
655 		devfs_freep(node);
656 	}
657 
658 	return NULL;
659 }
660 
661 /*
662  * devfs_reaperp() is a recursive function that iterates through all the
663  * topology, unlinking and freeing all devfs nodes.
664  */
665 static void *
666 devfs_reaperp_callback(struct devfs_node *node, void *unused)
667 {
668 	devfs_unlinkp(node);
669 	devfs_freep(node);
670 
671 	return NULL;
672 }
673 
674 /*
675  * Report any orphans that we couldn't delete.  The mp and mnt_data
676  * are both disappearing, so we must also clean up the nodes a bit.
677  */
678 static void
679 devfs_iterate_orphans_unmount(struct mount *mp)
680 {
681 	struct devfs_orphan *orphan;
682 
683 	while ((orphan = TAILQ_FIRST(DEVFS_ORPHANLIST(mp))) != NULL) {
684 		devfs_freep(orphan->node);
685 		/* orphan stale */
686 	}
687 }
688 
689 static void *
690 devfs_gc_dirs_callback(struct devfs_node *node, void *unused)
691 {
692 	if (node->node_type == Ndir) {
693 		if ((node->nchildren == 2) &&
694 		    !(node->flags & DEVFS_USER_CREATED)) {
695 			devfs_unlinkp(node);
696 			devfs_freep(node);
697 		}
698 	}
699 
700 	return NULL;
701 }
702 
703 static void *
704 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target)
705 {
706 	if ((node->node_type == Nlink) && (node->link_target == target)) {
707 		devfs_unlinkp(node);
708 		devfs_freep(node);
709 	}
710 
711 	return NULL;
712 }
713 
714 /*
715  * devfs_gc() is devfs garbage collector. It takes care of unlinking and
716  * freeing a node, but also removes empty directories and links that link
717  * via devfs auto-link mechanism to the node being deleted.
718  */
719 int
720 devfs_gc(struct devfs_node *node)
721 {
722 	struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
723 
724 	if (node->nlinks > 0)
725 		devfs_iterate_topology(root_node,
726 				(devfs_iterate_callback_t *)devfs_gc_links_callback, node);
727 
728 	devfs_unlinkp(node);
729 	devfs_iterate_topology(root_node,
730 			(devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL);
731 
732 	devfs_freep(node);
733 
734 	return 0;
735 }
736 
737 /*
738  * devfs_create_dev() is the asynchronous entry point for device creation.
739  * It just sends a message with the relevant details to the devfs core.
740  *
741  * This function will reference the passed device.  The reference is owned
742  * by devfs and represents all of the device's node associations.
743  */
744 int
745 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
746 {
747 	reference_dev(dev);
748 	devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
749 
750 	return 0;
751 }
752 
753 /*
754  * devfs_destroy_dev() is the asynchronous entry point for device destruction.
755  * It just sends a message with the relevant details to the devfs core.
756  */
757 int
758 devfs_destroy_dev(cdev_t dev)
759 {
760 	devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
761 	return 0;
762 }
763 
764 /*
765  * devfs_mount_add() is the synchronous entry point for adding a new devfs
766  * mount.  It sends a synchronous message with the relevant details to the
767  * devfs core.
768  */
769 int
770 devfs_mount_add(struct devfs_mnt_data *mnt)
771 {
772 	devfs_msg_t msg;
773 
774 	msg = devfs_msg_get();
775 	msg->mdv_mnt = mnt;
776 	devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
777 	devfs_msg_put(msg);
778 
779 	return 0;
780 }
781 
782 /*
783  * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
784  * It sends a synchronous message with the relevant details to the devfs core.
785  */
786 int
787 devfs_mount_del(struct devfs_mnt_data *mnt)
788 {
789 	devfs_msg_t msg;
790 
791 	msg = devfs_msg_get();
792 	msg->mdv_mnt = mnt;
793 	devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
794 	devfs_msg_put(msg);
795 
796 	return 0;
797 }
798 
799 /*
800  * devfs_destroy_related() is the synchronous entry point for device
801  * destruction by subname. It just sends a message with the relevant details to
802  * the devfs core.
803  */
804 int
805 devfs_destroy_related(cdev_t dev)
806 {
807 	devfs_msg_t msg;
808 
809 	msg = devfs_msg_get();
810 	msg->mdv_load = dev;
811 	devfs_msg_send_sync(DEVFS_DESTROY_RELATED, msg);
812 	devfs_msg_put(msg);
813 	return 0;
814 }
815 
816 int
817 devfs_clr_related_flag(cdev_t dev, uint32_t flag)
818 {
819 	devfs_msg_t msg;
820 
821 	msg = devfs_msg_get();
822 	msg->mdv_flags.dev = dev;
823 	msg->mdv_flags.flag = flag;
824 	devfs_msg_send_sync(DEVFS_CLR_RELATED_FLAG, msg);
825 	devfs_msg_put(msg);
826 
827 	return 0;
828 }
829 
830 int
831 devfs_destroy_related_without_flag(cdev_t dev, uint32_t flag)
832 {
833 	devfs_msg_t msg;
834 
835 	msg = devfs_msg_get();
836 	msg->mdv_flags.dev = dev;
837 	msg->mdv_flags.flag = flag;
838 	devfs_msg_send_sync(DEVFS_DESTROY_RELATED_WO_FLAG, msg);
839 	devfs_msg_put(msg);
840 
841 	return 0;
842 }
843 
844 /*
845  * devfs_create_all_dev is the asynchronous entry point to trigger device
846  * node creation.  It just sends a message with the relevant details to
847  * the devfs core.
848  */
849 int
850 devfs_create_all_dev(struct devfs_node *root)
851 {
852 	devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
853 	return 0;
854 }
855 
856 /*
857  * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
858  * devices with a specific set of dev_ops and minor.  It just sends a
859  * message with the relevant details to the devfs core.
860  */
861 int
862 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
863 {
864 	devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
865 	return 0;
866 }
867 
868 /*
869  * devfs_clone_handler_add is the synchronous entry point to add a new
870  * clone handler.  It just sends a message with the relevant details to
871  * the devfs core.
872  */
873 int
874 devfs_clone_handler_add(const char *name, d_clone_t *nhandler)
875 {
876 	devfs_msg_t msg;
877 
878 	msg = devfs_msg_get();
879 	msg->mdv_chandler.name = name;
880 	msg->mdv_chandler.nhandler = nhandler;
881 	devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg);
882 	devfs_msg_put(msg);
883 	return 0;
884 }
885 
886 /*
887  * devfs_clone_handler_del is the synchronous entry point to remove a
888  * clone handler.  It just sends a message with the relevant details to
889  * the devfs core.
890  */
891 int
892 devfs_clone_handler_del(const char *name)
893 {
894 	devfs_msg_t msg;
895 
896 	msg = devfs_msg_get();
897 	msg->mdv_chandler.name = name;
898 	msg->mdv_chandler.nhandler = NULL;
899 	devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg);
900 	devfs_msg_put(msg);
901 	return 0;
902 }
903 
904 /*
905  * devfs_find_device_by_name is the synchronous entry point to find a
906  * device given its name.  It sends a synchronous message with the
907  * relevant details to the devfs core and returns the answer.
908  */
909 cdev_t
910 devfs_find_device_by_name(const char *fmt, ...)
911 {
912 	cdev_t found = NULL;
913 	devfs_msg_t msg;
914 	char *target;
915 	__va_list ap;
916 
917 	if (fmt == NULL)
918 		return NULL;
919 
920 	__va_start(ap, fmt);
921 	kvasnprintf(&target, PATH_MAX, fmt, ap);
922 	__va_end(ap);
923 
924 	msg = devfs_msg_get();
925 	msg->mdv_name = target;
926 	devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
927 	found = msg->mdv_cdev;
928 	devfs_msg_put(msg);
929 	kvasfree(&target);
930 
931 	return found;
932 }
933 
934 /*
935  * devfs_find_device_by_devid is the synchronous entry point to find a
936  * device given its udev number.  It sends a synchronous message with
937  * the relevant details to the devfs core and returns the answer.
938  */
939 cdev_t
940 devfs_find_device_by_devid(dev_t udev)
941 {
942 	cdev_t found = NULL;
943 	devfs_msg_t msg;
944 
945 	msg = devfs_msg_get();
946 	msg->mdv_udev = udev;
947 	devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_DEVID, msg);
948 	found = msg->mdv_cdev;
949 	devfs_msg_put(msg);
950 
951 	devfs_debug(DEVFS_DEBUG_DEBUG,
952 		    "devfs_find_device_by_devid found? %s  -end:3-\n",
953 		    ((found) ? found->si_name:"NO"));
954 	return found;
955 }
956 
957 struct vnode *
958 devfs_inode_to_vnode(struct mount *mp, ino_t target)
959 {
960 	struct vnode *vp = NULL;
961 	devfs_msg_t msg;
962 
963 	if (mp == NULL)
964 		return NULL;
965 
966 	msg = devfs_msg_get();
967 	msg->mdv_ino.mp = mp;
968 	msg->mdv_ino.ino = target;
969 	devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg);
970 	vp = msg->mdv_ino.vp;
971 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
972 	devfs_msg_put(msg);
973 
974 	return vp;
975 }
976 
977 /*
978  * devfs_make_alias is the asynchronous entry point to register an alias
979  * for a device.  It just sends a message with the relevant details to the
980  * devfs core.
981  */
982 int
983 devfs_make_alias(const char *name, cdev_t dev_target)
984 {
985 	struct devfs_alias *alias;
986 	size_t len;
987 
988 	len = strlen(name);
989 
990 	alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
991 	alias->name = kstrdup(name, M_DEVFS);
992 	alias->namlen = len;
993 	alias->dev_target = dev_target;
994 
995 	devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
996 	return 0;
997 }
998 
999 /*
1000  * devfs_destroy_alias is the asynchronous entry point to deregister an alias
1001  * for a device.  It just sends a message with the relevant details to the
1002  * devfs core.
1003  */
1004 int
1005 devfs_destroy_alias(const char *name, cdev_t dev_target)
1006 {
1007 	struct devfs_alias *alias;
1008 	size_t len;
1009 
1010 	len = strlen(name);
1011 
1012 	alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
1013 	alias->name = kstrdup(name, M_DEVFS);
1014 	alias->namlen = len;
1015 	alias->dev_target = dev_target;
1016 
1017 	devfs_msg_send_generic(DEVFS_DESTROY_ALIAS, alias);
1018 	return 0;
1019 }
1020 
1021 /*
1022  * devfs_apply_rules is the asynchronous entry point to trigger application
1023  * of all rules.  It just sends a message with the relevant details to the
1024  * devfs core.
1025  */
1026 int
1027 devfs_apply_rules(char *mntto)
1028 {
1029 	char *new_name;
1030 
1031 	new_name = kstrdup(mntto, M_DEVFS);
1032 	devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
1033 
1034 	return 0;
1035 }
1036 
1037 /*
1038  * devfs_reset_rules is the asynchronous entry point to trigger reset of all
1039  * rules. It just sends a message with the relevant details to the devfs core.
1040  */
1041 int
1042 devfs_reset_rules(char *mntto)
1043 {
1044 	char *new_name;
1045 
1046 	new_name = kstrdup(mntto, M_DEVFS);
1047 	devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
1048 
1049 	return 0;
1050 }
1051 
1052 
1053 /*
1054  * devfs_scan_callback is the asynchronous entry point to call a callback
1055  * on all cdevs.
1056  * It just sends a message with the relevant details to the devfs core.
1057  */
1058 int
1059 devfs_scan_callback(devfs_scan_t *callback, void *arg)
1060 {
1061 	devfs_msg_t msg;
1062 
1063 	KKASSERT(callback);
1064 
1065 	msg = devfs_msg_get();
1066 	msg->mdv_load = callback;
1067 	msg->mdv_load2 = arg;
1068 	devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
1069 	devfs_msg_put(msg);
1070 
1071 	return 0;
1072 }
1073 
1074 
1075 /*
1076  * Acts as a message drain. Any message that is replied to here gets destroyed
1077  * and the memory freed.
1078  */
1079 static void
1080 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
1081 {
1082 	devfs_msg_put((devfs_msg_t)msg);
1083 }
1084 
1085 /*
1086  * devfs_msg_get allocates a new devfs msg and returns it.
1087  */
1088 devfs_msg_t
1089 devfs_msg_get(void)
1090 {
1091 	return objcache_get(devfs_msg_cache, M_WAITOK);
1092 }
1093 
1094 /*
1095  * devfs_msg_put deallocates a given devfs msg.
1096  */
1097 int
1098 devfs_msg_put(devfs_msg_t msg)
1099 {
1100 	objcache_put(devfs_msg_cache, msg);
1101 	return 0;
1102 }
1103 
1104 /*
1105  * devfs_msg_send is the generic asynchronous message sending facility
1106  * for devfs. By default the reply port is the automatic disposal port.
1107  *
1108  * If the current thread is the devfs_msg_port thread we execute the
1109  * operation synchronously.
1110  */
1111 void
1112 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
1113 {
1114 	lwkt_port_t port = &devfs_msg_port;
1115 
1116 	lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
1117 
1118 	devfs_msg->hdr.u.ms_result = cmd;
1119 
1120 	if (port->mpu_td == curthread) {
1121 		devfs_msg_exec(devfs_msg);
1122 		lwkt_replymsg(&devfs_msg->hdr, 0);
1123 	} else {
1124 		lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
1125 	}
1126 }
1127 
1128 /*
1129  * devfs_msg_send_sync is the generic synchronous message sending
1130  * facility for devfs. It initializes a local reply port and waits
1131  * for the core's answer. The core will write the answer on the same
1132  * message which is sent back as reply. The caller still has a reference
1133  * to the message, so we don't need to return it.
1134  */
1135 int
1136 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
1137 {
1138 	struct lwkt_port rep_port;
1139 	int	error;
1140 	lwkt_port_t port = &devfs_msg_port;
1141 
1142 	lwkt_initport_thread(&rep_port, curthread);
1143 	lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
1144 
1145 	devfs_msg->hdr.u.ms_result = cmd;
1146 
1147 	error = lwkt_domsg(port, (lwkt_msg_t)devfs_msg, 0);
1148 
1149 	return error;
1150 }
1151 
1152 /*
1153  * sends a message with a generic argument.
1154  */
1155 void
1156 devfs_msg_send_generic(uint32_t cmd, void *load)
1157 {
1158 	devfs_msg_t devfs_msg = devfs_msg_get();
1159 
1160 	devfs_msg->mdv_load = load;
1161 	devfs_msg_send(cmd, devfs_msg);
1162 }
1163 
1164 /*
1165  * sends a message with a name argument.
1166  */
1167 void
1168 devfs_msg_send_name(uint32_t cmd, char *name)
1169 {
1170 	devfs_msg_t devfs_msg = devfs_msg_get();
1171 
1172 	devfs_msg->mdv_name = name;
1173 	devfs_msg_send(cmd, devfs_msg);
1174 }
1175 
1176 /*
1177  * sends a message with a mount argument.
1178  */
1179 void
1180 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
1181 {
1182 	devfs_msg_t devfs_msg = devfs_msg_get();
1183 
1184 	devfs_msg->mdv_mnt = mnt;
1185 	devfs_msg_send(cmd, devfs_msg);
1186 }
1187 
1188 /*
1189  * sends a message with an ops argument.
1190  */
1191 void
1192 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
1193 {
1194 	devfs_msg_t devfs_msg = devfs_msg_get();
1195 
1196 	devfs_msg->mdv_ops.ops = ops;
1197 	devfs_msg->mdv_ops.minor = minor;
1198 	devfs_msg_send(cmd, devfs_msg);
1199 }
1200 
1201 /*
1202  * sends a message with a clone handler argument.
1203  */
1204 void
1205 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
1206 {
1207 	devfs_msg_t devfs_msg = devfs_msg_get();
1208 
1209 	devfs_msg->mdv_chandler.name = name;
1210 	devfs_msg->mdv_chandler.nhandler = handler;
1211 	devfs_msg_send(cmd, devfs_msg);
1212 }
1213 
1214 /*
1215  * sends a message with a device argument.
1216  */
1217 void
1218 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1219 {
1220 	devfs_msg_t devfs_msg = devfs_msg_get();
1221 
1222 	devfs_msg->mdv_dev.dev = dev;
1223 	devfs_msg->mdv_dev.uid = uid;
1224 	devfs_msg->mdv_dev.gid = gid;
1225 	devfs_msg->mdv_dev.perms = perms;
1226 
1227 	devfs_msg_send(cmd, devfs_msg);
1228 }
1229 
1230 /*
1231  * sends a message with a link argument.
1232  */
1233 void
1234 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1235 {
1236 	devfs_msg_t devfs_msg = devfs_msg_get();
1237 
1238 	devfs_msg->mdv_link.name = name;
1239 	devfs_msg->mdv_link.target = target;
1240 	devfs_msg->mdv_link.mp = mp;
1241 	devfs_msg_send(cmd, devfs_msg);
1242 }
1243 
1244 /*
1245  * devfs_msg_core is the main devfs thread. It handles all incoming messages
1246  * and calls the relevant worker functions. By using messages it's assured
1247  * that events occur in the correct order.
1248  */
1249 static void
1250 devfs_msg_core(void *arg)
1251 {
1252 	devfs_msg_t msg;
1253 
1254 	lwkt_initport_thread(&devfs_msg_port, curthread);
1255 
1256 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
1257 	devfs_run = 1;
1258 	wakeup(td_core);
1259 	lockmgr(&devfs_lock, LK_RELEASE);
1260 
1261 	lwkt_gettoken(&devfs_token);
1262 
1263 	while (devfs_run) {
1264 		msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1265 		devfs_debug(DEVFS_DEBUG_DEBUG,
1266 				"devfs_msg_core, new msg: %x\n",
1267 				(unsigned int)msg->hdr.u.ms_result);
1268 		devfs_msg_exec(msg);
1269 		lwkt_replymsg(&msg->hdr, 0);
1270 	}
1271 
1272 	lwkt_reltoken(&devfs_token);
1273 	wakeup(td_core);
1274 
1275 	lwkt_exit();
1276 }
1277 
1278 static void
1279 devfs_msg_exec(devfs_msg_t msg)
1280 {
1281 	struct devfs_mnt_data *mnt;
1282 	struct devfs_node *node;
1283 	cdev_t	dev;
1284 
1285 	/*
1286 	 * Acquire the devfs lock to ensure safety of all called functions
1287 	 */
1288 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
1289 
1290 	switch (msg->hdr.u.ms_result) {
1291 	case DEVFS_DEVICE_CREATE:
1292 		dev = msg->mdv_dev.dev;
1293 		devfs_create_dev_worker(dev,
1294 					msg->mdv_dev.uid,
1295 					msg->mdv_dev.gid,
1296 					msg->mdv_dev.perms);
1297 		break;
1298 	case DEVFS_DEVICE_DESTROY:
1299 		dev = msg->mdv_dev.dev;
1300 		devfs_destroy_dev_worker(dev);
1301 		break;
1302 	case DEVFS_DESTROY_RELATED:
1303 		devfs_destroy_related_worker(msg->mdv_load);
1304 		break;
1305 	case DEVFS_DESTROY_DEV_BY_OPS:
1306 		devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1307 						msg->mdv_ops.minor);
1308 		break;
1309 	case DEVFS_CREATE_ALL_DEV:
1310 		node = (struct devfs_node *)msg->mdv_load;
1311 		devfs_create_all_dev_worker(node);
1312 		break;
1313 	case DEVFS_MOUNT_ADD:
1314 		mnt = msg->mdv_mnt;
1315 		TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1316 		devfs_create_all_dev_worker(mnt->root_node);
1317 		break;
1318 	case DEVFS_MOUNT_DEL:
1319 		mnt = msg->mdv_mnt;
1320 		TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1321 		/* Be sure to remove all the aliases first */
1322 		devfs_iterate_topology(mnt->root_node,
1323 				       devfs_alias_reaper_callback,
1324 				       NULL);
1325 		devfs_iterate_topology(mnt->root_node,
1326 				       devfs_reaperp_callback,
1327 				       NULL);
1328 		devfs_iterate_orphans_unmount(mnt->mp);
1329 		if (mnt->leak_count) {
1330 			devfs_debug(DEVFS_DEBUG_SHOW,
1331 				    "Leaked %ld devfs_node elements!\n",
1332 				    mnt->leak_count);
1333 		}
1334 		break;
1335 	case DEVFS_CHANDLER_ADD:
1336 		devfs_chandler_add_worker(msg->mdv_chandler.name,
1337 				msg->mdv_chandler.nhandler);
1338 		break;
1339 	case DEVFS_CHANDLER_DEL:
1340 		devfs_chandler_del_worker(msg->mdv_chandler.name);
1341 		break;
1342 	case DEVFS_FIND_DEVICE_BY_NAME:
1343 		devfs_find_device_by_name_worker(msg);
1344 		break;
1345 	case DEVFS_FIND_DEVICE_BY_DEVID:
1346 		devfs_find_device_by_devid_worker(msg);
1347 		break;
1348 	case DEVFS_MAKE_ALIAS:
1349 		devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1350 		break;
1351 	case DEVFS_DESTROY_ALIAS:
1352 		devfs_destroy_alias_worker((struct devfs_alias *)msg->mdv_load);
1353 		break;
1354 	case DEVFS_APPLY_RULES:
1355 		devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1356 		break;
1357 	case DEVFS_RESET_RULES:
1358 		devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1359 		break;
1360 	case DEVFS_SCAN_CALLBACK:
1361 		devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load,
1362 			msg->mdv_load2);
1363 		break;
1364 	case DEVFS_CLR_RELATED_FLAG:
1365 		devfs_clr_related_flag_worker(msg->mdv_flags.dev,
1366 				msg->mdv_flags.flag);
1367 		break;
1368 	case DEVFS_DESTROY_RELATED_WO_FLAG:
1369 		devfs_destroy_related_without_flag_worker(msg->mdv_flags.dev,
1370 				msg->mdv_flags.flag);
1371 		break;
1372 	case DEVFS_INODE_TO_VNODE:
1373 		msg->mdv_ino.vp = devfs_iterate_topology(
1374 			DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node,
1375 			(devfs_iterate_callback_t *)devfs_inode_to_vnode_worker_callback,
1376 			&msg->mdv_ino.ino);
1377 		break;
1378 	case DEVFS_TERMINATE_CORE:
1379 		devfs_run = 0;
1380 		break;
1381 	case DEVFS_SYNC:
1382 		break;
1383 	default:
1384 		devfs_debug(DEVFS_DEBUG_WARNING,
1385 			    "devfs_msg_core: unknown message "
1386 			    "received at core\n");
1387 		break;
1388 	}
1389 	lockmgr(&devfs_lock, LK_RELEASE);
1390 }
1391 
1392 static void
1393 devfs_devctl_notify(cdev_t dev, const char *ev)
1394 {
1395 	static const char prefix[] = "cdev=";
1396 	char *data;
1397 	int namelen;
1398 
1399 	namelen = strlen(dev->si_name);
1400 	data = kmalloc(namelen + sizeof(prefix), M_TEMP, M_WAITOK);
1401 	memcpy(data, prefix, sizeof(prefix) - 1);
1402 	memcpy(data + sizeof(prefix) - 1, dev->si_name, namelen + 1);
1403 	devctl_notify("DEVFS", "CDEV", ev, data);
1404 	kfree(data, M_TEMP);
1405 }
1406 
1407 /*
1408  * Worker function to insert a new dev into the dev list and initialize its
1409  * permissions. It also calls devfs_propagate_dev which in turn propagates
1410  * the change to all mount points.
1411  *
1412  * The passed dev is already referenced.  This reference is eaten by this
1413  * function and represents the dev's linkage into devfs_dev_list.
1414  */
1415 static int
1416 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1417 {
1418 	KKASSERT(dev);
1419 
1420 	dev->si_uid = uid;
1421 	dev->si_gid = gid;
1422 	dev->si_perms = perms;
1423 
1424 	devfs_link_dev(dev);
1425 	devfs_propagate_dev(dev, 1);
1426 
1427 	udev_event_attach(dev, NULL, 0);
1428 	devfs_devctl_notify(dev, "CREATE");
1429 
1430 	return 0;
1431 }
1432 
1433 /*
1434  * Worker function to delete a dev from the dev list and free the cdev.
1435  * It also calls devfs_propagate_dev which in turn propagates the change
1436  * to all mount points.
1437  */
1438 static int
1439 devfs_destroy_dev_worker(cdev_t dev)
1440 {
1441 	int error;
1442 
1443 	KKASSERT(dev);
1444 	KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1445 
1446 	error = devfs_unlink_dev(dev);
1447 	devfs_propagate_dev(dev, 0);
1448 
1449 	devfs_devctl_notify(dev, "DESTROY");
1450 	udev_event_detach(dev, NULL, 0);
1451 
1452 	if (error == 0)
1453 		release_dev(dev);	/* link ref */
1454 	release_dev(dev);
1455 	release_dev(dev);
1456 
1457 	return 0;
1458 }
1459 
1460 /*
1461  * Worker function to destroy all devices with a certain basename.
1462  * Calls devfs_destroy_dev_worker for the actual destruction.
1463  */
1464 static int
1465 devfs_destroy_related_worker(cdev_t needle)
1466 {
1467 	cdev_t dev;
1468 
1469 restart:
1470 	devfs_debug(DEVFS_DEBUG_DEBUG, "related worker: %s\n",
1471 	    needle->si_name);
1472 	TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1473 		if (dev->si_parent == needle) {
1474 			devfs_destroy_related_worker(dev);
1475 			devfs_destroy_dev_worker(dev);
1476 			goto restart;
1477 		}
1478 	}
1479 	return 0;
1480 }
1481 
1482 static int
1483 devfs_clr_related_flag_worker(cdev_t needle, uint32_t flag)
1484 {
1485 	cdev_t dev, dev1;
1486 
1487 	TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1488 		if (dev->si_parent == needle) {
1489 			devfs_clr_related_flag_worker(dev, flag);
1490 			dev->si_flags &= ~flag;
1491 		}
1492 	}
1493 
1494 	return 0;
1495 }
1496 
1497 static int
1498 devfs_destroy_related_without_flag_worker(cdev_t needle, uint32_t flag)
1499 {
1500 	cdev_t dev;
1501 
1502 restart:
1503 	devfs_debug(DEVFS_DEBUG_DEBUG, "related_wo_flag: %s\n",
1504 	    needle->si_name);
1505 
1506 	TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1507 		if (dev->si_parent == needle) {
1508 			devfs_destroy_related_without_flag_worker(dev, flag);
1509 			if (!(dev->si_flags & flag)) {
1510 				devfs_destroy_dev_worker(dev);
1511 				devfs_debug(DEVFS_DEBUG_DEBUG,
1512 				    "related_wo_flag: %s restart\n", dev->si_name);
1513 				goto restart;
1514 			}
1515 		}
1516 	}
1517 
1518 	return 0;
1519 }
1520 
1521 /*
1522  * Worker function that creates all device nodes on top of a devfs
1523  * root node.
1524  */
1525 static int
1526 devfs_create_all_dev_worker(struct devfs_node *root)
1527 {
1528 	cdev_t dev;
1529 
1530 	KKASSERT(root);
1531 
1532 	TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1533 		devfs_create_device_node(root, dev, NULL, NULL, NULL);
1534 	}
1535 
1536 	return 0;
1537 }
1538 
1539 /*
1540  * Worker function that destroys all devices that match a specific
1541  * dev_ops and/or minor. If minor is less than 0, it is not matched
1542  * against. It also propagates all changes.
1543  */
1544 static int
1545 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1546 {
1547 	cdev_t dev, dev1;
1548 
1549 	KKASSERT(ops);
1550 
1551 	TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1552 		if (dev->si_ops != ops)
1553 			continue;
1554 		if ((minor < 0) || (dev->si_uminor == minor)) {
1555 			devfs_destroy_dev_worker(dev);
1556 		}
1557 	}
1558 
1559 	return 0;
1560 }
1561 
1562 /*
1563  * Worker function that registers a new clone handler in devfs.
1564  */
1565 static int
1566 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler)
1567 {
1568 	struct devfs_clone_handler *chandler = NULL;
1569 	u_char len = strlen(name);
1570 
1571 	if (len == 0)
1572 		return 1;
1573 
1574 	TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1575 		if (chandler->namlen != len)
1576 			continue;
1577 
1578 		if (!memcmp(chandler->name, name, len)) {
1579 			/* Clonable basename already exists */
1580 			return 1;
1581 		}
1582 	}
1583 
1584 	chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1585 	chandler->name = kstrdup(name, M_DEVFS);
1586 	chandler->namlen = len;
1587 	chandler->nhandler = nhandler;
1588 
1589 	TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1590 	return 0;
1591 }
1592 
1593 /*
1594  * Worker function that removes a given clone handler from the
1595  * clone handler list.
1596  */
1597 static int
1598 devfs_chandler_del_worker(const char *name)
1599 {
1600 	struct devfs_clone_handler *chandler, *chandler2;
1601 	u_char len = strlen(name);
1602 
1603 	if (len == 0)
1604 		return 1;
1605 
1606 	TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1607 		if (chandler->namlen != len)
1608 			continue;
1609 		if (memcmp(chandler->name, name, len))
1610 			continue;
1611 
1612 		TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1613 		kfree(chandler->name, M_DEVFS);
1614 		kfree(chandler, M_DEVFS);
1615 		break;
1616 	}
1617 
1618 	return 0;
1619 }
1620 
1621 /*
1622  * Worker function that finds a given device name and changes
1623  * the message received accordingly so that when replied to,
1624  * the answer is returned to the caller.
1625  */
1626 static int
1627 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1628 {
1629 	struct devfs_alias *alias;
1630 	cdev_t dev;
1631 	cdev_t found = NULL;
1632 
1633 	TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1634 		if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1635 			found = dev;
1636 			break;
1637 		}
1638 	}
1639 	if (found == NULL) {
1640 		TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1641 			if (strcmp(devfs_msg->mdv_name, alias->name) == 0) {
1642 				found = alias->dev_target;
1643 				break;
1644 			}
1645 		}
1646 	}
1647 	devfs_msg->mdv_cdev = found;
1648 
1649 	return 0;
1650 }
1651 
1652 /*
1653  * Worker function that finds a given device udev and changes
1654  * the message received accordingly so that when replied to,
1655  * the answer is returned to the caller.
1656  */
1657 static int
1658 devfs_find_device_by_devid_worker(devfs_msg_t devfs_msg)
1659 {
1660 	cdev_t dev, dev1;
1661 	cdev_t found = NULL;
1662 
1663 	TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1664 		if (((dev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1665 			found = dev;
1666 			break;
1667 		}
1668 	}
1669 	devfs_msg->mdv_cdev = found;
1670 
1671 	return 0;
1672 }
1673 
1674 /*
1675  * Worker function that inserts a given alias into the
1676  * alias list, and propagates the alias to all mount
1677  * points.
1678  */
1679 static int
1680 devfs_make_alias_worker(struct devfs_alias *alias)
1681 {
1682 	struct devfs_alias *alias2;
1683 	size_t len = strlen(alias->name);
1684 	int found = 0;
1685 
1686 	TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1687 		if (len != alias2->namlen)
1688 			continue;
1689 
1690 		if (!memcmp(alias->name, alias2->name, len)) {
1691 			found = 1;
1692 			break;
1693 		}
1694 	}
1695 
1696 	if (!found) {
1697 		/*
1698 		 * The alias doesn't exist yet, so we add it to the alias list
1699 		 */
1700 		TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1701 		devfs_alias_propagate(alias, 0);
1702 		udev_event_attach(alias->dev_target, alias->name, 1);
1703 	} else {
1704 		devfs_debug(DEVFS_DEBUG_WARNING,
1705 			    "Warning: duplicate devfs_make_alias for %s\n",
1706 			    alias->name);
1707 		kfree(alias->name, M_DEVFS);
1708 		kfree(alias, M_DEVFS);
1709 	}
1710 
1711 	return 0;
1712 }
1713 
1714 /*
1715  * Worker function that delete a given alias from the
1716  * alias list, and propagates the removal to all mount
1717  * points.
1718  */
1719 static int
1720 devfs_destroy_alias_worker(struct devfs_alias *alias)
1721 {
1722 	struct devfs_alias *alias2;
1723 	int found = 0;
1724 
1725 	TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1726 		if (alias->dev_target != alias2->dev_target)
1727 			continue;
1728 
1729 		if (devfs_WildCmp(alias->name, alias2->name) == 0) {
1730 			found = 1;
1731 			break;
1732 		}
1733 	}
1734 
1735 	if (!found) {
1736 		devfs_debug(DEVFS_DEBUG_WARNING,
1737 		    "Warning: devfs_destroy_alias for inexistant alias: %s\n",
1738 		    alias->name);
1739 		kfree(alias->name, M_DEVFS);
1740 		kfree(alias, M_DEVFS);
1741 	} else {
1742 		/*
1743 		 * The alias exists, so we delete it from the alias list
1744 		 */
1745 		TAILQ_REMOVE(&devfs_alias_list, alias2, link);
1746 		devfs_alias_propagate(alias2, 1);
1747 		udev_event_detach(alias2->dev_target, alias2->name, 1);
1748 		kfree(alias->name, M_DEVFS);
1749 		kfree(alias, M_DEVFS);
1750 		kfree(alias2->name, M_DEVFS);
1751 		kfree(alias2, M_DEVFS);
1752 	}
1753 
1754 	return 0;
1755 }
1756 
1757 /*
1758  * Function that removes and frees all aliases.
1759  */
1760 static int
1761 devfs_alias_reap(void)
1762 {
1763 	struct devfs_alias *alias, *alias2;
1764 
1765 	TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1766 		TAILQ_REMOVE(&devfs_alias_list, alias, link);
1767 		kfree(alias->name, M_DEVFS);
1768 		kfree(alias, M_DEVFS);
1769 	}
1770 	return 0;
1771 }
1772 
1773 /*
1774  * Function that removes an alias matching a specific cdev and frees
1775  * it accordingly.
1776  */
1777 static int
1778 devfs_alias_remove(cdev_t dev)
1779 {
1780 	struct devfs_alias *alias, *alias2;
1781 
1782 	TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1783 		if (alias->dev_target == dev) {
1784 			TAILQ_REMOVE(&devfs_alias_list, alias, link);
1785 			udev_event_detach(alias->dev_target, alias->name, 1);
1786 			kfree(alias->name, M_DEVFS);
1787 			kfree(alias, M_DEVFS);
1788 		}
1789 	}
1790 	return 0;
1791 }
1792 
1793 /*
1794  * This function propagates an alias addition or removal to
1795  * all mount points.
1796  */
1797 static int
1798 devfs_alias_propagate(struct devfs_alias *alias, int remove)
1799 {
1800 	struct devfs_mnt_data *mnt;
1801 
1802 	TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1803 		if (remove) {
1804 			devfs_destroy_node(mnt->root_node, alias->name);
1805 		} else {
1806 			devfs_alias_apply(mnt->root_node, alias);
1807 		}
1808 	}
1809 	return 0;
1810 }
1811 
1812 /*
1813  * This function is a recursive function iterating through
1814  * all device nodes in the topology and, if applicable,
1815  * creating the relevant alias for a device node.
1816  */
1817 static int
1818 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1819 {
1820 	struct devfs_node *node1, *node2;
1821 
1822 	KKASSERT(alias != NULL);
1823 
1824 	if ((node->node_type == Nroot) || (node->node_type == Ndir)) {
1825 		if (node->nchildren > 2) {
1826 			TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1827 				devfs_alias_apply(node1, alias);
1828 			}
1829 		}
1830 	} else {
1831 		if (node->d_dev == alias->dev_target)
1832 			devfs_alias_create(alias->name, node, 0);
1833 	}
1834 	return 0;
1835 }
1836 
1837 /*
1838  * This function checks if any alias possibly is applicable
1839  * to the given node. If so, the alias is created.
1840  */
1841 static int
1842 devfs_alias_check_create(struct devfs_node *node)
1843 {
1844 	struct devfs_alias *alias;
1845 
1846 	TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1847 		if (node->d_dev == alias->dev_target)
1848 			devfs_alias_create(alias->name, node, 0);
1849 	}
1850 	return 0;
1851 }
1852 
1853 /*
1854  * This function creates an alias with a given name
1855  * linking to a given devfs node. It also increments
1856  * the link count on the target node.
1857  */
1858 int
1859 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based)
1860 {
1861 	struct mount *mp = target->mp;
1862 	struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1863 	struct devfs_node *linknode;
1864 	char *create_path = NULL;
1865 	char *name;
1866 	char *name_buf;
1867 	int result = 0;
1868 
1869 	KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1870 
1871 	name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1872 	devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1873 
1874 	if (create_path)
1875 		parent = devfs_resolve_or_create_path(parent, create_path, 1);
1876 
1877 
1878 	if (devfs_find_device_node_by_name(parent, name)) {
1879 		devfs_debug(DEVFS_DEBUG_WARNING,
1880 			    "Node already exists: %s "
1881 			    "(devfs_make_alias_worker)!\n",
1882 			    name);
1883 		result = 1;
1884 		goto done;
1885 	}
1886 
1887 	linknode = devfs_allocp(Nlink, name, parent, mp, NULL);
1888 	if (linknode == NULL) {
1889 		result = 1;
1890 		goto done;
1891 	}
1892 
1893 	linknode->link_target = target;
1894 	target->nlinks++;
1895 
1896 	if (rule_based)
1897 		linknode->flags |= DEVFS_RULE_CREATED;
1898 
1899 done:
1900 	kfree(name_buf, M_TEMP);
1901 	return (result);
1902 }
1903 
1904 /*
1905  * This function is called by the core and handles mount point
1906  * strings. It either calls the relevant worker (devfs_apply_
1907  * reset_rules_worker) on all mountpoints or only a specific
1908  * one.
1909  */
1910 static int
1911 devfs_apply_reset_rules_caller(char *mountto, int apply)
1912 {
1913 	struct devfs_mnt_data *mnt;
1914 
1915 	if (mountto[0] == '*') {
1916 		TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1917 			devfs_iterate_topology(mnt->root_node,
1918 					(apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1919 					NULL);
1920 		}
1921 	} else {
1922 		TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1923 			if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) {
1924 				devfs_iterate_topology(mnt->root_node,
1925 					(apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1926 					NULL);
1927 				break;
1928 			}
1929 		}
1930 	}
1931 
1932 	kfree(mountto, M_DEVFS);
1933 	return 0;
1934 }
1935 
1936 /*
1937  * This function calls a given callback function for
1938  * every dev node in the devfs dev list.
1939  */
1940 static int
1941 devfs_scan_callback_worker(devfs_scan_t *callback, void *arg)
1942 {
1943 	cdev_t dev, dev1;
1944 	struct devfs_alias *alias, *alias1;
1945 
1946 	TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1947 		callback(dev->si_name, dev, false, arg);
1948 	}
1949 	TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias1) {
1950 		callback(alias->name, alias->dev_target, true, arg);
1951 	}
1952 
1953 	return 0;
1954 }
1955 
1956 /*
1957  * This function tries to resolve a given directory, or if not
1958  * found and creation requested, creates the given directory.
1959  */
1960 static struct devfs_node *
1961 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1962 			    size_t name_len, int create)
1963 {
1964 	struct devfs_node *node, *found = NULL;
1965 
1966 	TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1967 		if (name_len != node->d_dir.d_namlen)
1968 			continue;
1969 
1970 		if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1971 			found = node;
1972 			break;
1973 		}
1974 	}
1975 
1976 	if ((found == NULL) && (create)) {
1977 		found = devfs_allocp(Ndir, dir_name, parent, parent->mp, NULL);
1978 	}
1979 
1980 	return found;
1981 }
1982 
1983 /*
1984  * This function tries to resolve a complete path. If creation is requested,
1985  * if a given part of the path cannot be resolved (because it doesn't exist),
1986  * it is created.
1987  */
1988 struct devfs_node *
1989 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1990 {
1991 	struct devfs_node *node = parent;
1992 	char *buf;
1993 	size_t idx = 0;
1994 
1995 	if (path == NULL)
1996 		return parent;
1997 
1998 	buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1999 
2000 	while (*path && idx < PATH_MAX - 1) {
2001 		if (*path != '/') {
2002 			buf[idx++] = *path;
2003 		} else {
2004 			buf[idx] = '\0';
2005 			node = devfs_resolve_or_create_dir(node, buf, idx, create);
2006 			if (node == NULL) {
2007 				kfree(buf, M_TEMP);
2008 				return NULL;
2009 			}
2010 			idx = 0;
2011 		}
2012 		++path;
2013 	}
2014 	buf[idx] = '\0';
2015 	node = devfs_resolve_or_create_dir(node, buf, idx, create);
2016 	kfree (buf, M_TEMP);
2017 	return (node);
2018 }
2019 
2020 /*
2021  * Takes a full path and strips it into a directory path and a name.
2022  * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
2023  * requires a working buffer with enough size to keep the whole
2024  * fullpath.
2025  */
2026 int
2027 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
2028 {
2029 	char *name = NULL;
2030 	char *path = NULL;
2031 	size_t len = strlen(fullpath) + 1;
2032 	int i;
2033 
2034 	KKASSERT((fullpath != NULL) && (buf != NULL));
2035 	KKASSERT((pathp != NULL) && (namep != NULL));
2036 
2037 	memcpy(buf, fullpath, len);
2038 
2039 	for (i = len-1; i>= 0; i--) {
2040 		if (buf[i] == '/') {
2041 			buf[i] = '\0';
2042 			name = &(buf[i+1]);
2043 			path = buf;
2044 			break;
2045 		}
2046 	}
2047 
2048 	*pathp = path;
2049 
2050 	if (name) {
2051 		*namep = name;
2052 	} else {
2053 		*namep = buf;
2054 	}
2055 
2056 	return 0;
2057 }
2058 
2059 /*
2060  * This function creates a new devfs node for a given device.  It can
2061  * handle a complete path as device name, and accordingly creates
2062  * the path and the final device node.
2063  *
2064  * The reference count on the passed dev remains unchanged.
2065  */
2066 struct devfs_node *
2067 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
2068 			 int *existsp, char *dev_name, char *path_fmt, ...)
2069 {
2070 	struct devfs_node *parent, *node = NULL;
2071 	char *path = NULL;
2072 	char *name;
2073 	char *name_buf;
2074 	__va_list ap;
2075 	int i, found;
2076 	char *create_path = NULL;
2077 	char *names = "pqrsPQRS";
2078 
2079 	name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
2080 
2081 	if (existsp)
2082 		*existsp = 0;
2083 
2084 	if (path_fmt != NULL) {
2085 		__va_start(ap, path_fmt);
2086 		kvasnprintf(&path, PATH_MAX, path_fmt, ap);
2087 		__va_end(ap);
2088 	}
2089 
2090 	parent = devfs_resolve_or_create_path(root, path, 1);
2091 	KKASSERT(parent);
2092 
2093 	devfs_resolve_name_path(
2094 			((dev_name == NULL) && (dev))?(dev->si_name):(dev_name),
2095 			name_buf, &create_path, &name);
2096 
2097 	if (create_path)
2098 		parent = devfs_resolve_or_create_path(parent, create_path, 1);
2099 
2100 
2101 	node = devfs_find_device_node_by_name(parent, name);
2102 	if (node) {
2103 		if (node->d_dev == dev) {
2104 			/*
2105 			 * Allow case where device caches dev after the
2106 			 * close and might desire to reuse it.
2107 			 */
2108 			if (existsp)
2109 				*existsp = 1;
2110 		} else {
2111 			devfs_debug(DEVFS_DEBUG_WARNING,
2112 				    "devfs_create_device_node: "
2113 				    "DEVICE %s ALREADY EXISTS!!! "
2114 				    "Ignoring creation request.\n",
2115 				    name);
2116 			node = NULL;
2117 		}
2118 		goto out;
2119 	}
2120 
2121 	node = devfs_allocp(Ndev, name, parent, parent->mp, dev);
2122 	nanotime(&parent->mtime);
2123 
2124 	/*
2125 	 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
2126 	 * directory
2127 	 */
2128 	if ((dev) && (strlen(dev->si_name) >= 4) &&
2129 			(!memcmp(dev->si_name, "ptm/", 4))) {
2130 		node->parent->flags |= DEVFS_HIDDEN;
2131 		node->flags |= DEVFS_HIDDEN;
2132 	}
2133 
2134 	/*
2135 	 * Ugly pty magic, to tag pty devices as such and hide them if needed.
2136 	 */
2137 	if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
2138 		node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
2139 
2140 	if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
2141 		found = 0;
2142 		for (i = 0; i < strlen(names); i++) {
2143 			if (name[3] == names[i]) {
2144 				found = 1;
2145 				break;
2146 			}
2147 		}
2148 		if (found)
2149 			node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
2150 	}
2151 
2152 out:
2153 	kfree(name_buf, M_TEMP);
2154 	kvasfree(&path);
2155 	return node;
2156 }
2157 
2158 /*
2159  * This function finds a given device node in the topology with a given
2160  * cdev.
2161  */
2162 void *
2163 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target)
2164 {
2165 	if ((node->node_type == Ndev) && (node->d_dev == target)) {
2166 		return node;
2167 	}
2168 
2169 	return NULL;
2170 }
2171 
2172 /*
2173  * This function finds a device node in the given parent directory by its
2174  * name and returns it.
2175  */
2176 struct devfs_node *
2177 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
2178 {
2179 	struct devfs_node *node, *found = NULL;
2180 	size_t len = strlen(target);
2181 
2182 	TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
2183 		if (len != node->d_dir.d_namlen)
2184 			continue;
2185 
2186 		if (!memcmp(node->d_dir.d_name, target, len)) {
2187 			found = node;
2188 			break;
2189 		}
2190 	}
2191 
2192 	return found;
2193 }
2194 
2195 static void *
2196 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop)
2197 {
2198 	struct vnode *vp = NULL;
2199 	ino_t target = *inop;
2200 
2201 	if (node->d_dir.d_ino == target) {
2202 		if (node->v_node) {
2203 			vp = node->v_node;
2204 			vget(vp, LK_EXCLUSIVE | LK_RETRY);
2205 			vn_unlock(vp);
2206 		} else {
2207 			devfs_allocv(&vp, node);
2208 			vn_unlock(vp);
2209 		}
2210 	}
2211 
2212 	return vp;
2213 }
2214 
2215 /*
2216  * This function takes a cdev and removes its devfs node in the
2217  * given topology.  The cdev remains intact.
2218  */
2219 int
2220 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
2221 {
2222 	KKASSERT(target != NULL);
2223 	return devfs_destroy_node(root, target->si_name);
2224 }
2225 
2226 /*
2227  * This function takes a path to a devfs node, resolves it and
2228  * removes the devfs node from the given topology.
2229  */
2230 int
2231 devfs_destroy_node(struct devfs_node *root, char *target)
2232 {
2233 	struct devfs_node *node, *parent;
2234 	char *name;
2235 	char *name_buf;
2236 	char *create_path = NULL;
2237 
2238 	KKASSERT(target);
2239 
2240 	name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
2241 	ksnprintf(name_buf, PATH_MAX, "%s", target);
2242 
2243 	devfs_resolve_name_path(target, name_buf, &create_path, &name);
2244 
2245 	if (create_path)
2246 		parent = devfs_resolve_or_create_path(root, create_path, 0);
2247 	else
2248 		parent = root;
2249 
2250 	if (parent == NULL) {
2251 		kfree(name_buf, M_TEMP);
2252 		return 1;
2253 	}
2254 
2255 	node = devfs_find_device_node_by_name(parent, name);
2256 
2257 	if (node) {
2258 		nanotime(&node->parent->mtime);
2259 		devfs_gc(node);
2260 	}
2261 
2262 	kfree(name_buf, M_TEMP);
2263 
2264 	return 0;
2265 }
2266 
2267 /*
2268  * Just set perms and ownership for given node.
2269  */
2270 int
2271 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid,
2272 		u_short mode, u_long flags)
2273 {
2274 	node->mode = mode;
2275 	node->uid = uid;
2276 	node->gid = gid;
2277 
2278 	return 0;
2279 }
2280 
2281 /*
2282  * Propagates a device attach/detach to all mount
2283  * points. Also takes care of automatic alias removal
2284  * for a deleted cdev.
2285  */
2286 static int
2287 devfs_propagate_dev(cdev_t dev, int attach)
2288 {
2289 	struct devfs_mnt_data *mnt;
2290 
2291 	TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
2292 		if (attach) {
2293 			/* Device is being attached */
2294 			devfs_create_device_node(mnt->root_node, dev,
2295 						 NULL, NULL, NULL);
2296 		} else {
2297 			/* Device is being detached */
2298 			devfs_alias_remove(dev);
2299 			devfs_destroy_device_node(mnt->root_node, dev);
2300 		}
2301 	}
2302 	return 0;
2303 }
2304 
2305 /*
2306  * devfs_clone either returns a basename from a complete name by
2307  * returning the length of the name without trailing digits, or,
2308  * if clone != 0, calls the device's clone handler to get a new
2309  * device, which in turn is returned in devp.
2310  *
2311  * Caller must hold a shared devfs_lock
2312  */
2313 cdev_t
2314 devfs_clone(cdev_t dev, const char *name, size_t len, int mode,
2315 		struct ucred *cred)
2316 {
2317 	int error;
2318 	struct devfs_clone_handler *chandler;
2319 	struct dev_clone_args ap;
2320 
2321 	TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
2322 		if (chandler->namlen != len)
2323 			continue;
2324 		if ((!memcmp(chandler->name, name, len)) &&
2325 		    (chandler->nhandler)) {
2326 			/*
2327 			 * We have to unlock across the config and the
2328 			 * callback to avoid deadlocking.  The device is
2329 			 * likely to obtain its own lock in the callback
2330 			 * and might then call into devfs.
2331 			 */
2332 			lockmgr(&devfs_lock, LK_RELEASE);
2333 			devfs_config();
2334 			ap.a_head.a_dev = dev;
2335 			ap.a_dev = NULL;
2336 			ap.a_name = name;
2337 			ap.a_namelen = len;
2338 			ap.a_mode = mode;
2339 			ap.a_cred = cred;
2340 			error = (chandler->nhandler)(&ap);
2341 			lockmgr(&devfs_lock, LK_SHARED);
2342 			if (error)
2343 				continue;
2344 
2345 			return ap.a_dev;
2346 		}
2347 	}
2348 
2349 	return NULL;
2350 }
2351 
2352 
2353 /*
2354  * Registers a new orphan in the orphan list.
2355  */
2356 void
2357 devfs_tracer_add_orphan(struct devfs_node *node)
2358 {
2359 	struct devfs_orphan *orphan;
2360 
2361 	KKASSERT(node);
2362 	orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2363 	orphan->node = node;
2364 
2365 	KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2366 	node->flags |= DEVFS_ORPHANED;
2367 	TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2368 }
2369 
2370 /*
2371  * Removes an orphan from the orphan list.
2372  */
2373 void
2374 devfs_tracer_del_orphan(struct devfs_node *node)
2375 {
2376 	struct devfs_orphan *orphan;
2377 
2378 	KKASSERT(node);
2379 
2380 	TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link)	{
2381 		if (orphan->node == node) {
2382 			node->flags &= ~DEVFS_ORPHANED;
2383 			TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2384 			kfree(orphan, M_DEVFS);
2385 			break;
2386 		}
2387 	}
2388 }
2389 
2390 /*
2391  * Counts the orphans in the orphan list, and if cleanup
2392  * is specified, also frees the orphan and removes it from
2393  * the list.
2394  */
2395 size_t
2396 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2397 {
2398 	struct devfs_orphan *orphan, *orphan2;
2399 	size_t count = 0;
2400 
2401 	TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2)	{
2402 		count++;
2403 		/*
2404 		 * If we are instructed to clean up, we do so.
2405 		 */
2406 		if (cleanup) {
2407 			TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2408 			orphan->node->flags &= ~DEVFS_ORPHANED;
2409 			devfs_freep(orphan->node);
2410 			kfree(orphan, M_DEVFS);
2411 		}
2412 	}
2413 
2414 	return count;
2415 }
2416 
2417 /*
2418  * Fetch an ino_t from the global d_ino by increasing it
2419  * while spinlocked.
2420  */
2421 static ino_t
2422 devfs_fetch_ino(void)
2423 {
2424 	ino_t	ret;
2425 
2426 	spin_lock(&ino_lock);
2427 	ret = d_ino++;
2428 	spin_unlock(&ino_lock);
2429 
2430 	return ret;
2431 }
2432 
2433 /*
2434  * Allocates a new cdev and initializes it's most basic
2435  * fields.
2436  */
2437 cdev_t
2438 devfs_new_cdev(struct dev_ops *ops, int minor, struct dev_ops *bops)
2439 {
2440 	cdev_t dev = sysref_alloc(&cdev_sysref_class);
2441 
2442 	sysref_activate(&dev->si_sysref);
2443 	reference_dev(dev);
2444 	bzero(dev, offsetof(struct cdev, si_sysref));
2445 
2446 	dev->si_uid = 0;
2447 	dev->si_gid = 0;
2448 	dev->si_perms = 0;
2449 	dev->si_drv1 = NULL;
2450 	dev->si_drv2 = NULL;
2451 	dev->si_lastread = 0;		/* time_uptime */
2452 	dev->si_lastwrite = 0;		/* time_uptime */
2453 
2454 	dev->si_dict = NULL;
2455 	dev->si_parent = NULL;
2456 	dev->si_ops = ops;
2457 	dev->si_flags = 0;
2458 	dev->si_uminor = minor;
2459 	dev->si_bops = bops;
2460 
2461 	/*
2462 	 * Since the disk subsystem is in the way, we need to
2463 	 * propagate the D_CANFREE from bops (and ops) to
2464 	 * si_flags.
2465 	 */
2466 	if (bops && (bops->head.flags & D_CANFREE)) {
2467 		dev->si_flags |= SI_CANFREE;
2468 	} else if (ops->head.flags & D_CANFREE) {
2469 		dev->si_flags |= SI_CANFREE;
2470 	}
2471 
2472 	/* If there is a backing device, we reference its ops */
2473 	if (bops == NULL)
2474 		bops = ops;
2475 	dev->si_inode = makeudev(devfs_reference_ops(bops), minor);
2476 	dev->si_umajor = umajor(dev->si_inode);
2477 
2478 	return dev;
2479 }
2480 
2481 static void
2482 devfs_cdev_terminate(cdev_t dev)
2483 {
2484 	/*
2485 	 * Make sure the node isn't linked anymore. Otherwise we've screwed
2486 	 * up somewhere, since normal devs are unlinked on the call to
2487 	 * destroy_dev and only-cdevs that have not been used for cloning
2488 	 * are not linked in the first place. only-cdevs used for cloning
2489 	 * will be linked in, too, and should only be destroyed via
2490 	 * destroy_dev, not destroy_only_dev, so we catch that problem, too.
2491 	 */
2492 	KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2493 
2494 	/* If there is a backing device, we release the backing device's ops */
2495 	devfs_release_ops((dev->si_bops)?(dev->si_bops):(dev->si_ops));
2496 
2497 	/* devfs_cdev_unlock() is not called, unlock ourselves */
2498 	lockmgr(&devfs_lock, LK_RELEASE);
2499 
2500 	/* Finally destroy the device */
2501 	sysref_put(&dev->si_sysref);
2502 }
2503 
2504 /*
2505  * Dummies for now (individual locks for MPSAFE)
2506  */
2507 static void
2508 devfs_cdev_lock(cdev_t dev)
2509 {
2510 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
2511 }
2512 
2513 static void
2514 devfs_cdev_unlock(cdev_t dev)
2515 {
2516 	lockmgr(&devfs_lock, LK_RELEASE);
2517 }
2518 
2519 static int
2520 devfs_detached_filter_eof(struct knote *kn, long hint)
2521 {
2522 	kn->kn_flags |= (EV_EOF | EV_NODATA);
2523 	return (1);
2524 }
2525 
2526 static void
2527 devfs_detached_filter_detach(struct knote *kn)
2528 {
2529 	cdev_t dev = (cdev_t)kn->kn_hook;
2530 
2531 	knote_remove(&dev->si_kqinfo.ki_note, kn);
2532 }
2533 
2534 static struct filterops devfs_detached_filterops =
2535 	{ FILTEROP_ISFD, NULL,
2536 	  devfs_detached_filter_detach,
2537 	  devfs_detached_filter_eof };
2538 
2539 /*
2540  * Delegates knote filter handling responsibility to devfs
2541  *
2542  * Any device that implements kqfilter event handling and could be detached
2543  * or shut down out from under the kevent subsystem must allow devfs to
2544  * assume responsibility for any knotes it may hold.
2545  */
2546 void
2547 devfs_assume_knotes(cdev_t dev, struct kqinfo *kqi)
2548 {
2549 	/*
2550 	 * Let kern/kern_event.c do the heavy lifting.
2551 	 */
2552 	knote_assume_knotes(kqi, &dev->si_kqinfo,
2553 			    &devfs_detached_filterops, (void *)dev);
2554 
2555 	/*
2556 	 * These should probably be activated individually, but doing so
2557 	 * would require refactoring kq's public in-kernel interface.
2558 	 */
2559 	KNOTE(&dev->si_kqinfo.ki_note, 0);
2560 }
2561 
2562 /*
2563  * Links a given cdev into the dev list.
2564  */
2565 int
2566 devfs_link_dev(cdev_t dev)
2567 {
2568 	KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2569 	dev->si_flags |= SI_DEVFS_LINKED;
2570 	TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2571 
2572 	return 0;
2573 }
2574 
2575 /*
2576  * Removes a given cdev from the dev list.  The caller is responsible for
2577  * releasing the reference on the device associated with the linkage.
2578  *
2579  * Returns EALREADY if the dev has already been unlinked.
2580  */
2581 static int
2582 devfs_unlink_dev(cdev_t dev)
2583 {
2584 	if ((dev->si_flags & SI_DEVFS_LINKED)) {
2585 		TAILQ_REMOVE(&devfs_dev_list, dev, link);
2586 		dev->si_flags &= ~SI_DEVFS_LINKED;
2587 		return (0);
2588 	}
2589 	return (EALREADY);
2590 }
2591 
2592 int
2593 devfs_node_is_accessible(struct devfs_node *node)
2594 {
2595 	if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2596 		return 1;
2597 	else
2598 		return 0;
2599 }
2600 
2601 static int
2602 devfs_reference_ops(struct dev_ops *ops)
2603 {
2604 	int unit;
2605 	struct devfs_dev_ops *found = NULL;
2606 	struct devfs_dev_ops *devops;
2607 
2608 	TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2609 		if (devops->ops == ops) {
2610 			found = devops;
2611 			break;
2612 		}
2613 	}
2614 
2615 	if (!found) {
2616 		found = kmalloc(sizeof(struct devfs_dev_ops), M_DEVFS, M_WAITOK);
2617 		found->ops = ops;
2618 		found->ref_count = 0;
2619 		TAILQ_INSERT_TAIL(&devfs_dev_ops_list, found, link);
2620 	}
2621 
2622 	KKASSERT(found);
2623 
2624 	if (found->ref_count == 0) {
2625 		found->id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255);
2626 		if (found->id == -1) {
2627 			/* Ran out of unique ids */
2628 			devfs_debug(DEVFS_DEBUG_WARNING,
2629 				    "devfs_reference_ops: WARNING: ran "
2630 				    "out of unique ids\n");
2631 		}
2632 	}
2633 	unit = found->id;
2634 	++found->ref_count;
2635 
2636 	return unit;
2637 }
2638 
2639 /*
2640  * devfs must be locked
2641  */
2642 static void
2643 devfs_release_ops(struct dev_ops *ops)
2644 {
2645 	struct devfs_dev_ops *found = NULL;
2646 	struct devfs_dev_ops *devops;
2647 
2648 	TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2649 		if (devops->ops == ops) {
2650 			found = devops;
2651 			break;
2652 		}
2653 	}
2654 
2655 	KKASSERT(found);
2656 
2657 	--found->ref_count;
2658 
2659 	if (found->ref_count == 0) {
2660 		TAILQ_REMOVE(&devfs_dev_ops_list, found, link);
2661 		lockmgr(&devfs_lock, LK_RELEASE);
2662 		devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), found->id);
2663 		lockmgr(&devfs_lock, LK_EXCLUSIVE);
2664 		kfree(found, M_DEVFS);
2665 	}
2666 }
2667 
2668 /*
2669  * Wait for asynchronous messages to complete in the devfs helper
2670  * thread, then return.  Do nothing if the helper thread is dead
2671  * or we are being indirectly called from the helper thread itself.
2672  */
2673 void
2674 devfs_config(void)
2675 {
2676 	devfs_msg_t msg;
2677 
2678 	if (devfs_run && curthread != td_core) {
2679 		msg = devfs_msg_get();
2680 		devfs_msg_send_sync(DEVFS_SYNC, msg);
2681 		devfs_msg_put(msg);
2682 	}
2683 }
2684 
2685 /*
2686  * Called on init of devfs; creates the objcaches and
2687  * spawns off the devfs core thread. Also initializes
2688  * locks.
2689  */
2690 static void
2691 devfs_init(void)
2692 {
2693 	devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2694 	/* Create objcaches for nodes, msgs and devs */
2695 	devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2696 					   NULL, NULL, NULL,
2697 					   objcache_malloc_alloc,
2698 					   objcache_malloc_free,
2699 					   &devfs_node_malloc_args );
2700 
2701 	devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2702 					  NULL, NULL, NULL,
2703 					  objcache_malloc_alloc,
2704 					  objcache_malloc_free,
2705 					  &devfs_msg_malloc_args );
2706 
2707 	devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2708 					  NULL, NULL, NULL,
2709 					  objcache_malloc_alloc,
2710 					  objcache_malloc_free,
2711 					  &devfs_dev_malloc_args );
2712 
2713 	devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2714 
2715 	/* Initialize the reply-only port which acts as a message drain */
2716 	lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2717 
2718 	/* Initialize *THE* devfs lock */
2719 	lockinit(&devfs_lock, "devfs_core lock", 0, LK_CANRECURSE);
2720 	lwkt_token_init(&devfs_token, "devfs_core");
2721 
2722 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
2723 	lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2724 		    0, -1, "devfs_msg_core");
2725 	while (devfs_run == 0)
2726 		lksleep(td_core, &devfs_lock, 0, "devfsc", 0);
2727 	lockmgr(&devfs_lock, LK_RELEASE);
2728 
2729 	devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2730 }
2731 
2732 /*
2733  * Called on unload of devfs; takes care of destroying the core
2734  * and the objcaches. Also removes aliases that are no longer needed.
2735  */
2736 static void
2737 devfs_uninit(void)
2738 {
2739 	devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2740 
2741 	devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2742 	while (devfs_run)
2743 		tsleep(td_core, 0, "devfsc", hz*10);
2744 	tsleep(td_core, 0, "devfsc", hz);
2745 
2746 	devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2747 
2748 	/* Destroy the objcaches */
2749 	objcache_destroy(devfs_msg_cache);
2750 	objcache_destroy(devfs_node_cache);
2751 	objcache_destroy(devfs_dev_cache);
2752 
2753 	devfs_alias_reap();
2754 }
2755 
2756 /*
2757  * This is a sysctl handler to assist userland devname(3) to
2758  * find the device name for a given udev.
2759  */
2760 static int
2761 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2762 {
2763 	dev_t 	udev;
2764 	cdev_t	found;
2765 	int		error;
2766 
2767 	if ((error = SYSCTL_IN(req, &udev, sizeof(dev_t))))
2768 		return (error);
2769 
2770 	devfs_debug(DEVFS_DEBUG_DEBUG,
2771 		    "devfs sysctl, received udev: %d\n", udev);
2772 
2773 	if (udev == NOUDEV)
2774 		return(EINVAL);
2775 
2776 	if ((found = devfs_find_device_by_devid(udev)) == NULL)
2777 		return(ENOENT);
2778 
2779 	return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2780 }
2781 
2782 
2783 SYSCTL_PROC(_kern, OID_AUTO, devname,
2784 	    CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_NOLOCK,
2785 	    NULL, 0, devfs_sysctl_devname_helper, "",
2786 	    "helper for devname(3)");
2787 
2788 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2789 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2790 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable,
2791 		0, "Enable DevFS debugging");
2792 
2793 SYSINIT(vfs_devfs_register, SI_SUB_DEVFS_CORE, SI_ORDER_FIRST,
2794 		devfs_init, NULL);
2795 SYSUNINIT(vfs_devfs_register, SI_SUB_DEVFS_CORE, SI_ORDER_ANY,
2796 		devfs_uninit, NULL);
2797 
2798 /*
2799  * WildCmp() - compare wild string to sane string
2800  *
2801  *	Returns 0 on success, -1 on failure.
2802  */
2803 static int
2804 wildCmp(const char **mary, int d, const char *w, const char *s)
2805 {
2806     int i;
2807 
2808     /*
2809      * skip fixed portion
2810      */
2811     for (;;) {
2812 	switch(*w) {
2813 	case '*':
2814 	    /*
2815 	     * optimize terminator
2816 	     */
2817 	    if (w[1] == 0)
2818 		return(0);
2819 	    if (w[1] != '?' && w[1] != '*') {
2820 		/*
2821 		 * optimize * followed by non-wild
2822 		 */
2823 		for (i = 0; s + i < mary[d]; ++i) {
2824 		    if (s[i] == w[1] && wildCmp(mary, d + 1, w + 1, s + i) == 0)
2825 			return(0);
2826 		}
2827 	    } else {
2828 		/*
2829 		 * less-optimal
2830 		 */
2831 		for (i = 0; s + i < mary[d]; ++i) {
2832 		    if (wildCmp(mary, d + 1, w + 1, s + i) == 0)
2833 			return(0);
2834 		}
2835 	    }
2836 	    mary[d] = s;
2837 	    return(-1);
2838 	case '?':
2839 	    if (*s == 0)
2840 		return(-1);
2841 	    ++w;
2842 	    ++s;
2843 	    break;
2844 	default:
2845 	    if (*w != *s)
2846 		return(-1);
2847 	    if (*w == 0)	/* terminator */
2848 		return(0);
2849 	    ++w;
2850 	    ++s;
2851 	    break;
2852 	}
2853     }
2854     /* not reached */
2855     return(-1);
2856 }
2857 
2858 
2859 /*
2860  * WildCaseCmp() - compare wild string to sane string, case insensitive
2861  *
2862  *	Returns 0 on success, -1 on failure.
2863  */
2864 static int
2865 wildCaseCmp(const char **mary, int d, const char *w, const char *s)
2866 {
2867     int i;
2868 
2869     /*
2870      * skip fixed portion
2871      */
2872     for (;;) {
2873 	switch(*w) {
2874 	case '*':
2875 	    /*
2876 	     * optimize terminator
2877 	     */
2878 	    if (w[1] == 0)
2879 		return(0);
2880 	    if (w[1] != '?' && w[1] != '*') {
2881 		/*
2882 		 * optimize * followed by non-wild
2883 		 */
2884 		for (i = 0; s + i < mary[d]; ++i) {
2885 		    if (s[i] == w[1] && wildCaseCmp(mary, d + 1, w + 1, s + i) == 0)
2886 			return(0);
2887 		}
2888 	    } else {
2889 		/*
2890 		 * less-optimal
2891 		 */
2892 		for (i = 0; s + i < mary[d]; ++i) {
2893 		    if (wildCaseCmp(mary, d + 1, w + 1, s + i) == 0)
2894 			return(0);
2895 		}
2896 	    }
2897 	    mary[d] = s;
2898 	    return(-1);
2899 	case '?':
2900 	    if (*s == 0)
2901 		return(-1);
2902 	    ++w;
2903 	    ++s;
2904 	    break;
2905 	default:
2906 	    if (*w != *s) {
2907 #define tolower(x)	((x >= 'A' && x <= 'Z')?(x+('a'-'A')):(x))
2908 		if (tolower(*w) != tolower(*s))
2909 		    return(-1);
2910 	    }
2911 	    if (*w == 0)	/* terminator */
2912 		return(0);
2913 	    ++w;
2914 	    ++s;
2915 	    break;
2916 	}
2917     }
2918     /* not reached */
2919     return(-1);
2920 }
2921 
2922 struct cdev_privdata {
2923 	void		*cdpd_data;
2924 	cdevpriv_dtr_t	cdpd_dtr;
2925 };
2926 
2927 int
2928 devfs_get_cdevpriv(struct file *fp, void **datap)
2929 {
2930 	int error;
2931 
2932 	if (fp == NULL)
2933 		return(EBADF);
2934 
2935 	spin_lock_shared(&fp->f_spin);
2936 	if (fp->f_data1 == NULL) {
2937 		*datap = NULL;
2938 		error = ENOENT;
2939 	} else {
2940 		struct cdev_privdata *p = fp->f_data1;
2941 
2942 		*datap = p->cdpd_data;
2943 		error = 0;
2944 	}
2945 	spin_unlock_shared(&fp->f_spin);
2946 
2947 	return (error);
2948 }
2949 
2950 int
2951 devfs_set_cdevpriv(struct file *fp, void *priv, cdevpriv_dtr_t dtr)
2952 {
2953 	struct cdev_privdata *p;
2954 	int error;
2955 
2956 	if (fp == NULL)
2957 		return (ENOENT);
2958 
2959 	p = kmalloc(sizeof(struct cdev_privdata), M_DEVFS, M_WAITOK);
2960 	p->cdpd_data = priv;
2961 	p->cdpd_dtr = dtr;
2962 
2963 	spin_lock(&fp->f_spin);
2964 	if (fp->f_data1 == NULL) {
2965 		fp->f_data1 = p;
2966 		error = 0;
2967 	} else {
2968 		error = EBUSY;
2969 	}
2970 	spin_unlock(&fp->f_spin);
2971 
2972 	if (error)
2973 		kfree(p, M_DEVFS);
2974 
2975 	return error;
2976 }
2977 
2978 void
2979 devfs_clear_cdevpriv(struct file *fp)
2980 {
2981 	struct cdev_privdata *p;
2982 
2983 	if (fp == NULL)
2984 		return;
2985 
2986 	spin_lock(&fp->f_spin);
2987 	p = fp->f_data1;
2988 	fp->f_data1 = NULL;
2989 	spin_unlock(&fp->f_spin);
2990 
2991 	if (p != NULL) {
2992 		p->cdpd_dtr(p->cdpd_data);
2993 		kfree(p, M_DEVFS);
2994 	}
2995 }
2996 
2997 int
2998 devfs_WildCmp(const char *w, const char *s)
2999 {
3000     int i;
3001     int c;
3002     int slen = strlen(s);
3003     const char **mary;
3004 
3005     for (i = c = 0; w[i]; ++i) {
3006 	if (w[i] == '*')
3007 	    ++c;
3008     }
3009     mary = kmalloc(sizeof(char *) * (c + 1), M_DEVFS, M_WAITOK);
3010     for (i = 0; i < c; ++i)
3011 	mary[i] = s + slen;
3012     i = wildCmp(mary, 0, w, s);
3013     kfree(mary, M_DEVFS);
3014     return(i);
3015 }
3016 
3017 int
3018 devfs_WildCaseCmp(const char *w, const char *s)
3019 {
3020     int i;
3021     int c;
3022     int slen = strlen(s);
3023     const char **mary;
3024 
3025     for (i = c = 0; w[i]; ++i) {
3026 	if (w[i] == '*')
3027 	    ++c;
3028     }
3029     mary = kmalloc(sizeof(char *) * (c + 1), M_DEVFS, M_WAITOK);
3030     for (i = 0; i < c; ++i)
3031 	mary[i] = s + slen;
3032     i = wildCaseCmp(mary, 0, w, s);
3033     kfree(mary, M_DEVFS);
3034     return(i);
3035 }
3036 
3037