Index: uspace/lib/block/libblock.c
===================================================================
--- uspace/lib/block/libblock.c	(revision 08cba4b8a94988e64d6efb79b32a3f28694a26e9)
+++ uspace/lib/block/libblock.c	(revision affaf2e32d63acee2b44deef78a9bc2876c0cde3)
@@ -40,5 +40,4 @@
 #include "../../srv/vfs/vfs.h"
 #include <ipc/loc.h>
-#include <ipc/bd.h>
 #include <ipc/services.h>
 #include <errno.h>
@@ -47,4 +46,5 @@
 #include <as.h>
 #include <assert.h>
+#include <bd.h>
 #include <fibril_synch.h>
 #include <adt/list.h>
@@ -80,7 +80,5 @@
 	service_id_t service_id;
 	async_sess_t *sess;
-	fibril_mutex_t comm_area_lock;
-	void *comm_area;
-	size_t comm_size;
+	bd_t *bd;
 	void *bb_buf;
 	aoff64_t bb_addr;
@@ -89,8 +87,6 @@
 } devcon_t;
 
-static int read_blocks(devcon_t *, aoff64_t, size_t);
-static int write_blocks(devcon_t *, aoff64_t, size_t);
-static int get_block_size(async_sess_t *, size_t *);
-static int get_num_blocks(async_sess_t *, aoff64_t *);
+static int read_blocks(devcon_t *, aoff64_t, size_t, void *, size_t);
+static int write_blocks(devcon_t *, aoff64_t, size_t, void *, size_t);
 static aoff64_t ba_ltop(devcon_t *, aoff64_t);
 
