Index: uspace/srv/vfs/vfs_ops.c
===================================================================
--- uspace/srv/vfs/vfs_ops.c	(revision 5126f801cf167ce675ecc93b5079c64358dcbacb)
+++ uspace/srv/vfs/vfs_ops.c	(revision 0d35511b07352cce59e443fae082937725227cae)
@@ -68,6 +68,119 @@
 FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
 
-static int vfs_connect_internal(service_id_t service_id, unsigned flags, unsigned instance,
-    char *options, char *fsname, vfs_node_t **root)
+static size_t shared_path(char *a, char *b)
+{
+	size_t res = 0;
+	
+	while (a[res] == b[res] && a[res] != 0) {
+		res++;
+	}
+	
+	if (a[res] == b[res]) {
+		return res;
+	}
+	
+	res--;
+	while (a[res] != '/') {
+		res--;
+	}
+	return res;
+}
+
+/* This call destroys the file if and only if there are no hard links left. */
+static void out_destroy(vfs_triplet_t *file)
+{
+	async_exch_t *exch = vfs_exchange_grab(file->fs_handle);
+	async_msg_2(exch, VFS_OUT_DESTROY,
+		(sysarg_t) file->service_id, (sysarg_t) file->index);
+	vfs_exchange_release(exch);
+}
+
+int vfs_op_clone(int oldfd, bool desc)
+{
+	/* Lookup the file structure corresponding to fd. */
+	vfs_file_t *oldfile = vfs_file_get(oldfd);
+	if (oldfile == NULL) {
+		return EBADF;
+	}
+	assert(oldfile->node != NULL);
+	
+	vfs_file_t *newfile;
+	int newfd = vfs_fd_alloc(&newfile, desc);
+	if (newfd >= 0) {
+		newfile->node = oldfile->node;
+		newfile->permissions = oldfile->permissions;
+		vfs_node_addref(newfile->node);
+	
+		vfs_file_put(newfile);
+	}
+	vfs_file_put(oldfile);
+	
+	return newfd;
+}
+
+int vfs_op_close(int fd)
+{
+	return vfs_fd_free(fd);
+}
+
+int vfs_op_dup(int oldfd, int newfd)
+{
+	/* If the file descriptors are the same, do nothing. */
+	if (oldfd == newfd) {
+		return EOK;
+	}
+	
+	/* Lookup the file structure corresponding to oldfd. */
+	vfs_file_t *oldfile = vfs_file_get(oldfd);
+	if (!oldfile) {
+		return EBADF;
+	}
+	
+	/* Make sure newfd is closed. */
+	(void) vfs_fd_free(newfd);
+	
+	/* Assign the old file to newfd. */
+	int ret = vfs_fd_assign(oldfile, newfd);
+	vfs_file_put(oldfile);
+	
+	return ret;
+}
+
+int vfs_op_fstat_forward(int fd)
+{
+	vfs_file_t *file = vfs_file_get(fd);
+	if (!file) {
+		return EBADF;
+	}
+	assert(file->node);
+	
+	ipc_callid_t callid;
+	if (!async_data_read_receive(&callid, NULL)) {
+		vfs_file_put(file);
+		async_answer_0(callid, EINVAL);
+		return EINVAL;
+	}
+
+	async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
+	assert(exch);
+	
+	aid_t msg;
+	msg = async_send_3(exch, VFS_OUT_STAT, file->node->service_id,
+	    file->node->index, true, NULL);
+	assert(msg);
+	async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
+	
+	vfs_exchange_release(exch);
+	
+	sysarg_t rc;
+	async_wait_for(msg, &rc);
+	
+	vfs_file_put(file);
+	return rc;
+}
+
+static int vfs_connect_internal(service_id_t service_id, unsigned flags,
+    unsigned instance, const char *options, const char *fsname,
+    vfs_node_t **root)
 {
 	fs_handle_t fs_handle = 0;
@@ -92,5 +205,6 @@
 	ipc_call_t answer;
 	async_exch_t *exch = vfs_exchange_grab(fs_handle);
-	aid_t msg = async_send_1(exch, VFS_OUT_MOUNTED, (sysarg_t) service_id, &answer);
+	aid_t msg = async_send_1(exch, VFS_OUT_MOUNTED, (sysarg_t) service_id,
+	    &answer);
 	/* Send the mount options */
 	sysarg_t rc = async_data_write_start(exch, options, str_size(options));
@@ -111,5 +225,6 @@
 	res.triplet.service_id = service_id;
 	res.triplet.index = (fs_index_t) IPC_GET_ARG1(answer);
-	res.size = (int64_t) MERGE_LOUP32(IPC_GET_ARG2(answer), IPC_GET_ARG3(answer));
+	res.size = (int64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
+	    IPC_GET_ARG3(answer));
 	res.type = VFS_NODE_DIRECTORY;
 	
@@ -121,49 +236,12 @@
 }
 
