When writeback is enabled, SD_FLAG_CMD_CACHE flag is set to I/O
requests.  This patch uses it for write mode.

Signed-off-by: MORITA Kazutaka <[email protected]>
---
 sheep/object_cache.c |   83 ++++++++++++-------------------------------------
 sheep/request.c      |    2 +-
 sheep/sheep.c        |   13 +------
 sheep/sheep_priv.h   |    1 -
 4 files changed, 24 insertions(+), 75 deletions(-)

diff --git a/sheep/object_cache.c b/sheep/object_cache.c
index a722259..431fe26 100644
--- a/sheep/object_cache.c
+++ b/sheep/object_cache.c
@@ -75,9 +75,6 @@ struct object_cache {
        struct rb_root object_tree;
 
        pthread_rwlock_t lock;
-
-       int (*read)(struct object_cache_entry *, void *, size_t, off_t);
-       int (*write)(struct object_cache_entry *, void *, size_t, off_t, int);
 };
 
 static struct global_cache sys_cache;
@@ -372,19 +369,6 @@ out:
        return ret;
 }
 
-static int write_cache_object(struct object_cache_entry *entry, void *buf,
-                             size_t count, off_t offset, int create)
-{
-       uint32_t vid = entry->oc->vid, idx = entry_idx(entry);
-       int ret;
-
-       ret = write_cache_object_noupdate(vid, idx, buf, count, offset);
-
-       if (ret == SD_RES_SUCCESS)
-               update_cache_entry(entry, idx, count, offset, 1);
-       return ret;
-}
-
 static int read_cache_object(struct object_cache_entry *entry, void *buf,
                             size_t count, off_t offset)
 {
@@ -398,9 +382,9 @@ static int read_cache_object(struct object_cache_entry 
*entry, void *buf,
        return ret;
 }
 
-static int write_and_push_cache_object(struct object_cache_entry *entry,
-                                      void *buf, size_t count, off_t offset,
-                                      int create)
+static int write_cache_object(struct object_cache_entry *entry, void *buf,
+                             size_t count, off_t offset, int create,
+                             bool writeback)
 {
        uint32_t vid = entry->oc->vid, idx = entry_idx(entry);
        uint64_t oid = idx_to_oid(vid, idx);
@@ -412,6 +396,9 @@ static int write_and_push_cache_object(struct 
object_cache_entry *entry,
        if (ret != SD_RES_SUCCESS)
                return ret;
 
+       if (writeback)
+               goto out;
+
        if (create)
                sd_init_req(&hdr, SD_OP_CREATE_AND_WRITE_OBJ);
        else
@@ -427,8 +414,8 @@ static int write_and_push_cache_object(struct 
object_cache_entry *entry,
                eprintf("failed to write object %" PRIx64 ", %x\n", oid, ret);
                return ret;
        }
-
-       update_cache_entry(entry, idx, count, offset, 0);
+out:
+       update_cache_entry(entry, idx, count, offset, writeback);
        return ret;
 }
 
@@ -618,12 +605,6 @@ not_found:
 
                pthread_rwlock_init(&cache->lock, NULL);
                hlist_add_head(&cache->hash, head);
-
-               cache->read = read_cache_object;
-               if (sys->writethrough)
-                       cache->write = write_and_push_cache_object;
-               else
-                       cache->write = write_cache_object;
        } else {
                cache = NULL;
        }
@@ -690,7 +671,7 @@ static void add_to_object_cache(struct object_cache *oc, 
uint32_t idx,
 }
 
 static int object_cache_lookup(struct object_cache *oc, uint32_t idx,
-                              int create)
+                              int create, bool writeback)
 {
        struct strbuf buf;
        int fd, ret = SD_RES_SUCCESS, flags = def_open_flags;
@@ -728,12 +709,9 @@ static int object_cache_lookup(struct object_cache *oc, 
uint32_t idx,
        ret = prealloc(fd, data_length);
        if (ret != SD_RES_SUCCESS) {
                ret = SD_RES_EIO;
-       } else {
-               if (sys->writethrough)
-                       add_to_object_cache(oc, idx, 0);
-               else
-                       add_to_object_cache(oc, idx, 1);
-       }
+       } else
+               add_to_object_cache(oc, idx, writeback);
+
        close(fd);
 out:
        strbuf_release(&buf);
@@ -851,7 +829,7 @@ int object_is_cached(uint64_t oid)
        if (!cache)
                return 0;
 
-       return (object_cache_lookup(cache, idx, 0) == SD_RES_SUCCESS);
+       return (object_cache_lookup(cache, idx, 0, false) == SD_RES_SUCCESS);
 }
 
 void object_cache_delete(uint32_t vid)
