]>
Commit | Line | Data |
---|---|---|
fea681da | 1 | .\" Copyright 2003 Abhijit Menon-Sen <ams@wiw.org> |
26861234 | 2 | .\" and Copyright (C) 2010, 2015, 2017 Michael Kerrisk <mtk.manpages@gmail.com> |
2297bf0e | 3 | .\" |
93015253 | 4 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
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. | |
c13182ef | 13 | .\" |
fea681da MK |
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. | |
c13182ef | 21 | .\" |
fea681da MK |
22 | .\" Formatted or processed versions of this manual, if unaccompanied by |
23 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 24 | .\" %%%LICENSE_END |
fea681da | 25 | .\" |
f21a10c8 | 26 | .\" 2005-04-08 mtk, noted kernel version and added BUGS |
dc30fdc6 | 27 | .\" 2010-10-09, mtk, document arm_fadvise64_64() |
f21a10c8 | 28 | .\" |
734882f4 | 29 | .TH POSIX_FADVISE 2 2017-05-03 "Linux" "Linux Programmer's Manual" |
fea681da MK |
30 | .SH NAME |
31 | posix_fadvise \- predeclare an access pattern for file data | |
32 | .SH SYNOPSIS | |
33 | .nf | |
34 | .B #include <fcntl.h> | |
35 | .sp | |
34e8ac03 MK |
36 | .BI "int posix_fadvise(int " fd ", off_t " offset ", off_t " len \ |
37 | ", int " advice ");" | |
fea681da | 38 | .fi |
9a30939e MK |
39 | .sp |
40 | .ad l | |
41 | .in -4n | |
42 | Feature Test Macro Requirements for glibc (see | |
43 | .BR feature_test_macros (7)): | |
44 | .in | |
45 | .sp | |
46 | .BR posix_fadvise (): | |
47 | .RS 4 | |
a446ac0c | 48 | _POSIX_C_SOURCE\ >=\ 200112L |
9a30939e MK |
49 | .RE |
50 | .ad | |
fea681da | 51 | .SH DESCRIPTION |
60a90ecd MK |
52 | Programs can use |
53 | .BR posix_fadvise () | |
54 | to announce an intention to access | |
fea681da | 55 | file data in a specific pattern in the future, thus allowing the kernel |
d9bfdb9c | 56 | to perform appropriate optimizations. |
fea681da MK |
57 | |
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 | |
c13182ef | 60 | the file if \fIlen\fP is 0) within the file referred to by \fIfd\fP. |
b265f7bb YK |
61 | The \fIadvice\fP is not binding; |
62 | it merely constitutes an expectation on behalf of | |
fea681da MK |
63 | the application. |
64 | ||
65 | Permissible values for \fIadvice\fP include: | |
66 | .TP | |
67 | .B POSIX_FADV_NORMAL | |
68 | Indicates that the application has no advice to give about its access | |
c13182ef MK |
69 | pattern for the specified data. |
70 | If no advice is given for an open file, | |
fea681da MK |
71 | this is the default assumption. |
72 | .TP | |
73 | .B POSIX_FADV_SEQUENTIAL | |
74 | The application expects to access the specified data sequentially (with | |
75 | lower offsets read before higher ones). | |
76 | .TP | |
77 | .B POSIX_FADV_RANDOM | |
78 | The specified data will be accessed in random order. | |
79 | .TP | |
80 | .B POSIX_FADV_NOREUSE | |
81 | The specified data will be accessed only once. | |
a6b80261 MK |
82 | |
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. | |
fea681da MK |
86 | .TP |
87 | .B POSIX_FADV_WILLNEED | |
88 | The specified data will be accessed in the near future. | |
a6b80261 MK |
89 | |
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.) | |
fea681da MK |
96 | .TP |
97 | .B POSIX_FADV_DONTNEED | |
98 | The specified data will not be accessed in the near future. | |
a6b80261 MK |
99 | |
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 | |
103 | files. | |
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 | |
106 | discarded instead. | |
107 | ||
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 | |
111 | .I offset | |
112 | and | |
113 | .I len | |
114 | must be page-aligned. | |
115 | ||
f90b94e3 MK |
116 | The implementation |
117 | .I may | |
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, | |
122 | it should call | |
a6b80261 MK |
123 | .BR fsync (2) |
124 | or | |
125 | .BR fdatasync (2) | |
126 | first. | |
47297adb | 127 | .SH RETURN VALUE |
c13182ef | 128 | On success, zero is returned. |
b857d3da | 129 | On error, an error number is returned. |
fea681da MK |
130 | .SH ERRORS |
131 | .TP | |
132 | .B EBADF | |
133 | The \fIfd\fP argument was not a valid file descriptor. | |
134 | .TP | |
135 | .B EINVAL | |
136 | An invalid value was specified for \fIadvice\fP. | |
137 | .TP | |
138 | .B ESPIPE | |
682edefb | 139 | The specified file descriptor refers to a pipe or FIFO. |
e0f1f176 MK |
140 | .RB ( ESPIPE |
141 | is the error specified by POSIX, | |
77483b7c | 142 | but before kernel version 2.6.16, |
e0f1f176 MK |
143 | .\" commit 87ba81dba431232548ce29d5d224115d0c2355ac |
144 | Linux returned | |
682edefb MK |
145 | .B EINVAL |
146 | in this case.) | |
a1d5f77c | 147 | .SH VERSIONS |
e049eee8 MK |
148 | Kernel support first appeared in Linux 2.5.60; |
149 | the underlying system call is called | |
150 | .BR fadvise64 (). | |
151 | .\" of fadvise64_64() | |
152 | Library support has been provided since glibc version 2.2, | |
153 | via the wrapper function | |
154 | .BR posix_fadvise (). | |
732df53e MK |
155 | |
156 | Since Linux 3.18, | |
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. | |
47297adb | 162 | .SH CONFORMING TO |
fc588289 | 163 | POSIX.1-2001, POSIX.1-2008. |
a1d5f77c MK |
164 | Note that the type of the |
165 | .I len | |
c4bb193f | 166 | argument was changed from |
a1d5f77c MK |
167 | .I size_t |
168 | to | |
169 | .I off_t | |
170 | in POSIX.1-2003 TC1. | |
171 | .SH NOTES | |
fea681da MK |
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. | |
8c450534 | 175 | These changes affect the entire file, not just the specified region |
fea681da | 176 | (but other open file handles to the same file are unaffected). |
38ca1220 MK |
177 | |
178 | The contents of the kernel buffer cache can be cleared via the | |
179 | .IR /proc/sys/vm/drop_caches | |
180 | interface described in | |
181 | .BR proc (5). | |
ba759b3c MK |
182 | |
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 | |
185 | .BR mmap (2), | |
186 | and then applying | |
187 | .BR mincore (2) | |
188 | to the mapping. | |
0722a578 | 189 | .SS C library/kernel differences |
a97b7078 MK |
190 | The name of the wrapper function in the C library is |
191 | .BR posix_fadvise (). | |
192 | The underlying system call is called | |
193 | .BR fadvise64 () | |
194 | (or, on some architectures, | |
195 | .BR fadvise64_64 ()). | |
63ec43ae MK |
196 | .SS Architecture-specific variants |
197 | Some architectures require | |
198 | 64-bit arguments to be aligned in a suitable pair of registers (see | |
199 | .BR syscall (2) | |
200 | for further detail). | |
201 | On such architectures, the call signature of | |
dc30fdc6 | 202 | .BR posix_fadvise () |
63ec43ae MK |
203 | shown in the SYNOPSIS would force |
204 | a register to be wasted as padding between the | |
dc30fdc6 MK |
205 | .I fd |
206 | and | |
500bd052 | 207 | .I offset |
dc30fdc6 | 208 | arguments. |
63ec43ae MK |
209 | Therefore, these architectures define a version of the |
210 | system call that orders the arguments suitably, | |
416d9876 | 211 | but is otherwise exactly the same as |
63ec43ae MK |
212 | .BR posix_fadvise (). |
213 | ||
214 | For example, since Linux 2.6.14, ARM has the following system call: | |
dc30fdc6 MK |
215 | .PP |
216 | .in +4n | |
217 | .nf | |
218 | .BI "long arm_fadvise64_64(int " fd ", int " advice , | |
503979fa | 219 | .BI " loff_t " offset ", loff_t " len ); |
dc30fdc6 MK |
220 | .fi |
221 | .in | |
222 | .PP | |
63ec43ae MK |
223 | These architecture-specific details are generally |
224 | hidden from applications by the glibc | |
225 | .BR posix_fadvise () | |
226 | wrapper function, | |
227 | which invokes the appropriate architecture-specific system call. | |
f21a10c8 | 228 | .SH BUGS |
c13182ef | 229 | In kernels before 2.6.6, if |
f21a10c8 MK |
230 | .I len |
231 | was specified as 0, then this was interpreted literally as "zero bytes", | |
232 | rather than as meaning "all bytes through to the end of the file". | |
47297adb | 233 | .SH SEE ALSO |
4cb046d3 | 234 | .BR fincore (1), |
250d41b9 | 235 | .BR mincore (2), |
ef276d2f | 236 | .BR readahead (2), |