-void vfs_mount_srv(ipc_callid_t rid, ipc_call_t *request)
-{
-	int mpfd = IPC_GET_ARG1(*request);
-	
-	/*
-	 * We expect the library to do the device-name to device-handle
-	 * translation for us, thus the device handle will arrive as ARG1
-	 * in the request.
-	 */
-	service_id_t service_id = (service_id_t) IPC_GET_ARG2(*request);
-	
-	/*
-	 * Mount flags are passed as ARG2.
-	 */
-	unsigned int flags = (unsigned int) IPC_GET_ARG3(*request);
-	
-	/*
-	 * Instance number is passed as ARG3.
-	 */
-	unsigned int instance = IPC_GET_ARG4(*request);
-	
-	char *opts = NULL;
-	char *fs_name = NULL;
+int vfs_op_mount(int mpfd, unsigned service_id, unsigned flags,
+    unsigned instance, const char *opts, const char *fs_name, int *outfd)
+{
+	int rc;
 	vfs_file_t *mp = NULL;
 	vfs_file_t *file = NULL;
 	int fd = -1;
 	mtab_ent_t *mtab_ent = NULL;
-
-	/* Now we expect to receive the mount options. */
-	int rc = async_data_write_accept((void **) &opts, true, 0, MAX_MNTOPTS_LEN,
-	    0, NULL);
-	if (rc != EOK) {
-		async_data_write_void(rc);
-		goto out;
-	}
-	
-	/*
-	 * Now, we expect the client to send us data with the name of the file
-	 * system.
-	 */
-	rc = async_data_write_accept((void **) &fs_name, true, 0,
-	    FS_NAME_MAXLEN, 0, NULL);
-	if (rc != EOK) {
-		goto out;
-	}
 	
 	if (!(flags & VFS_MOUNT_CONNECT_ONLY)) {
@@ -209,5 +287,6 @@
 	fibril_rwlock_write_lock(&namespace_rwlock);
 
-	rc = vfs_connect_internal(service_id, flags, instance, opts, fs_name, &root);
+	rc = vfs_connect_internal(service_id, flags, instance, opts, fs_name,
+	     &root);
 	if (rc == EOK && !(flags & VFS_MOUNT_CONNECT_ONLY)) {
 		vfs_node_addref(mp->node);
@@ -235,30 +314,18 @@
 	
 	/* Add the filesystem info to the list of mounted filesystems */
-	if (rc == EOK) {
-		str_cpy(mtab_ent->mp, MAX_PATH_LEN, "fixme");
-		str_cpy(mtab_ent->fs_name, FS_NAME_MAXLEN, fs_name);
-		str_cpy(mtab_ent->opts, MAX_MNTOPTS_LEN, opts);
-		mtab_ent->instance = instance;
-		mtab_ent->service_id = service_id;
-
-		link_initialize(&mtab_ent->link);
-
-		fibril_mutex_lock(&mtab_list_lock);
-		list_append(&mtab_ent->link, &mtab_list);
-		mtab_size++;
-		fibril_mutex_unlock(&mtab_list_lock);
-	}	
-	
-	rc = EOK;
+	str_cpy(mtab_ent->mp, MAX_PATH_LEN, "fixme");
+	str_cpy(mtab_ent->fs_name, FS_NAME_MAXLEN, fs_name);
+	str_cpy(mtab_ent->opts, MAX_MNTOPTS_LEN, opts);
+	mtab_ent->instance = instance;
+	mtab_ent->service_id = service_id;
+
+	link_initialize(&mtab_ent->link);
+
+	fibril_mutex_lock(&mtab_list_lock);
+	list_append(&mtab_ent->link, &mtab_list);
+	mtab_size++;
+	fibril_mutex_unlock(&mtab_list_lock);	
 
 out:
-	async_answer_1(rid, rc, rc == EOK ? fd : 0);
-
-	if (opts) {
-		free(opts);
-	}
-	if (fs_name) {
-		free(fs_name);
-	}
 	if (mp) {
 		vfs_file_put(mp);
@@ -269,209 +336,92 @@
 	if (rc != EOK && fd >= 0) {
 		vfs_fd_free(fd);
-	}
-}
-
-void vfs_unmount_srv(ipc_callid_t rid, ipc_call_t *request)
-{
-	int mpfd = IPC_GET_ARG1(*request);
-	
-	vfs_file_t *mp = vfs_file_get(mpfd);
-	if (mp == NULL) {
-		async_answer_0(rid, EBADF);
-		return;
-	}
-	
-	if (mp->node->mount == NULL) {
-		async_answer_0(rid, ENOENT);
-		vfs_file_put(mp);
-		return;
-	}
-	
-	fibril_rwlock_write_lock(&namespace_rwlock);
-	
-	/*
-	 * Count the total number of references for the mounted file system. We
-	 * are expecting at least one, which is held by the mount point.
-	 * If we find more, it means that
-	 * the file system cannot be gracefully unmounted at the moment because
-	 * someone is working with it.
-	 */
-	if (vfs_nodes_refcount_sum_get(mp->node->mount->fs_handle, mp->node->mount->service_id) != 1) {
-		async_answer_0(rid, EBUSY);
-		vfs_file_put(mp);
-		fibril_rwlock_write_unlock(&namespace_rwlock);
-		return;
-	}
-	
-	async_exch_t *exch = vfs_exchange_grab(mp->node->mount->fs_handle);
-	int rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED, mp->node->mount->service_id);
-	vfs_exchange_release(exch);
-	
-	if (rc != EOK) {
-		async_answer_0(rid, rc);
-		vfs_file_put(mp);
-		fibril_rwlock_write_unlock(&namespace_rwlock);
-		return;
-	}
-	
-	vfs_node_forget(mp->node->mount);
-	vfs_node_put(mp->node);
-	mp->node->mount = NULL;
-	
-	fibril_rwlock_write_unlock(&namespace_rwlock);
-	
+		fd = 0;
+	}
+	
+	*outfd = fd;
+	return rc;
+}
+
+int vfs_op_mtab_get(void)
+{
+	ipc_callid_t callid;
+	ipc_call_t data;
+	sysarg_t rc = EOK;
+	size_t len;
+
 	fibril_mutex_lock(&mtab_list_lock);
-	int found = 0;
+
+	/* Send to the caller the number of mounted filesystems */
+	callid = async_get_call(&data);
+	if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
+		rc = ENOTSUP;
+		async_answer_0(callid, rc);
+		goto exit;
+	}
+	async_answer_1(callid, EOK, mtab_size);
 
 	list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
-		// FIXME: mp name
-		if (str_cmp(mtab_ent->mp, "fixme") == 0) {
-			list_remove(&mtab_ent->link);
-			mtab_size--;
-			free(mtab_ent);
-			found = 1;
-			break;
-		}
-	}
-	assert(found);
+		rc = ENOTSUP;
+
+		if (!async_data_read_receive(&callid, &len)) {
+			async_answer_0(callid, rc);
+			goto exit;
+		}
+
+		(void) async_data_read_finalize(callid, mtab_ent->mp,
+		    str_size(mtab_ent->mp));
+
+		if (!async_data_read_receive(&callid, &len)) {
+			async_answer_0(callid, rc);
+			goto exit;
+		}
+
+		(void) async_data_read_finalize(callid, mtab_ent->opts,
+		    str_size(mtab_ent->opts));
+
+		if (!async_data_read_receive(&callid, &len)) {
+			async_answer_0(callid, rc);
+			goto exit;
+		}
+
+		(void) async_data_read_finalize(callid, mtab_ent->fs_name,
+		    str_size(mtab_ent->fs_name));
+
+		callid = async_get_call(&data);
+
+		if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
+			async_answer_0(callid, rc);
+			goto exit;
+		}
+
+		rc = EOK;
+		async_answer_2(callid, rc, mtab_ent->instance,
+		    mtab_ent->service_id);
+	}
+
+exit:
 	fibril_mutex_unlock(&mtab_list_lock);
