[sheepdog] [PATCH 3/5] sheep: teach journal file to handle epoch and config write
Liu Yuan
namei.unix at gmail.com
Tue Apr 2 11:34:24 CEST 2013
From: Liu Yuan <tailai.ly at taobao.com>
journal_write_epoch and journal_write_confi is a placeholder for future use.
Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
---
sheep/journal_file.c | 96 +++++++++++++++++++++++++++++++++++++++-----------
sheep/plain_store.c | 7 ++--
sheep/sheep_priv.h | 5 ++-
3 files changed, 84 insertions(+), 24 deletions(-)
diff --git a/sheep/journal_file.c b/sheep/journal_file.c
index 4ae0e4b..eccd6b6 100644
--- a/sheep/journal_file.c
+++ b/sheep/journal_file.c
@@ -30,8 +30,12 @@ struct journal_file {
struct journal_descriptor {
uint32_t magic;
- uint32_t reserved;
- uint64_t oid;
+ uint16_t flag;
+ uint16_t reserved;
+ union {
+ uint32_t epoch;
+ uint64_t oid;
+ };
uint64_t offset;
uint64_t size;
uint8_t create;
@@ -46,6 +50,10 @@ struct journal_descriptor {
#define JOURNAL_END_MARKER 0xdeadbeef
+#define JF_STORE 0
+#define JF_EPOCH 1
+#define JF_CONFI 2
+
static const char *jfile_name[2] = { "journal_file0", "journal_file1", };
static int jfile_fds[2];
static size_t jfile_size;
@@ -128,6 +136,27 @@ static bool journal_entry_full_write(struct journal_descriptor *jd)
return true;
}
+static void journal_get_path(struct journal_descriptor *jd, char *path)
+{
+ switch (jd->flag) {
+ case JF_STORE:
+ snprintf(path, PATH_MAX, "%s/%016"PRIx64,
+ get_object_path(jd->oid), jd->oid);
+ sd_iprintf("%s, size %"PRIu64", off %"PRIu64", %d",
+ path, jd->size, jd->offset, jd->create);
+ break;
+ case JF_EPOCH:
+ snprintf(path, PATH_MAX, "%s/%08"PRIu32, epoch_path, jd->epoch);
+ sd_iprintf("%s, %"PRIu32" size %"PRIu64,
+ path, jd->epoch, jd->size);
+ break;
+ case JF_CONFI:
+ snprintf(path, PATH_MAX, "%s", config_path);
+ sd_iprintf("%s, size %"PRIu64, path, jd->size);
+ break;
+ }
+}
+
static int replay_journal_entry(struct journal_descriptor *jd)
{
char path[PATH_MAX];
@@ -136,20 +165,17 @@ static int replay_journal_entry(struct journal_descriptor *jd)
void *buf;
char *p = (char *)jd;
- sd_dprintf("%"PRIx64", size %"PRIu64", off %"PRIu64", %d", jd->oid,
- jd->size, jd->offset, jd->create);
-
if (jd->create)
flags |= O_CREAT;
- snprintf(path, sizeof(path), "%s/%016" PRIx64, get_object_path(jd->oid),
- jd->oid);
+
+ journal_get_path(jd, path);
fd = open(path, flags, def_fmode);
if (fd < 0) {
sd_eprintf("open %m");
return -1;
}
- if (jd->create) {
+ if (jd->create && jd->flag == JF_STORE) {
ret = prealloc(fd, get_objsize(jd->oid));
if (ret < 0)
goto out;
@@ -315,22 +341,15 @@ retry:
panic("%s", strerror(err));
}
-int journal_file_write(uint64_t oid, const char *buf, size_t size,
- off_t offset, bool create)
+static int journal_file_write(struct journal_descriptor *jd, const char *buf)
{
uint32_t marker = JOURNAL_END_MARKER;
int ret = SD_RES_SUCCESS;
+ uint64_t size = jd->size;
ssize_t written, rusize = roundup(size, SECTOR_SIZE),
wsize = JOURNAL_META_SIZE + rusize;
off_t woff;
char *wbuffer, *p;
- struct journal_descriptor jd = {
- .magic = JOURNAL_DESC_MAGIC,
- .offset = offset,
- .size = size,
- .oid = oid,
- .create = create,
- };
pthread_spin_lock(&jfile_lock);
if (!jfile_enough_space(wsize))
@@ -340,7 +359,7 @@ int journal_file_write(uint64_t oid, const char *buf, size_t size,
pthread_spin_unlock(&jfile_lock);
p = wbuffer = xvalloc(wsize);
- memcpy(p, &jd, JOURNAL_DESC_SIZE);
+ memcpy(p, jd, JOURNAL_DESC_SIZE);
p += JOURNAL_DESC_SIZE;
memcpy(p, buf, size);
p += size;
@@ -349,8 +368,6 @@ int journal_file_write(uint64_t oid, const char *buf, size_t size,
p += rusize - size;
}
memcpy(p, &marker, JOURNAL_MARKER_SIZE);
-
- sd_dprintf("oid %lx, pos %zu, wsize %zu", oid, jfile.pos, wsize);
/*
* Concurrent writes with the same FD is okay because we don't have any
* critical sections that need lock inside kernel write path, since we
@@ -369,3 +386,42 @@ out:
free(wbuffer);
return ret;
}
+
+int journal_write_store(uint64_t oid, const char *buf, size_t size,
+ off_t offset, bool create)
+{
+ struct journal_descriptor jd = {
+ .magic = JOURNAL_DESC_MAGIC,
+ .flag = JF_STORE,
+ .offset = offset,
+ .size = size,
+ .oid = oid,
+ .create = create,
+ };
+ return journal_file_write(&jd, buf);
+}
+
+int journal_write_epoch(const char *buf, size_t size, uint32_t epoch)
+{
+ struct journal_descriptor jd = {
+ .magic = JOURNAL_DESC_MAGIC,
+ .flag = JF_EPOCH,
+ .offset = 0,
+ .size = size,
+ .epoch = epoch,
+ .create = true,
+ };
+ return journal_file_write(&jd, buf);
+}
+
+int journal_write_confi(const char *buf, size_t size)
+{
+ struct journal_descriptor jd = {
+ .magic = JOURNAL_DESC_MAGIC,
+ .flag = JF_CONFI,
+ .offset = 0,
+ .size = size,
+ .create = true,
+ };
+ return journal_file_write(&jd, buf);
+}
diff --git a/sheep/plain_store.c b/sheep/plain_store.c
index 1352dc4..b5bb395 100644
--- a/sheep/plain_store.c
+++ b/sheep/plain_store.c
@@ -96,8 +96,8 @@ int default_write(uint64_t oid, const struct siocb *iocb)
get_obj_path(oid, path);
if (uatomic_is_true(&sys->use_journal) &&
- journal_file_write(oid, iocb->buf, iocb->length, iocb->offset,
- false)
+ journal_write_store(oid, iocb->buf, iocb->length, iocb->offset,
+ false)
!= SD_RES_SUCCESS) {
sd_eprintf("turn off journaling");
uatomic_set_false(&sys->use_journal);
@@ -292,7 +292,8 @@ int default_create_and_write(uint64_t oid, const struct siocb *iocb)
get_tmp_obj_path(oid, tmp_path);
if (uatomic_is_true(&sys->use_journal) &&
- journal_file_write(oid, iocb->buf, iocb->length, iocb->offset, true)
+ journal_write_store(oid, iocb->buf, iocb->length,
+ iocb->offset, true)
!= SD_RES_SUCCESS) {
sd_eprintf("turn off journaling");
uatomic_set_false(&sys->use_journal);
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index a067347..e8031fe 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -408,7 +408,10 @@ bool sheep_need_retry(uint32_t epoch);
/* journal_file.c */
int journal_file_init(const char *path, size_t size, bool skip);
-int journal_file_write(uint64_t oid, const char *buf, size_t size, off_t, bool);
+int
+journal_write_store(uint64_t oid, const char *buf, size_t size, off_t, bool);
+int journal_write_epoch(const char *buf, size_t size, uint32_t epoch);
+int journal_write_confi(const char *buf, size_t size);
/* md.c */
void md_add_disk(char *path);
--
1.7.9.5
More information about the sheepdog
mailing list