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