[Ocfs2-tools-devel] [PATCH 3/5] libocfs2: Fix some bugs in libocfs2

piaojun piaojun at huawei.com
Thu Apr 2 05:44:12 PDT 2015


1. In ocfs2_new_system_inode(), we should handle other error numbers
   ocfs2_chain_alloc_with_io() returns except for numbers
   OCFS2_ET_BIT_NOT_FOUND.

2. In ocfs2_expand_inline_dx_root(), we should check if dx_leaves is null
   before free it, and every element in dx_leaves should also be freed.

3. In ocfs2_dx_dir_name_hash(), the copy length of memcpy() should be
   sizeof(OCFS2_RAW_SB(fs->fs_super)->s_dx_seed), as s_dx_seed have only 3
   element of u32.

4. In ocfs2_process_dir_block(), blockcnt is uint64_t, so it can not be
   less than zero.

5. In ocfs2_allocate_unwritten_extents(), we should handle the error
    numbers ocfs2_extent_map_get_blocks() returns.

6. In create_generation(), we should close randfd if open() fails.

7. In ocfs2_format_slot_map(), if ocfs2_lookup_system_inode() fails, then
   'goto out' and check if sf.ci is null. And 'sf.ci' probably is not null,
   as 'sf' is not initialized before. This will lead to free the wrong
   cached inode with ocfs2_free_cached_inode(). So we need initialize 'sf'
   with zero at first.

Signed-off-by: Jun Piao <piaojun at huawei.com>
Reviewed-by: Alex Chen <alex.chen at huawei.com>

---
 libocfs2/alloc.c       |  2 ++
 libocfs2/dir_indexed.c | 33 +++++++++++++++++----------------
 libocfs2/dir_iterate.c |  3 ---
 libocfs2/extend_file.c |  3 +++
 libocfs2/refcount.c    |  4 +++-
 libocfs2/slot_map.c    |  2 +-
 6 files changed, 26 insertions(+), 21 deletions(-)

diff --git a/libocfs2/alloc.c b/libocfs2/alloc.c
index 1cd21c6..79579fc 100644
--- a/libocfs2/alloc.c
+++ b/libocfs2/alloc.c
@@ -307,6 +307,8 @@ errcode_t ocfs2_new_system_inode(ocfs2_filesys *fs, uint64_t *ino,
 		if (ret)
 			goto out;
 	}
+	else if (ret)
+		goto out;

 	memset(buf, 0, fs->fs_blocksize);
 	di = (struct ocfs2_dinode *)buf;
diff --git a/libocfs2/dir_indexed.c b/libocfs2/dir_indexed.c
index f59d945..6ad3b27 100644
--- a/libocfs2/dir_indexed.c
+++ b/libocfs2/dir_indexed.c
@@ -360,6 +360,19 @@ static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
 	dx_leaf->dl_list.de_num_used += 1;
 }

+static int ocfs2_dx_dir_free_leaves(ocfs2_filesys *fs,
+				struct ocfs2_dx_leaf **dx_leaves)
+{
+	int i, num;
+	num = ocfs2_clusters_to_blocks(fs, 1);
+	for (i = 0; i < num; i++) {
+		if (dx_leaves[i])
+			(void)ocfs2_free(&dx_leaves[i]);
+	}
+	ocfs2_free(&dx_leaves);
+	return 0;
+}
+
 static errcode_t ocfs2_expand_inline_dx_root(ocfs2_filesys *fs,
 					struct ocfs2_dx_root_block *dx_root)
 {
@@ -430,7 +443,8 @@ static errcode_t ocfs2_expand_inline_dx_root(ocfs2_filesys *fs,
 		goto out;

 out:
-	ocfs2_free(&dx_leaves);
+	if (dx_leaves)
+		(void)ocfs2_dx_dir_free_leaves(fs, dx_leaves);
 	return ret;
 }

@@ -799,20 +813,6 @@ out:
 	return ret;
 }

-static int ocfs2_dx_dir_free_leaves(ocfs2_filesys *fs,
-				struct ocfs2_dx_leaf **dx_leaves)
-{
-	int i, num;
-
-	num = ocfs2_clusters_to_blocks(fs, 1);
-	for (i = 0; i < num; i++) {
-		if (dx_leaves[i])
-			ocfs2_free(&dx_leaves[i]);
-	}
-	free(dx_leaves);
-	return 0;
-}
-
 /* from Linux kernel lib/sort.c */
 static void ocfs2_sort(void *base, size_t num, size_t size,
 			int (*cmp_func)(const void *, const void *),
@@ -1048,7 +1048,8 @@ void ocfs2_dx_dir_name_hash(ocfs2_filesys *fs,
 		goto out;
 	}

-	memcpy(buf, OCFS2_RAW_SB(fs->fs_super)->s_dx_seed, sizeof(buf));
+	memcpy(buf, OCFS2_RAW_SB(fs->fs_super)->s_dx_seed,
+			sizeof(OCFS2_RAW_SB(fs->fs_super)->s_dx_seed));

 	p = name;
 	while(len > 0) {
diff --git a/libocfs2/dir_iterate.c b/libocfs2/dir_iterate.c
index add3445..a80f47f 100644
--- a/libocfs2/dir_iterate.c
+++ b/libocfs2/dir_iterate.c
@@ -285,9 +285,6 @@ int ocfs2_process_dir_block(ocfs2_filesys *fs,
 	int		do_abort = 0;
 	int		entry;

-	if (blockcnt < 0)
-		return 0;
-
 	entry = blockcnt ? OCFS2_DIRENT_OTHER_FILE :
 		OCFS2_DIRENT_DOT_FILE;

diff --git a/libocfs2/extend_file.c b/libocfs2/extend_file.c
index fbb770a..172a164 100644
--- a/libocfs2/extend_file.c
+++ b/libocfs2/extend_file.c
@@ -198,6 +198,9 @@ errcode_t ocfs2_allocate_unwritten_extents(ocfs2_filesys *fs, uint64_t ino,
 		ret = ocfs2_extent_map_get_blocks(ci, v_blkno, 1,
 						  &p_blkno, &contig_blocks,
 						  NULL);
+		if (ret)
+			continue;
+
 		if (p_blkno) {
 			v_blkno += contig_blocks;
 			continue;
diff --git a/libocfs2/refcount.c b/libocfs2/refcount.c
index 6afe032..bb23530 100644
--- a/libocfs2/refcount.c
+++ b/libocfs2/refcount.c
@@ -2138,8 +2138,10 @@ static errcode_t create_generation(uint32_t *value)
 	if (randfd < 0)
 		return errno;

-	if (read(randfd, value, readlen) != readlen)
+	if (read(randfd, value, readlen) != readlen) {
+		close(randfd);
 		return errno;
+	}

 	close(randfd);

diff --git a/libocfs2/slot_map.c b/libocfs2/slot_map.c
index c33f458..995eb52 100644
--- a/libocfs2/slot_map.c
+++ b/libocfs2/slot_map.c
@@ -435,7 +435,7 @@ errcode_t ocfs2_format_slot_map(ocfs2_filesys *fs)
 {
 	errcode_t ret;
 	uint64_t blkno;
-	struct slotmap_format sf;
+	struct slotmap_format sf = {0};
 	struct ocfs2_slot_map_data *md = NULL;

 	ret = ocfs2_lookup_system_inode(fs, SLOT_MAP_SYSTEM_INODE, 0,
-- 
1.8.4.3




More information about the Ocfs2-tools-devel mailing list