fstrim.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * fstrim.c - discard the part (or whole) of mounted filesystem.
  4. *
  5. * 03 March 2012 - Malek Degachi <malek-degachi@laposte.net>
  6. * Adapted for busybox from util-linux-2.12a.
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. //config:config FSTRIM
  11. //config: bool "fstrim (5.5 kb)"
  12. //config: default y
  13. //config: select PLATFORM_LINUX
  14. //config: help
  15. //config: Discard unused blocks on a mounted filesystem.
  16. //applet:IF_FSTRIM(APPLET_NOEXEC(fstrim, fstrim, BB_DIR_SBIN, BB_SUID_DROP, fstrim))
  17. //kbuild:lib-$(CONFIG_FSTRIM) += fstrim.o
  18. //usage:#define fstrim_trivial_usage
  19. //usage: "[OPTIONS] MOUNTPOINT"
  20. //usage:#define fstrim_full_usage "\n\n"
  21. //usage: IF_LONG_OPTS(
  22. //usage: " -o,--offset OFFSET Offset in bytes to discard from"
  23. //usage: "\n -l,--length LEN Bytes to discard"
  24. //usage: "\n -m,--minimum MIN Minimum extent length"
  25. //usage: "\n -v,--verbose Print number of discarded bytes"
  26. //usage: )
  27. //usage: IF_NOT_LONG_OPTS(
  28. //usage: " -o OFFSET Offset in bytes to discard from"
  29. //usage: "\n -l LEN Bytes to discard"
  30. //usage: "\n -m MIN Minimum extent length"
  31. //usage: "\n -v Print number of discarded bytes"
  32. //usage: )
  33. #include "libbb.h"
  34. #include <linux/fs.h>
  35. #ifndef FITRIM
  36. struct fstrim_range {
  37. uint64_t start;
  38. uint64_t len;
  39. uint64_t minlen;
  40. };
  41. #define FITRIM _IOWR('X', 121, struct fstrim_range)
  42. #endif
  43. int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  44. int fstrim_main(int argc UNUSED_PARAM, char **argv)
  45. {
  46. struct fstrim_range range;
  47. char *arg_o, *arg_l, *arg_m, *mp;
  48. unsigned opts;
  49. int fd;
  50. enum {
  51. OPT_o = (1 << 0),
  52. OPT_l = (1 << 1),
  53. OPT_m = (1 << 2),
  54. OPT_v = (1 << 3),
  55. };
  56. #if ENABLE_LONG_OPTS
  57. static const char fstrim_longopts[] ALIGN1 =
  58. "offset\0" Required_argument "o"
  59. "length\0" Required_argument "l"
  60. "minimum\0" Required_argument "m"
  61. "verbose\0" No_argument "v"
  62. ;
  63. #endif
  64. opts = getopt32long(argv, "^"
  65. "o:l:m:v"
  66. "\0" "=1", fstrim_longopts,
  67. &arg_o, &arg_l, &arg_m
  68. );
  69. memset(&range, 0, sizeof(range));
  70. range.len = ULLONG_MAX;
  71. if (opts & OPT_o)
  72. range.start = xatoull_sfx(arg_o, kmg_i_suffixes);
  73. if (opts & OPT_l)
  74. range.len = xatoull_sfx(arg_l, kmg_i_suffixes);
  75. if (opts & OPT_m)
  76. range.minlen = xatoull_sfx(arg_m, kmg_i_suffixes);
  77. mp = argv[optind];
  78. //Wwhy bother checking that it's a blockdev?
  79. // if (find_block_device(mp)) {
  80. fd = xopen_nonblocking(mp);
  81. /* On ENOTTY error, util-linux 2.31 says:
  82. * "fstrim: FILE: the discard operation is not supported"
  83. */
  84. xioctl(fd, FITRIM, &range);
  85. if (ENABLE_FEATURE_CLEAN_UP)
  86. close(fd);
  87. if (opts & OPT_v)
  88. printf("%s: %llu bytes trimmed\n", mp, (unsigned long long)range.len);
  89. return EXIT_SUCCESS;
  90. // }
  91. return EXIT_FAILURE;
  92. }