1 /* $NetBSD: filedesc.h,v 1.51 2008/07/02 16:45:20 matt Exp $ */ 2 3 /*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * Copyright (c) 1990, 1993 31 * The Regents of the University of California. All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 3. Neither the name of the University nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 * 57 * @(#)filedesc.h 8.1 (Berkeley) 6/2/93 58 */ 59 60 #ifndef _SYS_FILEDESC_H_ 61 #define _SYS_FILEDESC_H_ 62 63 #include <sys/param.h> 64 #include <sys/queue.h> 65 #include <sys/mutex.h> 66 #include <sys/rwlock.h> 67 #include <sys/condvar.h> 68 69 /* 70 * This structure is used for the management of descriptors. It may be 71 * shared by multiple processes. 72 * 73 * A process is initially started out with NDFILE descriptors stored within 74 * this structure, selected to be enough for typical applications based on 75 * the historical limit of 20 open files (and the usage of descriptors by 76 * shells). If these descriptors are exhausted, a larger descriptor table 77 * may be allocated, up to a process' resource limit; the internal arrays 78 * are then unused. The initial expansion is set to NDEXTENT; each time 79 * it runs out, it is doubled until the resource limit is reached. NDEXTENT 80 * should be selected to be the biggest multiple of OFILESIZE (see below) 81 * that will fit in a power-of-two sized piece of memory. 82 */ 83 #define NDFILE 20 84 #define NDEXTENT 50 /* 250 bytes in 256-byte alloc */ 85 #define NDENTRIES 32 /* 32 fds per entry */ 86 #define NDENTRYMASK (NDENTRIES - 1) 87 #define NDENTRYSHIFT 5 /* bits per entry */ 88 #define NDLOSLOTS(x) (((x) + NDENTRIES - 1) >> NDENTRYSHIFT) 89 #define NDHISLOTS(x) ((NDLOSLOTS(x) + NDENTRIES - 1) >> NDENTRYSHIFT) 90 #define NDFDFILE 3 /* first 3 descriptors are free */ 91 92 /* 93 * locks: 94 * 95 * : unlocked 96 * d filedesc::fd_lock 97 * f fdfile::ff_lock, but stable if reference held 98 */ 99 typedef struct fdfile { 100 kmutex_t ff_lock; /* :: lock on structure */ 101 bool ff_exclose; /* :: close on exec flag */ 102 bool ff_allocated; /* d: descriptor slot is allocated */ 103 u_int ff_refcnt; /* f: reference count on structure */ 104 struct file *ff_file; /* f: pointer to file if open */ 105 SLIST_HEAD(,knote) ff_knlist; /* f: knotes attached to this fd */ 106 kcondvar_t ff_closing; /* f: notifier for close */ 107 } fdfile_t; 108 109 /* Reference count */ 110 #define FR_CLOSING (0x80000000) /* closing: must interlock */ 111 #define FR_MASK (~FR_CLOSING) /* reference count */ 112 113 typedef struct filedesc { 114 /* 115 * Built-in fdfile_t records first, since they have strict 116 * alignment requirements. 117 */ 118 uint8_t fd_dfdfile[NDFDFILE][CACHE_LINE_SIZE]; 119 /* 120 * All of the remaining fields are locked by fd_lock. 121 */ 122 kmutex_t fd_lock; /* lock on structure */ 123 fdfile_t **fd_ofiles; /* file structures for open files */ 124 uint32_t *fd_himap; /* each bit points to 32 fds */ 125 uint32_t *fd_lomap; /* bitmap of free fds */ 126 void *fd_discard; /* old fd_ofiles tables to discard */ 127 struct klist *fd_knhash; /* hash of attached non-fd knotes */ 128 int fd_lastkqfile; /* max descriptor for kqueue */ 129 int fd_lastfile; /* high-water mark of fd_ofiles */ 130 int fd_refcnt; /* reference count */ 131 int fd_nfiles; /* number of open files allocated */ 132 u_long fd_knhashmask; /* size of fd_knhash */ 133 #define fd_startzero fd_freefile /* area to zero on return to cache */ 134 int fd_freefile; /* approx. next free file */ 135 int fd_nused; /* number of slots in use */ 136 bool fd_exclose; /* non-zero if >0 fd with EXCLOSE */ 137 /* 138 * These arrays are used when the number of open files is 139 * <= NDFILE, and are then pointed to by the pointers above. 140 */ 141 fdfile_t *fd_dfiles[NDFILE]; 142 /* 143 * These arrays are used when the number of open files is 144 * <= 1024, and are then pointed to by the pointers above. 145 */ 146 uint32_t fd_dhimap[NDENTRIES >> NDENTRYSHIFT]; 147 uint32_t fd_dlomap[NDENTRIES]; 148 } filedesc_t; 149 150 typedef struct cwdinfo { 151 struct vnode *cwdi_cdir; /* current directory */ 152 struct vnode *cwdi_rdir; /* root directory */ 153 struct vnode *cwdi_edir; /* emulation root (if known) */ 154 krwlock_t cwdi_lock; /* lock on entire struct */ 155 u_short cwdi_cmask; /* mask for file creation */ 156 u_int cwdi_refcnt; /* reference count */ 157 } cwdinfo_t; 158 159 #ifdef _KERNEL 160 161 struct fileops; 162 struct socket; 163 struct proc; 164 165 /* 166 * Kernel global variables and routines. 167 */ 168 void fd_sys_init(void); 169 int fd_dupopen(int, int *, int, int); 170 int fd_alloc(struct proc *, int, int *); 171 void fd_tryexpand(struct proc *); 172 int fd_allocfile(file_t **, int *); 173 void fd_affix(struct proc *, file_t *, unsigned); 174 void fd_abort(struct proc *, file_t *, unsigned); 175 filedesc_t *fd_copy(void); 176 filedesc_t *fd_init(filedesc_t *); 177 void fd_share(proc_t *); 178 void fd_clear(void); 179 void fd_free(void); 180 void fd_remove(filedesc_t *, unsigned); 181 void fd_closeexec(void); 182 int fd_checkstd(void); 183 file_t *fd_getfile(unsigned); 184 file_t *fd_getfile2(proc_t *, unsigned); 185 void fd_putfile(unsigned); 186 int fd_getvnode(unsigned, file_t **); 187 int fd_getsock(unsigned, struct socket **); 188 void fd_putvnode(unsigned); 189 void fd_putsock(unsigned); 190 int fd_close(unsigned); 191 void fd_used(filedesc_t *, unsigned); 192 void fd_unused(filedesc_t *, unsigned); 193 bool fd_isused(filedesc_t *, unsigned); 194 int fd_dup(file_t *, int, int *, bool); 195 int fd_dup2(file_t *, unsigned); 196 int fd_clone(file_t *, unsigned, int, const struct fileops *, void *); 197 198 struct cwdinfo *cwdinit(void); 199 void cwdshare(proc_t *); 200 void cwdfree(struct cwdinfo *); 201 #define GETCWD_CHECK_ACCESS 0x0001 202 int getcwd_common(struct vnode *, struct vnode *, char **, char *, int, 203 int, struct lwp *); 204 int vnode_to_path(char *, size_t, struct vnode *, struct lwp *, 205 struct proc *); 206 207 void ffree(file_t *); 208 int closef(file_t *); 209 file_t *fgetdummy(void); 210 void fputdummy(file_t *); 211 212 struct stat; 213 int do_sys_fstat(int, struct stat *); 214 struct flock; 215 int do_fcntl_lock(int, int, struct flock *); 216 int do_posix_fadvise(int, off_t, off_t, int); 217 218 extern kmutex_t filelist_lock; 219 220 #endif /* _KERNEL */ 221 222 #endif /* !_SYS_FILEDESC_H_ */ 223