FreeBSD/Linux Kernel Cross Reference
sys/sys/filedesc.h
1 /* $NetBSD: filedesc.h,v 1.31 2003/10/30 07:27:02 provos 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 /*
38 * This structure is used for the management of descriptors. It may be
39 * shared by multiple processes.
40 *
41 * A process is initially started out with NDFILE descriptors stored within
42 * this structure, selected to be enough for typical applications based on
43 * the historical limit of 20 open files (and the usage of descriptors by
44 * shells). If these descriptors are exhausted, a larger descriptor table
45 * may be allocated, up to a process' resource limit; the internal arrays
46 * are then unused. The initial expansion is set to NDEXTENT; each time
47 * it runs out, it is doubled until the resource limit is reached. NDEXTENT
48 * should be selected to be the biggest multiple of OFILESIZE (see below)
49 * that will fit in a power-of-two sized piece of memory.
50 */
51 #define NDFILE 20
52 #define NDEXTENT 50 /* 250 bytes in 256-byte alloc */
53 #define NDENTRIES 32 /* 32 fds per entry */
54 #define NDENTRYMASK (NDENTRIES - 1)
55 #define NDENTRYSHIFT 5 /* bits per entry */
56 #define NDLOSLOTS(x) (((x) + NDENTRIES - 1) >> NDENTRYSHIFT)
57 #define NDHISLOTS(x) ((NDLOSLOTS(x) + NDENTRIES - 1) >> 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 int fd_nfiles; /* number of open files allocated */
63 uint32_t *fd_himap; /* each bit points to 32 fds */
64 uint32_t *fd_lomap; /* bitmap of free fds */
65 int fd_lastfile; /* high-water mark of fd_ofiles */
66 int fd_freefile; /* approx. next free file */
67 int fd_refcnt; /* reference count */
68
69 int fd_knlistsize; /* size of fd_knlist */
70 struct klist *fd_knlist; /*
71 * list of attached fd knotes,
72 * indexed by fd number
73 */
74 u_long fd_knhashmask; /* size of fd_knhash */
75 struct klist *fd_knhash; /*
76 * hash table for attached
77 * non-fd knotes
78 */
79 };
80
81 struct cwdinfo {
82 struct vnode *cwdi_cdir; /* current directory */
83 struct vnode *cwdi_rdir; /* root directory */
84 u_short cwdi_cmask; /* mask for file creation */
85 u_short cwdi_refcnt; /* reference count */
86 };
87
88
89 /*
90 * Basic allocation of descriptors:
91 * one of the above, plus arrays for NDFILE descriptors.
92 */
93 struct filedesc0 {
94 struct filedesc fd_fd;
95 /*
96 * These arrays are used when the number of open files is
97 * <= NDFILE, and are then pointed to by the pointers above.
98 */
99 struct file *fd_dfiles[NDFILE];
100 char fd_dfileflags[NDFILE];
101 /*
102 * These arrays are used when the number of open files is
103 * <= 1024, and are then pointed to by the pointers above.
104 */
105 uint32_t fd_dhimap[NDENTRIES >> NDENTRYSHIFT];
106 uint32_t fd_dlomap[NDENTRIES];
107 };
108
109 /*
110 * Per-process open flags.
111 */
112 #define UF_EXCLOSE 0x01 /* auto-close on exec */
113
114 /*
115 * Storage required per open file descriptor.
116 */
117 #define OFILESIZE (sizeof(struct file *) + sizeof(char))
118
119 #ifdef _KERNEL
120 /*
121 * Kernel global variables and routines.
122 */
123 int dupfdopen(struct proc *, int, int, int, int);
124 int fdalloc(struct proc *, int, int *);
125 void fdexpand(struct proc *);
126 int fdavail(struct proc *, int);
127 int falloc(struct proc *, struct file **, int *);
128 void ffree(struct file *);
129 struct filedesc *fdcopy(struct proc *);
130 struct filedesc *fdinit(struct proc *);
131 void fdshare(struct proc *, struct proc *);
132 void fdunshare(struct proc *);
133 void fdinit1(struct filedesc0 *);
134 void fdclear(struct proc *);
135 void fdfree(struct proc *);
136 void fdremove(struct filedesc *, int);
137 int fdrelease(struct proc *, int);
138 void fdcloseexec(struct proc *);
139 int fdcheckstd(struct proc *);
140
141 struct file *fd_getfile(struct filedesc *, int);
142
143 struct cwdinfo *cwdinit(struct proc *);
144 void cwdshare(struct proc *, struct proc *);
145 void cwdunshare(struct proc *);
146 void cwdfree(struct proc *);
147 #define GETCWD_CHECK_ACCESS 0x0001
148 int getcwd_common(struct vnode *, struct vnode *, char **, char *, int,
149 int, struct proc *);
150
151 int closef(struct file *, struct proc *);
152 int getsock(struct filedesc *, int, struct file **);
153 #endif /* _KERNEL */
154
155 #endif /* !_SYS_FILEDESC_H_ */
Cache object: ad999899faabb9f3ddeb2bf72a1b2c97
|