Index: uspace/srv/vfs/vfs.h
===================================================================
--- uspace/srv/vfs/vfs.h	(revision d60ce4abaa4bfa71b3cf2c3701c3157ffc9c341f)
+++ uspace/srv/vfs/vfs.h	(revision bf9dc4e2a497f5d183351f2b788ef6d7723ba6cf)
@@ -180,6 +180,6 @@
 extern vfs_info_t *fs_handle_to_info(fs_handle_t);
 
-extern int vfs_lookup_internal(char *, int, vfs_lookup_res_t *,
-    vfs_pair_t *, ...);
+extern int vfs_lookup_internal(vfs_triplet_t *, char *, int, vfs_lookup_res_t *);
+extern int vfs_link_internal(vfs_triplet_t *, char *, vfs_triplet_t *);
 
 extern bool vfs_nodes_init(void);
Index: uspace/srv/vfs/vfs_lookup.c
===================================================================
--- uspace/srv/vfs/vfs_lookup.c	(revision d60ce4abaa4bfa71b3cf2c3701c3157ffc9c341f)
+++ uspace/srv/vfs/vfs_lookup.c	(revision bf9dc4e2a497f5d183351f2b788ef6d7723ba6cf)
@@ -46,4 +46,8 @@
 #include <adt/list.h>
 #include <vfs/canonify.h>
+#include <dirent.h>
+#include <assert.h>
+
+#define DPRINTF(...)
 
 #define min(a, b)  ((a) < (b) ? (a) : (b))
@@ -53,49 +57,10 @@
 uint8_t *plb = NULL;
 
