xref: /dflybsd-src/sys/vfs/devfs/devfs_core.c (revision ae23dbdb346411139ff33e1c4e06d60a5b825344)
1 /*
2  * Copyright (c) 2009 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/mount.h>
38 #include <sys/vnode.h>
39 #include <sys/types.h>
40 #include <sys/lock.h>
41 #include <sys/msgport.h>
42 #include <sys/msgport2.h>
43 #include <sys/spinlock2.h>
44 #include <sys/sysctl.h>
45 #include <sys/ucred.h>
46 #include <sys/param.h>
47 #include <sys/sysref2.h>
48 #include <sys/systm.h>
49 #include <sys/devfs.h>
50 #include <sys/devfs_rules.h>
51 #include <sys/hotplug.h>
52 #include <sys/udev.h>
53 
54 MALLOC_DEFINE(M_DEVFS, "devfs", "Device File System (devfs) allocations");
55 DEVFS_DECLARE_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 	.mag_capacity = 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 static struct lwkt_port devfs_dispose_port;
102 static struct lwkt_port devfs_msg_port;
103 static struct thread 	*td_core;
104 
105 static struct spinlock  ino_lock;
106 static ino_t 	d_ino;
107 static int	devfs_debug_enable;
108 static int	devfs_run;
109 
110 static ino_t devfs_fetch_ino(void);
111 static int devfs_create_all_dev_worker(struct devfs_node *);
112 static int devfs_create_dev_worker(cdev_t, uid_t, gid_t, int);
113 static int devfs_destroy_dev_worker(cdev_t);
114 static int devfs_destroy_subnames_worker(char *);
115 static int devfs_destroy_dev_by_ops_worker(struct dev_ops *, int);
116 static int devfs_propagate_dev(cdev_t, int);
117 static int devfs_unlink_dev(cdev_t dev);
118 static void devfs_msg_exec(devfs_msg_t msg);
119 
120 static int devfs_chandler_add_worker(const char *, d_clone_t *);
121 static int devfs_chandler_del_worker(const char *);
122 
123 static void devfs_msg_autofree_reply(lwkt_port_t, lwkt_msg_t);
124 static void devfs_msg_core(void *);
125 
126 static int devfs_find_device_by_name_worker(devfs_msg_t);
127 static int devfs_find_device_by_udev_worker(devfs_msg_t);
128 
129 static int devfs_apply_reset_rules_caller(char *, int);
130 
131 static int devfs_scan_callback_worker(devfs_scan_t *, void *);
132 
133 static struct devfs_node *devfs_resolve_or_create_dir(struct devfs_node *,
134 		char *, size_t, int);
135 
136 static int devfs_make_alias_worker(struct devfs_alias *);
137 static int devfs_alias_remove(cdev_t);
138 static int devfs_alias_reap(void);
139 static int devfs_alias_propagate(struct devfs_alias *);
140 static int devfs_alias_apply(struct devfs_node *, struct devfs_alias *);
141 static int devfs_alias_check_create(struct devfs_node *);
142 
143 static int devfs_clr_subnames_flag_worker(char *, uint32_t);
144 static int devfs_destroy_subnames_without_flag_worker(char *, uint32_t);
145 
146 static void *devfs_reaperp_callback(struct devfs_node *, void *);
147 static void *devfs_gc_dirs_callback(struct devfs_node *, void *);
148 static void *devfs_gc_links_callback(struct devfs_node *, struct devfs_node *);
149 static void *
150 devfs_inode_to_vnode_worker_callback(struct devfs_node *, ino_t *);
151 
152 /* hotplug */
153 void (*devfs_node_added)(struct hotplug_device*) = NULL;
154 void (*devfs_node_removed)(struct hotplug_device*) = NULL;
155 
156 /*
157  * devfs_debug() is a SYSCTL and TUNABLE controlled debug output function
158  * using kvprintf
159  */
160 int
161 devfs_debug(int level, char *fmt, ...)
162 {
163 	__va_list ap;
164 
165 	__va_start(ap, fmt);
166 	if (level <= devfs_debug_enable)
167 		kvprintf(fmt, ap);
168 	__va_end(ap);
169 
170 	return 0;
171 }
172 
173 /*
174  * devfs_allocp() Allocates a new devfs node with the specified
175  * parameters. The node is also automatically linked into the topology
176  * if a parent is specified. It also calls the rule and alias stuff to
177  * be applied on the new node
178  */
179 struct devfs_node *
180 devfs_allocp(devfs_nodetype devfsnodetype, char *name,
181 	     struct devfs_node *parent, struct mount *mp, cdev_t dev)
182 {
183 	struct devfs_node *node = NULL;
184 	size_t namlen = strlen(name);
185 
186 	node = objcache_get(devfs_node_cache, M_WAITOK);
187 	bzero(node, sizeof(*node));
188 
189 	atomic_add_long(&(DEVFS_MNTDATA(mp)->leak_count), 1);
190 
191 	node->d_dev = NULL;
192 	node->nchildren = 1;
193 	node->mp = mp;
194 	node->d_dir.d_ino = devfs_fetch_ino();
195 
196 	/*
197 	 * Cookie jar for children. Leave 0 and 1 for '.' and '..' entries
198 	 * respectively.
199 	 */
200 	node->cookie_jar = 2;
201 
202 	/*
203 	 * Access Control members
204 	 */
205 	node->mode = DEVFS_DEFAULT_MODE;
206 	node->uid = DEVFS_DEFAULT_UID;
207 	node->gid = DEVFS_DEFAULT_GID;
208 
209 	switch (devfsnodetype) {
210 	case Proot:
211 		/*
212 		 * Ensure that we don't recycle the root vnode by marking it as
213 		 * linked into the topology.
214 		 */
215 		node->flags |= DEVFS_NODE_LINKED;
216 	case Pdir:
217 		TAILQ_INIT(DEVFS_DENODE_HEAD(node));
218 		node->d_dir.d_type = DT_DIR;
219 		node->nchildren = 2;
220 		break;
221 
222 	case Plink:
223 		node->d_dir.d_type = DT_LNK;
224 		break;
225 
226 	case Preg:
227 		node->d_dir.d_type = DT_REG;
228 		break;
229 
230 	case Pdev:
231 		if (dev != NULL) {
232 			node->d_dir.d_type = DT_CHR;
233 			node->d_dev = dev;
234 
235 			node->mode = dev->si_perms;
236 			node->uid = dev->si_uid;
237 			node->gid = dev->si_gid;
238 
239 			devfs_alias_check_create(node);
240 		}
241 		break;
242 
243 	default:
244 		panic("devfs_allocp: unknown node type");
245 	}
246 
247 	node->v_node = NULL;
248 	node->node_type = devfsnodetype;
249 
250 	/* Initialize the dirent structure of each devfs vnode */
251 	node->d_dir.d_namlen = namlen;
252 	node->d_dir.d_name = kmalloc(namlen+1, M_DEVFS, M_WAITOK);
253 	memcpy(node->d_dir.d_name, name, namlen);
254 	node->d_dir.d_name[namlen] = '\0';
255 
256 	/* Initialize the parent node element */
257 	node->parent = parent;
258 
259 	/* Apply rules */
260 	devfs_rule_check_apply(node, NULL);
261 
262 	/* Initialize *time members */
263 	nanotime(&node->atime);
264 	node->mtime = node->ctime = node->atime;
265 
266 	/*
267 	 * Associate with parent as last step, clean out namecache
268 	 * reference.
269 	 */
270 	if ((parent != NULL) &&
271 	    ((parent->node_type == Proot) || (parent->node_type == Pdir))) {
272 		parent->nchildren++;
273 		node->cookie = parent->cookie_jar++;
274 		node->flags |= DEVFS_NODE_LINKED;
275 		TAILQ_INSERT_TAIL(DEVFS_DENODE_HEAD(parent), node, link);
276 
277 		/* This forces negative namecache lookups to clear */
278 		++mp->mnt_namecache_gen;
279 	}
280 
281 	++DEVFS_MNTDATA(mp)->file_count;
282 
283 	return node;
284 }
285 
286 /*
287  * devfs_allocv() allocates a new vnode based on a devfs node.
288  */
289 int
290 devfs_allocv(struct vnode **vpp, struct devfs_node *node)
291 {
292 	struct vnode *vp;
293 	int error = 0;
294 
295 	KKASSERT(node);
296 
297 try_again:
298 	while ((vp = node->v_node) != NULL) {
299 		error = vget(vp, LK_EXCLUSIVE);
300 		if (error != ENOENT) {
301 			*vpp = vp;
302 			goto out;
303 		}
304 	}
305 
306 	if ((error = getnewvnode(VT_DEVFS, node->mp, vpp, 0, 0)) != 0)
307 		goto out;
308 
309 	vp = *vpp;
310 
311 	if (node->v_node != NULL) {
312 		vp->v_type = VBAD;
313 		vx_put(vp);
314 		goto try_again;
315 	}
316 
317 	vp->v_data = node;
318 	node->v_node = vp;
319 
320 	switch (node->node_type) {
321 	case Proot:
322 		vsetflags(vp, VROOT);
323 		/* fall through */
324 	case Pdir:
325 		vp->v_type = VDIR;
326 		break;
327 
328 	case Plink:
329 		vp->v_type = VLNK;
330 		break;
331 
332 	case Preg:
333 		vp->v_type = VREG;
334 		break;
335 
336 	case Pdev:
337 		vp->v_type = VCHR;
338 		KKASSERT(node->d_dev);
339 
340 		vp->v_uminor = node->d_dev->si_uminor;
341 		vp->v_umajor = 0;
342 
343 		v_associate_rdev(vp, node->d_dev);
344 		vp->v_ops = &node->mp->mnt_vn_spec_ops;
345 		break;
346 
347 	default:
348 		panic("devfs_allocv: unknown node type");
349 	}
350 
351 out:
352 	return error;
353 }
354 
355 /*
356  * devfs_allocvp allocates both a devfs node (with the given settings) and a vnode
357  * based on the newly created devfs node.
358  */
359 int
360 devfs_allocvp(struct mount *mp, struct vnode **vpp, devfs_nodetype devfsnodetype,
361 		char *name, struct devfs_node *parent, cdev_t dev)
362 {
363 	struct devfs_node *node;
364 
365 	node = devfs_allocp(devfsnodetype, name, parent, mp, dev);
366 
367 	if (node != NULL)
368 		devfs_allocv(vpp, node);
369 	else
370 		*vpp = NULL;
371 
372 	return 0;
373 }
374 
375 /*
376  * Destroy the devfs_node.  The node must be unlinked from the topology.
377  *
378  * This function will also destroy any vnode association with the node
379  * and device.
380  *
381  * The cdev_t itself remains intact.
382  */
383 int
384 devfs_freep(struct devfs_node *node)
385 {
386 	struct vnode *vp;
387 
388 	KKASSERT(node);
389 	KKASSERT(((node->flags & DEVFS_NODE_LINKED) == 0) ||
390 		 (node->node_type == Proot));
391 
392 	/*
393 	 * Protect against double frees
394 	 */
395 	KKASSERT((node->flags & DEVFS_DESTROYED) == 0);
396 	node->flags |= DEVFS_DESTROYED;
397 
398 	/*
399 	 * Avoid deadlocks between devfs_lock and the vnode lock when
400 	 * disassociating the vnode (stress2 pty vs ls -la /dev/pts).
401 	 *
402 	 * This also prevents the vnode reclaim code from double-freeing
403 	 * the node.  The vget() is required to safely modified the vp
404 	 * and cycle the refs to terminate an inactive vp.
405 	 */
406 	lockmgr(&devfs_lock, LK_RELEASE);
407 
408 	while ((vp = node->v_node) != NULL) {
409 		if (vget(vp, LK_EXCLUSIVE | LK_RETRY) != 0)
410 			break;
411 		v_release_rdev(vp);
412 		vp->v_data = NULL;
413 		node->v_node = NULL;
414 		cache_inval_vp(vp, CINV_DESTROY);
415 		vput(vp);
416 	}
417 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
418 
419 	/*
420 	 * Remaining cleanup
421 	 */
422 	atomic_subtract_long(&(DEVFS_MNTDATA(node->mp)->leak_count), 1);
423 	if (node->symlink_name)	{
424 		kfree(node->symlink_name, M_DEVFS);
425 		node->symlink_name = NULL;
426 	}
427 
428 	/*
429 	 * Remove the node from the orphan list if it is still on it.
430 	 */
431 	if (node->flags & DEVFS_ORPHANED)
432 		devfs_tracer_del_orphan(node);
433 
434 	if (node->d_dir.d_name) {
435 		kfree(node->d_dir.d_name, M_DEVFS);
436 		node->d_dir.d_name = NULL;
437 	}
438 	--DEVFS_MNTDATA(node->mp)->file_count;
439 	objcache_put(devfs_node_cache, node);
440 
441 	return 0;
442 }
443 
444 /*
445  * Unlink the devfs node from the topology and add it to the orphan list.
446  * The node will later be destroyed by freep.
447  *
448  * Any vnode association, including the v_rdev and v_data, remains intact
449  * until the freep.
450  */
451 int
452 devfs_unlinkp(struct devfs_node *node)
453 {
454 	struct devfs_node *parent;
455 	struct hotplug_device *hpdev;
456 	KKASSERT(node);
457 
458 	/*
459 	 * Add the node to the orphan list, so it is referenced somewhere, to
460 	 * so we don't leak it.
461 	 */
462 	devfs_tracer_add_orphan(node);
463 
464 	parent = node->parent;
465 
466 	/*
467 	 * If the parent is known we can unlink the node out of the topology
468 	 */
469 	if (parent)	{
470 		TAILQ_REMOVE(DEVFS_DENODE_HEAD(parent), node, link);
471 		parent->nchildren--;
472 		node->flags &= ~DEVFS_NODE_LINKED;
473 	}
474 	/* hotplug handler */
475 	if(devfs_node_removed) {
476 		hpdev = kmalloc(sizeof(struct hotplug_device), M_TEMP, M_WAITOK);
477 		hpdev->dev = node->d_dev;
478 		if(hpdev->dev)
479 			hpdev->name = node->d_dev->si_name;
480 		devfs_node_removed(hpdev);
481 		kfree(hpdev, M_TEMP);
482 	}
483 	node->parent = NULL;
484 	return 0;
485 }
486 
487 void *
488 devfs_iterate_topology(struct devfs_node *node,
489 		devfs_iterate_callback_t *callback, void *arg1)
490 {
491 	struct devfs_node *node1, *node2;
492 	void *ret = NULL;
493 
494 	if ((node->node_type == Proot) || (node->node_type == Pdir)) {
495 		if (node->nchildren > 2) {
496 			TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node),
497 							link, node2) {
498 				if ((ret = devfs_iterate_topology(node1, callback, arg1)))
499 					return ret;
500 			}
501 		}
502 	}
503 
504 	ret = callback(node, arg1);
505 	return ret;
506 }
507 
508 /*
509  * devfs_reaperp() is a recursive function that iterates through all the
510  * topology, unlinking and freeing all devfs nodes.
511  */
512 static void *
513 devfs_reaperp_callback(struct devfs_node *node, void *unused)
514 {
515 	devfs_unlinkp(node);
516 	devfs_freep(node);
517 
518 	return NULL;
519 }
520 
521 static void *
522 devfs_gc_dirs_callback(struct devfs_node *node, void *unused)
523 {
524 	if (node->node_type == Pdir) {
525 		if ((node->nchildren == 2) &&
526 		    !(node->flags & DEVFS_USER_CREATED)) {
527 			devfs_unlinkp(node);
528 			devfs_freep(node);
529 		}
530 	}
531 
532 	return NULL;
533 }
534 
535 static void *
536 devfs_gc_links_callback(struct devfs_node *node, struct devfs_node *target)
537 {
538 	if ((node->node_type == Plink) && (node->link_target == target)) {
539 		devfs_unlinkp(node);
540 		devfs_freep(node);
541 	}
542 
543 	return NULL;
544 }
545 
546 /*
547  * devfs_gc() is devfs garbage collector. It takes care of unlinking and
548  * freeing a node, but also removes empty directories and links that link
549  * via devfs auto-link mechanism to the node being deleted.
550  */
551 int
552 devfs_gc(struct devfs_node *node)
553 {
554 	struct devfs_node *root_node = DEVFS_MNTDATA(node->mp)->root_node;
555 
556 	if (node->nlinks > 0)
557 		devfs_iterate_topology(root_node,
558 				(devfs_iterate_callback_t *)devfs_gc_links_callback, node);
559 
560 	devfs_unlinkp(node);
561 	devfs_iterate_topology(root_node,
562 			(devfs_iterate_callback_t *)devfs_gc_dirs_callback, NULL);
563 
564 	devfs_freep(node);
565 
566 	return 0;
567 }
568 
569 /*
570  * devfs_create_dev() is the asynchronous entry point for device creation.
571  * It just sends a message with the relevant details to the devfs core.
572  *
573  * This function will reference the passed device.  The reference is owned
574  * by devfs and represents all of the device's node associations.
575  */
576 int
577 devfs_create_dev(cdev_t dev, uid_t uid, gid_t gid, int perms)
578 {
579 	reference_dev(dev);
580 	devfs_msg_send_dev(DEVFS_DEVICE_CREATE, dev, uid, gid, perms);
581 
582 	return 0;
583 }
584 
585 /*
586  * devfs_destroy_dev() is the asynchronous entry point for device destruction.
587  * It just sends a message with the relevant details to the devfs core.
588  */
589 int
590 devfs_destroy_dev(cdev_t dev)
591 {
592 	devfs_msg_send_dev(DEVFS_DEVICE_DESTROY, dev, 0, 0, 0);
593 	return 0;
594 }
595 
596 /*
597  * devfs_mount_add() is the synchronous entry point for adding a new devfs
598  * mount.  It sends a synchronous message with the relevant details to the
599  * devfs core.
600  */
601 int
602 devfs_mount_add(struct devfs_mnt_data *mnt)
603 {
604 	devfs_msg_t msg;
605 
606 	msg = devfs_msg_get();
607 	msg->mdv_mnt = mnt;
608 	msg = devfs_msg_send_sync(DEVFS_MOUNT_ADD, msg);
609 	devfs_msg_put(msg);
610 
611 	return 0;
612 }
613 
614 /*
615  * devfs_mount_del() is the synchronous entry point for removing a devfs mount.
616  * It sends a synchronous message with the relevant details to the devfs core.
617  */
618 int
619 devfs_mount_del(struct devfs_mnt_data *mnt)
620 {
621 	devfs_msg_t msg;
622 
623 	msg = devfs_msg_get();
624 	msg->mdv_mnt = mnt;
625 	msg = devfs_msg_send_sync(DEVFS_MOUNT_DEL, msg);
626 	devfs_msg_put(msg);
627 
628 	return 0;
629 }
630 
631 /*
632  * devfs_destroy_subnames() is the synchronous entry point for device
633  * destruction by subname. It just sends a message with the relevant details to
634  * the devfs core.
635  */
636 int
637 devfs_destroy_subnames(char *name)
638 {
639 	devfs_msg_t msg;
640 
641 	msg = devfs_msg_get();
642 	msg->mdv_load = name;
643 	msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES, msg);
644 	devfs_msg_put(msg);
645 	return 0;
646 }
647 
648 int
649 devfs_clr_subnames_flag(char *name, uint32_t flag)
650 {
651 	devfs_msg_t msg;
652 
653 	msg = devfs_msg_get();
654 	msg->mdv_flags.name = name;
655 	msg->mdv_flags.flag = flag;
656 	msg = devfs_msg_send_sync(DEVFS_CLR_SUBNAMES_FLAG, msg);
657 	devfs_msg_put(msg);
658 
659 	return 0;
660 }
661 
662 int
663 devfs_destroy_subnames_without_flag(char *name, uint32_t flag)
664 {
665 	devfs_msg_t msg;
666 
667 	msg = devfs_msg_get();
668 	msg->mdv_flags.name = name;
669 	msg->mdv_flags.flag = flag;
670 	msg = devfs_msg_send_sync(DEVFS_DESTROY_SUBNAMES_WO_FLAG, msg);
671 	devfs_msg_put(msg);
672 
673 	return 0;
674 }
675 
676 /*
677  * devfs_create_all_dev is the asynchronous entry point to trigger device
678  * node creation.  It just sends a message with the relevant details to
679  * the devfs core.
680  */
681 int
682 devfs_create_all_dev(struct devfs_node *root)
683 {
684 	devfs_msg_send_generic(DEVFS_CREATE_ALL_DEV, root);
685 	return 0;
686 }
687 
688 /*
689  * devfs_destroy_dev_by_ops is the asynchronous entry point to destroy all
690  * devices with a specific set of dev_ops and minor.  It just sends a
691  * message with the relevant details to the devfs core.
692  */
693 int
694 devfs_destroy_dev_by_ops(struct dev_ops *ops, int minor)
695 {
696 	devfs_msg_send_ops(DEVFS_DESTROY_DEV_BY_OPS, ops, minor);
697 	return 0;
698 }
699 
700 /*
701  * devfs_clone_handler_add is the synchronous entry point to add a new
702  * clone handler.  It just sends a message with the relevant details to
703  * the devfs core.
704  */
705 int
706 devfs_clone_handler_add(const char *name, d_clone_t *nhandler)
707 {
708 	devfs_msg_t msg;
709 
710 	msg = devfs_msg_get();
711 	msg->mdv_chandler.name = name;
712 	msg->mdv_chandler.nhandler = nhandler;
713 	msg = devfs_msg_send_sync(DEVFS_CHANDLER_ADD, msg);
714 	devfs_msg_put(msg);
715 	return 0;
716 }
717 
718 /*
719  * devfs_clone_handler_del is the synchronous entry point to remove a
720  * clone handler.  It just sends a message with the relevant details to
721  * the devfs core.
722  */
723 int
724 devfs_clone_handler_del(const char *name)
725 {
726 	devfs_msg_t msg;
727 
728 	msg = devfs_msg_get();
729 	msg->mdv_chandler.name = name;
730 	msg->mdv_chandler.nhandler = NULL;
731 	msg = devfs_msg_send_sync(DEVFS_CHANDLER_DEL, msg);
732 	devfs_msg_put(msg);
733 	return 0;
734 }
735 
736 /*
737  * devfs_find_device_by_name is the synchronous entry point to find a
738  * device given its name.  It sends a synchronous message with the
739  * relevant details to the devfs core and returns the answer.
740  */
741 cdev_t
742 devfs_find_device_by_name(const char *fmt, ...)
743 {
744 	cdev_t found = NULL;
745 	devfs_msg_t msg;
746 	char *target;
747 	__va_list ap;
748 
749 	if (fmt == NULL)
750 		return NULL;
751 
752 	__va_start(ap, fmt);
753 	kvasnrprintf(&target, PATH_MAX, 10, fmt, ap);
754 	__va_end(ap);
755 
756 	msg = devfs_msg_get();
757 	msg->mdv_name = target;
758 	msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_NAME, msg);
759 	found = msg->mdv_cdev;
760 	devfs_msg_put(msg);
761 	kvasfree(&target);
762 
763 	return found;
764 }
765 
766 /*
767  * devfs_find_device_by_udev is the synchronous entry point to find a
768  * device given its udev number.  It sends a synchronous message with
769  * the relevant details to the devfs core and returns the answer.
770  */
771 cdev_t
772 devfs_find_device_by_udev(udev_t udev)
773 {
774 	cdev_t found = NULL;
775 	devfs_msg_t msg;
776 
777 	msg = devfs_msg_get();
778 	msg->mdv_udev = udev;
779 	msg = devfs_msg_send_sync(DEVFS_FIND_DEVICE_BY_UDEV, msg);
780 	found = msg->mdv_cdev;
781 	devfs_msg_put(msg);
782 
783 	devfs_debug(DEVFS_DEBUG_DEBUG,
784 		    "devfs_find_device_by_udev found? %s  -end:3-\n",
785 		    ((found) ? found->si_name:"NO"));
786 	return found;
787 }
788 
789 struct vnode *
790 devfs_inode_to_vnode(struct mount *mp, ino_t target)
791 {
792 	struct vnode *vp = NULL;
793 	devfs_msg_t msg;
794 
795 	if (mp == NULL)
796 		return NULL;
797 
798 	msg = devfs_msg_get();
799 	msg->mdv_ino.mp = mp;
800 	msg->mdv_ino.ino = target;
801 	msg = devfs_msg_send_sync(DEVFS_INODE_TO_VNODE, msg);
802 	vp = msg->mdv_ino.vp;
803 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
804 	devfs_msg_put(msg);
805 
806 	return vp;
807 }
808 
809 /*
810  * devfs_make_alias is the asynchronous entry point to register an alias
811  * for a device.  It just sends a message with the relevant details to the
812  * devfs core.
813  */
814 int
815 devfs_make_alias(const char *name, cdev_t dev_target)
816 {
817 	struct devfs_alias *alias;
818 	size_t len;
819 
820 	len = strlen(name);
821 
822 	alias = kmalloc(sizeof(struct devfs_alias), M_DEVFS, M_WAITOK);
823 	alias->name = kstrdup(name, M_DEVFS);
824 	alias->namlen = len;
825 	alias->dev_target = dev_target;
826 
827 	devfs_msg_send_generic(DEVFS_MAKE_ALIAS, alias);
828 	return 0;
829 }
830 
831 /*
832  * devfs_apply_rules is the asynchronous entry point to trigger application
833  * of all rules.  It just sends a message with the relevant details to the
834  * devfs core.
835  */
836 int
837 devfs_apply_rules(char *mntto)
838 {
839 	char *new_name;
840 
841 	new_name = kstrdup(mntto, M_DEVFS);
842 	devfs_msg_send_name(DEVFS_APPLY_RULES, new_name);
843 
844 	return 0;
845 }
846 
847 /*
848  * devfs_reset_rules is the asynchronous entry point to trigger reset of all
849  * rules. It just sends a message with the relevant details to the devfs core.
850  */
851 int
852 devfs_reset_rules(char *mntto)
853 {
854 	char *new_name;
855 
856 	new_name = kstrdup(mntto, M_DEVFS);
857 	devfs_msg_send_name(DEVFS_RESET_RULES, new_name);
858 
859 	return 0;
860 }
861 
862 
863 /*
864  * devfs_scan_callback is the asynchronous entry point to call a callback
865  * on all cdevs.
866  * It just sends a message with the relevant details to the devfs core.
867  */
868 int
869 devfs_scan_callback(devfs_scan_t *callback, void *arg)
870 {
871 	devfs_msg_t msg;
872 
873 	KKASSERT(sizeof(callback) == sizeof(void *));
874 
875 	msg = devfs_msg_get();
876 	msg->mdv_load = callback;
877 	msg->mdv_load2 = arg;
878 	msg = devfs_msg_send_sync(DEVFS_SCAN_CALLBACK, msg);
879 	devfs_msg_put(msg);
880 
881 	return 0;
882 }
883 
884 
885 /*
886  * Acts as a message drain. Any message that is replied to here gets destroyed
887  * and the memory freed.
888  */
889 static void
890 devfs_msg_autofree_reply(lwkt_port_t port, lwkt_msg_t msg)
891 {
892 	devfs_msg_put((devfs_msg_t)msg);
893 }
894 
895 /*
896  * devfs_msg_get allocates a new devfs msg and returns it.
897  */
898 devfs_msg_t
899 devfs_msg_get(void)
900 {
901 	return objcache_get(devfs_msg_cache, M_WAITOK);
902 }
903 
904 /*
905  * devfs_msg_put deallocates a given devfs msg.
906  */
907 int
908 devfs_msg_put(devfs_msg_t msg)
909 {
910 	objcache_put(devfs_msg_cache, msg);
911 	return 0;
912 }
913 
914 /*
915  * devfs_msg_send is the generic asynchronous message sending facility
916  * for devfs. By default the reply port is the automatic disposal port.
917  *
918  * If the current thread is the devfs_msg_port thread we execute the
919  * operation synchronously.
920  */
921 void
922 devfs_msg_send(uint32_t cmd, devfs_msg_t devfs_msg)
923 {
924 	lwkt_port_t port = &devfs_msg_port;
925 
926 	lwkt_initmsg(&devfs_msg->hdr, &devfs_dispose_port, 0);
927 
928 	devfs_msg->hdr.u.ms_result = cmd;
929 
930 	if (port->mpu_td == curthread) {
931 		devfs_msg_exec(devfs_msg);
932 		lwkt_replymsg(&devfs_msg->hdr, 0);
933 	} else {
934 		lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
935 	}
936 }
937 
938 /*
939  * devfs_msg_send_sync is the generic synchronous message sending
940  * facility for devfs. It initializes a local reply port and waits
941  * for the core's answer. This answer is then returned.
942  */
943 devfs_msg_t
944 devfs_msg_send_sync(uint32_t cmd, devfs_msg_t devfs_msg)
945 {
946 	struct lwkt_port rep_port;
947 	devfs_msg_t	msg_incoming;
948 	lwkt_port_t port = &devfs_msg_port;
949 
950 	lwkt_initport_thread(&rep_port, curthread);
951 	lwkt_initmsg(&devfs_msg->hdr, &rep_port, 0);
952 
953 	devfs_msg->hdr.u.ms_result = cmd;
954 
955 	lwkt_sendmsg(port, (lwkt_msg_t)devfs_msg);
956 	msg_incoming = lwkt_waitport(&rep_port, 0);
957 
958 	return msg_incoming;
959 }
960 
961 /*
962  * sends a message with a generic argument.
963  */
964 void
965 devfs_msg_send_generic(uint32_t cmd, void *load)
966 {
967 	devfs_msg_t devfs_msg = devfs_msg_get();
968 
969 	devfs_msg->mdv_load = load;
970 	devfs_msg_send(cmd, devfs_msg);
971 }
972 
973 /*
974  * sends a message with a name argument.
975  */
976 void
977 devfs_msg_send_name(uint32_t cmd, char *name)
978 {
979 	devfs_msg_t devfs_msg = devfs_msg_get();
980 
981 	devfs_msg->mdv_name = name;
982 	devfs_msg_send(cmd, devfs_msg);
983 }
984 
985 /*
986  * sends a message with a mount argument.
987  */
988 void
989 devfs_msg_send_mount(uint32_t cmd, struct devfs_mnt_data *mnt)
990 {
991 	devfs_msg_t devfs_msg = devfs_msg_get();
992 
993 	devfs_msg->mdv_mnt = mnt;
994 	devfs_msg_send(cmd, devfs_msg);
995 }
996 
997 /*
998  * sends a message with an ops argument.
999  */
1000 void
1001 devfs_msg_send_ops(uint32_t cmd, struct dev_ops *ops, int minor)
1002 {
1003 	devfs_msg_t devfs_msg = devfs_msg_get();
1004 
1005 	devfs_msg->mdv_ops.ops = ops;
1006 	devfs_msg->mdv_ops.minor = minor;
1007 	devfs_msg_send(cmd, devfs_msg);
1008 }
1009 
1010 /*
1011  * sends a message with a clone handler argument.
1012  */
1013 void
1014 devfs_msg_send_chandler(uint32_t cmd, char *name, d_clone_t handler)
1015 {
1016 	devfs_msg_t devfs_msg = devfs_msg_get();
1017 
1018 	devfs_msg->mdv_chandler.name = name;
1019 	devfs_msg->mdv_chandler.nhandler = handler;
1020 	devfs_msg_send(cmd, devfs_msg);
1021 }
1022 
1023 /*
1024  * sends a message with a device argument.
1025  */
1026 void
1027 devfs_msg_send_dev(uint32_t cmd, cdev_t dev, uid_t uid, gid_t gid, int perms)
1028 {
1029 	devfs_msg_t devfs_msg = devfs_msg_get();
1030 
1031 	devfs_msg->mdv_dev.dev = dev;
1032 	devfs_msg->mdv_dev.uid = uid;
1033 	devfs_msg->mdv_dev.gid = gid;
1034 	devfs_msg->mdv_dev.perms = perms;
1035 
1036 	devfs_msg_send(cmd, devfs_msg);
1037 }
1038 
1039 /*
1040  * sends a message with a link argument.
1041  */
1042 void
1043 devfs_msg_send_link(uint32_t cmd, char *name, char *target, struct mount *mp)
1044 {
1045 	devfs_msg_t devfs_msg = devfs_msg_get();
1046 
1047 	devfs_msg->mdv_link.name = name;
1048 	devfs_msg->mdv_link.target = target;
1049 	devfs_msg->mdv_link.mp = mp;
1050 	devfs_msg_send(cmd, devfs_msg);
1051 }
1052 
1053 /*
1054  * devfs_msg_core is the main devfs thread. It handles all incoming messages
1055  * and calls the relevant worker functions. By using messages it's assured
1056  * that events occur in the correct order.
1057  */
1058 static void
1059 devfs_msg_core(void *arg)
1060 {
1061 	devfs_msg_t msg;
1062 
1063 	devfs_run = 1;
1064 	lwkt_initport_thread(&devfs_msg_port, curthread);
1065 	wakeup(td_core);
1066 
1067 	while (devfs_run) {
1068 		msg = (devfs_msg_t)lwkt_waitport(&devfs_msg_port, 0);
1069 		devfs_debug(DEVFS_DEBUG_DEBUG,
1070 				"devfs_msg_core, new msg: %x\n",
1071 				(unsigned int)msg->hdr.u.ms_result);
1072 		devfs_msg_exec(msg);
1073 		lwkt_replymsg(&msg->hdr, 0);
1074 	}
1075 	wakeup(td_core);
1076 	lwkt_exit();
1077 }
1078 
1079 static void
1080 devfs_msg_exec(devfs_msg_t msg)
1081 {
1082 	struct devfs_mnt_data *mnt;
1083 	struct devfs_node *node;
1084 	cdev_t	dev;
1085 
1086 	/*
1087 	 * Acquire the devfs lock to ensure safety of all called functions
1088 	 */
1089 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
1090 
1091 	switch (msg->hdr.u.ms_result) {
1092 	case DEVFS_DEVICE_CREATE:
1093 		dev = msg->mdv_dev.dev;
1094 		devfs_create_dev_worker(dev,
1095 					msg->mdv_dev.uid,
1096 					msg->mdv_dev.gid,
1097 					msg->mdv_dev.perms);
1098 		break;
1099 	case DEVFS_DEVICE_DESTROY:
1100 		dev = msg->mdv_dev.dev;
1101 		devfs_destroy_dev_worker(dev);
1102 		break;
1103 	case DEVFS_DESTROY_SUBNAMES:
1104 		devfs_destroy_subnames_worker(msg->mdv_load);
1105 		break;
1106 	case DEVFS_DESTROY_DEV_BY_OPS:
1107 		devfs_destroy_dev_by_ops_worker(msg->mdv_ops.ops,
1108 						msg->mdv_ops.minor);
1109 		break;
1110 	case DEVFS_CREATE_ALL_DEV:
1111 		node = (struct devfs_node *)msg->mdv_load;
1112 		devfs_create_all_dev_worker(node);
1113 		break;
1114 	case DEVFS_MOUNT_ADD:
1115 		mnt = msg->mdv_mnt;
1116 		TAILQ_INSERT_TAIL(&devfs_mnt_list, mnt, link);
1117 		devfs_create_all_dev_worker(mnt->root_node);
1118 		break;
1119 	case DEVFS_MOUNT_DEL:
1120 		mnt = msg->mdv_mnt;
1121 		TAILQ_REMOVE(&devfs_mnt_list, mnt, link);
1122 		devfs_iterate_topology(mnt->root_node, devfs_reaperp_callback,
1123 				       NULL);
1124 		if (mnt->leak_count) {
1125 			devfs_debug(DEVFS_DEBUG_SHOW,
1126 				    "Leaked %ld devfs_node elements!\n",
1127 				    mnt->leak_count);
1128 		}
1129 		break;
1130 	case DEVFS_CHANDLER_ADD:
1131 		devfs_chandler_add_worker(msg->mdv_chandler.name,
1132 				msg->mdv_chandler.nhandler);
1133 		break;
1134 	case DEVFS_CHANDLER_DEL:
1135 		devfs_chandler_del_worker(msg->mdv_chandler.name);
1136 		break;
1137 	case DEVFS_FIND_DEVICE_BY_NAME:
1138 		devfs_find_device_by_name_worker(msg);
1139 		break;
1140 	case DEVFS_FIND_DEVICE_BY_UDEV:
1141 		devfs_find_device_by_udev_worker(msg);
1142 		break;
1143 	case DEVFS_MAKE_ALIAS:
1144 		devfs_make_alias_worker((struct devfs_alias *)msg->mdv_load);
1145 		break;
1146 	case DEVFS_APPLY_RULES:
1147 		devfs_apply_reset_rules_caller(msg->mdv_name, 1);
1148 		break;
1149 	case DEVFS_RESET_RULES:
1150 		devfs_apply_reset_rules_caller(msg->mdv_name, 0);
1151 		break;
1152 	case DEVFS_SCAN_CALLBACK:
1153 		devfs_scan_callback_worker((devfs_scan_t *)msg->mdv_load,
1154 			msg->mdv_load2);
1155 		break;
1156 	case DEVFS_CLR_SUBNAMES_FLAG:
1157 		devfs_clr_subnames_flag_worker(msg->mdv_flags.name,
1158 				msg->mdv_flags.flag);
1159 		break;
1160 	case DEVFS_DESTROY_SUBNAMES_WO_FLAG:
1161 		devfs_destroy_subnames_without_flag_worker(msg->mdv_flags.name,
1162 				msg->mdv_flags.flag);
1163 		break;
1164 	case DEVFS_INODE_TO_VNODE:
1165 		msg->mdv_ino.vp = devfs_iterate_topology(
1166 			DEVFS_MNTDATA(msg->mdv_ino.mp)->root_node,
1167 			(devfs_iterate_callback_t *)devfs_inode_to_vnode_worker_callback,
1168 			&msg->mdv_ino.ino);
1169 		break;
1170 	case DEVFS_TERMINATE_CORE:
1171 		devfs_run = 0;
1172 		break;
1173 	case DEVFS_SYNC:
1174 		break;
1175 	default:
1176 		devfs_debug(DEVFS_DEBUG_WARNING,
1177 			    "devfs_msg_core: unknown message "
1178 			    "received at core\n");
1179 		break;
1180 	}
1181 	lockmgr(&devfs_lock, LK_RELEASE);
1182 }
1183 
1184 /*
1185  * Worker function to insert a new dev into the dev list and initialize its
1186  * permissions. It also calls devfs_propagate_dev which in turn propagates
1187  * the change to all mount points.
1188  *
1189  * The passed dev is already referenced.  This reference is eaten by this
1190  * function and represents the dev's linkage into devfs_dev_list.
1191  */
1192 static int
1193 devfs_create_dev_worker(cdev_t dev, uid_t uid, gid_t gid, int perms)
1194 {
1195 	KKASSERT(dev);
1196 
1197 	dev->si_uid = uid;
1198 	dev->si_gid = gid;
1199 	dev->si_perms = perms;
1200 
1201 	devfs_link_dev(dev);
1202 	devfs_propagate_dev(dev, 1);
1203 
1204 	udev_event_attach(dev, NULL, 0);
1205 
1206 	return 0;
1207 }
1208 
1209 /*
1210  * Worker function to delete a dev from the dev list and free the cdev.
1211  * It also calls devfs_propagate_dev which in turn propagates the change
1212  * to all mount points.
1213  */
1214 static int
1215 devfs_destroy_dev_worker(cdev_t dev)
1216 {
1217 	int error;
1218 
1219 	KKASSERT(dev);
1220 	KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1221 
1222 	error = devfs_unlink_dev(dev);
1223 	devfs_propagate_dev(dev, 0);
1224 
1225 	udev_event_detach(dev, NULL, 0);
1226 
1227 	if (error == 0)
1228 		release_dev(dev);	/* link ref */
1229 	release_dev(dev);
1230 	release_dev(dev);
1231 
1232 	return 0;
1233 }
1234 
1235 /*
1236  * Worker function to destroy all devices with a certain basename.
1237  * Calls devfs_destroy_dev_worker for the actual destruction.
1238  */
1239 static int
1240 devfs_destroy_subnames_worker(char *name)
1241 {
1242 	cdev_t dev, dev1;
1243 	size_t len = strlen(name);
1244 
1245 	TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1246 		if ((!strncmp(dev->si_name, name, len)) &&
1247 				(dev->si_name[len] != '\0')) {
1248 			devfs_destroy_dev_worker(dev);
1249 		}
1250 	}
1251 	return 0;
1252 }
1253 
1254 static int
1255 devfs_clr_subnames_flag_worker(char *name, uint32_t flag)
1256 {
1257 	cdev_t dev, dev1;
1258 	size_t len = strlen(name);
1259 
1260 	TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1261 		if ((!strncmp(dev->si_name, name, len)) &&
1262 				(dev->si_name[len] != '\0')) {
1263 			dev->si_flags &= ~flag;
1264 		}
1265 	}
1266 
1267 	return 0;
1268 }
1269 
1270 static int
1271 devfs_destroy_subnames_without_flag_worker(char *name, uint32_t flag)
1272 {
1273 	cdev_t dev, dev1;
1274 	size_t len = strlen(name);
1275 
1276 	TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1277 		if ((!strncmp(dev->si_name, name, len)) &&
1278 				(dev->si_name[len] != '\0')) {
1279 			if (!(dev->si_flags & flag)) {
1280 				devfs_destroy_dev_worker(dev);
1281 			}
1282 		}
1283 	}
1284 
1285 	return 0;
1286 }
1287 
1288 /*
1289  * Worker function that creates all device nodes on top of a devfs
1290  * root node.
1291  */
1292 static int
1293 devfs_create_all_dev_worker(struct devfs_node *root)
1294 {
1295 	cdev_t dev;
1296 
1297 	KKASSERT(root);
1298 
1299 	TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1300 		devfs_create_device_node(root, dev, NULL, NULL);
1301 	}
1302 
1303 	return 0;
1304 }
1305 
1306 /*
1307  * Worker function that destroys all devices that match a specific
1308  * dev_ops and/or minor. If minor is less than 0, it is not matched
1309  * against. It also propagates all changes.
1310  */
1311 static int
1312 devfs_destroy_dev_by_ops_worker(struct dev_ops *ops, int minor)
1313 {
1314 	cdev_t dev, dev1;
1315 
1316 	KKASSERT(ops);
1317 
1318 	TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1319 		if (dev->si_ops != ops)
1320 			continue;
1321 		if ((minor < 0) || (dev->si_uminor == minor)) {
1322 			devfs_destroy_dev_worker(dev);
1323 		}
1324 	}
1325 
1326 	return 0;
1327 }
1328 
1329 /*
1330  * Worker function that registers a new clone handler in devfs.
1331  */
1332 static int
1333 devfs_chandler_add_worker(const char *name, d_clone_t *nhandler)
1334 {
1335 	struct devfs_clone_handler *chandler = NULL;
1336 	u_char len = strlen(name);
1337 
1338 	if (len == 0)
1339 		return 1;
1340 
1341 	TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
1342 		if (chandler->namlen != len)
1343 			continue;
1344 
1345 		if (!memcmp(chandler->name, name, len)) {
1346 			/* Clonable basename already exists */
1347 			return 1;
1348 		}
1349 	}
1350 
1351 	chandler = kmalloc(sizeof(*chandler), M_DEVFS, M_WAITOK | M_ZERO);
1352 	chandler->name = kstrdup(name, M_DEVFS);
1353 	chandler->namlen = len;
1354 	chandler->nhandler = nhandler;
1355 
1356 	TAILQ_INSERT_TAIL(&devfs_chandler_list, chandler, link);
1357 	return 0;
1358 }
1359 
1360 /*
1361  * Worker function that removes a given clone handler from the
1362  * clone handler list.
1363  */
1364 static int
1365 devfs_chandler_del_worker(const char *name)
1366 {
1367 	struct devfs_clone_handler *chandler, *chandler2;
1368 	u_char len = strlen(name);
1369 
1370 	if (len == 0)
1371 		return 1;
1372 
1373 	TAILQ_FOREACH_MUTABLE(chandler, &devfs_chandler_list, link, chandler2) {
1374 		if (chandler->namlen != len)
1375 			continue;
1376 		if (memcmp(chandler->name, name, len))
1377 			continue;
1378 
1379 		TAILQ_REMOVE(&devfs_chandler_list, chandler, link);
1380 		kfree(chandler->name, M_DEVFS);
1381 		kfree(chandler, M_DEVFS);
1382 		break;
1383 	}
1384 
1385 	return 0;
1386 }
1387 
1388 /*
1389  * Worker function that finds a given device name and changes
1390  * the message received accordingly so that when replied to,
1391  * the answer is returned to the caller.
1392  */
1393 static int
1394 devfs_find_device_by_name_worker(devfs_msg_t devfs_msg)
1395 {
1396 	struct devfs_alias *alias;
1397 	cdev_t dev;
1398 	cdev_t found = NULL;
1399 
1400 	TAILQ_FOREACH(dev, &devfs_dev_list, link) {
1401 		if (strcmp(devfs_msg->mdv_name, dev->si_name) == 0) {
1402 			found = dev;
1403 			break;
1404 		}
1405 	}
1406 	if (found == NULL) {
1407 		TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1408 			if (strcmp(devfs_msg->mdv_name, alias->name) == 0) {
1409 				found = alias->dev_target;
1410 				break;
1411 			}
1412 		}
1413 	}
1414 	devfs_msg->mdv_cdev = found;
1415 
1416 	return 0;
1417 }
1418 
1419 /*
1420  * Worker function that finds a given device udev and changes
1421  * the message received accordingly so that when replied to,
1422  * the answer is returned to the caller.
1423  */
1424 static int
1425 devfs_find_device_by_udev_worker(devfs_msg_t devfs_msg)
1426 {
1427 	cdev_t dev, dev1;
1428 	cdev_t found = NULL;
1429 
1430 	TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1431 		if (((udev_t)dev->si_inode) == devfs_msg->mdv_udev) {
1432 			found = dev;
1433 			break;
1434 		}
1435 	}
1436 	devfs_msg->mdv_cdev = found;
1437 
1438 	return 0;
1439 }
1440 
1441 /*
1442  * Worker function that inserts a given alias into the
1443  * alias list, and propagates the alias to all mount
1444  * points.
1445  */
1446 static int
1447 devfs_make_alias_worker(struct devfs_alias *alias)
1448 {
1449 	struct devfs_alias *alias2;
1450 	size_t len = strlen(alias->name);
1451 	int found = 0;
1452 
1453 	TAILQ_FOREACH(alias2, &devfs_alias_list, link) {
1454 		if (len != alias2->namlen)
1455 			continue;
1456 
1457 		if (!memcmp(alias->name, alias2->name, len)) {
1458 			found = 1;
1459 			break;
1460 		}
1461 	}
1462 
1463 	if (!found) {
1464 		/*
1465 		 * The alias doesn't exist yet, so we add it to the alias list
1466 		 */
1467 		TAILQ_INSERT_TAIL(&devfs_alias_list, alias, link);
1468 		devfs_alias_propagate(alias);
1469 		udev_event_attach(alias->dev_target, alias->name, 1);
1470 	} else {
1471 		devfs_debug(DEVFS_DEBUG_WARNING,
1472 			    "Warning: duplicate devfs_make_alias for %s\n",
1473 			    alias->name);
1474 		kfree(alias->name, M_DEVFS);
1475 		kfree(alias, M_DEVFS);
1476 	}
1477 
1478 	return 0;
1479 }
1480 
1481 /*
1482  * Function that removes and frees all aliases.
1483  */
1484 static int
1485 devfs_alias_reap(void)
1486 {
1487 	struct devfs_alias *alias, *alias2;
1488 
1489 	TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1490 		TAILQ_REMOVE(&devfs_alias_list, alias, link);
1491 		kfree(alias, M_DEVFS);
1492 	}
1493 	return 0;
1494 }
1495 
1496 /*
1497  * Function that removes an alias matching a specific cdev and frees
1498  * it accordingly.
1499  */
1500 static int
1501 devfs_alias_remove(cdev_t dev)
1502 {
1503 	struct devfs_alias *alias, *alias2;
1504 
1505 	TAILQ_FOREACH_MUTABLE(alias, &devfs_alias_list, link, alias2) {
1506 		if (alias->dev_target == dev) {
1507 			TAILQ_REMOVE(&devfs_alias_list, alias, link);
1508 			udev_event_detach(alias->dev_target, alias->name, 1);
1509 			kfree(alias, M_DEVFS);
1510 		}
1511 	}
1512 	return 0;
1513 }
1514 
1515 /*
1516  * This function propagates a new alias to all mount points.
1517  */
1518 static int
1519 devfs_alias_propagate(struct devfs_alias *alias)
1520 {
1521 	struct devfs_mnt_data *mnt;
1522 
1523 	TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1524 		devfs_alias_apply(mnt->root_node, alias);
1525 	}
1526 	return 0;
1527 }
1528 
1529 /*
1530  * This function is a recursive function iterating through
1531  * all device nodes in the topology and, if applicable,
1532  * creating the relevant alias for a device node.
1533  */
1534 static int
1535 devfs_alias_apply(struct devfs_node *node, struct devfs_alias *alias)
1536 {
1537 	struct devfs_node *node1, *node2;
1538 
1539 	KKASSERT(alias != NULL);
1540 
1541 	if ((node->node_type == Proot) || (node->node_type == Pdir)) {
1542 		if (node->nchildren > 2) {
1543 			TAILQ_FOREACH_MUTABLE(node1, DEVFS_DENODE_HEAD(node), link, node2) {
1544 				devfs_alias_apply(node1, alias);
1545 			}
1546 		}
1547 	} else {
1548 		if (node->d_dev == alias->dev_target)
1549 			devfs_alias_create(alias->name, node, 0);
1550 	}
1551 	return 0;
1552 }
1553 
1554 /*
1555  * This function checks if any alias possibly is applicable
1556  * to the given node. If so, the alias is created.
1557  */
1558 static int
1559 devfs_alias_check_create(struct devfs_node *node)
1560 {
1561 	struct devfs_alias *alias;
1562 
1563 	TAILQ_FOREACH(alias, &devfs_alias_list, link) {
1564 		if (node->d_dev == alias->dev_target)
1565 			devfs_alias_create(alias->name, node, 0);
1566 	}
1567 	return 0;
1568 }
1569 
1570 /*
1571  * This function creates an alias with a given name
1572  * linking to a given devfs node. It also increments
1573  * the link count on the target node.
1574  */
1575 int
1576 devfs_alias_create(char *name_orig, struct devfs_node *target, int rule_based)
1577 {
1578 	struct mount *mp = target->mp;
1579 	struct devfs_node *parent = DEVFS_MNTDATA(mp)->root_node;
1580 	struct devfs_node *linknode;
1581 	struct hotplug_device *hpdev;
1582 	char *create_path = NULL;
1583 	char *name;
1584 	char *name_buf;
1585 	int result = 0;
1586 
1587 	KKASSERT((lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE);
1588 
1589 	name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1590 	devfs_resolve_name_path(name_orig, name_buf, &create_path, &name);
1591 
1592 	if (create_path)
1593 		parent = devfs_resolve_or_create_path(parent, create_path, 1);
1594 
1595 
1596 	if (devfs_find_device_node_by_name(parent, name)) {
1597 		devfs_debug(DEVFS_DEBUG_WARNING,
1598 			    "Node already exists: %s "
1599 			    "(devfs_make_alias_worker)!\n",
1600 			    name);
1601 		result = 1;
1602 		goto done;
1603 	}
1604 
1605 	linknode = devfs_allocp(Plink, name, parent, mp, NULL);
1606 	if (linknode == NULL) {
1607 		result = 1;
1608 		goto done;
1609 	}
1610 
1611 	linknode->link_target = target;
1612 	target->nlinks++;
1613 
1614 	if (rule_based)
1615 		linknode->flags |= DEVFS_RULE_CREATED;
1616 
1617 done:
1618 	/* hotplug handler */
1619 	if(devfs_node_added) {
1620 		hpdev = kmalloc(sizeof(struct hotplug_device), M_TEMP, M_WAITOK);
1621 		hpdev->dev = target->d_dev;
1622 		hpdev->name = name_orig;
1623 		devfs_node_added(hpdev);
1624 		kfree(hpdev, M_TEMP);
1625 	}
1626 	kfree(name_buf, M_TEMP);
1627 	return (result);
1628 }
1629 
1630 /*
1631  * This function is called by the core and handles mount point
1632  * strings. It either calls the relevant worker (devfs_apply_
1633  * reset_rules_worker) on all mountpoints or only a specific
1634  * one.
1635  */
1636 static int
1637 devfs_apply_reset_rules_caller(char *mountto, int apply)
1638 {
1639 	struct devfs_mnt_data *mnt;
1640 
1641 	if (mountto[0] == '*') {
1642 		TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1643 			devfs_iterate_topology(mnt->root_node,
1644 					(apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1645 					NULL);
1646 		}
1647 	} else {
1648 		TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1649 			if (!strcmp(mnt->mp->mnt_stat.f_mntonname, mountto)) {
1650 				devfs_iterate_topology(mnt->root_node,
1651 					(apply)?(devfs_rule_check_apply):(devfs_rule_reset_node),
1652 					NULL);
1653 				break;
1654 			}
1655 		}
1656 	}
1657 
1658 	kfree(mountto, M_DEVFS);
1659 	return 0;
1660 }
1661 
1662 /*
1663  * This function calls a given callback function for
1664  * every dev node in the devfs dev list.
1665  */
1666 static int
1667 devfs_scan_callback_worker(devfs_scan_t *callback, void *arg)
1668 {
1669 	cdev_t dev, dev1;
1670 
1671 	TAILQ_FOREACH_MUTABLE(dev, &devfs_dev_list, link, dev1) {
1672 		callback(dev, arg);
1673 	}
1674 
1675 	return 0;
1676 }
1677 
1678 /*
1679  * This function tries to resolve a given directory, or if not
1680  * found and creation requested, creates the given directory.
1681  */
1682 static struct devfs_node *
1683 devfs_resolve_or_create_dir(struct devfs_node *parent, char *dir_name,
1684 			    size_t name_len, int create)
1685 {
1686 	struct devfs_node *node, *found = NULL;
1687 
1688 	TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1689 		if (name_len != node->d_dir.d_namlen)
1690 			continue;
1691 
1692 		if (!memcmp(dir_name, node->d_dir.d_name, name_len)) {
1693 			found = node;
1694 			break;
1695 		}
1696 	}
1697 
1698 	if ((found == NULL) && (create)) {
1699 		found = devfs_allocp(Pdir, dir_name, parent, parent->mp, NULL);
1700 	}
1701 
1702 	return found;
1703 }
1704 
1705 /*
1706  * This function tries to resolve a complete path. If creation is requested,
1707  * if a given part of the path cannot be resolved (because it doesn't exist),
1708  * it is created.
1709  */
1710 struct devfs_node *
1711 devfs_resolve_or_create_path(struct devfs_node *parent, char *path, int create)
1712 {
1713 	struct devfs_node *node = parent;
1714 	char *buf;
1715 	size_t idx = 0;
1716 
1717 	if (path == NULL)
1718 		return parent;
1719 
1720 	buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1721 
1722 	while (*path && idx < PATH_MAX - 1) {
1723 		if (*path != '/') {
1724 			buf[idx++] = *path;
1725 		} else {
1726 			buf[idx] = '\0';
1727 			node = devfs_resolve_or_create_dir(node, buf, idx, create);
1728 			if (node == NULL) {
1729 				kfree(buf, M_TEMP);
1730 				return NULL;
1731 			}
1732 			idx = 0;
1733 		}
1734 		++path;
1735 	}
1736 	buf[idx] = '\0';
1737 	node = devfs_resolve_or_create_dir(node, buf, idx, create);
1738 	kfree (buf, M_TEMP);
1739 	return (node);
1740 }
1741 
1742 /*
1743  * Takes a full path and strips it into a directory path and a name.
1744  * For a/b/c/foo, it returns foo in namep and a/b/c in pathp. It
1745  * requires a working buffer with enough size to keep the whole
1746  * fullpath.
1747  */
1748 int
1749 devfs_resolve_name_path(char *fullpath, char *buf, char **pathp, char **namep)
1750 {
1751 	char *name = NULL;
1752 	char *path = NULL;
1753 	size_t len = strlen(fullpath) + 1;
1754 	int i;
1755 
1756 	KKASSERT((fullpath != NULL) && (buf != NULL));
1757 	KKASSERT((pathp != NULL) && (namep != NULL));
1758 
1759 	memcpy(buf, fullpath, len);
1760 
1761 	for (i = len-1; i>= 0; i--) {
1762 		if (buf[i] == '/') {
1763 			buf[i] = '\0';
1764 			name = &(buf[i+1]);
1765 			path = buf;
1766 			break;
1767 		}
1768 	}
1769 
1770 	*pathp = path;
1771 
1772 	if (name) {
1773 		*namep = name;
1774 	} else {
1775 		*namep = buf;
1776 	}
1777 
1778 	return 0;
1779 }
1780 
1781 /*
1782  * This function creates a new devfs node for a given device.  It can
1783  * handle a complete path as device name, and accordingly creates
1784  * the path and the final device node.
1785  *
1786  * The reference count on the passed dev remains unchanged.
1787  */
1788 struct devfs_node *
1789 devfs_create_device_node(struct devfs_node *root, cdev_t dev,
1790 			 char *dev_name, char *path_fmt, ...)
1791 {
1792 	struct devfs_node *parent, *node = NULL;
1793 	struct hotplug_device *hpdev;
1794 	char *path = NULL;
1795 	char *name;
1796 	char *name_buf;
1797 	__va_list ap;
1798 	int i, found;
1799 	char *create_path = NULL;
1800 	char *names = "pqrsPQRS";
1801 
1802 	name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1803 
1804 	if (path_fmt != NULL) {
1805 		__va_start(ap, path_fmt);
1806 		kvasnrprintf(&path, PATH_MAX, 10, path_fmt, ap);
1807 		__va_end(ap);
1808 	}
1809 
1810 	parent = devfs_resolve_or_create_path(root, path, 1);
1811 	KKASSERT(parent);
1812 
1813 	devfs_resolve_name_path(
1814 			((dev_name == NULL) && (dev))?(dev->si_name):(dev_name),
1815 			name_buf, &create_path, &name);
1816 
1817 	if (create_path)
1818 		parent = devfs_resolve_or_create_path(parent, create_path, 1);
1819 
1820 
1821 	if (devfs_find_device_node_by_name(parent, name)) {
1822 		devfs_debug(DEVFS_DEBUG_WARNING, "devfs_create_device_node: "
1823 			"DEVICE %s ALREADY EXISTS!!! Ignoring creation request.\n", name);
1824 		goto out;
1825 	}
1826 
1827 	node = devfs_allocp(Pdev, name, parent, parent->mp, dev);
1828 	nanotime(&parent->mtime);
1829 
1830 	/*
1831 	 * Ugly unix98 pty magic, to hide pty master (ptm) devices and their
1832 	 * directory
1833 	 */
1834 	if ((dev) && (strlen(dev->si_name) >= 4) &&
1835 			(!memcmp(dev->si_name, "ptm/", 4))) {
1836 		node->parent->flags |= DEVFS_HIDDEN;
1837 		node->flags |= DEVFS_HIDDEN;
1838 	}
1839 
1840 	/*
1841 	 * Ugly pty magic, to tag pty devices as such and hide them if needed.
1842 	 */
1843 	if ((strlen(name) >= 3) && (!memcmp(name, "pty", 3)))
1844 		node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1845 
1846 	if ((strlen(name) >= 3) && (!memcmp(name, "tty", 3))) {
1847 		found = 0;
1848 		for (i = 0; i < strlen(names); i++) {
1849 			if (name[3] == names[i]) {
1850 				found = 1;
1851 				break;
1852 			}
1853 		}
1854 		if (found)
1855 			node->flags |= (DEVFS_PTY | DEVFS_INVISIBLE);
1856 	}
1857 	/* hotplug handler */
1858 	if(devfs_node_added) {
1859 		hpdev = kmalloc(sizeof(struct hotplug_device), M_TEMP, M_WAITOK);
1860 		hpdev->dev = node->d_dev;
1861 		hpdev->name = node->d_dev->si_name;
1862 		devfs_node_added(hpdev);
1863 		kfree(hpdev, M_TEMP);
1864 	}
1865 
1866 out:
1867 	kfree(name_buf, M_TEMP);
1868 	kvasfree(&path);
1869 	return node;
1870 }
1871 
1872 /*
1873  * This function finds a given device node in the topology with a given
1874  * cdev.
1875  */
1876 void *
1877 devfs_find_device_node_callback(struct devfs_node *node, cdev_t target)
1878 {
1879 	if ((node->node_type == Pdev) && (node->d_dev == target)) {
1880 		return node;
1881 	}
1882 
1883 	return NULL;
1884 }
1885 
1886 /*
1887  * This function finds a device node in the given parent directory by its
1888  * name and returns it.
1889  */
1890 struct devfs_node *
1891 devfs_find_device_node_by_name(struct devfs_node *parent, char *target)
1892 {
1893 	struct devfs_node *node, *found = NULL;
1894 	size_t len = strlen(target);
1895 
1896 	TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(parent), link) {
1897 		if (len != node->d_dir.d_namlen)
1898 			continue;
1899 
1900 		if (!memcmp(node->d_dir.d_name, target, len)) {
1901 			found = node;
1902 			break;
1903 		}
1904 	}
1905 
1906 	return found;
1907 }
1908 
1909 static void *
1910 devfs_inode_to_vnode_worker_callback(struct devfs_node *node, ino_t *inop)
1911 {
1912 	struct vnode *vp = NULL;
1913 	ino_t target = *inop;
1914 
1915 	if (node->d_dir.d_ino == target) {
1916 		if (node->v_node) {
1917 			vp = node->v_node;
1918 			vget(vp, LK_EXCLUSIVE | LK_RETRY);
1919 			vn_unlock(vp);
1920 		} else {
1921 			devfs_allocv(&vp, node);
1922 			vn_unlock(vp);
1923 		}
1924 	}
1925 
1926 	return vp;
1927 }
1928 
1929 /*
1930  * This function takes a cdev and removes its devfs node in the
1931  * given topology.  The cdev remains intact.
1932  */
1933 int
1934 devfs_destroy_device_node(struct devfs_node *root, cdev_t target)
1935 {
1936 	struct devfs_node *node, *parent;
1937 	char *name;
1938 	char *name_buf;
1939 	char *create_path = NULL;
1940 
1941 	KKASSERT(target);
1942 
1943 	name_buf = kmalloc(PATH_MAX, M_TEMP, M_WAITOK);
1944 	ksnprintf(name_buf, PATH_MAX, "%s", target->si_name);
1945 
1946 	devfs_resolve_name_path(target->si_name, name_buf, &create_path, &name);
1947 
1948 	if (create_path)
1949 		parent = devfs_resolve_or_create_path(root, create_path, 0);
1950 	else
1951 		parent = root;
1952 
1953 	if (parent == NULL) {
1954 		kfree(name_buf, M_TEMP);
1955 		return 1;
1956 	}
1957 
1958 	node = devfs_find_device_node_by_name(parent, name);
1959 
1960 	if (node) {
1961 		nanotime(&node->parent->mtime);
1962 		devfs_gc(node);
1963 	}
1964 
1965 	kfree(name_buf, M_TEMP);
1966 
1967 	return 0;
1968 }
1969 
1970 /*
1971  * Just set perms and ownership for given node.
1972  */
1973 int
1974 devfs_set_perms(struct devfs_node *node, uid_t uid, gid_t gid,
1975 		u_short mode, u_long flags)
1976 {
1977 	node->mode = mode;
1978 	node->uid = uid;
1979 	node->gid = gid;
1980 
1981 	return 0;
1982 }
1983 
1984 /*
1985  * Propagates a device attach/detach to all mount
1986  * points. Also takes care of automatic alias removal
1987  * for a deleted cdev.
1988  */
1989 static int
1990 devfs_propagate_dev(cdev_t dev, int attach)
1991 {
1992 	struct devfs_mnt_data *mnt;
1993 
1994 	TAILQ_FOREACH(mnt, &devfs_mnt_list, link) {
1995 		if (attach) {
1996 			/* Device is being attached */
1997 			devfs_create_device_node(mnt->root_node, dev,
1998 						 NULL, NULL );
1999 		} else {
2000 			/* Device is being detached */
2001 			devfs_alias_remove(dev);
2002 			devfs_destroy_device_node(mnt->root_node, dev);
2003 		}
2004 	}
2005 	return 0;
2006 }
2007 
2008 /*
2009  * devfs_clone either returns a basename from a complete name by
2010  * returning the length of the name without trailing digits, or,
2011  * if clone != 0, calls the device's clone handler to get a new
2012  * device, which in turn is returned in devp.
2013  */
2014 cdev_t
2015 devfs_clone(cdev_t dev, const char *name, size_t len, int mode,
2016 		struct ucred *cred)
2017 {
2018 	int error;
2019 	struct devfs_clone_handler *chandler;
2020 	struct dev_clone_args ap;
2021 
2022 	TAILQ_FOREACH(chandler, &devfs_chandler_list, link) {
2023 		if (chandler->namlen != len)
2024 			continue;
2025 		if ((!memcmp(chandler->name, name, len)) && (chandler->nhandler)) {
2026 			lockmgr(&devfs_lock, LK_RELEASE);
2027 			devfs_config();
2028 			lockmgr(&devfs_lock, LK_EXCLUSIVE);
2029 
2030 			ap.a_head.a_dev = dev;
2031 			ap.a_dev = NULL;
2032 			ap.a_name = name;
2033 			ap.a_namelen = len;
2034 			ap.a_mode = mode;
2035 			ap.a_cred = cred;
2036 			error = (chandler->nhandler)(&ap);
2037 			if (error)
2038 				continue;
2039 
2040 			return ap.a_dev;
2041 		}
2042 	}
2043 
2044 	return NULL;
2045 }
2046 
2047 
2048 /*
2049  * Registers a new orphan in the orphan list.
2050  */
2051 void
2052 devfs_tracer_add_orphan(struct devfs_node *node)
2053 {
2054 	struct devfs_orphan *orphan;
2055 
2056 	KKASSERT(node);
2057 	orphan = kmalloc(sizeof(struct devfs_orphan), M_DEVFS, M_WAITOK);
2058 	orphan->node = node;
2059 
2060 	KKASSERT((node->flags & DEVFS_ORPHANED) == 0);
2061 	node->flags |= DEVFS_ORPHANED;
2062 	TAILQ_INSERT_TAIL(DEVFS_ORPHANLIST(node->mp), orphan, link);
2063 }
2064 
2065 /*
2066  * Removes an orphan from the orphan list.
2067  */
2068 void
2069 devfs_tracer_del_orphan(struct devfs_node *node)
2070 {
2071 	struct devfs_orphan *orphan;
2072 
2073 	KKASSERT(node);
2074 
2075 	TAILQ_FOREACH(orphan, DEVFS_ORPHANLIST(node->mp), link)	{
2076 		if (orphan->node == node) {
2077 			node->flags &= ~DEVFS_ORPHANED;
2078 			TAILQ_REMOVE(DEVFS_ORPHANLIST(node->mp), orphan, link);
2079 			kfree(orphan, M_DEVFS);
2080 			break;
2081 		}
2082 	}
2083 }
2084 
2085 /*
2086  * Counts the orphans in the orphan list, and if cleanup
2087  * is specified, also frees the orphan and removes it from
2088  * the list.
2089  */
2090 size_t
2091 devfs_tracer_orphan_count(struct mount *mp, int cleanup)
2092 {
2093 	struct devfs_orphan *orphan, *orphan2;
2094 	size_t count = 0;
2095 
2096 	TAILQ_FOREACH_MUTABLE(orphan, DEVFS_ORPHANLIST(mp), link, orphan2)	{
2097 		count++;
2098 		/*
2099 		 * If we are instructed to clean up, we do so.
2100 		 */
2101 		if (cleanup) {
2102 			TAILQ_REMOVE(DEVFS_ORPHANLIST(mp), orphan, link);
2103 			orphan->node->flags &= ~DEVFS_ORPHANED;
2104 			devfs_freep(orphan->node);
2105 			kfree(orphan, M_DEVFS);
2106 		}
2107 	}
2108 
2109 	return count;
2110 }
2111 
2112 /*
2113  * Fetch an ino_t from the global d_ino by increasing it
2114  * while spinlocked.
2115  */
2116 static ino_t
2117 devfs_fetch_ino(void)
2118 {
2119 	ino_t	ret;
2120 
2121 	spin_lock_wr(&ino_lock);
2122 	ret = d_ino++;
2123 	spin_unlock_wr(&ino_lock);
2124 
2125 	return ret;
2126 }
2127 
2128 /*
2129  * Allocates a new cdev and initializes it's most basic
2130  * fields.
2131  */
2132 cdev_t
2133 devfs_new_cdev(struct dev_ops *ops, int minor, struct dev_ops *bops)
2134 {
2135 	cdev_t dev = sysref_alloc(&cdev_sysref_class);
2136 
2137 	sysref_activate(&dev->si_sysref);
2138 	reference_dev(dev);
2139 	bzero(dev, offsetof(struct cdev, si_sysref));
2140 
2141 	dev->si_uid = 0;
2142 	dev->si_gid = 0;
2143 	dev->si_perms = 0;
2144 	dev->si_drv1 = NULL;
2145 	dev->si_drv2 = NULL;
2146 	dev->si_lastread = 0;		/* time_second */
2147 	dev->si_lastwrite = 0;		/* time_second */
2148 
2149 	dev->si_dict = NULL;
2150 	dev->si_ops = ops;
2151 	dev->si_flags = 0;
2152 	dev->si_umajor = 0;
2153 	dev->si_uminor = minor;
2154 	dev->si_bops = bops;
2155 	/* If there is a backing device, we reference its ops */
2156 	dev->si_inode = makeudev(
2157 		    devfs_reference_ops((bops)?(bops):(ops)),
2158 		    minor );
2159 
2160 	return dev;
2161 }
2162 
2163 static void
2164 devfs_cdev_terminate(cdev_t dev)
2165 {
2166 	int locked = 0;
2167 
2168 	/* Check if it is locked already. if not, we acquire the devfs lock */
2169 	if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) {
2170 		lockmgr(&devfs_lock, LK_EXCLUSIVE);
2171 		locked = 1;
2172 	}
2173 
2174 	/* Propagate destruction, just in case */
2175 	devfs_propagate_dev(dev, 0);
2176 
2177 	/* If we acquired the lock, we also get rid of it */
2178 	if (locked)
2179 		lockmgr(&devfs_lock, LK_RELEASE);
2180 
2181 	/* If there is a backing device, we release the backing device's ops */
2182 	devfs_release_ops((dev->si_bops)?(dev->si_bops):(dev->si_ops));
2183 
2184 	/* Finally destroy the device */
2185 	sysref_put(&dev->si_sysref);
2186 }
2187 
2188 /*
2189  * Dummies for now (individual locks for MPSAFE)
2190  */
2191 static void
2192 devfs_cdev_lock(cdev_t dev)
2193 {
2194 }
2195 
2196 static void
2197 devfs_cdev_unlock(cdev_t dev)
2198 {
2199 }
2200 
2201 /*
2202  * Links a given cdev into the dev list.
2203  */
2204 int
2205 devfs_link_dev(cdev_t dev)
2206 {
2207 	KKASSERT((dev->si_flags & SI_DEVFS_LINKED) == 0);
2208 	dev->si_flags |= SI_DEVFS_LINKED;
2209 	TAILQ_INSERT_TAIL(&devfs_dev_list, dev, link);
2210 
2211 	return 0;
2212 }
2213 
2214 /*
2215  * Removes a given cdev from the dev list.  The caller is responsible for
2216  * releasing the reference on the device associated with the linkage.
2217  *
2218  * Returns EALREADY if the dev has already been unlinked.
2219  */
2220 static int
2221 devfs_unlink_dev(cdev_t dev)
2222 {
2223 	if ((dev->si_flags & SI_DEVFS_LINKED)) {
2224 		TAILQ_REMOVE(&devfs_dev_list, dev, link);
2225 		dev->si_flags &= ~SI_DEVFS_LINKED;
2226 		return (0);
2227 	}
2228 	return (EALREADY);
2229 }
2230 
2231 int
2232 devfs_node_is_accessible(struct devfs_node *node)
2233 {
2234 	if ((node) && (!(node->flags & DEVFS_HIDDEN)))
2235 		return 1;
2236 	else
2237 		return 0;
2238 }
2239 
2240 int
2241 devfs_reference_ops(struct dev_ops *ops)
2242 {
2243 	int unit;
2244 	struct devfs_dev_ops *found = NULL;
2245 	struct devfs_dev_ops *devops;
2246 
2247 	TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2248 		if (devops->ops == ops) {
2249 			found = devops;
2250 			break;
2251 		}
2252 	}
2253 
2254 	if (!found) {
2255 		found = kmalloc(sizeof(struct devfs_dev_ops), M_DEVFS, M_WAITOK);
2256 		found->ops = ops;
2257 		found->ref_count = 0;
2258 		TAILQ_INSERT_TAIL(&devfs_dev_ops_list, found, link);
2259 	}
2260 
2261 	KKASSERT(found);
2262 
2263 	if (found->ref_count == 0) {
2264 		found->id = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(ops_id), 255);
2265 		if (found->id == -1) {
2266 			/* Ran out of unique ids */
2267 			devfs_debug(DEVFS_DEBUG_WARNING,
2268 					"devfs_reference_ops: WARNING: ran out of unique ids\n");
2269 		}
2270 	}
2271 	unit = found->id;
2272 	++found->ref_count;
2273 
2274 	return unit;
2275 }
2276 
2277 void
2278 devfs_release_ops(struct dev_ops *ops)
2279 {
2280 	struct devfs_dev_ops *found = NULL;
2281 	struct devfs_dev_ops *devops;
2282 
2283 	TAILQ_FOREACH(devops, &devfs_dev_ops_list, link) {
2284 		if (devops->ops == ops) {
2285 			found = devops;
2286 			break;
2287 		}
2288 	}
2289 
2290 	KKASSERT(found);
2291 
2292 	--found->ref_count;
2293 
2294 	if (found->ref_count == 0) {
2295 		TAILQ_REMOVE(&devfs_dev_ops_list, found, link);
2296 		devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(ops_id), found->id);
2297 		kfree(found, M_DEVFS);
2298 	}
2299 }
2300 
2301 /*
2302  * Wait for asynchronous messages to complete in the devfs helper
2303  * thread, then return.  Do nothing if the helper thread is dead
2304  * or we are being indirectly called from the helper thread itself.
2305  */
2306 void
2307 devfs_config(void)
2308 {
2309 	devfs_msg_t msg;
2310 
2311 	if (devfs_run && curthread != td_core) {
2312 		msg = devfs_msg_get();
2313 		msg = devfs_msg_send_sync(DEVFS_SYNC, msg);
2314 		devfs_msg_put(msg);
2315 	}
2316 }
2317 
2318 /*
2319  * Called on init of devfs; creates the objcaches and
2320  * spawns off the devfs core thread. Also initializes
2321  * locks.
2322  */
2323 static void
2324 devfs_init(void)
2325 {
2326 	devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init() called\n");
2327 	/* Create objcaches for nodes, msgs and devs */
2328 	devfs_node_cache = objcache_create("devfs-node-cache", 0, 0,
2329 					   NULL, NULL, NULL,
2330 					   objcache_malloc_alloc,
2331 					   objcache_malloc_free,
2332 					   &devfs_node_malloc_args );
2333 
2334 	devfs_msg_cache = objcache_create("devfs-msg-cache", 0, 0,
2335 					  NULL, NULL, NULL,
2336 					  objcache_malloc_alloc,
2337 					  objcache_malloc_free,
2338 					  &devfs_msg_malloc_args );
2339 
2340 	devfs_dev_cache = objcache_create("devfs-dev-cache", 0, 0,
2341 					  NULL, NULL, NULL,
2342 					  objcache_malloc_alloc,
2343 					  objcache_malloc_free,
2344 					  &devfs_dev_malloc_args );
2345 
2346 	devfs_clone_bitmap_init(&DEVFS_CLONE_BITMAP(ops_id));
2347 
2348 	/* Initialize the reply-only port which acts as a message drain */
2349 	lwkt_initport_replyonly(&devfs_dispose_port, devfs_msg_autofree_reply);
2350 
2351 	/* Initialize *THE* devfs lock */
2352 	lockinit(&devfs_lock, "devfs_core lock", 0, 0);
2353 
2354 
2355 	lwkt_create(devfs_msg_core, /*args*/NULL, &td_core, NULL,
2356 		    0, 0, "devfs_msg_core");
2357 
2358 	while (devfs_run == 0)
2359 		tsleep(td_core, 0, "devfsc", 0);
2360 
2361 	devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_init finished\n");
2362 }
2363 
2364 /*
2365  * Called on unload of devfs; takes care of destroying the core
2366  * and the objcaches. Also removes aliases that are no longer needed.
2367  */
2368 static void
2369 devfs_uninit(void)
2370 {
2371 	devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_uninit() called\n");
2372 
2373 	devfs_msg_send(DEVFS_TERMINATE_CORE, NULL);
2374 	while (devfs_run)
2375 		tsleep(td_core, 0, "devfsc", hz*10);
2376 	tsleep(td_core, 0, "devfsc", hz);
2377 
2378 	devfs_clone_bitmap_uninit(&DEVFS_CLONE_BITMAP(ops_id));
2379 
2380 	/* Destroy the objcaches */
2381 	objcache_destroy(devfs_msg_cache);
2382 	objcache_destroy(devfs_node_cache);
2383 	objcache_destroy(devfs_dev_cache);
2384 
2385 	devfs_alias_reap();
2386 }
2387 
2388 /*
2389  * This is a sysctl handler to assist userland devname(3) to
2390  * find the device name for a given udev.
2391  */
2392 static int
2393 devfs_sysctl_devname_helper(SYSCTL_HANDLER_ARGS)
2394 {
2395 	udev_t 	udev;
2396 	cdev_t	found;
2397 	int		error;
2398 
2399 
2400 	if ((error = SYSCTL_IN(req, &udev, sizeof(udev_t))))
2401 		return (error);
2402 
2403 	devfs_debug(DEVFS_DEBUG_DEBUG, "devfs sysctl, received udev: %d\n", udev);
2404 
2405 	if (udev == NOUDEV)
2406 		return(EINVAL);
2407 
2408 	if ((found = devfs_find_device_by_udev(udev)) == NULL)
2409 		return(ENOENT);
2410 
2411 	return(SYSCTL_OUT(req, found->si_name, strlen(found->si_name) + 1));
2412 }
2413 
2414 
2415 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
2416 			NULL, 0, devfs_sysctl_devname_helper, "", "helper for devname(3)");
2417 
2418 SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2419 TUNABLE_INT("vfs.devfs.debug", &devfs_debug_enable);
2420 SYSCTL_INT(_vfs_devfs, OID_AUTO, debug, CTLFLAG_RW, &devfs_debug_enable,
2421 		0, "Enable DevFS debugging");
2422 
2423 SYSINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST,
2424 		devfs_init, NULL);
2425 SYSUNINIT(vfs_devfs_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY,
2426 		devfs_uninit, NULL);
2427