1 .\" Copyright 2003 Abhijit Menon-Sen <ams@wiw.org>
2 .\" and Copyright (C) 2010, 2015, 2017 Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" 2005-04-08 mtk, noted kernel version and added BUGS
27 .\" 2010-10-09, mtk, document arm_fadvise64_64()
29 .TH POSIX_FADVISE 2 2017-09-15 "Linux" "Linux Programmer's Manual"
31 posix_fadvise \- predeclare an access pattern for file data
36 .BI "int posix_fadvise(int " fd ", off_t " offset ", off_t " len \
42 Feature Test Macro Requirements for glibc (see
43 .BR feature_test_macros (7)):
48 _POSIX_C_SOURCE\ >=\ 200112L
54 to announce an intention to access
55 file data in a specific pattern in the future, thus allowing the kernel
56 to perform appropriate optimizations.
58 The \fIadvice\fP applies to a (not necessarily existent) region starting
59 at \fIoffset\fP and extending for \fIlen\fP bytes (or until the end of
60 the file if \fIlen\fP is 0) within the file referred to by \fIfd\fP.
61 The \fIadvice\fP is not binding;
62 it merely constitutes an expectation on behalf of
65 Permissible values for \fIadvice\fP include:
68 Indicates that the application has no advice to give about its access
69 pattern for the specified data.
70 If no advice is given for an open file,
71 this is the default assumption.
73 .B POSIX_FADV_SEQUENTIAL
74 The application expects to access the specified data sequentially (with
75 lower offsets read before higher ones).
78 The specified data will be accessed in random order.
81 The specified data will be accessed only once.
83 In kernels before 2.6.18, \fBPOSIX_FADV_NOREUSE\fP had the
84 same semantics as \fBPOSIX_FADV_WILLNEED\fP.
85 This was probably a bug; since kernel 2.6.18, this flag is a no-op.
87 .B POSIX_FADV_WILLNEED
88 The specified data will be accessed in the near future.
90 \fBPOSIX_FADV_WILLNEED\fP initiates a
91 nonblocking read of the specified region into the page cache.
92 The amount of data read may be decreased by the kernel depending
93 on virtual memory load.
94 (A few megabytes will usually be fully satisfied,
95 and more is rarely useful.)
97 .B POSIX_FADV_DONTNEED
98 The specified data will not be accessed in the near future.
100 \fBPOSIX_FADV_DONTNEED\fP attempts to free cached pages associated with
101 the specified region.
102 This is useful, for example, while streaming large
104 A program may periodically request the kernel to free cached data
105 that has already been used, so that more useful cached pages are not
108 Requests to discard partial pages are ignored.
109 It is preferable to preserve needed data than discard unneeded data.
110 If the application requires that data be considered for discarding, then
114 must be page-aligned.
118 attempt to write back dirty pages in the specified region,
119 but this is not guaranteed.
120 Any unwritten dirty pages will not be freed.
121 If the application wishes to ensure that dirty pages will be released,
128 On success, zero is returned.
129 On error, an error number is returned.
133 The \fIfd\fP argument was not a valid file descriptor.
136 An invalid value was specified for \fIadvice\fP.
139 The specified file descriptor refers to a pipe or FIFO.
141 is the error specified by POSIX,
142 but before kernel version 2.6.16,
143 .\" commit 87ba81dba431232548ce29d5d224115d0c2355ac
148 Kernel support first appeared in Linux 2.5.60;
149 the underlying system call is called
151 .\" of fadvise64_64()
152 Library support has been provided since glibc version 2.2,
153 via the wrapper function
154 .BR posix_fadvise ().
157 .\" commit d3ac21cacc24790eb45d735769f35753f5b56ceb
158 support for the underlying system call is optional,
159 depending on the setting of the
160 .B CONFIG_ADVISE_SYSCALLS
161 configuration option.
163 POSIX.1-2001, POSIX.1-2008.
164 Note that the type of the
166 argument was changed from
172 Under Linux, \fBPOSIX_FADV_NORMAL\fP sets the readahead window to the
173 default size for the backing device; \fBPOSIX_FADV_SEQUENTIAL\fP doubles
174 this size, and \fBPOSIX_FADV_RANDOM\fP disables file readahead entirely.
175 These changes affect the entire file, not just the specified region
176 (but other open file handles to the same file are unaffected).
178 The contents of the kernel buffer cache can be cleared via the
179 .IR /proc/sys/vm/drop_caches
180 interface described in
183 One can obtain a snapshot of which pages of a file are resident
184 in the buffer cache by opening a file, mapping it with
189 .SS C library/kernel differences
190 The name of the wrapper function in the C library is
191 .BR posix_fadvise ().
192 The underlying system call is called
194 (or, on some architectures,
195 .BR fadvise64_64 ());
196 the difference between the two is that the former system call
197 assumes that the type of the \fIlen\fP argument is \fIsize_t\fP,
198 while the latter expects \fIloff_t\fP there.
199 .SS Architecture-specific variants
200 Some architectures require
201 64-bit arguments to be aligned in a suitable pair of registers (see
204 On such architectures, the call signature of
206 shown in the SYNOPSIS would force
207 a register to be wasted as padding between the
212 Therefore, these architectures define a version of the
213 system call that orders the arguments suitably,
214 but is otherwise exactly the same as
215 .BR posix_fadvise ().
217 For example, since Linux 2.6.14, ARM has the following system call:
221 .BI "long arm_fadvise64_64(int " fd ", int " advice ,
222 .BI " loff_t " offset ", loff_t " len );
226 These architecture-specific details are generally
227 hidden from applications by the glibc
230 which invokes the appropriate architecture-specific system call.
232 In kernels before 2.6.6, if
234 was specified as 0, then this was interpreted literally as "zero bytes",
235 rather than as meaning "all bytes through to the end of the file".
240 .BR sync_file_range (2),
241 .BR posix_fallocate (3),
242 .BR posix_madvise (3)