Skip to contentSkip to conversation

f2fs_io: add pin_file control command

PATCH New Jaegeuk Kim 1 participant 1 message Jan 4, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 f2fs_io: add pin_file control command

This requires a kernel patch "f2fs: export pin_file flag to user".

---
 tools/f2fs_io/f2fs_io.c | 53 +++++++++++++++++++++++++++++++++++++++++
 tools/f2fs_io/f2fs_io.h | 11 +++++++++
 2 files changed, 64 insertions(+)
tools/f2fs_io/f2fs_io.c +53 -0
index 9c3863d..35ee76a 100644
@@ -75,6 +75,58 @@ static void do_shutdown(int argc, char **argv, const struct cmd_desc *cmd)
 	exit(0);
 }
 
+#define pinfile_desc "pin file control"
+#define pinfile_help						\
+"f2fs_io pinfile [get|set] [file]\n\n"			\
+"get/set pinning given the file\n"				\
+
+static void do_pinfile(int argc, char **argv, const struct cmd_desc *cmd)
+{
+	u32 pin;
+	int ret, fd;
+
+	if (argc != 3) {
+		fputs("Excess arguments\n\n", stderr);
+		fputs(cmd->cmd_help, stderr);
+		exit(1);
+	}
+
+	fd = open(argv[2], O_RDWR);
+	if (fd == -1) {
+		fputs("Open failed\n\n", stderr);
+		fputs(cmd->cmd_help, stderr);
+		exit(1);
+	}
+
+	ret = -1;
+	if (!strcmp(argv[1], "set")) {
+		pin = 1;
+		ret = ioctl(fd, F2FS_IOC_SET_PIN_FILE, &pin);
+		if (ret != 0) {
+			perror("set_pin_file failed");
+			exit(1);
+		}
+		printf("set_pin_file: %u blocks moved in %s\n", ret, argv[2]);
+	} else if (!strcmp(argv[1], "get")) {
+		unsigned int flags;
+
+		ret = ioctl(fd, F2FS_IOC_GET_PIN_FILE, &pin);
+		if (ret < 0) {
+			perror("pin_file failed");
+			exit(1);
+		}
+		ret = ioctl(fd, F2FS_IOC_GETFLAGS, &flags);
+		if (ret < 0) {
+			perror("get flags failed");
+			exit(1);
+		}
+		printf("get_pin_file: %s with %u blocks moved in %s\n",
+				(flags & F2FS_PIN_FILE_FL) ? "pinned" : "un-pinned",
+				pin, argv[2]);
+	}
+	exit(0);
+}
+
 #define CMD_HIDDEN 	0x0001
 #define CMD(name) { #name, do_##name, name##_desc, name##_help, 0 }
 #define _CMD(name) { #name, do_##name, NULL, NULL, CMD_HIDDEN }
@@ -83,6 +135,7 @@ static void do_help(int argc, char **argv, const struct cmd_desc *cmd);
 const struct cmd_desc cmd_list[] = {
 	_CMD(help),
 	CMD(shutdown),
+	CMD(pinfile),
 	{ NULL, NULL, NULL, NULL, 0 }
 };
 
tools/f2fs_io/f2fs_io.h +11 -0
index a02ff2d..882707c 100644
@@ -36,7 +36,13 @@ typedef u16	__be16;
 typedef u32	__be32;
 #endif
 
+#ifndef FS_IOC_GETFLAGS
+#define FS_IOC_GETFLAGS			_IOR('f', 1, long)
+#endif
+
 #define F2FS_IOCTL_MAGIC		0xf5
+#define F2FS_IOC_GETFLAGS		FS_IOC_GETFLAGS
+
 #define F2FS_IOC_START_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 1)
 #define F2FS_IOC_COMMIT_ATOMIC_WRITE	_IO(F2FS_IOCTL_MAGIC, 2)
 #define F2FS_IOC_START_VOLATILE_WRITE	_IO(F2FS_IOCTL_MAGIC, 3)
@@ -61,6 +67,11 @@ typedef u32	__be32;
 #define F2FS_IOC_GET_ENCRYPTION_POLICY	FS_IOC_GET_ENCRYPTION_POLICY
 #define F2FS_IOC_GET_ENCRYPTION_PWSALT	FS_IOC_GET_ENCRYPTION_PWSALT
 
+/*
+ * Inode flags
+ */
+#define F2FS_PIN_FILE_FL		0x40000000 /* pin_file status */
+
 /*
  * should be same as XFS_IOC_GOINGDOWN.
  * Flags for going down operation used by FS_IOC_GOINGDOWN
Signature · 1 line
2.19.0.605.g01d371f741-goog