@@ -960,27 +938,6 @@ int bypass_object_cache(struct request *req)
 {
        uint64_t oid = req->rq.obj.oid;
 
-       if (!(req->rq.flags & SD_FLAG_CMD_CACHE)) {
-               uint32_t vid = oid_to_vid(oid);
-               struct object_cache *cache;
-
-               cache = find_object_cache(vid, 0);
-               if (!cache)
-                       return 1;
-               if (req->rq.flags & SD_FLAG_CMD_WRITE) {
-                       object_cache_flush_and_delete(cache);
-                       return 1;
-               } else  {
-                       /* For read requet, we can read cache if any */
-                       uint32_t idx = object_cache_oid_to_idx(oid);
-
-                       if (object_cache_lookup(cache, idx, 0) == 0)
-                               return 0;
-                       else
-                               return 1;
-               }
-       }
-
        /*
         * For vmstate && vdi_attr object, we don't do caching
         */
@@ -1009,7 +966,8 @@ int object_cache_handle_request(struct request *req)
                create = 1;
 
 retry:
-       ret = object_cache_lookup(cache, idx, create);
+       ret = object_cache_lookup(cache, idx, create,
+                                 hdr->flags & SD_FLAG_CMD_CACHE);
        if (ret == SD_RES_NO_CACHE) {
                ret = object_cache_pull(cache, idx);
                if (ret != SD_RES_SUCCESS)
@@ -1030,13 +988,14 @@ retry:
        }
 
        if (hdr->flags & SD_FLAG_CMD_WRITE) {
-               ret = cache->write(entry, req->data, hdr->data_length,
-                                  hdr->obj.offset, create);
+               ret = write_cache_object(entry, req->data, hdr->data_length,
+                                        hdr->obj.offset, create,
+                                        hdr->flags & SD_FLAG_CMD_CACHE);
                if (ret != SD_RES_SUCCESS)
                        goto err;
        } else {
-               ret = cache->read(entry, req->data, hdr->data_length,
-                                 hdr->obj.offset);
+               ret = read_cache_object(entry, req->data, hdr->data_length,
+                                       hdr->obj.offset);
                if (ret != SD_RES_SUCCESS)
                        goto err;
                req->rp.data_length = hdr->data_length;
@@ -1065,7 +1024,7 @@ int object_cache_write(uint64_t oid, char *data, unsigned 
int datalen,
                return SD_RES_NO_CACHE;
        }
 
-       ret = write_cache_object(entry, data, datalen, offset, create);
+       ret = write_cache_object(entry, data, datalen, offset, create, false);
 
        put_cache_entry(entry);
 
diff --git a/sheep/request.c b/sheep/request.c
index fd210d3..7c2dec8 100644
--- a/sheep/request.c
+++ b/sheep/request.c
@@ -287,7 +287,7 @@ static void queue_gateway_request(struct request *req)
         * Even if it doesn't exist in cache, we'll rely on cache layer to pull
         * it.
         */
-       if (sys->enable_write_cache && req->rq.flags & SD_FLAG_CMD_CACHE)
+       if (sys->enable_write_cache)
                goto queue_work;
 
        if (req->local_oid)
diff --git a/sheep/sheep.c b/sheep/sheep.c
index 36e059a..c168d26 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -197,7 +197,6 @@ int main(int argc, char **argv)
        struct cluster_driver *cdrv;
        int enable_object_cache = 0; /* disabled by default */
        char *pid_file = NULL;
-       char *object_cache_size, *object_cache_mode;
 
        signal(SIGPIPE, SIG_IGN);
 
@@ -266,9 +265,7 @@ int main(int argc, char **argv)
                        break;
                case 'w':
                        enable_object_cache = 1;
-                       object_cache_size = strtok(optarg, ",");
-                       object_cache_mode = strtok(NULL, ",");
-                       cache_size = strtol(object_cache_size, &p, 10);
+                       cache_size = strtol(optarg, &p, 10);
                        if (optarg == p || cache_size < 0 ||
                            UINT64_MAX < cache_size) {
                                fprintf(stderr, "Invalid cache size '%s': "
@@ -278,14 +275,8 @@ int main(int argc, char **argv)
                        }
                        sys->cache_size = cache_size * 1024 * 1024;
 
-                       if (!object_cache_mode ||
-                           strcmp(object_cache_mode, "writeback") != 0) {
-                               sys->writethrough = 1;
-                       }
                        fprintf(stdout, "enable write cache, "
-                               "max cache size %" PRIu64 "M, %s mode\n",
-                               cache_size, sys->writethrough ?
-                               "writethrough" : "writeback");
+                               "max cache size %" PRIu64 "M\n", cache_size);
                        break;
                case 's':
                        free_space = strtoll(optarg, &p, 10);
diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index 90006f6..dce1ae5 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -109,7 +109,6 @@ struct cluster_info {
        int use_directio;
        uint8_t gateway_only;
        uint8_t disable_recovery;
-       uint8_t writethrough;
 
        struct work_queue *gateway_wqueue;
        struct work_queue *io_wqueue;
-- 
1.7.2.5

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

Reply via email to