1 /* 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)filedesc.h 8.1 (Berkeley) 06/02/93 8 */ 9 10 /* 11 * This structure is used for the management of descriptors. It may be 12 * shared by multiple processes. 13 * 14 * A process is initially started out with NDFILE descriptors stored within 15 * this structure, selected to be enough for typical applications based on 16 * the historical limit of 20 open files (and the usage of descriptors by 17 * shells). If these descriptors are exhausted, a larger descriptor table 18 * may be allocated, up to a process' resource limit; the internal arrays 19 * are then unused. The initial expansion is set to NDEXTENT; each time 20 * it runs out, it is doubled until the resource limit is reached. NDEXTENT 21 * should be selected to be the biggest multiple of OFILESIZE (see below) 22 * that will fit in a power-of-two sized piece of memory. 23 */ 24 #define NDFILE 20 25 #define NDEXTENT 50 /* 250 bytes in 256-byte alloc. */ 26 27 struct filedesc { 28 struct file **fd_ofiles; /* file structures for open files */ 29 char *fd_ofileflags; /* per-process open file flags */ 30 struct vnode *fd_cdir; /* current directory */ 31 struct vnode *fd_rdir; /* root directory */ 32 int fd_nfiles; /* number of open files allocated */ 33 u_short fd_lastfile; /* high-water mark of fd_ofiles */ 34 u_short fd_freefile; /* approx. next free file */ 35 u_short fd_cmask; /* mask for file creation */ 36 u_short fd_refcnt; /* reference count */ 37 }; 38 39 /* 40 * Basic allocation of descriptors: 41 * one of the above, plus arrays for NDFILE descriptors. 42 */ 43 struct filedesc0 { 44 struct filedesc fd_fd; 45 /* 46 * These arrays are used when the number of open files is 47 * <= NDFILE, and are then pointed to by the pointers above. 48 */ 49 struct file *fd_dfiles[NDFILE]; 50 char fd_dfileflags[NDFILE]; 51 }; 52 53 /* 54 * Per-process open flags. 55 */ 56 #define UF_EXCLOSE 0x01 /* auto-close on exec */ 57 #define UF_MAPPED 0x02 /* mapped from device */ 58 59 /* 60 * Storage required per open file descriptor. 61 */ 62 #define OFILESIZE (sizeof(struct file *) + sizeof(char)) 63 64 #ifdef KERNEL 65 /* 66 * Kernel global variables and routines. 67 */ 68 int fdalloc __P((struct proc *p, int want, int *result)); 69 int fdavail __P((struct proc *p, int n)); 70 int falloc __P((struct proc *p, struct file **resultfp, int *resultfd)); 71 struct filedesc *fdcopy __P((struct proc *p)); 72 void fdfree __P((struct proc *p)); 73 #endif 74