1 .\" $NetBSD: file.9,v 1.12 2009/05/10 14:33:54 elad Exp $
3 .\" Copyright (c) 2002, 2005, 2006 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Gregory McGarry.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
41 .Nd operations on file entries
45 .Fn closef "struct file *fp" "struct lwp *l"
47 .Fn ffree "struct file *fp"
49 .Fn FILE_IS_USABLE "struct file *fp"
51 .Fn FILE_USE "struct file *fp"
53 .Fn FILE_UNUSE "struct file *fp" "struct lwp *l"
55 .Fn FILE_SET_MATURE "struct file *fp"
57 The file descriptor table of a process references a file entry for
58 each file used by the kernel.
61 for details of the file descriptor table.
62 Each file entry is given by:
66 LIST_ENTRY(file) f_list; /* list of active files */
68 int f_iflags; /* internal flags */
69 int f_type; /* descriptor type */
70 u_int f_count; /* reference count */
71 u_int f_msgcount; /* message queue references */
72 int f_usecount; /* number active users */
73 kauth_cred_t f_cred; /* creds associated with descriptor */
75 int (*fo_read)(struct file *fp, off_t *offset,
76 struct uio *uio, kauth_cred_t cred, int flags);
77 int (*fo_write)(struct file *fp, off_t *offset,
78 struct uio *uio, kauth_cred_t cred, int flags);
79 int (*fo_ioctl)(struct file *fp, u_long com, void *data,
81 int (*fo_fcntl)(struct file *fp, u_int com, void *data,
83 int (*fo_poll)(struct file *fp, int events,
85 int (*fo_stat)(struct file *fp, struct stat *sp,
87 int (*fo_close)(struct file *fp, struct lwp *l);
90 void *f_data; /* descriptor data */
95 treats file entries in an object-oriented fashion after they are created.
96 Each entry specifies the object type,
98 which can have the values
104 The file entry also has a pointer to a data structure,
106 that contains information specific to the instance of the underlying object.
107 The data structure is opaque to the routines that manipulate the file entry.
108 Each entry also contains an array of function pointers,
110 that translate the generic operations on a file descriptor into the
111 specific action associated with its type.
112 A reference to the data structure is passed as the first parameter to a
113 function that implements a file operation.
114 The operations that must be implemented for each descriptor type are
115 read, write, ioctl, fcntl, poll, stat, and close.
118 for an overview of the vnode file operations.
119 All state associated with an instance of an object must be stored in
120 that instance's data structure; the underlying objects are not permitted
121 to manipulate the file entry themselves.
123 For data files, the file entry points to a
126 Pipes and sockets do not have data blocks allocated on the disk and
127 are handled by the special-device filesystem that calls appropriate
128 drivers to handle I/O for them.
129 For pipes, the file entry points to a system block that is used during
131 For sockets, the file entry points to a system block that is used in
132 doing interprocess communications.
134 The descriptor table of a process (and thus access to the objects to
135 which the descriptors refer) is inherited from its parent, so several
136 different processes may reference the same file entry.
137 Thus, each file entry has a reference count,
139 Each time a new reference is created, the reference count is incremented.
140 When a descriptor is closed, the reference count is decremented.
141 When the reference count drops to zero, the file entry is freed.
143 Some file descriptor semantics can be altered through the
148 These flags are recorded in
150 member of the file entry.
151 For example, the flags record whether the descriptor is open for
152 reading, writing, or both reading and writing.
153 The following flags and their corresponding
157 .Bl -tag -offset indent -width FNONBLOCK -compact
180 Some additional state-specific flags are recorded in the
183 Valid values include:
185 .Bl -tag -offset indent -width FIF_WANTCLOSE -compact
187 If set, then the reference count on the file is zero, but there were
188 multiple users of the file.
189 This can happen if a file descriptor table is shared by multiple processes.
190 This flag notifies potential users that the file is closing and will
191 prevent them from adding additional uses to the file.
193 The file entry is not fully constructed (mature) and should not be used.
200 system calls do not take an offset in the file as an argument.
201 Instead, each read or write updates the current file offset,
203 in the file according to the number of bytes transferred.
204 Since more than one process may open the same file and each needs its
205 own offset in the file, the offset cannot be stored in the per-object
208 .Bl -tag -width compact
209 .It Fn closef "fp" "l"
212 which decrements the reference count on file entry
216 function release all locks on the file owned by lwp
218 decrements the reference count on the file entry, and invokes
220 to free the file entry.
221 .It Fn ffree "struct file *fp"
224 The file entry was created in
226 .It Fn FILE_IS_USABLE "fp"
227 Ensure that the file entry is useable by ensuring that neither the
228 FIF_WANTCLOSE and FIF_LARVAL flags are not set in
231 Increment the reference count on file entry
233 .It Fn FILE_UNUSE "fp" "l"
234 Decrement the reference count on file entry
239 the file entry is freed.
240 .It Fn FILE_SET_MATURE "fp"
241 Mark the file entry as being fully constructed (mature) by clearing
242 the FIF_LARVAL flag in
246 This section describes places within the
248 source tree where actual code implementing or using file entries
250 All pathnames are relative to
253 The framework for file entry handling is implemented within the file
254 .Pa sys/kern/kern_descrip.c .