diff mbox series

[v3,4/7] block: don't use constant 512 as sector size in crypto driver

Message ID 20170912112855.24269-5-berrange@redhat.com
State New
Headers show
Series Misc improvements to crypto block driver | expand

Commit Message

Daniel P. Berrangé Sept. 12, 2017, 11:28 a.m. UTC
Use the qcrypto_block_get_sector_size() value in the block
crypto driver instead of hardcoding 512 as the sector size.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
 block/crypto.c | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)

Comments

Max Reitz Sept. 16, 2017, 4:24 p.m. UTC | #1
On 2017-09-12 13:28, Daniel P. Berrange wrote:
> Use the qcrypto_block_get_sector_size() value in the block
> crypto driver instead of hardcoding 512 as the sector size.
> 
> Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
> ---
>  block/crypto.c | 34 ++++++++++++++++++----------------
>  1 file changed, 18 insertions(+), 16 deletions(-)
> 
> diff --git a/block/crypto.c b/block/crypto.c
> index d68cbac2ac..49d6d4c058 100644
> --- a/block/crypto.c
> +++ b/block/crypto.c
> @@ -392,8 +392,9 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num,
>      uint8_t *cipher_data = NULL;
>      QEMUIOVector hd_qiov;
>      int ret = 0;
> +    uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block);
>      uint64_t payload_offset =
> -        qcrypto_block_get_payload_offset(crypto->block) / 512;
> +        qcrypto_block_get_payload_offset(crypto->block) / sector_size;
>      assert(payload_offset < (INT64_MAX / 512));
>  
>      qemu_iovec_init(&hd_qiov, qiov->niov);
> @@ -401,9 +402,9 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num,
>      /* Bounce buffer because we don't wish to expose cipher text
>       * in qiov which points to guest memory.
>       */
> -    cipher_data =
> -        qemu_try_blockalign(bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * 512,
> -                                              qiov->size));
> +    cipher_data = qemu_try_blockalign(
> +        bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * sector_size,
> +                          qiov->size));
>      if (cipher_data == NULL) {
>          ret = -ENOMEM;
>          goto cleanup;
> @@ -417,7 +418,7 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num,
>          }
>  
>          qemu_iovec_reset(&hd_qiov);
> -        qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * 512);
> +        qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * sector_size);

cur_nr_sectors is based on remaining_sectors; which in turn is a
parameter to this function and comes from the block layer.  Therefore
its unit is BDRV_SECTOR_SIZE and not the crypto driver's sector size.

Same in the hunk below, and in block_crypto_co_writev().

>  
>          ret = bdrv_co_readv(bs->file,
>                              payload_offset + sector_num,

Same thing here, albeit in a different variation: The unit of this
parameter of bdrv_co_readv() (start sector index) is a block layer
sector, whose size is always BDRV_SECTOR_SIZE.

Therefore you cannot divide the result from
qcrypto_block_get_payload_offset() by the crypto driver's sector size
and then use it as a sector index here.

Same in block_crypto_co_writev().


I assume that you fix this in the next patch, but for now it's just wrong.

Max
Eric Blake Sept. 18, 2017, 1:57 p.m. UTC | #2
On 09/12/2017 06:28 AM, Daniel P. Berrange wrote:
> Use the qcrypto_block_get_sector_size() value in the block
> crypto driver instead of hardcoding 512 as the sector size.
> 
> Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
> ---
>  block/crypto.c | 34 ++++++++++++++++++----------------
>  1 file changed, 18 insertions(+), 16 deletions(-)
> 
> diff --git a/block/crypto.c b/block/crypto.c
> index d68cbac2ac..49d6d4c058 100644
> --- a/block/crypto.c
> +++ b/block/crypto.c
> @@ -392,8 +392,9 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num,
>      uint8_t *cipher_data = NULL;
>      QEMUIOVector hd_qiov;
>      int ret = 0;
> +    uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block);
>      uint64_t payload_offset =
> -        qcrypto_block_get_payload_offset(crypto->block) / 512;
> +        qcrypto_block_get_payload_offset(crypto->block) / sector_size;
>      assert(payload_offset < (INT64_MAX / 512));

