[Ocfs2-tools-commits] taoma commits r1342 - in branches/sparse-files: extras fswreck libocfs2

svn-commits at oss.oracle.com svn-commits at oss.oracle.com
Wed Apr 18 02:26:12 PDT 2007


Author: taoma
Date: 2007-04-18 02:26:05 -0700 (Wed, 18 Apr 2007)
New Revision: 1342

Added:
   branches/sparse-files/extras/sparse_kernel.c
Modified:
   branches/sparse-files/extras/Makefile
   branches/sparse-files/extras/sparse.c
   branches/sparse-files/fswreck/extent.c
   branches/sparse-files/libocfs2/extend_file.c
Log:
Add another test program in extras which use system calls(ocfs2 kernel)
to create sparse files.

This new program(sparse_file_kernel) accepts the same parameter as
sparse_file_test, so that the test script can check whether ocfs2-tools's
read/write functions can work well with kernel's read/write functions.

Note:
both sparse.c and sparse_kernel.c may be moved to ocfs2-test in future
if sparse-files branch is merged into main trunk.

This commit also includes another 3 minor modification:
1. Remove an unused variable in extend_file.c.
2. Modify fswreck/extent.c's call of ocfs2_insert_extent.
3. Modify sparse.c's output to be comfortable with sparse_kernel.c.

Modified: branches/sparse-files/extras/Makefile
===================================================================
--- branches/sparse-files/extras/Makefile	2007-04-16 02:39:35 UTC (rev 1341)
+++ branches/sparse-files/extras/Makefile	2007-04-18 09:26:05 UTC (rev 1342)
@@ -11,7 +11,7 @@
 
 CFLAGS = $(OPTS) $(WARNINGS) 
 
-UNINST_PROGRAMS = find_hardlinks find_dup_extents find_inode_paths set_random_bits decode_lockres encode_lockres mark_journal_dirty find_allocation_fragments compute_groups sparse_file_test
+UNINST_PROGRAMS = find_hardlinks find_dup_extents find_inode_paths set_random_bits decode_lockres encode_lockres mark_journal_dirty find_allocation_fragments compute_groups sparse_file_test sparse_file_kernel
 
 INCLUDES = -I../libocfs2/include -I$(TOPDIR)/libo2dlm/include -I$(TOPDIR)/libo2cb/include
 
@@ -31,6 +31,7 @@
 FIND_ALLOC_FRAG_CFILES = find_allocation_fragments.c
 COMPUTE_GROUPS_CFILES = compute_groups.c
 SPARSE_FILE_TEST_CFILES = sparse.c
+SPARSE_FILE_KERNEL_CFILES = sparse_kernel.c
 
 DIST_FILES = $(FIND_HARDLINKS_CFILES) $(FIND_DUP_EXTENTS_CFILES) $(FIND_INODE_PATHS_CFILES) $(SET_RANDOM_BITS_CFILES) $(DECODE_LOCKRES_CFILES) $(ENCODE_LOCKRES_CFILES) $(MARK_JOURNAL_DIRTY_CFILES) $(FIND_ALLOC_FRAG_CFILES) $(COMPUTE_GROUPS_CFILES)
 
@@ -44,6 +45,7 @@
 FIND_ALLOC_FRAG_OBJS = $(subst .c,.o,$(FIND_ALLOC_FRAG_CFILES))
 COMPUTE_GROUPS_OBJS = $(subst .c,.o,$(COMPUTE_GROUPS_CFILES))
 SPARSE_FILE_TEST_OBJS = $(subst .c,.o,$(SPARSE_FILE_TEST_CFILES))
+SPARSE_FILE_KERNEL_OBJS = $(subst .c,.o,$(SPARSE_FILE_KERNEL_CFILES))
 
 LIBOCFS2 = ../libocfs2/libocfs2.a
 EXTRAS_LIBS = $(LIBOCFS2) $(COM_ERR_LIBS)
@@ -78,4 +80,7 @@
 sparse_file_test: $(SPARSE_FILE_TEST_OBJS) $(LIBOCFS2)
 	$(LINK) $(EXTRAS_LIBS)
 
