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