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