filedesc.h 5.6 KB

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