]>
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 | |
7154cc89 | 48 | # define BLKDISCARD _IO(0x12,119) |
d964b669 LC |
49 | #endif |
50 | ||
51 | #ifndef BLKSECDISCARD | |
7154cc89 | 52 | # define BLKSECDISCARD _IO(0x12,125) |
d964b669 LC |
53 | #endif |
54 | ||
7154cc89 KZ |
55 | #ifndef BLKZEROOUT |
56 | # define BLKZEROOUT _IO(0x12,127) | |
57 | #endif | |
58 | ||
59 | enum { | |
60 | ACT_DISCARD = 0, /* default */ | |
61 | ACT_ZEROOUT, | |
62 | ACT_SECURE | |
63 | }; | |
64 | ||
65 | static void print_stats(int act, char *path, uint64_t stats[]) | |
66 | { | |
67 | switch (act) { | |
68 | case ACT_ZEROOUT: | |
69 | printf(_("%s: Zero-filled %" PRIu64 " bytes from the offset %" PRIu64"\n"), \ | |
70 | path, stats[1], stats[0]); | |
71 | break; | |
72 | case ACT_SECURE: | |
73 | case ACT_DISCARD: | |
74 | printf(_("%s: Discarded %" PRIu64 " bytes from the offset %" PRIu64"\n"), \ | |
75 | path, stats[1], stats[0]); | |
76 | break; | |
77 | } | |
78 | } | |
c472a7e3 | 79 | |
6e1eda6f | 80 | static void __attribute__((__noreturn__)) usage(void) |
d964b669 | 81 | { |
6e1eda6f | 82 | FILE *out = stdout; |
d964b669 LC |
83 | fputs(USAGE_HEADER, out); |
84 | fprintf(out, | |
85 | _(" %s [options] <device>\n"), program_invocation_short_name); | |
451dbcfa BS |
86 | |
87 | fputs(USAGE_SEPARATOR, out); | |
88 | fputs(_("Discard the content of sectors on a device.\n"), out); | |
89 | ||
d964b669 | 90 | fputs(USAGE_OPTIONS, out); |
34fed3ff | 91 | fputs(_(" -f, --force disable all checking\n"), out); |
b31fd516 BS |
92 | fputs(_(" -o, --offset <num> offset in bytes to discard from\n"), out); |
93 | fputs(_(" -l, --length <num> length of bytes to discard from the offset\n"), out); | |
94 | fputs(_(" -p, --step <num> size of the discard iterations within the offset\n"), out); | |
95 | fputs(_(" -s, --secure perform secure discard\n"), out); | |
96 | fputs(_(" -z, --zeroout zero-fill rather than discard\n"), out); | |
97 | fputs(_(" -v, --verbose print aligned length and offset\n"), out); | |
98 | ||
d964b669 | 99 | fputs(USAGE_SEPARATOR, out); |
f45f3ec3 | 100 | printf(USAGE_HELP_OPTIONS(21)); |
b31fd516 | 101 | |
f1970cc5 KZ |
102 | fputs(USAGE_ARGUMENTS, out); |
103 | printf(USAGE_ARG_SIZE(_("<num>"))); | |
104 | ||
f45f3ec3 | 105 | printf(USAGE_MAN_TAIL("blkdiscard(8)")); |
6e1eda6f | 106 | exit(EXIT_SUCCESS); |
d964b669 LC |
107 | } |
108 | ||
7154cc89 | 109 | |
d964b669 LC |
110 | int main(int argc, char **argv) |
111 | { | |
112 | char *path; | |
34fed3ff | 113 | int c, fd, verbose = 0, secsize, force = 0; |
c472a7e3 | 114 | uint64_t end, blksize, step, range[2], stats[2]; |
d964b669 | 115 | struct stat sb; |
700031ad | 116 | struct timeval now, last; |
7154cc89 | 117 | int act = ACT_DISCARD; |
d964b669 LC |
118 | |
119 | static const struct option longopts[] = { | |
87918040 SK |
120 | { "help", no_argument, NULL, 'h' }, |
121 | { "version", no_argument, NULL, 'V' }, | |
122 | { "offset", required_argument, NULL, 'o' }, | |
34fed3ff | 123 | { "force", no_argument, NULL, 'f' }, |
87918040 SK |
124 | { "length", required_argument, NULL, 'l' }, |
125 | { "step", required_argument, NULL, 'p' }, | |
126 | { "secure", no_argument, NULL, 's' }, | |
127 | { "verbose", no_argument, NULL, 'v' }, | |
128 | { "zeroout", no_argument, NULL, 'z' }, | |
129 | { NULL, 0, NULL, 0 } | |
d964b669 LC |
130 | }; |
131 | ||
132 | setlocale(LC_ALL, ""); | |
133 | bindtextdomain(PACKAGE, LOCALEDIR); | |
134 | textdomain(PACKAGE); | |
2c308875 | 135 | close_stdout_atexit(); |
d964b669 LC |
136 | |
137 | range[0] = 0; | |
138 | range[1] = ULLONG_MAX; | |
c472a7e3 | 139 | step = 0; |
d964b669 | 140 | |
34fed3ff | 141 | while ((c = getopt_long(argc, argv, "hfVsvo:l:p:z", longopts, NULL)) != -1) { |
d964b669 | 142 | switch(c) { |
34fed3ff KZ |
143 | case 'f': |
144 | force = 1; | |
145 | break; | |
d964b669 LC |
146 | case 'l': |
147 | range[1] = strtosize_or_err(optarg, | |
148 | _("failed to parse length")); | |
149 | break; | |
150 | case 'o': | |
151 | range[0] = strtosize_or_err(optarg, | |
152 | _("failed to parse offset")); | |
153 | break; | |
c472a7e3 FS |
154 | case 'p': |
155 | step = strtosize_or_err(optarg, | |
156 | _("failed to parse step")); | |
157 | break; | |
d964b669 | 158 | case 's': |
7154cc89 | 159 | act = ACT_SECURE; |
d964b669 LC |
160 | break; |
161 | case 'v': | |
162 | verbose = 1; | |
163 | break; | |
7154cc89 KZ |
164 | case 'z': |
165 | act = ACT_ZEROOUT; | |
166 | break; | |
2c308875 KZ |
167 | |
168 | case 'h': | |
169 | usage(); | |
170 | case 'V': | |
171 | print_version(EXIT_SUCCESS); | |
d964b669 | 172 | default: |
677ec86c | 173 | errtryhelp(EXIT_FAILURE); |
d964b669 LC |
174 | } |
175 | } | |
176 | ||
177 | if (optind == argc) | |
4ce393f4 | 178 | errx(EXIT_FAILURE, _("no device specified")); |
d964b669 LC |
179 | |
180 | path = argv[optind++]; | |
181 | ||
182 | if (optind != argc) { | |
183 | warnx(_("unexpected number of arguments")); | |
6e1eda6f | 184 | errtryhelp(EXIT_FAILURE); |
d964b669 LC |
185 | } |
186 | ||
34fed3ff | 187 | fd = open(path, O_WRONLY | (force ? 0 : O_EXCL)); |
d964b669 LC |
188 | if (fd < 0) |
189 | err(EXIT_FAILURE, _("cannot open %s"), path); | |
190 | ||
77751922 | 191 | if (fstat(fd, &sb) == -1) |
fc14ceba | 192 | err(EXIT_FAILURE, _("stat of %s failed"), path); |
77751922 KZ |
193 | if (!S_ISBLK(sb.st_mode)) |
194 | errx(EXIT_FAILURE, _("%s: not a block device"), path); | |
195 | ||
d964b669 LC |
196 | if (ioctl(fd, BLKGETSIZE64, &blksize)) |
197 | err(EXIT_FAILURE, _("%s: BLKGETSIZE64 ioctl failed"), path); | |
d964b669 LC |
198 | if (ioctl(fd, BLKSSZGET, &secsize)) |
199 | err(EXIT_FAILURE, _("%s: BLKSSZGET ioctl failed"), path); | |
200 | ||
d7ce9acb FS |
201 | /* check offset alignment to the sector size */ |
202 | if (range[0] % secsize) | |
203 | errx(EXIT_FAILURE, _("%s: offset %" PRIu64 " is not aligned " | |
204 | "to sector size %i"), path, range[0], secsize); | |
d964b669 LC |
205 | |
206 | /* is the range end behind the end of the device ?*/ | |
303884a4 | 207 | if (range[0] > blksize) |
0c75b6a4 | 208 | errx(EXIT_FAILURE, _("%s: offset is greater than device size"), path); |
d964b669 LC |
209 | end = range[0] + range[1]; |
210 | if (end < range[0] || end > blksize) | |
c472a7e3 FS |
211 | end = blksize; |
212 | ||
213 | range[1] = (step > 0) ? step : end - range[0]; | |
d964b669 | 214 | |
d7ce9acb FS |
215 | /* check length alignment to the sector size */ |
216 | if (range[1] % secsize) | |
217 | errx(EXIT_FAILURE, _("%s: length %" PRIu64 " is not aligned " | |
218 | "to sector size %i"), path, range[1], secsize); | |
219 | ||
c472a7e3 | 220 | stats[0] = range[0], stats[1] = 0; |
700031ad | 221 | gettime_monotonic(&last); |
c472a7e3 | 222 | |
ee24ab6f | 223 | for (/* nothing */; range[0] < end; range[0] += range[1]) { |
c472a7e3 FS |
224 | if (range[0] + range[1] > end) |
225 | range[1] = end - range[0]; | |
226 | ||
7154cc89 KZ |
227 | switch (act) { |
228 | case ACT_ZEROOUT: | |
229 | if (ioctl(fd, BLKZEROOUT, &range)) | |
230 | err(EXIT_FAILURE, _("%s: BLKZEROOUT ioctl failed"), path); | |
231 | break; | |
232 | case ACT_SECURE: | |
c472a7e3 FS |
233 | if (ioctl(fd, BLKSECDISCARD, &range)) |
234 | err(EXIT_FAILURE, _("%s: BLKSECDISCARD ioctl failed"), path); | |
7154cc89 KZ |
235 | break; |
236 | case ACT_DISCARD: | |
c472a7e3 FS |
237 | if (ioctl(fd, BLKDISCARD, &range)) |
238 | err(EXIT_FAILURE, _("%s: BLKDISCARD ioctl failed"), path); | |
7154cc89 | 239 | break; |
c472a7e3 FS |
240 | } |
241 | ||
a3e91e26 RM |
242 | stats[1] += range[1]; |
243 | ||
0e765365 | 244 | /* reporting progress at most once per second */ |
c472a7e3 | 245 | if (verbose && step) { |
700031ad | 246 | gettime_monotonic(&now); |
0e765365 RM |
247 | if (now.tv_sec > last.tv_sec && |
248 | (now.tv_usec >= last.tv_usec || now.tv_sec > last.tv_sec + 1)) { | |
7154cc89 | 249 | print_stats(act, path, stats); |
a3e91e26 | 250 | stats[0] += stats[1], stats[1] = 0; |
c472a7e3 FS |
251 | last = now; |
252 | } | |
253 | } | |
d964b669 LC |
254 | } |
255 | ||
eeae4488 | 256 | if (verbose && stats[1]) |
7154cc89 | 257 | print_stats(act, path, stats); |
d964b669 LC |
258 | |
259 | close(fd); | |
260 | return EXIT_SUCCESS; | |
261 | } |