xref: /netbsd-src/sys/kern/kern_descrip.c (revision 220b5c059a84c51ea44107ea8951a57ffaecdc8c)
1 /*	$NetBSD: kern_descrip.c,v 1.83 2001/12/07 07:09:29 jdolecek 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. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)kern_descrip.c	8.8 (Berkeley) 2/14/95
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.83 2001/12/07 07:09:29 jdolecek Exp $");
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/filedesc.h>
49 #include <sys/kernel.h>
50 #include <sys/vnode.h>
51 #include <sys/proc.h>
52 #include <sys/file.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/stat.h>
56 #include <sys/ioctl.h>
57 #include <sys/fcntl.h>
58 #include <sys/malloc.h>
59 #include <sys/pool.h>
60 #include <sys/syslog.h>
61 #include <sys/unistd.h>
62 #include <sys/resourcevar.h>
63 #include <sys/conf.h>
64 
65 #include <sys/mount.h>
66 #include <sys/syscallargs.h>
67 
68 /*
69  * Descriptor management.
70  */
71 struct filelist	filehead;	/* head of list of open files */
72 int		nfiles;		/* actual number of open files */
73 struct pool	file_pool;	/* memory pool for file structures */
74 struct pool	cwdi_pool;	/* memory pool for cwdinfo structures */
75 struct pool	filedesc0_pool;	/* memory pool for filedesc0 structures */
76 
77 static __inline void	fd_used(struct filedesc *, int);
78 static __inline void	fd_unused(struct filedesc *, int);
79 int			finishdup(struct proc *, int, int, register_t *);
80 int			fcntl_forfs(int, struct proc *, int, void *);
81 
82 static __inline void
83 fd_used(struct filedesc *fdp, int fd)
84 {
85 
86 	if (fd > fdp->fd_lastfile)
87 		fdp->fd_lastfile = fd;
88 }
89 
90 static __inline void
91 fd_unused(struct filedesc *fdp, int fd)
92 {
93 
94 	if (fd < fdp->fd_freefile)
95 		fdp->fd_freefile = fd;
96 #ifdef DIAGNOSTIC
97 	if (fd > fdp->fd_lastfile)
98 		panic("fd_unused: fd_lastfile inconsistent");
99 #endif
100 	if (fd == fdp->fd_lastfile) {
101 		do {
102 			fd--;
103 		} while (fd >= 0 && fdp->fd_ofiles[fd] == NULL);
104 		fdp->fd_lastfile = fd;
105 	}
106 }
107 
108 struct file *
109 fd_getfile(struct filedesc *fdp, int fd)
110 {
111 	struct file *fp;
112 
113 	if ((u_int) fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
114 		return (NULL);
115 
116 	if (FILE_IS_USABLE(fp) == 0)
117 		return (NULL);
118 
119 	return (fp);
120 }
121 
122 /*
123  * System calls on descriptors.
124  */
125 
126 /*
127  * Duplicate a file descriptor.
128  */
129 /* ARGSUSED */
130 int
131 sys_dup(struct proc *p, void *v, register_t *retval)
132 {
133 	struct sys_dup_args /* {
134 		syscallarg(int)	fd;
135 	} */ *uap = v;
136 	struct file	*fp;
137 	struct filedesc	*fdp;
138 	int		old, new, error;
139 
140 	fdp = p->p_fd;
141 	old = SCARG(uap, fd);
142 
143  restart:
144 	if ((fp = fd_getfile(fdp, old)) == NULL)
145 		return (EBADF);
146 
147 	FILE_USE(fp);
148 
149 	if ((error = fdalloc(p, 0, &new)) != 0) {
150 		if (error == ENOSPC) {
151 			fdexpand(p);
152 			FILE_UNUSE(fp, p);
153 			goto restart;
154 		}
155 		FILE_UNUSE(fp, p);
156 		return (error);
157 	}
158 
159 	/* finishdup() will unuse the descriptors for us */
160 	return (finishdup(p, old, new, retval));
161 }
162 
163 /*
164  * Duplicate a file descriptor to a particular value.
165  */
166 /* ARGSUSED */
167 int
168 sys_dup2(struct proc *p, void *v, register_t *retval)
169 {
170 	struct sys_dup2_args /* {
171 		syscallarg(int)	from;
172 		syscallarg(int)	to;
173 	} */ *uap = v;
174 	struct file	*fp;
175 	struct filedesc	*fdp;
176 	int		old, new, i, error;
177 
178 	fdp = p->p_fd;
179 	old = SCARG(uap, from);
180 	new = SCARG(uap, to);
181 
182  restart:
183 	if ((fp = fd_getfile(fdp, old)) == NULL)
184 		return (EBADF);
185 
186 	if ((u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
187 	    (u_int)new >= maxfiles)
188 		return (EBADF);
189 
190 	if (old == new) {
191 		*retval = new;
192 		return (0);
193 	}
194 
195 	FILE_USE(fp);
196 
197 	if (new >= fdp->fd_nfiles) {
198 		if ((error = fdalloc(p, new, &i)) != 0) {
199 			if (error == ENOSPC) {
200 				fdexpand(p);
201 				FILE_UNUSE(fp, p);
202 				goto restart;
203 			}
204 			FILE_UNUSE(fp, p);
205 			return (error);
206 		}
207 		if (new != i)
208 			panic("dup2: fdalloc");
209 	}
210 
211 	/*
212 	 * finishdup() will close the file that's in the `new'
213 	 * slot, if there's one there.
214 	 */
215 
216 	/* finishdup() will unuse the descriptors for us */
217 	return (finishdup(p, old, new, retval));
218 }
219 
220 /*
221  * The file control system call.
222  */
223 /* ARGSUSED */
224 int
225 sys_fcntl(struct proc *p, void *v, register_t *retval)
226 {
227 	struct sys_fcntl_args /* {
228 		syscallarg(int)		fd;
229 		syscallarg(int)		cmd;
230 		syscallarg(void *)	arg;
231 	} */ *uap = v;
232 	struct filedesc *fdp;
233 	struct file	*fp;
234 	struct vnode	*vp;
235 	int		fd, i, tmp, error, flg, cmd, newmin;
236 	struct flock	fl;
237 
238 	fd = SCARG(uap, fd);
239 	fdp = p->p_fd;
240 	error = 0;
241 	flg = F_POSIX;
242 
243  restart:
244 	if ((fp = fd_getfile(fdp, fd)) == NULL)
245 		return (EBADF);
246 
247 	FILE_USE(fp);
248 
249 	cmd = SCARG(uap, cmd);
250 	if ((cmd & F_FSCTL)) {
251 		error = fcntl_forfs(fd, p, cmd, SCARG(uap, arg));
252 		goto out;
253 	}
254 
255 	switch (cmd) {
256 
257 	case F_DUPFD:
258 		newmin = (long)SCARG(uap, arg);
259 		if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
260 		    (u_int)newmin >= maxfiles) {
261 			error = EINVAL;
262 			goto out;
263 		}
264 		if ((error = fdalloc(p, newmin, &i)) != 0) {
265 			if (error == ENOSPC) {
266 				fdexpand(p);
267 				FILE_UNUSE(fp, p);
268 				goto restart;
269 			}
270 			goto out;
271 		}
272 
273 		/* finishdup() will unuse the descriptors for us */
274 		return (finishdup(p, fd, i, retval));
275 
276 	case F_GETFD:
277 		*retval = fdp->fd_ofileflags[fd] & UF_EXCLOSE ? 1 : 0;
278 		break;
279 
280 	case F_SETFD:
281 		if ((long)SCARG(uap, arg) & 1)
282 			fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
283 		else
284 			fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
285 		break;
286 
287 	case F_GETFL:
288 		*retval = OFLAGS(fp->f_flag);
289 		break;
290 
291 	case F_SETFL:
292 		tmp = FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
293 		error = (*fp->f_ops->fo_fcntl)(fp, F_SETFL, (caddr_t)&tmp, p);
294 		if (error)
295 			goto out;
296 		fp->f_flag &= ~FCNTLFLAGS;
297 		fp->f_flag |= tmp;
298 		tmp = fp->f_flag & FNONBLOCK;
299 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
300 		if (error)
301 			goto out;
302 		tmp = fp->f_flag & FASYNC;
303 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
304 		if (error == 0)
305 			goto out;
306 		fp->f_flag &= ~FNONBLOCK;
307 		tmp = 0;
308 		(void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
309 		break;
310 
311 	case F_GETOWN:
312 		if (fp->f_type == DTYPE_SOCKET) {
313 			*retval = ((struct socket *)fp->f_data)->so_pgid;
314 			goto out;
315 		}
316 		error = (*fp->f_ops->fo_ioctl)
317 			(fp, TIOCGPGRP, (caddr_t)retval, p);
318 		*retval = -*retval;
319 		break;
320 
321 	case F_SETOWN:
322 		if (fp->f_type == DTYPE_SOCKET) {
323 			((struct socket *)fp->f_data)->so_pgid =
324 			    (long)SCARG(uap, arg);
325 			goto out;
326 		}
327 		if ((long)SCARG(uap, arg) <= 0) {
328 			tmp = (-(long)SCARG(uap, arg));
329 		} else {
330 			struct proc *p1 = pfind((long)SCARG(uap, arg));
331 			if (p1 == 0) {
332 				error = ESRCH;
333 				goto out;
334 			}
335 			tmp = (long)p1->p_pgrp->pg_id;
336 		}
337 		error = (*fp->f_ops->fo_ioctl)
338 		    (fp, TIOCSPGRP, (caddr_t)&tmp, p);
339 		break;
340 
341 	case F_SETLKW:
342 		flg |= F_WAIT;
343 		/* Fall into F_SETLK */
344 
345 	case F_SETLK:
346 		if (fp->f_type != DTYPE_VNODE) {
347 			error = EINVAL;
348 			goto out;
349 		}
350 		vp = (struct vnode *)fp->f_data;
351 		/* Copy in the lock structure */
352 		error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
353 		    sizeof(fl));
354 		if (error)
355 			goto out;
356 		if (fl.l_whence == SEEK_CUR)
357 			fl.l_start += fp->f_offset;
358 		switch (fl.l_type) {
359 		case F_RDLCK:
360 			if ((fp->f_flag & FREAD) == 0) {
361 				error = EBADF;
362 				goto out;
363 			}
364 			p->p_flag |= P_ADVLOCK;
365 			error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg);
366 			goto out;
367 
368 		case F_WRLCK:
369 			if ((fp->f_flag & FWRITE) == 0) {
370 				error = EBADF;
371 				goto out;
372 			}
373 			p->p_flag |= P_ADVLOCK;
374 			error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg);
375 			goto out;
376 
377 		case F_UNLCK:
378 			error = VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
379 			    F_POSIX);
380 			goto out;
381 
382 		default:
383 			error = EINVAL;
384 			goto out;
385 		}
386 
387 	case F_GETLK:
388 		if (fp->f_type != DTYPE_VNODE) {
389 			error = EINVAL;
390 			goto out;
391 		}
392 		vp = (struct vnode *)fp->f_data;
393 		/* Copy in the lock structure */
394 		error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
395 		    sizeof(fl));
396 		if (error)
397 			goto out;
398 		if (fl.l_whence == SEEK_CUR)
399 			fl.l_start += fp->f_offset;
400 		if (fl.l_type != F_RDLCK &&
401 		    fl.l_type != F_WRLCK &&
402 		    fl.l_type != F_UNLCK) {
403 			error = EINVAL;
404 			goto out;
405 		}
406 		error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX);
407 		if (error)
408 			goto out;
409 		error = copyout((caddr_t)&fl, (caddr_t)SCARG(uap, arg),
410 		    sizeof(fl));
411 		break;
412 
413 	default:
414 		error = EINVAL;
415 	}
416 
417  out:
418 	FILE_UNUSE(fp, p);
419 	return (error);
420 }
421 
422 /*
423  * Common code for dup, dup2, and fcntl(F_DUPFD).
424  */
425 int
426 finishdup(struct proc *p, int old, int new, register_t *retval)
427 {
428 	struct filedesc	*fdp;
429 	struct file	*fp, *delfp;
430 
431 	fdp = p->p_fd;
432 
433 	/*
434 	 * If there is a file in the new slot, remember it so we
435 	 * can close it after we've finished the dup.  We need
436 	 * to do it after the dup is finished, since closing
437 	 * the file may block.
438 	 *
439 	 * Note: `old' is already used for us.
440 	 */
441 	delfp = fdp->fd_ofiles[new];
442 
443 	fp = fdp->fd_ofiles[old];
444 	fdp->fd_ofiles[new] = fp;
445 	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
446 	fp->f_count++;
447 	/*
448 	 * Note, don't have to mark it "used" in the table if there
449 	 * was already a file in the `new' slot.
450 	 */
451 	if (delfp == NULL)
452 		fd_used(fdp, new);
453 	*retval = new;
454 	FILE_UNUSE(fp, p);
455 
456 	if (delfp != NULL) {
457 		FILE_USE(delfp);
458 		(void) closef(delfp, p);
459 	}
460 	return (0);
461 }
462 
463 void
464 fdremove(struct filedesc *fdp, int fd)
465 {
466 
467 	fdp->fd_ofiles[fd] = NULL;
468 	fd_unused(fdp, fd);
469 }
470 
471 int
472 fdrelease(struct proc *p, int fd)
473 {
474 	struct filedesc	*fdp;
475 	struct file	**fpp, *fp;
476 
477 	fdp = p->p_fd;
478 	fpp = &fdp->fd_ofiles[fd];
479 	fp = *fpp;
480 	if (fp == NULL)
481 		return (EBADF);
482 
483 	FILE_USE(fp);
484 
485 	*fpp = NULL;
486 	fdp->fd_ofileflags[fd] = 0;
487 	fd_unused(fdp, fd);
488 	return (closef(fp, p));
489 }
490 
491 /*
492  * Close a file descriptor.
493  */
494 /* ARGSUSED */
495 int
496 sys_close(struct proc *p, void *v, register_t *retval)
497 {
498 	struct sys_close_args /* {
499 		syscallarg(int)	fd;
500 	} */ *uap = v;
501 	int		fd;
502 	struct filedesc	*fdp;
503 	struct file	*fp;
504 
505 	fd = SCARG(uap, fd);
506 	fdp = p->p_fd;
507 
508 	if ((fp = fd_getfile(fdp, fd)) == NULL)
509 		return (EBADF);
510 
511 	return (fdrelease(p, fd));
512 }
513 
514 /*
515  * Return status information about a file descriptor.
516  */
517 /* ARGSUSED */
518 int
519 sys___fstat13(struct proc *p, void *v, register_t *retval)
520 {
521 	struct sys___fstat13_args /* {
522 		syscallarg(int)			fd;
523 		syscallarg(struct stat *)	sb;
524 	} */ *uap = v;
525 	int		fd;
526 	struct filedesc	*fdp;
527 	struct file	*fp;
528 	struct stat	ub;
529 	int		error;
530 
531 	fd = SCARG(uap, fd);
532 	fdp = p->p_fd;
533 
534 	if ((fp = fd_getfile(fdp, fd)) == NULL)
535 		return (EBADF);
536 
537 	FILE_USE(fp);
538 	error = (*fp->f_ops->fo_stat)(fp, &ub, p);
539 	FILE_UNUSE(fp, p);
540 
541 	if (error == 0)
542 		error = copyout(&ub, SCARG(uap, sb), sizeof(ub));
543 
544 	return (error);
545 }
546 
547 /*
548  * Return pathconf information about a file descriptor.
549  */
550 /* ARGSUSED */
551 int
552 sys_fpathconf(struct proc *p, void *v, register_t *retval)
553 {
554 	struct sys_fpathconf_args /* {
555 		syscallarg(int)	fd;
556 		syscallarg(int)	name;
557 	} */ *uap = v;
558 	int		fd;
559 	struct filedesc	*fdp;
560 	struct file	*fp;
561 	struct vnode	*vp;
562 	int		error;
563 
564 	fd = SCARG(uap, fd);
565 	fdp = p->p_fd;
566 	error = 0;
567 
568 	if ((fp = fd_getfile(fdp, fd)) == NULL)
569 		return (EBADF);
570 
571 	FILE_USE(fp);
572 
573 	switch (fp->f_type) {
574 
575 	case DTYPE_SOCKET:
576 	case DTYPE_PIPE:
577 		if (SCARG(uap, name) != _PC_PIPE_BUF)
578 			error = EINVAL;
579 		else
580 			*retval = PIPE_BUF;
581 		break;
582 
583 	case DTYPE_VNODE:
584 		vp = (struct vnode *)fp->f_data;
585 		error = VOP_PATHCONF(vp, SCARG(uap, name), retval);
586 		break;
587 
588 	default:
589 		panic("fpathconf");
590 	}
591 
592 	FILE_UNUSE(fp, p);
593 	return (error);
594 }
595 
596 /*
597  * Allocate a file descriptor for the process.
598  */
599 int	fdexpanded;		/* XXX: what else uses this? */
600 
601 int
602 fdalloc(struct proc *p, int want, int *result)
603 {
604 	struct filedesc	*fdp;
605 	int i, lim, last;
606 
607 	fdp = p->p_fd;
608 
609 	/*
610 	 * Search for a free descriptor starting at the higher
611 	 * of want or fd_freefile.  If that fails, consider
612 	 * expanding the ofile array.
613 	 */
614 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
615 	for (;;) {
616 		last = min(fdp->fd_nfiles, lim);
617 		if ((i = want) < fdp->fd_freefile)
618 			i = fdp->fd_freefile;
619 		for (; i < last; i++) {
620 			if (fdp->fd_ofiles[i] == NULL) {
621 				fd_used(fdp, i);
622 				if (want <= fdp->fd_freefile)
623 					fdp->fd_freefile = i;
624 				*result = i;
625 				return (0);
626 			}
627 		}
628 
629 		/* No space in current array.  Expand? */
630 		if (fdp->fd_nfiles >= lim)
631 			return (EMFILE);
632 
633 		/* Let the caller do it. */
634 		return (ENOSPC);
635 	}
636 }
637 
638 void
639 fdexpand(struct proc *p)
640 {
641 	struct filedesc	*fdp;
642 	int		i, nfiles;
643 	struct file	**newofile;
644 	char		*newofileflags;
645 
646 	fdp = p->p_fd;
647 
648 	if (fdp->fd_nfiles < NDEXTENT)
649 		nfiles = NDEXTENT;
650 	else
651 		nfiles = 2 * fdp->fd_nfiles;
652 	newofile = malloc(nfiles * OFILESIZE, M_FILEDESC, M_WAITOK);
653 	newofileflags = (char *) &newofile[nfiles];
654 	/*
655 	 * Copy the existing ofile and ofileflags arrays
656 	 * and zero the new portion of each array.
657 	 */
658 	memcpy(newofile, fdp->fd_ofiles,
659 		(i = sizeof(struct file *) * fdp->fd_nfiles));
660 	memset((char *)newofile + i, 0,
661 	    nfiles * sizeof(struct file *) - i);
662 	memcpy(newofileflags, fdp->fd_ofileflags,
663 	    (i = sizeof(char) * fdp->fd_nfiles));
664 	memset(newofileflags + i, 0, nfiles * sizeof(char) - i);
665 	if (fdp->fd_nfiles > NDFILE)
666 		free(fdp->fd_ofiles, M_FILEDESC);
667 	fdp->fd_ofiles = newofile;
668 	fdp->fd_ofileflags = newofileflags;
669 	fdp->fd_nfiles = nfiles;
670 	fdexpanded++;
671 }
672 
673 /*
674  * Check to see whether n user file descriptors
675  * are available to the process p.
676  */
677 int
678 fdavail(struct proc *p, int n)
679 {
680 	struct filedesc	*fdp;
681 	struct file	**fpp;
682 	int		i, lim;
683 
684 	fdp = p->p_fd;
685 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
686 	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
687 		return (1);
688 	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
689 	for (i = min(lim,fdp->fd_nfiles) - fdp->fd_freefile; --i >= 0; fpp++)
690 		if (*fpp == NULL && --n <= 0)
691 			return (1);
692 	return (0);
693 }
694 
695 /*
696  * Initialize the data structures necessary for managing files.
697  */
698 void
699 finit(void)
700 {
701 
702 	pool_init(&file_pool, sizeof(struct file), 0, 0, 0, "filepl",
703 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_FILE);
704 	pool_init(&cwdi_pool, sizeof(struct cwdinfo), 0, 0, 0, "cwdipl",
705 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_FILEDESC);
706 	pool_init(&filedesc0_pool, sizeof(struct filedesc0), 0, 0, 0, "fdescpl",
707 	    0, pool_page_alloc_nointr, pool_page_free_nointr, M_FILEDESC);
708 }
709 
710 /*
711  * Create a new open file structure and allocate
712  * a file decriptor for the process that refers to it.
713  */
714 int
715 falloc(struct proc *p, struct file **resultfp, int *resultfd)
716 {
717 	struct file	*fp, *fq;
718 	int		error, i;
719 
720  restart:
721 	if ((error = fdalloc(p, 0, &i)) != 0) {
722 		if (error == ENOSPC) {
723 			fdexpand(p);
724 			goto restart;
725 		}
726 		return (error);
727 	}
728 	if (nfiles >= maxfiles) {
729 		tablefull("file", "increase kern.maxfiles or MAXFILES");
730 		return (ENFILE);
731 	}
732 	/*
733 	 * Allocate a new file descriptor.
734 	 * If the process has file descriptor zero open, add to the list
735 	 * of open files at that point, otherwise put it at the front of
736 	 * the list of open files.
737 	 */
738 	nfiles++;
739 	fp = pool_get(&file_pool, PR_WAITOK);
740 	memset(fp, 0, sizeof(struct file));
741 	fp->f_iflags = FIF_LARVAL;
742 	if ((fq = p->p_fd->fd_ofiles[0]) != NULL) {
743 		LIST_INSERT_AFTER(fq, fp, f_list);
744 	} else {
745 		LIST_INSERT_HEAD(&filehead, fp, f_list);
746 	}
747 	p->p_fd->fd_ofiles[i] = fp;
748 	fp->f_count = 1;
749 	fp->f_cred = p->p_ucred;
750 	crhold(fp->f_cred);
751 	if (resultfp) {
752 		FILE_USE(fp);
753 		*resultfp = fp;
754 	}
755 	if (resultfd)
756 		*resultfd = i;
757 	return (0);
758 }
759 
760 /*
761  * Free a file descriptor.
762  */
763 void
764 ffree(struct file *fp)
765 {
766 
767 #ifdef DIAGNOSTIC
768 	if (fp->f_usecount)
769 		panic("ffree");
770 #endif
771 
772 	LIST_REMOVE(fp, f_list);
773 	crfree(fp->f_cred);
774 #ifdef DIAGNOSTIC
775 	fp->f_count = 0;
776 #endif
777 	nfiles--;
778 	pool_put(&file_pool, fp);
779 }
780 
781 /*
782  * Create an initial cwdinfo structure, using the same current and root
783  * directories as p.
784  */
785 struct cwdinfo *
786 cwdinit(struct proc *p)
787 {
788 	struct cwdinfo *cwdi;
789 
790 	cwdi = pool_get(&cwdi_pool, PR_WAITOK);
791 
792 	cwdi->cwdi_cdir = p->p_cwdi->cwdi_cdir;
793 	if (cwdi->cwdi_cdir)
794 		VREF(cwdi->cwdi_cdir);
795 	cwdi->cwdi_rdir = p->p_cwdi->cwdi_rdir;
796 	if (cwdi->cwdi_rdir)
797 		VREF(cwdi->cwdi_rdir);
798 	cwdi->cwdi_cmask =  p->p_cwdi->cwdi_cmask;
799 	cwdi->cwdi_refcnt = 1;
800 
801 	return (cwdi);
802 }
803 
804 /*
805  * Make p2 share p1's cwdinfo.
806  */
807 void
808 cwdshare(struct proc *p1, struct proc *p2)
809 {
810 
811 	p2->p_cwdi = p1->p_cwdi;
812 	p1->p_cwdi->cwdi_refcnt++;
813 }
814 
815 /*
816  * Make this process not share its cwdinfo structure, maintaining
817  * all cwdinfo state.
818  */
819 void
820 cwdunshare(struct proc *p)
821 {
822 	struct cwdinfo *newcwdi;
823 
824 	if (p->p_cwdi->cwdi_refcnt == 1)
825 		return;
826 
827 	newcwdi = cwdinit(p);
828 	cwdfree(p);
829 	p->p_cwdi = newcwdi;
830 }
831 
832 /*
833  * Release a cwdinfo structure.
834  */
835 void
836 cwdfree(struct proc *p)
837 {
838 	struct cwdinfo *cwdi;
839 
840 	cwdi = p->p_cwdi;
841 	if (--cwdi->cwdi_refcnt > 0)
842 		return;
843 
844 	p->p_cwdi = NULL;
845 
846 	vrele(cwdi->cwdi_cdir);
847 	if (cwdi->cwdi_rdir)
848 		vrele(cwdi->cwdi_rdir);
849 	pool_put(&cwdi_pool, cwdi);
850 }
851 
852 /*
853  * Create an initial filedesc structure, using the same current and root
854  * directories as p.
855  */
856 struct filedesc *
857 fdinit(struct proc *p)
858 {
859 	struct filedesc0 *newfdp;
860 
861 	newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
862 	memset(newfdp, 0, sizeof(struct filedesc0));
863 
864 	fdinit1(newfdp);
865 
866 	return (&newfdp->fd_fd);
867 }
868 
869 /*
870  * Initialize a file descriptor table.
871  */
872 void
873 fdinit1(struct filedesc0 *newfdp)
874 {
875 
876 	newfdp->fd_fd.fd_refcnt = 1;
877 	newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
878 	newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
879 	newfdp->fd_fd.fd_nfiles = NDFILE;
880 }
881 
882 /*
883  * Make p2 share p1's filedesc structure.
884  */
885 void
886 fdshare(struct proc *p1, struct proc *p2)
887 {
888 
889 	p2->p_fd = p1->p_fd;
890 	p1->p_fd->fd_refcnt++;
891 }
892 
893 /*
894  * Make this process not share its filedesc structure, maintaining
895  * all file descriptor state.
896  */
897 void
898 fdunshare(struct proc *p)
899 {
900 	struct filedesc *newfd;
901 
902 	if (p->p_fd->fd_refcnt == 1)
903 		return;
904 
905 	newfd = fdcopy(p);
906 	fdfree(p);
907 	p->p_fd = newfd;
908 }
909 
910 /*
911  * Clear a process's fd table.
912  */
913 void
914 fdclear(struct proc *p)
915 {
916 	struct filedesc *newfd;
917 
918 	newfd = fdinit(p);
919 	fdfree(p);
920 	p->p_fd = newfd;
921 }
922 
923 /*
924  * Copy a filedesc structure.
925  */
926 struct filedesc *
927 fdcopy(struct proc *p)
928 {
929 	struct filedesc	*newfdp, *fdp;
930 	struct file	**fpp;
931 	int		i;
932 
933 	fdp = p->p_fd;
934 	newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
935 	memcpy(newfdp, fdp, sizeof(struct filedesc));
936 	newfdp->fd_refcnt = 1;
937 
938 	/*
939 	 * If the number of open files fits in the internal arrays
940 	 * of the open file structure, use them, otherwise allocate
941 	 * additional memory for the number of descriptors currently
942 	 * in use.
943 	 */
944 	if (newfdp->fd_lastfile < NDFILE) {
945 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
946 		newfdp->fd_ofileflags =
947 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
948 		i = NDFILE;
949 	} else {
950 		/*
951 		 * Compute the smallest multiple of NDEXTENT needed
952 		 * for the file descriptors currently in use,
953 		 * allowing the table to shrink.
954 		 */
955 		i = newfdp->fd_nfiles;
956 		while (i >= 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
957 			i /= 2;
958 		newfdp->fd_ofiles = malloc(i * OFILESIZE, M_FILEDESC, M_WAITOK);
959 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
960 	}
961 	newfdp->fd_nfiles = i;
962 	memcpy(newfdp->fd_ofiles, fdp->fd_ofiles, i * sizeof(struct file **));
963 	memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags, i * sizeof(char));
964 	fpp = newfdp->fd_ofiles;
965 	for (i = newfdp->fd_lastfile; i >= 0; i--, fpp++)
966 		if (*fpp != NULL)
967 			(*fpp)->f_count++;
968 	return (newfdp);
969 }
970 
971 /*
972  * Release a filedesc structure.
973  */
974 void
975 fdfree(struct proc *p)
976 {
977 	struct filedesc	*fdp;
978 	struct file	**fpp, *fp;
979 	int		i;
980 
981 	fdp = p->p_fd;
982 	if (--fdp->fd_refcnt > 0)
983 		return;
984 	fpp = fdp->fd_ofiles;
985 	for (i = fdp->fd_lastfile; i >= 0; i--, fpp++) {
986 		fp = *fpp;
987 		if (fp != NULL) {
988 			*fpp = NULL;
989 			FILE_USE(fp);
990 			(void) closef(fp, p);
991 		}
992 	}
993 	p->p_fd = NULL;
994 	if (fdp->fd_nfiles > NDFILE)
995 		free(fdp->fd_ofiles, M_FILEDESC);
996 	pool_put(&filedesc0_pool, fdp);
997 }
998 
999 /*
1000  * Internal form of close.
1001  * Decrement reference count on file structure.
1002  * Note: p may be NULL when closing a file
1003  * that was being passed in a message.
1004  *
1005  * Note: we expect the caller is holding a usecount, and expects us
1006  * to drop it (the caller thinks the file is going away forever).
1007  */
1008 int
1009 closef(struct file *fp, struct proc *p)
1010 {
1011 	struct vnode	*vp;
1012 	struct flock	lf;
1013 	int		error;
1014 
1015 	if (fp == NULL)
1016 		return (0);
1017 
1018 	/*
1019 	 * POSIX record locking dictates that any close releases ALL
1020 	 * locks owned by this process.  This is handled by setting
1021 	 * a flag in the unlock to free ONLY locks obeying POSIX
1022 	 * semantics, and not to free BSD-style file locks.
1023 	 * If the descriptor was in a message, POSIX-style locks
1024 	 * aren't passed with the descriptor.
1025 	 */
1026 	if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
1027 		lf.l_whence = SEEK_SET;
1028 		lf.l_start = 0;
1029 		lf.l_len = 0;
1030 		lf.l_type = F_UNLCK;
1031 		vp = (struct vnode *)fp->f_data;
1032 		(void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
1033 	}
1034 
1035 	/*
1036 	 * If WANTCLOSE is set, then the reference count on the file
1037 	 * is 0, but there were multiple users of the file.  This can
1038 	 * happen if a filedesc structure is shared by multiple
1039 	 * processes.
1040 	 */
1041 	if (fp->f_iflags & FIF_WANTCLOSE) {
1042 		/*
1043 		 * Another user of the file is already closing, and is
1044 		 * simply waiting for other users of the file to drain.
1045 		 * Release our usecount, and wake up the closer if it
1046 		 * is the only remaining use.
1047 		 */
1048 #ifdef DIAGNOSTIC
1049 		if (fp->f_count != 0)
1050 			panic("closef: wantclose and count != 0");
1051 		if (fp->f_usecount < 2)
1052 			panic("closef: wantclose and usecount < 2");
1053 #endif
1054 		if (--fp->f_usecount == 1)
1055 			wakeup(&fp->f_usecount);
1056 		return (0);
1057 	} else {
1058 		/*
1059 		 * Decrement the reference count.  If we were not the
1060 		 * last reference, then release our use and just
1061 		 * return.
1062 		 */
1063 		if (--fp->f_count > 0) {
1064 #ifdef DIAGNOSTIC
1065 			if (fp->f_usecount < 1)
1066 				panic("closef: no wantclose and usecount < 1");
1067 #endif
1068 			fp->f_usecount--;
1069 			return (0);
1070 		}
1071 		if (fp->f_count < 0)
1072 			panic("closef: count < 0");
1073 	}
1074 
1075 	/*
1076 	 * The reference count is now 0.  However, there may be
1077 	 * multiple potential users of this file.  This can happen
1078 	 * if multiple processes shared a single filedesc structure.
1079 	 *
1080 	 * Notify these potential users that the file is closing.
1081 	 * This will prevent them from adding additional uses to
1082 	 * the file.
1083 	 */
1084 	fp->f_iflags |= FIF_WANTCLOSE;
1085 
1086 	/*
1087 	 * We expect the caller to add a use to the file.  So, if we
1088 	 * are the last user, usecount will be 1.  If it is not, we
1089 	 * must wait for the usecount to drain.  When it drains back
1090 	 * to 1, we will be awakened so that we may proceed with the
1091 	 * close.
1092 	 */
1093 #ifdef DIAGNOSTIC
1094 	if (fp->f_usecount < 1)
1095 		panic("closef: usecount < 1");
1096 #endif
1097 	while (fp->f_usecount > 1)
1098 		(void) tsleep(&fp->f_usecount, PRIBIO, "closef", 0);
1099 #ifdef DIAGNOSTIC
1100 	if (fp->f_usecount != 1)
1101 		panic("closef: usecount != 1");
1102 #endif
1103 
1104 	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1105 		lf.l_whence = SEEK_SET;
1106 		lf.l_start = 0;
1107 		lf.l_len = 0;
1108 		lf.l_type = F_UNLCK;
1109 		vp = (struct vnode *)fp->f_data;
1110 		(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1111 	}
1112 	if (fp->f_ops)
1113 		error = (*fp->f_ops->fo_close)(fp, p);
1114 	else
1115 		error = 0;
1116 
1117 	/* Nothing references the file now, drop the final use (us). */
1118 	fp->f_usecount--;
1119 
1120 	ffree(fp);
1121 	return (error);
1122 }
1123 
1124 /*
1125  * Apply an advisory lock on a file descriptor.
1126  *
1127  * Just attempt to get a record lock of the requested type on
1128  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1129  */
1130 /* ARGSUSED */
1131 int
1132 sys_flock(struct proc *p, void *v, register_t *retval)
1133 {
1134 	struct sys_flock_args /* {
1135 		syscallarg(int)	fd;
1136 		syscallarg(int)	how;
1137 	} */ *uap = v;
1138 	int		fd, how, error;
1139 	struct filedesc	*fdp;
1140 	struct file	*fp;
1141 	struct vnode	*vp;
1142 	struct flock	lf;
1143 
1144 	fd = SCARG(uap, fd);
1145 	how = SCARG(uap, how);
1146 	fdp = p->p_fd;
1147 	error = 0;
1148 
1149 	if ((fp = fd_getfile(fdp, fd)) == NULL)
1150 		return (EBADF);
1151 
1152 	FILE_USE(fp);
1153 
1154 	if (fp->f_type != DTYPE_VNODE) {
1155 		error = EOPNOTSUPP;
1156 		goto out;
1157 	}
1158 
1159 	vp = (struct vnode *)fp->f_data;
1160 	lf.l_whence = SEEK_SET;
1161 	lf.l_start = 0;
1162 	lf.l_len = 0;
1163 	if (how & LOCK_UN) {
1164 		lf.l_type = F_UNLCK;
1165 		fp->f_flag &= ~FHASLOCK;
1166 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1167 		goto out;
1168 	}
1169 	if (how & LOCK_EX)
1170 		lf.l_type = F_WRLCK;
1171 	else if (how & LOCK_SH)
1172 		lf.l_type = F_RDLCK;
1173 	else {
1174 		error = EINVAL;
1175 		goto out;
1176 	}
1177 	fp->f_flag |= FHASLOCK;
1178 	if (how & LOCK_NB)
1179 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK);
1180 	else
1181 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1182 		    F_FLOCK|F_WAIT);
1183  out:
1184 	FILE_UNUSE(fp, p);
1185 	return (error);
1186 }
1187 
1188 /*
1189  * File Descriptor pseudo-device driver (/dev/fd/).
1190  *
1191  * Opening minor device N dup()s the file (if any) connected to file
1192  * descriptor N belonging to the calling process.  Note that this driver
1193  * consists of only the ``open()'' routine, because all subsequent
1194  * references to this file will be direct to the other driver.
1195  */
1196 /* ARGSUSED */
1197 int
1198 filedescopen(dev_t dev, int mode, int type, struct proc *p)
1199 {
1200 
1201 	/*
1202 	 * XXX Kludge: set p->p_dupfd to contain the value of the
1203 	 * the file descriptor being sought for duplication. The error
1204 	 * return ensures that the vnode for this device will be released
1205 	 * by vn_open. Open will detect this special error and take the
1206 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1207 	 * will simply report the error.
1208 	 */
1209 	p->p_dupfd = minor(dev);
1210 	return (ENODEV);
1211 }
1212 
1213 /*
1214  * Duplicate the specified descriptor to a free descriptor.
1215  */
1216 int
1217 dupfdopen(struct proc *p, int indx, int dfd, int mode, int error)
1218 {
1219 	struct filedesc	*fdp;
1220 	struct file	*wfp, *fp;
1221 
1222 	fdp = p->p_fd;
1223 	/*
1224 	 * If the to-be-dup'd fd number is greater than the allowed number
1225 	 * of file descriptors, or the fd to be dup'd has already been
1226 	 * closed, reject.  Note, check for new == old is necessary as
1227 	 * falloc could allocate an already closed to-be-dup'd descriptor
1228 	 * as the new descriptor.
1229 	 */
1230 	fp = fdp->fd_ofiles[indx];
1231 
1232 	if ((wfp = fd_getfile(fdp, dfd)) == NULL)
1233 		return (EBADF);
1234 
1235 	if (fp == wfp)
1236 		return (EBADF);
1237 
1238 	FILE_USE(wfp);
1239 
1240 	/*
1241 	 * There are two cases of interest here.
1242 	 *
1243 	 * For ENODEV simply dup (dfd) to file descriptor
1244 	 * (indx) and return.
1245 	 *
1246 	 * For ENXIO steal away the file structure from (dfd) and
1247 	 * store it in (indx).  (dfd) is effectively closed by
1248 	 * this operation.
1249 	 *
1250 	 * Any other error code is just returned.
1251 	 */
1252 	switch (error) {
1253 	case ENODEV:
1254 		/*
1255 		 * Check that the mode the file is being opened for is a
1256 		 * subset of the mode of the existing descriptor.
1257 		 */
1258 		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
1259 			FILE_UNUSE(wfp, p);
1260 			return (EACCES);
1261 		}
1262 		fdp->fd_ofiles[indx] = wfp;
1263 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1264 		wfp->f_count++;
1265 		fd_used(fdp, indx);
1266 		FILE_UNUSE(wfp, p);
1267 		return (0);
1268 
1269 	case ENXIO:
1270 		/*
1271 		 * Steal away the file pointer from dfd, and stuff it into indx.
1272 		 */
1273 		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
1274 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1275 		fdp->fd_ofiles[dfd] = NULL;
1276 		fdp->fd_ofileflags[dfd] = 0;
1277 		/*
1278 		 * Complete the clean up of the filedesc structure by
1279 		 * recomputing the various hints.
1280 		 */
1281 		fd_used(fdp, indx);
1282 		fd_unused(fdp, dfd);
1283 		FILE_UNUSE(wfp, p);
1284 		return (0);
1285 
1286 	default:
1287 		FILE_UNUSE(wfp, p);
1288 		return (error);
1289 	}
1290 	/* NOTREACHED */
1291 }
1292 
1293 /*
1294  * fcntl call which is being passed to the file's fs.
1295  */
1296 int
1297 fcntl_forfs(int fd, struct proc *p, int cmd, void *arg)
1298 {
1299 	struct file	*fp;
1300 	struct filedesc	*fdp;
1301 	int		error;
1302 	u_int		size;
1303 	caddr_t		data, memp;
1304 #define STK_PARAMS	128
1305 	char		stkbuf[STK_PARAMS];
1306 
1307 	/* fd's value was validated in sys_fcntl before calling this routine */
1308 	fdp = p->p_fd;
1309 	fp = fdp->fd_ofiles[fd];
1310 
1311 	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
1312 		return (EBADF);
1313 
1314 	/*
1315 	 * Interpret high order word to find amount of data to be
1316 	 * copied to/from the user's address space.
1317 	 */
1318 	size = (size_t)F_PARAM_LEN(cmd);
1319 	if (size > F_PARAM_MAX)
1320 		return (EINVAL);
1321 	memp = NULL;
1322 	if (size > sizeof(stkbuf)) {
1323 		memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
1324 		data = memp;
1325 	} else
1326 		data = stkbuf;
1327 	if (cmd & F_FSIN) {
1328 		if (size) {
1329 			error = copyin(arg, data, size);
1330 			if (error) {
1331 				if (memp)
1332 					free(memp, M_IOCTLOPS);
1333 				return (error);
1334 			}
1335 		} else
1336 			*(caddr_t *)data = arg;
1337 	} else if ((cmd & F_FSOUT) && size)
1338 		/*
1339 		 * Zero the buffer so the user always
1340 		 * gets back something deterministic.
1341 		 */
1342 		memset(data, 0, size);
1343 	else if (cmd & F_FSVOID)
1344 		*(caddr_t *)data = arg;
1345 
1346 
1347 	error = (*fp->f_ops->fo_fcntl)(fp, cmd, data, p);
1348 
1349 	/*
1350 	 * Copy any data to user, size was
1351 	 * already set and checked above.
1352 	 */
1353 	if (error == 0 && (cmd & F_FSOUT) && size)
1354 		error = copyout(data, arg, size);
1355 	if (memp)
1356 		free(memp, M_IOCTLOPS);
1357 	return (error);
1358 }
1359 
1360 /*
1361  * Close any files on exec?
1362  */
1363 void
1364 fdcloseexec(struct proc *p)
1365 {
1366 	struct filedesc	*fdp;
1367 	int		fd;
1368 
1369 	fdunshare(p);
1370 	cwdunshare(p);
1371 
1372 	fdp = p->p_fd;
1373 	for (fd = 0; fd <= fdp->fd_lastfile; fd++)
1374 		if (fdp->fd_ofileflags[fd] & UF_EXCLOSE)
1375 			(void) fdrelease(p, fd);
1376 }
1377