Index: uspace/lib/ext4/libext4_directory.c
===================================================================
--- uspace/lib/ext4/libext4_directory.c	(revision c4f318d6780aaab9225c632703fc274e1e69aa03)
+++ uspace/lib/ext4/libext4_directory.c	(revision 1d68621f5d08a671b1dcaeb82325a6ff06f853a2)
@@ -323,6 +323,4 @@
 			ext4_inode_has_flag(parent->inode, EXT4_INODE_FLAG_INDEX)) {
 
-		EXT4FS_DBG("trying INDEX");
-
 		rc = ext4_directory_dx_add_entry(fs, parent, child, name_len, entry_name);
 
@@ -464,8 +462,4 @@
 
 		EXT4FS_DBG("index is corrupted - doing linear search");
-
-		// TODO Needed to clear dir index flag
-		//ext4_inode_clear_flag(parent->inode, EXT4_INODE_FLAG_INDEX);
-		//parent->dirty = true;
 
 	}
Index: uspace/lib/ext4/libext4_directory_index.c
===================================================================
--- uspace/lib/ext4/libext4_directory_index.c	(revision c4f318d6780aaab9225c632703fc274e1e69aa03)
+++ uspace/lib/ext4/libext4_directory_index.c	(revision 1d68621f5d08a671b1dcaeb82325a6ff06f853a2)
@@ -491,8 +491,8 @@
 	memcpy(entry_buffer, old_data_block->data, block_size);
 
-	// dot entry has the smalest size available
-	uint32_t entry_count =  block_size / sizeof(ext4_directory_dx_dot_entry_t);
-
-	ext4_dx_sort_entry_t *sort_array = malloc(entry_count * sizeof(ext4_dx_sort_entry_t));
+	// dot entry has the smallest size available
+	uint32_t max_entry_count =  block_size / sizeof(ext4_directory_dx_dot_entry_t);
+
+	ext4_dx_sort_entry_t *sort_array = malloc(max_entry_count * sizeof(ext4_dx_sort_entry_t));
 	if (sort_array == NULL) {
 		free(entry_buffer);
@@ -500,20 +500,18 @@
 	}
 
-	EXT4FS_DBG("sort_array allocated addr = \%u, size = \%u", (uint32_t)sort_array, entry_count * sizeof(ext4_dx_sort_entry_t));
-
 	ext4_directory_entry_ll_t *dentry = old_data_block->data;
 
-	// TODO tady nekde je chyba
-
-	EXT4FS_DBG("entry_buffer = \%u", (uint32_t)entry_buffer);
-
-	int idx = 0;
+	uint32_t idx = 0;
 	uint32_t real_size = 0;
 	void *entry_buffer_ptr = entry_buffer;
+
+	ext4_hash_info_t tmp_hinfo;
+	memcpy(&tmp_hinfo, hinfo, sizeof(ext4_hash_info_t));
+
 	while ((void *)dentry < old_data_block->data + block_size) {
 		char *name = (char *)dentry->name;
 
 		uint8_t len = ext4_directory_entry_ll_get_name_length(fs->superblock, dentry);
-		uint32_t hash = ext4_hash_string(hinfo, len, name);
+		ext4_hash_string(&tmp_hinfo, len, name);
 
 		uint32_t rec_len = 8 + len;
@@ -527,5 +525,5 @@
 		sort_array[idx].dentry = entry_buffer_ptr;
 		sort_array[idx].rec_len = rec_len;
-		sort_array[idx].hash = hash;
+		sort_array[idx].hash = tmp_hinfo.hash;
 
 		entry_buffer_ptr += rec_len;
@@ -536,5 +534,5 @@
 	}
 
-	qsort(sort_array, entry_count, sizeof(ext4_dx_sort_entry_t), dx_entry_comparator, NULL);
+	qsort(sort_array, idx, sizeof(ext4_dx_sort_entry_t), dx_entry_comparator, NULL);
 
 	uint32_t new_fblock;
@@ -547,5 +545,5 @@
 	}
 
-	EXT4FS_DBG("new block appended (iblock = \%u)", new_iblock);
+//	EXT4FS_DBG("new block appended (iblock = \%u, fblock = \%u)", new_iblock, new_fblock);
 
 	// Load new block
@@ -562,7 +560,7 @@
 	uint32_t new_hash = 0;
 	uint32_t current_size = 0;
-	int mid = 0;
-	for (int i = 0; i < idx; ++i) {
-		if (current_size > real_size / 2) {
+	uint32_t mid = 0;
+	for (uint32_t i = 0; i < idx; ++i) {
+		if ((current_size + sort_array[i].rec_len) > (real_size / 2)) {
 			new_hash = sort_array[i].hash;
 			mid = i;
@@ -577,5 +575,5 @@
 
 	// First part - to the old block
-	for (int i = 0; i < mid; ++i) {
+	for (uint32_t i = 0; i < mid; ++i) {
 		ptr = old_data_block->data + offset;
 		memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
@@ -593,5 +591,5 @@
 	// Second part - to the new block
 	offset = 0;
-	for (int i = mid; i < idx; ++i) {
+	for (uint32_t i = mid; i < idx; ++i) {
 		ptr = new_data_block_tmp->data + offset;
 		memcpy(ptr, sort_array[i].dentry, sort_array[i].rec_len);
@@ -613,14 +611,20 @@
 	free(entry_buffer);
 
+	ext4_directory_dx_entry_t *old_index_entry = index_block->position;
+	ext4_directory_dx_entry_t *new_index_entry = old_index_entry + 1;
+
 	ext4_directory_dx_countlimit_t *countlimit =
 			(ext4_directory_dx_countlimit_t *)index_block->entries;
 	uint32_t count = ext4_directory_dx_countlimit_get_count(countlimit);
-	count++;
-	ext4_directory_dx_countlimit_set_count(countlimit, count);
-
-	index_block->position++;
-
-	ext4_directory_dx_entry_set_block(index_block->position, new_iblock);
-	ext4_directory_dx_entry_set_hash(index_block->position, new_hash);
+
+	ext4_directory_dx_entry_t *start_index = index_block->entries;
+	size_t bytes = (void *)(start_index + count) - (void *)(new_index_entry);
+
+	memmove(new_index_entry + 1, new_index_entry, bytes);
+
+	ext4_directory_dx_entry_set_block(new_index_entry, new_iblock);
+	ext4_directory_dx_entry_set_hash(new_index_entry, new_hash);
+
+	ext4_directory_dx_countlimit_set_count(countlimit, count + 1);
 
 	index_block->block->dirty = true;
@@ -792,6 +796,5 @@
 
 	// TODO Where to save new entry
-	// Position in dx_block is set to NEW block entry
-	uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position);
+	uint32_t new_block_hash = ext4_directory_dx_entry_get_hash(dx_block->position + 1);
 	if (hinfo.hash >= new_block_hash) {
 		de = new_block->data;
@@ -825,6 +828,4 @@
    			if (free_space >= required_len) {
 
-   				EXT4FS_DBG("rec_len = \%u, used_space = \%u, free space = \%u", de_rec_len, used_space, free_space);
-
    				// Cut tail of current entry
    				ext4_directory_entry_ll_set_entry_length(de, used_space);
