kern_descrip.c revision 1.197 1 1.197 yamt /* $NetBSD: kern_descrip.c,v 1.197 2009/06/08 00:19:56 yamt Exp $ */
2 1.173 ad
3 1.173 ad /*-
4 1.190 ad * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
5 1.173 ad * All rights reserved.
6 1.173 ad *
7 1.190 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.190 ad * by Andrew Doran.
9 1.190 ad *
10 1.173 ad * Redistribution and use in source and binary forms, with or without
11 1.173 ad * modification, are permitted provided that the following conditions
12 1.173 ad * are met:
13 1.173 ad * 1. Redistributions of source code must retain the above copyright
14 1.173 ad * notice, this list of conditions and the following disclaimer.
15 1.173 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.173 ad * notice, this list of conditions and the following disclaimer in the
17 1.173 ad * documentation and/or other materials provided with the distribution.
18 1.173 ad *
19 1.173 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.173 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.173 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.173 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.173 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.173 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.173 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.173 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.173 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.173 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.173 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.173 ad */
31 1.22 cgd
32 1.16 cgd /*
33 1.17 cgd * Copyright (c) 1982, 1986, 1989, 1991, 1993
34 1.17 cgd * The Regents of the University of California. All rights reserved.
35 1.16 cgd * (c) UNIX System Laboratories, Inc.
36 1.16 cgd * All or some portions of this file are derived from material licensed
37 1.16 cgd * to the University of California by American Telephone and Telegraph
38 1.16 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 1.16 cgd * the permission of UNIX System Laboratories, Inc.
40 1.16 cgd *
41 1.16 cgd * Redistribution and use in source and binary forms, with or without
42 1.16 cgd * modification, are permitted provided that the following conditions
43 1.16 cgd * are met:
44 1.16 cgd * 1. Redistributions of source code must retain the above copyright
45 1.16 cgd * notice, this list of conditions and the following disclaimer.
46 1.16 cgd * 2. Redistributions in binary form must reproduce the above copyright
47 1.16 cgd * notice, this list of conditions and the following disclaimer in the
48 1.16 cgd * documentation and/or other materials provided with the distribution.
49 1.111 agc * 3. Neither the name of the University nor the names of its contributors
50 1.16 cgd * may be used to endorse or promote products derived from this software
51 1.16 cgd * without specific prior written permission.
52 1.16 cgd *
53 1.16 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 1.16 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 1.16 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 1.16 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 1.16 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 1.16 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 1.16 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 1.16 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 1.16 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 1.16 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 1.16 cgd * SUCH DAMAGE.
64 1.16 cgd *
65 1.51 fvdl * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
66 1.16 cgd */
67 1.81 lukem
68 1.173 ad /*
69 1.173 ad * File descriptor management.
70 1.173 ad */
71 1.173 ad
72 1.81 lukem #include <sys/cdefs.h>
73 1.197 yamt __KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.197 2009/06/08 00:19:56 yamt Exp $");
74 1.50 mrg
75 1.16 cgd #include <sys/param.h>
76 1.16 cgd #include <sys/systm.h>
77 1.16 cgd #include <sys/filedesc.h>
78 1.16 cgd #include <sys/kernel.h>
79 1.16 cgd #include <sys/proc.h>
80 1.16 cgd #include <sys/file.h>
81 1.16 cgd #include <sys/socket.h>
82 1.16 cgd #include <sys/socketvar.h>
83 1.16 cgd #include <sys/stat.h>
84 1.16 cgd #include <sys/ioctl.h>
85 1.16 cgd #include <sys/fcntl.h>
86 1.55 thorpej #include <sys/pool.h>
87 1.17 cgd #include <sys/unistd.h>
88 1.16 cgd #include <sys/resourcevar.h>
89 1.42 christos #include <sys/conf.h>
90 1.96 jdolecek #include <sys/event.h>
91 1.143 elad #include <sys/kauth.h>
92 1.163 ad #include <sys/atomic.h>
93 1.25 cgd #include <sys/syscallargs.h>
94 1.176 ad #include <sys/cpu.h>
95 1.184 pooka #include <sys/kmem.h>
96 1.184 pooka #include <sys/vnode.h>
97 1.38 christos
98 1.167 ad static int file_ctor(void *, void *, int);
99 1.167 ad static void file_dtor(void *, void *);
100 1.173 ad static int fdfile_ctor(void *, void *, int);
101 1.173 ad static void fdfile_dtor(void *, void *);
102 1.173 ad static int filedesc_ctor(void *, void *, int);
103 1.173 ad static void filedesc_dtor(void *, void *);
104 1.173 ad static int filedescopen(dev_t, int, int, lwp_t *);
105 1.162 ad
106 1.173 ad kmutex_t filelist_lock; /* lock on filehead */
107 1.72 lukem struct filelist filehead; /* head of list of open files */
108 1.167 ad u_int nfiles; /* actual number of open files */
109 1.161 ad
110 1.173 ad static pool_cache_t filedesc_cache;
111 1.162 ad static pool_cache_t file_cache;
112 1.173 ad static pool_cache_t fdfile_cache;
113 1.101 thorpej
114 1.173 ad const struct cdevsw filedesc_cdevsw = {
115 1.173 ad filedescopen, noclose, noread, nowrite, noioctl,
116 1.173 ad nostop, notty, nopoll, nommap, nokqfilter, D_OTHER | D_MPSAFE,
117 1.173 ad };
118 1.173 ad
119 1.173 ad /* For ease of reading. */
120 1.173 ad __strong_alias(fd_putvnode,fd_putfile)
121 1.173 ad __strong_alias(fd_putsock,fd_putfile)
122 1.173 ad
123 1.173 ad /*
124 1.173 ad * Initialize the descriptor system.
125 1.173 ad */
126 1.173 ad void
127 1.173 ad fd_sys_init(void)
128 1.173 ad {
129 1.173 ad
130 1.173 ad mutex_init(&filelist_lock, MUTEX_DEFAULT, IPL_NONE);
131 1.173 ad
132 1.174 ad file_cache = pool_cache_init(sizeof(file_t), coherency_unit, 0,
133 1.173 ad 0, "file", NULL, IPL_NONE, file_ctor, file_dtor, NULL);
134 1.173 ad KASSERT(file_cache != NULL);
135 1.173 ad
136 1.174 ad fdfile_cache = pool_cache_init(sizeof(fdfile_t), coherency_unit, 0,
137 1.173 ad PR_LARGECACHE, "fdfile", NULL, IPL_NONE, fdfile_ctor, fdfile_dtor,
138 1.173 ad NULL);
139 1.173 ad KASSERT(fdfile_cache != NULL);
140 1.173 ad
141 1.174 ad filedesc_cache = pool_cache_init(sizeof(filedesc_t), coherency_unit,
142 1.173 ad 0, 0, "filedesc", NULL, IPL_NONE, filedesc_ctor, filedesc_dtor,
143 1.173 ad NULL);
144 1.173 ad KASSERT(filedesc_cache != NULL);
145 1.173 ad }
146 1.72 lukem
147 1.192 ad static bool
148 1.192 ad fd_isused(filedesc_t *fdp, unsigned fd)
149 1.192 ad {
150 1.192 ad u_int off = fd >> NDENTRYSHIFT;
151 1.192 ad
152 1.192 ad KASSERT(fd < fdp->fd_dt->dt_nfiles);
153 1.192 ad
154 1.192 ad return (fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) != 0;
155 1.192 ad }
156 1.192 ad
157 1.192 ad /*
158 1.192 ad * Verify that the bitmaps match the descriptor table.
159 1.192 ad */
160 1.192 ad static inline void
161 1.192 ad fd_checkmaps(filedesc_t *fdp)
162 1.192 ad {
163 1.192 ad #ifdef DEBUG
164 1.192 ad fdtab_t *dt;
165 1.192 ad u_int fd;
166 1.192 ad
167 1.192 ad dt = fdp->fd_dt;
168 1.196 yamt if (fdp->fd_refcnt == -1) {
169 1.196 yamt /*
170 1.196 yamt * fd_free tears down the table without maintaining its bitmap.
171 1.196 yamt */
172 1.196 yamt return;
173 1.196 yamt }
174 1.192 ad for (fd = 0; fd < dt->dt_nfiles; fd++) {
175 1.192 ad if (fd < NDFDFILE) {
176 1.192 ad KASSERT(dt->dt_ff[fd] ==
177 1.192 ad (fdfile_t *)fdp->fd_dfdfile[fd]);
178 1.192 ad }
179 1.192 ad if (dt->dt_ff[fd] == NULL) {
180 1.192 ad KASSERT(!fd_isused(fdp, fd));
181 1.192 ad } else if (dt->dt_ff[fd]->ff_file != NULL) {
182 1.192 ad KASSERT(fd_isused(fdp, fd));
183 1.192 ad }
184 1.192 ad }
185 1.192 ad #else /* DEBUG */
186 1.192 ad /* nothing */
187 1.192 ad #endif /* DEBUG */
188 1.192 ad }
189 1.192 ad
190 1.173 ad static int
191 1.173 ad fd_next_zero(filedesc_t *fdp, uint32_t *bitmap, int want, u_int bits)
192 1.115 provos {
193 1.115 provos int i, off, maxoff;
194 1.115 provos uint32_t sub;
195 1.115 provos
196 1.173 ad KASSERT(mutex_owned(&fdp->fd_lock));
197 1.173 ad
198 1.192 ad fd_checkmaps(fdp);
199 1.192 ad
200 1.115 provos if (want > bits)
201 1.115 provos return -1;
202 1.115 provos
203 1.115 provos off = want >> NDENTRYSHIFT;
204 1.115 provos i = want & NDENTRYMASK;
205 1.115 provos if (i) {
206 1.115 provos sub = bitmap[off] | ((u_int)~0 >> (NDENTRIES - i));
207 1.115 provos if (sub != ~0)
208 1.115 provos goto found;
209 1.115 provos off++;
210 1.115 provos }
211 1.115 provos
212 1.115 provos maxoff = NDLOSLOTS(bits);
213 1.115 provos while (off < maxoff) {
214 1.115 provos if ((sub = bitmap[off]) != ~0)
215 1.115 provos goto found;
216 1.115 provos off++;
217 1.115 provos }
218 1.115 provos
219 1.115 provos return (-1);
220 1.115 provos
221 1.115 provos found:
222 1.115 provos return (off << NDENTRYSHIFT) + ffs(~sub) - 1;
223 1.115 provos }
224 1.115 provos
225 1.134 thorpej static int
226 1.173 ad fd_last_set(filedesc_t *fd, int last)
227 1.115 provos {
228 1.115 provos int off, i;
229 1.192 ad fdfile_t **ff = fd->fd_dt->dt_ff;
230 1.115 provos uint32_t *bitmap = fd->fd_lomap;
231 1.115 provos
232 1.173 ad KASSERT(mutex_owned(&fd->fd_lock));
233 1.173 ad
234 1.192 ad fd_checkmaps(fd);
235 1.192 ad
236 1.115 provos off = (last - 1) >> NDENTRYSHIFT;
237 1.115 provos
238 1.121 provos while (off >= 0 && !bitmap[off])
239 1.115 provos off--;
240 1.115 provos
241 1.115 provos if (off < 0)
242 1.128 cube return (-1);
243 1.131 perry
244 1.115 provos i = ((off + 1) << NDENTRYSHIFT) - 1;
245 1.115 provos if (i >= last)
246 1.115 provos i = last - 1;
247 1.115 provos
248 1.173 ad /* XXX should use bitmap */
249 1.192 ad while (i > 0 && (ff[i] == NULL || !ff[i]->ff_allocated))
250 1.115 provos i--;
251 1.115 provos
252 1.115 provos return (i);
253 1.115 provos }
254 1.115 provos
255 1.192 ad static inline void
256 1.173 ad fd_used(filedesc_t *fdp, unsigned fd)
257 1.27 mycroft {
258 1.115 provos u_int off = fd >> NDENTRYSHIFT;
259 1.173 ad fdfile_t *ff;
260 1.173 ad
261 1.192 ad ff = fdp->fd_dt->dt_ff[fd];
262 1.115 provos
263 1.173 ad KASSERT(mutex_owned(&fdp->fd_lock));
264 1.173 ad KASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) == 0);
265 1.173 ad KASSERT(ff != NULL);
266 1.173 ad KASSERT(ff->ff_file == NULL);
267 1.173 ad KASSERT(!ff->ff_allocated);
268 1.124 yamt
269 1.173 ad ff->ff_allocated = 1;
270 1.115 provos fdp->fd_lomap[off] |= 1 << (fd & NDENTRYMASK);
271 1.192 ad if (__predict_false(fdp->fd_lomap[off] == ~0)) {
272 1.173 ad KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
273 1.124 yamt (1 << (off & NDENTRYMASK))) == 0);
274 1.115 provos fdp->fd_himap[off >> NDENTRYSHIFT] |= 1 << (off & NDENTRYMASK);
275 1.124 yamt }
276 1.27 mycroft
277 1.173 ad if ((int)fd > fdp->fd_lastfile) {
278 1.27 mycroft fdp->fd_lastfile = fd;
279 1.173 ad }
280 1.173 ad
281 1.192 ad fd_checkmaps(fdp);
282 1.27 mycroft }
283 1.27 mycroft
284 1.192 ad static inline void
285 1.173 ad fd_unused(filedesc_t *fdp, unsigned fd)
286 1.27 mycroft {
287 1.115 provos u_int off = fd >> NDENTRYSHIFT;
288 1.173 ad fdfile_t *ff;
289 1.27 mycroft
290 1.192 ad ff = fdp->fd_dt->dt_ff[fd];
291 1.173 ad
292 1.173 ad /*
293 1.173 ad * Don't assert the lock is held here, as we may be copying
294 1.173 ad * the table during exec() and it is not needed there.
295 1.173 ad * procfs and sysctl are locked out by proc::p_reflock.
296 1.173 ad *
297 1.173 ad * KASSERT(mutex_owned(&fdp->fd_lock));
298 1.173 ad */
299 1.173 ad KASSERT(ff != NULL);
300 1.173 ad KASSERT(ff->ff_file == NULL);
301 1.173 ad KASSERT(ff->ff_allocated);
302 1.173 ad
303 1.173 ad if (fd < fdp->fd_freefile) {
304 1.27 mycroft fdp->fd_freefile = fd;
305 1.173 ad }
306 1.115 provos
307 1.124 yamt if (fdp->fd_lomap[off] == ~0) {
308 1.173 ad KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
309 1.124 yamt (1 << (off & NDENTRYMASK))) != 0);
310 1.124 yamt fdp->fd_himap[off >> NDENTRYSHIFT] &=
311 1.124 yamt ~(1 << (off & NDENTRYMASK));
312 1.124 yamt }
313 1.173 ad KASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) != 0);
314 1.115 provos fdp->fd_lomap[off] &= ~(1 << (fd & NDENTRYMASK));
315 1.173 ad ff->ff_allocated = 0;
316 1.115 provos
317 1.173 ad KASSERT(fd <= fdp->fd_lastfile);
318 1.173 ad if (fd == fdp->fd_lastfile) {
319 1.173 ad fdp->fd_lastfile = fd_last_set(fdp, fd);
320 1.173 ad }
321 1.192 ad fd_checkmaps(fdp);
322 1.77 thorpej }
323 1.77 thorpej
324 1.16 cgd /*
325 1.173 ad * Look up the file structure corresponding to a file descriptor
326 1.173 ad * and return the file, holding a reference on the descriptor.
327 1.134 thorpej */
328 1.173 ad inline file_t *
329 1.173 ad fd_getfile(unsigned fd)
330 1.134 thorpej {
331 1.173 ad filedesc_t *fdp;
332 1.173 ad fdfile_t *ff;
333 1.173 ad file_t *fp;
334 1.192 ad fdtab_t *dt;
335 1.134 thorpej
336 1.134 thorpej /*
337 1.173 ad * Look up the fdfile structure representing this descriptor.
338 1.192 ad * We are doing this unlocked. See fd_tryexpand().
339 1.134 thorpej */
340 1.192 ad fdp = curlwp->l_fd;
341 1.192 ad dt = fdp->fd_dt;
342 1.192 ad if (__predict_false(fd >= dt->dt_nfiles)) {
343 1.173 ad return NULL;
344 1.173 ad }
345 1.192 ad ff = dt->dt_ff[fd];
346 1.173 ad KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
347 1.173 ad if (__predict_false(ff == NULL)) {
348 1.173 ad return NULL;
349 1.173 ad }
350 1.134 thorpej
351 1.191 ad /* Now get a reference to the descriptor. */
352 1.191 ad if (fdp->fd_refcnt == 1) {
353 1.191 ad /*
354 1.191 ad * Single threaded: don't need to worry about concurrent
355 1.191 ad * access (other than earlier calls to kqueue, which may
356 1.191 ad * hold a reference to the descriptor).
357 1.191 ad */
358 1.191 ad ff->ff_refcnt++;
359 1.191 ad } else {
360 1.191 ad /*
361 1.192 ad * Multi threaded: issue a memory barrier to ensure that we
362 1.192 ad * acquire the file pointer _after_ adding a reference. If
363 1.192 ad * no memory barrier, we could fetch a stale pointer.
364 1.191 ad */
365 1.191 ad atomic_inc_uint(&ff->ff_refcnt);
366 1.173 ad #ifndef __HAVE_ATOMIC_AS_MEMBAR
367 1.191 ad membar_enter();
368 1.173 ad #endif
369 1.191 ad }
370 1.134 thorpej
371 1.173 ad /*
372 1.173 ad * If the file is not open or is being closed then put the
373 1.173 ad * reference back.
374 1.173 ad */
375 1.173 ad fp = ff->ff_file;
376 1.173 ad if (__predict_true(fp != NULL)) {
377 1.173 ad return fp;
378 1.134 thorpej }
379 1.173 ad fd_putfile(fd);
380 1.173 ad return NULL;
381 1.134 thorpej }
382 1.134 thorpej
383 1.134 thorpej /*
384 1.173 ad * Release a reference to a file descriptor acquired with fd_getfile().
385 1.161 ad */
386 1.161 ad void
387 1.173 ad fd_putfile(unsigned fd)
388 1.161 ad {
389 1.173 ad filedesc_t *fdp;
390 1.173 ad fdfile_t *ff;
391 1.173 ad u_int u, v;
392 1.173 ad
393 1.173 ad fdp = curlwp->l_fd;
394 1.192 ad ff = fdp->fd_dt->dt_ff[fd];
395 1.173 ad
396 1.192 ad KASSERT(fd < fdp->fd_dt->dt_nfiles);
397 1.173 ad KASSERT(ff != NULL);
398 1.173 ad KASSERT((ff->ff_refcnt & FR_MASK) > 0);
399 1.173 ad KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
400 1.161 ad
401 1.191 ad if (fdp->fd_refcnt == 1) {
402 1.191 ad /*
403 1.191 ad * Single threaded: don't need to worry about concurrent
404 1.191 ad * access (other than earlier calls to kqueue, which may
405 1.191 ad * hold a reference to the descriptor).
406 1.191 ad */
407 1.191 ad if (__predict_false((ff->ff_refcnt & FR_CLOSING) != 0)) {
408 1.191 ad fd_close(fd);
409 1.191 ad return;
410 1.191 ad }
411 1.191 ad ff->ff_refcnt--;
412 1.191 ad return;
413 1.191 ad }
414 1.191 ad
415 1.173 ad /*
416 1.173 ad * Ensure that any use of the file is complete and globally
417 1.173 ad * visible before dropping the final reference. If no membar,
418 1.173 ad * the current CPU could still access memory associated with
419 1.173 ad * the file after it has been freed or recycled by another
420 1.173 ad * CPU.
421 1.173 ad */
422 1.173 ad #ifndef __HAVE_ATOMIC_AS_MEMBAR
423 1.173 ad membar_exit();
424 1.173 ad #endif
425 1.161 ad
426 1.173 ad /*
427 1.173 ad * Be optimistic and start out with the assumption that no other
428 1.173 ad * threads are trying to close the descriptor. If the CAS fails,
429 1.173 ad * we lost a race and/or it's being closed.
430 1.173 ad */
431 1.173 ad for (u = ff->ff_refcnt & FR_MASK;; u = v) {
432 1.173 ad v = atomic_cas_uint(&ff->ff_refcnt, u, u - 1);
433 1.173 ad if (__predict_true(u == v)) {
434 1.173 ad return;
435 1.173 ad }
436 1.173 ad if (__predict_false((v & FR_CLOSING) != 0)) {
437 1.173 ad break;
438 1.173 ad }
439 1.173 ad }
440 1.162 ad
441 1.173 ad /* Another thread is waiting to close the file: join it. */
442 1.173 ad (void)fd_close(fd);
443 1.161 ad }
444 1.161 ad
445 1.161 ad /*
446 1.173 ad * Convenience wrapper around fd_getfile() that returns reference
447 1.173 ad * to a vnode.
448 1.16 cgd */
449 1.38 christos int
450 1.173 ad fd_getvnode(unsigned fd, file_t **fpp)
451 1.36 thorpej {
452 1.173 ad vnode_t *vp;
453 1.173 ad file_t *fp;
454 1.72 lukem
455 1.173 ad fp = fd_getfile(fd);
456 1.173 ad if (__predict_false(fp == NULL)) {
457 1.173 ad return EBADF;
458 1.173 ad }
459 1.173 ad if (__predict_false(fp->f_type != DTYPE_VNODE)) {
460 1.173 ad fd_putfile(fd);
461 1.173 ad return EINVAL;
462 1.173 ad }
463 1.173 ad vp = fp->f_data;
464 1.173 ad if (__predict_false(vp->v_type == VBAD)) {
465 1.173 ad /* XXX Is this case really necessary? */
466 1.173 ad fd_putfile(fd);
467 1.173 ad return EBADF;
468 1.59 thorpej }
469 1.173 ad *fpp = fp;
470 1.173 ad return 0;
471 1.16 cgd }
472 1.16 cgd
473 1.16 cgd /*
474 1.173 ad * Convenience wrapper around fd_getfile() that returns reference
475 1.173 ad * to a socket.
476 1.16 cgd */
477 1.38 christos int
478 1.173 ad fd_getsock(unsigned fd, struct socket **sop)
479 1.36 thorpej {
480 1.173 ad file_t *fp;
481 1.72 lukem
482 1.173 ad fp = fd_getfile(fd);
483 1.173 ad if (__predict_false(fp == NULL)) {
484 1.173 ad return EBADF;
485 1.103 pk }
486 1.173 ad if (__predict_false(fp->f_type != DTYPE_SOCKET)) {
487 1.173 ad fd_putfile(fd);
488 1.173 ad return ENOTSOCK;
489 1.17 cgd }
490 1.173 ad *sop = fp->f_data;
491 1.173 ad return 0;
492 1.16 cgd }
493 1.16 cgd
494 1.16 cgd /*
495 1.173 ad * Look up the file structure corresponding to a file descriptor
496 1.173 ad * and return it with a reference held on the file, not the
497 1.173 ad * descriptor.
498 1.173 ad *
499 1.173 ad * This is heavyweight and only used when accessing descriptors
500 1.173 ad * from a foreign process. The caller must ensure that `p' does
501 1.173 ad * not exit or fork across this call.
502 1.173 ad *
503 1.173 ad * To release the file (not descriptor) reference, use closef().
504 1.134 thorpej */
505 1.173 ad file_t *
506 1.173 ad fd_getfile2(proc_t *p, unsigned fd)
507 1.134 thorpej {
508 1.173 ad filedesc_t *fdp;
509 1.173 ad fdfile_t *ff;
510 1.173 ad file_t *fp;
511 1.192 ad fdtab_t *dt;
512 1.134 thorpej
513 1.173 ad fdp = p->p_fd;
514 1.173 ad mutex_enter(&fdp->fd_lock);
515 1.192 ad dt = fdp->fd_dt;
516 1.192 ad if (fd >= dt->dt_nfiles) {
517 1.173 ad mutex_exit(&fdp->fd_lock);
518 1.173 ad return NULL;
519 1.173 ad }
520 1.192 ad if ((ff = dt->dt_ff[fd]) == NULL) {
521 1.173 ad mutex_exit(&fdp->fd_lock);
522 1.173 ad return NULL;
523 1.173 ad }
524 1.173 ad if ((fp = ff->ff_file) == NULL) {
525 1.173 ad mutex_exit(&fdp->fd_lock);
526 1.173 ad return NULL;
527 1.158 dsl }
528 1.173 ad mutex_enter(&fp->f_lock);
529 1.173 ad fp->f_count++;
530 1.173 ad mutex_exit(&fp->f_lock);
531 1.173 ad mutex_exit(&fdp->fd_lock);
532 1.158 dsl
533 1.173 ad return fp;
534 1.158 dsl }
535 1.158 dsl
536 1.134 thorpej /*
537 1.173 ad * Internal form of close. Must be called with a reference to the
538 1.173 ad * descriptor, and will drop the reference. When all descriptor
539 1.173 ad * references are dropped, releases the descriptor slot and a single
540 1.173 ad * reference to the file structure.
541 1.173 ad */
542 1.173 ad int
543 1.173 ad fd_close(unsigned fd)
544 1.173 ad {
545 1.173 ad struct flock lf;
546 1.173 ad filedesc_t *fdp;
547 1.173 ad fdfile_t *ff;
548 1.173 ad file_t *fp;
549 1.173 ad proc_t *p;
550 1.173 ad lwp_t *l;
551 1.192 ad u_int refcnt;
552 1.72 lukem
553 1.173 ad l = curlwp;
554 1.99 thorpej p = l->l_proc;
555 1.173 ad fdp = l->l_fd;
556 1.192 ad ff = fdp->fd_dt->dt_ff[fd];
557 1.16 cgd
558 1.173 ad KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
559 1.122 christos
560 1.192 ad mutex_enter(&fdp->fd_lock);
561 1.173 ad KASSERT((ff->ff_refcnt & FR_MASK) > 0);
562 1.192 ad if (__predict_false(ff->ff_file == NULL)) {
563 1.173 ad /*
564 1.173 ad * Another user of the file is already closing, and is
565 1.173 ad * waiting for other users of the file to drain. Release
566 1.173 ad * our reference, and wake up the closer.
567 1.173 ad */
568 1.173 ad atomic_dec_uint(&ff->ff_refcnt);
569 1.173 ad cv_broadcast(&ff->ff_closing);
570 1.192 ad mutex_exit(&fdp->fd_lock);
571 1.122 christos
572 1.173 ad /*
573 1.173 ad * An application error, so pretend that the descriptor
574 1.173 ad * was already closed. We can't safely wait for it to
575 1.173 ad * be closed without potentially deadlocking.
576 1.173 ad */
577 1.16 cgd return (EBADF);
578 1.61 wrstuden }
579 1.173 ad KASSERT((ff->ff_refcnt & FR_CLOSING) == 0);
580 1.61 wrstuden
581 1.173 ad /*
582 1.173 ad * There may be multiple users of this file within the process.
583 1.173 ad * Notify existing and new users that the file is closing. This
584 1.173 ad * will prevent them from adding additional uses to this file
585 1.173 ad * while we are closing it.
586 1.173 ad */
587 1.173 ad fp = ff->ff_file;
588 1.173 ad ff->ff_file = NULL;
589 1.182 matt ff->ff_exclose = false;
590 1.17 cgd
591 1.173 ad /*
592 1.173 ad * We expect the caller to hold a descriptor reference - drop it.
593 1.173 ad * The reference count may increase beyond zero at this point due
594 1.173 ad * to an erroneous descriptor reference by an application, but
595 1.173 ad * fd_getfile() will notice that the file is being closed and drop
596 1.173 ad * the reference again.
597 1.173 ad */
598 1.192 ad if (fdp->fd_refcnt == 1) {
599 1.192 ad /* Single threaded. */
600 1.192 ad refcnt = --(ff->ff_refcnt);
601 1.192 ad } else {
602 1.192 ad /* Multi threaded. */
603 1.173 ad #ifndef __HAVE_ATOMIC_AS_MEMBAR
604 1.192 ad membar_producer();
605 1.173 ad #endif
606 1.192 ad refcnt = atomic_dec_uint_nv(&ff->ff_refcnt);
607 1.192 ad }
608 1.192 ad if (__predict_false(refcnt != 0)) {
609 1.173 ad /*
610 1.173 ad * Wait for other references to drain. This is typically
611 1.173 ad * an application error - the descriptor is being closed
612 1.173 ad * while still in use.
613 1.173 ad *
614 1.173 ad */
615 1.173 ad atomic_or_uint(&ff->ff_refcnt, FR_CLOSING);
616 1.190 ad
617 1.173 ad /*
618 1.173 ad * Remove any knotes attached to the file. A knote
619 1.173 ad * attached to the descriptor can hold references on it.
620 1.173 ad */
621 1.192 ad mutex_exit(&fdp->fd_lock);
622 1.173 ad if (!SLIST_EMPTY(&ff->ff_knlist)) {
623 1.173 ad knote_fdclose(fd);
624 1.75 thorpej }
625 1.190 ad
626 1.190 ad /* Try to drain out descriptor references. */
627 1.190 ad (*fp->f_ops->fo_drain)(fp);
628 1.192 ad mutex_enter(&fdp->fd_lock);
629 1.190 ad
630 1.173 ad /*
631 1.173 ad * We need to see the count drop to zero at least once,
632 1.173 ad * in order to ensure that all pre-existing references
633 1.173 ad * have been drained. New references past this point are
634 1.173 ad * of no interest.
635 1.173 ad */
636 1.173 ad while ((ff->ff_refcnt & FR_MASK) != 0) {
637 1.192 ad cv_wait(&ff->ff_closing, &fdp->fd_lock);
638 1.107 dsl }
639 1.173 ad atomic_and_uint(&ff->ff_refcnt, ~FR_CLOSING);
640 1.173 ad } else {
641 1.173 ad /* If no references, there must be no knotes. */
642 1.173 ad KASSERT(SLIST_EMPTY(&ff->ff_knlist));
643 1.16 cgd }
644 1.59 thorpej
645 1.173 ad /*
646 1.173 ad * POSIX record locking dictates that any close releases ALL
647 1.173 ad * locks owned by this process. This is handled by setting
648 1.173 ad * a flag in the unlock to free ONLY locks obeying POSIX
649 1.173 ad * semantics, and not to free BSD-style file locks.
650 1.173 ad * If the descriptor was in a message, POSIX-style locks
651 1.173 ad * aren't passed with the descriptor.
652 1.173 ad */
653 1.192 ad if (__predict_false((p->p_flag & PK_ADVLOCK) != 0 &&
654 1.192 ad fp->f_type == DTYPE_VNODE)) {
655 1.173 ad lf.l_whence = SEEK_SET;
656 1.173 ad lf.l_start = 0;
657 1.173 ad lf.l_len = 0;
658 1.173 ad lf.l_type = F_UNLCK;
659 1.192 ad mutex_exit(&fdp->fd_lock);
660 1.173 ad (void)VOP_ADVLOCK(fp->f_data, p, F_UNLCK, &lf, F_POSIX);
661 1.192 ad mutex_enter(&fdp->fd_lock);
662 1.103 pk }
663 1.103 pk
664 1.173 ad /* Free descriptor slot. */
665 1.126 pk fd_unused(fdp, fd);
666 1.173 ad mutex_exit(&fdp->fd_lock);
667 1.126 pk
668 1.173 ad /* Now drop reference to the file itself. */
669 1.173 ad return closef(fp);
670 1.27 mycroft }
671 1.27 mycroft
672 1.17 cgd /*
673 1.173 ad * Duplicate a file descriptor.
674 1.16 cgd */
675 1.38 christos int
676 1.182 matt fd_dup(file_t *fp, int minfd, int *newp, bool exclose)
677 1.36 thorpej {
678 1.173 ad proc_t *p;
679 1.173 ad int error;
680 1.16 cgd
681 1.173 ad p = curproc;
682 1.79 thorpej
683 1.173 ad while ((error = fd_alloc(p, minfd, newp)) != 0) {
684 1.173 ad if (error != ENOSPC) {
685 1.173 ad return error;
686 1.173 ad }
687 1.173 ad fd_tryexpand(p);
688 1.173 ad }
689 1.79 thorpej
690 1.192 ad curlwp->l_fd->fd_dt->dt_ff[*newp]->ff_exclose = exclose;
691 1.173 ad fd_affix(p, fp, *newp);
692 1.173 ad return 0;
693 1.16 cgd }
694 1.16 cgd
695 1.17 cgd /*
696 1.173 ad * dup2 operation.
697 1.153 dsl */
698 1.153 dsl int
699 1.173 ad fd_dup2(file_t *fp, unsigned new)
700 1.153 dsl {
701 1.173 ad filedesc_t *fdp;
702 1.173 ad fdfile_t *ff;
703 1.192 ad fdtab_t *dt;
704 1.153 dsl
705 1.173 ad fdp = curlwp->l_fd;
706 1.153 dsl
707 1.173 ad /*
708 1.173 ad * Ensure there are enough slots in the descriptor table,
709 1.173 ad * and allocate an fdfile_t up front in case we need it.
710 1.173 ad */
711 1.192 ad while (new >= fdp->fd_dt->dt_nfiles) {
712 1.173 ad fd_tryexpand(curproc);
713 1.173 ad }
714 1.173 ad ff = pool_cache_get(fdfile_cache, PR_WAITOK);
715 1.153 dsl
716 1.173 ad /*
717 1.173 ad * If there is already a file open, close it. If the file is
718 1.173 ad * half open, wait for it to be constructed before closing it.
719 1.173 ad * XXX Potential for deadlock here?
720 1.173 ad */
721 1.173 ad mutex_enter(&fdp->fd_lock);
722 1.173 ad while (fd_isused(fdp, new)) {
723 1.173 ad mutex_exit(&fdp->fd_lock);
724 1.173 ad if (fd_getfile(new) != NULL) {
725 1.173 ad (void)fd_close(new);
726 1.173 ad } else {
727 1.192 ad /*
728 1.192 ad * Crummy, but unlikely to happen.
729 1.192 ad * Can occur if we interrupt another
730 1.192 ad * thread while it is opening a file.
731 1.192 ad */
732 1.173 ad kpause("dup2", false, 1, NULL);
733 1.173 ad }
734 1.173 ad mutex_enter(&fdp->fd_lock);
735 1.173 ad }
736 1.192 ad dt = fdp->fd_dt;
737 1.192 ad if (dt->dt_ff[new] == NULL) {
738 1.173 ad KASSERT(new >= NDFDFILE);
739 1.192 ad dt->dt_ff[new] = ff;
740 1.173 ad ff = NULL;
741 1.173 ad }
742 1.173 ad fd_used(fdp, new);
743 1.173 ad mutex_exit(&fdp->fd_lock);
744 1.173 ad
745 1.173 ad /* Slot is now allocated. Insert copy of the file. */
746 1.173 ad fd_affix(curproc, fp, new);
747 1.173 ad if (ff != NULL) {
748 1.173 ad pool_cache_put(fdfile_cache, ff);
749 1.173 ad }
750 1.173 ad return 0;
751 1.153 dsl }
752 1.153 dsl
753 1.153 dsl /*
754 1.173 ad * Drop reference to a file structure.
755 1.17 cgd */
756 1.38 christos int
757 1.173 ad closef(file_t *fp)
758 1.36 thorpej {
759 1.173 ad struct flock lf;
760 1.173 ad int error;
761 1.16 cgd
762 1.173 ad /*
763 1.173 ad * Drop reference. If referenced elsewhere it's still open
764 1.173 ad * and we have nothing more to do.
765 1.173 ad */
766 1.173 ad mutex_enter(&fp->f_lock);
767 1.173 ad KASSERT(fp->f_count > 0);
768 1.173 ad if (--fp->f_count > 0) {
769 1.173 ad mutex_exit(&fp->f_lock);
770 1.173 ad return 0;
771 1.173 ad }
772 1.173 ad KASSERT(fp->f_count == 0);
773 1.173 ad mutex_exit(&fp->f_lock);
774 1.59 thorpej
775 1.173 ad /* We held the last reference - release locks, close and free. */
776 1.173 ad if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
777 1.173 ad lf.l_whence = SEEK_SET;
778 1.173 ad lf.l_start = 0;
779 1.173 ad lf.l_len = 0;
780 1.173 ad lf.l_type = F_UNLCK;
781 1.173 ad (void)VOP_ADVLOCK(fp->f_data, fp, F_UNLCK, &lf, F_FLOCK);
782 1.173 ad }
783 1.173 ad if (fp->f_ops != NULL) {
784 1.173 ad error = (*fp->f_ops->fo_close)(fp);
785 1.173 ad } else {
786 1.173 ad error = 0;
787 1.17 cgd }
788 1.191 ad KASSERT(fp->f_count == 0);
789 1.191 ad KASSERT(fp->f_cred != NULL);
790 1.191 ad pool_cache_put(file_cache, fp);
791 1.59 thorpej
792 1.173 ad return error;
793 1.16 cgd }
794 1.16 cgd
795 1.16 cgd /*
796 1.16 cgd * Allocate a file descriptor for the process.
797 1.16 cgd */
798 1.38 christos int
799 1.173 ad fd_alloc(proc_t *p, int want, int *result)
800 1.72 lukem {
801 1.173 ad filedesc_t *fdp;
802 1.126 pk int i, lim, last, error;
803 1.115 provos u_int off, new;
804 1.192 ad fdtab_t *dt;
805 1.173 ad
806 1.173 ad KASSERT(p == curproc || p == &proc0);
807 1.72 lukem
808 1.72 lukem fdp = p->p_fd;
809 1.16 cgd
810 1.16 cgd /*
811 1.16 cgd * Search for a free descriptor starting at the higher
812 1.173 ad * of want or fd_freefile.
813 1.16 cgd */
814 1.173 ad mutex_enter(&fdp->fd_lock);
815 1.192 ad fd_checkmaps(fdp);
816 1.192 ad dt = fdp->fd_dt;
817 1.192 ad KASSERT(dt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
818 1.17 cgd lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
819 1.192 ad last = min(dt->dt_nfiles, lim);
820 1.173 ad for (;;) {
821 1.173 ad if ((i = want) < fdp->fd_freefile)
822 1.173 ad i = fdp->fd_freefile;
823 1.173 ad off = i >> NDENTRYSHIFT;
824 1.173 ad new = fd_next_zero(fdp, fdp->fd_himap, off,
825 1.173 ad (last + NDENTRIES - 1) >> NDENTRYSHIFT);
826 1.173 ad if (new == -1)
827 1.173 ad break;
828 1.173 ad i = fd_next_zero(fdp, &fdp->fd_lomap[new],
829 1.115 provos new > off ? 0 : i & NDENTRYMASK, NDENTRIES);
830 1.115 provos if (i == -1) {
831 1.131 perry /*
832 1.173 ad * Free file descriptor in this block was
833 1.115 provos * below want, try again with higher want.
834 1.115 provos */
835 1.115 provos want = (new + 1) << NDENTRYSHIFT;
836 1.173 ad continue;
837 1.115 provos }
838 1.115 provos i += (new << NDENTRYSHIFT);
839 1.173 ad if (i >= last) {
840 1.173 ad break;
841 1.173 ad }
842 1.192 ad if (dt->dt_ff[i] == NULL) {
843 1.173 ad KASSERT(i >= NDFDFILE);
844 1.192 ad dt->dt_ff[i] = pool_cache_get(fdfile_cache, PR_WAITOK);
845 1.173 ad }
846 1.192 ad KASSERT(dt->dt_ff[i]->ff_refcnt == 0);
847 1.192 ad KASSERT(dt->dt_ff[i]->ff_file == NULL);
848 1.173 ad fd_used(fdp, i);
849 1.173 ad if (want <= fdp->fd_freefile) {
850 1.173 ad fdp->fd_freefile = i;
851 1.16 cgd }
852 1.173 ad *result = i;
853 1.192 ad KASSERT(i >= NDFDFILE ||
854 1.192 ad dt->dt_ff[i] == (fdfile_t *)fdp->fd_dfdfile[i]);
855 1.192 ad fd_checkmaps(fdp);
856 1.173 ad mutex_exit(&fdp->fd_lock);
857 1.173 ad return 0;
858 1.90 enami }
859 1.16 cgd
860 1.173 ad /* No space in current array. Let the caller expand and retry. */
861 1.192 ad error = (dt->dt_nfiles >= lim) ? EMFILE : ENOSPC;
862 1.173 ad mutex_exit(&fdp->fd_lock);
863 1.173 ad return error;
864 1.16 cgd }
865 1.16 cgd
866 1.173 ad /*
867 1.192 ad * Allocate memory for a descriptor table.
868 1.185 ad */
869 1.192 ad static fdtab_t *
870 1.192 ad fd_dtab_alloc(int n)
871 1.185 ad {
872 1.192 ad fdtab_t *dt;
873 1.192 ad size_t sz;
874 1.185 ad
875 1.185 ad KASSERT(n > NDFILE);
876 1.185 ad
877 1.192 ad sz = sizeof(*dt) + (n - NDFILE) * sizeof(dt->dt_ff[0]);
878 1.192 ad dt = kmem_alloc(sz, KM_SLEEP);
879 1.192 ad #ifdef DIAGNOSTIC
880 1.192 ad memset(dt, 0xff, sz);
881 1.192 ad #endif
882 1.192 ad dt->dt_nfiles = n;
883 1.192 ad dt->dt_link = NULL;
884 1.192 ad return dt;
885 1.185 ad }
886 1.185 ad
887 1.185 ad /*
888 1.192 ad * Free a descriptor table, and all tables linked for deferred free.
889 1.185 ad */
890 1.185 ad static void
891 1.192 ad fd_dtab_free(fdtab_t *dt)
892 1.185 ad {
893 1.192 ad fdtab_t *next;
894 1.192 ad size_t sz;
895 1.185 ad
896 1.192 ad do {
897 1.192 ad next = dt->dt_link;
898 1.192 ad KASSERT(dt->dt_nfiles > NDFILE);
899 1.192 ad sz = sizeof(*dt) +
900 1.192 ad (dt->dt_nfiles - NDFILE) * sizeof(dt->dt_ff[0]);
901 1.192 ad #ifdef DIAGNOSTIC
902 1.192 ad memset(dt, 0xff, sz);
903 1.192 ad #endif
904 1.192 ad kmem_free(dt, sz);
905 1.192 ad dt = next;
906 1.192 ad } while (dt != NULL);
907 1.185 ad }
908 1.185 ad
909 1.185 ad /*
910 1.185 ad * Allocate descriptor bitmap.
911 1.185 ad */
912 1.185 ad static void
913 1.185 ad fd_map_alloc(int n, uint32_t **lo, uint32_t **hi)
914 1.185 ad {
915 1.185 ad uint8_t *ptr;
916 1.185 ad size_t szlo, szhi;
917 1.185 ad
918 1.185 ad KASSERT(n > NDENTRIES);
919 1.185 ad
920 1.185 ad szlo = NDLOSLOTS(n) * sizeof(uint32_t);
921 1.185 ad szhi = NDHISLOTS(n) * sizeof(uint32_t);
922 1.185 ad ptr = kmem_alloc(szlo + szhi, KM_SLEEP);
923 1.185 ad *lo = (uint32_t *)ptr;
924 1.185 ad *hi = (uint32_t *)(ptr + szlo);
925 1.185 ad }
926 1.185 ad
927 1.185 ad /*
928 1.185 ad * Free descriptor bitmap.
929 1.185 ad */
930 1.185 ad static void
931 1.185 ad fd_map_free(int n, uint32_t *lo, uint32_t *hi)
932 1.185 ad {
933 1.185 ad size_t szlo, szhi;
934 1.185 ad
935 1.185 ad KASSERT(n > NDENTRIES);
936 1.185 ad
937 1.185 ad szlo = NDLOSLOTS(n) * sizeof(uint32_t);
938 1.185 ad szhi = NDHISLOTS(n) * sizeof(uint32_t);
939 1.185 ad KASSERT(hi == (uint32_t *)((uint8_t *)lo + szlo));
940 1.185 ad kmem_free(lo, szlo + szhi);
941 1.185 ad }
942 1.185 ad
943 1.185 ad /*
944 1.173 ad * Expand a process' descriptor table.
945 1.173 ad */
946 1.76 thorpej void
947 1.173 ad fd_tryexpand(proc_t *p)
948 1.76 thorpej {
949 1.173 ad filedesc_t *fdp;
950 1.173 ad int i, numfiles, oldnfiles;
951 1.192 ad fdtab_t *newdt, *dt;
952 1.173 ad uint32_t *newhimap, *newlomap;
953 1.173 ad
954 1.173 ad KASSERT(p == curproc || p == &proc0);
955 1.76 thorpej
956 1.76 thorpej fdp = p->p_fd;
957 1.173 ad newhimap = NULL;
958 1.173 ad newlomap = NULL;
959 1.192 ad oldnfiles = fdp->fd_dt->dt_nfiles;
960 1.126 pk
961 1.126 pk if (oldnfiles < NDEXTENT)
962 1.133 christos numfiles = NDEXTENT;
963 1.76 thorpej else
964 1.133 christos numfiles = 2 * oldnfiles;
965 1.126 pk
966 1.192 ad newdt = fd_dtab_alloc(numfiles);
967 1.133 christos if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
968 1.185 ad fd_map_alloc(numfiles, &newlomap, &newhimap);
969 1.126 pk }
970 1.126 pk
971 1.173 ad mutex_enter(&fdp->fd_lock);
972 1.192 ad dt = fdp->fd_dt;
973 1.192 ad KASSERT(dt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
974 1.192 ad if (dt->dt_nfiles != oldnfiles) {
975 1.173 ad /* fdp changed; caller must retry */
976 1.173 ad mutex_exit(&fdp->fd_lock);
977 1.192 ad fd_dtab_free(newdt);
978 1.185 ad if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
979 1.185 ad fd_map_free(numfiles, newlomap, newhimap);
980 1.185 ad }
981 1.173 ad return;
982 1.173 ad }
983 1.173 ad
984 1.192 ad /* Copy the existing descriptor table and zero the new portion. */
985 1.192 ad i = sizeof(fdfile_t *) * oldnfiles;
986 1.192 ad memcpy(newdt->dt_ff, dt->dt_ff, i);
987 1.194 yamt memset((uint8_t *)newdt->dt_ff + i, 0,
988 1.194 yamt numfiles * sizeof(fdfile_t *) - i);
989 1.173 ad
990 1.173 ad /*
991 1.192 ad * Link old descriptor array into list to be discarded. We defer
992 1.192 ad * freeing until the last reference to the descriptor table goes
993 1.192 ad * away (usually process exit). This allows us to do lockless
994 1.192 ad * lookups in fd_getfile().
995 1.173 ad */
996 1.173 ad if (oldnfiles > NDFILE) {
997 1.191 ad if (fdp->fd_refcnt > 1) {
998 1.192 ad newdt->dt_link = dt;
999 1.173 ad } else {
1000 1.192 ad fd_dtab_free(dt);
1001 1.173 ad }
1002 1.173 ad }
1003 1.115 provos
1004 1.133 christos if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
1005 1.173 ad i = NDHISLOTS(oldnfiles) * sizeof(uint32_t);
1006 1.173 ad memcpy(newhimap, fdp->fd_himap, i);
1007 1.173 ad memset((uint8_t *)newhimap + i, 0,
1008 1.133 christos NDHISLOTS(numfiles) * sizeof(uint32_t) - i);
1009 1.115 provos
1010 1.173 ad i = NDLOSLOTS(oldnfiles) * sizeof(uint32_t);
1011 1.173 ad memcpy(newlomap, fdp->fd_lomap, i);
1012 1.173 ad memset((uint8_t *)newlomap + i, 0,
1013 1.133 christos NDLOSLOTS(numfiles) * sizeof(uint32_t) - i);
1014 1.115 provos
1015 1.126 pk if (NDHISLOTS(oldnfiles) > NDHISLOTS(NDFILE)) {
1016 1.185 ad fd_map_free(oldnfiles, fdp->fd_lomap, fdp->fd_himap);
1017 1.115 provos }
1018 1.115 provos fdp->fd_himap = newhimap;
1019 1.115 provos fdp->fd_lomap = newlomap;
1020 1.115 provos }
1021 1.115 provos
1022 1.173 ad /*
1023 1.173 ad * All other modifications must become globally visible before
1024 1.192 ad * the change to fd_dt. See fd_getfile().
1025 1.173 ad */
1026 1.173 ad membar_producer();
1027 1.192 ad fdp->fd_dt = newdt;
1028 1.192 ad KASSERT(newdt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
1029 1.192 ad fd_checkmaps(fdp);
1030 1.173 ad mutex_exit(&fdp->fd_lock);
1031 1.76 thorpej }
1032 1.76 thorpej
1033 1.16 cgd /*
1034 1.173 ad * Create a new open file structure and allocate a file descriptor
1035 1.173 ad * for the current process.
1036 1.16 cgd */
1037 1.38 christos int
1038 1.173 ad fd_allocfile(file_t **resultfp, int *resultfd)
1039 1.16 cgd {
1040 1.191 ad kauth_cred_t cred;
1041 1.173 ad file_t *fp;
1042 1.173 ad proc_t *p;
1043 1.173 ad int error;
1044 1.16 cgd
1045 1.173 ad p = curproc;
1046 1.144 ad
1047 1.173 ad while ((error = fd_alloc(p, 0, resultfd)) != 0) {
1048 1.173 ad if (error != ENOSPC) {
1049 1.173 ad return error;
1050 1.76 thorpej }
1051 1.173 ad fd_tryexpand(p);
1052 1.75 thorpej }
1053 1.102 pk
1054 1.162 ad fp = pool_cache_get(file_cache, PR_WAITOK);
1055 1.191 ad if (fp == NULL) {
1056 1.191 ad return ENFILE;
1057 1.191 ad }
1058 1.173 ad KASSERT(fp->f_count == 0);
1059 1.188 mrg KASSERT(fp->f_msgcount == 0);
1060 1.188 mrg KASSERT(fp->f_unpcount == 0);
1061 1.167 ad
1062 1.191 ad /* Replace cached credentials if not what we need. */
1063 1.191 ad cred = curlwp->l_cred;
1064 1.191 ad if (__predict_false(cred != fp->f_cred)) {
1065 1.191 ad kauth_cred_free(fp->f_cred);
1066 1.191 ad kauth_cred_hold(cred);
1067 1.191 ad fp->f_cred = cred;
1068 1.16 cgd }
1069 1.167 ad
1070 1.188 mrg /*
1071 1.188 mrg * Don't allow recycled files to be scanned.
1072 1.191 ad * See uipc_usrreq.c.
1073 1.188 mrg */
1074 1.191 ad if (__predict_false((fp->f_flag & FSCAN) != 0)) {
1075 1.188 mrg mutex_enter(&fp->f_lock);
1076 1.188 mrg atomic_and_uint(&fp->f_flag, ~FSCAN);
1077 1.188 mrg mutex_exit(&fp->f_lock);
1078 1.188 mrg }
1079 1.188 mrg
1080 1.167 ad fp->f_advice = 0;
1081 1.167 ad fp->f_offset = 0;
1082 1.173 ad *resultfp = fp;
1083 1.173 ad
1084 1.173 ad return 0;
1085 1.173 ad }
1086 1.173 ad
1087 1.173 ad /*
1088 1.173 ad * Successful creation of a new descriptor: make visible to the process.
1089 1.173 ad */
1090 1.173 ad void
1091 1.173 ad fd_affix(proc_t *p, file_t *fp, unsigned fd)
1092 1.173 ad {
1093 1.173 ad fdfile_t *ff;
1094 1.173 ad filedesc_t *fdp;
1095 1.173 ad
1096 1.173 ad KASSERT(p == curproc || p == &proc0);
1097 1.173 ad
1098 1.173 ad /* Add a reference to the file structure. */
1099 1.173 ad mutex_enter(&fp->f_lock);
1100 1.173 ad fp->f_count++;
1101 1.173 ad mutex_exit(&fp->f_lock);
1102 1.167 ad
1103 1.16 cgd /*
1104 1.173 ad * Insert the new file into the descriptor slot.
1105 1.173 ad *
1106 1.173 ad * The memory barriers provided by lock activity in this routine
1107 1.173 ad * ensure that any updates to the file structure become globally
1108 1.173 ad * visible before the file becomes visible to other LWPs in the
1109 1.173 ad * current process.
1110 1.16 cgd */
1111 1.173 ad fdp = p->p_fd;
1112 1.192 ad ff = fdp->fd_dt->dt_ff[fd];
1113 1.173 ad
1114 1.173 ad KASSERT(ff != NULL);
1115 1.173 ad KASSERT(ff->ff_file == NULL);
1116 1.173 ad KASSERT(ff->ff_allocated);
1117 1.173 ad KASSERT(fd_isused(fdp, fd));
1118 1.192 ad KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
1119 1.173 ad
1120 1.173 ad /* No need to lock in order to make file initially visible. */
1121 1.173 ad ff->ff_file = fp;
1122 1.173 ad }
1123 1.173 ad
1124 1.173 ad /*
1125 1.173 ad * Abort creation of a new descriptor: free descriptor slot and file.
1126 1.173 ad */
1127 1.173 ad void
1128 1.173 ad fd_abort(proc_t *p, file_t *fp, unsigned fd)
1129 1.173 ad {
1130 1.173 ad filedesc_t *fdp;
1131 1.173 ad fdfile_t *ff;
1132 1.173 ad
1133 1.173 ad KASSERT(p == curproc || p == &proc0);
1134 1.173 ad
1135 1.173 ad fdp = p->p_fd;
1136 1.192 ad ff = fdp->fd_dt->dt_ff[fd];
1137 1.173 ad
1138 1.192 ad KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
1139 1.173 ad
1140 1.173 ad mutex_enter(&fdp->fd_lock);
1141 1.173 ad KASSERT(fd_isused(fdp, fd));
1142 1.173 ad fd_unused(fdp, fd);
1143 1.173 ad mutex_exit(&fdp->fd_lock);
1144 1.167 ad
1145 1.173 ad if (fp != NULL) {
1146 1.191 ad KASSERT(fp->f_count == 0);
1147 1.191 ad KASSERT(fp->f_cred != NULL);
1148 1.191 ad pool_cache_put(file_cache, fp);
1149 1.59 thorpej }
1150 1.16 cgd }
1151 1.16 cgd
1152 1.167 ad static int
1153 1.167 ad file_ctor(void *arg, void *obj, int flags)
1154 1.167 ad {
1155 1.173 ad file_t *fp = obj;
1156 1.167 ad
1157 1.167 ad memset(fp, 0, sizeof(*fp));
1158 1.167 ad
1159 1.167 ad mutex_enter(&filelist_lock);
1160 1.191 ad if (__predict_false(nfiles >= maxfiles)) {
1161 1.191 ad mutex_exit(&filelist_lock);
1162 1.191 ad tablefull("file", "increase kern.maxfiles or MAXFILES");
1163 1.191 ad return ENFILE;
1164 1.191 ad }
1165 1.191 ad nfiles++;
1166 1.167 ad LIST_INSERT_HEAD(&filehead, fp, f_list);
1167 1.191 ad mutex_init(&fp->f_lock, MUTEX_DEFAULT, IPL_NONE);
1168 1.191 ad fp->f_cred = curlwp->l_cred;
1169 1.191 ad kauth_cred_hold(fp->f_cred);
1170 1.167 ad mutex_exit(&filelist_lock);
1171 1.167 ad
1172 1.167 ad return 0;
1173 1.167 ad }
1174 1.167 ad
1175 1.167 ad static void
1176 1.167 ad file_dtor(void *arg, void *obj)
1177 1.167 ad {
1178 1.173 ad file_t *fp = obj;
1179 1.167 ad
1180 1.167 ad mutex_enter(&filelist_lock);
1181 1.191 ad nfiles--;
1182 1.167 ad LIST_REMOVE(fp, f_list);
1183 1.167 ad mutex_exit(&filelist_lock);
1184 1.167 ad
1185 1.191 ad kauth_cred_free(fp->f_cred);
1186 1.167 ad mutex_destroy(&fp->f_lock);
1187 1.167 ad }
1188 1.167 ad
1189 1.173 ad static int
1190 1.173 ad fdfile_ctor(void *arg, void *obj, int flags)
1191 1.173 ad {
1192 1.173 ad fdfile_t *ff = obj;
1193 1.173 ad
1194 1.173 ad memset(ff, 0, sizeof(*ff));
1195 1.173 ad cv_init(&ff->ff_closing, "fdclose");
1196 1.173 ad
1197 1.173 ad return 0;
1198 1.173 ad }
1199 1.173 ad
1200 1.173 ad static void
1201 1.173 ad fdfile_dtor(void *arg, void *obj)
1202 1.173 ad {
1203 1.173 ad fdfile_t *ff = obj;
1204 1.173 ad
1205 1.173 ad cv_destroy(&ff->ff_closing);
1206 1.173 ad }
1207 1.173 ad
1208 1.173 ad file_t *
1209 1.169 ad fgetdummy(void)
1210 1.169 ad {
1211 1.173 ad file_t *fp;
1212 1.169 ad
1213 1.169 ad fp = kmem_alloc(sizeof(*fp), KM_SLEEP);
1214 1.169 ad if (fp != NULL) {
1215 1.169 ad memset(fp, 0, sizeof(*fp));
1216 1.169 ad mutex_init(&fp->f_lock, MUTEX_DEFAULT, IPL_NONE);
1217 1.169 ad }
1218 1.169 ad return fp;
1219 1.169 ad }
1220 1.169 ad
1221 1.169 ad void
1222 1.173 ad fputdummy(file_t *fp)
1223 1.58 thorpej {
1224 1.58 thorpej
1225 1.173 ad mutex_destroy(&fp->f_lock);
1226 1.173 ad kmem_free(fp, sizeof(*fp));
1227 1.58 thorpej }
1228 1.58 thorpej
1229 1.58 thorpej /*
1230 1.173 ad * Create an initial filedesc structure.
1231 1.48 thorpej */
1232 1.173 ad filedesc_t *
1233 1.173 ad fd_init(filedesc_t *fdp)
1234 1.48 thorpej {
1235 1.192 ad #ifdef DIAGNOSTIC
1236 1.173 ad unsigned fd;
1237 1.192 ad #endif
1238 1.173 ad
1239 1.192 ad if (__predict_true(fdp == NULL)) {
1240 1.173 ad fdp = pool_cache_get(filedesc_cache, PR_WAITOK);
1241 1.173 ad } else {
1242 1.192 ad /* XXXRUMP KASSERT(fdp == &filedesc0); */
1243 1.173 ad filedesc_ctor(NULL, fdp, PR_WAITOK);
1244 1.173 ad }
1245 1.48 thorpej
1246 1.192 ad #ifdef DIAGNOSTIC
1247 1.173 ad KASSERT(fdp->fd_lastfile == -1);
1248 1.173 ad KASSERT(fdp->fd_lastkqfile == -1);
1249 1.173 ad KASSERT(fdp->fd_knhash == NULL);
1250 1.192 ad KASSERT(fdp->fd_freefile == 0);
1251 1.192 ad KASSERT(fdp->fd_exclose == false);
1252 1.192 ad KASSERT(fdp->fd_dt == &fdp->fd_dtbuiltin);
1253 1.192 ad KASSERT(fdp->fd_dtbuiltin.dt_nfiles == NDFILE);
1254 1.173 ad for (fd = 0; fd < NDFDFILE; fd++) {
1255 1.192 ad KASSERT(fdp->fd_dtbuiltin.dt_ff[fd] ==
1256 1.192 ad (fdfile_t *)fdp->fd_dfdfile[fd]);
1257 1.192 ad }
1258 1.192 ad for (fd = NDFDFILE; fd < NDFILE; fd++) {
1259 1.192 ad KASSERT(fdp->fd_dtbuiltin.dt_ff[fd] == NULL);
1260 1.173 ad }
1261 1.195 yamt KASSERT(fdp->fd_himap == fdp->fd_dhimap);
1262 1.195 yamt KASSERT(fdp->fd_lomap == fdp->fd_dlomap);
1263 1.192 ad #endif /* DIAGNOSTIC */
1264 1.192 ad
1265 1.192 ad fdp->fd_refcnt = 1;
1266 1.48 thorpej
1267 1.173 ad return fdp;
1268 1.48 thorpej }
1269 1.48 thorpej
1270 1.48 thorpej /*
1271 1.48 thorpej * Initialize a file descriptor table.
1272 1.48 thorpej */
1273 1.173 ad static int
1274 1.173 ad filedesc_ctor(void *arg, void *obj, int flag)
1275 1.48 thorpej {
1276 1.173 ad filedesc_t *fdp = obj;
1277 1.192 ad fdfile_t **ffp;
1278 1.173 ad int i;
1279 1.48 thorpej
1280 1.173 ad memset(fdp, 0, sizeof(*fdp));
1281 1.173 ad mutex_init(&fdp->fd_lock, MUTEX_DEFAULT, IPL_NONE);
1282 1.173 ad fdp->fd_lastfile = -1;
1283 1.173 ad fdp->fd_lastkqfile = -1;
1284 1.192 ad fdp->fd_dt = &fdp->fd_dtbuiltin;
1285 1.192 ad fdp->fd_dtbuiltin.dt_nfiles = NDFILE;
1286 1.195 yamt fdp->fd_himap = fdp->fd_dhimap;
1287 1.195 yamt fdp->fd_lomap = fdp->fd_dlomap;
1288 1.173 ad
1289 1.181 matt CTASSERT(sizeof(fdp->fd_dfdfile[0]) >= sizeof(fdfile_t));
1290 1.192 ad for (i = 0, ffp = fdp->fd_dt->dt_ff; i < NDFDFILE; i++, ffp++) {
1291 1.192 ad *ffp = (fdfile_t *)fdp->fd_dfdfile[i];
1292 1.192 ad (void)fdfile_ctor(NULL, fdp->fd_dfdfile[i], PR_WAITOK);
1293 1.173 ad }
1294 1.48 thorpej
1295 1.173 ad return 0;
1296 1.48 thorpej }
1297 1.48 thorpej
1298 1.173 ad static void
1299 1.173 ad filedesc_dtor(void *arg, void *obj)
1300 1.48 thorpej {
1301 1.173 ad filedesc_t *fdp = obj;
1302 1.173 ad int i;
1303 1.48 thorpej
1304 1.173 ad for (i = 0; i < NDFDFILE; i++) {
1305 1.173 ad fdfile_dtor(NULL, fdp->fd_dfdfile[i]);
1306 1.173 ad }
1307 1.48 thorpej
1308 1.173 ad mutex_destroy(&fdp->fd_lock);
1309 1.48 thorpej }
1310 1.48 thorpej
1311 1.48 thorpej /*
1312 1.173 ad * Make p2 share p1's filedesc structure.
1313 1.48 thorpej */
1314 1.48 thorpej void
1315 1.173 ad fd_share(struct proc *p2)
1316 1.48 thorpej {
1317 1.173 ad filedesc_t *fdp;
1318 1.48 thorpej
1319 1.173 ad fdp = curlwp->l_fd;
1320 1.173 ad p2->p_fd = fdp;
1321 1.173 ad atomic_inc_uint(&fdp->fd_refcnt);
1322 1.16 cgd }
1323 1.16 cgd
1324 1.16 cgd /*
1325 1.191 ad * Acquire a hold on a filedesc structure.
1326 1.191 ad */
1327 1.191 ad void
1328 1.191 ad fd_hold(void)
1329 1.191 ad {
1330 1.191 ad
1331 1.191 ad atomic_inc_uint(&curlwp->l_fd->fd_refcnt);
1332 1.191 ad }
1333 1.191 ad
1334 1.191 ad /*
1335 1.16 cgd * Copy a filedesc structure.
1336 1.16 cgd */
1337 1.173 ad filedesc_t *
1338 1.173 ad fd_copy(void)
1339 1.16 cgd {
1340 1.173 ad filedesc_t *newfdp, *fdp;
1341 1.192 ad fdfile_t *ff, **ffp, **nffp, *ff2;
1342 1.192 ad int i, j, numfiles, lastfile, newlast;
1343 1.173 ad file_t *fp;
1344 1.192 ad fdtab_t *newdt;
1345 1.16 cgd
1346 1.173 ad fdp = curproc->p_fd;
1347 1.173 ad newfdp = pool_cache_get(filedesc_cache, PR_WAITOK);
1348 1.16 cgd newfdp->fd_refcnt = 1;
1349 1.126 pk
1350 1.192 ad #ifdef DIAGNOSTIC
1351 1.192 ad KASSERT(newfdp->fd_lastfile == -1);
1352 1.192 ad KASSERT(newfdp->fd_lastkqfile == -1);
1353 1.173 ad KASSERT(newfdp->fd_knhash == NULL);
1354 1.192 ad KASSERT(newfdp->fd_freefile == 0);
1355 1.192 ad KASSERT(newfdp->fd_exclose == false);
1356 1.192 ad KASSERT(newfdp->fd_dt == &newfdp->fd_dtbuiltin);
1357 1.192 ad KASSERT(newfdp->fd_dtbuiltin.dt_nfiles == NDFILE);
1358 1.192 ad for (i = 0; i < NDFDFILE; i++) {
1359 1.192 ad KASSERT(newfdp->fd_dtbuiltin.dt_ff[i] ==
1360 1.192 ad (fdfile_t *)&newfdp->fd_dfdfile[i]);
1361 1.192 ad }
1362 1.192 ad for (i = NDFDFILE; i < NDFILE; i++) {
1363 1.192 ad KASSERT(newfdp->fd_dtbuiltin.dt_ff[i] == NULL);
1364 1.192 ad }
1365 1.192 ad #endif /* DIAGNOSTIC */
1366 1.173 ad
1367 1.192 ad mutex_enter(&fdp->fd_lock);
1368 1.192 ad fd_checkmaps(fdp);
1369 1.192 ad numfiles = fdp->fd_dt->dt_nfiles;
1370 1.192 ad lastfile = fdp->fd_lastfile;
1371 1.173 ad
1372 1.192 ad /*
1373 1.192 ad * If the number of open files fits in the internal arrays
1374 1.192 ad * of the open file structure, use them, otherwise allocate
1375 1.192 ad * additional memory for the number of descriptors currently
1376 1.192 ad * in use.
1377 1.192 ad */
1378 1.192 ad if (lastfile < NDFILE) {
1379 1.192 ad i = NDFILE;
1380 1.192 ad newdt = newfdp->fd_dt;
1381 1.192 ad KASSERT(newfdp->fd_dt == &newfdp->fd_dtbuiltin);
1382 1.192 ad } else {
1383 1.173 ad /*
1384 1.192 ad * Compute the smallest multiple of NDEXTENT needed
1385 1.192 ad * for the file descriptors currently in use,
1386 1.192 ad * allowing the table to shrink.
1387 1.173 ad */
1388 1.192 ad i = numfiles;
1389 1.192 ad while (i >= 2 * NDEXTENT && i > lastfile * 2) {
1390 1.192 ad i /= 2;
1391 1.192 ad }
1392 1.192 ad KASSERT(i > NDFILE);
1393 1.192 ad newdt = fd_dtab_alloc(i);
1394 1.192 ad newfdp->fd_dt = newdt;
1395 1.192 ad memcpy(newdt->dt_ff, newfdp->fd_dtbuiltin.dt_ff,
1396 1.192 ad NDFDFILE * sizeof(fdfile_t **));
1397 1.192 ad memset(newdt->dt_ff + NDFDFILE, 0,
1398 1.192 ad (i - NDFDFILE) * sizeof(fdfile_t **));
1399 1.192 ad }
1400 1.192 ad if (NDHISLOTS(i) <= NDHISLOTS(NDFILE)) {
1401 1.192 ad newfdp->fd_himap = newfdp->fd_dhimap;
1402 1.192 ad newfdp->fd_lomap = newfdp->fd_dlomap;
1403 1.192 ad } else {
1404 1.192 ad fd_map_alloc(i, &newfdp->fd_lomap, &newfdp->fd_himap);
1405 1.192 ad KASSERT(i >= NDENTRIES * NDENTRIES);
1406 1.192 ad memset(newfdp->fd_himap, 0, NDHISLOTS(i)*sizeof(uint32_t));
1407 1.192 ad memset(newfdp->fd_lomap, 0, NDLOSLOTS(i)*sizeof(uint32_t));
1408 1.115 provos }
1409 1.126 pk newfdp->fd_freefile = fdp->fd_freefile;
1410 1.173 ad newfdp->fd_exclose = fdp->fd_exclose;
1411 1.126 pk
1412 1.192 ad ffp = fdp->fd_dt->dt_ff;
1413 1.192 ad nffp = newdt->dt_ff;
1414 1.173 ad newlast = -1;
1415 1.192 ad for (i = 0; i <= (int)lastfile; i++, ffp++, nffp++) {
1416 1.192 ad KASSERT(i >= NDFDFILE ||
1417 1.192 ad *nffp == (fdfile_t *)newfdp->fd_dfdfile[i]);
1418 1.173 ad ff = *ffp;
1419 1.192 ad if (ff == NULL || (fp = ff->ff_file) == NULL) {
1420 1.192 ad /* Descriptor unused, or descriptor half open. */
1421 1.192 ad KASSERT(!fd_isused(newfdp, i));
1422 1.173 ad continue;
1423 1.173 ad }
1424 1.192 ad if (__predict_false(fp->f_type == DTYPE_KQUEUE)) {
1425 1.173 ad /* kqueue descriptors cannot be copied. */
1426 1.126 pk continue;
1427 1.173 ad }
1428 1.173 ad /* It's active: add a reference to the file. */
1429 1.173 ad mutex_enter(&fp->f_lock);
1430 1.173 ad fp->f_count++;
1431 1.173 ad mutex_exit(&fp->f_lock);
1432 1.192 ad
1433 1.192 ad /* Allocate an fdfile_t to represent it. */
1434 1.173 ad if (i >= NDFDFILE) {
1435 1.192 ad ff2 = pool_cache_get(fdfile_cache, PR_WAITOK);
1436 1.192 ad *nffp = ff2;
1437 1.192 ad } else {
1438 1.192 ad ff2 = newdt->dt_ff[i];
1439 1.173 ad }
1440 1.173 ad ff2->ff_file = fp;
1441 1.173 ad ff2->ff_exclose = ff->ff_exclose;
1442 1.182 matt ff2->ff_allocated = true;
1443 1.192 ad
1444 1.192 ad /* Fix up bitmaps. */
1445 1.192 ad j = i >> NDENTRYSHIFT;
1446 1.192 ad KASSERT((newfdp->fd_lomap[j] & (1 << (i & NDENTRYMASK))) == 0);
1447 1.192 ad newfdp->fd_lomap[j] |= 1 << (i & NDENTRYMASK);
1448 1.192 ad if (__predict_false(newfdp->fd_lomap[j] == ~0)) {
1449 1.192 ad KASSERT((newfdp->fd_himap[j >> NDENTRYSHIFT] &
1450 1.192 ad (1 << (j & NDENTRYMASK))) == 0);
1451 1.192 ad newfdp->fd_himap[j >> NDENTRYSHIFT] |=
1452 1.192 ad 1 << (j & NDENTRYMASK);
1453 1.173 ad }
1454 1.192 ad newlast = i;
1455 1.173 ad }
1456 1.192 ad KASSERT(newdt->dt_ff[0] == (fdfile_t *)newfdp->fd_dfdfile[0]);
1457 1.192 ad newfdp->fd_lastfile = newlast;
1458 1.192 ad fd_checkmaps(newfdp);
1459 1.173 ad mutex_exit(&fdp->fd_lock);
1460 1.192 ad
1461 1.16 cgd return (newfdp);
1462 1.16 cgd }
1463 1.16 cgd
1464 1.16 cgd /*
1465 1.16 cgd * Release a filedesc structure.
1466 1.16 cgd */
1467 1.16 cgd void
1468 1.173 ad fd_free(void)
1469 1.16 cgd {
1470 1.173 ad fdfile_t *ff;
1471 1.173 ad file_t *fp;
1472 1.192 ad int fd, nf;
1473 1.192 ad fdtab_t *dt;
1474 1.197 yamt lwp_t * const l = curlwp;
1475 1.197 yamt filedesc_t * const fdp = l->l_fd;
1476 1.197 yamt const bool noadvlock = (l->l_proc->p_flag & PK_ADVLOCK) == 0;
1477 1.173 ad
1478 1.192 ad KASSERT(fdp->fd_dt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
1479 1.192 ad KASSERT(fdp->fd_dtbuiltin.dt_nfiles == NDFILE);
1480 1.192 ad KASSERT(fdp->fd_dtbuiltin.dt_link == NULL);
1481 1.16 cgd
1482 1.192 ad #ifndef __HAVE_ATOMIC_AS_MEMBAR
1483 1.192 ad membar_exit();
1484 1.192 ad #endif
1485 1.164 ad if (atomic_dec_uint_nv(&fdp->fd_refcnt) > 0)
1486 1.16 cgd return;
1487 1.126 pk
1488 1.16 cgd /*
1489 1.173 ad * Close any files that the process holds open.
1490 1.16 cgd */
1491 1.192 ad dt = fdp->fd_dt;
1492 1.192 ad fd_checkmaps(fdp);
1493 1.196 yamt #ifdef DEBUG
1494 1.196 yamt fdp->fd_refcnt = -1; /* see fd_checkmaps */
1495 1.196 yamt #endif
1496 1.192 ad for (fd = 0, nf = dt->dt_nfiles; fd < nf; fd++) {
1497 1.192 ad ff = dt->dt_ff[fd];
1498 1.173 ad KASSERT(fd >= NDFDFILE ||
1499 1.173 ad ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
1500 1.192 ad if (ff == NULL)
1501 1.173 ad continue;
1502 1.173 ad if ((fp = ff->ff_file) != NULL) {
1503 1.173 ad /*
1504 1.192 ad * Must use fd_close() here if there is
1505 1.197 yamt * a reference from kqueue or we might have posix
1506 1.197 yamt * advisory locks.
1507 1.173 ad */
1508 1.197 yamt if (__predict_true(ff->ff_refcnt == 0) &&
1509 1.197 yamt (noadvlock || fp->f_type != DTYPE_VNODE)) {
1510 1.192 ad ff->ff_file = NULL;
1511 1.192 ad ff->ff_exclose = false;
1512 1.192 ad ff->ff_allocated = false;
1513 1.192 ad closef(fp);
1514 1.192 ad } else {
1515 1.192 ad ff->ff_refcnt++;
1516 1.192 ad fd_close(fd);
1517 1.192 ad }
1518 1.173 ad }
1519 1.173 ad KASSERT(ff->ff_refcnt == 0);
1520 1.173 ad KASSERT(ff->ff_file == NULL);
1521 1.173 ad KASSERT(!ff->ff_exclose);
1522 1.173 ad KASSERT(!ff->ff_allocated);
1523 1.173 ad if (fd >= NDFDFILE) {
1524 1.173 ad pool_cache_put(fdfile_cache, ff);
1525 1.192 ad dt->dt_ff[fd] = NULL;
1526 1.173 ad }
1527 1.16 cgd }
1528 1.59 thorpej
1529 1.59 thorpej /*
1530 1.173 ad * Clean out the descriptor table for the next user and return
1531 1.173 ad * to the cache.
1532 1.59 thorpej */
1533 1.192 ad if (__predict_false(dt != &fdp->fd_dtbuiltin)) {
1534 1.192 ad fd_dtab_free(fdp->fd_dt);
1535 1.192 ad /* Otherwise, done above. */
1536 1.192 ad memset(&fdp->fd_dtbuiltin.dt_ff[NDFDFILE], 0,
1537 1.192 ad (NDFILE - NDFDFILE) * sizeof(fdp->fd_dtbuiltin.dt_ff[0]));
1538 1.192 ad fdp->fd_dt = &fdp->fd_dtbuiltin;
1539 1.59 thorpej }
1540 1.192 ad if (__predict_false(NDHISLOTS(nf) > NDHISLOTS(NDFILE))) {
1541 1.173 ad KASSERT(fdp->fd_himap != fdp->fd_dhimap);
1542 1.173 ad KASSERT(fdp->fd_lomap != fdp->fd_dlomap);
1543 1.192 ad fd_map_free(nf, fdp->fd_lomap, fdp->fd_himap);
1544 1.16 cgd }
1545 1.192 ad if (__predict_false(fdp->fd_knhash != NULL)) {
1546 1.179 ad hashdone(fdp->fd_knhash, HASH_LIST, fdp->fd_knhashmask);
1547 1.173 ad fdp->fd_knhash = NULL;
1548 1.173 ad fdp->fd_knhashmask = 0;
1549 1.173 ad } else {
1550 1.173 ad KASSERT(fdp->fd_knhashmask == 0);
1551 1.137 yamt }
1552 1.192 ad fdp->fd_dt = &fdp->fd_dtbuiltin;
1553 1.173 ad fdp->fd_lastkqfile = -1;
1554 1.192 ad fdp->fd_lastfile = -1;
1555 1.192 ad fdp->fd_freefile = 0;
1556 1.192 ad fdp->fd_exclose = false;
1557 1.192 ad memset(&fdp->fd_startzero, 0, sizeof(*fdp) -
1558 1.192 ad offsetof(filedesc_t, fd_startzero));
1559 1.195 yamt fdp->fd_himap = fdp->fd_dhimap;
1560 1.195 yamt fdp->fd_lomap = fdp->fd_dlomap;
1561 1.192 ad KASSERT(fdp->fd_dtbuiltin.dt_nfiles == NDFILE);
1562 1.192 ad KASSERT(fdp->fd_dtbuiltin.dt_link == NULL);
1563 1.192 ad KASSERT(fdp->fd_dt == &fdp->fd_dtbuiltin);
1564 1.196 yamt #ifdef DEBUG
1565 1.196 yamt fdp->fd_refcnt = 0; /* see fd_checkmaps */
1566 1.196 yamt #endif
1567 1.192 ad fd_checkmaps(fdp);
1568 1.173 ad pool_cache_put(filedesc_cache, fdp);
1569 1.170 martin }
1570 1.170 martin
1571 1.16 cgd /*
1572 1.16 cgd * File Descriptor pseudo-device driver (/dev/fd/).
1573 1.16 cgd *
1574 1.16 cgd * Opening minor device N dup()s the file (if any) connected to file
1575 1.16 cgd * descriptor N belonging to the calling process. Note that this driver
1576 1.16 cgd * consists of only the ``open()'' routine, because all subsequent
1577 1.16 cgd * references to this file will be direct to the other driver.
1578 1.16 cgd */
1579 1.134 thorpej static int
1580 1.173 ad filedescopen(dev_t dev, int mode, int type, lwp_t *l)
1581 1.16 cgd {
1582 1.16 cgd
1583 1.28 mycroft /*
1584 1.112 jdolecek * XXX Kludge: set dupfd to contain the value of the
1585 1.89 enami * the file descriptor being sought for duplication. The error
1586 1.28 mycroft * return ensures that the vnode for this device will be released
1587 1.28 mycroft * by vn_open. Open will detect this special error and take the
1588 1.28 mycroft * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1589 1.28 mycroft * will simply report the error.
1590 1.28 mycroft */
1591 1.138 christos l->l_dupfd = minor(dev); /* XXX */
1592 1.127 christos return EDUPFD;
1593 1.27 mycroft }
1594 1.27 mycroft
1595 1.28 mycroft /*
1596 1.28 mycroft * Duplicate the specified descriptor to a free descriptor.
1597 1.28 mycroft */
1598 1.27 mycroft int
1599 1.173 ad fd_dupopen(int old, int *new, int mode, int error)
1600 1.72 lukem {
1601 1.173 ad filedesc_t *fdp;
1602 1.173 ad fdfile_t *ff;
1603 1.173 ad file_t *fp;
1604 1.192 ad fdtab_t *dt;
1605 1.27 mycroft
1606 1.173 ad if ((fp = fd_getfile(old)) == NULL) {
1607 1.173 ad return EBADF;
1608 1.173 ad }
1609 1.173 ad fdp = curlwp->l_fd;
1610 1.192 ad dt = fdp->fd_dt;
1611 1.192 ad ff = dt->dt_ff[old];
1612 1.59 thorpej
1613 1.27 mycroft /*
1614 1.28 mycroft * There are two cases of interest here.
1615 1.28 mycroft *
1616 1.127 christos * For EDUPFD simply dup (dfd) to file descriptor
1617 1.28 mycroft * (indx) and return.
1618 1.28 mycroft *
1619 1.127 christos * For EMOVEFD steal away the file structure from (dfd) and
1620 1.28 mycroft * store it in (indx). (dfd) is effectively closed by
1621 1.28 mycroft * this operation.
1622 1.28 mycroft *
1623 1.28 mycroft * Any other error code is just returned.
1624 1.27 mycroft */
1625 1.28 mycroft switch (error) {
1626 1.127 christos case EDUPFD:
1627 1.28 mycroft /*
1628 1.28 mycroft * Check that the mode the file is being opened for is a
1629 1.28 mycroft * subset of the mode of the existing descriptor.
1630 1.28 mycroft */
1631 1.173 ad if (((mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
1632 1.173 ad error = EACCES;
1633 1.173 ad break;
1634 1.173 ad }
1635 1.173 ad
1636 1.173 ad /* Copy it. */
1637 1.192 ad error = fd_dup(fp, 0, new, ff->ff_exclose);
1638 1.173 ad break;
1639 1.27 mycroft
1640 1.127 christos case EMOVEFD:
1641 1.173 ad /* Copy it. */
1642 1.192 ad error = fd_dup(fp, 0, new, ff->ff_exclose);
1643 1.173 ad if (error != 0) {
1644 1.173 ad break;
1645 1.173 ad }
1646 1.16 cgd
1647 1.173 ad /* Steal away the file pointer from 'old'. */
1648 1.173 ad (void)fd_close(old);
1649 1.173 ad return 0;
1650 1.28 mycroft }
1651 1.173 ad
1652 1.173 ad fd_putfile(old);
1653 1.173 ad return error;
1654 1.61 wrstuden }
1655 1.61 wrstuden
1656 1.61 wrstuden /*
1657 1.113 jdolecek * Sets descriptor owner. If the owner is a process, 'pgid'
1658 1.113 jdolecek * is set to positive value, process ID. If the owner is process group,
1659 1.113 jdolecek * 'pgid' is set to -pg_id.
1660 1.113 jdolecek */
1661 1.113 jdolecek int
1662 1.180 gmcgarry fsetown(pid_t *pgid, u_long cmd, const void *data)
1663 1.113 jdolecek {
1664 1.133 christos int id = *(const int *)data;
1665 1.113 jdolecek int error;
1666 1.113 jdolecek
1667 1.113 jdolecek switch (cmd) {
1668 1.113 jdolecek case TIOCSPGRP:
1669 1.113 jdolecek if (id < 0)
1670 1.113 jdolecek return (EINVAL);
1671 1.113 jdolecek id = -id;
1672 1.113 jdolecek break;
1673 1.113 jdolecek default:
1674 1.113 jdolecek break;
1675 1.113 jdolecek }
1676 1.113 jdolecek
1677 1.113 jdolecek if (id > 0 && !pfind(id))
1678 1.113 jdolecek return (ESRCH);
1679 1.173 ad else if (id < 0 && (error = pgid_in_session(curproc, -id)))
1680 1.113 jdolecek return (error);
1681 1.113 jdolecek
1682 1.113 jdolecek *pgid = id;
1683 1.113 jdolecek return (0);
1684 1.113 jdolecek }
1685 1.113 jdolecek
1686 1.113 jdolecek /*
1687 1.113 jdolecek * Return descriptor owner information. If the value is positive,
1688 1.113 jdolecek * it's process ID. If it's negative, it's process group ID and
1689 1.113 jdolecek * needs the sign removed before use.
1690 1.113 jdolecek */
1691 1.113 jdolecek int
1692 1.180 gmcgarry fgetown(pid_t pgid, u_long cmd, void *data)
1693 1.113 jdolecek {
1694 1.173 ad
1695 1.113 jdolecek switch (cmd) {
1696 1.113 jdolecek case TIOCGPGRP:
1697 1.113 jdolecek *(int *)data = -pgid;
1698 1.113 jdolecek break;
1699 1.113 jdolecek default:
1700 1.113 jdolecek *(int *)data = pgid;
1701 1.113 jdolecek break;
1702 1.113 jdolecek }
1703 1.113 jdolecek return (0);
1704 1.113 jdolecek }
1705 1.113 jdolecek
1706 1.113 jdolecek /*
1707 1.113 jdolecek * Send signal to descriptor owner, either process or process group.
1708 1.113 jdolecek */
1709 1.113 jdolecek void
1710 1.114 christos fownsignal(pid_t pgid, int signo, int code, int band, void *fdescdata)
1711 1.113 jdolecek {
1712 1.131 perry ksiginfo_t ksi;
1713 1.113 jdolecek
1714 1.176 ad KASSERT(!cpu_intr_p());
1715 1.176 ad
1716 1.189 rmind if (pgid == 0) {
1717 1.189 rmind return;
1718 1.189 rmind }
1719 1.189 rmind
1720 1.148 yamt KSI_INIT(&ksi);
1721 1.114 christos ksi.ksi_signo = signo;
1722 1.113 jdolecek ksi.ksi_code = code;
1723 1.113 jdolecek ksi.ksi_band = band;
1724 1.113 jdolecek
1725 1.176 ad mutex_enter(proc_lock);
1726 1.189 rmind if (pgid > 0) {
1727 1.189 rmind struct proc *p1;
1728 1.189 rmind
1729 1.189 rmind p1 = p_find(pgid, PFIND_LOCKED);
1730 1.189 rmind if (p1 != NULL) {
1731 1.189 rmind kpsignal(p1, &ksi, fdescdata);
1732 1.189 rmind }
1733 1.189 rmind } else {
1734 1.189 rmind struct pgrp *pgrp;
1735 1.189 rmind
1736 1.189 rmind KASSERT(pgid < 0);
1737 1.189 rmind pgrp = pg_find(-pgid, PFIND_LOCKED);
1738 1.189 rmind if (pgrp != NULL) {
1739 1.189 rmind kpgsignal(pgrp, &ksi, fdescdata, 0);
1740 1.189 rmind }
1741 1.189 rmind }
1742 1.176 ad mutex_exit(proc_lock);
1743 1.113 jdolecek }
1744 1.127 christos
1745 1.127 christos int
1746 1.173 ad fd_clone(file_t *fp, unsigned fd, int flag, const struct fileops *fops,
1747 1.173 ad void *data)
1748 1.127 christos {
1749 1.173 ad
1750 1.130 christos fp->f_flag = flag;
1751 1.127 christos fp->f_type = DTYPE_MISC;
1752 1.127 christos fp->f_ops = fops;
1753 1.127 christos fp->f_data = data;
1754 1.173 ad curlwp->l_dupfd = fd;
1755 1.173 ad fd_affix(curproc, fp, fd);
1756 1.127 christos
1757 1.127 christos return EMOVEFD;
1758 1.127 christos }
1759 1.127 christos
1760 1.127 christos int
1761 1.173 ad fnullop_fcntl(file_t *fp, u_int cmd, void *data)
1762 1.127 christos {
1763 1.147 yamt
1764 1.127 christos if (cmd == F_SETFL)
1765 1.127 christos return 0;
1766 1.127 christos
1767 1.127 christos return EOPNOTSUPP;
1768 1.127 christos }
1769 1.127 christos
1770 1.127 christos int
1771 1.173 ad fnullop_poll(file_t *fp, int which)
1772 1.127 christos {
1773 1.147 yamt
1774 1.127 christos return 0;
1775 1.127 christos }
1776 1.127 christos
1777 1.127 christos int
1778 1.173 ad fnullop_kqfilter(file_t *fp, struct knote *kn)
1779 1.127 christos {
1780 1.127 christos
1781 1.127 christos return 0;
1782 1.127 christos }
1783 1.127 christos
1784 1.190 ad void
1785 1.190 ad fnullop_drain(file_t *fp)
1786 1.190 ad {
1787 1.190 ad
1788 1.190 ad }
1789 1.190 ad
1790 1.127 christos int
1791 1.173 ad fbadop_read(file_t *fp, off_t *offset, struct uio *uio,
1792 1.173 ad kauth_cred_t cred, int flags)
1793 1.160 rmind {
1794 1.160 rmind
1795 1.160 rmind return EOPNOTSUPP;
1796 1.160 rmind }
1797 1.160 rmind
1798 1.160 rmind int
1799 1.173 ad fbadop_write(file_t *fp, off_t *offset, struct uio *uio,
1800 1.173 ad kauth_cred_t cred, int flags)
1801 1.160 rmind {
1802 1.160 rmind
1803 1.160 rmind return EOPNOTSUPP;
1804 1.160 rmind }
1805 1.160 rmind
1806 1.160 rmind int
1807 1.173 ad fbadop_ioctl(file_t *fp, u_long com, void *data)
1808 1.160 rmind {
1809 1.160 rmind
1810 1.160 rmind return EOPNOTSUPP;
1811 1.160 rmind }
1812 1.160 rmind
1813 1.160 rmind int
1814 1.173 ad fbadop_stat(file_t *fp, struct stat *sb)
1815 1.127 christos {
1816 1.147 yamt
1817 1.127 christos return EOPNOTSUPP;
1818 1.127 christos }
1819 1.160 rmind
1820 1.160 rmind int
1821 1.173 ad fbadop_close(file_t *fp)
1822 1.160 rmind {
1823 1.160 rmind
1824 1.160 rmind return EOPNOTSUPP;
1825 1.160 rmind }
1826