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