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