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