2016-01-02 05:48:32 +05:30
|
|
|
/*
|
|
|
|
* Mini blkdiscard implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 by Ari Sundholm <ari@tuxera.com> and Tuxera Inc.
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
|
|
|
*/
|
|
|
|
//config:config BLKDISCARD
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "blkdiscard (4.3 kb)"
|
2016-01-02 05:48:32 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: blkdiscard discards sectors on a given device.
|
2016-01-02 05:48:32 +05:30
|
|
|
|
2017-08-06 23:50:47 +05:30
|
|
|
//applet:IF_BLKDISCARD(APPLET_NOEXEC(blkdiscard, blkdiscard, BB_DIR_USR_BIN, BB_SUID_DROP, blkdiscard))
|
|
|
|
|
2016-01-02 05:48:32 +05:30
|
|
|
//kbuild:lib-$(CONFIG_BLKDISCARD) += blkdiscard.o
|
|
|
|
|
|
|
|
//usage:#define blkdiscard_trivial_usage
|
|
|
|
//usage: "[-o OFS] [-l LEN] [-s] DEVICE"
|
|
|
|
//usage:#define blkdiscard_full_usage "\n\n"
|
2021-09-10 03:50:05 +05:30
|
|
|
//usage: "Discard sectors on DEVICE\n"
|
|
|
|
//usage: "\n -o OFS Byte offset into device"
|
|
|
|
//usage: "\n -l LEN Number of bytes to discard"
|
|
|
|
//usage: "\n -s Perform a secure discard"
|
|
|
|
///////: "\n -f Disable check for mounted filesystem"
|
|
|
|
//////////////// -f: accepted but is a nop (we do no check anyway)
|
2016-01-02 05:48:32 +05:30
|
|
|
//usage:
|
|
|
|
//usage:#define blkdiscard_example_usage
|
|
|
|
//usage: "$ blkdiscard -o 0 -l 1G /dev/sdb"
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
#include <linux/fs.h>
|
|
|
|
|
2017-07-15 17:23:41 +05:30
|
|
|
#ifndef BLKDISCARD
|
|
|
|
#define BLKDISCARD 0x1277
|
|
|
|
#endif
|
|
|
|
#ifndef BLKSECDISCARD
|
|
|
|
#define BLKSECDISCARD 0x127d
|
|
|
|
#endif
|
|
|
|
|
2016-01-02 05:48:32 +05:30
|
|
|
int blkdiscard_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int blkdiscard_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
unsigned opts;
|
|
|
|
const char *offset_str = "0";
|
|
|
|
const char *length_str;
|
|
|
|
uint64_t offset; /* Leaving these two variables out does not */
|
|
|
|
uint64_t length; /* shrink code size and hampers readability. */
|
|
|
|
uint64_t range[2];
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
OPT_OFFSET = (1 << 0),
|
|
|
|
OPT_LENGTH = (1 << 1),
|
|
|
|
OPT_SECURE = (1 << 2),
|
2021-09-10 03:50:05 +05:30
|
|
|
OPT_FORCE = (1 << 3), //nop
|
2016-01-02 05:48:32 +05:30
|
|
|
};
|
|
|
|
|
2021-09-10 03:50:05 +05:30
|
|
|
opts = getopt32(argv, "^" "o:l:sf" "\0" "=1", &offset_str, &length_str);
|
2016-01-02 05:48:32 +05:30
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
fd = xopen(argv[0], O_RDWR|O_EXCL);
|
|
|
|
//Why bother, BLK[SEC]DISCARD will fail on non-blockdevs anyway?
|
|
|
|
// xfstat(fd, &st);
|
|
|
|
// if (!S_ISBLK(st.st_mode))
|
|
|
|
// bb_error_msg_and_die("%s: not a block device", argv[0]);
|
|
|
|
|
|
|
|
offset = xatoull_sfx(offset_str, kMG_suffixes);
|
|
|
|
|
|
|
|
if (opts & OPT_LENGTH)
|
|
|
|
length = xatoull_sfx(length_str, kMG_suffixes);
|
|
|
|
else {
|
|
|
|
xioctl(fd, BLKGETSIZE64, &length);
|
|
|
|
length -= offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
range[0] = offset;
|
|
|
|
range[1] = length;
|
|
|
|
ioctl_or_perror_and_die(fd,
|
|
|
|
(opts & OPT_SECURE) ? BLKSECDISCARD : BLKDISCARD,
|
|
|
|
&range,
|
|
|
|
"%s: %s failed",
|
|
|
|
argv[0],
|
|
|
|
(opts & OPT_SECURE) ? "BLKSECDISCARD" : "BLKDISCARD"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|