[Ocfs2-test-devel] [PATCH 3/7] Discontig-bg-test: Add utility to reserve a range of unwriten allocation within a file.

Tristan Ye tristan.ye at oracle.com
Mon Jun 28 00:37:22 PDT 2010


It could be used later as a tool to consume the majority portion of a volume,
which really helps to speed up the activation of discontig block group.

Signed-off-by: Tristan Ye <tristan.ye at oracle.com>
---
 programs/discontig_bg_test/Makefile         |    7 +-
 programs/discontig_bg_test/resv.h           |   26 +++++
 programs/discontig_bg_test/resv_unwritten.c |  161 +++++++++++++++++++++++++++
 3 files changed, 192 insertions(+), 2 deletions(-)
 create mode 100755 programs/discontig_bg_test/resv.h
 create mode 100755 programs/discontig_bg_test/resv_unwritten.c

diff --git a/programs/discontig_bg_test/Makefile b/programs/discontig_bg_test/Makefile
index 70bb9b2..372e5d7 100644
--- a/programs/discontig_bg_test/Makefile
+++ b/programs/discontig_bg_test/Makefile
@@ -8,11 +8,11 @@ CFLAGS = -O2 -Wall -g $(OCFS2_CFLAGS)
 
 MPI_LINK = $(MPICC) $(CFLAGS) $(LDFLAGS) -o $@ $^
 
-SOURCES = spawn_inodes.c gen_extents.c
+SOURCES = spawn_inodes.c gen_extents.c resv.h resv_unwritten.c
 
 DIST_FILES = $(SOURCES)
 
-BIN_PROGRAMS = spawn_inodes gen_extents
+BIN_PROGRAMS = spawn_inodes gen_extents resv_unwritten
 
 spawn_inode: spawn_inodes.c
 	$(LINK) $(OCFS2_LIBS)
@@ -20,4 +20,7 @@ spawn_inode: spawn_inodes.c
 gen_extents: gen_extents.c
 	$(LINK) $(OCFS2_LIBS)
 
+resv_unwritten: resv_unwritten.c
+	$(LINK) $(OCFS2_LIBS)
+
 include $(TOPDIR)/Postamble.make
diff --git a/programs/discontig_bg_test/resv.h b/programs/discontig_bg_test/resv.h
new file mode 100755
index 0000000..b96aba4
--- /dev/null
+++ b/programs/discontig_bg_test/resv.h
@@ -0,0 +1,26 @@
+#include <sys/ioctl.h>
+#include <inttypes.h>
+#include <linux/types.h>
+
+/*
+ * Space reservation / allocation / free ioctls and argument structure
+ * are designed to be compatible with XFS.
+ */
+struct ocfs2_space_resv {
+        int16_t         l_type;
+        int16_t         l_whence;
+        int64_t         l_start;
+        int64_t         l_len;          /* len == 0 means until end of file */
+        int32_t         l_sysid;
+        uint32_t	l_pid;
+        int32_t         l_pad[4];       /* reserve area                     */
+};
+
+#define OCFS2_IOC_ALLOCSP               _IOW ('X', 10, struct ocfs2_space_resv)
+#define OCFS2_IOC_FREESP                _IOW ('X', 11, struct ocfs2_space_resv)
+#define OCFS2_IOC_RESVSP                _IOW ('X', 40, struct ocfs2_space_resv)
+#define OCFS2_IOC_UNRESVSP      _IOW ('X', 41, struct ocfs2_space_resv)
+#define OCFS2_IOC_ALLOCSP64     _IOW ('X', 36, struct ocfs2_space_resv)
+#define OCFS2_IOC_FREESP64      _IOW ('X', 37, struct ocfs2_space_resv)
+#define OCFS2_IOC_RESVSP64      _IOW ('X', 42, struct ocfs2_space_resv)
+#define OCFS2_IOC_UNRESVSP64    _IOW ('X', 43, struct ocfs2_space_resv)
diff --git a/programs/discontig_bg_test/resv_unwritten.c b/programs/discontig_bg_test/resv_unwritten.c
new file mode 100755
index 0000000..b399cf4
--- /dev/null
+++ b/programs/discontig_bg_test/resv_unwritten.c
@@ -0,0 +1,161 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * resv_unwritten.c
+ *
+ * A simple utility to alloc a region of unwritten space.
+ *
+ * Copyright (C) 2010 Oracle.  All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ *
+ */
+
+#define _GNU_SOURCE
+#define _XOPEN_SOURCE 500
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <limits.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "resv.h"
+
+#define FILE_MODE	(S_IRUSR|S_IWUSR|S_IXUSR|S_IROTH|\
+			 S_IWOTH|S_IXOTH|S_IRGRP|S_IWGRP|S_IXGRP)
+
+char *filename;
+unsigned long long start;
+unsigned long long len;
+
+static int usage(void)
+{
+	fprintf(stdout, "resv_unwritten <-f path> <-s start> <-l length>\n");
+	fprintf(stdout, "Example:\n"
+			"       resv_unwritten -f /storage/testifle "
+		"-s 0 -l 1073741824\n");
+
+	exit(1);
+}
+
+int parse_opts(int argc, char **argv)
+{
+	char c;
+
+	filename = NULL;
+	start = 0;
+	len = 0;
+
+	while (1) {
+		c = getopt(argc, argv, "f:s:l:h:");
+		if (c == -1)
+			break;
+
+		switch (c) {
+		case 'f':
+			filename = optarg;
+			break;
+		case 's':
+			start = atoll(optarg);
+			break;
+		case 'l':
+			len = atoll(optarg);
+			break;
+		case 'h':
+			usage();
+			break;
+		default:
+			return -1;
+		}
+	}
+
+	if (!filename) {
+		fprintf(stderr, "filename is a mandatory option\n");
+		usage();
+	}
+
+	if (!len) {
+		fprintf(stderr, "length is a mandartory option\n");
+		usage();
+	}
+
+	return 0;
+}
+
+static int open_file(const char *filename)
+{
+	int fd = -1, ret = 0;
+	int open_rw_flags = O_RDWR|O_CREAT;
+
+	fd = open64(filename, open_rw_flags, FILE_MODE);
+	if (fd < 0) {
+		ret = errno;
+		fprintf(stderr, "create file %s failed:%d:%s\n", filename, ret,
+			strerror(ret));
+	}
+
+	ret = fd;
+
+	return ret;
+}
+
+static int resv_unwritten(int fd, uint64_t start, uint64_t len)
+{
+	int ret;
+	struct ocfs2_space_resv sr;
+
+	memset(&sr, 0, sizeof(sr));
+	sr.l_whence = 0;
+	sr.l_start = start;
+	sr.l_len = len;
+
+	fprintf(stdout, "reserve unwritten region from %lu to %lu.\n",
+		sr.l_start, sr.l_start + sr.l_len);
+	ret = ioctl(fd, OCFS2_IOC_RESVSP64, &sr);
+	if (ret < 0) {
+		fprintf(stderr, "ioctl error %d: \"%s\"\n",
+			errno, strerror(errno));
+		return -1;
+	}
+
+	return 0;
+}
+
+int main(int argc, char *argv[])
+{
+	int ret = 0, fd;
+
+	ret = parse_opts(argc, argv);
+	if (ret) {
+		usage();
+		return -1;
+	}
+
+	fd = open_file(filename);
+	if (fd < 0)
+		return fd;
+
+	ret = resv_unwritten(fd, start, len);
+	if (ret < 0)
+		fprintf(stderr, "resv allocation failed %s\n", strerror(ret));
+
+	return ret;
+}
-- 
1.5.5




More information about the Ocfs2-test-devel mailing list