-	
-	vfs_file_put(mp);
-	async_answer_0(rid, EOK);
-}
-
-static inline bool walk_flags_valid(int flags)
-{
-	if ((flags&~WALK_ALL_FLAGS) != 0) {
-		return false;
-	}
-	if ((flags&WALK_MAY_CREATE) && (flags&WALK_MUST_CREATE)) {
-		return false;
-	}
-	if ((flags&WALK_REGULAR) && (flags&WALK_DIRECTORY)) {
-		return false;
-	}
-	if ((flags&WALK_MAY_CREATE) || (flags&WALK_MUST_CREATE)) {
-		if (!(flags&WALK_DIRECTORY) && !(flags&WALK_REGULAR)) {
-			return false;
-		}
-	}
-	return true;
-}
-
-static inline int walk_lookup_flags(int flags)
-{
-	int lflags = 0;
-	if (flags&WALK_MAY_CREATE || flags&WALK_MUST_CREATE) {
-		lflags |= L_CREATE;
-	}
-	if (flags&WALK_MUST_CREATE) {
-		lflags |= L_EXCLUSIVE;
-	}
-	if (flags&WALK_REGULAR) {
-		lflags |= L_FILE;
-	}
-	if (flags&WALK_DIRECTORY) {
-		lflags |= L_DIRECTORY;
-	}
-	if (flags&WALK_MOUNT_POINT) {
-		lflags |= L_MP;
-	}
-	return lflags;
-}
-
-void vfs_walk(ipc_callid_t rid, ipc_call_t *request)
-{
-	/*
-	 * Parent is our relative root for file lookup.
-	 * For defined flags, see <ipc/vfs.h>.
-	 */
-	int parentfd = IPC_GET_ARG1(*request);
-	int flags = IPC_GET_ARG2(*request);
-	
-	if (!walk_flags_valid(flags)) {
-		async_answer_0(rid, EINVAL);
-		return;
-	}
-	
-	char *path;
-	int rc = async_data_write_accept((void **)&path, true, 0, 0, 0, NULL);
-	
-	/* Lookup the file structure corresponding to the file descriptor. */
-	vfs_file_t *parent = vfs_file_get(parentfd);
-	if (!parent) {
-		free(path);
-		async_answer_0(rid, EBADF);
-		return;
-	}
-	
-	fibril_rwlock_read_lock(&namespace_rwlock);
-	
-	vfs_lookup_res_t lr;
-	rc = vfs_lookup_internal(parent->node, path, walk_lookup_flags(flags), &lr);
-	free(path);
-
-	if (rc != EOK) {
-		fibril_rwlock_read_unlock(&namespace_rwlock);
-		if (parent) {
-			vfs_file_put(parent);
-		}
-		async_answer_0(rid, rc);
-		return;
-	}
-	
-	vfs_node_t *node = vfs_node_get(&lr);
-	
-	vfs_file_t *file;
-	int fd = vfs_fd_alloc(&file, false);
-	if (fd < 0) {
-		vfs_node_put(node);
-		if (parent) {
-			vfs_file_put(parent);
-		}
-		async_answer_0(rid, fd);
-		return;
-	}
-	assert(file != NULL);
-	
-	file->node = node;
-	if (parent) {
-		file->permissions = parent->permissions;
-	} else {
-		file->permissions = MODE_READ | MODE_WRITE | MODE_APPEND;
-	}
-	file->open_read = false;
-	file->open_write = false;
-	
-	vfs_file_put(file);
-	if (parent) {
-		vfs_file_put(parent);
-	}
-	
-	fibril_rwlock_read_unlock(&namespace_rwlock);
-
-	async_answer_1(rid, EOK, fd);
-}
-
-void vfs_open2(ipc_callid_t rid, ipc_call_t *request)
-{
-	int fd = IPC_GET_ARG1(*request);
-	int flags = IPC_GET_ARG2(*request);
-
+	return rc;
+}
+
+int vfs_op_open2(int fd, int flags)
+{
 	if (flags == 0) {
-		async_answer_0(rid, EINVAL);
-		return;
+		return EINVAL;
 	}
 
 	vfs_file_t *file = vfs_file_get(fd);
 	if (!file) {
-		async_answer_0(rid, EBADF);
-		return;
+		return EBADF;
 	}
 	
 	if ((flags & ~file->permissions) != 0) {
 		vfs_file_put(file);
-		async_answer_0(rid, EPERM);
-		return;
+		return EPERM;
+	}
+	
+	if (file->open_read || file->open_write) {
+		vfs_file_put(file);
+		return EBUSY;
 	}
 	
@@ -482,6 +432,5 @@
 	if (!file->open_read && !file->open_write) {
 		vfs_file_put(file);
-		async_answer_0(rid, EINVAL);
-		return;
+		return EINVAL;
 	}
 	
@@ -489,6 +438,5 @@
 		file->open_read = file->open_write = false;
 		vfs_file_put(file);
-		async_answer_0(rid, EINVAL);
-		return;
+		return EINVAL;
 	}
 	
@@ -497,50 +445,9 @@
 		file->open_read = file->open_write = false;
 		vfs_file_put(file);
-		async_answer_0(rid, rc);
-		return;
+		return rc;
 	}
 	
 	vfs_file_put(file);
