We need to modify all the direct accessing of data_vdi_id[] to common function
because the following change of index in inode.

Signed-off-by: Robin Dong <[email protected]>
---
 dog/cluster.c            |    7 ++--
 dog/common.c             |    7 +---
 dog/vdi.c                |   78 +++++++++++++++++----------------------------
 include/sheepdog_proto.h |    6 +++-
 lib/Makefile.am          |    2 +-
 lib/sd_inode.c           |   13 ++++++++
 sheep/vdi.c              |    7 ++--
 sheepfs/volume.c         |   15 +++-----
 8 files changed, 65 insertions(+), 70 deletions(-)
 create mode 100644 lib/sd_inode.c

diff --git a/dog/cluster.c b/dog/cluster.c
index c2f97ad..62b78d0 100644
--- a/dog/cluster.c
+++ b/dog/cluster.c
@@ -246,6 +246,7 @@ static void fill_object_tree(uint32_t vid, const char 
*name, const char *tag,
                             const struct sd_inode *i, void *data)
 {
        uint64_t vdi_oid = vid_to_vdi_oid(vid), vmstate_oid;
+       uint32_t vdi_id;
        int nr_objs, nr_vmstate_object;
 
        /* ignore active vdi */
@@ -258,9 +259,9 @@ static void fill_object_tree(uint32_t vid, const char 
*name, const char *tag,
        /* fill data object id */
        nr_objs = count_data_objs(i);
        for (uint64_t idx = 0; idx < nr_objs; idx++) {
-               if (i->data_vdi_id[idx]) {
-                       uint64_t oid = vid_to_data_oid(i->data_vdi_id[idx],
-                                                      idx);
+               vdi_id = sd_inode_get_vdi(i, idx);
+               if (vdi_id) {
+                       uint64_t oid = vid_to_data_oid(vdi_id, idx);
                        object_tree_insert(oid, i->nr_copies, i->copy_policy);
                }
        }
diff --git a/dog/common.c b/dog/common.c
index 5e7ce2e..5e4ed21 100644
--- a/dog/common.c
+++ b/dog/common.c
@@ -136,7 +136,7 @@ int parse_vdi(vdi_parser_func_t func, size_t size, void 
*data)
        struct sd_req req;
        struct sd_rsp *rsp = (struct sd_rsp *)&req;
        static DECLARE_BITMAP(vdi_inuse, SD_NR_VDIS);
-       unsigned int rlen = sizeof(vdi_inuse);
+       unsigned int rlen;
 
        sd_init_req(&req, SD_OP_READ_VDIS);
        req.data_length = sizeof(vdi_inuse);
@@ -165,10 +165,7 @@ int parse_vdi(vdi_parser_func_t func, size_t size, void 
*data)
                        continue;
 
                if (size > SD_INODE_HEADER_SIZE) {
-                       rlen = count_data_objs(&i) * sizeof(i.data_vdi_id[0]);
-                       if (rlen > size - SD_INODE_HEADER_SIZE)
-                               rlen = size - SD_INODE_HEADER_SIZE;
-
+                       rlen = size - SD_INODE_HEADER_SIZE;
                        ret = sd_read_object(oid, ((char *)&i) + 
SD_INODE_HEADER_SIZE,
                                             rlen, SD_INODE_HEADER_SIZE, true);
 
diff --git a/dog/vdi.c b/dog/vdi.c
index 08f154e..c50f886 100644
--- a/dog/vdi.c
+++ b/dog/vdi.c
@@ -73,46 +73,23 @@ static void stat_data_objs(const struct sd_inode *inode, 
uint64_t *my_objs,
                           uint64_t *cow_objs)
 {
        int nr;
-       uint64_t my, cow, *p;
+       uint64_t my, cow;
        uint32_t vid = inode->vdi_id;
+       uint32_t vdi_id;
 
        my = 0;
        cow = 0;
        nr = count_data_objs(inode);
 
-       if (nr % 2 != 0) {
-               if (is_data_obj_writeable(inode, 0))
-                       my++;
-               else if (inode->data_vdi_id[0] != 0)
-                       cow++;
-               p = (uint64_t *)(inode->data_vdi_id + 1);
-       } else
-               p = (uint64_t *)inode->data_vdi_id;
-
-       /*
-        * To boost performance, this function checks data_vdi_id for each 64
-        * bit integer.
-        */
-       nr /= 2;
        for (int i = 0; i < nr; i++) {
-               if (p[i] == 0)
+               vdi_id = sd_inode_get_vdi(inode, i);
+               if (vdi_id == 0)
                        continue;
-               if (p[i] == (((uint64_t)vid << 32) | vid)) {
-                       my += 2;
+               if (vdi_id == vid) {
+                       my++;
                        continue;
                }
-
-               /* Check the higher 32 bit */
-               if (p[i] >> 32 == vid)
-                       my++;
-               else if ((p[i] & 0xFFFFFFFF00000000) != 0)
-                       cow++;
-
-               /* Check the lower 32 bit */
-               if ((p[i] & 0xFFFFFFFF) == vid)
-                       my++;
-               else if ((p[i] & 0xFFFFFFFF) != 0)
-                       cow++;
+               cow++;
        }
 
        *my_objs = my;
@@ -293,14 +270,16 @@ static int obj_info_filler(const char *sheep, uint64_t 
oid, struct sd_rsp *rsp,
 {
        struct obj_info_filler_info *info = data;
        struct sd_inode *inode = (struct sd_inode *)buf;
+       uint32_t vdi_id;
 
        switch (rsp->result) {
        case SD_RES_SUCCESS:
                if (info->success)
                        break;
                info->success = true;
-               if (inode->data_vdi_id[info->idx]) {
-                       info->data_oid = 
vid_to_data_oid(inode->data_vdi_id[info->idx], info->idx);
+               vdi_id = sd_inode_get_vdi(inode, info->idx);
+               if (vdi_id) {
+                       info->data_oid = vid_to_data_oid(vdi_id, info->idx);
                        return 1;
                }
                break;
@@ -561,7 +540,7 @@ static int vdi_create(int argc, char **argv)
                        goto out;
                }
 
-               inode->data_vdi_id[idx] = vid;
+               sd_inode_set_vdi(inode, idx, vid);
                ret = sd_write_object(vid_to_vdi_oid(vid), 0, &vid, sizeof(vid),
                                      SD_INODE_HEADER_SIZE + sizeof(vid) * idx,
                                      0, inode->nr_copies, inode->copy_policy,
@@ -628,7 +607,7 @@ static int vdi_snapshot(int argc, char **argv)
 static int vdi_clone(int argc, char **argv)
 {
        const char *src_vdi = argv[optind++], *dst_vdi;
-       uint32_t base_vid, new_vid;
+       uint32_t base_vid, new_vid, vdi_id;
        uint64_t oid;
        int idx, max_idx, ret;
        struct sd_inode *inode = NULL;
@@ -668,8 +647,9 @@ static int vdi_clone(int argc, char **argv)
                size_t size;
 
                vdi_show_progress(idx * SD_DATA_OBJ_SIZE, inode->vdi_size);
-               if (inode->data_vdi_id[idx]) {
-                       oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
+               vdi_id = sd_inode_get_vdi(inode, idx);
+               if (vdi_id) {
+                       oid = vid_to_data_oid(vdi_id, idx);
                        ret = sd_read_object(oid, buf, SD_DATA_OBJ_SIZE, 0, 
true);
                        if (ret) {
                                ret = EXIT_FAILURE;
@@ -1192,6 +1172,7 @@ static int vdi_read(int argc, char **argv)
        int ret, idx;
        struct sd_inode *inode = NULL;
        uint64_t offset = 0, oid, done = 0, total = (uint64_t) -1;
+       uint32_t vdi_id;
        unsigned int len;
        char *buf = NULL;
 
@@ -1226,9 +1207,9 @@ static int vdi_read(int argc, char **argv)
        offset %= SD_DATA_OBJ_SIZE;
        while (done < total) {
                len = min(total - done, SD_DATA_OBJ_SIZE - offset);
-
-               if (inode->data_vdi_id[idx]) {
-                       oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
+               vdi_id = sd_inode_get_vdi(inode, idx);
+               if (vdi_id) {
+                       oid = vid_to_data_oid(vdi_id, idx);
                        ret = sd_read_object(oid, buf, len, offset, false);
                        if (ret != SD_RES_SUCCESS) {
                                sd_err("Failed to read VDI");
@@ -1261,7 +1242,7 @@ out:
 static int vdi_write(int argc, char **argv)
 {
        const char *vdiname = argv[optind++];
-       uint32_t vid, flags;
+       uint32_t vid, flags, vdi_id;
        int ret, idx;
        struct sd_inode *inode = NULL;
        uint64_t offset = 0, oid, old_oid, done = 0, total = (uint64_t) -1;
@@ -1302,11 +1283,12 @@ static int vdi_write(int argc, char **argv)
                flags = 0;
                len = min(total - done, SD_DATA_OBJ_SIZE - offset);
 
-               if (!inode->data_vdi_id[idx])
+               vdi_id = sd_inode_get_vdi(inode, idx);
+               if (!vdi_id)
                        create = true;
                else if (!is_data_obj_writeable(inode, idx)) {
                        create = true;
-                       old_oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
+                       old_oid = vid_to_data_oid(vdi_id, idx);
                }
 
                if (vdi_cmd_data.writeback)
@@ -1323,8 +1305,8 @@ static int vdi_write(int argc, char **argv)
                        total = done + len;
                }
 
-               inode->data_vdi_id[idx] = inode->vdi_id;
-               oid = vid_to_data_oid(inode->data_vdi_id[idx], idx);
+               sd_inode_set_vdi(inode, idx, inode->vdi_id);
+               oid = vid_to_data_oid(inode->vdi_id, idx);
                ret = sd_write_object(oid, old_oid, buf, len, offset, flags,
                                      inode->nr_copies, inode->copy_policy,
                                      create, false);
@@ -1685,7 +1667,7 @@ int do_vdi_check(const struct sd_inode *inode)
        max_idx = count_data_objs(inode);
        vdi_show_progress(done, inode->vdi_size);
        for (int idx = 0; idx < max_idx; idx++) {
-               vid = inode->data_vdi_id[idx];
+               vid = sd_inode_get_vdi(inode, idx);
                if (vid) {
                        oid = vid_to_data_oid(vid, idx);
                        queue_vdi_check_work(inode, oid, &done, wq);
@@ -1840,8 +1822,8 @@ static int vdi_backup(int argc, char **argv)
        }
 
        for (idx = 0; idx < nr_objs; idx++) {
-               uint32_t from_vid = from_inode->data_vdi_id[idx];
-               uint32_t to_vid = to_inode->data_vdi_id[idx];
+               uint32_t from_vid = sd_inode_get_vdi(from_inode, idx);
+               uint32_t to_vid = sd_inode_get_vdi(to_inode, idx);
 
                if (to_vid == 0 && from_vid == 0)
                        continue;
@@ -1893,7 +1875,7 @@ static int restore_obj(struct obj_backup *backup, 
uint32_t vid,
                       struct sd_inode *parent_inode)
 {
        int ret;
-       uint32_t parent_vid = parent_inode->data_vdi_id[backup->idx];
+       uint32_t parent_vid = sd_inode_get_vdi(parent_inode, backup->idx);
        uint64_t parent_oid = 0;
 
        if (parent_vid)
diff --git a/include/sheepdog_proto.h b/include/sheepdog_proto.h
index 7138608..d393460 100644
--- a/include/sheepdog_proto.h
+++ b/include/sheepdog_proto.h
@@ -235,6 +235,10 @@ struct sheepdog_vdi_attr {
        char value[SD_MAX_VDI_ATTR_VALUE_LEN];
 };
 
+uint32_t sd_inode_get_vdi(const struct sd_inode *inode, int idx);
+void sd_inode_set_vdi(struct sd_inode *inode, int idx, uint32_t vdi_id);
+void sd_inode_copy_vdis(struct sd_inode *oldi, struct sd_inode *newi);
+
 /* 64 bit FNV-1a non-zero initial basis */
 #define FNV1A_64_INIT ((uint64_t) 0xcbf29ce484222325ULL)
 #define FNV_64_PRIME ((uint64_t) 0x100000001b3ULL)
@@ -325,7 +329,7 @@ static inline uint64_t hash_64(uint64_t val, unsigned int 
bits)
 static inline bool is_data_obj_writeable(const struct sd_inode *inode,
                                         int idx)
 {
-       return inode->vdi_id == inode->data_vdi_id[idx];
+       return inode->vdi_id == sd_inode_get_vdi(inode, idx);
 }
 
 static inline bool is_vdi_obj(uint64_t oid)
diff --git a/lib/Makefile.am b/lib/Makefile.am
index b6ac290..a681167 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -5,7 +5,7 @@ AM_CPPFLAGS                = -I$(top_builddir)/include 
-I$(top_srcdir)/include
 noinst_LIBRARIES       = libsheepdog.a
 
 libsheepdog_a_SOURCES  = event.c logger.c net.c util.c rbtree.c strbuf.c \
-                         sha1.c option.c work.c sockfd_cache.c fec.c
+                         sha1.c option.c work.c sockfd_cache.c fec.c sd_inode.c
 
 if BUILD_SHA1_HW
 libsheepdog_a_SOURCES  += sha1_ssse3.S
diff --git a/lib/sd_inode.c b/lib/sd_inode.c
new file mode 100644
index 0000000..65a472a
--- /dev/null
+++ b/lib/sd_inode.c
@@ -0,0 +1,13 @@
+#include <string.h>
+
+#include "sheepdog_proto.h"
+
+uint32_t sd_inode_get_vdi(const struct sd_inode *inode, int idx)
+{
+       return inode->data_vdi_id[idx];
+}
+
+void sd_inode_set_vdi(struct sd_inode *inode, int idx, uint32_t vdi_id)
+{
+       inode->data_vdi_id[idx] = vdi_id;
+}
diff --git a/sheep/vdi.c b/sheep/vdi.c
index 221905d..27227b7 100644
--- a/sheep/vdi.c
+++ b/sheep/vdi.c
@@ -833,13 +833,14 @@ static void delete_one(struct work *work)
        nr_objs = count_data_objs(inode);
        for (nr_deleted = 0, i = 0; i < nr_objs; i++) {
                uint64_t oid;
+               uint32_t vid = sd_inode_get_vdi(inode, i);
 
-               if (!inode->data_vdi_id[i])
+               if (!vid)
                        continue;
 
-               oid = vid_to_data_oid(inode->data_vdi_id[i], i);
+               oid = vid_to_data_oid(vid, i);
 
-               if (inode->data_vdi_id[i] != inode->vdi_id) {
+               if (vid != inode->vdi_id) {
                        sd_debug("object %" PRIx64 " is base's data, would"
                                 " not be deleted.", oid);
                        continue;
diff --git a/sheepfs/volume.c b/sheepfs/volume.c
index ca8925a..ab16ada 100644
--- a/sheepfs/volume.c
+++ b/sheepfs/volume.c
@@ -117,7 +117,7 @@ static int volume_rw_object(char *buf, uint64_t oid, size_t 
size,
        struct sd_rsp *rsp = (struct sd_rsp *)&hdr;
        int ret, fd, sock_idx;
        bool create = false;
-       uint32_t vid = oid_to_vid(oid);
+       uint32_t vid = oid_to_vid(oid), vdi_id;
        struct vdi_inode *vdi;
        unsigned long idx = 0;
        uint64_t cow_oid = 0;
@@ -129,7 +129,8 @@ static int volume_rw_object(char *buf, uint64_t oid, size_t 
size,
        if (is_data_obj(oid)) {
                idx = data_oid_to_idx(oid);
                assert(vdi);
-               if (!vdi->inode->data_vdi_id[idx]) {
+               vdi_id = sd_inode_get_vdi(vdi->inode, idx);
+               if (!vdi_id) {
                        /* if object doesn't exist, we'er done */
                        if (rw == VOLUME_READ) {
                                memset(buf, 0, size);
@@ -138,14 +139,10 @@ static int volume_rw_object(char *buf, uint64_t oid, 
size_t size,
                        create = true;
                } else {
                        if (rw == VOLUME_READ) {
-                               oid = vid_to_data_oid(
-                                       vdi->inode->data_vdi_id[idx],
-                                       idx);
+                               oid = vid_to_data_oid(vdi_id, idx);
                        /* in case we are writing a COW object */
                        } else if (!is_data_obj_writeable(vdi->inode, idx)) {
-                               cow_oid = vid_to_data_oid(
-                                               vdi->inode->data_vdi_id[idx],
-                                               idx);
+                               cow_oid = vid_to_data_oid(vdi_id, idx);
                                hdr.flags |= SD_FLAG_CMD_COW;
                                create = true;
                        }
@@ -179,7 +176,7 @@ static int volume_rw_object(char *buf, uint64_t oid, size_t 
size,
        }
 
        if (create) {
-               vdi->inode->data_vdi_id[idx] = vid;
+               sd_inode_set_vdi(vdi->inode, idx, vid);
                /* writeback inode update */
                if (volume_rw_object((char *)&vid, vid_to_vdi_oid(vid),
                                     sizeof(vid),
-- 
1.7.1

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

Reply via email to