diff mbox

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

Message ID 1359392845-15905-10-git-send-email-benoit@irqsave.net
State New
Headers show

Commit Message

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

Comments

Kevin Wolf Feb. 8, 2013, 12:20 p.m. UTC | #1
Am 28.01.2013 18:07, schrieb Benoît Canet:
> Signed-off-by: Benoit Canet <benoit@irqsave.net>
> ---
>  block/quorum.c |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 53 insertions(+)
> 
> diff --git a/block/quorum.c b/block/quorum.c
> index 1c50ed5..459434f 100644
> --- a/block/quorum.c
> +++ b/block/quorum.c
> @@ -134,6 +134,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,
> @@ -525,6 +541,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);

Maybe it would be better to not include the num_value of failed requests.

> +    }
> +
> +    winner = quorum_get_vote_winner(&result_votes);
> +    result = winner->value.l;
> +
> +    winner = quorum_get_vote_winner(&num_votes);
> +    *pnum = winner->value.l;

No comparison against the threshold?

> +
> +    quorum_free_vote_list(&result_votes);
> +    quorum_free_vote_list(&num_votes);
> +
> +    return result;
> +}
> +
>  static BlockDriver bdrv_quorum = {
>      .format_name        = "quorum",
>      .protocol_name      = "quorum",
> @@ -536,6 +588,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)
> 

Kevin
diff mbox

Patch

diff --git a/block/quorum.c b/block/quorum.c
index 1c50ed5..459434f 100644
--- a/block/quorum.c
+++ b/block/quorum.c
@@ -134,6 +134,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,
@@ -525,6 +541,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",
@@ -536,6 +588,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)