cow_header_v2 is read and written directly from the image file
with bdrv_pread()/bdrv_pwrite(), and as such should be packed to
avoid unintentional padding.

Also change struct cow_header_v2 to a typedef, and some minor
code style changes to keep checkpatch.pl happy.

Signed-off-by: Jeff Cody <jc...@redhat.com>
---
 block/cow.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/block/cow.c b/block/cow.c
index 909c3e7..9c15afb 100644
--- a/block/cow.c
+++ b/block/cow.c
@@ -32,14 +32,14 @@
 #define COW_MAGIC 0x4f4f4f4d  /* MOOO */
 #define COW_VERSION 2
 
-struct cow_header_v2 {
+typedef struct QEMU_PACKED cow_header_v2 {
     uint32_t magic;
     uint32_t version;
     char backing_file[1024];
     int32_t mtime;
     uint64_t size;
     uint32_t sectorsize;
-};
+} COWHeaderV2;
 
 typedef struct BDRVCowState {
     CoMutex lock;
@@ -48,21 +48,22 @@ typedef struct BDRVCowState {
 
 static int cow_probe(const uint8_t *buf, int buf_size, const char *filename)
 {
-    const struct cow_header_v2 *cow_header = (const void *)buf;
+    const COWHeaderV2 *cow_header = (const void *)buf;
 
-    if (buf_size >= sizeof(struct cow_header_v2) &&
+    if (buf_size >= sizeof(COWHeaderV2) &&
         be32_to_cpu(cow_header->magic) == COW_MAGIC &&
-        be32_to_cpu(cow_header->version) == COW_VERSION)
+        be32_to_cpu(cow_header->version) == COW_VERSION) {
         return 100;
-    else
+    } else {
         return 0;
+    }
 }
 
 static int cow_open(BlockDriverState *bs, QDict *options, int flags,
                     Error **errp)
 {
     BDRVCowState *s = bs->opaque;
-    struct cow_header_v2 cow_header;
+    COWHeaderV2 cow_header;
     int bitmap_size;
     int64_t size;
     int ret;
@@ -109,7 +110,7 @@ static int cow_open(BlockDriverState *bs, QDict *options, 
int flags,
  */
 static inline int cow_set_bit(BlockDriverState *bs, int64_t bitnum, bool 
*first)
 {
-    uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
+    uint64_t offset = sizeof(COWHeaderV2) + bitnum / 8;
     uint8_t bitmap;
     int ret;
 
@@ -172,7 +173,7 @@ static int cow_find_streak(const uint8_t *bitmap, int 
value, int start, int nb_s
 static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs,
         int64_t sector_num, int nb_sectors, int *num_same)
 {
-    int64_t bitnum = sector_num + sizeof(struct cow_header_v2) * 8;
+    int64_t bitnum = sector_num + sizeof(COWHeaderV2) * 8;
     uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE;
     uint8_t bitmap[BDRV_SECTOR_SIZE];
     int ret;
@@ -298,7 +299,7 @@ static void cow_close(BlockDriverState *bs)
 static int cow_create(const char *filename, QEMUOptionParameter *options,
                       Error **errp)
 {
-    struct cow_header_v2 cow_header;
+    COWHeaderV2 cow_header;
     struct stat st;
     int64_t image_sectors = 0;
     const char *image_filename = NULL;
-- 
1.8.3.1


Reply via email to