FreeBSD/Linux Kernel Cross Reference
sys/sys/file.h
1 /* $NetBSD: file.h,v 1.56.12.1 2009/04/10 20:32:04 snj Exp $ */
2
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.3 (Berkeley) 1/9/95
32 */
33
34 #ifndef _SYS_FILE_H_
35 #define _SYS_FILE_H_
36
37 #include <sys/fcntl.h>
38 #include <sys/unistd.h>
39
40 #ifdef _KERNEL
41 #include <sys/mallocvar.h>
42 #include <sys/queue.h>
43 #include <sys/lock.h>
44
45 MALLOC_DECLARE(M_FILE);
46 MALLOC_DECLARE(M_IOCTLOPS);
47
48 struct proc;
49 struct lwp;
50 struct uio;
51 struct iovec;
52 struct stat;
53 struct knote;
54
55 /*
56 * Kernel descriptor table.
57 * One entry for each open kernel vnode and socket.
58 */
59 struct file {
60 LIST_ENTRY(file) f_list; /* list of active files */
61 int f_flag; /* see fcntl.h */
62 int f_iflags; /* internal flags; FIF_* */
63 int f_advice; /* access pattern hint; UVM_ADV_* */
64 #define DTYPE_VNODE 1 /* file */
65 #define DTYPE_SOCKET 2 /* communications endpoint */
66 #define DTYPE_PIPE 3 /* pipe */
67 #define DTYPE_KQUEUE 4 /* event queue */
68 #define DTYPE_MISC 5 /* misc file descriptor type */
69 #define DTYPE_CRYPTO 6 /* crypto */
70 #define DTYPE_NAMES \
71 "", "file", "socket", "pipe", "kqueue", "misc", "crypto"
72 int f_type; /* descriptor type */
73 u_int f_count; /* reference count */
74 u_int f_msgcount; /* references from message queue */
75 int f_usecount; /* number active users */
76 kauth_cred_t f_cred; /* creds associated with descriptor */
77 const struct fileops {
78 int (*fo_read) (struct file *, off_t *, struct uio *,
79 kauth_cred_t, int);
80 int (*fo_write) (struct file *, off_t *, struct uio *,
81 kauth_cred_t, int);
82 int (*fo_ioctl) (struct file *, u_long, void *,
83 struct lwp *);
84 int (*fo_fcntl) (struct file *, u_int, void *,
85 struct lwp *);
86 int (*fo_poll) (struct file *, int, struct lwp *);
87 int (*fo_stat) (struct file *, struct stat *,
88 struct lwp *);
89 int (*fo_close) (struct file *, struct lwp *);
90 int (*fo_kqfilter) (struct file *, struct knote *);
91 } *f_ops;
92 off_t f_offset;
93 void *f_data; /* descriptor data, e.g. vnode/socket */
94 struct simplelock f_slock;
95 };
96
97 #define FIF_WANTCLOSE 0x01 /* a close is waiting for usecount */
98 #define FIF_LARVAL 0x02 /* not fully constructed; don't use */
99 #define FIF_DISCARDED 0x04 /* file is discarded, pending close */
100
101 #define FILE_IS_USABLE(fp) (((fp)->f_iflags & \
102 (FIF_WANTCLOSE|FIF_LARVAL|FIF_DISCARDED)) == 0)
103
104 #define FILE_SET_MATURE(fp) \
105 do { \
106 (fp)->f_iflags &= ~FIF_LARVAL; \
107 } while (/*CONSTCOND*/0)
108
109 #ifdef DIAGNOSTIC
110 #define FILE_USE_CHECK(fp, str) \
111 do { \
112 if ((fp)->f_usecount < 0) \
113 panic(str); \
114 } while (/* CONSTCOND */ 0)
115 #else
116 #define FILE_USE_CHECK(fp, str) /* nothing */
117 #endif
118
119 /*
120 * FILE_USE() must be called with the file lock held.
121 * (Typical usage is: `fp = fd_getfile(..); FILE_USE(fp);'
122 * and fd_getfile() returns the file locked)
123 */
124 #define FILE_USE(fp) \
125 do { \
126 (fp)->f_usecount++; \
127 FILE_USE_CHECK((fp), "f_usecount overflow"); \
128 simple_unlock(&(fp)->f_slock); \
129 } while (/* CONSTCOND */ 0)
130
131 #define FILE_UNUSE_WLOCK(fp, l, havelock) \
132 do { \
133 if (!(havelock)) \
134 simple_lock(&(fp)->f_slock); \
135 if ((fp)->f_iflags & FIF_WANTCLOSE) { \
136 simple_unlock(&(fp)->f_slock); \
137 /* Will drop usecount */ \
138 (void) closef((fp), (l)); \
139 break; \
140 } else { \
141 (fp)->f_usecount--; \
142 FILE_USE_CHECK((fp), "f_usecount underflow"); \
143 } \
144 simple_unlock(&(fp)->f_slock); \
145 } while (/* CONSTCOND */ 0)
146 #define FILE_UNUSE(fp, l) FILE_UNUSE_WLOCK(fp, l, 0)
147 #define FILE_UNUSE_HAVELOCK(fp, l) FILE_UNUSE_WLOCK(fp, l, 1)
148
149 /*
150 * Flags for fo_read and fo_write.
151 */
152 #define FOF_UPDATE_OFFSET 0x01 /* update the file offset */
153
154 LIST_HEAD(filelist, file);
155 extern struct filelist filehead; /* head of list of open files */
156 extern int maxfiles; /* kernel limit on # of open files */
157 extern int nfiles; /* actual number of open files */
158
159 extern const struct fileops vnops; /* vnode operations for files */
160
161 int dofileread(struct lwp *, int, struct file *, void *, size_t,
162 off_t *, int, register_t *);
163 int dofilewrite(struct lwp *, int, struct file *, const void *,
164 size_t, off_t *, int, register_t *);
165
166 int dofilereadv(struct lwp *, int, struct file *,
167 const struct iovec *, int, off_t *, int, register_t *);
168 int dofilewritev(struct lwp *, int, struct file *,
169 const struct iovec *, int, off_t *, int, register_t *);
170
171 int fsetown(struct proc *, pid_t *, int, const void *);
172 int fgetown(struct proc *, pid_t, int, void *);
173 void fownsignal(pid_t, int, int, int, void *);
174
175 int fdclone(struct lwp *, struct file *, int, int, const struct fileops *,
176 void *);
177
178 /* Commonly used fileops */
179 int fnullop_fcntl(struct file *, u_int, void *, struct lwp *);
180 int fnullop_poll(struct file *, int, struct lwp *);
181 int fnullop_kqfilter(struct file *, struct knote *);
182 int fbadop_stat(struct file *, struct stat *, struct lwp *);
183
184 #endif /* _KERNEL */
185
186 #endif /* _SYS_FILE_H_ */
Cache object: 5179d56845a016cb5034f4384e792de6
|