]>
Commit | Line | Data |
---|---|---|
959ef981 | 1 | // SPDX-License-Identifier: GPL-2.0 |
48c46ee3 | 2 | /* |
da23017d NS |
3 | * Copyright (c) 2004-2005 Silicon Graphics, Inc. |
4 | * All Rights Reserved. | |
48c46ee3 NS |
5 | */ |
6 | ||
6b803e5a CH |
7 | #include "command.h" |
8 | #include "input.h" | |
48c46ee3 NS |
9 | #include "init.h" |
10 | #include "io.h" | |
11 | ||
12 | static cmdinfo_t fadvise_cmd; | |
13 | ||
48c46ee3 NS |
14 | static void |
15 | fadvise_help(void) | |
16 | { | |
17 | printf(_( | |
18 | "\n" | |
19 | " advise the page cache about expected I/O patterns on the current file\n" | |
20 | "\n" | |
21 | " Modifies kernel page cache behaviour when operating on the current file.\n" | |
22 | " The range arguments are required by some advise commands ([*] below).\n" | |
23 | " With no arguments, the POSIX_FADV_NORMAL advice is implied.\n" | |
24 | " -d -- don't need these pages (POSIX_FADV_DONTNEED) [*]\n" | |
25 | " -n -- data will be accessed once (POSIX_FADV_NOREUSE) [*]\n" | |
26 | " -r -- expect random page references (POSIX_FADV_RANDOM)\n" | |
27 | " -s -- expect sequential page references (POSIX_FADV_SEQUENTIAL)\n" | |
28 | " -w -- will need these pages (POSIX_FADV_WILLNEED) [*]\n" | |
29 | " Notes: these interfaces are not supported in Linux kernels before 2.6.\n" | |
30 | " NORMAL sets the default readahead setting on the file.\n" | |
31 | " RANDOM sets the readahead setting on the file to zero.\n" | |
32 | " SEQUENTIAL sets double the default readahead setting on the file.\n" | |
33 | " WILLNEED and NOREUSE are equivalent, and force the maximum readahead.\n" | |
34 | "\n")); | |
35 | } | |
36 | ||
37 | static int | |
38 | fadvise_f( | |
39 | int argc, | |
40 | char **argv) | |
41 | { | |
9e726740 | 42 | off_t offset = 0, length = 0; |
48c46ee3 NS |
43 | int c, range = 0, advise = POSIX_FADV_NORMAL; |
44 | ||
45 | while ((c = getopt(argc, argv, "dnrsw")) != EOF) { | |
46 | switch (c) { | |
47 | case 'd': /* Don't need these pages */ | |
48 | advise = POSIX_FADV_DONTNEED; | |
49 | range = 1; | |
50 | break; | |
51 | case 'n': /* Data will be accessed once */ | |
52 | advise = POSIX_FADV_NOREUSE; | |
53 | range = 1; | |
54 | break; | |
55 | case 'r': /* Expect random page references */ | |
56 | advise = POSIX_FADV_RANDOM; | |
57 | range = 0; | |
58 | break; | |
59 | case 's': /* Expect sequential page references */ | |
60 | advise = POSIX_FADV_SEQUENTIAL; | |
61 | range = 0; | |
62 | break; | |
63 | case 'w': /* Will need these pages */ | |
64 | advise = POSIX_FADV_WILLNEED; | |
65 | range = 1; | |
66 | break; | |
67 | default: | |
9e1595e6 | 68 | exitcode = 1; |
48c46ee3 NS |
69 | return command_usage(&fadvise_cmd); |
70 | } | |
71 | } | |
72 | if (range) { | |
2c2f6d79 | 73 | size_t blocksize, sectsize; |
48c46ee3 | 74 | |
9e1595e6 DC |
75 | if (optind != argc - 2) { |
76 | exitcode = 1; | |
48c46ee3 | 77 | return command_usage(&fadvise_cmd); |
9e1595e6 | 78 | } |
48c46ee3 NS |
79 | init_cvtnum(&blocksize, §size); |
80 | offset = cvtnum(blocksize, sectsize, argv[optind]); | |
81 | if (offset < 0) { | |
82 | printf(_("non-numeric offset argument -- %s\n"), | |
83 | argv[optind]); | |
9e1595e6 | 84 | exitcode = 1; |
48c46ee3 NS |
85 | return 0; |
86 | } | |
87 | optind++; | |
88 | length = cvtnum(blocksize, sectsize, argv[optind]); | |
89 | if (length < 0) { | |
90 | printf(_("non-numeric length argument -- %s\n"), | |
91 | argv[optind]); | |
9e1595e6 | 92 | exitcode = 1; |
48c46ee3 NS |
93 | return 0; |
94 | } | |
95 | } else if (optind != argc) { | |
9e1595e6 | 96 | exitcode = 1; |
48c46ee3 NS |
97 | return command_usage(&fadvise_cmd); |
98 | } | |
99 | ||
f8edba5a | 100 | if (posix_fadvise(file->fd, offset, length, advise) < 0) { |
48c46ee3 | 101 | perror("fadvise"); |
9e1595e6 | 102 | exitcode = 1; |
48c46ee3 NS |
103 | return 0; |
104 | } | |
105 | return 0; | |
106 | } | |
107 | ||
108 | void | |
109 | fadvise_init(void) | |
110 | { | |
ad765595 | 111 | fadvise_cmd.name = "fadvise"; |
48c46ee3 NS |
112 | fadvise_cmd.cfunc = fadvise_f; |
113 | fadvise_cmd.argmin = 0; | |
114 | fadvise_cmd.argmax = -1; | |
115 | fadvise_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK; | |
116 | fadvise_cmd.args = _("[-dnrsw] [off len]"); | |
117 | fadvise_cmd.oneline = _("advisory commands for sections of a file"); | |
118 | fadvise_cmd.help = fadvise_help; | |
119 | ||
120 | add_command(&fadvise_cmd); | |
121 | } |