-	async_answer_0(rid, EOK);
-}
-
-void vfs_sync(ipc_callid_t rid, ipc_call_t *request)
-{
-	int fd = IPC_GET_ARG1(*request);
-	
-	/* Lookup the file structure corresponding to the file descriptor. */
-	vfs_file_t *file = vfs_file_get(fd);
-	if (!file) {
-		async_answer_0(rid, ENOENT);
-		return;
-	}
-	
-	/*
-	 * Lock the open file structure so that no other thread can manipulate
-	 * the same open file at a time.
-	 */
-	async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
-	
-	/* Make a VFS_OUT_SYMC request at the destination FS server. */
-	aid_t msg;
-	ipc_call_t answer;
-	msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
-	    file->node->index, &answer);
-	
-	vfs_exchange_release(fs_exch);
-	
-	/* Wait for reply from the FS server. */
-	sysarg_t rc;
-	async_wait_for(msg, &rc);
-	
-	vfs_file_put(file);
-	async_answer_0(rid, rc);
-}
-
-void vfs_close(ipc_callid_t rid, ipc_call_t *request)
-{
-	int fd = IPC_GET_ARG1(*request);
-	int ret = vfs_fd_free(fd);
-	async_answer_0(rid, ret);
+	return EOK;
 }
 
@@ -629,5 +536,6 @@
 	assert(fs_info);
 	
