00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _SYS_FILE_H_
00034 #define _SYS_FILE_H_
00035
00036 #ifndef _KERNEL
00037 #include <sys/types.h>
00038 #include <sys/fcntl.h>
00039 #include <sys/unistd.h>
00040 #else
00041 #include <sys/queue.h>
00042 #include <sys/_lock.h>
00043 #include <sys/_mutex.h>
00044
00045 struct stat;
00046 struct thread;
00047 struct uio;
00048 struct knote;
00049 struct vnode;
00050 struct socket;
00051
00052
00053 #endif
00054
00055 #define DTYPE_VNODE 1
00056 #define DTYPE_SOCKET 2
00057 #define DTYPE_PIPE 3
00058 #define DTYPE_FIFO 4
00059 #define DTYPE_KQUEUE 5
00060 #define DTYPE_CRYPTO 6
00061
00062 #ifdef _KERNEL
00063
00064 struct file;
00065 struct ucred;
00066
00067 typedef int fo_rdwr_t(struct file *fp, struct uio *uio,
00068 struct ucred *active_cred, int flags,
00069 struct thread *td);
00070 #define FOF_OFFSET 1
00071 typedef int fo_ioctl_t(struct file *fp, u_long com, void *data,
00072 struct ucred *active_cred, struct thread *td);
00073 typedef int fo_poll_t(struct file *fp, int events,
00074 struct ucred *active_cred, struct thread *td);
00075 typedef int fo_kqfilter_t(struct file *fp, struct knote *kn);
00076 typedef int fo_stat_t(struct file *fp, struct stat *sb,
00077 struct ucred *active_cred, struct thread *td);
00078 typedef int fo_close_t(struct file *fp, struct thread *td);
00079 typedef int fo_flags_t;
00080
00081 struct fileops {
00082 fo_rdwr_t *fo_read;
00083 fo_rdwr_t *fo_write;
00084 fo_ioctl_t *fo_ioctl;
00085 fo_poll_t *fo_poll;
00086 fo_kqfilter_t *fo_kqfilter;
00087 fo_stat_t *fo_stat;
00088 fo_close_t *fo_close;
00089 fo_flags_t fo_flags;
00090 };
00091
00092 #define DFLAG_PASSABLE 0x01
00093 #define DFLAG_SEEKABLE 0x02
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 struct file {
00107 LIST_ENTRY(file) f_list;
00108 short f_type;
00109 void *f_data;
00110 u_int f_flag;
00111 struct mtx *f_mtxp;
00112 struct fileops *f_ops;
00113 struct ucred *f_cred;
00114 int f_count;
00115 struct vnode *f_vnode;
00116
00117
00118 off_t f_offset;
00119
00120
00121 short f_gcflag;
00122 #define FMARK 0x1
00123 #define FDEFER 0x2
00124 int f_msgcount;
00125
00126
00127 int f_seqcount;
00128
00129
00130
00131 off_t f_nextoff;
00132
00133
00134 void *f_label;
00135 };
00136
00137 #endif
00138
00139
00140
00141
00142 struct xfile {
00143 size_t xf_size;
00144 pid_t xf_pid;
00145 uid_t xf_uid;
00146 int xf_fd;
00147 void *xf_file;
00148 short xf_type;
00149 int xf_count;
00150 int xf_msgcount;
00151 off_t xf_offset;
00152 void *xf_data;
00153 void *xf_vnode;
00154 u_int xf_flag;
00155 };
00156
00157 #ifdef _KERNEL
00158 extern struct filelist filehead;
00159 extern struct fileops vnops;
00160 extern struct fileops badfileops;
00161 extern struct fileops socketops;
00162 extern int maxfiles;
00163 extern int maxfilesperproc;
00164 extern int openfiles;
00165 extern struct sx filelist_lock;
00166
00167
00168
00169
00170
00171
00172 fo_rdwr_t soo_read;
00173 fo_rdwr_t soo_write;
00174 fo_ioctl_t soo_ioctl;
00175 fo_poll_t soo_poll;
00176 fo_kqfilter_t soo_kqfilter;
00177 fo_stat_t soo_stat;
00178 fo_close_t soo_close;
00179
00180
00181 #define FILE_LOCK(f) mtx_lock((f)->f_mtxp)
00182 #define FILE_UNLOCK(f) mtx_unlock((f)->f_mtxp)
00183 #define FILE_LOCKED(f) mtx_owned((f)->f_mtxp)
00184 #define FILE_LOCK_ASSERT(f, type) mtx_assert((f)->f_mtxp, (type))
00185
00186 #define fhold_locked(fp) \
00187 do { \
00188 FILE_LOCK_ASSERT(fp, MA_OWNED); \
00189 (fp)->f_count++; \
00190 } while (0)
00191
00192 #define fhold(fp) \
00193 do { \
00194 FILE_LOCK(fp); \
00195 (fp)->f_count++; \
00196 FILE_UNLOCK(fp); \
00197 } while (0)
00198
00199 static __inline fo_rdwr_t fo_read;
00200 static __inline fo_rdwr_t fo_write;
00201 static __inline fo_ioctl_t fo_ioctl;
00202 static __inline fo_poll_t fo_poll;
00203 static __inline fo_kqfilter_t fo_kqfilter;
00204 static __inline fo_stat_t fo_stat;
00205 static __inline fo_close_t fo_close;
00206
00207 static __inline int
00208 fo_read(fp, uio, active_cred, flags, td)
00209 struct file *fp;
00210 struct uio *uio;
00211 struct ucred *active_cred;
00212 int flags;
00213 struct thread *td;
00214 {
00215
00216 return ((*fp->f_ops->fo_read)(fp, uio, active_cred, flags, td));
00217 }
00218
00219 static __inline int
00220 fo_write(fp, uio, active_cred, flags, td)
00221 struct file *fp;
00222 struct uio *uio;
00223 struct ucred *active_cred;
00224 int flags;
00225 struct thread *td;
00226 {
00227
00228 return ((*fp->f_ops->fo_write)(fp, uio, active_cred, flags, td));
00229 }
00230
00231 static __inline int
00232 fo_ioctl(fp, com, data, active_cred, td)
00233 struct file *fp;
00234 u_long com;
00235 void *data;
00236 struct ucred *active_cred;
00237 struct thread *td;
00238 {
00239
00240 return ((*fp->f_ops->fo_ioctl)(fp, com, data, active_cred, td));
00241 }
00242
00243 static __inline int
00244 fo_poll(fp, events, active_cred, td)
00245 struct file *fp;
00246 int events;
00247 struct ucred *active_cred;
00248 struct thread *td;
00249 {
00250
00251 return ((*fp->f_ops->fo_poll)(fp, events, active_cred, td));
00252 }
00253
00254 static __inline int
00255 fo_stat(fp, sb, active_cred, td)
00256 struct file *fp;
00257 struct stat *sb;
00258 struct ucred *active_cred;
00259 struct thread *td;
00260 {
00261
00262 return ((*fp->f_ops->fo_stat)(fp, sb, active_cred, td));
00263 }
00264
00265 static __inline int
00266 fo_close(fp, td)
00267 struct file *fp;
00268 struct thread *td;
00269 {
00270
00271 return ((*fp->f_ops->fo_close)(fp, td));
00272 }
00273
00274 static __inline int
00275 fo_kqfilter(fp, kn)
00276 struct file *fp;
00277 struct knote *kn;
00278 {
00279
00280 return ((*fp->f_ops->fo_kqfilter)(fp, kn));
00281 }
00282
00283 #endif
00284
00285 #endif