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