Abstract away mapping from a oid to a vnode into a new helper instead
of calling obj_to_sheep directly.

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

---
 sheep/farm/trunk.c   |    8 ++++----
 sheep/group.c        |   11 +++++++++++
 sheep/object_cache.c |   17 ++++++++---------
 sheep/sdnet.c        |   43 ++++++++++++++++++++++---------------------
 sheep/sheep_priv.h   |    4 ++++
 sheep/store.c        |   41 ++++++++++++++++++-----------------------
 6 files changed, 67 insertions(+), 57 deletions(-)

Index: sheepdog/sheep/farm/trunk.c
===================================================================
--- sheepdog.orig/sheep/farm/trunk.c	2012-04-26 15:54:41.000000000 +0200
+++ sheepdog/sheep/farm/trunk.c	2012-04-26 16:11:06.800382318 +0200
@@ -235,8 +235,9 @@ static unsigned char *omap_tree_insert(u
 
 static int oid_stale(uint64_t oid)
 {
-	int i, vidx, copies;
+	int i, copies;
 	struct vnode_info *vnodes = get_vnode_info();
+	struct sd_vnode *v;
 	int ret = 1;
 
 	copies = sys->nr_sobjs;
@@ -244,9 +245,8 @@ static int oid_stale(uint64_t oid)
 		copies = vnodes->nr_zones;
 
 	for (i = 0; i < copies; i++) {
-		vidx = obj_to_sheep(vnodes->entries, vnodes->nr_vnodes, oid, i);
-		if (is_myself(vnodes->entries[vidx].addr,
-			      vnodes->entries[vidx].port)) {
+		v = oid_to_vnode(vnodes, oid, i);
+		if (is_myself(v->addr, v->port)) {
 			ret = 0;
 			break;
 		}
Index: sheepdog/sheep/group.c
===================================================================
--- sheepdog.orig/sheep/group.c	2012-04-26 16:05:14.000000000 +0200
+++ sheepdog/sheep/group.c	2012-04-26 16:11:06.800382318 +0200
@@ -139,6 +139,17 @@ void put_vnode_info(struct vnode_info *v
 		free(vnodes);
 }
 
+struct sd_vnode *oid_to_vnode(struct vnode_info *vnode_info, uint64_t oid,
+		int copy_idx)
+{
+	int n;
+
+	n = obj_to_sheep(vnode_info->entries, vnode_info->nr_vnodes,
+			 oid, copy_idx);
+	return &vnode_info->entries[n];
+}
+
+
 static int update_vnode_info(void)
 {
 	struct vnode_info *vnode_info;
Index: sheepdog/sheep/object_cache.c
===================================================================
--- sheepdog.orig/sheep/object_cache.c	2012-04-26 15:54:41.000000000 +0200
+++ sheepdog/sheep/object_cache.c	2012-04-26 16:11:06.804382316 +0200
@@ -369,12 +369,13 @@ out:
 /* Fetch the object, cache it in success */
 int object_cache_pull(struct object_cache *oc, uint32_t idx)
 {
-	int i, n = 0, fd, ret = SD_RES_NO_MEM;
+	int i, fd, ret = SD_RES_NO_MEM;
 	unsigned wlen = 0, rlen, data_length, read_len;
 	uint64_t oid;
 	struct sd_obj_req hdr = { 0 };
 	struct sd_obj_rsp *rsp = (struct sd_obj_rsp *)&hdr;
 	struct vnode_info *vnodes = get_vnode_info();
+	struct sd_vnode *v;
 	void *buf;
 	int copies;
 
@@ -398,9 +399,8 @@ int object_cache_pull(struct object_cach
 
 	/* Check if we can read locally */
 	for (i = 0; i < copies; i++) {
-		n = obj_to_sheep(vnodes->entries, vnodes->nr_vnodes, oid, i);
-		if (is_myself(vnodes->entries[n].addr,
-			      vnodes->entries[n].port)) {
+		v = oid_to_vnode(vnodes, oid, i);
+		if (is_myself(v->addr, v->port)) {
 			struct siocb iocb = { 0 };
 			iocb.epoch = sys->epoch;
 			ret = sd_store->open(oid, &iocb, 0);
@@ -424,8 +424,9 @@ int object_cache_pull(struct object_cach
 pull_remote:
 	/* Okay, no luck, let's read remotely */
 	for (i = 0; i < copies; i++) {
-		n = obj_to_sheep(vnodes->entries, vnodes->nr_vnodes, oid, i);
-		if (is_myself(vnodes->entries[n].addr, vnodes->entries[n].port))
+		v = oid_to_vnode(vnodes, oid, i);
+
+		if (is_myself(v->addr, v->port))
 			continue;
 
 		hdr.opcode = SD_OP_READ_OBJ;
@@ -434,9 +435,7 @@ pull_remote:
 		hdr.data_length = rlen = data_length;
 		hdr.flags = SD_FLAG_CMD_IO_LOCAL;
 
-		fd = get_sheep_fd(vnodes->entries[n].addr,
-				  vnodes->entries[n].port,
-				  vnodes->entries[n].node_idx,
+		fd = get_sheep_fd(v->addr, v->port, v->node_idx,
 				  hdr.epoch);
 		if (fd < 0)
 			continue;
Index: sheepdog/sheep/sdnet.c
===================================================================
--- sheepdog.orig/sheep/sdnet.c	2012-04-26 16:08:22.000000000 +0200
+++ sheepdog/sheep/sdnet.c	2012-04-26 16:11:37.548383107 +0200
@@ -39,7 +39,8 @@ void resume_pending_requests(void)
 static int is_access_local(struct request *req, uint64_t oid, int copies)
 {
 	struct vnode_info *vnodes = req->vnodes;
-	int i, n;
+	struct sd_vnode *v;
+	int i;
 
 	if (oid == 0)
 		return 0;
@@ -50,9 +51,8 @@ static int is_access_local(struct reques
 		copies = vnodes->nr_zones;
 
 	for (i = 0; i < copies; i++) {
-		n = obj_to_sheep(vnodes->entries, vnodes->nr_vnodes, oid, i);
-
-		if (is_myself(vnodes->entries[n].addr, vnodes->entries[n].port))
+		v = oid_to_vnode(vnodes, oid, i);
+		if (is_myself(v->addr, v->port))
 			return 1;
 	}
 
@@ -680,7 +680,8 @@ int write_object(struct vnode_info *vnod
 		 uint64_t offset, uint16_t flags, int nr, int create)
 {
 	struct sd_obj_req hdr;
-	int i, n, fd, ret;
+	struct sd_vnode *v;
+	int i, fd, ret;
 	char name[128];
 
 	if (nr > vnodes->nr_zones)
@@ -689,9 +690,8 @@ int write_object(struct vnode_info *vnod
 	for (i = 0; i < nr; i++) {
 		unsigned rlen = 0, wlen = datalen;
 
-		n = obj_to_sheep(vnodes->entries, vnodes->nr_vnodes, oid, i);
-
-		if (is_myself(vnodes->entries[n].addr, vnodes->entries[n].port)) {
+		v = oid_to_vnode(vnodes, oid, i);
+		if (is_myself(v->addr, v->port)) {
 			ret = write_object_local(oid, data, datalen, offset,
 						 flags, nr, node_version, create);
 
@@ -703,9 +703,9 @@ int write_object(struct vnode_info *vnod
 			continue;
 		}
 
-		addr_to_str(name, sizeof(name), vnodes->entries[n].addr, 0);
+		addr_to_str(name, sizeof(name), v->addr, 0);
 
-		fd = connect_to(name, vnodes->entries[n].port);
+		fd = connect_to(name, v->port);
 		if (fd < 0) {
 			eprintf("failed to connect to host %s\n", name);
 			return -1;
@@ -743,17 +743,17 @@ int read_object(struct vnode_info *vnode
 {
 	struct sd_obj_req hdr;
 	struct sd_obj_rsp *rsp = (struct sd_obj_rsp *)&hdr;
+	struct sd_vnode *v;
 	char name[128];
-	int i = 0, n, fd, ret, last_error = SD_RES_SUCCESS;
+	int i = 0, fd, ret, last_error = SD_RES_SUCCESS;
 
 	if (nr > vnodes->nr_zones)
 		nr = vnodes->nr_zones;
 
 	/* search a local object first */
 	for (i = 0; i < nr; i++) {
-		n = obj_to_sheep(vnodes->entries, vnodes->nr_vnodes, oid, i);
-
-		if (is_myself(vnodes->entries[n].addr, vnodes->entries[n].port)) {
+		v = oid_to_vnode(vnodes, oid, i);
+		if (is_myself(v->addr, v->port)) {
 			ret = read_object_local(oid, data, datalen, offset, nr,
 						node_version);
 
@@ -770,11 +770,11 @@ int read_object(struct vnode_info *vnode
 	for (i = 0; i < nr; i++) {
 		unsigned wlen = 0, rlen = datalen;
 
-		n = obj_to_sheep(vnodes->entries, vnodes->nr_vnodes, oid, i);
+		v = oid_to_vnode(vnodes, oid, i);
 
-		addr_to_str(name, sizeof(name), vnodes->entries[n].addr, 0);
+		addr_to_str(name, sizeof(name), v->addr, 0);
 
-		fd = connect_to(name, vnodes->entries[n].port);
+		fd = connect_to(name, v->port);
 		if (fd < 0) {
 			printf("%s(%d): %s, %m\n", __func__, __LINE__,
 			       name);
@@ -813,7 +813,8 @@ int remove_object(struct vnode_info *vno
 	char name[128];
 	struct sd_obj_req hdr;
 	struct sd_obj_rsp *rsp = (struct sd_obj_rsp *)&hdr;
-	int i = 0, n, fd, ret;
+	struct sd_vnode *v;
+	int i = 0, fd, ret;
 
 	if (nr > vnodes->nr_zones)
 		nr = vnodes->nr_zones;
@@ -821,11 +822,11 @@ int remove_object(struct vnode_info *vno
 	for (i = 0; i < nr; i++) {
 		unsigned wlen = 0, rlen = 0;
 
-		n = obj_to_sheep(vnodes->entries, vnodes->nr_vnodes, oid, i);
+		v = oid_to_vnode(vnodes, oid, i);
 
-		addr_to_str(name, sizeof(name), vnodes->entries[n].addr, 0);
+		addr_to_str(name, sizeof(name), v->addr, 0);
 
-		fd = connect_to(name, vnodes->entries[n].port);
+		fd = connect_to(name, v->port);
 		if (fd < 0) {
 			rsp->result = SD_RES_EIO;
 			return -1;
Index: sheepdog/sheep/sheep_priv.h
===================================================================
--- sheepdog.orig/sheep/sheep_priv.h	2012-04-26 15:54:41.000000000 +0200
+++ sheepdog/sheep/sheep_priv.h	2012-04-26 16:11:06.804382316 +0200
@@ -242,6 +242,10 @@ int get_vdi_attr(uint32_t epoch, struct
 int get_zones_nr_from(struct sd_node *nodes, int nr_nodes);
 struct vnode_info *get_vnode_info(void);
 void put_vnode_info(struct vnode_info *vnodes);
+
+struct sd_vnode *oid_to_vnode(struct vnode_info *vnode_info, uint64_t oid,
+		int copy_idx);
+
 int is_access_to_busy_objects(uint64_t oid);
 
 void resume_pending_requests(void);
Index: sheepdog/sheep/store.c
===================================================================
--- sheepdog.orig/sheep/store.c	2012-04-26 15:54:41.000000000 +0200
+++ sheepdog/sheep/store.c	2012-04-26 16:11:06.804382316 +0200
@@ -223,27 +223,25 @@ int get_obj_list(const struct sd_list_re
 static int read_copy_from_replica(struct request *req, uint32_t epoch,
 				  uint64_t oid, char *buf)
 {
-	int i, n, nr, ret;
+	int i, nr, ret;
 	unsigned wlen, rlen;
 	char name[128];
-	struct sd_vnode *e;
+	struct sd_vnode *v;
 	struct sd_obj_req hdr;
 	struct sd_obj_rsp *rsp = (struct sd_obj_rsp *)&hdr;
 	struct siocb iocb;
 	int fd;
 
-	e = req->vnodes->entries;
-
 	nr = sys->nr_sobjs;
 	if (nr > req->vnodes->nr_zones)
 		nr = req->vnodes->nr_zones;
 
 	for (i = 0; i < nr; i++) {
-		n = obj_to_sheep(e, req->vnodes->nr_vnodes, oid, i);
+		v = oid_to_vnode(req->vnodes, oid, i);
 
-		addr_to_str(name, sizeof(name), e[n].addr, 0);
+		addr_to_str(name, sizeof(name), v->addr, 0);
 
-		if (is_myself(e[n].addr, e[n].port)) {
+		if (is_myself(v->addr, v->port)) {
 			memset(&iocb, 0, sizeof(iocb));
 			iocb.epoch = epoch;
 			ret = sd_store->open(oid, &iocb, 0);
@@ -260,7 +258,7 @@ static int read_copy_from_replica(struct
 			goto out;
 		}
 
-		fd = connect_to(name, e[n].port);
+		fd = connect_to(name, v->port);
 		if (fd < 0)
 			continue;
 
@@ -305,16 +303,14 @@ static int do_local_io(struct request *r
 
 static int forward_read_obj_req(struct request *req)
 {
-	int i, n, fd, ret = SD_RES_SUCCESS;
+	int i, fd, ret = SD_RES_SUCCESS;
 	unsigned wlen, rlen;
 	struct sd_obj_req hdr = *(struct sd_obj_req *)&req->rq;
 	struct sd_obj_rsp *rsp = (struct sd_obj_rsp *)&hdr;
-	struct sd_vnode *e;
+	struct sd_vnode *v;
 	uint64_t oid = hdr.oid;
 	int copies;
 
-	e = req->vnodes->entries;
-
 	copies = hdr.copies;
 
 	/* temporary hack */
@@ -327,9 +323,8 @@ static int forward_read_obj_req(struct r
 
 	/* TODO: we can do better; we need to check this first */
 	for (i = 0; i < copies; i++) {
-		n = obj_to_sheep(e, req->vnodes->nr_vnodes, oid, i);
-
-		if (is_myself(e[n].addr, e[n].port)) {
+		v = oid_to_vnode(req->vnodes, oid, i);
+		if (is_myself(v->addr, v->port)) {
 			ret = do_local_io(req, hdr.epoch);
 			if (ret != SD_RES_SUCCESS)
 				goto read_remote;
@@ -339,11 +334,11 @@ static int forward_read_obj_req(struct r
 
 read_remote:
 	for (i = 0; i < copies; i++) {
-		n = obj_to_sheep(e, req->vnodes->nr_vnodes, oid, i);
-		if (is_myself(e[n].addr, e[n].port))
+		v = oid_to_vnode(req->vnodes, oid, i);
+		if (is_myself(v->addr, v->port))
 			continue;
 
-		fd = get_sheep_fd(e[n].addr, e[n].port, e[n].node_idx, hdr.epoch);
+		fd = get_sheep_fd(v->addr, v->port, v->node_idx, hdr.epoch);
 		if (fd < 0) {
 			ret = SD_RES_NETWORK_ERROR;
 			continue;
@@ -374,7 +369,7 @@ int forward_write_obj_req(struct request
 	char name[128];
 	struct sd_obj_req hdr = *(struct sd_obj_req *)&req->rq;
 	struct sd_obj_rsp *rsp = (struct sd_obj_rsp *)&req->rp;
-	struct sd_vnode *e = req->vnodes->entries;
+	struct sd_vnode *v;
 	uint64_t oid = hdr.oid;
 	int copies;
 	struct pollfd pfds[SD_MAX_REDUNDANCY];
@@ -400,18 +395,18 @@ int forward_write_obj_req(struct request
 	wlen = hdr.data_length;
 
 	for (i = 0; i < copies; i++) {
-		n = obj_to_sheep(req->vnodes->entries, req->vnodes->nr_vnodes, oid, i);
+		v = oid_to_vnode(req->vnodes, oid, i);
 
 		addr_to_str(name, sizeof(name), req->vnodes->entries[n].addr, 0);
 
-		if (is_myself(e[n].addr, e[n].port)) {
+		if (is_myself(v->addr, v->port)) {
 			local = 1;
 			continue;
 		}
 
-		fd = get_sheep_fd(e[n].addr, e[n].port, e[n].node_idx, hdr.epoch);
+		fd = get_sheep_fd(v->addr, v->port, v->node_idx, hdr.epoch);
 		if (fd < 0) {
-			eprintf("failed to connect to %s:%"PRIu32"\n", name, e[n].port);
+			eprintf("failed to connect to %s:%"PRIu32"\n", name, v->port);
 			ret = SD_RES_NETWORK_ERROR;
 			goto out;
 		}


