xref: /xnu-8796.141.3/bsd/miscfs/devfs/devfs_tree.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions  *
15*1b191cb5SApple OSS Distributions  * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions  *
18*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions  * limitations under the License.
25*1b191cb5SApple OSS Distributions  *
26*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions  */
28*1b191cb5SApple OSS Distributions 
29*1b191cb5SApple OSS Distributions /*
30*1b191cb5SApple OSS Distributions  * Copyright 1997,1998 Julian Elischer.  All rights reserved.
31*1b191cb5SApple OSS Distributions  * [email protected]
32*1b191cb5SApple OSS Distributions  *
33*1b191cb5SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
34*1b191cb5SApple OSS Distributions  * modification, are permitted provided that the following conditions are
35*1b191cb5SApple OSS Distributions  * met:
36*1b191cb5SApple OSS Distributions  *  1. Redistributions of source code must retain the above copyright
37*1b191cb5SApple OSS Distributions  *     notice, this list of conditions and the following disclaimer.
38*1b191cb5SApple OSS Distributions  *  2. Redistributions in binary form must reproduce the above copyright notice,
39*1b191cb5SApple OSS Distributions  *     this list of conditions and the following disclaimer in the documentation
40*1b191cb5SApple OSS Distributions  *     and/or other materials provided with the distribution.
41*1b191cb5SApple OSS Distributions  *
42*1b191cb5SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
43*1b191cb5SApple OSS Distributions  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
44*1b191cb5SApple OSS Distributions  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45*1b191cb5SApple OSS Distributions  * DISCLAIMED.  IN NO EVENT SHALL THE HOLDER OR CONTRIBUTORS BE LIABLE FOR
46*1b191cb5SApple OSS Distributions  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47*1b191cb5SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48*1b191cb5SApple OSS Distributions  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49*1b191cb5SApple OSS Distributions  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50*1b191cb5SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51*1b191cb5SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52*1b191cb5SApple OSS Distributions  * SUCH DAMAGE.
53*1b191cb5SApple OSS Distributions  *
54*1b191cb5SApple OSS Distributions  * devfs_tree.c
55*1b191cb5SApple OSS Distributions  */
56*1b191cb5SApple OSS Distributions /*
57*1b191cb5SApple OSS Distributions  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
58*1b191cb5SApple OSS Distributions  * support for mandatory and extensible security protections.  This notice
59*1b191cb5SApple OSS Distributions  * is included in support of clause 2.2 (b) of the Apple Public License,
60*1b191cb5SApple OSS Distributions  * Version 2.0.
61*1b191cb5SApple OSS Distributions  */
62*1b191cb5SApple OSS Distributions 
63*1b191cb5SApple OSS Distributions /*
64*1b191cb5SApple OSS Distributions  * HISTORY
65*1b191cb5SApple OSS Distributions  *  Dieter Siegmund ([email protected]) Thu Apr  8 14:08:19 PDT 1999
66*1b191cb5SApple OSS Distributions  *  - removed mounting of "hidden" mountpoint
67*1b191cb5SApple OSS Distributions  *  - fixed problem in which devnode->dn_vn pointer was not
68*1b191cb5SApple OSS Distributions  *    updated with the vnode returned from checkalias()
69*1b191cb5SApple OSS Distributions  *  - replaced devfs_vntodn() with a macro VTODN()
70*1b191cb5SApple OSS Distributions  *  - rewrote dev_finddir() to not use recursion
71*1b191cb5SApple OSS Distributions  *  - added locking to avoid data structure corruption (DEVFS_(UN)LOCK())
72*1b191cb5SApple OSS Distributions  *  Dieter Siegmund ([email protected]) Wed Jul 14 13:37:59 PDT 1999
73*1b191cb5SApple OSS Distributions  *  - fixed problem with devfs_dntovn() checking the v_id against the
74*1b191cb5SApple OSS Distributions  *    value cached in the device node; a union mount on top of us causes
75*1b191cb5SApple OSS Distributions  *    the v_id to get incremented thus, we would end up returning a new
76*1b191cb5SApple OSS Distributions  *    vnode instead of the existing one that has the mounted_here
77*1b191cb5SApple OSS Distributions  *    field filled in; the net effect was that the filesystem mounted
78*1b191cb5SApple OSS Distributions  *    on top of us would never show up
79*1b191cb5SApple OSS Distributions  *  - added devfs_stats to store how many data structures are actually
80*1b191cb5SApple OSS Distributions  *    allocated
81*1b191cb5SApple OSS Distributions  */
82*1b191cb5SApple OSS Distributions 
83*1b191cb5SApple OSS Distributions /* SPLIT_DEVS means each devfs uses a different devnode for the same device */
84*1b191cb5SApple OSS Distributions /* Otherwise the same device always ends up at the same vnode even if  */
85*1b191cb5SApple OSS Distributions /* reached througgh a different devfs instance. The practical difference */
86*1b191cb5SApple OSS Distributions /* is that with the same vnode, chmods and chowns show up on all instances of */
87*1b191cb5SApple OSS Distributions /* a device. (etc) */
88*1b191cb5SApple OSS Distributions 
89*1b191cb5SApple OSS Distributions #define SPLIT_DEVS 1 /* maybe make this an option */
90*1b191cb5SApple OSS Distributions /*#define SPLIT_DEVS 1*/
91*1b191cb5SApple OSS Distributions 
92*1b191cb5SApple OSS Distributions #include <sys/param.h>
93*1b191cb5SApple OSS Distributions #include <sys/systm.h>
94*1b191cb5SApple OSS Distributions #include <sys/kernel.h>
95*1b191cb5SApple OSS Distributions #include <sys/conf.h>
96*1b191cb5SApple OSS Distributions #include <sys/malloc.h>
97*1b191cb5SApple OSS Distributions #include <sys/mount_internal.h>
98*1b191cb5SApple OSS Distributions #include <sys/proc.h>
99*1b191cb5SApple OSS Distributions #include <sys/vnode_internal.h>
100*1b191cb5SApple OSS Distributions #include <stdarg.h>
101*1b191cb5SApple OSS Distributions #include <libkern/OSAtomic.h>
102*1b191cb5SApple OSS Distributions #include <os/refcnt.h>
103*1b191cb5SApple OSS Distributions #define BSD_KERNEL_PRIVATE      1       /* devfs_make_link() prototype */
104*1b191cb5SApple OSS Distributions #include "devfs.h"
105*1b191cb5SApple OSS Distributions #include "devfsdefs.h"
106*1b191cb5SApple OSS Distributions 
107*1b191cb5SApple OSS Distributions #if CONFIG_MACF
108*1b191cb5SApple OSS Distributions #include <security/mac_framework.h>
109*1b191cb5SApple OSS Distributions #endif
110*1b191cb5SApple OSS Distributions 
111*1b191cb5SApple OSS Distributions #if FDESC
112*1b191cb5SApple OSS Distributions #include "fdesc.h"
113*1b191cb5SApple OSS Distributions #endif
114*1b191cb5SApple OSS Distributions 
115*1b191cb5SApple OSS Distributions typedef struct devfs_vnode_event {
116*1b191cb5SApple OSS Distributions 	vnode_t                 dve_vp;
117*1b191cb5SApple OSS Distributions 	uint32_t                dve_vid;
118*1b191cb5SApple OSS Distributions 	uint32_t                dve_events;
119*1b191cb5SApple OSS Distributions } *devfs_vnode_event_t;
120*1b191cb5SApple OSS Distributions 
121*1b191cb5SApple OSS Distributions /*
122*1b191cb5SApple OSS Distributions  * Size of stack buffer (fast path) for notifications.  If
123*1b191cb5SApple OSS Distributions  * the number of mounts is small, no need to malloc a buffer.
124*1b191cb5SApple OSS Distributions  */
125*1b191cb5SApple OSS Distributions #define NUM_STACK_ENTRIES 5
126*1b191cb5SApple OSS Distributions 
127*1b191cb5SApple OSS Distributions typedef struct devfs_event_log {
128*1b191cb5SApple OSS Distributions 	size_t                  del_max;
129*1b191cb5SApple OSS Distributions 	size_t                  del_used;
130*1b191cb5SApple OSS Distributions 	devfs_vnode_event_t     del_entries;
131*1b191cb5SApple OSS Distributions } *devfs_event_log_t;
132*1b191cb5SApple OSS Distributions 
133*1b191cb5SApple OSS Distributions 
134*1b191cb5SApple OSS Distributions static void     dev_free_hier(devdirent_t *);
135*1b191cb5SApple OSS Distributions static int      devfs_propogate(devdirent_t *, devdirent_t *, devfs_event_log_t);
136*1b191cb5SApple OSS Distributions static int      dev_finddir(const char *, devnode_t *, int, devnode_t **, devfs_event_log_t);
137*1b191cb5SApple OSS Distributions static int      dev_dup_entry(devnode_t *, devdirent_t *, devdirent_t **, struct devfsmount *);
138*1b191cb5SApple OSS Distributions void            devfs_ref_node(devnode_t *);
139*1b191cb5SApple OSS Distributions void            devfs_rele_node(devnode_t *);
140*1b191cb5SApple OSS Distributions static void     devfs_record_event(devfs_event_log_t, devnode_t*, uint32_t);
141*1b191cb5SApple OSS Distributions static int      devfs_init_event_log(devfs_event_log_t, uint32_t, devfs_vnode_event_t);
142*1b191cb5SApple OSS Distributions static void     devfs_release_event_log(devfs_event_log_t, int);
143*1b191cb5SApple OSS Distributions static void     devfs_bulk_notify(devfs_event_log_t);
144*1b191cb5SApple OSS Distributions static devdirent_t *devfs_make_node_internal(dev_t, devfstype_t type, uid_t, gid_t, int,
145*1b191cb5SApple OSS Distributions     int (*clone)(dev_t dev, int action), const char *fmt, va_list ap);
146*1b191cb5SApple OSS Distributions 
147*1b191cb5SApple OSS Distributions 
148*1b191cb5SApple OSS Distributions static LCK_GRP_DECLARE(devfs_lck_grp, "devfs_lock");
149*1b191cb5SApple OSS Distributions LCK_MTX_DECLARE(devfs_mutex, &devfs_lck_grp);
150*1b191cb5SApple OSS Distributions LCK_MTX_DECLARE(devfs_attr_mutex, &devfs_lck_grp);
151*1b191cb5SApple OSS Distributions 
152*1b191cb5SApple OSS Distributions os_refgrp_decl(static, devfs_refgrp, "devfs", NULL);
153*1b191cb5SApple OSS Distributions 
154*1b191cb5SApple OSS Distributions devdirent_t *           dev_root = NULL;        /* root of backing tree */
155*1b191cb5SApple OSS Distributions struct devfs_stats      devfs_stats;            /* hold stats */
156*1b191cb5SApple OSS Distributions 
157*1b191cb5SApple OSS Distributions static ino_t            devfs_unique_fileno = 0;
158*1b191cb5SApple OSS Distributions 
159*1b191cb5SApple OSS Distributions #ifdef HIDDEN_MOUNTPOINT
160*1b191cb5SApple OSS Distributions static struct mount *devfs_hidden_mount;
161*1b191cb5SApple OSS Distributions #endif /* HIDDEN_MOINTPOINT */
162*1b191cb5SApple OSS Distributions 
163*1b191cb5SApple OSS Distributions static int devfs_ready = 0;
164*1b191cb5SApple OSS Distributions static uint32_t devfs_nmountplanes = 0; /* The first plane is not used for a mount */
165*1b191cb5SApple OSS Distributions 
166*1b191cb5SApple OSS Distributions #define DEVFS_NOCREATE  FALSE
167*1b191cb5SApple OSS Distributions #define DEVFS_CREATE    TRUE
168*1b191cb5SApple OSS Distributions 
169*1b191cb5SApple OSS Distributions /*
170*1b191cb5SApple OSS Distributions  * Set up the root directory node in the backing plane
171*1b191cb5SApple OSS Distributions  * This is happenning before the vfs system has been
172*1b191cb5SApple OSS Distributions  * set up yet, so be careful about what we reference..
173*1b191cb5SApple OSS Distributions  * Notice that the ops are by indirection.. as they haven't
174*1b191cb5SApple OSS Distributions  * been set up yet!
175*1b191cb5SApple OSS Distributions  * DEVFS has a hidden mountpoint that is used as the anchor point
176*1b191cb5SApple OSS Distributions  * for the internal 'blueprint' version of the dev filesystem tree.
177*1b191cb5SApple OSS Distributions  */
178*1b191cb5SApple OSS Distributions /*proto*/
179*1b191cb5SApple OSS Distributions int
devfs_sinit(void)180*1b191cb5SApple OSS Distributions devfs_sinit(void)
181*1b191cb5SApple OSS Distributions {
182*1b191cb5SApple OSS Distributions 	int error;
183*1b191cb5SApple OSS Distributions 
184*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
185*1b191cb5SApple OSS Distributions 	error = dev_add_entry("root", NULL, DEV_DIR, NULL, NULL, NULL, &dev_root);
186*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
187*1b191cb5SApple OSS Distributions 
188*1b191cb5SApple OSS Distributions 	if (error) {
189*1b191cb5SApple OSS Distributions 		printf("devfs_sinit: dev_add_entry failed ");
190*1b191cb5SApple OSS Distributions 		return ENOTSUP;
191*1b191cb5SApple OSS Distributions 	}
192*1b191cb5SApple OSS Distributions #ifdef HIDDEN_MOUNTPOINT
193*1b191cb5SApple OSS Distributions 	devfs_hidden_mount = zalloc_flags(mount_zone, Z_WAITOK | Z_ZERO);
194*1b191cb5SApple OSS Distributions 	mount_lock_init(devfs_hidden_mount);
195*1b191cb5SApple OSS Distributions 	TAILQ_INIT(&devfs_hidden_mount->mnt_vnodelist);
196*1b191cb5SApple OSS Distributions 	TAILQ_INIT(&devfs_hidden_mount->mnt_workerqueue);
197*1b191cb5SApple OSS Distributions 	TAILQ_INIT(&devfs_hidden_mount->mnt_newvnodes);
198*1b191cb5SApple OSS Distributions #if CONFIG_MACF
199*1b191cb5SApple OSS Distributions 	mac_mount_label_init(devfs_hidden_mount);
200*1b191cb5SApple OSS Distributions 	mac_mount_label_associate(vfs_context_kernel(), devfs_hidden_mount);
201*1b191cb5SApple OSS Distributions #endif
202*1b191cb5SApple OSS Distributions 
203*1b191cb5SApple OSS Distributions 	/* Initialize the default IO constraints */
204*1b191cb5SApple OSS Distributions 	mp->mnt_maxreadcnt = mp->mnt_maxwritecnt = MAXPHYS;
205*1b191cb5SApple OSS Distributions 	mp->mnt_segreadcnt = mp->mnt_segwritecnt = 32;
206*1b191cb5SApple OSS Distributions 	mp->mnt_ioflags = 0;
207*1b191cb5SApple OSS Distributions 	mp->mnt_realrootvp = NULLVP;
208*1b191cb5SApple OSS Distributions 	mp->mnt_authcache_ttl = CACHED_LOOKUP_RIGHT_TTL;
209*1b191cb5SApple OSS Distributions 
210*1b191cb5SApple OSS Distributions 	devfs_mount(devfs_hidden_mount, "dummy", NULL, NULL, NULL);
211*1b191cb5SApple OSS Distributions 	dev_root->de_dnp->dn_dvm
212*1b191cb5SApple OSS Distributions 	        = (struct devfsmount *)devfs_hidden_mount->mnt_data;
213*1b191cb5SApple OSS Distributions #endif /* HIDDEN_MOUNTPOINT */
214*1b191cb5SApple OSS Distributions #if CONFIG_MACF
215*1b191cb5SApple OSS Distributions 	mac_devfs_label_associate_directory("/", (int) strlen("/"),
216*1b191cb5SApple OSS Distributions 	    dev_root->de_dnp, "/");
217*1b191cb5SApple OSS Distributions #endif
218*1b191cb5SApple OSS Distributions 	devfs_ready = 1;
219*1b191cb5SApple OSS Distributions 	return 0;
220*1b191cb5SApple OSS Distributions }
221*1b191cb5SApple OSS Distributions 
222*1b191cb5SApple OSS Distributions /***********************************************************************\
223*1b191cb5SApple OSS Distributions *************************************************************************
224*1b191cb5SApple OSS Distributions *	Routines used to find our way to a point in the tree		*
225*1b191cb5SApple OSS Distributions *************************************************************************
226*1b191cb5SApple OSS Distributions \***********************************************************************/
227*1b191cb5SApple OSS Distributions 
228*1b191cb5SApple OSS Distributions 
229*1b191cb5SApple OSS Distributions 
230*1b191cb5SApple OSS Distributions /***************************************************************
231*1b191cb5SApple OSS Distributions * Search down the linked list off a dir to find "name"
232*1b191cb5SApple OSS Distributions * return the devnode_t * for that node.
233*1b191cb5SApple OSS Distributions *
234*1b191cb5SApple OSS Distributions * called with DEVFS_LOCK held
235*1b191cb5SApple OSS Distributions ***************************************************************/
236*1b191cb5SApple OSS Distributions devdirent_t *
dev_findname(devnode_t * dir,const char * name)237*1b191cb5SApple OSS Distributions dev_findname(devnode_t * dir, const char *name)
238*1b191cb5SApple OSS Distributions {
239*1b191cb5SApple OSS Distributions 	devdirent_t * newfp;
240*1b191cb5SApple OSS Distributions 	if (dir->dn_type != DEV_DIR) {
241*1b191cb5SApple OSS Distributions 		return 0;                     /*XXX*/ /* printf?*/
242*1b191cb5SApple OSS Distributions 	}
243*1b191cb5SApple OSS Distributions 	if (name[0] == '.') {
244*1b191cb5SApple OSS Distributions 		if (name[1] == 0) {
245*1b191cb5SApple OSS Distributions 			return dir->dn_typeinfo.Dir.myname;
246*1b191cb5SApple OSS Distributions 		}
247*1b191cb5SApple OSS Distributions 		if ((name[1] == '.') && (name[2] == 0)) {
248*1b191cb5SApple OSS Distributions 			/* for root, .. == . */
249*1b191cb5SApple OSS Distributions 			return dir->dn_typeinfo.Dir.parent->dn_typeinfo.Dir.myname;
250*1b191cb5SApple OSS Distributions 		}
251*1b191cb5SApple OSS Distributions 	}
252*1b191cb5SApple OSS Distributions 	newfp = dir->dn_typeinfo.Dir.dirlist;
253*1b191cb5SApple OSS Distributions 
254*1b191cb5SApple OSS Distributions 	while (newfp) {
255*1b191cb5SApple OSS Distributions 		if (!(strncmp(name, newfp->de_name, sizeof(newfp->de_name)))) {
256*1b191cb5SApple OSS Distributions 			return newfp;
257*1b191cb5SApple OSS Distributions 		}
258*1b191cb5SApple OSS Distributions 		newfp = newfp->de_next;
259*1b191cb5SApple OSS Distributions 	}
260*1b191cb5SApple OSS Distributions 	return NULL;
261*1b191cb5SApple OSS Distributions }
262*1b191cb5SApple OSS Distributions 
263*1b191cb5SApple OSS Distributions /***********************************************************************
264*1b191cb5SApple OSS Distributions * Given a starting node (0 for root) and a pathname, return the node
265*1b191cb5SApple OSS Distributions * for the end item on the path. It MUST BE A DIRECTORY. If the 'DEVFS_CREATE'
266*1b191cb5SApple OSS Distributions * option is true, then create any missing nodes in the path and create
267*1b191cb5SApple OSS Distributions * and return the final node as well.
268*1b191cb5SApple OSS Distributions * This is used to set up a directory, before making nodes in it..
269*1b191cb5SApple OSS Distributions *
270*1b191cb5SApple OSS Distributions * called with DEVFS_LOCK held
271*1b191cb5SApple OSS Distributions ***********************************************************************/
272*1b191cb5SApple OSS Distributions static int
dev_finddir(const char * path,devnode_t * dirnode,int create,devnode_t ** dn_pp,devfs_event_log_t delp)273*1b191cb5SApple OSS Distributions dev_finddir(const char * path,
274*1b191cb5SApple OSS Distributions     devnode_t * dirnode,
275*1b191cb5SApple OSS Distributions     int create,
276*1b191cb5SApple OSS Distributions     devnode_t * * dn_pp,
277*1b191cb5SApple OSS Distributions     devfs_event_log_t delp)
278*1b191cb5SApple OSS Distributions {
279*1b191cb5SApple OSS Distributions 	devnode_t *     dnp = NULL;
280*1b191cb5SApple OSS Distributions 	int             error = 0;
281*1b191cb5SApple OSS Distributions 	const char *            scan;
282*1b191cb5SApple OSS Distributions #if CONFIG_MACF
283*1b191cb5SApple OSS Distributions 	char            fullpath[DEVMAXPATHSIZE];
284*1b191cb5SApple OSS Distributions #endif
285*1b191cb5SApple OSS Distributions 
286*1b191cb5SApple OSS Distributions 
287*1b191cb5SApple OSS Distributions 	if (!dirnode) { /* dirnode == NULL means start at root */
288*1b191cb5SApple OSS Distributions 		dirnode = dev_root->de_dnp;
289*1b191cb5SApple OSS Distributions 	}
290*1b191cb5SApple OSS Distributions 
291*1b191cb5SApple OSS Distributions 	if (dirnode->dn_type != DEV_DIR) {
292*1b191cb5SApple OSS Distributions 		return ENOTDIR;
293*1b191cb5SApple OSS Distributions 	}
294*1b191cb5SApple OSS Distributions 
295*1b191cb5SApple OSS Distributions 	if (strlen(path) > (DEVMAXPATHSIZE - 1)) {
296*1b191cb5SApple OSS Distributions 		return ENAMETOOLONG;
297*1b191cb5SApple OSS Distributions 	}
298*1b191cb5SApple OSS Distributions 
299*1b191cb5SApple OSS Distributions #if CONFIG_MACF
300*1b191cb5SApple OSS Distributions 	strlcpy(fullpath, path, DEVMAXPATHSIZE);
301*1b191cb5SApple OSS Distributions #endif
302*1b191cb5SApple OSS Distributions 	scan = path;
303*1b191cb5SApple OSS Distributions 
304*1b191cb5SApple OSS Distributions 	while (*scan == '/') {
305*1b191cb5SApple OSS Distributions 		scan++;
306*1b191cb5SApple OSS Distributions 	}
307*1b191cb5SApple OSS Distributions 
308*1b191cb5SApple OSS Distributions 	*dn_pp = NULL;
309*1b191cb5SApple OSS Distributions 
310*1b191cb5SApple OSS Distributions 	while (1) {
311*1b191cb5SApple OSS Distributions 		char                component[DEVMAXPATHSIZE];
312*1b191cb5SApple OSS Distributions 		devdirent_t *       dirent_p;
313*1b191cb5SApple OSS Distributions 		const char *        start;
314*1b191cb5SApple OSS Distributions 
315*1b191cb5SApple OSS Distributions 		if (*scan == 0) {
316*1b191cb5SApple OSS Distributions 			/* we hit the end of the string, we're done */
317*1b191cb5SApple OSS Distributions 			*dn_pp = dirnode;
318*1b191cb5SApple OSS Distributions 			break;
319*1b191cb5SApple OSS Distributions 		}
320*1b191cb5SApple OSS Distributions 		start = scan;
321*1b191cb5SApple OSS Distributions 		while (*scan != '/' && *scan) {
322*1b191cb5SApple OSS Distributions 			scan++;
323*1b191cb5SApple OSS Distributions 		}
324*1b191cb5SApple OSS Distributions 
325*1b191cb5SApple OSS Distributions 		strlcpy(component, start, (scan - start) + 1);
326*1b191cb5SApple OSS Distributions 		if (*scan == '/') {
327*1b191cb5SApple OSS Distributions 			scan++;
328*1b191cb5SApple OSS Distributions 		}
329*1b191cb5SApple OSS Distributions 
330*1b191cb5SApple OSS Distributions 		dirent_p = dev_findname(dirnode, component);
331*1b191cb5SApple OSS Distributions 		if (dirent_p) {
332*1b191cb5SApple OSS Distributions 			dnp = dirent_p->de_dnp;
333*1b191cb5SApple OSS Distributions 			if (dnp->dn_type != DEV_DIR) {
334*1b191cb5SApple OSS Distributions 				error = ENOTDIR;
335*1b191cb5SApple OSS Distributions 				break;
336*1b191cb5SApple OSS Distributions 			}
337*1b191cb5SApple OSS Distributions 		} else {
338*1b191cb5SApple OSS Distributions 			if (!create) {
339*1b191cb5SApple OSS Distributions 				error = ENOENT;
340*1b191cb5SApple OSS Distributions 				break;
341*1b191cb5SApple OSS Distributions 			}
342*1b191cb5SApple OSS Distributions 			error = dev_add_entry(component, dirnode,
343*1b191cb5SApple OSS Distributions 			    DEV_DIR, NULL, NULL, NULL, &dirent_p);
344*1b191cb5SApple OSS Distributions 			if (error) {
345*1b191cb5SApple OSS Distributions 				break;
346*1b191cb5SApple OSS Distributions 			}
347*1b191cb5SApple OSS Distributions 			dnp = dirent_p->de_dnp;
348*1b191cb5SApple OSS Distributions #if CONFIG_MACF
349*1b191cb5SApple OSS Distributions 			mac_devfs_label_associate_directory(
350*1b191cb5SApple OSS Distributions 				dirnode->dn_typeinfo.Dir.myname->de_name,
351*1b191cb5SApple OSS Distributions 				(int) strlen(dirnode->dn_typeinfo.Dir.myname->de_name),
352*1b191cb5SApple OSS Distributions 				dnp, fullpath);
353*1b191cb5SApple OSS Distributions #endif
354*1b191cb5SApple OSS Distributions 			devfs_propogate(dirnode->dn_typeinfo.Dir.myname, dirent_p, delp);
355*1b191cb5SApple OSS Distributions 		}
356*1b191cb5SApple OSS Distributions 		dirnode = dnp; /* continue relative to this directory */
357*1b191cb5SApple OSS Distributions 	}
358*1b191cb5SApple OSS Distributions 	return error;
359*1b191cb5SApple OSS Distributions }
360*1b191cb5SApple OSS Distributions 
361*1b191cb5SApple OSS Distributions 
362*1b191cb5SApple OSS Distributions /***********************************************************************
363*1b191cb5SApple OSS Distributions * Add a new NAME element to the devfs
364*1b191cb5SApple OSS Distributions * If we're creating a root node, then dirname is NULL
365*1b191cb5SApple OSS Distributions * Basically this creates a new namespace entry for the device node
366*1b191cb5SApple OSS Distributions *
367*1b191cb5SApple OSS Distributions * Creates a name node, and links it to the supplied node
368*1b191cb5SApple OSS Distributions *
369*1b191cb5SApple OSS Distributions * called with DEVFS_LOCK held
370*1b191cb5SApple OSS Distributions ***********************************************************************/
371*1b191cb5SApple OSS Distributions int
dev_add_name(const char * name,devnode_t * dirnode,__unused devdirent_t * back,devnode_t * dnp,devdirent_t ** dirent_pp)372*1b191cb5SApple OSS Distributions dev_add_name(const char * name, devnode_t * dirnode, __unused devdirent_t * back,
373*1b191cb5SApple OSS Distributions     devnode_t * dnp, devdirent_t * *dirent_pp)
374*1b191cb5SApple OSS Distributions {
375*1b191cb5SApple OSS Distributions 	devdirent_t *   dirent_p = NULL;
376*1b191cb5SApple OSS Distributions 
377*1b191cb5SApple OSS Distributions 	if (dirnode != NULL) {
378*1b191cb5SApple OSS Distributions 		if (dirnode->dn_type != DEV_DIR) {
379*1b191cb5SApple OSS Distributions 			return ENOTDIR;
380*1b191cb5SApple OSS Distributions 		}
381*1b191cb5SApple OSS Distributions 
382*1b191cb5SApple OSS Distributions 		if (dev_findname(dirnode, name)) {
383*1b191cb5SApple OSS Distributions 			return EEXIST;
384*1b191cb5SApple OSS Distributions 		}
385*1b191cb5SApple OSS Distributions 	}
386*1b191cb5SApple OSS Distributions 	/*
387*1b191cb5SApple OSS Distributions 	 * make sure the name is legal
388*1b191cb5SApple OSS Distributions 	 * slightly misleading in the case of NULL
389*1b191cb5SApple OSS Distributions 	 */
390*1b191cb5SApple OSS Distributions 	if (!name || (strlen(name) > (DEVMAXNAMESIZE - 1))) {
391*1b191cb5SApple OSS Distributions 		return ENAMETOOLONG;
392*1b191cb5SApple OSS Distributions 	}
393*1b191cb5SApple OSS Distributions 
394*1b191cb5SApple OSS Distributions 	/*
395*1b191cb5SApple OSS Distributions 	 * Allocate and fill out a new directory entry
396*1b191cb5SApple OSS Distributions 	 */
397*1b191cb5SApple OSS Distributions 	dirent_p = kalloc_type(devdirent_t, Z_WAITOK | Z_ZERO | Z_NOFAIL);
398*1b191cb5SApple OSS Distributions 
399*1b191cb5SApple OSS Distributions 	/* inherrit our parent's mount info */ /*XXX*/
400*1b191cb5SApple OSS Distributions 	/* a kludge but.... */
401*1b191cb5SApple OSS Distributions 	if (dirnode && (dnp->dn_dvm == NULL)) {
402*1b191cb5SApple OSS Distributions 		dnp->dn_dvm = dirnode->dn_dvm;
403*1b191cb5SApple OSS Distributions 		/* if(!dnp->dn_dvm) printf("parent had null dvm "); */
404*1b191cb5SApple OSS Distributions 	}
405*1b191cb5SApple OSS Distributions 
406*1b191cb5SApple OSS Distributions 	/*
407*1b191cb5SApple OSS Distributions 	 * Link the two together
408*1b191cb5SApple OSS Distributions 	 * include the implicit link in the count of links to the devnode..
409*1b191cb5SApple OSS Distributions 	 * this stops it from being accidentally freed later.
410*1b191cb5SApple OSS Distributions 	 */
411*1b191cb5SApple OSS Distributions 	dirent_p->de_dnp = dnp;
412*1b191cb5SApple OSS Distributions 	dnp->dn_links++;  /* implicit from our own name-node */
413*1b191cb5SApple OSS Distributions 
414*1b191cb5SApple OSS Distributions 	/*
415*1b191cb5SApple OSS Distributions 	 * Make sure that we can find all the links that reference a node
416*1b191cb5SApple OSS Distributions 	 * so that we can get them all if we need to zap the node.
417*1b191cb5SApple OSS Distributions 	 */
418*1b191cb5SApple OSS Distributions 	if (dnp->dn_linklist) {
419*1b191cb5SApple OSS Distributions 		dirent_p->de_nextlink = dnp->dn_linklist;
420*1b191cb5SApple OSS Distributions 		dirent_p->de_prevlinkp = dirent_p->de_nextlink->de_prevlinkp;
421*1b191cb5SApple OSS Distributions 		dirent_p->de_nextlink->de_prevlinkp = &(dirent_p->de_nextlink);
422*1b191cb5SApple OSS Distributions 		*dirent_p->de_prevlinkp = dirent_p;
423*1b191cb5SApple OSS Distributions 	} else {
424*1b191cb5SApple OSS Distributions 		dirent_p->de_nextlink = dirent_p;
425*1b191cb5SApple OSS Distributions 		dirent_p->de_prevlinkp = &(dirent_p->de_nextlink);
426*1b191cb5SApple OSS Distributions 	}
427*1b191cb5SApple OSS Distributions 	dnp->dn_linklist = dirent_p;
428*1b191cb5SApple OSS Distributions 
429*1b191cb5SApple OSS Distributions 	/*
430*1b191cb5SApple OSS Distributions 	 * If the node is a directory, then we need to handle the
431*1b191cb5SApple OSS Distributions 	 * creation of the .. link.
432*1b191cb5SApple OSS Distributions 	 * A NULL dirnode indicates a root node, so point to ourself.
433*1b191cb5SApple OSS Distributions 	 */
434*1b191cb5SApple OSS Distributions 	if (dnp->dn_type == DEV_DIR) {
435*1b191cb5SApple OSS Distributions 		dnp->dn_typeinfo.Dir.myname = dirent_p;
436*1b191cb5SApple OSS Distributions 		/*
437*1b191cb5SApple OSS Distributions 		 * If we are unlinking from an old dir, decrement its links
438*1b191cb5SApple OSS Distributions 		 * as we point our '..' elsewhere
439*1b191cb5SApple OSS Distributions 		 * Note: it's up to the calling code to remove the
440*1b191cb5SApple OSS Distributions 		 * us from the original directory's list
441*1b191cb5SApple OSS Distributions 		 */
442*1b191cb5SApple OSS Distributions 		if (dnp->dn_typeinfo.Dir.parent) {
443*1b191cb5SApple OSS Distributions 			dnp->dn_typeinfo.Dir.parent->dn_links--;
444*1b191cb5SApple OSS Distributions 		}
445*1b191cb5SApple OSS Distributions 		if (dirnode) {
446*1b191cb5SApple OSS Distributions 			dnp->dn_typeinfo.Dir.parent = dirnode;
447*1b191cb5SApple OSS Distributions 		} else {
448*1b191cb5SApple OSS Distributions 			dnp->dn_typeinfo.Dir.parent = dnp;
449*1b191cb5SApple OSS Distributions 		}
450*1b191cb5SApple OSS Distributions 		dnp->dn_typeinfo.Dir.parent->dn_links++; /* account for the new '..' */
451*1b191cb5SApple OSS Distributions 	}
452*1b191cb5SApple OSS Distributions 
453*1b191cb5SApple OSS Distributions 	/*
454*1b191cb5SApple OSS Distributions 	 * put the name into the directory entry.
455*1b191cb5SApple OSS Distributions 	 */
456*1b191cb5SApple OSS Distributions 	strlcpy(dirent_p->de_name, name, DEVMAXNAMESIZE);
457*1b191cb5SApple OSS Distributions 
458*1b191cb5SApple OSS Distributions 
459*1b191cb5SApple OSS Distributions 	/*
460*1b191cb5SApple OSS Distributions 	 * Check if we are not making a root node..
461*1b191cb5SApple OSS Distributions 	 * (i.e. have parent)
462*1b191cb5SApple OSS Distributions 	 */
463*1b191cb5SApple OSS Distributions 	if (dirnode) {
464*1b191cb5SApple OSS Distributions 		/*
465*1b191cb5SApple OSS Distributions 		 * Put it on the END of the linked list of directory entries
466*1b191cb5SApple OSS Distributions 		 */
467*1b191cb5SApple OSS Distributions 		dirent_p->de_parent = dirnode; /* null for root */
468*1b191cb5SApple OSS Distributions 		dirent_p->de_prevp = dirnode->dn_typeinfo.Dir.dirlast;
469*1b191cb5SApple OSS Distributions 		dirent_p->de_next = *(dirent_p->de_prevp); /* should be NULL */
470*1b191cb5SApple OSS Distributions 		                                           /*right?*/
471*1b191cb5SApple OSS Distributions 		*(dirent_p->de_prevp) = dirent_p;
472*1b191cb5SApple OSS Distributions 		dirnode->dn_typeinfo.Dir.dirlast = &(dirent_p->de_next);
473*1b191cb5SApple OSS Distributions 		dirnode->dn_typeinfo.Dir.entrycount++;
474*1b191cb5SApple OSS Distributions 		dirnode->dn_len += strlen(name) + 8;/*ok, ok?*/
475*1b191cb5SApple OSS Distributions 	}
476*1b191cb5SApple OSS Distributions 
477*1b191cb5SApple OSS Distributions 	*dirent_pp = dirent_p;
478*1b191cb5SApple OSS Distributions 	DEVFS_INCR_ENTRIES();
479*1b191cb5SApple OSS Distributions 	return 0;
480*1b191cb5SApple OSS Distributions }
481*1b191cb5SApple OSS Distributions 
482*1b191cb5SApple OSS Distributions 
483*1b191cb5SApple OSS Distributions /***********************************************************************
484*1b191cb5SApple OSS Distributions * Add a new element to the devfs plane.
485*1b191cb5SApple OSS Distributions *
486*1b191cb5SApple OSS Distributions * Creates a new dev_node to go with it if the prototype should not be
487*1b191cb5SApple OSS Distributions * reused. (Is a DIR, or we select SPLIT_DEVS at compile time)
488*1b191cb5SApple OSS Distributions * typeinfo gives us info to make our node if we don't have a prototype.
489*1b191cb5SApple OSS Distributions * If typeinfo is null and proto exists, then the typeinfo field of
490*1b191cb5SApple OSS Distributions * the proto is used intead in the DEVFS_CREATE case.
491*1b191cb5SApple OSS Distributions * note the 'links' count is 0 (except if a dir)
492*1b191cb5SApple OSS Distributions * but it is only cleared on a transition
493*1b191cb5SApple OSS Distributions * so this is ok till we link it to something
494*1b191cb5SApple OSS Distributions * Even in SPLIT_DEVS mode,
495*1b191cb5SApple OSS Distributions * if the node already exists on the wanted plane, just return it
496*1b191cb5SApple OSS Distributions *
497*1b191cb5SApple OSS Distributions * called with DEVFS_LOCK held
498*1b191cb5SApple OSS Distributions ***********************************************************************/
499*1b191cb5SApple OSS Distributions int
dev_add_node(int entrytype,devnode_type_t * typeinfo,devnode_t * proto,devnode_t ** dn_pp,struct devfsmount * dvm)500*1b191cb5SApple OSS Distributions dev_add_node(int entrytype, devnode_type_t * typeinfo, devnode_t * proto,
501*1b191cb5SApple OSS Distributions     devnode_t * *dn_pp, struct devfsmount *dvm)
502*1b191cb5SApple OSS Distributions {
503*1b191cb5SApple OSS Distributions 	devnode_t *     dnp = NULL;
504*1b191cb5SApple OSS Distributions 	int     error = 0;
505*1b191cb5SApple OSS Distributions 
506*1b191cb5SApple OSS Distributions #if defined SPLIT_DEVS
507*1b191cb5SApple OSS Distributions 	/*
508*1b191cb5SApple OSS Distributions 	 * If we have a prototype, then check if there is already a sibling
509*1b191cb5SApple OSS Distributions 	 * on the mount plane we are looking at, if so, just return it.
510*1b191cb5SApple OSS Distributions 	 */
511*1b191cb5SApple OSS Distributions 	if (proto) {
512*1b191cb5SApple OSS Distributions 		dnp = proto->dn_nextsibling;
513*1b191cb5SApple OSS Distributions 		while (dnp != proto) {
514*1b191cb5SApple OSS Distributions 			if (dnp->dn_dvm == dvm) {
515*1b191cb5SApple OSS Distributions 				*dn_pp = dnp;
516*1b191cb5SApple OSS Distributions 				return 0;
517*1b191cb5SApple OSS Distributions 			}
518*1b191cb5SApple OSS Distributions 			dnp = dnp->dn_nextsibling;
519*1b191cb5SApple OSS Distributions 		}
520*1b191cb5SApple OSS Distributions 		if (typeinfo == NULL) {
521*1b191cb5SApple OSS Distributions 			typeinfo = &(proto->dn_typeinfo);
522*1b191cb5SApple OSS Distributions 		}
523*1b191cb5SApple OSS Distributions 	}
524*1b191cb5SApple OSS Distributions #else   /* SPLIT_DEVS */
525*1b191cb5SApple OSS Distributions 	if (proto) {
526*1b191cb5SApple OSS Distributions 		switch (proto->type) {
527*1b191cb5SApple OSS Distributions 		case DEV_BDEV:
528*1b191cb5SApple OSS Distributions 		case DEV_CDEV:
529*1b191cb5SApple OSS Distributions 			*dn_pp = proto;
530*1b191cb5SApple OSS Distributions 			return 0;
531*1b191cb5SApple OSS Distributions 		}
532*1b191cb5SApple OSS Distributions 	}
533*1b191cb5SApple OSS Distributions #endif  /* SPLIT_DEVS */
534*1b191cb5SApple OSS Distributions 	dnp = kalloc_type(devnode_t, Z_WAITOK | Z_ZERO | Z_NOFAIL);
535*1b191cb5SApple OSS Distributions 
536*1b191cb5SApple OSS Distributions 	/*
537*1b191cb5SApple OSS Distributions 	 * If we have a proto, that means that we are duplicating some
538*1b191cb5SApple OSS Distributions 	 * other device, which can only happen if we are not at the back plane
539*1b191cb5SApple OSS Distributions 	 */
540*1b191cb5SApple OSS Distributions 	if (proto) {
541*1b191cb5SApple OSS Distributions 		bcopy(proto, dnp, sizeof(devnode_t));
542*1b191cb5SApple OSS Distributions 		dnp->dn_links = 0;
543*1b191cb5SApple OSS Distributions 		dnp->dn_linklist = NULL;
544*1b191cb5SApple OSS Distributions 		dnp->dn_vn = NULL;
545*1b191cb5SApple OSS Distributions 		dnp->dn_len = 0;
546*1b191cb5SApple OSS Distributions 		/* add to END of siblings list */
547*1b191cb5SApple OSS Distributions 		dnp->dn_prevsiblingp = proto->dn_prevsiblingp;
548*1b191cb5SApple OSS Distributions 		*(dnp->dn_prevsiblingp) = dnp;
549*1b191cb5SApple OSS Distributions 		dnp->dn_nextsibling = proto;
550*1b191cb5SApple OSS Distributions 		proto->dn_prevsiblingp = &(dnp->dn_nextsibling);
551*1b191cb5SApple OSS Distributions #if CONFIG_MACF
552*1b191cb5SApple OSS Distributions 		mac_devfs_label_init(dnp);
553*1b191cb5SApple OSS Distributions 		mac_devfs_label_copy(mac_devfs_label(proto), mac_devfs_label(dnp));
554*1b191cb5SApple OSS Distributions #endif
555*1b191cb5SApple OSS Distributions 	} else {
556*1b191cb5SApple OSS Distributions 		struct timeval tv;
557*1b191cb5SApple OSS Distributions 
558*1b191cb5SApple OSS Distributions 		/*
559*1b191cb5SApple OSS Distributions 		 * We have no prototype, so start off with a clean slate
560*1b191cb5SApple OSS Distributions 		 */
561*1b191cb5SApple OSS Distributions 		microtime(&tv);
562*1b191cb5SApple OSS Distributions 		dnp->dn_type = entrytype;
563*1b191cb5SApple OSS Distributions 		dnp->dn_nextsibling = dnp;
564*1b191cb5SApple OSS Distributions 		dnp->dn_prevsiblingp = &(dnp->dn_nextsibling);
565*1b191cb5SApple OSS Distributions 		dnp->dn_atime.tv_sec = tv.tv_sec;
566*1b191cb5SApple OSS Distributions 		dnp->dn_mtime.tv_sec = tv.tv_sec;
567*1b191cb5SApple OSS Distributions 		dnp->dn_ctime.tv_sec = tv.tv_sec;
568*1b191cb5SApple OSS Distributions #if CONFIG_MACF
569*1b191cb5SApple OSS Distributions 		mac_devfs_label_init(dnp);
570*1b191cb5SApple OSS Distributions #endif
571*1b191cb5SApple OSS Distributions 	}
572*1b191cb5SApple OSS Distributions 	dnp->dn_dvm = dvm;
573*1b191cb5SApple OSS Distributions 
574*1b191cb5SApple OSS Distributions 	/* Note: this inits the reference count to 1, this is considered unreferenced */
575*1b191cb5SApple OSS Distributions 	os_ref_init_raw(&dnp->dn_refcount, &devfs_refgrp);
576*1b191cb5SApple OSS Distributions 	dnp->dn_ino = devfs_unique_fileno;
577*1b191cb5SApple OSS Distributions 	devfs_unique_fileno++;
578*1b191cb5SApple OSS Distributions 
579*1b191cb5SApple OSS Distributions 	/*
580*1b191cb5SApple OSS Distributions 	 * fill out the dev node according to type
581*1b191cb5SApple OSS Distributions 	 */
582*1b191cb5SApple OSS Distributions 	switch (entrytype) {
583*1b191cb5SApple OSS Distributions 	case DEV_DIR:
584*1b191cb5SApple OSS Distributions 		/*
585*1b191cb5SApple OSS Distributions 		 * As it's a directory, make sure
586*1b191cb5SApple OSS Distributions 		 * it has a null entries list
587*1b191cb5SApple OSS Distributions 		 */
588*1b191cb5SApple OSS Distributions 		dnp->dn_typeinfo.Dir.dirlast = &(dnp->dn_typeinfo.Dir.dirlist);
589*1b191cb5SApple OSS Distributions 		dnp->dn_typeinfo.Dir.dirlist = (devdirent_t *)0;
590*1b191cb5SApple OSS Distributions 		dnp->dn_typeinfo.Dir.entrycount = 0;
591*1b191cb5SApple OSS Distributions 		/*  until we know better, it has a null parent pointer*/
592*1b191cb5SApple OSS Distributions 		dnp->dn_typeinfo.Dir.parent = NULL;
593*1b191cb5SApple OSS Distributions 		dnp->dn_links++; /* for .*/
594*1b191cb5SApple OSS Distributions 		dnp->dn_typeinfo.Dir.myname = NULL;
595*1b191cb5SApple OSS Distributions 		/*
596*1b191cb5SApple OSS Distributions 		 * make sure that the ops associated with it are the ops
597*1b191cb5SApple OSS Distributions 		 * that we use (by default) for directories
598*1b191cb5SApple OSS Distributions 		 */
599*1b191cb5SApple OSS Distributions 		dnp->dn_ops = &devfs_vnodeop_p;
600*1b191cb5SApple OSS Distributions 		dnp->dn_mode |= 0555;   /* default perms */
601*1b191cb5SApple OSS Distributions 		break;
602*1b191cb5SApple OSS Distributions 	case DEV_SLNK:
603*1b191cb5SApple OSS Distributions 		/*
604*1b191cb5SApple OSS Distributions 		 * As it's a symlink allocate and store the link info
605*1b191cb5SApple OSS Distributions 		 * Symlinks should only ever be created by the user,
606*1b191cb5SApple OSS Distributions 		 * so they are not on the back plane and should not be
607*1b191cb5SApple OSS Distributions 		 * propogated forward.. a bit like directories in that way..
608*1b191cb5SApple OSS Distributions 		 * A symlink only exists on one plane and has its own
609*1b191cb5SApple OSS Distributions 		 * node.. therefore we might be on any random plane.
610*1b191cb5SApple OSS Distributions 		 */
611*1b191cb5SApple OSS Distributions 		dnp->dn_typeinfo.Slnk.name = kalloc_data(typeinfo->Slnk.namelen + 1, Z_WAITOK);
612*1b191cb5SApple OSS Distributions 		if (!dnp->dn_typeinfo.Slnk.name) {
613*1b191cb5SApple OSS Distributions 			error = ENOMEM;
614*1b191cb5SApple OSS Distributions 			break;
615*1b191cb5SApple OSS Distributions 		}
616*1b191cb5SApple OSS Distributions 		strlcpy(dnp->dn_typeinfo.Slnk.name, typeinfo->Slnk.name,
617*1b191cb5SApple OSS Distributions 		    typeinfo->Slnk.namelen + 1);
618*1b191cb5SApple OSS Distributions 		dnp->dn_typeinfo.Slnk.namelen = typeinfo->Slnk.namelen;
619*1b191cb5SApple OSS Distributions 		DEVFS_INCR_STRINGSPACE(dnp->dn_typeinfo.Slnk.namelen + 1);
620*1b191cb5SApple OSS Distributions 		dnp->dn_ops = &devfs_vnodeop_p;
621*1b191cb5SApple OSS Distributions 		dnp->dn_mode |= 0555;   /* default perms */
622*1b191cb5SApple OSS Distributions 		break;
623*1b191cb5SApple OSS Distributions 	case DEV_CDEV:
624*1b191cb5SApple OSS Distributions 	case DEV_BDEV:
625*1b191cb5SApple OSS Distributions 		/*
626*1b191cb5SApple OSS Distributions 		 * Make sure it has DEVICE type ops
627*1b191cb5SApple OSS Distributions 		 * and device specific fields are correct
628*1b191cb5SApple OSS Distributions 		 */
629*1b191cb5SApple OSS Distributions 		dnp->dn_ops = &devfs_spec_vnodeop_p;
630*1b191cb5SApple OSS Distributions 		dnp->dn_typeinfo.dev = typeinfo->dev;
631*1b191cb5SApple OSS Distributions 		break;
632*1b191cb5SApple OSS Distributions 
633*1b191cb5SApple OSS Distributions 	#if FDESC
634*1b191cb5SApple OSS Distributions 	/* /dev/fd is special */
635*1b191cb5SApple OSS Distributions 	case DEV_DEVFD:
636*1b191cb5SApple OSS Distributions 		dnp->dn_ops = &devfs_devfd_vnodeop_p;
637*1b191cb5SApple OSS Distributions 		dnp->dn_mode |= 0555;   /* default perms */
638*1b191cb5SApple OSS Distributions 		break;
639*1b191cb5SApple OSS Distributions 
640*1b191cb5SApple OSS Distributions 	#endif /* FDESC */
641*1b191cb5SApple OSS Distributions 	default:
642*1b191cb5SApple OSS Distributions 		error = EINVAL;
643*1b191cb5SApple OSS Distributions 	}
644*1b191cb5SApple OSS Distributions 
645*1b191cb5SApple OSS Distributions 	if (error) {
646*1b191cb5SApple OSS Distributions 		kfree_type(devnode_t, dnp);
647*1b191cb5SApple OSS Distributions 	} else {
648*1b191cb5SApple OSS Distributions 		*dn_pp = dnp;
649*1b191cb5SApple OSS Distributions 		DEVFS_INCR_NODES();
650*1b191cb5SApple OSS Distributions 	}
651*1b191cb5SApple OSS Distributions 
652*1b191cb5SApple OSS Distributions 	return error;
653*1b191cb5SApple OSS Distributions }
654*1b191cb5SApple OSS Distributions 
655*1b191cb5SApple OSS Distributions 
656*1b191cb5SApple OSS Distributions /***********************************************************************
657*1b191cb5SApple OSS Distributions  * called with DEVFS_LOCK held
658*1b191cb5SApple OSS Distributions  **********************************************************************/
659*1b191cb5SApple OSS Distributions void
devnode_free(devnode_t * dnp)660*1b191cb5SApple OSS Distributions devnode_free(devnode_t * dnp)
661*1b191cb5SApple OSS Distributions {
662*1b191cb5SApple OSS Distributions #if CONFIG_MACF
663*1b191cb5SApple OSS Distributions 	mac_devfs_label_destroy(dnp);
664*1b191cb5SApple OSS Distributions #endif
665*1b191cb5SApple OSS Distributions 	if (dnp->dn_type == DEV_SLNK) {
666*1b191cb5SApple OSS Distributions 		DEVFS_DECR_STRINGSPACE(dnp->dn_typeinfo.Slnk.namelen + 1);
667*1b191cb5SApple OSS Distributions 		kfree_data(dnp->dn_typeinfo.Slnk.name, dnp->dn_typeinfo.Slnk.namelen + 1);
668*1b191cb5SApple OSS Distributions 	}
669*1b191cb5SApple OSS Distributions 	DEVFS_DECR_NODES();
670*1b191cb5SApple OSS Distributions 	kfree_type(devnode_t, dnp);
671*1b191cb5SApple OSS Distributions }
672*1b191cb5SApple OSS Distributions 
673*1b191cb5SApple OSS Distributions 
674*1b191cb5SApple OSS Distributions /***********************************************************************
675*1b191cb5SApple OSS Distributions  * called with DEVFS_LOCK held
676*1b191cb5SApple OSS Distributions  **********************************************************************/
677*1b191cb5SApple OSS Distributions static void
devfs_dn_free(devnode_t * dnp)678*1b191cb5SApple OSS Distributions devfs_dn_free(devnode_t * dnp)
679*1b191cb5SApple OSS Distributions {
680*1b191cb5SApple OSS Distributions 	if (--dnp->dn_links <= 0) { /* can be -1 for initial free, on error */
681*1b191cb5SApple OSS Distributions 		/*probably need to do other cleanups XXX */
682*1b191cb5SApple OSS Distributions 		if (dnp->dn_nextsibling != dnp) {
683*1b191cb5SApple OSS Distributions 			devnode_t * *   prevp = dnp->dn_prevsiblingp;
684*1b191cb5SApple OSS Distributions 			*prevp = dnp->dn_nextsibling;
685*1b191cb5SApple OSS Distributions 			dnp->dn_nextsibling->dn_prevsiblingp = prevp;
686*1b191cb5SApple OSS Distributions 		}
687*1b191cb5SApple OSS Distributions 
688*1b191cb5SApple OSS Distributions 		/* Can only free if there are no references; otherwise, wait for last vnode to be reclaimed */
689*1b191cb5SApple OSS Distributions 		os_ref_count_t rc = os_ref_get_count_raw(&dnp->dn_refcount);
690*1b191cb5SApple OSS Distributions 		if (rc == 1) {
691*1b191cb5SApple OSS Distributions 			/* release final reference from dev_add_node */
692*1b191cb5SApple OSS Distributions 			(void) os_ref_release_locked_raw(&dnp->dn_refcount, &devfs_refgrp);
693*1b191cb5SApple OSS Distributions 			devnode_free(dnp);
694*1b191cb5SApple OSS Distributions 		} else {
695*1b191cb5SApple OSS Distributions 			dnp->dn_lflags |= DN_DELETE;
696*1b191cb5SApple OSS Distributions 		}
697*1b191cb5SApple OSS Distributions 	}
698*1b191cb5SApple OSS Distributions }
699*1b191cb5SApple OSS Distributions 
700*1b191cb5SApple OSS Distributions /***********************************************************************\
701*1b191cb5SApple OSS Distributions *	Front Node Operations						*
702*1b191cb5SApple OSS Distributions *	Add or delete a chain of front nodes				*
703*1b191cb5SApple OSS Distributions \***********************************************************************/
704*1b191cb5SApple OSS Distributions 
705*1b191cb5SApple OSS Distributions 
706*1b191cb5SApple OSS Distributions /***********************************************************************
707*1b191cb5SApple OSS Distributions * Given a directory backing node, and a child backing node, add the
708*1b191cb5SApple OSS Distributions * appropriate front nodes to the front nodes of the directory to
709*1b191cb5SApple OSS Distributions * represent the child node to the user
710*1b191cb5SApple OSS Distributions *
711*1b191cb5SApple OSS Distributions * on failure, front nodes will either be correct or not exist for each
712*1b191cb5SApple OSS Distributions * front dir, however dirs completed will not be stripped of completed
713*1b191cb5SApple OSS Distributions * frontnodes on failure of a later frontnode
714*1b191cb5SApple OSS Distributions *
715*1b191cb5SApple OSS Distributions * This allows a new node to be propogated through all mounted planes
716*1b191cb5SApple OSS Distributions *
717*1b191cb5SApple OSS Distributions * called with DEVFS_LOCK held
718*1b191cb5SApple OSS Distributions ***********************************************************************/
719*1b191cb5SApple OSS Distributions static int
devfs_propogate(devdirent_t * parent,devdirent_t * child,devfs_event_log_t delp)720*1b191cb5SApple OSS Distributions devfs_propogate(devdirent_t * parent, devdirent_t * child, devfs_event_log_t delp)
721*1b191cb5SApple OSS Distributions {
722*1b191cb5SApple OSS Distributions 	int     error;
723*1b191cb5SApple OSS Distributions 	devdirent_t * newnmp;
724*1b191cb5SApple OSS Distributions 	devnode_t *     dnp = child->de_dnp;
725*1b191cb5SApple OSS Distributions 	devnode_t *     pdnp = parent->de_dnp;
726*1b191cb5SApple OSS Distributions 	devnode_t *     adnp = parent->de_dnp;
727*1b191cb5SApple OSS Distributions 	int type = child->de_dnp->dn_type;
728*1b191cb5SApple OSS Distributions 	uint32_t events;
729*1b191cb5SApple OSS Distributions 
730*1b191cb5SApple OSS Distributions 	events = (dnp->dn_type == DEV_DIR ? VNODE_EVENT_DIR_CREATED : VNODE_EVENT_FILE_CREATED);
731*1b191cb5SApple OSS Distributions 	if (delp != NULL) {
732*1b191cb5SApple OSS Distributions 		devfs_record_event(delp, pdnp, events);
733*1b191cb5SApple OSS Distributions 	}
734*1b191cb5SApple OSS Distributions 
735*1b191cb5SApple OSS Distributions 	/***********************************************
736*1b191cb5SApple OSS Distributions 	* Find the other instances of the parent node
737*1b191cb5SApple OSS Distributions 	***********************************************/
738*1b191cb5SApple OSS Distributions 	for (adnp = pdnp->dn_nextsibling;
739*1b191cb5SApple OSS Distributions 	    adnp != pdnp;
740*1b191cb5SApple OSS Distributions 	    adnp = adnp->dn_nextsibling) {
741*1b191cb5SApple OSS Distributions 		/*
742*1b191cb5SApple OSS Distributions 		 * Make the node, using the original as a prototype)
743*1b191cb5SApple OSS Distributions 		 * if the node already exists on that plane it won't be
744*1b191cb5SApple OSS Distributions 		 * re-made..
745*1b191cb5SApple OSS Distributions 		 */
746*1b191cb5SApple OSS Distributions 		if ((error = dev_add_entry(child->de_name, adnp, type,
747*1b191cb5SApple OSS Distributions 		    NULL, dnp, adnp->dn_dvm,
748*1b191cb5SApple OSS Distributions 		    &newnmp)) != 0) {
749*1b191cb5SApple OSS Distributions 			printf("duplicating %s failed\n", child->de_name);
750*1b191cb5SApple OSS Distributions 		} else {
751*1b191cb5SApple OSS Distributions 			if (delp != NULL) {
752*1b191cb5SApple OSS Distributions 				devfs_record_event(delp, adnp, events);
753*1b191cb5SApple OSS Distributions 
754*1b191cb5SApple OSS Distributions 				/*
755*1b191cb5SApple OSS Distributions 				 * Slightly subtle.  We're guaranteed that there will
756*1b191cb5SApple OSS Distributions 				 * only be a vnode hooked into this devnode if we're creating
757*1b191cb5SApple OSS Distributions 				 * a new link to an existing node; otherwise, the devnode is new
758*1b191cb5SApple OSS Distributions 				 * and no one can have looked it up yet. If we're making a link,
759*1b191cb5SApple OSS Distributions 				 * then the buffer is large enough for two nodes in each
760*1b191cb5SApple OSS Distributions 				 * plane; otherwise, there's no vnode and this call will
761*1b191cb5SApple OSS Distributions 				 * do nothing.
762*1b191cb5SApple OSS Distributions 				 */
763*1b191cb5SApple OSS Distributions 				devfs_record_event(delp, newnmp->de_dnp, VNODE_EVENT_LINK);
764*1b191cb5SApple OSS Distributions 			}
765*1b191cb5SApple OSS Distributions 		}
766*1b191cb5SApple OSS Distributions 	}
767*1b191cb5SApple OSS Distributions 	return 0;       /* for now always succeed */
768*1b191cb5SApple OSS Distributions }
769*1b191cb5SApple OSS Distributions 
770*1b191cb5SApple OSS Distributions static uint32_t
remove_notify_count(devnode_t * dnp)771*1b191cb5SApple OSS Distributions remove_notify_count(devnode_t *dnp)
772*1b191cb5SApple OSS Distributions {
773*1b191cb5SApple OSS Distributions 	uint32_t notify_count = 0;
774*1b191cb5SApple OSS Distributions 	devnode_t *dnp2;
775*1b191cb5SApple OSS Distributions 
776*1b191cb5SApple OSS Distributions 	/*
777*1b191cb5SApple OSS Distributions 	 * Could need to notify for one removed node on each mount and
778*1b191cb5SApple OSS Distributions 	 * one parent for each such node.
779*1b191cb5SApple OSS Distributions 	 */
780*1b191cb5SApple OSS Distributions 	notify_count = devfs_nmountplanes;
781*1b191cb5SApple OSS Distributions 	notify_count += dnp->dn_links;
782*1b191cb5SApple OSS Distributions 	for (dnp2 = dnp->dn_nextsibling; dnp2 != dnp; dnp2 = dnp2->dn_nextsibling) {
783*1b191cb5SApple OSS Distributions 		notify_count += dnp2->dn_links;
784*1b191cb5SApple OSS Distributions 	}
785*1b191cb5SApple OSS Distributions 
786*1b191cb5SApple OSS Distributions 	return notify_count;
787*1b191cb5SApple OSS Distributions }
788*1b191cb5SApple OSS Distributions 
789*1b191cb5SApple OSS Distributions /***********************************************************************
790*1b191cb5SApple OSS Distributions * remove all instances of this devicename [for backing nodes..]
791*1b191cb5SApple OSS Distributions * note.. if there is another link to the node (non dir nodes only)
792*1b191cb5SApple OSS Distributions * then the devfs_node will still exist as the ref count will be non-0
793*1b191cb5SApple OSS Distributions * removing a directory node will remove all sup-nodes on all planes (ZAP)
794*1b191cb5SApple OSS Distributions *
795*1b191cb5SApple OSS Distributions * Used by device drivers to remove nodes that are no longer relevant
796*1b191cb5SApple OSS Distributions * The argument is the 'cookie' they were given when they created the node
797*1b191cb5SApple OSS Distributions * this function is exported.. see devfs.h
798*1b191cb5SApple OSS Distributions ***********************************************************************/
799*1b191cb5SApple OSS Distributions void
devfs_remove(void * dirent_p)800*1b191cb5SApple OSS Distributions devfs_remove(void *dirent_p)
801*1b191cb5SApple OSS Distributions {
802*1b191cb5SApple OSS Distributions 	devnode_t * dnp = ((devdirent_t *)dirent_p)->de_dnp;
803*1b191cb5SApple OSS Distributions 	devnode_t * dnp2;
804*1b191cb5SApple OSS Distributions 	boolean_t   lastlink;
805*1b191cb5SApple OSS Distributions 	struct devfs_event_log event_log;
806*1b191cb5SApple OSS Distributions 	uint32_t    log_count = 0;
807*1b191cb5SApple OSS Distributions 	int         do_notify = 0;
808*1b191cb5SApple OSS Distributions 	int         need_free = 0;
809*1b191cb5SApple OSS Distributions 	struct devfs_vnode_event stackbuf[NUM_STACK_ENTRIES];
810*1b191cb5SApple OSS Distributions 
811*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
812*1b191cb5SApple OSS Distributions 
813*1b191cb5SApple OSS Distributions 	if (!devfs_ready) {
814*1b191cb5SApple OSS Distributions 		printf("devfs_remove: not ready for devices!\n");
815*1b191cb5SApple OSS Distributions 		goto out;
816*1b191cb5SApple OSS Distributions 	}
817*1b191cb5SApple OSS Distributions 
818*1b191cb5SApple OSS Distributions 	log_count = remove_notify_count(dnp);
819*1b191cb5SApple OSS Distributions 
820*1b191cb5SApple OSS Distributions 	if (log_count > NUM_STACK_ENTRIES) {
821*1b191cb5SApple OSS Distributions 		uint32_t new_count;
822*1b191cb5SApple OSS Distributions wrongsize:
823*1b191cb5SApple OSS Distributions 		DEVFS_UNLOCK();
824*1b191cb5SApple OSS Distributions 		if (devfs_init_event_log(&event_log, log_count, NULL) == 0) {
825*1b191cb5SApple OSS Distributions 			do_notify = 1;
826*1b191cb5SApple OSS Distributions 			need_free = 1;
827*1b191cb5SApple OSS Distributions 		}
828*1b191cb5SApple OSS Distributions 		DEVFS_LOCK();
829*1b191cb5SApple OSS Distributions 
830*1b191cb5SApple OSS Distributions 		new_count = remove_notify_count(dnp);
831*1b191cb5SApple OSS Distributions 		if (need_free && (new_count > log_count)) {
832*1b191cb5SApple OSS Distributions 			devfs_release_event_log(&event_log, 1);
833*1b191cb5SApple OSS Distributions 			need_free = 0;
834*1b191cb5SApple OSS Distributions 			do_notify = 0;
835*1b191cb5SApple OSS Distributions 			log_count = log_count * 2;
836*1b191cb5SApple OSS Distributions 			goto wrongsize;
837*1b191cb5SApple OSS Distributions 		}
838*1b191cb5SApple OSS Distributions 	} else {
839*1b191cb5SApple OSS Distributions 		if (devfs_init_event_log(&event_log, NUM_STACK_ENTRIES, &stackbuf[0]) == 0) {
840*1b191cb5SApple OSS Distributions 			do_notify = 1;
841*1b191cb5SApple OSS Distributions 		}
842*1b191cb5SApple OSS Distributions 	}
843*1b191cb5SApple OSS Distributions 
844*1b191cb5SApple OSS Distributions 	/* This file has been deleted */
845*1b191cb5SApple OSS Distributions 	if (do_notify != 0) {
846*1b191cb5SApple OSS Distributions 		devfs_record_event(&event_log, dnp, VNODE_EVENT_DELETE);
847*1b191cb5SApple OSS Distributions 	}
848*1b191cb5SApple OSS Distributions 
849*1b191cb5SApple OSS Distributions 	/* keep removing the next sibling till only we exist. */
850*1b191cb5SApple OSS Distributions 	while ((dnp2 = dnp->dn_nextsibling) != dnp) {
851*1b191cb5SApple OSS Distributions 		/*
852*1b191cb5SApple OSS Distributions 		 * Keep removing the next front node till no more exist
853*1b191cb5SApple OSS Distributions 		 */
854*1b191cb5SApple OSS Distributions 		dnp->dn_nextsibling = dnp2->dn_nextsibling;
855*1b191cb5SApple OSS Distributions 		dnp->dn_nextsibling->dn_prevsiblingp = &(dnp->dn_nextsibling);
856*1b191cb5SApple OSS Distributions 		dnp2->dn_nextsibling = dnp2;
857*1b191cb5SApple OSS Distributions 		dnp2->dn_prevsiblingp = &(dnp2->dn_nextsibling);
858*1b191cb5SApple OSS Distributions 
859*1b191cb5SApple OSS Distributions 		/* This file has been deleted in this plane */
860*1b191cb5SApple OSS Distributions 		if (do_notify != 0) {
861*1b191cb5SApple OSS Distributions 			devfs_record_event(&event_log, dnp2, VNODE_EVENT_DELETE);
862*1b191cb5SApple OSS Distributions 		}
863*1b191cb5SApple OSS Distributions 
864*1b191cb5SApple OSS Distributions 		if (dnp2->dn_linklist) {
865*1b191cb5SApple OSS Distributions 			do {
866*1b191cb5SApple OSS Distributions 				lastlink = (1 == dnp2->dn_links);
867*1b191cb5SApple OSS Distributions 				/* Each parent of a link to this file has lost a child in this plane */
868*1b191cb5SApple OSS Distributions 				if (do_notify != 0) {
869*1b191cb5SApple OSS Distributions 					devfs_record_event(&event_log, dnp2->dn_linklist->de_parent, VNODE_EVENT_FILE_REMOVED);
870*1b191cb5SApple OSS Distributions 				}
871*1b191cb5SApple OSS Distributions 				dev_free_name(dnp2->dn_linklist);
872*1b191cb5SApple OSS Distributions 			} while (!lastlink);
873*1b191cb5SApple OSS Distributions 		}
874*1b191cb5SApple OSS Distributions 	}
875*1b191cb5SApple OSS Distributions 
876*1b191cb5SApple OSS Distributions 	/*
877*1b191cb5SApple OSS Distributions 	 * then free the main node
878*1b191cb5SApple OSS Distributions 	 * If we are not running in SPLIT_DEVS mode, then
879*1b191cb5SApple OSS Distributions 	 * THIS is what gets rid of the propogated nodes.
880*1b191cb5SApple OSS Distributions 	 */
881*1b191cb5SApple OSS Distributions 	if (dnp->dn_linklist) {
882*1b191cb5SApple OSS Distributions 		do {
883*1b191cb5SApple OSS Distributions 			lastlink = (1 == dnp->dn_links);
884*1b191cb5SApple OSS Distributions 			/* Each parent of a link to this file has lost a child */
885*1b191cb5SApple OSS Distributions 			if (do_notify != 0) {
886*1b191cb5SApple OSS Distributions 				devfs_record_event(&event_log, dnp->dn_linklist->de_parent, VNODE_EVENT_FILE_REMOVED);
887*1b191cb5SApple OSS Distributions 			}
888*1b191cb5SApple OSS Distributions 			dev_free_name(dnp->dn_linklist);
889*1b191cb5SApple OSS Distributions 		} while (!lastlink);
890*1b191cb5SApple OSS Distributions 	}
891*1b191cb5SApple OSS Distributions out:
892*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
893*1b191cb5SApple OSS Distributions 	if (do_notify != 0) {
894*1b191cb5SApple OSS Distributions 		devfs_bulk_notify(&event_log);
895*1b191cb5SApple OSS Distributions 		devfs_release_event_log(&event_log, need_free);
896*1b191cb5SApple OSS Distributions 	}
897*1b191cb5SApple OSS Distributions 
898*1b191cb5SApple OSS Distributions 	return;
899*1b191cb5SApple OSS Distributions }
900*1b191cb5SApple OSS Distributions 
901*1b191cb5SApple OSS Distributions 
902*1b191cb5SApple OSS Distributions 
903*1b191cb5SApple OSS Distributions /***************************************************************
904*1b191cb5SApple OSS Distributions  * duplicate the backing tree into a tree of nodes hung off the
905*1b191cb5SApple OSS Distributions  * mount point given as the argument. Do this by
906*1b191cb5SApple OSS Distributions  * calling dev_dup_entry which recurses all the way
907*1b191cb5SApple OSS Distributions  * up the tree..
908*1b191cb5SApple OSS Distributions  *
909*1b191cb5SApple OSS Distributions  * called with DEVFS_LOCK held
910*1b191cb5SApple OSS Distributions  **************************************************************/
911*1b191cb5SApple OSS Distributions int
dev_dup_plane(struct devfsmount * devfs_mp_p)912*1b191cb5SApple OSS Distributions dev_dup_plane(struct devfsmount *devfs_mp_p)
913*1b191cb5SApple OSS Distributions {
914*1b191cb5SApple OSS Distributions 	devdirent_t *   new;
915*1b191cb5SApple OSS Distributions 	int             error = 0;
916*1b191cb5SApple OSS Distributions 
917*1b191cb5SApple OSS Distributions 	if ((error = dev_dup_entry(NULL, dev_root, &new, devfs_mp_p))) {
918*1b191cb5SApple OSS Distributions 		return error;
919*1b191cb5SApple OSS Distributions 	}
920*1b191cb5SApple OSS Distributions 	devfs_mp_p->plane_root = new;
921*1b191cb5SApple OSS Distributions 	devfs_nmountplanes++;
922*1b191cb5SApple OSS Distributions 	return error;
923*1b191cb5SApple OSS Distributions }
924*1b191cb5SApple OSS Distributions 
925*1b191cb5SApple OSS Distributions 
926*1b191cb5SApple OSS Distributions 
927*1b191cb5SApple OSS Distributions /***************************************************************
928*1b191cb5SApple OSS Distributions * Free a whole plane
929*1b191cb5SApple OSS Distributions *
930*1b191cb5SApple OSS Distributions * called with DEVFS_LOCK held
931*1b191cb5SApple OSS Distributions ***************************************************************/
932*1b191cb5SApple OSS Distributions void
devfs_free_plane(struct devfsmount * devfs_mp_p)933*1b191cb5SApple OSS Distributions devfs_free_plane(struct devfsmount *devfs_mp_p)
934*1b191cb5SApple OSS Distributions {
935*1b191cb5SApple OSS Distributions 	devdirent_t * dirent_p;
936*1b191cb5SApple OSS Distributions 
937*1b191cb5SApple OSS Distributions 	dirent_p = devfs_mp_p->plane_root;
938*1b191cb5SApple OSS Distributions 	if (dirent_p) {
939*1b191cb5SApple OSS Distributions 		dev_free_hier(dirent_p);
940*1b191cb5SApple OSS Distributions 		dev_free_name(dirent_p);
941*1b191cb5SApple OSS Distributions 	}
942*1b191cb5SApple OSS Distributions 	devfs_mp_p->plane_root = NULL;
943*1b191cb5SApple OSS Distributions 	devfs_nmountplanes--;
944*1b191cb5SApple OSS Distributions 
945*1b191cb5SApple OSS Distributions 	if (devfs_nmountplanes > (devfs_nmountplanes + 1)) {
946*1b191cb5SApple OSS Distributions 		panic("plane count wrapped around.");
947*1b191cb5SApple OSS Distributions 	}
948*1b191cb5SApple OSS Distributions }
949*1b191cb5SApple OSS Distributions 
950*1b191cb5SApple OSS Distributions 
951*1b191cb5SApple OSS Distributions /***************************************************************
952*1b191cb5SApple OSS Distributions * Create and link in a new front element..
953*1b191cb5SApple OSS Distributions * Parent can be 0 for a root node
954*1b191cb5SApple OSS Distributions * Not presently usable to make a symlink XXX
955*1b191cb5SApple OSS Distributions * (Ok, symlinks don't propogate)
956*1b191cb5SApple OSS Distributions * recursively will create subnodes corresponding to equivalent
957*1b191cb5SApple OSS Distributions * child nodes in the base level
958*1b191cb5SApple OSS Distributions *
959*1b191cb5SApple OSS Distributions * called with DEVFS_LOCK held
960*1b191cb5SApple OSS Distributions ***************************************************************/
961*1b191cb5SApple OSS Distributions static int
dev_dup_entry(devnode_t * parent,devdirent_t * back,devdirent_t ** dnm_pp,struct devfsmount * dvm)962*1b191cb5SApple OSS Distributions dev_dup_entry(devnode_t * parent, devdirent_t * back, devdirent_t * *dnm_pp,
963*1b191cb5SApple OSS Distributions     struct devfsmount *dvm)
964*1b191cb5SApple OSS Distributions {
965*1b191cb5SApple OSS Distributions 	devdirent_t *   entry_p = NULL;
966*1b191cb5SApple OSS Distributions 	devdirent_t *   newback;
967*1b191cb5SApple OSS Distributions 	devdirent_t *   newfront;
968*1b191cb5SApple OSS Distributions 	int     error;
969*1b191cb5SApple OSS Distributions 	devnode_t *     dnp = back->de_dnp;
970*1b191cb5SApple OSS Distributions 	int type = dnp->dn_type;
971*1b191cb5SApple OSS Distributions 
972*1b191cb5SApple OSS Distributions 	/*
973*1b191cb5SApple OSS Distributions 	 * go get the node made (if we need to)
974*1b191cb5SApple OSS Distributions 	 * use the back one as a prototype
975*1b191cb5SApple OSS Distributions 	 */
976*1b191cb5SApple OSS Distributions 	error = dev_add_entry(back->de_name, parent, type, NULL, dnp,
977*1b191cb5SApple OSS Distributions 	    parent?parent->dn_dvm:dvm, &entry_p);
978*1b191cb5SApple OSS Distributions 	if (!error && (entry_p == NULL)) {
979*1b191cb5SApple OSS Distributions 		error = ENOMEM; /* Really can't happen, but make static analyzer happy */
980*1b191cb5SApple OSS Distributions 	}
981*1b191cb5SApple OSS Distributions 	if (error != 0) {
982*1b191cb5SApple OSS Distributions 		printf("duplicating %s failed\n", back->de_name);
983*1b191cb5SApple OSS Distributions 		goto out;
984*1b191cb5SApple OSS Distributions 	}
985*1b191cb5SApple OSS Distributions 
986*1b191cb5SApple OSS Distributions 	/*
987*1b191cb5SApple OSS Distributions 	 * If we have just made the root, then insert the pointer to the
988*1b191cb5SApple OSS Distributions 	 * mount information
989*1b191cb5SApple OSS Distributions 	 */
990*1b191cb5SApple OSS Distributions 	if (dvm) {
991*1b191cb5SApple OSS Distributions 		entry_p->de_dnp->dn_dvm = dvm;
992*1b191cb5SApple OSS Distributions 	}
993*1b191cb5SApple OSS Distributions 
994*1b191cb5SApple OSS Distributions 	/*
995*1b191cb5SApple OSS Distributions 	 * If it is a directory, then recurse down all the other
996*1b191cb5SApple OSS Distributions 	 * subnodes in it....
997*1b191cb5SApple OSS Distributions 	 * note that this time we don't pass on the mount info..
998*1b191cb5SApple OSS Distributions 	 */
999*1b191cb5SApple OSS Distributions 	if (type == DEV_DIR) {
1000*1b191cb5SApple OSS Distributions 		for (newback = back->de_dnp->dn_typeinfo.Dir.dirlist;
1001*1b191cb5SApple OSS Distributions 		    newback; newback = newback->de_next) {
1002*1b191cb5SApple OSS Distributions 			if ((error = dev_dup_entry(entry_p->de_dnp,
1003*1b191cb5SApple OSS Distributions 			    newback, &newfront, NULL)) != 0) {
1004*1b191cb5SApple OSS Distributions 				break; /* back out with an error */
1005*1b191cb5SApple OSS Distributions 			}
1006*1b191cb5SApple OSS Distributions 		}
1007*1b191cb5SApple OSS Distributions 	}
1008*1b191cb5SApple OSS Distributions out:
1009*1b191cb5SApple OSS Distributions 	*dnm_pp = entry_p;
1010*1b191cb5SApple OSS Distributions 	return error;
1011*1b191cb5SApple OSS Distributions }
1012*1b191cb5SApple OSS Distributions 
1013*1b191cb5SApple OSS Distributions 
1014*1b191cb5SApple OSS Distributions /***************************************************************
1015*1b191cb5SApple OSS Distributions * Free a name node
1016*1b191cb5SApple OSS Distributions * remember that if there are other names pointing to the
1017*1b191cb5SApple OSS Distributions * dev_node then it may not get freed yet
1018*1b191cb5SApple OSS Distributions * can handle if there is no dnp
1019*1b191cb5SApple OSS Distributions *
1020*1b191cb5SApple OSS Distributions * called with DEVFS_LOCK held
1021*1b191cb5SApple OSS Distributions ***************************************************************/
1022*1b191cb5SApple OSS Distributions 
1023*1b191cb5SApple OSS Distributions int
dev_free_name(devdirent_t * dirent_p)1024*1b191cb5SApple OSS Distributions dev_free_name(devdirent_t * dirent_p)
1025*1b191cb5SApple OSS Distributions {
1026*1b191cb5SApple OSS Distributions 	devnode_t *     parent = dirent_p->de_parent;
1027*1b191cb5SApple OSS Distributions 	devnode_t *     dnp = dirent_p->de_dnp;
1028*1b191cb5SApple OSS Distributions 
1029*1b191cb5SApple OSS Distributions 	if (dnp) {
1030*1b191cb5SApple OSS Distributions 		if (dnp->dn_type == DEV_DIR) {
1031*1b191cb5SApple OSS Distributions 			devnode_t * p;
1032*1b191cb5SApple OSS Distributions 
1033*1b191cb5SApple OSS Distributions 			if (dnp->dn_typeinfo.Dir.dirlist) {
1034*1b191cb5SApple OSS Distributions 				return ENOTEMPTY;
1035*1b191cb5SApple OSS Distributions 			}
1036*1b191cb5SApple OSS Distributions 			p = dnp->dn_typeinfo.Dir.parent;
1037*1b191cb5SApple OSS Distributions 			devfs_dn_free(dnp);     /* account for '.' */
1038*1b191cb5SApple OSS Distributions 			devfs_dn_free(p);       /* '..' */
1039*1b191cb5SApple OSS Distributions 		}
1040*1b191cb5SApple OSS Distributions 		/*
1041*1b191cb5SApple OSS Distributions 		 * unlink us from the list of links for this node
1042*1b191cb5SApple OSS Distributions 		 * If we are the only link, it's easy!
1043*1b191cb5SApple OSS Distributions 		 * if we are a DIR of course there should not be any
1044*1b191cb5SApple OSS Distributions 		 * other links.
1045*1b191cb5SApple OSS Distributions 		 */
1046*1b191cb5SApple OSS Distributions 		if (dirent_p->de_nextlink == dirent_p) {
1047*1b191cb5SApple OSS Distributions 			dnp->dn_linklist = NULL;
1048*1b191cb5SApple OSS Distributions 		} else {
1049*1b191cb5SApple OSS Distributions 			if (dnp->dn_linklist == dirent_p) {
1050*1b191cb5SApple OSS Distributions 				dnp->dn_linklist = dirent_p->de_nextlink;
1051*1b191cb5SApple OSS Distributions 			}
1052*1b191cb5SApple OSS Distributions 		}
1053*1b191cb5SApple OSS Distributions 		devfs_dn_free(dnp);
1054*1b191cb5SApple OSS Distributions 	}
1055*1b191cb5SApple OSS Distributions 
1056*1b191cb5SApple OSS Distributions 	dirent_p->de_nextlink->de_prevlinkp = dirent_p->de_prevlinkp;
1057*1b191cb5SApple OSS Distributions 	*(dirent_p->de_prevlinkp) = dirent_p->de_nextlink;
1058*1b191cb5SApple OSS Distributions 
1059*1b191cb5SApple OSS Distributions 	/*
1060*1b191cb5SApple OSS Distributions 	 * unlink ourselves from the directory on this plane
1061*1b191cb5SApple OSS Distributions 	 */
1062*1b191cb5SApple OSS Distributions 	if (parent) { /* if not fs root */
1063*1b191cb5SApple OSS Distributions 		if ((*dirent_p->de_prevp = dirent_p->de_next)) {/* yes, assign */
1064*1b191cb5SApple OSS Distributions 			dirent_p->de_next->de_prevp = dirent_p->de_prevp;
1065*1b191cb5SApple OSS Distributions 		} else {
1066*1b191cb5SApple OSS Distributions 			parent->dn_typeinfo.Dir.dirlast
1067*1b191cb5SApple OSS Distributions 			        = dirent_p->de_prevp;
1068*1b191cb5SApple OSS Distributions 		}
1069*1b191cb5SApple OSS Distributions 		parent->dn_typeinfo.Dir.entrycount--;
1070*1b191cb5SApple OSS Distributions 		parent->dn_len -= strlen(dirent_p->de_name) + 8;
1071*1b191cb5SApple OSS Distributions 	}
1072*1b191cb5SApple OSS Distributions 
1073*1b191cb5SApple OSS Distributions 	DEVFS_DECR_ENTRIES();
1074*1b191cb5SApple OSS Distributions 	kfree_type(devdirent_t, dirent_p);
1075*1b191cb5SApple OSS Distributions 	return 0;
1076*1b191cb5SApple OSS Distributions }
1077*1b191cb5SApple OSS Distributions 
1078*1b191cb5SApple OSS Distributions 
1079*1b191cb5SApple OSS Distributions /***************************************************************
1080*1b191cb5SApple OSS Distributions * Free a hierarchy starting at a directory node name
1081*1b191cb5SApple OSS Distributions * remember that if there are other names pointing to the
1082*1b191cb5SApple OSS Distributions * dev_node then it may not get freed yet
1083*1b191cb5SApple OSS Distributions * can handle if there is no dnp
1084*1b191cb5SApple OSS Distributions * leave the node itself allocated.
1085*1b191cb5SApple OSS Distributions *
1086*1b191cb5SApple OSS Distributions * called with DEVFS_LOCK held
1087*1b191cb5SApple OSS Distributions ***************************************************************/
1088*1b191cb5SApple OSS Distributions 
1089*1b191cb5SApple OSS Distributions static void
dev_free_hier(devdirent_t * dirent_p)1090*1b191cb5SApple OSS Distributions dev_free_hier(devdirent_t * dirent_p)
1091*1b191cb5SApple OSS Distributions {
1092*1b191cb5SApple OSS Distributions 	devnode_t *     dnp = dirent_p->de_dnp;
1093*1b191cb5SApple OSS Distributions 
1094*1b191cb5SApple OSS Distributions 	if (dnp) {
1095*1b191cb5SApple OSS Distributions 		if (dnp->dn_type == DEV_DIR) {
1096*1b191cb5SApple OSS Distributions 			while (dnp->dn_typeinfo.Dir.dirlist) {
1097*1b191cb5SApple OSS Distributions 				dev_free_hier(dnp->dn_typeinfo.Dir.dirlist);
1098*1b191cb5SApple OSS Distributions 				dev_free_name(dnp->dn_typeinfo.Dir.dirlist);
1099*1b191cb5SApple OSS Distributions 			}
1100*1b191cb5SApple OSS Distributions 		}
1101*1b191cb5SApple OSS Distributions 	}
1102*1b191cb5SApple OSS Distributions }
1103*1b191cb5SApple OSS Distributions 
1104*1b191cb5SApple OSS Distributions 
1105*1b191cb5SApple OSS Distributions /***************************************************************
1106*1b191cb5SApple OSS Distributions  * given a dev_node, find the appropriate vnode if one is already
1107*1b191cb5SApple OSS Distributions  * associated, or get a new one and associate it with the dev_node
1108*1b191cb5SApple OSS Distributions  *
1109*1b191cb5SApple OSS Distributions  * called with DEVFS_LOCK held
1110*1b191cb5SApple OSS Distributions  *
1111*1b191cb5SApple OSS Distributions  * If an error is returned, then the dnp may have been freed (we
1112*1b191cb5SApple OSS Distributions  * raced with a delete and lost).  A devnode should not be accessed
1113*1b191cb5SApple OSS Distributions  * after devfs_dntovn() fails.
1114*1b191cb5SApple OSS Distributions  ****************************************************************/
1115*1b191cb5SApple OSS Distributions int
devfs_dntovn(devnode_t * dnp,struct vnode ** vn_pp,__unused struct proc * p)1116*1b191cb5SApple OSS Distributions devfs_dntovn(devnode_t * dnp, struct vnode **vn_pp, __unused struct proc * p)
1117*1b191cb5SApple OSS Distributions {
1118*1b191cb5SApple OSS Distributions 	struct vnode *vn_p;
1119*1b191cb5SApple OSS Distributions 	int error = 0;
1120*1b191cb5SApple OSS Distributions 	struct vnode_fsparam vfsp;
1121*1b191cb5SApple OSS Distributions 	enum vtype vtype = 0;
1122*1b191cb5SApple OSS Distributions 	int markroot = 0;
1123*1b191cb5SApple OSS Distributions 	int nretries = 0;
1124*1b191cb5SApple OSS Distributions 	int n_minor = DEVFS_CLONE_ALLOC; /* new minor number for clone device */
1125*1b191cb5SApple OSS Distributions 
1126*1b191cb5SApple OSS Distributions 	/*
1127*1b191cb5SApple OSS Distributions 	 * We should never come in and find that our devnode has been marked for delete.
1128*1b191cb5SApple OSS Distributions 	 * The lookup should have held the lock from entry until now; it should not have
1129*1b191cb5SApple OSS Distributions 	 * been able to find a removed entry. Any other pathway would have just created
1130*1b191cb5SApple OSS Distributions 	 * the devnode and come here without dropping the devfs lock, so no one would
1131*1b191cb5SApple OSS Distributions 	 * have a chance to delete.
1132*1b191cb5SApple OSS Distributions 	 */
1133*1b191cb5SApple OSS Distributions 	if (dnp->dn_lflags & DN_DELETE) {
1134*1b191cb5SApple OSS Distributions 		panic("devfs_dntovn: DN_DELETE set on a devnode upon entry.");
1135*1b191cb5SApple OSS Distributions 	}
1136*1b191cb5SApple OSS Distributions 
1137*1b191cb5SApple OSS Distributions 	devfs_ref_node(dnp);
1138*1b191cb5SApple OSS Distributions 
1139*1b191cb5SApple OSS Distributions retry:
1140*1b191cb5SApple OSS Distributions 	*vn_pp = NULL;
1141*1b191cb5SApple OSS Distributions 	vn_p = dnp->dn_vn;
1142*1b191cb5SApple OSS Distributions 
1143*1b191cb5SApple OSS Distributions 	if (vn_p) { /* already has a vnode */
1144*1b191cb5SApple OSS Distributions 		uint32_t vid;
1145*1b191cb5SApple OSS Distributions 
1146*1b191cb5SApple OSS Distributions 		vid = vnode_vid(vn_p);
1147*1b191cb5SApple OSS Distributions 
1148*1b191cb5SApple OSS Distributions 		vnode_hold(vn_p);
1149*1b191cb5SApple OSS Distributions 		DEVFS_UNLOCK();
1150*1b191cb5SApple OSS Distributions 
1151*1b191cb5SApple OSS Distributions 		/*
1152*1b191cb5SApple OSS Distributions 		 * We want to use the drainok variant of vnode_getwithvid
1153*1b191cb5SApple OSS Distributions 		 * because we _don't_ want to get an iocount if the vnode is
1154*1b191cb5SApple OSS Distributions 		 * is blocked in vnode_drain as it can cause infinite
1155*1b191cb5SApple OSS Distributions 		 * loops in vn_open_auth. While in use vnodes are typically
1156*1b191cb5SApple OSS Distributions 		 * only reclaimed on forced unmounts, In use devfs tty vnodes
1157*1b191cb5SApple OSS Distributions 		 * can  be quite frequently reclaimed by revoke(2) or by the
1158*1b191cb5SApple OSS Distributions 		 * exit of a controlling process.
1159*1b191cb5SApple OSS Distributions 		 */
1160*1b191cb5SApple OSS Distributions 		error = vnode_getwithvid_drainok(vn_p, vid);
1161*1b191cb5SApple OSS Distributions 
1162*1b191cb5SApple OSS Distributions 		vnode_drop(vn_p);
1163*1b191cb5SApple OSS Distributions 		DEVFS_LOCK();
1164*1b191cb5SApple OSS Distributions 
1165*1b191cb5SApple OSS Distributions 		if (dnp->dn_lflags & DN_DELETE) {
1166*1b191cb5SApple OSS Distributions 			/*
1167*1b191cb5SApple OSS Distributions 			 * our BUSY node got marked for
1168*1b191cb5SApple OSS Distributions 			 * deletion while the DEVFS lock
1169*1b191cb5SApple OSS Distributions 			 * was dropped...
1170*1b191cb5SApple OSS Distributions 			 */
1171*1b191cb5SApple OSS Distributions 			if (error == 0) {
1172*1b191cb5SApple OSS Distributions 				/*
1173*1b191cb5SApple OSS Distributions 				 * vnode_getwithvid returned a valid ref
1174*1b191cb5SApple OSS Distributions 				 * which we need to drop
1175*1b191cb5SApple OSS Distributions 				 */
1176*1b191cb5SApple OSS Distributions 				vnode_put(vn_p);
1177*1b191cb5SApple OSS Distributions 			}
1178*1b191cb5SApple OSS Distributions 
1179*1b191cb5SApple OSS Distributions 			/*
1180*1b191cb5SApple OSS Distributions 			 * This entry is no longer in the namespace.  This is only
1181*1b191cb5SApple OSS Distributions 			 * possible for lookup: no other path would not find an existing
1182*1b191cb5SApple OSS Distributions 			 * vnode.  Therefore, ENOENT is a valid result.
1183*1b191cb5SApple OSS Distributions 			 */
1184*1b191cb5SApple OSS Distributions 			error = ENOENT;
1185*1b191cb5SApple OSS Distributions 		} else if (error == ENODEV) {
1186*1b191cb5SApple OSS Distributions 			/*
1187*1b191cb5SApple OSS Distributions 			 * The Filesystem is getting unmounted.
1188*1b191cb5SApple OSS Distributions 			 */
1189*1b191cb5SApple OSS Distributions 			error = ENOENT;
1190*1b191cb5SApple OSS Distributions 		} else if (error && (nretries < DEV_MAX_VNODE_RETRY)) {
1191*1b191cb5SApple OSS Distributions 			/*
1192*1b191cb5SApple OSS Distributions 			 * If we got an error from vnode_getwithvid, it means
1193*1b191cb5SApple OSS Distributions 			 * we raced with a recycle and lost i.e. we asked for
1194*1b191cb5SApple OSS Distributions 			 * an iocount only after vnode_drain had been entered
1195*1b191cb5SApple OSS Distributions 			 * for the vnode and returned with an error only after
1196*1b191cb5SApple OSS Distributions 			 * devfs_reclaim was called on the vnode.  devfs_reclaim
1197*1b191cb5SApple OSS Distributions 			 * sets dn_vn to NULL but while we were waiting to
1198*1b191cb5SApple OSS Distributions 			 * reacquire DEVFS_LOCK, another vnode might have gotten
1199*1b191cb5SApple OSS Distributions 			 * associated with the dnp. In either case, we need to
1200*1b191cb5SApple OSS Distributions 			 * retry otherwise we will end up returning an ENOENT
1201*1b191cb5SApple OSS Distributions 			 * for this lookup but the next lookup will  succeed
1202*1b191cb5SApple OSS Distributions 			 * because it creates a new vnode (or a racing  lookup
1203*1b191cb5SApple OSS Distributions 			 * created a new vnode already).
1204*1b191cb5SApple OSS Distributions 			 */
1205*1b191cb5SApple OSS Distributions 			error = 0;
1206*1b191cb5SApple OSS Distributions 			nretries++;
1207*1b191cb5SApple OSS Distributions 			goto retry;
1208*1b191cb5SApple OSS Distributions 		}
1209*1b191cb5SApple OSS Distributions 		if (!error) {
1210*1b191cb5SApple OSS Distributions 			*vn_pp = vn_p;
1211*1b191cb5SApple OSS Distributions 		}
1212*1b191cb5SApple OSS Distributions 
1213*1b191cb5SApple OSS Distributions 		goto out;
1214*1b191cb5SApple OSS Distributions 	}
1215*1b191cb5SApple OSS Distributions 
1216*1b191cb5SApple OSS Distributions 	/*
1217*1b191cb5SApple OSS Distributions 	 * If we get here, then we've beaten any deletes;
1218*1b191cb5SApple OSS Distributions 	 * if someone sets DN_DELETE during a subsequent drop
1219*1b191cb5SApple OSS Distributions 	 * of the devfs lock, we'll still vend a vnode.
1220*1b191cb5SApple OSS Distributions 	 */
1221*1b191cb5SApple OSS Distributions 
1222*1b191cb5SApple OSS Distributions 	if (dnp->dn_lflags & DN_CREATE) {
1223*1b191cb5SApple OSS Distributions 		dnp->dn_lflags |= DN_CREATEWAIT;
1224*1b191cb5SApple OSS Distributions 		msleep(&dnp->dn_lflags, &devfs_mutex, PRIBIO, 0, 0);
1225*1b191cb5SApple OSS Distributions 		goto retry;
1226*1b191cb5SApple OSS Distributions 	}
1227*1b191cb5SApple OSS Distributions 
1228*1b191cb5SApple OSS Distributions 	dnp->dn_lflags |= DN_CREATE;
1229*1b191cb5SApple OSS Distributions 
1230*1b191cb5SApple OSS Distributions 	switch (dnp->dn_type) {
1231*1b191cb5SApple OSS Distributions 	case    DEV_SLNK:
1232*1b191cb5SApple OSS Distributions 		vtype = VLNK;
1233*1b191cb5SApple OSS Distributions 		break;
1234*1b191cb5SApple OSS Distributions 	case    DEV_DIR:
1235*1b191cb5SApple OSS Distributions 		if (dnp->dn_typeinfo.Dir.parent == dnp) {
1236*1b191cb5SApple OSS Distributions 			markroot = 1;
1237*1b191cb5SApple OSS Distributions 		}
1238*1b191cb5SApple OSS Distributions 		vtype = VDIR;
1239*1b191cb5SApple OSS Distributions 		break;
1240*1b191cb5SApple OSS Distributions 	case    DEV_BDEV:
1241*1b191cb5SApple OSS Distributions 	case    DEV_CDEV:
1242*1b191cb5SApple OSS Distributions 		vtype = (dnp->dn_type == DEV_BDEV) ? VBLK : VCHR;
1243*1b191cb5SApple OSS Distributions 		break;
1244*1b191cb5SApple OSS Distributions #if FDESC
1245*1b191cb5SApple OSS Distributions 	case    DEV_DEVFD:
1246*1b191cb5SApple OSS Distributions 		vtype = VDIR;
1247*1b191cb5SApple OSS Distributions 		break;
1248*1b191cb5SApple OSS Distributions #endif /* FDESC */
1249*1b191cb5SApple OSS Distributions 	}
1250*1b191cb5SApple OSS Distributions 	vfsp.vnfs_mp = dnp->dn_dvm->mount;
1251*1b191cb5SApple OSS Distributions 	vfsp.vnfs_vtype = vtype;
1252*1b191cb5SApple OSS Distributions 	vfsp.vnfs_str = "devfs";
1253*1b191cb5SApple OSS Distributions 	vfsp.vnfs_dvp = 0;
1254*1b191cb5SApple OSS Distributions 	vfsp.vnfs_fsnode = dnp;
1255*1b191cb5SApple OSS Distributions 	vfsp.vnfs_cnp = 0;
1256*1b191cb5SApple OSS Distributions 	vfsp.vnfs_vops = *(dnp->dn_ops);
1257*1b191cb5SApple OSS Distributions 
1258*1b191cb5SApple OSS Distributions 	if (vtype == VBLK || vtype == VCHR) {
1259*1b191cb5SApple OSS Distributions 		/*
1260*1b191cb5SApple OSS Distributions 		 * Ask the clone minor number function for a new minor number
1261*1b191cb5SApple OSS Distributions 		 * to use for the next device instance.  If an administative
1262*1b191cb5SApple OSS Distributions 		 * limit has been reached, this function will return -1.
1263*1b191cb5SApple OSS Distributions 		 */
1264*1b191cb5SApple OSS Distributions 		if (dnp->dn_clone != NULL) {
1265*1b191cb5SApple OSS Distributions 			int     n_major = major(dnp->dn_typeinfo.dev);
1266*1b191cb5SApple OSS Distributions 
1267*1b191cb5SApple OSS Distributions 			n_minor = (*dnp->dn_clone)(dnp->dn_typeinfo.dev, DEVFS_CLONE_ALLOC);
1268*1b191cb5SApple OSS Distributions 			if (n_minor == -1) {
1269*1b191cb5SApple OSS Distributions 				error = ENOMEM;
1270*1b191cb5SApple OSS Distributions 				goto out;
1271*1b191cb5SApple OSS Distributions 			}
1272*1b191cb5SApple OSS Distributions 
1273*1b191cb5SApple OSS Distributions 			vfsp.vnfs_rdev = makedev(n_major, n_minor);
1274*1b191cb5SApple OSS Distributions 		} else {
1275*1b191cb5SApple OSS Distributions 			vfsp.vnfs_rdev = dnp->dn_typeinfo.dev;
1276*1b191cb5SApple OSS Distributions 		}
1277*1b191cb5SApple OSS Distributions 	} else {
1278*1b191cb5SApple OSS Distributions 		vfsp.vnfs_rdev = 0;
1279*1b191cb5SApple OSS Distributions 	}
1280*1b191cb5SApple OSS Distributions 	vfsp.vnfs_filesize = 0;
1281*1b191cb5SApple OSS Distributions 	vfsp.vnfs_flags = VNFS_NOCACHE | VNFS_CANTCACHE;
1282*1b191cb5SApple OSS Distributions 	/* Tag system files */
1283*1b191cb5SApple OSS Distributions 	vfsp.vnfs_marksystem = 0;
1284*1b191cb5SApple OSS Distributions 	vfsp.vnfs_markroot = markroot;
1285*1b191cb5SApple OSS Distributions 
1286*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
1287*1b191cb5SApple OSS Distributions 
1288*1b191cb5SApple OSS Distributions 	error = vnode_create_ext(VNCREATE_FLAVOR, VCREATESIZE, &vfsp, &vn_p,
1289*1b191cb5SApple OSS Distributions 	    VNODE_CREATE_DEFAULT);
1290*1b191cb5SApple OSS Distributions 
1291*1b191cb5SApple OSS Distributions 	/* Do this before grabbing the lock */
1292*1b191cb5SApple OSS Distributions 	if (error == 0) {
1293*1b191cb5SApple OSS Distributions 		vnode_setneedinactive(vn_p);
1294*1b191cb5SApple OSS Distributions 	}
1295*1b191cb5SApple OSS Distributions 
1296*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
1297*1b191cb5SApple OSS Distributions 
1298*1b191cb5SApple OSS Distributions 	if (error == 0) {
1299*1b191cb5SApple OSS Distributions 		vnode_settag(vn_p, VT_DEVFS);
1300*1b191cb5SApple OSS Distributions 
1301*1b191cb5SApple OSS Distributions 		if ((dnp->dn_clone != NULL) && (dnp->dn_vn != NULLVP)) {
1302*1b191cb5SApple OSS Distributions 			panic("devfs_dntovn: cloning device with a vnode?");
1303*1b191cb5SApple OSS Distributions 		}
1304*1b191cb5SApple OSS Distributions 
1305*1b191cb5SApple OSS Distributions 		*vn_pp = vn_p;
1306*1b191cb5SApple OSS Distributions 
1307*1b191cb5SApple OSS Distributions 		/*
1308*1b191cb5SApple OSS Distributions 		 * Another vnode that has this devnode as its v_data.
1309*1b191cb5SApple OSS Distributions 		 * This reference, unlike the one taken at the start
1310*1b191cb5SApple OSS Distributions 		 * of the function, persists until a VNOP_RECLAIM
1311*1b191cb5SApple OSS Distributions 		 * comes through for this vnode.
1312*1b191cb5SApple OSS Distributions 		 */
1313*1b191cb5SApple OSS Distributions 		devfs_ref_node(dnp);
1314*1b191cb5SApple OSS Distributions 
1315*1b191cb5SApple OSS Distributions 		/*
1316*1b191cb5SApple OSS Distributions 		 * A cloned vnode is not hooked into the devnode; every lookup
1317*1b191cb5SApple OSS Distributions 		 * gets a new vnode.
1318*1b191cb5SApple OSS Distributions 		 */
1319*1b191cb5SApple OSS Distributions 		if (dnp->dn_clone == NULL) {
1320*1b191cb5SApple OSS Distributions 			dnp->dn_vn = vn_p;
1321*1b191cb5SApple OSS Distributions 		}
1322*1b191cb5SApple OSS Distributions 	} else if (n_minor != DEVFS_CLONE_ALLOC) {
1323*1b191cb5SApple OSS Distributions 		/*
1324*1b191cb5SApple OSS Distributions 		 * If we failed the create, we need to release the cloned minor
1325*1b191cb5SApple OSS Distributions 		 * back to the free list.  In general, this is only useful if
1326*1b191cb5SApple OSS Distributions 		 * the clone function results in a state change in the cloned
1327*1b191cb5SApple OSS Distributions 		 * device for which the minor number was obtained.  If we get
1328*1b191cb5SApple OSS Distributions 		 * past this point withouth falling into this case, it's
1329*1b191cb5SApple OSS Distributions 		 * assumed that any state to be released will be released when
1330*1b191cb5SApple OSS Distributions 		 * the vnode is dropped, instead.
1331*1b191cb5SApple OSS Distributions 		 */
1332*1b191cb5SApple OSS Distributions 		(void)(*dnp->dn_clone)(dnp->dn_typeinfo.dev, DEVFS_CLONE_FREE);
1333*1b191cb5SApple OSS Distributions 	}
1334*1b191cb5SApple OSS Distributions 
1335*1b191cb5SApple OSS Distributions 	dnp->dn_lflags &= ~DN_CREATE;
1336*1b191cb5SApple OSS Distributions 	if (dnp->dn_lflags & DN_CREATEWAIT) {
1337*1b191cb5SApple OSS Distributions 		dnp->dn_lflags &= ~DN_CREATEWAIT;
1338*1b191cb5SApple OSS Distributions 		wakeup(&dnp->dn_lflags);
1339*1b191cb5SApple OSS Distributions 	}
1340*1b191cb5SApple OSS Distributions 
1341*1b191cb5SApple OSS Distributions out:
1342*1b191cb5SApple OSS Distributions 	/*
1343*1b191cb5SApple OSS Distributions 	 * Release the reference we took to prevent deletion while we weren't holding the lock.
1344*1b191cb5SApple OSS Distributions 	 * If not returning success, then dropping this reference could delete the devnode;
1345*1b191cb5SApple OSS Distributions 	 * no one should access a devnode after a call to devfs_dntovn fails.
1346*1b191cb5SApple OSS Distributions 	 */
1347*1b191cb5SApple OSS Distributions 	devfs_rele_node(dnp);
1348*1b191cb5SApple OSS Distributions 
1349*1b191cb5SApple OSS Distributions 	return error;
1350*1b191cb5SApple OSS Distributions }
1351*1b191cb5SApple OSS Distributions 
1352*1b191cb5SApple OSS Distributions /*
1353*1b191cb5SApple OSS Distributions  * Increment refcount on a devnode; prevents free of the node
1354*1b191cb5SApple OSS Distributions  * while the devfs lock is not held.
1355*1b191cb5SApple OSS Distributions  */
1356*1b191cb5SApple OSS Distributions void
devfs_ref_node(devnode_t * dnp)1357*1b191cb5SApple OSS Distributions devfs_ref_node(devnode_t *dnp)
1358*1b191cb5SApple OSS Distributions {
1359*1b191cb5SApple OSS Distributions 	os_ref_retain_locked_raw(&dnp->dn_refcount, &devfs_refgrp);
1360*1b191cb5SApple OSS Distributions }
1361*1b191cb5SApple OSS Distributions 
1362*1b191cb5SApple OSS Distributions /*
1363*1b191cb5SApple OSS Distributions  * Release a reference on a devnode.  If the devnode is marked for
1364*1b191cb5SApple OSS Distributions  * free and the refcount is dropped to one, do the free.
1365*1b191cb5SApple OSS Distributions  */
1366*1b191cb5SApple OSS Distributions void
devfs_rele_node(devnode_t * dnp)1367*1b191cb5SApple OSS Distributions devfs_rele_node(devnode_t *dnp)
1368*1b191cb5SApple OSS Distributions {
1369*1b191cb5SApple OSS Distributions 	os_ref_count_t rc = os_ref_release_locked_raw(&dnp->dn_refcount, &devfs_refgrp);
1370*1b191cb5SApple OSS Distributions 	if (rc < 1) {
1371*1b191cb5SApple OSS Distributions 		panic("devfs_rele_node: devnode without a refcount!");
1372*1b191cb5SApple OSS Distributions 	} else if ((rc == 1) && (dnp->dn_lflags & DN_DELETE)) {
1373*1b191cb5SApple OSS Distributions 		/* release final reference from dev_add_node */
1374*1b191cb5SApple OSS Distributions 		(void) os_ref_release_locked_raw(&dnp->dn_refcount, &devfs_refgrp);
1375*1b191cb5SApple OSS Distributions 		devnode_free(dnp);
1376*1b191cb5SApple OSS Distributions 	}
1377*1b191cb5SApple OSS Distributions }
1378*1b191cb5SApple OSS Distributions 
1379*1b191cb5SApple OSS Distributions /***********************************************************************
1380*1b191cb5SApple OSS Distributions * add a whole device, with no prototype.. make name element and node
1381*1b191cb5SApple OSS Distributions * Used for adding the original device entries
1382*1b191cb5SApple OSS Distributions *
1383*1b191cb5SApple OSS Distributions * called with DEVFS_LOCK held
1384*1b191cb5SApple OSS Distributions ***********************************************************************/
1385*1b191cb5SApple OSS Distributions int
dev_add_entry(const char * name,devnode_t * parent,int type,devnode_type_t * typeinfo,devnode_t * proto,struct devfsmount * dvm,devdirent_t ** nm_pp)1386*1b191cb5SApple OSS Distributions dev_add_entry(const char *name, devnode_t * parent, int type, devnode_type_t * typeinfo,
1387*1b191cb5SApple OSS Distributions     devnode_t * proto, struct devfsmount *dvm, devdirent_t * *nm_pp)
1388*1b191cb5SApple OSS Distributions {
1389*1b191cb5SApple OSS Distributions 	devnode_t *     dnp;
1390*1b191cb5SApple OSS Distributions 	int     error = 0;
1391*1b191cb5SApple OSS Distributions 
1392*1b191cb5SApple OSS Distributions 	if ((error = dev_add_node(type, typeinfo, proto, &dnp,
1393*1b191cb5SApple OSS Distributions 	    (parent?parent->dn_dvm:dvm))) != 0) {
1394*1b191cb5SApple OSS Distributions 		printf("devfs: %s: base node allocation failed (Errno=%d)\n",
1395*1b191cb5SApple OSS Distributions 		    name, error);
1396*1b191cb5SApple OSS Distributions 		return error;
1397*1b191cb5SApple OSS Distributions 	}
1398*1b191cb5SApple OSS Distributions 	if ((error = dev_add_name(name, parent, NULL, dnp, nm_pp)) != 0) {
1399*1b191cb5SApple OSS Distributions 		devfs_dn_free(dnp); /* 1->0 for dir, 0->(-1) for other */
1400*1b191cb5SApple OSS Distributions 		printf("devfs: %s: name slot allocation failed (Errno=%d)\n",
1401*1b191cb5SApple OSS Distributions 		    name, error);
1402*1b191cb5SApple OSS Distributions 	}
1403*1b191cb5SApple OSS Distributions 	return error;
1404*1b191cb5SApple OSS Distributions }
1405*1b191cb5SApple OSS Distributions 
1406*1b191cb5SApple OSS Distributions static void
devfs_bulk_notify(devfs_event_log_t delp)1407*1b191cb5SApple OSS Distributions devfs_bulk_notify(devfs_event_log_t delp)
1408*1b191cb5SApple OSS Distributions {
1409*1b191cb5SApple OSS Distributions 	uint32_t i;
1410*1b191cb5SApple OSS Distributions 	for (i = 0; i < delp->del_used; i++) {
1411*1b191cb5SApple OSS Distributions 		devfs_vnode_event_t dvep = &delp->del_entries[i];
1412*1b191cb5SApple OSS Distributions 		if (vnode_getwithvid(dvep->dve_vp, dvep->dve_vid) == 0) {
1413*1b191cb5SApple OSS Distributions 			vnode_notify(dvep->dve_vp, dvep->dve_events, NULL);
1414*1b191cb5SApple OSS Distributions 			vnode_put(dvep->dve_vp);
1415*1b191cb5SApple OSS Distributions 		}
1416*1b191cb5SApple OSS Distributions 		vnode_drop(dvep->dve_vp);
1417*1b191cb5SApple OSS Distributions 	}
1418*1b191cb5SApple OSS Distributions }
1419*1b191cb5SApple OSS Distributions 
1420*1b191cb5SApple OSS Distributions static void
devfs_record_event(devfs_event_log_t delp,devnode_t * dnp,uint32_t events)1421*1b191cb5SApple OSS Distributions devfs_record_event(devfs_event_log_t delp, devnode_t *dnp, uint32_t events)
1422*1b191cb5SApple OSS Distributions {
1423*1b191cb5SApple OSS Distributions 	if (delp->del_used >= delp->del_max) {
1424*1b191cb5SApple OSS Distributions 		panic("devfs event log overflowed.");
1425*1b191cb5SApple OSS Distributions 	}
1426*1b191cb5SApple OSS Distributions 
1427*1b191cb5SApple OSS Distributions 	/* Can only notify for nodes that have an associated vnode */
1428*1b191cb5SApple OSS Distributions 	if (dnp->dn_vn != NULLVP && vnode_ismonitored(dnp->dn_vn)) {
1429*1b191cb5SApple OSS Distributions 		devfs_vnode_event_t dvep = &delp->del_entries[delp->del_used];
1430*1b191cb5SApple OSS Distributions 		dvep->dve_vp = dnp->dn_vn;
1431*1b191cb5SApple OSS Distributions 		dvep->dve_vid = vnode_vid(dnp->dn_vn);
1432*1b191cb5SApple OSS Distributions 		vnode_hold(dvep->dve_vp);
1433*1b191cb5SApple OSS Distributions 		dvep->dve_events = events;
1434*1b191cb5SApple OSS Distributions 		delp->del_used++;
1435*1b191cb5SApple OSS Distributions 	}
1436*1b191cb5SApple OSS Distributions }
1437*1b191cb5SApple OSS Distributions 
1438*1b191cb5SApple OSS Distributions static int
devfs_init_event_log(devfs_event_log_t delp,uint32_t count,devfs_vnode_event_t buf)1439*1b191cb5SApple OSS Distributions devfs_init_event_log(devfs_event_log_t delp, uint32_t count, devfs_vnode_event_t buf)
1440*1b191cb5SApple OSS Distributions {
1441*1b191cb5SApple OSS Distributions 	devfs_vnode_event_t dvearr;
1442*1b191cb5SApple OSS Distributions 
1443*1b191cb5SApple OSS Distributions 	if (buf == NULL) {
1444*1b191cb5SApple OSS Distributions 		dvearr = kalloc_type(struct devfs_vnode_event, count,
1445*1b191cb5SApple OSS Distributions 		    Z_WAITOK | Z_ZERO);
1446*1b191cb5SApple OSS Distributions 		if (dvearr == NULL) {
1447*1b191cb5SApple OSS Distributions 			return ENOMEM;
1448*1b191cb5SApple OSS Distributions 		}
1449*1b191cb5SApple OSS Distributions 	} else {
1450*1b191cb5SApple OSS Distributions 		dvearr = buf;
1451*1b191cb5SApple OSS Distributions 	}
1452*1b191cb5SApple OSS Distributions 
1453*1b191cb5SApple OSS Distributions 	delp->del_max = count;
1454*1b191cb5SApple OSS Distributions 	delp->del_used = 0;
1455*1b191cb5SApple OSS Distributions 	delp->del_entries = dvearr;
1456*1b191cb5SApple OSS Distributions 	return 0;
1457*1b191cb5SApple OSS Distributions }
1458*1b191cb5SApple OSS Distributions 
1459*1b191cb5SApple OSS Distributions static void
devfs_release_event_log(devfs_event_log_t delp,int need_free)1460*1b191cb5SApple OSS Distributions devfs_release_event_log(devfs_event_log_t delp, int need_free)
1461*1b191cb5SApple OSS Distributions {
1462*1b191cb5SApple OSS Distributions 	if (delp->del_entries == NULL) {
1463*1b191cb5SApple OSS Distributions 		panic("Free of devfs notify info that has not been intialized.");
1464*1b191cb5SApple OSS Distributions 	}
1465*1b191cb5SApple OSS Distributions 
1466*1b191cb5SApple OSS Distributions 	if (need_free) {
1467*1b191cb5SApple OSS Distributions 		kfree_type(struct devfs_vnode_event, delp->del_max,
1468*1b191cb5SApple OSS Distributions 		    delp->del_entries);
1469*1b191cb5SApple OSS Distributions 	}
1470*1b191cb5SApple OSS Distributions 
1471*1b191cb5SApple OSS Distributions 	delp->del_entries = NULL;
1472*1b191cb5SApple OSS Distributions }
1473*1b191cb5SApple OSS Distributions 
1474*1b191cb5SApple OSS Distributions /*
1475*1b191cb5SApple OSS Distributions  * Function: devfs_make_node
1476*1b191cb5SApple OSS Distributions  *
1477*1b191cb5SApple OSS Distributions  * Purpose
1478*1b191cb5SApple OSS Distributions  *   Create a device node with the given pathname in the devfs namespace.
1479*1b191cb5SApple OSS Distributions  *
1480*1b191cb5SApple OSS Distributions  * Parameters:
1481*1b191cb5SApple OSS Distributions  *   dev        - the dev_t value to associate
1482*1b191cb5SApple OSS Distributions  *   chrblk	- block or character device (DEVFS_CHAR or DEVFS_BLOCK)
1483*1b191cb5SApple OSS Distributions  *   uid, gid	- ownership
1484*1b191cb5SApple OSS Distributions  *   perms	- permissions
1485*1b191cb5SApple OSS Distributions  *   clone	- minor number cloning function
1486*1b191cb5SApple OSS Distributions  *   fmt, ...	- path format string with printf args to format the path name
1487*1b191cb5SApple OSS Distributions  * Returns:
1488*1b191cb5SApple OSS Distributions  *   A handle to a device node if successful, NULL otherwise.
1489*1b191cb5SApple OSS Distributions  */
1490*1b191cb5SApple OSS Distributions void *
devfs_make_node_clone(dev_t dev,int chrblk,uid_t uid,gid_t gid,int perms,int (* clone)(dev_t dev,int action),const char * fmt,...)1491*1b191cb5SApple OSS Distributions devfs_make_node_clone(dev_t dev, int chrblk, uid_t uid,
1492*1b191cb5SApple OSS Distributions     gid_t gid, int perms, int (*clone)(dev_t dev, int action),
1493*1b191cb5SApple OSS Distributions     const char *fmt, ...)
1494*1b191cb5SApple OSS Distributions {
1495*1b191cb5SApple OSS Distributions 	devdirent_t *   new_dev = NULL;
1496*1b191cb5SApple OSS Distributions 	devfstype_t     type;
1497*1b191cb5SApple OSS Distributions 	va_list ap;
1498*1b191cb5SApple OSS Distributions 
1499*1b191cb5SApple OSS Distributions 	switch (chrblk) {
1500*1b191cb5SApple OSS Distributions 	case DEVFS_CHAR:
1501*1b191cb5SApple OSS Distributions 		type = DEV_CDEV;
1502*1b191cb5SApple OSS Distributions 		break;
1503*1b191cb5SApple OSS Distributions 	case DEVFS_BLOCK:
1504*1b191cb5SApple OSS Distributions 		type = DEV_BDEV;
1505*1b191cb5SApple OSS Distributions 		break;
1506*1b191cb5SApple OSS Distributions 	default:
1507*1b191cb5SApple OSS Distributions 		goto out;
1508*1b191cb5SApple OSS Distributions 	}
1509*1b191cb5SApple OSS Distributions 
1510*1b191cb5SApple OSS Distributions 	va_start(ap, fmt);
1511*1b191cb5SApple OSS Distributions 	new_dev = devfs_make_node_internal(dev, type, uid, gid, perms, clone, fmt, ap);
1512*1b191cb5SApple OSS Distributions 	va_end(ap);
1513*1b191cb5SApple OSS Distributions out:
1514*1b191cb5SApple OSS Distributions 	return new_dev;
1515*1b191cb5SApple OSS Distributions }
1516*1b191cb5SApple OSS Distributions 
1517*1b191cb5SApple OSS Distributions 
1518*1b191cb5SApple OSS Distributions /*
1519*1b191cb5SApple OSS Distributions  * Function: devfs_make_node
1520*1b191cb5SApple OSS Distributions  *
1521*1b191cb5SApple OSS Distributions  * Purpose
1522*1b191cb5SApple OSS Distributions  *   Create a device node with the given pathname in the devfs namespace.
1523*1b191cb5SApple OSS Distributions  *
1524*1b191cb5SApple OSS Distributions  * Parameters:
1525*1b191cb5SApple OSS Distributions  *   dev        - the dev_t value to associate
1526*1b191cb5SApple OSS Distributions  *   chrblk	- block or character device (DEVFS_CHAR or DEVFS_BLOCK)
1527*1b191cb5SApple OSS Distributions  *   uid, gid	- ownership
1528*1b191cb5SApple OSS Distributions  *   perms	- permissions
1529*1b191cb5SApple OSS Distributions  *   fmt, ...	- path format string with printf args to format the path name
1530*1b191cb5SApple OSS Distributions  * Returns:
1531*1b191cb5SApple OSS Distributions  *   A handle to a device node if successful, NULL otherwise.
1532*1b191cb5SApple OSS Distributions  */
1533*1b191cb5SApple OSS Distributions void *
devfs_make_node(dev_t dev,int chrblk,uid_t uid,gid_t gid,int perms,const char * fmt,...)1534*1b191cb5SApple OSS Distributions devfs_make_node(dev_t dev, int chrblk, uid_t uid,
1535*1b191cb5SApple OSS Distributions     gid_t gid, int perms, const char *fmt, ...)
1536*1b191cb5SApple OSS Distributions {
1537*1b191cb5SApple OSS Distributions 	devdirent_t *   new_dev = NULL;
1538*1b191cb5SApple OSS Distributions 	devfstype_t type;
1539*1b191cb5SApple OSS Distributions 	va_list ap;
1540*1b191cb5SApple OSS Distributions 
1541*1b191cb5SApple OSS Distributions 	if (chrblk != DEVFS_CHAR && chrblk != DEVFS_BLOCK) {
1542*1b191cb5SApple OSS Distributions 		goto out;
1543*1b191cb5SApple OSS Distributions 	}
1544*1b191cb5SApple OSS Distributions 
1545*1b191cb5SApple OSS Distributions 	type = (chrblk == DEVFS_BLOCK ? DEV_BDEV : DEV_CDEV);
1546*1b191cb5SApple OSS Distributions 
1547*1b191cb5SApple OSS Distributions 	va_start(ap, fmt);
1548*1b191cb5SApple OSS Distributions 	new_dev = devfs_make_node_internal(dev, type, uid, gid, perms, NULL, fmt, ap);
1549*1b191cb5SApple OSS Distributions 	va_end(ap);
1550*1b191cb5SApple OSS Distributions 
1551*1b191cb5SApple OSS Distributions out:
1552*1b191cb5SApple OSS Distributions 	return new_dev;
1553*1b191cb5SApple OSS Distributions }
1554*1b191cb5SApple OSS Distributions 
1555*1b191cb5SApple OSS Distributions __printflike(7, 0)
1556*1b191cb5SApple OSS Distributions static devdirent_t *
devfs_make_node_internal(dev_t dev,devfstype_t type,uid_t uid,gid_t gid,int perms,int (* clone)(dev_t dev,int action),const char * fmt,va_list ap)1557*1b191cb5SApple OSS Distributions devfs_make_node_internal(dev_t dev, devfstype_t type, uid_t uid,
1558*1b191cb5SApple OSS Distributions     gid_t gid, int perms, int (*clone)(dev_t dev, int action), const char *fmt, va_list ap)
1559*1b191cb5SApple OSS Distributions {
1560*1b191cb5SApple OSS Distributions 	devdirent_t *   new_dev = NULL;
1561*1b191cb5SApple OSS Distributions 	devnode_t * dnp;
1562*1b191cb5SApple OSS Distributions 	devnode_type_t  typeinfo;
1563*1b191cb5SApple OSS Distributions 
1564*1b191cb5SApple OSS Distributions 	char            *name, buf[256]; /* XXX */
1565*1b191cb5SApple OSS Distributions 	const char      *path;
1566*1b191cb5SApple OSS Distributions #if CONFIG_MACF
1567*1b191cb5SApple OSS Distributions 	char buff[sizeof(buf)];
1568*1b191cb5SApple OSS Distributions #endif
1569*1b191cb5SApple OSS Distributions 	size_t          i;
1570*1b191cb5SApple OSS Distributions 	uint32_t        log_count;
1571*1b191cb5SApple OSS Distributions 	struct devfs_event_log event_log;
1572*1b191cb5SApple OSS Distributions 	struct devfs_vnode_event stackbuf[NUM_STACK_ENTRIES];
1573*1b191cb5SApple OSS Distributions 	int             need_free = 0;
1574*1b191cb5SApple OSS Distributions 
1575*1b191cb5SApple OSS Distributions 	vsnprintf(buf, sizeof(buf), fmt, ap);
1576*1b191cb5SApple OSS Distributions 
1577*1b191cb5SApple OSS Distributions #if CONFIG_MACF
1578*1b191cb5SApple OSS Distributions 	bcopy(buf, buff, sizeof(buff));
1579*1b191cb5SApple OSS Distributions 	buff[sizeof(buff) - 1] = 0;
1580*1b191cb5SApple OSS Distributions #endif
1581*1b191cb5SApple OSS Distributions 	name = NULL;
1582*1b191cb5SApple OSS Distributions 
1583*1b191cb5SApple OSS Distributions 	for (i = strlen(buf); i > 0; i--) {
1584*1b191cb5SApple OSS Distributions 		if (buf[i] == '/') {
1585*1b191cb5SApple OSS Distributions 			name = &buf[i];
1586*1b191cb5SApple OSS Distributions 			buf[i] = 0;
1587*1b191cb5SApple OSS Distributions 			break;
1588*1b191cb5SApple OSS Distributions 		}
1589*1b191cb5SApple OSS Distributions 	}
1590*1b191cb5SApple OSS Distributions 
1591*1b191cb5SApple OSS Distributions 	if (name) {
1592*1b191cb5SApple OSS Distributions 		*name++ = '\0';
1593*1b191cb5SApple OSS Distributions 		path = buf;
1594*1b191cb5SApple OSS Distributions 	} else {
1595*1b191cb5SApple OSS Distributions 		name = buf;
1596*1b191cb5SApple OSS Distributions 		path = "/";
1597*1b191cb5SApple OSS Distributions 	}
1598*1b191cb5SApple OSS Distributions 
1599*1b191cb5SApple OSS Distributions 	log_count = devfs_nmountplanes;
1600*1b191cb5SApple OSS Distributions 	if (log_count > NUM_STACK_ENTRIES) {
1601*1b191cb5SApple OSS Distributions wrongsize:
1602*1b191cb5SApple OSS Distributions 		need_free = 1;
1603*1b191cb5SApple OSS Distributions 		if (devfs_init_event_log(&event_log, log_count, NULL) != 0) {
1604*1b191cb5SApple OSS Distributions 			return NULL;
1605*1b191cb5SApple OSS Distributions 		}
1606*1b191cb5SApple OSS Distributions 	} else {
1607*1b191cb5SApple OSS Distributions 		need_free = 0;
1608*1b191cb5SApple OSS Distributions 		log_count = NUM_STACK_ENTRIES;
1609*1b191cb5SApple OSS Distributions 		if (devfs_init_event_log(&event_log, log_count, &stackbuf[0]) != 0) {
1610*1b191cb5SApple OSS Distributions 			return NULL;
1611*1b191cb5SApple OSS Distributions 		}
1612*1b191cb5SApple OSS Distributions 	}
1613*1b191cb5SApple OSS Distributions 
1614*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
1615*1b191cb5SApple OSS Distributions 	if (log_count < devfs_nmountplanes) {
1616*1b191cb5SApple OSS Distributions 		DEVFS_UNLOCK();
1617*1b191cb5SApple OSS Distributions 		devfs_release_event_log(&event_log, need_free);
1618*1b191cb5SApple OSS Distributions 		log_count = log_count * 2;
1619*1b191cb5SApple OSS Distributions 		goto wrongsize;
1620*1b191cb5SApple OSS Distributions 	}
1621*1b191cb5SApple OSS Distributions 
1622*1b191cb5SApple OSS Distributions 	if (!devfs_ready) {
1623*1b191cb5SApple OSS Distributions 		printf("devfs_make_node: not ready for devices!\n");
1624*1b191cb5SApple OSS Distributions 		goto out;
1625*1b191cb5SApple OSS Distributions 	}
1626*1b191cb5SApple OSS Distributions 
1627*1b191cb5SApple OSS Distributions 	/* find/create directory path ie. mkdir -p */
1628*1b191cb5SApple OSS Distributions 	if (dev_finddir(path, NULL, DEVFS_CREATE, &dnp, &event_log) == 0) {
1629*1b191cb5SApple OSS Distributions 		typeinfo.dev = dev;
1630*1b191cb5SApple OSS Distributions 		if (dev_add_entry(name, dnp, type, &typeinfo, NULL, NULL, &new_dev) == 0) {
1631*1b191cb5SApple OSS Distributions 			new_dev->de_dnp->dn_gid = gid;
1632*1b191cb5SApple OSS Distributions 			new_dev->de_dnp->dn_uid = uid;
1633*1b191cb5SApple OSS Distributions 			new_dev->de_dnp->dn_mode |= perms;
1634*1b191cb5SApple OSS Distributions 			new_dev->de_dnp->dn_clone = clone;
1635*1b191cb5SApple OSS Distributions #if CONFIG_MACF
1636*1b191cb5SApple OSS Distributions 			mac_devfs_label_associate_device(dev, new_dev->de_dnp, buff);
1637*1b191cb5SApple OSS Distributions #endif
1638*1b191cb5SApple OSS Distributions 			devfs_propogate(dnp->dn_typeinfo.Dir.myname, new_dev, &event_log);
1639*1b191cb5SApple OSS Distributions 		}
1640*1b191cb5SApple OSS Distributions 	}
1641*1b191cb5SApple OSS Distributions 
1642*1b191cb5SApple OSS Distributions out:
1643*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
1644*1b191cb5SApple OSS Distributions 
1645*1b191cb5SApple OSS Distributions 	devfs_bulk_notify(&event_log);
1646*1b191cb5SApple OSS Distributions 	devfs_release_event_log(&event_log, need_free);
1647*1b191cb5SApple OSS Distributions 	return new_dev;
1648*1b191cb5SApple OSS Distributions }
1649*1b191cb5SApple OSS Distributions 
1650*1b191cb5SApple OSS Distributions /*
1651*1b191cb5SApple OSS Distributions  * Function: devfs_make_link
1652*1b191cb5SApple OSS Distributions  *
1653*1b191cb5SApple OSS Distributions  * Purpose:
1654*1b191cb5SApple OSS Distributions  *   Create a link to a previously created device node.
1655*1b191cb5SApple OSS Distributions  *
1656*1b191cb5SApple OSS Distributions  * Returns:
1657*1b191cb5SApple OSS Distributions  *   0 if successful, -1 if failed
1658*1b191cb5SApple OSS Distributions  */
1659*1b191cb5SApple OSS Distributions int
devfs_make_link(void * original,char * fmt,...)1660*1b191cb5SApple OSS Distributions devfs_make_link(void *original, char *fmt, ...)
1661*1b191cb5SApple OSS Distributions {
1662*1b191cb5SApple OSS Distributions 	devdirent_t *   new_dev = NULL;
1663*1b191cb5SApple OSS Distributions 	devdirent_t *   orig = (devdirent_t *) original;
1664*1b191cb5SApple OSS Distributions 	devnode_t *     dirnode;        /* devnode for parent directory */
1665*1b191cb5SApple OSS Distributions 	struct devfs_event_log event_log;
1666*1b191cb5SApple OSS Distributions 	uint32_t        log_count;
1667*1b191cb5SApple OSS Distributions 
1668*1b191cb5SApple OSS Distributions 	va_list ap;
1669*1b191cb5SApple OSS Distributions 	char *p, buf[256]; /* XXX */
1670*1b191cb5SApple OSS Distributions 	size_t i;
1671*1b191cb5SApple OSS Distributions 
1672*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
1673*1b191cb5SApple OSS Distributions 
1674*1b191cb5SApple OSS Distributions 	if (!devfs_ready) {
1675*1b191cb5SApple OSS Distributions 		DEVFS_UNLOCK();
1676*1b191cb5SApple OSS Distributions 		printf("devfs_make_link: not ready for devices!\n");
1677*1b191cb5SApple OSS Distributions 		return -1;
1678*1b191cb5SApple OSS Distributions 	}
1679*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
1680*1b191cb5SApple OSS Distributions 
1681*1b191cb5SApple OSS Distributions 	va_start(ap, fmt);
1682*1b191cb5SApple OSS Distributions 	vsnprintf(buf, sizeof(buf), fmt, ap);
1683*1b191cb5SApple OSS Distributions 	va_end(ap);
1684*1b191cb5SApple OSS Distributions 
1685*1b191cb5SApple OSS Distributions 	p = NULL;
1686*1b191cb5SApple OSS Distributions 
1687*1b191cb5SApple OSS Distributions 	for (i = strlen(buf); i > 0; i--) {
1688*1b191cb5SApple OSS Distributions 		if (buf[i] == '/') {
1689*1b191cb5SApple OSS Distributions 			p = &buf[i];
1690*1b191cb5SApple OSS Distributions 			buf[i] = 0;
1691*1b191cb5SApple OSS Distributions 			break;
1692*1b191cb5SApple OSS Distributions 		}
1693*1b191cb5SApple OSS Distributions 	}
1694*1b191cb5SApple OSS Distributions 
1695*1b191cb5SApple OSS Distributions 	/*
1696*1b191cb5SApple OSS Distributions 	 * One slot for each directory, one for each devnode
1697*1b191cb5SApple OSS Distributions 	 * whose link count changes
1698*1b191cb5SApple OSS Distributions 	 */
1699*1b191cb5SApple OSS Distributions 	log_count = devfs_nmountplanes * 2;
1700*1b191cb5SApple OSS Distributions wrongsize:
1701*1b191cb5SApple OSS Distributions 	if (devfs_init_event_log(&event_log, log_count, NULL) != 0) {
1702*1b191cb5SApple OSS Distributions 		/* No lock held, no allocations done, can just return */
1703*1b191cb5SApple OSS Distributions 		return -1;
1704*1b191cb5SApple OSS Distributions 	}
1705*1b191cb5SApple OSS Distributions 
1706*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
1707*1b191cb5SApple OSS Distributions 
1708*1b191cb5SApple OSS Distributions 	if (log_count < devfs_nmountplanes) {
1709*1b191cb5SApple OSS Distributions 		DEVFS_UNLOCK();
1710*1b191cb5SApple OSS Distributions 		devfs_release_event_log(&event_log, 1);
1711*1b191cb5SApple OSS Distributions 		log_count = log_count * 2;
1712*1b191cb5SApple OSS Distributions 		goto wrongsize;
1713*1b191cb5SApple OSS Distributions 	}
1714*1b191cb5SApple OSS Distributions 
1715*1b191cb5SApple OSS Distributions 	if (p) {
1716*1b191cb5SApple OSS Distributions 		*p++ = '\0';
1717*1b191cb5SApple OSS Distributions 
1718*1b191cb5SApple OSS Distributions 		if (dev_finddir(buf, NULL, DEVFS_CREATE, &dirnode, &event_log)
1719*1b191cb5SApple OSS Distributions 		    || dev_add_name(p, dirnode, NULL, orig->de_dnp, &new_dev)) {
1720*1b191cb5SApple OSS Distributions 			goto fail;
1721*1b191cb5SApple OSS Distributions 		}
1722*1b191cb5SApple OSS Distributions 	} else {
1723*1b191cb5SApple OSS Distributions 		if (dev_finddir("", NULL, DEVFS_CREATE, &dirnode, &event_log)
1724*1b191cb5SApple OSS Distributions 		    || dev_add_name(buf, dirnode, NULL, orig->de_dnp, &new_dev)) {
1725*1b191cb5SApple OSS Distributions 			goto fail;
1726*1b191cb5SApple OSS Distributions 		}
1727*1b191cb5SApple OSS Distributions 	}
1728*1b191cb5SApple OSS Distributions 	devfs_propogate(dirnode->dn_typeinfo.Dir.myname, new_dev, &event_log);
1729*1b191cb5SApple OSS Distributions fail:
1730*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
1731*1b191cb5SApple OSS Distributions 	devfs_bulk_notify(&event_log);
1732*1b191cb5SApple OSS Distributions 	devfs_release_event_log(&event_log, 1);
1733*1b191cb5SApple OSS Distributions 
1734*1b191cb5SApple OSS Distributions 	return (new_dev != NULL) ? 0 : -1;
1735*1b191cb5SApple OSS Distributions }
1736