blob: 2291eec21d17943b09b5701cf4abc3e668ea37ed [file] [log] [blame]
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +01001/*
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 */
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +01008//config:config BLKDISCARD
Denys Vlasenkob097a842018-12-28 03:20:17 +01009//config: bool "blkdiscard (4.3 kb)"
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010010//config: default y
Samuel Thibault77216c32022-10-16 02:04:59 +020011//config: select PLATFORM_LINUX
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010012//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: blkdiscard discards sectors on a given device.
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010014
Denys Vlasenko277081e2017-08-06 20:20:47 +020015//applet:IF_BLKDISCARD(APPLET_NOEXEC(blkdiscard, blkdiscard, BB_DIR_USR_BIN, BB_SUID_DROP, blkdiscard))
16
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010017//kbuild:lib-$(CONFIG_BLKDISCARD) += blkdiscard.o
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010018
19//usage:#define blkdiscard_trivial_usage
20//usage: "[-o OFS] [-l LEN] [-s] DEVICE"
21//usage:#define blkdiscard_full_usage "\n\n"
Denys Vlasenkoc4213882021-09-10 00:20:05 +020022//usage: "Discard sectors on DEVICE\n"
23//usage: "\n -o OFS Byte offset into device"
24//usage: "\n -l LEN Number of bytes to discard"
25//usage: "\n -s Perform a secure discard"
26///////: "\n -f Disable check for mounted filesystem"
27//////////////// -f: accepted but is a nop (we do no check anyway)
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010028//usage:
29//usage:#define blkdiscard_example_usage
30//usage: "$ blkdiscard -o 0 -l 1G /dev/sdb"
31
32#include "libbb.h"
33#include <linux/fs.h>
34
Denys Vlasenko8ea061e2017-07-15 13:53:41 +020035#ifndef BLKDISCARD
36#define BLKDISCARD 0x1277
37#endif
38#ifndef BLKSECDISCARD
39#define BLKSECDISCARD 0x127d
40#endif
41
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010042int blkdiscard_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
43int blkdiscard_main(int argc UNUSED_PARAM, char **argv)
44{
45 unsigned opts;
46 const char *offset_str = "0";
47 const char *length_str;
48 uint64_t offset; /* Leaving these two variables out does not */
49 uint64_t length; /* shrink code size and hampers readability. */
50 uint64_t range[2];
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010051 int fd;
52
53 enum {
54 OPT_OFFSET = (1 << 0),
55 OPT_LENGTH = (1 << 1),
56 OPT_SECURE = (1 << 2),
Denys Vlasenkoc4213882021-09-10 00:20:05 +020057 OPT_FORCE = (1 << 3), //nop
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010058 };
59
Denys Vlasenkoc4213882021-09-10 00:20:05 +020060 opts = getopt32(argv, "^" "o:l:sf" "\0" "=1", &offset_str, &length_str);
Ari Sundholmdbf5a6d2016-01-02 01:18:32 +010061 argv += optind;
62
63 fd = xopen(argv[0], O_RDWR|O_EXCL);
64//Why bother, BLK[SEC]DISCARD will fail on non-blockdevs anyway?
65// xfstat(fd, &st);
66// if (!S_ISBLK(st.st_mode))
67// bb_error_msg_and_die("%s: not a block device", argv[0]);
68
69 offset = xatoull_sfx(offset_str, kMG_suffixes);
70
71 if (opts & OPT_LENGTH)
72 length = xatoull_sfx(length_str, kMG_suffixes);
73 else {
74 xioctl(fd, BLKGETSIZE64, &length);
75 length -= offset;
76 }
77
78 range[0] = offset;
79 range[1] = length;
80 ioctl_or_perror_and_die(fd,
81 (opts & OPT_SECURE) ? BLKSECDISCARD : BLKDISCARD,
82 &range,
83 "%s: %s failed",
84 argv[0],
85 (opts & OPT_SECURE) ? "BLKSECDISCARD" : "BLKDISCARD"
86 );
87
88 if (ENABLE_FEATURE_CLEAN_UP)
89 close(fd);
90
91 return EXIT_SUCCESS;
92}