]>
| Commit | Line | Data |
|---|---|---|
| d9e2d0dd LC |
1 | /* |
| 2 | * fstrim.c -- discard the part (or whole) of mounted filesystem. | |
| 3 | * | |
| fce05e96 | 4 | * Copyright (C) 2010 Red Hat, Inc. All rights reserved. |
| d9e2d0dd LC |
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 | 25 | */ |
| fce05e96 | 26 | |
| d9e2d0dd LC |
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> | |
| 67f974d4 | 38 | #include <sys/vfs.h> |
| 061d1a51 | 39 | #include <linux/fs.h> |
| a0357292 | 40 | |
| d9e2d0dd | 41 | #include "nls.h" |
| 9995da01 | 42 | #include "xalloc.h" |
| d9e2d0dd | 43 | #include "strutils.h" |
| eb76ca98 | 44 | #include "c.h" |
| efb8854f | 45 | #include "closestream.h" |
| 36c370cb KZ |
46 | #include "pathnames.h" |
| 47 | #include "sysfs.h" | |
| 9995da01 | 48 | #include "optutils.h" |
| 67f974d4 | 49 | #include "statfs_magic.h" |
| 36c370cb KZ |
50 | |
| 51 | #include <libmount.h> | |
| d9e2d0dd | 52 | |
| 43abda66 | 53 | |
| d9e2d0dd LC |
54 | #ifndef FITRIM |
| 55 | struct fstrim_range { | |
| 56 | uint64_t start; | |
| 57 | uint64_t len; | |
| 58 | uint64_t minlen; | |
| 59 | }; | |
| fce05e96 | 60 | #define FITRIM _IOWR('X', 121, struct fstrim_range) |
| d9e2d0dd LC |
61 | #endif |
| 62 | ||
| 72213916 KZ |
63 | struct fstrim_control { |
| 64 | struct fstrim_range range; | |
| b233ae61 | 65 | char *type_pattern; |
| 72213916 KZ |
66 | |
| 67 | unsigned int verbose : 1, | |
| ef89802e | 68 | quiet_unsupp : 1, |
| 72213916 KZ |
69 | dryrun : 1; |
| 70 | }; | |
| 71 | ||
| f227757c KZ |
72 | static int is_directory(const char *path, int silent) |
| 73 | { | |
| 74 | struct stat sb; | |
| 75 | ||
| 76 | if (stat(path, &sb) == -1) { | |
| 77 | if (!silent) | |
| 78 | warn(_("stat of %s failed"), path); | |
| 79 | return 0; | |
| 80 | } | |
| 81 | if (!S_ISDIR(sb.st_mode)) { | |
| 82 | if (!silent) | |
| 83 | warnx(_("%s: not a directory"), path); | |
| 84 | return 0; | |
| 85 | } | |
| 86 | return 1; | |
| 87 | } | |
| 88 | ||
| 36c370cb | 89 | /* returns: 0 = success, 1 = unsupported, < 0 = error */ |
| c5b8909f | 90 | static int fstrim_filesystem(struct fstrim_control *ctl, const char *path, const char *devname) |
| 36c370cb | 91 | { |
| 8302b200 | 92 | int fd = -1, rc; |
| 36c370cb | 93 | struct fstrim_range range; |
| 8302b200 | 94 | char *rpath = realpath(path, NULL); |
| 36c370cb | 95 | |
| 8302b200 WS |
96 | if (!rpath) { |
| 97 | warn(_("cannot get realpath: %s"), path); | |
| 98 | rc = -errno; | |
| 99 | goto done; | |
| 100 | } | |
| 36c370cb | 101 | /* kernel modifies the range */ |
| 72213916 | 102 | memcpy(&range, &ctl->range, sizeof(range)); |
| 36c370cb | 103 | |
| 8302b200 | 104 | fd = open(rpath, O_RDONLY); |
| e612ead9 SK |
105 | if (fd < 0) { |
| 106 | warn(_("cannot open %s"), path); | |
| 289b1533 KZ |
107 | rc = -errno; |
| 108 | goto done; | |
| e612ead9 | 109 | } |
| fda0e2cf | 110 | |
| 72213916 | 111 | if (ctl->dryrun) { |
| c5b8909f KZ |
112 | if (devname) |
| 113 | printf(_("%s: 0 B (dry run) trimmed on %s\n"), path, devname); | |
| 114 | else | |
| 115 | printf(_("%s: 0 B (dry run) trimmed\n"), path); | |
| fda0e2cf KZ |
116 | rc = 0; |
| 117 | goto done; | |
| 118 | } | |
| 119 | ||
| 36c370cb KZ |
120 | errno = 0; |
| 121 | if (ioctl(fd, FITRIM, &range)) { | |
| 374baa6f SK |
122 | switch (errno) { |
| 123 | case EBADF: | |
| 124 | case ENOTTY: | |
| 125 | case EOPNOTSUPP: | |
| 7b8fda2e | 126 | case ENOSYS: |
| 5fcdf204 KZ |
127 | rc = 1; |
| 128 | break; | |
| 374baa6f SK |
129 | default: |
| 130 | rc = -errno; | |
| 131 | } | |
| 132 | if (rc < 0) | |
| 36c370cb | 133 | warn(_("%s: FITRIM ioctl failed"), path); |
| 289b1533 | 134 | goto done; |
| 36c370cb KZ |
135 | } |
| 136 | ||
| 72213916 | 137 | if (ctl->verbose) { |
| 36c370cb KZ |
138 | char *str = size_to_human_string( |
| 139 | SIZE_SUFFIX_3LETTER | SIZE_SUFFIX_SPACE, | |
| 140 | (uint64_t) range.len); | |
| c5b8909f KZ |
141 | if (devname) |
| 142 | /* TRANSLATORS: The standard value here is a very large number. */ | |
| 143 | printf(_("%s: %s (%" PRIu64 " bytes) trimmed on %s\n"), | |
| 144 | path, str, (uint64_t) range.len, devname); | |
| 145 | else | |
| 146 | /* TRANSLATORS: The standard value here is a very large number. */ | |
| 147 | printf(_("%s: %s (%" PRIu64 " bytes) trimmed\n"), | |
| 36c370cb | 148 | path, str, (uint64_t) range.len); |
| c5b8909f | 149 | |
| 36c370cb KZ |
150 | free(str); |
| 151 | } | |
| 289b1533 KZ |
152 | |
| 153 | rc = 0; | |
| 154 | done: | |
| 155 | if (fd >= 0) | |
| 156 | close(fd); | |
| 8302b200 | 157 | free(rpath); |
| 289b1533 | 158 | return rc; |
| 36c370cb KZ |
159 | } |
| 160 | ||
| c69bd2cb | 161 | static int has_discard(const char *devname, struct path_cxt **wholedisk) |
| 36c370cb | 162 | { |
| c69bd2cb | 163 | struct path_cxt *pc = NULL; |
| 36c370cb KZ |
164 | uint64_t dg = 0; |
| 165 | dev_t disk = 0, dev; | |
| fe7d6310 | 166 | int rc = -1, rdonly = 0; |
| 36c370cb | 167 | |
| c69bd2cb | 168 | dev = sysfs_devname_to_devno(devname); |
| 36c370cb | 169 | if (!dev) |
| c69bd2cb KZ |
170 | goto fail; |
| 171 | ||
| 172 | pc = ul_new_sysfs_path(dev, NULL, NULL); | |
| 173 | if (!pc) | |
| 174 | goto fail; | |
| 175 | ||
| 36c370cb KZ |
176 | /* |
| 177 | * This is tricky to read the info from sys/, because the queue | |
| 9e930041 | 178 | * attributes are provided for whole devices (disk) only. We're trying |
| 36c370cb | 179 | * to reuse the whole-disk sysfs context to optimize this stuff (as |
| 0e65dcde | 180 | * system usually have just one disk only). |
| 36c370cb | 181 | */ |
| c69bd2cb KZ |
182 | rc = sysfs_blkdev_get_wholedisk(pc, NULL, 0, &disk); |
| 183 | if (rc != 0 || !disk) | |
| 184 | goto fail; | |
| 185 | ||
| 36c370cb | 186 | if (dev != disk) { |
| c69bd2cb KZ |
187 | /* Partition, try reuse whole-disk context if valid for the |
| 188 | * current device, otherwise create new context for the | |
| 189 | * whole-disk. | |
| 190 | */ | |
| 191 | if (*wholedisk && sysfs_blkdev_get_devno(*wholedisk) != disk) { | |
| 192 | ul_unref_path(*wholedisk); | |
| 193 | *wholedisk = NULL; | |
| 194 | } | |
| 195 | if (!*wholedisk) { | |
| 196 | *wholedisk = ul_new_sysfs_path(disk, NULL, NULL); | |
| 197 | if (!*wholedisk) | |
| 198 | goto fail; | |
| 36c370cb | 199 | } |
| c69bd2cb | 200 | sysfs_blkdev_set_parent(pc, *wholedisk); |
| 36c370cb KZ |
201 | } |
| 202 | ||
| c69bd2cb | 203 | rc = ul_path_read_u64(pc, &dg, "queue/discard_granularity"); |
| fe7d6310 KZ |
204 | if (!rc) |
| 205 | ul_path_scanf(pc, "ro", "%d", &rdonly); | |
| 36c370cb | 206 | |
| c69bd2cb | 207 | ul_unref_path(pc); |
| fe7d6310 | 208 | return rc == 0 && dg > 0 && rdonly == 0; |
| c69bd2cb KZ |
209 | fail: |
| 210 | ul_unref_path(pc); | |
| 211 | return 1; | |
| 36c370cb KZ |
212 | } |
| 213 | ||
| b233ae61 | 214 | static int is_unwanted_fs(struct libmnt_fs *fs, const char *tgt, const char *types) |
| 67f974d4 KZ |
215 | { |
| 216 | struct statfs vfs; | |
| 217 | int fd, rc; | |
| 218 | ||
| 219 | if (mnt_fs_is_pseudofs(fs)) | |
| 220 | return 1; | |
| 221 | if (mnt_fs_is_netfs(fs)) | |
| 222 | return 1; | |
| 223 | if (mnt_fs_is_swaparea(fs)) | |
| 224 | return 1; | |
| 225 | if (mnt_fs_match_fstype(fs, "autofs")) | |
| 226 | return 1; | |
| 227 | if (mnt_fs_match_options(fs, "ro")) | |
| 228 | return 1; | |
| 582eb71d SB |
229 | if (mnt_fs_match_options(fs, "+X-fstrim.notrim")) |
| 230 | return 1; | |
| b233ae61 KZ |
231 | if (types && mnt_fs_match_fstype(fs, types) == 0) |
| 232 | return 1; | |
| 67f974d4 KZ |
233 | |
| 234 | fd = open(tgt, O_PATH); | |
| 1e01704b | 235 | if (fd < 0) |
| 67f974d4 | 236 | return 1; |
| 3f33a29b | 237 | rc = fstatfs(fd, &vfs) != 0 || F_TYPE_EQUAL(vfs.f_type, STATFS_AUTOFS_MAGIC); |
| 67f974d4 | 238 | close(fd); |
| 20af6cee SS |
239 | if (rc) |
| 240 | return 1; | |
| 67f974d4 | 241 | |
| 20af6cee SS |
242 | /* FITRIM on read-only filesystem can fail, and it can fail */ |
| 243 | if (access(tgt, W_OK) != 0) { | |
| 244 | if (errno == EROFS) | |
| 245 | return 1; | |
| 246 | if (errno == EACCES) | |
| 247 | return 1; | |
| 248 | } | |
| 249 | return 0; | |
| 67f974d4 | 250 | } |
| e05a3400 KZ |
251 | |
| 252 | static int uniq_fs_target_cmp( | |
| 253 | struct libmnt_table *tb __attribute__((__unused__)), | |
| 254 | struct libmnt_fs *a, | |
| 255 | struct libmnt_fs *b) | |
| 256 | { | |
| 257 | return !mnt_fs_streq_target(a, mnt_fs_get_target(b)); | |
| 258 | } | |
| 259 | ||
| 8dd51c18 KZ |
260 | static int uniq_fs_source_cmp( |
| 261 | struct libmnt_table *tb __attribute__((__unused__)), | |
| 262 | struct libmnt_fs *a, | |
| 263 | struct libmnt_fs *b) | |
| 264 | { | |
| 8dd51c18 KZ |
265 | if (mnt_fs_is_pseudofs(a) || mnt_fs_is_netfs(a) || |
| 266 | mnt_fs_is_pseudofs(b) || mnt_fs_is_netfs(b)) | |
| 267 | return 1; | |
| 268 | ||
| afa382f2 | 269 | return !mnt_fs_streq_srcpath(a, mnt_fs_get_srcpath(b)); |
| 8dd51c18 KZ |
270 | } |
| 271 | ||
| 36c370cb | 272 | /* |
| 9995da01 KZ |
273 | * -1 = tab empty |
| 274 | * 0 = all success | |
| 36c370cb KZ |
275 | * 32 = all failed |
| 276 | * 64 = some failed, some success | |
| 277 | */ | |
| 9995da01 | 278 | static int fstrim_all_from_file(struct fstrim_control *ctl, const char *filename) |
| 36c370cb KZ |
279 | { |
| 280 | struct libmnt_fs *fs; | |
| 281 | struct libmnt_iter *itr; | |
| 282 | struct libmnt_table *tab; | |
| c5b8909f | 283 | struct libmnt_cache *cache = NULL; |
| c69bd2cb | 284 | struct path_cxt *wholedisk = NULL; |
| 36c370cb | 285 | int cnt = 0, cnt_err = 0; |
| 9995da01 | 286 | int fstab = 0; |
| c5b8909f KZ |
287 | |
| 288 | tab = mnt_new_table_from_file(filename); | |
| 36c370cb | 289 | if (!tab) |
| c5b8909f | 290 | err(MNT_EX_FAIL, _("failed to parse %s"), filename); |
| 36c370cb | 291 | |
| 9995da01 KZ |
292 | if (mnt_table_is_empty(tab)) { |
| 293 | mnt_unref_table(tab); | |
| 294 | return -1; | |
| 295 | } | |
| 296 | ||
| 297 | if (streq_paths(filename, "/etc/fstab")) | |
| 298 | fstab = 1; | |
| 299 | ||
| 8dd51c18 | 300 | /* de-duplicate by mountpoints */ |
| e05a3400 KZ |
301 | mnt_table_uniq_fs(tab, 0, uniq_fs_target_cmp); |
| 302 | ||
| 9995da01 | 303 | if (fstab) { |
| 410d12e5 KZ |
304 | char *rootdev = NULL; |
| 305 | ||
| c5b8909f KZ |
306 | cache = mnt_new_cache(); |
| 307 | if (!cache) | |
| 308 | err(MNT_EX_FAIL, _("failed to initialize libmount cache")); | |
| 410d12e5 | 309 | |
| 9995da01 | 310 | /* Make sure we trim also root FS on fstab */ |
| 410d12e5 KZ |
311 | if (mnt_table_find_target(tab, "/", MNT_ITER_FORWARD) == NULL && |
| 312 | mnt_guess_system_root(0, cache, &rootdev) == 0) { | |
| 313 | ||
| 314 | fs = mnt_new_fs(); | |
| 315 | if (!fs) | |
| 316 | err(MNT_EX_FAIL, _("failed to allocate FS handler")); | |
| 317 | mnt_fs_set_target(fs, "/"); | |
| 318 | mnt_fs_set_source(fs, rootdev); | |
| 319 | mnt_fs_set_fstype(fs, "auto"); | |
| 320 | mnt_table_add_fs(tab, fs); | |
| 321 | mnt_unref_fs(fs); | |
| 322 | fs = NULL; | |
| 323 | } | |
| a887d587 | 324 | free(rootdev); |
| c5b8909f KZ |
325 | } |
| 326 | ||
| 402006fa SB |
327 | itr = mnt_new_iter(MNT_ITER_BACKWARD); |
| 328 | if (!itr) | |
| 329 | err(MNT_EX_FAIL, _("failed to initialize libmount iterator")); | |
| 330 | ||
| 6466959d | 331 | /* Remove useless entries and canonicalize the table */ |
| 36c370cb KZ |
332 | while (mnt_table_next_fs(tab, itr, &fs) == 0) { |
| 333 | const char *src = mnt_fs_get_srcpath(fs), | |
| 334 | *tgt = mnt_fs_get_target(fs); | |
| 20af6cee SS |
335 | char *path; |
| 336 | int rc = 1; | |
| 36c370cb | 337 | |
| b233ae61 | 338 | if (!tgt || is_unwanted_fs(fs, tgt, ctl->type_pattern)) { |
| 402006fa | 339 | mnt_table_remove_fs(tab, fs); |
| c5b8909f | 340 | continue; |
| 402006fa | 341 | } |
| c5b8909f | 342 | |
| 4080be3d | 343 | /* convert LABEL= (etc.) from fstab to paths */ |
| c5b8909f | 344 | if (!src && cache) { |
| c5b8909f KZ |
345 | const char *spec = mnt_fs_get_source(fs); |
| 346 | ||
| 402006fa SB |
347 | if (!spec) { |
| 348 | mnt_table_remove_fs(tab, fs); | |
| c5b8909f | 349 | continue; |
| 402006fa | 350 | } |
| c5b8909f | 351 | src = mnt_resolve_spec(spec, cache); |
| 402006fa | 352 | mnt_fs_set_source(fs, src); |
| c5b8909f KZ |
353 | } |
| 354 | ||
| 402006fa SB |
355 | if (!src || *src != '/') { |
| 356 | mnt_table_remove_fs(tab, fs); | |
| 36c370cb | 357 | continue; |
| 402006fa | 358 | } |
| 36c370cb KZ |
359 | |
| 360 | /* Is it really accessible mountpoint? Not all mountpoints are | |
| 9e930041 | 361 | * accessible (maybe over mounted by another filesystem) */ |
| 36c370cb | 362 | path = mnt_get_mountpoint(tgt); |
| 3e840352 | 363 | if (path && streq_paths(path, tgt)) |
| 36c370cb KZ |
364 | rc = 0; |
| 365 | free(path); | |
| 20af6cee SS |
366 | if (rc) { |
| 367 | mnt_table_remove_fs(tab, fs); | |
| 36c370cb | 368 | continue; /* overlaying mount */ |
| 2d22ac64 SB |
369 | } |
| 370 | ||
| f227757c | 371 | if (!is_directory(tgt, 1) || |
| 20af6cee SS |
372 | !has_discard(src, &wholedisk)) { |
| 373 | mnt_table_remove_fs(tab, fs); | |
| 36c370cb | 374 | continue; |
| 20af6cee SS |
375 | } |
| 376 | } | |
| 377 | ||
| 378 | /* de-duplicate by source */ | |
| 379 | mnt_table_uniq_fs(tab, MNT_UNIQ_FORWARD, uniq_fs_source_cmp); | |
| 380 | ||
| 381 | mnt_reset_iter(itr, MNT_ITER_BACKWARD); | |
| 382 | ||
| 383 | /* Do FITRIM */ | |
| 384 | while (mnt_table_next_fs(tab, itr, &fs) == 0) { | |
| 385 | const char *src = mnt_fs_get_srcpath(fs), | |
| 386 | *tgt = mnt_fs_get_target(fs); | |
| 387 | int rc; | |
| 388 | ||
| 36c370cb KZ |
389 | cnt++; |
| 390 | ||
| 391 | /* | |
| 392 | * We're able to detect that the device supports discard, but | |
| 393 | * things also depend on filesystem or device mapping, for | |
| e1086439 | 394 | * example LUKS (by default) does not support FSTRIM. |
| 36c370cb KZ |
395 | * |
| 396 | * This is reason why we ignore EOPNOTSUPP and ENOTTY errors | |
| 397 | * from discard ioctl. | |
| 398 | */ | |
| 5fcdf204 KZ |
399 | rc = fstrim_filesystem(ctl, tgt, src); |
| 400 | if (rc < 0) | |
| 36c370cb | 401 | cnt_err++; |
| ef89802e | 402 | else if (rc == 1 && !ctl->quiet_unsupp) |
| 5fcdf204 | 403 | warnx(_("%s: the discard operation is not supported"), tgt); |
| 36c370cb | 404 | } |
| 402006fa | 405 | mnt_free_iter(itr); |
| 36c370cb | 406 | |
| c69bd2cb | 407 | ul_unref_path(wholedisk); |
| e05a3400 | 408 | mnt_unref_table(tab); |
| c5b8909f | 409 | mnt_unref_cache(cache); |
| 36c370cb KZ |
410 | |
| 411 | if (cnt && cnt == cnt_err) | |
| 8e9039af | 412 | return MNT_EX_FAIL; /* all failed */ |
| 36c370cb | 413 | if (cnt && cnt_err) |
| 8e9039af | 414 | return MNT_EX_SOMEOK; /* some ok */ |
| 36c370cb | 415 | |
| 7afdbb6f | 416 | return MNT_EX_SUCCESS; |
| 36c370cb KZ |
417 | } |
| 418 | ||
| 9995da01 KZ |
419 | /* |
| 420 | * fstrim --all follows "mount -a" return codes: | |
| 421 | * | |
| 422 | * 0 = all success | |
| 423 | * 32 = all failed | |
| 424 | * 64 = some failed, some success | |
| 425 | */ | |
| 426 | static int fstrim_all(struct fstrim_control *ctl, const char *tabs) | |
| 427 | { | |
| 428 | char *list = xstrdup(tabs); | |
| 429 | char *file; | |
| 430 | int rc = MNT_EX_FAIL; | |
| 431 | ||
| 432 | mnt_init_debug(0); | |
| 433 | ul_path_init_debug(); | |
| 434 | ||
| 435 | for (file = strtok(list, ":"); file; file = strtok(NULL, ":")) { | |
| 436 | struct stat st; | |
| 437 | ||
| 438 | if (stat(file, &st) < 0 || !S_ISREG(st.st_mode)) | |
| 439 | continue; | |
| 440 | ||
| 441 | rc = fstrim_all_from_file(ctl, file); | |
| 442 | if (rc >= 0) | |
| 443 | break; /* stop after first non-empty file */ | |
| 444 | } | |
| 445 | free(list); | |
| 446 | return rc; | |
| 447 | } | |
| 448 | ||
| 6e1eda6f | 449 | static void __attribute__((__noreturn__)) usage(void) |
| d9e2d0dd | 450 | { |
| 6e1eda6f | 451 | FILE *out = stdout; |
| 47a9edbd | 452 | fputs(USAGE_HEADER, out); |
| fce05e96 | 453 | fprintf(out, |
| e27ee134 | 454 | _(" %s [options] -A|-a|<mountpoint>\n"), program_invocation_short_name); |
| 451dbcfa BS |
455 | |
| 456 | fputs(USAGE_SEPARATOR, out); | |
| 457 | fputs(_("Discard unused blocks on a mounted filesystem.\n"), out); | |
| 458 | ||
| fce05e96 | 459 | fputs(USAGE_OPTIONS, out); |
| 9995da01 KZ |
460 | fputs(_(" -a, --all trim mounted filesystems\n"), out); |
| 461 | fputs(_(" -A, --fstab trim filesystems from /etc/fstab\n"), out); | |
| 462 | fputs(_(" -I, --listed-in <list> trim filesystems listed in specified files\n"), out); | |
| ef89802e KZ |
463 | fputs(_(" -o, --offset <num> the offset in bytes to start discarding from\n"), out); |
| 464 | fputs(_(" -l, --length <num> the number of bytes to discard\n"), out); | |
| 465 | fputs(_(" -m, --minimum <num> the minimum extent length to discard\n"), out); | |
| b233ae61 | 466 | fputs(_(" -t, --types <list> limit the set of filesystem types\n"), out); |
| ef89802e KZ |
467 | fputs(_(" -v, --verbose print number of discarded bytes\n"), out); |
| 468 | fputs(_(" --quiet-unsupported suppress error messages if trim unsupported\n"), out); | |
| 469 | fputs(_(" -n, --dry-run does everything, but trim\n"), out); | |
| a60fa93c | 470 | |
| 47a9edbd | 471 | fputs(USAGE_SEPARATOR, out); |
| bad4c729 | 472 | fprintf(out, USAGE_HELP_OPTIONS(21)); |
| f1970cc5 KZ |
473 | |
| 474 | fputs(USAGE_ARGUMENTS, out); | |
| bad4c729 | 475 | fprintf(out, USAGE_ARG_SIZE(_("<num>"))); |
| f1970cc5 | 476 | |
| bad4c729 | 477 | fprintf(out, USAGE_MAN_TAIL("fstrim(8)")); |
| 6e1eda6f | 478 | exit(EXIT_SUCCESS); |
| d9e2d0dd LC |
479 | } |
| 480 | ||
| c84ed54c KZ |
481 | int main(int argc, char **argv) |
| 482 | { | |
| b35c3727 | 483 | char *path = NULL; |
| 9995da01 | 484 | char *tabs = NULL; |
| 72213916 KZ |
485 | int c, rc, all = 0; |
| 486 | struct fstrim_control ctl = { | |
| 487 | .range = { .len = ULLONG_MAX } | |
| 488 | }; | |
| 374baa6f | 489 | enum { |
| ef89802e | 490 | OPT_QUIET_UNSUPP = CHAR_MAX + 1 |
| 374baa6f | 491 | }; |
| c84ed54c | 492 | |
| fce05e96 | 493 | static const struct option longopts[] = { |
| 87918040 | 494 | { "all", no_argument, NULL, 'a' }, |
| c5b8909f | 495 | { "fstab", no_argument, NULL, 'A' }, |
| 87918040 | 496 | { "help", no_argument, NULL, 'h' }, |
| 9995da01 | 497 | { "listed-in", required_argument, NULL, 'I' }, |
| 87918040 SK |
498 | { "version", no_argument, NULL, 'V' }, |
| 499 | { "offset", required_argument, NULL, 'o' }, | |
| 500 | { "length", required_argument, NULL, 'l' }, | |
| 501 | { "minimum", required_argument, NULL, 'm' }, | |
| b233ae61 | 502 | { "types", required_argument, NULL, 't' }, |
| 87918040 | 503 | { "verbose", no_argument, NULL, 'v' }, |
| ef89802e | 504 | { "quiet-unsupported", no_argument, NULL, OPT_QUIET_UNSUPP }, |
| 26b6525a | 505 | { "dry-run", no_argument, NULL, 'n' }, |
| 87918040 | 506 | { NULL, 0, NULL, 0 } |
| d9e2d0dd LC |
507 | }; |
| 508 | ||
| 9995da01 KZ |
509 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
| 510 | { 'A','I','a' }, | |
| 511 | { 0 } | |
| 512 | }; | |
| 513 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
| 514 | ||
| d9e2d0dd LC |
515 | setlocale(LC_ALL, ""); |
| 516 | bindtextdomain(PACKAGE, LOCALEDIR); | |
| 517 | textdomain(PACKAGE); | |
| 2c308875 | 518 | close_stdout_atexit(); |
| d9e2d0dd | 519 | |
| b233ae61 | 520 | while ((c = getopt_long(argc, argv, "AahI:l:m:no:t:Vv", longopts, NULL)) != -1) { |
| 9995da01 KZ |
521 | |
| 522 | err_exclusive_options(c, longopts, excl, excl_st); | |
| 523 | ||
| d9e2d0dd | 524 | switch(c) { |
| c5b8909f | 525 | case 'A': |
| 9995da01 KZ |
526 | all = 1; |
| 527 | tabs = _PATH_MNTTAB; /* fstab */ | |
| 528 | break; | |
| 36c370cb KZ |
529 | case 'a': |
| 530 | all = 1; | |
| 9995da01 KZ |
531 | tabs = _PATH_PROC_MOUNTINFO; /* mountinfo */ |
| 532 | break; | |
| 533 | case 'I': | |
| 534 | all = 1; | |
| 535 | tabs = optarg; | |
| 36c370cb | 536 | break; |
| 26b6525a | 537 | case 'n': |
| 72213916 | 538 | ctl.dryrun = 1; |
| fda0e2cf | 539 | break; |
| d9e2d0dd | 540 | case 'l': |
| 72213916 | 541 | ctl.range.len = strtosize_or_err(optarg, |
| 28c71021 | 542 | _("failed to parse length")); |
| d9e2d0dd LC |
543 | break; |
| 544 | case 'o': | |
| 72213916 | 545 | ctl.range.start = strtosize_or_err(optarg, |
| 28c71021 | 546 | _("failed to parse offset")); |
| d9e2d0dd LC |
547 | break; |
| 548 | case 'm': | |
| 72213916 | 549 | ctl.range.minlen = strtosize_or_err(optarg, |
| 28c71021 | 550 | _("failed to parse minimum extent length")); |
| d9e2d0dd | 551 | break; |
| b233ae61 KZ |
552 | case 't': |
| 553 | ctl.type_pattern = optarg; | |
| 554 | break; | |
| d9e2d0dd | 555 | case 'v': |
| 72213916 | 556 | ctl.verbose = 1; |
| d9e2d0dd | 557 | break; |
| ef89802e KZ |
558 | case OPT_QUIET_UNSUPP: |
| 559 | ctl.quiet_unsupp = 1; | |
| 374baa6f | 560 | break; |
| 2c308875 KZ |
561 | case 'h': |
| 562 | usage(); | |
| 563 | case 'V': | |
| 564 | print_version(EXIT_SUCCESS); | |
| d9e2d0dd | 565 | default: |
| 677ec86c | 566 | errtryhelp(EXIT_FAILURE); |
| d9e2d0dd | 567 | } |
| fce05e96 | 568 | } |
| d9e2d0dd | 569 | |
| 36c370cb KZ |
570 | if (!all) { |
| 571 | if (optind == argc) | |
| 572 | errx(EXIT_FAILURE, _("no mountpoint specified")); | |
| 573 | path = argv[optind++]; | |
| 574 | } | |
| d9e2d0dd LC |
575 | |
| 576 | if (optind != argc) { | |
| 577 | warnx(_("unexpected number of arguments")); | |
| 6e1eda6f | 578 | errtryhelp(EXIT_FAILURE); |
| d9e2d0dd LC |
579 | } |
| 580 | ||
| 36c370cb | 581 | if (all) |
| 9995da01 | 582 | return fstrim_all(&ctl, tabs); /* MNT_EX_* codes */ |
| 36c370cb | 583 | |
| f227757c KZ |
584 | if (!is_directory(path, 0)) |
| 585 | return EXIT_FAILURE; | |
| 586 | ||
| c5b8909f | 587 | rc = fstrim_filesystem(&ctl, path, NULL); |
| 86780859 KZ |
588 | if (rc == 1 && ctl.quiet_unsupp) |
| 589 | rc = 0; | |
| 590 | if (rc == 1) | |
| 7afdbb6f KZ |
591 | warnx(_("%s: the discard operation is not supported"), path); |
| 592 | ||
| 593 | return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; | |
| d9e2d0dd | 594 | } |