Home | History | Annotate | Line # | Download | only in kern
kern_descrip.c revision 1.239
      1 /*	$NetBSD: kern_descrip.c,v 1.239 2018/11/02 12:27:47 maxv Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  * (c) UNIX System Laboratories, Inc.
     36  * All or some portions of this file are derived from material licensed
     37  * to the University of California by American Telephone and Telegraph
     38  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     39  * the permission of UNIX System Laboratories, Inc.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  * 3. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  *	@(#)kern_descrip.c	8.8 (Berkeley) 2/14/95
     66  */
     67 
     68 /*
     69  * File descriptor management.
     70  */
     71 
     72 #include <sys/cdefs.h>
     73 __KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.239 2018/11/02 12:27:47 maxv Exp $");
     74 
     75 #include <sys/param.h>
     76 #include <sys/systm.h>
     77 #include <sys/filedesc.h>
     78 #include <sys/kernel.h>
     79 #include <sys/proc.h>
     80 #include <sys/file.h>
     81 #include <sys/socket.h>
     82 #include <sys/socketvar.h>
     83 #include <sys/stat.h>
     84 #include <sys/ioctl.h>
     85 #include <sys/fcntl.h>
     86 #include <sys/pool.h>
     87 #include <sys/unistd.h>
     88 #include <sys/resourcevar.h>
     89 #include <sys/conf.h>
     90 #include <sys/event.h>
     91 #include <sys/kauth.h>
     92 #include <sys/atomic.h>
     93 #include <sys/syscallargs.h>
     94 #include <sys/cpu.h>
     95 #include <sys/kmem.h>
     96 #include <sys/vnode.h>
     97 #include <sys/sysctl.h>
     98 #include <sys/ktrace.h>
     99 
    100 /*
    101  * A list (head) of open files, counter, and lock protecting them.
    102  */
    103 struct filelist		filehead	__cacheline_aligned;
    104 static u_int		nfiles		__cacheline_aligned;
    105 kmutex_t		filelist_lock	__cacheline_aligned;
    106 
    107 static pool_cache_t	filedesc_cache	__read_mostly;
    108 static pool_cache_t	file_cache	__read_mostly;
    109 static pool_cache_t	fdfile_cache	__read_mostly;
    110 
    111 static int	file_ctor(void *, void *, int);
    112 static void	file_dtor(void *, void *);
    113 static int	fdfile_ctor(void *, void *, int);
    114 static void	fdfile_dtor(void *, void *);
    115 static int	filedesc_ctor(void *, void *, int);
    116 static void	filedesc_dtor(void *, void *);
    117 static int	filedescopen(dev_t, int, int, lwp_t *);
    118 
    119 static int sysctl_kern_file(SYSCTLFN_PROTO);
    120 static int sysctl_kern_file2(SYSCTLFN_PROTO);
    121 static void fill_file(struct kinfo_file *, const file_t *, const fdfile_t *,
    122 		      int, pid_t);
    123 
    124 const struct cdevsw filedesc_cdevsw = {
    125 	.d_open = filedescopen,
    126 	.d_close = noclose,
    127 	.d_read = noread,
    128 	.d_write = nowrite,
    129 	.d_ioctl = noioctl,
    130 	.d_stop = nostop,
    131 	.d_tty = notty,
    132 	.d_poll = nopoll,
    133 	.d_mmap = nommap,
    134 	.d_kqfilter = nokqfilter,
    135 	.d_discard = nodiscard,
    136 	.d_flag = D_OTHER | D_MPSAFE
    137 };
    138 
    139 /* For ease of reading. */
    140 __strong_alias(fd_putvnode,fd_putfile)
    141 __strong_alias(fd_putsock,fd_putfile)
    142 
    143 /*
    144  * Initialize the descriptor system.
    145  */
    146 void
    147 fd_sys_init(void)
    148 {
    149 	static struct sysctllog *clog;
    150 
    151 	mutex_init(&filelist_lock, MUTEX_DEFAULT, IPL_NONE);
    152 
    153 	LIST_INIT(&filehead);
    154 
    155 	file_cache = pool_cache_init(sizeof(file_t), coherency_unit, 0,
    156 	    0, "file", NULL, IPL_NONE, file_ctor, file_dtor, NULL);
    157 	KASSERT(file_cache != NULL);
    158 
    159 	fdfile_cache = pool_cache_init(sizeof(fdfile_t), coherency_unit, 0,
    160 	    PR_LARGECACHE, "fdfile", NULL, IPL_NONE, fdfile_ctor, fdfile_dtor,
    161 	    NULL);
    162 	KASSERT(fdfile_cache != NULL);
    163 
    164 	filedesc_cache = pool_cache_init(sizeof(filedesc_t), coherency_unit,
    165 	    0, 0, "filedesc", NULL, IPL_NONE, filedesc_ctor, filedesc_dtor,
    166 	    NULL);
    167 	KASSERT(filedesc_cache != NULL);
    168 
    169 	sysctl_createv(&clog, 0, NULL, NULL,
    170 		       CTLFLAG_PERMANENT,
    171 		       CTLTYPE_STRUCT, "file",
    172 		       SYSCTL_DESCR("System open file table"),
    173 		       sysctl_kern_file, 0, NULL, 0,
    174 		       CTL_KERN, KERN_FILE, CTL_EOL);
    175 	sysctl_createv(&clog, 0, NULL, NULL,
    176 		       CTLFLAG_PERMANENT,
    177 		       CTLTYPE_STRUCT, "file2",
    178 		       SYSCTL_DESCR("System open file table"),
    179 		       sysctl_kern_file2, 0, NULL, 0,
    180 		       CTL_KERN, KERN_FILE2, CTL_EOL);
    181 }
    182 
    183 static bool
    184 fd_isused(filedesc_t *fdp, unsigned fd)
    185 {
    186 	u_int off = fd >> NDENTRYSHIFT;
    187 
    188 	KASSERT(fd < fdp->fd_dt->dt_nfiles);
    189 
    190 	return (fdp->fd_lomap[off] & (1U << (fd & NDENTRYMASK))) != 0;
    191 }
    192 
    193 /*
    194  * Verify that the bitmaps match the descriptor table.
    195  */
    196 static inline void
    197 fd_checkmaps(filedesc_t *fdp)
    198 {
    199 #ifdef DEBUG
    200 	fdtab_t *dt;
    201 	u_int fd;
    202 
    203 	dt = fdp->fd_dt;
    204 	if (fdp->fd_refcnt == -1) {
    205 		/*
    206 		 * fd_free tears down the table without maintaining its bitmap.
    207 		 */
    208 		return;
    209 	}
    210 	for (fd = 0; fd < dt->dt_nfiles; fd++) {
    211 		if (fd < NDFDFILE) {
    212 			KASSERT(dt->dt_ff[fd] ==
    213 			    (fdfile_t *)fdp->fd_dfdfile[fd]);
    214 		}
    215 		if (dt->dt_ff[fd] == NULL) {
    216 			KASSERT(!fd_isused(fdp, fd));
    217 		} else if (dt->dt_ff[fd]->ff_file != NULL) {
    218 			KASSERT(fd_isused(fdp, fd));
    219 		}
    220 	}
    221 #endif
    222 }
    223 
    224 static int
    225 fd_next_zero(filedesc_t *fdp, uint32_t *bitmap, int want, u_int bits)
    226 {
    227 	int i, off, maxoff;
    228 	uint32_t sub;
    229 
    230 	KASSERT(mutex_owned(&fdp->fd_lock));
    231 
    232 	fd_checkmaps(fdp);
    233 
    234 	if (want > bits)
    235 		return -1;
    236 
    237 	off = want >> NDENTRYSHIFT;
    238 	i = want & NDENTRYMASK;
    239 	if (i) {
    240 		sub = bitmap[off] | ((u_int)~0 >> (NDENTRIES - i));
    241 		if (sub != ~0)
    242 			goto found;
    243 		off++;
    244 	}
    245 
    246 	maxoff = NDLOSLOTS(bits);
    247 	while (off < maxoff) {
    248 		if ((sub = bitmap[off]) != ~0)
    249 			goto found;
    250 		off++;
    251 	}
    252 
    253 	return -1;
    254 
    255  found:
    256 	return (off << NDENTRYSHIFT) + ffs(~sub) - 1;
    257 }
    258 
    259 static int
    260 fd_last_set(filedesc_t *fd, int last)
    261 {
    262 	int off, i;
    263 	fdfile_t **ff = fd->fd_dt->dt_ff;
    264 	uint32_t *bitmap = fd->fd_lomap;
    265 
    266 	KASSERT(mutex_owned(&fd->fd_lock));
    267 
    268 	fd_checkmaps(fd);
    269 
    270 	off = (last - 1) >> NDENTRYSHIFT;
    271 
    272 	while (off >= 0 && !bitmap[off])
    273 		off--;
    274 
    275 	if (off < 0)
    276 		return -1;
    277 
    278 	i = ((off + 1) << NDENTRYSHIFT) - 1;
    279 	if (i >= last)
    280 		i = last - 1;
    281 
    282 	/* XXX should use bitmap */
    283 	while (i > 0 && (ff[i] == NULL || !ff[i]->ff_allocated))
    284 		i--;
    285 
    286 	return i;
    287 }
    288 
    289 static inline void
    290 fd_used(filedesc_t *fdp, unsigned fd)
    291 {
    292 	u_int off = fd >> NDENTRYSHIFT;
    293 	fdfile_t *ff;
    294 
    295 	ff = fdp->fd_dt->dt_ff[fd];
    296 
    297 	KASSERT(mutex_owned(&fdp->fd_lock));
    298 	KASSERT((fdp->fd_lomap[off] & (1U << (fd & NDENTRYMASK))) == 0);
    299 	KASSERT(ff != NULL);
    300 	KASSERT(ff->ff_file == NULL);
    301 	KASSERT(!ff->ff_allocated);
    302 
    303 	ff->ff_allocated = true;
    304 	fdp->fd_lomap[off] |= 1U << (fd & NDENTRYMASK);
    305 	if (__predict_false(fdp->fd_lomap[off] == ~0)) {
    306 		KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
    307 		    (1U << (off & NDENTRYMASK))) == 0);
    308 		fdp->fd_himap[off >> NDENTRYSHIFT] |= 1U << (off & NDENTRYMASK);
    309 	}
    310 
    311 	if ((int)fd > fdp->fd_lastfile) {
    312 		fdp->fd_lastfile = fd;
    313 	}
    314 
    315 	fd_checkmaps(fdp);
    316 }
    317 
    318 static inline void
    319 fd_unused(filedesc_t *fdp, unsigned fd)
    320 {
    321 	u_int off = fd >> NDENTRYSHIFT;
    322 	fdfile_t *ff;
    323 
    324 	ff = fdp->fd_dt->dt_ff[fd];
    325 
    326 	/*
    327 	 * Don't assert the lock is held here, as we may be copying
    328 	 * the table during exec() and it is not needed there.
    329 	 * procfs and sysctl are locked out by proc::p_reflock.
    330 	 *
    331 	 * KASSERT(mutex_owned(&fdp->fd_lock));
    332 	 */
    333 	KASSERT(ff != NULL);
    334 	KASSERT(ff->ff_file == NULL);
    335 	KASSERT(ff->ff_allocated);
    336 
    337 	if (fd < fdp->fd_freefile) {
    338 		fdp->fd_freefile = fd;
    339 	}
    340 
    341 	if (fdp->fd_lomap[off] == ~0) {
    342 		KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
    343 		    (1U << (off & NDENTRYMASK))) != 0);
    344 		fdp->fd_himap[off >> NDENTRYSHIFT] &=
    345 		    ~(1U << (off & NDENTRYMASK));
    346 	}
    347 	KASSERT((fdp->fd_lomap[off] & (1U << (fd & NDENTRYMASK))) != 0);
    348 	fdp->fd_lomap[off] &= ~(1U << (fd & NDENTRYMASK));
    349 	ff->ff_allocated = false;
    350 
    351 	KASSERT(fd <= fdp->fd_lastfile);
    352 	if (fd == fdp->fd_lastfile) {
    353 		fdp->fd_lastfile = fd_last_set(fdp, fd);
    354 	}
    355 	fd_checkmaps(fdp);
    356 }
    357 
    358 /*
    359  * Look up the file structure corresponding to a file descriptor
    360  * and return the file, holding a reference on the descriptor.
    361  */
    362 file_t *
    363 fd_getfile(unsigned fd)
    364 {
    365 	filedesc_t *fdp;
    366 	fdfile_t *ff;
    367 	file_t *fp;
    368 	fdtab_t *dt;
    369 
    370 	/*
    371 	 * Look up the fdfile structure representing this descriptor.
    372 	 * We are doing this unlocked.  See fd_tryexpand().
    373 	 */
    374 	fdp = curlwp->l_fd;
    375 	dt = fdp->fd_dt;
    376 	if (__predict_false(fd >= dt->dt_nfiles)) {
    377 		return NULL;
    378 	}
    379 	ff = dt->dt_ff[fd];
    380 	KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
    381 	if (__predict_false(ff == NULL)) {
    382 		return NULL;
    383 	}
    384 
    385 	/* Now get a reference to the descriptor. */
    386 	if (fdp->fd_refcnt == 1) {
    387 		/*
    388 		 * Single threaded: don't need to worry about concurrent
    389 		 * access (other than earlier calls to kqueue, which may
    390 		 * hold a reference to the descriptor).
    391 		 */
    392 		ff->ff_refcnt++;
    393 	} else {
    394 		/*
    395 		 * Multi threaded: issue a memory barrier to ensure that we
    396 		 * acquire the file pointer _after_ adding a reference.  If
    397 		 * no memory barrier, we could fetch a stale pointer.
    398 		 */
    399 		atomic_inc_uint(&ff->ff_refcnt);
    400 #ifndef __HAVE_ATOMIC_AS_MEMBAR
    401 		membar_enter();
    402 #endif
    403 	}
    404 
    405 	/*
    406 	 * If the file is not open or is being closed then put the
    407 	 * reference back.
    408 	 */
    409 	fp = ff->ff_file;
    410 	if (__predict_true(fp != NULL)) {
    411 		return fp;
    412 	}
    413 	fd_putfile(fd);
    414 	return NULL;
    415 }
    416 
    417 /*
    418  * Release a reference to a file descriptor acquired with fd_getfile().
    419  */
    420 void
    421 fd_putfile(unsigned fd)
    422 {
    423 	filedesc_t *fdp;
    424 	fdfile_t *ff;
    425 	u_int u, v;
    426 
    427 	fdp = curlwp->l_fd;
    428 	ff = fdp->fd_dt->dt_ff[fd];
    429 
    430 	KASSERT(fd < fdp->fd_dt->dt_nfiles);
    431 	KASSERT(ff != NULL);
    432 	KASSERT((ff->ff_refcnt & FR_MASK) > 0);
    433 	KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
    434 
    435 	if (fdp->fd_refcnt == 1) {
    436 		/*
    437 		 * Single threaded: don't need to worry about concurrent
    438 		 * access (other than earlier calls to kqueue, which may
    439 		 * hold a reference to the descriptor).
    440 		 */
    441 		if (__predict_false((ff->ff_refcnt & FR_CLOSING) != 0)) {
    442 			fd_close(fd);
    443 			return;
    444 		}
    445 		ff->ff_refcnt--;
    446 		return;
    447 	}
    448 
    449 	/*
    450 	 * Ensure that any use of the file is complete and globally
    451 	 * visible before dropping the final reference.  If no membar,
    452 	 * the current CPU could still access memory associated with
    453 	 * the file after it has been freed or recycled by another
    454 	 * CPU.
    455 	 */
    456 #ifndef __HAVE_ATOMIC_AS_MEMBAR
    457 	membar_exit();
    458 #endif
    459 
    460 	/*
    461 	 * Be optimistic and start out with the assumption that no other
    462 	 * threads are trying to close the descriptor.  If the CAS fails,
    463 	 * we lost a race and/or it's being closed.
    464 	 */
    465 	for (u = ff->ff_refcnt & FR_MASK;; u = v) {
    466 		v = atomic_cas_uint(&ff->ff_refcnt, u, u - 1);
    467 		if (__predict_true(u == v)) {
    468 			return;
    469 		}
    470 		if (__predict_false((v & FR_CLOSING) != 0)) {
    471 			break;
    472 		}
    473 	}
    474 
    475 	/* Another thread is waiting to close the file: join it. */
    476 	(void)fd_close(fd);
    477 }
    478 
    479 /*
    480  * Convenience wrapper around fd_getfile() that returns reference
    481  * to a vnode.
    482  */
    483 int
    484 fd_getvnode(unsigned fd, file_t **fpp)
    485 {
    486 	vnode_t *vp;
    487 	file_t *fp;
    488 
    489 	fp = fd_getfile(fd);
    490 	if (__predict_false(fp == NULL)) {
    491 		return EBADF;
    492 	}
    493 	if (__predict_false(fp->f_type != DTYPE_VNODE)) {
    494 		fd_putfile(fd);
    495 		return EINVAL;
    496 	}
    497 	vp = fp->f_vnode;
    498 	if (__predict_false(vp->v_type == VBAD)) {
    499 		/* XXX Is this case really necessary? */
    500 		fd_putfile(fd);
    501 		return EBADF;
    502 	}
    503 	*fpp = fp;
    504 	return 0;
    505 }
    506 
    507 /*
    508  * Convenience wrapper around fd_getfile() that returns reference
    509  * to a socket.
    510  */
    511 int
    512 fd_getsock1(unsigned fd, struct socket **sop, file_t **fp)
    513 {
    514 	*fp = fd_getfile(fd);
    515 	if (__predict_false(*fp == NULL)) {
    516 		return EBADF;
    517 	}
    518 	if (__predict_false((*fp)->f_type != DTYPE_SOCKET)) {
    519 		fd_putfile(fd);
    520 		return ENOTSOCK;
    521 	}
    522 	*sop = (*fp)->f_socket;
    523 	return 0;
    524 }
    525 
    526 int
    527 fd_getsock(unsigned fd, struct socket **sop)
    528 {
    529 	file_t *fp;
    530 	return fd_getsock1(fd, sop, &fp);
    531 }
    532 
    533 /*
    534  * Look up the file structure corresponding to a file descriptor
    535  * and return it with a reference held on the file, not the
    536  * descriptor.
    537  *
    538  * This is heavyweight and only used when accessing descriptors
    539  * from a foreign process.  The caller must ensure that `p' does
    540  * not exit or fork across this call.
    541  *
    542  * To release the file (not descriptor) reference, use closef().
    543  */
    544 file_t *
    545 fd_getfile2(proc_t *p, unsigned fd)
    546 {
    547 	filedesc_t *fdp;
    548 	fdfile_t *ff;
    549 	file_t *fp;
    550 	fdtab_t *dt;
    551 
    552 	fdp = p->p_fd;
    553 	mutex_enter(&fdp->fd_lock);
    554 	dt = fdp->fd_dt;
    555 	if (fd >= dt->dt_nfiles) {
    556 		mutex_exit(&fdp->fd_lock);
    557 		return NULL;
    558 	}
    559 	if ((ff = dt->dt_ff[fd]) == NULL) {
    560 		mutex_exit(&fdp->fd_lock);
    561 		return NULL;
    562 	}
    563 	if ((fp = ff->ff_file) == NULL) {
    564 		mutex_exit(&fdp->fd_lock);
    565 		return NULL;
    566 	}
    567 	mutex_enter(&fp->f_lock);
    568 	fp->f_count++;
    569 	mutex_exit(&fp->f_lock);
    570 	mutex_exit(&fdp->fd_lock);
    571 
    572 	return fp;
    573 }
    574 
    575 /*
    576  * Internal form of close.  Must be called with a reference to the
    577  * descriptor, and will drop the reference.  When all descriptor
    578  * references are dropped, releases the descriptor slot and a single
    579  * reference to the file structure.
    580  */
    581 int
    582 fd_close(unsigned fd)
    583 {
    584 	struct flock lf;
    585 	filedesc_t *fdp;
    586 	fdfile_t *ff;
    587 	file_t *fp;
    588 	proc_t *p;
    589 	lwp_t *l;
    590 	u_int refcnt;
    591 
    592 	l = curlwp;
    593 	p = l->l_proc;
    594 	fdp = l->l_fd;
    595 	ff = fdp->fd_dt->dt_ff[fd];
    596 
    597 	KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
    598 
    599 	mutex_enter(&fdp->fd_lock);
    600 	KASSERT((ff->ff_refcnt & FR_MASK) > 0);
    601 	if (__predict_false(ff->ff_file == NULL)) {
    602 		/*
    603 		 * Another user of the file is already closing, and is
    604 		 * waiting for other users of the file to drain.  Release
    605 		 * our reference, and wake up the closer.
    606 		 */
    607 		atomic_dec_uint(&ff->ff_refcnt);
    608 		cv_broadcast(&ff->ff_closing);
    609 		mutex_exit(&fdp->fd_lock);
    610 
    611 		/*
    612 		 * An application error, so pretend that the descriptor
    613 		 * was already closed.  We can't safely wait for it to
    614 		 * be closed without potentially deadlocking.
    615 		 */
    616 		return (EBADF);
    617 	}
    618 	KASSERT((ff->ff_refcnt & FR_CLOSING) == 0);
    619 
    620 	/*
    621 	 * There may be multiple users of this file within the process.
    622 	 * Notify existing and new users that the file is closing.  This
    623 	 * will prevent them from adding additional uses to this file
    624 	 * while we are closing it.
    625 	 */
    626 	fp = ff->ff_file;
    627 	ff->ff_file = NULL;
    628 	ff->ff_exclose = false;
    629 
    630 	/*
    631 	 * We expect the caller to hold a descriptor reference - drop it.
    632 	 * The reference count may increase beyond zero at this point due
    633 	 * to an erroneous descriptor reference by an application, but
    634 	 * fd_getfile() will notice that the file is being closed and drop
    635 	 * the reference again.
    636 	 */
    637 	if (fdp->fd_refcnt == 1) {
    638 		/* Single threaded. */
    639 		refcnt = --(ff->ff_refcnt);
    640 	} else {
    641 		/* Multi threaded. */
    642 #ifndef __HAVE_ATOMIC_AS_MEMBAR
    643 		membar_producer();
    644 #endif
    645 		refcnt = atomic_dec_uint_nv(&ff->ff_refcnt);
    646 	}
    647 	if (__predict_false(refcnt != 0)) {
    648 		/*
    649 		 * Wait for other references to drain.  This is typically
    650 		 * an application error - the descriptor is being closed
    651 		 * while still in use.
    652 		 * (Or just a threaded application trying to unblock its
    653 		 * thread that sleeps in (say) accept()).
    654 		 */
    655 		atomic_or_uint(&ff->ff_refcnt, FR_CLOSING);
    656 
    657 		/*
    658 		 * Remove any knotes attached to the file.  A knote
    659 		 * attached to the descriptor can hold references on it.
    660 		 */
    661 		mutex_exit(&fdp->fd_lock);
    662 		if (!SLIST_EMPTY(&ff->ff_knlist)) {
    663 			knote_fdclose(fd);
    664 		}
    665 
    666 		/*
    667 		 * Since the file system code doesn't know which fd
    668 		 * each request came from (think dup()), we have to
    669 		 * ask it to return ERESTART for any long-term blocks.
    670 		 * The re-entry through read/write/etc will detect the
    671 		 * closed fd and return EBAFD.
    672 		 * Blocked partial writes may return a short length.
    673 		 */
    674 		(*fp->f_ops->fo_restart)(fp);
    675 		mutex_enter(&fdp->fd_lock);
    676 
    677 		/*
    678 		 * We need to see the count drop to zero at least once,
    679 		 * in order to ensure that all pre-existing references
    680 		 * have been drained.  New references past this point are
    681 		 * of no interest.
    682 		 * XXX (dsl) this may need to call fo_restart() after a
    683 		 * timeout to guarantee that all the system calls exit.
    684 		 */
    685 		while ((ff->ff_refcnt & FR_MASK) != 0) {
    686 			cv_wait(&ff->ff_closing, &fdp->fd_lock);
    687 		}
    688 		atomic_and_uint(&ff->ff_refcnt, ~FR_CLOSING);
    689 	} else {
    690 		/* If no references, there must be no knotes. */
    691 		KASSERT(SLIST_EMPTY(&ff->ff_knlist));
    692 	}
    693 
    694 	/*
    695 	 * POSIX record locking dictates that any close releases ALL
    696 	 * locks owned by this process.  This is handled by setting
    697 	 * a flag in the unlock to free ONLY locks obeying POSIX
    698 	 * semantics, and not to free BSD-style file locks.
    699 	 * If the descriptor was in a message, POSIX-style locks
    700 	 * aren't passed with the descriptor.
    701 	 */
    702 	if (__predict_false((p->p_flag & PK_ADVLOCK) != 0 &&
    703 	    fp->f_type == DTYPE_VNODE)) {
    704 		lf.l_whence = SEEK_SET;
    705 		lf.l_start = 0;
    706 		lf.l_len = 0;
    707 		lf.l_type = F_UNLCK;
    708 		mutex_exit(&fdp->fd_lock);
    709 		(void)VOP_ADVLOCK(fp->f_vnode, p, F_UNLCK, &lf, F_POSIX);
    710 		mutex_enter(&fdp->fd_lock);
    711 	}
    712 
    713 	/* Free descriptor slot. */
    714 	fd_unused(fdp, fd);
    715 	mutex_exit(&fdp->fd_lock);
    716 
    717 	/* Now drop reference to the file itself. */
    718 	return closef(fp);
    719 }
    720 
    721 /*
    722  * Duplicate a file descriptor.
    723  */
    724 int
    725 fd_dup(file_t *fp, int minfd, int *newp, bool exclose)
    726 {
    727 	proc_t *p = curproc;
    728 	int error;
    729 
    730 	while ((error = fd_alloc(p, minfd, newp)) != 0) {
    731 		if (error != ENOSPC) {
    732 			return error;
    733 		}
    734 		fd_tryexpand(p);
    735 	}
    736 
    737 	curlwp->l_fd->fd_dt->dt_ff[*newp]->ff_exclose = exclose;
    738 	fd_affix(p, fp, *newp);
    739 	return 0;
    740 }
    741 
    742 /*
    743  * dup2 operation.
    744  */
    745 int
    746 fd_dup2(file_t *fp, unsigned newfd, int flags)
    747 {
    748 	filedesc_t *fdp = curlwp->l_fd;
    749 	fdfile_t *ff;
    750 	fdtab_t *dt;
    751 
    752 	if (flags & ~(O_CLOEXEC|O_NONBLOCK))
    753 		return EINVAL;
    754 	/*
    755 	 * Ensure there are enough slots in the descriptor table,
    756 	 * and allocate an fdfile_t up front in case we need it.
    757 	 */
    758 	while (newfd >= fdp->fd_dt->dt_nfiles) {
    759 		fd_tryexpand(curproc);
    760 	}
    761 	ff = pool_cache_get(fdfile_cache, PR_WAITOK);
    762 
    763 	/*
    764 	 * If there is already a file open, close it.  If the file is
    765 	 * half open, wait for it to be constructed before closing it.
    766 	 * XXX Potential for deadlock here?
    767 	 */
    768 	mutex_enter(&fdp->fd_lock);
    769 	while (fd_isused(fdp, newfd)) {
    770 		mutex_exit(&fdp->fd_lock);
    771 		if (fd_getfile(newfd) != NULL) {
    772 			(void)fd_close(newfd);
    773 		} else {
    774 			/*
    775 			 * Crummy, but unlikely to happen.
    776 			 * Can occur if we interrupt another
    777 			 * thread while it is opening a file.
    778 			 */
    779 			kpause("dup2", false, 1, NULL);
    780 		}
    781 		mutex_enter(&fdp->fd_lock);
    782 	}
    783 	dt = fdp->fd_dt;
    784 	if (dt->dt_ff[newfd] == NULL) {
    785 		KASSERT(newfd >= NDFDFILE);
    786 		dt->dt_ff[newfd] = ff;
    787 		ff = NULL;
    788 	}
    789 	fd_used(fdp, newfd);
    790 	mutex_exit(&fdp->fd_lock);
    791 
    792 	dt->dt_ff[newfd]->ff_exclose = (flags & O_CLOEXEC) != 0;
    793 	fp->f_flag |= flags & FNONBLOCK;
    794 	/* Slot is now allocated.  Insert copy of the file. */
    795 	fd_affix(curproc, fp, newfd);
    796 	if (ff != NULL) {
    797 		pool_cache_put(fdfile_cache, ff);
    798 	}
    799 	return 0;
    800 }
    801 
    802 /*
    803  * Drop reference to a file structure.
    804  */
    805 int
    806 closef(file_t *fp)
    807 {
    808 	struct flock lf;
    809 	int error;
    810 
    811 	/*
    812 	 * Drop reference.  If referenced elsewhere it's still open
    813 	 * and we have nothing more to do.
    814 	 */
    815 	mutex_enter(&fp->f_lock);
    816 	KASSERT(fp->f_count > 0);
    817 	if (--fp->f_count > 0) {
    818 		mutex_exit(&fp->f_lock);
    819 		return 0;
    820 	}
    821 	KASSERT(fp->f_count == 0);
    822 	mutex_exit(&fp->f_lock);
    823 
    824 	/* We held the last reference - release locks, close and free. */
    825 	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
    826 		lf.l_whence = SEEK_SET;
    827 		lf.l_start = 0;
    828 		lf.l_len = 0;
    829 		lf.l_type = F_UNLCK;
    830 		(void)VOP_ADVLOCK(fp->f_vnode, fp, F_UNLCK, &lf, F_FLOCK);
    831 	}
    832 	if (fp->f_ops != NULL) {
    833 		error = (*fp->f_ops->fo_close)(fp);
    834 	} else {
    835 		error = 0;
    836 	}
    837 	KASSERT(fp->f_count == 0);
    838 	KASSERT(fp->f_cred != NULL);
    839 	pool_cache_put(file_cache, fp);
    840 
    841 	return error;
    842 }
    843 
    844 /*
    845  * Allocate a file descriptor for the process.
    846  */
    847 int
    848 fd_alloc(proc_t *p, int want, int *result)
    849 {
    850 	filedesc_t *fdp = p->p_fd;
    851 	int i, lim, last, error, hi;
    852 	u_int off;
    853 	fdtab_t *dt;
    854 
    855 	KASSERT(p == curproc || p == &proc0);
    856 
    857 	/*
    858 	 * Search for a free descriptor starting at the higher
    859 	 * of want or fd_freefile.
    860 	 */
    861 	mutex_enter(&fdp->fd_lock);
    862 	fd_checkmaps(fdp);
    863 	dt = fdp->fd_dt;
    864 	KASSERT(dt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
    865 	lim = uimin((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    866 	last = uimin(dt->dt_nfiles, lim);
    867 	for (;;) {
    868 		if ((i = want) < fdp->fd_freefile)
    869 			i = fdp->fd_freefile;
    870 		off = i >> NDENTRYSHIFT;
    871 		hi = fd_next_zero(fdp, fdp->fd_himap, off,
    872 		    (last + NDENTRIES - 1) >> NDENTRYSHIFT);
    873 		if (hi == -1)
    874 			break;
    875 		i = fd_next_zero(fdp, &fdp->fd_lomap[hi],
    876 		    hi > off ? 0 : i & NDENTRYMASK, NDENTRIES);
    877 		if (i == -1) {
    878 			/*
    879 			 * Free file descriptor in this block was
    880 			 * below want, try again with higher want.
    881 			 */
    882 			want = (hi + 1) << NDENTRYSHIFT;
    883 			continue;
    884 		}
    885 		i += (hi << NDENTRYSHIFT);
    886 		if (i >= last) {
    887 			break;
    888 		}
    889 		if (dt->dt_ff[i] == NULL) {
    890 			KASSERT(i >= NDFDFILE);
    891 			dt->dt_ff[i] = pool_cache_get(fdfile_cache, PR_WAITOK);
    892 		}
    893 		KASSERT(dt->dt_ff[i]->ff_file == NULL);
    894 		fd_used(fdp, i);
    895 		if (want <= fdp->fd_freefile) {
    896 			fdp->fd_freefile = i;
    897 		}
    898 		*result = i;
    899 		KASSERT(i >= NDFDFILE ||
    900 		    dt->dt_ff[i] == (fdfile_t *)fdp->fd_dfdfile[i]);
    901 		fd_checkmaps(fdp);
    902 		mutex_exit(&fdp->fd_lock);
    903 		return 0;
    904 	}
    905 
    906 	/* No space in current array.  Let the caller expand and retry. */
    907 	error = (dt->dt_nfiles >= lim) ? EMFILE : ENOSPC;
    908 	mutex_exit(&fdp->fd_lock);
    909 	return error;
    910 }
    911 
    912 /*
    913  * Allocate memory for a descriptor table.
    914  */
    915 static fdtab_t *
    916 fd_dtab_alloc(int n)
    917 {
    918 	fdtab_t *dt;
    919 	size_t sz;
    920 
    921 	KASSERT(n > NDFILE);
    922 
    923 	sz = sizeof(*dt) + (n - NDFILE) * sizeof(dt->dt_ff[0]);
    924 	dt = kmem_alloc(sz, KM_SLEEP);
    925 #ifdef DIAGNOSTIC
    926 	memset(dt, 0xff, sz);
    927 #endif
    928 	dt->dt_nfiles = n;
    929 	dt->dt_link = NULL;
    930 	return dt;
    931 }
    932 
    933 /*
    934  * Free a descriptor table, and all tables linked for deferred free.
    935  */
    936 static void
    937 fd_dtab_free(fdtab_t *dt)
    938 {
    939 	fdtab_t *next;
    940 	size_t sz;
    941 
    942 	do {
    943 		next = dt->dt_link;
    944 		KASSERT(dt->dt_nfiles > NDFILE);
    945 		sz = sizeof(*dt) +
    946 		    (dt->dt_nfiles - NDFILE) * sizeof(dt->dt_ff[0]);
    947 #ifdef DIAGNOSTIC
    948 		memset(dt, 0xff, sz);
    949 #endif
    950 		kmem_free(dt, sz);
    951 		dt = next;
    952 	} while (dt != NULL);
    953 }
    954 
    955 /*
    956  * Allocate descriptor bitmap.
    957  */
    958 static void
    959 fd_map_alloc(int n, uint32_t **lo, uint32_t **hi)
    960 {
    961 	uint8_t *ptr;
    962 	size_t szlo, szhi;
    963 
    964 	KASSERT(n > NDENTRIES);
    965 
    966 	szlo = NDLOSLOTS(n) * sizeof(uint32_t);
    967 	szhi = NDHISLOTS(n) * sizeof(uint32_t);
    968 	ptr = kmem_alloc(szlo + szhi, KM_SLEEP);
    969 	*lo = (uint32_t *)ptr;
    970 	*hi = (uint32_t *)(ptr + szlo);
    971 }
    972 
    973 /*
    974  * Free descriptor bitmap.
    975  */
    976 static void
    977 fd_map_free(int n, uint32_t *lo, uint32_t *hi)
    978 {
    979 	size_t szlo, szhi;
    980 
    981 	KASSERT(n > NDENTRIES);
    982 
    983 	szlo = NDLOSLOTS(n) * sizeof(uint32_t);
    984 	szhi = NDHISLOTS(n) * sizeof(uint32_t);
    985 	KASSERT(hi == (uint32_t *)((uint8_t *)lo + szlo));
    986 	kmem_free(lo, szlo + szhi);
    987 }
    988 
    989 /*
    990  * Expand a process' descriptor table.
    991  */
    992 void
    993 fd_tryexpand(proc_t *p)
    994 {
    995 	filedesc_t *fdp;
    996 	int i, numfiles, oldnfiles;
    997 	fdtab_t *newdt, *dt;
    998 	uint32_t *newhimap, *newlomap;
    999 
   1000 	KASSERT(p == curproc || p == &proc0);
   1001 
   1002 	fdp = p->p_fd;
   1003 	newhimap = NULL;
   1004 	newlomap = NULL;
   1005 	oldnfiles = fdp->fd_dt->dt_nfiles;
   1006 
   1007 	if (oldnfiles < NDEXTENT)
   1008 		numfiles = NDEXTENT;
   1009 	else
   1010 		numfiles = 2 * oldnfiles;
   1011 
   1012 	newdt = fd_dtab_alloc(numfiles);
   1013 	if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
   1014 		fd_map_alloc(numfiles, &newlomap, &newhimap);
   1015 	}
   1016 
   1017 	mutex_enter(&fdp->fd_lock);
   1018 	dt = fdp->fd_dt;
   1019 	KASSERT(dt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
   1020 	if (dt->dt_nfiles != oldnfiles) {
   1021 		/* fdp changed; caller must retry */
   1022 		mutex_exit(&fdp->fd_lock);
   1023 		fd_dtab_free(newdt);
   1024 		if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
   1025 			fd_map_free(numfiles, newlomap, newhimap);
   1026 		}
   1027 		return;
   1028 	}
   1029 
   1030 	/* Copy the existing descriptor table and zero the new portion. */
   1031 	i = sizeof(fdfile_t *) * oldnfiles;
   1032 	memcpy(newdt->dt_ff, dt->dt_ff, i);
   1033 	memset((uint8_t *)newdt->dt_ff + i, 0,
   1034 	    numfiles * sizeof(fdfile_t *) - i);
   1035 
   1036 	/*
   1037 	 * Link old descriptor array into list to be discarded.  We defer
   1038 	 * freeing until the last reference to the descriptor table goes
   1039 	 * away (usually process exit).  This allows us to do lockless
   1040 	 * lookups in fd_getfile().
   1041 	 */
   1042 	if (oldnfiles > NDFILE) {
   1043 		if (fdp->fd_refcnt > 1) {
   1044 			newdt->dt_link = dt;
   1045 		} else {
   1046 			fd_dtab_free(dt);
   1047 		}
   1048 	}
   1049 
   1050 	if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
   1051 		i = NDHISLOTS(oldnfiles) * sizeof(uint32_t);
   1052 		memcpy(newhimap, fdp->fd_himap, i);
   1053 		memset((uint8_t *)newhimap + i, 0,
   1054 		    NDHISLOTS(numfiles) * sizeof(uint32_t) - i);
   1055 
   1056 		i = NDLOSLOTS(oldnfiles) * sizeof(uint32_t);
   1057 		memcpy(newlomap, fdp->fd_lomap, i);
   1058 		memset((uint8_t *)newlomap + i, 0,
   1059 		    NDLOSLOTS(numfiles) * sizeof(uint32_t) - i);
   1060 
   1061 		if (NDHISLOTS(oldnfiles) > NDHISLOTS(NDFILE)) {
   1062 			fd_map_free(oldnfiles, fdp->fd_lomap, fdp->fd_himap);
   1063 		}
   1064 		fdp->fd_himap = newhimap;
   1065 		fdp->fd_lomap = newlomap;
   1066 	}
   1067 
   1068 	/*
   1069 	 * All other modifications must become globally visible before
   1070 	 * the change to fd_dt.  See fd_getfile().
   1071 	 */
   1072 	membar_producer();
   1073 	fdp->fd_dt = newdt;
   1074 	KASSERT(newdt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
   1075 	fd_checkmaps(fdp);
   1076 	mutex_exit(&fdp->fd_lock);
   1077 }
   1078 
   1079 /*
   1080  * Create a new open file structure and allocate a file descriptor
   1081  * for the current process.
   1082  */
   1083 int
   1084 fd_allocfile(file_t **resultfp, int *resultfd)
   1085 {
   1086 	proc_t *p = curproc;
   1087 	kauth_cred_t cred;
   1088 	file_t *fp;
   1089 	int error;
   1090 
   1091 	while ((error = fd_alloc(p, 0, resultfd)) != 0) {
   1092 		if (error != ENOSPC) {
   1093 			return error;
   1094 		}
   1095 		fd_tryexpand(p);
   1096 	}
   1097 
   1098 	fp = pool_cache_get(file_cache, PR_WAITOK);
   1099 	if (fp == NULL) {
   1100 		fd_abort(p, NULL, *resultfd);
   1101 		return ENFILE;
   1102 	}
   1103 	KASSERT(fp->f_count == 0);
   1104 	KASSERT(fp->f_msgcount == 0);
   1105 	KASSERT(fp->f_unpcount == 0);
   1106 
   1107 	/* Replace cached credentials if not what we need. */
   1108 	cred = curlwp->l_cred;
   1109 	if (__predict_false(cred != fp->f_cred)) {
   1110 		kauth_cred_free(fp->f_cred);
   1111 		kauth_cred_hold(cred);
   1112 		fp->f_cred = cred;
   1113 	}
   1114 
   1115 	/*
   1116 	 * Don't allow recycled files to be scanned.
   1117 	 * See uipc_usrreq.c.
   1118 	 */
   1119 	if (__predict_false((fp->f_flag & FSCAN) != 0)) {
   1120 		mutex_enter(&fp->f_lock);
   1121 		atomic_and_uint(&fp->f_flag, ~FSCAN);
   1122 		mutex_exit(&fp->f_lock);
   1123 	}
   1124 
   1125 	fp->f_advice = 0;
   1126 	fp->f_offset = 0;
   1127 	*resultfp = fp;
   1128 
   1129 	return 0;
   1130 }
   1131 
   1132 /*
   1133  * Successful creation of a new descriptor: make visible to the process.
   1134  */
   1135 void
   1136 fd_affix(proc_t *p, file_t *fp, unsigned fd)
   1137 {
   1138 	fdfile_t *ff;
   1139 	filedesc_t *fdp;
   1140 
   1141 	KASSERT(p == curproc || p == &proc0);
   1142 
   1143 	/* Add a reference to the file structure. */
   1144 	mutex_enter(&fp->f_lock);
   1145 	fp->f_count++;
   1146 	mutex_exit(&fp->f_lock);
   1147 
   1148 	/*
   1149 	 * Insert the new file into the descriptor slot.
   1150 	 *
   1151 	 * The memory barriers provided by lock activity in this routine
   1152 	 * ensure that any updates to the file structure become globally
   1153 	 * visible before the file becomes visible to other LWPs in the
   1154 	 * current process.
   1155 	 */
   1156 	fdp = p->p_fd;
   1157 	ff = fdp->fd_dt->dt_ff[fd];
   1158 
   1159 	KASSERT(ff != NULL);
   1160 	KASSERT(ff->ff_file == NULL);
   1161 	KASSERT(ff->ff_allocated);
   1162 	KASSERT(fd_isused(fdp, fd));
   1163 	KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
   1164 
   1165 	/* No need to lock in order to make file initially visible. */
   1166 	ff->ff_file = fp;
   1167 }
   1168 
   1169 /*
   1170  * Abort creation of a new descriptor: free descriptor slot and file.
   1171  */
   1172 void
   1173 fd_abort(proc_t *p, file_t *fp, unsigned fd)
   1174 {
   1175 	filedesc_t *fdp;
   1176 	fdfile_t *ff;
   1177 
   1178 	KASSERT(p == curproc || p == &proc0);
   1179 
   1180 	fdp = p->p_fd;
   1181 	ff = fdp->fd_dt->dt_ff[fd];
   1182 	ff->ff_exclose = false;
   1183 
   1184 	KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
   1185 
   1186 	mutex_enter(&fdp->fd_lock);
   1187 	KASSERT(fd_isused(fdp, fd));
   1188 	fd_unused(fdp, fd);
   1189 	mutex_exit(&fdp->fd_lock);
   1190 
   1191 	if (fp != NULL) {
   1192 		KASSERT(fp->f_count == 0);
   1193 		KASSERT(fp->f_cred != NULL);
   1194 		pool_cache_put(file_cache, fp);
   1195 	}
   1196 }
   1197 
   1198 static int
   1199 file_ctor(void *arg, void *obj, int flags)
   1200 {
   1201 	file_t *fp = obj;
   1202 
   1203 	memset(fp, 0, sizeof(*fp));
   1204 
   1205 	mutex_enter(&filelist_lock);
   1206 	if (__predict_false(nfiles >= maxfiles)) {
   1207 		mutex_exit(&filelist_lock);
   1208 		tablefull("file", "increase kern.maxfiles or MAXFILES");
   1209 		return ENFILE;
   1210 	}
   1211 	nfiles++;
   1212 	LIST_INSERT_HEAD(&filehead, fp, f_list);
   1213 	mutex_init(&fp->f_lock, MUTEX_DEFAULT, IPL_NONE);
   1214 	fp->f_cred = curlwp->l_cred;
   1215 	kauth_cred_hold(fp->f_cred);
   1216 	mutex_exit(&filelist_lock);
   1217 
   1218 	return 0;
   1219 }
   1220 
   1221 static void
   1222 file_dtor(void *arg, void *obj)
   1223 {
   1224 	file_t *fp = obj;
   1225 
   1226 	mutex_enter(&filelist_lock);
   1227 	nfiles--;
   1228 	LIST_REMOVE(fp, f_list);
   1229 	mutex_exit(&filelist_lock);
   1230 
   1231 	kauth_cred_free(fp->f_cred);
   1232 	mutex_destroy(&fp->f_lock);
   1233 }
   1234 
   1235 static int
   1236 fdfile_ctor(void *arg, void *obj, int flags)
   1237 {
   1238 	fdfile_t *ff = obj;
   1239 
   1240 	memset(ff, 0, sizeof(*ff));
   1241 	cv_init(&ff->ff_closing, "fdclose");
   1242 
   1243 	return 0;
   1244 }
   1245 
   1246 static void
   1247 fdfile_dtor(void *arg, void *obj)
   1248 {
   1249 	fdfile_t *ff = obj;
   1250 
   1251 	cv_destroy(&ff->ff_closing);
   1252 }
   1253 
   1254 file_t *
   1255 fgetdummy(void)
   1256 {
   1257 	file_t *fp;
   1258 
   1259 	fp = kmem_zalloc(sizeof(*fp), KM_SLEEP);
   1260 	mutex_init(&fp->f_lock, MUTEX_DEFAULT, IPL_NONE);
   1261 	return fp;
   1262 }
   1263 
   1264 void
   1265 fputdummy(file_t *fp)
   1266 {
   1267 
   1268 	mutex_destroy(&fp->f_lock);
   1269 	kmem_free(fp, sizeof(*fp));
   1270 }
   1271 
   1272 /*
   1273  * Create an initial filedesc structure.
   1274  */
   1275 filedesc_t *
   1276 fd_init(filedesc_t *fdp)
   1277 {
   1278 #ifdef DIAGNOSTIC
   1279 	unsigned fd;
   1280 #endif
   1281 
   1282 	if (__predict_true(fdp == NULL)) {
   1283 		fdp = pool_cache_get(filedesc_cache, PR_WAITOK);
   1284 	} else {
   1285 		KASSERT(fdp == &filedesc0);
   1286 		filedesc_ctor(NULL, fdp, PR_WAITOK);
   1287 	}
   1288 
   1289 #ifdef DIAGNOSTIC
   1290 	KASSERT(fdp->fd_lastfile == -1);
   1291 	KASSERT(fdp->fd_lastkqfile == -1);
   1292 	KASSERT(fdp->fd_knhash == NULL);
   1293 	KASSERT(fdp->fd_freefile == 0);
   1294 	KASSERT(fdp->fd_exclose == false);
   1295 	KASSERT(fdp->fd_dt == &fdp->fd_dtbuiltin);
   1296 	KASSERT(fdp->fd_dtbuiltin.dt_nfiles == NDFILE);
   1297 	for (fd = 0; fd < NDFDFILE; fd++) {
   1298 		KASSERT(fdp->fd_dtbuiltin.dt_ff[fd] ==
   1299 		    (fdfile_t *)fdp->fd_dfdfile[fd]);
   1300 	}
   1301 	for (fd = NDFDFILE; fd < NDFILE; fd++) {
   1302 		KASSERT(fdp->fd_dtbuiltin.dt_ff[fd] == NULL);
   1303 	}
   1304 	KASSERT(fdp->fd_himap == fdp->fd_dhimap);
   1305 	KASSERT(fdp->fd_lomap == fdp->fd_dlomap);
   1306 #endif	/* DIAGNOSTIC */
   1307 
   1308 	fdp->fd_refcnt = 1;
   1309 	fd_checkmaps(fdp);
   1310 
   1311 	return fdp;
   1312 }
   1313 
   1314 /*
   1315  * Initialize a file descriptor table.
   1316  */
   1317 static int
   1318 filedesc_ctor(void *arg, void *obj, int flag)
   1319 {
   1320 	filedesc_t *fdp = obj;
   1321 	fdfile_t **ffp;
   1322 	int i;
   1323 
   1324 	memset(fdp, 0, sizeof(*fdp));
   1325 	mutex_init(&fdp->fd_lock, MUTEX_DEFAULT, IPL_NONE);
   1326 	fdp->fd_lastfile = -1;
   1327 	fdp->fd_lastkqfile = -1;
   1328 	fdp->fd_dt = &fdp->fd_dtbuiltin;
   1329 	fdp->fd_dtbuiltin.dt_nfiles = NDFILE;
   1330 	fdp->fd_himap = fdp->fd_dhimap;
   1331 	fdp->fd_lomap = fdp->fd_dlomap;
   1332 
   1333 	CTASSERT(sizeof(fdp->fd_dfdfile[0]) >= sizeof(fdfile_t));
   1334 	for (i = 0, ffp = fdp->fd_dt->dt_ff; i < NDFDFILE; i++, ffp++) {
   1335 		*ffp = (fdfile_t *)fdp->fd_dfdfile[i];
   1336 		(void)fdfile_ctor(NULL, fdp->fd_dfdfile[i], PR_WAITOK);
   1337 	}
   1338 
   1339 	return 0;
   1340 }
   1341 
   1342 static void
   1343 filedesc_dtor(void *arg, void *obj)
   1344 {
   1345 	filedesc_t *fdp = obj;
   1346 	int i;
   1347 
   1348 	for (i = 0; i < NDFDFILE; i++) {
   1349 		fdfile_dtor(NULL, fdp->fd_dfdfile[i]);
   1350 	}
   1351 
   1352 	mutex_destroy(&fdp->fd_lock);
   1353 }
   1354 
   1355 /*
   1356  * Make p share curproc's filedesc structure.
   1357  */
   1358 void
   1359 fd_share(struct proc *p)
   1360 {
   1361 	filedesc_t *fdp;
   1362 
   1363 	fdp = curlwp->l_fd;
   1364 	p->p_fd = fdp;
   1365 	atomic_inc_uint(&fdp->fd_refcnt);
   1366 }
   1367 
   1368 /*
   1369  * Acquire a hold on a filedesc structure.
   1370  */
   1371 void
   1372 fd_hold(lwp_t *l)
   1373 {
   1374 	filedesc_t *fdp = l->l_fd;
   1375 
   1376 	atomic_inc_uint(&fdp->fd_refcnt);
   1377 }
   1378 
   1379 /*
   1380  * Copy a filedesc structure.
   1381  */
   1382 filedesc_t *
   1383 fd_copy(void)
   1384 {
   1385 	filedesc_t *newfdp, *fdp;
   1386 	fdfile_t *ff, **ffp, **nffp, *ff2;
   1387 	int i, j, numfiles, lastfile, newlast;
   1388 	file_t *fp;
   1389 	fdtab_t *newdt;
   1390 
   1391 	fdp = curproc->p_fd;
   1392 	newfdp = pool_cache_get(filedesc_cache, PR_WAITOK);
   1393 	newfdp->fd_refcnt = 1;
   1394 
   1395 #ifdef DIAGNOSTIC
   1396 	KASSERT(newfdp->fd_lastfile == -1);
   1397 	KASSERT(newfdp->fd_lastkqfile == -1);
   1398 	KASSERT(newfdp->fd_knhash == NULL);
   1399 	KASSERT(newfdp->fd_freefile == 0);
   1400 	KASSERT(newfdp->fd_exclose == false);
   1401 	KASSERT(newfdp->fd_dt == &newfdp->fd_dtbuiltin);
   1402 	KASSERT(newfdp->fd_dtbuiltin.dt_nfiles == NDFILE);
   1403 	for (i = 0; i < NDFDFILE; i++) {
   1404 		KASSERT(newfdp->fd_dtbuiltin.dt_ff[i] ==
   1405 		    (fdfile_t *)&newfdp->fd_dfdfile[i]);
   1406 	}
   1407 	for (i = NDFDFILE; i < NDFILE; i++) {
   1408 		KASSERT(newfdp->fd_dtbuiltin.dt_ff[i] == NULL);
   1409 	}
   1410 #endif	/* DIAGNOSTIC */
   1411 
   1412 	mutex_enter(&fdp->fd_lock);
   1413 	fd_checkmaps(fdp);
   1414 	numfiles = fdp->fd_dt->dt_nfiles;
   1415 	lastfile = fdp->fd_lastfile;
   1416 
   1417 	/*
   1418 	 * If the number of open files fits in the internal arrays
   1419 	 * of the open file structure, use them, otherwise allocate
   1420 	 * additional memory for the number of descriptors currently
   1421 	 * in use.
   1422 	 */
   1423 	if (lastfile < NDFILE) {
   1424 		i = NDFILE;
   1425 		newdt = newfdp->fd_dt;
   1426 		KASSERT(newfdp->fd_dt == &newfdp->fd_dtbuiltin);
   1427 	} else {
   1428 		/*
   1429 		 * Compute the smallest multiple of NDEXTENT needed
   1430 		 * for the file descriptors currently in use,
   1431 		 * allowing the table to shrink.
   1432 		 */
   1433 		i = numfiles;
   1434 		while (i >= 2 * NDEXTENT && i > lastfile * 2) {
   1435 			i /= 2;
   1436 		}
   1437 		KASSERT(i > NDFILE);
   1438 		newdt = fd_dtab_alloc(i);
   1439 		newfdp->fd_dt = newdt;
   1440 		memcpy(newdt->dt_ff, newfdp->fd_dtbuiltin.dt_ff,
   1441 		    NDFDFILE * sizeof(fdfile_t **));
   1442 		memset(newdt->dt_ff + NDFDFILE, 0,
   1443 		    (i - NDFDFILE) * sizeof(fdfile_t **));
   1444 	}
   1445 	if (NDHISLOTS(i) <= NDHISLOTS(NDFILE)) {
   1446 		newfdp->fd_himap = newfdp->fd_dhimap;
   1447 		newfdp->fd_lomap = newfdp->fd_dlomap;
   1448 	} else {
   1449 		fd_map_alloc(i, &newfdp->fd_lomap, &newfdp->fd_himap);
   1450 		KASSERT(i >= NDENTRIES * NDENTRIES);
   1451 		memset(newfdp->fd_himap, 0, NDHISLOTS(i)*sizeof(uint32_t));
   1452 		memset(newfdp->fd_lomap, 0, NDLOSLOTS(i)*sizeof(uint32_t));
   1453 	}
   1454 	newfdp->fd_freefile = fdp->fd_freefile;
   1455 	newfdp->fd_exclose = fdp->fd_exclose;
   1456 
   1457 	ffp = fdp->fd_dt->dt_ff;
   1458 	nffp = newdt->dt_ff;
   1459 	newlast = -1;
   1460 	for (i = 0; i <= lastfile; i++, ffp++, nffp++) {
   1461 		KASSERT(i >= NDFDFILE ||
   1462 		    *nffp == (fdfile_t *)newfdp->fd_dfdfile[i]);
   1463 		ff = *ffp;
   1464 		if (ff == NULL || (fp = ff->ff_file) == NULL) {
   1465 			/* Descriptor unused, or descriptor half open. */
   1466 			KASSERT(!fd_isused(newfdp, i));
   1467 			continue;
   1468 		}
   1469 		if (__predict_false(fp->f_type == DTYPE_KQUEUE)) {
   1470 			/* kqueue descriptors cannot be copied. */
   1471 			if (i < newfdp->fd_freefile) {
   1472 				newfdp->fd_freefile = i;
   1473 			}
   1474 			continue;
   1475 		}
   1476 		/* It's active: add a reference to the file. */
   1477 		mutex_enter(&fp->f_lock);
   1478 		fp->f_count++;
   1479 		mutex_exit(&fp->f_lock);
   1480 
   1481 		/* Allocate an fdfile_t to represent it. */
   1482 		if (i >= NDFDFILE) {
   1483 			ff2 = pool_cache_get(fdfile_cache, PR_WAITOK);
   1484 			*nffp = ff2;
   1485 		} else {
   1486 			ff2 = newdt->dt_ff[i];
   1487 		}
   1488 		ff2->ff_file = fp;
   1489 		ff2->ff_exclose = ff->ff_exclose;
   1490 		ff2->ff_allocated = true;
   1491 
   1492 		/* Fix up bitmaps. */
   1493 		j = i >> NDENTRYSHIFT;
   1494 		KASSERT((newfdp->fd_lomap[j] & (1U << (i & NDENTRYMASK))) == 0);
   1495 		newfdp->fd_lomap[j] |= 1U << (i & NDENTRYMASK);
   1496 		if (__predict_false(newfdp->fd_lomap[j] == ~0)) {
   1497 			KASSERT((newfdp->fd_himap[j >> NDENTRYSHIFT] &
   1498 			    (1U << (j & NDENTRYMASK))) == 0);
   1499 			newfdp->fd_himap[j >> NDENTRYSHIFT] |=
   1500 			    1U << (j & NDENTRYMASK);
   1501 		}
   1502 		newlast = i;
   1503 	}
   1504 	KASSERT(newdt->dt_ff[0] == (fdfile_t *)newfdp->fd_dfdfile[0]);
   1505 	newfdp->fd_lastfile = newlast;
   1506 	fd_checkmaps(newfdp);
   1507 	mutex_exit(&fdp->fd_lock);
   1508 
   1509 	return newfdp;
   1510 }
   1511 
   1512 /*
   1513  * Release a filedesc structure.
   1514  */
   1515 void
   1516 fd_free(void)
   1517 {
   1518 	fdfile_t *ff;
   1519 	file_t *fp;
   1520 	int fd, nf;
   1521 	fdtab_t *dt;
   1522 	lwp_t * const l = curlwp;
   1523 	filedesc_t * const fdp = l->l_fd;
   1524 	const bool noadvlock = (l->l_proc->p_flag & PK_ADVLOCK) == 0;
   1525 
   1526 	KASSERT(fdp->fd_dt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
   1527 	KASSERT(fdp->fd_dtbuiltin.dt_nfiles == NDFILE);
   1528 	KASSERT(fdp->fd_dtbuiltin.dt_link == NULL);
   1529 
   1530 #ifndef __HAVE_ATOMIC_AS_MEMBAR
   1531 	membar_exit();
   1532 #endif
   1533 	if (atomic_dec_uint_nv(&fdp->fd_refcnt) > 0)
   1534 		return;
   1535 
   1536 	/*
   1537 	 * Close any files that the process holds open.
   1538 	 */
   1539 	dt = fdp->fd_dt;
   1540 	fd_checkmaps(fdp);
   1541 #ifdef DEBUG
   1542 	fdp->fd_refcnt = -1; /* see fd_checkmaps */
   1543 #endif
   1544 	for (fd = 0, nf = dt->dt_nfiles; fd < nf; fd++) {
   1545 		ff = dt->dt_ff[fd];
   1546 		KASSERT(fd >= NDFDFILE ||
   1547 		    ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
   1548 		if (ff == NULL)
   1549 			continue;
   1550 		if ((fp = ff->ff_file) != NULL) {
   1551 			/*
   1552 			 * Must use fd_close() here if there is
   1553 			 * a reference from kqueue or we might have posix
   1554 			 * advisory locks.
   1555 			 */
   1556 			if (__predict_true(ff->ff_refcnt == 0) &&
   1557 			    (noadvlock || fp->f_type != DTYPE_VNODE)) {
   1558 				ff->ff_file = NULL;
   1559 				ff->ff_exclose = false;
   1560 				ff->ff_allocated = false;
   1561 				closef(fp);
   1562 			} else {
   1563 				ff->ff_refcnt++;
   1564 				fd_close(fd);
   1565 			}
   1566 		}
   1567 		KASSERT(ff->ff_refcnt == 0);
   1568 		KASSERT(ff->ff_file == NULL);
   1569 		KASSERT(!ff->ff_exclose);
   1570 		KASSERT(!ff->ff_allocated);
   1571 		if (fd >= NDFDFILE) {
   1572 			pool_cache_put(fdfile_cache, ff);
   1573 			dt->dt_ff[fd] = NULL;
   1574 		}
   1575 	}
   1576 
   1577 	/*
   1578 	 * Clean out the descriptor table for the next user and return
   1579 	 * to the cache.
   1580 	 */
   1581 	if (__predict_false(dt != &fdp->fd_dtbuiltin)) {
   1582 		fd_dtab_free(fdp->fd_dt);
   1583 		/* Otherwise, done above. */
   1584 		memset(&fdp->fd_dtbuiltin.dt_ff[NDFDFILE], 0,
   1585 		    (NDFILE - NDFDFILE) * sizeof(fdp->fd_dtbuiltin.dt_ff[0]));
   1586 		fdp->fd_dt = &fdp->fd_dtbuiltin;
   1587 	}
   1588 	if (__predict_false(NDHISLOTS(nf) > NDHISLOTS(NDFILE))) {
   1589 		KASSERT(fdp->fd_himap != fdp->fd_dhimap);
   1590 		KASSERT(fdp->fd_lomap != fdp->fd_dlomap);
   1591 		fd_map_free(nf, fdp->fd_lomap, fdp->fd_himap);
   1592 	}
   1593 	if (__predict_false(fdp->fd_knhash != NULL)) {
   1594 		hashdone(fdp->fd_knhash, HASH_LIST, fdp->fd_knhashmask);
   1595 		fdp->fd_knhash = NULL;
   1596 		fdp->fd_knhashmask = 0;
   1597 	} else {
   1598 		KASSERT(fdp->fd_knhashmask == 0);
   1599 	}
   1600 	fdp->fd_dt = &fdp->fd_dtbuiltin;
   1601 	fdp->fd_lastkqfile = -1;
   1602 	fdp->fd_lastfile = -1;
   1603 	fdp->fd_freefile = 0;
   1604 	fdp->fd_exclose = false;
   1605 	memset(&fdp->fd_startzero, 0, sizeof(*fdp) -
   1606 	    offsetof(filedesc_t, fd_startzero));
   1607 	fdp->fd_himap = fdp->fd_dhimap;
   1608 	fdp->fd_lomap = fdp->fd_dlomap;
   1609 	KASSERT(fdp->fd_dtbuiltin.dt_nfiles == NDFILE);
   1610 	KASSERT(fdp->fd_dtbuiltin.dt_link == NULL);
   1611 	KASSERT(fdp->fd_dt == &fdp->fd_dtbuiltin);
   1612 #ifdef DEBUG
   1613 	fdp->fd_refcnt = 0; /* see fd_checkmaps */
   1614 #endif
   1615 	fd_checkmaps(fdp);
   1616 	pool_cache_put(filedesc_cache, fdp);
   1617 }
   1618 
   1619 /*
   1620  * File Descriptor pseudo-device driver (/dev/fd/).
   1621  *
   1622  * Opening minor device N dup()s the file (if any) connected to file
   1623  * descriptor N belonging to the calling process.  Note that this driver
   1624  * consists of only the ``open()'' routine, because all subsequent
   1625  * references to this file will be direct to the other driver.
   1626  */
   1627 static int
   1628 filedescopen(dev_t dev, int mode, int type, lwp_t *l)
   1629 {
   1630 
   1631 	/*
   1632 	 * XXX Kludge: set dupfd to contain the value of the
   1633 	 * the file descriptor being sought for duplication. The error
   1634 	 * return ensures that the vnode for this device will be released
   1635 	 * by vn_open. Open will detect this special error and take the
   1636 	 * actions in fd_dupopen below. Other callers of vn_open or VOP_OPEN
   1637 	 * will simply report the error.
   1638 	 */
   1639 	l->l_dupfd = minor(dev);	/* XXX */
   1640 	return EDUPFD;
   1641 }
   1642 
   1643 /*
   1644  * Duplicate the specified descriptor to a free descriptor.
   1645  */
   1646 int
   1647 fd_dupopen(int old, int *newp, int mode, int error)
   1648 {
   1649 	filedesc_t *fdp;
   1650 	fdfile_t *ff;
   1651 	file_t *fp;
   1652 	fdtab_t *dt;
   1653 
   1654 	if ((fp = fd_getfile(old)) == NULL) {
   1655 		return EBADF;
   1656 	}
   1657 	fdp = curlwp->l_fd;
   1658 	dt = fdp->fd_dt;
   1659 	ff = dt->dt_ff[old];
   1660 
   1661 	/*
   1662 	 * There are two cases of interest here.
   1663 	 *
   1664 	 * For EDUPFD simply dup (old) to file descriptor
   1665 	 * (new) and return.
   1666 	 *
   1667 	 * For EMOVEFD steal away the file structure from (old) and
   1668 	 * store it in (new).  (old) is effectively closed by
   1669 	 * this operation.
   1670 	 *
   1671 	 * Any other error code is just returned.
   1672 	 */
   1673 	switch (error) {
   1674 	case EDUPFD:
   1675 		/*
   1676 		 * Check that the mode the file is being opened for is a
   1677 		 * subset of the mode of the existing descriptor.
   1678 		 */
   1679 		if (((mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
   1680 			error = EACCES;
   1681 			break;
   1682 		}
   1683 
   1684 		/* Copy it. */
   1685 		error = fd_dup(fp, 0, newp, ff->ff_exclose);
   1686 		break;
   1687 
   1688 	case EMOVEFD:
   1689 		/* Copy it. */
   1690 		error = fd_dup(fp, 0, newp, ff->ff_exclose);
   1691 		if (error != 0) {
   1692 			break;
   1693 		}
   1694 
   1695 		/* Steal away the file pointer from 'old'. */
   1696 		(void)fd_close(old);
   1697 		return 0;
   1698 	}
   1699 
   1700 	fd_putfile(old);
   1701 	return error;
   1702 }
   1703 
   1704 /*
   1705  * Close open files on exec.
   1706  */
   1707 void
   1708 fd_closeexec(void)
   1709 {
   1710 	proc_t *p;
   1711 	filedesc_t *fdp;
   1712 	fdfile_t *ff;
   1713 	lwp_t *l;
   1714 	fdtab_t *dt;
   1715 	int fd;
   1716 
   1717 	l = curlwp;
   1718 	p = l->l_proc;
   1719 	fdp = p->p_fd;
   1720 
   1721 	if (fdp->fd_refcnt > 1) {
   1722 		fdp = fd_copy();
   1723 		fd_free();
   1724 		p->p_fd = fdp;
   1725 		l->l_fd = fdp;
   1726 	}
   1727 	if (!fdp->fd_exclose) {
   1728 		return;
   1729 	}
   1730 	fdp->fd_exclose = false;
   1731 	dt = fdp->fd_dt;
   1732 
   1733 	for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
   1734 		if ((ff = dt->dt_ff[fd]) == NULL) {
   1735 			KASSERT(fd >= NDFDFILE);
   1736 			continue;
   1737 		}
   1738 		KASSERT(fd >= NDFDFILE ||
   1739 		    ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
   1740 		if (ff->ff_file == NULL)
   1741 			continue;
   1742 		if (ff->ff_exclose) {
   1743 			/*
   1744 			 * We need a reference to close the file.
   1745 			 * No other threads can see the fdfile_t at
   1746 			 * this point, so don't bother locking.
   1747 			 */
   1748 			KASSERT((ff->ff_refcnt & FR_CLOSING) == 0);
   1749 			ff->ff_refcnt++;
   1750 			fd_close(fd);
   1751 		}
   1752 	}
   1753 }
   1754 
   1755 /*
   1756  * Sets descriptor owner. If the owner is a process, 'pgid'
   1757  * is set to positive value, process ID. If the owner is process group,
   1758  * 'pgid' is set to -pg_id.
   1759  */
   1760 int
   1761 fsetown(pid_t *pgid, u_long cmd, const void *data)
   1762 {
   1763 	pid_t id = *(const pid_t *)data;
   1764 	int error;
   1765 
   1766 	switch (cmd) {
   1767 	case TIOCSPGRP:
   1768 		if (id < 0)
   1769 			return EINVAL;
   1770 		id = -id;
   1771 		break;
   1772 	default:
   1773 		break;
   1774 	}
   1775 	if (id > 0) {
   1776 		mutex_enter(proc_lock);
   1777 		error = proc_find(id) ? 0 : ESRCH;
   1778 		mutex_exit(proc_lock);
   1779 	} else if (id < 0) {
   1780 		error = pgid_in_session(curproc, -id);
   1781 	} else {
   1782 		error = 0;
   1783 	}
   1784 	if (!error) {
   1785 		*pgid = id;
   1786 	}
   1787 	return error;
   1788 }
   1789 
   1790 void
   1791 fd_set_exclose(struct lwp *l, int fd, bool exclose)
   1792 {
   1793 	filedesc_t *fdp = l->l_fd;
   1794 	fdfile_t *ff = fdp->fd_dt->dt_ff[fd];
   1795 
   1796 	ff->ff_exclose = exclose;
   1797 	if (exclose)
   1798 		fdp->fd_exclose = true;
   1799 }
   1800 
   1801 /*
   1802  * Return descriptor owner information. If the value is positive,
   1803  * it's process ID. If it's negative, it's process group ID and
   1804  * needs the sign removed before use.
   1805  */
   1806 int
   1807 fgetown(pid_t pgid, u_long cmd, void *data)
   1808 {
   1809 
   1810 	switch (cmd) {
   1811 	case TIOCGPGRP:
   1812 		*(int *)data = -pgid;
   1813 		break;
   1814 	default:
   1815 		*(int *)data = pgid;
   1816 		break;
   1817 	}
   1818 	return 0;
   1819 }
   1820 
   1821 /*
   1822  * Send signal to descriptor owner, either process or process group.
   1823  */
   1824 void
   1825 fownsignal(pid_t pgid, int signo, int code, int band, void *fdescdata)
   1826 {
   1827 	ksiginfo_t ksi;
   1828 
   1829 	KASSERT(!cpu_intr_p());
   1830 
   1831 	if (pgid == 0) {
   1832 		return;
   1833 	}
   1834 
   1835 	KSI_INIT(&ksi);
   1836 	ksi.ksi_signo = signo;
   1837 	ksi.ksi_code = code;
   1838 	ksi.ksi_band = band;
   1839 
   1840 	mutex_enter(proc_lock);
   1841 	if (pgid > 0) {
   1842 		struct proc *p1;
   1843 
   1844 		p1 = proc_find(pgid);
   1845 		if (p1 != NULL) {
   1846 			kpsignal(p1, &ksi, fdescdata);
   1847 		}
   1848 	} else {
   1849 		struct pgrp *pgrp;
   1850 
   1851 		KASSERT(pgid < 0);
   1852 		pgrp = pgrp_find(-pgid);
   1853 		if (pgrp != NULL) {
   1854 			kpgsignal(pgrp, &ksi, fdescdata, 0);
   1855 		}
   1856 	}
   1857 	mutex_exit(proc_lock);
   1858 }
   1859 
   1860 int
   1861 fd_clone(file_t *fp, unsigned fd, int flag, const struct fileops *fops,
   1862 	 void *data)
   1863 {
   1864 	fdfile_t *ff;
   1865 	filedesc_t *fdp;
   1866 
   1867 	fp->f_flag = flag & FMASK;
   1868 	fdp = curproc->p_fd;
   1869 	ff = fdp->fd_dt->dt_ff[fd];
   1870 	KASSERT(ff != NULL);
   1871 	ff->ff_exclose = (flag & O_CLOEXEC) != 0;
   1872 	fp->f_type = DTYPE_MISC;
   1873 	fp->f_ops = fops;
   1874 	fp->f_data = data;
   1875 	curlwp->l_dupfd = fd;
   1876 	fd_affix(curproc, fp, fd);
   1877 
   1878 	return EMOVEFD;
   1879 }
   1880 
   1881 int
   1882 fnullop_fcntl(file_t *fp, u_int cmd, void *data)
   1883 {
   1884 
   1885 	if (cmd == F_SETFL)
   1886 		return 0;
   1887 
   1888 	return EOPNOTSUPP;
   1889 }
   1890 
   1891 int
   1892 fnullop_poll(file_t *fp, int which)
   1893 {
   1894 
   1895 	return 0;
   1896 }
   1897 
   1898 int
   1899 fnullop_kqfilter(file_t *fp, struct knote *kn)
   1900 {
   1901 
   1902 	return EOPNOTSUPP;
   1903 }
   1904 
   1905 void
   1906 fnullop_restart(file_t *fp)
   1907 {
   1908 
   1909 }
   1910 
   1911 int
   1912 fbadop_read(file_t *fp, off_t *offset, struct uio *uio,
   1913 	    kauth_cred_t cred, int flags)
   1914 {
   1915 
   1916 	return EOPNOTSUPP;
   1917 }
   1918 
   1919 int
   1920 fbadop_write(file_t *fp, off_t *offset, struct uio *uio,
   1921 	     kauth_cred_t cred, int flags)
   1922 {
   1923 
   1924 	return EOPNOTSUPP;
   1925 }
   1926 
   1927 int
   1928 fbadop_ioctl(file_t *fp, u_long com, void *data)
   1929 {
   1930 
   1931 	return EOPNOTSUPP;
   1932 }
   1933 
   1934 int
   1935 fbadop_stat(file_t *fp, struct stat *sb)
   1936 {
   1937 
   1938 	return EOPNOTSUPP;
   1939 }
   1940 
   1941 int
   1942 fbadop_close(file_t *fp)
   1943 {
   1944 
   1945 	return EOPNOTSUPP;
   1946 }
   1947 
   1948 /*
   1949  * sysctl routines pertaining to file descriptors
   1950  */
   1951 
   1952 /* Initialized in sysctl_init() for now... */
   1953 extern kmutex_t sysctl_file_marker_lock;
   1954 static u_int sysctl_file_marker = 1;
   1955 
   1956 /*
   1957  * Expects to be called with proc_lock and sysctl_file_marker_lock locked.
   1958  */
   1959 static void
   1960 sysctl_file_marker_reset(void)
   1961 {
   1962 	struct proc *p;
   1963 
   1964 	PROCLIST_FOREACH(p, &allproc) {
   1965 		struct filedesc *fd = p->p_fd;
   1966 		fdtab_t *dt;
   1967 		u_int i;
   1968 
   1969 		mutex_enter(&fd->fd_lock);
   1970 		dt = fd->fd_dt;
   1971 		for (i = 0; i < dt->dt_nfiles; i++) {
   1972 			struct file *fp;
   1973 			fdfile_t *ff;
   1974 
   1975 			if ((ff = dt->dt_ff[i]) == NULL) {
   1976 				continue;
   1977 			}
   1978 			if ((fp = ff->ff_file) == NULL) {
   1979 				continue;
   1980 			}
   1981 			fp->f_marker = 0;
   1982 		}
   1983 		mutex_exit(&fd->fd_lock);
   1984 	}
   1985 }
   1986 
   1987 /*
   1988  * sysctl helper routine for kern.file pseudo-subtree.
   1989  */
   1990 static int
   1991 sysctl_kern_file(SYSCTLFN_ARGS)
   1992 {
   1993 	int error;
   1994 	size_t buflen;
   1995 	struct file *fp, fbuf;
   1996 	char *start, *where;
   1997 	struct proc *p;
   1998 
   1999 	start = where = oldp;
   2000 	buflen = *oldlenp;
   2001 
   2002 	if (where == NULL) {
   2003 		/*
   2004 		 * overestimate by 10 files
   2005 		 */
   2006 		*oldlenp = sizeof(filehead) + (nfiles + 10) *
   2007 		    sizeof(struct file);
   2008 		return 0;
   2009 	}
   2010 
   2011 	/*
   2012 	 * first sysctl_copyout filehead
   2013 	 */
   2014 	if (buflen < sizeof(filehead)) {
   2015 		*oldlenp = 0;
   2016 		return 0;
   2017 	}
   2018 	sysctl_unlock();
   2019 	error = sysctl_copyout(l, &filehead, where, sizeof(filehead));
   2020 	if (error) {
   2021 		sysctl_relock();
   2022 		return error;
   2023 	}
   2024 	buflen -= sizeof(filehead);
   2025 	where += sizeof(filehead);
   2026 
   2027 	/*
   2028 	 * followed by an array of file structures
   2029 	 */
   2030 	mutex_enter(&sysctl_file_marker_lock);
   2031 	mutex_enter(proc_lock);
   2032 	PROCLIST_FOREACH(p, &allproc) {
   2033 		struct filedesc *fd;
   2034 		fdtab_t *dt;
   2035 		u_int i;
   2036 
   2037 		if (p->p_stat == SIDL) {
   2038 			/* skip embryonic processes */
   2039 			continue;
   2040 		}
   2041 		mutex_enter(p->p_lock);
   2042 		error = kauth_authorize_process(l->l_cred,
   2043 		    KAUTH_PROCESS_CANSEE, p,
   2044 		    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES),
   2045 		    NULL, NULL);
   2046 		mutex_exit(p->p_lock);
   2047 		if (error != 0) {
   2048 			/*
   2049 			 * Don't leak kauth retval if we're silently
   2050 			 * skipping this entry.
   2051 			 */
   2052 			error = 0;
   2053 			continue;
   2054 		}
   2055 
   2056 		/*
   2057 		 * Grab a hold on the process.
   2058 		 */
   2059 		if (!rw_tryenter(&p->p_reflock, RW_READER)) {
   2060 			continue;
   2061 		}
   2062 		mutex_exit(proc_lock);
   2063 
   2064 		fd = p->p_fd;
   2065 		mutex_enter(&fd->fd_lock);
   2066 		dt = fd->fd_dt;
   2067 		for (i = 0; i < dt->dt_nfiles; i++) {
   2068 			fdfile_t *ff;
   2069 
   2070 			if ((ff = dt->dt_ff[i]) == NULL) {
   2071 				continue;
   2072 			}
   2073 			if ((fp = ff->ff_file) == NULL) {
   2074 				continue;
   2075 			}
   2076 
   2077 			mutex_enter(&fp->f_lock);
   2078 
   2079 			if ((fp->f_count == 0) ||
   2080 			    (fp->f_marker == sysctl_file_marker)) {
   2081 				mutex_exit(&fp->f_lock);
   2082 				continue;
   2083 			}
   2084 
   2085 			/* Check that we have enough space. */
   2086 			if (buflen < sizeof(struct file)) {
   2087 				*oldlenp = where - start;
   2088 				mutex_exit(&fp->f_lock);
   2089 				error = ENOMEM;
   2090 				break;
   2091 			}
   2092 
   2093 			memcpy(&fbuf, fp, sizeof(fbuf));
   2094 			mutex_exit(&fp->f_lock);
   2095 			error = sysctl_copyout(l, &fbuf, where, sizeof(fbuf));
   2096 			if (error) {
   2097 				break;
   2098 			}
   2099 			buflen -= sizeof(struct file);
   2100 			where += sizeof(struct file);
   2101 
   2102 			fp->f_marker = sysctl_file_marker;
   2103 		}
   2104 		mutex_exit(&fd->fd_lock);
   2105 
   2106 		/*
   2107 		 * Release reference to process.
   2108 		 */
   2109 		mutex_enter(proc_lock);
   2110 		rw_exit(&p->p_reflock);
   2111 
   2112 		if (error)
   2113 			break;
   2114 	}
   2115 
   2116 	sysctl_file_marker++;
   2117 	/* Reset all markers if wrapped. */
   2118 	if (sysctl_file_marker == 0) {
   2119 		sysctl_file_marker_reset();
   2120 		sysctl_file_marker++;
   2121 	}
   2122 
   2123 	mutex_exit(proc_lock);
   2124 	mutex_exit(&sysctl_file_marker_lock);
   2125 
   2126 	*oldlenp = where - start;
   2127 	sysctl_relock();
   2128 	return error;
   2129 }
   2130 
   2131 /*
   2132  * sysctl helper function for kern.file2
   2133  */
   2134 static int
   2135 sysctl_kern_file2(SYSCTLFN_ARGS)
   2136 {
   2137 	struct proc *p;
   2138 	struct file *fp;
   2139 	struct filedesc *fd;
   2140 	struct kinfo_file kf;
   2141 	char *dp;
   2142 	u_int i, op;
   2143 	size_t len, needed, elem_size, out_size;
   2144 	int error, arg, elem_count;
   2145 	fdfile_t *ff;
   2146 	fdtab_t *dt;
   2147 
   2148 	if (namelen == 1 && name[0] == CTL_QUERY)
   2149 		return sysctl_query(SYSCTLFN_CALL(rnode));
   2150 
   2151 	if (namelen != 4)
   2152 		return EINVAL;
   2153 
   2154 	error = 0;
   2155 	dp = oldp;
   2156 	len = (oldp != NULL) ? *oldlenp : 0;
   2157 	op = name[0];
   2158 	arg = name[1];
   2159 	elem_size = name[2];
   2160 	elem_count = name[3];
   2161 	out_size = MIN(sizeof(kf), elem_size);
   2162 	needed = 0;
   2163 
   2164 	if (elem_size < 1 || elem_count < 0)
   2165 		return EINVAL;
   2166 
   2167 	switch (op) {
   2168 	case KERN_FILE_BYFILE:
   2169 	case KERN_FILE_BYPID:
   2170 		/*
   2171 		 * We're traversing the process list in both cases; the BYFILE
   2172 		 * case does additional work of keeping track of files already
   2173 		 * looked at.
   2174 		 */
   2175 
   2176 		/* doesn't use arg so it must be zero */
   2177 		if ((op == KERN_FILE_BYFILE) && (arg != 0))
   2178 			return EINVAL;
   2179 
   2180 		if ((op == KERN_FILE_BYPID) && (arg < -1))
   2181 			/* -1 means all processes */
   2182 			return EINVAL;
   2183 
   2184 		sysctl_unlock();
   2185 		if (op == KERN_FILE_BYFILE)
   2186 			mutex_enter(&sysctl_file_marker_lock);
   2187 		mutex_enter(proc_lock);
   2188 		PROCLIST_FOREACH(p, &allproc) {
   2189 			if (p->p_stat == SIDL) {
   2190 				/* skip embryonic processes */
   2191 				continue;
   2192 			}
   2193 			if (arg > 0 && p->p_pid != arg) {
   2194 				/* pick only the one we want */
   2195 				/* XXX want 0 to mean "kernel files" */
   2196 				continue;
   2197 			}
   2198 			mutex_enter(p->p_lock);
   2199 			error = kauth_authorize_process(l->l_cred,
   2200 			    KAUTH_PROCESS_CANSEE, p,
   2201 			    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES),
   2202 			    NULL, NULL);
   2203 			mutex_exit(p->p_lock);
   2204 			if (error != 0) {
   2205 				/*
   2206 				 * Don't leak kauth retval if we're silently
   2207 				 * skipping this entry.
   2208 				 */
   2209 				error = 0;
   2210 				continue;
   2211 			}
   2212 
   2213 			/*
   2214 			 * Grab a hold on the process.
   2215 			 */
   2216 			if (!rw_tryenter(&p->p_reflock, RW_READER)) {
   2217 				continue;
   2218 			}
   2219 			mutex_exit(proc_lock);
   2220 
   2221 			fd = p->p_fd;
   2222 			mutex_enter(&fd->fd_lock);
   2223 			dt = fd->fd_dt;
   2224 			for (i = 0; i < dt->dt_nfiles; i++) {
   2225 				if ((ff = dt->dt_ff[i]) == NULL) {
   2226 					continue;
   2227 				}
   2228 				if ((fp = ff->ff_file) == NULL) {
   2229 					continue;
   2230 				}
   2231 
   2232 				if ((op == KERN_FILE_BYFILE) &&
   2233 				    (fp->f_marker == sysctl_file_marker)) {
   2234 					continue;
   2235 				}
   2236 				if (len >= elem_size && elem_count > 0) {
   2237 					mutex_enter(&fp->f_lock);
   2238 					fill_file(&kf, fp, ff, i, p->p_pid);
   2239 					mutex_exit(&fp->f_lock);
   2240 					mutex_exit(&fd->fd_lock);
   2241 					error = sysctl_copyout(l,
   2242 					    &kf, dp, out_size);
   2243 					mutex_enter(&fd->fd_lock);
   2244 					if (error)
   2245 						break;
   2246 					dp += elem_size;
   2247 					len -= elem_size;
   2248 				}
   2249 				if (op == KERN_FILE_BYFILE)
   2250 					fp->f_marker = sysctl_file_marker;
   2251 				needed += elem_size;
   2252 				if (elem_count > 0 && elem_count != INT_MAX)
   2253 					elem_count--;
   2254 			}
   2255 			mutex_exit(&fd->fd_lock);
   2256 
   2257 			/*
   2258 			 * Release reference to process.
   2259 			 */
   2260 			mutex_enter(proc_lock);
   2261 			rw_exit(&p->p_reflock);
   2262 		}
   2263 		if (op == KERN_FILE_BYFILE) {
   2264 			sysctl_file_marker++;
   2265 
   2266 			/* Reset all markers if wrapped. */
   2267 			if (sysctl_file_marker == 0) {
   2268 				sysctl_file_marker_reset();
   2269 				sysctl_file_marker++;
   2270 			}
   2271 		}
   2272 		mutex_exit(proc_lock);
   2273 		if (op == KERN_FILE_BYFILE)
   2274 			mutex_exit(&sysctl_file_marker_lock);
   2275 		sysctl_relock();
   2276 		break;
   2277 	default:
   2278 		return EINVAL;
   2279 	}
   2280 
   2281 	if (oldp == NULL)
   2282 		needed += KERN_FILESLOP * elem_size;
   2283 	*oldlenp = needed;
   2284 
   2285 	return error;
   2286 }
   2287 
   2288 static void
   2289 fill_file(struct kinfo_file *kp, const file_t *fp, const fdfile_t *ff,
   2290 	  int i, pid_t pid)
   2291 {
   2292 	const bool allowaddr = get_expose_address(curproc);
   2293 
   2294 	memset(kp, 0, sizeof(*kp));
   2295 
   2296 	COND_SET_VALUE(kp->ki_fileaddr, PTRTOUINT64(fp), allowaddr);
   2297 	kp->ki_flag =		fp->f_flag;
   2298 	kp->ki_iflags =		0;
   2299 	kp->ki_ftype =		fp->f_type;
   2300 	kp->ki_count =		fp->f_count;
   2301 	kp->ki_msgcount =	fp->f_msgcount;
   2302 	COND_SET_VALUE(kp->ki_fucred, PTRTOUINT64(fp->f_cred), allowaddr);
   2303 	kp->ki_fuid =		kauth_cred_geteuid(fp->f_cred);
   2304 	kp->ki_fgid =		kauth_cred_getegid(fp->f_cred);
   2305 	COND_SET_VALUE(kp->ki_fops, PTRTOUINT64(fp->f_ops), allowaddr);
   2306 	kp->ki_foffset =	fp->f_offset;
   2307 	COND_SET_VALUE(kp->ki_fdata, PTRTOUINT64(fp->f_data), allowaddr);
   2308 
   2309 	/* vnode information to glue this file to something */
   2310 	if (fp->f_type == DTYPE_VNODE) {
   2311 		struct vnode *vp = fp->f_vnode;
   2312 
   2313 		COND_SET_VALUE(kp->ki_vun, PTRTOUINT64(vp->v_un.vu_socket),
   2314 		    allowaddr);
   2315 		kp->ki_vsize =	vp->v_size;
   2316 		kp->ki_vtype =	vp->v_type;
   2317 		kp->ki_vtag =	vp->v_tag;
   2318 		COND_SET_VALUE(kp->ki_vdata, PTRTOUINT64(vp->v_data),
   2319 		    allowaddr);
   2320 	}
   2321 
   2322 	/* process information when retrieved via KERN_FILE_BYPID */
   2323 	if (ff != NULL) {
   2324 		kp->ki_pid =		pid;
   2325 		kp->ki_fd =		i;
   2326 		kp->ki_ofileflags =	ff->ff_exclose;
   2327 		kp->ki_usecount =	ff->ff_refcnt;
   2328 	}
   2329 }
   2330