Index: uspace/lib/ext2/Makefile
===================================================================
--- uspace/lib/ext2/Makefile	(revision d5e276303aacb69e6369060c00aa10fcf9894915)
+++ uspace/lib/ext2/Makefile	(revision 36bca8ebd67f3523f829bd02ddf87f9c8f0640b1)
@@ -31,7 +31,9 @@
 USPACE_PREFIX = ../..
 LIBRARY = libext2
+EXTRA_CFLAGS = -I$(LIBBLOCK_PREFIX)
 
 SOURCES = \
-	libext2.c
+	libext2_filesystem.c \
+	libext2_superblock.c
 
 include $(USPACE_PREFIX)/Makefile.common
Index: uspace/lib/ext2/libext2.c
===================================================================
--- uspace/lib/ext2/libext2.c	(revision d5e276303aacb69e6369060c00aa10fcf9894915)
+++ 	(revision )
@@ -1,45 +1,0 @@
-/*
- * Copyright (c) 2010 Martin Sucha
- * 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 libext2
- * @{
- */
-/**
- * @file
- */
-
-#include "libext2.h"
-#include <macros.h>
-#include <errno.h>
-
-inline uint16_t ext2_superblock_get_magic(ext2_superblock_t *superblock) {
-	return uint16_t_le2host(superblock->magic);
-}
-
-/** @}
- */
Index: uspace/lib/ext2/libext2.h
===================================================================
--- uspace/lib/ext2/libext2.h	(revision d5e276303aacb69e6369060c00aa10fcf9894915)
+++ uspace/lib/ext2/libext2.h	(revision 36bca8ebd67f3523f829bd02ddf87f9c8f0640b1)
@@ -38,13 +38,46 @@
 
 #include <byteorder.h>
+#include <libblock.h>
 
 typedef struct ext2_superblock {
-	uint8_t unused[56];
-	uint16_t magic;
-} ext2_superblock_t;
+	uint8_t		unused[20];
+	uint32_t	first_block; // Block containing the superblock (either 0 or 1)
+	uint32_t	block_size_log2; // log_2(block_size)
+	int32_t		fragment_size_log2; // log_2(fragment size)
+	uint32_t	blocks_per_group; // Number of blocks in one block group
+	uint32_t	fragments_per_group; // Number of fragments per block group
+	uint32_t	inodes_per_group; // Number of inodes per block group
+	uint8_t		unused2[12];
+	uint16_t	magic; // Magic value
+} __attribute__ ((packed)) ext2_superblock_t;
+// TODO: add __attribute__((aligned(...)) for better performance?
+//       (it is necessary to ensure the superblock is correctly aligned then
+//        though)
 
-#define EXT2_SUPERBLOCK_MAGIC 0xEF53
+typedef struct ext2_filesystem {
+	devmap_handle_t		device;
+	ext2_superblock_t *	superblock;
+} ext2_filesystem_t;
 
-inline uint16_t ext2_superblock_get_magic(ext2_superblock_t *superblock);
+#define EXT2_SUPERBLOCK_MAGIC		0xEF53
+#define EXT2_SUPERBLOCK_SIZE		1024
+#define EXT2_SUPERBLOCK_OFFSET		1024
+#define EXT2_SUPERBLOCK_LAST_BYTE	(EXT2_SUPERBLOCK_OFFSET + \
+									 EXT2_SUPERBLOCK_SIZE -1)
+
+inline uint16_t	ext2_superblock_get_magic(ext2_superblock_t *sb);
+inline uint32_t	ext2_superblock_get_first_block(ext2_superblock_t *sb);
+inline uint32_t	ext2_superblock_get_block_size_log2(ext2_superblock_t *sb);
+inline uint32_t	ext2_superblock_get_block_size(ext2_superblock_t *sb);
+inline int32_t	ext2_superblock_get_fragment_size_log2(ext2_superblock_t *sb);
+inline uint32_t	ext2_superblock_get_fragment_size(ext2_superblock_t *sb);
+inline uint32_t	ext2_superblock_get_blocks_per_group(ext2_superblock_t *sb);
+inline uint32_t	ext2_superblock_get_fragments_per_group(ext2_superblock_t *sb);
+
+int ext2_superblock_read_direct(ext2_superblock_t **superblock,
+								devmap_handle_t dev);
+
+int ext2_filesystem_init(ext2_filesystem_t *fs, devmap_handle_t dev);
+void ext2_filesystem_fini(ext2_filesystem_t *fs);
 
 #endif
