2 * fstrim.c -- discard the part (or whole) of mounted filesystem.
4 * Copyright (C) 2010 Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * This program uses FITRIM ioctl to discard parts or the whole filesystem
20 * online (mounted). You can specify range (start and lenght) to be
21 * discarded, or simply discard while filesystem.
23 * Usage: fstrim [options] <mount point>
37 #include <sys/ioctl.h>
47 #define FITRIM _IOWR('X', 121, struct fstrim_range)
50 const char *program_name = "fstrim";
53 struct fstrim_range *range;
54 char mpoint[PATH_MAX];
58 static void usage(void)
61 "Usage: %s [-s start] [-l length] [-m minimum-extent]"
62 " [-v] {mountpoint}\n\t"
63 "-s Starting Byte to discard from\n\t"
64 "-l Number of Bytes to discard from the start\n\t"
65 "-m Minimum extent length to discard\n\t"
66 "-v Verbose - number of discarded bytes\n",
70 static void err_exit(const char *fmt, ...)
74 vfprintf(stderr, fmt, pvar);
81 * Get the number from argument. It can be number followed by
82 * units: k|K, m|M, g|G, t|T
84 static unsigned long long get_number(char **optarg)
87 unsigned long long number, max;
89 /* get the max to avoid overflow */
90 max = ULLONG_MAX / 1024;
95 err_exit("%s: %s (%s)\n", program_name,
96 strerror(ERANGE), *optarg);
100 number = strtoull(opt, &end , 0);
102 err_exit("%s: %s (%s)\n", program_name,
103 strerror(errno), *optarg);
106 * Convert units to numbers. Fall-through stack is used for units
107 * so absence of breaks is intentional.
110 case 'T': /* terabytes */
113 err_exit("%s: %s (%s)\n", program_name,
114 strerror(ERANGE), *optarg);
116 case 'G': /* gigabytes */
119 err_exit("%s: %s (%s)\n", program_name,
120 strerror(ERANGE), *optarg);
122 case 'M': /* megabytes */
125 err_exit("%s: %s (%s)\n", program_name,
126 strerror(ERANGE), *optarg);
128 case 'K': /* kilobytes */
131 err_exit("%s: %s (%s)\n", program_name,
132 strerror(ERANGE), *optarg);
135 case '\0': /* end of the string */
138 err_exit("%s: %s (%s)\n", program_name,
139 strerror(EINVAL), *optarg);
144 err_exit("%s: %s (%s)\n", program_name,
145 strerror(EINVAL), *optarg);
151 static int parse_opts(int argc, char **argv, struct options *opts)
155 while ((c = getopt(argc, argv, "s:l:m:v")) != EOF) {
157 case 's': /* starting point */
158 opts->range->start = get_number(&optarg);
160 case 'l': /* length */
161 opts->range->len = get_number(&optarg);
163 case 'm': /* minlen */
164 opts->range->minlen = get_number(&optarg);
166 case 'v': /* verbose */
177 int main(int argc, char **argv)
179 struct options *opts;
181 int fd, err = 0, ret = EXIT_FAILURE;
183 opts = malloc(sizeof(struct options));
185 err_exit("%s: malloc(): %s\n", program_name, strerror(errno));
191 strncpy(opts->mpoint, argv[argc - 1], sizeof(opts->mpoint));
193 opts->range = calloc(1, sizeof(struct fstrim_range));
195 fprintf(stderr, "%s: calloc(): %s\n", program_name,
200 opts->range->len = ULLONG_MAX;
203 err = parse_opts(argc, argv, opts);
210 if (strnlen(opts->mpoint, 1) < 1) {
211 fprintf(stderr, "%s: You have to specify mount point.\n",
217 if (stat(opts->mpoint, &sb) == -1) {
218 fprintf(stderr, "%s: %s: %s\n", program_name,
219 opts->mpoint, strerror(errno));
224 if (!S_ISDIR(sb.st_mode)) {
225 fprintf(stderr, "%s: %s: (%s)\n", program_name,
226 opts->mpoint, strerror(ENOTDIR));
231 fd = open(opts->mpoint, O_RDONLY);
233 fprintf(stderr, "%s: open(%s): %s\n", program_name,
234 opts->mpoint, strerror(errno));
238 if (ioctl(fd, FITRIM, opts->range)) {
239 fprintf(stderr, "%s: FSTRIM: %s\n", program_name,
244 if ((opts->verbose) && (opts->range))
245 fprintf(stdout, "%llu Bytes were trimmed\n", (unsigned long long)opts->range->len);