file.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* $OpenBSD: file.h,v 1.34 2014/11/18 15:16:35 mikeb Exp $ */
  2. /* $NetBSD: file.h,v 1.11 1995/03/26 20:24:13 jtc Exp $ */
  3. /*
  4. * Copyright (c) 1982, 1986, 1989, 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. * @(#)file.h 8.2 (Berkeley) 8/20/94
  32. */
  33. #include <sys/fcntl.h>
  34. #ifdef _KERNEL
  35. #include <sys/queue.h>
  36. struct proc;
  37. struct uio;
  38. struct knote;
  39. struct stat;
  40. struct file;
  41. struct ucred;
  42. struct fileops {
  43. int (*fo_read)(struct file *, off_t *, struct uio *,
  44. struct ucred *);
  45. int (*fo_write)(struct file *, off_t *, struct uio *,
  46. struct ucred *);
  47. int (*fo_ioctl)(struct file *, u_long, caddr_t,
  48. struct proc *);
  49. int (*fo_poll)(struct file *, int, struct proc *);
  50. int (*fo_kqfilter)(struct file *, struct knote *);
  51. int (*fo_stat)(struct file *, struct stat *, struct proc *);
  52. int (*fo_close)(struct file *, struct proc *);
  53. };
  54. /*
  55. * Kernel descriptor table.
  56. * One entry for each open kernel vnode and socket.
  57. */
  58. struct file {
  59. LIST_ENTRY(file) f_list;/* list of active files */
  60. short f_flag; /* see fcntl.h */
  61. #define DTYPE_VNODE 1 /* file */
  62. #define DTYPE_SOCKET 2 /* communications endpoint */
  63. #define DTYPE_PIPE 3 /* pipe */
  64. #define DTYPE_KQUEUE 4 /* event queue */
  65. /* was define DTYPE_CRYPTO 5 */
  66. #define DTYPE_SYSTRACE 6 /* system call tracing */
  67. short f_type; /* descriptor type */
  68. long f_count; /* reference count */
  69. long f_msgcount; /* references from message queue */
  70. struct ucred *f_cred; /* credentials associated with descriptor */
  71. struct fileops *f_ops;
  72. off_t f_offset;
  73. void *f_data; /* private data */
  74. int f_iflags; /* internal flags */
  75. u_int64_t f_rxfer; /* total number of read transfers */
  76. u_int64_t f_wxfer; /* total number of write transfers */
  77. u_int64_t f_seek; /* total independent seek operations */
  78. u_int64_t f_rbytes; /* total bytes read */
  79. u_int64_t f_wbytes; /* total bytes written */
  80. };
  81. #define FIF_HASLOCK 0x01 /* descriptor holds advisory lock */
  82. #define FIF_LARVAL 0x02 /* not fully constructed, don't use */
  83. #define FIF_MARK 0x04 /* mark during gc() */
  84. #define FIF_DEFER 0x08 /* defer for next gc() pass */
  85. #define FILE_IS_USABLE(fp) \
  86. (((fp)->f_iflags & FIF_LARVAL) == 0)
  87. #define FREF(fp) do { (fp)->f_count++; } while (0)
  88. #define FRELE(fp,p) (--(fp)->f_count == 0 ? fdrop(fp, p) : 0)
  89. #define FILE_SET_MATURE(fp,p) do { \
  90. (fp)->f_iflags &= ~FIF_LARVAL; \
  91. FRELE(fp, p); \
  92. } while (0)
  93. int fdrop(struct file *, struct proc *);
  94. LIST_HEAD(filelist, file);
  95. extern struct filelist filehead; /* head of list of open files */
  96. extern int maxfiles; /* kernel limit on number of open files */
  97. extern int nfiles; /* actual number of open files */
  98. extern struct fileops vnops; /* vnode operations for files */
  99. #endif /* _KERNEL */