[PATCH] f2fs_io: add do_clearflags to clear nocompress/compress flag

From: Qi Han
Date: Tue Aug 15 2023 - 07:20:53 EST


To align f2fs_io functionality with chattr +/-c and +/-m, the
do_clearflags function has been added to clear the FS_COMPR_FL and
FS_NOCOMP_FL flags.

Signed-off-by: Qi Han <hanqi@xxxxxxxx>
---
man/f2fs_io.8 | 4 ++++
tools/f2fs_io/f2fs_io.c | 37 +++++++++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+)

diff --git a/man/f2fs_io.8 b/man/f2fs_io.8
index 450f1b7..4ffb28d 100644
--- a/man/f2fs_io.8
+++ b/man/f2fs_io.8
@@ -18,6 +18,10 @@ Get the flags associated with the specified file.
Set an f2fs file on specified file. The flag can be casefold,
compression, and nocompression.
.TP
+\fBclearflags\fR \fI[flag] [file]\fR
+Clear an f2fs file on specified file. The flag can be compression,
+and nocompression.
+.TP
\fBshutdown\fR \fIshutdown filesystem\fR
Freeze and stop all IOs for the file system mounted on
.IR dir.
diff --git a/tools/f2fs_io/f2fs_io.c b/tools/f2fs_io/f2fs_io.c
index 73ac700..a450bf1 100644
--- a/tools/f2fs_io/f2fs_io.c
+++ b/tools/f2fs_io/f2fs_io.c
@@ -330,6 +330,42 @@ static void do_setflags(int argc, char **argv, const struct cmd_desc *cmd)
exit(0);
}

+#define clearflags_desc "clearflags ioctl"
+#define clearflags_help \
+"f2fs_io clearflags [flag] [file]\n\n" \
+"clear a flag given the file\n" \
+"flag can be\n" \
+" compression\n" \
+" nocompression\n" \
+
+static void do_clearflags(int argc, char **argv, const struct cmd_desc *cmd)
+{
+ long flag = 0;
+ int ret, fd;
+
+ if (argc != 3) {
+ fputs("Excess arguments\n\n", stderr);
+ fputs(cmd->cmd_help, stderr);
+ exit(1);
+ }
+
+ fd = xopen(argv[2], O_RDONLY, 0);
+
+ ret = ioctl(fd, F2FS_IOC_GETFLAGS, &flag);
+ printf("get a flag on %s ret=%d, flags=%lx\n", argv[1], ret, flag);
+ if (ret)
+ die_errno("F2FS_IOC_GETFLAGS failed");
+
+ if (!strcmp(argv[1], "compression"))
+ flag &= ~FS_COMPR_FL;
+ else if (!strcmp(argv[1], "nocompression"))
+ flag &= ~FS_NOCOMP_FL;
+
+ ret = ioctl(fd, F2FS_IOC_SETFLAGS, &flag);
+ printf("clear a flag on %s ret=%d, flags=%s\n", argv[2], ret, argv[1]);
+ exit(0);
+}
+
#define shutdown_desc "shutdown filesystem"
#define shutdown_help \
"f2fs_io shutdown [level] [dir]\n\n" \
@@ -1464,6 +1500,7 @@ const struct cmd_desc cmd_list[] = {
CMD(set_verity),
CMD(getflags),
CMD(setflags),
+ CMD(clearflags),
CMD(shutdown),
CMD(pinfile),
CMD(fallocate),
--
2.39.0