1 /* Declare an access pattern hint for files.
2    Copyright (C) 2010-2023 Free Software Foundation, Inc.
3 
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16 
17 #include <stdio.h>
18 #include <fcntl.h>
19 #include <sys/types.h>
20 
21 /* There are a few hints one can provide, which have the
22    following characteristics on Linux 2.6.31 at least.
23 
24    POSIX_FADV_SEQUENTIAL
25      Doubles the size of read ahead done for file
26    POSIX_FADV_WILLNEED
27      _synchronously_ prepopulate the buffer cache with the file
28    POSIX_FADV_NOREUSE
29      Could lower priority of data in buffer caches,
30      but currently does nothing.
31    POSIX_FADV_DONTNEED
32      Drop the file from cache.
33      Note this is automatically done when files are unlinked.
34 
35    We use this enum "type" both to make it explicit that
36    these options are mutually exclusive, and to discourage
37    the passing of the possibly undefined POSIX_FADV_... values.
38    Note we could #undef the POSIX_FADV_ values, but that would
39    preclude using the posix_fadvise() function with its standard
40    constants. Using posix_fadvise() might be required if the return
41    value is needed, but it must be guarded by appropriate #ifdefs.  */
42 
43 #if HAVE_POSIX_FADVISE
44 typedef enum {
45   FADVISE_NORMAL =     POSIX_FADV_NORMAL,
46   FADVISE_SEQUENTIAL = POSIX_FADV_SEQUENTIAL,
47   FADVISE_NOREUSE =    POSIX_FADV_NOREUSE,
48   FADVISE_DONTNEED =   POSIX_FADV_DONTNEED,
49   FADVISE_WILLNEED =   POSIX_FADV_WILLNEED,
50   FADVISE_RANDOM =     POSIX_FADV_RANDOM
51 } fadvice_t;
52 #else
53 typedef enum {
54   FADVISE_NORMAL,
55   FADVISE_SEQUENTIAL,
56   FADVISE_NOREUSE,
57   FADVISE_DONTNEED,
58   FADVISE_WILLNEED,
59   FADVISE_RANDOM
60 } fadvice_t;
61 #endif
62 
63 /* We ignore any errors as these hints are only advisory.
64    There is the chance one can pass invalid ADVICE, which will
65    not be indicated, but given the simplicity of the interface
66    this is unlikely.  Also not returning errors allows the
67    unconditional passing of descriptors to non standard files,
68    which will just be ignored if unsupported.  */
69 
70 void fdadvise (int fd, off_t offset, off_t len, fadvice_t advice);
71 void fadvise (FILE *fp, fadvice_t advice);
72