Index: uspace/lib/ext4/Makefile
===================================================================
--- uspace/lib/ext4/Makefile	(revision 6088193ffd64c208e1c21d029a45e322957f7e72)
+++ uspace/lib/ext4/Makefile	(revision 2674db6a4e996ff801e50122dc47e7e23ce0e20c)
@@ -33,4 +33,5 @@
 
 SOURCES = \
+	libext4_balloc.c \
 	libext4_bitmap.c \
 	libext4_block_group.c \
Index: uspace/lib/ext4/libext4.h
===================================================================
--- uspace/lib/ext4/libext4.h	(revision 6088193ffd64c208e1c21d029a45e322957f7e72)
+++ uspace/lib/ext4/libext4.h	(revision 2674db6a4e996ff801e50122dc47e7e23ce0e20c)
@@ -34,4 +34,5 @@
 #define LIBEXT4_LIBEXT4_H_
 
+#include "libext4_balloc.h"
 #include "libext4_bitmap.h"
 #include "libext4_block_group.h"
Index: uspace/lib/ext4/libext4_balloc.c
===================================================================
--- uspace/lib/ext4/libext4_balloc.c	(revision 2674db6a4e996ff801e50122dc47e7e23ce0e20c)
+++ uspace/lib/ext4/libext4_balloc.c	(revision 2674db6a4e996ff801e50122dc47e7e23ce0e20c)
@@ -0,0 +1,349 @@
+/*
+ * Copyright (c) 2011 Frantisek Princ
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libext4
+ * @{
+ */ 
+
+#include <errno.h>
+#include <sys/types.h>
+#include "libext4.h"
+
+int ext4_bitmap_free_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t block_index)
+{
+	int rc;
+	uint32_t blocks_per_group;
+	uint32_t block_group;
+	uint32_t index_in_group;
+	uint32_t bitmap_block;
+	uint32_t block_size;
+	ext4_block_group_ref_t *bg_ref;
+	block_t *block;
+	uint32_t first_block;
+
+	block_size = ext4_superblock_get_block_size(fs->superblock);
+	blocks_per_group = ext4_superblock_get_blocks_per_group(fs->superblock);
+	first_block = ext4_superblock_get_first_data_block(fs->superblock);
+
+	// First block == 0 or 1
+	if (first_block == 0) {
+		block_group = block_index / blocks_per_group;
+		index_in_group = block_index % blocks_per_group;
+	} else {
+		block_group = (block_index - 1) / blocks_per_group;
+		index_in_group = (block_index - 1) % blocks_per_group;
+	}
+
+	rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	bitmap_block = ext4_block_group_get_block_bitmap(bg_ref->block_group);
+
+	rc = block_get(&block, fs->device, bitmap_block, 0);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	ext4_bitmap_free_bit(block->data, index_in_group);
+
+	block->dirty = true;
+	rc = block_put(block);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
+	ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
+	ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
+	inode_ref->dirty = true;
+
+	uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg_ref->block_group);
+	free_blocks++;
+	ext4_block_group_set_free_blocks_count(bg_ref->block_group, free_blocks);
+	bg_ref->dirty = true;
+
+	// TODO change free blocks count in superblock
+
+	rc = ext4_filesystem_put_block_group_ref(bg_ref);
+	if (rc != EOK) {
+		EXT4FS_DBG("error in saving bg_ref \%d", rc);
+		// TODO error
+		return rc;
+	}
+
+	return EOK;
+}
+
+
+static uint32_t ext4_bitmap_find_goal(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref)
+{
+	uint32_t goal = 0;
+
+	int rc;
+	uint64_t inode_size = ext4_inode_get_size(fs->superblock, inode_ref->inode);
+	uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
+	uint32_t inode_block_count = inode_size / block_size;
+
+
+	if (inode_size % block_size != 0) {
+		inode_block_count++;
+	}
+
+	if (inode_block_count > 0) {
+		// TODO check retval
+//		EXT4FS_DBG("has blocks");
+		ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, inode_block_count - 1, &goal);
+
+		// TODO
+		// If goal == 0 -> SPARSE file !!!
+		goal++;
+		return goal;
+	}
+
+//	uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(fs->superblock);
+
+	// Identify block group of inode
+	uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
+	uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
+	block_size = ext4_superblock_get_block_size(fs->superblock);
+
+	ext4_block_group_ref_t *bg_ref;
+	rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
+	if (rc != EOK) {
+		return 0;
+	}
+
+	uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(bg_ref->block_group);
+	uint16_t inode_table_item_size = ext4_superblock_get_inode_size(fs->superblock);
+	uint32_t inode_table_bytes = inodes_per_group * inode_table_item_size;
+	uint32_t inode_table_blocks = inode_table_bytes / block_size;
+
+	if (inode_table_bytes % block_size) {
+		inode_table_blocks++;
+	}
+
+	goal = inode_table_first_block + inode_table_blocks;
+
+	ext4_filesystem_put_block_group_ref(bg_ref);
+
+	return goal;
+}
+
+int ext4_bitmap_alloc_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t *fblock)
+{
+	int rc;
+	ext4_block_group_ref_t *bg_ref;
+	uint32_t bitmap_block;
+	block_t *block;
+	uint32_t rel_block_idx = 0;
+	uint32_t index_in_group;
+	uint32_t tmp;
+
+	uint32_t allocated_block = 0;
+
+	// Determine GOAL
+	uint32_t goal = ext4_bitmap_find_goal(fs, inode_ref);
+
+	uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
+
+	//if (goal == 0) - unable to determine goal
+	if (goal == 0) {
+		// TODO
+		EXT4FS_DBG("ERRORR (goal == 0)");
+	}
+
+//	EXT4FS_DBG("goal = \%u", goal);
+
+	uint32_t blocks_per_group = ext4_superblock_get_blocks_per_group(fs->superblock);
+	uint32_t first_block = ext4_superblock_get_first_data_block(fs->superblock);
+
+	uint32_t block_group;
+
+	// First block == 0 or 1
+	if (first_block == 0) {
+		block_group = goal / blocks_per_group;
+		index_in_group = goal % blocks_per_group;
+	} else {
+		block_group = (goal - 1) / blocks_per_group;
+		index_in_group = (goal - 1) % blocks_per_group;
+	}
+
+
+//	EXT4FS_DBG("block_group = \%u, index_in_group = \%u", block_group, index_in_group);
+
+	rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	// Load bitmap
+	bitmap_block = ext4_block_group_get_block_bitmap(bg_ref->block_group);
+
+	rc = block_get(&block, fs->device, bitmap_block, 0);
+	if (rc != EOK) {
+		ext4_filesystem_put_block_group_ref(bg_ref);
+		return rc;
+	}
+
+//	EXT4FS_DBG("bitmap loaded");
+
+	if (ext4_bitmap_is_free_bit(block->data, index_in_group)) {
+
+//		EXT4FS_DBG("goal is free");
+
+		ext4_bitmap_set_bit(block->data, index_in_group);
+		block->dirty = true;
+		rc = block_put(block);
+		if (rc != EOK) {
+			// TODO error
+			EXT4FS_DBG("error in saving bitmap \%d", rc);
+		}
+
+		allocated_block = goal;
+		goto end;
+
+	}
+
+//	EXT4FS_DBG("try 63 blocks after goal");
+	// Try to find free block near to goal
+	for (tmp = index_in_group + 1; (tmp < blocks_per_group) && (tmp < ((index_in_group + 63) & ~63)); ++tmp) {
+
+//		EXT4FS_DBG("trying \%u", tmp);
+
+		if (ext4_bitmap_is_free_bit(block->data, tmp)) {
+
+//			EXT4FS_DBG("block \%u is free -> allocate it", tmp);
+
+			ext4_bitmap_set_bit(block->data, tmp);
+			block->dirty = true;
+			rc = block_put(block);
+			if (rc != EOK) {
+				// TODO error
+				EXT4FS_DBG("error in saving bitmap \%d", rc);
+			}
+
+			if (first_block == 0) {
+				allocated_block = blocks_per_group * block_group + tmp;
+			} else {
+				allocated_block = blocks_per_group * block_group + tmp + 1;
+			}
+
+			goto end;
+		}
+	}
+
+//	EXT4FS_DBG("try find free byte");
+
+	// Find free BYTE in bitmap
+	rc = ext4_bitmap_find_free_byte_and_set_bit(block->data, index_in_group, &rel_block_idx, block_size);
+	if (rc == EOK) {
+		block->dirty = true;
+		rc = block_put(block);
+		if (rc != EOK) {
+			// TODO error
+			EXT4FS_DBG("error in saving bitmap \%d", rc);
+		}
+
+		if (first_block == 0) {
+			allocated_block = blocks_per_group * block_group + rel_block_idx;
+		} else {
+			allocated_block = blocks_per_group * block_group + rel_block_idx + 1;
+		}
+
+		goto end;
+	}
+
+//	EXT4FS_DBG("try find free bit");
+
+	// Find free bit in bitmap
+	rc = ext4_bitmap_find_free_bit_and_set(block->data, index_in_group, &rel_block_idx, block_size);
+	if (rc == EOK) {
+		block->dirty = true;
+		rc = block_put(block);
+		if (rc != EOK) {
+			// TODO error
+			EXT4FS_DBG("error in saving bitmap \%d", rc);
+		}
+
+		if (first_block == 0) {
+			allocated_block = blocks_per_group * block_group + rel_block_idx;
+		} else {
+			allocated_block = blocks_per_group * block_group + rel_block_idx + 1;
+		}
+
+		goto end;
+	}
+
+
+	// TODO Try other block groups
+	EXT4FS_DBG("try other block group");
+	return ENOSPC;
+
+end:
+
+	;
+//	EXT4FS_DBG("returning block \%u", allocated_block);
+
+	// TODO decrement superblock free blocks count
+	//uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
+	//sb_free_blocks--;
+	//ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
+
+	uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
+	ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
+	ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
+	inode_ref->dirty = true;
+
+	uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(bg_ref->block_group);
+	bg_free_blocks--;
+	ext4_block_group_set_free_blocks_count(bg_ref->block_group, bg_free_blocks);
+	bg_ref->dirty = true;
+
+	ext4_filesystem_put_block_group_ref(bg_ref);
+
+//	EXT4FS_DBG("block \%u allocated", blocks_per_group * block_group + rel_block_idx + 1);
+
+
+
+	*fblock = allocated_block;
+	return EOK;
+}
+
+
+/**
+ * @file	libext4_balloc.c
+ * @brief	TODO
+ */
+
+
+
+/**
+ * @}
+ */ 
Index: uspace/lib/ext4/libext4_balloc.h
===================================================================
--- uspace/lib/ext4/libext4_balloc.h	(revision 2674db6a4e996ff801e50122dc47e7e23ce0e20c)
+++ uspace/lib/ext4/libext4_balloc.h	(revision 2674db6a4e996ff801e50122dc47e7e23ce0e20c)
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2011 Frantisek Princ
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * - The name of the author may not be used to endorse or promote products
+ *   derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/** @addtogroup libext4
+ * @{
+ */ 
+
+#ifndef LIBEXT4_LIBEXT4_BALLOC_H_
+#define LIBEXT4_LIBEXT4_BALLOC_H_
+
+#include <sys/types.h>
+#include "libext4_filesystem.h"
+
+extern int ext4_bitmap_free_block(ext4_filesystem_t *,
+		ext4_inode_ref_t *, uint32_t);
+extern int ext4_bitmap_alloc_block(ext4_filesystem_t *,
+		ext4_inode_ref_t *, uint32_t *);
+
+#endif
+
+/**
+ * @}
+ */
Index: uspace/lib/ext4/libext4_bitmap.c
===================================================================
--- uspace/lib/ext4/libext4_bitmap.c	(revision 6088193ffd64c208e1c21d029a45e322957f7e72)
+++ uspace/lib/ext4/libext4_bitmap.c	(revision 2674db6a4e996ff801e50122dc47e7e23ce0e20c)
@@ -33,5 +33,5 @@
 /**
  * @file	libext4_bitmap.c
- * @brief	Ext4 bitmap (block & inode) operations.
+ * @brief	Ext4 bitmap operations.
  */
 
