Also document it and rename it to get_nr_copies_int to avoid confusion
with get_nr_copies.

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

---
 sheep/group.c      |   12 ++++++++++++
 sheep/ops.c        |    2 +-
 sheep/sheep_priv.h |    2 +-
 sheep/store.c      |   31 +++++--------------------------
 4 files changed, 19 insertions(+), 28 deletions(-)

Index: sheepdog/sheep/group.c
===================================================================
--- sheepdog.orig/sheep/group.c	2012-04-26 19:02:14.440645222 +0200
+++ sheepdog/sheep/group.c	2012-04-26 19:10:10.952657423 +0200
@@ -133,6 +133,18 @@ int get_zones_nr_from(struct sd_node *no
 	return nr_zones;
 }
 
+/*
+ * If we have less zones available than the desired redundancy we have to do
+ * with nr_zones copies, sorry.
+ *
+ * Note that you generally want to use get_nr_copies below, as it uses the
+ * current vnode state snapshot instead of global data.
+ */
+int get_nr_copies_int(struct sd_node *nodes, int nr_nodes)
+{
+	return min((int)sys->nr_copies, get_zones_nr_from(nodes, nr_nodes));
+}
+
 struct vnode_info *get_vnode_info(void)
 {
 	assert(current_vnode_info);
Index: sheepdog/sheep/store.c
===================================================================
--- sheepdog.orig/sheep/store.c	2012-04-26 19:02:14.444645222 +0200
+++ sheepdog/sheep/store.c	2012-04-26 19:10:07.952657345 +0200
@@ -1133,27 +1133,6 @@ uint64_t get_cluster_ctime(void)
 	return ct;
 }
 
-int get_max_copies(struct sd_node *entries, int nr)
-{
-	int i, j;
-	unsigned int nr_zones = 0;
-	uint32_t zones[SD_MAX_REDUNDANCY];
-
-	for (i = 0; i < nr; i++) {
-		if (nr_zones >= ARRAY_SIZE(zones))
-			break;
-
-		for (j = 0; j < nr_zones; j++) {
-			if (zones[j] == entries[i].zone)
-				break;
-		}
-		if (j == nr_zones)
-			zones[nr_zones++] = entries[i].zone;
-	}
-
-	return min(sys->nr_copies, nr_zones);
-}
-
 /*
  * contains_node - checks that the node id is included in the target nodes
  *
@@ -1311,7 +1290,7 @@ static void *get_vnodes_from_epoch(int e
 		nodes_nr /= sizeof(nodes[0]);
 	}
 	*nr = nodes_to_vnodes(nodes, nodes_nr, buf);
-	*copies = get_max_copies(nodes, nodes_nr);
+	*copies = get_nr_copies_int(nodes, nodes_nr);
 
 	return buf;
 }
@@ -1443,8 +1422,8 @@ static int do_recover_object(struct reco
 	cur = xmalloc(sizeof(*cur) * SD_MAX_VNODES);
 	memcpy(old, rw->old_vnodes, sizeof(*old) * old_nr);
 	memcpy(cur, rw->cur_vnodes, sizeof(*cur) * cur_nr);
-	old_copies = get_max_copies(rw->old_nodes, rw->old_nr_nodes);
-	cur_copies = get_max_copies(rw->cur_nodes, rw->cur_nr_nodes);
+	old_copies = get_nr_copies_int(rw->old_nodes, rw->old_nr_nodes);
+	cur_copies = get_nr_copies_int(rw->cur_nodes, rw->cur_nr_nodes);
 
 again:
 	old_idx = obj_to_sheep(old, old_nr, oid, 0);
@@ -1502,7 +1481,7 @@ err:
 static int get_replica_idx(struct recovery_work *rw, uint64_t oid, int *copy_nr)
 {
 	int i, ret = -1;
-	*copy_nr = get_max_copies(rw->cur_nodes, rw->cur_nr_nodes);
+	*copy_nr = get_nr_copies_int(rw->cur_nodes, rw->cur_nr_nodes);
 	for (i = 0; i < *copy_nr; i++) {
 		int n = obj_to_sheep(rw->cur_vnodes, rw->cur_nr_vnodes, oid, i);
 		if (vnode_is_local(&rw->cur_vnodes[n])) {
@@ -1780,7 +1759,7 @@ static int screen_obj_list(struct recove
 	struct strbuf buf = STRBUF_INIT;
 	struct sd_vnode *nodes = rw->cur_vnodes;
 	int nodes_nr = rw->cur_nr_vnodes;
-	int nr_objs = get_max_copies(rw->cur_nodes, rw->cur_nr_nodes);
+	int nr_objs = get_nr_copies_int(rw->cur_nodes, rw->cur_nr_nodes);
 
 	for (i = 0; i < list_nr; i++) {
 		for (cp = 0; cp < nr_objs; cp++) {
Index: sheepdog/sheep/ops.c
===================================================================
--- sheepdog.orig/sheep/ops.c	2012-04-26 19:02:14.440645222 +0200
+++ sheepdog/sheep/ops.c	2012-04-26 19:09:33.224656460 +0200
@@ -320,7 +320,7 @@ static int local_stat_cluster(const stru
 			log->nr_nodes = epoch_log_read_remote(epoch,
 							      (char *)log->nodes,
 							      sizeof(log->nodes));
-		log->nr_copies = get_max_copies(log->nodes, log->nr_nodes);
+		log->nr_copies = get_nr_copies_int(log->nodes, log->nr_nodes);
 
 		rsp->data_length += sizeof(*log);
 		log->nr_nodes /= sizeof(log->nodes[0]);
Index: sheepdog/sheep/sheep_priv.h
===================================================================
--- sheepdog.orig/sheep/sheep_priv.h	2012-04-26 19:02:14.444645222 +0200
+++ sheepdog/sheep/sheep_priv.h	2012-04-26 19:09:40.428656641 +0200
@@ -278,7 +278,7 @@ int store_remove_obj(const struct sd_req
 
 int store_file_write(void *buffer, size_t len);
 void *store_file_read(void);
-int get_max_copies(struct sd_node *entries, int nr);
+int get_nr_copies_int(struct sd_node *entries, int nr);
 
 int epoch_log_read(uint32_t epoch, char *buf, int len);
 int epoch_log_read_nr(uint32_t epoch, char *buf, int len);


