Index: uspace/srv/volsrv/part.c
===================================================================
--- uspace/srv/volsrv/part.c	(revision da680b4b4a258b7464a3f60a90d226a51447f783)
+++ uspace/srv/volsrv/part.c	(revision be0f5e4d59071204ff8b374bbff33ba65da0d3e1)
@@ -35,12 +35,13 @@
  */
 
-#include <stdbool.h>
+#include <adt/list.h>
 #include <errno.h>
-#include <str_error.h>
 #include <fibril_synch.h>
 #include <io/log.h>
 #include <loc.h>
+#include <stdbool.h>
 #include <stdlib.h>
 #include <str.h>
+#include <str_error.h>
 #include <vfs/vfs.h>
 
@@ -50,10 +51,8 @@
 #include "types/part.h"
 
-static errno_t vol_part_add_locked(service_id_t);
+static errno_t vol_part_add_locked(vol_parts_t *, service_id_t);
 static void vol_part_remove_locked(vol_part_t *);
-static errno_t vol_part_find_by_id_ref_locked(service_id_t, vol_part_t **);
-
-static LIST_INITIALIZE(vol_parts); /* of vol_part_t */
-static FIBRIL_MUTEX_INITIALIZE(vol_parts_lock);
+static errno_t vol_part_find_by_id_ref_locked(vol_parts_t *, service_id_t,
+    vol_part_t **);
 
 struct fsname_type {
@@ -87,5 +86,5 @@
 
 /** Check for new and removed partitions */
-static errno_t vol_part_check_new(void)
+static errno_t vol_part_check_new(vol_parts_t *parts)
 {
 	bool already_known;
@@ -98,10 +97,10 @@
 	errno_t rc;
 
-	fibril_mutex_lock(&vol_parts_lock);
+	fibril_mutex_lock(&parts->lock);
 
 	rc = loc_category_get_id("partition", &part_cat, IPC_FLAG_BLOCKING);
 	if (rc != EOK) {
 		log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'partition'.");
-		fibril_mutex_unlock(&vol_parts_lock);
+		fibril_mutex_unlock(&parts->lock);
 		return ENOENT;
 	}
@@ -111,5 +110,5 @@
 		log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of partition "
 		    "devices.");
-		fibril_mutex_unlock(&vol_parts_lock);
+		fibril_mutex_unlock(&parts->lock);
 		return EIO;
 	}
@@ -120,5 +119,5 @@
 
 		// XXX Make this faster
