Index: uspace/lib/ext2/libext2_directory.c
===================================================================
--- uspace/lib/ext2/libext2_directory.c	(revision 102d4000af331bdf964271ff6b88d5992d3e1f77)
+++ uspace/lib/ext2/libext2_directory.c	(revision 9ffbdf145d39e6ae279fab6fcaf7b49ac229eecc)
@@ -37,4 +37,6 @@
 #include "libext2_directory.h"
 #include <byteorder.h>
+#include <errno.h>
+#include <assert.h>
 
 /**
@@ -75,4 +77,159 @@
 }
 
+/**
+ * Initialize a directory iterator
+ * 
+ * @param it pointer to iterator to initialize
+ * @param fs pointer to filesystem structure
+ * @param inode pointer to inode reference structure
+ * @return EOK on success or negative error code on failure
+ */
+int ext2_directory_iterator_init(ext2_directory_iterator_t *it,
+    ext2_filesystem_t *fs, ext2_inode_ref_t *inode_ref)
+{
+	int rc;
+	uint32_t block_id;
+	it->inode_ref = inode_ref;
+	it->fs = fs;
+	
+	// Get the first data block, so we can get first entry
+	rc = ext2_filesystem_get_inode_data_block_index(fs, inode_ref->inode, 0, 
+	    &block_id);
+	if (rc != EOK) {
+		return rc;
+	}
+	
+	rc = block_get(&it->current_block, fs->device, block_id, 0);
+	if (rc != EOK) {
+		return rc;
+	}
+	
+	it->current = it->current_block->data;
+	it->current_offset = 0;
+	
+	return EOK;
+}
+
+/**
+ * Advance the directory iterator to the next entry
+ * 
+ * @param it pointer to iterator to initialize
+ * @return EOK on success or negative error code on failure
+ */
+int ext2_directory_iterator_next(ext2_directory_iterator_t *it)
+{
+	int rc;
+	uint16_t skip;
+	uint64_t size;
+	aoff64_t current_block_idx;
+	aoff64_t next_block_idx;
+	uint32_t next_block_phys_idx;
+	uint32_t block_size;
+	uint32_t offset_in_block;
+	
+	assert(it->current != NULL);
+	
+	skip = ext2_directory_entry_ll_get_entry_length(it->current);
+	size = ext2_inode_get_size(it->fs->superblock, it->inode_ref->inode);
+	
+	// Are we at the end?
+	if (it->current_offset + skip >= size) {
+		rc = block_put(it->current_block);
+		it->current_block = NULL;
+		it->current = NULL;
+		if (rc != EOK) {
+			return rc;
+		}
+		
+		it->current_offset += skip;
+		return EOK;
+	}
+	
+	block_size = ext2_superblock_get_block_size(it->fs->superblock);
+	current_block_idx = it->current_offset / block_size;
+	next_block_idx = (it->current_offset + skip) / block_size;
+	
+	// If we are moving accross block boundary,
+	// we need to get another block
+	if (current_block_idx != next_block_idx) {
+		rc = block_put(it->current_block);
+		it->current_block = NULL;
+		it->current = NULL;
+		if (rc != EOK) {
+			return rc;
+		}
+		
+		rc = ext2_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;
+		}
+	}
+	
+	offset_in_block = (it->current_offset + skip) % block_size;
+	
+	// Ensure proper alignment
+	if ((offset_in_block % 4) != 0) {
+		it->current = NULL;
+		return EIO;
+	}
+	
+	// Ensure that the core of the entry does not overflow the block
+	if (offset_in_block > block_size - 8) {
+		it->current = NULL;
+		return EIO;
+	}
+		
+	it->current = it->current_block->data + offset_in_block;
+	it->current_offset += skip;
+	
+	// Ensure that the whole entry does not overflow the block
+	skip = ext2_directory_entry_ll_get_entry_length(it->current);
+	if (offset_in_block + skip > block_size) {
+		it->current = NULL;
+		return EIO;
+	}
+	
+	// Ensure the name length is not too large
+	if (ext2_directory_entry_ll_get_name_length(it->fs->superblock, 
+	    it->current) > skip-8) {
+		it->current = NULL;
+		return EIO;
+	}
+	
+	return EOK;
+}
+
+/**
+ * Release all resources asociated with the directory iterator
+ * 
+ * @param it pointer to iterator to initialize
+ * @return EOK on success or negative error code on failure
+ */
+int ext2_directory_iterator_fini(ext2_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/ext2/libext2_directory.h
===================================================================
--- uspace/lib/ext2/libext2_directory.h	(revision 102d4000af331bdf964271ff6b88d5992d3e1f77)
+++ uspace/lib/ext2/libext2_directory.h	(revision 9ffbdf145d39e6ae279fab6fcaf7b49ac229eecc)
@@ -38,4 +38,6 @@
 
 #include <libblock.h>
+#include "libext2_filesystem.h"
+#include "libext2_inode.h"
 
 /**
@@ -53,4 +55,12 @@
 } __attribute__ ((packed)) ext2_directory_entry_ll_t;
 
+typedef struct ext2_directory_iterator {
+	ext2_filesystem_t *fs;
+	ext2_inode_ref_t *inode_ref;
+	block_t *current_block;
+	aoff64_t current_offset;
+	ext2_directory_entry_ll_t *current;
+} ext2_directory_iterator_t;
+
 
 inline uint32_t	ext2_directory_entry_ll_get_inode(ext2_directory_entry_ll_t *);
@@ -60,4 +70,9 @@
     ext2_superblock_t *, ext2_directory_entry_ll_t *);
 
+extern int ext2_directory_iterator_init(ext2_directory_iterator_t *,
+    ext2_filesystem_t *, ext2_inode_ref_t *);
+extern int ext2_directory_iterator_next(ext2_directory_iterator_t *);
+extern int ext2_directory_iterator_fini(ext2_directory_iterator_t *);
+
 #endif
 
