Index: uspace/lib/ext4/Makefile
===================================================================
--- uspace/lib/ext4/Makefile	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/Makefile	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,45 @@
+#
+# 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.
+#
+
+USPACE_PREFIX = ../..
+LIBRARY = libext4
+EXTRA_CFLAGS = -I$(LIBBLOCK_PREFIX)
+LIBS = $(LIBBLOCK_PREFIX)/libblock.a
+
+SOURCES = \
+	libext4_block_group.c \
+	libext4_directory.c \
+	libext4_directory_index.c \
+	libext4_extent.c \
+	libext4_filesystem.c \
+	libext4_hash.c \
+	libext4_inode.c \
+	libext4_superblock.c
+	
+
+include $(USPACE_PREFIX)/Makefile.common
Index: uspace/lib/ext4/libext4.h
===================================================================
--- uspace/lib/ext4/libext4.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,52 @@
+/*
+ * 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_H_
+#define LIBEXT4_LIBEXT4_H_
+
+#include "libext4_block_group.h"
+#include "libext4_directory.h"
+#include "libext4_directory_index.h"
+#include "libext4_extent.h"
+#include "libext4_filesystem.h"
+#include "libext4_hash.h"
+#include "libext4_inode.h"
+#include "libext4_superblock.h"
+
+#include <stdio.h>
+#define EXT4FS_DBG(format, ...) {if (true) printf("ext4fs: %s: " format "\n", __FUNCTION__, ##__VA_ARGS__);}
+
+#endif
+
+/**
+ * @}
+ */
Index: uspace/lib/ext4/libext4_block_group.c
===================================================================
--- uspace/lib/ext4/libext4_block_group.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_block_group.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,95 @@
+/*
+ * 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
+ * @{
+ */ 
+
+/**
+ * @file	libext4_block_group.c
+ * @brief	Ext4 block group structure operations
+ */
+
+#include <byteorder.h>
+#include "libext4.h"
+
+uint64_t ext4_block_group_get_block_bitmap(ext4_block_group_t *bg)
+{
+	return ((uint64_t)uint32_t_le2host(bg->block_bitmap_hi) << 32) |
+		uint32_t_le2host(bg->block_bitmap_lo);
+}
+
+uint64_t ext4_block_group_get_inode_bitmap(ext4_block_group_t *bg)
+{
+	return ((uint64_t)uint32_t_le2host(bg->inode_bitmap_hi) << 32) |
+		uint32_t_le2host(bg->inode_bitmap_lo);
+}
+
+uint64_t ext4_block_group_get_inode_table_first_block(ext4_block_group_t *bg)
+{
+	return ((uint64_t)uint32_t_le2host(bg->inode_table_first_block_hi) << 32) |
+		uint32_t_le2host(bg->inode_table_first_block_lo);
+}
+
+uint32_t ext4_block_group_get_free_blocks_count(ext4_block_group_t *bg)
+{
+	return ((uint32_t)uint16_t_le2host(bg->free_blocks_count_hi) << 16) |
+		uint16_t_le2host(bg->free_blocks_count_lo);
+}
+
+uint32_t ext4_block_group_get_free_inodes_count(ext4_block_group_t *bg)
+{
+	return ((uint32_t)uint16_t_le2host(bg->free_inodes_count_hi) << 16) |
+		uint16_t_le2host(bg->free_inodes_count_lo);
+}
+
+uint32_t ext4_block_group_get_used_dirs_count(ext4_block_group_t *bg)
+{
+	return ((uint32_t)uint16_t_le2host(bg->used_dirs_count_hi) << 16) |
+		uint16_t_le2host(bg->used_dirs_count_lo);
+}
+
+uint16_t ext4_block_group_get_flags(ext4_block_group_t *bg)
+{
+	return uint16_t_le2host(bg->flags);
+}
+
+uint32_t ext4_block_group_get_itable_unused(ext4_block_group_t *bg)
+{
+	return ((uint32_t)uint16_t_le2host(bg->itable_unused_hi) << 16) |
+		uint16_t_le2host(bg->itable_unused_lo);
+}
+
+uint16_t ext4_block_group_get_checksum(ext4_block_group_t *bg)
+{
+	return uint16_t_le2host(bg->checksum);
+}
+
+/**
+ * @}
+ */ 
Index: uspace/lib/ext4/libext4_block_group.h
===================================================================
--- uspace/lib/ext4/libext4_block_group.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_block_group.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,85 @@
+/*
+ * 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_BLOCK_GROUP_H_
+#define LIBEXT4_LIBEXT4_BLOCK_GROUP_H_
+
+#include <libblock.h>
+#include <sys/types.h>
+#include "libext4_block_group.h"
+/*
+ * Structure of a blocks group descriptor
+ */
+typedef struct ext4_block_group {
+	uint32_t block_bitmap_lo; // Blocks bitmap block
+	uint32_t inode_bitmap_lo; // Inodes bitmap block
+	uint32_t inode_table_first_block_lo; // Inodes table block
+	uint16_t free_blocks_count_lo; // Free blocks count
+	uint16_t free_inodes_count_lo; // Free inodes count
+	uint16_t used_dirs_count_lo; // Directories count
+	uint16_t flags; // EXT4_BG_flags (INODE_UNINIT, etc)
+	uint32_t reserved[2]; // Likely block/inode bitmap checksum
+	uint16_t itable_unused_lo; // Unused inodes count
+	uint16_t checksum; // crc16(sb_uuid+group+desc)
+	/* -------------- */
+	uint32_t block_bitmap_hi; // Blocks bitmap block MSB
+	uint32_t inode_bitmap_hi; // Inodes bitmap block MSB
+	uint32_t inode_table_first_block_hi; // Inodes table block MSB
+	uint16_t free_blocks_count_hi; // Free blocks count MSB
+	uint16_t free_inodes_count_hi; // Free inodes count MSB
+	uint16_t used_dirs_count_hi; // Directories count MSB
+	uint16_t itable_unused_hi;  // Unused inodes count MSB
+	uint32_t reserved2[3]; // Padding
+} ext4_block_group_t;
+
+typedef struct ext4_block_group_ref {
+	block_t *block; // Reference to a block containing this block group descr
+	ext4_block_group_t *block_group;
+} ext4_block_group_ref_t;
+
+#define EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE 32
+
+extern uint64_t ext4_block_group_get_block_bitmap(ext4_block_group_t *);
+extern uint64_t ext4_block_group_get_inode_bitmap(ext4_block_group_t *);
+extern uint64_t ext4_block_group_get_inode_table_first_block(ext4_block_group_t *);
+extern uint32_t ext4_block_group_get_free_blocks_count(ext4_block_group_t *);
+extern uint32_t ext4_block_group_get_free_inodes_count(ext4_block_group_t *);
+extern uint32_t ext4_block_group_get_used_dirs_count(ext4_block_group_t *);
+extern uint16_t ext4_block_group_get_flags(ext4_block_group_t *);
+extern uint32_t ext4_block_group_get_itable_unused(ext4_block_group_t *);
+extern uint16_t ext4_block_group_get_checksum(ext4_block_group_t *);
+
+#endif
+
+/**
+ * @}
+ */
Index: uspace/lib/ext4/libext4_directory.c
===================================================================
--- uspace/lib/ext4/libext4_directory.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_directory.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,252 @@
+/*
+ * 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
+ * @{
+ */ 
+
+/**
+ * @file	libext4_directory.c
+ * @brief	Ext4 directory structure operations.
+ */
+
+#include <byteorder.h>
+#include <errno.h>
+#include "libext4.h"
+
+static int ext4_directory_iterator_set(ext4_directory_iterator_t *,
+    uint32_t);
+
+
+uint32_t ext4_directory_entry_ll_get_inode(ext4_directory_entry_ll_t *de)
+{
+	return uint32_t_le2host(de->inode);
+}
+
+uint16_t ext4_directory_entry_ll_get_entry_length(
+    ext4_directory_entry_ll_t *de)
+{
+	return uint16_t_le2host(de->entry_length);
+}
+
+uint16_t ext4_directory_entry_ll_get_name_length(
+    ext4_superblock_t *sb, ext4_directory_entry_ll_t *de)
+{
+	if (ext4_superblock_get_rev_level(sb) == 0 &&
+	    ext4_superblock_get_minor_rev_level(sb) < 5) {
+		return ((uint16_t)de->name_length_high) << 8 |
+		    ((uint16_t)de->name_length);
+	}
+	return de->name_length;
+}
+
+uint8_t ext4_directory_dx_root_info_get_hash_version(ext4_directory_dx_root_info_t *root_info)
+{
+	return root_info->hash_version;
+}
+
+uint8_t ext4_directory_dx_root_info_get_info_length(ext4_directory_dx_root_info_t *root_info)
+{
+	return root_info->info_length;
+}
+
+uint8_t ext4_directory_dx_root_info_get_indirect_levels(ext4_directory_dx_root_info_t *root_info)
+{
+	return root_info->indirect_levels;
+}
+
+uint16_t ext4_directory_dx_countlimit_get_limit(ext4_directory_dx_countlimit_t *countlimit)
+{
+	return uint16_t_le2host(countlimit->limit);
+}
+uint16_t ext4_directory_dx_countlimit_get_count(ext4_directory_dx_countlimit_t *countlimit)
+{
+	return uint16_t_le2host(countlimit->count);
+}
+
+uint32_t ext4_directory_dx_entry_get_hash(ext4_directory_dx_entry_t *entry)
+{
+	return uint32_t_le2host(entry->hash);
+}
+
+uint32_t ext4_directory_dx_entry_get_block(ext4_directory_dx_entry_t *entry)
+{
+	return uint32_t_le2host(entry->block);
+}
+
+
+
+int ext4_directory_iterator_init(ext4_directory_iterator_t *it,
+    ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, aoff64_t pos)
+{
+	it->inode_ref = inode_ref;
+	it->fs = fs;
+	it->current = NULL;
+	it->current_offset = 0;
+	it->current_block = NULL;
+
+	return ext4_directory_iterator_seek(it, pos);
+}
+
+
+int ext4_directory_iterator_next(ext4_directory_iterator_t *it)
+{
+	uint16_t skip;
+
+	assert(it->current != NULL);
+
+	skip = ext4_directory_entry_ll_get_entry_length(it->current);
+
+	return ext4_directory_iterator_seek(it, it->current_offset + skip);
+}
+
+
+int ext4_directory_iterator_seek(ext4_directory_iterator_t *it, aoff64_t pos)
+{
+	int rc;
+
+	uint64_t size;
+	aoff64_t current_block_idx;
+	aoff64_t next_block_idx;
+	uint32_t next_block_phys_idx;
+	uint32_t block_size;
+
+	size = ext4_inode_get_size(it->fs->superblock, it->inode_ref->inode);
+
+	/* The iterator is not valid until we seek to the desired position */
+	it->current = NULL;
+
+	/* Are we at the end? */
+	if (pos >= size) {
+		if (it->current_block) {
+			rc = block_put(it->current_block);
+			it->current_block = NULL;
+			if (rc != EOK) {
+				return rc;
+			}
+		}
+
+		it->current_offset = pos;
+		return EOK;
+	}
+
+	block_size = ext4_superblock_get_block_size(it->fs->superblock);
+	current_block_idx = it->current_offset / block_size;
+	next_block_idx = pos / block_size;
+
+	/* If we don't have a block or are moving accross block boundary,
+	 * we need to get another block
+	 */
+	if (it->current_block == NULL || current_block_idx != next_block_idx) {
+		if (it->current_block) {
+			rc = block_put(it->current_block);
+			it->current_block = NULL;
+			if (rc != EOK) {
+				return rc;
+			}
+		}
+
+		rc = ext4_filesystem_get_inode_data_block_index(it->fs,
+		    it->inode_ref->inode, next_block_idx, &next_block_phys_idx);
+		if (rc != EOK) {
+			return rc;
+		}
+
+		rc = block_get(&it->current_block, it->fs->device, next_block_phys_idx,
+		    BLOCK_FLAGS_NONE);
+		if (rc != EOK) {
+			it->current_block = NULL;
+			return rc;
+		}
+	}
+
+	it->current_offset = pos;
+
+	return ext4_directory_iterator_set(it, block_size);
+}
+
+static int ext4_directory_iterator_set(ext4_directory_iterator_t *it,
+    uint32_t block_size)
+{
+	uint32_t offset_in_block = it->current_offset % block_size;
+
+	it->current = NULL;
+
+	/* Ensure proper alignment */
+	if ((offset_in_block % 4) != 0) {
+		return EIO;
+	}
+
+	/* Ensure that the core of the entry does not overflow the block */
+	if (offset_in_block > block_size - 8) {
+		return EIO;
+	}
+
+	ext4_directory_entry_ll_t *entry = it->current_block->data + offset_in_block;
+
+	/* Ensure that the whole entry does not overflow the block */
+	uint16_t length = ext4_directory_entry_ll_get_entry_length(entry);
+	if (offset_in_block + length > block_size) {
+		return EIO;
+	}
+
+	/* Ensure the name length is not too large */
+	if (ext4_directory_entry_ll_get_name_length(it->fs->superblock,
+	    entry) > length-8) {
+		return EIO;
+	}
+
+	it->current = entry;
+	return EOK;
+}
+
+
+int ext4_directory_iterator_fini(ext4_directory_iterator_t *it)
+{
+	int rc;
+
+	it->fs = NULL;
+	it->inode_ref = NULL;
+	it->current = NULL;
+
+	if (it->current_block) {
+		rc = block_put(it->current_block);
+		if (rc != EOK) {
+			return rc;
+		}
+	}
+
+	return EOK;
+}
+
+
+
+
+/**
+ * @}
+ */ 
Index: uspace/lib/ext4/libext4_directory.h
===================================================================
--- uspace/lib/ext4/libext4_directory.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_directory.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,80 @@
+/*
+ * 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_DIRECTORY_H_
+#define LIBEXT4_LIBEXT4_DIRECTORY_H_
+
+#include "libext4_filesystem.h"
+#include "libext4_inode.h"
+
+#define EXT4_DIRECTORY_FILENAME_LEN	255
+
+/**
+ * Linked list directory entry structure
+ */
+typedef struct ext4_directory_entry_ll {
+	uint32_t inode; // Inode for the entry
+	uint16_t entry_length; // Distance to the next directory entry
+	uint8_t name_length; // Lower 8 bits of name length
+	union {
+		uint8_t name_length_high; // Higher 8 bits of name length
+		uint8_t inode_type; // Type of referenced inode (in rev >= 0.5)
+	} __attribute__ ((packed));
+	uint8_t name[EXT4_DIRECTORY_FILENAME_LEN]; // Entry name
+} __attribute__ ((packed)) ext4_directory_entry_ll_t;
+
+typedef struct ext4_directory_iterator {
+	ext4_filesystem_t *fs;
+	ext4_inode_ref_t *inode_ref;
+	block_t *current_block;
+	aoff64_t current_offset;
+	ext4_directory_entry_ll_t *current;
+} ext4_directory_iterator_t;
+
+
+extern uint32_t	ext4_directory_entry_ll_get_inode(ext4_directory_entry_ll_t *);
+extern uint16_t	ext4_directory_entry_ll_get_entry_length(
+    ext4_directory_entry_ll_t *);
+extern uint16_t	ext4_directory_entry_ll_get_name_length(
+    ext4_superblock_t *, ext4_directory_entry_ll_t *);
+
+extern int ext4_directory_iterator_init(ext4_directory_iterator_t *,
+		ext4_filesystem_t *, ext4_inode_ref_t *, aoff64_t);
+extern int ext4_directory_iterator_next(ext4_directory_iterator_t *);
+extern int ext4_directory_iterator_seek(ext4_directory_iterator_t *, aoff64_t pos);
+extern int ext4_directory_iterator_fini(ext4_directory_iterator_t *);
+
+#endif
+
+/**
+ * @}
+ */
Index: uspace/lib/ext4/libext4_directory_index.c
===================================================================
--- uspace/lib/ext4/libext4_directory_index.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_directory_index.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,374 @@
+/*
+ * 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
+ * @{
+ */ 
+
+/**
+ * @file	libext4_directory_index.c
+ * @brief	Ext4 directory index operations.
+ */
+
+#include <errno.h>
+#include "libext4.h"
+
+static int ext4_directory_hinfo_init(ext4_hash_info_t *hinfo, block_t *root_block,
+		ext4_superblock_t *sb, size_t name_len, const char *name)
+{
+	uint32_t block_size, entry_space;
+	uint16_t limit;
+	ext4_directory_dx_root_t *root;
+
+	root = (ext4_directory_dx_root_t *)root_block->data;
+
+	if (root->info.hash_version != EXT4_HASH_VERSION_TEA &&
+			root->info.hash_version != EXT4_HASH_VERSION_HALF_MD4 &&
+			root->info.hash_version != EXT4_HASH_VERSION_LEGACY) {
+		return EXT4_ERR_BAD_DX_DIR;
+	}
+
+	// Check unused flags
+	if (root->info.unused_flags != 0) {
+		EXT4FS_DBG("ERR: unused_flags = \%u", root->info.unused_flags);
+		return EXT4_ERR_BAD_DX_DIR;
+	}
+
+	// Check indirect levels
+	if (root->info.indirect_levels > 1) {
+		EXT4FS_DBG("ERR: indirect_levels = \%u", root->info.indirect_levels);
+		return EXT4_ERR_BAD_DX_DIR;
+	}
+
+	block_size = ext4_superblock_get_block_size(sb);
+
+	entry_space = block_size;
+	entry_space -= 2 * sizeof(ext4_directory_dx_dot_entry_t);
+	entry_space -= sizeof(ext4_directory_dx_root_info_t);
+    entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
+
+    limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)&root->entries);
+    if (limit != entry_space) {
+    	return EXT4_ERR_BAD_DX_DIR;
+	}
+
+	hinfo->hash_version = ext4_directory_dx_root_info_get_hash_version(&root->info);
+	if ((hinfo->hash_version <= EXT4_HASH_VERSION_TEA)
+			&& (ext4_superblock_has_flag(sb, EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH))) {
+		// 3 is magic from ext4 linux implementation
+		hinfo->hash_version += 3;
+	}
+
+	hinfo->seed = ext4_superblock_get_hash_seed(sb);
+
+	if (name) {
+		ext4_hash_string(hinfo, name_len, name);
+	}
+
+	return EOK;
+}
+
+static int ext4_directory_dx_get_leaf(ext4_hash_info_t *hinfo,
+		ext4_filesystem_t *fs, ext4_inode_t *inode, block_t *root_block,
+		ext4_directory_dx_block_t **dx_block, ext4_directory_dx_block_t *dx_blocks)
+{
+	int rc;
+	uint16_t count, limit, entry_space;
+	uint8_t indirect_level;
+	ext4_directory_dx_root_t *root;
+	ext4_directory_dx_entry_t *p, *q, *m, *at;
+	ext4_directory_dx_entry_t *entries;
+	block_t *tmp_block = root_block;
+	uint32_t fblock, next_block;
+	ext4_directory_dx_block_t *tmp_dx_block = dx_blocks;
+
+	root = (ext4_directory_dx_root_t *)root_block->data;
+	entries = (ext4_directory_dx_entry_t *)&root->entries;
+
+	limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
+	indirect_level = ext4_directory_dx_root_info_get_indirect_levels(&root->info);
+
+	while (true) {
+
+		count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)entries);
+		if ((count == 0) || (count > limit)) {
+			return EXT4_ERR_BAD_DX_DIR;
+		}
+
+		p = entries + 1;
+		q = entries + count - 1;
+
+		while (p <= q) {
+			m = p + (q - p) / 2;
+			if (ext4_directory_dx_entry_get_hash(m) > hinfo->hash) {
+				q = m - 1;
+			} else {
+				p = m + 1;
+			}
+		}
+
+		at = p - 1;
+
+		tmp_dx_block->block = tmp_block;
+		tmp_dx_block->entries = entries;
+		tmp_dx_block->position = at;
+
+        if (indirect_level == 0) {
+        	*dx_block = tmp_dx_block;
+        	return EOK;
+        }
+
+		next_block = ext4_directory_dx_entry_get_block(at);
+
+        indirect_level--;
+
+        rc = ext4_filesystem_get_inode_data_block_index(fs, inode, next_block, &fblock);
+        if (rc != EOK) {
+        	return rc;
+        }
+
+        rc = block_get(&tmp_block, fs->device, fblock, BLOCK_FLAGS_NONE);
+        if (rc != EOK) {
+        	return rc;
+        }
+
+		entries = ((ext4_directory_dx_node_t *) tmp_block->data)->entries;
+		limit = ext4_directory_dx_countlimit_get_limit((ext4_directory_dx_countlimit_t *)entries);
+
+        entry_space = ext4_superblock_get_block_size(fs->superblock) - sizeof(ext4_directory_dx_dot_entry_t);
+        entry_space = entry_space / sizeof(ext4_directory_dx_entry_t);
+
+
+		if (limit != entry_space) {
+			block_put(tmp_block);
+        	return EXT4_ERR_BAD_DX_DIR;
+		}
+
+		++tmp_dx_block;
+	}
+
+	// Unreachable
+	return EOK;
+}
+
+
+static int ext4_dirextory_dx_find_dir_entry(block_t *block,
+		ext4_superblock_t *sb, size_t name_len, const char *name,
+		ext4_directory_entry_ll_t **res_entry, aoff64_t *block_offset)
+{
+	ext4_directory_entry_ll_t *dentry;
+	uint16_t dentry_len;
+	uint8_t *addr_limit;
+	aoff64_t offset = 0;
+
+	dentry = (ext4_directory_entry_ll_t *)block->data;
+	addr_limit = block->data + ext4_superblock_get_block_size(sb);
+
+	while ((uint8_t *)dentry < addr_limit) {
+
+		if ((uint8_t*) dentry + name_len > addr_limit) {
+			break;
+		}
+
+		if (dentry->inode != 0) {
+			if (name_len == ext4_directory_entry_ll_get_name_length(sb, dentry)) {
+				// Compare names
+				if (bcmp((uint8_t *)name, dentry->name, name_len) == 0) {
+					*block_offset = offset;
+					*res_entry = dentry;
+					return 1;
+				}
+			}
+		}
+
+
+		// Goto next entry
+		dentry_len = ext4_directory_entry_ll_get_entry_length(dentry);
+
+        if (dentry_len == 0) {
+        	// TODO error
+        	return -1;
+        }
+
+		offset += dentry_len;
+		dentry = (ext4_directory_entry_ll_t *)((uint8_t *)dentry + dentry_len);
+	}
+
+	return 0;
+}
+
+static int ext4_directory_dx_next_block(ext4_filesystem_t *fs, ext4_inode_t *inode, uint32_t hash,
+		ext4_directory_dx_block_t *handle, ext4_directory_dx_block_t *handles)
+{
+	ext4_directory_dx_block_t *p;
+	uint16_t count;
+	uint32_t num_handles;
+	uint32_t current_hash;
+	block_t *block;
+	uint32_t block_addr, block_idx;
+    int rc;
+
+    num_handles = 0;
+    p = handle;
+
+    while (1) {
+
+    	p->position++;
+    	count = ext4_directory_dx_countlimit_get_count((ext4_directory_dx_countlimit_t *)p->entries);
+
+    	if (p->position < p->entries + count) {
+    		break;
+    	}
+
+    	if (p == handles) {
+    		return 0;
+    	}
+
+    	num_handles++;
+    	p--;
+    }
+
+    current_hash = ext4_directory_dx_entry_get_hash(p->position);
+
+    if ((hash & 1) == 0) {
+    	if ((current_hash & ~1) != hash) {
+    		return 0;
+    	}
+    }
+
+    while (num_handles--) {
+
+    	block_idx = ext4_directory_dx_entry_get_block(p->position);
+    	rc = ext4_filesystem_get_inode_data_block_index(fs, inode, block_idx, &block_addr);
+    	if (rc != EOK) {
+    		return rc;
+    	}
+
+    	rc = block_get(&block, fs->device, block_addr, BLOCK_FLAGS_NONE);
+    	if (rc != EOK) {
+    		return rc;
+    	}
+
+    	p++;
+
+    	block_put(p->block);
+        p->block = block;
+        p->entries = ((ext4_directory_dx_node_t *) block->data)->entries;
+        p->position = p->entries;
+    }
+
+    return 1;
+
+}
+
+int ext4_directory_dx_find_entry(ext4_directory_iterator_t *it,
+		ext4_filesystem_t *fs, ext4_inode_ref_t *inode_ref, size_t len, const char *name)
+{
+	int rc;
+	uint32_t root_block_addr, leaf_block_addr, leaf_block_idx;
+	aoff64_t block_offset;
+	block_t *root_block, *leaf_block;
+	ext4_hash_info_t hinfo;
+	ext4_directory_entry_ll_t *res_dentry;
+	ext4_directory_dx_block_t dx_blocks[2], *dx_block;
+
+	// get direct block 0 (index root)
+	rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, 0, &root_block_addr);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	rc = block_get(&root_block, fs->device, root_block_addr, BLOCK_FLAGS_NONE);
+	if (rc != EOK) {
+		it->current_block = NULL;
+		return rc;
+	}
+
+	rc = ext4_directory_hinfo_init(&hinfo, root_block, fs->superblock, len, name);
+	if (rc != EOK) {
+		block_put(root_block);
+		return EXT4_ERR_BAD_DX_DIR;
+	}
+
+	rc = ext4_directory_dx_get_leaf(&hinfo, fs, inode_ref->inode, root_block, &dx_block, dx_blocks);
+	if (rc != EOK) {
+		block_put(root_block);
+		return EXT4_ERR_BAD_DX_DIR;
+	}
+
+	do {
+
+		leaf_block_idx = ext4_directory_dx_entry_get_block(dx_block->position);
+
+    	rc = ext4_filesystem_get_inode_data_block_index(fs, inode_ref->inode, leaf_block_idx, &leaf_block_addr);
+    	if (rc != EOK) {
+    		return EXT4_ERR_BAD_DX_DIR;
+    	}
+
+		rc = block_get(&leaf_block, fs->device, leaf_block_addr, BLOCK_FLAGS_NONE);
+		if (rc != EOK) {
+			return EXT4_ERR_BAD_DX_DIR;
+		}
+
+		rc = ext4_dirextory_dx_find_dir_entry(leaf_block, fs->superblock, len, name,
+				&res_dentry, &block_offset);
+
+		// Found => return it
+		if (rc == 1) {
+			it->fs = fs;
+			it->inode_ref = inode_ref;
+			it->current_block = leaf_block;
+			it->current_offset = block_offset;
+			it->current = res_dentry;
+			return EOK;
+		}
+
+		block_put(leaf_block);
+
+		// ERROR - corrupted index
+		if (rc == -1) {
+			// TODO cleanup
+			return EXT4_ERR_BAD_DX_DIR;
+		}
+
+		rc = ext4_directory_dx_next_block(fs, inode_ref->inode, hinfo.hash, dx_block, &dx_blocks[0]);
+		if (rc < 0) {
+			// TODO cleanup
+			return EXT4_ERR_BAD_DX_DIR;
+		}
+
+	} while (rc == 1);
+
+	return ENOENT;
+}
+
+
+
+/**
+ * @}
+ */ 
Index: uspace/lib/ext4/libext4_directory_index.h
===================================================================
--- uspace/lib/ext4/libext4_directory_index.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_directory_index.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,111 @@
+/*
+ * 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_DIRECTORY_INDEX_H_
+#define LIBEXT4_LIBEXT4_DIRECTORY_INDEX_H_
+
+/* Structures for indexed directory */
+
+typedef struct ext4_directory_dx_countlimit {
+	uint16_t limit;
+    uint16_t count;
+} ext4_directory_dx_countlimit_t;
+
+typedef struct ext4_directory_dx_dot_entry {
+	uint32_t inode;
+	uint16_t entry_length;
+    uint8_t name_length;
+    uint8_t inode_type;
+    uint8_t name[4];
+} ext4_directory_dx_dot_entry_t;
+
+typedef struct ext4_directory_dx_root_info {
+	uint32_t reserved_zero;
+	uint8_t hash_version;
+	uint8_t info_length;
+	uint8_t indirect_levels;
+	uint8_t unused_flags;
+} ext4_directory_dx_root_info_t;
+
+typedef struct ext4_directory_dx_entry {
+	uint32_t hash;
+	uint32_t block;
+} ext4_directory_dx_entry_t;
+
+typedef struct ext4_directory_dx_root {
+		ext4_directory_dx_dot_entry_t dots[2];
+		ext4_directory_dx_root_info_t info;
+		ext4_directory_dx_entry_t entries[0];
+} ext4_directory_dx_root_t;
+
+typedef struct ext4_directory_dx_node {
+	struct fake_directory_entry {
+		uint32_t inode;
+		uint16_t entry_length;
+	    uint8_t name_length;
+	    uint8_t inode_type;
+	} fake;
+	ext4_directory_dx_entry_t entries[0];
+} ext4_directory_dx_node_t;
+
+
+typedef struct ext4_directory_dx_block {
+	block_t *block;
+	ext4_directory_dx_entry_t *entries;
+	ext4_directory_dx_entry_t *position;
+} ext4_directory_dx_block_t;
+
+
+
+#define EXT4_ERR_BAD_DX_DIR			(-75000)
+#define EXT4_DIRECTORY_HTREE_EOF	(uint32_t)0x7fffffff
+
+
+extern uint8_t ext4_directory_dx_root_info_get_hash_version(ext4_directory_dx_root_info_t *);
+extern uint8_t ext4_directory_dx_root_info_get_info_length(ext4_directory_dx_root_info_t *);
+extern uint8_t ext4_directory_dx_root_info_get_indirect_levels(ext4_directory_dx_root_info_t *);
+
+extern uint16_t ext4_directory_dx_countlimit_get_limit(ext4_directory_dx_countlimit_t *);
+extern uint16_t ext4_directory_dx_countlimit_get_count(ext4_directory_dx_countlimit_t *);
+
+extern uint32_t ext4_directory_dx_entry_get_hash(ext4_directory_dx_entry_t *);
+extern uint32_t ext4_directory_dx_entry_get_block(ext4_directory_dx_entry_t *);
+
+extern int ext4_directory_dx_find_entry(ext4_directory_iterator_t *,
+		ext4_filesystem_t *, ext4_inode_ref_t *, size_t, const char *);
+
+
+#endif
+
+/**
+ * @}
+ */
Index: uspace/lib/ext4/libext4_extent.c
===================================================================
--- uspace/lib/ext4/libext4_extent.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_extent.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,96 @@
+/*
+ * 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
+ * @{
+ */ 
+
+/**
+ * @file	libext4_extent.c
+ * @brief	Ext4 extent structures operations.
+ */
+
+#include <byteorder.h>
+#include "libext4_extent.h"
+
+uint32_t ext4_extent_get_first_block(ext4_extent_t *extent)
+{
+	return uint32_t_le2host(extent->first_block);
+}
+
+uint16_t ext4_extent_get_block_count(ext4_extent_t *extent)
+{
+	return uint16_t_le2host(extent->block_count);
+}
+
+uint64_t ext4_extent_get_start(ext4_extent_t *extent)
+{
+	return ((uint64_t)uint16_t_le2host(extent->start_hi)) << 32 |
+			((uint64_t)uint32_t_le2host(extent->start_lo));
+
+}
+
+uint32_t ext4_extent_index_get_first_block(ext4_extent_index_t *index)
+{
+	return uint32_t_le2host(index->first_block);
+}
+
+uint64_t ext4_extent_index_get_leaf(ext4_extent_index_t *index)
+{
+	return ((uint64_t)uint16_t_le2host(index->leaf_hi)) << 32 |
+				((uint64_t)uint32_t_le2host(index->leaf_lo));
+}
+
+uint16_t ext4_extent_header_get_magic(ext4_extent_header_t *header)
+{
+	return uint16_t_le2host(header->magic);
+}
+
+uint16_t ext4_extent_header_get_entries_count(ext4_extent_header_t *header)
+{
+	return uint16_t_le2host(header->entries_count);
+}
+
+uint16_t ext4_extent_header_get_max_entries_count(ext4_extent_header_t *header)
+{
+	return uint16_t_le2host(header->max_entries_count);
+}
+
+uint16_t ext4_extent_header_get_depth(ext4_extent_header_t *header)
+{
+	return uint16_t_le2host(header->depth);
+}
+
+uint32_t ext4_extent_header_get_generation(ext4_extent_header_t *header)
+{
+	return uint32_t_le2host(header->generation);
+}
+
+/**
+ * @}
+ */ 
Index: uspace/lib/ext4/libext4_extent.h
===================================================================
--- uspace/lib/ext4/libext4_extent.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_extent.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,93 @@
+/*
+ * 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_EXTENT_H_
+#define LIBEXT4_LIBEXT4_EXTENT_H_
+
+/*
+ * This is the extent on-disk structure.
+ * It's used at the bottom of the tree.
+ */
+typedef struct ext4_extent {
+	uint32_t first_block; // First logical block extent covers
+	uint16_t block_count; // Number of blocks covered by extent
+	uint16_t start_hi;    // High 16 bits of physical block
+	uint32_t start_lo;    // Low 32 bits of physical block
+} ext4_extent_t;
+
+/*
+ * This is index on-disk structure.
+ * It's used at all the levels except the bottom.
+ */
+typedef struct ext4_extent_index {
+	uint32_t first_block; // Index covers logical blocks from 'block'
+	uint32_t leaf_lo; /* Pointer to the physical block of the next
+	 	 	 	 	   * level. leaf or next index could be there */
+	uint16_t leaf_hi;     /* high 16 bits of physical block */
+	uint16_t padding;
+} ext4_extent_index_t;
+
+/*
+ * Each block (leaves and indexes), even inode-stored has header.
+ */
+typedef struct ext4_extent_header {
+	uint16_t magic;
+	uint16_t entries_count; // Number of valid entries
+	uint16_t max_entries_count; // Capacity of store in entries
+	uint16_t depth; // Has tree real underlying blocks?
+	uint32_t generation; // generation of the tree
+} ext4_extent_header_t;
+
+#define EXT4_EXTENT_MAGIC	0xF30A
+#define	EXT4_EXTENT_FIRST(header)	\
+		((ext4_extent_t *) (((void *) (header)) + sizeof(ext4_extent_header_t)))
+#define	EXT4_EXTENT_FIRST_INDEX(header)	\
+		((ext4_extent_index_t *) (((void *) (header)) + sizeof(ext4_extent_header_t)))
+
+extern uint32_t ext4_extent_get_first_block(ext4_extent_t *);
+extern uint16_t ext4_extent_get_block_count(ext4_extent_t *);
+extern uint64_t ext4_extent_get_start(ext4_extent_t *);
+
+extern uint32_t ext4_extent_index_get_first_block(ext4_extent_index_t *);
+extern uint64_t ext4_extent_index_get_leaf(ext4_extent_index_t *);
+
+extern uint16_t ext4_extent_header_get_magic(ext4_extent_header_t *);
+extern uint16_t ext4_extent_header_get_entries_count(ext4_extent_header_t *);
+extern uint16_t ext4_extent_header_get_max_entries_count(ext4_extent_header_t *);
+extern uint16_t ext4_extent_header_get_depth(ext4_extent_header_t *);
+extern uint32_t ext4_extent_header_get_generation(ext4_extent_header_t *);
+
+#endif
+
+/**
+ * @}
+ */
Index: uspace/lib/ext4/libext4_filesystem.c
===================================================================
--- uspace/lib/ext4/libext4_filesystem.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_filesystem.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,392 @@
+/*
+ * 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
+ * @{
+ */ 
+
+/**
+ * @file	libext4_filesystem.c
+ * @brief	TODO
+ */
+
+#include <byteorder.h>
+#include <errno.h>
+#include <malloc.h>
+#include "libext4.h"
+
+int ext4_filesystem_init(ext4_filesystem_t *fs, service_id_t service_id)
+{
+	int rc;
+	ext4_superblock_t *temp_superblock;
+	size_t block_size;
+	uint32_t block_ids_per_block;
+	int i;
+
+	fs->device = service_id;
+
+	// TODO what does constant 2048 mean?
+	rc = block_init(EXCHANGE_SERIALIZE, fs->device, 2048);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	/* Read superblock from device */
+	rc = ext4_superblock_read_direct(fs->device, &temp_superblock);
+	if (rc != EOK) {
+		block_fini(fs->device);
+		return rc;
+	}
+
+	/* Read block size from superblock and check */
+	block_size = ext4_superblock_get_block_size(temp_superblock);
+	if (block_size > EXT4_MAX_BLOCK_SIZE) {
+		block_fini(fs->device);
+		return ENOTSUP;
+	}
+
+	/* Initialize block caching */
+	rc = block_cache_init(service_id, block_size, 0, CACHE_MODE_WT);
+	if (rc != EOK) {
+		block_fini(fs->device);
+		return rc;
+	}
+
+	block_ids_per_block = block_size / sizeof(uint32_t);
+	fs->inode_block_limits[0] = EXT4_INODE_DIRECT_BLOCK_COUNT;
+	fs->inode_blocks_per_level[0] = 1;
+	for (i = 1; i < 4; i++) {
+		fs->inode_blocks_per_level[i]  = fs->inode_blocks_per_level[i-1] *
+		    block_ids_per_block;
+		fs->inode_block_limits[i] = fs->inode_block_limits[i-1] +
+				fs->inode_blocks_per_level[i];
+	}
+
+	/* Return loaded superblock */
+	fs->superblock = temp_superblock;
+
+	return EOK;
+}
+
+void ext4_filesystem_fini(ext4_filesystem_t *fs)
+{
+	free(fs->superblock);
+	block_fini(fs->device);
+}
+
+int ext4_filesystem_check_sanity(ext4_filesystem_t *fs)
+{
+	int rc;
+
+	rc = ext4_superblock_check_sanity(fs->superblock);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	return EOK;
+}
+
+int ext4_filesystem_check_features(ext4_filesystem_t *fs, bool *o_read_only)
+{
+	/* Feature flags are present in rev 1 and later */
+	if (ext4_superblock_get_rev_level(fs->superblock) == 0) {
+		*o_read_only = false;
+		return EOK;
+	}
+
+	uint32_t incompatible_features;
+	incompatible_features = ext4_superblock_get_features_incompatible(fs->superblock);
+	incompatible_features &= ~EXT4_FEATURE_INCOMPAT_SUPP;
+	if (incompatible_features > 0) {
+		*o_read_only = true;
+		return ENOTSUP;
+	}
+
+	uint32_t compatible_read_only;
+	compatible_read_only = ext4_superblock_get_features_read_only(fs->superblock);
+	compatible_read_only &= ~EXT4_FEATURE_RO_COMPAT_SUPP;
+	if (compatible_read_only > 0) {
+		*o_read_only = true;
+	}
+
+	return EOK;
+}
+
+int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *fs, uint32_t bgid,
+    ext4_block_group_ref_t **ref)
+{
+	int rc;
+	aoff64_t block_id;
+	uint32_t descriptors_per_block;
+	size_t offset;
+	ext4_block_group_ref_t *newref;
+
+	newref = malloc(sizeof(ext4_block_group_ref_t));
+	if (newref == NULL) {
+		return ENOMEM;
+	}
+
+	//EXT4FS_DBG("desc size = \%u", (uint32_t)ext4_superblock_get_desc_size(fs->superblock));
+
+	descriptors_per_block = ext4_superblock_get_block_size(fs->superblock)
+	    / ext4_superblock_get_desc_size(fs->superblock);
+
+	/* Block group descriptor table starts at the next block after superblock */
+	block_id = ext4_superblock_get_first_data_block(fs->superblock) + 1;
+
+	/* Find the block containing the descriptor we are looking for */
+	block_id += bgid / descriptors_per_block;
+	offset = (bgid % descriptors_per_block) * ext4_superblock_get_desc_size(fs->superblock);
+
+	rc = block_get(&newref->block, fs->device, block_id, 0);
+	if (rc != EOK) {
+		free(newref);
+		return rc;
+	}
+
+	newref->block_group = newref->block->data + offset;
+
+	*ref = newref;
+
+	return EOK;
+}
+
+int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *ref)
+{
+	int rc;
+
+	rc = block_put(ref->block);
+	free(ref);
+
+	return rc;
+}
+
+int ext4_filesystem_get_inode_ref(ext4_filesystem_t *fs, uint32_t index,
+    ext4_inode_ref_t **ref)
+{
+	int rc;
+	aoff64_t block_id;
+	uint32_t block_group;
+	uint32_t offset_in_group;
+	uint32_t byte_offset_in_group;
+	size_t offset_in_block;
+	uint32_t inodes_per_group;
+	uint32_t inode_table_start;
+	uint16_t inode_size;
+	uint32_t block_size;
+	ext4_block_group_ref_t *bg_ref;
+	ext4_inode_ref_t *newref;
+
+	newref = malloc(sizeof(ext4_inode_ref_t));
+	if (newref == NULL) {
+		return ENOMEM;
+	}
+
+	inodes_per_group = ext4_superblock_get_inodes_per_group(fs->superblock);
+
+	/* inode numbers are 1-based, but it is simpler to work with 0-based
+	 * when computing indices
+	 */
+	index -= 1;
+	block_group = index / inodes_per_group;
+	offset_in_group = index % inodes_per_group;
+
+	rc = ext4_filesystem_get_block_group_ref(fs, block_group, &bg_ref);
+	if (rc != EOK) {
+		free(newref);
+		return rc;
+	}
+
+	inode_table_start = ext4_block_group_get_inode_table_first_block(
+	    bg_ref->block_group);
+
+	rc = ext4_filesystem_put_block_group_ref(bg_ref);
+	if (rc != EOK) {
+		free(newref);
+		return rc;
+	}
+
+	inode_size = ext4_superblock_get_inode_size(fs->superblock);
+	block_size = ext4_superblock_get_block_size(fs->superblock);
+
+	byte_offset_in_group = offset_in_group * inode_size;
+
+	block_id = inode_table_start + (byte_offset_in_group / block_size);
+	offset_in_block = byte_offset_in_group % block_size;
+
+	rc = block_get(&newref->block, fs->device, block_id, 0);
+	if (rc != EOK) {
+		free(newref);
+		return rc;
+	}
+
+	newref->inode = newref->block->data + offset_in_block;
+	/* we decremented index above, but need to store the original value
+	 * in the reference
+	 */
+	newref->index = index+1;
+
+	*ref = newref;
+
+	return EOK;
+}
+
+
+int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *ref)
+{
+	int rc;
+
+	rc = block_put(ref->block);
+	free(ref);
+
+	return rc;
+}
+
+int ext4_filesystem_get_inode_data_block_index(ext4_filesystem_t *fs, ext4_inode_t* inode,
+    aoff64_t iblock, uint32_t* fblock)
+{
+	int rc;
+	uint32_t offset_in_block;
+	uint32_t current_block;
+	aoff64_t block_offset_in_level;
+	int i;
+	int level;
+	block_t *block;
+
+	/* Handle inode using extents */
+	if (ext4_superblock_has_feature_compatible(fs->superblock, EXT4_FEATURE_INCOMPAT_EXTENTS) &&
+			ext4_inode_has_flag(inode, EXT4_INODE_FLAG_EXTENTS)) {
+		current_block = ext4_inode_get_extent_block(inode, iblock, fs->device);
+		*fblock = current_block;
+		return EOK;
+
+	}
+
+	/* Handle simple case when we are dealing with direct reference */
+	if (iblock < EXT4_INODE_DIRECT_BLOCK_COUNT) {
+		current_block = ext4_inode_get_direct_block(inode, (uint32_t)iblock);
+		*fblock = current_block;
+		return EOK;
+	}
+
+	/* Determine the indirection level needed to get the desired block */
+	level = -1;
+	for (i = 1; i < 4; i++) {
+		if (iblock < fs->inode_block_limits[i]) {
+			level = i;
+			break;
+		}
+	}
+
+	if (level == -1) {
+		return EIO;
+	}
+
+	/* Compute offsets for the topmost level */
+	block_offset_in_level = iblock - fs->inode_block_limits[level-1];
+	current_block = ext4_inode_get_indirect_block(inode, level-1);
+	offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
+
+	/* Navigate through other levels, until we find the block number
+	 * or find null reference meaning we are dealing with sparse file
+	 */
+	while (level > 0) {
+		rc = block_get(&block, fs->device, current_block, 0);
+		if (rc != EOK) {
+			return rc;
+		}
+
+		current_block = uint32_t_le2host(((uint32_t*)block->data)[offset_in_block]);
+
+		rc = block_put(block);
+		if (rc != EOK) {
+			return rc;
+		}
+
+		if (current_block == 0) {
+			/* This is a sparse file */
+			*fblock = 0;
+			return EOK;
+		}
+
+		level -= 1;
+
+		/* If we are on the last level, break here as
+		 * there is no next level to visit
+		 */
+		if (level == 0) {
+			break;
+		}
+
+		/* Visit the next level */
+		block_offset_in_level %= fs->inode_blocks_per_level[level];
+		offset_in_block = block_offset_in_level / fs->inode_blocks_per_level[level-1];
+	}
+
+	*fblock = current_block;
+
+	return EOK;
+}
+
+
+int ext4_filesystem_release_inode_block(ext4_filesystem_t *fs, ext4_inode_t *inode, uint32_t iblock)
+{
+	int rc;
+	uint32_t fblock;
+
+	// TODO handle with extents
+
+	rc = ext4_filesystem_get_inode_data_block_index(fs, inode, iblock, &fblock);
+	if (rc != EOK) {
+		// TODO error
+		return rc;
+	}
+
+	// Sparse file
+	if (fblock == 0) {
+
+		//
+		return EOK;
+	}
+
+
+	// TODO vyhledat policko s ukazatelem a nastavit nulu
+
+
+	// TODO uvolnit blok v bitmape
+
+	// TODO return
+
+
+	return EOK;
+}
+
+
+/**
+ * @}
+ */ 
Index: uspace/lib/ext4/libext4_filesystem.h
===================================================================
--- uspace/lib/ext4/libext4_filesystem.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_filesystem.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,70 @@
+/*
+ * 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_FILESYSTEM_H_
+#define LIBEXT4_LIBEXT4_FILESYSTEM_H_
+
+#include <libblock.h>
+#include "libext4_block_group.h"
+#include "libext4_inode.h"
+#include "libext4_superblock.h"
+
+typedef struct ext4_filesystem {
+	service_id_t device;
+	ext4_superblock_t *	superblock;
+	aoff64_t inode_block_limits[4];
+	aoff64_t inode_blocks_per_level[4];
+} ext4_filesystem_t;
+
+#define EXT4_MAX_BLOCK_SIZE 	65536 //64 KiB
+#define EXT4_REV0_INODE_SIZE	128
+
+
+extern int ext4_filesystem_init(ext4_filesystem_t *, service_id_t);
+extern void ext4_filesystem_fini(ext4_filesystem_t *fs);
+extern int ext4_filesystem_check_sanity(ext4_filesystem_t *fs);
+extern int ext4_filesystem_check_features(ext4_filesystem_t *, bool *);
+extern int ext4_filesystem_get_block_group_ref(ext4_filesystem_t *, uint32_t,
+    ext4_block_group_ref_t **);
+extern int ext4_filesystem_put_block_group_ref(ext4_block_group_ref_t *);
+extern int ext4_filesystem_get_inode_ref(ext4_filesystem_t *, uint32_t,
+		ext4_inode_ref_t **);
+extern int ext4_filesystem_put_inode_ref(ext4_inode_ref_t *);
+extern int ext4_filesystem_get_inode_data_block_index(ext4_filesystem_t *,
+	ext4_inode_t *, aoff64_t iblock, uint32_t *);
+extern int ext4_filesystem_release_inode_block(ext4_filesystem_t *,
+		ext4_inode_t *, uint32_t);
+#endif
+
+/**
+ * @}
+ */
Index: uspace/lib/ext4/libext4_hash.c
===================================================================
--- uspace/lib/ext4/libext4_hash.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_hash.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,315 @@
+/*
+ * 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
+ * @{
+ */ 
+
+/**
+ * @file	libext4_hash.c
+ * @brief	Hashing algorithms for ext4 HTree.
+ */
+
+#include <errno.h>
+#include <mem.h>
+
+#include "libext4.h"
+
+#define TEA_DELTA 0x9E3779B9
+
+
+/* F, G and H are basic MD4 functions: selection, majority, parity */
+#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
+#define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))
+#define H(x, y, z) ((x) ^ (y) ^ (z))
+
+
+/*
+ * The generic round function.  The application is so specific that
+ * we don't bother protecting all the arguments with parens, as is generally
+ * good macro practice, in favor of extra legibility.
+ * Rotation is separate from addition to prevent recomputation
+ */
+#define ROUND(f, a, b, c, d, x, s)      \
+        (a += f(b, c, d) + x, a = (a << s) | (a >> (32 - s)))
+#define K1 0
+#define K2 013240474631UL
+#define K3 015666365641UL
+
+
+static void tea_transform(uint32_t buf[4], uint32_t const in[])
+{
+	uint32_t sum = 0;
+	uint32_t b0 = buf[0], b1 = buf[1];
+	uint32_t a = in[0], b = in[1], c = in[2], d = in[3];
+	int n = 16;
+
+	do {
+		sum += TEA_DELTA;
+		b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
+		b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
+	} while (--n);
+
+	buf[0] += b0;
+	buf[1] += b1;
+}
+
+
+static void half_md4_transform(uint32_t buf[4], const uint32_t in[8])
+{
+	uint32_t a = buf[0], b = buf[1], c = buf[2], d = buf[3];
+
+	/* Round 1 */
+	ROUND(F, a, b, c, d, in[0] + K1,  3);
+	ROUND(F, d, a, b, c, in[1] + K1,  7);
+	ROUND(F, c, d, a, b, in[2] + K1, 11);
+	ROUND(F, b, c, d, a, in[3] + K1, 19);
+	ROUND(F, a, b, c, d, in[4] + K1,  3);
+	ROUND(F, d, a, b, c, in[5] + K1,  7);
+	ROUND(F, c, d, a, b, in[6] + K1, 11);
+	ROUND(F, b, c, d, a, in[7] + K1, 19);
+
+	/* Round 2 */
+	ROUND(G, a, b, c, d, in[1] + K2,  3);
+	ROUND(G, d, a, b, c, in[3] + K2,  5);
+	ROUND(G, c, d, a, b, in[5] + K2,  9);
+	ROUND(G, b, c, d, a, in[7] + K2, 13);
+	ROUND(G, a, b, c, d, in[0] + K2,  3);
+	ROUND(G, d, a, b, c, in[2] + K2,  5);
+	ROUND(G, c, d, a, b, in[4] + K2,  9);
+	ROUND(G, b, c, d, a, in[6] + K2, 13);
+
+	/* Round 3 */
+	ROUND(H, a, b, c, d, in[3] + K3,  3);
+	ROUND(H, d, a, b, c, in[7] + K3,  9);
+	ROUND(H, c, d, a, b, in[2] + K3, 11);
+	ROUND(H, b, c, d, a, in[6] + K3, 15);
+	ROUND(H, a, b, c, d, in[1] + K3,  3);
+	ROUND(H, d, a, b, c, in[5] + K3,  9);
+	ROUND(H, c, d, a, b, in[0] + K3, 11);
+	ROUND(H, b, c, d, a, in[4] + K3, 15);
+
+	buf[0] += a;
+	buf[1] += b;
+	buf[2] += c;
+	buf[3] += d;
+
+}
+
+static uint32_t hash_unsigned(const char *name, int len)
+{
+	uint32_t hash, hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
+	const unsigned char *ucp = (const unsigned char *) name;
+
+	while (len--) {
+		hash = hash1 + (hash0 ^ (((int) *ucp++) * 7152373));
+
+		if (hash & 0x80000000) {
+			hash -= 0x7fffffff;
+		}
+		hash1 = hash0;
+		hash0 = hash;
+	}
+	return hash0 << 1;
+}
+
+static uint32_t hash_signed(const char *name, int len)
+{
+	uint32_t hash, hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
+	const signed char *scp = (const signed char *) name;
+
+	while (len--) {
+		hash = hash1 + (hash0 ^ (((int) *scp++) * 7152373));
+
+		if (hash & 0x80000000) {
+			hash -= 0x7fffffff;
+		}
+		hash1 = hash0;
+		hash0 = hash;
+	}
+	return hash0 << 1;
+}
+
+static void str2hashbuf_signed(const char *msg, int len, uint32_t *buf, int num)
+{
+	uint32_t pad, val;
+	int i;
+	const signed char *scp = (const signed char *) msg;
+
+	pad = (uint32_t)len | ((uint32_t)len << 8);
+	pad |= pad << 16;
+
+	val = pad;
+	if (len > num*4) {
+		len = num * 4;
+	}
+
+	for (i = 0; i < len; i++) {
+		if ((i % 4) == 0) {
+			val = pad;
+		}
+		val = ((int) scp[i]) + (val << 8);
+		if ((i % 4) == 3) {
+			*buf++ = val;
+			val = pad;
+			num--;
+		}
+	}
+
+	if (--num >= 0) {
+		*buf++ = val;
+	}
+
+	while (--num >= 0) {
+		*buf++ = pad;
+	}
+}
+
+
+static void str2hashbuf_unsigned(const char *msg, int len, uint32_t *buf, int num)
+{
+	uint32_t pad, val;
+	int i;
+	const unsigned char *ucp = (const unsigned char *) msg;
+
+	pad = (uint32_t)len | ((uint32_t)len << 8);
+	pad |= pad << 16;
+
+	val = pad;
+	if (len > num*4) {
+        len = num * 4;
+	}
+
+	for (i = 0; i < len; i++) {
+		if ((i % 4) == 0) {
+			val = pad;
+		}
+		val = ((int) ucp[i]) + (val << 8);
+		if ((i % 4) == 3) {
+			*buf++ = val;
+			val = pad;
+			num--;
+		}
+	}
+
+	if (--num >= 0) {
+		*buf++ = val;
+	}
+
+	while (--num >= 0) {
+		*buf++ = pad;
+	}
+}
+
+int ext4_hash_string(ext4_hash_info_t *hinfo, int len, const char *name)
+{
+	uint32_t hash = 0;
+	uint32_t minor_hash = 0;
+	const char *p;
+    int i;
+    uint32_t in[8], buf[4];
+    void (*str2hashbuf)(const char *, int, uint32_t *, int) = str2hashbuf_signed;
+
+    /* Initialize the default seed for the hash checksum functions */
+	buf[0] = 0x67452301;
+	buf[1] = 0xefcdab89;
+	buf[2] = 0x98badcfe;
+	buf[3] = 0x10325476;
+
+    /* Check if the seed is all zero's */
+	if (hinfo->seed) {
+		for (i = 0; i < 4; i++) {
+			if (hinfo->seed[i] != 0) {
+            	break;
+			}
+		}
+		if (i < 4) {
+			memcpy(buf, hinfo->seed, sizeof(buf));
+		}
+    }
+
+	switch (hinfo->hash_version) {
+		case EXT4_HASH_VERSION_LEGACY_UNSIGNED:
+			hash = hash_unsigned(name, len);
+			break;
+
+		case EXT4_HASH_VERSION_LEGACY:
+			hash = hash_signed(name, len);
+			break;
+
+
+		case EXT4_HASH_VERSION_HALF_MD4_UNSIGNED:
+			str2hashbuf = str2hashbuf_unsigned;
+
+		case EXT4_HASH_VERSION_HALF_MD4:
+			p = name;
+			while (len > 0) {
+				(*str2hashbuf)(p, len, in, 8);
+				half_md4_transform(buf, in);
+				len -= 32;
+				p += 32;
+			}
+			minor_hash = buf[2];
+			hash = buf[1];
+			break;
+
+
+		case EXT4_HASH_VERSION_TEA_UNSIGNED:
+			str2hashbuf = str2hashbuf_unsigned;
+
+		case EXT4_HASH_VERSION_TEA:
+			p = name;
+			while (len > 0) {
+				(*str2hashbuf)(p, len, in, 4);
+				tea_transform(buf, in);
+				len -= 16;
+				p += 16;
+			}
+			hash = buf[0];
+			minor_hash = buf[1];
+			break;
+
+		default:
+			hinfo->hash = 0;
+			return EINVAL;
+	}
+
+	hash = hash & ~1;
+	if (hash == (EXT4_DIRECTORY_HTREE_EOF << 1)) {
+		hash = (EXT4_DIRECTORY_HTREE_EOF-1) << 1;
+	}
+
+	hinfo->hash = hash;
+	hinfo->minor_hash = minor_hash;
+
+	return EOK;
+}
+
+/**
+ * @}
+ */ 
Index: uspace/lib/ext4/libext4_hash.h
===================================================================
--- uspace/lib/ext4/libext4_hash.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_hash.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,58 @@
+/*
+ * 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_HASH_H_
+#define LIBEXT4_LIBEXT4_HASH_H_
+
+#include <sys/types.h>
+
+#define EXT4_HASH_VERSION_LEGACY			0
+#define EXT4_HASH_VERSION_HALF_MD4			1
+#define EXT4_HASH_VERSION_TEA				2
+#define EXT4_HASH_VERSION_LEGACY_UNSIGNED	3
+#define EXT4_HASH_VERSION_HALF_MD4_UNSIGNED	4
+#define EXT4_HASH_VERSION_TEA_UNSIGNED		5
+
+typedef struct ext4_hash_info {
+	uint32_t hash;
+	uint32_t minor_hash;
+	uint32_t hash_version;
+	uint32_t *seed;
+} ext4_hash_info_t;
+
+extern int ext4_hash_string(ext4_hash_info_t *, int, const char *);
+
+#endif
+
+/**
+ * @}
+ */
Index: uspace/lib/ext4/libext4_inode.c
===================================================================
--- uspace/lib/ext4/libext4_inode.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_inode.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,192 @@
+/*
+ * 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
+ * @{
+ */ 
+
+/**
+ * @file	libext4_inode.c
+ * @brief	Ext4 inode structure operations.
+ */
+
+#include <byteorder.h>
+#include <errno.h>
+#include <libblock.h>
+#include "libext4.h"
+
+uint32_t ext4_inode_get_mode(ext4_superblock_t *sb, ext4_inode_t *inode)
+{
+	if (ext4_superblock_get_creator_os(sb) == EXT4_SUPERBLOCK_OS_HURD) {
+		return ((uint32_t)uint16_t_le2host(inode->osd2.hurd2.mode_high)) << 16 |
+		    ((uint32_t)uint16_t_le2host(inode->mode));
+	}
+	return uint16_t_le2host(inode->mode);
+}
+
+bool ext4_inode_is_type(ext4_superblock_t *sb, ext4_inode_t *inode, uint32_t type)
+{
+	uint32_t mode = ext4_inode_get_mode(sb, inode);
+	return (mode & EXT4_INODE_MODE_TYPE_MASK) == type;
+}
+
+/*
+uint32_t ext4_inode_get_uid(ext4_inode_t *inode)
+*/
+
+uint64_t ext4_inode_get_size(ext4_superblock_t *sb, ext4_inode_t *inode)
+{
+	uint32_t major_rev = ext4_superblock_get_rev_level(sb);
+
+	if ((major_rev > 0) && ext4_inode_is_type(sb, inode, EXT4_INODE_MODE_FILE)) {
+		return ((uint64_t)uint32_t_le2host(inode->size_hi)) << 32 |
+			    ((uint64_t)uint32_t_le2host(inode->size_lo));
+		}
+	return uint32_t_le2host(inode->size_lo);
+}
+
+/*
+extern uint32_t ext4_inode_get_access_time(ext4_inode_t *);
+extern uint32_t ext4_inode_get_change_inode_time(ext4_inode_t *);
+extern uint32_t ext4_inode_get_modification_time(ext4_inode_t *);
+extern uint32_t ext4_inode_get_deletion_time(ext4_inode_t *);
+extern uint32_t ext4_inode_get_gid(ext4_inode_t *);
+*/
+
+uint16_t ext4_inode_get_links_count(ext4_inode_t *inode)
+{
+	return uint16_t_le2host(inode->links_count);
+}
+
+/*
+extern uint64_t ext4_inode_get_blocks_count(ext4_inode_t *);
+*/
+
+uint32_t ext4_inode_get_flags(ext4_inode_t *inode) {
+	return uint32_t_le2host(inode->flags);
+}
+
+uint32_t ext4_inode_get_direct_block(ext4_inode_t *inode, uint8_t idx)
+{
+	assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
+	return uint32_t_le2host(inode->blocks[idx]);
+}
+
+void ext4_inode_set_direct_block(ext4_inode_t *inode, uint8_t idx, uint32_t fblock)
+{
+	assert(idx < EXT4_INODE_DIRECT_BLOCK_COUNT);
+	inode->blocks[idx] = host2uint32_t_le(fblock);
+}
+
+uint32_t ext4_inode_get_indirect_block(ext4_inode_t *inode, uint8_t idx)
+{
+	assert(idx < EXT4_INODE_INDIRECT_BLOCK_COUNT);
+	return uint32_t_le2host(inode->blocks[idx + EXT4_INODE_INDIRECT_BLOCK]);
+}
+
+uint32_t ext4_inode_get_extent_block(ext4_inode_t *inode, uint64_t idx, service_id_t service_id)
+{
+	ext4_extent_header_t *header = ext4_inode_get_extent_header(inode);
+	ext4_extent_t *extent;
+	ext4_extent_index_t *extent_index;
+
+	uint32_t first_block;
+	uint16_t block_count;
+	uint64_t phys_block = 0;
+	uint64_t child;
+
+	int rc;
+	block_t* block = NULL;
+
+	while (ext4_extent_header_get_depth(header) != 0) {
+
+		extent_index = EXT4_EXTENT_FIRST_INDEX(header);
+
+		for (uint16_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i) {
+			if(idx >= ext4_extent_index_get_first_block(extent_index)) {
+
+				child = ext4_extent_index_get_leaf(extent_index);
+
+				if (block != NULL) {
+					block_put(block);
+				}
+
+				rc = block_get(&block, service_id, child, BLOCK_FLAGS_NONE);
+				if (rc != EOK) {
+					return 0;
+				}
+
+				header = (ext4_extent_header_t *)block->data;
+				break;
+			}
+		}
+	}
+
+	extent = EXT4_EXTENT_FIRST(header);
+
+	for (uint16_t i = 0; i < ext4_extent_header_get_entries_count(header); ++i) {
+
+		first_block = ext4_extent_get_first_block(extent);
+		block_count = ext4_extent_get_block_count(extent);
+
+		if ((idx >= first_block) && (idx < first_block + block_count)) {
+			phys_block = ext4_extent_get_start(extent) + idx;
+			phys_block -= ext4_extent_get_first_block(extent);
+
+			// Memory leak prevention
+			if (block != NULL) {
+				block_put(block);
+			}
+			return phys_block;
+		}
+		// Go to the next extent
+		++extent;
+	}
+
+
+	EXT4FS_DBG("ERROR - reached function end");
+	return phys_block;
+}
+
+ext4_extent_header_t * ext4_inode_get_extent_header(ext4_inode_t *inode)
+{
+	return (ext4_extent_header_t *)inode->blocks;
+}
+
+// Flags checker
+bool ext4_inode_has_flag(ext4_inode_t *inode, uint32_t flag)
+{
+	if (ext4_inode_get_flags(inode) & flag) {
+		return true;
+	}
+	return false;
+}
+
+/**
+ * @}
+ */ 
Index: uspace/lib/ext4/libext4_inode.h
===================================================================
--- uspace/lib/ext4/libext4_inode.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_inode.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,192 @@
+/*
+ * 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_INODE_H_
+#define LIBEXT4_LIBEXT4_INODE_H_
+
+#include <libblock.h>
+#include <sys/types.h>
+#include "libext4_extent.h"
+#include "libext4_superblock.h"
+
+
+#define EXT4_INODE_DIRECT_BLOCK_COUNT		12
+#define EXT4_INODE_INDIRECT_BLOCK 			EXT4_INODE_DIRECT_BLOCK_COUNT
+#define EXT4_INODE_DOUBLE_INDIRECT_BLOCK	(EXT4_INODE_INDIRECT_BLOCK + 1)
+#define EXT4_INODE_TRIPPLE_INDIRECT_BLOCK	(EXT4_INODE_DOUBLE_INDIRECT_BLOCK + 1)
+#define EXT4_INODE_BLOCKS					(EXT4_INODE_TRIPPLE_INDIRECT_BLOCK + 1)
+#define EXT4_INODE_INDIRECT_BLOCK_COUNT		(EXT4_INODE_BLOCKS - EXT4_INODE_DIRECT_BLOCK_COUNT)
+
+/*
+ * Structure of an inode on the disk
+ */
+typedef struct ext4_inode {
+	uint16_t mode; // File mode
+	uint16_t uid; // Low 16 bits of owner uid
+	uint32_t size_lo; // Size in bytes
+	uint32_t acess_time; // Access time
+	uint32_t change_inode_time; // Inode change time
+	uint32_t modification_time; // Modification time
+	uint32_t deletion_time; // Deletion time
+	uint16_t gid; // Low 16 bits of group id
+	uint16_t links_count; // Links count
+	uint32_t blocks_count_lo; // Blocks count
+	uint32_t flags; // File flags
+	uint32_t unused_osd1; // OS dependent - not used in HelenOS
+    uint32_t blocks[EXT4_INODE_BLOCKS]; // Pointers to blocks
+    uint32_t generation; // File version (for NFS)
+    uint32_t file_acl_lo; // File ACL
+    uint32_t size_hi;
+    uint32_t obso_faddr; // Obsoleted fragment address
+    union {
+    	struct {
+    		uint16_t blocks_high; /* were l_i_reserved1 */
+    		uint16_t file_acl_high;
+    		uint16_t uid_high;   /* these 2 fields */
+    		uint16_t gid_high;   /* were reserved2[0] */
+    		uint32_t reserved2;
+    	} linux2;
+    	struct {
+    		uint16_t reserved1;  /* Obsoleted fragment number/size which are removed in ext4 */
+    		uint16_t mode_high;
+    		uint16_t uid_high;
+    		uint16_t gid_high;
+    		uint32_t author;
+    	} hurd2;
+    	struct {
+    		uint16_t reserved1;  /* Obsoleted fragment number/size which are removed in ext4 */
+    		uint16_t file_acl_high;
+    		uint32_t reserved2[2];
+    	} masix2;
+    } __attribute__ ((packed)) osd2;
+
+    uint16_t extra_isize;
+    uint16_t pad1;
+    uint32_t ctime_extra; // Extra change time (nsec << 2 | epoch)
+    uint32_t mtime_extra; // Extra Modification time (nsec << 2 | epoch)
+    uint32_t atime_extra; // Extra Access time (nsec << 2 | epoch)
+    uint32_t crtime; // File creation time
+    uint32_t crtime_extra; // Extra file creation time (nsec << 2 | epoch)
+    uint32_t version_hi;   // High 32 bits for 64-bit version
+} __attribute__ ((packed)) ext4_inode_t;
+
+#define EXT4_INODE_MODE_FIFO		0x1000
+#define EXT4_INODE_MODE_CHARDEV		0x2000
+#define EXT4_INODE_MODE_DIRECTORY	0x4000
+#define EXT4_INODE_MODE_BLOCKDEV	0x6000
+#define EXT4_INODE_MODE_FILE		0x8000
+#define EXT4_INODE_MODE_SOFTLINK	0xA000
+#define EXT4_INODE_MODE_SOCKET		0xC000
+#define EXT4_INODE_MODE_TYPE_MASK	0xF000
+
+/*
+ * Inode flags
+ */
+#define EXT4_INODE_FLAG_SECRM		0x00000001 // Secure deletion
+#define EXT4_INODE_FLAG_UNRM		0x00000002 // Undelete
+#define EXT4_INODE_FLAG_COMPR		0x00000004 // Compress file
+#define EXT4_INODE_FLAG_SYNC		0x00000008 // Synchronous updates
+#define EXT4_INODE_FLAG_IMMUTABLE   0x00000010 // Immutable file
+#define EXT4_INODE_FLAG_APPEND		0x00000020 // writes to file may only append
+#define EXT4_INODE_FLAG_NODUMP		0x00000040 // do not dump file
+#define EXT4_INODE_FLAG_NOATIME		0x00000080 // do not update atime
+/* Compression flags */
+#define EXT4_INODE_FLAG_DIRTY		0x00000100
+#define EXT4_INODE_FLAG_COMPRBLK	0x00000200 // One or more compressed clusters
+#define EXT4_INODE_FLAG_NOCOMPR		0x00000400 // Don't compress
+#define EXT4_INODE_FLAG_ECOMPR		0x00000800 // Compression error
+/* End compression flags --- maybe not all used */
+#define EXT4_INODE_FLAG_INDEX		0x00001000 // hash-indexed directory
+#define EXT4_INODE_FLAG_IMAGIC		0x00002000 // AFS directory */
+#define EXT4_INODE_FLAG_JOURNAL_DATA	0x00004000 // File data should be journaled
+#define EXT4_INODE_FLAG_NOTAIL		0x00008000 // File tail should not be merged
+#define EXT4_INODE_FLAG_DIRSYNC		0x00010000 // Dirsync behaviour (directories only)
+#define EXT4_INODE_FLAG_TOPDIR		0x00020000 // Top of directory hierarchies
+#define EXT4_INODE_FLAG_HUGE_FILE	0x00040000 // Set to each huge file
+#define EXT4_INODE_FLAG_EXTENTS		0x00080000 // Inode uses extents
+#define EXT4_INODE_FLAG_EA_INODE	0x00200000 // Inode used for large EA
+#define EXT4_INODE_FLAG_EOFBLOCKS	0x00400000 // Blocks allocated beyond EOF
+#define EXT4_INODE_FLAG_RESERVED	0x80000000 // reserved for ext4 lib
+
+#define EXT4_INODE_ROOT_INDEX	2
+
+typedef struct ext4_inode_ref {
+	block_t *block; // Reference to a block containing this inode
+	ext4_inode_t *inode;
+	uint32_t index; // Index number of this inode
+} ext4_inode_ref_t;
+
+
+extern uint32_t ext4_inode_get_mode(ext4_superblock_t *, ext4_inode_t *);
+extern bool ext4_inode_is_type(ext4_superblock_t *, ext4_inode_t *, uint32_t);
+/*
+extern uint32_t ext4_inode_get_uid(ext4_inode_t *);
+*/
+extern uint64_t ext4_inode_get_size(ext4_superblock_t *, ext4_inode_t *);
+/*
+extern uint32_t ext4_inode_get_access_time(ext4_inode_t *);
+extern uint32_t ext4_inode_get_change_inode_time(ext4_inode_t *);
+extern uint32_t ext4_inode_get_modification_time(ext4_inode_t *);
+extern uint32_t ext4_inode_get_deletion_time(ext4_inode_t *);
+extern uint32_t ext4_inode_get_gid(ext4_inode_t *);
+*/
+extern uint16_t ext4_inode_get_links_count(ext4_inode_t *);
+/*
+extern uint64_t ext4_inode_get_blocks_count(ext4_inode_t *)
+*/
+extern uint32_t ext4_inode_get_flags(ext4_inode_t *);
+
+extern uint32_t ext4_inode_get_direct_block(ext4_inode_t *, uint8_t);
+extern void ext4_inode_set_direct_block(ext4_inode_t *, uint8_t, uint32_t);
+extern uint32_t ext4_inode_get_indirect_block(ext4_inode_t *, uint8_t);
+extern uint32_t ext4_inode_get_extent_block(ext4_inode_t *, uint64_t, service_id_t);
+/*
+uint32_t blocks[EXT4_INODE_BLOCKS]; // Pointers to blocks
+uint32_t generation;
+uint32_t file_acl_lo; // File ACL
+uint16_t extra_isize;
+uint32_t ctime_extra; // Extra change time (nsec << 2 | epoch)
+uint32_t mtime_extra; // Extra Modification time (nsec << 2 | epoch)
+uint32_t atime_extra; // Extra Access time (nsec << 2 | epoch)
+uint32_t crtime; // File creation time
+uint32_t crtime_extra; // Extra file creation time (nsec << 2 | epoch)
+uint32_t version_hi;   // High 32 bits for 64-bit version
+*/
+
+extern ext4_extent_header_t * ext4_inode_get_extent_header(ext4_inode_t *);
+extern bool ext4_inode_has_flag(ext4_inode_t *, uint32_t);
+
+#endif
+
+/**
+ * @}
+ */
Index: uspace/lib/ext4/libext4_superblock.c
===================================================================
--- uspace/lib/ext4/libext4_superblock.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_superblock.c	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,284 @@
+/*
+ * 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
+ * @{
+ */ 
+
+/**
+ * @file	libext4_superblock.c
+ * @brief	Ext4 superblock operations.
+ */
+
+#include <byteorder.h>
+#include <errno.h>
+#include <libblock.h>
+#include <malloc.h>
+#include "libext4.h"
+
+uint32_t ext4_superblock_get_inodes_count(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->inodes_count);
+}
+
+uint64_t ext4_superblock_get_blocks_count(ext4_superblock_t *sb)
+{
+	return ((uint64_t)uint32_t_le2host(sb->blocks_count_hi) << 32) |
+			uint32_t_le2host(sb->blocks_count_lo);
+}
+
+uint64_t ext4_superblock_get_reserved_blocks_count(ext4_superblock_t *sb)
+{
+	return ((uint64_t)uint32_t_le2host(sb->reserved_blocks_count_hi) << 32) |
+			uint32_t_le2host(sb->reserved_blocks_count_lo);
+}
+
+uint64_t ext4_superblock_get_free_blocks_count(ext4_superblock_t *sb)
+{
+	return ((uint64_t)uint32_t_le2host(sb->free_blocks_count_hi) << 32) |
+			uint32_t_le2host(sb->free_blocks_count_lo);
+}
+
+uint32_t ext4_superblock_get_free_inodes_count(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->free_inodes_count);
+}
+
+uint32_t ext4_superblock_get_first_data_block(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->first_data_block);
+}
+
+uint32_t ext4_superblock_get_log_block_size(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->log_block_size);
+}
+
+uint32_t ext4_superblock_get_block_size(ext4_superblock_t *sb)
+{
+	return 1024 << ext4_superblock_get_log_block_size(sb);
+}
+
+
+uint32_t ext4_superblock_get_blocks_per_group(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->blocks_per_group);
+}
+
+uint32_t ext4_superblock_get_inodes_per_group(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->inodes_per_group);
+}
+
+uint32_t ext4_superblock_get_mount_time(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->mount_time);
+}
+
+uint32_t ext4_superblock_get_write_time(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->write_time);
+}
+
+uint16_t ext4_superblock_get_mount_count(ext4_superblock_t *sb)
+{
+	return uint16_t_le2host(sb->mount_count);
+}
+
+uint16_t ext4_superblock_get_max_mount_count(ext4_superblock_t *sb)
+{
+	return uint16_t_le2host(sb->max_mount_count);
+}
+
+uint16_t ext4_superblock_get_magic(ext4_superblock_t *sb)
+{
+	return uint16_t_le2host(sb->magic);
+}
+
+uint16_t ext4_superblock_get_state(ext4_superblock_t *sb)
+{
+	return uint16_t_le2host(sb->state);
+}
+
+uint16_t ext4_superblock_get_errors(ext4_superblock_t *sb)
+{
+	return uint16_t_le2host(sb->errors);
+}
+
+
+uint16_t ext4_superblock_get_minor_rev_level(ext4_superblock_t *sb)
+{
+	return uint16_t_le2host(sb->minor_rev_level);
+}
+
+uint32_t ext4_superblock_get_last_check_time(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->last_check_time);
+}
+
+uint32_t ext4_superblock_get_check_interval(ext4_superblock_t *sb){
+	return uint32_t_le2host(sb->check_interval);
+}
+
+uint32_t ext4_superblock_get_creator_os(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->creator_os);
+}
+
+uint32_t ext4_superblock_get_rev_level(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->rev_level);
+}
+
+uint16_t ext4_superblock_get_inode_size(ext4_superblock_t *sb)
+{
+	if (ext4_superblock_get_rev_level(sb) == 0) {
+		return EXT4_REV0_INODE_SIZE;
+	}
+	return uint16_t_le2host(sb->inode_size);
+}
+
+uint16_t ext4_superblock_get_block_group_number(ext4_superblock_t *sb)
+{
+	return uint16_t_le2host(sb->block_group_number);
+}
+
+uint32_t ext4_superblock_get_features_compatible(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->features_compatible);
+}
+
+uint32_t ext4_superblock_get_features_incompatible(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->features_incompatible);
+}
+
+uint32_t ext4_superblock_get_features_read_only(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->features_read_only);
+}
+
+
+uint32_t* ext4_superblock_get_hash_seed(ext4_superblock_t *sb)
+{
+	return sb->hash_seed;
+}
+
+uint16_t ext4_superblock_get_desc_size(ext4_superblock_t *sb)
+{
+	uint16_t size = uint16_t_le2host(sb->desc_size);
+
+	if (size < EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE) {
+		size = EXT4_BLOCK_MIN_GROUP_DESCRIPTOR_SIZE;
+	}
+
+	return size;
+}
+
+uint32_t ext4_superblock_get_flags(ext4_superblock_t *sb)
+{
+	return uint32_t_le2host(sb->flags);
+}
+
+
+/*
+ * More complex superblock operations
+ */
+
+bool ext4_superblock_has_flag(ext4_superblock_t *sb, uint32_t flag)
+{
+	if (ext4_superblock_get_flags(sb) & flag) {
+		return true;
+	}
+	return false;
+}
+
+// Feature checkers
+bool ext4_superblock_has_feature_compatible(ext4_superblock_t *sb, uint32_t feature)
+{
+	if (ext4_superblock_get_features_compatible(sb) & feature) {
+		return true;
+	}
+	return false;
+}
+
+bool ext4_superblock_has_feature_incompatible(ext4_superblock_t *sb, uint32_t feature)
+{
+	if (ext4_superblock_get_features_incompatible(sb) & feature) {
+		return true;
+	}
+	return false;
+}
+
+bool ext4_superblock_has_feature_read_only(ext4_superblock_t *sb, uint32_t feature)
+{
+	if (ext4_superblock_get_features_read_only(sb) & feature) {
+		return true;
+	}
+	return false;
+}
+
+
+int ext4_superblock_read_direct(service_id_t service_id,
+    ext4_superblock_t **superblock)
+{
+	void *data;
+	int rc;
+
+	data = malloc(EXT4_SUPERBLOCK_SIZE);
+	if (data == NULL) {
+		return ENOMEM;
+	}
+
+	rc = block_read_bytes_direct(service_id, EXT4_SUPERBLOCK_OFFSET,
+	    EXT4_SUPERBLOCK_SIZE, data);
+
+	if (rc != EOK) {
+		free(data);
+		return rc;
+	}
+
+	(*superblock) = data;
+
+	return EOK;
+}
+
+int ext4_superblock_check_sanity(ext4_superblock_t *sb)
+{
+	if (ext4_superblock_get_magic(sb) != EXT4_SUPERBLOCK_MAGIC) {
+		return ENOTSUP;
+	}
+
+	// TODO more checks !!!
+
+	return EOK;
+}
+
+/**
+ * @}
+ */ 
Index: uspace/lib/ext4/libext4_superblock.h
===================================================================
--- uspace/lib/ext4/libext4_superblock.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
+++ uspace/lib/ext4/libext4_superblock.h	(revision 5664cf64f194597acabbc06a1c44f52ae4df0710)
@@ -0,0 +1,308 @@
+/*
+ * 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_SUPERBLOCK_H_
+#define LIBEXT4_LIBEXT4_SUPERBLOCK_H_
+
+#include <libblock.h>
+#include <sys/types.h>
+
+/*
+ * Structure of the super block
+ */
+typedef struct ext4_superblock {
+	uint32_t inodes_count; // Inodes count
+	uint32_t blocks_count_lo; // Blocks count
+	uint32_t reserved_blocks_count_lo; // Reserved blocks count
+	uint32_t free_blocks_count_lo; // Free blocks count
+	uint32_t free_inodes_count; // Free inodes count
+	uint32_t first_data_block; // First Data Block
+	uint32_t log_block_size; // Block size
+	uint32_t obso_log_frag_size; // Obsoleted fragment size
+	uint32_t blocks_per_group; // Number of blocks per group
+	uint32_t obso_frags_per_group; // Obsoleted fragments per group
+	uint32_t inodes_per_group; // Number of inodes per group
+	uint32_t mount_time; // Mount time
+	uint32_t write_time; // Write time
+	uint16_t mount_count; // Mount count
+	uint16_t max_mount_count; // Maximal mount count
+	uint16_t magic; // Magic signature
+	uint16_t state; // File system state
+	uint16_t errors; // Behaviour when detecting errors
+	uint16_t minor_rev_level; // Minor revision level
+	uint32_t last_check_time; // Time of last check
+	uint32_t check_interval; // Maximum time between checks
+	uint32_t creator_os; // Creator OS
+	uint32_t rev_level; // Revision level
+	uint16_t def_resuid; // Default uid for reserved blocks
+	uint16_t def_resgid; // Default gid for reserved blocks
+
+	// Fields for EXT4_DYNAMIC_REV superblocks only.
+	uint32_t first_inode; // First non-reserved inode
+	uint16_t inode_size; // Size of inode structure
+	uint16_t block_group_number; // Block group number of this superblock
+	uint32_t features_compatible; // Compatible feature set
+	uint32_t features_incompatible; // Incompatible feature set
+	uint32_t features_read_only; // Readonly-compatible feature set
+	uint8_t uuid[16]; // 128-bit uuid for volume
+	char volume_name[16]; // Volume name
+	char last_mounted[64]; // Directory where last mounted
+	uint32_t algorithm_usage_bitmap; // For compression
+
+	/*
+	 * Performance hints. Directory preallocation should only
+	 * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on.
+	 */
+	uint8_t s_prealloc_blocks; // Number of blocks to try to preallocate
+	uint8_t s_prealloc_dir_blocks; // Number to preallocate for dirs
+	uint16_t s_reserved_gdt_blocks; // Per group desc for online growth
+
+	/*
+	 * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.
+	 */
+	uint8_t journal_uuid[16]; // UUID of journal superblock
+	uint32_t journal_inode_number; // Inode number of journal file
+	uint32_t journal_dev; // Device number of journal file
+	uint32_t last_orphan; // Head of list of inodes to delete
+	uint32_t hash_seed[4]; // HTREE hash seed
+	uint8_t default_hash_version; // Default hash version to use
+	uint8_t journal_backup_type;
+	uint16_t desc_size; // Size of group descriptor
+	uint32_t default_mount_opts; // Default mount options
+	uint32_t first_meta_bg; // First metablock block group
+	uint32_t mkfs_time; // When the filesystem was created
+	uint32_t journal_blocks[17]; // Backup of the journal inode
+
+	/* 64bit support valid if EXT4_FEATURE_COMPAT_64BIT */
+	uint32_t blocks_count_hi; // Blocks count
+	uint32_t reserved_blocks_count_hi; // Reserved blocks count
+	uint32_t free_blocks_count_hi; // Free blocks count
+	uint16_t min_extra_isize; // All inodes have at least # bytes
+	uint16_t want_extra_isize; // New inodes should reserve # bytes
+	uint32_t flags; // Miscellaneous flags
+	uint16_t raid_stride; // RAID stride
+	uint16_t mmp_interval; // # seconds to wait in MMP checking
+	uint64_t mmp_block; // Block for multi-mount protection
+	uint32_t raid_stripe_width; // blocks on all data disks (N*stride)
+	uint8_t log_groups_per_flex; // FLEX_BG group size
+	uint8_t reserved_char_pad;
+	uint16_t reserved_pad;
+	uint64_t kbytes_written; // Number of lifetime kilobytes written
+	uint32_t snapshot_inum; // Inode number of active snapshot
+	uint32_t snapshot_id; // Sequential ID of active snapshot
+	uint64_t snapshot_r_blocks_count; /* reserved blocks for active snapshot's future use */
+	uint32_t snapshot_list; // inode number of the head of the on-disk snapshot list
+	uint32_t error_count; // number of fs errors
+	uint32_t first_error_time; // First time an error happened
+	uint32_t first_error_ino; // Inode involved in first error
+	uint64_t first_error_block; // block involved of first error
+	uint8_t first_error_func[32]; // Function where the error happened
+	uint32_t first_error_line; // Line number where error happened
+	uint32_t last_error_time; // Most recent time of an error
+	uint32_t last_error_ino; // Inode involved in last error
+	uint32_t last_error_line; // Line number where error happened
+	uint64_t last_error_block;     // Block involved of last error
+	uint8_t last_error_func[32];  // Function where the error happened
+	uint8_t mount_opts[64];
+	uint32_t padding[112]; // Padding to the end of the block
+} __attribute__((packed)) ext4_superblock_t;
+
+#define EXT4_SUPERBLOCK_MAGIC		0xEF53
+#define EXT4_SUPERBLOCK_SIZE		1024
+#define EXT4_SUPERBLOCK_OFFSET		1024
+
+#define EXT4_SUPERBLOCK_OS_LINUX	0
+#define EXT4_SUPERBLOCK_OS_HURD		1
+
+/*
+ * Misc. filesystem flags
+ */
+#define EXT4_SUPERBLOCK_FLAGS_SIGNED_HASH	0x0001  /* Signed dirhash in use */
+#define EXT4_SUPERBLOCK_FLAGS_UNSIGNED_HASH	0x0002  /* Unsigned dirhash in use */
+#define EXT4_SUPERBLOCK_FLAGS_TEST_FILESYS	0x0004  /* to test development code */
+
+/* Compatible features */
+#define EXT4_FEATURE_COMPAT_DIR_PREALLOC        0x0001
+#define EXT4_FEATURE_COMPAT_IMAGIC_INODES       0x0002
+#define EXT4_FEATURE_COMPAT_HAS_JOURNAL         0x0004
+#define EXT4_FEATURE_COMPAT_EXT_ATTR            0x0008
+#define EXT4_FEATURE_COMPAT_RESIZE_INODE        0x0010
+#define EXT4_FEATURE_COMPAT_DIR_INDEX           0x0020
+
+/* Read-only compatible features */
+#define EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER     0x0001
+#define EXT4_FEATURE_RO_COMPAT_LARGE_FILE       0x0002
+#define EXT4_FEATURE_RO_COMPAT_BTREE_DIR        0x0004
+#define EXT4_FEATURE_RO_COMPAT_HUGE_FILE        0x0008
+#define EXT4_FEATURE_RO_COMPAT_GDT_CSUM         0x0010
+#define EXT4_FEATURE_RO_COMPAT_DIR_NLINK        0x0020
+#define EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE      0x0040
+
+/* Incompatible features */
+#define EXT4_FEATURE_INCOMPAT_COMPRESSION       0x0001
+#define EXT4_FEATURE_INCOMPAT_FILETYPE          0x0002
+#define EXT4_FEATURE_INCOMPAT_RECOVER           0x0004 /* Needs recovery */
+#define EXT4_FEATURE_INCOMPAT_JOURNAL_DEV       0x0008 /* Journal device */
+#define EXT4_FEATURE_INCOMPAT_META_BG           0x0010
+#define EXT4_FEATURE_INCOMPAT_EXTENTS           0x0040 /* extents support */
+#define EXT4_FEATURE_INCOMPAT_64BIT             0x0080
+#define EXT4_FEATURE_INCOMPAT_MMP               0x0100
+#define EXT4_FEATURE_INCOMPAT_FLEX_BG           0x0200
+#define EXT4_FEATURE_INCOMPAT_EA_INODE          0x0400 /* EA in inode */
+#define EXT4_FEATURE_INCOMPAT_DIRDATA           0x1000 /* data in dirent */
+
+// TODO MODIFY features corresponding with implementation
+#define EXT4_FEATURE_COMPAT_SUPP EXT4_FEATURE_COMPAT_EXT_ATTR
+
+#define EXT4_FEATURE_INCOMPAT_SUPP      (EXT4_FEATURE_INCOMPAT_FILETYPE| \
+                                         EXT4_FEATURE_INCOMPAT_RECOVER| \
+                                         EXT4_FEATURE_INCOMPAT_META_BG| \
+                                         EXT4_FEATURE_INCOMPAT_EXTENTS| \
+                                         EXT4_FEATURE_INCOMPAT_64BIT| \
+                                         EXT4_FEATURE_INCOMPAT_FLEX_BG)
+
+#define EXT4_FEATURE_RO_COMPAT_SUPP     (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
+                                         EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
+                                         EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
+                                         EXT4_FEATURE_RO_COMPAT_DIR_NLINK | \
+                                         EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE | \
+                                         EXT4_FEATURE_RO_COMPAT_BTREE_DIR |\
+                                         EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
+
+
+
+extern uint32_t ext4_superblock_get_inodes_count(ext4_superblock_t *);
+extern uint64_t ext4_superblock_get_blocks_count(ext4_superblock_t *);
+extern uint64_t ext4_superblock_get_reserved_blocks_count(ext4_superblock_t *);
+extern uint64_t ext4_superblock_get_free_blocks_count(ext4_superblock_t *);
+extern uint32_t ext4_superblock_get_free_inodes_count(ext4_superblock_t *);
+extern uint32_t ext4_superblock_get_first_data_block(ext4_superblock_t *);
+extern uint32_t ext4_superblock_get_log_block_size(ext4_superblock_t *);
+extern uint32_t ext4_superblock_get_block_size(ext4_superblock_t *);
+extern uint32_t ext4_superblock_get_blocks_per_group(ext4_superblock_t *);
+extern uint32_t ext4_superblock_get_inodes_per_group(ext4_superblock_t *);
+extern uint32_t ext4_superblock_get_mount_time(ext4_superblock_t *);
+extern uint32_t ext4_superblock_get_write_time(ext4_superblock_t *);
+extern uint16_t ext4_superblock_get_mount_count(ext4_superblock_t *);
+extern uint16_t ext4_superblock_get_max_mount_count(ext4_superblock_t *);
+extern uint16_t ext4_superblock_get_magic(ext4_superblock_t *);
+extern uint16_t ext4_superblock_get_state(ext4_superblock_t *);
+extern uint16_t ext4_superblock_get_errors(ext4_superblock_t *);
+extern uint16_t ext4_superblock_get_minor_rev_level(ext4_superblock_t *);
+extern uint32_t ext4_superblock_get_last_check_time(ext4_superblock_t *);
+extern uint32_t ext4_superblock_get_check_interval(ext4_superblock_t *);
+extern uint32_t ext4_superblock_get_creator_os(ext4_superblock_t *);
+extern uint32_t ext4_superblock_get_rev_level(ext4_superblock_t *);
+
+/*
+uint16_t s_def_resuid; // Default uid for reserved blocks
+uint16_t s_def_resgid; // Default gid for reserved blocks
+*/
+
+extern uint32_t ext4_superblock_get_first_inode(ext4_superblock_t *);
+extern uint16_t ext4_superblock_get_inode_size(ext4_superblock_t *);
+extern uint16_t ext4_superblock_get_block_group_number(ext4_superblock_t *);
+extern uint32_t	ext4_superblock_get_features_compatible(ext4_superblock_t *);
+extern uint32_t	ext4_superblock_get_features_incompatible(ext4_superblock_t *);
+extern uint32_t	ext4_superblock_get_features_read_only(ext4_superblock_t *);
+
+/*
+uint8_t s_uuid[16]; // 128-bit uuid for volume
+char volume_name[16]; // Volume name
+char last_mounted[64]; // Directory where last mounted
+uint32_t s_algorithm_usage_bitmap; // For compression
+uint8_t s_prealloc_blocks; // Number of blocks to try to preallocate
+uint8_t s_prealloc_dir_blocks; // Number to preallocate for dirs
+uint16_t s_reserved_gdt_blocks; // Per group desc for online growth
+uint8_t s_journal_uuid[16]; // UUID of journal superblock
+uint32_t s_journal_inum; // Inode number of journal file
+uint32_t s_journal_dev; // Device number of journal file
+uint32_t s_last_orphan; // Head of list of inodes to delete
+*/
+extern uint32_t* ext4_superblock_get_hash_seed(ext4_superblock_t *);
+
+/*
+uint8_t s_def_hash_version; // Default hash version to use
+uint8_t s_jnl_backup_type;
+*/
+
+extern uint16_t ext4_superblock_get_desc_size(ext4_superblock_t *);
+
+/*
+uint32_t s_default_mount_opts; // Default mount options
+uint32_t s_first_meta_bg; // First metablock block group
+uint32_t s_mkfs_time; // When the filesystem was created
+uint32_t s_jnl_blocks[17]; // Backup of the journal inode
+uint16_t s_min_extra_isize; // All inodes have at least # bytes
+uint16_t s_want_extra_isize; // New inodes should reserve # bytes
+*/
+extern uint32_t ext4_superblock_get_flags(ext4_superblock_t *);
+/*
+uint16_t s_raid_stride; // RAID stride
+uint16_t s_mmp_interval; // # seconds to wait in MMP checking
+uint64_t s_mmp_block; // Block for multi-mount protection
+uint32_t s_raid_stripe_width; // blocks on all data disks (N*stride)
+uint8_t s_log_groups_per_flex; // FLEX_BG group size
+uint8_t s_reserved_char_pad;
+uint16_t s_reserved_pad;
+uint64_t s_kbytes_written; // Number of lifetime kilobytes written
+uint32_t s_snapshot_inum; // Inode number of active snapshot
+uint32_t s_snapshot_id; // Sequential ID of active snapshot
+uint64_t s_snapshot_r_blocks_count; // reserved blocks for active snapshot's future use
+uint32_t s_snapshot_list; // inode number of the head of the on-disk snapshot list
+uint32_t s_error_count; // number of fs errors
+uint32_t s_first_error_time; // First time an error happened
+uint32_t s_first_error_ino; // Inode involved in first error
+uint64_t s_first_error_block; // block involved of first error
+uint8_t s_first_error_func[32]; // Function where the error happened
+uint32_t s_first_error_line; // Line number where error happened
+uint32_t s_last_error_time; // Most recent time of an error
+uint32_t s_last_error_ino; // Inode involved in last error
+uint32_t s_last_error_line; // Line number where error happened
+uint64_t s_last_error_block;     // block involved of last error
+uint8_t s_last_error_func[32];  // function where the error happened
+uint8_t s_mount_opts[64];
+*/
+
+/* More complex superblock functions */
+extern bool ext4_superblock_has_flag(ext4_superblock_t *, uint32_t);
+extern bool ext4_superblock_has_feature_compatible(ext4_superblock_t *, uint32_t);
+extern bool ext4_superblock_has_feature_incompatible(ext4_superblock_t *, uint32_t);
+extern bool ext4_superblock_has_feature_read_only(ext4_superblock_t *, uint32_t);
+extern int ext4_superblock_read_direct(service_id_t, ext4_superblock_t **);
+extern int ext4_superblock_check_sanity(ext4_superblock_t *);
+
+#endif
+
+/**
+ * @}
+ */
