[BACK]Return to kern_descrip.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Annotation of src/sys/kern/kern_descrip.c, Revision 1.234

1.233     kamil       1: /*     $NetBSD: kern_descrip.c,v 1.232 2018/07/03 12:17:54 kamil Exp $ */
1.173     ad          2:
                      3: /*-
1.190     ad          4:  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
1.173     ad          5:  * All rights reserved.
                      6:  *
1.190     ad          7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Andrew Doran.
                      9:  *
1.173     ad         10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     21:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     22:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     23:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     24:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     25:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     26:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     27:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     28:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     29:  * POSSIBILITY OF SUCH DAMAGE.
                     30:  */
1.22      cgd        31:
1.16      cgd        32: /*
1.17      cgd        33:  * Copyright (c) 1982, 1986, 1989, 1991, 1993
                     34:  *     The Regents of the University of California.  All rights reserved.
1.16      cgd        35:  * (c) UNIX System Laboratories, Inc.
                     36:  * All or some portions of this file are derived from material licensed
                     37:  * to the University of California by American Telephone and Telegraph
                     38:  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
                     39:  * the permission of UNIX System Laboratories, Inc.
                     40:  *
                     41:  * Redistribution and use in source and binary forms, with or without
                     42:  * modification, are permitted provided that the following conditions
                     43:  * are met:
                     44:  * 1. Redistributions of source code must retain the above copyright
                     45:  *    notice, this list of conditions and the following disclaimer.
                     46:  * 2. Redistributions in binary form must reproduce the above copyright
                     47:  *    notice, this list of conditions and the following disclaimer in the
                     48:  *    documentation and/or other materials provided with the distribution.
1.111     agc        49:  * 3. Neither the name of the University nor the names of its contributors
1.16      cgd        50:  *    may be used to endorse or promote products derived from this software
                     51:  *    without specific prior written permission.
                     52:  *
                     53:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     54:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     55:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     56:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     57:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     58:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     59:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     60:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     61:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     62:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     63:  * SUCH DAMAGE.
                     64:  *
1.51      fvdl       65:  *     @(#)kern_descrip.c      8.8 (Berkeley) 2/14/95
1.16      cgd        66:  */
1.81      lukem      67:
1.173     ad         68: /*
                     69:  * File descriptor management.
                     70:  */
                     71:
1.81      lukem      72: #include <sys/cdefs.h>
1.233     kamil      73: __KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.232 2018/07/03 12:17:54 kamil Exp $");
1.50      mrg        74:
1.16      cgd        75: #include <sys/param.h>
                     76: #include <sys/systm.h>
                     77: #include <sys/filedesc.h>
                     78: #include <sys/kernel.h>
                     79: #include <sys/proc.h>
                     80: #include <sys/file.h>
                     81: #include <sys/socket.h>
                     82: #include <sys/socketvar.h>
                     83: #include <sys/stat.h>
                     84: #include <sys/ioctl.h>
                     85: #include <sys/fcntl.h>
1.55      thorpej    86: #include <sys/pool.h>
1.17      cgd        87: #include <sys/unistd.h>
1.16      cgd        88: #include <sys/resourcevar.h>
1.42      christos   89: #include <sys/conf.h>
1.96      jdolecek   90: #include <sys/event.h>
1.143     elad       91: #include <sys/kauth.h>
1.163     ad         92: #include <sys/atomic.h>
1.25      cgd        93: #include <sys/syscallargs.h>
1.176     ad         94: #include <sys/cpu.h>
1.184     pooka      95: #include <sys/kmem.h>
                     96: #include <sys/vnode.h>
1.210     pooka      97: #include <sys/sysctl.h>
                     98: #include <sys/ktrace.h>
1.38      christos   99:
1.213     rmind     100: /*
                    101:  * A list (head) of open files, counter, and lock protecting them.
                    102:  */
                    103: struct filelist                filehead        __cacheline_aligned;
                    104: static u_int           nfiles          __cacheline_aligned;
                    105: kmutex_t               filelist_lock   __cacheline_aligned;
                    106:
                    107: static pool_cache_t    filedesc_cache  __read_mostly;
                    108: static pool_cache_t    file_cache      __read_mostly;
                    109: static pool_cache_t    fdfile_cache    __read_mostly;
                    110:
1.167     ad        111: static int     file_ctor(void *, void *, int);
                    112: static void    file_dtor(void *, void *);
1.173     ad        113: static int     fdfile_ctor(void *, void *, int);
                    114: static void    fdfile_dtor(void *, void *);
                    115: static int     filedesc_ctor(void *, void *, int);
                    116: static void    filedesc_dtor(void *, void *);
                    117: static int     filedescopen(dev_t, int, int, lwp_t *);
1.162     ad        118:
1.210     pooka     119: static int sysctl_kern_file(SYSCTLFN_PROTO);
                    120: static int sysctl_kern_file2(SYSCTLFN_PROTO);
                    121: static void fill_file(struct kinfo_file *, const file_t *, const fdfile_t *,
                    122:                      int, pid_t);
                    123:
1.173     ad        124: const struct cdevsw filedesc_cdevsw = {
1.224     dholland  125:        .d_open = filedescopen,
                    126:        .d_close = noclose,
                    127:        .d_read = noread,
                    128:        .d_write = nowrite,
                    129:        .d_ioctl = noioctl,
                    130:        .d_stop = nostop,
                    131:        .d_tty = notty,
                    132:        .d_poll = nopoll,
                    133:        .d_mmap = nommap,
                    134:        .d_kqfilter = nokqfilter,
1.225     dholland  135:        .d_discard = nodiscard,
1.224     dholland  136:        .d_flag = D_OTHER | D_MPSAFE
1.173     ad        137: };
                    138:
                    139: /* For ease of reading. */
                    140: __strong_alias(fd_putvnode,fd_putfile)
                    141: __strong_alias(fd_putsock,fd_putfile)
                    142:
                    143: /*
                    144:  * Initialize the descriptor system.
                    145:  */
                    146: void
                    147: fd_sys_init(void)
                    148: {
1.210     pooka     149:        static struct sysctllog *clog;
1.173     ad        150:
                    151:        mutex_init(&filelist_lock, MUTEX_DEFAULT, IPL_NONE);
                    152:
1.174     ad        153:        file_cache = pool_cache_init(sizeof(file_t), coherency_unit, 0,
1.173     ad        154:            0, "file", NULL, IPL_NONE, file_ctor, file_dtor, NULL);
                    155:        KASSERT(file_cache != NULL);
                    156:
1.174     ad        157:        fdfile_cache = pool_cache_init(sizeof(fdfile_t), coherency_unit, 0,
1.173     ad        158:            PR_LARGECACHE, "fdfile", NULL, IPL_NONE, fdfile_ctor, fdfile_dtor,
                    159:            NULL);
                    160:        KASSERT(fdfile_cache != NULL);
                    161:
1.174     ad        162:        filedesc_cache = pool_cache_init(sizeof(filedesc_t), coherency_unit,
1.173     ad        163:            0, 0, "filedesc", NULL, IPL_NONE, filedesc_ctor, filedesc_dtor,
                    164:            NULL);
                    165:        KASSERT(filedesc_cache != NULL);
1.210     pooka     166:
                    167:        sysctl_createv(&clog, 0, NULL, NULL,
                    168:                       CTLFLAG_PERMANENT,
                    169:                       CTLTYPE_STRUCT, "file",
                    170:                       SYSCTL_DESCR("System open file table"),
                    171:                       sysctl_kern_file, 0, NULL, 0,
                    172:                       CTL_KERN, KERN_FILE, CTL_EOL);
                    173:        sysctl_createv(&clog, 0, NULL, NULL,
                    174:                       CTLFLAG_PERMANENT,
                    175:                       CTLTYPE_STRUCT, "file2",
                    176:                       SYSCTL_DESCR("System open file table"),
                    177:                       sysctl_kern_file2, 0, NULL, 0,
                    178:                       CTL_KERN, KERN_FILE2, CTL_EOL);
1.173     ad        179: }
1.72      lukem     180:
1.192     ad        181: static bool
                    182: fd_isused(filedesc_t *fdp, unsigned fd)
                    183: {
                    184:        u_int off = fd >> NDENTRYSHIFT;
                    185:
                    186:        KASSERT(fd < fdp->fd_dt->dt_nfiles);
                    187:
1.233     kamil     188:        return (fdp->fd_lomap[off] & (1U << (fd & NDENTRYMASK))) != 0;
1.192     ad        189: }
                    190:
                    191: /*
                    192:  * Verify that the bitmaps match the descriptor table.
                    193:  */
                    194: static inline void
                    195: fd_checkmaps(filedesc_t *fdp)
                    196: {
                    197: #ifdef DEBUG
                    198:        fdtab_t *dt;
                    199:        u_int fd;
                    200:
                    201:        dt = fdp->fd_dt;
1.196     yamt      202:        if (fdp->fd_refcnt == -1) {
                    203:                /*
                    204:                 * fd_free tears down the table without maintaining its bitmap.
                    205:                 */
                    206:                return;
                    207:        }
1.192     ad        208:        for (fd = 0; fd < dt->dt_nfiles; fd++) {
                    209:                if (fd < NDFDFILE) {
                    210:                        KASSERT(dt->dt_ff[fd] ==
                    211:                            (fdfile_t *)fdp->fd_dfdfile[fd]);
                    212:                }
                    213:                if (dt->dt_ff[fd] == NULL) {
                    214:                        KASSERT(!fd_isused(fdp, fd));
                    215:                } else if (dt->dt_ff[fd]->ff_file != NULL) {
                    216:                        KASSERT(fd_isused(fdp, fd));
                    217:                }
                    218:        }
1.213     rmind     219: #endif
1.192     ad        220: }
                    221:
1.173     ad        222: static int
                    223: fd_next_zero(filedesc_t *fdp, uint32_t *bitmap, int want, u_int bits)
1.115     provos    224: {
                    225:        int i, off, maxoff;
                    226:        uint32_t sub;
                    227:
1.173     ad        228:        KASSERT(mutex_owned(&fdp->fd_lock));
                    229:
1.192     ad        230:        fd_checkmaps(fdp);
                    231:
1.115     provos    232:        if (want > bits)
                    233:                return -1;
                    234:
                    235:        off = want >> NDENTRYSHIFT;
                    236:        i = want & NDENTRYMASK;
                    237:        if (i) {
                    238:                sub = bitmap[off] | ((u_int)~0 >> (NDENTRIES - i));
                    239:                if (sub != ~0)
                    240:                        goto found;
                    241:                off++;
                    242:        }
                    243:
                    244:        maxoff = NDLOSLOTS(bits);
                    245:        while (off < maxoff) {
                    246:                if ((sub = bitmap[off]) != ~0)
                    247:                        goto found;
                    248:                off++;
                    249:        }
                    250:
1.213     rmind     251:        return -1;
1.115     provos    252:
                    253:  found:
                    254:        return (off << NDENTRYSHIFT) + ffs(~sub) - 1;
                    255: }
                    256:
1.134     thorpej   257: static int
1.173     ad        258: fd_last_set(filedesc_t *fd, int last)
1.115     provos    259: {
                    260:        int off, i;
1.192     ad        261:        fdfile_t **ff = fd->fd_dt->dt_ff;
1.115     provos    262:        uint32_t *bitmap = fd->fd_lomap;
                    263:
1.173     ad        264:        KASSERT(mutex_owned(&fd->fd_lock));
                    265:
1.192     ad        266:        fd_checkmaps(fd);
                    267:
1.115     provos    268:        off = (last - 1) >> NDENTRYSHIFT;
                    269:
1.121     provos    270:        while (off >= 0 && !bitmap[off])
1.115     provos    271:                off--;
                    272:
                    273:        if (off < 0)
1.213     rmind     274:                return -1;
1.131     perry     275:
1.115     provos    276:        i = ((off + 1) << NDENTRYSHIFT) - 1;
                    277:        if (i >= last)
                    278:                i = last - 1;
                    279:
1.173     ad        280:        /* XXX should use bitmap */
1.192     ad        281:        while (i > 0 && (ff[i] == NULL || !ff[i]->ff_allocated))
1.115     provos    282:                i--;
                    283:
1.213     rmind     284:        return i;
1.115     provos    285: }
                    286:
1.192     ad        287: static inline void
1.173     ad        288: fd_used(filedesc_t *fdp, unsigned fd)
1.27      mycroft   289: {
1.115     provos    290:        u_int off = fd >> NDENTRYSHIFT;
1.173     ad        291:        fdfile_t *ff;
                    292:
1.192     ad        293:        ff = fdp->fd_dt->dt_ff[fd];
1.115     provos    294:
1.173     ad        295:        KASSERT(mutex_owned(&fdp->fd_lock));
1.232     kamil     296:        KASSERT((fdp->fd_lomap[off] & (1U << (fd & NDENTRYMASK))) == 0);
1.173     ad        297:        KASSERT(ff != NULL);
                    298:        KASSERT(ff->ff_file == NULL);
1.213     rmind     299:        KASSERT(!ff->ff_allocated);
1.124     yamt      300:
1.217     chs       301:        ff->ff_allocated = true;
1.232     kamil     302:        fdp->fd_lomap[off] |= 1U << (fd & NDENTRYMASK);
1.192     ad        303:        if (__predict_false(fdp->fd_lomap[off] == ~0)) {
1.173     ad        304:                KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
1.232     kamil     305:                    (1U << (off & NDENTRYMASK))) == 0);
                    306:                fdp->fd_himap[off >> NDENTRYSHIFT] |= 1U << (off & NDENTRYMASK);
