[Ocfs2-test-devel] [PATCH 6/8] defrag-test: Add a utility to defrag the file as requsted.
Tristan Ye
tristan.ye at oracle.com
Fri Apr 15 02:01:50 PDT 2011
using new ioctl OCFS2_IOC_MOVE_EXT to defrag the single file on purpose.
Signed-off-by: Tristan Ye <tristan.ye at oracle.com>
---
programs/defrag-test/Makefile | 7 ++-
programs/defrag-test/defrager.c | 128 +++++++++++++++++++++++++++++++++++++++
2 files changed, 133 insertions(+), 2 deletions(-)
create mode 100644 programs/defrag-test/defrager.c
diff --git a/programs/defrag-test/Makefile b/programs/defrag-test/Makefile
index 3140562..08af47e 100644
--- a/programs/defrag-test/Makefile
+++ b/programs/defrag-test/Makefile
@@ -14,11 +14,11 @@ CFLAGS += $(INCLUDES)
LIBO2TEST = $(TOPDIR)/programs/libocfs2test/libocfs2test.a
-SOURCES = frager.c verify_file.c
+SOURCES = frager.c verify_file.c defrager.c
DIST_FILES = $(SOURCES)
-BIN_PROGRAMS = frager verify_file
+BIN_PROGRAMS = frager verify_file defrager
frager: frager.o
$(LINK) $(OCFS2_LIBS) $(LIBO2TEST)
@@ -26,4 +26,7 @@ frager: frager.o
verify_file: verify_file.o
$(LINK) $(OCFS2_LIBS) $(LIBO2TEST)
+defrager: defrager.o
+ $(LINK) $(OCFS2_LIBS)
+
include $(TOPDIR)/Postamble.make
diff --git a/programs/defrag-test/defrager.c b/programs/defrag-test/defrager.c
new file mode 100644
index 0000000..27a8530
--- /dev/null
+++ b/programs/defrag-test/defrager.c
@@ -0,0 +1,128 @@
+/* -*- mode: c; c-basic-offset: 8; -*-
+ * vim: noexpandtab sw=8 ts=8 sts=0:
+ *
+ * defrager.c
+ *
+ * simple utility to defrag or move extents for a file.
+ *
+ * Copyright (C) 2011 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, version 2, as published by the Free Software Foundation.
+ *
+ * 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 _XOPEN_SOURCE 600 /* Triggers magic in features.h */
+#define _LARGEFILE64_SOURCE
+
+#include <stdio.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <ctype.h>
+#include <inttypes.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <ocfs2/bitops.h>
+#include <libgen.h>
+#include <sys/vfs.h>
+#include <sys/ioctl.h>
+
+#include "ocfs2/ocfs2.h"
+#include "ocfs2/image.h"
+#include "ocfs2-kernel/ocfs2_ioctl.h"
+
+char *program_name = NULL;
+char *filename = NULL;
+
+static void usage(void)
+{
+ fprintf(stderr, ("Usage: %s [-s start_offset] [-l length] [-t thresh] "
+ "<file_path>\n"),
+ program_name);
+ exit(1);
+}
+
+int parse_opts(int argc, char **argv, struct ocfs2_move_extents *range)
+{
+ char c;
+
+ program_name = argv[0];
+ optind = 0;
+
+ while((c = getopt(argc, argv, "s:l:ht:")) != EOF) {
+ switch (c) {
+ case 's':
+ range->me_start = atol(optarg);
+ break;
+ case 'l':
+ range->me_len = atol(optarg);
+ break;
+ case 't':
+ range->me_thresh = atol(optarg);
+ break;
+ case 'h':
+ usage();
+ break;
+ default:
+ return -1;
+ }
+ }
+
+ if (optind >= argc) {
+ fprintf(stderr, "No file specified\n");
+ usage();
+ }
+
+ filename = argv[optind];
+
+ return 0;
+}
+
+int main(int argc, char *argv[])
+{
+
+ int ret, fd;
+ struct ocfs2_move_extents range;
+
+ memset(&range, 0, sizeof(range));
+
+ ret = parse_opts(argc, argv, &range);
+ if (ret)
+ return ret;
+
+ range.me_flags |= OCFS2_MOVE_EXT_FL_AUTO_DEFRAG;
+
+ fd = open(filename, O_RDWR);
+ if (fd < 0) {
+ ret = errno;
+ fprintf(stderr, "open file %s failed %d %s\n", filename,
+ ret, strerror(ret));
+ goto out;
+ }
+
+ ret = ioctl(fd, OCFS2_IOC_MOVE_EXT, &range);
+ if (ret < 0) {
+ ret = errno;
+ fprintf(stderr, "ioctl failed %d %s\n", ret, strerror(ret));
+ goto out;
+ }
+
+ if (!(range.me_flags & OCFS2_MOVE_EXT_FL_COMPLETE))
+ fprintf(stderr, "defrag didn't get finished completely.\n");
+out:
+ return ret;
+}
--
1.6.5.2
More information about the Ocfs2-test-devel
mailing list