1 /* $OpenBSD: filedesc.h,v 1.19 2004/07/22 06:11:10 tedu Exp $ */ 2 /* $NetBSD: filedesc.h,v 1.14 1996/04/09 20:55:28 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 1990, 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 * @(#)filedesc.h 8.1 (Berkeley) 6/2/93 33 */ 34 35 #include <sys/rwlock.h> 36 /* 37 * This structure is used for the management of descriptors. It may be 38 * shared by multiple processes. 39 * 40 * A process is initially started out with NDFILE descriptors stored within 41 * this structure, selected to be enough for typical applications based on 42 * the historical limit of 20 open files (and the usage of descriptors by 43 * shells). If these descriptors are exhausted, a larger descriptor table 44 * may be allocated, up to a process' resource limit; the internal arrays 45 * are then unused. The initial expansion is set to NDEXTENT; each time 46 * it runs out, it is doubled until the resource limit is reached. NDEXTENT 47 * should be selected to be the biggest multiple of OFILESIZE (see below) 48 * that will fit in a power-of-two sized piece of memory. 49 */ 50 #define NDFILE 20 51 #define NDEXTENT 50 /* 250 bytes in 256-byte alloc. */ 52 #define NDENTRIES 32 /* 32 fds per entry */ 53 #define NDENTRYMASK (NDENTRIES - 1) 54 #define NDENTRYSHIFT 5 /* bits per entry */ 55 #define NDREDUCE(x) (((x) + NDENTRIES - 1) >> NDENTRYSHIFT) 56 #define NDHISLOTS(x) (NDREDUCE(NDREDUCE(x))) 57 #define NDLOSLOTS(x) (NDHISLOTS(x) << NDENTRYSHIFT) 58 59 struct filedesc { 60 struct file **fd_ofiles; /* file structures for open files */ 61 char *fd_ofileflags; /* per-process open file flags */ 62 struct vnode *fd_cdir; /* current directory */ 63 struct vnode *fd_rdir; /* root directory */ 64 int fd_nfiles; /* number of open files allocated */ 65 u_int *fd_himap; /* each bit points to 32 fds */ 66 u_int *fd_lomap; /* bitmap of free fds */ 67 int fd_lastfile; /* high-water mark of fd_ofiles */ 68 int fd_freefile; /* approx. next free file */ 69 u_short fd_cmask; /* mask for file creation */ 70 u_short fd_refcnt; /* reference count */ 71 struct rwlock fd_lock; /* lock for the file descs */ 72 73 int fd_knlistsize; /* size of knlist */ 74 struct klist *fd_knlist; /* list of attached knotes */ 75 u_long fd_knhashmask; /* size of knhash */ 76 struct klist *fd_knhash; /* hash table for attached knotes */ 77 }; 78 79 /* 80 * Basic allocation of descriptors: 81 * one of the above, plus arrays for NDFILE descriptors. 82 */ 83 struct filedesc0 { 84 struct filedesc fd_fd; 85 /* 86 * These arrays are used when the number of open files is 87 * <= NDFILE, and are then pointed to by the pointers above. 88 */ 89 struct file *fd_dfiles[NDFILE]; 90 char fd_dfileflags[NDFILE]; 91 /* 92 * There arrays are used when the number of open files is 93 * <= 1024, and are then pointed to by the pointers above. 94 */ 95 u_int fd_dhimap[NDENTRIES >> NDENTRYSHIFT]; 96 u_int fd_dlomap[NDENTRIES]; 97 }; 98 99 /* 100 * Per-process open flags. 101 */ 102 #define UF_EXCLOSE 0x01 /* auto-close on exec */ 103 104 /* 105 * Storage required per open file descriptor. 106 */ 107 #define OFILESIZE (sizeof(struct file *) + sizeof(char)) 108 109 #ifdef _KERNEL 110 /* 111 * Kernel global variables and routines. 112 */ 113 void filedesc_init(void); 114 int dupfdopen(struct filedesc *fdp, int indx, int dfd, int mode, 115 int error); 116 int fdalloc(struct proc *p, int want, int *result); 117 void fdexpand(struct proc *); 118 int falloc(struct proc *p, struct file **resultfp, int *resultfd); 119 struct filedesc *fdinit(struct proc *p); 120 struct filedesc *fdshare(struct proc *p); 121 struct filedesc *fdcopy(struct proc *p); 122 void fdfree(struct proc *p); 123 int fdrelease(struct proc *p, int); 124 void fdremove(struct filedesc *, int); 125 void fdcloseexec(struct proc *); 126 struct file *fd_getfile(struct filedesc *, int fd); 127 128 int closef(struct file *, struct proc *); 129 int getsock(struct filedesc *, int, struct file **); 130 131 #define fdplock(fdp) rw_enter_write(&(fdp)->fd_lock) 132 #define fdpunlock(fdp) rw_exit_write(&(fdp)->fd_lock) 133 #endif