[Sheepdog] [PATCH] sheepdog: merge sd_load_vmstate and sd_save_vmstate

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Thu Apr 29 17:54:02 CEST 2010


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

diff --git a/block/sheepdog.c b/block/sheepdog.c
index dc43bd2..b0d0680 100644
--- a/block/sheepdog.c
+++ b/block/sheepdog.c
@@ -76,7 +76,6 @@
 #define SD_RES_VDI_NOT_LOCKED   0x17 /* Vdi is not locked */
 #define SD_RES_SHUTDOWN      0x18 /* Sheepdog is shutting down */
 
-#define VDI_VMSTATE_MASK	0x7fffffff00000000
 #define VDI_VMSTATE_BIT 	0x4000000000000000
 
 #define SD_NR_VDIS   (1U << 24)
@@ -322,6 +321,11 @@ static inline uint64_t to_data_oid(uint64_t vdi_oid, unsigned int idx)
 	return (vdi_oid & ~VDI_BIT) | idx;
 }
 
+static inline uint64_t to_vmstate_oid(uint64_t vdi_oid, unsigned int idx)
+{
+	return (vdi_oid & ~VDI_BIT) | VDI_VMSTATE_BIT | idx;
+}
+
 /*
  * 64 bit FNV-1a non-zero initial basis
  */
@@ -970,27 +974,6 @@ static int read_vdi_obj(char *buf, uint64_t oid, int *copies)
 	}
 }
 
-static uint32_t get_vmstate_index(uint64_t pos)
-{
-	return pos / SD_DATA_OBJ_SIZE;
-}
-
-static uint64_t get_vmstate_offset(uint64_t pos)
-{
-	return pos % SD_DATA_OBJ_SIZE;
-}
-
-static uint64_t get_vmstate_oid(uint64_t oid, uint32_t vdi_index)
-{
-	uint64_t vmstate_oid;
-
-	vmstate_oid = VDI_VMSTATE_MASK & oid;
-	vmstate_oid |= VDI_VMSTATE_BIT;
-	vmstate_oid |= vdi_index;
-
-	return vmstate_oid;
-}
-
 /* TODO: error cleanups */
 static int sd_open(BlockDriverState *bs, const char *filename, int flags)
 {
@@ -1683,14 +1666,13 @@ out:
 	return found;
 }
 
