williamr@2
|
1 |
/*
|
williamr@2
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
|
williamr@2
|
3 |
|
williamr@2
|
4 |
* Redistribution and use in source and binary forms, with or without
|
williamr@2
|
5 |
* modification, are permitted provided that the following conditions are met:
|
williamr@2
|
6 |
|
williamr@2
|
7 |
* Redistributions of source code must retain the above copyright notice, this
|
williamr@2
|
8 |
* list of conditions and the following disclaimer.
|
williamr@2
|
9 |
* Redistributions in binary form must reproduce the above copyright notice,
|
williamr@2
|
10 |
* this list of conditions and the following disclaimer in the documentation
|
williamr@2
|
11 |
* and/or other materials provided with the distribution.
|
williamr@2
|
12 |
* Neither the name of Nokia Corporation nor the names of its contributors
|
williamr@2
|
13 |
* may be used to endorse or promote products derived from this software
|
williamr@2
|
14 |
* without specific prior written permission.
|
williamr@2
|
15 |
|
williamr@2
|
16 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
williamr@2
|
17 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
williamr@2
|
18 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
williamr@2
|
19 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
williamr@2
|
20 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
williamr@2
|
21 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
williamr@2
|
22 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
williamr@2
|
23 |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
williamr@2
|
24 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
williamr@2
|
25 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
williamr@2
|
26 |
*
|
williamr@2
|
27 |
* Description:
|
williamr@2
|
28 |
*
|
williamr@2
|
29 |
*/
|
williamr@2
|
30 |
|
williamr@2
|
31 |
|
williamr@2
|
32 |
#ifndef _SYS_FILE_H_
|
williamr@2
|
33 |
#define _SYS_FILE_H_
|
williamr@2
|
34 |
|
williamr@2
|
35 |
#ifndef _KERNEL
|
williamr@2
|
36 |
#include <sys/types.h> /* XXX */
|
williamr@2
|
37 |
#include <sys/fcntl.h>
|
williamr@2
|
38 |
#include <sys/unistd.h>
|
williamr@2
|
39 |
#else
|
williamr@2
|
40 |
#include <sys/queue.h>
|
williamr@2
|
41 |
#include <sys/_lock.h>
|
williamr@2
|
42 |
#include <sys/_mutex.h>
|
williamr@2
|
43 |
|
williamr@2
|
44 |
struct stat;
|
williamr@2
|
45 |
struct thread;
|
williamr@2
|
46 |
struct uio;
|
williamr@2
|
47 |
struct knote;
|
williamr@2
|
48 |
struct vnode;
|
williamr@2
|
49 |
struct socket;
|
williamr@2
|
50 |
|
williamr@2
|
51 |
|
williamr@2
|
52 |
#endif /* _KERNEL */
|
williamr@2
|
53 |
|
williamr@2
|
54 |
#define DTYPE_VNODE 1 /* file */
|
williamr@2
|
55 |
#define DTYPE_SOCKET 2 /* communications endpoint */
|
williamr@2
|
56 |
#define DTYPE_PIPE 3 /* pipe */
|
williamr@2
|
57 |
#define DTYPE_FIFO 4 /* fifo (named pipe) */
|
williamr@2
|
58 |
#define DTYPE_KQUEUE 5 /* event queue */
|
williamr@2
|
59 |
#define DTYPE_CRYPTO 6 /* crypto */
|
williamr@2
|
60 |
|
williamr@2
|
61 |
#ifdef _KERNEL
|
williamr@2
|
62 |
|
williamr@2
|
63 |
struct file;
|
williamr@2
|
64 |
struct ucred;
|
williamr@2
|
65 |
|
williamr@2
|
66 |
typedef int fo_rdwr_t(struct file *fp, struct uio *uio,
|
williamr@2
|
67 |
struct ucred *active_cred, int flags,
|
williamr@2
|
68 |
struct thread *td);
|
williamr@2
|
69 |
#define FOF_OFFSET 1 /* Use the offset in uio argument */
|
williamr@2
|
70 |
typedef int fo_ioctl_t(struct file *fp, u_long com, void *data,
|
williamr@2
|
71 |
struct ucred *active_cred, struct thread *td);
|
williamr@2
|
72 |
typedef int fo_poll_t(struct file *fp, int events,
|
williamr@2
|
73 |
struct ucred *active_cred, struct thread *td);
|
williamr@2
|
74 |
typedef int fo_kqfilter_t(struct file *fp, struct knote *kn);
|
williamr@2
|
75 |
typedef int fo_stat_t(struct file *fp, struct stat *sb,
|
williamr@2
|
76 |
struct ucred *active_cred, struct thread *td);
|
williamr@2
|
77 |
typedef int fo_close_t(struct file *fp, struct thread *td);
|
williamr@2
|
78 |
typedef int fo_flags_t;
|
williamr@2
|
79 |
|
williamr@2
|
80 |
struct fileops {
|
williamr@2
|
81 |
fo_rdwr_t *fo_read;
|
williamr@2
|
82 |
fo_rdwr_t *fo_write;
|
williamr@2
|
83 |
fo_ioctl_t *fo_ioctl;
|
williamr@2
|
84 |
fo_poll_t *fo_poll;
|
williamr@2
|
85 |
fo_kqfilter_t *fo_kqfilter;
|
williamr@2
|
86 |
fo_stat_t *fo_stat;
|
williamr@2
|
87 |
fo_close_t *fo_close;
|
williamr@2
|
88 |
fo_flags_t fo_flags; /* DFLAG_* below */
|
williamr@2
|
89 |
};
|
williamr@2
|
90 |
|
williamr@2
|
91 |
#define DFLAG_PASSABLE 0x01 /* may be passed via unix sockets. */
|
williamr@2
|
92 |
#define DFLAG_SEEKABLE 0x02 /* seekable / nonsequential */
|
williamr@2
|
93 |
|
williamr@2
|
94 |
/*
|
williamr@2
|
95 |
* Kernel descriptor table.
|
williamr@2
|
96 |
* One entry for each open kernel vnode and socket.
|
williamr@2
|
97 |
*
|
williamr@2
|
98 |
* Below is the list of locks that protects members in struct file.
|
williamr@2
|
99 |
*
|
williamr@2
|
100 |
* (fl) filelist_lock
|
williamr@2
|
101 |
* (f) f_mtx in struct file
|
williamr@2
|
102 |
* none not locked
|
williamr@2
|
103 |
*/
|
williamr@2
|
104 |
|
williamr@2
|
105 |
struct file {
|
williamr@2
|
106 |
LIST_ENTRY(file) f_list;/* (fl) list of active files */
|
williamr@2
|
107 |
short f_type; /* descriptor type */
|
williamr@2
|
108 |
void *f_data; /* file descriptor specific data */
|
williamr@2
|
109 |
u_int f_flag; /* see fcntl.h */
|
williamr@2
|
110 |
struct mtx *f_mtxp; /* mutex to protect data */
|
williamr@2
|
111 |
struct fileops *f_ops; /* File operations */
|
williamr@2
|
112 |
struct ucred *f_cred; /* credentials associated with descriptor */
|
williamr@2
|
113 |
int f_count; /* (f) reference count */
|
williamr@2
|
114 |
struct vnode *f_vnode; /* NULL or applicable vnode */
|
williamr@2
|
115 |
|
williamr@2
|
116 |
/* DFLAG_SEEKABLE specific fields */
|
williamr@2
|
117 |
off_t f_offset;
|
williamr@2
|
118 |
|
williamr@2
|
119 |
/* DTYPE_SOCKET specific fields */
|
williamr@2
|
120 |
short f_gcflag; /* used by thread doing fd garbage collection */
|
williamr@2
|
121 |
#define FMARK 0x1 /* mark during gc() */
|
williamr@2
|
122 |
#define FDEFER 0x2 /* defer for next gc pass */
|
williamr@2
|
123 |
int f_msgcount; /* (f) references from message queue */
|
williamr@2
|
124 |
|
williamr@2
|
125 |
/* DTYPE_VNODE specific fields */
|
williamr@2
|
126 |
int f_seqcount; /*
|
williamr@2
|
127 |
* count of sequential accesses -- cleared
|
williamr@2
|
128 |
* by most seek operations.
|
williamr@2
|
129 |
*/
|
williamr@2
|
130 |
off_t f_nextoff; /*
|
williamr@2
|
131 |
* offset of next expected read or write
|
williamr@2
|
132 |
*/
|
williamr@2
|
133 |
void *f_label; /* Place-holder for struct label pointer. */
|
williamr@2
|
134 |
};
|
williamr@2
|
135 |
|
williamr@2
|
136 |
#endif /* _KERNEL */
|
williamr@2
|
137 |
|
williamr@2
|
138 |
/*
|
williamr@2
|
139 |
* Userland version of struct file, for sysctl
|
williamr@2
|
140 |
*/
|
williamr@2
|
141 |
struct xfile {
|
williamr@2
|
142 |
size_t xf_size; /* size of struct xfile */
|
williamr@2
|
143 |
pid_t xf_pid; /* owning process */
|
williamr@2
|
144 |
uid_t xf_uid; /* effective uid of owning process */
|
williamr@2
|
145 |
int xf_fd; /* descriptor number */
|
williamr@2
|
146 |
void *xf_file; /* address of struct file */
|
williamr@2
|
147 |
short xf_type; /* descriptor type */
|
williamr@2
|
148 |
int xf_count; /* reference count */
|
williamr@2
|
149 |
int xf_msgcount; /* references from message queue */
|
williamr@2
|
150 |
off_t xf_offset; /* file offset */
|
williamr@2
|
151 |
void *xf_data; /* file descriptor specific data */
|
williamr@2
|
152 |
void *xf_vnode; /* vnode pointer */
|
williamr@2
|
153 |
u_int xf_flag; /* flags (see fcntl.h) */
|
williamr@2
|
154 |
};
|
williamr@2
|
155 |
|
williamr@2
|
156 |
#ifdef _KERNEL
|
williamr@2
|
157 |
extern struct filelist filehead; /* (fl) head of list of open files */
|
williamr@2
|
158 |
extern struct fileops vnops;
|
williamr@2
|
159 |
extern struct fileops badfileops;
|
williamr@2
|
160 |
extern struct fileops socketops;
|
williamr@2
|
161 |
extern int maxfiles; /* kernel limit on number of open files */
|
williamr@2
|
162 |
extern int maxfilesperproc; /* per process limit on number of open files */
|
williamr@2
|
163 |
extern int openfiles; /* (fl) actual number of open files */
|
williamr@2
|
164 |
extern struct sx filelist_lock; /* sx to protect filelist and openfiles */
|
williamr@2
|
165 |
|
williamr@2
|
166 |
/*
|
williamr@2
|
167 |
* The socket operations are used a couple of places.
|
williamr@2
|
168 |
* XXX: This is wrong, they should go through the operations vector for
|
williamr@2
|
169 |
* XXX: sockets instead of going directly for the individual functions. /phk
|
williamr@2
|
170 |
*/
|
williamr@2
|
171 |
fo_rdwr_t soo_read;
|
williamr@2
|
172 |
fo_rdwr_t soo_write;
|
williamr@2
|
173 |
fo_ioctl_t soo_ioctl;
|
williamr@2
|
174 |
fo_poll_t soo_poll;
|
williamr@2
|
175 |
fo_kqfilter_t soo_kqfilter;
|
williamr@2
|
176 |
fo_stat_t soo_stat;
|
williamr@2
|
177 |
fo_close_t soo_close;
|
williamr@2
|
178 |
|
williamr@2
|
179 |
/* Lock a file. */
|
williamr@2
|
180 |
#define FILE_LOCK(f) mtx_lock((f)->f_mtxp)
|
williamr@2
|
181 |
#define FILE_UNLOCK(f) mtx_unlock((f)->f_mtxp)
|
williamr@2
|
182 |
#define FILE_LOCKED(f) mtx_owned((f)->f_mtxp)
|
williamr@2
|
183 |
#define FILE_LOCK_ASSERT(f, type) mtx_assert((f)->f_mtxp, (type))
|
williamr@2
|
184 |
|
williamr@2
|
185 |
#define fhold_locked(fp) \
|
williamr@2
|
186 |
do { \
|
williamr@2
|
187 |
FILE_LOCK_ASSERT(fp, MA_OWNED); \
|
williamr@2
|
188 |
(fp)->f_count++; \
|
williamr@2
|
189 |
} while (0)
|
williamr@2
|
190 |
|
williamr@2
|
191 |
#define fhold(fp) \
|
williamr@2
|
192 |
do { \
|
williamr@2
|
193 |
FILE_LOCK(fp); \
|
williamr@2
|
194 |
(fp)->f_count++; \
|
williamr@2
|
195 |
FILE_UNLOCK(fp); \
|
williamr@2
|
196 |
} while (0)
|
williamr@2
|
197 |
|
williamr@2
|
198 |
static __inline fo_rdwr_t fo_read;
|
williamr@2
|
199 |
static __inline fo_rdwr_t fo_write;
|
williamr@2
|
200 |
static __inline fo_ioctl_t fo_ioctl;
|
williamr@2
|
201 |
static __inline fo_poll_t fo_poll;
|
williamr@2
|
202 |
static __inline fo_kqfilter_t fo_kqfilter;
|
williamr@2
|
203 |
static __inline fo_stat_t fo_stat;
|
williamr@2
|
204 |
static __inline fo_close_t fo_close;
|
williamr@2
|
205 |
|
williamr@2
|
206 |
static __inline int
|
williamr@2
|
207 |
fo_read(fp, uio, active_cred, flags, td)
|
williamr@2
|
208 |
struct file *fp;
|
williamr@2
|
209 |
struct uio *uio;
|
williamr@2
|
210 |
struct ucred *active_cred;
|
williamr@2
|
211 |
int flags;
|
williamr@2
|
212 |
struct thread *td;
|
williamr@2
|
213 |
{
|
williamr@2
|
214 |
|
williamr@2
|
215 |
return ((*fp->f_ops->fo_read)(fp, uio, active_cred, flags, td));
|
williamr@2
|
216 |
}
|
williamr@2
|
217 |
|
williamr@2
|
218 |
static __inline int
|
williamr@2
|
219 |
fo_write(fp, uio, active_cred, flags, td)
|
williamr@2
|
220 |
struct file *fp;
|
williamr@2
|
221 |
struct uio *uio;
|
williamr@2
|
222 |
struct ucred *active_cred;
|
williamr@2
|
223 |
int flags;
|
williamr@2
|
224 |
struct thread *td;
|
williamr@2
|
225 |
{
|
williamr@2
|
226 |
|
williamr@2
|
227 |
return ((*fp->f_ops->fo_write)(fp, uio, active_cred, flags, td));
|
williamr@2
|
228 |
}
|
williamr@2
|
229 |
|
williamr@2
|
230 |
static __inline int
|
williamr@2
|
231 |
fo_ioctl(fp, com, data, active_cred, td)
|
williamr@2
|
232 |
struct file *fp;
|
williamr@2
|
233 |
u_long com;
|
williamr@2
|
234 |
void *data;
|
williamr@2
|
235 |
struct ucred *active_cred;
|
williamr@2
|
236 |
struct thread *td;
|
williamr@2
|
237 |
{
|
williamr@2
|
238 |
|
williamr@2
|
239 |
return ((*fp->f_ops->fo_ioctl)(fp, com, data, active_cred, td));
|
williamr@2
|
240 |
}
|
williamr@2
|
241 |
|
williamr@2
|
242 |
static __inline int
|
williamr@2
|
243 |
fo_poll(fp, events, active_cred, td)
|
williamr@2
|
244 |
struct file *fp;
|
williamr@2
|
245 |
int events;
|
williamr@2
|
246 |
struct ucred *active_cred;
|
williamr@2
|
247 |
struct thread *td;
|
williamr@2
|
248 |
{
|
williamr@2
|
249 |
|
williamr@2
|
250 |
return ((*fp->f_ops->fo_poll)(fp, events, active_cred, td));
|
williamr@2
|
251 |
}
|
williamr@2
|
252 |
|
williamr@2
|
253 |
static __inline int
|
williamr@2
|
254 |
fo_stat(fp, sb, active_cred, td)
|
williamr@2
|
255 |
struct file *fp;
|
williamr@2
|
256 |
struct stat *sb;
|
williamr@2
|
257 |
struct ucred *active_cred;
|
williamr@2
|
258 |
struct thread *td;
|
williamr@2
|
259 |
{
|
williamr@2
|
260 |
|
williamr@2
|
261 |
return ((*fp->f_ops->fo_stat)(fp, sb, active_cred, td));
|
williamr@2
|
262 |
}
|
williamr@2
|
263 |
|
williamr@2
|
264 |
static __inline int
|
williamr@2
|
265 |
fo_close(fp, td)
|
williamr@2
|
266 |
struct file *fp;
|
williamr@2
|
267 |
struct thread *td;
|
williamr@2
|
268 |
{
|
williamr@2
|
269 |
|
williamr@2
|
270 |
return ((*fp->f_ops->fo_close)(fp, td));
|
williamr@2
|
271 |
}
|
williamr@2
|
272 |
|
williamr@2
|
273 |
static __inline int
|
williamr@2
|
274 |
fo_kqfilter(fp, kn)
|
williamr@2
|
275 |
struct file *fp;
|
williamr@2
|
276 |
struct knote *kn;
|
williamr@2
|
277 |
{
|
williamr@2
|
278 |
|
williamr@2
|
279 |
return ((*fp->f_ops->fo_kqfilter)(fp, kn));
|
williamr@2
|
280 |
}
|
williamr@2
|
281 |
|
williamr@2
|
282 |
#endif /* _KERNEL */
|
williamr@2
|
283 |
|
williamr@2
|
284 |
#endif /* !SYS_FILE_H */
|