fstrim.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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"
  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(fstrim, BB_DIR_SBIN, BB_SUID_DROP))
  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. static const struct suffix_mult fstrim_sfx[] = {
  44. { "KiB", 1024 },
  45. { "kiB", 1024 },
  46. { "K", 1024 },
  47. { "k", 1024 },
  48. { "MiB", 1048576 },
  49. { "miB", 1048576 },
  50. { "M", 1048576 },
  51. { "m", 1048576 },
  52. { "GiB", 1073741824 },
  53. { "giB", 1073741824 },
  54. { "G", 1073741824 },
  55. { "g", 1073741824 },
  56. { "KB", 1000 },
  57. { "MB", 1000000 },
  58. { "GB", 1000000000 },
  59. { "", 0 }
  60. };
  61. int fstrim_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  62. int fstrim_main(int argc UNUSED_PARAM, char **argv)
  63. {
  64. struct fstrim_range range;
  65. char *arg_o, *arg_l, *arg_m, *mp;
  66. unsigned opts;
  67. int fd;
  68. enum {
  69. OPT_o = (1 << 0),
  70. OPT_l = (1 << 1),
  71. OPT_m = (1 << 2),
  72. OPT_v = (1 << 3),
  73. };
  74. #if ENABLE_LONG_OPTS
  75. static const char getopt_longopts[] ALIGN1 =
  76. "offset\0" Required_argument "o"
  77. "length\0" Required_argument "l"
  78. "minimum\0" Required_argument "m"
  79. "verbose\0" No_argument "v"
  80. ;
  81. applet_long_options = getopt_longopts;
  82. #endif
  83. opt_complementary = "=1"; /* exactly one non-option arg: the mountpoint */
  84. opts = getopt32(argv, "o:l:m:v", &arg_o, &arg_l, &arg_m);
  85. memset(&range, 0, sizeof(range));
  86. range.len = ULLONG_MAX;
  87. if (opts & OPT_o)
  88. range.start = xatoull_sfx(arg_o, fstrim_sfx);
  89. if (opts & OPT_l)
  90. range.len = xatoull_sfx(arg_l, fstrim_sfx);
  91. if (opts & OPT_m)
  92. range.minlen = xatoull_sfx(arg_m, fstrim_sfx);
  93. mp = argv[optind];
  94. if (find_block_device(mp)) {
  95. fd = xopen_nonblocking(mp);
  96. xioctl(fd, FITRIM, &range);
  97. if (ENABLE_FEATURE_CLEAN_UP)
  98. close(fd);
  99. if (opts & OPT_v)
  100. printf("%s: %llu bytes trimmed\n", mp, (unsigned long long)range.len);
  101. return EXIT_SUCCESS;
  102. }
  103. return EXIT_FAILURE;
  104. }