1.124     yamt      307:        }
1.27      mycroft   308:
1.173     ad        309:        if ((int)fd > fdp->fd_lastfile) {
1.27      mycroft   310:                fdp->fd_lastfile = fd;
1.173     ad        311:        }
                    312:
1.192     ad        313:        fd_checkmaps(fdp);
1.27      mycroft   314: }
                    315:
1.192     ad        316: static inline void
1.173     ad        317: fd_unused(filedesc_t *fdp, unsigned fd)
1.27      mycroft   318: {
1.115     provos    319:        u_int off = fd >> NDENTRYSHIFT;
1.173     ad        320:        fdfile_t *ff;
1.27      mycroft   321:
1.192     ad        322:        ff = fdp->fd_dt->dt_ff[fd];
1.173     ad        323:
                    324:        /*
                    325:         * Don't assert the lock is held here, as we may be copying
                    326:         * the table during exec() and it is not needed there.
                    327:         * procfs and sysctl are locked out by proc::p_reflock.
                    328:         *
                    329:         * KASSERT(mutex_owned(&fdp->fd_lock));
                    330:         */
                    331:        KASSERT(ff != NULL);
                    332:        KASSERT(ff->ff_file == NULL);
1.213     rmind     333:        KASSERT(ff->ff_allocated);
1.173     ad        334:
                    335:        if (fd < fdp->fd_freefile) {
1.27      mycroft   336:                fdp->fd_freefile = fd;
1.173     ad        337:        }
1.115     provos    338:
1.124     yamt      339:        if (fdp->fd_lomap[off] == ~0) {
1.173     ad        340:                KASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
1.124     yamt      341:                    (1 << (off & NDENTRYMASK))) != 0);
                    342:                fdp->fd_himap[off >> NDENTRYSHIFT] &=
                    343:                    ~(1 << (off & NDENTRYMASK));
                    344:        }
1.173     ad        345:        KASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) != 0);
1.115     provos    346:        fdp->fd_lomap[off] &= ~(1 << (fd & NDENTRYMASK));
1.217     chs       347:        ff->ff_allocated = false;
1.115     provos    348:
1.173     ad        349:        KASSERT(fd <= fdp->fd_lastfile);
                    350:        if (fd == fdp->fd_lastfile) {
                    351:                fdp->fd_lastfile = fd_last_set(fdp, fd);
                    352:        }
1.192     ad        353:        fd_checkmaps(fdp);
1.77      thorpej   354: }
                    355:
1.16      cgd       356: /*
1.173     ad        357:  * Look up the file structure corresponding to a file descriptor
                    358:  * and return the file, holding a reference on the descriptor.
1.134     thorpej   359:  */
1.214     rmind     360: file_t *
1.173     ad        361: fd_getfile(unsigned fd)
1.134     thorpej   362: {
1.173     ad        363:        filedesc_t *fdp;
                    364:        fdfile_t *ff;
                    365:        file_t *fp;
1.192     ad        366:        fdtab_t *dt;
1.134     thorpej   367:
                    368:        /*
1.173     ad        369:         * Look up the fdfile structure representing this descriptor.
1.192     ad        370:         * We are doing this unlocked.  See fd_tryexpand().
1.134     thorpej   371:         */
1.192     ad        372:        fdp = curlwp->l_fd;
                    373:        dt = fdp->fd_dt;
                    374:        if (__predict_false(fd >= dt->dt_nfiles)) {
1.173     ad        375:                return NULL;
                    376:        }
1.192     ad        377:        ff = dt->dt_ff[fd];
1.173     ad        378:        KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
                    379:        if (__predict_false(ff == NULL)) {
                    380:                return NULL;
                    381:        }
1.134     thorpej   382:
1.191     ad        383:        /* Now get a reference to the descriptor. */
                    384:        if (fdp->fd_refcnt == 1) {
                    385:                /*
                    386:                 * Single threaded: don't need to worry about concurrent
                    387:                 * access (other than earlier calls to kqueue, which may
                    388:                 * hold a reference to the descriptor).
                    389:                 */
                    390:                ff->ff_refcnt++;
                    391:        } else {
                    392:                /*
1.192     ad        393:                 * Multi threaded: issue a memory barrier to ensure that we
                    394:                 * acquire the file pointer _after_ adding a reference.  If
                    395:                 * no memory barrier, we could fetch a stale pointer.
1.191     ad        396:                 */
                    397:                atomic_inc_uint(&ff->ff_refcnt);
1.173     ad        398: #ifndef __HAVE_ATOMIC_AS_MEMBAR
1.191     ad        399:                membar_enter();
1.173     ad        400: #endif
1.191     ad        401:        }
1.134     thorpej   402:
1.173     ad        403:        /*
                    404:         * If the file is not open or is being closed then put the
                    405:         * reference back.
                    406:         */
                    407:        fp = ff->ff_file;
                    408:        if (__predict_true(fp != NULL)) {
                    409:                return fp;
1.134     thorpej   410:        }
1.173     ad        411:        fd_putfile(fd);
                    412:        return NULL;
1.134     thorpej   413: }
                    414:
                    415: /*
1.173     ad        416:  * Release a reference to a file descriptor acquired with fd_getfile().
1.161     ad        417:  */
                    418: void
1.173     ad        419: fd_putfile(unsigned fd)
1.161     ad        420: {
1.173     ad        421:        filedesc_t *fdp;
                    422:        fdfile_t *ff;
                    423:        u_int u, v;
                    424:
                    425:        fdp = curlwp->l_fd;
1.192     ad        426:        ff = fdp->fd_dt->dt_ff[fd];
1.173     ad        427:
1.192     ad        428:        KASSERT(fd < fdp->fd_dt->dt_nfiles);
1.173     ad        429:        KASSERT(ff != NULL);
                    430:        KASSERT((ff->ff_refcnt & FR_MASK) > 0);
                    431:        KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
1.161     ad        432:
1.191     ad        433:        if (fdp->fd_refcnt == 1) {
                    434:                /*
                    435:                 * Single threaded: don't need to worry about concurrent
                    436:                 * access (other than earlier calls to kqueue, which may
                    437:                 * hold a reference to the descriptor).
                    438:                 */
                    439:                if (__predict_false((ff->ff_refcnt & FR_CLOSING) != 0)) {
                    440:                        fd_close(fd);
                    441:                        return;
                    442:                }
                    443:                ff->ff_refcnt--;
                    444:                return;
                    445:        }
                    446:
1.173     ad        447:        /*
                    448:         * Ensure that any use of the file is complete and globally
                    449:         * visible before dropping the final reference.  If no membar,
                    450:         * the current CPU could still access memory associated with
                    451:         * the file after it has been freed or recycled by another
                    452:         * CPU.
                    453:         */
                    454: #ifndef __HAVE_ATOMIC_AS_MEMBAR
                    455:        membar_exit();
                    456: #endif
1.161     ad        457:
1.173     ad        458:        /*
                    459:         * Be optimistic and start out with the assumption that no other
                    460:         * threads are trying to close the descriptor.  If the CAS fails,
                    461:         * we lost a race and/or it's being closed.
                    462:         */
                    463:        for (u = ff->ff_refcnt & FR_MASK;; u = v) {
                    464:                v = atomic_cas_uint(&ff->ff_refcnt, u, u - 1);
                    465:                if (__predict_true(u == v)) {
                    466:                        return;
                    467:                }
                    468:                if (__predict_false((v & FR_CLOSING) != 0)) {
                    469:                        break;
                    470:                }
                    471:        }
1.162     ad        472:
1.173     ad        473:        /* Another thread is waiting to close the file: join it. */
                    474:        (void)fd_close(fd);
1.161     ad        475: }
                    476:
                    477: /*
1.173     ad        478:  * Convenience wrapper around fd_getfile() that returns reference
                    479:  * to a vnode.
1.16      cgd       480:  */
1.38      christos  481: int
1.173     ad        482: fd_getvnode(unsigned fd, file_t **fpp)
1.36      thorpej   483: {
1.173     ad        484:        vnode_t *vp;
                    485:        file_t *fp;
1.72      lukem     486:
1.173     ad        487:        fp = fd_getfile(fd);
                    488:        if (__predict_false(fp == NULL)) {
                    489:                return EBADF;
                    490:        }
                    491:        if (__predict_false(fp->f_type != DTYPE_VNODE)) {
                    492:                fd_putfile(fd);
                    493:                return EINVAL;
                    494:        }
1.227     matt      495:        vp = fp->f_vnode;
1.173     ad        496:        if (__predict_false(vp->v_type == VBAD)) {
                    497:                /* XXX Is this case really necessary? */
                    498:                fd_putfile(fd);
                    499:                return EBADF;
1.59      thorpej   500:        }
1.173     ad        501:        *fpp = fp;
                    502:        return 0;
1.16      cgd       503: }
                    504:
                    505: /*
1.173     ad        506:  * Convenience wrapper around fd_getfile() that returns reference
                    507:  * to a socket.
1.16      cgd       508:  */
1.38      christos  509: int
1.218     christos  510: fd_getsock1(unsigned fd, struct socket **sop, file_t **fp)
1.36      thorpej   511: {
1.218     christos  512:        *fp = fd_getfile(fd);
                    513:        if (__predict_false(*fp == NULL)) {
1.173     ad        514:                return EBADF;
1.103     pk        515:        }
1.218     christos  516:        if (__predict_false((*fp)->f_type != DTYPE_SOCKET)) {
1.173     ad        517:                fd_putfile(fd);
                    518:                return ENOTSOCK;
1.17      cgd       519:        }
1.227     matt      520:        *sop = (*fp)->f_socket;
1.173     ad        521:        return 0;
1.16      cgd       522: }
                    523:
1.218     christos  524: int
                    525: fd_getsock(unsigned fd, struct socket **sop)
                    526: {
                    527:        file_t *fp;
                    528:        return fd_getsock1(fd, sop, &fp);
                    529: }
                    530:
1.16      cgd       531: /*
1.173     ad        532:  * Look up the file structure corresponding to a file descriptor
                    533:  * and return it with a reference held on the file, not the
                    534:  * descriptor.
                    535:  *
                    536:  * This is heavyweight and only used when accessing descriptors
                    537:  * from a foreign process.  The caller must ensure that `p' does
                    538:  * not exit or fork across this call.
                    539:  *
                    540:  * To release the file (not descriptor) reference, use closef().
1.134     thorpej   541:  */
1.173     ad        542: file_t *
                    543: fd_getfile2(proc_t *p, unsigned fd)
1.134     thorpej   544: {
1.173     ad        545:        filedesc_t *fdp;
                    546:        fdfile_t *ff;
                    547:        file_t *fp;
1.192     ad        548:        fdtab_t *dt;
1.134     thorpej   549:
1.173     ad        550:        fdp = p->p_fd;
                    551:        mutex_enter(&fdp->fd_lock);
1.192     ad        552:        dt = fdp->fd_dt;
                    553:        if (fd >= dt->dt_nfiles) {
1.173     ad        554:                mutex_exit(&fdp->fd_lock);
                    555:                return NULL;
                    556:        }
1.192     ad        557:        if ((ff = dt->dt_ff[fd]) == NULL) {
1.173     ad        558:                mutex_exit(&fdp->fd_lock);
                    559:                return NULL;
                    560:        }
                    561:        if ((fp = ff->ff_file) == NULL) {
                    562:                mutex_exit(&fdp->fd_lock);
                    563:                return NULL;
1.158     dsl       564:        }
1.173     ad        565:        mutex_enter(&fp->f_lock);
                    566:        fp->f_count++;
                    567:        mutex_exit(&fp->f_lock);
                    568:        mutex_exit(&fdp->fd_lock);
1.158     dsl       569:
1.173     ad        570:        return fp;
1.158     dsl       571: }
                    572:
