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