1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3*c54f35caSApple OSS Distributions *
4*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions *
6*c54f35caSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions *
15*c54f35caSApple OSS Distributions * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions *
18*c54f35caSApple OSS Distributions * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions * limitations under the License.
25*c54f35caSApple OSS Distributions *
26*c54f35caSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions */
28*c54f35caSApple OSS Distributions /*
29*c54f35caSApple OSS Distributions * Copyright 1997,1998 Julian Elischer. All rights reserved.
30*c54f35caSApple OSS Distributions * [email protected]
31*c54f35caSApple OSS Distributions *
32*c54f35caSApple OSS Distributions * Redistribution and use in source and binary forms, with or without
33*c54f35caSApple OSS Distributions * modification, are permitted provided that the following conditions are
34*c54f35caSApple OSS Distributions * met:
35*c54f35caSApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
36*c54f35caSApple OSS Distributions * notice, this list of conditions and the following disclaimer.
37*c54f35caSApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright notice,
38*c54f35caSApple OSS Distributions * this list of conditions and the following disclaimer in the documentation
39*c54f35caSApple OSS Distributions * and/or other materials provided with the distribution.
40*c54f35caSApple OSS Distributions *
41*c54f35caSApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
42*c54f35caSApple OSS Distributions * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
43*c54f35caSApple OSS Distributions * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44*c54f35caSApple OSS Distributions * DISCLAIMED. IN NO EVENT SHALL THE HOLDER OR CONTRIBUTORS BE LIABLE FOR
45*c54f35caSApple OSS Distributions * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*c54f35caSApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47*c54f35caSApple OSS Distributions * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
48*c54f35caSApple OSS Distributions * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*c54f35caSApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*c54f35caSApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*c54f35caSApple OSS Distributions * SUCH DAMAGE.
52*c54f35caSApple OSS Distributions *
53*c54f35caSApple OSS Distributions * devfsdefs.h
54*c54f35caSApple OSS Distributions */
55*c54f35caSApple OSS Distributions /*
56*c54f35caSApple OSS Distributions * NOTICE: This file was modified by McAfee Research in 2004 to introduce
57*c54f35caSApple OSS Distributions * support for mandatory and extensible security protections. This notice
58*c54f35caSApple OSS Distributions * is included in support of clause 2.2 (b) of the Apple Public License,
59*c54f35caSApple OSS Distributions * Version 2.0.
60*c54f35caSApple OSS Distributions */
61*c54f35caSApple OSS Distributions
62*c54f35caSApple OSS Distributions /*
63*c54f35caSApple OSS Distributions * HISTORY
64*c54f35caSApple OSS Distributions * 8-April-1999 Dieter Siegmund ([email protected])
65*c54f35caSApple OSS Distributions * Ported to from FreeBSD 3.1
66*c54f35caSApple OSS Distributions * Removed unnecessary/unused defines
67*c54f35caSApple OSS Distributions * Renamed structures/elements to clarify usage in code.
68*c54f35caSApple OSS Distributions */
69*c54f35caSApple OSS Distributions
70*c54f35caSApple OSS Distributions #ifndef __DEVFS_DEVFSDEFS_H__
71*c54f35caSApple OSS Distributions #define __DEVFS_DEVFSDEFS_H__
72*c54f35caSApple OSS Distributions
73*c54f35caSApple OSS Distributions #include <sys/appleapiopts.h>
74*c54f35caSApple OSS Distributions
75*c54f35caSApple OSS Distributions __BEGIN_DECLS
76*c54f35caSApple OSS Distributions #ifdef __APPLE_API_PRIVATE
77*c54f35caSApple OSS Distributions #define DEVMAXNAMESIZE 32 /* XXX */
78*c54f35caSApple OSS Distributions #define DEVMAXPATHSIZE 128 /* XXX */
79*c54f35caSApple OSS Distributions
80*c54f35caSApple OSS Distributions typedef enum {
81*c54f35caSApple OSS Distributions DEV_DIR,
82*c54f35caSApple OSS Distributions DEV_BDEV,
83*c54f35caSApple OSS Distributions DEV_CDEV,
84*c54f35caSApple OSS Distributions DEV_SLNK,
85*c54f35caSApple OSS Distributions #if FDESC
86*c54f35caSApple OSS Distributions DEV_DEVFD
87*c54f35caSApple OSS Distributions #endif /* FDESC */
88*c54f35caSApple OSS Distributions } devfstype_t;
89*c54f35caSApple OSS Distributions
90*c54f35caSApple OSS Distributions extern int(**devfs_vnodeop_p)(void *); /* our own vector array for dirs */
91*c54f35caSApple OSS Distributions extern int(**devfs_spec_vnodeop_p)(void *); /* our own vector array for devs */
92*c54f35caSApple OSS Distributions extern const struct vfsops devfs_vfsops;
93*c54f35caSApple OSS Distributions
94*c54f35caSApple OSS Distributions typedef struct devnode devnode_t;
95*c54f35caSApple OSS Distributions typedef struct devdirent devdirent_t;
96*c54f35caSApple OSS Distributions typedef union devnode_type devnode_type_t;
97*c54f35caSApple OSS Distributions
98*c54f35caSApple OSS Distributions struct devfs_stats {
99*c54f35caSApple OSS Distributions int nodes;
100*c54f35caSApple OSS Distributions int entries;
101*c54f35caSApple OSS Distributions int mounts;
102*c54f35caSApple OSS Distributions int stringspace;
103*c54f35caSApple OSS Distributions };
104*c54f35caSApple OSS Distributions
105*c54f35caSApple OSS Distributions union devnode_type {
106*c54f35caSApple OSS Distributions dev_t dev;
107*c54f35caSApple OSS Distributions struct {
108*c54f35caSApple OSS Distributions devdirent_t * dirlist;
109*c54f35caSApple OSS Distributions devdirent_t * * dirlast;
110*c54f35caSApple OSS Distributions devnode_t * parent;
111*c54f35caSApple OSS Distributions devdirent_t * myname; /* my entry in .. */
112*c54f35caSApple OSS Distributions int entrycount;
113*c54f35caSApple OSS Distributions }Dir;
114*c54f35caSApple OSS Distributions struct {
115*c54f35caSApple OSS Distributions char * name;/* must be allocated separately */
116*c54f35caSApple OSS Distributions size_t namelen;
117*c54f35caSApple OSS Distributions }Slnk;
118*c54f35caSApple OSS Distributions };
119*c54f35caSApple OSS Distributions
120*c54f35caSApple OSS Distributions #define DEV_MAX_VNODE_RETRY 8 /* Max number of retries when we try to
121*c54f35caSApple OSS Distributions * get a vnode for the devnode */
122*c54f35caSApple OSS Distributions struct devnode {
123*c54f35caSApple OSS Distributions devfstype_t dn_type;
124*c54f35caSApple OSS Distributions /*
125*c54f35caSApple OSS Distributions * Number of vnodes that point to this devnode. Note, we do not
126*c54f35caSApple OSS Distributions * add another reference for a lookup which finds an existing
127*c54f35caSApple OSS Distributions * vnode; a reference is added when a vnode is created and removed
128*c54f35caSApple OSS Distributions * when a vnode is reclaimed. A devnode will not be freed while
129*c54f35caSApple OSS Distributions * there are outstanding references. A refcount can be added to
130*c54f35caSApple OSS Distributions * prevent the free of a devnode in situations where there is not
131*c54f35caSApple OSS Distributions * guaranteed to be a vnode holding a ref, but it is important to
132*c54f35caSApple OSS Distributions * make sure that a deferred delete eventually happens if it is
133*c54f35caSApple OSS Distributions * blocked behind that reference.
134*c54f35caSApple OSS Distributions */
135*c54f35caSApple OSS Distributions os_ref_atomic_t dn_refcount;
136*c54f35caSApple OSS Distributions u_short dn_mode;
137*c54f35caSApple OSS Distributions uid_t dn_uid;
138*c54f35caSApple OSS Distributions gid_t dn_gid;
139*c54f35caSApple OSS Distributions struct timespec dn_atime;/* time of last access */
140*c54f35caSApple OSS Distributions struct timespec dn_mtime;/* time of last modification */
141*c54f35caSApple OSS Distributions struct timespec dn_ctime;/* time file changed */
142*c54f35caSApple OSS Distributions int(***dn_ops)(void *);/* yuk... pointer to pointer(s) to funcs */
143*c54f35caSApple OSS Distributions int dn_links;/* how many file links does this node have? */
144*c54f35caSApple OSS Distributions struct devfsmount * dn_dvm; /* the mount structure for this 'plane' */
145*c54f35caSApple OSS Distributions struct vnode * dn_vn;/* address of last vnode that represented us */
146*c54f35caSApple OSS Distributions int dn_len;/* of any associated info (e.g. dir data) */
147*c54f35caSApple OSS Distributions devdirent_t * dn_linklist;/* circular list of hardlinks to this node */
148*c54f35caSApple OSS Distributions devnode_t * dn_nextsibling;/* the list of equivalent nodes */
149*c54f35caSApple OSS Distributions devnode_t * * dn_prevsiblingp;/* backpointer for the above */
150*c54f35caSApple OSS Distributions devnode_type_t dn_typeinfo;
151*c54f35caSApple OSS Distributions int dn_change;
152*c54f35caSApple OSS Distributions int dn_update;
153*c54f35caSApple OSS Distributions int dn_access;
154*c54f35caSApple OSS Distributions int dn_lflags;
155*c54f35caSApple OSS Distributions ino_t dn_ino;
156*c54f35caSApple OSS Distributions int (*dn_clone)(dev_t dev, int action);/* get minor # */
157*c54f35caSApple OSS Distributions struct label * dn_label; /* security label */
158*c54f35caSApple OSS Distributions };
159*c54f35caSApple OSS Distributions
160*c54f35caSApple OSS Distributions #define DN_DELETE 0x02
161*c54f35caSApple OSS Distributions #define DN_CREATE 0x04
162*c54f35caSApple OSS Distributions #define DN_CREATEWAIT 0x08
163*c54f35caSApple OSS Distributions
164*c54f35caSApple OSS Distributions
165*c54f35caSApple OSS Distributions struct devdirent {
166*c54f35caSApple OSS Distributions /*-----------------------directory entry fields-------------*/
167*c54f35caSApple OSS Distributions char de_name[DEVMAXNAMESIZE];
168*c54f35caSApple OSS Distributions devnode_t * de_dnp; /* the "inode" (devnode) pointer */
169*c54f35caSApple OSS Distributions devnode_t * de_parent; /* backpointer to the directory itself */
170*c54f35caSApple OSS Distributions devdirent_t * de_next; /* next object in this directory */
171*c54f35caSApple OSS Distributions devdirent_t * *de_prevp; /* previous pointer in directory linked list */
172*c54f35caSApple OSS Distributions devdirent_t * de_nextlink;/* next hardlink to this node */
173*c54f35caSApple OSS Distributions devdirent_t * *de_prevlinkp;/* previous hardlink pointer for this node */
174*c54f35caSApple OSS Distributions };
175*c54f35caSApple OSS Distributions
176*c54f35caSApple OSS Distributions extern devdirent_t * dev_root;
177*c54f35caSApple OSS Distributions extern struct devfs_stats devfs_stats;
178*c54f35caSApple OSS Distributions extern lck_mtx_t devfs_mutex;
179*c54f35caSApple OSS Distributions extern lck_mtx_t devfs_attr_mutex;
180*c54f35caSApple OSS Distributions
181*c54f35caSApple OSS Distributions /*
182*c54f35caSApple OSS Distributions * Rules for front nodes:
183*c54f35caSApple OSS Distributions * Dirs hava a strict 1:1 relationship with their OWN devnode
184*c54f35caSApple OSS Distributions * Symlinks similarly
185*c54f35caSApple OSS Distributions * Device Nodes ALWAYS point to the devnode that is linked
186*c54f35caSApple OSS Distributions * to the Backing node. (with a ref count)
187*c54f35caSApple OSS Distributions */
188*c54f35caSApple OSS Distributions
189*c54f35caSApple OSS Distributions /*
190*c54f35caSApple OSS Distributions * DEVFS specific per/mount information, used to link a monted fs to a
191*c54f35caSApple OSS Distributions * particular 'plane' of front nodes.
192*c54f35caSApple OSS Distributions */
193*c54f35caSApple OSS Distributions struct devfsmount {
194*c54f35caSApple OSS Distributions struct mount * mount;/* vfs mount struct for this fs */
195*c54f35caSApple OSS Distributions devdirent_t * plane_root;/* the root of this 'plane' */
196*c54f35caSApple OSS Distributions };
197*c54f35caSApple OSS Distributions
198*c54f35caSApple OSS Distributions /*
199*c54f35caSApple OSS Distributions * Prototypes for DEVFS virtual filesystem operations
200*c54f35caSApple OSS Distributions */
201*c54f35caSApple OSS Distributions #include <sys/lock.h>
202*c54f35caSApple OSS Distributions #include <miscfs/devfs/devfs_proto.h>
203*c54f35caSApple OSS Distributions #include <libkern/OSAtomic.h> /* required for OSAddAtomic() */
204*c54f35caSApple OSS Distributions
205*c54f35caSApple OSS Distributions //#define HIDDEN_MOUNTPOINT 1
206*c54f35caSApple OSS Distributions
207*c54f35caSApple OSS Distributions /* misc */
208*c54f35caSApple OSS Distributions
209*c54f35caSApple OSS Distributions #define VTODN(vp) ((devnode_t *)(vp)->v_data)
210*c54f35caSApple OSS Distributions
211*c54f35caSApple OSS Distributions #define DEVFS_LOCK() lck_mtx_lock(&devfs_mutex)
212*c54f35caSApple OSS Distributions #define DEVFS_UNLOCK() lck_mtx_unlock(&devfs_mutex)
213*c54f35caSApple OSS Distributions
214*c54f35caSApple OSS Distributions #define DEVFS_ATTR_LOCK_SPIN() lck_mtx_lock_spin(&devfs_attr_mutex);
215*c54f35caSApple OSS Distributions #define DEVFS_ATTR_UNLOCK() lck_mtx_unlock(&devfs_attr_mutex);
216*c54f35caSApple OSS Distributions
217*c54f35caSApple OSS Distributions /*
218*c54f35caSApple OSS Distributions * XXX all the (SInt32 *) casts below assume sizeof(int) == sizeof(long)
219*c54f35caSApple OSS Distributions */
220*c54f35caSApple OSS Distributions static __inline__ void
DEVFS_INCR_ENTRIES(void)221*c54f35caSApple OSS Distributions DEVFS_INCR_ENTRIES(void)
222*c54f35caSApple OSS Distributions {
223*c54f35caSApple OSS Distributions OSAddAtomic(1, &devfs_stats.entries);
224*c54f35caSApple OSS Distributions }
225*c54f35caSApple OSS Distributions
226*c54f35caSApple OSS Distributions static __inline__ void
DEVFS_DECR_ENTRIES(void)227*c54f35caSApple OSS Distributions DEVFS_DECR_ENTRIES(void)
228*c54f35caSApple OSS Distributions {
229*c54f35caSApple OSS Distributions OSAddAtomic(-1, &devfs_stats.entries);
230*c54f35caSApple OSS Distributions }
231*c54f35caSApple OSS Distributions
232*c54f35caSApple OSS Distributions static __inline__ void
DEVFS_INCR_NODES(void)233*c54f35caSApple OSS Distributions DEVFS_INCR_NODES(void)
234*c54f35caSApple OSS Distributions {
235*c54f35caSApple OSS Distributions OSAddAtomic(1, &devfs_stats.nodes);
236*c54f35caSApple OSS Distributions }
237*c54f35caSApple OSS Distributions
238*c54f35caSApple OSS Distributions static __inline__ void
DEVFS_DECR_NODES(void)239*c54f35caSApple OSS Distributions DEVFS_DECR_NODES(void)
240*c54f35caSApple OSS Distributions {
241*c54f35caSApple OSS Distributions OSAddAtomic(-1, &devfs_stats.nodes);
242*c54f35caSApple OSS Distributions }
243*c54f35caSApple OSS Distributions
244*c54f35caSApple OSS Distributions static __inline__ void
DEVFS_INCR_MOUNTS(void)245*c54f35caSApple OSS Distributions DEVFS_INCR_MOUNTS(void)
246*c54f35caSApple OSS Distributions {
247*c54f35caSApple OSS Distributions OSAddAtomic(1, &devfs_stats.mounts);
248*c54f35caSApple OSS Distributions }
249*c54f35caSApple OSS Distributions
250*c54f35caSApple OSS Distributions static __inline__ void
DEVFS_DECR_MOUNTS(void)251*c54f35caSApple OSS Distributions DEVFS_DECR_MOUNTS(void)
252*c54f35caSApple OSS Distributions {
253*c54f35caSApple OSS Distributions OSAddAtomic(-1, &devfs_stats.mounts);
254*c54f35caSApple OSS Distributions }
255*c54f35caSApple OSS Distributions
256*c54f35caSApple OSS Distributions static __inline__ void
DEVFS_INCR_STRINGSPACE(int space)257*c54f35caSApple OSS Distributions DEVFS_INCR_STRINGSPACE(int space)
258*c54f35caSApple OSS Distributions {
259*c54f35caSApple OSS Distributions OSAddAtomic(space, &devfs_stats.stringspace);
260*c54f35caSApple OSS Distributions }
261*c54f35caSApple OSS Distributions
262*c54f35caSApple OSS Distributions static __inline__ void
DEVFS_DECR_STRINGSPACE(int space)263*c54f35caSApple OSS Distributions DEVFS_DECR_STRINGSPACE(int space)
264*c54f35caSApple OSS Distributions {
265*c54f35caSApple OSS Distributions OSAddAtomic(-space, &devfs_stats.stringspace);
266*c54f35caSApple OSS Distributions }
267*c54f35caSApple OSS Distributions
268*c54f35caSApple OSS Distributions /*
269*c54f35caSApple OSS Distributions * Access, change, and modify times are protected by a separate lock,
270*c54f35caSApple OSS Distributions * which allows tty times to be updated (no more than once per second)
271*c54f35caSApple OSS Distributions * in the I/O path without too much fear of contention.
272*c54f35caSApple OSS Distributions *
273*c54f35caSApple OSS Distributions * For getattr, update times to current time if the last update was recent;
274*c54f35caSApple OSS Distributions * preserve legacy behavior that frequent stats can yield sub-second resolutions.
275*c54f35caSApple OSS Distributions * If the last time is old, however, we know that the event that triggered
276*c54f35caSApple OSS Distributions * the need for an update was no more than 1s after the last update. In that case,
277*c54f35caSApple OSS Distributions * use (last update + 1s) as the time, avoiding the illusion that last update happened
278*c54f35caSApple OSS Distributions * much later than it really did.
279*c54f35caSApple OSS Distributions */
280*c54f35caSApple OSS Distributions #define DEVFS_LAZY_UPDATE_SECONDS 1
281*c54f35caSApple OSS Distributions
282*c54f35caSApple OSS Distributions #define DEVFS_UPDATE_CHANGE 0x1
283*c54f35caSApple OSS Distributions #define DEVFS_UPDATE_MOD 0x2
284*c54f35caSApple OSS Distributions #define DEVFS_UPDATE_ACCESS 0x4
285*c54f35caSApple OSS Distributions
286*c54f35caSApple OSS Distributions static __inline__ void
dn_copy_times(devnode_t * target,devnode_t * source)287*c54f35caSApple OSS Distributions dn_copy_times(devnode_t * target, devnode_t * source)
288*c54f35caSApple OSS Distributions {
289*c54f35caSApple OSS Distributions DEVFS_ATTR_LOCK_SPIN();
290*c54f35caSApple OSS Distributions target->dn_atime = source->dn_atime;
291*c54f35caSApple OSS Distributions target->dn_mtime = source->dn_mtime;
292*c54f35caSApple OSS Distributions target->dn_ctime = source->dn_ctime;
293*c54f35caSApple OSS Distributions DEVFS_ATTR_UNLOCK();
294*c54f35caSApple OSS Distributions return;
295*c54f35caSApple OSS Distributions }
296*c54f35caSApple OSS Distributions
297*c54f35caSApple OSS Distributions #ifdef BSD_KERNEL_PRIVATE
298*c54f35caSApple OSS Distributions int devfs_make_symlink(devnode_t *dir_p, char *name, mode_t mode, char *target, devdirent_t **newent);
299*c54f35caSApple OSS Distributions #endif /* BSD_KERNEL_PRIVATE */
300*c54f35caSApple OSS Distributions
301*c54f35caSApple OSS Distributions #endif /* __APPLE_API_PRIVATE */
302*c54f35caSApple OSS Distributions
303*c54f35caSApple OSS Distributions __END_DECLS
304*c54f35caSApple OSS Distributions
305*c54f35caSApple OSS Distributions #endif /* __DEVFS_DEVFSDEFS_H__ */
306