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