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