[Sheepdog] [PATCH] sheepdog: merge sd_writev_bh_cb and sd_readv_bh_cb

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Thu Apr 29 10:32:44 CEST 2010


Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 block/sheepdog.c |  106 +++++++++++++----------------------------------------
 1 files changed, 26 insertions(+), 80 deletions(-)

diff --git a/block/sheepdog.c b/block/sheepdog.c
index b77b249..dc43bd2 100644
--- a/block/sheepdog.c
+++ b/block/sheepdog.c
@@ -654,8 +654,6 @@ out:
 	return ret;
 }
 
-static void sd_write_bh_cb(void *p);
-static void sd_readv_bh_cb(void *p);
 static int add_aio_request(struct bdrv_sd_state *s, struct aio_req *aio_req,
 			   struct iovec *iov, int niov, int create, int write);
 
@@ -1301,10 +1299,10 @@ out:
 	return ret;
 }
 
-static void sd_write_bh_cb(void *p)
+static void sd_readv_writev_bh_cb(void *p)
 {
 	struct sd_aiocb *acb = p;
-	int i, ret = 0;
+	int ret = 0;
 	unsigned long len, done = 0, total = acb->nb_sectors * 512;
 	unsigned long idx = acb->sector_num * 512 / CHUNK_SIZE;
 	uint64_t oid;
@@ -1313,23 +1311,17 @@ static void sd_write_bh_cb(void *p)
 	struct sd_inode *inode = &s->inode;
 	struct aio_req *aio_req;
 
-	if (acb->bh) {
-		qemu_bh_delete(acb->bh);
-		acb->bh = NULL;
-	}
+	qemu_bh_delete(acb->bh);
+	acb->bh = NULL;
 
-	if (!s->is_current) {
+	if (acb->write && !s->is_current) {
 		ret = sd_create_branch(s);
 		if (ret) {
-			ret = -EIO;
-			goto abort;
+			acb->ret = -EIO;
+			goto out;
 		}
 	}
 
-	acb->aio_done_func = sd_write_done;
-	acb->aiocb_type = AIOCB_WRITE_UDATA;
-
-	i = 0;
 	while (done != total) {
 		uint8_t flags = 0;
 		uint64_t old_oid = 0;
@@ -1339,18 +1331,21 @@ static void sd_write_bh_cb(void *p)
 
 		len = min_t(unsigned long, total - done, CHUNK_SIZE - offset);
 
-		if (!oid || !is_data_obj_writeable(inode, idx)) {
+		if (!oid) {
 			if (!acb->write)
 				goto done;
 
 			create = 1;
+		} else if (acb->write && !is_data_obj_writeable(inode, idx)) {
+			create = 1;
+			old_oid = oid;
+			flags = SD_FLAG_CMD_COW;
+		}
+
+		if (create) {
 			dprintf("update ino (%" PRIu64") %"
 				PRIu64 " %" PRIu64 " %" PRIu64 "\n",
 				inode->oid, oid, to_data_oid(inode->oid, idx), idx);
-			if (oid && !is_data_obj_writeable(inode, idx)) {
-				old_oid = oid;
-				flags = SD_FLAG_CMD_COW;
-			}
 			oid = to_data_oid(inode->oid, idx);
 			dprintf("new oid %lx\n", oid);
 		}
@@ -1360,7 +1355,7 @@ static void sd_write_bh_cb(void *p)
 		if (!aio_req) {
 			eprintf("too many requests\n");
 			acb->ret = -EIO;
-			goto abort;
+			goto out;
 		}
 
 		if (create) {
@@ -1374,24 +1369,19 @@ static void sd_write_bh_cb(void *p)
 		}
 
 		ret = add_aio_request(s, aio_req, acb->qiov->iov, acb->qiov->niov,
-				      create, 1);
+				      create, acb->write);
 		if (ret < 0) {
 			eprintf("add_aio_request is faled\n");
 			free_aio_req(s, aio_req);
-			ret = -EIO;
-			goto abort;
+			acb->ret = -EIO;
+			goto out;
 		}
 	done:
-
 		offset = 0;
 		idx++;
 		done += len;
-		i++;
 	}
-
-abort:
-	acb->ret = ret;
-
+out:
 	if (QLIST_EMPTY(&acb->aioreq_head))
 		sd_finish_aiocb(acb);
 }
@@ -1406,59 +1396,13 @@ static BlockDriverAIOCB *sd_aio_writev(BlockDriverState *bs,
 	struct sd_aiocb *acb;
 
 	acb = sd_aio_setup(bs, 1, qiov, sector_num, nb_sectors, cb, opaque);
+	acb->aio_done_func = sd_write_done;
+	acb->aiocb_type = AIOCB_WRITE_UDATA;
 
-	sd_schedule_bh(sd_write_bh_cb, acb);
+	sd_schedule_bh(sd_readv_writev_bh_cb, acb);
 	return &acb->common;
 }
 
-static void sd_readv_bh_cb(void *p)
-{
-	struct sd_aiocb *acb = p;
-	int ret = 0;
-	unsigned long len, done = 0, total = acb->nb_sectors * 512;
-	unsigned long idx = acb->sector_num * 512 / CHUNK_SIZE;
-	uint64_t oid;
-	uint64_t offset = (acb->sector_num * 512) % CHUNK_SIZE;
-	struct bdrv_sd_state *s = acb->common.bs->opaque;
-	struct aio_req *aio_req;
-
-	qemu_bh_delete(acb->bh);
-	acb->bh = NULL;
-
-	acb->aiocb_type = AIOCB_READ_UDATA;
-	acb->aio_done_func = sd_finish_aiocb;
-
-	while (done != total) {
-		oid = s->inode.data_oid[idx];
-
-		len = min_t(unsigned long, total - done, CHUNK_SIZE - offset);
-
-		if (oid) {
-			aio_req = alloc_aio_req(s, acb, oid, len, offset, 0, 0, done);
-			if (!aio_req) {
-				eprintf("too many requests\n");
-				acb->ret = -EIO;
-				goto out;
-			}
-
-			ret = add_aio_request(s, aio_req, NULL, 0, 0, 0);
-			if (ret) {
-				free_aio_req(s, aio_req);
-				acb->ret = -EIO;
-				goto out;
-			}
-		}
-
-		idx++;
-		offset = 0;
-		done += len;
-	}
-
-out:
-	if (QLIST_EMPTY(&acb->aioreq_head))
-		sd_finish_aiocb(acb);
-}
-
 static BlockDriverAIOCB *sd_aio_readv(BlockDriverState *bs,
 				      int64_t sector_num,
 				      QEMUIOVector *qiov,
@@ -1470,6 +1414,8 @@ static BlockDriverAIOCB *sd_aio_readv(BlockDriverState *bs,
 	int i;
 
 	acb = sd_aio_setup(bs, 0, qiov, sector_num, nb_sectors, cb, opaque);
+	acb->aiocb_type = AIOCB_READ_UDATA;
+	acb->aio_done_func = sd_finish_aiocb;
 
 	/*
 	 * TODO: we can do better; we don't need to initialize
@@ -1478,7 +1424,7 @@ static BlockDriverAIOCB *sd_aio_readv(BlockDriverState *bs,
 	for (i = 0; i < qiov->niov; i++)
 		memset(qiov->iov[i].iov_base, 0, qiov->iov[i].iov_len);
 
-	sd_schedule_bh(sd_readv_bh_cb, acb);
+	sd_schedule_bh(sd_readv_writev_bh_cb, acb);
 	return &acb->common;
 }
 
-- 
1.5.6.5




More information about the sheepdog mailing list