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