@@ -41,25 +41,62 @@
 #include "libext4.h"
 
-static void ext4_bitmap_free_bit(uint8_t *bitmap, uint32_t index)
+void ext4_bitmap_free_bit(uint8_t *bitmap, uint32_t index)
 {
-	// Block numbers are 1-based
 	uint32_t byte_index = index / 8;
 	uint32_t bit_index = index % 8;
 
-//	EXT4FS_DBG("freeing block \%u, byte \%u and bit \%u", index, byte_index, bit_index);
+	uint8_t *target = bitmap + byte_index;
+
+	*target &= ~ (1 << bit_index);
+}
+
+void ext4_bitmap_set_bit(uint8_t *bitmap, uint32_t index)
+{
+	uint32_t byte_index = index / 8;
+	uint32_t bit_index = index % 8;
 
 	uint8_t *target = bitmap + byte_index;
-	uint8_t old_value = *target;
 
-	uint8_t new_value = old_value & (~ (1 << bit_index));
-
-	*target = new_value;
+	*target |= 1 << bit_index;
 }
 
-static int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t *index, uint32_t size)
+bool ext4_bitmap_is_free_bit(uint8_t *bitmap, uint32_t index)
 {
-	uint8_t *pos = bitmap;
+	uint32_t byte_index = index / 8;
+	uint32_t bit_index = index % 8;
+
+	uint8_t *target = bitmap + byte_index;
+
+	if (*target & (1 << bit_index)) {
+		return false;
+	} else {
+		return true;
+	}
+
+}
+
+int ext4_bitmap_find_free_byte_and_set_bit(uint8_t *bitmap, uint32_t start, uint32_t *index, uint32_t size)
+{
+	uint8_t *pos = bitmap + (start / 8) + 1;
+
+	while (pos < bitmap + size) {
+		if (*pos == 0) {
+			*pos |= 1;
+
+			*index = (pos - bitmap) * 8;
+			return EOK;
+		}
+
+		++pos;
+	}
+
+	return ENOSPC;
+
+}
+
+int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t start, uint32_t *index, uint32_t size)
+{
+	uint8_t *pos = bitmap + (start / 8);
 	int i;
-	uint32_t idx = 0;
 	uint8_t value, new_value;
 
@@ -71,10 +108,21 @@
 
 		++pos;
-		idx += 8;
+	}
+
+	// Check the byte containing start
+	if (pos == bitmap + (start / 8)) {
+		for (i = start % 8; i < 8; ++i) {
+			value = *pos;
+			if ((value & (1 << i)) == 0) {
+				// free bit found
+				new_value = value | (1 << i);
+				*pos = new_value;
+				*index = (pos - bitmap) * 8 + i;
+				return EOK;
+			}
+		}
 	}
 
 	if (pos < bitmap + size) {
-
-//		EXT4FS_DBG("byte found \%u", (uint32_t)(pos - bitmap));
 
 		for(i = 0; i < 8; ++i) {
@@ -83,8 +131,7 @@
 			if ((value & (1 << i)) == 0) {
 				// free bit found
-//				EXT4FS_DBG("bit found \%u", i);
 				new_value = value | (1 << i);
 				*pos = new_value;
-				*index = idx + i;
+				*index = (pos - bitmap) * 8 + i;
 				return EOK;
 			}
@@ -95,133 +142,4 @@
 }
 
-int ext4_bitmap_free_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t block_index)
-{
-	int rc;
-	uint32_t blocks_per_group;
-	uint32_t block_group;
-	uint32_t index_in_group;
-	uint32_t bitmap_block;
-	uint32_t block_size;
-	ext4_block_group_ref_t *bg_ref;
-	block_t *block;
-
-	block_size = ext4_superblock_get_block_size(fs->superblock);
-
-	blocks_per_group = ext4_superblock_get_blocks_per_group(fs->superblock);
-	block_group = ((block_index - 1) / blocks_per_group);
-	index_in_group = (block_index - 1) % blocks_per_group;
-
-	rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
-	if (rc != EOK) {
-		return rc;
-	}
-
-	bitmap_block = ext4_block_group_get_block_bitmap(bg_ref->block_group);
-
-	rc = block_get(&block, fs->device, bitmap_block, 0);
-	if (rc != EOK) {
-		return rc;
-	}
-
-	ext4_bitmap_free_bit(block->data, index_in_group);
-
-	block->dirty = true;
-	rc = block_put(block);
-	if (rc != EOK) {
-		return rc;
-	}
-
-	uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
-	ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
-	ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
-	inode_ref->dirty = true;
-
-	uint32_t free_blocks = ext4_block_group_get_free_blocks_count(bg_ref->block_group);
-	free_blocks++;
-	ext4_block_group_set_free_blocks_count(bg_ref->block_group, free_blocks);
-	bg_ref->dirty = true;
-
-	// TODO change free blocks count in superblock
-
-	rc = ext4_filesystem_put_block_group_ref(bg_ref);
-	if (rc != EOK) {
-		EXT4FS_DBG("error in saving bg_ref \%d", rc);
-		// TODO error
-		return rc;
-	}
-
-//	EXT4FS_DBG("block \%u released", block_index);
-
-	return EOK;
-}
-
-int ext4_bitmap_alloc_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t *fblock)
-{
-	int rc;
-	uint32_t inodes_per_group, block_group, blocks_per_group;
-	ext4_block_group_ref_t *bg_ref;
-	uint32_t bitmap_block;
-	block_t *block;
-	uint32_t rel_block_idx = 0;
-	uint32_t block_size;
-
-	inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
-	block_group = (inode_ref->index - 1) / inodes_per_group;
-
-	block_size = ext4_superblock_get_block_size(fs->superblock);
-
-	rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
-	if (rc != EOK) {
-		return rc;
-	}
-
-	bitmap_block = ext4_block_group_get_block_bitmap(bg_ref->block_group);
-
-	rc = block_get(&block, fs->device, bitmap_block, 0);
-	if (rc != EOK) {
-		ext4_filesystem_put_block_group_ref(bg_ref);
-		return rc;
-	}
-
-	rc = ext4_bitmap_find_free_bit_and_set(block->data, &rel_block_idx, block_size);
-	if (rc != EOK) {
-		EXT4FS_DBG("no free block found");
-		// TODO if ENOSPC - try next block groups
-	}
-
-	block->dirty = true;
-
-	rc = block_put(block);
-	if (rc != EOK) {
-		// TODO error
-		EXT4FS_DBG("error in saving bitmap \%d", rc);
-	}
-
-	// TODO decrement superblock free blocks count
-	//uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
-	//sb_free_blocks--;
-	//ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
-
-	uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
-	ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
-	ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
-	inode_ref->dirty = true;
-
-	uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(bg_ref->block_group);
-	bg_free_blocks--;
-	ext4_block_group_set_free_blocks_count(bg_ref->block_group, bg_free_blocks);
-	bg_ref->dirty = true;
-
-	ext4_filesystem_put_block_group_ref(bg_ref);
-
-	blocks_per_group = ext4_superblock_get_blocks_per_group(fs->superblock);
-
-//	EXT4FS_DBG("block \%u allocated", blocks_per_group * block_group + rel_block_idx + 1);
-
-	*fblock = blocks_per_group * block_group + rel_block_idx + 1;
-	return EOK;
-
-}
-
 /**
  * @}
Index: uspace/lib/ext4/libext4_bitmap.h
===================================================================
--- uspace/lib/ext4/libext4_bitmap.h	(revision 6088193ffd64c208e1c21d029a45e322957f7e72)
+++ uspace/lib/ext4/libext4_bitmap.h	(revision 2674db6a4e996ff801e50122dc47e7e23ce0e20c)
@@ -35,10 +35,13 @@
 
 #include <sys/types.h>
-#include "libext4_filesystem.h"
 
-extern int ext4_bitmap_free_block(ext4_filesystem_t *,
-		ext4_inode_ref_t *, uint32_t);
-extern int ext4_bitmap_alloc_block(ext4_filesystem_t *,
-		ext4_inode_ref_t *, uint32_t *);
+extern void ext4_bitmap_free_bit(uint8_t *, uint32_t);
+extern void ext4_bitmap_set_bit(uint8_t *, uint32_t);
+extern bool ext4_bitmap_is_free_bit(uint8_t *, uint32_t);
+extern int ext4_bitmap_find_free_byte_and_set_bit(uint8_t *, uint32_t,
+		uint32_t *, uint32_t);
+extern int ext4_bitmap_find_free_bit_and_set(uint8_t *, uint32_t,
+		uint32_t *, uint32_t);
+
 #endif
 
Index: uspace/lib/ext4/libext4_filesystem.c
===================================================================
--- uspace/lib/ext4/libext4_filesystem.c	(revision 6088193ffd64c208e1c21d029a45e322957f7e72)
+++ uspace/lib/ext4/libext4_filesystem.c	(revision 2674db6a4e996ff801e50122dc47e7e23ce0e20c)
@@ -72,5 +72,6 @@
 
 	/* Initialize block caching */
-	rc = block_cache_init(service_id, block_size, 0, CACHE_MODE_WT);
+	// TODO set cache MODE to write through (now writeback for faster testing)
+	rc = block_cache_init(service_id, block_size, 0, CACHE_MODE_WB);
 	if (rc != EOK) {
 		block_fini(fs->device);
@@ -465,4 +466,5 @@
 				if (rc != EOK) {
 					// TODO error
+					EXT4FS_DBG("allocation error");
 				}
 