-/** Perform a path lookup.
- *
- * @param path    Path to be resolved; it must be a NULL-terminated
- *                string.
- * @param lflag   Flags to be used during lookup.
- * @param result  Empty structure where the lookup result will be stored.
- *                Can be NULL.
- * @param altroot If non-empty, will be used instead of rootfs as the root
- *                of the whole VFS tree.
- *
- * @return EOK on success or an error code from errno.h.
- *
- */
-int vfs_lookup_internal(char *path, int lflag, vfs_lookup_res_t *result,
-    vfs_pair_t *altroot, ...)
-{
-	vfs_pair_t *root;
-
-	if (altroot)
-		root = altroot;
-	else
-		root = &rootfs;
-
-	if (!root->fs_handle)
-		return ENOENT;
-	
-	size_t len;
-	path = canonify(path, &len);
-	if (!path)
-		return EINVAL;
-	
-	fs_index_t index = 0;
-	if (lflag & L_LINK) {
-		va_list ap;
-
-		va_start(ap, altroot);
-		index = va_arg(ap, fs_index_t);
-		va_end(ap);
-	}
-	
+static int plb_insert_entry(plb_entry_t *entry, char *path, size_t *start, size_t len)
+{
 	fibril_mutex_lock(&plb_mutex);
 
-	plb_entry_t entry;
-	link_initialize(&entry.plb_link);
-	entry.len = len;
+	link_initialize(&entry->plb_link);
+	entry->len = len;
 
 	size_t first;	/* the first free index */
@@ -138,6 +103,6 @@
 	 */
 
-	entry.index = first;
-	entry.len = len;
+	entry->index = first;
+	entry->len = len;
 
 	/*
@@ -145,5 +110,5 @@
 	 * buffer.
 	 */
-	list_append(&entry.plb_link, &plb_entries);
+	list_append(&entry->plb_link, &plb_entries);
 	
 	fibril_mutex_unlock(&plb_mutex);
@@ -158,29 +123,155 @@
 	memcpy(plb, &path[cnt1], cnt2);
 
-	ipc_call_t answer;
-	async_exch_t *exch = vfs_exchange_grab(root->fs_handle);
-	aid_t req = async_send_5(exch, VFS_OUT_LOOKUP, (sysarg_t) first,
-	    (sysarg_t) (first + len - 1) % PLB_SIZE,
-	    (sysarg_t) root->service_id, (sysarg_t) lflag, (sysarg_t) index,
-	    &answer);
-	
-	sysarg_t rc;
-	async_wait_for(req, &rc);
-	vfs_exchange_release(exch);
-	
+	*start = first;
+	return EOK;
+}
+
+static void plb_clear_entry(plb_entry_t *entry, size_t first, size_t len)
+{
 	fibril_mutex_lock(&plb_mutex);
-	list_remove(&entry.plb_link);
+	list_remove(&entry->plb_link);
 	/*
 	 * Erasing the path from PLB will come handy for debugging purposes.
 	 */
+	size_t cnt1 = min(len, (PLB_SIZE - first) + 1);
+	size_t cnt2 = len - cnt1;
 	memset(&plb[first], 0, cnt1);
 	memset(plb, 0, cnt2);
 	fibril_mutex_unlock(&plb_mutex);
-	
-	if ((int) rc < EOK)
-		return (int) rc;
-
-	if (!result)
-		return EOK;
+}
+
+static char *_strrchr(char *path, int c)
+{
+	char *res = NULL;
+	while (*path != 0) {
+		if (*path == c) {
+			res = path;
+		}
+		path++;
+	}
+	return res;
+}
+
+int vfs_link_internal(vfs_triplet_t *base, char *path, vfs_triplet_t *child)
+{
+	assert(base != NULL);
+	assert(child != NULL);
+	assert(base->fs_handle);
+	assert(child->fs_handle);
+	assert(path != NULL);
+	
+	vfs_lookup_res_t res;
+	char component[NAME_MAX + 1];
+	int rc;
+	
+	size_t len;
+	char *npath = canonify(path, &len);
+	if (!npath) {
+		rc = EINVAL;
+		goto out;
+	}
+	path = npath;
+	
+	char *slash = _strrchr(path, '/');
+	if (slash && slash != path) {
+		if (slash[1] == 0) {
+			rc = EINVAL;
+			goto out;
+		}
+		
+		memcpy(component, slash + 1, str_size(slash));
+		*slash = 0;
+		
+		rc = vfs_lookup_internal(base, path, L_DIRECTORY, &res);
+		if (rc != EOK) {
+			goto out;
+		}
+		base = &res.triplet;
+		
+		*slash = '/';
+	} else {
+		memcpy(component, path + 1, str_size(path));
+	}
+	
+	if (base->fs_handle != child->fs_handle || base->service_id != child->service_id) {
+		rc = EXDEV;
+		goto out;
+	}
+	
+	async_exch_t *exch = vfs_exchange_grab(base->fs_handle);
+	aid_t req = async_send_3(exch, VFS_OUT_LINK, base->service_id, base->index, child->index, NULL);
+	
+	rc = async_data_write_start(exch, component, str_size(component) + 1);
+	sysarg_t orig_rc;
+	async_wait_for(req, &orig_rc);
+	vfs_exchange_release(exch);
+	if (orig_rc != EOK) {
+		rc = orig_rc;
+	}
+	
+out:
+	DPRINTF("vfs_link_internal() with path '%s' returns %d\n", path, rc);
+	return rc;
+}
+
+/** Perform a path lookup.
+ *
+ * @param base    The file from which to perform the lookup.
+ * @param path    Path to be resolved; it must be a NULL-terminated
+ *                string.
+ * @param lflag   Flags to be used during lookup.
+ * @param result  Empty structure where the lookup result will be stored.
+ *                Can be NULL.
+ *
+ * @return EOK on success or an error code from errno.h.
+ *
+ */
+int vfs_lookup_internal(vfs_triplet_t *base, char *path, int lflag, vfs_lookup_res_t *result)
+{
+	assert(base != NULL);
+	assert(path != NULL);
+	
+	sysarg_t rc;
+	
+	if (!base->fs_handle) {
+		rc = ENOENT;
+		goto out;
+	}
+	
+	size_t len;
+	char *npath = canonify(path, &len);
+	if (!npath) {
+		rc = EINVAL;
+		goto out;
+	}
+	path = npath;
+	
+	assert(path[0] == '/');
+	
+	size_t first;
+	
+	plb_entry_t entry;
+	rc = plb_insert_entry(&entry, path, &first, len);
+	if (rc != EOK) {
+		goto out;
+	}
+	
+	ipc_call_t answer;
+	async_exch_t *exch = vfs_exchange_grab(base->fs_handle);
+	aid_t req = async_send_5(exch, VFS_OUT_LOOKUP, (sysarg_t) first, (sysarg_t) len,
+	    (sysarg_t) base->service_id, (sysarg_t) base->index, (sysarg_t) lflag, &answer);
+	async_wait_for(req, &rc);
+	vfs_exchange_release(exch);
+	
+	plb_clear_entry(&entry, first, len);
+	
+	if ((int) rc < 0) {
+		goto out;
+	}
+	
+	if (!result) {
+		rc = EOK;
+		goto out;
+	}
 	
 	result->triplet.fs_handle = (fs_handle_t) rc;
@@ -197,6 +288,10 @@
 	else
 		result->type = VFS_NODE_UNKNOWN;
-	
-	return EOK;
+
+	rc = EOK;
+
+out:
+	DPRINTF("vfs_lookup_internal() with path '%s' returns %d\n", path, rc);
+	return rc;
 }
 
Index: uspace/srv/vfs/vfs_ops.c
===================================================================
--- uspace/srv/vfs/vfs_ops.c	(revision d60ce4abaa4bfa71b3cf2c3701c3157ffc9c341f)
+++ uspace/srv/vfs/vfs_ops.c	(revision bf9dc4e2a497f5d183351f2b788ef6d7723ba6cf)
@@ -68,8 +68,5 @@
 FIBRIL_RWLOCK_INITIALIZE(namespace_rwlock);
 
-vfs_pair_t rootfs = {
-	.fs_handle = 0,
-	.service_id = 0
-};
+vfs_node_t *root = NULL;
 
 static int vfs_mount_internal(ipc_callid_t rid, service_id_t service_id,
@@ -90,87 +87,7 @@
 	/* Resolve the path to the mountpoint. */
 	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);
-			async_answer_0(rid, EBUSY);
-			return EBUSY;
-		}
-		
-		rc = vfs_lookup_internal(mp, L_MP, &mp_res, NULL);
-		if (rc != EOK) {
-			/* The lookup failed for some reason. */
-			fibril_rwlock_write_unlock(&namespace_rwlock);
-			async_answer_0(rid, rc);
-			return rc;
-		}
-		
-		mp_node = vfs_node_get(&mp_res);
-		if (!mp_node) {
-			fibril_rwlock_write_unlock(&namespace_rwlock);
-			async_answer_0(rid, ENOMEM);
-			return ENOMEM;
-		}
-		
-		/*
-		 * Now we hold a reference to mp_node.
-		 * It will be dropped upon the corresponding VFS_IN_UNMOUNT.
-		 * This prevents the mount point from being deleted.
-		 */
-	} else {
+	if (root == NULL) {
 		/* We still don't have the root file system mounted. */
-		if (str_cmp(mp, "/") == 0) {
-			/*
-			 * For this simple, but important case,
-			 * we are almost done.
-			 */
-			
-			/* Tell the mountee that it is being mounted. */
-			exch = vfs_exchange_grab(fs_handle);
-			msg = async_send_1(exch, VFS_OUT_MOUNTED,
-			    (sysarg_t) service_id, &answer);
-			/* Send the mount options */
-			rc = async_data_write_start(exch, (void *)opts,
-			    str_size(opts));
-			vfs_exchange_release(exch);
-			
-			if (rc != EOK) {
-				async_forget(msg);
-				fibril_rwlock_write_unlock(&namespace_rwlock);
-				async_answer_0(rid, rc);
-				return rc;
-			}
-			async_wait_for(msg, &rc);
-			
-			if (rc != EOK) {
-				fibril_rwlock_write_unlock(&namespace_rwlock);
-				async_answer_0(rid, rc);
-				return rc;
-			}
-
-			rindex = (fs_index_t) IPC_GET_ARG1(answer);
-			rsize = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
-			    IPC_GET_ARG3(answer));
-			rlnkcnt = (unsigned) IPC_GET_ARG4(answer);
-			
-			mr_res.triplet.fs_handle = fs_handle;
-			mr_res.triplet.service_id = service_id;
-			mr_res.triplet.index = rindex;
-			mr_res.size = rsize;
-			mr_res.lnkcnt = rlnkcnt;
-			mr_res.type = VFS_NODE_DIRECTORY;
-			
-			rootfs.fs_handle = fs_handle;
-			rootfs.service_id = service_id;
-			
-			/* Add reference to the mounted root. */
-			mr_node = vfs_node_get(&mr_res); 
-			assert(mr_node);
-			
-			fibril_rwlock_write_unlock(&namespace_rwlock);
-			async_answer_0(rid, rc);
-			return rc;
-		} else {
+		if (str_cmp(mp, "/") != 0) {
 			/*
 			 * We can't resolve this without the root filesystem
@@ -181,5 +98,82 @@
 			return ENOENT;
 		}
-	}
+		
+		/*
+		 * For this simple, but important case,
+		 * we are almost done.
+		 */
+			
+		/* Tell the mountee that it is being mounted. */
+		exch = vfs_exchange_grab(fs_handle);
+		msg = async_send_1(exch, VFS_OUT_MOUNTED,
+		    (sysarg_t) service_id, &answer);
+		/* Send the mount options */
+		rc = async_data_write_start(exch, (void *)opts,
+		    str_size(opts));
+		vfs_exchange_release(exch);
+			
+		if (rc != EOK) {
+			async_forget(msg);
+			fibril_rwlock_write_unlock(&namespace_rwlock);
+			async_answer_0(rid, rc);
+			return rc;
+		}
+		async_wait_for(msg, &rc);
+			
+		if (rc != EOK) {
+			fibril_rwlock_write_unlock(&namespace_rwlock);
+			async_answer_0(rid, rc);
+			return rc;
+		}
+
+		rindex = (fs_index_t) IPC_GET_ARG1(answer);
+		rsize = (aoff64_t) MERGE_LOUP32(IPC_GET_ARG2(answer),
+		    IPC_GET_ARG3(answer));
+		rlnkcnt = (unsigned) IPC_GET_ARG4(answer);
+		
+		mr_res.triplet.fs_handle = fs_handle;
+		mr_res.triplet.service_id = service_id;
+		mr_res.triplet.index = rindex;
+		mr_res.size = rsize;
+		mr_res.lnkcnt = rlnkcnt;
+		mr_res.type = VFS_NODE_DIRECTORY;
+			
+		/* Add reference to the mounted root. */
+		root = vfs_node_get(&mr_res); 
+		assert(root);
+			
+		fibril_rwlock_write_unlock(&namespace_rwlock);
+		async_answer_0(rid, rc);
+		return rc;
+	}
+	
+	/* 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);
+		async_answer_0(rid, EBUSY);
+		return EBUSY;
+	}
+	
+	rc = vfs_lookup_internal((vfs_triplet_t *) root, mp, L_DIRECTORY, &mp_res);
+	if (rc != EOK) {
+		/* The lookup failed. */
+		fibril_rwlock_write_unlock(&namespace_rwlock);
+		async_answer_0(rid, rc);
+		return rc;
+	}
+	
+	mp_node = vfs_node_get(&mp_res);
+	if (!mp_node) {
+		fibril_rwlock_write_unlock(&namespace_rwlock);
+		async_answer_0(rid, ENOMEM);
+		return ENOMEM;
+	}
+	
+	/*
+	 * Now we hold a reference to mp_node.
+	 * It will be dropped upon the corresponding VFS_IN_UNMOUNT.
+	 * This prevents the mount point from being deleted.
+	 */
 	
 	/*
@@ -435,5 +429,5 @@
 	 * Lookup the mounted root and instantiate it.
 	 */
-	rc = vfs_lookup_internal(mp, L_ROOT, &mr_res, NULL);
+	rc = vfs_lookup_internal((vfs_triplet_t *) root, mp, 0, &mr_res);
 	if (rc != EOK) {
 		fibril_rwlock_write_unlock(&namespace_rwlock);
@@ -488,6 +482,5 @@
 		}
 		
-		rootfs.fs_handle = 0;
-		rootfs.service_id = 0;
+		root = NULL;
 	} else {
 		
@@ -499,5 +492,5 @@
 		 */
 		
-		rc = vfs_lookup_internal(mp, L_MP, &mp_res, NULL);
+		rc = vfs_lookup_internal((vfs_triplet_t *) root, mp, L_MP, &mp_res);
 		if (rc != EOK) {
 			fibril_rwlock_write_unlock(&namespace_rwlock);
@@ -624,5 +617,5 @@
 	/* Lookup the file structure corresponding to the file descriptor. */
 	vfs_file_t *parent = NULL;
-	vfs_pair_t *parent_node = NULL;
+	vfs_node_t *parent_node = root;
 	// TODO: Client-side root.
 	if (parentfd != -1) {
@@ -633,5 +626,5 @@
 			return;
 		}
-		parent_node = (vfs_pair_t *)parent->node;
+		parent_node = parent->node;
 	}
 	
@@ -639,5 +632,5 @@
 	
 	vfs_lookup_res_t lr;
-	rc = vfs_lookup_internal(path, walk_lookup_flags(flags), &lr, parent_node);
+	rc = vfs_lookup_internal((vfs_triplet_t *) parent_node, path, walk_lookup_flags(flags), &lr);
 	free(path);
 
@@ -1069,5 +1062,5 @@
 	vfs_file_t *parent = NULL;
 	vfs_file_t *expect = NULL;
-	vfs_pair_t *parent_node = NULL;
+	vfs_node_t *parent_node = root;
 	
 	int parentfd = IPC_GET_ARG1(*request);
@@ -1091,5 +1084,5 @@
 			goto exit;
 		}
