Home | History | Annotate | Line # | Download | only in kern
kern_descrip.c revision 1.134.2.11
      1  1.134.2.11      yamt /*	$NetBSD: kern_descrip.c,v 1.134.2.11 2008/03/24 09:39:01 yamt Exp $	*/
      2  1.134.2.11      yamt 
      3  1.134.2.11      yamt /*-
      4  1.134.2.11      yamt  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  1.134.2.11      yamt  * All rights reserved.
      6  1.134.2.11      yamt  *
      7  1.134.2.11      yamt  * Redistribution and use in source and binary forms, with or without
      8  1.134.2.11      yamt  * modification, are permitted provided that the following conditions
      9  1.134.2.11      yamt  * are met:
     10  1.134.2.11      yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.134.2.11      yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.134.2.11      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.134.2.11      yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.134.2.11      yamt  *    documentation and/or other materials provided with the distribution.
     15  1.134.2.11      yamt  * 3. All advertising materials mentioning features or use of this software
     16  1.134.2.11      yamt  *    must display the following acknowledgement:
     17  1.134.2.11      yamt  *	This product includes software developed by the NetBSD
     18  1.134.2.11      yamt  *	Foundation, Inc. and its contributors.
     19  1.134.2.11      yamt  * 4. Neither the name of The NetBSD Foundation nor the names of its
     20  1.134.2.11      yamt  *    contributors may be used to endorse or promote products derived
     21  1.134.2.11      yamt  *    from this software without specific prior written permission.
     22  1.134.2.11      yamt  *
     23  1.134.2.11      yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     24  1.134.2.11      yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     25  1.134.2.11      yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     26  1.134.2.11      yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     27  1.134.2.11      yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     28  1.134.2.11      yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     29  1.134.2.11      yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     30  1.134.2.11      yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     31  1.134.2.11      yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     32  1.134.2.11      yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33  1.134.2.11      yamt  * POSSIBILITY OF SUCH DAMAGE.
     34  1.134.2.11      yamt  */
     35        1.22       cgd 
     36        1.16       cgd /*
     37        1.17       cgd  * Copyright (c) 1982, 1986, 1989, 1991, 1993
     38        1.17       cgd  *	The Regents of the University of California.  All rights reserved.
     39        1.16       cgd  * (c) UNIX System Laboratories, Inc.
     40        1.16       cgd  * All or some portions of this file are derived from material licensed
     41        1.16       cgd  * to the University of California by American Telephone and Telegraph
     42        1.16       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     43        1.16       cgd  * the permission of UNIX System Laboratories, Inc.
     44        1.16       cgd  *
     45        1.16       cgd  * Redistribution and use in source and binary forms, with or without
     46        1.16       cgd  * modification, are permitted provided that the following conditions
     47        1.16       cgd  * are met:
     48        1.16       cgd  * 1. Redistributions of source code must retain the above copyright
     49        1.16       cgd  *    notice, this list of conditions and the following disclaimer.
     50        1.16       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     51        1.16       cgd  *    notice, this list of conditions and the following disclaimer in the
     52        1.16       cgd  *    documentation and/or other materials provided with the distribution.
     53       1.111       agc  * 3. Neither the name of the University nor the names of its contributors
     54        1.16       cgd  *    may be used to endorse or promote products derived from this software
     55        1.16       cgd  *    without specific prior written permission.
     56        1.16       cgd  *
     57        1.16       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     58        1.16       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     59        1.16       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     60        1.16       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     61        1.16       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     62        1.16       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     63        1.16       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     64        1.16       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     65        1.16       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     66        1.16       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     67        1.16       cgd  * SUCH DAMAGE.
     68        1.16       cgd  *
     69        1.51      fvdl  *	@(#)kern_descrip.c	8.8 (Berkeley) 2/14/95
     70        1.16       cgd  */
     71        1.81     lukem 
     72  1.134.2.11      yamt /*
     73  1.134.2.11      yamt  * File descriptor management.
     74  1.134.2.11      yamt  */
     75  1.134.2.11      yamt 
     76        1.81     lukem #include <sys/cdefs.h>
     77  1.134.2.11      yamt __KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.134.2.11 2008/03/24 09:39:01 yamt Exp $");
     78        1.50       mrg 
     79        1.16       cgd #include <sys/param.h>
     80        1.16       cgd #include <sys/systm.h>
     81        1.16       cgd #include <sys/filedesc.h>
     82        1.16       cgd #include <sys/kernel.h>
     83        1.16       cgd #include <sys/vnode.h>
     84        1.16       cgd #include <sys/proc.h>
     85        1.16       cgd #include <sys/file.h>
     86        1.86  christos #include <sys/namei.h>
     87        1.16       cgd #include <sys/socket.h>
     88        1.16       cgd #include <sys/socketvar.h>
     89        1.16       cgd #include <sys/stat.h>
     90        1.16       cgd #include <sys/ioctl.h>
     91        1.16       cgd #include <sys/fcntl.h>
     92        1.16       cgd #include <sys/malloc.h>
     93        1.55   thorpej #include <sys/pool.h>
     94        1.16       cgd #include <sys/syslog.h>
     95        1.17       cgd #include <sys/unistd.h>
     96        1.16       cgd #include <sys/resourcevar.h>
     97        1.42  christos #include <sys/conf.h>
     98        1.96  jdolecek #include <sys/event.h>
     99   1.134.2.1      yamt #include <sys/kauth.h>
    100   1.134.2.7      yamt #include <sys/atomic.h>
    101        1.25       cgd #include <sys/mount.h>
    102        1.25       cgd #include <sys/syscallargs.h>
    103        1.38  christos 
    104   1.134.2.6      yamt static int	cwdi_ctor(void *, void *, int);
    105   1.134.2.6      yamt static void	cwdi_dtor(void *, void *);
    106   1.134.2.8      yamt static int	file_ctor(void *, void *, int);
    107   1.134.2.8      yamt static void	file_dtor(void *, void *);
    108  1.134.2.11      yamt static int	fdfile_ctor(void *, void *, int);
    109  1.134.2.11      yamt static void	fdfile_dtor(void *, void *);
    110  1.134.2.11      yamt static int	filedesc_ctor(void *, void *, int);
    111  1.134.2.11      yamt static void	filedesc_dtor(void *, void *);
    112  1.134.2.11      yamt static int	filedescopen(dev_t, int, int, lwp_t *);
    113   1.134.2.6      yamt 
    114  1.134.2.11      yamt kmutex_t	filelist_lock;	/* lock on filehead */
    115        1.72     lukem struct filelist	filehead;	/* head of list of open files */
    116   1.134.2.8      yamt u_int		nfiles;		/* actual number of open files */
    117   1.134.2.5      yamt 
    118   1.134.2.6      yamt static pool_cache_t cwdi_cache;
    119  1.134.2.11      yamt static pool_cache_t filedesc_cache;
    120   1.134.2.6      yamt static pool_cache_t file_cache;
    121  1.134.2.11      yamt static pool_cache_t fdfile_cache;
    122       1.101   thorpej 
    123       1.101   thorpej MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
    124        1.72     lukem 
    125  1.134.2.11      yamt const struct cdevsw filedesc_cdevsw = {
    126  1.134.2.11      yamt 	filedescopen, noclose, noread, nowrite, noioctl,
    127  1.134.2.11      yamt 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER | D_MPSAFE,
    128  1.134.2.11      yamt };
    129  1.134.2.11      yamt 
    130  1.134.2.11      yamt /* For ease of reading. */
    131  1.134.2.11      yamt __strong_alias(fd_putvnode,fd_putfile)
    132  1.134.2.11      yamt __strong_alias(fd_putsock,fd_putfile)
    133  1.134.2.11      yamt 
    134  1.134.2.11      yamt /*
    135  1.134.2.11      yamt  * Initialize the descriptor system.
    136  1.134.2.11      yamt  */
    137  1.134.2.11      yamt void
    138  1.134.2.11      yamt fd_sys_init(void)
    139  1.134.2.11      yamt {
    140  1.134.2.11      yamt 
    141  1.134.2.11      yamt 	mutex_init(&filelist_lock, MUTEX_DEFAULT, IPL_NONE);
    142  1.134.2.11      yamt 
    143  1.134.2.11      yamt 	file_cache = pool_cache_init(sizeof(file_t), CACHE_LINE_SIZE, 0,
    144  1.134.2.11      yamt 	    0, "file", NULL, IPL_NONE, file_ctor, file_dtor, NULL);
    145  1.134.2.11      yamt 	KASSERT(file_cache != NULL);
    146  1.134.2.11      yamt 
    147  1.134.2.11      yamt 	fdfile_cache = pool_cache_init(sizeof(fdfile_t), CACHE_LINE_SIZE, 0,
    148  1.134.2.11      yamt 	    PR_LARGECACHE, "fdfile", NULL, IPL_NONE, fdfile_ctor, fdfile_dtor,
    149  1.134.2.11      yamt 	    NULL);
    150  1.134.2.11      yamt 	KASSERT(fdfile_cache != NULL);
    151  1.134.2.11      yamt 
    152  1.134.2.11      yamt 	cwdi_cache = pool_cache_init(sizeof(struct cwdinfo), CACHE_LINE_SIZE,
    153  1.134.2.11      yamt 	    0, 0, "cwdi", NULL, IPL_NONE, cwdi_ctor, cwdi_dtor, NULL);
    154  1.134.2.11      yamt 	KASSERT(cwdi_cache != NULL);
    155  1.134.2.11      yamt 
    156  1.134.2.11      yamt 	filedesc_cache = pool_cache_init(sizeof(filedesc_t), CACHE_LINE_SIZE,
    157  1.134.2.11      yamt 	    0, 0, "filedesc", NULL, IPL_NONE, filedesc_ctor, filedesc_dtor,
    158  1.134.2.11      yamt 	    NULL);
    159  1.134.2.11      yamt 	KASSERT(filedesc_cache != NULL);
    160  1.134.2.11      yamt }
    161  1.134.2.11      yamt 
    162  1.134.2.11      yamt static int
    163  1.134.2.11      yamt fd_next_zero(filedesc_t *fdp, uint32_t *bitmap, int want, u_int bits)
    164       1.115    provos {
    165       1.115    provos 	int i, off, maxoff;
    166       1.115    provos 	uint32_t sub;
    167       1.115    provos 
    168  1.134.2.11      yamt 	KASSERT(mutex_owned(&fdp->fd_lock));
    169  1.134.2.11      yamt 
    170       1.115    provos 	if (want > bits)
    171       1.115    provos 		return -1;
    172       1.115    provos 
    173       1.115    provos 	off = want >> NDENTRYSHIFT;
    174       1.115    provos 	i = want & NDENTRYMASK;
    175       1.115    provos 	if (i) {
    176       1.115    provos 		sub = bitmap[off] | ((u_int)~0 >> (NDENTRIES - i));
    177       1.115    provos 		if (sub != ~0)
    178       1.115    provos 			goto found;
    179       1.115    provos 		off++;
    180       1.115    provos 	}
    181       1.115    provos 
    182       1.115    provos 	maxoff = NDLOSLOTS(bits);
    183       1.115    provos 	while (off < maxoff) {
    184       1.115    provos 		if ((sub = bitmap[off]) != ~0)
    185       1.115    provos 			goto found;
    186       1.115    provos 		off++;
    187       1.115    provos 	}
    188       1.115    provos 
    189       1.115    provos 	return (-1);
    190       1.115    provos 
    191       1.115    provos  found:
    192       1.115    provos 	return (off << NDENTRYSHIFT) + ffs(~sub) - 1;
    193       1.115    provos }
    194       1.115    provos 
    195       1.134   thorpej static int
    196  1.134.2.11      yamt fd_last_set(filedesc_t *fd, int last)
    197       1.115    provos {
    198       1.115    provos 	int off, i;
    199  1.134.2.11      yamt 	fdfile_t **ofiles = fd->fd_ofiles;
    200       1.115    provos 	uint32_t *bitmap = fd->fd_lomap;
    201       1.115    provos 
    202  1.134.2.11      yamt 	KASSERT(mutex_owned(&fd->fd_lock));
    203  1.134.2.11      yamt 
    204       1.115    provos 	off = (last - 1) >> NDENTRYSHIFT;
    205       1.115    provos 
    206       1.121    provos 	while (off >= 0 && !bitmap[off])
    207       1.115    provos 		off--;
    208       1.115    provos 
    209       1.115    provos 	if (off < 0)
    210       1.128      cube 		return (-1);
    211       1.131     perry 
    212       1.115    provos 	i = ((off + 1) << NDENTRYSHIFT) - 1;
    213       1.115    provos 	if (i >= last)
    214       1.115    provos 		i = last - 1;
    215       1.115    provos 
    216  1.134.2.11      yamt 	/* XXX should use bitmap */
    217  1.134.2.11      yamt 	/* XXXAD does not work for fd_copy() */
    218  1.134.2.11      yamt 	while (i > 0 && (ofiles[i] == NULL || !ofiles[i]->ff_allocated))
    219       1.115    provos 		i--;
    220       1.115    provos 
    221       1.115    provos 	return (i);
    222       1.115    provos }
    223       1.115    provos 
    224  1.134.2.11      yamt void
    225  1.134.2.11      yamt fd_used(filedesc_t *fdp, unsigned fd)
    226        1.27   mycroft {
    227       1.115    provos 	u_int off = fd >> NDENTRYSHIFT;
    228  1.134.2.11      yamt 	fdfile_t *ff;
    229  1.134.2.11      yamt 
    230  1.134.2.11      yamt 	ff = fdp->fd_ofiles[fd];
    231       1.115    provos 
    232  1.134.2.11      yamt 	KASSERT(mutex_owned(&fdp->fd_lock));
    233  1.134.2.11      yamt 	KASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) == 0);
    234  1.134.2.11      yamt 	KASSERT(ff != NULL);
    235  1.134.2.11      yamt 	KASSERT(ff->ff_file == NULL);
    236  1.134.2.11      yamt    	KASSERT(!ff->ff_allocated);
    237       1.124      yamt 
    238  1.134.2.11      yamt    	ff->ff_allocated = 1;
    239       1.115    provos 	fdp->fd_lomap[off] |= 1 << (fd & NDENTRYMASK);
    240       1.124      yamt 	if (fdp->fd_lomap[off] == ~0) {
    241  1.134.2.11      yamt 		KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
    242       1.124      yamt 		    (1 << (off & NDENTRYMASK))) == 0);
    243       1.115    provos 		fdp->fd_himap[off >> NDENTRYSHIFT] |= 1 << (off & NDENTRYMASK);
    244       1.124      yamt 	}
    245        1.27   mycroft 
    246  1.134.2.11      yamt 	if ((int)fd > fdp->fd_lastfile) {
    247        1.27   mycroft 		fdp->fd_lastfile = fd;
    248  1.134.2.11      yamt 	}
    249  1.134.2.11      yamt 
    250  1.134.2.11      yamt 	if (fd >= NDFDFILE) {
    251  1.134.2.11      yamt 		fdp->fd_nused++;
    252  1.134.2.11      yamt 	} else {
    253  1.134.2.11      yamt 		KASSERT(ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
    254  1.134.2.11      yamt 	}
    255        1.27   mycroft }
    256        1.27   mycroft 
    257  1.134.2.11      yamt void
    258  1.134.2.11      yamt fd_unused(filedesc_t *fdp, unsigned fd)
    259        1.27   mycroft {
    260       1.115    provos 	u_int off = fd >> NDENTRYSHIFT;
    261  1.134.2.11      yamt 	fdfile_t *ff;
    262        1.27   mycroft 
    263  1.134.2.11      yamt 	ff = fdp->fd_ofiles[fd];
    264  1.134.2.11      yamt 
    265  1.134.2.11      yamt 	/*
    266  1.134.2.11      yamt 	 * Don't assert the lock is held here, as we may be copying
    267  1.134.2.11      yamt 	 * the table during exec() and it is not needed there.
    268  1.134.2.11      yamt 	 * procfs and sysctl are locked out by proc::p_reflock.
    269  1.134.2.11      yamt 	 *
    270  1.134.2.11      yamt 	 * KASSERT(mutex_owned(&fdp->fd_lock));
    271  1.134.2.11      yamt 	 */
    272  1.134.2.11      yamt 	KASSERT(ff != NULL);
    273  1.134.2.11      yamt 	KASSERT(ff->ff_file == NULL);
    274  1.134.2.11      yamt    	KASSERT(ff->ff_allocated);
    275  1.134.2.11      yamt 
    276  1.134.2.11      yamt 	if (fd < fdp->fd_freefile) {
    277        1.27   mycroft 		fdp->fd_freefile = fd;
    278  1.134.2.11      yamt 	}
    279       1.115    provos 
    280       1.124      yamt 	if (fdp->fd_lomap[off] == ~0) {
    281  1.134.2.11      yamt 		KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
    282       1.124      yamt 		    (1 << (off & NDENTRYMASK))) != 0);
    283       1.124      yamt 		fdp->fd_himap[off >> NDENTRYSHIFT] &=
    284       1.124      yamt 		    ~(1 << (off & NDENTRYMASK));
    285       1.124      yamt 	}
    286  1.134.2.11      yamt 	KASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) != 0);
    287       1.115    provos 	fdp->fd_lomap[off] &= ~(1 << (fd & NDENTRYMASK));
    288  1.134.2.11      yamt 	ff->ff_allocated = 0;
    289       1.115    provos 
    290  1.134.2.11      yamt 	KASSERT(fd <= fdp->fd_lastfile);
    291  1.134.2.11      yamt 	if (fd == fdp->fd_lastfile) {
    292  1.134.2.11      yamt 		fdp->fd_lastfile = fd_last_set(fdp, fd);
    293  1.134.2.11      yamt 	}
    294  1.134.2.11      yamt 
    295  1.134.2.11      yamt 	if (fd >= NDFDFILE) {
    296  1.134.2.11      yamt 		KASSERT(fdp->fd_nused > 0);
    297  1.134.2.11      yamt 		fdp->fd_nused--;
    298  1.134.2.11      yamt 	} else {
    299  1.134.2.11      yamt 		KASSERT(ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
    300  1.134.2.11      yamt 	}
    301        1.27   mycroft }
    302        1.27   mycroft 
    303       1.103        pk /*
    304  1.134.2.11      yamt  * Custom version of fd_unused() for fd_copy(), where the descriptor
    305  1.134.2.11      yamt  * table is not yet fully initialized.
    306       1.103        pk  */
    307  1.134.2.11      yamt static inline void
    308  1.134.2.11      yamt fd_zap(filedesc_t *fdp, unsigned fd)
    309        1.77   thorpej {
    310  1.134.2.11      yamt 	u_int off = fd >> NDENTRYSHIFT;
    311        1.77   thorpej 
    312  1.134.2.11      yamt 	if (fd < fdp->fd_freefile) {
    313  1.134.2.11      yamt 		fdp->fd_freefile = fd;
    314   1.134.2.5      yamt 	}
    315        1.77   thorpej 
    316  1.134.2.11      yamt 	if (fdp->fd_lomap[off] == ~0) {
    317  1.134.2.11      yamt 		KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
    318  1.134.2.11      yamt 		    (1 << (off & NDENTRYMASK))) != 0);
    319  1.134.2.11      yamt 		fdp->fd_himap[off >> NDENTRYSHIFT] &=
    320  1.134.2.11      yamt 		    ~(1 << (off & NDENTRYMASK));
    321       1.103        pk 	}
    322  1.134.2.11      yamt 	KASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) != 0);
    323  1.134.2.11      yamt 	fdp->fd_lomap[off] &= ~(1 << (fd & NDENTRYMASK));
    324  1.134.2.11      yamt }
    325  1.134.2.11      yamt 
    326  1.134.2.11      yamt bool
    327  1.134.2.11      yamt fd_isused(filedesc_t *fdp, unsigned fd)
    328  1.134.2.11      yamt {
    329  1.134.2.11      yamt 	u_int off = fd >> NDENTRYSHIFT;
    330  1.134.2.11      yamt 
    331  1.134.2.11      yamt 	KASSERT(fd < fdp->fd_nfiles);
    332        1.77   thorpej 
    333  1.134.2.11      yamt 	return (fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) != 0;
    334        1.77   thorpej }
    335        1.77   thorpej 
    336        1.16       cgd /*
    337  1.134.2.11      yamt  * Look up the file structure corresponding to a file descriptor
    338  1.134.2.11      yamt  * and return the file, holding a reference on the descriptor.
    339       1.134   thorpej  */
    340  1.134.2.11      yamt inline file_t *
    341  1.134.2.11      yamt fd_getfile(unsigned fd)
    342       1.134   thorpej {
    343  1.134.2.11      yamt 	filedesc_t *fdp;
    344  1.134.2.11      yamt 	fdfile_t *ff;
    345  1.134.2.11      yamt 	file_t *fp;
    346       1.134   thorpej 
    347  1.134.2.11      yamt 	fdp = curlwp->l_fd;
    348       1.134   thorpej 
    349       1.134   thorpej 	/*
    350  1.134.2.11      yamt 	 * Look up the fdfile structure representing this descriptor.
    351  1.134.2.11      yamt 	 * Ensure that we see fd_nfiles before fd_ofiles since we
    352  1.134.2.11      yamt 	 * are doing this unlocked.  See fd_tryexpand().
    353       1.134   thorpej 	 */
    354  1.134.2.11      yamt 	if (__predict_false(fd >= fdp->fd_nfiles)) {
    355  1.134.2.11      yamt 		return NULL;
    356  1.134.2.11      yamt 	}
    357  1.134.2.11      yamt 	membar_consumer();
    358  1.134.2.11      yamt 	ff = fdp->fd_ofiles[fd];
    359  1.134.2.11      yamt 	KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
    360  1.134.2.11      yamt 	if (__predict_false(ff == NULL)) {
    361  1.134.2.11      yamt 		return NULL;
    362  1.134.2.11      yamt 	}
    363       1.134   thorpej 
    364  1.134.2.11      yamt 	/*
    365  1.134.2.11      yamt 	 * Now get a reference to the descriptor.   Issue a memory
    366  1.134.2.11      yamt 	 * barrier to ensure that we acquire the file pointer _after_
    367  1.134.2.11      yamt 	 * adding a reference.  If no memory barrier, we could fetch
    368  1.134.2.11      yamt 	 * a stale pointer.
    369  1.134.2.11      yamt 	 */
    370  1.134.2.11      yamt 	atomic_inc_uint(&ff->ff_refcnt);
    371  1.134.2.11      yamt #ifndef __HAVE_ATOMIC_AS_MEMBAR
    372  1.134.2.11      yamt 	membar_enter();
    373  1.134.2.11      yamt #endif
    374       1.134   thorpej 
    375  1.134.2.11      yamt 	/*
    376  1.134.2.11      yamt 	 * If the file is not open or is being closed then put the
    377  1.134.2.11      yamt 	 * reference back.
    378  1.134.2.11      yamt 	 */
    379  1.134.2.11      yamt 	fp = ff->ff_file;
    380  1.134.2.11      yamt 	if (__predict_true(fp != NULL)) {
    381  1.134.2.11      yamt 		return fp;
    382       1.134   thorpej 	}
    383  1.134.2.11      yamt 	fd_putfile(fd);
    384  1.134.2.11      yamt 	return NULL;
    385       1.134   thorpej }
    386       1.134   thorpej 
    387       1.134   thorpej /*
    388  1.134.2.11      yamt  * Release a reference to a file descriptor acquired with fd_getfile().
    389   1.134.2.5      yamt  */
    390   1.134.2.5      yamt void
    391  1.134.2.11      yamt fd_putfile(unsigned fd)
    392   1.134.2.5      yamt {
    393  1.134.2.11      yamt 	filedesc_t *fdp;
    394  1.134.2.11      yamt 	fdfile_t *ff;
    395  1.134.2.11      yamt 	u_int u, v;
    396  1.134.2.11      yamt 
    397  1.134.2.11      yamt 	fdp = curlwp->l_fd;
    398  1.134.2.11      yamt 	ff = fdp->fd_ofiles[fd];
    399  1.134.2.11      yamt 
    400  1.134.2.11      yamt 	KASSERT(fd < fdp->fd_nfiles);
    401  1.134.2.11      yamt 	KASSERT(ff != NULL);
    402  1.134.2.11      yamt 	KASSERT((ff->ff_refcnt & FR_MASK) > 0);
    403  1.134.2.11      yamt 	KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
    404   1.134.2.5      yamt 
    405  1.134.2.11      yamt 	/*
    406  1.134.2.11      yamt 	 * Ensure that any use of the file is complete and globally
    407  1.134.2.11      yamt 	 * visible before dropping the final reference.  If no membar,
    408  1.134.2.11      yamt 	 * the current CPU could still access memory associated with
    409  1.134.2.11      yamt 	 * the file after it has been freed or recycled by another
    410  1.134.2.11      yamt 	 * CPU.
    411  1.134.2.11      yamt 	 */
    412  1.134.2.11      yamt #ifndef __HAVE_ATOMIC_AS_MEMBAR
    413  1.134.2.11      yamt 	membar_exit();
    414  1.134.2.11      yamt #endif
    415   1.134.2.6      yamt 
    416  1.134.2.11      yamt 	/*
    417  1.134.2.11      yamt 	 * Be optimistic and start out with the assumption that no other
    418  1.134.2.11      yamt 	 * threads are trying to close the descriptor.  If the CAS fails,
    419  1.134.2.11      yamt 	 * we lost a race and/or it's being closed.
    420  1.134.2.11      yamt 	 */
    421  1.134.2.11      yamt 	for (u = ff->ff_refcnt & FR_MASK;; u = v) {
    422  1.134.2.11      yamt 		v = atomic_cas_uint(&ff->ff_refcnt, u, u - 1);
    423  1.134.2.11      yamt 		if (__predict_true(u == v)) {
    424  1.134.2.11      yamt 			return;
    425  1.134.2.11      yamt 		}
    426  1.134.2.11      yamt 		if (__predict_false((v & FR_CLOSING) != 0)) {
    427  1.134.2.11      yamt 			break;
    428  1.134.2.11      yamt 		}
    429  1.134.2.11      yamt 	}
    430   1.134.2.6      yamt 
    431  1.134.2.11      yamt 	/* Another thread is waiting to close the file: join it. */
    432  1.134.2.11      yamt 	(void)fd_close(fd);
    433   1.134.2.5      yamt }
    434   1.134.2.5      yamt 
    435   1.134.2.5      yamt /*
    436  1.134.2.11      yamt  * Convenience wrapper around fd_getfile() that returns reference
    437  1.134.2.11      yamt  * to a vnode.
    438        1.16       cgd  */
    439        1.38  christos int
    440  1.134.2.11      yamt fd_getvnode(unsigned fd, file_t **fpp)
    441        1.36   thorpej {
    442  1.134.2.11      yamt 	vnode_t *vp;
    443  1.134.2.11      yamt 	file_t *fp;
    444        1.16       cgd 
    445  1.134.2.11      yamt 	fp = fd_getfile(fd);
    446  1.134.2.11      yamt 	if (__predict_false(fp == NULL)) {
    447  1.134.2.11      yamt 		return EBADF;
    448        1.59   thorpej 	}
    449  1.134.2.11      yamt 	if (__predict_false(fp->f_type != DTYPE_VNODE)) {
    450  1.134.2.11      yamt 		fd_putfile(fd);
    451  1.134.2.11      yamt 		return EINVAL;
    452  1.134.2.11      yamt 	}
    453  1.134.2.11      yamt 	vp = fp->f_data;
    454  1.134.2.11      yamt 	if (__predict_false(vp->v_type == VBAD)) {
    455  1.134.2.11      yamt 		/* XXX Is this case really necessary? */
    456  1.134.2.11      yamt 		fd_putfile(fd);
    457  1.134.2.11      yamt 		return EBADF;
    458  1.134.2.11      yamt 	}
    459  1.134.2.11      yamt 	*fpp = fp;
    460  1.134.2.11      yamt 	return 0;
    461        1.16       cgd }
    462        1.16       cgd 
    463        1.16       cgd /*
    464  1.134.2.11      yamt  * Convenience wrapper around fd_getfile() that returns reference
    465  1.134.2.11      yamt  * to a socket.
    466        1.16       cgd  */
    467        1.38  christos int
    468  1.134.2.11      yamt fd_getsock(unsigned fd, struct socket **sop)
    469        1.36   thorpej {
    470  1.134.2.11      yamt 	file_t *fp;
    471        1.16       cgd 
    472  1.134.2.11      yamt 	fp = fd_getfile(fd);
    473  1.134.2.11      yamt 	if (__predict_false(fp == NULL)) {
    474  1.134.2.11      yamt 		return EBADF;
    475        1.17       cgd 	}
    476  1.134.2.11      yamt 	if (__predict_false(fp->f_type != DTYPE_SOCKET)) {
    477  1.134.2.11      yamt 		fd_putfile(fd);
    478  1.134.2.11      yamt 		return ENOTSOCK;
    479        1.16       cgd 	}
    480  1.134.2.11      yamt 	*sop = fp->f_data;
    481  1.134.2.11      yamt 	return 0;
    482        1.16       cgd }
    483        1.16       cgd 
    484        1.16       cgd /*
    485  1.134.2.11      yamt  * Look up the file structure corresponding to a file descriptor
    486  1.134.2.11      yamt  * and return it with a reference held on the file, not the
    487  1.134.2.11      yamt  * descriptor.
    488  1.134.2.11      yamt  *
    489  1.134.2.11      yamt  * This is heavyweight and only used when accessing descriptors
    490  1.134.2.11      yamt  * from a foreign process.  The caller must ensure that `p' does
    491  1.134.2.11      yamt  * not exit or fork across this call.
    492  1.134.2.11      yamt  *
    493  1.134.2.11      yamt  * To release the file (not descriptor) reference, use closef().
    494       1.134   thorpej  */
    495  1.134.2.11      yamt file_t *
    496  1.134.2.11      yamt fd_getfile2(proc_t *p, unsigned fd)
    497       1.134   thorpej {
    498  1.134.2.11      yamt 	filedesc_t *fdp;
    499  1.134.2.11      yamt 	fdfile_t *ff;
    500  1.134.2.11      yamt 	file_t *fp;
    501       1.134   thorpej 
    502  1.134.2.11      yamt 	fdp = p->p_fd;
    503  1.134.2.11      yamt 	mutex_enter(&fdp->fd_lock);
    504  1.134.2.11      yamt 	if (fd > fdp->fd_nfiles) {
    505  1.134.2.11      yamt 		mutex_exit(&fdp->fd_lock);
    506  1.134.2.11      yamt 		return NULL;
    507  1.134.2.11      yamt 	}
    508  1.134.2.11      yamt 	if ((ff = fdp->fd_ofiles[fd]) == NULL) {
    509  1.134.2.11      yamt 		mutex_exit(&fdp->fd_lock);
    510  1.134.2.11      yamt 		return NULL;
    511  1.134.2.11      yamt 	}
    512  1.134.2.11      yamt 	mutex_enter(&ff->ff_lock);
    513  1.134.2.11      yamt 	if ((fp = ff->ff_file) == NULL) {
    514  1.134.2.11      yamt 		mutex_exit(&ff->ff_lock);
    515  1.134.2.11      yamt 		mutex_exit(&fdp->fd_lock);
    516  1.134.2.11      yamt 		return NULL;
    517   1.134.2.4      yamt 	}
    518  1.134.2.11      yamt 	mutex_enter(&fp->f_lock);
    519  1.134.2.11      yamt 	fp->f_count++;
    520  1.134.2.11      yamt 	mutex_exit(&fp->f_lock);
    521  1.134.2.11      yamt 	mutex_exit(&ff->ff_lock);
    522  1.134.2.11      yamt 	mutex_exit(&fdp->fd_lock);
    523   1.134.2.4      yamt 
    524  1.134.2.11      yamt 	return fp;
    525   1.134.2.4      yamt }
    526   1.134.2.4      yamt 
    527       1.134   thorpej /*
    528  1.134.2.11      yamt  * Internal form of close.  Must be called with a reference to the
    529  1.134.2.11      yamt  * descriptor, and will drop the reference.  When all descriptor
    530  1.134.2.11      yamt  * references are dropped, releases the descriptor slot and a single
    531  1.134.2.11      yamt  * reference to the file structure.
    532  1.134.2.11      yamt  */
    533  1.134.2.11      yamt int
    534  1.134.2.11      yamt fd_close(unsigned fd)
    535  1.134.2.11      yamt {
    536  1.134.2.11      yamt 	struct flock lf;
    537  1.134.2.11      yamt 	filedesc_t *fdp;
    538  1.134.2.11      yamt 	fdfile_t *ff;
    539  1.134.2.11      yamt 	file_t *fp;
    540  1.134.2.11      yamt 	proc_t *p;
    541  1.134.2.11      yamt 	lwp_t *l;
    542        1.72     lukem 
    543  1.134.2.11      yamt 	l = curlwp;
    544        1.99   thorpej 	p = l->l_proc;
    545  1.134.2.11      yamt 	fdp = l->l_fd;
    546  1.134.2.11      yamt 	ff = fdp->fd_ofiles[fd];
    547        1.16       cgd 
    548  1.134.2.11      yamt 	KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
    549   1.134.2.4      yamt 
    550  1.134.2.11      yamt 	mutex_enter(&ff->ff_lock);
    551  1.134.2.11      yamt 	KASSERT((ff->ff_refcnt & FR_MASK) > 0);
    552  1.134.2.11      yamt 	if (ff->ff_file == NULL) {
    553  1.134.2.11      yamt 		/*
    554  1.134.2.11      yamt 		 * Another user of the file is already closing, and is
    555  1.134.2.11      yamt 		 * waiting for other users of the file to drain.  Release
    556  1.134.2.11      yamt 		 * our reference, and wake up the closer.
    557  1.134.2.11      yamt 		 */
    558  1.134.2.11      yamt 		atomic_dec_uint(&ff->ff_refcnt);
    559  1.134.2.11      yamt 		cv_broadcast(&ff->ff_closing);
    560  1.134.2.11      yamt 		mutex_exit(&ff->ff_lock);
    561       1.122  christos 
    562  1.134.2.11      yamt 		/*
    563  1.134.2.11      yamt 		 * An application error, so pretend that the descriptor
    564  1.134.2.11      yamt 		 * was already closed.  We can't safely wait for it to
    565  1.134.2.11      yamt 		 * be closed without potentially deadlocking.
    566  1.134.2.11      yamt 		 */
    567        1.16       cgd 		return (EBADF);
    568        1.61  wrstuden 	}
    569  1.134.2.11      yamt 	KASSERT((ff->ff_refcnt & FR_CLOSING) == 0);
    570        1.61  wrstuden 
    571  1.134.2.11      yamt 	/*
    572  1.134.2.11      yamt 	 * There may be multiple users of this file within the process.
    573  1.134.2.11      yamt 	 * Notify existing and new users that the file is closing.  This
    574  1.134.2.11      yamt 	 * will prevent them from adding additional uses to this file
    575  1.134.2.11      yamt 	 * while we are closing it.
    576  1.134.2.11      yamt 	 */
    577  1.134.2.11      yamt 	fp = ff->ff_file;
    578  1.134.2.11      yamt 	ff->ff_file = NULL;
    579  1.134.2.11      yamt 	ff->ff_exclose = 0;
    580        1.17       cgd 
    581  1.134.2.11      yamt 	/*
    582  1.134.2.11      yamt 	 * We expect the caller to hold a descriptor reference - drop it.
    583  1.134.2.11      yamt 	 * The reference count may increase beyond zero at this point due
    584  1.134.2.11      yamt 	 * to an erroneous descriptor reference by an application, but
    585  1.134.2.11      yamt 	 * fd_getfile() will notice that the file is being closed and drop
    586  1.134.2.11      yamt 	 * the reference again.
    587  1.134.2.11      yamt 	 */
    588  1.134.2.11      yamt #ifndef __HAVE_ATOMIC_AS_MEMBAR
    589  1.134.2.11      yamt 	membar_producer();
    590  1.134.2.11      yamt #endif
    591  1.134.2.11      yamt 	if (__predict_false(atomic_dec_uint_nv(&ff->ff_refcnt) != 0)) {
    592  1.134.2.11      yamt 		/*
    593  1.134.2.11      yamt 		 * Wait for other references to drain.  This is typically
    594  1.134.2.11      yamt 		 * an application error - the descriptor is being closed
    595  1.134.2.11      yamt 		 * while still in use.
    596  1.134.2.11      yamt 		 *
    597  1.134.2.11      yamt 		 */
    598  1.134.2.11      yamt 		atomic_or_uint(&ff->ff_refcnt, FR_CLOSING);
    599  1.134.2.11      yamt 		/*
    600  1.134.2.11      yamt 		 * Remove any knotes attached to the file.  A knote
    601  1.134.2.11      yamt 		 * attached to the descriptor can hold references on it.
    602  1.134.2.11      yamt 		 */
    603  1.134.2.11      yamt 		if (!SLIST_EMPTY(&ff->ff_knlist)) {
    604  1.134.2.11      yamt 			mutex_exit(&ff->ff_lock);
    605  1.134.2.11      yamt 			knote_fdclose(fd);
    606  1.134.2.11      yamt 			mutex_enter(&ff->ff_lock);
    607        1.75   thorpej 		}
    608  1.134.2.11      yamt 		/*
    609  1.134.2.11      yamt 		 * We need to see the count drop to zero at least once,
    610  1.134.2.11      yamt 		 * in order to ensure that all pre-existing references
    611  1.134.2.11      yamt 		 * have been drained.  New references past this point are
    612  1.134.2.11      yamt 		 * of no interest.
    613  1.134.2.11      yamt 		 */
    614  1.134.2.11      yamt 		while ((ff->ff_refcnt & FR_MASK) != 0) {
    615  1.134.2.11      yamt 			cv_wait(&ff->ff_closing, &ff->ff_lock);
    616       1.107       dsl 		}
    617  1.134.2.11      yamt 		atomic_and_uint(&ff->ff_refcnt, ~FR_CLOSING);
    618  1.134.2.11      yamt 	} else {
    619  1.134.2.11      yamt 		/* If no references, there must be no knotes. */
    620  1.134.2.11      yamt 		KASSERT(SLIST_EMPTY(&ff->ff_knlist));
    621        1.16       cgd 	}
    622  1.134.2.11      yamt 	mutex_exit(&ff->ff_lock);
    623        1.59   thorpej 
    624  1.134.2.11      yamt 	/*
    625  1.134.2.11      yamt 	 * POSIX record locking dictates that any close releases ALL
    626  1.134.2.11      yamt 	 * locks owned by this process.  This is handled by setting
    627  1.134.2.11      yamt 	 * a flag in the unlock to free ONLY locks obeying POSIX
    628  1.134.2.11      yamt 	 * semantics, and not to free BSD-style file locks.
    629  1.134.2.11      yamt 	 * If the descriptor was in a message, POSIX-style locks
    630  1.134.2.11      yamt 	 * aren't passed with the descriptor.
    631  1.134.2.11      yamt 	 */
    632  1.134.2.11      yamt 	if ((p->p_flag & PK_ADVLOCK) != 0 && fp->f_type == DTYPE_VNODE) {
    633  1.134.2.11      yamt 		lf.l_whence = SEEK_SET;
    634  1.134.2.11      yamt 		lf.l_start = 0;
    635  1.134.2.11      yamt 		lf.l_len = 0;
    636  1.134.2.11      yamt 		lf.l_type = F_UNLCK;
    637  1.134.2.11      yamt 		(void)VOP_ADVLOCK(fp->f_data, p, F_UNLCK, &lf, F_POSIX);
    638       1.103        pk 	}
    639       1.103        pk 
    640        1.59   thorpej 
    641  1.134.2.11      yamt 	/* Free descriptor slot. */
    642  1.134.2.11      yamt 	mutex_enter(&fdp->fd_lock);
    643       1.126        pk 	fd_unused(fdp, fd);
    644  1.134.2.11      yamt 	mutex_exit(&fdp->fd_lock);
    645       1.126        pk 
    646  1.134.2.11      yamt 	/* Now drop reference to the file itself. */
    647  1.134.2.11      yamt 	return closef(fp);
    648        1.27   mycroft }
    649        1.27   mycroft 
    650        1.17       cgd /*
    651  1.134.2.11      yamt  * Duplicate a file descriptor.
    652   1.134.2.4      yamt  */
    653   1.134.2.4      yamt int
    654  1.134.2.11      yamt fd_dup(file_t *fp, int minfd, int *newp, int exclose)
    655   1.134.2.4      yamt {
    656  1.134.2.11      yamt 	proc_t *p;
    657  1.134.2.11      yamt 	int error;
    658   1.134.2.4      yamt 
    659  1.134.2.11      yamt 	p = curproc;
    660   1.134.2.4      yamt 
    661  1.134.2.11      yamt 	while ((error = fd_alloc(p, minfd, newp)) != 0) {
    662  1.134.2.11      yamt 		if (error != ENOSPC) {
    663  1.134.2.11      yamt 			return error;
    664  1.134.2.11      yamt 		}
    665  1.134.2.11      yamt 		fd_tryexpand(p);
    666  1.134.2.11      yamt 	}
    667   1.134.2.4      yamt 
    668  1.134.2.11      yamt 	curlwp->l_fd->fd_ofiles[*newp]->ff_exclose = exclose;
    669  1.134.2.11      yamt 	fd_affix(p, fp, *newp);
    670  1.134.2.11      yamt 	return 0;
    671   1.134.2.4      yamt }
    672   1.134.2.4      yamt 
    673   1.134.2.4      yamt /*
    674  1.134.2.11      yamt  * dup2 operation.
    675        1.17       cgd  */
    676        1.38  christos int
    677  1.134.2.11      yamt fd_dup2(file_t *fp, unsigned new)
    678        1.36   thorpej {
    679  1.134.2.11      yamt 	filedesc_t *fdp;
    680  1.134.2.11      yamt 	fdfile_t *ff;
    681        1.16       cgd 
    682  1.134.2.11      yamt 	fdp = curlwp->l_fd;
    683        1.59   thorpej 
    684  1.134.2.11      yamt 	/*
    685  1.134.2.11      yamt 	 * Ensure there are enough slots in the descriptor table,
    686  1.134.2.11      yamt 	 * and allocate an fdfile_t up front in case we need it.
    687  1.134.2.11      yamt 	 */
    688  1.134.2.11      yamt 	while (new >= fdp->fd_nfiles) {
    689  1.134.2.11      yamt 		fd_tryexpand(curproc);
    690  1.134.2.11      yamt 	}
    691  1.134.2.11      yamt 	ff = pool_cache_get(fdfile_cache, PR_WAITOK);
    692        1.73  jdolecek 
    693  1.134.2.11      yamt 	/*
    694  1.134.2.11      yamt 	 * If there is already a file open, close it.  If the file is
    695  1.134.2.11      yamt 	 * half open, wait for it to be constructed before closing it.
    696  1.134.2.11      yamt 	 * XXX Potential for deadlock here?
    697  1.134.2.11      yamt 	 */
    698  1.134.2.11      yamt 	mutex_enter(&fdp->fd_lock);
    699  1.134.2.11      yamt 	while (fd_isused(fdp, new)) {
    700  1.134.2.11      yamt 		mutex_exit(&fdp->fd_lock);
    701  1.134.2.11      yamt 		if (fd_getfile(new) != NULL) {
    702  1.134.2.11      yamt 			(void)fd_close(new);
    703  1.134.2.11      yamt 		} else {
    704  1.134.2.11      yamt 			/* XXX Crummy, but unlikely to happen. */
    705  1.134.2.11      yamt 			kpause("dup2", false, 1, NULL);
    706  1.134.2.11      yamt 		}
    707  1.134.2.11      yamt 		mutex_enter(&fdp->fd_lock);
    708  1.134.2.11      yamt 	}
    709  1.134.2.11      yamt 	if (fdp->fd_ofiles[new] == NULL) {
    710  1.134.2.11      yamt 		KASSERT(new >= NDFDFILE);
    711  1.134.2.11      yamt 		fdp->fd_ofiles[new] = ff;
    712  1.134.2.11      yamt 		ff = NULL;
    713  1.134.2.11      yamt 	}
    714  1.134.2.11      yamt 	fd_used(fdp, new);
    715  1.134.2.11      yamt 	mutex_exit(&fdp->fd_lock);
    716  1.134.2.11      yamt 
    717  1.134.2.11      yamt 	/* Slot is now allocated.  Insert copy of the file. */
    718  1.134.2.11      yamt 	fd_affix(curproc, fp, new);
    719  1.134.2.11      yamt 	if (ff != NULL) {
    720  1.134.2.11      yamt 		pool_cache_put(fdfile_cache, ff);
    721  1.134.2.11      yamt 	}
    722  1.134.2.11      yamt 	return 0;
    723        1.16       cgd }
    724        1.16       cgd 
    725        1.16       cgd /*
    726  1.134.2.11      yamt  * Drop reference to a file structure.
    727        1.16       cgd  */
    728        1.38  christos int
    729  1.134.2.11      yamt closef(file_t *fp)
    730        1.36   thorpej {
    731  1.134.2.11      yamt 	struct flock lf;
    732  1.134.2.11      yamt 	int error;
    733        1.17       cgd 
    734  1.134.2.11      yamt 	/*
    735  1.134.2.11      yamt 	 * Drop reference.  If referenced elsewhere it's still open
    736  1.134.2.11      yamt 	 * and we have nothing more to do.
    737  1.134.2.11      yamt 	 */
    738  1.134.2.11      yamt 	mutex_enter(&fp->f_lock);
    739  1.134.2.11      yamt 	KASSERT(fp->f_count > 0);
    740  1.134.2.11      yamt 	if (--fp->f_count > 0) {
    741  1.134.2.11      yamt 		mutex_exit(&fp->f_lock);
    742  1.134.2.11      yamt 		return 0;
    743  1.134.2.11      yamt 	}
    744  1.134.2.11      yamt 	KASSERT(fp->f_count == 0);
    745  1.134.2.11      yamt 	mutex_exit(&fp->f_lock);
    746        1.96  jdolecek 
    747  1.134.2.11      yamt 	/* We held the last reference - release locks, close and free. */
    748  1.134.2.11      yamt         if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
    749  1.134.2.11      yamt         	lf.l_whence = SEEK_SET;
    750  1.134.2.11      yamt 		lf.l_start = 0;
    751  1.134.2.11      yamt 		lf.l_len = 0;
    752  1.134.2.11      yamt 		lf.l_type = F_UNLCK;
    753  1.134.2.11      yamt 		(void)VOP_ADVLOCK(fp->f_data, fp, F_UNLCK, &lf, F_FLOCK);
    754  1.134.2.11      yamt 	}
    755  1.134.2.11      yamt 	if (fp->f_ops != NULL) {
    756  1.134.2.11      yamt 		error = (*fp->f_ops->fo_close)(fp);
    757  1.134.2.11      yamt 	} else {
    758  1.134.2.11      yamt 		error = 0;
    759        1.17       cgd 	}
    760  1.134.2.11      yamt 	ffree(fp);
    761        1.59   thorpej 
    762  1.134.2.11      yamt 	return error;
    763        1.16       cgd }
    764        1.16       cgd 
    765        1.16       cgd /*
    766        1.16       cgd  * Allocate a file descriptor for the process.
    767        1.16       cgd  */
    768        1.38  christos int
    769  1.134.2.11      yamt fd_alloc(proc_t *p, int want, int *result)
    770        1.72     lukem {
    771  1.134.2.11      yamt 	filedesc_t *fdp;
    772       1.126        pk 	int i, lim, last, error;
    773       1.115    provos 	u_int off, new;
    774  1.134.2.11      yamt 	fdfile_t *ff;
    775  1.134.2.11      yamt 
    776  1.134.2.11      yamt 	KASSERT(p == curproc || p == &proc0);
    777        1.72     lukem 
    778        1.72     lukem 	fdp = p->p_fd;
    779  1.134.2.11      yamt 	ff = pool_cache_get(fdfile_cache, PR_WAITOK);
    780  1.134.2.11      yamt 	KASSERT(ff->ff_refcnt == 0);
    781  1.134.2.11      yamt 	KASSERT(ff->ff_file == NULL);
    782        1.16       cgd 
    783        1.16       cgd 	/*
    784        1.16       cgd 	 * Search for a free descriptor starting at the higher
    785  1.134.2.11      yamt 	 * of want or fd_freefile.
    786        1.16       cgd 	 */
    787  1.134.2.11      yamt 	mutex_enter(&fdp->fd_lock);
    788  1.134.2.11      yamt 	KASSERT(fdp->fd_ofiles[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
    789        1.17       cgd 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    790        1.90     enami 	last = min(fdp->fd_nfiles, lim);
    791  1.134.2.11      yamt 	for (;;) {
    792  1.134.2.11      yamt 		if ((i = want) < fdp->fd_freefile)
    793  1.134.2.11      yamt 			i = fdp->fd_freefile;
    794  1.134.2.11      yamt 		off = i >> NDENTRYSHIFT;
    795  1.134.2.11      yamt 		new = fd_next_zero(fdp, fdp->fd_himap, off,
    796  1.134.2.11      yamt 		    (last + NDENTRIES - 1) >> NDENTRYSHIFT);
    797  1.134.2.11      yamt 		if (new == -1)
    798  1.134.2.11      yamt 			break;
    799  1.134.2.11      yamt 		i = fd_next_zero(fdp, &fdp->fd_lomap[new],
    800       1.115    provos 		    new > off ? 0 : i & NDENTRYMASK, NDENTRIES);
    801       1.115    provos 		if (i == -1) {
    802       1.131     perry 			/*
    803  1.134.2.11      yamt 			 * Free file descriptor in this block was
    804       1.115    provos 			 * below want, try again with higher want.
    805       1.115    provos 			 */
    806       1.115    provos 			want = (new + 1) << NDENTRYSHIFT;
    807  1.134.2.11      yamt 			continue;
    808       1.115    provos 		}
    809       1.115    provos 		i += (new << NDENTRYSHIFT);
    810  1.134.2.11      yamt 		if (i >= last) {
    811  1.134.2.11      yamt 			break;
    812        1.16       cgd 		}
    813  1.134.2.11      yamt 		if (fdp->fd_ofiles[i] == NULL) {
    814  1.134.2.11      yamt 			KASSERT(i >= NDFDFILE);
    815  1.134.2.11      yamt 			fdp->fd_ofiles[i] = ff;
    816  1.134.2.11      yamt 		} else {
    817  1.134.2.11      yamt 		   	pool_cache_put(fdfile_cache, ff);
    818  1.134.2.11      yamt 		}
    819  1.134.2.11      yamt 		KASSERT(fdp->fd_ofiles[i]->ff_file == NULL);
    820  1.134.2.11      yamt 		fd_used(fdp, i);
    821  1.134.2.11      yamt 		if (want <= fdp->fd_freefile) {
    822  1.134.2.11      yamt 			fdp->fd_freefile = i;
    823  1.134.2.11      yamt 		}
    824  1.134.2.11      yamt 		*result = i;
    825  1.134.2.11      yamt 		mutex_exit(&fdp->fd_lock);
    826  1.134.2.11      yamt 		KASSERT(i >= NDFDFILE ||
    827  1.134.2.11      yamt 		    fdp->fd_ofiles[i] == (fdfile_t *)fdp->fd_dfdfile[i]);
    828  1.134.2.11      yamt 		return 0;
    829        1.90     enami 	}
    830        1.16       cgd 
    831  1.134.2.11      yamt 	/* No space in current array.  Let the caller expand and retry. */
    832       1.126        pk 	error = (fdp->fd_nfiles >= lim) ? EMFILE : ENOSPC;
    833  1.134.2.11      yamt 	mutex_exit(&fdp->fd_lock);
    834  1.134.2.11      yamt 	pool_cache_put(fdfile_cache, ff);
    835  1.134.2.11      yamt 	return error;
    836        1.16       cgd }
    837        1.16       cgd 
    838  1.134.2.11      yamt /*
    839  1.134.2.11      yamt  * Expand a process' descriptor table.
    840  1.134.2.11      yamt  */
    841        1.76   thorpej void
    842  1.134.2.11      yamt fd_tryexpand(proc_t *p)
    843        1.76   thorpej {
    844  1.134.2.11      yamt 	filedesc_t *fdp;
    845  1.134.2.11      yamt 	int i, numfiles, oldnfiles;
    846  1.134.2.11      yamt 	fdfile_t **newofile;
    847  1.134.2.11      yamt 	uint32_t *newhimap, *newlomap;
    848        1.76   thorpej 
    849  1.134.2.11      yamt 	KASSERT(p == curproc || p == &proc0);
    850        1.76   thorpej 
    851  1.134.2.11      yamt 	fdp = p->p_fd;
    852  1.134.2.11      yamt 	newhimap = NULL;
    853  1.134.2.11      yamt 	newlomap = NULL;
    854       1.126        pk 	oldnfiles = fdp->fd_nfiles;
    855       1.126        pk 
    856       1.126        pk 	if (oldnfiles < NDEXTENT)
    857       1.133  christos 		numfiles = NDEXTENT;
    858        1.76   thorpej 	else
    859       1.133  christos 		numfiles = 2 * oldnfiles;
    860       1.126        pk 
    861  1.134.2.11      yamt 	newofile = malloc(numfiles * sizeof(fdfile_t *), M_FILEDESC, M_WAITOK);
    862       1.133  christos 	if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
    863  1.134.2.11      yamt 		newhimap = malloc(NDHISLOTS(numfiles) *
    864  1.134.2.11      yamt 		    sizeof(uint32_t), M_FILEDESC, M_WAITOK);
    865  1.134.2.11      yamt 		newlomap = malloc(NDLOSLOTS(numfiles) *
    866  1.134.2.11      yamt 		    sizeof(uint32_t), M_FILEDESC, M_WAITOK);
    867       1.126        pk 	}
    868       1.126        pk 
    869  1.134.2.11      yamt 	mutex_enter(&fdp->fd_lock);
    870  1.134.2.11      yamt 	KASSERT(fdp->fd_ofiles[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
    871       1.126        pk 	if (fdp->fd_nfiles != oldnfiles) {
    872  1.134.2.11      yamt 		/* fdp changed; caller must retry */
    873  1.134.2.11      yamt 		mutex_exit(&fdp->fd_lock);
    874       1.126        pk 		free(newofile, M_FILEDESC);
    875  1.134.2.11      yamt 		if (newhimap != NULL)
    876  1.134.2.11      yamt 			free(newhimap, M_FILEDESC);
    877  1.134.2.11      yamt 		if (newlomap != NULL)
    878  1.134.2.11      yamt 			free(newlomap, M_FILEDESC);
    879  1.134.2.11      yamt 		return;
    880  1.134.2.11      yamt 	}
    881  1.134.2.11      yamt 
    882  1.134.2.11      yamt 	/* Copy the existing ofile array and zero the new portion. */
    883  1.134.2.11      yamt 	i = sizeof(fdfile_t *) * fdp->fd_nfiles;
    884  1.134.2.11      yamt 	memcpy(newofile, fdp->fd_ofiles, i);
    885  1.134.2.11      yamt 	memset((uint8_t *)newofile + i, 0, numfiles * sizeof(fdfile_t *) - i);
    886  1.134.2.11      yamt 
    887  1.134.2.11      yamt 	/*
    888  1.134.2.11      yamt 	 * Link old ofiles array into list to be discarded.  We defer
    889  1.134.2.11      yamt 	 * freeing until process exit if the descriptor table is visble
    890  1.134.2.11      yamt 	 * to other threads.
    891  1.134.2.11      yamt 	 */
    892  1.134.2.11      yamt 	if (oldnfiles > NDFILE) {
    893  1.134.2.11      yamt 		if ((fdp->fd_refcnt | p->p_nlwps) > 1) {
    894  1.134.2.11      yamt 			*(void **)fdp->fd_ofiles = fdp->fd_discard;
    895  1.134.2.11      yamt 			fdp->fd_discard = fdp->fd_ofiles;
    896  1.134.2.11      yamt 		} else {
    897  1.134.2.11      yamt 			free(fdp->fd_ofiles, M_FILEDESC);
    898  1.134.2.11      yamt 		}
    899  1.134.2.11      yamt 	}
    900       1.115    provos 
    901       1.133  christos 	if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
    902  1.134.2.11      yamt 		i = NDHISLOTS(oldnfiles) * sizeof(uint32_t);
    903  1.134.2.11      yamt 		memcpy(newhimap, fdp->fd_himap, i);
    904  1.134.2.11      yamt 		memset((uint8_t *)newhimap + i, 0,
    905       1.133  christos 		    NDHISLOTS(numfiles) * sizeof(uint32_t) - i);
    906       1.115    provos 
    907  1.134.2.11      yamt 		i = NDLOSLOTS(oldnfiles) * sizeof(uint32_t);
    908  1.134.2.11      yamt 		memcpy(newlomap, fdp->fd_lomap, i);
    909  1.134.2.11      yamt 		memset((uint8_t *)newlomap + i, 0,
    910       1.133  christos 		    NDLOSLOTS(numfiles) * sizeof(uint32_t) - i);
    911       1.115    provos 
    912       1.126        pk 		if (NDHISLOTS(oldnfiles) > NDHISLOTS(NDFILE)) {
    913       1.115    provos 			free(fdp->fd_himap, M_FILEDESC);
    914       1.115    provos 			free(fdp->fd_lomap, M_FILEDESC);
    915       1.115    provos 		}
    916       1.115    provos 		fdp->fd_himap = newhimap;
    917       1.115    provos 		fdp->fd_lomap = newlomap;
    918       1.115    provos 	}
    919       1.115    provos 
    920  1.134.2.11      yamt 	/*
    921  1.134.2.11      yamt 	 * All other modifications must become globally visible before
    922  1.134.2.11      yamt 	 * the change to fd_nfiles.  See fd_getfile().
    923  1.134.2.11      yamt 	 */
    924        1.76   thorpej 	fdp->fd_ofiles = newofile;
    925  1.134.2.11      yamt 	membar_producer();
    926       1.133  christos 	fdp->fd_nfiles = numfiles;
    927  1.134.2.11      yamt 	mutex_exit(&fdp->fd_lock);
    928       1.126        pk 
    929  1.134.2.11      yamt 	KASSERT(fdp->fd_ofiles[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
    930        1.76   thorpej }
    931        1.76   thorpej 
    932        1.16       cgd /*
    933  1.134.2.11      yamt  * Create a new open file structure and allocate a file descriptor
    934  1.134.2.11      yamt  * for the current process.
    935        1.16       cgd  */
    936        1.38  christos int
    937  1.134.2.11      yamt fd_allocfile(file_t **resultfp, int *resultfd)
    938        1.16       cgd {
    939  1.134.2.11      yamt 	file_t *fp;
    940  1.134.2.11      yamt 	proc_t *p;
    941  1.134.2.11      yamt 	int error;
    942        1.16       cgd 
    943  1.134.2.11      yamt 	p = curproc;
    944   1.134.2.2      yamt 
    945  1.134.2.11      yamt 	while ((error = fd_alloc(p, 0, resultfd)) != 0) {
    946  1.134.2.11      yamt 		if (error != ENOSPC) {
    947  1.134.2.11      yamt 			return error;
    948        1.76   thorpej 		}
    949  1.134.2.11      yamt 		fd_tryexpand(p);
    950        1.75   thorpej 	}
    951       1.102        pk 
    952   1.134.2.6      yamt 	fp = pool_cache_get(file_cache, PR_WAITOK);
    953  1.134.2.11      yamt 	KASSERT(fp->f_count == 0);
    954  1.134.2.11      yamt 	fp->f_cred = kauth_cred_get();
    955  1.134.2.11      yamt 	kauth_cred_hold(fp->f_cred);
    956   1.134.2.8      yamt 
    957  1.134.2.11      yamt 	if (__predict_false(atomic_inc_uint_nv(&nfiles) >= maxfiles)) {
    958  1.134.2.11      yamt 		fd_abort(p, fp, *resultfd);
    959        1.69  jdolecek 		tablefull("file", "increase kern.maxfiles or MAXFILES");
    960  1.134.2.11      yamt 		return ENFILE;
    961        1.16       cgd 	}
    962   1.134.2.8      yamt 
    963   1.134.2.8      yamt 	fp->f_advice = 0;
    964   1.134.2.8      yamt 	fp->f_msgcount = 0;
    965   1.134.2.8      yamt 	fp->f_offset = 0;
    966  1.134.2.11      yamt 	fp->f_iflags = 0;
    967  1.134.2.11      yamt 	*resultfp = fp;
    968  1.134.2.11      yamt 
    969  1.134.2.11      yamt 	return 0;
    970  1.134.2.11      yamt }
    971  1.134.2.11      yamt 
    972  1.134.2.11      yamt /*
    973  1.134.2.11      yamt  * Successful creation of a new descriptor: make visible to the process.
    974  1.134.2.11      yamt  */
    975  1.134.2.11      yamt void
    976  1.134.2.11      yamt fd_affix(proc_t *p, file_t *fp, unsigned fd)
    977  1.134.2.11      yamt {
    978  1.134.2.11      yamt 	fdfile_t *ff;
    979  1.134.2.11      yamt 	filedesc_t *fdp;
    980  1.134.2.11      yamt 
    981  1.134.2.11      yamt 	KASSERT(p == curproc || p == &proc0);
    982  1.134.2.11      yamt 
    983  1.134.2.11      yamt 	/* Add a reference to the file structure. */
    984  1.134.2.11      yamt 	mutex_enter(&fp->f_lock);
    985  1.134.2.11      yamt 	fp->f_count++;
    986  1.134.2.11      yamt 	mutex_exit(&fp->f_lock);
    987   1.134.2.8      yamt 
    988        1.16       cgd 	/*
    989  1.134.2.11      yamt 	 * Insert the new file into the descriptor slot.
    990  1.134.2.11      yamt 	 *
    991  1.134.2.11      yamt 	 * The memory barriers provided by lock activity in this routine
    992  1.134.2.11      yamt 	 * ensure that any updates to the file structure become globally
    993  1.134.2.11      yamt 	 * visible before the file becomes visible to other LWPs in the
    994  1.134.2.11      yamt 	 * current process.
    995        1.16       cgd 	 */
    996  1.134.2.11      yamt 	fdp = p->p_fd;
    997  1.134.2.11      yamt 	ff = fdp->fd_ofiles[fd];
    998   1.134.2.8      yamt 
    999  1.134.2.11      yamt 	KASSERT(ff != NULL);
   1000  1.134.2.11      yamt 	KASSERT(ff->ff_file == NULL);
   1001  1.134.2.11      yamt 	KASSERT(ff->ff_allocated);
   1002  1.134.2.11      yamt 	KASSERT(fd_isused(fdp, fd));
   1003  1.134.2.11      yamt 	KASSERT(fd >= NDFDFILE ||
   1004  1.134.2.11      yamt 	    fdp->fd_ofiles[fd] == (fdfile_t *)fdp->fd_dfdfile[fd]);
   1005   1.134.2.5      yamt 
   1006  1.134.2.11      yamt 	/* No need to lock in order to make file initially visible. */
   1007  1.134.2.11      yamt 	ff->ff_file = fp;
   1008  1.134.2.11      yamt }
   1009  1.134.2.11      yamt 
   1010  1.134.2.11      yamt /*
   1011  1.134.2.11      yamt  * Abort creation of a new descriptor: free descriptor slot and file.
   1012  1.134.2.11      yamt  */
   1013  1.134.2.11      yamt void
   1014  1.134.2.11      yamt fd_abort(proc_t *p, file_t *fp, unsigned fd)
   1015  1.134.2.11      yamt {
   1016  1.134.2.11      yamt 	filedesc_t *fdp;
   1017  1.134.2.11      yamt 	fdfile_t *ff;
   1018  1.134.2.11      yamt 
   1019  1.134.2.11      yamt 	KASSERT(p == curproc || p == &proc0);
   1020  1.134.2.11      yamt 
   1021  1.134.2.11      yamt 	fdp = p->p_fd;
   1022  1.134.2.11      yamt 	ff = fdp->fd_ofiles[fd];
   1023  1.134.2.11      yamt 
   1024  1.134.2.11      yamt 	KASSERT(fd >= NDFDFILE ||
   1025  1.134.2.11      yamt 	    fdp->fd_ofiles[fd] == (fdfile_t *)fdp->fd_dfdfile[fd]);
   1026  1.134.2.11      yamt 
   1027  1.134.2.11      yamt 	mutex_enter(&fdp->fd_lock);
   1028  1.134.2.11      yamt 	KASSERT(fd_isused(fdp, fd));
   1029  1.134.2.11      yamt 	fd_unused(fdp, fd);
   1030  1.134.2.11      yamt 	mutex_exit(&fdp->fd_lock);
   1031  1.134.2.11      yamt 
   1032  1.134.2.11      yamt 	if (fp != NULL) {
   1033  1.134.2.11      yamt 		ffree(fp);
   1034  1.134.2.11      yamt 	}
   1035        1.16       cgd }
   1036        1.16       cgd 
   1037        1.16       cgd /*
   1038        1.16       cgd  * Free a file descriptor.
   1039        1.16       cgd  */
   1040        1.38  christos void
   1041  1.134.2.11      yamt ffree(file_t *fp)
   1042        1.16       cgd {
   1043        1.59   thorpej 
   1044  1.134.2.11      yamt 	KASSERT(fp->f_count == 0);
   1045        1.59   thorpej 
   1046   1.134.2.8      yamt 	atomic_dec_uint(&nfiles);
   1047   1.134.2.8      yamt 	kauth_cred_free(fp->f_cred);
   1048   1.134.2.6      yamt 	pool_cache_put(file_cache, fp);
   1049        1.48   thorpej }
   1050        1.48   thorpej 
   1051        1.48   thorpej /*
   1052        1.58   thorpej  * Create an initial cwdinfo structure, using the same current and root
   1053  1.134.2.11      yamt  * directories as curproc.
   1054        1.58   thorpej  */
   1055        1.58   thorpej struct cwdinfo *
   1056  1.134.2.11      yamt cwdinit(void)
   1057        1.58   thorpej {
   1058        1.58   thorpej 	struct cwdinfo *cwdi;
   1059   1.134.2.5      yamt 	struct cwdinfo *copy;
   1060        1.58   thorpej 
   1061   1.134.2.6      yamt 	cwdi = pool_cache_get(cwdi_cache, PR_WAITOK);
   1062  1.134.2.11      yamt 	copy = curproc->p_cwdi;
   1063   1.134.2.5      yamt 
   1064   1.134.2.5      yamt 	rw_enter(&copy->cwdi_lock, RW_READER);
   1065  1.134.2.11      yamt 	cwdi->cwdi_cdir = copy->cwdi_cdir;
   1066        1.63   thorpej 	if (cwdi->cwdi_cdir)
   1067        1.63   thorpej 		VREF(cwdi->cwdi_cdir);
   1068  1.134.2.11      yamt 	cwdi->cwdi_rdir = copy->cwdi_rdir;
   1069        1.58   thorpej 	if (cwdi->cwdi_rdir)
   1070        1.58   thorpej 		VREF(cwdi->cwdi_rdir);
   1071  1.134.2.11      yamt 	cwdi->cwdi_edir = copy->cwdi_edir;
   1072   1.134.2.4      yamt 	if (cwdi->cwdi_edir)
   1073   1.134.2.4      yamt 		VREF(cwdi->cwdi_edir);
   1074  1.134.2.11      yamt 	cwdi->cwdi_cmask =  copy->cwdi_cmask;
   1075        1.58   thorpej 	cwdi->cwdi_refcnt = 1;
   1076   1.134.2.5      yamt 	rw_exit(&copy->cwdi_lock);
   1077        1.58   thorpej 
   1078        1.58   thorpej 	return (cwdi);
   1079        1.58   thorpej }
   1080        1.58   thorpej 
   1081   1.134.2.6      yamt static int
   1082   1.134.2.6      yamt cwdi_ctor(void *arg, void *obj, int flags)
   1083   1.134.2.6      yamt {
   1084   1.134.2.8      yamt 	struct cwdinfo *cwdi = obj;
   1085   1.134.2.6      yamt 
   1086   1.134.2.6      yamt 	rw_init(&cwdi->cwdi_lock);
   1087   1.134.2.6      yamt 
   1088   1.134.2.6      yamt 	return 0;
   1089   1.134.2.6      yamt }
   1090   1.134.2.6      yamt 
   1091   1.134.2.6      yamt static void
   1092   1.134.2.6      yamt cwdi_dtor(void *arg, void *obj)
   1093   1.134.2.6      yamt {
   1094   1.134.2.8      yamt 	struct cwdinfo *cwdi = obj;
   1095   1.134.2.6      yamt 
   1096   1.134.2.6      yamt 	rw_destroy(&cwdi->cwdi_lock);
   1097   1.134.2.6      yamt }
   1098   1.134.2.6      yamt 
   1099   1.134.2.8      yamt static int
   1100   1.134.2.8      yamt file_ctor(void *arg, void *obj, int flags)
   1101   1.134.2.8      yamt {
   1102  1.134.2.11      yamt 	file_t *fp = obj;
   1103   1.134.2.8      yamt 
   1104   1.134.2.8      yamt 	memset(fp, 0, sizeof(*fp));
   1105   1.134.2.8      yamt 	mutex_init(&fp->f_lock, MUTEX_DEFAULT, IPL_NONE);
   1106   1.134.2.8      yamt 
   1107   1.134.2.8      yamt 	mutex_enter(&filelist_lock);
   1108   1.134.2.8      yamt 	LIST_INSERT_HEAD(&filehead, fp, f_list);
   1109   1.134.2.8      yamt 	mutex_exit(&filelist_lock);
   1110   1.134.2.8      yamt 
   1111   1.134.2.8      yamt 	return 0;
   1112   1.134.2.8      yamt }
   1113   1.134.2.8      yamt 
   1114   1.134.2.8      yamt static void
   1115   1.134.2.8      yamt file_dtor(void *arg, void *obj)
   1116   1.134.2.8      yamt {
   1117  1.134.2.11      yamt 	file_t *fp = obj;
   1118   1.134.2.8      yamt 
   1119   1.134.2.8      yamt 	mutex_enter(&filelist_lock);
   1120   1.134.2.8      yamt 	LIST_REMOVE(fp, f_list);
   1121   1.134.2.8      yamt 	mutex_exit(&filelist_lock);
   1122   1.134.2.8      yamt 
   1123   1.134.2.8      yamt 	mutex_destroy(&fp->f_lock);
   1124   1.134.2.8      yamt }
   1125   1.134.2.8      yamt 
   1126  1.134.2.11      yamt static int
   1127  1.134.2.11      yamt fdfile_ctor(void *arg, void *obj, int flags)
   1128  1.134.2.11      yamt {
   1129  1.134.2.11      yamt 	fdfile_t *ff = obj;
   1130  1.134.2.11      yamt 
   1131  1.134.2.11      yamt 	memset(ff, 0, sizeof(*ff));
   1132  1.134.2.11      yamt 	mutex_init(&ff->ff_lock, MUTEX_DEFAULT, IPL_NONE);
   1133  1.134.2.11      yamt 	cv_init(&ff->ff_closing, "fdclose");
   1134  1.134.2.11      yamt 
   1135  1.134.2.11      yamt 	return 0;
   1136  1.134.2.11      yamt }
   1137  1.134.2.11      yamt 
   1138  1.134.2.11      yamt static void
   1139  1.134.2.11      yamt fdfile_dtor(void *arg, void *obj)
   1140  1.134.2.11      yamt {
   1141  1.134.2.11      yamt 	fdfile_t *ff = obj;
   1142  1.134.2.11      yamt 
   1143  1.134.2.11      yamt 	mutex_destroy(&ff->ff_lock);
   1144  1.134.2.11      yamt 	cv_destroy(&ff->ff_closing);
   1145  1.134.2.11      yamt }
   1146  1.134.2.11      yamt 
   1147  1.134.2.11      yamt file_t *
   1148   1.134.2.8      yamt fgetdummy(void)
   1149   1.134.2.8      yamt {
   1150  1.134.2.11      yamt 	file_t *fp;
   1151   1.134.2.8      yamt 
   1152   1.134.2.8      yamt 	fp = kmem_alloc(sizeof(*fp), KM_SLEEP);
   1153   1.134.2.8      yamt 	if (fp != NULL) {
   1154   1.134.2.8      yamt 		memset(fp, 0, sizeof(*fp));
   1155   1.134.2.8      yamt 		mutex_init(&fp->f_lock, MUTEX_DEFAULT, IPL_NONE);
   1156   1.134.2.8      yamt 	}
   1157   1.134.2.8      yamt 	return fp;
   1158   1.134.2.8      yamt }
   1159   1.134.2.8      yamt 
   1160   1.134.2.8      yamt void
   1161  1.134.2.11      yamt fputdummy(file_t *fp)
   1162        1.58   thorpej {
   1163        1.58   thorpej 
   1164  1.134.2.11      yamt 	mutex_destroy(&fp->f_lock);
   1165  1.134.2.11      yamt 	kmem_free(fp, sizeof(*fp));
   1166        1.58   thorpej }
   1167        1.58   thorpej 
   1168        1.58   thorpej /*
   1169  1.134.2.11      yamt  * Make p2 share p1's cwdinfo.
   1170        1.58   thorpej  */
   1171        1.58   thorpej void
   1172  1.134.2.11      yamt cwdshare(struct proc *p2)
   1173        1.58   thorpej {
   1174  1.134.2.11      yamt 	struct cwdinfo *cwdi;
   1175        1.58   thorpej 
   1176  1.134.2.11      yamt 	cwdi = curproc->p_cwdi;
   1177        1.58   thorpej 
   1178  1.134.2.11      yamt 	atomic_inc_uint(&cwdi->cwdi_refcnt);
   1179  1.134.2.11      yamt 	p2->p_cwdi = cwdi;
   1180        1.58   thorpej }
   1181        1.58   thorpej 
   1182        1.58   thorpej /*
   1183        1.58   thorpej  * Release a cwdinfo structure.
   1184        1.58   thorpej  */
   1185        1.58   thorpej void
   1186       1.126        pk cwdfree(struct cwdinfo *cwdi)
   1187        1.58   thorpej {
   1188        1.58   thorpej 
   1189   1.134.2.7      yamt 	if (atomic_dec_uint_nv(&cwdi->cwdi_refcnt) > 0)
   1190        1.58   thorpej 		return;
   1191        1.58   thorpej 
   1192        1.58   thorpej 	vrele(cwdi->cwdi_cdir);
   1193        1.58   thorpej 	if (cwdi->cwdi_rdir)
   1194        1.58   thorpej 		vrele(cwdi->cwdi_rdir);
   1195   1.134.2.4      yamt 	if (cwdi->cwdi_edir)
   1196   1.134.2.4      yamt 		vrele(cwdi->cwdi_edir);
   1197   1.134.2.6      yamt 	pool_cache_put(cwdi_cache, cwdi);
   1198        1.58   thorpej }
   1199        1.58   thorpej 
   1200        1.58   thorpej /*
   1201  1.134.2.11      yamt  * Create an initial filedesc structure.
   1202        1.48   thorpej  */
   1203  1.134.2.11      yamt filedesc_t *
   1204  1.134.2.11      yamt fd_init(filedesc_t *fdp)
   1205        1.48   thorpej {
   1206  1.134.2.11      yamt 	unsigned fd;
   1207  1.134.2.11      yamt 
   1208  1.134.2.11      yamt 	if (fdp == NULL) {
   1209  1.134.2.11      yamt 		fdp = pool_cache_get(filedesc_cache, PR_WAITOK);
   1210  1.134.2.11      yamt 	} else {
   1211  1.134.2.11      yamt 		filedesc_ctor(NULL, fdp, PR_WAITOK);
   1212  1.134.2.11      yamt 	}
   1213        1.48   thorpej 
   1214  1.134.2.11      yamt 	fdp->fd_refcnt = 1;
   1215  1.134.2.11      yamt 	fdp->fd_ofiles = fdp->fd_dfiles;
   1216  1.134.2.11      yamt 	fdp->fd_nfiles = NDFILE;
   1217  1.134.2.11      yamt 	fdp->fd_himap = fdp->fd_dhimap;
   1218  1.134.2.11      yamt 	fdp->fd_lomap = fdp->fd_dlomap;
   1219  1.134.2.11      yamt 	KASSERT(fdp->fd_lastfile == -1);
   1220  1.134.2.11      yamt 	KASSERT(fdp->fd_lastkqfile == -1);
   1221  1.134.2.11      yamt 	KASSERT(fdp->fd_knhash == NULL);
   1222        1.48   thorpej 
   1223  1.134.2.11      yamt 	memset(&fdp->fd_startzero, 0, sizeof(*fdp) -
   1224  1.134.2.11      yamt 	    offsetof(filedesc_t, fd_startzero));
   1225  1.134.2.11      yamt 	for (fd = 0; fd < NDFDFILE; fd++) {
   1226  1.134.2.11      yamt 		fdp->fd_ofiles[fd] = (fdfile_t *)fdp->fd_dfdfile[fd];
   1227  1.134.2.11      yamt 	}
   1228        1.48   thorpej 
   1229  1.134.2.11      yamt 	return fdp;
   1230        1.48   thorpej }
   1231        1.48   thorpej 
   1232        1.48   thorpej /*
   1233        1.48   thorpej  * Initialize a file descriptor table.
   1234        1.48   thorpej  */
   1235  1.134.2.11      yamt static int
   1236  1.134.2.11      yamt filedesc_ctor(void *arg, void *obj, int flag)
   1237        1.48   thorpej {
   1238  1.134.2.11      yamt 	filedesc_t *fdp = obj;
   1239  1.134.2.11      yamt 	int i;
   1240        1.48   thorpej 
   1241  1.134.2.11      yamt 	memset(fdp, 0, sizeof(*fdp));
   1242  1.134.2.11      yamt 	mutex_init(&fdp->fd_lock, MUTEX_DEFAULT, IPL_NONE);
   1243  1.134.2.11      yamt 	fdp->fd_lastfile = -1;
   1244  1.134.2.11      yamt 	fdp->fd_lastkqfile = -1;
   1245  1.134.2.11      yamt 
   1246  1.134.2.11      yamt 	KASSERT(sizeof(fdp->fd_dfdfile[0]) >= sizeof(fdfile_t));
   1247  1.134.2.11      yamt 	for (i = 0; i < NDFDFILE; i++) {
   1248  1.134.2.11      yamt 		fdfile_ctor(NULL, fdp->fd_dfdfile[i], PR_WAITOK);
   1249  1.134.2.11      yamt 	}
   1250        1.48   thorpej 
   1251  1.134.2.11      yamt 	return 0;
   1252        1.48   thorpej }
   1253        1.48   thorpej 
   1254  1.134.2.11      yamt static void
   1255  1.134.2.11      yamt filedesc_dtor(void *arg, void *obj)
   1256        1.48   thorpej {
   1257  1.134.2.11      yamt 	filedesc_t *fdp = obj;
   1258  1.134.2.11      yamt 	int i;
   1259        1.48   thorpej 
   1260  1.134.2.11      yamt 	for (i = 0; i < NDFDFILE; i++) {
   1261  1.134.2.11      yamt 		fdfile_dtor(NULL, fdp->fd_dfdfile[i]);
   1262  1.134.2.11      yamt 	}
   1263        1.48   thorpej 
   1264  1.134.2.11      yamt 	mutex_destroy(&fdp->fd_lock);
   1265        1.48   thorpej }
   1266        1.48   thorpej 
   1267        1.48   thorpej /*
   1268  1.134.2.11      yamt  * Make p2 share p1's filedesc structure.
   1269        1.48   thorpej  */
   1270        1.48   thorpej void
   1271  1.134.2.11      yamt fd_share(struct proc *p2)
   1272        1.48   thorpej {
   1273  1.134.2.11      yamt 	filedesc_t *fdp;
   1274        1.48   thorpej 
   1275  1.134.2.11      yamt 	fdp = curlwp->l_fd;
   1276  1.134.2.11      yamt 	p2->p_fd = fdp;
   1277  1.134.2.11      yamt 	atomic_inc_uint(&fdp->fd_refcnt);
   1278        1.16       cgd }
   1279        1.16       cgd 
   1280        1.16       cgd /*
   1281        1.16       cgd  * Copy a filedesc structure.
   1282        1.16       cgd  */
   1283  1.134.2.11      yamt filedesc_t *
   1284  1.134.2.11      yamt fd_copy(void)
   1285        1.16       cgd {
   1286  1.134.2.11      yamt 	filedesc_t *newfdp, *fdp;
   1287  1.134.2.11      yamt 	fdfile_t *ff, *fflist, **ffp, **nffp, *ff2;
   1288  1.134.2.11      yamt 	int i, nused, numfiles, lastfile, j, newlast;
   1289  1.134.2.11      yamt 	file_t *fp;
   1290        1.16       cgd 
   1291  1.134.2.11      yamt 	fdp = curproc->p_fd;
   1292  1.134.2.11      yamt 	newfdp = pool_cache_get(filedesc_cache, PR_WAITOK);
   1293        1.16       cgd 	newfdp->fd_refcnt = 1;
   1294       1.126        pk 
   1295  1.134.2.11      yamt 	KASSERT(newfdp->fd_knhash == NULL);
   1296  1.134.2.11      yamt 	KASSERT(newfdp->fd_knhashmask == 0);
   1297  1.134.2.11      yamt 	KASSERT(newfdp->fd_discard == NULL);
   1298  1.134.2.11      yamt 
   1299  1.134.2.11      yamt 	for (;;) {
   1300  1.134.2.11      yamt 		numfiles = fdp->fd_nfiles;
   1301  1.134.2.11      yamt 		lastfile = fdp->fd_lastfile;
   1302  1.134.2.11      yamt 
   1303  1.134.2.11      yamt 		/*
   1304  1.134.2.11      yamt 		 * If the number of open files fits in the internal arrays
   1305  1.134.2.11      yamt 		 * of the open file structure, use them, otherwise allocate
   1306  1.134.2.11      yamt 		 * additional memory for the number of descriptors currently
   1307  1.134.2.11      yamt 		 * in use.
   1308  1.134.2.11      yamt 		 */
   1309  1.134.2.11      yamt 		if (lastfile < NDFILE) {
   1310  1.134.2.11      yamt 			i = NDFILE;
   1311  1.134.2.11      yamt 			newfdp->fd_ofiles = newfdp->fd_dfiles;
   1312  1.134.2.11      yamt 		} else {
   1313  1.134.2.11      yamt 			/*
   1314  1.134.2.11      yamt 			 * Compute the smallest multiple of NDEXTENT needed
   1315  1.134.2.11      yamt 			 * for the file descriptors currently in use,
   1316  1.134.2.11      yamt 			 * allowing the table to shrink.
   1317  1.134.2.11      yamt 			 */
   1318  1.134.2.11      yamt 			i = numfiles;
   1319  1.134.2.11      yamt 			while (i >= 2 * NDEXTENT && i > lastfile * 2) {
   1320  1.134.2.11      yamt 				i /= 2;
   1321  1.134.2.11      yamt 			}
   1322  1.134.2.11      yamt 			newfdp->fd_ofiles = malloc(i * sizeof(fdfile_t *),
   1323  1.134.2.11      yamt 			    M_FILEDESC, M_WAITOK);
   1324  1.134.2.11      yamt 			KASSERT(i >= NDFILE);
   1325  1.134.2.11      yamt 		}
   1326  1.134.2.11      yamt 		if (NDHISLOTS(i) <= NDHISLOTS(NDFILE)) {
   1327  1.134.2.11      yamt 			newfdp->fd_himap = newfdp->fd_dhimap;
   1328  1.134.2.11      yamt 			newfdp->fd_lomap = newfdp->fd_dlomap;
   1329  1.134.2.11      yamt 		} else {
   1330  1.134.2.11      yamt 			newfdp->fd_himap = malloc(NDHISLOTS(i) *
   1331  1.134.2.11      yamt 			    sizeof(uint32_t), M_FILEDESC, M_WAITOK);
   1332  1.134.2.11      yamt 			newfdp->fd_lomap = malloc(NDLOSLOTS(i) *
   1333  1.134.2.11      yamt 			    sizeof(uint32_t), M_FILEDESC, M_WAITOK);
   1334  1.134.2.11      yamt 		}
   1335        1.16       cgd 
   1336        1.16       cgd 		/*
   1337  1.134.2.11      yamt 		 * Allocate and string together fdfile structures.
   1338  1.134.2.11      yamt 		 * We abuse fdfile_t::ff_file here, but it will be
   1339  1.134.2.11      yamt 		 * cleared before this routine returns.
   1340        1.16       cgd 		 */
   1341  1.134.2.11      yamt 		nused = fdp->fd_nused;
   1342  1.134.2.11      yamt 		fflist = NULL;
   1343  1.134.2.11      yamt 		for (j = nused; j != 0; j--) {
   1344  1.134.2.11      yamt 			ff = pool_cache_get(fdfile_cache, PR_WAITOK);
   1345  1.134.2.11      yamt 			ff->ff_file = (void *)fflist;
   1346  1.134.2.11      yamt 			fflist = ff;
   1347  1.134.2.11      yamt 		}
   1348  1.134.2.11      yamt 
   1349  1.134.2.11      yamt 		mutex_enter(&fdp->fd_lock);
   1350  1.134.2.11      yamt 		if (numfiles == fdp->fd_nfiles && nused == fdp->fd_nused &&
   1351  1.134.2.11      yamt 		    lastfile == fdp->fd_lastfile) {
   1352  1.134.2.11      yamt 			break;
   1353  1.134.2.11      yamt 		}
   1354  1.134.2.11      yamt 		mutex_exit(&fdp->fd_lock);
   1355  1.134.2.11      yamt 		if (i >= NDFILE) {
   1356       1.126        pk 			free(newfdp->fd_ofiles, M_FILEDESC);
   1357  1.134.2.11      yamt 		}
   1358       1.126        pk 		if (NDHISLOTS(i) > NDHISLOTS(NDFILE)) {
   1359       1.126        pk 			free(newfdp->fd_himap, M_FILEDESC);
   1360       1.126        pk 			free(newfdp->fd_lomap, M_FILEDESC);
   1361       1.126        pk 		}
   1362  1.134.2.11      yamt 		while (fflist != NULL) {
   1363  1.134.2.11      yamt 			ff = fflist;
   1364  1.134.2.11      yamt 			fflist = (void *)ff->ff_file;
   1365  1.134.2.11      yamt 			ff->ff_file = NULL;
   1366  1.134.2.11      yamt 			pool_cache_put(fdfile_cache, ff);
   1367  1.134.2.11      yamt 		}
   1368       1.115    provos 	}
   1369       1.115    provos 
   1370        1.16       cgd 	newfdp->fd_nfiles = i;
   1371       1.126        pk 	newfdp->fd_freefile = fdp->fd_freefile;
   1372  1.134.2.11      yamt 	newfdp->fd_exclose = fdp->fd_exclose;
   1373       1.126        pk 
   1374  1.134.2.11      yamt 	/*
   1375  1.134.2.11      yamt 	 * Clear the entries that will not be copied over.
   1376  1.134.2.11      yamt 	 * Avoid calling memset with 0 size.
   1377  1.134.2.11      yamt 	 */
   1378  1.134.2.11      yamt 	if (lastfile < (i-1)) {
   1379       1.128      cube 		memset(newfdp->fd_ofiles + lastfile + 1, 0,
   1380  1.134.2.11      yamt 		    (i - lastfile - 1) * sizeof(file_t **));
   1381  1.134.2.11      yamt 	}
   1382  1.134.2.11      yamt 	if (i < NDENTRIES * NDENTRIES) {
   1383       1.120      yamt 		i = NDENTRIES * NDENTRIES; /* size of inlined bitmaps */
   1384  1.134.2.11      yamt 	}
   1385       1.115    provos 	memcpy(newfdp->fd_himap, fdp->fd_himap, NDHISLOTS(i)*sizeof(uint32_t));
   1386       1.115    provos 	memcpy(newfdp->fd_lomap, fdp->fd_lomap, NDLOSLOTS(i)*sizeof(uint32_t));
   1387       1.126        pk 
   1388  1.134.2.11      yamt 	ffp = fdp->fd_ofiles;
   1389  1.134.2.11      yamt 	nffp = newfdp->fd_ofiles;
   1390  1.134.2.11      yamt 	j = imax(lastfile, (NDFDFILE - 1));
   1391  1.134.2.11      yamt 	newlast = -1;
   1392  1.134.2.11      yamt 	KASSERT(j < fdp->fd_nfiles);
   1393  1.134.2.11      yamt 	for (i = 0; i <= j; i++, ffp++, *nffp++ = ff2) {
   1394  1.134.2.11      yamt 		ff = *ffp;
   1395  1.134.2.11      yamt 		/* Install built-in fdfiles even if unused here. */
   1396  1.134.2.11      yamt 		if (i < NDFDFILE) {
   1397  1.134.2.11      yamt 			ff2 = (fdfile_t *)newfdp->fd_dfdfile[i];
   1398  1.134.2.11      yamt 		} else {
   1399  1.134.2.11      yamt 			ff2 = NULL;
   1400  1.134.2.11      yamt 		}
   1401  1.134.2.11      yamt 		/* Determine if descriptor is active in parent. */
   1402  1.134.2.11      yamt 		if (ff == NULL || !fd_isused(fdp, i)) {
   1403  1.134.2.11      yamt 			KASSERT(ff != NULL || i >= NDFDFILE);
   1404       1.126        pk 			continue;
   1405        1.96  jdolecek 		}
   1406  1.134.2.11      yamt 		mutex_enter(&ff->ff_lock);
   1407  1.134.2.11      yamt 		fp = ff->ff_file;
   1408  1.134.2.11      yamt 		if (fp == NULL) {
   1409  1.134.2.11      yamt 			/* Descriptor is half-open: free slot. */
   1410  1.134.2.11      yamt 			fd_zap(newfdp, i);
   1411  1.134.2.11      yamt 			mutex_exit(&ff->ff_lock);
   1412  1.134.2.11      yamt 			continue;
   1413  1.134.2.11      yamt 		}
   1414  1.134.2.11      yamt 		if (fp->f_type == DTYPE_KQUEUE) {
   1415  1.134.2.11      yamt 			/* kqueue descriptors cannot be copied. */
   1416  1.134.2.11      yamt 			fd_zap(newfdp, i);
   1417  1.134.2.11      yamt 			mutex_exit(&ff->ff_lock);
   1418  1.134.2.11      yamt 			continue;
   1419  1.134.2.11      yamt 		}
   1420  1.134.2.11      yamt 		/* It's active: add a reference to the file. */
   1421  1.134.2.11      yamt 		mutex_enter(&fp->f_lock);
   1422  1.134.2.11      yamt 		fp->f_count++;
   1423  1.134.2.11      yamt 		mutex_exit(&fp->f_lock);
   1424  1.134.2.11      yamt 		/* Consume one fdfile_t to represent it. */
   1425  1.134.2.11      yamt 		if (i >= NDFDFILE) {
   1426  1.134.2.11      yamt 			ff2 = fflist;
   1427  1.134.2.11      yamt 			fflist = (void *)ff2->ff_file;
   1428  1.134.2.11      yamt 		}
   1429  1.134.2.11      yamt 		ff2->ff_file = fp;
   1430  1.134.2.11      yamt 		ff2->ff_exclose = ff->ff_exclose;
   1431  1.134.2.11      yamt 		ff2->ff_allocated = 1;
   1432  1.134.2.11      yamt 		mutex_exit(&ff->ff_lock);
   1433  1.134.2.11      yamt 		if (i > newlast) {
   1434  1.134.2.11      yamt 			newlast = i;
   1435  1.134.2.11      yamt 		}
   1436  1.134.2.11      yamt 	}
   1437  1.134.2.11      yamt 	mutex_exit(&fdp->fd_lock);
   1438  1.134.2.11      yamt 
   1439  1.134.2.11      yamt 	/* Discard unused fdfile_t structures. */
   1440  1.134.2.11      yamt 	while (__predict_false(fflist != NULL)) {
   1441  1.134.2.11      yamt 		ff = fflist;
   1442  1.134.2.11      yamt 		fflist = (void *)ff->ff_file;
   1443  1.134.2.11      yamt 		ff->ff_file = NULL;
   1444  1.134.2.11      yamt 		pool_cache_put(fdfile_cache, ff);
   1445  1.134.2.11      yamt 		nused--;
   1446       1.126        pk 	}
   1447  1.134.2.11      yamt 	KASSERT(nused >= 0);
   1448  1.134.2.11      yamt 	KASSERT(newfdp->fd_ofiles[0] == (fdfile_t *)newfdp->fd_dfdfile[0]);
   1449       1.126        pk 
   1450  1.134.2.11      yamt 	newfdp->fd_nused = nused;
   1451  1.134.2.11      yamt 	newfdp->fd_lastfile = newlast;
   1452       1.126        pk 
   1453        1.16       cgd 	return (newfdp);
   1454        1.16       cgd }
   1455        1.16       cgd 
   1456        1.16       cgd /*
   1457        1.16       cgd  * Release a filedesc structure.
   1458        1.16       cgd  */
   1459        1.16       cgd void
   1460  1.134.2.11      yamt fd_free(void)
   1461        1.16       cgd {
   1462  1.134.2.11      yamt 	filedesc_t *fdp;
   1463  1.134.2.11      yamt 	fdfile_t *ff;
   1464  1.134.2.11      yamt 	file_t *fp;
   1465  1.134.2.11      yamt 	int fd, lastfd;
   1466  1.134.2.11      yamt 	void *discard;
   1467       1.126        pk 
   1468  1.134.2.11      yamt 	fdp = curlwp->l_fd;
   1469        1.16       cgd 
   1470  1.134.2.11      yamt 	KASSERT(fdp->fd_ofiles[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
   1471        1.59   thorpej 
   1472  1.134.2.11      yamt 	if (atomic_dec_uint_nv(&fdp->fd_refcnt) > 0)
   1473  1.134.2.11      yamt 		return;
   1474        1.59   thorpej 
   1475        1.59   thorpej 	/*
   1476  1.134.2.11      yamt 	 * Close any files that the process holds open.
   1477        1.59   thorpej 	 */
   1478  1.134.2.11      yamt 	for (fd = 0, lastfd = fdp->fd_lastfile; fd <= lastfd; fd++) {
   1479  1.134.2.11      yamt 		ff = fdp->fd_ofiles[fd];
   1480  1.134.2.11      yamt 		KASSERT(fd >= NDFDFILE ||
   1481  1.134.2.11      yamt 		    ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
   1482  1.134.2.11      yamt 		if ((ff = fdp->fd_ofiles[fd]) == NULL)
   1483  1.134.2.11      yamt 			continue;
   1484  1.134.2.11      yamt 		if ((fp = ff->ff_file) != NULL) {
   1485  1.134.2.11      yamt 			/*
   1486  1.134.2.11      yamt 			 * Must use fd_close() here as kqueue holds
   1487  1.134.2.11      yamt 			 * long term references to descriptors.
   1488  1.134.2.11      yamt 			 */
   1489  1.134.2.11      yamt 			ff->ff_refcnt++;
   1490  1.134.2.11      yamt 			fd_close(fd);
   1491  1.134.2.11      yamt 		}
   1492  1.134.2.11      yamt 		KASSERT(ff->ff_refcnt == 0);
   1493  1.134.2.11      yamt 		KASSERT(ff->ff_file == NULL);
   1494  1.134.2.11      yamt 		KASSERT(!ff->ff_exclose);
   1495  1.134.2.11      yamt 		KASSERT(!ff->ff_allocated);
   1496  1.134.2.11      yamt 		if (fd >= NDFDFILE) {
   1497  1.134.2.11      yamt 			pool_cache_put(fdfile_cache, ff);
   1498        1.59   thorpej 		}
   1499        1.59   thorpej 	}
   1500        1.59   thorpej 
   1501        1.59   thorpej 	/*
   1502  1.134.2.11      yamt 	 * Clean out the descriptor table for the next user and return
   1503  1.134.2.11      yamt 	 * to the cache.
   1504  1.134.2.11      yamt 	 */
   1505  1.134.2.11      yamt 	while ((discard = fdp->fd_discard) != NULL) {
   1506  1.134.2.11      yamt 		KASSERT(discard != fdp->fd_ofiles);
   1507  1.134.2.11      yamt 		fdp->fd_discard = *(void **)discard;
   1508  1.134.2.11      yamt 		free(discard, M_FILEDESC);
   1509   1.134.2.1      yamt 	}
   1510  1.134.2.11      yamt 	if (NDHISLOTS(fdp->fd_nfiles) > NDHISLOTS(NDFILE)) {
   1511  1.134.2.11      yamt 		KASSERT(fdp->fd_himap != fdp->fd_dhimap);
   1512  1.134.2.11      yamt 		KASSERT(fdp->fd_lomap != fdp->fd_dlomap);
   1513  1.134.2.11      yamt 		free(fdp->fd_himap, M_FILEDESC);
   1514  1.134.2.11      yamt 		free(fdp->fd_lomap, M_FILEDESC);
   1515   1.134.2.1      yamt 	}
   1516  1.134.2.11      yamt 	if (fdp->fd_nfiles > NDFILE) {
   1517  1.134.2.11      yamt 		KASSERT(fdp->fd_ofiles != fdp->fd_dfiles);
   1518  1.134.2.11      yamt 		free(fdp->fd_ofiles, M_FILEDESC);
   1519   1.134.2.1      yamt 	}
   1520  1.134.2.11      yamt 	if (fdp->fd_knhash != NULL) {
   1521  1.134.2.11      yamt 		hashdone(fdp->fd_knhash, M_KEVENT);
   1522  1.134.2.11      yamt 		fdp->fd_knhash = NULL;
   1523  1.134.2.11      yamt 		fdp->fd_knhashmask = 0;
   1524  1.134.2.11      yamt 	} else {
   1525  1.134.2.11      yamt 		KASSERT(fdp->fd_knhashmask == 0);
   1526   1.134.2.1      yamt 	}
   1527  1.134.2.11      yamt 	fdp->fd_lastkqfile = -1;
   1528  1.134.2.11      yamt 	pool_cache_put(filedesc_cache, fdp);
   1529   1.134.2.9      yamt }
   1530   1.134.2.9      yamt 
   1531        1.16       cgd /*
   1532        1.16       cgd  * File Descriptor pseudo-device driver (/dev/fd/).
   1533        1.16       cgd  *
   1534        1.16       cgd  * Opening minor device N dup()s the file (if any) connected to file
   1535        1.16       cgd  * descriptor N belonging to the calling process.  Note that this driver
   1536        1.16       cgd  * consists of only the ``open()'' routine, because all subsequent
   1537        1.16       cgd  * references to this file will be direct to the other driver.
   1538        1.16       cgd  */
   1539       1.134   thorpej static int
   1540  1.134.2.11      yamt filedescopen(dev_t dev, int mode, int type, lwp_t *l)
   1541        1.16       cgd {
   1542        1.16       cgd 
   1543        1.28   mycroft 	/*
   1544       1.112  jdolecek 	 * XXX Kludge: set dupfd to contain the value of the
   1545        1.89     enami 	 * the file descriptor being sought for duplication. The error
   1546        1.28   mycroft 	 * return ensures that the vnode for this device will be released
   1547        1.28   mycroft 	 * by vn_open. Open will detect this special error and take the
   1548        1.28   mycroft 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
   1549        1.28   mycroft 	 * will simply report the error.
   1550        1.28   mycroft 	 */
   1551   1.134.2.1      yamt 	l->l_dupfd = minor(dev);	/* XXX */
   1552       1.127  christos 	return EDUPFD;
   1553        1.27   mycroft }
   1554        1.27   mycroft 
   1555        1.28   mycroft /*
   1556        1.28   mycroft  * Duplicate the specified descriptor to a free descriptor.
   1557        1.28   mycroft  */
   1558        1.27   mycroft int
   1559  1.134.2.11      yamt fd_dupopen(int old, int *new, int mode, int error)
   1560        1.72     lukem {
   1561  1.134.2.11      yamt 	filedesc_t *fdp;
   1562  1.134.2.11      yamt 	fdfile_t *ff;
   1563  1.134.2.11      yamt 	file_t *fp;
   1564        1.77   thorpej 
   1565  1.134.2.11      yamt 	if ((fp = fd_getfile(old)) == NULL) {
   1566  1.134.2.11      yamt 		return EBADF;
   1567  1.134.2.11      yamt 	}
   1568  1.134.2.11      yamt 	fdp = curlwp->l_fd;
   1569  1.134.2.11      yamt 	ff = fdp->fd_ofiles[old];
   1570        1.59   thorpej 
   1571        1.27   mycroft 	/*
   1572        1.28   mycroft 	 * There are two cases of interest here.
   1573        1.28   mycroft 	 *
   1574       1.127  christos 	 * For EDUPFD simply dup (dfd) to file descriptor
   1575        1.28   mycroft 	 * (indx) and return.
   1576        1.28   mycroft 	 *
   1577       1.127  christos 	 * For EMOVEFD steal away the file structure from (dfd) and
   1578        1.28   mycroft 	 * store it in (indx).  (dfd) is effectively closed by
   1579        1.28   mycroft 	 * this operation.
   1580        1.28   mycroft 	 *
   1581        1.28   mycroft 	 * Any other error code is just returned.
   1582        1.27   mycroft 	 */
   1583        1.28   mycroft 	switch (error) {
   1584       1.127  christos 	case EDUPFD:
   1585        1.28   mycroft 		/*
   1586        1.28   mycroft 		 * Check that the mode the file is being opened for is a
   1587        1.28   mycroft 		 * subset of the mode of the existing descriptor.
   1588        1.28   mycroft 		 */
   1589  1.134.2.11      yamt 		if (((mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
   1590  1.134.2.11      yamt 			error = EACCES;
   1591  1.134.2.11      yamt 			break;
   1592  1.134.2.11      yamt 		}
   1593  1.134.2.11      yamt 
   1594  1.134.2.11      yamt 		/* Copy it. */
   1595  1.134.2.11      yamt 		error = fd_dup(fp, 0, new, fdp->fd_ofiles[old]->ff_exclose);
   1596  1.134.2.11      yamt 		break;
   1597        1.27   mycroft 
   1598       1.127  christos 	case EMOVEFD:
   1599  1.134.2.11      yamt 		/* Copy it. */
   1600  1.134.2.11      yamt 		error = fd_dup(fp, 0, new, fdp->fd_ofiles[old]->ff_exclose);
   1601  1.134.2.11      yamt 		if (error != 0) {
   1602  1.134.2.11      yamt 			break;
   1603  1.134.2.11      yamt 		}
   1604        1.16       cgd 
   1605  1.134.2.11      yamt 		/* Steal away the file pointer from 'old'. */
   1606  1.134.2.11      yamt 		(void)fd_close(old);
   1607  1.134.2.11      yamt 		return 0;
   1608        1.28   mycroft 	}
   1609  1.134.2.11      yamt 
   1610  1.134.2.11      yamt 	fd_putfile(old);
   1611  1.134.2.11      yamt 	return error;
   1612        1.61  wrstuden }
   1613        1.61  wrstuden 
   1614        1.61  wrstuden /*
   1615  1.134.2.11      yamt  * Close open files on exec.
   1616        1.27   mycroft  */
   1617        1.27   mycroft void
   1618  1.134.2.11      yamt fd_closeexec(void)
   1619        1.27   mycroft {
   1620  1.134.2.11      yamt 	struct cwdinfo *cwdi;
   1621  1.134.2.11      yamt 	proc_t *p;
   1622  1.134.2.11      yamt 	filedesc_t *fdp;
   1623  1.134.2.11      yamt 	fdfile_t *ff;
   1624  1.134.2.11      yamt 	lwp_t *l;
   1625  1.134.2.11      yamt 	int fd;
   1626        1.80   thorpej 
   1627  1.134.2.11      yamt 	l = curlwp;
   1628  1.134.2.11      yamt 	p = l->l_proc;
   1629  1.134.2.11      yamt 	fdp = p->p_fd;
   1630  1.134.2.11      yamt 	cwdi = p->p_cwdi;
   1631        1.16       cgd 
   1632  1.134.2.11      yamt 	if (cwdi->cwdi_refcnt > 1) {
   1633  1.134.2.11      yamt 		cwdi = cwdinit();
   1634  1.134.2.11      yamt 		cwdfree(p->p_cwdi);
   1635  1.134.2.11      yamt 		p->p_cwdi = cwdi;
   1636  1.134.2.11      yamt 	}
   1637  1.134.2.11      yamt 	if (p->p_cwdi->cwdi_edir) {
   1638   1.134.2.4      yamt 		vrele(p->p_cwdi->cwdi_edir);
   1639  1.134.2.11      yamt 	}
   1640   1.134.2.4      yamt 
   1641  1.134.2.11      yamt 	if (fdp->fd_refcnt > 1) {
   1642  1.134.2.11      yamt 		fdp = fd_copy();
   1643  1.134.2.11      yamt 		fd_free();
   1644  1.134.2.11      yamt 		p->p_fd = fdp;
   1645  1.134.2.11      yamt 		l->l_fd = fdp;
   1646  1.134.2.11      yamt 	}
   1647  1.134.2.11      yamt 	if (!fdp->fd_exclose) {
   1648  1.134.2.11      yamt 		return;
   1649  1.134.2.11      yamt 	}
   1650  1.134.2.11      yamt 	fdp->fd_exclose = 0;
   1651  1.134.2.11      yamt 
   1652  1.134.2.11      yamt 	for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
   1653  1.134.2.11      yamt 		if ((ff = fdp->fd_ofiles[fd]) == NULL) {
   1654  1.134.2.11      yamt 			KASSERT(fd >= NDFDFILE);
   1655  1.134.2.11      yamt 			continue;
   1656  1.134.2.11      yamt 		}
   1657  1.134.2.11      yamt 		KASSERT(fd >= NDFDFILE ||
   1658  1.134.2.11      yamt 		    ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
   1659  1.134.2.11      yamt 		if (ff->ff_file == NULL)
   1660  1.134.2.11      yamt 			continue;
   1661  1.134.2.11      yamt 		if (ff->ff_exclose) {
   1662  1.134.2.11      yamt 			/*
   1663  1.134.2.11      yamt 			 * We need a reference to close the file.
   1664  1.134.2.11      yamt 			 * No other threads can see the fdfile_t at
   1665  1.134.2.11      yamt 			 * this point, so don't bother locking.
   1666  1.134.2.11      yamt 			 */
   1667  1.134.2.11      yamt 			KASSERT((ff->ff_refcnt & FR_CLOSING) == 0);
   1668  1.134.2.11      yamt 			ff->ff_refcnt++;
   1669  1.134.2.11      yamt 			fd_close(fd);
   1670  1.134.2.11      yamt 		}
   1671  1.134.2.11      yamt 	}
   1672        1.86  christos }
   1673        1.86  christos 
   1674        1.86  christos /*
   1675        1.86  christos  * It is unsafe for set[ug]id processes to be started with file
   1676        1.86  christos  * descriptors 0..2 closed, as these descriptors are given implicit
   1677        1.86  christos  * significance in the Standard C library.  fdcheckstd() will create a
   1678        1.86  christos  * descriptor referencing /dev/null for each of stdin, stdout, and
   1679        1.86  christos  * stderr that is not already open.
   1680        1.86  christos  */
   1681        1.92    atatat #define CHECK_UPTO 3
   1682        1.86  christos int
   1683  1.134.2.11      yamt fd_checkstd(void)
   1684       1.109   darrenr {
   1685   1.134.2.1      yamt 	struct proc *p;
   1686        1.86  christos 	struct nameidata nd;
   1687  1.134.2.11      yamt 	filedesc_t *fdp;
   1688  1.134.2.11      yamt 	file_t *fp;
   1689        1.91     enami 	struct proc *pp;
   1690  1.134.2.11      yamt 	int fd, i, error, flags = FREAD|FWRITE;
   1691        1.92    atatat 	char closed[CHECK_UPTO * 3 + 1], which[3 + 1];
   1692        1.86  christos 
   1693  1.134.2.11      yamt 	p = curproc;
   1694        1.92    atatat 	closed[0] = '\0';
   1695        1.86  christos 	if ((fdp = p->p_fd) == NULL)
   1696        1.89     enami 		return (0);
   1697        1.92    atatat 	for (i = 0; i < CHECK_UPTO; i++) {
   1698  1.134.2.11      yamt 		KASSERT(i >= NDFDFILE ||
   1699  1.134.2.11      yamt 		    fdp->fd_ofiles[i] == (fdfile_t *)fdp->fd_dfdfile[i]);
   1700  1.134.2.11      yamt 		if (fdp->fd_ofiles[i]->ff_file != NULL)
   1701        1.86  christos 			continue;
   1702        1.92    atatat 		snprintf(which, sizeof(which), ",%d", i);
   1703       1.108    itojun 		strlcat(closed, which, sizeof(closed));
   1704  1.134.2.11      yamt 		if ((error = fd_allocfile(&fp, &fd)) != 0)
   1705  1.134.2.11      yamt 			return (error);
   1706  1.134.2.11      yamt 		KASSERT(fd < CHECK_UPTO);
   1707  1.134.2.11      yamt 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null");
   1708  1.134.2.11      yamt 		if ((error = vn_open(&nd, flags, 0)) != 0) {
   1709  1.134.2.11      yamt 			fd_abort(p, fp, fd);
   1710  1.134.2.11      yamt 			return (error);
   1711        1.86  christos 		}
   1712  1.134.2.11      yamt 		fp->f_data = nd.ni_vp;
   1713  1.134.2.11      yamt 		fp->f_flag = flags;
   1714  1.134.2.11      yamt 		fp->f_ops = &vnops;
   1715  1.134.2.11      yamt 		fp->f_type = DTYPE_VNODE;
   1716  1.134.2.11      yamt 		VOP_UNLOCK(nd.ni_vp, 0);
   1717  1.134.2.11      yamt 		fd_affix(p, fp, fd);
   1718        1.86  christos 	}
   1719        1.92    atatat 	if (closed[0] != '\0') {
   1720   1.134.2.4      yamt 		mutex_enter(&proclist_lock);
   1721        1.92    atatat 		pp = p->p_pptr;
   1722   1.134.2.3      yamt 		mutex_enter(&pp->p_mutex);
   1723        1.92    atatat 		log(LOG_WARNING, "set{u,g}id pid %d (%s) "
   1724        1.92    atatat 		    "was invoked by uid %d ppid %d (%s) "
   1725        1.92    atatat 		    "with fd %s closed\n",
   1726   1.134.2.1      yamt 		    p->p_pid, p->p_comm, kauth_cred_geteuid(pp->p_cred),
   1727        1.92    atatat 		    pp->p_pid, pp->p_comm, &closed[1]);
   1728   1.134.2.3      yamt 		mutex_exit(&pp->p_mutex);
   1729   1.134.2.4      yamt 		mutex_exit(&proclist_lock);
   1730        1.92    atatat 	}
   1731        1.89     enami 	return (0);
   1732        1.16       cgd }
   1733        1.92    atatat #undef CHECK_UPTO
   1734       1.113  jdolecek 
   1735       1.113  jdolecek /*
   1736       1.113  jdolecek  * Sets descriptor owner. If the owner is a process, 'pgid'
   1737       1.113  jdolecek  * is set to positive value, process ID. If the owner is process group,
   1738       1.113  jdolecek  * 'pgid' is set to -pg_id.
   1739       1.113  jdolecek  */
   1740       1.113  jdolecek int
   1741  1.134.2.11      yamt fsetown(pid_t *pgid, int cmd, const void *data)
   1742       1.113  jdolecek {
   1743       1.133  christos 	int id = *(const int *)data;
   1744       1.113  jdolecek 	int error;
   1745       1.113  jdolecek 
   1746       1.113  jdolecek 	switch (cmd) {
   1747       1.113  jdolecek 	case TIOCSPGRP:
   1748       1.113  jdolecek 		if (id < 0)
   1749       1.113  jdolecek 			return (EINVAL);
   1750       1.113  jdolecek 		id = -id;
   1751       1.113  jdolecek 		break;
   1752       1.113  jdolecek 	default:
   1753       1.113  jdolecek 		break;
   1754       1.113  jdolecek 	}
   1755       1.113  jdolecek 
   1756       1.113  jdolecek 	if (id > 0 && !pfind(id))
   1757       1.113  jdolecek 		return (ESRCH);
   1758  1.134.2.11      yamt 	else if (id < 0 && (error = pgid_in_session(curproc, -id)))
   1759       1.113  jdolecek 		return (error);
   1760       1.113  jdolecek 
   1761       1.113  jdolecek 	*pgid = id;
   1762       1.113  jdolecek 	return (0);
   1763       1.113  jdolecek }
   1764       1.113  jdolecek 
   1765       1.113  jdolecek /*
   1766       1.113  jdolecek  * Return descriptor owner information. If the value is positive,
   1767       1.113  jdolecek  * it's process ID. If it's negative, it's process group ID and
   1768       1.113  jdolecek  * needs the sign removed before use.
   1769       1.113  jdolecek  */
   1770       1.113  jdolecek int
   1771  1.134.2.11      yamt fgetown(pid_t pgid, int cmd, void *data)
   1772       1.113  jdolecek {
   1773  1.134.2.11      yamt 
   1774       1.113  jdolecek 	switch (cmd) {
   1775       1.113  jdolecek 	case TIOCGPGRP:
   1776       1.113  jdolecek 		*(int *)data = -pgid;
   1777       1.113  jdolecek 		break;
   1778       1.113  jdolecek 	default:
   1779       1.113  jdolecek 		*(int *)data = pgid;
   1780       1.113  jdolecek 		break;
   1781       1.113  jdolecek 	}
   1782       1.113  jdolecek 	return (0);
   1783       1.113  jdolecek }
   1784       1.113  jdolecek 
   1785       1.113  jdolecek /*
   1786       1.113  jdolecek  * Send signal to descriptor owner, either process or process group.
   1787       1.113  jdolecek  */
   1788       1.113  jdolecek void
   1789       1.114  christos fownsignal(pid_t pgid, int signo, int code, int band, void *fdescdata)
   1790       1.113  jdolecek {
   1791       1.113  jdolecek 	struct proc *p1;
   1792   1.134.2.3      yamt 	struct pgrp *pgrp;
   1793       1.131     perry 	ksiginfo_t ksi;
   1794       1.113  jdolecek 
   1795   1.134.2.2      yamt 	KSI_INIT(&ksi);
   1796       1.114  christos 	ksi.ksi_signo = signo;
   1797       1.113  jdolecek 	ksi.ksi_code = code;
   1798       1.113  jdolecek 	ksi.ksi_band = band;
   1799       1.113  jdolecek 
   1800   1.134.2.3      yamt 	/*
   1801   1.134.2.3      yamt 	 * Since we may be called from an interrupt context, we must use
   1802   1.134.2.3      yamt 	 * the proclist_mutex.
   1803   1.134.2.3      yamt 	 */
   1804   1.134.2.3      yamt 	mutex_enter(&proclist_mutex);
   1805   1.134.2.3      yamt 	if (pgid > 0 && (p1 = p_find(pgid, PFIND_LOCKED)))
   1806       1.113  jdolecek 		kpsignal(p1, &ksi, fdescdata);
   1807   1.134.2.3      yamt 	else if (pgid < 0 && (pgrp = pg_find(-pgid, PFIND_LOCKED)))
   1808   1.134.2.3      yamt 		kpgsignal(pgrp, &ksi, fdescdata, 0);
   1809   1.134.2.3      yamt 	mutex_exit(&proclist_mutex);
   1810       1.113  jdolecek }
   1811       1.127  christos 
   1812       1.127  christos int
   1813  1.134.2.11      yamt fd_clone(file_t *fp, unsigned fd, int flag, const struct fileops *fops,
   1814  1.134.2.11      yamt 	 void *data)
   1815       1.127  christos {
   1816  1.134.2.11      yamt 
   1817       1.130  christos 	fp->f_flag = flag;
   1818       1.127  christos 	fp->f_type = DTYPE_MISC;
   1819       1.127  christos 	fp->f_ops = fops;
   1820       1.127  christos 	fp->f_data = data;
   1821  1.134.2.11      yamt 	curlwp->l_dupfd = fd;
   1822  1.134.2.11      yamt 	fd_affix(curproc, fp, fd);
   1823       1.127  christos 
   1824       1.127  christos 	return EMOVEFD;
   1825       1.127  christos }
   1826       1.127  christos 
   1827       1.127  christos int
   1828  1.134.2.11      yamt fnullop_fcntl(file_t *fp, u_int cmd, void *data)
   1829       1.127  christos {
   1830   1.134.2.2      yamt 
   1831       1.127  christos 	if (cmd == F_SETFL)
   1832       1.127  christos 		return 0;
   1833       1.127  christos 
   1834       1.127  christos 	return EOPNOTSUPP;
   1835       1.127  christos }
   1836       1.127  christos 
   1837       1.127  christos int
   1838  1.134.2.11      yamt fnullop_poll(file_t *fp, int which)
   1839       1.127  christos {
   1840   1.134.2.2      yamt 
   1841       1.127  christos 	return 0;
   1842       1.127  christos }
   1843       1.127  christos 
   1844       1.127  christos int
   1845  1.134.2.11      yamt fnullop_kqfilter(file_t *fp, struct knote *kn)
   1846       1.127  christos {
   1847       1.127  christos 
   1848       1.127  christos 	return 0;
   1849       1.127  christos }
   1850       1.127  christos 
   1851       1.127  christos int
   1852  1.134.2.11      yamt fbadop_read(file_t *fp, off_t *offset, struct uio *uio,
   1853  1.134.2.11      yamt 	    kauth_cred_t cred, int flags)
   1854   1.134.2.5      yamt {
   1855   1.134.2.5      yamt 
   1856   1.134.2.5      yamt 	return EOPNOTSUPP;
   1857   1.134.2.5      yamt }
   1858   1.134.2.5      yamt 
   1859   1.134.2.5      yamt int
   1860  1.134.2.11      yamt fbadop_write(file_t *fp, off_t *offset, struct uio *uio,
   1861  1.134.2.11      yamt 	     kauth_cred_t cred, int flags)
   1862   1.134.2.5      yamt {
   1863   1.134.2.5      yamt 
   1864   1.134.2.5      yamt 	return EOPNOTSUPP;
   1865   1.134.2.5      yamt }
   1866   1.134.2.5      yamt 
   1867   1.134.2.5      yamt int
   1868  1.134.2.11      yamt fbadop_ioctl(file_t *fp, u_long com, void *data)
   1869   1.134.2.5      yamt {
   1870   1.134.2.5      yamt 
   1871   1.134.2.5      yamt 	return EOPNOTSUPP;
   1872   1.134.2.5      yamt }
   1873   1.134.2.5      yamt 
   1874   1.134.2.5      yamt int
   1875  1.134.2.11      yamt fbadop_stat(file_t *fp, struct stat *sb)
   1876       1.127  christos {
   1877   1.134.2.2      yamt 
   1878       1.127  christos 	return EOPNOTSUPP;
   1879       1.127  christos }
   1880   1.134.2.5      yamt 
   1881   1.134.2.5      yamt int
   1882  1.134.2.11      yamt fbadop_close(file_t *fp)
   1883   1.134.2.5      yamt {
   1884   1.134.2.5      yamt 
   1885   1.134.2.5      yamt 	return EOPNOTSUPP;
   1886   1.134.2.5      yamt }
   1887