1.134     thorpej   573: /*
1.173     ad        574:  * Internal form of close.  Must be called with a reference to the
                    575:  * descriptor, and will drop the reference.  When all descriptor
                    576:  * references are dropped, releases the descriptor slot and a single
                    577:  * reference to the file structure.
                    578:  */
                    579: int
                    580: fd_close(unsigned fd)
                    581: {
                    582:        struct flock lf;
                    583:        filedesc_t *fdp;
                    584:        fdfile_t *ff;
                    585:        file_t *fp;
                    586:        proc_t *p;
                    587:        lwp_t *l;
1.192     ad        588:        u_int refcnt;
1.72      lukem     589:
1.173     ad        590:        l = curlwp;
1.99      thorpej   591:        p = l->l_proc;
1.173     ad        592:        fdp = l->l_fd;
1.192     ad        593:        ff = fdp->fd_dt->dt_ff[fd];
1.16      cgd       594:
1.173     ad        595:        KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
1.122     christos  596:
1.192     ad        597:        mutex_enter(&fdp->fd_lock);
1.173     ad        598:        KASSERT((ff->ff_refcnt & FR_MASK) > 0);
1.192     ad        599:        if (__predict_false(ff->ff_file == NULL)) {
1.173     ad        600:                /*
                    601:                 * Another user of the file is already closing, and is
                    602:                 * waiting for other users of the file to drain.  Release
                    603:                 * our reference, and wake up the closer.
                    604:                 */
                    605:                atomic_dec_uint(&ff->ff_refcnt);
                    606:                cv_broadcast(&ff->ff_closing);
1.192     ad        607:                mutex_exit(&fdp->fd_lock);
1.122     christos  608:
1.173     ad        609:                /*
                    610:                 * An application error, so pretend that the descriptor
                    611:                 * was already closed.  We can't safely wait for it to
                    612:                 * be closed without potentially deadlocking.
                    613:                 */
1.16      cgd       614:                return (EBADF);
1.61      wrstuden  615:        }
1.173     ad        616:        KASSERT((ff->ff_refcnt & FR_CLOSING) == 0);
1.61      wrstuden  617:
1.173     ad        618:        /*
                    619:         * There may be multiple users of this file within the process.
                    620:         * Notify existing and new users that the file is closing.  This
                    621:         * will prevent them from adding additional uses to this file
                    622:         * while we are closing it.
                    623:         */
                    624:        fp = ff->ff_file;
                    625:        ff->ff_file = NULL;
1.182     matt      626:        ff->ff_exclose = false;
1.17      cgd       627:
1.173     ad        628:        /*
                    629:         * We expect the caller to hold a descriptor reference - drop it.
                    630:         * The reference count may increase beyond zero at this point due
                    631:         * to an erroneous descriptor reference by an application, but
                    632:         * fd_getfile() will notice that the file is being closed and drop
                    633:         * the reference again.
                    634:         */
1.192     ad        635:        if (fdp->fd_refcnt == 1) {
                    636:                /* Single threaded. */
                    637:                refcnt = --(ff->ff_refcnt);
                    638:        } else {
                    639:                /* Multi threaded. */
1.173     ad        640: #ifndef __HAVE_ATOMIC_AS_MEMBAR
1.192     ad        641:                membar_producer();
1.173     ad        642: #endif
1.192     ad        643:                refcnt = atomic_dec_uint_nv(&ff->ff_refcnt);
                    644:        }
                    645:        if (__predict_false(refcnt != 0)) {
1.173     ad        646:                /*
                    647:                 * Wait for other references to drain.  This is typically
                    648:                 * an application error - the descriptor is being closed
                    649:                 * while still in use.
1.202     dsl       650:                 * (Or just a threaded application trying to unblock its
                    651:                 * thread that sleeps in (say) accept()).
1.173     ad        652:                 */
                    653:                atomic_or_uint(&ff->ff_refcnt, FR_CLOSING);
1.190     ad        654:
1.173     ad        655:                /*
                    656:                 * Remove any knotes attached to the file.  A knote
                    657:                 * attached to the descriptor can hold references on it.
                    658:                 */
1.192     ad        659:                mutex_exit(&fdp->fd_lock);
1.173     ad        660:                if (!SLIST_EMPTY(&ff->ff_knlist)) {
                    661:                        knote_fdclose(fd);
1.75      thorpej   662:                }
1.190     ad        663:
1.202     dsl       664:                /*
                    665:                 * Since the file system code doesn't know which fd
                    666:                 * each request came from (think dup()), we have to
                    667:                 * ask it to return ERESTART for any long-term blocks.
                    668:                 * The re-entry through read/write/etc will detect the
                    669:                 * closed fd and return EBAFD.
                    670:                 * Blocked partial writes may return a short length.
                    671:                 */
                    672:                (*fp->f_ops->fo_restart)(fp);
1.192     ad        673:                mutex_enter(&fdp->fd_lock);
1.190     ad        674:
1.173     ad        675:                /*
                    676:                 * We need to see the count drop to zero at least once,
                    677:                 * in order to ensure that all pre-existing references
                    678:                 * have been drained.  New references past this point are
                    679:                 * of no interest.
1.202     dsl       680:                 * XXX (dsl) this may need to call fo_restart() after a
                    681:                 * timeout to guarantee that all the system calls exit.
1.173     ad        682:                 */
                    683:                while ((ff->ff_refcnt & FR_MASK) != 0) {
1.192     ad        684:                        cv_wait(&ff->ff_closing, &fdp->fd_lock);
1.107     dsl       685:                }
1.173     ad        686:                atomic_and_uint(&ff->ff_refcnt, ~FR_CLOSING);
                    687:        } else {
                    688:                /* If no references, there must be no knotes. */
                    689:                KASSERT(SLIST_EMPTY(&ff->ff_knlist));
1.16      cgd       690:        }
1.59      thorpej   691:
1.173     ad        692:        /*
                    693:         * POSIX record locking dictates that any close releases ALL
                    694:         * locks owned by this process.  This is handled by setting
                    695:         * a flag in the unlock to free ONLY locks obeying POSIX
                    696:         * semantics, and not to free BSD-style file locks.
                    697:         * If the descriptor was in a message, POSIX-style locks
                    698:         * aren't passed with the descriptor.
                    699:         */
1.192     ad        700:        if (__predict_false((p->p_flag & PK_ADVLOCK) != 0 &&
                    701:            fp->f_type == DTYPE_VNODE)) {
1.173     ad        702:                lf.l_whence = SEEK_SET;
                    703:                lf.l_start = 0;
                    704:                lf.l_len = 0;
                    705:                lf.l_type = F_UNLCK;
1.192     ad        706:                mutex_exit(&fdp->fd_lock);
1.227     matt      707:                (void)VOP_ADVLOCK(fp->f_vnode, p, F_UNLCK, &lf, F_POSIX);
1.192     ad        708:                mutex_enter(&fdp->fd_lock);
1.103     pk        709:        }
                    710:
1.173     ad        711:        /* Free descriptor slot. */
1.126     pk        712:        fd_unused(fdp, fd);
1.173     ad        713:        mutex_exit(&fdp->fd_lock);
1.126     pk        714:
1.173     ad        715:        /* Now drop reference to the file itself. */
                    716:        return closef(fp);
1.27      mycroft   717: }
                    718:
1.17      cgd       719: /*
1.173     ad        720:  * Duplicate a file descriptor.
1.16      cgd       721:  */
1.38      christos  722: int
1.182     matt      723: fd_dup(file_t *fp, int minfd, int *newp, bool exclose)
1.36      thorpej   724: {
1.213     rmind     725:        proc_t *p = curproc;
1.173     ad        726:        int error;
1.16      cgd       727:
1.173     ad        728:        while ((error = fd_alloc(p, minfd, newp)) != 0) {
                    729:                if (error != ENOSPC) {
                    730:                        return error;
                    731:                }
                    732:                fd_tryexpand(p);
                    733:        }
1.79      thorpej   734:
1.192     ad        735:        curlwp->l_fd->fd_dt->dt_ff[*newp]->ff_exclose = exclose;
1.173     ad        736:        fd_affix(p, fp, *newp);
                    737:        return 0;
1.16      cgd       738: }
                    739:
1.17      cgd       740: /*
1.173     ad        741:  * dup2 operation.
1.153     dsl       742:  */
                    743: int
1.226     matt      744: fd_dup2(file_t *fp, unsigned newfd, int flags)
1.153     dsl       745: {
1.213     rmind     746:        filedesc_t *fdp = curlwp->l_fd;
1.173     ad        747:        fdfile_t *ff;
1.192     ad        748:        fdtab_t *dt;
1.153     dsl       749:
1.216     christos  750:        if (flags & ~(O_CLOEXEC|O_NONBLOCK))
                    751:                return EINVAL;
1.173     ad        752:        /*
                    753:         * Ensure there are enough slots in the descriptor table,
                    754:         * and allocate an fdfile_t up front in case we need it.
                    755:         */
1.226     matt      756:        while (newfd >= fdp->fd_dt->dt_nfiles) {
1.173     ad        757:                fd_tryexpand(curproc);
                    758:        }
                    759:        ff = pool_cache_get(fdfile_cache, PR_WAITOK);
1.153     dsl       760:
1.173     ad        761:        /*
                    762:         * If there is already a file open, close it.  If the file is
                    763:         * half open, wait for it to be constructed before closing it.
                    764:         * XXX Potential for deadlock here?
                    765:         */
                    766:        mutex_enter(&fdp->fd_lock);
1.226     matt      767:        while (fd_isused(fdp, newfd)) {
1.173     ad        768:                mutex_exit(&fdp->fd_lock);
1.226     matt      769:                if (fd_getfile(newfd) != NULL) {
                    770:                        (void)fd_close(newfd);
1.173     ad        771:                } else {
1.192     ad        772:                        /*
                    773:                         * Crummy, but unlikely to happen.
                    774:                         * Can occur if we interrupt another
                    775:                         * thread while it is opening a file.
                    776:                         */
1.173     ad        777:                        kpause("dup2", false, 1, NULL);
                    778:                }
                    779:                mutex_enter(&fdp->fd_lock);
                    780:        }
1.192     ad        781:        dt = fdp->fd_dt;
1.226     matt      782:        if (dt->dt_ff[newfd] == NULL) {
                    783:                KASSERT(newfd >= NDFDFILE);
                    784:                dt->dt_ff[newfd] = ff;
1.173     ad        785:                ff = NULL;
1.213     rmind     786:        }
1.226     matt      787:        fd_used(fdp, newfd);
1.173     ad        788:        mutex_exit(&fdp->fd_lock);
                    789:
1.226     matt      790:        dt->dt_ff[newfd]->ff_exclose = (flags & O_CLOEXEC) != 0;
1.215     christos  791:        fp->f_flag |= flags & FNONBLOCK;
1.173     ad        792:        /* Slot is now allocated.  Insert copy of the file. */
1.226     matt      793:        fd_affix(curproc, fp, newfd);
1.173     ad        794:        if (ff != NULL) {
                    795:                pool_cache_put(fdfile_cache, ff);
                    796:        }
                    797:        return 0;
1.153     dsl       798: }
                    799:
                    800: /*
1.173     ad        801:  * Drop reference to a file structure.
1.17      cgd       802:  */
1.38      christos  803: int
1.173     ad        804: closef(file_t *fp)
1.36      thorpej   805: {
1.173     ad        806:        struct flock lf;
                    807:        int error;
1.16      cgd       808:
1.173     ad        809:        /*
                    810:         * Drop reference.  If referenced elsewhere it's still open
                    811:         * and we have nothing more to do.
                    812:         */
                    813:        mutex_enter(&fp->f_lock);
                    814:        KASSERT(fp->f_count > 0);
                    815:        if (--fp->f_count > 0) {
                    816:                mutex_exit(&fp->f_lock);
                    817:                return 0;
                    818:        }
                    819:        KASSERT(fp->f_count == 0);
                    820:        mutex_exit(&fp->f_lock);
1.59      thorpej   821:
1.173     ad        822:        /* We held the last reference - release locks, close and free. */
1.213     rmind     823:        if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
                    824:                lf.l_whence = SEEK_SET;
1.173     ad        825:                lf.l_start = 0;
                    826:                lf.l_len = 0;
                    827:                lf.l_type = F_UNLCK;
1.227     matt      828:                (void)VOP_ADVLOCK(fp->f_vnode, fp, F_UNLCK, &lf, F_FLOCK);
1.173     ad        829:        }
                    830:        if (fp->f_ops != NULL) {
                    831:                error = (*fp->f_ops->fo_close)(fp);
                    832:        } else {
                    833:                error = 0;
1.17      cgd       834:        }
1.191     ad        835:        KASSERT(fp->f_count == 0);
                    836:        KASSERT(fp->f_cred != NULL);
                    837:        pool_cache_put(file_cache, fp);
1.59      thorpej   838:
1.173     ad        839:        return error;
1.16      cgd       840: }
                    841:
                    842: /*
                    843:  * Allocate a file descriptor for the process.
                    844:  */
