1 /* $NetBSD: file.h,v 1.34 2002/10/23 09:14:57 jdolecek Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)file.h 8.3 (Berkeley) 1/9/95 36 */ 37 38 #ifndef _SYS_FILE_H_ 39 #define _SYS_FILE_H_ 40 41 #include <sys/fcntl.h> 42 #include <sys/unistd.h> 43 44 #ifdef _KERNEL 45 #include <sys/queue.h> 46 47 struct proc; 48 struct uio; 49 struct iovec; 50 struct stat; 51 struct knote; 52 53 /* 54 * Kernel descriptor table. 55 * One entry for each open kernel vnode and socket. 56 */ 57 struct file { 58 LIST_ENTRY(file) f_list; /* list of active files */ 59 int f_flag; /* see fcntl.h */ 60 int f_iflags; /* internal flags */ 61 #define DTYPE_VNODE 1 /* file */ 62 #define DTYPE_SOCKET 2 /* communications endpoint */ 63 #define DTYPE_PIPE 3 /* pipe */ 64 #define DTYPE_KQUEUE 4 /* event queue */ 65 #define DTYPE_MISC 5 /* misc file descriptor type */ 66 int f_type; /* descriptor type */ 67 u_int f_count; /* reference count */ 68 u_int f_msgcount; /* references from message queue */ 69 int f_usecount; /* number active users */ 70 struct ucred *f_cred; /* creds associated with descriptor */ 71 struct fileops { 72 int (*fo_read) (struct file *fp, off_t *offset, 73 struct uio *uio, 74 struct ucred *cred, int flags); 75 int (*fo_write) (struct file *fp, off_t *offset, 76 struct uio *uio, 77 struct ucred *cred, int flags); 78 int (*fo_ioctl) (struct file *fp, u_long com, 79 caddr_t data, struct proc *p); 80 int (*fo_fcntl) (struct file *fp, u_int com, 81 caddr_t data, struct proc *p); 82 int (*fo_poll) (struct file *fp, int events, 83 struct proc *p); 84 int (*fo_stat) (struct file *fp, struct stat *sp, 85 struct proc *p); 86 int (*fo_close) (struct file *fp, struct proc *p); 87 int (*fo_kqfilter) (struct file *fp, struct knote *kn); 88 } *f_ops; 89 off_t f_offset; 90 caddr_t f_data; /* descriptor data, e.g. vnode/socket */ 91 }; 92 93 #define FIF_WANTCLOSE 0x01 /* a close is waiting for usecount */ 94 #define FIF_LARVAL 0x02 /* not fully constructed; don't use */ 95 96 #define FILE_IS_USABLE(fp) (((fp)->f_iflags & \ 97 (FIF_WANTCLOSE|FIF_LARVAL)) == 0) 98 99 #define FILE_SET_MATURE(fp) \ 100 do { \ 101 (fp)->f_iflags &= ~FIF_LARVAL; \ 102 } while (/*CONSTCOND*/0) 103 104 #ifdef DIAGNOSTIC 105 #define FILE_USE_CHECK(fp, str) \ 106 do { \ 107 if ((fp)->f_usecount < 0) \ 108 panic(str); \ 109 } while (/* CONSTCOND */ 0) 110 #else 111 #define FILE_USE_CHECK(fp, str) /* nothing */ 112 #endif 113 114 #define FILE_USE(fp) \ 115 do { \ 116 (fp)->f_usecount++; \ 117 FILE_USE_CHECK((fp), "f_usecount overflow"); \ 118 } while (/* CONSTCOND */ 0) 119 120 #define FILE_UNUSE(fp, p) \ 121 do { \ 122 if ((fp)->f_iflags & FIF_WANTCLOSE) { \ 123 /* Will drop usecount */ \ 124 (void) closef((fp), (p)); \ 125 } else { \ 126 (fp)->f_usecount--; \ 127 FILE_USE_CHECK((fp), "f_usecount underflow"); \ 128 } \ 129 } while (/* CONSTCOND */ 0) 130 131 /* 132 * Flags for fo_read and fo_write. 133 */ 134 #define FOF_UPDATE_OFFSET 0x01 /* update the file offset */ 135 136 LIST_HEAD(filelist, file); 137 extern struct filelist filehead; /* head of list of open files */ 138 extern int maxfiles; /* kernel limit on # of open files */ 139 extern int nfiles; /* actual number of open files */ 140 141 extern struct fileops vnops; /* vnode operations for files */ 142 143 int dofileread(struct proc *, int, struct file *, void *, size_t, 144 off_t *, int, register_t *); 145 int dofilewrite(struct proc *, int, struct file *, const void *, 146 size_t, off_t *, int, register_t *); 147 148 int dofilereadv(struct proc *, int, struct file *, 149 const struct iovec *, int, off_t *, int, register_t *); 150 int dofilewritev(struct proc *, int, struct file *, 151 const struct iovec *, int, off_t *, int, register_t *); 152 153 void finit(void); 154 155 #endif /* _KERNEL */ 156 157 #endif /* _SYS_FILE_H_ */ 158