sl@0: /*- sl@0: * Copyright (c) 1982, 1986, 1989, 1993 sl@0: * The Regents of the University of California. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 4. Neither the name of the University nor the names of its contributors sl@0: * may be used to endorse or promote products derived from this software sl@0: * without specific prior written permission. sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * © Portions Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: * @(#)file.h 8.3 (Berkeley) 1/9/95 sl@0: * $FreeBSD: src/sys/sys/file.h,v 1.70 2005/02/10 12:27:58 phk Exp $ sl@0: */ sl@0: sl@0: #ifndef _SYS_FILE_H_ sl@0: #define _SYS_FILE_H_ sl@0: sl@0: #ifndef _KERNEL sl@0: #include /* XXX */ sl@0: #include sl@0: #include sl@0: #else sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: struct stat; sl@0: struct thread; sl@0: struct uio; sl@0: struct knote; sl@0: struct vnode; sl@0: struct socket; sl@0: sl@0: sl@0: #endif /* _KERNEL */ sl@0: sl@0: #define DTYPE_VNODE 1 /* file */ sl@0: #define DTYPE_SOCKET 2 /* communications endpoint */ sl@0: #define DTYPE_PIPE 3 /* pipe */ sl@0: #define DTYPE_FIFO 4 /* fifo (named pipe) */ sl@0: #define DTYPE_KQUEUE 5 /* event queue */ sl@0: #define DTYPE_CRYPTO 6 /* crypto */ sl@0: sl@0: #ifdef _KERNEL sl@0: sl@0: struct file; sl@0: struct ucred; sl@0: sl@0: typedef int fo_rdwr_t(struct file *fp, struct uio *uio, sl@0: struct ucred *active_cred, int flags, sl@0: struct thread *td); sl@0: #define FOF_OFFSET 1 /* Use the offset in uio argument */ sl@0: typedef int fo_ioctl_t(struct file *fp, u_long com, void *data, sl@0: struct ucred *active_cred, struct thread *td); sl@0: typedef int fo_poll_t(struct file *fp, int events, sl@0: struct ucred *active_cred, struct thread *td); sl@0: typedef int fo_kqfilter_t(struct file *fp, struct knote *kn); sl@0: typedef int fo_stat_t(struct file *fp, struct stat *sb, sl@0: struct ucred *active_cred, struct thread *td); sl@0: typedef int fo_close_t(struct file *fp, struct thread *td); sl@0: typedef int fo_flags_t; sl@0: sl@0: struct fileops { sl@0: fo_rdwr_t *fo_read; sl@0: fo_rdwr_t *fo_write; sl@0: fo_ioctl_t *fo_ioctl; sl@0: fo_poll_t *fo_poll; sl@0: fo_kqfilter_t *fo_kqfilter; sl@0: fo_stat_t *fo_stat; sl@0: fo_close_t *fo_close; sl@0: fo_flags_t fo_flags; /* DFLAG_* below */ sl@0: }; sl@0: sl@0: #define DFLAG_PASSABLE 0x01 /* may be passed via unix sockets. */ sl@0: #define DFLAG_SEEKABLE 0x02 /* seekable / nonsequential */ sl@0: sl@0: /* sl@0: * Kernel descriptor table. sl@0: * One entry for each open kernel vnode and socket. sl@0: * sl@0: * Below is the list of locks that protects members in struct file. sl@0: * sl@0: * (fl) filelist_lock sl@0: * (f) f_mtx in struct file sl@0: * none not locked sl@0: */ sl@0: sl@0: struct file { sl@0: LIST_ENTRY(file) f_list;/* (fl) list of active files */ sl@0: short f_type; /* descriptor type */ sl@0: void *f_data; /* file descriptor specific data */ sl@0: u_int f_flag; /* see fcntl.h */ sl@0: struct mtx *f_mtxp; /* mutex to protect data */ sl@0: struct fileops *f_ops; /* File operations */ sl@0: struct ucred *f_cred; /* credentials associated with descriptor */ sl@0: int f_count; /* (f) reference count */ sl@0: struct vnode *f_vnode; /* NULL or applicable vnode */ sl@0: sl@0: /* DFLAG_SEEKABLE specific fields */ sl@0: off_t f_offset; sl@0: sl@0: /* DTYPE_SOCKET specific fields */ sl@0: short f_gcflag; /* used by thread doing fd garbage collection */ sl@0: #define FMARK 0x1 /* mark during gc() */ sl@0: #define FDEFER 0x2 /* defer for next gc pass */ sl@0: int f_msgcount; /* (f) references from message queue */ sl@0: sl@0: /* DTYPE_VNODE specific fields */ sl@0: int f_seqcount; /* sl@0: * count of sequential accesses -- cleared sl@0: * by most seek operations. sl@0: */ sl@0: off_t f_nextoff; /* sl@0: * offset of next expected read or write sl@0: */ sl@0: void *f_label; /* Place-holder for struct label pointer. */ sl@0: }; sl@0: sl@0: #endif /* _KERNEL */ sl@0: sl@0: /* sl@0: * Userland version of struct file, for sysctl sl@0: */ sl@0: struct xfile { sl@0: size_t xf_size; /* size of struct xfile */ sl@0: pid_t xf_pid; /* owning process */ sl@0: uid_t xf_uid; /* effective uid of owning process */ sl@0: int xf_fd; /* descriptor number */ sl@0: void *xf_file; /* address of struct file */ sl@0: short xf_type; /* descriptor type */ sl@0: int xf_count; /* reference count */ sl@0: int xf_msgcount; /* references from message queue */ sl@0: off_t xf_offset; /* file offset */ sl@0: void *xf_data; /* file descriptor specific data */ sl@0: void *xf_vnode; /* vnode pointer */ sl@0: u_int xf_flag; /* flags (see fcntl.h) */ sl@0: }; sl@0: sl@0: #ifdef _KERNEL sl@0: extern struct filelist filehead; /* (fl) head of list of open files */ sl@0: extern struct fileops vnops; sl@0: extern struct fileops badfileops; sl@0: extern struct fileops socketops; sl@0: extern int maxfiles; /* kernel limit on number of open files */ sl@0: extern int maxfilesperproc; /* per process limit on number of open files */ sl@0: extern int openfiles; /* (fl) actual number of open files */ sl@0: extern struct sx filelist_lock; /* sx to protect filelist and openfiles */ sl@0: sl@0: /* sl@0: * The socket operations are used a couple of places. sl@0: * XXX: This is wrong, they should go through the operations vector for sl@0: * XXX: sockets instead of going directly for the individual functions. /phk sl@0: */ sl@0: fo_rdwr_t soo_read; sl@0: fo_rdwr_t soo_write; sl@0: fo_ioctl_t soo_ioctl; sl@0: fo_poll_t soo_poll; sl@0: fo_kqfilter_t soo_kqfilter; sl@0: fo_stat_t soo_stat; sl@0: fo_close_t soo_close; sl@0: sl@0: /* Lock a file. */ sl@0: #define FILE_LOCK(f) mtx_lock((f)->f_mtxp) sl@0: #define FILE_UNLOCK(f) mtx_unlock((f)->f_mtxp) sl@0: #define FILE_LOCKED(f) mtx_owned((f)->f_mtxp) sl@0: #define FILE_LOCK_ASSERT(f, type) mtx_assert((f)->f_mtxp, (type)) sl@0: sl@0: #define fhold_locked(fp) \ sl@0: do { \ sl@0: FILE_LOCK_ASSERT(fp, MA_OWNED); \ sl@0: (fp)->f_count++; \ sl@0: } while (0) sl@0: sl@0: #define fhold(fp) \ sl@0: do { \ sl@0: FILE_LOCK(fp); \ sl@0: (fp)->f_count++; \ sl@0: FILE_UNLOCK(fp); \ sl@0: } while (0) sl@0: sl@0: static __inline fo_rdwr_t fo_read; sl@0: static __inline fo_rdwr_t fo_write; sl@0: static __inline fo_ioctl_t fo_ioctl; sl@0: static __inline fo_poll_t fo_poll; sl@0: static __inline fo_kqfilter_t fo_kqfilter; sl@0: static __inline fo_stat_t fo_stat; sl@0: static __inline fo_close_t fo_close; sl@0: sl@0: static __inline int sl@0: fo_read(fp, uio, active_cred, flags, td) sl@0: struct file *fp; sl@0: struct uio *uio; sl@0: struct ucred *active_cred; sl@0: int flags; sl@0: struct thread *td; sl@0: { sl@0: sl@0: return ((*fp->f_ops->fo_read)(fp, uio, active_cred, flags, td)); sl@0: } sl@0: sl@0: static __inline int sl@0: fo_write(fp, uio, active_cred, flags, td) sl@0: struct file *fp; sl@0: struct uio *uio; sl@0: struct ucred *active_cred; sl@0: int flags; sl@0: struct thread *td; sl@0: { sl@0: sl@0: return ((*fp->f_ops->fo_write)(fp, uio, active_cred, flags, td)); sl@0: } sl@0: sl@0: static __inline int sl@0: fo_ioctl(fp, com, data, active_cred, td) sl@0: struct file *fp; sl@0: u_long com; sl@0: void *data; sl@0: struct ucred *active_cred; sl@0: struct thread *td; sl@0: { sl@0: sl@0: return ((*fp->f_ops->fo_ioctl)(fp, com, data, active_cred, td)); sl@0: } sl@0: sl@0: static __inline int sl@0: fo_poll(fp, events, active_cred, td) sl@0: struct file *fp; sl@0: int events; sl@0: struct ucred *active_cred; sl@0: struct thread *td; sl@0: { sl@0: sl@0: return ((*fp->f_ops->fo_poll)(fp, events, active_cred, td)); sl@0: } sl@0: sl@0: static __inline int sl@0: fo_stat(fp, sb, active_cred, td) sl@0: struct file *fp; sl@0: struct stat *sb; sl@0: struct ucred *active_cred; sl@0: struct thread *td; sl@0: { sl@0: sl@0: return ((*fp->f_ops->fo_stat)(fp, sb, active_cred, td)); sl@0: } sl@0: sl@0: static __inline int sl@0: fo_close(fp, td) sl@0: struct file *fp; sl@0: struct thread *td; sl@0: { sl@0: sl@0: return ((*fp->f_ops->fo_close)(fp, td)); sl@0: } sl@0: sl@0: static __inline int sl@0: fo_kqfilter(fp, kn) sl@0: struct file *fp; sl@0: struct knote *kn; sl@0: { sl@0: sl@0: return ((*fp->f_ops->fo_kqfilter)(fp, kn)); sl@0: } sl@0: sl@0: #endif /* _KERNEL */ sl@0: sl@0: #endif /* !SYS_FILE_H */