1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include "libbb.h"
32#include <linux/fs.h>
33
34#ifndef BLKDISCARD
35#define BLKDISCARD 0x1277
36#endif
37#ifndef BLKSECDISCARD
38#define BLKSECDISCARD 0x127d
39#endif
40
41int blkdiscard_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42int blkdiscard_main(int argc UNUSED_PARAM, char **argv)
43{
44 unsigned opts;
45 const char *offset_str = "0";
46 const char *length_str;
47 uint64_t offset;
48 uint64_t length;
49 uint64_t range[2];
50 int fd;
51
52 enum {
53 OPT_OFFSET = (1 << 0),
54 OPT_LENGTH = (1 << 1),
55 OPT_SECURE = (1 << 2),
56 OPT_FORCE = (1 << 3),
57 };
58
59 opts = getopt32(argv, "^" "o:l:sf" "\0" "=1", &offset_str, &length_str);
60 argv += optind;
61
62 fd = xopen(argv[0], O_RDWR|O_EXCL);
63
64
65
66
67
68 offset = xatoull_sfx(offset_str, kMG_suffixes);
69
70 if (opts & OPT_LENGTH)
71 length = xatoull_sfx(length_str, kMG_suffixes);
72 else {
73 xioctl(fd, BLKGETSIZE64, &length);
74 length -= offset;
75 }
76
77 range[0] = offset;
78 range[1] = length;
79 ioctl_or_perror_and_die(fd,
80 (opts & OPT_SECURE) ? BLKSECDISCARD : BLKDISCARD,
81 &range,
82 "%s: %s failed",
83 argv[0],
84 (opts & OPT_SECURE) ? "BLKSECDISCARD" : "BLKDISCARD"
85 );
86
87 if (ENABLE_FEATURE_CLEAN_UP)
88 close(fd);
89
90 return EXIT_SUCCESS;
91}
92