-	bool rlock = read || ((fs_info->concurrent_read_write) && (fs_info->write_retains_size));
+	bool rlock = read ||
+	    (fs_info->concurrent_read_write && fs_info->write_retains_size);
 	
 	/*
@@ -650,7 +558,9 @@
 		if (!read) {
 			if (rlock) {
-				fibril_rwlock_read_unlock(&file->node->contents_rwlock);
+				fibril_rwlock_read_unlock(
+				    &file->node->contents_rwlock);
 			} else {
-				fibril_rwlock_write_unlock(&file->node->contents_rwlock);
+				fibril_rwlock_write_unlock(
+				    &file->node->contents_rwlock);
 			}
 			vfs_file_put(file);
@@ -700,12 +610,4 @@
 	return rc;
 }
-	
-static void vfs_rdwr_client(ipc_callid_t rid, ipc_call_t *request, bool read)
-{
-	size_t bytes = 0;	
-	int rc = vfs_rdwr(IPC_GET_ARG1(*request), read, rdwr_ipc_client,
-	    &bytes);
-	async_answer_1(rid, rc, bytes);
-}
 
 int vfs_rdwr_internal(int fd, bool read, rdwr_io_chunk_t *chunk)
@@ -714,288 +616,18 @@
 }
 
-void vfs_read(ipc_callid_t rid, ipc_call_t *request)
-{
-	vfs_rdwr_client(rid, request, true);
-}
-
-void vfs_write(ipc_callid_t rid, ipc_call_t *request)
-{
-	vfs_rdwr_client(rid, request, false);
-}
-
-void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
-{
-	int fd = (int) IPC_GET_ARG1(*request);
-	off64_t off = (off64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
-	    IPC_GET_ARG3(*request));
-	int whence = (int) IPC_GET_ARG4(*request);
-	
-	/* Lookup the file structure corresponding to the file descriptor. */
-	vfs_file_t *file = vfs_file_get(fd);
-	if (!file) {
-		async_answer_0(rid, ENOENT);
-		return;
-	}
-	
-	off64_t newoff;
-	switch (whence) {
-	case SEEK_SET:
-		if (off >= 0) {
-			file->pos = (aoff64_t) off;
-			vfs_file_put(file);
-			async_answer_1(rid, EOK, off);
-			return;
-		}
-		break;
-	case SEEK_CUR:
-		if ((off >= 0) && (file->pos + off < file->pos)) {
-			vfs_file_put(file);
-			async_answer_0(rid, EOVERFLOW);
-			return;
-		}
-		
-		if ((off < 0) && (file->pos < (aoff64_t) -off)) {
-			vfs_file_put(file);
-			async_answer_0(rid, EOVERFLOW);
-			return;
-		}
-		
-		file->pos += off;
-		newoff = (file->pos > OFF64_MAX) ? OFF64_MAX : file->pos;
-		
-		vfs_file_put(file);
-		async_answer_2(rid, EOK, LOWER32(newoff),
-		    UPPER32(newoff));
-		return;
-	case SEEK_END:
-		fibril_rwlock_read_lock(&file->node->contents_rwlock);
-		aoff64_t size = vfs_node_get_size(file->node);
-		
-		if ((off >= 0) && (size + off < size)) {
-			fibril_rwlock_read_unlock(&file->node->contents_rwlock);
-			vfs_file_put(file);
-			async_answer_0(rid, EOVERFLOW);
-			return;
-		}
-		
-		if ((off < 0) && (size < (aoff64_t) -off)) {
-			fibril_rwlock_read_unlock(&file->node->contents_rwlock);
-			vfs_file_put(file);
-			async_answer_0(rid, EOVERFLOW);
-			return;
-		}
-		
-		file->pos = size + off;
-		newoff = (file->pos > OFF64_MAX) ?  OFF64_MAX : file->pos;
-		
-		fibril_rwlock_read_unlock(&file->node->contents_rwlock);
-		vfs_file_put(file);
-		async_answer_2(rid, EOK, LOWER32(newoff), UPPER32(newoff));
-		return;
-	}
-	
-	vfs_file_put(file);
-	async_answer_0(rid, EINVAL);
-}
-
-int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
-    fs_index_t index, aoff64_t size)
-{
-	async_exch_t *exch = vfs_exchange_grab(fs_handle);
-	sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
-	    (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
-	    UPPER32(size));
-	vfs_exchange_release(exch);
-	
-	return (int) rc;
-}
-
-void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
-{
-	int fd = IPC_GET_ARG1(*request);
-	aoff64_t size = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(*request),
-	    IPC_GET_ARG3(*request));
-	int rc;
-
-	vfs_file_t *file = vfs_file_get(fd);
-	if (!file) {
-		async_answer_0(rid, ENOENT);
-		return;
-	}
-
-	fibril_rwlock_write_lock(&file->node->contents_rwlock);
-	rc = vfs_truncate_internal(file->node->fs_handle,
-	    file->node->service_id, file->node->index, size);
-	if (rc == EOK)
-		file->node->size = size;
-	fibril_rwlock_write_unlock(&file->node->contents_rwlock);
-
-	vfs_file_put(file);
-	async_answer_0(rid, (sysarg_t)rc);
-}
-
-void vfs_fstat(ipc_callid_t rid, ipc_call_t *request)
-{
-	int fd = IPC_GET_ARG1(*request);
-	sysarg_t rc;
-
-	vfs_file_t *file = vfs_file_get(fd);
-	if (!file) {
-		async_answer_0(rid, ENOENT);
-		return;
-	}
-	assert(file->node);
-
-	ipc_callid_t callid;
-	if (!async_data_read_receive(&callid, NULL)) {
-		vfs_file_put(file);
-		async_answer_0(callid, EINVAL);
-		async_answer_0(rid, EINVAL);
-		return;
-	}
-
-	async_exch_t *exch = vfs_exchange_grab(file->node->fs_handle);
-	assert(exch);
-	
-	aid_t msg;
-	msg = async_send_3(exch, VFS_OUT_STAT, file->node->service_id,
-	    file->node->index, true, NULL);
-	assert(msg);
-	async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
-	
-	vfs_exchange_release(exch);
-	
-	async_wait_for(msg, &rc);
-	
-	vfs_file_put(file);
-	async_answer_0(rid, rc);
-}
-
-static void out_destroy(vfs_triplet_t *file)
-{
-	async_exch_t *exch = vfs_exchange_grab(file->fs_handle);
-	async_msg_2(exch, VFS_OUT_DESTROY,
-		(sysarg_t) file->service_id, (sysarg_t) file->index);
-	vfs_exchange_release(exch);
-}
-
-void vfs_unlink2(ipc_callid_t rid, ipc_call_t *request)
-{
-	int rc;
-	char *path;
-	vfs_file_t *parent = NULL;
-	vfs_file_t *expect = NULL;
-	
-	int parentfd = IPC_GET_ARG1(*request);
-	int expectfd = IPC_GET_ARG2(*request);
-	int wflag = IPC_GET_ARG3(*request);
-	
-	rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
-	if (rc != EOK) {
-		async_answer_0(rid, rc);
-		return;
-	}
-	if (parentfd == expectfd) {
-		async_answer_0(rid, EINVAL);
-		return;
-	}
-	
-	fibril_rwlock_write_lock(&namespace_rwlock);
-	
-	int lflag = (wflag&WALK_DIRECTORY) ? L_DIRECTORY: 0;
-
-	/* Files are retrieved in order of file descriptors, to prevent deadlock. */
-	if (parentfd < expectfd) {
-		parent = vfs_file_get(parentfd);
-		if (!parent) {
-			rc = EBADF;
-			goto exit;
-		}
-	}
-	
-	if (expectfd >= 0) {
-		expect = vfs_file_get(expectfd);
-		if (!expect) {
-			rc = ENOENT;
-			goto exit;
-		}
-	}
-	
-	if (parentfd > expectfd) {
-		parent = vfs_file_get(parentfd);
-		if (!parent) {
-			rc = EBADF;
-			goto exit;
-		}
-	}
-	
-	assert(parent != NULL);
-	
-	if (expectfd >= 0) {
-		vfs_lookup_res_t lr;
-		rc = vfs_lookup_internal(parent->node, path, lflag, &lr);
-		if (rc != EOK) {
-			goto exit;
-		}
-		
-		vfs_node_t *found_node = vfs_node_peek(&lr);		
-		if (expect->node != found_node) {
-			rc = ENOENT;
-			goto exit;
-		}
-		
-		vfs_file_put(expect);
-		expect = NULL;
-	}
-	
-	vfs_lookup_res_t lr;
-	rc = vfs_lookup_internal(parent->node, path, lflag | L_UNLINK, &lr);
-	if (rc != EOK) {
-		goto exit;
-	}
-
-	/* If the node is not held by anyone, try to destroy it. */
-	if (vfs_node_peek(&lr) == NULL) {
-		out_destroy(&lr.triplet);
-	}
-
-exit:
-	if (path) {
-		free(path);
-	}
-	if (parent) {
-		vfs_file_put(parent);
-	}
-	if (expect) {
-		vfs_file_put(expect);
-	}
-	fibril_rwlock_write_unlock(&namespace_rwlock);
-	async_answer_0(rid, rc);
-}
-
-static size_t shared_path(char *a, char *b)
-{
-	size_t res = 0;
-	
-	while (a[res] == b[res] && a[res] != 0) {
-		res++;
-	}
-	
-	if (a[res] == b[res]) {
-		return res;
-	}
-	
-	res--;
-	while (a[res] != '/') {
-		res--;
-	}
-	return res;
-}
-
-static int vfs_rename_internal(vfs_node_t *base, char *old, char *new)
-{
-	assert(base != NULL);
-	assert(old != NULL);
-	assert(new != NULL);
+int vfs_op_read(int fd, size_t *out_bytes)
+{
+	return vfs_rdwr(fd, true, rdwr_ipc_client, out_bytes);
+}
+
+int vfs_op_rename(int basefd, char *old, char *new)
+{
+	vfs_file_t *base_file = vfs_file_get(basefd);
+	if (!base_file) {
+		return EBADF;
+	}
+	vfs_node_t *base = base_file->node;
+	vfs_node_addref(base);
+	vfs_file_put(base_file);
 	
 	vfs_lookup_res_t base_lr;
@@ -1026,14 +658,13 @@
 		}
 		
+		vfs_node_put(base);
 		base = vfs_node_get(&base_lr);
 		old[shared] = '/';
 		old += shared;
 		new += shared;
-	} else {
-		vfs_node_addref(base);
-	}
-	
-	
-	rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS, &new_lr_orig);
+	}
+		
+	rc = vfs_lookup_internal(base, new, L_UNLINK | L_DISABLE_MOUNTS,
+	    &new_lr_orig);
 	if (rc == EOK) {
 		orig_unlinked = true;
@@ -1044,5 +675,6 @@
 	}
 	