+sparse_file_kernel: $(SPARSE_FILE_KERNEL_OBJS) $(LIBOCFS2)
+	$(LINK) $(EXTRAS_LIBS)
+
 include $(TOPDIR)/Postamble.make

Modified: branches/sparse-files/extras/sparse.c
===================================================================
--- branches/sparse-files/extras/sparse.c	2007-04-16 02:39:35 UTC (rev 1341)
+++ branches/sparse-files/extras/sparse.c	2007-04-18 09:26:05 UTC (rev 1342)
@@ -22,30 +22,13 @@
  *
  */
 #include <stdio.h>
-#include <stdlib.h>
 #include <unistd.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
 #include <errno.h>
-#include <pwd.h>
-#include <grp.h>
-#include <time.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netinet/in.h>
 #include <signal.h>
-#include <sys/raw.h>
-#include <linux/kdev_t.h>
 #include <inttypes.h>
+#include <libgen.h>
 
-#include <linux/types.h>
-
 #include "ocfs2.h"
-#include "ocfs2_fs.h"
-#include "ocfs1_fs_compat.h"
 
 
 char *progname = NULL;
@@ -79,8 +62,8 @@
 static void usage (const char *progname)
 {
 	fprintf(stderr, "usage: %s [-f file-name] [-b block-offset]\n"
-			"\t\t[-w write-bytes] [-r read-bytes] [-s new-size] device\n",
-			progname);
+		"\t\t[-w write-bytes] [-r read-bytes] [-s new-size] device\n",
+		progname);
 
 	exit (0);
 }
@@ -179,6 +162,8 @@
 
 	for (i = 0; i < bytes; i++) {
 		printf("%d,", buf[i]);
+		if (i % 10 == 0)
+			printf("\n");
 	}
 	printf("\n");
 }
@@ -250,9 +235,8 @@
 				 uint64_t block_offset, int write_byte)
 {
 	errcode_t ret;
-	uint64_t offset, p_blkno;
+	uint64_t offset;
 	uint32_t got;
-	uint32_t found;
 	ocfs2_cached_inode *ci = NULL;
 	int buflen, block_num;
 	char *buf = NULL;
@@ -284,6 +268,7 @@
 		goto bail;
 	}
 
+	srand(write_byte);
 	create_data(buf, write_byte);
 
 	ret = ocfs2_extent_map_init(fs, ci);
@@ -292,24 +277,12 @@
 		goto bail;
 	}
 
-	/* In order to test CONTIG_LEFT, we need to test like this:
-	 * 1. Remove the comments.
-	 * call ocfs2_new_clusters to allocate a cluster first and release
-	 * it after the ocfs2_file_write.
-	 * 2. Comments out ocfs2_new_clusters. So the old cluster will be
-	 * used, and CONTIG_LEFT will be used when ocfs2_insert_extent is
-	 * called.
-	 */
-	/* ocfs2_new_clusters(fs, 1, 1, &p_blkno, &found); */
-
 	ret = ocfs2_file_write(ci, buf, buflen, offset, &got);
 	if (ret) {
 		com_err(progname, ret, "while reading %u bytes", buflen);
 		goto bail;
 	}
 
-	/* ocfs2_free_clusters(fs, 1, p_blkno); */
-
 	output(buf, write_byte);
 
 bail:
@@ -345,9 +318,8 @@
 		goto bail;
 	}
 
