1 .\" Copyright 2003 Abhijit Menon-Sen <ams@wiw.org>
2 .\" and Copyright (C) 2010, 2015, 2017 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" 2005-04-08 mtk, noted kernel version and added BUGS
7 .\" 2010-10-09, mtk, document arm_fadvise64_64()
9 .TH posix_fadvise 2 (date) "Linux man-pages (unreleased)"
11 posix_fadvise \- predeclare an access pattern for file data
14 .RI ( libc ", " \-lc )
19 .BI "int posix_fadvise(int " fd ", off_t " offset ", off_t " len \
25 Feature Test Macro Requirements for glibc (see
26 .BR feature_test_macros (7)):
31 _POSIX_C_SOURCE >= 200112L
36 to announce an intention to access
37 file data in a specific pattern in the future, thus allowing the kernel
38 to perform appropriate optimizations.
40 The \fIadvice\fP applies to a (not necessarily existent) region starting
41 at \fIoffset\fP and extending for \fIlen\fP bytes (or until the end of
42 the file if \fIlen\fP is 0) within the file referred to by \fIfd\fP.
43 The \fIadvice\fP is not binding;
44 it merely constitutes an expectation on behalf of
47 Permissible values for \fIadvice\fP include:
50 Indicates that the application has no advice to give about its access
51 pattern for the specified data.
52 If no advice is given for an open file,
53 this is the default assumption.
55 .B POSIX_FADV_SEQUENTIAL
56 The application expects to access the specified data sequentially (with
57 lower offsets read before higher ones).
60 The specified data will be accessed in random order.
63 The specified data will be accessed only once.
65 Before Linux 2.6.18, \fBPOSIX_FADV_NOREUSE\fP had the
66 same semantics as \fBPOSIX_FADV_WILLNEED\fP.
67 This was probably a bug; since Linux 2.6.18, this flag is a no-op.
69 .B POSIX_FADV_WILLNEED
70 The specified data will be accessed in the near future.
72 \fBPOSIX_FADV_WILLNEED\fP initiates a
73 nonblocking read of the specified region into the page cache.
74 The amount of data read may be decreased by the kernel depending
75 on virtual memory load.
76 (A few megabytes will usually be fully satisfied,
77 and more is rarely useful.)
79 .B POSIX_FADV_DONTNEED
80 The specified data will not be accessed in the near future.
82 \fBPOSIX_FADV_DONTNEED\fP attempts to free cached pages associated with
84 This is useful, for example, while streaming large
86 A program may periodically request the kernel to free cached data
87 that has already been used, so that more useful cached pages are not
90 Requests to discard partial pages are ignored.
91 It is preferable to preserve needed data than discard unneeded data.
92 If the application requires that data be considered for discarding, then
100 attempt to write back dirty pages in the specified region,
101 but this is not guaranteed.
102 Any unwritten dirty pages will not be freed.
103 If the application wishes to ensure that dirty pages will be released,
110 On success, zero is returned.
111 On error, an error number is returned.
115 The \fIfd\fP argument was not a valid file descriptor.
118 An invalid value was specified for \fIadvice\fP.
121 The specified file descriptor refers to a pipe or FIFO.
123 is the error specified by POSIX,
124 but before Linux 2.6.16,
125 .\" commit 87ba81dba431232548ce29d5d224115d0c2355ac
130 Under Linux, \fBPOSIX_FADV_NORMAL\fP sets the readahead window to the
131 default size for the backing device; \fBPOSIX_FADV_SEQUENTIAL\fP doubles
132 this size, and \fBPOSIX_FADV_RANDOM\fP disables file readahead entirely.
133 These changes affect the entire file, not just the specified region
134 (but other open file handles to the same file are unaffected).
135 .SS C library/kernel differences
136 The name of the wrapper function in the C library is
137 .BR posix_fadvise ().
138 The underlying system call is called
140 (or, on some architectures,
141 .BR fadvise64_64 ());
142 the difference between the two is that the former system call
143 assumes that the type of the \fIlen\fP argument is \fIsize_t\fP,
144 while the latter expects \fIloff_t\fP there.
145 .SS Architecture-specific variants
146 Some architectures require
147 64-bit arguments to be aligned in a suitable pair of registers (see
150 On such architectures, the call signature of
152 shown in the SYNOPSIS would force
153 a register to be wasted as padding between the
158 Therefore, these architectures define a version of the
159 system call that orders the arguments suitably,
160 but is otherwise exactly the same as
161 .BR posix_fadvise ().
163 For example, since Linux 2.6.14, ARM has the following system call:
167 .BI "long arm_fadvise64_64(int " fd ", int " advice ,
168 .BI " loff_t " offset ", loff_t " len );
172 These architecture-specific details are generally
173 hidden from applications by the glibc
176 which invokes the appropriate architecture-specific system call.
182 Kernel support first appeared in Linux 2.5.60;
183 the underlying system call is called
185 .\" of fadvise64_64()
186 Library support has been provided since glibc 2.2,
187 via the wrapper function
188 .BR posix_fadvise ().
191 .\" commit d3ac21cacc24790eb45d735769f35753f5b56ceb
192 support for the underlying system call is optional,
193 depending on the setting of the
194 .B CONFIG_ADVISE_SYSCALLS
195 configuration option.
199 argument was changed from
205 The contents of the kernel buffer cache can be cleared via the
206 .I /proc/sys/vm/drop_caches
207 interface described in
210 One can obtain a snapshot of which pages of a file are resident
211 in the buffer cache by opening a file, mapping it with
217 Before Linux 2.6.6, if
219 was specified as 0, then this was interpreted literally as "zero bytes",
220 rather than as meaning "all bytes through to the end of the file".
225 .BR sync_file_range (2),
226 .BR posix_fallocate (3),
227 .BR posix_madvise (3)