-		list_foreach(vol_parts, lparts, vol_part_t, part) {
+		list_foreach(parts->parts, lparts, vol_part_t, part) {
 			if (part->svc_id == svcs[i]) {
 				already_known = true;
@@ -130,5 +129,5 @@
 			log_msg(LOG_DEFAULT, LVL_NOTE, "Found partition '%lu'",
 			    (unsigned long) svcs[i]);
-			rc = vol_part_add_locked(svcs[i]);
+			rc = vol_part_add_locked(parts, svcs[i]);
 			if (rc != EOK) {
 				log_msg(LOG_DEFAULT, LVL_ERROR, "Could not add "
@@ -139,7 +138,7 @@
 
 	/* Check for removed partitions */
-	cur = list_first(&vol_parts);
+	cur = list_first(&parts->parts);
 	while (cur != NULL) {
-		next = list_next(cur, &vol_parts);
+		next = list_next(cur, &parts->parts);
 		part = list_get_instance(cur, vol_part_t, lparts);
 
@@ -164,5 +163,5 @@
 	free(svcs);
 
-	fibril_mutex_unlock(&vol_parts_lock);
+	fibril_mutex_unlock(&parts->lock);
 	return EOK;
 }
@@ -180,4 +179,5 @@
 	atomic_set(&part->refcnt, 1);
 	link_initialize(&part->lparts);
+	part->parts = NULL;
 	part->pcnt = vpc_empty;
 
@@ -206,5 +206,5 @@
 	log_msg(LOG_DEFAULT, LVL_NOTE, "Probe partition %s", part->svc_name);
 
-	assert(fibril_mutex_is_locked(&vol_parts_lock));
+	assert(fibril_mutex_is_locked(&part->parts->lock));
 
 	fst = &fstab[0];
@@ -324,14 +324,14 @@
 }
 
-static errno_t vol_part_add_locked(service_id_t sid)
+static errno_t vol_part_add_locked(vol_parts_t *parts, service_id_t sid)
 {
 	vol_part_t *part;
 	errno_t rc;
 
-	assert(fibril_mutex_is_locked(&vol_parts_lock));
+	assert(fibril_mutex_is_locked(&parts->lock));
 	log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_add_locked(%zu)", sid);
 
 	/* Check for duplicates */
-	rc = vol_part_find_by_id_ref_locked(sid, &part);
+	rc = vol_part_find_by_id_ref_locked(parts, sid, &part);
 	if (rc == EOK) {
 		vol_part_del_ref(part);
@@ -346,4 +346,5 @@
 
 	part->svc_id = sid;
+	part->parts = parts;
 
 	rc = loc_service_get_name(sid, &part->svc_name);
@@ -361,5 +362,5 @@
 		goto error;
 
-	list_append(&part->lparts, &vol_parts);
+	list_append(&part->lparts, &parts->parts);
 
 	log_msg(LOG_DEFAULT, LVL_NOTE, "Added partition %zu", part->svc_id);
@@ -374,6 +375,7 @@
 static void vol_part_remove_locked(vol_part_t *part)
 {
-	assert(fibril_mutex_is_locked(&vol_parts_lock));
-	log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_remove_locked(%zu)", part->svc_id);
+	assert(fibril_mutex_is_locked(&part->parts->lock));
+	log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_remove_locked(%zu)",
+	    part->svc_id);
 
 	list_remove(&part->lparts);
@@ -383,30 +385,51 @@
 }
 
-errno_t vol_part_add(service_id_t sid)
-{
-	errno_t rc;
-
-	fibril_mutex_lock(&vol_parts_lock);
-	rc = vol_part_add_locked(sid);
-	fibril_mutex_unlock(&vol_parts_lock);
+errno_t vol_part_add(vol_parts_t *parts, service_id_t sid)
+{
+	errno_t rc;
+
+	fibril_mutex_lock(&parts->lock);
+	rc = vol_part_add_locked(parts, sid);
+	fibril_mutex_unlock(&parts->lock);
 
 	return rc;
 }
 
-static void vol_part_cat_change_cb(void)
-{
-	(void) vol_part_check_new();
-}
-
-errno_t vol_part_init(void)
-{
-	return EOK;
-}
-
-errno_t vol_part_discovery_start(void)
-{
-	errno_t rc;
-
-	rc = loc_register_cat_change_cb(vol_part_cat_change_cb);
+static void vol_part_cat_change_cb(void *arg)
+{
+	vol_parts_t *parts = (vol_parts_t *) arg;
+
+	(void) vol_part_check_new(parts);
+}
+
+errno_t vol_parts_create(vol_parts_t **rparts)
+{
+	vol_parts_t *parts;
+
+	parts = calloc(1, sizeof(vol_parts_t));
+	if (parts == NULL)
+		return ENOMEM;
+
+	fibril_mutex_initialize(&parts->lock);
+	list_initialize(&parts->parts);
+
+	*rparts = parts;
+	return EOK;
+}
+
+void vol_parts_destroy(vol_parts_t *parts)
+{
+	if (parts == NULL)
+		return;
+
+	assert(list_empty(&parts->parts));
+	free(parts);
+}
+
+errno_t vol_part_discovery_start(vol_parts_t *parts)
+{
+	errno_t rc;
+
+	rc = loc_register_cat_change_cb(vol_part_cat_change_cb, parts);
 	if (rc != EOK) {
 		log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback "
@@ -415,27 +438,28 @@
 	}
 
-	return vol_part_check_new();
+	return vol_part_check_new(parts);
 }
 
 /** Get list of partitions as array of service IDs. */
-errno_t vol_part_get_ids(service_id_t *id_buf, size_t buf_size, size_t *act_size)
+errno_t vol_part_get_ids(vol_parts_t *parts, service_id_t *id_buf,
+    size_t buf_size, size_t *act_size)
 {
 	size_t act_cnt;
 	size_t buf_cnt;
 
-	fibril_mutex_lock(&vol_parts_lock);
+	fibril_mutex_lock(&parts->lock);
 
 	buf_cnt = buf_size / sizeof(service_id_t);
 
-	act_cnt = list_count(&vol_parts);
+	act_cnt = list_count(&parts->parts);
 	*act_size = act_cnt * sizeof(service_id_t);
 
 	if (buf_size % sizeof(service_id_t) != 0) {
-		fibril_mutex_unlock(&vol_parts_lock);
+		fibril_mutex_unlock(&parts->lock);
 		return EINVAL;
 	}
 
 	size_t pos = 0;
-	list_foreach(vol_parts, lparts, vol_part_t, part) {
+	list_foreach(parts->parts, lparts, vol_part_t, part) {
 		if (pos < buf_cnt)
 			id_buf[pos] = part->svc_id;
@@ -443,14 +467,14 @@
 	}
 
-	fibril_mutex_unlock(&vol_parts_lock);
-	return EOK;
-}
-
-static errno_t vol_part_find_by_id_ref_locked(service_id_t sid,
-    vol_part_t **rpart)
-{
-	assert(fibril_mutex_is_locked(&vol_parts_lock));
-
-	list_foreach(vol_parts, lparts, vol_part_t, part) {
+	fibril_mutex_unlock(&parts->lock);
+	return EOK;
+}
+
+static errno_t vol_part_find_by_id_ref_locked(vol_parts_t *parts,
+    service_id_t sid, vol_part_t **rpart)
+{
+	assert(fibril_mutex_is_locked(&parts->lock));
+
+	list_foreach(parts->parts, lparts, vol_part_t, part) {
 		if (part->svc_id == sid) {
 			/* Add reference */
@@ -464,11 +488,12 @@
 }
 
-errno_t vol_part_find_by_id_ref(service_id_t sid, vol_part_t **rpart)
-{
-	errno_t rc;
-
-	fibril_mutex_lock(&vol_parts_lock);
-	rc = vol_part_find_by_id_ref_locked(sid, rpart);
-	fibril_mutex_unlock(&vol_parts_lock);
+errno_t vol_part_find_by_id_ref(vol_parts_t *parts, service_id_t sid,
+    vol_part_t **rpart)
+{
+	errno_t rc;
+
+	fibril_mutex_lock(&parts->lock);
+	rc = vol_part_find_by_id_ref_locked(parts, sid, rpart);
+	fibril_mutex_unlock(&parts->lock);
 
 	return rc;
@@ -539,5 +564,5 @@
 	log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_mkfs_part()");
 
-	fibril_mutex_lock(&vol_parts_lock);
+	fibril_mutex_lock(&part->parts->lock);
 
 	rc = volsrv_part_mkfs(part->svc_id, fstype, label);
@@ -545,5 +570,5 @@
 		log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_mkfs_part() - failed %s",
 		    str_error(rc));
-		fibril_mutex_unlock(&vol_parts_lock);
+		fibril_mutex_unlock(&part->parts->lock);
 		return rc;
 	}
@@ -556,5 +581,5 @@
 	rc = vol_part_probe(part);
 	if (rc != EOK) {
-		fibril_mutex_unlock(&vol_parts_lock);
+		fibril_mutex_unlock(&part->parts->lock);
 		return rc;
 	}
@@ -562,9 +587,9 @@
 	rc = vol_part_mount(part);
 	if (rc != EOK) {
-		fibril_mutex_unlock(&vol_parts_lock);
+		fibril_mutex_unlock(&part->parts->lock);
 		return rc;
 	}
 
-	fibril_mutex_unlock(&vol_parts_lock);
+	fibril_mutex_unlock(&part->parts->lock);
 	return EOK;
 }
