This allows reusing a passed in node list from recovery later on.

Signed-off-by: Christoph Hellwig <[email protected]>

diff --git a/sheep/farm/farm.c b/sheep/farm/farm.c
index cbfe99d..e2e7522 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_nr(epoch, (char *)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_nr(sys->epoch, (char *)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(int 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 3134c28..5b3310e 100644
--- a/sheep/farm/snap.c
+++ b/sheep/farm/snap.c
@@ -155,26 +155,26 @@ 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(int 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;
-       uint64_t epoch_size = epoch_log_read(tgt_epoch, (char *)nodes, 
sizeof(nodes));
-       struct sha1_file_hdr hdr = { .size = epoch_size + SHA1_LEN,
-                                    .priv = tgt_epoch };
+       struct sha1_file_hdr hdr = {
+               .size = SHA1_LEN + nr_nodes * sizeof(struct sd_node),
+               .priv = epoch,
+       };
        memcpy(hdr.tag, TAG_SNAP, TAG_LEN);
 
        strbuf_add(&buf, &hdr, sizeof(hdr));
        strbuf_add(&buf, trunksha1, SHA1_LEN);
-       strbuf_add(&buf, (char *)nodes, epoch_size);
+       strbuf_add(&buf, nodes, nr_nodes * sizeof(struct sd_node));
        if (sha1_file_write((void *)buf.buf, buf.len, outsha1) < 0) {
                ret = -1;
                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;

-- 
sheepdog mailing list
[email protected]
http://lists.wpkg.org/mailman/listinfo/sheepdog

Reply via email to