]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/sync_file_range.c
xfsprogs: Release v6.7.0
[thirdparty/xfsprogs-dev.git] / io / sync_file_range.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2012 Red Hat, Inc.
4 * All Rights Reserved.
5 */
6
7 #include "command.h"
8 #include "input.h"
9 #include "init.h"
10 #include "io.h"
11
12 static cmdinfo_t sync_range_cmd;
13
14 static void
15 sync_range_help(void)
16 {
17 printf(_(
18 "\n"
19 " Trigger specific writeback commands on a range of the current file\n"
20 "\n"
21 " With no options, the SYNC_FILE_RANGE_WRITE is implied.\n"
22 " -a -- wait for IO to finish after writing (SYNC_FILE_RANGE_WAIT_AFTER).\n"
23 " -b -- wait for IO to finish before writing (SYNC_FILE_RANGE_WAIT_BEFORE).\n"
24 " -w -- write dirty data in range (SYNC_FILE_RANGE_WRITE).\n"
25 "\n"));
26 }
27
28 static int
29 sync_range_f(
30 int argc,
31 char **argv)
32 {
33 off64_t offset = 0, length = 0;
34 int c, sync_mode = 0;
35 size_t blocksize, sectsize;
36
37 while ((c = getopt(argc, argv, "abw")) != EOF) {
38 switch (c) {
39 case 'a':
40 sync_mode = SYNC_FILE_RANGE_WAIT_AFTER;
41 break;
42 case 'b':
43 sync_mode = SYNC_FILE_RANGE_WAIT_BEFORE;
44 break;
45 case 'w':
46 sync_mode = SYNC_FILE_RANGE_WRITE;
47 break;
48 default:
49 return command_usage(&sync_range_cmd);
50 }
51 }
52
53 /* default to just starting writeback on the range */
54 if (!sync_mode)
55 sync_mode = SYNC_FILE_RANGE_WRITE;
56
57 if (optind != argc - 2)
58 return command_usage(&sync_range_cmd);
59 init_cvtnum(&blocksize, &sectsize);
60 offset = cvtnum(blocksize, sectsize, argv[optind]);
61 if (offset < 0) {
62 printf(_("non-numeric offset argument -- %s\n"),
63 argv[optind]);
64 return 0;
65 }
66 optind++;
67 length = cvtnum(blocksize, sectsize, argv[optind]);
68 if (length < 0) {
69 printf(_("non-numeric length argument -- %s\n"),
70 argv[optind]);
71 return 0;
72 }
73
74 if (sync_file_range(file->fd, offset, length, sync_mode) < 0) {
75 perror("sync_file_range");
76 return 0;
77 }
78 return 0;
79 }
80
81 void
82 sync_range_init(void)
83 {
84 sync_range_cmd.name = "sync_range";
85 sync_range_cmd.cfunc = sync_range_f;
86 sync_range_cmd.argmin = 2;
87 sync_range_cmd.argmax = -1;
88 sync_range_cmd.flags = CMD_NOMAP_OK | CMD_FOREIGN_OK;
89 sync_range_cmd.args = _("[-abw] off len");
90 sync_range_cmd.oneline = _("Control writeback on a range of a file");
91 sync_range_cmd.help = sync_range_help;
92
93 add_command(&sync_range_cmd);
94 }