Home | History | Annotate | Line # | Download | only in kern
kern_descrip.c revision 1.126
      1 /*	$NetBSD: kern_descrip.c,v 1.126 2004/05/31 15:30:55 pk Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)kern_descrip.c	8.8 (Berkeley) 2/14/95
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.126 2004/05/31 15:30:55 pk Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/filedesc.h>
     45 #include <sys/kernel.h>
     46 #include <sys/vnode.h>
     47 #include <sys/proc.h>
     48 #include <sys/file.h>
     49 #include <sys/namei.h>
     50 #include <sys/socket.h>
     51 #include <sys/socketvar.h>
     52 #include <sys/stat.h>
     53 #include <sys/ioctl.h>
     54 #include <sys/fcntl.h>
     55 #include <sys/malloc.h>
     56 #include <sys/pool.h>
     57 #include <sys/syslog.h>
     58 #include <sys/unistd.h>
     59 #include <sys/resourcevar.h>
     60 #include <sys/conf.h>
     61 #include <sys/event.h>
     62 
     63 #include <sys/mount.h>
     64 #include <sys/sa.h>
     65 #include <sys/syscallargs.h>
     66 
     67 /*
     68  * Descriptor management.
     69  */
     70 struct filelist	filehead;	/* head of list of open files */
     71 int		nfiles;		/* actual number of open files */
     72 POOL_INIT(file_pool, sizeof(struct file), 0, 0, 0, "filepl",
     73     &pool_allocator_nointr);
     74 POOL_INIT(cwdi_pool, sizeof(struct cwdinfo), 0, 0, 0, "cwdipl",
     75     &pool_allocator_nointr);
     76 POOL_INIT(filedesc0_pool, sizeof(struct filedesc0), 0, 0, 0, "fdescpl",
     77     &pool_allocator_nointr);
     78 
     79 /* Global file list lock */
     80 static struct simplelock filelist_slock = SIMPLELOCK_INITIALIZER;
     81 
     82 MALLOC_DEFINE(M_FILE, "file", "Open file structure");
     83 MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
     84 MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
     85 
     86 static __inline void	fd_used(struct filedesc *, int);
     87 static __inline void	fd_unused(struct filedesc *, int);
     88 static __inline int	find_next_zero(uint32_t *, int, u_int);
     89 int			finishdup(struct proc *, int, int, register_t *);
     90 int			find_last_set(struct filedesc *, int);
     91 int			fcntl_forfs(int, struct proc *, int, void *);
     92 
     93 dev_type_open(filedescopen);
     94 
     95 const struct cdevsw filedesc_cdevsw = {
     96 	filedescopen, noclose, noread, nowrite, noioctl,
     97 	nostop, notty, nopoll, nommap, nokqfilter,
     98 };
     99 
    100 static __inline int
    101 find_next_zero(uint32_t *bitmap, int want, u_int bits)
    102 {
    103 	int i, off, maxoff;
    104 	uint32_t sub;
    105 
    106 	if (want > bits)
    107 		return -1;
    108 
    109 	off = want >> NDENTRYSHIFT;
    110 	i = want & NDENTRYMASK;
    111 	if (i) {
    112 		sub = bitmap[off] | ((u_int)~0 >> (NDENTRIES - i));
    113 		if (sub != ~0)
    114 			goto found;
    115 		off++;
    116 	}
    117 
    118 	maxoff = NDLOSLOTS(bits);
    119 	while (off < maxoff) {
    120 		if ((sub = bitmap[off]) != ~0)
    121 			goto found;
    122 		off++;
    123 	}
    124 
    125 	return (-1);
    126 
    127  found:
    128 	return (off << NDENTRYSHIFT) + ffs(~sub) - 1;
    129 }
    130 
    131 int
    132 find_last_set(struct filedesc *fd, int last)
    133 {
    134 	int off, i;
    135 	struct file **ofiles = fd->fd_ofiles;
    136 	uint32_t *bitmap = fd->fd_lomap;
    137 
    138 	off = (last - 1) >> NDENTRYSHIFT;
    139 
    140 	while (off >= 0 && !bitmap[off])
    141 		off--;
    142 
    143 	if (off < 0)
    144 		return (0);
    145 
    146 	i = ((off + 1) << NDENTRYSHIFT) - 1;
    147 	if (i >= last)
    148 		i = last - 1;
    149 
    150 	while (i > 0 && ofiles[i] == NULL)
    151 		i--;
    152 
    153 	return (i);
    154 }
    155 
    156 static __inline void
    157 fd_used(struct filedesc *fdp, int fd)
    158 {
    159 	u_int off = fd >> NDENTRYSHIFT;
    160 
    161 	LOCK_ASSERT(simple_lock_held(&fdp->fd_slock));
    162 	KDASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) == 0);
    163 
    164 	fdp->fd_lomap[off] |= 1 << (fd & NDENTRYMASK);
    165 	if (fdp->fd_lomap[off] == ~0) {
    166 		KDASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
    167 		    (1 << (off & NDENTRYMASK))) == 0);
    168 		fdp->fd_himap[off >> NDENTRYSHIFT] |= 1 << (off & NDENTRYMASK);
    169 	}
    170 
    171 	if (fd > fdp->fd_lastfile)
    172 		fdp->fd_lastfile = fd;
    173 }
    174 
    175 static __inline void
    176 fd_unused(struct filedesc *fdp, int fd)
    177 {
    178 	u_int off = fd >> NDENTRYSHIFT;
    179 
    180 	LOCK_ASSERT(simple_lock_held(&fdp->fd_slock));
    181 	if (fd < fdp->fd_freefile)
    182 		fdp->fd_freefile = fd;
    183 
    184 	if (fdp->fd_lomap[off] == ~0) {
    185 		KDASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
    186 		    (1 << (off & NDENTRYMASK))) != 0);
    187 		fdp->fd_himap[off >> NDENTRYSHIFT] &=
    188 		    ~(1 << (off & NDENTRYMASK));
    189 	}
    190 	KDASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) != 0);
    191 	fdp->fd_lomap[off] &= ~(1 << (fd & NDENTRYMASK));
    192 
    193 #ifdef DIAGNOSTIC
    194 	if (fd > fdp->fd_lastfile)
    195 		panic("fd_unused: fd_lastfile inconsistent");
    196 #endif
    197 	if (fd == fdp->fd_lastfile)
    198 		fdp->fd_lastfile = find_last_set(fdp, fd);
    199 }
    200 
    201 /*
    202  * Lookup the file structure corresponding to a file descriptor
    203  * and return it locked.
    204  * Note: typical usage is: `fp = fd_getfile(..); FILE_USE(fp);'
    205  * The locking strategy has been optimised for this case, i.e.
    206  * fd_getfile() returns the file locked while FILE_USE() will increment
    207  * the file's use count and unlock.
    208  */
    209 struct file *
    210 fd_getfile(struct filedesc *fdp, int fd)
    211 {
    212 	struct file *fp;
    213 
    214 	if ((u_int) fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
    215 		return (NULL);
    216 
    217 	simple_lock(&fp->f_slock);
    218 	if (FILE_IS_USABLE(fp) == 0) {
    219 		simple_unlock(&fp->f_slock);
    220 		return (NULL);
    221 	}
    222 
    223 	return (fp);
    224 }
    225 
    226 /*
    227  * System calls on descriptors.
    228  */
    229 
    230 /*
    231  * Duplicate a file descriptor.
    232  */
    233 /* ARGSUSED */
    234 int
    235 sys_dup(struct lwp *l, void *v, register_t *retval)
    236 {
    237 	struct sys_dup_args /* {
    238 		syscallarg(int)	fd;
    239 	} */ *uap = v;
    240 	struct file	*fp;
    241 	struct filedesc	*fdp;
    242 	struct proc	*p;
    243 	int		old, new, error;
    244 
    245 	p = l->l_proc;
    246 	fdp = p->p_fd;
    247 	old = SCARG(uap, fd);
    248 
    249  restart:
    250 	if ((fp = fd_getfile(fdp, old)) == NULL)
    251 		return (EBADF);
    252 
    253 	FILE_USE(fp);
    254 
    255 	if ((error = fdalloc(p, 0, &new)) != 0) {
    256 		if (error == ENOSPC) {
    257 			fdexpand(p);
    258 			FILE_UNUSE(fp, p);
    259 			goto restart;
    260 		}
    261 		FILE_UNUSE(fp, p);
    262 		return (error);
    263 	}
    264 
    265 	/* finishdup() will unuse the descriptors for us */
    266 	return (finishdup(p, old, new, retval));
    267 }
    268 
    269 /*
    270  * Duplicate a file descriptor to a particular value.
    271  */
    272 /* ARGSUSED */
    273 int
    274 sys_dup2(struct lwp *l, void *v, register_t *retval)
    275 {
    276 	struct sys_dup2_args /* {
    277 		syscallarg(int)	from;
    278 		syscallarg(int)	to;
    279 	} */ *uap = v;
    280 	struct file	*fp;
    281 	struct filedesc	*fdp;
    282 	struct proc	*p;
    283 	int		old, new, i, error;
    284 
    285 	p = l->l_proc;
    286 	fdp = p->p_fd;
    287 	old = SCARG(uap, from);
    288 	new = SCARG(uap, to);
    289 
    290  restart:
    291 	if ((fp = fd_getfile(fdp, old)) == NULL)
    292 		return (EBADF);
    293 
    294 	if ((u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    295 	    (u_int)new >= maxfiles) {
    296 		simple_unlock(&fp->f_slock);
    297 		return (EBADF);
    298 	}
    299 
    300 	if (old == new) {
    301 		simple_unlock(&fp->f_slock);
    302 		*retval = new;
    303 		return (0);
    304 	}
    305 
    306 	FILE_USE(fp);
    307 
    308 	if (new >= fdp->fd_nfiles) {
    309 		if ((error = fdalloc(p, new, &i)) != 0) {
    310 			if (error == ENOSPC) {
    311 				fdexpand(p);
    312 				FILE_UNUSE(fp, p);
    313 				goto restart;
    314 			}
    315 			FILE_UNUSE(fp, p);
    316 			return (error);
    317 		}
    318 		if (new != i)
    319 			panic("dup2: fdalloc");
    320 	} else {
    321 		simple_lock(&fdp->fd_slock);
    322 		/*
    323 		 * Mark `new' slot "used" only if it was empty.
    324 		 */
    325 		if (fdp->fd_ofiles[new] == NULL)
    326 			fd_used(fdp, new);
    327 		simple_unlock(&fdp->fd_slock);
    328 	}
    329 
    330 	/*
    331 	 * finishdup() will close the file that's in the `new'
    332 	 * slot, if there's one there.
    333 	 */
    334 
    335 	/* finishdup() will unuse the descriptors for us */
    336 	return (finishdup(p, old, new, retval));
    337 }
    338 
    339 /*
    340  * The file control system call.
    341  */
    342 /* ARGSUSED */
    343 int
    344 sys_fcntl(struct lwp *l, void *v, register_t *retval)
    345 {
    346 	struct sys_fcntl_args /* {
    347 		syscallarg(int)		fd;
    348 		syscallarg(int)		cmd;
    349 		syscallarg(void *)	arg;
    350 	} */ *uap = v;
    351 	struct filedesc *fdp;
    352 	struct file	*fp;
    353 	struct proc	*p;
    354 	struct vnode	*vp;
    355 	int		fd, i, tmp, error, flg, cmd, newmin;
    356 	struct flock	fl;
    357 
    358 	p = l->l_proc;
    359 	fd = SCARG(uap, fd);
    360 	cmd = SCARG(uap, cmd);
    361 	fdp = p->p_fd;
    362 	error = 0;
    363 	flg = F_POSIX;
    364 
    365 	switch (cmd) {
    366 	case F_CLOSEM:
    367 		if (fd < 0)
    368 			return EBADF;
    369 		while (fdp->fd_lastfile >= fd)
    370 			fdrelease(p, fdp->fd_lastfile);
    371 		return 0;
    372 
    373 	case F_MAXFD:
    374 		*retval = fdp->fd_lastfile;
    375 		return 0;
    376 
    377 	default:
    378 		/* Handled below */
    379 		break;
    380 	}
    381 
    382  restart:
    383 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    384 		return (EBADF);
    385 
    386 	FILE_USE(fp);
    387 
    388 	if ((cmd & F_FSCTL)) {
    389 		error = fcntl_forfs(fd, p, cmd, SCARG(uap, arg));
    390 		goto out;
    391 	}
    392 
    393 	switch (cmd) {
    394 
    395 	case F_DUPFD:
    396 		newmin = (long)SCARG(uap, arg);
    397 		if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    398 		    (u_int)newmin >= maxfiles) {
    399 			error = EINVAL;
    400 			goto out;
    401 		}
    402 		if ((error = fdalloc(p, newmin, &i)) != 0) {
    403 			if (error == ENOSPC) {
    404 				fdexpand(p);
    405 				FILE_UNUSE(fp, p);
    406 				goto restart;
    407 			}
    408 			goto out;
    409 		}
    410 
    411 		/* finishdup() will unuse the descriptors for us */
    412 		return (finishdup(p, fd, i, retval));
    413 
    414 	case F_GETFD:
    415 		*retval = fdp->fd_ofileflags[fd] & UF_EXCLOSE ? 1 : 0;
    416 		break;
    417 
    418 	case F_SETFD:
    419 		if ((long)SCARG(uap, arg) & 1)
    420 			fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
    421 		else
    422 			fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
    423 		break;
    424 
    425 	case F_GETFL:
    426 		*retval = OFLAGS(fp->f_flag);
    427 		break;
    428 
    429 	case F_SETFL:
    430 		tmp = FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
    431 		error = (*fp->f_ops->fo_fcntl)(fp, F_SETFL, &tmp, p);
    432 		if (error)
    433 			break;
    434 		i = tmp ^ fp->f_flag;
    435 		if (i & FNONBLOCK) {
    436 			int fl = tmp & FNONBLOCK;
    437 			error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, &fl, p);
    438 			if (error)
    439 				goto reset_fcntl;
    440 		}
    441 		if (i & FASYNC) {
    442 			int fl = tmp & FASYNC;
    443 			error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, &fl, p);
    444 			if (error) {
    445 				if (i & FNONBLOCK) {
    446 					tmp = fp->f_flag & FNONBLOCK;
    447 					(void)(*fp->f_ops->fo_ioctl)(fp,
    448 						FIONBIO, &tmp, p);
    449 				}
    450 				goto reset_fcntl;
    451 			}
    452 		}
    453 		fp->f_flag = (fp->f_flag & ~FCNTLFLAGS) | tmp;
    454 		break;
    455 	    reset_fcntl:
    456 		(void)(*fp->f_ops->fo_fcntl)(fp, F_SETFL, &fp->f_flag, p);
    457 		break;
    458 
    459 	case F_GETOWN:
    460 		error = (*fp->f_ops->fo_ioctl)(fp, FIOGETOWN, retval, p);
    461 		break;
    462 
    463 	case F_SETOWN:
    464 		tmp = (int)(intptr_t) SCARG(uap, arg);
    465 		error = (*fp->f_ops->fo_ioctl)(fp, FIOSETOWN, &tmp, p);
    466 		break;
    467 
    468 	case F_SETLKW:
    469 		flg |= F_WAIT;
    470 		/* Fall into F_SETLK */
    471 
    472 	case F_SETLK:
    473 		if (fp->f_type != DTYPE_VNODE) {
    474 			error = EINVAL;
    475 			goto out;
    476 		}
    477 		vp = (struct vnode *)fp->f_data;
    478 		/* Copy in the lock structure */
    479 		error = copyin(SCARG(uap, arg), &fl, sizeof(fl));
    480 		if (error)
    481 			goto out;
    482 		if (fl.l_whence == SEEK_CUR)
    483 			fl.l_start += fp->f_offset;
    484 		switch (fl.l_type) {
    485 		case F_RDLCK:
    486 			if ((fp->f_flag & FREAD) == 0) {
    487 				error = EBADF;
    488 				goto out;
    489 			}
    490 			p->p_flag |= P_ADVLOCK;
    491 			error = VOP_ADVLOCK(vp, p, F_SETLK, &fl, flg);
    492 			goto out;
    493 
    494 		case F_WRLCK:
    495 			if ((fp->f_flag & FWRITE) == 0) {
    496 				error = EBADF;
    497 				goto out;
    498 			}
    499 			p->p_flag |= P_ADVLOCK;
    500 			error = VOP_ADVLOCK(vp, p, F_SETLK, &fl, flg);
    501 			goto out;
    502 
    503 		case F_UNLCK:
    504 			error = VOP_ADVLOCK(vp, p, F_UNLCK, &fl, F_POSIX);
    505 			goto out;
    506 
    507 		default:
    508 			error = EINVAL;
    509 			goto out;
    510 		}
    511 
    512 	case F_GETLK:
    513 		if (fp->f_type != DTYPE_VNODE) {
    514 			error = EINVAL;
    515 			goto out;
    516 		}
    517 		vp = (struct vnode *)fp->f_data;
    518 		/* Copy in the lock structure */
    519 		error = copyin(SCARG(uap, arg), &fl, sizeof(fl));
    520 		if (error)
    521 			goto out;
    522 		if (fl.l_whence == SEEK_CUR)
    523 			fl.l_start += fp->f_offset;
    524 		if (fl.l_type != F_RDLCK &&
    525 		    fl.l_type != F_WRLCK &&
    526 		    fl.l_type != F_UNLCK) {
    527 			error = EINVAL;
    528 			goto out;
    529 		}
    530 		error = VOP_ADVLOCK(vp, p, F_GETLK, &fl, F_POSIX);
    531 		if (error)
    532 			goto out;
    533 		error = copyout(&fl, SCARG(uap, arg), sizeof(fl));
    534 		break;
    535 
    536 	default:
    537 		error = EINVAL;
    538 	}
    539 
    540  out:
    541 	FILE_UNUSE(fp, p);
    542 	return (error);
    543 }
    544 
    545 /*
    546  * Common code for dup, dup2, and fcntl(F_DUPFD).
    547  */
    548 int
    549 finishdup(struct proc *p, int old, int new, register_t *retval)
    550 {
    551 	struct filedesc	*fdp;
    552 	struct file	*fp, *delfp;
    553 
    554 	fdp = p->p_fd;
    555 
    556 	/*
    557 	 * If there is a file in the new slot, remember it so we
    558 	 * can close it after we've finished the dup.  We need
    559 	 * to do it after the dup is finished, since closing
    560 	 * the file may block.
    561 	 *
    562 	 * Note: `old' is already used for us.
    563 	 * Note: Caller already marked `new' slot "used".
    564 	 */
    565 	simple_lock(&fdp->fd_slock);
    566 	delfp = fdp->fd_ofiles[new];
    567 
    568 	fp = fdp->fd_ofiles[old];
    569 	KDASSERT(fp != NULL);
    570 	fdp->fd_ofiles[new] = fp;
    571 	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
    572 	simple_unlock(&fdp->fd_slock);
    573 
    574 	*retval = new;
    575 	simple_lock(&fp->f_slock);
    576 	fp->f_count++;
    577 	FILE_UNUSE_HAVELOCK(fp, p);
    578 
    579 	if (delfp != NULL) {
    580 		simple_lock(&delfp->f_slock);
    581 		FILE_USE(delfp);
    582 		if (new < fdp->fd_knlistsize)
    583 			knote_fdclose(p, new);
    584 		(void) closef(delfp, p);
    585 	}
    586 	return (0);
    587 }
    588 
    589 void
    590 fdremove(struct filedesc *fdp, int fd)
    591 {
    592 
    593 	simple_lock(&fdp->fd_slock);
    594 	fdp->fd_ofiles[fd] = NULL;
    595 	fd_unused(fdp, fd);
    596 	simple_unlock(&fdp->fd_slock);
    597 }
    598 
    599 int
    600 fdrelease(struct proc *p, int fd)
    601 {
    602 	struct filedesc	*fdp;
    603 	struct file	**fpp, *fp;
    604 
    605 	fdp = p->p_fd;
    606 	simple_lock(&fdp->fd_slock);
    607 	if ((u_int) fd > fdp->fd_lastfile)
    608 		goto badf;
    609 	fpp = &fdp->fd_ofiles[fd];
    610 	fp = *fpp;
    611 	if (fp == NULL)
    612 		goto badf;
    613 
    614 	simple_lock(&fp->f_slock);
    615 	if (!FILE_IS_USABLE(fp)) {
    616 		simple_unlock(&fp->f_slock);
    617 		goto badf;
    618 	}
    619 
    620 	FILE_USE(fp);
    621 
    622 	*fpp = NULL;
    623 	fdp->fd_ofileflags[fd] = 0;
    624 	fd_unused(fdp, fd);
    625 	simple_unlock(&fdp->fd_slock);
    626 	if (fd < fdp->fd_knlistsize)
    627 		knote_fdclose(p, fd);
    628 	return (closef(fp, p));
    629 
    630 badf:
    631 	simple_unlock(&fdp->fd_slock);
    632 	return (EBADF);
    633 }
    634 
    635 /*
    636  * Close a file descriptor.
    637  */
    638 /* ARGSUSED */
    639 int
    640 sys_close(struct lwp *l, void *v, register_t *retval)
    641 {
    642 	struct sys_close_args /* {
    643 		syscallarg(int)	fd;
    644 	} */ *uap = v;
    645 	int		fd;
    646 	struct filedesc	*fdp;
    647 	struct proc *p;
    648 
    649 	p = l->l_proc;
    650 	fd = SCARG(uap, fd);
    651 	fdp = p->p_fd;
    652 
    653 #if 0
    654 	if (fd_getfile(fdp, fd) == NULL)
    655 		return (EBADF);
    656 #endif
    657 
    658 	return (fdrelease(p, fd));
    659 }
    660 
    661 /*
    662  * Return status information about a file descriptor.
    663  */
    664 /* ARGSUSED */
    665 int
    666 sys___fstat13(struct lwp *l, void *v, register_t *retval)
    667 {
    668 	struct sys___fstat13_args /* {
    669 		syscallarg(int)			fd;
    670 		syscallarg(struct stat *)	sb;
    671 	} */ *uap = v;
    672 	int		fd;
    673 	struct filedesc	*fdp;
    674 	struct file	*fp;
    675 	struct proc	*p;
    676 	struct stat	ub;
    677 	int		error;
    678 
    679 	p = l->l_proc;
    680 	fd = SCARG(uap, fd);
    681 	fdp = p->p_fd;
    682 
    683 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    684 		return (EBADF);
    685 
    686 	FILE_USE(fp);
    687 	error = (*fp->f_ops->fo_stat)(fp, &ub, p);
    688 	FILE_UNUSE(fp, p);
    689 
    690 	if (error == 0)
    691 		error = copyout(&ub, SCARG(uap, sb), sizeof(ub));
    692 
    693 	return (error);
    694 }
    695 
    696 /*
    697  * Return pathconf information about a file descriptor.
    698  */
    699 /* ARGSUSED */
    700 int
    701 sys_fpathconf(struct lwp *l, void *v, register_t *retval)
    702 {
    703 	struct sys_fpathconf_args /* {
    704 		syscallarg(int)	fd;
    705 		syscallarg(int)	name;
    706 	} */ *uap = v;
    707 	int		fd;
    708 	struct filedesc	*fdp;
    709 	struct file	*fp;
    710 	struct proc 	*p;
    711 	struct vnode	*vp;
    712 	int		error;
    713 
    714 	p = l->l_proc;
    715 	fd = SCARG(uap, fd);
    716 	fdp = p->p_fd;
    717 	error = 0;
    718 
    719 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    720 		return (EBADF);
    721 
    722 	FILE_USE(fp);
    723 
    724 	switch (fp->f_type) {
    725 
    726 	case DTYPE_SOCKET:
    727 	case DTYPE_PIPE:
    728 		if (SCARG(uap, name) != _PC_PIPE_BUF)
    729 			error = EINVAL;
    730 		else
    731 			*retval = PIPE_BUF;
    732 		break;
    733 
    734 	case DTYPE_VNODE:
    735 		vp = (struct vnode *)fp->f_data;
    736 		error = VOP_PATHCONF(vp, SCARG(uap, name), retval);
    737 		break;
    738 
    739 	case DTYPE_KQUEUE:
    740 		error = EINVAL;
    741 		break;
    742 
    743 	default:
    744 		error = EOPNOTSUPP;
    745 		break;
    746 	}
    747 
    748 	FILE_UNUSE(fp, p);
    749 	return (error);
    750 }
    751 
    752 /*
    753  * Allocate a file descriptor for the process.
    754  */
    755 int	fdexpanded;		/* XXX: what else uses this? */
    756 
    757 int
    758 fdalloc(struct proc *p, int want, int *result)
    759 {
    760 	struct filedesc	*fdp;
    761 	int i, lim, last, error;
    762 	u_int off, new;
    763 
    764 	fdp = p->p_fd;
    765 	simple_lock(&fdp->fd_slock);
    766 
    767 	/*
    768 	 * Search for a free descriptor starting at the higher
    769 	 * of want or fd_freefile.  If that fails, consider
    770 	 * expanding the ofile array.
    771 	 */
    772 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    773 	last = min(fdp->fd_nfiles, lim);
    774  again:
    775 	if ((i = want) < fdp->fd_freefile)
    776 		i = fdp->fd_freefile;
    777 	off = i >> NDENTRYSHIFT;
    778 	new = find_next_zero(fdp->fd_himap, off,
    779 	    (last + NDENTRIES - 1) >> NDENTRYSHIFT);
    780 	if (new != -1) {
    781 		i = find_next_zero(&fdp->fd_lomap[new],
    782 		    new > off ? 0 : i & NDENTRYMASK, NDENTRIES);
    783 		if (i == -1) {
    784 			/*
    785 			 * free file descriptor in this block was
    786 			 * below want, try again with higher want.
    787 			 */
    788 			want = (new + 1) << NDENTRYSHIFT;
    789 			goto again;
    790 		}
    791 		i += (new << NDENTRYSHIFT);
    792 		if (i < last) {
    793 			if (fdp->fd_ofiles[i] == NULL) {
    794 				fd_used(fdp, i);
    795 				if (want <= fdp->fd_freefile)
    796 					fdp->fd_freefile = i;
    797 				*result = i;
    798 				error = 0;
    799 				goto out;
    800 			}
    801 		}
    802 	}
    803 
    804 	/* No space in current array.  Expand or let the caller do it. */
    805 	error = (fdp->fd_nfiles >= lim) ? EMFILE : ENOSPC;
    806 
    807 out:
    808 	simple_unlock(&fdp->fd_slock);
    809 	return (error);
    810 }
    811 
    812 void
    813 fdexpand(struct proc *p)
    814 {
    815 	struct filedesc	*fdp;
    816 	int		i, nfiles, oldnfiles;
    817 	struct file	**newofile;
    818 	char		*newofileflags;
    819 	uint32_t	*newhimap = NULL, *newlomap = NULL;
    820 
    821 	fdp = p->p_fd;
    822 
    823 restart:
    824 	oldnfiles = fdp->fd_nfiles;
    825 
    826 	if (oldnfiles < NDEXTENT)
    827 		nfiles = NDEXTENT;
    828 	else
    829 		nfiles = 2 * oldnfiles;
    830 
    831 	newofile = malloc(nfiles * OFILESIZE, M_FILEDESC, M_WAITOK);
    832 	if (NDHISLOTS(nfiles) > NDHISLOTS(oldnfiles)) {
    833 		newhimap = malloc(NDHISLOTS(nfiles) * sizeof(uint32_t),
    834 		    M_FILEDESC, M_WAITOK);
    835 		newlomap = malloc(NDLOSLOTS(nfiles) * sizeof(uint32_t),
    836 		    M_FILEDESC, M_WAITOK);
    837 	}
    838 
    839 	simple_lock(&fdp->fd_slock);
    840 	/* lock fdp */
    841 	if (fdp->fd_nfiles != oldnfiles) {
    842 		/* fdp changed; retry */
    843 		simple_unlock(&fdp->fd_slock);
    844 		free(newofile, M_FILEDESC);
    845 		if (newhimap != NULL) free(newhimap, M_FILEDESC);
    846 		if (newlomap != NULL) free(newlomap, M_FILEDESC);
    847 		goto restart;
    848 	}
    849 
    850 	newofileflags = (char *) &newofile[nfiles];
    851 	/*
    852 	 * Copy the existing ofile and ofileflags arrays
    853 	 * and zero the new portion of each array.
    854 	 */
    855 	memcpy(newofile, fdp->fd_ofiles,
    856 	    (i = sizeof(struct file *) * fdp->fd_nfiles));
    857 	memset((char *)newofile + i, 0,
    858 	    nfiles * sizeof(struct file *) - i);
    859 	memcpy(newofileflags, fdp->fd_ofileflags,
    860 	    (i = sizeof(char) * fdp->fd_nfiles));
    861 	memset(newofileflags + i, 0, nfiles * sizeof(char) - i);
    862 	if (oldnfiles > NDFILE)
    863 		free(fdp->fd_ofiles, M_FILEDESC);
    864 
    865 	if (NDHISLOTS(nfiles) > NDHISLOTS(oldnfiles)) {
    866 		memcpy(newhimap, fdp->fd_himap,
    867 		    (i = NDHISLOTS(oldnfiles) * sizeof(uint32_t)));
    868 		memset((char *)newhimap + i, 0,
    869 		    NDHISLOTS(nfiles) * sizeof(uint32_t) - i);
    870 
    871 		memcpy(newlomap, fdp->fd_lomap,
    872 		    (i = NDLOSLOTS(oldnfiles) * sizeof(uint32_t)));
    873 		memset((char *)newlomap + i, 0,
    874 		    NDLOSLOTS(nfiles) * sizeof(uint32_t) - i);
    875 
    876 		if (NDHISLOTS(oldnfiles) > NDHISLOTS(NDFILE)) {
    877 			free(fdp->fd_himap, M_FILEDESC);
    878 			free(fdp->fd_lomap, M_FILEDESC);
    879 		}
    880 		fdp->fd_himap = newhimap;
    881 		fdp->fd_lomap = newlomap;
    882 	}
    883 
    884 	fdp->fd_ofiles = newofile;
    885 	fdp->fd_ofileflags = newofileflags;
    886 	fdp->fd_nfiles = nfiles;
    887 
    888 	simple_unlock(&fdp->fd_slock);
    889 
    890 	fdexpanded++;
    891 }
    892 
    893 /*
    894  * Check to see whether n user file descriptors
    895  * are available to the process p.
    896  */
    897 int
    898 fdavail(struct proc *p, int n)
    899 {
    900 	struct filedesc	*fdp;
    901 	struct file	**fpp;
    902 	int		i, lim;
    903 
    904 	fdp = p->p_fd;
    905 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    906 	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
    907 		return (1);
    908 	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
    909 	for (i = min(lim,fdp->fd_nfiles) - fdp->fd_freefile; --i >= 0; fpp++)
    910 		if (*fpp == NULL && --n <= 0)
    911 			return (1);
    912 	return (0);
    913 }
    914 
    915 /*
    916  * Create a new open file structure and allocate
    917  * a file descriptor for the process that refers to it.
    918  */
    919 int
    920 falloc(struct proc *p, struct file **resultfp, int *resultfd)
    921 {
    922 	struct file	*fp, *fq;
    923 	int		error, i;
    924 
    925  restart:
    926 	if ((error = fdalloc(p, 0, &i)) != 0) {
    927 		if (error == ENOSPC) {
    928 			fdexpand(p);
    929 			goto restart;
    930 		}
    931 		return (error);
    932 	}
    933 
    934 	fp = pool_get(&file_pool, PR_WAITOK);
    935 	simple_lock(&filelist_slock);
    936 	if (nfiles >= maxfiles) {
    937 		tablefull("file", "increase kern.maxfiles or MAXFILES");
    938 		simple_unlock(&filelist_slock);
    939 		fd_unused(p->p_fd, i);
    940 		pool_put(&file_pool, fp);
    941 		return (ENFILE);
    942 	}
    943 	/*
    944 	 * Allocate a new file descriptor.
    945 	 * If the process has file descriptor zero open, add to the list
    946 	 * of open files at that point, otherwise put it at the front of
    947 	 * the list of open files.
    948 	 */
    949 	nfiles++;
    950 	memset(fp, 0, sizeof(struct file));
    951 	fp->f_iflags = FIF_LARVAL;
    952 	if ((fq = p->p_fd->fd_ofiles[0]) != NULL) {
    953 		LIST_INSERT_AFTER(fq, fp, f_list);
    954 	} else {
    955 		LIST_INSERT_HEAD(&filehead, fp, f_list);
    956 	}
    957 	simple_unlock(&filelist_slock);
    958 	KDASSERT(p->p_fd->fd_ofiles[i] == NULL);
    959 	p->p_fd->fd_ofiles[i] = fp;
    960 	simple_lock_init(&fp->f_slock);
    961 	fp->f_count = 1;
    962 	fp->f_cred = p->p_ucred;
    963 	crhold(fp->f_cred);
    964 	if (resultfp) {
    965 		fp->f_usecount = 1;
    966 		*resultfp = fp;
    967 	}
    968 	if (resultfd)
    969 		*resultfd = i;
    970 	return (0);
    971 }
    972 
    973 /*
    974  * Free a file descriptor.
    975  */
    976 void
    977 ffree(struct file *fp)
    978 {
    979 
    980 #ifdef DIAGNOSTIC
    981 	if (fp->f_usecount)
    982 		panic("ffree");
    983 #endif
    984 
    985 	simple_lock(&filelist_slock);
    986 	LIST_REMOVE(fp, f_list);
    987 	crfree(fp->f_cred);
    988 #ifdef DIAGNOSTIC
    989 	fp->f_count = 0; /* What's the point? */
    990 #endif
    991 	nfiles--;
    992 	simple_unlock(&filelist_slock);
    993 	pool_put(&file_pool, fp);
    994 }
    995 
    996 /*
    997  * Create an initial cwdinfo structure, using the same current and root
    998  * directories as p.
    999  */
   1000 struct cwdinfo *
   1001 cwdinit(struct proc *p)
   1002 {
   1003 	struct cwdinfo *cwdi;
   1004 
   1005 	cwdi = pool_get(&cwdi_pool, PR_WAITOK);
   1006 
   1007 	simple_lock_init(&cwdi->cwdi_slock);
   1008 	cwdi->cwdi_cdir = p->p_cwdi->cwdi_cdir;
   1009 	if (cwdi->cwdi_cdir)
   1010 		VREF(cwdi->cwdi_cdir);
   1011 	cwdi->cwdi_rdir = p->p_cwdi->cwdi_rdir;
   1012 	if (cwdi->cwdi_rdir)
   1013 		VREF(cwdi->cwdi_rdir);
   1014 	cwdi->cwdi_cmask =  p->p_cwdi->cwdi_cmask;
   1015 	cwdi->cwdi_refcnt = 1;
   1016 
   1017 	return (cwdi);
   1018 }
   1019 
   1020 /*
   1021  * Make p2 share p1's cwdinfo.
   1022  */
   1023 void
   1024 cwdshare(struct proc *p1, struct proc *p2)
   1025 {
   1026 	struct cwdinfo *cwdi = p1->p_cwdi;
   1027 
   1028 	simple_lock(&cwdi->cwdi_slock);
   1029 	cwdi->cwdi_refcnt++;
   1030 	simple_unlock(&cwdi->cwdi_slock);
   1031 	p2->p_cwdi = cwdi;
   1032 }
   1033 
   1034 /*
   1035  * Make this process not share its cwdinfo structure, maintaining
   1036  * all cwdinfo state.
   1037  */
   1038 void
   1039 cwdunshare(struct proc *p)
   1040 {
   1041 	struct cwdinfo *oldcwdi, *newcwdi;
   1042 
   1043 	if (p->p_cwdi->cwdi_refcnt == 1)
   1044 		return;
   1045 
   1046 	newcwdi = cwdinit(p);
   1047 	oldcwdi = p->p_cwdi;
   1048 	p->p_cwdi = newcwdi;
   1049 	cwdfree(oldcwdi);
   1050 }
   1051 
   1052 /*
   1053  * Release a cwdinfo structure.
   1054  */
   1055 void
   1056 cwdfree(struct cwdinfo *cwdi)
   1057 {
   1058 	int n;
   1059 
   1060 	simple_lock(&cwdi->cwdi_slock);
   1061 	n = --cwdi->cwdi_refcnt;
   1062 	simple_unlock(&cwdi->cwdi_slock);
   1063 	if (n > 0)
   1064 		return;
   1065 
   1066 	vrele(cwdi->cwdi_cdir);
   1067 	if (cwdi->cwdi_rdir)
   1068 		vrele(cwdi->cwdi_rdir);
   1069 	pool_put(&cwdi_pool, cwdi);
   1070 }
   1071 
   1072 /*
   1073  * Create an initial filedesc structure, using the same current and root
   1074  * directories as p.
   1075  */
   1076 struct filedesc *
   1077 fdinit(struct proc *p)
   1078 {
   1079 	struct filedesc0 *newfdp;
   1080 
   1081 	newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
   1082 	memset(newfdp, 0, sizeof(struct filedesc0));
   1083 
   1084 	fdinit1(newfdp);
   1085 
   1086 	return (&newfdp->fd_fd);
   1087 }
   1088 
   1089 /*
   1090  * Initialize a file descriptor table.
   1091  */
   1092 void
   1093 fdinit1(struct filedesc0 *newfdp)
   1094 {
   1095 
   1096 	newfdp->fd_fd.fd_refcnt = 1;
   1097 	newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
   1098 	newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
   1099 	newfdp->fd_fd.fd_nfiles = NDFILE;
   1100 	newfdp->fd_fd.fd_knlistsize = -1;
   1101 	newfdp->fd_fd.fd_himap = newfdp->fd_dhimap;
   1102 	newfdp->fd_fd.fd_lomap = newfdp->fd_dlomap;
   1103 	simple_lock_init(&newfdp->fd_fd.fd_slock);
   1104 }
   1105 
   1106 /*
   1107  * Make p2 share p1's filedesc structure.
   1108  */
   1109 void
   1110 fdshare(struct proc *p1, struct proc *p2)
   1111 {
   1112 	struct filedesc *fdp = p1->p_fd;
   1113 
   1114 	simple_lock(&fdp->fd_slock);
   1115 	p2->p_fd = fdp;
   1116 	fdp->fd_refcnt++;
   1117 	simple_unlock(&fdp->fd_slock);
   1118 }
   1119 
   1120 /*
   1121  * Make this process not share its filedesc structure, maintaining
   1122  * all file descriptor state.
   1123  */
   1124 void
   1125 fdunshare(struct proc *p)
   1126 {
   1127 	struct filedesc *newfd;
   1128 
   1129 	if (p->p_fd->fd_refcnt == 1)
   1130 		return;
   1131 
   1132 	newfd = fdcopy(p);
   1133 	fdfree(p);
   1134 	p->p_fd = newfd;
   1135 }
   1136 
   1137 /*
   1138  * Clear a process's fd table.
   1139  */
   1140 void
   1141 fdclear(struct proc *p)
   1142 {
   1143 	struct filedesc *newfd;
   1144 
   1145 	newfd = fdinit(p);
   1146 	fdfree(p);
   1147 	p->p_fd = newfd;
   1148 }
   1149 
   1150 /*
   1151  * Copy a filedesc structure.
   1152  */
   1153 struct filedesc *
   1154 fdcopy(struct proc *p)
   1155 {
   1156 	struct filedesc	*newfdp, *fdp;
   1157 	struct file	**fpp, **nfpp;
   1158 	int		i, nfiles, lastfile;
   1159 
   1160 	fdp = p->p_fd;
   1161 	newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
   1162 	newfdp->fd_refcnt = 1;
   1163 	simple_lock_init(&newfdp->fd_slock);
   1164 
   1165 restart:
   1166 	nfiles = fdp->fd_nfiles;
   1167 	lastfile = fdp->fd_lastfile;
   1168 
   1169 	/*
   1170 	 * If the number of open files fits in the internal arrays
   1171 	 * of the open file structure, use them, otherwise allocate
   1172 	 * additional memory for the number of descriptors currently
   1173 	 * in use.
   1174 	 */
   1175 	if (lastfile < NDFILE) {
   1176 		i = NDFILE;
   1177 	} else {
   1178 		/*
   1179 		 * Compute the smallest multiple of NDEXTENT needed
   1180 		 * for the file descriptors currently in use,
   1181 		 * allowing the table to shrink.
   1182 		 */
   1183 		i = nfiles;
   1184 		while (i >= 2 * NDEXTENT && i > lastfile * 2)
   1185 			i /= 2;
   1186 		newfdp->fd_ofiles = malloc(i * OFILESIZE, M_FILEDESC, M_WAITOK);
   1187 	}
   1188 	if (NDHISLOTS(i) > NDHISLOTS(NDFILE)) {
   1189 		newfdp->fd_himap = malloc(NDHISLOTS(i) * sizeof(uint32_t),
   1190 		    M_FILEDESC, M_WAITOK);
   1191 		newfdp->fd_lomap = malloc(NDLOSLOTS(i) * sizeof(uint32_t),
   1192 		    M_FILEDESC, M_WAITOK);
   1193 	}
   1194 
   1195 	simple_lock(&fdp->fd_slock);
   1196 	if (nfiles != fdp->fd_nfiles || lastfile != fdp->fd_lastfile) {
   1197 		simple_unlock(&fdp->fd_slock);
   1198 		if (i > NDFILE)
   1199 			free(newfdp->fd_ofiles, M_FILEDESC);
   1200 		if (NDHISLOTS(i) > NDHISLOTS(NDFILE)) {
   1201 			free(newfdp->fd_himap, M_FILEDESC);
   1202 			free(newfdp->fd_lomap, M_FILEDESC);
   1203 		}
   1204 		goto restart;
   1205 	}
   1206 
   1207 	if (lastfile < NDFILE) {
   1208 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
   1209 		newfdp->fd_ofileflags =
   1210 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
   1211 	} else {
   1212 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
   1213 	}
   1214 	if (NDHISLOTS(i) <= NDHISLOTS(NDFILE)) {
   1215 		newfdp->fd_himap =
   1216 		    ((struct filedesc0 *) newfdp)->fd_dhimap;
   1217 		newfdp->fd_lomap =
   1218 		    ((struct filedesc0 *) newfdp)->fd_dlomap;
   1219 	}
   1220 
   1221 	newfdp->fd_nfiles = i;
   1222 	newfdp->fd_lastfile = lastfile;
   1223 	newfdp->fd_freefile = fdp->fd_freefile;
   1224 
   1225 	memset(newfdp->fd_ofiles + lastfile, 0,
   1226 		(i - lastfile) * sizeof(struct file **));
   1227 	memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags, i * sizeof(char));
   1228 	if (i < NDENTRIES * NDENTRIES)
   1229 		i = NDENTRIES * NDENTRIES; /* size of inlined bitmaps */
   1230 	memcpy(newfdp->fd_himap, fdp->fd_himap, NDHISLOTS(i)*sizeof(uint32_t));
   1231 	memcpy(newfdp->fd_lomap, fdp->fd_lomap, NDLOSLOTS(i)*sizeof(uint32_t));
   1232 
   1233 	fpp = fdp->fd_ofiles;
   1234 	nfpp = newfdp->fd_ofiles;
   1235 	for (i = 0; i <= lastfile; i++, fpp++, nfpp++) {
   1236 		if ((*nfpp = *fpp) == NULL)
   1237 			continue;
   1238 
   1239 		if ((*fpp)->f_type == DTYPE_KQUEUE)
   1240 			/* kq descriptors cannot be copied. */
   1241 			fdremove(newfdp, i);
   1242 		else {
   1243 			simple_lock(&(*fpp)->f_slock);
   1244 			(*fpp)->f_count++;
   1245 			simple_unlock(&(*fpp)->f_slock);
   1246 		}
   1247 	}
   1248 
   1249 	simple_unlock(&fdp->fd_slock);
   1250 
   1251 	newfdp->fd_knlist = NULL;
   1252 	newfdp->fd_knlistsize = -1;
   1253 	newfdp->fd_knhash = NULL;
   1254 	newfdp->fd_knhashmask = 0;
   1255 
   1256 	return (newfdp);
   1257 }
   1258 
   1259 /*
   1260  * Release a filedesc structure.
   1261  */
   1262 void
   1263 fdfree(struct proc *p)
   1264 {
   1265 	struct filedesc	*fdp;
   1266 	struct file	**fpp, *fp;
   1267 	int		i;
   1268 
   1269 	fdp = p->p_fd;
   1270 	simple_lock(&fdp->fd_slock);
   1271 	i = --fdp->fd_refcnt;
   1272 	simple_unlock(&fdp->fd_slock);
   1273 	if (i > 0)
   1274 		return;
   1275 
   1276 	fpp = fdp->fd_ofiles;
   1277 	for (i = fdp->fd_lastfile; i >= 0; i--, fpp++) {
   1278 		fp = *fpp;
   1279 		if (fp != NULL) {
   1280 			*fpp = NULL;
   1281 			simple_lock(&fp->f_slock);
   1282 			FILE_USE(fp);
   1283 			if (i < fdp->fd_knlistsize)
   1284 				knote_fdclose(p, fdp->fd_lastfile - i);
   1285 			(void) closef(fp, p);
   1286 		}
   1287 	}
   1288 	p->p_fd = NULL;
   1289 	if (fdp->fd_nfiles > NDFILE)
   1290 		free(fdp->fd_ofiles, M_FILEDESC);
   1291 	if (NDHISLOTS(fdp->fd_nfiles) > NDHISLOTS(NDFILE)) {
   1292 		free(fdp->fd_himap, M_FILEDESC);
   1293 		free(fdp->fd_lomap, M_FILEDESC);
   1294 	}
   1295 	if (fdp->fd_knlist)
   1296 		free(fdp->fd_knlist, M_KEVENT);
   1297 	if (fdp->fd_knhash)
   1298 		hashdone(fdp->fd_knhash, M_KEVENT);
   1299 	pool_put(&filedesc0_pool, fdp);
   1300 }
   1301 
   1302 /*
   1303  * Internal form of close.
   1304  * Decrement reference count on file structure.
   1305  * Note: p may be NULL when closing a file
   1306  * that was being passed in a message.
   1307  *
   1308  * Note: we expect the caller is holding a usecount, and expects us
   1309  * to drop it (the caller thinks the file is going away forever).
   1310  */
   1311 int
   1312 closef(struct file *fp, struct proc *p)
   1313 {
   1314 	struct vnode	*vp;
   1315 	struct flock	lf;
   1316 	int		error;
   1317 
   1318 	if (fp == NULL)
   1319 		return (0);
   1320 
   1321 	/*
   1322 	 * POSIX record locking dictates that any close releases ALL
   1323 	 * locks owned by this process.  This is handled by setting
   1324 	 * a flag in the unlock to free ONLY locks obeying POSIX
   1325 	 * semantics, and not to free BSD-style file locks.
   1326 	 * If the descriptor was in a message, POSIX-style locks
   1327 	 * aren't passed with the descriptor.
   1328 	 */
   1329 	if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
   1330 		lf.l_whence = SEEK_SET;
   1331 		lf.l_start = 0;
   1332 		lf.l_len = 0;
   1333 		lf.l_type = F_UNLCK;
   1334 		vp = (struct vnode *)fp->f_data;
   1335 		(void) VOP_ADVLOCK(vp, p, F_UNLCK, &lf, F_POSIX);
   1336 	}
   1337 
   1338 	/*
   1339 	 * If WANTCLOSE is set, then the reference count on the file
   1340 	 * is 0, but there were multiple users of the file.  This can
   1341 	 * happen if a filedesc structure is shared by multiple
   1342 	 * processes.
   1343 	 */
   1344 	simple_lock(&fp->f_slock);
   1345 	if (fp->f_iflags & FIF_WANTCLOSE) {
   1346 		/*
   1347 		 * Another user of the file is already closing, and is
   1348 		 * simply waiting for other users of the file to drain.
   1349 		 * Release our usecount, and wake up the closer if it
   1350 		 * is the only remaining use.
   1351 		 */
   1352 #ifdef DIAGNOSTIC
   1353 		if (fp->f_count != 0)
   1354 			panic("closef: wantclose and count != 0");
   1355 		if (fp->f_usecount < 2)
   1356 			panic("closef: wantclose and usecount < 2");
   1357 #endif
   1358 		if (--fp->f_usecount == 1)
   1359 			wakeup(&fp->f_usecount);
   1360 		simple_unlock(&fp->f_slock);
   1361 		return (0);
   1362 	} else {
   1363 		/*
   1364 		 * Decrement the reference count.  If we were not the
   1365 		 * last reference, then release our use and just
   1366 		 * return.
   1367 		 */
   1368 		if (--fp->f_count > 0) {
   1369 #ifdef DIAGNOSTIC
   1370 			if (fp->f_usecount < 1)
   1371 				panic("closef: no wantclose and usecount < 1");
   1372 #endif
   1373 			fp->f_usecount--;
   1374 			simple_unlock(&fp->f_slock);
   1375 			return (0);
   1376 		}
   1377 	}
   1378 
   1379 	/*
   1380 	 * The reference count is now 0.  However, there may be
   1381 	 * multiple potential users of this file.  This can happen
   1382 	 * if multiple processes shared a single filedesc structure.
   1383 	 *
   1384 	 * Notify these potential users that the file is closing.
   1385 	 * This will prevent them from adding additional uses to
   1386 	 * the file.
   1387 	 */
   1388 	fp->f_iflags |= FIF_WANTCLOSE;
   1389 
   1390 	/*
   1391 	 * We expect the caller to add a use to the file.  So, if we
   1392 	 * are the last user, usecount will be 1.  If it is not, we
   1393 	 * must wait for the usecount to drain.  When it drains back
   1394 	 * to 1, we will be awakened so that we may proceed with the
   1395 	 * close.
   1396 	 */
   1397 #ifdef DIAGNOSTIC
   1398 	if (fp->f_usecount < 1)
   1399 		panic("closef: usecount < 1");
   1400 #endif
   1401 	while (fp->f_usecount > 1)
   1402 		(void) ltsleep(&fp->f_usecount, PRIBIO, "closef", 0,
   1403 				&fp->f_slock);
   1404 #ifdef DIAGNOSTIC
   1405 	if (fp->f_usecount != 1)
   1406 		panic("closef: usecount != 1");
   1407 #endif
   1408 
   1409 	simple_unlock(&fp->f_slock);
   1410 	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
   1411 		lf.l_whence = SEEK_SET;
   1412 		lf.l_start = 0;
   1413 		lf.l_len = 0;
   1414 		lf.l_type = F_UNLCK;
   1415 		vp = (struct vnode *)fp->f_data;
   1416 		(void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
   1417 	}
   1418 	if (fp->f_ops)
   1419 		error = (*fp->f_ops->fo_close)(fp, p);
   1420 	else
   1421 		error = 0;
   1422 
   1423 	/* Nothing references the file now, drop the final use (us). */
   1424 	fp->f_usecount--;
   1425 
   1426 	ffree(fp);
   1427 	return (error);
   1428 }
   1429 
   1430 /*
   1431  * Apply an advisory lock on a file descriptor.
   1432  *
   1433  * Just attempt to get a record lock of the requested type on
   1434  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
   1435  */
   1436 /* ARGSUSED */
   1437 int
   1438 sys_flock(struct lwp *l, void *v, register_t *retval)
   1439 {
   1440 	struct sys_flock_args /* {
   1441 		syscallarg(int)	fd;
   1442 		syscallarg(int)	how;
   1443 	} */ *uap = v;
   1444 	int		fd, how, error;
   1445 	struct proc	*p;
   1446 	struct filedesc	*fdp;
   1447 	struct file	*fp;
   1448 	struct vnode	*vp;
   1449 	struct flock	lf;
   1450 
   1451 	p = l->l_proc;
   1452 	fd = SCARG(uap, fd);
   1453 	how = SCARG(uap, how);
   1454 	fdp = p->p_fd;
   1455 	error = 0;
   1456 
   1457 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1458 		return (EBADF);
   1459 
   1460 	FILE_USE(fp);
   1461 
   1462 	if (fp->f_type != DTYPE_VNODE) {
   1463 		error = EOPNOTSUPP;
   1464 		goto out;
   1465 	}
   1466 
   1467 	vp = (struct vnode *)fp->f_data;
   1468 	lf.l_whence = SEEK_SET;
   1469 	lf.l_start = 0;
   1470 	lf.l_len = 0;
   1471 	if (how & LOCK_UN) {
   1472 		lf.l_type = F_UNLCK;
   1473 		fp->f_flag &= ~FHASLOCK;
   1474 		error = VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
   1475 		goto out;
   1476 	}
   1477 	if (how & LOCK_EX)
   1478 		lf.l_type = F_WRLCK;
   1479 	else if (how & LOCK_SH)
   1480 		lf.l_type = F_RDLCK;
   1481 	else {
   1482 		error = EINVAL;
   1483 		goto out;
   1484 	}
   1485 	fp->f_flag |= FHASLOCK;
   1486 	if (how & LOCK_NB)
   1487 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK);
   1488 	else
   1489 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf,
   1490 		    F_FLOCK|F_WAIT);
   1491  out:
   1492 	FILE_UNUSE(fp, p);
   1493 	return (error);
   1494 }
   1495 
   1496 /*
   1497  * File Descriptor pseudo-device driver (/dev/fd/).
   1498  *
   1499  * Opening minor device N dup()s the file (if any) connected to file
   1500  * descriptor N belonging to the calling process.  Note that this driver
   1501  * consists of only the ``open()'' routine, because all subsequent
   1502  * references to this file will be direct to the other driver.
   1503  */
   1504 /* ARGSUSED */
   1505 int
   1506 filedescopen(dev_t dev, int mode, int type, struct proc *p)
   1507 {
   1508 
   1509 	/*
   1510 	 * XXX Kludge: set dupfd to contain the value of the
   1511 	 * the file descriptor being sought for duplication. The error
   1512 	 * return ensures that the vnode for this device will be released
   1513 	 * by vn_open. Open will detect this special error and take the
   1514 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
   1515 	 * will simply report the error.
   1516 	 */
   1517 	curlwp->l_dupfd = minor(dev);	/* XXX */
   1518 	return (ENODEV);
   1519 }
   1520 
   1521 /*
   1522  * Duplicate the specified descriptor to a free descriptor.
   1523  *
   1524  * 'indx' has been fdalloc'ed (and will be fdremove'ed on error) by the caller.
   1525  */
   1526 int
   1527 dupfdopen(struct proc *p, int indx, int dfd, int mode, int error)
   1528 {
   1529 	struct filedesc	*fdp;
   1530 	struct file	*wfp;
   1531 
   1532 	fdp = p->p_fd;
   1533 
   1534 	/* should be cleared by the caller */
   1535 	KASSERT(fdp->fd_ofiles[indx] == NULL);
   1536 
   1537 	/*
   1538 	 * If the to-be-dup'd fd number is greater than the allowed number
   1539 	 * of file descriptors, or the fd to be dup'd has already been
   1540 	 * closed, reject.
   1541 	 */
   1542 
   1543 	/*
   1544 	 * Note, in the case of indx == dfd, fd_getfile below returns NULL.
   1545 	 */
   1546 	if ((wfp = fd_getfile(fdp, dfd)) == NULL)
   1547 		return (EBADF);
   1548 
   1549 	FILE_USE(wfp);
   1550 
   1551 	/*
   1552 	 * There are two cases of interest here.
   1553 	 *
   1554 	 * For ENODEV simply dup (dfd) to file descriptor
   1555 	 * (indx) and return.
   1556 	 *
   1557 	 * For ENXIO steal away the file structure from (dfd) and
   1558 	 * store it in (indx).  (dfd) is effectively closed by
   1559 	 * this operation.
   1560 	 *
   1561 	 * Any other error code is just returned.
   1562 	 */
   1563 	switch (error) {
   1564 	case ENODEV:
   1565 		/*
   1566 		 * Check that the mode the file is being opened for is a
   1567 		 * subset of the mode of the existing descriptor.
   1568 		 */
   1569 		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
   1570 			FILE_UNUSE(wfp, p);
   1571 			return (EACCES);
   1572 		}
   1573 		simple_lock(&fdp->fd_slock);
   1574 		fdp->fd_ofiles[indx] = wfp;
   1575 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
   1576 		simple_unlock(&fdp->fd_slock);
   1577 		simple_lock(&wfp->f_slock);
   1578 		wfp->f_count++;
   1579 		/* 'indx' has been fd_used'ed by caller */
   1580 		FILE_UNUSE_HAVELOCK(wfp, p);
   1581 		return (0);
   1582 
   1583 	case ENXIO:
   1584 		/*
   1585 		 * Steal away the file pointer from dfd, and stuff it into indx.
   1586 		 */
   1587 		simple_lock(&fdp->fd_slock);
   1588 		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
   1589 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
   1590 		fdp->fd_ofiles[dfd] = NULL;
   1591 		fdp->fd_ofileflags[dfd] = 0;
   1592 		/*
   1593 		 * Complete the clean up of the filedesc structure by
   1594 		 * recomputing the various hints.
   1595 		 */
   1596 		/* 'indx' has been fd_used'ed by caller */
   1597 		fd_unused(fdp, dfd);
   1598 		simple_unlock(&fdp->fd_slock);
   1599 		FILE_UNUSE(wfp, p);
   1600 		return (0);
   1601 
   1602 	default:
   1603 		FILE_UNUSE(wfp, p);
   1604 		return (error);
   1605 	}
   1606 	/* NOTREACHED */
   1607 }
   1608 
   1609 /*
   1610  * fcntl call which is being passed to the file's fs.
   1611  */
   1612 int
   1613 fcntl_forfs(int fd, struct proc *p, int cmd, void *arg)
   1614 {
   1615 	struct file	*fp;
   1616 	struct filedesc	*fdp;
   1617 	int		error;
   1618 	u_int		size;
   1619 	void		*data, *memp;
   1620 #define STK_PARAMS	128
   1621 	char		stkbuf[STK_PARAMS];
   1622 
   1623 	/* fd's value was validated in sys_fcntl before calling this routine */
   1624 	fdp = p->p_fd;
   1625 	fp = fdp->fd_ofiles[fd];
   1626 
   1627 	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
   1628 		return (EBADF);
   1629 
   1630 	/*
   1631 	 * Interpret high order word to find amount of data to be
   1632 	 * copied to/from the user's address space.
   1633 	 */
   1634 	size = (size_t)F_PARAM_LEN(cmd);
   1635 	if (size > F_PARAM_MAX)
   1636 		return (EINVAL);
   1637 	memp = NULL;
   1638 	if (size > sizeof(stkbuf)) {
   1639 		memp = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
   1640 		data = memp;
   1641 	} else
   1642 		data = stkbuf;
   1643 	if (cmd & F_FSIN) {
   1644 		if (size) {
   1645 			error = copyin(arg, data, size);
   1646 			if (error) {
   1647 				if (memp)
   1648 					free(memp, M_IOCTLOPS);
   1649 				return (error);
   1650 			}
   1651 		} else
   1652 			*(void **)data = arg;
   1653 	} else if ((cmd & F_FSOUT) && size)
   1654 		/*
   1655 		 * Zero the buffer so the user always
   1656 		 * gets back something deterministic.
   1657 		 */
   1658 		memset(data, 0, size);
   1659 	else if (cmd & F_FSVOID)
   1660 		*(void **)data = arg;
   1661 
   1662 
   1663 	error = (*fp->f_ops->fo_fcntl)(fp, cmd, data, p);
   1664 
   1665 	/*
   1666 	 * Copy any data to user, size was
   1667 	 * already set and checked above.
   1668 	 */
   1669 	if (error == 0 && (cmd & F_FSOUT) && size)
   1670 		error = copyout(data, arg, size);
   1671 	if (memp)
   1672 		free(memp, M_IOCTLOPS);
   1673 	return (error);
   1674 }
   1675 
   1676 /*
   1677  * Close any files on exec?
   1678  */
   1679 void
   1680 fdcloseexec(struct proc *p)
   1681 {
   1682 	struct filedesc	*fdp;
   1683 	int		fd;
   1684 
   1685 	fdunshare(p);
   1686 	cwdunshare(p);
   1687 
   1688 	fdp = p->p_fd;
   1689 	for (fd = 0; fd <= fdp->fd_lastfile; fd++)
   1690 		if (fdp->fd_ofileflags[fd] & UF_EXCLOSE)
   1691 			(void) fdrelease(p, fd);
   1692 }
   1693 
   1694 /*
   1695  * It is unsafe for set[ug]id processes to be started with file
   1696  * descriptors 0..2 closed, as these descriptors are given implicit
   1697  * significance in the Standard C library.  fdcheckstd() will create a
   1698  * descriptor referencing /dev/null for each of stdin, stdout, and
   1699  * stderr that is not already open.
   1700  */
   1701 #define CHECK_UPTO 3
   1702 int
   1703 fdcheckstd(p)
   1704 	struct proc *p;
   1705 {
   1706 	struct nameidata nd;
   1707 	struct filedesc *fdp;
   1708 	struct file *fp;
   1709 	struct file *devnullfp = NULL;	/* Quell compiler warning */
   1710 	struct proc *pp;
   1711 	register_t retval;
   1712 	int fd, i, error, flags = FREAD|FWRITE, devnull = -1;
   1713 	char closed[CHECK_UPTO * 3 + 1], which[3 + 1];
   1714 
   1715 	closed[0] = '\0';
   1716 	if ((fdp = p->p_fd) == NULL)
   1717 		return (0);
   1718 	for (i = 0; i < CHECK_UPTO; i++) {
   1719 		if (fdp->fd_ofiles[i] != NULL)
   1720 			continue;
   1721 		snprintf(which, sizeof(which), ",%d", i);
   1722 		strlcat(closed, which, sizeof(closed));
   1723 		if (devnull < 0) {
   1724 			if ((error = falloc(p, &fp, &fd)) != 0)
   1725 				return (error);
   1726 			NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null",
   1727 			    p);
   1728 			if ((error = vn_open(&nd, flags, 0)) != 0) {
   1729 				FILE_UNUSE(fp, p);
   1730 				ffree(fp);
   1731 				fdremove(p->p_fd, fd);
   1732 				return (error);
   1733 			}
   1734 			fp->f_data = nd.ni_vp;
   1735 			fp->f_flag = flags;
   1736 			fp->f_ops = &vnops;
   1737 			fp->f_type = DTYPE_VNODE;
   1738 			VOP_UNLOCK(nd.ni_vp, 0);
   1739 			devnull = fd;
   1740 			devnullfp = fp;
   1741 			FILE_SET_MATURE(fp);
   1742 		} else {
   1743 restart:
   1744 			if ((error = fdalloc(p, 0, &fd)) != 0) {
   1745 				if (error == ENOSPC) {
   1746 					fdexpand(p);
   1747 					goto restart;
   1748 				}
   1749 				return (error);
   1750 			}
   1751 
   1752 			simple_lock(&devnullfp->f_slock);
   1753 			FILE_USE(devnullfp);
   1754 			/* finishdup() will unuse the descriptors for us */
   1755 			if ((error = finishdup(p, devnull, fd, &retval)) != 0)
   1756 				return (error);
   1757 		}
   1758 	}
   1759 	if (devnullfp)
   1760 		FILE_UNUSE(devnullfp, p);
   1761 	if (closed[0] != '\0') {
   1762 		pp = p->p_pptr;
   1763 		log(LOG_WARNING, "set{u,g}id pid %d (%s) "
   1764 		    "was invoked by uid %d ppid %d (%s) "
   1765 		    "with fd %s closed\n",
   1766 		    p->p_pid, p->p_comm, pp->p_ucred->cr_uid,
   1767 		    pp->p_pid, pp->p_comm, &closed[1]);
   1768 	}
   1769 	return (0);
   1770 }
   1771 #undef CHECK_UPTO
   1772 
   1773 /*
   1774  * Sets descriptor owner. If the owner is a process, 'pgid'
   1775  * is set to positive value, process ID. If the owner is process group,
   1776  * 'pgid' is set to -pg_id.
   1777  */
   1778 int
   1779 fsetown(struct proc *p, pid_t *pgid, int cmd, const void *data)
   1780 {
   1781 	int id = *(int *)data;
   1782 	int error;
   1783 
   1784 	switch (cmd) {
   1785 	case TIOCSPGRP:
   1786 		if (id < 0)
   1787 			return (EINVAL);
   1788 		id = -id;
   1789 		break;
   1790 	default:
   1791 		break;
   1792 	}
   1793 
   1794 	if (id > 0 && !pfind(id))
   1795 		return (ESRCH);
   1796 	else if (id < 0 && (error = pgid_in_session(p, -id)))
   1797 		return (error);
   1798 
   1799 	*pgid = id;
   1800 	return (0);
   1801 }
   1802 
   1803 /*
   1804  * Return descriptor owner information. If the value is positive,
   1805  * it's process ID. If it's negative, it's process group ID and
   1806  * needs the sign removed before use.
   1807  */
   1808 int
   1809 fgetown(struct proc *p, pid_t pgid, int cmd, void *data)
   1810 {
   1811 	switch (cmd) {
   1812 	case TIOCGPGRP:
   1813 		*(int *)data = -pgid;
   1814 		break;
   1815 	default:
   1816 		*(int *)data = pgid;
   1817 		break;
   1818 	}
   1819 	return (0);
   1820 }
   1821 
   1822 /*
   1823  * Send signal to descriptor owner, either process or process group.
   1824  */
   1825 void
   1826 fownsignal(pid_t pgid, int signo, int code, int band, void *fdescdata)
   1827 {
   1828 	struct proc *p1;
   1829 	ksiginfo_t ksi;
   1830 
   1831 	memset(&ksi, 0, sizeof(ksi));
   1832 	ksi.ksi_signo = signo;
   1833 	ksi.ksi_code = code;
   1834 	ksi.ksi_band = band;
   1835 
   1836 	if (pgid > 0 && (p1 = pfind(pgid)))
   1837 		kpsignal(p1, &ksi, fdescdata);
   1838 	else if (pgid < 0)
   1839 		kgsignal(-pgid, &ksi, fdescdata);
   1840 }
   1841