blkdiscard.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Mini blkdiscard implementation for busybox
  3. *
  4. * Copyright (C) 2015 by Ari Sundholm <ari@tuxera.com> and Tuxera Inc.
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  7. */
  8. //config:config BLKDISCARD
  9. //config: bool "blkdiscard (4.3 kb)"
  10. //config: default y
  11. //config: help
  12. //config: blkdiscard discards sectors on a given device.
  13. //applet:IF_BLKDISCARD(APPLET_NOEXEC(blkdiscard, blkdiscard, BB_DIR_USR_BIN, BB_SUID_DROP, blkdiscard))
  14. //kbuild:lib-$(CONFIG_BLKDISCARD) += blkdiscard.o
  15. //usage:#define blkdiscard_trivial_usage
  16. //usage: "[-o OFS] [-l LEN] [-s] DEVICE"
  17. //usage:#define blkdiscard_full_usage "\n\n"
  18. //usage: "Discard sectors on DEVICE\n"
  19. //usage: "\n -o OFS Byte offset into device"
  20. //usage: "\n -l LEN Number of bytes to discard"
  21. //usage: "\n -s Perform a secure discard"
  22. ///////: "\n -f Disable check for mounted filesystem"
  23. //////////////// -f: accepted but is a nop (we do no check anyway)
  24. //usage:
  25. //usage:#define blkdiscard_example_usage
  26. //usage: "$ blkdiscard -o 0 -l 1G /dev/sdb"
  27. #include "libbb.h"
  28. #include <linux/fs.h>
  29. #ifndef BLKDISCARD
  30. #define BLKDISCARD 0x1277
  31. #endif
  32. #ifndef BLKSECDISCARD
  33. #define BLKSECDISCARD 0x127d
  34. #endif
  35. int blkdiscard_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  36. int blkdiscard_main(int argc UNUSED_PARAM, char **argv)
  37. {
  38. unsigned opts;
  39. const char *offset_str = "0";
  40. const char *length_str;
  41. uint64_t offset; /* Leaving these two variables out does not */
  42. uint64_t length; /* shrink code size and hampers readability. */
  43. uint64_t range[2];
  44. int fd;
  45. enum {
  46. OPT_OFFSET = (1 << 0),
  47. OPT_LENGTH = (1 << 1),
  48. OPT_SECURE = (1 << 2),
  49. OPT_FORCE = (1 << 3), //nop
  50. };
  51. opts = getopt32(argv, "^" "o:l:sf" "\0" "=1", &offset_str, &length_str);
  52. argv += optind;
  53. fd = xopen(argv[0], O_RDWR|O_EXCL);
  54. //Why bother, BLK[SEC]DISCARD will fail on non-blockdevs anyway?
  55. // xfstat(fd, &st);
  56. // if (!S_ISBLK(st.st_mode))
  57. // bb_error_msg_and_die("%s: not a block device", argv[0]);
  58. offset = xatoull_sfx(offset_str, kMG_suffixes);
  59. if (opts & OPT_LENGTH)
  60. length = xatoull_sfx(length_str, kMG_suffixes);
  61. else {
  62. xioctl(fd, BLKGETSIZE64, &length);
  63. length -= offset;
  64. }
  65. range[0] = offset;
  66. range[1] = length;
  67. ioctl_or_perror_and_die(fd,
  68. (opts & OPT_SECURE) ? BLKSECDISCARD : BLKDISCARD,
  69. &range,
  70. "%s: %s failed",
  71. argv[0],
  72. (opts & OPT_SECURE) ? "BLKSECDISCARD" : "BLKDISCARD"
  73. );
  74. if (ENABLE_FEATURE_CLEAN_UP)
  75. close(fd);
  76. return EXIT_SUCCESS;
  77. }