In addition to Max's comments, should the assert be dividing by
sector_size instead of 512?
Daniel P. Berrangé Sept. 18, 2017, 3:02 p.m. UTC | #3
On Sat, Sep 16, 2017 at 06:24:56PM +0200, Max Reitz wrote:
> On 2017-09-12 13:28, Daniel P. Berrange wrote:
> > Use the qcrypto_block_get_sector_size() value in the block
> > crypto driver instead of hardcoding 512 as the sector size.
> > 
> > Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
> > ---
> >  block/crypto.c | 34 ++++++++++++++++++----------------
> >  1 file changed, 18 insertions(+), 16 deletions(-)
> > 
> > diff --git a/block/crypto.c b/block/crypto.c
> > index d68cbac2ac..49d6d4c058 100644
> > --- a/block/crypto.c
> > +++ b/block/crypto.c
> > @@ -392,8 +392,9 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num,
> >      uint8_t *cipher_data = NULL;
> >      QEMUIOVector hd_qiov;
> >      int ret = 0;
> > +    uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block);
> >      uint64_t payload_offset =
> > -        qcrypto_block_get_payload_offset(crypto->block) / 512;
> > +        qcrypto_block_get_payload_offset(crypto->block) / sector_size;
> >      assert(payload_offset < (INT64_MAX / 512));
> >  
> >      qemu_iovec_init(&hd_qiov, qiov->niov);
> > @@ -401,9 +402,9 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num,
> >      /* Bounce buffer because we don't wish to expose cipher text
> >       * in qiov which points to guest memory.
> >       */
> > -    cipher_data =
> > -        qemu_try_blockalign(bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * 512,
> > -                                              qiov->size));
> > +    cipher_data = qemu_try_blockalign(
> > +        bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * sector_size,
> > +                          qiov->size));
> >      if (cipher_data == NULL) {
> >          ret = -ENOMEM;
> >          goto cleanup;
> > @@ -417,7 +418,7 @@ block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num,
> >          }
> >  
> >          qemu_iovec_reset(&hd_qiov);
> > -        qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * 512);
> > +        qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * sector_size);
> 
> cur_nr_sectors is based on remaining_sectors; which in turn is a
> parameter to this function and comes from the block layer.  Therefore
> its unit is BDRV_SECTOR_SIZE and not the crypto driver's sector size.
> 
> Same in the hunk below, and in block_crypto_co_writev().
> 
> >  
> >          ret = bdrv_co_readv(bs->file,
> >                              payload_offset + sector_num,
> 
> Same thing here, albeit in a different variation: The unit of this
> parameter of bdrv_co_readv() (start sector index) is a block layer
> sector, whose size is always BDRV_SECTOR_SIZE.
> 
> Therefore you cannot divide the result from
> qcrypto_block_get_payload_offset() by the crypto driver's sector size
> and then use it as a sector index here.
> 
> Same in block_crypto_co_writev().
> 
> 
> I assume that you fix this in the next patch, but for now it's just wrong.

Yeah, in retrospect I should have kept this patch using BDRV_SECTOR_SIZE
throughout as previous versions had it, so I'm going to go back to doing
that. Only use the encryption sector size in a later patch where we have
already switched to doing byte based I/O.


Regards,
Daniel
diff mbox series

Patch

diff --git a/block/crypto.c b/block/crypto.c
index d68cbac2ac..49d6d4c058 100644
--- a/block/crypto.c
+++ b/block/crypto.c
@@ -392,8 +392,9 @@  block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num,
     uint8_t *cipher_data = NULL;
     QEMUIOVector hd_qiov;
     int ret = 0;
+    uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block);
     uint64_t payload_offset =
-        qcrypto_block_get_payload_offset(crypto->block) / 512;
+        qcrypto_block_get_payload_offset(crypto->block) / sector_size;
     assert(payload_offset < (INT64_MAX / 512));
 
     qemu_iovec_init(&hd_qiov, qiov->niov);
