Index: uspace/lib/ext4/libext4_balloc.c
===================================================================
--- uspace/lib/ext4/libext4_balloc.c	(revision 9104bb52fb95a82c4ca9099469079a13710b3044)
+++ uspace/lib/ext4/libext4_balloc.c	(revision 1ac1ab45ddda2b390c1b5c8f669d639559500b3d)
@@ -82,10 +82,13 @@
 
 
-int ext4_balloc_free_block(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, uint32_t block_addr)
+int ext4_balloc_free_block(ext4_inode_ref_t *inode_ref, uint32_t block_addr)
 {
 	int rc;
 
-	uint32_t block_group = ext4_balloc_get_bgid_of_block(fs->superblock, block_addr);
-	uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(fs->superblock, block_addr);
+	ext4_filesystem_t *fs = inode_ref->fs;
+	ext4_superblock_t *sb = fs->superblock;
+
+	uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, block_addr);
+	uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(sb, block_addr);
 
 	ext4_block_group_ref_t *bg_ref;
@@ -97,5 +100,5 @@
 
 	uint32_t bitmap_block_addr = ext4_block_group_get_block_bitmap(
-			bg_ref->block_group, fs->superblock);
+			bg_ref->block_group, sb);
 	block_t *bitmap_block;
 	rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
@@ -116,23 +119,23 @@
 	}
 
-	uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
+	uint32_t block_size = ext4_superblock_get_block_size(sb);
 
 	// Update superblock free blocks count
-	uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(fs->superblock);
+	uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
 	sb_free_blocks--;
-	ext4_superblock_set_free_blocks_count(fs->superblock, sb_free_blocks);
+	ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
 
 	// Update inode blocks count
-	uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
+	uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
 	ino_blocks -= block_size / EXT4_INODE_BLOCK_SIZE;
-	ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
+	ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
 	inode_ref->dirty = true;
 
 	// Update block group free blocks count
 	uint32_t free_blocks = ext4_block_group_get_free_blocks_count(
-			bg_ref->block_group, fs->superblock);
+			bg_ref->block_group, sb);
 	free_blocks++;
 	ext4_block_group_set_free_blocks_count(bg_ref->block_group,
-			fs->superblock, free_blocks);
+			sb, free_blocks);
 	bg_ref->dirty = true;
 
@@ -178,11 +181,13 @@
 
 
-static uint32_t ext4_balloc_find_goal(ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref)
+static uint32_t ext4_balloc_find_goal(ext4_inode_ref_t *inode_ref)
 {
 	int rc;
 	uint32_t goal = 0;
 
-	uint64_t inode_size = ext4_inode_get_size(fs->superblock, inode_ref->inode);
-	uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
+	ext4_superblock_t *sb = inode_ref->fs->superblock;
+
+	uint64_t inode_size = ext4_inode_get_size(sb, inode_ref->inode);
+	uint32_t block_size = ext4_superblock_get_block_size(sb);
 	uint32_t inode_block_count = inode_size / block_size;
 
@@ -193,5 +198,5 @@
 	if (inode_block_count > 0) {
 		// TODO check retval
-		ext4_filesystem_get_inode_data_block_index(fs, inode_ref, inode_block_count - 1, &goal);
+		ext4_filesystem_get_inode_data_block_index(inode_ref, inode_block_count - 1, &goal);
 
 		// TODO
@@ -203,18 +208,18 @@
 
 	// Identify block group of inode
-	uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
+	uint32_t inodes_per_group = ext4_superblock_get_inodes_per_group(sb);
 	uint32_t block_group = (inode_ref->index - 1) / inodes_per_group;
-	block_size = ext4_superblock_get_block_size(fs->superblock);
+	block_size = ext4_superblock_get_block_size(sb);
 
 	ext4_block_group_ref_t *bg_ref;
-	rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
+	rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
 	if (rc != EOK) {
 		return 0;
 	}
 
-	uint32_t block_group_count = ext4_superblock_get_block_group_count(fs->superblock);
+	uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
 	uint32_t inode_table_first_block = ext4_block_group_get_inode_table_first_block(
-			bg_ref->block_group, fs->superblock);
-	uint16_t inode_table_item_size = ext4_superblock_get_inode_size(fs->superblock);
+			bg_ref->block_group, sb);
+	uint16_t inode_table_item_size = ext4_superblock_get_inode_size(sb);
 	uint32_t inode_table_bytes;
 
@@ -223,5 +228,5 @@
 	} else {
 		// last block group could be smaller
-		uint32_t inodes_count_total = ext4_superblock_get_inodes_count(fs->superblock);
+		uint32_t inodes_count_total = ext4_superblock_get_inodes_count(sb);
 		inode_table_bytes =
 				(inodes_count_total - ((block_group_count - 1) * inodes_per_group))
@@ -242,5 +247,5 @@
 }
 
