]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 * Copyright 1997,1998 Julian Elischer. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions are
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright notice,
36 * this list of conditions and the following disclaimer in the documentation
37 * and/or other materials provided with the distribution.
39 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
40 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
41 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42 * DISCLAIMED. IN NO EVENT SHALL THE HOLDER OR CONTRIBUTORS BE LIABLE FOR
43 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * Dieter Siegmund (dieter@apple.com) Thu Apr 8 14:08:19 PDT 1999
57 * - removed mounting of "hidden" mountpoint
58 * - fixed problem in which devnode->dn_vn pointer was not
59 * updated with the vnode returned from checkalias()
60 * - replaced devfs_vntodn() with a macro VTODN()
61 * - rewrote dev_finddir() to not use recursion
62 * - added locking to avoid data structure corruption (DEVFS_(UN)LOCK())
63 * Dieter Siegmund (dieter@apple.com) Wed Jul 14 13:37:59 PDT 1999
64 * - fixed problem with devfs_dntovn() checking the v_id against the
65 * value cached in the device node; a union mount on top of us causes
66 * the v_id to get incremented thus, we would end up returning a new
67 * vnode instead of the existing one that has the mounted_here
68 * field filled in; the net effect was that the filesystem mounted
69 * on top of us would never show up
70 * - added devfs_stats to store how many data structures are actually
74 /* SPLIT_DEVS means each devfs uses a different devnode for the same device */
75 /* Otherwise the same device always ends up at the same vnode even if */
76 /* reached througgh a different devfs instance. The practical difference */
77 /* is that with the same vnode, chmods and chowns show up on all instances of */
80 #define SPLIT_DEVS 1 /* maybe make this an option */
81 /*#define SPLIT_DEVS 1*/
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/kernel.h>
87 #include <sys/malloc.h>
88 #include <sys/mount.h>
90 #include <sys/vnode.h>
94 #include "devfsdefs.h"
96 struct lock__bsd__ devfs_lock
; /* the "big switch" */
97 devdirent_t
* dev_root
= NULL
; /* root of backing tree */
98 struct devfs_stats devfs_stats
; /* hold stats */
100 #ifdef HIDDEN_MOUNTPOINT
101 static struct mount
*devfs_hidden_mount
;
102 #endif /* HIDDEN_MOINTPOINT */
104 static int devfs_ready
= 0;
106 #define NOCREATE FALSE
110 * Set up the root directory node in the backing plane
111 * This is happenning before the vfs system has been
112 * set up yet, so be careful about what we reference..
113 * Notice that the ops are by indirection.. as they haven't
115 * DEVFS has a hidden mountpoint that is used as the anchor point
116 * for the internal 'blueprint' version of the dev filesystem tree.
122 lockinit(&devfs_lock
, PINOD
, "devfs", 0, 0);
123 if (dev_add_entry("root", NULL
, DEV_DIR
, NULL
, NULL
, NULL
,
125 printf("devfs_sinit: dev_add_entry failed ");
128 #ifdef HIDDEN_MOUNTPOINT
129 MALLOC(devfs_hidden_mount
, struct mount
*, sizeof(struct mount
),
131 bzero(devfs_hidden_mount
,sizeof(struct mount
));
133 /* Initialize the default IO constraints */
134 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
135 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
137 devfs_mount(devfs_hidden_mount
,"dummy",NULL
,NULL
,NULL
);
138 dev_root
->de_dnp
->dn_dvm
139 = (struct devfsmount
*)devfs_hidden_mount
->mnt_data
;
140 #endif /* HIDDEN_MOUNTPOINT */
145 /***********************************************************************\
146 *************************************************************************
147 * Routines used to find our way to a point in the tree *
148 *************************************************************************
149 \***********************************************************************/
152 /***************************************************************\
153 * Search down the linked list off a dir to find "name" *
154 * return the devnode_t * for that node.
155 \***************************************************************/
158 dev_findname(devnode_t
* dir
,char *name
)
161 if (dir
->dn_type
!= DEV_DIR
) return 0;/*XXX*/ /* printf?*/
167 return dir
->dn_typeinfo
.Dir
.myname
;
169 if((name
[1] == '.') && (name
[2] == 0))
171 /* for root, .. == . */
172 return dir
->dn_typeinfo
.Dir
.parent
->dn_typeinfo
.Dir
.myname
;
175 newfp
= dir
->dn_typeinfo
.Dir
.dirlist
;
178 if(!(strcmp(name
,newfp
->de_name
)))
180 newfp
= newfp
->de_next
;
186 /***********************************************************************\
187 * Given a starting node (0 for root) and a pathname, return the node *
188 * for the end item on the path. It MUST BE A DIRECTORY. If the 'CREATE' *
189 * option is true, then create any missing nodes in the path and create *
190 * and return the final node as well. *
191 * This is used to set up a directory, before making nodes in it.. *
193 * Warning: This function is RECURSIVE. *
194 \***********************************************************************/
196 dev_finddir(char * orig_path
, /* find this dir (err if not dir) */
197 devnode_t
* dirnode
, /* starting point */
198 int create
, /* create path? */
199 devnode_t
* * dn_pp
) /* returned */
201 devdirent_t
* dirent_p
;
202 devnode_t
* dnp
= NULL
;
203 char pathbuf
[DEVMAXPATHSIZE
];
210 /***************************************\
211 * If no parent directory is given *
212 * then start at the root of the tree *
213 \***************************************/
214 if(!dirnode
) dirnode
= dev_root
->de_dnp
;
216 /***************************************\
218 \***************************************/
219 if (dirnode
->dn_type
!= DEV_DIR
) return ENOTDIR
;
220 if(strlen(orig_path
) > (DEVMAXPATHSIZE
- 1)) return ENAMETOOLONG
;
224 strcpy(path
,orig_path
);
226 /***************************************\
227 * always absolute, skip leading / *
228 * get rid of / or // or /// etc. *
229 \***************************************/
230 while(*path
== '/') path
++;
232 /***************************************\
233 * If nothing left, then parent was it.. *
234 \***************************************/
235 if ( *path
== '\0' ) {
240 /***************************************\
241 * find the next segment of the name *
242 \***************************************/
244 while((*cp
!= '/') && (*cp
!= 0)) {
248 /***********************************************\
249 * Check to see if it's the last component *
250 \***********************************************/
252 path
= cp
+ 1; /* path refers to the rest */
253 *cp
= 0; /* name is now a separate string */
255 path
= (char *)0; /* was trailing slash */
258 path
= NULL
; /* no more to do */
261 /***************************************\
262 * Start scanning along the linked list *
263 \***************************************/
264 dirent_p
= dev_findname(dirnode
,name
);
265 if(dirent_p
) { /* check it's a directory */
266 dnp
= dirent_p
->de_dnp
;
267 if(dnp
->dn_type
!= DEV_DIR
) return ENOTDIR
;
269 /***************************************\
270 * The required element does not exist *
271 * So we will add it if asked to. *
272 \***************************************/
273 if(!create
) return ENOENT
;
275 if((retval
= dev_add_entry(name
, dirnode
,
276 DEV_DIR
, NULL
, NULL
, NULL
,
280 dnp
= dirent_p
->de_dnp
;
281 devfs_propogate(dirnode
->dn_typeinfo
.Dir
.myname
,dirent_p
);
283 if(path
!= NULL
) { /* decide whether to recurse more or return */
284 return (dev_finddir(path
,dnp
,create
,dn_pp
));
291 /***********************************************************************\
292 * Given a starting node (0 for root) and a pathname, return the node *
293 * for the end item on the path. It MUST BE A DIRECTORY. If the 'CREATE' *
294 * option is true, then create any missing nodes in the path and create *
295 * and return the final node as well. *
296 * This is used to set up a directory, before making nodes in it.. *
297 \***********************************************************************/
300 dev_finddir(char * path
,
305 devnode_t
* dnp
= NULL
;
310 if (!dirnode
) /* dirnode == NULL means start at root */
311 dirnode
= dev_root
->de_dnp
;
313 if (dirnode
->dn_type
!= DEV_DIR
)
316 if (strlen(path
) > (DEVMAXPATHSIZE
- 1))
327 char component
[DEVMAXPATHSIZE
];
328 devdirent_t
* dirent_p
;
332 /* we hit the end of the string, we're done */
337 while (*scan
!= '/' && *scan
)
340 strncpy(component
, start
, scan
- start
);
341 component
[ scan
- start
] = '\0';
345 dirent_p
= dev_findname(dirnode
, component
);
347 dnp
= dirent_p
->de_dnp
;
348 if (dnp
->dn_type
!= DEV_DIR
) {
358 error
= dev_add_entry(component
, dirnode
,
359 DEV_DIR
, NULL
, NULL
, NULL
, &dirent_p
);
362 dnp
= dirent_p
->de_dnp
;
363 devfs_propogate(dirnode
->dn_typeinfo
.Dir
.myname
, dirent_p
);
365 dirnode
= dnp
; /* continue relative to this directory */
371 /***********************************************************************\
372 * Add a new NAME element to the devfs *
373 * If we're creating a root node, then dirname is NULL *
374 * Basically this creates a new namespace entry for the device node *
376 * Creates a name node, and links it to the supplied node *
377 \***********************************************************************/
380 dev_add_name(char * name
, devnode_t
* dirnode
, devdirent_t
* back
,
381 devnode_t
* dnp
, devdirent_t
* *dirent_pp
)
383 devdirent_t
* dirent_p
= NULL
;
385 if(dirnode
!= NULL
) {
386 if(dirnode
->dn_type
!= DEV_DIR
) return(ENOTDIR
);
388 if( dev_findname(dirnode
,name
))
392 * make sure the name is legal
393 * slightly misleading in the case of NULL
395 if (!name
|| (strlen(name
) > (DEVMAXNAMESIZE
- 1)))
396 return (ENAMETOOLONG
);
399 * Allocate and fill out a new directory entry
401 MALLOC(dirent_p
, devdirent_t
*, sizeof(devdirent_t
),
402 M_DEVFSNAME
, M_WAITOK
);
406 bzero(dirent_p
,sizeof(devdirent_t
));
408 /* inherrit our parent's mount info */ /*XXX*/
409 /* a kludge but.... */
410 if(dirnode
&& ( dnp
->dn_dvm
== NULL
)) {
411 dnp
->dn_dvm
= dirnode
->dn_dvm
;
412 /* if(!dnp->dn_dvm) printf("parent had null dvm "); */
416 * Link the two together
417 * include the implicit link in the count of links to the devnode..
418 * this stops it from being accidentally freed later.
420 dirent_p
->de_dnp
= dnp
;
421 dnp
->dn_links
++ ; /* implicit from our own name-node */
424 * Make sure that we can find all the links that reference a node
425 * so that we can get them all if we need to zap the node.
427 if(dnp
->dn_linklist
) {
428 dirent_p
->de_nextlink
= dnp
->dn_linklist
;
429 dirent_p
->de_prevlinkp
= dirent_p
->de_nextlink
->de_prevlinkp
;
430 dirent_p
->de_nextlink
->de_prevlinkp
= &(dirent_p
->de_nextlink
);
431 *dirent_p
->de_prevlinkp
= dirent_p
;
433 dirent_p
->de_nextlink
= dirent_p
;
434 dirent_p
->de_prevlinkp
= &(dirent_p
->de_nextlink
);
436 dnp
->dn_linklist
= dirent_p
;
439 * If the node is a directory, then we need to handle the
440 * creation of the .. link.
441 * A NULL dirnode indicates a root node, so point to ourself.
443 if(dnp
->dn_type
== DEV_DIR
) {
444 dnp
->dn_typeinfo
.Dir
.myname
= dirent_p
;
446 * If we are unlinking from an old dir, decrement its links
447 * as we point our '..' elsewhere
448 * Note: it's up to the calling code to remove the
449 * us from the original directory's list
451 if(dnp
->dn_typeinfo
.Dir
.parent
) {
452 dnp
->dn_typeinfo
.Dir
.parent
->dn_links
--;
455 dnp
->dn_typeinfo
.Dir
.parent
= dirnode
;
457 dnp
->dn_typeinfo
.Dir
.parent
= dnp
;
459 dnp
->dn_typeinfo
.Dir
.parent
->dn_links
++; /* account for the new '..' */
463 * put the name into the directory entry.
465 strcpy(dirent_p
->de_name
, name
);
469 * Check if we are not making a root node..
474 * Put it on the END of the linked list of directory entries
478 dirent_p
->de_parent
= dirnode
; /* null for root */
479 dirent_p
->de_prevp
= dirnode
->dn_typeinfo
.Dir
.dirlast
;
480 dirent_p
->de_next
= *(dirent_p
->de_prevp
); /* should be NULL */
482 *(dirent_p
->de_prevp
) = dirent_p
;
483 dirnode
->dn_typeinfo
.Dir
.dirlast
= &(dirent_p
->de_next
);
484 dirnode
->dn_typeinfo
.Dir
.entrycount
++;
485 dirnode
->dn_len
+= strlen(name
) + 8;/*ok, ok?*/
488 *dirent_pp
= dirent_p
;
489 DEVFS_INCR_ENTRIES();
494 /***********************************************************************\
495 * Add a new element to the devfs plane. *
497 * Creates a new dev_node to go with it if the prototype should not be *
498 * reused. (Is a DIR, or we select SPLIT_DEVS at compile time) *
499 * typeinfo gives us info to make our node if we don't have a prototype. *
500 * If typeinfo is null and proto exists, then the typeinfo field of *
501 * the proto is used intead in the CREATE case. *
502 * note the 'links' count is 0 (except if a dir) *
503 * but it is only cleared on a transition *
504 * so this is ok till we link it to something *
505 * Even in SPLIT_DEVS mode, *
506 * if the node already exists on the wanted plane, just return it *
507 \***********************************************************************/
510 dev_add_node(int entrytype
, devnode_type_t
* typeinfo
, devnode_t
* proto
,
511 devnode_t
* *dn_pp
, struct devfsmount
*dvm
)
513 devnode_t
* dnp
= NULL
;
515 #if defined SPLIT_DEVS
517 * If we have a prototype, then check if there is already a sibling
518 * on the mount plane we are looking at, if so, just return it.
521 dnp
= proto
->dn_nextsibling
;
522 while( dnp
!= proto
) {
523 if (dnp
->dn_dvm
== dvm
) {
527 dnp
= dnp
->dn_nextsibling
;
529 if (typeinfo
== NULL
)
530 typeinfo
= &(proto
->dn_typeinfo
);
532 #else /* SPLIT_DEVS */
534 switch (proto
->type
) {
541 #endif /* SPLIT_DEVS */
542 MALLOC(dnp
, devnode_t
*, sizeof(devnode_t
), M_DEVFSNODE
, M_WAITOK
);
548 * If we have a proto, that means that we are duplicating some
549 * other device, which can only happen if we are not at the back plane
552 bcopy(proto
, dnp
, sizeof(devnode_t
));
554 dnp
->dn_linklist
= NULL
;
557 /* add to END of siblings list */
558 dnp
->dn_prevsiblingp
= proto
->dn_prevsiblingp
;
559 *(dnp
->dn_prevsiblingp
) = dnp
;
560 dnp
->dn_nextsibling
= proto
;
561 proto
->dn_prevsiblingp
= &(dnp
->dn_nextsibling
);
566 * We have no prototype, so start off with a clean slate
569 bzero(dnp
,sizeof(devnode_t
));
570 dnp
->dn_type
= entrytype
;
571 dnp
->dn_nextsibling
= dnp
;
572 dnp
->dn_prevsiblingp
= &(dnp
->dn_nextsibling
);
573 dnp
->dn_atime
.tv_sec
= tv
.tv_sec
;
574 dnp
->dn_mtime
.tv_sec
= tv
.tv_sec
;
575 dnp
->dn_ctime
.tv_sec
= tv
.tv_sec
;
580 * fill out the dev node according to type
585 * As it's a directory, make sure
586 * it has a null entries list
588 dnp
->dn_typeinfo
.Dir
.dirlast
= &(dnp
->dn_typeinfo
.Dir
.dirlist
);
589 dnp
->dn_typeinfo
.Dir
.dirlist
= (devdirent_t
*)0;
590 dnp
->dn_typeinfo
.Dir
.entrycount
= 0;
591 /* until we know better, it has a null parent pointer*/
592 dnp
->dn_typeinfo
.Dir
.parent
= NULL
;
593 dnp
->dn_links
++; /* for .*/
594 dnp
->dn_typeinfo
.Dir
.myname
= NULL
;
596 * make sure that the ops associated with it are the ops
597 * that we use (by default) for directories
599 dnp
->dn_ops
= &devfs_vnodeop_p
;
600 dnp
->dn_mode
|= 0555; /* default perms */
604 * As it's a symlink allocate and store the link info
605 * Symlinks should only ever be created by the user,
606 * so they are not on the back plane and should not be
607 * propogated forward.. a bit like directories in that way..
608 * A symlink only exists on one plane and has its own
609 * node.. therefore we might be on any random plane.
611 MALLOC(dnp
->dn_typeinfo
.Slnk
.name
, char *,
612 typeinfo
->Slnk
.namelen
+1,
613 M_DEVFSNODE
, M_WAITOK
);
614 if (!dnp
->dn_typeinfo
.Slnk
.name
) {
615 FREE(dnp
,M_DEVFSNODE
);
618 strncpy(dnp
->dn_typeinfo
.Slnk
.name
, typeinfo
->Slnk
.name
,
619 typeinfo
->Slnk
.namelen
);
620 dnp
->dn_typeinfo
.Slnk
.name
[typeinfo
->Slnk
.namelen
] = '\0';
621 dnp
->dn_typeinfo
.Slnk
.namelen
= typeinfo
->Slnk
.namelen
;
622 DEVFS_INCR_STRINGSPACE(dnp
->dn_typeinfo
.Slnk
.namelen
+ 1);
623 dnp
->dn_ops
= &devfs_vnodeop_p
;
624 dnp
->dn_mode
|= 0555; /* default perms */
629 * Make sure it has DEVICE type ops
630 * and device specific fields are correct
632 dnp
->dn_ops
= &devfs_spec_vnodeop_p
;
633 dnp
->dn_typeinfo
.dev
= typeinfo
->dev
;
647 devnode_free(devnode_t
* dnp
)
649 if (dnp
->dn_type
== DEV_SLNK
) {
650 DEVFS_DECR_STRINGSPACE(dnp
->dn_typeinfo
.Slnk
.namelen
+ 1);
651 FREE(dnp
->dn_typeinfo
.Slnk
.name
,M_DEVFSNODE
);
653 FREE(dnp
, M_DEVFSNODE
);
660 devfs_dn_free(devnode_t
* dnp
)
662 if(--dnp
->dn_links
<= 0 ) /* can be -1 for initial free, on error */
664 /*probably need to do other cleanups XXX */
665 if (dnp
->dn_nextsibling
!= dnp
) {
666 devnode_t
* * prevp
= dnp
->dn_prevsiblingp
;
667 *prevp
= dnp
->dn_nextsibling
;
668 dnp
->dn_nextsibling
->dn_prevsiblingp
= prevp
;
671 if (dnp
->dn_vn
== NULL
) {
673 printf("devfs_dn_free: free'ing %x\n", (unsigned int)dnp
);
675 devnode_free(dnp
); /* no accesses/references */
679 printf("devfs_dn_free: marking %x for deletion\n",
682 dnp
->dn_delete
= TRUE
;
687 /***********************************************************************\
688 * Front Node Operations *
689 * Add or delete a chain of front nodes *
690 \***********************************************************************/
692 /***********************************************************************\
693 * Given a directory backing node, and a child backing node, add the *
694 * appropriate front nodes to the front nodes of the directory to *
695 * represent the child node to the user *
697 * on failure, front nodes will either be correct or not exist for each *
698 * front dir, however dirs completed will not be stripped of completed *
699 * frontnodes on failure of a later frontnode *
701 * This allows a new node to be propogated through all mounted planes *
703 \***********************************************************************/
706 devfs_propogate(devdirent_t
* parent
,devdirent_t
* child
)
709 devdirent_t
* newnmp
;
710 devnode_t
* dnp
= child
->de_dnp
;
711 devnode_t
* pdnp
= parent
->de_dnp
;
712 devnode_t
* adnp
= parent
->de_dnp
;
713 int type
= child
->de_dnp
->dn_type
;
715 /***********************************************\
716 * Find the other instances of the parent node *
717 \***********************************************/
718 for (adnp
= pdnp
->dn_nextsibling
;
720 adnp
= adnp
->dn_nextsibling
)
723 * Make the node, using the original as a prototype)
724 * if the node already exists on that plane it won't be
727 if ((error
= dev_add_entry(child
->de_name
, adnp
, type
,
728 NULL
, dnp
, adnp
->dn_dvm
,
730 printf("duplicating %s failed\n",child
->de_name
);
733 return 0; /* for now always succeed */
736 /***********************************************************************
737 * remove all instances of this devicename [for backing nodes..]
738 * note.. if there is another link to the node (non dir nodes only)
739 * then the devfs_node will still exist as the ref count will be non-0
740 * removing a directory node will remove all sup-nodes on all planes (ZAP)
742 * Used by device drivers to remove nodes that are no longer relevant
743 * The argument is the 'cookie' they were given when they created the node
744 * this function is exported.. see devfs.h
745 ***********************************************************************/
747 devfs_remove(void *dirent_p
)
749 devnode_t
* dnp
= ((devdirent_t
*)dirent_p
)->de_dnp
;
751 boolean_t funnel_state
;
754 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
757 printf("devfs_remove: not ready for devices!\n");
763 /* keep removing the next sibling till only we exist. */
764 while((dnp2
= dnp
->dn_nextsibling
) != dnp
) {
767 * Keep removing the next front node till no more exist
769 dnp
->dn_nextsibling
= dnp2
->dn_nextsibling
;
770 dnp
->dn_nextsibling
->dn_prevsiblingp
= &(dnp
->dn_nextsibling
);
771 dnp2
->dn_nextsibling
= dnp2
;
772 dnp2
->dn_prevsiblingp
= &(dnp2
->dn_nextsibling
);
773 if(dnp2
->dn_linklist
) {
775 lastlink
= (1 == dnp2
->dn_links
);
776 dev_free_name(dnp2
->dn_linklist
);
782 * then free the main node
783 * If we are not running in SPLIT_DEVS mode, then
784 * THIS is what gets rid of the propogated nodes.
786 if(dnp
->dn_linklist
) {
788 lastlink
= (1 == dnp
->dn_links
);
789 dev_free_name(dnp
->dn_linklist
);
794 (void) thread_funnel_set(kernel_flock
, funnel_state
);
799 /***************************************************************
800 * duplicate the backing tree into a tree of nodes hung off the
801 * mount point given as the argument. Do this by
802 * calling dev_dup_entry which recurses all the way
804 **************************************************************/
807 dev_dup_plane(struct devfsmount
*devfs_mp_p
)
812 if ((error
= dev_dup_entry(NULL
, dev_root
, &new, devfs_mp_p
)))
814 devfs_mp_p
->plane_root
= new;
820 /***************************************************************\
822 \***************************************************************/
825 devfs_free_plane(struct devfsmount
*devfs_mp_p
)
827 devdirent_t
* dirent_p
;
829 dirent_p
= devfs_mp_p
->plane_root
;
831 dev_free_hier(dirent_p
);
832 dev_free_name(dirent_p
);
834 devfs_mp_p
->plane_root
= NULL
;
837 /***************************************************************\
838 * Create and link in a new front element.. *
839 * Parent can be 0 for a root node *
840 * Not presently usable to make a symlink XXX *
841 * (Ok, symlinks don't propogate)
842 * recursively will create subnodes corresponding to equivalent *
843 * child nodes in the base level *
844 \***************************************************************/
847 dev_dup_entry(devnode_t
* parent
, devdirent_t
* back
, devdirent_t
* *dnm_pp
,
848 struct devfsmount
*dvm
)
850 devdirent_t
* entry_p
;
851 devdirent_t
* newback
;
852 devdirent_t
* newfront
;
854 devnode_t
* dnp
= back
->de_dnp
;
855 int type
= dnp
->dn_type
;
858 * go get the node made (if we need to)
859 * use the back one as a prototype
861 if ((error
= dev_add_entry(back
->de_name
, parent
, type
,
863 parent
?parent
->dn_dvm
:dvm
, &entry_p
)) != 0) {
864 printf("duplicating %s failed\n",back
->de_name
);
868 * If we have just made the root, then insert the pointer to the
872 entry_p
->de_dnp
->dn_dvm
= dvm
;
876 * If it is a directory, then recurse down all the other
878 * note that this time we don't pass on the mount info..
882 for(newback
= back
->de_dnp
->dn_typeinfo
.Dir
.dirlist
;
883 newback
; newback
= newback
->de_next
)
885 if((error
= dev_dup_entry(entry_p
->de_dnp
,
886 newback
, &newfront
, NULL
)) != 0)
888 break; /* back out with an error */
896 /***************************************************************\
898 * remember that if there are other names pointing to the *
899 * dev_node then it may not get freed yet *
900 * can handle if there is no dnp *
901 \***************************************************************/
904 dev_free_name(devdirent_t
* dirent_p
)
906 devnode_t
* parent
= dirent_p
->de_parent
;
907 devnode_t
* dnp
= dirent_p
->de_dnp
;
910 if(dnp
->dn_type
== DEV_DIR
)
914 if(dnp
->dn_typeinfo
.Dir
.dirlist
)
916 p
= dnp
->dn_typeinfo
.Dir
.parent
;
917 devfs_dn_free(dnp
); /* account for '.' */
918 devfs_dn_free(p
); /* '..' */
921 * unlink us from the list of links for this node
922 * If we are the only link, it's easy!
923 * if we are a DIR of course there should not be any
926 if(dirent_p
->de_nextlink
== dirent_p
) {
927 dnp
->dn_linklist
= NULL
;
929 if(dnp
->dn_linklist
== dirent_p
) {
930 dnp
->dn_linklist
= dirent_p
->de_nextlink
;
932 dirent_p
->de_nextlink
->de_prevlinkp
933 = dirent_p
->de_prevlinkp
;
934 *dirent_p
->de_prevlinkp
= dirent_p
->de_nextlink
;
940 * unlink ourselves from the directory on this plane
942 if(parent
) /* if not fs root */
944 if( (*dirent_p
->de_prevp
= dirent_p
->de_next
) )/* yes, assign */
946 dirent_p
->de_next
->de_prevp
= dirent_p
->de_prevp
;
950 parent
->dn_typeinfo
.Dir
.dirlast
951 = dirent_p
->de_prevp
;
953 parent
->dn_typeinfo
.Dir
.entrycount
--;
954 parent
->dn_len
-= strlen(dirent_p
->de_name
) + 8;
957 DEVFS_DECR_ENTRIES();
958 FREE(dirent_p
,M_DEVFSNAME
);
962 /***************************************************************\
963 * Free a hierarchy starting at a directory node name *
964 * remember that if there are other names pointing to the *
965 * dev_node then it may not get freed yet *
966 * can handle if there is no dnp *
967 * leave the node itself allocated. *
968 \***************************************************************/
971 dev_free_hier(devdirent_t
* dirent_p
)
973 devnode_t
* dnp
= dirent_p
->de_dnp
;
976 if(dnp
->dn_type
== DEV_DIR
)
978 while(dnp
->dn_typeinfo
.Dir
.dirlist
)
980 dev_free_hier(dnp
->dn_typeinfo
.Dir
.dirlist
);
981 dev_free_name(dnp
->dn_typeinfo
.Dir
.dirlist
);
987 /***************************************************************\
988 * given a dev_node, find the appropriate vnode if one is already
989 * associated, or get a new one and associate it with the dev_node
990 \***************************************************************/
993 devfs_dntovn(devnode_t
* dnp
, struct vnode
**vn_pp
, struct proc
* p
)
995 struct vnode
*vn_p
, *nvp
;
1000 if (vn_p
) { /* already has a vnode */
1002 return(vget(vn_p
, LK_EXCLUSIVE
, p
));
1004 if (!(error
= getnewvnode(VT_DEVFS
, dnp
->dn_dvm
->mount
,
1005 *(dnp
->dn_ops
), &vn_p
))) {
1006 switch(dnp
->dn_type
) {
1008 vn_p
->v_type
= VLNK
;
1011 if (dnp
->dn_typeinfo
.Dir
.parent
== dnp
) {
1012 vn_p
->v_flag
|= VROOT
;
1014 vn_p
->v_type
= VDIR
;
1019 = (dnp
->dn_type
== DEV_BDEV
) ? VBLK
: VCHR
;
1020 if ((nvp
= checkalias(vn_p
, dnp
->dn_typeinfo
.dev
,
1021 dnp
->dn_dvm
->mount
)) != NULL
) {
1027 vn_p
->v_mount
= dnp
->dn_dvm
->mount
;/* XXX Duplicated */
1029 vn_p
->v_data
= (void *)dnp
;
1031 error
= vn_lock(vn_p
, LK_EXCLUSIVE
| LK_RETRY
, p
);
1036 /***********************************************************************\
1037 * add a whole device, with no prototype.. make name element and node *
1038 * Used for adding the original device entries *
1039 \***********************************************************************/
1042 dev_add_entry(char *name
, devnode_t
* parent
, int type
, devnode_type_t
* typeinfo
,
1043 devnode_t
* proto
, struct devfsmount
*dvm
, devdirent_t
* *nm_pp
)
1048 if ((error
= dev_add_node(type
, typeinfo
, proto
, &dnp
,
1049 (parent
?parent
->dn_dvm
:dvm
))) != 0)
1051 printf("devfs: %s: base node allocation failed (Errno=%d)\n",
1055 if ((error
= dev_add_name(name
,parent
,NULL
, dnp
, nm_pp
)) != 0)
1057 devfs_dn_free(dnp
); /* 1->0 for dir, 0->(-1) for other */
1058 printf("devfs: %s: name slot allocation failed (Errno=%d)\n",
1066 * Function: devfs_make_node
1069 * Create a device node with the given pathname in the devfs namespace.
1072 * dev - the dev_t value to associate
1073 * chrblk - block or character device (DEVFS_CHAR or DEVFS_BLOCK)
1074 * uid, gid - ownership
1075 * perms - permissions
1076 * fmt, ... - path format string with printf args to format the path name
1078 * A handle to a device node if successful, NULL otherwise.
1081 devfs_make_node(dev_t dev
, int chrblk
, uid_t uid
,
1082 gid_t gid
, int perms
, char *fmt
, ...)
1084 devdirent_t
* new_dev
= NULL
;
1085 devnode_t
* dnp
; /* devnode for parent directory */
1086 devnode_type_t typeinfo
;
1088 char *name
, *path
, buf
[256]; /* XXX */
1089 boolean_t funnel_state
;
1093 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1096 printf("devfs_make_node: not ready for devices!\n");
1100 if (chrblk
!= DEVFS_CHAR
&& chrblk
!= DEVFS_BLOCK
)
1104 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1109 for(i
=strlen(buf
); i
>0; i
--)
1125 /* find/create directory path ie. mkdir -p */
1126 if (dev_finddir(path
, NULL
, CREATE
, &dnp
) == 0) {
1128 if (dev_add_entry(name
, dnp
,
1129 (chrblk
== DEVFS_CHAR
) ? DEV_CDEV
: DEV_BDEV
,
1130 &typeinfo
, NULL
, NULL
, &new_dev
) == 0) {
1131 new_dev
->de_dnp
->dn_gid
= gid
;
1132 new_dev
->de_dnp
->dn_uid
= uid
;
1133 new_dev
->de_dnp
->dn_mode
|= perms
;
1134 devfs_propogate(dnp
->dn_typeinfo
.Dir
.myname
, new_dev
);
1140 (void) thread_funnel_set(kernel_flock
, funnel_state
);
1145 * Function: devfs_make_link
1148 * Create a link to a previously created device node.
1151 * 0 if successful, -1 if failed
1154 devfs_make_link(void *original
, char *fmt
, ...)
1156 devdirent_t
* new_dev
= NULL
;
1157 devdirent_t
* orig
= (devdirent_t
*) original
;
1158 devnode_t
* dirnode
; /* devnode for parent directory */
1161 char *p
, buf
[256]; /* XXX */
1163 boolean_t funnel_state
;
1165 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1168 printf("devfs_make_link: not ready for devices!\n");
1173 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
1178 for(i
=strlen(buf
); i
>0; i
--)
1187 if (dev_finddir(buf
, NULL
, CREATE
, &dirnode
)
1188 || dev_add_name(p
, dirnode
, NULL
, orig
->de_dnp
, &new_dev
))
1191 if (dev_finddir("", NULL
, CREATE
, &dirnode
)
1192 || dev_add_name(buf
, dirnode
, NULL
, orig
->de_dnp
, &new_dev
))
1195 devfs_propogate(dirnode
->dn_typeinfo
.Dir
.myname
, new_dev
);
1199 (void) thread_funnel_set(kernel_flock
, funnel_state
);
1200 return ((new_dev
!= NULL
) ? 0 : -1);