@@ -401,9 +402,9 @@  block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num,
     /* Bounce buffer because we don't wish to expose cipher text
      * in qiov which points to guest memory.
      */
-    cipher_data =
-        qemu_try_blockalign(bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * 512,
-                                              qiov->size));
+    cipher_data = qemu_try_blockalign(
+        bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * sector_size,
+                          qiov->size));
     if (cipher_data == NULL) {
         ret = -ENOMEM;
         goto cleanup;
@@ -417,7 +418,7 @@  block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num,
         }
 
         qemu_iovec_reset(&hd_qiov);
-        qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * 512);
+        qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * sector_size);
 
         ret = bdrv_co_readv(bs->file,
                             payload_offset + sector_num,
@@ -428,18 +429,18 @@  block_crypto_co_readv(BlockDriverState *bs, int64_t sector_num,
 
         if (qcrypto_block_decrypt(crypto->block,
                                   sector_num,
-                                  cipher_data, cur_nr_sectors * 512,
+                                  cipher_data, cur_nr_sectors * sector_size,
                                   NULL) < 0) {
             ret = -EIO;
             goto cleanup;
         }
 
         qemu_iovec_from_buf(qiov, bytes_done,
-                            cipher_data, cur_nr_sectors * 512);
+                            cipher_data, cur_nr_sectors * sector_size);
 
         remaining_sectors -= cur_nr_sectors;
         sector_num += cur_nr_sectors;
-        bytes_done += cur_nr_sectors * 512;
+        bytes_done += cur_nr_sectors * sector_size;
     }
 
  cleanup:
@@ -460,8 +461,9 @@  block_crypto_co_writev(BlockDriverState *bs, int64_t sector_num,
     uint8_t *cipher_data = NULL;
     QEMUIOVector hd_qiov;
     int ret = 0;
+    uint64_t sector_size = qcrypto_block_get_sector_size(crypto->block);
     uint64_t payload_offset =
-        qcrypto_block_get_payload_offset(crypto->block) / 512;
+        qcrypto_block_get_payload_offset(crypto->block) / sector_size;
     assert(payload_offset < (INT64_MAX / 512));
 
     qemu_iovec_init(&hd_qiov, qiov->niov);
@@ -469,9 +471,9 @@  block_crypto_co_writev(BlockDriverState *bs, int64_t sector_num,
     /* Bounce buffer because we're not permitted to touch
      * contents of qiov - it points to guest memory.
      */
-    cipher_data =
-        qemu_try_blockalign(bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * 512,
-                                              qiov->size));
+    cipher_data = qemu_try_blockalign(
+        bs->file->bs, MIN(BLOCK_CRYPTO_MAX_SECTORS * sector_size,
+                          qiov->size));
     if (cipher_data == NULL) {
         ret = -ENOMEM;
         goto cleanup;
@@ -485,18 +487,18 @@  block_crypto_co_writev(BlockDriverState *bs, int64_t sector_num,
         }
 
         qemu_iovec_to_buf(qiov, bytes_done,
-                          cipher_data, cur_nr_sectors * 512);
+                          cipher_data, cur_nr_sectors * sector_size);
 
         if (qcrypto_block_encrypt(crypto->block,
                                   sector_num,
-                                  cipher_data, cur_nr_sectors * 512,
+                                  cipher_data, cur_nr_sectors * sector_size,
                                   NULL) < 0) {
             ret = -EIO;
             goto cleanup;
         }
 
         qemu_iovec_reset(&hd_qiov);
-        qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * 512);
+        qemu_iovec_add(&hd_qiov, cipher_data, cur_nr_sectors * sector_size);
 
         ret = bdrv_co_writev(bs->file,
                              payload_offset + sector_num,
@@ -507,7 +509,7 @@  block_crypto_co_writev(BlockDriverState *bs, int64_t sector_num,
 
         remaining_sectors -= cur_nr_sectors;
         sector_num += cur_nr_sectors;
-        bytes_done += cur_nr_sectors * 512;
+        bytes_done += cur_nr_sectors * sector_size;
     }
 
  cleanup: