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