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