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/sys/filedesc.h

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 /*      $OpenBSD: filedesc.h,v 1.46 2022/05/12 13:33:09 mvs Exp $       */
    2 /*      $NetBSD: filedesc.h,v 1.14 1996/04/09 20:55:28 cgd Exp $        */
    3 
    4 /*
    5  * Copyright (c) 1990, 1993
    6  *      The Regents of the University of California.  All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of the University nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  *
   32  *      @(#)filedesc.h  8.1 (Berkeley) 6/2/93
   33  */
   34 
   35 #include <sys/mutex.h>
   36 #include <sys/rwlock.h>
   37 /*
   38  * This structure is used for the management of descriptors.  It may be
   39  * shared by multiple processes.
   40  *
   41  * A process is initially started out with NDFILE descriptors stored within
   42  * this structure, selected to be enough for typical applications based on
   43  * the historical limit of 20 open files (and the usage of descriptors by
   44  * shells).  If these descriptors are exhausted, a larger descriptor table
   45  * may be allocated, up to a process' resource limit; the internal arrays
   46  * are then unused.  The initial expansion is set to NDEXTENT; each time
   47  * it runs out, it is doubled until the resource limit is reached. NDEXTENT
   48  * should be selected to be the biggest multiple of OFILESIZE (see below)
   49  * that will fit in a power-of-two sized piece of memory.
   50  */
   51 #define NDFILE          20
   52 #define NDEXTENT        50              /* 250 bytes in 256-byte alloc. */
   53 #define NDENTRIES       32              /* 32 fds per entry */
   54 #define NDENTRYMASK     (NDENTRIES - 1)
   55 #define NDENTRYSHIFT    5               /* bits per entry */
   56 #define NDREDUCE(x)     (((x) + NDENTRIES - 1) >> NDENTRYSHIFT)
   57 #define NDHISLOTS(x)    (NDREDUCE(NDREDUCE(x)))
   58 #define NDLOSLOTS(x)    (NDHISLOTS(x) << NDENTRYSHIFT)
   59 
   60 struct kqueue;
   61 
   62 /*
   63  * Locking:
   64  *      a       atomic operations
   65  *      f       fd_lock
   66  *      f/w     fd_lock when writing
   67  *      K       kernel lock
   68  *      m       fd_fplock
   69  */
   70 struct filedesc {
   71         struct  file **fd_ofiles;       /* [f/w,m] file structures for
   72                                          *     open files */
   73         char    *fd_ofileflags;         /* [f] per-process open file flags */
   74         struct  vnode *fd_cdir;         /* [K] current directory */
   75         struct  vnode *fd_rdir;         /* [K] root directory */
   76         int     fd_nfiles;              /* [f] number of open files allocated */
   77         int     fd_openfd;              /* [f] number of files currently open */
   78         u_int   *fd_himap;              /* [f] each bit points to 32 fds */
   79         u_int   *fd_lomap;              /* [f] bitmap of free fds */
   80         int     fd_lastfile;            /* [f] high-water mark of fd_ofiles */
   81         int     fd_freefile;            /* [f] approx. next free file */
   82         mode_t  fd_cmask;               /* [f/w] mask for file creation */
   83         u_int   fd_refcnt;              /* [K] reference count */
   84         struct rwlock fd_lock;          /* lock for the file descs */
   85         struct mutex fd_fplock;         /* lock for reading fd_ofiles without
   86                                          * fd_lock */
   87         LIST_HEAD(, kqueue) fd_kqlist;  /* [f] kqueues attached to this
   88                                          *     filedesc */
   89         int fd_flags;                   /* [a] flags on this filedesc */
   90 };
   91 
   92 /*
   93  * Basic allocation of descriptors:
   94  * one of the above, plus arrays for NDFILE descriptors.
   95  */
   96 struct filedesc0 {
   97         struct  filedesc fd_fd;
   98         /*
   99          * These arrays are used when the number of open files is
  100          * <= NDFILE, and are then pointed to by the pointers above.
  101          */
  102         struct  file *fd_dfiles[NDFILE];
  103         char    fd_dfileflags[NDFILE];
  104         /*
  105          * There arrays are used when the number of open files is
  106          * <= 1024, and are then pointed to by the pointers above.
  107          */
  108         u_int   fd_dhimap[NDENTRIES >> NDENTRYSHIFT];
  109         u_int   fd_dlomap[NDENTRIES];
  110 };
  111 
  112 /*
  113  * Per-process open flags.
  114  */
  115 #define UF_EXCLOSE      0x01            /* auto-close on exec */
  116 #define UF_PLEDGED      0x02            /* open after pledge(2) */
  117 
  118 /*
  119  * Flags on the file descriptor table.
  120  */
  121 #define FD_ADVLOCK      0x01            /* May hold a POSIX adv. lock. */
  122 
  123 /*
  124  * Storage required per open file descriptor.
  125  */
  126 #define OFILESIZE (sizeof(struct file *) + sizeof(char))
  127 
  128 #ifdef _KERNEL
  129 /*
  130  * Kernel global variables and routines.
  131  */
  132 void    filedesc_init(void);
  133 int     dupfdopen(struct proc *, int, int);
  134 int     fdalloc(struct proc *p, int want, int *result);
  135 void    fdexpand(struct proc *);
  136 struct  file *fnew(struct proc *_p);
  137 int     falloc(struct proc *_p, struct file **_rfp, int *_rfd);
  138 struct  filedesc *fdinit(void);
  139 struct  filedesc *fdshare(struct process *);
  140 struct  filedesc *fdcopy(struct process *);
  141 void    fdfree(struct proc *p);
  142 int     fdrelease(struct proc *p, int);
  143 void    fdinsert(struct filedesc *, int, int, struct file *);
  144 void    fdremove(struct filedesc *, int);
  145 void    fdcloseexec(struct proc *);
  146 struct file *fd_iterfile(struct file *, struct proc *);
  147 struct file *fd_getfile(struct filedesc *, int);
  148 struct file *fd_getfile_mode(struct filedesc *, int, int);
  149 int     fd_checkclosed(struct filedesc *, int, struct file *);
  150 
  151 int     closef(struct file *, struct proc *);
  152 int     getsock(struct proc *, int, struct file **);
  153 
  154 #define fdplock(fdp)    do { NET_ASSERT_UNLOCKED(); rw_enter_write(&(fdp)->fd_lock); } while (0)
  155 #define fdpunlock(fdp)  rw_exit_write(&(fdp)->fd_lock)
  156 #define fdpassertlocked(fdp)    rw_assert_wrlock(&(fdp)->fd_lock)
  157 #endif

Cache object: e32768e9037f2d27fe7f5be0f20cba9f


[ 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.