]>
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.h>
87 #include <sys/vnode.h>
91 #include "devfsdefs.h"
93 struct lock__bsd__ devfs_lock
; /* the "big switch" */
94 devdirent_t
* dev_root
= NULL
; /* root of backing tree */
95 struct devfs_stats devfs_stats
; /* hold stats */
97 #ifdef HIDDEN_MOUNTPOINT
98 static struct mount
*devfs_hidden_mount
;
99 #endif HIDDEN_MOINTPOINT
101 static int devfs_ready
= 0;
103 #define NOCREATE FALSE
107 * Set up the root directory node in the backing plane
108 * This is happenning before the vfs system has been
109 * set up yet, so be careful about what we reference..
110 * Notice that the ops are by indirection.. as they haven't
112 * DEVFS has a hidden mountpoint that is used as the anchor point
113 * for the internal 'blueprint' version of the dev filesystem tree.
119 lockinit(&devfs_lock
, PINOD
, "devfs", 0, 0);
120 if (dev_add_entry("root", NULL
, DEV_DIR
, NULL
, NULL
, NULL
,
122 printf("devfs_sinit: dev_add_entry failed ");
125 #ifdef HIDDEN_MOUNTPOINT
126 MALLOC(devfs_hidden_mount
, struct mount
*, sizeof(struct mount
),
128 bzero(devfs_hidden_mount
,sizeof(struct mount
));
130 /* Initialize the default IO constraints */
131 mp
->mnt_maxreadcnt
= mp
->mnt_maxwritecnt
= MAXPHYS
;
132 mp
->mnt_segreadcnt
= mp
->mnt_segwritecnt
= 32;
134 devfs_mount(devfs_hidden_mount
,"dummy",NULL
,NULL
,NULL
);
135 dev_root
->de_dnp
->dn_dvm
136 = (struct devfsmount
*)devfs_hidden_mount
->mnt_data
;
137 #endif HIDDEN_MOUNTPOINT
142 /***********************************************************************\
143 *************************************************************************
144 * Routines used to find our way to a point in the tree *
145 *************************************************************************
146 \***********************************************************************/
149 /***************************************************************\
150 * Search down the linked list off a dir to find "name" *
151 * return the devnode_t * for that node.
152 \***************************************************************/
155 dev_findname(devnode_t
* dir
,char *name
)
158 if (dir
->dn_type
!= DEV_DIR
) return 0;/*XXX*/ /* printf?*/
164 return dir
->dn_typeinfo
.Dir
.myname
;
166 if((name
[1] == '.') && (name
[2] == 0))
168 /* for root, .. == . */
169 return dir
->dn_typeinfo
.Dir
.parent
->dn_typeinfo
.Dir
.myname
;
172 newfp
= dir
->dn_typeinfo
.Dir
.dirlist
;
175 if(!(strcmp(name
,newfp
->de_name
)))
177 newfp
= newfp
->de_next
;
183 /***********************************************************************\
184 * Given a starting node (0 for root) and a pathname, return the node *
185 * for the end item on the path. It MUST BE A DIRECTORY. If the 'CREATE' *
186 * option is true, then create any missing nodes in the path and create *
187 * and return the final node as well. *
188 * This is used to set up a directory, before making nodes in it.. *
190 * Warning: This function is RECURSIVE. *
191 \***********************************************************************/
193 dev_finddir(char * orig_path
, /* find this dir (err if not dir) */
194 devnode_t
* dirnode
, /* starting point */
195 int create
, /* create path? */
196 devnode_t
* * dn_pp
) /* returned */
198 devdirent_t
* dirent_p
;
199 devnode_t
* dnp
= NULL
;
200 char pathbuf
[DEVMAXPATHSIZE
];
207 /***************************************\
208 * If no parent directory is given *
209 * then start at the root of the tree *
210 \***************************************/
211 if(!dirnode
) dirnode
= dev_root
->de_dnp
;
213 /***************************************\
215 \***************************************/
216 if (dirnode
->dn_type
!= DEV_DIR
) return ENOTDIR
;
217 if(strlen(orig_path
) > (DEVMAXPATHSIZE
- 1)) return ENAMETOOLONG
;
221 strcpy(path
,orig_path
);
223 /***************************************\
224 * always absolute, skip leading / *
225 * get rid of / or // or /// etc. *
226 \***************************************/
227 while(*path
== '/') path
++;
229 /***************************************\
230 * If nothing left, then parent was it.. *
231 \***************************************/
232 if ( *path
== '\0' ) {
237 /***************************************\
238 * find the next segment of the name *
239 \***************************************/
241 while((*cp
!= '/') && (*cp
!= 0)) {
245 /***********************************************\
246 * Check to see if it's the last component *
247 \***********************************************/
249 path
= cp
+ 1; /* path refers to the rest */
250 *cp
= 0; /* name is now a separate string */
252 path
= (char *)0; /* was trailing slash */
255 path
= NULL
; /* no more to do */
258 /***************************************\
259 * Start scanning along the linked list *
260 \***************************************/
261 dirent_p
= dev_findname(dirnode
,name
);
262 if(dirent_p
) { /* check it's a directory */
263 dnp
= dirent_p
->de_dnp
;
264 if(dnp
->dn_type
!= DEV_DIR
) return ENOTDIR
;
266 /***************************************\
267 * The required element does not exist *
268 * So we will add it if asked to. *
269 \***************************************/
270 if(!create
) return ENOENT
;
272 if((retval
= dev_add_entry(name
, dirnode
,
273 DEV_DIR
, NULL
, NULL
, NULL
,
277 dnp
= dirent_p
->de_dnp
;
278 devfs_propogate(dirnode
->dn_typeinfo
.Dir
.myname
,dirent_p
);
280 if(path
!= NULL
) { /* decide whether to recurse more or return */
281 return (dev_finddir(path
,dnp
,create
,dn_pp
));
288 /***********************************************************************\
289 * Given a starting node (0 for root) and a pathname, return the node *
290 * for the end item on the path. It MUST BE A DIRECTORY. If the 'CREATE' *
291 * option is true, then create any missing nodes in the path and create *
292 * and return the final node as well. *
293 * This is used to set up a directory, before making nodes in it.. *
294 \***********************************************************************/
297 dev_finddir(char * path
,
302 devnode_t
* dnp
= NULL
;
307 if (!dirnode
) /* dirnode == NULL means start at root */
308 dirnode
= dev_root
->de_dnp
;
310 if (dirnode
->dn_type
!= DEV_DIR
)
313 if (strlen(path
) > (DEVMAXPATHSIZE
- 1))
324 char component
[DEVMAXPATHSIZE
];
325 devdirent_t
* dirent_p
;
329 /* we hit the end of the string, we're done */
334 while (*scan
!= '/' && *scan
)
337 strncpy(component
, start
, scan
- start
);
341 dirent_p
= dev_findname(dirnode
, component
);
343 dnp
= dirent_p
->de_dnp
;
344 if (dnp
->dn_type
!= DEV_DIR
) {
354 error
= dev_add_entry(component
, dirnode
,
355 DEV_DIR
, NULL
, NULL
, NULL
, &dirent_p
);
358 dnp
= dirent_p
->de_dnp
;
359 devfs_propogate(dirnode
->dn_typeinfo
.Dir
.myname
, dirent_p
);
361 dirnode
= dnp
; /* continue relative to this directory */
367 /***********************************************************************\
368 * Add a new NAME element to the devfs *
369 * If we're creating a root node, then dirname is NULL *
370 * Basically this creates a new namespace entry for the device node *
372 * Creates a name node, and links it to the supplied node *
373 \***********************************************************************/
376 dev_add_name(char * name
, devnode_t
* dirnode
, devdirent_t
* back
,
377 devnode_t
* dnp
, devdirent_t
* *dirent_pp
)
379 devdirent_t
* dirent_p
= NULL
;
381 if(dirnode
!= NULL
) {
382 if(dirnode
->dn_type
!= DEV_DIR
) return(ENOTDIR
);
384 if( dev_findname(dirnode
,name
))
388 * make sure the name is legal
389 * slightly misleading in the case of NULL
391 if (!name
|| (strlen(name
) > (DEVMAXNAMESIZE
- 1)))
392 return (ENAMETOOLONG
);
395 * Allocate and fill out a new directory entry
397 MALLOC(dirent_p
, devdirent_t
*, sizeof(devdirent_t
),
398 M_DEVFSNAME
, M_WAITOK
);
402 bzero(dirent_p
,sizeof(devdirent_t
));
404 /* inherrit our parent's mount info */ /*XXX*/
405 /* a kludge but.... */
406 if(dirnode
&& ( dnp
->dn_dvm
== NULL
)) {
407 dnp
->dn_dvm
= dirnode
->dn_dvm
;
408 /* if(!dnp->dn_dvm) printf("parent had null dvm "); */
412 * Link the two together
413 * include the implicit link in the count of links to the devnode..
414 * this stops it from being accidentally freed later.
416 dirent_p
->de_dnp
= dnp
;
417 dnp
->dn_links
++ ; /* implicit from our own name-node */
420 * Make sure that we can find all the links that reference a node
421 * so that we can get them all if we need to zap the node.
423 if(dnp
->dn_linklist
) {
424 dirent_p
->de_nextlink
= dnp
->dn_linklist
;
425 dirent_p
->de_prevlinkp
= dirent_p
->de_nextlink
->de_prevlinkp
;
426 dirent_p
->de_nextlink
->de_prevlinkp
= &(dirent_p
->de_nextlink
);
427 *dirent_p
->de_prevlinkp
= dirent_p
;
429 dirent_p
->de_nextlink
= dirent_p
;
430 dirent_p
->de_prevlinkp
= &(dirent_p
->de_nextlink
);
432 dnp
->dn_linklist
= dirent_p
;
435 * If the node is a directory, then we need to handle the
436 * creation of the .. link.
437 * A NULL dirnode indicates a root node, so point to ourself.
439 if(dnp
->dn_type
== DEV_DIR
) {
440 dnp
->dn_typeinfo
.Dir
.myname
= dirent_p
;
442 * If we are unlinking from an old dir, decrement its links
443 * as we point our '..' elsewhere
444 * Note: it's up to the calling code to remove the
445 * us from the original directory's list
447 if(dnp
->dn_typeinfo
.Dir
.parent
) {
448 dnp
->dn_typeinfo
.Dir
.parent
->dn_links
--;
451 dnp
->dn_typeinfo
.Dir
.parent
= dirnode
;
453 dnp
->dn_typeinfo
.Dir
.parent
= dnp
;
455 dnp
->dn_typeinfo
.Dir
.parent
->dn_links
++; /* account for the new '..' */
459 * put the name into the directory entry.
461 strcpy(dirent_p
->de_name
, name
);
465 * Check if we are not making a root node..
470 * Put it on the END of the linked list of directory entries
474 dirent_p
->de_parent
= dirnode
; /* null for root */
475 dirent_p
->de_prevp
= dirnode
->dn_typeinfo
.Dir
.dirlast
;
476 dirent_p
->de_next
= *(dirent_p
->de_prevp
); /* should be NULL */
478 *(dirent_p
->de_prevp
) = dirent_p
;
479 dirnode
->dn_typeinfo
.Dir
.dirlast
= &(dirent_p
->de_next
);
480 dirnode
->dn_typeinfo
.Dir
.entrycount
++;
481 dirnode
->dn_len
+= strlen(name
) + 8;/*ok, ok?*/
484 *dirent_pp
= dirent_p
;
485 DEVFS_INCR_ENTRIES();
490 /***********************************************************************\
491 * Add a new element to the devfs plane. *
493 * Creates a new dev_node to go with it if the prototype should not be *
494 * reused. (Is a DIR, or we select SPLIT_DEVS at compile time) *
495 * typeinfo gives us info to make our node if we don't have a prototype. *
496 * If typeinfo is null and proto exists, then the typeinfo field of *
497 * the proto is used intead in the CREATE case. *
498 * note the 'links' count is 0 (except if a dir) *
499 * but it is only cleared on a transition *
500 * so this is ok till we link it to something *
501 * Even in SPLIT_DEVS mode, *
502 * if the node already exists on the wanted plane, just return it *
503 \***********************************************************************/
506 dev_add_node(int entrytype
, devnode_type_t
* typeinfo
, devnode_t
* proto
,
507 devnode_t
* *dn_pp
, struct devfsmount
*dvm
)
509 devnode_t
* dnp
= NULL
;
511 #if defined SPLIT_DEVS
513 * If we have a prototype, then check if there is already a sibling
514 * on the mount plane we are looking at, if so, just return it.
517 dnp
= proto
->dn_nextsibling
;
518 while( dnp
!= proto
) {
519 if (dnp
->dn_dvm
== dvm
) {
523 dnp
= dnp
->dn_nextsibling
;
525 if (typeinfo
== NULL
)
526 typeinfo
= &(proto
->dn_typeinfo
);
528 #else /* SPLIT_DEVS */
530 switch (proto
->type
) {
537 #endif /* SPLIT_DEVS */
538 MALLOC(dnp
, devnode_t
*, sizeof(devnode_t
), M_DEVFSNODE
, M_WAITOK
);
544 * If we have a proto, that means that we are duplicating some
545 * other device, which can only happen if we are not at the back plane
548 bcopy(proto
, dnp
, sizeof(devnode_t
));
550 dnp
->dn_linklist
= NULL
;
553 /* add to END of siblings list */
554 dnp
->dn_prevsiblingp
= proto
->dn_prevsiblingp
;
555 *(dnp
->dn_prevsiblingp
) = dnp
;
556 dnp
->dn_nextsibling
= proto
;
557 proto
->dn_prevsiblingp
= &(dnp
->dn_nextsibling
);
562 * We have no prototype, so start off with a clean slate
565 bzero(dnp
,sizeof(devnode_t
));
566 dnp
->dn_type
= entrytype
;
567 dnp
->dn_nextsibling
= dnp
;
568 dnp
->dn_prevsiblingp
= &(dnp
->dn_nextsibling
);
569 dnp
->dn_atime
.tv_sec
= tv
.tv_sec
;
570 dnp
->dn_mtime
.tv_sec
= tv
.tv_sec
;
571 dnp
->dn_ctime
.tv_sec
= tv
.tv_sec
;
576 * fill out the dev node according to type
581 * As it's a directory, make sure
582 * it has a null entries list
584 dnp
->dn_typeinfo
.Dir
.dirlast
= &(dnp
->dn_typeinfo
.Dir
.dirlist
);
585 dnp
->dn_typeinfo
.Dir
.dirlist
= (devdirent_t
*)0;
586 dnp
->dn_typeinfo
.Dir
.entrycount
= 0;
587 /* until we know better, it has a null parent pointer*/
588 dnp
->dn_typeinfo
.Dir
.parent
= NULL
;
589 dnp
->dn_links
++; /* for .*/
590 dnp
->dn_typeinfo
.Dir
.myname
= NULL
;
592 * make sure that the ops associated with it are the ops
593 * that we use (by default) for directories
595 dnp
->dn_ops
= &devfs_vnodeop_p
;
596 dnp
->dn_mode
|= 0555; /* default perms */
600 * As it's a symlink allocate and store the link info
601 * Symlinks should only ever be created by the user,
602 * so they are not on the back plane and should not be
603 * propogated forward.. a bit like directories in that way..
604 * A symlink only exists on one plane and has its own
605 * node.. therefore we might be on any random plane.
607 MALLOC(dnp
->dn_typeinfo
.Slnk
.name
, char *,
608 typeinfo
->Slnk
.namelen
+1,
609 M_DEVFSNODE
, M_WAITOK
);
610 if (!dnp
->dn_typeinfo
.Slnk
.name
) {
611 FREE(dnp
,M_DEVFSNODE
);
614 strncpy(dnp
->dn_typeinfo
.Slnk
.name
, typeinfo
->Slnk
.name
,
615 typeinfo
->Slnk
.namelen
);
616 dnp
->dn_typeinfo
.Slnk
.name
[typeinfo
->Slnk
.namelen
] = '\0';
617 dnp
->dn_typeinfo
.Slnk
.namelen
= typeinfo
->Slnk
.namelen
;
618 DEVFS_INCR_STRINGSPACE(dnp
->dn_typeinfo
.Slnk
.namelen
+ 1);
619 dnp
->dn_ops
= &devfs_vnodeop_p
;
620 dnp
->dn_mode
|= 0555; /* default perms */
625 * Make sure it has DEVICE type ops
626 * and device specific fields are correct
628 dnp
->dn_ops
= &devfs_spec_vnodeop_p
;
629 dnp
->dn_typeinfo
.dev
= typeinfo
->dev
;
643 devnode_free(devnode_t
* dnp
)
645 if (dnp
->dn_type
== DEV_SLNK
) {
646 DEVFS_DECR_STRINGSPACE(dnp
->dn_typeinfo
.Slnk
.namelen
+ 1);
647 FREE(dnp
->dn_typeinfo
.Slnk
.name
,M_DEVFSNODE
);
649 FREE(dnp
, M_DEVFSNODE
);
656 devfs_dn_free(devnode_t
* dnp
)
658 if(--dnp
->dn_links
<= 0 ) /* can be -1 for initial free, on error */
660 /*probably need to do other cleanups XXX */
661 if (dnp
->dn_nextsibling
!= dnp
) {
662 devnode_t
* * prevp
= dnp
->dn_prevsiblingp
;
663 *prevp
= dnp
->dn_nextsibling
;
664 dnp
->dn_nextsibling
->dn_prevsiblingp
= prevp
;
667 if (dnp
->dn_vn
== NULL
) {
669 printf("devfs_dn_free: free'ing %x\n", (unsigned int)dnp
);
671 devnode_free(dnp
); /* no accesses/references */
675 printf("devfs_dn_free: marking %x for deletion\n",
678 dnp
->dn_delete
= TRUE
;
683 /***********************************************************************\
684 * Front Node Operations *
685 * Add or delete a chain of front nodes *
686 \***********************************************************************/
688 /***********************************************************************\
689 * Given a directory backing node, and a child backing node, add the *
690 * appropriate front nodes to the front nodes of the directory to *
691 * represent the child node to the user *
693 * on failure, front nodes will either be correct or not exist for each *
694 * front dir, however dirs completed will not be stripped of completed *
695 * frontnodes on failure of a later frontnode *
697 * This allows a new node to be propogated through all mounted planes *
699 \***********************************************************************/
702 devfs_propogate(devdirent_t
* parent
,devdirent_t
* child
)
705 devdirent_t
* newnmp
;
706 devnode_t
* dnp
= child
->de_dnp
;
707 devnode_t
* pdnp
= parent
->de_dnp
;
708 devnode_t
* adnp
= parent
->de_dnp
;
709 int type
= child
->de_dnp
->dn_type
;
711 /***********************************************\
712 * Find the other instances of the parent node *
713 \***********************************************/
714 for (adnp
= pdnp
->dn_nextsibling
;
716 adnp
= adnp
->dn_nextsibling
)
719 * Make the node, using the original as a prototype)
720 * if the node already exists on that plane it won't be
723 if ((error
= dev_add_entry(child
->de_name
, adnp
, type
,
724 NULL
, dnp
, adnp
->dn_dvm
,
726 printf("duplicating %s failed\n",child
->de_name
);
729 return 0; /* for now always succeed */
732 /***********************************************************************
733 * remove all instances of this devicename [for backing nodes..]
734 * note.. if there is another link to the node (non dir nodes only)
735 * then the devfs_node will still exist as the ref count will be non-0
736 * removing a directory node will remove all sup-nodes on all planes (ZAP)
738 * Used by device drivers to remove nodes that are no longer relevant
739 * The argument is the 'cookie' they were given when they created the node
740 * this function is exported.. see devfs.h
741 ***********************************************************************/
743 devfs_remove(void *dirent_p
)
745 devnode_t
* dnp
= ((devdirent_t
*)dirent_p
)->de_dnp
;
747 boolean_t funnel_state
;
749 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
752 printf("devfs_remove: not ready for devices!\n");
758 /* keep removing the next sibling till only we exist. */
759 while((dnp2
= dnp
->dn_nextsibling
) != dnp
) {
762 * Keep removing the next front node till no more exist
764 dnp
->dn_nextsibling
= dnp2
->dn_nextsibling
;
765 dnp
->dn_nextsibling
->dn_prevsiblingp
= &(dnp
->dn_nextsibling
);
766 dnp2
->dn_nextsibling
= dnp2
;
767 dnp2
->dn_prevsiblingp
= &(dnp2
->dn_nextsibling
);
768 while(dnp2
->dn_linklist
) {
769 dev_free_name(dnp2
->dn_linklist
);
774 * then free the main node
775 * If we are not running in SPLIT_DEVS mode, then
776 * THIS is what gets rid of the propogated nodes.
778 while(dnp
->dn_linklist
) {
779 dev_free_name(dnp
->dn_linklist
);
783 (void) thread_funnel_set(kernel_flock
, funnel_state
);
788 /***************************************************************
789 * duplicate the backing tree into a tree of nodes hung off the
790 * mount point given as the argument. Do this by
791 * calling dev_dup_entry which recurses all the way
793 **************************************************************/
796 dev_dup_plane(struct devfsmount
*devfs_mp_p
)
801 if ((error
= dev_dup_entry(NULL
, dev_root
, &new, devfs_mp_p
)))
803 devfs_mp_p
->plane_root
= new;
809 /***************************************************************\
811 \***************************************************************/
814 devfs_free_plane(struct devfsmount
*devfs_mp_p
)
816 devdirent_t
* dirent_p
;
818 dirent_p
= devfs_mp_p
->plane_root
;
820 dev_free_hier(dirent_p
);
821 dev_free_name(dirent_p
);
823 devfs_mp_p
->plane_root
= NULL
;
826 /***************************************************************\
827 * Create and link in a new front element.. *
828 * Parent can be 0 for a root node *
829 * Not presently usable to make a symlink XXX *
830 * (Ok, symlinks don't propogate)
831 * recursively will create subnodes corresponding to equivalent *
832 * child nodes in the base level *
833 \***************************************************************/
836 dev_dup_entry(devnode_t
* parent
, devdirent_t
* back
, devdirent_t
* *dnm_pp
,
837 struct devfsmount
*dvm
)
839 devdirent_t
* entry_p
;
840 devdirent_t
* newback
;
841 devdirent_t
* newfront
;
843 devnode_t
* dnp
= back
->de_dnp
;
844 int type
= dnp
->dn_type
;
847 * go get the node made (if we need to)
848 * use the back one as a prototype
850 if ((error
= dev_add_entry(back
->de_name
, parent
, type
,
852 parent
?parent
->dn_dvm
:dvm
, &entry_p
)) != 0) {
853 printf("duplicating %s failed\n",back
->de_name
);
857 * If we have just made the root, then insert the pointer to the
861 entry_p
->de_dnp
->dn_dvm
= dvm
;
865 * If it is a directory, then recurse down all the other
867 * note that this time we don't pass on the mount info..
871 for(newback
= back
->de_dnp
->dn_typeinfo
.Dir
.dirlist
;
872 newback
; newback
= newback
->de_next
)
874 if((error
= dev_dup_entry(entry_p
->de_dnp
,
875 newback
, &newfront
, NULL
)) != 0)
877 break; /* back out with an error */
885 /***************************************************************\
887 * remember that if there are other names pointing to the *
888 * dev_node then it may not get freed yet *
889 * can handle if there is no dnp *
890 \***************************************************************/
893 dev_free_name(devdirent_t
* dirent_p
)
895 devnode_t
* parent
= dirent_p
->de_parent
;
896 devnode_t
* dnp
= dirent_p
->de_dnp
;
899 if(dnp
->dn_type
== DEV_DIR
)
903 if(dnp
->dn_typeinfo
.Dir
.dirlist
)
905 p
= dnp
->dn_typeinfo
.Dir
.parent
;
906 devfs_dn_free(dnp
); /* account for '.' */
907 devfs_dn_free(p
); /* '..' */
910 * unlink us from the list of links for this node
911 * If we are the only link, it's easy!
912 * if we are a DIR of course there should not be any
915 if(dirent_p
->de_nextlink
== dirent_p
) {
916 dnp
->dn_linklist
= NULL
;
918 if(dnp
->dn_linklist
== dirent_p
) {
919 dnp
->dn_linklist
= dirent_p
->de_nextlink
;
921 dirent_p
->de_nextlink
->de_prevlinkp
922 = dirent_p
->de_prevlinkp
;
923 *dirent_p
->de_prevlinkp
= dirent_p
->de_nextlink
;
929 * unlink ourselves from the directory on this plane
931 if(parent
) /* if not fs root */
933 if( (*dirent_p
->de_prevp
= dirent_p
->de_next
) )/* yes, assign */
935 dirent_p
->de_next
->de_prevp
= dirent_p
->de_prevp
;
939 parent
->dn_typeinfo
.Dir
.dirlast
940 = dirent_p
->de_prevp
;
942 parent
->dn_typeinfo
.Dir
.entrycount
--;
943 parent
->dn_len
-= strlen(dirent_p
->de_name
) + 8;
946 DEVFS_DECR_ENTRIES();
947 FREE(dirent_p
,M_DEVFSNAME
);
951 /***************************************************************\
952 * Free a hierarchy starting at a directory node name *
953 * remember that if there are other names pointing to the *
954 * dev_node then it may not get freed yet *
955 * can handle if there is no dnp *
956 * leave the node itself allocated. *
957 \***************************************************************/
960 dev_free_hier(devdirent_t
* dirent_p
)
962 devnode_t
* dnp
= dirent_p
->de_dnp
;
965 if(dnp
->dn_type
== DEV_DIR
)
967 while(dnp
->dn_typeinfo
.Dir
.dirlist
)
969 dev_free_hier(dnp
->dn_typeinfo
.Dir
.dirlist
);
970 dev_free_name(dnp
->dn_typeinfo
.Dir
.dirlist
);
976 /***************************************************************\
977 * given a dev_node, find the appropriate vnode if one is already
978 * associated, or get a new one and associate it with the dev_node
979 \***************************************************************/
982 devfs_dntovn(devnode_t
* dnp
, struct vnode
**vn_pp
, struct proc
* p
)
984 struct vnode
*vn_p
, *nvp
;
989 if (vn_p
) { /* already has a vnode */
991 return(vget(vn_p
, LK_EXCLUSIVE
, p
));
993 if (!(error
= getnewvnode(VT_DEVFS
, dnp
->dn_dvm
->mount
,
994 *(dnp
->dn_ops
), &vn_p
))) {
995 switch(dnp
->dn_type
) {
1000 if (dnp
->dn_typeinfo
.Dir
.parent
== dnp
) {
1001 vn_p
->v_flag
|= VROOT
;
1003 vn_p
->v_type
= VDIR
;
1008 = (dnp
->dn_type
== DEV_BDEV
) ? VBLK
: VCHR
;
1009 if ((nvp
= checkalias(vn_p
, dnp
->dn_typeinfo
.dev
,
1010 dnp
->dn_dvm
->mount
)) != NULL
) {
1016 vn_p
->v_mount
= dnp
->dn_dvm
->mount
;/* XXX Duplicated */
1018 vn_p
->v_data
= (void *)dnp
;
1020 error
= vn_lock(vn_p
, LK_EXCLUSIVE
| LK_RETRY
, p
);
1025 /***********************************************************************\
1026 * add a whole device, with no prototype.. make name element and node *
1027 * Used for adding the original device entries *
1028 \***********************************************************************/
1031 dev_add_entry(char *name
, devnode_t
* parent
, int type
, devnode_type_t
* typeinfo
,
1032 devnode_t
* proto
, struct devfsmount
*dvm
, devdirent_t
* *nm_pp
)
1037 if ((error
= dev_add_node(type
, typeinfo
, proto
, &dnp
,
1038 (parent
?parent
->dn_dvm
:dvm
))) != 0)
1040 printf("devfs: %s: base node allocation failed (Errno=%d)\n",
1044 if ((error
= dev_add_name(name
,parent
,NULL
, dnp
, nm_pp
)) != 0)
1046 devfs_dn_free(dnp
); /* 1->0 for dir, 0->(-1) for other */
1047 printf("devfs: %s: name slot allocation failed (Errno=%d)\n",
1054 #include <sys/subr_prf.h>
1057 * Function: devfs_make_node
1060 * Create a device node with the given pathname in the devfs namespace.
1063 * dev - the dev_t value to associate
1064 * chrblk - block or character device (DEVFS_CHAR or DEVFS_BLOCK)
1065 * uid, gid - ownership
1066 * perms - permissions
1067 * fmt, ... - path format string with printf args to format the path name
1069 * A handle to a device node if successful, NULL otherwise.
1072 devfs_make_node(dev_t dev
, int chrblk
, uid_t uid
,
1073 gid_t gid
, int perms
, char *fmt
, ...)
1075 devdirent_t
* new_dev
= NULL
;
1076 devnode_t
* dnp
; /* devnode for parent directory */
1077 devnode_type_t typeinfo
;
1079 char *name
, *path
, buf
[256]; /* XXX */
1081 boolean_t funnel_state
;
1085 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1088 printf("devfs_make_node: not ready for devices!\n");
1092 if (chrblk
!= DEVFS_CHAR
&& chrblk
!= DEVFS_BLOCK
)
1096 prf(fmt
, ap
, TOSTR
, (struct tty
*)&b_ptr
);
1102 for(i
=strlen(buf
); i
>0; i
--)
1118 /* find/create directory path ie. mkdir -p */
1119 if (dev_finddir(path
, NULL
, CREATE
, &dnp
) == 0) {
1121 if (dev_add_entry(name
, dnp
,
1122 (chrblk
== DEVFS_CHAR
) ? DEV_CDEV
: DEV_BDEV
,
1123 &typeinfo
, NULL
, NULL
, &new_dev
) == 0) {
1124 new_dev
->de_dnp
->dn_gid
= gid
;
1125 new_dev
->de_dnp
->dn_uid
= uid
;
1126 new_dev
->de_dnp
->dn_mode
|= perms
;
1127 devfs_propogate(dnp
->dn_typeinfo
.Dir
.myname
, new_dev
);
1133 (void) thread_funnel_set(kernel_flock
, funnel_state
);
1138 * Function: devfs_make_link
1141 * Create a link to a previously created device node.
1144 * 0 if successful, -1 if failed
1147 devfs_make_link(void *original
, char *fmt
, ...)
1149 devdirent_t
* new_dev
= NULL
;
1150 devdirent_t
* orig
= (devdirent_t
*) original
;
1151 devnode_t
* dirnode
; /* devnode for parent directory */
1154 char *p
, buf
[256]; /* XXX */
1157 boolean_t funnel_state
;
1159 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1162 printf("devfs_make_link: not ready for devices!\n");
1167 prf(fmt
, ap
, TOSTR
, (struct tty
*)&b_ptr
);
1173 for(i
=strlen(buf
); i
>0; i
--)
1182 if (dev_finddir(buf
, NULL
, CREATE
, &dirnode
)
1183 || dev_add_name(p
, dirnode
, NULL
, orig
->de_dnp
, &new_dev
))
1186 if (dev_finddir("", NULL
, CREATE
, &dirnode
)
1187 || dev_add_name(buf
, dirnode
, NULL
, orig
->de_dnp
, &new_dev
))
1190 devfs_propogate(dirnode
->dn_typeinfo
.Dir
.myname
, new_dev
);
1194 (void) thread_funnel_set(kernel_flock
, funnel_state
);
1195 return ((new_dev
!= NULL
) ? 0 : -1);