-int ext4_balloc_alloc_block(ext4_filesystem_t *fs,
+int ext4_balloc_alloc_block(
 		ext4_inode_ref_t *inode_ref, uint32_t *fblock)
 {
@@ -253,5 +258,5 @@
 
 	// Find GOAL
-	uint32_t goal = ext4_balloc_find_goal(fs, inode_ref);
+	uint32_t goal = ext4_balloc_find_goal(inode_ref);
 	if (goal == 0) {
 		// TODO
@@ -260,11 +265,13 @@
 	}
 
+	ext4_superblock_t *sb = inode_ref->fs->superblock;
+
 	// Load block group number for goal and relative index
-	uint32_t block_group = ext4_balloc_get_bgid_of_block(fs->superblock, goal);
-	uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(fs->superblock, goal);
+	uint32_t block_group = ext4_balloc_get_bgid_of_block(sb, goal);
+	uint32_t index_in_group = ext4_balloc_blockaddr2_index_in_group(sb, goal);
 
 
 	ext4_block_group_ref_t *bg_ref;
-	rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
+	rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, block_group, &bg_ref);
 	if (rc != EOK) {
 		EXT4FS_DBG("initial BG ref not loaded");
@@ -273,9 +280,9 @@
 
 	uint32_t first_in_group =
-			ext4_balloc_get_first_data_block_in_group(fs->superblock,
+			ext4_balloc_get_first_data_block_in_group(sb,
 					bg_ref->block_group, block_group);
 
 	uint32_t first_in_group_index = ext4_balloc_blockaddr2_index_in_group(
-			fs->superblock, first_in_group);
+			sb, first_in_group);
 
 	if (index_in_group < first_in_group_index) {
@@ -285,7 +292,7 @@
 	// Load bitmap
 	bitmap_block_addr = ext4_block_group_get_block_bitmap(bg_ref->block_group,
-			fs->superblock);
-
-	rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
+			sb);
+
+	rc = block_get(&bitmap_block, inode_ref->fs->device, bitmap_block_addr, 0);
 	if (rc != EOK) {
 		ext4_filesystem_put_block_group_ref(bg_ref);
@@ -311,5 +318,5 @@
 	}
 
-	uint32_t blocks_in_group = ext4_superblock_get_blocks_in_group(fs->superblock, block_group);
+	uint32_t blocks_in_group = ext4_superblock_get_blocks_in_group(sb, block_group);
 
 	uint32_t end_idx = (index_in_group + 63) & ~63;
@@ -331,5 +338,5 @@
 
 			allocated_block = ext4_balloc_index_in_group2blockaddr(
-					fs->superblock, tmp_idx, block_group);
+					sb, tmp_idx, block_group);
 
 			goto success;
@@ -349,5 +356,5 @@
 
 		allocated_block = ext4_balloc_index_in_group2blockaddr(
-				fs->superblock, rel_block_idx, block_group);
+				sb, rel_block_idx, block_group);
 
 		goto success;
@@ -365,5 +372,5 @@
 
 		allocated_block = ext4_balloc_index_in_group2blockaddr(
-				fs->superblock, rel_block_idx, block_group);
+				sb, rel_block_idx, block_group);
 
 		goto success;
@@ -375,5 +382,5 @@
 
 	// Try other block groups
