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