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