From c6bde4711e00386de73e8125eb95438b3372b801 Mon Sep 17 00:00:00 2001
From: Kevin Wolf <kwolf@redhat.com>
Date: Mon, 29 Mar 2010 08:54:48 -0300
Subject: [PATCH 2/4] Wrong error message in block_passwd command

RH-Author: Kevin Wolf <kwolf@redhat.com>
Message-id: <1269852888-20127-3-git-send-email-kwolf@redhat.com>
Patchwork-id: 8160
O-Subject: [PATCH 2/2] Wrong error message in block_passwd command
Bugzilla: 563641
RH-Acked-by: Juan Quintela <quintela@redhat.com>
RH-Acked-by: Luiz Capitulino <lcapitulino@redhat.com>
RH-Acked-by: Shahar Havivi <shaharh@redhat.com>

From: Shahar Havivi <shaharh@redhat.com>

Bugzilla: 563641
Upstream commit: fd04a2aedae37dc9f481225f445d04bddd1590b0

Signed-off-by: Shahar Havivi <shaharh@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block.c   |    7 +++++--
 monitor.c |    7 ++++++-
 2 files changed, 11 insertions(+), 3 deletions(-)

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 block.c   |    7 +++++--
 monitor.c |    7 ++++++-
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/block.c b/block.c
index 74a0e46..b5cf547 100644
--- a/block.c
+++ b/block.c
@@ -1056,8 +1056,11 @@ int bdrv_set_key(BlockDriverState *bs, const char *key)
         if (!bs->encrypted)
             return 0;
     }
-    if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
-        return -1;
+    if (!bs->encrypted) {
+        return -EINVAL;
+    } else if (!bs->drv || !bs->drv->bdrv_set_key) {
+        return -ENOMEDIUM;
+    }
     ret = bs->drv->bdrv_set_key(bs, key);
     if (ret < 0) {
         bs->valid_key = 0;
diff --git a/monitor.c b/monitor.c
index 5b92a80..3747466 100644
--- a/monitor.c
+++ b/monitor.c
@@ -1080,6 +1080,7 @@ static int do_block_set_passwd(Monitor *mon, const QDict *qdict,
                                 QObject **ret_data)
 {
     BlockDriverState *bs;
+    int err;
 
     bs = bdrv_find(qdict_get_str(qdict, "device"));
     if (!bs) {
@@ -1087,7 +1088,11 @@ static int do_block_set_passwd(Monitor *mon, const QDict *qdict,
         return -1;
     }
 
-    if (bdrv_set_key(bs, qdict_get_str(qdict, "password")) < 0) {
+    err = bdrv_set_key(bs, qdict_get_str(qdict, "password"));
+    if (err == -EINVAL) {
+        qemu_error_new(QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs));
+        return -1;
+    } else if (err < 0) {
         qemu_error_new(QERR_INVALID_PASSWORD);
         return -1;
     }
-- 
1.7.0.3

