1 /* $NetBSD: filedesc.h,v 1.35 2006/03/07 17:13:53 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 1990, 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)filedesc.h 8.1 (Berkeley) 6/2/93 32 */ 33 34 #ifndef _SYS_FILEDESC_H_ 35 #define _SYS_FILEDESC_H_ 36 37 #include <sys/lock.h> 38 39 /* 40 * This structure is used for the management of descriptors. It may be 41 * shared by multiple processes. 42 * 43 * A process is initially started out with NDFILE descriptors stored within 44 * this structure, selected to be enough for typical applications based on 45 * the historical limit of 20 open files (and the usage of descriptors by 46 * shells). If these descriptors are exhausted, a larger descriptor table 47 * may be allocated, up to a process' resource limit; the internal arrays 48 * are then unused. The initial expansion is set to NDEXTENT; each time 49 * it runs out, it is doubled until the resource limit is reached. NDEXTENT 50 * should be selected to be the biggest multiple of OFILESIZE (see below) 51 * that will fit in a power-of-two sized piece of memory. 52 */ 53 #define NDFILE 20 54 #define NDEXTENT 50 /* 250 bytes in 256-byte alloc */ 55 #define NDENTRIES 32 /* 32 fds per entry */ 56 #define NDENTRYMASK (NDENTRIES - 1) 57 #define NDENTRYSHIFT 5 /* bits per entry */ 58 #define NDLOSLOTS(x) (((x) + NDENTRIES - 1) >> NDENTRYSHIFT) 59 #define NDHISLOTS(x) ((NDLOSLOTS(x) + NDENTRIES - 1) >> NDENTRYSHIFT) 60 61 struct filedesc { 62 struct file **fd_ofiles; /* file structures for open files */ 63 char *fd_ofileflags; /* per-process open file flags */ 64 int fd_nfiles; /* number of open files allocated */ 65 uint32_t *fd_himap; /* each bit points to 32 fds */ 66 uint32_t *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 int fd_refcnt; /* reference count */ 70 71 int fd_knlistsize; /* size of fd_knlist */ 72 struct klist *fd_knlist; /* 73 * list of attached fd knotes, 74 * indexed by fd number 75 */ 76 u_long fd_knhashmask; /* size of fd_knhash */ 77 struct klist *fd_knhash; /* 78 * hash table for attached 79 * non-fd knotes 80 */ 81 struct simplelock fd_slock; /* mutex. Note on locking order: 82 * acquire this lock first when 83 * also locking an associated 84 * `struct file' lock. 85 */ 86 }; 87 88 struct cwdinfo { 89 struct vnode *cwdi_cdir; /* current directory */ 90 struct vnode *cwdi_rdir; /* root directory */ 91 u_short cwdi_cmask; /* mask for file creation */ 92 u_short cwdi_refcnt; /* reference count */ 93 struct simplelock cwdi_slock; /* mutex */ 94 }; 95 96 97 /* 98 * Basic allocation of descriptors: 99 * one of the above, plus arrays for NDFILE descriptors. 100 */ 101 struct filedesc0 { 102 struct filedesc fd_fd; 103 /* 104 * These arrays are used when the number of open files is 105 * <= NDFILE, and are then pointed to by the pointers above. 106 */ 107 struct file *fd_dfiles[NDFILE]; 108 char fd_dfileflags[NDFILE]; 109 /* 110 * These arrays are used when the number of open files is 111 * <= 1024, and are then pointed to by the pointers above. 112 */ 113 uint32_t fd_dhimap[NDENTRIES >> NDENTRYSHIFT]; 114 uint32_t fd_dlomap[NDENTRIES]; 115 }; 116 117 /* 118 * Per-process open flags. 119 */ 120 #define UF_EXCLOSE 0x01 /* auto-close on exec */ 121 122 /* 123 * Storage required per open file descriptor. 124 */ 125 #define OFILESIZE (sizeof(struct file *) + sizeof(char)) 126 127 #ifdef _KERNEL 128 /* 129 * Kernel global variables and routines. 130 */ 131 int dupfdopen(struct lwp *, int, int, int, int); 132 int fdalloc(struct proc *, int, int *); 133 void fdexpand(struct proc *); 134 int falloc(struct proc *, struct file **, int *); 135 void ffree(struct file *); 136 struct filedesc *fdcopy(struct proc *); 137 struct filedesc *fdinit(struct proc *); 138 void fdshare(struct proc *, struct proc *); 139 void fdunshare(struct lwp *); 140 void fdinit1(struct filedesc0 *); 141 void fdclear(struct lwp *); 142 void fdfree(struct lwp *); 143 void fdremove(struct filedesc *, int); 144 int fdrelease(struct lwp *, int); 145 void fdcloseexec(struct lwp *); 146 int fdcheckstd(struct lwp *); 147 148 struct file *fd_getfile(struct filedesc *, int); 149 150 struct cwdinfo *cwdinit(struct proc *); 151 void cwdshare(struct proc *, struct proc *); 152 void cwdunshare(struct proc *); 153 void cwdfree(struct cwdinfo *); 154 #define GETCWD_CHECK_ACCESS 0x0001 155 int getcwd_common(struct vnode *, struct vnode *, char **, char *, int, 156 int, struct lwp *); 157 158 int closef(struct file *, struct lwp *); 159 int getsock(struct filedesc *, int, struct file **); 160 #endif /* _KERNEL */ 161 162 #endif /* !_SYS_FILEDESC_H_ */ 163