Index: uspace/lib/ext4/libext4_filesystem.c
===================================================================
--- uspace/lib/ext4/libext4_filesystem.c	(revision 4059a28d00aaa44ab59ca33550b95a278a210296)
+++ uspace/lib/ext4/libext4_filesystem.c	(revision 840e227e23022d5e168d594dff6b207772e7ec31)
@@ -56,4 +56,5 @@
 	rc = block_init(EXCHANGE_SERIALIZE, fs->device, 4096);
 	if (rc != EOK) {
+		EXT4FS_DBG("block init error: \%d", rc);
 		return rc;
 	}
@@ -64,4 +65,5 @@
 	if (rc != EOK) {
 		block_fini(fs->device);
+		EXT4FS_DBG("superblock read error: \%d", rc);
 		return rc;
 	}
@@ -71,4 +73,5 @@
 	if (block_size > EXT4_MAX_BLOCK_SIZE) {
 		block_fini(fs->device);
+		EXT4FS_DBG("get blocksize error: \%d", rc);
 		return ENOTSUP;
 	}
@@ -78,4 +81,5 @@
 	if (rc != EOK) {
 		block_fini(fs->device);
+		EXT4FS_DBG("block cache init error: \%d", rc);
 		return rc;
 	}
@@ -98,4 +102,7 @@
 
 	if (state != EXT4_SUPERBLOCK_STATE_VALID_FS) {
+		block_cache_fini(fs->device);
+		block_fini(fs->device);
+		EXT4FS_DBG("invalid state error");
 		return ENOTSUP;
 	}
@@ -105,4 +112,7 @@
 	rc = ext4_superblock_write_direct(fs->device, fs->superblock);
 	if (rc != EOK) {
+		block_cache_fini(fs->device);
+		block_fini(fs->device);
+		EXT4FS_DBG("state write error: \%d", rc);
 		return rc;
 	}
@@ -1103,6 +1113,4 @@
 	int rc;
 
-	EXT4FS_DBG("");
-
 	// Handle extents separately
 	if (ext4_superblock_has_feature_incompatible(
@@ -1159,4 +1167,7 @@
 int ext4_filesystem_add_orphan(ext4_inode_ref_t *inode_ref)
 {
+
+	EXT4FS_DBG("adding orphan \%u", inode_ref->index);
+
 	uint32_t next_orphan = ext4_superblock_get_last_orphan(
 			inode_ref->fs->superblock);
@@ -1180,4 +1191,7 @@
 int ext4_filesystem_delete_orphan(ext4_inode_ref_t *inode_ref)
 {
+
+	EXT4FS_DBG("adding orphan \%u", inode_ref->index);
+
 	int rc;
 
@@ -1185,23 +1199,22 @@
 	uint32_t last_orphan = ext4_superblock_get_last_orphan(
 			inode_ref->fs->superblock);
-	assert(last_orphan > 0);
-
-	uint32_t next_orphan = ext4_inode_get_deletion_time(inode_ref->inode);
+
+	assert (last_orphan != 0);
+
+	ext4_inode_ref_t *current;
+	rc = ext4_filesystem_get_inode_ref(inode_ref->fs, last_orphan, &current);
+	if (rc != EOK) {
+		return rc;
+	}
+
+	uint32_t next_orphan = ext4_inode_get_deletion_time(current->inode);
 
 	// Check if the head is the target
 	if (last_orphan == inode_ref->index) {
 		ext4_superblock_set_last_orphan(inode_ref->fs->superblock, next_orphan);
-		ext4_inode_set_deletion_time(inode_ref->inode, 0);
-		inode_ref->dirty = true;
+//		ext4_inode_set_deletion_time(inode_ref->inode, 0);
+//		inode_ref->dirty = true;
 		return EOK;
 	}
-
-	ext4_inode_ref_t *current;
-	rc = ext4_filesystem_get_inode_ref(inode_ref->fs, last_orphan, &current);
-	if (rc != EOK) {
-		return rc;
-	}
-
-	next_orphan = ext4_inode_get_deletion_time(current->inode);
 
 	bool found = false;
@@ -1214,4 +1227,6 @@
 			next_orphan = ext4_inode_get_deletion_time(inode_ref->inode);
 			ext4_inode_set_deletion_time(current->inode, next_orphan);
+//			ext4_inode_set_deletion_time(inode_ref->inode, 0);
+//			inode_ref->dirty = true;
 			current->dirty = true;
 			found = true;
@@ -1229,8 +1244,4 @@
 	}
 
-	if (found) {
-		ext4_inode_set_deletion_time(inode_ref->inode, 0);
-	}
-
 	rc = ext4_filesystem_put_inode_ref(current);
 	if (rc != EOK) {
@@ -1240,7 +1251,7 @@
 	if (!found) {
 		return ENOENT;
-	}
-
-	return EOK;
+	} else {
+		return EOK;
+	}
 }
 
