[sheepdog] [PATCH v2 04/12] sheep: remove farm from sheep

Kai Zhang kyle at zelin.io
Tue May 14 09:16:42 CEST 2013


Signed-off-by: Kai Zhang <kyle at zelin.io>
---
 sheep/Makefile.am      |   10 +-
 sheep/farm/farm.c      |  271 -----------------------------------------------
 sheep/farm/farm.h      |   68 ------------
 sheep/farm/sha1_file.c |  273 ------------------------------------------------
 sheep/farm/snap.c      |  152 ---------------------------
 sheep/farm/trunk.c     |  139 ------------------------
 sheep/ops.c            |   62 -----------
 sheep/sheep_priv.h     |    6 +-
 sheep/store.c          |    1 -
 9 files changed, 7 insertions(+), 975 deletions(-)
 delete mode 100644 sheep/farm/farm.c
 delete mode 100644 sheep/farm/farm.h
 delete mode 100644 sheep/farm/sha1_file.c
 delete mode 100644 sheep/farm/snap.c
 delete mode 100644 sheep/farm/trunk.c

diff --git a/sheep/Makefile.am b/sheep/Makefile.am
index 32bc1c0..566e50b 100644
--- a/sheep/Makefile.am
+++ b/sheep/Makefile.am
@@ -37,22 +37,20 @@ if BUILD_ZOOKEEPER
 sheep_SOURCES		+= cluster/zookeeper.c
 endif
 
-sheep_SOURCES		+= farm/sha1_file.c farm/trunk.c farm/snap.c farm/farm.c
-
 if BUILD_TRACE
 sheep_SOURCES		+= trace/trace.c trace/mcount.S trace/stabs.c trace/graph.c
 endif
 
-sheep_LDADD	  	= ../lib/libsheepdog.a -lpthread -lm\
+sheep_LDADD		= ../lib/libsheepdog.a -lpthread -lm\
 			  $(libcpg_LIBS) $(libcfg_LIBS) $(libacrd_LIBS) $(LIBS)
 sheep_DEPENDENCIES	= ../lib/libsheepdog.a
 
 
-noinst_HEADERS		= work.h sheep_priv.h cluster.h farm/farm.h trace/trace.h
+noinst_HEADERS		= work.h sheep_priv.h cluster.h trace/trace.h
 
-EXTRA_DIST		= 
+EXTRA_DIST		=
 
-all-local: 
+all-local:
 	@echo Built sheep
 
 clean-local:
