1 /* $NetBSD: file.h,v 1.31 2001/12/18 22:29:25 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 52 /* 53 * Kernel descriptor table. 54 * One entry for each open kernel vnode and socket. 55 */ 56 struct file { 57 LIST_ENTRY(file) f_list; /* list of active files */ 58 int f_flag; /* see fcntl.h */ 59 int f_iflags; /* internal flags */ 60 #define DTYPE_VNODE 1 /* file */ 61 #define DTYPE_SOCKET 2 /* communications endpoint */ 62 #define DTYPE_PIPE 3 /* pipe */ 63 int f_type; /* descriptor type */ 64 u_int f_count; /* reference count */ 65 u_int f_msgcount; /* references from message queue */ 66 int f_usecount; /* number active users */ 67 struct ucred *f_cred; /* creds associated with descriptor */ 68 struct fileops { 69 int (*fo_read) (struct file *fp, off_t *offset, 70 struct uio *uio, 71 struct ucred *cred, int flags); 72 int (*fo_write) (struct file *fp, off_t *offset, 73 struct uio *uio, 74 struct ucred *cred, int flags); 75 int (*fo_ioctl) (struct file *fp, u_long com, 76 caddr_t data, struct proc *p); 77 int (*fo_fcntl) (struct file *fp, u_int com, 78 caddr_t data, struct proc *p); 79 int (*fo_poll) (struct file *fp, int events, 80 struct proc *p); 81 int (*fo_stat) (struct file *fp, struct stat *sp, 82 struct proc *p); 83 int (*fo_close) (struct file *fp, struct proc *p); 84 } *f_ops; 85 off_t f_offset; 86 caddr_t f_data; /* descriptor data, e.g. vnode/socket */ 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) (((fp)->f_iflags & \ 93 (FIF_WANTCLOSE|FIF_LARVAL)) == 0) 94 95 #define FILE_SET_MATURE(fp) \ 96 do { \ 97 (fp)->f_iflags &= ~FIF_LARVAL; \ 98 } while (/*CONSTCOND*/0) 99 100 #ifdef DIAGNOSTIC 101 #define FILE_USE_CHECK(fp, str) \ 102 do { \ 103 if ((fp)->f_usecount < 0) \ 104 panic(str); \ 105 } while (/* CONSTCOND */ 0) 106 #else 107 #define FILE_USE_CHECK(fp, str) /* nothing */ 108 #endif 109 110 #define FILE_USE(fp) \ 111 do { \ 112 (fp)->f_usecount++; \ 113 FILE_USE_CHECK((fp), "f_usecount overflow"); \ 114 } while (/* CONSTCOND */ 0) 115 116 #define FILE_UNUSE(fp, p) \ 117 do { \ 118 if ((fp)->f_iflags & FIF_WANTCLOSE) { \ 119 /* Will drop usecount */ \ 120 (void) closef((fp), (p)); \ 121 } else { \ 122 (fp)->f_usecount--; \ 123 FILE_USE_CHECK((fp), "f_usecount underflow"); \ 124 } \ 125 } while (/* CONSTCOND */ 0) 126 127 /* 128 * Flags for fo_read and fo_write. 129 */ 130 #define FOF_UPDATE_OFFSET 0x01 /* update the file offset */ 131 132 LIST_HEAD(filelist, file); 133 extern struct filelist filehead; /* head of list of open files */ 134 extern int maxfiles; /* kernel limit on # of open files */ 135 extern int nfiles; /* actual number of open files */ 136 137 extern struct fileops vnops; /* vnode operations for files */ 138 139 int dofileread(struct proc *, int, struct file *, void *, size_t, 140 off_t *, int, register_t *); 141 int dofilewrite(struct proc *, int, struct file *, const void *, 142 size_t, off_t *, int, register_t *); 143 144 int dofilereadv(struct proc *, int, struct file *, 145 const struct iovec *, int, off_t *, int, register_t *); 146 int dofilewritev(struct proc *, int, struct file *, 147 const struct iovec *, int, off_t *, int, register_t *); 148 149 void finit(void); 150 151 #endif /* _KERNEL */ 152 153 #endif /* _SYS_FILE_H_ */ 154