file.h

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1982, 1986, 1989, 1993
00003  *      The Regents of the University of California.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 4. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  * © Portions copyright (c) 2007 Symbian Software Ltd. All rights reserved.
00029  *      @(#)file.h      8.3 (Berkeley) 1/9/95
00030  * $FreeBSD: src/sys/sys/file.h,v 1.70 2005/02/10 12:27:58 phk Exp $
00031  */
00032 
00033 #ifndef _SYS_FILE_H_
00034 #define _SYS_FILE_H_
00035 
00036 #ifndef _KERNEL
00037 #include <sys/types.h> /* XXX */
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 /* _KERNEL */
00054 
00055 #define DTYPE_VNODE     1       /* file */
00056 #define DTYPE_SOCKET    2       /* communications endpoint */
00057 #define DTYPE_PIPE      3       /* pipe */
00058 #define DTYPE_FIFO      4       /* fifo (named pipe) */
00059 #define DTYPE_KQUEUE    5       /* event queue */
00060 #define DTYPE_CRYPTO    6       /* crypto */
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       /* Use the offset in uio argument */
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;       /* DFLAG_* below */
00090 };
00091 
00092 #define DFLAG_PASSABLE  0x01    /* may be passed via unix sockets. */
00093 #define DFLAG_SEEKABLE  0x02    /* seekable / nonsequential */
00094 
00095 /*
00096  * Kernel descriptor table.
00097  * One entry for each open kernel vnode and socket.
00098  *
00099  * Below is the list of locks that protects members in struct file.
00100  *
00101  * (fl) filelist_lock
00102  * (f)  f_mtx in struct file
00103  * none not locked
00104  */
00105 
00106 struct file {
00107         LIST_ENTRY(file) f_list;/* (fl) list of active files */
00108         short   f_type;         /* descriptor type */
00109         void    *f_data;        /* file descriptor specific data */
00110         u_int   f_flag;         /* see fcntl.h */
00111         struct mtx      *f_mtxp;        /* mutex to protect data */
00112         struct fileops *f_ops;  /* File operations */
00113         struct  ucred *f_cred;  /* credentials associated with descriptor */
00114         int     f_count;        /* (f) reference count */
00115         struct vnode *f_vnode;  /* NULL or applicable vnode */
00116 
00117         /* DFLAG_SEEKABLE specific fields */
00118         off_t   f_offset;
00119 
00120         /* DTYPE_SOCKET specific fields */
00121         short   f_gcflag;       /* used by thread doing fd garbage collection */
00122 #define FMARK           0x1     /* mark during gc() */
00123 #define FDEFER          0x2     /* defer for next gc pass */
00124         int     f_msgcount;     /* (f) references from message queue */
00125 
00126         /* DTYPE_VNODE specific fields */
00127         int     f_seqcount;     /*
00128                                  * count of sequential accesses -- cleared
00129                                  * by most seek operations.
00130                                  */
00131         off_t   f_nextoff;      /*
00132                                  * offset of next expected read or write
00133                                  */
00134         void    *f_label;       /* Place-holder for struct label pointer. */
00135 };
00136 
00137 #endif /* _KERNEL */
00138 
00139 /*
00140  * Userland version of struct file, for sysctl
00141  */
00142 struct xfile {
00143         size_t  xf_size;        /* size of struct xfile */
00144         pid_t   xf_pid;         /* owning process */
00145         uid_t   xf_uid;         /* effective uid of owning process */
00146         int     xf_fd;          /* descriptor number */
00147         void    *xf_file;       /* address of struct file */
00148         short   xf_type;        /* descriptor type */
00149         int     xf_count;       /* reference count */
00150         int     xf_msgcount;    /* references from message queue */
00151         off_t   xf_offset;      /* file offset */
00152         void    *xf_data;       /* file descriptor specific data */
00153         void    *xf_vnode;      /* vnode pointer */
00154         u_int   xf_flag;        /* flags (see fcntl.h) */
00155 };
00156 
00157 #ifdef _KERNEL
00158 extern struct filelist filehead; /* (fl) head of list of open files */
00159 extern struct fileops vnops;
00160 extern struct fileops badfileops;
00161 extern struct fileops socketops;
00162 extern int maxfiles;            /* kernel limit on number of open files */
00163 extern int maxfilesperproc;     /* per process limit on number of open files */
00164 extern int openfiles;           /* (fl) actual number of open files */
00165 extern struct sx filelist_lock; /* sx to protect filelist and openfiles */
00166 
00167 /*
00168  * The socket operations are used a couple of places.
00169  * XXX: This is wrong, they should go through the operations vector for
00170  * XXX: sockets instead of going directly for the individual functions. /phk
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 /* Lock a file. */
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 /* _KERNEL */
00284 
00285 #endif /* !SYS_FILE_H */

Copyright © Nokia Corporation 2001-2008
Back to top