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