Changes.old: tfix
[man-pages.git] / man2 / posix_fadvise.2
blob7984f7989de28d33931f16fc1f9f48f05ca0e1ed
1 .\" Copyright 2003 Abhijit Menon-Sen <ams@wiw.org>
2 .\" and Copyright (C) 2010, 2015, 2017 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .\" 2005-04-08 mtk, noted kernel version and added BUGS
7 .\" 2010-10-09, mtk, document arm_fadvise64_64()
8 .\"
9 .TH posix_fadvise 2 (date) "Linux man-pages (unreleased)"
10 .SH NAME
11 posix_fadvise \- predeclare an access pattern for file data
12 .SH LIBRARY
13 Standard C library
14 .RI ( libc ", " \-lc )
15 .SH SYNOPSIS
16 .nf
17 .B #include <fcntl.h>
19 .BI "int posix_fadvise(int " fd ", off_t " offset ", off_t " len \
20 ", int " advice ");"
21 .fi
23 .ad l
24 .RS -4
25 Feature Test Macro Requirements for glibc (see
26 .BR feature_test_macros (7)):
27 .RE
29 .BR posix_fadvise ():
30 .nf
31     _POSIX_C_SOURCE >= 200112L
32 .fi
33 .SH DESCRIPTION
34 Programs can use
35 .BR posix_fadvise ()
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
45 the application.
47 Permissible values for \fIadvice\fP include:
48 .TP
49 .B POSIX_FADV_NORMAL
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.
54 .TP
55 .B POSIX_FADV_SEQUENTIAL
56 The application expects to access the specified data sequentially (with
57 lower offsets read before higher ones).
58 .TP
59 .B POSIX_FADV_RANDOM
60 The specified data will be accessed in random order.
61 .TP
62 .B POSIX_FADV_NOREUSE
63 The specified data will be accessed only once.
64 .IP
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.
68 .TP
69 .B POSIX_FADV_WILLNEED
70 The specified data will be accessed in the near future.
71 .IP
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.)
78 .TP
79 .B POSIX_FADV_DONTNEED
80 The specified data will not be accessed in the near future.
81 .IP
82 \fBPOSIX_FADV_DONTNEED\fP attempts to free cached pages associated with
83 the specified region.
84 This is useful, for example, while streaming large
85 files.
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
88 discarded instead.
89 .IP
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
93 .I offset
94 and
95 .I len
96 must be page-aligned.
97 .IP
98 The implementation
99 .I may
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,
104 it should call
105 .BR fsync (2)
107 .BR fdatasync (2)
108 first.
109 .SH RETURN VALUE
110 On success, zero is returned.
111 On error, an error number is returned.
112 .SH ERRORS
114 .B EBADF
115 The \fIfd\fP argument was not a valid file descriptor.
117 .B EINVAL
118 An invalid value was specified for \fIadvice\fP.
120 .B ESPIPE
121 The specified file descriptor refers to a pipe or FIFO.
122 .RB ( ESPIPE
123 is the error specified by POSIX,
124 but before Linux 2.6.16,
125 .\" commit 87ba81dba431232548ce29d5d224115d0c2355ac
126 Linux returned
127 .B EINVAL
128 in this case.)
129 .SH VERSIONS
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
139 .BR fadvise64 ()
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
148 .BR syscall (2)
149 for further detail).
150 On such architectures, the call signature of
151 .BR posix_fadvise ()
152 shown in the SYNOPSIS would force
153 a register to be wasted as padding between the
154 .I fd
156 .I offset
157 arguments.
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:
165 .in +4n
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
174 .BR posix_fadvise ()
175 wrapper function,
176 which invokes the appropriate architecture-specific system call.
177 .SH STANDARDS
178 POSIX.1-2008.
179 .SH HISTORY
180 POSIX.1-2001.
182 Kernel support first appeared in Linux 2.5.60;
183 the underlying system call is called
184 .BR fadvise64 ().
185 .\" of fadvise64_64()
186 Library support has been provided since glibc 2.2,
187 via the wrapper function
188 .BR posix_fadvise ().
190 Since Linux 3.18,
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.
197 The type of the
198 .I len
199 argument was changed from
200 .I size_t
202 .I off_t
203 in POSIX.1-2001 TC1.
204 .SH NOTES
205 The contents of the kernel buffer cache can be cleared via the
206 .I /proc/sys/vm/drop_caches
207 interface described in
208 .BR proc (5).
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
212 .BR mmap (2),
213 and then applying
214 .BR mincore (2)
215 to the mapping.
216 .SH BUGS
217 Before Linux 2.6.6, if
218 .I len
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".
221 .SH SEE ALSO
222 .BR fincore (1),
223 .BR mincore (2),
224 .BR readahead (2),
225 .BR sync_file_range (2),
226 .BR posix_fallocate (3),
227 .BR posix_madvise (3)