]>
git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/devfs/devfs_tree.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
24 * Copyright 1997,1998 Julian Elischer. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions are
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright notice,
33 * this list of conditions and the following disclaimer in the documentation
34 * and/or other materials provided with the distribution.
36 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
37 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE HOLDER OR CONTRIBUTORS BE LIABLE FOR
40 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
42 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
43 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * Dieter Siegmund (dieter@apple.com) Thu Apr 8 14:08:19 PDT 1999
54 * - removed mounting of "hidden" mountpoint
55 * - fixed problem in which devnode->dn_vn pointer was not
56 * updated with the vnode returned from checkalias()
57 * - replaced devfs_vntodn() with a macro VTODN()
58 * - rewrote dev_finddir() to not use recursion
59 * - added locking to avoid data structure corruption (DEVFS_(UN)LOCK())
60 * Dieter Siegmund (dieter@apple.com) Wed Jul 14 13:37:59 PDT 1999
61 * - fixed problem with devfs_dntovn() checking the v_id against the
62 * value cached in the device node; a union mount on top of us causes
63 * the v_id to get incremented thus, we would end up returning a new
64 * vnode instead of the existing one that has the mounted_here
65 * field filled in; the net effect was that the filesystem mounted
66 * on top of us would never show up
67 * - added devfs_stats to store how many data structures are actually
71 /* SPLIT_DEVS means each devfs uses a different devnode for the same device */
72 /* Otherwise the same device always ends up at the same vnode even if */
73 /* reached througgh a different devfs instance. The practical difference */
74 /* is that with the same vnode, chmods and chowns show up on all instances of */
77 #define SPLIT_DEVS 1 /* maybe make this an option */
78 /*#define SPLIT_DEVS 1*/
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
84 #include <sys/malloc.h>
85 #include <sys/mount_internal.h>
87 #include <sys/vnode.h>
91 #include "devfsdefs.h"
93 static void devfs_release_busy(devnode_t
*);
94 static void dev_free_hier(devdirent_t
*);
95 static int devfs_propogate(devdirent_t
*, devdirent_t
*);
96 static int dev_finddir(char *, devnode_t
*, int, devnode_t
**);
97 static int dev_dup_entry(devnode_t
*, devdirent_t
*, devdirent_t
**, struct devfsmount
*);
100 lck_grp_t
* devfs_lck_grp
;
101 lck_grp_attr_t
* devfs_lck_grp_attr
;
102 lck_attr_t
* devfs_lck_attr
;
103 lck_mtx_t devfs_mutex
;
105 devdirent_t
* dev_root
= NULL
; /* root of backing tree */
106 struct devfs_stats devfs_stats
; /* hold stats */
108 #ifdef HIDDEN_MOUNTPOINT
109 static struct mount
*devfs_hidden_mount
;
110 #endif /* HIDDEN_MOINTPOINT */
112 static int devfs_ready
= 0;
114 #define NOCREATE FALSE
118 * Set up the root directory node in the backing plane
119 * This is happenning before the vfs system has been
120 * set up yet, so be careful about what we reference..
121 * Notice that the ops are by indirection.. as they haven't
123 * DEVFS has a hidden mountpoint that is used as the anchor point
124 * for the internal 'blueprint' version of the dev filesystem tree.
132 devfs_lck_grp_attr
= lck_grp_attr_alloc_init();
133 devfs_lck_grp
= lck_grp_alloc_init("devfs_lock", devfs_lck_grp_attr
);
135 devfs_lck_attr
= lck_attr_alloc_init();
137 lck_mtx_init(&devfs_mutex
, devfs_lck_grp
, devfs_lck_attr
);
140 error
= dev_add_entry("root", NULL
, DEV_DIR
, NULL
, NULL
, NULL
, &dev_root
);
144 printf("devfs_sinit: dev_add_entry failed ");
147 #ifdef HIDDEN_MOUNTPOINT
148 MALLOC(devfs_hidden_mount
, struct mount
*, sizeof(struct mount
),
150 bzero(devfs_hidden_mount
,sizeof(struct mount
));
151 mount_lock_init(devfs_hidden_mount
);
152 TAILQ_INIT(&devfs_hidden_mount
->mnt_vnodelist
);
153 TAILQ_INIT(&devfs_hidden_mount
->mnt_workerqueue
);
154 TAILQ_INIT(&devfs_hidden_mount
->mnt_newvnodes
);
156 /* Initialize the default IO constraints */
157 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
158 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
160 devfs_mount(devfs_hidden_mount
,"dummy",NULL
,NULL
,NULL
);
161 dev_root
->de_dnp
->dn_dvm
162 = (struct devfsmount
*)devfs_hidden_mount
->mnt_data
;
163 #endif /* HIDDEN_MOUNTPOINT */
168 /***********************************************************************\
169 *************************************************************************
170 * Routines used to find our way to a point in the tree *
171 *************************************************************************
172 \***********************************************************************/
176 /***************************************************************
177 * Search down the linked list off a dir to find "name"
178 * return the devnode_t * for that node.
180 * called with DEVFS_LOCK held
181 ***************************************************************/
183 dev_findname(devnode_t
* dir
, char *name
)
186 if (dir
->dn_type
!= DEV_DIR
) return 0;/*XXX*/ /* printf?*/
192 return dir
->dn_typeinfo
.Dir
.myname
;
194 if((name
[1] == '.') && (name
[2] == 0))
196 /* for root, .. == . */
197 return dir
->dn_typeinfo
.Dir
.parent
->dn_typeinfo
.Dir
.myname
;
200 newfp
= dir
->dn_typeinfo
.Dir
.dirlist
;
204 if(!(strcmp(name
,newfp
->de_name
)))
206 newfp
= newfp
->de_next
;
211 /***********************************************************************
212 * Given a starting node (0 for root) and a pathname, return the node
213 * for the end item on the path. It MUST BE A DIRECTORY. If the 'CREATE'
214 * option is true, then create any missing nodes in the path and create
215 * and return the final node as well.
216 * This is used to set up a directory, before making nodes in it..
218 * called with DEVFS_LOCK held
219 ***********************************************************************/
221 dev_finddir(char * path
,
226 devnode_t
* dnp
= NULL
;
231 if (!dirnode
) /* dirnode == NULL means start at root */
232 dirnode
= dev_root
->de_dnp
;
234 if (dirnode
->dn_type
!= DEV_DIR
)
237 if (strlen(path
) > (DEVMAXPATHSIZE
- 1))
248 char component
[DEVMAXPATHSIZE
];
249 devdirent_t
* dirent_p
;
253 /* we hit the end of the string, we're done */
258 while (*scan
!= '/' && *scan
)
261 strncpy(component
, start
, scan
- start
);
262 component
[ scan
- start
] = '\0';
266 dirent_p
= dev_findname(dirnode
, component
);
268 dnp
= dirent_p
->de_dnp
;
269 if (dnp
->dn_type
!= DEV_DIR
) {
279 error
= dev_add_entry(component
, dirnode
,
280 DEV_DIR
, NULL
, NULL
, NULL
, &dirent_p
);
283 dnp
= dirent_p
->de_dnp
;
284 devfs_propogate(dirnode
->dn_typeinfo
.Dir
.myname
, dirent_p
);
286 dirnode
= dnp
; /* continue relative to this directory */
292 /***********************************************************************
293 * Add a new NAME element to the devfs
294 * If we're creating a root node, then dirname is NULL
295 * Basically this creates a new namespace entry for the device node
297 * Creates a name node, and links it to the supplied node
299 * called with DEVFS_LOCK held
300 ***********************************************************************/
302 dev_add_name(char * name
, devnode_t
* dirnode
, __unused devdirent_t
* back
,
303 devnode_t
* dnp
, devdirent_t
* *dirent_pp
)
305 devdirent_t
* dirent_p
= NULL
;
307 if(dirnode
!= NULL
) {
308 if(dirnode
->dn_type
!= DEV_DIR
) return(ENOTDIR
);
310 if( dev_findname(dirnode
,name
))
314 * make sure the name is legal
315 * slightly misleading in the case of NULL
317 if (!name
|| (strlen(name
) > (DEVMAXNAMESIZE
- 1)))
318 return (ENAMETOOLONG
);
321 * Allocate and fill out a new directory entry
323 MALLOC(dirent_p
, devdirent_t
*, sizeof(devdirent_t
),
324 M_DEVFSNAME
, M_WAITOK
);
328 bzero(dirent_p
,sizeof(devdirent_t
));
330 /* inherrit our parent's mount info */ /*XXX*/
331 /* a kludge but.... */
332 if(dirnode
&& ( dnp
->dn_dvm
== NULL
)) {
333 dnp
->dn_dvm
= dirnode
->dn_dvm
;
334 /* if(!dnp->dn_dvm) printf("parent had null dvm "); */
338 * Link the two together
339 * include the implicit link in the count of links to the devnode..
340 * this stops it from being accidentally freed later.
342 dirent_p
->de_dnp
= dnp
;
343 dnp
->dn_links
++ ; /* implicit from our own name-node */
346 * Make sure that we can find all the links that reference a node
347 * so that we can get them all if we need to zap the node.
349 if(dnp
->dn_linklist
) {
350 dirent_p
->de_nextlink
= dnp
->dn_linklist
;
351 dirent_p
->de_prevlinkp
= dirent_p
->de_nextlink
->de_prevlinkp
;
352 dirent_p
->de_nextlink
->de_prevlinkp
= &(dirent_p
->de_nextlink
);
353 *dirent_p
->de_prevlinkp
= dirent_p
;
355 dirent_p
->de_nextlink
= dirent_p
;
356 dirent_p
->de_prevlinkp
= &(dirent_p
->de_nextlink
);
358 dnp
->dn_linklist
= dirent_p
;
361 * If the node is a directory, then we need to handle the
362 * creation of the .. link.
363 * A NULL dirnode indicates a root node, so point to ourself.
365 if(dnp
->dn_type
== DEV_DIR
) {
366 dnp
->dn_typeinfo
.Dir
.myname
= dirent_p
;
368 * If we are unlinking from an old dir, decrement its links
369 * as we point our '..' elsewhere
370 * Note: it's up to the calling code to remove the
371 * us from the original directory's list
373 if(dnp
->dn_typeinfo
.Dir
.parent
) {
374 dnp
->dn_typeinfo
.Dir
.parent
->dn_links
--;
377 dnp
->dn_typeinfo
.Dir
.parent
= dirnode
;
379 dnp
->dn_typeinfo
.Dir
.parent
= dnp
;
381 dnp
->dn_typeinfo
.Dir
.parent
->dn_links
++; /* account for the new '..' */
385 * put the name into the directory entry.
387 strcpy(dirent_p
->de_name
, name
);
391 * Check if we are not making a root node..
396 * Put it on the END of the linked list of directory entries
398 dirent_p
->de_parent
= dirnode
; /* null for root */
399 dirent_p
->de_prevp
= dirnode
->dn_typeinfo
.Dir
.dirlast
;
400 dirent_p
->de_next
= *(dirent_p
->de_prevp
); /* should be NULL */
402 *(dirent_p
->de_prevp
) = dirent_p
;
403 dirnode
->dn_typeinfo
.Dir
.dirlast
= &(dirent_p
->de_next
);
404 dirnode
->dn_typeinfo
.Dir
.entrycount
++;
405 dirnode
->dn_len
+= strlen(name
) + 8;/*ok, ok?*/
408 *dirent_pp
= dirent_p
;
409 DEVFS_INCR_ENTRIES();
414 /***********************************************************************
415 * Add a new element to the devfs plane.
417 * Creates a new dev_node to go with it if the prototype should not be
418 * reused. (Is a DIR, or we select SPLIT_DEVS at compile time)
419 * typeinfo gives us info to make our node if we don't have a prototype.
420 * If typeinfo is null and proto exists, then the typeinfo field of
421 * the proto is used intead in the CREATE case.
422 * note the 'links' count is 0 (except if a dir)
423 * but it is only cleared on a transition
424 * so this is ok till we link it to something
425 * Even in SPLIT_DEVS mode,
426 * if the node already exists on the wanted plane, just return it
428 * called with DEVFS_LOCK held
429 ***********************************************************************/
431 dev_add_node(int entrytype
, devnode_type_t
* typeinfo
, devnode_t
* proto
,
432 devnode_t
* *dn_pp
, struct devfsmount
*dvm
)
434 devnode_t
* dnp
= NULL
;
436 #if defined SPLIT_DEVS
438 * If we have a prototype, then check if there is already a sibling
439 * on the mount plane we are looking at, if so, just return it.
442 dnp
= proto
->dn_nextsibling
;
443 while( dnp
!= proto
) {
444 if (dnp
->dn_dvm
== dvm
) {
448 dnp
= dnp
->dn_nextsibling
;
450 if (typeinfo
== NULL
)
451 typeinfo
= &(proto
->dn_typeinfo
);
453 #else /* SPLIT_DEVS */
455 switch (proto
->type
) {
462 #endif /* SPLIT_DEVS */
463 MALLOC(dnp
, devnode_t
*, sizeof(devnode_t
), M_DEVFSNODE
, M_WAITOK
);
469 * If we have a proto, that means that we are duplicating some
470 * other device, which can only happen if we are not at the back plane
473 bcopy(proto
, dnp
, sizeof(devnode_t
));
475 dnp
->dn_linklist
= NULL
;
478 /* add to END of siblings list */
479 dnp
->dn_prevsiblingp
= proto
->dn_prevsiblingp
;
480 *(dnp
->dn_prevsiblingp
) = dnp
;
481 dnp
->dn_nextsibling
= proto
;
482 proto
->dn_prevsiblingp
= &(dnp
->dn_nextsibling
);
487 * We have no prototype, so start off with a clean slate
490 bzero(dnp
, sizeof(devnode_t
));
491 dnp
->dn_type
= entrytype
;
492 dnp
->dn_nextsibling
= dnp
;
493 dnp
->dn_prevsiblingp
= &(dnp
->dn_nextsibling
);
494 dnp
->dn_atime
.tv_sec
= tv
.tv_sec
;
495 dnp
->dn_mtime
.tv_sec
= tv
.tv_sec
;
496 dnp
->dn_ctime
.tv_sec
= tv
.tv_sec
;
501 * fill out the dev node according to type
506 * As it's a directory, make sure
507 * it has a null entries list
509 dnp
->dn_typeinfo
.Dir
.dirlast
= &(dnp
->dn_typeinfo
.Dir
.dirlist
);
510 dnp
->dn_typeinfo
.Dir
.dirlist
= (devdirent_t
*)0;
511 dnp
->dn_typeinfo
.Dir
.entrycount
= 0;
512 /* until we know better, it has a null parent pointer*/
513 dnp
->dn_typeinfo
.Dir
.parent
= NULL
;
514 dnp
->dn_links
++; /* for .*/
515 dnp
->dn_typeinfo
.Dir
.myname
= NULL
;
517 * make sure that the ops associated with it are the ops
518 * that we use (by default) for directories
520 dnp
->dn_ops
= &devfs_vnodeop_p
;
521 dnp
->dn_mode
|= 0555; /* default perms */
525 * As it's a symlink allocate and store the link info
526 * Symlinks should only ever be created by the user,
527 * so they are not on the back plane and should not be
528 * propogated forward.. a bit like directories in that way..
529 * A symlink only exists on one plane and has its own
530 * node.. therefore we might be on any random plane.
532 MALLOC(dnp
->dn_typeinfo
.Slnk
.name
, char *,
533 typeinfo
->Slnk
.namelen
+1,
534 M_DEVFSNODE
, M_WAITOK
);
535 if (!dnp
->dn_typeinfo
.Slnk
.name
) {
536 FREE(dnp
,M_DEVFSNODE
);
539 strncpy(dnp
->dn_typeinfo
.Slnk
.name
, typeinfo
->Slnk
.name
,
540 typeinfo
->Slnk
.namelen
);
541 dnp
->dn_typeinfo
.Slnk
.name
[typeinfo
->Slnk
.namelen
] = '\0';
542 dnp
->dn_typeinfo
.Slnk
.namelen
= typeinfo
->Slnk
.namelen
;
543 DEVFS_INCR_STRINGSPACE(dnp
->dn_typeinfo
.Slnk
.namelen
+ 1);
544 dnp
->dn_ops
= &devfs_vnodeop_p
;
545 dnp
->dn_mode
|= 0555; /* default perms */
550 * Make sure it has DEVICE type ops
551 * and device specific fields are correct
553 dnp
->dn_ops
= &devfs_spec_vnodeop_p
;
554 dnp
->dn_typeinfo
.dev
= typeinfo
->dev
;
566 /***********************************************************************
567 * called with DEVFS_LOCK held
568 **********************************************************************/
570 devnode_free(devnode_t
* dnp
)
572 if (dnp
->dn_lflags
& DN_BUSY
) {
573 dnp
->dn_lflags
|= DN_DELETE
;
576 if (dnp
->dn_type
== DEV_SLNK
) {
577 DEVFS_DECR_STRINGSPACE(dnp
->dn_typeinfo
.Slnk
.namelen
+ 1);
578 FREE(dnp
->dn_typeinfo
.Slnk
.name
, M_DEVFSNODE
);
581 FREE(dnp
, M_DEVFSNODE
);
585 /***********************************************************************
586 * called with DEVFS_LOCK held
587 **********************************************************************/
589 devfs_dn_free(devnode_t
* dnp
)
591 if(--dnp
->dn_links
<= 0 ) /* can be -1 for initial free, on error */
593 /*probably need to do other cleanups XXX */
594 if (dnp
->dn_nextsibling
!= dnp
) {
595 devnode_t
* * prevp
= dnp
->dn_prevsiblingp
;
596 *prevp
= dnp
->dn_nextsibling
;
597 dnp
->dn_nextsibling
->dn_prevsiblingp
= prevp
;
600 if (dnp
->dn_vn
== NULL
) {
601 devnode_free(dnp
); /* no accesses/references */
604 dnp
->dn_delete
= TRUE
;
609 /***********************************************************************\
610 * Front Node Operations *
611 * Add or delete a chain of front nodes *
612 \***********************************************************************/
615 /***********************************************************************
616 * Given a directory backing node, and a child backing node, add the
617 * appropriate front nodes to the front nodes of the directory to
618 * represent the child node to the user
620 * on failure, front nodes will either be correct or not exist for each
621 * front dir, however dirs completed will not be stripped of completed
622 * frontnodes on failure of a later frontnode
624 * This allows a new node to be propogated through all mounted planes
626 * called with DEVFS_LOCK held
627 ***********************************************************************/
629 devfs_propogate(devdirent_t
* parent
,devdirent_t
* child
)
632 devdirent_t
* newnmp
;
633 devnode_t
* dnp
= child
->de_dnp
;
634 devnode_t
* pdnp
= parent
->de_dnp
;
635 devnode_t
* adnp
= parent
->de_dnp
;
636 int type
= child
->de_dnp
->dn_type
;
638 /***********************************************
639 * Find the other instances of the parent node
640 ***********************************************/
641 for (adnp
= pdnp
->dn_nextsibling
;
643 adnp
= adnp
->dn_nextsibling
)
646 * Make the node, using the original as a prototype)
647 * if the node already exists on that plane it won't be
650 if ((error
= dev_add_entry(child
->de_name
, adnp
, type
,
651 NULL
, dnp
, adnp
->dn_dvm
,
653 printf("duplicating %s failed\n",child
->de_name
);
656 return 0; /* for now always succeed */
660 /***********************************************************************
661 * remove all instances of this devicename [for backing nodes..]
662 * note.. if there is another link to the node (non dir nodes only)
663 * then the devfs_node will still exist as the ref count will be non-0
664 * removing a directory node will remove all sup-nodes on all planes (ZAP)
666 * Used by device drivers to remove nodes that are no longer relevant
667 * The argument is the 'cookie' they were given when they created the node
668 * this function is exported.. see devfs.h
669 ***********************************************************************/
671 devfs_remove(void *dirent_p
)
673 devnode_t
* dnp
= ((devdirent_t
*)dirent_p
)->de_dnp
;
680 printf("devfs_remove: not ready for devices!\n");
684 /* keep removing the next sibling till only we exist. */
685 while ((dnp2
= dnp
->dn_nextsibling
) != dnp
) {
688 * Keep removing the next front node till no more exist
690 dnp
->dn_nextsibling
= dnp2
->dn_nextsibling
;
691 dnp
->dn_nextsibling
->dn_prevsiblingp
= &(dnp
->dn_nextsibling
);
692 dnp2
->dn_nextsibling
= dnp2
;
693 dnp2
->dn_prevsiblingp
= &(dnp2
->dn_nextsibling
);
694 if (dnp2
->dn_linklist
) {
696 lastlink
= (1 == dnp2
->dn_links
);
697 dev_free_name(dnp2
->dn_linklist
);
703 * then free the main node
704 * If we are not running in SPLIT_DEVS mode, then
705 * THIS is what gets rid of the propogated nodes.
707 if (dnp
->dn_linklist
) {
709 lastlink
= (1 == dnp
->dn_links
);
710 dev_free_name(dnp
->dn_linklist
);
721 /***************************************************************
722 * duplicate the backing tree into a tree of nodes hung off the
723 * mount point given as the argument. Do this by
724 * calling dev_dup_entry which recurses all the way
727 * called with DEVFS_LOCK held
728 **************************************************************/
730 dev_dup_plane(struct devfsmount
*devfs_mp_p
)
735 if ((error
= dev_dup_entry(NULL
, dev_root
, &new, devfs_mp_p
)))
737 devfs_mp_p
->plane_root
= new;
743 /***************************************************************
746 * called with DEVFS_LOCK held
747 ***************************************************************/
749 devfs_free_plane(struct devfsmount
*devfs_mp_p
)
751 devdirent_t
* dirent_p
;
753 dirent_p
= devfs_mp_p
->plane_root
;
755 dev_free_hier(dirent_p
);
756 dev_free_name(dirent_p
);
758 devfs_mp_p
->plane_root
= NULL
;
762 /***************************************************************
763 * Create and link in a new front element..
764 * Parent can be 0 for a root node
765 * Not presently usable to make a symlink XXX
766 * (Ok, symlinks don't propogate)
767 * recursively will create subnodes corresponding to equivalent
768 * child nodes in the base level
770 * called with DEVFS_LOCK held
771 ***************************************************************/
773 dev_dup_entry(devnode_t
* parent
, devdirent_t
* back
, devdirent_t
* *dnm_pp
,
774 struct devfsmount
*dvm
)
776 devdirent_t
* entry_p
;
777 devdirent_t
* newback
;
778 devdirent_t
* newfront
;
780 devnode_t
* dnp
= back
->de_dnp
;
781 int type
= dnp
->dn_type
;
784 * go get the node made (if we need to)
785 * use the back one as a prototype
787 if ((error
= dev_add_entry(back
->de_name
, parent
, type
,
789 parent
?parent
->dn_dvm
:dvm
, &entry_p
)) != 0) {
790 printf("duplicating %s failed\n",back
->de_name
);
794 * If we have just made the root, then insert the pointer to the
798 entry_p
->de_dnp
->dn_dvm
= dvm
;
802 * If it is a directory, then recurse down all the other
804 * note that this time we don't pass on the mount info..
808 for(newback
= back
->de_dnp
->dn_typeinfo
.Dir
.dirlist
;
809 newback
; newback
= newback
->de_next
)
811 if((error
= dev_dup_entry(entry_p
->de_dnp
,
812 newback
, &newfront
, NULL
)) != 0)
814 break; /* back out with an error */
823 /***************************************************************
825 * remember that if there are other names pointing to the
826 * dev_node then it may not get freed yet
827 * can handle if there is no dnp
829 * called with DEVFS_LOCK held
830 ***************************************************************/
833 dev_free_name(devdirent_t
* dirent_p
)
835 devnode_t
* parent
= dirent_p
->de_parent
;
836 devnode_t
* dnp
= dirent_p
->de_dnp
;
839 if(dnp
->dn_type
== DEV_DIR
)
843 if(dnp
->dn_typeinfo
.Dir
.dirlist
)
845 p
= dnp
->dn_typeinfo
.Dir
.parent
;
846 devfs_dn_free(dnp
); /* account for '.' */
847 devfs_dn_free(p
); /* '..' */
850 * unlink us from the list of links for this node
851 * If we are the only link, it's easy!
852 * if we are a DIR of course there should not be any
855 if(dirent_p
->de_nextlink
== dirent_p
) {
856 dnp
->dn_linklist
= NULL
;
858 if(dnp
->dn_linklist
== dirent_p
) {
859 dnp
->dn_linklist
= dirent_p
->de_nextlink
;
861 dirent_p
->de_nextlink
->de_prevlinkp
862 = dirent_p
->de_prevlinkp
;
863 *dirent_p
->de_prevlinkp
= dirent_p
->de_nextlink
;
869 * unlink ourselves from the directory on this plane
871 if(parent
) /* if not fs root */
873 if( (*dirent_p
->de_prevp
= dirent_p
->de_next
) )/* yes, assign */
875 dirent_p
->de_next
->de_prevp
= dirent_p
->de_prevp
;
879 parent
->dn_typeinfo
.Dir
.dirlast
880 = dirent_p
->de_prevp
;
882 parent
->dn_typeinfo
.Dir
.entrycount
--;
883 parent
->dn_len
-= strlen(dirent_p
->de_name
) + 8;
886 DEVFS_DECR_ENTRIES();
887 FREE(dirent_p
, M_DEVFSNAME
);
892 /***************************************************************
893 * Free a hierarchy starting at a directory node name
894 * remember that if there are other names pointing to the
895 * dev_node then it may not get freed yet
896 * can handle if there is no dnp
897 * leave the node itself allocated.
899 * called with DEVFS_LOCK held
900 ***************************************************************/
903 dev_free_hier(devdirent_t
* dirent_p
)
905 devnode_t
* dnp
= dirent_p
->de_dnp
;
908 if(dnp
->dn_type
== DEV_DIR
)
910 while(dnp
->dn_typeinfo
.Dir
.dirlist
)
912 dev_free_hier(dnp
->dn_typeinfo
.Dir
.dirlist
);
913 dev_free_name(dnp
->dn_typeinfo
.Dir
.dirlist
);
920 /***************************************************************
921 * given a dev_node, find the appropriate vnode if one is already
922 * associated, or get a new one and associate it with the dev_node
924 * called with DEVFS_LOCK held
925 ***************************************************************/
927 devfs_dntovn(devnode_t
* dnp
, struct vnode
**vn_pp
, __unused
struct proc
* p
)
931 struct vnode_fsparam vfsp
;
932 enum vtype vtype
= 0;
939 dnp
->dn_lflags
|= DN_BUSY
;
941 if (vn_p
) { /* already has a vnode */
944 vid
= vnode_vid(vn_p
);
948 error
= vnode_getwithvid(vn_p
, vid
);
952 if (dnp
->dn_lflags
& DN_DELETE
) {
954 * our BUSY node got marked for
955 * deletion while the DEVFS lock
960 * vnode_getwithvid returned a valid ref
961 * which we need to drop
966 * set the error to EAGAIN
967 * which will cause devfs_lookup
975 devfs_release_busy(dnp
);
980 if (dnp
->dn_lflags
& DN_CREATE
) {
981 dnp
->dn_lflags
|= DN_CREATEWAIT
;
982 msleep(&dnp
->dn_lflags
, &devfs_mutex
, PRIBIO
, 0 , 0);
986 dnp
->dn_lflags
|= DN_CREATE
;
988 switch (dnp
->dn_type
) {
993 if (dnp
->dn_typeinfo
.Dir
.parent
== dnp
) {
1000 vtype
= (dnp
->dn_type
== DEV_BDEV
) ? VBLK
: VCHR
;
1003 vfsp
.vnfs_mp
= dnp
->dn_dvm
->mount
;
1004 vfsp
.vnfs_vtype
= vtype
;
1005 vfsp
.vnfs_str
= "devfs";
1007 vfsp
.vnfs_fsnode
= dnp
;
1009 vfsp
.vnfs_vops
= *(dnp
->dn_ops
);
1011 if (vtype
== VBLK
|| vtype
== VCHR
)
1012 vfsp
.vnfs_rdev
= dnp
->dn_typeinfo
.dev
;
1015 vfsp
.vnfs_filesize
= 0;
1016 vfsp
.vnfs_flags
= VNFS_NOCACHE
| VNFS_CANTCACHE
;
1017 /* Tag system files */
1018 vfsp
.vnfs_marksystem
= 0;
1019 vfsp
.vnfs_markroot
= markroot
;
1023 error
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, &vn_p
);
1029 panic("devnode already has a vnode?");
1033 vnode_settag(vn_p
, VT_DEVFS
);
1037 dnp
->dn_lflags
&= ~DN_CREATE
;
1039 if (dnp
->dn_lflags
& DN_CREATEWAIT
) {
1040 dnp
->dn_lflags
&= ~DN_CREATEWAIT
;
1041 wakeup(&dnp
->dn_lflags
);
1044 devfs_release_busy(dnp
);
1050 /***********************************************************************
1051 * called with DEVFS_LOCK held
1052 ***********************************************************************/
1054 devfs_release_busy(devnode_t
*dnp
) {
1056 dnp
->dn_lflags
&= ~DN_BUSY
;
1058 if (dnp
->dn_lflags
& DN_DELETE
)
1062 /***********************************************************************
1063 * add a whole device, with no prototype.. make name element and node
1064 * Used for adding the original device entries
1066 * called with DEVFS_LOCK held
1067 ***********************************************************************/
1069 dev_add_entry(char *name
, devnode_t
* parent
, int type
, devnode_type_t
* typeinfo
,
1070 devnode_t
* proto
, struct devfsmount
*dvm
, devdirent_t
* *nm_pp
)
1075 if ((error
= dev_add_node(type
, typeinfo
, proto
, &dnp
,
1076 (parent
?parent
->dn_dvm
:dvm
))) != 0)
1078 printf("devfs: %s: base node allocation failed (Errno=%d)\n",
1082 if ((error
= dev_add_name(name
,parent
,NULL
, dnp
, nm_pp
)) != 0)
1084 devfs_dn_free(dnp
); /* 1->0 for dir, 0->(-1) for other */
1085 printf("devfs: %s: name slot allocation failed (Errno=%d)\n",
1094 * Function: devfs_make_node
1097 * Create a device node with the given pathname in the devfs namespace.
1100 * dev - the dev_t value to associate
1101 * chrblk - block or character device (DEVFS_CHAR or DEVFS_BLOCK)
1102 * uid, gid - ownership
1103 * perms - permissions
1104 * fmt, ... - path format string with printf args to format the path name
1106 * A handle to a device node if successful, NULL otherwise.
1109 devfs_make_node(dev_t dev
, int chrblk
, uid_t uid
,
1110 gid_t gid
, int perms
, const char *fmt
, ...)
1112 devdirent_t
* new_dev
= NULL
;
1113 devnode_t
* dnp
; /* devnode for parent directory */
1114 devnode_type_t typeinfo
;
1116 char *name
, *path
, buf
[256]; /* XXX */
1124 printf("devfs_make_node: not ready for devices!\n");
1127 if (chrblk
!= DEVFS_CHAR
&& chrblk
!= DEVFS_BLOCK
)
1133 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1138 for(i
=strlen(buf
); i
>0; i
--)
1154 /* find/create directory path ie. mkdir -p */
1155 if (dev_finddir(path
, NULL
, CREATE
, &dnp
) == 0) {
1157 if (dev_add_entry(name
, dnp
,
1158 (chrblk
== DEVFS_CHAR
) ? DEV_CDEV
: DEV_BDEV
,
1159 &typeinfo
, NULL
, NULL
, &new_dev
) == 0) {
1160 new_dev
->de_dnp
->dn_gid
= gid
;
1161 new_dev
->de_dnp
->dn_uid
= uid
;
1162 new_dev
->de_dnp
->dn_mode
|= perms
;
1163 devfs_propogate(dnp
->dn_typeinfo
.Dir
.myname
, new_dev
);
1173 * Function: devfs_make_link
1176 * Create a link to a previously created device node.
1179 * 0 if successful, -1 if failed
1182 devfs_make_link(void *original
, char *fmt
, ...)
1184 devdirent_t
* new_dev
= NULL
;
1185 devdirent_t
* orig
= (devdirent_t
*) original
;
1186 devnode_t
* dirnode
; /* devnode for parent directory */
1189 char *p
, buf
[256]; /* XXX */
1195 printf("devfs_make_link: not ready for devices!\n");
1201 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1206 for(i
=strlen(buf
); i
>0; i
--) {
1218 if (dev_finddir(buf
, NULL
, CREATE
, &dirnode
)
1219 || dev_add_name(p
, dirnode
, NULL
, orig
->de_dnp
, &new_dev
))
1222 if (dev_finddir("", NULL
, CREATE
, &dirnode
)
1223 || dev_add_name(buf
, dirnode
, NULL
, orig
->de_dnp
, &new_dev
))
1226 devfs_propogate(dirnode
->dn_typeinfo
.Dir
.myname
, new_dev
);
1231 return ((new_dev
!= NULL
) ? 0 : -1);