@@ -112,10 +108,7 @@
 
 static int devcon_add(service_id_t service_id, async_sess_t *sess,
-    size_t bsize, void *comm_area, size_t comm_size)
+    size_t bsize, bd_t *bd)
 {
 	devcon_t *devcon;
-	
-	if (comm_size < bsize)
-		return EINVAL;
 	
 	devcon = malloc(sizeof(devcon_t));
@@ -126,7 +119,5 @@
 	devcon->service_id = service_id;
 	devcon->sess = sess;
-	fibril_mutex_initialize(&devcon->comm_area_lock);
-	devcon->comm_area = comm_area;
-	devcon->comm_size = comm_size;
+	devcon->bd = bd;
 	devcon->bb_buf = NULL;
 	devcon->bb_addr = 0;
@@ -158,23 +149,14 @@
     size_t comm_size)
 {
-	void *comm_area = mmap(NULL, comm_size, PROTO_READ | PROTO_WRITE,
-	    MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
-	if (!comm_area)
-		return ENOMEM;
-	
+	bd_t *bd;
+
 	async_sess_t *sess = loc_service_connect(mgmt, service_id,
 	    IPC_FLAG_BLOCKING);
 	if (!sess) {
-		munmap(comm_area, comm_size);
 		return ENOENT;
 	}
 	
-	async_exch_t *exch = async_exchange_begin(sess);
-	int rc = async_share_out_start(exch, comm_area,
-	    AS_AREA_READ | AS_AREA_WRITE);
-	async_exchange_end(exch);
-	
+	int rc = bd_open(sess, &bd);
 	if (rc != EOK) {
-		munmap(comm_area, comm_size);
 		async_hangup(sess);
 		return rc;
@@ -182,15 +164,14 @@
 	
 	size_t bsize;
-	rc = get_block_size(sess, &bsize);
-	
+	rc = bd_get_block_size(bd, &bsize);
 	if (rc != EOK) {
-		munmap(comm_area, comm_size);
+		bd_close(bd);
 		async_hangup(sess);
 		return rc;
 	}
 	
-	rc = devcon_add(service_id, sess, bsize, comm_area, comm_size);
+	rc = devcon_add(service_id, sess, bsize, bd);
 	if (rc != EOK) {
-		munmap(comm_area, comm_size);
+		bd_close(bd);
 		async_hangup(sess);
 		return rc;
@@ -213,5 +194,5 @@
 		free(devcon->bb_buf);
 	
-	munmap(devcon->comm_area, devcon->comm_size);
+	bd_close(devcon->bd);
 	async_hangup(devcon->sess);
 	
@@ -233,13 +214,9 @@
 		return ENOMEM;
 
-	fibril_mutex_lock(&devcon->comm_area_lock);
-	rc = read_blocks(devcon, 0, 1);
+	rc = read_blocks(devcon, 0, 1, bb_buf, devcon->pblock_size);
 	if (rc != EOK) {
-		fibril_mutex_unlock(&devcon->comm_area_lock);
 	    	free(bb_buf);
 		return rc;
 	}
-	memcpy(bb_buf, devcon->comm_area, devcon->pblock_size);
-	fibril_mutex_unlock(&devcon->comm_area_lock);
 
 	devcon->bb_buf = bb_buf;
@@ -338,6 +315,6 @@
 		list_remove(&b->free_link);
 		if (b->dirty) {
-			memcpy(devcon->comm_area, b->data, b->size);
-			rc = write_blocks(devcon, b->pba, cache->blocks_cluster);
+			rc = write_blocks(devcon, b->pba, cache->blocks_cluster,
+			    b->data, b->size);
 			if (rc != EOK)
 				return rc;
@@ -481,9 +458,6 @@
 				list_append(&b->free_link, &cache->free_list);
 				fibril_mutex_unlock(&cache->lock);
-				fibril_mutex_lock(&devcon->comm_area_lock);
-				memcpy(devcon->comm_area, b->data, b->size);
 				rc = write_blocks(devcon, b->pba,
-				    cache->blocks_cluster);
-				fibril_mutex_unlock(&devcon->comm_area_lock);
+				    cache->blocks_cluster, b->data, b->size);
 				if (rc != EOK) {
 					/*
@@ -555,8 +529,6 @@
 			 * the new contents from the device.
 			 */
-			fibril_mutex_lock(&devcon->comm_area_lock);
-			rc = read_blocks(devcon, b->pba, cache->blocks_cluster);
-			memcpy(b->data, devcon->comm_area, cache->lblock_size);
-			fibril_mutex_unlock(&devcon->comm_area_lock);
+			rc = read_blocks(devcon, b->pba, cache->blocks_cluster,
+			    b->data, cache->lblock_size);
 			if (rc != EOK) 
 				b->toxic = true;
@@ -616,8 +588,6 @@
 	if (block->dirty && (block->refcnt == 1) &&
 	    (blocks_cached > CACHE_HI_WATERMARK || mode != CACHE_MODE_WB)) {
-		fibril_mutex_lock(&devcon->comm_area_lock);
-		memcpy(devcon->comm_area, block->data, block->size);
-		rc = write_blocks(devcon, block->pba, cache->blocks_cluster);
-		fibril_mutex_unlock(&devcon->comm_area_lock);
+		rc = write_blocks(devcon, block->pba, cache->blocks_cluster,
+		    block->data, block->size);
 		block->dirty = false;
 	}
@@ -688,4 +658,5 @@
  *
  * @param service_id	Service ID of the block device.
+ * @param buf		Buffer for holding one block
  * @param bufpos	Pointer to the first unread valid offset within the
  * 			communication buffer.
@@ -699,6 +670,6 @@
  * @return		EOK on success or a negative return code on failure.
  */
-int block_seqread(service_id_t service_id, size_t *bufpos, size_t *buflen,
-    aoff64_t *pos, void *dst, size_t size)
+int block_seqread(service_id_t service_id, void *buf, size_t *bufpos,
+    size_t *buflen, aoff64_t *pos, void *dst, size_t size)
 {
 	size_t offset = 0;
@@ -711,5 +682,4 @@
 	block_size = devcon->pblock_size;
 	
-	fibril_mutex_lock(&devcon->comm_area_lock);
 	while (left > 0) {
 		size_t rd;
@@ -725,5 +695,5 @@
 			 * destination buffer.
 			 */
-			memcpy(dst + offset, devcon->comm_area + *bufpos, rd);
+			memcpy(dst + offset, buf + *bufpos, rd);
 			offset += rd;
 			*bufpos += rd;
@@ -736,7 +706,7 @@
 			int rc;
 
-			rc = read_blocks(devcon, *pos / block_size, 1);
+			rc = read_blocks(devcon, *pos / block_size, 1, buf,
+			    devcon->pblock_size);
 			if (rc != EOK) {
-				fibril_mutex_unlock(&devcon->comm_area_lock);
 				return rc;
 			}
@@ -746,5 +716,4 @@
 		}
 	}
-	fibril_mutex_unlock(&devcon->comm_area_lock);
 	
 	return EOK;
@@ -763,18 +732,9 @@
 {
 	devcon_t *devcon;
-	int rc;
 
 	devcon = devcon_search(service_id);
 	assert(devcon);
-	
-	fibril_mutex_lock(&devcon->comm_area_lock);
-
-	rc = read_blocks(devcon, ba, cnt);
-	if (rc == EOK)
-		memcpy(buf, devcon->comm_area, devcon->pblock_size * cnt);
-
-	fibril_mutex_unlock(&devcon->comm_area_lock);
-
-	return rc;
+
+	return read_blocks(devcon, ba, cnt, buf, devcon->pblock_size * cnt);
 }
 
@@ -792,17 +752,9 @@
 {
 	devcon_t *devcon;
-	int rc;
 
 	devcon = devcon_search(service_id);
 	assert(devcon);
-	
-	fibril_mutex_lock(&devcon->comm_area_lock);
-
-	memcpy(devcon->comm_area, data, devcon->pblock_size * cnt);
-	rc = write_blocks(devcon, ba, cnt);
-
-	fibril_mutex_unlock(&devcon->comm_area_lock);
-
-	return rc;
+
+	return write_blocks(devcon, ba, cnt, (void *)data, devcon->pblock_size * cnt);
 }
 
@@ -820,6 +772,6 @@
 	devcon = devcon_search(service_id);
 	assert(devcon);
-	
-	return get_block_size(devcon->sess, bsize);
+
+	return bd_get_block_size(devcon->bd, bsize);
 }
 
@@ -836,5 +788,5 @@
 	assert(devcon);
 	
-	return get_num_blocks(devcon->sess, nblocks);
+	return bd_get_num_blocks(devcon->bd, nblocks);
 }
 
@@ -887,5 +839,5 @@
 	memcpy(data, buffer + offset, bytes);
 	free(buffer);
-	
+
 	return EOK;
 }
@@ -903,25 +855,18 @@
 {
 	devcon_t *devcon = devcon_search(service_id);
-	assert(devcon);
-	
 	toc_block_t *toc = NULL;
-	
-	fibril_mutex_lock(&devcon->comm_area_lock);
-	
-	async_exch_t *exch = async_exchange_begin(devcon->sess);
-	int rc = async_req_1_0(exch, BD_READ_TOC, session);
-	async_exchange_end(exch);
-	
-	if (rc == EOK) {
-		toc = (toc_block_t *) malloc(sizeof(toc_block_t));
-		if (toc != NULL) {
-			memset(toc, 0, sizeof(toc_block_t));
-			memcpy(toc, devcon->comm_area,
-			    min(devcon->pblock_size, sizeof(toc_block_t)));
-		}
-	}
-	
-	
-	fibril_mutex_unlock(&devcon->comm_area_lock);
+	int rc;
+	
+	assert(devcon);
+	
+	toc = (toc_block_t *) malloc(sizeof(toc_block_t));
+	if (toc == NULL)
+		return NULL;
+	
+	rc = bd_read_toc(devcon->bd, session, toc, sizeof(toc_block_t));
+	if (rc != EOK) {
+		free(toc);
+		return NULL;
+	}
 	
 	return toc;
@@ -937,13 +882,10 @@
  * @return		EOK on success or negative error code on failure.
  */
-static int read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt)
-{
-	assert(devcon);
-	
-	async_exch_t *exch = async_exchange_begin(devcon->sess);
-	int rc = async_req_3_0(exch, BD_READ_BLOCKS, LOWER32(ba),
-	    UPPER32(ba), cnt);
-	async_exchange_end(exch);
-	
+static int read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt, void *buf,
+    size_t size)
+{
+	assert(devcon);
+	
+	int rc = bd_read_blocks(devcon->bd, ba, cnt, buf, size);
 	if (rc != EOK) {
 		printf("Error %d reading %zu blocks starting at block %" PRIuOFF64
@@ -967,13 +909,10 @@
  * @return		EOK on success or negative error code on failure.
  */
-static int write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt)
-{
-	assert(devcon);
-	
-	async_exch_t *exch = async_exchange_begin(devcon->sess);
-	int rc = async_req_3_0(exch, BD_WRITE_BLOCKS, LOWER32(ba),
-	    UPPER32(ba), cnt);
-	async_exchange_end(exch);
-	
+static int write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt, void *data,
+    size_t size)
+{
+	assert(devcon);
+	
+	int rc = bd_write_blocks(devcon->bd, ba, cnt, data, size);
 	if (rc != EOK) {
 		printf("Error %d writing %zu blocks starting at block %" PRIuOFF64
@@ -987,35 +926,4 @@
 }
 
-/** Get block size used by the device. */
-static int get_block_size(async_sess_t *sess, size_t *bsize)
-{
-	sysarg_t bs;
-	
-	async_exch_t *exch = async_exchange_begin(sess);
-	int rc = async_req_0_1(exch, BD_GET_BLOCK_SIZE, &bs);
-	async_exchange_end(exch);
-	
-	if (rc == EOK)
-		*bsize = (size_t) bs;
-	
-	return rc;
-}
-
-/** Get total number of blocks on block device. */
-static int get_num_blocks(async_sess_t *sess, aoff64_t *nblocks)
-{
-	sysarg_t nb_l;
-	sysarg_t nb_h;
-	
-	async_exch_t *exch = async_exchange_begin(sess);
-	int rc = async_req_0_2(exch, BD_GET_NUM_BLOCKS, &nb_l, &nb_h);
-	async_exchange_end(exch);
-	
-	if (rc == EOK)
-		*nblocks = (aoff64_t) MERGE_LOUP32(nb_l, nb_h);
-	
-	return rc;
-}
-
 /** Convert logical block address to physical block address. */
 static aoff64_t ba_ltop(devcon_t *devcon, aoff64_t lba)
