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