diff options
author | Ocean Chen <oceanchen@google.com> | 2020-10-14 01:22:40 +0800 |
---|---|---|
committer | Jaegeuk Kim <jaegeuk@kernel.org> | 2020-12-07 09:25:14 -0800 |
commit | 1bfc17340d3276bd5ce050211cd25dad894485b3 (patch) | |
tree | 81c9b4c9f0e0b960a379c42edb89e2769d7e12a5 | |
parent | e59bb175ac861d317601a42e6915cdedf3edefc4 (diff) | |
download | f2fs-tools-1bfc17340d3276bd5ce050211cd25dad894485b3.tar.gz |
f2fs_io: add erase option
f2fs_io erase [block_device_path]
Signed-off-by: Ocean Chen <oceanchen@google.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
-rw-r--r-- | tools/f2fs_io/f2fs_io.c | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/tools/f2fs_io/f2fs_io.c b/tools/f2fs_io/f2fs_io.c index 5223903..d7f4d23 100644 --- a/tools/f2fs_io/f2fs_io.c +++ b/tools/f2fs_io/f2fs_io.c @@ -24,13 +24,13 @@ #include <getopt.h> #include <inttypes.h> #include <limits.h> +#include <linux/fs.h> #include <signal.h> #include <stdarg.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <string.h> #include <sys/mman.h> #include <sys/sendfile.h> #include <sys/stat.h> @@ -434,6 +434,56 @@ static void do_fallocate(int argc, char **argv, const struct cmd_desc *cmd) exit(0); } +#define erase_desc "erase a block device" +#define erase_help \ +"f2fs_io erase [block_device_path]\n\n" \ +"Send DISCARD | BLKSECDISCARD comamnd to" \ +"block device in block_device_path\n" \ + +static void do_erase(int argc, char **argv, const struct cmd_desc *cmd) +{ + int fd, ret; + struct stat st; + u64 range[2]; + + if (argc != 2) { + fputs("Excess arguments\n\n", stderr); + fputs(cmd->cmd_help, stderr); + exit(1); + } + + if (stat(argv[1], &st) != 0) { + fputs("stat error\n", stderr); + exit(1); + } + + if (!S_ISBLK(st.st_mode)) { + fputs(argv[1], stderr); + fputs(" is not a block device\n", stderr); + exit(1); + } + + fd = xopen(argv[1], O_WRONLY, 0); + + range[0] = 0; + ret = ioctl(fd, BLKGETSIZE64, &range[1]); + if (ret < 0) { + fputs("get size failed\n", stderr); + exit(1); + } + + ret = ioctl(fd, BLKSECDISCARD, &range); + if (ret < 0) { + ret = ioctl(fd, BLKDISCARD, &range); + if (ret < 0) { + fputs("Discard failed\n", stderr); + exit(1); + } + } + + exit(0); +} + #define write_desc "write data into file" #define write_help \ "f2fs_io write [chunk_size in 4kb] [offset in chunk_size] [count] [pattern] [IO] [file_path]\n\n" \ @@ -957,6 +1007,7 @@ const struct cmd_desc cmd_list[] = { CMD(shutdown), CMD(pinfile), CMD(fallocate), + CMD(erase), CMD(write), CMD(read), CMD(randread), |