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