2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1989, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
63 * CHW 8/5/98 Added F_SETSIZE command to truncate without
65 * CHW 7/6/98 Updated Preallocate command to take a structure
67 * CHW 6/25/98 Fixed a bug in the lock call in fcntl
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/filedesc.h>
74 #include <sys/kernel.h>
75 #include <sys/vnode.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
81 #include <sys/ioctl.h>
82 #include <sys/fcntl.h>
83 #include <sys/malloc.h>
84 #include <sys/syslog.h>
85 #include <sys/unistd.h>
86 #include <sys/resourcevar.h>
88 #include <sys/mount.h>
91 * Descriptor management.
93 struct filelist filehead
; /* head of list of open files */
94 int nfiles
; /* actual number of open files */
97 * System calls on descriptors.
101 getdtablesize(p
, uap
, retval
)
107 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
113 ogetdtablesize(p
, uap
, retval
)
119 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, NOFILE
);
124 void _fdrelse(fdp
, fd
)
125 register struct filedesc
*fdp
;
128 if (fd
< fdp
->fd_freefile
)
129 fdp
->fd_freefile
= fd
;
131 if (fd
> fdp
->fd_lastfile
)
132 panic("fdrelse: fd_lastfile inconsistent");
134 fdp
->fd_ofiles
[fd
] = NULL
;
135 fdp
->fd_ofileflags
[fd
] = 0;
136 while ((fd
= fdp
->fd_lastfile
) > 0 &&
137 fdp
->fd_ofiles
[fd
] == NULL
&&
138 !(fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
143 * Duplicate a file descriptor.
152 struct dup_args
*uap
;
155 register struct filedesc
*fdp
= p
->p_fd
;
156 register int old
= uap
->fd
;
159 if ((u_int
)old
>= fdp
->fd_nfiles
||
160 fdp
->fd_ofiles
[old
] == NULL
||
161 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
))
163 if (error
= fdalloc(p
, 0, &new))
165 return (finishdup(fdp
, old
, new, retval
));
169 * Duplicate a file descriptor to a particular value.
179 struct dup2_args
*uap
;
182 register struct filedesc
*fdp
= p
->p_fd
;
183 register int old
= uap
->from
, new = uap
->to
;
186 if ((u_int
)old
>= fdp
->fd_nfiles
||
187 fdp
->fd_ofiles
[old
] == NULL
||
188 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
) ||
189 (u_int
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
190 (u_int
)new >= maxfiles
)
196 if ((u_int
)new >= fdp
->fd_nfiles
) {
197 if (error
= fdalloc(p
, new, &i
))
208 if ((flags
= fdp
->fd_ofileflags
[new]) & UF_RESERVED
)
210 fdp
->fd_ofileflags
[new] = (flags
& ~UF_MAPPED
) | UF_RESERVED
;
212 * dup2() must succeed even if the close has an error.
214 if (*(fpp
= &fdp
->fd_ofiles
[new])) {
215 struct file
*fp
= *fpp
;
217 *fpp
= NULL
; (void) closef(fp
, p
);
220 return (finishdup(fdp
, old
, new, retval
));
224 * The file control system call.
233 fcntl(p
, uap
, retval
)
235 register struct fcntl_args
*uap
;
239 register struct filedesc
*fdp
= p
->p_fd
;
240 register struct file
*fp
;
242 struct vnode
*vp
, *devvp
;
243 int i
, tmp
, error
, error2
, flg
= F_POSIX
;
245 fstore_t alloc_struct
; /* structure for allocate command */
246 u_int32_t alloc_flags
= 0;
247 off_t offset
; /* used for F_SETSIZE */
249 struct radvisory ra_struct
;
250 fbootstraptransfer_t fbt_struct
; /* for F_READBOOTSTRAP and F_WRITEBOOTSTRAP */
251 struct log2phys l2p_struct
; /* structure for allocate command */
253 int devBlockSize
= 0;
255 if ((u_int
)fd
>= fdp
->fd_nfiles
||
256 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
257 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
259 pop
= &fdp
->fd_ofileflags
[fd
];
263 newmin
= (long)uap
->arg
;
264 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
265 (u_int
)newmin
>= maxfiles
)
267 if (error
= fdalloc(p
, newmin
, &i
))
269 return (finishdup(fdp
, fd
, i
, retval
));
272 *retval
= (*pop
& UF_EXCLOSE
)? 1 : 0;
276 *pop
= (*pop
&~ UF_EXCLOSE
) |
277 ((long)(uap
->arg
) & 1)? UF_EXCLOSE
: 0;
281 *retval
= OFLAGS(fp
->f_flag
);
285 fp
->f_flag
&= ~FCNTLFLAGS
;
286 fp
->f_flag
|= FFLAGS((long)uap
->arg
) & FCNTLFLAGS
;
287 tmp
= fp
->f_flag
& FNONBLOCK
;
288 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
291 tmp
= fp
->f_flag
& FASYNC
;
292 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, FIOASYNC
, (caddr_t
)&tmp
, p
);
295 fp
->f_flag
&= ~FNONBLOCK
;
297 (void) (*fp
->f_ops
->fo_ioctl
)(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
301 if (fp
->f_type
== DTYPE_SOCKET
) {
302 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
305 error
= (*fp
->f_ops
->fo_ioctl
)
306 (fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, p
);
311 if (fp
->f_type
== DTYPE_SOCKET
) {
312 ((struct socket
*)fp
->f_data
)->so_pgid
=
316 if ((long)uap
->arg
<= 0) {
317 uap
->arg
= (void *)(-(long)(uap
->arg
));
319 struct proc
*p1
= pfind((long)uap
->arg
);
322 uap
->arg
= (void *)(long)p1
->p_pgrp
->pg_id
;
324 return ((*fp
->f_ops
->fo_ioctl
)
325 (fp
, (int)TIOCSPGRP
, (caddr_t
)&uap
->arg
, p
));
329 /* Fall into F_SETLK */
332 if (fp
->f_type
!= DTYPE_VNODE
)
334 vp
= (struct vnode
*)fp
->f_data
;
335 /* Copy in the lock structure */
336 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&fl
,
340 if (fl
.l_whence
== SEEK_CUR
)
341 fl
.l_start
+= fp
->f_offset
;
345 if ((fp
->f_flag
& FREAD
) == 0)
347 p
->p_flag
|= P_ADVLOCK
;
348 return (VOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
));
351 if ((fp
->f_flag
& FWRITE
) == 0)
353 p
->p_flag
|= P_ADVLOCK
;
354 return (VOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
));
357 return (VOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &fl
,
365 if (fp
->f_type
!= DTYPE_VNODE
)
367 vp
= (struct vnode
*)fp
->f_data
;
368 /* Copy in the lock structure */
369 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&fl
,
373 if (fl
.l_whence
== SEEK_CUR
)
374 fl
.l_start
+= fp
->f_offset
;
375 if (error
= VOP_ADVLOCK(vp
, (caddr_t
)p
, F_GETLK
, &fl
, F_POSIX
))
377 return (copyout((caddr_t
)&fl
, (caddr_t
)uap
->arg
,
382 /* Copy in the structure */
384 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&alloc_struct
,
385 sizeof (alloc_struct
));
390 /* now set the space allocated to 0 and pass it out in
391 case we get a parameter checking error */
393 alloc_struct
.fst_bytesalloc
= 0;
395 error
= copyout((caddr_t
)&alloc_struct
, (caddr_t
)uap
->arg
,
396 sizeof (alloc_struct
));
401 /* First make sure that we have write permission */
403 if ((fp
->f_flag
& FWRITE
) == 0)
407 /* Do some simple parameter checking */
410 /* set up the flags */
412 alloc_flags
|= PREALLOCATE
;
414 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
) {
415 alloc_flags
|= ALLOCATECONTIG
;
418 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
) {
419 alloc_flags
|= ALLOCATEALL
;
422 /* Do any position mode specific stuff. The only */
423 /* position mode supported now is PEOFPOSMODE */
425 switch (alloc_struct
.fst_posmode
) {
429 if ((alloc_struct
.fst_offset
!= 0) ||
430 (alloc_struct
.fst_length
< 0))
433 alloc_flags
|= ALLOCATEFROMPEOF
;
443 /* Now lock the vnode and call allocate to get the space */
445 vp
= (struct vnode
*)fp
->f_data
;
447 VOP_LOCK(vp
,LK_EXCLUSIVE
,p
);
448 error
= VOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
449 &alloc_struct
.fst_bytesalloc
,fp
->f_cred
,p
);
452 if (error2
= (copyout((caddr_t
)&alloc_struct
, (caddr_t
)uap
->arg
,
453 sizeof (alloc_struct
)))) {
465 /* Copy in the structure */
467 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&offset
,
474 /* First make sure that we are root. Growing a file */
475 /* without zero filling the data is a security hole */
476 /* root would have access anyway so we'll allow it */
482 /* Now lock the vnode and call allocate to get the space */
484 vp
= (struct vnode
*)fp
->f_data
;
486 VOP_LOCK(vp
,LK_EXCLUSIVE
,p
);
487 error
= VOP_TRUNCATE(vp
,offset
,IO_NOZEROFILL
,fp
->f_cred
,p
);
493 vp
= (struct vnode
*)fp
->f_data
;
495 simple_lock(&vp
->v_interlock
);
497 vp
->v_flag
&= ~VRAOFF
;
499 vp
->v_flag
|= VRAOFF
;
500 simple_unlock(&vp
->v_interlock
);
505 vp
= (struct vnode
*)fp
->f_data
;
507 simple_lock(&vp
->v_interlock
);
509 vp
->v_flag
|= VNOCACHE_DATA
;
511 vp
->v_flag
&= ~VNOCACHE_DATA
;
512 simple_unlock(&vp
->v_interlock
);
517 vp
= (struct vnode
*)fp
->f_data
;
519 if (error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&ra_struct
, sizeof (ra_struct
)))
521 return (VOP_IOCTL(vp
, 1, &ra_struct
, 0, fp
->f_cred
, p
));
523 case F_READBOOTSTRAP
:
524 case F_WRITEBOOTSTRAP
:
526 /* Copy in the structure */
528 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&fbt_struct
,
529 sizeof (fbt_struct
));
535 if (uap
->cmd
== F_WRITEBOOTSTRAP
) {
536 /* First make sure that we are root. Updating the */
537 /* bootstrap on a disk could be a security hole */
544 /* Now lock the vnode and call VOP_IOCTL to handle the I/O: */
546 vp
= (struct vnode
*)fp
->f_data
;
547 if (vp
->v_tag
!= VT_HFS
) {
550 VOP_LOCK(vp
,LK_EXCLUSIVE
,p
);
551 error
= VOP_IOCTL(vp
, (uap
->cmd
== F_WRITEBOOTSTRAP
) ? 3 : 2, &fbt_struct
, 0, fp
->f_cred
, p
);
558 if (fp
->f_type
!= DTYPE_VNODE
)
560 vp
= (struct vnode
*)fp
->f_data
;
561 VOP_LOCK(vp
, LK_EXCLUSIVE
, p
);
562 if (VOP_OFFTOBLK(vp
, fp
->f_offset
, &lbn
))
563 panic("fcntl LOG2PHYS OFFTOBLK");
564 if (VOP_BLKTOOFF(vp
, lbn
, &offset
))
565 panic("fcntl LOG2PHYS BLKTOOFF1");
566 error
= VOP_BMAP(vp
, lbn
, &devvp
, &bn
, 0);
567 VOP_DEVBLOCKSIZE(devvp
, &devBlockSize
);
568 VOP_UNLOCK(vp
, 0, p
);
570 l2p_struct
.l2p_flags
= 0; /* for now */
571 l2p_struct
.l2p_contigbytes
= 0; /* for now */
572 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
573 l2p_struct
.l2p_devoffset
+= fp
->f_offset
- offset
;
574 error
= copyout((caddr_t
)&l2p_struct
,
576 sizeof (l2p_struct
));
588 * Common code for dup, dup2, and fcntl(F_DUPFD).
591 finishdup(fdp
, old
, new, retval
)
592 register struct filedesc
*fdp
;
593 register int old
, new;
596 register struct file
*fp
;
598 if ((fp
= fdp
->fd_ofiles
[old
]) == NULL
||
599 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
603 fdp
->fd_ofiles
[new] = fp
;
604 fdp
->fd_ofileflags
[new] = fdp
->fd_ofileflags
[old
] &~ UF_EXCLOSE
;
606 if (new > fdp
->fd_lastfile
)
607 fdp
->fd_lastfile
= new;
613 * Close a file descriptor.
620 close(p
, uap
, retval
)
622 struct close_args
*uap
;
626 register struct filedesc
*fdp
= p
->p_fd
;
627 register struct file
*fp
;
629 if ((u_int
)fd
>= fdp
->fd_nfiles
||
630 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
631 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
634 return (closef(fp
, p
));
638 * Return status information about a file descriptor.
646 fstat(p
, uap
, retval
)
648 register struct fstat_args
*uap
;
652 register struct filedesc
*fdp
= p
->p_fd
;
653 register struct file
*fp
;
657 if ((u_int
)fd
>= fdp
->fd_nfiles
||
658 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
659 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
661 switch (fp
->f_type
) {
664 error
= vn_stat((struct vnode
*)fp
->f_data
, &ub
, p
);
668 error
= soo_stat((struct socket
*)fp
->f_data
, &ub
);
672 error
= pshm_stat((void *)fp
->f_data
, &ub
);
679 error
= copyout((caddr_t
)&ub
, (caddr_t
)uap
->sb
,
686 * Return status information about a file descriptor.
693 ofstat(p
, uap
, retval
)
695 register struct ofstat_args
*uap
;
699 register struct filedesc
*fdp
= p
->p_fd
;
700 register struct file
*fp
;
705 if ((u_int
)fd
>= fdp
->fd_nfiles
||
706 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
707 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
709 switch (fp
->f_type
) {
712 error
= vn_stat((struct vnode
*)fp
->f_data
, &ub
, p
);
716 error
= soo_stat((struct socket
*)fp
->f_data
, &ub
);
725 error
= copyout((caddr_t
)&oub
, (caddr_t
)uap
->sb
,
729 #endif /* COMPAT_43 */
732 * Return pathconf information about a file descriptor.
734 struct fpathconf_args
{
739 fpathconf(p
, uap
, retval
)
741 register struct fpathconf_args
*uap
;
745 struct filedesc
*fdp
= p
->p_fd
;
749 if ((u_int
)fd
>= fdp
->fd_nfiles
||
750 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
751 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
753 switch (fp
->f_type
) {
756 if (uap
->name
!= _PC_PIPE_BUF
)
762 vp
= (struct vnode
*)fp
->f_data
;
763 return (VOP_PATHCONF(vp
, uap
->name
, retval
));
772 * Allocate a file descriptor for the process.
777 fdalloc(p
, want
, result
)
782 register struct filedesc
*fdp
= p
->p_fd
;
784 int lim
, last
, nfiles
, oldnfiles
;
785 struct file
**newofiles
, **ofiles
;
786 char *newofileflags
, *ofileflags
;
789 * Search for a free descriptor starting at the higher
790 * of want or fd_freefile. If that fails, consider
791 * expanding the ofile array.
793 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
795 last
= min(fdp
->fd_nfiles
, lim
);
796 if ((i
= want
) < fdp
->fd_freefile
)
797 i
= fdp
->fd_freefile
;
798 ofiles
= &fdp
->fd_ofiles
[i
];
799 ofileflags
= &fdp
->fd_ofileflags
[i
];
800 for (; i
< last
; i
++) {
801 if (*ofiles
== NULL
&& !(*ofileflags
& UF_RESERVED
)) {
802 *ofileflags
= UF_RESERVED
;
803 if (i
> fdp
->fd_lastfile
)
804 fdp
->fd_lastfile
= i
;
805 if (want
<= fdp
->fd_freefile
)
806 fdp
->fd_freefile
= i
;
810 ofiles
++; ofileflags
++;
814 * No space in current array. Expand?
816 if (fdp
->fd_nfiles
>= lim
)
818 if (fdp
->fd_nfiles
< NDEXTENT
)
821 nfiles
= 2 * fdp
->fd_nfiles
;
825 MALLOC_ZONE(newofiles
, struct file
**,
826 nfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
827 if (fdp
->fd_nfiles
>= nfiles
) {
828 FREE_ZONE(newofiles
, nfiles
* OFILESIZE
, M_OFILETABL
);
831 newofileflags
= (char *) &newofiles
[nfiles
];
833 * Copy the existing ofile and ofileflags arrays
834 * and zero the new portion of each array.
836 oldnfiles
= fdp
->fd_nfiles
;
837 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
838 oldnfiles
* sizeof *fdp
->fd_ofiles
);
839 (void) memset(&newofiles
[oldnfiles
], 0,
840 (nfiles
- oldnfiles
) * sizeof *fdp
->fd_ofiles
);
842 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
843 oldnfiles
* sizeof *fdp
->fd_ofileflags
);
844 (void) memset(&newofileflags
[oldnfiles
], 0,
845 (nfiles
- oldnfiles
) *
846 sizeof *fdp
->fd_ofileflags
);
847 ofiles
= fdp
->fd_ofiles
;
848 fdp
->fd_ofiles
= newofiles
;
849 fdp
->fd_ofileflags
= newofileflags
;
850 fdp
->fd_nfiles
= nfiles
;
851 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
857 * Check to see whether n user file descriptors
858 * are available to the process p.
865 register struct filedesc
*fdp
= p
->p_fd
;
866 register struct file
**fpp
;
867 register char *flags
;
870 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
871 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
873 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
874 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
875 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
876 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
886 _fdrelse(p
->p_fd
, fd
);
890 fdgetf(p
, fd
, resultfp
)
891 register struct proc
*p
;
893 struct file
**resultfp
;
895 register struct filedesc
*fdp
= p
->p_fd
;
898 if ((u_int
)fd
>= fdp
->fd_nfiles
||
899 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
900 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
909 * Create a new open file structure and allocate
910 * a file decriptor for the process that refers to it.
913 falloc(p
, resultfp
, resultfd
)
914 register struct proc
*p
;
915 struct file
**resultfp
;
918 register struct file
*fp
, *fq
;
921 if (error
= fdalloc(p
, 0, &i
))
923 if (nfiles
>= maxfiles
) {
928 * Allocate a new file descriptor.
929 * If the process has file descriptor zero open, add to the list
930 * of open files at that point, otherwise put it at the front of
931 * the list of open files.
934 MALLOC_ZONE(fp
, struct file
*, sizeof(struct file
), M_FILE
, M_WAITOK
);
935 bzero(fp
, sizeof(struct file
));
936 if (fq
= p
->p_fd
->fd_ofiles
[0]) {
937 LIST_INSERT_AFTER(fq
, fp
, f_list
);
939 LIST_INSERT_HEAD(&filehead
, fp
, f_list
);
941 p
->p_fd
->fd_ofiles
[i
] = fp
;
943 fp
->f_cred
= p
->p_ucred
;
953 * Free a file structure.
957 register struct file
*fp
;
959 register struct file
*fq
;
962 LIST_REMOVE(fp
, f_list
);
964 if (cred
!= NOCRED
) {
972 FREE_ZONE(fp
, sizeof *fp
, M_FILE
);
979 register struct filedesc
*fdp
= p
->p_fd
;
980 register int i
= fdp
->fd_lastfile
;
981 register struct file
**fpp
= &fdp
->fd_ofiles
[i
];
982 register char *flags
= &fdp
->fd_ofileflags
[i
];
985 if ((*flags
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
) {
986 register struct file
*fp
= *fpp
;
988 *fpp
= NULL
; *flags
= 0;
989 if (i
== fdp
->fd_lastfile
&& i
> 0)
994 *flags
&= ~UF_MAPPED
;
1001 * Copy a filedesc structure.
1007 register struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
1010 MALLOC_ZONE(newfdp
, struct filedesc
*,
1011 sizeof *newfdp
, M_FILEDESC
, M_WAITOK
);
1012 (void) memcpy(newfdp
, fdp
, sizeof *newfdp
);
1013 VREF(newfdp
->fd_cdir
);
1014 if (newfdp
->fd_rdir
)
1015 VREF(newfdp
->fd_rdir
);
1016 newfdp
->fd_refcnt
= 1;
1019 * If the number of open files fits in the internal arrays
1020 * of the open file structure, use them, otherwise allocate
1021 * additional memory for the number of descriptors currently
1024 if (newfdp
->fd_lastfile
< NDFILE
)
1028 * Compute the smallest multiple of NDEXTENT needed
1029 * for the file descriptors currently in use,
1030 * allowing the table to shrink.
1032 i
= newfdp
->fd_nfiles
;
1033 while (i
> 2 * NDEXTENT
&& i
> newfdp
->fd_lastfile
* 2)
1036 MALLOC_ZONE(newfdp
->fd_ofiles
, struct file
**,
1037 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
1038 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
1039 newfdp
->fd_nfiles
= i
;
1040 if (fdp
->fd_nfiles
> 0) {
1041 register struct file
**fpp
;
1042 register char *flags
;
1044 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
1045 i
* sizeof *fdp
->fd_ofiles
);
1046 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
1047 i
* sizeof *fdp
->fd_ofileflags
);
1049 fpp
= newfdp
->fd_ofiles
;
1050 flags
= newfdp
->fd_ofileflags
;
1051 for (i
= newfdp
->fd_lastfile
; i
-- >= 0; fpp
++, flags
++)
1052 if (*fpp
!= NULL
&& !(*flags
& UF_RESERVED
)) {
1060 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
1066 * Release a filedesc structure.
1072 register struct filedesc
*fdp
;
1073 register struct file
**fpp
;
1076 if ((fdp
= p
->p_fd
) == NULL
)
1078 if (--fdp
->fd_refcnt
> 0)
1081 if (fdp
->fd_nfiles
> 0) {
1082 fpp
= fdp
->fd_ofiles
;
1083 for (i
= fdp
->fd_lastfile
; i
-- >= 0; fpp
++)
1085 (void) closef(*fpp
, p
);
1086 FREE_ZONE(fdp
->fd_ofiles
,
1087 fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
1089 vrele(fdp
->fd_cdir
);
1091 vrele(fdp
->fd_rdir
);
1092 FREE_ZONE(fdp
, sizeof *fdp
, M_FILEDESC
);
1096 * Internal form of close.
1097 * Decrement reference count on file structure.
1098 * Note: p may be NULL when closing a file
1099 * that was being passed in a message.
1103 register struct file
*fp
;
1104 register struct proc
*p
;
1113 * POSIX record locking dictates that any close releases ALL
1114 * locks owned by this process. This is handled by setting
1115 * a flag in the unlock to free ONLY locks obeying POSIX
1116 * semantics, and not to free BSD-style file locks.
1117 * If the descriptor was in a message, POSIX-style locks
1118 * aren't passed with the descriptor.
1120 if (p
&& (p
->p_flag
& P_ADVLOCK
) && fp
->f_type
== DTYPE_VNODE
) {
1121 lf
.l_whence
= SEEK_SET
;
1124 lf
.l_type
= F_UNLCK
;
1125 vp
= (struct vnode
*)fp
->f_data
;
1126 (void) VOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
);
1130 if ((fp
->f_flag
& FHASLOCK
) && fp
->f_type
== DTYPE_VNODE
) {
1131 lf
.l_whence
= SEEK_SET
;
1134 lf
.l_type
= F_UNLCK
;
1135 vp
= (struct vnode
*)fp
->f_data
;
1136 (void) VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_UNLCK
, &lf
, F_FLOCK
);
1139 error
= (*fp
->f_ops
->fo_close
)(fp
, p
);
1147 * Apply an advisory lock on a file descriptor.
1149 * Just attempt to get a record lock of the requested type on
1150 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1158 flock(p
, uap
, retval
)
1160 register struct flock_args
*uap
;
1165 register struct filedesc
*fdp
= p
->p_fd
;
1166 register struct file
*fp
;
1170 if ((u_int
)fd
>= fdp
->fd_nfiles
||
1171 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1172 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
1174 if (fp
->f_type
!= DTYPE_VNODE
)
1175 return (EOPNOTSUPP
);
1176 vp
= (struct vnode
*)fp
->f_data
;
1177 lf
.l_whence
= SEEK_SET
;
1180 if (how
& LOCK_UN
) {
1181 lf
.l_type
= F_UNLCK
;
1182 fp
->f_flag
&= ~FHASLOCK
;
1183 return (VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_UNLCK
, &lf
, F_FLOCK
));
1186 lf
.l_type
= F_WRLCK
;
1187 else if (how
& LOCK_SH
)
1188 lf
.l_type
= F_RDLCK
;
1191 fp
->f_flag
|= FHASLOCK
;
1193 return (VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, F_FLOCK
));
1194 return (VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
));
1198 * File Descriptor pseudo-device driver (/dev/fd/).
1200 * Opening minor device N dup()s the file (if any) connected to file
1201 * descriptor N belonging to the calling process. Note that this driver
1202 * consists of only the ``open()'' routine, because all subsequent
1203 * references to this file will be direct to the other driver.
1207 fdopen(dev
, mode
, type
, p
)
1214 * XXX Kludge: set curproc->p_dupfd to contain the value of the
1215 * the file descriptor being sought for duplication. The error
1216 * return ensures that the vnode for this device will be released
1217 * by vn_open. Open will detect this special error and take the
1218 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1219 * will simply report the error.
1221 p
->p_dupfd
= minor(dev
);
1226 * Duplicate the specified descriptor to a free descriptor.
1229 dupfdopen(fdp
, indx
, dfd
, mode
, error
)
1230 register struct filedesc
*fdp
;
1231 register int indx
, dfd
;
1235 register struct file
*wfp
;
1239 * If the to-be-dup'd fd number is greater than the allowed number
1240 * of file descriptors, or the fd to be dup'd has already been
1241 * closed, reject. Note, check for new == old is necessary as
1242 * falloc could allocate an already closed to-be-dup'd descriptor
1243 * as the new descriptor.
1245 fp
= fdp
->fd_ofiles
[indx
];
1246 if ((u_int
)dfd
>= fdp
->fd_nfiles
||
1247 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
1248 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
))
1252 * There are two cases of interest here.
1254 * For ENODEV simply dup (dfd) to file descriptor
1255 * (indx) and return.
1257 * For ENXIO steal away the file structure from (dfd) and
1258 * store it in (indx). (dfd) is effectively closed by
1261 * Any other error code is just returned.
1266 * Check that the mode the file is being opened for is a
1267 * subset of the mode of the existing descriptor.
1269 if (((mode
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
)
1272 if (indx
> fdp
->fd_lastfile
)
1273 fdp
->fd_lastfile
= indx
;;
1274 fdp
->fd_ofiles
[indx
] = wfp
;
1275 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
1280 * Steal away the file pointer from dfd, and stuff it into indx.
1282 if (indx
> fdp
->fd_lastfile
)
1283 fdp
->fd_lastfile
= indx
;;
1284 fdp
->fd_ofiles
[indx
] = fdp
->fd_ofiles
[dfd
];
1285 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
1295 /* Reference manipulation routines for the file structure */
1298 fref(struct file
*fp
)
1300 if (++fp
->f_count
<= 0)
1301 panic("fref: f_count");
1302 return ((int)fp
->f_count
);
1306 frele(struct file
*fp
)
1308 if (--fp
->f_count
< 0)
1309 panic("frele: count < 0");
1310 return ((int)fp
->f_count
);
1314 fcount(struct file
*fp
)
1316 return ((int)fp
->f_count
);