Skip to contentSkip to conversation

f2fs_io: expend fallocate command

PATCH Superseded Yangtao Li 2 participants 2 messages Jun 7, 2023 – Jun 8, 2023

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yangtao Li

author
#0 lore raw

PATCH v2 1/1 f2fs_io: expend fallocate command

Expend fallocate command to support more flags.

---
 tools/f2fs_io/f2fs_io.c | 39 +++++++++++++++++++++++++++++++++------
 1 file changed, 33 insertions(+), 6 deletions(-)
tools/f2fs_io/f2fs_io.c +33 -6
index 1774eca..5a6a6a0 100644
@@ -413,9 +413,13 @@ static void do_pinfile(int argc, char **argv, const struct cmd_desc *cmd)
 
 #define fallocate_desc "fallocate"
 #define fallocate_help						\
-"f2fs_io fallocate [keep_size] [offset] [length] [file]\n\n"	\
+"f2fs_io fallocate [-cipz] [keep_size] [offset] [length] [file]\n\n"	\
 "fallocate given the file\n"					\
 " [keep_size] : 1 or 0\n"					\
+" -p : punch hole\n"						\
+" -c : collapse range\n"					\
+" -z : zero range\n"						\
+" -i : insert range\n"						\
 
 static void do_fallocate(int argc, char **argv, const struct cmd_desc *cmd)
 {
@@ -423,20 +427,43 @@ static void do_fallocate(int argc, char **argv, const struct cmd_desc *cmd)
 	off_t offset, length;
 	struct stat sb;
 	int mode = 0;
+	int c;
 
-	if (argc != 5) {
+	while ((c = getopt(argc, argv, "cipz")) != -1) {
+		switch (c) {
+		case 'c':
+			mode |= FALLOC_FL_COLLAPSE_RANGE;
+			break;
+		case 'i':
+			mode |= FALLOC_FL_INSERT_RANGE;
+			break;
+		case 'p':
+			mode |= FALLOC_FL_PUNCH_HOLE;
+			break;
+		case 'z':
+			mode |= FALLOC_FL_ZERO_RANGE;
+			break;
+		default:
+			fputs(cmd->cmd_help, stderr);
+			exit(2);
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
+	if (argc != 4) {
 		fputs("Excess arguments\n\n", stderr);
 		fputs(cmd->cmd_help, stderr);
 		exit(1);
 	}
 
-	if (!strcmp(argv[1], "1"))
+	if (!strcmp(argv[0], "1"))
 		mode |= FALLOC_FL_KEEP_SIZE;
 
-	offset = atoi(argv[2]);
-	length = atoll(argv[3]);
+	offset = atoi(argv[1]);
+	length = atoll(argv[2]);
 
-	fd = xopen(argv[4], O_RDWR, 0);
+	fd = xopen(argv[3], O_RDWR, 0);
 
 	if (fallocate(fd, mode, offset, length) != 0)
 		die_errno("fallocate failed");
Signature · 8 lines
2.39.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw
Quoted 12 lines · Yangtao Li

Expend fallocate command to support more flags.

---
  tools/f2fs_io/f2fs_io.c | 39 +++++++++++++++++++++++++++++++++------
  1 file changed, 33 insertions(+), 6 deletions(-)
tools/f2fs_io/f2fs_io.c +0 -0
index 1774eca..5a6a6a0 100644

tools/f2fs_io/f2fs_io.c static void do_pinfile(int argc, char **argv, const struct cmd_desc *cmd) · line 416

Quoted 5 lines
tools/f2fs_io/f2fs_io.c +1 -1
@@ -413,9 +413,13 @@ static void do_pinfile(int argc, char **argv, const struct cmd_desc *cmd)
 
  #define fallocate_desc "fallocate"
  #define fallocate_help						\
-"f2fs_io fallocate [keep_size] [offset] [length] [file]\n\n"	\
+"f2fs_io fallocate [-cipz] [keep_size] [offset] [length] [file]\n\n"	\

Yangtao,

It missed to update man/f2fs_io.8.

Thanks,

Quoted 58 lines
tools/f2fs_io/f2fs_io.c +32 -5
  "fallocate given the file\n"					\
  " [keep_size] : 1 or 0\n"					\
+" -p : punch hole\n"						\
+" -c : collapse range\n"					\
+" -z : zero range\n"						\
+" -i : insert range\n"						\
 
  static void do_fallocate(int argc, char **argv, const struct cmd_desc *cmd)
  {
@@ -423,20 +427,43 @@ static void do_fallocate(int argc, char **argv, const struct cmd_desc *cmd)
  	off_t offset, length;
  	struct stat sb;
  	int mode = 0;
+	int c;
 
-	if (argc != 5) {
+	while ((c = getopt(argc, argv, "cipz")) != -1) {
+		switch (c) {
+		case 'c':
+			mode |= FALLOC_FL_COLLAPSE_RANGE;
+			break;
+		case 'i':
+			mode |= FALLOC_FL_INSERT_RANGE;
+			break;
+		case 'p':
+			mode |= FALLOC_FL_PUNCH_HOLE;
+			break;
+		case 'z':
+			mode |= FALLOC_FL_ZERO_RANGE;
+			break;
+		default:
+			fputs(cmd->cmd_help, stderr);
+			exit(2);
+		}
+	}
+	argc -= optind;
+	argv += optind;
+
+	if (argc != 4) {
  		fputs("Excess arguments\n\n", stderr);
  		fputs(cmd->cmd_help, stderr);
  		exit(1);
  	}
 
-	if (!strcmp(argv[1], "1"))
+	if (!strcmp(argv[0], "1"))
  		mode |= FALLOC_FL_KEEP_SIZE;
 
-	offset = atoi(argv[2]);
-	length = atoll(argv[3]);
+	offset = atoi(argv[1]);
+	length = atoll(argv[2]);
 
-	fd = xopen(argv[4], O_RDWR, 0);
+	fd = xopen(argv[3], O_RDWR, 0);
 
  	if (fallocate(fd, mode, offset, length) != 0)
  		die_errno("fallocate failed");