]> git.saurik.com Git - apple/xnu.git/blame - bsd/miscfs/devfs/devfsdefs.h
xnu-7195.101.1.tar.gz
[apple/xnu.git] / bsd / miscfs / devfs / devfsdefs.h
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
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.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright 1997,1998 Julian Elischer. All rights reserved.
30 * julian@freebsd.org
0a7de745 31 *
1c79356b
A
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.
0a7de745 40 *
1c79356b
A
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.
0a7de745 52 *
1c79356b
A
53 * devfsdefs.h
54 */
2d21ac55
A
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 */
1c79356b
A
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
9bccf70c
A
70#ifndef __DEVFS_DEVFSDEFS_H__
71#define __DEVFS_DEVFSDEFS_H__
1c79356b 72
9bccf70c
A
73#include <sys/appleapiopts.h>
74
b0d623f7 75__BEGIN_DECLS
9bccf70c 76#ifdef __APPLE_API_PRIVATE
0a7de745
A
77#define DEVMAXNAMESIZE 32 /* XXX */
78#define DEVMAXPATHSIZE 128 /* XXX */
1c79356b
A
79
80typedef enum {
0a7de745
A
81 DEV_DIR,
82 DEV_BDEV,
83 DEV_CDEV,
84 DEV_SLNK,
b0d623f7 85#if FDESC
0a7de745 86 DEV_DEVFD
b0d623f7 87#endif /* FDESC */
1c79356b
A
88} devfstype_t;
89
0a7de745
A
90extern int(**devfs_vnodeop_p)(void *); /* our own vector array for dirs */
91extern int(**devfs_spec_vnodeop_p)(void *); /* our own vector array for devs */
cb323159 92extern const struct vfsops devfs_vfsops;
1c79356b 93
0a7de745
A
94typedef struct devnode devnode_t;
95typedef struct devdirent devdirent_t;
96typedef union devnode_type devnode_type_t;
1c79356b
A
97
98struct devfs_stats {
0a7de745
A
99 int nodes;
100 int entries;
101 int mounts;
102 int stringspace;
1c79356b
A
103};
104
105union devnode_type {
0a7de745
A
106 dev_t dev;
107 struct {
108 devdirent_t * dirlist;
109 devdirent_t * * dirlast;
110 devnode_t * parent;
111 devdirent_t * myname; /* my entry in .. */
112 int entrycount;
113 }Dir;
114 struct {
115 char * name;/* must be allocated separately */
f427ee49 116 size_t namelen;
0a7de745 117 }Slnk;
1c79356b
A
118};
119
fe8ab488 120#define DEV_MAX_VNODE_RETRY 8 /* Max number of retries when we try to
0a7de745
A
121 * get a vnode for the devnode */
122struct devnode {
123 devfstype_t dn_type;
124 /*
125 * Number of vnodes that point to this devnode. Note, we do not
126 * add another reference for a lookup which finds an existing
127 * vnode; a reference is added when a vnode is created and removed
128 * when a vnode is reclaimed. A devnode will not be freed while
129 * there are outstanding references. A refcount can be added to
130 * prevent the free of a devnode in situations where there is not
131 * guaranteed to be a vnode holding a ref, but it is important to
b0d623f7 132 * make sure that a deferred delete eventually happens if it is
0a7de745
A
133 * blocked behind that reference.
134 */
cb323159 135 os_ref_atomic_t dn_refcount;
0a7de745
A
136 u_short dn_mode;
137 uid_t dn_uid;
138 gid_t dn_gid;
139 struct timespec dn_atime;/* time of last access */
140 struct timespec dn_mtime;/* time of last modification */
141 struct timespec dn_ctime;/* time file changed */
142 int(***dn_ops)(void *);/* yuk... pointer to pointer(s) to funcs */
143 int dn_links;/* how many file links does this node have? */
144 struct devfsmount * dn_dvm; /* the mount structure for this 'plane' */
145 struct vnode * dn_vn;/* address of last vnode that represented us */
146 int dn_len;/* of any associated info (e.g. dir data) */
147 devdirent_t * dn_linklist;/* circular list of hardlinks to this node */
148 devnode_t * dn_nextsibling;/* the list of equivalent nodes */
149 devnode_t * * dn_prevsiblingp;/* backpointer for the above */
150 devnode_type_t dn_typeinfo;
151 int dn_change;
152 int dn_update;
153 int dn_access;
154 int dn_lflags;
155 ino_t dn_ino;
156 int (*dn_clone)(dev_t dev, int action);/* get minor # */
157 struct label * dn_label; /* security label */
1c79356b
A
158};
159
0a7de745
A
160#define DN_DELETE 0x02
161#define DN_CREATE 0x04
162#define DN_CREATEWAIT 0x08
91447636
A
163
164
0a7de745
A
165struct devdirent {
166 /*-----------------------directory entry fields-------------*/
167 char de_name[DEVMAXNAMESIZE];
168 devnode_t * de_dnp; /* the "inode" (devnode) pointer */
169 devnode_t * de_parent; /* backpointer to the directory itself */
170 devdirent_t * de_next; /* next object in this directory */
171 devdirent_t * *de_prevp; /* previous pointer in directory linked list */
172 devdirent_t * de_nextlink;/* next hardlink to this node */
173 devdirent_t * *de_prevlinkp;/* previous hardlink pointer for this node */
1c79356b
A
174};
175
0a7de745
A
176extern devdirent_t * dev_root;
177extern struct devfs_stats devfs_stats;
178extern lck_mtx_t devfs_mutex;
179extern lck_mtx_t devfs_attr_mutex;
1c79356b
A
180
181/*
182 * Rules for front nodes:
183 * Dirs hava a strict 1:1 relationship with their OWN devnode
184 * Symlinks similarly
185 * Device Nodes ALWAYS point to the devnode that is linked
186 * to the Backing node. (with a ref count)
187 */
188
189/*
190 * DEVFS specific per/mount information, used to link a monted fs to a
191 * particular 'plane' of front nodes.
192 */
0a7de745
A
193struct devfsmount {
194 struct mount * mount;/* vfs mount struct for this fs */
195 devdirent_t * plane_root;/* the root of this 'plane' */
1c79356b
A
196};
197
198/*
199 * Prototypes for DEVFS virtual filesystem operations
200 */
201#include <sys/lock.h>
202#include <miscfs/devfs/devfs_proto.h>
0a7de745 203#include <libkern/OSAtomic.h> /* required for OSAddAtomic() */
1c79356b
A
204
205//#define HIDDEN_MOUNTPOINT 1
206
207/* misc */
0a7de745
A
208#define M_DEVFSNAME M_DEVFS
209#define M_DEVFSNODE M_DEVFS
210#define M_DEVFSMNT M_DEVFS
1c79356b 211
0a7de745 212#define VTODN(vp) ((devnode_t *)(vp)->v_data)
1c79356b 213
0a7de745
A
214#define DEVFS_LOCK() lck_mtx_lock(&devfs_mutex)
215#define DEVFS_UNLOCK() lck_mtx_unlock(&devfs_mutex)
91447636 216
0a7de745
A
217#define DEVFS_ATTR_LOCK_SPIN() lck_mtx_lock_spin(&devfs_attr_mutex);
218#define DEVFS_ATTR_UNLOCK() lck_mtx_unlock(&devfs_attr_mutex);
1c79356b 219
2d21ac55
A
220/*
221 * XXX all the (SInt32 *) casts below assume sizeof(int) == sizeof(long)
222 */
1c79356b 223static __inline__ void
2d21ac55 224DEVFS_INCR_ENTRIES(void)
1c79356b 225{
0a7de745 226 OSAddAtomic(1, &devfs_stats.entries);
1c79356b
A
227}
228
229static __inline__ void
2d21ac55 230DEVFS_DECR_ENTRIES(void)
1c79356b 231{
0a7de745 232 OSAddAtomic(-1, &devfs_stats.entries);
1c79356b
A
233}
234
235static __inline__ void
2d21ac55 236DEVFS_INCR_NODES(void)
1c79356b 237{
0a7de745 238 OSAddAtomic(1, &devfs_stats.nodes);
1c79356b
A
239}
240
241static __inline__ void
2d21ac55 242DEVFS_DECR_NODES(void)
1c79356b 243{
0a7de745 244 OSAddAtomic(-1, &devfs_stats.nodes);
1c79356b
A
245}
246
247static __inline__ void
2d21ac55 248DEVFS_INCR_MOUNTS(void)
1c79356b 249{
0a7de745 250 OSAddAtomic(1, &devfs_stats.mounts);
1c79356b
A
251}
252
253static __inline__ void
2d21ac55 254DEVFS_DECR_MOUNTS(void)
1c79356b 255{
0a7de745 256 OSAddAtomic(-1, &devfs_stats.mounts);
1c79356b
A
257}
258
259static __inline__ void
260DEVFS_INCR_STRINGSPACE(int space)
261{
0a7de745 262 OSAddAtomic(space, &devfs_stats.stringspace);
1c79356b
A
263}
264
265static __inline__ void
266DEVFS_DECR_STRINGSPACE(int space)
267{
0a7de745 268 OSAddAtomic(-space, &devfs_stats.stringspace);
1c79356b
A
269}
270
0a7de745 271/*
6d2010ae
A
272 * Access, change, and modify times are protected by a separate lock,
273 * which allows tty times to be updated (no more than once per second)
274 * in the I/O path without too much fear of contention.
275 *
0a7de745
A
276 * For getattr, update times to current time if the last update was recent;
277 * preserve legacy behavior that frequent stats can yield sub-second resolutions.
6d2010ae
A
278 * If the last time is old, however, we know that the event that triggered
279 * the need for an update was no more than 1s after the last update. In that case,
280 * use (last update + 1s) as the time, avoiding the illusion that last update happened
281 * much later than it really did.
282 */
0a7de745 283#define DEVFS_LAZY_UPDATE_SECONDS 1
6d2010ae 284
0a7de745
A
285#define DEVFS_UPDATE_CHANGE 0x1
286#define DEVFS_UPDATE_MOD 0x2
287#define DEVFS_UPDATE_ACCESS 0x4
1c79356b
A
288
289static __inline__ void
290dn_copy_times(devnode_t * target, devnode_t * source)
291{
0a7de745
A
292 DEVFS_ATTR_LOCK_SPIN();
293 target->dn_atime = source->dn_atime;
294 target->dn_mtime = source->dn_mtime;
295 target->dn_ctime = source->dn_ctime;
296 DEVFS_ATTR_UNLOCK();
297 return;
1c79356b 298}
b0d623f7
A
299
300#ifdef BSD_KERNEL_PRIVATE
f427ee49 301int devfs_make_symlink(devnode_t *dir_p, char *name, mode_t mode, char *target, devdirent_t **newent);
b0d623f7
A
302#endif /* BSD_KERNEL_PRIVATE */
303
9bccf70c 304#endif /* __APPLE_API_PRIVATE */
b0d623f7
A
305
306__END_DECLS
307
9bccf70c 308#endif /* __DEVFS_DEVFSDEFS_H__ */