diff --git a/sheep/farm/farm.c b/sheep/farm/farm.c
deleted file mode 100644
index cca8bbd..0000000
--- a/sheep/farm/farm.c
+++ /dev/null
@@ -1,271 +0,0 @@
-/*
- * Copyright (C) 2011 Taobao Inc.
- *
- * Liu Yuan <namei.unix at gmail.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License version
- * 2 as published by the Free Software Foundation.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <pthread.h>
-#include <linux/limits.h>
-#include <sys/file.h>
-#include <sys/types.h>
-#include <sys/xattr.h>
-
-#include "farm.h"
-#include "sheep_priv.h"
-#include "sheepdog_proto.h"
-#include "sheep.h"
-
-char farm_obj_dir[PATH_MAX];
-char farm_dir[PATH_MAX];
-
-static int create_directory(const char *p)
-{
-	int i, ret = 0;
-	struct strbuf buf = STRBUF_INIT;
-
-	strbuf_addstr(&buf, p);
-	strbuf_addstr(&buf, "/.farm");
-	if (xmkdir(buf.buf, 0755) < 0) {
-		sd_eprintf("%m");
-		ret = -1;
-		goto err;
-	}
-
-	if (!strlen(farm_dir))
-		strbuf_copyout(&buf, farm_dir, sizeof(farm_dir));
-
-	strbuf_addstr(&buf, "/objects");
-	if (xmkdir(buf.buf, 0755) < 0) {
-		sd_eprintf("%m");
-		ret = -1;
-		goto err;
-	}
-	for (i = 0; i < 256; i++) {
-		strbuf_addf(&buf, "/%02x", i);
-		if (xmkdir(buf.buf, 0755) < 0) {
-			sd_eprintf("%m");
-			ret = -1;
-			goto err;
-		}
-		strbuf_remove(&buf, buf.len - 3, 3);
-	}
-
-	if (!strlen(farm_obj_dir))
-		strbuf_copyout(&buf, farm_obj_dir, sizeof(farm_obj_dir));
-err:
-	strbuf_release(&buf);
-	return ret;
-}
-
-static int get_trunk_sha1(uint32_t epoch, unsigned char *outsha1)
-{
-	int i, nr_logs = -1, ret = -1;
-	struct snap_log *log_buf, *log_free = NULL;
-	void *snap_buf = NULL;
-	struct sha1_file_hdr hdr;
-
-	log_free = log_buf = snap_log_read(&nr_logs);
-	sd_dprintf("%d", nr_logs);
-	if (nr_logs < 0)
-		goto out;
-
-	for (i = 0; i < nr_logs; i++, log_buf++) {
-		if (log_buf->epoch != epoch)
-			continue;
-		snap_buf = snap_file_read(log_buf->sha1, &hdr);
-		if (!snap_buf)
-			goto out;
-		memcpy(outsha1, snap_buf, SHA1_LEN);
-		ret = 0;
-		break;
-	}
-out:
-	free(log_free);
-	free(snap_buf);
-	return ret;
-}
-
-static int farm_init(void)
-{
-	sd_dprintf("use farm store driver");
-	if (create_directory(obj_path) < 0)
-		goto err;
-
-	if (!is_xattr_enabled(obj_path)) {
-		sd_eprintf("xattrs are not enabled on %s", obj_path);
-		goto err;
-	}
-
-	if (snap_init() < 0)
-		goto err;
-
-	if (default_init() < 0)
-		goto err;
-
-	return SD_RES_SUCCESS;
-err:
-	return SD_RES_EIO;
-}
-
-static int farm_snapshot(const 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;
-
-	buffer = snap_log_read(&log_nr);
-	if (!buffer)
-		goto out;
-
-	epoch = log_nr + 1;
-	sd_dprintf("user epoch %d", epoch);
-
-	nr_nodes = epoch_log_read(sys->epoch, nodes, sizeof(nodes));
-	if (nr_nodes < 0)
-		goto out;
-
-	if (trunk_file_write(trunk_sha1) < 0)
-		goto out;
-
-	if (snap_file_write(sys->epoch, nodes, nr_nodes,
-			    trunk_sha1, snap_sha1) < 0)
-		goto out;
-
-	if (snap_log_write(epoch, snap_sha1) < 0)
-		goto out;
-
-	ret = SD_RES_SUCCESS;
-out:
-	free(buffer);
-	return ret;
-}
-
-static int restore_objects_from_snap(uint32_t epoch)
-{
-	struct sha1_file_hdr hdr;
-	struct trunk_entry *trunk_buf, *trunk_free = NULL;
-	unsigned char trunk_sha1[SHA1_LEN];
-	uint64_t nr_trunks, i;
-	int ret = SD_RES_EIO;
-
-	if (get_trunk_sha1(epoch, trunk_sha1) < 0)
-		goto out;
-
-	trunk_free = trunk_buf = trunk_file_read(trunk_sha1, &hdr);
-	if (!trunk_buf)
-		goto out;
-
-	nr_trunks = hdr.priv;
-	ret = SD_RES_SUCCESS;
-	for (i = 0; i < nr_trunks; i++, trunk_buf++) {
-		struct sha1_file_hdr h;
-		struct siocb io = { 0 };
-		uint64_t oid;
-		void *buffer = NULL;
-
-		oid = trunk_buf->oid;
-		buffer = sha1_file_read(trunk_buf->sha1, &h);
-		if (!buffer) {
-			sd_eprintf("oid %"PRIx64" not restored", oid);
-			goto out;
-		}
-		io.length = h.size;
-		io.buf = buffer;
-		ret = default_create_and_write(oid, &io);
-		if (ret != SD_RES_SUCCESS) {
-			sd_eprintf("oid %"PRIx64" not restored", oid);
-			goto out;
-		} else
-			sd_dprintf("oid %"PRIx64" restored", oid);
-
-		free(buffer);
-	}
-out:
-	free(trunk_free);
-	return ret;
-}
-
-static int rm_object(uint64_t oid, char *path, void *arg)
-{
-	char p[PATH_MAX];
-	int ret = SD_RES_SUCCESS;
-
-	snprintf(p, sizeof(p), "%s/%"PRIx64, path, oid);
-	if (unlink(path) < 0) {
-		sd_eprintf("failed to remove cached object %m");
-		if (errno == ENOENT)
-			return SD_RES_SUCCESS;
-		ret = SD_RES_EIO;
-		goto out;
-	}
-out:
-	return ret;
-}
-
-static int farm_restore(const struct siocb *iocb)
-{
-	int ret = SD_RES_EIO, epoch = iocb->epoch;
-
-	sd_dprintf("try recover user epoch %d", epoch);
-
-	/* Remove all the objects of WD and object cache */
-	for_each_object_in_wd(rm_object, true, NULL);
-	if (sys->enable_object_cache)
-		object_cache_format();
-
-	ret = restore_objects_from_snap(epoch);
-	if (ret != SD_RES_SUCCESS)
-		goto out;
-out:
-	return ret;
-}
-
-static int farm_get_snap_file(struct siocb *iocb)
-{
-	int ret = SD_RES_EIO;
-	void *buffer = NULL;
-	size_t size;
-	int nr;
-
-	sd_dprintf("try get snap file");
-	buffer = snap_log_read(&nr);
-	if (!buffer)
-		goto out;
-	size = nr * sizeof(struct snap_log);
-	memcpy(iocb->buf, buffer, size);
-	iocb->length = size;
-	ret = SD_RES_SUCCESS;
-out:
-	free(buffer);
-	return ret;
-}
-
-static struct store_driver farm = {
-	.name = "farm",
-	.init = farm_init,
-	.exist = default_exist,
-	.create_and_write = default_create_and_write,
-	.write = default_write,
-	.read = default_read,
-	.link = default_link,
-	.update_epoch = default_update_epoch,
-	.snapshot = farm_snapshot,
-	.cleanup = default_cleanup,
-	.restore = farm_restore,
-	.get_snap_file = farm_get_snap_file,
-	.format = default_format,
-	.purge_obj = default_purge_obj,
-	.remove_object = default_remove_object,
-};
-
-add_store_driver(farm);
diff --git a/sheep/farm/farm.h b/sheep/farm/farm.h
deleted file mode 100644
index 0fbdf12..0000000
--- a/sheep/farm/farm.h
+++ /dev/null
@@ -1,68 +0,0 @@
-#ifndef FARM_H
-#define FARM_H
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <inttypes.h>
-#include <memory.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <sys/mman.h>
-#include <linux/limits.h>
-
-#include "sheepdog_proto.h"
-#include "sheep.h"
-#include "logger.h"
-#include "strbuf.h"
-#include "sha1.h"
-
-#define HEX_LEN         40
-#define NAME_LEN        HEX_LEN
-
-#define TAG_LEN         6
-#define TAG_DATA        "data\0\0"
-#define TAG_TRUNK       "trunk\0"
-#define TAG_SNAP        "snap\0\0"
-
-struct sha1_file_hdr {
-	char tag[TAG_LEN];
-	uint64_t size;
-	uint64_t priv;
-	uint64_t reserved;
-};
-
-struct trunk_entry {
-	uint64_t oid;
-	unsigned char sha1[SHA1_LEN];
-};
-
-/* farm.c */
-extern char farm_dir[PATH_MAX];
-extern char farm_obj_dir[PATH_MAX];
-
-/* sha1_file.c */
-char *sha1_to_path(const unsigned char *sha1);
-int sha1_file_write(unsigned char *buf, unsigned len, unsigned char *);
-void *sha1_file_read(const unsigned char *sha1, struct sha1_file_hdr *);
-char *sha1_to_hex(const unsigned char *sha1);
-int get_sha1_hex(const char *hex, unsigned char *sha1);
-int sha1_file_try_delete(const unsigned char *sha1);
-
-/* trunk.c */
-int trunk_init(void);
-int trunk_file_write(unsigned char *outsha1);
-void *trunk_file_read(unsigned char *sha1, struct sha1_file_hdr *);
-
-/* snap.c */
-int snap_init(void);
-void *snap_file_read(unsigned char *sha1, struct sha1_file_hdr *outhdr);
-int snap_file_write(uint32_t epoch, struct sd_node *nodes, int nr_nodes,
-		unsigned char *trunksha1, unsigned char *outsha1);
-int snap_log_truncate(void);
-void *snap_log_read(int *);
-int snap_log_write(uint32_t epoch, unsigned char *sha1);
-
-#endif
diff --git a/sheep/farm/sha1_file.c b/sheep/farm/sha1_file.c
deleted file mode 100644
index 3883d95..0000000
--- a/sheep/farm/sha1_file.c
+++ /dev/null
@@ -1,273 +0,0 @@
-/*
- * Copyright (C) 2011 Taobao Inc.
- *
- * Liu Yuan <namei.unix at gmail.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License version
- * 2 as published by the Free Software Foundation.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/*
- *   sha1_file provide us some useful features:
- *
- *   - Regardless of object type, all objects are all in deflated with zlib,
- *     and have a header that not only specifies their tag, but also size
- *     information about the data in the object.
- *
- *   - the general consistency of an object can always be tested independently
- *     of the contents or the type of the object: all objects can be validated
- *     by verifying that their hashes match the content of the file.
- */
-#include <sys/types.h>
-#include <sys/xattr.h>
-
-#include "farm.h"
-#include "util.h"
-
-static inline char *get_object_directory(void)
-{
-	return farm_obj_dir;
-}
-
-static void fill_sha1_path(char *pathbuf, const unsigned char *sha1)
-{
-	int i;
-	for (i = 0; i < SHA1_LEN; i++) {
-		static const char hex[] = "0123456789abcdef";
-		unsigned int val = sha1[i];
-		char *pos = pathbuf + i*2 + (i > 0);
-		*pos++ = hex[val >> 4];
-		*pos = hex[val & 0xf];
-	}
-}
-
-char *sha1_to_path(const unsigned char *sha1)
-{
-
-	static char buf[PATH_MAX];
-	const char *objdir;
-	int len;
-
-	objdir = get_object_directory();
-	len = strlen(objdir);
-
-	/* '/' + sha1(2) + '/' + sha1(38) + '\0' */
-	memcpy(buf, objdir, len);
-	buf[len] = '/';
-	buf[len+3] = '/';
-	buf[len+42] = '\0';
-	fill_sha1_path(buf + len + 1, sha1);
-	return buf;
-}
-
-#define CNAME	"user.farm.count"
-#define CSIZE	sizeof(uint32_t)
-
-static void get_sha1_file(char *name)
-{
-	uint32_t count;
-	if (getxattr(name, CNAME, &count, CSIZE) < 0) {
-		if (errno == ENODATA) {
-			count = 1;
-			if (setxattr(name, CNAME, &count, CSIZE, 0) < 0)
-				panic("%m");
-			return;
-		} else
-			panic("%m");
-	}
-	count++;
-	if (setxattr(name, CNAME, &count, CSIZE, 0) < 0)
-		panic("%m");
-}
-
-static int put_sha1_file(char *name)
-{
-	uint32_t count;
-
-	if (getxattr(name, CNAME, &count, CSIZE) < 0) {
-		if (errno == ENOENT) {
-			sd_dprintf("sha1 file doesn't exist");
-			return -1;
-		} else
-			panic("%m");
-	}
-
-	count--;
-	if (count == 0) {
-		if (unlink(name) < 0) {
-			sd_dprintf("%m");
-			return -1;
-		}
-		sd_dprintf("%s deleted", name);
-	} else {
-		if (setxattr(name, CNAME, &count, CSIZE, 0) < 0)
-			panic("%m");
-	}
-	return 0;
-}
-
-static int sha1_buffer_write(const unsigned char *sha1, void *buf, unsigned int size)
-{
-	char *filename = sha1_to_path(sha1);
-	int fd, ret = 0, len;
-
-	fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
-	if (fd < 0) {
-		if (errno != EEXIST)
-			ret = -1;
-		goto err_open;
-	}
-	len = xwrite(fd, buf, size);
-	if (len != size) {
-		sd_dprintf("%m");
-		close(fd);
-		return -1;
-	}
-
-	close(fd);
-	get_sha1_file(filename);
-err_open:
-	return ret;
-}
-
-int sha1_file_write(unsigned char *buf, unsigned len, unsigned char *outsha1)
-{
-	unsigned char sha1[SHA1_LEN];
-	struct sha1_ctx c;
-
-	sha1_init(&c);
-	sha1_update(&c, buf, len);
-	sha1_final(&c, sha1);
-
-	if (sha1_buffer_write(sha1, buf, len) < 0)
-		return -1;
-	if (outsha1)
-		memcpy(outsha1, sha1, SHA1_LEN);
-	return 0;
-}
-
-static void *map_sha1_file(const unsigned char *sha1, unsigned long *size)
-{
-	char *filename = sha1_to_path(sha1);
-	int fd = open(filename, O_RDONLY);
-	struct stat st;
-	void *map;
-
-	if (fd < 0) {
-		perror(filename);
-		return NULL;
-	}
-	if (fstat(fd, &st) < 0) {
-		sd_dprintf("%m");
-		close(fd);
-		return NULL;
-	}
-	map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-	close(fd);
-	if (map == MAP_FAILED) {
-		sd_dprintf("%m");
-		return NULL;
-	}
-	*size = st.st_size;
-	return map;
-}
-
-static void *unpack_sha1_file(void *map, unsigned long mapsize, struct sha1_file_hdr *hdr)
-{
-	int hdr_len;
-	char *buf;
-
-	memcpy(hdr, map, sizeof(*hdr));
-	hdr_len = sizeof(*hdr);
-	buf = valloc(hdr->size);
-	if (!buf) {
-		sd_dprintf("%m");
-		return NULL;
-	}
-
-	memcpy(buf, (char *)map + hdr_len, mapsize - hdr_len);
-	return buf;
-}
-
-static int verify_sha1_file(const unsigned char *sha1, void *buf, unsigned long len)
-{
-	unsigned char tmp[SHA1_LEN];
-	struct sha1_ctx c;
-
-	sha1_init(&c);
-	sha1_update(&c, buf, len);
-	sha1_final(&c, tmp);
-
-	if (memcmp((char *)tmp, (char *)sha1, SHA1_LEN) != 0) {
-		sd_dprintf("failed, %s != %s", sha1_to_hex(sha1),
-			   sha1_to_hex(tmp));
-		return -1;
-	}
-	return 0;
-}
-
-void *sha1_file_read(const unsigned char *sha1, struct sha1_file_hdr *hdr)
-{
-	unsigned long mapsize;
-	void *map, *buf;
-
-	map = map_sha1_file(sha1, &mapsize);
-	if (map) {
-		if (verify_sha1_file(sha1, map, mapsize) < 0)
-			return NULL;
-		buf = unpack_sha1_file(map, mapsize, hdr);
-		munmap(map, mapsize);
-		return buf;
-	}
-	return NULL;
-}
-
-int sha1_file_try_delete(const unsigned char *sha1)
-{
-	char *filename = sha1_to_path(sha1);
-
-	return put_sha1_file(filename);
-}
-
-static unsigned hexval(char c)
-{
-	if (c >= '0' && c <= '9')
-		return c - '0';
-	if (c >= 'a' && c <= 'f')
-		return c - 'a' + 10;
-	if (c >= 'A' && c <= 'F')
-		return c - 'A' + 10;
-	return ~0;
-}
-
-int get_sha1_hex(const char *hex, unsigned char *sha1)
-{
-	int i;
-	for (i = 0; i < SHA1_LEN; i++) {
-		unsigned int val = (hexval(hex[0]) << 4) | hexval(hex[1]);
-		if (val & ~0xff)
-			return -1;
-		*sha1++ = val;
-		hex += 2;
-	}
-	return 0;
-}
-
-char *sha1_to_hex(const unsigned char *sha1)
-{
-	static char buffer[50];
-	static const char hex[] = "0123456789abcdef";
-	char *buf = buffer;
-	int i;
-
-	for (i = 0; i < SHA1_LEN; i++) {
-		unsigned int val = *sha1++;
-		*buf++ = hex[val >> 4];
-		*buf++ = hex[val & 0xf];
-	}
-	return buffer;
-}
diff --git a/sheep/farm/snap.c b/sheep/farm/snap.c
deleted file mode 100644
index 0cc18ba..0000000
--- a/sheep/farm/snap.c
+++ /dev/null
@@ -1,152 +0,0 @@
-/*
- * Copyright (C) 2011 Taobao Inc.
- *
- * Liu Yuan <namei.unix at gmail.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License version
- * 2 as published by the Free Software Foundation.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/*
- * Snap object is the meta data that describes the snapshot, either triggered
- * by recovery logic or end users.
- */
-#include <time.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-#include "farm.h"
-#include "sheep_priv.h"
-
-int snap_init(void)
-{
-	int fd, ret = 0;
-	struct strbuf buf = STRBUF_INIT;
-
-	strbuf_addstr(&buf, farm_dir);
-	strbuf_addf(&buf, "/%s", "user_snap");
-
-	fd = open(buf.buf, O_CREAT | O_EXCL, 0666);
-	if (fd < 0) {
-		if (errno != EEXIST) {
-			ret = -1;
-			goto out;
-		}
-	} else
-		close(fd);
-
-out:
-	strbuf_release(&buf);
-	return ret;
-}
-
-int snap_log_write(uint32_t epoch, unsigned char *sha1)
-{
-	int fd, ret = -1;
-	struct strbuf buf = STRBUF_INIT;
-	struct snap_log log = { .epoch = epoch,
-				.time = time(NULL) };
-
-	memcpy(log.sha1, sha1, SHA1_LEN);
-	strbuf_addstr(&buf, farm_dir);
-	strbuf_addf(&buf, "/%s", "user_snap");
-
-	fd = open(buf.buf, O_WRONLY | O_APPEND);
-	if (fd < 0) {
-		sd_dprintf("%m");
-		goto out;
-	}
-
-	strbuf_reset(&buf);
-	strbuf_add(&buf, &log, sizeof(log));
-	ret = xwrite(fd, buf.buf, buf.len);
-	if (ret != buf.len)
-		ret = -1;
-
-	close(fd);
-out:
-	strbuf_release(&buf);
-	return ret;
-}
-
-void *snap_log_read(int *out_nr)
-{
-	struct strbuf buf = STRBUF_INIT;
-	struct stat st;
-	void *buffer = NULL;
-	int len, fd;
-
-	strbuf_addstr(&buf, farm_dir);
-	strbuf_addf(&buf, "/%s", "user_snap");
-
-	fd = open(buf.buf, O_RDONLY);
-	if (fd < 0) {
-		sd_dprintf("%m");
-		goto out;
-	}
-	if (fstat(fd, &st) < 0) {
-		sd_dprintf("%m");
-		goto out_close;
-	}
-
-	len = st.st_size;
-	buffer = xmalloc(len);
-	len = xread(fd, buffer, len);
-	if (len != st.st_size) {
-		free(buffer);
-		buffer = NULL;
-		goto out_close;
-	}
-	*out_nr = len / sizeof(struct snap_log);
-out_close:
-	close(fd);
-out:
-	strbuf_release(&buf);
-	return buffer;
-}
-
-void *snap_file_read(unsigned char *sha1, struct sha1_file_hdr *outhdr)
-{
-	void *buffer = NULL;
-
-	sd_dprintf("%s", sha1_to_hex(sha1));
-	buffer = sha1_file_read(sha1, outhdr);
-	if (!buffer)
-		return NULL;
-	if (strcmp(outhdr->tag, TAG_SNAP) != 0) {
-		free(buffer);
-		return NULL;
-	}
-
-	return buffer;
-}
-
-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 sha1_file_hdr hdr = {};
-
-	memcpy(hdr.tag, TAG_SNAP, TAG_LEN);
-	hdr.size = nr_nodes * sizeof(*nodes) + SHA1_LEN;
-	hdr.priv = epoch;
-	hdr.reserved = 0;
-
-	strbuf_add(&buf, &hdr, sizeof(hdr));
-	strbuf_add(&buf, trunksha1, SHA1_LEN);
-	strbuf_add(&buf, (char *)nodes, nr_nodes * sizeof(*nodes));
-	if (sha1_file_write((void *)buf.buf, buf.len, outsha1) < 0) {
-		ret = -1;
-		goto err;
-	}
-
-	sd_dprintf("epoch: %" PRIu32 ", sha1: %s", epoch, sha1_to_hex(outsha1));
-err:
-	strbuf_release(&buf);
-	return ret;
-}
diff --git a/sheep/farm/trunk.c b/sheep/farm/trunk.c
deleted file mode 100644
index eaa4193..0000000
--- a/sheep/farm/trunk.c
+++ /dev/null
@@ -1,139 +0,0 @@
-/*
- * Copyright (C) 2011 Taobao Inc.
- *
- * Liu Yuan <namei.unix at gmail.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License version
- * 2 as published by the Free Software Foundation.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/*
- * Trunk object is meta data that describes the structure of the data objects
- * at the time of snapshot being taken. It ties data objects together into a
- * flat directory structure.
- */
-#include <pthread.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-#include "farm.h"
-#include "strbuf.h"
-#include "list.h"
-#include "util.h"
-#include "sheepdog_proto.h"
-#include "sheep_priv.h"
-
-static int fill_entry_new_sha1(struct trunk_entry *entry)
-{
-	struct strbuf buf = STRBUF_INIT;
-	int fd, ret = 0;
-	struct sha1_file_hdr hdr = { .priv = 0 };
-
-	memcpy(hdr.tag, TAG_DATA, TAG_LEN);
-	strbuf_addstr(&buf, get_object_path(entry->oid));
-	strbuf_addf(&buf, "/%016" PRIx64, entry->oid);
-	fd = open(buf.buf, O_RDONLY);
-	if (fd < 0) {
-		sd_dprintf("%m, %s", buf.buf);
-		ret = -1;
-		goto out;
-	}
-	strbuf_reset(&buf);
-	if (!strbuf_read(&buf, fd, SD_DATA_OBJ_SIZE) == SD_DATA_OBJ_SIZE) {
-		sd_dprintf("strbuf_read fail to read full");
-		ret = -1;
-		goto out_close;
-	}
-	hdr.size = buf.len;
-	strbuf_insert(&buf, 0, &hdr, sizeof(hdr));
-
-	if (sha1_file_write((void *)buf.buf, buf.len, entry->sha1) < 0) {
-		ret = -1;
-		goto out_close;
-	}
-	sd_dprintf("data sha1:%s, %"PRIx64, sha1_to_hex(entry->sha1),
-		   entry->oid);
-out_close:
-	close(fd);
-out:
-	strbuf_release(&buf);
-	return ret;
-}
-
-static int inc_object_nr(uint64_t oid, char *wd, void *arg)
-{
-	uint64_t *object_nr = arg;
-
-	(*object_nr)++;
-
-	return SD_RES_SUCCESS;
-}
-
-static int init_trunk_entry(uint64_t oid, char *path, void *arg)
-{
-	struct trunk_entry entry = {};
-	struct strbuf *buf = arg;
-
-	entry.oid = oid;
-	if (fill_entry_new_sha1(&entry) < 0)
-		return SD_RES_UNKNOWN;
-
-	strbuf_add(buf, &entry, sizeof(struct trunk_entry));
-	return SD_RES_SUCCESS;
-}
-
-int trunk_file_write(unsigned char *outsha1)
-{
-	struct strbuf buf;
-	struct sha1_file_hdr hdr = {};
-	uint64_t data_size, object_nr = 0;
-	int ret = 0;
-
-	/* Add the hdr first */
-	for_each_object_in_wd(inc_object_nr, false, &object_nr);
-	if (ret != SD_RES_SUCCESS) {
-		ret = -1;
-		goto out;
-	}
-	data_size = sizeof(struct trunk_entry) * object_nr;
-	hdr.size = data_size;
-	hdr.priv = object_nr;
-	memcpy(hdr.tag, TAG_TRUNK, TAG_LEN);
-	strbuf_init(&buf, sizeof(hdr) + data_size);
-	strbuf_add(&buf, &hdr, sizeof(hdr));
-
-	ret = for_each_object_in_wd(init_trunk_entry, false,  &buf);
-	if (ret != SD_RES_SUCCESS) {
-		ret = -1;
-		goto out;
-	}
-
-	if (sha1_file_write((void *)buf.buf, buf.len, outsha1) < 0) {
-		ret = -1;
-		goto out;
-	}
-	sd_dprintf("trunk sha1: %s", sha1_to_hex(outsha1));
-out:
-	strbuf_release(&buf);
-	return ret;
-}
-
-void *trunk_file_read(unsigned char *sha1, struct sha1_file_hdr *outhdr)
-{
-	void *buffer;
-
-	sd_dprintf("%s", sha1_to_hex(sha1));
-	buffer = sha1_file_read(sha1, outhdr);
-	if (!buffer)
-		return NULL;
-	if (strcmp(outhdr->tag, TAG_TRUNK) != 0) {
-		free(buffer);
-		return NULL;
-	}
-
-	return buffer;
-}
diff --git a/sheep/ops.c b/sheep/ops.c
index e40fc65..56bcf62 100644
--- a/sheep/ops.c
+++ b/sheep/ops.c
@@ -591,20 +591,6 @@ err:
 	panic("failed in force recovery");
 }
 