Index: uspace/lib/ext2/libext2_filesystem.c
===================================================================
--- uspace/lib/ext2/libext2_filesystem.c	(revision 36bca8ebd67f3523f829bd02ddf87f9c8f0640b1)
+++ uspace/lib/ext2/libext2_filesystem.c	(revision 36bca8ebd67f3523f829bd02ddf87f9c8f0640b1)
@@ -0,0 +1,130 @@
+/*
+ * Copyright (c) 2011 Martin Sucha
+ * 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 libext2
+ * @{
+ */
+/**
+ * @file
+ */
+
+#include "libext2.h"
+#include <errno.h>
+#include <libblock.h>
+#include <mem.h>
+#include <malloc.h>
+
+int ext2_superblock_read_direct(ext2_superblock_t **superblock,
+								devmap_handle_t dev) {
+	int rc;
+	size_t phys_block_size;
+	size_t buf_size;
+	uint8_t *buffer;
+	size_t first_block;
+	size_t last_block;
+	size_t blocks;
+	size_t offset;
+	ext2_superblock_t *tmp_superblock;
+	
+	rc = block_get_bsize(dev, &phys_block_size);
+	if (rc != EOK) {
+		return rc;
+	}
+	
+	// calculate superblock position and required space
+	first_block = EXT2_SUPERBLOCK_OFFSET / phys_block_size;
+	offset = EXT2_SUPERBLOCK_OFFSET % phys_block_size;
+	last_block = EXT2_SUPERBLOCK_LAST_BYTE / phys_block_size;
+	blocks = last_block - first_block + 1;
+	buf_size = blocks * phys_block_size;
+	
+	// read the superblock into memory
+	buffer = malloc(buf_size);
+	if (buffer == NULL) {
+		return ENOMEM;
+	}
+	
+	rc = block_read_direct(dev, first_block, blocks, buffer);
+	if (rc != EOK) {
+		free(buffer);
+		return rc;
+	}
+	
+	// copy the superblock from the buffer
+	// as it may not be at the start of the block
+	// (i.e. blocks larger than 1K)
+	tmp_superblock = malloc(EXT2_SUPERBLOCK_SIZE);
+	if (tmp_superblock == NULL) {
+		free(buffer);
+		return ENOMEM;
+	}
+	
+	memcpy(tmp_superblock, buffer + offset, EXT2_SUPERBLOCK_SIZE);
+	free(buffer);
+	(*superblock) = tmp_superblock;
+	
+	return EOK;
+}
+
+/**
+ * Initialize an instance of filesystem on the device.
+ * This function reads superblock from the device and
+ * initializes libblock cache with appropriate logical block size.
+ */
+int ext2_filesystem_init(ext2_filesystem_t *fs, devmap_handle_t dev) {
+	int rc;
+	ext2_superblock_t *temp_superblock;
+	
+	fs->device = dev;
+	
+	rc = block_init(fs->device, 2048);
+	if (rc != EOK) {
+		return rc;
+	}
+	
+	rc = ext2_superblock_read_direct(&temp_superblock, dev);
+	if (rc != EOK) {
+		block_fini(dev);
+		return rc;
+	}
+	
+	free(temp_superblock);
+	
+	return EOK; 
+}
+
+/**
+ * Finalize an instance of filesystem
+ */
+void ext2_filesystem_fini(ext2_filesystem_t *fs) {
+	block_fini(fs->device);
+}
+
+
+/** @}
+ */
Index: uspace/lib/ext2/libext2_superblock.c
===================================================================
--- uspace/lib/ext2/libext2_superblock.c	(revision 36bca8ebd67f3523f829bd02ddf87f9c8f0640b1)
+++ uspace/lib/ext2/libext2_superblock.c	(revision 36bca8ebd67f3523f829bd02ddf87f9c8f0640b1)
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2011 Martin Sucha
+ * 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 libext2
+ * @{
+ */
+/**
+ * @file
+ */
+
+#include "libext2.h"
+
+/**
+ * Return a magic number from ext2 superblock, this should be equal to
+ * EXT_SUPERBLOCK_MAGIC for valid ext2 superblock
+ */
+inline uint16_t ext2_superblock_get_magic(ext2_superblock_t *sb) {
+	return uint16_t_le2host(sb->magic);
+}
+
+/**
+ * Get the position of first ext2 data block (i.e. the block number
+ * containing main superblock)
+ */
+inline uint32_t ext2_superblock_get_first_block(ext2_superblock_t *sb) {
+	return uint32_t_le2host(sb->first_block);
+}
+
+/**
+ * Get the number of bits to shift a value of 1024 to the left necessary
+ * to get the size of a block
+ */
+inline uint32_t ext2_superblock_get_block_size_log2(ext2_superblock_t *sb) {
+	return uint32_t_le2host(sb->block_size_log2);
+}
+
+/**
+ * Get the size of a block, in bytes 
+ */
+inline uint32_t ext2_superblock_get_block_size(ext2_superblock_t *sb) {
+	return 1024 << ext2_superblock_get_block_size(sb);
+}
+
+/**
+ * Get the number of bits to shift a value of 1024 to the left necessary
+ * to get the size of a fragment (note that this is a signed integer and
+ * if negative, the value should be shifted to the right instead)
+ */
+inline int32_t ext2_superblock_get_fragment_size_log2(ext2_superblock_t *sb) {
+	return uint32_t_le2host(sb->fragment_size_log2);
+}
+
+/**
+ * Get the size of a fragment, in bytes 
+ */
+inline uint32_t ext2_superblock_get_fragment_size(ext2_superblock_t *sb) {
+	int32_t log = ext2_superblock_get_fragment_size_log2(sb);
+	if (log >= 0) {
+		return 1024 << log;
+	}
+	else {
+		return 1024 >> -log;
+	}
+}
+
+/**
+ * Get number of blocks per block group
+ */
+inline uint32_t ext2_superblock_get_blocks_per_group(ext2_superblock_t *sb) {
+	return uint32_t_le2host(sb->blocks_per_group);
+}
+
+/**
+ * Get number of fragments per block group
+ */
+inline uint32_t ext2_superblock_get_fragments_per_group(ext2_superblock_t *sb) {
+	return uint32_t_le2host(sb->fragments_per_group);
+}
+
+
+/** @}
+ */
