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