This will allow passing in a node list from recovery later.

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

---
 sheep/farm/farm.c |   21 +++++++++++++++++++--
 sheep/farm/farm.h |    3 ++-
 sheep/farm/snap.c |   14 ++++----------
 3 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/sheep/farm/farm.c b/sheep/farm/farm.c
index 323b086..f17173f 100644
--- a/sheep/farm/farm.c
+++ b/sheep/farm/farm.c
@@ -533,14 +533,23 @@ static int farm_end_recover(struct siocb *iocb)
 	unsigned char snap_sha1[SHA1_LEN];
 	unsigned char trunk_sha1[SHA1_LEN];
 	uint32_t epoch = iocb->epoch - 1;
+	struct sd_node nodes[SD_MAX_NODES];
+	int nr_nodes;
 
 	if (epoch == 0)
 		return SD_RES_SUCCESS;
+
+	nr_nodes = epoch_log_read(epoch, nodes, sizeof(nodes));
+	if (nr_nodes < 0) {
+		dprintf("failed to read nodes for epoch %d\n", epoch);
+		return SD_RES_EIO;
+	}
+
 	dprintf("epoch %d\n", iocb->epoch);
 	if (trunk_file_write_recovery(trunk_sha1) < 0)
 		return SD_RES_EIO;
 
-	if (snap_file_write(epoch, trunk_sha1, snap_sha1, 0) < 0)
+	if (snap_file_write(epoch, nodes, nr_nodes, trunk_sha1, snap_sha1) < 0)
 		return SD_RES_EIO;
 
 	if (snap_log_write(epoch, snap_sha1, 0) < 0)
@@ -553,6 +562,8 @@ static int farm_snapshot(struct siocb *iocb)
 {
 	unsigned char snap_sha1[SHA1_LEN];
 	unsigned char trunk_sha1[SHA1_LEN];
+	struct sd_node nodes[SD_MAX_NODES];
+	int nr_nodes;
 	void *buffer;
 	int log_nr, ret = SD_RES_EIO, epoch;
 
@@ -562,10 +573,16 @@ static int farm_snapshot(struct siocb *iocb)
 
 	epoch = log_nr + 1;
 	dprintf("user epoch %d\n", epoch);
+
+	nr_nodes = epoch_log_read(sys->epoch, nodes, sizeof(nodes));
+	if (nr_nodes < 0)
+		goto out;
+
 	if (trunk_file_write_user(trunk_sha1) < 0)
 		goto out;
 
-	if (snap_file_write(epoch, trunk_sha1, snap_sha1, 1) < 0)
+	if (snap_file_write(sys->epoch, nodes, nr_nodes,
+			    trunk_sha1, snap_sha1) < 0)
 		goto out;
 
 	if (snap_log_write(epoch, snap_sha1, 1) < 0)
diff --git a/sheep/farm/farm.h b/sheep/farm/farm.h
index 2a21061..2742dbf 100644
--- a/sheep/farm/farm.h
+++ b/sheep/farm/farm.h
@@ -71,7 +71,8 @@ extern void trunk_get_entry(uint64_t oid);
 /* snap.c */
 extern int snap_init(void);
 extern void *snap_file_read(unsigned char *sha1, struct sha1_file_hdr *outhdr);
-extern int snap_file_write(uint32_t epoch, unsigned char *trunksha1, unsigned char *outsha1, int user);
+extern int snap_file_write(uint32_t epoch, struct sd_node *nodes, int nr_nodes,
+		unsigned char *trunksha1, unsigned char *outsha1);
 extern int snap_log_truncate(void);
 extern void *snap_log_read(int *, int user);
 extern int snap_log_write(uint32_t epoch, unsigned char *sha1, int user);
diff --git a/sheep/farm/snap.c b/sheep/farm/snap.c
index 5a7a642..d636b1c 100644
--- a/sheep/farm/snap.c
+++ b/sheep/farm/snap.c
@@ -155,22 +155,16 @@ void *snap_file_read(unsigned char *sha1, struct sha1_file_hdr *outhdr)
 	return buffer;
 }
 
-int snap_file_write(uint32_t epoch, unsigned char *trunksha1, unsigned char *outsha1, int user)
+int snap_file_write(uint32_t epoch, struct sd_node *nodes, int nr_nodes,
+		unsigned char *trunksha1, unsigned char *outsha1)
 {
 	int ret = 0;
 	struct strbuf buf = STRBUF_INIT;
-	struct sd_node nodes[SD_MAX_NODES];
-	int tgt_epoch = user ? sys_epoch() : epoch;
-	int nr_nodes;
 	struct sha1_file_hdr hdr;
 
-	nr_nodes = epoch_log_read(tgt_epoch, nodes, sizeof(nodes));
-	if (nr_nodes == -1)
-		return -1;
-
 	memcpy(hdr.tag, TAG_SNAP, TAG_LEN);
 	hdr.size = nr_nodes * sizeof(*nodes) + SHA1_LEN;
-	hdr.priv = tgt_epoch;
+	hdr.priv = epoch;
 	hdr.reserved = 0;
 
 	strbuf_add(&buf, &hdr, sizeof(hdr));
@@ -181,7 +175,7 @@ int snap_file_write(uint32_t epoch, unsigned char *trunksha1, unsigned char *out
 		goto err;
 	}
 
-	dprintf("epoch %u, sha1: %s\n", epoch, sha1_to_hex(outsha1));
+	dprintf("epoch: %d, sha1: %s\n", epoch, sha1_to_hex(outsha1));
 err:
 	strbuf_release(&buf);
 	return ret;
-- 
1.7.10



