Index: uspace/srv/vfs/vfs_ops.c
===================================================================
--- uspace/srv/vfs/vfs_ops.c	(revision d9c8c81f21850cb9e08890ce3919e1d8e253e810)
+++ uspace/srv/vfs/vfs_ops.c	(revision 0e31a2bc6d1fecb2d1fd60c2eb47eed52130f540)
@@ -45,5 +45,5 @@
 #include <bool.h>
 #include <futex.h>
-#include <rwlock.h>
+#include <fibril_sync.h>
 #include <adt/list.h>
 #include <unistd.h>
@@ -73,5 +73,5 @@
  * concurrent VFS operation which modifies the file system namespace.
  */
-RWLOCK_INITIALIZE(namespace_rwlock);
+FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
 
 vfs_pair_t rootfs = {
@@ -96,11 +96,11 @@
 	
 	/* Resolve the path to the mountpoint. */
-	rwlock_write_lock(&namespace_rwlock);
+	fibril_rwlock_write_lock(&namespace_rwlock);
 	if (rootfs.fs_handle) {
 		/* We already have the root FS. */
 		if (str_cmp(mp, "/") == 0) {
 			/* Trying to mount root FS over root FS */
+			fibril_rwlock_write_unlock(&namespace_rwlock);
 			ipc_answer_0(rid, EBUSY);
-			rwlock_write_unlock(&namespace_rwlock);
 			return;
 		}
@@ -109,6 +109,6 @@
 		if (rc != EOK) {
 			/* The lookup failed for some reason. */
+			fibril_rwlock_write_unlock(&namespace_rwlock);
 			ipc_answer_0(rid, rc);
-			rwlock_write_unlock(&namespace_rwlock);
 			return;
 		}
@@ -116,6 +116,6 @@
 		mp_node = vfs_node_get(&mp_res);
 		if (!mp_node) {
+			fibril_rwlock_write_unlock(&namespace_rwlock);
 			ipc_answer_0(rid, ENOMEM);
-			rwlock_write_unlock(&namespace_rwlock);
 			return;
 		}
@@ -142,16 +142,16 @@
 			    str_size(opts));
 			if (rc != EOK) {
+				vfs_release_phone(phone);
 				async_wait_for(msg, NULL);
-				vfs_release_phone(phone);
+				fibril_rwlock_write_unlock(&namespace_rwlock);
 				ipc_answer_0(rid, rc);
-				rwlock_write_unlock(&namespace_rwlock);
 				return;
 			}
+			vfs_release_phone(phone);
 			async_wait_for(msg, &rc);
-			vfs_release_phone(phone);
 			
 			if (rc != EOK) {
+				fibril_rwlock_write_unlock(&namespace_rwlock);
 				ipc_answer_0(rid, rc);
-				rwlock_write_unlock(&namespace_rwlock);
 				return;
 			}
@@ -175,6 +175,6 @@
 			assert(mr_node);
 			
+			fibril_rwlock_write_unlock(&namespace_rwlock);
 			ipc_answer_0(rid, rc);
-			rwlock_write_unlock(&namespace_rwlock);
 			return;
 		} else {
@@ -183,6 +183,6 @@
 			 * being mounted first.
 			 */
+			fibril_rwlock_write_unlock(&namespace_rwlock);
 			ipc_answer_0(rid, ENOENT);
-			rwlock_write_unlock(&namespace_rwlock);
 			return;
 		}
@@ -208,11 +208,11 @@
 	rc = async_req_1_0(phone, IPC_M_CONNECTION_CLONE, mountee_phone);
 	if (rc != EOK) {
+		vfs_release_phone(phone);
 		async_wait_for(msg, NULL);
-		vfs_release_phone(phone);
 		/* Mount failed, drop reference to mp_node. */
 		if (mp_node)
 			vfs_node_put(mp_node);
 		ipc_answer_0(rid, rc);
-		rwlock_write_unlock(&namespace_rwlock);
+		fibril_rwlock_write_unlock(&namespace_rwlock);
 		return;
 	}