-	uint32_t block_group_count = ext4_superblock_get_block_group_count(fs->superblock);
+	uint32_t block_group_count = ext4_superblock_get_block_group_count(sb);
 
 	uint32_t bgid = (block_group + 1) % block_group_count;
@@ -381,5 +388,5 @@
 
 	while (count > 0) {
-		rc = ext4_filesystem_get_block_group_ref(fs, bgid, &bg_ref);
+		rc = ext4_filesystem_get_block_group_ref(inode_ref->fs, bgid, &bg_ref);
 		if (rc != EOK) {
 			EXT4FS_DBG("errrrrrrrrrrr");
@@ -389,7 +396,7 @@
 		// Load bitmap
 		bitmap_block_addr = ext4_block_group_get_block_bitmap(
-				bg_ref->block_group, fs->superblock);
-
-		rc = block_get(&bitmap_block, fs->device, bitmap_block_addr, 0);
+				bg_ref->block_group, sb);
+
+		rc = block_get(&bitmap_block, inode_ref->fs->device, bitmap_block_addr, 0);
 		if (rc != EOK) {
 			ext4_filesystem_put_block_group_ref(bg_ref);
@@ -399,11 +406,11 @@
 
 		first_in_group = ext4_balloc_get_first_data_block_in_group(
-				fs->superblock, bg_ref->block_group, bgid);
-		index_in_group = ext4_balloc_blockaddr2_index_in_group(fs->superblock,
+				sb, bg_ref->block_group, bgid);
+		index_in_group = ext4_balloc_blockaddr2_index_in_group(sb,
 						first_in_group);
-		blocks_in_group = ext4_superblock_get_blocks_in_group(fs->superblock, bgid);
+		blocks_in_group = ext4_superblock_get_blocks_in_group(sb, bgid);
 
 		first_in_group_index = ext4_balloc_blockaddr2_index_in_group(
-			fs->superblock, first_in_group);
+			sb, first_in_group);
 
 		if (index_in_group < first_in_group_index) {
@@ -422,5 +429,5 @@
 
 			allocated_block = ext4_balloc_index_in_group2blockaddr(
-					fs->superblock, rel_block_idx, bgid);
+					sb, rel_block_idx, bgid);
 
 			goto success;
@@ -438,5 +445,5 @@
 
 			allocated_block = ext4_balloc_index_in_group2blockaddr(
-					fs->superblock, rel_block_idx, bgid);
+					sb, rel_block_idx, bgid);
 
 			goto success;
@@ -456,24 +463,23 @@
 	; 	// Empty command - because of syntax
 	
-	uint32_t block_size = ext4_superblock_get_block_size(fs->superblock);
+	uint32_t block_size = ext4_superblock_get_block_size(sb);
 
 	// Update superblock free blocks count
-	uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(fs->superblock);
+	uint32_t sb_free_blocks = ext4_superblock_get_free_blocks_count(sb);
 	sb_free_blocks--;
-	ext4_superblock_set_free_blocks_count(fs->superblock, sb_free_blocks);
+	ext4_superblock_set_free_blocks_count(sb, sb_free_blocks);
 
 	// Update inode blocks (different block size!) count
 
-	uint64_t ino_blocks = ext4_inode_get_blocks_count(fs->superblock, inode_ref->inode);
+	uint64_t ino_blocks = ext4_inode_get_blocks_count(sb, inode_ref->inode);
 	ino_blocks += block_size / EXT4_INODE_BLOCK_SIZE;
-	ext4_inode_set_blocks_count(fs->superblock, inode_ref->inode, ino_blocks);
+	ext4_inode_set_blocks_count(sb, inode_ref->inode, ino_blocks);
 	inode_ref->dirty = true;
 
 	// Update block group free blocks count
 	uint32_t bg_free_blocks = ext4_block_group_get_free_blocks_count(
-			bg_ref->block_group, fs->superblock);
+			bg_ref->block_group, sb);
 	bg_free_blocks--;
-	ext4_block_group_set_free_blocks_count(bg_ref->block_group,
-			fs->superblock, bg_free_blocks);
+	ext4_block_group_set_free_blocks_count(bg_ref->block_group, sb, bg_free_blocks);
 	bg_ref->dirty = true;
 
