From patchwork Thu Sep 19 18:43:22 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Cody X-Patchwork-Id: 276043 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 49DE52C00C8 for ; Fri, 20 Sep 2013 04:44:18 +1000 (EST) Received: from localhost ([::1]:52599 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VMjD6-0003CT-C3 for incoming@patchwork.ozlabs.org; Thu, 19 Sep 2013 14:44:16 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39859) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VMjCX-000314-BX for qemu-devel@nongnu.org; Thu, 19 Sep 2013 14:43:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VMjCS-0000Uz-EU for qemu-devel@nongnu.org; Thu, 19 Sep 2013 14:43:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:19167) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VMjCS-0000Ut-6G for qemu-devel@nongnu.org; Thu, 19 Sep 2013 14:43:36 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r8JIhZV0005525 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 19 Sep 2013 14:43:35 -0400 Received: from localhost ([10.3.112.15]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r8JIhX5W019286 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Thu, 19 Sep 2013 14:43:34 -0400 From: Jeff Cody To: qemu-devel@nongnu.org Date: Thu, 19 Sep 2013 14:43:22 -0400 Message-Id: <61a5c47913cce201bbea67a973544805f7b13947.1379615569.git.jcody@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, stefanha@redhat.com Subject: [Qemu-devel] [PATCH 4/5] block: cow - used QEMU_PACKED for on-disk structures X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- 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;