]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/fadvise.c
61724b5ce246781aea06ed3897fb95bed5e0aad1
[thirdparty/xfsprogs-dev.git] / io / fadvise.c
1 /*
2 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would 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, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <xfs/libxfs.h>
20 #include <xfs/command.h>
21 #include <xfs/input.h>
22 #include "init.h"
23 #include "io.h"
24
25 static cmdinfo_t fadvise_cmd;
26
27 static void
28 fadvise_help(void)
29 {
30 printf(_(
31 "\n"
32 " advise the page cache about expected I/O patterns on the current file\n"
33 "\n"
34 " Modifies kernel page cache behaviour when operating on the current file.\n"
35 " The range arguments are required by some advise commands ([*] below).\n"
36 " With no arguments, the POSIX_FADV_NORMAL advice is implied.\n"
37 " -d -- don't need these pages (POSIX_FADV_DONTNEED) [*]\n"
38 " -n -- data will be accessed once (POSIX_FADV_NOREUSE) [*]\n"
39 " -r -- expect random page references (POSIX_FADV_RANDOM)\n"
40 " -s -- expect sequential page references (POSIX_FADV_SEQUENTIAL)\n"
41 " -w -- will need these pages (POSIX_FADV_WILLNEED) [*]\n"
42 " Notes: these interfaces are not supported in Linux kernels before 2.6.\n"
43 " NORMAL sets the default readahead setting on the file.\n"
44 " RANDOM sets the readahead setting on the file to zero.\n"
45 " SEQUENTIAL sets double the default readahead setting on the file.\n"
46 " WILLNEED and NOREUSE are equivalent, and force the maximum readahead.\n"
47 "\n"));
48 }
49
50 static int
51 fadvise_f(
52 int argc,
53 char **argv)
54 {
55 off64_t offset = 0;
56 size_t length = 0;
57 int c, range = 0, advise = POSIX_FADV_NORMAL;
58
59 while ((c = getopt(argc, argv, "dnrsw")) != EOF) {
60 switch (c) {
61 case 'd': /* Don't need these pages */
62 advise = POSIX_FADV_DONTNEED;
63 range = 1;
64 break;
65 case 'n': /* Data will be accessed once */
66 advise = POSIX_FADV_NOREUSE;
67 range = 1;
68 break;
69 case 'r': /* Expect random page references */
70 advise = POSIX_FADV_RANDOM;
71 range = 0;
72 break;
73 case 's': /* Expect sequential page references */
74 advise = POSIX_FADV_SEQUENTIAL;
75 range = 0;
76 break;
77 case 'w': /* Will need these pages */
78 advise = POSIX_FADV_WILLNEED;
79 range = 1;
80 break;
81 default:
82 return command_usage(&fadvise_cmd);
83 }
84 }
85 if (range) {
86 size_t blocksize, sectsize;
87
88 if (optind != argc - 2)
89 return command_usage(&fadvise_cmd);
90 init_cvtnum(&blocksize, &sectsize);
91 offset = cvtnum(blocksize, sectsize, argv[optind]);
92 if (offset < 0) {
93 printf(_("non-numeric offset argument -- %s\n"),
94 argv[optind]);
95 return 0;
96 }
97 optind++;
98 length = cvtnum(blocksize, sectsize, argv[optind]);
99 if (length < 0) {
100 printf(_("non-numeric length argument -- %s\n"),
101 argv[optind]);
102 return 0;
103 }
104 } else if (optind != argc) {
105 return command_usage(&fadvise_cmd);
106 }
107
108 if (posix_fadvise64(file->fd, offset, length, advise) < 0) {
109 perror("fadvise");
110 return 0;
111 }
112 return 0;
113 }
114
115 void
116 fadvise_init(void)
117 {
118 fadvise_cmd.name = _("fadvise");
119 fadvise_cmd.cfunc = fadvise_f;
120 fadvise_cmd.argmin = 0;
121 fadvise_cmd.argmax = -1;
122 fadvise_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
123 fadvise_cmd.args = _("[-dnrsw] [off len]");
124 fadvise_cmd.oneline = _("advisory commands for sections of a file");
125 fadvise_cmd.help = fadvise_help;
126
127 add_command(&fadvise_cmd);
128 }