[Ocfs2-tools-devel] [PATCH 40/50] libocfs2: Add helper function for creating and attaching refcount tree.

Tao Ma tao.ma at oracle.com
Mon Jan 11 07:31:26 PST 2010


In fsck.ocfs2, sometimes we need to create a new refcount tree
or attach a file to a refcount tree. So add the helper function.

Signed-off-by: Tao Ma <tao.ma at oracle.com>
---
 include/ocfs2/ocfs2.h |    3 ++
 libocfs2/refcount.c   |   60 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 0 deletions(-)

diff --git a/include/ocfs2/ocfs2.h b/include/ocfs2/ocfs2.h
index 65e10e7..09f12ef 100644
--- a/include/ocfs2/ocfs2.h
+++ b/include/ocfs2/ocfs2.h
@@ -445,6 +445,9 @@ int ocfs2_get_refcount_rec(ocfs2_filesys *fs,
 			   struct ocfs2_refcount_rec *ret_rec,
 			   int *index,
 			   char *ret_buf);
+errcode_t ocfs2_create_refcount_tree(ocfs2_filesys *fs, uint64_t *refcount_loc);
+errcode_t ocfs2_attach_refcount_tree(ocfs2_filesys *fs,
+				     uint64_t ino, uint64_t refcount_loc);
 errcode_t ocfs2_swap_dir_entries_from_cpu(void *buf, uint64_t bytes);
 errcode_t ocfs2_swap_dir_entries_to_cpu(void *buf, uint64_t bytes);
 void ocfs2_swap_dir_trailer(struct ocfs2_dir_block_trailer *trailer);
diff --git a/libocfs2/refcount.c b/libocfs2/refcount.c
index 58afe32..92b6ba8 100644
--- a/libocfs2/refcount.c
+++ b/libocfs2/refcount.c
@@ -24,6 +24,8 @@
 #include <string.h>
 #include <inttypes.h>
 #include <assert.h>
+#include <errno.h>
+#include <unistd.h>
 
 #include "ocfs2/byteorder.h"
 #include "ocfs2/ocfs2.h"
@@ -2126,6 +2128,64 @@ out:
 	return ret;
 }
 
+static errcode_t create_generation(uint32_t *value)
+{
+	int randfd = 0;
+	int readlen = sizeof(*value);
+
+	randfd = open("/dev/urandom", O_RDONLY);
+	if (randfd < 0)
+		return errno;
+
+	if (read(randfd, value, readlen) != readlen)
+		return errno;
+
+	close(randfd);
+
+	return 0;
+}
+
+errcode_t ocfs2_create_refcount_tree(ocfs2_filesys *fs, uint64_t *refcount_loc)
+{
+	errcode_t ret;
+	uint32_t generation;
+
+	ret = create_generation(&generation);
+	if (ret)
+		return ret;
+
+	return ocfs2_new_refcount_block(fs, refcount_loc, 0, generation);
+}
+
+errcode_t ocfs2_attach_refcount_tree(ocfs2_filesys *fs,
+				     uint64_t ino, uint64_t refcount_loc)
+{
+	errcode_t ret;
+	char *buf = NULL;
+	struct ocfs2_dinode *di;
+
+	ret = ocfs2_malloc_block(fs->fs_io, &buf);
+	if (ret)
+		return ret;
+
+	ret = ocfs2_read_inode(fs, ino, buf);
+	if (ret)
+		goto out;
+
+	di = (struct ocfs2_dinode *)buf;
+
+	assert(!(di->i_dyn_features & OCFS2_HAS_REFCOUNT_FL));
+	assert(!di->i_refcount_loc);
+
+	di->i_refcount_loc = refcount_loc;
+	di->i_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
+
+	ret = ocfs2_write_inode(fs, ino, buf);
+out:
+	ocfs2_free(&buf);
+	return ret;
+}
+
 struct xattr_value_cow_object {
 	struct ocfs2_xattr_value_root *xv;
 	uint64_t xe_blkno;
-- 
1.5.5




More information about the Ocfs2-tools-devel mailing list