-		parent_node = (vfs_pair_t *)parent->node;
+		parent_node = parent->node;
 	}
 	
@@ -1102,5 +1095,5 @@
 		
 		vfs_lookup_res_t lr;
-		rc = vfs_lookup_internal(path, lflag, &lr, parent_node);
+		rc = vfs_lookup_internal((vfs_triplet_t *) parent_node, path, lflag, &lr);
 		if (rc != EOK) {
 			goto exit;
@@ -1117,5 +1110,5 @@
 	
 	vfs_lookup_res_t lr;
-	rc = vfs_lookup_internal(path, lflag | L_UNLINK, &lr, parent_node);
+	rc = vfs_lookup_internal((vfs_triplet_t *) parent_node, path, lflag | L_UNLINK, &lr);
 	if (rc != EOK) {
 		goto exit;
@@ -1201,5 +1194,5 @@
 	
 	/* Lookup the node belonging to the old file name. */
-	rc = vfs_lookup_internal(oldc, L_NONE, &old_lr, NULL);
+	rc = vfs_lookup_internal((vfs_triplet_t *) root, oldc, L_NONE, &old_lr);
 	if (rc != EOK) {
 		fibril_rwlock_write_unlock(&namespace_rwlock);
@@ -1237,5 +1230,5 @@
 	
 	/* Lookup parent of the new file name. */
-	rc = vfs_lookup_internal(parentc, L_NONE, &new_par_lr, NULL);
+	rc = vfs_lookup_internal((vfs_triplet_t *) root, parentc, L_NONE, &new_par_lr);
 	free(parentc);	/* not needed anymore */
 	if (rc != EOK) {
@@ -1261,5 +1254,5 @@
 	/* Destroy the old link for the new name. */
 	vfs_node_t *new_node = NULL;
-	rc = vfs_lookup_internal(newc, L_UNLINK, &new_lr, NULL);
+	rc = vfs_lookup_internal((vfs_triplet_t *) root, newc, L_UNLINK, &new_lr);
 	
 	switch (rc) {
@@ -1291,5 +1284,5 @@
 	
 	/* Create the new link for the new name. */
-	rc = vfs_lookup_internal(newc, L_LINK, NULL, NULL, old_node->index);
+	rc = vfs_link_internal((vfs_triplet_t *) root, newc, (vfs_triplet_t *) old_node);
 	if (rc != EOK) {
 		fibril_rwlock_write_unlock(&namespace_rwlock);
@@ -1308,5 +1301,5 @@
 	
 	/* Destroy the link for the old name. */
-	rc = vfs_lookup_internal(oldc, L_UNLINK, NULL, NULL);
+	rc = vfs_lookup_internal((vfs_triplet_t *) root, oldc, L_UNLINK, NULL);
 	if (rc != EOK) {
 		fibril_rwlock_write_unlock(&namespace_rwlock);