-	if (!device || !options.ops) {
+	if (!device || !options.ops)
 		goto bail;
-	}
 
 	ret = ocfs2_open(device, OCFS2_FLAG_RW, 0, 0, &fs);
 	if (ret) {

Added: branches/sparse-files/extras/sparse_kernel.c
===================================================================
--- branches/sparse-files/extras/sparse_kernel.c	2007-04-16 02:39:35 UTC (rev 1341)
+++ branches/sparse-files/extras/sparse_kernel.c	2007-04-18 09:26:05 UTC (rev 1342)
@@ -0,0 +1,287 @@
+/*
+ * sparse_kernel.c
+ *
+ * test file for creating sparse files by system calls(using ocfs2 kernel).
+ * this program use the same parameters as sparse.c.
+ *
+ * Copyright (C) 2007 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.
+ *
+ */
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <libgen.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "ocfs2.h"
+
+char *progname = NULL;
+int blocksize = 1024;
+
+enum operations {
+	READ_FILE=1,
+	WRITE_FILE,
+	SET_SIZE
+};
+
+static char *device = NULL;
+
+struct{
+	char file_name[256];
+	uint64_t block_offset;
+	enum operations ops;
+	uint64_t bytes;
+} options;
+
+static void usage (const char *progname)
+{
+	fprintf(stderr, "usage: %s [-f file-name] [-b block-offset]\n"
+		"\t\t[-w write-bytes] [-r read-bytes] [-s new-size] device\n",
+		progname);
+
+	exit (1);
+}
+
+static int read_options(int argc, char **argv)
+{
+	int c;
+
+	progname = basename(argv[0]);
+
+	if (argc < 2) {
+		return 1;
+	}
+
+	while(1) {
+		c = getopt(argc, argv, "f:b:w:r:s:");
+		if (c == -1)
+			break;
+
+		switch (c) {
+		case 'f':	/* file name */
+			strcpy(options.file_name, optarg);
+			break;
+
+		case 'b':	/* block_offset */
+			options.block_offset = strtoull(optarg, NULL, 0);
+			break;
+
+		case 'w':
+			options.ops = WRITE_FILE;
+			options.bytes = strtoull(optarg, NULL, 0);
+			break;
+
+		case 'r':
+			options.ops = READ_FILE;
+			options.bytes = strtoull(optarg, NULL, 0);
+			break;
+
+		case 's':
+			options.ops = SET_SIZE;
+			options.bytes = strtoull(optarg, NULL, 0);
+			break;
+
+		default:
+			break;
+		}
+	}
+
+	if (optind < argc && argv[optind])
+		device = argv[optind];
+
+	return 0;
+}
+
+static void output(char *buf, int bytes)
+{
+	int i;
+
+	for (i = 0; i < bytes; i++) {
+		printf("%d,", buf[i]);
+		if (i % 10 == 0)
+			printf("\n");
+	}
+	printf("\n");
+}
+
+static int test_file_read(char *file_name, uint64_t block_offset,
+			  int read_bytes)
+{
+	int tot, read_len;
+	int fd;
+	uint64_t offset;
+	char *buf = NULL;
+	struct stat status;
+
+	if (lstat(file_name, &status) < 0)
+		return errno;
+
+	offset = block_offset * blocksize;
+	if (status.st_size < offset + read_bytes) {
+		fprintf(stderr, "File's size is too small.\n");
+		return -1;
+	}
+
+	fd = open(file_name, O_RDONLY);
+	if (fd < 0) {
+		fprintf(stderr, "can't open the file  %s, errno = %d",
+			file_name, errno);
+		return errno;
+	}
+
+	if (lseek(fd, offset, SEEK_SET) == -1) {
+		fprintf(stderr, "can't lseek the file  %s, errno = %d",
+			file_name, errno);
+		goto out;
+	}
+
+	buf = (char *)malloc(read_bytes);
+	if (!buf)
+		return errno;
+	memset(buf, 0, read_bytes);
+
+	tot = 0;
+	while (tot < read_bytes) {
+		read_len = read(fd, buf + tot, read_bytes - tot);
+		if (read_len < 0) {
+			fprintf(stderr, "read from the file %s failed,"
+				" errno = %d", file_name, errno);
+			goto out;
+		}
+
+		tot += read_len;
+	}
+
+	output(buf, read_bytes);
+out:
+	if (buf)
+		free(buf);
+	close(fd);
+	return errno;
+}
+
+static unsigned long get_rand(unsigned long min, unsigned long max)
+{
+	if (min == 0 && max == 0)
+		return 0;
+
+	return min + (rand() % (max - min));
+}
+
+static void create_data(char *data, int len)
+{
+	int i;
+
+	for (i = 0; i < len - 1; i ++) {
+		data[i] = 'A' + get_rand(0,52);
+	}
+	data[i] = '\0';
+}
+
+static int test_file_write(char *file_name, uint64_t block_offset,
+			   int write_bytes)
+{
+	int tot, write_len;
+	int fd;
+	uint64_t offset;
+	char *buf = NULL;
+
+	fd = open(file_name, O_RDWR|O_CREAT, 0744);
+	if (fd < 0) {
+		fprintf(stderr, "can't open the file  %s, errno = %d",
+			file_name, errno);
+		return errno;
+	}
+
+	offset = block_offset * blocksize;
+	if (lseek(fd, offset, SEEK_SET) == -1) {
+		fprintf(stderr, "can't lseek the file  %s, errno = %d",
+			file_name, errno);
+		goto out;
+	}
+
+	buf = (char *)malloc(write_bytes);
+	if (!buf)
+		return errno;
+
+	srand(write_bytes);
+	create_data(buf, write_bytes);
+
+	tot = 0;
+	while (tot < write_bytes) {
+		write_len = write(fd, buf + tot, write_bytes - tot);
+		if (write_len < 0) {
+			fprintf(stderr, "can't write the file  %s, errno = %d",
+				file_name, errno);
+			goto out;
+		}
+
+		tot += write_len;
+	}
+
+	output(buf, write_bytes);
+
+	/* since we may use ocfs2-tools to read from the disk, we want the
+	 * file content synchronized to the disk.
+	 */
+	fsync(fd);
+out:
+	if (buf)
+		free(buf);
+	close(fd);
+	return errno;
+}
+
+int main (int argc, char **argv)
+{
+	int ret = 1;
+	ocfs2_filesys *fs = NULL;
+
+	memset(&options, 0, sizeof(options));
+	if (read_options(argc, argv)) {
+		usage(progname);
+		goto bail;
+	}
+
+	if (!device || !options.ops)
+		goto bail;
+
+	/* get the block size from the device. */
+	ret = ocfs2_open(device, OCFS2_FLAG_RO, 0, 0, &fs);
+	if (ret) {
+		fprintf(stderr, "open the device[%s] failed", device);
+		goto bail;
+	}
+
+	blocksize = fs->fs_blocksize;
+	ocfs2_close(fs);
+
+	if (options.ops == READ_FILE)
+		ret = test_file_read(options.file_name, options.block_offset,
+				     options.bytes);
+	else if (options.ops == WRITE_FILE)
+		ret = test_file_write(options.file_name, options.block_offset,
+				      options.bytes);
+	else if (options.ops == SET_SIZE)
+		ret = truncate(options.file_name, options.bytes);
+
+bail:
+	return ret;
+}

Modified: branches/sparse-files/fswreck/extent.c
===================================================================
--- branches/sparse-files/fswreck/extent.c	2007-04-16 02:39:35 UTC (rev 1341)
+++ branches/sparse-files/fswreck/extent.c	2007-04-18 09:26:05 UTC (rev 1342)
@@ -103,7 +103,8 @@
 		 * we insert each cluster in reverse. */
 		for(i = n_clusters; i; --i) {
 			tmpblk = blkno + ocfs2_clusters_to_blocks(fs, i - 1);
-		 	ret = ocfs2_insert_extent(fs, ino, tmpblk, 1);
+		 	ret = ocfs2_insert_extent(fs, ino, n_clusters - i,
+						  tmpblk, 1);
 			if (ret) 
 				FSWRK_COM_FATAL(progname, ret);	
 		}

Modified: branches/sparse-files/libocfs2/extend_file.c
===================================================================
--- branches/sparse-files/libocfs2/extend_file.c	2007-04-16 02:39:35 UTC (rev 1341)
+++ branches/sparse-files/libocfs2/extend_file.c	2007-04-18 09:26:05 UTC (rev 1342)
@@ -2073,7 +2073,6 @@
 static void free_duplicated_extent_block_dinode(ocfs2_filesys *fs,
 						char *di_buf)
 {
-	int i;
 	struct ocfs2_dinode *di = NULL;
 	struct ocfs2_extent_list *el = NULL;
 




More information about the Ocfs2-tools-commits mailing list