1.38      christos  845: int
1.173     ad        846: fd_alloc(proc_t *p, int want, int *result)
1.72      lukem     847: {
1.213     rmind     848:        filedesc_t *fdp = p->p_fd;
1.226     matt      849:        int i, lim, last, error, hi;
                    850:        u_int off;
1.192     ad        851:        fdtab_t *dt;
1.173     ad        852:
                    853:        KASSERT(p == curproc || p == &proc0);
1.72      lukem     854:
1.16      cgd       855:        /*
                    856:         * Search for a free descriptor starting at the higher
1.173     ad        857:         * of want or fd_freefile.
1.16      cgd       858:         */
1.173     ad        859:        mutex_enter(&fdp->fd_lock);
1.192     ad        860:        fd_checkmaps(fdp);
                    861:        dt = fdp->fd_dt;
                    862:        KASSERT(dt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
1.17      cgd       863:        lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
1.192     ad        864:        last = min(dt->dt_nfiles, lim);
1.173     ad        865:        for (;;) {
                    866:                if ((i = want) < fdp->fd_freefile)
                    867:                        i = fdp->fd_freefile;
                    868:                off = i >> NDENTRYSHIFT;
1.226     matt      869:                hi = fd_next_zero(fdp, fdp->fd_himap, off,
1.173     ad        870:                    (last + NDENTRIES - 1) >> NDENTRYSHIFT);
1.226     matt      871:                if (hi == -1)
1.173     ad        872:                        break;
1.226     matt      873:                i = fd_next_zero(fdp, &fdp->fd_lomap[hi],
                    874:                    hi > off ? 0 : i & NDENTRYMASK, NDENTRIES);
1.115     provos    875:                if (i == -1) {
1.131     perry     876:                        /*
1.173     ad        877:                         * Free file descriptor in this block was
1.115     provos    878:                         * below want, try again with higher want.
                    879:                         */
1.226     matt      880:                        want = (hi + 1) << NDENTRYSHIFT;
1.173     ad        881:                        continue;
1.115     provos    882:                }
1.226     matt      883:                i += (hi << NDENTRYSHIFT);
1.173     ad        884:                if (i >= last) {
                    885:                        break;
                    886:                }
1.192     ad        887:                if (dt->dt_ff[i] == NULL) {
1.173     ad        888:                        KASSERT(i >= NDFDFILE);
1.192     ad        889:                        dt->dt_ff[i] = pool_cache_get(fdfile_cache, PR_WAITOK);
1.173     ad        890:                }
1.192     ad        891:                KASSERT(dt->dt_ff[i]->ff_file == NULL);
1.173     ad        892:                fd_used(fdp, i);
                    893:                if (want <= fdp->fd_freefile) {
                    894:                        fdp->fd_freefile = i;
1.16      cgd       895:                }
1.173     ad        896:                *result = i;
1.192     ad        897:                KASSERT(i >= NDFDFILE ||
                    898:                    dt->dt_ff[i] == (fdfile_t *)fdp->fd_dfdfile[i]);
                    899:                fd_checkmaps(fdp);
1.173     ad        900:                mutex_exit(&fdp->fd_lock);
                    901:                return 0;
1.90      enami     902:        }
1.16      cgd       903:
1.173     ad        904:        /* No space in current array.  Let the caller expand and retry. */
1.192     ad        905:        error = (dt->dt_nfiles >= lim) ? EMFILE : ENOSPC;
1.173     ad        906:        mutex_exit(&fdp->fd_lock);
                    907:        return error;
1.16      cgd       908: }
                    909:
1.173     ad        910: /*
1.192     ad        911:  * Allocate memory for a descriptor table.
1.185     ad        912:  */
1.192     ad        913: static fdtab_t *
                    914: fd_dtab_alloc(int n)
1.185     ad        915: {
1.192     ad        916:        fdtab_t *dt;
                    917:        size_t sz;
1.185     ad        918:
                    919:        KASSERT(n > NDFILE);
                    920:
1.192     ad        921:        sz = sizeof(*dt) + (n - NDFILE) * sizeof(dt->dt_ff[0]);
                    922:        dt = kmem_alloc(sz, KM_SLEEP);
                    923: #ifdef DIAGNOSTIC
                    924:        memset(dt, 0xff, sz);
                    925: #endif
                    926:        dt->dt_nfiles = n;
                    927:        dt->dt_link = NULL;
                    928:        return dt;
1.185     ad        929: }
                    930:
                    931: /*
1.192     ad        932:  * Free a descriptor table, and all tables linked for deferred free.
1.185     ad        933:  */
                    934: static void
1.192     ad        935: fd_dtab_free(fdtab_t *dt)
1.185     ad        936: {
1.192     ad        937:        fdtab_t *next;
                    938:        size_t sz;
1.185     ad        939:
1.192     ad        940:        do {
                    941:                next = dt->dt_link;
                    942:                KASSERT(dt->dt_nfiles > NDFILE);
                    943:                sz = sizeof(*dt) +
                    944:                    (dt->dt_nfiles - NDFILE) * sizeof(dt->dt_ff[0]);
                    945: #ifdef DIAGNOSTIC
                    946:                memset(dt, 0xff, sz);
                    947: #endif
                    948:                kmem_free(dt, sz);
                    949:                dt = next;
                    950:        } while (dt != NULL);
1.185     ad        951: }
                    952:
                    953: /*
                    954:  * Allocate descriptor bitmap.
                    955:  */
                    956: static void
                    957: fd_map_alloc(int n, uint32_t **lo, uint32_t **hi)
                    958: {
                    959:        uint8_t *ptr;
                    960:        size_t szlo, szhi;
                    961:
                    962:        KASSERT(n > NDENTRIES);
                    963:
                    964:        szlo = NDLOSLOTS(n) * sizeof(uint32_t);
                    965:        szhi = NDHISLOTS(n) * sizeof(uint32_t);
                    966:        ptr = kmem_alloc(szlo + szhi, KM_SLEEP);
                    967:        *lo = (uint32_t *)ptr;
                    968:        *hi = (uint32_t *)(ptr + szlo);
                    969: }
                    970:
                    971: /*
                    972:  * Free descriptor bitmap.
                    973:  */
                    974: static void
                    975: fd_map_free(int n, uint32_t *lo, uint32_t *hi)
                    976: {
                    977:        size_t szlo, szhi;
                    978:
                    979:        KASSERT(n > NDENTRIES);
                    980:
                    981:        szlo = NDLOSLOTS(n) * sizeof(uint32_t);
                    982:        szhi = NDHISLOTS(n) * sizeof(uint32_t);
                    983:        KASSERT(hi == (uint32_t *)((uint8_t *)lo + szlo));
                    984:        kmem_free(lo, szlo + szhi);
                    985: }
                    986:
                    987: /*
1.173     ad        988:  * Expand a process' descriptor table.
                    989:  */
1.76      thorpej   990: void
1.173     ad        991: fd_tryexpand(proc_t *p)
1.76      thorpej   992: {
1.173     ad        993:        filedesc_t *fdp;
                    994:        int i, numfiles, oldnfiles;
1.192     ad        995:        fdtab_t *newdt, *dt;
1.173     ad        996:        uint32_t *newhimap, *newlomap;
                    997:
                    998:        KASSERT(p == curproc || p == &proc0);
1.76      thorpej   999:
                   1000:        fdp = p->p_fd;
1.173     ad       1001:        newhimap = NULL;
                   1002:        newlomap = NULL;
1.192     ad       1003:        oldnfiles = fdp->fd_dt->dt_nfiles;
1.126     pk       1004:
                   1005:        if (oldnfiles < NDEXTENT)
1.133     christos 1006:                numfiles = NDEXTENT;
1.76      thorpej  1007:        else
1.133     christos 1008:                numfiles = 2 * oldnfiles;
1.126     pk       1009:
1.192     ad       1010:        newdt = fd_dtab_alloc(numfiles);
1.133     christos 1011:        if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
1.185     ad       1012:                fd_map_alloc(numfiles, &newlomap, &newhimap);
1.126     pk       1013:        }
                   1014:
1.173     ad       1015:        mutex_enter(&fdp->fd_lock);
1.192     ad       1016:        dt = fdp->fd_dt;
                   1017:        KASSERT(dt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
                   1018:        if (dt->dt_nfiles != oldnfiles) {
1.173     ad       1019:                /* fdp changed; caller must retry */
                   1020:                mutex_exit(&fdp->fd_lock);
1.192     ad       1021:                fd_dtab_free(newdt);
1.185     ad       1022:                if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
                   1023:                        fd_map_free(numfiles, newlomap, newhimap);
                   1024:                }
1.173     ad       1025:                return;
                   1026:        }
                   1027:
1.192     ad       1028:        /* Copy the existing descriptor table and zero the new portion. */
                   1029:        i = sizeof(fdfile_t *) * oldnfiles;
                   1030:        memcpy(newdt->dt_ff, dt->dt_ff, i);
1.194     yamt     1031:        memset((uint8_t *)newdt->dt_ff + i, 0,
                   1032:            numfiles * sizeof(fdfile_t *) - i);
1.173     ad       1033:
                   1034:        /*
1.192     ad       1035:         * Link old descriptor array into list to be discarded.  We defer
                   1036:         * freeing until the last reference to the descriptor table goes
                   1037:         * away (usually process exit).  This allows us to do lockless
                   1038:         * lookups in fd_getfile().
1.173     ad       1039:         */
                   1040:        if (oldnfiles > NDFILE) {
1.191     ad       1041:                if (fdp->fd_refcnt > 1) {
1.192     ad       1042:                        newdt->dt_link = dt;
1.173     ad       1043:                } else {
1.192     ad       1044:                        fd_dtab_free(dt);
1.173     ad       1045:                }
                   1046:        }
1.115     provos   1047:
1.133     christos 1048:        if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
1.173     ad       1049:                i = NDHISLOTS(oldnfiles) * sizeof(uint32_t);
                   1050:                memcpy(newhimap, fdp->fd_himap, i);
                   1051:                memset((uint8_t *)newhimap + i, 0,
1.133     christos 1052:                    NDHISLOTS(numfiles) * sizeof(uint32_t) - i);
1.115     provos   1053:
1.173     ad       1054:                i = NDLOSLOTS(oldnfiles) * sizeof(uint32_t);
                   1055:                memcpy(newlomap, fdp->fd_lomap, i);
                   1056:                memset((uint8_t *)newlomap + i, 0,
1.133     christos 1057:                    NDLOSLOTS(numfiles) * sizeof(uint32_t) - i);
1.115     provos   1058:
1.126     pk       1059:                if (NDHISLOTS(oldnfiles) > NDHISLOTS(NDFILE)) {
1.185     ad       1060:                        fd_map_free(oldnfiles, fdp->fd_lomap, fdp->fd_himap);
1.115     provos   1061:                }
                   1062:                fdp->fd_himap = newhimap;
                   1063:                fdp->fd_lomap = newlomap;
                   1064:        }
                   1065:
1.173     ad       1066:        /*
                   1067:         * All other modifications must become globally visible before
1.192     ad       1068:         * the change to fd_dt.  See fd_getfile().
1.173     ad       1069:         */
                   1070:        membar_producer();
1.192     ad       1071:        fdp->fd_dt = newdt;
                   1072:        KASSERT(newdt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
                   1073:        fd_checkmaps(fdp);
1.173     ad       1074:        mutex_exit(&fdp->fd_lock);
1.76      thorpej  1075: }
                   1076:
