]>
Commit | Line | Data |
---|---|---|
d9e2d0dd LC |
1 | /* |
2 | * fstrim.c -- discard the part (or whole) of mounted filesystem. | |
3 | * | |
4 | * Copyright (C) 2010 Red Hat, Inc. All rights reserved. | |
5 | * Written by Lukas Czerner <lczerner@redhat.com> | |
6 | * Karel Zak <kzak@redhat.com> | |
7 | * | |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | * | |
21 | * | |
22 | * This program uses FITRIM ioctl to discard parts or the whole filesystem | |
23 | * online (mounted). You can specify range (start and length) to be | |
e3d61a45 | 24 | * discarded, or simply discard whole filesystem. |
d9e2d0dd LC |
25 | */ |
26 | ||
27 | #include <string.h> | |
28 | #include <unistd.h> | |
29 | #include <stdlib.h> | |
30 | #include <stdio.h> | |
31 | #include <stdint.h> | |
32 | #include <fcntl.h> | |
33 | #include <limits.h> | |
34 | #include <getopt.h> | |
d9e2d0dd LC |
35 | |
36 | #include <sys/ioctl.h> | |
37 | #include <sys/stat.h> | |
38 | #include <linux/fs.h> | |
39 | ||
40 | #include "nls.h" | |
41 | #include "strutils.h" | |
eb76ca98 | 42 | #include "c.h" |
d9e2d0dd LC |
43 | |
44 | #ifndef FITRIM | |
45 | struct fstrim_range { | |
46 | uint64_t start; | |
47 | uint64_t len; | |
48 | uint64_t minlen; | |
49 | }; | |
50 | #define FITRIM _IOWR('X', 121, struct fstrim_range) | |
51 | #endif | |
52 | ||
53 | static void __attribute__((__noreturn__)) usage(FILE *out) | |
54 | { | |
2daae862 KZ |
55 | fputs(_("\nUsage:\n"), out); |
56 | fprintf(out, | |
57 | _(" %s [options] <mount point>\n"), program_invocation_short_name); | |
d9e2d0dd | 58 | |
2daae862 KZ |
59 | fputs(_("\nOptions:\n"), out); |
60 | fputs(_(" -h, --help this help\n" | |
d9e2d0dd LC |
61 | " -o, --offset <num> offset in bytes to discard from\n" |
62 | " -l, --length <num> length of bytes to discard from the offset\n" | |
63 | " -m, --minimum <num> minimum extent length to discard\n" | |
2daae862 | 64 | " -v, --verbose print number of discarded bytes\n"), out); |
d9e2d0dd | 65 | |
cf48ee22 | 66 | fputs(_("\nFor more information see fstrim(8).\n"), out); |
d9e2d0dd LC |
67 | |
68 | exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS); | |
69 | } | |
70 | ||
71 | int main(int argc, char **argv) | |
72 | { | |
73 | char *path; | |
74 | int c, fd, verbose = 0; | |
75 | struct fstrim_range range; | |
76 | struct stat sb; | |
77 | ||
6c7d5ae9 | 78 | static const struct option longopts[] = { |
d9e2d0dd LC |
79 | { "help", 0, 0, 'h' }, |
80 | { "offset", 1, 0, 'o' }, | |
81 | { "length", 1, 0, 'l' }, | |
82 | { "minimum", 1, 0, 'm' }, | |
83 | { "verbose", 0, 0, 'v' }, | |
84 | { NULL, 0, 0, 0 } | |
85 | }; | |
86 | ||
87 | setlocale(LC_ALL, ""); | |
88 | bindtextdomain(PACKAGE, LOCALEDIR); | |
89 | textdomain(PACKAGE); | |
90 | ||
91 | memset(&range, 0, sizeof(range)); | |
92 | range.len = ULLONG_MAX; | |
93 | ||
94 | while ((c = getopt_long(argc, argv, "ho:l:m:v", longopts, NULL)) != -1) { | |
95 | switch(c) { | |
96 | case 'h': | |
97 | usage(stdout); | |
98 | break; | |
99 | case 'l': | |
fa41c101 | 100 | if (strtosize(optarg, (uint64_t *) &range.len)) |
d9e2d0dd LC |
101 | errx(EXIT_FAILURE, |
102 | _("failed to parse length: %s"), optarg); | |
103 | break; | |
104 | case 'o': | |
fa41c101 | 105 | if (strtosize(optarg, (uint64_t *) &range.start)) |
d9e2d0dd LC |
106 | errx(EXIT_FAILURE, |
107 | _("failed to parse offset: %s"), optarg); | |
108 | break; | |
109 | case 'm': | |
fa41c101 | 110 | if (strtosize(optarg, (uint64_t *) &range.minlen)) |
d9e2d0dd | 111 | errx(EXIT_FAILURE, |
e22d8b95 | 112 | _("failed to parse minimum extent length: %s"), |
d9e2d0dd LC |
113 | optarg); |
114 | break; | |
115 | case 'v': | |
116 | verbose = 1; | |
117 | break; | |
118 | default: | |
119 | usage(stderr); | |
120 | break; | |
121 | } | |
122 | } | |
123 | ||
124 | if (optind == argc) | |
125 | errx(EXIT_FAILURE, _("no mountpoint specified.")); | |
126 | ||
127 | path = argv[optind++]; | |
128 | ||
129 | if (optind != argc) { | |
130 | warnx(_("unexpected number of arguments")); | |
131 | usage(stderr); | |
132 | } | |
133 | ||
134 | if (stat(path, &sb) == -1) | |
135 | err(EXIT_FAILURE, _("%s: stat failed"), path); | |
136 | if (!S_ISDIR(sb.st_mode)) | |
137 | errx(EXIT_FAILURE, _("%s: not a directory"), path); | |
138 | ||
139 | fd = open(path, O_RDONLY); | |
140 | if (fd < 0) | |
141 | err(EXIT_FAILURE, _("%s: open failed"), path); | |
142 | ||
c60103a9 FC |
143 | if (ioctl(fd, FITRIM, &range)) |
144 | err(EXIT_FAILURE, _("%s: FITRIM ioctl failed"), path); | |
145 | ||
d9e2d0dd | 146 | if (verbose) |
f14f02f5 | 147 | /* TRANSLATORS: The standard value here is a very large number. */ |
79d5a3b4 | 148 | printf(_("%s: %" PRIu64 " bytes were trimmed\n"), |
fa41c101 | 149 | path, (uint64_t) range.len); |
d9e2d0dd LC |
150 | close(fd); |
151 | return EXIT_SUCCESS; | |
152 | } |