xref: /netbsd-src/share/man/man9/file.9 (revision d53aff86f98fae01b5767139f79b8a5d097079cb)
1*d53aff86Swiz.\"     $NetBSD: file.9,v 1.19 2017/04/23 11:37:29 wiz Exp $
2174f011eSgmcgarry.\"
3ff8e3c8cSrpaulo.\" Copyright (c) 2002, 2005, 2006 The NetBSD Foundation, Inc.
4174f011eSgmcgarry.\" All rights reserved.
5174f011eSgmcgarry.\"
6174f011eSgmcgarry.\" This code is derived from software contributed to The NetBSD Foundation
7174f011eSgmcgarry.\" by Gregory McGarry.
8174f011eSgmcgarry.\"
9174f011eSgmcgarry.\" Redistribution and use in source and binary forms, with or without
10174f011eSgmcgarry.\" modification, are permitted provided that the following conditions
11174f011eSgmcgarry.\" are met:
12174f011eSgmcgarry.\" 1. Redistributions of source code must retain the above copyright
13174f011eSgmcgarry.\"    notice, this list of conditions and the following disclaimer.
14174f011eSgmcgarry.\" 2. Redistributions in binary form must reproduce the above copyright
15174f011eSgmcgarry.\"    notice, this list of conditions and the following disclaimer in the
16174f011eSgmcgarry.\"    documentation and/or other materials provided with the distribution.
17174f011eSgmcgarry.\"
18174f011eSgmcgarry.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19174f011eSgmcgarry.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20174f011eSgmcgarry.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21174f011eSgmcgarry.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22174f011eSgmcgarry.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23174f011eSgmcgarry.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24174f011eSgmcgarry.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25174f011eSgmcgarry.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26174f011eSgmcgarry.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27174f011eSgmcgarry.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28174f011eSgmcgarry.\" POSSIBILITY OF SUCH DAMAGE.
29174f011eSgmcgarry.\"
30a0de6c0fSelad.Dd May 17, 2009
31174f011eSgmcgarry.Dt FILE 9
32174f011eSgmcgarry.Os
33174f011eSgmcgarry.Sh NAME
34174f011eSgmcgarry.Nm file ,
35174f011eSgmcgarry.Nm closef ,
36174f011eSgmcgarry.Nm ffree ,
37174f011eSgmcgarry.Nm FILE_IS_USABLE ,
38174f011eSgmcgarry.Nm FILE_USE ,
39174f011eSgmcgarry.Nm FILE_UNUSE ,
40a0de6c0fSelad.Nm FILE_SET_MATURE
41174f011eSgmcgarry.Nd operations on file entries
42174f011eSgmcgarry.Sh SYNOPSIS
43472351e1Swiz.In sys/file.h
44174f011eSgmcgarry.Ft int
457d7fe4b7Srpaulo.Fn closef "struct file *fp" "struct lwp *l"
46174f011eSgmcgarry.Ft void
47174f011eSgmcgarry.Fn ffree "struct file *fp"
48174f011eSgmcgarry.Ft int
49174f011eSgmcgarry.Fn FILE_IS_USABLE "struct file *fp"
50174f011eSgmcgarry.Ft void
51174f011eSgmcgarry.Fn FILE_USE "struct file *fp"
52174f011eSgmcgarry.Ft void
537d7fe4b7Srpaulo.Fn FILE_UNUSE "struct file *fp" "struct lwp *l"
54174f011eSgmcgarry.Ft void
55174f011eSgmcgarry.Fn FILE_SET_MATURE "struct file *fp"
56174f011eSgmcgarry.Sh DESCRIPTION
57174f011eSgmcgarryThe file descriptor table of a process references a file entry for
58152e83aaSwizeach file used by the kernel.
59152e83aaSwizSee
60174f011eSgmcgarry.Xr filedesc 9
61152e83aaSwizfor details of the file descriptor table.
62152e83aaSwizEach file entry is given by:
63174f011eSgmcgarry.Bd -literal
64174f011eSgmcgarrystruct file {
65174f011eSgmcgarry        LIST_ENTRY(file) f_list;        /* list of active files */
66174f011eSgmcgarry        int             f_flag;
67174f011eSgmcgarry        int             f_iflags;       /* internal flags */
68174f011eSgmcgarry        int             f_type;         /* descriptor type */
69174f011eSgmcgarry        u_int           f_count;        /* reference count */
70174f011eSgmcgarry        u_int           f_msgcount;     /* message queue references */
71174f011eSgmcgarry        int             f_usecount;     /* number active users */
729852bfb5Spooka        kauth_cred_t    f_cred;         /* creds associated with descriptor */
73174f011eSgmcgarry        struct fileops {
74174f011eSgmcgarry                int (*fo_read)(struct file *fp, off_t *offset,
759852bfb5Spooka			struct uio *uio, kauth_cred_t cred, int flags);
76174f011eSgmcgarry                int (*fo_write)(struct file *fp, off_t *offset,
779852bfb5Spooka                        struct uio *uio, kauth_cred_t cred, int flags);
78c1a14b53Sdogcow                int (*fo_ioctl)(struct file *fp, u_long com, void *data,
797d7fe4b7Srpaulo			struct lwp *l);
80c1a14b53Sdogcow                int (*fo_fcntl)(struct file *fp, u_int com, void *data,
817d7fe4b7Srpaulo			struct lwp *l);
82174f011eSgmcgarry                int (*fo_poll)(struct file *fp, int events,
837d7fe4b7Srpaulo			struct lwp *l);
84174f011eSgmcgarry                int (*fo_stat)(struct file *fp, struct stat *sp,
857d7fe4b7Srpaulo			struct lwp *l);
867d7fe4b7Srpaulo                int (*fo_close)(struct file *fp, struct lwp *l);
87174f011eSgmcgarry        } *f_ops;
88174f011eSgmcgarry        off_t           f_offset;
89c1a14b53Sdogcow        void         *f_data;         /* descriptor data */
90174f011eSgmcgarry};
91174f011eSgmcgarry.Ed
92174f011eSgmcgarry.Pp
93174f011eSgmcgarry.Nx
94152e83aaSwiztreats file entries in an object-oriented fashion after they are created.
95152e83aaSwizEach entry specifies the object type,
96174f011eSgmcgarry.Em f_type ,
97152e83aaSwizwhich can have the values
98152e83aaSwiz.Dv DTYPE_VNODE ,
99152e83aaSwiz.Dv DTYPE_SOCKET ,
100152e83aaSwiz.Dv DTYPE_PIPE
101152e83aaSwizand
102152e83aaSwiz.Dv DTYPE_MISC .
103152e83aaSwizThe file entry also has a pointer to a data structure,
104174f011eSgmcgarry.Em f_data ,
105152e83aaSwizthat contains information specific to the instance of the underlying object.
106152e83aaSwizThe data structure is opaque to the routines that manipulate the file entry.
107152e83aaSwizEach entry also contains an array of function pointers,
108174f011eSgmcgarry.Em f_ops ,
109174f011eSgmcgarrythat translate the generic operations on a file descriptor into the
110152e83aaSwizspecific action associated with its type.
111152e83aaSwizA reference to the data structure is passed as the first parameter to a
112152e83aaSwizfunction that implements a file operation.
113152e83aaSwizThe operations that must be implemented for each descriptor type are
114152e83aaSwizread, write, ioctl, fcntl, poll, stat, and close.
115fa2f17c9SgmcgarrySee
116fa2f17c9Sgmcgarry.Xr vnfileops 9
117fa2f17c9Sgmcgarryfor an overview of the vnode file operations.
118152e83aaSwizAll state associated with an instance of an object must be stored in
119152e83aaSwizthat instance's data structure; the underlying objects are not permitted
120152e83aaSwizto manipulate the file entry themselves.
121174f011eSgmcgarry.Pp
122174f011eSgmcgarryFor data files, the file entry points to a
123174f011eSgmcgarry.Xr vnode 9
124152e83aaSwizstructure.
125152e83aaSwizPipes and sockets do not have data blocks allocated on the disk and
126152e83aaSwizare handled by the special-device filesystem that calls appropriate
127152e83aaSwizdrivers to handle I/O for them.
128152e83aaSwizFor pipes, the file entry points to a system block that is used during
129152e83aaSwizdata transfer.
130152e83aaSwizFor sockets, the file entry points to a system block that is used in
131152e83aaSwizdoing interprocess communications.
132174f011eSgmcgarry.Pp
133174f011eSgmcgarryThe descriptor table of a process (and thus access to the objects to
134174f011eSgmcgarrywhich the descriptors refer) is inherited from its parent, so several
135152e83aaSwizdifferent processes may reference the same file entry.
136152e83aaSwizThus, each file entry has a reference count,
137174f011eSgmcgarry.Em f_count .
138152e83aaSwizEach time a new reference is created, the reference count is incremented.
139152e83aaSwizWhen a descriptor is closed, the reference count is decremented.
140152e83aaSwizWhen the reference count drops to zero, the file entry is freed.
141174f011eSgmcgarry.Pp
142174f011eSgmcgarrySome file descriptor semantics can be altered through the
143174f011eSgmcgarry.Ar flags
144174f011eSgmcgarryargument to the
145174f011eSgmcgarry.Xr open 2
146152e83aaSwizsystem call.
147152e83aaSwizThese flags are recorded in
148174f011eSgmcgarry.Em f_flags
149152e83aaSwizmember of the file entry.
150152e83aaSwizFor example, the flags record whether the descriptor is open for
151152e83aaSwizreading, writing, or both reading and writing.
152174f011eSgmcgarryThe following flags and their corresponding
153174f011eSgmcgarry.Xr open 2
154174f011eSgmcgarryflags are:
155174f011eSgmcgarry.Pp
156174f011eSgmcgarry.Bl -tag -offset indent -width FNONBLOCK -compact
157174f011eSgmcgarry.It FAPPEND
158152e83aaSwiz.Dv O_APPEND
159174f011eSgmcgarry.It FASYNC
160152e83aaSwiz.Dv O_ASYNC
161174f011eSgmcgarry.It O_FSYNC
162152e83aaSwiz.Dv O_SYNC
163174f011eSgmcgarry.It FNDELAY
164152e83aaSwiz.Dv O_NONBLOCK
165174f011eSgmcgarry.It O_NDELAY
166152e83aaSwiz.Dv O_NONBLOCK
167174f011eSgmcgarry.It FNONBLOCK
168152e83aaSwiz.Dv O_NONBLOCK
169174f011eSgmcgarry.It FFSYNC
170152e83aaSwiz.Dv O_SYNC
171174f011eSgmcgarry.It FDSYNC
172152e83aaSwiz.Dv O_DSYNC
173174f011eSgmcgarry.It FRSYNC
174152e83aaSwiz.Dv O_RSYNC
175174f011eSgmcgarry.It FALTIO
176152e83aaSwiz.Dv O_ALT_IO
177174f011eSgmcgarry.El
178174f011eSgmcgarry.Pp
179174f011eSgmcgarrySome additional state-specific flags are recorded in the
180174f011eSgmcgarry.Em f_iflags
181152e83aaSwizmember.
182152e83aaSwizValid values include:
183174f011eSgmcgarry.Pp
184174f011eSgmcgarry.Bl -tag -offset indent -width FIF_WANTCLOSE -compact
185*d53aff86Swiz.It Dv FIF_WANTCLOSE
186174f011eSgmcgarryIf set, then the reference count on the file is zero, but there were
187152e83aaSwizmultiple users of the file.
188152e83aaSwizThis can happen if a file descriptor table is shared by multiple processes.
189152e83aaSwizThis flag notifies potential users that the file is closing and will
190152e83aaSwizprevent them from adding additional uses to the file.
191*d53aff86Swiz.It Dv FIF_LARVAL
192152e83aaSwizThe file entry is not fully constructed (mature) and should not be used.
193174f011eSgmcgarry.El
194174f011eSgmcgarry.Pp
195174f011eSgmcgarryThe
196174f011eSgmcgarry.Xr read 2
197174f011eSgmcgarryand
198174f011eSgmcgarry.Xr write 2
199174f011eSgmcgarrysystem calls do not take an offset in the file as an argument.
200174f011eSgmcgarryInstead, each read or write updates the current file offset,
201174f011eSgmcgarry.Em f_offset
202152e83aaSwizin the file according to the number of bytes transferred.
203152e83aaSwizSince more than one process may open the same file and each needs its
204152e83aaSwizown offset in the file, the offset cannot be stored in the per-object
205152e83aaSwizdata structure.
206174f011eSgmcgarry.Sh FUNCTIONS
207174f011eSgmcgarry.Bl -tag -width compact
208ff8e3c8cSrpaulo.It Fn closef "fp" "l"
209174f011eSgmcgarryThe internal form of
210174f011eSgmcgarry.Xr close 2
211174f011eSgmcgarrywhich decrements the reference count on file entry
212174f011eSgmcgarry.Fa fp .
213174f011eSgmcgarryThe
214174f011eSgmcgarry.Fn closef
215ff8e3c8cSrpaulofunction  release all locks on the file owned by lwp
216ff8e3c8cSrpaulo.Fa l ,
217174f011eSgmcgarrydecrements the reference count on the file entry, and invokes
218174f011eSgmcgarry.Fn ffree
219174f011eSgmcgarryto free the file entry.
220174f011eSgmcgarry.It Fn ffree "struct file *fp"
221174f011eSgmcgarryFree file entry
222174f011eSgmcgarry.Fa fp .
223174f011eSgmcgarryThe file entry was created in
224174f011eSgmcgarry.Xr falloc 9 .
225174f011eSgmcgarry.It Fn FILE_IS_USABLE "fp"
22640383af6SpgoyetteEnsure that the file entry is usable by ensuring that neither the
227*d53aff86Swiz.Dv FIF_WANTCLOSE
228*d53aff86Swizflag nor the
229*d53aff86Swiz.Dv FIF_LARVAL
230*d53aff86Swizflag is set in
231174f011eSgmcgarry.Em f_iflags .
232174f011eSgmcgarry.It Fn FILE_USE "fp"
233174f011eSgmcgarryIncrement the reference count on file entry
234174f011eSgmcgarry.Fa fp .
235ff8e3c8cSrpaulo.It Fn FILE_UNUSE "fp" "l"
236174f011eSgmcgarryDecrement the reference count on file entry
237174f011eSgmcgarry.Fa fp .
238*d53aff86SwizIf the
239*d53aff86Swiz.Dv FIF_WANTCLOSE
240174f011eSgmcgarryflag is set in
241174f011eSgmcgarry.Em f_iflags ,
242174f011eSgmcgarrythe file entry is freed.
243174f011eSgmcgarry.It Fn FILE_SET_MATURE "fp"
244*d53aff86SwizMark the file entry as being fully constructed (mature) by clearing the
245*d53aff86Swiz.Dv FIF_LARVAL
246*d53aff86Swizflag in
247174f011eSgmcgarry.Em f_iflags .
248174f011eSgmcgarry.El
249174f011eSgmcgarry.Sh CODE REFERENCES
250174f011eSgmcgarryThe framework for file entry handling is implemented within the file
251174f011eSgmcgarry.Pa sys/kern/kern_descrip.c .
252174f011eSgmcgarry.Sh SEE ALSO
253174f011eSgmcgarry.Xr dofileread 9 ,
254174f011eSgmcgarry.Xr filedesc 9 ,
255174f011eSgmcgarry.Xr vnfileops 9 ,
256174f011eSgmcgarry.Xr vnode 9
257