Enforce that all internal commands have the SD_SHEEP_PROTO_VER protocol
version, and any external command that has a protocol version has the
right (0x01) one.  Also bump SD_SHEEP_PROTO_VER to 0x05 for the upcoming
0.4.0 release which has lots of internal protocol changes.

Signed-off-by: Christoph Hellwig <hch@lst.de>

---
 collie/cluster.c         |   38 +++++++++++---------------------------
 collie/collie.c          |    3 +--
 collie/common.c          |   15 ++++++---------
 collie/debug.c           |    7 ++-----
 collie/node.c            |    8 ++------
 collie/vdi.c             |   31 +++++++++++--------------------
 include/internal_proto.h |    7 ++++---
 include/sheep.h          |    7 +++++++
 sheep/gateway.c          |    3 +++
 sheep/group.c            |    3 +--
 sheep/object_cache.c     |   16 +++++++++-------
 sheep/ops.c              |    6 +-----
 sheep/recovery.c         |    6 ++----
 sheep/sdnet.c            |   17 +++++++++++++++++
 sheep/store.c            |   15 +++++++--------
 15 files changed, 84 insertions(+), 98 deletions(-)

Index: sheepdog/include/internal_proto.h
===================================================================
--- sheepdog.orig/include/internal_proto.h	2012-07-06 13:38:49.523234843 +0200
+++ sheepdog/include/internal_proto.h	2012-07-06 13:39:15.069901357 +0200
@@ -19,7 +19,7 @@
 
 #include <stdint.h>
 
-#define SD_SHEEP_PROTO_VER 0x04
+#define SD_SHEEP_PROTO_VER 0x05
 
 #define SD_DEFAULT_REDUNDANCY 3
 #define SD_MAX_REDUNDANCY 8
@@ -31,8 +31,9 @@
 
 /*
  * Operations with opcodes above 0x80 are considered part of the inter-sheep
- * protocol and will in the near future be versioned independently of the
- * external sheepdog protocol.
+ * protocol and are versioned using SD_SHEEP_PROTO_VER instead of SD_PROTO_VER.
+ *
+ * These same applies for the above 0x80 flags and error values below.
  */
 #define SD_OP_DEL_VDI        0x81
 #define SD_OP_GET_NODE_LIST  0x82