-	rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS, &old_lr);
+	rc = vfs_lookup_internal(base, old, L_UNLINK | L_DISABLE_MOUNTS,
+	    &old_lr);
 	if (rc != EOK) {
 		if (orig_unlinked) {
@@ -1075,172 +707,68 @@
 }
 
-void vfs_rename(ipc_callid_t rid, ipc_call_t *request)
-{
-	/* The common base directory. */
-	int basefd;
-	char *old = NULL;
-	char *new = NULL;
-	vfs_file_t *base = NULL;
-	int rc;
-	
-	basefd = IPC_GET_ARG1(*request);
-	
-	/* Retrieve the old path. */
-	rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
-	if (rc != EOK) {
-		goto out;
-	}
-	
-	/* Retrieve the new path. */
-	rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
-	if (rc != EOK) {
-		goto out;
-	}
-	
-	size_t olen;
-	size_t nlen;
-	char *oldc = canonify(old, &olen);
-	char *newc = canonify(new, &nlen);
-	
-	if ((!oldc) || (!newc)) {
-		rc = EINVAL;
-		goto out;
-	}
-	
-	assert(oldc[olen] == '\0');
-	assert(newc[nlen] == '\0');
-	
-	/* Lookup the file structure corresponding to the file descriptor. */
-	base = vfs_file_get(basefd);
-	if (!base) {
-		rc = EBADF;
-		goto out;
-	}
-	
-	rc = vfs_rename_internal(base->node, oldc, newc);
-
-out:
-	async_answer_0(rid, rc);
-
-	if (old) {
-		free(old);
-	}
-	if (new) {
-		free(new);
-	}
-	if (base) {
-		vfs_file_put(base);
-	}
-}
-
-void vfs_dup(ipc_callid_t rid, ipc_call_t *request)
-{
-	int oldfd = IPC_GET_ARG1(*request);
-	int newfd = IPC_GET_ARG2(*request);
-	
-	/* If the file descriptors are the same, do nothing. */
-	if (oldfd == newfd) {
-		async_answer_1(rid, EOK, newfd);
-		return;
-	}
-	
-	/* Lookup the file structure corresponding to oldfd. */
-	vfs_file_t *oldfile = vfs_file_get(oldfd);
-	if (!oldfile) {
-		async_answer_0(rid, EBADF);
-		return;
-	}
-	
-	/* Make sure newfd is closed. */
-	(void) vfs_fd_free(newfd);
-	
-	/* Assign the old file to newfd. */
-	int ret = vfs_fd_assign(oldfile, newfd);
-	vfs_file_put(oldfile);
-	
-	if (ret != EOK)
-		async_answer_0(rid, ret);
-	else
-		async_answer_1(rid, EOK, newfd);
-}
-
-void vfs_wait_handle(ipc_callid_t rid, ipc_call_t *request)
-{
-	bool high_fd = IPC_GET_ARG1(*request);
-	int fd = vfs_wait_handle_internal(high_fd);
-	async_answer_1(rid, EOK, fd);
-}
-
-void vfs_get_mtab(ipc_callid_t rid, ipc_call_t *request)
+int vfs_op_seek(int fd, int64_t offset, int whence, int64_t *out_offset)
+{
+	vfs_file_t *file = vfs_file_get(fd);
+	if (!file) {
+		return EBADF;
+	}
+	
+	switch (whence) {
+	case SEEK_SET:
+		if (offset < 0) {
+			vfs_file_put(file);
+			return EINVAL;
+		}
+		file->pos = offset;
+		*out_offset = offset;
+		vfs_file_put(file);
+		return EOK;
+	case SEEK_CUR:
+		if (offset > 0 && file->pos > (INT64_MAX - offset)) {
+			vfs_file_put(file);
+			return EOVERFLOW;
+		}
+		
+		if (offset < 0 && -file->pos > offset) {
+			vfs_file_put(file);
+			return EOVERFLOW;
+		}
+		
+		file->pos += offset;
+		*out_offset = file->pos;
+		vfs_file_put(file);
+		return EOK;
+	case SEEK_END:
+		fibril_rwlock_read_lock(&file->node->contents_rwlock);
+		int64_t size = vfs_node_get_size(file->node);
+		fibril_rwlock_read_unlock(&file->node->contents_rwlock);
+		
+		if (offset > 0 && size > (INT64_MAX - offset)) {
+			vfs_file_put(file);
+			return EOVERFLOW;
+		}
+		
+		if (offset < 0 && -size > offset) {
+			vfs_file_put(file);
+			return EOVERFLOW;
+		}
+		
+		file->pos = size + offset;
+		*out_offset = file->pos;
+		vfs_file_put(file);
+		return EOK;
+	}
+	
+	vfs_file_put(file);
+	return EINVAL;
+}
+
+int vfs_op_statfs(int fd)
 {
 	ipc_callid_t callid;
-	ipc_call_t data;
-	sysarg_t rc = EOK;
-	size_t len;
-
-	fibril_mutex_lock(&mtab_list_lock);
-
-	/* Send to the caller the number of mounted filesystems */
-	callid = async_get_call(&data);
-	if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
-		rc = ENOTSUP;
-		async_answer_0(callid, rc);
-		goto exit;
-	}
-	async_answer_1(callid, EOK, mtab_size);
-
-	list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
-		rc = ENOTSUP;
-
-		if (!async_data_read_receive(&callid, &len)) {
-			async_answer_0(callid, rc);
-			goto exit;
-		}
-
-		(void) async_data_read_finalize(callid, mtab_ent->mp,
-		    str_size(mtab_ent->mp));
-
-		if (!async_data_read_receive(&callid, &len)) {
-			async_answer_0(callid, rc);
-			goto exit;
-		}
-
-		(void) async_data_read_finalize(callid, mtab_ent->opts,
-		    str_size(mtab_ent->opts));
-
-		if (!async_data_read_receive(&callid, &len)) {
-			async_answer_0(callid, rc);
-			goto exit;
-		}
-
-		(void) async_data_read_finalize(callid, mtab_ent->fs_name,
-		    str_size(mtab_ent->fs_name));
-
-		callid = async_get_call(&data);
-
-		if (IPC_GET_IMETHOD(data) != VFS_IN_PING) {
-			async_answer_0(callid, rc);
-			goto exit;
-		}
-
-		rc = EOK;
-		async_answer_2(callid, rc, mtab_ent->instance,
-		    mtab_ent->service_id);
-	}
-
-exit:
-	fibril_mutex_unlock(&mtab_list_lock);
-	async_answer_0(rid, rc);
-}
-
-void vfs_statfs(ipc_callid_t rid, ipc_call_t *request)
-{
-	int fd = IPC_GET_ARG1(*request);
-	
-	ipc_callid_t callid;
+	
 	if (!async_data_read_receive(&callid, NULL)) {
 		async_answer_0(callid, EINVAL);
-		async_answer_0(rid, EINVAL);
-		return;
+	       	return EINVAL;
 	}
 
@@ -1248,5 +776,5 @@
 	if (!file) {
 		async_answer_0(callid, EBADF);
-		async_answer_0(rid, EBADF);
+		return EBADF;
 	}
 
@@ -1254,49 +782,322 @@
 
 	async_exch_t *exch = vfs_exchange_grab(node->fs_handle);
-	
+							        
 	aid_t msg;
 	msg = async_send_3(exch, VFS_OUT_STATFS, node->service_id,
-	    node->index, false, NULL);
+	node->index, false, NULL);
 	async_forward_fast(callid, exch, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
-	
+
 	vfs_exchange_release(exch);
-	
+
 	sysarg_t rv;
 	async_wait_for(msg, &rv);
 
 	vfs_file_put(file);
-
-	async_answer_0(rid, rv);
-}
-
-void vfs_op_clone(ipc_callid_t rid, ipc_call_t *request)
-{
-	int oldfd = IPC_GET_ARG1(*request);
-	bool desc = IPC_GET_ARG2(*request);
-	
-	/* Lookup the file structure corresponding to fd. */
-	vfs_file_t *oldfile = vfs_file_get(oldfd);
-	if (oldfile == NULL) {
-		async_answer_0(rid, EBADF);
-		return;
-	}
-	
-	vfs_file_t *newfile;
-	int newfd = vfs_fd_alloc(&newfile, desc);
-	async_answer_0(rid, newfd);
-	
-	if (newfd < 0) {
-		vfs_file_put(oldfile);
-		return;
-	}
-	
-	assert(oldfile->node != NULL);
-	
-	newfile->node = oldfile->node;
-	newfile->permissions = oldfile->permissions;
-	vfs_node_addref(newfile->node);
-
-	vfs_file_put(oldfile);
-	vfs_file_put(newfile);
+	return rv;
+}
+
+int vfs_op_sync(int fd)
+{
+	vfs_file_t *file = vfs_file_get(fd);
+	if (!file) {
+		return EBADF;
+	}
+	
+	async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle);
+	
+	aid_t msg;
+	ipc_call_t answer;
+	msg = async_send_2(fs_exch, VFS_OUT_SYNC, file->node->service_id,
+		file->node->index, &answer);
+	
+	vfs_exchange_release(fs_exch);
+	
+	sysarg_t rc;
+	async_wait_for(msg, &rc);
+	
+	vfs_file_put(file);
+	return rc;
+	
+}
+
+static int vfs_truncate_internal(fs_handle_t fs_handle, service_id_t service_id,
+    fs_index_t index, aoff64_t size)
+{
+	async_exch_t *exch = vfs_exchange_grab(fs_handle);
+	sysarg_t rc = async_req_4_0(exch, VFS_OUT_TRUNCATE,
+	    (sysarg_t) service_id, (sysarg_t) index, LOWER32(size),
+	    UPPER32(size));
+	vfs_exchange_release(exch);
+	
+	return (int) rc;
+}
+
+int vfs_op_truncate(int fd, int64_t size)
+{
+	vfs_file_t *file = vfs_file_get(fd);
+	if (!file) {
+		return EBADF;
+	}
+
+	fibril_rwlock_write_lock(&file->node->contents_rwlock);
+	
+	int rc = vfs_truncate_internal(file->node->fs_handle,
+		file->node->service_id, file->node->index, size);
+	if (rc == EOK) {
+		file->node->size = size;
+	}
+	
+	fibril_rwlock_write_unlock(&file->node->contents_rwlock);
+	vfs_file_put(file);
+	return rc;
+}
+
+int vfs_op_unlink2(int parentfd, int expectfd, int wflag, char *path)
+{
+	int rc = EOK;
+	vfs_file_t *parent = NULL;
+	vfs_file_t *expect = NULL;
+	
+	if (parentfd == expectfd) {
+		return EINVAL;
+	}
+	
+	fibril_rwlock_write_lock(&namespace_rwlock);
+	
+	int lflag = (wflag&WALK_DIRECTORY) ? L_DIRECTORY: 0;
+
+	/* 
+	 * Files are retrieved in order of file descriptors, to prevent
+	 * deadlock.
+	 */
+	if (parentfd < expectfd) {
+		parent = vfs_file_get(parentfd);
+		if (!parent) {
+			rc = EBADF;
+			goto exit;
+		}
+	}
+	
+	if (expectfd >= 0) {
+		expect = vfs_file_get(expectfd);
+		if (!expect) {
+			rc = EBADF;
+			goto exit;
+		}
+	}
+	
+	if (parentfd > expectfd) {
+		parent = vfs_file_get(parentfd);
+		if (!parent) {
+			rc = EBADF;
+			goto exit;
+		}
+	}
+	
+	assert(parent != NULL);
+	
+	if (expectfd >= 0) {
+		vfs_lookup_res_t lr;
+		rc = vfs_lookup_internal(parent->node, path, lflag, &lr);
+		if (rc != EOK) {
+			goto exit;
+		}
+		
+		vfs_node_t *found_node = vfs_node_peek(&lr);		
+		if (expect->node != found_node) {
+			rc = ENOENT;
+			goto exit;
+		}
+		
+		vfs_file_put(expect);
+		expect = NULL;
+	}
+	
+	vfs_lookup_res_t lr;
+	rc = vfs_lookup_internal(parent->node, path, lflag | L_UNLINK, &lr);
+	if (rc != EOK) {
+		goto exit;
+	}
+
+	/* If the node is not held by anyone, try to destroy it. */
+	if (vfs_node_peek(&lr) == NULL) {
+		out_destroy(&lr.triplet);
+	}
+
+exit:
+	if (path) {
+		free(path);
+	}
+	if (parent) {
+		vfs_file_put(parent);
+	}
+	if (expect) {
+		vfs_file_put(expect);
+	}
+	fibril_rwlock_write_unlock(&namespace_rwlock);
+	return rc;
+}
+
+int vfs_op_unmount(int mpfd)
+{
+	vfs_file_t *mp = vfs_file_get(mpfd);
+	if (mp == NULL) {
+		return EBADF;
+	}
+	
+	if (mp->node->mount == NULL) {
+		vfs_file_put(mp);
+		return ENOENT;
+	}
+	
+	fibril_rwlock_write_lock(&namespace_rwlock);
+	
+	/*
+	 * Count the total number of references for the mounted file system. We
+	 * are expecting at least one, which is held by the mount point.
+	 * If we find more, it means that
+	 * the file system cannot be gracefully unmounted at the moment because
+	 * someone is working with it.
+	 */
+	if (vfs_nodes_refcount_sum_get(mp->node->mount->fs_handle,
+	    mp->node->mount->service_id) != 1) {
+		vfs_file_put(mp);
+		fibril_rwlock_write_unlock(&namespace_rwlock);
+		return EBUSY;
+	}
+	
+	async_exch_t *exch = vfs_exchange_grab(mp->node->mount->fs_handle);
+	int rc = async_req_1_0(exch, VFS_OUT_UNMOUNTED,
+	    mp->node->mount->service_id);
+	vfs_exchange_release(exch);
+	
+	if (rc != EOK) {
+		vfs_file_put(mp);
+		fibril_rwlock_write_unlock(&namespace_rwlock);
+		return rc;
+	}
+	
+	vfs_node_forget(mp->node->mount);
+	vfs_node_put(mp->node);
+	mp->node->mount = NULL;
+	
+	fibril_rwlock_write_unlock(&namespace_rwlock);
+	
+	fibril_mutex_lock(&mtab_list_lock);
+	int found = 0;
+
+	list_foreach(mtab_list, link, mtab_ent_t, mtab_ent) {
+		// FIXME: mp name
+		if (str_cmp(mtab_ent->mp, "fixme") == 0) {
+			list_remove(&mtab_ent->link);
+			mtab_size--;
+			free(mtab_ent);
+			found = 1;
+			break;
+		}
+	}
+	assert(found);
+	fibril_mutex_unlock(&mtab_list_lock);
+	
+	vfs_file_put(mp);
+	return EOK;
+}
+
+int vfs_op_wait_handle(bool high_fd)
+{
+	return vfs_wait_handle_internal(high_fd);
+}
+
+static inline bool walk_flags_valid(int flags)
+{
+	if ((flags&~WALK_ALL_FLAGS) != 0) {
+		return false;
+	}
+	if ((flags&WALK_MAY_CREATE) && (flags&WALK_MUST_CREATE)) {
+		return false;
+	}
+	if ((flags&WALK_REGULAR) && (flags&WALK_DIRECTORY)) {
+		return false;
+	}
+	if ((flags&WALK_MAY_CREATE) || (flags&WALK_MUST_CREATE)) {
+		if (!(flags&WALK_DIRECTORY) && !(flags&WALK_REGULAR)) {
+			return false;
+		}
+	}
+	return true;
+}
+
+static inline int walk_lookup_flags(int flags)
+{
+	int lflags = 0;
+	if (flags&WALK_MAY_CREATE || flags&WALK_MUST_CREATE) {
+		lflags |= L_CREATE;
+	}
+	if (flags&WALK_MUST_CREATE) {
+		lflags |= L_EXCLUSIVE;
+	}
+	if (flags&WALK_REGULAR) {
+		lflags |= L_FILE;
+	}
+	if (flags&WALK_DIRECTORY) {
+		lflags |= L_DIRECTORY;
+	}
+	if (flags&WALK_MOUNT_POINT) {
+		lflags |= L_MP;
+	}
+	return lflags;
+}
+
+int vfs_op_walk(int parentfd, int flags, char *path, int *out_fd)
+{
+	if (!walk_flags_valid(flags)) {
+		return EINVAL;
+	}
+	
+	vfs_file_t *parent = vfs_file_get(parentfd);
+	if (!parent) {
+		return EBADF;
+	}
+	
+	fibril_rwlock_read_lock(&namespace_rwlock);
+	
+	vfs_lookup_res_t lr;
+	int rc = vfs_lookup_internal(parent->node, path,
+	    walk_lookup_flags(flags), &lr);
+
+	if (rc != EOK) {
+		fibril_rwlock_read_unlock(&namespace_rwlock);
+		vfs_file_put(parent);
+		return rc;
+	}
+	
+	vfs_node_t *node = vfs_node_get(&lr);
+	
+	vfs_file_t *file;
+	int fd = vfs_fd_alloc(&file, false);
+	if (fd < 0) {
+		vfs_node_put(node);
+		vfs_file_put(parent);
+		return fd;
+	}
+	assert(file != NULL);
+	
+	file->node = node;
+	file->permissions = parent->permissions;
+	file->open_read = false;
+	file->open_write = false;
+	
+	vfs_file_put(file);
+	vfs_file_put(parent);
+	
+	fibril_rwlock_read_unlock(&namespace_rwlock);
+
+	*out_fd = fd;
+	return EOK;
+}
+
+int vfs_op_write(int fd, size_t *out_bytes)
+{
+	return vfs_rdwr(fd, false, rdwr_ipc_client, out_bytes);
 }
 
