Index: uspace/lib/ext4/libext4_directory_index.c
===================================================================
--- uspace/lib/ext4/libext4_directory_index.c	(revision e63ce679c8d778483bbfdb5b4002d12e23106fc8)
+++ uspace/lib/ext4/libext4_directory_index.c	(revision e8d054af6ad72693f6278e95399d139ef729e962)
@@ -383,5 +383,5 @@
 			result->block = leaf_block;
 			result->dentry = res_dentry;
-			return EOK;
+			goto cleanup;
 		}
 
@@ -400,5 +400,5 @@
 	} while (rc == 1);
 
-	return ENOENT;
+	rc = ENOENT;
 
 cleanup:
@@ -464,4 +464,5 @@
 	int rc = EOK;
 
+	// Allocate buffer for directory entries
 	uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
 	void *entry_buffer = malloc(block_size);
@@ -470,10 +471,8 @@
 	}
 
-	// Copy data to buffer
-	memcpy(entry_buffer, old_data_block->data, block_size);
-
 	// dot entry has the smallest size available
 	uint32_t max_entry_count =  block_size / sizeof(ext4_directory_dx_dot_entry_t);
 
+	// Allocate sort entry
 	ext4_dx_sort_entry_t *sort_array = malloc(max_entry_count * sizeof(ext4_dx_sort_entry_t));
 	if (sort_array == NULL) {
@@ -482,21 +481,21 @@
 	}
 
-	ext4_directory_entry_ll_t *dentry = old_data_block->data;
-
 	uint32_t idx = 0;
 	uint32_t real_size = 0;
-	void *entry_buffer_ptr = entry_buffer;
-
+
+	// Initialize hinfo
 	ext4_hash_info_t tmp_hinfo;
 	memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
 
+	// Load all valid entries to the buffer
+	ext4_directory_entry_ll_t *dentry = old_data_block->data;
+	void *entry_buffer_ptr = entry_buffer;
 	while ((void *)dentry < old_data_block->data + block_size) {
 
 		// Read only valid entries
 		if (ext4_directory_entry_ll_get_inode(dentry) != 0) {
-			char *name = (char *)dentry->name;
 
 			uint8_t len = ext4_directory_entry_ll_get_name_length(fs->superblock, dentry);
-			ext4_hash_string(&tmp_hinfo, len, name);
+			ext4_hash_string(&tmp_hinfo, len, (char *)dentry->name);
 
 			uint32_t rec_len = 8 + len;
@@ -788,11 +787,5 @@
 	}
 
-	if (rc != EOK) {
-		goto terminate;
-	}
-
-
 // TODO check rc handling
-terminate:
 	rc = block_put(new_block);
 	if (rc != EOK) {
@@ -807,5 +800,6 @@
 	rc = block_put(target_block);
 	if (rc != EOK) {
-		return rc;
+		EXT4FS_DBG("error writing target block");
+//		return rc;
 	}
 
@@ -814,7 +808,9 @@
 
 	while (dx_it <= dx_block) {
+		EXT4FS_DBG("dirty = \%s, refcount = \%u", dx_it->block->dirty ? "true" : "false", dx_it->block->refcnt);
 		rc = block_put(dx_it->block);
 		if (rc != EOK) {
-			return rc;
+			EXT4FS_DBG("error writing index block");
+//			return rc;
 		}
 		dx_it++;
