2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1989, 1991, 1993
28 * The Regents of the University of California. All rights reserved.
29 * (c) UNIX System Laboratories, Inc.
30 * All or some portions of this file are derived from material licensed
31 * to the University of California by American Telephone and Telegraph
32 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
33 * the permission of UNIX System Laboratories, Inc.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/filedesc.h>
69 #include <sys/kernel.h>
70 #include <sys/vnode.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
76 #include <sys/ioctl.h>
77 #include <sys/fcntl.h>
78 #include <sys/malloc.h>
79 #include <sys/syslog.h>
80 #include <sys/unistd.h>
81 #include <sys/resourcevar.h>
83 #include <sys/mount.h>
86 * Descriptor management.
88 struct filelist filehead
; /* head of list of open files */
89 int nfiles
; /* actual number of open files */
91 static int frele_internal(struct file
*);
94 * System calls on descriptors.
98 getdtablesize(p
, uap
, retval
)
103 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
109 ogetdtablesize(p
, uap
, retval
)
114 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, NOFILE
);
119 void _fdrelse(fdp
, fd
)
120 register struct filedesc
*fdp
;
123 if (fd
< fdp
->fd_freefile
)
124 fdp
->fd_freefile
= fd
;
126 if (fd
> fdp
->fd_lastfile
)
127 panic("fdrelse: fd_lastfile inconsistent");
129 fdp
->fd_ofiles
[fd
] = NULL
;
130 fdp
->fd_ofileflags
[fd
] = 0;
131 while ((fd
= fdp
->fd_lastfile
) > 0 &&
132 fdp
->fd_ofiles
[fd
] == NULL
&&
133 !(fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
138 * Duplicate a file descriptor.
147 struct dup_args
*uap
;
150 register struct filedesc
*fdp
= p
->p_fd
;
151 register int old
= uap
->fd
;
154 if ((u_int
)old
>= fdp
->fd_nfiles
||
155 fdp
->fd_ofiles
[old
] == NULL
||
156 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
))
158 if (error
= fdalloc(p
, 0, &new))
160 return (finishdup(fdp
, old
, new, retval
));
164 * Duplicate a file descriptor to a particular value.
174 struct dup2_args
*uap
;
177 register struct filedesc
*fdp
= p
->p_fd
;
178 register int old
= uap
->from
, new = uap
->to
;
181 if ((u_int
)old
>= fdp
->fd_nfiles
||
182 fdp
->fd_ofiles
[old
] == NULL
||
183 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
) ||
184 (u_int
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
185 (u_int
)new >= maxfiles
)
191 if ((u_int
)new >= fdp
->fd_nfiles
) {
192 if (error
= fdalloc(p
, new, &i
))
202 if ((flags
= fdp
->fd_ofileflags
[new]) & UF_RESERVED
)
204 fdp
->fd_ofileflags
[new] = (flags
& ~UF_MAPPED
) | UF_RESERVED
;
206 * dup2() must succeed even if the close has an error.
208 if (*(fpp
= &fdp
->fd_ofiles
[new])) {
209 struct file
*fp
= *fpp
;
212 (void) closef(fp
, p
);
215 return (finishdup(fdp
, old
, new, retval
));
219 * The file control system call.
228 fcntl(p
, uap
, retval
)
230 register struct fcntl_args
*uap
;
234 register struct filedesc
*fdp
= p
->p_fd
;
235 register struct file
*fp
;
237 struct vnode
*vp
, *devvp
;
238 int i
, tmp
, error
, error2
, flg
= F_POSIX
;
240 fstore_t alloc_struct
; /* structure for allocate command */
241 u_int32_t alloc_flags
= 0;
242 off_t offset
; /* used for F_SETSIZE */
244 struct radvisory ra_struct
;
245 fbootstraptransfer_t fbt_struct
; /* for F_READBOOTSTRAP and F_WRITEBOOTSTRAP */
246 struct log2phys l2p_struct
; /* structure for allocate command */
248 int devBlockSize
= 0;
250 if ((u_int
)fd
>= fdp
->fd_nfiles
||
251 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
252 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
254 pop
= &fdp
->fd_ofileflags
[fd
];
258 newmin
= (long)uap
->arg
;
259 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
260 (u_int
)newmin
>= maxfiles
)
262 if (error
= fdalloc(p
, newmin
, &i
))
264 return (finishdup(fdp
, fd
, i
, retval
));
267 *retval
= (*pop
& UF_EXCLOSE
)? 1 : 0;
271 *pop
= (*pop
&~ UF_EXCLOSE
) |
272 ((long)(uap
->arg
) & 1)? UF_EXCLOSE
: 0;
276 *retval
= OFLAGS(fp
->f_flag
);
280 fp
->f_flag
&= ~FCNTLFLAGS
;
281 fp
->f_flag
|= FFLAGS((long)uap
->arg
) & FCNTLFLAGS
;
282 tmp
= fp
->f_flag
& FNONBLOCK
;
283 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
286 tmp
= fp
->f_flag
& FASYNC
;
287 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, p
);
290 fp
->f_flag
&= ~FNONBLOCK
;
292 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
296 if (fp
->f_type
== DTYPE_SOCKET
) {
297 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
300 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, p
);
305 if (fp
->f_type
== DTYPE_SOCKET
) {
306 ((struct socket
*)fp
->f_data
)->so_pgid
=
310 if ((long)uap
->arg
<= 0) {
311 uap
->arg
= (int)(-(long)(uap
->arg
));
313 struct proc
*p1
= pfind((long)uap
->arg
);
316 uap
->arg
= (int)p1
->p_pgrp
->pg_id
;
318 return (fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&uap
->arg
, p
));
322 /* Fall into F_SETLK */
325 if (fp
->f_type
!= DTYPE_VNODE
)
327 vp
= (struct vnode
*)fp
->f_data
;
328 /* Copy in the lock structure */
329 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&fl
,
333 if (fl
.l_whence
== SEEK_CUR
)
334 fl
.l_start
+= fp
->f_offset
;
338 if ((fp
->f_flag
& FREAD
) == 0)
340 p
->p_flag
|= P_ADVLOCK
;
341 return (VOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
));
344 if ((fp
->f_flag
& FWRITE
) == 0)
346 p
->p_flag
|= P_ADVLOCK
;
347 return (VOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
));
350 return (VOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &fl
,
358 if (fp
->f_type
!= DTYPE_VNODE
)
360 vp
= (struct vnode
*)fp
->f_data
;
361 /* Copy in the lock structure */
362 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&fl
,
366 if (fl
.l_whence
== SEEK_CUR
)
367 fl
.l_start
+= fp
->f_offset
;
368 if (error
= VOP_ADVLOCK(vp
, (caddr_t
)p
, F_GETLK
, &fl
, F_POSIX
))
370 return (copyout((caddr_t
)&fl
, (caddr_t
)uap
->arg
,
374 if (fp
->f_type
!= DTYPE_VNODE
)
377 /* make sure that we have write permission */
378 if ((fp
->f_flag
& FWRITE
) == 0)
381 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&alloc_struct
,
382 sizeof (alloc_struct
));
386 /* now set the space allocated to 0 */
387 alloc_struct
.fst_bytesalloc
= 0;
390 * Do some simple parameter checking
393 /* set up the flags */
395 alloc_flags
|= PREALLOCATE
;
397 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
398 alloc_flags
|= ALLOCATECONTIG
;
400 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
401 alloc_flags
|= ALLOCATEALL
;
404 * Do any position mode specific stuff. The only
405 * position mode supported now is PEOFPOSMODE
408 switch (alloc_struct
.fst_posmode
) {
411 if (alloc_struct
.fst_offset
!= 0)
414 alloc_flags
|= ALLOCATEFROMPEOF
;
418 if (alloc_struct
.fst_offset
<= 0)
421 alloc_flags
|= ALLOCATEFROMVOL
;
428 vp
= (struct vnode
*)fp
->f_data
;
430 /* lock the vnode and call allocate to get the space */
431 error
= vn_lock(vp
, LK_EXCLUSIVE
|LK_RETRY
, p
);
434 error
= VOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
435 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
437 VOP_UNLOCK(vp
, 0, p
);
439 if (error2
= copyout((caddr_t
)&alloc_struct
,
441 sizeof (alloc_struct
))) {
450 if (fp
->f_type
!= DTYPE_VNODE
)
453 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&offset
,
459 * Make sure that we are root. Growing a file
460 * without zero filling the data is a security hole
461 * root would have access anyway so we'll allow it
467 vp
= (struct vnode
*)fp
->f_data
;
469 /* lock the vnode and call allocate to get the space */
470 error
= vn_lock(vp
, LK_EXCLUSIVE
|LK_RETRY
, p
);
473 error
= VOP_TRUNCATE(vp
,offset
,IO_NOZEROFILL
,fp
->f_cred
,p
);
478 if (fp
->f_type
!= DTYPE_VNODE
)
480 vp
= (struct vnode
*)fp
->f_data
;
482 simple_lock(&vp
->v_interlock
);
484 vp
->v_flag
&= ~VRAOFF
;
486 vp
->v_flag
|= VRAOFF
;
487 simple_unlock(&vp
->v_interlock
);
491 if (fp
->f_type
!= DTYPE_VNODE
)
493 vp
= (struct vnode
*)fp
->f_data
;
495 simple_lock(&vp
->v_interlock
);
497 vp
->v_flag
|= VNOCACHE_DATA
;
499 vp
->v_flag
&= ~VNOCACHE_DATA
;
500 simple_unlock(&vp
->v_interlock
);
504 if (fp
->f_type
!= DTYPE_VNODE
)
506 vp
= (struct vnode
*)fp
->f_data
;
508 if (error
= copyin((caddr_t
)uap
->arg
,
509 (caddr_t
)&ra_struct
, sizeof (ra_struct
)))
511 return (VOP_IOCTL(vp
, 1, (caddr_t
)&ra_struct
, 0, fp
->f_cred
, p
));
513 case F_READBOOTSTRAP
:
514 case F_WRITEBOOTSTRAP
:
515 if (fp
->f_type
!= DTYPE_VNODE
)
518 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&fbt_struct
,
519 sizeof (fbt_struct
));
523 if (uap
->cmd
== F_WRITEBOOTSTRAP
) {
525 * Make sure that we are root. Updating the
526 * bootstrap on a disk could be a security hole
532 vp
= (struct vnode
*)fp
->f_data
;
533 if (vp
->v_tag
!= VT_HFS
) /* XXX */
536 /* lock the vnode and call VOP_IOCTL to handle the I/O */
537 error
= vn_lock(vp
, LK_EXCLUSIVE
|LK_RETRY
, p
);
540 error
= VOP_IOCTL(vp
, (uap
->cmd
== F_WRITEBOOTSTRAP
) ? 3 : 2,
541 (caddr_t
)&fbt_struct
, 0, fp
->f_cred
, p
);
547 if (fp
->f_type
!= DTYPE_VNODE
)
549 vp
= (struct vnode
*)fp
->f_data
;
550 error
= vn_lock(vp
, LK_EXCLUSIVE
|LK_RETRY
, p
);
553 if (VOP_OFFTOBLK(vp
, fp
->f_offset
, &lbn
))
554 panic("fcntl LOG2PHYS OFFTOBLK");
555 if (VOP_BLKTOOFF(vp
, lbn
, &offset
))
556 panic("fcntl LOG2PHYS BLKTOOFF1");
557 error
= VOP_BMAP(vp
, lbn
, &devvp
, &bn
, 0);
558 VOP_DEVBLOCKSIZE(devvp
, &devBlockSize
);
559 VOP_UNLOCK(vp
, 0, p
);
561 l2p_struct
.l2p_flags
= 0; /* for now */
562 l2p_struct
.l2p_contigbytes
= 0; /* for now */
563 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
564 l2p_struct
.l2p_devoffset
+= fp
->f_offset
- offset
;
565 error
= copyout((caddr_t
)&l2p_struct
,
567 sizeof (l2p_struct
));
578 * Common code for dup, dup2, and fcntl(F_DUPFD).
581 finishdup(fdp
, old
, new, retval
)
582 register struct filedesc
*fdp
;
583 register int old
, new;
586 register struct file
*fp
;
588 if ((fp
= fdp
->fd_ofiles
[old
]) == NULL
||
589 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
593 fdp
->fd_ofiles
[new] = fp
;
594 fdp
->fd_ofileflags
[new] = fdp
->fd_ofileflags
[old
] &~ UF_EXCLOSE
;
596 if (new > fdp
->fd_lastfile
)
597 fdp
->fd_lastfile
= new;
603 * Close a file descriptor.
610 close(p
, uap
, retval
)
612 struct close_args
*uap
;
616 register struct filedesc
*fdp
= p
->p_fd
;
617 register struct file
*fp
;
619 if ((u_int
)fd
>= fdp
->fd_nfiles
||
620 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
621 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
624 return (closef(fp
, p
));
628 * Return status information about a file descriptor.
636 fstat(p
, uap
, retval
)
638 register struct fstat_args
*uap
;
642 register struct filedesc
*fdp
= p
->p_fd
;
643 register struct file
*fp
;
647 if ((u_int
)fd
>= fdp
->fd_nfiles
||
648 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
649 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
651 switch (fp
->f_type
) {
654 error
= vn_stat((struct vnode
*)fp
->f_data
, &ub
, p
);
658 error
= soo_stat((struct socket
*)fp
->f_data
, &ub
);
662 error
= pshm_stat((void *)fp
->f_data
, &ub
);
669 error
= copyout((caddr_t
)&ub
, (caddr_t
)uap
->sb
,
676 * Return status information about a file descriptor.
683 ofstat(p
, uap
, retval
)
685 register struct ofstat_args
*uap
;
689 register struct filedesc
*fdp
= p
->p_fd
;
690 register struct file
*fp
;
695 if ((u_int
)fd
>= fdp
->fd_nfiles
||
696 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
697 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
699 switch (fp
->f_type
) {
702 error
= vn_stat((struct vnode
*)fp
->f_data
, &ub
, p
);
706 error
= soo_stat((struct socket
*)fp
->f_data
, &ub
);
715 error
= copyout((caddr_t
)&oub
, (caddr_t
)uap
->sb
,
719 #endif /* COMPAT_43 */
722 * Return pathconf information about a file descriptor.
724 struct fpathconf_args
{
729 fpathconf(p
, uap
, retval
)
731 register struct fpathconf_args
*uap
;
735 struct filedesc
*fdp
= p
->p_fd
;
739 if ((u_int
)fd
>= fdp
->fd_nfiles
||
740 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
741 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
743 switch (fp
->f_type
) {
746 if (uap
->name
!= _PC_PIPE_BUF
)
752 vp
= (struct vnode
*)fp
->f_data
;
753 return (VOP_PATHCONF(vp
, uap
->name
, retval
));
762 * Allocate a file descriptor for the process.
767 fdalloc(p
, want
, result
)
772 register struct filedesc
*fdp
= p
->p_fd
;
774 int lim
, last
, nfiles
, oldnfiles
;
775 struct file
**newofiles
, **ofiles
;
776 char *newofileflags
, *ofileflags
;
779 * Search for a free descriptor starting at the higher
780 * of want or fd_freefile. If that fails, consider
781 * expanding the ofile array.
783 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
785 last
= min(fdp
->fd_nfiles
, lim
);
786 if ((i
= want
) < fdp
->fd_freefile
)
787 i
= fdp
->fd_freefile
;
788 ofiles
= &fdp
->fd_ofiles
[i
];
789 ofileflags
= &fdp
->fd_ofileflags
[i
];
790 for (; i
< last
; i
++) {
791 if (*ofiles
== NULL
&& !(*ofileflags
& UF_RESERVED
)) {
792 *ofileflags
= UF_RESERVED
;
793 if (i
> fdp
->fd_lastfile
)
794 fdp
->fd_lastfile
= i
;
795 if (want
<= fdp
->fd_freefile
)
796 fdp
->fd_freefile
= i
;
800 ofiles
++; ofileflags
++;
804 * No space in current array. Expand?
806 if (fdp
->fd_nfiles
>= lim
)
808 if (fdp
->fd_nfiles
< NDEXTENT
)
811 nfiles
= 2 * fdp
->fd_nfiles
;
815 MALLOC_ZONE(newofiles
, struct file
**,
816 nfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
817 if (fdp
->fd_nfiles
>= nfiles
) {
818 FREE_ZONE(newofiles
, nfiles
* OFILESIZE
, M_OFILETABL
);
821 newofileflags
= (char *) &newofiles
[nfiles
];
823 * Copy the existing ofile and ofileflags arrays
824 * and zero the new portion of each array.
826 oldnfiles
= fdp
->fd_nfiles
;
827 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
828 oldnfiles
* sizeof *fdp
->fd_ofiles
);
829 (void) memset(&newofiles
[oldnfiles
], 0,
830 (nfiles
- oldnfiles
) * sizeof *fdp
->fd_ofiles
);
832 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
833 oldnfiles
* sizeof *fdp
->fd_ofileflags
);
834 (void) memset(&newofileflags
[oldnfiles
], 0,
835 (nfiles
- oldnfiles
) *
836 sizeof *fdp
->fd_ofileflags
);
837 ofiles
= fdp
->fd_ofiles
;
838 fdp
->fd_ofiles
= newofiles
;
839 fdp
->fd_ofileflags
= newofileflags
;
840 fdp
->fd_nfiles
= nfiles
;
841 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
847 * Check to see whether n user file descriptors
848 * are available to the process p.
855 register struct filedesc
*fdp
= p
->p_fd
;
856 register struct file
**fpp
;
857 register char *flags
;
860 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
861 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
863 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
864 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
865 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
866 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
876 _fdrelse(p
->p_fd
, fd
);
880 fdgetf(p
, fd
, resultfp
)
881 register struct proc
*p
;
883 struct file
**resultfp
;
885 register struct filedesc
*fdp
= p
->p_fd
;
888 if ((u_int
)fd
>= fdp
->fd_nfiles
||
889 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
890 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
899 * Create a new open file structure and allocate
900 * a file decriptor for the process that refers to it.
903 falloc(p
, resultfp
, resultfd
)
904 register struct proc
*p
;
905 struct file
**resultfp
;
908 register struct file
*fp
, *fq
;
911 if (error
= fdalloc(p
, 0, &i
))
913 if (nfiles
>= maxfiles
) {
918 * Allocate a new file descriptor.
919 * If the process has file descriptor zero open, add to the list
920 * of open files at that point, otherwise put it at the front of
921 * the list of open files.
924 MALLOC_ZONE(fp
, struct file
*, sizeof(struct file
), M_FILE
, M_WAITOK
);
925 bzero(fp
, sizeof(struct file
));
926 if (fq
= p
->p_fd
->fd_ofiles
[0]) {
927 LIST_INSERT_AFTER(fq
, fp
, f_list
);
929 LIST_INSERT_HEAD(&filehead
, fp
, f_list
);
931 p
->p_fd
->fd_ofiles
[i
] = fp
;
933 fp
->f_cred
= p
->p_ucred
;
943 * Free a file structure.
947 register struct file
*fp
;
949 register struct file
*fq
;
952 LIST_REMOVE(fp
, f_list
);
954 if (cred
!= NOCRED
) {
960 memset(fp
, 0xff, sizeof *fp
);
961 fp
->f_count
= (short)0xffff;
963 FREE_ZONE(fp
, sizeof *fp
, M_FILE
);
970 register struct filedesc
*fdp
= p
->p_fd
;
971 register int i
= fdp
->fd_lastfile
;
972 register struct file
**fpp
= &fdp
->fd_ofiles
[i
];
973 register char *flags
= &fdp
->fd_ofileflags
[i
];
976 if ((*flags
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
) {
977 register struct file
*fp
= *fpp
;
979 *fpp
= NULL
; *flags
= 0;
980 if (i
== fdp
->fd_lastfile
&& i
> 0)
985 *flags
&= ~UF_MAPPED
;
992 * Copy a filedesc structure.
998 register struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
1001 MALLOC_ZONE(newfdp
, struct filedesc
*,
1002 sizeof *newfdp
, M_FILEDESC
, M_WAITOK
);
1003 (void) memcpy(newfdp
, fdp
, sizeof *newfdp
);
1004 VREF(newfdp
->fd_cdir
);
1005 if (newfdp
->fd_rdir
)
1006 VREF(newfdp
->fd_rdir
);
1007 newfdp
->fd_refcnt
= 1;
1010 * If the number of open files fits in the internal arrays
1011 * of the open file structure, use them, otherwise allocate
1012 * additional memory for the number of descriptors currently
1015 if (newfdp
->fd_lastfile
< NDFILE
)
1019 * Compute the smallest multiple of NDEXTENT needed
1020 * for the file descriptors currently in use,
1021 * allowing the table to shrink.
1023 i
= newfdp
->fd_nfiles
;
1024 while (i
> 2 * NDEXTENT
&& i
> newfdp
->fd_lastfile
* 2)
1027 MALLOC_ZONE(newfdp
->fd_ofiles
, struct file
**,
1028 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
1029 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
1030 newfdp
->fd_nfiles
= i
;
1031 if (fdp
->fd_nfiles
> 0) {
1032 register struct file
**fpp
;
1033 register char *flags
;
1035 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
1036 i
* sizeof *fdp
->fd_ofiles
);
1037 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
1038 i
* sizeof *fdp
->fd_ofileflags
);
1040 fpp
= newfdp
->fd_ofiles
;
1041 flags
= newfdp
->fd_ofileflags
;
1042 for (i
= newfdp
->fd_lastfile
; i
-- >= 0; fpp
++, flags
++)
1043 if (*fpp
!= NULL
&& !(*flags
& UF_RESERVED
)) {
1050 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
1056 * Release a filedesc structure.
1062 struct filedesc
*fdp
;
1067 if ((fdp
= p
->p_fd
) == NULL
)
1069 if (--fdp
->fd_refcnt
> 0)
1072 if (fdp
->fd_nfiles
> 0) {
1073 fpp
= fdp
->fd_ofiles
;
1074 for (i
= fdp
->fd_lastfile
; i
-- >= 0; fpp
++)
1076 (void) closef(*fpp
, p
);
1077 FREE_ZONE(fdp
->fd_ofiles
,
1078 fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
1081 fdp
->fd_cdir
= NULL
;
1085 fdp
->fd_rdir
= NULL
;
1088 FREE_ZONE(fdp
, sizeof *fdp
, M_FILEDESC
);
1092 closef_finish(fp
, p
)
1093 register struct file
*fp
;
1094 register struct proc
*p
;
1100 if ((fp
->f_flag
& FHASLOCK
) && fp
->f_type
== DTYPE_VNODE
) {
1101 lf
.l_whence
= SEEK_SET
;
1104 lf
.l_type
= F_UNLCK
;
1105 vp
= (struct vnode
*)fp
->f_data
;
1106 (void) VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_UNLCK
, &lf
, F_FLOCK
);
1109 error
= fo_close(fp
, p
);
1117 * Internal form of close.
1118 * Decrement reference count on file structure.
1119 * Note: p may be NULL when closing a file
1120 * that was being passed in a message.
1124 register struct file
*fp
;
1125 register struct proc
*p
;
1134 * POSIX record locking dictates that any close releases ALL
1135 * locks owned by this process. This is handled by setting
1136 * a flag in the unlock to free ONLY locks obeying POSIX
1137 * semantics, and not to free BSD-style file locks.
1138 * If the descriptor was in a message, POSIX-style locks
1139 * aren't passed with the descriptor.
1141 if (p
&& (p
->p_flag
& P_ADVLOCK
) && fp
->f_type
== DTYPE_VNODE
) {
1142 lf
.l_whence
= SEEK_SET
;
1145 lf
.l_type
= F_UNLCK
;
1146 vp
= (struct vnode
*)fp
->f_data
;
1147 (void) VOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
);
1149 if (frele_internal(fp
) > 0)
1151 return(closef_finish(fp
, p
));
1155 * Apply an advisory lock on a file descriptor.
1157 * Just attempt to get a record lock of the requested type on
1158 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1166 flock(p
, uap
, retval
)
1168 register struct flock_args
*uap
;
1173 register struct filedesc
*fdp
= p
->p_fd
;
1174 register struct file
*fp
;
1178 if ((u_int
)fd
>= fdp
->fd_nfiles
||
1179 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1180 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
1182 if (fp
->f_type
!= DTYPE_VNODE
)
1183 return (EOPNOTSUPP
);
1184 vp
= (struct vnode
*)fp
->f_data
;
1185 lf
.l_whence
= SEEK_SET
;
1188 if (how
& LOCK_UN
) {
1189 lf
.l_type
= F_UNLCK
;
1190 fp
->f_flag
&= ~FHASLOCK
;
1191 return (VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_UNLCK
, &lf
, F_FLOCK
));
1194 lf
.l_type
= F_WRLCK
;
1195 else if (how
& LOCK_SH
)
1196 lf
.l_type
= F_RDLCK
;
1199 fp
->f_flag
|= FHASLOCK
;
1201 return (VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, F_FLOCK
));
1202 return (VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
));
1206 * File Descriptor pseudo-device driver (/dev/fd/).
1208 * Opening minor device N dup()s the file (if any) connected to file
1209 * descriptor N belonging to the calling process. Note that this driver
1210 * consists of only the ``open()'' routine, because all subsequent
1211 * references to this file will be direct to the other driver.
1215 fdopen(dev
, mode
, type
, p
)
1222 * XXX Kludge: set curproc->p_dupfd to contain the value of the
1223 * the file descriptor being sought for duplication. The error
1224 * return ensures that the vnode for this device will be released
1225 * by vn_open. Open will detect this special error and take the
1226 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1227 * will simply report the error.
1229 p
->p_dupfd
= minor(dev
);
1234 * Duplicate the specified descriptor to a free descriptor.
1237 dupfdopen(fdp
, indx
, dfd
, mode
, error
)
1238 register struct filedesc
*fdp
;
1239 register int indx
, dfd
;
1243 register struct file
*wfp
;
1247 * If the to-be-dup'd fd number is greater than the allowed number
1248 * of file descriptors, or the fd to be dup'd has already been
1249 * closed, reject. Note, check for new == old is necessary as
1250 * falloc could allocate an already closed to-be-dup'd descriptor
1251 * as the new descriptor.
1253 fp
= fdp
->fd_ofiles
[indx
];
1254 if ((u_int
)dfd
>= fdp
->fd_nfiles
||
1255 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
1256 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
))
1260 * There are two cases of interest here.
1262 * For ENODEV simply dup (dfd) to file descriptor
1263 * (indx) and return.
1265 * For ENXIO steal away the file structure from (dfd) and
1266 * store it in (indx). (dfd) is effectively closed by
1269 * Any other error code is just returned.
1274 * Check that the mode the file is being opened for is a
1275 * subset of the mode of the existing descriptor.
1277 if (((mode
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
)
1280 if (indx
> fdp
->fd_lastfile
)
1281 fdp
->fd_lastfile
= indx
;;
1282 fdp
->fd_ofiles
[indx
] = wfp
;
1283 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
1288 * Steal away the file pointer from dfd, and stuff it into indx.
1290 if (indx
> fdp
->fd_lastfile
)
1291 fdp
->fd_lastfile
= indx
;;
1292 fdp
->fd_ofiles
[indx
] = fdp
->fd_ofiles
[dfd
];
1293 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
1303 /* Reference manipulation routines for the file structure */
1306 fref(struct file
*fp
)
1308 if (fp
->f_count
== (short)0xffff)
1310 if (++fp
->f_count
<= 0)
1311 panic("fref: f_count");
1312 return ((int)fp
->f_count
);
1316 frele_internal(struct file
*fp
)
1318 if (fp
->f_count
== (short)0xffff)
1319 panic("frele: stale");
1320 if (--fp
->f_count
< 0)
1321 panic("frele: count < 0");
1322 return ((int)fp
->f_count
);
1327 frele(struct file
*fp
)
1331 extern int disable_funnel
;
1333 fnl
= thread_funnel_get();
1335 * If the funnels are merged then atleast a funnel should be held
1336 * else frele should come in with kernel funnel only
1338 if (!disable_funnel
&& (fnl
!= kernel_flock
)) {
1339 panic("frele: kernel funnel not held");
1341 } else if (fnl
== THR_FUNNEL_NULL
) {
1342 panic("frele: no funnel held");
1345 if ((count
= frele_internal(fp
)) == 0) {
1346 /* some one closed the fd while we were blocked */
1347 (void)closef_finish(fp
, current_proc());
1353 fcount(struct file
*fp
)
1355 if (fp
->f_count
== (short)0xffff)
1356 panic("fcount: stale");
1357 return ((int)fp
->f_count
);