Index: uspace/srv/devman/main.c
===================================================================
--- uspace/srv/devman/main.c	(revision c1a04886f6fc4fd68787bad7a58a1e5f594797ce)
+++ uspace/srv/devman/main.c	(revision e2b9b3419fee2bcd7b8c7c52f092a8f64c857124)
@@ -391,4 +391,12 @@
 	fibril_rwlock_write_lock(&tree->rwlock);
 	
+	/* Check device state */
+	if (pdev->state == DEVICE_REMOVED) {
+		fibril_rwlock_write_unlock(&tree->rwlock);
+		dev_del_ref(pdev);
+		async_answer_0(callid, ENOENT);
+		return;
+	}
+	
 	/* Check that function with same name is not there already. */
 	if (find_fun_node_in_device(tree, pdev, fun_name) != NULL) {
@@ -453,4 +461,11 @@
 	fibril_rwlock_read_lock(&device_tree.rwlock);
 	
+	/* Check function state */
+	if (fun->state == FUN_REMOVED) {
+		fibril_rwlock_read_unlock(&device_tree.rwlock);
+		async_answer_0(callid, ENOENT);
+		return;
+	}
+	
 	rc = loc_category_get_id(cat_name, &cat_id, IPC_FLAG_BLOCKING);
 	if (rc == EOK) {
@@ -561,4 +576,11 @@
 	
 	log_msg(LVL_DEBUG, "devman_remove_function(fun='%s')", fun->pathname);
+	
+	/* Check function state */
+	if (fun->state == FUN_REMOVED) {
+		fibril_rwlock_write_unlock(&tree->rwlock);
+		async_answer_0(callid, ENOENT);
+		return;
+	}
 	
 	if (fun->ftype == fun_inner) {
@@ -700,5 +722,13 @@
 
 	fibril_rwlock_read_lock(&device_tree.rwlock);
+
+	/* Check function state */
+	if (fun->state == FUN_REMOVED) {
+		fibril_rwlock_read_unlock(&device_tree.rwlock);
+		async_answer_0(iid, ENOENT);
+		return;
+	}
 	handle = fun->handle;
+
 	fibril_rwlock_read_unlock(&device_tree.rwlock);
 
@@ -738,4 +768,15 @@
 	fibril_rwlock_read_lock(&device_tree.rwlock);
 
+	/* Check function state */
+	if (fun->state == FUN_REMOVED) {
+		fibril_rwlock_read_unlock(&device_tree.rwlock);
+		free(buffer);
+
+		async_answer_0(data_callid, ENOENT);
+		async_answer_0(iid, ENOENT);
+		fun_del_ref(fun);
+		return;
+	}
+
 	size_t sent_length = str_size(fun->name);
 	if (sent_length > data_len) {
@@ -781,4 +822,15 @@
 	fibril_rwlock_read_lock(&device_tree.rwlock);
 	
+	/* Check function state */
+	if (fun->state == FUN_REMOVED) {
+		fibril_rwlock_read_unlock(&device_tree.rwlock);
+		free(buffer);
+
+		async_answer_0(data_callid, ENOENT);
+		async_answer_0(iid, ENOENT);
+		fun_del_ref(fun);
+		return;
+	}
+	
 	size_t sent_length = str_size(fun->pathname);
 	if (sent_length > data_len) {
@@ -811,5 +863,5 @@
 	dev_node_t *dev = find_dev_node_no_lock(&device_tree,
 	    IPC_GET_ARG1(*icall));
-	if (dev == NULL) {
+	if (dev == NULL || dev->state == DEVICE_REMOVED) {
 		fibril_rwlock_read_unlock(&device_tree.rwlock);
 		async_answer_0(callid, ENOENT);
@@ -850,6 +902,6 @@
 	fibril_rwlock_read_lock(&device_tree.rwlock);
 	
-	fun = find_fun_node(&device_tree, IPC_GET_ARG1(*icall));
-	if (fun == NULL) {
+	fun = find_fun_node_no_lock(&device_tree, IPC_GET_ARG1(*icall));
+	if (fun == NULL || fun->state == FUN_REMOVED) {
 		fibril_rwlock_read_unlock(&device_tree.rwlock);
 		async_answer_0(iid, ENOENT);
@@ -858,5 +910,4 @@
 	
 	if (fun->child == NULL) {
-		fun_del_ref(fun);
 		fibril_rwlock_read_unlock(&device_tree.rwlock);
 		async_answer_0(iid, ENOENT);
@@ -866,5 +917,4 @@
 	async_answer_1(iid, EOK, fun->child->handle);
 	
-	fun_del_ref(fun);
 	fibril_rwlock_read_unlock(&device_tree.rwlock);
 }
@@ -931,4 +981,12 @@
 
 	fibril_rwlock_read_lock(&device_tree.rwlock);
+
+	/* Check function state */
+	if (fun->state == FUN_REMOVED) {
+		fibril_rwlock_read_unlock(&device_tree.rwlock);
+		async_answer_0(iid, ENOENT);
+		return;
+	}
+
 	async_answer_1(iid, EOK, fun->handle);
 	fibril_rwlock_read_unlock(&device_tree.rwlock);
@@ -1022,4 +1080,6 @@
 	driver_t *driver = NULL;
 	
+	fibril_rwlock_read_lock(&device_tree.rwlock);
+	
 	if (drv_to_parent) {
 		/* Connect to parent function of a device (or device function). */
@@ -1035,4 +1095,6 @@
 	}
 	
+	fibril_rwlock_read_unlock(&device_tree.rwlock);
+	
 	if (driver == NULL) {
 		log_msg(LVL_ERROR, "IPC forwarding refused - " \
@@ -1083,28 +1145,35 @@
 	fun_node_t *fun;
 	dev_node_t *dev;
+	devman_handle_t handle;
+	driver_t *driver;
 
 	fun = find_loc_tree_function(&device_tree, service_id);
 	
-	if (fun == NULL || fun->dev->drv == NULL) {
+	fibril_rwlock_read_lock(&device_tree.rwlock);
+	
+	if (fun == NULL || fun->dev == NULL || fun->dev->drv == NULL) {
 		log_msg(LVL_WARN, "devman_connection_loc(): function "
 		    "not found.\n");
-		async_answer_0(iid, ENOENT);
-		return;
-	}
-	
-	fibril_rwlock_read_lock(&device_tree.rwlock);
+		fibril_rwlock_read_unlock(&device_tree.rwlock);
+		async_answer_0(iid, ENOENT);
+		return;
+	}
+	
 	dev = fun->dev;
-	fun_del_ref(fun);
-	
-	async_exch_t *exch = async_exchange_begin(dev->drv->sess);
-	async_forward_fast(iid, exch, DRIVER_CLIENT, fun->handle, 0,
+	driver = dev->drv;
+	handle = fun->handle;
+	
+	fibril_rwlock_read_unlock(&device_tree.rwlock);
+	
+	async_exch_t *exch = async_exchange_begin(driver->sess);
+	async_forward_fast(iid, exch, DRIVER_CLIENT, handle, 0,
 	    IPC_FF_NONE);
 	async_exchange_end(exch);
 	
-	fibril_rwlock_read_unlock(&device_tree.rwlock);
-	
 	log_msg(LVL_DEBUG,
 	    "Forwarding loc service request for `%s' function to driver `%s'.",
-	    fun->pathname, dev->drv->name);
+	    fun->pathname, driver->name);
+
+	fun_del_ref(fun);
 }
 
