1 /* $OpenBSD: file.h,v 1.66 2022/06/20 01:39:44 visa Exp $ */
2 /* $NetBSD: file.h,v 1.11 1995/03/26 20:24:13 jtc Exp $ */
3
4 /*
5 * Copyright (c) 1982, 1986, 1989, 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 * @(#)file.h 8.2 (Berkeley) 8/20/94
33 */
34
35 #ifndef _KERNEL
36 #include <sys/fcntl.h>
37
38 #else /* _KERNEL */
39 #include <sys/queue.h>
40 #include <sys/mutex.h>
41 #endif /* _KERNEL */
42
43 #define DTYPE_VNODE 1 /* file */
44 #define DTYPE_SOCKET 2 /* communications endpoint */
45 #define DTYPE_PIPE 3 /* pipe */
46 #define DTYPE_KQUEUE 4 /* event queue */
47 #define DTYPE_DMABUF 5 /* DMA buffer (for DRM) */
48 #define DTYPE_SYNC 6 /* sync file (for DRM) */
49
50 #ifdef _KERNEL
51 struct proc;
52 struct uio;
53 struct knote;
54 struct stat;
55 struct file;
56 struct ucred;
57
58 /**
59 * File operations.
60 * The following entries could be called without KERNEL_LOCK hold:
61 * - fo_read
62 * - fo_write
63 * - fo_close
64 */
65 struct fileops {
66 int (*fo_read)(struct file *, struct uio *, int);
67 int (*fo_write)(struct file *, struct uio *, int);
68 int (*fo_ioctl)(struct file *, u_long, caddr_t, struct proc *);
69 int (*fo_kqfilter)(struct file *, struct knote *);
70 int (*fo_stat)(struct file *, struct stat *, struct proc *);
71 int (*fo_close)(struct file *, struct proc *);
72 int (*fo_seek)(struct file *, off_t *, int, struct proc *);
73 };
74 #define FO_POSITION 0x00000001 /* positioned read/write */
75
76 /*
77 * Kernel descriptor table.
78 * One entry for each open kernel vnode and socket.
79 *
80 * Locks used to protect struct members in this file:
81 * I immutable after creation
82 * F global `fhdlk' mutex
83 * a atomic operations
84 * f per file `f_mtx'
85 * v vnode lock
86 */
87 struct file {
88 LIST_ENTRY(file) f_list;/* [F] list of active files */
89 struct mutex f_mtx;
90 u_int f_flag; /* [a] see fcntl.h */
91 u_int f_iflags; /* [a] internal flags */
92 int f_type; /* [I] descriptor type */
93 u_int f_count; /* [a] reference count */
94 struct ucred *f_cred; /* [I] credentials associated with descriptor */
95 const struct fileops *f_ops; /* [I] file operation pointers */
96 off_t f_offset; /* [f,v] offset */
97 void *f_data; /* [I] private data */
98 uint64_t f_rxfer; /* [f] total number of read transfers */
99 uint64_t f_wxfer; /* [f] total number of write transfers */
100 uint64_t f_seek; /* [f] total independent seek operations */
101 uint64_t f_rbytes; /* [f] total bytes read */
102 uint64_t f_wbytes; /* [f] total bytes written */
103 };
104
105 #define FIF_HASLOCK 0x01 /* descriptor holds advisory lock */
106 #define FIF_INSERTED 0x80 /* present in `filehead' */
107
108 #define FREF(fp) \
109 do { \
110 extern void vfs_stall_barrier(void); \
111 vfs_stall_barrier(); \
112 atomic_inc_int(&(fp)->f_count); \
113 } while (0)
114
115 #define FRELE(fp,p) \
116 (atomic_dec_int_nv(&fp->f_count) == 0 ? fdrop(fp, p) : 0)
117
118 #define FDUP_MAX_COUNT (UINT_MAX - 2 * MAXCPUS)
119
120 int fdrop(struct file *, struct proc *);
121
122 static inline off_t
foffset(struct file * fp)123 foffset(struct file *fp)
124 {
125 off_t offset;
126
127 mtx_enter(&fp->f_mtx);
128 offset = fp->f_offset;
129 mtx_leave(&fp->f_mtx);
130 return (offset);
131 }
132
133 LIST_HEAD(filelist, file);
134 extern int maxfiles; /* kernel limit on number of open files */
135 extern int numfiles; /* actual number of open files */
136 extern const struct fileops socketops; /* socket operations for files */
137 extern const struct fileops vnops; /* vnode operations for files */
138
139 #endif /* _KERNEL */
140