Index: sheepdog/sheep/ops.c
===================================================================
--- sheepdog.orig/sheep/ops.c	2012-07-06 13:37:53.256568513 +0200
+++ sheepdog/sheep/ops.c	2012-07-06 13:39:15.069901357 +0200
@@ -162,9 +162,6 @@ static int cluster_get_vdi_info(struct r
 	void *tag;
 	int ret;
 
-	if (hdr->proto_ver != SD_PROTO_VER)
-		return SD_RES_VER_MISMATCH;
-
 	if (hdr->data_length == SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN)
 		tag = (char *)req->data + SD_MAX_VDI_LEN;
 	else if (hdr->data_length == SD_MAX_VDI_LEN)
@@ -608,8 +605,7 @@ static int read_copy_from_replica(struct
 		rlen = SD_DATA_OBJ_SIZE;
 		wlen = 0;
 
-		memset(&hdr, 0, sizeof(hdr));
-		hdr.opcode = SD_OP_READ_PEER;
+		sd_init_req(&hdr, SD_OP_READ_PEER);
 		hdr.epoch = epoch;
 		hdr.data_length = rlen;
 
Index: sheepdog/sheep/sdnet.c
===================================================================
--- sheepdog.orig/sheep/sdnet.c	2012-07-06 13:37:52.683235184 +0200
+++ sheepdog/sheep/sdnet.c	2012-07-06 13:39:15.069901357 +0200
@@ -296,6 +296,23 @@ static void queue_request(struct request
 	struct sd_req *hdr = &req->rq;
 	struct sd_rsp *rsp = &req->rp;
 
+	/*
+	 * Check the protocol version for all internal commands, and public
+	 * commands that have it set.  We can't enforce it on all public
+	 * ones as it isn't a mandatory part of the public protocol.
+	 */
+	if (hdr->opcode >= 0x80) {
+		if (hdr->proto_ver != SD_SHEEP_PROTO_VER) {
+			rsp->result = SD_RES_VER_MISMATCH;
+			goto done;
+		}
+	} else if (hdr->proto_ver) {
+		if (hdr->proto_ver != SD_PROTO_VER) {
+			rsp->result = SD_RES_VER_MISMATCH;
+			goto done;
+		}
+	}
+
 	req->op = get_sd_op(hdr->opcode);
 	if (!req->op) {
 		eprintf("invalid opcode %d\n", hdr->opcode);
Index: sheepdog/collie/cluster.c
===================================================================
--- sheepdog.orig/collie/cluster.c	2012-07-06 13:37:11.516568762 +0200
+++ sheepdog/collie/cluster.c	2012-07-06 13:39:15.069901357 +0200
@@ -45,11 +45,10 @@ static int list_store(void)
 	if (fd < 0)
 		return EXIT_SYSFAIL;
 
-	memset(&hdr, 0, sizeof(hdr));
-
 	wlen = 0;
 	rlen = 512;
-	hdr.opcode = SD_OP_GET_STORE_LIST;
+
+	sd_init_req(&hdr, SD_OP_GET_STORE_LIST);
 	hdr.data_length = rlen;
 
 	ret = exec_req(fd, &hdr, buf, &wlen, &rlen);
@@ -87,9 +86,7 @@ static int cluster_format(int argc, char
 
 	gettimeofday(&tv, NULL);
 
-	memset(&hdr, 0, sizeof(hdr));
-
-	hdr.opcode = SD_OP_MAKE_FS;
+	sd_init_req((struct sd_req *)&hdr, SD_OP_MAKE_FS);
 	hdr.copies = cluster_cmd_data.copies;
 	if (cluster_cmd_data.nohalt)
 		set_nohalt(&hdr.flags);
@@ -149,9 +146,7 @@ again:
 	if (fd < 0)
 		goto error;
 
-	memset(&hdr, 0, sizeof(hdr));
-
-	hdr.opcode = SD_OP_STAT_CLUSTER;
+	sd_init_req(&hdr, SD_OP_STAT_CLUSTER);
 	hdr.epoch = sd_epoch;
 	hdr.data_length = log_length;
 
@@ -220,9 +215,7 @@ static int cluster_shutdown(int argc, ch
 	if (fd < 0)
 		return EXIT_SYSFAIL;
 
-	memset(&hdr, 0, sizeof(hdr));
-
-	hdr.opcode = SD_OP_SHUTDOWN;
+	sd_init_req(&hdr, SD_OP_SHUTDOWN);
 	hdr.epoch = sd_epoch;
 
 	rlen = 0;
@@ -255,9 +248,7 @@ static int restore_snap(uint32_t epoch)
 	if (fd < 0)
 		return EXIT_SYSFAIL;
 
-	memset(&hdr, 0, sizeof(hdr));
-
-	hdr.opcode = SD_OP_RESTORE;
+	sd_init_req(&hdr, SD_OP_RESTORE);
 	hdr.obj.tgt_epoch = epoch;
 
 	rlen = 0;
@@ -309,11 +300,10 @@ static int list_snap(void)
 	if (fd < 0)
 		goto out;
 
-	memset(&hdr, 0, sizeof(hdr));
-
 	wlen = 0;
 	rlen = SD_DATA_OBJ_SIZE;
-	hdr.opcode = SD_OP_GET_SNAP_FILE;
+
+	sd_init_req(&hdr, SD_OP_GET_SNAP_FILE);
 	hdr.data_length = rlen;
 
 	ret = exec_req(fd, &hdr, buf, &wlen, &rlen);
@@ -348,9 +338,7 @@ static int do_snapshot(void)
 	if (fd < 0)
 		return EXIT_SYSFAIL;
 
-	memset(&hdr, 0, sizeof(hdr));
-
-	hdr.opcode = SD_OP_SNAPSHOT;
+	sd_init_req(&hdr, SD_OP_SNAPSHOT);
 
 	rlen = 0;
 	wlen = 0;
@@ -394,9 +382,7 @@ static int cluster_cleanup(int argc, cha
 	if (fd < 0)
 		return EXIT_SYSFAIL;
 
-	memset(&hdr, 0, sizeof(hdr));
-
-	hdr.opcode = SD_OP_CLEANUP;
+	sd_init_req(&hdr, SD_OP_CLEANUP);
 
 	rlen = 0;
 	wlen = 0;
@@ -450,9 +436,7 @@ static int cluster_recover(int argc, cha
 			return EXIT_SUCCESS;
 	}
 
-	memset(&hdr, 0, sizeof(hdr));
-
-	hdr.opcode = SD_OP_RECOVER;
+	sd_init_req(&hdr, SD_OP_RECOVER);
 	hdr.epoch = sd_epoch;
 
 	rlen = 0;
Index: sheepdog/collie/collie.c
===================================================================
--- sheepdog.orig/collie/collie.c	2012-07-06 13:37:11.516568762 +0200
+++ sheepdog/collie/collie.c	2012-07-06 13:39:15.073234690 +0200
@@ -80,8 +80,7 @@ static int update_node_list(int max_node
 		goto out;
 	}
 
-	memset(&hdr, 0, sizeof(hdr));
-	hdr.opcode = SD_OP_GET_NODE_LIST;
+	sd_init_req((struct sd_req *)&hdr, SD_OP_GET_NODE_LIST);
 	hdr.request_ver = epoch;
 
 	hdr.data_length = size;
Index: sheepdog/collie/common.c
===================================================================
--- sheepdog.orig/collie/common.c	2012-07-06 13:38:25.213234989 +0200
+++ sheepdog/collie/common.c	2012-07-06 13:39:15.073234690 +0200
@@ -56,9 +56,8 @@ int sd_read_object(uint64_t oid, void *d
 		return SD_RES_EIO;
 	}
 
-	memset(&hdr, 0, sizeof(hdr));
+	sd_init_req(&hdr, SD_OP_READ_OBJ);
 	hdr.epoch = sd_epoch;
-	hdr.opcode = SD_OP_READ_OBJ;
 	hdr.data_length = rlen;
 
 	hdr.obj.oid = oid;
@@ -95,12 +94,12 @@ int sd_write_object(uint64_t oid, uint64
 		return SD_RES_EIO;
 	}
 
-	memset(&hdr, 0, sizeof(hdr));
-	hdr.epoch = sd_epoch;
 	if (create)
-		hdr.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
+		sd_init_req(&hdr, SD_OP_CREATE_AND_WRITE_OBJ);
 	else
-		hdr.opcode = SD_OP_WRITE_OBJ;
+		sd_init_req(&hdr, SD_OP_WRITE_OBJ);
+
+	hdr.epoch = sd_epoch;
 	hdr.data_length = wlen;
 	hdr.flags = flags | SD_FLAG_CMD_WRITE;
 
@@ -140,9 +139,7 @@ int parse_vdi(vdi_parser_func_t func, si
 		return fd;
 	}
 
-	memset(&req, 0, sizeof(req));
-
-	req.opcode = SD_OP_READ_VDIS;
+	sd_init_req(&req, SD_OP_READ_VDIS);
 	req.data_length = sizeof(vdi_inuse);
 	req.epoch = sd_epoch;
 
Index: sheepdog/collie/debug.c
===================================================================
--- sheepdog.orig/collie/debug.c	2012-07-06 13:37:11.516568762 +0200
+++ sheepdog/collie/debug.c	2012-07-06 13:39:15.073234690 +0200
@@ -45,8 +45,7 @@ static int do_trace_cat(void)
 	if (fd < 0)
 		return EXIT_SYSFAIL;
 
-	memset(&hdr, 0, sizeof(hdr));
-	hdr.opcode = SD_OP_TRACE_CAT;
+	sd_init_req(&hdr, SD_OP_TRACE_CAT);
 	hdr.data_length = rlen = TRACE_BUF_LEN;
 	hdr.epoch = sd_epoch;
 
@@ -102,9 +101,7 @@ static int debug_trace(int argc, char **
 	if (fd < 0)
 		return EXIT_SYSFAIL;
 
-	memset(&hdr, 0, sizeof(hdr));
-
-	hdr.opcode = SD_OP_TRACE;
+	sd_init_req(&hdr, SD_OP_TRACE);
 	hdr.epoch = sd_epoch;
 	hdr.data_length = enabled;
 
Index: sheepdog/collie/node.c
===================================================================
--- sheepdog.orig/collie/node.c	2012-07-06 13:37:11.516568762 +0200
+++ sheepdog/collie/node.c	2012-07-06 13:39:15.073234690 +0200
@@ -73,9 +73,7 @@ static int node_info(int argc, char **ar
 		if (fd < 0)
 			return 1;
 
-		memset(&req, 0, sizeof(req));
-
-		req.opcode = SD_OP_STAT_SHEEP;
+		sd_init_req((struct sd_req *)&req, SD_OP_STAT_SHEEP);
 		req.epoch = sd_epoch;
 
 		wlen = 0;
@@ -140,9 +138,7 @@ static int node_recovery(int argc, char
 		if (fd < 0)
 			return EXIT_FAILURE;
 
-		memset(&req, 0, sizeof(req));
-
-		req.opcode = SD_OP_STAT_RECOVERY;
+		sd_init_req((struct sd_req *)&req, SD_OP_STAT_RECOVERY);
 
 		wlen = 0;
 		rlen = 0;
Index: sheepdog/collie/vdi.c
===================================================================
--- sheepdog.orig/collie/vdi.c	2012-07-06 13:37:11.516568762 +0200
+++ sheepdog/collie/vdi.c	2012-07-06 13:39:15.073234690 +0200
@@ -280,9 +280,7 @@ static void parse_objs(uint64_t oid, obj
 		if (fd < 0)
 			break;
 
-		memset(&hdr, 0, sizeof(hdr));
-
-		hdr.opcode = SD_OP_READ_PEER;
+		sd_init_req(&hdr, SD_OP_READ_PEER);
 		hdr.data_length = rlen;
 		hdr.flags = 0;
 		hdr.epoch = sd_epoch;
@@ -371,11 +369,10 @@ static int find_vdi_name(char *vdiname,
 	strncpy(buf, vdiname, SD_MAX_VDI_LEN);
 	strncpy(buf + SD_MAX_VDI_LEN, tag, SD_MAX_VDI_TAG_LEN);
 
-	memset(&hdr, 0, sizeof(hdr));
 	if (for_snapshot)
-		hdr.opcode = SD_OP_GET_VDI_INFO;
+		sd_init_req(&hdr, SD_OP_GET_VDI_INFO);
 	else
-		hdr.opcode = SD_OP_LOCK_VDI;
+		sd_init_req(&hdr, SD_OP_LOCK_VDI);
 	wlen = SD_MAX_VDI_LEN + SD_MAX_VDI_TAG_LEN;
 	hdr.proto_ver = SD_PROTO_VER;
 	hdr.data_length = wlen;
@@ -422,8 +419,7 @@ static int do_vdi_create(char *vdiname,
 
 	wlen = SD_MAX_VDI_LEN;
 
-	memset(&hdr, 0, sizeof(hdr));
-	hdr.opcode = SD_OP_NEW_VDI;
+	sd_init_req(&hdr, SD_OP_NEW_VDI);
 	hdr.flags = SD_FLAG_CMD_WRITE;
 	hdr.data_length = wlen;
 
@@ -709,12 +705,10 @@ static int vdi_delete(int argc, char **a
 	if (fd < 0)
 		return EXIT_SYSFAIL;
 
-	memset(&hdr, 0, sizeof(hdr));
-
 	rlen = 0;
 	wlen = sizeof(vdiname);
 
-	hdr.opcode = SD_OP_DEL_VDI;
+	sd_init_req(&hdr, SD_OP_DEL_VDI);
 	hdr.epoch = sd_epoch;
 	hdr.flags = SD_FLAG_CMD_WRITE;
 	hdr.data_length = wlen;
@@ -828,9 +822,7 @@ again:
 	if (fd < 0)
 		goto error;
 
-	memset(&hdr, 0, sizeof(hdr));
-
-	hdr.opcode = SD_OP_STAT_CLUSTER;
+	sd_init_req(&hdr, SD_OP_STAT_CLUSTER);
 	hdr.epoch = sd_epoch;
 	hdr.data_length = log_length;
 
@@ -952,8 +944,7 @@ static int find_vdi_attr_oid(char *vdina
 		return SD_RES_EIO;
 	}
 
-	memset(&hdr, 0, sizeof(hdr));
-	hdr.opcode = SD_OP_GET_VDI_ATTR;
+	sd_init_req(&hdr, SD_OP_GET_VDI_ATTR);
 	wlen = SD_ATTR_OBJ_SIZE;
 	rlen = 0;
 	hdr.proto_ver = SD_PROTO_VER;
@@ -1318,7 +1309,7 @@ out:
 
 static void *read_object_from(struct sd_vnode *vnode, uint64_t oid)
 {
-	struct sd_req hdr = { 0 };
+	struct sd_req hdr;
 	struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
 	int fd, ret;
 	unsigned wlen = 0, rlen = SD_DATA_OBJ_SIZE;
@@ -1339,7 +1330,7 @@ static void *read_object_from(struct sd_
 		exit(EXIT_FAILURE);
 	}
 
-	hdr.opcode = SD_OP_READ_PEER;
+	sd_init_req(&hdr, SD_OP_READ_PEER);
 	hdr.epoch = sd_epoch;
 	hdr.flags = 0;
 	hdr.data_length = rlen;
@@ -1364,7 +1355,7 @@ static void *read_object_from(struct sd_
 
 static void write_object_to(struct sd_vnode *vnode, uint64_t oid, void *buf)
 {
-	struct sd_req hdr = { 0 };
+	struct sd_req hdr;
 	struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
 	int fd, ret;
 	unsigned wlen = SD_DATA_OBJ_SIZE, rlen = 0;
@@ -1378,7 +1369,7 @@ static void write_object_to(struct sd_vn
 		exit(EXIT_FAILURE);
 	}
 
-	hdr.opcode = SD_OP_WRITE_PEER;
+	sd_init_req(&hdr, SD_OP_WRITE_PEER);
 	hdr.epoch = sd_epoch;
 	hdr.flags = SD_FLAG_CMD_WRITE;
 	hdr.data_length = wlen;
Index: sheepdog/include/sheep.h
===================================================================
--- sheepdog.orig/include/sheep.h	2012-07-06 13:37:11.516568762 +0200
+++ sheepdog/include/sheep.h	2012-07-06 13:39:15.073234690 +0200
@@ -39,6 +39,13 @@ struct trace_graph_item {
 	unsigned long long return_time;
 };
 
+static inline void sd_init_req(struct sd_req *req, uint8_t opcode)
+{
+	memset(req, 0, sizeof(*req));
+	req->opcode = opcode;
+	req->proto_ver = opcode < 0x80 ? SD_PROTO_VER : SD_SHEEP_PROTO_VER;
+}
+
 static inline int same_node(struct sd_vnode *e, int n1, int n2)
 {
 	if (memcmp(e[n1].nid.addr, e[n2].nid.addr, sizeof(e->nid.addr)) == 0 &&
Index: sheepdog/sheep/gateway.c
===================================================================
--- sheepdog.orig/sheep/gateway.c	2012-07-06 13:37:11.516568762 +0200
+++ sheepdog/sheep/gateway.c	2012-07-06 13:39:15.073234690 +0200
@@ -59,6 +59,7 @@ int gateway_read_obj(struct request *req
 
 		memcpy(&fwd_hdr, &req->rq, sizeof(fwd_hdr));
 		fwd_hdr.opcode = SD_OP_READ_PEER;
+		fwd_hdr.proto_ver = SD_SHEEP_PROTO_VER;
 
 		v = obj_vnodes[idx];
 		if (vnode_is_local(v))
@@ -238,6 +239,7 @@ static int do_gateway_write_obj(struct r
 		fwd_hdr.opcode = SD_OP_CREATE_AND_WRITE_PEER;
 	else
 		fwd_hdr.opcode = SD_OP_WRITE_PEER;
+	fwd_hdr.proto_ver = SD_SHEEP_PROTO_VER;
 
 	wlen = fwd_hdr.data_length;
 
@@ -320,6 +322,7 @@ int gateway_remove_obj(struct request *r
 	write_info_init(&wi);
 	memcpy(&fwd_hdr, &req->rq, sizeof(fwd_hdr));
 	fwd_hdr.opcode = SD_OP_REMOVE_PEER;
+	fwd_hdr.proto_ver = SD_SHEEP_PROTO_VER;
 
 	wlen = fwd_hdr.data_length;
 
Index: sheepdog/sheep/group.c
===================================================================
--- sheepdog.orig/sheep/group.c	2012-07-06 13:37:11.516568762 +0200
+++ sheepdog/sheep/group.c	2012-07-06 13:39:15.076568024 +0200
@@ -662,8 +662,7 @@ static int get_vdi_bitmap_from(struct sd
 
 	vprintf(SDOG_ERR, "%s:%d\n", host, node->nid.port);
 
-	memset(&hdr, 0, sizeof(hdr));
-	hdr.opcode = SD_OP_READ_VDIS;
+	sd_init_req(&hdr, SD_OP_READ_VDIS);
 	hdr.epoch = sys->epoch;
 	hdr.data_length = sizeof(tmp_vdi_inuse);
 	rlen = hdr.data_length;
Index: sheepdog/sheep/object_cache.c
===================================================================
--- sheepdog.orig/sheep/object_cache.c	2012-07-06 13:37:11.516568762 +0200
+++ sheepdog/sheep/object_cache.c	2012-07-06 13:39:15.076568024 +0200
@@ -495,7 +495,7 @@ out:
 /* Fetch the object, cache it in success */
 static int object_cache_pull(struct object_cache *oc, uint32_t idx)
 {
-	struct sd_req hdr = { 0 };
+	struct sd_req hdr;
 	int ret = SD_RES_NO_MEM;
 	uint64_t oid;
 	uint32_t data_length;
@@ -515,7 +515,7 @@ static int object_cache_pull(struct obje
 		goto out;
 	}
 
-	hdr.opcode = SD_OP_READ_OBJ;
+	sd_init_req(&hdr, SD_OP_READ_OBJ);
 	hdr.data_length = data_length;
 	hdr.obj.oid = oid;
 	hdr.obj.offset = 0;
@@ -581,8 +581,10 @@ static int push_cache_object(uint32_t vi
 	if (ret != SD_RES_SUCCESS)
 		goto out;
 
-	memset(&hdr, 0, sizeof(hdr));
-	hdr.opcode = create ? SD_OP_CREATE_AND_WRITE_OBJ : SD_OP_WRITE_OBJ;
+	if (create)
+		sd_init_req(&hdr, SD_OP_CREATE_AND_WRITE_OBJ);
+	else
+		sd_init_req(&hdr, SD_OP_WRITE_OBJ);
 	hdr.flags = SD_FLAG_CMD_WRITE;
 	hdr.data_length = data_length;
 	hdr.obj.oid = oid;
@@ -790,9 +792,9 @@ int object_cache_write(uint64_t oid, cha
 		return SD_RES_NO_MEM;
 
 	if (create)
-		req->rq.opcode = SD_OP_CREATE_AND_WRITE_OBJ;
+		sd_init_req(&req->rq, SD_OP_CREATE_AND_WRITE_OBJ);
 	else
-		req->rq.opcode = SD_OP_WRITE_OBJ;
+		sd_init_req(&req->rq, SD_OP_WRITE_OBJ);
 	req->rq.flags = flags | SD_FLAG_CMD_WRITE;
 	req->rq.data_length = datalen;
 
@@ -823,7 +825,7 @@ int object_cache_read(uint64_t oid, char
 	if (!req)
 		return SD_RES_NO_MEM;
 
-	req->rq.opcode = SD_OP_READ_OBJ;
+	sd_init_req(&req->rq, SD_OP_READ_OBJ);
 	req->rq.data_length = datalen;
 
 	req->rq.obj.oid = oid;
Index: sheepdog/sheep/recovery.c
===================================================================
--- sheepdog.orig/sheep/recovery.c	2012-07-06 13:37:11.516568762 +0200
+++ sheepdog/sheep/recovery.c	2012-07-06 13:39:15.076568024 +0200
@@ -102,8 +102,7 @@ static int recover_object_from_replica(u
 		goto out;
 	}
 
-	memset(&hdr, 0, sizeof(hdr));
-	hdr.opcode = SD_OP_READ_PEER;
+	sd_init_req(&hdr, SD_OP_READ_PEER);
 	hdr.epoch = epoch;
 	hdr.flags = SD_FLAG_CMD_RECOVERY;
 	hdr.data_length = rlen;
@@ -489,8 +488,7 @@ static int fetch_object_list(struct sd_n
 	wlen = 0;
 	rlen = buf_size;
 
-	memset(&hdr, 0, sizeof(hdr));
-	hdr.opcode = SD_OP_GET_OBJ_LIST;
+	sd_init_req((struct sd_req *)&hdr, SD_OP_GET_OBJ_LIST);
 	hdr.tgt_epoch = epoch - 1;
 	hdr.flags = 0;
 	hdr.data_length = rlen;
Index: sheepdog/sheep/store.c
===================================================================
--- sheepdog.orig/sheep/store.c	2012-07-06 13:37:11.516568762 +0200
+++ sheepdog/sheep/store.c	2012-07-06 13:39:15.076568024 +0200
@@ -113,8 +113,7 @@ int epoch_log_read_remote(uint32_t epoch
 			continue;
 		}
 
-		memset(&hdr, 0, sizeof(hdr));
-		hdr.opcode = SD_OP_GET_EPOCH;
+		sd_init_req(&hdr, SD_OP_GET_EPOCH);
 		hdr.data_length = rlen = len;
 		hdr.obj.tgt_epoch = epoch;
 
@@ -501,8 +500,10 @@ int write_object(uint64_t oid, char *dat
 		}
 	}
 
-	memset(&hdr, 0, sizeof(hdr));
-	hdr.opcode = create ? SD_OP_CREATE_AND_WRITE_OBJ : SD_OP_WRITE_OBJ;
+	if (create)
+		sd_init_req(&hdr, SD_OP_CREATE_AND_WRITE_OBJ);
+	else
+		sd_init_req(&hdr, SD_OP_WRITE_OBJ);
 	hdr.flags = flags | SD_FLAG_CMD_WRITE;
 	hdr.data_length = datalen;
 
@@ -537,8 +538,7 @@ int read_object(uint64_t oid, char *data
 	}
 
 forward_read:
-	memset(&hdr, 0, sizeof(hdr));
-	hdr.opcode = SD_OP_READ_OBJ;
+	sd_init_req(&hdr, SD_OP_READ_OBJ);
 	hdr.data_length = datalen;
 	hdr.obj.oid = oid;
 	hdr.obj.offset = offset;
@@ -555,8 +555,7 @@ int remove_object(uint64_t oid)
 	struct sd_req hdr;
 	int ret;
 
-	memset(&hdr, 0, sizeof(hdr));
-	hdr.opcode = SD_OP_REMOVE_OBJ;
+	sd_init_req(&hdr, SD_OP_REMOVE_OBJ);
 	hdr.obj.oid = oid;
 
 	ret = exec_local_req(&hdr, NULL);


