[Ocfs2-test-devel] [PATCH 4/7] Discontig-bg-test: Add utility to punch hole.
Tristan Ye
tristan.ye at oracle.com
Mon Jun 28 00:37:23 PDT 2010
It could be later used as a tool to remove desired extent records.
Signed-off-by: Tristan Ye <tristan.ye at oracle.com>
---
programs/discontig_bg_test/Makefile | 7 +-
programs/discontig_bg_test/punch_hole.c | 162 +++++++++++++++++++++++++++++++
2 files changed, 167 insertions(+), 2 deletions(-)
create mode 100755 programs/discontig_bg_test/punch_hole.c
diff --git a/programs/discontig_bg_test/Makefile b/programs/discontig_bg_test/Makefile
index 372e5d7..2d48bb1 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 resv.h resv_unwritten.c
+SOURCES = spawn_inodes.c gen_extents.c resv.h resv_unwritten.c punch_hole.c
DIST_FILES = $(SOURCES)
-BIN_PROGRAMS = spawn_inodes gen_extents resv_unwritten
+BIN_PROGRAMS = spawn_inodes gen_extents resv_unwritten punch_hole
spawn_inode: spawn_inodes.c
$(LINK) $(OCFS2_LIBS)
@@ -23,4 +23,7 @@ gen_extents: gen_extents.c
resv_unwritten: resv_unwritten.c
$(LINK) $(OCFS2_LIBS)
+punch_hole: punch_hole.c
+ $(LINK) $(OCFS2_LIBS)
+
include $(TOPDIR)/Postamble.make
diff --git a/programs/discontig_bg_test/punch_hole.c b/programs/discontig_bg_test/punch_hole.c
new file mode 100755
index 0000000..bdfa8be
--- /dev/null
+++ b/programs/discontig_bg_test/punch_hole.c
@@ -0,0 +1,162 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * punch_hole.c
+ *
+ * A simple utility to punch a hole on a file.
+ *
+ * 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 <sys/mman.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.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, "punch_hole <-f path> <-s start> <-l length>\n");
+ fprintf(stdout, "Example:\n"
+ " punch_hole -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();
+ }
+
+ return 0;
+}
+
+static int open_file(const char *filename)
+{
+ int fd = -1, ret = 0;
+ int open_rw_flags = O_RDWR;
+
+ 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 punch_hole(int fd, uint64_t start, uint64_t len)
+{
+ int ret = 0;
+ struct ocfs2_space_resv sr;
+
+ memset(&sr, 0, sizeof(sr));
+ sr.l_whence = 0;
+ sr.l_start = start;
+ sr.l_len = len;
+
+ fprintf(stdout, "punching holes from %lu to %lu.\n",
+ sr.l_start, sr.l_start + sr.l_len);
+
+ ret = ioctl(fd, OCFS2_IOC_UNRESVSP64, &sr);
+ if (ret == -1) {
+ fprintf(stderr, "ioctl error %d: \"%s\"\n",
+ errno, strerror(errno));
+ return -1;
+ }
+
+ return ret;
+}
+
+int main(int argc, char *argv[])
+{
+ int fd, ret = 0;
+
+ ret = parse_opts(argc, argv);
+ if (ret) {
+ usage();
+ return -1;
+ }
+
+ fd = open_file(filename);
+ if (fd < 0) {
+ fprintf(stderr, "Open file %s failed.\n", filename);
+ ret = -1;
+ }
+
+ ret = punch_hole(fd, start, len);
+ if (ret < 0)
+ fprintf(stderr, "Punch hole failed.\n");
+
+ if (fd > 0)
+ close(fd);
+
+ return ret;
+}
--
1.5.5
More information about the Ocfs2-test-devel
mailing list