-static int cluster_snapshot(const struct sd_req *req, struct sd_rsp *rsp,
-			    void *data)
-{
-	int ret;
-	struct siocb iocb = { 0 };
-
-	if (sd_store->snapshot)
-		ret = sd_store->snapshot(&iocb);
-	else
-		ret = SD_RES_NO_SUPPORT;
-
-	return ret;
-}
-
 static int cluster_cleanup(const struct sd_req *req, struct sd_rsp *rsp,
 				void *data)
 {
@@ -761,33 +747,6 @@ static int local_md_unplug(const struct sd_req *req, struct sd_rsp *rsp,
 	return md_unplug_disks(disks);
 }
 
-static int cluster_restore(const struct sd_req *req, struct sd_rsp *rsp,
-			   void *data)
-{
-	int ret;
-	struct siocb iocb = { .epoch = req->obj.tgt_epoch };
-
-	if (sd_store->restore)
-		ret = sd_store->restore(&iocb);
-	else
-		ret = SD_RES_NO_SUPPORT;
-	return ret;
-}
-
-static int local_get_snap_file(struct request *req)
-{
-	int ret;
-	struct siocb iocb = { .buf = req->data };
-
-	if (sd_store->get_snap_file) {
-		ret = sd_store->get_snap_file(&iocb);
-		req->rp.data_length = iocb.length;
-	} else
-		ret = SD_RES_NO_SUPPORT;
-
-	return ret;
-}
-
 /* Return SD_RES_INVALID_PARMS to ask client not to send flush req again */
 static int local_flush_vdi(struct request *req)
 {
@@ -1051,20 +1010,6 @@ static struct sd_op_template sd_ops[] = {
 		.process_main = cluster_force_recover_main,
 	},
 
-	[SD_OP_SNAPSHOT] = {
-		.name = "SNAPSHOT",
-		.type = SD_OP_TYPE_CLUSTER,
-		.force = true,
-		.process_main = cluster_snapshot,
-	},
-
-	[SD_OP_RESTORE] = {
-		.name = "RESTORE",
-		.type = SD_OP_TYPE_CLUSTER,
-		.force = true,
-		.process_main = cluster_restore,
-	},
-
 	[SD_OP_CLEANUP] = {
 		.name = "CLEANUP",
 		.type = SD_OP_TYPE_CLUSTER,
@@ -1177,13 +1122,6 @@ static struct sd_op_template sd_ops[] = {
 		.process_work = local_get_epoch,
 	},
 
-	[SD_OP_GET_SNAP_FILE] = {
-		.name = "GET_SNAP_FILE",
-		.type = SD_OP_TYPE_LOCAL,
-		.force = true,
-		.process_work = local_get_snap_file,
-	},
-
 	[SD_OP_FLUSH_VDI] = {
 		.name = "FLUSH_VDI",
 		.type = SD_OP_TYPE_LOCAL,
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index ab2636a..003532a 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -26,6 +26,9 @@
 #include "rbtree.h"
 #include "strbuf.h"
 
+#define HEX_LEN  40
+#define NAME_LEN HEX_LEN
+
 struct client_info {
 	struct connection conn;
 
@@ -171,10 +174,7 @@ struct store_driver {
 	int (*update_epoch)(uint32_t epoch);
 	int (*purge_obj)(void);
 	/* Operations for snapshot */
-	int (*snapshot)(const struct siocb *);
 	int (*cleanup)(void);
-	int (*restore)(const struct siocb *);
-	int (*get_snap_file)(struct siocb *);
 };
 
 int default_init(void);
diff --git a/sheep/store.c b/sheep/store.c
index 28e2ab7..4bcdaca 100644
--- a/sheep/store.c
+++ b/sheep/store.c
@@ -26,7 +26,6 @@
 #include "sheep_priv.h"
 #include "strbuf.h"
 #include "util.h"
-#include "farm/farm.h"
 
 char *obj_path;
 char *epoch_path;
-- 
1.7.1




More information about the sheepdog mailing list