Index: uspace/srv/bd/file_bd/file_bd.c
===================================================================
--- uspace/srv/bd/file_bd/file_bd.c	(revision faba83975da4ba5bb605f70d4e02e065f9513e84)
+++ uspace/srv/bd/file_bd/file_bd.c	(revision 4802dd7d8d17cb02ec8f79547ffde538bb8c6736)
@@ -41,7 +41,7 @@
 #include <stdio.h>
 #include <unistd.h>
-#include <ipc/bd.h>
 #include <async.h>
 #include <as.h>
+#include <bd_srv.h>
 #include <fibril_synch.h>
 #include <loc.h>
@@ -62,4 +62,5 @@
 
 static service_id_t service_id;
+static bd_srv_t bd_srv;
 static fibril_mutex_t dev_lock;
 
@@ -67,6 +68,20 @@
 static int file_bd_init(const char *fname);
 static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *);
-static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf);
-static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf);
+
+static int file_bd_open(bd_srv_t *);
+static int file_bd_close(bd_srv_t *);
+static int file_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
+static int file_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
+static int file_bd_get_block_size(bd_srv_t *, size_t *);
+static int file_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
+
+static bd_ops_t file_bd_ops = {
+	.open = file_bd_open,
+	.close = file_bd_close,
+	.read_blocks = file_bd_read_blocks,
+	.write_blocks = file_bd_write_blocks,
+	.get_block_size = file_bd_get_block_size,
+	.get_num_blocks = file_bd_get_num_blocks
+};
 
 int main(int argc, char **argv)
@@ -139,4 +154,7 @@
 static int file_bd_init(const char *fname)
 {
+	bd_srv_init(&bd_srv);
+	bd_srv.ops = &file_bd_ops;
+	
 	async_set_client_connection(file_bd_connection);
 	int rc = loc_server_register(NAME);
@@ -170,79 +188,28 @@
 static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
 {
-	void *fs_va = NULL;
-	ipc_callid_t callid;
-	ipc_call_t call;
-	sysarg_t method;
-	size_t comm_size;
-	unsigned int flags;
-	int retval;
-	uint64_t ba;
-	size_t cnt;
-
-	/* Answer the IPC_M_CONNECT_ME_TO call. */
-	async_answer_0(iid, EOK);
-
-	if (!async_share_out_receive(&callid, &comm_size, &flags)) {
-		async_answer_0(callid, EHANGUP);
-		return;
-	}
-
-	(void) async_share_out_finalize(callid, &fs_va);
-	if (fs_va == AS_MAP_FAILED) {
-		async_answer_0(callid, EHANGUP);
-		return;
-	}
-
-	while (true) {
-		callid = async_get_call(&call);
-		method = IPC_GET_IMETHOD(call);
-		
-		if (!method) {
-			/* The other side has hung up. */
-			async_answer_0(callid, EOK);
-			return;
-		}
-		
-		switch (method) {
-		case BD_READ_BLOCKS:
-			ba = MERGE_LOUP32(IPC_GET_ARG1(call),
-			    IPC_GET_ARG2(call));
-			cnt = IPC_GET_ARG3(call);
-			if (cnt * block_size > comm_size) {
-				retval = ELIMIT;
-				break;
-			}
-			retval = file_bd_read_blocks(ba, cnt, fs_va);
-			break;
-		case BD_WRITE_BLOCKS:
-			ba = MERGE_LOUP32(IPC_GET_ARG1(call),
-			    IPC_GET_ARG2(call));
-			cnt = IPC_GET_ARG3(call);
-			if (cnt * block_size > comm_size) {
-				retval = ELIMIT;
-				break;
-			}
-			retval = file_bd_write_blocks(ba, cnt, fs_va);
-			break;
-		case BD_GET_BLOCK_SIZE:
-			async_answer_1(callid, EOK, block_size);
-			continue;
-		case BD_GET_NUM_BLOCKS:
-			async_answer_2(callid, EOK, LOWER32(num_blocks),
-			    UPPER32(num_blocks));
-			continue;
-		default:
-			retval = EINVAL;
-			break;
-		}
-		async_answer_0(callid, retval);
-	}
+	bd_conn(iid, icall, &bd_srv);
+}
+
+/** Open device. */
+static int file_bd_open(bd_srv_t *bd)
+{
+	return EOK;
+}
+
+/** Close device. */
+static int file_bd_close(bd_srv_t *bd)
+{
+	return EOK;
 }
 
 /** Read blocks from the device. */
-static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf)
+static int file_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
+    size_t size)
 {
 	size_t n_rd;
 	int rc;
+
+	if (size < cnt * block_size)
+		return EINVAL;
 
 	/* Check whether access is within device address bounds. */
@@ -279,8 +246,12 @@
 
 /** Write blocks to the device. */
-static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
+static int file_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
+    const void *buf, size_t size)
 {
 	size_t n_wr;
 	int rc;
+
+	if (size < cnt * block_size)
+		return EINVAL;
 
 	/* Check whether access is within device address bounds. */
@@ -318,4 +289,18 @@
 }
 
+/** Get device block size. */
+static int file_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
+{
+	*rsize = block_size;
+	return EOK;
+}
+
+/** Get number of blocks on device. */
+static int file_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
+{
+	*rnb = num_blocks;
+	return EOK;
+}
+
 /**
  * @}
