Patchwork [RFC,V7,09/13] quorum: Add quorum_co_is_allocated.

login
register
mail settings
Submitter Benoît Canet
Date Jan. 28, 2013, 1:22 p.m.
Message ID <1359379379-6017-10-git-send-email-benoit@irqsave.net>
Download mbox | patch
Permalink /patch/216221/
State New
Headers show

Comments

Benoît Canet - Jan. 28, 2013, 1:22 p.m.
Signed-off-by: Benoit Canet <benoit@irqsave.net>
---
 block/quorum.c |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

Patch

diff --git a/block/quorum.c b/block/quorum.c
index 6254f95..ed97a76 100644
--- a/block/quorum.c
+++ b/block/quorum.c
@@ -135,6 +135,22 @@  static int quorum_sha256_compare(QuorumVoteValue *a, QuorumVoteValue *b)
     return memcmp(a, b, HASH_LENGTH);
 }
 
+static int quorum_long_compare(QuorumVoteValue *a, QuorumVoteValue *b)
+{
+    unsigned long i = a->l;
+    unsigned long j = b->l;
+
+    if (i < j) {
+        return -1;
+    }
+
+    if (i > j) {
+        return 1;
+    }
+
+    return 0;
+}
+
 static QuorumAIOCB *quorum_aio_get(BDRVQuorumState *s,
                                    BlockDriverState *bs,
                                    QEMUIOVector *qiov,
@@ -527,6 +543,42 @@  static void quorum_invalidate_cache(BlockDriverState *bs)
     }
 }
 
+static int coroutine_fn quorum_co_is_allocated(BlockDriverState *bs,
+                                               int64_t sector_num,
+                                               int nb_sectors,
+                                               int *pnum)
+{
+    BDRVQuorumState *s = bs->opaque;
+    QuorumVoteVersion *winner = NULL;
+    QuorumVotes result_votes, num_votes;
+    QuorumVoteValue result_value, num_value;
+    int i, result = 0, num;
+
+    QLIST_INIT(&result_votes.vote_list);
+    QLIST_INIT(&num_votes.vote_list);
+    result_votes.compare = quorum_long_compare;
+    num_votes.compare = quorum_long_compare;
+
+    for (i = 0; i < s->total; i++) {
+        result = bdrv_co_is_allocated(s->bs[i], sector_num, nb_sectors, &num);
+        result_value.l = result;
+        num_value.l = num;
+        quorum_count_vote(&result_votes, &result_value, i);
+        quorum_count_vote(&num_votes, &num_value, i);
+    }
+
+    winner = quorum_get_vote_winner(&result_votes);
+    result = winner->value.l;
+
+    winner = quorum_get_vote_winner(&num_votes);
+    *pnum = winner->value.l;
+
+    quorum_free_vote_list(&result_votes);
+    quorum_free_vote_list(&num_votes);
+
+    return result;
+}
+
 static BlockDriver bdrv_quorum = {
     .format_name        = "quorum",
     .protocol_name      = "quorum",
@@ -538,6 +590,7 @@  static BlockDriver bdrv_quorum = {
     .bdrv_aio_readv     = quorum_aio_readv,
     .bdrv_aio_writev    = quorum_aio_writev,
     .bdrv_invalidate_cache = quorum_invalidate_cache,
+    .bdrv_co_is_allocated  = quorum_co_is_allocated,
 };
 
 static void bdrv_quorum_init(void)