@@ -221,15 +221,15 @@
 	rc = ipc_data_write_start(phone, (void *)opts, str_size(opts));
 	if (rc != EOK) {
+		vfs_release_phone(phone);
 		async_wait_for(msg, NULL);
-		vfs_release_phone(phone);
 		/* Mount failed, drop reference to mp_node. */
 		if (mp_node)
 			vfs_node_put(mp_node);
-		ipc_answer_0(rid, rc);
-		rwlock_write_unlock(&namespace_rwlock);
-		return;
-	}
+		fibril_rwlock_write_unlock(&namespace_rwlock);
+		ipc_answer_0(rid, rc);
+		return;
+	}
+	vfs_release_phone(phone);
 	async_wait_for(msg, &rc);
-	vfs_release_phone(phone);
 	
 	if (rc == EOK) {
@@ -255,5 +255,5 @@
 
 	ipc_answer_0(rid, rc);
-	rwlock_write_unlock(&namespace_rwlock);
+	fibril_rwlock_write_unlock(&namespace_rwlock);
 }
 
@@ -509,7 +509,7 @@
 	 * L_DIRECTORY. Make sure that the user does not pass L_OPEN.
 	 */
-	if (((lflag & (L_FILE | L_DIRECTORY)) == 0)
-	    || ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY))
-	    || ((lflag & L_OPEN) != 0)) {
+	if (((lflag & (L_FILE | L_DIRECTORY)) == 0) ||
+	    ((lflag & (L_FILE | L_DIRECTORY)) == (L_FILE | L_DIRECTORY)) ||
+	    ((lflag & L_OPEN) != 0)) {
 		ipc_answer_0(rid, EINVAL);
 		return;
@@ -549,7 +549,7 @@
 	 */
 	if (lflag & L_CREATE)
-		rwlock_write_lock(&namespace_rwlock);
+		fibril_rwlock_write_lock(&namespace_rwlock);
 	else
-		rwlock_read_lock(&namespace_rwlock);
+		fibril_rwlock_read_lock(&namespace_rwlock);
 	
 	/* The path is now populated and we can call vfs_lookup_internal(). */
@@ -558,7 +558,7 @@
 	if (rc != EOK) {
 		if (lflag & L_CREATE)
-			rwlock_write_unlock(&namespace_rwlock);
+			fibril_rwlock_write_unlock(&namespace_rwlock);
 		else
-			rwlock_read_unlock(&namespace_rwlock);
+			fibril_rwlock_read_unlock(&namespace_rwlock);
 		ipc_answer_0(rid, rc);
 		free(path);
@@ -571,16 +571,16 @@
 	vfs_node_t *node = vfs_node_get(&lr);
 	if (lflag & L_CREATE)
-		rwlock_write_unlock(&namespace_rwlock);
+		fibril_rwlock_write_unlock(&namespace_rwlock);
 	else
-		rwlock_read_unlock(&namespace_rwlock);
+		fibril_rwlock_read_unlock(&namespace_rwlock);
 	
 	/* Truncate the file if requested and if necessary. */
 	if (oflag & O_TRUNC) {
-		rwlock_write_lock(&node->contents_rwlock);
+		fibril_rwlock_write_lock(&node->contents_rwlock);
 		if (node->size) {
 			rc = vfs_truncate_internal(node->fs_handle,
 			    node->dev_handle, node->index, 0);
 			if (rc) {
-				rwlock_write_unlock(&node->contents_rwlock);
+				fibril_rwlock_write_unlock(&node->contents_rwlock);
 				vfs_node_put(node);
 				ipc_answer_0(rid, rc);
@@ -589,5 +589,5 @@
 			node->size = 0;
 		}
-		rwlock_write_unlock(&node->contents_rwlock);
+		fibril_rwlock_write_unlock(&node->contents_rwlock);
 	}
 	
@@ -640,9 +640,9 @@
 	int oflag = IPC_GET_ARG4(*request);
 	
-	rwlock_read_lock(&namespace_rwlock);
+	fibril_rwlock_read_lock(&namespace_rwlock);
 	
 	int rc = vfs_open_node_internal(&lr);
 	if (rc != EOK) {
-		rwlock_read_unlock(&namespace_rwlock);
+		fibril_rwlock_read_unlock(&namespace_rwlock);
 		ipc_answer_0(rid, rc);
 		return;
@@ -650,14 +650,14 @@
 	
 	vfs_node_t *node = vfs_node_get(&lr);
-	rwlock_read_unlock(&namespace_rwlock);
+	fibril_rwlock_read_unlock(&namespace_rwlock);
 	
 	/* Truncate the file if requested and if necessary. */
 	if (oflag & O_TRUNC) {
-		rwlock_write_lock(&node->contents_rwlock);
+		fibril_rwlock_write_lock(&node->contents_rwlock);
 		if (node->size) {
 			rc = vfs_truncate_internal(node->fs_handle,
 			    node->dev_handle, node->index, 0);
 			if (rc) {
-				rwlock_write_unlock(&node->contents_rwlock);
+				fibril_rwlock_write_unlock(&node->contents_rwlock);
 				vfs_node_put(node);
 				ipc_answer_0(rid, rc);
@@ -666,5 +666,5 @@
 			node->size = 0;
 		}
-		rwlock_write_unlock(&node->contents_rwlock);
+		fibril_rwlock_write_unlock(&node->contents_rwlock);
 	}
 	
@@ -709,6 +709,6 @@
 	}
 	
-	ipc_answer_3(rid, EOK, file->node->fs_handle,
-	    file->node->dev_handle, file->node->index);
+	ipc_answer_3(rid, EOK, file->node->fs_handle, file->node->dev_handle,
+	    file->node->index);
 }
 
@@ -728,5 +728,5 @@
 	 * the same open file at a time.
 	 */
-	futex_down(&file->lock);
+	fibril_mutex_lock(&file->lock);
 	int fs_phone = vfs_grab_phone(file->node->fs_handle);
 	
@@ -737,10 +737,11 @@
 	    file->node->dev_handle, file->node->index, &answer);
 	
+	vfs_release_phone(fs_phone);
+
 	/* Wait for reply from the FS server. */
 	ipcarg_t rc;
 	async_wait_for(msg, &rc);
 	
-	vfs_release_phone(fs_phone);
-	futex_up(&file->lock);
+	fibril_mutex_unlock(&file->lock);
 	
 	ipc_answer_1(rid, EOK, IPC_GET_ARG1(answer));
@@ -762,5 +763,5 @@
 	 * the same open file at a time.
 	 */
-	futex_down(&file->lock);
+	fibril_mutex_lock(&file->lock);
 	int fs_phone = vfs_grab_phone(file->node->fs_handle);
 	
@@ -770,11 +771,12 @@
 	msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
 	    file->node->dev_handle, file->node->index, &answer);
-	
+
+	vfs_release_phone(fs_phone);
+
 	/* Wait for reply from the FS server. */
 	ipcarg_t rc;
 	async_wait_for(msg, &rc);
 	
-	vfs_release_phone(fs_phone);
-	futex_up(&file->lock);
+	fibril_mutex_unlock(&file->lock);
 	
 	ipc_answer_0(rid, rc);
@@ -796,5 +798,5 @@
 	 * the same open file at a time.
 	 */
-	futex_down(&file->lock);
+	fibril_mutex_lock(&file->lock);
 	
 	int fs_phone = vfs_grab_phone(file->node->fs_handle);
@@ -805,4 +807,6 @@
 	msg = async_send_2(fs_phone, IPC_GET_METHOD(*request),
 	    file->node->dev_handle, file->node->index, &answer);
+
+	vfs_release_phone(fs_phone);
 	
 	/* Wait for reply from the FS server. */
@@ -810,6 +814,5 @@
 	async_wait_for(msg, &rc);
 	
-	vfs_release_phone(fs_phone);
-	futex_up(&file->lock);
+	fibril_mutex_unlock(&file->lock);
 	
 	int retval = IPC_GET_ARG1(answer);
@@ -863,5 +866,5 @@
 	 * the same open file at a time.
 	 */
-	futex_down(&file->lock);
+	fibril_mutex_lock(&file->lock);
 
 	/*
@@ -870,7 +873,7 @@
 	 */
 	if (read)
-		rwlock_read_lock(&file->node->contents_rwlock);
+		fibril_rwlock_read_lock(&file->node->contents_rwlock);
 	else
-		rwlock_write_lock(&file->node->contents_rwlock);
+		fibril_rwlock_write_lock(&file->node->contents_rwlock);
 
 	if (file->node->type == VFS_NODE_DIRECTORY) {
@@ -880,5 +883,5 @@
 		 */
 		assert(read);
-		rwlock_read_lock(&namespace_rwlock);
+		fibril_rwlock_read_lock(&namespace_rwlock);
 	}
 	
@@ -900,4 +903,6 @@
 	 */
 	ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
+
+	vfs_release_phone(fs_phone);
 	
 	/* Wait for reply from the FS server. */
@@ -905,19 +910,17 @@
 	async_wait_for(msg, &rc);
 	
-	vfs_release_phone(fs_phone);
-	
 	size_t bytes = IPC_GET_ARG1(answer);
 
 	if (file->node->type == VFS_NODE_DIRECTORY)
-		rwlock_read_unlock(&namespace_rwlock);
+		fibril_rwlock_read_unlock(&namespace_rwlock);
 	
 	/* Unlock the VFS node. */
 	if (read)
-		rwlock_read_unlock(&file->node->contents_rwlock);
+		fibril_rwlock_read_unlock(&file->node->contents_rwlock);
 	else {
 		/* Update the cached version of node's size. */
 		if (rc == EOK)
 			file->node->size = IPC_GET_ARG2(answer); 
-		rwlock_write_unlock(&file->node->contents_rwlock);
+		fibril_rwlock_write_unlock(&file->node->contents_rwlock);
 	}
 	
@@ -925,5 +928,5 @@
 	if (rc == EOK)
 		file->pos += bytes;
-	futex_up(&file->lock);
+	fibril_mutex_unlock(&file->lock);
 	
 	/*
@@ -959,8 +962,8 @@
 
 	off_t newpos;
-	futex_down(&file->lock);
+	fibril_mutex_lock(&file->lock);
 	if (whence == SEEK_SET) {
 		file->pos = off;
-		futex_up(&file->lock);
+		fibril_mutex_unlock(&file->lock);
 		ipc_answer_1(rid, EOK, off);
 		return;
@@ -968,5 +971,5 @@
 	if (whence == SEEK_CUR) {
 		if (file->pos + off < file->pos) {
-			futex_up(&file->lock);
+			fibril_mutex_unlock(&file->lock);
 			ipc_answer_0(rid, EOVERFLOW);
 			return;
@@ -974,23 +977,23 @@
 		file->pos += off;
 		newpos = file->pos;
-		futex_up(&file->lock);
+		fibril_mutex_unlock(&file->lock);
 		ipc_answer_1(rid, EOK, newpos);
 		return;
 	}
 	if (whence == SEEK_END) {
-		rwlock_read_lock(&file->node->contents_rwlock);
+		fibril_rwlock_read_lock(&file->node->contents_rwlock);
 		size_t size = file->node->size;
-		rwlock_read_unlock(&file->node->contents_rwlock);
+		fibril_rwlock_read_unlock(&file->node->contents_rwlock);
 		if (size + off < size) {
-			futex_up(&file->lock);
+			fibril_mutex_unlock(&file->lock);
 			ipc_answer_0(rid, EOVERFLOW);
 			return;
 		}
 		newpos = size + off;
-		futex_up(&file->lock);
+		fibril_mutex_unlock(&file->lock);
 		ipc_answer_1(rid, EOK, newpos);
 		return;
 	}
-	futex_up(&file->lock);
+	fibril_mutex_unlock(&file->lock);
 	ipc_answer_0(rid, EINVAL);
 }
@@ -1021,14 +1024,14 @@
 		return;
 	}
-	futex_down(&file->lock);
-
-	rwlock_write_lock(&file->node->contents_rwlock);
+	fibril_mutex_lock(&file->lock);
+
+	fibril_rwlock_write_lock(&file->node->contents_rwlock);
 	rc = vfs_truncate_internal(file->node->fs_handle,
 	    file->node->dev_handle, file->node->index, size);
 	if (rc == EOK)
 		file->node->size = size;
-	rwlock_write_unlock(&file->node->contents_rwlock);
-
-	futex_up(&file->lock);
+	fibril_rwlock_write_unlock(&file->node->contents_rwlock);
+
+	fibril_mutex_unlock(&file->lock);
 	ipc_answer_0(rid, (ipcarg_t)rc);
 }
@@ -1060,8 +1063,8 @@
 	path[len] = '\0';
 	
-	rwlock_write_lock(&namespace_rwlock);
+	fibril_rwlock_write_lock(&namespace_rwlock);
 	int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
 	rc = vfs_lookup_internal(path, lflag, NULL, NULL);
-	rwlock_write_unlock(&namespace_rwlock);
+	fibril_rwlock_write_unlock(&namespace_rwlock);
 	free(path);
 	ipc_answer_0(rid, rc);
@@ -1094,5 +1097,5 @@
 	path[len] = '\0';
 	
-	rwlock_write_lock(&namespace_rwlock);
+	fibril_rwlock_write_lock(&namespace_rwlock);
 	lflag &= L_DIRECTORY;	/* sanitize lflag */
 	vfs_lookup_res_t lr;
@@ -1100,5 +1103,5 @@
 	free(path);
 	if (rc != EOK) {
-		rwlock_write_unlock(&namespace_rwlock);
+		fibril_rwlock_write_unlock(&namespace_rwlock);
 		ipc_answer_0(rid, rc);
 		return;
@@ -1114,5 +1117,5 @@
 	node->lnkcnt--;
 	futex_up(&nodes_futex);
-	rwlock_write_unlock(&namespace_rwlock);
+	fibril_rwlock_write_unlock(&namespace_rwlock);
 	vfs_node_put(node);
 	ipc_answer_0(rid, EOK);
@@ -1195,9 +1198,9 @@
 	vfs_lookup_res_t new_lr;
 	vfs_lookup_res_t new_par_lr;
-	rwlock_write_lock(&namespace_rwlock);
+	fibril_rwlock_write_lock(&namespace_rwlock);
 	/* Lookup the node belonging to the old file name. */
 	rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
 	if (rc != EOK) {
-		rwlock_write_unlock(&namespace_rwlock);
+		fibril_rwlock_write_unlock(&namespace_rwlock);
 		ipc_answer_0(rid, rc);
 		free(old);
@@ -1207,5 +1210,5 @@
 	vfs_node_t *old_node = vfs_node_get(&old_lr);
 	if (!old_node) {
-		rwlock_write_unlock(&namespace_rwlock);
+		fibril_rwlock_write_unlock(&namespace_rwlock);
 		ipc_answer_0(rid, ENOMEM);
 		free(old);
@@ -1216,5 +1219,5 @@
 	char *parentc = str_dup(newc);
 	if (!parentc) {
-		rwlock_write_unlock(&namespace_rwlock);
+		fibril_rwlock_write_unlock(&namespace_rwlock);
 		ipc_answer_0(rid, rc);
 		free(old);
@@ -1231,5 +1234,5 @@
 	free(parentc);	/* not needed anymore */
 	if (rc != EOK) {
-		rwlock_write_unlock(&namespace_rwlock);
+		fibril_rwlock_write_unlock(&namespace_rwlock);
 		ipc_answer_0(rid, rc);
 		free(old);
@@ -1240,5 +1243,5 @@
 	if ((old_node->fs_handle != new_par_lr.triplet.fs_handle) ||
 	    (old_node->dev_handle != new_par_lr.triplet.dev_handle)) {
-		rwlock_write_unlock(&namespace_rwlock);
+		fibril_rwlock_write_unlock(&namespace_rwlock);
 		ipc_answer_0(rid, EXDEV);	/* different file systems */
 		free(old);
@@ -1256,5 +1259,5 @@
 		new_node = vfs_node_get(&new_lr);
 		if (!new_node) {
-			rwlock_write_unlock(&namespace_rwlock);
+			fibril_rwlock_write_unlock(&namespace_rwlock);
 			ipc_answer_0(rid, ENOMEM);
 			free(old);
@@ -1267,5 +1270,5 @@
 		break;
 	default:
-		rwlock_write_unlock(&namespace_rwlock);
+		fibril_rwlock_write_unlock(&namespace_rwlock);
 		ipc_answer_0(rid, ENOTEMPTY);
 		free(old);
@@ -1276,5 +1279,5 @@
 	rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
 	if (rc != EOK) {
-		rwlock_write_unlock(&namespace_rwlock);
+		fibril_rwlock_write_unlock(&namespace_rwlock);
 		if (new_node)
 			vfs_node_put(new_node);
@@ -1290,5 +1293,5 @@
 	rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
 	if (rc != EOK) {
-		rwlock_write_unlock(&namespace_rwlock);
+		fibril_rwlock_write_unlock(&namespace_rwlock);
 		vfs_node_put(old_node);
 		if (new_node)
@@ -1302,5 +1305,5 @@
 	old_node->lnkcnt--;
 	futex_up(&nodes_futex);
-	rwlock_write_unlock(&namespace_rwlock);
+	fibril_rwlock_write_unlock(&namespace_rwlock);
 	vfs_node_put(old_node);
 	if (new_node)
