Index: uspace/srv/fs/fat/fat.h
===================================================================
--- uspace/srv/fs/fat/fat.h	(revision cde485d925ad71041a0d21bd2017a6e1860f19be)
+++ uspace/srv/fs/fat/fat.h	(revision c05a50f0decf2abb14cd4d0101cd06a0477db5c2)
@@ -41,5 +41,7 @@
 #include "../../vfs/vfs.h"
 
+#ifndef dprintf
 #define dprintf(...)	printf(__VA_ARGS__)
+#endif
 
 typedef struct {
Index: uspace/srv/fs/fat/fat_idx.c
===================================================================
--- uspace/srv/fs/fat/fat_idx.c	(revision cde485d925ad71041a0d21bd2017a6e1860f19be)
+++ uspace/srv/fs/fat/fat_idx.c	(revision c05a50f0decf2abb14cd4d0101cd06a0477db5c2)
@@ -84,4 +84,21 @@
 }
 
+static unused_t *unused_find(dev_handle_t dev_handle, bool lock)
+{
+	unused_t *u;
+	link_t *l;
+
+	if (lock)
+		futex_down(&unused_futex);
+	for (l = unused_head.next; l != &unused_head; l = l->next) {
+		u = list_get_instance(l, unused_t, link);
+		if (u->dev_handle == dev_handle) 
+			return u;
+	}
+	if (lock)
+		futex_up(&unused_futex);
+	return NULL;
+}
+
 /** Futex protecting the up_hash and ui_hash. */
 static futex_t used_futex = FUTEX_INITIALIZER; 
@@ -200,20 +217,11 @@
 static bool fat_idx_alloc(dev_handle_t dev_handle, fs_index_t *index)
 {
-	link_t *l;
 	unused_t *u;
 	
 	assert(index);
-	futex_down(&unused_futex);
-	for (l = unused_head.next; l != &unused_head; l = l->next) {
-		u = list_get_instance(l, unused_t, link);
-		if (u->dev_handle == dev_handle) 
-			goto hit;
-	}
-	futex_up(&unused_futex);
-	
-	/* dev_handle not found */
-	return false;	
-
-hit:
+	u = unused_find(dev_handle, true);
+	if (!u)
+		return false;	
+
 	if (list_empty(&u->freed_head)) {
 		if (u->remaining) { 
@@ -271,19 +279,9 @@
 static void fat_idx_free(dev_handle_t dev_handle, fs_index_t index)
 {
-	link_t *l;
 	unused_t *u;
 
-	futex_down(&unused_futex);
-	for (l = unused_head.next; l != &unused_head; l = l->next) {
-		u = list_get_instance(l, unused_t, link);
-		if (u->dev_handle == dev_handle)
-			goto hit;
-	}
-	futex_up(&unused_futex);
-
-	/* should not happen */
-	assert(0);
-
-hit:
+	u = unused_find(dev_handle, true);
+	assert(u);
+
 	if (u->next == index + 1) {
 		/* The index can be returned directly to the counter. */
@@ -431,12 +429,18 @@
 int fat_idx_init_by_dev_handle(dev_handle_t dev_handle)
 {
-	unused_t *u = (unused_t *) malloc(sizeof(unused_t));
+	unused_t *u;
+	int rc = EOK;
+
+	u = (unused_t *) malloc(sizeof(unused_t));
 	if (!u)
 		return ENOMEM;
 	unused_initialize(u, dev_handle);
 	futex_down(&unused_futex);
-	list_append(&u->link, &unused_head);
+	if (!unused_find(dev_handle, false))
+		list_append(&u->link, &unused_head);
+	else
+		rc = EEXIST;
 	futex_up(&unused_futex);
-	return EOK;
+	return rc;
 }
 
@@ -444,17 +448,7 @@
 {
 	unused_t *u;
-	link_t *l;
-
-	futex_down(&unused_futex);
-	for (l = unused_head.next; l != &unused_head; l = l->next) {
-		u = list_get_instance(l, unused_t, link);
-		if (u->dev_handle == dev_handle) 
-			goto hit;
-	}
-	futex_up(&unused_futex);
-
-	assert(false);	/* should not happen */
-
-hit:
+
+	u = unused_find(dev_handle, true);
+	assert(u);
 	list_remove(&u->link);
 	futex_up(&unused_futex);
