2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
24 #include <sys/types.h> /* XXX */
25 #include <sys/fcntl.h>
26 #include <sys/unistd.h>
28 #include <sys/queue.h>
29 #include <sys/_lock.h>
30 #include <sys/_mutex.h>
42 #define DTYPE_VNODE 1 /* file */
43 #define DTYPE_SOCKET 2 /* communications endpoint */
44 #define DTYPE_PIPE 3 /* pipe */
45 #define DTYPE_FIFO 4 /* fifo (named pipe) */
46 #define DTYPE_KQUEUE 5 /* event queue */
47 #define DTYPE_CRYPTO 6 /* crypto */
54 typedef int fo_rdwr_t(struct file *fp, struct uio *uio,
55 struct ucred *active_cred, int flags,
57 #define FOF_OFFSET 1 /* Use the offset in uio argument */
58 typedef int fo_ioctl_t(struct file *fp, u_long com, void *data,
59 struct ucred *active_cred, struct thread *td);
60 typedef int fo_poll_t(struct file *fp, int events,
61 struct ucred *active_cred, struct thread *td);
62 typedef int fo_kqfilter_t(struct file *fp, struct knote *kn);
63 typedef int fo_stat_t(struct file *fp, struct stat *sb,
64 struct ucred *active_cred, struct thread *td);
65 typedef int fo_close_t(struct file *fp, struct thread *td);
66 typedef int fo_flags_t;
73 fo_kqfilter_t *fo_kqfilter;
76 fo_flags_t fo_flags; /* DFLAG_* below */
79 #define DFLAG_PASSABLE 0x01 /* may be passed via unix sockets. */
80 #define DFLAG_SEEKABLE 0x02 /* seekable / nonsequential */
83 * Kernel descriptor table.
84 * One entry for each open kernel vnode and socket.
86 * Below is the list of locks that protects members in struct file.
89 * (f) f_mtx in struct file
94 LIST_ENTRY(file) f_list;/* (fl) list of active files */
95 short f_type; /* descriptor type */
96 void *f_data; /* file descriptor specific data */
97 u_int f_flag; /* see fcntl.h */
98 struct mtx *f_mtxp; /* mutex to protect data */
99 struct fileops *f_ops; /* File operations */
100 struct ucred *f_cred; /* credentials associated with descriptor */
101 int f_count; /* (f) reference count */
102 struct vnode *f_vnode; /* NULL or applicable vnode */
104 /* DFLAG_SEEKABLE specific fields */
107 /* DTYPE_SOCKET specific fields */
108 short f_gcflag; /* used by thread doing fd garbage collection */
109 #define FMARK 0x1 /* mark during gc() */
110 #define FDEFER 0x2 /* defer for next gc pass */
111 int f_msgcount; /* (f) references from message queue */
113 /* DTYPE_VNODE specific fields */
115 * count of sequential accesses -- cleared
116 * by most seek operations.
119 * offset of next expected read or write
121 void *f_label; /* Place-holder for struct label pointer. */
127 * Userland version of struct file, for sysctl
130 size_t xf_size; /* size of struct xfile */
131 pid_t xf_pid; /* owning process */
132 uid_t xf_uid; /* effective uid of owning process */
133 int xf_fd; /* descriptor number */
134 void *xf_file; /* address of struct file */
135 short xf_type; /* descriptor type */
136 int xf_count; /* reference count */
137 int xf_msgcount; /* references from message queue */
138 off_t xf_offset; /* file offset */
139 void *xf_data; /* file descriptor specific data */
140 void *xf_vnode; /* vnode pointer */
141 u_int xf_flag; /* flags (see fcntl.h) */
145 extern struct filelist filehead; /* (fl) head of list of open files */
146 extern struct fileops vnops;
147 extern struct fileops badfileops;
148 extern struct fileops socketops;
149 extern int maxfiles; /* kernel limit on number of open files */
150 extern int maxfilesperproc; /* per process limit on number of open files */
151 extern int openfiles; /* (fl) actual number of open files */
152 extern struct sx filelist_lock; /* sx to protect filelist and openfiles */
155 * The socket operations are used a couple of places.
156 * XXX: This is wrong, they should go through the operations vector for
157 * XXX: sockets instead of going directly for the individual functions. /phk
161 fo_ioctl_t soo_ioctl;
163 fo_kqfilter_t soo_kqfilter;
165 fo_close_t soo_close;
168 #define FILE_LOCK(f) mtx_lock((f)->f_mtxp)
169 #define FILE_UNLOCK(f) mtx_unlock((f)->f_mtxp)
170 #define FILE_LOCKED(f) mtx_owned((f)->f_mtxp)
171 #define FILE_LOCK_ASSERT(f, type) mtx_assert((f)->f_mtxp, (type))
173 #define fhold_locked(fp) \
175 FILE_LOCK_ASSERT(fp, MA_OWNED); \
186 static __inline fo_rdwr_t fo_read;
187 static __inline fo_rdwr_t fo_write;
188 static __inline fo_ioctl_t fo_ioctl;
189 static __inline fo_poll_t fo_poll;
190 static __inline fo_kqfilter_t fo_kqfilter;
191 static __inline fo_stat_t fo_stat;
192 static __inline fo_close_t fo_close;
195 fo_read(fp, uio, active_cred, flags, td)
198 struct ucred *active_cred;
203 return ((*fp->f_ops->fo_read)(fp, uio, active_cred, flags, td));
207 fo_write(fp, uio, active_cred, flags, td)
210 struct ucred *active_cred;
215 return ((*fp->f_ops->fo_write)(fp, uio, active_cred, flags, td));
219 fo_ioctl(fp, com, data, active_cred, td)
223 struct ucred *active_cred;
227 return ((*fp->f_ops->fo_ioctl)(fp, com, data, active_cred, td));
231 fo_poll(fp, events, active_cred, td)
234 struct ucred *active_cred;
238 return ((*fp->f_ops->fo_poll)(fp, events, active_cred, td));
242 fo_stat(fp, sb, active_cred, td)
245 struct ucred *active_cred;
249 return ((*fp->f_ops->fo_stat)(fp, sb, active_cred, td));
258 return ((*fp->f_ops->fo_close)(fp, td));
267 return ((*fp->f_ops->fo_kqfilter)(fp, kn));
272 #endif /* !SYS_FILE_H */