-static int sd_save_vmstate(BlockDriverState *bs, const uint8_t *data,
-				int64_t pos, int size)
+static int do_load_save_vmstate(struct bdrv_sd_state *s, uint8_t *data,
+				int64_t pos, int size, int load)
 {
-	struct bdrv_sd_state *s = bs->opaque;
 	struct sd_obj_req hdr;
 	int fd;
 	int ret = 0;
-	unsigned int rlen = 0, wlen = 0;
+	unsigned int rlen = 0, wlen = 0, data_len;
 	uint64_t vmstate_oid;
 	uint32_t vdi_index;
 	uint64_t offset;
@@ -1702,100 +1684,62 @@ static int sd_save_vmstate(BlockDriverState *bs, const uint8_t *data,
 	}
 
 	while (size) {
-		vdi_index = get_vmstate_index(pos);
-		offset = get_vmstate_offset(pos);
+		vdi_index = pos / SD_DATA_OBJ_SIZE;
+		offset = pos % SD_DATA_OBJ_SIZE;
 
-		if (size > SD_DATA_OBJ_SIZE)
-			wlen = SD_DATA_OBJ_SIZE;
-		else
-			wlen = size;
+		data_len = min_t(unsigned int, size, SD_DATA_OBJ_SIZE);
 
-		vmstate_oid = get_vmstate_oid(s->inode.oid, vdi_index);
+		vmstate_oid = to_vmstate_oid(s->inode.oid, vdi_index);
 
 		memset(&hdr, 0, sizeof(hdr));
-		if (offset)
-			hdr.opcode = SD_OP_WRITE_OBJ;
-		else
-			hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
-
+		if (load) {
+			hdr.opcode = SD_OP_READ_OBJ;
+			wlen = 0;
+			rlen = data_len;
+			hdr.flags = 0;
+		} else {
+			if (offset)
+				hdr.opcode = SD_OP_WRITE_OBJ;
+			else
+				hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
+			hdr.flags = SD_FLAG_CMD_WRITE;
+			wlen = data_len;
+			rlen = 0;
+		}
 		hdr.oid = vmstate_oid;
-		hdr.cow_oid = 0;
 		hdr.copies = s->inode.nr_copies;
-
-		hdr.flags |= SD_FLAG_CMD_WRITE;
-		hdr.data_length = wlen;
+		hdr.data_length = data_len;
 		hdr.offset = offset;
 
-		rlen = 0;
-
-		if (do_req(fd, (struct sd_req *)&hdr, (char *)data, &wlen, &rlen) < 0) {
+		if (do_req(fd, (struct sd_req *)&hdr, data, &wlen, &rlen) < 0) {
 			eprintf("failed to save vmstate %m\n");
 			ret = -EIO;
 			goto cleanup;
 		}
 
-		pos += wlen;
-		size -= wlen;
-		ret += wlen;
+		pos += data_len;
+		size -= data_len;
+		ret += data_len;
 	}
-
 cleanup:
 	close(fd);
 	return ret;
 }
 
-static int sd_load_vmstate(BlockDriverState *bs, uint8_t *data,
-				int64_t pos, int size)
+static int sd_save_vmstate(BlockDriverState *bs, const uint8_t *data,
+			   int64_t pos, int size)
 {
 	struct bdrv_sd_state *s = bs->opaque;
-	int ret = 0;
-	int fd;
-	unsigned int rlen = 0, wlen = 0;
-	struct sd_obj_req hdr;
-	uint64_t vmstate_oid;
-	uint32_t vdi_index;
-	uint64_t offset;
 
-	fd = connect_to_vost();
-	if (fd < 0) {
-		ret = -EIO;
-		goto cleanup;
-	}
-
-	while (size) {
-		vdi_index = get_vmstate_index(pos);
-		offset = get_vmstate_offset(pos);
-
-		if (size > SD_DATA_OBJ_SIZE)
-			rlen = SD_DATA_OBJ_SIZE;
-		else
-			rlen = size;
-
-		vmstate_oid = get_vmstate_oid(s->inode.oid, vdi_index);
-
-		memset(&hdr, 0, sizeof(hdr));
-
-		hdr.opcode = SD_OP_READ_OBJ;
-		hdr.oid = vmstate_oid;
-		hdr.data_length = rlen;
-		hdr.offset = offset;
-
-		wlen = 0;
-
-		if (do_req(fd, (struct sd_req *)&hdr, data, &wlen, &rlen) < 0) {
-			eprintf("illegal vmstate %m\n");
-			ret = -EIO;
-			goto cleanup;
-		}
+	return do_load_save_vmstate(s, (uint8_t *)data, pos, size, 0);
+}
 
-		pos += rlen;
-		size -= rlen;
-		ret += rlen;
-	}
+static int sd_load_vmstate(BlockDriverState *bs, uint8_t *data,
+			   int64_t pos, int size)
+{
+	struct bdrv_sd_state *s = bs->opaque;
 
-cleanup:
-	close(fd);
-	return ret;
+	return do_load_save_vmstate(s, data, pos, size, 1);
 }
 
 
@@ -1831,7 +1775,6 @@ BlockDriver bdrv_sheepdog = {
 	.bdrv_snapshot_delete   = sd_snapshot_delete,
 	.bdrv_snapshot_list = sd_snapshot_list,
 
-
 	.bdrv_save_vmstate    = sd_save_vmstate,
 	.bdrv_load_vmstate    = sd_load_vmstate,
 
-- 
1.5.6.5




More information about the sheepdog mailing list