The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_descrip.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  * (c) UNIX System Laboratories, Inc.
    5  * All or some portions of this file are derived from material licensed
    6  * to the University of California by American Telephone and Telegraph
    7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
    8  * the permission of UNIX System Laboratories, Inc.
    9  *
   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  * 4. Neither the name of the University nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  *      @(#)kern_descrip.c      8.6 (Berkeley) 4/19/94
   35  */
   36 
   37 #include <sys/cdefs.h>
   38 __FBSDID("$FreeBSD: releng/6.1/sys/kern/kern_descrip.c 176272 2008-02-14 11:47:39Z simon $");
   39 
   40 #include "opt_compat.h"
   41 #include "opt_ddb.h"
   42 
   43 #include <sys/param.h>
   44 #include <sys/systm.h>
   45 
   46 #include <sys/conf.h>
   47 #include <sys/fcntl.h>
   48 #include <sys/file.h>
   49 #include <sys/filedesc.h>
   50 #include <sys/filio.h>
   51 #include <sys/jail.h>
   52 #include <sys/kernel.h>
   53 #include <sys/limits.h>
   54 #include <sys/lock.h>
   55 #include <sys/malloc.h>
   56 #include <sys/mount.h>
   57 #include <sys/mutex.h>
   58 #include <sys/namei.h>
   59 #include <sys/proc.h>
   60 #include <sys/resourcevar.h>
   61 #include <sys/signalvar.h>
   62 #include <sys/socketvar.h>
   63 #include <sys/stat.h>
   64 #include <sys/sx.h>
   65 #include <sys/syscallsubr.h>
   66 #include <sys/sysctl.h>
   67 #include <sys/sysproto.h>
   68 #include <sys/unistd.h>
   69 #include <sys/vnode.h>
   70 
   71 #include <vm/uma.h>
   72 
   73 #include <ddb/ddb.h>
   74 
   75 static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
   76 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "file desc to leader",
   77                      "file desc to leader structures");
   78 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
   79 
   80 static uma_zone_t file_zone;
   81 
   82 
   83 /* How to treat 'new' parameter when allocating a fd for do_dup(). */
   84 enum dup_type { DUP_VARIABLE, DUP_FIXED };
   85 
   86 static int do_dup(struct thread *td, enum dup_type type, int old, int new,
   87     register_t *retval);
   88 static int      fd_first_free(struct filedesc *, int, int);
   89 static int      fd_last_used(struct filedesc *, int, int);
   90 static void     fdgrowtable(struct filedesc *, int);
   91 static int      fdrop_locked(struct file *fp, struct thread *td);
   92 static void     fdunused(struct filedesc *fdp, int fd);
   93 static void     fdused(struct filedesc *fdp, int fd);
   94 
   95 /*
   96  * A process is initially started out with NDFILE descriptors stored within
   97  * this structure, selected to be enough for typical applications based on
   98  * the historical limit of 20 open files (and the usage of descriptors by
   99  * shells).  If these descriptors are exhausted, a larger descriptor table
  100  * may be allocated, up to a process' resource limit; the internal arrays
  101  * are then unused.
  102  */
  103 #define NDFILE          20
  104 #define NDSLOTSIZE      sizeof(NDSLOTTYPE)
  105 #define NDENTRIES       (NDSLOTSIZE * __CHAR_BIT)
  106 #define NDSLOT(x)       ((x) / NDENTRIES)
  107 #define NDBIT(x)        ((NDSLOTTYPE)1 << ((x) % NDENTRIES))
  108 #define NDSLOTS(x)      (((x) + NDENTRIES - 1) / NDENTRIES)
  109 
  110 /*
  111  * Storage required per open file descriptor.
  112  */
  113 #define OFILESIZE (sizeof(struct file *) + sizeof(char))
  114 
  115 /*
  116  * Basic allocation of descriptors:
  117  * one of the above, plus arrays for NDFILE descriptors.
  118  */
  119 struct filedesc0 {
  120         struct  filedesc fd_fd;
  121         /*
  122          * These arrays are used when the number of open files is
  123          * <= NDFILE, and are then pointed to by the pointers above.
  124          */
  125         struct  file *fd_dfiles[NDFILE];
  126         char    fd_dfileflags[NDFILE];
  127         NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
  128 };
  129 
  130 /*
  131  * Descriptor management.
  132  */
  133 struct filelist filehead;       /* head of list of open files */
  134 int openfiles;                  /* actual number of open files */
  135 struct sx filelist_lock;        /* sx to protect filelist */
  136 struct mtx sigio_lock;          /* mtx to protect pointers to sigio */
  137 
  138 /* A mutex to protect the association between a proc and filedesc. */
  139 static struct mtx       fdesc_mtx;
  140 
  141 /*
  142  * Find the first zero bit in the given bitmap, starting at low and not
  143  * exceeding size - 1.
  144  */
  145 static int
  146 fd_first_free(struct filedesc *fdp, int low, int size)
  147 {
  148         NDSLOTTYPE *map = fdp->fd_map;
  149         NDSLOTTYPE mask;
  150         int off, maxoff;
  151 
  152         if (low >= size)
  153                 return (low);
  154 
  155         off = NDSLOT(low);
  156         if (low % NDENTRIES) {
  157                 mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
  158                 if ((mask &= ~map[off]) != 0UL)
  159                         return (off * NDENTRIES + ffsl(mask) - 1);
  160                 ++off;
  161         }
  162         for (maxoff = NDSLOTS(size); off < maxoff; ++off)
  163                 if (map[off] != ~0UL)
  164                         return (off * NDENTRIES + ffsl(~map[off]) - 1);
  165         return (size);
  166 }
  167 
  168 /*
  169  * Find the highest non-zero bit in the given bitmap, starting at low and
  170  * not exceeding size - 1.
  171  */
  172 static int
  173 fd_last_used(struct filedesc *fdp, int low, int size)
  174 {
  175         NDSLOTTYPE *map = fdp->fd_map;
  176         NDSLOTTYPE mask;
  177         int off, minoff;
  178 
  179         if (low >= size)
  180                 return (-1);
  181 
  182         off = NDSLOT(size);
  183         if (size % NDENTRIES) {
  184                 mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
  185                 if ((mask &= map[off]) != 0)
  186                         return (off * NDENTRIES + flsl(mask) - 1);
  187                 --off;
  188         }
  189         for (minoff = NDSLOT(low); off >= minoff; --off)
  190                 if (map[off] != 0)
  191                         return (off * NDENTRIES + flsl(map[off]) - 1);
  192         return (low - 1);
  193 }
  194 
  195 static int
  196 fdisused(struct filedesc *fdp, int fd)
  197 {
  198         KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
  199             ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
  200         return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
  201 }
  202 
  203 /*
  204  * Mark a file descriptor as used.
  205  */
  206 static void
  207 fdused(struct filedesc *fdp, int fd)
  208 {
  209         FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
  210         KASSERT(!fdisused(fdp, fd),
  211             ("fd already used"));
  212         fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd);
  213         if (fd > fdp->fd_lastfile)
  214                 fdp->fd_lastfile = fd;
  215         if (fd == fdp->fd_freefile)
  216                 fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
  217 }
  218 
  219 /*
  220  * Mark a file descriptor as unused.
  221  */
  222 static void
  223 fdunused(struct filedesc *fdp, int fd)
  224 {
  225         FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
  226         KASSERT(fdisused(fdp, fd),
  227             ("fd is already unused"));
  228         KASSERT(fdp->fd_ofiles[fd] == NULL,
  229             ("fd is still in use"));
  230         fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd);
  231         if (fd < fdp->fd_freefile)
  232                 fdp->fd_freefile = fd;
  233         if (fd == fdp->fd_lastfile)
  234                 fdp->fd_lastfile = fd_last_used(fdp, 0, fd);
  235 }
  236 
  237 /*
  238  * System calls on descriptors.
  239  */
  240 #ifndef _SYS_SYSPROTO_H_
  241 struct getdtablesize_args {
  242         int     dummy;
  243 };
  244 #endif
  245 /*
  246  * MPSAFE
  247  */
  248 /* ARGSUSED */
  249 int
  250 getdtablesize(struct thread *td, struct getdtablesize_args *uap)
  251 {
  252         struct proc *p = td->td_proc;
  253 
  254         PROC_LOCK(p);
  255         td->td_retval[0] =
  256             min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
  257         PROC_UNLOCK(p);
  258         return (0);
  259 }
  260 
  261 /*
  262  * Duplicate a file descriptor to a particular value.
  263  *
  264  * note: keep in mind that a potential race condition exists when closing
  265  * descriptors from a shared descriptor table (via rfork).
  266  */
  267 #ifndef _SYS_SYSPROTO_H_
  268 struct dup2_args {
  269         u_int   from;
  270         u_int   to;
  271 };
  272 #endif
  273 /*
  274  * MPSAFE
  275  */
  276 /* ARGSUSED */
  277 int
  278 dup2(struct thread *td, struct dup2_args *uap)
  279 {
  280 
  281         return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
  282                     td->td_retval));
  283 }
  284 
  285 /*
  286  * Duplicate a file descriptor.
  287  */
  288 #ifndef _SYS_SYSPROTO_H_
  289 struct dup_args {
  290         u_int   fd;
  291 };
  292 #endif
  293 /*
  294  * MPSAFE
  295  */
  296 /* ARGSUSED */
  297 int
  298 dup(struct thread *td, struct dup_args *uap)
  299 {
  300 
  301         return (do_dup(td, DUP_VARIABLE, (int)uap->fd, 0, td->td_retval));
  302 }
  303 
  304 /*
  305  * The file control system call.
  306  */
  307 #ifndef _SYS_SYSPROTO_H_
  308 struct fcntl_args {
  309         int     fd;
  310         int     cmd;
  311         long    arg;
  312 };
  313 #endif
  314 /*
  315  * MPSAFE
  316  */
  317 /* ARGSUSED */
  318 int
  319 fcntl(struct thread *td, struct fcntl_args *uap)
  320 {
  321         struct flock fl;
  322         intptr_t arg;
  323         int error;
  324 
  325         error = 0;
  326         switch (uap->cmd) {
  327         case F_GETLK:
  328         case F_SETLK:
  329         case F_SETLKW:
  330                 error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
  331                 arg = (intptr_t)&fl;
  332                 break;
  333         default:
  334                 arg = uap->arg;
  335                 break;
  336         }
  337         if (error)
  338                 return (error);
  339         error = kern_fcntl(td, uap->fd, uap->cmd, arg);
  340         if (error)
  341                 return (error);
  342         if (uap->cmd == F_GETLK)
  343                 error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
  344         return (error);
  345 }
  346 
  347 int
  348 kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
  349 {
  350         struct filedesc *fdp;
  351         struct flock *flp;
  352         struct file *fp;
  353         struct proc *p;
  354         char *pop;
  355         struct vnode *vp;
  356         u_int newmin;
  357         int error, flg, tmp;
  358         int giant_locked;
  359 
  360         /*
  361          * XXXRW: Some fcntl() calls require Giant -- others don't.  Try to
  362          * avoid grabbing Giant for calls we know don't need it.
  363          */
  364         switch (cmd) {
  365         case F_DUPFD:
  366         case F_GETFD:
  367         case F_SETFD:
  368         case F_GETFL:
  369                 giant_locked = 0;
  370                 break;
  371 
  372         default:
  373                 giant_locked = 1;
  374                 mtx_lock(&Giant);
  375         }
  376 
  377         error = 0;
  378         flg = F_POSIX;
  379         p = td->td_proc;
  380         fdp = p->p_fd;
  381         FILEDESC_LOCK(fdp);
  382         if ((unsigned)fd >= fdp->fd_nfiles ||
  383             (fp = fdp->fd_ofiles[fd]) == NULL) {
  384                 FILEDESC_UNLOCK(fdp);
  385                 error = EBADF;
  386                 goto done2;
  387         }
  388         pop = &fdp->fd_ofileflags[fd];
  389 
  390         switch (cmd) {
  391         case F_DUPFD:
  392                 /* mtx_assert(&Giant, MA_NOTOWNED); */
  393                 FILEDESC_UNLOCK(fdp);
  394                 newmin = arg;
  395                 PROC_LOCK(p);
  396                 if (newmin >= lim_cur(p, RLIMIT_NOFILE) ||
  397                     newmin >= maxfilesperproc) {
  398                         PROC_UNLOCK(p);
  399                         error = EINVAL;
  400                         break;
  401                 }
  402                 PROC_UNLOCK(p);
  403                 error = do_dup(td, DUP_VARIABLE, fd, newmin, td->td_retval);
  404                 break;
  405 
  406         case F_GETFD:
  407                 /* mtx_assert(&Giant, MA_NOTOWNED); */
  408                 td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
  409                 FILEDESC_UNLOCK(fdp);
  410                 break;
  411 
  412         case F_SETFD:
  413                 /* mtx_assert(&Giant, MA_NOTOWNED); */
  414                 *pop = (*pop &~ UF_EXCLOSE) |
  415                     (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
  416                 FILEDESC_UNLOCK(fdp);
  417                 break;
  418 
  419         case F_GETFL:
  420                 /* mtx_assert(&Giant, MA_NOTOWNED); */
  421                 FILE_LOCK(fp);
  422                 td->td_retval[0] = OFLAGS(fp->f_flag);
  423                 FILE_UNLOCK(fp);
  424                 FILEDESC_UNLOCK(fdp);
  425                 break;
  426 
  427         case F_SETFL:
  428                 mtx_assert(&Giant, MA_OWNED);
  429                 FILE_LOCK(fp);
  430                 fhold_locked(fp);
  431                 fp->f_flag &= ~FCNTLFLAGS;
  432                 fp->f_flag |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
  433                 FILE_UNLOCK(fp);
  434                 FILEDESC_UNLOCK(fdp);
  435                 tmp = fp->f_flag & FNONBLOCK;
  436                 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
  437                 if (error) {
  438                         fdrop(fp, td);
  439                         break;
  440                 }
  441                 tmp = fp->f_flag & FASYNC;
  442                 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
  443                 if (error == 0) {
  444                         fdrop(fp, td);
  445                         break;
  446                 }
  447                 FILE_LOCK(fp);
  448                 fp->f_flag &= ~FNONBLOCK;
  449                 FILE_UNLOCK(fp);
  450                 tmp = 0;
  451                 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
  452                 fdrop(fp, td);
  453                 break;
  454 
  455         case F_GETOWN:
  456                 mtx_assert(&Giant, MA_OWNED);
  457                 fhold(fp);
  458                 FILEDESC_UNLOCK(fdp);
  459                 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
  460                 if (error == 0)
  461                         td->td_retval[0] = tmp;
  462                 fdrop(fp, td);
  463                 break;
  464 
  465         case F_SETOWN:
  466                 mtx_assert(&Giant, MA_OWNED);
  467                 fhold(fp);
  468                 FILEDESC_UNLOCK(fdp);
  469                 tmp = arg;
  470                 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
  471                 fdrop(fp, td);
  472                 break;
  473 
  474         case F_SETLKW:
  475                 mtx_assert(&Giant, MA_OWNED);
  476                 flg |= F_WAIT;
  477                 /* FALLTHROUGH F_SETLK */
  478 
  479         case F_SETLK:
  480                 mtx_assert(&Giant, MA_OWNED);
  481                 if (fp->f_type != DTYPE_VNODE) {
  482                         FILEDESC_UNLOCK(fdp);
  483                         error = EBADF;
  484                         break;
  485                 }
  486 
  487                 flp = (struct flock *)arg;
  488                 if (flp->l_whence == SEEK_CUR) {
  489                         if (fp->f_offset < 0 ||
  490                             (flp->l_start > 0 &&
  491                              fp->f_offset > OFF_MAX - flp->l_start)) {
  492                                 FILEDESC_UNLOCK(fdp);
  493                                 error = EOVERFLOW;
  494                                 break;
  495                         }
  496                         flp->l_start += fp->f_offset;
  497                 }
  498 
  499                 /*
  500                  * VOP_ADVLOCK() may block.
  501                  */
  502                 fhold(fp);
  503                 FILEDESC_UNLOCK(fdp);
  504                 vp = fp->f_vnode;
  505 
  506                 switch (flp->l_type) {
  507                 case F_RDLCK:
  508                         if ((fp->f_flag & FREAD) == 0) {
  509                                 error = EBADF;
  510                                 break;
  511                         }
  512                         PROC_LOCK(p->p_leader);
  513                         p->p_leader->p_flag |= P_ADVLOCK;
  514                         PROC_UNLOCK(p->p_leader);
  515                         error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
  516                             flp, flg);
  517                         break;
  518                 case F_WRLCK:
  519                         if ((fp->f_flag & FWRITE) == 0) {
  520                                 error = EBADF;
  521                                 break;
  522                         }
  523                         PROC_LOCK(p->p_leader);
  524                         p->p_leader->p_flag |= P_ADVLOCK;
  525                         PROC_UNLOCK(p->p_leader);
  526                         error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
  527                             flp, flg);
  528                         break;
  529                 case F_UNLCK:
  530                         error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
  531                             flp, F_POSIX);
  532                         break;
  533                 default:
  534                         error = EINVAL;
  535                         break;
  536                 }
  537                 /* Check for race with close */
  538                 FILEDESC_LOCK_FAST(fdp);
  539                 if ((unsigned) fd >= fdp->fd_nfiles ||
  540                     fp != fdp->fd_ofiles[fd]) {
  541                         FILEDESC_UNLOCK_FAST(fdp);
  542                         flp->l_whence = SEEK_SET;
  543                         flp->l_start = 0;
  544                         flp->l_len = 0;
  545                         flp->l_type = F_UNLCK;
  546                         (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
  547                                            F_UNLCK, flp, F_POSIX);
  548                 } else
  549                         FILEDESC_UNLOCK_FAST(fdp);
  550                 fdrop(fp, td);
  551                 break;
  552 
  553         case F_GETLK:
  554                 mtx_assert(&Giant, MA_OWNED);
  555                 if (fp->f_type != DTYPE_VNODE) {
  556                         FILEDESC_UNLOCK(fdp);
  557                         error = EBADF;
  558                         break;
  559                 }
  560                 flp = (struct flock *)arg;
  561                 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
  562                     flp->l_type != F_UNLCK) {
  563                         FILEDESC_UNLOCK(fdp);
  564                         error = EINVAL;
  565                         break;
  566                 }
  567                 if (flp->l_whence == SEEK_CUR) {
  568                         if ((flp->l_start > 0 &&
  569                             fp->f_offset > OFF_MAX - flp->l_start) ||
  570                             (flp->l_start < 0 &&
  571                              fp->f_offset < OFF_MIN - flp->l_start)) {
  572                                 FILEDESC_UNLOCK(fdp);
  573                                 error = EOVERFLOW;
  574                                 break;
  575                         }
  576                         flp->l_start += fp->f_offset;
  577                 }
  578                 /*
  579                  * VOP_ADVLOCK() may block.
  580                  */
  581                 fhold(fp);
  582                 FILEDESC_UNLOCK(fdp);
  583                 vp = fp->f_vnode;
  584                 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
  585                     F_POSIX);
  586                 fdrop(fp, td);
  587                 break;
  588         default:
  589                 FILEDESC_UNLOCK(fdp);
  590                 error = EINVAL;
  591                 break;
  592         }
  593 done2:
  594         if (giant_locked)
  595                 mtx_unlock(&Giant);
  596         return (error);
  597 }
  598 
  599 /*
  600  * Common code for dup, dup2, and fcntl(F_DUPFD).
  601  */
  602 static int
  603 do_dup(struct thread *td, enum dup_type type, int old, int new, register_t *retval)
  604 {
  605         struct filedesc *fdp;
  606         struct proc *p;
  607         struct file *fp;
  608         struct file *delfp;
  609         int error, holdleaders, maxfd;
  610 
  611         KASSERT((type == DUP_VARIABLE || type == DUP_FIXED),
  612             ("invalid dup type %d", type));
  613 
  614         p = td->td_proc;
  615         fdp = p->p_fd;
  616 
  617         /*
  618          * Verify we have a valid descriptor to dup from and possibly to
  619          * dup to.
  620          */
  621         if (old < 0 || new < 0)
  622                 return (EBADF);
  623         PROC_LOCK(p);
  624         maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
  625         PROC_UNLOCK(p);
  626         if (new >= maxfd)
  627                 return (EMFILE);
  628 
  629         FILEDESC_LOCK(fdp);
  630         if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
  631                 FILEDESC_UNLOCK(fdp);
  632                 return (EBADF);
  633         }
  634         if (type == DUP_FIXED && old == new) {
  635                 *retval = new;
  636                 FILEDESC_UNLOCK(fdp);
  637                 return (0);
  638         }
  639         fp = fdp->fd_ofiles[old];
  640         fhold(fp);
  641 
  642         /*
  643          * If the caller specified a file descriptor, make sure the file
  644          * table is large enough to hold it, and grab it.  Otherwise, just
  645          * allocate a new descriptor the usual way.  Since the filedesc
  646          * lock may be temporarily dropped in the process, we have to look
  647          * out for a race.
  648          */
  649         if (type == DUP_FIXED) {
  650                 if (new >= fdp->fd_nfiles)
  651                         fdgrowtable(fdp, new + 1);
  652                 if (fdp->fd_ofiles[new] == NULL)
  653                         fdused(fdp, new);
  654         } else {
  655                 if ((error = fdalloc(td, new, &new)) != 0) {
  656                         FILEDESC_UNLOCK(fdp);
  657                         fdrop(fp, td);
  658                         return (error);
  659                 }
  660         }
  661 
  662         /*
  663          * If the old file changed out from under us then treat it as a
  664          * bad file descriptor.  Userland should do its own locking to
  665          * avoid this case.
  666          */
  667         if (fdp->fd_ofiles[old] != fp) {
  668                 /* we've allocated a descriptor which we won't use */
  669                 if (fdp->fd_ofiles[new] == NULL)
  670                         fdunused(fdp, new);
  671                 FILEDESC_UNLOCK(fdp);
  672                 fdrop(fp, td);
  673                 return (EBADF);
  674         }
  675         KASSERT(old != new,
  676             ("new fd is same as old"));
  677 
  678         /*
  679          * Save info on the descriptor being overwritten.  We cannot close
  680          * it without introducing an ownership race for the slot, since we
  681          * need to drop the filedesc lock to call closef().
  682          *
  683          * XXX this duplicates parts of close().
  684          */
  685         delfp = fdp->fd_ofiles[new];
  686         holdleaders = 0;
  687         if (delfp != NULL) {
  688                 if (td->td_proc->p_fdtol != NULL) {
  689                         /*
  690                          * Ask fdfree() to sleep to ensure that all relevant
  691                          * process leaders can be traversed in closef().
  692                          */
  693                         fdp->fd_holdleaderscount++;
  694                         holdleaders = 1;
  695                 }
  696         }
  697 
  698         /*
  699          * Duplicate the source descriptor
  700          */
  701         fdp->fd_ofiles[new] = fp;
  702         fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
  703         if (new > fdp->fd_lastfile)
  704                 fdp->fd_lastfile = new;
  705         *retval = new;
  706 
  707         /*
  708          * If we dup'd over a valid file, we now own the reference to it
  709          * and must dispose of it using closef() semantics (as if a
  710          * close() were performed on it).
  711          *
  712          * XXX this duplicates parts of close().
  713          */
  714         if (delfp != NULL) {
  715                 knote_fdclose(td, new);
  716                 FILEDESC_UNLOCK(fdp);
  717                 (void) closef(delfp, td);
  718                 if (holdleaders) {
  719                         FILEDESC_LOCK_FAST(fdp);
  720                         fdp->fd_holdleaderscount--;
  721                         if (fdp->fd_holdleaderscount == 0 &&
  722                             fdp->fd_holdleaderswakeup != 0) {
  723                                 fdp->fd_holdleaderswakeup = 0;
  724                                 wakeup(&fdp->fd_holdleaderscount);
  725                         }
  726                         FILEDESC_UNLOCK_FAST(fdp);
  727                 }
  728         } else {
  729                 FILEDESC_UNLOCK(fdp);
  730         }
  731         return (0);
  732 }
  733 
  734 /*
  735  * If sigio is on the list associated with a process or process group,
  736  * disable signalling from the device, remove sigio from the list and
  737  * free sigio.
  738  */
  739 void
  740 funsetown(struct sigio **sigiop)
  741 {
  742         struct sigio *sigio;
  743 
  744         SIGIO_LOCK();
  745         sigio = *sigiop;
  746         if (sigio == NULL) {
  747                 SIGIO_UNLOCK();
  748                 return;
  749         }
  750         *(sigio->sio_myref) = NULL;
  751         if ((sigio)->sio_pgid < 0) {
  752                 struct pgrp *pg = (sigio)->sio_pgrp;
  753                 PGRP_LOCK(pg);
  754                 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
  755                              sigio, sio_pgsigio);
  756                 PGRP_UNLOCK(pg);
  757         } else {
  758                 struct proc *p = (sigio)->sio_proc;
  759                 PROC_LOCK(p);
  760                 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
  761                              sigio, sio_pgsigio);
  762                 PROC_UNLOCK(p);
  763         }
  764         SIGIO_UNLOCK();
  765         crfree(sigio->sio_ucred);
  766         FREE(sigio, M_SIGIO);
  767 }
  768 
  769 /*
  770  * Free a list of sigio structures.
  771  * We only need to lock the SIGIO_LOCK because we have made ourselves
  772  * inaccessible to callers of fsetown and therefore do not need to lock
  773  * the proc or pgrp struct for the list manipulation.
  774  */
  775 void
  776 funsetownlst(struct sigiolst *sigiolst)
  777 {
  778         struct proc *p;
  779         struct pgrp *pg;
  780         struct sigio *sigio;
  781 
  782         sigio = SLIST_FIRST(sigiolst);
  783         if (sigio == NULL)
  784                 return;
  785         p = NULL;
  786         pg = NULL;
  787 
  788         /*
  789          * Every entry of the list should belong
  790          * to a single proc or pgrp.
  791          */
  792         if (sigio->sio_pgid < 0) {
  793                 pg = sigio->sio_pgrp;
  794                 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
  795         } else /* if (sigio->sio_pgid > 0) */ {
  796                 p = sigio->sio_proc;
  797                 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
  798         }
  799 
  800         SIGIO_LOCK();
  801         while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
  802                 *(sigio->sio_myref) = NULL;
  803                 if (pg != NULL) {
  804                         KASSERT(sigio->sio_pgid < 0,
  805                             ("Proc sigio in pgrp sigio list"));
  806                         KASSERT(sigio->sio_pgrp == pg,
  807                             ("Bogus pgrp in sigio list"));
  808                         PGRP_LOCK(pg);
  809                         SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
  810                             sio_pgsigio);
  811                         PGRP_UNLOCK(pg);
  812                 } else /* if (p != NULL) */ {
  813                         KASSERT(sigio->sio_pgid > 0,
  814                             ("Pgrp sigio in proc sigio list"));
  815                         KASSERT(sigio->sio_proc == p,
  816                             ("Bogus proc in sigio list"));
  817                         PROC_LOCK(p);
  818                         SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
  819                             sio_pgsigio);
  820                         PROC_UNLOCK(p);
  821                 }
  822                 SIGIO_UNLOCK();
  823                 crfree(sigio->sio_ucred);
  824                 FREE(sigio, M_SIGIO);
  825                 SIGIO_LOCK();
  826         }
  827         SIGIO_UNLOCK();
  828 }
  829 
  830 /*
  831  * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
  832  *
  833  * After permission checking, add a sigio structure to the sigio list for
  834  * the process or process group.
  835  */
  836 int
  837 fsetown(pid_t pgid, struct sigio **sigiop)
  838 {
  839         struct proc *proc;
  840         struct pgrp *pgrp;
  841         struct sigio *sigio;
  842         int ret;
  843 
  844         if (pgid == 0) {
  845                 funsetown(sigiop);
  846                 return (0);
  847         }
  848 
  849         ret = 0;
  850 
  851         /* Allocate and fill in the new sigio out of locks. */
  852         MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, M_WAITOK);
  853         sigio->sio_pgid = pgid;
  854         sigio->sio_ucred = crhold(curthread->td_ucred);
  855         sigio->sio_myref = sigiop;
  856 
  857         sx_slock(&proctree_lock);
  858         if (pgid > 0) {
  859                 proc = pfind(pgid);
  860                 if (proc == NULL) {
  861                         ret = ESRCH;
  862                         goto fail;
  863                 }
  864 
  865                 /*
  866                  * Policy - Don't allow a process to FSETOWN a process
  867                  * in another session.
  868                  *
  869                  * Remove this test to allow maximum flexibility or
  870                  * restrict FSETOWN to the current process or process
  871                  * group for maximum safety.
  872                  */
  873                 PROC_UNLOCK(proc);
  874                 if (proc->p_session != curthread->td_proc->p_session) {
  875                         ret = EPERM;
  876                         goto fail;
  877                 }
  878 
  879                 pgrp = NULL;
  880         } else /* if (pgid < 0) */ {
  881                 pgrp = pgfind(-pgid);
  882                 if (pgrp == NULL) {
  883                         ret = ESRCH;
  884                         goto fail;
  885                 }
  886                 PGRP_UNLOCK(pgrp);
  887 
  888                 /*
  889                  * Policy - Don't allow a process to FSETOWN a process
  890                  * in another session.
  891                  *
  892                  * Remove this test to allow maximum flexibility or
  893                  * restrict FSETOWN to the current process or process
  894                  * group for maximum safety.
  895                  */
  896                 if (pgrp->pg_session != curthread->td_proc->p_session) {
  897                         ret = EPERM;
  898                         goto fail;
  899                 }
  900 
  901                 proc = NULL;
  902         }
  903         funsetown(sigiop);
  904         if (pgid > 0) {
  905                 PROC_LOCK(proc);
  906                 /*
  907                  * Since funsetownlst() is called without the proctree
  908                  * locked, we need to check for P_WEXIT.
  909                  * XXX: is ESRCH correct?
  910                  */
  911                 if ((proc->p_flag & P_WEXIT) != 0) {
  912                         PROC_UNLOCK(proc);
  913                         ret = ESRCH;
  914                         goto fail;
  915                 }
  916                 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
  917                 sigio->sio_proc = proc;
  918                 PROC_UNLOCK(proc);
  919         } else {
  920                 PGRP_LOCK(pgrp);
  921                 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
  922                 sigio->sio_pgrp = pgrp;
  923                 PGRP_UNLOCK(pgrp);
  924         }
  925         sx_sunlock(&proctree_lock);
  926         SIGIO_LOCK();
  927         *sigiop = sigio;
  928         SIGIO_UNLOCK();
  929         return (0);
  930 
  931 fail:
  932         sx_sunlock(&proctree_lock);
  933         crfree(sigio->sio_ucred);
  934         FREE(sigio, M_SIGIO);
  935         return (ret);
  936 }
  937 
  938 /*
  939  * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
  940  */
  941 pid_t
  942 fgetown(sigiop)
  943         struct sigio **sigiop;
  944 {
  945         pid_t pgid;
  946 
  947         SIGIO_LOCK();
  948         pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
  949         SIGIO_UNLOCK();
  950         return (pgid);
  951 }
  952 
  953 /*
  954  * Close a file descriptor.
  955  */
  956 #ifndef _SYS_SYSPROTO_H_
  957 struct close_args {
  958         int     fd;
  959 };
  960 #endif
  961 /*
  962  * MPSAFE
  963  */
  964 /* ARGSUSED */
  965 int
  966 close(td, uap)
  967         struct thread *td;
  968         struct close_args *uap;
  969 {
  970         struct filedesc *fdp;
  971         struct file *fp;
  972         int fd, error;
  973         int holdleaders;
  974 
  975         fd = uap->fd;
  976         error = 0;
  977         holdleaders = 0;
  978         fdp = td->td_proc->p_fd;
  979         FILEDESC_LOCK(fdp);
  980         if ((unsigned)fd >= fdp->fd_nfiles ||
  981             (fp = fdp->fd_ofiles[fd]) == NULL) {
  982                 FILEDESC_UNLOCK(fdp);
  983                 return (EBADF);
  984         }
  985         fdp->fd_ofiles[fd] = NULL;
  986         fdp->fd_ofileflags[fd] = 0;
  987         fdunused(fdp, fd);
  988         if (td->td_proc->p_fdtol != NULL) {
  989                 /*
  990                  * Ask fdfree() to sleep to ensure that all relevant
  991                  * process leaders can be traversed in closef().
  992                  */
  993                 fdp->fd_holdleaderscount++;
  994                 holdleaders = 1;
  995         }
  996 
  997         /*
  998          * We now hold the fp reference that used to be owned by the descriptor
  999          * array.
 1000          * We have to unlock the FILEDESC *AFTER* knote_fdclose to prevent a
 1001          * race of the fd getting opened, a knote added, and deleteing a knote
 1002          * for the new fd.
 1003          */
 1004         knote_fdclose(td, fd);
 1005         FILEDESC_UNLOCK(fdp);
 1006 
 1007         error = closef(fp, td);
 1008         if (holdleaders) {
 1009                 FILEDESC_LOCK_FAST(fdp);
 1010                 fdp->fd_holdleaderscount--;
 1011                 if (fdp->fd_holdleaderscount == 0 &&
 1012                     fdp->fd_holdleaderswakeup != 0) {
 1013                         fdp->fd_holdleaderswakeup = 0;
 1014                         wakeup(&fdp->fd_holdleaderscount);
 1015                 }
 1016                 FILEDESC_UNLOCK_FAST(fdp);
 1017         }
 1018         return (error);
 1019 }
 1020 
 1021 #if defined(COMPAT_43)
 1022 /*
 1023  * Return status information about a file descriptor.
 1024  */
 1025 #ifndef _SYS_SYSPROTO_H_
 1026 struct ofstat_args {
 1027         int     fd;
 1028         struct  ostat *sb;
 1029 };
 1030 #endif
 1031 /*
 1032  * MPSAFE
 1033  */
 1034 /* ARGSUSED */
 1035 int
 1036 ofstat(struct thread *td, struct ofstat_args *uap)
 1037 {
 1038         struct ostat oub;
 1039         struct stat ub;
 1040         int error;
 1041 
 1042         error = kern_fstat(td, uap->fd, &ub);
 1043         if (error == 0) {
 1044                 cvtstat(&ub, &oub);
 1045                 error = copyout(&oub, uap->sb, sizeof(oub));
 1046         }
 1047         return (error);
 1048 }
 1049 #endif /* COMPAT_43 */
 1050 
 1051 /*
 1052  * Return status information about a file descriptor.
 1053  */
 1054 #ifndef _SYS_SYSPROTO_H_
 1055 struct fstat_args {
 1056         int     fd;
 1057         struct  stat *sb;
 1058 };
 1059 #endif
 1060 /*
 1061  * MPSAFE
 1062  */
 1063 /* ARGSUSED */
 1064 int
 1065 fstat(struct thread *td, struct fstat_args *uap)
 1066 {
 1067         struct stat ub;
 1068         int error;
 1069 
 1070         error = kern_fstat(td, uap->fd, &ub);
 1071         if (error == 0)
 1072                 error = copyout(&ub, uap->sb, sizeof(ub));
 1073         return (error);
 1074 }
 1075 
 1076 int
 1077 kern_fstat(struct thread *td, int fd, struct stat *sbp)
 1078 {
 1079         struct file *fp;
 1080         int error;
 1081 
 1082         if ((error = fget(td, fd, &fp)) != 0)
 1083                 return (error);
 1084         error = fo_stat(fp, sbp, td->td_ucred, td);
 1085         fdrop(fp, td);
 1086         return (error);
 1087 }
 1088 
 1089 /*
 1090  * Return status information about a file descriptor.
 1091  */
 1092 #ifndef _SYS_SYSPROTO_H_
 1093 struct nfstat_args {
 1094         int     fd;
 1095         struct  nstat *sb;
 1096 };
 1097 #endif
 1098 /*
 1099  * MPSAFE
 1100  */
 1101 /* ARGSUSED */
 1102 int
 1103 nfstat(struct thread *td, struct nfstat_args *uap)
 1104 {
 1105         struct nstat nub;
 1106         struct stat ub;
 1107         int error;
 1108 
 1109         error = kern_fstat(td, uap->fd, &ub);
 1110         if (error == 0) {
 1111                 cvtnstat(&ub, &nub);
 1112                 error = copyout(&nub, uap->sb, sizeof(nub));
 1113         }
 1114         return (error);
 1115 }
 1116 
 1117 /*
 1118  * Return pathconf information about a file descriptor.
 1119  */
 1120 #ifndef _SYS_SYSPROTO_H_
 1121 struct fpathconf_args {
 1122         int     fd;
 1123         int     name;
 1124 };
 1125 #endif
 1126 /*
 1127  * MPSAFE
 1128  */
 1129 /* ARGSUSED */
 1130 int
 1131 fpathconf(struct thread *td, struct fpathconf_args *uap)
 1132 {
 1133         struct file *fp;
 1134         struct vnode *vp;
 1135         int error;
 1136 
 1137         if ((error = fget(td, uap->fd, &fp)) != 0)
 1138                 return (error);
 1139 
 1140         /* If asynchronous I/O is available, it works for all descriptors. */
 1141         if (uap->name == _PC_ASYNC_IO) {
 1142                 td->td_retval[0] = async_io_version;
 1143                 goto out;
 1144         }
 1145         vp = fp->f_vnode;
 1146         if (vp != NULL) {
 1147                 int vfslocked;
 1148                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1149                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
 1150                 error = VOP_PATHCONF(vp, uap->name, td->td_retval);
 1151                 VOP_UNLOCK(vp, 0, td);
 1152                 VFS_UNLOCK_GIANT(vfslocked);
 1153         } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
 1154                 if (uap->name != _PC_PIPE_BUF) {
 1155                         error = EINVAL;
 1156                 } else {
 1157                         td->td_retval[0] = PIPE_BUF;
 1158                 error = 0;
 1159                 }
 1160         } else {
 1161                 error = EOPNOTSUPP;
 1162         }
 1163 out:
 1164         fdrop(fp, td);
 1165         return (error);
 1166 }
 1167 
 1168 /*
 1169  * Grow the file table to accomodate (at least) nfd descriptors.  This may
 1170  * block and drop the filedesc lock, but it will reacquire it before
 1171  * returning.
 1172  */
 1173 static void
 1174 fdgrowtable(struct filedesc *fdp, int nfd)
 1175 {
 1176         struct file **ntable;
 1177         char *nfileflags;
 1178         int nnfiles, onfiles;
 1179         NDSLOTTYPE *nmap;
 1180 
 1181         FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
 1182 
 1183         KASSERT(fdp->fd_nfiles > 0,
 1184             ("zero-length file table"));
 1185 
 1186         /* compute the size of the new table */
 1187         onfiles = fdp->fd_nfiles;
 1188         nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
 1189         if (nnfiles <= onfiles)
 1190                 /* the table is already large enough */
 1191                 return;
 1192 
 1193         /* allocate a new table and (if required) new bitmaps */
 1194         FILEDESC_UNLOCK(fdp);
 1195         MALLOC(ntable, struct file **, nnfiles * OFILESIZE,
 1196             M_FILEDESC, M_ZERO | M_WAITOK);
 1197         nfileflags = (char *)&ntable[nnfiles];
 1198         if (NDSLOTS(nnfiles) > NDSLOTS(onfiles))
 1199                 MALLOC(nmap, NDSLOTTYPE *, NDSLOTS(nnfiles) * NDSLOTSIZE,
 1200                     M_FILEDESC, M_ZERO | M_WAITOK);
 1201         else
 1202                 nmap = NULL;
 1203         FILEDESC_LOCK(fdp);
 1204 
 1205         /*
 1206          * We now have new tables ready to go.  Since we dropped the
 1207          * filedesc lock to call malloc(), watch out for a race.
 1208          */
 1209         onfiles = fdp->fd_nfiles;
 1210         if (onfiles >= nnfiles) {
 1211                 /* we lost the race, but that's OK */
 1212                 free(ntable, M_FILEDESC);
 1213                 if (nmap != NULL)
 1214                         free(nmap, M_FILEDESC);
 1215                 return;
 1216         }
 1217         bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable));
 1218         bcopy(fdp->fd_ofileflags, nfileflags, onfiles);
 1219         if (onfiles > NDFILE)
 1220                 free(fdp->fd_ofiles, M_FILEDESC);
 1221         fdp->fd_ofiles = ntable;
 1222         fdp->fd_ofileflags = nfileflags;
 1223         if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) {
 1224                 bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap));
 1225                 if (NDSLOTS(onfiles) > NDSLOTS(NDFILE))
 1226                         free(fdp->fd_map, M_FILEDESC);
 1227                 fdp->fd_map = nmap;
 1228         }
 1229         fdp->fd_nfiles = nnfiles;
 1230 }
 1231 
 1232 /*
 1233  * Allocate a file descriptor for the process.
 1234  */
 1235 int
 1236 fdalloc(struct thread *td, int minfd, int *result)
 1237 {
 1238         struct proc *p = td->td_proc;
 1239         struct filedesc *fdp = p->p_fd;
 1240         int fd = -1, maxfd;
 1241 
 1242         FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
 1243 
 1244         if (fdp->fd_freefile > minfd)
 1245                 minfd = fdp->fd_freefile;          
 1246 
 1247         PROC_LOCK(p);
 1248         maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
 1249         PROC_UNLOCK(p);
 1250 
 1251         /*
 1252          * Search the bitmap for a free descriptor.  If none is found, try
 1253          * to grow the file table.  Keep at it until we either get a file
 1254          * descriptor or run into process or system limits; fdgrowtable()
 1255          * may drop the filedesc lock, so we're in a race.
 1256          */
 1257         for (;;) {
 1258                 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
 1259                 if (fd >= maxfd)
 1260                         return (EMFILE);
 1261                 if (fd < fdp->fd_nfiles)
 1262                         break;
 1263                 fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd));
 1264         }
 1265 
 1266         /*
 1267          * Perform some sanity checks, then mark the file descriptor as
 1268          * used and return it to the caller.
 1269          */
 1270         KASSERT(!fdisused(fdp, fd),
 1271             ("fd_first_free() returned non-free descriptor"));
 1272         KASSERT(fdp->fd_ofiles[fd] == NULL,
 1273             ("free descriptor isn't"));
 1274         fdp->fd_ofileflags[fd] = 0; /* XXX needed? */
 1275         fdused(fdp, fd);
 1276         *result = fd;
 1277         return (0);
 1278 }
 1279 
 1280 /*
 1281  * Check to see whether n user file descriptors
 1282  * are available to the process p.
 1283  */
 1284 int
 1285 fdavail(struct thread *td, int n)
 1286 {
 1287         struct proc *p = td->td_proc;
 1288         struct filedesc *fdp = td->td_proc->p_fd;
 1289         struct file **fpp;
 1290         int i, lim, last;
 1291 
 1292         FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
 1293 
 1294         PROC_LOCK(p);
 1295         lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
 1296         PROC_UNLOCK(p);
 1297         if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
 1298                 return (1);
 1299         last = min(fdp->fd_nfiles, lim);
 1300         fpp = &fdp->fd_ofiles[fdp->fd_freefile];
 1301         for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
 1302                 if (*fpp == NULL && --n <= 0)
 1303                         return (1);
 1304         }
 1305         return (0);
 1306 }
 1307 
 1308 /*
 1309  * Create a new open file structure and allocate
 1310  * a file decriptor for the process that refers to it.
 1311  * We add one reference to the file for the descriptor table
 1312  * and one reference for resultfp. This is to prevent us being
 1313  * preempted and the entry in the descriptor table closed after
 1314  * we release the FILEDESC lock.
 1315  */
 1316 int
 1317 falloc(struct thread *td, struct file **resultfp, int *resultfd)
 1318 {
 1319         struct proc *p = td->td_proc;
 1320         struct file *fp, *fq;
 1321         int error, i;
 1322         int maxuserfiles = maxfiles - (maxfiles / 20);
 1323         static struct timeval lastfail;
 1324         static int curfail;
 1325 
 1326         fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
 1327         sx_xlock(&filelist_lock);
 1328         if ((openfiles >= maxuserfiles && (td->td_ucred->cr_ruid != 0 ||
 1329            jailed(td->td_ucred))) || openfiles >= maxfiles) {
 1330                 if (ppsratecheck(&lastfail, &curfail, 1)) {
 1331                         printf("kern.maxfiles limit exceeded by uid %i, please see tuning(7).\n",
 1332                                 td->td_ucred->cr_ruid);
 1333                 }
 1334                 sx_xunlock(&filelist_lock);
 1335                 uma_zfree(file_zone, fp);
 1336                 return (ENFILE);
 1337         }
 1338         openfiles++;
 1339 
 1340         /*
 1341          * If the process has file descriptor zero open, add the new file
 1342          * descriptor to the list of open files at that point, otherwise
 1343          * put it at the front of the list of open files.
 1344          */
 1345         fp->f_mtxp = mtx_pool_alloc(mtxpool_sleep);
 1346         fp->f_count = 1;
 1347         if (resultfp)
 1348                 fp->f_count++;
 1349         fp->f_cred = crhold(td->td_ucred);
 1350         fp->f_ops = &badfileops;
 1351         fp->f_data = NULL;
 1352         fp->f_vnode = NULL;
 1353         FILEDESC_LOCK(p->p_fd);
 1354         if ((fq = p->p_fd->fd_ofiles[0])) {
 1355                 LIST_INSERT_AFTER(fq, fp, f_list);
 1356         } else {
 1357                 LIST_INSERT_HEAD(&filehead, fp, f_list);
 1358         }
 1359         sx_xunlock(&filelist_lock);
 1360         if ((error = fdalloc(td, 0, &i))) {
 1361                 FILEDESC_UNLOCK(p->p_fd);
 1362                 fdrop(fp, td);
 1363                 if (resultfp)
 1364                         fdrop(fp, td);
 1365                 return (error);
 1366         }
 1367         p->p_fd->fd_ofiles[i] = fp;
 1368         FILEDESC_UNLOCK(p->p_fd);
 1369         if (resultfp)
 1370                 *resultfp = fp;
 1371         if (resultfd)
 1372                 *resultfd = i;
 1373         return (0);
 1374 }
 1375 
 1376 /*
 1377  * Build a new filedesc structure from another.
 1378  * Copy the current, root, and jail root vnode references.
 1379  */
 1380 struct filedesc *
 1381 fdinit(struct filedesc *fdp)
 1382 {
 1383         struct filedesc0 *newfdp;
 1384 
 1385         newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO);
 1386         mtx_init(&newfdp->fd_fd.fd_mtx, FILEDESC_LOCK_DESC, NULL, MTX_DEF);
 1387         if (fdp != NULL) {
 1388                 FILEDESC_LOCK(fdp);
 1389                 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
 1390                 if (newfdp->fd_fd.fd_cdir)
 1391                         VREF(newfdp->fd_fd.fd_cdir);
 1392                 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
 1393                 if (newfdp->fd_fd.fd_rdir)
 1394                         VREF(newfdp->fd_fd.fd_rdir);
 1395                 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
 1396                 if (newfdp->fd_fd.fd_jdir)
 1397                         VREF(newfdp->fd_fd.fd_jdir);
 1398                 FILEDESC_UNLOCK(fdp);
 1399         }
 1400 
 1401         /* Create the file descriptor table. */
 1402         newfdp->fd_fd.fd_refcnt = 1;
 1403         newfdp->fd_fd.fd_holdcnt = 1;
 1404         newfdp->fd_fd.fd_cmask = CMASK;
 1405         newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
 1406         newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
 1407         newfdp->fd_fd.fd_nfiles = NDFILE;
 1408         newfdp->fd_fd.fd_map = newfdp->fd_dmap;
 1409         newfdp->fd_fd.fd_lastfile = -1;
 1410         return (&newfdp->fd_fd);
 1411 }
 1412 
 1413 static struct filedesc *
 1414 fdhold(struct proc *p)
 1415 {
 1416         struct filedesc *fdp;
 1417 
 1418         mtx_lock(&fdesc_mtx);
 1419         fdp = p->p_fd;
 1420         if (fdp != NULL)
 1421                 fdp->fd_holdcnt++;
 1422         mtx_unlock(&fdesc_mtx);
 1423         return (fdp);
 1424 }
 1425 
 1426 static void
 1427 fddrop(struct filedesc *fdp)
 1428 {
 1429         int i;
 1430 
 1431         mtx_lock(&fdesc_mtx);
 1432         i = --fdp->fd_holdcnt;
 1433         mtx_unlock(&fdesc_mtx);
 1434         if (i > 0)
 1435                 return;
 1436 
 1437         mtx_destroy(&fdp->fd_mtx);
 1438         FREE(fdp, M_FILEDESC);
 1439 }
 1440 
 1441 /*
 1442  * Share a filedesc structure.
 1443  */
 1444 struct filedesc *
 1445 fdshare(struct filedesc *fdp)
 1446 {
 1447         FILEDESC_LOCK_FAST(fdp);
 1448         fdp->fd_refcnt++;
 1449         FILEDESC_UNLOCK_FAST(fdp);
 1450         return (fdp);
 1451 }
 1452 
 1453 /*
 1454  * Unshare a filedesc structure, if necessary by making a copy
 1455  */
 1456 void
 1457 fdunshare(struct proc *p, struct thread *td)
 1458 {
 1459 
 1460         FILEDESC_LOCK_FAST(p->p_fd);
 1461         if (p->p_fd->fd_refcnt > 1) {
 1462                 struct filedesc *tmp;
 1463 
 1464                 FILEDESC_UNLOCK_FAST(p->p_fd);
 1465                 tmp = fdcopy(p->p_fd);
 1466                 fdfree(td);
 1467                 p->p_fd = tmp;
 1468         } else
 1469                 FILEDESC_UNLOCK_FAST(p->p_fd);
 1470 }
 1471 
 1472 /*
 1473  * Copy a filedesc structure.
 1474  * A NULL pointer in returns a NULL reference, this is to ease callers,
 1475  * not catch errors.
 1476  */
 1477 struct filedesc *
 1478 fdcopy(struct filedesc *fdp)
 1479 {
 1480         struct filedesc *newfdp;
 1481         int i;
 1482 
 1483         /* Certain daemons might not have file descriptors. */
 1484         if (fdp == NULL)
 1485                 return (NULL);
 1486 
 1487         newfdp = fdinit(fdp);
 1488         FILEDESC_LOCK_FAST(fdp);
 1489         while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
 1490                 FILEDESC_UNLOCK_FAST(fdp);
 1491                 FILEDESC_LOCK(newfdp);
 1492                 fdgrowtable(newfdp, fdp->fd_lastfile + 1);
 1493                 FILEDESC_UNLOCK(newfdp);
 1494                 FILEDESC_LOCK_FAST(fdp);
 1495         }
 1496         /* copy everything except kqueue descriptors */
 1497         newfdp->fd_freefile = -1;
 1498         for (i = 0; i <= fdp->fd_lastfile; ++i) {
 1499                 if (fdisused(fdp, i) &&
 1500                     fdp->fd_ofiles[i]->f_type != DTYPE_KQUEUE) {
 1501                         newfdp->fd_ofiles[i] = fdp->fd_ofiles[i];
 1502                         newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i];
 1503                         fhold(newfdp->fd_ofiles[i]);
 1504                         newfdp->fd_lastfile = i;
 1505                 } else {
 1506                         if (newfdp->fd_freefile == -1)
 1507                                 newfdp->fd_freefile = i;
 1508                 }
 1509         }
 1510         FILEDESC_UNLOCK_FAST(fdp);
 1511         FILEDESC_LOCK(newfdp);
 1512         for (i = 0; i <= newfdp->fd_lastfile; ++i)
 1513                 if (newfdp->fd_ofiles[i] != NULL)
 1514                         fdused(newfdp, i);
 1515         FILEDESC_UNLOCK(newfdp);
 1516         FILEDESC_LOCK_FAST(fdp);
 1517         if (newfdp->fd_freefile == -1)
 1518                 newfdp->fd_freefile = i;
 1519         newfdp->fd_cmask = fdp->fd_cmask;
 1520         FILEDESC_UNLOCK_FAST(fdp);
 1521         return (newfdp);
 1522 }
 1523 
 1524 /*
 1525  * Release a filedesc structure.
 1526  */
 1527 void
 1528 fdfree(struct thread *td)
 1529 {
 1530         struct filedesc *fdp;
 1531         struct file **fpp;
 1532         int i, locked;
 1533         struct filedesc_to_leader *fdtol;
 1534         struct file *fp;
 1535         struct vnode *cdir, *jdir, *rdir, *vp;
 1536         struct flock lf;
 1537 
 1538         /* Certain daemons might not have file descriptors. */
 1539         fdp = td->td_proc->p_fd;
 1540         if (fdp == NULL)
 1541                 return;
 1542 
 1543         /* Check for special need to clear POSIX style locks */
 1544         fdtol = td->td_proc->p_fdtol;
 1545         if (fdtol != NULL) {
 1546                 FILEDESC_LOCK(fdp);
 1547                 KASSERT(fdtol->fdl_refcount > 0,
 1548                         ("filedesc_to_refcount botch: fdl_refcount=%d",
 1549                          fdtol->fdl_refcount));
 1550                 if (fdtol->fdl_refcount == 1 &&
 1551                     (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
 1552                         for (i = 0, fpp = fdp->fd_ofiles;
 1553                              i <= fdp->fd_lastfile;
 1554                              i++, fpp++) {
 1555                                 if (*fpp == NULL ||
 1556                                     (*fpp)->f_type != DTYPE_VNODE)
 1557                                         continue;
 1558                                 fp = *fpp;
 1559                                 fhold(fp);
 1560                                 FILEDESC_UNLOCK(fdp);
 1561                                 lf.l_whence = SEEK_SET;
 1562                                 lf.l_start = 0;
 1563                                 lf.l_len = 0;
 1564                                 lf.l_type = F_UNLCK;
 1565                                 vp = fp->f_vnode;
 1566                                 locked = VFS_LOCK_GIANT(vp->v_mount);
 1567                                 (void) VOP_ADVLOCK(vp,
 1568                                                    (caddr_t)td->td_proc->
 1569                                                    p_leader,
 1570                                                    F_UNLCK,
 1571                                                    &lf,
 1572                                                    F_POSIX);
 1573                                 VFS_UNLOCK_GIANT(locked);
 1574                                 FILEDESC_LOCK(fdp);
 1575                                 fdrop(fp, td);
 1576                                 fpp = fdp->fd_ofiles + i;
 1577                         }
 1578                 }
 1579         retry:
 1580                 if (fdtol->fdl_refcount == 1) {
 1581                         if (fdp->fd_holdleaderscount > 0 &&
 1582                             (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
 1583                                 /*
 1584                                  * close() or do_dup() has cleared a reference
 1585                                  * in a shared file descriptor table.
 1586                                  */
 1587                                 fdp->fd_holdleaderswakeup = 1;
 1588                                 msleep(&fdp->fd_holdleaderscount, &fdp->fd_mtx,
 1589                                        PLOCK, "fdlhold", 0);
 1590                                 goto retry;
 1591                         }
 1592                         if (fdtol->fdl_holdcount > 0) {
 1593                                 /*
 1594                                  * Ensure that fdtol->fdl_leader
 1595                                  * remains valid in closef().
 1596                                  */
 1597                                 fdtol->fdl_wakeup = 1;
 1598                                 msleep(fdtol, &fdp->fd_mtx,
 1599                                        PLOCK, "fdlhold", 0);
 1600                                 goto retry;
 1601                         }
 1602                 }
 1603                 fdtol->fdl_refcount--;
 1604                 if (fdtol->fdl_refcount == 0 &&
 1605                     fdtol->fdl_holdcount == 0) {
 1606                         fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
 1607                         fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
 1608                 } else
 1609                         fdtol = NULL;
 1610                 td->td_proc->p_fdtol = NULL;
 1611                 FILEDESC_UNLOCK(fdp);
 1612                 if (fdtol != NULL)
 1613                         FREE(fdtol, M_FILEDESC_TO_LEADER);
 1614         }
 1615         FILEDESC_LOCK_FAST(fdp);
 1616         i = --fdp->fd_refcnt;
 1617         FILEDESC_UNLOCK_FAST(fdp);
 1618         if (i > 0)
 1619                 return;
 1620         /*
 1621          * We are the last reference to the structure, so we can
 1622          * safely assume it will not change out from under us.
 1623          */
 1624         fpp = fdp->fd_ofiles;
 1625         for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
 1626                 if (*fpp)
 1627                         (void) closef(*fpp, td);
 1628         }
 1629         FILEDESC_LOCK(fdp);
 1630 
 1631         /* XXX This should happen earlier. */
 1632         mtx_lock(&fdesc_mtx);
 1633         td->td_proc->p_fd = NULL;
 1634         mtx_unlock(&fdesc_mtx);
 1635 
 1636         if (fdp->fd_nfiles > NDFILE)
 1637                 FREE(fdp->fd_ofiles, M_FILEDESC);
 1638         if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
 1639                 FREE(fdp->fd_map, M_FILEDESC);
 1640 
 1641         fdp->fd_nfiles = 0;
 1642 
 1643         cdir = fdp->fd_cdir;
 1644         fdp->fd_cdir = NULL;
 1645         rdir = fdp->fd_rdir;
 1646         fdp->fd_rdir = NULL;
 1647         jdir = fdp->fd_jdir;
 1648         fdp->fd_jdir = NULL;
 1649         FILEDESC_UNLOCK(fdp);
 1650 
 1651         if (cdir) {
 1652                 locked = VFS_LOCK_GIANT(cdir->v_mount);
 1653                 vrele(cdir);
 1654                 VFS_UNLOCK_GIANT(locked);
 1655         }
 1656         if (rdir) {
 1657                 locked = VFS_LOCK_GIANT(rdir->v_mount);
 1658                 vrele(rdir);
 1659                 VFS_UNLOCK_GIANT(locked);
 1660         }
 1661         if (jdir) {
 1662                 locked = VFS_LOCK_GIANT(jdir->v_mount);
 1663                 vrele(jdir);
 1664                 VFS_UNLOCK_GIANT(locked);
 1665         }
 1666 
 1667         fddrop(fdp);
 1668 }
 1669 
 1670 /*
 1671  * For setugid programs, we don't want to people to use that setugidness
 1672  * to generate error messages which write to a file which otherwise would
 1673  * otherwise be off-limits to the process.  We check for filesystems where
 1674  * the vnode can change out from under us after execve (like [lin]procfs).
 1675  *
 1676  * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
 1677  * sufficient.  We also don't check for setugidness since we know we are.
 1678  */
 1679 static int
 1680 is_unsafe(struct file *fp)
 1681 {
 1682         if (fp->f_type == DTYPE_VNODE) {
 1683                 struct vnode *vp = fp->f_vnode;
 1684 
 1685                 if ((vp->v_vflag & VV_PROCDEP) != 0)
 1686                         return (1);
 1687         }
 1688         return (0);
 1689 }
 1690 
 1691 /*
 1692  * Make this setguid thing safe, if at all possible.
 1693  */
 1694 void
 1695 setugidsafety(struct thread *td)
 1696 {
 1697         struct filedesc *fdp;
 1698         int i;
 1699 
 1700         /* Certain daemons might not have file descriptors. */
 1701         fdp = td->td_proc->p_fd;
 1702         if (fdp == NULL)
 1703                 return;
 1704 
 1705         /*
 1706          * Note: fdp->fd_ofiles may be reallocated out from under us while
 1707          * we are blocked in a close.  Be careful!
 1708          */
 1709         FILEDESC_LOCK(fdp);
 1710         for (i = 0; i <= fdp->fd_lastfile; i++) {
 1711                 if (i > 2)
 1712                         break;
 1713                 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
 1714                         struct file *fp;
 1715 
 1716                         knote_fdclose(td, i);
 1717                         /*
 1718                          * NULL-out descriptor prior to close to avoid
 1719                          * a race while close blocks.
 1720                          */
 1721                         fp = fdp->fd_ofiles[i];
 1722                         fdp->fd_ofiles[i] = NULL;
 1723                         fdp->fd_ofileflags[i] = 0;
 1724                         fdunused(fdp, i);
 1725                         FILEDESC_UNLOCK(fdp);
 1726                         (void) closef(fp, td);
 1727                         FILEDESC_LOCK(fdp);
 1728                 }
 1729         }
 1730         FILEDESC_UNLOCK(fdp);
 1731 }
 1732 
 1733 void
 1734 fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
 1735 {
 1736 
 1737         FILEDESC_LOCK(fdp);
 1738         if (fdp->fd_ofiles[idx] == fp) {
 1739                 fdp->fd_ofiles[idx] = NULL;
 1740                 fdunused(fdp, idx);
 1741                 FILEDESC_UNLOCK(fdp);
 1742                 fdrop(fp, td);
 1743         } else {
 1744                 FILEDESC_UNLOCK(fdp);
 1745         }
 1746 }
 1747 
 1748 /*
 1749  * Close any files on exec?
 1750  */
 1751 void
 1752 fdcloseexec(struct thread *td)
 1753 {
 1754         struct filedesc *fdp;
 1755         int i;
 1756 
 1757         /* Certain daemons might not have file descriptors. */
 1758         fdp = td->td_proc->p_fd;
 1759         if (fdp == NULL)
 1760                 return;
 1761 
 1762         FILEDESC_LOCK(fdp);
 1763 
 1764         /*
 1765          * We cannot cache fd_ofiles or fd_ofileflags since operations
 1766          * may block and rip them out from under us.
 1767          */
 1768         for (i = 0; i <= fdp->fd_lastfile; i++) {
 1769                 if (fdp->fd_ofiles[i] != NULL &&
 1770                     (fdp->fd_ofileflags[i] & UF_EXCLOSE)) {
 1771                         struct file *fp;
 1772 
 1773                         knote_fdclose(td, i);
 1774                         /*
 1775                          * NULL-out descriptor prior to close to avoid
 1776                          * a race while close blocks.
 1777                          */
 1778                         fp = fdp->fd_ofiles[i];
 1779                         fdp->fd_ofiles[i] = NULL;
 1780                         fdp->fd_ofileflags[i] = 0;
 1781                         fdunused(fdp, i);
 1782                         FILEDESC_UNLOCK(fdp);
 1783                         (void) closef(fp, td);
 1784                         FILEDESC_LOCK(fdp);
 1785                 }
 1786         }
 1787         FILEDESC_UNLOCK(fdp);
 1788 }
 1789 
 1790 /*
 1791  * It is unsafe for set[ug]id processes to be started with file
 1792  * descriptors 0..2 closed, as these descriptors are given implicit
 1793  * significance in the Standard C library.  fdcheckstd() will create a
 1794  * descriptor referencing /dev/null for each of stdin, stdout, and
 1795  * stderr that is not already open.
 1796  */
 1797 int
 1798 fdcheckstd(struct thread *td)
 1799 {
 1800         struct nameidata nd;
 1801         struct filedesc *fdp;
 1802         struct file *fp;
 1803         register_t retval;
 1804         int fd, i, error, flags, devnull;
 1805 
 1806         fdp = td->td_proc->p_fd;
 1807         if (fdp == NULL)
 1808                 return (0);
 1809         KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
 1810         devnull = -1;
 1811         error = 0;
 1812         for (i = 0; i < 3; i++) {
 1813                 if (fdp->fd_ofiles[i] != NULL)
 1814                         continue;
 1815                 if (devnull < 0) {
 1816                         int vfslocked;
 1817                         error = falloc(td, &fp, &fd);
 1818                         if (error != 0)
 1819                                 break;
 1820                         /* Note extra ref on `fp' held for us by falloc(). */
 1821                         KASSERT(fd == i, ("oof, we didn't get our fd"));
 1822                         NDINIT(&nd, LOOKUP, FOLLOW | MPSAFE, UIO_SYSSPACE,
 1823                             "/dev/null", td);
 1824                         flags = FREAD | FWRITE;
 1825                         error = vn_open(&nd, &flags, 0, fd);
 1826                         if (error != 0) {
 1827                                 /*
 1828                                  * Someone may have closed the entry in the
 1829                                  * file descriptor table, so check it hasn't
 1830                                  * changed before dropping the reference count.
 1831                                  */
 1832                                 FILEDESC_LOCK(fdp);
 1833                                 KASSERT(fdp->fd_ofiles[fd] == fp,
 1834                                     ("table not shared, how did it change?"));
 1835                                 fdp->fd_ofiles[fd] = NULL;
 1836                                 fdunused(fdp, fd);
 1837                                 FILEDESC_UNLOCK(fdp);
 1838                                 fdrop(fp, td);
 1839                                 fdrop(fp, td);
 1840                                 break;
 1841                         }
 1842                         vfslocked = NDHASGIANT(&nd);
 1843                         NDFREE(&nd, NDF_ONLY_PNBUF);
 1844                         fp->f_flag = flags;
 1845                         fp->f_vnode = nd.ni_vp;
 1846                         if (fp->f_data == NULL)
 1847                                 fp->f_data = nd.ni_vp;
 1848                         if (fp->f_ops == &badfileops)
 1849                                 fp->f_ops = &vnops;
 1850                         fp->f_type = DTYPE_VNODE;
 1851                         VOP_UNLOCK(nd.ni_vp, 0, td);
 1852                         VFS_UNLOCK_GIANT(vfslocked);
 1853                         devnull = fd;
 1854                         fdrop(fp, td);
 1855                 } else {
 1856                         error = do_dup(td, DUP_FIXED, devnull, i, &retval);
 1857                         if (error != 0)
 1858                                 break;
 1859                 }
 1860         }
 1861         return (error);
 1862 }
 1863 
 1864 /*
 1865  * Internal form of close.
 1866  * Decrement reference count on file structure.
 1867  * Note: td may be NULL when closing a file that was being passed in a
 1868  * message.
 1869  *
 1870  * XXXRW: Giant is not required for the caller, but often will be held; this
 1871  * makes it moderately likely the Giant will be recursed in the VFS case.
 1872  */
 1873 int
 1874 closef(struct file *fp, struct thread *td)
 1875 {
 1876         struct vnode *vp;
 1877         struct flock lf;
 1878         struct filedesc_to_leader *fdtol;
 1879         struct filedesc *fdp;
 1880 
 1881         /*
 1882          * POSIX record locking dictates that any close releases ALL
 1883          * locks owned by this process.  This is handled by setting
 1884          * a flag in the unlock to free ONLY locks obeying POSIX
 1885          * semantics, and not to free BSD-style file locks.
 1886          * If the descriptor was in a message, POSIX-style locks
 1887          * aren't passed with the descriptor, and the thread pointer
 1888          * will be NULL.  Callers should be careful only to pass a
 1889          * NULL thread pointer when there really is no owning
 1890          * context that might have locks, or the locks will be
 1891          * leaked.
 1892          */
 1893         if (fp->f_type == DTYPE_VNODE && td != NULL) {
 1894                 int vfslocked;
 1895 
 1896                 vp = fp->f_vnode;
 1897                 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
 1898                 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
 1899                         lf.l_whence = SEEK_SET;
 1900                         lf.l_start = 0;
 1901                         lf.l_len = 0;
 1902                         lf.l_type = F_UNLCK;
 1903                         (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
 1904                                            F_UNLCK, &lf, F_POSIX);
 1905                 }
 1906                 fdtol = td->td_proc->p_fdtol;
 1907                 if (fdtol != NULL) {
 1908                         /*
 1909                          * Handle special case where file descriptor table
 1910                          * is shared between multiple process leaders.
 1911                          */
 1912                         fdp = td->td_proc->p_fd;
 1913                         FILEDESC_LOCK(fdp);
 1914                         for (fdtol = fdtol->fdl_next;
 1915                              fdtol != td->td_proc->p_fdtol;
 1916                              fdtol = fdtol->fdl_next) {
 1917                                 if ((fdtol->fdl_leader->p_flag &
 1918                                      P_ADVLOCK) == 0)
 1919                                         continue;
 1920                                 fdtol->fdl_holdcount++;
 1921                                 FILEDESC_UNLOCK(fdp);
 1922                                 lf.l_whence = SEEK_SET;
 1923                                 lf.l_start = 0;
 1924                                 lf.l_len = 0;
 1925                                 lf.l_type = F_UNLCK;
 1926                                 vp = fp->f_vnode;
 1927                                 (void) VOP_ADVLOCK(vp,
 1928                                                    (caddr_t)fdtol->fdl_leader,
 1929                                                    F_UNLCK, &lf, F_POSIX);
 1930                                 FILEDESC_LOCK(fdp);
 1931                                 fdtol->fdl_holdcount--;
 1932                                 if (fdtol->fdl_holdcount == 0 &&
 1933                                     fdtol->fdl_wakeup != 0) {
 1934                                         fdtol->fdl_wakeup = 0;
 1935                                         wakeup(fdtol);
 1936                                 }
 1937                         }
 1938                         FILEDESC_UNLOCK(fdp);
 1939                 }
 1940                 VFS_UNLOCK_GIANT(vfslocked);
 1941         }
 1942         return (fdrop(fp, td));
 1943 }
 1944 
 1945 /*
 1946  * Extract the file pointer associated with the specified descriptor for
 1947  * the current user process.
 1948  *
 1949  * If the descriptor doesn't exist, EBADF is returned.
 1950  *
 1951  * If the descriptor exists but doesn't match 'flags' then
 1952  * return EBADF for read attempts and EINVAL for write attempts.
 1953  *
 1954  * If 'hold' is set (non-zero) the file's refcount will be bumped on return.
 1955  * It should be dropped with fdrop().
 1956  * If it is not set, then the refcount will not be bumped however the
 1957  * thread's filedesc struct will be returned locked (for fgetsock).
 1958  *
 1959  * If an error occured the non-zero error is returned and *fpp is set to NULL.
 1960  * Otherwise *fpp is set and zero is returned.
 1961  */
 1962 static __inline int
 1963 _fget(struct thread *td, int fd, struct file **fpp, int flags, int hold)
 1964 {
 1965         struct filedesc *fdp;
 1966         struct file *fp;
 1967 
 1968         *fpp = NULL;
 1969         if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
 1970                 return (EBADF);
 1971         FILEDESC_LOCK(fdp);
 1972         if ((fp = fget_locked(fdp, fd)) == NULL || fp->f_ops == &badfileops) {
 1973                 FILEDESC_UNLOCK(fdp);
 1974                 return (EBADF);
 1975         }
 1976 
 1977         /*
 1978          * Note: FREAD failure returns EBADF to maintain backwards
 1979          * compatibility with what routines returned before.
 1980          *
 1981          * Only one flag, or 0, may be specified.
 1982          */
 1983         if (flags == FREAD && (fp->f_flag & FREAD) == 0) {
 1984                 FILEDESC_UNLOCK(fdp);
 1985                 return (EBADF);
 1986         }
 1987         if (flags == FWRITE && (fp->f_flag & FWRITE) == 0) {
 1988                 FILEDESC_UNLOCK(fdp);
 1989                 return (EINVAL);
 1990         }
 1991         if (hold) {
 1992                 fhold(fp);
 1993                 FILEDESC_UNLOCK(fdp);
 1994         }
 1995         *fpp = fp;
 1996         return (0);
 1997 }
 1998 
 1999 int
 2000 fget(struct thread *td, int fd, struct file **fpp)
 2001 {
 2002 
 2003         return(_fget(td, fd, fpp, 0, 1));
 2004 }
 2005 
 2006 int
 2007 fget_read(struct thread *td, int fd, struct file **fpp)
 2008 {
 2009 
 2010         return(_fget(td, fd, fpp, FREAD, 1));
 2011 }
 2012 
 2013 int
 2014 fget_write(struct thread *td, int fd, struct file **fpp)
 2015 {
 2016 
 2017         return(_fget(td, fd, fpp, FWRITE, 1));
 2018 }
 2019 
 2020 /*
 2021  * Like fget() but loads the underlying vnode, or returns an error if
 2022  * the descriptor does not represent a vnode.  Note that pipes use vnodes
 2023  * but never have VM objects.  The returned vnode will be vref()d.
 2024  *
 2025  * XXX: what about the unused flags ?
 2026  */
 2027 static __inline int
 2028 _fgetvp(struct thread *td, int fd, struct vnode **vpp, int flags)
 2029 {
 2030         struct file *fp;
 2031         int error;
 2032 
 2033         *vpp = NULL;
 2034         if ((error = _fget(td, fd, &fp, flags, 0)) != 0)
 2035                 return (error);
 2036         if (fp->f_vnode == NULL) {
 2037                 error = EINVAL;
 2038         } else {
 2039                 *vpp = fp->f_vnode;
 2040                 vref(*vpp);
 2041         }
 2042         FILEDESC_UNLOCK(td->td_proc->p_fd);
 2043         return (error);
 2044 }
 2045 
 2046 int
 2047 fgetvp(struct thread *td, int fd, struct vnode **vpp)
 2048 {
 2049 
 2050         return (_fgetvp(td, fd, vpp, 0));
 2051 }
 2052 
 2053 int
 2054 fgetvp_read(struct thread *td, int fd, struct vnode **vpp)
 2055 {
 2056 
 2057         return (_fgetvp(td, fd, vpp, FREAD));
 2058 }
 2059 
 2060 #ifdef notyet
 2061 int
 2062 fgetvp_write(struct thread *td, int fd, struct vnode **vpp)
 2063 {
 2064 
 2065         return (_fgetvp(td, fd, vpp, FWRITE));
 2066 }
 2067 #endif
 2068 
 2069 /*
 2070  * Like fget() but loads the underlying socket, or returns an error if
 2071  * the descriptor does not represent a socket.
 2072  *
 2073  * We bump the ref count on the returned socket.  XXX Also obtain the SX
 2074  * lock in the future.
 2075  */
 2076 int
 2077 fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp)
 2078 {
 2079         struct file *fp;
 2080         int error;
 2081 
 2082         NET_ASSERT_GIANT();
 2083 
 2084         *spp = NULL;
 2085         if (fflagp != NULL)
 2086                 *fflagp = 0;
 2087         if ((error = _fget(td, fd, &fp, 0, 0)) != 0)
 2088                 return (error);
 2089         if (fp->f_type != DTYPE_SOCKET) {
 2090                 error = ENOTSOCK;
 2091         } else {
 2092                 *spp = fp->f_data;
 2093                 if (fflagp)
 2094                         *fflagp = fp->f_flag;
 2095                 SOCK_LOCK(*spp);
 2096                 soref(*spp);
 2097                 SOCK_UNLOCK(*spp);
 2098         }
 2099         FILEDESC_UNLOCK(td->td_proc->p_fd);
 2100         return (error);
 2101 }
 2102 
 2103 /*
 2104  * Drop the reference count on the socket and XXX release the SX lock in
 2105  * the future.  The last reference closes the socket.
 2106  */
 2107 void
 2108 fputsock(struct socket *so)
 2109 {
 2110 
 2111         NET_ASSERT_GIANT();
 2112         ACCEPT_LOCK();
 2113         SOCK_LOCK(so);
 2114         sorele(so);
 2115 }
 2116 
 2117 int
 2118 fdrop(struct file *fp, struct thread *td)
 2119 {
 2120 
 2121         FILE_LOCK(fp);
 2122         return (fdrop_locked(fp, td));
 2123 }
 2124 
 2125 /*
 2126  * Drop reference on struct file passed in, may call closef if the
 2127  * reference hits zero.
 2128  * Expects struct file locked, and will unlock it.
 2129  */
 2130 static int
 2131 fdrop_locked(struct file *fp, struct thread *td)
 2132 {
 2133         int error;
 2134 
 2135         FILE_LOCK_ASSERT(fp, MA_OWNED);
 2136 
 2137         if (--fp->f_count > 0) {
 2138                 FILE_UNLOCK(fp);
 2139                 return (0);
 2140         }
 2141         /* We have the last ref so we can proceed without the file lock. */
 2142         FILE_UNLOCK(fp);
 2143         if (fp->f_count < 0)
 2144                 panic("fdrop: count < 0");
 2145         if (fp->f_ops != &badfileops)
 2146                 error = fo_close(fp, td);
 2147         else
 2148                 error = 0;
 2149 
 2150         sx_xlock(&filelist_lock);
 2151         LIST_REMOVE(fp, f_list);
 2152         openfiles--;
 2153         sx_xunlock(&filelist_lock);
 2154         crfree(fp->f_cred);
 2155         uma_zfree(file_zone, fp);
 2156 
 2157         return (error);
 2158 }
 2159 
 2160 /*
 2161  * Apply an advisory lock on a file descriptor.
 2162  *
 2163  * Just attempt to get a record lock of the requested type on
 2164  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
 2165  */
 2166 #ifndef _SYS_SYSPROTO_H_
 2167 struct flock_args {
 2168         int     fd;
 2169         int     how;
 2170 };
 2171 #endif
 2172 /*
 2173  * MPSAFE
 2174  */
 2175 /* ARGSUSED */
 2176 int
 2177 flock(struct thread *td, struct flock_args *uap)
 2178 {
 2179         struct file *fp;
 2180         struct vnode *vp;
 2181         struct flock lf;
 2182         int error;
 2183 
 2184         if ((error = fget(td, uap->fd, &fp)) != 0)
 2185                 return (error);
 2186         if (fp->f_type != DTYPE_VNODE) {
 2187                 fdrop(fp, td);
 2188                 return (EOPNOTSUPP);
 2189         }
 2190 
 2191         mtx_lock(&Giant);
 2192         vp = fp->f_vnode;
 2193         lf.l_whence = SEEK_SET;
 2194         lf.l_start = 0;
 2195         lf.l_len = 0;
 2196         if (uap->how & LOCK_UN) {
 2197                 lf.l_type = F_UNLCK;
 2198                 FILE_LOCK(fp);
 2199                 fp->f_flag &= ~FHASLOCK;
 2200                 FILE_UNLOCK(fp);
 2201                 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
 2202                 goto done2;
 2203         }
 2204         if (uap->how & LOCK_EX)
 2205                 lf.l_type = F_WRLCK;
 2206         else if (uap->how & LOCK_SH)
 2207                 lf.l_type = F_RDLCK;
 2208         else {
 2209                 error = EBADF;
 2210                 goto done2;
 2211         }
 2212         FILE_LOCK(fp);
 2213         fp->f_flag |= FHASLOCK;
 2214         FILE_UNLOCK(fp);
 2215         error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
 2216             (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
 2217 done2:
 2218         fdrop(fp, td);
 2219         mtx_unlock(&Giant);
 2220         return (error);
 2221 }
 2222 /*
 2223  * Duplicate the specified descriptor to a free descriptor.
 2224  */
 2225 int
 2226 dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd, int mode, int error)
 2227 {
 2228         struct file *wfp;
 2229         struct file *fp;
 2230 
 2231         /*
 2232          * If the to-be-dup'd fd number is greater than the allowed number
 2233          * of file descriptors, or the fd to be dup'd has already been
 2234          * closed, then reject.
 2235          */
 2236         FILEDESC_LOCK(fdp);
 2237         if (dfd < 0 || dfd >= fdp->fd_nfiles ||
 2238             (wfp = fdp->fd_ofiles[dfd]) == NULL) {
 2239                 FILEDESC_UNLOCK(fdp);
 2240                 return (EBADF);
 2241         }
 2242 
 2243         /*
 2244          * There are two cases of interest here.
 2245          *
 2246          * For ENODEV simply dup (dfd) to file descriptor
 2247          * (indx) and return.
 2248          *
 2249          * For ENXIO steal away the file structure from (dfd) and
 2250          * store it in (indx).  (dfd) is effectively closed by
 2251          * this operation.
 2252          *
 2253          * Any other error code is just returned.
 2254          */
 2255         switch (error) {
 2256         case ENODEV:
 2257                 /*
 2258                  * Check that the mode the file is being opened for is a
 2259                  * subset of the mode of the existing descriptor.
 2260                  */
 2261                 FILE_LOCK(wfp);
 2262                 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
 2263                         FILE_UNLOCK(wfp);
 2264                         FILEDESC_UNLOCK(fdp);
 2265                         return (EACCES);
 2266                 }
 2267                 fp = fdp->fd_ofiles[indx];
 2268                 fdp->fd_ofiles[indx] = wfp;
 2269                 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
 2270                 if (fp == NULL)
 2271                         fdused(fdp, indx);
 2272                 fhold_locked(wfp);
 2273                 FILE_UNLOCK(wfp);
 2274                 FILEDESC_UNLOCK(fdp);
 2275                 if (fp != NULL) {
 2276                         /*
 2277                          * We now own the reference to fp that the ofiles[]
 2278                          * array used to own.  Release it.
 2279                          */
 2280                         FILE_LOCK(fp);
 2281                         fdrop_locked(fp, td);
 2282                 }
 2283                 return (0);
 2284 
 2285         case ENXIO:
 2286                 /*
 2287                  * Steal away the file pointer from dfd and stuff it into indx.
 2288                  */
 2289                 fp = fdp->fd_ofiles[indx];
 2290                 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
 2291                 fdp->fd_ofiles[dfd] = NULL;
 2292                 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
 2293                 fdp->fd_ofileflags[dfd] = 0;
 2294                 fdunused(fdp, dfd);
 2295                 if (fp == NULL)
 2296                         fdused(fdp, indx);
 2297                 if (fp != NULL)
 2298                         FILE_LOCK(fp);
 2299 
 2300                 /*
 2301                  * We now own the reference to fp that the ofiles[] array
 2302                  * used to own.  Release it.
 2303                  */
 2304                 if (fp != NULL)
 2305                         fdrop_locked(fp, td);
 2306 
 2307                 FILEDESC_UNLOCK(fdp);
 2308 
 2309                 return (0);
 2310 
 2311         default:
 2312                 FILEDESC_UNLOCK(fdp);
 2313                 return (error);
 2314         }
 2315         /* NOTREACHED */
 2316 }
 2317 
 2318 /*
 2319  * Scan all active processes to see if any of them have a current
 2320  * or root directory of `olddp'. If so, replace them with the new
 2321  * mount point.
 2322  */
 2323 void
 2324 mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
 2325 {
 2326         struct filedesc *fdp;
 2327         struct proc *p;
 2328         int nrele;
 2329 
 2330         if (vrefcnt(olddp) == 1)
 2331                 return;
 2332         sx_slock(&allproc_lock);
 2333         LIST_FOREACH(p, &allproc, p_list) {
 2334                 fdp = fdhold(p);
 2335                 if (fdp == NULL)
 2336                         continue;
 2337                 nrele = 0;
 2338                 FILEDESC_LOCK_FAST(fdp);
 2339                 if (fdp->fd_cdir == olddp) {
 2340                         vref(newdp);
 2341                         fdp->fd_cdir = newdp;
 2342                         nrele++;
 2343                 }
 2344                 if (fdp->fd_rdir == olddp) {
 2345                         vref(newdp);
 2346                         fdp->fd_rdir = newdp;
 2347                         nrele++;
 2348                 }
 2349                 FILEDESC_UNLOCK_FAST(fdp);
 2350                 fddrop(fdp);
 2351                 while (nrele--)
 2352                         vrele(olddp);
 2353         }
 2354         sx_sunlock(&allproc_lock);
 2355         if (rootvnode == olddp) {
 2356                 vrele(rootvnode);
 2357                 vref(newdp);
 2358                 rootvnode = newdp;
 2359         }
 2360 }
 2361 
 2362 struct filedesc_to_leader *
 2363 filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader)
 2364 {
 2365         struct filedesc_to_leader *fdtol;
 2366 
 2367         MALLOC(fdtol, struct filedesc_to_leader *,
 2368                sizeof(struct filedesc_to_leader),
 2369                M_FILEDESC_TO_LEADER,
 2370                M_WAITOK);
 2371         fdtol->fdl_refcount = 1;
 2372         fdtol->fdl_holdcount = 0;
 2373         fdtol->fdl_wakeup = 0;
 2374         fdtol->fdl_leader = leader;
 2375         if (old != NULL) {
 2376                 FILEDESC_LOCK(fdp);
 2377                 fdtol->fdl_next = old->fdl_next;
 2378                 fdtol->fdl_prev = old;
 2379                 old->fdl_next = fdtol;
 2380                 fdtol->fdl_next->fdl_prev = fdtol;
 2381                 FILEDESC_UNLOCK(fdp);
 2382         } else {
 2383                 fdtol->fdl_next = fdtol;
 2384                 fdtol->fdl_prev = fdtol;
 2385         }
 2386         return (fdtol);
 2387 }
 2388 
 2389 /*
 2390  * Get file structures.
 2391  */
 2392 static int
 2393 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
 2394 {
 2395         struct xfile xf;
 2396         struct filedesc *fdp;
 2397         struct file *fp;
 2398         struct proc *p;
 2399         int error, n;
 2400 
 2401         /*
 2402          * Note: because the number of file descriptors is calculated
 2403          * in different ways for sizing vs returning the data,
 2404          * there is information leakage from the first loop.  However,
 2405          * it is of a similar order of magnitude to the leakage from
 2406          * global system statistics such as kern.openfiles.
 2407          */
 2408         error = sysctl_wire_old_buffer(req, 0);
 2409         if (error != 0)
 2410                 return (error);
 2411         if (req->oldptr == NULL) {
 2412                 n = 16;         /* A slight overestimate. */
 2413                 sx_slock(&filelist_lock);
 2414                 LIST_FOREACH(fp, &filehead, f_list) {
 2415                         /*
 2416                          * We should grab the lock, but this is an
 2417                          * estimate, so does it really matter?
 2418                          */
 2419                         /* mtx_lock(fp->f_mtxp); */
 2420                         n += fp->f_count;
 2421                         /* mtx_unlock(f->f_mtxp); */
 2422                 }
 2423                 sx_sunlock(&filelist_lock);
 2424                 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
 2425         }
 2426         error = 0;
 2427         bzero(&xf, sizeof(xf));
 2428         xf.xf_size = sizeof(xf);
 2429         sx_slock(&allproc_lock);
 2430         LIST_FOREACH(p, &allproc, p_list) {
 2431                 if (p->p_state == PRS_NEW)
 2432                         continue;
 2433                 PROC_LOCK(p);
 2434                 if (p_cansee(req->td, p) != 0) {
 2435                         PROC_UNLOCK(p);
 2436                         continue;
 2437                 }
 2438                 xf.xf_pid = p->p_pid;
 2439                 xf.xf_uid = p->p_ucred->cr_uid;
 2440                 PROC_UNLOCK(p);
 2441                 fdp = fdhold(p);
 2442                 if (fdp == NULL)
 2443                         continue;
 2444                 FILEDESC_LOCK_FAST(fdp);
 2445                 for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) {
 2446                         if ((fp = fdp->fd_ofiles[n]) == NULL)
 2447                                 continue;
 2448                         xf.xf_fd = n;
 2449                         xf.xf_file = fp;
 2450                         xf.xf_data = fp->f_data;
 2451                         xf.xf_vnode = fp->f_vnode;
 2452                         xf.xf_type = fp->f_type;
 2453                         xf.xf_count = fp->f_count;
 2454                         xf.xf_msgcount = fp->f_msgcount;
 2455                         xf.xf_offset = fp->f_offset;
 2456                         xf.xf_flag = fp->f_flag;
 2457                         error = SYSCTL_OUT(req, &xf, sizeof(xf));
 2458                         if (error)
 2459                                 break;
 2460                 }
 2461                 FILEDESC_UNLOCK_FAST(fdp);
 2462                 fddrop(fdp);
 2463                 if (error)
 2464                         break;
 2465         }
 2466         sx_sunlock(&allproc_lock);
 2467         return (error);
 2468 }
 2469 
 2470 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
 2471     0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
 2472 
 2473 #ifdef DDB
 2474 /*
 2475  * For the purposes of debugging, generate a human-readable string for the
 2476  * file type.
 2477  */
 2478 static const char *
 2479 file_type_to_name(short type)
 2480 {
 2481 
 2482         switch (type) {
 2483         case 0:
 2484                 return ("zero");
 2485         case DTYPE_VNODE:
 2486                 return ("vnod");
 2487         case DTYPE_SOCKET:
 2488                 return ("sock");
 2489         case DTYPE_PIPE:
 2490                 return ("pipe");
 2491         case DTYPE_FIFO:
 2492                 return ("fifo");
 2493         case DTYPE_CRYPTO:
 2494                 return ("crpt");
 2495         default:
 2496                 return ("unkn");
 2497         }
 2498 }
 2499 
 2500 /*
 2501  * For the purposes of debugging, identify a process (if any, perhaps one of
 2502  * many) that references the passed file in its file descriptor array. Return
 2503  * NULL if none.
 2504  */
 2505 static struct proc *
 2506 file_to_first_proc(struct file *fp)
 2507 {
 2508         struct filedesc *fdp;
 2509         struct proc *p;
 2510         int n;
 2511 
 2512         LIST_FOREACH(p, &allproc, p_list) {
 2513                 if (p->p_state == PRS_NEW)
 2514                         continue;
 2515                 fdp = p->p_fd;
 2516                 if (fdp == NULL)
 2517                         continue;
 2518                 for (n = 0; n < fdp->fd_nfiles; n++) {
 2519                         if (fp == fdp->fd_ofiles[n])
 2520                                 return (p);
 2521                 }
 2522         }
 2523         return (NULL);
 2524 }
 2525 
 2526 DB_SHOW_COMMAND(files, db_show_files)
 2527 {
 2528         struct file *fp;
 2529         struct proc *p;
 2530 
 2531         db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n", "File",
 2532             "Type", "Data", "Flag", "GCFl", "Count", "MCount", "Vnode",
 2533             "FPID", "FCmd");
 2534         LIST_FOREACH(fp, &filehead, f_list) {
 2535                 p = file_to_first_proc(fp);
 2536                 db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
 2537                     file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
 2538                     fp->f_gcflag, fp->f_count, fp->f_msgcount, fp->f_vnode,
 2539                     p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-");
 2540         }
 2541 }
 2542 #endif
 2543 
 2544 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
 2545     &maxfilesperproc, 0, "Maximum files allowed open per process");
 2546 
 2547 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
 2548     &maxfiles, 0, "Maximum number of files");
 2549 
 2550 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
 2551     &openfiles, 0, "System-wide number of open files");
 2552 
 2553 /* ARGSUSED*/
 2554 static void
 2555 filelistinit(void *dummy)
 2556 {
 2557 
 2558         file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
 2559             NULL, NULL, UMA_ALIGN_PTR, 0);
 2560         sx_init(&filelist_lock, "filelist lock");
 2561         mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
 2562         mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF);
 2563 }
 2564 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL)
 2565 
 2566 /*-------------------------------------------------------------------*/
 2567 
 2568 static int
 2569 badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
 2570 {
 2571 
 2572         return (EBADF);
 2573 }
 2574 
 2575 static int
 2576 badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, struct thread *td)
 2577 {
 2578 
 2579         return (EBADF);
 2580 }
 2581 
 2582 static int
 2583 badfo_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td)
 2584 {
 2585 
 2586         return (0);
 2587 }
 2588 
 2589 static int
 2590 badfo_kqfilter(struct file *fp, struct knote *kn)
 2591 {
 2592 
 2593         return (0);
 2594 }
 2595 
 2596 static int
 2597 badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, struct thread *td)
 2598 {
 2599 
 2600         return (EBADF);
 2601 }
 2602 
 2603 static int
 2604 badfo_close(struct file *fp, struct thread *td)
 2605 {
 2606 
 2607         return (EBADF);
 2608 }
 2609 
 2610 struct fileops badfileops = {
 2611         .fo_read = badfo_readwrite,
 2612         .fo_write = badfo_readwrite,
 2613         .fo_ioctl = badfo_ioctl,
 2614         .fo_poll = badfo_poll,
 2615         .fo_kqfilter = badfo_kqfilter,
 2616         .fo_stat = badfo_stat,
 2617         .fo_close = badfo_close,
 2618 };
 2619 
 2620 
 2621 /*-------------------------------------------------------------------*/
 2622 
 2623 /*
 2624  * File Descriptor pseudo-device driver (/dev/fd/).
 2625  *
 2626  * Opening minor device N dup()s the file (if any) connected to file
 2627  * descriptor N belonging to the calling process.  Note that this driver
 2628  * consists of only the ``open()'' routine, because all subsequent
 2629  * references to this file will be direct to the other driver.
 2630  *
 2631  * XXX: we could give this one a cloning event handler if necessary.
 2632  */
 2633 
 2634 /* ARGSUSED */
 2635 static int
 2636 fdopen(struct cdev *dev, int mode, int type, struct thread *td)
 2637 {
 2638 
 2639         /*
 2640          * XXX Kludge: set curthread->td_dupfd to contain the value of the
 2641          * the file descriptor being sought for duplication. The error
 2642          * return ensures that the vnode for this device will be released
 2643          * by vn_open. Open will detect this special error and take the
 2644          * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
 2645          * will simply report the error.
 2646          */
 2647         td->td_dupfd = dev2unit(dev);
 2648         return (ENODEV);
 2649 }
 2650 
 2651 static struct cdevsw fildesc_cdevsw = {
 2652         .d_version =    D_VERSION,
 2653         .d_flags =      D_NEEDGIANT,
 2654         .d_open =       fdopen,
 2655         .d_name =       "FD",
 2656 };
 2657 
 2658 static void
 2659 fildesc_drvinit(void *unused)
 2660 {
 2661         struct cdev *dev;
 2662 
 2663         dev = make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "fd/0");
 2664         make_dev_alias(dev, "stdin");
 2665         dev = make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "fd/1");
 2666         make_dev_alias(dev, "stdout");
 2667         dev = make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "fd/2");
 2668         make_dev_alias(dev, "stderr");
 2669 }
 2670 
 2671 SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL)

Cache object: 4ce9d8f514d05d99ce5d013f30cfe203


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.