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