]>
Commit | Line | Data |
---|---|---|
d964b669 LC |
1 | /* |
2 | * blkdiscard.c -- discard the part (or whole) of the block device. | |
3 | * | |
4 | * Copyright (C) 2012 Red Hat, Inc. All rights reserved. | |
5 | * Written by Lukas Czerner <lczerner@redhat.com> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | * | |
20 | * This program uses BLKDISCARD ioctl to discard part or the whole block | |
21 | * device if the device supports it. You can specify range (start and | |
22 | * length) to be discarded, or simply discard the whole device. | |
23 | */ | |
24 | ||
25 | ||
26 | #include <string.h> | |
27 | #include <unistd.h> | |
28 | #include <stdlib.h> | |
29 | #include <stdio.h> | |
30 | #include <stdint.h> | |
31 | #include <fcntl.h> | |
32 | #include <limits.h> | |
33 | #include <getopt.h> | |
4f55368c | 34 | #include <time.h> |
d964b669 LC |
35 | |
36 | #include <sys/ioctl.h> | |
37 | #include <sys/stat.h> | |
4f55368c | 38 | #include <sys/time.h> |
d964b669 LC |
39 | #include <linux/fs.h> |
40 | ||
41 | #include "nls.h" | |
42 | #include "strutils.h" | |
43 | #include "c.h" | |
44 | #include "closestream.h" | |
784467ad | 45 | #include "monotonic.h" |
d964b669 LC |
46 | |
47 | #ifndef BLKDISCARD | |
48 | #define BLKDISCARD _IO(0x12,119) | |
49 | #endif | |
50 | ||
51 | #ifndef BLKSECDISCARD | |
52 | #define BLKSECDISCARD _IO(0x12,125) | |
53 | #endif | |
54 | ||
c472a7e3 FS |
55 | #define print_stats(path, stats) \ |
56 | printf(_("%s: Discarded %" PRIu64 " bytes from the " \ | |
57 | "offset %" PRIu64"\n"), path, stats[1], stats[0]); | |
58 | ||
d964b669 LC |
59 | static void __attribute__((__noreturn__)) usage(FILE *out) |
60 | { | |
61 | fputs(USAGE_HEADER, out); | |
62 | fprintf(out, | |
63 | _(" %s [options] <device>\n"), program_invocation_short_name); | |
451dbcfa BS |
64 | |
65 | fputs(USAGE_SEPARATOR, out); | |
66 | fputs(_("Discard the content of sectors on a device.\n"), out); | |
67 | ||
d964b669 LC |
68 | fputs(USAGE_OPTIONS, out); |
69 | fputs(_(" -o, --offset <num> offset in bytes to discard from\n" | |
70 | " -l, --length <num> length of bytes to discard from the offset\n" | |
c472a7e3 | 71 | " -p, --step <num> size of the discard iterations within the offset\n" |
d964b669 LC |
72 | " -s, --secure perform secure discard\n" |
73 | " -v, --verbose print aligned length and offset\n"), | |
74 | out); | |
75 | fputs(USAGE_SEPARATOR, out); | |
76 | fputs(USAGE_HELP, out); | |
77 | fputs(USAGE_VERSION, out); | |
78 | fprintf(out, USAGE_MAN_TAIL("blkdiscard(8)")); | |
79 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
80 | } | |
81 | ||
82 | int main(int argc, char **argv) | |
83 | { | |
84 | char *path; | |
88839459 | 85 | int c, fd, verbose = 0, secure = 0, secsize; |
c472a7e3 | 86 | uint64_t end, blksize, step, range[2], stats[2]; |
d964b669 | 87 | struct stat sb; |
700031ad | 88 | struct timeval now, last; |
d964b669 LC |
89 | |
90 | static const struct option longopts[] = { | |
91 | { "help", 0, 0, 'h' }, | |
92 | { "version", 0, 0, 'V' }, | |
93 | { "offset", 1, 0, 'o' }, | |
94 | { "length", 1, 0, 'l' }, | |
c472a7e3 | 95 | { "step", 1, 0, 'p' }, |
d964b669 LC |
96 | { "secure", 0, 0, 's' }, |
97 | { "verbose", 0, 0, 'v' }, | |
98 | { NULL, 0, 0, 0 } | |
99 | }; | |
100 | ||
101 | setlocale(LC_ALL, ""); | |
102 | bindtextdomain(PACKAGE, LOCALEDIR); | |
103 | textdomain(PACKAGE); | |
104 | atexit(close_stdout); | |
105 | ||
106 | range[0] = 0; | |
107 | range[1] = ULLONG_MAX; | |
c472a7e3 | 108 | step = 0; |
d964b669 | 109 | |
c472a7e3 | 110 | while ((c = getopt_long(argc, argv, "hVsvo:l:p:", longopts, NULL)) != -1) { |
d964b669 LC |
111 | switch(c) { |
112 | case 'h': | |
113 | usage(stdout); | |
114 | break; | |
115 | case 'V': | |
116 | printf(UTIL_LINUX_VERSION); | |
117 | return EXIT_SUCCESS; | |
118 | case 'l': | |
119 | range[1] = strtosize_or_err(optarg, | |
120 | _("failed to parse length")); | |
121 | break; | |
122 | case 'o': | |
123 | range[0] = strtosize_or_err(optarg, | |
124 | _("failed to parse offset")); | |
125 | break; | |
c472a7e3 FS |
126 | case 'p': |
127 | step = strtosize_or_err(optarg, | |
128 | _("failed to parse step")); | |
129 | break; | |
d964b669 LC |
130 | case 's': |
131 | secure = 1; | |
132 | break; | |
133 | case 'v': | |
134 | verbose = 1; | |
135 | break; | |
136 | default: | |
137 | usage(stderr); | |
138 | break; | |
139 | } | |
140 | } | |
141 | ||
142 | if (optind == argc) | |
4ce393f4 | 143 | errx(EXIT_FAILURE, _("no device specified")); |
d964b669 LC |
144 | |
145 | path = argv[optind++]; | |
146 | ||
147 | if (optind != argc) { | |
148 | warnx(_("unexpected number of arguments")); | |
149 | usage(stderr); | |
150 | } | |
151 | ||
d964b669 LC |
152 | fd = open(path, O_WRONLY); |
153 | if (fd < 0) | |
154 | err(EXIT_FAILURE, _("cannot open %s"), path); | |
155 | ||
77751922 | 156 | if (fstat(fd, &sb) == -1) |
fc14ceba | 157 | err(EXIT_FAILURE, _("stat of %s failed"), path); |
77751922 KZ |
158 | if (!S_ISBLK(sb.st_mode)) |
159 | errx(EXIT_FAILURE, _("%s: not a block device"), path); | |
160 | ||
d964b669 LC |
161 | if (ioctl(fd, BLKGETSIZE64, &blksize)) |
162 | err(EXIT_FAILURE, _("%s: BLKGETSIZE64 ioctl failed"), path); | |
d964b669 LC |
163 | if (ioctl(fd, BLKSSZGET, &secsize)) |
164 | err(EXIT_FAILURE, _("%s: BLKSSZGET ioctl failed"), path); | |
165 | ||
d7ce9acb FS |
166 | /* check offset alignment to the sector size */ |
167 | if (range[0] % secsize) | |
168 | errx(EXIT_FAILURE, _("%s: offset %" PRIu64 " is not aligned " | |
169 | "to sector size %i"), path, range[0], secsize); | |
d964b669 LC |
170 | |
171 | /* is the range end behind the end of the device ?*/ | |
303884a4 | 172 | if (range[0] > blksize) |
0c75b6a4 | 173 | errx(EXIT_FAILURE, _("%s: offset is greater than device size"), path); |
d964b669 LC |
174 | end = range[0] + range[1]; |
175 | if (end < range[0] || end > blksize) | |
c472a7e3 FS |
176 | end = blksize; |
177 | ||
178 | range[1] = (step > 0) ? step : end - range[0]; | |
d964b669 | 179 | |
d7ce9acb FS |
180 | /* check length alignment to the sector size */ |
181 | if (range[1] % secsize) | |
182 | errx(EXIT_FAILURE, _("%s: length %" PRIu64 " is not aligned " | |
183 | "to sector size %i"), path, range[1], secsize); | |
184 | ||
c472a7e3 | 185 | stats[0] = range[0], stats[1] = 0; |
700031ad | 186 | gettime_monotonic(&last); |
c472a7e3 | 187 | |
ee24ab6f | 188 | for (/* nothing */; range[0] < end; range[0] += range[1]) { |
c472a7e3 FS |
189 | if (range[0] + range[1] > end) |
190 | range[1] = end - range[0]; | |
191 | ||
192 | if (secure) { | |
193 | if (ioctl(fd, BLKSECDISCARD, &range)) | |
194 | err(EXIT_FAILURE, _("%s: BLKSECDISCARD ioctl failed"), path); | |
195 | } else { | |
196 | if (ioctl(fd, BLKDISCARD, &range)) | |
197 | err(EXIT_FAILURE, _("%s: BLKDISCARD ioctl failed"), path); | |
198 | } | |
199 | ||
a3e91e26 RM |
200 | stats[1] += range[1]; |
201 | ||
0e765365 | 202 | /* reporting progress at most once per second */ |
c472a7e3 | 203 | if (verbose && step) { |
700031ad | 204 | gettime_monotonic(&now); |
0e765365 RM |
205 | if (now.tv_sec > last.tv_sec && |
206 | (now.tv_usec >= last.tv_usec || now.tv_sec > last.tv_sec + 1)) { | |
c472a7e3 | 207 | print_stats(path, stats); |
a3e91e26 | 208 | stats[0] += stats[1], stats[1] = 0; |
c472a7e3 FS |
209 | last = now; |
210 | } | |
211 | } | |
d964b669 LC |
212 | } |
213 | ||
eeae4488 | 214 | if (verbose && stats[1]) |
c472a7e3 | 215 | print_stats(path, stats); |
d964b669 LC |
216 | |
217 | close(fd); | |
218 | return EXIT_SUCCESS; | |
219 | } |