2 * Copyright (c) 2000-2002 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 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/filedesc.h>
66 #include <sys/kernel.h>
67 #include <sys/vnode.h>
70 #include <sys/socket.h>
71 #include <sys/socketvar.h>
73 #include <sys/ioctl.h>
74 #include <sys/fcntl.h>
75 #include <sys/malloc.h>
76 #include <sys/syslog.h>
77 #include <sys/unistd.h>
78 #include <sys/resourcevar.h>
80 #include <sys/mount.h>
83 * Descriptor management.
85 struct filelist filehead
; /* head of list of open files */
86 int nfiles
; /* actual number of open files */
88 static int frele_internal(struct file
*);
91 * System calls on descriptors.
95 getdtablesize(p
, uap
, retval
)
100 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
106 ogetdtablesize(p
, uap
, retval
)
111 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, NOFILE
);
116 void _fdrelse(fdp
, fd
)
117 register struct filedesc
*fdp
;
120 if (fd
< fdp
->fd_freefile
)
121 fdp
->fd_freefile
= fd
;
123 if (fd
> fdp
->fd_lastfile
)
124 panic("fdrelse: fd_lastfile inconsistent");
126 fdp
->fd_ofiles
[fd
] = NULL
;
127 fdp
->fd_ofileflags
[fd
] = 0;
128 while ((fd
= fdp
->fd_lastfile
) > 0 &&
129 fdp
->fd_ofiles
[fd
] == NULL
&&
130 !(fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
135 * Duplicate a file descriptor.
144 struct dup_args
*uap
;
147 register struct filedesc
*fdp
= p
->p_fd
;
148 register int old
= uap
->fd
;
151 if ((u_int
)old
>= fdp
->fd_nfiles
||
152 fdp
->fd_ofiles
[old
] == NULL
||
153 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
))
155 if (error
= fdalloc(p
, 0, &new))
157 return (finishdup(fdp
, old
, new, retval
));
161 * Duplicate a file descriptor to a particular value.
171 struct dup2_args
*uap
;
174 register struct filedesc
*fdp
= p
->p_fd
;
175 register int old
= uap
->from
, new = uap
->to
;
178 if ((u_int
)old
>= fdp
->fd_nfiles
||
179 fdp
->fd_ofiles
[old
] == NULL
||
180 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
) ||
181 (u_int
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
182 (u_int
)new >= maxfiles
)
188 if ((u_int
)new >= fdp
->fd_nfiles
) {
189 if (error
= fdalloc(p
, new, &i
))
199 if ((flags
= fdp
->fd_ofileflags
[new]) & UF_RESERVED
)
201 fdp
->fd_ofileflags
[new] = (flags
& ~UF_MAPPED
) | UF_RESERVED
;
203 * dup2() must succeed even if the close has an error.
205 if (*(fpp
= &fdp
->fd_ofiles
[new])) {
206 struct file
*fp
= *fpp
;
209 (void) closef(fp
, p
);
212 return (finishdup(fdp
, old
, new, retval
));
216 * The file control system call.
225 fcntl(p
, uap
, retval
)
227 register struct fcntl_args
*uap
;
231 register struct filedesc
*fdp
= p
->p_fd
;
232 register struct file
*fp
;
234 struct vnode
*vp
, *devvp
;
235 int i
, tmp
, error
, error2
, flg
= F_POSIX
;
237 fstore_t alloc_struct
; /* structure for allocate command */
238 u_int32_t alloc_flags
= 0;
239 off_t offset
; /* used for F_SETSIZE */
241 struct radvisory ra_struct
;
242 fbootstraptransfer_t fbt_struct
; /* for F_READBOOTSTRAP and F_WRITEBOOTSTRAP */
243 struct log2phys l2p_struct
; /* structure for allocate command */
245 int devBlockSize
= 0;
247 if ((u_int
)fd
>= fdp
->fd_nfiles
||
248 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
249 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
251 pop
= &fdp
->fd_ofileflags
[fd
];
255 newmin
= (long)uap
->arg
;
256 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
257 (u_int
)newmin
>= maxfiles
)
259 if (error
= fdalloc(p
, newmin
, &i
))
261 return (finishdup(fdp
, fd
, i
, retval
));
264 *retval
= (*pop
& UF_EXCLOSE
)? 1 : 0;
268 *pop
= (*pop
&~ UF_EXCLOSE
) |
269 ((long)(uap
->arg
) & 1)? UF_EXCLOSE
: 0;
273 *retval
= OFLAGS(fp
->f_flag
);
277 fp
->f_flag
&= ~FCNTLFLAGS
;
278 fp
->f_flag
|= FFLAGS((long)uap
->arg
) & FCNTLFLAGS
;
279 tmp
= fp
->f_flag
& FNONBLOCK
;
280 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
283 tmp
= fp
->f_flag
& FASYNC
;
284 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, p
);
287 fp
->f_flag
&= ~FNONBLOCK
;
289 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
293 if (fp
->f_type
== DTYPE_SOCKET
) {
294 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
297 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, p
);
302 if (fp
->f_type
== DTYPE_SOCKET
) {
303 ((struct socket
*)fp
->f_data
)->so_pgid
=
307 if ((long)uap
->arg
<= 0) {
308 uap
->arg
= (void *)(-(long)(uap
->arg
));
310 struct proc
*p1
= pfind((long)uap
->arg
);
313 uap
->arg
= (void *)(long)p1
->p_pgrp
->pg_id
;
315 return (fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&uap
->arg
, p
));
319 /* Fall into F_SETLK */
322 if (fp
->f_type
!= DTYPE_VNODE
)
324 vp
= (struct vnode
*)fp
->f_data
;
325 /* Copy in the lock structure */
326 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&fl
,
330 if (fl
.l_whence
== SEEK_CUR
)
331 fl
.l_start
+= fp
->f_offset
;
335 if ((fp
->f_flag
& FREAD
) == 0)
337 p
->p_flag
|= P_ADVLOCK
;
338 return (VOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
));
341 if ((fp
->f_flag
& FWRITE
) == 0)
343 p
->p_flag
|= P_ADVLOCK
;
344 return (VOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
));
347 return (VOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &fl
,
355 if (fp
->f_type
!= DTYPE_VNODE
)
357 vp
= (struct vnode
*)fp
->f_data
;
358 /* Copy in the lock structure */
359 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&fl
,
363 if (fl
.l_whence
== SEEK_CUR
)
364 fl
.l_start
+= fp
->f_offset
;
365 if (error
= VOP_ADVLOCK(vp
, (caddr_t
)p
, F_GETLK
, &fl
, F_POSIX
))
367 return (copyout((caddr_t
)&fl
, (caddr_t
)uap
->arg
,
371 if (fp
->f_type
!= DTYPE_VNODE
)
374 /* make sure that we have write permission */
375 if ((fp
->f_flag
& FWRITE
) == 0)
378 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&alloc_struct
,
379 sizeof (alloc_struct
));
383 /* now set the space allocated to 0 */
384 alloc_struct
.fst_bytesalloc
= 0;
387 * Do some simple parameter checking
390 /* set up the flags */
392 alloc_flags
|= PREALLOCATE
;
394 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
395 alloc_flags
|= ALLOCATECONTIG
;
397 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
398 alloc_flags
|= ALLOCATEALL
;
401 * Do any position mode specific stuff. The only
402 * position mode supported now is PEOFPOSMODE
405 switch (alloc_struct
.fst_posmode
) {
408 if (alloc_struct
.fst_offset
!= 0)
411 alloc_flags
|= ALLOCATEFROMPEOF
;
415 if (alloc_struct
.fst_offset
<= 0)
418 alloc_flags
|= ALLOCATEFROMVOL
;
425 vp
= (struct vnode
*)fp
->f_data
;
427 /* lock the vnode and call allocate to get the space */
428 error
= vn_lock(vp
, LK_EXCLUSIVE
|LK_RETRY
, p
);
431 error
= VOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
432 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
434 VOP_UNLOCK(vp
, 0, p
);
436 if (error2
= copyout((caddr_t
)&alloc_struct
,
438 sizeof (alloc_struct
))) {
447 if (fp
->f_type
!= DTYPE_VNODE
)
450 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&offset
,
456 * Make sure that we are root. Growing a file
457 * without zero filling the data is a security hole
458 * root would have access anyway so we'll allow it
464 vp
= (struct vnode
*)fp
->f_data
;
466 /* lock the vnode and call allocate to get the space */
467 error
= vn_lock(vp
, LK_EXCLUSIVE
|LK_RETRY
, p
);
470 error
= VOP_TRUNCATE(vp
,offset
,IO_NOZEROFILL
,fp
->f_cred
,p
);
475 if (fp
->f_type
!= DTYPE_VNODE
)
477 vp
= (struct vnode
*)fp
->f_data
;
479 simple_lock(&vp
->v_interlock
);
481 vp
->v_flag
&= ~VRAOFF
;
483 vp
->v_flag
|= VRAOFF
;
484 simple_unlock(&vp
->v_interlock
);
488 if (fp
->f_type
!= DTYPE_VNODE
)
490 vp
= (struct vnode
*)fp
->f_data
;
492 simple_lock(&vp
->v_interlock
);
494 vp
->v_flag
|= VNOCACHE_DATA
;
496 vp
->v_flag
&= ~VNOCACHE_DATA
;
497 simple_unlock(&vp
->v_interlock
);
501 if (fp
->f_type
!= DTYPE_VNODE
)
503 vp
= (struct vnode
*)fp
->f_data
;
505 if (error
= copyin((caddr_t
)uap
->arg
,
506 (caddr_t
)&ra_struct
, sizeof (ra_struct
)))
508 return (VOP_IOCTL(vp
, 1, &ra_struct
, 0, fp
->f_cred
, p
));
510 case F_READBOOTSTRAP
:
511 case F_WRITEBOOTSTRAP
:
512 if (fp
->f_type
!= DTYPE_VNODE
)
515 error
= copyin((caddr_t
)uap
->arg
, (caddr_t
)&fbt_struct
,
516 sizeof (fbt_struct
));
520 if (uap
->cmd
== F_WRITEBOOTSTRAP
) {
522 * Make sure that we are root. Updating the
523 * bootstrap on a disk could be a security hole
529 vp
= (struct vnode
*)fp
->f_data
;
530 if (vp
->v_tag
!= VT_HFS
) /* XXX */
533 /* lock the vnode and call VOP_IOCTL to handle the I/O */
534 error
= vn_lock(vp
, LK_EXCLUSIVE
|LK_RETRY
, p
);
537 error
= VOP_IOCTL(vp
, (uap
->cmd
== F_WRITEBOOTSTRAP
) ? 3 : 2,
538 &fbt_struct
, 0, fp
->f_cred
, p
);
544 if (fp
->f_type
!= DTYPE_VNODE
)
546 vp
= (struct vnode
*)fp
->f_data
;
547 error
= vn_lock(vp
, LK_EXCLUSIVE
|LK_RETRY
, p
);
550 if (VOP_OFFTOBLK(vp
, fp
->f_offset
, &lbn
))
551 panic("fcntl LOG2PHYS OFFTOBLK");
552 if (VOP_BLKTOOFF(vp
, lbn
, &offset
))
553 panic("fcntl LOG2PHYS BLKTOOFF1");
554 error
= VOP_BMAP(vp
, lbn
, &devvp
, &bn
, 0);
555 VOP_DEVBLOCKSIZE(devvp
, &devBlockSize
);
556 VOP_UNLOCK(vp
, 0, p
);
558 l2p_struct
.l2p_flags
= 0; /* for now */
559 l2p_struct
.l2p_contigbytes
= 0; /* for now */
560 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
561 l2p_struct
.l2p_devoffset
+= fp
->f_offset
- offset
;
562 error
= copyout((caddr_t
)&l2p_struct
,
564 sizeof (l2p_struct
));
575 * Common code for dup, dup2, and fcntl(F_DUPFD).
578 finishdup(fdp
, old
, new, retval
)
579 register struct filedesc
*fdp
;
580 register int old
, new;
583 register struct file
*fp
;
585 if ((fp
= fdp
->fd_ofiles
[old
]) == NULL
||
586 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
590 fdp
->fd_ofiles
[new] = fp
;
591 fdp
->fd_ofileflags
[new] = fdp
->fd_ofileflags
[old
] &~ UF_EXCLOSE
;
593 if (new > fdp
->fd_lastfile
)
594 fdp
->fd_lastfile
= new;
600 * Close a file descriptor.
607 close(p
, uap
, retval
)
609 struct close_args
*uap
;
613 register struct filedesc
*fdp
= p
->p_fd
;
614 register struct file
*fp
;
616 if ((u_int
)fd
>= fdp
->fd_nfiles
||
617 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
618 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
621 return (closef(fp
, p
));
625 * Return status information about a file descriptor.
633 fstat(p
, uap
, retval
)
635 register struct fstat_args
*uap
;
639 register struct filedesc
*fdp
= p
->p_fd
;
640 register struct file
*fp
;
644 if ((u_int
)fd
>= fdp
->fd_nfiles
||
645 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
646 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
648 switch (fp
->f_type
) {
651 error
= vn_stat((struct vnode
*)fp
->f_data
, &ub
, p
);
655 error
= soo_stat((struct socket
*)fp
->f_data
, &ub
);
659 error
= pshm_stat((void *)fp
->f_data
, &ub
);
666 error
= copyout((caddr_t
)&ub
, (caddr_t
)uap
->sb
,
673 * Return status information about a file descriptor.
680 ofstat(p
, uap
, retval
)
682 register struct ofstat_args
*uap
;
686 register struct filedesc
*fdp
= p
->p_fd
;
687 register struct file
*fp
;
692 if ((u_int
)fd
>= fdp
->fd_nfiles
||
693 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
694 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
696 switch (fp
->f_type
) {
699 error
= vn_stat((struct vnode
*)fp
->f_data
, &ub
, p
);
703 error
= soo_stat((struct socket
*)fp
->f_data
, &ub
);
712 error
= copyout((caddr_t
)&oub
, (caddr_t
)uap
->sb
,
716 #endif /* COMPAT_43 */
719 * Return pathconf information about a file descriptor.
721 struct fpathconf_args
{
726 fpathconf(p
, uap
, retval
)
728 register struct fpathconf_args
*uap
;
732 struct filedesc
*fdp
= p
->p_fd
;
736 if ((u_int
)fd
>= fdp
->fd_nfiles
||
737 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
738 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
740 switch (fp
->f_type
) {
743 if (uap
->name
!= _PC_PIPE_BUF
)
749 vp
= (struct vnode
*)fp
->f_data
;
750 return (VOP_PATHCONF(vp
, uap
->name
, retval
));
759 * Allocate a file descriptor for the process.
764 fdalloc(p
, want
, result
)
769 register struct filedesc
*fdp
= p
->p_fd
;
771 int lim
, last
, nfiles
, oldnfiles
;
772 struct file
**newofiles
, **ofiles
;
773 char *newofileflags
, *ofileflags
;
776 * Search for a free descriptor starting at the higher
777 * of want or fd_freefile. If that fails, consider
778 * expanding the ofile array.
780 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
782 last
= min(fdp
->fd_nfiles
, lim
);
783 if ((i
= want
) < fdp
->fd_freefile
)
784 i
= fdp
->fd_freefile
;
785 ofiles
= &fdp
->fd_ofiles
[i
];
786 ofileflags
= &fdp
->fd_ofileflags
[i
];
787 for (; i
< last
; i
++) {
788 if (*ofiles
== NULL
&& !(*ofileflags
& UF_RESERVED
)) {
789 *ofileflags
= UF_RESERVED
;
790 if (i
> fdp
->fd_lastfile
)
791 fdp
->fd_lastfile
= i
;
792 if (want
<= fdp
->fd_freefile
)
793 fdp
->fd_freefile
= i
;
797 ofiles
++; ofileflags
++;
801 * No space in current array. Expand?
803 if (fdp
->fd_nfiles
>= lim
)
805 if (fdp
->fd_nfiles
< NDEXTENT
)
808 nfiles
= 2 * fdp
->fd_nfiles
;
812 MALLOC_ZONE(newofiles
, struct file
**,
813 nfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
814 if (fdp
->fd_nfiles
>= nfiles
) {
815 FREE_ZONE(newofiles
, nfiles
* OFILESIZE
, M_OFILETABL
);
818 newofileflags
= (char *) &newofiles
[nfiles
];
820 * Copy the existing ofile and ofileflags arrays
821 * and zero the new portion of each array.
823 oldnfiles
= fdp
->fd_nfiles
;
824 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
825 oldnfiles
* sizeof *fdp
->fd_ofiles
);
826 (void) memset(&newofiles
[oldnfiles
], 0,
827 (nfiles
- oldnfiles
) * sizeof *fdp
->fd_ofiles
);
829 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
830 oldnfiles
* sizeof *fdp
->fd_ofileflags
);
831 (void) memset(&newofileflags
[oldnfiles
], 0,
832 (nfiles
- oldnfiles
) *
833 sizeof *fdp
->fd_ofileflags
);
834 ofiles
= fdp
->fd_ofiles
;
835 fdp
->fd_ofiles
= newofiles
;
836 fdp
->fd_ofileflags
= newofileflags
;
837 fdp
->fd_nfiles
= nfiles
;
838 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
844 * Check to see whether n user file descriptors
845 * are available to the process p.
852 register struct filedesc
*fdp
= p
->p_fd
;
853 register struct file
**fpp
;
854 register char *flags
;
857 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
858 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
860 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
861 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
862 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
863 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
873 _fdrelse(p
->p_fd
, fd
);
877 fdgetf(p
, fd
, resultfp
)
878 register struct proc
*p
;
880 struct file
**resultfp
;
882 register struct filedesc
*fdp
= p
->p_fd
;
885 if ((u_int
)fd
>= fdp
->fd_nfiles
||
886 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
887 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
896 * Create a new open file structure and allocate
897 * a file decriptor for the process that refers to it.
900 falloc(p
, resultfp
, resultfd
)
901 register struct proc
*p
;
902 struct file
**resultfp
;
905 register struct file
*fp
, *fq
;
908 if (error
= fdalloc(p
, 0, &i
))
910 if (nfiles
>= maxfiles
) {
915 * Allocate a new file descriptor.
916 * If the process has file descriptor zero open, add to the list
917 * of open files at that point, otherwise put it at the front of
918 * the list of open files.
921 MALLOC_ZONE(fp
, struct file
*, sizeof(struct file
), M_FILE
, M_WAITOK
);
922 bzero(fp
, sizeof(struct file
));
923 if (fq
= p
->p_fd
->fd_ofiles
[0]) {
924 LIST_INSERT_AFTER(fq
, fp
, f_list
);
926 LIST_INSERT_HEAD(&filehead
, fp
, f_list
);
928 p
->p_fd
->fd_ofiles
[i
] = fp
;
930 fp
->f_cred
= p
->p_ucred
;
940 * Free a file structure.
944 register struct file
*fp
;
946 register struct file
*fq
;
949 LIST_REMOVE(fp
, f_list
);
951 if (cred
!= NOCRED
) {
959 FREE_ZONE(fp
, sizeof *fp
, M_FILE
);
966 register struct filedesc
*fdp
= p
->p_fd
;
967 register int i
= fdp
->fd_lastfile
;
968 register struct file
**fpp
= &fdp
->fd_ofiles
[i
];
969 register char *flags
= &fdp
->fd_ofileflags
[i
];
972 if ((*flags
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
) {
973 register struct file
*fp
= *fpp
;
975 *fpp
= NULL
; *flags
= 0;
976 if (i
== fdp
->fd_lastfile
&& i
> 0)
981 *flags
&= ~UF_MAPPED
;
988 * Copy a filedesc structure.
994 register struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
997 MALLOC_ZONE(newfdp
, struct filedesc
*,
998 sizeof *newfdp
, M_FILEDESC
, M_WAITOK
);
999 (void) memcpy(newfdp
, fdp
, sizeof *newfdp
);
1000 VREF(newfdp
->fd_cdir
);
1001 if (newfdp
->fd_rdir
)
1002 VREF(newfdp
->fd_rdir
);
1003 newfdp
->fd_refcnt
= 1;
1006 * If the number of open files fits in the internal arrays
1007 * of the open file structure, use them, otherwise allocate
1008 * additional memory for the number of descriptors currently
1011 if (newfdp
->fd_lastfile
< NDFILE
)
1015 * Compute the smallest multiple of NDEXTENT needed
1016 * for the file descriptors currently in use,
1017 * allowing the table to shrink.
1019 i
= newfdp
->fd_nfiles
;
1020 while (i
> 2 * NDEXTENT
&& i
> newfdp
->fd_lastfile
* 2)
1023 MALLOC_ZONE(newfdp
->fd_ofiles
, struct file
**,
1024 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
1025 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
1026 newfdp
->fd_nfiles
= i
;
1027 if (fdp
->fd_nfiles
> 0) {
1028 register struct file
**fpp
;
1029 register char *flags
;
1031 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
1032 i
* sizeof *fdp
->fd_ofiles
);
1033 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
1034 i
* sizeof *fdp
->fd_ofileflags
);
1036 fpp
= newfdp
->fd_ofiles
;
1037 flags
= newfdp
->fd_ofileflags
;
1038 for (i
= newfdp
->fd_lastfile
; i
-- >= 0; fpp
++, flags
++)
1039 if (*fpp
!= NULL
&& !(*flags
& UF_RESERVED
)) {
1046 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
1052 * Release a filedesc structure.
1058 struct filedesc
*fdp
;
1063 if ((fdp
= p
->p_fd
) == NULL
)
1065 if (--fdp
->fd_refcnt
> 0)
1068 if (fdp
->fd_nfiles
> 0) {
1069 fpp
= fdp
->fd_ofiles
;
1070 for (i
= fdp
->fd_lastfile
; i
-- >= 0; fpp
++)
1072 (void) closef(*fpp
, p
);
1073 FREE_ZONE(fdp
->fd_ofiles
,
1074 fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
1077 fdp
->fd_cdir
= NULL
;
1081 fdp
->fd_rdir
= NULL
;
1084 FREE_ZONE(fdp
, sizeof *fdp
, M_FILEDESC
);
1088 closef_finish(fp
, p
)
1089 register struct file
*fp
;
1090 register struct proc
*p
;
1096 if ((fp
->f_flag
& FHASLOCK
) && fp
->f_type
== DTYPE_VNODE
) {
1097 lf
.l_whence
= SEEK_SET
;
1100 lf
.l_type
= F_UNLCK
;
1101 vp
= (struct vnode
*)fp
->f_data
;
1102 (void) VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_UNLCK
, &lf
, F_FLOCK
);
1105 error
= fo_close(fp
, p
);
1113 * Internal form of close.
1114 * Decrement reference count on file structure.
1115 * Note: p may be NULL when closing a file
1116 * that was being passed in a message.
1120 register struct file
*fp
;
1121 register struct proc
*p
;
1130 * POSIX record locking dictates that any close releases ALL
1131 * locks owned by this process. This is handled by setting
1132 * a flag in the unlock to free ONLY locks obeying POSIX
1133 * semantics, and not to free BSD-style file locks.
1134 * If the descriptor was in a message, POSIX-style locks
1135 * aren't passed with the descriptor.
1137 if (p
&& (p
->p_flag
& P_ADVLOCK
) && fp
->f_type
== DTYPE_VNODE
) {
1138 lf
.l_whence
= SEEK_SET
;
1141 lf
.l_type
= F_UNLCK
;
1142 vp
= (struct vnode
*)fp
->f_data
;
1143 (void) VOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
);
1145 if (frele_internal(fp
) > 0)
1147 return(closef_finish(fp
, p
));
1151 * Apply an advisory lock on a file descriptor.
1153 * Just attempt to get a record lock of the requested type on
1154 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1162 flock(p
, uap
, retval
)
1164 register struct flock_args
*uap
;
1169 register struct filedesc
*fdp
= p
->p_fd
;
1170 register struct file
*fp
;
1174 if ((u_int
)fd
>= fdp
->fd_nfiles
||
1175 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1176 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
1178 if (fp
->f_type
!= DTYPE_VNODE
)
1179 return (EOPNOTSUPP
);
1180 vp
= (struct vnode
*)fp
->f_data
;
1181 lf
.l_whence
= SEEK_SET
;
1184 if (how
& LOCK_UN
) {
1185 lf
.l_type
= F_UNLCK
;
1186 fp
->f_flag
&= ~FHASLOCK
;
1187 return (VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_UNLCK
, &lf
, F_FLOCK
));
1190 lf
.l_type
= F_WRLCK
;
1191 else if (how
& LOCK_SH
)
1192 lf
.l_type
= F_RDLCK
;
1195 fp
->f_flag
|= FHASLOCK
;
1197 return (VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, F_FLOCK
));
1198 return (VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
));
1202 * File Descriptor pseudo-device driver (/dev/fd/).
1204 * Opening minor device N dup()s the file (if any) connected to file
1205 * descriptor N belonging to the calling process. Note that this driver
1206 * consists of only the ``open()'' routine, because all subsequent
1207 * references to this file will be direct to the other driver.
1211 fdopen(dev
, mode
, type
, p
)
1218 * XXX Kludge: set curproc->p_dupfd to contain the value of the
1219 * the file descriptor being sought for duplication. The error
1220 * return ensures that the vnode for this device will be released
1221 * by vn_open. Open will detect this special error and take the
1222 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1223 * will simply report the error.
1225 p
->p_dupfd
= minor(dev
);
1230 * Duplicate the specified descriptor to a free descriptor.
1233 dupfdopen(fdp
, indx
, dfd
, mode
, error
)
1234 register struct filedesc
*fdp
;
1235 register int indx
, dfd
;
1239 register struct file
*wfp
;
1243 * If the to-be-dup'd fd number is greater than the allowed number
1244 * of file descriptors, or the fd to be dup'd has already been
1245 * closed, reject. Note, check for new == old is necessary as
1246 * falloc could allocate an already closed to-be-dup'd descriptor
1247 * as the new descriptor.
1249 fp
= fdp
->fd_ofiles
[indx
];
1250 if ((u_int
)dfd
>= fdp
->fd_nfiles
||
1251 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
1252 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
))
1256 * There are two cases of interest here.
1258 * For ENODEV simply dup (dfd) to file descriptor
1259 * (indx) and return.
1261 * For ENXIO steal away the file structure from (dfd) and
1262 * store it in (indx). (dfd) is effectively closed by
1265 * Any other error code is just returned.
1270 * Check that the mode the file is being opened for is a
1271 * subset of the mode of the existing descriptor.
1273 if (((mode
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
)
1276 if (indx
> fdp
->fd_lastfile
)
1277 fdp
->fd_lastfile
= indx
;;
1278 fdp
->fd_ofiles
[indx
] = wfp
;
1279 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
1284 * Steal away the file pointer from dfd, and stuff it into indx.
1286 if (indx
> fdp
->fd_lastfile
)
1287 fdp
->fd_lastfile
= indx
;;
1288 fdp
->fd_ofiles
[indx
] = fdp
->fd_ofiles
[dfd
];
1289 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
1299 /* Reference manipulation routines for the file structure */
1302 fref(struct file
*fp
)
1304 if (++fp
->f_count
<= 0)
1305 panic("fref: f_count");
1306 return ((int)fp
->f_count
);
1310 frele_internal(struct file
*fp
)
1312 if (--fp
->f_count
< 0)
1313 panic("frele: count < 0");
1314 return ((int)fp
->f_count
);
1319 frele(struct file
*fp
)
1323 extern int disable_funnel
;
1325 fnl
= thread_funnel_get();
1327 * If the funnels are merged then atleast a funnel should be held
1328 * else frele should come in with kernel funnel only
1330 if (!disable_funnel
&& (fnl
!= kernel_flock
)) {
1331 panic("frele: kernel funnel not held");
1333 } else if (fnl
== THR_FUNNEL_NULL
) {
1334 panic("frele: no funnel held");
1337 if ((count
= frele_internal(fp
)) == 0) {
1338 /* some one closed the fd while we were blocked */
1339 (void)closef_finish(fp
, current_proc());
1345 fcount(struct file
*fp
)
1347 return ((int)fp
->f_count
);