1.16      cgd      1077: /*
1.173     ad       1078:  * Create a new open file structure and allocate a file descriptor
                   1079:  * for the current process.
1.16      cgd      1080:  */
1.38      christos 1081: int
1.173     ad       1082: fd_allocfile(file_t **resultfp, int *resultfd)
1.16      cgd      1083: {
1.213     rmind    1084:        proc_t *p = curproc;
1.191     ad       1085:        kauth_cred_t cred;
1.173     ad       1086:        file_t *fp;
                   1087:        int error;
1.16      cgd      1088:
1.173     ad       1089:        while ((error = fd_alloc(p, 0, resultfd)) != 0) {
                   1090:                if (error != ENOSPC) {
                   1091:                        return error;
1.76      thorpej  1092:                }
1.173     ad       1093:                fd_tryexpand(p);
1.75      thorpej  1094:        }
1.102     pk       1095:
1.162     ad       1096:        fp = pool_cache_get(file_cache, PR_WAITOK);
1.191     ad       1097:        if (fp == NULL) {
1.217     chs      1098:                fd_abort(p, NULL, *resultfd);
1.191     ad       1099:                return ENFILE;
                   1100:        }
1.173     ad       1101:        KASSERT(fp->f_count == 0);
1.188     mrg      1102:        KASSERT(fp->f_msgcount == 0);
                   1103:        KASSERT(fp->f_unpcount == 0);
1.167     ad       1104:
1.191     ad       1105:        /* Replace cached credentials if not what we need. */
                   1106:        cred = curlwp->l_cred;
                   1107:        if (__predict_false(cred != fp->f_cred)) {
                   1108:                kauth_cred_free(fp->f_cred);
                   1109:                kauth_cred_hold(cred);
                   1110:                fp->f_cred = cred;
1.16      cgd      1111:        }
1.167     ad       1112:
1.188     mrg      1113:        /*
                   1114:         * Don't allow recycled files to be scanned.
1.191     ad       1115:         * See uipc_usrreq.c.
1.188     mrg      1116:         */
1.191     ad       1117:        if (__predict_false((fp->f_flag & FSCAN) != 0)) {
1.188     mrg      1118:                mutex_enter(&fp->f_lock);
                   1119:                atomic_and_uint(&fp->f_flag, ~FSCAN);
                   1120:                mutex_exit(&fp->f_lock);
                   1121:        }
                   1122:
1.167     ad       1123:        fp->f_advice = 0;
                   1124:        fp->f_offset = 0;
1.173     ad       1125:        *resultfp = fp;
                   1126:
                   1127:        return 0;
                   1128: }
                   1129:
                   1130: /*
                   1131:  * Successful creation of a new descriptor: make visible to the process.
                   1132:  */
                   1133: void
                   1134: fd_affix(proc_t *p, file_t *fp, unsigned fd)
                   1135: {
                   1136:        fdfile_t *ff;
                   1137:        filedesc_t *fdp;
                   1138:
                   1139:        KASSERT(p == curproc || p == &proc0);
                   1140:
                   1141:        /* Add a reference to the file structure. */
                   1142:        mutex_enter(&fp->f_lock);
                   1143:        fp->f_count++;
                   1144:        mutex_exit(&fp->f_lock);
1.167     ad       1145:
1.16      cgd      1146:        /*
1.173     ad       1147:         * Insert the new file into the descriptor slot.
                   1148:         *
                   1149:         * The memory barriers provided by lock activity in this routine
                   1150:         * ensure that any updates to the file structure become globally
                   1151:         * visible before the file becomes visible to other LWPs in the
                   1152:         * current process.
1.16      cgd      1153:         */
1.173     ad       1154:        fdp = p->p_fd;
1.192     ad       1155:        ff = fdp->fd_dt->dt_ff[fd];
1.173     ad       1156:
                   1157:        KASSERT(ff != NULL);
                   1158:        KASSERT(ff->ff_file == NULL);
                   1159:        KASSERT(ff->ff_allocated);
                   1160:        KASSERT(fd_isused(fdp, fd));
1.192     ad       1161:        KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
1.173     ad       1162:
                   1163:        /* No need to lock in order to make file initially visible. */
                   1164:        ff->ff_file = fp;
                   1165: }
                   1166:
                   1167: /*
                   1168:  * Abort creation of a new descriptor: free descriptor slot and file.
                   1169:  */
                   1170: void
                   1171: fd_abort(proc_t *p, file_t *fp, unsigned fd)
                   1172: {
                   1173:        filedesc_t *fdp;
                   1174:        fdfile_t *ff;
                   1175:
                   1176:        KASSERT(p == curproc || p == &proc0);
                   1177:
                   1178:        fdp = p->p_fd;
1.192     ad       1179:        ff = fdp->fd_dt->dt_ff[fd];
1.220     pooka    1180:        ff->ff_exclose = false;
1.173     ad       1181:
1.192     ad       1182:        KASSERT(fd >= NDFDFILE || ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
1.173     ad       1183:
                   1184:        mutex_enter(&fdp->fd_lock);
                   1185:        KASSERT(fd_isused(fdp, fd));
                   1186:        fd_unused(fdp, fd);
                   1187:        mutex_exit(&fdp->fd_lock);
1.167     ad       1188:
1.173     ad       1189:        if (fp != NULL) {
1.191     ad       1190:                KASSERT(fp->f_count == 0);
                   1191:                KASSERT(fp->f_cred != NULL);
                   1192:                pool_cache_put(file_cache, fp);
1.59      thorpej  1193:        }
1.16      cgd      1194: }
                   1195:
1.167     ad       1196: static int
                   1197: file_ctor(void *arg, void *obj, int flags)
                   1198: {
1.173     ad       1199:        file_t *fp = obj;
1.167     ad       1200:
                   1201:        memset(fp, 0, sizeof(*fp));
                   1202:
                   1203:        mutex_enter(&filelist_lock);
1.191     ad       1204:        if (__predict_false(nfiles >= maxfiles)) {
                   1205:                mutex_exit(&filelist_lock);
                   1206:                tablefull("file", "increase kern.maxfiles or MAXFILES");
                   1207:                return ENFILE;
                   1208:        }
                   1209:        nfiles++;
1.167     ad       1210:        LIST_INSERT_HEAD(&filehead, fp, f_list);
1.191     ad       1211:        mutex_init(&fp->f_lock, MUTEX_DEFAULT, IPL_NONE);
                   1212:        fp->f_cred = curlwp->l_cred;
                   1213:        kauth_cred_hold(fp->f_cred);
1.167     ad       1214:        mutex_exit(&filelist_lock);
                   1215:
                   1216:        return 0;
                   1217: }
                   1218:
                   1219: static void
                   1220: file_dtor(void *arg, void *obj)
                   1221: {
1.173     ad       1222:        file_t *fp = obj;
1.167     ad       1223:
                   1224:        mutex_enter(&filelist_lock);
1.191     ad       1225:        nfiles--;
1.167     ad       1226:        LIST_REMOVE(fp, f_list);
                   1227:        mutex_exit(&filelist_lock);
                   1228:
1.191     ad       1229:        kauth_cred_free(fp->f_cred);
1.167     ad       1230:        mutex_destroy(&fp->f_lock);
                   1231: }
                   1232:
1.173     ad       1233: static int
                   1234: fdfile_ctor(void *arg, void *obj, int flags)
                   1235: {
                   1236:        fdfile_t *ff = obj;
                   1237:
                   1238:        memset(ff, 0, sizeof(*ff));
                   1239:        cv_init(&ff->ff_closing, "fdclose");
                   1240:
                   1241:        return 0;
                   1242: }
                   1243:
                   1244: static void
                   1245: fdfile_dtor(void *arg, void *obj)
                   1246: {
                   1247:        fdfile_t *ff = obj;
                   1248:
                   1249:        cv_destroy(&ff->ff_closing);
                   1250: }
                   1251:
                   1252: file_t *
1.169     ad       1253: fgetdummy(void)
                   1254: {
1.173     ad       1255:        file_t *fp;
1.169     ad       1256:
1.213     rmind    1257:        fp = kmem_zalloc(sizeof(*fp), KM_SLEEP);
1.231     chs      1258:        mutex_init(&fp->f_lock, MUTEX_DEFAULT, IPL_NONE);
1.169     ad       1259:        return fp;
                   1260: }
                   1261:
                   1262: void
1.173     ad       1263: fputdummy(file_t *fp)
1.58      thorpej  1264: {
                   1265:
1.173     ad       1266:        mutex_destroy(&fp->f_lock);
                   1267:        kmem_free(fp, sizeof(*fp));
1.58      thorpej  1268: }
                   1269:
                   1270: /*
1.173     ad       1271:  * Create an initial filedesc structure.
1.48      thorpej  1272:  */
1.173     ad       1273: filedesc_t *
                   1274: fd_init(filedesc_t *fdp)
1.48      thorpej  1275: {
1.192     ad       1276: #ifdef DIAGNOSTIC
1.173     ad       1277:        unsigned fd;
1.192     ad       1278: #endif
1.173     ad       1279:
1.192     ad       1280:        if (__predict_true(fdp == NULL)) {
1.173     ad       1281:                fdp = pool_cache_get(filedesc_cache, PR_WAITOK);
                   1282:        } else {
1.206     pooka    1283:                KASSERT(fdp == &filedesc0);
1.173     ad       1284:                filedesc_ctor(NULL, fdp, PR_WAITOK);
                   1285:        }
1.48      thorpej  1286:
1.192     ad       1287: #ifdef DIAGNOSTIC
1.173     ad       1288:        KASSERT(fdp->fd_lastfile == -1);
                   1289:        KASSERT(fdp->fd_lastkqfile == -1);
                   1290:        KASSERT(fdp->fd_knhash == NULL);
1.192     ad       1291:        KASSERT(fdp->fd_freefile == 0);
                   1292:        KASSERT(fdp->fd_exclose == false);
                   1293:        KASSERT(fdp->fd_dt == &fdp->fd_dtbuiltin);
                   1294:        KASSERT(fdp->fd_dtbuiltin.dt_nfiles == NDFILE);
1.173     ad       1295:        for (fd = 0; fd < NDFDFILE; fd++) {
1.192     ad       1296:                KASSERT(fdp->fd_dtbuiltin.dt_ff[fd] ==
                   1297:                    (fdfile_t *)fdp->fd_dfdfile[fd]);
                   1298:        }
                   1299:        for (fd = NDFDFILE; fd < NDFILE; fd++) {
                   1300:                KASSERT(fdp->fd_dtbuiltin.dt_ff[fd] == NULL);
1.173     ad       1301:        }
1.195     yamt     1302:        KASSERT(fdp->fd_himap == fdp->fd_dhimap);
                   1303:        KASSERT(fdp->fd_lomap == fdp->fd_dlomap);
1.192     ad       1304: #endif /* DIAGNOSTIC */
                   1305:
                   1306:        fdp->fd_refcnt = 1;
1.199     yamt     1307:        fd_checkmaps(fdp);
1.48      thorpej  1308:
1.173     ad       1309:        return fdp;
1.48      thorpej  1310: }
                   1311:
                   1312: /*
                   1313:  * Initialize a file descriptor table.
                   1314:  */
1.173     ad       1315: static int
                   1316: filedesc_ctor(void *arg, void *obj, int flag)
1.48      thorpej  1317: {
1.173     ad       1318:        filedesc_t *fdp = obj;
1.192     ad       1319:        fdfile_t **ffp;
1.173     ad       1320:        int i;
1.48      thorpej  1321:
1.173     ad       1322:        memset(fdp, 0, sizeof(*fdp));
                   1323:        mutex_init(&fdp->fd_lock, MUTEX_DEFAULT, IPL_NONE);
                   1324:        fdp->fd_lastfile = -1;
                   1325:        fdp->fd_lastkqfile = -1;
1.192     ad       1326:        fdp->fd_dt = &fdp->fd_dtbuiltin;
                   1327:        fdp->fd_dtbuiltin.dt_nfiles = NDFILE;
1.195     yamt     1328:        fdp->fd_himap = fdp->fd_dhimap;
                   1329:        fdp->fd_lomap = fdp->fd_dlomap;
1.173     ad       1330:
1.181     matt     1331:        CTASSERT(sizeof(fdp->fd_dfdfile[0]) >= sizeof(fdfile_t));
1.192     ad       1332:        for (i = 0, ffp = fdp->fd_dt->dt_ff; i < NDFDFILE; i++, ffp++) {
                   1333:                *ffp = (fdfile_t *)fdp->fd_dfdfile[i];
                   1334:                (void)fdfile_ctor(NULL, fdp->fd_dfdfile[i], PR_WAITOK);
1.173     ad       1335:        }
1.48      thorpej  1336:
1.173     ad       1337:        return 0;
1.48      thorpej  1338: }
                   1339:
1.173     ad       1340: static void
                   1341: filedesc_dtor(void *arg, void *obj)
1.48      thorpej  1342: {
1.173     ad       1343:        filedesc_t *fdp = obj;
                   1344:        int i;
1.48      thorpej  1345:
1.173     ad       1346:        for (i = 0; i < NDFDFILE; i++) {
                   1347:                fdfile_dtor(NULL, fdp->fd_dfdfile[i]);
                   1348:        }
1.48      thorpej  1349:
1.173     ad       1350:        mutex_destroy(&fdp->fd_lock);
1.48      thorpej  1351: }
                   1352:
                   1353: /*
1.209     pooka    1354:  * Make p share curproc's filedesc structure.
1.48      thorpej  1355:  */
                   1356: void
1.209     pooka    1357: fd_share(struct proc *p)
1.48      thorpej  1358: {
1.173     ad       1359:        filedesc_t *fdp;
1.48      thorpej  1360:
1.173     ad       1361:        fdp = curlwp->l_fd;
1.209     pooka    1362:        p->p_fd = fdp;
1.173     ad       1363:        atomic_inc_uint(&fdp->fd_refcnt);
1.16      cgd      1364: }
                   1365:
                   1366: /*
1.191     ad       1367:  * Acquire a hold on a filedesc structure.
                   1368:  */
                   1369: void
1.200     rmind    1370: fd_hold(lwp_t *l)
1.191     ad       1371: {
1.200     rmind    1372:        filedesc_t *fdp = l->l_fd;
1.191     ad       1373:
1.200     rmind    1374:        atomic_inc_uint(&fdp->fd_refcnt);
1.191     ad       1375: }
                   1376:
                   1377: /*
1.16      cgd      1378:  * Copy a filedesc structure.
                   1379:  */
1.173     ad       1380: filedesc_t *
                   1381: fd_copy(void)
1.16      cgd      1382: {
1.173     ad       1383:        filedesc_t *newfdp, *fdp;
1.192     ad       1384:        fdfile_t *ff, **ffp, **nffp, *ff2;
                   1385:        int i, j, numfiles, lastfile, newlast;
1.173     ad       1386:        file_t *fp;
1.192     ad       1387:        fdtab_t *newdt;
1.16      cgd      1388:
1.173     ad       1389:        fdp = curproc->p_fd;
                   1390:        newfdp = pool_cache_get(filedesc_cache, PR_WAITOK);
1.16      cgd      1391:        newfdp->fd_refcnt = 1;
1.126     pk       1392:
1.192     ad       1393: #ifdef DIAGNOSTIC
                   1394:        KASSERT(newfdp->fd_lastfile == -1);
                   1395:        KASSERT(newfdp->fd_lastkqfile == -1);
1.173     ad       1396:        KASSERT(newfdp->fd_knhash == NULL);
1.192     ad       1397:        KASSERT(newfdp->fd_freefile == 0);
                   1398:        KASSERT(newfdp->fd_exclose == false);
                   1399:        KASSERT(newfdp->fd_dt == &newfdp->fd_dtbuiltin);
                   1400:        KASSERT(newfdp->fd_dtbuiltin.dt_nfiles == NDFILE);
                   1401:        for (i = 0; i < NDFDFILE; i++) {
                   1402:                KASSERT(newfdp->fd_dtbuiltin.dt_ff[i] ==
                   1403:                    (fdfile_t *)&newfdp->fd_dfdfile[i]);
                   1404:        }
                   1405:        for (i = NDFDFILE; i < NDFILE; i++) {
                   1406:                KASSERT(newfdp->fd_dtbuiltin.dt_ff[i] == NULL);
                   1407:        }
                   1408: #endif /* DIAGNOSTIC */
1.173     ad       1409:
1.192     ad       1410:        mutex_enter(&fdp->fd_lock);
                   1411:        fd_checkmaps(fdp);
                   1412:        numfiles = fdp->fd_dt->dt_nfiles;
                   1413:        lastfile = fdp->fd_lastfile;
1.173     ad       1414:
1.192     ad       1415:        /*
                   1416:         * If the number of open files fits in the internal arrays
                   1417:         * of the open file structure, use them, otherwise allocate
                   1418:         * additional memory for the number of descriptors currently
                   1419:         * in use.
                   1420:         */
                   1421:        if (lastfile < NDFILE) {
                   1422:                i = NDFILE;
                   1423:                newdt = newfdp->fd_dt;
                   1424:                KASSERT(newfdp->fd_dt == &newfdp->fd_dtbuiltin);
                   1425:        } else {
1.173     ad       1426:                /*
1.192     ad       1427:                 * Compute the smallest multiple of NDEXTENT needed
                   1428:                 * for the file descriptors currently in use,
                   1429:                 * allowing the table to shrink.
1.173     ad       1430:                 */
1.192     ad       1431:                i = numfiles;
                   1432:                while (i >= 2 * NDEXTENT && i > lastfile * 2) {
                   1433:                        i /= 2;
                   1434:                }
                   1435:                KASSERT(i > NDFILE);
                   1436:                newdt = fd_dtab_alloc(i);
                   1437:                newfdp->fd_dt = newdt;
                   1438:                memcpy(newdt->dt_ff, newfdp->fd_dtbuiltin.dt_ff,
                   1439:                    NDFDFILE * sizeof(fdfile_t **));
                   1440:                memset(newdt->dt_ff + NDFDFILE, 0,
                   1441:                    (i - NDFDFILE) * sizeof(fdfile_t **));
                   1442:        }
                   1443:        if (NDHISLOTS(i) <= NDHISLOTS(NDFILE)) {
                   1444:                newfdp->fd_himap = newfdp->fd_dhimap;
                   1445:                newfdp->fd_lomap = newfdp->fd_dlomap;
                   1446:        } else {
                   1447:                fd_map_alloc(i, &newfdp->fd_lomap, &newfdp->fd_himap);
                   1448:                KASSERT(i >= NDENTRIES * NDENTRIES);
                   1449:                memset(newfdp->fd_himap, 0, NDHISLOTS(i)*sizeof(uint32_t));
                   1450:                memset(newfdp->fd_lomap, 0, NDLOSLOTS(i)*sizeof(uint32_t));
1.115     provos   1451:        }
1.126     pk       1452:        newfdp->fd_freefile = fdp->fd_freefile;
1.173     ad       1453:        newfdp->fd_exclose = fdp->fd_exclose;
1.126     pk       1454:
1.192     ad       1455:        ffp = fdp->fd_dt->dt_ff;
                   1456:        nffp = newdt->dt_ff;
1.173     ad       1457:        newlast = -1;
1.228     christos 1458:        for (i = 0; i <= lastfile; i++, ffp++, nffp++) {
1.192     ad       1459:                KASSERT(i >= NDFDFILE ||
                   1460:                    *nffp == (fdfile_t *)newfdp->fd_dfdfile[i]);
1.173     ad       1461:                ff = *ffp;
1.192     ad       1462:                if (ff == NULL || (fp = ff->ff_file) == NULL) {
                   1463:                        /* Descriptor unused, or descriptor half open. */
                   1464:                        KASSERT(!fd_isused(newfdp, i));
1.173     ad       1465:                        continue;
                   1466:                }
1.192     ad       1467:                if (__predict_false(fp->f_type == DTYPE_KQUEUE)) {
1.173     ad       1468:                        /* kqueue descriptors cannot be copied. */
1.213     rmind    1469:                        if (i < newfdp->fd_freefile) {
                   1470:                                newfdp->fd_freefile = i;
                   1471:                        }
1.126     pk       1472:                        continue;
1.173     ad       1473:                }
                   1474:                /* It's active: add a reference to the file. */
                   1475:                mutex_enter(&fp->f_lock);
                   1476:                fp->f_count++;
                   1477:                mutex_exit(&fp->f_lock);
1.192     ad       1478:
                   1479:                /* Allocate an fdfile_t to represent it. */
1.173     ad       1480:                if (i >= NDFDFILE) {
1.192     ad       1481:                        ff2 = pool_cache_get(fdfile_cache, PR_WAITOK);
                   1482:                        *nffp = ff2;
                   1483:                } else {
                   1484:                        ff2 = newdt->dt_ff[i];
1.173     ad       1485:                }
                   1486:                ff2->ff_file = fp;
                   1487:                ff2->ff_exclose = ff->ff_exclose;
1.182     matt     1488:                ff2->ff_allocated = true;
1.192     ad       1489:
                   1490:                /* Fix up bitmaps. */
                   1491:                j = i >> NDENTRYSHIFT;
1.234   ! kamil    1492:                KASSERT((newfdp->fd_lomap[j] & (1U << (i & NDENTRYMASK))) == 0);
        !          1493:                newfdp->fd_lomap[j] |= 1U << (i & NDENTRYMASK);
1.192     ad       1494:                if (__predict_false(newfdp->fd_lomap[j] == ~0)) {
                   1495:                        KASSERT((newfdp->fd_himap[j >> NDENTRYSHIFT] &
1.234   ! kamil    1496:                            (1U << (j & NDENTRYMASK))) == 0);
1.192     ad       1497:                        newfdp->fd_himap[j >> NDENTRYSHIFT] |=
1.234   ! kamil    1498:                            1U << (j & NDENTRYMASK);
1.173     ad       1499:                }
1.192     ad       1500:                newlast = i;
1.173     ad       1501:        }
1.192     ad       1502:        KASSERT(newdt->dt_ff[0] == (fdfile_t *)newfdp->fd_dfdfile[0]);
                   1503:        newfdp->fd_lastfile = newlast;
                   1504:        fd_checkmaps(newfdp);
1.173     ad       1505:        mutex_exit(&fdp->fd_lock);
1.213     rmind    1506:
                   1507:        return newfdp;
1.16      cgd      1508: }
                   1509:
                   1510: /*
                   1511:  * Release a filedesc structure.
                   1512:  */
                   1513: void
1.173     ad       1514: fd_free(void)
1.16      cgd      1515: {
1.173     ad       1516:        fdfile_t *ff;
                   1517:        file_t *fp;
1.192     ad       1518:        int fd, nf;
                   1519:        fdtab_t *dt;
1.197     yamt     1520:        lwp_t * const l = curlwp;
                   1521:        filedesc_t * const fdp = l->l_fd;
                   1522:        const bool noadvlock = (l->l_proc->p_flag & PK_ADVLOCK) == 0;
1.173     ad       1523:
1.192     ad       1524:        KASSERT(fdp->fd_dt->dt_ff[0] == (fdfile_t *)fdp->fd_dfdfile[0]);
                   1525:        KASSERT(fdp->fd_dtbuiltin.dt_nfiles == NDFILE);
                   1526:        KASSERT(fdp->fd_dtbuiltin.dt_link == NULL);
1.16      cgd      1527:
1.192     ad       1528: #ifndef __HAVE_ATOMIC_AS_MEMBAR
                   1529:        membar_exit();
                   1530: #endif
1.164     ad       1531:        if (atomic_dec_uint_nv(&fdp->fd_refcnt) > 0)
1.16      cgd      1532:                return;
1.126     pk       1533:
1.16      cgd      1534:        /*
1.173     ad       1535:         * Close any files that the process holds open.
1.16      cgd      1536:         */
1.192     ad       1537:        dt = fdp->fd_dt;
                   1538:        fd_checkmaps(fdp);
1.196     yamt     1539: #ifdef DEBUG
                   1540:        fdp->fd_refcnt = -1; /* see fd_checkmaps */
                   1541: #endif
1.192     ad       1542:        for (fd = 0, nf = dt->dt_nfiles; fd < nf; fd++) {
                   1543:                ff = dt->dt_ff[fd];
1.173     ad       1544:                KASSERT(fd >= NDFDFILE ||
                   1545:                    ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
1.192     ad       1546:                if (ff == NULL)
1.173     ad       1547:                        continue;
                   1548:                if ((fp = ff->ff_file) != NULL) {
                   1549:                        /*
1.192     ad       1550:                         * Must use fd_close() here if there is
1.197     yamt     1551:                         * a reference from kqueue or we might have posix
                   1552:                         * advisory locks.
1.173     ad       1553:                         */
1.197     yamt     1554:                        if (__predict_true(ff->ff_refcnt == 0) &&
                   1555:                            (noadvlock || fp->f_type != DTYPE_VNODE)) {
1.192     ad       1556:                                ff->ff_file = NULL;
                   1557:                                ff->ff_exclose = false;
                   1558:                                ff->ff_allocated = false;
                   1559:                                closef(fp);
                   1560:                        } else {
                   1561:                                ff->ff_refcnt++;
                   1562:                                fd_close(fd);
                   1563:                        }
1.173     ad       1564:                }
                   1565:                KASSERT(ff->ff_refcnt == 0);
                   1566:                KASSERT(ff->ff_file == NULL);
                   1567:                KASSERT(!ff->ff_exclose);
                   1568:                KASSERT(!ff->ff_allocated);
                   1569:                if (fd >= NDFDFILE) {
                   1570:                        pool_cache_put(fdfile_cache, ff);
1.192     ad       1571:                        dt->dt_ff[fd] = NULL;
1.173     ad       1572:                }
1.16      cgd      1573:        }
1.59      thorpej  1574:
                   1575:        /*
1.173     ad       1576:         * Clean out the descriptor table for the next user and return
                   1577:         * to the cache.
1.59      thorpej  1578:         */
1.192     ad       1579:        if (__predict_false(dt != &fdp->fd_dtbuiltin)) {
                   1580:                fd_dtab_free(fdp->fd_dt);
                   1581:                /* Otherwise, done above. */
                   1582:                memset(&fdp->fd_dtbuiltin.dt_ff[NDFDFILE], 0,
                   1583:                    (NDFILE - NDFDFILE) * sizeof(fdp->fd_dtbuiltin.dt_ff[0]));
                   1584:                fdp->fd_dt = &fdp->fd_dtbuiltin;
1.59      thorpej  1585:        }
1.192     ad       1586:        if (__predict_false(NDHISLOTS(nf) > NDHISLOTS(NDFILE))) {
1.173     ad       1587:                KASSERT(fdp->fd_himap != fdp->fd_dhimap);
                   1588:                KASSERT(fdp->fd_lomap != fdp->fd_dlomap);
1.192     ad       1589:                fd_map_free(nf, fdp->fd_lomap, fdp->fd_himap);
1.16      cgd      1590:        }
1.192     ad       1591:        if (__predict_false(fdp->fd_knhash != NULL)) {
1.179     ad       1592:                hashdone(fdp->fd_knhash, HASH_LIST, fdp->fd_knhashmask);
1.173     ad       1593:                fdp->fd_knhash = NULL;
                   1594:                fdp->fd_knhashmask = 0;
                   1595:        } else {
                   1596:                KASSERT(fdp->fd_knhashmask == 0);
1.137     yamt     1597:        }
1.192     ad       1598:        fdp->fd_dt = &fdp->fd_dtbuiltin;
1.173     ad       1599:        fdp->fd_lastkqfile = -1;
1.192     ad       1600:        fdp->fd_lastfile = -1;
                   1601:        fdp->fd_freefile = 0;
                   1602:        fdp->fd_exclose = false;
                   1603:        memset(&fdp->fd_startzero, 0, sizeof(*fdp) -
                   1604:            offsetof(filedesc_t, fd_startzero));
1.195     yamt     1605:        fdp->fd_himap = fdp->fd_dhimap;
                   1606:        fdp->fd_lomap = fdp->fd_dlomap;
1.192     ad       1607:        KASSERT(fdp->fd_dtbuiltin.dt_nfiles == NDFILE);
                   1608:        KASSERT(fdp->fd_dtbuiltin.dt_link == NULL);
                   1609:        KASSERT(fdp->fd_dt == &fdp->fd_dtbuiltin);
1.196     yamt     1610: #ifdef DEBUG
                   1611:        fdp->fd_refcnt = 0; /* see fd_checkmaps */
                   1612: #endif
1.192     ad       1613:        fd_checkmaps(fdp);
1.173     ad       1614:        pool_cache_put(filedesc_cache, fdp);
1.170     martin   1615: }
                   1616:
1.16      cgd      1617: /*
                   1618:  * File Descriptor pseudo-device driver (/dev/fd/).
                   1619:  *
                   1620:  * Opening minor device N dup()s the file (if any) connected to file
                   1621:  * descriptor N belonging to the calling process.  Note that this driver
                   1622:  * consists of only the ``open()'' routine, because all subsequent
                   1623:  * references to this file will be direct to the other driver.
                   1624:  */
1.134     thorpej  1625: static int
1.173     ad       1626: filedescopen(dev_t dev, int mode, int type, lwp_t *l)
1.16      cgd      1627: {
                   1628:
1.28      mycroft  1629:        /*
1.112     jdolecek 1630:         * XXX Kludge: set dupfd to contain the value of the
1.89      enami    1631:         * the file descriptor being sought for duplication. The error
1.28      mycroft  1632:         * return ensures that the vnode for this device will be released
                   1633:         * by vn_open. Open will detect this special error and take the
1.208     yamt     1634:         * actions in fd_dupopen below. Other callers of vn_open or VOP_OPEN
1.28      mycroft  1635:         * will simply report the error.
                   1636:         */
1.138     christos 1637:        l->l_dupfd = minor(dev);        /* XXX */
1.127     christos 1638:        return EDUPFD;
1.27      mycroft  1639: }
                   1640:
1.28      mycroft  1641: /*
                   1642:  * Duplicate the specified descriptor to a free descriptor.
                   1643:  */
1.27      mycroft  1644: int
1.226     matt     1645: fd_dupopen(int old, int *newp, int mode, int error)
1.72      lukem    1646: {
1.173     ad       1647:        filedesc_t *fdp;
                   1648:        fdfile_t *ff;
                   1649:        file_t *fp;
1.192     ad       1650:        fdtab_t *dt;
1.27      mycroft  1651:
1.173     ad       1652:        if ((fp = fd_getfile(old)) == NULL) {
                   1653:                return EBADF;
                   1654:        }
                   1655:        fdp = curlwp->l_fd;
1.192     ad       1656:        dt = fdp->fd_dt;
                   1657:        ff = dt->dt_ff[old];
1.59      thorpej  1658:
1.27      mycroft  1659:        /*
1.28      mycroft  1660:         * There are two cases of interest here.
                   1661:         *
1.208     yamt     1662:         * For EDUPFD simply dup (old) to file descriptor
                   1663:         * (new) and return.
1.28      mycroft  1664:         *
1.208     yamt     1665:         * For EMOVEFD steal away the file structure from (old) and
                   1666:         * store it in (new).  (old) is effectively closed by
1.28      mycroft  1667:         * this operation.
                   1668:         *
                   1669:         * Any other error code is just returned.
1.27      mycroft  1670:         */
1.28      mycroft  1671:        switch (error) {
1.127     christos 1672:        case EDUPFD:
1.28      mycroft  1673:                /*
                   1674:                 * Check that the mode the file is being opened for is a
                   1675:                 * subset of the mode of the existing descriptor.
                   1676:                 */
1.173     ad       1677:                if (((mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
                   1678:                        error = EACCES;
                   1679:                        break;
                   1680:                }
                   1681:
                   1682:                /* Copy it. */
1.226     matt     1683:                error = fd_dup(fp, 0, newp, ff->ff_exclose);
1.173     ad       1684:                break;
1.27      mycroft  1685:
1.127     christos 1686:        case EMOVEFD:
1.173     ad       1687:                /* Copy it. */
1.226     matt     1688:                error = fd_dup(fp, 0, newp, ff->ff_exclose);
1.173     ad       1689:                if (error != 0) {
                   1690:                        break;
                   1691:                }
1.16      cgd      1692:
1.173     ad       1693:                /* Steal away the file pointer from 'old'. */
                   1694:                (void)fd_close(old);
                   1695:                return 0;
1.28      mycroft  1696:        }
1.173     ad       1697:
                   1698:        fd_putfile(old);
                   1699:        return error;
1.61      wrstuden 1700: }
                   1701:
                   1702: /*
1.211     pooka    1703:  * Close open files on exec.
                   1704:  */
                   1705: void
                   1706: fd_closeexec(void)
                   1707: {
                   1708:        proc_t *p;
                   1709:        filedesc_t *fdp;
                   1710:        fdfile_t *ff;
                   1711:        lwp_t *l;
                   1712:        fdtab_t *dt;
                   1713:        int fd;
                   1714:
                   1715:        l = curlwp;
                   1716:        p = l->l_proc;
                   1717:        fdp = p->p_fd;
                   1718:
                   1719:        if (fdp->fd_refcnt > 1) {
                   1720:                fdp = fd_copy();
                   1721:                fd_free();
                   1722:                p->p_fd = fdp;
                   1723:                l->l_fd = fdp;
                   1724:        }
                   1725:        if (!fdp->fd_exclose) {
                   1726:                return;
                   1727:        }
                   1728:        fdp->fd_exclose = false;
                   1729:        dt = fdp->fd_dt;
                   1730:
                   1731:        for (fd = 0; fd <= fdp->fd_lastfile; fd++) {
                   1732:                if ((ff = dt->dt_ff[fd]) == NULL) {
                   1733:                        KASSERT(fd >= NDFDFILE);
                   1734:                        continue;
                   1735:                }
                   1736:                KASSERT(fd >= NDFDFILE ||
                   1737:                    ff == (fdfile_t *)fdp->fd_dfdfile[fd]);
                   1738:                if (ff->ff_file == NULL)
                   1739:                        continue;
                   1740:                if (ff->ff_exclose) {
                   1741:                        /*
                   1742:                         * We need a reference to close the file.
                   1743:                         * No other threads can see the fdfile_t at
                   1744:                         * this point, so don't bother locking.
                   1745:                         */
                   1746:                        KASSERT((ff->ff_refcnt & FR_CLOSING) == 0);
                   1747:                        ff->ff_refcnt++;
                   1748:                        fd_close(fd);
                   1749:                }
                   1750:        }
                   1751: }
                   1752:
                   1753: /*
1.113     jdolecek 1754:  * Sets descriptor owner. If the owner is a process, 'pgid'
                   1755:  * is set to positive value, process ID. If the owner is process group,
                   1756:  * 'pgid' is set to -pg_id.
                   1757:  */
                   1758: int
1.180     gmcgarry 1759: fsetown(pid_t *pgid, u_long cmd, const void *data)
1.113     jdolecek 1760: {
1.203     rmind    1761:        pid_t id = *(const pid_t *)data;
1.113     jdolecek 1762:        int error;
                   1763:
                   1764:        switch (cmd) {
                   1765:        case TIOCSPGRP:
                   1766:                if (id < 0)
1.203     rmind    1767:                        return EINVAL;
1.113     jdolecek 1768:                id = -id;
                   1769:                break;
                   1770:        default:
                   1771:                break;
                   1772:        }
1.203     rmind    1773:        if (id > 0) {
                   1774:                mutex_enter(proc_lock);
                   1775:                error = proc_find(id) ? 0 : ESRCH;
                   1776:                mutex_exit(proc_lock);
                   1777:        } else if (id < 0) {
                   1778:                error = pgid_in_session(curproc, -id);
                   1779:        } else {
                   1780:                error = 0;
                   1781:        }
                   1782:        if (!error) {
                   1783:                *pgid = id;
                   1784:        }
                   1785:        return error;
1.113     jdolecek 1786: }
                   1787:
1.212     christos 1788: void
                   1789: fd_set_exclose(struct lwp *l, int fd, bool exclose)
                   1790: {
                   1791:        filedesc_t *fdp = l->l_fd;
                   1792:        fdfile_t *ff = fdp->fd_dt->dt_ff[fd];
1.213     rmind    1793:
1.212     christos 1794:        ff->ff_exclose = exclose;
                   1795:        if (exclose)
                   1796:                fdp->fd_exclose = true;
                   1797: }
                   1798:
1.113     jdolecek 1799: /*
                   1800:  * Return descriptor owner information. If the value is positive,
                   1801:  * it's process ID. If it's negative, it's process group ID and
                   1802:  * needs the sign removed before use.
                   1803:  */
                   1804: int
1.180     gmcgarry 1805: fgetown(pid_t pgid, u_long cmd, void *data)
1.113     jdolecek 1806: {
1.173     ad       1807:
1.113     jdolecek 1808:        switch (cmd) {
                   1809:        case TIOCGPGRP:
                   1810:                *(int *)data = -pgid;
                   1811:                break;
                   1812:        default:
                   1813:                *(int *)data = pgid;
                   1814:                break;
                   1815:        }
1.213     rmind    1816:        return 0;
1.113     jdolecek 1817: }
                   1818:
                   1819: /*
                   1820:  * Send signal to descriptor owner, either process or process group.
                   1821:  */
                   1822: void
1.114     christos 1823: fownsignal(pid_t pgid, int signo, int code, int band, void *fdescdata)
1.113     jdolecek 1824: {
1.131     perry    1825:        ksiginfo_t ksi;
1.113     jdolecek 1826:
1.176     ad       1827:        KASSERT(!cpu_intr_p());
                   1828:
1.189     rmind    1829:        if (pgid == 0) {
                   1830:                return;
                   1831:        }
                   1832:
1.148     yamt     1833:        KSI_INIT(&ksi);
1.114     christos 1834:        ksi.ksi_signo = signo;
1.113     jdolecek 1835:        ksi.ksi_code = code;
                   1836:        ksi.ksi_band = band;
                   1837:
1.176     ad       1838:        mutex_enter(proc_lock);
1.189     rmind    1839:        if (pgid > 0) {
                   1840:                struct proc *p1;
                   1841:
1.203     rmind    1842:                p1 = proc_find(pgid);
1.189     rmind    1843:                if (p1 != NULL) {
                   1844:                        kpsignal(p1, &ksi, fdescdata);
                   1845:                }
                   1846:        } else {
                   1847:                struct pgrp *pgrp;
                   1848:
                   1849:                KASSERT(pgid < 0);
1.203     rmind    1850:                pgrp = pgrp_find(-pgid);
1.189     rmind    1851:                if (pgrp != NULL) {
                   1852:                        kpgsignal(pgrp, &ksi, fdescdata, 0);
                   1853:                }
                   1854:        }
1.176     ad       1855:        mutex_exit(proc_lock);
1.113     jdolecek 1856: }
1.127     christos 1857:
                   1858: int
1.173     ad       1859: fd_clone(file_t *fp, unsigned fd, int flag, const struct fileops *fops,
                   1860:         void *data)
1.127     christos 1861: {
1.229     christos 1862:        fdfile_t *ff;
                   1863:        filedesc_t *fdp;
1.173     ad       1864:
1.230     nat      1865:        fp->f_flag = flag & FMASK;
1.229     christos 1866:        fdp = curproc->p_fd;
                   1867:        ff = fdp->fd_dt->dt_ff[fd];
                   1868:        KASSERT(ff != NULL);
                   1869:        ff->ff_exclose = (flag & O_CLOEXEC) != 0;
1.127     christos 1870:        fp->f_type = DTYPE_MISC;
                   1871:        fp->f_ops = fops;
                   1872:        fp->f_data = data;
1.173     ad       1873:        curlwp->l_dupfd = fd;
                   1874:        fd_affix(curproc, fp, fd);
1.127     christos 1875:
                   1876:        return EMOVEFD;
                   1877: }
                   1878:
                   1879: int
1.173     ad       1880: fnullop_fcntl(file_t *fp, u_int cmd, void *data)
1.127     christos 1881: {
1.147     yamt     1882:
1.127     christos 1883:        if (cmd == F_SETFL)
                   1884:                return 0;
                   1885:
                   1886:        return EOPNOTSUPP;
                   1887: }
                   1888:
                   1889: int
1.173     ad       1890: fnullop_poll(file_t *fp, int which)
1.127     christos 1891: {
1.147     yamt     1892:
1.127     christos 1893:        return 0;
                   1894: }
                   1895:
                   1896: int
1.173     ad       1897: fnullop_kqfilter(file_t *fp, struct knote *kn)
1.127     christos 1898: {
                   1899:
1.219     christos 1900:        return EOPNOTSUPP;
1.127     christos 1901: }
                   1902:
1.190     ad       1903: void
1.202     dsl      1904: fnullop_restart(file_t *fp)
1.190     ad       1905: {
                   1906:
                   1907: }
                   1908:
1.127     christos 1909: int
1.173     ad       1910: fbadop_read(file_t *fp, off_t *offset, struct uio *uio,
                   1911:            kauth_cred_t cred, int flags)
1.160     rmind    1912: {
                   1913:
                   1914:        return EOPNOTSUPP;
                   1915: }
                   1916:
                   1917: int
1.173     ad       1918: fbadop_write(file_t *fp, off_t *offset, struct uio *uio,
                   1919:             kauth_cred_t cred, int flags)
1.160     rmind    1920: {
                   1921:
                   1922:        return EOPNOTSUPP;
                   1923: }
                   1924:
                   1925: int
1.173     ad       1926: fbadop_ioctl(file_t *fp, u_long com, void *data)
1.160     rmind    1927: {
                   1928:
                   1929:        return EOPNOTSUPP;
                   1930: }
                   1931:
                   1932: int
1.173     ad       1933: fbadop_stat(file_t *fp, struct stat *sb)
1.127     christos 1934: {
1.147     yamt     1935:
1.127     christos 1936:        return EOPNOTSUPP;
                   1937: }
1.160     rmind    1938:
                   1939: int
1.173     ad       1940: fbadop_close(file_t *fp)
1.160     rmind    1941: {
                   1942:
                   1943:        return EOPNOTSUPP;
                   1944: }
1.210     pooka    1945:
                   1946: /*
                   1947:  * sysctl routines pertaining to file descriptors
                   1948:  */
                   1949:
                   1950: /* Initialized in sysctl_init() for now... */
                   1951: extern kmutex_t sysctl_file_marker_lock;
                   1952: static u_int sysctl_file_marker = 1;
                   1953:
                   1954: /*
                   1955:  * Expects to be called with proc_lock and sysctl_file_marker_lock locked.
                   1956:  */
                   1957: static void
                   1958: sysctl_file_marker_reset(void)
                   1959: {
                   1960:        struct proc *p;
                   1961:
                   1962:        PROCLIST_FOREACH(p, &allproc) {
                   1963:                struct filedesc *fd = p->p_fd;
                   1964:                fdtab_t *dt;
                   1965:                u_int i;
                   1966:
                   1967:                mutex_enter(&fd->fd_lock);
                   1968:                dt = fd->fd_dt;
                   1969:                for (i = 0; i < dt->dt_nfiles; i++) {
                   1970:                        struct file *fp;
                   1971:                        fdfile_t *ff;
                   1972:
                   1973:                        if ((ff = dt->dt_ff[i]) == NULL) {
                   1974:                                continue;
                   1975:                        }
                   1976:                        if ((fp = ff->ff_file) == NULL) {
                   1977:                                continue;
                   1978:                        }
                   1979:                        fp->f_marker = 0;
                   1980:                }
                   1981:                mutex_exit(&fd->fd_lock);
                   1982:        }
                   1983: }
                   1984:
                   1985: /*
                   1986:  * sysctl helper routine for kern.file pseudo-subtree.
                   1987:  */
                   1988: static int
                   1989: sysctl_kern_file(SYSCTLFN_ARGS)
                   1990: {
                   1991:        int error;
                   1992:        size_t buflen;
                   1993:        struct file *fp, fbuf;
                   1994:        char *start, *where;
                   1995:        struct proc *p;
                   1996:
                   1997:        start = where = oldp;
                   1998:        buflen = *oldlenp;
                   1999:
                   2000:        if (where == NULL) {
                   2001:                /*
                   2002:                 * overestimate by 10 files
                   2003:                 */
                   2004:                *oldlenp = sizeof(filehead) + (nfiles + 10) *
                   2005:                    sizeof(struct file);
1.213     rmind    2006:                return 0;
1.210     pooka    2007:        }
                   2008:
                   2009:        /*
                   2010:         * first sysctl_copyout filehead
                   2011:         */
                   2012:        if (buflen < sizeof(filehead)) {
                   2013:                *oldlenp = 0;
1.213     rmind    2014:                return 0;
1.210     pooka    2015:        }
                   2016:        sysctl_unlock();
                   2017:        error = sysctl_copyout(l, &filehead, where, sizeof(filehead));
                   2018:        if (error) {
1.213     rmind    2019:                sysctl_relock();
1.210     pooka    2020:                return error;
                   2021:        }
                   2022:        buflen -= sizeof(filehead);
                   2023:        where += sizeof(filehead);
                   2024:
                   2025:        /*
                   2026:         * followed by an array of file structures
                   2027:         */
                   2028:        mutex_enter(&sysctl_file_marker_lock);
                   2029:        mutex_enter(proc_lock);
                   2030:        PROCLIST_FOREACH(p, &allproc) {
                   2031:                struct filedesc *fd;
                   2032:                fdtab_t *dt;
                   2033:                u_int i;
                   2034:
                   2035:                if (p->p_stat == SIDL) {
                   2036:                        /* skip embryonic processes */
                   2037:                        continue;
                   2038:                }
                   2039:                mutex_enter(p->p_lock);
                   2040:                error = kauth_authorize_process(l->l_cred,
                   2041:                    KAUTH_PROCESS_CANSEE, p,
                   2042:                    KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES),
                   2043:                    NULL, NULL);
                   2044:                mutex_exit(p->p_lock);
                   2045:                if (error != 0) {
                   2046:                        /*
                   2047:                         * Don't leak kauth retval if we're silently
                   2048:                         * skipping this entry.
                   2049:                         */
                   2050:                        error = 0;
                   2051:                        continue;
                   2052:                }
                   2053:
                   2054:                /*
                   2055:                 * Grab a hold on the process.
                   2056:                 */
                   2057:                if (!rw_tryenter(&p->p_reflock, RW_READER)) {
                   2058:                        continue;
                   2059:                }
                   2060:                mutex_exit(proc_lock);
                   2061:
                   2062:                fd = p->p_fd;
                   2063:                mutex_enter(&fd->fd_lock);
                   2064:                dt = fd->fd_dt;
                   2065:                for (i = 0; i < dt->dt_nfiles; i++) {
                   2066:                        fdfile_t *ff;
                   2067:
                   2068:                        if ((ff = dt->dt_ff[i]) == NULL) {
                   2069:                                continue;
                   2070:                        }
                   2071:                        if ((fp = ff->ff_file) == NULL) {
                   2072:                                continue;
                   2073:                        }
                   2074:
                   2075:                        mutex_enter(&fp->f_lock);
                   2076:
                   2077:                        if ((fp->f_count == 0) ||
                   2078:                            (fp->f_marker == sysctl_file_marker)) {
                   2079:                                mutex_exit(&fp->f_lock);
                   2080:                                continue;
                   2081:                        }
                   2082:
                   2083:                        /* Check that we have enough space. */
                   2084:                        if (buflen < sizeof(struct file)) {
                   2085:                                *oldlenp = where - start;
1.213     rmind    2086:                                mutex_exit(&fp->f_lock);
1.210     pooka    2087:                                error = ENOMEM;
                   2088:                                break;
                   2089:                        }
                   2090:
                   2091:                        memcpy(&fbuf, fp, sizeof(fbuf));
                   2092:                        mutex_exit(&fp->f_lock);
                   2093:                        error = sysctl_copyout(l, &fbuf, where, sizeof(fbuf));
                   2094:                        if (error) {
                   2095:                                break;
                   2096:                        }
                   2097:                        buflen -= sizeof(struct file);
                   2098:                        where += sizeof(struct file);
                   2099:
                   2100:                        fp->f_marker = sysctl_file_marker;
                   2101:                }
                   2102:                mutex_exit(&fd->fd_lock);
                   2103:
                   2104:                /*
                   2105:                 * Release reference to process.
                   2106:                 */
                   2107:                mutex_enter(proc_lock);
                   2108:                rw_exit(&p->p_reflock);
                   2109:
                   2110:                if (error)
                   2111:                        break;
                   2112:        }
                   2113:
                   2114:        sysctl_file_marker++;
                   2115:        /* Reset all markers if wrapped. */
                   2116:        if (sysctl_file_marker == 0) {
                   2117:                sysctl_file_marker_reset();
                   2118:                sysctl_file_marker++;
                   2119:        }
                   2120:
                   2121:        mutex_exit(proc_lock);
                   2122:        mutex_exit(&sysctl_file_marker_lock);
                   2123:
                   2124:        *oldlenp = where - start;
1.213     rmind    2125:        sysctl_relock();
                   2126:        return error;
1.210     pooka    2127: }
                   2128:
                   2129: /*
                   2130:  * sysctl helper function for kern.file2
                   2131:  */
                   2132: static int
                   2133: sysctl_kern_file2(SYSCTLFN_ARGS)
                   2134: {
                   2135:        struct proc *p;
                   2136:        struct file *fp;
                   2137:        struct filedesc *fd;
                   2138:        struct kinfo_file kf;
                   2139:        char *dp;
                   2140:        u_int i, op;
                   2141:        size_t len, needed, elem_size, out_size;
                   2142:        int error, arg, elem_count;
                   2143:        fdfile_t *ff;
                   2144:        fdtab_t *dt;
                   2145:
                   2146:        if (namelen == 1 && name[0] == CTL_QUERY)
1.213     rmind    2147:                return sysctl_query(SYSCTLFN_CALL(rnode));
1.210     pooka    2148:
                   2149:        if (namelen != 4)
1.213     rmind    2150:                return EINVAL;
1.210     pooka    2151:
                   2152:        error = 0;
                   2153:        dp = oldp;
                   2154:        len = (oldp != NULL) ? *oldlenp : 0;
                   2155:        op = name[0];
                   2156:        arg = name[1];
                   2157:        elem_size = name[2];
                   2158:        elem_count = name[3];
                   2159:        out_size = MIN(sizeof(kf), elem_size);
                   2160:        needed = 0;
                   2161:
                   2162:        if (elem_size < 1 || elem_count < 0)
1.213     rmind    2163:                return EINVAL;
1.210     pooka    2164:
                   2165:        switch (op) {
                   2166:        case KERN_FILE_BYFILE:
                   2167:        case KERN_FILE_BYPID:
                   2168:                /*
                   2169:                 * We're traversing the process list in both cases; the BYFILE
                   2170:                 * case does additional work of keeping track of files already
                   2171:                 * looked at.
                   2172:                 */
                   2173:
                   2174:                /* doesn't use arg so it must be zero */
                   2175:                if ((op == KERN_FILE_BYFILE) && (arg != 0))
                   2176:                        return EINVAL;
                   2177:
                   2178:                if ((op == KERN_FILE_BYPID) && (arg < -1))
                   2179:                        /* -1 means all processes */
1.213     rmind    2180:                        return EINVAL;
1.210     pooka    2181:
                   2182:                sysctl_unlock();
                   2183:                if (op == KERN_FILE_BYFILE)
                   2184:                        mutex_enter(&sysctl_file_marker_lock);
                   2185:                mutex_enter(proc_lock);
                   2186:                PROCLIST_FOREACH(p, &allproc) {
                   2187:                        if (p->p_stat == SIDL) {
                   2188:                                /* skip embryonic processes */
                   2189:                                continue;
                   2190:                        }
                   2191:                        if (arg > 0 && p->p_pid != arg) {
                   2192:                                /* pick only the one we want */
                   2193:                                /* XXX want 0 to mean "kernel files" */
                   2194:                                continue;
                   2195:                        }
                   2196:                        mutex_enter(p->p_lock);
                   2197:                        error = kauth_authorize_process(l->l_cred,
                   2198:                            KAUTH_PROCESS_CANSEE, p,
                   2199:                            KAUTH_ARG(KAUTH_REQ_PROCESS_CANSEE_OPENFILES),
                   2200:                            NULL, NULL);
                   2201:                        mutex_exit(p->p_lock);
                   2202:                        if (error != 0) {
                   2203:                                /*
                   2204:                                 * Don't leak kauth retval if we're silently
                   2205:                                 * skipping this entry.
                   2206:                                 */
                   2207:                                error = 0;
                   2208:                                continue;
                   2209:                        }
                   2210:
                   2211:                        /*
                   2212:                         * Grab a hold on the process.
                   2213:                         */
                   2214:                        if (!rw_tryenter(&p->p_reflock, RW_READER)) {
                   2215:                                continue;
                   2216:                        }
                   2217:                        mutex_exit(proc_lock);
                   2218:
                   2219:                        fd = p->p_fd;
                   2220:                        mutex_enter(&fd->fd_lock);
                   2221:                        dt = fd->fd_dt;
                   2222:                        for (i = 0; i < dt->dt_nfiles; i++) {
                   2223:                                if ((ff = dt->dt_ff[i]) == NULL) {
                   2224:                                        continue;
                   2225:                                }
                   2226:                                if ((fp = ff->ff_file) == NULL) {
                   2227:                                        continue;
                   2228:                                }
                   2229:
                   2230:                                if ((op == KERN_FILE_BYFILE) &&
                   2231:                                    (fp->f_marker == sysctl_file_marker)) {
                   2232:                                        continue;
                   2233:                                }
                   2234:                                if (len >= elem_size && elem_count > 0) {
                   2235:                                        mutex_enter(&fp->f_lock);
                   2236:                                        fill_file(&kf, fp, ff, i, p->p_pid);
                   2237:                                        mutex_exit(&fp->f_lock);
                   2238:                                        mutex_exit(&fd->fd_lock);
                   2239:                                        error = sysctl_copyout(l,
                   2240:                                            &kf, dp, out_size);
                   2241:                                        mutex_enter(&fd->fd_lock);
                   2242:                                        if (error)
                   2243:                                                break;
                   2244:                                        dp += elem_size;
                   2245:                                        len -= elem_size;
                   2246:                                }
                   2247:                                if (op == KERN_FILE_BYFILE)
                   2248:                                        fp->f_marker = sysctl_file_marker;
                   2249:                                needed += elem_size;
                   2250:                                if (elem_count > 0 && elem_count != INT_MAX)
                   2251:                                        elem_count--;
                   2252:                        }
                   2253:                        mutex_exit(&fd->fd_lock);
                   2254:
                   2255:                        /*
                   2256:                         * Release reference to process.
                   2257:                         */
                   2258:                        mutex_enter(proc_lock);
                   2259:                        rw_exit(&p->p_reflock);
                   2260:                }
                   2261:                if (op == KERN_FILE_BYFILE) {
                   2262:                        sysctl_file_marker++;
                   2263:
                   2264:                        /* Reset all markers if wrapped. */
                   2265:                        if (sysctl_file_marker == 0) {
                   2266:                                sysctl_file_marker_reset();
                   2267:                                sysctl_file_marker++;
                   2268:                        }
                   2269:                }
                   2270:                mutex_exit(proc_lock);
                   2271:                if (op == KERN_FILE_BYFILE)
                   2272:                        mutex_exit(&sysctl_file_marker_lock);
                   2273:                sysctl_relock();
                   2274:                break;
                   2275:        default:
1.213     rmind    2276:                return EINVAL;
1.210     pooka    2277:        }
                   2278:
                   2279:        if (oldp == NULL)
                   2280:                needed += KERN_FILESLOP * elem_size;
                   2281:        *oldlenp = needed;
                   2282:
1.213     rmind    2283:        return error;
1.210     pooka    2284: }
                   2285:
                   2286: static void
                   2287: fill_file(struct kinfo_file *kp, const file_t *fp, const fdfile_t *ff,
                   2288:          int i, pid_t pid)
                   2289: {
                   2290:
                   2291:        memset(kp, 0, sizeof(*kp));
                   2292:
                   2293:        kp->ki_fileaddr =       PTRTOUINT64(fp);
                   2294:        kp->ki_flag =           fp->f_flag;
                   2295:        kp->ki_iflags =         0;
                   2296:        kp->ki_ftype =          fp->f_type;
                   2297:        kp->ki_count =          fp->f_count;
                   2298:        kp->ki_msgcount =       fp->f_msgcount;
                   2299:        kp->ki_fucred =         PTRTOUINT64(fp->f_cred);
                   2300:        kp->ki_fuid =           kauth_cred_geteuid(fp->f_cred);
                   2301:        kp->ki_fgid =           kauth_cred_getegid(fp->f_cred);
                   2302:        kp->ki_fops =           PTRTOUINT64(fp->f_ops);
                   2303:        kp->ki_foffset =        fp->f_offset;
                   2304:        kp->ki_fdata =          PTRTOUINT64(fp->f_data);
                   2305:
                   2306:        /* vnode information to glue this file to something */
                   2307:        if (fp->f_type == DTYPE_VNODE) {
1.227     matt     2308:                struct vnode *vp = fp->f_vnode;
1.210     pooka    2309:
                   2310:                kp->ki_vun =    PTRTOUINT64(vp->v_un.vu_socket);
                   2311:                kp->ki_vsize =  vp->v_size;
                   2312:                kp->ki_vtype =  vp->v_type;
                   2313:                kp->ki_vtag =   vp->v_tag;
                   2314:                kp->ki_vdata =  PTRTOUINT64(vp->v_data);
                   2315:        }
                   2316:
                   2317:        /* process information when retrieved via KERN_FILE_BYPID */
                   2318:        if (ff != NULL) {
                   2319:                kp->ki_pid =            pid;
                   2320:                kp->ki_fd =             i;
                   2321:                kp->ki_ofileflags =     ff->ff_exclose;
                   2322:                kp->ki_usecount =       ff->ff_refcnt;
                   2323:        }
                   2324: }

CVSweb <webmaster@jp.NetBSD.org>