1 /* $OpenBSD: file.h,v 1.24 2006/03/26 17:47:10 mickey Exp $ */ 2 /* $NetBSD: file.h,v 1.11 1995/03/26 20:24:13 jtc Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)file.h 8.2 (Berkeley) 8/20/94 33 */ 34 35 #include <sys/fcntl.h> 36 #include <sys/unistd.h> 37 38 #ifdef _KERNEL 39 #include <sys/queue.h> 40 41 struct proc; 42 struct uio; 43 struct knote; 44 struct stat; 45 struct file; 46 47 struct fileops { 48 int (*fo_read)(struct file *, off_t *, struct uio *, 49 struct ucred *); 50 int (*fo_write)(struct file *, off_t *, struct uio *, 51 struct ucred *); 52 int (*fo_ioctl)(struct file *, u_long, caddr_t, 53 struct proc *); 54 int (*fo_poll)(struct file *, int, struct proc *); 55 int (*fo_kqfilter)(struct file *, struct knote *); 56 int (*fo_stat)(struct file *, struct stat *, struct proc *); 57 int (*fo_close)(struct file *, struct proc *); 58 }; 59 60 /* 61 * Kernel descriptor table. 62 * One entry for each open kernel vnode and socket. 63 */ 64 struct file { 65 LIST_ENTRY(file) f_list;/* list of active files */ 66 short f_flag; /* see fcntl.h */ 67 #define DTYPE_VNODE 1 /* file */ 68 #define DTYPE_SOCKET 2 /* communications endpoint */ 69 #define DTYPE_PIPE 3 /* pipe */ 70 #define DTYPE_KQUEUE 4 /* event queue */ 71 #define DTYPE_CRYPTO 5 /* crypto */ 72 #define DTYPE_SYSTRACE 6 /* system call tracing */ 73 short f_type; /* descriptor type */ 74 long f_count; /* reference count */ 75 long f_msgcount; /* references from message queue */ 76 struct ucred *f_cred; /* credentials associated with descriptor */ 77 struct fileops *f_ops; 78 off_t f_offset; 79 void *f_data; /* private data */ 80 int f_iflags; /* internal flags */ 81 int f_usecount; /* number of users (temporary references). */ 82 u_int64_t f_rxfer; /* total number of read transfers */ 83 u_int64_t f_wxfer; /* total number of write transfers */ 84 u_int64_t f_seek; /* total independent seek operations */ 85 u_int64_t f_rbytes; /* total bytes read */ 86 u_int64_t f_wbytes; /* total bytes written */ 87 }; 88 89 #define FIF_WANTCLOSE 0x01 /* a close is waiting for usecount */ 90 #define FIF_LARVAL 0x02 /* not fully constructed, don't use */ 91 92 #define FILE_IS_USABLE(fp) \ 93 (((fp)->f_iflags & (FIF_WANTCLOSE|FIF_LARVAL)) == 0) 94 95 #define FREF(fp) do { (fp)->f_usecount++; } while (0) 96 #define FRELE(fp) do { \ 97 --(fp)->f_usecount; \ 98 if (((fp)->f_iflags & FIF_WANTCLOSE) != 0) \ 99 wakeup(&(fp)->f_usecount); \ 100 } while (0) 101 102 #define FILE_SET_MATURE(fp) do { \ 103 (fp)->f_iflags &= ~FIF_LARVAL; \ 104 FRELE(fp); \ 105 } while (0) 106 107 LIST_HEAD(filelist, file); 108 extern struct filelist filehead; /* head of list of open files */ 109 extern int maxfiles; /* kernel limit on number of open files */ 110 extern int nfiles; /* actual number of open files */ 111 extern struct fileops vnops; /* vnode operations for files */ 112 113 int dofileread(struct proc *, int, struct file *, void *, size_t, 114 off_t *, register_t *); 115 int dofilewrite(struct proc *, int, struct file *, const void *, 116 size_t, off_t *, register_t *); 117 118 #endif /* _KERNEL */