]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/posix_fadvise.2
posix_fadvise.2: Reorganize some text
[thirdparty/man-pages.git] / man2 / posix_fadvise.2
CommitLineData
fea681da 1.\" Copyright 2003 Abhijit Menon-Sen <ams@wiw.org>
2297bf0e 2.\"
93015253 3.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
7.\"
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
c13182ef 12.\"
fea681da
MK
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
16.\" the use of the information contained herein. The author(s) may not
17.\" have taken the same level of care in the production of this manual,
18.\" which is licensed free of charge, as they might when working
19.\" professionally.
c13182ef 20.\"
fea681da
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 23.\" %%%LICENSE_END
fea681da 24.\"
f21a10c8 25.\" 2005-04-08 mtk, noted kernel version and added BUGS
dc30fdc6 26.\" 2010-10-09, mtk, document arm_fadvise64_64()
f21a10c8 27.\"
97986708 28.TH POSIX_FADVISE 2 2016-03-15 "Linux" "Linux Programmer's Manual"
fea681da
MK
29.SH NAME
30posix_fadvise \- predeclare an access pattern for file data
31.SH SYNOPSIS
32.nf
33.B #include <fcntl.h>
34.sp
34e8ac03
MK
35.BI "int posix_fadvise(int " fd ", off_t " offset ", off_t " len \
36", int " advice ");"
fea681da 37.fi
9a30939e
MK
38.sp
39.ad l
40.in -4n
41Feature Test Macro Requirements for glibc (see
42.BR feature_test_macros (7)):
43.in
44.sp
45.BR posix_fadvise ():
46.RS 4
a446ac0c 47_POSIX_C_SOURCE\ >=\ 200112L
9a30939e
MK
48.RE
49.ad
fea681da 50.SH DESCRIPTION
60a90ecd
MK
51Programs can use
52.BR posix_fadvise ()
53to announce an intention to access
fea681da 54file data in a specific pattern in the future, thus allowing the kernel
d9bfdb9c 55to perform appropriate optimizations.
fea681da
MK
56
57The \fIadvice\fP applies to a (not necessarily existent) region starting
58at \fIoffset\fP and extending for \fIlen\fP bytes (or until the end of
c13182ef 59the file if \fIlen\fP is 0) within the file referred to by \fIfd\fP.
b265f7bb
YK
60The \fIadvice\fP is not binding;
61it merely constitutes an expectation on behalf of
fea681da
MK
62the application.
63
64Permissible values for \fIadvice\fP include:
65.TP
66.B POSIX_FADV_NORMAL
67Indicates that the application has no advice to give about its access
c13182ef
MK
68pattern for the specified data.
69If no advice is given for an open file,
fea681da
MK
70this is the default assumption.
71.TP
72.B POSIX_FADV_SEQUENTIAL
73The application expects to access the specified data sequentially (with
74lower offsets read before higher ones).
75.TP
76.B POSIX_FADV_RANDOM
77The specified data will be accessed in random order.
78.TP
79.B POSIX_FADV_NOREUSE
80The specified data will be accessed only once.
a6b80261
MK
81
82In kernels before 2.6.18, \fBPOSIX_FADV_NOREUSE\fP had the
83same semantics as \fBPOSIX_FADV_WILLNEED\fP.
84This was probably a bug; since kernel 2.6.18, this flag is a no-op.
fea681da
MK
85.TP
86.B POSIX_FADV_WILLNEED
87The specified data will be accessed in the near future.
a6b80261
MK
88
89\fBPOSIX_FADV_WILLNEED\fP initiates a
90nonblocking read of the specified region into the page cache.
91The amount of data read may be decreased by the kernel depending
92on virtual memory load.
93(A few megabytes will usually be fully satisfied,
94and more is rarely useful.)
fea681da
MK
95.TP
96.B POSIX_FADV_DONTNEED
97The specified data will not be accessed in the near future.
a6b80261
MK
98
99\fBPOSIX_FADV_DONTNEED\fP attempts to free cached pages associated with
100the specified region.
101This is useful, for example, while streaming large
102files.
103A program may periodically request the kernel to free cached data
104that has already been used, so that more useful cached pages are not
105discarded instead.
106
107Requests to discard partial pages are ignored.
108It is preferable to preserve needed data than discard unneeded data.
109If the application requires that data be considered for discarding, then
110.I offset
111and
112.I len
113must be page-aligned.
114
115Pages that have not yet been written out will be unaffected, so if the
116application wishes to guarantee that pages will be released, it should
117call
118.BR fsync (2)
119or
120.BR fdatasync (2)
121first.
47297adb 122.SH RETURN VALUE
c13182ef 123On success, zero is returned.
b857d3da 124On error, an error number is returned.
fea681da
MK
125.SH ERRORS
126.TP
127.B EBADF
128The \fIfd\fP argument was not a valid file descriptor.
129.TP
130.B EINVAL
131An invalid value was specified for \fIadvice\fP.
132.TP
133.B ESPIPE
682edefb 134The specified file descriptor refers to a pipe or FIFO.
e0f1f176
MK
135.RB ( ESPIPE
136is the error specified by POSIX,
77483b7c 137but before kernel version 2.6.16,
e0f1f176
MK
138.\" commit 87ba81dba431232548ce29d5d224115d0c2355ac
139Linux returned
682edefb
MK
140.B EINVAL
141in this case.)
a1d5f77c 142.SH VERSIONS
e049eee8
MK
143Kernel support first appeared in Linux 2.5.60;
144the underlying system call is called
145.BR fadvise64 ().
146.\" of fadvise64_64()
147Library support has been provided since glibc version 2.2,
148via the wrapper function
149.BR posix_fadvise ().
732df53e
MK
150
151Since Linux 3.18,
152.\" commit d3ac21cacc24790eb45d735769f35753f5b56ceb
153support for the underlying system call is optional,
154depending on the setting of the
155.B CONFIG_ADVISE_SYSCALLS
156configuration option.
47297adb 157.SH CONFORMING TO
fc588289 158POSIX.1-2001, POSIX.1-2008.
a1d5f77c
MK
159Note that the type of the
160.I len
c4bb193f 161argument was changed from
a1d5f77c
MK
162.I size_t
163to
164.I off_t
165in POSIX.1-2003 TC1.
166.SH NOTES
fea681da
MK
167Under Linux, \fBPOSIX_FADV_NORMAL\fP sets the readahead window to the
168default size for the backing device; \fBPOSIX_FADV_SEQUENTIAL\fP doubles
169this size, and \fBPOSIX_FADV_RANDOM\fP disables file readahead entirely.
8c450534 170These changes affect the entire file, not just the specified region
fea681da 171(but other open file handles to the same file are unaffected).
0722a578 172.SS C library/kernel differences
a97b7078
MK
173The name of the wrapper function in the C library is
174.BR posix_fadvise ().
175The underlying system call is called
176.BR fadvise64 ()
177(or, on some architectures,
178.BR fadvise64_64 ()).
63ec43ae
MK
179.SS Architecture-specific variants
180Some architectures require
18164-bit arguments to be aligned in a suitable pair of registers (see
182.BR syscall (2)
183for further detail).
184On such architectures, the call signature of
dc30fdc6 185.BR posix_fadvise ()
63ec43ae
MK
186shown in the SYNOPSIS would force
187a register to be wasted as padding between the
dc30fdc6
MK
188.I fd
189and
500bd052 190.I offset
dc30fdc6 191arguments.
63ec43ae
MK
192Therefore, these architectures define a version of the
193system call that orders the arguments suitably,
416d9876 194but is otherwise exactly the same as
63ec43ae
MK
195.BR posix_fadvise ().
196
197For example, since Linux 2.6.14, ARM has the following system call:
dc30fdc6
MK
198.PP
199.in +4n
200.nf
201.BI "long arm_fadvise64_64(int " fd ", int " advice ,
503979fa 202.BI " loff_t " offset ", loff_t " len );
dc30fdc6
MK
203.fi
204.in
205.PP
63ec43ae
MK
206These architecture-specific details are generally
207hidden from applications by the glibc
208.BR posix_fadvise ()
209wrapper function,
210which invokes the appropriate architecture-specific system call.
f21a10c8 211.SH BUGS
c13182ef 212In kernels before 2.6.6, if
f21a10c8
MK
213.I len
214was specified as 0, then this was interpreted literally as "zero bytes",
215rather than as meaning "all bytes through to the end of the file".
47297adb 216.SH SEE ALSO
ef276d2f 217.BR readahead (2),